2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
27 extern void afsi_log(char *pattern, ...);
30 int cm_enableServerLocks = 1;
32 int cm_followBackupPath = 0;
35 * Case-folding array. This was constructed by inspecting of SMBtrace output.
36 * I do not know anything more about it.
38 unsigned char cm_foldUpper[256] = {
39 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
40 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
41 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
42 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
43 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
44 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
45 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
46 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
47 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
48 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
49 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
50 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
51 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
52 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
53 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
54 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
55 0x80, 0x9a, 0x90, 0x41, 0x8e, 0x41, 0x8f, 0x80,
56 0x45, 0x45, 0x45, 0x49, 0x49, 0x49, 0x8e, 0x8f,
57 0x90, 0x92, 0x92, 0x4f, 0x99, 0x4f, 0x55, 0x55,
58 0x59, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
59 0x41, 0x49, 0x4f, 0x55, 0xa5, 0xa5, 0x56, 0xa7,
60 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
61 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
62 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
63 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
64 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
65 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
66 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
67 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
68 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
69 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
70 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
74 * Case-insensitive string comparison. We used to use stricmp, but it doesn't
75 * know about 8-bit characters (e.g. 129 is lowercase u-umlaut, 154 is
76 * upper-case u-umlaut).
78 int cm_stricmp(const char *str1, const char *str2)
90 c1 = (char) cm_foldUpper[(unsigned char)(*str1++)];
91 c2 = (char) cm_foldUpper[(unsigned char)(*str2++)];
99 /* characters that are legal in an 8.3 name */
101 * We used to have 1's for all characters from 128 to 254. But
102 * the NT client behaves better if we create an 8.3 name for any
103 * name that has a character with the high bit on, and if we
104 * delete those characters from 8.3 names. In particular, see
105 * Sybase defect 10859.
107 char cm_LegalChars[256] = {
108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,
111 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
112 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
113 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
114 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
115 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
126 /* return true iff component is a valid 8.3 name */
127 int cm_Is8Dot3(char *namep)
134 * can't have a leading dot;
135 * special case for . and ..
137 if (namep[0] == '.') {
140 if (namep[1] == '.' && namep[2] == 0)
144 while (tc = *namep++) {
146 /* saw another dot */
147 if (sawDot) return 0; /* second dot */
152 if (cm_LegalChars[tc] == 0)
155 if (!sawDot && charCount > 8)
156 /* more than 8 chars in name */
158 if (sawDot && charCount > 3)
159 /* more than 3 chars in extension */
166 * Number unparsing map for generating 8.3 names;
167 * The version taken from DFS was on drugs.
168 * You can't include '&' and '@' in a file name.
170 char cm_8Dot3Mapping[42] =
171 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
172 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
173 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
174 'V', 'W', 'X', 'Y', 'Z', '_', '-', '$', '#', '!', '+', '='
176 int cm_8Dot3MapSize = sizeof(cm_8Dot3Mapping);
178 void cm_Gen8Dot3NameInt(const char * longname, cm_dirFid_t * pfid,
179 char *shortName, char **shortNameEndp)
183 int vnode = ntohl(pfid->vnode);
185 int validExtension = 0;
189 /* Unparse the file's vnode number to get a "uniquifier" */
191 number[nsize] = cm_8Dot3Mapping[vnode % cm_8Dot3MapSize];
193 vnode /= cm_8Dot3MapSize;
197 * Look for valid extension. There has to be a dot, and
198 * at least one of the characters following has to be legal.
200 lastDot = strrchr(longname, '.');
202 temp = lastDot; temp++;
204 if (cm_LegalChars[tc])
210 /* Copy name characters */
211 for (i = 0, name = longname;
212 i < (7 - nsize) && name != lastDot; ) {
217 if (!cm_LegalChars[tc])
220 *shortName++ = toupper(tc);
226 /* Copy uniquifier characters */
227 memcpy(shortName, number, nsize);
230 if (validExtension) {
231 /* Copy extension characters */
232 *shortName++ = *lastDot++; /* copy dot */
233 for (i = 0, tc = *lastDot++;
236 if (cm_LegalChars[tc]) {
238 *shortName++ = toupper(tc);
247 *shortNameEndp = shortName;
250 /* return success if we can open this file in this mode */
251 long cm_CheckOpen(cm_scache_t *scp, int openMode, int trunc, cm_user_t *userp,
259 rights |= PRSFS_READ;
260 if (openMode == 1 || openMode == 2 || trunc)
261 rights |= PRSFS_WRITE;
263 lock_ObtainMutex(&scp->mx);
265 code = cm_SyncOp(scp, NULL, userp, reqp, rights,
266 CM_SCACHESYNC_GETSTATUS
267 | CM_SCACHESYNC_NEEDCALLBACK
268 | CM_SCACHESYNC_LOCK);
271 ((rights & PRSFS_WRITE) || (rights & PRSFS_READ)) &&
272 scp->fileType == CM_SCACHETYPE_FILE) {
275 unsigned int sLockType;
276 LARGE_INTEGER LOffset, LLength;
278 /* Check if there's some sort of lock on the file at the
281 key = cm_GenerateKey(CM_SESSION_CMINT,0,0);
283 if (rights & PRSFS_WRITE)
286 sLockType = LOCKING_ANDX_SHARED_LOCK;
288 LOffset.HighPart = CM_FLSHARE_OFFSET_HIGH;
289 LOffset.LowPart = CM_FLSHARE_OFFSET_LOW;
290 LLength.HighPart = CM_FLSHARE_LENGTH_HIGH;
291 LLength.LowPart = CM_FLSHARE_LENGTH_LOW;
293 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp, NULL);
296 cm_Unlock(scp, sLockType, LOffset, LLength, key, userp, reqp);
298 /* In this case, we allow the file open to go through even
299 though we can't enforce mandatory locking on the
301 if (code == CM_ERROR_NOACCESS &&
302 !(rights & PRSFS_WRITE))
306 case CM_ERROR_ALLOFFLINE:
307 case CM_ERROR_ALLDOWN:
308 case CM_ERROR_ALLBUSY:
309 case CM_ERROR_TIMEDOUT:
311 case CM_ERROR_WOULDBLOCK:
314 code = CM_ERROR_SHARING_VIOLATION;
319 } else if (code != 0) {
323 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
327 lock_ReleaseMutex(&scp->mx);
332 /* return success if we can open this file in this mode */
333 long cm_CheckNTOpen(cm_scache_t *scp, unsigned int desiredAccess,
334 unsigned int createDisp, cm_user_t *userp, cm_req_t *reqp,
335 cm_lock_data_t **ldpp)
340 osi_assertx(ldpp != NULL, "null cm_lock_data_t");
343 /* Always allow delete; the RPC will tell us if it's OK */
344 if (desiredAccess == DELETE)
349 if (desiredAccess & AFS_ACCESS_READ)
350 rights |= (scp->fileType == CM_SCACHETYPE_DIRECTORY ? PRSFS_LOOKUP : PRSFS_READ);
352 /* We used to require PRSFS_WRITE if createDisp was 4
353 (OPEN_ALWAYS) even if AFS_ACCESS_WRITE was not requested.
354 However, we don't need to do that since the existence of the
355 scp implies that we don't need to create it. */
356 if (desiredAccess & AFS_ACCESS_WRITE)
357 rights |= PRSFS_WRITE;
359 lock_ObtainMutex(&scp->mx);
361 code = cm_SyncOp(scp, NULL, userp, reqp, rights,
362 CM_SCACHESYNC_GETSTATUS
363 | CM_SCACHESYNC_NEEDCALLBACK
364 | CM_SCACHESYNC_LOCK);
367 * If the open will fail because the volume is readonly, then we will
368 * return an access denied error instead. This is to help brain-dead
369 * apps run correctly on replicated volumes.
370 * See defect 10007 for more information.
372 if (code == CM_ERROR_READONLY)
373 code = CM_ERROR_NOACCESS;
376 ((rights & PRSFS_WRITE) || (rights & PRSFS_READ)) &&
377 scp->fileType == CM_SCACHETYPE_FILE) {
379 unsigned int sLockType;
380 LARGE_INTEGER LOffset, LLength;
382 /* Check if there's some sort of lock on the file at the
385 key = cm_GenerateKey(CM_SESSION_CMINT,0,0);
386 if (rights & PRSFS_WRITE)
389 sLockType = LOCKING_ANDX_SHARED_LOCK;
391 /* single byte lock at offset 0x0100 0000 0000 0000 */
392 LOffset.HighPart = CM_FLSHARE_OFFSET_HIGH;
393 LOffset.LowPart = CM_FLSHARE_OFFSET_LOW;
394 LLength.HighPart = CM_FLSHARE_LENGTH_HIGH;
395 LLength.LowPart = CM_FLSHARE_LENGTH_LOW;
397 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp, NULL);
400 (*ldpp) = (cm_lock_data_t *)malloc(sizeof(cm_lock_data_t));
407 (*ldpp)->sLockType = sLockType;
408 (*ldpp)->LOffset.HighPart = LOffset.HighPart;
409 (*ldpp)->LOffset.LowPart = LOffset.LowPart;
410 (*ldpp)->LLength.HighPart = LLength.HighPart;
411 (*ldpp)->LLength.LowPart = LLength.LowPart;
413 /* In this case, we allow the file open to go through even
414 though we can't enforce mandatory locking on the
416 if (code == CM_ERROR_NOACCESS &&
417 !(rights & PRSFS_WRITE))
421 case CM_ERROR_ALLOFFLINE:
422 case CM_ERROR_ALLDOWN:
423 case CM_ERROR_ALLBUSY:
424 case CM_ERROR_TIMEDOUT:
426 case CM_ERROR_WOULDBLOCK:
429 code = CM_ERROR_SHARING_VIOLATION;
433 } else if (code != 0) {
438 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
441 lock_ReleaseMutex(&scp->mx);
443 osi_Log3(afsd_logp,"cm_CheckNTOpen scp 0x%p ldp 0x%p code 0x%x", scp, *ldpp, code);
447 extern long cm_CheckNTOpenDone(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
448 cm_lock_data_t ** ldpp)
450 osi_Log2(afsd_logp,"cm_CheckNTOpenDone scp 0x%p ldp 0x%p", scp, *ldpp);
452 lock_ObtainMutex(&scp->mx);
453 cm_Unlock(scp, (*ldpp)->sLockType, (*ldpp)->LOffset, (*ldpp)->LLength,
454 (*ldpp)->key, userp, reqp);
455 lock_ReleaseMutex(&scp->mx);
462 * When CAP_NT_SMBS has been negotiated, deletion (of files or directories) is
463 * done in three steps:
464 * (1) open for deletion (NT_CREATE_AND_X)
465 * (2) set for deletion on close (NT_TRANSACTION2, SET_FILE_INFO)
467 * We must not do the RPC until step 3. But if we are going to return an error
468 * code (e.g. directory not empty), we must return it by step 2, otherwise most
469 * clients will not notice it. So we do a preliminary check. For deleting
470 * files, this is almost free, since we have already done the RPC to get the
471 * parent directory's status bits. But for deleting directories, we must do an
472 * additional RPC to get the directory's data to check if it is empty. Sigh.
474 long cm_CheckNTDelete(cm_scache_t *dscp, cm_scache_t *scp, cm_user_t *userp,
480 cm_dirEntry_t *dep = 0;
481 unsigned short *hashTable;
483 int BeyondPage = 0, HaveDot = 0, HaveDotDot = 0;
485 /* First check permissions */
486 lock_ObtainMutex(&scp->mx);
487 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_DELETE,
488 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
489 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
490 lock_ReleaseMutex(&scp->mx);
494 /* If deleting directory, must be empty */
496 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
499 thyper.HighPart = 0; thyper.LowPart = 0;
500 code = buf_Get(scp, &thyper, &bufferp);
504 lock_ObtainMutex(&bufferp->mx);
505 lock_ObtainMutex(&scp->mx);
507 code = cm_SyncOp(scp, bufferp, userp, reqp, 0,
508 CM_SCACHESYNC_NEEDCALLBACK
510 | CM_SCACHESYNC_BUFLOCKED);
514 if (cm_HaveBuffer(scp, bufferp, 1))
517 /* otherwise, load the buffer and try again */
518 lock_ReleaseMutex(&bufferp->mx);
519 code = cm_GetBuffer(scp, bufferp, NULL, userp, reqp);
520 lock_ReleaseMutex(&scp->mx);
521 lock_ObtainMutex(&bufferp->mx);
522 lock_ObtainMutex(&scp->mx);
523 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
528 /* We try to determine emptiness without looking beyond the first page,
529 * and without assuming "." and ".." are present and are on the first
530 * page (though these assumptions might, after all, be reasonable).
532 hashTable = (unsigned short *)(bufferp->datap + (32 * 5));
533 for (i=0; i<128; i++) {
534 idx = ntohs(hashTable[i]);
540 dep = (cm_dirEntry_t *)(bufferp->datap + (32 * idx));
541 if (strcmp(dep->name, ".") == 0)
543 else if (strcmp(dep->name, "..") == 0)
546 code = CM_ERROR_NOTEMPTY;
549 idx = ntohs(dep->next);
552 if (BeyondPage && HaveDot && HaveDotDot)
553 code = CM_ERROR_NOTEMPTY;
557 lock_ReleaseMutex(&bufferp->mx);
558 buf_Release(bufferp);
559 lock_ReleaseMutex(&scp->mx);
564 * Iterate through all entries in a directory.
565 * When the function funcp is called, the buffer is locked but the
566 * directory vnode is not.
568 * If the retscp parameter is not NULL, the parmp must be a
569 * cm_lookupSearch_t object.
571 long cm_ApplyDir(cm_scache_t *scp, cm_DirFuncp_t funcp, void *parmp,
572 osi_hyper_t *startOffsetp, cm_user_t *userp, cm_req_t *reqp,
573 cm_scache_t **retscp)
577 cm_dirEntry_t *dep = 0;
580 osi_hyper_t dirLength;
581 osi_hyper_t bufferOffset;
582 osi_hyper_t curOffset;
586 cm_pageHeader_t *pageHeaderp;
588 long nextEntryCookie;
589 int numDirChunks; /* # of 32 byte dir chunks in this entry */
591 /* get the directory size */
592 lock_ObtainMutex(&scp->mx);
593 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_LOOKUP,
594 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
595 lock_ReleaseMutex(&scp->mx);
599 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
600 return CM_ERROR_NOTDIR;
602 if (retscp) /* if this is a lookup call */
604 cm_lookupSearch_t* sp = parmp;
607 #ifdef AFS_FREELANCE_CLIENT
608 /* Freelance entries never end up in the DNLC because they
609 * do not have an associated cm_server_t
611 !(cm_freelanceEnabled &&
612 sp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
613 sp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
614 #else /* !AFS_FREELANCE_CLIENT */
619 int casefold = sp->caseFold;
620 sp->caseFold = 0; /* we have a strong preference for exact matches */
621 if ( *retscp = cm_dnlcLookup(scp, sp)) /* dnlc hit */
623 sp->caseFold = casefold;
626 sp->caseFold = casefold;
628 /* see if we can find it using the directory hash tables.
629 we can only do exact matches, since the hash is case
639 code = cm_BeginDirOp(scp, userp, reqp, CM_DIRLOCK_READ, &dirop);
643 code = cm_BPlusDirLookup(&dirop, sp->searchNamep, &sp->fid);
648 code = cm_DirLookup(&dirop, sp->searchNamep, &sp->fid);
656 sp->ExactFound = TRUE;
657 *retscp = NULL; /* force caller to call cm_GetSCache() */
662 if (sp->caseFold && code == CM_ERROR_INEXACT_MATCH) {
665 sp->ExactFound = FALSE;
666 *retscp = NULL; /* force caller to call cm_GetSCache() */
670 return CM_ERROR_BPLUS_NOMATCH;
678 * XXX We only get the length once. It might change when we drop the
681 dirLength = scp->length;
684 bufferOffset.LowPart = bufferOffset.HighPart = 0;
686 curOffset = *startOffsetp;
688 curOffset.HighPart = 0;
689 curOffset.LowPart = 0;
693 /* make sure that curOffset.LowPart doesn't point to the first
694 * 32 bytes in the 2nd through last dir page, and that it
695 * doesn't point at the first 13 32-byte chunks in the first
696 * dir page, since those are dir and page headers, and don't
697 * contain useful information.
699 temp = curOffset.LowPart & (2048-1);
700 if (curOffset.HighPart == 0 && curOffset.LowPart < 2048) {
701 /* we're in the first page */
702 if (temp < 13*32) temp = 13*32;
705 /* we're in a later dir page */
706 if (temp < 32) temp = 32;
709 /* make sure the low order 5 bits are zero */
712 /* now put temp bits back ito curOffset.LowPart */
713 curOffset.LowPart &= ~(2048-1);
714 curOffset.LowPart |= temp;
716 /* check if we've passed the dir's EOF */
717 if (LargeIntegerGreaterThanOrEqualTo(curOffset, dirLength))
720 /* see if we can use the bufferp we have now; compute in which
721 * page the current offset would be, and check whether that's
722 * the offset of the buffer we have. If not, get the buffer.
724 thyper.HighPart = curOffset.HighPart;
725 thyper.LowPart = curOffset.LowPart & ~(cm_data.buf_blockSize-1);
726 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
729 lock_ReleaseMutex(&bufferp->mx);
730 buf_Release(bufferp);
734 code = buf_Get(scp, &thyper, &bufferp);
736 /* if buf_Get() fails we do not have a buffer object to lock */
742 /* for the IFS version, we bulkstat the dirents because this
743 routine is used in place of smb_ReceiveCoreSearchDir. our
744 other option is to modify smb_ReceiveCoreSearchDir itself,
745 but this seems to be the proper use for cm_ApplyDir. */
746 lock_ObtainMutex(&scp->mx);
747 if ((scp->flags & CM_SCACHEFLAG_BULKSTATTING) == 0
748 && (scp->bulkStatProgress.QuadPart <= thyper.QuadPart))
750 scp->flags |= CM_SCACHEFLAG_BULKSTATTING;
751 code = cm_TryBulkStat(scp, &thyper, userp, reqp);
752 scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
753 scp->bulkStatProgress = thyper;
755 lock_ReleaseMutex(&scp->mx);
758 lock_ObtainMutex(&bufferp->mx);
759 bufferOffset = thyper;
761 /* now get the data in the cache */
763 lock_ObtainMutex(&scp->mx);
764 code = cm_SyncOp(scp, bufferp, userp, reqp,
766 CM_SCACHESYNC_NEEDCALLBACK
768 | CM_SCACHESYNC_BUFLOCKED);
770 lock_ReleaseMutex(&scp->mx);
773 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
775 if (cm_HaveBuffer(scp, bufferp, 1)) {
776 lock_ReleaseMutex(&scp->mx);
780 /* otherwise, load the buffer and try again */
781 lock_ReleaseMutex(&bufferp->mx);
782 code = cm_GetBuffer(scp, bufferp, NULL, userp,
784 lock_ReleaseMutex(&scp->mx);
785 lock_ObtainMutex(&bufferp->mx);
790 lock_ReleaseMutex(&bufferp->mx);
791 buf_Release(bufferp);
795 } /* if (wrong buffer) ... */
797 /* now we have the buffer containing the entry we're interested
798 * in; copy it out if it represents a non-deleted entry.
800 entryInDir = curOffset.LowPart & (2048-1);
801 entryInBuffer = curOffset.LowPart & (cm_data.buf_blockSize - 1);
803 /* page header will help tell us which entries are free. Page
804 * header can change more often than once per buffer, since
805 * AFS 3 dir page size may be less than (but not more than) a
806 * buffer package buffer.
808 /* only look intra-buffer */
809 temp = curOffset.LowPart & (cm_data.buf_blockSize - 1);
810 temp &= ~(2048 - 1); /* turn off intra-page bits */
811 pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
813 /* now determine which entry we're looking at in the page. If
814 * it is free (there's a free bitmap at the start of the dir),
815 * we should skip these 32 bytes.
817 slotInPage = (entryInDir & 0x7e0) >> 5;
818 if (!(pageHeaderp->freeBitmap[slotInPage>>3]
819 & (1 << (slotInPage & 0x7)))) {
820 /* this entry is free */
821 numDirChunks = 1; /* only skip this guy */
825 tp = bufferp->datap + entryInBuffer;
826 dep = (cm_dirEntry_t *) tp; /* now points to AFS3 dir entry */
828 /* while we're here, compute the next entry's location, too,
829 * since we'll need it when writing out the cookie into the
830 * dir listing stream.
832 numDirChunks = cm_NameEntries(dep->name, NULL);
834 /* compute the offset of the cookie representing the next entry */
835 nextEntryCookie = curOffset.LowPart
836 + (CM_DIR_CHUNKSIZE * numDirChunks);
838 if (dep->fid.vnode != 0) {
839 /* this is one of the entries to use: it is not deleted */
840 code = (*funcp)(scp, dep, parmp, &curOffset);
843 } /* if we're including this name */
846 /* and adjust curOffset to be where the new cookie is */
848 thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
849 curOffset = LargeIntegerAdd(thyper, curOffset);
850 } /* while copying data for dir listing */
852 /* release the mutex */
854 lock_ReleaseMutex(&bufferp->mx);
855 buf_Release(bufferp);
860 int cm_NoneUpper(char *s)
864 if (c >= 'A' && c <= 'Z')
869 int cm_NoneLower(char *s)
873 if (c >= 'a' && c <= 'z')
878 long cm_LookupSearchProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
881 cm_lookupSearch_t *sp;
886 sp = (cm_lookupSearch_t *) rockp;
888 matchName = dep->name;
890 match = cm_stricmp(matchName, sp->searchNamep);
892 match = strcmp(matchName, sp->searchNamep);
896 && !cm_Is8Dot3(dep->name)) {
897 matchName = shortName;
898 cm_Gen8Dot3Name(dep, shortName, NULL);
900 match = cm_stricmp(matchName, sp->searchNamep);
902 match = strcmp(matchName, sp->searchNamep);
912 if (!sp->caseFold || matchName == shortName) {
913 sp->fid.vnode = ntohl(dep->fid.vnode);
914 sp->fid.unique = ntohl(dep->fid.unique);
915 return CM_ERROR_STOPNOW;
919 * If we get here, we are doing a case-insensitive search, and we
920 * have found a match. Now we determine what kind of match it is:
921 * exact, lower-case, upper-case, or none of the above. This is done
922 * in order to choose among matches, if there are more than one.
925 /* Exact matches are the best. */
926 match = strcmp(matchName, sp->searchNamep);
929 sp->fid.vnode = ntohl(dep->fid.vnode);
930 sp->fid.unique = ntohl(dep->fid.unique);
931 return CM_ERROR_STOPNOW;
934 /* Lower-case matches are next. */
937 if (cm_NoneUpper(matchName)) {
942 /* Upper-case matches are next. */
945 if (cm_NoneLower(matchName)) {
950 /* General matches are last. */
956 sp->fid.vnode = ntohl(dep->fid.vnode);
957 sp->fid.unique = ntohl(dep->fid.unique);
961 /* read the contents of a mount point into the appropriate string.
962 * called with locked scp, and returns with locked scp.
964 long cm_ReadMountPoint(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
971 if (scp->mountPointStringp[0])
974 /* otherwise, we have to read it in */
975 lock_ReleaseMutex(&scp->mx);
977 thyper.LowPart = thyper.HighPart = 0;
978 code = buf_Get(scp, &thyper, &bufp);
980 lock_ObtainMutex(&scp->mx);
985 code = cm_SyncOp(scp, bufp, userp, reqp, 0,
986 CM_SCACHESYNC_READ | CM_SCACHESYNC_NEEDCALLBACK);
990 cm_SyncOpDone(scp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
992 if (cm_HaveBuffer(scp, bufp, 0))
995 /* otherwise load buffer */
996 code = cm_GetBuffer(scp, bufp, NULL, userp, reqp);
1000 /* locked, has callback, has valid data in buffer */
1001 if ((tlen = scp->length.LowPart) > MOUNTPOINTLEN - 1)
1002 return CM_ERROR_TOOBIG;
1004 code = CM_ERROR_INVAL;
1008 /* someone else did the work while we were out */
1009 if (scp->mountPointStringp[0]) {
1014 /* otherwise, copy out the link */
1015 memcpy(scp->mountPointStringp, bufp->datap, tlen);
1017 /* now make it null-terminated. Note that the original contents of a
1018 * link that is a mount point is "#volname." where "." is there just to
1019 * be turned into a null. That is, we can trash the last char of the
1020 * link without damaging the vol name. This is a stupid convention,
1021 * but that's the protocol.
1023 scp->mountPointStringp[tlen-1] = 0;
1033 /* called with a locked scp and chases the mount point, yielding outScpp.
1034 * scp remains locked, just for simplicity of describing the interface.
1036 long cm_FollowMountPoint(cm_scache_t *scp, cm_scache_t *dscp, cm_user_t *userp,
1037 cm_req_t *reqp, cm_scache_t **outScpp)
1045 cm_volume_t *volp = NULL;
1052 if (scp->mountRootFid.cell != 0 && scp->mountRootGen >= cm_data.mountRootGen) {
1053 tfid = scp->mountRootFid;
1054 lock_ReleaseMutex(&scp->mx);
1055 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1056 lock_ObtainMutex(&scp->mx);
1060 /* parse the volume name */
1061 mpNamep = scp->mountPointStringp;
1063 return CM_ERROR_NOSUCHPATH;
1064 tlen = (int)strlen(scp->mountPointStringp);
1065 mtType = *scp->mountPointStringp;
1066 cellNamep = malloc(tlen);
1067 volNamep = malloc(tlen);
1069 cp = strrchr(mpNamep, ':');
1071 /* cellular mount point */
1072 memset(cellNamep, 0, tlen);
1073 strncpy(cellNamep, mpNamep+1, cp - mpNamep - 1);
1074 strcpy(volNamep, cp+1);
1075 /* now look up the cell */
1076 lock_ReleaseMutex(&scp->mx);
1077 cellp = cm_GetCell(cellNamep, CM_FLAG_CREATE);
1078 lock_ObtainMutex(&scp->mx);
1082 strcpy(volNamep, mpNamep+1);
1084 cellp = cm_FindCellByID(scp->fid.cell, 0);
1088 code = CM_ERROR_NOSUCHCELL;
1092 vnLength = strlen(volNamep);
1093 if (vnLength >= 8 && strcmp(volNamep + vnLength - 7, ".backup") == 0)
1094 targetType = BACKVOL;
1095 else if (vnLength >= 10
1096 && strcmp(volNamep + vnLength - 9, ".readonly") == 0)
1101 /* check for backups within backups */
1102 if (targetType == BACKVOL
1103 && (scp->flags & (CM_SCACHEFLAG_RO | CM_SCACHEFLAG_PURERO))
1104 == CM_SCACHEFLAG_RO) {
1105 code = CM_ERROR_NOSUCHVOLUME;
1109 /* now we need to get the volume */
1110 lock_ReleaseMutex(&scp->mx);
1111 if (cm_VolNameIsID(volNamep)) {
1112 code = cm_GetVolumeByID(cellp, atoi(volNamep), userp, reqp,
1113 CM_GETVOL_FLAG_CREATE, &volp);
1115 code = cm_GetVolumeByName(cellp, volNamep, userp, reqp,
1116 CM_GETVOL_FLAG_CREATE, &volp);
1118 lock_ObtainMutex(&scp->mx);
1121 afs_uint32 cell, volume;
1123 /* save the parent of the volume root for this is the
1124 * place where the volume is mounted and we must remember
1125 * this in the volume structure rather than just in the
1126 * scache entry lest the scache entry gets recycled
1129 lock_ObtainMutex(&volp->mx);
1130 volp->dotdotFid = dscp->fid;
1131 lock_ReleaseMutex(&volp->mx);
1133 cell = cellp->cellID;
1135 /* if the mt pt originates in a .backup volume (not a .readonly)
1136 * and FollowBackupPath is active, and if there is a .backup
1137 * volume for the target, then use the .backup of the target
1138 * instead of the read-write.
1140 if (cm_followBackupPath &&
1142 (dscp->flags & (CM_SCACHEFLAG_RO|CM_SCACHEFLAG_PURERO)) == CM_SCACHEFLAG_RO &&
1143 (targetType == RWVOL || targetType == ROVOL && volp->ro.ID == 0)
1145 targetType = BACKVOL;
1147 /* if the mt pt is in a read-only volume (not just a
1148 * backup), and if there is a read-only volume for the
1149 * target, and if this is a targetType '#' mount point, use
1150 * the read-only, otherwise use the one specified.
1152 else if (mtType == '#' && targetType == RWVOL &&
1153 (scp->flags & CM_SCACHEFLAG_PURERO) &&
1157 if (targetType == ROVOL)
1158 volume = volp->ro.ID;
1159 else if (targetType == BACKVOL)
1160 volume = volp->bk.ID;
1162 volume = volp->rw.ID;
1164 /* the rest of the fid is a magic number */
1165 cm_SetFid(&scp->mountRootFid, cell, volume, 1, 1);
1166 scp->mountRootGen = cm_data.mountRootGen;
1168 tfid = scp->mountRootFid;
1169 lock_ReleaseMutex(&scp->mx);
1170 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1171 lock_ObtainMutex(&scp->mx);
1182 long cm_LookupInternal(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1183 cm_req_t *reqp, cm_scache_t **outpScpp)
1186 int dnlcHit = 1; /* did we hit in the dnlc? yes, we did */
1187 cm_scache_t *tscp = NULL;
1188 cm_scache_t *mountedScp;
1189 cm_lookupSearch_t rock;
1192 memset(&rock, 0, sizeof(rock));
1194 if (dscp->fid.vnode == 1 && dscp->fid.unique == 1
1195 && strcmp(namep, "..") == 0) {
1196 if (dscp->dotdotFid.volume == 0)
1197 return CM_ERROR_NOSUCHVOLUME;
1198 rock.fid = dscp->dotdotFid;
1200 } else if (strcmp(namep, ".") == 0) {
1201 rock.fid = dscp->fid;
1205 if (flags & CM_FLAG_NOMOUNTCHASE) {
1206 /* In this case, we should go and call cm_Dir* functions
1207 directly since the following cm_ApplyDir() function will
1215 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ, &dirop);
1218 code = cm_BPlusDirLookup(&dirop, namep, &rock.fid);
1223 code = cm_DirLookup(&dirop, namep, &rock.fid);
1225 cm_EndDirOp(&dirop);
1235 if (code == CM_ERROR_INEXACT_MATCH && (flags & CM_FLAG_CASEFOLD)) {
1242 return CM_ERROR_BPLUS_NOMATCH;
1247 rock.fid.cell = dscp->fid.cell;
1248 rock.fid.volume = dscp->fid.volume;
1249 rock.searchNamep = namep;
1250 rock.caseFold = (flags & CM_FLAG_CASEFOLD);
1251 rock.hasTilde = ((strchr(namep, '~') != NULL) ? 1 : 0);
1253 /* If NOMOUNTCHASE, bypass DNLC by passing NULL scp pointer */
1254 code = cm_ApplyDir(dscp, cm_LookupSearchProc, &rock, NULL, userp, reqp,
1255 (flags & CM_FLAG_NOMOUNTCHASE) ? NULL : &tscp);
1257 /* code == 0 means we fell off the end of the dir, while stopnow means
1258 * that we stopped early, probably because we found the entry we're
1259 * looking for. Any other non-zero code is an error.
1261 if (code && code != CM_ERROR_STOPNOW) {
1262 /* if the cm_scache_t we are searching in is not a directory
1263 * we must return path not found because the error
1264 * is to describe the final component not an intermediary
1266 if (code == CM_ERROR_NOTDIR) {
1267 if (flags & CM_FLAG_CHECKPATH)
1268 return CM_ERROR_NOSUCHPATH;
1270 return CM_ERROR_NOSUCHFILE;
1275 getroot = (dscp==cm_data.rootSCachep) ;
1277 if (!cm_freelanceEnabled || !getroot) {
1278 if (flags & CM_FLAG_CHECKPATH)
1279 return CM_ERROR_NOSUCHPATH;
1281 return CM_ERROR_NOSUCHFILE;
1283 else if (!strchr(namep, '#') && !strchr(namep, '%') &&
1284 strcmp(namep, "srvsvc") && strcmp(namep, "wkssvc") &&
1285 strcmp(namep, "ipc$")) {
1286 /* nonexistent dir on freelance root, so add it */
1287 char fullname[200] = ".";
1290 osi_Log1(afsd_logp,"cm_Lookup adding mount for non-existent directory: %s",
1291 osi_LogSaveString(afsd_logp,namep));
1294 * There is an ugly behavior where a share name "foo" will be searched
1295 * for as "fo". If the searched for name differs by an already existing
1296 * symlink or mount point in the Freelance directory, do not add the
1297 * new value automatically.
1301 if (namep[0] == '.') {
1302 if (cm_GetCell_Gen(&namep[1], &fullname[1], CM_FLAG_CREATE)) {
1304 if (!cm_FreelanceMountPointExists(fullname, 0))
1305 code = cm_FreelanceAddMount(fullname, &fullname[1], "root.cell.", 1, &rock.fid);
1306 if ( stricmp(&namep[1], &fullname[1]) &&
1307 !cm_FreelanceMountPointExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0) &&
1308 !cm_FreelanceSymlinkExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0))
1309 code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1312 if (cm_GetCell_Gen(namep, fullname, CM_FLAG_CREATE)) {
1314 if (!cm_FreelanceMountPointExists(fullname, 0))
1315 code = cm_FreelanceAddMount(fullname, fullname, "root.cell.", 0, &rock.fid);
1316 if ( stricmp(namep, fullname) &&
1317 !cm_FreelanceMountPointExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0) &&
1318 !cm_FreelanceSymlinkExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0))
1319 code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1322 if (!found || code < 0) { /* add mount point failed, so give up */
1323 if (flags & CM_FLAG_CHECKPATH)
1324 return CM_ERROR_NOSUCHPATH;
1326 return CM_ERROR_NOSUCHFILE;
1328 tscp = NULL; /* to force call of cm_GetSCache */
1333 if ( !tscp ) /* we did not find it in the dnlc */
1336 code = cm_GetSCache(&rock.fid, &tscp, userp, reqp);
1340 /* tscp is now held */
1342 lock_ObtainMutex(&tscp->mx);
1343 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1344 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1346 lock_ReleaseMutex(&tscp->mx);
1347 cm_ReleaseSCache(tscp);
1350 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1351 /* tscp is now locked */
1353 if (!(flags & CM_FLAG_NOMOUNTCHASE)
1354 && tscp->fileType == CM_SCACHETYPE_MOUNTPOINT) {
1355 /* mount points are funny: they have a volume name to mount
1358 code = cm_ReadMountPoint(tscp, userp, reqp);
1360 code = cm_FollowMountPoint(tscp, dscp, userp, reqp,
1362 lock_ReleaseMutex(&tscp->mx);
1363 cm_ReleaseSCache(tscp);
1370 lock_ReleaseMutex(&tscp->mx);
1373 /* copy back pointer */
1376 /* insert scache in dnlc */
1377 if ( !dnlcHit && !(flags & CM_FLAG_NOMOUNTCHASE) && rock.ExactFound ) {
1378 /* lock the directory entry to prevent racing callback revokes */
1379 lock_ObtainMutex(&dscp->mx);
1380 if ( dscp->cbServerp != NULL && dscp->cbExpires > 0 )
1381 cm_dnlcEnter(dscp, namep, tscp);
1382 lock_ReleaseMutex(&dscp->mx);
1389 int cm_ExpandSysName(char *inp, char *outp, long outSize, unsigned int index)
1394 tp = strrchr(inp, '@');
1396 return 0; /* no @sys */
1398 if (strcmp(tp, "@sys") != 0)
1399 return 0; /* no @sys */
1401 /* caller just wants to know if this is a valid @sys type of name */
1405 if (index >= MAXNUMSYSNAMES)
1408 /* otherwise generate the properly expanded @sys name */
1409 prefixCount = (int)(tp - inp);
1411 strncpy(outp, inp, prefixCount); /* copy out "a." from "a.@sys" */
1412 outp[prefixCount] = 0; /* null terminate the "a." */
1413 strcat(outp, cm_sysNameList[index]);/* append i386_nt40 */
1417 long cm_EvaluateVolumeReference(char * namep, long flags, cm_user_t * userp,
1418 cm_req_t *reqp, cm_scache_t ** outpScpp)
1421 char cellName[CELL_MAXNAMELEN];
1422 char volumeName[VL_MAXNAMELEN];
1427 cm_cell_t * cellp = NULL;
1428 cm_volume_t * volp = NULL;
1432 int mountType = RWVOL;
1434 osi_Log1(afsd_logp, "cm_EvaluateVolumeReference for string [%s]",
1435 osi_LogSaveString(afsd_logp, namep));
1437 if (strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) != 0) {
1438 goto _exit_invalid_path;
1441 /* namep is assumed to look like the following:
1443 @vol:<cellname>%<volume>\0
1445 @vol:<cellname>#<volume>\0
1449 cp = namep + CM_PREFIX_VOL_CCH; /* cp points to cell name, hopefully */
1450 tp = strchr(cp, '%');
1452 tp = strchr(cp, '#');
1454 (len = tp - cp) == 0 ||
1455 len > CELL_MAXNAMELEN)
1456 goto _exit_invalid_path;
1457 strncpy(cellName, cp, len);
1458 cellName[len] = '\0';
1463 cp = tp+1; /* cp now points to volume, supposedly */
1464 strncpy(volumeName, cp, VL_MAXNAMELEN-1);
1465 volumeName[VL_MAXNAMELEN - 1] = 0;
1467 /* OK, now we have the cell and the volume */
1468 osi_Log2(afsd_logp, " Found cell [%s] and volume [%s]",
1469 osi_LogSaveString(afsd_logp, cellName),
1470 osi_LogSaveString(afsd_logp, volumeName));
1472 cellp = cm_GetCell(cellName, CM_FLAG_CREATE);
1473 if (cellp == NULL) {
1474 goto _exit_invalid_path;
1477 len = strlen(volumeName);
1478 if (len >= 8 && strcmp(volumeName + len - 7, ".backup") == 0)
1480 else if (len >= 10 &&
1481 strcmp(volumeName + len - 9, ".readonly") == 0)
1486 if (cm_VolNameIsID(volumeName)) {
1487 code = cm_GetVolumeByID(cellp, atoi(volumeName), userp, reqp,
1488 CM_GETVOL_FLAG_CREATE, &volp);
1490 code = cm_GetVolumeByName(cellp, volumeName, userp, reqp,
1491 CM_GETVOL_FLAG_CREATE, &volp);
1497 if (volType == BACKVOL)
1498 volume = volp->bk.ID;
1499 else if (volType == ROVOL ||
1500 (volType == RWVOL && mountType == ROVOL && volp->ro.ID != 0))
1501 volume = volp->ro.ID;
1503 volume = volp->rw.ID;
1505 cm_SetFid(&fid, cellp->cellID, volume, 1, 1);
1507 code = cm_GetSCache(&fid, outpScpp, userp, reqp);
1517 if (flags & CM_FLAG_CHECKPATH)
1518 return CM_ERROR_NOSUCHPATH;
1520 return CM_ERROR_NOSUCHFILE;
1523 #ifdef DEBUG_REFCOUNT
1524 long cm_LookupDbg(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1525 cm_req_t *reqp, cm_scache_t **outpScpp, char * file, long line)
1527 long cm_Lookup(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1528 cm_req_t *reqp, cm_scache_t **outpScpp)
1532 char tname[AFSPATHMAX];
1533 int sysNameIndex = 0;
1534 cm_scache_t *scp = NULL;
1536 #ifdef DEBUG_REFCOUNT
1537 afsi_log("%s:%d cm_Lookup dscp 0x%p ref %d", file, line, dscp, dscp->refCount, file, line);
1538 osi_Log2(afsd_logp, "cm_Lookup dscp 0x%p ref %d", dscp, dscp->refCount);
1541 if ( stricmp(namep,SMB_IOCTL_FILENAME_NOSLASH) == 0 ) {
1542 if (flags & CM_FLAG_CHECKPATH)
1543 return CM_ERROR_NOSUCHPATH;
1545 return CM_ERROR_NOSUCHFILE;
1548 if (dscp == cm_data.rootSCachep &&
1549 strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) == 0) {
1550 return cm_EvaluateVolumeReference(namep, flags, userp, reqp, outpScpp);
1553 if (cm_ExpandSysName(namep, NULL, 0, 0) > 0) {
1554 for ( sysNameIndex = 0; sysNameIndex < MAXNUMSYSNAMES; sysNameIndex++) {
1555 code = cm_ExpandSysName(namep, tname, sizeof(tname), sysNameIndex);
1557 code = cm_LookupInternal(dscp, tname, flags, userp, reqp, &scp);
1558 #ifdef DEBUG_REFCOUNT
1559 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);
1560 osi_Log3(afsd_logp, "cm_LookupInternal (1) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1568 cm_ReleaseSCache(scp);
1572 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1573 #ifdef DEBUG_REFCOUNT
1574 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);
1575 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1582 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1583 #ifdef DEBUG_REFCOUNT
1584 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);
1585 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1591 /* None of the possible sysName expansions could be found */
1592 if (flags & CM_FLAG_CHECKPATH)
1593 return CM_ERROR_NOSUCHPATH;
1595 return CM_ERROR_NOSUCHFILE;
1598 long cm_Unlink(cm_scache_t *dscp, char *namep, cm_user_t *userp, cm_req_t *reqp)
1604 AFSFetchStatus newDirStatus;
1606 struct rx_connection * callp;
1609 #ifdef AFS_FREELANCE_CLIENT
1610 if (cm_freelanceEnabled && dscp == cm_data.rootSCachep) {
1611 /* deleting a mount point from the root dir. */
1612 code = cm_FreelanceRemoveMount(namep);
1617 /* make sure we don't screw up the dir status during the merge */
1618 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
1620 lock_ObtainMutex(&dscp->mx);
1621 sflags = CM_SCACHESYNC_STOREDATA;
1622 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, sflags);
1623 lock_ReleaseMutex(&dscp->mx);
1625 cm_EndDirOp(&dirop);
1630 afsFid.Volume = dscp->fid.volume;
1631 afsFid.Vnode = dscp->fid.vnode;
1632 afsFid.Unique = dscp->fid.unique;
1634 osi_Log1(afsd_logp, "CALL RemoveFile scp 0x%p", dscp);
1636 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
1640 callp = cm_GetRxConn(connp);
1641 code = RXAFS_RemoveFile(callp, &afsFid, namep,
1642 &newDirStatus, &volSync);
1643 rx_PutConnection(callp);
1645 } while (cm_Analyze(connp, userp, reqp, &dscp->fid, &volSync, NULL, NULL, code));
1646 code = cm_MapRPCError(code, reqp);
1649 osi_Log1(afsd_logp, "CALL RemoveFile FAILURE, code 0x%x", code);
1651 osi_Log0(afsd_logp, "CALL RemoveFile SUCCESS");
1654 lock_ObtainWrite(&dirop.scp->dirlock);
1655 dirop.lockType = CM_DIRLOCK_WRITE;
1657 lock_ObtainMutex(&dscp->mx);
1658 cm_dnlcRemove(dscp, namep);
1659 cm_SyncOpDone(dscp, NULL, sflags);
1661 cm_MergeStatus(NULL, dscp, &newDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
1662 } else if (code == CM_ERROR_NOSUCHFILE) {
1663 /* windows would not have allowed the request to delete the file
1664 * if it did not believe the file existed. therefore, we must
1665 * have an inconsistent view of the world.
1667 dscp->cbServerp = NULL;
1669 lock_ReleaseMutex(&dscp->mx);
1671 if (code == 0 && cm_CheckDirOpForSingleChange(&dirop)) {
1672 cm_DirDeleteEntry(&dirop, namep);
1674 cm_BPlusDirDeleteEntry(&dirop, namep);
1677 cm_EndDirOp(&dirop);
1682 /* called with a locked vnode, and fills in the link info.
1683 * returns this the vnode still locked.
1685 long cm_HandleLink(cm_scache_t *linkScp, cm_user_t *userp, cm_req_t *reqp)
1692 lock_AssertMutex(&linkScp->mx);
1693 if (!linkScp->mountPointStringp[0]) {
1694 /* read the link data */
1695 lock_ReleaseMutex(&linkScp->mx);
1696 thyper.LowPart = thyper.HighPart = 0;
1697 code = buf_Get(linkScp, &thyper, &bufp);
1698 lock_ObtainMutex(&linkScp->mx);
1702 code = cm_SyncOp(linkScp, bufp, userp, reqp, 0,
1703 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1708 cm_SyncOpDone(linkScp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1710 if (cm_HaveBuffer(linkScp, bufp, 0))
1713 code = cm_GetBuffer(linkScp, bufp, NULL, userp, reqp);
1718 } /* while loop to get the data */
1720 /* now if we still have no link read in,
1721 * copy the data from the buffer */
1722 if ((temp = linkScp->length.LowPart) >= MOUNTPOINTLEN) {
1724 return CM_ERROR_TOOBIG;
1727 /* otherwise, it fits; make sure it is still null (could have
1728 * lost race with someone else referencing this link above),
1729 * and if so, copy in the data.
1731 if (!linkScp->mountPointStringp[0]) {
1732 strncpy(linkScp->mountPointStringp, bufp->datap, temp);
1733 linkScp->mountPointStringp[temp] = 0; /* null terminate */
1735 if ( !strnicmp(linkScp->mountPointStringp, "msdfs:", strlen("msdfs:")) )
1736 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1739 } /* don't have sym link contents cached */
1744 /* called with a held vnode and a path suffix, with the held vnode being a
1745 * symbolic link. Our goal is to generate a new path to interpret, and return
1746 * this new path in newSpaceBufferp. If the new vnode is relative to a dir
1747 * other than the directory containing the symbolic link, then the new root is
1748 * returned in *newRootScpp, otherwise a null is returned there.
1750 long cm_AssembleLink(cm_scache_t *linkScp, char *pathSuffixp,
1751 cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1752 cm_user_t *userp, cm_req_t *reqp)
1759 *newRootScpp = NULL;
1760 *newSpaceBufferp = NULL;
1762 lock_ObtainMutex(&linkScp->mx);
1763 code = cm_HandleLink(linkScp, userp, reqp);
1767 /* if we may overflow the buffer, bail out; buffer is signficantly
1768 * bigger than max path length, so we don't really have to worry about
1769 * being a little conservative here.
1771 if (strlen(linkScp->mountPointStringp) + strlen(pathSuffixp) + 2
1772 >= CM_UTILS_SPACESIZE)
1773 return CM_ERROR_TOOBIG;
1775 tsp = cm_GetSpace();
1776 linkp = linkScp->mountPointStringp;
1777 if (strncmp(linkp, cm_mountRoot, cm_mountRootLen) == 0) {
1778 if (strlen(linkp) > cm_mountRootLen)
1779 strcpy(tsp->data, linkp+cm_mountRootLen+1);
1782 *newRootScpp = cm_data.rootSCachep;
1783 cm_HoldSCache(cm_data.rootSCachep);
1784 } else if (linkp[0] == '\\' && linkp[1] == '\\') {
1785 if (!strnicmp(&linkp[2], cm_NetbiosName, (len = (long)strlen(cm_NetbiosName))))
1787 char * p = &linkp[len + 3];
1788 if (strnicmp(p, "all", 3) == 0)
1791 strcpy(tsp->data, p);
1792 for (p = tsp->data; *p; p++) {
1796 *newRootScpp = cm_data.rootSCachep;
1797 cm_HoldSCache(cm_data.rootSCachep);
1799 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1800 strcpy(tsp->data, linkp);
1801 code = CM_ERROR_PATH_NOT_COVERED;
1803 } else if ( linkScp->fileType == CM_SCACHETYPE_DFSLINK ||
1804 !strnicmp(linkp, "msdfs:", (len = (long)strlen("msdfs:"))) ) {
1805 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1806 strcpy(tsp->data, linkp);
1807 code = CM_ERROR_PATH_NOT_COVERED;
1808 } else if (*linkp == '\\' || *linkp == '/') {
1810 /* formerly, this was considered to be from the AFS root,
1811 * but this seems to create problems. instead, we will just
1812 * reject the link */
1813 strcpy(tsp->data, linkp+1);
1814 *newRootScpp = cm_data.rootSCachep;
1815 cm_HoldSCache(cm_data.rootSCachep);
1817 /* we still copy the link data into the response so that
1818 * the user can see what the link points to
1820 linkScp->fileType = CM_SCACHETYPE_INVALID;
1821 strcpy(tsp->data, linkp);
1822 code = CM_ERROR_NOSUCHPATH;
1825 /* a relative link */
1826 strcpy(tsp->data, linkp);
1828 if (pathSuffixp[0] != 0) { /* if suffix string is non-null */
1829 strcat(tsp->data, "\\");
1830 strcat(tsp->data, pathSuffixp);
1833 *newSpaceBufferp = tsp;
1837 if (code == CM_ERROR_PATH_NOT_COVERED && reqp->tidPathp && reqp->relPathp)
1838 cm_VolStatus_Notify_DFS_Mapping(linkScp, reqp->tidPathp, reqp->relPathp);
1842 lock_ReleaseMutex(&linkScp->mx);
1845 #ifdef DEBUG_REFCOUNT
1846 long cm_NameIDbg(cm_scache_t *rootSCachep, char *pathp, long flags,
1847 cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp,
1848 char * file, long line)
1850 long cm_NameI(cm_scache_t *rootSCachep, char *pathp, long flags,
1851 cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp)
1855 char *tp; /* ptr moving through input buffer */
1856 char tc; /* temp char */
1857 int haveComponent; /* has new component started? */
1858 char component[AFSPATHMAX]; /* this is the new component */
1859 char *cp; /* component name being assembled */
1860 cm_scache_t *tscp; /* current location in the hierarchy */
1861 cm_scache_t *nscp; /* next dude down */
1862 cm_scache_t *dirScp; /* last dir we searched */
1863 cm_scache_t *linkScp; /* new root for the symlink we just
1865 cm_space_t *psp; /* space for current path, if we've hit
1867 cm_space_t *tempsp; /* temp vbl */
1868 char *restp; /* rest of the pathname to interpret */
1869 int symlinkCount; /* count of # of symlinks traversed */
1870 int extraFlag; /* avoid chasing mt pts for dir cmd */
1871 int phase = 1; /* 1 = tidPathp, 2 = pathp */
1872 #define MAX_FID_COUNT 512
1873 cm_fid_t fids[MAX_FID_COUNT]; /* array of fids processed in this path walk */
1874 int fid_count = 0; /* number of fids processed in this path walk */
1877 #ifdef DEBUG_REFCOUNT
1878 afsi_log("%s:%d cm_NameI rootscp 0x%p ref %d", file, line, rootSCachep, rootSCachep->refCount);
1879 osi_Log4(afsd_logp,"cm_NameI rootscp 0x%p path %s tidpath %s flags 0x%x",
1880 rootSCachep, pathp ? pathp : "<NULL>", tidPathp ? tidPathp : "<NULL>",
1895 cm_HoldSCache(tscp);
1903 /* map Unix slashes into DOS ones so we can interpret Unix
1909 if (!haveComponent) {
1912 } else if (tc == 0) {
1926 /* we have a component here */
1927 if (tc == 0 || tc == '\\') {
1928 /* end of the component; we're at the last
1929 * component if tc == 0. However, if the last
1930 * is a symlink, we have more to do.
1932 *cp++ = 0; /* add null termination */
1934 if ((flags & CM_FLAG_DIRSEARCH) && tc == 0)
1935 extraFlag = CM_FLAG_NOMOUNTCHASE;
1936 code = cm_Lookup(tscp, component,
1938 userp, reqp, &nscp);
1941 if (!strcmp(component,"..") || !strcmp(component,".")) {
1943 * roll back the fid list until we find the fid
1944 * that matches where we are now. Its not necessarily
1945 * one or two fids because they might have been
1946 * symlinks or mount points or both that were crossed.
1948 for ( i=fid_count-1; i>=0; i--) {
1949 if (!cm_FidCmp(&nscp->fid, &fids[i]))
1954 /* add the new fid to the list */
1955 for ( i=0; i<fid_count; i++) {
1956 if ( !cm_FidCmp(&nscp->fid, &fids[i]) ) {
1957 code = CM_ERROR_TOO_MANY_SYMLINKS;
1958 cm_ReleaseSCache(nscp);
1963 if (i == fid_count && fid_count < MAX_FID_COUNT) {
1964 fids[fid_count++] = nscp->fid;
1970 cm_ReleaseSCache(tscp);
1972 cm_ReleaseSCache(dirScp);
1975 if ((code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) &&
1976 tscp->fileType == CM_SCACHETYPE_SYMLINK)
1978 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_NOSUCHPATH");
1979 return CM_ERROR_NOSUCHPATH;
1981 osi_Log1(afsd_logp,"cm_NameI code 0x%x", code);
1986 haveComponent = 0; /* component done */
1988 cm_ReleaseSCache(dirScp);
1989 dirScp = tscp; /* for some symlinks */
1990 tscp = nscp; /* already held */
1992 if (tc == 0 && !(flags & CM_FLAG_FOLLOW) && phase == 2) {
1995 cm_ReleaseSCache(dirScp);
2001 /* now, if tscp is a symlink, we should follow
2002 * it and assemble the path again.
2004 lock_ObtainMutex(&tscp->mx);
2005 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
2006 CM_SCACHESYNC_GETSTATUS
2007 | CM_SCACHESYNC_NEEDCALLBACK);
2009 lock_ReleaseMutex(&tscp->mx);
2010 cm_ReleaseSCache(tscp);
2013 cm_ReleaseSCache(dirScp);
2018 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2020 if (tscp->fileType == CM_SCACHETYPE_SYMLINK) {
2021 /* this is a symlink; assemble a new buffer */
2022 lock_ReleaseMutex(&tscp->mx);
2023 if (symlinkCount++ >= MAX_SYMLINK_COUNT) {
2024 cm_ReleaseSCache(tscp);
2027 cm_ReleaseSCache(dirScp);
2032 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_TOO_MANY_SYMLINKS");
2033 return CM_ERROR_TOO_MANY_SYMLINKS;
2039 code = cm_AssembleLink(tscp, restp, &linkScp, &tempsp, userp, reqp);
2041 if (code == 0 && linkScp != NULL) {
2042 if (linkScp == cm_data.rootSCachep)
2045 for ( i=0; i<fid_count; i++) {
2046 if ( !cm_FidCmp(&linkScp->fid, &fids[i]) ) {
2047 code = CM_ERROR_TOO_MANY_SYMLINKS;
2048 cm_ReleaseSCache(linkScp);
2054 if (i == fid_count && fid_count < MAX_FID_COUNT) {
2055 fids[fid_count++] = linkScp->fid;
2060 /* something went wrong */
2061 cm_ReleaseSCache(tscp);
2064 cm_ReleaseSCache(dirScp);
2070 /* otherwise, tempsp has the new path,
2071 * and linkScp is the new root from
2072 * which to interpret that path.
2073 * Continue with the namei processing,
2074 * also doing the bookkeeping for the
2075 * space allocation and tracking the
2076 * vnode reference counts.
2082 cm_ReleaseSCache(tscp);
2087 * now, if linkScp is null, that's
2088 * AssembleLink's way of telling us that
2089 * the sym link is relative to the dir
2090 * containing the link. We have a ref
2091 * to it in dirScp, and we hold it now
2092 * and reuse it as the new spot in the
2100 /* not a symlink, we may be done */
2101 lock_ReleaseMutex(&tscp->mx);
2109 cm_ReleaseSCache(dirScp);
2117 cm_ReleaseSCache(dirScp);
2120 } /* end of a component */
2123 } /* we have a component */
2124 } /* big while loop over all components */
2128 cm_ReleaseSCache(dirScp);
2134 cm_ReleaseSCache(tscp);
2136 #ifdef DEBUG_REFCOUNT
2137 afsi_log("%s:%d cm_NameI code 0x%x outScpp 0x%p ref %d", file, line, code, *outScpp, (*outScpp)->refCount);
2139 osi_Log2(afsd_logp,"cm_NameI code 0x%x outScpp 0x%p", code, *outScpp);
2143 /* called with a dir, and a vnode within the dir that happens to be a symlink.
2144 * We chase the link, and return a held pointer to the target, if it exists,
2145 * in *outScpp. If we succeed, we return 0, otherwise we return an error code
2146 * and do not hold or return a target vnode.
2148 * This is very similar to calling cm_NameI with the last component of a name,
2149 * which happens to be a symlink, except that we've already passed by the name.
2151 * This function is typically called by the directory listing functions, which
2152 * encounter symlinks but need to return the proper file length so programs
2153 * like "more" work properly when they make use of the attributes retrieved from
2156 * The input vnode should not be locked when this function is called.
2158 long cm_EvaluateSymLink(cm_scache_t *dscp, cm_scache_t *linkScp,
2159 cm_scache_t **outScpp, cm_user_t *userp, cm_req_t *reqp)
2163 cm_scache_t *newRootScp;
2165 osi_Log1(afsd_logp, "Evaluating symlink scp 0x%p", linkScp);
2167 code = cm_AssembleLink(linkScp, "", &newRootScp, &spacep, userp, reqp);
2171 /* now, if newRootScp is NULL, we're really being told that the symlink
2172 * is relative to the current directory (dscp).
2174 if (newRootScp == NULL) {
2176 cm_HoldSCache(dscp);
2179 code = cm_NameI(newRootScp, spacep->data,
2180 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW | CM_FLAG_DIRSEARCH,
2181 userp, NULL, reqp, outScpp);
2183 if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH)
2184 code = CM_ERROR_NOSUCHPATH;
2186 /* this stuff is allocated no matter what happened on the namei call,
2188 cm_FreeSpace(spacep);
2189 cm_ReleaseSCache(newRootScp);
2191 if (linkScp == *outScpp) {
2192 cm_ReleaseSCache(*outScpp);
2194 code = CM_ERROR_NOSUCHPATH;
2200 /* make this big enough so that one buffer of dir pages won't overflow. We'll
2201 * check anyway, but we want to minimize the chance that we have to leave stuff
2204 #define CM_BULKMAX (3 * AFSCBMAX)
2206 /* rock for bulk stat calls */
2207 typedef struct cm_bulkStat {
2208 osi_hyper_t bufOffset; /* only do it for things in this buffer page */
2210 /* info for the actual call */
2211 int counter; /* next free slot */
2212 AFSFid fids[CM_BULKMAX];
2213 AFSFetchStatus stats[CM_BULKMAX];
2214 AFSCallBack callbacks[CM_BULKMAX];
2217 /* for a given entry, make sure that it isn't in the stat cache, and then
2218 * add it to the list of file IDs to be obtained.
2220 * Don't bother adding it if we already have a vnode. Note that the dir
2221 * is locked, so we have to be careful checking the vnode we're thinking of
2222 * processing, to avoid deadlocks.
2224 long cm_TryBulkProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
2235 /* Don't overflow bsp. */
2236 if (bsp->counter >= CM_BULKMAX)
2237 return CM_ERROR_STOPNOW;
2239 thyper.LowPart = cm_data.buf_blockSize;
2240 thyper.HighPart = 0;
2241 thyper = LargeIntegerAdd(thyper, bsp->bufOffset);
2243 /* thyper is now the first byte past the end of the record we're
2244 * interested in, and bsp->bufOffset is the first byte of the record
2245 * we're interested in.
2246 * Skip data in the others.
2249 if (LargeIntegerLessThan(*offp, bsp->bufOffset))
2251 if (LargeIntegerGreaterThanOrEqualTo(*offp, thyper))
2252 return CM_ERROR_STOPNOW;
2253 if (strcmp(dep->name, ".") == 0 || strcmp(dep->name, "..") == 0)
2256 cm_SetFid(&tfid, scp->fid.cell, scp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
2257 tscp = cm_FindSCache(&tfid);
2259 if (lock_TryMutex(&tscp->mx)) {
2260 /* we have an entry that we can look at */
2261 if (!(tscp->flags & CM_SCACHEFLAG_EACCESS) && cm_HaveCallback(tscp)) {
2262 /* we have a callback on it. Don't bother
2263 * fetching this stat entry, since we're happy
2264 * with the info we have.
2266 lock_ReleaseMutex(&tscp->mx);
2267 cm_ReleaseSCache(tscp);
2270 lock_ReleaseMutex(&tscp->mx);
2272 cm_ReleaseSCache(tscp);
2275 #ifdef AFS_FREELANCE_CLIENT
2276 // yj: if this is a mountpoint under root.afs then we don't want it
2277 // to be bulkstat-ed, instead, we call getSCache directly and under
2278 // getSCache, it is handled specially.
2279 if ( cm_freelanceEnabled &&
2280 tfid.cell==AFS_FAKE_ROOT_CELL_ID &&
2281 tfid.volume==AFS_FAKE_ROOT_VOL_ID &&
2282 !(tfid.vnode==0x1 && tfid.unique==0x1) )
2284 osi_Log0(afsd_logp, "cm_TryBulkProc Freelance calls cm_SCache on root.afs mountpoint");
2285 return cm_GetSCache(&tfid, &tscp, NULL, NULL);
2287 #endif /* AFS_FREELANCE_CLIENT */
2290 bsp->fids[i].Volume = scp->fid.volume;
2291 bsp->fids[i].Vnode = tfid.vnode;
2292 bsp->fids[i].Unique = tfid.unique;
2296 /* called with a locked scp and a pointer to a buffer. Make bulk stat
2297 * calls on all undeleted files in the page of the directory specified.
2300 cm_TryBulkStat(cm_scache_t *dscp, osi_hyper_t *offsetp, cm_user_t *userp,
2304 cm_bulkStat_t bb; /* this is *BIG*, probably 16K or so;
2305 * watch for stack problems */
2306 AFSCBFids fidStruct;
2307 AFSBulkStats statStruct;
2309 AFSCBs callbackStruct;
2312 cm_callbackRequest_t cbReq;
2318 struct rx_connection * callp;
2319 int inlinebulk = 0; /* Did we use InlineBulkStatus RPC or not? */
2321 osi_Log1(afsd_logp, "cm_TryBulkStat dir 0x%p", dscp);
2323 /* should be on a buffer boundary */
2324 osi_assertx((offsetp->LowPart & (cm_data.buf_blockSize - 1)) == 0, "invalid offset");
2326 memset(&bb, 0, sizeof(bb));
2327 bb.bufOffset = *offsetp;
2329 lock_ReleaseMutex(&dscp->mx);
2330 /* first, assemble the file IDs we need to stat */
2331 code = cm_ApplyDir(dscp, cm_TryBulkProc, (void *) &bb, offsetp, userp, reqp, NULL);
2333 /* if we failed, bail out early */
2334 if (code && code != CM_ERROR_STOPNOW) {
2335 lock_ObtainMutex(&dscp->mx);
2339 /* otherwise, we may have one or more bulk stat's worth of stuff in bb;
2340 * make the calls to create the entries. Handle AFSCBMAX files at a
2344 while (filex < bb.counter) {
2345 filesThisCall = bb.counter - filex;
2346 if (filesThisCall > AFSCBMAX)
2347 filesThisCall = AFSCBMAX;
2349 fidStruct.AFSCBFids_len = filesThisCall;
2350 fidStruct.AFSCBFids_val = &bb.fids[filex];
2351 statStruct.AFSBulkStats_len = filesThisCall;
2352 statStruct.AFSBulkStats_val = &bb.stats[filex];
2353 callbackStruct.AFSCBs_len = filesThisCall;
2354 callbackStruct.AFSCBs_val = &bb.callbacks[filex];
2355 cm_StartCallbackGrantingCall(NULL, &cbReq);
2356 osi_Log1(afsd_logp, "CALL BulkStatus, %d entries", filesThisCall);
2358 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2362 callp = cm_GetRxConn(connp);
2363 if (!(connp->serverp->flags & CM_SERVERFLAG_NOINLINEBULK)) {
2364 code = RXAFS_InlineBulkStatus(callp, &fidStruct,
2365 &statStruct, &callbackStruct, &volSync);
2366 if (code == RXGEN_OPCODE) {
2367 cm_SetServerNoInlineBulk(connp->serverp, 0);
2373 code = RXAFS_BulkStatus(callp, &fidStruct,
2374 &statStruct, &callbackStruct, &volSync);
2376 rx_PutConnection(callp);
2378 } while (cm_Analyze(connp, userp, reqp, &dscp->fid,
2379 &volSync, NULL, &cbReq, code));
2380 code = cm_MapRPCError(code, reqp);
2382 osi_Log2(afsd_logp, "CALL %sBulkStatus FAILURE code 0x%x",
2383 inlinebulk ? "Inline" : "", code);
2385 osi_Log1(afsd_logp, "CALL %sBulkStatus SUCCESS", inlinebulk ? "Inline" : "");
2387 /* may as well quit on an error, since we're not going to do
2388 * much better on the next immediate call, either.
2391 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2395 /* otherwise, we should do the merges */
2396 for (i = 0; i<filesThisCall; i++) {
2398 cm_SetFid(&tfid, dscp->fid.cell, bb.fids[j].Volume, bb.fids[j].Vnode, bb.fids[j].Unique);
2399 code = cm_GetSCache(&tfid, &scp, userp, reqp);
2403 /* otherwise, if this entry has no callback info,
2406 lock_ObtainMutex(&scp->mx);
2407 /* now, we have to be extra paranoid on merging in this
2408 * information, since we didn't use cm_SyncOp before
2409 * starting the fetch to make sure that no bad races
2410 * were occurring. Specifically, we need to make sure
2411 * we don't obliterate any newer information in the
2412 * vnode than have here.
2414 * Right now, be pretty conservative: if there's a
2415 * callback or a pending call, skip it.
2417 if ((scp->cbServerp == NULL || (scp->flags & CM_SCACHEFLAG_EACCESS))
2419 (CM_SCACHEFLAG_FETCHING
2420 | CM_SCACHEFLAG_STORING
2421 | CM_SCACHEFLAG_SIZESTORING))) {
2422 cm_EndCallbackGrantingCall(scp, &cbReq,
2424 CM_CALLBACK_MAINTAINCOUNT);
2425 cm_MergeStatus(dscp, scp, &bb.stats[j], &volSync, userp, 0);
2427 lock_ReleaseMutex(&scp->mx);
2428 cm_ReleaseSCache(scp);
2429 } /* all files in the response */
2430 /* now tell it to drop the count,
2431 * after doing the vnode processing above */
2432 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2434 filex += filesThisCall;
2435 } /* while there are still more files to process */
2436 lock_ObtainMutex(&dscp->mx);
2438 /* If we did the InlineBulk RPC pull out the return code and log it */
2440 if ((&bb.stats[0])->errorCode) {
2441 osi_Log1(afsd_logp, "cm_TryBulkStat bulk stat error: %d",
2442 (&bb.stats[0])->errorCode);
2446 osi_Log0(afsd_logp, "END cm_TryBulkStat");
2450 void cm_StatusFromAttr(AFSStoreStatus *statusp, cm_scache_t *scp, cm_attr_t *attrp)
2454 /* initialize store back mask as inexpensive local variable */
2456 memset(statusp, 0, sizeof(AFSStoreStatus));
2458 /* copy out queued info from scache first, if scp passed in */
2460 if (scp->mask & CM_SCACHEMASK_CLIENTMODTIME) {
2461 statusp->ClientModTime = scp->clientModTime;
2462 mask |= AFS_SETMODTIME;
2463 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
2468 /* now add in our locally generated request */
2469 if (attrp->mask & CM_ATTRMASK_CLIENTMODTIME) {
2470 statusp->ClientModTime = attrp->clientModTime;
2471 mask |= AFS_SETMODTIME;
2473 if (attrp->mask & CM_ATTRMASK_UNIXMODEBITS) {
2474 statusp->UnixModeBits = attrp->unixModeBits;
2475 mask |= AFS_SETMODE;
2477 if (attrp->mask & CM_ATTRMASK_OWNER) {
2478 statusp->Owner = attrp->owner;
2479 mask |= AFS_SETOWNER;
2481 if (attrp->mask & CM_ATTRMASK_GROUP) {
2482 statusp->Group = attrp->group;
2483 mask |= AFS_SETGROUP;
2486 statusp->Mask = mask;
2489 /* set the file size, and make sure that all relevant buffers have been
2490 * truncated. Ensure that any partially truncated buffers have been zeroed
2491 * to the end of the buffer.
2493 long cm_SetLength(cm_scache_t *scp, osi_hyper_t *sizep, cm_user_t *userp,
2499 /* start by locking out buffer creation */
2500 lock_ObtainWrite(&scp->bufCreateLock);
2502 /* verify that this is a file, not a dir or a symlink */
2503 lock_ObtainMutex(&scp->mx);
2504 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
2505 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2508 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2510 if (scp->fileType != CM_SCACHETYPE_FILE) {
2511 code = CM_ERROR_ISDIR;
2516 if (LargeIntegerLessThan(*sizep, scp->length))
2521 lock_ReleaseMutex(&scp->mx);
2523 /* can't hold scp->mx lock here, since we may wait for a storeback to
2524 * finish if the buffer package is cleaning a buffer by storing it to
2528 buf_Truncate(scp, userp, reqp, sizep);
2530 /* now ensure that file length is short enough, and update truncPos */
2531 lock_ObtainMutex(&scp->mx);
2533 /* make sure we have a callback (so we have the right value for the
2534 * length), and wait for it to be safe to do a truncate.
2536 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_WRITE,
2537 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2538 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2540 /* If we only have 'i' bits, then we should still be able to set
2541 the size of a file we created. */
2542 if (code == CM_ERROR_NOACCESS && scp->creator == userp) {
2543 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
2544 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2545 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2551 if (LargeIntegerLessThan(*sizep, scp->length)) {
2552 /* a real truncation. If truncPos is not set yet, or is bigger
2553 * than where we're truncating the file, set truncPos to this
2558 if (!(scp->mask & CM_SCACHEMASK_TRUNCPOS)
2559 || LargeIntegerLessThan(*sizep, scp->length)) {
2561 scp->truncPos = *sizep;
2562 scp->mask |= CM_SCACHEMASK_TRUNCPOS;
2564 /* in either case, the new file size has been changed */
2565 scp->length = *sizep;
2566 scp->mask |= CM_SCACHEMASK_LENGTH;
2568 else if (LargeIntegerGreaterThan(*sizep, scp->length)) {
2569 /* really extending the file */
2570 scp->length = *sizep;
2571 scp->mask |= CM_SCACHEMASK_LENGTH;
2574 /* done successfully */
2577 cm_SyncOpDone(scp, NULL,
2578 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2579 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2582 lock_ReleaseMutex(&scp->mx);
2583 lock_ReleaseWrite(&scp->bufCreateLock);
2588 /* set the file size or other attributes (but not both at once) */
2589 long cm_SetAttr(cm_scache_t *scp, cm_attr_t *attrp, cm_user_t *userp,
2593 AFSFetchStatus afsOutStatus;
2597 AFSStoreStatus afsInStatus;
2598 struct rx_connection * callp;
2600 /* handle file length setting */
2601 if (attrp->mask & CM_ATTRMASK_LENGTH)
2602 return cm_SetLength(scp, &attrp->length, userp, reqp);
2604 lock_ObtainMutex(&scp->mx);
2605 /* otherwise, we have to make an RPC to get the status */
2606 code = cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STORESTATUS);
2608 lock_ReleaseMutex(&scp->mx);
2612 /* make the attr structure */
2613 cm_StatusFromAttr(&afsInStatus, scp, attrp);
2615 tfid.Volume = scp->fid.volume;
2616 tfid.Vnode = scp->fid.vnode;
2617 tfid.Unique = scp->fid.unique;
2618 lock_ReleaseMutex(&scp->mx);
2620 /* now make the RPC */
2621 osi_Log1(afsd_logp, "CALL StoreStatus scp 0x%p", scp);
2623 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2627 callp = cm_GetRxConn(connp);
2628 code = RXAFS_StoreStatus(callp, &tfid,
2629 &afsInStatus, &afsOutStatus, &volSync);
2630 rx_PutConnection(callp);
2632 } while (cm_Analyze(connp, userp, reqp,
2633 &scp->fid, &volSync, NULL, NULL, code));
2634 code = cm_MapRPCError(code, reqp);
2637 osi_Log1(afsd_logp, "CALL StoreStatus FAILURE, code 0x%x", code);
2639 osi_Log0(afsd_logp, "CALL StoreStatus SUCCESS");
2641 lock_ObtainMutex(&scp->mx);
2642 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STORESTATUS);
2644 cm_MergeStatus(NULL, scp, &afsOutStatus, &volSync, userp,
2645 CM_MERGEFLAG_FORCE|CM_MERGEFLAG_STOREDATA);
2647 /* if we're changing the mode bits, discard the ACL cache,
2648 * since we changed the mode bits.
2650 if (afsInStatus.Mask & AFS_SETMODE)
2651 cm_FreeAllACLEnts(scp);
2652 lock_ReleaseMutex(&scp->mx);
2656 long cm_Create(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2657 cm_scache_t **scpp, cm_user_t *userp, cm_req_t *reqp)
2662 cm_callbackRequest_t cbReq;
2665 cm_scache_t *scp = NULL;
2667 AFSStoreStatus inStatus;
2668 AFSFetchStatus updatedDirStatus;
2669 AFSFetchStatus newFileStatus;
2670 AFSCallBack newFileCallback;
2672 struct rx_connection * callp;
2675 /* can't create names with @sys in them; must expand it manually first.
2676 * return "invalid request" if they try.
2678 if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2679 return CM_ERROR_ATSYS;
2682 #ifdef AFS_FREELANCE_CLIENT
2683 /* Freelance root volume does not hold files */
2684 if (cm_freelanceEnabled &&
2685 dscp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
2686 dscp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
2688 return CM_ERROR_NOACCESS;
2690 #endif /* AFS_FREELANCE_CLIENT */
2692 /* before starting the RPC, mark that we're changing the file data, so
2693 * that someone who does a chmod will know to wait until our call
2696 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2697 lock_ObtainMutex(&dscp->mx);
2698 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2699 lock_ReleaseMutex(&dscp->mx);
2701 cm_StartCallbackGrantingCall(NULL, &cbReq);
2703 cm_EndDirOp(&dirop);
2710 cm_StatusFromAttr(&inStatus, NULL, attrp);
2712 /* try the RPC now */
2713 osi_Log1(afsd_logp, "CALL CreateFile scp 0x%p", dscp);
2715 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2719 dirAFSFid.Volume = dscp->fid.volume;
2720 dirAFSFid.Vnode = dscp->fid.vnode;
2721 dirAFSFid.Unique = dscp->fid.unique;
2723 callp = cm_GetRxConn(connp);
2724 code = RXAFS_CreateFile(connp->callp, &dirAFSFid, namep,
2725 &inStatus, &newAFSFid, &newFileStatus,
2726 &updatedDirStatus, &newFileCallback,
2728 rx_PutConnection(callp);
2730 } while (cm_Analyze(connp, userp, reqp,
2731 &dscp->fid, &volSync, NULL, &cbReq, code));
2732 code = cm_MapRPCError(code, reqp);
2735 osi_Log1(afsd_logp, "CALL CreateFile FAILURE, code 0x%x", code);
2737 osi_Log0(afsd_logp, "CALL CreateFile SUCCESS");
2740 lock_ObtainWrite(&dirop.scp->dirlock);
2741 dirop.lockType = CM_DIRLOCK_WRITE;
2743 lock_ObtainMutex(&dscp->mx);
2744 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2746 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2748 lock_ReleaseMutex(&dscp->mx);
2750 /* now try to create the file's entry, too, but be careful to
2751 * make sure that we don't merge in old info. Since we weren't locking
2752 * out any requests during the file's creation, we may have pretty old
2756 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
2757 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2759 lock_ObtainMutex(&scp->mx);
2760 scp->creator = userp; /* remember who created it */
2761 if (!cm_HaveCallback(scp)) {
2762 cm_MergeStatus(dscp, scp, &newFileStatus, &volSync,
2764 cm_EndCallbackGrantingCall(scp, &cbReq,
2765 &newFileCallback, 0);
2768 lock_ReleaseMutex(&scp->mx);
2773 /* make sure we end things properly */
2775 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2777 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2778 cm_DirCreateEntry(&dirop, namep, &newFid);
2780 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2783 cm_EndDirOp(&dirop);
2788 long cm_FSync(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
2792 code = buf_CleanVnode(scp, userp, reqp);
2794 lock_ObtainMutex(&scp->mx);
2796 if (scp->mask & (CM_SCACHEMASK_TRUNCPOS
2797 | CM_SCACHEMASK_CLIENTMODTIME
2798 | CM_SCACHEMASK_LENGTH))
2799 code = cm_StoreMini(scp, userp, reqp);
2801 if (scp->flags & (CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE)) {
2802 code = (scp->flags & CM_SCACHEFLAG_OVERQUOTA) ? CM_ERROR_QUOTA : CM_ERROR_SPACE;
2803 scp->flags &= ~(CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE);
2806 lock_ReleaseMutex(&scp->mx);
2811 long cm_MakeDir(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2812 cm_user_t *userp, cm_req_t *reqp)
2817 cm_callbackRequest_t cbReq;
2820 cm_scache_t *scp = NULL;
2822 AFSStoreStatus inStatus;
2823 AFSFetchStatus updatedDirStatus;
2824 AFSFetchStatus newDirStatus;
2825 AFSCallBack newDirCallback;
2827 struct rx_connection * callp;
2830 /* can't create names with @sys in them; must expand it manually first.
2831 * return "invalid request" if they try.
2833 if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2834 return CM_ERROR_ATSYS;
2837 #ifdef AFS_FREELANCE_CLIENT
2838 /* Freelance root volume does not hold subdirectories */
2839 if (cm_freelanceEnabled &&
2840 dscp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
2841 dscp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
2843 return CM_ERROR_NOACCESS;
2845 #endif /* AFS_FREELANCE_CLIENT */
2847 /* before starting the RPC, mark that we're changing the directory
2848 * data, so that someone who does a chmod on the dir will wait until
2849 * our call completes.
2851 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2852 lock_ObtainMutex(&dscp->mx);
2853 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2854 lock_ReleaseMutex(&dscp->mx);
2856 cm_StartCallbackGrantingCall(NULL, &cbReq);
2858 cm_EndDirOp(&dirop);
2865 cm_StatusFromAttr(&inStatus, NULL, attrp);
2867 /* try the RPC now */
2868 osi_Log1(afsd_logp, "CALL MakeDir scp 0x%p", dscp);
2870 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2874 dirAFSFid.Volume = dscp->fid.volume;
2875 dirAFSFid.Vnode = dscp->fid.vnode;
2876 dirAFSFid.Unique = dscp->fid.unique;
2878 callp = cm_GetRxConn(connp);
2879 code = RXAFS_MakeDir(connp->callp, &dirAFSFid, namep,
2880 &inStatus, &newAFSFid, &newDirStatus,
2881 &updatedDirStatus, &newDirCallback,
2883 rx_PutConnection(callp);
2885 } while (cm_Analyze(connp, userp, reqp,
2886 &dscp->fid, &volSync, NULL, &cbReq, code));
2887 code = cm_MapRPCError(code, reqp);
2890 osi_Log1(afsd_logp, "CALL MakeDir FAILURE, code 0x%x", code);
2892 osi_Log0(afsd_logp, "CALL MakeDir SUCCESS");
2895 lock_ObtainWrite(&dirop.scp->dirlock);
2896 dirop.lockType = CM_DIRLOCK_WRITE;
2898 lock_ObtainMutex(&dscp->mx);
2899 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2901 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2903 lock_ReleaseMutex(&dscp->mx);
2905 /* now try to create the new dir's entry, too, but be careful to
2906 * make sure that we don't merge in old info. Since we weren't locking
2907 * out any requests during the file's creation, we may have pretty old
2911 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
2912 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2914 lock_ObtainMutex(&scp->mx);
2915 if (!cm_HaveCallback(scp)) {
2916 cm_MergeStatus(dscp, scp, &newDirStatus, &volSync,
2918 cm_EndCallbackGrantingCall(scp, &cbReq,
2919 &newDirCallback, 0);
2922 lock_ReleaseMutex(&scp->mx);
2923 cm_ReleaseSCache(scp);
2927 /* make sure we end things properly */
2929 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2931 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2932 cm_DirCreateEntry(&dirop, namep, &newFid);
2934 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2937 cm_EndDirOp(&dirop);
2939 /* and return error code */
2943 long cm_Link(cm_scache_t *dscp, char *namep, cm_scache_t *sscp, long flags,
2944 cm_user_t *userp, cm_req_t *reqp)
2949 AFSFid existingAFSFid;
2950 AFSFetchStatus updatedDirStatus;
2951 AFSFetchStatus newLinkStatus;
2953 struct rx_connection * callp;
2956 if (dscp->fid.cell != sscp->fid.cell ||
2957 dscp->fid.volume != sscp->fid.volume) {
2958 return CM_ERROR_CROSSDEVLINK;
2961 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2962 lock_ObtainMutex(&dscp->mx);
2963 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2964 lock_ReleaseMutex(&dscp->mx);
2966 cm_EndDirOp(&dirop);
2971 /* try the RPC now */
2972 osi_Log1(afsd_logp, "CALL Link scp 0x%p", dscp);
2974 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2977 dirAFSFid.Volume = dscp->fid.volume;
2978 dirAFSFid.Vnode = dscp->fid.vnode;
2979 dirAFSFid.Unique = dscp->fid.unique;
2981 existingAFSFid.Volume = sscp->fid.volume;
2982 existingAFSFid.Vnode = sscp->fid.vnode;
2983 existingAFSFid.Unique = sscp->fid.unique;
2985 callp = cm_GetRxConn(connp);
2986 code = RXAFS_Link(callp, &dirAFSFid, namep, &existingAFSFid,
2987 &newLinkStatus, &updatedDirStatus, &volSync);
2988 rx_PutConnection(callp);
2989 osi_Log1(smb_logp," RXAFS_Link returns 0x%x", code);
2991 } while (cm_Analyze(connp, userp, reqp,
2992 &dscp->fid, &volSync, NULL, NULL, code));
2994 code = cm_MapRPCError(code, reqp);
2997 osi_Log1(afsd_logp, "CALL Link FAILURE, code 0x%x", code);
2999 osi_Log0(afsd_logp, "CALL Link SUCCESS");
3002 lock_ObtainWrite(&dirop.scp->dirlock);
3003 dirop.lockType = CM_DIRLOCK_WRITE;
3005 lock_ObtainMutex(&dscp->mx);
3006 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3008 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3010 lock_ReleaseMutex(&dscp->mx);
3013 if (cm_CheckDirOpForSingleChange(&dirop)) {
3014 cm_DirCreateEntry(&dirop, namep, &sscp->fid);
3016 cm_BPlusDirCreateEntry(&dirop, namep, &sscp->fid);
3020 cm_EndDirOp(&dirop);
3025 long cm_SymLink(cm_scache_t *dscp, char *namep, char *contentsp, long flags,
3026 cm_attr_t *attrp, cm_user_t *userp, cm_req_t *reqp)
3034 AFSStoreStatus inStatus;
3035 AFSFetchStatus updatedDirStatus;
3036 AFSFetchStatus newLinkStatus;
3038 struct rx_connection * callp;
3041 /* before starting the RPC, mark that we're changing the directory data,
3042 * so that someone who does a chmod on the dir will wait until our
3045 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3046 lock_ObtainMutex(&dscp->mx);
3047 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3048 lock_ReleaseMutex(&dscp->mx);
3050 cm_EndDirOp(&dirop);
3055 cm_StatusFromAttr(&inStatus, NULL, attrp);
3057 /* try the RPC now */
3058 osi_Log1(afsd_logp, "CALL Symlink scp 0x%p", dscp);
3060 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3064 dirAFSFid.Volume = dscp->fid.volume;
3065 dirAFSFid.Vnode = dscp->fid.vnode;
3066 dirAFSFid.Unique = dscp->fid.unique;
3068 callp = cm_GetRxConn(connp);
3069 code = RXAFS_Symlink(callp, &dirAFSFid, namep, contentsp,
3070 &inStatus, &newAFSFid, &newLinkStatus,
3071 &updatedDirStatus, &volSync);
3072 rx_PutConnection(callp);
3074 } while (cm_Analyze(connp, userp, reqp,
3075 &dscp->fid, &volSync, NULL, NULL, code));
3076 code = cm_MapRPCError(code, reqp);
3079 osi_Log1(afsd_logp, "CALL Symlink FAILURE, code 0x%x", code);
3081 osi_Log0(afsd_logp, "CALL Symlink SUCCESS");
3084 lock_ObtainWrite(&dirop.scp->dirlock);
3085 dirop.lockType = CM_DIRLOCK_WRITE;
3087 lock_ObtainMutex(&dscp->mx);
3088 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3090 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3092 lock_ReleaseMutex(&dscp->mx);
3095 if (cm_CheckDirOpForSingleChange(&dirop)) {
3096 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3098 cm_DirCreateEntry(&dirop, namep, &newFid);
3100 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
3104 cm_EndDirOp(&dirop);
3106 /* now try to create the new dir's entry, too, but be careful to
3107 * make sure that we don't merge in old info. Since we weren't locking
3108 * out any requests during the file's creation, we may have pretty old
3112 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3113 code = cm_GetSCache(&newFid, &scp, userp, reqp);
3115 lock_ObtainMutex(&scp->mx);
3116 if (!cm_HaveCallback(scp)) {
3117 cm_MergeStatus(dscp, scp, &newLinkStatus, &volSync,
3120 lock_ReleaseMutex(&scp->mx);
3121 cm_ReleaseSCache(scp);
3125 /* and return error code */
3129 long cm_RemoveDir(cm_scache_t *dscp, char *namep, cm_user_t *userp,
3136 AFSFetchStatus updatedDirStatus;
3138 struct rx_connection * callp;
3141 /* before starting the RPC, mark that we're changing the directory data,
3142 * so that someone who does a chmod on the dir will wait until our
3145 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3146 lock_ObtainMutex(&dscp->mx);
3147 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3148 lock_ReleaseMutex(&dscp->mx);
3150 cm_EndDirOp(&dirop);
3155 /* try the RPC now */
3156 osi_Log1(afsd_logp, "CALL RemoveDir scp 0x%p", dscp);
3158 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3162 dirAFSFid.Volume = dscp->fid.volume;
3163 dirAFSFid.Vnode = dscp->fid.vnode;
3164 dirAFSFid.Unique = dscp->fid.unique;
3166 callp = cm_GetRxConn(connp);
3167 code = RXAFS_RemoveDir(callp, &dirAFSFid, namep,
3168 &updatedDirStatus, &volSync);
3169 rx_PutConnection(callp);
3171 } while (cm_Analyze(connp, userp, reqp,
3172 &dscp->fid, &volSync, NULL, NULL, code));
3173 code = cm_MapRPCErrorRmdir(code, reqp);
3176 osi_Log1(afsd_logp, "CALL RemoveDir FAILURE, code 0x%x", code);
3178 osi_Log0(afsd_logp, "CALL RemoveDir SUCCESS");
3181 lock_ObtainWrite(&dirop.scp->dirlock);
3182 dirop.lockType = CM_DIRLOCK_WRITE;
3184 lock_ObtainMutex(&dscp->mx);
3185 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3187 cm_dnlcRemove(dscp, namep);
3188 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3190 lock_ReleaseMutex(&dscp->mx);
3193 if (cm_CheckDirOpForSingleChange(&dirop)) {
3194 cm_DirDeleteEntry(&dirop, namep);
3196 cm_BPlusDirDeleteEntry(&dirop, namep);
3200 cm_EndDirOp(&dirop);
3202 /* and return error code */
3206 long cm_Open(cm_scache_t *scp, int type, cm_user_t *userp)
3208 /* grab mutex on contents */
3209 lock_ObtainMutex(&scp->mx);
3211 /* reset the prefetch info */
3212 scp->prefetch.base.LowPart = 0; /* base */
3213 scp->prefetch.base.HighPart = 0;
3214 scp->prefetch.end.LowPart = 0; /* and end */
3215 scp->prefetch.end.HighPart = 0;
3217 /* release mutex on contents */
3218 lock_ReleaseMutex(&scp->mx);
3224 long cm_Rename(cm_scache_t *oldDscp, char *oldNamep, cm_scache_t *newDscp,
3225 char *newNamep, cm_user_t *userp, cm_req_t *reqp)
3229 AFSFid oldDirAFSFid;
3230 AFSFid newDirAFSFid;
3232 AFSFetchStatus updatedOldDirStatus;
3233 AFSFetchStatus updatedNewDirStatus;
3236 struct rx_connection * callp;
3237 cm_dirOp_t oldDirOp;
3240 cm_dirOp_t newDirOp;
3242 /* before starting the RPC, mark that we're changing the directory data,
3243 * so that someone who does a chmod on the dir will wait until our call
3244 * completes. We do this in vnode order so that we don't deadlock,
3245 * which makes the code a little verbose.
3247 if (oldDscp == newDscp) {
3248 /* check for identical names */
3249 if (strcmp(oldNamep, newNamep) == 0)
3250 return CM_ERROR_RENAME_IDENTICAL;
3253 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3254 lock_ObtainMutex(&oldDscp->mx);
3255 cm_dnlcRemove(oldDscp, oldNamep);
3256 cm_dnlcRemove(oldDscp, newNamep);
3257 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3258 CM_SCACHESYNC_STOREDATA);
3259 lock_ReleaseMutex(&oldDscp->mx);
3261 cm_EndDirOp(&oldDirOp);
3265 /* two distinct dir vnodes */
3267 if (oldDscp->fid.cell != newDscp->fid.cell ||
3268 oldDscp->fid.volume != newDscp->fid.volume)
3269 return CM_ERROR_CROSSDEVLINK;
3271 /* shouldn't happen that we have distinct vnodes for two
3272 * different files, but could due to deliberate attack, or
3273 * stale info. Avoid deadlocks and quit now.
3275 if (oldDscp->fid.vnode == newDscp->fid.vnode)
3276 return CM_ERROR_CROSSDEVLINK;
3278 if (oldDscp->fid.vnode < newDscp->fid.vnode) {
3279 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3280 lock_ObtainMutex(&oldDscp->mx);
3281 cm_dnlcRemove(oldDscp, oldNamep);
3282 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3283 CM_SCACHESYNC_STOREDATA);
3284 lock_ReleaseMutex(&oldDscp->mx);
3286 cm_EndDirOp(&oldDirOp);
3288 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3289 lock_ObtainMutex(&newDscp->mx);
3290 cm_dnlcRemove(newDscp, newNamep);
3291 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3292 CM_SCACHESYNC_STOREDATA);
3293 lock_ReleaseMutex(&newDscp->mx);
3295 cm_EndDirOp(&newDirOp);
3297 /* cleanup first one */
3298 lock_ObtainMutex(&oldDscp->mx);
3299 cm_SyncOpDone(oldDscp, NULL,
3300 CM_SCACHESYNC_STOREDATA);
3301 lock_ReleaseMutex(&oldDscp->mx);
3302 cm_EndDirOp(&oldDirOp);
3307 /* lock the new vnode entry first */
3308 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3309 lock_ObtainMutex(&newDscp->mx);
3310 cm_dnlcRemove(newDscp, newNamep);
3311 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3312 CM_SCACHESYNC_STOREDATA);
3313 lock_ReleaseMutex(&newDscp->mx);
3315 cm_EndDirOp(&newDirOp);
3317 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3318 lock_ObtainMutex(&oldDscp->mx);
3319 cm_dnlcRemove(oldDscp, oldNamep);
3320 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3321 CM_SCACHESYNC_STOREDATA);
3322 lock_ReleaseMutex(&oldDscp->mx);
3324 cm_EndDirOp(&oldDirOp);
3326 /* cleanup first one */
3327 lock_ObtainMutex(&newDscp->mx);
3328 cm_SyncOpDone(newDscp, NULL,
3329 CM_SCACHESYNC_STOREDATA);
3330 lock_ReleaseMutex(&newDscp->mx);
3331 cm_EndDirOp(&newDirOp);
3335 } /* two distinct vnodes */
3342 /* try the RPC now */
3343 osi_Log2(afsd_logp, "CALL Rename old scp 0x%p new scp 0x%p",
3346 code = cm_ConnFromFID(&oldDscp->fid, userp, reqp, &connp);
3350 oldDirAFSFid.Volume = oldDscp->fid.volume;
3351 oldDirAFSFid.Vnode = oldDscp->fid.vnode;
3352 oldDirAFSFid.Unique = oldDscp->fid.unique;
3353 newDirAFSFid.Volume = newDscp->fid.volume;
3354 newDirAFSFid.Vnode = newDscp->fid.vnode;
3355 newDirAFSFid.Unique = newDscp->fid.unique;
3357 callp = cm_GetRxConn(connp);
3358 code = RXAFS_Rename(callp, &oldDirAFSFid, oldNamep,
3359 &newDirAFSFid, newNamep,
3360 &updatedOldDirStatus, &updatedNewDirStatus,
3362 rx_PutConnection(callp);
3364 } while (cm_Analyze(connp, userp, reqp, &oldDscp->fid,
3365 &volSync, NULL, NULL, code));
3366 code = cm_MapRPCError(code, reqp);
3369 osi_Log1(afsd_logp, "CALL Rename FAILURE, code 0x%x", code);
3371 osi_Log0(afsd_logp, "CALL Rename SUCCESS");
3373 /* update the individual stat cache entries for the directories */
3375 lock_ObtainWrite(&oldDirOp.scp->dirlock);
3376 oldDirOp.lockType = CM_DIRLOCK_WRITE;
3378 lock_ObtainMutex(&oldDscp->mx);
3379 cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_STOREDATA);
3382 cm_MergeStatus(NULL, oldDscp, &updatedOldDirStatus, &volSync,
3383 userp, CM_MERGEFLAG_DIROP);
3384 lock_ReleaseMutex(&oldDscp->mx);
3387 if (cm_CheckDirOpForSingleChange(&oldDirOp)) {
3390 diropCode = cm_BPlusDirLookup(&oldDirOp, oldNamep, &fileFid);
3391 if (diropCode == CM_ERROR_INEXACT_MATCH)
3393 else if (diropCode == EINVAL)
3395 diropCode = cm_DirLookup(&oldDirOp, oldNamep, &fileFid);
3397 if (diropCode == 0) {
3399 diropCode = cm_DirCreateEntry(&oldDirOp, newNamep, &fileFid);
3401 cm_BPlusDirCreateEntry(&oldDirOp, newNamep, &fileFid);
3405 if (diropCode == 0) {
3406 diropCode = cm_DirDeleteEntry(&oldDirOp, oldNamep);
3408 cm_BPlusDirDeleteEntry(&oldDirOp, oldNamep);
3414 cm_EndDirOp(&oldDirOp);
3416 /* and update it for the new one, too, if necessary */
3419 lock_ObtainWrite(&newDirOp.scp->dirlock);
3420 newDirOp.lockType = CM_DIRLOCK_WRITE;
3422 lock_ObtainMutex(&newDscp->mx);
3423 cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_STOREDATA);
3425 cm_MergeStatus(NULL, newDscp, &updatedNewDirStatus, &volSync,
3426 userp, CM_MERGEFLAG_DIROP);
3427 lock_ReleaseMutex(&newDscp->mx);
3430 /* we only make the local change if we successfully made
3431 the change in the old directory AND there was only one
3432 change in the new directory */
3433 if (diropCode == 0 && cm_CheckDirOpForSingleChange(&newDirOp)) {
3434 cm_DirCreateEntry(&newDirOp, newNamep, &fileFid);
3436 cm_BPlusDirCreateEntry(&newDirOp, newNamep, &fileFid);
3440 cm_EndDirOp(&newDirOp);
3443 /* and return error code */
3447 /* Byte range locks:
3449 The OpenAFS Windows client has to fake byte range locks given no
3450 server side support for such locks. This is implemented as keyed