DEVEL15-windows-volstat-and-vista-dfs-support-20071222
[openafs.git] / src / WINNT / afsd / cm_vnodeops.c
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 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #ifndef DJGPP
14 #include <windows.h>
15 #include <winsock2.h>
16 #endif /* !DJGPP */
17 #include <stddef.h>
18 #include <malloc.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <errno.h>
22
23 #include <osi.h>
24
25 #include "afsd.h"
26 #include "cm_btree.h"
27
28 #ifdef DEBUG
29 extern void afsi_log(char *pattern, ...);
30 #endif
31
32 int cm_enableServerLocks = 1;
33
34 int cm_followBackupPath = 0;
35
36 /*
37  * Case-folding array.  This was constructed by inspecting of SMBtrace output.
38  * I do not know anything more about it.
39  */
40 unsigned char cm_foldUpper[256] = {
41      0x0,  0x1,  0x2,  0x3,  0x4,  0x5,  0x6,  0x7,
42      0x8,  0x9,  0xa,  0xb,  0xc,  0xd,  0xe,  0xf,
43     0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
44     0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
45     0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
46     0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
47     0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
48     0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
49     0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
50     0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
51     0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
52     0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
53     0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
54     0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
55     0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
56     0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
57     0x80, 0x9a, 0x90, 0x41, 0x8e, 0x41, 0x8f, 0x80,
58     0x45, 0x45, 0x45, 0x49, 0x49, 0x49, 0x8e, 0x8f,
59     0x90, 0x92, 0x92, 0x4f, 0x99, 0x4f, 0x55, 0x55,
60     0x59, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
61     0x41, 0x49, 0x4f, 0x55, 0xa5, 0xa5, 0x56, 0xa7,
62     0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
63     0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
64     0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
65     0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
66     0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
67     0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
68     0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
69     0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
70     0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
71     0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
72     0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
73 };
74
75 /*
76  * Case-insensitive string comparison.  We used to use stricmp, but it doesn't
77  * know about 8-bit characters (e.g. 129 is lowercase u-umlaut, 154 is
78  * upper-case u-umlaut).
79  */
80 int cm_stricmp(const char *str1, const char *str2)
81 {
82     char c1, c2;
83
84     while (1) {
85         if (*str1 == 0)
86             if (*str2 == 0)
87                 return 0;
88             else
89                 return -1;
90         if (*str2 == 0)
91             return 1;
92         c1 = (char) cm_foldUpper[(unsigned char)(*str1++)];
93         c2 = (char) cm_foldUpper[(unsigned char)(*str2++)];
94         if (c1 < c2)
95             return -1;
96         if (c1 > c2)
97             return 1;
98     }
99 }
100
101 /* characters that are legal in an 8.3 name */
102 /*
103  * We used to have 1's for all characters from 128 to 254.  But
104  * the NT client behaves better if we create an 8.3 name for any
105  * name that has a character with the high bit on, and if we
106  * delete those characters from 8.3 names.  In particular, see
107  * Sybase defect 10859.
108  */
109 char cm_LegalChars[256] = {
110  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112  0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,
113  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
114  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
115  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
116  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
117  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
118  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
120  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
126 };
127
128 /* return true iff component is a valid 8.3 name */
129 int cm_Is8Dot3(char *namep)
130 {
131     int sawDot = 0;
132     unsigned char tc;
133     int charCount = 0;
134         
135     /*
136      * can't have a leading dot;
137      * special case for . and ..
138      */
139     if (namep[0] == '.') {
140         if (namep[1] == 0)
141             return 1;
142         if (namep[1] == '.' && namep[2] == 0)
143             return 1;
144         return 0;
145     }
146     while (tc = *namep++) {
147         if (tc == '.') {
148             /* saw another dot */
149             if (sawDot) return 0;       /* second dot */
150             sawDot = 1;
151             charCount = 0;
152             continue;
153         }
154         if (cm_LegalChars[tc] == 0)
155             return 0;
156         charCount++;
157         if (!sawDot && charCount > 8)
158             /* more than 8 chars in name */
159             return 0;
160         if (sawDot && charCount > 3)
161             /* more than 3 chars in extension */
162             return 0;
163     }
164     return 1;
165 }
166
167 /*
168  * Number unparsing map for generating 8.3 names;
169  * The version taken from DFS was on drugs.  
170  * You can't include '&' and '@' in a file name.
171  */
172 char cm_8Dot3Mapping[42] =
173 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
174  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 
175  'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 
176  'V', 'W', 'X', 'Y', 'Z', '_', '-', '$', '#', '!', '+', '='
177 };
178 int cm_8Dot3MapSize = sizeof(cm_8Dot3Mapping);
179
180 void cm_Gen8Dot3NameInt(const char * longname, cm_dirFid_t * pfid,
181                         char *shortName, char **shortNameEndp)
182 {
183     char number[12];
184     int i, nsize = 0;
185     int vnode = ntohl(pfid->vnode);
186     char *lastDot;
187     int validExtension = 0;
188     char tc, *temp;
189     const char *name;
190
191     /* Unparse the file's vnode number to get a "uniquifier" */
192     do {
193         number[nsize] = cm_8Dot3Mapping[vnode % cm_8Dot3MapSize];
194         nsize++;
195         vnode /= cm_8Dot3MapSize;
196     } while (vnode);
197
198     /*
199      * Look for valid extension.  There has to be a dot, and
200      * at least one of the characters following has to be legal.
201      */
202     lastDot = strrchr(longname, '.');
203     if (lastDot) {
204         temp = lastDot; temp++;
205         while (tc = *temp++)
206             if (cm_LegalChars[tc])
207                 break;
208         if (tc)
209             validExtension = 1;
210     }
211
212     /* Copy name characters */
213     for (i = 0, name = longname;
214           i < (7 - nsize) && name != lastDot; ) {
215         tc = *name++;
216
217         if (tc == 0)
218             break;
219         if (!cm_LegalChars[tc])
220             continue;
221         i++;
222         *shortName++ = toupper(tc);
223     }
224
225     /* tilde */
226     *shortName++ = '~';
227
228     /* Copy uniquifier characters */
229     memcpy(shortName, number, nsize);
230     shortName += nsize;
231
232     if (validExtension) {
233         /* Copy extension characters */
234         *shortName++ = *lastDot++;      /* copy dot */
235         for (i = 0, tc = *lastDot++;
236               i < 3 && tc;
237               tc = *lastDot++) {
238             if (cm_LegalChars[tc]) {
239                 i++;
240                 *shortName++ = toupper(tc);
241             }
242         }
243     }
244
245     /* Trailing null */
246     *shortName = 0;
247
248     if (shortNameEndp)
249         *shortNameEndp = shortName;
250 }       
251
252 /* return success if we can open this file in this mode */
253 long cm_CheckOpen(cm_scache_t *scp, int openMode, int trunc, cm_user_t *userp,
254                   cm_req_t *reqp)
255 {
256     long rights;
257     long code;
258
259     rights = 0;
260     if (openMode != 1) 
261         rights |= PRSFS_READ;
262     if (openMode == 1 || openMode == 2 || trunc) 
263         rights |= PRSFS_WRITE;
264         
265     lock_ObtainMutex(&scp->mx);
266
267     code = cm_SyncOp(scp, NULL, userp, reqp, rights,
268                       CM_SCACHESYNC_GETSTATUS
269                      | CM_SCACHESYNC_NEEDCALLBACK
270                      | CM_SCACHESYNC_LOCK);
271
272     if (code == 0 && 
273         ((rights & PRSFS_WRITE) || (rights & PRSFS_READ)) &&
274         scp->fileType == CM_SCACHETYPE_FILE) {
275
276         cm_key_t key;
277         unsigned int sLockType;
278         LARGE_INTEGER LOffset, LLength;
279
280         /* Check if there's some sort of lock on the file at the
281            moment. */
282
283         key = cm_GenerateKey(CM_SESSION_CMINT,0,0);
284
285         if (rights & PRSFS_WRITE)
286             sLockType = 0;
287         else
288             sLockType = LOCKING_ANDX_SHARED_LOCK;
289
290         LOffset.HighPart = CM_FLSHARE_OFFSET_HIGH;
291         LOffset.LowPart  = CM_FLSHARE_OFFSET_LOW;
292         LLength.HighPart = CM_FLSHARE_LENGTH_HIGH;
293         LLength.LowPart  = CM_FLSHARE_LENGTH_LOW;
294
295         code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp, NULL);
296
297         if (code == 0) {
298             cm_Unlock(scp, sLockType, LOffset, LLength, key, userp, reqp);
299         } else {
300             /* In this case, we allow the file open to go through even
301                though we can't enforce mandatory locking on the
302                file. */
303             if (code == CM_ERROR_NOACCESS &&
304                 !(rights & PRSFS_WRITE))
305                 code = 0;
306             else {
307                 switch (code) {
308                 case CM_ERROR_ALLOFFLINE:
309                 case CM_ERROR_ALLDOWN:
310                 case CM_ERROR_ALLBUSY:
311                 case CM_ERROR_TIMEDOUT:
312                 case CM_ERROR_RETRY:
313                 case CM_ERROR_WOULDBLOCK:
314                     break;
315                 default:
316                     code = CM_ERROR_SHARING_VIOLATION;
317                 }
318             }
319         }
320
321     } else if (code != 0) {
322         goto _done;
323     }
324
325     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
326
327  _done:
328
329     lock_ReleaseMutex(&scp->mx);
330
331     return code;
332 }
333
334 /* return success if we can open this file in this mode */
335 long cm_CheckNTOpen(cm_scache_t *scp, unsigned int desiredAccess,
336                     unsigned int createDisp, cm_user_t *userp, cm_req_t *reqp, 
337                     cm_lock_data_t **ldpp)
338 {
339     long rights;
340     long code;
341
342     osi_assertx(ldpp != NULL, "null cm_lock_data_t");
343     *ldpp = NULL;
344
345     /* Always allow delete; the RPC will tell us if it's OK */
346     if (desiredAccess == DELETE)
347         return 0;
348
349     rights = 0;
350
351     if (desiredAccess & AFS_ACCESS_READ)
352         rights |= (scp->fileType == CM_SCACHETYPE_DIRECTORY ? PRSFS_LOOKUP : PRSFS_READ);
353
354     /* We used to require PRSFS_WRITE if createDisp was 4
355        (OPEN_ALWAYS) even if AFS_ACCESS_WRITE was not requested.
356        However, we don't need to do that since the existence of the
357        scp implies that we don't need to create it. */
358     if (desiredAccess & AFS_ACCESS_WRITE)
359         rights |= PRSFS_WRITE;
360
361     lock_ObtainMutex(&scp->mx);
362
363     code = cm_SyncOp(scp, NULL, userp, reqp, rights,
364                       CM_SCACHESYNC_GETSTATUS
365                      | CM_SCACHESYNC_NEEDCALLBACK
366                      | CM_SCACHESYNC_LOCK);
367
368     /*
369      * If the open will fail because the volume is readonly, then we will
370      * return an access denied error instead.  This is to help brain-dead
371      * apps run correctly on replicated volumes.
372      * See defect 10007 for more information.
373      */
374     if (code == CM_ERROR_READONLY)
375         code = CM_ERROR_NOACCESS;
376
377     if (code == 0 &&
378              ((rights & PRSFS_WRITE) || (rights & PRSFS_READ)) &&
379              scp->fileType == CM_SCACHETYPE_FILE) {
380         cm_key_t key;
381         unsigned int sLockType;
382         LARGE_INTEGER LOffset, LLength;
383
384         /* Check if there's some sort of lock on the file at the
385            moment. */
386
387         key = cm_GenerateKey(CM_SESSION_CMINT,0,0);
388         if (rights & PRSFS_WRITE)
389             sLockType = 0;
390         else
391             sLockType = LOCKING_ANDX_SHARED_LOCK;
392
393         /* single byte lock at offset 0x0100 0000 0000 0000 */
394         LOffset.HighPart = CM_FLSHARE_OFFSET_HIGH;
395         LOffset.LowPart  = CM_FLSHARE_OFFSET_LOW;
396         LLength.HighPart = CM_FLSHARE_LENGTH_HIGH;
397         LLength.LowPart  = CM_FLSHARE_LENGTH_LOW;
398
399         code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp, NULL);
400
401         if (code == 0) {
402             (*ldpp) = (cm_lock_data_t *)malloc(sizeof(cm_lock_data_t));
403             if (!*ldpp) {
404                 code = ENOMEM;
405                 goto _done;
406             }
407
408             (*ldpp)->key = key;
409             (*ldpp)->sLockType = sLockType;
410             (*ldpp)->LOffset.HighPart = LOffset.HighPart;
411             (*ldpp)->LOffset.LowPart = LOffset.LowPart;
412             (*ldpp)->LLength.HighPart = LLength.HighPart;
413             (*ldpp)->LLength.LowPart = LLength.LowPart;
414         } else {
415             /* In this case, we allow the file open to go through even
416                though we can't enforce mandatory locking on the
417                file. */
418             if (code == CM_ERROR_NOACCESS &&
419                 !(rights & PRSFS_WRITE))
420                 code = 0;
421             else {
422                 switch (code) {
423                 case CM_ERROR_ALLOFFLINE:
424                 case CM_ERROR_ALLDOWN:
425                 case CM_ERROR_ALLBUSY:
426                 case CM_ERROR_TIMEDOUT:
427                 case CM_ERROR_RETRY:
428                 case CM_ERROR_WOULDBLOCK:
429                     break;
430                 default:
431                     code = CM_ERROR_SHARING_VIOLATION;
432                 }
433             }
434         }
435     } else if (code != 0) {
436         goto _done;
437     }
438
439     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
440
441  _done:
442     lock_ReleaseMutex(&scp->mx);
443
444     return code;
445 }
446
447 extern long cm_CheckNTOpenDone(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp, 
448                                cm_lock_data_t ** ldpp)
449 {
450     if (*ldpp) {
451         lock_ObtainMutex(&scp->mx);
452         cm_Unlock(scp, (*ldpp)->sLockType, (*ldpp)->LOffset, (*ldpp)->LLength, 
453                   (*ldpp)->key, userp, reqp);
454         lock_ReleaseMutex(&scp->mx);
455         free(*ldpp);
456         *ldpp = NULL;
457     }
458     return 0;
459 }
460 /*
461  * When CAP_NT_SMBS has been negotiated, deletion (of files or directories) is
462  * done in three steps:
463  * (1) open for deletion (NT_CREATE_AND_X)
464  * (2) set for deletion on close (NT_TRANSACTION2, SET_FILE_INFO)
465  * (3) close (CLOSE)
466  * We must not do the RPC until step 3.  But if we are going to return an error
467  * code (e.g. directory not empty), we must return it by step 2, otherwise most
468  * clients will not notice it.  So we do a preliminary check.  For deleting
469  * files, this is almost free, since we have already done the RPC to get the
470  * parent directory's status bits.  But for deleting directories, we must do an
471  * additional RPC to get the directory's data to check if it is empty.  Sigh.
472  */
473 long cm_CheckNTDelete(cm_scache_t *dscp, cm_scache_t *scp, cm_user_t *userp,
474         cm_req_t *reqp)
475 {
476     long code;
477     osi_hyper_t thyper;
478     cm_buf_t *bufferp;
479     cm_dirEntry_t *dep = 0;
480     unsigned short *hashTable;
481     unsigned int i, idx;
482     int BeyondPage = 0, HaveDot = 0, HaveDotDot = 0;
483
484     /* First check permissions */
485     lock_ObtainMutex(&scp->mx);
486     code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_DELETE,
487                       CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
488     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
489     lock_ReleaseMutex(&scp->mx);
490     if (code)
491         return code;
492
493     /* If deleting directory, must be empty */
494
495     if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
496         return code;
497
498     thyper.HighPart = 0; thyper.LowPart = 0;
499     lock_ObtainRead(&scp->bufCreateLock);
500     code = buf_Get(scp, &thyper, &bufferp);
501     lock_ReleaseRead(&scp->bufCreateLock);
502     if (code)
503         return code;
504
505     lock_ObtainMutex(&bufferp->mx);
506     lock_ObtainMutex(&scp->mx);
507     while (1) {
508         code = cm_SyncOp(scp, bufferp, userp, reqp, 0,
509                           CM_SCACHESYNC_NEEDCALLBACK
510                           | CM_SCACHESYNC_READ
511                           | CM_SCACHESYNC_BUFLOCKED);
512         if (code)
513             break;
514
515         if (cm_HaveBuffer(scp, bufferp, 1))
516             break;
517
518         /* otherwise, load the buffer and try again */
519         lock_ReleaseMutex(&bufferp->mx);
520         code = cm_GetBuffer(scp, bufferp, NULL, userp, reqp);
521         lock_ReleaseMutex(&scp->mx);
522         lock_ObtainMutex(&bufferp->mx);
523         lock_ObtainMutex(&scp->mx);
524         cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
525         if (code)
526             break;
527     }
528
529     /* We try to determine emptiness without looking beyond the first page,
530      * and without assuming "." and ".." are present and are on the first
531      * page (though these assumptions might, after all, be reasonable).
532      */
533     hashTable = (unsigned short *)(bufferp->datap + (32 * 5));
534     for (i=0; i<128; i++) {
535         idx = ntohs(hashTable[i]);
536         while (idx) {
537             if (idx >= 64) {
538                 BeyondPage = 1;
539                 break;
540             }
541             dep = (cm_dirEntry_t *)(bufferp->datap + (32 * idx));
542             if (strcmp(dep->name, ".") == 0)
543                 HaveDot = 1;
544             else if (strcmp(dep->name, "..") == 0)
545                 HaveDotDot = 1;
546             else {
547                 code = CM_ERROR_NOTEMPTY;
548                 goto done;
549             }
550             idx = ntohs(dep->next);
551         }
552     }
553     if (BeyondPage && HaveDot && HaveDotDot)
554         code = CM_ERROR_NOTEMPTY;
555     else
556         code = 0;
557   done:   
558     lock_ReleaseMutex(&bufferp->mx);
559     buf_Release(bufferp);
560     lock_ReleaseMutex(&scp->mx);
561     return code;
562 }       
563
564 /*
565  * Iterate through all entries in a directory.
566  * When the function funcp is called, the buffer is locked but the
567  * directory vnode is not.
568  *
569  * If the retscp parameter is not NULL, the parmp must be a 
570  * cm_lookupSearch_t object.  
571  */
572 long cm_ApplyDir(cm_scache_t *scp, cm_DirFuncp_t funcp, void *parmp,
573                   osi_hyper_t *startOffsetp, cm_user_t *userp, cm_req_t *reqp,
574                   cm_scache_t **retscp)
575 {
576     char *tp;
577     long code;
578     cm_dirEntry_t *dep = 0;
579     cm_buf_t *bufferp;
580     long temp;
581     osi_hyper_t dirLength;
582     osi_hyper_t bufferOffset;
583     osi_hyper_t curOffset;
584     osi_hyper_t thyper;
585     long entryInDir;
586     long entryInBuffer;
587     cm_pageHeader_t *pageHeaderp;
588     int slotInPage;
589     long nextEntryCookie;
590     int numDirChunks;   /* # of 32 byte dir chunks in this entry */
591         
592     /* get the directory size */
593     lock_ObtainMutex(&scp->mx);
594     code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_LOOKUP,
595                       CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
596     lock_ReleaseMutex(&scp->mx);
597     if (code)
598         return code;
599         
600     if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
601         return CM_ERROR_NOTDIR;
602
603     if (retscp)                         /* if this is a lookup call */
604     {
605         cm_lookupSearch_t*      sp = parmp;
606
607         if (
608 #ifdef AFS_FREELANCE_CLIENT
609         /* Freelance entries never end up in the DNLC because they
610          * do not have an associated cm_server_t
611          */
612             !(cm_freelanceEnabled &&
613             sp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
614               sp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
615 #else /* !AFS_FREELANCE_CLIENT */
616             TRUE
617 #endif
618             ) 
619         {
620             int casefold = sp->caseFold;
621             sp->caseFold = 0; /* we have a strong preference for exact matches */
622             if ( *retscp = cm_dnlcLookup(scp, sp))      /* dnlc hit */
623             {
624                 sp->caseFold = casefold;
625                 return 0;
626             }
627             sp->caseFold = casefold;
628
629             /* see if we can find it using the directory hash tables.
630                we can only do exact matches, since the hash is case
631                sensitive. */
632             {
633                 cm_dirOp_t dirop;
634 #ifdef USE_BPLUS
635                 int usedBplus = 0;
636 #endif
637
638                 code = ENOENT;
639
640                 code = cm_BeginDirOp(scp, userp, reqp, CM_DIRLOCK_READ, &dirop);
641                 if (code == 0) {
642
643 #ifdef USE_BPLUS
644                     code = cm_BPlusDirLookup(&dirop, sp->searchNamep, &sp->fid);
645                     if (code != EINVAL)
646                         usedBplus = 1;
647                     else 
648 #endif
649                         code = cm_DirLookup(&dirop, sp->searchNamep, &sp->fid);
650
651                     cm_EndDirOp(&dirop);
652                 }
653
654                 if (code == 0) {
655                     /* found it */
656                     sp->found = TRUE;
657                     sp->ExactFound = TRUE;
658                     *retscp = NULL; /* force caller to call cm_GetSCache() */
659                     return 0;
660                 }
661 #ifdef USE_BPLUS
662                 if (usedBplus) {
663                     if (sp->caseFold && code == CM_ERROR_INEXACT_MATCH) {
664                         /* found it */
665                         sp->found = TRUE;
666                         sp->ExactFound = FALSE;
667                         *retscp = NULL; /* force caller to call cm_GetSCache() */
668                         return 0;
669                     }
670                     
671                     return CM_ERROR_BPLUS_NOMATCH;
672                 }
673 #endif 
674             }
675         }
676     }   
677
678     /*
679      * XXX We only get the length once.  It might change when we drop the
680      * lock.
681      */
682     dirLength = scp->length;
683
684     bufferp = NULL;
685     bufferOffset.LowPart = bufferOffset.HighPart = 0;
686     if (startOffsetp)
687         curOffset = *startOffsetp;
688     else {
689         curOffset.HighPart = 0;
690         curOffset.LowPart = 0;
691     }   
692
693     while (1) {
694         /* make sure that curOffset.LowPart doesn't point to the first
695          * 32 bytes in the 2nd through last dir page, and that it
696          * doesn't point at the first 13 32-byte chunks in the first
697          * dir page, since those are dir and page headers, and don't
698          * contain useful information.
699          */
700         temp = curOffset.LowPart & (2048-1);
701         if (curOffset.HighPart == 0 && curOffset.LowPart < 2048) {
702             /* we're in the first page */
703             if (temp < 13*32) temp = 13*32;
704         }
705         else {
706             /* we're in a later dir page */
707             if (temp < 32) temp = 32;
708         }       
709                 
710         /* make sure the low order 5 bits are zero */
711         temp &= ~(32-1);
712                 
713         /* now put temp bits back ito curOffset.LowPart */
714         curOffset.LowPart &= ~(2048-1);
715         curOffset.LowPart |= temp;
716
717         /* check if we've passed the dir's EOF */
718         if (LargeIntegerGreaterThanOrEqualTo(curOffset, dirLength))
719             break;
720                 
721         /* see if we can use the bufferp we have now; compute in which
722          * page the current offset would be, and check whether that's
723          * the offset of the buffer we have.  If not, get the buffer.
724          */
725         thyper.HighPart = curOffset.HighPart;
726         thyper.LowPart = curOffset.LowPart & ~(cm_data.buf_blockSize-1);
727         if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
728             /* wrong buffer */
729             if (bufferp) {
730                 lock_ReleaseMutex(&bufferp->mx);
731                 buf_Release(bufferp);
732                 bufferp = NULL;
733             }
734
735             lock_ObtainRead(&scp->bufCreateLock);
736             code = buf_Get(scp, &thyper, &bufferp);
737             lock_ReleaseRead(&scp->bufCreateLock);
738             if (code) {
739                 /* if buf_Get() fails we do not have a buffer object to lock */
740                 bufferp = NULL;
741                 break;
742             }
743
744 #ifdef AFSIFS
745             /* for the IFS version, we bulkstat the dirents because this
746                routine is used in place of smb_ReceiveCoreSearchDir.  our
747                other option is to modify smb_ReceiveCoreSearchDir itself, 
748                but this seems to be the proper use for cm_ApplyDir. */
749             lock_ObtainMutex(&scp->mx);
750             if ((scp->flags & CM_SCACHEFLAG_BULKSTATTING) == 0
751                  && (scp->bulkStatProgress.QuadPart <= thyper.QuadPart))
752             {
753                 scp->flags |= CM_SCACHEFLAG_BULKSTATTING;
754                 code = cm_TryBulkStat(scp, &thyper, userp, reqp);
755                 scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
756                 scp->bulkStatProgress = thyper;
757             }
758             lock_ReleaseMutex(&scp->mx);
759 #endif
760
761             lock_ObtainMutex(&bufferp->mx);
762             bufferOffset = thyper;
763
764             /* now get the data in the cache */
765             while (1) {
766                 lock_ObtainMutex(&scp->mx);
767                 code = cm_SyncOp(scp, bufferp, userp, reqp,
768                                   PRSFS_LOOKUP,
769                                   CM_SCACHESYNC_NEEDCALLBACK
770                                   | CM_SCACHESYNC_READ
771                                   | CM_SCACHESYNC_BUFLOCKED);
772                 if (code) {
773                     lock_ReleaseMutex(&scp->mx);
774                     break;
775                 }
776                 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
777                                 
778                 if (cm_HaveBuffer(scp, bufferp, 1)) {
779                     lock_ReleaseMutex(&scp->mx);
780                     break;
781                 }
782
783                 /* otherwise, load the buffer and try again */
784                 lock_ReleaseMutex(&bufferp->mx);
785                 code = cm_GetBuffer(scp, bufferp, NULL, userp,
786                                     reqp);
787                 lock_ReleaseMutex(&scp->mx);
788                 lock_ObtainMutex(&bufferp->mx);
789                 if (code) 
790                     break;
791             }
792             if (code) {
793                 lock_ReleaseMutex(&bufferp->mx);
794                 buf_Release(bufferp);
795                 bufferp = NULL;
796                 break;
797             }
798         }       /* if (wrong buffer) ... */
799            
800         /* now we have the buffer containing the entry we're interested
801          * in; copy it out if it represents a non-deleted entry.
802          */
803         entryInDir = curOffset.LowPart & (2048-1);
804         entryInBuffer = curOffset.LowPart & (cm_data.buf_blockSize - 1);
805
806         /* page header will help tell us which entries are free.  Page
807          * header can change more often than once per buffer, since
808          * AFS 3 dir page size may be less than (but not more than) a
809          * buffer package buffer.
810          */
811         /* only look intra-buffer */
812         temp = curOffset.LowPart & (cm_data.buf_blockSize - 1);
813         temp &= ~(2048 - 1);    /* turn off intra-page bits */
814         pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
815
816         /* now determine which entry we're looking at in the page.  If
817          * it is free (there's a free bitmap at the start of the dir),
818          * we should skip these 32 bytes.
819          */
820         slotInPage = (entryInDir & 0x7e0) >> 5;
821         if (!(pageHeaderp->freeBitmap[slotInPage>>3]
822                & (1 << (slotInPage & 0x7)))) {
823             /* this entry is free */
824             numDirChunks = 1;   /* only skip this guy */
825             goto nextEntry;
826         }
827
828         tp = bufferp->datap + entryInBuffer;
829         dep = (cm_dirEntry_t *) tp;     /* now points to AFS3 dir entry */
830
831         /* while we're here, compute the next entry's location, too,
832          * since we'll need it when writing out the cookie into the
833          * dir listing stream.
834          */
835         numDirChunks = cm_NameEntries(dep->name, NULL);
836                 
837         /* compute the offset of the cookie representing the next entry */
838         nextEntryCookie = curOffset.LowPart
839             + (CM_DIR_CHUNKSIZE * numDirChunks);
840
841         if (dep->fid.vnode != 0) {
842             /* this is one of the entries to use: it is not deleted */
843             code = (*funcp)(scp, dep, parmp, &curOffset);
844             if (code) 
845                 break;
846         }       /* if we're including this name */
847                 
848       nextEntry:
849         /* and adjust curOffset to be where the new cookie is */
850         thyper.HighPart = 0;
851         thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
852         curOffset = LargeIntegerAdd(thyper, curOffset);
853     }           /* while copying data for dir listing */
854
855     /* release the mutex */
856     if (bufferp) {
857         lock_ReleaseMutex(&bufferp->mx);
858         buf_Release(bufferp);
859     }
860     return code;
861 }
862
863 int cm_NoneUpper(char *s)
864 {
865     char c;
866     while (c = *s++)
867         if (c >= 'A' && c <= 'Z')
868             return 0;
869     return 1;
870 }
871
872 int cm_NoneLower(char *s)
873 {
874     char c;
875     while (c = *s++)
876         if (c >= 'a' && c <= 'z')
877             return 0;
878     return 1;
879 }
880
881 long cm_LookupSearchProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
882                           osi_hyper_t *offp)
883 {
884     cm_lookupSearch_t *sp;
885     int match;
886     char shortName[13];
887     char *matchName;
888
889     sp = (cm_lookupSearch_t *) rockp;
890
891     matchName = dep->name;
892     if (sp->caseFold)
893         match = cm_stricmp(matchName, sp->searchNamep);
894     else
895         match = strcmp(matchName, sp->searchNamep);
896
897     if (match != 0
898          && sp->hasTilde
899          && !cm_Is8Dot3(dep->name)) {
900         matchName = shortName;
901         cm_Gen8Dot3Name(dep, shortName, NULL);
902         if (sp->caseFold)
903             match = cm_stricmp(matchName, sp->searchNamep);
904         else
905             match = strcmp(matchName, sp->searchNamep);
906     }
907
908     if (match != 0)
909         return 0;
910
911     sp->found = 1;
912     if (!sp->caseFold) 
913         sp->ExactFound = 1;
914
915     if (!sp->caseFold || matchName == shortName) {
916         sp->fid.vnode = ntohl(dep->fid.vnode);
917         sp->fid.unique = ntohl(dep->fid.unique);
918         return CM_ERROR_STOPNOW;
919     }
920
921     /*
922      * If we get here, we are doing a case-insensitive search, and we
923      * have found a match.  Now we determine what kind of match it is:
924      * exact, lower-case, upper-case, or none of the above.  This is done
925      * in order to choose among matches, if there are more than one.
926      */
927
928     /* Exact matches are the best. */
929     match = strcmp(matchName, sp->searchNamep);
930     if (match == 0) {
931         sp->ExactFound = 1;
932         sp->fid.vnode = ntohl(dep->fid.vnode);
933         sp->fid.unique = ntohl(dep->fid.unique);
934         return CM_ERROR_STOPNOW;
935     }
936
937     /* Lower-case matches are next. */
938     if (sp->LCfound)
939         return 0;
940     if (cm_NoneUpper(matchName)) {
941         sp->LCfound = 1;
942         goto inexact;
943     }
944
945     /* Upper-case matches are next. */
946     if (sp->UCfound)
947         return 0;
948     if (cm_NoneLower(matchName)) {
949         sp->UCfound = 1;
950         goto inexact;
951     }
952
953     /* General matches are last. */
954     if (sp->NCfound)
955         return 0;
956     sp->NCfound = 1;
957
958   inexact:
959     sp->fid.vnode = ntohl(dep->fid.vnode);
960     sp->fid.unique = ntohl(dep->fid.unique);
961     return 0;
962 }       
963
964 /* read the contents of a mount point into the appropriate string.
965  * called with locked scp, and returns with locked scp.
966  */
967 long cm_ReadMountPoint(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
968 {
969     long code;
970     cm_buf_t *bufp;
971     osi_hyper_t thyper;
972     int tlen;
973
974     if (scp->mountPointStringp[0]) 
975         return 0;
976         
977     /* otherwise, we have to read it in */
978     lock_ReleaseMutex(&scp->mx);
979
980     lock_ObtainRead(&scp->bufCreateLock);
981     thyper.LowPart = thyper.HighPart = 0;
982     code = buf_Get(scp, &thyper, &bufp);
983     lock_ReleaseRead(&scp->bufCreateLock);
984
985     lock_ObtainMutex(&scp->mx);
986     if (code)
987         return code;
988
989     while (1) {
990         code = cm_SyncOp(scp, bufp, userp, reqp, 0,
991                           CM_SCACHESYNC_READ | CM_SCACHESYNC_NEEDCALLBACK);
992         if (code)
993             goto done;
994
995         cm_SyncOpDone(scp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
996
997         if (cm_HaveBuffer(scp, bufp, 0)) 
998             break;
999
1000         /* otherwise load buffer */
1001         code = cm_GetBuffer(scp, bufp, NULL, userp, reqp);
1002         if (code)
1003             goto done;
1004     }
1005     /* locked, has callback, has valid data in buffer */
1006     if ((tlen = scp->length.LowPart) > MOUNTPOINTLEN - 1) 
1007         return CM_ERROR_TOOBIG;
1008     if (tlen <= 0) {
1009         code = CM_ERROR_INVAL;
1010         goto done;
1011     }
1012
1013     /* someone else did the work while we were out */
1014     if (scp->mountPointStringp[0]) {
1015         code = 0;
1016         goto done;
1017     }
1018
1019     /* otherwise, copy out the link */
1020     memcpy(scp->mountPointStringp, bufp->datap, tlen);
1021
1022     /* now make it null-terminated.  Note that the original contents of a
1023      * link that is a mount point is "#volname." where "." is there just to
1024      * be turned into a null.  That is, we can trash the last char of the
1025      * link without damaging the vol name.  This is a stupid convention,
1026      * but that's the protocol.
1027      */
1028     scp->mountPointStringp[tlen-1] = 0;
1029     code = 0;
1030
1031   done:
1032     if (bufp) 
1033         buf_Release(bufp);
1034     return code;
1035 }
1036
1037
1038 /* called with a locked scp and chases the mount point, yielding outScpp.
1039  * scp remains locked, just for simplicity of describing the interface.
1040  */
1041 long cm_FollowMountPoint(cm_scache_t *scp, cm_scache_t *dscp, cm_user_t *userp,
1042                          cm_req_t *reqp, cm_scache_t **outScpp)
1043 {
1044     char *cellNamep;
1045     char *volNamep;
1046     int tlen;
1047     long code;
1048     char *cp;
1049     char *mpNamep;
1050     cm_volume_t *volp = NULL;
1051     cm_cell_t *cellp;
1052     char mtType;
1053     cm_fid_t tfid;
1054     size_t vnLength;
1055     int targetType;
1056
1057     if (scp->mountRootFid.cell != 0 && scp->mountRootGen >= cm_data.mountRootGen) {
1058         tfid = scp->mountRootFid;
1059         lock_ReleaseMutex(&scp->mx);
1060         code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1061         lock_ObtainMutex(&scp->mx);
1062         return code;
1063     }
1064
1065     /* parse the volume name */
1066     mpNamep = scp->mountPointStringp;
1067     if (!mpNamep[0])
1068         return CM_ERROR_NOSUCHPATH;
1069     tlen = (int)strlen(scp->mountPointStringp);
1070     mtType = *scp->mountPointStringp;
1071     cellNamep = malloc(tlen);
1072     volNamep = malloc(tlen);
1073
1074     cp = strrchr(mpNamep, ':');
1075     if (cp) {
1076         /* cellular mount point */
1077         memset(cellNamep, 0, tlen);
1078         strncpy(cellNamep, mpNamep+1, cp - mpNamep - 1);
1079         strcpy(volNamep, cp+1);
1080         /* now look up the cell */
1081         lock_ReleaseMutex(&scp->mx);
1082         cellp = cm_GetCell(cellNamep, CM_FLAG_CREATE);
1083         lock_ObtainMutex(&scp->mx);
1084     }
1085     else {
1086         /* normal mt pt */
1087         strcpy(volNamep, mpNamep+1);
1088
1089         cellp = cm_FindCellByID(scp->fid.cell);
1090     }
1091
1092     if (!cellp) {
1093         code = CM_ERROR_NOSUCHCELL;
1094         goto done;
1095     }
1096
1097     vnLength = strlen(volNamep);
1098     if (vnLength >= 8 && strcmp(volNamep + vnLength - 7, ".backup") == 0)
1099         targetType = BACKVOL;
1100     else if (vnLength >= 10
1101               && strcmp(volNamep + vnLength - 9, ".readonly") == 0)
1102         targetType = ROVOL;
1103     else
1104         targetType = RWVOL;
1105
1106     /* check for backups within backups */
1107     if (targetType == BACKVOL
1108          && (scp->flags & (CM_SCACHEFLAG_RO | CM_SCACHEFLAG_PURERO))
1109          == CM_SCACHEFLAG_RO) {
1110         code = CM_ERROR_NOSUCHVOLUME;
1111         goto done;
1112     }
1113
1114     /* now we need to get the volume */
1115     lock_ReleaseMutex(&scp->mx);
1116     if (cm_VolNameIsID(volNamep)) {
1117         code = cm_GetVolumeByID(cellp, atoi(volNamep), userp, reqp, 
1118                                 CM_GETVOL_FLAG_CREATE, &volp);
1119     } else {
1120         code = cm_GetVolumeByName(cellp, volNamep, userp, reqp, 
1121                                   CM_GETVOL_FLAG_CREATE, &volp);
1122     }
1123     lock_ObtainMutex(&scp->mx);
1124         
1125     if (code == 0) {
1126         /* save the parent of the volume root for this is the 
1127          * place where the volume is mounted and we must remember 
1128          * this in the volume structure rather than just in the 
1129          * scache entry lest the scache entry gets recycled 
1130          * (defect 11489)
1131          */
1132         lock_ObtainMutex(&volp->mx);
1133         volp->dotdotFid = dscp->fid;
1134         lock_ReleaseMutex(&volp->mx);
1135
1136         scp->mountRootFid.cell = cellp->cellID;
1137         
1138         /* if the mt pt originates in a .backup volume (not a .readonly)
1139          * and FollowBackupPath is active, and if there is a .backup
1140          * volume for the target, then use the .backup of the target
1141          * instead of the read-write.
1142          */
1143         if (cm_followBackupPath && targetType == RWVOL &&
1144             (scp->flags & CM_SCACHEFLAG_RO|CM_SCACHEFLAG_PURERO) == CM_SCACHEFLAG_RO &&
1145             volp->bk.ID != 0) {
1146             targetType = BACKVOL;
1147         } 
1148         /* if the mt pt is in a read-only volume (not just a
1149          * backup), and if there is a read-only volume for the
1150          * target, and if this is a targetType '#' mount point, use
1151          * the read-only, otherwise use the one specified.
1152          */
1153         else if (mtType == '#' && targetType == RWVOL && 
1154                  (scp->flags & CM_SCACHEFLAG_PURERO) && 
1155                  volp->ro.ID != 0) {
1156             targetType = ROVOL;
1157         }
1158         if (targetType == ROVOL)
1159             scp->mountRootFid.volume = volp->ro.ID;
1160         else if (targetType == BACKVOL)
1161             scp->mountRootFid.volume = volp->bk.ID;
1162         else
1163             scp->mountRootFid.volume = volp->rw.ID;
1164
1165         /* the rest of the fid is a magic number */
1166         scp->mountRootFid.vnode = 1;
1167         scp->mountRootFid.unique = 1;
1168         scp->mountRootGen = cm_data.mountRootGen;
1169
1170         tfid = scp->mountRootFid;
1171         lock_ReleaseMutex(&scp->mx);
1172         code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1173         lock_ObtainMutex(&scp->mx);
1174     }
1175
1176   done:
1177     if (volp)
1178         cm_PutVolume(volp);
1179     free(cellNamep);
1180     free(volNamep);
1181     return code;
1182 }       
1183
1184 long cm_LookupInternal(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1185                        cm_req_t *reqp, cm_scache_t **outpScpp)
1186 {
1187     long code;
1188     int dnlcHit = 1;    /* did we hit in the dnlc? yes, we did */
1189     cm_scache_t *tscp = NULL;
1190     cm_scache_t *mountedScp;
1191     cm_lookupSearch_t rock;
1192     int getroot;
1193
1194     memset(&rock, 0, sizeof(rock));
1195
1196     if (dscp->fid.vnode == 1 && dscp->fid.unique == 1
1197          && strcmp(namep, "..") == 0) {
1198         if (dscp->dotdotFid.volume == 0)
1199             return CM_ERROR_NOSUCHVOLUME;
1200         rock.fid = dscp->dotdotFid;
1201         goto haveFid;
1202     } else if (strcmp(namep, ".") == 0) {
1203         rock.fid = dscp->fid;
1204         goto haveFid;
1205     }
1206
1207     if (flags & CM_FLAG_NOMOUNTCHASE) {
1208         /* In this case, we should go and call cm_Dir* functions
1209            directly since the following cm_ApplyDir() function will
1210            not. */
1211
1212         cm_dirOp_t dirop;
1213 #ifdef USE_BPLUS
1214         int usedBplus = 0;
1215 #endif
1216
1217         code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ, &dirop);
1218         if (code == 0) {
1219 #ifdef USE_BPLUS
1220             code = cm_BPlusDirLookup(&dirop, namep, &rock.fid);
1221             if (code != EINVAL)
1222                 usedBplus = 1;
1223             else
1224 #endif
1225                 code = cm_DirLookup(&dirop, namep, &rock.fid);
1226
1227             cm_EndDirOp(&dirop);
1228         }
1229
1230         if (code == 0) {
1231             /* found it */
1232             rock.found = TRUE;
1233             goto haveFid;
1234         }
1235 #ifdef USE_BPLUS
1236         if (usedBplus) {
1237             if (code == CM_ERROR_INEXACT_MATCH && (flags & CM_FLAG_CASEFOLD)) {
1238                 /* found it */
1239                 code = 0;
1240                 rock.found = TRUE;
1241                 goto haveFid;
1242             }
1243             
1244             return CM_ERROR_BPLUS_NOMATCH;
1245         }
1246 #endif
1247     }
1248
1249     rock.fid.cell = dscp->fid.cell;
1250     rock.fid.volume = dscp->fid.volume;
1251     rock.searchNamep = namep;
1252     rock.caseFold = (flags & CM_FLAG_CASEFOLD);
1253     rock.hasTilde = ((strchr(namep, '~') != NULL) ? 1 : 0);
1254
1255     /* If NOMOUNTCHASE, bypass DNLC by passing NULL scp pointer */
1256     code = cm_ApplyDir(dscp, cm_LookupSearchProc, &rock, NULL, userp, reqp,
1257                         (flags & CM_FLAG_NOMOUNTCHASE) ? NULL : &tscp);
1258
1259     /* code == 0 means we fell off the end of the dir, while stopnow means
1260      * that we stopped early, probably because we found the entry we're
1261      * looking for.  Any other non-zero code is an error.
1262      */
1263     if (code && code != CM_ERROR_STOPNOW) {
1264         /* if the cm_scache_t we are searching in is not a directory 
1265          * we must return path not found because the error 
1266          * is to describe the final component not an intermediary
1267          */
1268         if (code == CM_ERROR_NOTDIR) {
1269             if (flags & CM_FLAG_CHECKPATH)
1270                 return CM_ERROR_NOSUCHPATH;
1271             else
1272                 return CM_ERROR_NOSUCHFILE;
1273         }
1274         return code;
1275     }
1276
1277     getroot = (dscp==cm_data.rootSCachep) ;
1278     if (!rock.found) {
1279         if (!cm_freelanceEnabled || !getroot) {
1280             if (flags & CM_FLAG_CHECKPATH)
1281                 return CM_ERROR_NOSUCHPATH;
1282             else
1283                 return CM_ERROR_NOSUCHFILE;
1284         }
1285         else {  /* nonexistent dir on freelance root, so add it */
1286             char fullname[200] = ".";
1287             int  found = 0;
1288
1289             osi_Log1(afsd_logp,"cm_Lookup adding mount for non-existent directory: %s", 
1290                       osi_LogSaveString(afsd_logp,namep));
1291             if (namep[0] == '.') {
1292                 if (cm_GetCell_Gen(&namep[1], &fullname[1], CM_FLAG_CREATE)) {
1293                     found = 1;
1294                     if ( stricmp(&namep[1], &fullname[1]) )
1295                         code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1296                     else
1297                         code = cm_FreelanceAddMount(namep, &fullname[1], "root.cell.", 1, &rock.fid);
1298                 }
1299             } else {
1300                 if (cm_GetCell_Gen(namep, fullname, CM_FLAG_CREATE)) {
1301                     found = 1;
1302                     if ( stricmp(namep, fullname) )
1303                         code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1304                     else
1305                         code = cm_FreelanceAddMount(namep, fullname, "root.cell.", 0, &rock.fid);
1306                 }
1307             }
1308             if (!found || code < 0) {   /* add mount point failed, so give up */
1309                 if (flags & CM_FLAG_CHECKPATH)
1310                     return CM_ERROR_NOSUCHPATH;
1311                 else
1312                     return CM_ERROR_NOSUCHFILE;
1313             }
1314             tscp = NULL;   /* to force call of cm_GetSCache */
1315         }
1316     }
1317
1318   haveFid:       
1319     if ( !tscp )    /* we did not find it in the dnlc */
1320     {
1321         dnlcHit = 0;    
1322         code = cm_GetSCache(&rock.fid, &tscp, userp, reqp);
1323         if (code) 
1324             return code;
1325     }       
1326     /* tscp is now held */
1327
1328     lock_ObtainMutex(&tscp->mx);
1329     code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1330                       CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1331     if (code) { 
1332         lock_ReleaseMutex(&tscp->mx);
1333         cm_ReleaseSCache(tscp);
1334         return code;
1335     }
1336     cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1337     /* tscp is now locked */
1338
1339     if (!(flags & CM_FLAG_NOMOUNTCHASE)
1340          && tscp->fileType == CM_SCACHETYPE_MOUNTPOINT) {
1341         /* mount points are funny: they have a volume name to mount
1342          * the root of.
1343          */
1344         code = cm_ReadMountPoint(tscp, userp, reqp);
1345         if (code == 0)
1346             code = cm_FollowMountPoint(tscp, dscp, userp, reqp,
1347                                         &mountedScp);
1348         lock_ReleaseMutex(&tscp->mx);
1349         cm_ReleaseSCache(tscp);
1350         if (code) {
1351             return code;
1352         }
1353         tscp = mountedScp;
1354     }
1355     else {
1356         lock_ReleaseMutex(&tscp->mx);
1357     }
1358
1359     /* copy back pointer */
1360     *outpScpp = tscp;
1361
1362     /* insert scache in dnlc */
1363     if ( !dnlcHit && !(flags & CM_FLAG_NOMOUNTCHASE) && rock.ExactFound ) {
1364         /* lock the directory entry to prevent racing callback revokes */
1365         lock_ObtainMutex(&dscp->mx);
1366         if ( dscp->cbServerp != NULL && dscp->cbExpires > 0 )
1367             cm_dnlcEnter(dscp, namep, tscp);
1368         lock_ReleaseMutex(&dscp->mx);
1369     }
1370
1371     /* and return */
1372     return 0;
1373 }
1374
1375 int cm_ExpandSysName(char *inp, char *outp, long outSize, unsigned int index)
1376 {
1377     char *tp;
1378     int prefixCount;
1379
1380     tp = strrchr(inp, '@');
1381     if (tp == NULL) 
1382         return 0;               /* no @sys */
1383
1384     if (strcmp(tp, "@sys") != 0) 
1385         return 0;       /* no @sys */
1386
1387     /* caller just wants to know if this is a valid @sys type of name */
1388     if (outp == NULL) 
1389         return 1;
1390
1391     if (index >= MAXNUMSYSNAMES)
1392         return -1;
1393
1394     /* otherwise generate the properly expanded @sys name */
1395     prefixCount = (int)(tp - inp);
1396
1397     strncpy(outp, inp, prefixCount);    /* copy out "a." from "a.@sys" */
1398     outp[prefixCount] = 0;              /* null terminate the "a." */
1399     strcat(outp, cm_sysNameList[index]);/* append i386_nt40 */
1400     return 1;
1401 }   
1402
1403 long cm_EvaluateVolumeReference(char * namep, long flags, cm_user_t * userp,
1404                                 cm_req_t *reqp, cm_scache_t ** outpScpp)
1405 {
1406     long          code = 0;
1407     char          cellName[CELL_MAXNAMELEN];
1408     char          volumeName[VL_MAXNAMELEN];
1409     size_t        len;
1410     char *        cp;
1411     char *        tp;
1412
1413     cm_cell_t *   cellp = NULL;
1414     cm_volume_t * volp = NULL;
1415     cm_fid_t      fid;
1416     int           volType;
1417     int           mountType = RWVOL;
1418
1419     osi_Log1(afsd_logp, "cm_EvaluateVolumeReference for string [%s]",
1420              osi_LogSaveString(afsd_logp, namep));
1421
1422     if (strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) != 0) {
1423         goto _exit_invalid_path;
1424     }
1425
1426     /* namep is assumed to look like the following:
1427
1428        @vol:<cellname>%<volume>\0
1429        or
1430        @vol:<cellname>#<volume>\0
1431
1432      */
1433
1434     cp = namep + CM_PREFIX_VOL_CCH; /* cp points to cell name, hopefully */
1435     tp = strchr(cp, '%');
1436     if (tp == NULL)
1437         tp = strchr(cp, '#');
1438     if (tp == NULL ||
1439         (len = tp - cp) == 0 ||
1440         len > CELL_MAXNAMELEN)
1441         goto _exit_invalid_path;
1442     strncpy(cellName, cp, len);
1443     cellName[len] = '\0';
1444
1445     if (*tp == '#')
1446         mountType = ROVOL;
1447
1448     cp = tp+1;                  /* cp now points to volume, supposedly */
1449     strncpy(volumeName, cp, VL_MAXNAMELEN-1);
1450     volumeName[VL_MAXNAMELEN - 1] = 0;
1451
1452     /* OK, now we have the cell and the volume */
1453     osi_Log2(afsd_logp, "   Found cell [%s] and volume [%s]",
1454              osi_LogSaveString(afsd_logp, cellName),
1455              osi_LogSaveString(afsd_logp, volumeName));
1456
1457     cellp = cm_GetCell(cellName, CM_FLAG_CREATE);
1458     if (cellp == NULL) {
1459         goto _exit_invalid_path;
1460     }
1461
1462     len = strlen(volumeName);
1463     if (len >= 8 && strcmp(volumeName + len - 7, ".backup") == 0)
1464         volType = BACKVOL;
1465     else if (len >= 10 &&
1466              strcmp(volumeName + len - 9, ".readonly") == 0)
1467         volType = ROVOL;
1468     else
1469         volType = RWVOL;
1470
1471     if (cm_VolNameIsID(volumeName)) {
1472         code = cm_GetVolumeByID(cellp, atoi(volumeName), userp, reqp,
1473                                 CM_GETVOL_FLAG_CREATE, &volp);
1474     } else {
1475         code = cm_GetVolumeByName(cellp, volumeName, userp, reqp,
1476                                   CM_GETVOL_FLAG_CREATE, &volp);
1477     }
1478
1479     if (code != 0)
1480         goto _exit_cleanup;
1481
1482     fid.cell = cellp->cellID;
1483
1484     if (volType == BACKVOL)
1485         fid.volume = volp->bk.ID;
1486     else if (volType == ROVOL ||
1487              (volType == RWVOL && mountType == ROVOL && volp->ro.ID != 0))
1488         fid.volume = volp->ro.ID;
1489     else
1490         fid.volume = volp->rw.ID;
1491
1492     fid.vnode = 1;
1493     fid.unique = 1;
1494
1495     code = cm_GetSCache(&fid, outpScpp, userp, reqp);
1496
1497  _exit_cleanup:
1498     if (volp)
1499         cm_PutVolume(volp);
1500
1501     if (code == 0)
1502         return code;
1503
1504  _exit_invalid_path:
1505     if (flags & CM_FLAG_CHECKPATH)
1506         return CM_ERROR_NOSUCHPATH;
1507     else
1508         return CM_ERROR_NOSUCHFILE;
1509 }
1510
1511 #ifdef DEBUG_REFCOUNT
1512 long cm_LookupDbg(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1513                cm_req_t *reqp, cm_scache_t **outpScpp, char * file, long line)
1514 #else
1515 long cm_Lookup(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1516                cm_req_t *reqp, cm_scache_t **outpScpp)
1517 #endif
1518 {
1519     long code;
1520     char tname[AFSPATHMAX];
1521     int sysNameIndex = 0;
1522     cm_scache_t *scp = NULL;
1523
1524 #ifdef DEBUG_REFCOUNT
1525     afsi_log("%s:%d cm_Lookup dscp 0x%p ref %d", file, line, dscp, dscp->refCount, file, line);
1526     osi_Log2(afsd_logp, "cm_Lookup dscp 0x%p ref %d", dscp, dscp->refCount);
1527 #endif
1528
1529     if ( stricmp(namep,SMB_IOCTL_FILENAME_NOSLASH) == 0 ) {
1530         if (flags & CM_FLAG_CHECKPATH)
1531             return CM_ERROR_NOSUCHPATH;
1532         else
1533             return CM_ERROR_NOSUCHFILE;
1534     }
1535
1536     if (dscp == cm_data.rootSCachep &&
1537         strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) == 0) {
1538         return cm_EvaluateVolumeReference(namep, flags, userp, reqp, outpScpp);
1539     }
1540
1541     if (cm_ExpandSysName(namep, NULL, 0, 0) > 0) {
1542         for ( sysNameIndex = 0; sysNameIndex < MAXNUMSYSNAMES; sysNameIndex++) {
1543             code = cm_ExpandSysName(namep, tname, sizeof(tname), sysNameIndex);
1544             if (code > 0) {
1545                 code = cm_LookupInternal(dscp, tname, flags, userp, reqp, &scp);
1546 #ifdef DEBUG_REFCOUNT
1547                 afsi_log("%s:%d cm_LookupInternal (1) code 0x%x dscp 0x%p ref %d scp 0x%p ref %d", file, line, code, dscp, dscp->refCount, scp, scp ? scp->refCount : 0);
1548                 osi_Log3(afsd_logp, "cm_LookupInternal (1) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1549 #endif
1550
1551                 if (code == 0) {
1552                     *outpScpp = scp;
1553                     return 0;
1554                 }
1555                 if (scp) {
1556                     cm_ReleaseSCache(scp);
1557                     scp = NULL;
1558                 }
1559             } else {
1560                 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1561 #ifdef DEBUG_REFCOUNT
1562                 afsi_log("%s:%d cm_LookupInternal (2) code 0x%x dscp 0x%p ref %d scp 0x%p ref %d", file, line, code, dscp, dscp->refCount, scp, scp ? scp->refCount : 0);
1563                 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1564 #endif
1565                 *outpScpp = scp;
1566                 return code;
1567             }
1568         }
1569     } else {
1570         code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1571 #ifdef DEBUG_REFCOUNT
1572         afsi_log("%s:%d cm_LookupInternal (2) code 0x%x dscp 0x%p ref %d scp 0x%p ref %d", file, line, code, dscp, dscp->refCount, scp, scp ? scp->refCount : 0);
1573         osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1574 #endif
1575         *outpScpp = scp;
1576         return code;
1577     }
1578
1579     /* None of the possible sysName expansions could be found */
1580     if (flags & CM_FLAG_CHECKPATH)
1581         return CM_ERROR_NOSUCHPATH;
1582     else
1583         return CM_ERROR_NOSUCHFILE;
1584 }
1585
1586 long cm_Unlink(cm_scache_t *dscp, char *namep, cm_user_t *userp, cm_req_t *reqp)
1587 {
1588     long code;
1589     cm_conn_t *connp;
1590     AFSFid afsFid;
1591     int sflags;
1592     AFSFetchStatus newDirStatus;
1593     AFSVolSync volSync;
1594     struct rx_connection * callp;
1595     cm_dirOp_t dirop;
1596
1597 #ifdef AFS_FREELANCE_CLIENT
1598     if (cm_freelanceEnabled && dscp == cm_data.rootSCachep) {
1599         /* deleting a mount point from the root dir. */
1600         code = cm_FreelanceRemoveMount(namep);
1601         return code;
1602     }
1603 #endif  
1604
1605     /* make sure we don't screw up the dir status during the merge */
1606     code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
1607
1608     lock_ObtainMutex(&dscp->mx);
1609     sflags = CM_SCACHESYNC_STOREDATA;
1610     code = cm_SyncOp(dscp, NULL, userp, reqp, 0, sflags);
1611     lock_ReleaseMutex(&dscp->mx);
1612     if (code) {
1613         cm_EndDirOp(&dirop);
1614         return code;
1615     }
1616
1617     /* make the RPC */
1618     afsFid.Volume = dscp->fid.volume;
1619     afsFid.Vnode = dscp->fid.vnode;
1620     afsFid.Unique = dscp->fid.unique;
1621
1622     osi_Log1(afsd_logp, "CALL RemoveFile scp 0x%p", dscp);
1623     do {
1624         code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
1625         if (code) 
1626             continue;
1627
1628         callp = cm_GetRxConn(connp);
1629         code = RXAFS_RemoveFile(callp, &afsFid, namep,
1630                                  &newDirStatus, &volSync);
1631         rx_PutConnection(callp);
1632
1633     } while (cm_Analyze(connp, userp, reqp, &dscp->fid, &volSync, NULL, NULL, code));
1634     code = cm_MapRPCError(code, reqp);
1635
1636     if (code)
1637         osi_Log1(afsd_logp, "CALL RemoveFile FAILURE, code 0x%x", code);
1638     else
1639         osi_Log0(afsd_logp, "CALL RemoveFile SUCCESS");
1640
1641     if (dirop.scp) {
1642         lock_ObtainWrite(&dirop.scp->dirlock);
1643         dirop.lockType = CM_DIRLOCK_WRITE;
1644     }
1645     lock_ObtainMutex(&dscp->mx);
1646     cm_dnlcRemove(dscp, namep);
1647     cm_SyncOpDone(dscp, NULL, sflags);
1648     if (code == 0) {
1649         cm_MergeStatus(NULL, dscp, &newDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
1650     } else if (code == CM_ERROR_NOSUCHFILE) {
1651         /* windows would not have allowed the request to delete the file 
1652          * if it did not believe the file existed.  therefore, we must 
1653          * have an inconsistent view of the world.
1654          */
1655         dscp->cbServerp = NULL;
1656     }
1657     lock_ReleaseMutex(&dscp->mx);
1658
1659     if (code == 0 && cm_CheckDirOpForSingleChange(&dirop)) {
1660         cm_DirDeleteEntry(&dirop, namep);
1661 #ifdef USE_BPLUS
1662         cm_BPlusDirDeleteEntry(&dirop, namep);
1663 #endif
1664     }
1665     cm_EndDirOp(&dirop);
1666
1667     return code;
1668 }
1669
1670 /* called with a locked vnode, and fills in the link info.
1671  * returns this the vnode still locked.
1672  */
1673 long cm_HandleLink(cm_scache_t *linkScp, cm_user_t *userp, cm_req_t *reqp)
1674 {
1675     long code;
1676     cm_buf_t *bufp;
1677     long temp;
1678     osi_hyper_t thyper;
1679
1680     lock_AssertMutex(&linkScp->mx);
1681     if (!linkScp->mountPointStringp[0]) {
1682         /* read the link data */
1683         lock_ReleaseMutex(&linkScp->mx);
1684         thyper.LowPart = thyper.HighPart = 0;
1685         code = buf_Get(linkScp, &thyper, &bufp);
1686         lock_ObtainMutex(&linkScp->mx);
1687         if (code) 
1688             return code;
1689         while (1) {
1690             code = cm_SyncOp(linkScp, bufp, userp, reqp, 0,
1691                               CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1692             if (code) {
1693                 buf_Release(bufp);
1694                 return code;
1695             }
1696             cm_SyncOpDone(linkScp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1697
1698             if (cm_HaveBuffer(linkScp, bufp, 0)) 
1699                 break;
1700
1701             code = cm_GetBuffer(linkScp, bufp, NULL, userp, reqp);
1702             if (code) {
1703                 buf_Release(bufp);
1704                 return code;
1705             }
1706         } /* while loop to get the data */
1707                 
1708         /* now if we still have no link read in,
1709          * copy the data from the buffer */
1710         if ((temp = linkScp->length.LowPart) >= MOUNTPOINTLEN) {
1711             buf_Release(bufp);
1712             return CM_ERROR_TOOBIG;
1713         }
1714
1715         /* otherwise, it fits; make sure it is still null (could have
1716          * lost race with someone else referencing this link above),
1717          * and if so, copy in the data.
1718          */
1719         if (!linkScp->mountPointStringp[0]) {
1720             strncpy(linkScp->mountPointStringp, bufp->datap, temp);
1721             linkScp->mountPointStringp[temp] = 0;       /* null terminate */
1722
1723             if ( !strnicmp(linkScp->mountPointStringp, "msdfs:", strlen("msdfs:")) )
1724                  linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1725         }
1726         buf_Release(bufp);
1727     }   /* don't have sym link contents cached */
1728
1729     return 0;
1730 }       
1731
1732 /* called with a held vnode and a path suffix, with the held vnode being a
1733  * symbolic link.  Our goal is to generate a new path to interpret, and return
1734  * this new path in newSpaceBufferp.  If the new vnode is relative to a dir
1735  * other than the directory containing the symbolic link, then the new root is
1736  * returned in *newRootScpp, otherwise a null is returned there.
1737  */
1738 long cm_AssembleLink(cm_scache_t *linkScp, char *pathSuffixp,
1739                       cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1740                       cm_user_t *userp, cm_req_t *reqp)
1741 {
1742     long code = 0;
1743     long len;
1744     char *linkp;
1745     cm_space_t *tsp;
1746
1747     *newRootScpp = NULL;
1748     *newSpaceBufferp = NULL;
1749
1750     lock_ObtainMutex(&linkScp->mx);
1751     code = cm_HandleLink(linkScp, userp, reqp);
1752     if (code)
1753         goto done;
1754
1755     /* if we may overflow the buffer, bail out; buffer is signficantly
1756      * bigger than max path length, so we don't really have to worry about
1757      * being a little conservative here.
1758      */
1759     if (strlen(linkScp->mountPointStringp) + strlen(pathSuffixp) + 2
1760          >= CM_UTILS_SPACESIZE)
1761         return CM_ERROR_TOOBIG;
1762
1763     tsp = cm_GetSpace();
1764     linkp = linkScp->mountPointStringp;
1765     if (strncmp(linkp, cm_mountRoot, cm_mountRootLen) == 0) {
1766         if (strlen(linkp) > cm_mountRootLen)
1767             strcpy(tsp->data, linkp+cm_mountRootLen+1);
1768         else
1769             tsp->data[0] = 0;
1770         *newRootScpp = cm_data.rootSCachep;
1771         cm_HoldSCache(cm_data.rootSCachep);
1772     } else if (linkp[0] == '\\' && linkp[1] == '\\') {
1773         if (!strnicmp(&linkp[2], cm_NetbiosName, (len = (long)strlen(cm_NetbiosName)))) 
1774         {
1775             char * p = &linkp[len + 3];
1776             if (strnicmp(p, "all", 3) == 0)
1777                 p += 4;
1778
1779             strcpy(tsp->data, p);
1780             for (p = tsp->data; *p; p++) {
1781                 if (*p == '\\')
1782                     *p = '/';
1783             }
1784             *newRootScpp = cm_data.rootSCachep;
1785             cm_HoldSCache(cm_data.rootSCachep);
1786         } else {
1787             linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1788             strcpy(tsp->data, linkp);
1789             code = CM_ERROR_PATH_NOT_COVERED;
1790         }
1791     } else if ( linkScp->fileType == CM_SCACHETYPE_DFSLINK ||
1792                 !strnicmp(linkp, "msdfs:", (len = (long)strlen("msdfs:"))) ) {
1793         linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1794         strcpy(tsp->data, linkp);
1795         code = CM_ERROR_PATH_NOT_COVERED;
1796     } else if (*linkp == '\\' || *linkp == '/') {
1797 #if 0   
1798         /* formerly, this was considered to be from the AFS root,
1799          * but this seems to create problems.  instead, we will just
1800          * reject the link */
1801         strcpy(tsp->data, linkp+1);
1802         *newRootScpp = cm_data.rootSCachep;
1803         cm_HoldSCache(cm_data.rootSCachep);
1804 #else
1805         /* we still copy the link data into the response so that 
1806          * the user can see what the link points to
1807          */
1808         linkScp->fileType = CM_SCACHETYPE_INVALID;
1809         strcpy(tsp->data, linkp);
1810         code = CM_ERROR_NOSUCHPATH;
1811 #endif  
1812     } else {
1813         /* a relative link */
1814         strcpy(tsp->data, linkp);
1815     }
1816     if (pathSuffixp[0] != 0) {  /* if suffix string is non-null */
1817         strcat(tsp->data, "\\");
1818         strcat(tsp->data, pathSuffixp);
1819     }
1820     if (code == 0)
1821         *newSpaceBufferp = tsp;
1822     else {
1823         cm_FreeSpace(tsp);
1824
1825         if (code == CM_ERROR_PATH_NOT_COVERED && reqp->tidPathp && reqp->relPathp)
1826             cm_VolStatus_Notify_DFS_Mapping(linkScp, reqp->tidPathp, reqp->relPathp);
1827     }
1828
1829   done:
1830     lock_ReleaseMutex(&linkScp->mx);
1831     return code;
1832 }
1833 #ifdef DEBUG_REFCOUNT
1834 long cm_NameIDbg(cm_scache_t *rootSCachep, char *pathp, long flags,
1835                cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp, 
1836                char * file, long line)
1837 #else
1838 long cm_NameI(cm_scache_t *rootSCachep, char *pathp, long flags,
1839                cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp)
1840 #endif
1841 {
1842     long code;
1843     char *tp;                   /* ptr moving through input buffer */
1844     char tc;                    /* temp char */
1845     int haveComponent;          /* has new component started? */
1846     char component[AFSPATHMAX]; /* this is the new component */
1847     char *cp;                   /* component name being assembled */
1848     cm_scache_t *tscp;          /* current location in the hierarchy */
1849     cm_scache_t *nscp;          /* next dude down */
1850     cm_scache_t *dirScp;        /* last dir we searched */
1851     cm_scache_t *linkScp;       /* new root for the symlink we just
1852     * looked up */
1853     cm_space_t *psp;            /* space for current path, if we've hit
1854     * any symlinks */
1855     cm_space_t *tempsp;         /* temp vbl */
1856     char *restp;                /* rest of the pathname to interpret */
1857     int symlinkCount;           /* count of # of symlinks traversed */
1858     int extraFlag;              /* avoid chasing mt pts for dir cmd */
1859     int phase = 1;              /* 1 = tidPathp, 2 = pathp */
1860 #define MAX_FID_COUNT 512
1861     cm_fid_t fids[MAX_FID_COUNT]; /* array of fids processed in this path walk */
1862     int fid_count = 0;          /* number of fids processed in this path walk */
1863     int i;
1864
1865 #ifdef DEBUG_REFCOUNT
1866     afsi_log("%s:%d cm_NameI rootscp 0x%p ref %d", file, line, rootSCachep, rootSCachep->refCount);
1867     osi_Log4(afsd_logp,"cm_NameI rootscp 0x%p path %s tidpath %s flags 0x%x",
1868               rootSCachep, pathp ? pathp : "<NULL>", tidPathp ? tidPathp : "<NULL>", 
1869               flags);
1870 #endif
1871
1872     tp = tidPathp;
1873     if (tp == NULL) {
1874         tp = pathp;
1875         phase = 2;
1876     }
1877     if (tp == NULL) {
1878         tp = "";
1879     }
1880     haveComponent = 0;
1881     psp = NULL;
1882     tscp = rootSCachep;
1883     cm_HoldSCache(tscp);
1884     symlinkCount = 0;
1885     dirScp = NULL;
1886
1887
1888     while (1) {
1889         tc = *tp++;
1890
1891         /* map Unix slashes into DOS ones so we can interpret Unix
1892          * symlinks properly
1893          */
1894         if (tc == '/') 
1895             tc = '\\';
1896
1897         if (!haveComponent) {
1898             if (tc == '\\') {
1899                 continue;
1900             } else if (tc == 0) {
1901                 if (phase == 1) {
1902                     phase = 2;
1903                     tp = pathp;
1904                     continue;
1905                 }
1906                 code = 0;
1907                 break;
1908             } else {
1909                 haveComponent = 1;
1910                 cp = component;
1911                 *cp++ = tc;
1912             }
1913         } else {
1914             /* we have a component here */
1915             if (tc == 0 || tc == '\\') {
1916                 /* end of the component; we're at the last
1917                  * component if tc == 0.  However, if the last
1918                  * is a symlink, we have more to do.
1919                  */
1920                 *cp++ = 0;      /* add null termination */
1921                 extraFlag = 0;
1922                 if ((flags & CM_FLAG_DIRSEARCH) && tc == 0)
1923                     extraFlag = CM_FLAG_NOMOUNTCHASE;
1924                 code = cm_Lookup(tscp, component,
1925                                   flags | extraFlag,
1926                                   userp, reqp, &nscp);
1927
1928                 if (code == 0) {
1929                     if (!strcmp(component,"..") || !strcmp(component,".")) {
1930                         /* 
1931                          * roll back the fid list until we find the fid 
1932                          * that matches where we are now.  Its not necessarily
1933                          * one or two fids because they might have been 
1934                          * symlinks or mount points or both that were crossed.  
1935                          */
1936                         for ( i=fid_count-1; i>=0; i--) {
1937                             if (!cm_FidCmp(&nscp->fid, &fids[i]))
1938                                 break;
1939                         }
1940                     } else {
1941                         /* add the new fid to the list */
1942                         for ( i=0; i<fid_count; i++) {
1943                             if ( !cm_FidCmp(&nscp->fid, &fids[i]) ) {
1944                                 code = CM_ERROR_TOO_MANY_SYMLINKS;
1945                                 cm_ReleaseSCache(nscp);
1946                                 nscp = NULL;
1947                                 break;
1948                             }
1949                         }
1950                         if (i == fid_count && fid_count < MAX_FID_COUNT) {
1951                             fids[fid_count++] = nscp->fid;
1952                         }
1953                     }
1954                 }
1955
1956                 if (code) {
1957                     cm_ReleaseSCache(tscp);
1958                     if (dirScp)
1959                         cm_ReleaseSCache(dirScp);
1960                     if (psp) 
1961                         cm_FreeSpace(psp);
1962                     if ((code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) && 
1963                          tscp->fileType == CM_SCACHETYPE_SYMLINK) 
1964                     {
1965                         osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_NOSUCHPATH");
1966                         return CM_ERROR_NOSUCHPATH;
1967                     } else {
1968                         osi_Log1(afsd_logp,"cm_NameI code 0x%x", code);
1969                         return code;
1970                     }
1971                 }
1972
1973                 haveComponent = 0;      /* component done */
1974                 if (dirScp)
1975                     cm_ReleaseSCache(dirScp);
1976                 dirScp = tscp;          /* for some symlinks */
1977                 tscp = nscp;            /* already held */
1978                 nscp = NULL;
1979                 if (tc == 0 && !(flags & CM_FLAG_FOLLOW) && phase == 2) {
1980                     code = 0;
1981                     if (dirScp) {
1982                         cm_ReleaseSCache(dirScp);
1983                         dirScp = NULL;
1984                     }
1985                     break;
1986                 }
1987
1988                 /* now, if tscp is a symlink, we should follow
1989                  * it and assemble the path again.
1990                  */
1991                 lock_ObtainMutex(&tscp->mx);
1992                 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1993                                   CM_SCACHESYNC_GETSTATUS
1994                                   | CM_SCACHESYNC_NEEDCALLBACK);
1995                 if (code) {
1996                     lock_ReleaseMutex(&tscp->mx);
1997                     cm_ReleaseSCache(tscp);
1998                     tscp = NULL;
1999                     if (dirScp) {
2000                         cm_ReleaseSCache(dirScp);
2001                         dirScp = NULL;
2002                     }
2003                     break;
2004                 }
2005                 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2006
2007                 if (tscp->fileType == CM_SCACHETYPE_SYMLINK) {
2008                     /* this is a symlink; assemble a new buffer */
2009                     lock_ReleaseMutex(&tscp->mx);
2010                     if (symlinkCount++ >= MAX_SYMLINK_COUNT) {
2011                         cm_ReleaseSCache(tscp);
2012                         tscp = NULL;
2013                         if (dirScp) {
2014                             cm_ReleaseSCache(dirScp);
2015                             dirScp = NULL;
2016                         }
2017                         if (psp) 
2018                             cm_FreeSpace(psp);
2019                         osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_TOO_MANY_SYMLINKS");
2020                         return CM_ERROR_TOO_MANY_SYMLINKS;
2021                     }
2022                     if (tc == 0) 
2023                         restp = "";
2024                     else 
2025                         restp = tp;
2026                     code = cm_AssembleLink(tscp, restp, &linkScp, &tempsp, userp, reqp);
2027
2028                     if (code == 0 && linkScp != NULL) {
2029                         if (linkScp == cm_data.rootSCachep) 
2030                             fid_count = 0;
2031                         else {
2032                             for ( i=0; i<fid_count; i++) {
2033                                 if ( !cm_FidCmp(&linkScp->fid, &fids[i]) ) {
2034                                     code = CM_ERROR_TOO_MANY_SYMLINKS;
2035                                     cm_ReleaseSCache(linkScp);
2036                                     nscp = NULL;
2037                                     break;
2038                                 }
2039                             }
2040                         }
2041                         if (i == fid_count && fid_count < MAX_FID_COUNT) {
2042                             fids[fid_count++] = linkScp->fid;
2043                         }
2044                     }
2045
2046                     if (code) {
2047                         /* something went wrong */
2048                         cm_ReleaseSCache(tscp);
2049                         tscp = NULL;
2050                         if (dirScp) {
2051                             cm_ReleaseSCache(dirScp);
2052                             dirScp = NULL;
2053                         }
2054                         break;
2055                     }
2056
2057                     /* otherwise, tempsp has the new path,
2058                      * and linkScp is the new root from
2059                      * which to interpret that path.
2060                      * Continue with the namei processing,
2061                      * also doing the bookkeeping for the
2062                      * space allocation and tracking the
2063                      * vnode reference counts.
2064                      */
2065                     if (psp) 
2066                         cm_FreeSpace(psp);
2067                     psp = tempsp;
2068                     tp = psp->data;
2069                     cm_ReleaseSCache(tscp);
2070                     tscp = linkScp;
2071                     linkScp = NULL;
2072                     /* already held
2073                      * by AssembleLink
2074                      * now, if linkScp is null, that's
2075                      * AssembleLink's way of telling us that
2076                      * the sym link is relative to the dir
2077                      * containing the link.  We have a ref
2078                      * to it in dirScp, and we hold it now
2079                      * and reuse it as the new spot in the
2080                      * dir hierarchy.
2081                      */
2082                     if (tscp == NULL) {
2083                         tscp = dirScp;
2084                         dirScp = NULL;
2085                     }
2086                 } else {
2087                     /* not a symlink, we may be done */
2088                     lock_ReleaseMutex(&tscp->mx);
2089                     if (tc == 0) {
2090                         if (phase == 1) {
2091                             phase = 2;
2092                             tp = pathp;
2093                             continue;
2094                         }
2095                         if (dirScp) {
2096                             cm_ReleaseSCache(dirScp);
2097                             dirScp = NULL;
2098                         }
2099                         code = 0;
2100                         break;
2101                     }
2102                 }
2103                 if (dirScp) {
2104                     cm_ReleaseSCache(dirScp);
2105                     dirScp = NULL;
2106                 }
2107             } /* end of a component */
2108             else 
2109                 *cp++ = tc;
2110         } /* we have a component */
2111     } /* big while loop over all components */
2112
2113     /* already held */
2114     if (dirScp)
2115         cm_ReleaseSCache(dirScp);
2116     if (psp) 
2117         cm_FreeSpace(psp);
2118     if (code == 0) 
2119         *outScpp = tscp;
2120     else if (tscp)
2121         cm_ReleaseSCache(tscp);
2122
2123 #ifdef DEBUG_REFCOUNT
2124     afsi_log("%s:%d cm_NameI code 0x%x outScpp 0x%p ref %d", file, line, code, *outScpp, (*outScpp)->refCount);
2125 #endif
2126     osi_Log2(afsd_logp,"cm_NameI code 0x%x outScpp 0x%p", code, *outScpp);
2127     return code;
2128 }
2129
2130 /* called with a dir, and a vnode within the dir that happens to be a symlink.
2131  * We chase the link, and return a held pointer to the target, if it exists,
2132  * in *outScpp.  If we succeed, we return 0, otherwise we return an error code
2133  * and do not hold or return a target vnode.
2134  *
2135  * This is very similar to calling cm_NameI with the last component of a name,
2136  * which happens to be a symlink, except that we've already passed by the name.
2137  *
2138  * This function is typically called by the directory listing functions, which
2139  * encounter symlinks but need to return the proper file length so programs
2140  * like "more" work properly when they make use of the attributes retrieved from
2141  * the dir listing.
2142  *
2143  * The input vnode should not be locked when this function is called.
2144  */
2145 long cm_EvaluateSymLink(cm_scache_t *dscp, cm_scache_t *linkScp,
2146                          cm_scache_t **outScpp, cm_user_t *userp, cm_req_t *reqp)
2147 {
2148     long code;
2149     cm_space_t *spacep;
2150     cm_scache_t *newRootScp;
2151
2152     osi_Log1(afsd_logp, "Evaluating symlink scp 0x%p", linkScp);
2153
2154     code = cm_AssembleLink(linkScp, "", &newRootScp, &spacep, userp, reqp);
2155     if (code) 
2156         return code;
2157
2158     /* now, if newRootScp is NULL, we're really being told that the symlink
2159      * is relative to the current directory (dscp).
2160      */
2161     if (newRootScp == NULL) {
2162         newRootScp = dscp;
2163         cm_HoldSCache(dscp);
2164     }
2165
2166     code = cm_NameI(newRootScp, spacep->data,
2167                      CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW | CM_FLAG_DIRSEARCH,
2168                      userp, NULL, reqp, outScpp);
2169
2170     if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH)
2171         code = CM_ERROR_NOSUCHPATH;
2172
2173     /* this stuff is allocated no matter what happened on the namei call,
2174      * so free it */
2175     cm_FreeSpace(spacep);
2176     cm_ReleaseSCache(newRootScp);
2177
2178     if (linkScp == *outScpp) {
2179         cm_ReleaseSCache(*outScpp);
2180         *outScpp = NULL;
2181         code = CM_ERROR_NOSUCHPATH;
2182     }
2183
2184     return code;
2185 }
2186
2187 /* make this big enough so that one buffer of dir pages won't overflow.  We'll
2188  * check anyway, but we want to minimize the chance that we have to leave stuff
2189  * unstat'd.
2190  */
2191 #define CM_BULKMAX              (3 * AFSCBMAX)
2192
2193 /* rock for bulk stat calls */
2194 typedef struct cm_bulkStat {
2195     osi_hyper_t bufOffset;      /* only do it for things in this buffer page */
2196
2197     /* info for the actual call */
2198     int counter;                        /* next free slot */
2199     AFSFid fids[CM_BULKMAX];
2200     AFSFetchStatus stats[CM_BULKMAX];
2201     AFSCallBack callbacks[CM_BULKMAX];
2202 } cm_bulkStat_t;
2203
2204 /* for a given entry, make sure that it isn't in the stat cache, and then
2205  * add it to the list of file IDs to be obtained.
2206  *
2207  * Don't bother adding it if we already have a vnode.  Note that the dir
2208  * is locked, so we have to be careful checking the vnode we're thinking of
2209  * processing, to avoid deadlocks.
2210  */
2211 long cm_TryBulkProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
2212                      osi_hyper_t *offp)
2213 {
2214     osi_hyper_t thyper;
2215     cm_bulkStat_t *bsp;
2216     int i;
2217     cm_scache_t *tscp;
2218     cm_fid_t tfid;
2219
2220     bsp = rockp;
2221
2222     /* Don't overflow bsp. */
2223     if (bsp->counter >= CM_BULKMAX)
2224         return CM_ERROR_STOPNOW;
2225
2226     thyper.LowPart = cm_data.buf_blockSize;
2227     thyper.HighPart = 0;
2228     thyper = LargeIntegerAdd(thyper, bsp->bufOffset);
2229
2230     /* thyper is now the first byte past the end of the record we're
2231      * interested in, and bsp->bufOffset is the first byte of the record
2232      * we're interested in.
2233      * Skip data in the others.
2234      * Skip '.' and '..'
2235      */
2236     if (LargeIntegerLessThan(*offp, bsp->bufOffset))
2237         return 0;
2238     if (LargeIntegerGreaterThanOrEqualTo(*offp, thyper))
2239         return CM_ERROR_STOPNOW;
2240     if (strcmp(dep->name, ".") == 0 || strcmp(dep->name, "..") == 0)
2241         return 0;
2242
2243     tfid.cell = scp->fid.cell;
2244     tfid.volume = scp->fid.volume;
2245     tfid.vnode = ntohl(dep->fid.vnode);
2246     tfid.unique = ntohl(dep->fid.unique);
2247     tscp = cm_FindSCache(&tfid);
2248     if (tscp) {
2249         if (lock_TryMutex(&tscp->mx)) {
2250             /* we have an entry that we can look at */
2251             if (!(tscp->flags & CM_SCACHEFLAG_EACCESS) && cm_HaveCallback(tscp)) {
2252                 /* we have a callback on it.  Don't bother
2253                  * fetching this stat entry, since we're happy
2254                  * with the info we have.
2255                  */
2256                 lock_ReleaseMutex(&tscp->mx);
2257                 cm_ReleaseSCache(tscp);
2258                 return 0;
2259             }
2260             lock_ReleaseMutex(&tscp->mx);
2261         }       /* got lock */
2262         cm_ReleaseSCache(tscp);
2263     }   /* found entry */
2264
2265 #ifdef AFS_FREELANCE_CLIENT
2266     // yj: if this is a mountpoint under root.afs then we don't want it
2267     // to be bulkstat-ed, instead, we call getSCache directly and under
2268     // getSCache, it is handled specially.
2269     if  ( cm_freelanceEnabled &&
2270           tfid.cell==AFS_FAKE_ROOT_CELL_ID && 
2271           tfid.volume==AFS_FAKE_ROOT_VOL_ID &&
2272           !(tfid.vnode==0x1 && tfid.unique==0x1) )
2273     {       
2274         osi_Log0(afsd_logp, "cm_TryBulkProc Freelance calls cm_SCache on root.afs mountpoint");
2275         return cm_GetSCache(&tfid, &tscp, NULL, NULL);
2276     }
2277 #endif /* AFS_FREELANCE_CLIENT */
2278
2279     i = bsp->counter++;
2280     bsp->fids[i].Volume = scp->fid.volume;
2281     bsp->fids[i].Vnode = tfid.vnode;
2282     bsp->fids[i].Unique = tfid.unique;
2283     return 0;
2284 }       
2285
2286 /* called with a locked scp and a pointer to a buffer.  Make bulk stat
2287  * calls on all undeleted files in the page of the directory specified.
2288  */
2289 afs_int32
2290 cm_TryBulkStat(cm_scache_t *dscp, osi_hyper_t *offsetp, cm_user_t *userp,
2291                cm_req_t *reqp)
2292 {
2293     long code;
2294     cm_bulkStat_t bb;   /* this is *BIG*, probably 16K or so;
2295                          * watch for stack problems */
2296     AFSCBFids fidStruct;
2297     AFSBulkStats statStruct;
2298     cm_conn_t *connp;
2299     AFSCBs callbackStruct;
2300     long filex;
2301     AFSVolSync volSync;
2302     cm_callbackRequest_t cbReq;
2303     long filesThisCall;
2304     long i;
2305     long j;
2306     cm_scache_t *scp;
2307     cm_fid_t tfid;
2308     struct rx_connection * callp;
2309     int inlinebulk = 0;         /* Did we use InlineBulkStatus RPC or not? */
2310
2311     osi_Log1(afsd_logp, "cm_TryBulkStat dir 0x%p", dscp);
2312
2313     /* should be on a buffer boundary */
2314     osi_assertx((offsetp->LowPart & (cm_data.buf_blockSize - 1)) == 0, "invalid offset");
2315
2316     memset(&bb, 0, sizeof(bb));
2317     bb.bufOffset = *offsetp;
2318
2319     lock_ReleaseMutex(&dscp->mx);
2320     /* first, assemble the file IDs we need to stat */
2321     code = cm_ApplyDir(dscp, cm_TryBulkProc, (void *) &bb, offsetp, userp, reqp, NULL);
2322
2323     /* if we failed, bail out early */
2324     if (code && code != CM_ERROR_STOPNOW) {
2325         lock_ObtainMutex(&dscp->mx);
2326         return code;
2327     }
2328
2329     /* otherwise, we may have one or more bulk stat's worth of stuff in bb;
2330      * make the calls to create the entries.  Handle AFSCBMAX files at a
2331      * time.
2332      */
2333     filex = 0;
2334     while (filex < bb.counter) {
2335         filesThisCall = bb.counter - filex;
2336         if (filesThisCall > AFSCBMAX) 
2337             filesThisCall = AFSCBMAX;
2338
2339         fidStruct.AFSCBFids_len = filesThisCall;
2340         fidStruct.AFSCBFids_val = &bb.fids[filex];
2341         statStruct.AFSBulkStats_len = filesThisCall;
2342         statStruct.AFSBulkStats_val = &bb.stats[filex];
2343         callbackStruct.AFSCBs_len = filesThisCall;
2344         callbackStruct.AFSCBs_val = &bb.callbacks[filex];
2345         cm_StartCallbackGrantingCall(NULL, &cbReq);
2346         osi_Log1(afsd_logp, "CALL BulkStatus, %d entries", filesThisCall);
2347         do {
2348             code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2349             if (code) 
2350                 continue;
2351
2352             callp = cm_GetRxConn(connp);
2353             if (!(connp->serverp->flags & CM_SERVERFLAG_NOINLINEBULK)) {
2354                 code = RXAFS_InlineBulkStatus(callp, &fidStruct,
2355                                      &statStruct, &callbackStruct, &volSync);
2356                 if (code == RXGEN_OPCODE) {
2357                     cm_SetServerNoInlineBulk(connp->serverp, 0);
2358                 } else {
2359                     inlinebulk = 1;
2360                 }
2361             }
2362             if (!inlinebulk) {
2363                 code = RXAFS_BulkStatus(callp, &fidStruct,
2364                                         &statStruct, &callbackStruct, &volSync);
2365             }
2366             rx_PutConnection(callp);
2367
2368         } while (cm_Analyze(connp, userp, reqp, &dscp->fid,
2369                              &volSync, NULL, &cbReq, code));
2370         code = cm_MapRPCError(code, reqp);
2371         if (code)
2372             osi_Log2(afsd_logp, "CALL %sBulkStatus FAILURE code 0x%x", 
2373                       inlinebulk ? "Inline" : "", code);
2374         else
2375             osi_Log1(afsd_logp, "CALL %sBulkStatus SUCCESS", inlinebulk ? "Inline" : "");
2376
2377         /* may as well quit on an error, since we're not going to do
2378          * much better on the next immediate call, either.
2379          */
2380         if (code) {
2381             cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2382             break;
2383         }
2384
2385         /* otherwise, we should do the merges */
2386         for (i = 0; i<filesThisCall; i++) {
2387             j = filex + i;
2388             tfid.cell = dscp->fid.cell;
2389             tfid.volume = bb.fids[j].Volume;
2390             tfid.vnode = bb.fids[j].Vnode;
2391             tfid.unique = bb.fids[j].Unique;
2392             code = cm_GetSCache(&tfid, &scp, userp, reqp);
2393             if (code != 0) 
2394                 continue;
2395
2396             /* otherwise, if this entry has no callback info, 
2397              * merge in this.
2398              */
2399             lock_ObtainMutex(&scp->mx);
2400             /* now, we have to be extra paranoid on merging in this
2401              * information, since we didn't use cm_SyncOp before
2402              * starting the fetch to make sure that no bad races
2403              * were occurring.  Specifically, we need to make sure
2404              * we don't obliterate any newer information in the
2405              * vnode than have here.
2406              *
2407              * Right now, be pretty conservative: if there's a
2408              * callback or a pending call, skip it.
2409              */
2410             if ((scp->cbServerp == NULL || (scp->flags & CM_SCACHEFLAG_EACCESS))
2411                  && !(scp->flags &
2412                        (CM_SCACHEFLAG_FETCHING
2413                          | CM_SCACHEFLAG_STORING
2414                          | CM_SCACHEFLAG_SIZESTORING))) {
2415                 cm_EndCallbackGrantingCall(scp, &cbReq,
2416                                             &bb.callbacks[j],
2417                                             CM_CALLBACK_MAINTAINCOUNT);
2418                 cm_MergeStatus(dscp, scp, &bb.stats[j], &volSync, userp, 0);
2419             }       
2420             lock_ReleaseMutex(&scp->mx);
2421             cm_ReleaseSCache(scp);
2422         } /* all files in the response */
2423         /* now tell it to drop the count,
2424          * after doing the vnode processing above */
2425         cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2426
2427         filex += filesThisCall;
2428     }   /* while there are still more files to process */
2429     lock_ObtainMutex(&dscp->mx);
2430
2431     /* If we did the InlineBulk RPC pull out the return code and log it */
2432     if (inlinebulk) {
2433         if ((&bb.stats[0])->errorCode) {
2434             osi_Log1(afsd_logp, "cm_TryBulkStat bulk stat error: %d", 
2435                      (&bb.stats[0])->errorCode);
2436         }
2437     }
2438
2439     osi_Log0(afsd_logp, "END cm_TryBulkStat");
2440     return 0;
2441 }       
2442
2443 void cm_StatusFromAttr(AFSStoreStatus *statusp, cm_scache_t *scp, cm_attr_t *attrp)
2444 {
2445     long mask;
2446
2447     /* initialize store back mask as inexpensive local variable */
2448     mask = 0;
2449     memset(statusp, 0, sizeof(AFSStoreStatus));
2450
2451     /* copy out queued info from scache first, if scp passed in */
2452     if (scp) {
2453         if (scp->mask & CM_SCACHEMASK_CLIENTMODTIME) {
2454             statusp->ClientModTime = scp->clientModTime;
2455             mask |= AFS_SETMODTIME;
2456             scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
2457         }
2458     }
2459
2460     if (attrp) {
2461         /* now add in our locally generated request */
2462         if (attrp->mask & CM_ATTRMASK_CLIENTMODTIME) {
2463             statusp->ClientModTime = attrp->clientModTime;
2464             mask |= AFS_SETMODTIME;
2465         }
2466         if (attrp->mask & CM_ATTRMASK_UNIXMODEBITS) {
2467             statusp->UnixModeBits = attrp->unixModeBits;
2468             mask |= AFS_SETMODE;
2469         }
2470         if (attrp->mask & CM_ATTRMASK_OWNER) {
2471             statusp->Owner = attrp->owner;
2472             mask |= AFS_SETOWNER;
2473         }
2474         if (attrp->mask & CM_ATTRMASK_GROUP) {
2475             statusp->Group = attrp->group;
2476             mask |= AFS_SETGROUP;
2477         }
2478     }
2479     statusp->Mask = mask;
2480 }       
2481
2482 /* set the file size, and make sure that all relevant buffers have been
2483  * truncated.  Ensure that any partially truncated buffers have been zeroed
2484  * to the end of the buffer.
2485  */
2486 long cm_SetLength(cm_scache_t *scp, osi_hyper_t *sizep, cm_user_t *userp,
2487                    cm_req_t *reqp)
2488 {
2489     long code;
2490     int shrinking;
2491
2492     /* start by locking out buffer creation */
2493     lock_ObtainWrite(&scp->bufCreateLock);
2494
2495     /* verify that this is a file, not a dir or a symlink */
2496     lock_ObtainMutex(&scp->mx);
2497     code = cm_SyncOp(scp, NULL, userp, reqp, 0,
2498                       CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2499     if (code) 
2500         goto done;
2501     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2502
2503     if (scp->fileType != CM_SCACHETYPE_FILE) {
2504         code = CM_ERROR_ISDIR;
2505         goto done;
2506     }
2507
2508   startover:
2509     if (LargeIntegerLessThan(*sizep, scp->length))
2510         shrinking = 1;
2511     else
2512         shrinking = 0;
2513
2514     lock_ReleaseMutex(&scp->mx);
2515
2516     /* can't hold scp->mx lock here, since we may wait for a storeback to
2517      * finish if the buffer package is cleaning a buffer by storing it to
2518      * the server.
2519      */
2520     if (shrinking)
2521         buf_Truncate(scp, userp, reqp, sizep);
2522
2523     /* now ensure that file length is short enough, and update truncPos */
2524     lock_ObtainMutex(&scp->mx);
2525
2526     /* make sure we have a callback (so we have the right value for the
2527      * length), and wait for it to be safe to do a truncate.
2528      */
2529     code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_WRITE,
2530                       CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2531                       | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2532
2533     /* If we only have 'i' bits, then we should still be able to set
2534        the size of a file we created. */
2535     if (code == CM_ERROR_NOACCESS && scp->creator == userp) {
2536         code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
2537                          CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2538                          | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2539     }
2540
2541     if (code) 
2542         goto done;
2543
2544     if (LargeIntegerLessThan(*sizep, scp->length)) {
2545         /* a real truncation.  If truncPos is not set yet, or is bigger
2546          * than where we're truncating the file, set truncPos to this
2547          * new value.
2548          */
2549         if (!shrinking)
2550             goto startover;
2551         if (!(scp->mask & CM_SCACHEMASK_TRUNCPOS)
2552              || LargeIntegerLessThan(*sizep, scp->length)) {
2553             /* set trunc pos */
2554             scp->truncPos = *sizep;
2555             scp->mask |= CM_SCACHEMASK_TRUNCPOS;
2556         }
2557         /* in either case, the new file size has been changed */
2558         scp->length = *sizep;
2559         scp->mask |= CM_SCACHEMASK_LENGTH;
2560     }
2561     else if (LargeIntegerGreaterThan(*sizep, scp->length)) {
2562         /* really extending the file */
2563         scp->length = *sizep;
2564         scp->mask |= CM_SCACHEMASK_LENGTH;
2565     }
2566
2567     /* done successfully */
2568     code = 0;
2569
2570     cm_SyncOpDone(scp, NULL, 
2571                    CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2572                    | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2573
2574   done:
2575     lock_ReleaseMutex(&scp->mx);
2576     lock_ReleaseWrite(&scp->bufCreateLock);
2577
2578     return code;
2579 }
2580
2581 /* set the file size or other attributes (but not both at once) */
2582 long cm_SetAttr(cm_scache_t *scp, cm_attr_t *attrp, cm_user_t *userp,
2583                 cm_req_t *reqp)
2584 {
2585     long code;
2586     AFSFetchStatus afsOutStatus;
2587     AFSVolSync volSync;
2588     cm_conn_t *connp;
2589     AFSFid tfid;
2590     AFSStoreStatus afsInStatus;
2591     struct rx_connection * callp;
2592
2593     /* handle file length setting */
2594     if (attrp->mask & CM_ATTRMASK_LENGTH)
2595         return cm_SetLength(scp, &attrp->length, userp, reqp);
2596
2597     lock_ObtainMutex(&scp->mx);
2598     /* otherwise, we have to make an RPC to get the status */
2599     code = cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STORESTATUS);
2600     if (code) {
2601         lock_ReleaseMutex(&scp->mx);
2602         return code;
2603     }
2604
2605     /* make the attr structure */
2606     cm_StatusFromAttr(&afsInStatus, scp, attrp);
2607
2608     tfid.Volume = scp->fid.volume;
2609     tfid.Vnode = scp->fid.vnode;
2610     tfid.Unique = scp->fid.unique;
2611         lock_ReleaseMutex(&scp->mx);
2612
2613     /* now make the RPC */
2614     osi_Log1(afsd_logp, "CALL StoreStatus scp 0x%p", scp);
2615     do {
2616         code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2617         if (code) 
2618             continue;
2619
2620         callp = cm_GetRxConn(connp);
2621         code = RXAFS_StoreStatus(callp, &tfid,
2622                                   &afsInStatus, &afsOutStatus, &volSync);
2623         rx_PutConnection(callp);
2624
2625     } while (cm_Analyze(connp, userp, reqp,
2626                          &scp->fid, &volSync, NULL, NULL, code));
2627     code = cm_MapRPCError(code, reqp);
2628
2629     if (code)
2630         osi_Log1(afsd_logp, "CALL StoreStatus FAILURE, code 0x%x", code);
2631     else
2632         osi_Log0(afsd_logp, "CALL StoreStatus SUCCESS");
2633
2634     lock_ObtainMutex(&scp->mx);
2635     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STORESTATUS);
2636     if (code == 0)
2637         cm_MergeStatus(NULL, scp, &afsOutStatus, &volSync, userp,
2638                         CM_MERGEFLAG_FORCE|CM_MERGEFLAG_STOREDATA);
2639         
2640     /* if we're changing the mode bits, discard the ACL cache, 
2641      * since we changed the mode bits.
2642      */
2643     if (afsInStatus.Mask & AFS_SETMODE) 
2644         cm_FreeAllACLEnts(scp);
2645     lock_ReleaseMutex(&scp->mx);
2646     return code;
2647 }       
2648
2649 long cm_Create(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2650                cm_scache_t **scpp, cm_user_t *userp, cm_req_t *reqp)
2651 {       
2652     cm_conn_t *connp;
2653     long code;
2654     AFSFid dirAFSFid;
2655     cm_callbackRequest_t cbReq;
2656     AFSFid newAFSFid;
2657     cm_fid_t newFid;
2658     cm_scache_t *scp = NULL;
2659     int didEnd;
2660     AFSStoreStatus inStatus;
2661     AFSFetchStatus updatedDirStatus;
2662     AFSFetchStatus newFileStatus;
2663     AFSCallBack newFileCallback;
2664     AFSVolSync volSync;
2665     struct rx_connection * callp;
2666     cm_dirOp_t dirop;
2667
2668     /* can't create names with @sys in them; must expand it manually first.
2669      * return "invalid request" if they try.
2670      */
2671     if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2672         return CM_ERROR_ATSYS;
2673     }
2674
2675     /* before starting the RPC, mark that we're changing the file data, so
2676      * that someone who does a chmod will know to wait until our call
2677      * completes.
2678      */
2679     cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2680     lock_ObtainMutex(&dscp->mx);
2681     code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2682     lock_ReleaseMutex(&dscp->mx);
2683     if (code == 0) {
2684         cm_StartCallbackGrantingCall(NULL, &cbReq);
2685     } else {
2686         cm_EndDirOp(&dirop);
2687     }
2688     if (code) {
2689         return code;
2690     }
2691     didEnd = 0;
2692
2693     cm_StatusFromAttr(&inStatus, NULL, attrp);
2694
2695     /* try the RPC now */
2696     osi_Log1(afsd_logp, "CALL CreateFile scp 0x%p", dscp);
2697     do {
2698         code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2699         if (code) 
2700             continue;
2701
2702         dirAFSFid.Volume = dscp->fid.volume;
2703         dirAFSFid.Vnode = dscp->fid.vnode;
2704         dirAFSFid.Unique = dscp->fid.unique;
2705
2706         callp = cm_GetRxConn(connp);
2707         code = RXAFS_CreateFile(connp->callp, &dirAFSFid, namep,
2708                                  &inStatus, &newAFSFid, &newFileStatus,
2709                                  &updatedDirStatus, &newFileCallback,
2710                                  &volSync);
2711         rx_PutConnection(callp);
2712
2713     } while (cm_Analyze(connp, userp, reqp,
2714                          &dscp->fid, &volSync, NULL, &cbReq, code));
2715     code = cm_MapRPCError(code, reqp);
2716         
2717     if (code)
2718         osi_Log1(afsd_logp, "CALL CreateFile FAILURE, code 0x%x", code);
2719     else
2720         osi_Log0(afsd_logp, "CALL CreateFile SUCCESS");
2721
2722     if (dirop.scp) {
2723         lock_ObtainWrite(&dirop.scp->dirlock);
2724         dirop.lockType = CM_DIRLOCK_WRITE;
2725     }
2726     lock_ObtainMutex(&dscp->mx);
2727     cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2728     if (code == 0) {
2729         cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2730     }
2731     lock_ReleaseMutex(&dscp->mx);
2732
2733     /* now try to create the file's entry, too, but be careful to 
2734      * make sure that we don't merge in old info.  Since we weren't locking
2735      * out any requests during the file's creation, we may have pretty old
2736      * info.
2737      */
2738     if (code == 0) {
2739         newFid.cell = dscp->fid.cell;
2740         newFid.volume = dscp->fid.volume;
2741         newFid.vnode = newAFSFid.Vnode;
2742         newFid.unique = newAFSFid.Unique;
2743         code = cm_GetSCache(&newFid, &scp, userp, reqp);
2744         if (code == 0) {
2745             lock_ObtainMutex(&scp->mx);
2746             scp->creator = userp;               /* remember who created it */
2747             if (!cm_HaveCallback(scp)) {
2748                 cm_MergeStatus(dscp, scp, &newFileStatus, &volSync,
2749                                 userp, 0);
2750                 cm_EndCallbackGrantingCall(scp, &cbReq,
2751                                             &newFileCallback, 0);
2752                 didEnd = 1;     
2753             }       
2754             lock_ReleaseMutex(&scp->mx);
2755             *scpp = scp;
2756         }
2757     }
2758
2759     /* make sure we end things properly */
2760     if (!didEnd)
2761         cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2762
2763     if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2764         cm_DirCreateEntry(&dirop, namep, &newFid);
2765 #ifdef USE_BPLUS
2766         cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2767 #endif
2768     }
2769     cm_EndDirOp(&dirop);
2770
2771     return code;
2772 }       
2773
2774 long cm_FSync(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
2775 {
2776     long code;
2777
2778     lock_ObtainWrite(&scp->bufCreateLock);
2779     code = buf_CleanVnode(scp, userp, reqp);
2780     lock_ReleaseWrite(&scp->bufCreateLock);
2781     if (code == 0) {
2782         lock_ObtainMutex(&scp->mx);
2783
2784         if (scp->mask & (CM_SCACHEMASK_TRUNCPOS
2785                           | CM_SCACHEMASK_CLIENTMODTIME
2786                           | CM_SCACHEMASK_LENGTH))
2787             code = cm_StoreMini(scp, userp, reqp);
2788
2789         if (scp->flags & (CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE)) {
2790             code = (scp->flags & CM_SCACHEFLAG_OVERQUOTA) ? CM_ERROR_QUOTA : CM_ERROR_SPACE;
2791             scp->flags &= ~(CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE);
2792         }
2793
2794         lock_ReleaseMutex(&scp->mx);
2795     }
2796     return code;
2797 }
2798
2799 long cm_MakeDir(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2800                  cm_user_t *userp, cm_req_t *reqp)
2801 {
2802     cm_conn_t *connp;
2803     long code;
2804     AFSFid dirAFSFid;
2805     cm_callbackRequest_t cbReq;
2806     AFSFid newAFSFid;
2807     cm_fid_t newFid;
2808     cm_scache_t *scp = NULL;
2809     int didEnd;
2810     AFSStoreStatus inStatus;
2811     AFSFetchStatus updatedDirStatus;
2812     AFSFetchStatus newDirStatus;
2813     AFSCallBack newDirCallback;
2814     AFSVolSync volSync;
2815     struct rx_connection * callp;
2816     cm_dirOp_t dirop;
2817
2818     /* can't create names with @sys in them; must expand it manually first.
2819      * return "invalid request" if they try.
2820      */
2821     if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2822         return CM_ERROR_ATSYS;
2823     }
2824
2825     /* before starting the RPC, mark that we're changing the directory
2826      * data, so that someone who does a chmod on the dir will wait until
2827      * our call completes.
2828      */
2829     cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2830     lock_ObtainMutex(&dscp->mx);
2831     code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2832     lock_ReleaseMutex(&dscp->mx);
2833     if (code == 0) {
2834         cm_StartCallbackGrantingCall(NULL, &cbReq);
2835     } else {
2836         cm_EndDirOp(&dirop);
2837     }
2838     if (code) {
2839         return code;
2840     }
2841     didEnd = 0;
2842
2843     cm_StatusFromAttr(&inStatus, NULL, attrp);
2844
2845     /* try the RPC now */
2846     osi_Log1(afsd_logp, "CALL MakeDir scp 0x%p", dscp);
2847     do {
2848         code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2849         if (code) 
2850             continue;
2851
2852         dirAFSFid.Volume = dscp->fid.volume;
2853         dirAFSFid.Vnode = dscp->fid.vnode;
2854         dirAFSFid.Unique = dscp->fid.unique;
2855
2856         callp = cm_GetRxConn(connp);
2857         code = RXAFS_MakeDir(connp->callp, &dirAFSFid, namep,
2858                               &inStatus, &newAFSFid, &newDirStatus,
2859                               &updatedDirStatus, &newDirCallback,
2860                               &volSync);
2861         rx_PutConnection(callp);
2862
2863     } while (cm_Analyze(connp, userp, reqp,
2864                          &dscp->fid, &volSync, NULL, &cbReq, code));
2865     code = cm_MapRPCError(code, reqp);
2866         
2867     if (code)
2868         osi_Log1(afsd_logp, "CALL MakeDir FAILURE, code 0x%x", code);
2869     else
2870         osi_Log0(afsd_logp, "CALL MakeDir SUCCESS");
2871
2872     if (dirop.scp) {
2873         lock_ObtainWrite(&dirop.scp->dirlock);
2874         dirop.lockType = CM_DIRLOCK_WRITE;
2875     }
2876     lock_ObtainMutex(&dscp->mx);
2877     cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2878     if (code == 0) {
2879         cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2880     }
2881     lock_ReleaseMutex(&dscp->mx);
2882
2883     /* now try to create the new dir's entry, too, but be careful to 
2884      * make sure that we don't merge in old info.  Since we weren't locking
2885      * out any requests during the file's creation, we may have pretty old
2886      * info.
2887      */
2888     if (code == 0) {
2889         newFid.cell = dscp->fid.cell;
2890         newFid.volume = dscp->fid.volume;
2891         newFid.vnode = newAFSFid.Vnode;
2892         newFid.unique = newAFSFid.Unique;
2893         code = cm_GetSCache(&newFid, &scp, userp, reqp);
2894         if (code == 0) {
2895             lock_ObtainMutex(&scp->mx);
2896             if (!cm_HaveCallback(scp)) {
2897                 cm_MergeStatus(dscp, scp, &newDirStatus, &volSync,
2898                                 userp, 0);
2899                 cm_EndCallbackGrantingCall(scp, &cbReq,
2900                                             &newDirCallback, 0);
2901                 didEnd = 1;             
2902             }
2903             lock_ReleaseMutex(&scp->mx);
2904             cm_ReleaseSCache(scp);
2905         }
2906     }
2907
2908     /* make sure we end things properly */
2909     if (!didEnd)
2910         cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2911
2912     if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2913         cm_DirCreateEntry(&dirop, namep, &newFid);
2914 #ifdef USE_BPLUS
2915         cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2916 #endif
2917     }
2918     cm_EndDirOp(&dirop);
2919
2920     /* and return error code */
2921     return code;
2922 }       
2923
2924 long cm_Link(cm_scache_t *dscp, char *namep, cm_scache_t *sscp, long flags,
2925              cm_user_t *userp, cm_req_t *reqp)
2926 {
2927     cm_conn_t *connp;
2928     long code = 0;
2929     AFSFid dirAFSFid;
2930     AFSFid existingAFSFid;
2931     AFSFetchStatus updatedDirStatus;
2932     AFSFetchStatus newLinkStatus;
2933     AFSVolSync volSync;
2934     struct rx_connection * callp;
2935     cm_dirOp_t dirop;
2936
2937     if (dscp->fid.cell != sscp->fid.cell ||
2938         dscp->fid.volume != sscp->fid.volume) {
2939         return CM_ERROR_CROSSDEVLINK;
2940     }
2941
2942     cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2943     lock_ObtainMutex(&dscp->mx);
2944     code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2945     lock_ReleaseMutex(&dscp->mx);
2946     if (code != 0)
2947         cm_EndDirOp(&dirop);
2948
2949     if (code)
2950         return code;
2951
2952     /* try the RPC now */
2953     osi_Log1(afsd_logp, "CALL Link scp 0x%p", dscp);
2954     do {
2955         code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2956         if (code) continue;
2957
2958         dirAFSFid.Volume = dscp->fid.volume;
2959         dirAFSFid.Vnode = dscp->fid.vnode;
2960         dirAFSFid.Unique = dscp->fid.unique;
2961
2962         existingAFSFid.Volume = sscp->fid.volume;
2963         existingAFSFid.Vnode = sscp->fid.vnode;
2964         existingAFSFid.Unique = sscp->fid.unique;
2965
2966         callp = cm_GetRxConn(connp);
2967         code = RXAFS_Link(callp, &dirAFSFid, namep, &existingAFSFid,
2968             &newLinkStatus, &updatedDirStatus, &volSync);
2969         rx_PutConnection(callp);
2970         osi_Log1(smb_logp,"  RXAFS_Link returns 0x%x", code);
2971
2972     } while (cm_Analyze(connp, userp, reqp,
2973         &dscp->fid, &volSync, NULL, NULL, code));
2974
2975     code = cm_MapRPCError(code, reqp);
2976
2977     if (code)
2978         osi_Log1(afsd_logp, "CALL Link FAILURE, code 0x%x", code);
2979     else
2980         osi_Log0(afsd_logp, "CALL Link SUCCESS");
2981
2982     if (dirop.scp) {
2983         lock_ObtainWrite(&dirop.scp->dirlock);
2984         dirop.lockType = CM_DIRLOCK_WRITE;
2985     }
2986     lock_ObtainMutex(&dscp->mx);
2987     cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2988     if (code == 0) {
2989         cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2990     }
2991     lock_ReleaseMutex(&dscp->mx);
2992
2993     if (code == 0) {
2994         if (cm_CheckDirOpForSingleChange(&dirop)) {
2995             cm_DirCreateEntry(&dirop, namep, &sscp->fid);
2996 #ifdef USE_BPLUS
2997             cm_BPlusDirCreateEntry(&dirop, namep, &sscp->fid);
2998 #endif
2999         }
3000     }
3001     cm_EndDirOp(&dirop);
3002
3003     return code;
3004 }
3005
3006 long cm_SymLink(cm_scache_t *dscp, char *namep, char *contentsp, long flags,
3007                 cm_attr_t *attrp, cm_user_t *userp, cm_req_t *reqp)
3008 {
3009     cm_conn_t *connp;
3010     long code;
3011     AFSFid dirAFSFid;
3012     AFSFid newAFSFid;
3013     cm_fid_t newFid;
3014     cm_scache_t *scp;
3015     AFSStoreStatus inStatus;
3016     AFSFetchStatus updatedDirStatus;
3017     AFSFetchStatus newLinkStatus;
3018     AFSVolSync volSync;
3019     struct rx_connection * callp;
3020     cm_dirOp_t dirop;
3021
3022     /* before starting the RPC, mark that we're changing the directory data,
3023      * so that someone who does a chmod on the dir will wait until our
3024      * call completes.
3025      */
3026     cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3027     lock_ObtainMutex(&dscp->mx);
3028     code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3029     lock_ReleaseMutex(&dscp->mx);
3030     if (code != 0)
3031         cm_EndDirOp(&dirop);
3032     if (code) {
3033         return code;
3034     }
3035
3036     cm_StatusFromAttr(&inStatus, NULL, attrp);
3037
3038     /* try the RPC now */
3039     osi_Log1(afsd_logp, "CALL Symlink scp 0x%p", dscp);
3040     do {
3041         code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3042         if (code) 
3043             continue;
3044
3045         dirAFSFid.Volume = dscp->fid.volume;
3046         dirAFSFid.Vnode = dscp->fid.vnode;
3047         dirAFSFid.Unique = dscp->fid.unique;
3048
3049         callp = cm_GetRxConn(connp);
3050         code = RXAFS_Symlink(callp, &dirAFSFid, namep, contentsp,
3051                               &inStatus, &newAFSFid, &newLinkStatus,
3052                               &updatedDirStatus, &volSync);
3053         rx_PutConnection(callp);
3054
3055     } while (cm_Analyze(connp, userp, reqp,
3056                          &dscp->fid, &volSync, NULL, NULL, code));
3057     code = cm_MapRPCError(code, reqp);
3058         
3059     if (code)
3060         osi_Log1(afsd_logp, "CALL Symlink FAILURE, code 0x%x", code);
3061     else
3062         osi_Log0(afsd_logp, "CALL Symlink SUCCESS");
3063
3064     if (dirop.scp) {
3065         lock_ObtainWrite(&dirop.scp->dirlock);
3066         dirop.lockType = CM_DIRLOCK_WRITE;
3067     }
3068     lock_ObtainMutex(&dscp->mx);
3069     cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3070     if (code == 0) {
3071         cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3072     }
3073     lock_ReleaseMutex(&dscp->mx);
3074
3075     if (code == 0) {
3076         if (cm_CheckDirOpForSingleChange(&dirop)) {
3077             newFid.cell = dscp->fid.cell;
3078             newFid.volume = dscp->fid.volume;
3079             newFid.vnode = newAFSFid.Vnode;
3080             newFid.unique = newAFSFid.Unique;
3081
3082             cm_DirCreateEntry(&dirop, namep, &newFid);
3083 #ifdef USE_BPLUS
3084             cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
3085 #endif
3086         }
3087     }
3088     cm_EndDirOp(&dirop);
3089
3090     /* now try to create the new dir's entry, too, but be careful to 
3091      * make sure that we don't merge in old info.  Since we weren't locking
3092      * out any requests during the file's creation, we may have pretty old
3093      * info.
3094      */
3095     if (code == 0) {
3096         newFid.cell = dscp->fid.cell;
3097         newFid.volume = dscp->fid.volume;
3098         newFid.vnode = newAFSFid.Vnode;
3099         newFid.unique = newAFSFid.Unique;
3100         code = cm_GetSCache(&newFid, &scp, userp, reqp);
3101         if (code == 0) {
3102             lock_ObtainMutex(&scp->mx);
3103             if (!cm_HaveCallback(scp)) {
3104                 cm_MergeStatus(dscp, scp, &newLinkStatus, &volSync,
3105                                 userp, 0);
3106             }       
3107             lock_ReleaseMutex(&scp->mx);
3108             cm_ReleaseSCache(scp);
3109         }
3110     }
3111         
3112     /* and return error code */
3113     return code;
3114 }
3115
3116 long cm_RemoveDir(cm_scache_t *dscp, char *namep, cm_user_t *userp,
3117                    cm_req_t *reqp)
3118 {
3119     cm_conn_t *connp;
3120     long code;
3121     AFSFid dirAFSFid;
3122     int didEnd;
3123     AFSFetchStatus updatedDirStatus;
3124     AFSVolSync volSync;
3125     struct rx_connection * callp;
3126     cm_dirOp_t dirop;
3127
3128     /* before starting the RPC, mark that we're changing the directory data,
3129      * so that someone who does a chmod on the dir will wait until our
3130      * call completes.
3131      */
3132     cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3133     lock_ObtainMutex(&dscp->mx);
3134     code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3135     lock_ReleaseMutex(&dscp->mx);
3136     if (code) {
3137         cm_EndDirOp(&dirop);
3138         return code;
3139     }
3140     didEnd = 0;
3141
3142     /* try the RPC now */
3143     osi_Log1(afsd_logp, "CALL RemoveDir scp 0x%p", dscp);
3144     do {
3145         code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3146         if (code) 
3147             continue;
3148
3149         dirAFSFid.Volume = dscp->fid.volume;
3150         dirAFSFid.Vnode = dscp->fid.vnode;
3151         dirAFSFid.Unique = dscp->fid.unique;
3152
3153         callp = cm_GetRxConn(connp);
3154         code = RXAFS_RemoveDir(callp, &dirAFSFid, namep,
3155                                 &updatedDirStatus, &volSync);
3156         rx_PutConnection(callp);
3157
3158     } while (cm_Analyze(connp, userp, reqp,
3159                          &dscp->fid, &volSync, NULL, NULL, code));
3160     code = cm_MapRPCErrorRmdir(code, reqp);
3161
3162     if (code)
3163         osi_Log1(afsd_logp, "CALL RemoveDir FAILURE, code 0x%x", code);
3164     else
3165         osi_Log0(afsd_logp, "CALL RemoveDir SUCCESS");
3166
3167     if (dirop.scp) {
3168         lock_ObtainWrite(&dirop.scp->dirlock);
3169         dirop.lockType = CM_DIRLOCK_WRITE;
3170     }
3171     lock_ObtainMutex(&dscp->mx);
3172     cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3173     if (code == 0) {
3174         cm_dnlcRemove(dscp, namep); 
3175         cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3176     }
3177     lock_ReleaseMutex(&dscp->mx);
3178
3179     if (code == 0) {
3180         if (cm_CheckDirOpForSingleChange(&dirop)) {
3181             cm_DirDeleteEntry(&dirop, namep);
3182 #ifdef USE_BPLUS
3183             cm_BPlusDirDeleteEntry(&dirop, namep);
3184 #endif
3185         }
3186     }
3187     cm_EndDirOp(&dirop);
3188
3189     /* and return error code */
3190     return code;
3191 }
3192
3193 long cm_Open(cm_scache_t *scp, int type, cm_user_t *userp)
3194 {
3195     /* grab mutex on contents */
3196     lock_ObtainMutex(&scp->mx);
3197
3198     /* reset the prefetch info */
3199     scp->prefetch.base.LowPart = 0;             /* base */
3200     scp->prefetch.base.HighPart = 0;
3201     scp->prefetch.end.LowPart = 0;              /* and end */
3202     scp->prefetch.end.HighPart = 0;
3203
3204     /* release mutex on contents */
3205     lock_ReleaseMutex(&scp->mx);
3206
3207     /* we're done */
3208     return 0;
3209 }       
3210
3211 long cm_Rename(cm_scache_t *oldDscp, char *oldNamep, cm_scache_t *newDscp,
3212                 char *newNamep, cm_user_t *userp, cm_req_t *reqp)
3213 {
3214     cm_conn_t *connp;
3215     long code;
3216     AFSFid oldDirAFSFid;
3217     AFSFid newDirAFSFid;
3218     int didEnd;
3219     AFSFetchStatus updatedOldDirStatus;
3220     AFSFetchStatus updatedNewDirStatus;
3221     AFSVolSync volSync;
3222     int oneDir;
3223     struct rx_connection * callp;
3224     cm_dirOp_t oldDirOp;
3225     cm_fid_t   fileFid;
3226     int        diropCode = -1;
3227     cm_dirOp_t newDirOp;
3228
3229     /* before starting the RPC, mark that we're changing the directory data,
3230      * so that someone who does a chmod on the dir will wait until our call
3231      * completes.  We do this in vnode order so that we don't deadlock,
3232      * which makes the code a little verbose.
3233      */
3234     if (oldDscp == newDscp) {
3235         /* check for identical names */
3236         if (strcmp(oldNamep, newNamep) == 0)
3237             return CM_ERROR_RENAME_IDENTICAL;
3238
3239         oneDir = 1;
3240         cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3241         lock_ObtainMutex(&oldDscp->mx);
3242         cm_dnlcRemove(oldDscp, oldNamep);
3243         cm_dnlcRemove(oldDscp, newNamep);
3244         code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3245                           CM_SCACHESYNC_STOREDATA);
3246         lock_ReleaseMutex(&oldDscp->mx);
3247         if (code != 0) {
3248             cm_EndDirOp(&oldDirOp);
3249         }
3250     }
3251     else {
3252         /* two distinct dir vnodes */
3253         oneDir = 0;
3254         if (oldDscp->fid.cell != newDscp->fid.cell ||
3255              oldDscp->fid.volume != newDscp->fid.volume)
3256             return CM_ERROR_CROSSDEVLINK;
3257
3258         /* shouldn't happen that we have distinct vnodes for two
3259          * different files, but could due to deliberate attack, or
3260          * stale info.  Avoid deadlocks and quit now.
3261          */
3262         if (oldDscp->fid.vnode == newDscp->fid.vnode)
3263             return CM_ERROR_CROSSDEVLINK;
3264
3265         if (oldDscp->fid.vnode < newDscp->fid.vnode) {
3266             cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3267             lock_ObtainMutex(&oldDscp->mx);
3268             cm_dnlcRemove(oldDscp, oldNamep);
3269             code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3270                               CM_SCACHESYNC_STOREDATA);
3271             lock_ReleaseMutex(&oldDscp->mx);
3272             if (code != 0)
3273                 cm_EndDirOp(&oldDirOp);
3274             if (code == 0) {
3275                 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3276                 lock_ObtainMutex(&newDscp->mx);
3277                 cm_dnlcRemove(newDscp, newNamep);
3278                 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3279                                   CM_SCACHESYNC_STOREDATA);
3280                 lock_ReleaseMutex(&newDscp->mx);
3281                 if (code) {
3282                     cm_EndDirOp(&newDirOp);
3283
3284                     /* cleanup first one */
3285                     lock_ObtainMutex(&oldDscp->mx);
3286                     cm_SyncOpDone(oldDscp, NULL,
3287                                    CM_SCACHESYNC_STOREDATA);
3288                     lock_ReleaseMutex(&oldDscp->mx);
3289                     cm_EndDirOp(&oldDirOp);
3290                 }       
3291             }
3292         }
3293         else {
3294             /* lock the new vnode entry first */
3295             cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3296             lock_ObtainMutex(&newDscp->mx);
3297             cm_dnlcRemove(newDscp, newNamep);
3298             code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3299                               CM_SCACHESYNC_STOREDATA);
3300             lock_ReleaseMutex(&newDscp->mx);
3301             if (code != 0)
3302                 cm_EndDirOp(&newDirOp);
3303             if (code == 0) {
3304                 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3305                 lock_ObtainMutex(&oldDscp->mx);
3306                 cm_dnlcRemove(oldDscp, oldNamep);
3307                 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3308                                   CM_SCACHESYNC_STOREDATA);
3309                 lock_ReleaseMutex(&oldDscp->mx);
3310                 if (code != 0)
3311                     cm_EndDirOp(&oldDirOp);
3312                 if (code) {
3313                     /* cleanup first one */
3314                     lock_ObtainMutex(&newDscp->mx);
3315                     cm_SyncOpDone(newDscp, NULL,
3316                                    CM_SCACHESYNC_STOREDATA);
3317                     lock_ReleaseMutex(&newDscp->mx);
3318                     cm_EndDirOp(&newDirOp);
3319                 }       
3320             }
3321         }
3322     }   /* two distinct vnodes */
3323
3324     if (code) {
3325         return code;
3326     }
3327     didEnd = 0;
3328
3329     /* try the RPC now */
3330     osi_Log2(afsd_logp, "CALL Rename old scp 0x%p new scp 0x%p", 
3331               oldDscp, newDscp);
3332     do {
3333         code = cm_ConnFromFID(&oldDscp->fid, userp, reqp, &connp);
3334         if (code) 
3335             continue;
3336
3337         oldDirAFSFid.Volume = oldDscp->fid.volume;
3338         oldDirAFSFid.Vnode = oldDscp->fid.vnode;
3339         oldDirAFSFid.Unique = oldDscp->fid.unique;
3340         newDirAFSFid.Volume = newDscp->fid.volume;
3341         newDirAFSFid.Vnode = newDscp->fid.vnode;
3342         newDirAFSFid.Unique = newDscp->fid.unique;
3343
3344         callp = cm_GetRxConn(connp);
3345         code = RXAFS_Rename(callp, &oldDirAFSFid, oldNamep,
3346                              &newDirAFSFid, newNamep,
3347                              &updatedOldDirStatus, &updatedNewDirStatus,
3348                              &volSync);
3349         rx_PutConnection(callp);
3350
3351     } while (cm_Analyze(connp, userp, reqp, &oldDscp->fid,
3352                          &volSync, NULL, NULL, code));
3353     code = cm_MapRPCError(code, reqp);
3354         
3355     if (code)
3356         osi_Log1(afsd_logp, "CALL Rename FAILURE, code 0x%x", code);
3357     else
3358         osi_Log0(afsd_logp, "CALL Rename SUCCESS");
3359
3360     /* update the individual stat cache entries for the directories */
3361     if (oldDirOp.scp) {
3362         lock_ObtainWrite(&oldDirOp.scp->dirlock);
3363         oldDirOp.lockType = CM_DIRLOCK_WRITE;
3364     }
3365     lock_ObtainMutex(&oldDscp->mx);
3366     cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_STOREDATA);
3367
3368     if (code == 0)
3369         cm_MergeStatus(NULL, oldDscp, &updatedOldDirStatus, &volSync,
3370                         userp, CM_MERGEFLAG_DIROP);
3371     lock_ReleaseMutex(&oldDscp->mx);
3372
3373     if (code == 0) {
3374         if (cm_CheckDirOpForSingleChange(&oldDirOp)) {
3375
3376 #ifdef USE_BPLUS
3377             diropCode = cm_BPlusDirLookup(&oldDirOp, oldNamep, &fileFid);
3378             if (diropCode == CM_ERROR_INEXACT_MATCH)
3379                 diropCode = 0;
3380             else if (diropCode == EINVAL)
3381 #endif
3382                 diropCode = cm_DirLookup(&oldDirOp, oldNamep, &fileFid);
3383
3384             if (diropCode == 0) {
3385                 if (oneDir) {
3386                     diropCode = cm_DirCreateEntry(&oldDirOp, newNamep, &fileFid);
3387 #ifdef USE_BPLUS
3388                     cm_BPlusDirCreateEntry(&oldDirOp, newNamep, &fileFid);
3389 #endif
3390                 }
3391
3392                 if (diropCode == 0) { 
3393                     diropCode = cm_DirDeleteEntry(&oldDirOp, oldNamep);
3394 #ifdef USE_BPLUS
3395                     cm_BPlusDirDeleteEntry(&oldDirOp, oldNamep);
3396 #endif
3397                 }
3398             }
3399         }
3400     }
3401     cm_EndDirOp(&oldDirOp);
3402
3403     /* and update it for the new one, too, if necessary */
3404     if (!oneDir) {
3405         if (newDirOp.scp) {
3406             lock_ObtainWrite(&newDirOp.scp->dirlock);
3407             newDirOp.lockType = CM_DIRLOCK_WRITE;
3408         }
3409         lock_ObtainMutex(&newDscp->mx);
3410         cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_STOREDATA);
3411         if (code == 0)
3412             cm_MergeStatus(NULL, newDscp, &updatedNewDirStatus, &volSync,
3413                             userp, CM_MERGEFLAG_DIROP);
3414         lock_ReleaseMutex(&newDscp->mx);
3415
3416         if (code == 0) {
3417             /* we only make the local change if we successfully made
3418                the change in the old directory AND there was only one
3419                change in the new directory */
3420             if (diropCode == 0 && cm_CheckDirOpForSingleChange(&newDirOp)) {
3421                 cm_DirCreateEntry(&newDirOp, newNamep, &fileFid);
3422 #ifdef USE_BPLUS
3423                 cm_BPlusDirCreateEntry(&newDirOp, newNamep, &fileFid);
3424 #endif
3425             }
3426         }
3427         cm_EndDirOp(&newDirOp);
3428     }
3429
3430     /* and return error code */
3431     return code;
3432 }
3433
3434 /* Byte range locks:
3435
3436    The OpenAFS Windows client has to fake byte range locks given no
3437    server side support for such locks.  This is implemented as keyed
3438    byte range locks on the cache manager.
3439
3440    Keyed byte range locks:
3441
3442    Each cm_scache_t structure keeps track of a list of keyed locks.
3443    The key for a lock identifies an owner of a set of locks (referred
3444    to as a client).  Each key is represented by a value.  The set of
3445    key values used within a specific cm_scache_t structure form a
3446    namespace that has a scope of just that cm_scache_t structure.  The
3447    same key value can be used with another cm_scache_t structure and
3448    correspond to a completely different client.  However it is
3449    advantageous for the SMB or IFS layer to make sure that there is a
3450    1-1 mapping between client and keys over all cm_scache_t objects.
3451
3452    Assume a client C has key Key(C) (although, since the scope of the
3453    key is a cm_scache_t, the key can be Key(C,S), where S is the
3454    cm_scache_t.  But assume a 1-1 relation between keys and clients).
3455    A byte range (O,+L) denotes byte addresses (O) through (O+L-1)
3456    inclusive (a.k.a. [O,O+L-1]).  The function Key(x) is implemented
3457    through cm_generateKey() function for both SMB and IFS.
3458
3459    The list of locks for a cm_scache_t object S is maintained in
3460    S->fileLocks.  The cache manager will set a lock on the AFS file
3461    server in order to assert the locks in S->fileLocks.  If only
3462    shared locks are in place for S, then the cache manager will obtain
3463    a LockRead lock, while if there are any exclusive locks, it will
3464    obtain a LockWrite lock.  If the exclusive locks are all released
3465    while the shared locks remain, then the cache manager will
3466    downgrade the lock from LockWrite to LockRead.  Similarly, if an
3467    exclusive lock is obtained when only shared locks exist, then the
3468    cache manager will try to upgrade the lock from LockRead to
3469    LockWrite.
3470
3471    Each lock L owned by client C maintains a key L->key such that
3472    L->key == Key(C), the effective range defined by L->LOffset and
3473    L->LLength such that the range of bytes affected by the lock is
3474    (L->LOffset, +L->LLength), a type maintained in L->LockType which
3475    is either exclusive or shared.
3476
3477    Lock states:
3478
3479    A lock exists iff it is in S->fileLocks for some cm_scache_t
3480    S. Existing locks are in one of the following states: ACTIVE,
3481    WAITLOCK, WAITUNLOCK, LOST, DELETED.
3482
3483    The following sections describe each lock and the associated
3484    transitions.
3485
3486    1. ACTIVE: A lock L is ACTIVE iff the cache manager has asserted
3487       the lock with the AFS file server.  This type of lock can be
3488       exercised by a client to read or write to the locked region (as
3489       the lock allows).
3490
3491       1.1 ACTIVE->LOST: When the AFS file server fails to extend a
3492         server lock that was required to assert the lock.  Before
3493         marking the lock as lost, the cache manager checks if the file
3494         has changed on the server.  If the file has not changed, then
3495         the cache manager will attempt to obtain a new server lock
3496         that is sufficient to assert the client side locks for the
3497         file.  If any of these fail, the lock is marked as LOST.
3498         Otherwise, it is left as ACTIVE.
3499
3500       1.2 ACTIVE->DELETED: Lock is released.
3501
3502    2. WAITLOCK: A lock is in a WAITLOCK state if the cache manager
3503       grants the lock but the lock is yet to be asserted with the AFS
3504       file server.  Once the file server grants the lock, the state
3505       will transition to an ACTIVE lock.
3506
3507       2.1 WAITLOCK->ACTIVE: The server granted the lock.
3508
3509       2.2 WAITLOCK->DELETED: Lock is abandoned, or timed out during
3510         waiting.
3511
3512       2.3 WAITLOCK->LOST: One or more locks from this client were
3513         marked as LOST.  No further locks will be granted to this
3514         client until all lost locks are removed.
3515
3516    3. WAITUNLOCK: A lock is in a WAITUNLOCK state if the cache manager
3517       receives a request for a lock that conflicts with an existing
3518       ACTIVE or WAITLOCK lock.  The lock will be placed in the queue
3519       and will be granted at such time the conflicting locks are
3520       removed, at which point the state will transition to either
3521       WAITLOCK or ACTIVE.
3522
3523       3.1 WAITUNLOCK->ACTIVE: The conflicting lock was removed.  The
3524         current serverLock is sufficient to assert this lock, or a
3525         sufficient serverLock is obtained.
3526
3527       3.2 WAITUNLOCK->WAITLOCK: The conflicting lock was removed,
3528         however the required serverLock is yet to be asserted with the
3529         server.
3530
3531       3.3 WAITUNLOCK->DELETED: The lock is abandoned, timed out or
3532         released.
3533
3534       3.5 WAITUNLOCK->LOST: One or more locks from this client were
3535         marked as LOST.  No further locks will be granted to this
3536         client until all lost locks are removed.
3537
3538    4. LOST: A lock L is LOST if the server lock that was required to
3539       assert the lock could not be obtained or if it could not be
3540       extended, or if other locks by the same client were LOST.
3541       Essentially, once a lock is LOST, the contract between the cache
3542       manager and that specific client is no longer valid.
3543
3544       The cache manager rechecks the server lock once every minute and
3545       extends it as appropriate.  If this is not done for 5 minutes,
3546       the AFS file server will release the lock (the 5 minute timeout
3547       is based on current file server code and is fairly arbitrary).
3548       Once released, the lock cannot be re-obtained without verifying
3549       that the contents of the file hasn't been modified since the
3550       time the lock was released.  Re-obtaining the lock without
3551       verifying this may lead to data corruption.  If the lock can not
3552       be obtained safely, then all active locks for the cm_scache_t
3553       are marked as LOST.
3554
3555       4.1 LOST->DELETED: The lock is released.
3556
3557    5. DELETED: The lock is no longer relevant.  Eventually, it will
3558       get removed from the cm_scache_t. In the meantime, it will be
3559       treated as if it does not exist.
3560
3561       5.1 DELETED->not exist: The lock is removed from the
3562         cm_scache_t.
3563
3564    The following are classifications of locks based on their state.
3565
3566    6* A lock L is ACCEPTED if it is ACTIVE or WAITLOCK.  These locks
3567       have been accepted by the cache manager, but may or may not have
3568       been granted back to the client.
3569
3570    7* A lock L is QUEUED if it is ACTIVE, WAITLOCK or WAITUNLOCK.
3571
3572    8* A lock L is WAITING if it is WAITLOCK or WAITUNLOCK.
3573
3574    Lock operation:
3575
3576    A client C can READ range (Offset,+Length) of a file represented by
3577    cm_scache_t S iff (1):
3578
3579    1. for all _a_ in (Offset,+Length), all of the following is true:
3580
3581        1.1 For each ACTIVE lock L in S->fileLocks such that _a_ in
3582          (L->LOffset,+L->LLength); L->key == Key(C) OR L->LockType is
3583          shared.
3584
3585        1.2 For each LOST lock L in S->fileLocks such that _a_ in
3586          (L->LOffset,+L->LLength); L->LockType is shared AND L->key !=
3587          Key(C)
3588
3589        (When locks are lost on an cm_scache_t, all locks are lost.  By
3590        4.2 (below), if there is an exclusive LOST lock, then there
3591        can't be any overlapping ACTIVE locks.)
3592
3593    A client C can WRITE range (Offset,+Length) of cm_scache_t S iff (2):
3594
3595    2. for all _a_ in (Offset,+Length), one of the following is true:
3596
3597        2.1 Byte _a_ of S is unowned (as specified in 1.1) AND there
3598          does not exist a LOST lock L such that _a_ in
3599          (L->LOffset,+L->LLength).
3600
3601        2.2 Byte _a_ of S is owned by C under lock L (as specified in
3602          1.2) AND L->LockType is exclusive.
3603
3604    A client C can OBTAIN a lock L on cm_scache_t S iff (both 3 and 4):
3605
3606    3. for all _a_ in (L->LOffset,+L->LLength), ALL of the following is
3607       true:
3608
3609        3.1 If L->LockType is exclusive then there does NOT exist a
3610          ACCEPTED lock M in S->fileLocks such that _a_ in
3611          (M->LOffset,+M->LLength).
3612
3613          (If we count all QUEUED locks then we hit cases such as
3614          cascading waiting locks where the locks later on in the queue
3615          can be granted without compromising file integrity.  On the
3616          other hand if only ACCEPTED locks are considered, then locks
3617          that were received earlier may end up waiting for locks that
3618          were received later to be unlocked. The choice of ACCEPTED
3619          locks was made to mimic the Windows byte range lock
3620          semantics.)
3621
3622        3.2 If L->LockType is shared then for each ACCEPTED lock M in
3623          S->fileLocks, if _a_ in (M->LOffset,+M->LLength) then
3624          M->LockType is shared.
3625
3626    4. For all LOST locks M in S->fileLocks, ALL of the following are true:
3627
3628        4.1 M->key != Key(C)
3629
3630        4.2 If M->LockType is exclusive, then (L->LOffset,+L->LLength)
3631          and (M->LOffset,+M->LLength) do not intersect.
3632
3633          (Note: If a client loses a lock, it loses all locks.
3634          Subsequently, it will not be allowed to obtain any more locks
3635          until all existing LOST locks that belong to the client are
3636          released.  Once all locks are released by a single client,
3637          there exists no further contract between the client and AFS
3638          about the contents of the file, hence the client can then
3639          proceed to obtain new locks and establish a new contract.
3640
3641          This doesn't quite work as you think it should, because most
3642          applications aren't built to deal with losing locks they
3643          thought they once had.  For now, we don't have a good
3644          solution to lost locks.
3645
3646          Also, for consistency reasons, we have to hold off on
3647          granting locks that overlap exclusive LOST locks.)
3648
3649    A client C can only unlock locks L in S->fileLocks which have
3650    L->key == Key(C).
3651
3652    The representation and invariants are as follows:
3653
3654    - Each cm_scache_t structure keeps:
3655
3656        - A queue of byte-range locks (cm_scache_t::fileLocks) which
3657          are of type cm_file_lock_t.
3658
3659        - A record of the highest server-side lock that has been
3660          obtained for this object (cm_scache_t::serverLock), which is
3661          one of (-1), LockRead, LockWrite.
3662
3663        - A count of ACCEPTED exclusive and shared locks that are in the
3664          queue (cm_scache_t::sharedLocks and
3665          cm_scache_t::exclusiveLocks)
3666
3667    - Each cm_file_lock_t structure keeps:
3668
3669        - The type of lock (cm_file_lock_t::LockType)
3670
3671        - The key associated with the lock (cm_file_lock_t::key)
3672
3673        - The offset and length of the lock (cm_file_lock_t::LOffset
3674          and cm_file_lock_t::LLength)
3675
3676        - The state of the lock.
3677
3678        - Time of issuance or last successful extension
3679
3680    Semantic invariants:
3681
3682        I1. The number of ACCEPTED locks in S->fileLocks are
3683            (S->sharedLocks + S->exclusiveLocks)
3684
3685    External invariants:
3686
3687        I3. S->serverLock is the lock that we have asserted with the
3688            AFS file server for this cm_scache_t.
3689
3690        I4. S->serverLock == LockRead iff there is at least one ACTIVE
3691            shared lock, but no ACTIVE exclusive locks.
3692
3693        I5. S->serverLock == LockWrite iff there is at least one ACTIVE
3694            exclusive lock.
3695
3696        I6. If L is a LOST lock, then for each lock M in S->fileLocks,
3697            M->key == L->key IMPLIES M is LOST or DELETED.
3698
3699    --asanka
3700  */
3701
3702 #define IS_LOCK_ACTIVE(lockp)     (((lockp)->flags & (CM_FILELOCK_FLAG_DELETED|CM_FILELOCK_FLAG_WAITLOCK|CM_FILELOCK_FLAG_WAITUNLOCK|CM_FILELOCK_FLAG_LOST)) == 0)
3703
3704 #define IS_LOCK_WAITLOCK(lockp)   (((lockp)->flags & (CM_FILELOCK_FLAG_DELETED|CM_FILELOCK_FLAG_WAITLOCK|CM_FILELOCK_FLAG_WAITUNLOCK|CM_FILELOCK_FLAG_LOST)) == CM_FILELOCK_FLAG_WAITLOCK)
3705
3706 #define IS_LOCK_WAITUNLOCK(lockp) (((lockp)->flags & (CM_FILELOCK_FLAG_DELETED|CM_FILELOCK_FLAG_WAITLOCK|CM_FILELOCK_FLAG_WAITUNLOCK|CM_FILELOCK_FLAG_LOST)) == CM_FILELOCK_FLAG_WAITUNLOCK)
3707
3708 #define IS_LOCK_LOST(lockp)       (((lockp)->flags & (CM_FILELOCK_FLAG_DELETED|CM_FILELOCK_FLAG_LOST)) == CM_FILELOCK_FLAG_LOST)
3709
3710 #define IS_LOCK_DELETED(lockp)    (((lockp)->flags & CM_FILELOCK_FLAG_DELETED) == CM_FILELOCK_FLAG_DELETED)
3711
3712 /* unsafe */
3713 #define IS_LOCK_ACCEPTED(lockp)   (IS_LOCK_ACTIVE(lockp) || IS_LOCK_WAITLOCK(lockp))
3714
3715 /* unsafe */
3716 #define IS_LOCK_CLIENTONLY(lockp) ((((lockp)->scp->flags & CM_SCACHEFLAG_RO) == CM_SCACHEFLAG_RO) || (((lockp)->flags & CM_FILELOCK_FLAG_CLIENTONLY) == CM_FILELOCK_FLAG_CLIENTONLY))
3717
3718 /* unsafe */
3719 #define INTERSECT_RANGE(r1,r2) (((r2).offset+(r2).length) > (r1).offset && ((r1).offset +(r1).length) > (r2).offset)
3720
3721 /* unsafe */
3722 #define CONTAINS_RANGE(r1,r2) (((r2).offset+(r2).length) <= ((r1).offset+(r1).length) && (r1).offset <= (r2).offset)
3723
3724 #if defined(VICED_CAPABILITY_USE_BYTE_RANGE_LOCKS) && !defined(LOCK_TESTING)
3725 #define SCP_SUPPORTS_BRLOCKS(scp) ((scp)->cbServerp && ((scp)->cbServerp->capabilities & VICED_CAPABILITY_USE_BYTE_RANGE_LOCKS))
3726 #else
3727 #define SCP_SUPPORTS_BRLOCKS(scp) (1)
3728 #endif
3729
3730 #define SERVERLOCKS_ENABLED(scp) (!((scp)->flags & CM_SCACHEFLAG_RO) && cm_enableServerLocks && SCP_SUPPORTS_BRLOCKS(scp))
3731
3732 #if defined(VICED_CAPABILITY_WRITELOCKACL)
3733 #define SCP_SUPPORTS_WRITELOCKACL(scp) ((scp)->cbServerp && ((scp->cbServerp->capabilities & VICED_CAPABILITY_WRITELOCKACL)))
3734 #else
3735 #define SCP_SUPPORTS_WRITELOCKACL(scp) (0)
3736
3737 /* This should really be defined in any build that this code is being
3738    compiled. */
3739 #error  VICED_CAPABILITY_WRITELOCKACL not defined.
3740 #endif
3741
3742 static void cm_LockRangeSubtract(cm_range_t * pos, const cm_range_t * neg)
3743 {
3744     afs_int64 int_begin;
3745     afs_int64 int_end;
3746
3747     int_begin = MAX(pos->offset, neg->offset);
3748     int_end = MIN(pos->offset+pos->length, neg->offset+neg->length);
3749
3750     if (int_begin < int_end) {
3751         if (int_begin == pos->offset) {
3752             pos->length = pos->offset + pos->length - int_end;
3753             pos->offset = int_end;
3754         } else if (int_end == pos->offset + pos->length) {
3755             pos->length = int_begin - pos->offset;
3756         }
3757
3758         /* We only subtract ranges if the resulting range is
3759            contiguous.  If we try to support non-contigous ranges, we
3760            aren't actually improving performance. */
3761     }
3762 }
3763
3764 /* Called with scp->mx held.  Returns 0 if all is clear to read the
3765    specified range by the client identified by key.
3766  */
3767 long cm_LockCheckRead(cm_scache_t *scp, 
3768                       LARGE_INTEGER LOffset, 
3769                       LARGE_INTEGER LLength, 
3770                       cm_key_t key)
3771 {
3772 #ifndef ADVISORY_LOCKS
3773
3774     cm_file_lock_t *fileLock;
3775     osi_queue_t *q;
3776     long code = 0;
3777     cm_range_t range;
3778     int substract_ranges = FALSE;
3779
3780     range.offset = LOffset.QuadPart;
3781     range.length = LLength.QuadPart;
3782
3783     /*
3784
3785      1. for all _a_ in (Offset,+Length), all of the following is true:
3786
3787        1.1 For each ACTIVE lock L in S->fileLocks such that _a_ in
3788          (L->LOffset,+L->LLength); L->key == Key(C) OR L->LockType is
3789          shared.
3790
3791        1.2 For each LOST lock L in S->fileLocks such that _a_ in
3792          (L->LOffset,+L->LLength); L->LockType is shared AND L->key !=
3793          Key(C)
3794
3795     */
3796
3797     lock_ObtainRead(&cm_scacheLock);
3798
3799     for (q = scp->fileLocksH; q && range.length > 0; q = osi_QNext(q)) {
3800         fileLock = 
3801             (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
3802
3803         if (INTERSECT_RANGE(range, fileLock->range)) {
3804             if (IS_LOCK_ACTIVE(fileLock)) {
3805                 if (fileLock->key == key) {
3806
3807                     /* If there is an active lock for this client, it
3808                        is safe to substract ranges.*/
3809                     cm_LockRangeSubtract(&range, &fileLock->range);
3810                     substract_ranges = TRUE;
3811                 } else {
3812                     if (fileLock->lockType != LockRead) {
3813                         code = CM_ERROR_LOCK_CONFLICT;
3814                         break;
3815                     }
3816
3817                     /* even if the entire range is locked for reading,
3818                        we still can't grant the lock at this point
3819                        because the client may have lost locks. That
3820                        is, unless we have already seen an active lock
3821                        belonging to the client, in which case there
3822                        can't be any lost locks for this client. */
3823                     if (substract_ranges)
3824                         cm_LockRangeSubtract(&range, &fileLock->range);
3825                 }
3826             } else if (IS_LOCK_LOST(fileLock) &&
3827                       (fileLock->key == key || fileLock->lockType == LockWrite)) {
3828                 code = CM_ERROR_BADFD;
3829                 break;
3830             }
3831         }
3832     }
3833
3834     lock_ReleaseRead(&cm_scacheLock);
3835
3836     osi_Log4(afsd_logp, "cm_LockCheckRead scp 0x%x offset %d length %d code 0x%x",
3837               scp, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart, code);
3838
3839     return code;
3840
3841 #else
3842
3843     return 0;
3844
3845 #endif
3846 }
3847
3848 /* Called with scp->mx held.  Returns 0 if all is clear to write the
3849    specified range by the client identified by key.
3850  */
3851 long cm_LockCheckWrite(cm_scache_t *scp,
3852                        LARGE_INTEGER LOffset,
3853                        LARGE_INTEGER LLength,
3854                        cm_key_t key)
3855 {
3856 #ifndef ADVISORY_LOCKS
3857
3858     cm_file_lock_t *fileLock;
3859     osi_queue_t *q;
3860     long code = 0;
3861     cm_range_t range;
3862
3863     range.offset = LOffset.QuadPart;
3864     range.length = LLength.QuadPart;
3865
3866     /*
3867    A client C can WRITE range (Offset,+Length) of cm_scache_t S iff (2):
3868
3869    2. for all _a_ in (Offset,+Length), one of the following is true:
3870
3871        2.1 Byte _a_ of S is unowned AND there does not exist a LOST
3872          lock L such that _a_ in (L->LOffset,+L->LLength).
3873
3874        2.2 Byte _a_ of S is owned by C under lock L AND L->LockType is
3875          exclusive.
3876     */
3877
3878     lock_ObtainRead(&cm_scacheLock);
3879
3880     for (q = scp->fileLocksH; q && range.length > 0; q = osi_QNext(q)) {
3881         fileLock = 
3882             (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
3883
3884         if (INTERSECT_RANGE(range, fileLock->range)) {
3885             if (IS_LOCK_ACTIVE(fileLock)) {
3886                 if (fileLock->key == key) {
3887                     if (fileLock->lockType == LockWrite) {
3888
3889                         /* if there is an active lock for this client, it
3890                            is safe to substract ranges */
3891                         cm_LockRangeSubtract(&range, &fileLock->range);
3892                     } else {
3893                         code = CM_ERROR_LOCK_CONFLICT;
3894                         break;
3895                     }
3896                 } else {
3897                     code = CM_ERROR_LOCK_CONFLICT;
3898                     break;
3899                 }
3900             } else if (IS_LOCK_LOST(fileLock)) {
3901                 code = CM_ERROR_BADFD;
3902                 break;
3903             }
3904         }
3905     }
3906
3907     lock_ReleaseRead(&cm_scacheLock);
3908
3909     osi_Log4(afsd_logp, "cm_LockCheckWrite scp 0x%x offset %d length %d code 0x%x",
3910               scp, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart, code);
3911
3912     return code;
3913
3914 #else
3915
3916     return 0;
3917
3918 #endif
3919 }
3920
3921 /* Forward dcl. */
3922 static void cm_LockMarkSCacheLost(cm_scache_t * scp);
3923
3924 /* Called with cm_scacheLock write locked */
3925 static cm_file_lock_t * cm_GetFileLock(void) {
3926     cm_file_lock_t * l;
3927
3928     l = (cm_file_lock_t *) cm_freeFileLocks;
3929     if (l) {
3930         osi_QRemove(&cm_freeFileLocks, &l->q);
3931     } else {
3932         l = malloc(sizeof(cm_file_lock_t));
3933         osi_assertx(l, "null cm_file_lock_t");
3934     }
3935
3936     memset(l, 0, sizeof(cm_file_lock_t));
3937
3938     return l;
3939 }
3940
3941 /* Called with cm_scacheLock write locked */
3942 static void cm_PutFileLock(cm_file_lock_t *l) {
3943     osi_QAdd(&cm_freeFileLocks, &l->q);
3944 }
3945
3946 /* called with scp->mx held.  May release it during processing, but
3947    leaves it held on exit. */
3948 long cm_IntSetLock(cm_scache_t * scp, cm_user_t * userp, int lockType,
3949                    cm_req_t * reqp) {
3950     long code = 0;
3951     AFSFid tfid;
3952     cm_fid_t cfid;
3953     cm_conn_t * connp;
3954     struct rx_connection * callp;
3955     AFSVolSync volSync;
3956
3957     tfid.Volume = scp->fid.volume;
3958     tfid.Vnode = scp->fid.vnode;
3959     tfid.Unique = scp->fid.unique;
3960     cfid = scp->fid;
3961
3962     osi_Log2(afsd_logp, "CALL SetLock scp 0x%p for lock %d", scp, lockType);
3963
3964     lock_ReleaseMutex(&scp->mx);
3965
3966     do {
3967         code = cm_ConnFromFID(&cfid, userp, reqp, &connp);
3968         if (code) 
3969             break;
3970
3971         callp = cm_GetRxConn(connp);
3972         code = RXAFS_SetLock(callp, &tfid, lockType,
3973                              &volSync);
3974         rx_PutConnection(callp);
3975
3976     } while (cm_Analyze(connp, userp, reqp, &cfid, &volSync,
3977                         NULL, NULL, code));
3978
3979     code = cm_MapRPCError(code, reqp);
3980     if (code) {
3981         osi_Log1(afsd_logp, "CALL SetLock FAILURE, code 0x%x", code);
3982     } else {
3983         osi_Log0(afsd_logp, "CALL SetLock SUCCESS");
3984     }
3985
3986     lock_ObtainMutex(&scp->mx);
3987
3988     return code;
3989 }
3990
3991 /* called with scp->mx held.  Releases it during processing */
3992 long cm_IntReleaseLock(cm_scache_t * scp, cm_user_t * userp,
3993                        cm_req_t * reqp) {
3994     long code = 0;
3995     AFSFid tfid;
3996     cm_fid_t cfid;
3997     cm_conn_t * connp;
3998     struct rx_connection * callp;
3999     AFSVolSync volSync;
4000
4001     tfid.Volume = scp->fid.volume;
4002     tfid.Vnode = scp->fid.vnode;
4003     tfid.Unique = scp->fid.unique;
4004     cfid = scp->fid;
4005
4006     lock_ReleaseMutex(&scp->mx);
4007
4008     osi_Log1(afsd_logp, "CALL ReleaseLock scp 0x%p", scp);
4009
4010     do {
4011         code = cm_ConnFromFID(&cfid, userp, reqp, &connp);
4012         if (code) 
4013             break;
4014
4015         callp = cm_GetRxConn(connp);
4016         code = RXAFS_ReleaseLock(callp, &tfid, &volSync);
4017         rx_PutConnection(callp);
4018
4019     } while (cm_Analyze(connp, userp, reqp, &cfid, &volSync,
4020                         NULL, NULL, code));
4021     code = cm_MapRPCError(code, reqp);
4022     if (code)
4023         osi_Log1(afsd_logp,
4024                  "CALL ReleaseLock FAILURE, code 0x%x", code);
4025     else
4026         osi_Log0(afsd_logp,
4027                  "CALL ReleaseLock SUCCESS");
4028         
4029     lock_ObtainMutex(&scp->mx);
4030
4031     return code;
4032 }
4033
4034 /* called with scp->mx held.  May release it during processing, but
4035    will exit with lock held.
4036
4037    This will return:
4038
4039    - 0 if the user has permission to get the specified lock for the scp
4040
4041    - CM_ERROR_NOACCESS if not
4042
4043    Any other error from cm_SyncOp will be sent down untranslated.
4044
4045    If CM_ERROR_NOACCESS is returned and lock_type is LockRead, then
4046    phas_insert (if non-NULL) will receive a boolean value indicating
4047    whether the user has INSERT permission or not.
4048 */
4049 long cm_LockCheckPerms(cm_scache_t * scp,
4050                        int lock_type,
4051                        cm_user_t * userp,
4052                        cm_req_t * reqp,
4053                        int * phas_insert)
4054 {
4055     long rights = 0;
4056     long code = 0, code2 = 0;
4057
4058     /* lock permissions are slightly tricky because of the 'i' bit.
4059        If the user has PRSFS_LOCK, she can read-lock the file.  If the
4060        user has PRSFS_WRITE, she can write-lock the file.  However, if
4061        the user has PRSFS_INSERT, then she can write-lock new files,
4062        but not old ones.  Since we don't have information about
4063        whether a file is new or not, we assume that if the user owns
4064        the scp, then she has the permissions that are granted by
4065        PRSFS_INSERT. */
4066
4067     osi_Log3(afsd_logp, "cm_LockCheckPerms for scp[0x%p] type[%d] user[0x%p]",
4068              scp, lock_type, userp);
4069
4070     if (lock_type == LockRead)
4071         rights |= PRSFS_LOCK;
4072     else if (lock_type == LockWrite)
4073         rights |= PRSFS_WRITE | PRSFS_LOCK;
4074     else {
4075         /* hmmkay */
4076         osi_assertx(FALSE, "invalid lock type");
4077         return 0;
4078     }
4079
4080     if (phas_insert)
4081         *phas_insert = FALSE;
4082
4083     code = cm_SyncOp(scp, NULL, userp, reqp, rights,
4084                      CM_SCACHESYNC_GETSTATUS |
4085                      CM_SCACHESYNC_NEEDCALLBACK);
4086
4087     if (phas_insert && scp->creator == userp) {
4088
4089         /* If this file was created by the user, then we check for
4090            PRSFS_INSERT.  If the file server is recent enough, then
4091            this should be sufficient for her to get a write-lock (but
4092            not necessarily a read-lock). VICED_CAPABILITY_WRITELOCKACL
4093            indicates whether a file server supports getting write
4094            locks when the user only has PRSFS_INSERT. 
4095            
4096            If the file was not created by the user we skip the check
4097            because the INSERT bit will not apply to this user even
4098            if it is set.
4099          */
4100
4101         code2 = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
4102                          CM_SCACHESYNC_GETSTATUS |
4103                          CM_SCACHESYNC_NEEDCALLBACK);
4104
4105         if (code2 == CM_ERROR_NOACCESS) {
4106             osi_Log0(afsd_logp, "cm_LockCheckPerms user has no INSERT bits");
4107         } else {
4108             *phas_insert = TRUE;
4109             osi_Log0(afsd_logp, "cm_LockCheckPerms user has INSERT bits");
4110         }
4111     }
4112
4113     cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
4114
4115     osi_Log1(afsd_logp, "cm_LockCheckPerms returning code %d", code);
4116
4117     return code;
4118 }
4119
4120 /* called with scp->mx held */
4121 long cm_Lock(cm_scache_t *scp, unsigned char sLockType,
4122              LARGE_INTEGER LOffset, LARGE_INTEGER LLength,
4123              cm_key_t key,
4124              int allowWait, cm_user_t *userp, cm_req_t *reqp,
4125              cm_file_lock_t **lockpp)
4126 {
4127     long code = 0;
4128     int Which = ((sLockType & LOCKING_ANDX_SHARED_LOCK) ? LockRead : LockWrite);
4129     cm_file_lock_t *fileLock;
4130     osi_queue_t *q;
4131     cm_range_t range;
4132     int wait_unlock = FALSE;
4133     int force_client_lock = FALSE;
4134
4135     osi_Log4(afsd_logp, "cm_Lock scp 0x%x type 0x%x offset %d length %d",
4136              scp, sLockType, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart);
4137     osi_Log3(afsd_logp, "... allowWait %d key 0x%x:%x", allowWait, 
4138              (unsigned long)(key >> 32), (unsigned long)(key & 0xffffffff));
4139
4140     /*
4141    A client C can OBTAIN a lock L on cm_scache_t S iff (both 3 and 4):
4142
4143    3. for all _a_ in (L->LOffset,+L->LLength), ALL of the following is
4144       true:
4145
4146        3.1 If L->LockType is exclusive then there does NOT exist a
4147          ACCEPTED lock M in S->fileLocks such that _a_ in
4148          (M->LOffset,+M->LLength).
4149
4150        3.2 If L->LockType is shared then for each ACCEPTED lock M in
4151          S->fileLocks, if _a_ in (M->LOffset,+M->LLength) then
4152          M->LockType is shared.
4153
4154    4. For all LOST locks M in S->fileLocks, ALL of the following are true:
4155
4156        4.1 M->key != Key(C)
4157
4158        4.2 If M->LockType is exclusive, then (L->LOffset,+L->LLength)
4159          and (M->LOffset,+M->LLength) do not intersect.
4160     */
4161
4162     range.offset = LOffset.QuadPart;
4163     range.length = LLength.QuadPart;
4164
4165     lock_ObtainRead(&cm_scacheLock);
4166
4167     for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
4168         fileLock =
4169             (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
4170
4171         if (IS_LOCK_LOST(fileLock)) {
4172             if (fileLock->key == key) {
4173                 code = CM_ERROR_BADFD;
4174                 break;
4175             } else if (fileLock->lockType == LockWrite && INTERSECT_RANGE(range, fileLock->range)) {
4176                 code = CM_ERROR_WOULDBLOCK;
4177                 wait_unlock = TRUE;
4178                 break;
4179             }
4180         }
4181
4182         /* we don't need to check for deleted locks here since deleted
4183            locks are dequeued from scp->fileLocks */
4184         if (IS_LOCK_ACCEPTED(fileLock) &&
4185            INTERSECT_RANGE(range, fileLock->range)) {
4186
4187             if ((sLockType & LOCKING_ANDX_SHARED_LOCK) == 0 ||
4188                 fileLock->lockType != LockRead) {
4189                 wait_unlock = TRUE;
4190                 code = CM_ERROR_WOULDBLOCK;
4191                 break;
4192             }
4193         }
4194     }
4195
4196     lock_ReleaseRead(&cm_scacheLock);
4197
4198     if (code == 0 && SERVERLOCKS_ENABLED(scp)) {
4199         if (Which == scp->serverLock ||
4200            (Which == LockRead && scp->serverLock == LockWrite)) {
4201
4202             int has_insert = 0;
4203
4204             /* we already have the lock we need */
4205             osi_Log3(afsd_logp, "   we already have the correct lock. exclusives[%d], shared[%d], serverLock[%d]", 
4206                      scp->exclusiveLocks, scp->sharedLocks, (int)(signed char) scp->serverLock);
4207
4208             code = cm_LockCheckPerms(scp, Which, userp, reqp, &has_insert);
4209
4210             /* special case: if we don't have permission to read-lock
4211                the file, then we force a clientside lock.  This is to
4212                compensate for applications that obtain a read-lock for
4213                reading files off of directories that don't grant
4214                read-locks to the user. */
4215             if (code == CM_ERROR_NOACCESS && Which == LockRead) {
4216
4217                 if (has_insert && SCP_SUPPORTS_WRITELOCKACL(scp)) {
4218                     osi_Log0(afsd_logp, "   User has no read-lock perms, but has INSERT perms.");
4219                     code = 0;
4220                 } else {
4221                     osi_Log0(afsd_logp, "   User has no read-lock perms. Forcing client-side lock");
4222                     force_client_lock = TRUE;
4223                 }
4224             }
4225
4226         } else if ((scp->exclusiveLocks > 0) ||
4227                    (scp->sharedLocks > 0 && scp->serverLock != LockRead)) {
4228             int has_insert = 0;
4229
4230             /* We are already waiting for some other lock.  We should
4231                wait for the daemon to catch up instead of generating a
4232                flood of SetLock calls. */
4233             osi_Log3(afsd_logp, "   already waiting for other lock. exclusives[%d], shared[%d], serverLock[%d]",
4234                      scp->exclusiveLocks, scp->sharedLocks, (int)(signed char) scp->serverLock);
4235
4236             /* see if we have permission to create the lock in the
4237                first place. */
4238             code = cm_LockCheckPerms(scp, Which, userp, reqp, &has_insert);
4239             if (code == 0)
4240                 code = CM_ERROR_WOULDBLOCK;
4241             else if (code == CM_ERROR_NOACCESS && Which == LockRead) {
4242
4243                 if (has_insert && SCP_SUPPORTS_WRITELOCKACL(scp)) {
4244                     osi_Log0(afsd_logp,
4245                              "   User has no read-lock perms, but has INSERT perms.");
4246                     code = CM_ERROR_WOULDBLOCK;
4247                 } else {
4248                     osi_Log0(afsd_logp,
4249                              "   User has no read-lock perms. Forcing client-side lock");
4250                     force_client_lock = TRUE;
4251                 }
4252             }
4253
4254             /* leave any other codes as-is */
4255
4256         } else {
4257             int newLock;
4258             int check_data_version = FALSE;
4259             int has_insert = 0;
4260
4261             /* first check if we have permission to elevate or obtain
4262                the lock. */
4263             code = cm_LockCheckPerms(scp, Which, userp, reqp, &has_insert);
4264             if (code) {
4265                 if (code == CM_ERROR_NOACCESS && Which == LockRead &&
4266                     (!has_insert || !SCP_SUPPORTS_WRITELOCKACL(scp))) {
4267                     osi_Log0(afsd_logp, "   User has no read-lock perms.  Forcing client-side lock");
4268                     force_client_lock = TRUE;
4269                 }
4270                 goto check_code;
4271             }
4272
4273             /* has_insert => (Which == LockRead, code == CM_ERROR_NOACCESS) */
4274
4275             if (scp->serverLock == LockRead && Which == LockWrite) {
4276
4277                 /* We want to escalate the lock to a LockWrite.
4278                  * Unfortunately that's not really possible without
4279                  * letting go of the current lock.  But for now we do
4280                  * it anyway. */
4281
4282                 osi_Log0(afsd_logp,
4283                          "   attempting to UPGRADE from LockRead to LockWrite.");
4284                 osi_Log1(afsd_logp,
4285                          "   dataVersion on scp: %I64d", scp->dataVersion);
4286
4287                 /* we assume at this point (because scp->serverLock
4288                    was valid) that we had a valid server lock. */
4289                 scp->lockDataVersion = scp->dataVersion;
4290                 check_data_version = TRUE;
4291         
4292                 code = cm_IntReleaseLock(scp, userp, reqp);
4293
4294                 if (code) {
4295                     /* We couldn't release the lock */
4296                     goto check_code;
4297                 } else {
4298                     scp->serverLock = -1;
4299                 }
4300             }
4301
4302             /* We need to obtain a server lock of type Which in order
4303              * to assert this file lock */
4304 #ifndef AGGRESSIVE_LOCKS
4305             newLock = Which;
4306 #else
4307             newLock = LockWrite;
4308 #endif
4309
4310             code = cm_IntSetLock(scp, userp, newLock, reqp);
4311
4312 #ifdef AGGRESSIVE_LOCKS
4313             if ((code == CM_ERROR_WOULDBLOCK ||
4314                  code == CM_ERROR_NOACCESS) && newLock != Which) {
4315                 /* we wanted LockRead.  We tried LockWrite. Now try
4316                  * LockRead again */
4317                 newLock = Which;
4318
4319                 /* am I sane? */
4320                 osi_assertx(newLock == LockRead, "lock type not read");
4321
4322                 code = cm_IntSetLock(scp, userp, newLock, reqp);
4323             }
4324 #endif
4325
4326             if (code == CM_ERROR_NOACCESS) {
4327                 if (Which == LockRead) {
4328                     if (has_insert && SCP_SUPPORTS_WRITELOCKACL(scp)) {
4329                         long tcode;
4330                         /* We requested a read-lock, but we have permission to
4331                          * get a write-lock. Try that */
4332
4333                         tcode = cm_LockCheckPerms(scp, LockWrite, userp, reqp, NULL);
4334
4335                         if (tcode == 0) {
4336                             newLock = LockWrite;
4337
4338                             osi_Log0(afsd_logp, "   User has 'i' perms and the request was for a LockRead.  Trying to get a LockWrite instead");
4339
4340                             code = cm_IntSetLock(scp, userp, newLock, reqp);
4341                         }
4342                     } else {
4343                         osi_Log0(afsd_logp, "   User has no read-lock perms.  Forcing client-side lock");
4344                         force_client_lock = TRUE;
4345                     }
4346                 } else if (Which == LockWrite &&
4347                            scp->creator == userp && !SCP_SUPPORTS_WRITELOCKACL(scp)) {
4348                     long tcode;
4349
4350                     /* Special case: if the lock request was for a
4351                      * LockWrite and the user owns the file and we weren't
4352                      * allowed to obtain the serverlock, we either lost a
4353                      * race (the permissions changed from under us), or we
4354                      * have 'i' bits, but we aren't allowed to lock the
4355                      * file. */
4356
4357                     /* check if we lost a race... */
4358                     tcode = cm_LockCheckPerms(scp, Which, userp, reqp, NULL);
4359
4360                     if (tcode == 0) {
4361                         osi_Log0(afsd_logp, "   User has 'i' perms but can't obtain write locks. Using client-side locks.");
4362                         force_client_lock = TRUE;
4363                     }
4364                 }
4365             }
4366
4367             if (code == 0 && check_data_version &&
4368                scp->dataVersion != scp->lockDataVersion) {
4369                 /* We lost a race.  Although we successfully obtained
4370                  * a lock, someone modified the file in between.  The
4371                  * locks have all been technically lost. */
4372
4373                 osi_Log0(afsd_logp,
4374                          "  Data version mismatch while upgrading lock.");
4375                 osi_Log2(afsd_logp,
4376                          "  Data versions before=%I64d, after=%I64d",
4377                          scp->lockDataVersion,
4378                          scp->dataVersion);
4379                 osi_Log1(afsd_logp,
4380                          "  Releasing stale lock for scp 0x%x", scp);
4381
4382                 code = cm_IntReleaseLock(scp, userp, reqp);
4383
4384                 scp->serverLock = -1;
4385
4386                 code = CM_ERROR_INVAL;
4387             } else if (code == 0) {
4388                 scp->serverLock = newLock;
4389                 scp->lockDataVersion = scp->dataVersion;
4390             }
4391
4392             if (code != 0 &&
4393                 (scp->sharedLocks > 0 || scp->exclusiveLocks > 0) &&
4394                 scp->serverLock == -1) {
4395                 /* Oops. We lost the lock. */
4396                 cm_LockMarkSCacheLost(scp);
4397             }
4398         }
4399     } else if (code == 0) {     /* server locks not enabled */
4400         osi_Log0(afsd_logp,
4401                  "  Skipping server lock for scp");
4402     }
4403
4404  check_code:
4405
4406     if (code != 0 && !force_client_lock) {
4407         /* Special case error translations
4408
4409            Applications don't expect certain errors from a
4410            LockFile/UnlockFile call.  We need to translate some error
4411            code to codes that apps expect and handle. */
4412
4413         /* We shouldn't actually need to handle this case since we
4414            simulate locks for RO scps anyway. */
4415         if (code == CM_ERROR_READONLY) {
4416             osi_Log0(afsd_logp, "   Reinterpreting CM_ERROR_READONLY as CM_ERROR_NOACCESS");
4417             code = CM_ERROR_NOACCESS;
4418         }
4419     }
4420
4421     if (code == 0 || (code == CM_ERROR_WOULDBLOCK && allowWait) ||
4422         force_client_lock) {
4423
4424         /* clear the error if we are forcing a client lock, so we
4425            don't get confused later. */
4426         if (force_client_lock && code != CM_ERROR_WOULDBLOCK)
4427             code = 0;
4428
4429         lock_ObtainWrite(&cm_scacheLock);
4430         fileLock = cm_GetFileLock();
4431         lock_ReleaseWrite(&cm_scacheLock);
4432 #ifdef DEBUG
4433         fileLock->fid = scp->fid;
4434 #endif
4435         fileLock->key = key;
4436         fileLock->lockType = Which;
4437         cm_HoldUser(userp);
4438         fileLock->userp = userp;
4439         fileLock->range = range;
4440         fileLock->flags = (code == 0 ? 0 : 
4441                            ((wait_unlock)?
4442                             CM_FILELOCK_FLAG_WAITUNLOCK :
4443                             CM_FILELOCK_FLAG_WAITLOCK));
4444
4445         if (force_client_lock || !SERVERLOCKS_ENABLED(scp))
4446             fileLock->flags |= CM_FILELOCK_FLAG_CLIENTONLY;
4447
4448         fileLock->lastUpdate = (code == 0 && !force_client_lock) ? time(NULL) : 0;
4449
4450         lock_ObtainWrite(&cm_scacheLock);
4451         osi_QAddT(&scp->fileLocksH, &scp->fileLocksT, &fileLock->fileq);
4452         cm_HoldSCacheNoLock(scp);
4453         fileLock->scp = scp;
4454         osi_QAdd(&cm_allFileLocks, &fileLock->q);
4455         lock_ReleaseWrite(&cm_scacheLock);
4456
4457         if (code != 0) {
4458             *lockpp = fileLock;
4459         }
4460
4461         if (IS_LOCK_CLIENTONLY(fileLock)) {
4462             scp->clientLocks++;
4463         } else if (IS_LOCK_ACCEPTED(fileLock)) {
4464             if (Which == LockRead)
4465                 scp->sharedLocks++;
4466             else
4467                 scp->exclusiveLocks++;
4468         }
4469
4470         osi_Log3(afsd_logp,
4471                  "cm_Lock Lock added 0x%p flags 0x%x to scp [0x%p]",
4472                  fileLock, fileLock->flags, scp);
4473         osi_Log4(afsd_logp,
4474                  "   exclusives[%d] shared[%d] client[%d] serverLock[%d]",
4475                  scp->exclusiveLocks, scp->sharedLocks, scp->clientLocks,
4476                  (int)(signed char) scp->serverLock);
4477     } else {
4478         osi_Log1(afsd_logp,
4479                  "cm_Lock Rejecting lock (code = 0x%x)", code);
4480     }
4481
4482     return code;
4483 }
4484
4485 static int cm_KeyEquals(cm_key_t k1, cm_key_t k2, int flags);
4486
4487 /* Called with scp->mx held */
4488 long cm_UnlockByKey(cm_scache_t * scp,
4489                     cm_key_t key,
4490                     int flags,
4491                     cm_user_t * userp,
4492                      cm_req_t * reqp)
4493 {
4494     long code = 0;
4495     cm_file_lock_t *fileLock;
4496     osi_queue_t *q, *qn;
4497     int n_unlocks = 0;
4498
4499     osi_Log4(afsd_logp, "cm_UnlockByKey scp 0x%p key 0x%x:%x flags=0x%x",
4500              scp,
4501              (unsigned long)(key >> 32),
4502              (unsigned long)(key & 0xffffffff),
4503              flags);
4504
4505     lock_ObtainWrite(&cm_scacheLock);
4506
4507     for (q = scp->fileLocksH; q; q = qn) {
4508         qn = osi_QNext(q);
4509
4510         fileLock = (cm_file_lock_t *)
4511             ((char *) q - offsetof(cm_file_lock_t, fileq));
4512
4513 #ifdef DEBUG
4514         osi_Log4(afsd_logp, "   Checking lock[0x%x] range[%d,+%d] type[%d]",
4515                  fileLock,
4516                  (unsigned long) fileLock->range.offset,
4517                  (unsigned long) fileLock->range.length,
4518                 fileLock->lockType);
4519         osi_Log3(afsd_logp, "     key[0x%x:%x] flags[0x%x]",
4520                  (unsigned long)(fileLock->key >> 32),
4521                  (unsigned long)(fileLock->key & 0xffffffff),
4522                  fileLock->flags);
4523
4524         if (cm_FidCmp(&fileLock->fid, &fileLock->scp->fid)) {
4525             osi_Log0(afsd_logp, "!!fileLock->fid != scp->fid");
4526             osi_Log4(afsd_logp, "  fileLock->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4527                      fileLock->fid.cell,
4528                      fileLock->fid.volume,
4529                      fileLock->fid.vnode,
4530                      fileLock->fid.unique);
4531             osi_Log4(afsd_logp, "  scp->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4532                      fileLock->scp->fid.cell,
4533                      fileLock->scp->fid.volume,
4534                      fileLock->scp->fid.vnode,
4535                      fileLock->scp->fid.unique);
4536             osi_assertx(FALSE, "invalid fid value");
4537         }
4538 #endif
4539
4540         if (!IS_LOCK_DELETED(fileLock) &&
4541             cm_KeyEquals(fileLock->key, key, flags)) {
4542             osi_Log3(afsd_logp, "...Unlock range [%d,+%d] type %d",
4543                     fileLock->range.offset,
4544                     fileLock->range.length,
4545                     fileLock->lockType);
4546
4547             if (scp->fileLocksT == q)
4548                 scp->fileLocksT = osi_QPrev(q);
4549             osi_QRemoveHT(&scp->fileLocksH, &scp->fileLocksT, q);
4550
4551             if (IS_LOCK_CLIENTONLY(fileLock)) {
4552                 scp->clientLocks--;
4553             } else if (IS_LOCK_ACCEPTED(fileLock)) {
4554                 if (fileLock->lockType == LockRead)
4555                     scp->sharedLocks--;
4556                 else
4557                     scp->exclusiveLocks--;
4558             }
4559
4560             fileLock->flags |= CM_FILELOCK_FLAG_DELETED;
4561
4562             cm_ReleaseUser(fileLock->userp);
4563             cm_ReleaseSCacheNoLock(scp);
4564
4565             fileLock->userp = NULL;
4566             fileLock->scp = NULL;
4567
4568             n_unlocks++;
4569         }
4570     }
4571
4572     lock_ReleaseWrite(&cm_scacheLock);
4573
4574     if (n_unlocks == 0) {
4575         osi_Log0(afsd_logp, "cm_UnlockByKey no locks found");
4576         osi_Log3(afsd_logp, "   Leaving scp with exclusives[%d], shared[%d], serverLock[%d]",
4577                  scp->exclusiveLocks, scp->sharedLocks, (int)(signed char) scp->serverLock);
4578         
4579         return 0;
4580     }
4581
4582     osi_Log1(afsd_logp, "cm_UnlockByKey done with %d locks", n_unlocks);
4583
4584     osi_assertx(scp->sharedLocks >= 0, "scp->sharedLocks < 0");
4585     osi_assertx(scp->exclusiveLocks >= 0, "scp->exclusiveLocks < 0");
4586     osi_assertx(scp->clientLocks >= 0, "scp->clientLocks < 0");
4587
4588     if (!SERVERLOCKS_ENABLED(scp)) {
4589         osi_Log0(afsd_logp, "  Skipping server lock for scp");
4590         goto done;
4591     }
4592
4593     /* Ideally we would go through the rest of the locks to determine
4594      * if one or more locks that were formerly in WAITUNLOCK can now
4595      * be put to ACTIVE or WAITLOCK and update scp->exclusiveLocks and
4596      * scp->sharedLocks accordingly.  However, the retrying of locks
4597      * in that manner is done cm_RetryLock() manually.
4598      */
4599
4600     if (scp->serverLock == LockWrite &&
4601         scp->exclusiveLocks == 0 &&
4602         scp->sharedLocks > 0) {
4603
4604         /* The serverLock should be downgraded to LockRead */
4605         osi_Log0(afsd_logp, "  DOWNGRADE lock from LockWrite to LockRead");
4606
4607         /* since scp->serverLock looked sane, we are going to assume
4608            that we have a valid server lock. */
4609         scp->lockDataVersion = scp->dataVersion;
4610         osi_Log1(afsd_logp, "  dataVersion on scp = %I64d", scp->dataVersion);
4611
4612         code = cm_IntReleaseLock(scp, userp, reqp);
4613
4614         if (code) {
4615             /* so we couldn't release it.  Just let the lock be for now */
4616             code = 0;
4617             goto done;
4618         } else {
4619             scp->serverLock = -1;
4620         }
4621
4622         code = cm_IntSetLock(scp, userp, LockRead, reqp);
4623
4624         if (code == 0 && scp->lockDataVersion == scp->dataVersion) {
4625             scp->serverLock = LockRead;
4626         } else if (code == 0 && scp->lockDataVersion != scp->dataVersion) {
4627             /* We lost a race condition.  Although we have a valid
4628                lock on the file, the data has changed and essentially
4629                we have lost the lock we had during the transition. */
4630
4631             osi_Log0(afsd_logp, "Data version mismatch during lock downgrade");
4632             osi_Log2(afsd_logp, "  Data versions before=%I64d, after=%I64d",
4633                      scp->lockDataVersion,
4634                      scp->dataVersion);
4635             
4636             code = cm_IntReleaseLock(scp, userp, reqp);
4637
4638             code = CM_ERROR_INVAL;
4639             scp->serverLock = -1;
4640         }
4641
4642         if (code != 0 &&
4643             (scp->sharedLocks > 0 || scp->exclusiveLocks > 0) &&
4644                 (scp->serverLock == -1)) {
4645                 /* Oopsie */
4646                 cm_LockMarkSCacheLost(scp);
4647             }
4648
4649         /* failure here has no bearing on the return value of
4650            cm_Unlock() */
4651         code = 0;
4652
4653     } else if (scp->serverLock != (-1) &&
4654               scp->exclusiveLocks == 0 &&
4655               scp->sharedLocks == 0) {
4656         /* The serverLock should be released entirely */
4657
4658         code = cm_IntReleaseLock(scp, userp, reqp);
4659
4660         if (code == 0)
4661             scp->serverLock = (-1);
4662     }
4663
4664  done:
4665
4666     osi_Log1(afsd_logp, "cm_UnlockByKey code 0x%x", code);
4667     osi_Log4(afsd_logp, "   Leaving scp with excl[%d], shared[%d], client[%d], serverLock[%d]",
4668              scp->exclusiveLocks, scp->sharedLocks, scp->clientLocks,
4669              (int)(signed char) scp->serverLock);
4670
4671     return code;
4672 }
4673
4674 long cm_Unlock(cm_scache_t *scp, 
4675                unsigned char sLockType,
4676                LARGE_INTEGER LOffset, LARGE_INTEGER LLength,
4677                cm_key_t key, 
4678                cm_user_t *userp, 
4679                cm_req_t *reqp)
4680 {
4681     long code = 0;
4682     int Which = ((sLockType & LOCKING_ANDX_SHARED_LOCK) ? LockRead : LockWrite);
4683     cm_file_lock_t *fileLock;
4684     osi_queue_t *q;
4685     int release_userp = FALSE;
4686
4687     osi_Log4(afsd_logp, "cm_Unlock scp 0x%p type 0x%x offset %d length %d",
4688              scp, sLockType, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart);
4689     osi_Log2(afsd_logp, "... key 0x%x:%x",
4690              (unsigned long) (key >> 32), (unsigned long) (key & 0xffffffff));
4691
4692     lock_ObtainRead(&cm_scacheLock);
4693
4694     for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
4695         fileLock = (cm_file_lock_t *)
4696             ((char *) q - offsetof(cm_file_lock_t, fileq));
4697
4698 #ifdef DEBUG
4699         if (cm_FidCmp(&fileLock->fid, &fileLock->scp->fid)) {
4700             osi_Log0(afsd_logp, "!!fileLock->fid != scp->fid");
4701             osi_Log4(afsd_logp, "  fileLock->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4702                      fileLock->fid.cell,
4703                      fileLock->fid.volume,
4704                      fileLock->fid.vnode,
4705                      fileLock->fid.unique);
4706             osi_Log4(afsd_logp, "  scp->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4707                      fileLock->scp->fid.cell,
4708                      fileLock->scp->fid.volume,
4709                      fileLock->scp->fid.vnode,
4710                      fileLock->scp->fid.unique);
4711             osi_assertx(FALSE, "invalid fid value");
4712         }
4713 #endif
4714         if (!IS_LOCK_DELETED(fileLock) &&
4715             fileLock->key == key &&
4716             fileLock->range.offset == LOffset.QuadPart &&
4717             fileLock->range.length == LLength.QuadPart) {
4718             break;
4719         }
4720     }
4721
4722     if (!q) {
4723         osi_Log0(afsd_logp, "cm_Unlock lock not found; failure");
4724         
4725         lock_ReleaseRead(&cm_scacheLock);
4726
4727         /* The lock didn't exist anyway. *shrug* */
4728         return 0;
4729     }
4730
4731     lock_ReleaseRead(&cm_scacheLock);
4732
4733     /* discard lock record */
4734     lock_ObtainWrite(&cm_scacheLock);
4735     if (scp->fileLocksT == q)
4736         scp->fileLocksT = osi_QPrev(q);
4737     osi_QRemoveHT(&scp->fileLocksH, &scp->fileLocksT, q);
4738
4739     /*
4740      * Don't delete it here; let the daemon delete it, to simplify
4741      * the daemon's traversal of the list.
4742      */
4743
4744     if (IS_LOCK_CLIENTONLY(fileLock)) {
4745         scp->clientLocks--;
4746     } else if (IS_LOCK_ACCEPTED(fileLock)) {
4747         if (fileLock->lockType == LockRead)
4748             scp->sharedLocks--;
4749         else
4750             scp->exclusiveLocks--;
4751     }
4752
4753     fileLock->flags |= CM_FILELOCK_FLAG_DELETED;
4754     if (userp != NULL) {
4755         cm_ReleaseUser(fileLock->userp);
4756     } else {
4757         userp = fileLock->userp;
4758         release_userp = TRUE;
4759     }
4760     fileLock->userp = NULL;
4761     cm_ReleaseSCacheNoLock(scp);
4762     fileLock->scp = NULL;
4763     lock_ReleaseWrite(&cm_scacheLock);
4764
4765     if (!SERVERLOCKS_ENABLED(scp)) {
4766         osi_Log0(afsd_logp, "   Skipping server locks for scp");
4767         goto done;
4768     }
4769
4770     /* Ideally we would go through the rest of the locks to determine
4771      * if one or more locks that were formerly in WAITUNLOCK can now
4772      * be put to ACTIVE or WAITLOCK and update scp->exclusiveLocks and
4773      * scp->sharedLocks accordingly.  However, the retrying of locks
4774      * in that manner is done cm_RetryLock() manually.
4775      */
4776
4777     if (scp->serverLock == LockWrite &&
4778         scp->exclusiveLocks == 0 &&
4779         scp->sharedLocks > 0) {
4780
4781         /* The serverLock should be downgraded to LockRead */
4782         osi_Log0(afsd_logp, "  DOWNGRADE lock from LockWrite to LockRead");
4783
4784         /* Since we already had a lock, we assume that there is a
4785            valid server lock. */
4786         scp->lockDataVersion = scp->dataVersion;
4787         osi_Log1(afsd_logp, "   dataVersion on scp is %I64d", scp->dataVersion);
4788
4789         /* before we downgrade, make sure that we have enough
4790            permissions to get the read lock. */
4791         code = cm_LockCheckPerms(scp, LockRead, userp, reqp, NULL);
4792         if (code != 0) {
4793
4794             osi_Log0(afsd_logp, "  SKIPPING downgrade because user doesn't have perms to get downgraded lock");
4795
4796             code = 0;
4797             goto done;
4798         }
4799
4800         code = cm_IntReleaseLock(scp, userp, reqp);
4801
4802         if (code) {
4803             /* so we couldn't release it.  Just let the lock be for now */
4804             code = 0;
4805             goto done;
4806         } else {
4807             scp->serverLock = -1;
4808         }
4809
4810         code = cm_IntSetLock(scp, userp, LockRead, reqp);
4811
4812         if (code == 0 && scp->lockDataVersion == scp->dataVersion) {
4813             scp->serverLock = LockRead;
4814         } else if (code == 0 && scp->lockDataVersion != scp->dataVersion) {
4815             /* Lost a race.  We obtained a new lock, but that is
4816                meaningless since someone modified the file
4817                inbetween. */
4818
4819             osi_Log0(afsd_logp,
4820                      "Data version mismatch while downgrading lock");
4821             osi_Log2(afsd_logp,
4822                      "  Data versions before=%I64d, after=%I64d",
4823                      scp->lockDataVersion,
4824                      scp->dataVersion);
4825             
4826             code = cm_IntReleaseLock(scp, userp, reqp);
4827
4828             scp->serverLock = -1;
4829             code = CM_ERROR_INVAL;
4830         }
4831
4832         if (code != 0 &&
4833             (scp->sharedLocks > 0 || scp->exclusiveLocks > 0) &&
4834                 (scp->serverLock == -1)) {
4835                 /* Oopsie */
4836                 cm_LockMarkSCacheLost(scp);
4837             }
4838
4839         /* failure here has no bearing on the return value of
4840            cm_Unlock() */
4841         code = 0;
4842
4843     } else if (scp->serverLock != (-1) &&
4844               scp->exclusiveLocks == 0 &&
4845               scp->sharedLocks == 0) {
4846         /* The serverLock should be released entirely */
4847
4848         code = cm_IntReleaseLock(scp, userp, reqp);
4849
4850         if (code == 0) {
4851             scp->serverLock = (-1);
4852         }
4853     }
4854
4855     if (release_userp)
4856         cm_ReleaseUser(userp);
4857
4858  done:
4859
4860     osi_Log1(afsd_logp, "cm_Unlock code 0x%x", code);
4861     osi_Log4(afsd_logp, "  leaving scp with excl[%d], shared[%d], client[%d], serverLock[%d]",
4862              scp->exclusiveLocks, scp->sharedLocks, scp->clientLocks,
4863              (int)(signed char) scp->serverLock);
4864
4865     return code;
4866 }
4867
4868 /* called with scp->mx held */
4869 static void cm_LockMarkSCacheLost(cm_scache_t * scp)
4870 {
4871     cm_file_lock_t *fileLock;
4872     osi_queue_t *q;
4873
4874     osi_Log1(afsd_logp, "cm_LockMarkSCacheLost scp 0x%x", scp);
4875
4876 #ifdef DEBUG
4877     /* With the current code, we can't lose a lock on a RO scp */
4878     osi_assertx(!(scp->flags & CM_SCACHEFLAG_RO), "CM_SCACHEFLAG_RO unexpected");
4879 #endif
4880
4881     /* cm_scacheLock needed because we are modifying fileLock->flags */
4882     lock_ObtainWrite(&cm_scacheLock);
4883
4884     for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
4885         fileLock = 
4886             (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
4887
4888         if (IS_LOCK_ACTIVE(fileLock) &&
4889             !IS_LOCK_CLIENTONLY(fileLock)) {
4890             if (fileLock->lockType == LockRead)
4891                 scp->sharedLocks--;
4892             else
4893                 scp->exclusiveLocks--;
4894
4895             fileLock->flags |= CM_FILELOCK_FLAG_LOST;
4896         }
4897     }
4898
4899     scp->serverLock = -1;
4900     scp->lockDataVersion = -1;
4901     lock_ReleaseWrite(&cm_scacheLock);
4902 }
4903
4904 /* Called with no relevant locks held */
4905 void cm_CheckLocks()
4906 {
4907     osi_queue_t *q, *nq;
4908     cm_file_lock_t *fileLock;
4909     cm_req_t req;
4910     AFSFid tfid;
4911     AFSVolSync volSync;
4912     cm_conn_t *connp;
4913     long code;
4914     struct rx_connection * callp;
4915     cm_scache_t * scp;
4916
4917     cm_InitReq(&req);
4918
4919     lock_ObtainWrite(&cm_scacheLock);
4920
4921     cm_lockRefreshCycle++;
4922
4923     osi_Log1(afsd_logp, "cm_CheckLocks starting lock check cycle %d", cm_lockRefreshCycle);
4924
4925     for (q = cm_allFileLocks; q; q = nq) {
4926         fileLock = (cm_file_lock_t *) q;
4927         nq = osi_QNext(q);
4928         code = -1;
4929
4930         if (IS_LOCK_DELETED(fileLock)) {
4931
4932             osi_QRemove(&cm_allFileLocks, q);
4933             cm_PutFileLock(fileLock);
4934
4935         } else if (IS_LOCK_ACTIVE(fileLock) && !IS_LOCK_CLIENTONLY(fileLock)) {
4936
4937             /* Server locks must have been enabled for us to have
4938                received an active non-client-only lock. */
4939             osi_assertx(cm_enableServerLocks, "!cm_enableServerLocks");
4940
4941             scp = fileLock->scp;
4942             osi_assertx(scp != NULL, "null cm_scache_t");
4943
4944             cm_HoldSCacheNoLock(scp);
4945
4946 #ifdef DEBUG
4947             if (cm_FidCmp(&fileLock->fid, &fileLock->scp->fid)) {
4948                 osi_Log0(afsd_logp, "!!fileLock->fid != scp->fid");
4949                 osi_Log4(afsd_logp, "  fileLock->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4950                          fileLock->fid.cell,
4951                          fileLock->fid.volume,
4952                          fileLock->fid.vnode,
4953                          fileLock->fid.unique);
4954                 osi_Log4(afsd_logp, "  scp->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4955                          fileLock->scp->fid.cell,
4956                          fileLock->scp->fid.volume,
4957                          fileLock->scp->fid.vnode,
4958                          fileLock->scp->fid.unique);
4959                 osi_assertx(FALSE, "invalid fid");
4960             }
4961 #endif
4962             /* Server locks are extended once per scp per refresh
4963                cycle. */
4964             if (scp->lastRefreshCycle != cm_lockRefreshCycle) {
4965
4966                 int scp_done = FALSE;
4967
4968                 osi_Log1(afsd_logp, "cm_CheckLocks Updating scp 0x%x", scp);
4969
4970                 lock_ReleaseWrite(&cm_scacheLock);
4971                 lock_ObtainMutex(&scp->mx);
4972
4973                 /* did the lock change while we weren't holding the lock? */
4974                 if (!IS_LOCK_ACTIVE(fileLock))
4975                     goto post_syncopdone;
4976
4977                 code = cm_SyncOp(scp, NULL, fileLock->userp, &req, 0,
4978                                  CM_SCACHESYNC_NEEDCALLBACK
4979                                  | CM_SCACHESYNC_GETSTATUS
4980                                  | CM_SCACHESYNC_LOCK);
4981
4982                 if (code) {
4983                     osi_Log1(smb_logp,
4984                              "cm_CheckLocks SyncOp failure code 0x%x", code);
4985                     goto post_syncopdone;
4986                 }
4987
4988                 /* cm_SyncOp releases scp->mx during which the lock
4989                    may get released. */
4990                 if (!IS_LOCK_ACTIVE(fileLock))
4991                     goto pre_syncopdone;
4992
4993                 if (scp->serverLock != -1) {
4994                     cm_fid_t cfid;
4995                     cm_user_t * userp;
4996
4997                     tfid.Volume = scp->fid.volume;
4998                     tfid.Vnode = scp->fid.vnode;
4999                     tfid.Unique = scp->fid.unique;
5000                     cfid = scp->fid;
5001                     userp = fileLock->userp;
5002                     
5003                     osi_Log3(afsd_logp, "CALL ExtendLock lock 0x%p for scp=0x%p with lock %d", 
5004                              fileLock,
5005                              scp,
5006                              (int) scp->serverLock);
5007
5008                     lock_ReleaseMutex(&scp->mx);
5009
5010                     do {
5011                         code = cm_ConnFromFID(&cfid, userp,
5012                                        &req, &connp);
5013                         if (code) 
5014                             break;
5015
5016                         callp = cm_GetRxConn(connp);
5017                         code = RXAFS_ExtendLock(callp, &tfid,
5018                                                 &volSync);
5019                         rx_PutConnection(callp);
5020
5021                         osi_Log1(afsd_logp, "   ExtendLock returns %d", code);
5022
5023                     } while (cm_Analyze(connp, userp, &req,
5024                                         &cfid, &volSync, NULL, NULL,
5025                                         code));
5026
5027                     code = cm_MapRPCError(code, &req);
5028
5029                     lock_ObtainMutex(&scp->mx);
5030
5031                     if (code) {
5032                         osi_Log1(afsd_logp, "CALL ExtendLock FAILURE, code 0x%x", code);
5033                     } else {
5034                         osi_Log0(afsd_logp, "CALL ExtendLock SUCCESS");
5035                         scp->lockDataVersion = scp->dataVersion;
5036                     }
5037
5038                     if ((code == EINVAL || code == CM_ERROR_INVAL) &&
5039                         scp->lockDataVersion == scp->dataVersion) {
5040                         int lockType;
5041
5042                         lockType =
5043                             (scp->exclusiveLocks > 0) ? LockWrite: LockRead;
5044
5045                         /* we might still have a chance to obtain a
5046                            new lock */
5047
5048                         code = cm_IntSetLock(scp, userp, lockType, &req);
5049
5050                         if (code) {
5051                             code = CM_ERROR_INVAL;
5052                         } else if (scp->lockDataVersion != scp->dataVersion) {
5053
5054                             /* now check if we still have the file at
5055                                the right data version. */
5056                             osi_Log1(afsd_logp,
5057                                      "Data version mismatch on scp 0x%p",
5058                                      scp);
5059                             osi_Log2(afsd_logp,
5060                                      "   Data versions: before=%I64d, after=%I64d",
5061                                      scp->lockDataVersion,
5062                                      scp->dataVersion);
5063
5064                             code = cm_IntReleaseLock(scp, userp, &req);
5065
5066                             code = CM_ERROR_INVAL;
5067                         }
5068                     }
5069
5070                     if (code == EINVAL || code == CM_ERROR_INVAL) {
5071                         cm_LockMarkSCacheLost(scp);
5072                     }
5073
5074                 } else {
5075                     /* interestingly, we have found an active lock
5076                        belonging to an scache that has no
5077                        serverLock */
5078                     cm_LockMarkSCacheLost(scp);
5079                 }
5080
5081                 scp_done = TRUE;
5082
5083             pre_syncopdone:
5084
5085                 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
5086
5087             post_syncopdone:
5088                 lock_ReleaseMutex(&scp->mx);
5089
5090                 lock_ObtainWrite(&cm_scacheLock);
5091
5092                 if (code == 0) {
5093                     fileLock->lastUpdate = time(NULL);
5094                 }
5095                 
5096                 if (scp_done)
5097                     scp->lastRefreshCycle = cm_lockRefreshCycle;
5098
5099             } else {
5100                 /* we have already refreshed the locks on this scp */
5101                 fileLock->lastUpdate = time(NULL);
5102             }
5103
5104             cm_ReleaseSCacheNoLock(scp);
5105
5106         } else if (IS_LOCK_ACTIVE(fileLock) && IS_LOCK_CLIENTONLY(fileLock)) {
5107             /* TODO: Check callbacks */
5108         }
5109     }
5110
5111     lock_ReleaseWrite(&cm_scacheLock);
5112     osi_Log1(afsd_logp, "cm_CheckLocks completes lock check cycle %d", cm_lockRefreshCycle);
5113 }
5114
5115 /* NOT called with scp->mx held. */
5116 long cm_RetryLock(cm_file_lock_t *oldFileLock, int client_is_dead)
5117 {
5118     long code = 0;
5119     cm_scache_t *scp = NULL;
5120     cm_file_lock_t *fileLock;
5121     osi_queue_t *q;
5122     cm_req_t req;
5123     int newLock = -1;
5124     int force_client_lock = FALSE;
5125     int has_insert = FALSE;
5126     int check_data_version = FALSE;
5127
5128     cm_InitReq(&req);
5129
5130     if (client_is_dead) {
5131         code = CM_ERROR_TIMEDOUT;
5132         goto updateLock;
5133     }
5134
5135     lock_ObtainRead(&cm_scacheLock);
5136
5137     osi_Log2(afsd_logp, "cm_RetryLock checking lock %p (scp=%p)", oldFileLock, oldFileLock->scp);
5138     osi_Log4(afsd_logp, "    offset(%x:%x) length(%x:%x)",
5139              (unsigned)(oldFileLock->range.offset >> 32),
5140              (unsigned)(oldFileLock->range.offset & 0xffffffff),
5141              (unsigned)(oldFileLock->range.length >> 32),
5142              (unsigned)(oldFileLock->range.length & 0xffffffff));
5143     osi_Log3(afsd_logp, "    key(%x:%x) flags=%x",
5144              (unsigned)(oldFileLock->key >> 32),
5145              (unsigned)(oldFileLock->key & 0xffffffff),
5146              (unsigned)(oldFileLock->flags));
5147
5148     /* if the lock has already been granted, then we have nothing to do */
5149     if (IS_LOCK_ACTIVE(oldFileLock)) {
5150         lock_ReleaseRead(&cm_scacheLock);
5151         osi_Log0(afsd_logp, "cm_RetryLock lock already granted");
5152         return 0;
5153     }
5154
5155     /* we can't do anything with lost or deleted locks at the moment. */
5156     if (IS_LOCK_LOST(oldFileLock) || IS_LOCK_DELETED(oldFileLock)) {
5157         code = CM_ERROR_BADFD;
5158         osi_Log0(afsd_logp, "cm_RetryLock lock is lost or deleted");
5159         lock_ReleaseRead(&cm_scacheLock);
5160         goto updateLock;
5161     }
5162
5163     scp = oldFileLock->scp;
5164
5165     osi_assertx(scp != NULL, "null cm_scache_t");
5166
5167     lock_ReleaseRead(&cm_scacheLock);
5168     lock_ObtainMutex(&scp->mx);
5169
5170     code = cm_LockCheckPerms(scp, oldFileLock->lockType,
5171                              oldFileLock->userp,
5172                              &req, &has_insert);
5173
5174     if (code == CM_ERROR_NOACCESS && oldFileLock->lockType == LockRead) {
5175         if (!has_insert || !SCP_SUPPORTS_WRITELOCKACL(scp)) {
5176         force_client_lock = TRUE;
5177         }
5178         code = 0;
5179     } else if (code) {
5180         lock_ReleaseMutex(&scp->mx);
5181         return code;
5182     }
5183
5184     lock_ObtainWrite(&cm_scacheLock);
5185
5186     /* Check if we already have a sufficient server lock to allow this
5187        lock to go through. */
5188     if (IS_LOCK_WAITLOCK(oldFileLock) &&
5189         (!SERVERLOCKS_ENABLED(scp) ||
5190          scp->serverLock == oldFileLock->lockType ||
5191          scp->serverLock == LockWrite)) {
5192
5193         oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITLOCK;
5194
5195         if (SERVERLOCKS_ENABLED(scp)) {
5196             osi_Log1(afsd_logp, "cm_RetryLock Server lock (%d) is sufficient for lock.  Granting",
5197                      (int) scp->serverLock);
5198         } else {
5199             osi_Log0(afsd_logp, "cm_RetryLock skipping server lock for scp");
5200         }
5201
5202         lock_ReleaseWrite(&cm_scacheLock);
5203         lock_ReleaseMutex(&scp->mx);
5204
5205         return 0;
5206     }
5207
5208     if (IS_LOCK_WAITUNLOCK(oldFileLock)) {
5209
5210         /* check if the conflicting locks have dissappeared already */
5211         for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
5212
5213             fileLock = (cm_file_lock_t *)
5214                 ((char *) q - offsetof(cm_file_lock_t, fileq));
5215
5216             if (IS_LOCK_LOST(fileLock)) {
5217                 if (fileLock->key == oldFileLock->key) {
5218                     code = CM_ERROR_BADFD;
5219                     oldFileLock->flags |= CM_FILELOCK_FLAG_LOST;
5220                     osi_Log1(afsd_logp, "    found lost lock %p for same key.  Marking lock as lost",
5221                              fileLock);
5222                     break;
5223                 } else if (fileLock->lockType == LockWrite &&
5224                            INTERSECT_RANGE(oldFileLock->range, fileLock->range)) {
5225                     osi_Log1(afsd_logp, "    found conflicting LOST lock %p", fileLock);
5226                     code = CM_ERROR_WOULDBLOCK;
5227                     break;
5228                 }
5229             }
5230
5231             if (IS_LOCK_ACCEPTED(fileLock) &&
5232                 INTERSECT_RANGE(oldFileLock->range, fileLock->range)) {
5233
5234                 if (oldFileLock->lockType != LockRead ||
5235                    fileLock->lockType != LockRead) {
5236
5237                     osi_Log1(afsd_logp, "    found conflicting lock %p", fileLock);
5238                     code = CM_ERROR_WOULDBLOCK;
5239                     break;
5240                 }
5241             }
5242         }
5243     }
5244
5245     if (code != 0) {
5246         lock_ReleaseWrite(&cm_scacheLock);
5247         lock_ReleaseMutex(&scp->mx);
5248
5249         goto handleCode;
5250     }
5251
5252     /* when we get here, the lock is either a WAITUNLOCK or WAITLOCK.
5253        If it is WAITUNLOCK, then we didn't find any conflicting lock
5254        but we haven't verfied whether the serverLock is sufficient to
5255        assert it.  If it is WAITLOCK, then the serverLock is
5256        insufficient to assert it. Eitherway, we are ready to accept
5257        the lock as either ACTIVE or WAITLOCK depending on the
5258        serverLock. */
5259
5260     /* First, promote the WAITUNLOCK to a WAITLOCK */
5261     if (IS_LOCK_WAITUNLOCK(oldFileLock)) {
5262         if (oldFileLock->lockType == LockRead)
5263             scp->sharedLocks++;
5264         else
5265             scp->exclusiveLocks++;
5266
5267         oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITUNLOCK;
5268         oldFileLock->flags |= CM_FILELOCK_FLAG_WAITLOCK;
5269     }
5270
5271     osi_assertx(IS_LOCK_WAITLOCK(oldFileLock), "!IS_LOCK_WAITLOCK");
5272
5273     if (force_client_lock ||
5274         !SERVERLOCKS_ENABLED(scp) ||
5275         scp->serverLock == oldFileLock->lockType ||
5276         (oldFileLock->lockType == LockRead &&
5277          scp->serverLock == LockWrite)) {
5278
5279         oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITLOCK;
5280
5281         if ((force_client_lock ||
5282              !SERVERLOCKS_ENABLED(scp)) &&
5283             !IS_LOCK_CLIENTONLY(oldFileLock)) {
5284
5285             oldFileLock->flags |= CM_FILELOCK_FLAG_CLIENTONLY;
5286
5287             if (oldFileLock->lockType == LockRead)
5288                 scp->sharedLocks--;
5289             else
5290                 scp->exclusiveLocks--;
5291
5292             scp->clientLocks++;
5293         }
5294
5295         lock_ReleaseWrite(&cm_scacheLock);
5296         lock_ReleaseMutex(&scp->mx);
5297
5298         return 0;
5299
5300     } else {
5301         cm_user_t * userp;
5302
5303         code = cm_SyncOp(scp, NULL, oldFileLock->userp, &req, 0,
5304                          CM_SCACHESYNC_NEEDCALLBACK
5305                          | CM_SCACHESYNC_GETSTATUS
5306                          | CM_SCACHESYNC_LOCK);
5307         if (code) {
5308             osi_Log1(smb_logp, "cm_RetryLock SyncOp failure code 0x%x", code);
5309             lock_ReleaseWrite(&cm_scacheLock);
5310             goto post_syncopdone;
5311         }
5312
5313         if (!IS_LOCK_WAITLOCK(oldFileLock))
5314             goto pre_syncopdone;
5315
5316         userp = oldFileLock->userp;
5317
5318 #ifndef AGGRESSIVE_LOCKS
5319         newLock = oldFileLock->lockType;
5320 #else
5321         newLock = LockWrite;
5322 #endif
5323
5324         if (has_insert) {
5325             /* if has_insert is non-zero, then:
5326                - the lock a LockRead
5327                - we don't have permission to get a LockRead
5328                - we do have permission to get a LockWrite
5329                - the server supports VICED_CAPABILITY_WRITELOCKACL
5330             */
5331
5332             newLock = LockWrite;
5333         }
5334
5335         lock_ReleaseWrite(&cm_scacheLock);
5336
5337         /* when we get here, either we have a read-lock and want a
5338            write-lock or we don't have any locks and we want some
5339            lock. */
5340
5341         if (scp->serverLock == LockRead) {
5342
5343             osi_assertx(newLock == LockWrite, "!LockWrite");
5344
5345             osi_Log0(afsd_logp, "  Attempting to UPGRADE from LockRead to LockWrite");
5346
5347             scp->lockDataVersion = scp->dataVersion;
5348             check_data_version = TRUE;
5349
5350             code = cm_IntReleaseLock(scp, userp, &req);
5351
5352             if (code)
5353                 goto pre_syncopdone;
5354             else
5355                 scp->serverLock = -1;
5356         }
5357
5358         code = cm_IntSetLock(scp, userp, newLock, &req);
5359
5360         if (code == 0) {
5361             if (scp->dataVersion != scp->lockDataVersion) {
5362                 /* we lost a race.  too bad */
5363
5364                 osi_Log0(afsd_logp,
5365                          "  Data version mismatch while upgrading lock.");
5366                 osi_Log2(afsd_logp,
5367                          "  Data versions before=%I64d, after=%I64d",
5368                          scp->lockDataVersion,
5369                          scp->dataVersion);
5370                 osi_Log1(afsd_logp,
5371                          "  Releasing stale lock for scp 0x%x", scp);
5372
5373                 code = cm_IntReleaseLock(scp, userp, &req);
5374
5375                 scp->serverLock = -1;
5376
5377                 code = CM_ERROR_INVAL;
5378
5379                 cm_LockMarkSCacheLost(scp);
5380             } else {
5381                 scp->serverLock = newLock;
5382             }
5383         }
5384
5385     pre_syncopdone:
5386         cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
5387     post_syncopdone:
5388         ;
5389     }
5390
5391   handleCode:
5392     if (code != 0 && code != CM_ERROR_WOULDBLOCK) {
5393         lock_ObtainWrite(&cm_scacheLock);
5394         if (scp->fileLocksT == &oldFileLock->fileq)
5395             scp->fileLocksT = osi_QPrev(&oldFileLock->fileq);
5396         osi_QRemoveHT(&scp->fileLocksH, &scp->fileLocksT, &oldFileLock->fileq);
5397         lock_ReleaseWrite(&cm_scacheLock);
5398     }
5399     lock_ReleaseMutex(&scp->mx);
5400
5401   updateLock:
5402     lock_ObtainWrite(&cm_scacheLock);
5403     if (code == 0) {
5404         oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITLOCK;
5405     } else if (code != CM_ERROR_WOULDBLOCK) {
5406         oldFileLock->flags |= CM_FILELOCK_FLAG_DELETED;
5407         cm_ReleaseUser(oldFileLock->userp);
5408         oldFileLock->userp = NULL;
5409         if (oldFileLock->scp) {
5410             cm_ReleaseSCacheNoLock(oldFileLock->scp);
5411             oldFileLock->scp = NULL;
5412         }
5413     }
5414     lock_ReleaseWrite(&cm_scacheLock);
5415
5416     return code;
5417 }
5418
5419 cm_key_t cm_GenerateKey(unsigned int session_id, unsigned long process_id, unsigned int file_id)
5420 {
5421 #ifdef DEBUG
5422     osi_assertx((process_id & 0xffffffff) == process_id, "unexpected process_id");
5423     osi_assertx((session_id & 0xffff) == session_id, "unexpected session_id");
5424     osi_assertx((file_id & 0xffff) == file_id, "unexpected file_id");
5425 #endif
5426
5427     return 
5428         (((cm_key_t) (process_id & 0xffffffff)) << 32) |
5429         (((cm_key_t) (session_id & 0xffff)) << 16) |
5430         (((cm_key_t) (file_id & 0xffff)));
5431 }
5432
5433 static int cm_KeyEquals(cm_key_t k1, cm_key_t k2, int flags)
5434 {
5435     if (flags & CM_UNLOCK_BY_FID) {
5436         return ((k1 & 0xffffffff) == (k2 & 0xffffffff));
5437     } else {
5438         return (k1 == k2);
5439     }
5440 }
5441
5442 void cm_ReleaseAllLocks(void)
5443 {
5444     cm_scache_t *scp;
5445     cm_req_t req;
5446     cm_user_t *userp;
5447     cm_key_t   key;
5448     cm_file_lock_t *fileLock;
5449     unsigned int i;
5450
5451     for (i = 0; i < cm_data.scacheHashTableSize; i++)
5452     {
5453         for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
5454             while (scp->fileLocksH != NULL) {
5455                 lock_ObtainMutex(&scp->mx);
5456                 lock_ObtainWrite(&cm_scacheLock);
5457                 if (!scp->fileLocksH) {
5458                     lock_ReleaseWrite(&cm_scacheLock);
5459                     lock_ReleaseMutex(&scp->mx);
5460                     break;
5461                 }
5462                 fileLock = (cm_file_lock_t *)((char *) scp->fileLocksH - offsetof(cm_file_lock_t, fileq));
5463                 userp = fileLock->userp;
5464                 cm_HoldUser(userp);
5465                 key = fileLock->key;
5466                 cm_HoldSCacheNoLock(scp);
5467                 lock_ReleaseWrite(&cm_scacheLock);
5468                 cm_UnlockByKey(scp, key, 0, userp, &req);
5469                 cm_ReleaseSCache(scp);
5470                 cm_ReleaseUser(userp);
5471                 lock_ReleaseMutex(&scp->mx);
5472             }
5473         }
5474     }
5475 }