-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdOfs.cc
2295 lines (1933 loc) · 78.8 KB
/
XrdOfs.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************/
/* */
/* X r d O f s . c c */
/* */
/* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Deprtment of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <netdb.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include "XProtocol/XProtocol.hh"
#include "XrdCks/XrdCks.hh"
#include "XrdCks/XrdCksConfig.hh"
#include "XrdCks/XrdCksData.hh"
#include "XrdNet/XrdNetAddr.hh"
#include "XrdNet/XrdNetIF.hh"
#include "XrdNet/XrdNetUtils.hh"
#include "XrdOfs/XrdOfs.hh"
#include "XrdOfs/XrdOfsEvs.hh"
#include "XrdOfs/XrdOfsHandle.hh"
#include "XrdOfs/XrdOfsPoscq.hh"
#include "XrdOfs/XrdOfsPrepare.hh"
#include "XrdOfs/XrdOfsTrace.hh"
#include "XrdOfs/XrdOfsSecurity.hh"
#include "XrdOfs/XrdOfsStats.hh"
#include "XrdOfs/XrdOfsTPC.hh"
#include "XrdCms/XrdCmsClient.hh"
#include "XrdOss/XrdOss.hh"
#include "XrdSys/XrdSysError.hh"
#include "XrdSys/XrdSysHeaders.hh"
#include "XrdSys/XrdSysLogger.hh"
#include "XrdSys/XrdSysPlatform.hh"
#include "XrdSys/XrdSysPthread.hh"
#include "XrdOuc/XrdOuca2x.hh"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdOuc/XrdOucERoute.hh"
#include "XrdOuc/XrdOucLock.hh"
#include "XrdOuc/XrdOucMsubs.hh"
#include "XrdOuc/XrdOucTList.hh"
#include "XrdOuc/XrdOucTPC.hh"
#include "XrdOuc/XrdOucTrace.hh"
#include "XrdSec/XrdSecEntity.hh"
#include "XrdSfs/XrdSfsAio.hh"
#include "XrdSfs/XrdSfsFlags.hh"
#include "XrdSfs/XrdSfsInterface.hh"
#ifdef AIX
#include <sys/mode.h>
#endif
#ifndef O_DIRECT
#define O_DIRECT 0
#endif
/******************************************************************************/
/* E r r o r R o u t i n g O b j e c t */
/******************************************************************************/
XrdSysError OfsEroute(0);
XrdOucTrace OfsTrace(&OfsEroute);
/******************************************************************************/
/* S t a t i s t i c a l D a t a O b j e c t */
/******************************************************************************/
XrdOfsStats OfsStats;
/******************************************************************************/
/* S t a t i c O b j e c t s */
/******************************************************************************/
XrdOfsHandle *XrdOfs::dummyHandle;
int XrdOfs::MaxDelay = 60;
int XrdOfs::OSSDelay = 30;
/******************************************************************************/
/* F i l e S y s t e m O b j e c t */
/******************************************************************************/
extern XrdOfs* XrdOfsFS;
/******************************************************************************/
/* S t o r a g e S y s t e m O b j e c t */
/******************************************************************************/
XrdOss *XrdOfsOss;
/******************************************************************************/
/* X r d O f s C o n s t r u c t o r */
/******************************************************************************/
XrdOfs::XrdOfs()
{
const char *bp;
// Establish defaults
//
ofsConfig = 0;
Authorization = 0;
Finder = 0;
Balancer = 0;
evsObject = 0;
ossRPList = 0;
myRole = strdup("server");
OssIsProxy = 0;
ossRW =' ';
// Obtain port number we will be using. Note that the constructor must occur
// after the port number is known (i.e., this cannot be a global static).
//
myPort = (bp = getenv("XRDPORT")) ? strtol(bp, (char **)NULL, 10) : 0;
// Defaults for POSC
//
poscQ = 0;
poscLog = 0;
poscHold= 10*60;
poscAuto= 0;
poscSync= 1;
// Set the configuration file name and dummy handle
//
ConfigFN = 0;
XrdOfsHandle::Alloc(&dummyHandle);
// Set checksum pointers
//
Cks = 0;
CksPfn = true;
CksRdr = true;
// Prepare handling
//
prepHandler = 0;
prepAuth = true;
// Set TPC redirect targets
//
tpcRdrHost= 0;
tpcRdrPort= 0;
// Eextended attribute limits
//
usxMaxNsz = kXR_faMaxNlen;
usxMaxVsz = kXR_faMaxVlen;
}
/******************************************************************************/
/* X r d O f s F i l e C o n s t r u c t o r */
/******************************************************************************/
XrdOfsFile::XrdOfsFile(XrdOucErrInfo &eInfo, const char *user)
: XrdSfsFile(eInfo)
{
oh = XrdOfs::dummyHandle;
dorawio = 0;
viaDel = 0;
myTPC = 0;
tident = (user ? user : "");
}
/******************************************************************************/
/* */
/* D i r e c t o r y O b j e c t I n t e r f a c e s */
/* */
/******************************************************************************/
/******************************************************************************/
/* o p e n */
/******************************************************************************/
int XrdOfsDirectory::open(const char *dir_path, // In
const XrdSecEntity *client, // In
const char *info) // In
/*
Function: Open the directory `path' and prepare for reading.
Input: path - The fully qualified name of the directory to open.
client - Authentication credentials, if any.
info - Opaque information to be used as seen fit.
Output: Returns SFS_OK upon success, otherwise SFS_ERROR.
Notes: 1. The code here assumes that directory file descriptors are never
shared. Hence, no locks need to be obtained. It works out that
lock overhead is worse than have a duplicate file descriptor for
very short durations.
*/
{
EPNAME("opendir");
XrdOucEnv Open_Env(info,0,client);
int retc;
// Trace entry
//
XTRACE(opendir, dir_path, "");
// Verify that this object is not already associated with an open directory
//
if (dp) return
XrdOfsFS->Emsg(epname, error, EADDRINUSE, "open directory", dir_path);
// Apply security, as needed
//
AUTHORIZE(client,&Open_Env,AOP_Readdir,"open directory",dir_path,error);
// Open the directory and allocate a handle for it
//
if (!(dp = XrdOfsOss->newDir(tident))) retc = -ENOMEM;
else if (!(retc = dp->Opendir(dir_path, Open_Env)))
{fname = strdup(dir_path);
return SFS_OK;
}
else {delete dp; dp = 0;}
// Encountered an error
//
return XrdOfsFS->Emsg(epname, error, retc, "open directory", dir_path);
}
/******************************************************************************/
/* n e x t E n t r y */
/******************************************************************************/
const char *XrdOfsDirectory::nextEntry()
/*
Function: Read the next directory entry.
Input: n/a
Output: Upon success, returns the contents of the next directory entry as
a null terminated string. Returns a null pointer upon EOF or an
error. To differentiate the two cases, getErrorInfo will return
0 upon EOF and an actual error code (i.e., not 0) on error.
Notes: 1. The code here assumes that idle directory file descriptors are
*not* closed. This needs to be the case because we need to return
non-duplicate directory entries. Anyway, the xrootd readdir protocol
is handled internally so directories should never be idle.
2. The code here assumes that directory file descriptors are never
shared. Hence, no locks need to be obtained. It works out that
lock overhead is worse than have a duplicate file descriptor for
very short durations.
*/
{
EPNAME("readdir");
int retc;
// Check if this directory is actually open
//
if (!dp) {XrdOfsFS->Emsg(epname, error, EBADF, "read directory");
return 0;
}
// Check if we are at EOF (once there we stay there)
//
if (atEOF) return 0;
// Read the next directory entry
//
if ((retc = dp->Readdir(dname, sizeof(dname))) < 0)
{XrdOfsFS->Emsg(epname, error, retc, "read directory", fname);
return 0;
}
// Check if we have reached end of file
//
if (!*dname)
{atEOF = 1;
error.clear();
XTRACE(readdir, fname, "<eof>");
return 0;
}
// Return the actual entry
//
XTRACE(readdir, fname, dname);
return (const char *)(dname);
}
/******************************************************************************/
/* c l o s e */
/******************************************************************************/
int XrdOfsDirectory::close()
/*
Function: Close the directory object.
Input: n/a
Output: Returns SFS_OK upon success and SFS_ERROR upon failure.
Notes: 1. The code here assumes that directory file descriptors are never
shared. Hence, no locks need to be obtained. It works out that
lock overhead is worse than have a duplicate file descriptor for
very short durations.
*/
{
EPNAME("closedir");
int retc;
// Check if this directory is actually open
//
if (!dp) {XrdOfsFS->Emsg(epname, error, EBADF, "close directory");
return SFS_ERROR;
}
XTRACE(closedir, fname, "");
// Close this directory
//
if ((retc = dp->Close()))
retc = XrdOfsFS->Emsg(epname, error, retc, "close", fname);
else retc = SFS_OK;
// All done
//
delete dp;
dp = 0;
free(fname);
fname = 0;
return retc;
}
/******************************************************************************/
/* a u t o S t a t */
/******************************************************************************/
int XrdOfsDirectory::autoStat(struct stat *buf)
/*
Function: Set stat buffer to automaticaly return stat information
Input: Pointer to stat buffer which will be filled in on each
nextEntry() and represent stat information for that entry.
Output: Upon success, returns zero. Upon error returns SFS_ERROR and sets
the error object to contain the reason.
Notes: 1. If autoStat() is not supported he caller will need to follow up
with a manual stat() call for the full path, a slow and tedious
process. The autoStat function significantly reduces overhead by
automatically providing stat information for the entry read.
*/
{
EPNAME("autoStat");
int retc;
// Check if this directory is actually open
//
if (!dp) {XrdOfsFS->Emsg(epname, error, EBADF, "autostat directory");
return SFS_ERROR;
}
// Set the stat buffer in the storage system directory.
//
if ((retc = dp->StatRet(buf)))
retc = XrdOfsFS->Emsg(epname, error, retc, "autostat", fname);
else retc = SFS_OK;
// All done
//
return retc;
}
/******************************************************************************/
/* */
/* F i l e O b j e c t I n t e r f a c e s */
/* */
/******************************************************************************/
/******************************************************************************/
/* o p e n */
/******************************************************************************/
int XrdOfsFile::open(const char *path, // In
XrdSfsFileOpenMode open_mode, // In
mode_t Mode, // In
const XrdSecEntity *client, // In
const char *info) // In
/*
Function: Open the file `path' in the mode indicated by `open_mode'.
Input: path - The fully qualified name of the file to open.
open_mode - One of the following flag values:
SFS_O_RDONLY - Open file for reading.
SFS_O_WRONLY - Open file for writing.
SFS_O_RDWR - Open file for update
SFS_O_NOTPC - Disallow TPC opens
SFS_O_REPLICA- Open file for replication
SFS_O_CREAT - Create the file open in RW mode
SFS_O_TRUNC - Trunc the file open in RW mode
SFS_O_POSC - Presist file on successful close
Mode - The Posix access mode bits to be assigned to the file.
These bits correspond to the standard Unix permission
bits (e.g., 744 == "rwxr--r--"). Additionally, Mode
may contain SFS_O_MKPTH to force creation of the full
directory path if it does not exist. This parameter is
ignored unless open_mode = SFS_O_CREAT.
client - Authentication credentials, if any.
info - Opaque information to be used as seen fit.
Output: Returns SFS_OK upon success, otherwise SFS_ERROR is returned.
*/
{
EPNAME("open");
static const int crMask = (SFS_O_CREAT | SFS_O_TRUNC);
static const int opMask = (SFS_O_RDONLY | SFS_O_WRONLY | SFS_O_RDWR);
struct OpenHelper
{const char *Path;
XrdOfsHandle *hP;
XrdOssDF *fP;
int poscNum;
int OK() {hP = 0; fP = 0; poscNum = 0; return SFS_OK;}
OpenHelper(const char *path)
: Path(path), hP(0), fP(0), poscNum(0) {}
~OpenHelper()
{int retc;
if (hP) hP->Retire(retc);
if (fP) delete fP;
if (poscNum > 0) XrdOfsFS->poscQ->Del(Path, poscNum, 1);
}
} oP(path);
mode_t theMode = Mode & S_IAMB;
const char *tpcKey;
int retc, isPosc = 0, crOpts = 0, isRW = 0, open_flag = 0;
int find_flag = open_mode & (SFS_O_NOWAIT | SFS_O_RESET | SFS_O_MULTIW);
XrdOucEnv Open_Env(info,0,client);
// Trace entry
//
ZTRACE(open, std::hex <<open_mode <<"-" <<std::oct <<Mode <<std::dec <<" fn=" <<path);
// Verify that this object is not already associated with an open file
//
XrdOfsFS->ocMutex.Lock();
if (oh != XrdOfs::dummyHandle)
{XrdOfsFS->ocMutex.UnLock();
return XrdOfsFS->Emsg(epname,error,EADDRINUSE,"open file",path);
}
XrdOfsFS->ocMutex.UnLock();
// Handle the open mode options
//
if (open_mode & crMask)
{crOpts = (Mode & SFS_O_MKPTH ? XRDOSS_mkpath : 0);
if (XrdOfsFS->poscQ && ((open_mode & SFS_O_POSC) ||
XrdOfsFS->poscAuto || Open_Env.Get("ofs.posc")))
{isPosc = 1; isRW = XrdOfsHandle::opPC;}
else isRW = XrdOfsHandle::opRW;
if (open_mode & SFS_O_CREAT)
{open_flag = O_RDWR | O_CREAT | O_EXCL;
find_flag |= SFS_O_RDWR | SFS_O_CREAT | (open_mode & SFS_O_REPLICA);
crOpts |= XRDOSS_new;
} else {
open_flag |= O_RDWR | O_CREAT | O_TRUNC;
find_flag |= SFS_O_RDWR | SFS_O_TRUNC;
}
}
else
switch(open_mode & opMask)
{case SFS_O_RDONLY: open_flag = O_RDONLY; find_flag |= SFS_O_RDONLY;
break;
case SFS_O_WRONLY: open_flag = O_WRONLY; find_flag |= SFS_O_WRONLY;
isRW = XrdOfsHandle::opRW;
if (XrdOfsFS->poscQ && ((open_mode & SFS_O_POSC) ||
Open_Env.Get("ofs.posc"))) oP.poscNum = -1;
break;
case SFS_O_RDWR: open_flag = O_RDWR; find_flag |= SFS_O_RDWR;
isRW = XrdOfsHandle::opRW;
if (XrdOfsFS->poscQ && ((open_mode & SFS_O_POSC) ||
Open_Env.Get("ofs.posc"))) oP.poscNum = -1;
break;
default: open_flag = O_RDONLY; find_flag |= SFS_O_RDONLY;
break;
}
// Preset TPC handling
//
tpcKey = Open_Env.Get(XrdOucTPC::tpcKey);
// Check if we will be redirecting the tpc request
//
if (tpcKey && isRW && XrdOfsFS->tpcRdrHost)
{error.setErrInfo(XrdOfsFS->tpcRdrPort, XrdOfsFS->tpcRdrHost);
return SFS_REDIRECT;
}
// If we have a finder object, use it to direct the client. The final
// destination will apply the security that is needed
//
if (XrdOfsFS->Finder && (retc = XrdOfsFS->Finder->Locate(error, path,
find_flag, &Open_Env)))
return XrdOfsFS->fsError(error, retc);
// Preset TPC handling and if not allowed, complain
//
if ((tpcKey = Open_Env.Get(XrdOucTPC::tpcKey)) && (open_flag & SFS_O_NOTPC))
return XrdOfsFS->Emsg(epname, error, EPROTOTYPE, "tpc", path);
// Create the file if so requested o/w try to attach the file
//
if (open_flag & O_CREAT)
{// Apply security, as needed
//
AUTHORIZE(client,&Open_Env,AOP_Create,"create",path,error);
OOIDENTENV(client, Open_Env);
// For ephemeral file, we must enter the file into the queue
//
if (isPosc && (oP.poscNum = XrdOfsFS->poscQ->Add(tident,path)) < 0)
return XrdOfsFS->Emsg(epname, error, oP.poscNum, "pcreate", path);
// Create the file. If ENOTSUP is returned, promote the creation to
// the subsequent open. This is to accomodate proxy support.
//
if ((retc = XrdOfsOss->Create(tident, path, theMode, Open_Env,
((open_flag << 8) | crOpts))))
{if (retc > 0) return XrdOfsFS->Stall(error, retc, path);
if (retc == -EINPROGRESS)
{XrdOfsFS->evrObject.Wait4Event(path,&error);
return XrdOfsFS->fsError(error, SFS_STARTED);
}
if (retc != -ENOTSUP)
{if (XrdOfsFS->Balancer) XrdOfsFS->Balancer->Removed(path);
return XrdOfsFS->Emsg(epname, error, retc, "create", path);
}
} else {
if (XrdOfsFS->Balancer) XrdOfsFS->Balancer->Added(path, isPosc);
open_flag = O_RDWR|O_TRUNC;
if (XrdOfsFS->evsObject
&& XrdOfsFS->evsObject->Enabled(XrdOfsEvs::Create))
{XrdOfsEvsInfo evInfo(tident,path,info,&Open_Env,Mode);
XrdOfsFS->evsObject->Notify(XrdOfsEvs::Create, evInfo);
}
}
} else {
// Apply security, as needed
//
if (tpcKey && !isRW)
{XrdOfsTPC::Facts Args(client, &error, &Open_Env, tpcKey, path);
if ((retc = XrdOfsTPC::Authorize(&myTPC, Args))) return retc;
} else {AUTHORIZE(client, &Open_Env, (isRW?AOP_Update:AOP_Read),
"open", path, error);
}
OOIDENTENV(client, Open_Env);
}
// Get a handle for this file.
//
if ((retc = XrdOfsHandle::Alloc(path, isRW, &oP.hP)))
{if (retc > 0) return XrdOfsFS->Stall(error, retc, path);
return XrdOfsFS->Emsg(epname, error, retc, "attach", path);
}
// If this is a third party copy and we are the destination, then validate
// specification at this point and setup to transfer. Note that if the
// call fails and auto removal is enabled, the file we created will be deleted.
//
if (tpcKey && isRW)
{char pfnbuff[MAXPATHLEN+8]; const char *pfnP;
if (!(pfnP = XrdOfsOss->Lfn2Pfn(path, pfnbuff, MAXPATHLEN, retc)))
return XrdOfsFS->Emsg(epname, error, retc, "open", path);
XrdOfsTPC::Facts Args(client, &error, &Open_Env, tpcKey, path, pfnP);
if ((retc = XrdOfsTPC::Validate(&myTPC, Args))) return retc;
}
// Assign/transfer posc ownership. We may need to delay the client if the
// file create ownership does not match and this is not a create request.
//
if (oP.hP->isRW == XrdOfsHandle::opPC)
{if (!isRW) return XrdOfsFS->Stall(error, -1, path);
if ((retc = oP.hP->PoscSet(tident, oP.poscNum, theMode)))
{if (retc > 0) XrdOfsFS->poscQ->Del(path, retc);
else return XrdOfsFS->Emsg(epname, error, retc, "access", path);
}
}
// If this is a previously existing handle, we are almost done. If this is
// the target of a third party copy requesy, fail it now. We don't support
// multiple writers in tpc mode (this should really never happen).
//
if (!(oP.hP->Inactive()))
{dorawio = (oh->isCompressed && open_mode & SFS_O_RAWIO ? 1 : 0);
if (tpcKey && isRW)
return XrdOfsFS->Emsg(epname, error, EALREADY, "tpc", path);
XrdOfsFS->ocMutex.Lock(); oh = oP.hP; XrdOfsFS->ocMutex.UnLock();
FTRACE(open, "attach use=" <<oh->Usage());
if (oP.poscNum > 0) XrdOfsFS->poscQ->Commit(path, oP.poscNum);
oP.hP->UnLock();
OfsStats.sdMutex.Lock();
isRW ? OfsStats.Data.numOpenW++ : OfsStats.Data.numOpenR++;
if (oP.poscNum > 0) OfsStats.Data.numOpenP++;
OfsStats.sdMutex.UnLock();
return oP.OK();
}
// Get a storage system object
//
if (!(oP.fP = XrdOfsOss->newFile(tident)))
return XrdOfsFS->Emsg(epname, error, ENOMEM, "open", path);
// We need to make special provisions for proxy servers in the presence of
// the TPC option and possibly cache as it's handled differently in this case.
//
if (XrdOfsFS->OssIsProxy)
{if (myTPC) open_flag |= O_NOFOLLOW;
if (error.getUCap() & XrdOucEI::uUrlOK &&
error.getUCap() & XrdOucEI::uLclF) open_flag |= O_DIRECT;
}
// Open the file
//
if ((retc = oP.fP->Open(path, open_flag, Mode, Open_Env)))
{if (retc > 0) return XrdOfsFS->Stall(error, retc, path);
if (retc == -EINPROGRESS)
{XrdOfsFS->evrObject.Wait4Event(path,&error);
return XrdOfsFS->fsError(error, SFS_STARTED);
}
if (retc == -ETXTBSY) return XrdOfsFS->Stall(error, -1, path);
if (retc == -EDESTADDRREQ)
{char *url = Open_Env.Get("FileURL");
if (url) {error.setErrInfo(-1, url); return SFS_REDIRECT;}
}
if (XrdOfsFS->Balancer && retc != -ECANCELED)
XrdOfsFS->Balancer->Removed(path);
return XrdOfsFS->Emsg(epname, error, retc, "open", path);
}
// Verify that we can actually use this file
//
if (oP.poscNum > 0)
{if ((retc = oP.fP->Fchmod(static_cast<mode_t>(theMode|XRDSFS_POSCPEND))))
return XrdOfsFS->Emsg(epname, error, retc, "fchmod", path);
XrdOfsFS->poscQ->Commit(path, oP.poscNum);
}
// Set compression values and activate the handle
//
if (oP.fP->isCompressed() > 0)
{oP.hP->isCompressed = 1;
dorawio = (open_mode & SFS_O_RAWIO ? 1 : 0);
}
oP.hP->Activate(oP.fP);
oP.hP->UnLock();
// Send an open event if we must
//
if (XrdOfsFS->evsObject)
{XrdOfsEvs::Event theEvent = (isRW ? XrdOfsEvs::Openw : XrdOfsEvs::Openr);
if (XrdOfsFS->evsObject->Enabled(theEvent))
{XrdOfsEvsInfo evInfo(tident, path, info, &Open_Env);
XrdOfsFS->evsObject->Notify(theEvent, evInfo);
}
}
// Maintain statistics
//
OfsStats.sdMutex.Lock();
isRW ? OfsStats.Data.numOpenW++ : OfsStats.Data.numOpenR++;
if (oP.poscNum > 0) OfsStats.Data.numOpenP++;
OfsStats.sdMutex.UnLock();
// All done
//
XrdOfsFS->ocMutex.Lock(); oh = oP.hP; XrdOfsFS->ocMutex.UnLock();
return oP.OK();
}
/******************************************************************************/
/* c l o s e */
/******************************************************************************/
int XrdOfsFile::close() // In
/*
Function: Close the file object.
Input: n/a
Output: Returns SFS_OK upon success and SFS_ERROR upon failure.
*/
{
EPNAME("close");
class CloseFH : public XrdOfsHanCB
{public: void Retired(XrdOfsHandle *hP) {XrdOfsFS->Unpersist(hP);}};
static XrdOfsHanCB *hCB = static_cast<XrdOfsHanCB *>(new CloseFH);
XrdOfsHandle *hP;
int poscNum, retc, cRetc = 0;
short theMode;
// Trace the call
//
FTRACE(close, "use=" <<oh->Usage()); // Unreliable trace, no origin lock
// Verify the handle (we briefly maintain a global lock)
//
XrdOfsFS->ocMutex.Lock();
if (oh == XrdOfs::dummyHandle)
{XrdOfsFS->ocMutex.UnLock(); return SFS_OK;}
if ((oh->Inactive()))
{XrdOfsFS->ocMutex.UnLock();
return XrdOfsFS->Emsg(epname, error, EBADF, "close file");
}
hP = oh; oh = XrdOfs::dummyHandle;
XrdOfsFS->ocMutex.UnLock();
hP->Lock();
// Delete the tpc object, if any
//
if (myTPC) {myTPC->Del(); myTPC = 0;}
// Maintain statistics
//
OfsStats.sdMutex.Lock();
if (!(hP->isRW)) OfsStats.Data.numOpenR--;
else {OfsStats.Data.numOpenW--;
if (hP->isRW == XrdOfsHandle::opPC) OfsStats.Data.numOpenP--;
}
OfsStats.sdMutex.UnLock();
// If this file was tagged as a POSC then we need to make sure it will persist
// Note that we unpersist the file immediately when it's inactive or if no hold
// time is allowed. Also, close events occur only for active handles. If the
// entry was via delete then we ignore the close return code as there is no
// one to handle it on the other side.
//
if ((poscNum = hP->PoscGet(theMode, !viaDel)))
{if (viaDel)
{if (hP->Inactive() || !XrdOfsFS->poscHold)
{XrdOfsFS->Unpersist(hP, !hP->Inactive()); hP->Retire(cRetc);}
else hP->Retire(hCB, XrdOfsFS->poscHold);
return SFS_OK;
}
if ((retc = hP->Select().Fchmod(theMode)))
XrdOfsFS->Emsg(epname, error, retc, "fchmod", hP->Name());
else {XrdOfsFS->poscQ->Del(hP->Name(), poscNum);
if (XrdOfsFS->Balancer) XrdOfsFS->Balancer->Added(hP->Name());
}
}
// We need to handle the cunudrum that an event may have to be sent upon
// the final close. However, that would cause the path name to be destroyed.
// So, we have two modes of logic where we copy out the pathname if a final
// close actually occurs. The path is not copied if it's not final and we
// don't bother with any of it if we need not generate an event.
//
if (XrdOfsFS->evsObject && tident
&& XrdOfsFS->evsObject->Enabled(hP->isRW ? XrdOfsEvs::Closew
: XrdOfsEvs::Closer))
{long long FSize, *retsz;
char pathbuff[MAXPATHLEN+8];
XrdOfsEvs::Event theEvent;
if (hP->isRW) {theEvent = XrdOfsEvs::Closew; retsz = &FSize;}
else { theEvent = XrdOfsEvs::Closer; retsz = 0; FSize=0;}
if (!(hP->Retire(cRetc, retsz, pathbuff, sizeof(pathbuff))))
{XrdOfsEvsInfo evInfo(tident, pathbuff, "" , 0, 0, FSize);
XrdOfsFS->evsObject->Notify(theEvent, evInfo);
}
} else hP->Retire(cRetc);
// All done
//
return (cRetc ? XrdOfsFS->Emsg(epname, error, cRetc, "close file") : SFS_OK);
}
/******************************************************************************/
/* f c t l */
/******************************************************************************/
int XrdOfsFile::fctl(const int cmd,
const char *args,
XrdOucErrInfo &out_error)
{
// See if we can do this
//
if (cmd == SFS_FCTL_GETFD)
{out_error.setErrCode(oh->Select().getFD());
return SFS_OK;
}
// We don't support this
//
out_error.setErrInfo(ENOTSUP, "fctl operation not supported");
return SFS_ERROR;
}
/******************************************************************************/
int XrdOfsFile::fctl(const int cmd,
int alen,
const char *args,
const XrdSecEntity *client)
{ // 12345678901234
static const char *fctlArg = "ofs.tpc cancel";
static const int fctlAsz = 15;
// See if the is a tpc cancellation (the only thing we support here)
//
if (cmd != SFS_FCTL_SPEC1 || !args || alen < fctlAsz || strcmp(fctlArg,args))
{error.setErrInfo(ENOTSUP, "fctl operation not supported");
return SFS_ERROR;
}
// Check if we have a tpc operation in progress
//
if (!myTPC)
{error.setErrInfo(ESRCH, "tpc operation not found");
return SFS_ERROR;
}
// Cancel the tpc
//
myTPC->Del();
myTPC = 0;
return SFS_OK;
}
/******************************************************************************/
/* r e a d */
/******************************************************************************/
int XrdOfsFile::read(XrdSfsFileOffset offset, // In
XrdSfsXferSize blen) // In
/*
Function: Preread `blen' bytes at `offset'
Input: offset - The absolute byte offset at which to start the read.
blen - The amount to preread.
Output: Returns SFS_OK upon success and SFS_ERROR o/w.
*/
{
EPNAME("read");
int retc;
// Perform required tracing
//
FTRACE(read, "preread " <<blen <<"@" <<offset);
// Make sure the offset is not too large
//
#if _FILE_OFFSET_BITS!=64
if (offset > 0x000000007fffffff)
return XrdOfsFS->Emsg(epname, error, EFBIG, "read", oh->Name());
#endif
// Now preread the actual number of bytes
//
if ((retc = oh->Select().Read((off_t)offset, (size_t)blen)) < 0)
return XrdOfsFS->Emsg(epname, error, (int)retc, "preread", oh->Name());
// Return number of bytes read
//
return retc;
}
/******************************************************************************/
/* r e a d */
/******************************************************************************/
XrdSfsXferSize XrdOfsFile::read(XrdSfsFileOffset offset, // In
char *buff, // Out
XrdSfsXferSize blen) // In
/*
Function: Read `blen' bytes at `offset' into 'buff' and return the actual
number of bytes read.
Input: offset - The absolute byte offset at which to start the read.
buff - Address of the buffer in which to place the data.
blen - The size of the buffer. This is the maximum number
of bytes that will be read from 'fd'.
Output: Returns the number of bytes read upon success and SFS_ERROR o/w.
*/
{
EPNAME("read");
XrdSfsXferSize nbytes;
// Perform required tracing
//
FTRACE(read, blen <<"@" <<offset);
// Make sure the offset is not too large
//
#if _FILE_OFFSET_BITS!=64
if (offset > 0x000000007fffffff)
return XrdOfsFS->Emsg(epname, error, EFBIG, "read", oh->Name());
#endif
// Now read the actual number of bytes
//
nbytes = (dorawio ?
(XrdSfsXferSize)(oh->Select().ReadRaw((void *)buff,
(off_t)offset, (size_t)blen))
: (XrdSfsXferSize)(oh->Select().Read((void *)buff,
(off_t)offset, (size_t)blen)));
if (nbytes < 0)
return XrdOfsFS->Emsg(epname, error, (int)nbytes, "read", oh->Name());
// Return number of bytes read
//
return nbytes;
}
/******************************************************************************/
/* r e a d v */
/******************************************************************************/
XrdSfsXferSize XrdOfsFile::readv(XrdOucIOVec *readV, // In
int readCount) // In
/*
Function: Perform all the reads specified in the readV vector.
Input: readV - A description of the reads to perform; includes the
absolute offset, the size of the read, and the buffer
to place the data into.
readCount - The size of the readV vector.
Output: Returns the number of bytes read upon success and SFS_ERROR o/w.
If the number of bytes read is less than requested, it is considered
an error.
*/
{
EPNAME("readv");
XrdSfsXferSize nbytes = oh->Select().ReadV(readV, readCount);
if (nbytes < 0)
return XrdOfsFS->Emsg(epname, error, (int)nbytes, "readv", oh->Name());
return nbytes;
}
/******************************************************************************/
/* r e a d A I O */
/******************************************************************************/
/*
Function: Read `blen' bytes at `offset' into 'buff' and return the actual
number of bytes read using asynchronous I/O, if possible.
Output: Returns the 0 if successfullt queued, otherwise returns an error.
The underlying implementation will convert the request to
synchronous I/O is async mode is not possible.
*/
int XrdOfsFile::read(XrdSfsAio *aiop)
{
EPNAME("aioread");
int rc;
// Async mode for compressed files is not supported.