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 lock_ObtainRead(&scp->bufCreateLock);
498 code = buf_Get(scp, &thyper, &bufferp);
499 lock_ReleaseRead(&scp->bufCreateLock);
503 lock_ObtainMutex(&bufferp->mx);
504 lock_ObtainMutex(&scp->mx);
506 code = cm_SyncOp(scp, bufferp, userp, reqp, 0,
507 CM_SCACHESYNC_NEEDCALLBACK
509 | CM_SCACHESYNC_BUFLOCKED);
513 if (cm_HaveBuffer(scp, bufferp, 1))
516 /* otherwise, load the buffer and try again */
517 lock_ReleaseMutex(&bufferp->mx);
518 code = cm_GetBuffer(scp, bufferp, NULL, userp, reqp);
519 lock_ReleaseMutex(&scp->mx);
520 lock_ObtainMutex(&bufferp->mx);
521 lock_ObtainMutex(&scp->mx);
522 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
527 /* We try to determine emptiness without looking beyond the first page,
528 * and without assuming "." and ".." are present and are on the first
529 * page (though these assumptions might, after all, be reasonable).
531 hashTable = (unsigned short *)(bufferp->datap + (32 * 5));
532 for (i=0; i<128; i++) {
533 idx = ntohs(hashTable[i]);
539 dep = (cm_dirEntry_t *)(bufferp->datap + (32 * idx));
540 if (strcmp(dep->name, ".") == 0)
542 else if (strcmp(dep->name, "..") == 0)
545 code = CM_ERROR_NOTEMPTY;
548 idx = ntohs(dep->next);
551 if (BeyondPage && HaveDot && HaveDotDot)
552 code = CM_ERROR_NOTEMPTY;
556 lock_ReleaseMutex(&bufferp->mx);
557 buf_Release(bufferp);
558 lock_ReleaseMutex(&scp->mx);
563 * Iterate through all entries in a directory.
564 * When the function funcp is called, the buffer is locked but the
565 * directory vnode is not.
567 * If the retscp parameter is not NULL, the parmp must be a
568 * cm_lookupSearch_t object.
570 long cm_ApplyDir(cm_scache_t *scp, cm_DirFuncp_t funcp, void *parmp,
571 osi_hyper_t *startOffsetp, cm_user_t *userp, cm_req_t *reqp,
572 cm_scache_t **retscp)
576 cm_dirEntry_t *dep = 0;
579 osi_hyper_t dirLength;
580 osi_hyper_t bufferOffset;
581 osi_hyper_t curOffset;
585 cm_pageHeader_t *pageHeaderp;
587 long nextEntryCookie;
588 int numDirChunks; /* # of 32 byte dir chunks in this entry */
590 /* get the directory size */
591 lock_ObtainMutex(&scp->mx);
592 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_LOOKUP,
593 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
594 lock_ReleaseMutex(&scp->mx);
598 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
599 return CM_ERROR_NOTDIR;
601 if (retscp) /* if this is a lookup call */
603 cm_lookupSearch_t* sp = parmp;
606 #ifdef AFS_FREELANCE_CLIENT
607 /* Freelance entries never end up in the DNLC because they
608 * do not have an associated cm_server_t
610 !(cm_freelanceEnabled &&
611 sp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
612 sp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
613 #else /* !AFS_FREELANCE_CLIENT */
618 int casefold = sp->caseFold;
619 sp->caseFold = 0; /* we have a strong preference for exact matches */
620 if ( *retscp = cm_dnlcLookup(scp, sp)) /* dnlc hit */
622 sp->caseFold = casefold;
625 sp->caseFold = casefold;
627 /* see if we can find it using the directory hash tables.
628 we can only do exact matches, since the hash is case
638 code = cm_BeginDirOp(scp, userp, reqp, CM_DIRLOCK_READ, &dirop);
642 code = cm_BPlusDirLookup(&dirop, sp->searchNamep, &sp->fid);
647 code = cm_DirLookup(&dirop, sp->searchNamep, &sp->fid);
655 sp->ExactFound = TRUE;
656 *retscp = NULL; /* force caller to call cm_GetSCache() */
661 if (sp->caseFold && code == CM_ERROR_INEXACT_MATCH) {
664 sp->ExactFound = FALSE;
665 *retscp = NULL; /* force caller to call cm_GetSCache() */
669 return CM_ERROR_BPLUS_NOMATCH;
677 * XXX We only get the length once. It might change when we drop the
680 dirLength = scp->length;
683 bufferOffset.LowPart = bufferOffset.HighPart = 0;
685 curOffset = *startOffsetp;
687 curOffset.HighPart = 0;
688 curOffset.LowPart = 0;
692 /* make sure that curOffset.LowPart doesn't point to the first
693 * 32 bytes in the 2nd through last dir page, and that it
694 * doesn't point at the first 13 32-byte chunks in the first
695 * dir page, since those are dir and page headers, and don't
696 * contain useful information.
698 temp = curOffset.LowPart & (2048-1);
699 if (curOffset.HighPart == 0 && curOffset.LowPart < 2048) {
700 /* we're in the first page */
701 if (temp < 13*32) temp = 13*32;
704 /* we're in a later dir page */
705 if (temp < 32) temp = 32;
708 /* make sure the low order 5 bits are zero */
711 /* now put temp bits back ito curOffset.LowPart */
712 curOffset.LowPart &= ~(2048-1);
713 curOffset.LowPart |= temp;
715 /* check if we've passed the dir's EOF */
716 if (LargeIntegerGreaterThanOrEqualTo(curOffset, dirLength))
719 /* see if we can use the bufferp we have now; compute in which
720 * page the current offset would be, and check whether that's
721 * the offset of the buffer we have. If not, get the buffer.
723 thyper.HighPart = curOffset.HighPart;
724 thyper.LowPart = curOffset.LowPart & ~(cm_data.buf_blockSize-1);
725 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
728 lock_ReleaseMutex(&bufferp->mx);
729 buf_Release(bufferp);
733 lock_ObtainRead(&scp->bufCreateLock);
734 code = buf_Get(scp, &thyper, &bufferp);
735 lock_ReleaseRead(&scp->bufCreateLock);
737 /* if buf_Get() fails we do not have a buffer object to lock */
743 /* for the IFS version, we bulkstat the dirents because this
744 routine is used in place of smb_ReceiveCoreSearchDir. our
745 other option is to modify smb_ReceiveCoreSearchDir itself,
746 but this seems to be the proper use for cm_ApplyDir. */
747 lock_ObtainMutex(&scp->mx);
748 if ((scp->flags & CM_SCACHEFLAG_BULKSTATTING) == 0
749 && (scp->bulkStatProgress.QuadPart <= thyper.QuadPart))
751 scp->flags |= CM_SCACHEFLAG_BULKSTATTING;
752 code = cm_TryBulkStat(scp, &thyper, userp, reqp);
753 scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
754 scp->bulkStatProgress = thyper;
756 lock_ReleaseMutex(&scp->mx);
759 lock_ObtainMutex(&bufferp->mx);
760 bufferOffset = thyper;
762 /* now get the data in the cache */
764 lock_ObtainMutex(&scp->mx);
765 code = cm_SyncOp(scp, bufferp, userp, reqp,
767 CM_SCACHESYNC_NEEDCALLBACK
769 | CM_SCACHESYNC_BUFLOCKED);
771 lock_ReleaseMutex(&scp->mx);
774 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
776 if (cm_HaveBuffer(scp, bufferp, 1)) {
777 lock_ReleaseMutex(&scp->mx);
781 /* otherwise, load the buffer and try again */
782 lock_ReleaseMutex(&bufferp->mx);
783 code = cm_GetBuffer(scp, bufferp, NULL, userp,
785 lock_ReleaseMutex(&scp->mx);
786 lock_ObtainMutex(&bufferp->mx);
791 lock_ReleaseMutex(&bufferp->mx);
792 buf_Release(bufferp);
796 } /* if (wrong buffer) ... */
798 /* now we have the buffer containing the entry we're interested
799 * in; copy it out if it represents a non-deleted entry.
801 entryInDir = curOffset.LowPart & (2048-1);
802 entryInBuffer = curOffset.LowPart & (cm_data.buf_blockSize - 1);
804 /* page header will help tell us which entries are free. Page
805 * header can change more often than once per buffer, since
806 * AFS 3 dir page size may be less than (but not more than) a
807 * buffer package buffer.
809 /* only look intra-buffer */
810 temp = curOffset.LowPart & (cm_data.buf_blockSize - 1);
811 temp &= ~(2048 - 1); /* turn off intra-page bits */
812 pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
814 /* now determine which entry we're looking at in the page. If
815 * it is free (there's a free bitmap at the start of the dir),
816 * we should skip these 32 bytes.
818 slotInPage = (entryInDir & 0x7e0) >> 5;
819 if (!(pageHeaderp->freeBitmap[slotInPage>>3]
820 & (1 << (slotInPage & 0x7)))) {
821 /* this entry is free */
822 numDirChunks = 1; /* only skip this guy */
826 tp = bufferp->datap + entryInBuffer;
827 dep = (cm_dirEntry_t *) tp; /* now points to AFS3 dir entry */
829 /* while we're here, compute the next entry's location, too,
830 * since we'll need it when writing out the cookie into the
831 * dir listing stream.
833 numDirChunks = cm_NameEntries(dep->name, NULL);
835 /* compute the offset of the cookie representing the next entry */
836 nextEntryCookie = curOffset.LowPart
837 + (CM_DIR_CHUNKSIZE * numDirChunks);
839 if (dep->fid.vnode != 0) {
840 /* this is one of the entries to use: it is not deleted */
841 code = (*funcp)(scp, dep, parmp, &curOffset);
844 } /* if we're including this name */
847 /* and adjust curOffset to be where the new cookie is */
849 thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
850 curOffset = LargeIntegerAdd(thyper, curOffset);
851 } /* while copying data for dir listing */
853 /* release the mutex */
855 lock_ReleaseMutex(&bufferp->mx);
856 buf_Release(bufferp);
861 int cm_NoneUpper(char *s)
865 if (c >= 'A' && c <= 'Z')
870 int cm_NoneLower(char *s)
874 if (c >= 'a' && c <= 'z')
879 long cm_LookupSearchProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
882 cm_lookupSearch_t *sp;
887 sp = (cm_lookupSearch_t *) rockp;
889 matchName = dep->name;
891 match = cm_stricmp(matchName, sp->searchNamep);
893 match = strcmp(matchName, sp->searchNamep);
897 && !cm_Is8Dot3(dep->name)) {
898 matchName = shortName;
899 cm_Gen8Dot3Name(dep, shortName, NULL);
901 match = cm_stricmp(matchName, sp->searchNamep);
903 match = strcmp(matchName, sp->searchNamep);
913 if (!sp->caseFold || matchName == shortName) {
914 sp->fid.vnode = ntohl(dep->fid.vnode);
915 sp->fid.unique = ntohl(dep->fid.unique);
916 return CM_ERROR_STOPNOW;
920 * If we get here, we are doing a case-insensitive search, and we
921 * have found a match. Now we determine what kind of match it is:
922 * exact, lower-case, upper-case, or none of the above. This is done
923 * in order to choose among matches, if there are more than one.
926 /* Exact matches are the best. */
927 match = strcmp(matchName, sp->searchNamep);
930 sp->fid.vnode = ntohl(dep->fid.vnode);
931 sp->fid.unique = ntohl(dep->fid.unique);
932 return CM_ERROR_STOPNOW;
935 /* Lower-case matches are next. */
938 if (cm_NoneUpper(matchName)) {
943 /* Upper-case matches are next. */
946 if (cm_NoneLower(matchName)) {
951 /* General matches are last. */
957 sp->fid.vnode = ntohl(dep->fid.vnode);
958 sp->fid.unique = ntohl(dep->fid.unique);
962 /* read the contents of a mount point into the appropriate string.
963 * called with locked scp, and returns with locked scp.
965 long cm_ReadMountPoint(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
972 if (scp->mountPointStringp[0])
975 /* otherwise, we have to read it in */
976 lock_ReleaseMutex(&scp->mx);
978 lock_ObtainRead(&scp->bufCreateLock);
979 thyper.LowPart = thyper.HighPart = 0;
980 code = buf_Get(scp, &thyper, &bufp);
981 lock_ReleaseRead(&scp->bufCreateLock);
983 lock_ObtainMutex(&scp->mx);
988 code = cm_SyncOp(scp, bufp, userp, reqp, 0,
989 CM_SCACHESYNC_READ | CM_SCACHESYNC_NEEDCALLBACK);
993 cm_SyncOpDone(scp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
995 if (cm_HaveBuffer(scp, bufp, 0))
998 /* otherwise load buffer */
999 code = cm_GetBuffer(scp, bufp, NULL, userp, reqp);
1003 /* locked, has callback, has valid data in buffer */
1004 if ((tlen = scp->length.LowPart) > MOUNTPOINTLEN - 1)
1005 return CM_ERROR_TOOBIG;
1007 code = CM_ERROR_INVAL;
1011 /* someone else did the work while we were out */
1012 if (scp->mountPointStringp[0]) {
1017 /* otherwise, copy out the link */
1018 memcpy(scp->mountPointStringp, bufp->datap, tlen);
1020 /* now make it null-terminated. Note that the original contents of a
1021 * link that is a mount point is "#volname." where "." is there just to
1022 * be turned into a null. That is, we can trash the last char of the
1023 * link without damaging the vol name. This is a stupid convention,
1024 * but that's the protocol.
1026 scp->mountPointStringp[tlen-1] = 0;
1036 /* called with a locked scp and chases the mount point, yielding outScpp.
1037 * scp remains locked, just for simplicity of describing the interface.
1039 long cm_FollowMountPoint(cm_scache_t *scp, cm_scache_t *dscp, cm_user_t *userp,
1040 cm_req_t *reqp, cm_scache_t **outScpp)
1048 cm_volume_t *volp = NULL;
1055 if (scp->mountRootFid.cell != 0 && scp->mountRootGen >= cm_data.mountRootGen) {
1056 tfid = scp->mountRootFid;
1057 lock_ReleaseMutex(&scp->mx);
1058 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1059 lock_ObtainMutex(&scp->mx);
1063 /* parse the volume name */
1064 mpNamep = scp->mountPointStringp;
1066 return CM_ERROR_NOSUCHPATH;
1067 tlen = (int)strlen(scp->mountPointStringp);
1068 mtType = *scp->mountPointStringp;
1069 cellNamep = malloc(tlen);
1070 volNamep = malloc(tlen);
1072 cp = strrchr(mpNamep, ':');
1074 /* cellular mount point */
1075 memset(cellNamep, 0, tlen);
1076 strncpy(cellNamep, mpNamep+1, cp - mpNamep - 1);
1077 strcpy(volNamep, cp+1);
1078 /* now look up the cell */
1079 lock_ReleaseMutex(&scp->mx);
1080 cellp = cm_GetCell(cellNamep, CM_FLAG_CREATE);
1081 lock_ObtainMutex(&scp->mx);
1085 strcpy(volNamep, mpNamep+1);
1087 cellp = cm_FindCellByID(scp->fid.cell);
1091 code = CM_ERROR_NOSUCHCELL;
1095 vnLength = strlen(volNamep);
1096 if (vnLength >= 8 && strcmp(volNamep + vnLength - 7, ".backup") == 0)
1097 targetType = BACKVOL;
1098 else if (vnLength >= 10
1099 && strcmp(volNamep + vnLength - 9, ".readonly") == 0)
1104 /* check for backups within backups */
1105 if (targetType == BACKVOL
1106 && (scp->flags & (CM_SCACHEFLAG_RO | CM_SCACHEFLAG_PURERO))
1107 == CM_SCACHEFLAG_RO) {
1108 code = CM_ERROR_NOSUCHVOLUME;
1112 /* now we need to get the volume */
1113 lock_ReleaseMutex(&scp->mx);
1114 if (cm_VolNameIsID(volNamep)) {
1115 code = cm_GetVolumeByID(cellp, atoi(volNamep), userp, reqp,
1116 CM_GETVOL_FLAG_CREATE, &volp);
1118 code = cm_GetVolumeByName(cellp, volNamep, userp, reqp,
1119 CM_GETVOL_FLAG_CREATE, &volp);
1121 lock_ObtainMutex(&scp->mx);
1124 /* save the parent of the volume root for this is the
1125 * place where the volume is mounted and we must remember
1126 * this in the volume structure rather than just in the
1127 * scache entry lest the scache entry gets recycled
1130 lock_ObtainMutex(&volp->mx);
1131 volp->dotdotFid = dscp->fid;
1132 lock_ReleaseMutex(&volp->mx);
1134 scp->mountRootFid.cell = cellp->cellID;
1136 /* if the mt pt originates in a .backup volume (not a .readonly)
1137 * and FollowBackupPath is active, and if there is a .backup
1138 * volume for the target, then use the .backup of the target
1139 * instead of the read-write.
1141 if (cm_followBackupPath &&
1143 (dscp->flags & (CM_SCACHEFLAG_RO|CM_SCACHEFLAG_PURERO)) == CM_SCACHEFLAG_RO &&
1144 (targetType == RWVOL || targetType == ROVOL && volp->ro.ID == 0)
1146 targetType = BACKVOL;
1148 /* if the mt pt is in a read-only volume (not just a
1149 * backup), and if there is a read-only volume for the
1150 * target, and if this is a targetType '#' mount point, use
1151 * the read-only, otherwise use the one specified.
1153 else if (mtType == '#' && targetType == RWVOL &&
1154 (scp->flags & CM_SCACHEFLAG_PURERO) &&
1158 if (targetType == ROVOL)
1159 scp->mountRootFid.volume = volp->ro.ID;
1160 else if (targetType == BACKVOL)
1161 scp->mountRootFid.volume = volp->bk.ID;
1163 scp->mountRootFid.volume = volp->rw.ID;
1165 /* the rest of the fid is a magic number */
1166 scp->mountRootFid.vnode = 1;
1167 scp->mountRootFid.unique = 1;
1168 scp->mountRootGen = cm_data.mountRootGen;
1170 tfid = scp->mountRootFid;
1171 lock_ReleaseMutex(&scp->mx);
1172 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1173 lock_ObtainMutex(&scp->mx);
1184 long cm_LookupInternal(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1185 cm_req_t *reqp, cm_scache_t **outpScpp)
1188 int dnlcHit = 1; /* did we hit in the dnlc? yes, we did */
1189 cm_scache_t *tscp = NULL;
1190 cm_scache_t *mountedScp;
1191 cm_lookupSearch_t rock;
1194 memset(&rock, 0, sizeof(rock));
1196 if (dscp->fid.vnode == 1 && dscp->fid.unique == 1
1197 && strcmp(namep, "..") == 0) {
1198 if (dscp->dotdotFid.volume == 0)
1199 return CM_ERROR_NOSUCHVOLUME;
1200 rock.fid = dscp->dotdotFid;
1202 } else if (strcmp(namep, ".") == 0) {
1203 rock.fid = dscp->fid;
1207 if (flags & CM_FLAG_NOMOUNTCHASE) {
1208 /* In this case, we should go and call cm_Dir* functions
1209 directly since the following cm_ApplyDir() function will
1217 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ, &dirop);
1220 code = cm_BPlusDirLookup(&dirop, namep, &rock.fid);
1225 code = cm_DirLookup(&dirop, namep, &rock.fid);
1227 cm_EndDirOp(&dirop);
1237 if (code == CM_ERROR_INEXACT_MATCH && (flags & CM_FLAG_CASEFOLD)) {
1244 return CM_ERROR_BPLUS_NOMATCH;
1249 rock.fid.cell = dscp->fid.cell;
1250 rock.fid.volume = dscp->fid.volume;
1251 rock.searchNamep = namep;
1252 rock.caseFold = (flags & CM_FLAG_CASEFOLD);
1253 rock.hasTilde = ((strchr(namep, '~') != NULL) ? 1 : 0);
1255 /* If NOMOUNTCHASE, bypass DNLC by passing NULL scp pointer */
1256 code = cm_ApplyDir(dscp, cm_LookupSearchProc, &rock, NULL, userp, reqp,
1257 (flags & CM_FLAG_NOMOUNTCHASE) ? NULL : &tscp);
1259 /* code == 0 means we fell off the end of the dir, while stopnow means
1260 * that we stopped early, probably because we found the entry we're
1261 * looking for. Any other non-zero code is an error.
1263 if (code && code != CM_ERROR_STOPNOW) {
1264 /* if the cm_scache_t we are searching in is not a directory
1265 * we must return path not found because the error
1266 * is to describe the final component not an intermediary
1268 if (code == CM_ERROR_NOTDIR) {
1269 if (flags & CM_FLAG_CHECKPATH)
1270 return CM_ERROR_NOSUCHPATH;
1272 return CM_ERROR_NOSUCHFILE;
1277 getroot = (dscp==cm_data.rootSCachep) ;
1279 if (!cm_freelanceEnabled || !getroot) {
1280 if (flags & CM_FLAG_CHECKPATH)
1281 return CM_ERROR_NOSUCHPATH;
1283 return CM_ERROR_NOSUCHFILE;
1285 else { /* nonexistent dir on freelance root, so add it */
1286 char fullname[200] = ".";
1289 osi_Log1(afsd_logp,"cm_Lookup adding mount for non-existent directory: %s",
1290 osi_LogSaveString(afsd_logp,namep));
1291 if (namep[0] == '.') {
1292 if (cm_GetCell_Gen(&namep[1], &fullname[1], CM_FLAG_CREATE)) {
1294 if ( stricmp(&namep[1], &fullname[1]) )
1295 code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1297 code = cm_FreelanceAddMount(namep, &fullname[1], "root.cell.", 1, &rock.fid);
1300 if (cm_GetCell_Gen(namep, fullname, CM_FLAG_CREATE)) {
1302 if ( stricmp(namep, fullname) )
1303 code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1305 code = cm_FreelanceAddMount(namep, fullname, "root.cell.", 0, &rock.fid);
1308 if (!found || code < 0) { /* add mount point failed, so give up */
1309 if (flags & CM_FLAG_CHECKPATH)
1310 return CM_ERROR_NOSUCHPATH;
1312 return CM_ERROR_NOSUCHFILE;
1314 tscp = NULL; /* to force call of cm_GetSCache */
1319 if ( !tscp ) /* we did not find it in the dnlc */
1322 code = cm_GetSCache(&rock.fid, &tscp, userp, reqp);
1326 /* tscp is now held */
1328 lock_ObtainMutex(&tscp->mx);
1329 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1330 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1332 lock_ReleaseMutex(&tscp->mx);
1333 cm_ReleaseSCache(tscp);
1336 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1337 /* tscp is now locked */
1339 if (!(flags & CM_FLAG_NOMOUNTCHASE)
1340 && tscp->fileType == CM_SCACHETYPE_MOUNTPOINT) {
1341 /* mount points are funny: they have a volume name to mount
1344 code = cm_ReadMountPoint(tscp, userp, reqp);
1346 code = cm_FollowMountPoint(tscp, dscp, userp, reqp,
1348 lock_ReleaseMutex(&tscp->mx);
1349 cm_ReleaseSCache(tscp);
1356 lock_ReleaseMutex(&tscp->mx);
1359 /* copy back pointer */
1362 /* insert scache in dnlc */
1363 if ( !dnlcHit && !(flags & CM_FLAG_NOMOUNTCHASE) && rock.ExactFound ) {
1364 /* lock the directory entry to prevent racing callback revokes */
1365 lock_ObtainMutex(&dscp->mx);
1366 if ( dscp->cbServerp != NULL && dscp->cbExpires > 0 )
1367 cm_dnlcEnter(dscp, namep, tscp);
1368 lock_ReleaseMutex(&dscp->mx);
1375 int cm_ExpandSysName(char *inp, char *outp, long outSize, unsigned int index)
1380 tp = strrchr(inp, '@');
1382 return 0; /* no @sys */
1384 if (strcmp(tp, "@sys") != 0)
1385 return 0; /* no @sys */
1387 /* caller just wants to know if this is a valid @sys type of name */
1391 if (index >= MAXNUMSYSNAMES)
1394 /* otherwise generate the properly expanded @sys name */
1395 prefixCount = (int)(tp - inp);
1397 strncpy(outp, inp, prefixCount); /* copy out "a." from "a.@sys" */
1398 outp[prefixCount] = 0; /* null terminate the "a." */
1399 strcat(outp, cm_sysNameList[index]);/* append i386_nt40 */
1403 long cm_EvaluateVolumeReference(char * namep, long flags, cm_user_t * userp,
1404 cm_req_t *reqp, cm_scache_t ** outpScpp)
1407 char cellName[CELL_MAXNAMELEN];
1408 char volumeName[VL_MAXNAMELEN];
1413 cm_cell_t * cellp = NULL;
1414 cm_volume_t * volp = NULL;
1417 int mountType = RWVOL;
1419 osi_Log1(afsd_logp, "cm_EvaluateVolumeReference for string [%s]",
1420 osi_LogSaveString(afsd_logp, namep));
1422 if (strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) != 0) {
1423 goto _exit_invalid_path;
1426 /* namep is assumed to look like the following:
1428 @vol:<cellname>%<volume>\0
1430 @vol:<cellname>#<volume>\0
1434 cp = namep + CM_PREFIX_VOL_CCH; /* cp points to cell name, hopefully */
1435 tp = strchr(cp, '%');
1437 tp = strchr(cp, '#');
1439 (len = tp - cp) == 0 ||
1440 len > CELL_MAXNAMELEN)
1441 goto _exit_invalid_path;
1442 strncpy(cellName, cp, len);
1443 cellName[len] = '\0';
1448 cp = tp+1; /* cp now points to volume, supposedly */
1449 strncpy(volumeName, cp, VL_MAXNAMELEN-1);
1450 volumeName[VL_MAXNAMELEN - 1] = 0;
1452 /* OK, now we have the cell and the volume */
1453 osi_Log2(afsd_logp, " Found cell [%s] and volume [%s]",
1454 osi_LogSaveString(afsd_logp, cellName),
1455 osi_LogSaveString(afsd_logp, volumeName));
1457 cellp = cm_GetCell(cellName, CM_FLAG_CREATE);
1458 if (cellp == NULL) {
1459 goto _exit_invalid_path;
1462 len = strlen(volumeName);
1463 if (len >= 8 && strcmp(volumeName + len - 7, ".backup") == 0)
1465 else if (len >= 10 &&
1466 strcmp(volumeName + len - 9, ".readonly") == 0)
1471 if (cm_VolNameIsID(volumeName)) {
1472 code = cm_GetVolumeByID(cellp, atoi(volumeName), userp, reqp,
1473 CM_GETVOL_FLAG_CREATE, &volp);
1475 code = cm_GetVolumeByName(cellp, volumeName, userp, reqp,
1476 CM_GETVOL_FLAG_CREATE, &volp);
1482 fid.cell = cellp->cellID;
1484 if (volType == BACKVOL)
1485 fid.volume = volp->bk.ID;
1486 else if (volType == ROVOL ||
1487 (volType == RWVOL && mountType == ROVOL && volp->ro.ID != 0))
1488 fid.volume = volp->ro.ID;
1490 fid.volume = volp->rw.ID;
1495 code = cm_GetSCache(&fid, outpScpp, userp, reqp);
1505 if (flags & CM_FLAG_CHECKPATH)
1506 return CM_ERROR_NOSUCHPATH;
1508 return CM_ERROR_NOSUCHFILE;
1511 #ifdef DEBUG_REFCOUNT
1512 long cm_LookupDbg(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1513 cm_req_t *reqp, cm_scache_t **outpScpp, char * file, long line)
1515 long cm_Lookup(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1516 cm_req_t *reqp, cm_scache_t **outpScpp)
1520 char tname[AFSPATHMAX];
1521 int sysNameIndex = 0;
1522 cm_scache_t *scp = NULL;
1524 #ifdef DEBUG_REFCOUNT
1525 afsi_log("%s:%d cm_Lookup dscp 0x%p ref %d", file, line, dscp, dscp->refCount, file, line);
1526 osi_Log2(afsd_logp, "cm_Lookup dscp 0x%p ref %d", dscp, dscp->refCount);
1529 if ( stricmp(namep,SMB_IOCTL_FILENAME_NOSLASH) == 0 ) {
1530 if (flags & CM_FLAG_CHECKPATH)
1531 return CM_ERROR_NOSUCHPATH;
1533 return CM_ERROR_NOSUCHFILE;
1536 if (dscp == cm_data.rootSCachep &&
1537 strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) == 0) {
1538 return cm_EvaluateVolumeReference(namep, flags, userp, reqp, outpScpp);
1541 if (cm_ExpandSysName(namep, NULL, 0, 0) > 0) {
1542 for ( sysNameIndex = 0; sysNameIndex < MAXNUMSYSNAMES; sysNameIndex++) {
1543 code = cm_ExpandSysName(namep, tname, sizeof(tname), sysNameIndex);
1545 code = cm_LookupInternal(dscp, tname, flags, userp, reqp, &scp);
1546 #ifdef DEBUG_REFCOUNT
1547 afsi_log("%s:%d cm_LookupInternal (1) code 0x%x dscp 0x%p ref %d scp 0x%p ref %d", file, line, code, dscp, dscp->refCount, scp, scp ? scp->refCount : 0);
1548 osi_Log3(afsd_logp, "cm_LookupInternal (1) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1556 cm_ReleaseSCache(scp);
1560 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1561 #ifdef DEBUG_REFCOUNT
1562 afsi_log("%s:%d cm_LookupInternal (2) code 0x%x dscp 0x%p ref %d scp 0x%p ref %d", file, line, code, dscp, dscp->refCount, scp, scp ? scp->refCount : 0);
1563 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1570 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1571 #ifdef DEBUG_REFCOUNT
1572 afsi_log("%s:%d cm_LookupInternal (2) code 0x%x dscp 0x%p ref %d scp 0x%p ref %d", file, line, code, dscp, dscp->refCount, scp, scp ? scp->refCount : 0);
1573 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1579 /* None of the possible sysName expansions could be found */
1580 if (flags & CM_FLAG_CHECKPATH)
1581 return CM_ERROR_NOSUCHPATH;
1583 return CM_ERROR_NOSUCHFILE;
1586 long cm_Unlink(cm_scache_t *dscp, char *namep, cm_user_t *userp, cm_req_t *reqp)
1592 AFSFetchStatus newDirStatus;
1594 struct rx_connection * callp;
1597 #ifdef AFS_FREELANCE_CLIENT
1598 if (cm_freelanceEnabled && dscp == cm_data.rootSCachep) {
1599 /* deleting a mount point from the root dir. */
1600 code = cm_FreelanceRemoveMount(namep);
1605 /* make sure we don't screw up the dir status during the merge */
1606 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
1608 lock_ObtainMutex(&dscp->mx);
1609 sflags = CM_SCACHESYNC_STOREDATA;
1610 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, sflags);
1611 lock_ReleaseMutex(&dscp->mx);
1613 cm_EndDirOp(&dirop);
1618 afsFid.Volume = dscp->fid.volume;
1619 afsFid.Vnode = dscp->fid.vnode;
1620 afsFid.Unique = dscp->fid.unique;
1622 osi_Log1(afsd_logp, "CALL RemoveFile scp 0x%p", dscp);
1624 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
1628 callp = cm_GetRxConn(connp);
1629 code = RXAFS_RemoveFile(callp, &afsFid, namep,
1630 &newDirStatus, &volSync);
1631 rx_PutConnection(callp);
1633 } while (cm_Analyze(connp, userp, reqp, &dscp->fid, &volSync, NULL, NULL, code));
1634 code = cm_MapRPCError(code, reqp);
1637 osi_Log1(afsd_logp, "CALL RemoveFile FAILURE, code 0x%x", code);
1639 osi_Log0(afsd_logp, "CALL RemoveFile SUCCESS");
1642 lock_ObtainWrite(&dirop.scp->dirlock);
1643 dirop.lockType = CM_DIRLOCK_WRITE;
1645 lock_ObtainMutex(&dscp->mx);
1646 cm_dnlcRemove(dscp, namep);
1647 cm_SyncOpDone(dscp, NULL, sflags);
1649 cm_MergeStatus(NULL, dscp, &newDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
1650 } else if (code == CM_ERROR_NOSUCHFILE) {
1651 /* windows would not have allowed the request to delete the file
1652 * if it did not believe the file existed. therefore, we must
1653 * have an inconsistent view of the world.
1655 dscp->cbServerp = NULL;
1657 lock_ReleaseMutex(&dscp->mx);
1659 if (code == 0 && cm_CheckDirOpForSingleChange(&dirop)) {
1660 cm_DirDeleteEntry(&dirop, namep);
1662 cm_BPlusDirDeleteEntry(&dirop, namep);
1665 cm_EndDirOp(&dirop);
1670 /* called with a locked vnode, and fills in the link info.
1671 * returns this the vnode still locked.
1673 long cm_HandleLink(cm_scache_t *linkScp, cm_user_t *userp, cm_req_t *reqp)
1680 lock_AssertMutex(&linkScp->mx);
1681 if (!linkScp->mountPointStringp[0]) {
1682 /* read the link data */
1683 lock_ReleaseMutex(&linkScp->mx);
1684 thyper.LowPart = thyper.HighPart = 0;
1685 code = buf_Get(linkScp, &thyper, &bufp);
1686 lock_ObtainMutex(&linkScp->mx);
1690 code = cm_SyncOp(linkScp, bufp, userp, reqp, 0,
1691 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1696 cm_SyncOpDone(linkScp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1698 if (cm_HaveBuffer(linkScp, bufp, 0))
1701 code = cm_GetBuffer(linkScp, bufp, NULL, userp, reqp);
1706 } /* while loop to get the data */
1708 /* now if we still have no link read in,
1709 * copy the data from the buffer */
1710 if ((temp = linkScp->length.LowPart) >= MOUNTPOINTLEN) {
1712 return CM_ERROR_TOOBIG;
1715 /* otherwise, it fits; make sure it is still null (could have
1716 * lost race with someone else referencing this link above),
1717 * and if so, copy in the data.
1719 if (!linkScp->mountPointStringp[0]) {
1720 strncpy(linkScp->mountPointStringp, bufp->datap, temp);
1721 linkScp->mountPointStringp[temp] = 0; /* null terminate */
1723 if ( !strnicmp(linkScp->mountPointStringp, "msdfs:", strlen("msdfs:")) )
1724 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1727 } /* don't have sym link contents cached */
1732 /* called with a held vnode and a path suffix, with the held vnode being a
1733 * symbolic link. Our goal is to generate a new path to interpret, and return
1734 * this new path in newSpaceBufferp. If the new vnode is relative to a dir
1735 * other than the directory containing the symbolic link, then the new root is
1736 * returned in *newRootScpp, otherwise a null is returned there.
1738 long cm_AssembleLink(cm_scache_t *linkScp, char *pathSuffixp,
1739 cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1740 cm_user_t *userp, cm_req_t *reqp)
1747 *newRootScpp = NULL;
1748 *newSpaceBufferp = NULL;
1750 lock_ObtainMutex(&linkScp->mx);
1751 code = cm_HandleLink(linkScp, userp, reqp);
1755 /* if we may overflow the buffer, bail out; buffer is signficantly
1756 * bigger than max path length, so we don't really have to worry about
1757 * being a little conservative here.
1759 if (strlen(linkScp->mountPointStringp) + strlen(pathSuffixp) + 2
1760 >= CM_UTILS_SPACESIZE)
1761 return CM_ERROR_TOOBIG;
1763 tsp = cm_GetSpace();
1764 linkp = linkScp->mountPointStringp;
1765 if (strncmp(linkp, cm_mountRoot, cm_mountRootLen) == 0) {
1766 if (strlen(linkp) > cm_mountRootLen)
1767 strcpy(tsp->data, linkp+cm_mountRootLen+1);
1770 *newRootScpp = cm_data.rootSCachep;
1771 cm_HoldSCache(cm_data.rootSCachep);
1772 } else if (linkp[0] == '\\' && linkp[1] == '\\') {
1773 if (!strnicmp(&linkp[2], cm_NetbiosName, (len = (long)strlen(cm_NetbiosName))))
1775 char * p = &linkp[len + 3];
1776 if (strnicmp(p, "all", 3) == 0)
1779 strcpy(tsp->data, p);
1780 for (p = tsp->data; *p; p++) {
1784 *newRootScpp = cm_data.rootSCachep;
1785 cm_HoldSCache(cm_data.rootSCachep);
1787 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1788 strcpy(tsp->data, linkp);
1789 code = CM_ERROR_PATH_NOT_COVERED;
1791 } else if ( linkScp->fileType == CM_SCACHETYPE_DFSLINK ||
1792 !strnicmp(linkp, "msdfs:", (len = (long)strlen("msdfs:"))) ) {
1793 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1794 strcpy(tsp->data, linkp);
1795 code = CM_ERROR_PATH_NOT_COVERED;
1796 } else if (*linkp == '\\' || *linkp == '/') {
1798 /* formerly, this was considered to be from the AFS root,
1799 * but this seems to create problems. instead, we will just
1800 * reject the link */
1801 strcpy(tsp->data, linkp+1);
1802 *newRootScpp = cm_data.rootSCachep;
1803 cm_HoldSCache(cm_data.rootSCachep);
1805 /* we still copy the link data into the response so that
1806 * the user can see what the link points to
1808 linkScp->fileType = CM_SCACHETYPE_INVALID;
1809 strcpy(tsp->data, linkp);
1810 code = CM_ERROR_NOSUCHPATH;
1813 /* a relative link */
1814 strcpy(tsp->data, linkp);
1816 if (pathSuffixp[0] != 0) { /* if suffix string is non-null */
1817 strcat(tsp->data, "\\");
1818 strcat(tsp->data, pathSuffixp);
1821 *newSpaceBufferp = tsp;
1825 if (code == CM_ERROR_PATH_NOT_COVERED && reqp->tidPathp && reqp->relPathp)
1826 cm_VolStatus_Notify_DFS_Mapping(linkScp, reqp->tidPathp, reqp->relPathp);
1830 lock_ReleaseMutex(&linkScp->mx);
1833 #ifdef DEBUG_REFCOUNT
1834 long cm_NameIDbg(cm_scache_t *rootSCachep, char *pathp, long flags,
1835 cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp,
1836 char * file, long line)
1838 long cm_NameI(cm_scache_t *rootSCachep, char *pathp, long flags,
1839 cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp)
1843 char *tp; /* ptr moving through input buffer */
1844 char tc; /* temp char */
1845 int haveComponent; /* has new component started? */
1846 char component[AFSPATHMAX]; /* this is the new component */
1847 char *cp; /* component name being assembled */
1848 cm_scache_t *tscp; /* current location in the hierarchy */
1849 cm_scache_t *nscp; /* next dude down */
1850 cm_scache_t *dirScp; /* last dir we searched */
1851 cm_scache_t *linkScp; /* new root for the symlink we just
1853 cm_space_t *psp; /* space for current path, if we've hit
1855 cm_space_t *tempsp; /* temp vbl */
1856 char *restp; /* rest of the pathname to interpret */
1857 int symlinkCount; /* count of # of symlinks traversed */
1858 int extraFlag; /* avoid chasing mt pts for dir cmd */
1859 int phase = 1; /* 1 = tidPathp, 2 = pathp */
1860 #define MAX_FID_COUNT 512
1861 cm_fid_t fids[MAX_FID_COUNT]; /* array of fids processed in this path walk */
1862 int fid_count = 0; /* number of fids processed in this path walk */
1865 #ifdef DEBUG_REFCOUNT
1866 afsi_log("%s:%d cm_NameI rootscp 0x%p ref %d", file, line, rootSCachep, rootSCachep->refCount);
1867 osi_Log4(afsd_logp,"cm_NameI rootscp 0x%p path %s tidpath %s flags 0x%x",
1868 rootSCachep, pathp ? pathp : "<NULL>", tidPathp ? tidPathp : "<NULL>",
1883 cm_HoldSCache(tscp);
1891 /* map Unix slashes into DOS ones so we can interpret Unix
1897 if (!haveComponent) {
1900 } else if (tc == 0) {
1914 /* we have a component here */
1915 if (tc == 0 || tc == '\\') {
1916 /* end of the component; we're at the last
1917 * component if tc == 0. However, if the last
1918 * is a symlink, we have more to do.
1920 *cp++ = 0; /* add null termination */
1922 if ((flags & CM_FLAG_DIRSEARCH) && tc == 0)
1923 extraFlag = CM_FLAG_NOMOUNTCHASE;
1924 code = cm_Lookup(tscp, component,
1926 userp, reqp, &nscp);
1929 if (!strcmp(component,"..") || !strcmp(component,".")) {
1931 * roll back the fid list until we find the fid
1932 * that matches where we are now. Its not necessarily
1933 * one or two fids because they might have been
1934 * symlinks or mount points or both that were crossed.
1936 for ( i=fid_count-1; i>=0; i--) {
1937 if (!cm_FidCmp(&nscp->fid, &fids[i]))
1941 /* add the new fid to the list */
1942 for ( i=0; i<fid_count; i++) {
1943 if ( !cm_FidCmp(&nscp->fid, &fids[i]) ) {
1944 code = CM_ERROR_TOO_MANY_SYMLINKS;
1945 cm_ReleaseSCache(nscp);
1950 if (i == fid_count && fid_count < MAX_FID_COUNT) {
1951 fids[fid_count++] = nscp->fid;
1957 cm_ReleaseSCache(tscp);
1959 cm_ReleaseSCache(dirScp);
1962 if ((code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) &&
1963 tscp->fileType == CM_SCACHETYPE_SYMLINK)
1965 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_NOSUCHPATH");
1966 return CM_ERROR_NOSUCHPATH;
1968 osi_Log1(afsd_logp,"cm_NameI code 0x%x", code);
1973 haveComponent = 0; /* component done */
1975 cm_ReleaseSCache(dirScp);
1976 dirScp = tscp; /* for some symlinks */
1977 tscp = nscp; /* already held */
1979 if (tc == 0 && !(flags & CM_FLAG_FOLLOW) && phase == 2) {
1982 cm_ReleaseSCache(dirScp);
1988 /* now, if tscp is a symlink, we should follow
1989 * it and assemble the path again.
1991 lock_ObtainMutex(&tscp->mx);
1992 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1993 CM_SCACHESYNC_GETSTATUS
1994 | CM_SCACHESYNC_NEEDCALLBACK);
1996 lock_ReleaseMutex(&tscp->mx);
1997 cm_ReleaseSCache(tscp);
2000 cm_ReleaseSCache(dirScp);
2005 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2007 if (tscp->fileType == CM_SCACHETYPE_SYMLINK) {
2008 /* this is a symlink; assemble a new buffer */
2009 lock_ReleaseMutex(&tscp->mx);
2010 if (symlinkCount++ >= MAX_SYMLINK_COUNT) {
2011 cm_ReleaseSCache(tscp);
2014 cm_ReleaseSCache(dirScp);
2019 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_TOO_MANY_SYMLINKS");
2020 return CM_ERROR_TOO_MANY_SYMLINKS;
2026 code = cm_AssembleLink(tscp, restp, &linkScp, &tempsp, userp, reqp);
2028 if (code == 0 && linkScp != NULL) {
2029 if (linkScp == cm_data.rootSCachep)
2032 for ( i=0; i<fid_count; i++) {
2033 if ( !cm_FidCmp(&linkScp->fid, &fids[i]) ) {
2034 code = CM_ERROR_TOO_MANY_SYMLINKS;
2035 cm_ReleaseSCache(linkScp);
2041 if (i == fid_count && fid_count < MAX_FID_COUNT) {
2042 fids[fid_count++] = linkScp->fid;
2047 /* something went wrong */
2048 cm_ReleaseSCache(tscp);
2051 cm_ReleaseSCache(dirScp);
2057 /* otherwise, tempsp has the new path,
2058 * and linkScp is the new root from
2059 * which to interpret that path.
2060 * Continue with the namei processing,
2061 * also doing the bookkeeping for the
2062 * space allocation and tracking the
2063 * vnode reference counts.
2069 cm_ReleaseSCache(tscp);
2074 * now, if linkScp is null, that's
2075 * AssembleLink's way of telling us that
2076 * the sym link is relative to the dir
2077 * containing the link. We have a ref
2078 * to it in dirScp, and we hold it now
2079 * and reuse it as the new spot in the
2087 /* not a symlink, we may be done */
2088 lock_ReleaseMutex(&tscp->mx);
2096 cm_ReleaseSCache(dirScp);
2104 cm_ReleaseSCache(dirScp);
2107 } /* end of a component */
2110 } /* we have a component */
2111 } /* big while loop over all components */
2115 cm_ReleaseSCache(dirScp);
2121 cm_ReleaseSCache(tscp);
2123 #ifdef DEBUG_REFCOUNT
2124 afsi_log("%s:%d cm_NameI code 0x%x outScpp 0x%p ref %d", file, line, code, *outScpp, (*outScpp)->refCount);
2126 osi_Log2(afsd_logp,"cm_NameI code 0x%x outScpp 0x%p", code, *outScpp);
2130 /* called with a dir, and a vnode within the dir that happens to be a symlink.
2131 * We chase the link, and return a held pointer to the target, if it exists,
2132 * in *outScpp. If we succeed, we return 0, otherwise we return an error code
2133 * and do not hold or return a target vnode.
2135 * This is very similar to calling cm_NameI with the last component of a name,
2136 * which happens to be a symlink, except that we've already passed by the name.
2138 * This function is typically called by the directory listing functions, which
2139 * encounter symlinks but need to return the proper file length so programs
2140 * like "more" work properly when they make use of the attributes retrieved from
2143 * The input vnode should not be locked when this function is called.
2145 long cm_EvaluateSymLink(cm_scache_t *dscp, cm_scache_t *linkScp,
2146 cm_scache_t **outScpp, cm_user_t *userp, cm_req_t *reqp)
2150 cm_scache_t *newRootScp;
2152 osi_Log1(afsd_logp, "Evaluating symlink scp 0x%p", linkScp);
2154 code = cm_AssembleLink(linkScp, "", &newRootScp, &spacep, userp, reqp);
2158 /* now, if newRootScp is NULL, we're really being told that the symlink
2159 * is relative to the current directory (dscp).
2161 if (newRootScp == NULL) {
2163 cm_HoldSCache(dscp);
2166 code = cm_NameI(newRootScp, spacep->data,
2167 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW | CM_FLAG_DIRSEARCH,
2168 userp, NULL, reqp, outScpp);
2170 if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH)
2171 code = CM_ERROR_NOSUCHPATH;
2173 /* this stuff is allocated no matter what happened on the namei call,
2175 cm_FreeSpace(spacep);
2176 cm_ReleaseSCache(newRootScp);
2178 if (linkScp == *outScpp) {
2179 cm_ReleaseSCache(*outScpp);
2181 code = CM_ERROR_NOSUCHPATH;
2187 /* make this big enough so that one buffer of dir pages won't overflow. We'll
2188 * check anyway, but we want to minimize the chance that we have to leave stuff
2191 #define CM_BULKMAX (3 * AFSCBMAX)
2193 /* rock for bulk stat calls */
2194 typedef struct cm_bulkStat {
2195 osi_hyper_t bufOffset; /* only do it for things in this buffer page */
2197 /* info for the actual call */
2198 int counter; /* next free slot */
2199 AFSFid fids[CM_BULKMAX];
2200 AFSFetchStatus stats[CM_BULKMAX];
2201 AFSCallBack callbacks[CM_BULKMAX];
2204 /* for a given entry, make sure that it isn't in the stat cache, and then
2205 * add it to the list of file IDs to be obtained.
2207 * Don't bother adding it if we already have a vnode. Note that the dir
2208 * is locked, so we have to be careful checking the vnode we're thinking of
2209 * processing, to avoid deadlocks.
2211 long cm_TryBulkProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
2222 /* Don't overflow bsp. */
2223 if (bsp->counter >= CM_BULKMAX)
2224 return CM_ERROR_STOPNOW;
2226 thyper.LowPart = cm_data.buf_blockSize;
2227 thyper.HighPart = 0;
2228 thyper = LargeIntegerAdd(thyper, bsp->bufOffset);
2230 /* thyper is now the first byte past the end of the record we're
2231 * interested in, and bsp->bufOffset is the first byte of the record
2232 * we're interested in.
2233 * Skip data in the others.
2236 if (LargeIntegerLessThan(*offp, bsp->bufOffset))
2238 if (LargeIntegerGreaterThanOrEqualTo(*offp, thyper))
2239 return CM_ERROR_STOPNOW;
2240 if (strcmp(dep->name, ".") == 0 || strcmp(dep->name, "..") == 0)
2243 tfid.cell = scp->fid.cell;
2244 tfid.volume = scp->fid.volume;
2245 tfid.vnode = ntohl(dep->fid.vnode);
2246 tfid.unique = ntohl(dep->fid.unique);
2247 tscp = cm_FindSCache(&tfid);
2249 if (lock_TryMutex(&tscp->mx)) {
2250 /* we have an entry that we can look at */
2251 if (!(tscp->flags & CM_SCACHEFLAG_EACCESS) && cm_HaveCallback(tscp)) {
2252 /* we have a callback on it. Don't bother
2253 * fetching this stat entry, since we're happy
2254 * with the info we have.
2256 lock_ReleaseMutex(&tscp->mx);
2257 cm_ReleaseSCache(tscp);
2260 lock_ReleaseMutex(&tscp->mx);
2262 cm_ReleaseSCache(tscp);
2265 #ifdef AFS_FREELANCE_CLIENT
2266 // yj: if this is a mountpoint under root.afs then we don't want it
2267 // to be bulkstat-ed, instead, we call getSCache directly and under
2268 // getSCache, it is handled specially.
2269 if ( cm_freelanceEnabled &&
2270 tfid.cell==AFS_FAKE_ROOT_CELL_ID &&
2271 tfid.volume==AFS_FAKE_ROOT_VOL_ID &&
2272 !(tfid.vnode==0x1 && tfid.unique==0x1) )
2274 osi_Log0(afsd_logp, "cm_TryBulkProc Freelance calls cm_SCache on root.afs mountpoint");
2275 return cm_GetSCache(&tfid, &tscp, NULL, NULL);
2277 #endif /* AFS_FREELANCE_CLIENT */
2280 bsp->fids[i].Volume = scp->fid.volume;
2281 bsp->fids[i].Vnode = tfid.vnode;
2282 bsp->fids[i].Unique = tfid.unique;
2286 /* called with a locked scp and a pointer to a buffer. Make bulk stat
2287 * calls on all undeleted files in the page of the directory specified.
2290 cm_TryBulkStat(cm_scache_t *dscp, osi_hyper_t *offsetp, cm_user_t *userp,
2294 cm_bulkStat_t bb; /* this is *BIG*, probably 16K or so;
2295 * watch for stack problems */
2296 AFSCBFids fidStruct;
2297 AFSBulkStats statStruct;
2299 AFSCBs callbackStruct;
2302 cm_callbackRequest_t cbReq;
2308 struct rx_connection * callp;
2309 int inlinebulk = 0; /* Did we use InlineBulkStatus RPC or not? */
2311 osi_Log1(afsd_logp, "cm_TryBulkStat dir 0x%p", dscp);
2313 /* should be on a buffer boundary */
2314 osi_assertx((offsetp->LowPart & (cm_data.buf_blockSize - 1)) == 0, "invalid offset");
2316 memset(&bb, 0, sizeof(bb));
2317 bb.bufOffset = *offsetp;
2319 lock_ReleaseMutex(&dscp->mx);
2320 /* first, assemble the file IDs we need to stat */
2321 code = cm_ApplyDir(dscp, cm_TryBulkProc, (void *) &bb, offsetp, userp, reqp, NULL);
2323 /* if we failed, bail out early */
2324 if (code && code != CM_ERROR_STOPNOW) {
2325 lock_ObtainMutex(&dscp->mx);
2329 /* otherwise, we may have one or more bulk stat's worth of stuff in bb;
2330 * make the calls to create the entries. Handle AFSCBMAX files at a
2334 while (filex < bb.counter) {
2335 filesThisCall = bb.counter - filex;
2336 if (filesThisCall > AFSCBMAX)
2337 filesThisCall = AFSCBMAX;
2339 fidStruct.AFSCBFids_len = filesThisCall;
2340 fidStruct.AFSCBFids_val = &bb.fids[filex];
2341 statStruct.AFSBulkStats_len = filesThisCall;
2342 statStruct.AFSBulkStats_val = &bb.stats[filex];
2343 callbackStruct.AFSCBs_len = filesThisCall;
2344 callbackStruct.AFSCBs_val = &bb.callbacks[filex];
2345 cm_StartCallbackGrantingCall(NULL, &cbReq);
2346 osi_Log1(afsd_logp, "CALL BulkStatus, %d entries", filesThisCall);
2348 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2352 callp = cm_GetRxConn(connp);
2353 if (!(connp->serverp->flags & CM_SERVERFLAG_NOINLINEBULK)) {
2354 code = RXAFS_InlineBulkStatus(callp, &fidStruct,
2355 &statStruct, &callbackStruct, &volSync);
2356 if (code == RXGEN_OPCODE) {
2357 cm_SetServerNoInlineBulk(connp->serverp, 0);
2363 code = RXAFS_BulkStatus(callp, &fidStruct,
2364 &statStruct, &callbackStruct, &volSync);
2366 rx_PutConnection(callp);
2368 } while (cm_Analyze(connp, userp, reqp, &dscp->fid,
2369 &volSync, NULL, &cbReq, code));
2370 code = cm_MapRPCError(code, reqp);
2372 osi_Log2(afsd_logp, "CALL %sBulkStatus FAILURE code 0x%x",
2373 inlinebulk ? "Inline" : "", code);
2375 osi_Log1(afsd_logp, "CALL %sBulkStatus SUCCESS", inlinebulk ? "Inline" : "");
2377 /* may as well quit on an error, since we're not going to do
2378 * much better on the next immediate call, either.
2381 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2385 /* otherwise, we should do the merges */
2386 for (i = 0; i<filesThisCall; i++) {
2388 tfid.cell = dscp->fid.cell;
2389 tfid.volume = bb.fids[j].Volume;
2390 tfid.vnode = bb.fids[j].Vnode;
2391 tfid.unique = bb.fids[j].Unique;
2392 code = cm_GetSCache(&tfid, &scp, userp, reqp);
2396 /* otherwise, if this entry has no callback info,
2399 lock_ObtainMutex(&scp->mx);
2400 /* now, we have to be extra paranoid on merging in this
2401 * information, since we didn't use cm_SyncOp before
2402 * starting the fetch to make sure that no bad races
2403 * were occurring. Specifically, we need to make sure
2404 * we don't obliterate any newer information in the
2405 * vnode than have here.
2407 * Right now, be pretty conservative: if there's a
2408 * callback or a pending call, skip it.
2410 if ((scp->cbServerp == NULL || (scp->flags & CM_SCACHEFLAG_EACCESS))
2412 (CM_SCACHEFLAG_FETCHING
2413 | CM_SCACHEFLAG_STORING
2414 | CM_SCACHEFLAG_SIZESTORING))) {
2415 cm_EndCallbackGrantingCall(scp, &cbReq,
2417 CM_CALLBACK_MAINTAINCOUNT);
2418 cm_MergeStatus(dscp, scp, &bb.stats[j], &volSync, userp, 0);
2420 lock_ReleaseMutex(&scp->mx);
2421 cm_ReleaseSCache(scp);
2422 } /* all files in the response */
2423 /* now tell it to drop the count,
2424 * after doing the vnode processing above */
2425 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2427 filex += filesThisCall;
2428 } /* while there are still more files to process */
2429 lock_ObtainMutex(&dscp->mx);
2431 /* If we did the InlineBulk RPC pull out the return code and log it */
2433 if ((&bb.stats[0])->errorCode) {
2434 osi_Log1(afsd_logp, "cm_TryBulkStat bulk stat error: %d",
2435 (&bb.stats[0])->errorCode);
2439 osi_Log0(afsd_logp, "END cm_TryBulkStat");
2443 void cm_StatusFromAttr(AFSStoreStatus *statusp, cm_scache_t *scp, cm_attr_t *attrp)
2447 /* initialize store back mask as inexpensive local variable */
2449 memset(statusp, 0, sizeof(AFSStoreStatus));
2451 /* copy out queued info from scache first, if scp passed in */
2453 if (scp->mask & CM_SCACHEMASK_CLIENTMODTIME) {
2454 statusp->ClientModTime = scp->clientModTime;
2455 mask |= AFS_SETMODTIME;
2456 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
2461 /* now add in our locally generated request */
2462 if (attrp->mask & CM_ATTRMASK_CLIENTMODTIME) {
2463 statusp->ClientModTime = attrp->clientModTime;
2464 mask |= AFS_SETMODTIME;
2466 if (attrp->mask & CM_ATTRMASK_UNIXMODEBITS) {
2467 statusp->UnixModeBits = attrp->unixModeBits;
2468 mask |= AFS_SETMODE;
2470 if (attrp->mask & CM_ATTRMASK_OWNER) {
2471 statusp->Owner = attrp->owner;
2472 mask |= AFS_SETOWNER;
2474 if (attrp->mask & CM_ATTRMASK_GROUP) {
2475 statusp->Group = attrp->group;
2476 mask |= AFS_SETGROUP;
2479 statusp->Mask = mask;
2482 /* set the file size, and make sure that all relevant buffers have been
2483 * truncated. Ensure that any partially truncated buffers have been zeroed
2484 * to the end of the buffer.
2486 long cm_SetLength(cm_scache_t *scp, osi_hyper_t *sizep, cm_user_t *userp,
2492 /* start by locking out buffer creation */
2493 lock_ObtainWrite(&scp->bufCreateLock);
2495 /* verify that this is a file, not a dir or a symlink */
2496 lock_ObtainMutex(&scp->mx);
2497 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
2498 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2501 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2503 if (scp->fileType != CM_SCACHETYPE_FILE) {
2504 code = CM_ERROR_ISDIR;
2509 if (LargeIntegerLessThan(*sizep, scp->length))
2514 lock_ReleaseMutex(&scp->mx);
2516 /* can't hold scp->mx lock here, since we may wait for a storeback to
2517 * finish if the buffer package is cleaning a buffer by storing it to
2521 buf_Truncate(scp, userp, reqp, sizep);
2523 /* now ensure that file length is short enough, and update truncPos */
2524 lock_ObtainMutex(&scp->mx);
2526 /* make sure we have a callback (so we have the right value for the
2527 * length), and wait for it to be safe to do a truncate.
2529 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_WRITE,
2530 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2531 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2533 /* If we only have 'i' bits, then we should still be able to set
2534 the size of a file we created. */
2535 if (code == CM_ERROR_NOACCESS && scp->creator == userp) {
2536 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
2537 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2538 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2544 if (LargeIntegerLessThan(*sizep, scp->length)) {
2545 /* a real truncation. If truncPos is not set yet, or is bigger
2546 * than where we're truncating the file, set truncPos to this
2551 if (!(scp->mask & CM_SCACHEMASK_TRUNCPOS)
2552 || LargeIntegerLessThan(*sizep, scp->length)) {
2554 scp->truncPos = *sizep;
2555 scp->mask |= CM_SCACHEMASK_TRUNCPOS;
2557 /* in either case, the new file size has been changed */
2558 scp->length = *sizep;
2559 scp->mask |= CM_SCACHEMASK_LENGTH;
2561 else if (LargeIntegerGreaterThan(*sizep, scp->length)) {
2562 /* really extending the file */
2563 scp->length = *sizep;
2564 scp->mask |= CM_SCACHEMASK_LENGTH;
2567 /* done successfully */
2570 cm_SyncOpDone(scp, NULL,
2571 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2572 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2575 lock_ReleaseMutex(&scp->mx);
2576 lock_ReleaseWrite(&scp->bufCreateLock);
2581 /* set the file size or other attributes (but not both at once) */
2582 long cm_SetAttr(cm_scache_t *scp, cm_attr_t *attrp, cm_user_t *userp,
2586 AFSFetchStatus afsOutStatus;
2590 AFSStoreStatus afsInStatus;
2591 struct rx_connection * callp;
2593 /* handle file length setting */
2594 if (attrp->mask & CM_ATTRMASK_LENGTH)
2595 return cm_SetLength(scp, &attrp->length, userp, reqp);
2597 lock_ObtainMutex(&scp->mx);
2598 /* otherwise, we have to make an RPC to get the status */
2599 code = cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STORESTATUS);
2601 lock_ReleaseMutex(&scp->mx);
2605 /* make the attr structure */
2606 cm_StatusFromAttr(&afsInStatus, scp, attrp);
2608 tfid.Volume = scp->fid.volume;
2609 tfid.Vnode = scp->fid.vnode;
2610 tfid.Unique = scp->fid.unique;
2611 lock_ReleaseMutex(&scp->mx);
2613 /* now make the RPC */
2614 osi_Log1(afsd_logp, "CALL StoreStatus scp 0x%p", scp);
2616 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2620 callp = cm_GetRxConn(connp);
2621 code = RXAFS_StoreStatus(callp, &tfid,
2622 &afsInStatus, &afsOutStatus, &volSync);
2623 rx_PutConnection(callp);
2625 } while (cm_Analyze(connp, userp, reqp,
2626 &scp->fid, &volSync, NULL, NULL, code));
2627 code = cm_MapRPCError(code, reqp);
2630 osi_Log1(afsd_logp, "CALL StoreStatus FAILURE, code 0x%x", code);
2632 osi_Log0(afsd_logp, "CALL StoreStatus SUCCESS");
2634 lock_ObtainMutex(&scp->mx);
2635 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STORESTATUS);
2637 cm_MergeStatus(NULL, scp, &afsOutStatus, &volSync, userp,
2638 CM_MERGEFLAG_FORCE|CM_MERGEFLAG_STOREDATA);
2640 /* if we're changing the mode bits, discard the ACL cache,
2641 * since we changed the mode bits.
2643 if (afsInStatus.Mask & AFS_SETMODE)
2644 cm_FreeAllACLEnts(scp);
2645 lock_ReleaseMutex(&scp->mx);
2649 long cm_Create(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2650 cm_scache_t **scpp, cm_user_t *userp, cm_req_t *reqp)
2655 cm_callbackRequest_t cbReq;
2658 cm_scache_t *scp = NULL;
2660 AFSStoreStatus inStatus;
2661 AFSFetchStatus updatedDirStatus;
2662 AFSFetchStatus newFileStatus;
2663 AFSCallBack newFileCallback;
2665 struct rx_connection * callp;
2668 /* can't create names with @sys in them; must expand it manually first.
2669 * return "invalid request" if they try.
2671 if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2672 return CM_ERROR_ATSYS;
2675 /* before starting the RPC, mark that we're changing the file data, so
2676 * that someone who does a chmod will know to wait until our call
2679 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2680 lock_ObtainMutex(&dscp->mx);
2681 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2682 lock_ReleaseMutex(&dscp->mx);
2684 cm_StartCallbackGrantingCall(NULL, &cbReq);
2686 cm_EndDirOp(&dirop);
2693 cm_StatusFromAttr(&inStatus, NULL, attrp);
2695 /* try the RPC now */
2696 osi_Log1(afsd_logp, "CALL CreateFile scp 0x%p", dscp);
2698 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2702 dirAFSFid.Volume = dscp->fid.volume;
2703 dirAFSFid.Vnode = dscp->fid.vnode;
2704 dirAFSFid.Unique = dscp->fid.unique;
2706 callp = cm_GetRxConn(connp);
2707 code = RXAFS_CreateFile(connp->callp, &dirAFSFid, namep,
2708 &inStatus, &newAFSFid, &newFileStatus,
2709 &updatedDirStatus, &newFileCallback,
2711 rx_PutConnection(callp);
2713 } while (cm_Analyze(connp, userp, reqp,
2714 &dscp->fid, &volSync, NULL, &cbReq, code));
2715 code = cm_MapRPCError(code, reqp);
2718 osi_Log1(afsd_logp, "CALL CreateFile FAILURE, code 0x%x", code);
2720 osi_Log0(afsd_logp, "CALL CreateFile SUCCESS");
2723 lock_ObtainWrite(&dirop.scp->dirlock);
2724 dirop.lockType = CM_DIRLOCK_WRITE;
2726 lock_ObtainMutex(&dscp->mx);
2727 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2729 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2731 lock_ReleaseMutex(&dscp->mx);
2733 /* now try to create the file's entry, too, but be careful to
2734 * make sure that we don't merge in old info. Since we weren't locking
2735 * out any requests during the file's creation, we may have pretty old
2739 newFid.cell = dscp->fid.cell;
2740 newFid.volume = dscp->fid.volume;
2741 newFid.vnode = newAFSFid.Vnode;
2742 newFid.unique = newAFSFid.Unique;
2743 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2745 lock_ObtainMutex(&scp->mx);
2746 scp->creator = userp; /* remember who created it */
2747 if (!cm_HaveCallback(scp)) {
2748 cm_MergeStatus(dscp, scp, &newFileStatus, &volSync,
2750 cm_EndCallbackGrantingCall(scp, &cbReq,
2751 &newFileCallback, 0);
2754 lock_ReleaseMutex(&scp->mx);
2759 /* make sure we end things properly */
2761 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2763 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2764 cm_DirCreateEntry(&dirop, namep, &newFid);
2766 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2769 cm_EndDirOp(&dirop);
2774 long cm_FSync(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
2778 lock_ObtainWrite(&scp->bufCreateLock);
2779 code = buf_CleanVnode(scp, userp, reqp);
2780 lock_ReleaseWrite(&scp->bufCreateLock);
2782 lock_ObtainMutex(&scp->mx);
2784 if (scp->mask & (CM_SCACHEMASK_TRUNCPOS
2785 | CM_SCACHEMASK_CLIENTMODTIME
2786 | CM_SCACHEMASK_LENGTH))
2787 code = cm_StoreMini(scp, userp, reqp);
2789 if (scp->flags & (CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE)) {
2790 code = (scp->flags & CM_SCACHEFLAG_OVERQUOTA) ? CM_ERROR_QUOTA : CM_ERROR_SPACE;
2791 scp->flags &= ~(CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE);
2794 lock_ReleaseMutex(&scp->mx);
2799 long cm_MakeDir(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2800 cm_user_t *userp, cm_req_t *reqp)
2805 cm_callbackRequest_t cbReq;
2808 cm_scache_t *scp = NULL;
2810 AFSStoreStatus inStatus;
2811 AFSFetchStatus updatedDirStatus;
2812 AFSFetchStatus newDirStatus;
2813 AFSCallBack newDirCallback;
2815 struct rx_connection * callp;
2818 /* can't create names with @sys in them; must expand it manually first.
2819 * return "invalid request" if they try.
2821 if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2822 return CM_ERROR_ATSYS;
2825 /* before starting the RPC, mark that we're changing the directory
2826 * data, so that someone who does a chmod on the dir will wait until
2827 * our call completes.
2829 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2830 lock_ObtainMutex(&dscp->mx);
2831 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2832 lock_ReleaseMutex(&dscp->mx);
2834 cm_StartCallbackGrantingCall(NULL, &cbReq);
2836 cm_EndDirOp(&dirop);
2843 cm_StatusFromAttr(&inStatus, NULL, attrp);
2845 /* try the RPC now */
2846 osi_Log1(afsd_logp, "CALL MakeDir scp 0x%p", dscp);
2848 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2852 dirAFSFid.Volume = dscp->fid.volume;
2853 dirAFSFid.Vnode = dscp->fid.vnode;
2854 dirAFSFid.Unique = dscp->fid.unique;
2856 callp = cm_GetRxConn(connp);
2857 code = RXAFS_MakeDir(connp->callp, &dirAFSFid, namep,
2858 &inStatus, &newAFSFid, &newDirStatus,
2859 &updatedDirStatus, &newDirCallback,
2861 rx_PutConnection(callp);
2863 } while (cm_Analyze(connp, userp, reqp,
2864 &dscp->fid, &volSync, NULL, &cbReq, code));
2865 code = cm_MapRPCError(code, reqp);
2868 osi_Log1(afsd_logp, "CALL MakeDir FAILURE, code 0x%x", code);
2870 osi_Log0(afsd_logp, "CALL MakeDir SUCCESS");
2873 lock_ObtainWrite(&dirop.scp->dirlock);
2874 dirop.lockType = CM_DIRLOCK_WRITE;
2876 lock_ObtainMutex(&dscp->mx);
2877 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2879 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2881 lock_ReleaseMutex(&dscp->mx);
2883 /* now try to create the new dir's entry, too, but be careful to
2884 * make sure that we don't merge in old info. Since we weren't locking
2885 * out any requests during the file's creation, we may have pretty old
2889 newFid.cell = dscp->fid.cell;
2890 newFid.volume = dscp->fid.volume;
2891 newFid.vnode = newAFSFid.Vnode;
2892 newFid.unique = newAFSFid.Unique;
2893 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2895 lock_ObtainMutex(&scp->mx);
2896 if (!cm_HaveCallback(scp)) {
2897 cm_MergeStatus(dscp, scp, &newDirStatus, &volSync,
2899 cm_EndCallbackGrantingCall(scp, &cbReq,
2900 &newDirCallback, 0);
2903 lock_ReleaseMutex(&scp->mx);
2904 cm_ReleaseSCache(scp);
2908 /* make sure we end things properly */
2910 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2912 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2913 cm_DirCreateEntry(&dirop, namep, &newFid);
2915 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2918 cm_EndDirOp(&dirop);
2920 /* and return error code */
2924 long cm_Link(cm_scache_t *dscp, char *namep, cm_scache_t *sscp, long flags,
2925 cm_user_t *userp, cm_req_t *reqp)
2930 AFSFid existingAFSFid;
2931 AFSFetchStatus updatedDirStatus;
2932 AFSFetchStatus newLinkStatus;
2934 struct rx_connection * callp;
2937 if (dscp->fid.cell != sscp->fid.cell ||
2938 dscp->fid.volume != sscp->fid.volume) {
2939 return CM_ERROR_CROSSDEVLINK;
2942 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2943 lock_ObtainMutex(&dscp->mx);
2944 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2945 lock_ReleaseMutex(&dscp->mx);
2947 cm_EndDirOp(&dirop);
2952 /* try the RPC now */
2953 osi_Log1(afsd_logp, "CALL Link scp 0x%p", dscp);
2955 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2958 dirAFSFid.Volume = dscp->fid.volume;
2959 dirAFSFid.Vnode = dscp->fid.vnode;
2960 dirAFSFid.Unique = dscp->fid.unique;
2962 existingAFSFid.Volume = sscp->fid.volume;
2963 existingAFSFid.Vnode = sscp->fid.vnode;
2964 existingAFSFid.Unique = sscp->fid.unique;
2966 callp = cm_GetRxConn(connp);
2967 code = RXAFS_Link(callp, &dirAFSFid, namep, &existingAFSFid,
2968 &newLinkStatus, &updatedDirStatus, &volSync);
2969 rx_PutConnection(callp);
2970 osi_Log1(smb_logp," RXAFS_Link returns 0x%x", code);
2972 } while (cm_Analyze(connp, userp, reqp,
2973 &dscp->fid, &volSync, NULL, NULL, code));
2975 code = cm_MapRPCError(code, reqp);
2978 osi_Log1(afsd_logp, "CALL Link FAILURE, code 0x%x", code);
2980 osi_Log0(afsd_logp, "CALL Link SUCCESS");
2983 lock_ObtainWrite(&dirop.scp->dirlock);
2984 dirop.lockType = CM_DIRLOCK_WRITE;
2986 lock_ObtainMutex(&dscp->mx);
2987 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2989 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2991 lock_ReleaseMutex(&dscp->mx);
2994 if (cm_CheckDirOpForSingleChange(&dirop)) {
2995 cm_DirCreateEntry(&dirop, namep, &sscp->fid);
2997 cm_BPlusDirCreateEntry(&dirop, namep, &sscp->fid);
3001 cm_EndDirOp(&dirop);
3006 long cm_SymLink(cm_scache_t *dscp, char *namep, char *contentsp, long flags,
3007 cm_attr_t *attrp, cm_user_t *userp, cm_req_t *reqp)
3015 AFSStoreStatus inStatus;
3016 AFSFetchStatus updatedDirStatus;
3017 AFSFetchStatus newLinkStatus;
3019 struct rx_connection * callp;
3022 /* before starting the RPC, mark that we're changing the directory data,
3023 * so that someone who does a chmod on the dir will wait until our
3026 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3027 lock_ObtainMutex(&dscp->mx);
3028 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3029 lock_ReleaseMutex(&dscp->mx);
3031 cm_EndDirOp(&dirop);
3036 cm_StatusFromAttr(&inStatus, NULL, attrp);
3038 /* try the RPC now */
3039 osi_Log1(afsd_logp, "CALL Symlink scp 0x%p", dscp);
3041 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3045 dirAFSFid.Volume = dscp->fid.volume;
3046 dirAFSFid.Vnode = dscp->fid.vnode;
3047 dirAFSFid.Unique = dscp->fid.unique;
3049 callp = cm_GetRxConn(connp);
3050 code = RXAFS_Symlink(callp, &dirAFSFid, namep, contentsp,
3051 &inStatus, &newAFSFid, &newLinkStatus,
3052 &updatedDirStatus, &volSync);
3053 rx_PutConnection(callp);
3055 } while (cm_Analyze(connp, userp, reqp,
3056 &dscp->fid, &volSync, NULL, NULL, code));
3057 code = cm_MapRPCError(code, reqp);
3060 osi_Log1(afsd_logp, "CALL Symlink FAILURE, code 0x%x", code);
3062 osi_Log0(afsd_logp, "CALL Symlink SUCCESS");
3065 lock_ObtainWrite(&dirop.scp->dirlock);
3066 dirop.lockType = CM_DIRLOCK_WRITE;
3068 lock_ObtainMutex(&dscp->mx);
3069 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3071 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3073 lock_ReleaseMutex(&dscp->mx);
3076 if (cm_CheckDirOpForSingleChange(&dirop)) {
3077 newFid.cell = dscp->fid.cell;
3078 newFid.volume = dscp->fid.volume;
3079 newFid.vnode = newAFSFid.Vnode;
3080 newFid.unique = newAFSFid.Unique;
3082 cm_DirCreateEntry(&dirop, namep, &newFid);
3084 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
3088 cm_EndDirOp(&dirop);
3090 /* now try to create the new dir's entry, too, but be careful to
3091 * make sure that we don't merge in old info. Since we weren't locking
3092 * out any requests during the file's creation, we may have pretty old
3096 newFid.cell = dscp->fid.cell;
3097 newFid.volume = dscp->fid.volume;
3098 newFid.vnode = newAFSFid.Vnode;
3099 newFid.unique = newAFSFid.Unique;
3100 code = cm_GetSCache(&newFid, &scp, userp, reqp);
3102 lock_ObtainMutex(&scp->mx);
3103 if (!cm_HaveCallback(scp)) {
3104 cm_MergeStatus(dscp, scp, &newLinkStatus, &volSync,
3107 lock_ReleaseMutex(&scp->mx);
3108 cm_ReleaseSCache(scp);
3112 /* and return error code */
3116 long cm_RemoveDir(cm_scache_t *dscp, char *namep, cm_user_t *userp,
3123 AFSFetchStatus updatedDirStatus;
3125 struct rx_connection * callp;
3128 /* before starting the RPC, mark that we're changing the directory data,
3129 * so that someone who does a chmod on the dir will wait until our
3132 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3133 lock_ObtainMutex(&dscp->mx);
3134 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3135 lock_ReleaseMutex(&dscp->mx);
3137 cm_EndDirOp(&dirop);
3142 /* try the RPC now */
3143 osi_Log1(afsd_logp, "CALL RemoveDir scp 0x%p", dscp);
3145 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3149 dirAFSFid.Volume = dscp->fid.volume;
3150 dirAFSFid.Vnode = dscp->fid.vnode;
3151 dirAFSFid.Unique = dscp->fid.unique;
3153 callp = cm_GetRxConn(connp);
3154 code = RXAFS_RemoveDir(callp, &dirAFSFid, namep,
3155 &updatedDirStatus, &volSync);
3156 rx_PutConnection(callp);
3158 } while (cm_Analyze(connp, userp, reqp,
3159 &dscp->fid, &volSync, NULL, NULL, code));
3160 code = cm_MapRPCErrorRmdir(code, reqp);
3163 osi_Log1(afsd_logp, "CALL RemoveDir FAILURE, code 0x%x", code);
3165 osi_Log0(afsd_logp, "CALL RemoveDir SUCCESS");
3168 lock_ObtainWrite(&dirop.scp->dirlock);
3169 dirop.lockType = CM_DIRLOCK_WRITE;
3171 lock_ObtainMutex(&dscp->mx);
3172 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3174 cm_dnlcRemove(dscp, namep);
3175 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3177 lock_ReleaseMutex(&dscp->mx);
3180 if (cm_CheckDirOpForSingleChange(&dirop)) {
3181 cm_DirDeleteEntry(&dirop, namep);
3183 cm_BPlusDirDeleteEntry(&dirop, namep);
3187 cm_EndDirOp(&dirop);
3189 /* and return error code */
3193 long cm_Open(cm_scache_t *scp, int type, cm_user_t *userp)
3195 /* grab mutex on contents */
3196 lock_ObtainMutex(&scp->mx);
3198 /* reset the prefetch info */
3199 scp->prefetch.base.LowPart = 0; /* base */
3200 scp->prefetch.base.HighPart = 0;
3201 scp->prefetch.end.LowPart = 0; /* and end */
3202 scp->prefetch.end.HighPart = 0;
3204 /* release mutex on contents */
3205 lock_ReleaseMutex(&scp->mx);
3211 long cm_Rename(cm_scache_t *oldDscp, char *oldNamep, cm_scache_t *newDscp,
3212 char *newNamep, cm_user_t *userp, cm_req_t *reqp)
3216 AFSFid oldDirAFSFid;
3217 AFSFid newDirAFSFid;
3219 AFSFetchStatus updatedOldDirStatus;
3220 AFSFetchStatus updatedNewDirStatus;
3223 struct rx_connection * callp;
3224 cm_dirOp_t oldDirOp;
3227 cm_dirOp_t newDirOp;
3229 /* before starting the RPC, mark that we're changing the directory data,
3230 * so that someone who does a chmod on the dir will wait until our call
3231 * completes. We do this in vnode order so that we don't deadlock,
3232 * which makes the code a little verbose.
3234 if (oldDscp == newDscp) {
3235 /* check for identical names */
3236 if (strcmp(oldNamep, newNamep) == 0)
3237 return CM_ERROR_RENAME_IDENTICAL;
3240 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3241 lock_ObtainMutex(&oldDscp->mx);
3242 cm_dnlcRemove(oldDscp, oldNamep);
3243 cm_dnlcRemove(oldDscp, newNamep);
3244 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3245 CM_SCACHESYNC_STOREDATA);
3246 lock_ReleaseMutex(&oldDscp->mx);
3248 cm_EndDirOp(&oldDirOp);
3252 /* two distinct dir vnodes */
3254 if (oldDscp->fid.cell != newDscp->fid.cell ||
3255 oldDscp->fid.volume != newDscp->fid.volume)
3256 return CM_ERROR_CROSSDEVLINK;
3258 /* shouldn't happen that we have distinct vnodes for two
3259 * different files, but could due to deliberate attack, or
3260 * stale info. Avoid deadlocks and quit now.
3262 if (oldDscp->fid.vnode == newDscp->fid.vnode)
3263 return CM_ERROR_CROSSDEVLINK;
3265 if (oldDscp->fid.vnode < newDscp->fid.vnode) {
3266 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3267 lock_ObtainMutex(&oldDscp->mx);
3268 cm_dnlcRemove(oldDscp, oldNamep);
3269 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3270 CM_SCACHESYNC_STOREDATA);
3271 lock_ReleaseMutex(&oldDscp->mx);
3273 cm_EndDirOp(&oldDirOp);
3275 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3276 lock_ObtainMutex(&newDscp->mx);
3277 cm_dnlcRemove(newDscp, newNamep);
3278 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3279 CM_SCACHESYNC_STOREDATA);
3280 lock_ReleaseMutex(&newDscp->mx);
3282 cm_EndDirOp(&newDirOp);
3284 /* cleanup first one */
3285 lock_ObtainMutex(&oldDscp->mx);
3286 cm_SyncOpDone(oldDscp, NULL,
3287 CM_SCACHESYNC_STOREDATA);
3288 lock_ReleaseMutex(&oldDscp->mx);
3289 cm_EndDirOp(&oldDirOp);
3294 /* lock the new vnode entry first */
3295 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3296 lock_ObtainMutex(&newDscp->mx);
3297 cm_dnlcRemove(newDscp, newNamep);
3298 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3299 CM_SCACHESYNC_STOREDATA);
3300 lock_ReleaseMutex(&newDscp->mx);
3302 cm_EndDirOp(&newDirOp);
3304 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3305 lock_ObtainMutex(&oldDscp->mx);
3306 cm_dnlcRemove(oldDscp, oldNamep);
3307 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3308 CM_SCACHESYNC_STOREDATA);
3309 lock_ReleaseMutex(&oldDscp->mx);
3311 cm_EndDirOp(&oldDirOp);
3313 /* cleanup first one */
3314 lock_ObtainMutex(&newDscp->mx);
3315 cm_SyncOpDone(newDscp, NULL,
3316 CM_SCACHESYNC_STOREDATA);
3317 lock_ReleaseMutex(&newDscp->mx);
3318 cm_EndDirOp(&newDirOp);
3322 } /* two distinct vnodes */
3329 /* try the RPC now */
3330 osi_Log2(afsd_logp, "CALL Rename old scp 0x%p new scp 0x%p",
3333 code = cm_ConnFromFID(&oldDscp->fid, userp, reqp, &connp);
3337 oldDirAFSFid.Volume = oldDscp->fid.volume;
3338 oldDirAFSFid.Vnode = oldDscp->fid.vnode;
3339 oldDirAFSFid.Unique = oldDscp->fid.unique;
3340 newDirAFSFid.Volume = newDscp->fid.volume;
3341 newDirAFSFid.Vnode = newDscp->fid.vnode;
3342 newDirAFSFid.Unique = newDscp->fid.unique;
3344 callp = cm_GetRxConn(connp);
3345 code = RXAFS_Rename(callp, &oldDirAFSFid, oldNamep,
3346 &newDirAFSFid, newNamep,
3347 &updatedOldDirStatus, &updatedNewDirStatus,
3349 rx_PutConnection(callp);
3351 } while (cm_Analyze(connp, userp, reqp, &oldDscp->fid,
3352 &volSync, NULL, NULL, code));
3353 code = cm_MapRPCError(code, reqp);
3356 osi_Log1(afsd_logp, "CALL Rename FAILURE, code 0x%x", code);
3358 osi_Log0(afsd_logp, "CALL Rename SUCCESS");
3360 /* update the individual stat cache entries for the directories */
3362 lock_ObtainWrite(&oldDirOp.scp->dirlock);
3363 oldDirOp.lockType = CM_DIRLOCK_WRITE;
3365 lock_ObtainMutex(&oldDscp->mx);
3366 cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_STOREDATA);
3369 cm_MergeStatus(NULL, oldDscp, &updatedOldDirStatus, &volSync,
3370 userp, CM_MERGEFLAG_DIROP);
3371 lock_ReleaseMutex(&oldDscp->mx);
3374 if (cm_CheckDirOpForSingleChange(&oldDirOp)) {
3377 diropCode = cm_BPlusDirLookup(&oldDirOp, oldNamep, &fileFid);
3378 if (diropCode == CM_ERROR_INEXACT_MATCH)
3380 else if (diropCode == EINVAL)
3382 diropCode = cm_DirLookup(&oldDirOp, oldNamep, &fileFid);
3384 if (diropCode == 0) {
3386 diropCode = cm_DirCreateEntry(&oldDirOp, newNamep, &fileFid);
3388 cm_BPlusDirCreateEntry(&oldDirOp, newNamep, &fileFid);
3392 if (diropCode == 0) {
3393 diropCode = cm_DirDeleteEntry(&oldDirOp, oldNamep);
3395 cm_BPlusDirDeleteEntry(&oldDirOp, oldNamep);
3401 cm_EndDirOp(&oldDirOp);
3403 /* and update it for the new one, too, if necessary */
3406 lock_ObtainWrite(&newDirOp.scp->dirlock);
3407 newDirOp.lockType = CM_DIRLOCK_WRITE;
3409 lock_ObtainMutex(&newDscp->mx);
3410 cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_STOREDATA);
3412 cm_MergeStatus(NULL, newDscp, &updatedNewDirStatus, &volSync,
3413 userp, CM_MERGEFLAG_DIROP);
3414 lock_ReleaseMutex(&newDscp->mx);
3417 /* we only make the local change if we successfully made
3418 the change in the old directory AND there was only one
3419 change in the new directory */
3420 if (diropCode == 0 && cm_CheckDirOpForSingleChange(&newDirOp)) {
3421 cm_DirCreateEntry(&newDirOp, newNamep, &fileFid);
3423 cm_BPlusDirCreateEntry(&newDirOp, newNamep, &fileFid);
3427 cm_EndDirOp(&newDirOp);
3430 /* and return error code */
3434 /* Byte range locks:
3436 The OpenAFS Windows client has to fake byte range locks given no
3437 server side support for such locks. This is implemented as keyed
3438 byte range locks on the cache manager.
3440 Keyed byte range locks:
3442 Each cm_scache_t structure keeps track of a list of keyed locks.
3443 The key for a lock identifies an owner of a set of locks (referred
3444 to as a client). Each key is represented by a value. The set of
3445 key values used within a specific cm_scache_t structure form a
3446 namespace that has a scope of just that cm_scache_t structure. The
3447 same key value can be used with another cm_scache_t structure and
3448 correspond to a completely different client. However it is
3449 advantageous for the SMB or IFS layer to make sure that there is a
3450 1-1 mapping between client and keys over all cm_scache_t objects.
3452 Assume a client C has key Key(C) (although, since the scope of the
3453 key is a cm_scache_t, the key can be Key(C,S), where S is the
3454 cm_scache_t. But assume a 1-1 relation between keys and clients).
3455 A byte range (O,+L) denotes byte addresses (O) through (O+L-1)
3456 inclusive (a.k.a. [O,O+L-1]). The function&nb