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) {
437 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
440 lock_ReleaseMutex(&scp->mx);
445 extern long cm_CheckNTOpenDone(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
446 cm_lock_data_t ** ldpp)
449 lock_ObtainMutex(&scp->mx);
450 cm_Unlock(scp, (*ldpp)->sLockType, (*ldpp)->LOffset, (*ldpp)->LLength,
451 (*ldpp)->key, userp, reqp);
452 lock_ReleaseMutex(&scp->mx);
459 * When CAP_NT_SMBS has been negotiated, deletion (of files or directories) is
460 * done in three steps:
461 * (1) open for deletion (NT_CREATE_AND_X)
462 * (2) set for deletion on close (NT_TRANSACTION2, SET_FILE_INFO)
464 * We must not do the RPC until step 3. But if we are going to return an error
465 * code (e.g. directory not empty), we must return it by step 2, otherwise most
466 * clients will not notice it. So we do a preliminary check. For deleting
467 * files, this is almost free, since we have already done the RPC to get the
468 * parent directory's status bits. But for deleting directories, we must do an
469 * additional RPC to get the directory's data to check if it is empty. Sigh.
471 long cm_CheckNTDelete(cm_scache_t *dscp, cm_scache_t *scp, cm_user_t *userp,
477 cm_dirEntry_t *dep = 0;
478 unsigned short *hashTable;
480 int BeyondPage = 0, HaveDot = 0, HaveDotDot = 0;
482 /* First check permissions */
483 lock_ObtainMutex(&scp->mx);
484 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_DELETE,
485 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
486 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
487 lock_ReleaseMutex(&scp->mx);
491 /* If deleting directory, must be empty */
493 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
496 thyper.HighPart = 0; thyper.LowPart = 0;
497 code = buf_Get(scp, &thyper, &bufferp);
501 lock_ObtainMutex(&bufferp->mx);
502 lock_ObtainMutex(&scp->mx);
504 code = cm_SyncOp(scp, bufferp, userp, reqp, 0,
505 CM_SCACHESYNC_NEEDCALLBACK
507 | CM_SCACHESYNC_BUFLOCKED);
511 if (cm_HaveBuffer(scp, bufferp, 1))
514 /* otherwise, load the buffer and try again */
515 lock_ReleaseMutex(&bufferp->mx);
516 code = cm_GetBuffer(scp, bufferp, NULL, userp, reqp);
517 lock_ReleaseMutex(&scp->mx);
518 lock_ObtainMutex(&bufferp->mx);
519 lock_ObtainMutex(&scp->mx);
520 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
525 /* We try to determine emptiness without looking beyond the first page,
526 * and without assuming "." and ".." are present and are on the first
527 * page (though these assumptions might, after all, be reasonable).
529 hashTable = (unsigned short *)(bufferp->datap + (32 * 5));
530 for (i=0; i<128; i++) {
531 idx = ntohs(hashTable[i]);
537 dep = (cm_dirEntry_t *)(bufferp->datap + (32 * idx));
538 if (strcmp(dep->name, ".") == 0)
540 else if (strcmp(dep->name, "..") == 0)
543 code = CM_ERROR_NOTEMPTY;
546 idx = ntohs(dep->next);
549 if (BeyondPage && HaveDot && HaveDotDot)
550 code = CM_ERROR_NOTEMPTY;
554 lock_ReleaseMutex(&bufferp->mx);
555 buf_Release(bufferp);
556 lock_ReleaseMutex(&scp->mx);
561 * Iterate through all entries in a directory.
562 * When the function funcp is called, the buffer is locked but the
563 * directory vnode is not.
565 * If the retscp parameter is not NULL, the parmp must be a
566 * cm_lookupSearch_t object.
568 long cm_ApplyDir(cm_scache_t *scp, cm_DirFuncp_t funcp, void *parmp,
569 osi_hyper_t *startOffsetp, cm_user_t *userp, cm_req_t *reqp,
570 cm_scache_t **retscp)
574 cm_dirEntry_t *dep = 0;
577 osi_hyper_t dirLength;
578 osi_hyper_t bufferOffset;
579 osi_hyper_t curOffset;
583 cm_pageHeader_t *pageHeaderp;
585 long nextEntryCookie;
586 int numDirChunks; /* # of 32 byte dir chunks in this entry */
588 /* get the directory size */
589 lock_ObtainMutex(&scp->mx);
590 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_LOOKUP,
591 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
592 lock_ReleaseMutex(&scp->mx);
596 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
597 return CM_ERROR_NOTDIR;
599 if (retscp) /* if this is a lookup call */
601 cm_lookupSearch_t* sp = parmp;
604 #ifdef AFS_FREELANCE_CLIENT
605 /* Freelance entries never end up in the DNLC because they
606 * do not have an associated cm_server_t
608 !(cm_freelanceEnabled &&
609 sp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
610 sp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
611 #else /* !AFS_FREELANCE_CLIENT */
616 int casefold = sp->caseFold;
617 sp->caseFold = 0; /* we have a strong preference for exact matches */
618 if ( *retscp = cm_dnlcLookup(scp, sp)) /* dnlc hit */
620 sp->caseFold = casefold;
623 sp->caseFold = casefold;
625 /* see if we can find it using the directory hash tables.
626 we can only do exact matches, since the hash is case
636 code = cm_BeginDirOp(scp, userp, reqp, CM_DIRLOCK_READ, &dirop);
640 code = cm_BPlusDirLookup(&dirop, sp->searchNamep, &sp->fid);
645 code = cm_DirLookup(&dirop, sp->searchNamep, &sp->fid);
653 sp->ExactFound = TRUE;
654 *retscp = NULL; /* force caller to call cm_GetSCache() */
659 if (sp->caseFold && code == CM_ERROR_INEXACT_MATCH) {
662 sp->ExactFound = FALSE;
663 *retscp = NULL; /* force caller to call cm_GetSCache() */
667 return CM_ERROR_BPLUS_NOMATCH;
675 * XXX We only get the length once. It might change when we drop the
678 dirLength = scp->length;
681 bufferOffset.LowPart = bufferOffset.HighPart = 0;
683 curOffset = *startOffsetp;
685 curOffset.HighPart = 0;
686 curOffset.LowPart = 0;
690 /* make sure that curOffset.LowPart doesn't point to the first
691 * 32 bytes in the 2nd through last dir page, and that it
692 * doesn't point at the first 13 32-byte chunks in the first
693 * dir page, since those are dir and page headers, and don't
694 * contain useful information.
696 temp = curOffset.LowPart & (2048-1);
697 if (curOffset.HighPart == 0 && curOffset.LowPart < 2048) {
698 /* we're in the first page */
699 if (temp < 13*32) temp = 13*32;
702 /* we're in a later dir page */
703 if (temp < 32) temp = 32;
706 /* make sure the low order 5 bits are zero */
709 /* now put temp bits back ito curOffset.LowPart */
710 curOffset.LowPart &= ~(2048-1);
711 curOffset.LowPart |= temp;
713 /* check if we've passed the dir's EOF */
714 if (LargeIntegerGreaterThanOrEqualTo(curOffset, dirLength))
717 /* see if we can use the bufferp we have now; compute in which
718 * page the current offset would be, and check whether that's
719 * the offset of the buffer we have. If not, get the buffer.
721 thyper.HighPart = curOffset.HighPart;
722 thyper.LowPart = curOffset.LowPart & ~(cm_data.buf_blockSize-1);
723 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
726 lock_ReleaseMutex(&bufferp->mx);
727 buf_Release(bufferp);
731 code = buf_Get(scp, &thyper, &bufferp);
733 /* if buf_Get() fails we do not have a buffer object to lock */
739 /* for the IFS version, we bulkstat the dirents because this
740 routine is used in place of smb_ReceiveCoreSearchDir. our
741 other option is to modify smb_ReceiveCoreSearchDir itself,
742 but this seems to be the proper use for cm_ApplyDir. */
743 lock_ObtainMutex(&scp->mx);
744 if ((scp->flags & CM_SCACHEFLAG_BULKSTATTING) == 0
745 && (scp->bulkStatProgress.QuadPart <= thyper.QuadPart))
747 scp->flags |= CM_SCACHEFLAG_BULKSTATTING;
748 code = cm_TryBulkStat(scp, &thyper, userp, reqp);
749 scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
750 scp->bulkStatProgress = thyper;
752 lock_ReleaseMutex(&scp->mx);
755 lock_ObtainMutex(&bufferp->mx);
756 bufferOffset = thyper;
758 /* now get the data in the cache */
760 lock_ObtainMutex(&scp->mx);
761 code = cm_SyncOp(scp, bufferp, userp, reqp,
763 CM_SCACHESYNC_NEEDCALLBACK
765 | CM_SCACHESYNC_BUFLOCKED);
767 lock_ReleaseMutex(&scp->mx);
770 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
772 if (cm_HaveBuffer(scp, bufferp, 1)) {
773 lock_ReleaseMutex(&scp->mx);
777 /* otherwise, load the buffer and try again */
778 lock_ReleaseMutex(&bufferp->mx);
779 code = cm_GetBuffer(scp, bufferp, NULL, userp,
781 lock_ReleaseMutex(&scp->mx);
782 lock_ObtainMutex(&bufferp->mx);
787 lock_ReleaseMutex(&bufferp->mx);
788 buf_Release(bufferp);
792 } /* if (wrong buffer) ... */
794 /* now we have the buffer containing the entry we're interested
795 * in; copy it out if it represents a non-deleted entry.
797 entryInDir = curOffset.LowPart & (2048-1);
798 entryInBuffer = curOffset.LowPart & (cm_data.buf_blockSize - 1);
800 /* page header will help tell us which entries are free. Page
801 * header can change more often than once per buffer, since
802 * AFS 3 dir page size may be less than (but not more than) a
803 * buffer package buffer.
805 /* only look intra-buffer */
806 temp = curOffset.LowPart & (cm_data.buf_blockSize - 1);
807 temp &= ~(2048 - 1); /* turn off intra-page bits */
808 pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
810 /* now determine which entry we're looking at in the page. If
811 * it is free (there's a free bitmap at the start of the dir),
812 * we should skip these 32 bytes.
814 slotInPage = (entryInDir & 0x7e0) >> 5;
815 if (!(pageHeaderp->freeBitmap[slotInPage>>3]
816 & (1 << (slotInPage & 0x7)))) {
817 /* this entry is free */
818 numDirChunks = 1; /* only skip this guy */
822 tp = bufferp->datap + entryInBuffer;
823 dep = (cm_dirEntry_t *) tp; /* now points to AFS3 dir entry */
825 /* while we're here, compute the next entry's location, too,
826 * since we'll need it when writing out the cookie into the
827 * dir listing stream.
829 numDirChunks = cm_NameEntries(dep->name, NULL);
831 /* compute the offset of the cookie representing the next entry */
832 nextEntryCookie = curOffset.LowPart
833 + (CM_DIR_CHUNKSIZE * numDirChunks);
835 if (dep->fid.vnode != 0) {
836 /* this is one of the entries to use: it is not deleted */
837 code = (*funcp)(scp, dep, parmp, &curOffset);
840 } /* if we're including this name */
843 /* and adjust curOffset to be where the new cookie is */
845 thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
846 curOffset = LargeIntegerAdd(thyper, curOffset);
847 } /* while copying data for dir listing */
849 /* release the mutex */
851 lock_ReleaseMutex(&bufferp->mx);
852 buf_Release(bufferp);
857 int cm_NoneUpper(char *s)
861 if (c >= 'A' && c <= 'Z')
866 int cm_NoneLower(char *s)
870 if (c >= 'a' && c <= 'z')
875 long cm_LookupSearchProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
878 cm_lookupSearch_t *sp;
883 sp = (cm_lookupSearch_t *) rockp;
885 matchName = dep->name;
887 match = cm_stricmp(matchName, sp->searchNamep);
889 match = strcmp(matchName, sp->searchNamep);
893 && !cm_Is8Dot3(dep->name)) {
894 matchName = shortName;
895 cm_Gen8Dot3Name(dep, shortName, NULL);
897 match = cm_stricmp(matchName, sp->searchNamep);
899 match = strcmp(matchName, sp->searchNamep);
909 if (!sp->caseFold || matchName == shortName) {
910 sp->fid.vnode = ntohl(dep->fid.vnode);
911 sp->fid.unique = ntohl(dep->fid.unique);
912 return CM_ERROR_STOPNOW;
916 * If we get here, we are doing a case-insensitive search, and we
917 * have found a match. Now we determine what kind of match it is:
918 * exact, lower-case, upper-case, or none of the above. This is done
919 * in order to choose among matches, if there are more than one.
922 /* Exact matches are the best. */
923 match = strcmp(matchName, sp->searchNamep);
926 sp->fid.vnode = ntohl(dep->fid.vnode);
927 sp->fid.unique = ntohl(dep->fid.unique);
928 return CM_ERROR_STOPNOW;
931 /* Lower-case matches are next. */
934 if (cm_NoneUpper(matchName)) {
939 /* Upper-case matches are next. */
942 if (cm_NoneLower(matchName)) {
947 /* General matches are last. */
953 sp->fid.vnode = ntohl(dep->fid.vnode);
954 sp->fid.unique = ntohl(dep->fid.unique);
958 /* read the contents of a mount point into the appropriate string.
959 * called with locked scp, and returns with locked scp.
961 long cm_ReadMountPoint(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
968 if (scp->mountPointStringp[0])
971 /* otherwise, we have to read it in */
972 lock_ReleaseMutex(&scp->mx);
974 thyper.LowPart = thyper.HighPart = 0;
975 code = buf_Get(scp, &thyper, &bufp);
977 lock_ObtainMutex(&scp->mx);
982 code = cm_SyncOp(scp, bufp, userp, reqp, 0,
983 CM_SCACHESYNC_READ | CM_SCACHESYNC_NEEDCALLBACK);
987 cm_SyncOpDone(scp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
989 if (cm_HaveBuffer(scp, bufp, 0))
992 /* otherwise load buffer */
993 code = cm_GetBuffer(scp, bufp, NULL, userp, reqp);
997 /* locked, has callback, has valid data in buffer */
998 if ((tlen = scp->length.LowPart) > MOUNTPOINTLEN - 1)
999 return CM_ERROR_TOOBIG;
1001 code = CM_ERROR_INVAL;
1005 /* someone else did the work while we were out */
1006 if (scp->mountPointStringp[0]) {
1011 /* otherwise, copy out the link */
1012 memcpy(scp->mountPointStringp, bufp->datap, tlen);
1014 /* now make it null-terminated. Note that the original contents of a
1015 * link that is a mount point is "#volname." where "." is there just to
1016 * be turned into a null. That is, we can trash the last char of the
1017 * link without damaging the vol name. This is a stupid convention,
1018 * but that's the protocol.
1020 scp->mountPointStringp[tlen-1] = 0;
1030 /* called with a locked scp and chases the mount point, yielding outScpp.
1031 * scp remains locked, just for simplicity of describing the interface.
1033 long cm_FollowMountPoint(cm_scache_t *scp, cm_scache_t *dscp, cm_user_t *userp,
1034 cm_req_t *reqp, cm_scache_t **outScpp)
1042 cm_volume_t *volp = NULL;
1049 if (scp->mountRootFid.cell != 0 && scp->mountRootGen >= cm_data.mountRootGen) {
1050 tfid = scp->mountRootFid;
1051 lock_ReleaseMutex(&scp->mx);
1052 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1053 lock_ObtainMutex(&scp->mx);
1057 /* parse the volume name */
1058 mpNamep = scp->mountPointStringp;
1060 return CM_ERROR_NOSUCHPATH;
1061 tlen = (int)strlen(scp->mountPointStringp);
1062 mtType = *scp->mountPointStringp;
1063 cellNamep = malloc(tlen);
1064 volNamep = malloc(tlen);
1066 cp = strrchr(mpNamep, ':');
1068 /* cellular mount point */
1069 memset(cellNamep, 0, tlen);
1070 strncpy(cellNamep, mpNamep+1, cp - mpNamep - 1);
1071 strcpy(volNamep, cp+1);
1072 /* now look up the cell */
1073 lock_ReleaseMutex(&scp->mx);
1074 cellp = cm_GetCell(cellNamep, CM_FLAG_CREATE);
1075 lock_ObtainMutex(&scp->mx);
1079 strcpy(volNamep, mpNamep+1);
1081 cellp = cm_FindCellByID(scp->fid.cell, 0);
1085 code = CM_ERROR_NOSUCHCELL;
1089 vnLength = strlen(volNamep);
1090 if (vnLength >= 8 && strcmp(volNamep + vnLength - 7, ".backup") == 0)
1091 targetType = BACKVOL;
1092 else if (vnLength >= 10
1093 && strcmp(volNamep + vnLength - 9, ".readonly") == 0)
1098 /* check for backups within backups */
1099 if (targetType == BACKVOL
1100 && (scp->flags & (CM_SCACHEFLAG_RO | CM_SCACHEFLAG_PURERO))
1101 == CM_SCACHEFLAG_RO) {
1102 code = CM_ERROR_NOSUCHVOLUME;
1106 /* now we need to get the volume */
1107 lock_ReleaseMutex(&scp->mx);
1108 if (cm_VolNameIsID(volNamep)) {
1109 code = cm_GetVolumeByID(cellp, atoi(volNamep), userp, reqp,
1110 CM_GETVOL_FLAG_CREATE, &volp);
1112 code = cm_GetVolumeByName(cellp, volNamep, userp, reqp,
1113 CM_GETVOL_FLAG_CREATE, &volp);
1115 lock_ObtainMutex(&scp->mx);
1118 afs_uint32 cell, volume;
1120 /* save the parent of the volume root for this is the
1121 * place where the volume is mounted and we must remember
1122 * this in the volume structure rather than just in the
1123 * scache entry lest the scache entry gets recycled
1126 lock_ObtainMutex(&volp->mx);
1127 volp->dotdotFid = dscp->fid;
1128 lock_ReleaseMutex(&volp->mx);
1130 cell = cellp->cellID;
1132 /* if the mt pt originates in a .backup volume (not a .readonly)
1133 * and FollowBackupPath is active, and if there is a .backup
1134 * volume for the target, then use the .backup of the target
1135 * instead of the read-write.
1137 if (cm_followBackupPath &&
1139 (dscp->flags & (CM_SCACHEFLAG_RO|CM_SCACHEFLAG_PURERO)) == CM_SCACHEFLAG_RO &&
1140 (targetType == RWVOL || targetType == ROVOL && volp->ro.ID == 0)
1142 targetType = BACKVOL;
1144 /* if the mt pt is in a read-only volume (not just a
1145 * backup), and if there is a read-only volume for the
1146 * target, and if this is a targetType '#' mount point, use
1147 * the read-only, otherwise use the one specified.
1149 else if (mtType == '#' && targetType == RWVOL &&
1150 (scp->flags & CM_SCACHEFLAG_PURERO) &&
1154 if (targetType == ROVOL)
1155 volume = volp->ro.ID;
1156 else if (targetType == BACKVOL)
1157 volume = volp->bk.ID;
1159 volume = volp->rw.ID;
1161 /* the rest of the fid is a magic number */
1162 cm_SetFid(&scp->mountRootFid, cell, volume, 1, 1);
1163 scp->mountRootGen = cm_data.mountRootGen;
1165 tfid = scp->mountRootFid;
1166 lock_ReleaseMutex(&scp->mx);
1167 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1168 lock_ObtainMutex(&scp->mx);
1179 long cm_LookupInternal(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1180 cm_req_t *reqp, cm_scache_t **outpScpp)
1183 int dnlcHit = 1; /* did we hit in the dnlc? yes, we did */
1184 cm_scache_t *tscp = NULL;
1185 cm_scache_t *mountedScp;
1186 cm_lookupSearch_t rock;
1189 memset(&rock, 0, sizeof(rock));
1191 if (dscp->fid.vnode == 1 && dscp->fid.unique == 1
1192 && strcmp(namep, "..") == 0) {
1193 if (dscp->dotdotFid.volume == 0)
1194 return CM_ERROR_NOSUCHVOLUME;
1195 rock.fid = dscp->dotdotFid;
1197 } else if (strcmp(namep, ".") == 0) {
1198 rock.fid = dscp->fid;
1202 if (flags & CM_FLAG_NOMOUNTCHASE) {
1203 /* In this case, we should go and call cm_Dir* functions
1204 directly since the following cm_ApplyDir() function will
1212 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ, &dirop);
1215 code = cm_BPlusDirLookup(&dirop, namep, &rock.fid);
1220 code = cm_DirLookup(&dirop, namep, &rock.fid);
1222 cm_EndDirOp(&dirop);
1232 if (code == CM_ERROR_INEXACT_MATCH && (flags & CM_FLAG_CASEFOLD)) {
1239 return CM_ERROR_BPLUS_NOMATCH;
1244 rock.fid.cell = dscp->fid.cell;
1245 rock.fid.volume = dscp->fid.volume;
1246 rock.searchNamep = namep;
1247 rock.caseFold = (flags & CM_FLAG_CASEFOLD);
1248 rock.hasTilde = ((strchr(namep, '~') != NULL) ? 1 : 0);
1250 /* If NOMOUNTCHASE, bypass DNLC by passing NULL scp pointer */
1251 code = cm_ApplyDir(dscp, cm_LookupSearchProc, &rock, NULL, userp, reqp,
1252 (flags & CM_FLAG_NOMOUNTCHASE) ? NULL : &tscp);
1254 /* code == 0 means we fell off the end of the dir, while stopnow means
1255 * that we stopped early, probably because we found the entry we're
1256 * looking for. Any other non-zero code is an error.
1258 if (code && code != CM_ERROR_STOPNOW) {
1259 /* if the cm_scache_t we are searching in is not a directory
1260 * we must return path not found because the error
1261 * is to describe the final component not an intermediary
1263 if (code == CM_ERROR_NOTDIR) {
1264 if (flags & CM_FLAG_CHECKPATH)
1265 return CM_ERROR_NOSUCHPATH;
1267 return CM_ERROR_NOSUCHFILE;
1272 getroot = (dscp==cm_data.rootSCachep) ;
1274 if (!cm_freelanceEnabled || !getroot) {
1275 if (flags & CM_FLAG_CHECKPATH)
1276 return CM_ERROR_NOSUCHPATH;
1278 return CM_ERROR_NOSUCHFILE;
1280 else if (!strchr(namep, '#') && !strchr(namep, '%') &&
1281 strcmp(namep, "srvsvc") && strcmp(namep, "wkssvc") &&
1282 strcmp(namep, "ipc$")) {
1283 /* nonexistent dir on freelance root, so add it */
1284 char fullname[200] = ".";
1287 osi_Log1(afsd_logp,"cm_Lookup adding mount for non-existent directory: %s",
1288 osi_LogSaveString(afsd_logp,namep));
1291 * There is an ugly behavior where a share name "foo" will be searched
1292 * for as "fo". If the searched for name differs by an already existing
1293 * symlink or mount point in the Freelance directory, do not add the
1294 * new value automatically.
1298 if (namep[0] == '.') {
1299 if (cm_GetCell_Gen(&namep[1], &fullname[1], CM_FLAG_CREATE)) {
1301 if (!cm_FreelanceMountPointExists(fullname, 0))
1302 code = cm_FreelanceAddMount(fullname, &fullname[1], "root.cell.", 1, &rock.fid);
1303 if ( stricmp(&namep[1], &fullname[1]) &&
1304 !cm_FreelanceMountPointExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0) &&
1305 !cm_FreelanceSymlinkExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0))
1306 code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1309 if (cm_GetCell_Gen(namep, fullname, CM_FLAG_CREATE)) {
1311 if (!cm_FreelanceMountPointExists(fullname, 0))
1312 code = cm_FreelanceAddMount(fullname, fullname, "root.cell.", 0, &rock.fid);
1313 if ( stricmp(namep, fullname) &&
1314 !cm_FreelanceMountPointExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0) &&
1315 !cm_FreelanceSymlinkExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0))
1316 code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1319 if (!found || code < 0) { /* add mount point failed, so give up */
1320 if (flags & CM_FLAG_CHECKPATH)
1321 return CM_ERROR_NOSUCHPATH;
1323 return CM_ERROR_NOSUCHFILE;
1325 tscp = NULL; /* to force call of cm_GetSCache */
1330 if ( !tscp ) /* we did not find it in the dnlc */
1333 code = cm_GetSCache(&rock.fid, &tscp, userp, reqp);
1337 /* tscp is now held */
1339 lock_ObtainMutex(&tscp->mx);
1340 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1341 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1343 lock_ReleaseMutex(&tscp->mx);
1344 cm_ReleaseSCache(tscp);
1347 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1348 /* tscp is now locked */
1350 if (!(flags & CM_FLAG_NOMOUNTCHASE)
1351 && tscp->fileType == CM_SCACHETYPE_MOUNTPOINT) {
1352 /* mount points are funny: they have a volume name to mount
1355 code = cm_ReadMountPoint(tscp, userp, reqp);
1357 code = cm_FollowMountPoint(tscp, dscp, userp, reqp,
1359 lock_ReleaseMutex(&tscp->mx);
1360 cm_ReleaseSCache(tscp);
1367 lock_ReleaseMutex(&tscp->mx);
1370 /* copy back pointer */
1373 /* insert scache in dnlc */
1374 if ( !dnlcHit && !(flags & CM_FLAG_NOMOUNTCHASE) && rock.ExactFound ) {
1375 /* lock the directory entry to prevent racing callback revokes */
1376 lock_ObtainMutex(&dscp->mx);
1377 if ( dscp->cbServerp != NULL && dscp->cbExpires > 0 )
1378 cm_dnlcEnter(dscp, namep, tscp);
1379 lock_ReleaseMutex(&dscp->mx);
1386 int cm_ExpandSysName(char *inp, char *outp, long outSize, unsigned int index)
1391 tp = strrchr(inp, '@');
1393 return 0; /* no @sys */
1395 if (strcmp(tp, "@sys") != 0)
1396 return 0; /* no @sys */
1398 /* caller just wants to know if this is a valid @sys type of name */
1402 if (index >= MAXNUMSYSNAMES)
1405 /* otherwise generate the properly expanded @sys name */
1406 prefixCount = (int)(tp - inp);
1408 strncpy(outp, inp, prefixCount); /* copy out "a." from "a.@sys" */
1409 outp[prefixCount] = 0; /* null terminate the "a." */
1410 strcat(outp, cm_sysNameList[index]);/* append i386_nt40 */
1414 long cm_EvaluateVolumeReference(char * namep, long flags, cm_user_t * userp,
1415 cm_req_t *reqp, cm_scache_t ** outpScpp)
1418 char cellName[CELL_MAXNAMELEN];
1419 char volumeName[VL_MAXNAMELEN];
1424 cm_cell_t * cellp = NULL;
1425 cm_volume_t * volp = NULL;
1429 int mountType = RWVOL;
1431 osi_Log1(afsd_logp, "cm_EvaluateVolumeReference for string [%s]",
1432 osi_LogSaveString(afsd_logp, namep));
1434 if (strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) != 0) {
1435 goto _exit_invalid_path;
1438 /* namep is assumed to look like the following:
1440 @vol:<cellname>%<volume>\0
1442 @vol:<cellname>#<volume>\0
1446 cp = namep + CM_PREFIX_VOL_CCH; /* cp points to cell name, hopefully */
1447 tp = strchr(cp, '%');
1449 tp = strchr(cp, '#');
1451 (len = tp - cp) == 0 ||
1452 len > CELL_MAXNAMELEN)
1453 goto _exit_invalid_path;
1454 strncpy(cellName, cp, len);
1455 cellName[len] = '\0';
1460 cp = tp+1; /* cp now points to volume, supposedly */
1461 strncpy(volumeName, cp, VL_MAXNAMELEN-1);
1462 volumeName[VL_MAXNAMELEN - 1] = 0;
1464 /* OK, now we have the cell and the volume */
1465 osi_Log2(afsd_logp, " Found cell [%s] and volume [%s]",
1466 osi_LogSaveString(afsd_logp, cellName),
1467 osi_LogSaveString(afsd_logp, volumeName));
1469 cellp = cm_GetCell(cellName, CM_FLAG_CREATE);
1470 if (cellp == NULL) {
1471 goto _exit_invalid_path;
1474 len = strlen(volumeName);
1475 if (len >= 8 && strcmp(volumeName + len - 7, ".backup") == 0)
1477 else if (len >= 10 &&
1478 strcmp(volumeName + len - 9, ".readonly") == 0)
1483 if (cm_VolNameIsID(volumeName)) {
1484 code = cm_GetVolumeByID(cellp, atoi(volumeName), userp, reqp,
1485 CM_GETVOL_FLAG_CREATE, &volp);
1487 code = cm_GetVolumeByName(cellp, volumeName, userp, reqp,
1488 CM_GETVOL_FLAG_CREATE, &volp);
1494 if (volType == BACKVOL)
1495 volume = volp->bk.ID;
1496 else if (volType == ROVOL ||
1497 (volType == RWVOL && mountType == ROVOL && volp->ro.ID != 0))
1498 volume = volp->ro.ID;
1500 volume = volp->rw.ID;
1502 cm_SetFid(&fid, cellp->cellID, volume, 1, 1);
1504 code = cm_GetSCache(&fid, outpScpp, userp, reqp);
1514 if (flags & CM_FLAG_CHECKPATH)
1515 return CM_ERROR_NOSUCHPATH;
1517 return CM_ERROR_NOSUCHFILE;
1520 #ifdef DEBUG_REFCOUNT
1521 long cm_LookupDbg(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1522 cm_req_t *reqp, cm_scache_t **outpScpp, char * file, long line)
1524 long cm_Lookup(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1525 cm_req_t *reqp, cm_scache_t **outpScpp)
1529 char tname[AFSPATHMAX];
1530 int sysNameIndex = 0;
1531 cm_scache_t *scp = NULL;
1533 #ifdef DEBUG_REFCOUNT
1534 afsi_log("%s:%d cm_Lookup dscp 0x%p ref %d", file, line, dscp, dscp->refCount, file, line);
1535 osi_Log2(afsd_logp, "cm_Lookup dscp 0x%p ref %d", dscp, dscp->refCount);
1538 if ( stricmp(namep,SMB_IOCTL_FILENAME_NOSLASH) == 0 ) {
1539 if (flags & CM_FLAG_CHECKPATH)
1540 return CM_ERROR_NOSUCHPATH;
1542 return CM_ERROR_NOSUCHFILE;
1545 if (dscp == cm_data.rootSCachep &&
1546 strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) == 0) {
1547 return cm_EvaluateVolumeReference(namep, flags, userp, reqp, outpScpp);
1550 if (cm_ExpandSysName(namep, NULL, 0, 0) > 0) {
1551 for ( sysNameIndex = 0; sysNameIndex < MAXNUMSYSNAMES; sysNameIndex++) {
1552 code = cm_ExpandSysName(namep, tname, sizeof(tname), sysNameIndex);
1554 code = cm_LookupInternal(dscp, tname, flags, userp, reqp, &scp);
1555 #ifdef DEBUG_REFCOUNT
1556 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);
1557 osi_Log3(afsd_logp, "cm_LookupInternal (1) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1565 cm_ReleaseSCache(scp);
1569 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1570 #ifdef DEBUG_REFCOUNT
1571 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);
1572 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1579 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1580 #ifdef DEBUG_REFCOUNT
1581 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);
1582 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1588 /* None of the possible sysName expansions could be found */
1589 if (flags & CM_FLAG_CHECKPATH)
1590 return CM_ERROR_NOSUCHPATH;
1592 return CM_ERROR_NOSUCHFILE;
1595 long cm_Unlink(cm_scache_t *dscp, char *namep, cm_user_t *userp, cm_req_t *reqp)
1601 AFSFetchStatus newDirStatus;
1603 struct rx_connection * callp;
1606 #ifdef AFS_FREELANCE_CLIENT
1607 if (cm_freelanceEnabled && dscp == cm_data.rootSCachep) {
1608 /* deleting a mount point from the root dir. */
1609 code = cm_FreelanceRemoveMount(namep);
1614 /* make sure we don't screw up the dir status during the merge */
1615 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
1617 lock_ObtainMutex(&dscp->mx);
1618 sflags = CM_SCACHESYNC_STOREDATA;
1619 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, sflags);
1620 lock_ReleaseMutex(&dscp->mx);
1622 cm_EndDirOp(&dirop);
1627 afsFid.Volume = dscp->fid.volume;
1628 afsFid.Vnode = dscp->fid.vnode;
1629 afsFid.Unique = dscp->fid.unique;
1631 osi_Log1(afsd_logp, "CALL RemoveFile scp 0x%p", dscp);
1633 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
1637 callp = cm_GetRxConn(connp);
1638 code = RXAFS_RemoveFile(callp, &afsFid, namep,
1639 &newDirStatus, &volSync);
1640 rx_PutConnection(callp);
1642 } while (cm_Analyze(connp, userp, reqp, &dscp->fid, &volSync, NULL, NULL, code));
1643 code = cm_MapRPCError(code, reqp);
1646 osi_Log1(afsd_logp, "CALL RemoveFile FAILURE, code 0x%x", code);
1648 osi_Log0(afsd_logp, "CALL RemoveFile SUCCESS");
1651 lock_ObtainWrite(&dirop.scp->dirlock);
1652 dirop.lockType = CM_DIRLOCK_WRITE;
1654 lock_ObtainMutex(&dscp->mx);
1655 cm_dnlcRemove(dscp, namep);
1656 cm_SyncOpDone(dscp, NULL, sflags);
1658 cm_MergeStatus(NULL, dscp, &newDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
1659 } else if (code == CM_ERROR_NOSUCHFILE) {
1660 /* windows would not have allowed the request to delete the file
1661 * if it did not believe the file existed. therefore, we must
1662 * have an inconsistent view of the world.
1664 dscp->cbServerp = NULL;
1666 lock_ReleaseMutex(&dscp->mx);
1668 if (code == 0 && cm_CheckDirOpForSingleChange(&dirop)) {
1669 cm_DirDeleteEntry(&dirop, namep);
1671 cm_BPlusDirDeleteEntry(&dirop, namep);
1674 cm_EndDirOp(&dirop);
1679 /* called with a locked vnode, and fills in the link info.
1680 * returns this the vnode still locked.
1682 long cm_HandleLink(cm_scache_t *linkScp, cm_user_t *userp, cm_req_t *reqp)
1689 lock_AssertMutex(&linkScp->mx);
1690 if (!linkScp->mountPointStringp[0]) {
1691 /* read the link data */
1692 lock_ReleaseMutex(&linkScp->mx);
1693 thyper.LowPart = thyper.HighPart = 0;
1694 code = buf_Get(linkScp, &thyper, &bufp);
1695 lock_ObtainMutex(&linkScp->mx);
1699 code = cm_SyncOp(linkScp, bufp, userp, reqp, 0,
1700 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1705 cm_SyncOpDone(linkScp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1707 if (cm_HaveBuffer(linkScp, bufp, 0))
1710 code = cm_GetBuffer(linkScp, bufp, NULL, userp, reqp);
1715 } /* while loop to get the data */
1717 /* now if we still have no link read in,
1718 * copy the data from the buffer */
1719 if ((temp = linkScp->length.LowPart) >= MOUNTPOINTLEN) {
1721 return CM_ERROR_TOOBIG;
1724 /* otherwise, it fits; make sure it is still null (could have
1725 * lost race with someone else referencing this link above),
1726 * and if so, copy in the data.
1728 if (!linkScp->mountPointStringp[0]) {
1729 strncpy(linkScp->mountPointStringp, bufp->datap, temp);
1730 linkScp->mountPointStringp[temp] = 0; /* null terminate */
1732 if ( !strnicmp(linkScp->mountPointStringp, "msdfs:", strlen("msdfs:")) )
1733 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1736 } /* don't have sym link contents cached */
1741 /* called with a held vnode and a path suffix, with the held vnode being a
1742 * symbolic link. Our goal is to generate a new path to interpret, and return
1743 * this new path in newSpaceBufferp. If the new vnode is relative to a dir
1744 * other than the directory containing the symbolic link, then the new root is
1745 * returned in *newRootScpp, otherwise a null is returned there.
1747 long cm_AssembleLink(cm_scache_t *linkScp, char *pathSuffixp,
1748 cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1749 cm_user_t *userp, cm_req_t *reqp)
1756 *newRootScpp = NULL;
1757 *newSpaceBufferp = NULL;
1759 lock_ObtainMutex(&linkScp->mx);
1760 code = cm_HandleLink(linkScp, userp, reqp);
1764 /* if we may overflow the buffer, bail out; buffer is signficantly
1765 * bigger than max path length, so we don't really have to worry about
1766 * being a little conservative here.
1768 if (strlen(linkScp->mountPointStringp) + strlen(pathSuffixp) + 2
1769 >= CM_UTILS_SPACESIZE)
1770 return CM_ERROR_TOOBIG;
1772 tsp = cm_GetSpace();
1773 linkp = linkScp->mountPointStringp;
1774 if (strncmp(linkp, cm_mountRoot, cm_mountRootLen) == 0) {
1775 if (strlen(linkp) > cm_mountRootLen)
1776 strcpy(tsp->data, linkp+cm_mountRootLen+1);
1779 *newRootScpp = cm_data.rootSCachep;
1780 cm_HoldSCache(cm_data.rootSCachep);
1781 } else if (linkp[0] == '\\' && linkp[1] == '\\') {
1782 if (!strnicmp(&linkp[2], cm_NetbiosName, (len = (long)strlen(cm_NetbiosName))))
1784 char * p = &linkp[len + 3];
1785 if (strnicmp(p, "all", 3) == 0)
1788 strcpy(tsp->data, p);
1789 for (p = tsp->data; *p; p++) {
1793 *newRootScpp = cm_data.rootSCachep;
1794 cm_HoldSCache(cm_data.rootSCachep);
1796 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1797 strcpy(tsp->data, linkp);
1798 code = CM_ERROR_PATH_NOT_COVERED;
1800 } else if ( linkScp->fileType == CM_SCACHETYPE_DFSLINK ||
1801 !strnicmp(linkp, "msdfs:", (len = (long)strlen("msdfs:"))) ) {
1802 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1803 strcpy(tsp->data, linkp);
1804 code = CM_ERROR_PATH_NOT_COVERED;
1805 } else if (*linkp == '\\' || *linkp == '/') {
1807 /* formerly, this was considered to be from the AFS root,
1808 * but this seems to create problems. instead, we will just
1809 * reject the link */
1810 strcpy(tsp->data, linkp+1);
1811 *newRootScpp = cm_data.rootSCachep;
1812 cm_HoldSCache(cm_data.rootSCachep);
1814 /* we still copy the link data into the response so that
1815 * the user can see what the link points to
1817 linkScp->fileType = CM_SCACHETYPE_INVALID;
1818 strcpy(tsp->data, linkp);
1819 code = CM_ERROR_NOSUCHPATH;
1822 /* a relative link */
1823 strcpy(tsp->data, linkp);
1825 if (pathSuffixp[0] != 0) { /* if suffix string is non-null */
1826 strcat(tsp->data, "\\");
1827 strcat(tsp->data, pathSuffixp);
1830 *newSpaceBufferp = tsp;
1834 if (code == CM_ERROR_PATH_NOT_COVERED && reqp->tidPathp && reqp->relPathp)
1835 cm_VolStatus_Notify_DFS_Mapping(linkScp, reqp->tidPathp, reqp->relPathp);
1839 lock_ReleaseMutex(&linkScp->mx);
1842 #ifdef DEBUG_REFCOUNT
1843 long cm_NameIDbg(cm_scache_t *rootSCachep, char *pathp, long flags,
1844 cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp,
1845 char * file, long line)
1847 long cm_NameI(cm_scache_t *rootSCachep, char *pathp, long flags,
1848 cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp)
1852 char *tp; /* ptr moving through input buffer */
1853 char tc; /* temp char */
1854 int haveComponent; /* has new component started? */
1855 char component[AFSPATHMAX]; /* this is the new component */
1856 char *cp; /* component name being assembled */
1857 cm_scache_t *tscp; /* current location in the hierarchy */
1858 cm_scache_t *nscp; /* next dude down */
1859 cm_scache_t *dirScp; /* last dir we searched */
1860 cm_scache_t *linkScp; /* new root for the symlink we just
1862 cm_space_t *psp; /* space for current path, if we've hit
1864 cm_space_t *tempsp; /* temp vbl */
1865 char *restp; /* rest of the pathname to interpret */
1866 int symlinkCount; /* count of # of symlinks traversed */
1867 int extraFlag; /* avoid chasing mt pts for dir cmd */
1868 int phase = 1; /* 1 = tidPathp, 2 = pathp */
1869 #define MAX_FID_COUNT 512
1870 cm_fid_t fids[MAX_FID_COUNT]; /* array of fids processed in this path walk */
1871 int fid_count = 0; /* number of fids processed in this path walk */
1874 #ifdef DEBUG_REFCOUNT
1875 afsi_log("%s:%d cm_NameI rootscp 0x%p ref %d", file, line, rootSCachep, rootSCachep->refCount);
1876 osi_Log4(afsd_logp,"cm_NameI rootscp 0x%p path %s tidpath %s flags 0x%x",
1877 rootSCachep, pathp ? pathp : "<NULL>", tidPathp ? tidPathp : "<NULL>",
1892 cm_HoldSCache(tscp);
1900 /* map Unix slashes into DOS ones so we can interpret Unix
1906 if (!haveComponent) {
1909 } else if (tc == 0) {
1923 /* we have a component here */
1924 if (tc == 0 || tc == '\\') {
1925 /* end of the component; we're at the last
1926 * component if tc == 0. However, if the last
1927 * is a symlink, we have more to do.
1929 *cp++ = 0; /* add null termination */
1931 if ((flags & CM_FLAG_DIRSEARCH) && tc == 0)
1932 extraFlag = CM_FLAG_NOMOUNTCHASE;
1933 code = cm_Lookup(tscp, component,
1935 userp, reqp, &nscp);
1938 if (!strcmp(component,"..") || !strcmp(component,".")) {
1940 * roll back the fid list until we find the fid
1941 * that matches where we are now. Its not necessarily
1942 * one or two fids because they might have been
1943 * symlinks or mount points or both that were crossed.
1945 for ( i=fid_count-1; i>=0; i--) {
1946 if (!cm_FidCmp(&nscp->fid, &fids[i]))
1951 /* add the new fid to the list */
1952 for ( i=0; i<fid_count; i++) {
1953 if ( !cm_FidCmp(&nscp->fid, &fids[i]) ) {
1954 code = CM_ERROR_TOO_MANY_SYMLINKS;
1955 cm_ReleaseSCache(nscp);
1960 if (i == fid_count && fid_count < MAX_FID_COUNT) {
1961 fids[fid_count++] = nscp->fid;
1967 cm_ReleaseSCache(tscp);
1969 cm_ReleaseSCache(dirScp);
1972 if ((code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) &&
1973 tscp->fileType == CM_SCACHETYPE_SYMLINK)
1975 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_NOSUCHPATH");
1976 return CM_ERROR_NOSUCHPATH;
1978 osi_Log1(afsd_logp,"cm_NameI code 0x%x", code);
1983 haveComponent = 0; /* component done */
1985 cm_ReleaseSCache(dirScp);
1986 dirScp = tscp; /* for some symlinks */
1987 tscp = nscp; /* already held */
1989 if (tc == 0 && !(flags & CM_FLAG_FOLLOW) && phase == 2) {
1992 cm_ReleaseSCache(dirScp);
1998 /* now, if tscp is a symlink, we should follow
1999 * it and assemble the path again.
2001 lock_ObtainMutex(&tscp->mx);
2002 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
2003 CM_SCACHESYNC_GETSTATUS
2004 | CM_SCACHESYNC_NEEDCALLBACK);
2006 lock_ReleaseMutex(&tscp->mx);
2007 cm_ReleaseSCache(tscp);
2010 cm_ReleaseSCache(dirScp);
2015 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2017 if (tscp->fileType == CM_SCACHETYPE_SYMLINK) {
2018 /* this is a symlink; assemble a new buffer */
2019 lock_ReleaseMutex(&tscp->mx);
2020 if (symlinkCount++ >= MAX_SYMLINK_COUNT) {
2021 cm_ReleaseSCache(tscp);
2024 cm_ReleaseSCache(dirScp);
2029 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_TOO_MANY_SYMLINKS");
2030 return CM_ERROR_TOO_MANY_SYMLINKS;
2036 code = cm_AssembleLink(tscp, restp, &linkScp, &tempsp, userp, reqp);
2038 if (code == 0 && linkScp != NULL) {
2039 if (linkScp == cm_data.rootSCachep)
2042 for ( i=0; i<fid_count; i++) {
2043 if ( !cm_FidCmp(&linkScp->fid, &fids[i]) ) {
2044 code = CM_ERROR_TOO_MANY_SYMLINKS;
2045 cm_ReleaseSCache(linkScp);
2051 if (i == fid_count && fid_count < MAX_FID_COUNT) {
2052 fids[fid_count++] = linkScp->fid;
2057 /* something went wrong */
2058 cm_ReleaseSCache(tscp);
2061 cm_ReleaseSCache(dirScp);
2067 /* otherwise, tempsp has the new path,
2068 * and linkScp is the new root from
2069 * which to interpret that path.
2070 * Continue with the namei processing,
2071 * also doing the bookkeeping for the
2072 * space allocation and tracking the
2073 * vnode reference counts.
2079 cm_ReleaseSCache(tscp);
2084 * now, if linkScp is null, that's
2085 * AssembleLink's way of telling us that
2086 * the sym link is relative to the dir
2087 * containing the link. We have a ref
2088 * to it in dirScp, and we hold it now
2089 * and reuse it as the new spot in the
2097 /* not a symlink, we may be done */
2098 lock_ReleaseMutex(&tscp->mx);
2106 cm_ReleaseSCache(dirScp);
2114 cm_ReleaseSCache(dirScp);
2117 } /* end of a component */
2120 } /* we have a component */
2121 } /* big while loop over all components */
2125 cm_ReleaseSCache(dirScp);
2131 cm_ReleaseSCache(tscp);
2133 #ifdef DEBUG_REFCOUNT
2134 afsi_log("%s:%d cm_NameI code 0x%x outScpp 0x%p ref %d", file, line, code, *outScpp, (*outScpp)->refCount);
2136 osi_Log2(afsd_logp,"cm_NameI code 0x%x outScpp 0x%p", code, *outScpp);
2140 /* called with a dir, and a vnode within the dir that happens to be a symlink.
2141 * We chase the link, and return a held pointer to the target, if it exists,
2142 * in *outScpp. If we succeed, we return 0, otherwise we return an error code
2143 * and do not hold or return a target vnode.
2145 * This is very similar to calling cm_NameI with the last component of a name,
2146 * which happens to be a symlink, except that we've already passed by the name.
2148 * This function is typically called by the directory listing functions, which
2149 * encounter symlinks but need to return the proper file length so programs
2150 * like "more" work properly when they make use of the attributes retrieved from
2153 * The input vnode should not be locked when this function is called.
2155 long cm_EvaluateSymLink(cm_scache_t *dscp, cm_scache_t *linkScp,
2156 cm_scache_t **outScpp, cm_user_t *userp, cm_req_t *reqp)
2160 cm_scache_t *newRootScp;
2162 osi_Log1(afsd_logp, "Evaluating symlink scp 0x%p", linkScp);
2164 code = cm_AssembleLink(linkScp, "", &newRootScp, &spacep, userp, reqp);
2168 /* now, if newRootScp is NULL, we're really being told that the symlink
2169 * is relative to the current directory (dscp).
2171 if (newRootScp == NULL) {
2173 cm_HoldSCache(dscp);
2176 code = cm_NameI(newRootScp, spacep->data,
2177 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW | CM_FLAG_DIRSEARCH,
2178 userp, NULL, reqp, outScpp);
2180 if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH)
2181 code = CM_ERROR_NOSUCHPATH;
2183 /* this stuff is allocated no matter what happened on the namei call,
2185 cm_FreeSpace(spacep);
2186 cm_ReleaseSCache(newRootScp);
2188 if (linkScp == *outScpp) {
2189 cm_ReleaseSCache(*outScpp);
2191 code = CM_ERROR_NOSUCHPATH;
2197 /* make this big enough so that one buffer of dir pages won't overflow. We'll
2198 * check anyway, but we want to minimize the chance that we have to leave stuff
2201 #define CM_BULKMAX (3 * AFSCBMAX)
2203 /* rock for bulk stat calls */
2204 typedef struct cm_bulkStat {
2205 osi_hyper_t bufOffset; /* only do it for things in this buffer page */
2207 /* info for the actual call */
2208 int counter; /* next free slot */
2209 AFSFid fids[CM_BULKMAX];
2210 AFSFetchStatus stats[CM_BULKMAX];
2211 AFSCallBack callbacks[CM_BULKMAX];
2214 /* for a given entry, make sure that it isn't in the stat cache, and then
2215 * add it to the list of file IDs to be obtained.
2217 * Don't bother adding it if we already have a vnode. Note that the dir
2218 * is locked, so we have to be careful checking the vnode we're thinking of
2219 * processing, to avoid deadlocks.
2221 long cm_TryBulkProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
2232 /* Don't overflow bsp. */
2233 if (bsp->counter >= CM_BULKMAX)
2234 return CM_ERROR_STOPNOW;
2236 thyper.LowPart = cm_data.buf_blockSize;
2237 thyper.HighPart = 0;
2238 thyper = LargeIntegerAdd(thyper, bsp->bufOffset);
2240 /* thyper is now the first byte past the end of the record we're
2241 * interested in, and bsp->bufOffset is the first byte of the record
2242 * we're interested in.
2243 * Skip data in the others.
2246 if (LargeIntegerLessThan(*offp, bsp->bufOffset))
2248 if (LargeIntegerGreaterThanOrEqualTo(*offp, thyper))
2249 return CM_ERROR_STOPNOW;
2250 if (strcmp(dep->name, ".") == 0 || strcmp(dep->name, "..") == 0)
2253 cm_SetFid(&tfid, scp->fid.cell, scp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
2254 tscp = cm_FindSCache(&tfid);
2256 if (lock_TryMutex(&tscp->mx)) {
2257 /* we have an entry that we can look at */
2258 if (!(tscp->flags & CM_SCACHEFLAG_EACCESS) && cm_HaveCallback(tscp)) {
2259 /* we have a callback on it. Don't bother
2260 * fetching this stat entry, since we're happy
2261 * with the info we have.
2263 lock_ReleaseMutex(&tscp->mx);
2264 cm_ReleaseSCache(tscp);
2267 lock_ReleaseMutex(&tscp->mx);
2269 cm_ReleaseSCache(tscp);
2272 #ifdef AFS_FREELANCE_CLIENT
2273 // yj: if this is a mountpoint under root.afs then we don't want it
2274 // to be bulkstat-ed, instead, we call getSCache directly and under
2275 // getSCache, it is handled specially.
2276 if ( cm_freelanceEnabled &&
2277 tfid.cell==AFS_FAKE_ROOT_CELL_ID &&
2278 tfid.volume==AFS_FAKE_ROOT_VOL_ID &&
2279 !(tfid.vnode==0x1 && tfid.unique==0x1) )
2281 osi_Log0(afsd_logp, "cm_TryBulkProc Freelance calls cm_SCache on root.afs mountpoint");
2282 return cm_GetSCache(&tfid, &tscp, NULL, NULL);
2284 #endif /* AFS_FREELANCE_CLIENT */
2287 bsp->fids[i].Volume = scp->fid.volume;
2288 bsp->fids[i].Vnode = tfid.vnode;
2289 bsp->fids[i].Unique = tfid.unique;
2293 /* called with a locked scp and a pointer to a buffer. Make bulk stat
2294 * calls on all undeleted files in the page of the directory specified.
2297 cm_TryBulkStat(cm_scache_t *dscp, osi_hyper_t *offsetp, cm_user_t *userp,
2301 cm_bulkStat_t bb; /* this is *BIG*, probably 16K or so;
2302 * watch for stack problems */
2303 AFSCBFids fidStruct;
2304 AFSBulkStats statStruct;
2306 AFSCBs callbackStruct;
2309 cm_callbackRequest_t cbReq;
2315 struct rx_connection * callp;
2316 int inlinebulk = 0; /* Did we use InlineBulkStatus RPC or not? */
2318 osi_Log1(afsd_logp, "cm_TryBulkStat dir 0x%p", dscp);
2320 /* should be on a buffer boundary */
2321 osi_assertx((offsetp->LowPart & (cm_data.buf_blockSize - 1)) == 0, "invalid offset");
2323 memset(&bb, 0, sizeof(bb));
2324 bb.bufOffset = *offsetp;
2326 lock_ReleaseMutex(&dscp->mx);
2327 /* first, assemble the file IDs we need to stat */
2328 code = cm_ApplyDir(dscp, cm_TryBulkProc, (void *) &bb, offsetp, userp, reqp, NULL);
2330 /* if we failed, bail out early */
2331 if (code && code != CM_ERROR_STOPNOW) {
2332 lock_ObtainMutex(&dscp->mx);
2336 /* otherwise, we may have one or more bulk stat's worth of stuff in bb;
2337 * make the calls to create the entries. Handle AFSCBMAX files at a
2341 while (filex < bb.counter) {
2342 filesThisCall = bb.counter - filex;
2343 if (filesThisCall > AFSCBMAX)
2344 filesThisCall = AFSCBMAX;
2346 fidStruct.AFSCBFids_len = filesThisCall;
2347 fidStruct.AFSCBFids_val = &bb.fids[filex];
2348 statStruct.AFSBulkStats_len = filesThisCall;
2349 statStruct.AFSBulkStats_val = &bb.stats[filex];
2350 callbackStruct.AFSCBs_len = filesThisCall;
2351 callbackStruct.AFSCBs_val = &bb.callbacks[filex];
2352 cm_StartCallbackGrantingCall(NULL, &cbReq);
2353 osi_Log1(afsd_logp, "CALL BulkStatus, %d entries", filesThisCall);
2355 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2359 callp = cm_GetRxConn(connp);
2360 if (!(connp->serverp->flags & CM_SERVERFLAG_NOINLINEBULK)) {
2361 code = RXAFS_InlineBulkStatus(callp, &fidStruct,
2362 &statStruct, &callbackStruct, &volSync);
2363 if (code == RXGEN_OPCODE) {
2364 cm_SetServerNoInlineBulk(connp->serverp, 0);
2370 code = RXAFS_BulkStatus(callp, &fidStruct,
2371 &statStruct, &callbackStruct, &volSync);
2373 rx_PutConnection(callp);
2375 } while (cm_Analyze(connp, userp, reqp, &dscp->fid,
2376 &volSync, NULL, &cbReq, code));
2377 code = cm_MapRPCError(code, reqp);
2379 osi_Log2(afsd_logp, "CALL %sBulkStatus FAILURE code 0x%x",
2380 inlinebulk ? "Inline" : "", code);
2382 osi_Log1(afsd_logp, "CALL %sBulkStatus SUCCESS", inlinebulk ? "Inline" : "");
2384 /* may as well quit on an error, since we're not going to do
2385 * much better on the next immediate call, either.
2388 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2392 /* otherwise, we should do the merges */
2393 for (i = 0; i<filesThisCall; i++) {
2395 cm_SetFid(&tfid, dscp->fid.cell, bb.fids[j].Volume, bb.fids[j].Vnode, bb.fids[j].Unique);
2396 code = cm_GetSCache(&tfid, &scp, userp, reqp);
2400 /* otherwise, if this entry has no callback info,
2403 lock_ObtainMutex(&scp->mx);
2404 /* now, we have to be extra paranoid on merging in this
2405 * information, since we didn't use cm_SyncOp before
2406 * starting the fetch to make sure that no bad races
2407 * were occurring. Specifically, we need to make sure
2408 * we don't obliterate any newer information in the
2409 * vnode than have here.
2411 * Right now, be pretty conservative: if there's a
2412 * callback or a pending call, skip it.
2414 if ((scp->cbServerp == NULL || (scp->flags & CM_SCACHEFLAG_EACCESS))
2416 (CM_SCACHEFLAG_FETCHING
2417 | CM_SCACHEFLAG_STORING
2418 | CM_SCACHEFLAG_SIZESTORING))) {
2419 cm_EndCallbackGrantingCall(scp, &cbReq,
2421 CM_CALLBACK_MAINTAINCOUNT);
2422 cm_MergeStatus(dscp, scp, &bb.stats[j], &volSync, userp, 0);
2424 lock_ReleaseMutex(&scp->mx);
2425 cm_ReleaseSCache(scp);
2426 } /* all files in the response */
2427 /* now tell it to drop the count,
2428 * after doing the vnode processing above */
2429 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2431 filex += filesThisCall;
2432 } /* while there are still more files to process */
2433 lock_ObtainMutex(&dscp->mx);
2435 /* If we did the InlineBulk RPC pull out the return code and log it */
2437 if ((&bb.stats[0])->errorCode) {
2438 osi_Log1(afsd_logp, "cm_TryBulkStat bulk stat error: %d",
2439 (&bb.stats[0])->errorCode);
2443 osi_Log0(afsd_logp, "END cm_TryBulkStat");
2447 void cm_StatusFromAttr(AFSStoreStatus *statusp, cm_scache_t *scp, cm_attr_t *attrp)
2451 /* initialize store back mask as inexpensive local variable */
2453 memset(statusp, 0, sizeof(AFSStoreStatus));
2455 /* copy out queued info from scache first, if scp passed in */
2457 if (scp->mask & CM_SCACHEMASK_CLIENTMODTIME) {
2458 statusp->ClientModTime = scp->clientModTime;
2459 mask |= AFS_SETMODTIME;
2460 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
2465 /* now add in our locally generated request */
2466 if (attrp->mask & CM_ATTRMASK_CLIENTMODTIME) {
2467 statusp->ClientModTime = attrp->clientModTime;
2468 mask |= AFS_SETMODTIME;
2470 if (attrp->mask & CM_ATTRMASK_UNIXMODEBITS) {
2471 statusp->UnixModeBits = attrp->unixModeBits;
2472 mask |= AFS_SETMODE;
2474 if (attrp->mask & CM_ATTRMASK_OWNER) {
2475 statusp->Owner = attrp->owner;
2476 mask |= AFS_SETOWNER;
2478 if (attrp->mask & CM_ATTRMASK_GROUP) {
2479 statusp->Group = attrp->group;
2480 mask |= AFS_SETGROUP;
2483 statusp->Mask = mask;
2486 /* set the file size, and make sure that all relevant buffers have been
2487 * truncated. Ensure that any partially truncated buffers have been zeroed
2488 * to the end of the buffer.
2490 long cm_SetLength(cm_scache_t *scp, osi_hyper_t *sizep, cm_user_t *userp,
2496 /* start by locking out buffer creation */
2497 lock_ObtainWrite(&scp->bufCreateLock);
2499 /* verify that this is a file, not a dir or a symlink */
2500 lock_ObtainMutex(&scp->mx);
2501 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
2502 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2505 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2507 if (scp->fileType != CM_SCACHETYPE_FILE) {
2508 code = CM_ERROR_ISDIR;
2513 if (LargeIntegerLessThan(*sizep, scp->length))
2518 lock_ReleaseMutex(&scp->mx);
2520 /* can't hold scp->mx lock here, since we may wait for a storeback to
2521 * finish if the buffer package is cleaning a buffer by storing it to
2525 buf_Truncate(scp, userp, reqp, sizep);
2527 /* now ensure that file length is short enough, and update truncPos */
2528 lock_ObtainMutex(&scp->mx);
2530 /* make sure we have a callback (so we have the right value for the
2531 * length), and wait for it to be safe to do a truncate.
2533 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_WRITE,
2534 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2535 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2537 /* If we only have 'i' bits, then we should still be able to set
2538 the size of a file we created. */
2539 if (code == CM_ERROR_NOACCESS && scp->creator == userp) {
2540 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
2541 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2542 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2548 if (LargeIntegerLessThan(*sizep, scp->length)) {
2549 /* a real truncation. If truncPos is not set yet, or is bigger
2550 * than where we're truncating the file, set truncPos to this
2555 if (!(scp->mask & CM_SCACHEMASK_TRUNCPOS)
2556 || LargeIntegerLessThan(*sizep, scp->length)) {
2558 scp->truncPos = *sizep;
2559 scp->mask |= CM_SCACHEMASK_TRUNCPOS;
2561 /* in either case, the new file size has been changed */
2562 scp->length = *sizep;
2563 scp->mask |= CM_SCACHEMASK_LENGTH;
2565 else if (LargeIntegerGreaterThan(*sizep, scp->length)) {
2566 /* really extending the file */
2567 scp->length = *sizep;
2568 scp->mask |= CM_SCACHEMASK_LENGTH;
2571 /* done successfully */
2574 cm_SyncOpDone(scp, NULL,
2575 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2576 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2579 lock_ReleaseMutex(&scp->mx);
2580 lock_ReleaseWrite(&scp->bufCreateLock);
2585 /* set the file size or other attributes (but not both at once) */
2586 long cm_SetAttr(cm_scache_t *scp, cm_attr_t *attrp, cm_user_t *userp,
2590 AFSFetchStatus afsOutStatus;
2594 AFSStoreStatus afsInStatus;
2595 struct rx_connection * callp;
2597 /* handle file length setting */
2598 if (attrp->mask & CM_ATTRMASK_LENGTH)
2599 return cm_SetLength(scp, &attrp->length, userp, reqp);
2601 lock_ObtainMutex(&scp->mx);
2602 /* otherwise, we have to make an RPC to get the status */
2603 code = cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STORESTATUS);
2605 lock_ReleaseMutex(&scp->mx);
2609 /* make the attr structure */
2610 cm_StatusFromAttr(&afsInStatus, scp, attrp);
2612 tfid.Volume = scp->fid.volume;
2613 tfid.Vnode = scp->fid.vnode;
2614 tfid.Unique = scp->fid.unique;
2615 lock_ReleaseMutex(&scp->mx);
2617 /* now make the RPC */
2618 osi_Log1(afsd_logp, "CALL StoreStatus scp 0x%p", scp);
2620 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2624 callp = cm_GetRxConn(connp);
2625 code = RXAFS_StoreStatus(callp, &tfid,
2626 &afsInStatus, &afsOutStatus, &volSync);
2627 rx_PutConnection(callp);
2629 } while (cm_Analyze(connp, userp, reqp,
2630 &scp->fid, &volSync, NULL, NULL, code));
2631 code = cm_MapRPCError(code, reqp);
2634 osi_Log1(afsd_logp, "CALL StoreStatus FAILURE, code 0x%x", code);
2636 osi_Log0(afsd_logp, "CALL StoreStatus SUCCESS");
2638 lock_ObtainMutex(&scp->mx);
2639 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STORESTATUS);
2641 cm_MergeStatus(NULL, scp, &afsOutStatus, &volSync, userp,
2642 CM_MERGEFLAG_FORCE|CM_MERGEFLAG_STOREDATA);
2644 /* if we're changing the mode bits, discard the ACL cache,
2645 * since we changed the mode bits.
2647 if (afsInStatus.Mask & AFS_SETMODE)
2648 cm_FreeAllACLEnts(scp);
2649 lock_ReleaseMutex(&scp->mx);
2653 long cm_Create(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2654 cm_scache_t **scpp, cm_user_t *userp, cm_req_t *reqp)
2659 cm_callbackRequest_t cbReq;
2662 cm_scache_t *scp = NULL;
2664 AFSStoreStatus inStatus;
2665 AFSFetchStatus updatedDirStatus;
2666 AFSFetchStatus newFileStatus;
2667 AFSCallBack newFileCallback;
2669 struct rx_connection * callp;
2672 /* can't create names with @sys in them; must expand it manually first.
2673 * return "invalid request" if they try.
2675 if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2676 return CM_ERROR_ATSYS;
2679 #ifdef AFS_FREELANCE_CLIENT
2680 /* Freelance root volume does not hold files */
2681 if (cm_freelanceEnabled &&
2682 dscp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
2683 dscp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
2685 return CM_ERROR_NOACCESS;
2687 #endif /* AFS_FREELANCE_CLIENT */
2689 /* before starting the RPC, mark that we're changing the file data, so
2690 * that someone who does a chmod will know to wait until our call
2693 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2694 lock_ObtainMutex(&dscp->mx);
2695 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2696 lock_ReleaseMutex(&dscp->mx);
2698 cm_StartCallbackGrantingCall(NULL, &cbReq);
2700 cm_EndDirOp(&dirop);
2707 cm_StatusFromAttr(&inStatus, NULL, attrp);
2709 /* try the RPC now */
2710 osi_Log1(afsd_logp, "CALL CreateFile scp 0x%p", dscp);
2712 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2716 dirAFSFid.Volume = dscp->fid.volume;
2717 dirAFSFid.Vnode = dscp->fid.vnode;
2718 dirAFSFid.Unique = dscp->fid.unique;
2720 callp = cm_GetRxConn(connp);
2721 code = RXAFS_CreateFile(connp->callp, &dirAFSFid, namep,
2722 &inStatus, &newAFSFid, &newFileStatus,
2723 &updatedDirStatus, &newFileCallback,
2725 rx_PutConnection(callp);
2727 } while (cm_Analyze(connp, userp, reqp,
2728 &dscp->fid, &volSync, NULL, &cbReq, code));
2729 code = cm_MapRPCError(code, reqp);
2732 osi_Log1(afsd_logp, "CALL CreateFile FAILURE, code 0x%x", code);
2734 osi_Log0(afsd_logp, "CALL CreateFile SUCCESS");
2737 lock_ObtainWrite(&dirop.scp->dirlock);
2738 dirop.lockType = CM_DIRLOCK_WRITE;
2740 lock_ObtainMutex(&dscp->mx);
2741 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2743 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2745 lock_ReleaseMutex(&dscp->mx);
2747 /* now try to create the file's entry, too, but be careful to
2748 * make sure that we don't merge in old info. Since we weren't locking
2749 * out any requests during the file's creation, we may have pretty old
2753 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
2754 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2756 lock_ObtainMutex(&scp->mx);
2757 scp->creator = userp; /* remember who created it */
2758 if (!cm_HaveCallback(scp)) {
2759 cm_MergeStatus(dscp, scp, &newFileStatus, &volSync,
2761 cm_EndCallbackGrantingCall(scp, &cbReq,
2762 &newFileCallback, 0);
2765 lock_ReleaseMutex(&scp->mx);
2770 /* make sure we end things properly */
2772 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2774 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2775 cm_DirCreateEntry(&dirop, namep, &newFid);
2777 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2780 cm_EndDirOp(&dirop);
2785 long cm_FSync(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
2789 code = buf_CleanVnode(scp, userp, reqp);
2791 lock_ObtainMutex(&scp->mx);
2793 if (scp->mask & (CM_SCACHEMASK_TRUNCPOS
2794 | CM_SCACHEMASK_CLIENTMODTIME
2795 | CM_SCACHEMASK_LENGTH))
2796 code = cm_StoreMini(scp, userp, reqp);
2798 if (scp->flags & (CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE)) {
2799 code = (scp->flags & CM_SCACHEFLAG_OVERQUOTA) ? CM_ERROR_QUOTA : CM_ERROR_SPACE;
2800 scp->flags &= ~(CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE);
2803 lock_ReleaseMutex(&scp->mx);
2808 long cm_MakeDir(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2809 cm_user_t *userp, cm_req_t *reqp)
2814 cm_callbackRequest_t cbReq;
2817 cm_scache_t *scp = NULL;
2819 AFSStoreStatus inStatus;
2820 AFSFetchStatus updatedDirStatus;
2821 AFSFetchStatus newDirStatus;
2822 AFSCallBack newDirCallback;
2824 struct rx_connection * callp;
2827 /* can't create names with @sys in them; must expand it manually first.
2828 * return "invalid request" if they try.
2830 if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2831 return CM_ERROR_ATSYS;
2834 #ifdef AFS_FREELANCE_CLIENT
2835 /* Freelance root volume does not hold subdirectories */
2836 if (cm_freelanceEnabled &&
2837 dscp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
2838 dscp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
2840 return CM_ERROR_NOACCESS;
2842 #endif /* AFS_FREELANCE_CLIENT */
2844 /* before starting the RPC, mark that we're changing the directory
2845 * data, so that someone who does a chmod on the dir will wait until
2846 * our call completes.
2848 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2849 lock_ObtainMutex(&dscp->mx);
2850 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2851 lock_ReleaseMutex(&dscp->mx);
2853 cm_StartCallbackGrantingCall(NULL, &cbReq);
2855 cm_EndDirOp(&dirop);
2862 cm_StatusFromAttr(&inStatus, NULL, attrp);
2864 /* try the RPC now */
2865 osi_Log1(afsd_logp, "CALL MakeDir scp 0x%p", dscp);
2867 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2871 dirAFSFid.Volume = dscp->fid.volume;
2872 dirAFSFid.Vnode = dscp->fid.vnode;
2873 dirAFSFid.Unique = dscp->fid.unique;
2875 callp = cm_GetRxConn(connp);
2876 code = RXAFS_MakeDir(connp->callp, &dirAFSFid, namep,
2877 &inStatus, &newAFSFid, &newDirStatus,
2878 &updatedDirStatus, &newDirCallback,
2880 rx_PutConnection(callp);
2882 } while (cm_Analyze(connp, userp, reqp,
2883 &dscp->fid, &volSync, NULL, &cbReq, code));
2884 code = cm_MapRPCError(code, reqp);
2887 osi_Log1(afsd_logp, "CALL MakeDir FAILURE, code 0x%x", code);
2889 osi_Log0(afsd_logp, "CALL MakeDir SUCCESS");
2892 lock_ObtainWrite(&dirop.scp->dirlock);
2893 dirop.lockType = CM_DIRLOCK_WRITE;
2895 lock_ObtainMutex(&dscp->mx);
2896 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2898 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2900 lock_ReleaseMutex(&dscp->mx);
2902 /* now try to create the new dir's entry, too, but be careful to
2903 * make sure that we don't merge in old info. Since we weren't locking
2904 * out any requests during the file's creation, we may have pretty old
2908 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
2909 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2911 lock_ObtainMutex(&scp->mx);
2912 if (!cm_HaveCallback(scp)) {
2913 cm_MergeStatus(dscp, scp, &newDirStatus, &volSync,
2915 cm_EndCallbackGrantingCall(scp, &cbReq,
2916 &newDirCallback, 0);
2919 lock_ReleaseMutex(&scp->mx);
2920 cm_ReleaseSCache(scp);
2924 /* make sure we end things properly */
2926 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2928 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2929 cm_DirCreateEntry(&dirop, namep, &newFid);
2931 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2934 cm_EndDirOp(&dirop);
2936 /* and return error code */
2940 long cm_Link(cm_scache_t *dscp, char *namep, cm_scache_t *sscp, long flags,
2941 cm_user_t *userp, cm_req_t *reqp)
2946 AFSFid existingAFSFid;
2947 AFSFetchStatus updatedDirStatus;
2948 AFSFetchStatus newLinkStatus;
2950 struct rx_connection * callp;
2953 if (dscp->fid.cell != sscp->fid.cell ||
2954 dscp->fid.volume != sscp->fid.volume) {
2955 return CM_ERROR_CROSSDEVLINK;
2958 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2959 lock_ObtainMutex(&dscp->mx);
2960 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2961 lock_ReleaseMutex(&dscp->mx);
2963 cm_EndDirOp(&dirop);
2968 /* try the RPC now */
2969 osi_Log1(afsd_logp, "CALL Link scp 0x%p", dscp);
2971 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2974 dirAFSFid.Volume = dscp->fid.volume;
2975 dirAFSFid.Vnode = dscp->fid.vnode;
2976 dirAFSFid.Unique = dscp->fid.unique;
2978 existingAFSFid.Volume = sscp->fid.volume;
2979 existingAFSFid.Vnode = sscp->fid.vnode;
2980 existingAFSFid.Unique = sscp->fid.unique;
2982 callp = cm_GetRxConn(connp);
2983 code = RXAFS_Link(callp, &dirAFSFid, namep, &existingAFSFid,
2984 &newLinkStatus, &updatedDirStatus, &volSync);
2985 rx_PutConnection(callp);
2986 osi_Log1(smb_logp," RXAFS_Link returns 0x%x", code);
2988 } while (cm_Analyze(connp, userp, reqp,
2989 &dscp->fid, &volSync, NULL, NULL, code));
2991 code = cm_MapRPCError(code, reqp);
2994 osi_Log1(afsd_logp, "CALL Link FAILURE, code 0x%x", code);
2996 osi_Log0(afsd_logp, "CALL Link SUCCESS");
2999 lock_ObtainWrite(&dirop.scp->dirlock);
3000 dirop.lockType = CM_DIRLOCK_WRITE;
3002 lock_ObtainMutex(&dscp->mx);
3003 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3005 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3007 lock_ReleaseMutex(&dscp->mx);
3010 if (cm_CheckDirOpForSingleChange(&dirop)) {
3011 cm_DirCreateEntry(&dirop, namep, &sscp->fid);
3013 cm_BPlusDirCreateEntry(&dirop, namep, &sscp->fid);
3017 cm_EndDirOp(&dirop);
3022 long cm_SymLink(cm_scache_t *dscp, char *namep, char *contentsp, long flags,
3023 cm_attr_t *attrp, cm_user_t *userp, cm_req_t *reqp)
3031 AFSStoreStatus inStatus;
3032 AFSFetchStatus updatedDirStatus;
3033 AFSFetchStatus newLinkStatus;
3035 struct rx_connection * callp;
3038 /* before starting the RPC, mark that we're changing the directory data,
3039 * so that someone who does a chmod on the dir will wait until our
3042 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3043 lock_ObtainMutex(&dscp->mx);
3044 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3045 lock_ReleaseMutex(&dscp->mx);
3047 cm_EndDirOp(&dirop);
3052 cm_StatusFromAttr(&inStatus, NULL, attrp);
3054 /* try the RPC now */
3055 osi_Log1(afsd_logp, "CALL Symlink scp 0x%p", dscp);
3057 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3061 dirAFSFid.Volume = dscp->fid.volume;
3062 dirAFSFid.Vnode = dscp->fid.vnode;
3063 dirAFSFid.Unique = dscp->fid.unique;
3065 callp = cm_GetRxConn(connp);
3066 code = RXAFS_Symlink(callp, &dirAFSFid, namep, contentsp,
3067 &inStatus, &newAFSFid, &newLinkStatus,
3068 &updatedDirStatus, &volSync);
3069 rx_PutConnection(callp);
3071 } while (cm_Analyze(connp, userp, reqp,
3072 &dscp->fid, &volSync, NULL, NULL, code));
3073 code = cm_MapRPCError(code, reqp);
3076 osi_Log1(afsd_logp, "CALL Symlink FAILURE, code 0x%x", code);
3078 osi_Log0(afsd_logp, "CALL Symlink SUCCESS");
3081 lock_ObtainWrite(&dirop.scp->dirlock);
3082 dirop.lockType = CM_DIRLOCK_WRITE;
3084 lock_ObtainMutex(&dscp->mx);
3085 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3087 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3089 lock_ReleaseMutex(&dscp->mx);
3092 if (cm_CheckDirOpForSingleChange(&dirop)) {
3093 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3095 cm_DirCreateEntry(&dirop, namep, &newFid);
3097 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
3101 cm_EndDirOp(&dirop);
3103 /* now try to create the new dir's entry, too, but be careful to
3104 * make sure that we don't merge in old info. Since we weren't locking
3105 * out any requests during the file's creation, we may have pretty old
3109 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3110 code = cm_GetSCache(&newFid, &scp, userp, reqp);
3112 lock_ObtainMutex(&scp->mx);
3113 if (!cm_HaveCallback(scp)) {
3114 cm_MergeStatus(dscp, scp, &newLinkStatus, &volSync,
3117 lock_ReleaseMutex(&scp->mx);
3118 cm_ReleaseSCache(scp);
3122 /* and return error code */
3126 long cm_RemoveDir(cm_scache_t *dscp, char *namep, cm_user_t *userp,
3133 AFSFetchStatus updatedDirStatus;
3135 struct rx_connection * callp;
3138 /* before starting the RPC, mark that we're changing the directory data,
3139 * so that someone who does a chmod on the dir will wait until our
3142 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3143 lock_ObtainMutex(&dscp->mx);
3144 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3145 lock_ReleaseMutex(&dscp->mx);
3147 cm_EndDirOp(&dirop);
3152 /* try the RPC now */
3153 osi_Log1(afsd_logp, "CALL RemoveDir scp 0x%p", dscp);
3155 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3159 dirAFSFid.Volume = dscp->fid.volume;
3160 dirAFSFid.Vnode = dscp->fid.vnode;
3161 dirAFSFid.Unique = dscp->fid.unique;
3163 callp = cm_GetRxConn(connp);
3164 code = RXAFS_RemoveDir(callp, &dirAFSFid, namep,
3165 &updatedDirStatus, &volSync);
3166 rx_PutConnection(callp);
3168 } while (cm_Analyze(connp, userp, reqp,
3169 &dscp->fid, &volSync, NULL, NULL, code));
3170 code = cm_MapRPCErrorRmdir(code, reqp);
3173 osi_Log1(afsd_logp, "CALL RemoveDir FAILURE, code 0x%x", code);
3175 osi_Log0(afsd_logp, "CALL RemoveDir SUCCESS");
3178 lock_ObtainWrite(&dirop.scp->dirlock);
3179 dirop.lockType = CM_DIRLOCK_WRITE;
3181 lock_ObtainMutex(&dscp->mx);
3182 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3184 cm_dnlcRemove(dscp, namep);
3185 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3187 lock_ReleaseMutex(&dscp->mx);
3190 if (cm_CheckDirOpForSingleChange(&dirop)) {
3191 cm_DirDeleteEntry(&dirop, namep);
3193 cm_BPlusDirDeleteEntry(&dirop, namep);
3197 cm_EndDirOp(&dirop);
3199 /* and return error code */
3203 long cm_Open(cm_scache_t *scp, int type, cm_user_t *userp)
3205 /* grab mutex on contents */
3206 lock_ObtainMutex(&scp->mx);
3208 /* reset the prefetch info */
3209 scp->prefetch.base.LowPart = 0; /* base */
3210 scp->prefetch.base.HighPart = 0;
3211 scp->prefetch.end.LowPart = 0; /* and end */
3212 scp->prefetch.end.HighPart = 0;
3214 /* release mutex on contents */
3215 lock_ReleaseMutex(&scp->mx);
3221 long cm_Rename(cm_scache_t *oldDscp, char *oldNamep, cm_scache_t *newDscp,
3222 char *newNamep, cm_user_t *userp, cm_req_t *reqp)
3226 AFSFid oldDirAFSFid;
3227 AFSFid newDirAFSFid;
3229 AFSFetchStatus updatedOldDirStatus;
3230 AFSFetchStatus updatedNewDirStatus;
3233 struct rx_connection * callp;
3234 cm_dirOp_t oldDirOp;
3237 cm_dirOp_t newDirOp;
3239 /* before starting the RPC, mark that we're changing the directory data,
3240 * so that someone who does a chmod on the dir will wait until our call
3241 * completes. We do this in vnode order so that we don't deadlock,
3242 * which makes the code a little verbose.
3244 if (oldDscp == newDscp) {
3245 /* check for identical names */
3246 if (strcmp(oldNamep, newNamep) == 0)
3247 return CM_ERROR_RENAME_IDENTICAL;
3250 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3251 lock_ObtainMutex(&oldDscp->mx);
3252 cm_dnlcRemove(oldDscp, oldNamep);
3253 cm_dnlcRemove(oldDscp, newNamep);
3254 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3255 CM_SCACHESYNC_STOREDATA);
3256 lock_ReleaseMutex(&oldDscp->mx);
3258 cm_EndDirOp(&oldDirOp);
3262 /* two distinct dir vnodes */
3264 if (oldDscp->fid.cell != newDscp->fid.cell ||
3265 oldDscp->fid.volume != newDscp->fid.volume)
3266 return CM_ERROR_CROSSDEVLINK;
3268 /* shouldn't happen that we have distinct vnodes for two
3269 * different files, but could due to deliberate attack, or
3270 * stale info. Avoid deadlocks and quit now.
3272 if (oldDscp->fid.vnode == newDscp->fid.vnode)
3273 return CM_ERROR_CROSSDEVLINK;
3275 if (oldDscp->fid.vnode < newDscp->fid.vnode) {
3276 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3277 lock_ObtainMutex(&oldDscp->mx);
3278 cm_dnlcRemove(oldDscp, oldNamep);
3279 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3280 CM_SCACHESYNC_STOREDATA);
3281 lock_ReleaseMutex(&oldDscp->mx);
3283 cm_EndDirOp(&oldDirOp);
3285 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3286 lock_ObtainMutex(&newDscp->mx);
3287 cm_dnlcRemove(newDscp, newNamep);
3288 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3289 CM_SCACHESYNC_STOREDATA);
3290 lock_ReleaseMutex(&newDscp->mx);
3292 cm_EndDirOp(&newDirOp);
3294 /* cleanup first one */
3295 lock_ObtainMutex(&oldDscp->mx);
3296 cm_SyncOpDone(oldDscp, NULL,
3297 CM_SCACHESYNC_STOREDATA);
3298 lock_ReleaseMutex(&oldDscp->mx);
3299 cm_EndDirOp(&oldDirOp);
3304 /* lock the new vnode entry first */
3305 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3306 lock_ObtainMutex(&newDscp->mx);
3307 cm_dnlcRemove(newDscp, newNamep);
3308 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3309 CM_SCACHESYNC_STOREDATA);
3310 lock_ReleaseMutex(&newDscp->mx);
3312 cm_EndDirOp(&newDirOp);
3314 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3315 lock_ObtainMutex(&oldDscp->mx);
3316 cm_dnlcRemove(oldDscp, oldNamep);
3317 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3318 CM_SCACHESYNC_STOREDATA);
3319 lock_ReleaseMutex(&oldDscp->mx);
3321 cm_EndDirOp(&oldDirOp);
3323 /* cleanup first one */
3324 lock_ObtainMutex(&newDscp->mx);
3325 cm_SyncOpDone(newDscp, NULL,
3326 CM_SCACHESYNC_STOREDATA);
3327 lock_ReleaseMutex(&newDscp->mx);
3328 cm_EndDirOp(&newDirOp);
3332 } /* two distinct vnodes */
3339 /* try the RPC now */
3340 osi_Log2(afsd_logp, "CALL Rename old scp 0x%p new scp 0x%p",
3343 code = cm_ConnFromFID(&oldDscp->fid, userp, reqp, &connp);
3347 oldDirAFSFid.Volume = oldDscp->fid.volume;
3348 oldDirAFSFid.Vnode = oldDscp->fid.vnode;
3349 oldDirAFSFid.Unique = oldDscp->fid.unique;
3350 newDirAFSFid.Volume = newDscp->fid.volume;
3351 newDirAFSFid.Vnode = newDscp->fid.vnode;
3352 newDirAFSFid.Unique = newDscp->fid.unique;
3354 callp = cm_GetRxConn(connp);
3355 code = RXAFS_Rename(callp, &oldDirAFSFid, oldNamep,
3356 &newDirAFSFid, newNamep,
3357 &updatedOldDirStatus, &updatedNewDirStatus,
3359 rx_PutConnection(callp);
3361 } while (cm_Analyze(connp, userp, reqp, &oldDscp->fid,
3362 &volSync, NULL, NULL, code));
3363 code = cm_MapRPCError(code, reqp);
3366 osi_Log1(afsd_logp, "CALL Rename FAILURE, code 0x%x", code);
3368 osi_Log0(afsd_logp, "CALL Rename SUCCESS");
3370 /* update the individual stat cache entries for the directories */
3372 lock_ObtainWrite(&oldDirOp.scp->dirlock);
3373 oldDirOp.lockType = CM_DIRLOCK_WRITE;
3375 lock_ObtainMutex(&oldDscp->mx);
3376 cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_STOREDATA);
3379 cm_MergeStatus(NULL, oldDscp, &updatedOldDirStatus, &volSync,
3380 userp, CM_MERGEFLAG_DIROP);
3381 lock_ReleaseMutex(&oldDscp->mx);
3384 if (cm_CheckDirOpForSingleChange(&oldDirOp)) {
3387 diropCode = cm_BPlusDirLookup(&oldDirOp, oldNamep, &fileFid);
3388 if (diropCode == CM_ERROR_INEXACT_MATCH)
3390 else if (diropCode == EINVAL)
3392 diropCode = cm_DirLookup(&oldDirOp, oldNamep, &fileFid);
3394 if (diropCode == 0) {
3396 diropCode = cm_DirCreateEntry(&oldDirOp, newNamep, &fileFid);
3398 cm_BPlusDirCreateEntry(&oldDirOp, newNamep, &fileFid);
3402 if (diropCode == 0) {
3403 diropCode = cm_DirDeleteEntry(&oldDirOp, oldNamep);
3405 cm_BPlusDirDeleteEntry(&oldDirOp, oldNamep);
3411 cm_EndDirOp(&oldDirOp);
3413 /* and update it for the new one, too, if necessary */
3416 lock_ObtainWrite(&newDirOp.scp->dirlock);
3417 newDirOp.lockType = CM_DIRLOCK_WRITE;
3419 lock_ObtainMutex(&newDscp->mx);
3420 cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_STOREDATA);
3422 cm_MergeStatus(NULL, newDscp, &updatedNewDirStatus, &volSync,
3423 userp, CM_MERGEFLAG_DIROP);
3424 lock_ReleaseMutex(&newDscp->mx);
3427 /* we only make the local change if we successfully made
3428 the change in the old directory AND there was only one
3429 change in the new directory */
3430 if (diropCode == 0 && cm_CheckDirOpForSingleChange(&newDirOp)) {
3431 cm_DirCreateEntry(&newDirOp, newNamep, &fileFid);
3433 cm_BPlusDirCreateEntry(&newDirOp, newNamep, &fileFid);
3437 cm_EndDirOp(&newDirOp);
3440 /* and return error code */
3444 /* Byte range locks:
3446 The OpenAFS Windows client has to fake byte range locks given no
3447 server side support for such locks. This is implemented as keyed
3448 byte range locks on the cache manager.
3450 Keyed byte range locks: