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