2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
33 extern void afsi_log(char *pattern, ...);
36 int cm_enableServerLocks = 1;
38 int cm_followBackupPath = 0;
41 * Case-folding array. This was constructed by inspecting of SMBtrace output.
42 * I do not know anything more about it.
44 unsigned char cm_foldUpper[256] = {
45 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
46 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
47 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
48 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
49 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
50 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
51 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
52 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
53 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
54 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
55 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
56 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
57 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
58 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
59 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
60 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
61 0x80, 0x9a, 0x90, 0x41, 0x8e, 0x41, 0x8f, 0x80,
62 0x45, 0x45, 0x45, 0x49, 0x49, 0x49, 0x8e, 0x8f,
63 0x90, 0x92, 0x92, 0x4f, 0x99, 0x4f, 0x55, 0x55,
64 0x59, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
65 0x41, 0x49, 0x4f, 0x55, 0xa5, 0xa5, 0x56, 0xa7,
66 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
67 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
68 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
69 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
70 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
71 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
72 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
73 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
74 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
75 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
76 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
80 * Case-insensitive string comparison. We used to use stricmp, but it doesn't
81 * know about 8-bit characters (e.g. 129 is lowercase u-umlaut, 154 is
82 * upper-case u-umlaut).
84 int cm_stricmp(const char *str1, const char *str2)
96 c1 = (char) cm_foldUpper[(unsigned char)(*str1++)];
97 c2 = (char) cm_foldUpper[(unsigned char)(*str2++)];
107 /* return success if we can open this file in this mode */
108 long cm_CheckOpen(cm_scache_t *scp, int openMode, int trunc, cm_user_t *userp,
116 rights |= PRSFS_READ;
117 if (openMode == 1 || openMode == 2 || trunc)
118 rights |= PRSFS_WRITE;
120 lock_ObtainWrite(&scp->rw);
122 code = cm_SyncOp(scp, NULL, userp, reqp, rights,
123 CM_SCACHESYNC_GETSTATUS
124 | CM_SCACHESYNC_NEEDCALLBACK
125 | CM_SCACHESYNC_LOCK);
128 ((rights & PRSFS_WRITE) || (rights & PRSFS_READ)) &&
129 scp->fileType == CM_SCACHETYPE_FILE) {
132 unsigned int sLockType;
133 LARGE_INTEGER LOffset, LLength;
135 /* Check if there's some sort of lock on the file at the
138 key = cm_GenerateKey(CM_SESSION_CMINT,0,0);
140 if (rights & PRSFS_WRITE)
143 sLockType = LOCKING_ANDX_SHARED_LOCK;
145 LOffset.HighPart = CM_FLSHARE_OFFSET_HIGH;
146 LOffset.LowPart = CM_FLSHARE_OFFSET_LOW;
147 LLength.HighPart = CM_FLSHARE_LENGTH_HIGH;
148 LLength.LowPart = CM_FLSHARE_LENGTH_LOW;
150 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp, NULL);
153 cm_Unlock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp);
155 /* In this case, we allow the file open to go through even
156 though we can't enforce mandatory locking on the
158 if (code == CM_ERROR_NOACCESS &&
159 !(rights & PRSFS_WRITE))
163 case CM_ERROR_ALLOFFLINE:
164 case CM_ERROR_ALLDOWN:
165 case CM_ERROR_ALLBUSY:
166 case CM_ERROR_TIMEDOUT:
168 case CM_ERROR_WOULDBLOCK:
171 code = CM_ERROR_SHARING_VIOLATION;
176 } else if (code != 0) {
180 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
184 lock_ReleaseWrite(&scp->rw);
189 /* return success if we can open this file in this mode */
190 long cm_CheckNTOpen(cm_scache_t *scp, unsigned int desiredAccess,
191 unsigned int createDisp, cm_user_t *userp, cm_req_t *reqp,
192 cm_lock_data_t **ldpp)
197 osi_assertx(ldpp != NULL, "null cm_lock_data_t");
200 /* Always allow delete; the RPC will tell us if it's OK */
203 if (desiredAccess == DELETE)
206 if (desiredAccess & (AFS_ACCESS_READ|AFS_ACCESS_EXECUTE))
207 rights |= (scp->fileType == CM_SCACHETYPE_DIRECTORY ? PRSFS_LOOKUP : PRSFS_READ);
209 /* We used to require PRSFS_WRITE if createDisp was 4
210 (OPEN_ALWAYS) even if AFS_ACCESS_WRITE was not requested.
211 However, we don't need to do that since the existence of the
212 scp implies that we don't need to create it. */
213 if (desiredAccess & AFS_ACCESS_WRITE)
214 rights |= PRSFS_WRITE;
216 if (desiredAccess & DELETE)
217 rights |= PRSFS_DELETE;
219 lock_ObtainWrite(&scp->rw);
221 code = cm_SyncOp(scp, NULL, userp, reqp, rights,
222 CM_SCACHESYNC_GETSTATUS
223 | CM_SCACHESYNC_NEEDCALLBACK
224 | CM_SCACHESYNC_LOCK);
227 * If the open will fail because the volume is readonly, then we will
228 * return an access denied error instead. This is to help brain-dead
229 * apps run correctly on replicated volumes.
230 * See defect 10007 for more information.
232 if (code == CM_ERROR_READONLY)
233 code = CM_ERROR_NOACCESS;
236 ((rights & PRSFS_WRITE) || (rights & PRSFS_READ)) &&
237 scp->fileType == CM_SCACHETYPE_FILE) {
239 unsigned int sLockType;
240 LARGE_INTEGER LOffset, LLength;
242 /* Check if there's some sort of lock on the file at the
245 key = cm_GenerateKey(CM_SESSION_CMINT,0,0);
246 if (rights & PRSFS_WRITE)
249 sLockType = LOCKING_ANDX_SHARED_LOCK;
251 /* single byte lock at offset 0x0100 0000 0000 0000 */
252 LOffset.HighPart = CM_FLSHARE_OFFSET_HIGH;
253 LOffset.LowPart = CM_FLSHARE_OFFSET_LOW;
254 LLength.HighPart = CM_FLSHARE_LENGTH_HIGH;
255 LLength.LowPart = CM_FLSHARE_LENGTH_LOW;
257 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, reqp, NULL);
260 (*ldpp) = (cm_lock_data_t *)malloc(sizeof(cm_lock_data_t));
267 (*ldpp)->sLockType = sLockType;
268 (*ldpp)->LOffset.HighPart = LOffset.HighPart;
269 (*ldpp)->LOffset.LowPart = LOffset.LowPart;
270 (*ldpp)->LLength.HighPart = LLength.HighPart;
271 (*ldpp)->LLength.LowPart = LLength.LowPart;
273 /* In this case, we allow the file open to go through even
274 though we can't enforce mandatory locking on the
276 if (code == CM_ERROR_NOACCESS &&
277 !(rights & PRSFS_WRITE))
281 case CM_ERROR_ALLOFFLINE:
282 case CM_ERROR_ALLDOWN:
283 case CM_ERROR_ALLBUSY:
284 case CM_ERROR_TIMEDOUT:
286 case CM_ERROR_WOULDBLOCK:
289 code = CM_ERROR_SHARING_VIOLATION;
293 } else if (code != 0) {
298 lock_ReleaseWrite(&scp->rw);
301 osi_Log3(afsd_logp,"cm_CheckNTOpen scp 0x%p ldp 0x%p code 0x%x", scp, *ldpp, code);
305 extern long cm_CheckNTOpenDone(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
306 cm_lock_data_t ** ldpp)
308 osi_Log2(afsd_logp,"cm_CheckNTOpenDone scp 0x%p ldp 0x%p", scp, *ldpp);
309 lock_ObtainWrite(&scp->rw);
311 cm_Unlock(scp, (*ldpp)->sLockType, (*ldpp)->LOffset, (*ldpp)->LLength,
312 (*ldpp)->key, 0, userp, reqp);
316 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
317 lock_ReleaseWrite(&scp->rw);
321 * When CAP_NT_SMBS has been negotiated, deletion (of files or directories) is
322 * done in three steps:
323 * (1) open for deletion (NT_CREATE_AND_X)
324 * (2) set for deletion on close (NT_TRANSACTION2, SET_FILE_INFO)
326 * We must not do the RPC until step 3. But if we are going to return an error
327 * code (e.g. directory not empty), we must return it by step 2, otherwise most
328 * clients will not notice it. So we do a preliminary check. For deleting
329 * files, this is almost free, since we have already done the RPC to get the
330 * parent directory's status bits. But for deleting directories, we must do an
331 * additional RPC to get the directory's data to check if it is empty. Sigh.
333 long cm_CheckNTDelete(cm_scache_t *dscp, cm_scache_t *scp, cm_user_t *userp,
339 cm_dirEntry_t *dep = 0;
340 unsigned short *hashTable;
342 int BeyondPage = 0, HaveDot = 0, HaveDotDot = 0;
345 /* First check permissions */
346 lock_ObtainWrite(&scp->rw);
347 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_DELETE,
348 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
350 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
351 lock_ReleaseWrite(&scp->rw);
355 /* If deleting directory, must be empty */
357 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
360 thyper.HighPart = 0; thyper.LowPart = 0;
361 code = buf_Get(scp, &thyper, reqp, &bufferp);
365 lock_ObtainMutex(&bufferp->mx);
366 lock_ObtainWrite(&scp->rw);
369 code = cm_SyncOp(scp, bufferp, userp, reqp, 0,
370 CM_SCACHESYNC_NEEDCALLBACK
372 | CM_SCACHESYNC_BUFLOCKED);
376 if (cm_HaveBuffer(scp, bufferp, 1))
379 /* otherwise, load the buffer and try again */
380 lock_ReleaseMutex(&bufferp->mx);
381 code = cm_GetBuffer(scp, bufferp, NULL, userp, reqp);
382 lock_ReleaseWrite(&scp->rw);
383 lock_ObtainMutex(&bufferp->mx);
384 lock_ObtainWrite(&scp->rw);
385 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
390 lock_ReleaseWrite(&scp->rw);
393 /* We try to determine emptiness without looking beyond the first page,
394 * and without assuming "." and ".." are present and are on the first
395 * page (though these assumptions might, after all, be reasonable).
397 hashTable = (unsigned short *)(bufferp->datap + (32 * 5));
398 for (i=0; i<128; i++) {
399 idx = ntohs(hashTable[i]);
405 dep = (cm_dirEntry_t *)(bufferp->datap + (32 * idx));
406 if (strcmp(dep->name, ".") == 0)
408 else if (strcmp(dep->name, "..") == 0)
411 code = CM_ERROR_NOTEMPTY;
414 idx = ntohs(dep->next);
417 if (BeyondPage && HaveDot && HaveDotDot)
418 code = CM_ERROR_NOTEMPTY;
422 lock_ReleaseMutex(&bufferp->mx);
423 buf_Release(bufferp);
425 lock_ReleaseWrite(&scp->rw);
430 * Iterate through all entries in a directory.
431 * When the function funcp is called, the buffer is locked but the
432 * directory vnode is not.
434 * If the retscp parameter is not NULL, the parmp must be a
435 * cm_lookupSearch_t object.
437 long cm_ApplyDir(cm_scache_t *scp, cm_DirFuncp_t funcp, void *parmp,
438 osi_hyper_t *startOffsetp, cm_user_t *userp, cm_req_t *reqp,
439 cm_scache_t **retscp)
443 cm_dirEntry_t *dep = 0;
446 osi_hyper_t dirLength;
447 osi_hyper_t bufferOffset;
448 osi_hyper_t curOffset;
452 cm_pageHeader_t *pageHeaderp;
454 long nextEntryCookie;
455 int numDirChunks; /* # of 32 byte dir chunks in this entry */
457 /* get the directory size */
458 lock_ObtainWrite(&scp->rw);
459 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_LOOKUP,
460 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
461 lock_ReleaseWrite(&scp->rw);
465 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
466 return CM_ERROR_NOTDIR;
468 if (retscp) /* if this is a lookup call */
470 cm_lookupSearch_t* sp = parmp;
473 #ifdef AFS_FREELANCE_CLIENT
474 /* Freelance entries never end up in the DNLC because they
475 * do not have an associated cm_server_t
477 !(cm_freelanceEnabled &&
478 sp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
479 sp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
480 #else /* !AFS_FREELANCE_CLIENT */
485 int casefold = sp->caseFold;
486 sp->caseFold = 0; /* we have a strong preference for exact matches */
487 if ( *retscp = cm_dnlcLookup(scp, sp)) /* dnlc hit */
489 sp->caseFold = casefold;
492 sp->caseFold = casefold;
496 * see if we can find it using the directory hash tables.
497 * we can only do exact matches, since the hash is case
500 if (funcp != (cm_DirFuncp_t)cm_BPlusDirFoo)
509 code = cm_BeginDirOp(scp, userp, reqp, CM_DIRLOCK_READ,
510 CM_DIROP_FLAG_NONE, &dirop);
514 code = cm_BPlusDirLookup(&dirop, sp->nsearchNamep, &sp->fid);
519 code = cm_DirLookup(&dirop, sp->searchNamep, &sp->fid);
527 sp->ExactFound = TRUE;
528 *retscp = NULL; /* force caller to call cm_GetSCache() */
533 if (sp->caseFold && code == CM_ERROR_INEXACT_MATCH) {
536 sp->ExactFound = FALSE;
537 *retscp = NULL; /* force caller to call cm_GetSCache() */
541 return CM_ERROR_BPLUS_NOMATCH;
548 * XXX We only get the length once. It might change when we drop the
551 dirLength = scp->length;
554 bufferOffset.LowPart = bufferOffset.HighPart = 0;
556 curOffset = *startOffsetp;
558 curOffset.HighPart = 0;
559 curOffset.LowPart = 0;
563 /* make sure that curOffset.LowPart doesn't point to the first
564 * 32 bytes in the 2nd through last dir page, and that it
565 * doesn't point at the first 13 32-byte chunks in the first
566 * dir page, since those are dir and page headers, and don't
567 * contain useful information.
569 temp = curOffset.LowPart & (2048-1);
570 if (curOffset.HighPart == 0 && curOffset.LowPart < 2048) {
571 /* we're in the first page */
572 if (temp < 13*32) temp = 13*32;
575 /* we're in a later dir page */
576 if (temp < 32) temp = 32;
579 /* make sure the low order 5 bits are zero */
582 /* now put temp bits back ito curOffset.LowPart */
583 curOffset.LowPart &= ~(2048-1);
584 curOffset.LowPart |= temp;
586 /* check if we've passed the dir's EOF */
587 if (LargeIntegerGreaterThanOrEqualTo(curOffset, dirLength))
590 /* see if we can use the bufferp we have now; compute in which
591 * page the current offset would be, and check whether that's
592 * the offset of the buffer we have. If not, get the buffer.
594 thyper.HighPart = curOffset.HighPart;
595 thyper.LowPart = curOffset.LowPart & ~(cm_data.buf_blockSize-1);
596 if (!bufferp || !LargeIntegerEqualTo(thyper, bufferOffset)) {
599 lock_ReleaseMutex(&bufferp->mx);
600 buf_Release(bufferp);
604 code = buf_Get(scp, &thyper, reqp, &bufferp);
606 /* if buf_Get() fails we do not have a buffer object to lock */
611 lock_ObtainMutex(&bufferp->mx);
612 bufferOffset = thyper;
614 /* now get the data in the cache */
616 lock_ObtainWrite(&scp->rw);
617 code = cm_SyncOp(scp, bufferp, userp, reqp,
619 CM_SCACHESYNC_NEEDCALLBACK
621 | CM_SCACHESYNC_BUFLOCKED);
623 lock_ReleaseWrite(&scp->rw);
626 cm_SyncOpDone(scp, bufferp, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_READ | CM_SCACHESYNC_BUFLOCKED);
628 if (cm_HaveBuffer(scp, bufferp, 1)) {
629 lock_ReleaseWrite(&scp->rw);
633 /* otherwise, load the buffer and try again */
634 lock_ReleaseMutex(&bufferp->mx);
635 code = cm_GetBuffer(scp, bufferp, NULL, userp,
637 lock_ReleaseWrite(&scp->rw);
638 lock_ObtainMutex(&bufferp->mx);
643 lock_ReleaseMutex(&bufferp->mx);
644 buf_Release(bufferp);
648 } /* if (wrong buffer) ... */
650 /* now we have the buffer containing the entry we're interested
651 * in; copy it out if it represents a non-deleted entry.
653 entryInDir = curOffset.LowPart & (2048-1);
654 entryInBuffer = curOffset.LowPart & (cm_data.buf_blockSize - 1);
656 /* page header will help tell us which entries are free. Page
657 * header can change more often than once per buffer, since
658 * AFS 3 dir page size may be less than (but not more than) a
659 * buffer package buffer.
661 /* only look intra-buffer */
662 temp = curOffset.LowPart & (cm_data.buf_blockSize - 1);
663 temp &= ~(2048 - 1); /* turn off intra-page bits */
664 pageHeaderp = (cm_pageHeader_t *) (bufferp->datap + temp);
666 /* now determine which entry we're looking at in the page. If
667 * it is free (there's a free bitmap at the start of the dir),
668 * we should skip these 32 bytes.
670 slotInPage = (entryInDir & 0x7e0) >> 5;
671 if (!(pageHeaderp->freeBitmap[slotInPage>>3]
672 & (1 << (slotInPage & 0x7)))) {
673 /* this entry is free */
674 numDirChunks = 1; /* only skip this guy */
678 tp = bufferp->datap + entryInBuffer;
679 dep = (cm_dirEntry_t *) tp; /* now points to AFS3 dir entry */
682 * here are some consistency checks
684 if (dep->flag != CM_DIR_FFIRST ||
685 strlen(dep->name) > 256) {
686 code = CM_ERROR_INVAL;
688 "cm_ApplyDir invalid directory entry for scp %p bufp %p",
690 osi_Log4(afsd_logp,"... cell %u vol %u vnode %u uniq %u",
691 scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique);
692 bufferp->dataVersion = CM_BUF_VERSION_BAD;
696 /* while we're here, compute the next entry's location, too,
697 * since we'll need it when writing out the cookie into the
698 * dir listing stream.
700 numDirChunks = cm_NameEntries(dep->name, NULL);
702 /* compute the offset of the cookie representing the next entry */
703 nextEntryCookie = curOffset.LowPart
704 + (CM_DIR_CHUNKSIZE * numDirChunks);
706 if (dep->fid.vnode != 0) {
707 /* this is one of the entries to use: it is not deleted */
708 code = (*funcp)(scp, dep, parmp, &curOffset);
711 } /* if we're including this name */
714 /* and adjust curOffset to be where the new cookie is */
716 thyper.LowPart = CM_DIR_CHUNKSIZE * numDirChunks;
717 curOffset = LargeIntegerAdd(thyper, curOffset);
718 } /* while copying data for dir listing */
720 /* release the mutex */
722 lock_ReleaseMutex(&bufferp->mx);
723 buf_Release(bufferp);
728 int cm_NoneUpper(normchar_t *s)
732 if (c >= 'A' && c <= 'Z')
737 int cm_NoneLower(normchar_t *s)
741 if (c >= 'a' && c <= 'z')
746 long cm_LookupSearchProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
749 cm_lookupSearch_t *sp;
751 normchar_t matchName[MAX_PATH];
752 int looking_for_short_name = FALSE;
754 sp = (cm_lookupSearch_t *) rockp;
756 if (cm_FsStringToNormString(dep->name, -1, matchName, lengthof(matchName)) == 0) {
757 /* Can't normalize FS string. */
762 match = cm_NormStrCmpI(matchName, sp->nsearchNamep);
764 match = cm_NormStrCmp(matchName, sp->nsearchNamep);
768 && !cm_Is8Dot3(matchName)) {
770 cm_Gen8Dot3NameInt(dep->name, &dep->fid, matchName, NULL);
772 match = cm_NormStrCmpI(matchName, sp->nsearchNamep);
774 match = cm_NormStrCmp(matchName, sp->nsearchNamep);
775 looking_for_short_name = TRUE;
785 if (!sp->caseFold || looking_for_short_name) {
786 cm_SetFid(&sp->fid, sp->fid.cell, sp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
787 return CM_ERROR_STOPNOW;
791 * If we get here, we are doing a case-insensitive search, and we
792 * have found a match. Now we determine what kind of match it is:
793 * exact, lower-case, upper-case, or none of the above. This is done
794 * in order to choose among matches, if there are more than one.
797 /* Exact matches are the best. */
798 match = cm_NormStrCmp(matchName, sp->nsearchNamep);
801 cm_SetFid(&sp->fid, sp->fid.cell, sp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
802 return CM_ERROR_STOPNOW;
805 /* Lower-case matches are next. */
808 if (cm_NoneUpper(matchName)) {
813 /* Upper-case matches are next. */
816 if (cm_NoneLower(matchName)) {
821 /* General matches are last. */
827 cm_SetFid(&sp->fid, sp->fid.cell, sp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
831 /* read the contents of a mount point into the appropriate string.
832 * called with write locked scp, and returns with locked scp.
834 long cm_ReadMountPoint(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
838 if (scp->mountPointStringp[0])
841 #ifdef AFS_FREELANCE_CLIENT
842 /* File servers do not have data for freelance entries */
843 if (cm_freelanceEnabled &&
844 scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
845 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
847 code = cm_FreelanceFetchMountPointString(scp);
849 #endif /* AFS_FREELANCE_CLIENT */
851 char temp[MOUNTPOINTLEN];
854 /* otherwise, we have to read it in */
855 offset.LowPart = offset.HighPart = 0;
856 code = cm_GetData(scp, &offset, temp, MOUNTPOINTLEN, userp, reqp);
861 * scp->length is the actual length of the mount point string.
862 * It is current because cm_GetData merged the most up to date
863 * status info into scp and has not dropped the rwlock since.
865 if (scp->length.LowPart > MOUNTPOINTLEN - 1)
866 return CM_ERROR_TOOBIG;
867 if (scp->length.LowPart == 0)
868 return CM_ERROR_INVAL;
870 /* convert the terminating dot to a NUL */
871 temp[scp->length.LowPart - 1] = 0;
872 memcpy(scp->mountPointStringp, temp, scp->length.LowPart);
879 /* called with a locked scp and chases the mount point, yielding outScpp.
880 * scp remains write locked, just for simplicity of describing the interface.
882 long cm_FollowMountPoint(cm_scache_t *scp, cm_scache_t *dscp, cm_user_t *userp,
883 cm_req_t *reqp, cm_scache_t **outScpp)
885 fschar_t *cellNamep = NULL;
886 fschar_t *volNamep = NULL;
890 cm_volume_t *volp = NULL;
899 if (scp->mountRootFid.cell != 0 && scp->mountRootGen >= cm_data.mountRootGen) {
900 tfid = scp->mountRootFid;
901 lock_ReleaseWrite(&scp->rw);
902 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
903 lock_ObtainWrite(&scp->rw);
907 /* parse the volume name */
908 mpNamep = scp->mountPointStringp;
910 return CM_ERROR_NOSUCHPATH;
911 mtType = *scp->mountPointStringp;
913 cp = cm_FsStrChr(mpNamep, _FS(':'));
915 /* cellular mount point */
916 cellNamep = (fschar_t *)malloc((cp - mpNamep) * sizeof(fschar_t));
917 cm_FsStrCpyN(cellNamep, cp - mpNamep, mpNamep + 1, cp - mpNamep - 1);
918 volNamep = cm_FsStrDup(cp+1);
920 /* now look up the cell */
921 lock_ReleaseWrite(&scp->rw);
922 cellp = cm_GetCell(cellNamep, CM_FLAG_CREATE);
923 lock_ObtainWrite(&scp->rw);
926 volNamep = cm_FsStrDup(mpNamep + 1);
928 #ifdef AFS_FREELANCE_CLIENT
930 * Mount points in the Freelance cell should default
931 * to the workstation cell.
933 if (cm_freelanceEnabled &&
934 scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
935 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
937 fschar_t rootCellName[256]="";
938 cm_GetRootCellName(rootCellName);
939 cellp = cm_GetCell(rootCellName, 0);
941 #endif /* AFS_FREELANCE_CLIENT */
942 cellp = cm_FindCellByID(scp->fid.cell, 0);
946 code = CM_ERROR_NOSUCHCELL;
950 vnLength = cm_FsStrLen(volNamep);
951 if (vnLength >= 8 && cm_FsStrCmp(volNamep + vnLength - 7, ".backup") == 0)
952 targetType = BACKVOL;
953 else if (vnLength >= 10
954 && cm_FsStrCmp(volNamep + vnLength - 9, ".readonly") == 0)
959 /* check for backups within backups */
960 if (targetType == BACKVOL
961 && (scp->flags & (CM_SCACHEFLAG_RO | CM_SCACHEFLAG_PURERO))
962 == CM_SCACHEFLAG_RO) {
963 code = CM_ERROR_NOSUCHVOLUME;
967 /* now we need to get the volume */
968 lock_ReleaseWrite(&scp->rw);
969 if (cm_VolNameIsID(volNamep)) {
970 code = cm_FindVolumeByID(cellp, atoi(volNamep), userp, reqp,
971 CM_GETVOL_FLAG_CREATE, &volp);
973 code = cm_FindVolumeByName(cellp, volNamep, userp, reqp,
974 CM_GETVOL_FLAG_CREATE, &volp);
976 lock_ObtainWrite(&scp->rw);
979 afs_uint32 cell, volume;
980 cm_vol_state_t *statep;
982 cell = cellp->cellID;
984 /* if the mt pt originates in a .backup volume (not a .readonly)
985 * and FollowBackupPath is active, and if there is a .backup
986 * volume for the target, then use the .backup of the target
987 * instead of the read-write.
989 if (cm_followBackupPath &&
990 volp->vol[BACKVOL].ID != 0 &&
991 (dscp->flags & (CM_SCACHEFLAG_RO|CM_SCACHEFLAG_PURERO)) == CM_SCACHEFLAG_RO &&
992 (targetType == RWVOL || targetType == ROVOL && volp->vol[ROVOL].ID == 0)
994 targetType = BACKVOL;
996 /* if the mt pt is in a read-only volume (not just a
997 * backup), and if there is a read-only volume for the
998 * target, and if this is a targetType '#' mount point, use
999 * the read-only, otherwise use the one specified.
1001 else if (mtType == '#' && targetType == RWVOL &&
1002 (scp->flags & CM_SCACHEFLAG_PURERO) &&
1003 volp->vol[ROVOL].ID != 0) {
1007 lock_ObtainWrite(&volp->rw);
1008 statep = cm_VolumeStateByType(volp, targetType);
1009 volume = statep->ID;
1010 statep->dotdotFid = dscp->fid;
1011 lock_ReleaseWrite(&volp->rw);
1013 /* the rest of the fid is a magic number */
1014 cm_SetFid(&scp->mountRootFid, cell, volume, 1, 1);
1015 scp->mountRootGen = cm_data.mountRootGen;
1017 tfid = scp->mountRootFid;
1018 lock_ReleaseWrite(&scp->rw);
1019 code = cm_GetSCache(&tfid, outScpp, userp, reqp);
1020 lock_ObtainWrite(&scp->rw);
1033 long cm_LookupInternal(cm_scache_t *dscp, clientchar_t *cnamep, long flags, cm_user_t *userp,
1034 cm_req_t *reqp, cm_scache_t **outScpp)
1037 int dnlcHit = 1; /* did we hit in the dnlc? yes, we did */
1038 cm_scache_t *tscp = NULL;
1039 cm_scache_t *mountedScp;
1040 cm_lookupSearch_t rock;
1042 normchar_t *nnamep = NULL;
1043 fschar_t *fnamep = NULL;
1048 memset(&rock, 0, sizeof(rock));
1050 if (dscp->fid.vnode == 1 && dscp->fid.unique == 1
1051 && cm_ClientStrCmp(cnamep, _C("..")) == 0) {
1052 if (dscp->dotdotFid.volume == 0)
1053 return CM_ERROR_NOSUCHVOLUME;
1054 rock.fid = dscp->dotdotFid;
1056 } else if (cm_ClientStrCmp(cnamep, _C(".")) == 0) {
1057 rock.fid = dscp->fid;
1061 nnamep = cm_ClientStringToNormStringAlloc(cnamep, -1, NULL);
1063 code = CM_ERROR_NOSUCHFILE;
1066 fnamep = cm_ClientStringToFsStringAlloc(cnamep, -1, NULL);
1068 code = CM_ERROR_NOSUCHFILE;
1073 if (flags & CM_FLAG_NOMOUNTCHASE) {
1074 /* In this case, we should go and call cm_Dir* functions
1075 directly since the following cm_ApplyDir() function will
1083 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ,
1084 CM_DIROP_FLAG_NONE, &dirop);
1087 code = cm_BPlusDirLookup(&dirop, nnamep, &rock.fid);
1092 code = cm_DirLookup(&dirop, fnamep, &rock.fid);
1094 cm_EndDirOp(&dirop);
1104 if (code == CM_ERROR_INEXACT_MATCH && (flags & CM_FLAG_CASEFOLD)) {
1111 code = CM_ERROR_BPLUS_NOMATCH;
1117 rock.fid.cell = dscp->fid.cell;
1118 rock.fid.volume = dscp->fid.volume;
1119 rock.searchNamep = fnamep;
1120 rock.nsearchNamep = nnamep;
1121 rock.caseFold = (flags & CM_FLAG_CASEFOLD);
1122 rock.hasTilde = ((cm_ClientStrChr(cnamep, '~') != NULL) ? 1 : 0);
1124 /* If NOMOUNTCHASE, bypass DNLC by passing NULL scp pointer */
1125 code = cm_ApplyDir(dscp, cm_LookupSearchProc, &rock, NULL, userp, reqp,
1126 (flags & CM_FLAG_NOMOUNTCHASE) ? NULL : &tscp);
1128 /* code == 0 means we fell off the end of the dir, while stopnow means
1129 * that we stopped early, probably because we found the entry we're
1130 * looking for. Any other non-zero code is an error.
1132 if (code && code != CM_ERROR_STOPNOW && code != CM_ERROR_BPLUS_NOMATCH) {
1133 /* if the cm_scache_t we are searching in is not a directory
1134 * we must return path not found because the error
1135 * is to describe the final component not an intermediary
1137 if (code == CM_ERROR_NOTDIR) {
1138 if (flags & CM_FLAG_CHECKPATH)
1139 code = CM_ERROR_NOSUCHPATH;
1141 code = CM_ERROR_NOSUCHFILE;
1147 getroot = (dscp==cm_data.rootSCachep) ;
1149 if (!cm_freelanceEnabled || !getroot) {
1150 if (flags & CM_FLAG_CHECKPATH)
1151 code = CM_ERROR_NOSUCHPATH;
1153 code = CM_ERROR_NOSUCHFILE;
1156 else if (!cm_ClientStrChr(cnamep, '#') &&
1157 !cm_ClientStrChr(cnamep, '%') &&
1158 cm_ClientStrCmpI(cnamep, _C("srvsvc")) &&
1159 cm_ClientStrCmpI(cnamep, _C("wkssvc")) &&
1160 cm_ClientStrCmpI(cnamep, _C("ipc$")))
1162 /* nonexistent dir on freelance root, so add it */
1163 fschar_t fullname[CELL_MAXNAMELEN + 1] = "."; /* +1 so that when we skip the . the size is still CELL_MAXNAMELEN */
1167 osi_Log1(afsd_logp,"cm_Lookup adding mount for non-existent directory: %S",
1168 osi_LogSaveClientString(afsd_logp,cnamep));
1171 * There is an ugly behavior where a share name "foo" will be searched
1172 * for as "fo". If the searched for name differs by an already existing
1173 * symlink or mount point in the Freelance directory, do not add the
1174 * new value automatically.
1178 fnlen = strlen(fnamep);
1179 if ( fnamep[fnlen-1] == '.') {
1180 fnamep[fnlen-1] = '\0';
1185 if (cnamep[0] == '.') {
1186 if (cm_GetCell_Gen(&fnamep[1], &fullname[1], CM_FLAG_CREATE)) {
1188 code = cm_FreelanceAddMount(fullname, &fullname[1], "root.cell", 1, &rock.fid);
1189 if ( cm_FsStrCmpI(&fnamep[1], &fullname[1])) {
1191 * Do not permit symlinks that are one of:
1192 * . the cellname followed by a dot
1193 * . the cellname minus a single character
1194 * . a substring of the cellname that does not consist of full components
1196 if ( cm_strnicmp_utf8(&fnamep[1], fullname, (int)fnlen-1) == 0 &&
1197 (fnlen-1 == strlen(fullname)-1 || fullname[fnlen-1] != '.'))
1199 /* do not add; substitute fullname for the search */
1201 fnamep = malloc(strlen(fullname)+2);
1203 strncpy(&fnamep[1], fullname, strlen(fullname)+1);
1206 code = cm_FreelanceAddSymlink(fnamep, fullname, &rock.fid);
1211 if (cm_GetCell_Gen(fnamep, fullname, CM_FLAG_CREATE)) {
1213 code = cm_FreelanceAddMount(fullname, fullname, "root.cell", 0, &rock.fid);
1214 if ( cm_FsStrCmpI(fnamep, fullname)) {
1216 * Do not permit symlinks that are one of:
1217 * . the cellname followed by a dot
1218 * . the cellname minus a single character
1219 * . a substring of the cellname that does not consist of full components
1221 if ( cm_strnicmp_utf8(fnamep, fullname, (int)fnlen-1) == 0 &&
1222 (fnlen == strlen(fullname)-1 || fullname[fnlen] != '.'))
1224 /* do not add; substitute fullname for the search */
1226 fnamep = strdup(fullname);
1230 code = cm_FreelanceAddSymlink(fnamep, fullname, &rock.fid);
1239 nnamep = cm_FsStringToNormStringAlloc(fnamep, -1, NULL);
1243 if (!found || code) { /* add mount point failed, so give up */
1244 if (flags & CM_FLAG_CHECKPATH)
1245 code = CM_ERROR_NOSUCHPATH;
1247 code = CM_ERROR_NOSUCHFILE;
1250 tscp = NULL; /* to force call of cm_GetSCache */
1252 if (flags & CM_FLAG_CHECKPATH)
1253 code = CM_ERROR_NOSUCHPATH;
1255 code = CM_ERROR_NOSUCHFILE;
1261 if ( !tscp ) /* we did not find it in the dnlc */
1264 code = cm_GetSCache(&rock.fid, &tscp, userp, reqp);
1268 /* tscp is now held */
1270 lock_ObtainWrite(&tscp->rw);
1273 * Do not get status if we do not already have a callback.
1274 * The process of reading the mount point string will obtain status information
1275 * in a single RPC. No reason to add a second round trip.
1277 * If we do have a callback, use cm_SyncOp to get status in case the
1278 * current cm_user_t is not the same as the one that obtained the
1279 * mount point string contents.
1281 if (cm_HaveCallback(tscp)) {
1282 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
1283 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1285 lock_ReleaseWrite(&tscp->rw);
1286 cm_ReleaseSCache(tscp);
1289 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1291 /* tscp is now locked */
1293 if (!(flags & CM_FLAG_NOMOUNTCHASE)
1294 && tscp->fileType == CM_SCACHETYPE_MOUNTPOINT) {
1295 /* mount points are funny: they have a volume name to mount
1298 code = cm_ReadMountPoint(tscp, userp, reqp);
1300 code = cm_FollowMountPoint(tscp, dscp, userp, reqp,
1302 lock_ReleaseWrite(&tscp->rw);
1303 cm_ReleaseSCache(tscp);
1310 lock_ReleaseWrite(&tscp->rw);
1313 /* copy back pointer */
1316 /* insert scache in dnlc */
1317 if ( !dnlcHit && !(flags & CM_FLAG_NOMOUNTCHASE) && rock.ExactFound ) {
1318 /* lock the directory entry to prevent racing callback revokes */
1319 lock_ObtainRead(&dscp->rw);
1320 if ( dscp->cbServerp != NULL && dscp->cbExpires > 0 ) {
1321 /* TODO: reuse nnamep from above */
1324 nnamep = cm_ClientStringToNormStringAlloc(cnamep, -1, NULL);
1326 cm_dnlcEnter(dscp, nnamep, tscp);
1328 lock_ReleaseRead(&dscp->rw);
1345 int cm_ExpandSysName(clientchar_t *inp, clientchar_t *outp, long outSizeCch, unsigned int index)
1350 tp = cm_ClientStrRChr(inp, '@');
1352 return 0; /* no @sys */
1354 if (cm_ClientStrCmp(tp, _C("@sys")) != 0)
1355 return 0; /* no @sys */
1357 /* caller just wants to know if this is a valid @sys type of name */
1361 if (index >= cm_sysNameCount)
1364 /* otherwise generate the properly expanded @sys name */
1365 prefixCount = (int)(tp - inp);
1367 cm_ClientStrCpyN(outp, outSizeCch, inp, prefixCount); /* copy out "a." from "a.@sys" */
1368 outp[prefixCount] = 0; /* null terminate the "a." */
1369 cm_ClientStrCat(outp, outSizeCch, cm_sysNameList[index]);/* append i386_nt40 */
1373 long cm_EvaluateVolumeReference(clientchar_t * namep, long flags, cm_user_t * userp,
1374 cm_req_t *reqp, cm_scache_t ** outScpp)
1376 afs_uint32 code = 0;
1377 fschar_t cellName[CELL_MAXNAMELEN];
1378 fschar_t volumeName[VL_MAXNAMELEN];
1382 fschar_t * fnamep = NULL;
1384 cm_cell_t * cellp = NULL;
1385 cm_volume_t * volp = NULL;
1389 int mountType = RWVOL;
1391 osi_Log1(afsd_logp, "cm_EvaluateVolumeReference for string [%S]",
1392 osi_LogSaveClientString(afsd_logp, namep));
1394 if (cm_ClientStrCmpNI(namep, _C(CM_PREFIX_VOL), CM_PREFIX_VOL_CCH) != 0) {
1395 goto _exit_invalid_path;
1398 /* namep is assumed to look like the following:
1400 @vol:<cellname>%<volume>\0
1402 @vol:<cellname>#<volume>\0
1406 fnamep = cm_ClientStringToFsStringAlloc(namep, -1, NULL);
1407 cp = fnamep + CM_PREFIX_VOL_CCH; /* cp points to cell name, hopefully */
1408 tp = cm_FsStrChr(cp, '%');
1410 tp = cm_FsStrChr(cp, '#');
1412 (len = tp - cp) == 0 ||
1413 len > CELL_MAXNAMELEN)
1414 goto _exit_invalid_path;
1415 cm_FsStrCpyN(cellName, lengthof(cellName), cp, len);
1420 cp = tp+1; /* cp now points to volume, supposedly */
1421 cm_FsStrCpy(volumeName, lengthof(volumeName), cp);
1423 /* OK, now we have the cell and the volume */
1424 osi_Log2(afsd_logp, " Found cell [%s] and volume [%s]",
1425 osi_LogSaveFsString(afsd_logp, cellName),
1426 osi_LogSaveFsString(afsd_logp, volumeName));
1428 cellp = cm_GetCell(cellName, CM_FLAG_CREATE);
1429 if (cellp == NULL) {
1430 goto _exit_invalid_path;
1433 len = cm_FsStrLen(volumeName);
1434 if (len >= 8 && cm_FsStrCmp(volumeName + len - 7, ".backup") == 0)
1436 else if (len >= 10 &&
1437 cm_FsStrCmp(volumeName + len - 9, ".readonly") == 0)
1442 if (cm_VolNameIsID(volumeName)) {
1443 code = cm_FindVolumeByID(cellp, atoi(volumeName), userp, reqp,
1444 CM_GETVOL_FLAG_CREATE, &volp);
1446 code = cm_FindVolumeByName(cellp, volumeName, userp, reqp,
1447 CM_GETVOL_FLAG_CREATE, &volp);
1453 if (volType == BACKVOL)
1454 volume = volp->vol[BACKVOL].ID;
1455 else if (volType == ROVOL ||
1456 (volType == RWVOL && mountType == ROVOL && volp->vol[ROVOL].ID != 0))
1457 volume = volp->vol[ROVOL].ID;
1459 volume = volp->vol[RWVOL].ID;
1461 cm_SetFid(&fid, cellp->cellID, volume, 1, 1);
1463 code = cm_GetSCache(&fid, outScpp, userp, reqp);
1476 if (flags & CM_FLAG_CHECKPATH)
1477 return CM_ERROR_NOSUCHPATH;
1479 return CM_ERROR_NOSUCHFILE;
1482 #ifdef DEBUG_REFCOUNT
1483 long cm_LookupDbg(cm_scache_t *dscp, clientchar_t *namep, long flags, cm_user_t *userp,
1484 cm_req_t *reqp, cm_scache_t **outScpp, char * file, long line)
1486 long cm_Lookup(cm_scache_t *dscp, clientchar_t *namep, long flags, cm_user_t *userp,
1487 cm_req_t *reqp, cm_scache_t **outScpp)
1491 clientchar_t tname[AFSPATHMAX];
1492 int sysNameIndex = 0;
1493 cm_scache_t *scp = NULL;
1495 #ifdef DEBUG_REFCOUNT
1496 afsi_log("%s:%d cm_Lookup dscp 0x%p ref %d", file, line, dscp, dscp->refCount, file, line);
1497 osi_Log2(afsd_logp, "cm_Lookup dscp 0x%p ref %d", dscp, dscp->refCount);
1500 if ( cm_ClientStrCmpI(namep,_C(SMB_IOCTL_FILENAME_NOSLASH)) == 0 ) {
1501 if (flags & CM_FLAG_CHECKPATH)
1502 return CM_ERROR_NOSUCHPATH;
1504 return CM_ERROR_NOSUCHFILE;
1507 if (dscp == cm_data.rootSCachep &&
1508 cm_ClientStrCmpNI(namep, _C(CM_PREFIX_VOL), CM_PREFIX_VOL_CCH) == 0) {
1509 return cm_EvaluateVolumeReference(namep, flags, userp, reqp, outScpp);
1512 if (cm_ExpandSysName(namep, NULL, 0, 0) > 0) {
1513 for ( sysNameIndex = 0; sysNameIndex < MAXNUMSYSNAMES; sysNameIndex++) {
1514 code = cm_ExpandSysName(namep, tname, lengthof(tname), sysNameIndex);
1516 code = cm_LookupInternal(dscp, tname, flags, userp, reqp, &scp);
1517 #ifdef DEBUG_REFCOUNT
1518 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);
1519 osi_Log3(afsd_logp, "cm_LookupInternal (1) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1527 cm_ReleaseSCache(scp);
1531 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1532 #ifdef DEBUG_REFCOUNT
1533 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);
1534 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1541 code = cm_LookupInternal(dscp, namep, flags, userp, reqp, &scp);
1542 #ifdef DEBUG_REFCOUNT
1543 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);
1544 osi_Log3(afsd_logp, "cm_LookupInternal (2) code 0x%x dscp 0x%p scp 0x%p", code, dscp, scp);
1550 /* None of the possible sysName expansions could be found */
1551 if (flags & CM_FLAG_CHECKPATH)
1552 return CM_ERROR_NOSUCHPATH;
1554 return CM_ERROR_NOSUCHFILE;
1557 /*! \brief Unlink a file name
1559 Encapsulates a call to RXAFS_RemoveFile().
1561 \param[in] dscp cm_scache_t pointing at the directory containing the
1562 name to be unlinked.
1564 \param[in] fnamep Original name to be unlinked. This is the
1565 name that will be passed into the RXAFS_RemoveFile() call.
1566 This parameter is optional. If not provided, the value will
1569 \param[in] came Client name to be unlinked. This name will be used
1570 to update the local directory caches.
1572 \param[in] userp cm_user_t for the request.
1574 \param[in] reqp Request tracker.
1577 long cm_Unlink(cm_scache_t *dscp, fschar_t *fnamep, clientchar_t * cnamep,
1578 cm_user_t *userp, cm_req_t *reqp)
1584 AFSFetchStatus newDirStatus;
1586 struct rx_connection * rxconnp;
1588 cm_scache_t *scp = NULL;
1589 int free_fnamep = FALSE;
1591 memset(&volSync, 0, sizeof(volSync));
1593 if (fnamep == NULL) {
1596 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ,
1597 CM_DIROP_FLAG_NONE, &dirop);
1599 code = cm_BPlusDirLookupOriginalName(&dirop, cnamep, &fnamep);
1602 cm_EndDirOp(&dirop);
1609 #ifdef AFS_FREELANCE_CLIENT
1610 if (cm_freelanceEnabled && dscp == cm_data.rootSCachep) {
1611 /* deleting a mount point from the root dir. */
1612 code = cm_FreelanceRemoveMount(fnamep);
1617 code = cm_Lookup(dscp, cnamep, CM_FLAG_NOMOUNTCHASE, userp, reqp, &scp);
1621 /* Check for RO volume */
1622 if (dscp->flags & CM_SCACHEFLAG_RO) {
1623 code = CM_ERROR_READONLY;
1627 /* make sure we don't screw up the dir status during the merge */
1628 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE,
1629 CM_DIROP_FLAG_NONE, &dirop);
1631 lock_ObtainWrite(&dscp->rw);
1632 sflags = CM_SCACHESYNC_STOREDATA;
1633 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, sflags);
1634 lock_ReleaseWrite(&dscp->rw);
1636 cm_EndDirOp(&dirop);
1641 afsFid.Volume = dscp->fid.volume;
1642 afsFid.Vnode = dscp->fid.vnode;
1643 afsFid.Unique = dscp->fid.unique;
1645 osi_Log1(afsd_logp, "CALL RemoveFile scp 0x%p", dscp);
1647 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
1651 rxconnp = cm_GetRxConn(connp);
1652 code = RXAFS_RemoveFile(rxconnp, &afsFid, fnamep,
1653 &newDirStatus, &volSync);
1654 rx_PutConnection(rxconnp);
1656 } while (cm_Analyze(connp, userp, reqp, &dscp->fid, &volSync, NULL, NULL, code));
1657 code = cm_MapRPCError(code, reqp);
1660 osi_Log1(afsd_logp, "CALL RemoveFile FAILURE, code 0x%x", code);
1662 osi_Log0(afsd_logp, "CALL RemoveFile SUCCESS");
1665 lock_ObtainWrite(&dirop.scp->dirlock);
1666 dirop.lockType = CM_DIRLOCK_WRITE;
1668 lock_ObtainWrite(&dscp->rw);
1669 cm_dnlcRemove(dscp, cnamep);
1671 cm_MergeStatus(NULL, dscp, &newDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
1672 } else if (code == CM_ERROR_NOSUCHFILE) {
1673 /* windows would not have allowed the request to delete the file
1674 * if it did not believe the file existed. therefore, we must
1675 * have an inconsistent view of the world.
1677 dscp->cbServerp = NULL;
1679 cm_SyncOpDone(dscp, NULL, sflags);
1680 lock_ReleaseWrite(&dscp->rw);
1682 if (code == 0 && cm_CheckDirOpForSingleChange(&dirop) && cnamep) {
1683 cm_DirDeleteEntry(&dirop, fnamep);
1685 cm_BPlusDirDeleteEntry(&dirop, cnamep);
1688 cm_EndDirOp(&dirop);
1691 cm_ReleaseSCache(scp);
1693 lock_ObtainWrite(&scp->rw);
1694 if (--scp->linkCount == 0)
1695 scp->flags |= CM_SCACHEFLAG_DELETED;
1696 cm_DiscardSCache(scp);
1697 lock_ReleaseWrite(&scp->rw);
1708 /* called with a write locked vnode, and fills in the link info.
1709 * returns this the vnode still write locked.
1711 long cm_HandleLink(cm_scache_t *linkScp, cm_user_t *userp, cm_req_t *reqp)
1715 lock_AssertWrite(&linkScp->rw);
1716 if (!linkScp->mountPointStringp[0]) {
1718 #ifdef AFS_FREELANCE_CLIENT
1719 /* File servers do not have data for freelance entries */
1720 if (cm_freelanceEnabled &&
1721 linkScp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
1722 linkScp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
1724 code = cm_FreelanceFetchMountPointString(linkScp);
1726 #endif /* AFS_FREELANCE_CLIENT */
1728 char temp[MOUNTPOINTLEN];
1731 /* read the link data from the file server */
1732 offset.LowPart = offset.HighPart = 0;
1733 code = cm_GetData(linkScp, &offset, temp, MOUNTPOINTLEN, userp, reqp);
1738 * linkScp->length is the actual length of the symlink target string.
1739 * It is current because cm_GetData merged the most up to date
1740 * status info into scp and has not dropped the rwlock since.
1742 if (linkScp->length.LowPart > MOUNTPOINTLEN - 1)
1743 return CM_ERROR_TOOBIG;
1744 if (linkScp->length.LowPart == 0)
1745 return CM_ERROR_INVAL;
1747 /* make sure we are NUL terminated */
1748 temp[linkScp->length.LowPart] = 0;
1749 memcpy(linkScp->mountPointStringp, temp, linkScp->length.LowPart + 1);
1752 if ( !strnicmp(linkScp->mountPointStringp, "msdfs:", strlen("msdfs:")) )
1753 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1755 } /* don't have symlink contents cached */
1760 /* called with a held vnode and a path suffix, with the held vnode being a
1761 * symbolic link. Our goal is to generate a new path to interpret, and return
1762 * this new path in newSpaceBufferp. If the new vnode is relative to a dir
1763 * other than the directory containing the symbolic link, then the new root is
1764 * returned in *newRootScpp, otherwise a null is returned there.
1766 long cm_AssembleLink(cm_scache_t *linkScp, fschar_t *pathSuffixp,
1767 cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1768 cm_user_t *userp, cm_req_t *reqp)
1775 *newRootScpp = NULL;
1776 *newSpaceBufferp = NULL;
1778 lock_ObtainWrite(&linkScp->rw);
1780 * Do not get status if we do not already have a callback.
1781 * The process of reading the symlink string will obtain status information
1782 * in a single RPC. No reason to add a second round trip.
1784 * If we do have a callback, use cm_SyncOp to get status in case the
1785 * current cm_user_t is not the same as the one that obtained the
1786 * symlink string contents.
1788 if (cm_HaveCallback(linkScp)) {
1789 code = cm_SyncOp(linkScp, NULL, userp, reqp, 0,
1790 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1792 lock_ReleaseWrite(&linkScp->rw);
1793 cm_ReleaseSCache(linkScp);
1796 cm_SyncOpDone(linkScp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1798 code = cm_HandleLink(linkScp, userp, reqp);
1802 /* if we may overflow the buffer, bail out; buffer is signficantly
1803 * bigger than max path length, so we don't really have to worry about
1804 * being a little conservative here.
1806 if (cm_FsStrLen(linkScp->mountPointStringp) + cm_FsStrLen(pathSuffixp) + 2
1807 >= CM_UTILS_SPACESIZE) {
1808 code = CM_ERROR_TOOBIG;
1812 tsp = cm_GetSpace();
1813 linkp = linkScp->mountPointStringp;
1814 if (strncmp(linkp, cm_mountRoot, cm_mountRootLen) == 0) {
1815 if (strlen(linkp) > cm_mountRootLen)
1816 StringCbCopyA((char *) tsp->data, sizeof(tsp->data), linkp+cm_mountRootLen+1);
1819 *newRootScpp = cm_RootSCachep(userp, reqp);
1820 cm_HoldSCache(*newRootScpp);
1821 } else if (linkp[0] == '\\' && linkp[1] == '\\') {
1822 if (!strnicmp(&linkp[2], cm_NetbiosName, (len = (long)strlen(cm_NetbiosName))))
1824 char * p = &linkp[len + 3];
1825 if (strnicmp(p, "all", 3) == 0)
1828 StringCbCopyA(tsp->data, sizeof(tsp->data), p);
1829 for (p = tsp->data; *p; p++) {
1833 *newRootScpp = cm_RootSCachep(userp, reqp);
1834 cm_HoldSCache(*newRootScpp);
1836 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1837 StringCchCopyA(tsp->data,lengthof(tsp->data), linkp);
1838 code = CM_ERROR_PATH_NOT_COVERED;
1840 } else if ( linkScp->fileType == CM_SCACHETYPE_DFSLINK ||
1841 !strnicmp(linkp, "msdfs:", (len = (long)strlen("msdfs:"))) ) {
1842 linkScp->fileType = CM_SCACHETYPE_DFSLINK;
1843 StringCchCopyA(tsp->data,lengthof(tsp->data), linkp);
1844 code = CM_ERROR_PATH_NOT_COVERED;
1845 } else if (*linkp == '\\' || *linkp == '/') {
1847 /* formerly, this was considered to be from the AFS root,
1848 * but this seems to create problems. instead, we will just
1849 * reject the link */
1850 StringCchCopyA(tsp->data,lengthof(tsp->data), linkp+1);
1851 *newRootScpp = cm_RootSCachep(userp, reqp);
1852 cm_HoldSCache(*newRootScpp);
1854 /* we still copy the link data into the response so that
1855 * the user can see what the link points to
1857 linkScp->fileType = CM_SCACHETYPE_INVALID;
1858 StringCchCopyA(tsp->data,lengthof(tsp->data), linkp);
1859 code = CM_ERROR_NOSUCHPATH;
1862 /* a relative link */
1863 StringCchCopyA(tsp->data,lengthof(tsp->data), linkp);
1865 if (pathSuffixp[0] != 0) { /* if suffix string is non-null */
1866 StringCchCatA(tsp->data,lengthof(tsp->data), "\\");
1867 StringCchCatA(tsp->data,lengthof(tsp->data), pathSuffixp);
1871 clientchar_t * cpath = cm_FsStringToClientStringAlloc(tsp->data, -1, NULL);
1872 if (cpath != NULL) {
1873 cm_ClientStrCpy(tsp->wdata, lengthof(tsp->wdata), cpath);
1875 *newSpaceBufferp = tsp;
1877 code = CM_ERROR_NOSUCHPATH;
1884 if (code == CM_ERROR_PATH_NOT_COVERED && reqp->tidPathp && reqp->relPathp) {
1885 cm_VolStatus_Notify_DFS_Mapping(linkScp, reqp->tidPathp, reqp->relPathp);
1890 lock_ReleaseWrite(&linkScp->rw);
1893 #ifdef DEBUG_REFCOUNT
1894 long cm_NameIDbg(cm_scache_t *rootSCachep, clientchar_t *pathp, long flags,
1895 cm_user_t *userp, clientchar_t *tidPathp, cm_req_t *reqp,
1896 cm_scache_t **outScpp,
1897 char * file, long line)
1899 long cm_NameI(cm_scache_t *rootSCachep, clientchar_t *pathp, long flags,
1900 cm_user_t *userp, clientchar_t *tidPathp,
1901 cm_req_t *reqp, cm_scache_t **outScpp)
1905 clientchar_t *tp; /* ptr moving through input buffer */
1906 clientchar_t tc; /* temp char */
1907 int haveComponent; /* has new component started? */
1908 clientchar_t component[AFSPATHMAX]; /* this is the new component */
1909 clientchar_t *cp; /* component name being assembled */
1910 cm_scache_t *tscp; /* current location in the hierarchy */
1911 cm_scache_t *nscp; /* next dude down */
1912 cm_scache_t *dirScp; /* last dir we searched */
1913 cm_scache_t *linkScp; /* new root for the symlink we just
1915 cm_space_t *psp; /* space for current path, if we've hit
1917 cm_space_t *tempsp; /* temp vbl */
1918 clientchar_t *restp; /* rest of the pathname to interpret */
1919 int symlinkCount; /* count of # of symlinks traversed */
1920 int extraFlag; /* avoid chasing mt pts for dir cmd */
1921 int phase = 1; /* 1 = tidPathp, 2 = pathp */
1922 #define MAX_FID_COUNT 512
1923 cm_fid_t fids[MAX_FID_COUNT]; /* array of fids processed in this path walk */
1924 int fid_count = 0; /* number of fids processed in this path walk */
1929 #ifdef DEBUG_REFCOUNT
1930 afsi_log("%s:%d cm_NameI rootscp 0x%p ref %d", file, line, rootSCachep, rootSCachep->refCount);
1931 osi_Log4(afsd_logp,"cm_NameI rootscp 0x%p path %S tidpath %S flags 0x%x",
1932 rootSCachep, pathp ? pathp : L"<NULL>", tidPathp ? tidPathp : L"<NULL>",
1947 cm_HoldSCache(tscp);
1955 /* map Unix slashes into DOS ones so we can interpret Unix
1961 if (!haveComponent) {
1964 } else if (tc == 0) {
1978 /* we have a component here */
1979 if (tc == 0 || tc == '\\') {
1980 /* end of the component; we're at the last
1981 * component if tc == 0. However, if the last
1982 * is a symlink, we have more to do.
1984 *cp++ = 0; /* add null termination */
1986 if ((flags & CM_FLAG_DIRSEARCH) && tc == 0)
1987 extraFlag = CM_FLAG_NOMOUNTCHASE;
1988 code = cm_Lookup(tscp, component,
1990 userp, reqp, &nscp);
1993 if (!cm_ClientStrCmp(component,_C("..")) ||
1994 !cm_ClientStrCmp(component,_C("."))) {
1996 * roll back the fid list until we find the
1997 * fid that matches where we are now. Its not
1998 * necessarily one or two fids because they
1999 * might have been symlinks or mount points or
2000 * both that were crossed.
2002 for ( i=fid_count-1; i>=0; i--) {
2003 if (!cm_FidCmp(&nscp->fid, &fids[i]))
2008 /* add the new fid to the list */
2009 if (fid_count == MAX_FID_COUNT) {
2010 code = CM_ERROR_TOO_MANY_SYMLINKS;
2011 cm_ReleaseSCache(nscp);
2015 fids[fid_count++] = nscp->fid;
2020 cm_ReleaseSCache(tscp);
2022 cm_ReleaseSCache(dirScp);
2025 if ((code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) &&
2026 tscp->fileType == CM_SCACHETYPE_SYMLINK) {
2027 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_NOSUCHPATH");
2028 return CM_ERROR_NOSUCHPATH;
2030 osi_Log1(afsd_logp,"cm_NameI code 0x%x", code);
2035 haveComponent = 0; /* component done */
2037 cm_ReleaseSCache(dirScp);
2038 dirScp = tscp; /* for some symlinks */
2039 tscp = nscp; /* already held */
2041 if (tc == 0 && !(flags & CM_FLAG_FOLLOW) && phase == 2) {
2044 cm_ReleaseSCache(dirScp);
2050 /* now, if tscp is a symlink, we should follow it and
2051 * assemble the path again.
2053 lock_ObtainWrite(&tscp->rw);
2054 code = cm_SyncOp(tscp, NULL, userp, reqp, 0,
2055 CM_SCACHESYNC_GETSTATUS
2056 | CM_SCACHESYNC_NEEDCALLBACK);
2058 lock_ReleaseWrite(&tscp->rw);
2059 cm_ReleaseSCache(tscp);
2062 cm_ReleaseSCache(dirScp);
2067 cm_SyncOpDone(tscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2069 if (tscp->fileType == CM_SCACHETYPE_SYMLINK) {
2070 /* this is a symlink; assemble a new buffer */
2071 lock_ReleaseWrite(&tscp->rw);
2072 if (symlinkCount++ >= MAX_SYMLINK_COUNT) {
2073 cm_ReleaseSCache(tscp);
2076 cm_ReleaseSCache(dirScp);
2081 osi_Log0(afsd_logp,"cm_NameI code CM_ERROR_TOO_MANY_SYMLINKS");
2082 return CM_ERROR_TOO_MANY_SYMLINKS;
2092 /* TODO: make this better */
2093 frestp = cm_ClientStringToFsStringAlloc(restp, -1, NULL);
2094 code = cm_AssembleLink(tscp, frestp, &linkScp, &tempsp, userp, reqp);
2098 if (code == 0 && linkScp != NULL) {
2099 if (linkScp == cm_data.rootSCachep) {
2103 for ( i=0; i<fid_count; i++) {
2104 if ( !cm_FidCmp(&linkScp->fid, &fids[i]) ) {
2105 code = CM_ERROR_TOO_MANY_SYMLINKS;
2106 cm_ReleaseSCache(linkScp);
2112 if (i == fid_count && fid_count < MAX_FID_COUNT) {
2113 fids[fid_count++] = linkScp->fid;
2118 /* something went wrong */
2119 cm_ReleaseSCache(tscp);
2122 cm_ReleaseSCache(dirScp);
2128 /* otherwise, tempsp has the new path,
2129 * and linkScp is the new root from
2130 * which to interpret that path.
2131 * Continue with the namei processing,
2132 * also doing the bookkeeping for the
2133 * space allocation and tracking the
2134 * vnode reference counts.
2140 cm_ReleaseSCache(tscp);
2145 * now, if linkScp is null, that's
2146 * AssembleLink's way of telling us that
2147 * the sym link is relative to the dir
2148 * containing the link. We have a ref
2149 * to it in dirScp, and we hold it now
2150 * and reuse it as the new spot in the
2158 /* not a symlink, we may be done */
2159 lock_ReleaseWrite(&tscp->rw);
2167 cm_ReleaseSCache(dirScp);
2175 cm_ReleaseSCache(dirScp);
2178 } /* end of a component */
2181 } /* we have a component */
2182 } /* big while loop over all components */
2186 cm_ReleaseSCache(dirScp);
2192 cm_ReleaseSCache(tscp);
2194 #ifdef DEBUG_REFCOUNT
2195 afsi_log("%s:%d cm_NameI code 0x%x outScpp 0x%p ref %d", file, line, code, *outScpp, (*outScpp) ? (*outScpp)->refCount : 0);
2197 osi_Log2(afsd_logp,"cm_NameI code 0x%x outScpp 0x%p", code, *outScpp);
2201 /* called with a dir, and a vnode within the dir that happens to be a symlink.
2202 * We chase the link, and return a held pointer to the target, if it exists,
2203 * in *outScpp. If we succeed, we return 0, otherwise we return an error code
2204 * and do not hold or return a target vnode.
2206 * This is very similar to calling cm_NameI with the last component of a name,
2207 * which happens to be a symlink, except that we've already passed by the name.
2209 * This function is typically called by the directory listing functions, which
2210 * encounter symlinks but need to return the proper file length so programs
2211 * like "more" work properly when they make use of the attributes retrieved from
2214 * The input vnode should not be locked when this function is called.
2216 long cm_EvaluateSymLink(cm_scache_t *dscp, cm_scache_t *linkScp,
2217 cm_scache_t **outScpp, cm_user_t *userp, cm_req_t *reqp)
2221 cm_scache_t *newRootScp;
2225 osi_Log1(afsd_logp, "Evaluating symlink scp 0x%p", linkScp);
2227 code = cm_AssembleLink(linkScp, "", &newRootScp, &spacep, userp, reqp);
2231 /* now, if newRootScp is NULL, we're really being told that the symlink
2232 * is relative to the current directory (dscp).
2234 if (newRootScp == NULL) {
2236 cm_HoldSCache(dscp);
2239 code = cm_NameI(newRootScp, spacep->wdata,
2240 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW | CM_FLAG_DIRSEARCH,
2241 userp, NULL, reqp, outScpp);
2243 if (code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH)
2244 code = CM_ERROR_NOSUCHPATH;
2246 /* this stuff is allocated no matter what happened on the namei call,
2248 cm_FreeSpace(spacep);
2249 cm_ReleaseSCache(newRootScp);
2251 if (linkScp == *outScpp) {
2252 cm_ReleaseSCache(*outScpp);
2254 code = CM_ERROR_NOSUCHPATH;
2260 /* for a given entry, make sure that it isn't in the stat cache, and then
2261 * add it to the list of file IDs to be obtained.
2263 * Don't bother adding it if we already have a vnode. Note that the dir
2264 * is locked, so we have to be careful checking the vnode we're thinking of
2265 * processing, to avoid deadlocks.
2267 long cm_TryBulkProc(cm_scache_t *scp, cm_dirEntry_t *dep, void *rockp,
2278 /* Don't overflow bsp. */
2279 if (bsp->counter >= CM_BULKMAX)
2280 return CM_ERROR_STOPNOW;
2282 thyper.LowPart = cm_data.buf_blockSize;
2283 thyper.HighPart = 0;
2284 thyper = LargeIntegerAdd(thyper, bsp->bufOffset);
2286 /* thyper is now the first byte past the end of the record we're
2287 * interested in, and bsp->bufOffset is the first byte of the record
2288 * we're interested in.
2289 * Skip data in the others.
2292 if (LargeIntegerLessThan(*offp, bsp->bufOffset))
2294 if (LargeIntegerGreaterThanOrEqualTo(*offp, thyper))
2295 return CM_ERROR_STOPNOW;
2296 if (strcmp(dep->name, ".") == 0 || strcmp(dep->name, "..") == 0)
2299 cm_SetFid(&tfid, scp->fid.cell, scp->fid.volume, ntohl(dep->fid.vnode), ntohl(dep->fid.unique));
2300 tscp = cm_FindSCache(&tfid);
2302 if (lock_TryWrite(&tscp->rw)) {
2303 /* we have an entry that we can look at */
2304 if (!(tscp->flags & CM_SCACHEFLAG_EACCESS) && cm_HaveCallback(tscp)) {
2305 /* we have a callback on it. Don't bother
2306 * fetching this stat entry, since we're happy
2307 * with the info we have.
2309 lock_ReleaseWrite(&tscp->rw);
2310 cm_ReleaseSCache(tscp);
2313 lock_ReleaseWrite(&tscp->rw);
2315 cm_ReleaseSCache(tscp);
2318 #ifdef AFS_FREELANCE_CLIENT
2319 // yj: if this is a mountpoint under root.afs then we don't want it
2320 // to be bulkstat-ed, instead, we call getSCache directly and under
2321 // getSCache, it is handled specially.
2322 if ( cm_freelanceEnabled &&
2323 tfid.cell==AFS_FAKE_ROOT_CELL_ID &&
2324 tfid.volume==AFS_FAKE_ROOT_VOL_ID &&
2325 !(tfid.vnode==0x1 && tfid.unique==0x1) )
2327 osi_Log0(afsd_logp, "cm_TryBulkProc Freelance calls cm_SCache on root.afs mountpoint");
2328 return cm_GetSCache(&tfid, &tscp, NULL, NULL);
2330 #endif /* AFS_FREELANCE_CLIENT */
2333 bsp->fids[i].Volume = scp->fid.volume;
2334 bsp->fids[i].Vnode = tfid.vnode;
2335 bsp->fids[i].Unique = tfid.unique;
2340 cm_TryBulkStatRPC(cm_scache_t *dscp, cm_bulkStat_t *bbp, cm_user_t *userp, cm_req_t *reqp)
2343 AFSCBFids fidStruct;
2344 AFSBulkStats statStruct;
2346 AFSCBs callbackStruct;
2349 cm_callbackRequest_t cbReq;
2355 struct rx_connection * rxconnp;
2356 int inlinebulk; /* Did we use InlineBulkStatus RPC or not? */
2358 memset(&volSync, 0, sizeof(volSync));
2360 /* otherwise, we may have one or more bulk stat's worth of stuff in bb;
2361 * make the calls to create the entries. Handle AFSCBMAX files at a
2364 for (filex = 0; filex < bbp->counter; filex += filesThisCall) {
2365 filesThisCall = bbp->counter - filex;
2366 if (filesThisCall > AFSCBMAX)
2367 filesThisCall = AFSCBMAX;
2369 fidStruct.AFSCBFids_len = filesThisCall;
2370 fidStruct.AFSCBFids_val = &bbp->fids[filex];
2371 statStruct.AFSBulkStats_len = filesThisCall;
2372 statStruct.AFSBulkStats_val = &bbp->stats[filex];
2373 callbackStruct.AFSCBs_len = filesThisCall;
2374 callbackStruct.AFSCBs_val = &bbp->callbacks[filex];
2375 cm_StartCallbackGrantingCall(NULL, &cbReq);
2376 osi_Log1(afsd_logp, "CALL BulkStatus, %d entries", filesThisCall);
2379 * Whenever cm_Analyze is called for a RXAFS_ RPC there must
2380 * be a FID provided. However, the error code from RXAFS_BulkStatus
2381 * or RXAFS_InlinkBulkStatus does not apply to any FID. Therefore,
2382 * we generate an invalid FID to match with the RPC error.
2384 cm_SetFid(&tfid, dscp->fid.cell, dscp->fid.volume, 0, 0);
2389 code = cm_ConnFromFID(&tfid, userp, reqp, &connp);
2393 rxconnp = cm_GetRxConn(connp);
2394 if (!(connp->serverp->flags & CM_SERVERFLAG_NOINLINEBULK)) {
2395 code = RXAFS_InlineBulkStatus(rxconnp, &fidStruct,
2396 &statStruct, &callbackStruct, &volSync);
2397 if (code == RXGEN_OPCODE) {
2398 cm_SetServerNoInlineBulk(connp->serverp, 0);
2404 code = RXAFS_BulkStatus(rxconnp, &fidStruct,
2405 &statStruct, &callbackStruct, &volSync);
2407 rx_PutConnection(rxconnp);
2410 * If InlineBulk RPC was called and it succeeded,
2411 * then pull out the return code from the status info
2412 * and use it for cm_Analyze so that we can failover to other
2413 * .readonly volume instances. But only do it for errors that
2414 * are volume global.
2416 if (inlinebulk && code == 0 && (&bbp->stats[0])->errorCode) {
2417 osi_Log1(afsd_logp, "cm_TryBulkStat inline-bulk stat error: %d",
2418 (&bbp->stats[0])->errorCode);
2419 switch ((&bbp->stats[0])->errorCode) {
2428 code = (&bbp->stats[0])->errorCode;
2431 /* Rx and Rxkad errors are volume global */
2432 if ( (&bbp->stats[0])->errorCode >= -64 && (&bbp->stats[0])->errorCode < 0 ||
2433 (&bbp->stats[0])->errorCode >= ERROR_TABLE_BASE_RXK && (&bbp->stats[0])->errorCode < ERROR_TABLE_BASE_RXK + 256)
2434 code = (&bbp->stats[0])->errorCode;
2437 } while (cm_Analyze(connp, userp, reqp, &tfid, &volSync, NULL, &cbReq, code));
2438 code = cm_MapRPCError(code, reqp);
2441 * might as well quit on an error, since we're not going to do
2442 * much better on the next immediate call, either.
2445 osi_Log2(afsd_logp, "CALL %sBulkStatus FAILURE code 0x%x",
2446 inlinebulk ? "Inline" : "", code);
2447 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, NULL, 0);
2452 * The bulk RPC has succeeded or at least not failed with a
2453 * volume global error result. For items that have inlineBulk
2454 * errors we must call cm_Analyze in order to perform required
2455 * logging of errors.
2457 * If the RPC was not inline bulk or the entry either has no error
2458 * the status must be merged.
2460 osi_Log1(afsd_logp, "CALL %sBulkStatus SUCCESS", inlinebulk ? "Inline" : "");
2462 for (i = 0; i<filesThisCall; i++) {
2464 cm_SetFid(&tfid, dscp->fid.cell, bbp->fids[j].Volume, bbp->fids[j].Vnode, bbp->fids[j].Unique);
2466 if (inlinebulk && (&bbp->stats[j])->errorCode) {
2467 cm_req_t treq = *reqp;
2468 cm_Analyze(NULL, userp, &treq, &tfid, &volSync, NULL, &cbReq, (&bbp->stats[j])->errorCode);
2470 code = cm_GetSCache(&tfid, &scp, userp, reqp);
2475 * otherwise, if this entry has no callback info,
2476 * merge in this. If there is existing callback info
2477 * we skip the merge because the existing data must be
2478 * current (we have a callback) and the response from
2479 * a non-inline bulk rpc might actually be wrong.
2481 * now, we have to be extra paranoid on merging in this
2482 * information, since we didn't use cm_SyncOp before
2483 * starting the fetch to make sure that no bad races
2484 * were occurring. Specifically, we need to make sure
2485 * we don't obliterate any newer information in the
2486 * vnode than have here.
2488 * Right now, be pretty conservative: if there's a
2489 * callback or a pending call, skip it.
2490 * However, if the prior attempt to obtain status
2491 * was refused access or the volume is .readonly,
2492 * take the data in any case since we have nothing
2493 * better for the in flight directory enumeration that
2494 * resulted in this function being called.
2496 lock_ObtainRead(&scp->rw);
2497 if ((scp->cbServerp == NULL &&
2498 !(scp->flags & (CM_SCACHEFLAG_FETCHING | CM_SCACHEFLAG_STORING | CM_SCACHEFLAG_SIZESTORING))) ||
2499 (scp->flags & CM_SCACHEFLAG_PURERO) ||
2500 (scp->flags & CM_SCACHEFLAG_EACCESS))
2502 lock_ConvertRToW(&scp->rw);
2503 cm_EndCallbackGrantingCall(scp, &cbReq,
2506 CM_CALLBACK_MAINTAINCOUNT);
2507 cm_MergeStatus(dscp, scp, &bbp->stats[j], &volSync, userp, reqp, 0);
2508 lock_ReleaseWrite(&scp->rw);
2510 lock_ReleaseRead(&scp->rw);
2512 cm_ReleaseSCache(scp);
2514 } /* all files in the response */
2515 /* now tell it to drop the count,
2516 * after doing the vnode processing above */
2517 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, NULL, 0);
2518 } /* while there are still more files to process */
2523 /* called with a write locked scp and a pointer to a buffer. Make bulk stat
2524 * calls on all undeleted files in the page of the directory specified.
2527 cm_TryBulkStat(cm_scache_t *dscp, osi_hyper_t *offsetp, cm_user_t *userp,
2533 osi_Log1(afsd_logp, "cm_TryBulkStat dir 0x%p", dscp);
2535 /* should be on a buffer boundary */
2536 osi_assertx((offsetp->LowPart & (cm_data.buf_blockSize - 1)) == 0, "invalid offset");
2538 bbp = malloc(sizeof(cm_bulkStat_t));
2539 memset(bbp, 0, sizeof(cm_bulkStat_t));
2540 bbp->bufOffset = *offsetp;
2542 lock_ReleaseWrite(&dscp->rw);
2543 /* first, assemble the file IDs we need to stat */
2544 code = cm_ApplyDir(dscp, cm_TryBulkProc, (void *) bbp, offsetp, userp, reqp, NULL);
2546 /* if we failed, bail out early */
2547 if (code && code != CM_ERROR_STOPNOW) {
2549 lock_ObtainWrite(&dscp->rw);
2553 code = cm_TryBulkStatRPC(dscp, bbp, userp, reqp);
2554 osi_Log1(afsd_logp, "END cm_TryBulkStat code 0x%x", code);
2556 lock_ObtainWrite(&dscp->rw);
2561 void cm_StatusFromAttr(AFSStoreStatus *statusp, cm_scache_t *scp, cm_attr_t *attrp)
2565 /* initialize store back mask as inexpensive local variable */
2567 memset(statusp, 0, sizeof(AFSStoreStatus));
2569 /* copy out queued info from scache first, if scp passed in */
2571 if (scp->mask & CM_SCACHEMASK_CLIENTMODTIME) {
2572 statusp->ClientModTime = scp->clientModTime;
2573 mask |= AFS_SETMODTIME;
2574 scp->mask &= ~CM_SCACHEMASK_CLIENTMODTIME;
2579 /* now add in our locally generated request */
2580 if (attrp->mask & CM_ATTRMASK_CLIENTMODTIME) {
2581 statusp->ClientModTime = attrp->clientModTime;
2582 mask |= AFS_SETMODTIME;
2584 if (attrp->mask & CM_ATTRMASK_UNIXMODEBITS) {
2585 statusp->UnixModeBits = attrp->unixModeBits;
2586 mask |= AFS_SETMODE;
2588 if (attrp->mask & CM_ATTRMASK_OWNER) {
2589 statusp->Owner = attrp->owner;
2590 mask |= AFS_SETOWNER;
2592 if (attrp->mask & CM_ATTRMASK_GROUP) {
2593 statusp->Group = attrp->group;
2594 mask |= AFS_SETGROUP;
2597 statusp->Mask = mask;
2600 /* set the file size, and make sure that all relevant buffers have been
2601 * truncated. Ensure that any partially truncated buffers have been zeroed
2602 * to the end of the buffer.
2604 long cm_SetLength(cm_scache_t *scp, osi_hyper_t *sizep, cm_user_t *userp,
2610 /* start by locking out buffer creation */
2611 lock_ObtainWrite(&scp->bufCreateLock);
2613 /* verify that this is a file, not a dir or a symlink */
2614 lock_ObtainWrite(&scp->rw);
2615 code = cm_SyncOp(scp, NULL, userp, reqp, 0,
2616 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2619 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2621 if (scp->fileType != CM_SCACHETYPE_FILE) {
2622 code = CM_ERROR_ISDIR;
2627 if (LargeIntegerLessThan(*sizep, scp->length))
2632 lock_ReleaseWrite(&scp->rw);
2634 /* can't hold scp->rw lock here, since we may wait for a storeback to
2635 * finish if the buffer package is cleaning a buffer by storing it to
2639 buf_Truncate(scp, userp, reqp, sizep);
2641 /* now ensure that file length is short enough, and update truncPos */
2642 lock_ObtainWrite(&scp->rw);
2644 /* make sure we have a callback (so we have the right value for the
2645 * length), and wait for it to be safe to do a truncate.
2647 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_WRITE,
2648 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2649 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2651 /* If we only have 'i' bits, then we should still be able to set
2652 the size of a file we created. */
2653 if (code == CM_ERROR_NOACCESS && scp->creator == userp) {
2654 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_INSERT,
2655 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2656 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2662 if (LargeIntegerLessThan(*sizep, scp->length)) {
2663 /* a real truncation. If truncPos is not set yet, or is bigger
2664 * than where we're truncating the file, set truncPos to this
2669 if (!(scp->mask & CM_SCACHEMASK_TRUNCPOS)
2670 || LargeIntegerLessThan(*sizep, scp->length)) {
2672 scp->truncPos = *sizep;
2673 scp->mask |= CM_SCACHEMASK_TRUNCPOS;
2675 /* in either case, the new file size has been changed */
2676 scp->length = *sizep;
2677 scp->mask |= CM_SCACHEMASK_LENGTH;
2679 else if (LargeIntegerGreaterThan(*sizep, scp->length)) {
2680 /* really extending the file */
2681 scp->length = *sizep;
2682 scp->mask |= CM_SCACHEMASK_LENGTH;
2685 /* done successfully */
2688 cm_SyncOpDone(scp, NULL,
2689 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS
2690 | CM_SCACHESYNC_SETSTATUS | CM_SCACHESYNC_SETSIZE);
2693 lock_ReleaseWrite(&scp->rw);
2694 lock_ReleaseWrite(&scp->bufCreateLock);
2699 /* set the file size or other attributes (but not both at once) */
2700 long cm_SetAttr(cm_scache_t *scp, cm_attr_t *attrp, cm_user_t *userp,
2704 AFSFetchStatus afsOutStatus;
2708 AFSStoreStatus afsInStatus;
2709 struct rx_connection * rxconnp;
2711 memset(&volSync, 0, sizeof(volSync));
2713 /* handle file length setting */
2714 if (attrp->mask & CM_ATTRMASK_LENGTH)
2715 return cm_SetLength(scp, &attrp->length, userp, reqp);
2717 lock_ObtainWrite(&scp->rw);
2718 /* Check for RO volume */
2719 if (scp->flags & CM_SCACHEFLAG_RO) {
2720 code = CM_ERROR_READONLY;
2721 lock_ReleaseWrite(&scp->rw);
2725 /* otherwise, we have to make an RPC to get the status */
2726 code = cm_SyncOp(scp, NULL, userp, reqp, 0, CM_SCACHESYNC_STORESTATUS);
2728 lock_ReleaseWrite(&scp->rw);
2731 lock_ConvertWToR(&scp->rw);
2733 /* make the attr structure */
2734 cm_StatusFromAttr(&afsInStatus, scp, attrp);
2736 tfid.Volume = scp->fid.volume;
2737 tfid.Vnode = scp->fid.vnode;
2738 tfid.Unique = scp->fid.unique;
2739 lock_ReleaseRead(&scp->rw);
2741 /* now make the RPC */
2742 osi_Log1(afsd_logp, "CALL StoreStatus scp 0x%p", scp);
2744 code = cm_ConnFromFID(&scp->fid, userp, reqp, &connp);
2748 rxconnp = cm_GetRxConn(connp);
2749 code = RXAFS_StoreStatus(rxconnp, &tfid,
2750 &afsInStatus, &afsOutStatus, &volSync);
2751 rx_PutConnection(rxconnp);
2753 } while (cm_Analyze(connp, userp, reqp,
2754 &scp->fid, &volSync, NULL, NULL, code));
2755 code = cm_MapRPCError(code, reqp);
2758 osi_Log1(afsd_logp, "CALL StoreStatus FAILURE, code 0x%x", code);
2760 osi_Log0(afsd_logp, "CALL StoreStatus SUCCESS");
2762 lock_ObtainWrite(&scp->rw);
2764 cm_MergeStatus(NULL, scp, &afsOutStatus, &volSync, userp, reqp,
2765 CM_MERGEFLAG_FORCE|CM_MERGEFLAG_STOREDATA);
2766 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_STORESTATUS);
2768 /* if we're changing the mode bits, discard the ACL cache,
2769 * since we changed the mode bits.
2771 if (afsInStatus.Mask & AFS_SETMODE)
2772 cm_FreeAllACLEnts(scp);
2773 lock_ReleaseWrite(&scp->rw);
2777 long cm_Create(cm_scache_t *dscp, clientchar_t *cnamep, long flags, cm_attr_t *attrp,
2778 cm_scache_t **scpp, cm_user_t *userp, cm_req_t *reqp)
2783 cm_callbackRequest_t cbReq;
2786 cm_scache_t *scp = NULL;
2788 AFSStoreStatus inStatus;
2789 AFSFetchStatus updatedDirStatus;
2790 AFSFetchStatus newFileStatus;
2791 AFSCallBack newFileCallback;
2793 struct rx_connection * rxconnp;
2795 fschar_t * fnamep = NULL;
2797 memset(&volSync, 0, sizeof(volSync));
2799 /* can't create names with @sys in them; must expand it manually first.
2800 * return "invalid request" if they try.
2802 if (cm_ExpandSysName(cnamep, NULL, 0, 0)) {
2803 return CM_ERROR_ATSYS;
2806 #ifdef AFS_FREELANCE_CLIENT
2807 /* Freelance root volume does not hold files */
2808 if (cm_freelanceEnabled &&
2809 dscp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
2810 dscp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
2812 return CM_ERROR_NOACCESS;
2814 #endif /* AFS_FREELANCE_CLIENT */
2816 /* Check for RO volume */
2817 if (dscp->flags & CM_SCACHEFLAG_RO)
2818 return CM_ERROR_READONLY;
2820 /* before starting the RPC, mark that we're changing the file data, so
2821 * that someone who does a chmod will know to wait until our call
2824 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, CM_DIROP_FLAG_NONE,
2826 lock_ObtainWrite(&dscp->rw);
2827 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
2828 lock_ReleaseWrite(&dscp->rw);
2830 cm_StartCallbackGrantingCall(NULL, &cbReq);
2832 cm_EndDirOp(&dirop);
2839 fnamep = cm_ClientStringToFsStringAlloc(cnamep, -1, NULL);
2841 cm_StatusFromAttr(&inStatus, NULL, attrp);
2843 /* try the RPC now */
2844 osi_Log1(afsd_logp, "CALL CreateFile scp 0x%p", dscp);
2846 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
2850 dirAFSFid.Volume = dscp->fid.volume;
2851 dirAFSFid.Vnode = dscp->fid.vnode;
2852 dirAFSFid.Unique = dscp->fid.unique;
2854 rxconnp = cm_GetRxConn(connp);
2855 code = RXAFS_CreateFile(connp->rxconnp, &dirAFSFid, fnamep,
2856 &inStatus, &newAFSFid, &newFileStatus,
2857 &updatedDirStatus, &newFileCallback,
2859 rx_PutConnection(rxconnp);
2861 } while (cm_Analyze(connp, userp, reqp,
2862 &dscp->fid, &volSync, NULL, &cbReq, code));
2863 code = cm_MapRPCError(code, reqp);
2866 osi_Log1(afsd_logp, "CALL CreateFile FAILURE, code 0x%x", code);
2868 osi_Log0(afsd_logp, "CALL CreateFile SUCCESS");
2871 lock_ObtainWrite(&dirop.scp->dirlock);
2872 dirop.lockType = CM_DIRLOCK_WRITE;
2874 lock_ObtainWrite(&dscp->rw);
2876 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
2877 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
2878 lock_ReleaseWrite(&dscp->rw);
2880 /* now try to create the file's entry, too, but be careful to
2881 * make sure that we don't merge in old info. Since we weren't locking
2882 * out any requests during the file's creation, we may have pretty old
2886 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
2887 code = cm_GetSCache(&newFid, &scp, userp, reqp);
2889 lock_ObtainWrite(&scp->rw);
2890 scp->creator = userp; /* remember who created it */
2891 if (!cm_HaveCallback(scp)) {
2892 cm_EndCallbackGrantingCall(scp, &cbReq,
2893 &newFileCallback, &volSync, 0);
2894 cm_MergeStatus(dscp, scp, &newFileStatus, &volSync,
2898 lock_ReleaseWrite(&scp->rw);
2902 /* make sure we end things properly */
2904 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, NULL, 0);
2906 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
2907 cm_DirCreateEntry(&dirop, fnamep, &newFid);
2909 cm_BPlusDirCreateEntry(&dirop, cnamep, &newFid);
2912 cm_EndDirOp(&dirop);
2921 cm_ReleaseSCache(scp);
2927 * locked if TRUE means write-locked
2928 * else the cm_scache_t rw must not be held
2930 long cm_FSync(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp, afs_uint32 locked)
2935 lock_ReleaseWrite(&scp->rw);
2936 code = buf_CleanVnode(scp, userp, reqp);
2938 lock_ObtainWrite(&scp->rw);
2940 if (scp->mask & (CM_SCACHEMASK_TRUNCPOS
2941 | CM_SCACHEMASK_CLIENTMODTIME
2942 | CM_SCACHEMASK_LENGTH))
2943 code = cm_StoreMini(scp, userp, reqp);
2945 if (scp->flags & (CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE)) {
2946 code = (scp->flags & CM_SCACHEFLAG_OVERQUOTA) ? CM_ERROR_QUOTA : CM_ERROR_SPACE;
2947 scp->flags &= ~(CM_SCACHEFLAG_OVERQUOTA | CM_SCACHEFLAG_OUTOFSPACE);
2951 lock_ReleaseWrite(&scp->rw);
2952 } else if (locked) {
2953 lock_ObtainWrite(&scp->rw);
2958 long cm_MakeDir(cm_scache_t *dscp, clientchar_t *cnamep, long flags, cm_attr_t *attrp,
2959 cm_user_t *userp, cm_req_t *reqp, cm_scache_t **scpp)
2964 cm_callbackRequest_t cbReq;
2967 cm_scache_t *scp = NULL;
2969 AFSStoreStatus inStatus;
2970 AFSFetchStatus updatedDirStatus;
2971 AFSFetchStatus newDirStatus;
2972 AFSCallBack newDirCallback;
2974 struct rx_connection * rxconnp;
2976 fschar_t * fnamep = NULL;
2978 memset(&volSync, 0, sizeof(volSync));
2980 /* can't create names with @sys in them; must expand it manually first.
2981 * return "invalid request" if they try.
2983 if (cm_ExpandSysName(cnamep, NULL, 0, 0)) {
2984 return CM_ERROR_ATSYS;
2987 #ifdef AFS_FREELANCE_CLIENT
2988 /* Freelance root volume does not hold subdirectories */
2989 if (cm_freelanceEnabled &&
2990 dscp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
2991 dscp->fid.volume==AFS_FAKE_ROOT_VOL_ID )
2993 return CM_ERROR_NOACCESS;
2995 #endif /* AFS_FREELANCE_CLIENT */
2997 /* Check for RO volume */
2998 if (dscp->flags & CM_SCACHEFLAG_RO)
2999 return CM_ERROR_READONLY;
3001 /* before starting the RPC, mark that we're changing the directory
3002 * data, so that someone who does a chmod on the dir will wait until
3003 * our call completes.
3005 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, CM_DIROP_FLAG_NONE,
3007 lock_ObtainWrite(&dscp->rw);
3008 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3009 lock_ReleaseWrite(&dscp->rw);
3011 cm_StartCallbackGrantingCall(NULL, &cbReq);
3013 cm_EndDirOp(&dirop);
3020 fnamep = cm_ClientStringToFsStringAlloc(cnamep, -1, NULL);
3021 cm_StatusFromAttr(&inStatus, NULL, attrp);
3023 /* try the RPC now */
3024 osi_Log1(afsd_logp, "CALL MakeDir scp 0x%p", dscp);
3026 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3030 dirAFSFid.Volume = dscp->fid.volume;
3031 dirAFSFid.Vnode = dscp->fid.vnode;
3032 dirAFSFid.Unique = dscp->fid.unique;
3034 rxconnp = cm_GetRxConn(connp);
3035 code = RXAFS_MakeDir(connp->rxconnp, &dirAFSFid, fnamep,
3036 &inStatus, &newAFSFid, &newDirStatus,
3037 &updatedDirStatus, &newDirCallback,
3039 rx_PutConnection(rxconnp);
3041 } while (cm_Analyze(connp, userp, reqp,
3042 &dscp->fid, &volSync, NULL, &cbReq, code));
3043 code = cm_MapRPCError(code, reqp);
3046 osi_Log1(afsd_logp, "CALL MakeDir FAILURE, code 0x%x", code);
3048 osi_Log0(afsd_logp, "CALL MakeDir SUCCESS");
3051 lock_ObtainWrite(&dirop.scp->dirlock);
3052 dirop.lockType = CM_DIRLOCK_WRITE;
3054 lock_ObtainWrite(&dscp->rw);
3056 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
3057 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3058 lock_ReleaseWrite(&dscp->rw);
3060 /* now try to create the new dir's entry, too, but be careful to
3061 * make sure that we don't merge in old info. Since we weren't locking
3062 * out any requests during the file's creation, we may have pretty old
3066 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3067 code = cm_GetSCache(&newFid, &scp, userp, reqp);
3069 lock_ObtainWrite(&scp->rw);
3070 if (!cm_HaveCallback(scp)) {
3071 cm_EndCallbackGrantingCall(scp, &cbReq,
3072 &newDirCallback, &volSync, 0);
3073 cm_MergeStatus(dscp, scp, &newDirStatus, &volSync,
3077 lock_ReleaseWrite(&scp->rw);
3081 /* make sure we end things properly */
3083 cm_EndCallbackGrantingCall(NULL, &cbReq, NULL, NULL, 0);
3085 if (scp && cm_CheckDirOpForSingleChange(&dirop)) {
3086 cm_DirCreateEntry(&dirop, fnamep, &newFid);
3088 cm_BPlusDirCreateEntry(&dirop, cnamep, &newFid);
3091 cm_EndDirOp(&dirop);
3099 cm_ReleaseSCache(scp);
3102 /* and return error code */
3106 long cm_Link(cm_scache_t *dscp, clientchar_t *cnamep, cm_scache_t *sscp, long flags,
3107 cm_user_t *userp, cm_req_t *reqp)
3112 AFSFid existingAFSFid;
3113 AFSFetchStatus updatedDirStatus;
3114 AFSFetchStatus newLinkStatus;
3116 struct rx_connection * rxconnp;
3118 fschar_t * fnamep = NULL;
3120 memset(&volSync, 0, sizeof(volSync));
3122 if (dscp->fid.cell != sscp->fid.cell ||
3123 dscp->fid.volume != sscp->fid.volume) {
3124 return CM_ERROR_CROSSDEVLINK;
3127 /* Check for RO volume */
3128 if (dscp->flags & CM_SCACHEFLAG_RO)
3129 return CM_ERROR_READONLY;
3131 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, CM_DIROP_FLAG_NONE,
3133 lock_ObtainWrite(&dscp->rw);
3134 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3135 lock_ReleaseWrite(&dscp->rw);
3137 cm_EndDirOp(&dirop);
3142 fnamep = cm_ClientStringToFsStringAlloc(cnamep, -1, NULL);
3144 /* try the RPC now */
3145 osi_Log1(afsd_logp, "CALL Link scp 0x%p", dscp);
3147 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3150 dirAFSFid.Volume = dscp->fid.volume;
3151 dirAFSFid.Vnode = dscp->fid.vnode;
3152 dirAFSFid.Unique = dscp->fid.unique;
3154 existingAFSFid.Volume = sscp->fid.volume;
3155 existingAFSFid.Vnode = sscp->fid.vnode;
3156 existingAFSFid.Unique = sscp->fid.unique;
3158 rxconnp = cm_GetRxConn(connp);
3159 code = RXAFS_Link(rxconnp, &dirAFSFid, fnamep, &existingAFSFid,
3160 &newLinkStatus, &updatedDirStatus, &volSync);
3161 rx_PutConnection(rxconnp);
3162 osi_Log1(afsd_logp," RXAFS_Link returns 0x%x", code);
3164 } while (cm_Analyze(connp, userp, reqp,
3165 &dscp->fid, &volSync, NULL, NULL, code));
3167 code = cm_MapRPCError(code, reqp);
3170 osi_Log1(afsd_logp, "CALL Link FAILURE, code 0x%x", code);
3172 osi_Log0(afsd_logp, "CALL Link SUCCESS");
3175 lock_ObtainWrite(&dirop.scp->dirlock);
3176 dirop.lockType = CM_DIRLOCK_WRITE;
3178 lock_ObtainWrite(&dscp->rw);
3180 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
3182 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3183 lock_ReleaseWrite(&dscp->rw);
3186 if (cm_CheckDirOpForSingleChange(&dirop)) {
3187 cm_DirCreateEntry(&dirop, fnamep, &sscp->fid);
3189 cm_BPlusDirCreateEntry(&dirop, cnamep, &sscp->fid);
3193 cm_EndDirOp(&dirop);
3195 /* Update the linked object status */
3197 lock_ObtainWrite(&sscp->rw);
3198 cm_MergeStatus(NULL, sscp, &newLinkStatus, &volSync, userp, reqp, 0);
3199 lock_ReleaseWrite(&sscp->rw);
3207 long cm_SymLink(cm_scache_t *dscp, clientchar_t *cnamep, fschar_t *contentsp, long flags,
3208 cm_attr_t *attrp, cm_user_t *userp, cm_req_t *reqp)
3216 AFSStoreStatus inStatus;
3217 AFSFetchStatus updatedDirStatus;
3218 AFSFetchStatus newLinkStatus;
3220 struct rx_connection * rxconnp;
3222 fschar_t *fnamep = NULL;
3224 /* Check for RO volume */
3225 if (dscp->flags & CM_SCACHEFLAG_RO)
3226 return CM_ERROR_READONLY;
3228 memset(&volSync, 0, sizeof(volSync));
3230 /* before starting the RPC, mark that we're changing the directory data,
3231 * so that someone who does a chmod on the dir will wait until our
3234 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, CM_DIROP_FLAG_NONE,
3236 lock_ObtainWrite(&dscp->rw);
3237 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3238 lock_ReleaseWrite(&dscp->rw);
3240 cm_EndDirOp(&dirop);
3245 fnamep = cm_ClientStringToFsStringAlloc(cnamep, -1, NULL);
3247 cm_StatusFromAttr(&inStatus, NULL, attrp);
3249 /* try the RPC now */
3250 osi_Log1(afsd_logp, "CALL Symlink scp 0x%p", dscp);
3252 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3256 dirAFSFid.Volume = dscp->fid.volume;
3257 dirAFSFid.Vnode = dscp->fid.vnode;
3258 dirAFSFid.Unique = dscp->fid.unique;
3260 rxconnp = cm_GetRxConn(connp);
3261 code = RXAFS_Symlink(rxconnp, &dirAFSFid, fnamep, contentsp,
3262 &inStatus, &newAFSFid, &newLinkStatus,
3263 &updatedDirStatus, &volSync);
3264 rx_PutConnection(rxconnp);
3266 } while (cm_Analyze(connp, userp, reqp,
3267 &dscp->fid, &volSync, NULL, NULL, code));
3268 code = cm_MapRPCError(code, reqp);
3271 osi_Log1(afsd_logp, "CALL Symlink FAILURE, code 0x%x", code);
3273 osi_Log0(afsd_logp, "CALL Symlink SUCCESS");
3276 lock_ObtainWrite(&dirop.scp->dirlock);
3277 dirop.lockType = CM_DIRLOCK_WRITE;
3279 lock_ObtainWrite(&dscp->rw);
3281 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
3282 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3283 lock_ReleaseWrite(&dscp->rw);
3286 if (cm_CheckDirOpForSingleChange(&dirop)) {
3287 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3289 cm_DirCreateEntry(&dirop, fnamep, &newFid);
3291 cm_BPlusDirCreateEntry(&dirop, cnamep, &newFid);
3295 cm_EndDirOp(&dirop);
3297 /* now try to create the new dir's entry, too, but be careful to
3298 * make sure that we don't merge in old info. Since we weren't locking
3299 * out any requests during the file's creation, we may have pretty old
3303 cm_SetFid(&newFid, dscp->fid.cell, dscp->fid.volume, newAFSFid.Vnode, newAFSFid.Unique);
3304 code = cm_GetSCache(&newFid, &scp, userp, reqp);
3306 lock_ObtainWrite(&scp->rw);
3307 if (!cm_HaveCallback(scp)) {
3308 cm_MergeStatus(dscp, scp, &newLinkStatus, &volSync,
3311 lock_ReleaseWrite(&scp->rw);
3312 cm_ReleaseSCache(scp);
3318 /* and return error code */
3322 /*! \brief Remove a directory
3324 Encapsulates a call to RXAFS_RemoveDir().
3326 \param[in] dscp cm_scache_t for the directory containing the
3327 directory to be removed.
3329 \param[in] fnamep This will be the original name of the directory
3330 as known to the file server. It will be passed in to RXAFS_RemoveDir().
3331 This parameter is optional. If it is not provided the value
3334 \param[in] cnamep Normalized name used to update the local
3337 \param[in] userp cm_user_t for the request.
3339 \param[in] reqp Request tracker.
3341 long cm_RemoveDir(cm_scache_t *dscp, fschar_t *fnamep, clientchar_t *cnamep, cm_user_t *userp, cm_req_t *reqp)
3347 AFSFetchStatus updatedDirStatus;
3349 struct rx_connection * rxconnp;
3351 cm_scache_t *scp = NULL;
3352 int free_fnamep = FALSE;
3354 memset(&volSync, 0, sizeof(volSync));
3356 if (fnamep == NULL) {
3359 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ,
3360 CM_DIROP_FLAG_NONE, &dirop);
3362 code = cm_BPlusDirLookupOriginalName(&dirop, cnamep, &fnamep);
3365 cm_EndDirOp(&dirop);
3372 code = cm_Lookup(dscp, cnamep, CM_FLAG_NOMOUNTCHASE, userp, reqp, &scp);
3376 /* Check for RO volume */
3377 if (dscp->flags & CM_SCACHEFLAG_RO) {
3378 code = CM_ERROR_READONLY;
3382 /* before starting the RPC, mark that we're changing the directory data,
3383 * so that someone who does a chmod on the dir will wait until our
3386 cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_NONE, CM_DIROP_FLAG_NONE,
3388 lock_ObtainWrite(&dscp->rw);
3389 code = cm_SyncOp(dscp, NULL, userp, reqp, 0, CM_SCACHESYNC_STOREDATA);
3390 lock_ReleaseWrite(&dscp->rw);
3392 cm_EndDirOp(&dirop);
3397 /* try the RPC now */
3398 osi_Log1(afsd_logp, "CALL RemoveDir scp 0x%p", dscp);
3400 code = cm_ConnFromFID(&dscp->fid, userp, reqp, &connp);
3404 dirAFSFid.Volume = dscp->fid.volume;
3405 dirAFSFid.Vnode = dscp->fid.vnode;
3406 dirAFSFid.Unique = dscp->fid.unique;
3408 rxconnp = cm_GetRxConn(connp);
3409 code = RXAFS_RemoveDir(rxconnp, &dirAFSFid, fnamep,
3410 &updatedDirStatus, &volSync);
3411 rx_PutConnection(rxconnp);
3413 } while (cm_Analyze(connp, userp, reqp,
3414 &dscp->fid, &volSync, NULL, NULL, code));
3415 code = cm_MapRPCErrorRmdir(code, reqp);
3418 osi_Log1(afsd_logp, "CALL RemoveDir FAILURE, code 0x%x", code);
3420 osi_Log0(afsd_logp, "CALL RemoveDir SUCCESS");
3423 lock_ObtainWrite(&dirop.scp->dirlock);
3424 dirop.lockType = CM_DIRLOCK_WRITE;
3426 lock_ObtainWrite(&dscp->rw);
3428 cm_dnlcRemove(dscp, cnamep);
3429 cm_MergeStatus(NULL, dscp, &updatedDirStatus, &volSync, userp, reqp, CM_MERGEFLAG_DIROP);
3431 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_STOREDATA);
3432 lock_ReleaseWrite(&dscp->rw);
3435 if (cm_CheckDirOpForSingleChange(&dirop) && cnamep != NULL) {
3436 cm_DirDeleteEntry(&dirop, fnamep);
3438 cm_BPlusDirDeleteEntry(&dirop, cnamep);
3442 cm_EndDirOp(&dirop);
3445 cm_ReleaseSCache(scp);
3447 lock_ObtainWrite(&scp->rw);
3448 scp->flags |= CM_SCACHEFLAG_DELETED;
3449 lock_ReleaseWrite(&scp->rw);
3457 /* and return error code */