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 <afsconfig.h>
11 #include <afs/param.h>
33 extern void afsi_log(char *pattern, ...);
36 int cm_enableServerLocks = 1;
38 int cm_followBackupPath = 0;
41 * Case-folding array. This was constructed by inspecting of SMBtrace output.
42 * I do not know anything more about it.
44 unsigned char cm_foldUpper[256] = {
45 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
46 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
47 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
48 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
49 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
50 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
51 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
52 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
53 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
54 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
55 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
56 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
57 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
58 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
59 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
60 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
61 0x80, 0x9a, 0x90, 0x41, 0x8e, 0x41, 0x8f, 0x80,
62 0x45, 0x45, 0x45, 0x49, 0x49, 0x49, 0x8e, 0x8f,
63 0x90, 0x92, 0x92, 0x4f, 0x99, 0x4f, 0x55, 0x55,
64 0x59, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
65 0x41, 0x49, 0x4f, 0x55, 0xa5, 0xa5, 0x56, 0xa7,
66 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
67 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
68 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
69 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
70 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
71 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
72 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
73 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
74 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
75 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
76 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
80 * Case-insensitive string comparison. We used to use stricmp, but it doesn't
81 * know about 8-bit characters (e.g. 129 is lowercase u-umlaut, 154 is
82 * upper-case u-umlaut).
84 int cm_stricmp(const char *str1, const char *str2)
96 c1 = (char) cm_foldUpper[(unsigned char)(*str1++)];
97 c2 = (char) cm_foldUpper[(unsigned char)(*str2++)];
107 /* return success if we can open this file in this mode */
108 long cm_CheckOpen(cm_scache_t *scp, int openMode, int trunc, cm_user_t *userp,
116 rights |= PRSFS_READ;
117 if (openMode == 1 || openMode == 2 || trunc)
118 rights |= PRSFS_WRITE;
120 lock_ObtainWrite(&scp->rw);
122 code = cm_SyncOp(scp, NULL, userp, reqp, rights,
123 CM_SCACHESYNC_GETSTATUS
124 | CM_SCACHESYNC_NEEDCALLBACK
125 | CM_SCACHESYNC_LOCK);
128 ((rights & PRSFS_WRITE) || (rights & PRSFS_READ)) &&
129 scp->fileType == CM_SCACHETYPE_FILE) {
132 unsigned int sLockType;
133 LARGE_INTEGER LOffset, LLength;
135 /* Check if there's some sort of lock on the file at the
138 key = cm_GenerateKey(CM_SESSION_CMINT,0,0);
140 if (rights & PRSFS_WRITE)
143 sLockType = LOCKING_ANDX_SHARED_LOCK;
145 LOffset.HighPart = CM_FLSHARE_OFFSET_HIGH;
146 LOffset.LowPart = CM_FLSHARE_OFFSET_LOW;
147 LLength.HighPart = CM_FLSHARE_LENGTH_HIGH;
148 LLength.LowPart = CM_FLSHARE_LENGTH_LOW;
150 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp, NULL);
153 cm_Unlock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp);
155 /* In this case, we allow the file open to go through even
156 though we can't enforce mandatory locking on the
158 if (code == CM_ERROR_NOACCESS &&
159 !(rights & PRSFS_WRITE))
162 if (code == CM_ERROR_LOCK_NOT_GRANTED)
163 code = CM_ERROR_SHARING_VIOLATION;
167 } else if (code != 0) {
171 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
175 lock_ReleaseWrite(&scp->rw);
180 /* return success if we can open this file in this mode */
181 long cm_CheckNTOpen(cm_scache_t *scp, unsigned int desiredAccess,
182 unsigned int createDisp, cm_user_t *userp, cm_req_t *reqp,
183 cm_lock_data_t **ldpp)
188 osi_assertx(ldpp != NULL, "null cm_lock_data_t");
191 /* Always allow delete; the RPC will tell us if it's OK */
194 if (desiredAccess == DELETE)
197 if (desiredAccess & (AFS_ACCESS_READ|AFS_ACCESS_EXECUTE))
198 rights |= (scp->fileType == CM_SCACHETYPE_DIRECTORY ? PRSFS_LOOKUP : PRSFS_READ);
200 /* We used to require PRSFS_WRITE if createDisp was 4
201 (OPEN_ALWAYS) even if AFS_ACCESS_WRITE was not requested.
202 However, we don't need to do that since the existence of the
203 scp implies that we don't need to create it. */
204 if (desiredAccess & AFS_ACCESS_WRITE)
205 rights |= PRSFS_WRITE;
207 if (desiredAccess & DELETE)
208 rights |= PRSFS_DELETE;
210 lock_ObtainWrite(&scp->rw);
212 code = cm_SyncOp(scp, NULL, userp, reqp, rights,
213 CM_SCACHESYNC_GETSTATUS
214 | CM_SCACHESYNC_NEEDCALLBACK
215 | CM_SCACHESYNC_LOCK);
218 * If the open will fail because the volume is readonly, then we will
219 * return an access denied error instead. This is to help brain-dead
220 * apps run correctly on replicated volumes.
221 * See defect 10007 for more information.
223 if (code == CM_ERROR_READONLY)
224 code = CM_ERROR_NOACCESS;
227 ((rights & PRSFS_WRITE) || (rights & PRSFS_READ)) &&
228 scp->fileType == CM_SCACHETYPE_FILE) {
230 unsigned int sLockType;
231 LARGE_INTEGER LOffset, LLength;
233 /* Check if there's some sort of lock on the file at the
236 key = cm_GenerateKey(CM_SESSION_CMINT,0,0);
237 if (rights & PRSFS_WRITE)
240 sLockType = LOCKING_ANDX_SHARED_LOCK;
242 /* single byte lock at offset 0x0100 0000 0000 0000 */
243 LOffset.HighPart = CM_FLSHARE_OFFSET_HIGH;
244 LOffset.LowPart = CM_FLSHARE_OFFSET_LOW;
245 LLength.HighPart = CM_FLSHARE_LENGTH_HIGH;
246 LLength.LowPart = CM_FLSHARE_LENGTH_LOW;
248 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp, NULL);
251 (*ldpp) = (cm_lock_data_t *)malloc(sizeof(cm_lock_data_t));
258 (*ldpp)->sLockType = sLockType;
259 (*ldpp)->LOffset.HighPart = LOffset.HighPart;
260 (*ldpp)->LOffset.LowPart = LOffset.LowPart;
261 (*ldpp)->LLength.HighPart = LLength.HighPart;
262 (*ldpp)->LLength.LowPart = LLength.LowPart;
264 /* In this case, we allow the file open to go through even
265 though we can't enforce mandatory locking on the
267 if (code == CM_ERROR_NOACCESS &&
268 !(rights & PRSFS_WRITE))
271 if (code == CM_ERROR_LOCK_NOT_GRANTED)
272 code = CM_ERROR_SHARING_VIOLATION;
275 } else if (code != 0) {
280 lock_ReleaseWrite(&scp->rw);
283 osi_Log3(afsd_logp,"cm_CheckNTOpen scp 0x%p ldp 0x%p code 0x%x", scp, *ldpp, code);
287 extern long cm_CheckNTOpenDone(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
288 cm_lock_data_t ** ldpp)
290 osi_Log2(afsd_logp,"cm_CheckNTOpenDone scp 0x%p ldp 0x%p", scp, *ldpp);
291 lock_ObtainWrite(&scp->rw);
293 cm_Unlock(scp, (*ldpp)->sLockType, (*ldpp)->LOffset, (*ldpp)->LLength,
294 (*ldpp)->key, 0, userp, reqp);
298 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
299 lock_ReleaseWrite(&scp->rw);
303 * When CAP_NT_SMBS has been negotiated, deletion (of files or directories) is
304 * done in three steps:
305 * (1) open for deletion (NT_CREATE_AND_X)
306 * (2) set for deletion on close (NT_TRANSACTION2, SET_FILE_INFO)
308 * We must not do the RPC until step 3. But if we are going to return an error
309 * code (e.g. directory not empty), we must return it by step 2, otherwise most
310 * clients will not notice it. So we do a preliminary check. For deleting
311 * files, this is almost free, since we have already done the RPC to get the
312 * parent directory's status bits. But for deleting directories, we must do an
313 * additional RPC to get the directory's data to check if it is empty. Sigh.
315 long cm_CheckNTDelete(cm_scache_t *dscp, cm_scache_t *scp, cm_user_t *userp,
321 cm_dirEntry_t *dep = 0;
322 unsigned short *hashTable;
324 int BeyondPage = 0, HaveDot = 0, HaveDotDot = 0;
327 /* First check permissions */
328 lock_ObtainWrite(&scp->rw);
329 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_DELETE,
330 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
332 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
333 lock_ReleaseWrite(&scp->rw);
337 /* If deleting directory, must be empty */
339 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
342 thyper.HighPart = 0; thyper.LowPart = 0;
343 code = buf_Get(scp, &thyper, reqp, &bufferp);
347 lock_ObtainMutex(&bufferp->mx);
348 lock_ObtainWrite(&scp->rw);
351 code = cm_SyncOp(scp, bufferp, userp, reqp, 0,
352 CM_SCACHESYNC_NEEDCALLBACK
354 | CM_SCACHESYNC_BUFLOCKED);
358 if (cm_HaveBuffer(scp, bufferp, 1))
361 /* otherwise, load the buffer and try again */
362 lock_ReleaseMutex(&bufferp->mx);
363 code = cm_GetBuffer(scp, bufferp, NULL, userp, reqp);
364 lock_ReleaseWrite(&scp->rw);
365 lock_ObtainMutex(&bufferp->mx);
366 lock_ObtainWrite(&scp->rw);
367 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
372 lock_ReleaseWrite(&scp->rw);
375 /* We try to determine emptiness without looking beyond the first page,
376 * and without assuming "." and ".." are present and are on the first
377 * page (though these assumptions might, after all, be reasonable).
379 hashTable = (unsigned short *)(bufferp->datap + (32 * 5));
380 for (i=0; i<128; i++) {
381 idx = ntohs(hashTable[i]);
387 dep = (cm_dirEntry_t *)(bufferp->datap + (32 * idx));
388 if (strcmp(dep->name, ".") == 0)
390 else if (strcmp(dep->name, "..") == 0)
393 code = CM_ERROR_NOTEMPTY;
396 idx = ntohs(dep->next);
399 if (BeyondPage && HaveDot && HaveDotDot)
400 code = CM_ERROR_NOTEMPTY;
404 lock_ReleaseMutex(&bufferp->mx);
405 buf_Release(bufferp);
407 lock_ReleaseWrite(&scp->rw);
412 * Iterate through all entries in a directory.
413 * When the function funcp is called, the buffer is locked but the
414 * directory vnode is not.
416 * If the retscp parameter is not NULL, the parmp must be a
417 * cm_lookupSearch_t object.
419 long cm_ApplyDir(cm_scache_t *scp, cm_DirFuncp_t funcp, void *parmp,
420 osi_hyper_t *startOffsetp, cm_user_t *userp, cm_req_t *reqp,
421 cm_scache_t **retscp)
425 cm_dirEntry_t *dep = 0;
428 osi_hyper_t dirLength;
429 osi_hyper_t bufferOffset;
430 osi_hyper_t curOffset;
434 cm_pageHeader_t *pageHeaderp;
436 long nextEntryCookie;
437 int numDirChunks; /* # of 32 byte dir chunks in this entry */
439 /* get the directory size */
440 lock_ObtainWrite(&scp->rw);
441 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_LOOKUP,
442 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
443 lock_ReleaseWrite(&scp->rw);
447 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
448 return CM_ERROR_NOTDIR;
450 if (retscp) /* if this is a lookup call */
452 cm_lookupSearch_t* sp = parmp;
455 #ifdef AFS_FREELANCE_CLIENT
456 /* Freelance entries never end up in the DNLC because they
457 * do not have an associated cm_server_t
459 !(cm_freelanceEnabled &&
460 sp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
461 sp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
462 #else /* !AFS_FREELANCE_CLIENT */
467 int casefold = sp->caseFold;
468 sp->caseFold = 0; /* we have a strong preference for exact matches */
469 if ( *retscp = cm_dnlcLookup(scp, sp)) /* dnlc hit */
471 sp->caseFold = casefold;
474 sp->caseFold = casefold;
478 * see if we can find it using the directory hash tables.
479 * we can only do exact matches, since the hash is case
482 if (funcp != (cm_DirFuncp_t)cm_BPlusDirFoo)
491 code = cm_BeginDirOp(scp, userp, reqp, CM_DIRLOCK_READ,
492 CM_DIROP_FLAG_NONE, &dirop);
496 code = cm_BPlusDirLookup(&dirop, sp->nsearchNamep, &sp->fid);
501 code = cm_DirLookup(&dirop, sp->searchNamep, &sp->fid);
509 sp->ExactFound = TRUE;
510 *retscp = NULL; /* force caller to call cm_GetSCache() */
515 if (sp->caseFold && code == CM_ERROR_INEXACT_MATCH) {
518 sp->ExactFound = FALSE;
519 *retscp = NULL; /* force caller to call cm_GetSCache() */
523 return CM_ERROR_BPLUS_NOMATCH;
530 * XXX We only get the length once. It might change when we drop the
533 dirLength = scp->length;
536 bufferOffset.LowPart = bufferOffset.HighPart = 0;
538 curOffset = *startOffsetp;
540 curOffset.HighPart = 0;
541 curOffset.LowPart = 0;
545 /* make sure that curOffset.LowPart doesn't point to the first
546 * 32 bytes in the 2nd through last dir page, and that it
547 * doesn't point at the first 13 32-byte chunks in the first
548 * dir page, since those are dir and page headers, and don't
549 * contain useful information.
551 temp = curOffset.LowPart & (2048-1);
552 if (curOffset.HighPart == 0 && curOffset.LowPart < 2048) {
553 /* we're in the first page */
554 if (temp < 13*32) temp = 13*32;
557 /* we're in a later dir page */
558 if (temp < 32) temp = 32;
561 /* make sure the low order 5 bits are zero */
564 /* now put temp bits back ito curOffset.LowPart */
565 curOffset.LowPart &= ~(2048-1);
566 curOffset.LowPart |= temp;
568 /* check if we've passed the dir's EOF */
569 if (LargeIntegerGreaterThanOrEqualTo(curOffset, dirLength))
572 /* see if we can use the bufferp we have now; compute in which
573 * page the current offset would be, and check whether that's
574 * the offset of the buffer we have. If not, get the buffer.
576 thyper.HighPart = curOffset.HighPart;
577 thyper.LowPart = curOffset.LowPart & ~(cm_data.buf_blockSize-1);
578 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
581 lock_ReleaseMutex(&bufferp->mx);
582 buf_Release(bufferp);
586 code = buf_Get(scp, &thyper, reqp, &bufferp);
588 /* if buf_Get() fails we do not have a buffer object to lock */
593 lock_ObtainMutex(&bufferp->mx);
594 bufferOffset = thyper;
596 /* now get the data in the cache */
598 lock_ObtainWrite(&scp->rw);
599 code = cm_SyncOp(scp, bufferp, userp, reqp,
601 CM_SCACHESYNC_NEEDCALLBACK
603 | CM_SCACHESYNC_BUFLOCKED);
605 lock_ReleaseWrite(&scp->rw);
608 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
610 if (cm_HaveBuffer(scp, bufferp, 1)) {
611 lock_ReleaseWrite(&scp->rw);
615 /* otherwise, load the buffer and try again */
616 lock_ReleaseMutex(&bufferp->mx);
617 code = cm_GetBuffer(scp, bufferp, NULL, userp,
619 lock_ReleaseWrite(&scp->rw);
620 lock_ObtainMutex(&bufferp->mx);
625 lock_ReleaseMutex(&bufferp->mx);
626 buf_Release(bufferp);
630 } /* if (wrong buffer) ... */
632 /* now we have the buffer containing the entry we're interested
633 * in; copy it out if it represents a non-deleted entry.
635 entryInDir = curOffset.LowPart & (2048-1);
636 entryInBuffer = curOffset.LowPart & (cm_data.buf_blockSize - 1);
638 /* page header will help tell us which entries are free. Page
639 * header can change more often than once per buffer, since
640 * AFS 3 dir page size may be less than (but not more than) a
641 * buffer package buffer.
643 /* only look intra-buffer */
644 temp = curOffset.LowPart & (cm_data.buf_blockSize - 1);
645 temp &= ~(2048 - 1); /* turn off intra-page bits */
646 pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
648 /* now determine which entry we're looking at in the page. If
649 * it is free (there's a free bitmap at the start of the dir),
650 * we should skip these 32 bytes.
652 slotInPage = (entryInDir & 0x7e0) >> 5;
653 if (!(pageHeaderp->freeBitmap[slotInPage>>3]
654 & (1 << (slotInPage & 0x7)))) {
655 /* this entry is free */
656 numDirChunks = 1; /* only skip this guy */
660 tp = bufferp->datap + entryInBuffer;
661 dep = (cm_dirEntry_t *) tp; /* now points to AFS3 dir entry */
664 * here are some consistency checks
666 if (dep->flag != CM_DIR_FFIRST ||
667 strlen(dep->name) > 256) {
668 code = CM_ERROR_INVAL;
670 "cm_ApplyDir invalid directory entry for scp %p bufp %p",
672 osi_Log4(afsd_logp,"... cell %u vol %u vnode %u uniq %u",
673 scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
674 bufferp->dataVersion = CM_BUF_VERSION_BAD;
678 /* while we're here, compute the next entry's location, too,
679 * since we'll need it when writing out the cookie into the
680 * dir listing stream.
682 numDirChunks = cm_NameEntries(dep->name, NULL);
684 /* compute the offset of the cookie representing the next entry */
685 nextEntryCookie = curOffset.LowPart
686 + (CM_DIR_CHUNKSIZE * numDirChunks);
688 if (dep->fid.vnode != 0) {
689 /* this is one of the entries to use: it is not deleted */
690 code = (*funcp)(scp, dep, parmp, &curOffset);
693 } /* if we're including this name */
696 /* and adjust curOffset to be where the new cookie is */
698 thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
699 curOffset = LargeIntegerAdd(thyper, curOffset);
700 } /* while copying data for dir listing */
702 /* release the mutex */
704 lock_ReleaseMutex(&bufferp->mx);
705 buf_Release(bufferp);
710 int cm_NoneUpper(normchar_t *s)
714 if (c >= 'A' && c <= 'Z')
719 int cm_NoneLower(normchar_t *s)
723 if (c >= 'a' && c <= 'z')
728 long cm_LookupSearchProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
731 cm_lookupSearch_t *sp;
733 normchar_t matchName[MAX_PATH];
734 int looking_for_short_name = FALSE;
736 sp = (cm_lookupSearch_t *) rockp;
738 if (cm_FsStringToNormString(dep->name, -1, matchName, lengthof(matchName)) == 0) {
739 /* Can't normalize FS string. */
744 match = cm_NormStrCmpI(matchName, sp->nsearchNamep);
746 match = cm_NormStrCmp(matchName, sp->nsearchNamep);
750 && !cm_Is8Dot3(matchName)) {
752 cm_Gen8Dot3NameInt(dep->name, &dep->fid, matchName, NULL);
754 match = cm_NormStrCmpI(matchName, sp->nsearchNamep);
756 match = cm_NormStrCmp(matchName, sp->nsearchNamep);
757 looking_for_short_name = TRUE;
767 if (!sp->caseFold || looking_for_short_name) {
768 cm_SetFid(&sp->fid, sp->fid.cell, sp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
769 return CM_ERROR_STOPNOW;
773 * If we get here, we are doing a case-insensitive search, and we
774 * have found a match. Now we determine what kind of match it is:
775 * exact, lower-case, upper-case, or none of the above. This is done
776 * in order to choose among matches, if there are more than one.
779 /* Exact matches are the best. */
780 match = cm_NormStrCmp(matchName, sp->nsearchNamep);
783 cm_SetFid(&sp->fid, sp->fid.cell, sp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
784 return CM_ERROR_STOPNOW;
787 /* Lower-case matches are next. */
790 if (cm_NoneUpper(matchName)) {
795 /* Upper-case matches are next. */
798 if (cm_NoneLower(matchName)) {
803 /* General matches are last. */
809 cm_SetFid(&sp->fid, sp->fid.cell, sp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
813 /* read the contents of a mount point into the appropriate string.
814 * called with write locked scp, and returns with locked scp.
816 long cm_ReadMountPoint(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
820 if (scp->mountPointStringp[0])
823 #ifdef AFS_FREELANCE_CLIENT
824 /* File servers do not have data for freelance entries */
825 if (cm_freelanceEnabled &&
826 scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
827 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
829 code = cm_FreelanceFetchMountPointString(scp);
831 #endif /* AFS_FREELANCE_CLIENT */
833 char temp[MOUNTPOINTLEN];
836 /* otherwise, we have to read it in */
837 offset.LowPart = offset.HighPart = 0;
838 code = cm_GetData(scp, &offset, temp, MOUNTPOINTLEN, userp, reqp);
843 * scp->length is the actual length of the mount point string.
844 * It is current because cm_GetData merged the most up to date
845 * status info into scp and has not dropped the rwlock since.
847 if (scp->length.LowPart > MOUNTPOINTLEN - 1)
848 return CM_ERROR_TOOBIG;
849 if (scp->length.LowPart == 0)
850 return CM_ERROR_INVAL;
852 /* convert the terminating dot to a NUL */
853 temp[scp->length.LowPart - 1] = 0;
854 memcpy(scp->mountPointStringp, temp, scp->length.LowPart);
861 /* called with a locked scp and chases the mount point, yielding outScpp.
862 * scp remains write locked, just for simplicity of describing the interface.
864 long cm_FollowMountPoint(cm_scache_t *scp, cm_scache_t *dscp, cm_user_t *userp,
865 cm_req_t *reqp, cm_scache_t **outScpp)
867 fschar_t *cellNamep = NULL;
868 fschar_t *volNamep = NULL;
872 cm_volume_t *volp = NULL;
881 if (scp->mountRootFid.cell != 0 && scp->mountRootGen >= cm_data.mountRootGen) {
882 tfid = scp->mountRootFid;
883 lock_ReleaseWrite(&scp->rw);
884 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
885 lock_ObtainWrite(&scp->rw);
889 /* parse the volume name */
890 mpNamep = scp->mountPointStringp;
892 return CM_ERROR_NOSUCHPATH;
893 mtType = *scp->mountPointStringp;
895 cp = cm_FsStrChr(mpNamep, _FS(':'));
897 /* cellular mount point */
898 cellNamep = (fschar_t *)malloc((cp - mpNamep) * sizeof(fschar_t));
899 cm_FsStrCpyN(cellNamep, cp - mpNamep, mpNamep + 1, cp - mpNamep - 1);
900 volNamep = cm_FsStrDup(cp+1);
902 /* now look up the cell */
903 lock_ReleaseWrite(&scp->rw);
904 cellp = cm_GetCell(cellNamep, CM_FLAG_CREATE);
905 lock_ObtainWrite(&scp->rw);
908 volNamep = cm_FsStrDup(mpNamep + 1);
910 #ifdef AFS_FREELANCE_CLIENT
912 * Mount points in the Freelance cell should default
913 * to the workstation cell.
915 if (cm_freelanceEnabled &&
916 scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
917 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
919 fschar_t rootCellName[256]="";
920 cm_GetRootCellName(rootCellName);
921 cellp = cm_GetCell(rootCellName, 0);
923 #endif /* AFS_FREELANCE_CLIENT */
924 cellp = cm_FindCellByID(scp->fid.cell, 0);
928 code = CM_ERROR_NOSUCHCELL;
932 vnLength = cm_FsStrLen(volNamep);
933 if (vnLength >= 8 && cm_FsStrCmp(volNamep + vnLength - 7, ".backup") == 0)
934 targetType = BACKVOL;
935 else if (vnLength >= 10
936 && cm_FsStrCmp(volNamep + vnLength - 9, ".readonly") == 0)
941 /* check for backups within backups */
942 if (targetType == BACKVOL
943 && (scp->flags & (CM_SCACHEFLAG_RO | CM_SCACHEFLAG_PURERO))
944 == CM_SCACHEFLAG_RO) {
945 code = CM_ERROR_NOSUCHVOLUME;
949 /* now we need to get the volume */
950 lock_ReleaseWrite(&scp->rw);
951 if (cm_VolNameIsID(volNamep)) {
952 code = cm_FindVolumeByID(cellp, atoi(volNamep), userp, reqp,
953 CM_GETVOL_FLAG_CREATE, &volp);
955 code = cm_FindVolumeByName(cellp, volNamep, userp, reqp,
956 CM_GETVOL_FLAG_CREATE, &volp);
958 lock_ObtainWrite(&scp->rw);
961 afs_uint32 cell, volume;
962 cm_vol_state_t *statep;
964 cell = cellp->cellID;
966 /* if the mt pt originates in a .backup volume (not a .readonly)
967 * and FollowBackupPath is active, and if there is a .backup
968 * volume for the target, then use the .backup of the target
969 * instead of the read-write.
971 if (cm_followBackupPath &&
972 volp->vol[BACKVOL].ID != 0 &&
973 (dscp->flags & (CM_SCACHEFLAG_RO|CM_SCACHEFLAG_PURERO)) == CM_SCACHEFLAG_RO &&
974 (targetType == RWVOL || targetType == ROVOL && volp->vol[ROVOL].ID == 0)
976 targetType = BACKVOL;
978 /* if the mt pt is in a read-only volume (not just a
979 * backup), and if there is a read-only volume for the
980 * target, and if this is a targetType '#' mount point, use
981 * the read-only, otherwise use the one specified.
983 else if (mtType == '#' && targetType == RWVOL &&
984 (scp->flags & CM_SCACHEFLAG_PURERO) &&
985 volp->vol[ROVOL].ID != 0) {
989 lock_ObtainWrite(&volp->rw);
990 statep = cm_VolumeStateByType(volp, targetType);
992 statep->dotdotFid = dscp->fid;
993 lock_ReleaseWrite(&volp->rw);
995 /* the rest of the fid is a magic number */
996 cm_SetFid(&scp->mountRootFid, cell, volume, 1, 1);
997 scp->mountRootGen = cm_data.mountRootGen;
999 tfid = scp->mountRootFid;
1000 lock_ReleaseWrite(&scp->rw);
1001 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1002 lock_ObtainWrite(&scp->rw);
1015 long cm_LookupInternal(cm_scache_t *dscp, clientchar_t *cnamep, long flags, cm_user_t *userp,
1016 cm_req_t *reqp, cm_scache_t **outScpp)
1019 int dnlcHit = 1; /* did we hit in the dnlc? yes, we did */
1020 cm_scache_t *tscp = NULL;
1021 cm_scache_t *mountedScp;
1022 cm_lookupSearch_t rock;
1024 normchar_t *nnamep = NULL;
1025 fschar_t *fnamep = NULL;
1030 memset(&rock, 0, sizeof(rock));
1032 if (dscp->fid.vnode == 1 && dscp->fid.unique == 1
1033 && cm_ClientStrCmp(cnamep, _C("..")) == 0) {
1034 if (dscp->dotdotFid.volume == 0)
1035 return CM_ERROR_NOSUCHVOLUME;
1036 rock.fid = dscp->dotdotFid;
1038 } else if (cm_ClientStrCmp(cnamep, _C(".")) == 0) {
1039 rock.fid = dscp->fid;
1043 nnamep = cm_ClientStringToNormStringAlloc(cnamep, -1, NULL);
1045 code = CM_ERROR_NOSUCHFILE;
1048 fnamep = cm_ClientStringToFsStringAlloc(cnamep, -1, NULL);
1050 code = CM_ERROR_NOSUCHFILE;
1055 if (flags & CM_FLAG_NOMOUNTCHASE) {
1056 /* In this case, we should go and call cm_Dir* functions
1057 directly since the following cm_ApplyDir() function will
1065 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ,
1066 CM_DIROP_FLAG_NONE, &dirop);
1069 code = cm_BPlusDirLookup(&dirop, nnamep, &rock.fid);
1074 code = cm_DirLookup(&dirop, fnamep, &rock.fid);
1076 cm_EndDirOp(&dirop);
1086 if (code == CM_ERROR_INEXACT_MATCH && (flags & CM_FLAG_CASEFOLD)) {
1093 code = CM_ERROR_BPLUS_NOMATCH;
1099 rock.fid.cell = dscp->fid.cell;
1100 rock.fid.volume = dscp->fid.volume;
1101 rock.searchNamep = fnamep;
1102 rock.nsearchNamep = nnamep;
1103 rock.caseFold = (flags & CM_FLAG_CASEFOLD);
1104 rock.hasTilde = ((cm_ClientStrChr(cnamep, '~') != NULL) ? 1 : 0);
1106 /* If NOMOUNTCHASE, bypass DNLC by passing NULL scp pointer */
1107 code = cm_ApplyDir(dscp, cm_LookupSearchProc, &rock, NULL, userp, reqp,
1108 (flags & CM_FLAG_NOMOUNTCHASE) ? NULL : &tscp);
1110 /* code == 0 means we fell off the end of the dir, while stopnow means
1111 * that we stopped early, probably because we found the entry we're
1112 * looking for. Any other non-zero code is an error.
1114 if (code && code != CM_ERROR_STOPNOW && code != CM_ERROR_BPLUS_NOMATCH) {
1115 /* if the cm_scache_t we are searching in is not a directory
1116 * we must return path not found because the error
1117 * is to describe the final component not an intermediary
1119 if (code == CM_ERROR_NOTDIR) {
1120 if (flags & CM_FLAG_CHECKPATH)
1121 code = CM_ERROR_NOSUCHPATH;
1123 code = CM_ERROR_NOSUCHFILE;
1129 getroot = (dscp==cm_data.rootSCachep) ;
1131 if (!cm_freelanceEnabled || !getroot) {
1132 if (flags & CM_FLAG_CHECKPATH)
1133 code = CM_ERROR_NOSUCHPATH;
1135 code = CM_ERROR_NOSUCHFILE;
1138 else if (!cm_ClientStrChr(cnamep, '#') &&
1139 !cm_ClientStrChr(cnamep, '%') &&
1140 cm_ClientStrCmpI(cnamep, _C("srvsvc")) &&
1141 cm_ClientStrCmpI(cnamep, _C("wkssvc")) &&
1142 cm_ClientStrCmpI(cnamep, _C("ipc$")))
1144 /* nonexistent dir on freelance root, so add it */
1145 fschar_t fullname[CELL_MAXNAMELEN + 1] = "."; /* +1 so that when we skip the . the size is still CELL_MAXNAMELEN */
1149 osi_Log1(afsd_logp,"cm_Lookup adding mount for non-existent directory: %S",
1150 osi_LogSaveClientString(afsd_logp,cnamep));
1153 * There is an ugly behavior where a share name "foo" will be searched
1154 * for as "fo". If the searched for name differs by an already existing
1155 * symlink or mount point in the Freelance directory, do not add the
1156 * new value automatically.
1160 fnlen = strlen(fnamep);
1161 if ( fnamep[fnlen-1] == '.') {
1162 fnamep[fnlen-1] = '\0';
1167 if (cnamep[0] == '.') {
1168 if (cm_GetCell_Gen(&fnamep[1], &fullname[1], CM_FLAG_CREATE)) {
1170 code = cm_FreelanceAddMount(fullname, &fullname[1], "root.cell", 1, &rock.fid);
1171 if ( cm_FsStrCmpI(&fnamep[1], &fullname[1])) {
1173 * Do not permit symlinks that are one of:
1174 * . the cellname followed by a dot
1175 * . the cellname minus a single character
1176 * . a substring of the cellname that does not consist of full components
1178 if ( cm_strnicmp_utf8(&fnamep[1], fullname, (int)fnlen-1) == 0 &&
1179 (fnlen-1 == strlen(fullname)-1 || fullname[fnlen-1] != '.'))
1181 /* do not add; substitute fullname for the search */
1183 fnamep = malloc(strlen(fullname)+2);
1185 strncpy(&fnamep[1], fullname, strlen(fullname)+1);
1188 code = cm_FreelanceAddSymlink(fnamep, fullname, &rock.fid);
1193 if (cm_GetCell_Gen(fnamep, fullname, CM_FLAG_CREATE)) {
1195 code = cm_FreelanceAddMount(fullname, fullname, "root.cell", 0, &rock.fid);
1196 if ( cm_FsStrCmpI(fnamep, fullname)) {
1198 * Do not permit symlinks that are one of:
1199 * . the cellname followed by a dot
1200 * . the cellname minus a single character
1201 * . a substring of the cellname that does not consist of full components
1203 if ( cm_strnicmp_utf8(fnamep, fullname, (int)fnlen-1) == 0 &&
1204 (fnlen == strlen(fullname)-1 || fullname[fnlen] != '.'))
1206 /* do not add; substitute fullname for the search */
1208 fnamep = strdup(fullname);
1212 code = cm_FreelanceAddSymlink(fnamep, fullname, &rock.fid);
1221 nnamep = cm_FsStringToNormStringAlloc(fnamep, -1, NULL);
1225 if (!found || code) { /* add mount point failed, so give up */
1226 if (flags & CM_FLAG_CHECKPATH)
1227 code = CM_ERROR_NOSUCHPATH;
1229 code = CM_ERROR_NOSUCHFILE;
1232 tscp = NULL; /* to force call of cm_GetSCache */
1234 if (flags & CM_FLAG_CHECKPATH)
1235 code = CM_ERROR_NOSUCHPATH;
1237 code = CM_ERROR_NOSUCHFILE;
1243 if ( !tscp ) /* we did not find it in the dnlc */
1246 code = cm_GetSCache(&rock.fid, &tscp, userp, reqp);
1250 /* tscp is now held */
1252 lock_ObtainWrite(&tscp->rw);
1255 * Do not get status if we do not already have a callback.
1256 * The process of reading the mount point string will obtain status information
1257 * in a single RPC. No reason to add a second round trip.
1259 * If we do have a callback, use cm_SyncOp to get status in case the
1260 * current cm_user_t is not the same as the one that obtained the
1261 * mount point string contents.
1263 if (cm_HaveCallback(tscp)) {
1264 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1265 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1267 lock_ReleaseWrite(&tscp->rw);
1268 cm_ReleaseSCache(tscp);
1271 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1273 /* tscp is now locked */
1275 if (!(flags & CM_FLAG_NOMOUNTCHASE)
1276 && tscp->fileType == CM_SCACHETYPE_MOUNTPOINT) {
1277 /* mount points are funny: they have a volume name to mount
1280 code = cm_ReadMountPoint(tscp, userp, reqp);
1282 code = cm_FollowMountPoint(tscp, dscp, userp, reqp,
1284 lock_ReleaseWrite(&tscp->rw);
1285 cm_ReleaseSCache(tscp);
1292 lock_ReleaseWrite(&tscp->rw);
1295 /* copy back pointer */
1298 /* insert scache in dnlc */
1299 if ( !dnlcHit && !(flags & CM_FLAG_NOMOUNTCHASE) && rock.ExactFound ) {
1300 /* lock the directory entry to prevent racing callback revokes */
1301 lock_ObtainRead(&dscp->rw);
1302 if ( dscp->cbServerp != NULL && dscp->cbExpires > 0 ) {
1303 /* TODO: reuse nnamep from above */
1306 nnamep = cm_ClientStringToNormStringAlloc(cnamep, -1, NULL);
1308 cm_dnlcEnter(dscp, nnamep, tscp);
1310 lock_ReleaseRead(&dscp->rw);
1327 int cm_ExpandSysName(clientchar_t *inp, clientchar_t *outp, long outSizeCch, unsigned int index)
1332 tp = cm_ClientStrRChr(inp, '@');
1334 return 0; /* no @sys */
1336 if (cm_ClientStrCmp(tp, _C("@sys")) != 0)
1337 return 0; /* no @sys */
1339 /* caller just wants to know if this is a valid @sys type of name */
1343 if (index >= cm_sysNameCount)
1346 /* otherwise generate the properly expanded @sys name */
1347 prefixCount = (int)(tp - inp);
1349 cm_ClientStrCpyN(outp, outSizeCch, inp, prefixCount); /* copy out "a." from "a.@sys" */
1350 outp[prefixCount] = 0; /* null terminate the "a." */
1351 cm_ClientStrCat(outp, outSizeCch, cm_sysNameList[index]);/* append i386_nt40 */
1355 long cm_EvaluateVolumeReference(clientchar_t * namep, long flags, cm_user_t * userp,
1356 cm_req_t *reqp, cm_scache_t ** outScpp)
1358 afs_uint32 code = 0;
1359 fschar_t cellName[CELL_MAXNAMELEN];
1360 fschar_t volumeName[VL_MAXNAMELEN];
1364 fschar_t * fnamep = NULL;
1366 cm_cell_t * cellp = NULL;
1367 cm_volume_t * volp = NULL;
1371 int mountType = RWVOL;
1373 osi_Log1(afsd_logp, "cm_EvaluateVolumeReference for string [%S]",
1374 osi_LogSaveClientString(afsd_logp, namep));
1376 if (cm_ClientStrCmpNI(namep, _C(CM_PREFIX_VOL), CM_PREFIX_VOL_CCH) != 0) {
1377 goto _exit_invalid_path;
1380 /* namep is assumed to look like the following:
1382 @vol:<cellname>%<volume>\0
1384 @vol:<cellname>#<volume>\0
1388 fnamep = cm_ClientStringToFsStringAlloc(namep, -1, NULL);
1389 cp = fnamep + CM_PREFIX_VOL_CCH; /* cp points to cell name, hopefully */
1390 tp = cm_FsStrChr(cp, '%');
1392 tp = cm_FsStrChr(cp, '#');
1394 (len = tp - cp) == 0 ||
1395 len > CELL_MAXNAMELEN)
1396 goto _exit_invalid_path;
1397 cm_FsStrCpyN(cellName, lengthof(cellName), cp, len);
1402 cp = tp+1; /* cp now points to volume, supposedly */
1403 cm_FsStrCpy(volumeName, lengthof(volumeName), cp);
1405 /* OK, now we have the cell and the volume */
1406 osi_Log2(afsd_logp, " Found cell [%s] and volume [%s]",
1407 osi_LogSaveFsString(afsd_logp, cellName),
1408 osi_LogSaveFsString(afsd_logp, volumeName));
1410 cellp = cm_GetCell(cellName, CM_FLAG_CREATE);
1411 if (cellp == NULL) {
1412 goto _exit_invalid_path;
1415 len = cm_FsStrLen(volumeName);
1416 if (len >= 8 && cm_FsStrCmp(volumeName + len - 7, ".backup") == 0)
1418 else if (len >= 10 &&
1419 cm_FsStrCmp(volumeName + len - 9, ".readonly") == 0)
1424 if (cm_VolNameIsID(volumeName)) {
1425 code = cm_FindVolumeByID(cellp, atoi(volumeName), userp, reqp,
1426 CM_GETVOL_FLAG_CREATE, &volp);
1428 code = cm_FindVolumeByName(cellp, volumeName, userp, reqp,
1429 CM_GETVOL_FLAG_CREATE, &volp);
1435 if (volType == BACKVOL)
1436 volume = volp->vol[BACKVOL].ID;
1437 else if (volType == ROVOL ||
1438 (volType == RWVOL && mountType == ROVOL && volp->vol[ROVOL].ID != 0))
1439 volume = volp->vol[ROVOL].ID;
1441 volume = volp->vol[RWVOL].ID;
1443 cm_SetFid(&fid, cellp->cellID, volume, 1, 1);
1445 code = cm_GetSCache(&fid, outScpp, userp, reqp);
1458 if (flags & CM_FLAG_CHECKPATH)
1459 return CM_ERROR_NOSUCHPATH;
1461 return CM_ERROR_NOSUCHFILE;
1464 #ifdef DEBUG_REFCOUNT
1465 long cm_LookupDbg(cm_scache_t *dscp, clientchar_t *namep, long flags, cm_user_t *userp,
1466 cm_req_t *reqp, cm_scache_t **outScpp, char * file, long line)
1468 long cm_Lookup(cm_scache_t *dscp, clientchar_t *namep, long flags, cm_user_t *userp,
1469 cm_req_t *reqp, cm_scache_t **outScpp)
1473 clientchar_t tname[AFSPATHMAX];
1474 int sysNameIndex = 0;
1475 cm_scache_t *scp = NULL;
1477 #ifdef DEBUG_REFCOUNT
1478 afsi_log("%s:%d cm_Lookup dscp 0x%p ref %d", file, line, dscp, dscp->refCount, file, line);
1479 osi_Log2(afsd_logp, "cm_Lookup dscp 0x%p ref %d", dscp, dscp->refCount);
1482 if ( cm_ClientStrCmpI(namep,_C(SMB_IOCTL_FILENAME_NOSLASH)) == 0 ) {
1483 if (flags & CM_FLAG_CHECKPATH)
1484 return CM_ERROR_NOSUCHPATH;
1486 return CM_ERROR_NOSUCHFILE;
1489 if (dscp == cm_data.rootSCachep &&
1490 cm_ClientStrCmpNI(namep, _C(CM_PREFIX_VOL), CM_PREFIX_VOL_CCH) == 0) {
1491 return cm_EvaluateVolumeReference(namep, flags, userp, reqp, outScpp);
1494 if (cm_ExpandSysName(namep, NULL, 0, 0) > 0) {
1495 for ( sysNameIndex = 0; sysNameIndex < MAXNUMSYSNAMES; sysNameIndex++) {
1496 code = cm_ExpandSysName(namep, tname, lengthof(tname), sysNameIndex);
1498 code = cm_LookupInternal(dscp, tname, flags, userp, reqp, &scp);
1499 #ifdef DEBUG_REFCOUNT
1500 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);
1501 osi_Log3(afsd_logp, "cm_LookupInternal (1) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1509 cm_ReleaseSCache(scp);
1513 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1514 #ifdef DEBUG_REFCOUNT
1515 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);
1516 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1523 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1524 #ifdef DEBUG_REFCOUNT
1525 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);
1526 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1532 /* None of the possible sysName expansions could be found */
1533 if (flags & CM_FLAG_CHECKPATH)
1534 return CM_ERROR_NOSUCHPATH;
1536 return CM_ERROR_NOSUCHFILE;
1539 /*! \brief Unlink a file name
1541 Encapsulates a call to RXAFS_RemoveFile().
1543 \param[in] dscp cm_scache_t pointing at the directory containing the
1544 name to be unlinked.
1546 \param[in] fnamep Original name to be unlinked. This is the
1547 name that will be passed into the RXAFS_RemoveFile() call.
1548 This parameter is optional. If not provided, the value will
1551 \param[in] came Client name to be unlinked. This name will be used
1552 to update the local directory caches.
1554 \param[in] userp cm_user_t for the request.
1556 \param[in] reqp Request tracker.
1559 long cm_Unlink(cm_scache_t *dscp, fschar_t *fnamep, clientchar_t * cnamep,
1560 cm_user_t *userp, cm_req_t *reqp)
1566 AFSFetchStatus newDirStatus;
1568 struct rx_connection * rxconnp;
1570 cm_scache_t *scp = NULL;
1571 int free_fnamep = FALSE;
1573 memset(&volSync, 0, sizeof(volSync));
1575 if (fnamep == NULL) {
1578 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ,
1579 CM_DIROP_FLAG_NONE, &dirop);
1581 code = cm_BPlusDirLookupOriginalName(&dirop, cnamep, &fnamep);
1584 cm_EndDirOp(&dirop);
1591 #ifdef AFS_FREELANCE_CLIENT
1592 if (cm_freelanceEnabled && dscp == cm_data.rootSCachep) {
1593 /* deleting a mount point from the root dir. */
1594 code = cm_FreelanceRemoveMount(fnamep);
1599 code = cm_Lookup(dscp, cnamep, CM_FLAG_NOMOUNTCHASE, userp, reqp, &scp);
1603 /* Check for RO volume */
1604 if (dscp->flags & CM_SCACHEFLAG_RO) {
1605 code = CM_ERROR_READONLY;
1609 /* make sure we don't screw up the dir status during the merge */
1610 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE,
1611 CM_DIROP_FLAG_NONE, &dirop);
1613 lock_ObtainWrite(&dscp->rw);
1614 sflags = CM_SCACHESYNC_STOREDATA;
1615 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, sflags);
1616 lock_ReleaseWrite(&dscp->rw);
1618 cm_EndDirOp(&dirop);
1623 afsFid.Volume = dscp->fid.volume;
1624 afsFid.Vnode = dscp->fid.vnode;
1625 afsFid.Unique = dscp->fid.unique;
1627 osi_Log1(afsd_logp, "CALL RemoveFile scp 0x%p", dscp);
1629 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
1633 rxconnp = cm_GetRxConn(connp);
1634 code = RXAFS_RemoveFile(rxconnp, &afsFid, fnamep,
1635 &newDirStatus, &volSync);
1636 rx_PutConnection(rxconnp);
1638 } while (cm_Analyze(connp, userp, reqp, &dscp->fid, &volSync, NULL, NULL, code));
1639 code = cm_MapRPCError(code, reqp);
1642 osi_Log1(afsd_logp, "CALL RemoveFile FAILURE, code 0x%x", code);
1644 osi_Log0(afsd_logp, "CALL RemoveFile SUCCESS");
1647 lock_ObtainWrite(&dirop.scp->dirlock);
1648 dirop.lockType = CM_DIRLOCK_WRITE;
1650 lock_ObtainWrite(&dscp->rw);
1651 cm_dnlcRemove(dscp, cnamep);
1653 cm_MergeStatus(NULL, dscp, &newDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
1654 } else if (code == CM_ERROR_NOSUCHFILE) {
1655 /* windows would not have allowed the request to delete the file
1656 * if it did not believe the file existed. therefore, we must
1657 * have an inconsistent view of the world.
1659 dscp->cbServerp = NULL;
1661 cm_SyncOpDone(dscp, NULL, sflags);
1662 lock_ReleaseWrite(&dscp->rw);
1664 if (code == 0 && cm_CheckDirOpForSingleChange(&dirop) && cnamep) {
1665 cm_DirDeleteEntry(&dirop, fnamep);
1667 cm_BPlusDirDeleteEntry(&dirop, cnamep);
1670 cm_EndDirOp(&dirop);
1673 cm_ReleaseSCache(scp);
1675 lock_ObtainWrite(&scp->rw);
1676 if (--scp->linkCount == 0) {
1677 scp->flags |= CM_SCACHEFLAG_DELETED;
1678 lock_ObtainWrite(&cm_scacheLock);
1679 cm_AdjustScacheLRU(scp);
1680 cm_RemoveSCacheFromHashTable(scp);
1681 lock_ReleaseWrite(&cm_scacheLock);
1683 cm_DiscardSCache(scp);
1684 lock_ReleaseWrite(&scp->rw);
1695 /* called with a write locked vnode, and fills in the link info.
1696 * returns this the vnode still write locked.
1698 long cm_HandleLink(cm_scache_t *linkScp, cm_user_t *userp, cm_req_t *reqp)
1702 lock_AssertWrite(&linkScp->rw);
1703 if (!linkScp->mountPointStringp[0]) {
1705 #ifdef AFS_FREELANCE_CLIENT
1706 /* File servers do not have data for freelance entries */
1707 if (cm_freelanceEnabled &&
1708 linkScp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1709 linkScp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
1711 code = cm_FreelanceFetchMountPointString(linkScp);
1713 #endif /* AFS_FREELANCE_CLIENT */
1715 char temp[MOUNTPOINTLEN];
1718 /* read the link data from the file server */
1719 offset.LowPart = offset.HighPart = 0;
1720 code = cm_GetData(linkScp, &offset, temp, MOUNTPOINTLEN, userp, reqp);
1725 * linkScp->length is the actual length of the symlink target string.
1726 * It is current because cm_GetData merged the most up to date
1727 * status info into scp and has not dropped the rwlock since.
1729 if (linkScp->length.LowPart > MOUNTPOINTLEN - 1)
1730 return CM_ERROR_TOOBIG;
1731 if (linkScp->length.LowPart == 0)
1732 return CM_ERROR_INVAL;
1734 /* make sure we are NUL terminated */
1735 temp[linkScp->length.LowPart] = 0;
1736 memcpy(linkScp->mountPointStringp, temp, linkScp->length.LowPart + 1);
1739 if ( !strnicmp(linkScp->mountPointStringp, "msdfs:", strlen("msdfs:")) )
1740 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1742 } /* don't have symlink contents cached */
1747 /* called with a held vnode and a path suffix, with the held vnode being a
1748 * symbolic link. Our goal is to generate a new path to interpret, and return
1749 * this new path in newSpaceBufferp. If the new vnode is relative to a dir
1750 * other than the directory containing the symbolic link, then the new root is
1751 * returned in *newRootScpp, otherwise a null is returned there.
1753 long cm_AssembleLink(cm_scache_t *linkScp, fschar_t *pathSuffixp,
1754 cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1755 cm_user_t *userp, cm_req_t *reqp)
1762 *newRootScpp = NULL;
1763 *newSpaceBufferp = NULL;
1765 lock_ObtainWrite(&linkScp->rw);
1767 * Do not get status if we do not already have a callback.
1768 * The process of reading the symlink string will obtain status information
1769 * in a single RPC. No reason to add a second round trip.
1771 * If we do have a callback, use cm_SyncOp to get status in case the
1772 * current cm_user_t is not the same as the one that obtained the
1773 * symlink string contents.
1775 if (cm_HaveCallback(linkScp)) {
1776 code = cm_SyncOp(linkScp, NULL, userp, reqp, 0,
1777 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1779 lock_ReleaseWrite(&linkScp->rw);
1780 cm_ReleaseSCache(linkScp);
1783 cm_SyncOpDone(linkScp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1785 code = cm_HandleLink(linkScp, userp, reqp);
1789 /* if we may overflow the buffer, bail out; buffer is signficantly
1790 * bigger than max path length, so we don't really have to worry about
1791 * being a little conservative here.
1793 if (cm_FsStrLen(linkScp->mountPointStringp) + cm_FsStrLen(pathSuffixp) + 2
1794 >= CM_UTILS_SPACESIZE) {
1795 code = CM_ERROR_TOOBIG;
1799 tsp = cm_GetSpace();
1800 linkp = linkScp->mountPointStringp;
1801 if (strncmp(linkp, cm_mountRoot, cm_mountRootLen) == 0) {
1802 if (strlen(linkp) > cm_mountRootLen)
1803 StringCbCopyA((char *) tsp->data, sizeof(tsp->data), linkp+cm_mountRootLen+1);
1806 *newRootScpp = cm_RootSCachep(userp, reqp);
1807 cm_HoldSCache(*newRootScpp);
1808 } else if (linkp[0] == '\\' && linkp[1] == '\\') {
1809 if (!strnicmp(&linkp[2], cm_NetbiosName, (len = (long)strlen(cm_NetbiosName))))
1811 char * p = &linkp[len + 3];
1812 if (strnicmp(p, "all", 3) == 0)
1815 StringCbCopyA(tsp->data, sizeof(tsp->data), p);
1816 for (p = tsp->data; *p; p++) {
1820 *newRootScpp = cm_RootSCachep(userp, reqp);
1821 cm_HoldSCache(*newRootScpp);
1823 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1824 StringCchCopyA(tsp->data,lengthof(tsp->data), linkp);
1825 code = CM_ERROR_PATH_NOT_COVERED;
1827 } else if ( linkScp->fileType == CM_SCACHETYPE_DFSLINK ||
1828 !strnicmp(linkp, "msdfs:", (len = (long)strlen("msdfs:"))) ) {
1829 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1830 StringCchCopyA(tsp->data,lengthof(tsp->data), linkp);
1831 code = CM_ERROR_PATH_NOT_COVERED;
1832 } else if (*linkp == '\\' || *linkp == '/') {
1834 /* formerly, this was considered to be from the AFS root,
1835 * but this seems to create problems. instead, we will just
1836 * reject the link */
1837 StringCchCopyA(tsp->data,lengthof(tsp->data), linkp+1);
1838 *newRootScpp = cm_RootSCachep(userp, reqp);
1839 cm_HoldSCache(*newRootScpp);
1841 /* we still copy the link data into the response so that
1842 * the user can see what the link points to
1844 linkScp->fileType = CM_SCACHETYPE_INVALID;
1845 StringCchCopyA(tsp->data,lengthof(tsp->data), linkp);
1846 code = CM_ERROR_NOSUCHPATH;
1849 /* a relative link */
1850 StringCchCopyA(tsp->data,lengthof(tsp->data), linkp);
1852 if (pathSuffixp[0] != 0) { /* if suffix string is non-null */
1853 StringCchCatA(tsp->data,lengthof(tsp->data), "\\");
1854 StringCchCatA(tsp->data,lengthof(tsp->data), pathSuffixp);
1858 clientchar_t * cpath = cm_FsStringToClientStringAlloc(tsp->data, -1, NULL);
1859 if (cpath != NULL) {
1860 cm_ClientStrCpy(tsp->wdata, lengthof(tsp->wdata), cpath);
1862 *newSpaceBufferp = tsp;
1864 code = CM_ERROR_NOSUCHPATH;
1871 if (code == CM_ERROR_PATH_NOT_COVERED && reqp->tidPathp && reqp->relPathp) {
1872 cm_VolStatus_Notify_DFS_Mapping(linkScp, reqp->tidPathp, reqp->relPathp);
1877 lock_ReleaseWrite(&linkScp->rw);
1880 #ifdef DEBUG_REFCOUNT
1881 long cm_NameIDbg(cm_scache_t *rootSCachep, clientchar_t *pathp, long flags,
1882 cm_user_t *userp, clientchar_t *tidPathp, cm_req_t *reqp,
1883 cm_scache_t **outScpp,
1884 char * file, long line)
1886 long cm_NameI(cm_scache_t *rootSCachep, clientchar_t *pathp, long flags,
1887 cm_user_t *userp, clientchar_t *tidPathp,
1888 cm_req_t *reqp, cm_scache_t **outScpp)
1892 clientchar_t *tp; /* ptr moving through input buffer */
1893 clientchar_t tc; /* temp char */
1894 int haveComponent; /* has new component started? */
1895 clientchar_t component[AFSPATHMAX]; /* this is the new component */
1896 clientchar_t *cp; /* component name being assembled */
1897 cm_scache_t *tscp; /* current location in the hierarchy */
1898 cm_scache_t *nscp; /* next dude down */
1899 cm_scache_t *dirScp; /* last dir we searched */
1900 cm_scache_t *linkScp; /* new root for the symlink we just
1902 cm_space_t *psp; /* space for current path, if we've hit
1904 cm_space_t *tempsp; /* temp vbl */
1905 clientchar_t *restp; /* rest of the pathname to interpret */
1906 int symlinkCount; /* count of # of symlinks traversed */
1907 int extraFlag; /* avoid chasing mt pts for dir cmd */
1908 int phase = 1; /* 1 = tidPathp, 2 = pathp */
1909 #define MAX_FID_COUNT 512
1910 cm_fid_t fids[MAX_FID_COUNT]; /* array of fids processed in this path walk */
1911 int fid_count = 0; /* number of fids processed in this path walk */
1916 #ifdef DEBUG_REFCOUNT
1917 afsi_log("%s:%d cm_NameI rootscp 0x%p ref %d", file, line, rootSCachep, rootSCachep->refCount);
1918 osi_Log4(afsd_logp,"cm_NameI rootscp 0x%p path %S tidpath %S flags 0x%x",
1919 rootSCachep, pathp ? pathp : L"<NULL>", tidPathp ? tidPathp : L"<NULL>",
1934 cm_HoldSCache(tscp);
1942 /* map Unix slashes into DOS ones so we can interpret Unix
1948 if (!haveComponent) {
1951 } else if (tc == 0) {
1965 /* we have a component here */
1966 if (tc == 0 || tc == '\\') {
1967 /* end of the component; we're at the last
1968 * component if tc == 0. However, if the last
1969 * is a symlink, we have more to do.
1971 *cp++ = 0; /* add null termination */
1973 if ((flags & CM_FLAG_DIRSEARCH) && tc == 0)
1974 extraFlag = CM_FLAG_NOMOUNTCHASE;
1975 code = cm_Lookup(tscp, component,
1977 userp, reqp, &nscp);
1980 if (!cm_ClientStrCmp(component,_C("..")) ||
1981 !cm_ClientStrCmp(component,_C("."))) {
1983 * roll back the fid list until we find the
1984 * fid that matches where we are now. Its not
1985 * necessarily one or two fids because they
1986 * might have been symlinks or mount points or
1987 * both that were crossed.
1989 for ( i=fid_count-1; i>=0; i--) {
1990 if (!cm_FidCmp(&nscp->fid, &fids[i]))
1995 /* add the new fid to the list */
1996 if (fid_count == MAX_FID_COUNT) {
1997 code = CM_ERROR_TOO_MANY_SYMLINKS;
1998 cm_ReleaseSCache(nscp);
2002 fids[fid_count++] = nscp->fid;
2007 cm_ReleaseSCache(tscp);
2009 cm_ReleaseSCache(dirScp);
2012 if ((code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) &&
2013 tscp->fileType == CM_SCACHETYPE_SYMLINK) {
2014 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_NOSUCHPATH");
2015 return CM_ERROR_NOSUCHPATH;
2017 osi_Log1(afsd_logp,"cm_NameI code 0x%x", code);
2022 haveComponent = 0; /* component done */
2024 cm_ReleaseSCache(dirScp);
2025 dirScp = tscp; /* for some symlinks */
2026 tscp = nscp; /* already held */
2028 if (tc == 0 && !(flags & CM_FLAG_FOLLOW) && phase == 2) {
2031 cm_ReleaseSCache(dirScp);
2037 /* now, if tscp is a symlink, we should follow it and
2038 * assemble the path again.
2040 lock_ObtainWrite(&tscp->rw);
2041 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
2042 CM_SCACHESYNC_GETSTATUS
2043 | CM_SCACHESYNC_NEEDCALLBACK);
2045 lock_ReleaseWrite(&tscp->rw);
2046 cm_ReleaseSCache(tscp);
2049 cm_ReleaseSCache(dirScp);
2054 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2056 if (tscp->fileType == CM_SCACHETYPE_SYMLINK) {
2057 /* this is a symlink; assemble a new buffer */
2058 lock_ReleaseWrite(&tscp->rw);
2059 if (symlinkCount++ >= MAX_SYMLINK_COUNT) {
2060 cm_ReleaseSCache(tscp);
2063 cm_ReleaseSCache(dirScp);
2068 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_TOO_MANY_SYMLINKS");
2069 return CM_ERROR_TOO_MANY_SYMLINKS;
2079 /* TODO: make this better */
2080 frestp = cm_ClientStringToFsStringAlloc(restp, -1, NULL);
2081 code = cm_AssembleLink(tscp, frestp, &linkScp, &tempsp, userp, reqp);
2085 if (code == 0 && linkScp != NULL) {
2086 if (linkScp == cm_data.rootSCachep) {
2090 for ( i=0; i<fid_count; i++) {
2091 if ( !cm_FidCmp(&linkScp->fid, &fids[i]) ) {
2092 code = CM_ERROR_TOO_MANY_SYMLINKS;
2093 cm_ReleaseSCache(linkScp);
2099 if (i == fid_count && fid_count < MAX_FID_COUNT) {
2100 fids[fid_count++] = linkScp->fid;
2105 /* something went wrong */
2106 cm_ReleaseSCache(tscp);
2109 cm_ReleaseSCache(dirScp);
2115 /* otherwise, tempsp has the new path,
2116 * and linkScp is the new root from
2117 * which to interpret that path.
2118 * Continue with the namei processing,
2119 * also doing the bookkeeping for the
2120 * space allocation and tracking the
2121 * vnode reference counts.
2127 cm_ReleaseSCache(tscp);
2132 * now, if linkScp is null, that's
2133 * AssembleLink's way of telling us that
2134 * the sym link is relative to the dir
2135 * containing the link. We have a ref
2136 * to it in dirScp, and we hold it now
2137 * and reuse it as the new spot in the
2145 /* not a symlink, we may be done */
2146 lock_ReleaseWrite(&tscp->rw);
2154 cm_ReleaseSCache(dirScp);
2162 cm_ReleaseSCache(dirScp);
2165 } /* end of a component */
2168 } /* we have a component */
2169 } /* big while loop over all components */
2173 cm_ReleaseSCache(dirScp);
2179 cm_ReleaseSCache(tscp);
2181 #ifdef DEBUG_REFCOUNT
2182 afsi_log("%s:%d cm_NameI code 0x%x outScpp 0x%p ref %d", file, line, code, *outScpp, (*outScpp) ? (*outScpp)->refCount : 0);
2184 osi_Log2(afsd_logp,"cm_NameI code 0x%x outScpp 0x%p", code, *outScpp);
2188 /* called with a dir, and a vnode within the dir that happens to be a symlink.
2189 * We chase the link, and return a held pointer to the target, if it exists,
2190 * in *outScpp. If we succeed, we return 0, otherwise we return an error code
2191 * and do not hold or return a target vnode.
2193 * This is very similar to calling cm_NameI with the last component of a name,
2194 * which happens to be a symlink, except that we've already passed by the name.
2196 * This function is typically called by the directory listing functions, which
2197 * encounter symlinks but need to return the proper file length so programs
2198 * like "more" work properly when they make use of the attributes retrieved from
2201 * The input vnode should not be locked when this function is called.
2203 long cm_EvaluateSymLink(cm_scache_t *dscp, cm_scache_t *linkScp,
2204 cm_scache_t **outScpp, cm_user_t *userp, cm_req_t *reqp)
2208 cm_scache_t *newRootScp;
2212 osi_Log1(afsd_logp, "Evaluating symlink scp 0x%p", linkScp);
2214 code = cm_AssembleLink(linkScp, "", &newRootScp, &spacep, userp, reqp);
2218 /* now, if newRootScp is NULL, we're really being told that the symlink
2219 * is relative to the current directory (dscp).
2221 if (newRootScp == NULL) {
2223 cm_HoldSCache(dscp);
2226 code = cm_NameI(newRootScp, spacep->wdata,
2227 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW | CM_FLAG_DIRSEARCH,
2228 userp, NULL, reqp, outScpp);
2230 if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH)
2231 code = CM_ERROR_NOSUCHPATH;
2233 /* this stuff is allocated no matter what happened on the namei call,
2235 cm_FreeSpace(spacep);
2236 cm_ReleaseSCache(newRootScp);
2238 if (linkScp == *outScpp) {
2239 cm_ReleaseSCache(*outScpp);
2241 code = CM_ERROR_NOSUCHPATH;
2247 /* for a given entry, make sure that it isn't in the stat cache, and then
2248 * add it to the list of file IDs to be obtained.
2250 * Don't bother adding it if we already have a vnode. Note that the dir
2251 * is locked, so we have to be careful checking the vnode we're thinking of
2252 * processing, to avoid deadlocks.
2254 long cm_TryBulkProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
2265 /* Don't overflow bsp. */
2266 if (bsp->counter >= CM_BULKMAX)
2267 return CM_ERROR_STOPNOW;
2269 thyper.LowPart = cm_data.buf_blockSize;
2270 thyper.HighPart = 0;
2271 thyper = LargeIntegerAdd(thyper, bsp->bufOffset);
2273 /* thyper is now the first byte past the end of the record we're
2274 * interested in, and bsp->bufOffset is the first byte of the record
2275 * we're interested in.
2276 * Skip data in the others.
2279 if (LargeIntegerLessThan(*offp, bsp->bufOffset))
2281 if (LargeIntegerGreaterThanOrEqualTo(*offp, thyper))
2282 return CM_ERROR_STOPNOW;
2283 if (strcmp(dep->name, ".") == 0 || strcmp(dep->name, "..") == 0)
2286 cm_SetFid(&tfid, scp->fid.cell, scp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
2287 tscp = cm_FindSCache(&tfid);
2289 if (lock_TryWrite(&tscp->rw)) {
2290 /* we have an entry that we can look at */
2291 if (!(tscp->flags & CM_SCACHEFLAG_EACCESS) && cm_HaveCallback(tscp)) {
2292 /* we have a callback on it. Don't bother
2293 * fetching this stat entry, since we're happy
2294 * with the info we have.
2296 lock_ReleaseWrite(&tscp->rw);
2297 cm_ReleaseSCache(tscp);
2300 lock_ReleaseWrite(&tscp->rw);
2302 cm_ReleaseSCache(tscp);
2305 #ifdef AFS_FREELANCE_CLIENT
2306 // yj: if this is a mountpoint under root.afs then we don't want it
2307 // to be bulkstat-ed, instead, we call getSCache directly and under
2308 // getSCache, it is handled specially.
2309 if ( cm_freelanceEnabled &&
2310 tfid.cell==AFS_FAKE_ROOT_CELL_ID &&
2311 tfid.volume==AFS_FAKE_ROOT_VOL_ID &&
2312 !(tfid.vnode==0x1 && tfid.unique==0x1) )
2314 osi_Log0(afsd_logp, "cm_TryBulkProc Freelance calls cm_SCache on root.afs mountpoint");
2315 return cm_GetSCache(&tfid, &tscp, NULL, NULL);
2317 #endif /* AFS_FREELANCE_CLIENT */
2320 bsp->fids[i].Volume = scp->fid.volume;
2321 bsp->fids[i].Vnode = tfid.vnode;
2322 bsp->fids[i].Unique = tfid.unique;
2327 cm_TryBulkStatRPC(cm_scache_t *dscp, cm_bulkStat_t *bbp, cm_user_t *userp, cm_req_t *reqp)
2330 AFSCBFids fidStruct;
2331 AFSBulkStats statStruct;
2333 AFSCBs callbackStruct;
2336 cm_callbackRequest_t cbReq;
2342 struct rx_connection * rxconnp;
2343 int inlinebulk; /* Did we use InlineBulkStatus RPC or not? */
2345 memset(&volSync, 0, sizeof(volSync));
2347 /* otherwise, we may have one or more bulk stat's worth of stuff in bb;
2348 * make the calls to create the entries. Handle AFSCBMAX files at a
2351 for (filex = 0; filex < bbp->counter; filex += filesThisCall) {
2352 filesThisCall = bbp->counter - filex;
2353 if (filesThisCall > AFSCBMAX)
2354 filesThisCall = AFSCBMAX;
2356 fidStruct.AFSCBFids_len = filesThisCall;
2357 fidStruct.AFSCBFids_val = &bbp->fids[filex];
2358 statStruct.AFSBulkStats_len = filesThisCall;
2359 statStruct.AFSBulkStats_val = &bbp->stats[filex];
2360 callbackStruct.AFSCBs_len = filesThisCall;
2361 callbackStruct.AFSCBs_val = &bbp->callbacks[filex];
2362 cm_StartCallbackGrantingCall(NULL, &cbReq);
2363 osi_Log1(afsd_logp, "CALL BulkStatus, %d entries", filesThisCall);
2366 * Whenever cm_Analyze is called for a RXAFS_ RPC there must
2367 * be a FID provided. However, the error code from RXAFS_BulkStatus
2368 * or RXAFS_InlinkBulkStatus does not apply to any FID. Therefore,
2369 * we generate an invalid FID to match with the RPC error.
2371 cm_SetFid(&tfid, dscp->fid.cell, dscp->fid.volume, 0, 0);
2376 code = cm_ConnFromFID(&tfid, userp, reqp, &connp);
2380 rxconnp = cm_GetRxConn(connp);
2381 if (!(connp->serverp->flags & CM_SERVERFLAG_NOINLINEBULK)) {
2382 code = RXAFS_InlineBulkStatus(rxconnp, &fidStruct,
2383 &statStruct, &callbackStruct, &volSync);
2384 if (code == RXGEN_OPCODE) {
2385 cm_SetServerNoInlineBulk(connp->serverp, 0);
2391 code = RXAFS_BulkStatus(rxconnp, &fidStruct,
2392 &statStruct, &callbackStruct, &volSync);
2394 rx_PutConnection(rxconnp);
2397 * If InlineBulk RPC was called and it succeeded,
2398 * then pull out the return code from the status info
2399 * and use it for cm_Analyze so that we can failover to other
2400 * .readonly volume instances. But only do it for errors that
2401 * are volume global.
2403 if (inlinebulk && code == 0 && (&bbp->stats[0])->errorCode) {
2404 osi_Log1(afsd_logp, "cm_TryBulkStat inline-bulk stat error: %d",
2405 (&bbp->stats[0])->errorCode);
2406 switch ((&bbp->stats[0])->errorCode) {
2415 code = (&bbp->stats[0])->errorCode;
2418 /* Rx and Rxkad errors are volume global */
2419 if ( (&bbp->stats[0])->errorCode >= -64 && (&bbp->stats[0])->errorCode < 0 ||
2420 (&bbp->stats[0])->errorCode >= ERROR_TABLE_BASE_RXK && (&bbp->stats[0])->errorCode < ERROR_TABLE_BASE_RXK + 256)
2421 code = (&bbp->stats[0])->errorCode;
2424 } while (cm_Analyze(connp, userp, reqp, &tfid, &volSync, NULL, &cbReq, code));
2425 code = cm_MapRPCError(code, reqp);
2428 * might as well quit on an error, since we're not going to do
2429 * much better on the next immediate call, either.
2432 osi_Log2(afsd_logp, "CALL %sBulkStatus FAILURE code 0x%x",
2433 inlinebulk ? "Inline" : "", code);
2434 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, NULL, 0);
2439 * The bulk RPC has succeeded or at least not failed with a
2440 * volume global error result. For items that have inlineBulk
2441 * errors we must call cm_Analyze in order to perform required
2442 * logging of errors.
2444 * If the RPC was not inline bulk or the entry either has no error
2445 * the status must be merged.
2447 osi_Log1(afsd_logp, "CALL %sBulkStatus SUCCESS", inlinebulk ? "Inline" : "");
2449 for (i = 0; i<filesThisCall; i++) {
2451 cm_SetFid(&tfid, dscp->fid.cell, bbp->fids[j].Volume, bbp->fids[j].Vnode, bbp->fids[j].Unique);
2453 if (inlinebulk && (&bbp->stats[j])->errorCode) {
2454 cm_req_t treq = *reqp;
2455 cm_Analyze(NULL, userp, &treq, &tfid, &volSync, NULL, &cbReq, (&bbp->stats[j])->errorCode);
2457 code = cm_GetSCache(&tfid, &scp, userp, reqp);
2462 * otherwise, if this entry has no callback info,
2463 * merge in this. If there is existing callback info
2464 * we skip the merge because the existing data must be
2465 * current (we have a callback) and the response from
2466 * a non-inline bulk rpc might actually be wrong.
2468 * now, we have to be extra paranoid on merging in this
2469 * information, since we didn't use cm_SyncOp before
2470 * starting the fetch to make sure that no bad races
2471 * were occurring. Specifically, we need to make sure
2472 * we don't obliterate any newer information in the
2473 * vnode than have here.
2475 * Right now, be pretty conservative: if there's a
2476 * callback or a pending call, skip it.
2477 * However, if the prior attempt to obtain status
2478 * was refused access or the volume is .readonly,
2479 * take the data in any case since we have nothing
2480 * better for the in flight directory enumeration that
2481 * resulted in this function being called.
2483 lock_ObtainRead(&scp->rw);
2484 if ((scp->cbServerp == NULL &&
2485 !(scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING))) ||
2486 (scp->flags & CM_SCACHEFLAG_PURERO) ||
2487 (scp->flags & CM_SCACHEFLAG_EACCESS))
2489 lock_ConvertRToW(&scp->rw);
2490 cm_EndCallbackGrantingCall(scp, &cbReq,
2493 CM_CALLBACK_MAINTAINCOUNT);
2494 cm_MergeStatus(dscp, scp, &bbp->stats[j], &volSync, userp, reqp, 0);
2495 lock_ReleaseWrite(&scp->rw);
2497 lock_ReleaseRead(&scp->rw);
2499 cm_ReleaseSCache(scp);
2501 } /* all files in the response */
2502 /* now tell it to drop the count,
2503 * after doing the vnode processing above */
2504 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, NULL, 0);
2505 } /* while there are still more files to process */
2510 /* called with a write locked scp and a pointer to a buffer. Make bulk stat
2511 * calls on all undeleted files in the page of the directory specified.
2514 cm_TryBulkStat(cm_scache_t *dscp, osi_hyper_t *offsetp, cm_user_t *userp,
2520 osi_Log1(afsd_logp, "cm_TryBulkStat dir 0x%p", dscp);
2522 /* should be on a buffer boundary */
2523 osi_assertx((offsetp->LowPart & (cm_data.buf_blockSize - 1)) == 0, "invalid offset");
2525 bbp = malloc(sizeof(cm_bulkStat_t));
2526 memset(bbp, 0, sizeof(cm_bulkStat_t));
2527 bbp->bufOffset = *offsetp;
2529 lock_ReleaseWrite(&dscp->rw);
2530 /* first, assemble the file IDs we need to stat */
2531 code = cm_ApplyDir(dscp, cm_TryBulkProc, (void *) bbp, offsetp, userp, reqp, NULL);
2533 /* if we failed, bail out early */
2534 if (code && code != CM_ERROR_STOPNOW) {
2536 lock_ObtainWrite(&dscp->rw);
2540 code = cm_TryBulkStatRPC(dscp, bbp, userp, reqp);
2541 osi_Log1(afsd_logp, "END cm_TryBulkStat code 0x%x", code);
2543 lock_ObtainWrite(&dscp->rw);
2548 void cm_StatusFromAttr(AFSStoreStatus *statusp, cm_scache_t *scp, cm_attr_t *attrp)
2552 /* initialize store back mask as inexpensive local variable */
2554 memset(statusp, 0, sizeof(AFSStoreStatus));
2556 /* copy out queued info from scache first, if scp passed in */
2558 if (scp->mask & CM_SCACHEMASK_CLIENTMODTIME) {
2559 statusp->ClientModTime = scp->clientModTime;
2560 mask |= AFS_SETMODTIME;
2561 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
2566 /* now add in our locally generated request */
2567 if (attrp->mask & CM_ATTRMASK_CLIENTMODTIME) {
2568 statusp->ClientModTime = attrp->clientModTime;
2569 mask |= AFS_SETMODTIME;
2571 if (attrp->mask & CM_ATTRMASK_UNIXMODEBITS) {
2572 statusp->UnixModeBits = attrp->unixModeBits;
2573 mask |= AFS_SETMODE;
2575 if (attrp->mask & CM_ATTRMASK_OWNER) {
2576 statusp->Owner = attrp->owner;
2577 mask |= AFS_SETOWNER;
2579 if (attrp->mask & CM_ATTRMASK_GROUP) {
2580 statusp->Group = attrp->group;
2581 mask |= AFS_SETGROUP;
2584 statusp->Mask = mask;
2587 /* set the file size, and make sure that all relevant buffers have been
2588 * truncated. Ensure that any partially truncated buffers have been zeroed
2589 * to the end of the buffer.
2591 long cm_SetLength(cm_scache_t *scp, osi_hyper_t *sizep, cm_user_t *userp,
2597 /* start by locking out buffer creation */
2598 lock_ObtainWrite(&scp->bufCreateLock);
2600 /* verify that this is a file, not a dir or a symlink */
2601 lock_ObtainWrite(&scp->rw);
2602 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
2603 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2606 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2608 if (scp->fileType != CM_SCACHETYPE_FILE) {
2609 code = CM_ERROR_ISDIR;
2614 if (LargeIntegerLessThan(*sizep, scp->length))
2619 lock_ReleaseWrite(&scp->rw);
2621 /* can't hold scp->rw lock here, since we may wait for a storeback to
2622 * finish if the buffer package is cleaning a buffer by storing it to
2626 buf_Truncate(scp, userp, reqp, sizep);
2628 /* now ensure that file length is short enough, and update truncPos */
2629 lock_ObtainWrite(&scp->rw);
2631 /* make sure we have a callback (so we have the right value for the
2632 * length), and wait for it to be safe to do a truncate.
2634 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_WRITE,
2635 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2636 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2638 /* If we only have 'i' bits, then we should still be able to set
2639 the size of a file we created. */
2640 if (code == CM_ERROR_NOACCESS && scp->creator == userp) {
2641 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
2642 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2643 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2649 if (LargeIntegerLessThan(*sizep, scp->length)) {
2650 /* a real truncation. If truncPos is not set yet, or is bigger
2651 * than where we're truncating the file, set truncPos to this
2656 if (!(scp->mask & CM_SCACHEMASK_TRUNCPOS)
2657 || LargeIntegerLessThan(*sizep, scp->length)) {
2659 scp->truncPos = *sizep;
2660 scp->mask |= CM_SCACHEMASK_TRUNCPOS;
2662 /* in either case, the new file size has been changed */
2663 scp->length = *sizep;
2664 scp->mask |= CM_SCACHEMASK_LENGTH;
2666 else if (LargeIntegerGreaterThan(*sizep, scp->length)) {
2667 /* really extending the file */
2668 scp->length = *sizep;
2669 scp->mask |= CM_SCACHEMASK_LENGTH;
2672 /* done successfully */
2675 cm_SyncOpDone(scp, NULL,
2676 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2677 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2680 lock_ReleaseWrite(&scp->rw);
2681 lock_ReleaseWrite(&scp->bufCreateLock);
2686 /* set the file size or other attributes (but not both at once) */
2687 long cm_SetAttr(cm_scache_t *scp, cm_attr_t *attrp, cm_user_t *userp,
2691 AFSFetchStatus afsOutStatus;
2695 AFSStoreStatus afsInStatus;
2696 struct rx_connection * rxconnp;
2698 memset(&volSync, 0, sizeof(volSync));
2700 /* handle file length setting */
2701 if (attrp->mask & CM_ATTRMASK_LENGTH)
2702 return cm_SetLength(scp, &attrp->length, userp, reqp);
2704 lock_ObtainWrite(&scp->rw);
2705 /* Check for RO volume */
2706 if (scp->flags & CM_SCACHEFLAG_RO) {
2707 code = CM_ERROR_READONLY;
2708 lock_ReleaseWrite(&scp->rw);
2712 /* otherwise, we have to make an RPC to get the status */
2713 code = cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STORESTATUS);
2715 lock_ReleaseWrite(&scp->rw);
2718 lock_ConvertWToR(&scp->rw);
2720 /* make the attr structure */
2721 cm_StatusFromAttr(&afsInStatus, scp, attrp);
2723 tfid.Volume = scp->fid.volume;
2724 tfid.Vnode = scp->fid.vnode;
2725 tfid.Unique = scp->fid.unique;
2726 lock_ReleaseRead(&scp->rw);
2728 /* now make the RPC */
2729 osi_Log1(afsd_logp, "CALL StoreStatus scp 0x%p", scp);
2731 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2735 rxconnp = cm_GetRxConn(connp);
2736 code = RXAFS_StoreStatus(rxconnp, &tfid,
2737 &afsInStatus, &afsOutStatus, &volSync);
2738 rx_PutConnection(rxconnp);
2740 } while (cm_Analyze(connp, userp, reqp,
2741 &scp->fid, &volSync, NULL, NULL, code));
2742 code = cm_MapRPCError(code, reqp);
2745 osi_Log1(afsd_logp, "CALL StoreStatus FAILURE, code 0x%x", code);
2747 osi_Log0(afsd_logp, "CALL StoreStatus SUCCESS");
2749 lock_ObtainWrite(&scp->rw);
2751 cm_MergeStatus(NULL, scp, &afsOutStatus, &volSync, userp, reqp,
2752 CM_MERGEFLAG_FORCE|CM_MERGEFLAG_STOREDATA);
2753 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STORESTATUS);
2755 /* if we're changing the mode bits, discard the ACL cache,
2756 * since we changed the mode bits.
2758 if (afsInStatus.Mask & AFS_SETMODE)
2759 cm_FreeAllACLEnts(scp);
2760 lock_ReleaseWrite(&scp->rw);
2764 long cm_Create(cm_scache_t *dscp, clientchar_t *cnamep, long flags, cm_attr_t *attrp,
2765 cm_scache_t **scpp, cm_user_t *userp, cm_req_t *reqp)
2770 cm_callbackRequest_t cbReq;
2773 cm_scache_t *scp = NULL;
2775 AFSStoreStatus inStatus;
2776 AFSFetchStatus updatedDirStatus;
2777 AFSFetchStatus newFileStatus;
2778 AFSCallBack newFileCallback;
2780 struct rx_connection * rxconnp;
2782 fschar_t * fnamep = NULL;
2784 memset(&volSync, 0, sizeof(volSync));
2786 /* can't create names with @sys in them; must expand it manually first.
2787 * return "invalid request" if they try.
2789 if (cm_ExpandSysName(cnamep, NULL, 0, 0)) {
2790 return CM_ERROR_ATSYS;
2793 #ifdef AFS_FREELANCE_CLIENT
2794 /* Freelance root volume does not hold files */
2795 if (cm_freelanceEnabled &&
2796 dscp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
2797 dscp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
2799 return CM_ERROR_NOACCESS;
2801 #endif /* AFS_FREELANCE_CLIENT */
2803 /* Check for RO volume */
2804 if (dscp->flags & CM_SCACHEFLAG_RO)
2805 return CM_ERROR_READONLY;
2807 /* before starting the RPC, mark that we're changing the file data, so
2808 * that someone who does a chmod will know to wait until our call
2811 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, CM_DIROP_FLAG_NONE,
2813 lock_ObtainWrite(&dscp->rw);
2814 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2815 lock_ReleaseWrite(&dscp->rw);
2817 cm_StartCallbackGrantingCall(NULL, &cbReq);
2819 cm_EndDirOp(&dirop);
2826 fnamep = cm_ClientStringToFsStringAlloc(cnamep, -1, NULL);
2828 cm_StatusFromAttr(&inStatus, NULL, attrp);
2830 /* try the RPC now */
2831 osi_Log1(afsd_logp, "CALL CreateFile scp 0x%p", dscp);
2833 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2837 dirAFSFid.Volume = dscp->fid.volume;
2838 dirAFSFid.Vnode = dscp->fid.vnode;
2839 dirAFSFid.Unique = dscp->fid.unique;
2841 rxconnp = cm_GetRxConn(connp);
2842 code = RXAFS_CreateFile(connp->rxconnp, &dirAFSFid, fnamep,
2843 &inStatus, &newAFSFid, &newFileStatus,
2844 &updatedDirStatus, &newFileCallback,
2846 rx_PutConnection(rxconnp);
2848 } while (cm_Analyze(connp, userp, reqp,
2849 &dscp->fid, &volSync, NULL, &cbReq, code));
2850 code = cm_MapRPCError(code, reqp);
2853 osi_Log1(afsd_logp, "CALL CreateFile FAILURE, code 0x%x", code);
2855 osi_Log0(afsd_logp, "CALL CreateFile SUCCESS");
2858 lock_ObtainWrite(&dirop.scp->dirlock);
2859 dirop.lockType = CM_DIRLOCK_WRITE;
2861 lock_ObtainWrite(&dscp->rw);
2863 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
2864 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2865 lock_ReleaseWrite(&dscp->rw);
2867 /* now try to create the file's entry, too, but be careful to
2868 * make sure that we don't merge in old info. Since we weren't locking
2869 * out any requests during the file's creation, we may have pretty old
2873 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
2874 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2876 lock_ObtainWrite(&scp->rw);
2877 scp->creator = userp; /* remember who created it */
2878 if (!cm_HaveCallback(scp)) {
2879 cm_EndCallbackGrantingCall(scp, &cbReq,
2880 &newFileCallback, &volSync, 0);
2881 cm_MergeStatus(dscp, scp, &newFileStatus, &volSync,
2885 lock_ReleaseWrite(&scp->rw);
2889 /* make sure we end things properly */
2891 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, NULL, 0);
2893 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2894 cm_DirCreateEntry(&dirop, fnamep, &newFid);
2896 cm_BPlusDirCreateEntry(&dirop, cnamep, &newFid);
2899 cm_EndDirOp(&dirop);
2908 cm_ReleaseSCache(scp);
2914 * locked if TRUE means write-locked
2915 * else the cm_scache_t rw must not be held
2917 long cm_FSync(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp, afs_uint32 locked)
2922 lock_ReleaseWrite(&scp->rw);
2924 osi_Log2(afsd_logp, "cm_FSync scp 0x%p userp 0x%p", scp, userp);
2926 code = buf_CleanVnode(scp, userp, reqp);
2928 lock_ObtainWrite(&scp->rw);
2930 if (scp->mask & (CM_SCACHEMASK_TRUNCPOS
2931 | CM_SCACHEMASK_CLIENTMODTIME
2932 | CM_SCACHEMASK_LENGTH))
2933 code = cm_StoreMini(scp, userp, reqp);
2935 if (scp->flags & (CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE)) {
2936 code = (scp->flags & CM_SCACHEFLAG_OVERQUOTA) ? CM_ERROR_QUOTA : CM_ERROR_SPACE;
2937 scp->flags &= ~(CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE);
2941 lock_ReleaseWrite(&scp->rw);
2942 } else if (locked) {
2943 lock_ObtainWrite(&scp->rw);
2948 long cm_MakeDir(cm_scache_t *dscp, clientchar_t *cnamep, long flags, cm_attr_t *attrp,
2949 cm_user_t *userp, cm_req_t *reqp, cm_scache_t **scpp)
2954 cm_callbackRequest_t cbReq;
2957 cm_scache_t *scp = NULL;
2959 AFSStoreStatus inStatus;
2960 AFSFetchStatus updatedDirStatus;
2961 AFSFetchStatus newDirStatus;
2962 AFSCallBack newDirCallback;
2964 struct rx_connection * rxconnp;
2966 fschar_t * fnamep = NULL;
2968 memset(&volSync, 0, sizeof(volSync));
2970 /* can't create names with @sys in them; must expand it manually first.
2971 * return "invalid request" if they try.
2973 if (cm_ExpandSysName(cnamep, NULL, 0, 0)) {
2974 return CM_ERROR_ATSYS;
2977 #ifdef AFS_FREELANCE_CLIENT
2978 /* Freelance root volume does not hold subdirectories */
2979 if (cm_freelanceEnabled &&
2980 dscp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
2981 dscp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
2983 return CM_ERROR_NOACCESS;
2985 #endif /* AFS_FREELANCE_CLIENT */
2987 /* Check for RO volume */
2988 if (dscp->flags & CM_SCACHEFLAG_RO)
2989 return CM_ERROR_READONLY;
2991 /* before starting the RPC, mark that we're changing the directory
2992 * data, so that someone who does a chmod on the dir will wait until
2993 * our call completes.
2995 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, CM_DIROP_FLAG_NONE,
2997 lock_ObtainWrite(&dscp->rw);
2998 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2999 lock_ReleaseWrite(&dscp->rw);
3001 cm_StartCallbackGrantingCall(NULL, &cbReq);
3003 cm_EndDirOp(&dirop);
3010 fnamep = cm_ClientStringToFsStringAlloc(cnamep, -1, NULL);
3011 cm_StatusFromAttr(&inStatus, NULL, attrp);
3013 /* try the RPC now */
3014 osi_Log1(afsd_logp, "CALL MakeDir 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 rxconnp = cm_GetRxConn(connp);
3025 code = RXAFS_MakeDir(connp->rxconnp, &dirAFSFid, fnamep,
3026 &inStatus, &newAFSFid, &newDirStatus,
3027 &updatedDirStatus, &newDirCallback,
3029 rx_PutConnection(rxconnp);
3031 } while (cm_Analyze(connp, userp, reqp,
3032 &dscp->fid, &volSync, NULL, &cbReq, code));
3033 code = cm_MapRPCError(code, reqp);
3036 osi_Log1(afsd_logp, "CALL MakeDir FAILURE, code 0x%x", code);
3038 osi_Log0(afsd_logp, "CALL MakeDir SUCCESS");
3041 lock_ObtainWrite(&dirop.scp->dirlock);
3042 dirop.lockType = CM_DIRLOCK_WRITE;
3044 lock_ObtainWrite(&dscp->rw);
3046 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
3047 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3048 lock_ReleaseWrite(&dscp->rw);
3050 /* now try to create the new dir's entry, too, but be careful to
3051 * make sure that we don't merge in old info. Since we weren't locking
3052 * out any requests during the file's creation, we may have pretty old
3056 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3057 code = cm_GetSCache(&newFid, &scp, userp, reqp);
3059 lock_ObtainWrite(&scp->rw);
3060 if (!cm_HaveCallback(scp)) {
3061 cm_EndCallbackGrantingCall(scp, &cbReq,
3062 &newDirCallback, &volSync, 0);
3063 cm_MergeStatus(dscp, scp, &newDirStatus, &volSync,
3067 lock_ReleaseWrite(&scp->rw);
3071 /* make sure we end things properly */
3073 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, NULL, 0);
3075 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
3076 cm_DirCreateEntry(&dirop, fnamep, &newFid);
3078 cm_BPlusDirCreateEntry(&dirop, cnamep, &newFid);
3081 cm_EndDirOp(&dirop);
3089 cm_ReleaseSCache(scp);
3092 /* and return error code */
3096 long cm_Link(cm_scache_t *dscp, clientchar_t *cnamep, cm_scache_t *sscp, long flags,
3097 cm_user_t *userp, cm_req_t *reqp)
3102 AFSFid existingAFSFid;
3103 AFSFetchStatus updatedDirStatus;
3104 AFSFetchStatus newLinkStatus;
3106 struct rx_connection * rxconnp;
3108 fschar_t * fnamep = NULL;
3110 memset(&volSync, 0, sizeof(volSync));
3112 if (dscp->fid.cell != sscp->fid.cell ||
3113 dscp->fid.volume != sscp->fid.volume) {
3114 return CM_ERROR_CROSSDEVLINK;
3117 /* Check for RO volume */
3118 if (dscp->flags & CM_SCACHEFLAG_RO)
3119 return CM_ERROR_READONLY;
3121 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, CM_DIROP_FLAG_NONE,
3123 lock_ObtainWrite(&dscp->rw);
3124 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3125 lock_ReleaseWrite(&dscp->rw);
3127 cm_EndDirOp(&dirop);
3132 fnamep = cm_ClientStringToFsStringAlloc(cnamep, -1, NULL);
3134 /* try the RPC now */
3135 osi_Log1(afsd_logp, "CALL Link scp 0x%p", dscp);
3137 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3140 dirAFSFid.Volume = dscp->fid.volume;
3141 dirAFSFid.Vnode = dscp->fid.vnode;
3142 dirAFSFid.Unique = dscp->fid.unique;
3144 existingAFSFid.Volume = sscp->fid.volume;
3145 existingAFSFid.Vnode = sscp->fid.vnode;
3146 existingAFSFid.Unique = sscp->fid.unique;
3148 rxconnp = cm_GetRxConn(connp);
3149 code = RXAFS_Link(rxconnp, &dirAFSFid, fnamep, &existingAFSFid,
3150 &newLinkStatus, &updatedDirStatus, &volSync);
3151 rx_PutConnection(rxconnp);
3152 osi_Log1(afsd_logp," RXAFS_Link returns 0x%x", code);
3154 } while (cm_Analyze(connp, userp, reqp,
3155 &dscp->fid, &volSync, NULL, NULL, code));
3157 code = cm_MapRPCError(code, reqp);
3160 osi_Log1(afsd_logp, "CALL Link FAILURE, code 0x%x", code);
3162 osi_Log0(afsd_logp, "CALL Link SUCCESS");
3165 lock_ObtainWrite(&dirop.scp->dirlock);
3166 dirop.lockType = CM_DIRLOCK_WRITE;
3168 lock_ObtainWrite(&dscp->rw);
3170 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
3172 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3173 lock_ReleaseWrite(&dscp->rw);
3176 if (cm_CheckDirOpForSingleChange(&dirop)) {
3177 cm_DirCreateEntry(&dirop, fnamep, &sscp->fid);
3179 cm_BPlusDirCreateEntry(&dirop, cnamep, &sscp->fid);
3183 cm_EndDirOp(&dirop);
3185 /* Update the linked object status */
3187 lock_ObtainWrite(&sscp->rw);
3188 cm_MergeStatus(NULL, sscp, &newLinkStatus, &volSync, userp, reqp, 0);
3189 lock_ReleaseWrite(&sscp->rw);
3197 long cm_SymLink(cm_scache_t *dscp, clientchar_t *cnamep, fschar_t *contentsp, long flags,
3198 cm_attr_t *attrp, cm_user_t *userp, cm_req_t *reqp)
3206 AFSStoreStatus inStatus;
3207 AFSFetchStatus updatedDirStatus;
3208 AFSFetchStatus newLinkStatus;
3210 struct rx_connection * rxconnp;
3212 fschar_t *fnamep = NULL;
3214 /* Check for RO volume */
3215 if (dscp->flags & CM_SCACHEFLAG_RO)
3216 return CM_ERROR_READONLY;
3218 memset(&volSync, 0, sizeof(volSync));
3220 /* before starting the RPC, mark that we're changing the directory data,
3221 * so that someone who does a chmod on the dir will wait until our
3224 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, CM_DIROP_FLAG_NONE,
3226 lock_ObtainWrite(&dscp->rw);
3227 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3228 lock_ReleaseWrite(&dscp->rw);
3230 cm_EndDirOp(&dirop);
3235 fnamep = cm_ClientStringToFsStringAlloc(cnamep, -1, NULL);
3237 cm_StatusFromAttr(&inStatus, NULL, attrp);
3239 /* try the RPC now */
3240 osi_Log1(afsd_logp, "CALL Symlink scp 0x%p", dscp);
3242 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3246 dirAFSFid.Volume = dscp->fid.volume;
3247 dirAFSFid.Vnode = dscp->fid.vnode;
3248 dirAFSFid.Unique = dscp->fid.unique;
3250 rxconnp = cm_GetRxConn(connp);
3251 code = RXAFS_Symlink(rxconnp, &dirAFSFid, fnamep, contentsp,
3252 &inStatus, &newAFSFid, &newLinkStatus,
3253 &updatedDirStatus, &volSync);
3254 rx_PutConnection(rxconnp);
3256 } while (cm_Analyze(connp, userp, reqp,
3257 &dscp->fid, &volSync, NULL, NULL, code));
3258 code = cm_MapRPCError(code, reqp);
3261 osi_Log1(afsd_logp, "CALL Symlink FAILURE, code 0x%x", code);
3263 osi_Log0(afsd_logp, "CALL Symlink SUCCESS");
3266 lock_ObtainWrite(&dirop.scp->dirlock);
3267 dirop.lockType = CM_DIRLOCK_WRITE;
3269 lock_ObtainWrite(&dscp->rw);
3271 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
3272 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3273 lock_ReleaseWrite(&dscp->rw);
3276 if (cm_CheckDirOpForSingleChange(&dirop)) {
3277 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3279 cm_DirCreateEntry(&dirop, fnamep, &newFid);
3281 cm_BPlusDirCreateEntry(&dirop, cnamep, &newFid);
3285 cm_EndDirOp(&dirop);
3287 /* now try to create the new dir's entry, too, but be careful to
3288 * make sure that we don't merge in old info. Since we weren't locking
3289 * out any requests during the file's creation, we may have pretty old
3293 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3294 code = cm_GetSCache(&newFid, &scp, userp, reqp);
3296 lock_ObtainWrite(&scp->rw);
3297 if (!cm_HaveCallback(scp)) {
3298 cm_MergeStatus(dscp, scp, &newLinkStatus, &volSync,
3301 lock_ReleaseWrite(&scp->rw);
3302 cm_ReleaseSCache(scp);
3308 /* and return error code */
3312 /*! \brief Remove a directory
3314 Encapsulates a call to RXAFS_RemoveDir().
3316 \param[in] dscp cm_scache_t for the directory containing the
3317 directory to be removed.
3319 \param[in] fnamep This will be the original name of the directory
3320 as known to the file server. It will be passed in to RXAFS_RemoveDir().
3321 This parameter is optional. If it is not provided the value
3324 \param[in] cnamep Normalized name used to update the local
3327 \param[in] userp cm_user_t for the request.
3329 \param[in] reqp Request tracker.
3331 long cm_RemoveDir(cm_scache_t *dscp, fschar_t *fnamep, clientchar_t *cnamep, cm_user_t *userp, cm_req_t *reqp)
3337 AFSFetchStatus updatedDirStatus;
3339 struct rx_connection * rxconnp;
3341 cm_scache_t *scp = NULL;
3342 int free_fnamep = FALSE;
3344 memset(&volSync, 0, sizeof(volSync));
3346 if (fnamep == NULL) {
3349 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ,
3350 CM_DIROP_FLAG_NONE, &dirop);
3352 code = cm_BPlusDirLookupOriginalName(&dirop, cnamep, &fnamep);
3355 cm_EndDirOp(&dirop);
3362 code = cm_Lookup(dscp, cnamep, CM_FLAG_NOMOUNTCHASE, userp, reqp, &scp);
3366 /* Check for RO volume */
3367 if (dscp->flags & CM_SCACHEFLAG_RO) {
3368 code = CM_ERROR_READONLY;
3372 /* before starting the RPC, mark that we're changing the directory data,
3373 * so that someone who does a chmod on the dir will wait until our
3376 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, CM_DIROP_FLAG_NONE,
3378 lock_ObtainWrite(&dscp->rw);
3379 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3380 lock_ReleaseWrite(&dscp->rw);
3382 cm_EndDirOp(&dirop);
3387 /* try the RPC now */
3388 osi_Log1(afsd_logp, "CALL RemoveDir scp 0x%p", dscp);
3390 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3394 dirAFSFid.Volume = dscp->fid.volume;
3395 dirAFSFid.Vnode = dscp->fid.vnode;
3396 dirAFSFid.Unique = dscp->fid.unique;
3398 rxconnp = cm_GetRxConn(connp);
3399 code = RXAFS_RemoveDir(rxconnp, &dirAFSFid, fnamep,
3400 &updatedDirStatus, &volSync);
3401 rx_PutConnection(rxconnp);
3403 } while (cm_Analyze(connp, userp, reqp,
3404 &dscp->fid, &volSync, NULL, NULL, code));
3405 code = cm_MapRPCErrorRmdir(code, reqp);
3408 osi_Log1(afsd_logp, "CALL RemoveDir FAILURE, code 0x%x", code);
3410 osi_Log0(afsd_logp, "CALL RemoveDir SUCCESS");
3413 lock_ObtainWrite(&dirop.scp->dirlock);
3414 dirop.lockType = CM_DIRLOCK_WRITE;
3416 lock_ObtainWrite(&dscp->rw);
3418 cm_dnlcRemove(dscp, cnamep);
3419 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
3421 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3422 lock_ReleaseWrite(&dscp->rw);
3425 if (cm_CheckDirOpForSingleChange(&dirop) && cnamep != NULL) {
3426 cm_DirDeleteEntry(&dirop, fnamep);
3428 cm_BPlusDirDeleteEntry(&dirop, cnamep);
3432 cm_EndDirOp(&dirop);
3435 cm_ReleaseSCache(scp);
3437 lock_ObtainWrite(&scp->rw);
3438 scp->flags |= CM_SCACHEFLAG_DELETED;
3439 lock_ObtainWrite(&cm_scacheLock);
3440 cm_AdjustScacheLRU(scp);
3441 cm_RemoveSCacheFromHashTable(scp);
3442 lock_ReleaseWrite(&cm_scacheLock);
3443 lock_ReleaseWrite(&scp->rw);
3451 /* and return error code */
3455 long cm_Open(cm_scache_t *scp, int type, cm_user_t *userp)
3457 /* grab mutex on contents */
3458 lock_ObtainWrite(&scp->rw);
3460 /* reset the prefetch info */
3461 scp->prefetch.base.LowPart = 0; /* base */
3462 scp->prefetch.base.HighPart = 0;
3463 scp->prefetch.end.LowPart = 0; /* and end */
3464 scp->prefetch.end.HighPart = 0;
3466 /* release mutex on contents */
3467 lock_ReleaseWrite(&scp->rw);
3473 /*! \brief Rename a file or directory
3475 Encapsulates a RXAFS_Rename() call.
3477 \param[in] oldDscp cm_scache_t for the directory containing the old
3480 \param[in] oldNamep The original old name known to the file server.
3481 This is the name that will be passed into the RXAFS_Rename().
3482 If it is not provided, it will be looked up.
3484 \param[in] normalizedOldNamep Normalized old name. This is used for
3485 updating local directory caches.
3487 \param[in] newDscp cm_scache_t for the directory containing the new
3490 \param[in] newNamep New name. Normalized.
3492 \param[in] userp cm_user_t for the request.
3494 \param[in,out] reqp Request tracker.
3497 long cm_Rename(cm_scache_t *oldDscp, fschar_t *oldNamep, clientchar_t *cOldNamep,
3498 cm_scache_t *newDscp, clientchar_t *cNewNamep, cm_user_t *userp,
3503 AFSFid oldDirAFSFid;
3504 AFSFid newDirAFSFid;
3506 AFSFetchStatus updatedOldDirStatus;
3507 AFSFetchStatus updatedNewDirStatus;
3510 struct rx_connection * rxconnp;
3511 cm_dirOp_t oldDirOp;
3514 cm_dirOp_t newDirOp;
3515 fschar_t * newNamep = NULL;
3516 int free_oldNamep = FALSE;
3517 cm_scache_t *oldScp = NULL, *newScp = NULL;
3519 memset(&volSync, 0, sizeof(volSync));
3521 if (cOldNamep == NULL || cNewNamep == NULL ||
3522 cm_ClientStrLen(cOldNamep) == 0 ||
3523 cm_ClientStrLen(cNewNamep) == 0)
3524 return CM_ERROR_INVAL;
3527 * Before we permit the operation, make sure that we do not already have
3528 * an object in the destination directory that has a case-insensitive match
3529 * for this name UNLESS the matching object is the object we are renaming.
3531 code = cm_Lookup(oldDscp, cOldNamep, 0, userp, reqp, &oldScp);
3533 osi_Log2(afsd_logp, "cm_Rename oldDscp 0x%p cOldName %S old name lookup failed",
3534 oldDscp, osi_LogSaveStringW(afsd_logp, cOldNamep));
3538 code = cm_Lookup(newDscp, cNewNamep, CM_FLAG_CASEFOLD, userp, reqp, &newScp);
3540 /* found a matching object with the new name */
3541 if (cm_FidCmp(&oldScp->fid, &newScp->fid)) {
3542 /* and they don't match so return an error */
3543 osi_Log2(afsd_logp, "cm_Rename newDscp 0x%p cNewName %S new name already exists",
3544 newDscp, osi_LogSaveStringW(afsd_logp, cNewNamep));
3545 code = CM_ERROR_EXISTS;
3547 cm_ReleaseSCache(newScp);
3549 } else if (code == CM_ERROR_AMBIGUOUS_FILENAME) {
3550 code = CM_ERROR_EXISTS;
3555 /* Check for RO volume */
3557 (oldDscp->flags & CM_SCACHEFLAG_RO) || (newDscp->flags & CM_SCACHEFLAG_RO)) {
3558 code = CM_ERROR_READONLY;
3564 if (oldNamep == NULL) {
3567 code = cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_READ,
3568 CM_DIROP_FLAG_NONE, &oldDirOp);
3570 code = cm_BPlusDirLookupOriginalName(&oldDirOp, cOldNamep, &oldNamep);
3572 free_oldNamep = TRUE;
3573 cm_EndDirOp(&oldDirOp);
3577 osi_Log2(afsd_logp, "cm_Rename oldDscp 0x%p cOldName %S Original Name lookup failed",
3578 oldDscp, osi_LogSaveStringW(afsd_logp, cOldNamep));
3584 /* before starting the RPC, mark that we're changing the directory data,
3585 * so that someone who does a chmod on the dir will wait until our call
3586 * completes. We do this in vnode order so that we don't deadlock,
3587 * which makes the code a little verbose.
3589 if (oldDscp == newDscp) {
3590 /* check for identical names */
3591 if (cm_ClientStrCmp(cOldNamep, cNewNamep) == 0) {
3592 osi_Log2(afsd_logp, "cm_Rename oldDscp 0x%p newDscp 0x%p CM_ERROR_RENAME_IDENTICAL",
3594 code = CM_ERROR_RENAME_IDENTICAL;
3599 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE,
3600 CM_DIROP_FLAG_NONE, &oldDirOp);
3601 lock_ObtainWrite(&oldDscp->rw);
3602 cm_dnlcRemove(oldDscp, cOldNamep);
3603 cm_dnlcRemove(oldDscp, cNewNamep);
3604 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3605 CM_SCACHESYNC_STOREDATA);
3606 lock_ReleaseWrite(&oldDscp->rw);
3608 cm_EndDirOp(&oldDirOp);
3612 /* two distinct dir vnodes */
3614 if (oldDscp->fid.cell != newDscp->fid.cell ||
3615 oldDscp->fid.volume != newDscp->fid.volume) {
3616 osi_Log2(afsd_logp, "cm_Rename oldDscp 0x%p newDscp 0x%p CM_ERROR_CROSSDEVLINK",
3618 code = CM_ERROR_CROSSDEVLINK;
3622 /* shouldn't happen that we have distinct vnodes for two
3623 * different files, but could due to deliberate attack, or
3624 * stale info. Avoid deadlocks and quit now.
3626 if (oldDscp->fid.vnode == newDscp->fid.vnode) {
3627 osi_Log2(afsd_logp, "cm_Rename oldDscp 0x%p newDscp 0x%p vnode collision",
3629 code = CM_ERROR_CROSSDEVLINK;
3633 if (oldDscp->fid.vnode < newDscp->fid.vnode) {
3634 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE,
3635 CM_DIROP_FLAG_NONE, &oldDirOp);
3636 lock_ObtainWrite(&oldDscp->rw);
3637 cm_dnlcRemove(oldDscp, cOldNamep);
3638 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3639 CM_SCACHESYNC_STOREDATA);
3640 lock_ReleaseWrite(&oldDscp->rw);
3642 cm_EndDirOp(&oldDirOp);
3644 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE,
3645 CM_DIROP_FLAG_NONE, &newDirOp);
3646 lock_ObtainWrite(&newDscp->rw);
3647 cm_dnlcRemove(newDscp, cNewNamep);
3648 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3649 CM_SCACHESYNC_STOREDATA);
3650 lock_ReleaseWrite(&newDscp->rw);
3652 cm_EndDirOp(&newDirOp);
3654 /* cleanup first one */
3655 lock_ObtainWrite(&oldDscp->rw);
3656 cm_SyncOpDone(oldDscp, NULL,
3657 CM_SCACHESYNC_STOREDATA);
3658 lock_ReleaseWrite(&oldDscp->rw);
3659 cm_EndDirOp(&oldDirOp);
3664 /* lock the new vnode entry first */
3665 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE,
3666 CM_DIROP_FLAG_NONE, &newDirOp);
3667 lock_ObtainWrite(&newDscp->rw);
3668 cm_dnlcRemove(newDscp, cNewNamep);
3669 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3670 CM_SCACHESYNC_STOREDATA);
3671 lock_ReleaseWrite(&newDscp->rw);
3673 cm_EndDirOp(&newDirOp);
3675 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE,
3676 CM_DIROP_FLAG_NONE, &oldDirOp);
3677 lock_ObtainWrite(&oldDscp->rw);
3678 cm_dnlcRemove(oldDscp, cOldNamep);
3679 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3680 CM_SCACHESYNC_STOREDATA);
3681 lock_ReleaseWrite(&oldDscp->rw);
3683 cm_EndDirOp(&oldDirOp);
3685 /* cleanup first one */
3686 lock_ObtainWrite(&newDscp->rw);
3687 cm_SyncOpDone(newDscp, NULL,
3688 CM_SCACHESYNC_STOREDATA);
3689 lock_ReleaseWrite(&newDscp->rw);
3690 cm_EndDirOp(&newDirOp);
3694 } /* two distinct vnodes */
3701 newNamep = cm_ClientStringToFsStringAlloc(cNewNamep, -1, NULL);
3703 /* try the RPC now */
3704 osi_Log2(afsd_logp, "CALL Rename old scp 0x%p new scp 0x%p",
3707 code = cm_ConnFromFID(&oldDscp->fid, userp, reqp, &connp);
3711 oldDirAFSFid.Volume = oldDscp->fid.volume;
3712 oldDirAFSFid.Vnode = oldDscp->fid.vnode;
3713 oldDirAFSFid.Unique = oldDscp->fid.unique;
3714 newDirAFSFid.Volume = newDscp->fid.volume;
3715 newDirAFSFid.Vnode = newDscp->fid.vnode;
3716 newDirAFSFid.Unique = newDscp->fid.unique;
3718 rxconnp = cm_GetRxConn(connp);
3719 code = RXAFS_Rename(rxconnp, &oldDirAFSFid, oldNamep,
3720 &newDirAFSFid, newNamep,
3721 &updatedOldDirStatus, &updatedNewDirStatus,
3723 rx_PutConnection(rxconnp);
3725 } while (cm_Analyze(connp, userp, reqp, &oldDscp->fid,
3726 &volSync, NULL, NULL, code));
3727 code = cm_MapRPCError(code, reqp);
3730 osi_Log1(afsd_logp, "CALL Rename FAILURE, code 0x%x", code);
3732 osi_Log0(afsd_logp, "CALL Rename SUCCESS");
3734 /* update the individual stat cache entries for the directories */
3736 lock_ObtainWrite(&oldDirOp.scp->dirlock);
3737 oldDirOp.lockType = CM_DIRLOCK_WRITE;
3739 lock_ObtainWrite(&oldDscp->rw);
3742 cm_MergeStatus(NULL, oldDscp, &updatedOldDirStatus, &volSync,
3743 userp, reqp, CM_MERGEFLAG_DIROP);
3744 cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_STOREDATA);
3745 lock_ReleaseWrite(&oldDscp->rw);
3747 if (code == 0 && cm_CheckDirOpForSingleChange(&oldDirOp)) {
3749 diropCode = cm_BPlusDirLookup(&oldDirOp, cOldNamep, &fileFid);
3750 if (diropCode == CM_ERROR_INEXACT_MATCH)
3752 else if (diropCode == EINVAL)
3754 diropCode = cm_DirLookup(&oldDirOp, oldNamep, &fileFid);
3756 if (diropCode == 0) {
3758 diropCode = cm_DirCreateEntry(&oldDirOp, newNamep, &fileFid);
3760 cm_BPlusDirCreateEntry(&oldDirOp, cNewNamep, &fileFid);
3764 if (diropCode == 0) {
3765 diropCode = cm_DirDeleteEntry(&oldDirOp, oldNamep);
3767 cm_BPlusDirDeleteEntry(&oldDirOp, cOldNamep);
3772 cm_EndDirOp(&oldDirOp);
3774 /* and update it for the new one, too, if necessary */
3777 lock_ObtainWrite(&newDirOp.scp->dirlock);
3778 newDirOp.lockType = CM_DIRLOCK_WRITE;
3780 lock_ObtainWrite(&newDscp->rw);
3782 cm_MergeStatus(NULL, newDscp, &updatedNewDirStatus, &volSync,
3783 userp, reqp, CM_MERGEFLAG_DIROP);
3784 cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_STOREDATA);
3785 lock_ReleaseWrite(&newDscp->rw);
3789 * The following optimization does not work.
3790 * When the file server processed a RXAFS_Rename() request the
3791 * FID of the object being moved between directories is not
3792 * preserved. The client does not know the new FID nor the
3793 * version number of the target. Not only can we not create
3794 * the directory entry in the new directory, but we can't
3795 * preserve the cached data for the file. It must be re-read
3796 * from the file server. - jaltman, 2009/02/20
3799 /* we only make the local change if we successfully made
3800 the change in the old directory AND there was only one
3801 change in the new directory */
3802 if (diropCode == 0 && cm_CheckDirOpForSingleChange(&newDirOp)) {
3803 cm_DirCreateEntry(&newDirOp, newNamep, &fileFid);
3805 cm_BPlusDirCreateEntry(&newDirOp, cNewNamep, &fileFid);
3810 cm_EndDirOp(&newDirOp);
3814 * After the rename the file server has invalidated the callbacks
3815 * on the file that was moved nor do we have a directory reference
3818 lock_ObtainWrite(&oldScp->rw);
3819 cm_DiscardSCache(oldScp);
3820 lock_ReleaseWrite(&oldScp->rw);
3824 cm_ReleaseSCache(oldScp);
3831 /* and return error code */
3835 /* Byte range locks:
3837 The OpenAFS Windows client has to fake byte range locks given no
3838 server side support for such locks. This is implemented as keyed
3839 byte range locks on the cache manager.
3841 Keyed byte range locks:
3843 Each cm_scache_t structure keeps track of a list of keyed locks.
3844 The key for a lock identifies an owner of a set of locks (referred
3845 to as a client). Each key is represented by a value. The set of
3846 key values used within a specific cm_scache_t structure form a
3847 namespace that has a scope of just that cm_scache_t structure. The
3848 same key value can be used with another cm_scache_t structure and
3849 correspond to a completely different client. However it is
3850 advantageous for the SMB or IFS layer to make sure that there is a
3851 1-1 mapping between client and keys over all cm_scache_t objects.
3853 Assume a client C has key Key(C) (although, since the scope of the
3854 key is a cm_scache_t, the key can be Key(C,S), where S is the
3855 cm_scache_t. But assume a 1-1 relation between keys and clients).
3856 A byte range (O,+L) denotes byte addresses (O) through (O+L-1)
3857 inclusive (a.k.a. [O,O+L-1]). The function Key(x) is implemented
3858 through cm_generateKey() function for both SMB and IFS.
3860 The list of locks for a cm_scache_t object S is maintained in
3861 S->fileLocks. The cache manager will set a lock on the AFS file
3862 server in order to assert the locks in S->fileLocks. If only
3863 shared locks are in place for S, then the cache manager will obtain
3864 a LockRead lock, while if there are any exclusive locks, it will
3865 obtain a LockWrite lock. If the exclusive locks are all released
3866 while the shared locks remain, then the cache manager will
3867 downgrade the lock from LockWrite to LockRead. Similarly, if an
3868 exclusive lock is obtained when only shared locks exist, then the
3869 cache manager will try to upgrade the lock from LockRead to
3872 Each lock L owned by client C maintains a key L->key such that
3873 L->key == Key(C), the effective range defined by L->LOffset and
3874 L->LLength such that the range of bytes affected by the lock is
3875 (L->LOffset, +L->LLength), a type maintained in L->LockType which
3876 is either exclusive or shared.
3880 A lock exists iff it is in S->fileLocks for some cm_scache_t
3881 S. Existing locks are in one of the following states: ACTIVE,
3882 WAITLOCK, WAITUNLOCK, LOST, DELETED.
3884 The following sections describe each lock and the associated
3887 1. ACTIVE: A lock L is ACTIVE iff the cache manager has asserted
3888 the lock with the AFS file server. This type of lock can be
3889 exercised by a client to read or write to the locked region (as
3892 1.1 ACTIVE->LOST: When the AFS file server fails to extend a
3893 server lock that was required to assert the lock. Before
3894 marking the lock as lost, the cache manager checks if the file
3895 has changed on the server. If the file has not changed, then
3896 the cache manager will attempt to obtain a new server lock
3897 that is sufficient to assert the client side locks for the
3898 file. If any of these fail, the lock is marked as LOST.
3899 Otherwise, it is left as ACTIVE.
3901 1.2 ACTIVE->DELETED: Lock is released.
3903 2. WAITLOCK: A lock is in a WAITLOCK state if the cache manager
3904 grants the lock but the lock is yet to be asserted with the AFS
3905 file server. Once the file server grants the lock, the state
3906 will transition to an ACTIVE lock.
3908 2.1 WAITLOCK->ACTIVE: The server granted the lock.
3910 2.2 WAITLOCK->DELETED: Lock is abandoned, or timed out during
3913 2.3 WAITLOCK->LOST: One or more locks from this client were
3914 marked as LOST. No further locks will be granted to this
3915 client until all lost locks are removed.
3917 3. WAITUNLOCK: A lock is in a WAITUNLOCK state if the cache manager
3918 receives a request for a lock that conflicts with an existing
3919 ACTIVE or WAITLOCK lock. The lock will be placed in the queue
3920 and will be granted at such time the conflicting locks are
3921 removed, at which point the state will transition to either
3924 3.1 WAITUNLOCK->ACTIVE: The conflicting lock was removed. The
3925 current serverLock is sufficient to assert this lock, or a
3926 sufficient serverLock is obtained.
3928 3.2 WAITUNLOCK->WAITLOCK: The conflicting lock was removed,
3929 however the required serverLock is yet to be asserted with the
3932 3.3 WAITUNLOCK->DELETED: The lock is abandoned, timed out or
3935 3.5 WAITUNLOCK->LOST: One or more locks from this client were
3936 marked as LOST. No further locks will be granted to this
3937 client until all lost locks are removed.
3939 4. LOST: A lock L is LOST if the server lock that was required to
3940 assert the lock could not be obtained or if it could not be
3941 extended, or if other locks by the same client were LOST.
3942 Essentially, once a lock is LOST, the contract between the cache
3943 manager and that specific client is no longer valid.
3945 The cache manager rechecks the server lock once every minute and
3946 extends it as appropriate. If this is not done for 5 minutes,
3947 the AFS file server will release the lock (the 5 minute timeout
3948 is based on current file server code and is fairly arbitrary).
3949 Once released, the lock cannot be re-obtained without verifying
3950 that the contents of the file hasn't been modified since the
3951 time the lock was released. Re-obtaining the lock without
3952 verifying this may lead to data corruption. If the lock can not
3953 be obtained safely, then all active locks for the cm_scache_t
3956 4.1 LOST->DELETED: The lock is released.
3958 5. DELETED: The lock is no longer relevant. Eventually, it will
3959 get removed from the cm_scache_t. In the meantime, it will be
3960 treated as if it does not exist.
3962 5.1 DELETED->not exist: The lock is removed from the
3965 The following are classifications of locks based on their state.
3967 6* A lock L is ACCEPTED if it is ACTIVE or WAITLOCK. These locks
3968 have been accepted by the cache manager, but may or may not have
3969 been granted back to the client.
3971 7* A lock L is QUEUED if it is ACTIVE, WAITLOCK or WAITUNLOCK.
3973 8* A lock L is WAITING if it is WAITLOCK or WAITUNLOCK.
3977 A client C can READ range (Offset,+Length) of a file represented by
3978 cm_scache_t S iff (1):
3980 1. for all _a_ in (Offset,+Length), all of the following is true:
3982 1.1 For each ACTIVE lock L in S->fileLocks such that _a_ in
3983 (L->LOffset,+L->LLength); L->key == Key(C) OR L->LockType is
3986 1.2 For each LOST lock L in S->fileLocks such that _a_ in
3987 (L->LOffset,+L->LLength); L->LockType is shared AND L->key !=
3990 (When locks are lost on an cm_scache_t, all locks are lost. By
3991 4.2 (below), if there is an exclusive LOST lock, then there
3992 can't be any overlapping ACTIVE locks.)
3994 A client C can WRITE range (Offset,+Length) of cm_scache_t S iff (2):
3996 2. for all _a_ in (Offset,+Length), one of the following is true:
3998 2.1 Byte _a_ of S is unowned (as specified in 1.1) AND there
3999 does not exist a LOST lock L such that _a_ in
4000 (L->LOffset,+L->LLength).
4002 2.2 Byte _a_ of S is owned by C under lock L (as specified in
4003 1.2) AND L->LockType is exclusive.
4005 A client C can OBTAIN a lock L on cm_scache_t S iff (both 3 and 4):
4007 3. for all _a_ in (L->LOffset,+L->LLength), ALL of the following is
4010 3.1 If L->LockType is exclusive then there does NOT exist a
4011 ACCEPTED lock M in S->fileLocks such that _a_ in
4012 (M->LOffset,+M->LLength).
4014 (If we count all QUEUED locks then we hit cases such as
4015 cascading waiting locks where the locks later on in the queue
4016 can be granted without compromising file integrity. On the
4017 other hand if only ACCEPTED locks are considered, then locks
4018 that were received earlier may end up waiting for locks that
4019 were received later to be unlocked. The choice of ACCEPTED
4020 locks was made to mimic the Windows byte range lock
4023 3.2 If L->LockType is shared then for each ACCEPTED lock M in
4024 S->fileLocks, if _a_ in (M->LOffset,+M->LLength) then
4025 M->LockType is shared.
4027 4. For all LOST locks M in S->fileLocks, ALL of the following are true:
4029 4.1 M->key != Key(C)
4031 4.2 If M->LockType is exclusive, then (L->LOffset,+L->LLength)
4032 and (M->LOffset,+M->LLength) do not intersect.
4034 (Note: If a client loses a lock, it loses all locks.
4035 Subsequently, it will not be allowed to obtain any more locks
4036 until all existing LOST locks that belong to the client are
4037 released. Once all locks are released by a single client,
4038 there exists no further contract between the client and AFS
4039 about the contents of the file, hence the client can then
4040 proceed to obtain new locks and establish a new contract.
4042 This doesn't quite work as you think it should, because most
4043 applications aren't built to deal with losing locks they
4044 thought they once had. For now, we don't have a good
4045 solution to lost locks.
4047 Also, for consistency reasons, we have to hold off on
4048 granting locks that overlap exclusive LOST locks.)
4050 A client C can only unlock locks L in S->fileLocks which have
4053 The representation and invariants are as follows:
4055 - Each cm_scache_t structure keeps:
4057 - A queue of byte-range locks (cm_scache_t::fileLocks) which
4058 are of type cm_file_lock_t.
4060 - A record of the highest server-side lock that has been
4061 obtained for this object (cm_scache_t::serverLock), which is
4062 one of (-1), LockRead, LockWrite.
4064 - A count of ACCEPTED exclusive and shared locks that are in the
4065 queue (cm_scache_t::sharedLocks and
4066 cm_scache_t::exclusiveLocks)
4068 - Each cm_file_lock_t structure keeps:
4070 - The type of lock (cm_file_lock_t::LockType)
4072 - The key associated with the lock (cm_file_lock_t::key)
4074 - The offset and length of the lock (cm_file_lock_t::LOffset
4075 and cm_file_lock_t::LLength)
4077 - The state of the lock.
4079 - Time of issuance or last successful extension
4081 Semantic invariants:
4083 I1. The number of ACCEPTED locks in S->fileLocks are
4084 (S->sharedLocks + S->exclusiveLocks)
4086 External invariants:
4088 I3. S->serverLock is the lock that we have asserted with the
4089 AFS file server for this cm_scache_t.
4091 I4. S->serverLock == LockRead iff there is at least one ACTIVE
4092 shared lock, but no ACTIVE exclusive locks.
4094 I5. S->serverLock == LockWrite iff there is at least one ACTIVE
4097 I6. If L is a LOST lock, then for each lock M in S->fileLocks,
4098 M->key == L->key IMPLIES M is LOST or DELETED.
4103 #define IS_LOCK_ACTIVE(lockp) (((lockp)->flags & (CM_FILELOCK_FLAG_DELETED|CM_FILELOCK_FLAG_WAITLOCK|CM_FILELOCK_FLAG_WAITUNLOCK|CM_FILELOCK_FLAG_LOST)) == 0)
4105 #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)
4107 #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)
4109 #define IS_LOCK_LOST(lockp) (((lockp)->flags & (CM_FILELOCK_FLAG_DELETED|CM_FILELOCK_FLAG_LOST)) == CM_FILELOCK_FLAG_LOST)
4111 #define IS_LOCK_DELETED(lockp) (((lockp)->flags & CM_FILELOCK_FLAG_DELETED) == CM_FILELOCK_FLAG_DELETED)
4114 #define IS_LOCK_ACCEPTED(lockp) (IS_LOCK_ACTIVE(lockp) || IS_LOCK_WAITLOCK(lockp))
4117 #define IS_LOCK_CLIENTONLY(lockp) ((((lockp)->scp->flags & CM_SCACHEFLAG_RO) == CM_SCACHEFLAG_RO) || (((lockp)->flags & CM_FILELOCK_FLAG_CLIENTONLY) == CM_FILELOCK_FLAG_CLIENTONLY))
4120 #define INTERSECT_RANGE(r1,r2) (((r2).offset+(r2).length) > (r1).offset && ((r1).offset +(r1).length) > (r2).offset)
4123 #define CONTAINS_RANGE(r1,r2) (((r2).offset+(r2).length) <= ((r1).offset+(r1).length) && (r1).offset <= (r2).offset)
4125 #if defined(VICED_CAPABILITY_USE_BYTE_RANGE_LOCKS) && !defined(LOCK_TESTING)
4126 #define SCP_SUPPORTS_BRLOCKS(scp) ((scp)->cbServerp && ((scp)->cbServerp->capabilities & VICED_CAPABILITY_USE_BYTE_RANGE_LOCKS))
4128 #define SCP_SUPPORTS_BRLOCKS(scp) (1)
4131 #define SERVERLOCKS_ENABLED(scp) (!((scp)->flags & CM_SCACHEFLAG_RO) && cm_enableServerLocks && SCP_SUPPORTS_BRLOCKS(scp))
4133 #if defined(VICED_CAPABILITY_WRITELOCKACL)
4134 #define SCP_SUPPORTS_WRITELOCKACL(scp) ((scp)->cbServerp && ((scp->cbServerp->capabilities & VICED_CAPABILITY_WRITELOCKACL)))
4136 #define SCP_SUPPORTS_WRITELOCKACL(scp) (0)
4138 /* This should really be defined in any build that this code is being
4140 #error VICED_CAPABILITY_WRITELOCKACL not defined.
4143 static void cm_LockRangeSubtract(cm_range_t * pos, const cm_range_t * neg)
4145 afs_int64 int_begin;
4148 int_begin = MAX(pos->offset, neg->offset);
4149 int_end = MIN(pos->offset+pos->length, neg->offset+neg->length);
4151 if (int_begin < int_end) {
4152 if (int_begin == pos->offset) {
4153 pos->length = pos->offset + pos->length - int_end;
4154 pos->offset = int_end;
4155 } else if (int_end == pos->offset + pos->length) {
4156 pos->length = int_begin - pos->offset;
4159 /* We only subtract ranges if the resulting range is
4160 contiguous. If we try to support non-contigous ranges, we
4161 aren't actually improving performance. */
4165 /* Called with scp->rw held. Returns 0 if all is clear to read the
4166 specified range by the client identified by key.
4168 long cm_LockCheckRead(cm_scache_t *scp,
4169 LARGE_INTEGER LOffset,
4170 LARGE_INTEGER LLength,
4173 #ifndef ADVISORY_LOCKS
4175 cm_file_lock_t *fileLock;
4179 int substract_ranges = FALSE;
4181 range.offset = LOffset.QuadPart;
4182 range.length = LLength.QuadPart;
4186 1. for all _a_ in (Offset,+Length), all of the following is true:
4188 1.1 For each ACTIVE lock L in S->fileLocks such that _a_ in
4189 (L->LOffset,+L->LLength); L->key == Key(C) OR L->LockType is
4192 1.2 For each LOST lock L in S->fileLocks such that _a_ in
4193 (L->LOffset,+L->LLength); L->LockType is shared AND L->key !=
4198 lock_ObtainRead(&cm_scacheLock);
4200 for (q = scp->fileLocksH; q && range.length > 0; q = osi_QNext(q)) {
4202 (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
4204 if (INTERSECT_RANGE(range, fileLock->range)) {
4205 if (IS_LOCK_ACTIVE(fileLock)) {
4206 if (cm_KeyEquals(&fileLock->key, &key, 0)) {
4208 /* If there is an active lock for this client, it
4209 is safe to substract ranges.*/
4210 cm_LockRangeSubtract(&range, &fileLock->range);
4211 substract_ranges = TRUE;
4213 if (fileLock->lockType != LockRead) {
4214 code = CM_ERROR_LOCK_CONFLICT;
4218 /* even if the entire range is locked for reading,
4219 we still can't grant the lock at this point
4220 because the client may have lost locks. That
4221 is, unless we have already seen an active lock
4222 belonging to the client, in which case there
4223 can't be any lost locks for this client. */
4224 if (substract_ranges)
4225 cm_LockRangeSubtract(&range, &fileLock->range);
4227 } else if (IS_LOCK_LOST(fileLock) &&
4228 (cm_KeyEquals(&fileLock->key, &key, 0) || fileLock->lockType == LockWrite)) {
4229 code = CM_ERROR_BADFD;
4235 lock_ReleaseRead(&cm_scacheLock);
4237 osi_Log4(afsd_logp, "cm_LockCheckRead scp 0x%x offset %d length %d code 0x%x",
4238 scp, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart, code);
4249 /* Called with scp->rw held. Returns 0 if all is clear to write the
4250 specified range by the client identified by key.
4252 long cm_LockCheckWrite(cm_scache_t *scp,
4253 LARGE_INTEGER LOffset,
4254 LARGE_INTEGER LLength,
4257 #ifndef ADVISORY_LOCKS
4259 cm_file_lock_t *fileLock;
4264 range.offset = LOffset.QuadPart;
4265 range.length = LLength.QuadPart;
4268 A client C can WRITE range (Offset,+Length) of cm_scache_t S iff (2):
4270 2. for all _a_ in (Offset,+Length), one of the following is true:
4272 2.1 Byte _a_ of S is unowned AND there does not exist a LOST
4273 lock L such that _a_ in (L->LOffset,+L->LLength).
4275 2.2 Byte _a_ of S is owned by C under lock L AND L->LockType is
4279 lock_ObtainRead(&cm_scacheLock);
4281 for (q = scp->fileLocksH; q && range.length > 0; q = osi_QNext(q)) {
4283 (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
4285 if (INTERSECT_RANGE(range, fileLock->range)) {
4286 if (IS_LOCK_ACTIVE(fileLock)) {
4287 if (cm_KeyEquals(&fileLock->key, &key, 0)) {
4288 if (fileLock->lockType == LockWrite) {
4290 /* if there is an active lock for this client, it
4291 is safe to substract ranges */
4292 cm_LockRangeSubtract(&range, &fileLock->range);
4294 code = CM_ERROR_LOCK_CONFLICT;
4298 code = CM_ERROR_LOCK_CONFLICT;
4301 } else if (IS_LOCK_LOST(fileLock)) {
4302 code = CM_ERROR_BADFD;
4308 lock_ReleaseRead(&cm_scacheLock);
4310 osi_Log4(afsd_logp, "cm_LockCheckWrite scp 0x%x offset %d length %d code 0x%x",
4311 scp, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart, code);
4322 /* Called with cm_scacheLock write locked */
4323 static cm_file_lock_t * cm_GetFileLock(void) {
4326 l = (cm_file_lock_t *) cm_freeFileLocks;
4328 osi_QRemove(&cm_freeFileLocks, &l->q);
4330 l = malloc(sizeof(cm_file_lock_t));
4331 osi_assertx(l, "null cm_file_lock_t");
4334 memset(l, 0, sizeof(cm_file_lock_t));
4339 /* Called with cm_scacheLock write locked */
4340 static void cm_PutFileLock(cm_file_lock_t *l) {
4341 osi_QAdd(&cm_freeFileLocks, &l->q);
4344 /* called with scp->rw held. May release it during processing, but
4345 leaves it held on exit. */
4346 long cm_IntSetLock(cm_scache_t * scp, cm_user_t * userp, int lockType,
4352 struct rx_connection * rxconnp;
4354 afs_uint32 reqflags = reqp->flags;
4356 memset(&volSync, 0, sizeof(volSync));
4358 tfid.Volume = scp->fid.volume;
4359 tfid.Vnode = scp->fid.vnode;
4360 tfid.Unique = scp->fid.unique;
4363 osi_Log2(afsd_logp, "CALL SetLock scp 0x%p for lock %d", scp, lockType);
4365 reqp->flags |= CM_REQ_NORETRY;
4366 lock_ReleaseWrite(&scp->rw);
4369 code = cm_ConnFromFID(&cfid, userp, reqp, &connp);
4373 rxconnp = cm_GetRxConn(connp);
4374 code = RXAFS_SetLock(rxconnp, &tfid, lockType,
4376 rx_PutConnection(rxconnp);
4378 } while (cm_Analyze(connp, userp, reqp, &cfid, &volSync,
4381 code = cm_MapRPCError(code, reqp);
4383 osi_Log1(afsd_logp, "CALL SetLock FAILURE, code 0x%x", code);
4385 osi_Log0(afsd_logp, "CALL SetLock SUCCESS");
4388 reqp->flags = reqflags;
4390 lock_ObtainWrite(&scp->rw);
4393 * The file server does not return a status structure so we must
4394 * locally track the file server lock count to the best of our
4397 if (lockType == LockWrite)
4398 scp->fsLockCount = -1;
4405 /* called with scp->rw held. Releases it during processing */
4406 long cm_IntReleaseLock(cm_scache_t * scp, cm_user_t * userp,
4412 struct rx_connection * rxconnp;
4415 if (scp->flags & CM_SCACHEFLAG_DELETED) {
4416 osi_Log1(afsd_logp, "CALL ReleaseLock on Deleted Vnode scp 0x%p", scp);
4420 memset(&volSync, 0, sizeof(volSync));
4422 tfid.Volume = scp->fid.volume;
4423 tfid.Vnode = scp->fid.vnode;
4424 tfid.Unique = scp->fid.unique;
4427 lock_ReleaseWrite(&scp->rw);
4429 osi_Log1(afsd_logp, "CALL ReleaseLock scp 0x%p", scp);
4432 code = cm_ConnFromFID(&cfid, userp, reqp, &connp);
4436 rxconnp = cm_GetRxConn(connp);
4437 code = RXAFS_ReleaseLock(rxconnp, &tfid, &volSync);
4438 rx_PutConnection(rxconnp);
4440 } while (cm_Analyze(connp, userp, reqp, &cfid, &volSync,
4442 code = cm_MapRPCError(code, reqp);
4445 "CALL ReleaseLock FAILURE, code 0x%x", code);
4448 "CALL ReleaseLock SUCCESS");
4450 lock_ObtainWrite(&scp->rw);
4453 * The file server does not return a status structure so we must
4454 * locally track the file server lock count to the best of our
4458 if (scp->fsLockCount < 0)
4459 scp->fsLockCount = 0;
4462 return (code != CM_ERROR_BADFD ? code : 0);
4465 /* called with scp->rw held. May release it during processing, but
4466 will exit with lock held.
4470 - 0 if the user has permission to get the specified lock for the scp
4472 - CM_ERROR_NOACCESS if not
4474 Any other error from cm_SyncOp will be sent down untranslated.
4476 If CM_ERROR_NOACCESS is returned and lock_type is LockRead, then
4477 phas_insert (if non-NULL) will receive a boolean value indicating
4478 whether the user has INSERT permission or not.
4480 long cm_LockCheckPerms(cm_scache_t * scp,
4487 long code = 0, code2 = 0;
4489 /* lock permissions are slightly tricky because of the 'i' bit.
4490 If the user has PRSFS_LOCK, she can read-lock the file. If the
4491 user has PRSFS_WRITE, she can write-lock the file. However, if
4492 the user has PRSFS_INSERT, then she can write-lock new files,
4493 but not old ones. Since we don't have information about
4494 whether a file is new or not, we assume that if the user owns
4495 the scp, then she has the permissions that are granted by
4498 osi_Log3(afsd_logp, "cm_LockCheckPerms for scp[0x%p] type[%d] user[0x%p]",
4499 scp, lock_type, userp);
4501 if (lock_type == LockRead)
4502 rights |= PRSFS_LOCK;
4503 else if (lock_type == LockWrite)
4504 rights |= PRSFS_WRITE | PRSFS_LOCK;
4507 osi_assertx(FALSE, "invalid lock type");
4512 *phas_insert = FALSE;
4514 code = cm_SyncOp(scp, NULL, userp, reqp, rights,
4515 CM_SCACHESYNC_GETSTATUS |
4516 CM_SCACHESYNC_NEEDCALLBACK);
4518 if (phas_insert && scp->creator == userp) {
4520 /* If this file was created by the user, then we check for
4521 PRSFS_INSERT. If the file server is recent enough, then
4522 this should be sufficient for her to get a write-lock (but
4523 not necessarily a read-lock). VICED_CAPABILITY_WRITELOCKACL
4524 indicates whether a file server supports getting write
4525 locks when the user only has PRSFS_INSERT.
4527 If the file was not created by the user we skip the check
4528 because the INSERT bit will not apply to this user even
4532 code2 = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
4533 CM_SCACHESYNC_GETSTATUS |
4534 CM_SCACHESYNC_NEEDCALLBACK);
4536 if (code2 == CM_ERROR_NOACCESS) {
4537 osi_Log0(afsd_logp, "cm_LockCheckPerms user has no INSERT bits");
4539 *phas_insert = TRUE;
4540 osi_Log0(afsd_logp, "cm_LockCheckPerms user has INSERT bits");
4544 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
4546 osi_Log1(afsd_logp, "cm_LockCheckPerms returning code %d", code);
4551 /* called with scp->rw held */
4552 long cm_Lock(cm_scache_t *scp, unsigned char sLockType,
4553 LARGE_INTEGER LOffset, LARGE_INTEGER LLength,
4555 int allowWait, cm_user_t *userp, cm_req_t *reqp,
4556 cm_file_lock_t **lockpp)
4559 int Which = ((sLockType & LOCKING_ANDX_SHARED_LOCK) ? LockRead : LockWrite);
4560 cm_file_lock_t *fileLock;
4563 int wait_unlock = FALSE;
4564 int force_client_lock = FALSE;
4566 osi_Log4(afsd_logp, "cm_Lock scp 0x%x type 0x%x offset %d length %d",
4567 scp, sLockType, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart);
4568 osi_Log4(afsd_logp, "... allowWait %d key <0x%x, 0x%x, 0x%x>", allowWait,
4569 key.process_id, key.session_id, key.file_id);
4572 A client C can OBTAIN a lock L on cm_scache_t S iff (both 3 and 4):
4574 3. for all _a_ in (L->LOffset,+L->LLength), ALL of the following is
4577 3.1 If L->LockType is exclusive then there does NOT exist a
4578 ACCEPTED lock M in S->fileLocks such that _a_ in
4579 (M->LOffset,+M->LLength).
4581 3.2 If L->LockType is shared then for each ACCEPTED lock M in
4582 S->fileLocks, if _a_ in (M->LOffset,+M->LLength) then
4583 M->LockType is shared.
4585 4. For all LOST locks M in S->fileLocks, ALL of the following are true:
4587 4.1 M->key != Key(C)
4589 4.2 If M->LockType is exclusive, then (L->LOffset,+L->LLength)
4590 and (M->LOffset,+M->LLength) do not intersect.
4593 range.offset = LOffset.QuadPart;
4594 range.length = LLength.QuadPart;
4596 lock_ObtainRead(&cm_scacheLock);
4598 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
4600 (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
4602 if (IS_LOCK_LOST(fileLock)) {
4603 if (cm_KeyEquals(&fileLock->key, &key, 0)) {
4604 code = CM_ERROR_BADFD;
4606 } else if (fileLock->lockType == LockWrite && INTERSECT_RANGE(range, fileLock->range)) {
4607 code = CM_ERROR_WOULDBLOCK;
4613 /* we don't need to check for deleted locks here since deleted
4614 locks are dequeued from scp->fileLocks */
4615 if (IS_LOCK_ACCEPTED(fileLock) &&
4616 INTERSECT_RANGE(range, fileLock->range)) {
4618 if ((sLockType & LOCKING_ANDX_SHARED_LOCK) == 0 ||
4619 fileLock->lockType != LockRead) {
4621 code = CM_ERROR_WOULDBLOCK;
4627 lock_ReleaseRead(&cm_scacheLock);
4629 if (code == 0 && SERVERLOCKS_ENABLED(scp)) {
4630 if (Which == scp->serverLock ||
4631 (Which == LockRead && scp->serverLock == LockWrite)) {
4635 /* we already have the lock we need */
4636 osi_Log3(afsd_logp, " we already have the correct lock. exclusives[%d], shared[%d], serverLock[%d]",
4637 scp->exclusiveLocks, scp->sharedLocks, (int)(signed char) scp->serverLock);
4639 code = cm_LockCheckPerms(scp, Which, userp, reqp, &has_insert);
4641 /* special case: if we don't have permission to read-lock
4642 the file, then we force a clientside lock. This is to
4643 compensate for applications that obtain a read-lock for
4644 reading files off of directories that don't grant
4645 read-locks to the user. */
4646 if (code == CM_ERROR_NOACCESS && Which == LockRead) {
4648 if (has_insert && SCP_SUPPORTS_WRITELOCKACL(scp)) {
4649 osi_Log0(afsd_logp, " User has no read-lock perms, but has INSERT perms.");
4652 osi_Log0(afsd_logp, " User has no read-lock perms. Forcing client-side lock");
4653 force_client_lock = TRUE;
4657 } else if ((scp->exclusiveLocks > 0) ||
4658 (scp->sharedLocks > 0 && scp->serverLock != LockRead)) {
4661 /* We are already waiting for some other lock. We should
4662 wait for the daemon to catch up instead of generating a
4663 flood of SetLock calls. */
4664 osi_Log3(afsd_logp, " already waiting for other lock. exclusives[%d], shared[%d], serverLock[%d]",
4665 scp->exclusiveLocks, scp->sharedLocks, (int)(signed char) scp->serverLock);
4667 /* see if we have permission to create the lock in the
4669 code = cm_LockCheckPerms(scp, Which, userp, reqp, &has_insert);
4671 code = CM_ERROR_WOULDBLOCK;
4672 else if (code == CM_ERROR_NOACCESS && Which == LockRead) {
4674 if (has_insert && SCP_SUPPORTS_WRITELOCKACL(scp)) {
4676 " User has no read-lock perms, but has INSERT perms.");
4677 code = CM_ERROR_WOULDBLOCK;
4680 " User has no read-lock perms. Forcing client-side lock");
4681 force_client_lock = TRUE;
4685 /* leave any other codes as-is */
4689 int check_data_version = FALSE;
4692 /* first check if we have permission to elevate or obtain
4694 code = cm_LockCheckPerms(scp, Which, userp, reqp, &has_insert);
4696 if (code == CM_ERROR_NOACCESS && Which == LockRead &&
4697 (!has_insert || !SCP_SUPPORTS_WRITELOCKACL(scp))) {
4698 osi_Log0(afsd_logp, " User has no read-lock perms. Forcing client-side lock");
4699 force_client_lock = TRUE;
4704 /* has_insert => (Which == LockRead, code == CM_ERROR_NOACCESS) */
4706 if (scp->serverLock == LockRead && Which == LockWrite) {
4708 /* We want to escalate the lock to a LockWrite.
4709 * Unfortunately that's not really possible without
4710 * letting go of the current lock. But for now we do
4714 " attempting to UPGRADE from LockRead to LockWrite.");
4716 " dataVersion on scp: %I64d", scp->dataVersion);
4718 /* we assume at this point (because scp->serverLock
4719 was valid) that we had a valid server lock. */
4720 scp->lockDataVersion = scp->dataVersion;
4721 check_data_version = TRUE;
4723 code = cm_IntReleaseLock(scp, userp, reqp);
4726 /* We couldn't release the lock */
4729 scp->serverLock = -1;
4733 /* We need to obtain a server lock of type Which in order
4734 * to assert this file lock */
4735 #ifndef AGGRESSIVE_LOCKS
4738 newLock = LockWrite;
4741 code = cm_IntSetLock(scp, userp, newLock, reqp);
4743 #ifdef AGGRESSIVE_LOCKS
4744 if ((code == CM_ERROR_WOULDBLOCK ||
4745 code == CM_ERROR_NOACCESS) && newLock != Which) {
4746 /* we wanted LockRead. We tried LockWrite. Now try
4751 osi_assertx(newLock == LockRead, "lock type not read");
4753 code = cm_IntSetLock(scp, userp, newLock, reqp);
4757 if (code == CM_ERROR_NOACCESS) {
4758 if (Which == LockRead) {
4759 if (has_insert && SCP_SUPPORTS_WRITELOCKACL(scp)) {
4761 /* We requested a read-lock, but we have permission to
4762 * get a write-lock. Try that */
4764 tcode = cm_LockCheckPerms(scp, LockWrite, userp, reqp, NULL);
4767 newLock = LockWrite;
4769 osi_Log0(afsd_logp, " User has 'i' perms and the request was for a LockRead. Trying to get a LockWrite instead");
4771 code = cm_IntSetLock(scp, userp, newLock, reqp);
4774 osi_Log0(afsd_logp, " User has no read-lock perms. Forcing client-side lock");
4775 force_client_lock = TRUE;
4777 } else if (Which == LockWrite &&
4778 scp->creator == userp && !SCP_SUPPORTS_WRITELOCKACL(scp)) {
4781 /* Special case: if the lock request was for a
4782 * LockWrite and the user owns the file and we weren't
4783 * allowed to obtain the serverlock, we either lost a
4784 * race (the permissions changed from under us), or we
4785 * have 'i' bits, but we aren't allowed to lock the
4788 /* check if we lost a race... */
4789 tcode = cm_LockCheckPerms(scp, Which, userp, reqp, NULL);
4792 osi_Log0(afsd_logp, " User has 'i' perms but can't obtain write locks. Using client-side locks.");
4793 force_client_lock = TRUE;
4798 if (code == 0 && check_data_version &&
4799 scp->dataVersion != scp->lockDataVersion) {
4800 /* We lost a race. Although we successfully obtained
4801 * a lock, someone modified the file in between. The
4802 * locks have all been technically lost. */
4805 " Data version mismatch while upgrading lock.");
4807 " Data versions before=%I64d, after=%I64d",
4808 scp->lockDataVersion,
4811 " Releasing stale lock for scp 0x%x", scp);
4813 code = cm_IntReleaseLock(scp, userp, reqp);
4815 scp->serverLock = -1;
4817 code = CM_ERROR_INVAL;
4818 } else if (code == 0) {
4819 scp->serverLock = newLock;
4820 scp->lockDataVersion = scp->dataVersion;
4824 (scp->sharedLocks > 0 || scp->exclusiveLocks > 0) &&
4825 scp->serverLock == -1) {
4826 /* Oops. We lost the lock. */
4827 cm_LockMarkSCacheLost(scp);
4830 } else if (code == 0) { /* server locks not enabled */
4832 " Skipping server lock for scp");
4837 if (code != 0 && !force_client_lock) {
4838 /* Special case error translations
4840 Applications don't expect certain errors from a
4841 LockFile/UnlockFile call. We need to translate some error
4842 code to codes that apps expect and handle. */
4844 /* We shouldn't actually need to handle this case since we
4845 simulate locks for RO scps anyway. */
4846 if (code == CM_ERROR_READONLY) {
4847 osi_Log0(afsd_logp, " Reinterpreting CM_ERROR_READONLY as CM_ERROR_NOACCESS");
4848 code = CM_ERROR_NOACCESS;
4852 if (code == 0 || (code == CM_ERROR_WOULDBLOCK && allowWait) ||
4853 force_client_lock) {
4855 /* clear the error if we are forcing a client lock, so we
4856 don't get confused later. */
4857 if (force_client_lock && code != CM_ERROR_WOULDBLOCK)
4862 lock_ObtainWrite(&cm_scacheLock);
4863 fileLock = cm_GetFileLock();
4865 fileLock->fid = scp->fid;
4867 fileLock->key = key;
4868 fileLock->lockType = Which;
4869 fileLock->userp = userp;
4870 fileLock->range = range;
4871 fileLock->flags = (code == 0 ? 0 :
4873 CM_FILELOCK_FLAG_WAITUNLOCK :
4874 CM_FILELOCK_FLAG_WAITLOCK));
4876 if (force_client_lock || !SERVERLOCKS_ENABLED(scp))
4877 fileLock->flags |= CM_FILELOCK_FLAG_CLIENTONLY;
4879 fileLock->lastUpdate = (code == 0 && !force_client_lock) ? time(NULL) : 0;
4881 osi_QAddT(&scp->fileLocksH, &scp->fileLocksT, &fileLock->fileq);
4882 cm_HoldSCacheNoLock(scp);
4883 fileLock->scp = scp;
4884 osi_QAdd(&cm_allFileLocks, &fileLock->q);
4885 lock_ReleaseWrite(&cm_scacheLock);
4891 if (IS_LOCK_CLIENTONLY(fileLock)) {
4893 } else if (IS_LOCK_ACCEPTED(fileLock)) {
4894 if (Which == LockRead)
4897 scp->exclusiveLocks++;
4901 "cm_Lock Lock added 0x%p flags 0x%x to scp [0x%p]",
4902 fileLock, fileLock->flags, scp);
4904 " exclusives[%d] shared[%d] client[%d] serverLock[%d]",
4905 scp->exclusiveLocks, scp->sharedLocks, scp->clientLocks,
4906 (int)(signed char) scp->serverLock);
4909 "cm_Lock Rejecting lock (code = 0x%x)", code);
4912 /* Convert from would block to lock not granted */
4913 if (code == CM_ERROR_WOULDBLOCK)
4914 code = CM_ERROR_LOCK_NOT_GRANTED;
4920 cm_IntUnlock(cm_scache_t * scp,
4926 osi_assertx(scp->sharedLocks >= 0, "scp->sharedLocks < 0");
4927 osi_assertx(scp->exclusiveLocks >= 0, "scp->exclusiveLocks < 0");
4928 osi_assertx(scp->clientLocks >= 0, "scp->clientLocks < 0");
4930 if (!SERVERLOCKS_ENABLED(scp)) {
4931 osi_Log0(afsd_logp, " Skipping server lock for scp");
4935 /* Ideally we would go through the rest of the locks to determine
4936 * if one or more locks that were formerly in WAITUNLOCK can now
4937 * be put to ACTIVE or WAITLOCK and update scp->exclusiveLocks and
4938 * scp->sharedLocks accordingly. However, the retrying of locks
4939 * in that manner is done cm_RetryLock() manually.
4942 if (scp->serverLock == LockWrite &&
4943 scp->exclusiveLocks == 0 &&
4944 scp->sharedLocks > 0) {
4945 /* The serverLock should be downgraded to LockRead */
4946 osi_Log0(afsd_logp, " DOWNGRADE lock from LockWrite to LockRead");
4948 /* Make sure there are no dirty buffers left. */
4949 code = cm_FSync(scp, userp, reqp, TRUE);
4951 /* since scp->serverLock looked sane, we are going to assume
4952 that we have a valid server lock. */
4953 scp->lockDataVersion = scp->dataVersion;
4954 osi_Log1(afsd_logp, " dataVersion on scp = %I64d", scp->dataVersion);
4956 /* before we downgrade, make sure that we have enough
4957 permissions to get the read lock. */
4958 code = cm_LockCheckPerms(scp, LockRead, userp, reqp, NULL);
4961 osi_Log0(afsd_logp, " SKIPPING downgrade because user doesn't have perms to get downgraded lock");
4967 code = cm_IntReleaseLock(scp, userp, reqp);
4970 /* so we couldn't release it. Just let the lock be for now */
4974 scp->serverLock = -1;
4977 code = cm_IntSetLock(scp, userp, LockRead, reqp);
4979 if (code == 0 && scp->lockDataVersion == scp->dataVersion) {
4980 scp->serverLock = LockRead;
4981 } else if (code == 0 && scp->lockDataVersion != scp->dataVersion) {
4982 /* We lost a race condition. Although we have a valid
4983 lock on the file, the data has changed and essentially
4984 we have lost the lock we had during the transition. */
4986 osi_Log0(afsd_logp, "Data version mismatch during lock downgrade");
4987 osi_Log2(afsd_logp, " Data versions before=%I64d, after=%I64d",
4988 scp->lockDataVersion,
4991 code = cm_IntReleaseLock(scp, userp, reqp);
4993 code = CM_ERROR_INVAL;
4994 scp->serverLock = -1;
4998 (scp->sharedLocks > 0 || scp->exclusiveLocks > 0) &&
4999 (scp->serverLock == -1)) {
5001 cm_LockMarkSCacheLost(scp);
5004 /* failure here has no bearing on the return value of cm_Unlock() */
5007 } else if (scp->serverLock != (-1) &&
5008 scp->exclusiveLocks == 0 &&
5009 scp->sharedLocks == 0) {
5010 /* The serverLock should be released entirely */
5012 if (scp->serverLock == LockWrite) {
5013 osi_Log0(afsd_logp, " RELEASE LockWrite -> LockNone");
5015 /* Make sure there are no dirty buffers left. */
5016 code = cm_FSync(scp, userp, reqp, TRUE);
5018 osi_Log0(afsd_logp, " RELEASE LockRead -> LockNone");
5021 code = cm_IntReleaseLock(scp, userp, reqp);
5024 scp->serverLock = (-1);
5030 /* Called with scp->rw held */
5031 long cm_UnlockByKey(cm_scache_t * scp,
5038 cm_file_lock_t *fileLock;
5039 osi_queue_t *q, *qn;
5042 osi_Log4(afsd_logp, "cm_UnlockByKey scp 0x%p key <0x%x,0x%x,0x%x",
5043 scp, key.process_id, key.session_id, key.file_id);
5044 osi_Log1(afsd_logp, " flags=0x%x", flags);
5046 lock_ObtainWrite(&cm_scacheLock);
5048 for (q = scp->fileLocksH; q; q = qn) {
5051 fileLock = (cm_file_lock_t *)
5052 ((char *) q - offsetof(cm_file_lock_t, fileq));
5055 osi_Log4(afsd_logp, " Checking lock[0x%x] range[%d,+%d] type[%d]",
5057 (unsigned long) fileLock->range.offset,
5058 (unsigned long) fileLock->range.length,
5059 fileLock->lockType);
5060 osi_Log4(afsd_logp, " key<0x%x, 0x%x, 0x%x> flags[0x%x]",
5061 fileLock->key.process_id, fileLock->key.session_id, fileLock->key.file_id,
5064 if (cm_FidCmp(&fileLock->fid, &fileLock->scp->fid)) {
5065 osi_Log0(afsd_logp, "!!fileLock->fid != scp->fid");
5066 osi_Log4(afsd_logp, " fileLock->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
5068 fileLock->fid.volume,
5069 fileLock->fid.vnode,
5070 fileLock->fid.unique);
5071 osi_Log4(afsd_logp, " scp->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
5072 fileLock->scp->fid.cell,
5073 fileLock->scp->fid.volume,
5074 fileLock->scp->fid.vnode,
5075 fileLock->scp->fid.unique);
5076 osi_assertx(FALSE, "invalid fid value");
5080 if (!IS_LOCK_DELETED(fileLock) &&
5081 cm_KeyEquals(&fileLock->key, &key, flags)) {
5082 osi_Log3(afsd_logp, "...Unlock range [%d,+%d] type %d",
5083 fileLock->range.offset,
5084 fileLock->range.length,
5085 fileLock->lockType);
5087 osi_QRemoveHT(&scp->fileLocksH, &scp->fileLocksT, q);
5089 if (IS_LOCK_CLIENTONLY(fileLock)) {
5091 } else if (IS_LOCK_ACCEPTED(fileLock)) {
5092 if (fileLock->lockType == LockRead)
5095 scp->exclusiveLocks--;
5098 fileLock->flags |= CM_FILELOCK_FLAG_DELETED;
5104 lock_ReleaseWrite(&cm_scacheLock);
5106 if (n_unlocks == 0) {
5107 osi_Log0(afsd_logp, "cm_UnlockByKey no locks found");
5108 osi_Log3(afsd_logp, " Leaving scp with exclusives[%d], shared[%d], serverLock[%d]",
5109 scp->exclusiveLocks, scp->sharedLocks, (int)(signed char) scp->serverLock);
5114 osi_Log1(afsd_logp, "cm_UnlockByKey done with %d locks", n_unlocks);
5116 osi_Log1(afsd_logp, "cm_UnlockByKey code 0x%x", code);
5117 osi_Log4(afsd_logp, " Leaving scp with excl[%d], shared[%d], client[%d], serverLock[%d]",
5118 scp->exclusiveLocks, scp->sharedLocks, scp->clientLocks,
5119 (int)(signed char) scp->serverLock);
5124 long cm_Unlock(cm_scache_t *scp,
5125 unsigned char sLockType,
5126 LARGE_INTEGER LOffset, LARGE_INTEGER LLength,
5133 int Which = ((sLockType & LOCKING_ANDX_SHARED_LOCK) ? LockRead : LockWrite);
5134 cm_file_lock_t *fileLock;
5136 int release_userp = FALSE;
5137 int exact_match = !(flags & CM_UNLOCK_FLAG_MATCH_RANGE);
5139 LARGE_INTEGER RangeEnd;
5141 osi_Log4(afsd_logp, "cm_Unlock scp 0x%p type 0x%x offset 0x%x length 0x%x",
5142 scp, sLockType, (unsigned long)LOffset.QuadPart, (unsigned long)LLength.QuadPart);
5143 osi_Log4(afsd_logp, "... key <0x%x,0x%x,0x%x> flags 0x%x",
5144 key.process_id, key.session_id, key.file_id, flags);
5147 RangeEnd.QuadPart = LOffset.QuadPart + LLength.QuadPart;
5150 lock_ObtainRead(&cm_scacheLock);
5152 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
5153 fileLock = (cm_file_lock_t *)
5154 ((char *) q - offsetof(cm_file_lock_t, fileq));
5157 if (cm_FidCmp(&fileLock->fid, &fileLock->scp->fid)) {
5158 osi_Log0(afsd_logp, "!!fileLock->fid != scp->fid");
5159 osi_Log4(afsd_logp, " fileLock->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
5161 fileLock->fid.volume,
5162 fileLock->fid.vnode,
5163 fileLock->fid.unique);
5164 osi_Log4(afsd_logp, " scp->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
5165 fileLock->scp->fid.cell,
5166 fileLock->scp->fid.volume,
5167 fileLock->scp->fid.vnode,
5168 fileLock->scp->fid.unique);
5169 osi_assertx(FALSE, "invalid fid value");
5173 if (!IS_LOCK_DELETED(fileLock) &&
5174 cm_KeyEquals(&fileLock->key, &key, 0) &&
5175 fileLock->range.offset == LOffset.QuadPart &&
5176 fileLock->range.length == LLength.QuadPart) {
5182 if (!IS_LOCK_DELETED(fileLock) &&
5183 cm_KeyEquals(&fileLock->key, &key, 0) &&
5184 fileLock->range.offset >= LOffset.QuadPart &&
5185 fileLock->range.offset < RangeEnd.QuadPart &&
5186 (fileLock->range.offset + fileLock->range.length) <= RangeEnd.QuadPart) {
5194 lock_ReleaseRead(&cm_scacheLock);
5196 if (lock_found && !exact_match) {
5200 osi_Log0(afsd_logp, "cm_Unlock lock not found; failure");
5202 /* The lock didn't exist anyway. *shrug* */
5203 return CM_ERROR_RANGE_NOT_LOCKED;
5207 /* discard lock record */
5208 lock_ConvertRToW(&cm_scacheLock);
5209 osi_QRemoveHT(&scp->fileLocksH, &scp->fileLocksT, q);
5212 * Don't delete it here; let the daemon delete it, to simplify
5213 * the daemon's traversal of the list.
5216 if (IS_LOCK_CLIENTONLY(fileLock)) {
5218 } else if (IS_LOCK_ACCEPTED(fileLock)) {
5219 if (fileLock->lockType == LockRead)
5222 scp->exclusiveLocks--;
5225 fileLock->flags |= CM_FILELOCK_FLAG_DELETED;
5226 lock_ReleaseWrite(&cm_scacheLock);
5228 if (release_userp) {
5229 cm_ReleaseUser(userp);
5230 release_userp = FALSE;
5234 osi_Log1(afsd_logp, "cm_Unlock not exact match, searching for next lock, code 0x%x", code);
5235 goto try_again; /* might be more than one lock in the range */
5240 osi_Log1(afsd_logp, "cm_Unlock code 0x%x", code);
5241 osi_Log4(afsd_logp, " leaving scp with excl[%d], shared[%d], client[%d], serverLock[%d]",
5242 scp->exclusiveLocks, scp->sharedLocks, scp->clientLocks,
5243 (int)(signed char) scp->serverLock);
5248 /* called with scp->rw held */
5249 void cm_LockMarkSCacheLost(cm_scache_t * scp)
5251 cm_file_lock_t *fileLock;
5254 osi_Log1(afsd_logp, "cm_LockMarkSCacheLost scp 0x%x", scp);
5256 /* cm_scacheLock needed because we are modifying fileLock->flags */
5257 lock_ObtainWrite(&cm_scacheLock);
5259 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
5261 (cm_file_lock_t *)((char *) q - offsetof(cm_file_lock_t, fileq));
5263 if (IS_LOCK_ACTIVE(fileLock) &&
5264 !IS_LOCK_CLIENTONLY(fileLock)) {
5265 if (fileLock->lockType == LockRead)
5268 scp->exclusiveLocks--;
5270 fileLock->flags |= CM_FILELOCK_FLAG_LOST;
5274 scp->serverLock = -1;
5275 scp->lockDataVersion = CM_SCACHE_VERSION_BAD;
5276 lock_ReleaseWrite(&cm_scacheLock);
5279 /* Called with no relevant locks held */
5280 void cm_CheckLocks()
5282 osi_queue_t *q, *nq;
5283 cm_file_lock_t *fileLock;
5289 struct rx_connection * rxconnp;
5292 memset(&volSync, 0, sizeof(volSync));
5296 lock_ObtainWrite(&cm_scacheLock);
5298 cm_lockRefreshCycle++;
5300 osi_Log1(afsd_logp, "cm_CheckLocks starting lock check cycle %d", cm_lockRefreshCycle);
5302 for (q = cm_allFileLocks; q; q = nq) {
5303 fileLock = (cm_file_lock_t *) q;
5307 if (IS_LOCK_DELETED(fileLock)) {
5308 cm_user_t *userp = fileLock->userp;
5309 cm_scache_t *scp = fileLock->scp;
5310 fileLock->userp = NULL;
5311 fileLock->scp = NULL;
5313 lock_ReleaseWrite(&cm_scacheLock);
5314 lock_ObtainWrite(&scp->rw);
5315 code = cm_IntUnlock(scp, userp, &req);
5316 lock_ReleaseWrite(&scp->rw);
5318 cm_ReleaseUser(fileLock->userp);
5319 lock_ObtainWrite(&cm_scacheLock);
5320 cm_ReleaseSCacheNoLock(scp);
5322 osi_QRemove(&cm_allFileLocks, q);
5323 cm_PutFileLock(fileLock);
5325 } else if (IS_LOCK_ACTIVE(fileLock) && !IS_LOCK_CLIENTONLY(fileLock)) {
5327 /* Server locks must have been enabled for us to have
5328 received an active non-client-only lock. */
5329 osi_assertx(cm_enableServerLocks, "!cm_enableServerLocks");
5331 scp = fileLock->scp;
5332 osi_assertx(scp != NULL, "null cm_scache_t");
5334 cm_HoldSCacheNoLock(scp);
5337 if (cm_FidCmp(&fileLock->fid, &fileLock->scp->fid)) {
5338 osi_Log0(afsd_logp, "!!fileLock->fid != scp->fid");
5339 osi_Log4(afsd_logp, " fileLock->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
5341 fileLock->fid.volume,
5342 fileLock->fid.vnode,
5343 fileLock->fid.unique);
5344 osi_Log4(afsd_logp, " scp->fid(cell=[%d], volume=[%d], vnode=[%d], unique=[%d]",
5345 fileLock->scp->fid.cell,
5346 fileLock->scp->fid.volume,
5347 fileLock->scp->fid.vnode,
5348 fileLock->scp->fid.unique);
5349 osi_assertx(FALSE, "invalid fid");
5352 /* Server locks are extended once per scp per refresh
5354 if (scp->lastRefreshCycle != cm_lockRefreshCycle) {
5356 int scp_done = FALSE;
5358 osi_Log1(afsd_logp, "cm_CheckLocks Updating scp 0x%x", scp);
5360 lock_ReleaseWrite(&cm_scacheLock);
5361 lock_ObtainWrite(&scp->rw);
5363 /* did the lock change while we weren't holding the lock? */
5364 if (!IS_LOCK_ACTIVE(fileLock))
5365 goto post_syncopdone;
5367 code = cm_SyncOp(scp, NULL, fileLock->userp, &req, 0,
5368 CM_SCACHESYNC_NEEDCALLBACK
5369 | CM_SCACHESYNC_GETSTATUS
5370 | CM_SCACHESYNC_LOCK);
5374 "cm_CheckLocks SyncOp failure code 0x%x", code);
5375 goto post_syncopdone;
5378 /* cm_SyncOp releases scp->rw during which the lock
5379 may get released. */
5380 if (!IS_LOCK_ACTIVE(fileLock))
5381 goto pre_syncopdone;
5383 if (scp->serverLock != -1 && !(scp->flags & CM_SCACHEFLAG_DELETED)) {
5387 tfid.Volume = scp->fid.volume;
5388 tfid.Vnode = scp->fid.vnode;
5389 tfid.Unique = scp->fid.unique;
5391 userp = fileLock->userp;
5393 osi_Log3(afsd_logp, "CALL ExtendLock lock 0x%p for scp=0x%p with lock %d",
5396 (int) scp->serverLock);
5398 lock_ReleaseWrite(&scp->rw);
5401 code = cm_ConnFromFID(&cfid, userp,
5406 rxconnp = cm_GetRxConn(connp);
5407 code = RXAFS_ExtendLock(rxconnp, &tfid,
5409 rx_PutConnection(rxconnp);
5411 osi_Log1(afsd_logp, " ExtendLock returns %d", code);
5413 } while (cm_Analyze(connp, userp, &req,
5414 &cfid, &volSync, NULL, NULL,
5417 code = cm_MapRPCError(code, &req);
5419 lock_ObtainWrite(&scp->rw);
5422 osi_Log1(afsd_logp, "CALL ExtendLock FAILURE, code 0x%x", code);
5423 scp->fsLockCount = 0;
5425 osi_Log0(afsd_logp, "CALL ExtendLock SUCCESS");
5426 scp->lockDataVersion = scp->dataVersion;
5429 if ((code == EINVAL || code == CM_ERROR_INVAL) &&
5430 scp->lockDataVersion == scp->dataVersion) {
5434 (scp->exclusiveLocks > 0) ? LockWrite: LockRead;
5436 /* we might still have a chance to obtain a
5439 code = cm_IntSetLock(scp, userp, lockType, &req);
5442 code = CM_ERROR_INVAL;
5443 } else if (scp->lockDataVersion != scp->dataVersion) {
5445 /* now check if we still have the file at
5446 the right data version. */
5448 "Data version mismatch on scp 0x%p",
5451 " Data versions: before=%I64d, after=%I64d",
5452 scp->lockDataVersion,
5455 code = cm_IntReleaseLock(scp, userp, &req);
5457 code = CM_ERROR_INVAL;
5461 if (code == EINVAL || code == CM_ERROR_INVAL ||
5462 code == CM_ERROR_BADFD) {
5463 cm_LockMarkSCacheLost(scp);
5467 /* interestingly, we have found an active lock
5468 belonging to an scache that has no
5470 cm_LockMarkSCacheLost(scp);
5477 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
5480 lock_ReleaseWrite(&scp->rw);
5482 lock_ObtainWrite(&cm_scacheLock);
5485 fileLock->lastUpdate = time(NULL);
5489 scp->lastRefreshCycle = cm_lockRefreshCycle;
5492 /* we have already refreshed the locks on this scp */
5493 fileLock->lastUpdate = time(NULL);
5496 cm_ReleaseSCacheNoLock(scp);
5498 } else if (IS_LOCK_ACTIVE(fileLock) && IS_LOCK_CLIENTONLY(fileLock)) {
5499 /* TODO: Check callbacks */
5503 lock_ReleaseWrite(&cm_scacheLock);
5504 osi_Log1(afsd_logp, "cm_CheckLocks completes lock check cycle %d", cm_lockRefreshCycle);
5507 /* NOT called with scp->rw held. */
5508 long cm_RetryLock(cm_file_lock_t *oldFileLock, int client_is_dead)
5511 cm_scache_t *scp = NULL;
5512 cm_file_lock_t *fileLock;
5516 int force_client_lock = FALSE;
5517 int has_insert = FALSE;
5518 int check_data_version = FALSE;
5522 if (client_is_dead) {
5523 code = CM_ERROR_TIMEDOUT;
5527 lock_ObtainRead(&cm_scacheLock);
5529 osi_Log2(afsd_logp, "cm_RetryLock checking lock %p (scp=%p)", oldFileLock, oldFileLock->scp);
5530 osi_Log4(afsd_logp, " offset(%x:%x) length(%x:%x)",
5531 (unsigned)(oldFileLock->range.offset >> 32),
5532 (unsigned)(oldFileLock->range.offset & 0xffffffff),
5533 (unsigned)(oldFileLock->range.length >> 32),
5534 (unsigned)(oldFileLock->range.length & 0xffffffff));
5535 osi_Log4(afsd_logp, " key<0x%x,0x%x,0x%x> flags=%x",
5536 oldFileLock->key.process_id, oldFileLock->key.session_id, oldFileLock->key.file_id,
5537 (unsigned)(oldFileLock->flags));
5539 /* if the lock has already been granted, then we have nothing to do */
5540 if (IS_LOCK_ACTIVE(oldFileLock)) {
5541 lock_ReleaseRead(&cm_scacheLock);
5542 osi_Log0(afsd_logp, "cm_RetryLock lock already granted");
5546 /* we can't do anything with lost or deleted locks at the moment. */
5547 if (IS_LOCK_LOST(oldFileLock) || IS_LOCK_DELETED(oldFileLock)) {
5548 code = CM_ERROR_BADFD;
5549 osi_Log0(afsd_logp, "cm_RetryLock lock is lost or deleted");
5550 lock_ReleaseRead(&cm_scacheLock);
5554 scp = oldFileLock->scp;
5556 osi_assertx(scp != NULL, "null cm_scache_t");
5558 lock_ReleaseRead(&cm_scacheLock);
5559 lock_ObtainWrite(&scp->rw);
5561 code = cm_LockCheckPerms(scp, oldFileLock->lockType,
5565 if (code == CM_ERROR_NOACCESS && oldFileLock->lockType == LockRead) {
5566 if (!has_insert || !SCP_SUPPORTS_WRITELOCKACL(scp)) {
5567 force_client_lock = TRUE;
5571 lock_ReleaseWrite(&scp->rw);
5575 lock_ObtainWrite(&cm_scacheLock);
5577 /* Check if we already have a sufficient server lock to allow this
5578 lock to go through. */
5579 if (IS_LOCK_WAITLOCK(oldFileLock) &&
5580 (!SERVERLOCKS_ENABLED(scp) ||
5581 scp->serverLock == oldFileLock->lockType ||
5582 scp->serverLock == LockWrite)) {
5584 oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITLOCK;
5586 if (SERVERLOCKS_ENABLED(scp)) {
5587 osi_Log1(afsd_logp, "cm_RetryLock Server lock (%d) is sufficient for lock. Granting",
5588 (int) scp->serverLock);
5590 osi_Log0(afsd_logp, "cm_RetryLock skipping server lock for scp");
5593 lock_ReleaseWrite(&cm_scacheLock);
5594 lock_ReleaseWrite(&scp->rw);
5599 if (IS_LOCK_WAITUNLOCK(oldFileLock)) {
5601 /* check if the conflicting locks have dissappeared already */
5602 for (q = scp->fileLocksH; q; q = osi_QNext(q)) {
5604 fileLock = (cm_file_lock_t *)
5605 ((char *) q - offsetof(cm_file_lock_t, fileq));
5607 if (IS_LOCK_LOST(fileLock)) {
5608 if (cm_KeyEquals(&fileLock->key, &oldFileLock->key, 0)) {
5609 code = CM_ERROR_BADFD;
5610 oldFileLock->flags |= CM_FILELOCK_FLAG_LOST;
5611 osi_Log1(afsd_logp, " found lost lock %p for same key. Marking lock as lost",
5614 } else if (fileLock->lockType == LockWrite &&
5615 INTERSECT_RANGE(oldFileLock->range, fileLock->range)) {
5616 osi_Log1(afsd_logp, " found conflicting LOST lock %p", fileLock);
5617 code = CM_ERROR_WOULDBLOCK;
5622 if (IS_LOCK_ACCEPTED(fileLock) &&
5623 INTERSECT_RANGE(oldFileLock->range, fileLock->range)) {
5625 if (oldFileLock->lockType != LockRead ||
5626 fileLock->lockType != LockRead) {
5628 osi_Log1(afsd_logp, " found conflicting lock %p", fileLock);
5629 code = CM_ERROR_WOULDBLOCK;
5637 lock_ReleaseWrite(&cm_scacheLock);
5638 lock_ReleaseWrite(&scp->rw);
5643 /* when we get here, the lock is either a WAITUNLOCK or WAITLOCK.
5644 If it is WAITUNLOCK, then we didn't find any conflicting lock
5645 but we haven't verfied whether the serverLock is sufficient to
5646 assert it. If it is WAITLOCK, then the serverLock is
5647 insufficient to assert it. Eitherway, we are ready to accept
5648 the lock as either ACTIVE or WAITLOCK depending on the
5651 /* First, promote the WAITUNLOCK to a WAITLOCK */
5652 if (IS_LOCK_WAITUNLOCK(oldFileLock)) {
5653 if (oldFileLock->lockType == LockRead)
5656 scp->exclusiveLocks++;
5658 oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITUNLOCK;
5659 oldFileLock->flags |= CM_FILELOCK_FLAG_WAITLOCK;
5662 osi_assertx(IS_LOCK_WAITLOCK(oldFileLock), "!IS_LOCK_WAITLOCK");
5664 if (force_client_lock ||
5665 !SERVERLOCKS_ENABLED(scp) ||
5666 scp->serverLock == oldFileLock->lockType ||
5667 (oldFileLock->lockType == LockRead &&
5668 scp->serverLock == LockWrite)) {
5670 oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITLOCK;
5672 if ((force_client_lock ||
5673 !SERVERLOCKS_ENABLED(scp)) &&
5674 !IS_LOCK_CLIENTONLY(oldFileLock)) {
5676 oldFileLock->flags |= CM_FILELOCK_FLAG_CLIENTONLY;
5678 if (oldFileLock->lockType == LockRead)
5681 scp->exclusiveLocks--;
5686 lock_ReleaseWrite(&cm_scacheLock);
5687 lock_ReleaseWrite(&scp->rw);
5694 code = cm_SyncOp(scp, NULL, oldFileLock->userp, &req, 0,
5695 CM_SCACHESYNC_NEEDCALLBACK
5696 | CM_SCACHESYNC_GETSTATUS
5697 | CM_SCACHESYNC_LOCK);
5699 osi_Log1(afsd_logp, "cm_RetryLock SyncOp failure code 0x%x", code);
5700 lock_ReleaseWrite(&cm_scacheLock);
5701 goto post_syncopdone;
5704 if (!IS_LOCK_WAITLOCK(oldFileLock))
5705 goto pre_syncopdone;
5707 userp = oldFileLock->userp;
5709 #ifndef AGGRESSIVE_LOCKS
5710 newLock = oldFileLock->lockType;
5712 newLock = LockWrite;
5716 /* if has_insert is non-zero, then:
5717 - the lock a LockRead
5718 - we don't have permission to get a LockRead
5719 - we do have permission to get a LockWrite
5720 - the server supports VICED_CAPABILITY_WRITELOCKACL
5723 newLock = LockWrite;
5726 lock_ReleaseWrite(&cm_scacheLock);
5728 /* when we get here, either we have a read-lock and want a
5729 write-lock or we don't have any locks and we want some
5732 if (scp->serverLock == LockRead) {
5734 osi_assertx(newLock == LockWrite, "!LockWrite");
5736 osi_Log0(afsd_logp, " Attempting to UPGRADE from LockRead to LockWrite");
5738 scp->lockDataVersion = scp->dataVersion;
5739 check_data_version = TRUE;
5741 code = cm_IntReleaseLock(scp, userp, &req);
5744 goto pre_syncopdone;
5746 scp->serverLock = -1;
5749 code = cm_IntSetLock(scp, userp, newLock, &req);
5752 if (scp->dataVersion != scp->lockDataVersion) {
5753 /* we lost a race. too bad */
5756 " Data version mismatch while upgrading lock.");
5758 " Data versions before=%I64d, after=%I64d",
5759 scp->lockDataVersion,
5762 " Releasing stale lock for scp 0x%x", scp);
5764 code = cm_IntReleaseLock(scp, userp, &req);
5766 scp->serverLock = -1;
5768 code = CM_ERROR_INVAL;
5770 cm_LockMarkSCacheLost(scp);
5772 scp->serverLock = newLock;
5777 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
5783 if (code != 0 && code != CM_ERROR_WOULDBLOCK) {
5784 lock_ObtainWrite(&cm_scacheLock);
5785 osi_QRemoveHT(&scp->fileLocksH, &scp->fileLocksT, &oldFileLock->fileq);
5786 lock_ReleaseWrite(&cm_scacheLock);
5788 lock_ReleaseWrite(&scp->rw);
5791 lock_ObtainWrite(&cm_scacheLock);
5793 oldFileLock->flags &= ~CM_FILELOCK_FLAG_WAITLOCK;
5794 } else if (code != CM_ERROR_WOULDBLOCK) {
5795 oldFileLock->flags |= CM_FILELOCK_FLAG_DELETED;
5796 cm_ReleaseUser(oldFileLock->userp);
5797 oldFileLock->userp = NULL;
5798 if (oldFileLock->scp) {
5799 cm_ReleaseSCacheNoLock(oldFileLock->scp);
5800 oldFileLock->scp = NULL;
5803 lock_ReleaseWrite(&cm_scacheLock);
5808 cm_key_t cm_GenerateKey(afs_uint16 session_id, afs_offs_t process_id, afs_uint16 file_id)
5812 key.process_id = process_id;
5813 key.session_id = session_id;
5814 key.file_id = file_id;
5819 int cm_KeyEquals(cm_key_t *k1, cm_key_t *k2, int flags)
5821 return (k1->session_id == k2->session_id) && (k1->file_id == k2->file_id) &&
5822 ((flags & CM_UNLOCK_BY_FID) || (k1->process_id == k2->process_id));
5825 void cm_ReleaseAllLocks(void)
5831 cm_file_lock_t *fileLock;
5834 for (i = 0; i < cm_data.scacheHashTableSize; i++)
5836 for ( scp = cm_data.scacheHashTablep[i]; scp; scp = scp->nextp ) {
5837 while (scp->fileLocksH != NULL) {
5838 lock_ObtainWrite(&scp->rw);
5839 lock_ObtainWrite(&cm_scacheLock);
5840 if (!scp->fileLocksH) {
5841 lock_ReleaseWrite(&cm_scacheLock);
5842 lock_ReleaseWrite(&scp->rw);
5845 fileLock = (cm_file_lock_t *)((char *) scp->fileLocksH - offsetof(cm_file_lock_t, fileq));
5846 userp = fileLock->userp;
5848 key = fileLock->key;
5849 cm_HoldSCacheNoLock(scp);
5850 lock_ReleaseWrite(&cm_scacheLock);
5851 cm_UnlockByKey(scp, key, 0, userp, &req);
5852 cm_ReleaseSCache(scp);
5853 cm_ReleaseUser(userp);
5854 lock_ReleaseWrite(&scp->rw);