XRootD
XrdThrottleFileSystemConfig.cc
Go to the documentation of this file.
1 
2 #include <fcntl.h>
3 
4 #include "XrdSys/XrdSysPlugin.hh"
5 #include "XrdSys/XrdSysLogger.hh"
6 #include "XrdOuc/XrdOuca2x.hh"
7 #include "XrdOuc/XrdOucEnv.hh"
8 #include "XrdOuc/XrdOucStream.hh"
9 
13 
14 using namespace XrdThrottle;
15 
16 #define OFS_NAME "libXrdOfs.so"
17 
18 /*
19  * Note nothing in this file is thread-safe.
20  */
21 
22 static XrdSfsFileSystem *
23 LoadFS(const std::string &fslib, XrdSysError &eDest, const std::string &config_file){
24  // Load the library
25  XrdSysPlugin ofsLib(&eDest, fslib.c_str(), "fslib", NULL);
26  XrdSfsFileSystem *fs;
27  if (fslib == OFS_NAME)
28  {
30  XrdSysLogger *lp,
31  const char *configfn,
32  XrdOucEnv *EnvInfo);
33 
34  if (!(fs = XrdSfsGetDefaultFileSystem(0, eDest.logger(), config_file.c_str(), 0)))
35  {
36  eDest.Emsg("Config", "Unable to load OFS filesystem.");
37  }
38  }
39  else
40  {
41  XrdSfsFileSystem *(*ep)(XrdSfsFileSystem *, XrdSysLogger *, const char *);
42  if (!(ep = (XrdSfsFileSystem *(*)(XrdSfsFileSystem *,XrdSysLogger *,const char *))
43  ofsLib.getPlugin("XrdSfsGetFileSystem")))
44  return NULL;
45  if (!(fs = (*ep)(0, eDest.logger(), config_file.c_str())))
46  {
47  eDest.Emsg("Config", "Unable to create file system object via", fslib.c_str());
48  return NULL;
49  }
50  }
51  ofsLib.Persist();
52 
53  return fs;
54 }
55 
56 namespace XrdThrottle {
59  XrdSysLogger *lp,
60  const char *configfn,
61  XrdOucEnv *envP)
62 {
63  FileSystem* fs = NULL;
64  if (envP && envP->GetInt("XrdOssThrottle") == 1) {
65  XrdSysError eDest(lp, "XrdOssThrottle");
66  eDest.Emsg("Config", "XrdOssThrottle is loaded; not stacking XrdThrottle on OFS. "
67  "This is a warning for backward compatability; this configuration may generate an "
68  "error in the future.");
69  return native_fs;
70  }
71  FileSystem::Initialize(fs, native_fs, lp, configfn, envP);
72  return fs;
73 }
74 }
75 
76 // Export the symbol necessary for this to be dynamically loaded.
77 extern "C" {
80  XrdSysLogger *lp,
81  const char *configfn)
82 {
83  return XrdSfsGetFileSystem_Internal(native_fs, lp, configfn, nullptr);
84 }
85 
88  XrdSysLogger *lp,
89  const char *configfn,
90  XrdOucEnv *envP)
91 {
92  return XrdSfsGetFileSystem_Internal(native_fs, lp, configfn, envP);
93 }
94 }
95 
98 
99 FileSystem* FileSystem::m_instance = 0;
100 
101 FileSystem::FileSystem()
102  : m_eroute(0), m_trace(&m_eroute), m_sfs_ptr(0), m_initialized(false), m_throttle(&m_eroute, &m_trace)
103 {
104  myVersion = &XrdVERSIONINFOVAR(XrdSfsGetFileSystem);
105 }
106 
107 FileSystem::~FileSystem() {}
108 
109 void
110 FileSystem::Initialize(FileSystem *&fs,
111  XrdSfsFileSystem *native_fs,
112  XrdSysLogger *lp,
113  const char *configfn,
114  XrdOucEnv *envP)
115 {
116  fs = NULL;
117  if (m_instance == NULL && !(m_instance = new FileSystem()))
118  {
119  return;
120  }
121  fs = m_instance;
122  if (!fs->m_initialized)
123  {
124  fs->m_config_file = configfn;
125  fs->m_eroute.logger(lp);
126  fs->m_eroute.Say("Initializing a Throttled file system.");
127  if (fs->Configure(fs->m_eroute, native_fs, envP))
128  {
129  fs->m_eroute.Say("Initialization of throttled file system failed.");
130  fs = NULL;
131  return;
132  }
133  fs->m_throttle.Init();
134  fs->m_initialized = true;
135  }
136 }
137 
138 #define TS_Xeq(key, func) NoGo = (strcmp(key, var) == 0) ? func(Config) : 0
139 int
140 FileSystem::Configure(XrdSysError & log, XrdSfsFileSystem *native_fs, XrdOucEnv *envP)
141 {
143  if (Config.Configure(m_config_file))
144  {
145  log.Emsg("Config", "Unable to load configuration file", m_config_file.c_str());
146  return 1;
147  }
148 
149  m_throttle.FromConfig(Config);
150  m_trace.What = Config.GetTraceLevels();
151 
152  // Load the filesystem object.
153  m_sfs_ptr = native_fs ? native_fs : LoadFS(Config.GetFileSystemLibrary(), m_eroute, m_config_file);
154  if (!m_sfs_ptr) return 1;
155 
156  // Overwrite the environment variable saying that throttling is the fslib.
157  XrdOucEnv::Export("XRDOFSLIB", Config.GetFileSystemLibrary().c_str());
158 
159  if (envP)
160  {
161  auto gstream = reinterpret_cast<XrdXrootdGStream*>(envP->GetPtr("Throttle.gStream*"));
162  log.Say("Config", "Throttle g-stream has", gstream ? "" : " NOT", " been configured via xrootd.mongstream directive");
163  m_throttle.SetMonitor(gstream);
164  }
165 
166  // The Feature function is not a virtual but implemented by the base class to
167  // look at a protected member. Thus, to forward the call, we need to copy
168  // from the underlying filesystem
169  FeatureSet = m_sfs_ptr->Features();
170  return 0;
171 }
static XrdSysError eDest(0,"crypto_")
XrdSfsFileSystem * XrdSfsGetDefaultFileSystem(XrdSfsFileSystem *native_fs, XrdSysLogger *lp, const char *configfn, XrdOucEnv *EnvInfo)
Definition: XrdOfsFS.cc:49
static XrdSfsFileSystem * LoadFS(const std::string &fslib, XrdSysError &eDest, const std::string &config_file)
#define OFS_NAME
XrdSfsFileSystem * XrdSfsGetFileSystem2(XrdSfsFileSystem *native_fs, XrdSysLogger *lp, const char *configfn, XrdOucEnv *envP)
XrdSfsFileSystem * XrdSfsGetFileSystem(XrdSfsFileSystem *native_fs, XrdSysLogger *lp, const char *configfn)
XrdVERSIONINFO(XrdSfsGetFileSystem, FileSystem)
long GetInt(const char *varname)
Definition: XrdOucEnv.cc:253
static int Export(const char *Var, const char *Val)
Definition: XrdOucEnv.cc:188
void * GetPtr(const char *varname)
Definition: XrdOucEnv.cc:281
uint64_t FeatureSet
Adjust features at initialization.
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
void Say(const char *text1, const char *text2=0, const char *txt3=0, const char *text4=0, const char *text5=0, const char *txt6=0)
Definition: XrdSysError.cc:141
XrdSysLogger * logger(XrdSysLogger *lp=0)
Definition: XrdSysError.hh:141
void * getPlugin(const char *pname, int optional=0)
void * Persist()
void FromConfig(XrdThrottle::Configuration &config)
void SetMonitor(XrdXrootdGStream *gstream)
XrdVersionInfo myVersion
XrdCmsConfig Config
XrdOucEnv * envP
Definition: XrdPss.cc:109
XrdSfsFileSystem * XrdSfsGetFileSystem_Internal(XrdSfsFileSystem *native_fs, XrdSysLogger *lp, const char *configfn, XrdOucEnv *envP)