5bfc6fa1b92f664957e42d992d958c9e5b883555
[openafs.git] / src / WINNT / afsd / cm_scache.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #ifndef __CM_SCACHE_H_ENV__
11 #define __CM_SCACHE_H_ENV__ 1
12
13 #define MOUNTPOINTLEN   1024    /* max path length for symlink; same as AFSPATHMAX */
14
15 typedef struct cm_fid {
16     afs_uint32 cell;
17     afs_uint32 volume;
18     afs_uint32 vnode;
19     afs_uint32 unique;
20     afs_uint32 hash;
21 } cm_fid_t;
22
23
24 /* Key used for byte range locking.  Each unique key identifies a
25    unique client per cm_scache_t for the purpose of locking. */
26 typedef struct cm_key {
27     afs_offs_t process_id;      /* process IDs can be 64bit on 64bit environments */
28     afs_uint16 session_id;
29     afs_uint16 file_id;
30 } cm_key_t;
31
32 typedef struct cm_range {
33     afs_int64 offset;
34     afs_int64 length;
35 } cm_range_t;
36
37 /* forward dcls */
38 struct cm_scache;
39 typedef struct cm_scache cm_scache_t;
40
41 typedef struct cm_file_lock {
42     osi_queue_t q;              /* list of all locks [protected by
43                                    cm_scacheLock] */
44     osi_queue_t fileq;          /* per-file list of locks [protected
45                                    by scp->rw]*/
46
47     cm_user_t *userp;           /* The user to which this lock belongs
48                                    to [immutable; held] */
49     cm_scache_t *scp;           /* The scache to which this lock
50                                    applies to [immutable; held] */
51 #ifdef DEBUG
52     cm_fid_t   fid;
53 #endif
54
55     cm_range_t range;           /* Range for the lock [immutable] */
56     cm_key_t key;               /* Key for the lock [immutable] */
57     unsigned char lockType;     /* LockRead or LockWrite [immutable] */
58     unsigned char flags;        /* combination of CM_FILELOCK_FLAG__*
59                                  * [protected by cm_scacheLock] */
60     time_t lastUpdate;          /* time of last assertion with
61                                  * server. [protected by
62                                  * cm_scacheLock] */
63 } cm_file_lock_t;
64
65 #define CM_FILELOCK_FLAG_DELETED         0x01
66 #define CM_FILELOCK_FLAG_LOST            0x02
67
68 /* the following are mutually exclusive */
69 #define CM_FILELOCK_FLAG_WAITLOCK        0x04
70 #define CM_FILELOCK_FLAG_WAITUNLOCK      0x0C
71
72 /* the following is used to indicate that there are no server side
73    locks associated with this lock.  This is true for locks obtained
74    against files in RO volumes as well as files residing on servers
75    that disable client side byte range locking. */
76 #define CM_FILELOCK_FLAG_CLIENTONLY      0x10
77
78 #define CM_FLSHARE_OFFSET_HIGH           0x01000000
79 #define CM_FLSHARE_OFFSET_LOW            0x00000000
80 #define CM_FLSHARE_LENGTH_HIGH           0x00000000
81 #define CM_FLSHARE_LENGTH_LOW            0x00000001
82
83 typedef struct cm_prefetch {            /* last region scanned for prefetching */
84         osi_hyper_t base;               /* start of region */
85         osi_hyper_t end;                /* first char past region */
86 } cm_prefetch_t;
87
88 #define CM_SCACHE_MAGIC ('S' | 'C'<<8 | 'A'<<16 | 'C'<<24)
89
90 typedef struct cm_scache {
91     osi_queue_t q;                      /* lru queue; cm_scacheLock */
92     afs_uint32      magic;
93     struct cm_scache *nextp;            /* next in hash; cm_scacheLock */
94     struct cm_scache *allNextp;         /* next in all scache list; cm_scacheLock */
95     cm_fid_t fid;
96     afs_uint32 flags;                   /* flags; locked by rw */
97
98     /* synchronization stuff */
99     osi_rwlock_t rw;                    /* rwlock for this structure */
100     osi_rwlock_t bufCreateLock;         /* read-locked during buffer creation;
101                                          * write-locked to prevent buffers from
102                                          * being created during a truncate op, etc.
103                                          */
104     afs_int32 refCount;                 /* reference count; cm_scacheLock */
105     osi_queueData_t *bufReadsp;         /* queue of buffers being read */
106     osi_queueData_t *bufWritesp;        /* queue of buffers being written */
107
108     /* parent info for ACLs */
109     afs_uint32 parentVnode;             /* parent vnode for ACL callbacks */
110     afs_uint32 parentUnique;            /* for ACL callbacks */
111
112     /* local modification stat */
113     afs_uint32 mask;                    /* for clientModTime, length and
114                                          * truncPos */
115
116     /* file status */
117     afs_uint32 fileType;                /* file type */
118     time_t clientModTime;               /* mtime */
119     time_t serverModTime;               /* at server, for concurrent call
120                                          * comparisons */
121     osi_hyper_t length;                 /* file length */
122     cm_prefetch_t prefetch;             /* prefetch info structure */
123     afs_uint32 unixModeBits;            /* unix protection mode bits */
124     afs_uint32 linkCount;               /* link count */
125     afs_uint64 dataVersion;             /* data version */
126     afs_uint64 bufDataVersionLow;       /* range of valid cm_buf_t dataVersions */
127     afs_uint32 owner;                   /* file owner */
128     afs_uint32 group;                   /* file owning group */
129     cm_user_t *creator;                 /* user, if new file */
130
131     /* volume status */
132     time_t volumeCreationDate;          /* volume creation date from AFSVolSync */
133
134     /* pseudo file status */
135     osi_hyper_t serverLength;           /* length known to server */
136
137     /* aux file status */
138     osi_hyper_t truncPos;               /* file size to truncate to before
139                                          * storing data */
140
141     /* symlink and mount point info */
142     char mountPointStringp[MOUNTPOINTLEN];      /* the string stored in a mount point;
143                                                  * first char is type, then vol name.
144                                          * If this is a normal symlink, we store
145                                          * the link contents here.
146                                          */
147     cm_fid_t  mountRootFid;             /* mounted on root */
148     time_t    mountRootGen;             /* time to update mountRootFidp? */
149     cm_fid_t  dotdotFid;                /* parent of volume root */
150
151     /* callback info */
152     struct cm_server *cbServerp;        /* server granting callback */
153     time_t cbExpires;                   /* time callback expires */
154
155     /* access cache */
156     long anyAccess;                     /* anonymous user's access */
157     struct cm_aclent *randomACLp;       /* access cache entries */
158
159     /* file locks */
160     afs_int32    serverLock;    /* current lock we have acquired on
161                                  * this file.  One of (-1), LockRead
162                                  * or LockWrite. [protected by
163                                  * scp->rw].  In the future, this
164                                  * should be replaced by a queue of
165                                  * cm_server_lock_t objects which keep
166                                  * track of lock type, the user for
167                                  * whom the lock was obtained, the
168                                  * dataVersion at the time the lock
169                                  * was asserted last, lastRefreshCycle
170                                  * and lateUpdateTime.
171                                  */
172     unsigned long lastRefreshCycle; /* protected with cm_scacheLock
173                                      * for all scaches. */
174     afs_uint64  lockDataVersion; /* dataVersion of the scp at the time
175                                    the server lock for the scp was
176                                    asserted for this lock the last
177                                    time. */
178     osi_queue_t *fileLocksH;    /* queue of locks (head) */
179     osi_queue_t *fileLocksT;    /* queue of locks (tail) */
180
181     afs_uint32   sharedLocks;   /* number of shared locks on
182                                  * ::fileLocks.  This count does not
183                                  * include locks which have
184                                  * CM_FILELOCK_FLAG_CLIENTONLY set. */
185
186     afs_uint32   exclusiveLocks; /* number of exclusive locks on
187                                   * ::fileLocks.  This count does not
188                                   * include locks which have
189                                   * CM_FILELOCK_FLAG_CLIENTONLY set.
190                                   */
191
192     afs_uint32   clientLocks;   /* number of locks on ::fileLocks that
193                                    have CM_FILELOCK_FLAG_CLIENTONLY
194                                    set. */
195
196     afs_uint32   fsLockCount;   /* number of locks held as reported
197                                  * by the file server in the most
198                                  * recent fetch status.
199                                  */
200
201     /* bulk stat progress */
202     osi_hyper_t bulkStatProgress;       /* track bulk stats of large dirs */
203
204 #ifdef USE_BPLUS
205     /* directory B+ tree */             /* only allocated if is directory */
206     osi_rwlock_t dirlock;               /* controls access to dirBplus */
207     afs_uint64   dirDataVersion;        /* data version represented by dirBplus */
208     struct tree *dirBplus;              /* dirBplus */
209 #endif
210
211     /* open state */
212     afs_uint16 openReads;               /* open for reading */
213     afs_uint16 openWrites;              /* open for writing */
214     afs_uint16 openShares;              /* open for read excl */
215     afs_uint16 openExcls;               /* open for exclusives */
216
217     /* syncop state */
218     afs_uint32 waitCount;           /* number of threads waiting */
219     afs_uint32 waitRequests;        /* num of thread wait requests */
220     osi_queue_t * waitQueueH;       /* Queue of waiting threads.
221                                        Holds queue of
222                                        cm_scache_waiter_t
223                                        objects. Protected by
224                                        cm_cacheLock. */
225     osi_queue_t * waitQueueT;       /* locked by cm_scacheLock */
226 } cm_scache_t;
227
228 /* dataVersion */
229 #define CM_SCACHE_VERSION_BAD           0xFFFFFFFFFFFFFFFF
230
231 /* mask field - tell what has been modified */
232 #define CM_SCACHEMASK_CLIENTMODTIME     1       /* client mod time */
233 #define CM_SCACHEMASK_LENGTH            2       /* length */
234 #define CM_SCACHEMASK_TRUNCPOS          4       /* truncation position */
235
236 /* fileType values */
237 #define CM_SCACHETYPE_UNKNOWN           0       /* unknown */
238 #define CM_SCACHETYPE_FILE              1       /* a file */
239 #define CM_SCACHETYPE_DIRECTORY         2       /* a dir */
240 #define CM_SCACHETYPE_SYMLINK           3       /* a symbolic link */
241 #define CM_SCACHETYPE_MOUNTPOINT        4       /* a mount point */
242 #define CM_SCACHETYPE_DFSLINK           5       /* a Microsoft Dfs link */
243 #define CM_SCACHETYPE_INVALID           99      /* an invalid link */
244
245 /* flag bits */
246 #define CM_SCACHEFLAG_STATD             0x01    /* status info is valid */
247 #define CM_SCACHEFLAG_DELETED           0x02    /* file has been deleted */
248 #define CM_SCACHEFLAG_CALLBACK          0x04    /* have a valid callback */
249 #define CM_SCACHEFLAG_STORING           0x08    /* status being stored back */
250 #define CM_SCACHEFLAG_FETCHING          0x10    /* status being fetched */
251 #define CM_SCACHEFLAG_SIZESTORING       0x20    /* status being stored that
252                                                  * changes the data; typically,
253                                                  * this is a truncate op. */
254 #define CM_SCACHEFLAG_INHASH            0x40    /* in the hash table */
255 #define CM_SCACHEFLAG_BULKSTATTING      0x80    /* doing a bulk stat */
256 #define CM_SCACHEFLAG_SIZESETTING       0x100   /* Stabilized; Truncate */
257 #define CM_SCACHEFLAG_WAITING           0x200   /* waiting for fetch/store
258                                                  * state to change */
259 #define CM_SCACHEFLAG_PURERO            0x400   /* read-only (not even backup);
260                                                  * for mount point eval */
261 #define CM_SCACHEFLAG_RO                0x800   /* read-only
262                                                  * (can't do write ops) */
263 #define CM_SCACHEFLAG_GETCALLBACK       0x1000  /* we're getting a callback */
264 #define CM_SCACHEFLAG_DATASTORING       0x2000  /* data being stored */
265 #define CM_SCACHEFLAG_PREFETCHING       0x4000  /* somebody is prefetching */
266 #define CM_SCACHEFLAG_OVERQUOTA         0x8000  /* over quota */
267 #define CM_SCACHEFLAG_OUTOFSPACE        0x10000 /* out of space */
268 #define CM_SCACHEFLAG_ASYNCSTORING      0x20000 /* scheduled to store back */
269 #define CM_SCACHEFLAG_LOCKING           0x40000 /* setting/clearing file lock */
270 #define CM_SCACHEFLAG_WATCHED           0x80000 /* directory being watched */
271 #define CM_SCACHEFLAG_WATCHEDSUBTREE    0x100000 /* dir subtree being watched */
272 #define CM_SCACHEFLAG_ANYWATCH \
273                         (CM_SCACHEFLAG_WATCHED | CM_SCACHEFLAG_WATCHEDSUBTREE)
274
275 #define CM_SCACHEFLAG_EACCESS           0x200000 /* Bulk Stat returned EACCES */
276 #define CM_SCACHEFLAG_SMB_FID           0x400000
277 #define CM_SCACHEFLAG_LOCAL             0x800000 /* Locally modified */
278
279 /* sync flags for calls to the server.  The CM_SCACHEFLAG_FETCHING,
280  * CM_SCACHEFLAG_STORING and CM_SCACHEFLAG_SIZESTORING flags correspond to the
281  * below, except for FETCHDATA and STOREDATA, which correspond to non-null
282  * buffers in bufReadsp and bufWritesp.
283  * These flags correspond to individual RPCs that we may be making, and at most
284  * one can be set in any one call to SyncOp.
285  */
286 #define CM_SCACHESYNC_FETCHSTATUS           0x01        /* fetching status info */
287 #define CM_SCACHESYNC_STORESTATUS           0x02        /* storing status info */
288 #define CM_SCACHESYNC_FETCHDATA             0x04        /* fetch data */
289 #define CM_SCACHESYNC_STOREDATA             0x08        /* store data */
290 #define CM_SCACHESYNC_STORESIZE         0x10    /* store new file size */
291 #define CM_SCACHESYNC_GETCALLBACK       0x20    /* fetching a callback */
292 #define CM_SCACHESYNC_STOREDATA_EXCL    0x40    /* store data */
293 #define CM_SCACHESYNC_ASYNCSTORE        0x80    /* schedule data store */
294 #define CM_SCACHESYNC_LOCK              0x100   /* set/clear file lock */
295
296 /* sync flags for calls within the client; there are no corresponding flags
297  * in the scache entry, because we hold the scache entry locked during the
298  * operations below.
299  */
300 #define CM_SCACHESYNC_GETSTATUS         0x1000  /* read the status */
301 #define CM_SCACHESYNC_SETSTATUS         0x2000  /* e.g. utimes */
302 #define CM_SCACHESYNC_READ              0x4000  /* read data from a chunk */
303 #define CM_SCACHESYNC_WRITE             0x8000  /* write data to a chunk */
304 #define CM_SCACHESYNC_SETSIZE           0x10000 /* shrink the size of a file,
305                                                  * e.g. truncate */
306 #define CM_SCACHESYNC_NEEDCALLBACK      0x20000 /* need a callback on the file */
307 #define CM_SCACHESYNC_CHECKRIGHTS       0x40000 /* check that user has desired
308                                                  * access rights */
309 #define CM_SCACHESYNC_BUFLOCKED         0x80000 /* the buffer is locked */
310 #define CM_SCACHESYNC_NOWAIT            0x100000/* don't wait for the state,
311                                                  * just fail */
312 #define CM_SCACHESYNC_FORCECB           0x200000/* when calling cm_GetCallback()
313                                                  * set the force flag */
314
315 /* flags for cm_RecycleSCache   */
316 #define CM_SCACHE_RECYCLEFLAG_DESTROY_BUFFERS   0x1
317
318 /* flags for cm_MergeStatus */
319 #define CM_MERGEFLAG_FORCE              1       /* check mtime before merging;
320                                                  * used to see if we're merging
321                                                  * in old info.
322                                                  */
323 #define CM_MERGEFLAG_STOREDATA          2       /* Merge due to storedata op */
324 #define CM_MERGEFLAG_DIROP              4       /* Merge due to directory op */
325
326 /* hash define.  Must not include the cell, since the callback revocation code
327  * doesn't necessarily know the cell in the case of a multihomed server
328  * contacting us from a mystery address.
329  */
330 #define CM_SCACHE_HASH(fidp)    (((unsigned long)       \
331                                    ((fidp)->volume +    \
332                                     (fidp)->vnode +     \
333                                     (fidp)->unique))    \
334                                         % cm_data.scacheHashTableSize)
335
336 #include "cm_conn.h"
337 #include "cm_buf.h"
338
339 typedef struct cm_scache_waiter {
340     osi_queue_t q;
341     afs_int32   threadId;
342
343     cm_scache_t *scp;
344     afs_int32   flags;
345     cm_buf_t    *bufp;
346 } cm_scache_waiter_t;
347
348 extern void cm_InitSCache(int, long);
349
350 #ifdef DEBUG_REFCOUNT
351 extern long cm_GetSCacheDbg(cm_fid_t *, cm_scache_t **, struct cm_user *,
352         struct cm_req *, char *, long);
353
354 #define cm_GetSCache(a,b,c,d)  cm_GetSCacheDbg(a,b,c,d,__FILE__,__LINE__)
355 #else
356 extern long cm_GetSCache(cm_fid_t *, cm_scache_t **, struct cm_user *,
357         struct cm_req *);
358 #endif
359
360 extern cm_scache_t *cm_GetNewSCache(void);
361
362 extern __inline int cm_FidCmp(cm_fid_t *, cm_fid_t *);
363
364 extern void cm_SetFid(cm_fid_t *, afs_uint32 cell, afs_uint32 volume, afs_uint32 vnode, afs_uint32 unique);
365
366 extern long cm_SyncOp(cm_scache_t *, struct cm_buf *, struct cm_user *,
367         struct cm_req *, afs_uint32, afs_uint32);
368
369 extern void cm_SyncOpDone(cm_scache_t *, struct cm_buf *, afs_uint32);
370
371 extern void cm_MergeStatus(cm_scache_t * dscp, cm_scache_t * scp,
372                            struct AFSFetchStatus * statusp,
373                            struct AFSVolSync * volsyncp,
374                            struct cm_user *userp,
375                            cm_req_t *reqp,
376                            afs_uint32 flags);
377
378 extern void cm_AFSFidFromFid(struct AFSFid *, cm_fid_t *);
379
380 #ifdef DEBUG_REFCOUNT
381 extern void cm_HoldSCacheNoLockDbg(cm_scache_t *, char *, long);
382
383 extern void cm_HoldSCacheDbg(cm_scache_t *, char *, long);
384
385 extern void cm_ReleaseSCacheNoLockDbg(cm_scache_t *, char *, long);
386
387 extern void cm_ReleaseSCacheDbg(cm_scache_t *, char *, long);
388
389 #define cm_HoldSCacheNoLock(scp)    cm_HoldSCacheNoLockDbg(scp, __FILE__, __LINE__)
390 #define cm_HoldSCache(scp)          cm_HoldSCacheDbg(scp, __FILE__, __LINE__)
391 #define cm_ReleaseSCacheNoLock(scp) cm_ReleaseSCacheNoLockDbg(scp, __FILE__, __LINE__)
392 #define cm_ReleaseSCache(scp)       cm_ReleaseSCacheDbg(scp, __FILE__, __LINE__)
393 #else
394 extern void cm_HoldSCacheNoLock(cm_scache_t *);
395
396 extern void cm_HoldSCache(cm_scache_t *);
397
398 extern void cm_ReleaseSCacheNoLock(cm_scache_t *);
399
400 extern void cm_ReleaseSCache(cm_scache_t *);
401 #endif
402 extern cm_scache_t *cm_FindSCache(cm_fid_t *fidp);
403
404 extern cm_scache_t *cm_FindSCacheParent(cm_scache_t *);
405
406 extern osi_rwlock_t cm_scacheLock;
407
408 extern osi_queue_t *cm_allFileLocks;
409
410 extern osi_queue_t *cm_freeFileLocks;
411
412 extern unsigned long cm_lockRefreshCycle;
413
414 extern void cm_DiscardSCache(cm_scache_t *scp);
415
416 extern int cm_FindFileType(cm_fid_t *fidp);
417
418 extern long cm_ValidateSCache(void);
419
420 extern long cm_ShutdownSCache(void);
421
422 extern void cm_SuspendSCache(void);
423
424 extern long cm_RecycleSCache(cm_scache_t *scp, afs_int32 flags);
425
426 extern void cm_RemoveSCacheFromHashTable(cm_scache_t *scp);
427
428 extern void cm_AdjustScacheLRU(cm_scache_t *scp);
429
430 extern int cm_DumpSCache(FILE *outputFile, char *cookie, int lock);
431
432 extern void cm_ResetSCacheDirectory(cm_scache_t *scp, afs_int32 locked);
433
434 extern cm_scache_t * cm_RootSCachep(cm_user_t *userp, cm_req_t *reqp);
435 #endif /*  __CM_SCACHE_H_ENV__ */