XRootD
XrdSsiFile.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d S s i F i l e . c c */
4 /* */
5 /* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* Produced by Andrew Hanushevsky for Stanford University under contract */
7 /* DE-AC02-76-SFO0515 with the Department of Energy */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19 /* License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24 /* */
25 /* The copyright holder's institutional names and contributor's names may not */
26 /* be used to endorse or promote products derived from this software without */
27 /* specific prior written permission of the institution or contributor. */
28 /******************************************************************************/
29 
30 #include <cerrno>
31 #include <fcntl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/uio.h>
35 
36 #include "XrdOuc/XrdOucBuffer.hh"
37 #include "XrdOuc/XrdOucEnv.hh"
38 #include "XrdOuc/XrdOucPList.hh"
39 
40 #include "XrdSfs/XrdSfsAio.hh"
41 
42 #include "XrdSsi/XrdSsiFile.hh"
43 #include "XrdSsi/XrdSsiFileSess.hh"
44 #include "XrdSsi/XrdSsiSfs.hh"
45 #include "XrdSsi/XrdSsiUtils.hh"
46 
47 /******************************************************************************/
48 /* G l o b a l s */
49 /******************************************************************************/
50 
51 namespace XrdSsi
52 {
56 extern bool fsChk;
57 };
58 
59 using namespace XrdSsi;
60 
61 /******************************************************************************/
62 /* X r d S s i F i l e D e s t r u c t o r */
63 /******************************************************************************/
64 
66 {
67 
68 // If we have a file object then delete it -- it needs to close. Else do it.
69 //
70  if (fsFile) delete fsFile;
71  if (fSessP) fSessP->Recycle();
72 }
73 
74 /******************************************************************************/
75 /* c l o s e */
76 /******************************************************************************/
77 
79 /*
80  Function: Close the file object.
81 
82  Input: None
83 
84  Output: Always returns SFS_OK for SSI or whatever SfsFile returns
85 */
86 {
87 
88 // Route this request as needed
89 //
90  if (fsFile) return fsFile->close();
91 
92 // Forward this to the file session object
93 //
94  return fSessP->close();
95 }
96 
97 /******************************************************************************/
98 /* f c t l */
99 /******************************************************************************/
100 
101 int XrdSsiFile::fctl(const int cmd,
102  const char *args,
103  XrdOucErrInfo &out_error)
104 {
105 
106 // Route this request as needed
107 //
108  if (fsFile) return fsFile->fctl(cmd, args, out_error);
109 
110 // Indicate we would like to use SendData()
111 //
112  if (cmd == SFS_FCTL_GETFD)
113  {out_error.setErrCode(SFS_SFIO_FDVAL);
114  return SFS_OK;
115  }
116 
117 // We don't support any other kind of command
118 //
119  return XrdSsiUtils::Emsg("fctl",ENOTSUP,"fctl",fSessP->FName(),out_error);
120 }
121 
122 /******************************************************************************/
123 
124 int XrdSsiFile::fctl(const int cmd,
125  int alen,
126  const char *args,
127  const XrdSecEntity *client)
128 {
129 
130 // Route this request as needed (callback possible)
131 //
132  if (fsFile) return fsFile->fctl(cmd, alen, args, client);
133 
134 // Forward this to the session object
135 //
136  return fSessP->fctl(cmd, alen, args, client);
137 }
138 
139 /******************************************************************************/
140 /* F N a m e */
141 /******************************************************************************/
142 
143 const char *XrdSsiFile::FName()
144 {
145 
146 // Route to filesystem if need be
147 //
148  if (fsFile) return fsFile->FName();
149 
150 // Return our name
151 //
152  return fSessP->FName();
153 }
154 
155 /******************************************************************************/
156 /* g e t C X i n f o */
157 /******************************************************************************/
158 
159 int XrdSsiFile::getCXinfo(char cxtype[4], int &cxrsz)
160 /*
161  Function: Set the length of the file object to 'flen' bytes.
162 
163  Input: n/a
164 
165  Output: cxtype - Compression algorithm code
166  cxrsz - Compression region size
167 
168  Returns SFS_OK upon success and SFS_ERROR upon failure.
169 */
170 {
171 // Route this request as needed (no callback possible)
172 //
173  if (fsFile) return fsFile->getCXinfo(cxtype, cxrsz);
174 
175 // Indicate we don't support compression
176 //
177  cxrsz = 0;
178  return SFS_OK;
179 }
180 
181 /******************************************************************************/
182 /* g e t M m a p */
183 /******************************************************************************/
184 
185 int XrdSsiFile::getMmap(void **Addr, off_t &Size) // Out
186 /*
187  Function: Return memory mapping for file, if any.
188 
189  Output: Addr - Address of memory location
190  Size - Size of the file or zero if not memory mapped.
191  Returns SFS_OK upon success and SFS_ERROR upon failure.
192 */
193 {
194 // Route this request as needed (no callback possible)
195 //
196  if (fsFile) return fsFile->getMmap(Addr, Size);
197 
198 // Indicate we don't support memory mapping
199 //
200  if (Addr) *Addr = 0;
201  Size = 0;
202  return SFS_OK;
203 }
204 
205 /******************************************************************************/
206 /* o p e n */
207 /******************************************************************************/
208 
209 int XrdSsiFile::open(const char *path, // In
210  XrdSfsFileOpenMode open_mode, // In
211  mode_t Mode, // In
212  const XrdSecEntity *client, // In
213  const char *info) // In
214 /*
215  Function: Open the file `path' in the mode indicated by `open_mode'.
216 
217  Input: path - The fully qualified name of the resource.
218  open_mode - It must contain only SFS_O_RDWR.
219  Mode - Ignored.
220  client - Authentication credentials, if any.
221  info - Opaque information to be used as seen fit.
222 
223  Output: Returns OOSS_OK upon success, otherwise SFS_ERROR is returned.
224 */
225 {
226  static const char *epname = "open";
227  int eNum;
228 
229 // Verify that this object is not already associated with an open file
230 //
231  if (fsFile || fSessP)
232  return XrdSsiUtils::Emsg(epname, EADDRINUSE, "open session", path, error);
233 
234 // Open a regular file if this is wanted
235 //
236  if (fsChk && FSPath.Find(path))
237  {if (!(fsFile = theFS->newFile(error)))
238  return XrdSsiUtils::Emsg(epname, ENOMEM, "open file", path, error);
239  return fsFile->open(path, open_mode, Mode, client, info);
240  }
241 
242 // Convert opaque and security into an environment
243 //
244  XrdOucEnv Open_Env(info, 0, client);
245 
246 // Allocate file session and issue open
247 //
248  fSessP = XrdSsiFileSess::Alloc(error, error.getErrUser());
249  eNum = fSessP->open(path, Open_Env, open_mode);
250  if (eNum) {fSessP->Recycle(); fSessP = 0;}
251  return eNum;
252 }
253 
254 /******************************************************************************/
255 /* r e a d */
256 /******************************************************************************/
257 
259  XrdSfsXferSize blen) // In
260 /*
261  Function: Preread `blen' bytes at `offset'
262 
263  Input: offset - The absolute byte offset at which to start the read.
264  blen - The amount to preread.
265 
266  Output: Returns SFS_OK upon success and SFS_ERROR o/w.
267 */
268 {
269 
270 // Route to file system if need be (no callback possible)
271 //
272  if (fsFile) return fsFile->read(offset, blen);
273 
274 // We ignore these
275 //
276  return SFS_OK;
277 }
278 
279 /******************************************************************************/
280 /* r e a d */
281 /******************************************************************************/
282 
284  char *buff, // Out
285  XrdSfsXferSize blen) // In
286 /*
287  Function: Read `blen' bytes at `offset' into 'buff' and return the actual
288  number of bytes read.
289 
290  Input: offset - Contains request information.
291  buff - Address of the buffer in which to place the data.
292  blen - The size of the buffer. This is the maximum number
293  of bytes that will be returned.
294 
295  Output: Returns the number of bytes read upon success and SFS_ERROR o/w.
296 */
297 {
298 
299 // Route to file system if need be (no callback possible)
300 //
301  if (fsFile) return fsFile->read(offset, buff, blen);
302 
303 // Forward this to the file session
304 //
305  return fSessP->read(offset, buff, blen);
306 }
307 
308 /******************************************************************************/
309 /* r e a d A I O */
310 /******************************************************************************/
311 
313 {
314 
315 // Route to file system if need be (no callback possible)
316 //
317  if (fsFile) return fsFile->read(aiop);
318 
319 // Execute this request in a synchronous fashion
320 //
321  aiop->Result = fSessP->read((XrdSfsFileOffset)aiop->sfsAio.aio_offset,
322  (char *)aiop->sfsAio.aio_buf,
324  aiop->doneRead();
325  return 0;
326 }
327 
328 /******************************************************************************/
329 /* r e a d v */
330 /******************************************************************************/
331 
333  int readCount) // In
334 /*
335  Function: Perform all the reads specified in the readV vector.
336 
337  Input: readV - A description of the reads to perform; includes the
338  absolute offset, the size of the read, and the buffer
339  to place the data into.
340  readCount - The size of the readV vector.
341 
342  Output: Returns an error as this is not supported.
343 */
344 {
345 
346 // Route this request to file system if need be (no callback possible)
347 //
348  if (fsFile) return fsFile->readv(readV, readCount);
349 
350  return XrdSsiUtils::Emsg("readv",ENOTSUP,"readv",fSessP->FName(),error);
351 }
352 
353 /******************************************************************************/
354 /* S e n d D a t a */
355 /******************************************************************************/
356 
358  XrdSfsFileOffset offset,
359  XrdSfsXferSize size)
360 {
361 
362 // Route this request to file system if need be (no callback possible)
363 //
364  if (fsFile) return fsFile->SendData(sfDio, offset, size);
365 
366 // Forward this to the file session object
367 //
368  return fSessP->SendData(sfDio, offset, size);
369 }
370 
371 /******************************************************************************/
372 /* s e t X i o */
373 /******************************************************************************/
374 
376 {
377  if (fSessP) fSessP->setXio(xP);
378  if (fsFile) fsFile->setXio(xP);
379 }
380 
381 /******************************************************************************/
382 /* s t a t */
383 /******************************************************************************/
384 
385 int XrdSsiFile::stat(struct stat *buf) // Out
386 /*
387  Function: Return file status information
388 
389  Input: buf - The stat structure to hold the results
390 
391  Output: Returns SFS_OK upon success and SFS_ERROR upon failure.
392 */
393 {
394 
395 // Route this request to file system if need be (no callback possible)
396 //
397  if (fsFile) return fsFile->stat(buf);
398 
399 // Otherwise there is no stat information
400 //
401  memset(buf, 0 , sizeof(struct stat));
402  return SFS_OK;
403 }
404 
405 /******************************************************************************/
406 /* s y n c */
407 /******************************************************************************/
408 
410 /*
411  Function: Commit all unwritten bytes to physical media.
412 
413  Input: None
414 
415  Output: Returns SFS_OK if a response is ready or SFS_STARTED otherwise.
416 */
417 {
418 
419 // Route this request to file system if need be (callback possible)
420 //
421  if (fsFile) return fsFile->sync();
422 
423 // We don't support this
424 //
425  return XrdSsiUtils::Emsg("sync",ENOTSUP,"sync",fSessP->FName(),error);
426 }
427 
428 /******************************************************************************/
429 /* s y n c A I O */
430 /******************************************************************************/
431 
433 {
434 
435 // Route this request to file system if need be (callback possible)
436 //
437  if (fsFile) return fsFile->sync(aiop);
438 
439 // We don't support this
440 //
441  return XrdSsiUtils::Emsg("syncaio",ENOTSUP,"sync",fSessP->FName(),error);
442 }
443 
444 /******************************************************************************/
445 /* t r u n c a t e */
446 /******************************************************************************/
447 
449 /*
450  Function: Set the length of the file object to 'flen' bytes.
451 
452  Input: flen - The new size of the file.
453 
454  Output: Returns SFS_ERROR a this function is not supported.
455 */
456 {
457 
458 // Route this request to file system if need be (callback possible)
459 //
460  if (fsFile) return fsFile->truncate(flen);
461 
462 // Route this to the file session object
463 //
464  return fSessP->truncate(flen);
465 }
466 
467 /******************************************************************************/
468 /* w r i t e */
469 /******************************************************************************/
470 
472  const char *buff, // In
473  XrdSfsXferSize blen) // In
474 /*
475  Function: Write `blen' bytes at `offset' from 'buff' and return the actual
476  number of bytes written.
477 
478  Input: offset - The absolute byte offset at which to start the write.
479  buff - Address of the buffer from which to get the data.
480  blen - The size of the buffer. This is the maximum number
481  of bytes that will be written to 'fd'.
482 
483  Output: Returns the number of bytes written upon success and SFS_ERROR o/w.
484 
485  Notes: An error return may be delayed until the next write(), close(), or
486  sync() call.
487 */
488 {
489 
490 // Route this request to file system if need be (no callback possible)
491 //
492  if (fsFile) return fsFile->write(offset, buff, blen);
493 
494 // Route this to the file session object
495 //
496  return fSessP->write(offset, buff, blen);
497 }
498 
499 /******************************************************************************/
500 /* w r i t e A I O */
501 /******************************************************************************/
502 
504 {
505 
506 // Route to file system if need be (no callback possible)
507 //
508  if (fsFile) return fsFile->write(aiop);
509 
510 // Execute this request in a synchronous fashion
511 //
512  aiop->Result = fSessP->write((XrdSfsFileOffset)aiop->sfsAio.aio_offset,
513  (char *)aiop->sfsAio.aio_buf,
515  aiop->doneWrite();
516  return 0;
517 }
int stat(const char *path, struct stat *buf)
int Mode
off_t aio_offset
Definition: XrdSfsAio.hh:49
size_t aio_nbytes
Definition: XrdSfsAio.hh:48
void * aio_buf
Definition: XrdSfsAio.hh:47
#define SFS_SFIO_FDVAL
#define SFS_FCTL_GETFD
int XrdSfsFileOpenMode
#define SFS_OK
long long XrdSfsFileOffset
int XrdSfsXferSize
int setErrCode(int code)
unsigned long long Find(const char *pathname)
Definition: XrdOucPList.hh:112
ssize_t Result
Definition: XrdSfsAio.hh:65
virtual void doneRead()=0
struct aiocb sfsAio
Definition: XrdSfsAio.hh:62
virtual void doneWrite()=0
virtual XrdSfsFile * newFile(char *user=0, int MonID=0)=0
static XrdSsiFileSess * Alloc(XrdOucErrInfo &einfo, const char *user)
XrdSfsXferSize readv(XrdOucIOVec *readV, int readCount)
Definition: XrdSsiFile.cc:332
int fctl(const int cmd, const char *args, XrdOucErrInfo &out_error)
Definition: XrdSsiFile.cc:101
XrdSfsXferSize write(XrdSfsFileOffset fileOffset, const char *buffer, XrdSfsXferSize buffer_size)
Definition: XrdSsiFile.cc:471
int getCXinfo(char cxtype[4], int &cxrsz)
Definition: XrdSsiFile.cc:159
const char * FName()
Definition: XrdSsiFile.cc:143
void setXio(XrdSfsXio *xP)
Definition: XrdSsiFile.cc:375
int close()
Definition: XrdSsiFile.cc:78
int getMmap(void **Addr, off_t &Size)
Definition: XrdSsiFile.cc:185
int stat(struct stat *buf)
Definition: XrdSsiFile.cc:385
int open(const char *fileName, XrdSfsFileOpenMode openMode, mode_t createMode, const XrdSecEntity *client=0, const char *opaque=0)
Definition: XrdSsiFile.cc:209
int truncate(XrdSfsFileOffset fileOffset)
Definition: XrdSsiFile.cc:448
int read(XrdSfsFileOffset fileOffset, XrdSfsXferSize preread_sz)
Definition: XrdSsiFile.cc:258
int SendData(XrdSfsDio *sfDio, XrdSfsFileOffset offset, XrdSfsXferSize size)
Definition: XrdSsiFile.cc:357
virtual ~XrdSsiFile()
Definition: XrdSsiFile.cc:65
int sync()
Definition: XrdSsiFile.cc:409
static int Emsg(const char *pfx, int ecode, const char *op, const char *path, XrdOucErrInfo &eDest)
Definition: XrdSsiUtils.cc:159
XrdSfsFileSystem * theFS
Definition: XrdSsiFile.cc:54
XrdOucBuffPool EmsgPool
Definition: XrdSsiFile.cc:53
bool fsChk
Definition: XrdSsiFile.cc:56
XrdOucPListAnchor FSPath
Definition: XrdSsiFile.cc:55