XRootD
XrdPosixExtra.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d P o s i x E x t r a . c c */
4 /* */
5 /* */
6 /* (c) 2021 by the Board of Trustees of the Leland Stanford, Jr., University */
7 /* All Rights Reserved */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /* Modified by Frank Winklmeier to add the full Posix file system definition. */
31 /******************************************************************************/
32 
33 #include <cerrno>
34 
38 #include "XrdPosix/XrdPosixFile.hh"
39 
40 
41 
42 /******************************************************************************/
43 /* p g R e a d */
44 /******************************************************************************/
45 
46 ssize_t XrdPosixExtra::pgRead (int fildes, void* buffer,
47  off_t offset, size_t rdlen,
48  std::vector<uint32_t>& csvec,
49  uint64_t opts,
50  XrdPosixCallBackIO* cbp)
51 {
52  XrdPosixFile *fp;
53  long long offs, bytes;
54  uint64_t fOpts;
55  int iosz;
56 
57 // Find the file object
58 //
59  if (!(fp = XrdPosixObject::File(fildes)))
60  {if (!cbp) return -1;
61  cbp->Complete(-1);
62  return 0;
63  }
64 
65 // Make sure the size is not too large
66 //
67  if (rdlen > (size_t)0x7fffffff)
68  {fp->UnLock();
69 
70  errno = EOVERFLOW;
71  if (!cbp) return -1;
72  cbp->Complete(-1);
73  return 0;
74  }
75 
76 // Get the parameters
77 //
78  iosz = static_cast<int>(rdlen);
79  offs = static_cast<long long>(offset);
80  csvec.clear();
81  fOpts= (opts & forceCS ? XrdOucCacheIO::forceCS : 0);
82 
83 // Issue the read in the sync case
84 //
85  if (!cbp)
86  {bytes = fp->XCio->pgRead((char *)buffer, offs, (int)iosz, csvec, fOpts);
87  if (bytes < 0)
88  {fp->ecMsg.SetErrno(-bytes);
89  fp->UnLock();
90  return -1;
91  }
92  fp->UnLock();
93  return (ssize_t)bytes;
94  }
95 
96 // Handle the read in the async case
97 //
98  cbp->theFile = fp;
99  fp->Ref(); fp->UnLock();
100 
101 // Issue the read
102 //
103  fp->XCio->pgRead(*cbp, (char *)buffer, offs, (int)iosz, csvec, fOpts);
104  return 0;
105 }
106 
107 /******************************************************************************/
108 /* p g W r i t e */
109 /******************************************************************************/
110 
111 ssize_t XrdPosixExtra::pgWrite(int fildes, void* buffer,
112  off_t offset, size_t wrlen,
113  std::vector<uint32_t>& csvec,
114  uint64_t opts,
115  XrdPosixCallBackIO* cbp)
116 {
117  XrdPosixFile *fp;
118  long long offs;
119  int iosz, bytes;
120 
121 // Find the file object
122 //
123  if (!(fp = XrdPosixObject::File(fildes)))
124  {if (!cbp) return -1;
125  cbp->Complete(-1);
126  return 0;
127  }
128 
129 // Make sure the size is not too large
130 //
131  if (wrlen > (size_t)0x7fffffff)
132  {fp->UnLock();
133  errno = EOVERFLOW;
134  if (!cbp) return -1;
135  cbp->Complete(-1);
136  return 0;
137  }
138 
139 // Check if we need to generate checksums or verify that we have the right num.
140 //
141  if (csvec.size() == 0)
142  XrdOucPgrwUtils::csCalc((const char *)buffer, offset, wrlen, csvec);
143  else if (XrdOucPgrwUtils::csNum(offset, wrlen) != (int)csvec.size())
144  {fp->UnLock();
145  errno = EINVAL;
146  if (!cbp) return -1;
147  cbp->Complete(-1);
148  return 0;
149  }
150 
151 // Get the parameters
152 //
153  iosz = static_cast<int>(wrlen);
154  offs = static_cast<long long>(offset);
155 
156 // Sync: Issue the write
157 //
158  if (!cbp)
159  {bytes = fp->XCio->pgWrite((char *)buffer, offs, (int)iosz, csvec);
160  if (bytes < 0)
161  {fp->ecMsg.SetErrno(-bytes);
162  fp->UnLock();
163  return -1;
164  }
165  fp->UpdtSize(offs + iosz);
166  fp->UnLock();
167  return (ssize_t)bytes;
168  }
169 
170 // Async: Prepare for writing
171 //
172  cbp->theFile = fp;
173  fp->Ref(); fp->UnLock();
174 
175 // Issue the write
176 //
177  fp->XCio->pgWrite(*cbp, (char *)buffer, offs, (int)iosz, csvec);
178  return 0;
179 }
struct myOpts opts
static const uint64_t forceCS
Definition: XrdOucCache.hh:188
virtual int pgRead(char *buff, long long offs, int rdlen, std::vector< uint32_t > &csvec, uint64_t opts=0, int *csfix=0)
Definition: XrdOucCache.cc:39
virtual int pgWrite(char *buff, long long offs, int wrlen, std::vector< uint32_t > &csvec, uint64_t opts=0, int *csfix=0)
Definition: XrdOucCache.cc:68
int SetErrno(int ecc, int ret=-1, const char *alt=0)
Definition: XrdOucECMsg.cc:152
static void csCalc(const char *data, off_t offs, size_t count, uint32_t *csval)
static int csNum(off_t offs, int count)
Compute the required size of a checksum vector based on offset & length.
virtual void Complete(ssize_t Result)=0
static ssize_t pgWrite(int fildes, void *buffer, off_t offset, size_t wrlen, std::vector< uint32_t > &csvec, uint64_t opts=0, XrdPosixCallBackIO *cbp=0)
static const uint64_t forceCS
static ssize_t pgRead(int fildes, void *buffer, off_t offset, size_t rdlen, std::vector< uint32_t > &csvec, uint64_t opts=0, XrdPosixCallBackIO *cbp=0)
void UpdtSize(size_t newsz)
XrdOucCacheIO * XCio
Definition: XrdPosixFile.hh:65
XrdOucECMsg ecMsg
static XrdPosixFile * File(int fildes, bool glk=false)