XRootD
XrdOssMirage.cc
Go to the documentation of this file.
1 #include "XrdOssMirage.hh"
2 #include "XrdOssMirageDir.hh"
3 #include "XrdOssMirageFile.hh"
4 #include "XrdOssMirageXAttr.hh"
5 #include "XrdVersion.hh"
6 
7 #include "XrdSys/XrdSysFAttr.hh"
8 
10 
11 extern "C"
12 {
13  XrdOss *XrdOssGetStorageSystem(XrdOss* native_oss, XrdSysLogger* lp, const char* config_fn, const char* parms)
14  {
15  return static_cast<XrdOss *>(new XrdOssMirage);
16  }
17 }
18 
20 {
21  return new XrdOssMirageDir;
22 }
23 
25 {
26  return new XrdOssMirageFile(*this);
27 }
28 
29 int XrdOssMirage::Chmod(const char * path, mode_t mode, XrdOucEnv *envP)
30 {
31  return -ENOTSUP;
32 }
33 
34 int XrdOssMirage::Create(const char *tid, const char *path, mode_t mode, XrdOucEnv &env, int opts)
35 {
36  const std::lock_guard lock(mutex);
37 
38  if (has_entry(path))
39  {
40  if (opts & XRDOSS_new)
41  return -EEXIST;
42 
43  if (is_entry_being_written(path))
44  return -EBUSY;
45  }
46 
47  // preserve previous configuration but reset the size in case it already exists
48  entries.try_emplace(path, std::make_shared<XrdOssMirageEntry>());
49  entries[path]->size = 0;
50 
51  static std::once_flag xattr_injection_flag;
52  std::call_once(xattr_injection_flag, [this]() noexcept
53  {
54  if (XrdOssMirageXAttr * const xattr = dynamic_cast<XrdOssMirageXAttr*>(XrdSysFAttr::Xat); xattr != nullptr)
55  xattr->setOss(*this);
56  });
57 
58  return XrdOssOK;
59 }
60 
62 {
63  return XRDOSS_HASNAIO;
64 }
65 
66 int XrdOssMirage::Init(XrdSysLogger *lp, const char *cfn)
67 {
68  return XrdOssOK;
69 }
70 
71 int XrdOssMirage::Mkdir(const char *path, mode_t mode, int mkpath, XrdOucEnv *envP)
72 {
73  return -ENOTSUP;
74 }
75 
76 int XrdOssMirage::Remdir(const char *path, int Opts, XrdOucEnv *envP)
77 {
78  return -ENOTSUP;
79 }
80 
81 int XrdOssMirage::Rename(const char *oPath, const char *nPath, XrdOucEnv *oEnvP, XrdOucEnv *nEnvP)
82 {
83  const std::lock_guard lock(mutex);
84 
85  if (!has_entry(oPath))
86  return -ENOENT;
87 
88  if (has_entry(nPath))
89  return -EEXIST;
90 
91  entries[nPath] = std::move(entries[oPath]);
92  entries.erase(oPath);
93 
94  return XrdOssOK;
95 }
96 
97 int XrdOssMirage::Stat(const char *path, struct stat *buff, int opts, XrdOucEnv *envP)
98 {
99  const std::lock_guard lock(mutex);
100 
101  if (!has_entry(path))
102  return -ENOENT;
103 
104  *buff = {};
105  buff->st_size = entries[path]->size;
106 
107  return XrdOssOK;
108 }
109 
110 int XrdOssMirage::Truncate(const char *path, unsigned long long fsize, XrdOucEnv *envP)
111 {
112  const std::lock_guard lock(mutex);
113 
114  if (!has_entry(path))
115  return -ENOENT;
116 
117  if (is_entry_being_written(path))
118  return -EBUSY;
119 
120  entries[path]->size = fsize;
121 
122  return XrdOssOK;
123 }
124 
125 int XrdOssMirage::Unlink(const char *path, int Opts, XrdOucEnv *envP)
126 {
127  const std::lock_guard lock(mutex);
128 
129  if (!has_entry(path))
130  return -ENOENT;
131 
132  entries.erase(path);
133 
134  return XrdOssOK;
135 }
136 
137 std::optional<XrdOssMirageEntry> XrdOssMirage::get_entry_read(const char *path)
138 {
139  const std::lock_guard lock(mutex);
140 
141  if (!has_entry(path) || is_entry_being_written(path))
142  return {};
143 
144  return *entries[path];
145 }
146 
147 std::optional<XrdOssMirageEntryPtr> XrdOssMirage::get_entry_write(const char *path)
148 {
149  const std::lock_guard lock(mutex);
150 
151  if (!has_entry(path) || is_entry_being_written(path))
152  return {};
153 
154  return entries[path];
155 }
156 
157 bool XrdOssMirage::has_entry(const char *path)
158 {
159  return entries.find(path) != entries.end();
160 }
161 
162 bool XrdOssMirage::is_entry_being_written(const char *path)
163 {
164  return entries[path].use_count() > 1;
165 }
#define tident
XrdVERSIONINFO(XrdOssGetStorageSystem, XrdOssMirage)
XrdOss * XrdOssGetStorageSystem(XrdOss *native_oss, XrdSysLogger *lp, const char *config_fn, const char *parms)
Definition: XrdOssMirage.cc:13
#define XrdOssOK
Definition: XrdOss.hh:54
#define XRDOSS_new
Definition: XrdOss.hh:527
#define XRDOSS_HASNAIO
Definition: XrdOss.hh:541
#define stat(a, b)
Definition: XrdPosix.hh:105
struct myOpts opts
virtual int Stat(const char *path, struct stat *buff, int opts=0, XrdOucEnv *envP=0) override
Definition: XrdOssMirage.cc:97
virtual int Init(XrdSysLogger *lp, const char *cfn) override
Definition: XrdOssMirage.cc:66
virtual XrdOssDF * newDir(const char *tident) override
Definition: XrdOssMirage.cc:19
virtual int Chmod(const char *path, mode_t mode, XrdOucEnv *envP=0) override
Definition: XrdOssMirage.cc:29
std::optional< XrdOssMirageEntry > get_entry_read(const char *path)
virtual int Create(const char *tid, const char *path, mode_t mode, XrdOucEnv &env, int opts=0) override
Definition: XrdOssMirage.cc:34
virtual int Mkdir(const char *path, mode_t mode, int mkpath=0, XrdOucEnv *envP=0) override
Definition: XrdOssMirage.cc:71
virtual int Rename(const char *oPath, const char *nPath, XrdOucEnv *oEnvP=0, XrdOucEnv *nEnvP=0) override
Definition: XrdOssMirage.cc:81
virtual int Truncate(const char *path, unsigned long long fsize, XrdOucEnv *envP=0) override
std::optional< XrdOssMirageEntryPtr > get_entry_write(const char *path)
virtual XrdOssDF * newFile(const char *tident) override
Definition: XrdOssMirage.cc:24
virtual int Remdir(const char *path, int Opts=0, XrdOucEnv *envP=0) override
Definition: XrdOssMirage.cc:76
virtual int Unlink(const char *path, int Opts=0, XrdOucEnv *envP=0) override
virtual uint64_t Features() override
Definition: XrdOssMirage.cc:61
static XrdSysXAttr * Xat
Definition: XrdSysFAttr.hh:51
int Opts
Definition: XrdMpxStats.cc:58
XrdOucEnv * envP
Definition: XrdPss.cc:110