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);
442 osi_Log3(afsd_logp,"cm_CheckNTOpen scp 0x%p ldp 0x%p code 0x%x", scp, *ldpp, code);
446 extern long cm_CheckNTOpenDone(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
447 cm_lock_data_t ** ldpp)
449 osi_Log2(afsd_logp,"cm_CheckNTOpenDone scp 0x%p ldp 0x%p", scp, *ldpp);
451 lock_ObtainMutex(&scp->mx);
452 cm_Unlock(scp, (*ldpp)->sLockType, (*ldpp)->LOffset, (*ldpp)->LLength,
453 (*ldpp)->key, userp, reqp);
454 lock_ReleaseMutex(&scp->mx);
461 * When CAP_NT_SMBS has been negotiated, deletion (of files or directories) is
462 * done in three steps:
463 * (1) open for deletion (NT_CREATE_AND_X)
464 * (2) set for deletion on close (NT_TRANSACTION2, SET_FILE_INFO)
466 * We must not do the RPC until step 3. But if we are going to return an error
467 * code (e.g. directory not empty), we must return it by step 2, otherwise most
468 * clients will not notice it. So we do a preliminary check. For deleting
469 * files, this is almost free, since we have already done the RPC to get the
470 * parent directory's status bits. But for deleting directories, we must do an
471 * additional RPC to get the directory's data to check if it is empty. Sigh.
473 long cm_CheckNTDelete(cm_scache_t *dscp, cm_scache_t *scp, cm_user_t *userp,
479 cm_dirEntry_t *dep = 0;
480 unsigned short *hashTable;
482 int BeyondPage = 0, HaveDot = 0, HaveDotDot = 0;
484 /* First check permissions */
485 lock_ObtainMutex(&scp->mx);
486 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_DELETE,
487 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
488 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
489 lock_ReleaseMutex(&scp->mx);
493 /* If deleting directory, must be empty */
495 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
498 thyper.HighPart = 0; thyper.LowPart = 0;
499 code = buf_Get(scp, &thyper, &bufferp);
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 code = buf_Get(scp, &thyper, &bufferp);
735 /* if buf_Get() fails we do not have a buffer object to lock */
741 /* for the IFS version, we bulkstat the dirents because this
742 routine is used in place of smb_ReceiveCoreSearchDir. our
743 other option is to modify smb_ReceiveCoreSearchDir itself,
744 but this seems to be the proper use for cm_ApplyDir. */
745 lock_ObtainMutex(&scp->mx);
746 if ((scp->flags & CM_SCACHEFLAG_BULKSTATTING) == 0
747 && (scp->bulkStatProgress.QuadPart <= thyper.QuadPart))
749 scp->flags |= CM_SCACHEFLAG_BULKSTATTING;
750 code = cm_TryBulkStat(scp, &thyper, userp, reqp);
751 scp->flags &= ~CM_SCACHEFLAG_BULKSTATTING;
752 scp->bulkStatProgress = thyper;
754 lock_ReleaseMutex(&scp->mx);
757 lock_ObtainMutex(&bufferp->mx);
758 bufferOffset = thyper;
760 /* now get the data in the cache */
762 lock_ObtainMutex(&scp->mx);
763 code = cm_SyncOp(scp, bufferp, userp, reqp,
765 CM_SCACHESYNC_NEEDCALLBACK
767 | CM_SCACHESYNC_BUFLOCKED);
769 lock_ReleaseMutex(&scp->mx);
772 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
774 if (cm_HaveBuffer(scp, bufferp, 1)) {
775 lock_ReleaseMutex(&scp->mx);
779 /* otherwise, load the buffer and try again */
780 lock_ReleaseMutex(&bufferp->mx);
781 code = cm_GetBuffer(scp, bufferp, NULL, userp,
783 lock_ReleaseMutex(&scp->mx);
784 lock_ObtainMutex(&bufferp->mx);
789 lock_ReleaseMutex(&bufferp->mx);
790 buf_Release(bufferp);
794 } /* if (wrong buffer) ... */
796 /* now we have the buffer containing the entry we're interested
797 * in; copy it out if it represents a non-deleted entry.
799 entryInDir = curOffset.LowPart & (2048-1);
800 entryInBuffer = curOffset.LowPart & (cm_data.buf_blockSize - 1);
802 /* page header will help tell us which entries are free. Page
803 * header can change more often than once per buffer, since
804 * AFS 3 dir page size may be less than (but not more than) a
805 * buffer package buffer.
807 /* only look intra-buffer */
808 temp = curOffset.LowPart & (cm_data.buf_blockSize - 1);
809 temp &= ~(2048 - 1); /* turn off intra-page bits */
810 pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
812 /* now determine which entry we're looking at in the page. If
813 * it is free (there's a free bitmap at the start of the dir),
814 * we should skip these 32 bytes.
816 slotInPage = (entryInDir & 0x7e0) >> 5;
817 if (!(pageHeaderp->freeBitmap[slotInPage>>3]
818 & (1 << (slotInPage & 0x7)))) {
819 /* this entry is free */
820 numDirChunks = 1; /* only skip this guy */
824 tp = bufferp->datap + entryInBuffer;
825 dep = (cm_dirEntry_t *) tp; /* now points to AFS3 dir entry */
827 /* while we're here, compute the next entry's location, too,
828 * since we'll need it when writing out the cookie into the
829 * dir listing stream.
831 numDirChunks = cm_NameEntries(dep->name, NULL);
833 /* compute the offset of the cookie representing the next entry */
834 nextEntryCookie = curOffset.LowPart
835 + (CM_DIR_CHUNKSIZE * numDirChunks);
837 if (dep->fid.vnode != 0) {
838 /* this is one of the entries to use: it is not deleted */
839 code = (*funcp)(scp, dep, parmp, &curOffset);
842 } /* if we're including this name */
845 /* and adjust curOffset to be where the new cookie is */
847 thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
848 curOffset = LargeIntegerAdd(thyper, curOffset);
849 } /* while copying data for dir listing */
851 /* release the mutex */
853 lock_ReleaseMutex(&bufferp->mx);
854 buf_Release(bufferp);
859 int cm_NoneUpper(char *s)
863 if (c >= 'A' && c <= 'Z')
868 int cm_NoneLower(char *s)
872 if (c >= 'a' && c <= 'z')
877 long cm_LookupSearchProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
880 cm_lookupSearch_t *sp;
885 sp = (cm_lookupSearch_t *) rockp;
887 matchName = dep->name;
889 match = cm_stricmp(matchName, sp->searchNamep);
891 match = strcmp(matchName, sp->searchNamep);
895 && !cm_Is8Dot3(dep->name)) {
896 matchName = shortName;
897 cm_Gen8Dot3Name(dep, shortName, NULL);
899 match = cm_stricmp(matchName, sp->searchNamep);
901 match = strcmp(matchName, sp->searchNamep);
911 if (!sp->caseFold || matchName == shortName) {
912 sp->fid.vnode = ntohl(dep->fid.vnode);
913 sp->fid.unique = ntohl(dep->fid.unique);
914 return CM_ERROR_STOPNOW;
918 * If we get here, we are doing a case-insensitive search, and we
919 * have found a match. Now we determine what kind of match it is:
920 * exact, lower-case, upper-case, or none of the above. This is done
921 * in order to choose among matches, if there are more than one.
924 /* Exact matches are the best. */
925 match = strcmp(matchName, sp->searchNamep);
928 sp->fid.vnode = ntohl(dep->fid.vnode);
929 sp->fid.unique = ntohl(dep->fid.unique);
930 return CM_ERROR_STOPNOW;
933 /* Lower-case matches are next. */
936 if (cm_NoneUpper(matchName)) {
941 /* Upper-case matches are next. */
944 if (cm_NoneLower(matchName)) {
949 /* General matches are last. */
955 sp->fid.vnode = ntohl(dep->fid.vnode);
956 sp->fid.unique = ntohl(dep->fid.unique);
960 /* read the contents of a mount point into the appropriate string.
961 * called with locked scp, and returns with locked scp.
963 long cm_ReadMountPoint(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
970 if (scp->mountPointStringp[0])
973 /* otherwise, we have to read it in */
974 lock_ReleaseMutex(&scp->mx);
976 thyper.LowPart = thyper.HighPart = 0;
977 code = buf_Get(scp, &thyper, &bufp);
979 lock_ObtainMutex(&scp->mx);
984 code = cm_SyncOp(scp, bufp, userp, reqp, 0,
985 CM_SCACHESYNC_READ | CM_SCACHESYNC_NEEDCALLBACK);
989 cm_SyncOpDone(scp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
991 if (cm_HaveBuffer(scp, bufp, 0))
994 /* otherwise load buffer */
995 code = cm_GetBuffer(scp, bufp, NULL, userp, reqp);
999 /* locked, has callback, has valid data in buffer */
1000 if ((tlen = scp->length.LowPart) > MOUNTPOINTLEN - 1)
1001 return CM_ERROR_TOOBIG;
1003 code = CM_ERROR_INVAL;
1007 /* someone else did the work while we were out */
1008 if (scp->mountPointStringp[0]) {
1013 /* otherwise, copy out the link */
1014 memcpy(scp->mountPointStringp, bufp->datap, tlen);
1016 /* now make it null-terminated. Note that the original contents of a
1017 * link that is a mount point is "#volname." where "." is there just to
1018 * be turned into a null. That is, we can trash the last char of the
1019 * link without damaging the vol name. This is a stupid convention,
1020 * but that's the protocol.
1022 scp->mountPointStringp[tlen-1] = 0;
1032 /* called with a locked scp and chases the mount point, yielding outScpp.
1033 * scp remains locked, just for simplicity of describing the interface.
1035 long cm_FollowMountPoint(cm_scache_t *scp, cm_scache_t *dscp, cm_user_t *userp,
1036 cm_req_t *reqp, cm_scache_t **outScpp)
1044 cm_volume_t *volp = NULL;
1051 if (scp->mountRootFid.cell != 0 && scp->mountRootGen >= cm_data.mountRootGen) {
1052 tfid = scp->mountRootFid;
1053 lock_ReleaseMutex(&scp->mx);
1054 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1055 lock_ObtainMutex(&scp->mx);
1059 /* parse the volume name */
1060 mpNamep = scp->mountPointStringp;
1062 return CM_ERROR_NOSUCHPATH;
1063 tlen = (int)strlen(scp->mountPointStringp);
1064 mtType = *scp->mountPointStringp;
1065 cellNamep = malloc(tlen);
1066 volNamep = malloc(tlen);
1068 cp = strrchr(mpNamep, ':');
1070 /* cellular mount point */
1071 memset(cellNamep, 0, tlen);
1072 strncpy(cellNamep, mpNamep+1, cp - mpNamep - 1);
1073 strcpy(volNamep, cp+1);
1074 /* now look up the cell */
1075 lock_ReleaseMutex(&scp->mx);
1076 cellp = cm_GetCell(cellNamep, CM_FLAG_CREATE);
1077 lock_ObtainMutex(&scp->mx);
1081 strcpy(volNamep, mpNamep+1);
1083 cellp = cm_FindCellByID(scp->fid.cell, 0);
1087 code = CM_ERROR_NOSUCHCELL;
1091 vnLength = strlen(volNamep);
1092 if (vnLength >= 8 && strcmp(volNamep + vnLength - 7, ".backup") == 0)
1093 targetType = BACKVOL;
1094 else if (vnLength >= 10
1095 && strcmp(volNamep + vnLength - 9, ".readonly") == 0)
1100 /* check for backups within backups */
1101 if (targetType == BACKVOL
1102 && (scp->flags & (CM_SCACHEFLAG_RO | CM_SCACHEFLAG_PURERO))
1103 == CM_SCACHEFLAG_RO) {
1104 code = CM_ERROR_NOSUCHVOLUME;
1108 /* now we need to get the volume */
1109 lock_ReleaseMutex(&scp->mx);
1110 if (cm_VolNameIsID(volNamep)) {
1111 code = cm_GetVolumeByID(cellp, atoi(volNamep), userp, reqp,
1112 CM_GETVOL_FLAG_CREATE, &volp);
1114 code = cm_GetVolumeByName(cellp, volNamep, userp, reqp,
1115 CM_GETVOL_FLAG_CREATE, &volp);
1117 lock_ObtainMutex(&scp->mx);
1120 afs_uint32 cell, volume;
1122 /* save the parent of the volume root for this is the
1123 * place where the volume is mounted and we must remember
1124 * this in the volume structure rather than just in the
1125 * scache entry lest the scache entry gets recycled
1128 lock_ObtainMutex(&volp->mx);
1129 volp->dotdotFid = dscp->fid;
1130 lock_ReleaseMutex(&volp->mx);
1132 cell = cellp->cellID;
1134 /* if the mt pt originates in a .backup volume (not a .readonly)
1135 * and FollowBackupPath is active, and if there is a .backup
1136 * volume for the target, then use the .backup of the target
1137 * instead of the read-write.
1139 if (cm_followBackupPath &&
1141 (dscp->flags & (CM_SCACHEFLAG_RO|CM_SCACHEFLAG_PURERO)) == CM_SCACHEFLAG_RO &&
1142 (targetType == RWVOL || targetType == ROVOL && volp->ro.ID == 0)
1144 targetType = BACKVOL;
1146 /* if the mt pt is in a read-only volume (not just a
1147 * backup), and if there is a read-only volume for the
1148 * target, and if this is a targetType '#' mount point, use
1149 * the read-only, otherwise use the one specified.
1151 else if (mtType == '#' && targetType == RWVOL &&
1152 (scp->flags & CM_SCACHEFLAG_PURERO) &&
1156 if (targetType == ROVOL)
1157 volume = volp->ro.ID;
1158 else if (targetType == BACKVOL)
1159 volume = volp->bk.ID;
1161 volume = volp->rw.ID;
1163 /* the rest of the fid is a magic number */
1164 cm_SetFid(&scp->mountRootFid, cell, volume, 1, 1);
1165 scp->mountRootGen = cm_data.mountRootGen;
1167 tfid = scp->mountRootFid;
1168 lock_ReleaseMutex(&scp->mx);
1169 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1170 lock_ObtainMutex(&scp->mx);
1181 long cm_LookupInternal(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1182 cm_req_t *reqp, cm_scache_t **outpScpp)
1185 int dnlcHit = 1; /* did we hit in the dnlc? yes, we did */
1186 cm_scache_t *tscp = NULL;
1187 cm_scache_t *mountedScp;
1188 cm_lookupSearch_t rock;
1191 memset(&rock, 0, sizeof(rock));
1193 if (dscp->fid.vnode == 1 && dscp->fid.unique == 1
1194 && strcmp(namep, "..") == 0) {
1195 if (dscp->dotdotFid.volume == 0)
1196 return CM_ERROR_NOSUCHVOLUME;
1197 rock.fid = dscp->dotdotFid;
1199 } else if (strcmp(namep, ".") == 0) {
1200 rock.fid = dscp->fid;
1204 if (flags & CM_FLAG_NOMOUNTCHASE) {
1205 /* In this case, we should go and call cm_Dir* functions
1206 directly since the following cm_ApplyDir() function will
1214 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ, &dirop);
1217 code = cm_BPlusDirLookup(&dirop, namep, &rock.fid);
1222 code = cm_DirLookup(&dirop, namep, &rock.fid);
1224 cm_EndDirOp(&dirop);
1234 if (code == CM_ERROR_INEXACT_MATCH && (flags & CM_FLAG_CASEFOLD)) {
1241 return CM_ERROR_BPLUS_NOMATCH;
1246 rock.fid.cell = dscp->fid.cell;
1247 rock.fid.volume = dscp->fid.volume;
1248 rock.searchNamep = namep;
1249 rock.caseFold = (flags & CM_FLAG_CASEFOLD);
1250 rock.hasTilde = ((strchr(namep, '~') != NULL) ? 1 : 0);
1252 /* If NOMOUNTCHASE, bypass DNLC by passing NULL scp pointer */
1253 code = cm_ApplyDir(dscp, cm_LookupSearchProc, &rock, NULL, userp, reqp,
1254 (flags & CM_FLAG_NOMOUNTCHASE) ? NULL : &tscp);
1256 /* code == 0 means we fell off the end of the dir, while stopnow means
1257 * that we stopped early, probably because we found the entry we're
1258 * looking for. Any other non-zero code is an error.
1260 if (code && code != CM_ERROR_STOPNOW) {
1261 /* if the cm_scache_t we are searching in is not a directory
1262 * we must return path not found because the error
1263 * is to describe the final component not an intermediary
1265 if (code == CM_ERROR_NOTDIR) {
1266 if (flags & CM_FLAG_CHECKPATH)
1267 return CM_ERROR_NOSUCHPATH;
1269 return CM_ERROR_NOSUCHFILE;
1274 getroot = (dscp==cm_data.rootSCachep) ;
1276 if (!cm_freelanceEnabled || !getroot) {
1277 if (flags & CM_FLAG_CHECKPATH)
1278 return CM_ERROR_NOSUCHPATH;
1280 return CM_ERROR_NOSUCHFILE;
1282 else if (!strchr(namep, '#') && !strchr(namep, '%') &&
1283 strcmp(namep, "srvsvc") && strcmp(namep, "wkssvc") &&
1284 strcmp(namep, "ipc$")) {
1285 /* 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));
1293 * There is an ugly behavior where a share name "foo" will be searched
1294 * for as "fo". If the searched for name differs by an already existing
1295 * symlink or mount point in the Freelance directory, do not add the
1296 * new value automatically.
1300 if (namep[0] == '.') {
1301 if (cm_GetCell_Gen(&namep[1], &fullname[1], CM_FLAG_CREATE)) {
1303 if (!cm_FreelanceMountPointExists(fullname, 0))
1304 code = cm_FreelanceAddMount(fullname, &fullname[1], "root.cell.", 1, &rock.fid);
1305 if ( stricmp(&namep[1], &fullname[1]) &&
1306 !cm_FreelanceMountPointExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0) &&
1307 !cm_FreelanceSymlinkExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0))
1308 code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1311 if (cm_GetCell_Gen(namep, fullname, CM_FLAG_CREATE)) {
1313 if (!cm_FreelanceMountPointExists(fullname, 0))
1314 code = cm_FreelanceAddMount(fullname, fullname, "root.cell.", 0, &rock.fid);
1315 if ( stricmp(namep, fullname) &&
1316 !cm_FreelanceMountPointExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0) &&
1317 !cm_FreelanceSymlinkExists(namep, flags & CM_FLAG_DFS_REFERRAL ? 1 : 0))
1318 code = cm_FreelanceAddSymlink(namep, fullname, &rock.fid);
1321 if (!found || code < 0) { /* add mount point failed, so give up */
1322 if (flags & CM_FLAG_CHECKPATH)
1323 return CM_ERROR_NOSUCHPATH;
1325 return CM_ERROR_NOSUCHFILE;
1327 tscp = NULL; /* to force call of cm_GetSCache */
1332 if ( !tscp ) /* we did not find it in the dnlc */
1335 code = cm_GetSCache(&rock.fid, &tscp, userp, reqp);
1339 /* tscp is now held */
1341 lock_ObtainMutex(&tscp->mx);
1342 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1343 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1345 lock_ReleaseMutex(&tscp->mx);
1346 cm_ReleaseSCache(tscp);
1349 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1350 /* tscp is now locked */
1352 if (!(flags & CM_FLAG_NOMOUNTCHASE)
1353 && tscp->fileType == CM_SCACHETYPE_MOUNTPOINT) {
1354 /* mount points are funny: they have a volume name to mount
1357 code = cm_ReadMountPoint(tscp, userp, reqp);
1359 code = cm_FollowMountPoint(tscp, dscp, userp, reqp,
1361 lock_ReleaseMutex(&tscp->mx);
1362 cm_ReleaseSCache(tscp);
1369 lock_ReleaseMutex(&tscp->mx);
1372 /* copy back pointer */
1375 /* insert scache in dnlc */
1376 if ( !dnlcHit && !(flags & CM_FLAG_NOMOUNTCHASE) && rock.ExactFound ) {
1377 /* lock the directory entry to prevent racing callback revokes */
1378 lock_ObtainMutex(&dscp->mx);
1379 if ( dscp->cbServerp != NULL && dscp->cbExpires > 0 )
1380 cm_dnlcEnter(dscp, namep, tscp);
1381 lock_ReleaseMutex(&dscp->mx);
1388 int cm_ExpandSysName(char *inp, char *outp, long outSize, unsigned int index)
1393 tp = strrchr(inp, '@');
1395 return 0; /* no @sys */
1397 if (strcmp(tp, "@sys") != 0)
1398 return 0; /* no @sys */
1400 /* caller just wants to know if this is a valid @sys type of name */
1404 if (index >= MAXNUMSYSNAMES)
1407 /* otherwise generate the properly expanded @sys name */
1408 prefixCount = (int)(tp - inp);
1410 strncpy(outp, inp, prefixCount); /* copy out "a." from "a.@sys" */
1411 outp[prefixCount] = 0; /* null terminate the "a." */
1412 strcat(outp, cm_sysNameList[index]);/* append i386_nt40 */
1416 long cm_EvaluateVolumeReference(char * namep, long flags, cm_user_t * userp,
1417 cm_req_t *reqp, cm_scache_t ** outpScpp)
1420 char cellName[CELL_MAXNAMELEN];
1421 char volumeName[VL_MAXNAMELEN];
1426 cm_cell_t * cellp = NULL;
1427 cm_volume_t * volp = NULL;
1431 int mountType = RWVOL;
1433 osi_Log1(afsd_logp, "cm_EvaluateVolumeReference for string [%s]",
1434 osi_LogSaveString(afsd_logp, namep));
1436 if (strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) != 0) {
1437 goto _exit_invalid_path;
1440 /* namep is assumed to look like the following:
1442 @vol:<cellname>%<volume>\0
1444 @vol:<cellname>#<volume>\0
1448 cp = namep + CM_PREFIX_VOL_CCH; /* cp points to cell name, hopefully */
1449 tp = strchr(cp, '%');
1451 tp = strchr(cp, '#');
1453 (len = tp - cp) == 0 ||
1454 len > CELL_MAXNAMELEN)
1455 goto _exit_invalid_path;
1456 strncpy(cellName, cp, len);
1457 cellName[len] = '\0';
1462 cp = tp+1; /* cp now points to volume, supposedly */
1463 strncpy(volumeName, cp, VL_MAXNAMELEN-1);
1464 volumeName[VL_MAXNAMELEN - 1] = 0;
1466 /* OK, now we have the cell and the volume */
1467 osi_Log2(afsd_logp, " Found cell [%s] and volume [%s]",
1468 osi_LogSaveString(afsd_logp, cellName),
1469 osi_LogSaveString(afsd_logp, volumeName));
1471 cellp = cm_GetCell(cellName, CM_FLAG_CREATE);
1472 if (cellp == NULL) {
1473 goto _exit_invalid_path;
1476 len = strlen(volumeName);
1477 if (len >= 8 && strcmp(volumeName + len - 7, ".backup") == 0)
1479 else if (len >= 10 &&
1480 strcmp(volumeName + len - 9, ".readonly") == 0)
1485 if (cm_VolNameIsID(volumeName)) {
1486 code = cm_GetVolumeByID(cellp, atoi(volumeName), userp, reqp,
1487 CM_GETVOL_FLAG_CREATE, &volp);
1489 code = cm_GetVolumeByName(cellp, volumeName, userp, reqp,
1490 CM_GETVOL_FLAG_CREATE, &volp);
1496 if (volType == BACKVOL)
1497 volume = volp->bk.ID;
1498 else if (volType == ROVOL ||
1499 (volType == RWVOL && mountType == ROVOL && volp->ro.ID != 0))
1500 volume = volp->ro.ID;
1502 volume = volp->rw.ID;
1504 cm_SetFid(&fid, cellp->cellID, volume, 1, 1);
1506 code = cm_GetSCache(&fid, outpScpp, userp, reqp);
1516 if (flags & CM_FLAG_CHECKPATH)
1517 return CM_ERROR_NOSUCHPATH;
1519 return CM_ERROR_NOSUCHFILE;
1522 #ifdef DEBUG_REFCOUNT
1523 long cm_LookupDbg(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1524 cm_req_t *reqp, cm_scache_t **outpScpp, char * file, long line)
1526 long cm_Lookup(cm_scache_t *dscp, char *namep, long flags, cm_user_t *userp,
1527 cm_req_t *reqp, cm_scache_t **outpScpp)
1531 char tname[AFSPATHMAX];
1532 int sysNameIndex = 0;
1533 cm_scache_t *scp = NULL;
1535 #ifdef DEBUG_REFCOUNT
1536 afsi_log("%s:%d cm_Lookup dscp 0x%p ref %d", file, line, dscp, dscp->refCount, file, line);
1537 osi_Log2(afsd_logp, "cm_Lookup dscp 0x%p ref %d", dscp, dscp->refCount);
1540 if ( stricmp(namep,SMB_IOCTL_FILENAME_NOSLASH) == 0 ) {
1541 if (flags & CM_FLAG_CHECKPATH)
1542 return CM_ERROR_NOSUCHPATH;
1544 return CM_ERROR_NOSUCHFILE;
1547 if (dscp == cm_data.rootSCachep &&
1548 strnicmp(namep, CM_PREFIX_VOL, CM_PREFIX_VOL_CCH) == 0) {
1549 return cm_EvaluateVolumeReference(namep, flags, userp, reqp, outpScpp);
1552 if (cm_ExpandSysName(namep, NULL, 0, 0) > 0) {
1553 for ( sysNameIndex = 0; sysNameIndex < MAXNUMSYSNAMES; sysNameIndex++) {
1554 code = cm_ExpandSysName(namep, tname, sizeof(tname), sysNameIndex);
1556 code = cm_LookupInternal(dscp, tname, flags, userp, reqp, &scp);
1557 #ifdef DEBUG_REFCOUNT
1558 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);
1559 osi_Log3(afsd_logp, "cm_LookupInternal (1) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1567 cm_ReleaseSCache(scp);
1571 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1572 #ifdef DEBUG_REFCOUNT
1573 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);
1574 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1581 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1582 #ifdef DEBUG_REFCOUNT
1583 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);
1584 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1590 /* None of the possible sysName expansions could be found */
1591 if (flags & CM_FLAG_CHECKPATH)
1592 return CM_ERROR_NOSUCHPATH;
1594 return CM_ERROR_NOSUCHFILE;
1597 long cm_Unlink(cm_scache_t *dscp, char *namep, cm_user_t *userp, cm_req_t *reqp)
1603 AFSFetchStatus newDirStatus;
1605 struct rx_connection * callp;
1608 #ifdef AFS_FREELANCE_CLIENT
1609 if (cm_freelanceEnabled && dscp == cm_data.rootSCachep) {
1610 /* deleting a mount point from the root dir. */
1611 code = cm_FreelanceRemoveMount(namep);
1616 /* make sure we don't screw up the dir status during the merge */
1617 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
1619 lock_ObtainMutex(&dscp->mx);
1620 sflags = CM_SCACHESYNC_STOREDATA;
1621 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, sflags);
1622 lock_ReleaseMutex(&dscp->mx);
1624 cm_EndDirOp(&dirop);
1629 afsFid.Volume = dscp->fid.volume;
1630 afsFid.Vnode = dscp->fid.vnode;
1631 afsFid.Unique = dscp->fid.unique;
1633 osi_Log1(afsd_logp, "CALL RemoveFile scp 0x%p", dscp);
1635 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
1639 callp = cm_GetRxConn(connp);
1640 code = RXAFS_RemoveFile(callp, &afsFid, namep,
1641 &newDirStatus, &volSync);
1642 rx_PutConnection(callp);
1644 } while (cm_Analyze(connp, userp, reqp, &dscp->fid, &volSync, NULL, NULL, code));
1645 code = cm_MapRPCError(code, reqp);
1648 osi_Log1(afsd_logp, "CALL RemoveFile FAILURE, code 0x%x", code);
1650 osi_Log0(afsd_logp, "CALL RemoveFile SUCCESS");
1653 lock_ObtainWrite(&dirop.scp->dirlock);
1654 dirop.lockType = CM_DIRLOCK_WRITE;
1656 lock_ObtainMutex(&dscp->mx);
1657 cm_dnlcRemove(dscp, namep);
1658 cm_SyncOpDone(dscp, NULL, sflags);
1660 cm_MergeStatus(NULL, dscp, &newDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
1661 } else if (code == CM_ERROR_NOSUCHFILE) {
1662 /* windows would not have allowed the request to delete the file
1663 * if it did not believe the file existed. therefore, we must
1664 * have an inconsistent view of the world.
1666 dscp->cbServerp = NULL;
1668 lock_ReleaseMutex(&dscp->mx);
1670 if (code == 0 && cm_CheckDirOpForSingleChange(&dirop)) {
1671 cm_DirDeleteEntry(&dirop, namep);
1673 cm_BPlusDirDeleteEntry(&dirop, namep);
1676 cm_EndDirOp(&dirop);
1681 /* called with a locked vnode, and fills in the link info.
1682 * returns this the vnode still locked.
1684 long cm_HandleLink(cm_scache_t *linkScp, cm_user_t *userp, cm_req_t *reqp)
1691 lock_AssertMutex(&linkScp->mx);
1692 if (!linkScp->mountPointStringp[0]) {
1693 /* read the link data */
1694 lock_ReleaseMutex(&linkScp->mx);
1695 thyper.LowPart = thyper.HighPart = 0;
1696 code = buf_Get(linkScp, &thyper, &bufp);
1697 lock_ObtainMutex(&linkScp->mx);
1701 code = cm_SyncOp(linkScp, bufp, userp, reqp, 0,
1702 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1707 cm_SyncOpDone(linkScp, bufp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ);
1709 if (cm_HaveBuffer(linkScp, bufp, 0))
1712 code = cm_GetBuffer(linkScp, bufp, NULL, userp, reqp);
1717 } /* while loop to get the data */
1719 /* now if we still have no link read in,
1720 * copy the data from the buffer */
1721 if ((temp = linkScp->length.LowPart) >= MOUNTPOINTLEN) {
1723 return CM_ERROR_TOOBIG;
1726 /* otherwise, it fits; make sure it is still null (could have
1727 * lost race with someone else referencing this link above),
1728 * and if so, copy in the data.
1730 if (!linkScp->mountPointStringp[0]) {
1731 strncpy(linkScp->mountPointStringp, bufp->datap, temp);
1732 linkScp->mountPointStringp[temp] = 0; /* null terminate */
1734 if ( !strnicmp(linkScp->mountPointStringp, "msdfs:", strlen("msdfs:")) )
1735 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1738 } /* don't have sym link contents cached */
1743 /* called with a held vnode and a path suffix, with the held vnode being a
1744 * symbolic link. Our goal is to generate a new path to interpret, and return
1745 * this new path in newSpaceBufferp. If the new vnode is relative to a dir
1746 * other than the directory containing the symbolic link, then the new root is
1747 * returned in *newRootScpp, otherwise a null is returned there.
1749 long cm_AssembleLink(cm_scache_t *linkScp, char *pathSuffixp,
1750 cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1751 cm_user_t *userp, cm_req_t *reqp)
1758 *newRootScpp = NULL;
1759 *newSpaceBufferp = NULL;
1761 lock_ObtainMutex(&linkScp->mx);
1762 code = cm_HandleLink(linkScp, userp, reqp);
1766 /* if we may overflow the buffer, bail out; buffer is signficantly
1767 * bigger than max path length, so we don't really have to worry about
1768 * being a little conservative here.
1770 if (strlen(linkScp->mountPointStringp) + strlen(pathSuffixp) + 2
1771 >= CM_UTILS_SPACESIZE)
1772 return CM_ERROR_TOOBIG;
1774 tsp = cm_GetSpace();
1775 linkp = linkScp->mountPointStringp;
1776 if (strncmp(linkp, cm_mountRoot, cm_mountRootLen) == 0) {
1777 if (strlen(linkp) > cm_mountRootLen)
1778 strcpy(tsp->data, linkp+cm_mountRootLen+1);
1781 *newRootScpp = cm_data.rootSCachep;
1782 cm_HoldSCache(cm_data.rootSCachep);
1783 } else if (linkp[0] == '\\' && linkp[1] == '\\') {
1784 if (!strnicmp(&linkp[2], cm_NetbiosName, (len = (long)strlen(cm_NetbiosName))))
1786 char * p = &linkp[len + 3];
1787 if (strnicmp(p, "all", 3) == 0)
1790 strcpy(tsp->data, p);
1791 for (p = tsp->data; *p; p++) {
1795 *newRootScpp = cm_data.rootSCachep;
1796 cm_HoldSCache(cm_data.rootSCachep);
1798 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1799 strcpy(tsp->data, linkp);
1800 code = CM_ERROR_PATH_NOT_COVERED;
1802 } else if ( linkScp->fileType == CM_SCACHETYPE_DFSLINK ||
1803 !strnicmp(linkp, "msdfs:", (len = (long)strlen("msdfs:"))) ) {
1804 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1805 strcpy(tsp->data, linkp);
1806 code = CM_ERROR_PATH_NOT_COVERED;
1807 } else if (*linkp == '\\' || *linkp == '/') {
1809 /* formerly, this was considered to be from the AFS root,
1810 * but this seems to create problems. instead, we will just
1811 * reject the link */
1812 strcpy(tsp->data, linkp+1);
1813 *newRootScpp = cm_data.rootSCachep;
1814 cm_HoldSCache(cm_data.rootSCachep);
1816 /* we still copy the link data into the response so that
1817 * the user can see what the link points to
1819 linkScp->fileType = CM_SCACHETYPE_INVALID;
1820 strcpy(tsp->data, linkp);
1821 code = CM_ERROR_NOSUCHPATH;
1824 /* a relative link */
1825 strcpy(tsp->data, linkp);
1827 if (pathSuffixp[0] != 0) { /* if suffix string is non-null */
1828 strcat(tsp->data, "\\");
1829 strcat(tsp->data, pathSuffixp);
1832 *newSpaceBufferp = tsp;
1836 if (code == CM_ERROR_PATH_NOT_COVERED && reqp->tidPathp && reqp->relPathp)
1837 cm_VolStatus_Notify_DFS_Mapping(linkScp, reqp->tidPathp, reqp->relPathp);
1841 lock_ReleaseMutex(&linkScp->mx);
1844 #ifdef DEBUG_REFCOUNT
1845 long cm_NameIDbg(cm_scache_t *rootSCachep, char *pathp, long flags,
1846 cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp,
1847 char * file, long line)
1849 long cm_NameI(cm_scache_t *rootSCachep, char *pathp, long flags,
1850 cm_user_t *userp, char *tidPathp, cm_req_t *reqp, cm_scache_t **outScpp)
1854 char *tp; /* ptr moving through input buffer */
1855 char tc; /* temp char */
1856 int haveComponent; /* has new component started? */
1857 char component[AFSPATHMAX]; /* this is the new component */
1858 char *cp; /* component name being assembled */
1859 cm_scache_t *tscp; /* current location in the hierarchy */
1860 cm_scache_t *nscp; /* next dude down */
1861 cm_scache_t *dirScp; /* last dir we searched */
1862 cm_scache_t *linkScp; /* new root for the symlink we just
1864 cm_space_t *psp; /* space for current path, if we've hit
1866 cm_space_t *tempsp; /* temp vbl */
1867 char *restp; /* rest of the pathname to interpret */
1868 int symlinkCount; /* count of # of symlinks traversed */
1869 int extraFlag; /* avoid chasing mt pts for dir cmd */
1870 int phase = 1; /* 1 = tidPathp, 2 = pathp */
1871 #define MAX_FID_COUNT 512
1872 cm_fid_t fids[MAX_FID_COUNT]; /* array of fids processed in this path walk */
1873 int fid_count = 0; /* number of fids processed in this path walk */
1876 #ifdef DEBUG_REFCOUNT
1877 afsi_log("%s:%d cm_NameI rootscp 0x%p ref %d", file, line, rootSCachep, rootSCachep->refCount);
1878 osi_Log4(afsd_logp,"cm_NameI rootscp 0x%p path %s tidpath %s flags 0x%x",
1879 rootSCachep, pathp ? pathp : "<NULL>", tidPathp ? tidPathp : "<NULL>",
1894 cm_HoldSCache(tscp);
1902 /* map Unix slashes into DOS ones so we can interpret Unix
1908 if (!haveComponent) {
1911 } else if (tc == 0) {
1925 /* we have a component here */
1926 if (tc == 0 || tc == '\\') {
1927 /* end of the component; we're at the last
1928 * component if tc == 0. However, if the last
1929 * is a symlink, we have more to do.
1931 *cp++ = 0; /* add null termination */
1933 if ((flags & CM_FLAG_DIRSEARCH) && tc == 0)
1934 extraFlag = CM_FLAG_NOMOUNTCHASE;
1935 code = cm_Lookup(tscp, component,
1937 userp, reqp, &nscp);
1940 if (!strcmp(component,"..") || !strcmp(component,".")) {
1942 * roll back the fid list until we find the fid
1943 * that matches where we are now. Its not necessarily
1944 * one or two fids because they might have been
1945 * symlinks or mount points or both that were crossed.
1947 for ( i=fid_count-1; i>=0; i--) {
1948 if (!cm_FidCmp(&nscp->fid, &fids[i]))
1953 /* add the new fid to the list */
1954 for ( i=0; i<fid_count; i++) {
1955 if ( !cm_FidCmp(&nscp->fid, &fids[i]) ) {
1956 code = CM_ERROR_TOO_MANY_SYMLINKS;
1957 cm_ReleaseSCache(nscp);
1962 if (i == fid_count && fid_count < MAX_FID_COUNT) {
1963 fids[fid_count++] = nscp->fid;
1969 cm_ReleaseSCache(tscp);
1971 cm_ReleaseSCache(dirScp);
1974 if ((code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) &&
1975 tscp->fileType == CM_SCACHETYPE_SYMLINK)
1977 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_NOSUCHPATH");
1978 return CM_ERROR_NOSUCHPATH;
1980 osi_Log1(afsd_logp,"cm_NameI code 0x%x", code);
1985 haveComponent = 0; /* component done */
1987 cm_ReleaseSCache(dirScp);
1988 dirScp = tscp; /* for some symlinks */
1989 tscp = nscp; /* already held */
1991 if (tc == 0 && !(flags & CM_FLAG_FOLLOW) && phase == 2) {
1994 cm_ReleaseSCache(dirScp);
2000 /* now, if tscp is a symlink, we should follow
2001 * it and assemble the path again.
2003 lock_ObtainMutex(&tscp->mx);
2004 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
2005 CM_SCACHESYNC_GETSTATUS
2006 | CM_SCACHESYNC_NEEDCALLBACK);
2008 lock_ReleaseMutex(&tscp->mx);
2009 cm_ReleaseSCache(tscp);
2012 cm_ReleaseSCache(dirScp);
2017 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2019 if (tscp->fileType == CM_SCACHETYPE_SYMLINK) {
2020 /* this is a symlink; assemble a new buffer */
2021 lock_ReleaseMutex(&tscp->mx);
2022 if (symlinkCount++ >= MAX_SYMLINK_COUNT) {
2023 cm_ReleaseSCache(tscp);
2026 cm_ReleaseSCache(dirScp);
2031 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_TOO_MANY_SYMLINKS");
2032 return CM_ERROR_TOO_MANY_SYMLINKS;
2038 code = cm_AssembleLink(tscp, restp, &linkScp, &tempsp, userp, reqp);
2040 if (code == 0 && linkScp != NULL) {
2041 if (linkScp == cm_data.rootSCachep)
2044 for ( i=0; i<fid_count; i++) {
2045 if ( !cm_FidCmp(&linkScp->fid, &fids[i]) ) {
2046 code = CM_ERROR_TOO_MANY_SYMLINKS;
2047 cm_ReleaseSCache(linkScp);
2053 if (i == fid_count && fid_count < MAX_FID_COUNT) {
2054 fids[fid_count++] = linkScp->fid;
2059 /* something went wrong */
2060 cm_ReleaseSCache(tscp);
2063 cm_ReleaseSCache(dirScp);
2069 /* otherwise, tempsp has the new path,
2070 * and linkScp is the new root from
2071 * which to interpret that path.
2072 * Continue with the namei processing,
2073 * also doing the bookkeeping for the
2074 * space allocation and tracking the
2075 * vnode reference counts.
2081 cm_ReleaseSCache(tscp);
2086 * now, if linkScp is null, that's
2087 * AssembleLink's way of telling us that
2088 * the sym link is relative to the dir
2089 * containing the link. We have a ref
2090 * to it in dirScp, and we hold it now
2091 * and reuse it as the new spot in the
2099 /* not a symlink, we may be done */
2100 lock_ReleaseMutex(&tscp->mx);
2108 cm_ReleaseSCache(dirScp);
2116 cm_ReleaseSCache(dirScp);
2119 } /* end of a component */
2122 } /* we have a component */
2123 } /* big while loop over all components */
2127 cm_ReleaseSCache(dirScp);
2133 cm_ReleaseSCache(tscp);
2135 #ifdef DEBUG_REFCOUNT
2136 afsi_log("%s:%d cm_NameI code 0x%x outScpp 0x%p ref %d", file, line, code, *outScpp, (*outScpp)->refCount);
2138 osi_Log2(afsd_logp,"cm_NameI code 0x%x outScpp 0x%p", code, *outScpp);
2142 /* called with a dir, and a vnode within the dir that happens to be a symlink.
2143 * We chase the link, and return a held pointer to the target, if it exists,
2144 * in *outScpp. If we succeed, we return 0, otherwise we return an error code
2145 * and do not hold or return a target vnode.
2147 * This is very similar to calling cm_NameI with the last component of a name,
2148 * which happens to be a symlink, except that we've already passed by the name.
2150 * This function is typically called by the directory listing functions, which
2151 * encounter symlinks but need to return the proper file length so programs
2152 * like "more" work properly when they make use of the attributes retrieved from
2155 * The input vnode should not be locked when this function is called.
2157 long cm_EvaluateSymLink(cm_scache_t *dscp, cm_scache_t *linkScp,
2158 cm_scache_t **outScpp, cm_user_t *userp, cm_req_t *reqp)
2162 cm_scache_t *newRootScp;
2164 osi_Log1(afsd_logp, "Evaluating symlink scp 0x%p", linkScp);
2166 code = cm_AssembleLink(linkScp, "", &newRootScp, &spacep, userp, reqp);
2170 /* now, if newRootScp is NULL, we're really being told that the symlink
2171 * is relative to the current directory (dscp).
2173 if (newRootScp == NULL) {
2175 cm_HoldSCache(dscp);
2178 code = cm_NameI(newRootScp, spacep->data,
2179 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW | CM_FLAG_DIRSEARCH,
2180 userp, NULL, reqp, outScpp);
2182 if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH)
2183 code = CM_ERROR_NOSUCHPATH;
2185 /* this stuff is allocated no matter what happened on the namei call,
2187 cm_FreeSpace(spacep);
2188 cm_ReleaseSCache(newRootScp);
2190 if (linkScp == *outScpp) {
2191 cm_ReleaseSCache(*outScpp);
2193 code = CM_ERROR_NOSUCHPATH;
2199 /* make this big enough so that one buffer of dir pages won't overflow. We'll
2200 * check anyway, but we want to minimize the chance that we have to leave stuff
2203 #define CM_BULKMAX (3 * AFSCBMAX)
2205 /* rock for bulk stat calls */
2206 typedef struct cm_bulkStat {
2207 osi_hyper_t bufOffset; /* only do it for things in this buffer page */
2209 /* info for the actual call */
2210 int counter; /* next free slot */
2211 AFSFid fids[CM_BULKMAX];
2212 AFSFetchStatus stats[CM_BULKMAX];
2213 AFSCallBack callbacks[CM_BULKMAX];
2216 /* for a given entry, make sure that it isn't in the stat cache, and then
2217 * add it to the list of file IDs to be obtained.
2219 * Don't bother adding it if we already have a vnode. Note that the dir
2220 * is locked, so we have to be careful checking the vnode we're thinking of
2221 * processing, to avoid deadlocks.
2223 long cm_TryBulkProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
2234 /* Don't overflow bsp. */
2235 if (bsp->counter >= CM_BULKMAX)
2236 return CM_ERROR_STOPNOW;
2238 thyper.LowPart = cm_data.buf_blockSize;
2239 thyper.HighPart = 0;
2240 thyper = LargeIntegerAdd(thyper, bsp->bufOffset);
2242 /* thyper is now the first byte past the end of the record we're
2243 * interested in, and bsp->bufOffset is the first byte of the record
2244 * we're interested in.
2245 * Skip data in the others.
2248 if (LargeIntegerLessThan(*offp, bsp->bufOffset))
2250 if (LargeIntegerGreaterThanOrEqualTo(*offp, thyper))
2251 return CM_ERROR_STOPNOW;
2252 if (strcmp(dep->name, ".") == 0 || strcmp(dep->name, "..") == 0)
2255 cm_SetFid(&tfid, scp->fid.cell, scp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
2256 tscp = cm_FindSCache(&tfid);
2258 if (lock_TryMutex(&tscp->mx)) {
2259 /* we have an entry that we can look at */
2260 if (!(tscp->flags & CM_SCACHEFLAG_EACCESS) && cm_HaveCallback(tscp)) {
2261 /* we have a callback on it. Don't bother
2262 * fetching this stat entry, since we're happy
2263 * with the info we have.
2265 lock_ReleaseMutex(&tscp->mx);
2266 cm_ReleaseSCache(tscp);
2269 lock_ReleaseMutex(&tscp->mx);
2271 cm_ReleaseSCache(tscp);
2274 #ifdef AFS_FREELANCE_CLIENT
2275 // yj: if this is a mountpoint under root.afs then we don't want it
2276 // to be bulkstat-ed, instead, we call getSCache directly and under
2277 // getSCache, it is handled specially.
2278 if ( cm_freelanceEnabled &&
2279 tfid.cell==AFS_FAKE_ROOT_CELL_ID &&
2280 tfid.volume==AFS_FAKE_ROOT_VOL_ID &&
2281 !(tfid.vnode==0x1 && tfid.unique==0x1) )
2283 osi_Log0(afsd_logp, "cm_TryBulkProc Freelance calls cm_SCache on root.afs mountpoint");
2284 return cm_GetSCache(&tfid, &tscp, NULL, NULL);
2286 #endif /* AFS_FREELANCE_CLIENT */
2289 bsp->fids[i].Volume = scp->fid.volume;
2290 bsp->fids[i].Vnode = tfid.vnode;
2291 bsp->fids[i].Unique = tfid.unique;
2295 /* called with a locked scp and a pointer to a buffer. Make bulk stat
2296 * calls on all undeleted files in the page of the directory specified.
2299 cm_TryBulkStat(cm_scache_t *dscp, osi_hyper_t *offsetp, cm_user_t *userp,
2303 cm_bulkStat_t bb; /* this is *BIG*, probably 16K or so;
2304 * watch for stack problems */
2305 AFSCBFids fidStruct;
2306 AFSBulkStats statStruct;
2308 AFSCBs callbackStruct;
2311 cm_callbackRequest_t cbReq;
2317 struct rx_connection * callp;
2318 int inlinebulk = 0; /* Did we use InlineBulkStatus RPC or not? */
2320 osi_Log1(afsd_logp, "cm_TryBulkStat dir 0x%p", dscp);
2322 /* should be on a buffer boundary */
2323 osi_assertx((offsetp->LowPart & (cm_data.buf_blockSize - 1)) == 0, "invalid offset");
2325 memset(&bb, 0, sizeof(bb));
2326 bb.bufOffset = *offsetp;
2328 lock_ReleaseMutex(&dscp->mx);
2329 /* first, assemble the file IDs we need to stat */
2330 code = cm_ApplyDir(dscp, cm_TryBulkProc, (void *) &bb, offsetp, userp, reqp, NULL);
2332 /* if we failed, bail out early */
2333 if (code && code != CM_ERROR_STOPNOW) {
2334 lock_ObtainMutex(&dscp->mx);
2338 /* otherwise, we may have one or more bulk stat's worth of stuff in bb;
2339 * make the calls to create the entries. Handle AFSCBMAX files at a
2343 while (filex < bb.counter) {
2344 filesThisCall = bb.counter - filex;
2345 if (filesThisCall > AFSCBMAX)
2346 filesThisCall = AFSCBMAX;
2348 fidStruct.AFSCBFids_len = filesThisCall;
2349 fidStruct.AFSCBFids_val = &bb.fids[filex];
2350 statStruct.AFSBulkStats_len = filesThisCall;
2351 statStruct.AFSBulkStats_val = &bb.stats[filex];
2352 callbackStruct.AFSCBs_len = filesThisCall;
2353 callbackStruct.AFSCBs_val = &bb.callbacks[filex];
2354 cm_StartCallbackGrantingCall(NULL, &cbReq);
2355 osi_Log1(afsd_logp, "CALL BulkStatus, %d entries", filesThisCall);
2357 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2361 callp = cm_GetRxConn(connp);
2362 if (!(connp->serverp->flags & CM_SERVERFLAG_NOINLINEBULK)) {
2363 code = RXAFS_InlineBulkStatus(callp, &fidStruct,
2364 &statStruct, &callbackStruct, &volSync);
2365 if (code == RXGEN_OPCODE) {
2366 cm_SetServerNoInlineBulk(connp->serverp, 0);
2372 code = RXAFS_BulkStatus(callp, &fidStruct,
2373 &statStruct, &callbackStruct, &volSync);
2375 rx_PutConnection(callp);
2377 } while (cm_Analyze(connp, userp, reqp, &dscp->fid,
2378 &volSync, NULL, &cbReq, code));
2379 code = cm_MapRPCError(code, reqp);
2381 osi_Log2(afsd_logp, "CALL %sBulkStatus FAILURE code 0x%x",
2382 inlinebulk ? "Inline" : "", code);
2384 osi_Log1(afsd_logp, "CALL %sBulkStatus SUCCESS", inlinebulk ? "Inline" : "");
2386 /* may as well quit on an error, since we're not going to do
2387 * much better on the next immediate call, either.
2390 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2394 /* otherwise, we should do the merges */
2395 for (i = 0; i<filesThisCall; i++) {
2397 cm_SetFid(&tfid, dscp->fid.cell, bb.fids[j].Volume, bb.fids[j].Vnode, bb.fids[j].Unique);
2398 code = cm_GetSCache(&tfid, &scp, userp, reqp);
2402 /* otherwise, if this entry has no callback info,
2405 lock_ObtainMutex(&scp->mx);
2406 /* now, we have to be extra paranoid on merging in this
2407 * information, since we didn't use cm_SyncOp before
2408 * starting the fetch to make sure that no bad races
2409 * were occurring. Specifically, we need to make sure
2410 * we don't obliterate any newer information in the
2411 * vnode than have here.
2413 * Right now, be pretty conservative: if there's a
2414 * callback or a pending call, skip it.
2416 if ((scp->cbServerp == NULL || (scp->flags & CM_SCACHEFLAG_EACCESS))
2418 (CM_SCACHEFLAG_FETCHING
2419 | CM_SCACHEFLAG_STORING
2420 | CM_SCACHEFLAG_SIZESTORING))) {
2421 cm_EndCallbackGrantingCall(scp, &cbReq,
2423 CM_CALLBACK_MAINTAINCOUNT);
2424 cm_MergeStatus(dscp, scp, &bb.stats[j], &volSync, userp, 0);
2426 lock_ReleaseMutex(&scp->mx);
2427 cm_ReleaseSCache(scp);
2428 } /* all files in the response */
2429 /* now tell it to drop the count,
2430 * after doing the vnode processing above */
2431 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2433 filex += filesThisCall;
2434 } /* while there are still more files to process */
2435 lock_ObtainMutex(&dscp->mx);
2437 /* If we did the InlineBulk RPC pull out the return code and log it */
2439 if ((&bb.stats[0])->errorCode) {
2440 osi_Log1(afsd_logp, "cm_TryBulkStat bulk stat error: %d",
2441 (&bb.stats[0])->errorCode);
2445 osi_Log0(afsd_logp, "END cm_TryBulkStat");
2449 void cm_StatusFromAttr(AFSStoreStatus *statusp, cm_scache_t *scp, cm_attr_t *attrp)
2453 /* initialize store back mask as inexpensive local variable */
2455 memset(statusp, 0, sizeof(AFSStoreStatus));
2457 /* copy out queued info from scache first, if scp passed in */
2459 if (scp->mask & CM_SCACHEMASK_CLIENTMODTIME) {
2460 statusp->ClientModTime = scp->clientModTime;
2461 mask |= AFS_SETMODTIME;
2462 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
2467 /* now add in our locally generated request */
2468 if (attrp->mask & CM_ATTRMASK_CLIENTMODTIME) {
2469 statusp->ClientModTime = attrp->clientModTime;
2470 mask |= AFS_SETMODTIME;
2472 if (attrp->mask & CM_ATTRMASK_UNIXMODEBITS) {
2473 statusp->UnixModeBits = attrp->unixModeBits;
2474 mask |= AFS_SETMODE;
2476 if (attrp->mask & CM_ATTRMASK_OWNER) {
2477 statusp->Owner = attrp->owner;
2478 mask |= AFS_SETOWNER;
2480 if (attrp->mask & CM_ATTRMASK_GROUP) {
2481 statusp->Group = attrp->group;
2482 mask |= AFS_SETGROUP;
2485 statusp->Mask = mask;
2488 /* set the file size, and make sure that all relevant buffers have been
2489 * truncated. Ensure that any partially truncated buffers have been zeroed
2490 * to the end of the buffer.
2492 long cm_SetLength(cm_scache_t *scp, osi_hyper_t *sizep, cm_user_t *userp,
2498 /* start by locking out buffer creation */
2499 lock_ObtainWrite(&scp->bufCreateLock);
2501 /* verify that this is a file, not a dir or a symlink */
2502 lock_ObtainMutex(&scp->mx);
2503 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
2504 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2507 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2509 if (scp->fileType != CM_SCACHETYPE_FILE) {
2510 code = CM_ERROR_ISDIR;
2515 if (LargeIntegerLessThan(*sizep, scp->length))
2520 lock_ReleaseMutex(&scp->mx);
2522 /* can't hold scp->mx lock here, since we may wait for a storeback to
2523 * finish if the buffer package is cleaning a buffer by storing it to
2527 buf_Truncate(scp, userp, reqp, sizep);
2529 /* now ensure that file length is short enough, and update truncPos */
2530 lock_ObtainMutex(&scp->mx);
2532 /* make sure we have a callback (so we have the right value for the
2533 * length), and wait for it to be safe to do a truncate.
2535 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_WRITE,
2536 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2537 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2539 /* If we only have 'i' bits, then we should still be able to set
2540 the size of a file we created. */
2541 if (code == CM_ERROR_NOACCESS && scp->creator == userp) {
2542 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
2543 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2544 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2550 if (LargeIntegerLessThan(*sizep, scp->length)) {
2551 /* a real truncation. If truncPos is not set yet, or is bigger
2552 * than where we're truncating the file, set truncPos to this
2557 if (!(scp->mask & CM_SCACHEMASK_TRUNCPOS)
2558 || LargeIntegerLessThan(*sizep, scp->length)) {
2560 scp->truncPos = *sizep;
2561 scp->mask |= CM_SCACHEMASK_TRUNCPOS;
2563 /* in either case, the new file size has been changed */
2564 scp->length = *sizep;
2565 scp->mask |= CM_SCACHEMASK_LENGTH;
2567 else if (LargeIntegerGreaterThan(*sizep, scp->length)) {
2568 /* really extending the file */
2569 scp->length = *sizep;
2570 scp->mask |= CM_SCACHEMASK_LENGTH;
2573 /* done successfully */
2576 cm_SyncOpDone(scp, NULL,
2577 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2578 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2581 lock_ReleaseMutex(&scp->mx);
2582 lock_ReleaseWrite(&scp->bufCreateLock);
2587 /* set the file size or other attributes (but not both at once) */
2588 long cm_SetAttr(cm_scache_t *scp, cm_attr_t *attrp, cm_user_t *userp,
2592 AFSFetchStatus afsOutStatus;
2596 AFSStoreStatus afsInStatus;
2597 struct rx_connection * callp;
2599 /* handle file length setting */
2600 if (attrp->mask & CM_ATTRMASK_LENGTH)
2601 return cm_SetLength(scp, &attrp->length, userp, reqp);
2603 lock_ObtainMutex(&scp->mx);
2604 /* otherwise, we have to make an RPC to get the status */
2605 code = cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STORESTATUS);
2607 lock_ReleaseMutex(&scp->mx);
2611 /* make the attr structure */
2612 cm_StatusFromAttr(&afsInStatus, scp, attrp);
2614 tfid.Volume = scp->fid.volume;
2615 tfid.Vnode = scp->fid.vnode;
2616 tfid.Unique = scp->fid.unique;
2617 lock_ReleaseMutex(&scp->mx);
2619 /* now make the RPC */
2620 osi_Log1(afsd_logp, "CALL StoreStatus scp 0x%p", scp);
2622 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2626 callp = cm_GetRxConn(connp);
2627 code = RXAFS_StoreStatus(callp, &tfid,
2628 &afsInStatus, &afsOutStatus, &volSync);
2629 rx_PutConnection(callp);
2631 } while (cm_Analyze(connp, userp, reqp,
2632 &scp->fid, &volSync, NULL, NULL, code));
2633 code = cm_MapRPCError(code, reqp);
2636 osi_Log1(afsd_logp, "CALL StoreStatus FAILURE, code 0x%x", code);
2638 osi_Log0(afsd_logp, "CALL StoreStatus SUCCESS");
2640 lock_ObtainMutex(&scp->mx);
2641 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STORESTATUS);
2643 cm_MergeStatus(NULL, scp, &afsOutStatus, &volSync, userp,
2644 CM_MERGEFLAG_FORCE|CM_MERGEFLAG_STOREDATA);
2646 /* if we're changing the mode bits, discard the ACL cache,
2647 * since we changed the mode bits.
2649 if (afsInStatus.Mask & AFS_SETMODE)
2650 cm_FreeAllACLEnts(scp);
2651 lock_ReleaseMutex(&scp->mx);
2655 long cm_Create(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2656 cm_scache_t **scpp, cm_user_t *userp, cm_req_t *reqp)
2661 cm_callbackRequest_t cbReq;
2664 cm_scache_t *scp = NULL;
2666 AFSStoreStatus inStatus;
2667 AFSFetchStatus updatedDirStatus;
2668 AFSFetchStatus newFileStatus;
2669 AFSCallBack newFileCallback;
2671 struct rx_connection * callp;
2674 /* can't create names with @sys in them; must expand it manually first.
2675 * return "invalid request" if they try.
2677 if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2678 return CM_ERROR_ATSYS;
2681 #ifdef AFS_FREELANCE_CLIENT
2682 /* Freelance root volume does not hold files */
2683 if (cm_freelanceEnabled &&
2684 dscp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
2685 dscp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
2687 return CM_ERROR_NOACCESS;
2689 #endif /* AFS_FREELANCE_CLIENT */
2691 /* before starting the RPC, mark that we're changing the file data, so
2692 * that someone who does a chmod will know to wait until our call
2695 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2696 lock_ObtainMutex(&dscp->mx);
2697 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2698 lock_ReleaseMutex(&dscp->mx);
2700 cm_StartCallbackGrantingCall(NULL, &cbReq);
2702 cm_EndDirOp(&dirop);
2709 cm_StatusFromAttr(&inStatus, NULL, attrp);
2711 /* try the RPC now */
2712 osi_Log1(afsd_logp, "CALL CreateFile scp 0x%p", dscp);
2714 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2718 dirAFSFid.Volume = dscp->fid.volume;
2719 dirAFSFid.Vnode = dscp->fid.vnode;
2720 dirAFSFid.Unique = dscp->fid.unique;
2722 callp = cm_GetRxConn(connp);
2723 code = RXAFS_CreateFile(connp->callp, &dirAFSFid, namep,
2724 &inStatus, &newAFSFid, &newFileStatus,
2725 &updatedDirStatus, &newFileCallback,
2727 rx_PutConnection(callp);
2729 } while (cm_Analyze(connp, userp, reqp,
2730 &dscp->fid, &volSync, NULL, &cbReq, code));
2731 code = cm_MapRPCError(code, reqp);
2734 osi_Log1(afsd_logp, "CALL CreateFile FAILURE, code 0x%x", code);
2736 osi_Log0(afsd_logp, "CALL CreateFile SUCCESS");
2739 lock_ObtainWrite(&dirop.scp->dirlock);
2740 dirop.lockType = CM_DIRLOCK_WRITE;
2742 lock_ObtainMutex(&dscp->mx);
2743 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2745 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2747 lock_ReleaseMutex(&dscp->mx);
2749 /* now try to create the file's entry, too, but be careful to
2750 * make sure that we don't merge in old info. Since we weren't locking
2751 * out any requests during the file's creation, we may have pretty old
2755 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
2756 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2758 lock_ObtainMutex(&scp->mx);
2759 scp->creator = userp; /* remember who created it */
2760 if (!cm_HaveCallback(scp)) {
2761 cm_MergeStatus(dscp, scp, &newFileStatus, &volSync,
2763 cm_EndCallbackGrantingCall(scp, &cbReq,
2764 &newFileCallback, 0);
2767 lock_ReleaseMutex(&scp->mx);
2772 /* make sure we end things properly */
2774 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2776 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2777 cm_DirCreateEntry(&dirop, namep, &newFid);
2779 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2782 cm_EndDirOp(&dirop);
2787 long cm_FSync(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
2791 code = buf_CleanVnode(scp, userp, reqp);
2793 lock_ObtainMutex(&scp->mx);
2795 if (scp->mask & (CM_SCACHEMASK_TRUNCPOS
2796 | CM_SCACHEMASK_CLIENTMODTIME
2797 | CM_SCACHEMASK_LENGTH))
2798 code = cm_StoreMini(scp, userp, reqp);
2800 if (scp->flags & (CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE)) {
2801 code = (scp->flags & CM_SCACHEFLAG_OVERQUOTA) ? CM_ERROR_QUOTA : CM_ERROR_SPACE;
2802 scp->flags &= ~(CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE);
2805 lock_ReleaseMutex(&scp->mx);
2810 long cm_MakeDir(cm_scache_t *dscp, char *namep, long flags, cm_attr_t *attrp,
2811 cm_user_t *userp, cm_req_t *reqp)
2816 cm_callbackRequest_t cbReq;
2819 cm_scache_t *scp = NULL;
2821 AFSStoreStatus inStatus;
2822 AFSFetchStatus updatedDirStatus;
2823 AFSFetchStatus newDirStatus;
2824 AFSCallBack newDirCallback;
2826 struct rx_connection * callp;
2829 /* can't create names with @sys in them; must expand it manually first.
2830 * return "invalid request" if they try.
2832 if (cm_ExpandSysName(namep, NULL, 0, 0)) {
2833 return CM_ERROR_ATSYS;
2836 #ifdef AFS_FREELANCE_CLIENT
2837 /* Freelance root volume does not hold subdirectories */
2838 if (cm_freelanceEnabled &&
2839 dscp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
2840 dscp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
2842 return CM_ERROR_NOACCESS;
2844 #endif /* AFS_FREELANCE_CLIENT */
2846 /* before starting the RPC, mark that we're changing the directory
2847 * data, so that someone who does a chmod on the dir will wait until
2848 * our call completes.
2850 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2851 lock_ObtainMutex(&dscp->mx);
2852 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2853 lock_ReleaseMutex(&dscp->mx);
2855 cm_StartCallbackGrantingCall(NULL, &cbReq);
2857 cm_EndDirOp(&dirop);
2864 cm_StatusFromAttr(&inStatus, NULL, attrp);
2866 /* try the RPC now */
2867 osi_Log1(afsd_logp, "CALL MakeDir scp 0x%p", dscp);
2869 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2873 dirAFSFid.Volume = dscp->fid.volume;
2874 dirAFSFid.Vnode = dscp->fid.vnode;
2875 dirAFSFid.Unique = dscp->fid.unique;
2877 callp = cm_GetRxConn(connp);
2878 code = RXAFS_MakeDir(connp->callp, &dirAFSFid, namep,
2879 &inStatus, &newAFSFid, &newDirStatus,
2880 &updatedDirStatus, &newDirCallback,
2882 rx_PutConnection(callp);
2884 } while (cm_Analyze(connp, userp, reqp,
2885 &dscp->fid, &volSync, NULL, &cbReq, code));
2886 code = cm_MapRPCError(code, reqp);
2889 osi_Log1(afsd_logp, "CALL MakeDir FAILURE, code 0x%x", code);
2891 osi_Log0(afsd_logp, "CALL MakeDir SUCCESS");
2894 lock_ObtainWrite(&dirop.scp->dirlock);
2895 dirop.lockType = CM_DIRLOCK_WRITE;
2897 lock_ObtainMutex(&dscp->mx);
2898 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2900 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
2902 lock_ReleaseMutex(&dscp->mx);
2904 /* now try to create the new dir's entry, too, but be careful to
2905 * make sure that we don't merge in old info. Since we weren't locking
2906 * out any requests during the file's creation, we may have pretty old
2910 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
2911 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2913 lock_ObtainMutex(&scp->mx);
2914 if (!cm_HaveCallback(scp)) {
2915 cm_MergeStatus(dscp, scp, &newDirStatus, &volSync,
2917 cm_EndCallbackGrantingCall(scp, &cbReq,
2918 &newDirCallback, 0);
2921 lock_ReleaseMutex(&scp->mx);
2922 cm_ReleaseSCache(scp);
2926 /* make sure we end things properly */
2928 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, 0);
2930 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2931 cm_DirCreateEntry(&dirop, namep, &newFid);
2933 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
2936 cm_EndDirOp(&dirop);
2938 /* and return error code */
2942 long cm_Link(cm_scache_t *dscp, char *namep, cm_scache_t *sscp, long flags,
2943 cm_user_t *userp, cm_req_t *reqp)
2948 AFSFid existingAFSFid;
2949 AFSFetchStatus updatedDirStatus;
2950 AFSFetchStatus newLinkStatus;
2952 struct rx_connection * callp;
2955 if (dscp->fid.cell != sscp->fid.cell ||
2956 dscp->fid.volume != sscp->fid.volume) {
2957 return CM_ERROR_CROSSDEVLINK;
2960 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
2961 lock_ObtainMutex(&dscp->mx);
2962 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2963 lock_ReleaseMutex(&dscp->mx);
2965 cm_EndDirOp(&dirop);
2970 /* try the RPC now */
2971 osi_Log1(afsd_logp, "CALL Link scp 0x%p", dscp);
2973 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2976 dirAFSFid.Volume = dscp->fid.volume;
2977 dirAFSFid.Vnode = dscp->fid.vnode;
2978 dirAFSFid.Unique = dscp->fid.unique;
2980 existingAFSFid.Volume = sscp->fid.volume;
2981 existingAFSFid.Vnode = sscp->fid.vnode;
2982 existingAFSFid.Unique = sscp->fid.unique;
2984 callp = cm_GetRxConn(connp);
2985 code = RXAFS_Link(callp, &dirAFSFid, namep, &existingAFSFid,
2986 &newLinkStatus, &updatedDirStatus, &volSync);
2987 rx_PutConnection(callp);
2988 osi_Log1(smb_logp," RXAFS_Link returns 0x%x", code);
2990 } while (cm_Analyze(connp, userp, reqp,
2991 &dscp->fid, &volSync, NULL, NULL, code));
2993 code = cm_MapRPCError(code, reqp);
2996 osi_Log1(afsd_logp, "CALL Link FAILURE, code 0x%x", code);
2998 osi_Log0(afsd_logp, "CALL Link SUCCESS");
3001 lock_ObtainWrite(&dirop.scp->dirlock);
3002 dirop.lockType = CM_DIRLOCK_WRITE;
3004 lock_ObtainMutex(&dscp->mx);
3005 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3007 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3009 lock_ReleaseMutex(&dscp->mx);
3012 if (cm_CheckDirOpForSingleChange(&dirop)) {
3013 cm_DirCreateEntry(&dirop, namep, &sscp->fid);
3015 cm_BPlusDirCreateEntry(&dirop, namep, &sscp->fid);
3019 cm_EndDirOp(&dirop);
3024 long cm_SymLink(cm_scache_t *dscp, char *namep, char *contentsp, long flags,
3025 cm_attr_t *attrp, cm_user_t *userp, cm_req_t *reqp)
3033 AFSStoreStatus inStatus;
3034 AFSFetchStatus updatedDirStatus;
3035 AFSFetchStatus newLinkStatus;
3037 struct rx_connection * callp;
3040 /* before starting the RPC, mark that we're changing the directory data,
3041 * so that someone who does a chmod on the dir will wait until our
3044 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3045 lock_ObtainMutex(&dscp->mx);
3046 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3047 lock_ReleaseMutex(&dscp->mx);
3049 cm_EndDirOp(&dirop);
3054 cm_StatusFromAttr(&inStatus, NULL, attrp);
3056 /* try the RPC now */
3057 osi_Log1(afsd_logp, "CALL Symlink scp 0x%p", dscp);
3059 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3063 dirAFSFid.Volume = dscp->fid.volume;
3064 dirAFSFid.Vnode = dscp->fid.vnode;
3065 dirAFSFid.Unique = dscp->fid.unique;
3067 callp = cm_GetRxConn(connp);
3068 code = RXAFS_Symlink(callp, &dirAFSFid, namep, contentsp,
3069 &inStatus, &newAFSFid, &newLinkStatus,
3070 &updatedDirStatus, &volSync);
3071 rx_PutConnection(callp);
3073 } while (cm_Analyze(connp, userp, reqp,
3074 &dscp->fid, &volSync, NULL, NULL, code));
3075 code = cm_MapRPCError(code, reqp);
3078 osi_Log1(afsd_logp, "CALL Symlink FAILURE, code 0x%x", code);
3080 osi_Log0(afsd_logp, "CALL Symlink SUCCESS");
3083 lock_ObtainWrite(&dirop.scp->dirlock);
3084 dirop.lockType = CM_DIRLOCK_WRITE;
3086 lock_ObtainMutex(&dscp->mx);
3087 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3089 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3091 lock_ReleaseMutex(&dscp->mx);
3094 if (cm_CheckDirOpForSingleChange(&dirop)) {
3095 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3097 cm_DirCreateEntry(&dirop, namep, &newFid);
3099 cm_BPlusDirCreateEntry(&dirop, namep, &newFid);
3103 cm_EndDirOp(&dirop);
3105 /* now try to create the new dir's entry, too, but be careful to
3106 * make sure that we don't merge in old info. Since we weren't locking
3107 * out any requests during the file's creation, we may have pretty old
3111 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3112 code = cm_GetSCache(&newFid, &scp, userp, reqp);
3114 lock_ObtainMutex(&scp->mx);
3115 if (!cm_HaveCallback(scp)) {
3116 cm_MergeStatus(dscp, scp, &newLinkStatus, &volSync,
3119 lock_ReleaseMutex(&scp->mx);
3120 cm_ReleaseSCache(scp);
3124 /* and return error code */
3128 long cm_RemoveDir(cm_scache_t *dscp, char *namep, cm_user_t *userp,
3135 AFSFetchStatus updatedDirStatus;
3137 struct rx_connection * callp;
3140 /* before starting the RPC, mark that we're changing the directory data,
3141 * so that someone who does a chmod on the dir will wait until our
3144 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, &dirop);
3145 lock_ObtainMutex(&dscp->mx);
3146 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3147 lock_ReleaseMutex(&dscp->mx);
3149 cm_EndDirOp(&dirop);
3154 /* try the RPC now */
3155 osi_Log1(afsd_logp, "CALL RemoveDir scp 0x%p", dscp);
3157 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3161 dirAFSFid.Volume = dscp->fid.volume;
3162 dirAFSFid.Vnode = dscp->fid.vnode;
3163 dirAFSFid.Unique = dscp->fid.unique;
3165 callp = cm_GetRxConn(connp);
3166 code = RXAFS_RemoveDir(callp, &dirAFSFid, namep,
3167 &updatedDirStatus, &volSync);
3168 rx_PutConnection(callp);
3170 } while (cm_Analyze(connp, userp, reqp,
3171 &dscp->fid, &volSync, NULL, NULL, code));
3172 code = cm_MapRPCErrorRmdir(code, reqp);
3175 osi_Log1(afsd_logp, "CALL RemoveDir FAILURE, code 0x%x", code);
3177 osi_Log0(afsd_logp, "CALL RemoveDir SUCCESS");
3180 lock_ObtainWrite(&dirop.scp->dirlock);
3181 dirop.lockType = CM_DIRLOCK_WRITE;
3183 lock_ObtainMutex(&dscp->mx);
3184 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3186 cm_dnlcRemove(dscp, namep);
3187 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, CM_MERGEFLAG_DIROP);
3189 lock_ReleaseMutex(&dscp->mx);
3192 if (cm_CheckDirOpForSingleChange(&dirop)) {
3193 cm_DirDeleteEntry(&dirop, namep);
3195 cm_BPlusDirDeleteEntry(&dirop, namep);
3199 cm_EndDirOp(&dirop);
3201 /* and return error code */
3205 long cm_Open(cm_scache_t *scp, int type, cm_user_t *userp)
3207 /* grab mutex on contents */
3208 lock_ObtainMutex(&scp->mx);
3210 /* reset the prefetch info */
3211 scp->prefetch.base.LowPart = 0; /* base */
3212 scp->prefetch.base.HighPart = 0;
3213 scp->prefetch.end.LowPart = 0; /* and end */
3214 scp->prefetch.end.HighPart = 0;
3216 /* release mutex on contents */
3217 lock_ReleaseMutex(&scp->mx);
3223 long cm_Rename(cm_scache_t *oldDscp, char *oldNamep, cm_scache_t *newDscp,
3224 char *newNamep, cm_user_t *userp, cm_req_t *reqp)
3228 AFSFid oldDirAFSFid;
3229 AFSFid newDirAFSFid;
3231 AFSFetchStatus updatedOldDirStatus;
3232 AFSFetchStatus updatedNewDirStatus;
3235 struct rx_connection * callp;
3236 cm_dirOp_t oldDirOp;
3239 cm_dirOp_t newDirOp;
3241 /* before starting the RPC, mark that we're changing the directory data,
3242 * so that someone who does a chmod on the dir will wait until our call
3243 * completes. We do this in vnode order so that we don't deadlock,
3244 * which makes the code a little verbose.
3246 if (oldDscp == newDscp) {
3247 /* check for identical names */
3248 if (strcmp(oldNamep, newNamep) == 0)
3249 return CM_ERROR_RENAME_IDENTICAL;
3252 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3253 lock_ObtainMutex(&oldDscp->mx);
3254 cm_dnlcRemove(oldDscp, oldNamep);
3255 cm_dnlcRemove(oldDscp, newNamep);
3256 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3257 CM_SCACHESYNC_STOREDATA);
3258 lock_ReleaseMutex(&oldDscp->mx);
3260 cm_EndDirOp(&oldDirOp);
3264 /* two distinct dir vnodes */
3266 if (oldDscp->fid.cell != newDscp->fid.cell ||
3267 oldDscp->fid.volume != newDscp->fid.volume)
3268 return CM_ERROR_CROSSDEVLINK;
3270 /* shouldn't happen that we have distinct vnodes for two
3271 * different files, but could due to deliberate attack, or
3272 * stale info. Avoid deadlocks and quit now.
3274 if (oldDscp->fid.vnode == newDscp->fid.vnode)
3275 return CM_ERROR_CROSSDEVLINK;
3277 if (oldDscp->fid.vnode < newDscp->fid.vnode) {
3278 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3279 lock_ObtainMutex(&oldDscp->mx);
3280 cm_dnlcRemove(oldDscp, oldNamep);
3281 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3282 CM_SCACHESYNC_STOREDATA);
3283 lock_ReleaseMutex(&oldDscp->mx);
3285 cm_EndDirOp(&oldDirOp);
3287 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3288 lock_ObtainMutex(&newDscp->mx);
3289 cm_dnlcRemove(newDscp, newNamep);
3290 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3291 CM_SCACHESYNC_STOREDATA);
3292 lock_ReleaseMutex(&newDscp->mx);
3294 cm_EndDirOp(&newDirOp);
3296 /* cleanup first one */
3297 lock_ObtainMutex(&oldDscp->mx);
3298 cm_SyncOpDone(oldDscp, NULL,
3299 CM_SCACHESYNC_STOREDATA);
3300 lock_ReleaseMutex(&oldDscp->mx);
3301 cm_EndDirOp(&oldDirOp);
3306 /* lock the new vnode entry first */
3307 cm_BeginDirOp(newDscp, userp, reqp, CM_DIRLOCK_NONE, &newDirOp);
3308 lock_ObtainMutex(&newDscp->mx);
3309 cm_dnlcRemove(newDscp, newNamep);
3310 code = cm_SyncOp(newDscp, NULL, userp, reqp, 0,
3311 CM_SCACHESYNC_STOREDATA);
3312 lock_ReleaseMutex(&newDscp->mx);
3314 cm_EndDirOp(&newDirOp);
3316 cm_BeginDirOp(oldDscp, userp, reqp, CM_DIRLOCK_NONE, &oldDirOp);
3317 lock_ObtainMutex(&oldDscp->mx);
3318 cm_dnlcRemove(oldDscp, oldNamep);
3319 code = cm_SyncOp(oldDscp, NULL, userp, reqp, 0,
3320 CM_SCACHESYNC_STOREDATA);
3321 lock_ReleaseMutex(&oldDscp->mx);
3323 cm_EndDirOp(&oldDirOp);
3325 /* cleanup first one */
3326 lock_ObtainMutex(&newDscp->mx);
3327 cm_SyncOpDone(newDscp, NULL,
3328 CM_SCACHESYNC_STOREDATA);
3329 lock_ReleaseMutex(&newDscp->mx);
3330 cm_EndDirOp(&newDirOp);
3334 } /* two distinct vnodes */
3341 /* try the RPC now */
3342 osi_Log2(afsd_logp, "CALL Rename old scp 0x%p new scp 0x%p",
3345 code = cm_ConnFromFID(&oldDscp->fid, userp, reqp, &connp);
3349 oldDirAFSFid.Volume = oldDscp->fid.volume;
3350 oldDirAFSFid.Vnode = oldDscp->fid.vnode;
3351 oldDirAFSFid.Unique = oldDscp->fid.unique;
3352 newDirAFSFid.Volume = newDscp->fid.volume;
3353 newDirAFSFid.Vnode = newDscp->fid.vnode;
3354 newDirAFSFid.Unique = newDscp->fid.unique;
3356 callp = cm_GetRxConn(connp);
3357 code = RXAFS_Rename(callp, &oldDirAFSFid, oldNamep,
3358 &newDirAFSFid, newNamep,
3359 &updatedOldDirStatus, &updatedNewDirStatus,
3361 rx_PutConnection(callp);
3363 } while (cm_Analyze(connp, userp, reqp, &oldDscp->fid,
3364 &volSync, NULL, NULL, code));
3365 code = cm_MapRPCError(code, reqp);
3368 osi_Log1(afsd_logp, "CALL Rename FAILURE, code 0x%x", code);
3370 osi_Log0(afsd_logp, "CALL Rename SUCCESS");
3372 /* update the individual stat cache entries for the directories */
3374 lock_ObtainWrite(&oldDirOp.scp->dirlock);
3375 oldDirOp.lockType = CM_DIRLOCK_WRITE;
3377 lock_ObtainMutex(&oldDscp->mx);
3378 cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_STOREDATA);
3381 cm_MergeStatus(NULL, oldDscp, &updatedOldDirStatus, &volSync,
3382 userp, CM_MERGEFLAG_DIROP);
3383 lock_ReleaseMutex(&oldDscp->mx);
3386 if (cm_CheckDirOpForSingleChange(&oldDirOp)) {
3389 diropCode = cm_BPlusDirLookup(&oldDirOp, oldNamep, &fileFid);
3390 if (diropCode == CM_ERROR_INEXACT_MATCH)
3392 else if (diropCode == EINVAL)
3394 diropCode = cm_DirLookup(&oldDirOp, oldNamep, &fileFid);
3396 if (diropCode == 0) {
3398 diropCode = cm_DirCreateEntry(&oldDirOp, newNamep, &fileFid);
3400 cm_BPlusDirCreateEntry(&oldDirOp, newNamep, &fileFid);
3404 if (diropCode == 0) {
3405 diropCode = cm_DirDeleteEntry(&oldDirOp, oldNamep);
3407 cm_BPlusDirDeleteEntry(&oldDirOp, oldNamep);
3413 cm_EndDirOp(&oldDirOp);
3415 /* and update it for the new one, too, if necessary */
3418 lock_ObtainWrite(&newDirOp.scp->dirlock);
3419 newDirOp.lockType = CM_DIRLOCK_WRITE;
3421 lock_ObtainMutex(&newDscp->mx);
3422 cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_STOREDATA);
3424 cm_MergeStatus(NULL, newDscp, &updatedNewDirStatus, &volSync,
3425 userp, CM_MERGEFLAG_DIROP);
3426 lock_ReleaseMutex(&newDscp->mx);
3429 /* we only make the local change if we successfully made
3430 the change in the old directory AND there was only one
3431 change in the new directory */
3432 if (diropCode == 0 && cm_CheckDirOpForSingleChange(&newDirOp)) {
3433 cm_DirCreateEntry(&newDirOp, newNamep, &fileFid);
3435 cm_BPlusDirCreateEntry(&newDirOp, newNamep, &fileFid);
3439 cm_EndDirOp(&newDirOp);
3442 /* and return error code */
3446 /* Byte range locks:
3448 The OpenAFS Windows client has to fake byte range locks given no
3449 server side support for such locks. This is implemented as keyed
3450 byte range locks on the cache manager.