2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afs/param.h>
29 extern void afsi_log(char *pattern, ...);
32 int cm_enableServerLocks = 1;
35 * Case-folding array. This was constructed by inspecting of SMBtrace output.
36 * I do not know anything more about it.
38 unsigned char cm_foldUpper[256] = {
39 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
40 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
41 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
42 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
43 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
44 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
45 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
46 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
47 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
48 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
49 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
50 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
51 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
52 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
53 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
54 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
55 0x80, 0x9a, 0x90, 0x41, 0x8e, 0x41, 0x8f, 0x80,
56 0x45, 0x45, 0x45, 0x49, 0x49, 0x49, 0x8e, 0x8f,
57 0x90, 0x92, 0x92, 0x4f, 0x99, 0x4f, 0x55, 0x55,
58 0x59, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
59 0x41, 0x49, 0x4f, 0x55, 0xa5, 0xa5, 0x56, 0xa7,
60 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
61 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
62 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
63 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
64 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
65 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
66 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
67 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
68 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
69 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
70 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
74 * Case-insensitive string comparison. We used to use stricmp, but it doesn't
75 * know about 8-bit characters (e.g. 129 is lowercase u-umlaut, 154 is
76 * upper-case u-umlaut).
78 int cm_stricmp(const char *str1, const char *str2)
90 c1 = (char) cm_foldUpper[(unsigned char)(*str1++)];
91 c2 = (char) cm_foldUpper[(unsigned char)(*str2++)];
99 /* characters that are legal in an 8.3 name */
101 * We used to have 1's for all characters from 128 to 254. But
102 * the NT client behaves better if we create an 8.3 name for any
103 * name that has a character with the high bit on, and if we
104 * delete those characters from 8.3 names. In particular, see
105 * Sybase defect 10859.
107 char cm_LegalChars[256] = {
108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,
111 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
112 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
113 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
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, 1, 0, 1, 1, 1,
116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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
126 /* return true iff component is a valid 8.3 name */
127 int cm_Is8Dot3(char *namep)
134 * can't have a leading dot;
135 * special case for . and ..
137 if (namep[0] == '.') {
140 if (namep[1] == '.' && namep[2] == 0)
144 while (tc = *namep++) {
146 /* saw another dot */
147 if (sawDot) return 0; /* second dot */
152 if (cm_LegalChars[tc] == 0)
155 if (!sawDot && charCount > 8)
156 /* more than 8 chars in name */
158 if (sawDot && charCount > 3)
159 /* more than 3 chars in extension */
166 * Number unparsing map for generating 8.3 names;
167 * The version taken from DFS was on drugs.
168 * You can't include '&' and '@' in a file name.
170 char cm_8Dot3Mapping[42] =
171 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
172 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
173 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
174 'V', 'W', 'X', 'Y', 'Z', '_', '-', '$', '#', '!', '+', '='
176 int cm_8Dot3MapSize = sizeof(cm_8Dot3Mapping);
178 void cm_Gen8Dot3NameInt(const char * longname, cm_dirFid_t * pfid,
179 char *shortName, char **shortNameEndp)
183 int vnode = ntohl(pfid->vnode);
185 int validExtension = 0;
189 /* Unparse the file's vnode number to get a "uniquifier" */
191 number[nsize] = cm_8Dot3Mapping[vnode % cm_8Dot3MapSize];
193 vnode /= cm_8Dot3MapSize;
197 * Look for valid extension. There has to be a dot, and
198 * at least one of the characters following has to be legal.
200 lastDot = strrchr(longname, '.');
202 temp = lastDot; temp++;
204 if (cm_LegalChars[tc])
210 /* Copy name characters */
211 for (i = 0, name = longname;
212 i < (7 - nsize) && name != lastDot; ) {
217 if (!cm_LegalChars[tc])
220 *shortName++ = toupper(tc);
226 /* Copy uniquifier characters */
227 memcpy(shortName, number, nsize);
230 if (validExtension) {
231 /* Copy extension characters */
232 *shortName++ = *lastDot++; /* copy dot */
233 for (i = 0, tc = *lastDot++;
236 if (cm_LegalChars[tc]) {
238 *shortName++ = toupper(tc);
247 *shortNameEndp = shortName;
250 /* return success if we can open this file in this mode */
251 long cm_CheckOpen(cm_scache_t *scp, int openMode, int trunc, cm_user_t *userp,
259 rights |= PRSFS_READ;
260 if (openMode == 1 || openMode == 2 || trunc)
261 rights |= PRSFS_WRITE;
263 lock_ObtainMutex(&scp->mx);
265 code = cm_SyncOp(scp, NULL, userp, reqp, rights,
266 CM_SCACHESYNC_GETSTATUS
267 | CM_SCACHESYNC_NEEDCALLBACK
268 | CM_SCACHESYNC_LOCK);
271 ((rights & PRSFS_WRITE) || (rights & PRSFS_READ)) &&
272 scp->fileType == CM_SCACHETYPE_FILE) {
275 unsigned int sLockType;
276 LARGE_INTEGER LOffset, LLength;
278 /* Check if there's some sort of lock on the file at the
281 key = cm_GenerateKey(CM_SESSION_CMINT,0,0);
283 if (rights & PRSFS_WRITE)
286 sLockType = LOCKING_ANDX_SHARED_LOCK;
288 LOffset.HighPart = CM_FLSHARE_OFFSET_HIGH;
289 LOffset.LowPart = CM_FLSHARE_OFFSET_LOW;
290 LLength.HighPart = CM_FLSHARE_LENGTH_HIGH;
291 LLength.LowPart = CM_FLSHARE_LENGTH_LOW;
293 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp, NULL);
296 cm_Unlock(scp, sLockType, LOffset, LLength, key, userp, reqp);
298 /* In this case, we allow the file open to go through even
299 though we can't enforce mandatory locking on the
301 if (code == CM_ERROR_NOACCESS &&
302 !(rights & PRSFS_WRITE))
306 case CM_ERROR_ALLOFFLINE:
307 case CM_ERROR_ALLDOWN:
308 case CM_ERROR_ALLBUSY:
309 case CM_ERROR_TIMEDOUT:
311 case CM_ERROR_WOULDBLOCK:
314 code = CM_ERROR_SHARING_VIOLATION;
319 } else if (code != 0) {
323 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
327 lock_ReleaseMutex(&scp->mx);
332 /* return success if we can open this file in this mode */
333 long cm_CheckNTOpen(cm_scache_t *scp, unsigned int desiredAccess,
334 unsigned int createDisp, cm_user_t *userp, cm_req_t *reqp,
335 cm_lock_data_t **ldpp)
340 osi_assertx(ldpp != NULL, "null cm_lock_data_t");
343 /* Always allow delete; the RPC will tell us if it's OK */
344 if (desiredAccess == DELETE)
349 if (desiredAccess & AFS_ACCESS_READ)
350 rights |= (scp->fileType == CM_SCACHETYPE_DIRECTORY ? PRSFS_LOOKUP : PRSFS_READ);
352 /* We used to require PRSFS_WRITE if createDisp was 4
353 (OPEN_ALWAYS) even if AFS_ACCESS_WRITE was not requested.
354 However, we don't need to do that since the existence of the
355 scp implies that we don't need to create it. */
356 if (desiredAccess & AFS_ACCESS_WRITE)
357 rights |= PRSFS_WRITE;
359 lock_ObtainMutex(&scp->mx);
361 code = cm_SyncOp(scp, NULL, userp, reqp, rights,
362 CM_SCACHESYNC_GETSTATUS
363 | CM_SCACHESYNC_NEEDCALLBACK
364 | CM_SCACHESYNC_LOCK);
367 * If the open will fail because the volume is readonly, then we will
368 * return an access denied error instead. This is to help brain-dead
369 * apps run correctly on replicated volumes.
370 * See defect 10007 for more information.
372 if (code == CM_ERROR_READONLY)
373 code = CM_ERROR_NOACCESS;
376 ((rights & PRSFS_WRITE) || (rights & PRSFS_READ)) &&
377 scp->fileType == CM_SCACHETYPE_FILE) {
379 unsigned int sLockType;
380 LARGE_INTEGER LOffset, LLength;
382 /* Check if there's some sort of lock on the file at the
385 key = cm_GenerateKey(CM_SESSION_CMINT,0,0);
386 if (rights & PRSFS_WRITE)
389 sLockType = LOCKING_ANDX_SHARED_LOCK;
391 /* single byte lock at offset 0x0100 0000 0000 0000 */
392 LOffset.HighPart = CM_FLSHARE_OFFSET_HIGH;
393 LOffset.LowPart = CM_FLSHARE_OFFSET_LOW;
394 LLength.HighPart = CM_FLSHARE_LENGTH_HIGH;
395 LLength.LowPart = CM_FLSHARE_LENGTH_LOW;
397 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp, NULL);
400 (*ldpp) = (cm_lock_data_t *)malloc(sizeof(cm_lock_data_t));
407 (*ldpp)->sLockType = sLockType;
408 (*ldpp)->LOffset.HighPart = LOffset.HighPart;
409 (*ldpp)->LOffset.LowPart = LOffset.LowPart;
410 (*ldpp)->LLength.HighPart = LLength.HighPart;
411 (*ldpp)->LLength.LowPart = LLength.LowPart;
413 /* In this case, we allow the file open to go through even
414 though we can't enforce mandatory locking on the
416 if (code == CM_ERROR_NOACCESS &&
417 !(rights & PRSFS_WRITE))
421 case CM_ERROR_ALLOFFLINE:
422 case CM_ERROR_ALLDOWN:
423 case CM_ERROR_ALLBUSY:
424 case CM_ERROR_TIMEDOUT:
426 case CM_ERROR_WOULDBLOCK:
429 code = CM_ERROR_SHARING_VIOLATION;
433 } else if (code != 0) {
437 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
440 lock_ReleaseMutex(&scp->mx);
445 extern long cm_CheckNTOpenDone(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
446 cm_lock_data_t ** ldpp)
449 lock_ObtainMutex(&scp->mx);
450 cm_Unlock(scp, (*ldpp)->sLockType, (*ldpp)->LOffset, (*ldpp)->LLength,
451 (*ldpp)->key, userp, reqp);
452 lock_ReleaseMutex(&scp->mx);
459 * When CAP_NT_SMBS has been negotiated, deletion (of files or directories) is
460 * done in three steps:
461 * (1) open for deletion (NT_CREATE_AND_X)
462 * (2) set for deletion on close (NT_TRANSACTION2, SET_FILE_INFO)
464 * We must not do the RPC until step 3. But if we are going to return an error
465 * code (e.g. directory not empty), we must return it by step 2, otherwise most
466 * clients will not notice it. So we do a preliminary check. For deleting
467 * files, this is almost free, since we have already done the RPC to get the
468 * parent directory's status bits. But for deleting directories, we must do an
469 * additional RPC to get the directory's data to check if it is empty. Sigh.
471 long cm_CheckNTDelete(cm_scache_t *dscp, cm_scache_t *scp, cm_user_t *userp,
478 unsigned short *hashTable;
480 int BeyondPage = 0, HaveDot = 0, HaveDotDot = 0;
482 /* First check permissions */
483 lock_ObtainMutex(&scp->mx);
484 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_DELETE,
485 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
486 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
487 lock_ReleaseMutex(&scp->mx);
491 /* If deleting directory, must be empty */
493 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
496 thyper.HighPart = 0; thyper.LowPart = 0;
497 lock_ObtainRead(&scp->bufCreateLock);
498 code = buf_Get(scp, &thyper, &bufferp);
499 lock_ReleaseRead(&scp->bufCreateLock);
503 lock_ObtainMutex(&bufferp->mx);
504 lock_ObtainMutex(&scp->mx);
506 code = cm_SyncOp(scp, bufferp, userp, reqp, 0,
507 CM_SCACHESYNC_NEEDCALLBACK
509 | CM_SCACHESYNC_BUFLOCKED);
513 if (cm_HaveBuffer(scp, bufferp, 1))
516 /* otherwise, load the buffer and try again */
517 lock_ReleaseMutex(&bufferp->mx);
518 code = cm_GetBuffer(scp, bufferp, NULL, userp, reqp);
519 lock_ReleaseMutex(&scp->mx);
520 lock_ObtainMutex(&bufferp->mx);
521 lock_ObtainMutex(&scp->mx);
522 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
527 /* We try to determine emptiness without looking beyond the first page,
528 * and without assuming "." and ".." are present and are on the first
529 * page (though these assumptions might, after all, be reasonable).
531 hashTable = (unsigned short *)(bufferp->datap + (32 * 5));
532 for (i=0; i<128; i++) {
533 idx = ntohs(hashTable[i]);
539 dep = (cm_dirEntry_t *)(bufferp->datap + (32 * idx));
540 if (strcmp(dep->name, ".") == 0)
542 else if (strcmp(dep->name, "..") == 0)
545 code = CM_ERROR_NOTEMPTY;
548 idx = ntohs(dep->next);
551 if (BeyondPage && HaveDot && HaveDotDot)
552 code = CM_ERROR_NOTEMPTY;
556 lock_ReleaseMutex(&bufferp->mx);
557 buf_Release(bufferp);
558 lock_ReleaseMutex(&scp->mx);
563 * Iterate through all entries in a directory.
564 * When the function funcp is called, the buffer is locked but the
565 * directory vnode is not.
567 * If the retscp parameter is not NULL, the parmp must be a
568 * cm_lookupSearch_t object.
570 long cm_ApplyDir(cm_scache_t *scp, cm_DirFuncp_t funcp, void *parmp,
571 osi_hyper_t *startOffsetp, cm_user_t *userp, cm_req_t *reqp,
572 cm_scache_t **retscp)
579 osi_hyper_t dirLength;
580 osi_hyper_t bufferOffset;
581 osi_hyper_t curOffset;
585 cm_pageHeader_t *pageHeaderp;
587 long nextEntryCookie;
588 int numDirChunks; /* # of 32 byte dir chunks in this entry */
590 /* get the directory size */
591 lock_ObtainMutex(&scp->mx);
592 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_LOOKUP,
593 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
594 lock_ReleaseMutex(&scp->mx);
598 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
599 return CM_ERROR_NOTDIR;
601 if (retscp) /* if this is a lookup call */
603 cm_lookupSearch_t* sp = parmp;
606 #ifdef AFS_FREELANCE_CLIENT
607 /* Freelance entries never end up in the DNLC because they
608 * do not have an associated cm_server_t
610 !(cm_freelanceEnabled &&
611 sp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
612 sp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
613 #else /* !AFS_FREELANCE_CLIENT */
618 int casefold = sp->caseFold;
619 sp->caseFold = 0; /* we have a strong preference for exact matches */
620 if ( *retscp = cm_dnlcLookup(scp, sp)) /* dnlc hit */
622 sp->caseFold = casefold;
625 sp->caseFold = casefold;
627 /* see if we can find it using the directory hash tables.
628 we can only do exact matches, since the hash is case
638 code = cm_BeginDirOp(scp, userp, reqp, CM_DIRLOCK_READ, &dirop);
642 code = cm_BPlusDirLookup(&dirop, sp->searchNamep, &sp->fid);
647 code = cm_DirLookup(&dirop, sp->searchNamep, &sp->fid);
655 sp->ExactFound = TRUE;
656 *retscp = NULL; /* force caller to call cm_GetSCache() */
661 if (sp->caseFold && code == CM_ERROR_INEXACT_MATCH) {
664 sp->ExactFound = FALSE;
665 *retscp = NULL; /* force caller to call cm_GetSCache() */
669 return CM_ERROR_BPLUS_NOMATCH;
677 * XXX We only get the length once. It might change when we drop the
680 dirLength = scp->length;
683 bufferOffset.LowPart = bufferOffset.HighPart = 0;
685 curOffset = *startOffsetp;
687 curOffset.HighPart = 0;
688 curOffset.LowPart = 0;
692 /* make sure that curOffset.LowPart doesn't point to the first
693 * 32 bytes in the 2nd through last dir page, and that it
694 * doesn't point at the first 13 32-byte chunks in the first
695 * dir page, since those are dir and page headers, and don't
696 * contain useful information.
698 temp = curOffset.LowPart & (2048-1);
699 if (curOffset.HighPart == 0 && curOffset.LowPart < 2048) {
700 /* we're in the first page */
701 if (temp < 13*32) temp = 13*32;
704 /* we're in a later dir page */
705 if (temp < 32) temp = 32;
708 /* make sure the low order 5 bits are zero */
711 /* now put temp bits back ito curOffset.LowPart */
712 curOffset.LowPart &= ~(2048-1);
713 curOffset.LowPart |= temp;
715 /* check if we've passed the dir's EOF */
716 if (LargeIntegerGreaterThanOrEqualTo(curOffset, dirLength))
719 /* see if we can use the bufferp we have now; compute in which
720 * page the current offset would be, and check whether that's
721 * the offset of the buffer we have. If not, get the buffer.
723 thyper.HighPart = curOffset.HighPart;
724 thyper.LowPart = curOffset.LowPart & ~(cm_data.buf_blockSize-1);
725 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
728 lock_ReleaseMutex(&bufferp->mx);
729 buf_Release(bufferp);
733 lock_ObtainRead(&scp->bufCreateLock);
734 code = buf_Get(scp, &thyper, &bufferp);
735 lock_ReleaseRead(&scp->bufCreateLock);
737 /* if buf_Get() fails we do not have a buffer object to lock */
743 /* for the IFS version, we bulkstat the dirents because this
744 routine is used in place of smb_ReceiveCoreSearchDir. our
745 other option is to modify smb_ReceiveCoreSearchDir itself,
746 but this seems to be the proper use for cm_ApplyDir. */
747 lock_ObtainMutex(&scp->mx);
748 if ((scp->flags & CM_SCACHEFLAG_BULKSTATTING) == 0
749 && (scp->bulkStatProgress.QuadPart <= thyper.QuadPart))
751 scp->flags |= CM_SCACHEFLAG_BULKSTATTING;
752 code = cm_TryBulkStat(scp, &thyper, userp, reqp);
753 scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
754 scp->bulkStatProgress = thyper;
756 lock_ReleaseMutex(&scp->mx);
759 lock_ObtainMutex(&bufferp->mx);
760 bufferOffset = thyper;
762 /* now get the data in the cache */
764 lock_ObtainMutex(&scp->mx);
765 code = cm_SyncOp(scp, bufferp, userp, reqp,
767 CM_SCACHESYNC_NEEDCALLBACK
769 | CM_SCACHESYNC_BUFLOCKED);
771 lock_ReleaseMutex(&scp->mx);
774 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
776 if (cm_HaveBuffer(scp, bufferp, 1)) {
777 lock_ReleaseMutex(&scp->mx);
781 /* otherwise, load the buffer and try again */
782 lock_ReleaseMutex(&bufferp->mx);
783 code = cm_GetBuffer(scp, bufferp, NULL, userp,
785 lock_ReleaseMutex(&scp->mx);
786 lock_ObtainMutex(&bufferp->mx);
791 lock_ReleaseMutex(&bufferp->mx);
792 buf_Release(bufferp);
796 } /* if (wrong buffer) ... */
798 /* now we have the buffer containing the entry we're interested
799 * in; copy it out if it represents a non-deleted entry.
801 entryInDir = curOffset.LowPart & (2048-1);
802 entryInBuffer = curOffset.LowPart & (cm_data.buf_blockSize - 1);
804 /* page header will help tell us which entries are free. Page
805 * header can change more often than once per buffer, since
806 * AFS 3 dir page size may be less than (but not more than) a
807 * buffer package buffer.
809 /* only look intra-buffer */
810 temp = curOffset.LowPart & (cm_data.buf_blockSize - 1);
811 temp &= ~(2048 - 1); /* turn off intra-page bits */
812 pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
814 /* now determine which entry we're looking at in the page. If
815 * it is free (there's a free bitmap at the start of the dir),
816 * we should skip these 32 bytes.
818 slotInPage = (entryInDir & 0x7e0) >> 5;
819 if (!(pageHeaderp->freeBitmap[slotInPage>>3]
820 & (1 << (slotInPage & 0x7)))) {
821 /* this entry is free */
822 numDirChunks = 1; /* only skip this guy */
826 tp = bufferp->datap + entryInBuffer;
827 dep = (cm_dirEntry_t *) tp; /* now points to AFS3 dir entry */
829 /* while we're here, compute the next entry's location, too,
830 * since we'll need it when writing out the cookie into the
831 * dir listing stream.
833 numDirChunks = cm_NameEntries(dep->name, NULL);
835 /* compute the offset of the cookie representing the next entry */
836 nextEntryCookie = curOffset.LowPart
837 + (CM_DIR_CHUNKSIZE * numDirChunks);
839 if (dep->fid.vnode != 0) {
840 /* this is one of the entries to use: it is not deleted */
841 code = (*funcp)(scp, dep, parmp, &curOffset);
844 } /* if we're including this name */
847 /* and adjust curOffset to be where the new cookie is */
849 thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
850 curOffset = LargeIntegerAdd(thyper, curOffset);
851 } /* while copying data for dir listing */
853 /* release the mutex */
855 lock_ReleaseMutex(&bufferp->mx);
856 buf_Release(bufferp);
861 int cm_NoneUpper(char *s)
865 if (c >= 'A' && c <= 'Z')
870 int cm_NoneLower(char *s)
874 if (c >= 'a' && c <= 'z')
879 long cm_LookupSearchProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
882 cm_lookupSearch_t *sp;
887 sp = (cm_lookupSearch_t *) rockp;
889 matchName = dep->name;
891 match = cm_stricmp(matchName, sp->searchNamep);
893 match = strcmp(matchName, sp->searchNamep);
897 && !cm_Is8Dot3(dep->name)) {
898 matchName = shortName;
899 cm_Gen8Dot3Name(dep, shortName, NULL);
901 match = cm_stricmp(matchName, sp->searchNamep);
903 match = strcmp(matchName, sp->searchNamep);
913 if (!sp->caseFold || matchName == shortName) {
914 sp->fid.vnode = ntohl(dep->fid.vnode);
915 sp->fid.unique = ntohl(dep->fid.unique);
916 return CM_ERROR_STOPNOW;
920 * If we get here, we are doing a case-insensitive search, and we
921 * have found a match. Now we determine what kind of match it is:
922 * exact, lower-case, upper-case, or none of the above. This is done
923 * in order to choose among matches, if there are more than one.
926 /* Exact matches are the best. */
927 match = strcmp(matchName, sp->searchNamep);
930 sp->fid.vnode = ntohl(dep->fid.vnode);
931 sp->fid.unique = ntohl(dep->fid.unique);
932 return CM_ERROR_STOPNOW;
935 /* Lower-case matches are next. */
938 if (cm_NoneUpper(matchName)) {
943 /* Upper-case matches are next. */
946 if (cm_NoneLower(matchName)) {
951 /* General matches are last. */
957 sp->fid.vnode = ntohl(dep->fid.vnode);
958 sp->fid.unique = ntohl(dep->fid.unique);
962 /* read the contents of a mount point into the appropriate string.
963 * called with locked scp, and returns with locked scp.
965 long cm_ReadMountPoint(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
972 if (scp->mountPointStringp[0])
975 /* otherwise, we have to read it in */
976 lock_ReleaseMutex(&scp->mx);
978 lock_ObtainRead(&scp->bufCreateLock);
979 thyper.LowPart = thyper.HighPart = 0;
980 code = buf_Get(scp, &thyper, &bufp);
981 lock_ReleaseRead(&scp->bufCreateLock);
983 lock_ObtainMutex(&scp->mx);
988 code = cm_SyncOp(scp, bufp, userp, reqp, 0,
989 CM_SCACHESYNC_READ | CM_SCACHESYNC_NEEDCALLBACK);
993 cm_SyncOpDone(scp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
995 if (cm_HaveBuffer(scp, bufp, 0))
998 /* otherwise load buffer */
999 code = cm_GetBuffer(scp, bufp, NULL, userp, reqp);
1003 /* locked, has callback, has valid data in buffer */
1004 if ((tlen = scp->length.LowPart) > MOUNTPOINTLEN - 1)
1005 return CM_ERROR_TOOBIG;
1007 code = CM_ERROR_INVAL;
1011 /* someone else did the work while we were out */
1012 if (scp->mountPointStringp[0]) {
1017 /* otherwise, copy out the link */
1018 memcpy(scp->mountPointStringp, bufp->datap, tlen);
1020 /* now make it null-terminated. Note that the original contents of a
1021 * link that is a mount point is "#volname." where "." is there just to
1022 * be turned into a null. That is, we can trash the last char of the
1023 * link without damaging the vol name. This is a stupid convention,
1024 * but that's the protocol.
1026 scp->mountPointStringp[tlen-1] = 0;
1036 /* called with a locked scp and chases the mount point, yielding outScpp.
1037 * scp remains locked, just for simplicity of describing the interface.
1039 long cm_FollowMountPoint(cm_scache_t *scp, cm_scache_t *dscp, cm_user_t *userp,
1040 cm_req_t *reqp, cm_scache_t **outScpp)
1048 cm_volume_t *volp = NULL;
1055 if (scp->mountRootFid.cell != 0 && scp->mountRootGen >= cm_data.mountRootGen) {
1056 tfid = scp->mountRootFid;
1057 lock_ReleaseMutex(&scp->mx);
1058 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1059 lock_ObtainMutex(&scp->mx);
1063 /* parse the volume name */
1064 mpNamep = scp->mountPointStringp;
1066 return CM_ERROR_NOSUCHPATH;
1067 tlen = (int)strlen(scp->mountPointStringp);
1068 mtType = *scp->mountPointStringp;
1069 cellNamep = malloc(tlen);
1070 volNamep = malloc(tlen);
1072 cp = strrchr(mpNamep, ':');
1074 /* cellular mount point */
1075 memset(cellNamep, 0, tlen);
1076 strncpy(cellNamep, mpNamep+1, cp - mpNamep - 1);
1077 strcpy(volNamep, cp+1);
1078 /* now look up the cell */
1079 lock_ReleaseMutex(&scp->mx);
1080 cellp = cm_GetCell(cellNamep, CM_FLAG_CREATE);
1081 lock_ObtainMutex(&scp->mx);
1085 strcpy(volNamep, mpNamep+1);
1087 cellp = cm_FindCellByID(scp->fid.cell);
1091 code = CM_ERROR_NOSUCHCELL;
1095 vnLength = strlen(volNamep);
1096 if (vnLength >= 8 && strcmp(volNamep + vnLength - 7, ".backup") == 0)
1098 else if (vnLength >= 10
1099 && strcmp(volNamep + vnLength - 9, ".readonly") == 0)
1104 /* check for backups within backups */
1106 && (scp->flags & (CM_SCACHEFLAG_RO | CM_SCACHEFLAG_PURERO))
1107 == CM_SCACHEFLAG_RO) {
1108 code = CM_ERROR_NOSUCHVOLUME;
1112 /* now we need to get the volume */
1113 lock_ReleaseMutex(&scp->mx);
1114 if (cm_VolNameIsID(volNamep)) {
1115 code = cm_GetVolumeByID(cellp, atoi(volNamep), userp, reqp,
1116 CM_GETVOL_FLAG_CREATE, &volp);
1118 code = cm_GetVolumeByName(cellp, volNamep, userp, reqp,
1119 CM_GETVOL_FLAG_CREATE, &volp);
1121 lock_ObtainMutex(&scp->mx);
1124 /* save the parent of the volume root for this is the
1125 * place where the volume is mounted and we must remember
1126 * this in the volume structure rather than just in the
1127 * scache entry lest the scache entry gets recycled
1130 lock_ObtainMutex(&volp->mx);
1131 volp->dotdotFid = dscp->fid;
1132 lock_ReleaseMutex(&volp->mx);
1134 scp->mountRootFid.cell = cellp->cellID;
1135 /* if the mt pt is in a read-only volume (not just a
1136 * backup), and if there is a read-only volume for the
1137 * target, and if this is a type '#' mount point, use
1138 * the read-only, otherwise use the one specified.
1140 if (mtType == '#' && (scp->flags & CM_SCACHEFLAG_PURERO)
1141 && volp->ro.ID != 0 && type == RWVOL)
1144 scp->mountRootFid.volume = volp->ro.ID;
1145 else if (type == BACKVOL)
1146 scp->mountRootFid.volume = volp->bk.ID;
1148 scp->mountRootFid.volume = volp->rw.ID;
1150 /* the rest of the fid is a magic number */
1151 scp->mountRootFid.vnode = 1;
1152 scp->mountRootFid.unique = 1;
1153 scp->mountRootGen = cm_data.mountRootGen;
1155 tfid = scp->mountRootFid;
1156 lock_ReleaseMutex(&scp->mx);
1157 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1158 lock_ObtainMutex(&scp->mx);
1169 long cm_LookupInternal(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1170 cm_req_t *reqp, cm_scache_t **outpScpp)
1173 int dnlcHit = 1; /* did we hit in the dnlc? yes, we did */
1174 cm_scache_t *tscp = NULL;
1175 cm_scache_t *mountedScp;
1176 cm_lookupSearch_t rock;
1179 memset(&rock, 0, sizeof(rock));
1181 if (dscp->fid.vnode == 1 && dscp->fid.unique == 1
1182 && strcmp(namep, "..") == 0) {
1183 if (dscp->dotdotFid.volume == 0)
1184 return CM_ERROR_NOSUCHVOLUME;
1185 rock.fid = dscp->dotdotFid;
1187 } else if (strcmp(namep, ".") == 0) {
1188 rock.fid = dscp->fid;
1192 if (flags & CM_FLAG_NOMOUNTCHASE) {
1193 /* In this case, we should go and call cm_Dir* functions
1194 directly since the following cm_ApplyDir() function will
1202 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ, &dirop);
1205 code = cm_BPlusDirLookup(&dirop, namep, &rock.fid);
1210 code = cm_DirLookup(&dirop, namep, &rock.fid);
1212 cm_EndDirOp(&dirop);
1222 if (code == CM_ERROR_INEXACT_MATCH && (flags & CM_FLAG_CASEFOLD)) {
1229 return CM_ERROR_BPLUS_NOMATCH;
1234 rock.fid.cell = dscp->fid.cell;
1235 rock.fid.volume = dscp->fid.volume;
1236 rock.searchNamep = namep;
1237 rock.caseFold = (flags & CM_FLAG_CASEFOLD);
1238 rock.hasTilde = ((strchr(namep, '~') != NULL) ? 1 : 0);
1240 /* If NOMOUNTCHASE, bypass DNLC by passing NULL scp pointer */
1241 code = cm_ApplyDir(dscp, cm_LookupSearchProc, &rock, NULL, userp, reqp,
1242 (flags & CM_FLAG_NOMOUNTCHASE) ? NULL : &tscp);
1244 /* code == 0 means we fell off the end of the dir, while stopnow means
1245 * that we stopped early, probably because we found the entry we're
1246 * looking for. Any other non-zero code is an error.
1248 if (code && code != CM_ERROR_STOPNOW) {
1249 /* if the cm_scache_t we are searching in is not a directory
1250 * we must return path not found because the error
1251 * is to describe the final component not an intermediary
1253 if (code == CM_ERROR_NOTDIR) {
1254 if (flags & CM_FLAG_CHECKPATH)
1255 return CM_ERROR_NOSUCHPATH;
1257 return CM_ERROR_NOSUCHFILE;
1262 getroot = (dscp==cm_data.rootSCachep) ;
1264 if (!cm_freelanceEnabled || !getroot) {
1265 if (flags & CM_FLAG_CHECKPATH)
1266 return CM_ERROR_NOSUCHPATH;
1268 return CM_ERROR_NOSUCHFILE;
1270 else { /* nonexistent dir on freelance root, so add it */
1271 char fullname[200] = ".";
1274 osi_Log1(afsd_logp,"cm_Lookup adding mount for non-existent directory: %s",
1275 osi_LogSaveString(afsd_logp,namep));
1276 if (namep[0] == '.') {
1277 if (cm_GetCell_Gen(&namep[1], &fullname[1], CM_FLAG_CREATE)) {
1279 if ( stricmp(&namep[1], &fullname[1]) )
1280 code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1282 code = cm_FreelanceAddMount(namep, &fullname[1], "root.cell.", 1, &rock.fid);
1285 if (cm_GetCell_Gen(namep, fullname, CM_FLAG_CREATE)) {
1287 if ( stricmp(namep, fullname) )
1288 code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1290 code = cm_FreelanceAddMount(namep, fullname, "root.cell.", 0, &rock.fid);
1293 if (!found || code < 0) { /* add mount point failed, so give up */
1294 if (flags & CM_FLAG_CHECKPATH)
1295 return CM_ERROR_NOSUCHPATH;
1297 return CM_ERROR_NOSUCHFILE;
1299 tscp = NULL; /* to force call of cm_GetSCache */
1304 if ( !tscp ) /* we did not find it in the dnlc */
1307 code = cm_GetSCache(&rock.fid, &tscp, userp, reqp);
1311 /* tscp is now held */
1313 lock_ObtainMutex(&tscp->mx);
1314 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1315 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1317 lock_ReleaseMutex(&tscp->mx);
1318 cm_ReleaseSCache(tscp);
1321 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1322 /* tscp is now locked */
1324 if (!(flags & CM_FLAG_NOMOUNTCHASE)
1325 && tscp->fileType == CM_SCACHETYPE_MOUNTPOINT) {
1326 /* mount points are funny: they have a volume name to mount
1329 code = cm_ReadMountPoint(tscp, userp, reqp);
1331 code = cm_FollowMountPoint(tscp, dscp, userp, reqp,
1333 lock_ReleaseMutex(&tscp->mx);
1334 cm_ReleaseSCache(tscp);
1341 lock_ReleaseMutex(&tscp->mx);
1344 /* copy back pointer */
1347 /* insert scache in dnlc */
1348 if ( !dnlcHit && !(flags & CM_FLAG_NOMOUNTCHASE) && rock.ExactFound ) {
1349 /* lock the directory entry to prevent racing callback revokes */
1350 lock_ObtainMutex(&dscp->mx);
1351 if ( dscp->cbServerp != NULL && dscp->cbExpires > 0 )
1352 cm_dnlcEnter(dscp, namep, tscp);
1353 lock_ReleaseMutex(&dscp->mx);
1360 int cm_ExpandSysName(char *inp, char *outp, long outSize, unsigned int index)
1365 tp = strrchr(inp, '@');
1367 return 0; /* no @sys */
1369 if (strcmp(tp, "@sys") != 0)
1370 return 0; /* no @sys */
1372 /* caller just wants to know if this is a valid @sys type of name */
1376 if (index >= MAXNUMSYSNAMES)
1379 /* otherwise generate the properly expanded @sys name */
1380 prefixCount = (int)(tp - inp);
1382 strncpy(outp, inp, prefixCount); /* copy out "a." from "a.@sys" */
1383 outp[prefixCount] = 0; /* null terminate the "a." */
1384 strcat(outp, cm_sysNameList[index]);/* append i386_nt40 */
1388 long cm_EvaluateVolumeReference(char * namep, long flags, cm_user_t * userp,
1389 cm_req_t *reqp, cm_scache_t ** outpScpp)
1392 char cellName[CELL_MAXNAMELEN];
1393 char volumeName[VL_MAXNAMELEN];
1398 cm_cell_t * cellp = NULL;
1399 cm_volume_t * volp = NULL;
1402 int mountType = RWVOL;
1404 osi_Log1(afsd_logp, "cm_EvaluateVolumeReference for string [%s]",
1405 osi_LogSaveString(afsd_logp, namep));
1407 if (strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) != 0) {
1408 goto _exit_invalid_path;
1411 /* namep is assumed to look like the following:
1413 @vol:<cellname>%<volume>\0
1415 @vol:<cellname>#<volume>\0
1419 cp = namep + CM_PREFIX_VOL_CCH; /* cp points to cell name, hopefully */
1420 tp = strchr(cp, '%');
1422 tp = strchr(cp, '#');
1424 (len = tp - cp) == 0 ||
1425 len > CELL_MAXNAMELEN)
1426 goto _exit_invalid_path;
1427 strncpy(cellName, cp, len);
1428 cellName[len] = '\0';
1433 cp = tp+1; /* cp now points to volume, supposedly */
1434 strncpy(volumeName, cp, VL_MAXNAMELEN-1);
1435 volumeName[VL_MAXNAMELEN - 1] = 0;
1437 /* OK, now we have the cell and the volume */
1438 osi_Log2(afsd_logp, " Found cell [%s] and volume [%s]",
1439 osi_LogSaveString(afsd_logp, cellName),
1440 osi_LogSaveString(afsd_logp, volumeName));
1442 cellp = cm_GetCell(cellName, CM_FLAG_CREATE);
1443 if (cellp == NULL) {
1444 goto _exit_invalid_path;
1447 len = strlen(volumeName);
1448 if (len >= 8 && strcmp(volumeName + len - 7, ".backup") == 0)
1450 else if (len >= 10 &&
1451 strcmp(volumeName + len - 9, ".readonly") == 0)
1456 if (cm_VolNameIsID(volumeName)) {
1457 code = cm_GetVolumeByID(cellp, atoi(volumeName), userp, reqp,
1458 CM_GETVOL_FLAG_CREATE, &volp);
1460 code = cm_GetVolumeByName(cellp, volumeName, userp, reqp,
1461 CM_GETVOL_FLAG_CREATE, &volp);
1467 fid.cell = cellp->cellID;
1469 if (volType == BACKVOL)
1470 fid.volume = volp->bk.ID;
1471 else if (volType == ROVOL ||
1472 (volType == RWVOL && mountType == ROVOL && volp->ro.ID != 0))
1473 fid.volume = volp->ro.ID;
1475 fid.volume = volp->rw.ID;
1480 code = cm_GetSCache(&fid, outpScpp, userp, reqp);
1490 if (flags & CM_FLAG_CHECKPATH)
1491 return CM_ERROR_NOSUCHPATH;
1493 return CM_ERROR_NOSUCHFILE;
1496 #ifdef DEBUG_REFCOUNT
1497 long cm_LookupDbg(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1498 cm_req_t *reqp, cm_scache_t **outpScpp, char * file, long line)
1500 long cm_Lookup(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1501 cm_req_t *reqp, cm_scache_t **outpScpp)
1505 char tname[AFSPATHMAX];
1506 int sysNameIndex = 0;
1507 cm_scache_t *scp = NULL;
1509 #ifdef DEBUG_REFCOUNT
1510 afsi_log("%s:%d cm_Lookup dscp 0x%p ref %d", file, line, dscp, dscp->refCount, file, line);
1511 osi_Log2(afsd_logp, "cm_Lookup dscp 0x%p ref %d", dscp, dscp->refCount);
1514 if ( stricmp(namep,SMB_IOCTL_FILENAME_NOSLASH) == 0 ) {
1515 if (flags & CM_FLAG_CHECKPATH)
1516 return CM_ERROR_NOSUCHPATH;
1518 return CM_ERROR_NOSUCHFILE;
1521 if (dscp == cm_data.rootSCachep &&
1522 strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) == 0) {
1523 return cm_EvaluateVolumeReference(namep, flags, userp, reqp, outpScpp);
1526 if (cm_ExpandSysName(namep, NULL, 0, 0) > 0) {
1527 for ( sysNameIndex = 0; sysNameIndex < MAXNUMSYSNAMES; sysNameIndex++) {
1528 code = cm_ExpandSysName(namep, tname, sizeof(tname), sysNameIndex);
1530 code = cm_LookupInternal(dscp, tname, flags, userp, reqp, &scp);
1531 #ifdef DEBUG_REFCOUNT
1532 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);
1533 osi_Log3(afsd_logp, "cm_LookupInternal (1) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1541 cm_ReleaseSCache(scp);
1545 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1546 #ifdef DEBUG_REFCOUNT
1547 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);
1548 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1555 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1556 #ifdef DEBUG_REFCOUNT
1557 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);
1558 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1564 /* None of the possible sysName expansions could be found */
1565 if (flags & CM_FLAG_CHECKPATH)
1566 return CM_ERROR_NOSUCHPATH;
1568 return CM_ERROR_NOSUCHFILE;
1571 long cm_Unlink(cm_scache_t *dscp, char *namep, cm_user_t *userp, cm_req_t *reqp)
1577 AFSFetchStatus newDirStatus;
1579 struct rx_connection * callp;
1582 #ifdef AFS_FREELANCE_CLIENT
1583 if (cm_freelanceEnabled && dscp == cm_data.rootSCachep) {
1584 /* deleting a mount point from the root dir. */
1585 code = cm_FreelanceRemoveMount(namep);
1590 /* make sure we don't screw up the dir status during the merge */
1591 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
1593 lock_ObtainMutex(&dscp->mx);
1594 sflags = CM_SCACHESYNC_STOREDATA;
1595 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, sflags);
1596 lock_ReleaseMutex(&dscp->mx);
1598 cm_EndDirOp(&dirop);
1603 afsFid.Volume = dscp->fid.volume;
1604 afsFid.Vnode = dscp->fid.vnode;
1605 afsFid.Unique = dscp->fid.unique;
1607 osi_Log1(afsd_logp, "CALL RemoveFile scp 0x%p", dscp);
1609 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
1613 callp = cm_GetRxConn(connp);
1614 code = RXAFS_RemoveFile(callp, &afsFid, namep,
1615 &newDirStatus, &volSync);
1616 rx_PutConnection(callp);
1618 } while (cm_Analyze(connp, userp, reqp, &dscp->fid, &volSync, NULL, NULL, code));
1619 code = cm_MapRPCError(code, reqp);
1622 osi_Log1(afsd_logp, "CALL RemoveFile FAILURE, code 0x%x", code);
1624 osi_Log0(afsd_logp, "CALL RemoveFile SUCCESS");
1627 lock_ObtainWrite(&dirop.scp->dirlock);
1628 dirop.lockType = CM_DIRLOCK_WRITE;
1630 lock_ObtainMutex(&dscp->mx);
1631 cm_dnlcRemove(dscp, namep);
1632 cm_SyncOpDone(dscp, NULL, sflags);
1634 cm_MergeStatus(NULL, dscp, &newDirStatus, &volSync, userp, 0);
1635 } else if (code == CM_ERROR_NOSUCHFILE) {
1636 /* windows would not have allowed the request to delete the file
1637 * if it did not believe the file existed. therefore, we must
1638 * have an inconsistent view of the world.
1640 dscp->cbServerp = NULL;
1642 lock_ReleaseMutex(&dscp->mx);
1644 if (code == 0 && cm_CheckDirOpForSingleChange(&dirop)) {
1645 cm_DirDeleteEntry(&dirop, namep);
1647 cm_BPlusDirDeleteEntry(&dirop, namep);
1650 cm_EndDirOp(&dirop);
1655 /* called with a locked vnode, and fills in the link info.
1656 * returns this the vnode still locked.
1658 long cm_HandleLink(cm_scache_t *linkScp, cm_user_t *userp, cm_req_t *reqp)
1665 lock_AssertMutex(&linkScp->mx);
1666 if (!linkScp->mountPointStringp[0]) {
1667 /* read the link data */
1668 lock_ReleaseMutex(&linkScp->mx);
1669 thyper.LowPart = thyper.HighPart = 0;
1670 code = buf_Get(linkScp, &thyper, &bufp);
1671 lock_ObtainMutex(&linkScp->mx);
1675 code = cm_SyncOp(linkScp, bufp, userp, reqp, 0,
1676 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1681 cm_SyncOpDone(linkScp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1683 if (cm_HaveBuffer(linkScp, bufp, 0))
1686 code = cm_GetBuffer(linkScp, bufp, NULL, userp, reqp);
1691 } /* while loop to get the data */
1693 /* now if we still have no link read in,
1694 * copy the data from the buffer */
1695 if ((temp = linkScp->length.LowPart) >= MOUNTPOINTLEN) {
1697 return CM_ERROR_TOOBIG;
1700 /* otherwise, it fits; make sure it is still null (could have
1701 * lost race with someone else referencing this link above),
1702 * and if so, copy in the data.
1704 if (!linkScp->mountPointStringp[0]) {
1705 strncpy(linkScp->mountPointStringp, bufp->datap, temp);
1706 linkScp->mountPointStringp[temp] = 0; /* null terminate */
1709 } /* don't have sym link contents cached */
1714 /* called with a held vnode and a path suffix, with the held vnode being a
1715 * symbolic link. Our goal is to generate a new path to interpret, and return
1716 * this new path in newSpaceBufferp. If the new vnode is relative to a dir
1717 * other than the directory containing the symbolic link, then the new root is
1718 * returned in *newRootScpp, otherwise a null is returned there.
1720 long cm_AssembleLink(cm_scache_t *linkScp, char *pathSuffixp,
1721 cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1722 cm_user_t *userp, cm_req_t *reqp)
1729 lock_ObtainMutex(&linkScp->mx);
1730 code = cm_HandleLink(linkScp, userp, reqp);
1734 /* if we may overflow the buffer, bail out; buffer is signficantly
1735 * bigger than max path length, so we don't really have to worry about
1736 * being a little conservative here.
1738 if (strlen(linkScp->mountPointStringp) + strlen(pathSuffixp) + 2
1739 >= CM_UTILS_SPACESIZE)
1740 return CM_ERROR_TOOBIG;
1742 tsp = cm_GetSpace();
1743 linkp = linkScp->mountPointStringp;
1744 if (strncmp(linkp, cm_mountRoot, cm_mountRootLen) == 0) {
1745 if (strlen(linkp) > cm_mountRootLen)
1746 strcpy(tsp->data, linkp+cm_mountRootLen+1);
1749 *newRootScpp = cm_data.rootSCachep;
1750 cm_HoldSCache(cm_data.rootSCachep);
1751 } else if (linkp[0] == '\\' && linkp[1] == '\\') {
1752 if (!strnicmp(&linkp[2], cm_NetbiosName, (len = (long)strlen(cm_NetbiosName))))
1754 char * p = &linkp[len + 3];
1755 if (strnicmp(p, "all", 3) == 0)
1758 strcpy(tsp->data, p);
1759 for (p = tsp->data; *p; p++) {
1763 *newRootScpp = cm_data.rootSCachep;
1764 cm_HoldSCache(cm_data.rootSCachep);
1766 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1767 strcpy(tsp->data, linkp);
1768 *newRootScpp = NULL;
1769 code = CM_ERROR_PATH_NOT_COVERED;
1771 } else if ( !strnicmp(linkp, "msdfs:", (len = (long)strlen("msdfs:"))) ) {
1772 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1773 strcpy(tsp->data, linkp);
1774 *newRootScpp = NULL;
1775 code = CM_ERROR_PATH_NOT_COVERED;
1776 } else if (*linkp == '\\' || *linkp == '/') {
1778 /* formerly, this was considered to be from the AFS root,
1779 * but this seems to create problems. instead, we will just
1780 * reject the link */
1781 strcpy(tsp->data, linkp+1);
1782 *newRootScpp = cm_data.rootSCachep;
1783 cm_HoldSCache(cm_data.rootSCachep);
1785 /* we still copy the link data into the response so that
1786 * the user can see what the link points to
1788 linkScp->fileType = CM_SCACHETYPE_INVALID;
1789 strcpy(tsp->data, linkp);
1790 *newRootScpp = NULL;
1791 code = CM_ERROR_NOSUCHPATH;
1794 /* a relative link */
1795 strcpy(tsp->data, linkp);
1796 *newRootScpp = NULL;
1798 if (pathSuffixp[0] != 0) { /* if suffix string is non-null */
1799 strcat(tsp->data, "\\");
1800 strcat(tsp->data, pathSuffixp);
1802 *newSpaceBufferp = tsp;
1805 lock_ReleaseMutex(&linkScp->mx);
1808 #ifdef DEBUG_REFCOUNT
1809 long cm_NameIDbg(cm_scache_t *rootSCachep, char *pathp, long flags,
1810 cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp,
1811 char * file, long line)
1813 long cm_NameI(cm_scache_t *rootSCachep, char *pathp, long flags,
1814 cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp)
1818 char *tp; /* ptr moving through input buffer */
1819 char tc; /* temp char */
1820 int haveComponent; /* has new component started? */
1821 char component[AFSPATHMAX]; /* this is the new component */
1822 char *cp; /* component name being assembled */
1823 cm_scache_t *tscp; /* current location in the hierarchy */
1824 cm_scache_t *nscp; /* next dude down */
1825 cm_scache_t *dirScp; /* last dir we searched */
1826 cm_scache_t *linkScp; /* new root for the symlink we just
1828 cm_space_t *psp; /* space for current path, if we've hit
1830 cm_space_t *tempsp; /* temp vbl */
1831 char *restp; /* rest of the pathname to interpret */
1832 int symlinkCount; /* count of # of symlinks traversed */
1833 int extraFlag; /* avoid chasing mt pts for dir cmd */
1834 int phase = 1; /* 1 = tidPathp, 2 = pathp */
1835 #define MAX_FID_COUNT 512
1836 cm_fid_t fids[MAX_FID_COUNT]; /* array of fids processed in this path walk */
1837 int fid_count = 0; /* number of fids processed in this path walk */
1840 #ifdef DEBUG_REFCOUNT
1841 afsi_log("%s:%d cm_NameI rootscp 0x%p ref %d", file, line, rootSCachep, rootSCachep->refCount);
1842 osi_Log4(afsd_logp,"cm_NameI rootscp 0x%p path %s tidpath %s flags 0x%x",
1843 rootSCachep, pathp ? pathp : "<NULL>", tidPathp ? tidPathp : "<NULL>",
1858 cm_HoldSCache(tscp);
1866 /* map Unix slashes into DOS ones so we can interpret Unix
1872 if (!haveComponent) {
1875 } else if (tc == 0) {
1889 /* we have a component here */
1890 if (tc == 0 || tc == '\\') {
1891 /* end of the component; we're at the last
1892 * component if tc == 0. However, if the last
1893 * is a symlink, we have more to do.
1895 *cp++ = 0; /* add null termination */
1897 if ((flags & CM_FLAG_DIRSEARCH) && tc == 0)
1898 extraFlag = CM_FLAG_NOMOUNTCHASE;
1899 code = cm_Lookup(tscp, component,
1901 userp, reqp, &nscp);
1904 if (!strcmp(component,"..") || !strcmp(component,".")) {
1906 * roll back the fid list until we find the fid
1907 * that matches where we are now. Its not necessarily
1908 * one or two fids because they might have been
1909 * symlinks or mount points or both that were crossed.
1911 for ( i=fid_count-1; i>=0; i--) {
1912 if (!cm_FidCmp(&nscp->fid, &fids[i]))
1916 /* add the new fid to the list */
1917 for ( i=0; i<fid_count; i++) {
1918 if ( !cm_FidCmp(&nscp->fid, &fids[i]) ) {
1919 code = CM_ERROR_TOO_MANY_SYMLINKS;
1920 cm_ReleaseSCache(nscp);
1925 if (i == fid_count && fid_count < MAX_FID_COUNT) {
1926 fids[fid_count++] = nscp->fid;
1932 cm_ReleaseSCache(tscp);
1934 cm_ReleaseSCache(dirScp);
1937 if ((code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) &&
1938 tscp->fileType == CM_SCACHETYPE_SYMLINK)
1940 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_NOSUCHPATH");
1941 return CM_ERROR_NOSUCHPATH;
1943 osi_Log1(afsd_logp,"cm_NameI code 0x%x", code);
1948 haveComponent = 0; /* component done */
1950 cm_ReleaseSCache(dirScp);
1951 dirScp = tscp; /* for some symlinks */
1952 tscp = nscp; /* already held */
1954 if (tc == 0 && !(flags & CM_FLAG_FOLLOW) && phase == 2) {
1957 cm_ReleaseSCache(dirScp);
1963 /* now, if tscp is a symlink, we should follow
1964 * it and assemble the path again.
1966 lock_ObtainMutex(&tscp->mx);
1967 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1968 CM_SCACHESYNC_GETSTATUS
1969 | CM_SCACHESYNC_NEEDCALLBACK);
1971 lock_ReleaseMutex(&tscp->mx);
1972 cm_ReleaseSCache(tscp);
1975 cm_ReleaseSCache(dirScp);
1980 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1982 if (tscp->fileType == CM_SCACHETYPE_SYMLINK) {
1983 /* this is a symlink; assemble a new buffer */
1984 lock_ReleaseMutex(&tscp->mx);
1985 if (symlinkCount++ >= MAX_SYMLINK_COUNT) {
1986 cm_ReleaseSCache(tscp);
1989 cm_ReleaseSCache(dirScp);
1994 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_TOO_MANY_SYMLINKS");
1995 return CM_ERROR_TOO_MANY_SYMLINKS;
2001 code = cm_AssembleLink(tscp, restp, &linkScp, &tempsp, userp, reqp);
2003 if (code == 0 && linkScp != NULL) {
2004 if (linkScp == cm_data.rootSCachep)
2007 for ( i=0; i<fid_count; i++) {
2008 if ( !cm_FidCmp(&linkScp->fid, &fids[i]) ) {
2009 code = CM_ERROR_TOO_MANY_SYMLINKS;
2010 cm_ReleaseSCache(linkScp);
2016 if (i == fid_count && fid_count < MAX_FID_COUNT) {
2017 fids[fid_count++] = linkScp->fid;
2022 /* something went wrong */
2023 cm_ReleaseSCache(tscp);
2026 cm_ReleaseSCache(dirScp);
2032 /* otherwise, tempsp has the new path,
2033 * and linkScp is the new root from
2034 * which to interpret that path.
2035 * Continue with the namei processing,
2036 * also doing the bookkeeping for the
2037 * space allocation and tracking the
2038 * vnode reference counts.
2044 cm_ReleaseSCache(tscp);
2049 * now, if linkScp is null, that's
2050 * AssembleLink's way of telling us that
2051 * the sym link is relative to the dir
2052 * containing the link. We have a ref
2053 * to it in dirScp, and we hold it now
2054 * and reuse it as the new spot in the
2062 /* not a symlink, we may be done */
2063 lock_ReleaseMutex(&tscp->mx);
2071 cm_ReleaseSCache(dirScp);
2079 cm_ReleaseSCache(dirScp);
2082 } /* end of a component */
2085 } /* we have a component */
2086 } /* big while loop over all components */
2090 cm_ReleaseSCache(dirScp);
2096 cm_ReleaseSCache(tscp);
2098 #ifdef DEBUG_REFCOUNT
2099 afsi_log("%s:%d cm_NameI code 0x%x outScpp 0x%p ref %d", file, line, code, *outScpp, (*outScpp)->refCount);
2101 osi_Log2(afsd_logp,"cm_NameI code 0x%x outScpp 0x%p", code, *outScpp);
2105 /* called with a dir, and a vnode within the dir that happens to be a symlink.
2106 * We chase the link, and return a held pointer to the target, if it exists,
2107 * in *outScpp. If we succeed, we return 0, otherwise we return an error code
2108 * and do not hold or return a target vnode.
2110 * This is very similar to calling cm_NameI with the last component of a name,
2111 * which happens to be a symlink, except that we've already passed by the name.
2113 * This function is typically called by the directory listing functions, which
2114 * encounter symlinks but need to return the proper file length so programs
2115 * like "more" work properly when they make use of the attributes retrieved from
2118 * The input vnode should not be locked when this function is called.
2120 long cm_EvaluateSymLink(cm_scache_t *dscp, cm_scache_t *linkScp,
2121 cm_scache_t **outScpp, cm_user_t *userp, cm_req_t *reqp)
2125 cm_scache_t *newRootScp;
2127 osi_Log1(afsd_logp, "Evaluating symlink scp 0x%p", linkScp);
2129 code = cm_AssembleLink(linkScp, "", &newRootScp, &spacep, userp, reqp);
2133 /* now, if newRootScp is NULL, we're really being told that the symlink
2134 * is relative to the current directory (dscp).
2136 if (newRootScp == NULL) {
2138 cm_HoldSCache(dscp);
2141 code = cm_NameI(newRootScp, spacep->data,
2142 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW | CM_FLAG_DIRSEARCH,
2143 userp, NULL, reqp, outScpp);
2145 if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH)
2146 code = CM_ERROR_NOSUCHPATH;
2148 /* this stuff is allocated no matter what happened on the namei call,
2150 cm_FreeSpace(spacep);
2151 cm_ReleaseSCache(newRootScp);
2153 if (linkScp == *outScpp) {
2154 cm_ReleaseSCache(*outScpp);
2156 code = CM_ERROR_NOSUCHPATH;
2162 /* make this big enough so that one buffer of dir pages won't overflow. We'll
2163 * check anyway, but we want to minimize the chance that we have to leave stuff
2166 #define CM_BULKMAX (3 * AFSCBMAX)
2168 /* rock for bulk stat calls */
2169 typedef struct cm_bulkStat {
2170 osi_hyper_t bufOffset; /* only do it for things in this buffer page */
2172 /* info for the actual call */
2173 int counter; /* next free slot */
2174 AFSFid fids[CM_BULKMAX];
2175 AFSFetchStatus stats[CM_BULKMAX];
2176 AFSCallBack callbacks[CM_BULKMAX];
2179 /* for a given entry, make sure that it isn't in the stat cache, and then
2180 * add it to the list of file IDs to be obtained.
2182 * Don't bother adding it if we already have a vnode. Note that the dir
2183 * is locked, so we have to be careful checking the vnode we're thinking of
2184 * processing, to avoid deadlocks.
2186 long cm_TryBulkProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
2197 /* Don't overflow bsp. */
2198 if (bsp->counter >= CM_BULKMAX)
2199 return CM_ERROR_STOPNOW;
2201 thyper.LowPart = cm_data.buf_blockSize;
2202 thyper.HighPart = 0;
2203 thyper = LargeIntegerAdd(thyper, bsp->bufOffset);
2205 /* thyper is now the first byte past the end of the record we're
2206 * interested in, and bsp->bufOffset is the first byte of the record
2207 * we're interested in.
2208 * Skip data in the others.
2211 if (LargeIntegerLessThan(*offp, bsp->bufOffset))
2213 if (LargeIntegerGreaterThanOrEqualTo(*offp, thyper))
2214 return CM_ERROR_STOPNOW;
2215 if (strcmp(dep->name, ".") == 0 || strcmp(dep->name, "..") == 0)
2218 tfid.cell = scp->fid.cell;
2219 tfid.volume = scp->fid.volume;
2220 tfid.vnode = ntohl(dep->fid.vnode);
2221 tfid.unique = ntohl(dep->fid.unique);
2222 tscp = cm_FindSCache(&tfid);
2224 if (lock_TryMutex(&tscp->mx)) {
2225 /* we have an entry that we can look at */
2226 if (!(tscp->flags & CM_SCACHEFLAG_EACCESS) && cm_HaveCallback(tscp)) {
2227 /* we have a callback on it. Don't bother
2228 * fetching this stat entry, since we're happy
2229 * with the info we have.
2231 lock_ReleaseMutex(&tscp->mx);
2232 cm_ReleaseSCache(tscp);
2235 lock_ReleaseMutex(&tscp->mx);
2237 cm_ReleaseSCache(tscp);
2240 #ifdef AFS_FREELANCE_CLIENT
2241 // yj: if this is a mountpoint under root.afs then we don't want it
2242 // to be bulkstat-ed, instead, we call getSCache directly and under
2243 // getSCache, it is handled specially.
2244 if ( cm_freelanceEnabled &&
2245 tfid.cell==AFS_FAKE_ROOT_CELL_ID &&
2246 tfid.volume==AFS_FAKE_ROOT_VOL_ID &&
2247 !(tfid.vnode==0x1 && tfid.unique==0x1) )
2249 osi_Log0(afsd_logp, "cm_TryBulkProc Freelance calls cm_SCache on root.afs mountpoint");
2250 return cm_GetSCache(&tfid, &tscp, NULL, NULL);
2252 #endif /* AFS_FREELANCE_CLIENT */
2255 bsp->fids[i].Volume = scp->fid.volume;
2256 bsp->fids[i].Vnode = tfid.vnode;
2257 bsp->fids[i].Unique = tfid.unique;
2261 /* called with a locked scp and a pointer to a buffer. Make bulk stat
2262 * calls on all undeleted files in the page of the directory specified.
2265 cm_TryBulkStat(cm_scache_t *dscp, osi_hyper_t *offsetp, cm_user_t *userp,
2269 cm_bulkStat_t bb; /* this is *BIG*, probably 16K or so;
2270 * watch for stack problems */
2271 AFSCBFids fidStruct;
2272 AFSBulkStats statStruct;
2274 AFSCBs callbackStruct;
2277 cm_callbackRequest_t cbReq;
2283 struct rx_connection * callp;
2284 int inlinebulk = 0; /* Did we use InlineBulkStatus RPC or not? */
2286 osi_Log1(afsd_logp, "cm_TryBulkStat dir 0x%p", dscp);
2288 /* should be on a buffer boundary */
2289 osi_assertx((offsetp->LowPart & (cm_data.buf_blockSize - 1)) == 0, "invalid offset");
2291 memset(&bb, 0, sizeof(bb));
2292 bb.bufOffset = *offsetp;
2294 lock_ReleaseMutex(&dscp->mx);
2295 /* first, assemble the file IDs we need to stat */
2296 code = cm_ApplyDir(dscp, cm_TryBulkProc, (void *) &bb, offsetp, userp, reqp, NULL);
2298 /* if we failed, bail out early */
2299 if (code && code != CM_ERROR_STOPNOW) {
2300 lock_ObtainMutex(&dscp->mx);
2304 /* otherwise, we may have one or more bulk stat's worth of stuff in bb;
2305 * make the calls to create the entries. Handle AFSCBMAX files at a
2309 while (filex < bb.counter) {
2310 filesThisCall = bb.counter - filex;
2311 if (filesThisCall > AFSCBMAX)
2312 filesThisCall = AFSCBMAX;
2314 fidStruct.AFSCBFids_len = filesThisCall;
2315 fidStruct.AFSCBFids_val = &bb.fids[filex];
2316 statStruct.AFSBulkStats_len = filesThisCall;
2317 statStruct.AFSBulkStats_val = &bb.stats[filex];
2318 callbackStruct.AFSCBs_len = filesThisCall;
2319 callbackStruct.AFSCBs_val = &bb.callbacks[filex];
2320 cm_StartCallbackGrantingCall(NULL, &cbReq);
2321 osi_Log1(afsd_logp, "CALL BulkStatus, %d entries", filesThisCall);
2323 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2327 callp = cm_GetRxConn(connp);
2328 if (!(connp->serverp->flags & CM_SERVERFLAG_NOINLINEBULK)) {
2329 code = RXAFS_InlineBulkStatus(callp, &fidStruct,
2330 &statStruct, &callbackStruct, &volSync);
2331 if (code == RXGEN_OPCODE) {
2332 cm_SetServerNoInlineBulk(connp->serverp, 0);
2338 code = RXAFS_BulkStatus(callp, &fidStruct,
2339 &statStruct, &callbackStruct, &volSync);
2341 rx_PutConnection(callp);
2343 } while (cm_Analyze(connp, userp, reqp, &dscp->fid,
2344 &volSync, NULL, &cbReq, code));
2345 code = cm_MapRPCError(code, reqp);
2347 osi_Log2(afsd_logp, "CALL %sBulkStatus FAILURE code 0x%x",
2348 inlinebulk ? "Inline" : "", code);
2350 osi_Log1(afsd_logp, "CALL %sBulkStatus SUCCESS", inlinebulk ? "Inline" : "");
2352 /* may as well quit on an error, since we're not going to do
2353 * much better on the next immediate call, either.
2356 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2360 /* otherwise, we should do the merges */
2361 for (i = 0; i<filesThisCall; i++) {
2363 tfid.cell = dscp->fid.cell;
2364 tfid.volume = bb.fids[j].Volume;
2365 tfid.vnode = bb.fids[j].Vnode;
2366 tfid.unique = bb.fids[j].Unique;
2367 code = cm_GetSCache(&tfid, &scp, userp, reqp);
2371 /* otherwise, if this entry has no callback info,
2374 lock_ObtainMutex(&scp->mx);
2375 /* now, we have to be extra paranoid on merging in this
2376 * information, since we didn't use cm_SyncOp before
2377 * starting the fetch to make sure that no bad races
2378 * were occurring. Specifically, we need to make sure
2379 * we don't obliterate any newer information in the
2380 * vnode than have here.
2382 * Right now, be pretty conservative: if there's a
2383 * callback or a pending call, skip it.
2385 if ((scp->cbServerp == NULL || (scp->flags & CM_SCACHEFLAG_EACCESS))
2387 (CM_SCACHEFLAG_FETCHING
2388 | CM_SCACHEFLAG_STORING
2389 | CM_SCACHEFLAG_SIZESTORING))) {
2390 cm_EndCallbackGrantingCall(scp, &cbReq,
2392 CM_CALLBACK_MAINTAINCOUNT);
2393 cm_MergeStatus(dscp, scp, &bb.stats[j], &volSync, userp, 0);
2395 lock_ReleaseMutex(&scp->mx);
2396 cm_ReleaseSCache(scp);
2397 } /* all files in the response */
2398 /* now tell it to drop the count,
2399 * after doing the vnode processing above */
2400 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2402 filex += filesThisCall;
2403 } /* while there are still more files to process */
2404 lock_ObtainMutex(&dscp->mx);
2406 /* If we did the InlineBulk RPC pull out the return code and log it */
2408 if ((&bb.stats[0])->errorCode) {
2409 osi_Log1(afsd_logp, "cm_TryBulkStat bulk stat error: %d",
2410 (&bb.stats[0])->errorCode);
2414 osi_Log0(afsd_logp, "END cm_TryBulkStat");
2418 void cm_StatusFromAttr(AFSStoreStatus *statusp, cm_scache_t *scp, cm_attr_t *attrp)
2422 /* initialize store back mask as inexpensive local variable */
2424 memset(statusp, 0, sizeof(AFSStoreStatus));
2426 /* copy out queued info from scache first, if scp passed in */
2428 if (scp->mask & CM_SCACHEMASK_CLIENTMODTIME) {
2429 statusp->ClientModTime = scp->clientModTime;
2430 mask |= AFS_SETMODTIME;
2431 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
2436 /* now add in our locally generated request */
2437 if (attrp->mask & CM_ATTRMASK_CLIENTMODTIME) {
2438 statusp->ClientModTime = attrp->clientModTime;
2439 mask |= AFS_SETMODTIME;
2441 if (attrp->mask & CM_ATTRMASK_UNIXMODEBITS) {
2442 statusp->UnixModeBits = attrp->unixModeBits;
2443 mask |= AFS_SETMODE;
2445 if (attrp->mask & CM_ATTRMASK_OWNER) {
2446 statusp->Owner = attrp->owner;
2447 mask |= AFS_SETOWNER;
2449 if (attrp->mask & CM_ATTRMASK_GROUP) {
2450 statusp->Group = attrp->group;
2451 mask |= AFS_SETGROUP;
2454 statusp->Mask = mask;
2457 /* set the file size, and make sure that all relevant buffers have been
2458 * truncated. Ensure that any partially truncated buffers have been zeroed
2459 * to the end of the buffer.
2461 long cm_SetLength(cm_scache_t *scp, osi_hyper_t *sizep, cm_user_t *userp,
2467 /* start by locking out buffer creation */
2468 lock_ObtainWrite(&scp->bufCreateLock);
2470 /* verify that this is a file, not a dir or a symlink */
2471 lock_ObtainMutex(&scp->mx);
2472 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
2473 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2476 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2478 if (scp->fileType != CM_SCACHETYPE_FILE) {
2479 code = CM_ERROR_ISDIR;
2484 if (LargeIntegerLessThan(*sizep, scp->length))
2489 lock_ReleaseMutex(&scp->mx);
2491 /* can't hold scp->mx lock here, since we may wait for a storeback to
2492 * finish if the buffer package is cleaning a buffer by storing it to
2496 buf_Truncate(scp, userp, reqp, sizep);
2498 /* now ensure that file length is short enough, and update truncPos */
2499 lock_ObtainMutex(&scp->mx);
2501 /* make sure we have a callback (so we have the right value for the
2502 * length), and wait for it to be safe to do a truncate.
2504 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_WRITE,
2505 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2506 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2508 /* If we only have 'i' bits, then we should still be able to set
2509 the size of a file we created. */
2510 if (code == CM_ERROR_NOACCESS && scp->creator == userp) {
2511 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
2512 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2513 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2519 if (LargeIntegerLessThan(*sizep, scp->length)) {
2520 /* a real truncation. If truncPos is not set yet, or is bigger
2521 * than where we're truncating the file, set truncPos to this
2526 if (!(scp->mask & CM_SCACHEMASK_TRUNCPOS)
2527 || LargeIntegerLessThan(*sizep, scp->length)) {
2529 scp->truncPos = *sizep;
2530 scp->mask |= CM_SCACHEMASK_TRUNCPOS;
2532 /* in either case, the new file size has been changed */
2533 scp->length = *sizep;
2534 scp->mask |= CM_SCACHEMASK_LENGTH;
2536 else if (LargeIntegerGreaterThan(*sizep, scp->length)) {
2537 /* really extending the file */
2538 scp->length = *sizep;
2539 scp->mask |= CM_SCACHEMASK_LENGTH;
2542 /* done successfully */
2545 cm_SyncOpDone(scp, NULL,
2546 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2547 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2550 lock_ReleaseMutex(&scp->mx);
2551 lock_ReleaseWrite(&scp->bufCreateLock);
2556 /* set the file size or other attributes (but not both at once) */
2557 long cm_SetAttr(cm_scache_t *scp, cm_attr_t *attrp, cm_user_t *userp,
2561 AFSFetchStatus afsOutStatus;
2565 AFSStoreStatus afsInStatus;
2566 struct rx_connection * callp;
2568 /* handle file length setting */
2569 if (attrp->mask & CM_ATTRMASK_LENGTH)
2570 return cm_SetLength(scp, &attrp->length, userp, reqp);
2572 lock_ObtainMutex(&scp->mx);
2573 /* otherwise, we have to make an RPC to get the status */
2574 code = cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STORESTATUS);
2576 lock_ReleaseMutex(&scp->mx);
2580 /* make the attr structure */
2581 cm_StatusFromAttr(&afsInStatus, scp, attrp);
2583 tfid.Volume = scp->fid.volume;
2584 tfid.Vnode = scp->fid.vnode;
2585 tfid.Unique = scp->fid.unique;
2586 lock_ReleaseMutex(&scp->mx);
2588 /* now make the RPC */
2589 osi_Log1(afsd_logp, "CALL StoreStatus scp 0x%p", scp);
2591 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2595 callp = cm_GetRxConn(connp);
2596 code = RXAFS_StoreStatus(callp, &tfid,
2597 &afsInStatus, &afsOutStatus, &volSync);
2598 rx_PutConnection(callp);
2600 } while (cm_Analyze(connp, userp, reqp,
2601 &scp->fid, &volSync, NULL, NULL, code));
2602 code = cm_MapRPCError(code, reqp);
2605 osi_Log1(afsd_logp, "CALL StoreStatus FAILURE, code 0x%x", code);
2607 osi_Log0(afsd_logp, "CALL StoreStatus SUCCESS");
2609 lock_ObtainMutex(&scp->mx);
2610 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STORESTATUS);
2612 cm_MergeStatus(NULL, scp, &afsOutStatus, &volSync, userp,
2613 CM_MERGEFLAG_FORCE|CM_MERGEFLAG_STOREDATA);
2615 /* if we're changing the mode bits, discard the ACL cache,
2616 * since we changed the mode bits.
2618 if (afsInStatus.Mask & AFS_SETMODE)
2619 cm_FreeAllACLEnts(scp);
2620 lock_ReleaseMutex(&scp->mx);
2624 long cm_Create(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2625 cm_scache_t **scpp, cm_user_t *userp, cm_req_t *reqp)
2630 cm_callbackRequest_t cbReq;
2633 cm_scache_t *scp = NULL;
2635 AFSStoreStatus inStatus;
2636 AFSFetchStatus updatedDirStatus;
2637 AFSFetchStatus newFileStatus;
2638 AFSCallBack newFileCallback;
2640 struct rx_connection * callp;
2643 /* can't create names with @sys in them; must expand it manually first.
2644 * return "invalid request" if they try.
2646 if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2647 return CM_ERROR_ATSYS;
2650 /* before starting the RPC, mark that we're changing the file data, so
2651 * that someone who does a chmod will know to wait until our call
2654 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2655 lock_ObtainMutex(&dscp->mx);
2656 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2657 lock_ReleaseMutex(&dscp->mx);
2659 cm_StartCallbackGrantingCall(NULL, &cbReq);
2661 cm_EndDirOp(&dirop);
2668 cm_StatusFromAttr(&inStatus, NULL, attrp);
2670 /* try the RPC now */
2671 osi_Log1(afsd_logp, "CALL CreateFile scp 0x%p", dscp);
2673 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2677 dirAFSFid.Volume = dscp->fid.volume;
2678 dirAFSFid.Vnode = dscp->fid.vnode;
2679 dirAFSFid.Unique = dscp->fid.unique;
2681 callp = cm_GetRxConn(connp);
2682 code = RXAFS_CreateFile(connp->callp, &dirAFSFid, namep,
2683 &inStatus, &newAFSFid, &newFileStatus,
2684 &updatedDirStatus, &newFileCallback,
2686 rx_PutConnection(callp);
2688 } while (cm_Analyze(connp, userp, reqp,
2689 &dscp->fid, &volSync, NULL, &cbReq, code));
2690 code = cm_MapRPCError(code, reqp);
2693 osi_Log1(afsd_logp, "CALL CreateFile FAILURE, code 0x%x", code);
2695 osi_Log0(afsd_logp, "CALL CreateFile SUCCESS");
2698 lock_ObtainWrite(&dirop.scp->dirlock);
2699 dirop.lockType = CM_DIRLOCK_WRITE;
2701 lock_ObtainMutex(&dscp->mx);
2702 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2704 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, 0);
2706 lock_ReleaseMutex(&dscp->mx);
2708 /* now try to create the file's entry, too, but be careful to
2709 * make sure that we don't merge in old info. Since we weren't locking
2710 * out any requests during the file's creation, we may have pretty old
2714 newFid.cell = dscp->fid.cell;
2715 newFid.volume = dscp->fid.volume;
2716 newFid.vnode = newAFSFid.Vnode;
2717 newFid.unique = newAFSFid.Unique;
2718 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2720 lock_ObtainMutex(&scp->mx);
2721 scp->creator = userp; /* remember who created it */
2722 if (!cm_HaveCallback(scp)) {
2723 cm_MergeStatus(dscp, scp, &newFileStatus, &volSync,
2725 cm_EndCallbackGrantingCall(scp, &cbReq,
2726 &newFileCallback, 0);
2729 lock_ReleaseMutex(&scp->mx);
2734 /* make sure we end things properly */
2736 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2738 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2739 cm_DirCreateEntry(&dirop, namep, &newFid);
2741 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2744 cm_EndDirOp(&dirop);
2749 long cm_FSync(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
2753 lock_ObtainWrite(&scp->bufCreateLock);
2754 code = buf_CleanVnode(scp, userp, reqp);
2755 lock_ReleaseWrite(&scp->bufCreateLock);
2757 lock_ObtainMutex(&scp->mx);
2759 if (scp->mask & (CM_SCACHEMASK_TRUNCPOS
2760 | CM_SCACHEMASK_CLIENTMODTIME
2761 | CM_SCACHEMASK_LENGTH))
2762 code = cm_StoreMini(scp, userp, reqp);
2764 if (scp->flags & (CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE)) {
2765 code = (scp->flags & CM_SCACHEFLAG_OVERQUOTA) ? CM_ERROR_QUOTA : CM_ERROR_SPACE;
2766 scp->flags &= ~(CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE);
2769 lock_ReleaseMutex(&scp->mx);
2774 long cm_MakeDir(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2775 cm_user_t *userp, cm_req_t *reqp)
2780 cm_callbackRequest_t cbReq;
2783 cm_scache_t *scp = NULL;
2785 AFSStoreStatus inStatus;
2786 AFSFetchStatus updatedDirStatus;
2787 AFSFetchStatus newDirStatus;
2788 AFSCallBack newDirCallback;
2790 struct rx_connection * callp;
2793 /* can't create names with @sys in them; must expand it manually first.
2794 * return "invalid request" if they try.
2796 if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2797 return CM_ERROR_ATSYS;
2800 /* before starting the RPC, mark that we're changing the directory
2801 * data, so that someone who does a chmod on the dir will wait until
2802 * our call completes.
2804 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2805 lock_ObtainMutex(&dscp->mx);
2806 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2807 lock_ReleaseMutex(&dscp->mx);
2809 cm_StartCallbackGrantingCall(NULL, &cbReq);
2811 cm_EndDirOp(&dirop);
2818 cm_StatusFromAttr(&inStatus, NULL, attrp);
2820 /* try the RPC now */
2821 osi_Log1(afsd_logp, "CALL MakeDir scp 0x%p", dscp);
2823 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2827 dirAFSFid.Volume = dscp->fid.volume;
2828 dirAFSFid.Vnode = dscp->fid.vnode;
2829 dirAFSFid.Unique = dscp->fid.unique;
2831 callp = cm_GetRxConn(connp);
2832 code = RXAFS_MakeDir(connp->callp, &dirAFSFid, namep,
2833 &inStatus, &newAFSFid, &newDirStatus,
2834 &updatedDirStatus, &newDirCallback,
2836 rx_PutConnection(callp);
2838 } while (cm_Analyze(connp, userp, reqp,
2839 &dscp->fid, &volSync, NULL, &cbReq, code));
2840 code = cm_MapRPCError(code, reqp);
2843 osi_Log1(afsd_logp, "CALL MakeDir FAILURE, code 0x%x", code);
2845 osi_Log0(afsd_logp, "CALL MakeDir SUCCESS");
2848 lock_ObtainWrite(&dirop.scp->dirlock);
2849 dirop.lockType = CM_DIRLOCK_WRITE;
2851 lock_ObtainMutex(&dscp->mx);
2852 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2854 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, 0);
2856 lock_ReleaseMutex(&dscp->mx);
2858 /* now try to create the new dir's entry, too, but be careful to
2859 * make sure that we don't merge in old info. Since we weren't locking
2860 * out any requests during the file's creation, we may have pretty old
2864 newFid.cell = dscp->fid.cell;
2865 newFid.volume = dscp->fid.volume;
2866 newFid.vnode = newAFSFid.Vnode;
2867 newFid.unique = newAFSFid.Unique;
2868 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2870 lock_ObtainMutex(&scp->mx);
2871 if (!cm_HaveCallback(scp)) {
2872 cm_MergeStatus(dscp, scp, &newDirStatus, &volSync,
2874 cm_EndCallbackGrantingCall(scp, &cbReq,
2875 &newDirCallback, 0);
2878 lock_ReleaseMutex(&scp->mx);
2879 cm_ReleaseSCache(scp);
2883 /* make sure we end things properly */
2885 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2887 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2888 cm_DirCreateEntry(&dirop, namep, &newFid);
2890 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2893 cm_EndDirOp(&dirop);
2895 /* and return error code */
2899 long cm_Link(cm_scache_t *dscp, char *namep, cm_scache_t *sscp, long flags,
2900 cm_user_t *userp, cm_req_t *reqp)
2905 AFSFid existingAFSFid;
2906 AFSFetchStatus updatedDirStatus;
2907 AFSFetchStatus newLinkStatus;
2909 struct rx_connection * callp;
2912 if (dscp->fid.cell != sscp->fid.cell ||
2913 dscp->fid.volume != sscp->fid.volume) {
2914 return CM_ERROR_CROSSDEVLINK;
2917 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2918 lock_ObtainMutex(&dscp->mx);
2919 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2920 lock_ReleaseMutex(&dscp->mx);
2922 cm_EndDirOp(&dirop);
2927 /* try the RPC now */
2928 osi_Log1(afsd_logp, "CALL Link scp 0x%p", dscp);
2930 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2933 dirAFSFid.Volume = dscp->fid.volume;
2934 dirAFSFid.Vnode = dscp->fid.vnode;
2935 dirAFSFid.Unique = dscp->fid.unique;
2937 existingAFSFid.Volume = sscp->fid.volume;
2938 existingAFSFid.Vnode = sscp->fid.vnode;
2939 existingAFSFid.Unique = sscp->fid.unique;
2941 callp = cm_GetRxConn(connp);
2942 code = RXAFS_Link(callp, &dirAFSFid, namep, &existingAFSFid,
2943 &newLinkStatus, &updatedDirStatus, &volSync);
2944 rx_PutConnection(callp);
2945 osi_Log1(smb_logp," RXAFS_Link returns 0x%x", code);
2947 } while (cm_Analyze(connp, userp, reqp,
2948 &dscp->fid, &volSync, NULL, NULL, code));
2950 code = cm_MapRPCError(code, reqp);
2953 osi_Log1(afsd_logp, "CALL Link FAILURE, code 0x%x", code);
2955 osi_Log0(afsd_logp, "CALL Link SUCCESS");
2958 lock_ObtainWrite(&dirop.scp->dirlock);
2959 dirop.lockType = CM_DIRLOCK_WRITE;
2961 lock_ObtainMutex(&dscp->mx);
2962 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2964 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, 0);
2966 lock_ReleaseMutex(&dscp->mx);
2969 if (cm_CheckDirOpForSingleChange(&dirop)) {
2970 cm_DirCreateEntry(&dirop, namep, &sscp->fid);
2972 cm_BPlusDirCreateEntry(&dirop, namep, &sscp->fid);
2976 cm_EndDirOp(&dirop);
2981 long cm_SymLink(cm_scache_t *dscp, char *namep, char *contentsp, long flags,
2982 cm_attr_t *attrp, cm_user_t *userp, cm_req_t *reqp)
2990 AFSStoreStatus inStatus;
2991 AFSFetchStatus updatedDirStatus;
2992 AFSFetchStatus newLinkStatus;
2994 struct rx_connection * callp;
2997 /* before starting the RPC, mark that we're changing the directory data,
2998 * so that someone who does a chmod on the dir will wait until our
3001 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3002 lock_ObtainMutex(&dscp->mx);
3003 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3004 lock_ReleaseMutex(&dscp->mx);
3006 cm_EndDirOp(&dirop);
3011 cm_StatusFromAttr(&inStatus, NULL, attrp);
3013 /* try the RPC now */
3014 osi_Log1(afsd_logp, "CALL Symlink scp 0x%p", dscp);
3016 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3020 dirAFSFid.Volume = dscp->fid.volume;
3021 dirAFSFid.Vnode = dscp->fid.vnode;
3022 dirAFSFid.Unique = dscp->fid.unique;
3024 callp = cm_GetRxConn(connp);
3025 code = RXAFS_Symlink(callp, &dirAFSFid, namep, contentsp,
3026 &inStatus, &newAFSFid, &newLinkStatus,
3027 &updatedDirStatus, &volSync);
3028 rx_PutConnection(callp);
3030 } while (cm_Analyze(connp, userp, reqp,
3031 &dscp->fid, &volSync, NULL, NULL, code));
3032 code = cm_MapRPCError(code, reqp);
3035 osi_Log1(afsd_logp, "CALL Symlink FAILURE, code 0x%x", code);
3037 osi_Log0(afsd_logp, "CALL Symlink SUCCESS");
3040 lock_ObtainWrite(&dirop.scp->dirlock);
3041 dirop.lockType = CM_DIRLOCK_WRITE;
3043 lock_ObtainMutex(&dscp->mx);
3044 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3046 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, 0);
3048 lock_ReleaseMutex(&dscp->mx);
3051 if (cm_CheckDirOpForSingleChange(&dirop)) {
3052 newFid.cell = dscp->fid.cell;
3053 newFid.volume = dscp->fid.volume;
3054 newFid.vnode = newAFSFid.Vnode;
3055 newFid.unique = newAFSFid.Unique;
3057 cm_DirCreateEntry(&dirop, namep, &newFid);
3059 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
3063 cm_EndDirOp(&dirop);
3065 /* now try to create the new dir's entry, too, but be careful to
3066 * make sure that we don't merge in old info. Since we weren't locking
3067 * out any requests during the file's creation, we may have pretty old
3071 newFid.cell = dscp->fid.cell;
3072 newFid.volume = dscp->fid.volume;
3073 newFid.vnode = newAFSFid.Vnode;
3074 newFid.unique = newAFSFid.Unique;
3075 code = cm_GetSCache(&newFid, &scp, userp, reqp);
3077 lock_ObtainMutex(&scp->mx);
3078 if (!cm_HaveCallback(scp)) {
3079 cm_MergeStatus(dscp, scp, &newLinkStatus, &volSync,
3082 lock_ReleaseMutex(&scp->mx);
3083 cm_ReleaseSCache(scp);
3087 /* and return error code */
3091 long cm_RemoveDir(cm_scache_t *dscp, char *namep, cm_user_t *userp,
3098 AFSFetchStatus updatedDirStatus;
3100 struct rx_connection * callp;
3103 /* before starting the RPC, mark that we're changing the directory data,
3104 * so that someone who does a chmod on the dir will wait until our
3107 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3108 lock_ObtainMutex(&dscp->mx);
3109 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3110 lock_ReleaseMutex(&dscp->mx);
3112 cm_EndDirOp(&dirop);
3117 /* try the RPC now */
3118 osi_Log1(afsd_logp, "CALL RemoveDir scp 0x%p", dscp);
3120 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3124 dirAFSFid.Volume = dscp->fid.volume;
3125 dirAFSFid.Vnode = dscp->fid.vnode;
3126 dirAFSFid.Unique = dscp->fid.unique;
3128 callp = cm_GetRxConn(connp);
3129 code = RXAFS_RemoveDir(callp, &dirAFSFid, namep,
3130 &updatedDirStatus, &volSync);
3131 rx_PutConnection(callp);
3133 } while (cm_Analyze(connp, userp, reqp,
3134 &dscp->fid, &volSync, NULL, NULL, code));
3135 code = cm_MapRPCErrorRmdir(code, reqp);
3138 osi_Log1(afsd_logp, "CALL RemoveDir FAILURE, code 0x%x", code);
3140 osi_Log0(afsd_logp, "CALL RemoveDir SUCCESS");
3143 lock_ObtainWrite(&dirop.scp->dirlock);
3144 dirop.lockType = CM_DIRLOCK_WRITE;
3146 lock_ObtainMutex(&dscp->mx);
3147 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3149 cm_dnlcRemove(dscp, namep);
3150 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, 0);
3152 lock_ReleaseMutex(&dscp->mx);
3155 if (cm_CheckDirOpForSingleChange(&dirop)) {
3156 cm_DirDeleteEntry(&dirop, namep);
3158 cm_BPlusDirDeleteEntry(&dirop, namep);
3162 cm_EndDirOp(&dirop);
3164 /* and return error code */
3168 long cm_Open(cm_scache_t *scp, int type, cm_user_t *userp)
3170 /* grab mutex on contents */
3171 lock_ObtainMutex(&scp->mx);
3173 /* reset the prefetch info */
3174 scp->prefetch.base.LowPart = 0; /* base */
3175 scp->prefetch.base.HighPart = 0;
3176 scp->prefetch.end.LowPart = 0; /* and end */
3177 scp->prefetch.end.HighPart = 0;
3179 /* release mutex on contents */
3180 lock_ReleaseMutex(&scp->mx);
3186 long cm_Rename(cm_scache_t *oldDscp, char *oldNamep, cm_scache_t *newDscp,
3187 char *newNamep, cm_user_t *userp, cm_req_t *reqp)
3191 AFSFid oldDirAFSFid;
3192 AFSFid newDirAFSFid;
3194 AFSFetchStatus updatedOldDirStatus;
3195 AFSFetchStatus updatedNewDirStatus;
3198 struct rx_connection * callp;
3199 cm_dirOp_t oldDirOp;
3202 cm_dirOp_t newDirOp;
3204 /* before starting the RPC, mark that we're changing the directory data,
3205 * so that someone who does a chmod on the dir will wait until our call
3206 * completes. We do this in vnode order so that we don't deadlock,
3207 * which makes the code a little verbose.
3209 if (oldDscp == newDscp) {
3210 /* check for identical names */
3211 if (strcmp(oldNamep, newNamep) == 0)
3212 return CM_ERROR_RENAME_IDENTICAL;
3215 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3216 lock_ObtainMutex(&oldDscp->mx);
3217 cm_dnlcRemove(oldDscp, oldNamep);
3218 cm_dnlcRemove(oldDscp, newNamep);
3219 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3220 CM_SCACHESYNC_STOREDATA);
3221 lock_ReleaseMutex(&oldDscp->mx);
3223 cm_EndDirOp(&oldDirOp);
3227 /* two distinct dir vnodes */
3229 if (oldDscp->fid.cell != newDscp->fid.cell ||
3230 oldDscp->fid.volume != newDscp->fid.volume)
3231 return CM_ERROR_CROSSDEVLINK;
3233 /* shouldn't happen that we have distinct vnodes for two
3234 * different files, but could due to deliberate attack, or
3235 * stale info. Avoid deadlocks and quit now.
3237 if (oldDscp->fid.vnode == newDscp->fid.vnode)
3238 return CM_ERROR_CROSSDEVLINK;
3240 if (oldDscp->fid.vnode < newDscp->fid.vnode) {
3241 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3242 lock_ObtainMutex(&oldDscp->mx);
3243 cm_dnlcRemove(oldDscp, oldNamep);
3244 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3245 CM_SCACHESYNC_STOREDATA);
3246 lock_ReleaseMutex(&oldDscp->mx);
3248 cm_EndDirOp(&oldDirOp);
3250 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3251 lock_ObtainMutex(&newDscp->mx);
3252 cm_dnlcRemove(newDscp, newNamep);
3253 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3254 CM_SCACHESYNC_STOREDATA);
3255 lock_ReleaseMutex(&newDscp->mx);
3257 cm_EndDirOp(&newDirOp);
3259 /* cleanup first one */
3260 lock_ObtainMutex(&oldDscp->mx);
3261 cm_SyncOpDone(oldDscp, NULL,
3262 CM_SCACHESYNC_STOREDATA);
3263 lock_ReleaseMutex(&oldDscp->mx);
3264 cm_EndDirOp(&oldDirOp);
3269 /* lock the new vnode entry first */
3270 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3271 lock_ObtainMutex(&newDscp->mx);
3272 cm_dnlcRemove(newDscp, newNamep);
3273 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3274 CM_SCACHESYNC_STOREDATA);
3275 lock_ReleaseMutex(&newDscp->mx);
3277 cm_EndDirOp(&newDirOp);
3279 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3280 lock_ObtainMutex(&oldDscp->mx);
3281 cm_dnlcRemove(oldDscp, oldNamep);
3282 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3283 CM_SCACHESYNC_STOREDATA);
3284 lock_ReleaseMutex(&oldDscp->mx);
3286 cm_EndDirOp(&oldDirOp);
3288 /* cleanup first one */
3289 lock_ObtainMutex(&newDscp->mx);
3290 cm_SyncOpDone(newDscp, NULL,
3291 CM_SCACHESYNC_STOREDATA);
3292 lock_ReleaseMutex(&newDscp->mx);
3293 cm_EndDirOp(&newDirOp);
3297 } /* two distinct vnodes */
3304 /* try the RPC now */
3305 osi_Log2(afsd_logp, "CALL Rename old scp 0x%p new scp 0x%p",
3308 code = cm_ConnFromFID(&oldDscp->fid, userp, reqp, &connp);
3312 oldDirAFSFid.Volume = oldDscp->fid.volume;
3313 oldDirAFSFid.Vnode = oldDscp->fid.vnode;
3314 oldDirAFSFid.Unique = oldDscp->fid.unique;
3315 newDirAFSFid.Volume = newDscp->fid.volume;
3316 newDirAFSFid.Vnode = newDscp->fid.vnode;
3317 newDirAFSFid.Unique = newDscp->fid.unique;
3319 callp = cm_GetRxConn(connp);
3320 code = RXAFS_Rename(callp, &oldDirAFSFid, oldNamep,
3321 &newDirAFSFid, newNamep,
3322 &updatedOldDirStatus, &updatedNewDirStatus,
3324 rx_PutConnection(callp);
3326 } while (cm_Analyze(connp, userp, reqp, &oldDscp->fid,
3327 &volSync, NULL, NULL, code));
3328 code = cm_MapRPCError(code, reqp);
3331 osi_Log1(afsd_logp, "CALL Rename FAILURE, code 0x%x", code);
3333 osi_Log0(afsd_logp, "CALL Rename SUCCESS");
3335 /* update the individual stat cache entries for the directories */
3337 lock_ObtainWrite(&oldDirOp.scp->dirlock);
3338 oldDirOp.lockType = CM_DIRLOCK_WRITE;
3340 lock_ObtainMutex(&oldDscp->mx);
3341 cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_STOREDATA);
3344 cm_MergeStatus(NULL, oldDscp, &updatedOldDirStatus, &volSync,
3346 lock_ReleaseMutex(&oldDscp->mx);
3349 if (cm_CheckDirOpForSingleChange(&oldDirOp)) {
3352 diropCode = cm_BPlusDirLookup(&oldDirOp, oldNamep, &fileFid);
3353 if (diropCode == CM_ERROR_INEXACT_MATCH)
3355 else if (diropCode == EINVAL)
3357 diropCode = cm_DirLookup(&oldDirOp, oldNamep, &fileFid);
3359 if (diropCode == 0) {
3361 diropCode = cm_DirCreateEntry(&oldDirOp, newNamep, &fileFid);
3363 cm_BPlusDirCreateEntry(&oldDirOp, newNamep, &fileFid);
3367 if (diropCode == 0) {
3368 diropCode = cm_DirDeleteEntry(&oldDirOp, oldNamep);
3370 cm_BPlusDirDeleteEntry(&oldDirOp, oldNamep);
3376 cm_EndDirOp(&oldDirOp);
3378 /* and update it for the new one, too, if necessary */
3381 lock_ObtainWrite(&newDirOp.scp->dirlock);
3382 newDirOp.lockType = CM_DIRLOCK_WRITE;
3384 lock_ObtainMutex(&newDscp->mx);
3385 cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_STOREDATA);
3387 cm_MergeStatus(NULL, newDscp, &updatedNewDirStatus, &volSync,
3389 lock_ReleaseMutex(&newDscp->mx);
3392 /* we only make the local change if we successfully made
3393 the change in the old directory AND there was only one
3394 change in the new directory */
3395 if (diropCode == 0 && cm_CheckDirOpForSingleChange(&newDirOp)) {
3396 cm_DirCreateEntry(&newDirOp, newNamep, &fileFid);
3398 cm_BPlusDirCreateEntry(&newDirOp, newNamep, &fileFid);
3402 cm_EndDirOp(&newDirOp);
3405 /* and return error code */
3409 /* Byte range locks:
3411 The OpenAFS Windows client has to fake byte range locks given no
3412 server side support for such locks. This is implemented as keyed
3413 byte range locks on the cache manager.
3415 Keyed byte range locks:
3417 Each cm_scache_t structure keeps track of a list of keyed locks.
3418 The key for a lock identifies an owner of a set of locks (referred
3419 to as a client). Each key is represented by a value. The set of
3420 key values used within a specific cm_scache_t structure form a
3421 namespace that has a scope of just that cm_scache_t structure. The
3422 same key value can be used with another cm_scache_t structure and
3423 correspond to a completely different client. However it is
3424 advantageous for the SMB or IFS layer to make sure that there is a
3425 1-1 mapping between client and keys over all cm_scache_t objects.
3427 Assume a client C has key Key(C) (although, since the scope of the
3428 key is a cm_scache_t, the key can be Key(C,S), where S is the
3429 cm_scache_t. But assume a 1-1 relation between keys and clients).
3430 A byte range (O,+L) denotes byte addresses (O) through (O+L-1)
3431 inclusive (a.k.a. [O,O+L-1]). The function Key(x) is implemented
3432 through cm_generateKey() function for both SMB and IFS.
3434 The list of locks for a cm_scache_t object S is maintained in
3435 S->fileLocks. The cache manager will set a lock on the AFS file
3436 server in order to assert the locks in S->fileLocks. If only
3437 shared locks are in place for S, then the cache manager will obtain
3438 a LockRead lock, while if there are any exclusive locks, it will
3439 obtain a LockWrite lock. If the exclusive locks are all released
3440 while the shared locks remain, then the cache manager will
3441 downgrade the lock from LockWrite to LockRead. Similarly, if an
3442 exclusive lock is obtained when only shared locks exist, then the
3443 cache manager will try to upgrade the lock from LockRead to
3446 Each lock L owned by client C maintains a key L->key such that
3447 L->key == Key(C), the effective range defined by L->LOffset and
3448 L->LLength such that the range of bytes affected by the lock is
3449 (L->LOffset, +L->LLength), a type maintained in L->LockType which
3450 is either exclusive or shared.
3454 A lock exists iff it is in S->fileLocks for some cm_scache_t
3455 S. Existing locks are in one of the following states: ACTIVE,
3456 WAITLOCK, WAITUNLOCK, LOST, DELETED.
3458 The following sections describe each lock and the associated
3461 1. ACTIVE: A lock L is ACTIVE iff the cache manager has asserted
3462 the lock with the AFS file server. This type of lock can be
3463 exercised by a client to read or write to the locked region (as
3466 1.1 ACTIVE->LOST: When the AFS file server fails to extend a
3467 server lock that was required to assert the lock. Before
3468 marking the lock as lost, the cache manager checks if the file
3469 has changed on the server. If the file has not changed, then
3470 the cache manager will attempt to obtain a new server lock
3471 that is sufficient to assert the client side locks for the
3472 file. If any of these fail, the lock is marked as LOST.
3473 Otherwise, it is left as ACTIVE.
3475 1.2 ACTIVE->DELETED: Lock is released.
3477 2. WAITLOCK: A lock is in a WAITLOCK state if the cache manager
3478 grants the lock but the lock is yet to be asserted with the AFS
3479 file server. Once the file server grants the lock, the state
3480 will transition to an ACTIVE lock.
3482 2.1 WAITLOCK->ACTIVE: The server granted the lock.
3484 2.2 WAITLOCK->DELETED: Lock is abandoned, or timed out during
3487 2.3 WAITLOCK->LOST: One or more locks from this client were
3488 marked as LOST. No further locks will be granted to this
3489 client until all lost locks are removed.
3491 3. WAITUNLOCK: A lock is in a WAITUNLOCK state if the cache manager
3492 receives a request for a lock that conflicts with an existing
3493 ACTIVE or WAITLOCK lock. The lock will be placed in the queue
3494 and will be granted at such time the conflicting locks are
3495 removed, at which point the state will transition to either
3498 3.1 WAITUNLOCK->ACTIVE: The conflicting lock was removed. The
3499 current serverLock is sufficient to assert this lock, or a
3500 sufficient serverLock is obtained.
3502 3.2 WAITUNLOCK->WAITLOCK: The conflicting lock was removed,
3503 however the required serverLock is yet to be asserted with the
3506 3.3 WAITUNLOCK->DELETED: The lock is abandoned, timed out or
3509 3.5 WAITUNLOCK->LOST: One or more locks from this client were
3510 marked as LOST. No further locks will be granted to this
3511 client until all lost locks are removed.
3513 4. LOST: A lock L is LOST if the server lock that was required to
3514 assert the lock could not be obtained or if it could not be
3515 extended, or if other locks by the same client were LOST.
3516 Essentially, once a lock is LOST, the contract between the cache
3517 manager and that specific client is no longer valid.
3519 The cache manager rechecks the server lock once every minute and
3520 extends it as appropriate. If this is not done for 5 minutes,
3521 the AFS file server will release the lock (the 5 minute timeout
3522 is based on current file server code and is fairly arbitrary).
3523 Once released, the lock cannot be re-obtained without verifying
3524 that the contents of the file hasn't been modified since the
3525 time the lock was released. Re-obtaining the lock without
3526 verifying this may lead to data corruption. If the lock can not
3527 be obtained safely, then all active locks for the cm_scache_t
3530 4.1 LOST->DELETED: The lock is released.
3532 5. DELETED: The lock is no longer relevant. Eventually, it will
3533 get removed from the cm_scache_t. In the meantime, it will be
3534 treated as if it does not exist.
3536 5.1 DELETED->not exist: The lock is removed from the
3539 The following are classifications of locks based on their state.
3541 6* A lock L is ACCEPTED if it is ACTIVE or WAITLOCK. These locks
3542 have been accepted by the cache manager, but may or may not have
3543 been granted back to the client.
3545 7* A lock L is QUEUED if it is ACTIVE, WAITLOCK or WAITUNLOCK.
3547 8* A lock L is WAITING if it is WAITLOCK or WAITUNLOCK.
3551 A client C can READ range (Offset,+Length) of a file represented by
3552 cm_scache_t S iff (1):
3554 1. for all _a_ in (Offset,+Length), all of the following is true:
3556 1.1 For each ACTIVE lock L in S->fileLocks such that _a_ in
3557 (L->LOffset,+L->LLength); L->key == Key(C) OR L->LockType is
3560 1.2 For each LOST lock L in S->fileLocks such that _a_ in
3561 (L->LOffset,+L->LLength); L->LockType is shared AND L->key !=
3564 (When locks are lost on an cm_scache_t, all locks are lost. By
3565 4.2 (below), if there is an exclusive LOST lock, then there
3566 can't be any overlapping ACTIVE locks.)
3568 A client C can WRITE range (Offset,+Length) of cm_scache_t S iff (2):
3570 2. for all _a_ in (Offset,+Length), one of the following is true:
3572 2.1 Byte _a_ of S is unowned (as specified in 1.1) AND there
3573 does not exist a LOST lock L such that _a_ in
3574 (L->LOffset,+L->LLength).
3576 2.2 Byte _a_ of S is owned by C under lock L (as specified in
3577 1.2) AND L->LockType is exclusive.
3579 A client C can OBTAIN a lock L on cm_scache_t S iff (both 3 and 4):
3581 3. for all _a_ in (L->LOffset,+L->LLength), ALL of the following is
3584 3.1 If L->LockType is exclusive then there does NOT exist a
3585 ACCEPTED lock M in S->fileLocks such that _a_ in
3586 (M->LOffset,+M->LLength).
3588 (If we count all QUEUED locks then we hit cases such as
3589 cascading waiting locks where the locks later on in the queue
3590 can be granted without compromising file integrity. On the
3591 other hand if only ACCEPTED locks are considered, then locks
3592 that were received earlier may end up waiting for locks that
3593 were received later to be unlocked. The choice of ACCEPTED
3594 locks was made to mimic the Windows byte range lock
3597 3.2 If L->LockType is shared then for each ACCEPTED lock M in
3598 S->fileLocks, if _a_ in (M->LOffset,+M->LLength) then
3599 M->LockType is shared.
3601 4. For all LOST locks M in S->fileLocks, ALL of the following are true:
3603 4.1 M->key != Key(C)
3605 4.2 If M->LockType is exclusive, then (L->LOffset,+L->LLength)
3606 and (M->LOffset,+M->LLength) do not intersect.
3608 (Note: If a client loses a lock, it loses all locks.
3609 Subsequently, it will not be allowed to obtain any more locks
3610 until all existing LOST locks that belong to the client are
3611 released. Once all locks are released by a single client,
3612 there exists no further contract between the client and AFS
3613 about the contents of the file, hence the client can then
3614 proceed to obtain new locks and establish a new contract.
3616 This doesn't quite work as you think it should, because most
3617 applications aren't built to deal with losing locks they
3618 thought they once had. For now, we don't have a good
3619 solution to lost locks.
3621 Also, for consistency reasons, we have to hold off on
3622 granting locks that overlap exclusive LOST locks.)
3624 A client C can only unlock locks L in S->fileLocks which have
3627 The representation and invariants are as follows:
3629 - Each cm_scache_t structure keeps:
3631 - A queue of byte-range locks (cm_scache_t::fileLocks) which
3632 are of type cm_file_lock_t.
3634 - A record of the highest server-side lock that has been
3635 obtained for this object (cm_scache_t::serverLock), which is
3636 one of (-1), LockRead, LockWrite.
3638 - A count of ACCEPTED exclusive and shared locks that are in the
3639 queue (cm_scache_t::sharedLocks and
3640 cm_scache_t::exclusiveLocks)
3642 - Each cm_file_lock_t structure keeps:
3644 - The type of lock (cm_file_lock_t::LockType)
3646 - The key associated with the lock (cm_file_lock_t::key)
3648 - The offset and length of the lock (cm_file_lock_t::LOffset
3649 and cm_file_lock_t::LLength)
3651 - The state of the lock.
3653 - Time of issuance or last successful extension
3655 Semantic invariants:
3657 I1. The number of ACCEPTED locks in S->fileLocks are
3658 (S->sharedLocks + S->exclusiveLocks)
3660 External invariants:
3662 I3. S->serverLock is the lock that we have asserted with the
3663 AFS file server for this cm_scache_t.
3665 I4. S->serverLock == LockRead iff there is at least one ACTIVE
3666 shared lock, but no ACTIVE exclusive locks.
3668 I5. S->serverLock == LockWrite iff there is at least one ACTIVE
3671 I6. If L is a LOST lock, then for each lock M in S->fileLocks,
3672 M->key == L->key IMPLIES M is LOST or DELETED.
3677 #define IS_LOCK_ACTIVE(lockp) (((lockp)->flags & (CM_FILELOCK_FLAG_DELETED|CM_FILELOCK_FLAG_WAITLOCK|CM_FILELOCK_FLAG_WAITUNLOCK|CM_FILELOCK_FLAG_LOST)) == 0)
3679 #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)
3681 #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)
3683 #define IS_LOCK_LOST(lockp) (((lockp)->flags & (CM_FILELOCK_FLAG_DELETED|CM_FILELOCK_FLAG_LOST)) == CM_FILELOCK_FLAG_LOST)
3685 #define IS_LOCK_DELETED(lockp) (((lockp)->flags & CM_FILELOCK_FLAG_DELETED) == CM_FILELOCK_FLAG_DELETED)
3688 #define IS_LOCK_ACCEPTED(lockp) (IS_LOCK_ACTIVE(lockp) || IS_LOCK_WAITLOCK(lockp))
3691 #define IS_LOCK_CLIENTONLY(lockp) ((((lockp)->scp->flags & CM_SCACHEFLAG_RO) == CM_SCACHEFLAG_RO) || (((lockp)->flags & CM_FILELOCK_FLAG_CLIENTONLY) == CM_FILELOCK_FLAG_CLIENTONLY))
3694 #define INTERSECT_RANGE(r1,r2) (((r2).offset+(r2).length) > (r1).offset && ((r1).offset +(r1).length) > (r2).offset)
3697 #define CONTAINS_RANGE(r1,r2) (((r2).offset+(r2).length) <= ((r1).offset+(r1).length) && (r1).offset <= (r2).offset)
3699 #if defined(VICED_CAPABILITY_USE_BYTE_RANGE_LOCKS) && !defined(LOCK_TESTING)
3700 #define SCP_SUPPORTS_BRLOCKS(scp) ((scp)->cbServerp && ((scp)->cbServerp->capabilities & VICED_CAPABILITY_USE_BYTE_RANGE_LOCKS))
3702 #define SCP_SUPPORTS_BRLOCKS(scp) (1)
3705 #define SERVERLOCKS_ENABLED(scp) (!((scp)->flags & CM_SCACHEFLAG_RO) && cm_enableServerLocks && SCP_SUPPORTS_BRLOCKS(scp))
3707 #if defined(VICED_CAPABILITY_WRITELOCKACL)
3708 #define SCP_SUPPORTS_WRITELOCKACL(scp) ((scp)->cbServerp && ((scp->cbServerp->capabilities & VICED_CAPABILITY_WRITELOCKACL)))
3710 #define SCP_SUPPORTS_WRITELOCKACL(scp) (0)
3712 /* This should really be defined in any build that this code is being
3714 #error VICED_CAPABILITY_WRITELOCKACL not defined.
3717 static void cm_LockRangeSubtract(cm_range_t * pos, const cm_range_t * neg)
3719 afs_int64 int_begin;
3722 int_begin = MAX(pos->offset, neg->offset);
3723 int_end = MIN(pos->offset+pos->length, neg->offset+neg->length);
3725 if (int_begin < int_end) {
3726 if (int_begin == pos->offset) {
3727 pos->length = pos->offset + pos->length - int_end;
3728 pos->offset = int_end;
3729 } else if (int_end == pos->offset + pos->length) {
3730 pos->length = int_begin - pos->offset;
3733 /* We only subtract ranges if the resulting range is
3734 contiguous. If we try to support non-contigous ranges, we
3735 aren't actually improving performance. */
3739 /* Called with scp->mx held. Returns 0 if all is clear to read the
3740 specified range by the client identified by key.
3742 long cm_LockCheckRead(cm_scache_t *scp,
3743 LARGE_INTEGER LOffset,
3744 LARGE_INTEGER LLength,
3747 #ifndef ADVISORY_LOCKS
3749 cm_file_lock_t *fileLock;
3753 int substract_ranges = FALSE;
3755 range.offset = LOffset.QuadPart;
3756 range.length = LLength.QuadPart;
3760 1. for all _a_ in (Offset,+Length), all of the following is true:
3762 1.1 For each ACTIVE lock L in S->fileLocks such that _a_ in
3763 (L->LOffset,+L->LLength); L->key == Key(C) OR L->LockType is
3766 1.2 For each LOST lock L in S->fileLocks such that _a_ in
3767 (L->LOffset,+L->LLength); L->LockType is shared AND L->key !=
3772 lock_ObtainRead(&cm_scacheLock);
3774 for (q = scp->fileLocksH; q && range.length > 0; q = osi_QNext(q)) {
3776 (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
3778 if (INTERSECT_RANGE(range, fileLock->range)) {
3779 if (IS_LOCK_ACTIVE(fileLock)) {
3780 if (fileLock->key == key) {
3782 /* If there is an active lock for this client, it
3783 is safe to substract ranges.*/
3784 cm_LockRangeSubtract(&range, &fileLock->range);
3785 substract_ranges = TRUE;
3787 if (fileLock->lockType != LockRead) {
3788 code = CM_ERROR_LOCK_CONFLICT;
3792 /* even if the entire range is locked for reading,
3793 we still can't grant the lock at this point
3794 because the client may have lost locks. That
3795 is, unless we have already seen an active lock
3796 belonging to the client, in which case there
3797 can't be any lost locks for this client. */
3798 if (substract_ranges)
3799 cm_LockRangeSubtract(&range, &fileLock->range);
3801 } else if (IS_LOCK_LOST(fileLock) &&
3802 (fileLock->key == key || fileLock->lockType == LockWrite)) {
3803 code = CM_ERROR_BADFD;
3809 lock_ReleaseRead(&cm_scacheLock);
3811 osi_Log4(afsd_logp, "cm_LockCheckRead scp 0x%x offset %d length %d code 0x%x",
3812 scp, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart, code);
3823 /* Called with scp->mx held. Returns 0 if all is clear to write the
3824 specified range by the client identified by key.
3826 long cm_LockCheckWrite(cm_scache_t *scp,
3827 LARGE_INTEGER LOffset,
3828 LARGE_INTEGER LLength,
3831 #ifndef ADVISORY_LOCKS
3833 cm_file_lock_t *fileLock;
3838 range.offset = LOffset.QuadPart;
3839 range.length = LLength.QuadPart;
3842 A client C can WRITE range (Offset,+Length) of cm_scache_t S iff (2):
3844 2. for all _a_ in (Offset,+Length), one of the following is true:
3846 2.1 Byte _a_ of S is unowned AND there does not exist a LOST
3847 lock L such that _a_ in (L->LOffset,+L->LLength).
3849 2.2 Byte _a_ of S is owned by C under lock L AND L->LockType is
3853 lock_ObtainRead(&cm_scacheLock);
3855 for (q = scp->fileLocksH; q && range.length > 0; q = osi_QNext(q)) {
3857 (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
3859 if (INTERSECT_RANGE(range, fileLock->range)) {
3860 if (IS_LOCK_ACTIVE(fileLock)) {
3861 if (fileLock->key == key) {
3862 if (fileLock->lockType == LockWrite) {
3864 /* if there is an active lock for this client, it
3865 is safe to substract ranges */
3866 cm_LockRangeSubtract(&range, &fileLock->range);
3868 code = CM_ERROR_LOCK_CONFLICT;
3872 code = CM_ERROR_LOCK_CONFLICT;
3875 } else if (IS_LOCK_LOST(fileLock)) {
3876 code = CM_ERROR_BADFD;
3882 lock_ReleaseRead(&cm_scacheLock);
3884 osi_Log4(afsd_logp, "cm_LockCheckWrite scp 0x%x offset %d length %d code 0x%x",
3885 scp, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart, code);
3897 static void cm_LockMarkSCacheLost(cm_scache_t * scp);
3899 /* Called with cm_scacheLock write locked */
3900 static cm_file_lock_t * cm_GetFileLock(void) {
3903 l = (cm_file_lock_t *) cm_freeFileLocks;
3905 osi_QRemove(&cm_freeFileLocks, &l->q);
3907 l = malloc(sizeof(cm_file_lock_t));
3908 osi_assertx(l, "null cm_file_lock_t");
3911 memset(l, 0, sizeof(cm_file_lock_t));
3916 /* Called with cm_scacheLock write locked */
3917 static void cm_PutFileLock(cm_file_lock_t *l) {
3918 osi_QAdd(&cm_freeFileLocks, &l->q);
3921 /* called with scp->mx held. May release it during processing, but
3922 leaves it held on exit. */
3923 long cm_IntSetLock(cm_scache_t * scp, cm_user_t * userp, int lockType,
3929 struct rx_connection * callp;
3932 tfid.Volume = scp->fid.volume;
3933 tfid.Vnode = scp->fid.vnode;
3934 tfid.Unique = scp->fid.unique;
3937 osi_Log2(afsd_logp, "CALL SetLock scp 0x%p for lock %d", scp, lockType);
3939 lock_ReleaseMutex(&scp->mx);
3942 code = cm_ConnFromFID(&cfid, userp, reqp, &connp);
3946 callp = cm_GetRxConn(connp);
3947 code = RXAFS_SetLock(callp, &tfid, lockType,
3949 rx_PutConnection(callp);
3951 } while (cm_Analyze(connp, userp, reqp, &cfid, &volSync,
3954 code = cm_MapRPCError(code, reqp);
3956 osi_Log1(afsd_logp, "CALL SetLock FAILURE, code 0x%x", code);
3958 osi_Log0(afsd_logp, "CALL SetLock SUCCESS");
3961 lock_ObtainMutex(&scp->mx);
3966 /* called with scp->mx held. Releases it during processing */
3967 long cm_IntReleaseLock(cm_scache_t * scp, cm_user_t * userp,
3973 struct rx_connection * callp;
3976 tfid.Volume = scp->fid.volume;
3977 tfid.Vnode = scp->fid.vnode;
3978 tfid.Unique = scp->fid.unique;
3981 lock_ReleaseMutex(&scp->mx);
3983 osi_Log1(afsd_logp, "CALL ReleaseLock scp 0x%p", scp);
3986 code = cm_ConnFromFID(&cfid, userp, reqp, &connp);
3990 callp = cm_GetRxConn(connp);
3991 code = RXAFS_ReleaseLock(callp, &tfid, &volSync);
3992 rx_PutConnection(callp);
3994 } while (cm_Analyze(connp, userp, reqp, &cfid, &volSync,
3996 code = cm_MapRPCError(code, reqp);
3999 "CALL ReleaseLock FAILURE, code 0x%x", code);
4002 "CALL ReleaseLock SUCCESS");
4004 lock_ObtainMutex(&scp->mx);
4009 /* called with scp->mx held. May release it during processing, but
4010 will exit with lock held.
4014 - 0 if the user has permission to get the specified lock for the scp
4016 - CM_ERROR_NOACCESS if not
4018 Any other error from cm_SyncOp will be sent down untranslated.
4020 If CM_ERROR_NOACCESS is returned and lock_type is LockRead, then
4021 phas_insert (if non-NULL) will receive a boolean value indicating
4022 whether the user has INSERT permission or not.
4024 long cm_LockCheckPerms(cm_scache_t * scp,
4031 long code = 0, code2 = 0;
4033 /* lock permissions are slightly tricky because of the 'i' bit.
4034 If the user has PRSFS_LOCK, she can read-lock the file. If the
4035 user has PRSFS_WRITE, she can write-lock the file. However, if
4036 the user has PRSFS_INSERT, then she can write-lock new files,
4037 but not old ones. Since we don't have information about
4038 whether a file is new or not, we assume that if the user owns
4039 the scp, then she has the permissions that are granted by
4042 osi_Log3(afsd_logp, "cm_LockCheckPerms for scp[0x%p] type[%d] user[0x%p]",
4043 scp, lock_type, userp);
4045 if (lock_type == LockRead)
4046 rights |= PRSFS_LOCK;
4047 else if (lock_type == LockWrite)
4048 rights |= PRSFS_WRITE | PRSFS_LOCK;
4051 osi_assertx(FALSE, "invalid lock type");
4056 *phas_insert = FALSE;
4058 code = cm_SyncOp(scp, NULL, userp, reqp, rights,
4059 CM_SCACHESYNC_GETSTATUS |
4060 CM_SCACHESYNC_NEEDCALLBACK);
4062 if (phas_insert && scp->creator == userp) {
4064 /* If this file was created by the user, then we check for
4065 PRSFS_INSERT. If the file server is recent enough, then
4066 this should be sufficient for her to get a write-lock (but
4067 not necessarily a read-lock). VICED_CAPABILITY_WRITELOCKACL
4068 indicates whether a file server supports getting write
4069 locks when the user only has PRSFS_INSERT.
4071 If the file was not created by the user we skip the check
4072 because the INSERT bit will not apply to this user even
4076 code2 = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
4077 CM_SCACHESYNC_GETSTATUS |
4078 CM_SCACHESYNC_NEEDCALLBACK);
4080 if (code2 == CM_ERROR_NOACCESS) {
4081 osi_Log0(afsd_logp, "cm_LockCheckPerms user has no INSERT bits");
4083 *phas_insert = TRUE;
4084 osi_Log0(afsd_logp, "cm_LockCheckPerms user has INSERT bits");
4088 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
4090 osi_Log1(afsd_logp, "cm_LockCheckPerms returning code %d", code);
4095 /* called with scp->mx held */
4096 long cm_Lock(cm_scache_t *scp, unsigned char sLockType,
4097 LARGE_INTEGER LOffset, LARGE_INTEGER LLength,
4099 int allowWait, cm_user_t *userp, cm_req_t *reqp,
4100 cm_file_lock_t **lockpp)
4103 int Which = ((sLockType & LOCKING_ANDX_SHARED_LOCK) ? LockRead : LockWrite);
4104 cm_file_lock_t *fileLock;
4107 int wait_unlock = FALSE;
4108 int force_client_lock = FALSE;
4110 osi_Log4(afsd_logp, "cm_Lock scp 0x%x type 0x%x offset %d length %d",
4111 scp, sLockType, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart);
4112 osi_Log3(afsd_logp, "... allowWait %d key 0x%x:%x", allowWait,
4113 (unsigned long)(key >> 32), (unsigned long)(key & 0xffffffff));
4116 A client C can OBTAIN a lock L on cm_scache_t S iff (both 3 and 4):
4118 3. for all _a_ in (L->LOffset,+L->LLength), ALL of the following is
4121 3.1 If L->LockType is exclusive then there does NOT exist a
4122 ACCEPTED lock M in S->fileLocks such that _a_ in
4123 (M->LOffset,+M->LLength).
4125 3.2 If L->LockType is shared then for each ACCEPTED lock M in
4126 S->fileLocks, if _a_ in (M->LOffset,+M->LLength) then
4127 M->LockType is shared.
4129 4. For all LOST locks M in S->fileLocks, ALL of the following are true:
4131 4.1 M->key != Key(C)
4133 4.2 If M->LockType is exclusive, then (L->LOffset,+L->LLength)
4134 and (M->LOffset,+M->LLength) do not intersect.
4137 range.offset = LOffset.QuadPart;
4138 range.length = LLength.QuadPart;
4140 lock_ObtainRead(&cm_scacheLock);
4142 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
4144 (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
4146 if (IS_LOCK_LOST(fileLock)) {
4147 if (fileLock->key == key) {
4148 code = CM_ERROR_BADFD;
4150 } else if (fileLock->lockType == LockWrite && INTERSECT_RANGE(range, fileLock->range)) {
4151 code = CM_ERROR_WOULDBLOCK;
4157 /* we don't need to check for deleted locks here since deleted
4158 locks are dequeued from scp->fileLocks */
4159 if (IS_LOCK_ACCEPTED(fileLock) &&
4160 INTERSECT_RANGE(range, fileLock->range)) {
4162 if ((sLockType & LOCKING_ANDX_SHARED_LOCK) == 0 ||
4163 fileLock->lockType != LockRead) {
4165 code = CM_ERROR_WOULDBLOCK;
4171 lock_ReleaseRead(&cm_scacheLock);
4173 if (code == 0 && SERVERLOCKS_ENABLED(scp)) {
4174 if (Which == scp->serverLock ||
4175 (Which == LockRead && scp->serverLock == LockWrite)) {
4179 /* we already have the lock we need */
4180 osi_Log3(afsd_logp, " we already have the correct lock. exclusives[%d], shared[%d], serverLock[%d]",
4181 scp->exclusiveLocks, scp->sharedLocks, (int)(signed char) scp->serverLock);
4183 code = cm_LockCheckPerms(scp, Which, userp, reqp, &has_insert);
4185 /* special case: if we don't have permission to read-lock
4186 the file, then we force a clientside lock. This is to
4187 compensate for applications that obtain a read-lock for
4188 reading files off of directories that don't grant
4189 read-locks to the user. */
4190 if (code == CM_ERROR_NOACCESS && Which == LockRead) {
4192 if (has_insert && SCP_SUPPORTS_WRITELOCKACL(scp)) {
4193 osi_Log0(afsd_logp, " User has no read-lock perms, but has INSERT perms.");
4196 osi_Log0(afsd_logp, " User has no read-lock perms. Forcing client-side lock");
4197 force_client_lock = TRUE;
4201 } else if ((scp->exclusiveLocks > 0) ||
4202 (scp->sharedLocks > 0 && scp->serverLock != LockRead)) {
4205 /* We are already waiting for some other lock. We should
4206 wait for the daemon to catch up instead of generating a
4207 flood of SetLock calls. */
4208 osi_Log3(afsd_logp, " already waiting for other lock. exclusives[%d], shared[%d], serverLock[%d]",
4209 scp->exclusiveLocks, scp->sharedLocks, (int)(signed char) scp->serverLock);
4211 /* see if we have permission to create the lock in the
4213 code = cm_LockCheckPerms(scp, Which, userp, reqp, &has_insert);
4215 code = CM_ERROR_WOULDBLOCK;
4216 else if (code == CM_ERROR_NOACCESS && Which == LockRead) {
4218 if (has_insert && SCP_SUPPORTS_WRITELOCKACL(scp)) {
4220 " User has no read-lock perms, but has INSERT perms.");
4221 code = CM_ERROR_WOULDBLOCK;
4224 " User has no read-lock perms. Forcing client-side lock");
4225 force_client_lock = TRUE;
4229 /* leave any other codes as-is */
4233 int check_data_version = FALSE;
4236 /* first check if we have permission to elevate or obtain
4238 code = cm_LockCheckPerms(scp, Which, userp, reqp, &has_insert);
4240 if (code == CM_ERROR_NOACCESS && Which == LockRead &&
4241 (!has_insert || !SCP_SUPPORTS_WRITELOCKACL(scp))) {
4242 osi_Log0(afsd_logp, " User has no read-lock perms. Forcing client-side lock");
4243 force_client_lock = TRUE;
4248 /* has_insert => (Which == LockRead, code == CM_ERROR_NOACCESS) */
4250 if (scp->serverLock == LockRead && Which == LockWrite) {
4252 /* We want to escalate the lock to a LockWrite.
4253 * Unfortunately that's not really possible without
4254 * letting go of the current lock. But for now we do
4258 " attempting to UPGRADE from LockRead to LockWrite.");
4260 " dataVersion on scp: %I64d", scp->dataVersion);
4262 /* we assume at this point (because scp->serverLock
4263 was valid) that we had a valid server lock. */
4264 scp->lockDataVersion = scp->dataVersion;
4265 check_data_version = TRUE;
4267 code = cm_IntReleaseLock(scp, userp, reqp);
4270 /* We couldn't release the lock */
4273 scp->serverLock = -1;
4277 /* We need to obtain a server lock of type Which in order
4278 * to assert this file lock */
4279 #ifndef AGGRESSIVE_LOCKS
4282 newLock = LockWrite;
4285 code = cm_IntSetLock(scp, userp, newLock, reqp);
4287 #ifdef AGGRESSIVE_LOCKS
4288 if ((code == CM_ERROR_WOULDBLOCK ||
4289 code == CM_ERROR_NOACCESS) && newLock != Which) {
4290 /* we wanted LockRead. We tried LockWrite. Now try
4295 osi_assertx(newLock == LockRead, "lock type not read");
4297 code = cm_IntSetLock(scp, userp, newLock, reqp);
4301 if (code == CM_ERROR_NOACCESS) {
4302 if (Which == LockRead) {
4303 if (has_insert && SCP_SUPPORTS_WRITELOCKACL(scp)) {
4305 /* We requested a read-lock, but we have permission to
4306 * get a write-lock. Try that */
4308 tcode = cm_LockCheckPerms(scp, LockWrite, userp, reqp, NULL);
4311 newLock = LockWrite;
4313 osi_Log0(afsd_logp, " User has 'i' perms and the request was for a LockRead. Trying to get a LockWrite instead");
4315 code = cm_IntSetLock(scp, userp, newLock, reqp);
4318 osi_Log0(afsd_logp, " User has no read-lock perms. Forcing client-side lock");
4319 force_client_lock = TRUE;
4321 } else if (Which == LockWrite &&
4322 scp->creator == userp && !SCP_SUPPORTS_WRITELOCKACL(scp)) {
4325 /* Special case: if the lock request was for a
4326 * LockWrite and the user owns the file and we weren't
4327 * allowed to obtain the serverlock, we either lost a
4328 * race (the permissions changed from under us), or we
4329 * have 'i' bits, but we aren't allowed to lock the
4332 /* check if we lost a race... */
4333 tcode = cm_LockCheckPerms(scp, Which, userp, reqp, NULL);
4336 osi_Log0(afsd_logp, " User has 'i' perms but can't obtain write locks. Using client-side locks.");
4337 force_client_lock = TRUE;
4342 if (code == 0 && check_data_version &&
4343 scp->dataVersion != scp->lockDataVersion) {
4344 /* We lost a race. Although we successfully obtained
4345 * a lock, someone modified the file in between. The
4346 * locks have all been technically lost. */
4349 " Data version mismatch while upgrading lock.");
4351 " Data versions before=%I64d, after=%I64d",
4352 scp->lockDataVersion,
4355 " Releasing stale lock for scp 0x%x", scp);
4357 code = cm_IntReleaseLock(scp, userp, reqp);
4359 scp->serverLock = -1;
4361 code = CM_ERROR_INVAL;
4362 } else if (code == 0) {
4363 scp->serverLock = newLock;
4364 scp->lockDataVersion = scp->dataVersion;
4368 (scp->sharedLocks > 0 || scp->exclusiveLocks > 0) &&
4369 scp->serverLock == -1) {
4370 /* Oops. We lost the lock. */
4371 cm_LockMarkSCacheLost(scp);
4374 } else if (code == 0) { /* server locks not enabled */
4376 " Skipping server lock for scp");
4381 if (code != 0 && !force_client_lock) {
4382 /* Special case error translations
4384 Applications don't expect certain errors from a
4385 LockFile/UnlockFile call. We need to translate some error
4386 code to codes that apps expect and handle. */
4388 /* We shouldn't actually need to handle this case since we
4389 simulate locks for RO scps anyway. */
4390 if (code == CM_ERROR_READONLY) {
4391 osi_Log0(afsd_logp, " Reinterpreting CM_ERROR_READONLY as CM_ERROR_NOACCESS");
4392 code = CM_ERROR_NOACCESS;
4396 if (code == 0 || (code == CM_ERROR_WOULDBLOCK && allowWait) ||
4397 force_client_lock) {
4399 /* clear the error if we are forcing a client lock, so we
4400 don't get confused later. */
4401 if (force_client_lock && code != CM_ERROR_WOULDBLOCK)
4404 lock_ObtainWrite(&cm_scacheLock);
4405 fileLock = cm_GetFileLock();
4406 lock_ReleaseWrite(&cm_scacheLock);
4408 fileLock->fid = scp->fid;
4410 fileLock->key = key;
4411 fileLock->lockType = Which;
4413 fileLock->userp = userp;
4414 fileLock->range = range;
4415 fileLock->flags = (code == 0 ? 0 :
4417 CM_FILELOCK_FLAG_WAITUNLOCK :
4418 CM_FILELOCK_FLAG_WAITLOCK));
4420 if (force_client_lock || !SERVERLOCKS_ENABLED(scp))
4421 fileLock->flags |= CM_FILELOCK_FLAG_CLIENTONLY;
4423 fileLock->lastUpdate = (code == 0 && !force_client_lock) ? time(NULL) : 0;
4425 lock_ObtainWrite(&cm_scacheLock);
4426 osi_QAddT(&scp->fileLocksH, &scp->fileLocksT, &fileLock->fileq);
4427 cm_HoldSCacheNoLock(scp);
4428 fileLock->scp = scp;
4429 osi_QAdd(&cm_allFileLocks, &fileLock->q);
4430 lock_ReleaseWrite(&cm_scacheLock);
4436 if (IS_LOCK_CLIENTONLY(fileLock)) {
4438 } else if (IS_LOCK_ACCEPTED(fileLock)) {
4439 if (Which == LockRead)
4442 scp->exclusiveLocks++;
4446 "cm_Lock Lock added 0x%p flags 0x%x to scp [0x%p]",
4447 fileLock, fileLock->flags, scp);
4449 " exclusives[%d] shared[%d] client[%d] serverLock[%d]",
4450 scp->exclusiveLocks, scp->sharedLocks, scp->clientLocks,
4451 (int)(signed char) scp->serverLock);
4454 "cm_Lock Rejecting lock (code = 0x%x)", code);
4460 static int cm_KeyEquals(cm_key_t k1, cm_key_t k2, int flags);
4462 /* Called with scp->mx held */
4463 long cm_UnlockByKey(cm_scache_t * scp,
4470 cm_file_lock_t *fileLock;
4471 osi_queue_t *q, *qn;
4474 osi_Log4(afsd_logp, "cm_UnlockByKey scp 0x%p key 0x%x:%x flags=0x%x",
4476 (unsigned long)(key >> 32),
4477 (unsigned long)(key & 0xffffffff),
4480 lock_ObtainWrite(&cm_scacheLock);
4482 for (q = scp->fileLocksH; q; q = qn) {
4485 fileLock = (cm_file_lock_t *)
4486 ((char *) q - offsetof(cm_file_lock_t, fileq));
4489 osi_Log4(afsd_logp, " Checking lock[0x%x] range[%d,+%d] type[%d]",
4491 (unsigned long) fileLock->range.offset,
4492 (unsigned long) fileLock->range.length,
4493 fileLock->lockType);
4494 osi_Log3(afsd_logp, " key[0x%x:%x] flags[0x%x]",
4495 (unsigned long)(fileLock->key >> 32),
4496 (unsigned long)(fileLock->key & 0xffffffff),
4499 if (cm_FidCmp(&fileLock->fid, &fileLock->scp->fid)) {
4500 osi_Log0(afsd_logp, "!!fileLock->fid != scp->fid");
4501 osi_Log4(afsd_logp, " fileLock->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4503 fileLock->fid.volume,
4504 fileLock->fid.vnode,
4505 fileLock->fid.unique);
4506 osi_Log4(afsd_logp, " scp->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4507 fileLock->scp->fid.cell,
4508 fileLock->scp->fid.volume,
4509 fileLock->scp->fid.vnode,
4510 fileLock->scp->fid.unique);
4511 osi_assertx(FALSE, "invalid fid value");
4515 if (!IS_LOCK_DELETED(fileLock) &&
4516 cm_KeyEquals(fileLock->key, key, flags)) {
4517 osi_Log3(afsd_logp, "...Unlock range [%d,+%d] type %d",
4518 fileLock->range.offset,
4519 fileLock->range.length,
4520 fileLock->lockType);
4522 if (scp->fileLocksT == q)
4523 scp->fileLocksT = osi_QPrev(q);
4524 osi_QRemoveHT(&scp->fileLocksH, &scp->fileLocksT, q);
4526 if (IS_LOCK_CLIENTONLY(fileLock)) {
4528 } else if (IS_LOCK_ACCEPTED(fileLock)) {
4529 if (fileLock->lockType == LockRead)
4532 scp->exclusiveLocks--;
4535 fileLock->flags |= CM_FILELOCK_FLAG_DELETED;
4537 cm_ReleaseUser(fileLock->userp);
4538 cm_ReleaseSCacheNoLock(scp);
4540 fileLock->userp = NULL;
4541 fileLock->scp = NULL;
4547 lock_ReleaseWrite(&cm_scacheLock);
4549 if (n_unlocks == 0) {
4550 osi_Log0(afsd_logp, "cm_UnlockByKey no locks found");
4551 osi_Log3(afsd_logp, " Leaving scp with exclusives[%d], shared[%d], serverLock[%d]",
4552 scp->exclusiveLocks, scp->sharedLocks, (int)(signed char) scp->serverLock);
4557 osi_Log1(afsd_logp, "cm_UnlockByKey done with %d locks", n_unlocks);
4559 osi_assertx(scp->sharedLocks >= 0, "scp->sharedLocks < 0");
4560 osi_assertx(scp->exclusiveLocks >= 0, "scp->exclusiveLocks < 0");
4561 osi_assertx(scp->clientLocks >= 0, "scp->clientLocks < 0");
4563 if (!SERVERLOCKS_ENABLED(scp)) {
4564 osi_Log0(afsd_logp, " Skipping server lock for scp");
4568 /* Ideally we would go through the rest of the locks to determine
4569 * if one or more locks that were formerly in WAITUNLOCK can now
4570 * be put to ACTIVE or WAITLOCK and update scp->exclusiveLocks and
4571 * scp->sharedLocks accordingly. However, the retrying of locks
4572 * in that manner is done cm_RetryLock() manually.
4575 if (scp->serverLock == LockWrite &&
4576 scp->exclusiveLocks == 0 &&
4577 scp->sharedLocks > 0) {
4579 /* The serverLock should be downgraded to LockRead */
4580 osi_Log0(afsd_logp, " DOWNGRADE lock from LockWrite to LockRead");
4582 /* since scp->serverLock looked sane, we are going to assume
4583 that we have a valid server lock. */
4584 scp->lockDataVersion = scp->dataVersion;
4585 osi_Log1(afsd_logp, " dataVersion on scp = %I64d", scp->dataVersion);
4587 code = cm_IntReleaseLock(scp, userp, reqp);
4590 /* so we couldn't release it. Just let the lock be for now */
4594 scp->serverLock = -1;
4597 code = cm_IntSetLock(scp, userp, LockRead, reqp);
4599 if (code == 0 && scp->lockDataVersion == scp->dataVersion) {
4600 scp->serverLock = LockRead;
4601 } else if (code == 0 && scp->lockDataVersion != scp->dataVersion) {
4602 /* We lost a race condition. Although we have a valid
4603 lock on the file, the data has changed and essentially
4604 we have lost the lock we had during the transition. */
4606 osi_Log0(afsd_logp, "Data version mismatch during lock downgrade");
4607 osi_Log2(afsd_logp, " Data versions before=%I64d, after=%I64d",
4608 scp->lockDataVersion,
4611 code = cm_IntReleaseLock(scp, userp, reqp);
4613 code = CM_ERROR_INVAL;
4614 scp->serverLock = -1;
4618 (scp->sharedLocks > 0 || scp->exclusiveLocks > 0) &&
4619 (scp->serverLock == -1)) {
4621 cm_LockMarkSCacheLost(scp);
4624 /* failure here has no bearing on the return value of
4628 } else if (scp->serverLock != (-1) &&
4629 scp->exclusiveLocks == 0 &&
4630 scp->sharedLocks == 0) {
4631 /* The serverLock should be released entirely */
4633 code = cm_IntReleaseLock(scp, userp, reqp);
4636 scp->serverLock = (-1);
4641 osi_Log1(afsd_logp, "cm_UnlockByKey code 0x%x", code);
4642 osi_Log4(afsd_logp, " Leaving scp with excl[%d], shared[%d], client[%d], serverLock[%d]",
4643 scp->exclusiveLocks, scp->sharedLocks, scp->clientLocks,
4644 (int)(signed char) scp->serverLock);
4649 long cm_Unlock(cm_scache_t *scp,
4650 unsigned char sLockType,
4651 LARGE_INTEGER LOffset, LARGE_INTEGER LLength,
4657 int Which = ((sLockType & LOCKING_ANDX_SHARED_LOCK) ? LockRead : LockWrite);
4658 cm_file_lock_t *fileLock;
4660 int release_userp = FALSE;
4662 osi_Log4(afsd_logp, "cm_Unlock scp 0x%p type 0x%x offset %d length %d",
4663 scp, sLockType, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart);
4664 osi_Log2(afsd_logp, "... key 0x%x:%x",
4665 (unsigned long) (key >> 32), (unsigned long) (key & 0xffffffff));
4667 lock_ObtainRead(&cm_scacheLock);
4669 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
4670 fileLock = (cm_file_lock_t *)
4671 ((char *) q - offsetof(cm_file_lock_t, fileq));
4674 if (cm_FidCmp(&fileLock->fid, &fileLock->scp->fid)) {
4675 osi_Log0(afsd_logp, "!!fileLock->fid != scp->fid");
4676 osi_Log4(afsd_logp, " fileLock->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4678 fileLock->fid.volume,
4679 fileLock->fid.vnode,
4680 fileLock->fid.unique);
4681 osi_Log4(afsd_logp, " scp->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4682 fileLock->scp->fid.cell,
4683 fileLock->scp->fid.volume,
4684 fileLock->scp->fid.vnode,
4685 fileLock->scp->fid.unique);
4686 osi_assertx(FALSE, "invalid fid value");
4689 if (!IS_LOCK_DELETED(fileLock) &&
4690 fileLock->key == key &&
4691 fileLock->range.offset == LOffset.QuadPart &&
4692 fileLock->range.length == LLength.QuadPart) {
4698 osi_Log0(afsd_logp, "cm_Unlock lock not found; failure");
4700 lock_ReleaseRead(&cm_scacheLock);
4702 /* The lock didn't exist anyway. *shrug* */
4706 lock_ReleaseRead(&cm_scacheLock);
4708 /* discard lock record */
4709 lock_ObtainWrite(&cm_scacheLock);
4710 if (scp->fileLocksT == q)
4711 scp->fileLocksT = osi_QPrev(q);
4712 osi_QRemoveHT(&scp->fileLocksH, &scp->fileLocksT, q);
4715 * Don't delete it here; let the daemon delete it, to simplify
4716 * the daemon's traversal of the list.
4719 if (IS_LOCK_CLIENTONLY(fileLock)) {
4721 } else if (IS_LOCK_ACCEPTED(fileLock)) {
4722 if (fileLock->lockType == LockRead)
4725 scp->exclusiveLocks--;
4728 fileLock->flags |= CM_FILELOCK_FLAG_DELETED;
4729 if (userp != NULL) {
4730 cm_ReleaseUser(fileLock->userp);
4732 userp = fileLock->userp;
4733 release_userp = TRUE;
4735 fileLock->userp = NULL;
4736 cm_ReleaseSCacheNoLock(scp);
4737 fileLock->scp = NULL;
4738 lock_ReleaseWrite(&cm_scacheLock);
4740 if (!SERVERLOCKS_ENABLED(scp)) {
4741 osi_Log0(afsd_logp, " Skipping server locks for scp");
4745 /* Ideally we would go through the rest of the locks to determine
4746 * if one or more locks that were formerly in WAITUNLOCK can now
4747 * be put to ACTIVE or WAITLOCK and update scp->exclusiveLocks and
4748 * scp->sharedLocks accordingly. However, the retrying of locks
4749 * in that manner is done cm_RetryLock() manually.
4752 if (scp->serverLock == LockWrite &&
4753 scp->exclusiveLocks == 0 &&
4754 scp->sharedLocks > 0) {
4756 /* The serverLock should be downgraded to LockRead */
4757 osi_Log0(afsd_logp, " DOWNGRADE lock from LockWrite to LockRead");
4759 /* Since we already had a lock, we assume that there is a
4760 valid server lock. */
4761 scp->lockDataVersion = scp->dataVersion;
4762 osi_Log1(afsd_logp, " dataVersion on scp is %I64d", scp->dataVersion);
4764 /* before we downgrade, make sure that we have enough
4765 permissions to get the read lock. */
4766 code = cm_LockCheckPerms(scp, LockRead, userp, reqp, NULL);
4769 osi_Log0(afsd_logp, " SKIPPING downgrade because user doesn't have perms to get downgraded lock");
4775 code = cm_IntReleaseLock(scp, userp, reqp);
4778 /* so we couldn't release it. Just let the lock be for now */
4782 scp->serverLock = -1;
4785 code = cm_IntSetLock(scp, userp, LockRead, reqp);
4787 if (code == 0 && scp->lockDataVersion == scp->dataVersion) {
4788 scp->serverLock = LockRead;
4789 } else if (code == 0 && scp->lockDataVersion != scp->dataVersion) {
4790 /* Lost a race. We obtained a new lock, but that is
4791 meaningless since someone modified the file
4795 "Data version mismatch while downgrading lock");
4797 " Data versions before=%I64d, after=%I64d",
4798 scp->lockDataVersion,
4801 code = cm_IntReleaseLock(scp, userp, reqp);
4803 scp->serverLock = -1;
4804 code = CM_ERROR_INVAL;
4808 (scp->sharedLocks > 0 || scp->exclusiveLocks > 0) &&
4809 (scp->serverLock == -1)) {
4811 cm_LockMarkSCacheLost(scp);
4814 /* failure here has no bearing on the return value of
4818 } else if (scp->serverLock != (-1) &&
4819 scp->exclusiveLocks == 0 &&
4820 scp->sharedLocks == 0) {
4821 /* The serverLock should be released entirely */
4823 code = cm_IntReleaseLock(scp, userp, reqp);
4826 scp->serverLock = (-1);
4831 cm_ReleaseUser(userp);
4835 osi_Log1(afsd_logp, "cm_Unlock code 0x%x", code);
4836 osi_Log4(afsd_logp, " leaving scp with excl[%d], shared[%d], client[%d], serverLock[%d]",
4837 scp->exclusiveLocks, scp->sharedLocks, scp->clientLocks,
4838 (int)(signed char) scp->serverLock);
4843 /* called with scp->mx held */
4844 static void cm_LockMarkSCacheLost(cm_scache_t * scp)
4846 cm_file_lock_t *fileLock;
4849 osi_Log1(afsd_logp, "cm_LockMarkSCacheLost scp 0x%x", scp);
4852 /* With the current code, we can't lose a lock on a RO scp */
4853 osi_assertx(!(scp->flags & CM_SCACHEFLAG_RO), "CM_SCACHEFLAG_RO unexpected");
4856 /* cm_scacheLock needed because we are modifying fileLock->flags */
4857 lock_ObtainWrite(&cm_scacheLock);
4859 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
4861 (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
4863 if (IS_LOCK_ACTIVE(fileLock) &&
4864 !IS_LOCK_CLIENTONLY(fileLock)) {
4865 if (fileLock->lockType == LockRead)
4868 scp->exclusiveLocks--;
4870 fileLock->flags |= CM_FILELOCK_FLAG_LOST;
4874 scp->serverLock = -1;
4875 scp->lockDataVersion = -1;
4876 lock_ReleaseWrite(&cm_scacheLock);
4879 /* Called with no relevant locks held */
4880 void cm_CheckLocks()
4882 osi_queue_t *q, *nq;
4883 cm_file_lock_t *fileLock;
4889 struct rx_connection * callp;
4894 lock_ObtainWrite(&cm_scacheLock);
4896 cm_lockRefreshCycle++;
4898 osi_Log1(afsd_logp, "cm_CheckLocks starting lock check cycle %d", cm_lockRefreshCycle);
4900 for (q = cm_allFileLocks; q; q = nq) {
4901 fileLock = (cm_file_lock_t *) q;
4905 if (IS_LOCK_DELETED(fileLock)) {
4907 osi_QRemove(&cm_allFileLocks, q);
4908 cm_PutFileLock(fileLock);
4910 } else if (IS_LOCK_ACTIVE(fileLock) && !IS_LOCK_CLIENTONLY(fileLock)) {
4912 /* Server locks must have been enabled for us to have
4913 received an active non-client-only lock. */
4914 osi_assertx(cm_enableServerLocks, "!cm_enableServerLocks");
4916 scp = fileLock->scp;
4917 osi_assertx(scp != NULL, "null cm_scache_t");
4919 cm_HoldSCacheNoLock(scp);
4922 if (cm_FidCmp(&fileLock->fid, &fileLock->scp->fid)) {
4923 osi_Log0(afsd_logp, "!!fileLock->fid != scp->fid");
4924 osi_Log4(afsd_logp, " fileLock->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4926 fileLock->fid.volume,
4927 fileLock->fid.vnode,
4928 fileLock->fid.unique);
4929 osi_Log4(afsd_logp, " scp->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
4930 fileLock->scp->fid.cell,
4931 fileLock->scp->fid.volume,
4932 fileLock->scp->fid.vnode,
4933 fileLock->scp->fid.unique);
4934 osi_assertx(FALSE, "invalid fid");
4937 /* Server locks are extended once per scp per refresh
4939 if (scp->lastRefreshCycle != cm_lockRefreshCycle) {
4941 int scp_done = FALSE;
4943 osi_Log1(afsd_logp, "cm_CheckLocks Updating scp 0x%x", scp);
4945 lock_ReleaseWrite(&cm_scacheLock);
4946 lock_ObtainMutex(&scp->mx);
4948 /* did the lock change while we weren't holding the lock? */
4949 if (!IS_LOCK_ACTIVE(fileLock))
4950 goto post_syncopdone;
4952 code = cm_SyncOp(scp, NULL, fileLock->userp, &req, 0,
4953 CM_SCACHESYNC_NEEDCALLBACK
4954 | CM_SCACHESYNC_GETSTATUS
4955 | CM_SCACHESYNC_LOCK);
4959 "cm_CheckLocks SyncOp failure code 0x%x", code);
4960 goto post_syncopdone;
4963 /* cm_SyncOp releases scp->mx during which the lock
4964 may get released. */
4965 if (!IS_LOCK_ACTIVE(fileLock))
4966 goto pre_syncopdone;
4968 if (scp->serverLock != -1) {
4972 tfid.Volume = scp->fid.volume;
4973 tfid.Vnode = scp->fid.vnode;
4974 tfid.Unique = scp->fid.unique;
4976 userp = fileLock->userp;
4978 osi_Log3(afsd_logp, "CALL ExtendLock lock 0x%p for scp=0x%p with lock %d",
4981 (int) scp->serverLock);
4983 lock_ReleaseMutex(&scp->mx);
4986 code = cm_ConnFromFID(&cfid, userp,
4991 callp = cm_GetRxConn(connp);
4992 code = RXAFS_ExtendLock(callp, &tfid,
4994 rx_PutConnection(callp);
4996 osi_Log1(afsd_logp, " ExtendLock returns %d", code);
4998 } while (cm_Analyze(connp, userp, &req,
4999 &cfid, &volSync, NULL, NULL,
5002 code = cm_MapRPCError(code, &req);
5004 lock_ObtainMutex(&scp->mx);
5007 osi_Log1(afsd_logp, "CALL ExtendLock FAILURE, code 0x%x", code);
5009 osi_Log0(afsd_logp, "CALL ExtendLock SUCCESS");
5010 scp->lockDataVersion = scp->dataVersion;
5013 if ((code == EINVAL || code == CM_ERROR_INVAL) &&
5014 scp->lockDataVersion == scp->dataVersion) {
5018 (scp->exclusiveLocks > 0) ? LockWrite: LockRead;
5020 /* we might still have a chance to obtain a
5023 code = cm_IntSetLock(scp, userp, lockType, &req);
5026 code = CM_ERROR_INVAL;
5027 } else if (scp->lockDataVersion != scp->dataVersion) {
5029 /* now check if we still have the file at
5030 the right data version. */
5032 "Data version mismatch on scp 0x%p",
5035 " Data versions: before=%I64d, after=%I64d",
5036 scp->lockDataVersion,
5039 code = cm_IntReleaseLock(scp, userp, &req);
5041 code = CM_ERROR_INVAL;
5045 if (code == EINVAL || code == CM_ERROR_INVAL) {
5046 cm_LockMarkSCacheLost(scp);
5050 /* interestingly, we have found an active lock
5051 belonging to an scache that has no
5053 cm_LockMarkSCacheLost(scp);
5060 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
5063 lock_ReleaseMutex(&scp->mx);
5065 lock_ObtainWrite(&cm_scacheLock);
5068 fileLock->lastUpdate = time(NULL);
5072 scp->lastRefreshCycle = cm_lockRefreshCycle;
5075 /* we have already refreshed the locks on this scp */
5076 fileLock->lastUpdate = time(NULL);
5079 cm_ReleaseSCacheNoLock(scp);
5081 } else if (IS_LOCK_ACTIVE(fileLock) && IS_LOCK_CLIENTONLY(fileLock)) {
5082 /* TODO: Check callbacks */
5086 lock_ReleaseWrite(&cm_scacheLock);
5087 osi_Log1(afsd_logp, "cm_CheckLocks completes lock check cycle %d", cm_lockRefreshCycle);
5090 /* NOT called with scp->mx held. */
5091 long cm_RetryLock(cm_file_lock_t *oldFileLock, int client_is_dead)
5094 cm_scache_t *scp = NULL;
5095 cm_file_lock_t *fileLock;
5099 int force_client_lock = FALSE;
5100 int has_insert = FALSE;
5101 int check_data_version = FALSE;
5105 if (client_is_dead) {
5106 code = CM_ERROR_TIMEDOUT;
5110 lock_ObtainRead(&cm_scacheLock);
5112 osi_Log2(afsd_logp, "cm_RetryLock checking lock %p (scp=%p)", oldFileLock, oldFileLock->scp);
5113 osi_Log4(afsd_logp, " offset(%x:%x) length(%x:%x)",
5114 (unsigned)(oldFileLock->range.offset >> 32),
5115 (unsigned)(oldFileLock->range.offset & 0xffffffff),
5116 (unsigned)(oldFileLock->range.length >> 32),
5117 (unsigned)(oldFileLock->range.length & 0xffffffff));
5118 osi_Log3(afsd_logp, " key(%x:%x) flags=%x",
5119 (unsigned)(oldFileLock->key >> 32),
5120 (unsigned)(oldFileLock->key & 0xffffffff),
5121 (unsigned)(oldFileLock->flags));
5123 /* if the lock has already been granted, then we have nothing to do */
5124 if (IS_LOCK_ACTIVE(oldFileLock)) {
5125 lock_ReleaseRead(&cm_scacheLock);
5126 osi_Log0(afsd_logp, "cm_RetryLock lock already granted");
5130 /* we can't do anything with lost or deleted locks at the moment. */
5131 if (IS_LOCK_LOST(oldFileLock) || IS_LOCK_DELETED(oldFileLock)) {
5132 code = CM_ERROR_BADFD;
5133 osi_Log0(afsd_logp, "cm_RetryLock lock is lost or deleted");
5134 lock_ReleaseRead(&cm_scacheLock);
5138 scp = oldFileLock->scp;
5140 osi_assertx(scp != NULL, "null cm_scache_t");
5142 lock_ReleaseRead(&cm_scacheLock);
5143 lock_ObtainMutex(&scp->mx);
5145 code = cm_LockCheckPerms(scp, oldFileLock->lockType,
5149 if (code == CM_ERROR_NOACCESS && oldFileLock->lockType == LockRead) {
5150 if (!has_insert || !SCP_SUPPORTS_WRITELOCKACL(scp)) {
5151 force_client_lock = TRUE;
5155 lock_ReleaseMutex(&scp->mx);
5159 lock_ObtainWrite(&cm_scacheLock);
5161 /* Check if we already have a sufficient server lock to allow this
5162 lock to go through. */
5163 if (IS_LOCK_WAITLOCK(oldFileLock) &&
5164 (!SERVERLOCKS_ENABLED(scp) ||
5165 scp->serverLock == oldFileLock->lockType ||
5166 scp->serverLock == LockWrite)) {
5168 oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITLOCK;
5170 if (SERVERLOCKS_ENABLED(scp)) {
5171 osi_Log1(afsd_logp, "cm_RetryLock Server lock (%d) is sufficient for lock. Granting",
5172 (int) scp->serverLock);
5174 osi_Log0(afsd_logp, "cm_RetryLock skipping server lock for scp");
5177 lock_ReleaseWrite(&cm_scacheLock);
5178 lock_ReleaseMutex(&scp->mx);
5183 if (IS_LOCK_WAITUNLOCK(oldFileLock)) {
5185 /* check if the conflicting locks have dissappeared already */
5186 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
5188 fileLock = (cm_file_lock_t *)
5189 ((char *) q - offsetof(cm_file_lock_t, fileq));
5191 if (IS_LOCK_LOST(fileLock)) {
5192 if (fileLock->key == oldFileLock->key) {
5193 code = CM_ERROR_BADFD;
5194 oldFileLock->flags |= CM_FILELOCK_FLAG_LOST;
5195 osi_Log1(afsd_logp, " found lost lock %p for same key. Marking lock as lost",
5198 } else if (fileLock->lockType == LockWrite &&
5199 INTERSECT_RANGE(oldFileLock->range, fileLock->range)) {
5200 osi_Log1(afsd_logp, " found conflicting LOST lock %p", fileLock);
5201 code = CM_ERROR_WOULDBLOCK;
5206 if (IS_LOCK_ACCEPTED(fileLock) &&
5207 INTERSECT_RANGE(oldFileLock->range, fileLock->range)) {
5209 if (oldFileLock->lockType != LockRead ||
5210 fileLock->lockType != LockRead) {
5212 osi_Log1(afsd_logp, " found conflicting lock %p", fileLock);
5213 code = CM_ERROR_WOULDBLOCK;
5221 lock_ReleaseWrite(&cm_scacheLock);
5222 lock_ReleaseMutex(&scp->mx);
5227 /* when we get here, the lock is either a WAITUNLOCK or WAITLOCK.
5228 If it is WAITUNLOCK, then we didn't find any conflicting lock
5229 but we haven't verfied whether the serverLock is sufficient to
5230 assert it. If it is WAITLOCK, then the serverLock is
5231 insufficient to assert it. Eitherway, we are ready to accept
5232 the lock as either ACTIVE or WAITLOCK depending on the
5235 /* First, promote the WAITUNLOCK to a WAITLOCK */
5236 if (IS_LOCK_WAITUNLOCK(oldFileLock)) {
5237 if (oldFileLock->lockType == LockRead)
5240 scp->exclusiveLocks++;
5242 oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITUNLOCK;
5243 oldFileLock->flags |= CM_FILELOCK_FLAG_WAITLOCK;
5246 osi_assertx(IS_LOCK_WAITLOCK(oldFileLock), "!IS_LOCK_WAITLOCK");
5248 if (force_client_lock ||
5249 !SERVERLOCKS_ENABLED(scp) ||
5250 scp->serverLock == oldFileLock->lockType ||
5251 (oldFileLock->lockType == LockRead &&
5252 scp->serverLock == LockWrite)) {
5254 oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITLOCK;
5256 if ((force_client_lock ||
5257 !SERVERLOCKS_ENABLED(scp)) &&
5258 !IS_LOCK_CLIENTONLY(oldFileLock)) {
5260 oldFileLock->flags |= CM_FILELOCK_FLAG_CLIENTONLY;
5262 if (oldFileLock->lockType == LockRead)
5265 scp->exclusiveLocks--;
5270 lock_ReleaseWrite(&cm_scacheLock);
5271 lock_ReleaseMutex(&scp->mx);
5278 code = cm_SyncOp(scp, NULL, oldFileLock->userp, &req, 0,
5279 CM_SCACHESYNC_NEEDCALLBACK
5280 | CM_SCACHESYNC_GETSTATUS
5281 | CM_SCACHESYNC_LOCK);
5283 osi_Log1(smb_logp, "cm_RetryLock SyncOp failure code 0x%x", code);
5284 lock_ReleaseWrite(&cm_scacheLock);
5285 goto post_syncopdone;
5288 if (!IS_LOCK_WAITLOCK(oldFileLock))
5289 goto pre_syncopdone;
5291 userp = oldFileLock->userp;
5293 #ifndef AGGRESSIVE_LOCKS
5294 newLock = oldFileLock->lockType;
5296 newLock = LockWrite;
5300 /* if has_insert is non-zero, then:
5301 - the lock a LockRead
5302 - we don't have permission to get a LockRead
5303 - we do have permission to get a LockWrite
5304 - the server supports VICED_CAPABILITY_WRITELOCKACL
5307 newLock = LockWrite;
5310 lock_ReleaseWrite(&cm_scacheLock);
5312 /* when we get here, either we have a read-lock and want a
5313 write-lock or we don't have any locks and we want some
5316 if (scp->serverLock == LockRead) {
5318 osi_assertx(newLock == LockWrite, "!LockWrite");
5320 osi_Log0(afsd_logp, " Attempting to UPGRADE from LockRead to LockWrite");
5322 scp->lockDataVersion = scp->dataVersion;
5323 check_data_version = TRUE;
5325 code = cm_IntReleaseLock(scp, userp, &req);
5328 goto pre_syncopdone;
5330 scp->serverLock = -1;
5333 code = cm_IntSetLock(scp, userp, newLock, &req);
5336 if (scp->dataVersion != scp->lockDataVersion) {
5337 /* we lost a race. too bad */
5340 " Data version mismatch while upgrading lock.");
5342 " Data versions before=%I64d, after=%I64d",
5343 scp->lockDataVersion,
5346 " Releasing stale lock for scp 0x%x", scp);
5348 code = cm_IntReleaseLock(scp, userp, &req);
5350 scp->serverLock = -1;
5352 code = CM_ERROR_INVAL;
5354 cm_LockMarkSCacheLost(scp);
5356 scp->serverLock = newLock;
5361 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
5367 if (code != 0 && code != CM_ERROR_WOULDBLOCK) {
5368 lock_ObtainWrite(&cm_scacheLock);
5369 if (scp->fileLocksT == &oldFileLock->fileq)
5370 scp->fileLocksT = osi_QPrev(&oldFileLock->fileq);
5371 osi_QRemoveHT(&scp->fileLocksH, &scp->fileLocksT, &oldFileLock->fileq);
5372 lock_ReleaseWrite(&cm_scacheLock);
5374 lock_ReleaseMutex(&scp->mx);
5377 lock_ObtainWrite(&cm_scacheLock);
5379 oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITLOCK;
5380 } else if (code != CM_ERROR_WOULDBLOCK) {
5381 oldFileLock->flags |= CM_FILELOCK_FLAG_DELETED;
5382 cm_ReleaseUser(oldFileLock->userp);
5383 oldFileLock->userp = NULL;
5384 if (oldFileLock->scp) {
5385 cm_ReleaseSCacheNoLock(oldFileLock->scp);
5386 oldFileLock->scp = NULL;
5389 lock_ReleaseWrite(&cm_scacheLock);
5394 cm_key_t cm_GenerateKey(unsigned int session_id, unsigned long process_id, unsigned int file_id)
5397 osi_assertx((process_id & 0xffffffff) == process_id, "unexpected process_id");
5398 osi_assertx((session_id & 0xffff) == session_id, "unexpected session_id");
5399 osi_assertx((file_id & 0xffff) == file_id, "unexpected file_id");
5403 (((cm_key_t) (process_id & 0xffffffff)) << 32) |
5404 (((cm_key_t) (session_id & 0xffff)) << 16) |
5405 (((cm_key_t) (file_id & 0xffff)));
5408 static int cm_KeyEquals(cm_key_t k1, cm_key_t k2, int flags)
5410 if (flags & CM_UNLOCK_BY_FID) {
5411 return ((k1 & 0xffffffff) == (k2 & 0xffffffff));
5417 void cm_ReleaseAllLocks(void)
5423 cm_file_lock_t *fileLock;
5426 for (i = 0; i < cm_data.scacheHashTableSize; i++)
5428 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
5429 while (scp->fileLocksH != NULL) {
5430 lock_ObtainMutex(&scp->mx);
5431 lock_ObtainWrite(&cm_scacheLock);
5432 if (!scp->fileLocksH) {
5433 lock_ReleaseWrite(&cm_scacheLock);
5434 lock_ReleaseMutex(&scp->mx);
5437 fileLock = (cm_file_lock_t *)((char *) scp->fileLocksH - offsetof(cm_file_lock_t, fileq));
5438 userp = fileLock->userp;
5440 key = fileLock->key;
5441 cm_HoldSCacheNoLock(scp);
5442 lock_ReleaseWrite(&cm_scacheLock);
5443 cm_UnlockByKey(scp, key, 0, userp, &req);
5444 cm_ReleaseSCache(scp);
5445 cm_ReleaseUser(userp);
5446 lock_ReleaseMutex(&scp->mx);