xrootd
XrdOfsHandle.hh
Go to the documentation of this file.
00001 #ifndef __OFS_HANDLE__
00002 #define __OFS_HANDLE__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                       X r d O f s H a n d l e . h h                        */
00006 /*                                                                            */
00007 /* (c) 2003 by the Board of Trustees of the Leland Stanford, Jr., University  */
00008 /*                            All Rights Reserved                             */
00009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00010 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00011 /*                                                                            */
00012 /* This file is part of the XRootD software suite.                            */
00013 /*                                                                            */
00014 /* XRootD is free software: you can redistribute it and/or modify it under    */
00015 /* the terms of the GNU Lesser General Public License as published by the     */
00016 /* Free Software Foundation, either version 3 of the License, or (at your     */
00017 /* option) any later version.                                                 */
00018 /*                                                                            */
00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00022 /* License for more details.                                                  */
00023 /*                                                                            */
00024 /* You should have received a copy of the GNU Lesser General Public License   */
00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00027 /*                                                                            */
00028 /* The copyright holder's institutional names and contributor's names may not */
00029 /* be used to endorse or promote products derived from this software without  */
00030 /* specific prior written permission of the institution or contributor.       */
00031 /******************************************************************************/
00032 
00033 /* These are private data structures. They are allocated dynamically to the
00034    appropriate size (yes, that means dbx has a tough time).
00035 */
00036 
00037 #include <stdlib.h>
00038 
00039 #include "XrdOuc/XrdOucCRC.hh"
00040 #include "XrdSys/XrdSysPthread.hh"
00041 
00042 /******************************************************************************/
00043 /*                    C l a s s   X r d O f s H a n K e y                     */
00044 /******************************************************************************/
00045   
00046 class XrdOfsHanKey
00047 {
00048 public:
00049 
00050 const char          *Val;
00051 unsigned int         Hash;
00052 short                Len;
00053 unsigned short       Links;
00054 
00055 inline XrdOfsHanKey& operator=(const XrdOfsHanKey &rhs)
00056                                  {Val = strdup(rhs.Val); Hash = rhs.Hash;
00057                                   Len = rhs.Len;
00058                                   return *this;
00059                                  }
00060 
00061 inline int           operator==(const XrdOfsHanKey &oth)
00062                                  {return Hash == oth.Hash && Len == oth.Len
00063                                       && !strcmp(Val, oth.Val);
00064                                  }
00065 
00066 inline int           operator!=(const XrdOfsHanKey &oth)
00067                                  {return Hash != oth.Hash || Len != oth.Len
00068                                       || strcmp(Val, oth.Val);
00069                                  }
00070 
00071                     XrdOfsHanKey(const char *key=0, int kln=0)
00072                                 : Val(key), Len(kln), Links(0)
00073                     {Hash = (key && kln ?
00074                           XrdOucCRC::CRC32((const unsigned char *)key,kln) : 0);
00075                     }
00076 
00077                    ~XrdOfsHanKey() {};
00078 };
00079 
00080 /******************************************************************************/
00081 /*                    C l a s s   X r d O f s H a n T a b                     */
00082 /******************************************************************************/
00083 
00084 class XrdOfsHandle;
00085   
00086 class XrdOfsHanTab
00087 {
00088 public:
00089 void           Add(XrdOfsHandle *hP);
00090 
00091 XrdOfsHandle  *Find(XrdOfsHanKey &Key);
00092 
00093 int            Remove(XrdOfsHandle *rip);
00094 
00095 // When allocateing a new nash, specify the required starting size. Make
00096 // sure that the previous number is the correct Fibonocci antecedent. The
00097 // series is simply n[j] = n[j-1] + n[j-2].
00098 //
00099     XrdOfsHanTab(int psize = 987, int size = 1597);
00100    ~XrdOfsHanTab() {} // Never gets deleted
00101 
00102 private:
00103 
00104 static const int LoadMax = 80;
00105 
00106 void             Expand();
00107 
00108 XrdOfsHandle   **nashtable;
00109 int              prevtablesize;
00110 int              nashtablesize;
00111 int              nashnum;
00112 int              Threshold;
00113 };
00114 
00115 /******************************************************************************/
00116 /*                    C l a s s   X r d O f s H a n d l e                     */
00117 /******************************************************************************/
00118   
00119 class XrdOssDF;
00120 class XrdOfsHanCB;
00121 class XrdOfsHanPsc;
00122 
00123 class XrdOfsHandle
00124 {
00125 friend class XrdOfsHanTab;
00126 friend class XrdOfsHanXpr;
00127 public:
00128 
00129 char                isPending;    // 1-> File  is pending sync()
00130 char                isChanged;    // 1-> File was modified
00131 char                isCompressed; // 1-> File  is compressed
00132 char                isRW;         // T-> File  is open in r/w mode
00133 
00134 void                Activate(XrdOssDF *ssP) {ssi = ssP;}
00135 
00136 static const int    opRW = 1;
00137 static const int    opPC = 3;
00138 
00139 static       int    Alloc(const char *thePath,int Opts,XrdOfsHandle **Handle);
00140 static       int    Alloc(                             XrdOfsHandle **Handle);
00141 
00142 static       void   Hide(const char *thePath);
00143 
00144 inline       int    Inactive() {return (ssi == ossDF);}
00145 
00146 inline const char  *Name() {return Path.Val;}
00147 
00148              int    PoscGet(short &Mode, int Done=0);
00149 
00150              int    PoscSet(const char *User, int Unum, short Mode);
00151 
00152        const char  *PoscUsr();
00153 
00154              int    Retire(long long *retsz=0, char *buff=0, int blen=0);
00155 
00156              int    Retire(XrdOfsHanCB *, int DSec);
00157 
00158 XrdOssDF           &Select(void) {return *ssi;}   // To allow for mt interfaces
00159 
00160 static       int    StartXpr(int Init=0);         // Internal use only!
00161 
00162              int    Usage() {return Path.Links;}
00163 
00164 inline       void   Lock()   {hMutex.Lock();}
00165 inline       void   UnLock() {hMutex.UnLock();}
00166 
00167           XrdOfsHandle() : Path(0,0) {}
00168 
00169          ~XrdOfsHandle() {Retire();}
00170 
00171 private:
00172 static int           Alloc(XrdOfsHanKey, int Opts, XrdOfsHandle **Handle);
00173        int           WaitLock(void);
00174 
00175 static const int     LockTries =   3; // Times to try for a lock
00176 static const int     LockWait  = 333; // Mills to wait between tries
00177 static const int     nolokDelay=   3; // Secs to delay client when lock failed
00178 static const int     nomemDelay=  15; // Secs to delay client when ENOMEM
00179 
00180 static XrdSysMutex   myMutex;
00181 static XrdOfsHanTab  roTable;    // File handles open r/o
00182 static XrdOfsHanTab  rwTable;    // File Handles open r/w
00183 static XrdOssDF     *ossDF;      // Dummy storage sysem
00184 static XrdOfsHandle *Free;       // List of free handles
00185 
00186        XrdSysMutex   hMutex;
00187        XrdOssDF     *ssi;        // Storage System Interface
00188        XrdOfsHandle *Next;
00189        XrdOfsHanKey  Path;       // Path for this handle
00190        XrdOfsHanPsc *Posc;       // -> Info for posc-type files
00191 };
00192   
00193 /******************************************************************************/
00194 /*                     C l a s s   X r d O f s H a n C B                      */
00195 /******************************************************************************/
00196   
00197 class XrdOfsHanCB
00198 {
00199 public:
00200 
00201 virtual void Retired(XrdOfsHandle *) = 0;
00202 
00203              XrdOfsHanCB() {}
00204 virtual     ~XrdOfsHanCB() {}
00205 };
00206 #endif