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