2 * Copyright (c) 2008 Secure Endpoints, Inc.
3 * Copyright (c) 2009-2011 Your File System, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Secure Endpoints Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission from Secure Endpoints, Inc. and
17 * Your File System, Inc.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <afsconfig.h>
33 #include <afs/param.h>
36 #define _WIN32_WINNT 0x0500
38 #define _CRT_SECURE_NO_DEPRECATE
39 #define _CRT_NON_CONFORMING_SWPRINTFS
40 #define INITGUID /* define AFS_AUTH_GUID_NO_PAG */
43 #define WIN32_NO_STATUS
53 #pragma warning(disable: 4005)
57 #include "..\\Common\\AFSUserDefines.h"
58 #include "..\\Common\\AFSUserStructs.h"
76 #include <RDRPrototypes.h>
80 static CHAR * RDR_extentBaseAddress = NULL;
83 RDR_InitReq(cm_req_t *reqp, BOOL bWow64)
86 reqp->flags |= CM_REQ_SOURCE_REDIR;
88 reqp->flags |= CM_REQ_WOW64;
92 RDR_fid2FID( cm_fid_t *fid, AFSFileID *FileId)
94 FileId->Cell = fid->cell;
95 FileId->Volume = fid->volume;
96 FileId->Vnode = fid->vnode;
97 FileId->Unique = fid->unique;
98 FileId->Hash = fid->hash;
102 RDR_FID2fid( AFSFileID *FileId, cm_fid_t *fid)
104 fid->cell = FileId->Cell;
105 fid->volume = FileId->Volume;
106 fid->vnode = FileId->Vnode;
107 fid->unique = FileId->Unique;
108 fid->hash = FileId->Hash;
112 RDR_SetInitParams( OUT AFSRedirectorInitInfo **ppRedirInitInfo, OUT DWORD * pRedirInitInfoLen )
114 extern char cm_CachePath[];
115 extern cm_config_data_t cm_data;
116 extern int smb_hideDotFiles;
117 size_t cm_CachePathLen = strlen(cm_CachePath);
119 DWORD TempPathLen = ExpandEnvironmentStringsW(L"%TEMP%", NULL, 0);
120 MEMORYSTATUSEX memStatus;
121 DWORD maxMemoryCacheSize;
123 memStatus.dwLength = sizeof(memStatus);
124 if (GlobalMemoryStatusEx(&memStatus)) {
126 * Use the memory extent interface in the afs redirector
127 * whenever the cache size is less than equal to 10% of
128 * physical memory. Do not use too much because this memory
129 * will be locked by the redirector so it can't be swapped
132 maxMemoryCacheSize = (DWORD)(memStatus.ullTotalPhys / 1024 / 10);
135 * If we can't determine the amount of physical memory
136 * in the system, be conservative and limit the use of
137 * memory extent interface to 64MB data caches.
139 maxMemoryCacheSize = 65536;
142 *pRedirInitInfoLen = (DWORD) (sizeof(AFSRedirectorInitInfo) + (cm_CachePathLen + TempPathLen) * sizeof(WCHAR));
143 *ppRedirInitInfo = (AFSRedirectorInitInfo *)malloc(*pRedirInitInfoLen);
144 (*ppRedirInitInfo)->Flags = smb_hideDotFiles ? AFS_REDIR_INIT_FLAG_HIDE_DOT_FILES : 0;
145 (*ppRedirInitInfo)->Flags |= cm_shortNames ? 0 : AFS_REDIR_INIT_FLAG_DISABLE_SHORTNAMES;
146 (*ppRedirInitInfo)->MaximumChunkLength = cm_data.chunkSize;
147 (*ppRedirInitInfo)->GlobalFileId.Cell = cm_data.rootFid.cell;
148 (*ppRedirInitInfo)->GlobalFileId.Volume = cm_data.rootFid.volume;
149 (*ppRedirInitInfo)->GlobalFileId.Vnode = cm_data.rootFid.vnode;
150 (*ppRedirInitInfo)->GlobalFileId.Unique = cm_data.rootFid.unique;
151 (*ppRedirInitInfo)->GlobalFileId.Hash = cm_data.rootFid.hash;
152 (*ppRedirInitInfo)->ExtentCount.QuadPart = cm_data.buf_nbuffers;
153 (*ppRedirInitInfo)->CacheBlockSize = cm_data.blockSize;
154 (*ppRedirInitInfo)->MaxPathLinkCount = MAX_FID_COUNT;
155 (*ppRedirInitInfo)->NameArrayLength = MAX_FID_COUNT;
156 if (cm_virtualCache || cm_data.bufferSize <= maxMemoryCacheSize) {
157 osi_Log0(afsd_logp, "RDR_SetInitParams Initializing Memory Extent Interface");
158 (*ppRedirInitInfo)->MemoryCacheOffset.QuadPart = (LONGLONG)cm_data.bufDataBaseAddress;
159 (*ppRedirInitInfo)->MemoryCacheLength.QuadPart = cm_data.bufEndOfData - cm_data.bufDataBaseAddress;
160 (*ppRedirInitInfo)->CacheFileNameLength = 0;
161 RDR_extentBaseAddress = cm_data.bufDataBaseAddress;
163 (*ppRedirInitInfo)->MemoryCacheOffset.QuadPart = 0;
164 (*ppRedirInitInfo)->MemoryCacheLength.QuadPart = 0;
165 (*ppRedirInitInfo)->CacheFileNameLength = (ULONG) (cm_CachePathLen * sizeof(WCHAR));
166 err = mbstowcs((*ppRedirInitInfo)->CacheFileName, cm_CachePath, (cm_CachePathLen + 1) *sizeof(WCHAR));
168 free(*ppRedirInitInfo);
169 osi_Log0(afsd_logp, "RDR_SetInitParams Invalid Object Name");
170 return STATUS_OBJECT_NAME_INVALID;
172 RDR_extentBaseAddress = cm_data.baseAddress;
174 (*ppRedirInitInfo)->DumpFileLocationOffset = FIELD_OFFSET(AFSRedirectorInitInfo, CacheFileName) + (*ppRedirInitInfo)->CacheFileNameLength;
175 (*ppRedirInitInfo)->DumpFileLocationLength = (TempPathLen - 1) * sizeof(WCHAR);
176 ExpandEnvironmentStringsW(L"%TEMP%",
177 (LPWSTR)(((PBYTE)(*ppRedirInitInfo)) + (*ppRedirInitInfo)->DumpFileLocationOffset),
180 osi_Log0(afsd_logp,"RDR_SetInitParams Success");
184 static wchar_t cname[MAX_COMPUTERNAME_LENGTH+1] = L"";
187 RDR_GetLocalSystemUser( void)
190 cm_user_t *userp = NULL;
192 if ( cname[0] == '\0') {
193 int len = MAX_COMPUTERNAME_LENGTH+1;
194 GetComputerNameW(cname, &len);
197 unp = smb_FindUserByName(NTSID_LOCAL_SYSTEM, cname, SMB_FLAG_CREATE);
198 lock_ObtainMutex(&unp->mx);
200 unp->userp = cm_NewUser();
201 unp->flags |= SMB_USERNAMEFLAG_SID;
202 lock_ReleaseMutex(&unp->mx);
205 smb_ReleaseUsername(unp);
208 userp = cm_rootUserp;
216 RDR_UserFromCommRequest( IN AFSCommRequest *RequestBuffer)
219 return RDR_UserFromAuthGroup( &RequestBuffer->AuthGroup);
223 RDR_UserFromAuthGroup( IN GUID *pGuid)
226 cm_user_t * userp = NULL;
227 RPC_WSTR UuidString = NULL;
229 if (UuidToStringW((UUID *)pGuid, &UuidString) != RPC_S_OK)
232 if ( cname[0] == '\0') {
233 int len = MAX_COMPUTERNAME_LENGTH+1;
234 GetComputerNameW(cname, &len);
238 unp = smb_FindUserByName(UuidString, cname, SMB_FLAG_CREATE);
239 lock_ObtainMutex(&unp->mx);
241 unp->userp = cm_NewUser();
242 memcpy(&unp->userp->authgroup, pGuid, sizeof(GUID));
244 unp->flags |= SMB_USERNAMEFLAG_SID;
245 lock_ReleaseMutex(&unp->mx);
248 smb_ReleaseUsername(unp);
252 userp = cm_rootUserp;
256 osi_Log2(afsd_logp, "RDR_UserFromCommRequest Guid %S userp = 0x%p",
257 osi_LogSaveStringW(afsd_logp, UuidString),
261 RpcStringFreeW(&UuidString);
267 RDR_ReleaseUser( IN cm_user_t *userp )
269 osi_Log1(afsd_logp, "RDR_ReleaseUser userp = 0x%p", userp);
270 cm_ReleaseUser(userp);
275 * RDR_FlagScpInUse flags the scp with CM_SCACHEFLAG_RDR_IN_USE
278 RDR_FlagScpInUse( IN cm_scache_t *scp, IN BOOL bLocked )
281 lock_ObtainWrite(&scp->rw);
283 lock_AssertWrite(&scp->rw);
284 scp->flags |= CM_SCACHEFLAG_RDR_IN_USE;
287 lock_ReleaseWrite(&scp->rw);
291 * Obtain the status information for the specified object using
292 * an inline bulk status rpc. cm_BPlusDirEnumBulkStatOne() will
293 * obtain current status for the directory object, the object
294 * which is the focus of the inquiry and as many other objects
295 * in the directory for which there are not callbacks registered
296 * since we are likely to be asked for other objects in the directory.
299 RDR_BulkStatLookup( cm_scache_t *dscp,
304 cm_direnum_t * enump = NULL;
308 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
310 code = cm_BPlusDirEnumerate(dscp, userp, reqp, TRUE, NULL, TRUE, &enump);
312 osi_Log1(afsd_logp, "RDR_BulkStatLookup cm_BPlusDirEnumerate failure code=0x%x",
317 osi_Log1(afsd_logp, "RDR_BulkStatLookup cm_BeginDirOp failure code=0x%x",
323 code = cm_BPlusDirEnumBulkStatOne(enump, scp);
325 osi_Log1(afsd_logp, "RDR_BulkStatLookup cm_BPlusDirEnumBulkStatOne failure code=0x%x",
328 cm_BPlusDirFreeEnumeration(enump);
335 #define RDR_POP_FOLLOW_MOUNTPOINTS 0x01
336 #define RDR_POP_EVALUATE_SYMLINKS 0x02
337 #define RDR_POP_WOW64 0x04
338 #define RDR_POP_NO_GETSTATUS 0x08
341 RDR_PopulateCurrentEntry( IN AFSDirEnumEntry * pCurrentEntry,
342 IN DWORD dwMaxEntryLength,
343 IN cm_scache_t * dscp,
344 IN cm_scache_t * scp,
345 IN cm_user_t * userp,
348 IN wchar_t * shortName,
350 IN afs_uint32 cmError,
351 OUT AFSDirEnumEntry **ppNextEntry,
352 OUT DWORD * pdwRemainingLength)
355 WCHAR * wname, *wtarget;
358 afs_uint32 code = 0, code2 = 0;
359 BOOL bMustFake = FALSE;
361 osi_Log5(afsd_logp, "RDR_PopulateCurrentEntry dscp=0x%p scp=0x%p name=%S short=%S flags=0x%x",
362 dscp, scp, osi_LogSaveStringW(afsd_logp, name),
363 osi_LogSaveStringW(afsd_logp, shortName), dwFlags);
364 osi_Log1(afsd_logp, "... maxLength=%d", dwMaxEntryLength);
366 if (dwMaxEntryLength < sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t)) {
368 *ppNextEntry = pCurrentEntry;
369 if (pdwRemainingLength)
370 *pdwRemainingLength = dwMaxEntryLength;
371 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry Not Enough Room for Entry %d < %d",
372 dwMaxEntryLength, sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t));
373 return CM_ERROR_TOOBIG;
381 dwEntryLength = sizeof(AFSDirEnumEntry);
383 lock_ObtainWrite(&scp->rw);
384 if (dwFlags & RDR_POP_NO_GETSTATUS) {
385 if (!cm_HaveCallback(scp))
388 #ifdef AFS_FREELANCE_CLIENT
389 if (scp->fid.cell == AFS_FAKE_ROOT_CELL_ID && scp->fid.volume == AFS_FAKE_ROOT_VOL_ID) {
391 * If the FID is from the Freelance Local Root always perform
392 * a single item status check.
394 code = cm_SyncOp( scp, NULL, userp, reqp, 0,
395 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
397 lock_ReleaseWrite(&scp->rw);
398 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_SyncOp failed for scp=0x%p code=0x%x",
406 * For non-Freelance objects, check to see if we have current
407 * status information. If not, perform a bulk status lookup of multiple
408 * entries in order to reduce the number of RPCs issued to the file server.
410 if (cm_EAccesFindEntry(userp, &scp->fid))
412 else if (!cm_HaveCallback(scp)) {
413 lock_ReleaseWrite(&scp->rw);
414 code = RDR_BulkStatLookup(dscp, scp, userp, reqp);
416 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry RDR_BulkStatLookup failed for scp=0x%p code=0x%x",
420 lock_ObtainWrite(&scp->rw);
422 * RDR_BulkStatLookup can succeed but it may be the case that there
423 * still is not valid status info. If we get this far, generate fake
426 if (!cm_HaveCallback(scp))
432 /* Populate the error code */
433 smb_MapNTError(cmError, &pCurrentEntry->NTStatus, TRUE);
435 /* Populate the real or fake data */
436 pCurrentEntry->FileId.Cell = scp->fid.cell;
437 pCurrentEntry->FileId.Volume = scp->fid.volume;
438 pCurrentEntry->FileId.Vnode = scp->fid.vnode;
439 pCurrentEntry->FileId.Unique = scp->fid.unique;
440 pCurrentEntry->FileId.Hash = scp->fid.hash;
442 pCurrentEntry->FileType = scp->fileType;
444 pCurrentEntry->DataVersion.QuadPart = scp->dataVersion;
446 if (scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
447 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
448 cm_LargeSearchTimeFromUnixTime(&ft, MAX_AFS_UINT32);
450 cm_LargeSearchTimeFromUnixTime(&ft, scp->cbExpires);
452 pCurrentEntry->Expiration.LowPart = ft.dwLowDateTime;
453 pCurrentEntry->Expiration.HighPart = ft.dwHighDateTime;
456 /* 1969-12-31 23:59:59 +00 */
457 ft.dwHighDateTime = 0x19DB200;
458 ft.dwLowDateTime = 0x5BB78980;
460 cm_LargeSearchTimeFromUnixTime(&ft, scp->clientModTime);
461 pCurrentEntry->CreationTime.LowPart = ft.dwLowDateTime;
462 pCurrentEntry->CreationTime.HighPart = ft.dwHighDateTime;
463 pCurrentEntry->LastAccessTime = pCurrentEntry->CreationTime;
464 pCurrentEntry->LastWriteTime = pCurrentEntry->CreationTime;
465 pCurrentEntry->ChangeTime = pCurrentEntry->CreationTime;
467 pCurrentEntry->EndOfFile = scp->length;
468 pCurrentEntry->AllocationSize = scp->length;
471 switch (scp->fileType) {
472 case CM_SCACHETYPE_DIRECTORY:
473 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY;
475 case CM_SCACHETYPE_MOUNTPOINT:
476 case CM_SCACHETYPE_INVALID:
477 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY | SMB_ATTR_REPARSE_POINT;
479 case CM_SCACHETYPE_SYMLINK:
480 if (cm_TargetPerceivedAsDirectory(scp->mountPointStringp))
481 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY | SMB_ATTR_REPARSE_POINT;
483 pCurrentEntry->FileAttributes = SMB_ATTR_REPARSE_POINT;
486 /* if we get here we either have a normal file
487 * or we have a file for which we have never
488 * received status info. In this case, we can
489 * check the even/odd value of the entry's vnode.
490 * odd means it is to be treated as a directory
491 * and even means it is to be treated as a file.
493 if (scp->fid.vnode & 0x1)
494 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY;
496 pCurrentEntry->FileAttributes = SMB_ATTR_NORMAL;
499 pCurrentEntry->FileAttributes = smb_ExtAttributes(scp);
500 pCurrentEntry->EaSize = 0;
501 pCurrentEntry->Links = scp->linkCount;
503 len = wcslen(shortName);
504 wcsncpy(pCurrentEntry->ShortName, shortName, len);
505 pCurrentEntry->ShortNameLength = (CCHAR)(len * sizeof(WCHAR));
507 pCurrentEntry->FileNameOffset = sizeof(AFSDirEnumEntry);
509 wname = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->FileNameOffset);
510 wcsncpy(wname, name, len);
511 pCurrentEntry->FileNameLength = (ULONG)(sizeof(WCHAR) * len);
513 osi_Log3(afsd_logp, "RDR_PopulateCurrentEntry scp=0x%p fileType=%d dv=%u",
514 scp, scp->fileType, (afs_uint32)scp->dataVersion);
516 if (!(dwFlags & RDR_POP_NO_GETSTATUS))
517 cm_SyncOpDone( scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
519 if ((dwFlags & RDR_POP_NO_GETSTATUS) || !cm_HaveCallback(scp)) {
520 pCurrentEntry->TargetNameOffset = 0;
521 pCurrentEntry->TargetNameLength = 0;
524 switch (scp->fileType) {
525 case CM_SCACHETYPE_MOUNTPOINT:
526 if (dwFlags & RDR_POP_FOLLOW_MOUNTPOINTS) {
527 if ((code2 = cm_ReadMountPoint(scp, userp, reqp)) == 0) {
528 cm_scache_t *targetScp = NULL;
530 pCurrentEntry->TargetNameOffset = pCurrentEntry->FileNameOffset + pCurrentEntry->FileNameLength;
531 len = strlen(scp->mountPointStringp);
532 wtarget = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->TargetNameOffset);
535 cch = MultiByteToWideChar( CP_UTF8, 0, scp->mountPointStringp,
538 len * sizeof(WCHAR));
540 mbstowcs(wtarget, scp->mountPointStringp, len);
542 pCurrentEntry->TargetNameLength = (ULONG)(sizeof(WCHAR) * len);
544 code2 = cm_FollowMountPoint(scp, dscp, userp, reqp, &targetScp);
547 pCurrentEntry->TargetFileId.Cell = targetScp->fid.cell;
548 pCurrentEntry->TargetFileId.Volume = targetScp->fid.volume;
549 pCurrentEntry->TargetFileId.Vnode = targetScp->fid.vnode;
550 pCurrentEntry->TargetFileId.Unique = targetScp->fid.unique;
551 pCurrentEntry->TargetFileId.Hash = targetScp->fid.hash;
553 osi_Log4(afsd_logp, "RDR_PopulateCurrentEntry target FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
554 pCurrentEntry->TargetFileId.Cell, pCurrentEntry->TargetFileId.Volume,
555 pCurrentEntry->TargetFileId.Vnode, pCurrentEntry->TargetFileId.Unique);
557 cm_ReleaseSCache(targetScp);
559 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_FollowMountPoint failed scp=0x%p code=0x%x",
563 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_ReadMountPoint failed scp=0x%p code=0x%x",
568 case CM_SCACHETYPE_SYMLINK:
569 case CM_SCACHETYPE_DFSLINK:
571 pCurrentEntry->TargetNameOffset = pCurrentEntry->FileNameOffset + pCurrentEntry->FileNameLength;
572 wtarget = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->TargetNameOffset);
574 if (dwFlags & RDR_POP_EVALUATE_SYMLINKS) {
577 code2 = cm_HandleLink(scp, userp, reqp);
579 mp = scp->mountPointStringp;
582 /* Strip off the msdfs: prefix from the target name for the file system */
583 if (scp->fileType == CM_SCACHETYPE_DFSLINK) {
584 osi_Log0(afsd_logp, "RDR_PopulateCurrentEntry DFSLink Detected");
585 pCurrentEntry->FileType = scp->fileType;
587 if (!strncmp("msdfs:", mp, 6)) {
592 /* only send one slash to the redirector */
593 if (mp[0] == '\\' && mp[1] == '\\') {
598 cch = MultiByteToWideChar( CP_UTF8, 0, mp,
601 len * sizeof(WCHAR));
603 mbstowcs(wtarget, mp, len);
606 pCurrentEntry->TargetNameLength = (ULONG)(sizeof(WCHAR) * len);
608 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_HandleLink failed scp=0x%p code=0x%x",
617 pCurrentEntry->TargetNameOffset = 0;
618 pCurrentEntry->TargetNameLength = 0;
620 lock_ReleaseWrite(&scp->rw);
622 dwEntryLength += pCurrentEntry->FileNameLength + pCurrentEntry->TargetNameLength;
623 dwEntryLength += (dwEntryLength % 8) ? 8 - (dwEntryLength % 8) : 0; /* quad align */
625 *ppNextEntry = (AFSDirEnumEntry *)((PBYTE)pCurrentEntry + dwEntryLength);
626 if (pdwRemainingLength)
627 *pdwRemainingLength = dwMaxEntryLength - dwEntryLength;
629 osi_Log3(afsd_logp, "RDR_PopulateCurrentEntry Success FileNameLength=%d TargetNameLength=%d RemainingLength=%d",
630 pCurrentEntry->FileNameLength, pCurrentEntry->TargetNameLength, *pdwRemainingLength);
636 RDR_PopulateCurrentEntryNoScp( IN AFSDirEnumEntry * pCurrentEntry,
637 IN DWORD dwMaxEntryLength,
638 IN cm_scache_t * dscp,
640 IN cm_user_t * userp,
643 IN wchar_t * shortName,
645 IN afs_uint32 cmError,
646 OUT AFSDirEnumEntry **ppNextEntry,
647 OUT DWORD * pdwRemainingLength)
653 afs_uint32 code = 0, code2 = 0;
655 osi_Log4(afsd_logp, "RDR_PopulateCurrentEntryNoEntry dscp=0x%p name=%S short=%S flags=0x%x",
656 dscp, osi_LogSaveStringW(afsd_logp, name),
657 osi_LogSaveStringW(afsd_logp, shortName), dwFlags);
658 osi_Log1(afsd_logp, "... maxLength=%d", dwMaxEntryLength);
660 if (dwMaxEntryLength < sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t)) {
662 *ppNextEntry = pCurrentEntry;
663 if (pdwRemainingLength)
664 *pdwRemainingLength = dwMaxEntryLength;
665 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntryNoEntry Not Enough Room for Entry %d < %d",
666 dwMaxEntryLength, sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t));
667 return CM_ERROR_TOOBIG;
675 dwEntryLength = sizeof(AFSDirEnumEntry);
677 /* Populate the error code */
678 smb_MapNTError(cmError, &pCurrentEntry->NTStatus, TRUE);
680 /* Populate the fake data */
681 pCurrentEntry->FileId.Cell = fidp->cell;
682 pCurrentEntry->FileId.Volume = fidp->volume;
683 pCurrentEntry->FileId.Vnode = fidp->vnode;
684 pCurrentEntry->FileId.Unique = fidp->unique;
685 pCurrentEntry->FileId.Hash = fidp->hash;
687 pCurrentEntry->FileType = CM_SCACHETYPE_UNKNOWN;
689 pCurrentEntry->DataVersion.QuadPart = CM_SCACHE_VERSION_BAD;
691 cm_LargeSearchTimeFromUnixTime(&ft, 0);
692 pCurrentEntry->Expiration.LowPart = ft.dwLowDateTime;
693 pCurrentEntry->Expiration.HighPart = ft.dwHighDateTime;
695 cm_LargeSearchTimeFromUnixTime(&ft, 0);
696 pCurrentEntry->CreationTime.LowPart = ft.dwLowDateTime;
697 pCurrentEntry->CreationTime.HighPart = ft.dwHighDateTime;
698 pCurrentEntry->LastAccessTime = pCurrentEntry->CreationTime;
699 pCurrentEntry->LastWriteTime = pCurrentEntry->CreationTime;
700 pCurrentEntry->ChangeTime = pCurrentEntry->CreationTime;
702 pCurrentEntry->EndOfFile.QuadPart = 0;
703 pCurrentEntry->AllocationSize.QuadPart = 0;
704 pCurrentEntry->FileAttributes = 0;
705 pCurrentEntry->EaSize = 0;
706 pCurrentEntry->Links = 0;
708 len = wcslen(shortName);
709 wcsncpy(pCurrentEntry->ShortName, shortName, len);
710 pCurrentEntry->ShortNameLength = (CCHAR)(len * sizeof(WCHAR));
712 pCurrentEntry->FileNameOffset = sizeof(AFSDirEnumEntry);
714 wname = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->FileNameOffset);
715 wcsncpy(wname, name, len);
716 pCurrentEntry->FileNameLength = (ULONG)(sizeof(WCHAR) * len);
718 pCurrentEntry->TargetNameOffset = 0;
719 pCurrentEntry->TargetNameLength = 0;
721 dwEntryLength += pCurrentEntry->FileNameLength + pCurrentEntry->TargetNameLength;
722 dwEntryLength += (dwEntryLength % 8) ? 8 - (dwEntryLength % 8) : 0; /* quad align */
724 *ppNextEntry = (AFSDirEnumEntry *)((PBYTE)pCurrentEntry + dwEntryLength);
725 if (pdwRemainingLength)
726 *pdwRemainingLength = dwMaxEntryLength - dwEntryLength;
728 osi_Log3(afsd_logp, "RDR_PopulateCurrentEntryNoScp Success FileNameLength=%d TargetNameLength=%d RemainingLength=%d",
729 pCurrentEntry->FileNameLength, pCurrentEntry->TargetNameLength, *pdwRemainingLength);
735 RDR_EnumerateDirectory( IN cm_user_t *userp,
737 IN AFSDirQueryCB *QueryCB,
740 IN DWORD ResultBufferLength,
741 IN OUT AFSCommResult **ResultCB)
744 cm_direnum_t * enump = NULL;
745 AFSDirEnumResp * pDirEnumResp;
746 AFSDirEnumEntry * pCurrentEntry;
747 size_t size = ResultBufferLength ? sizeof(AFSCommResult) + ResultBufferLength - 1 : sizeof(AFSCommResult);
748 DWORD dwMaxEntryLength;
751 cm_scache_t * dscp = NULL;
754 RDR_InitReq(&req, bWow64);
756 osi_Log4(afsd_logp, "RDR_EnumerateDirectory FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
757 DirID.Cell, DirID.Volume, DirID.Vnode, DirID.Unique);
759 *ResultCB = (AFSCommResult *)malloc(size);
761 osi_Log0(afsd_logp, "RDR_EnumerateDirectory Out of Memory");
765 memset(*ResultCB, 0, size);
767 if (QueryCB->EnumHandle == (ULONG_PTR)-1) {
768 osi_Log0(afsd_logp, "RDR_EnumerateDirectory No More Entries");
769 (*ResultCB)->ResultStatus = STATUS_NO_MORE_ENTRIES;
770 (*ResultCB)->ResultBufferLength = 0;
774 (*ResultCB)->ResultBufferLength = dwMaxEntryLength = ResultBufferLength;
775 if (ResultBufferLength) {
776 pDirEnumResp = (AFSDirEnumResp *)&(*ResultCB)->ResultData;
777 pCurrentEntry = (AFSDirEnumEntry *)&pDirEnumResp->Entry;
778 dwMaxEntryLength -= FIELD_OFFSET( AFSDirEnumResp, Entry); /* AFSDirEnumResp */
781 if (DirID.Cell != 0) {
782 fid.cell = DirID.Cell;
783 fid.volume = DirID.Volume;
784 fid.vnode = DirID.Vnode;
785 fid.unique = DirID.Unique;
786 fid.hash = DirID.Hash;
788 code = cm_GetSCache(&fid, NULL, &dscp, userp, &req);
790 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
791 (*ResultCB)->ResultStatus = status;
792 osi_Log2(afsd_logp, "RDR_EnumerateDirectory cm_GetSCache failure code=0x%x status=0x%x",
797 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
798 osi_Log0(afsd_logp, "RDR_EnumerateDirectory Object Name Invalid - Cell = 0");
802 /* get the directory size */
803 lock_ObtainWrite(&dscp->rw);
804 code = cm_SyncOp(dscp, NULL, userp, &req, PRSFS_LOOKUP,
805 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
807 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
808 (*ResultCB)->ResultStatus = status;
809 lock_ReleaseWrite(&dscp->rw);
810 cm_ReleaseSCache(dscp);
811 osi_Log2(afsd_logp, "RDR_EnumerateDirectory cm_SyncOp failure code=0x%x status=0x%x",
816 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
817 lock_ReleaseWrite(&dscp->rw);
819 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
820 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
821 cm_ReleaseSCache(dscp);
822 osi_Log1(afsd_logp, "RDR_EnumerateDirectory Not a Directory dscp=0x%p",
827 osi_Log1(afsd_logp, "RDR_EnumerateDirectory dv=%u", (afs_uint32)dscp->dataVersion);
830 * If there is no enumeration handle, then this is a new query
831 * and we must perform an enumeration for the specified object.
833 if (QueryCB->EnumHandle == (ULONG_PTR)NULL) {
836 code = cm_BeginDirOp(dscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
838 code = cm_BPlusDirEnumerate(dscp, userp, &req,
839 TRUE /* dir locked */, NULL /* no mask */,
840 TRUE /* fetch status? */, &enump);
842 osi_Log1(afsd_logp, "RDR_EnumerateDirectory cm_BPlusDirEnumerate failure code=0x%x",
847 osi_Log1(afsd_logp, "RDR_EnumerateDirectory cm_BeginDirOp failure code=0x%x",
851 enump = (cm_direnum_t *)QueryCB->EnumHandle;
855 if (ResultBufferLength == 0) {
856 code = cm_BPlusDirEnumBulkStat(enump);
858 osi_Log1(afsd_logp, "RDR_EnumerateDirectory cm_BPlusDirEnumBulkStat failure code=0x%x",
862 cm_direnum_entry_t * entryp = NULL;
864 pDirEnumResp->SnapshotDataVersion.QuadPart = enump->dataVersion;
867 if (dwMaxEntryLength < sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t)) {
868 osi_Log0(afsd_logp, "RDR_EnumerateDirectory out of space, returning");
872 code = cm_BPlusDirNextEnumEntry(enump, &entryp);
874 if ((code == 0 || code == CM_ERROR_STOPNOW) && entryp) {
875 cm_scache_t *scp = NULL;
876 int stopnow = (code == CM_ERROR_STOPNOW);
878 if ( !wcscmp(L".", entryp->name) || !wcscmp(L"..", entryp->name) ) {
879 osi_Log0(afsd_logp, "RDR_EnumerateDirectory skipping . or ..");
886 code = cm_GetSCache(&entryp->fid, &dscp->fid, &scp, userp, &req);
888 osi_Log5(afsd_logp, "RDR_EnumerateDirectory cm_GetSCache failure cell %u vol %u vnode %u uniq %u code=0x%x",
889 entryp->fid.cell, entryp->fid.volume, entryp->fid.vnode, entryp->fid.unique, code);
892 code = entryp->errorCode;
893 scp = code ? NULL : cm_FindSCache(&entryp->fid);
897 code = RDR_PopulateCurrentEntry( pCurrentEntry, dwMaxEntryLength,
898 dscp, scp, userp, &req,
900 cm_shortNames && cm_Is8Dot3(entryp->name) ? NULL : entryp->shortName,
901 (bWow64 ? RDR_POP_WOW64 : 0) |
902 (bSkipStatus ? RDR_POP_NO_GETSTATUS : 0),
904 &pCurrentEntry, &dwMaxEntryLength);
905 cm_ReleaseSCache(scp);
907 code = RDR_PopulateCurrentEntryNoScp( pCurrentEntry, dwMaxEntryLength,
908 dscp, &entryp->fid, userp, &req,
910 cm_shortNames && cm_Is8Dot3(entryp->name) ? NULL : entryp->shortName,
911 (bWow64 ? RDR_POP_WOW64 : 0),
913 &pCurrentEntry, &dwMaxEntryLength);
924 if (code || enump->next == enump->count || ResultBufferLength == 0) {
925 cm_BPlusDirFreeEnumeration(enump);
926 enump = (cm_direnum_t *)(ULONG_PTR)-1;
929 if (code == 0 || code == CM_ERROR_STOPNOW) {
930 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
931 osi_Log0(afsd_logp, "RDR_EnumerateDirectory SUCCESS");
933 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
934 (*ResultCB)->ResultStatus = status;
935 osi_Log2(afsd_logp, "RDR_EnumerateDirectory Failure code=0x%x status=0x%x",
939 if (ResultBufferLength) {
940 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwMaxEntryLength;
942 pDirEnumResp->EnumHandle = (ULONG_PTR) enump;
943 pDirEnumResp->CurrentDataVersion.QuadPart = dscp->dataVersion;
947 cm_ReleaseSCache(dscp);
953 RDR_EvaluateNodeByName( IN cm_user_t *userp,
954 IN AFSFileID ParentID,
955 IN WCHAR *FileNameCounted,
956 IN DWORD FileNameLength,
957 IN BOOL CaseSensitive,
961 IN DWORD ResultBufferLength,
962 IN OUT AFSCommResult **ResultCB)
964 AFSFileEvalResultCB *pEvalResultCB = NULL;
965 AFSDirEnumEntry * pCurrentEntry;
966 size_t size = ResultBufferLength ? sizeof(AFSCommResult) + ResultBufferLength - 1 : sizeof(AFSCommResult);
968 cm_scache_t * scp = NULL;
969 cm_scache_t * dscp = NULL;
974 WCHAR * wszName = NULL;
977 wchar_t FileName[260];
979 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
981 RDR_InitReq(&req, bWow64);
983 osi_Log4(afsd_logp, "RDR_EvaluateNodeByName parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
984 ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
986 /* Allocate enough room to add a volume prefix if necessary */
987 cbName = FileNameLength + (CM_PREFIX_VOL_CCH + 64) * sizeof(WCHAR);
988 wszName = malloc(cbName);
990 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName Out of Memory");
993 StringCbCopyNW(wszName, cbName, FileName, FileNameLength);
994 osi_Log1(afsd_logp, "... name=%S", osi_LogSaveStringW(afsd_logp, wszName));
996 *ResultCB = (AFSCommResult *)malloc(size);
998 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName Out of Memory");
1003 memset(*ResultCB, 0, size);
1004 (*ResultCB)->ResultBufferLength = 0;
1005 dwRemaining = ResultBufferLength;
1006 if (ResultBufferLength >= sizeof( AFSFileEvalResultCB)) {
1007 pEvalResultCB = (AFSFileEvalResultCB *)&(*ResultCB)->ResultData;
1008 pCurrentEntry = &pEvalResultCB->DirEnum;
1009 dwRemaining -= (sizeof( AFSFileEvalResultCB) - sizeof( AFSDirEnumEntry));
1012 if (ParentID.Cell != 0) {
1013 parentFid.cell = ParentID.Cell;
1014 parentFid.volume = ParentID.Volume;
1015 parentFid.vnode = ParentID.Vnode;
1016 parentFid.unique = ParentID.Unique;
1017 parentFid.hash = ParentID.Hash;
1019 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1021 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1022 (*ResultCB)->ResultStatus = status;
1023 if ( status == STATUS_INVALID_HANDLE)
1024 status = STATUS_OBJECT_PATH_INVALID;
1025 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName cm_GetSCache parentFID failure code=0x%x status=0x%x",
1031 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
1032 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName Object Name Invalid - Cell = 0");
1036 /* get the directory size */
1037 lock_ObtainWrite(&dscp->rw);
1038 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1039 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1041 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1042 (*ResultCB)->ResultStatus = status;
1043 lock_ReleaseWrite(&dscp->rw);
1044 cm_ReleaseSCache(dscp);
1045 osi_Log3(afsd_logp, "RDR_EvaluateNodeByName cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1046 dscp, code, status);
1050 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1051 lock_ReleaseWrite(&dscp->rw);
1053 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1054 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1055 cm_ReleaseSCache(dscp);
1056 osi_Log1(afsd_logp, "RDR_EvaluateNodeByName Not a Directory dscp=0x%p",
1062 code = cm_Lookup(dscp, wszName, CM_FLAG_CHECKPATH, userp, &req, &scp);
1064 if ((code == CM_ERROR_NOSUCHPATH || code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) &&
1065 dscp == cm_data.rootSCachep) {
1067 if (wcschr(wszName, '%') != NULL || wcschr(wszName, '#') != NULL) {
1069 * A volume reference: <cell>{%,#}<volume> -> @vol:<cell>{%,#}<volume>
1071 StringCchCopyNW(wszName, cbName, _C(CM_PREFIX_VOL), CM_PREFIX_VOL_CCH);
1072 StringCbCatNW(wszName, cbName, FileName, FileNameLength);
1075 code = cm_EvaluateVolumeReference(wszName, CM_FLAG_CHECKPATH, userp, &req, &scp);
1077 #ifdef AFS_FREELANCE_CLIENT
1078 else if (dscp->fid.cell == AFS_FAKE_ROOT_CELL_ID && dscp->fid.volume == AFS_FAKE_ROOT_VOL_ID &&
1079 dscp->fid.vnode == 1 && dscp->fid.unique == 1) {
1081 * If this is the Freelance volume root directory then treat unrecognized
1082 * names as cell names and attempt to find the appropriate "root.cell".
1084 StringCchCopyNW(wszName, cbName, _C(CM_PREFIX_VOL), CM_PREFIX_VOL_CCH);
1085 if (FileName[0] == L'.') {
1086 StringCbCatNW(wszName, cbName, &FileName[1], FileNameLength);
1087 StringCbCatNW(wszName, cbName, L"%", sizeof(WCHAR));
1089 StringCbCatNW(wszName, cbName, FileName, FileNameLength);
1090 StringCbCatNW(wszName, cbName, L"#", sizeof(WCHAR));
1092 StringCbCatNW(wszName, cbName, L"root.cell", 9 * sizeof(WCHAR));
1095 code = cm_EvaluateVolumeReference(wszName, CM_FLAG_CHECKPATH, userp, &req, &scp);
1100 if (code == 0 && scp) {
1101 wchar_t shortName[13]=L"";
1103 if (!cm_shortNames) {
1104 shortName[0] = L'\0';
1106 cm_Gen8Dot3VolNameW(scp->fid.cell, scp->fid.volume, shortName, NULL);
1107 } else if (!cm_Is8Dot3(wszName)) {
1110 dfid.vnode = htonl(scp->fid.vnode);
1111 dfid.unique = htonl(scp->fid.unique);
1113 cm_Gen8Dot3NameIntW(FileName, &dfid, shortName, NULL);
1115 shortName[0] = L'\0';
1118 code = RDR_PopulateCurrentEntry(pCurrentEntry, dwRemaining,
1119 dscp, scp, userp, &req,
1120 FileName, shortName,
1121 (bWow64 ? RDR_POP_WOW64 : 0) |
1122 (bNoFollow ? 0 : (RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS)),
1123 0, NULL, &dwRemaining);
1125 RDR_FlagScpInUse( scp, FALSE );
1126 cm_ReleaseSCache(scp);
1129 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1130 (*ResultCB)->ResultStatus = status;
1131 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName FAILURE code=0x%x status=0x%x",
1134 pEvalResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
1135 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
1136 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1137 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName SUCCESS");
1140 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1141 (*ResultCB)->ResultStatus = status;
1142 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName FAILURE code=0x%x status=0x%x",
1145 (*ResultCB)->ResultStatus = STATUS_NO_SUCH_FILE;
1146 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName No Such File");
1148 cm_ReleaseSCache(dscp);
1155 RDR_EvaluateNodeByID( IN cm_user_t *userp,
1156 IN AFSFileID ParentID, /* not used */
1157 IN AFSFileID SourceID,
1161 IN DWORD ResultBufferLength,
1162 IN OUT AFSCommResult **ResultCB)
1164 AFSFileEvalResultCB *pEvalResultCB = NULL;
1165 AFSDirEnumEntry * pCurrentEntry = NULL;
1166 size_t size = ResultBufferLength ? sizeof(AFSCommResult) + ResultBufferLength - 1 : sizeof(AFSCommResult);
1167 afs_uint32 code = 0;
1168 cm_scache_t * scp = NULL;
1169 cm_scache_t * dscp = NULL;
1176 osi_Log4(afsd_logp, "RDR_EvaluateNodeByID source FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1177 SourceID.Cell, SourceID.Volume, SourceID.Vnode, SourceID.Unique);
1178 osi_Log4(afsd_logp, "... parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1179 ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
1181 *ResultCB = (AFSCommResult *)malloc(size);
1183 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Out of Memory");
1187 memset(*ResultCB, 0, size);
1188 (*ResultCB)->ResultBufferLength = 0;
1189 dwRemaining = ResultBufferLength;
1190 if (ResultBufferLength >= sizeof( AFSFileEvalResultCB)) {
1191 pEvalResultCB = (AFSFileEvalResultCB *)&(*ResultCB)->ResultData;
1192 pCurrentEntry = &pEvalResultCB->DirEnum;
1193 dwRemaining -= (sizeof( AFSFileEvalResultCB) - sizeof( AFSDirEnumEntry));
1196 RDR_InitReq(&req, bWow64);
1198 if (SourceID.Cell != 0) {
1199 cm_SetFid(&Fid, SourceID.Cell, SourceID.Volume, SourceID.Vnode, SourceID.Unique);
1200 code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
1202 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1203 (*ResultCB)->ResultStatus = status;
1204 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache SourceFID failure code=0x%x status=0x%x",
1209 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
1210 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Object Name Invalid - Cell = 0");
1214 if (ParentID.Cell != 0) {
1215 cm_SetFid(&parentFid, ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
1216 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1218 cm_ReleaseSCache(scp);
1219 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1220 if ( status == STATUS_INVALID_HANDLE)
1221 status = STATUS_OBJECT_PATH_INVALID;
1222 (*ResultCB)->ResultStatus = status;
1223 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache parentFID failure code=0x%x status=0x%x",
1227 } else if (SourceID.Vnode == 1) {
1229 cm_HoldSCache(dscp);
1230 } else if (scp->parentVnode) {
1231 cm_SetFid(&parentFid, SourceID.Cell, SourceID.Volume, scp->parentVnode, scp->parentUnique);
1232 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1234 cm_ReleaseSCache(scp);
1235 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1236 if ( status == STATUS_INVALID_HANDLE)
1237 status = STATUS_OBJECT_PATH_INVALID;
1238 (*ResultCB)->ResultStatus = status;
1239 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache parentFID failure code=0x%x status=0x%x",
1244 (*ResultCB)->ResultStatus = STATUS_OBJECT_PATH_INVALID;
1245 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Object Path Invalid - Unknown Parent");
1249 /* Make sure the directory is current */
1250 lock_ObtainWrite(&dscp->rw);
1251 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1252 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1254 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1255 (*ResultCB)->ResultStatus = status;
1256 lock_ReleaseWrite(&dscp->rw);
1257 cm_ReleaseSCache(dscp);
1258 cm_ReleaseSCache(scp);
1259 osi_Log3(afsd_logp, "RDR_EvaluateNodeByID cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1260 dscp, code, status);
1264 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1265 lock_ReleaseWrite(&dscp->rw);
1267 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1268 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1269 cm_ReleaseSCache(dscp);
1270 cm_ReleaseSCache(scp);
1271 osi_Log1(afsd_logp, "RDR_EvaluateNodeByID Not a Directory dscp=0x%p", dscp);
1275 code = RDR_PopulateCurrentEntry(pCurrentEntry, dwRemaining,
1276 dscp, scp, userp, &req, NULL, NULL,
1277 (bWow64 ? RDR_POP_WOW64 : 0) |
1278 (bNoFollow ? 0 : (RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS)),
1279 0, NULL, &dwRemaining);
1282 RDR_FlagScpInUse( scp, FALSE );
1283 cm_ReleaseSCache(scp);
1284 cm_ReleaseSCache(dscp);
1287 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1288 (*ResultCB)->ResultStatus = status;
1289 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID FAILURE code=0x%x status=0x%x",
1292 pEvalResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
1294 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
1295 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1296 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID SUCCESS");
1302 RDR_CreateFileEntry( IN cm_user_t *userp,
1303 IN WCHAR *FileNameCounted,
1304 IN DWORD FileNameLength,
1305 IN AFSFileCreateCB *CreateCB,
1308 IN DWORD ResultBufferLength,
1309 IN OUT AFSCommResult **ResultCB)
1311 AFSFileCreateResultCB *pResultCB = NULL;
1312 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1315 cm_scache_t * dscp = NULL;
1316 afs_uint32 flags = 0;
1318 cm_scache_t * scp = NULL;
1321 wchar_t FileName[260];
1323 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
1325 osi_Log4(afsd_logp, "RDR_CreateFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1326 CreateCB->ParentId.Cell, CreateCB->ParentId.Volume,
1327 CreateCB->ParentId.Vnode, CreateCB->ParentId.Unique);
1328 osi_Log1(afsd_logp, "... name=%S", osi_LogSaveStringW(afsd_logp, FileName));
1330 RDR_InitReq(&req, bWow64);
1331 memset(&setAttr, 0, sizeof(cm_attr_t));
1333 *ResultCB = (AFSCommResult *)malloc(size);
1335 osi_Log0(afsd_logp, "RDR_CreateFileEntry out of memory");
1343 parentFid.cell = CreateCB->ParentId.Cell;
1344 parentFid.volume = CreateCB->ParentId.Volume;
1345 parentFid.vnode = CreateCB->ParentId.Vnode;
1346 parentFid.unique = CreateCB->ParentId.Unique;
1347 parentFid.hash = CreateCB->ParentId.Hash;
1349 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1351 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1352 (*ResultCB)->ResultStatus = status;
1353 if ( status == STATUS_INVALID_HANDLE)
1354 status = STATUS_OBJECT_PATH_INVALID;
1355 osi_Log2(afsd_logp, "RDR_CreateFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1360 lock_ObtainWrite(&dscp->rw);
1361 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1362 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1364 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1365 (*ResultCB)->ResultStatus = status;
1366 lock_ReleaseWrite(&dscp->rw);
1367 cm_ReleaseSCache(dscp);
1368 osi_Log3(afsd_logp, "RDR_CreateFileEntry cm_SyncOp failure (1) dscp=0x%p code=0x%x status=0x%x",
1369 dscp, code, status);
1373 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1374 lock_ReleaseWrite(&dscp->rw);
1376 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1377 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1378 cm_ReleaseSCache(dscp);
1379 osi_Log1(afsd_logp, "RDR_CreateFileEntry Not a Directory dscp=0x%p",
1384 /* Use current time */
1385 setAttr.mask = CM_ATTRMASK_CLIENTMODTIME;
1386 setAttr.clientModTime = time(NULL);
1388 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1389 if (smb_unixModeDefaultDir) {
1390 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1391 setAttr.unixModeBits = smb_unixModeDefaultDir;
1392 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)
1393 setAttr.unixModeBits &= ~0222; /* disable the write bits */
1396 code = cm_MakeDir(dscp, FileName, flags, &setAttr, userp, &req, &scp);
1398 if (smb_unixModeDefaultFile) {
1399 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1400 setAttr.unixModeBits = smb_unixModeDefaultFile;
1401 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)
1402 setAttr.unixModeBits &= ~0222; /* disable the write bits */
1405 setAttr.mask |= CM_ATTRMASK_LENGTH;
1406 setAttr.length.LowPart = CreateCB->AllocationSize.LowPart;
1407 setAttr.length.HighPart = CreateCB->AllocationSize.HighPart;
1408 code = cm_Create(dscp, FileName, flags, &setAttr, &scp, userp, &req);
1411 wchar_t shortName[13]=L"";
1415 (*ResultCB)->ResultStatus = 0; // We will be able to fit all the data in here
1417 (*ResultCB)->ResultBufferLength = sizeof( AFSFileCreateResultCB);
1419 pResultCB = (AFSFileCreateResultCB *)(*ResultCB)->ResultData;
1421 dwRemaining = ResultBufferLength - sizeof( AFSFileCreateResultCB) + sizeof( AFSDirEnumEntry);
1423 lock_ObtainWrite(&dscp->rw);
1424 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1425 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1427 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1428 (*ResultCB)->ResultStatus = status;
1429 lock_ReleaseWrite(&dscp->rw);
1430 cm_ReleaseSCache(dscp);
1431 cm_ReleaseSCache(scp);
1432 osi_Log3(afsd_logp, "RDR_CreateFileEntry cm_SyncOp failure (2) dscp=0x%p code=0x%x status=0x%x",
1433 dscp, code, status);
1437 pResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
1439 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1440 lock_ReleaseWrite(&dscp->rw);
1442 if (cm_shortNames) {
1443 dfid.vnode = htonl(scp->fid.vnode);
1444 dfid.unique = htonl(scp->fid.unique);
1446 if (!cm_Is8Dot3(FileName))
1447 cm_Gen8Dot3NameIntW(FileName, &dfid, shortName, NULL);
1449 shortName[0] = '\0';
1452 code = RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
1453 dscp, scp, userp, &req, FileName, shortName,
1454 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
1455 0, NULL, &dwRemaining);
1458 RDR_FlagScpInUse( scp, FALSE );
1459 cm_ReleaseSCache(scp);
1460 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1461 osi_Log0(afsd_logp, "RDR_CreateFileEntry SUCCESS");
1463 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1464 (*ResultCB)->ResultStatus = status;
1465 (*ResultCB)->ResultBufferLength = 0;
1466 osi_Log2(afsd_logp, "RDR_CreateFileEntry FAILURE code=0x%x status=0x%x",
1470 cm_ReleaseSCache(dscp);
1476 RDR_UpdateFileEntry( IN cm_user_t *userp,
1477 IN AFSFileID FileId,
1478 IN AFSFileUpdateCB *UpdateCB,
1480 IN DWORD ResultBufferLength,
1481 IN OUT AFSCommResult **ResultCB)
1483 AFSFileUpdateResultCB *pResultCB = NULL;
1484 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1488 afs_uint32 flags = 0;
1490 cm_scache_t * scp = NULL;
1491 cm_scache_t * dscp = NULL;
1493 time_t clientModTime;
1496 BOOL bScpLocked = FALSE;
1498 RDR_InitReq(&req, bWow64);
1499 memset(&setAttr, 0, sizeof(cm_attr_t));
1501 osi_Log4(afsd_logp, "RDR_UpdateFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1502 UpdateCB->ParentId.Cell, UpdateCB->ParentId.Volume,
1503 UpdateCB->ParentId.Vnode, UpdateCB->ParentId.Unique);
1504 osi_Log4(afsd_logp, "... object FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1505 FileId.Cell, FileId.Volume,
1506 FileId.Vnode, FileId.Unique);
1508 *ResultCB = (AFSCommResult *)malloc( size);
1510 osi_Log0(afsd_logp, "RDR_UpdateFileEntry Out of Memory");
1518 parentFid.cell = UpdateCB->ParentId.Cell;
1519 parentFid.volume = UpdateCB->ParentId.Volume;
1520 parentFid.vnode = UpdateCB->ParentId.Vnode;
1521 parentFid.unique = UpdateCB->ParentId.Unique;
1522 parentFid.hash = UpdateCB->ParentId.Hash;
1524 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1526 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1527 (*ResultCB)->ResultStatus = status;
1528 if ( status == STATUS_INVALID_HANDLE)
1529 status = STATUS_OBJECT_PATH_INVALID;
1530 osi_Log2(afsd_logp, "RDR_UpdateFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1535 lock_ObtainWrite(&dscp->rw);
1537 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1538 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1540 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1541 (*ResultCB)->ResultStatus = status;
1542 lock_ReleaseWrite(&dscp->rw);
1543 cm_ReleaseSCache(dscp);
1544 osi_Log3(afsd_logp, "RDR_UpdateFileEntry cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1545 dscp, code, status);
1549 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1550 lock_ReleaseWrite(&dscp->rw);
1553 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1554 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1555 cm_ReleaseSCache(dscp);
1556 osi_Log1(afsd_logp, "RDR_UpdateFileEntry Not a Directory dscp=0x%p",
1561 Fid.cell = FileId.Cell;
1562 Fid.volume = FileId.Volume;
1563 Fid.vnode = FileId.Vnode;
1564 Fid.unique = FileId.Unique;
1565 Fid.hash = FileId.Hash;
1567 code = cm_GetSCache(&Fid, &dscp->fid, &scp, userp, &req);
1569 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1570 (*ResultCB)->ResultStatus = status;
1571 cm_ReleaseSCache(dscp);
1572 osi_Log2(afsd_logp, "RDR_UpdateFileEntry cm_GetSCache object FID failure code=0x%x status=0x%x",
1577 lock_ObtainWrite(&scp->rw);
1579 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1580 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1582 lock_ReleaseWrite(&scp->rw);
1583 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1584 (*ResultCB)->ResultStatus = status;
1585 (*ResultCB)->ResultBufferLength = 0;
1586 cm_ReleaseSCache(dscp);
1587 cm_ReleaseSCache(scp);
1588 osi_Log3(afsd_logp, "RDR_UpdateFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
1592 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1594 if (UpdateCB->ChangeTime.QuadPart) {
1596 if (scp->fileType == CM_SCACHETYPE_FILE) {
1597 /* Do not set length and other attributes at the same time */
1598 if (scp->length.QuadPart != UpdateCB->AllocationSize.QuadPart) {
1599 osi_Log2(afsd_logp, "RDR_UpdateFileEntry Length Change 0x%x -> 0x%x",
1600 (afs_uint32)scp->length.QuadPart, (afs_uint32)UpdateCB->AllocationSize.QuadPart);
1601 setAttr.mask |= CM_ATTRMASK_LENGTH;
1602 setAttr.length.LowPart = UpdateCB->AllocationSize.LowPart;
1603 setAttr.length.HighPart = UpdateCB->AllocationSize.HighPart;
1604 lock_ReleaseWrite(&scp->rw);
1606 code = cm_SetAttr(scp, &setAttr, userp, &req);
1614 lock_ObtainWrite(&scp->rw);
1617 if ((scp->unixModeBits & 0200) && (UpdateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1618 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1619 setAttr.unixModeBits = scp->unixModeBits & ~0222;
1620 } else if (!(scp->unixModeBits & 0200) && !(UpdateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1621 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1622 setAttr.unixModeBits = scp->unixModeBits | 0222;
1626 if (UpdateCB->LastWriteTime.QuadPart) {
1627 ft.dwLowDateTime = UpdateCB->LastWriteTime.LowPart;
1628 ft.dwHighDateTime = UpdateCB->LastWriteTime.HighPart;
1630 cm_UnixTimeFromLargeSearchTime(& clientModTime, &ft);
1633 lock_ObtainWrite(&scp->rw);
1636 if (scp->clientModTime != clientModTime) {
1637 setAttr.mask |= CM_ATTRMASK_CLIENTMODTIME;
1638 setAttr.clientModTime = clientModTime;
1643 lock_ReleaseWrite(&scp->rw);
1645 code = cm_SetAttr(scp, &setAttr, userp, &req);
1652 lock_ReleaseWrite(&scp->rw);
1656 DWORD dwRemaining = ResultBufferLength - sizeof( AFSFileUpdateResultCB) + sizeof( AFSDirEnumEntry);
1658 pResultCB = (AFSFileUpdateResultCB *)(*ResultCB)->ResultData;
1660 pResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
1662 code = RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
1663 dscp, scp, userp, &req, NULL, NULL,
1664 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
1665 0, NULL, &dwRemaining);
1666 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1667 osi_Log0(afsd_logp, "RDR_UpdateFileEntry SUCCESS");
1669 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1670 (*ResultCB)->ResultStatus = status;
1671 (*ResultCB)->ResultBufferLength = 0;
1672 osi_Log2(afsd_logp, "RDR_UpdateFileEntry FAILURE code=0x%x status=0x%x",
1675 cm_ReleaseSCache(scp);
1676 cm_ReleaseSCache(dscp);
1682 RDR_CleanupFileEntry( IN cm_user_t *userp,
1683 IN AFSFileID FileId,
1684 IN WCHAR *FileNameCounted,
1685 IN DWORD FileNameLength,
1686 IN AFSFileCleanupCB *CleanupCB,
1688 IN BOOL bLastHandle,
1689 IN BOOL bDeleteFile,
1690 IN BOOL bUnlockFile,
1691 IN DWORD ResultBufferLength,
1692 IN OUT AFSCommResult **ResultCB)
1694 AFSFileCleanupResultCB *pResultCB = NULL;
1695 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1698 afs_uint32 code = 0;
1699 afs_uint32 flags = 0;
1701 cm_scache_t * scp = NULL;
1702 cm_scache_t * dscp = NULL;
1704 time_t clientModTime;
1707 BOOL bScpLocked = FALSE;
1708 BOOL bDscpLocked = FALSE;
1709 BOOL bFlushFile = FALSE;
1712 RDR_InitReq(&req, bWow64);
1713 memset(&setAttr, 0, sizeof(cm_attr_t));
1715 osi_Log4(afsd_logp, "RDR_CleanupFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1716 CleanupCB->ParentId.Cell, CleanupCB->ParentId.Volume,
1717 CleanupCB->ParentId.Vnode, CleanupCB->ParentId.Unique);
1718 osi_Log4(afsd_logp, "... object FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1719 FileId.Cell, FileId.Volume,
1720 FileId.Vnode, FileId.Unique);
1722 *ResultCB = (AFSCommResult *)malloc( size);
1724 osi_Log0(afsd_logp, "RDR_CleanupFileEntry Out of Memory");
1732 parentFid.cell = CleanupCB->ParentId.Cell;
1733 parentFid.volume = CleanupCB->ParentId.Volume;
1734 parentFid.vnode = CleanupCB->ParentId.Vnode;
1735 parentFid.unique = CleanupCB->ParentId.Unique;
1736 parentFid.hash = CleanupCB->ParentId.Hash;
1738 if (parentFid.cell) {
1739 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1741 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1742 if ( status == STATUS_INVALID_HANDLE)
1743 status = STATUS_OBJECT_PATH_INVALID;
1744 (*ResultCB)->ResultStatus = status;
1745 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1750 lock_ObtainWrite(&dscp->rw);
1752 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1753 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1755 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp failure dscp=0x%p code=0x%x",
1761 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1762 lock_ReleaseWrite(&dscp->rw);
1763 bDscpLocked = FALSE;
1765 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1766 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1767 cm_ReleaseSCache(dscp);
1768 osi_Log1(afsd_logp, "RDR_CleanupFileEntry Not a Directory dscp=0x%p",
1775 Fid.cell = FileId.Cell;
1776 Fid.volume = FileId.Volume;
1777 Fid.vnode = FileId.Vnode;
1778 Fid.unique = FileId.Unique;
1779 Fid.hash = FileId.Hash;
1781 code = cm_GetSCache(&Fid, dscp ? &dscp->fid : NULL, &scp, userp, &req);
1783 osi_Log1(afsd_logp, "RDR_CleanupFileEntry cm_GetSCache object FID failure code=0x%x",
1788 lock_ObtainWrite(&scp->rw);
1790 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1791 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1793 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp failure scp=0x%p code=0x%x",
1797 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1799 if (bLastHandle && (scp->fileType == CM_SCACHETYPE_FILE) &&
1800 scp->redirBufCount > 0)
1802 LARGE_INTEGER heldExtents;
1803 AFSFileExtentCB extentList[1024];
1804 DWORD extentCount = 0;
1809 heldExtents.QuadPart = 0;
1811 for ( srbp = redirq_to_cm_buf_t(scp->redirQueueT);
1813 srbp = redirq_to_cm_buf_t(osi_QPrev(&srbp->redirq)))
1815 extentList[extentCount].Flags = 0;
1816 extentList[extentCount].Length = cm_data.blockSize;
1817 extentList[extentCount].FileOffset.QuadPart = srbp->offset.QuadPart;
1818 extentList[extentCount].CacheOffset.QuadPart = srbp->datap - RDR_extentBaseAddress;
1819 lock_ObtainWrite(&buf_globalLock);
1820 srbp->redirReleaseRequested = now;
1821 lock_ReleaseWrite(&buf_globalLock);
1824 if (extentCount == 1024) {
1825 lock_ReleaseWrite(&scp->rw);
1826 code = RDR_RequestExtentRelease(&scp->fid, heldExtents, extentCount, extentList);
1828 if (code == CM_ERROR_RETRY) {
1830 * The redirector either is not holding the extents or cannot let them
1831 * go because they are otherwise in use. At the moment, do nothing.
1838 lock_ObtainWrite(&scp->rw);
1842 if (code == 0 && extentCount > 0) {
1844 lock_ReleaseWrite(&scp->rw);
1847 code = RDR_RequestExtentRelease(&scp->fid, heldExtents, extentCount, extentList);
1852 /* No longer in use by redirector */
1854 lock_ObtainWrite(&scp->rw);
1859 lock_AssertWrite(&scp->rw);
1860 scp->flags &= ~CM_SCACHEFLAG_RDR_IN_USE;
1863 /* If not a readonly object, flush dirty data and update metadata */
1864 if (!(scp->flags & CM_SCACHEFLAG_RO)) {
1865 if ((scp->fileType == CM_SCACHETYPE_FILE) && (bLastHandle || bFlushFile)) {
1866 /* Serialize with any outstanding AsyncStore operation */
1867 code = cm_SyncOp(scp, NULL, userp, &req, 0, CM_SCACHESYNC_ASYNCSTORE);
1870 lock_ReleaseWrite(&scp->rw);
1874 code = cm_FSync(scp, userp, &req, bScpLocked);
1876 if (bLastHandle && code)
1880 if (CleanupCB->ChangeTime.QuadPart) {
1882 if (scp->fileType == CM_SCACHETYPE_FILE) {
1883 /* Do not set length and other attributes at the same time */
1884 if (scp->length.QuadPart != CleanupCB->AllocationSize.QuadPart) {
1885 osi_Log2(afsd_logp, "RDR_CleanupFileEntry Length Change 0x%x -> 0x%x",
1886 (afs_uint32)scp->length.QuadPart, (afs_uint32)CleanupCB->AllocationSize.QuadPart);
1887 setAttr.mask |= CM_ATTRMASK_LENGTH;
1888 setAttr.length.LowPart = CleanupCB->AllocationSize.LowPart;
1889 setAttr.length.HighPart = CleanupCB->AllocationSize.HighPart;
1892 lock_ReleaseWrite(&scp->rw);
1895 code = cm_SetAttr(scp, &setAttr, userp, &req);
1903 lock_ObtainWrite(&scp->rw);
1907 if ((scp->unixModeBits & 0200) && (CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1908 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1909 setAttr.unixModeBits = scp->unixModeBits & ~0222;
1910 } else if (!(scp->unixModeBits & 0200) && !(CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1911 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1912 setAttr.unixModeBits = scp->unixModeBits | 0222;
1916 if (CleanupCB->LastWriteTime.QuadPart) {
1917 ft.dwLowDateTime = CleanupCB->LastWriteTime.LowPart;
1918 ft.dwHighDateTime = CleanupCB->LastWriteTime.HighPart;
1920 cm_UnixTimeFromLargeSearchTime(&clientModTime, &ft);
1921 if (scp->clientModTime != clientModTime) {
1922 setAttr.mask |= CM_ATTRMASK_CLIENTMODTIME;
1923 setAttr.clientModTime = clientModTime;
1930 lock_ReleaseWrite(&scp->rw);
1933 code = cm_SetAttr(scp, &setAttr, userp, &req);
1939 /* Now drop the lock enforcing the share access */
1940 if ( CleanupCB->FileAccess != AFS_FILE_ACCESS_NOLOCK) {
1941 unsigned int sLockType;
1942 LARGE_INTEGER LOffset, LLength;
1944 if (CleanupCB->FileAccess == AFS_FILE_ACCESS_SHARED)
1945 sLockType = LOCKING_ANDX_SHARED_LOCK;
1949 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, CleanupCB->Identifier);
1951 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
1952 LOffset.LowPart = SMB_FID_QLOCK_LOW;
1953 LLength.HighPart = 0;
1954 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
1957 lock_ObtainWrite(&scp->rw);
1961 code = cm_SyncOp(scp, NULL, userp, &req, 0, CM_SCACHESYNC_LOCK);
1964 code = cm_Unlock(scp, sLockType, LOffset, LLength, key, 0, userp, &req);
1966 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
1968 if (code == CM_ERROR_RANGE_NOT_LOCKED)
1970 osi_Log3(afsd_logp, "RDR_CleanupFileEntry Range Not Locked -- FileAccess 0x%x ProcessId 0x%x HandleId 0x%x",
1971 CleanupCB->FileAccess, CleanupCB->ProcessId, CleanupCB->Identifier);
1977 if (bUnlockFile || bDeleteFile) {
1979 lock_ObtainWrite(&scp->rw);
1982 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1983 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
1985 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp (2) failure scp=0x%p code=0x%x",
1990 key = cm_GenerateKey(CM_SESSION_IFS, CleanupCB->ProcessId, 0);
1992 /* the scp is now locked and current */
1993 code = cm_UnlockByKey(scp, key,
1994 bDeleteFile ? CM_UNLOCK_FLAG_BY_FID : 0,
1997 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
2005 lock_ReleaseWrite(&dscp->rw);
2007 lock_ReleaseWrite(&scp->rw);
2009 if (code == 0 && dscp && bDeleteFile) {
2010 WCHAR FileName[260];
2012 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
2014 if (scp->fileType == CM_SCACHETYPE_DIRECTORY)
2015 code = cm_RemoveDir(dscp, NULL, FileName, userp, &req);
2017 code = cm_Unlink(dscp, NULL, FileName, userp, &req);
2021 if ( ResultBufferLength >= sizeof( AFSFileCleanupResultCB))
2023 (*ResultCB)->ResultBufferLength = sizeof( AFSFileCleanupResultCB);
2024 pResultCB = (AFSFileCleanupResultCB *)&(*ResultCB)->ResultData;
2025 pResultCB->ParentDataVersion.QuadPart = dscp ? dscp->dataVersion : 0;
2027 (*ResultCB)->ResultBufferLength = 0;
2030 (*ResultCB)->ResultStatus = 0;
2031 osi_Log0(afsd_logp, "RDR_CleanupFileEntry SUCCESS");
2033 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2034 (*ResultCB)->ResultStatus = status;
2035 osi_Log2(afsd_logp, "RDR_CleanupFileEntry FAILURE code=0x%x status=0x%x",
2040 cm_ReleaseSCache(scp);
2042 cm_ReleaseSCache(dscp);
2048 RDR_DeleteFileEntry( IN cm_user_t *userp,
2049 IN AFSFileID ParentId,
2050 IN ULONGLONG ProcessId,
2051 IN WCHAR *FileNameCounted,
2052 IN DWORD FileNameLength,
2055 IN DWORD ResultBufferLength,
2056 IN OUT AFSCommResult **ResultCB)
2059 AFSFileDeleteResultCB *pResultCB = NULL;
2060 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
2063 cm_scache_t * dscp = NULL;
2064 cm_scache_t * scp = NULL;
2065 afs_uint32 flags = 0;
2069 wchar_t FileName[260];
2072 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
2074 osi_Log4(afsd_logp, "RDR_DeleteFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2075 ParentId.Cell, ParentId.Volume,
2076 ParentId.Vnode, ParentId.Unique);
2077 osi_Log2(afsd_logp, "... name=%S checkOnly=%x",
2078 osi_LogSaveStringW(afsd_logp, FileName),
2081 RDR_InitReq(&req, bWow64);
2082 memset(&setAttr, 0, sizeof(cm_attr_t));
2084 *ResultCB = (AFSCommResult *)malloc( size);
2086 osi_Log0(afsd_logp, "RDR_DeleteFileEntry out of memory");
2094 parentFid.cell = ParentId.Cell;
2095 parentFid.volume = ParentId.Volume;
2096 parentFid.vnode = ParentId.Vnode;
2097 parentFid.unique = ParentId.Unique;
2098 parentFid.hash = ParentId.Hash;
2100 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
2102 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2103 if ( status == STATUS_INVALID_HANDLE)
2104 status = STATUS_OBJECT_PATH_INVALID;
2105 (*ResultCB)->ResultStatus = status;
2106 osi_Log2(afsd_logp, "RDR_DeleteFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
2111 lock_ObtainWrite(&dscp->rw);
2113 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
2114 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2116 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2117 (*ResultCB)->ResultStatus = status;
2118 (*ResultCB)->ResultBufferLength = 0;
2119 lock_ReleaseWrite(&dscp->rw);
2120 cm_ReleaseSCache(dscp);
2121 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
2122 dscp, code, status);
2126 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2127 lock_ReleaseWrite(&dscp->rw);
2129 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2130 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2131 cm_ReleaseSCache(dscp);
2132 osi_Log1(afsd_logp, "RDR_DeleteFileEntry Not a Directory dscp=0x%p",
2137 code = cm_Lookup(dscp, FileName, 0, userp, &req, &scp);
2139 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2140 (*ResultCB)->ResultStatus = status;
2141 (*ResultCB)->ResultBufferLength = 0;
2142 cm_ReleaseSCache(dscp);
2143 osi_Log2(afsd_logp, "RDR_DeleteFileEntry cm_Lookup failure code=0x%x status=0x%x",
2148 lock_ObtainWrite(&scp->rw);
2149 code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_DELETE,
2150 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2152 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2153 (*ResultCB)->ResultStatus = status;
2154 (*ResultCB)->ResultBufferLength = 0;
2155 lock_ReleaseWrite(&scp->rw);
2156 cm_ReleaseSCache(scp);
2157 cm_ReleaseSCache(dscp);
2158 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2164 /* Drop all locks since the file is being deleted */
2165 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2166 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
2168 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2169 (*ResultCB)->ResultStatus = status;
2170 (*ResultCB)->ResultBufferLength = 0;
2171 lock_ReleaseWrite(&scp->rw);
2172 cm_ReleaseSCache(scp);
2173 cm_ReleaseSCache(dscp);
2174 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp Lock failure scp=0x%p code=0x%x status=0x%x",
2178 /* the scp is now locked and current */
2179 key = cm_GenerateKey(CM_SESSION_IFS, ProcessId, 0);
2181 code = cm_UnlockByKey(scp, key,
2182 CM_UNLOCK_FLAG_BY_FID,
2185 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
2186 lock_ReleaseWrite(&scp->rw);
2188 if (scp->fileType == CM_SCACHETYPE_DIRECTORY)
2189 code = cm_RemoveDir(dscp, NULL, FileName, userp, &req);
2191 code = cm_Unlink(dscp, NULL, FileName, userp, &req);
2193 lock_ReleaseWrite(&scp->rw);
2197 (*ResultCB)->ResultStatus = 0; // We will be able to fit all the data in here
2199 (*ResultCB)->ResultBufferLength = sizeof( AFSFileDeleteResultCB);
2201 pResultCB = (AFSFileDeleteResultCB *)(*ResultCB)->ResultData;
2203 pResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
2204 osi_Log0(afsd_logp, "RDR_DeleteFileEntry SUCCESS");
2206 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2207 (*ResultCB)->ResultStatus = status;
2208 (*ResultCB)->ResultBufferLength = 0;
2209 osi_Log2(afsd_logp, "RDR_DeleteFileEntry FAILURE code=0x%x status=0x%x",
2213 cm_ReleaseSCache(dscp);
2214 cm_ReleaseSCache(scp);
2220 RDR_RenameFileEntry( IN cm_user_t *userp,
2221 IN WCHAR *SourceFileNameCounted,
2222 IN DWORD SourceFileNameLength,
2223 IN AFSFileID SourceFileId,
2224 IN AFSFileRenameCB *pRenameCB,
2226 IN DWORD ResultBufferLength,
2227 IN OUT AFSCommResult **ResultCB)
2230 AFSFileRenameResultCB *pResultCB = NULL;
2231 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
2232 AFSFileID SourceParentId = pRenameCB->SourceParentId;
2233 AFSFileID TargetParentId = pRenameCB->TargetParentId;
2234 WCHAR * TargetFileNameCounted = pRenameCB->TargetName;
2235 DWORD TargetFileNameLength = pRenameCB->TargetNameLength;
2236 cm_fid_t SourceParentFid;
2237 cm_fid_t TargetParentFid;
2239 cm_fid_t OrigTargetFid = {0,0,0,0,0};
2241 cm_scache_t * oldDscp;
2242 cm_scache_t * newDscp;
2244 wchar_t shortName[13];
2245 wchar_t SourceFileName[260];
2246 wchar_t TargetFileName[260];
2252 RDR_InitReq(&req, bWow64);
2254 StringCchCopyNW(SourceFileName, 260, SourceFileNameCounted, SourceFileNameLength / sizeof(WCHAR));
2255 StringCchCopyNW(TargetFileName, 260, TargetFileNameCounted, TargetFileNameLength / sizeof(WCHAR));
2257 osi_Log4(afsd_logp, "RDR_RenameFileEntry Source Parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2258 SourceParentId.Cell, SourceParentId.Volume,
2259 SourceParentId.Vnode, SourceParentId.Unique);
2260 osi_Log2(afsd_logp, "... Source Name=%S Length %u", osi_LogSaveStringW(afsd_logp, SourceFileName), SourceFileNameLength);
2261 osi_Log4(afsd_logp, "... Target Parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2262 TargetParentId.Cell, TargetParentId.Volume,
2263 TargetParentId.Vnode, TargetParentId.Unique);
2264 osi_Log2(afsd_logp, "... Target Name=%S Length %u", osi_LogSaveStringW(afsd_logp, TargetFileName), TargetFileNameLength);
2266 *ResultCB = (AFSCommResult *)malloc( size);
2274 pResultCB = (AFSFileRenameResultCB *)(*ResultCB)->ResultData;
2276 if (SourceFileNameLength == 0 || TargetFileNameLength == 0)
2278 osi_Log2(afsd_logp, "RDR_RenameFileEntry Invalid Name Length: src %u target %u",
2279 SourceFileNameLength, TargetFileNameLength);
2280 (*ResultCB)->ResultStatus = STATUS_INVALID_PARAMETER;
2284 SourceParentFid.cell = SourceParentId.Cell;
2285 SourceParentFid.volume = SourceParentId.Volume;
2286 SourceParentFid.vnode = SourceParentId.Vnode;
2287 SourceParentFid.unique = SourceParentId.Unique;
2288 SourceParentFid.hash = SourceParentId.Hash;
2290 TargetParentFid.cell = TargetParentId.Cell;
2291 TargetParentFid.volume = TargetParentId.Volume;
2292 TargetParentFid.vnode = TargetParentId.Vnode;
2293 TargetParentFid.unique = TargetParentId.Unique;
2294 TargetParentFid.hash = TargetParentId.Hash;
2296 code = cm_GetSCache(&SourceParentFid, NULL, &oldDscp, userp, &req);
2298 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache source parent failed code 0x%x", code);
2299 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2300 if ( status == STATUS_INVALID_HANDLE)
2301 status = STATUS_OBJECT_PATH_INVALID;
2302 (*ResultCB)->ResultStatus = status;
2306 lock_ObtainWrite(&oldDscp->rw);
2307 code = cm_SyncOp(oldDscp, NULL, userp, &req, 0,
2308 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2310 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp oldDscp 0x%p failed code 0x%x", oldDscp, code);
2311 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2312 if ( status == STATUS_INVALID_HANDLE)
2313 status = STATUS_OBJECT_PATH_INVALID;
2314 (*ResultCB)->ResultStatus = status;
2315 lock_ReleaseWrite(&oldDscp->rw);
2316 cm_ReleaseSCache(oldDscp);
2320 cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2321 lock_ReleaseWrite(&oldDscp->rw);
2324 if (oldDscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2325 osi_Log1(afsd_logp, "RDR_RenameFileEntry oldDscp 0x%p not a directory", oldDscp);
2326 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2327 cm_ReleaseSCache(oldDscp);
2331 code = cm_GetSCache(&TargetParentFid, NULL, &newDscp, userp, &req);
2333 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache target parent failed code 0x%x", code);
2334 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2335 (*ResultCB)->ResultStatus = status;
2336 cm_ReleaseSCache(oldDscp);
2340 lock_ObtainWrite(&newDscp->rw);
2341 code = cm_SyncOp(newDscp, NULL, userp, &req, 0,
2342 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2344 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp newDscp 0x%p failed code 0x%x", newDscp, code);
2345 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2346 (*ResultCB)->ResultStatus = status;
2347 lock_ReleaseWrite(&newDscp->rw);
2348 cm_ReleaseSCache(oldDscp);
2349 cm_ReleaseSCache(newDscp);
2353 cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2354 lock_ReleaseWrite(&newDscp->rw);
2357 if (newDscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2358 osi_Log1(afsd_logp, "RDR_RenameFileEntry newDscp 0x%p not a directory", newDscp);
2359 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2360 cm_ReleaseSCache(oldDscp);
2361 cm_ReleaseSCache(newDscp);
2365 /* Obtain the original FID just for debugging purposes */
2366 code = cm_BeginDirOp( oldDscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
2368 code = cm_BPlusDirLookup(&dirop, SourceFileName, &SourceFid);
2369 code = cm_BPlusDirLookup(&dirop, TargetFileName, &OrigTargetFid);
2370 cm_EndDirOp(&dirop);
2373 code = cm_Rename( oldDscp, NULL, SourceFileName,
2374 newDscp, TargetFileName, userp, &req);
2376 cm_scache_t *scp = 0;
2379 (*ResultCB)->ResultBufferLength = ResultBufferLength;
2380 dwRemaining = ResultBufferLength - sizeof( AFSFileRenameResultCB) + sizeof( AFSDirEnumEntry);
2381 (*ResultCB)->ResultStatus = 0;
2383 pResultCB->SourceParentDataVersion.QuadPart = oldDscp->dataVersion;
2384 pResultCB->TargetParentDataVersion.QuadPart = newDscp->dataVersion;
2386 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_Rename oldDscp 0x%p newDscp 0x%p SUCCESS",
2389 code = cm_BeginDirOp( newDscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
2391 code = cm_BPlusDirLookup(&dirop, TargetFileName, &TargetFid);
2392 cm_EndDirOp(&dirop);
2396 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_BPlusDirLookup failed code 0x%x",
2398 (*ResultCB)->ResultStatus = STATUS_OBJECT_PATH_INVALID;
2399 cm_ReleaseSCache(oldDscp);
2400 cm_ReleaseSCache(newDscp);
2404 osi_Log4(afsd_logp, "RDR_RenameFileEntry Target FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2405 TargetFid.cell, TargetFid.volume,
2406 TargetFid.vnode, TargetFid.unique);
2408 code = cm_GetSCache(&TargetFid, &newDscp->fid, &scp, userp, &req);
2410 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache target failed code 0x%x", code);
2411 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2412 (*ResultCB)->ResultStatus = status;
2413 cm_ReleaseSCache(oldDscp);
2414 cm_ReleaseSCache(newDscp);
2418 /* Make sure the source vnode is current */
2419 lock_ObtainWrite(&scp->rw);
2420 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2421 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2423 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp scp 0x%p failed code 0x%x", scp, code);
2424 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2425 (*ResultCB)->ResultStatus = status;
2426 lock_ReleaseWrite(&scp->rw);
2427 cm_ReleaseSCache(oldDscp);
2428 cm_ReleaseSCache(newDscp);
2429 cm_ReleaseSCache(scp);
2433 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2434 lock_ReleaseWrite(&scp->rw);
2436 if (cm_shortNames) {
2437 dfid.vnode = htonl(scp->fid.vnode);
2438 dfid.unique = htonl(scp->fid.unique);
2440 if (!cm_Is8Dot3(TargetFileName))
2441 cm_Gen8Dot3NameIntW(TargetFileName, &dfid, shortName, NULL);
2443 shortName[0] = '\0';
2446 RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
2447 newDscp, scp, userp, &req, TargetFileName, shortName,
2448 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
2449 0, NULL, &dwRemaining);
2450 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
2451 cm_ReleaseSCache(scp);
2453 osi_Log0(afsd_logp, "RDR_RenameFileEntry SUCCESS");
2455 osi_Log3(afsd_logp, "RDR_RenameFileEntry cm_Rename oldDscp 0x%p newDscp 0x%p failed code 0x%x",
2456 oldDscp, newDscp, code);
2457 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2458 (*ResultCB)->ResultStatus = status;
2459 (*ResultCB)->ResultBufferLength = 0;
2462 cm_ReleaseSCache(oldDscp);
2463 cm_ReleaseSCache(newDscp);
2468 * AFS does not support cross-directory hard links but RDR_HardLinkFileEntry
2469 * is written as if AFS does. The check for cross-directory links is
2470 * implemented in cm_Link().
2472 * Windows supports optional ReplaceIfExists functionality. The AFS file
2473 * server does not. If the target name already exists and bReplaceIfExists
2474 * is true, check to see if the user has insert permission before calling
2475 * cm_Unlink() on the existing object. If the user does not have insert
2476 * permission return STATUS_ACCESS_DENIED.
2480 RDR_HardLinkFileEntry( IN cm_user_t *userp,
2481 IN WCHAR *SourceFileNameCounted,
2482 IN DWORD SourceFileNameLength,
2483 IN AFSFileID SourceFileId,
2484 IN AFSFileHardLinkCB *pHardLinkCB,
2486 IN DWORD ResultBufferLength,
2487 IN OUT AFSCommResult **ResultCB)
2490 AFSFileHardLinkResultCB *pResultCB = NULL;
2491 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
2492 AFSFileID SourceParentId = pHardLinkCB->SourceParentId;
2493 AFSFileID TargetParentId = pHardLinkCB->TargetParentId;
2494 WCHAR * TargetFileNameCounted = pHardLinkCB->TargetName;
2495 DWORD TargetFileNameLength = pHardLinkCB->TargetNameLength;
2496 cm_fid_t SourceParentFid;
2497 cm_fid_t TargetParentFid;
2499 cm_fid_t OrigTargetFid = {0,0,0,0,0};
2500 cm_scache_t * srcDscp = NULL;
2501 cm_scache_t * targetDscp = NULL;
2502 cm_scache_t * srcScp = NULL;
2504 wchar_t shortName[13];
2505 wchar_t SourceFileName[260];
2506 wchar_t TargetFileName[260];
2512 RDR_InitReq(&req, bWow64);
2514 StringCchCopyNW(SourceFileName, 260, SourceFileNameCounted, SourceFileNameLength / sizeof(WCHAR));
2515 StringCchCopyNW(TargetFileName, 260, TargetFileNameCounted, TargetFileNameLength / sizeof(WCHAR));
2517 osi_Log4(afsd_logp, "RDR_HardLinkFileEntry Source Parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2518 SourceParentId.Cell, SourceParentId.Volume,
2519 SourceParentId.Vnode, SourceParentId.Unique);
2520 osi_Log2(afsd_logp, "... Source Name=%S Length %u", osi_LogSaveStringW(afsd_logp, SourceFileName), SourceFileNameLength);
2521 osi_Log4(afsd_logp, "... Target Parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2522 TargetParentId.Cell, TargetParentId.Volume,
2523 TargetParentId.Vnode, TargetParentId.Unique);
2524 osi_Log2(afsd_logp, "... Target Name=%S Length %u", osi_LogSaveStringW(afsd_logp, TargetFileName), TargetFileNameLength);
2526 *ResultCB = (AFSCommResult *)malloc( size);
2534 pResultCB = (AFSFileHardLinkResultCB *)(*ResultCB)->ResultData;
2536 if (SourceFileNameLength == 0 || TargetFileNameLength == 0)
2538 osi_Log2(afsd_logp, "RDR_HardLinkFileEntry Invalid Name Length: src %u target %u",
2539 SourceFileNameLength, TargetFileNameLength);
2540 (*ResultCB)->ResultStatus = STATUS_INVALID_PARAMETER;
2544 SourceFid.cell = SourceFileId.Cell;
2545 SourceFid.volume = SourceFileId.Volume;
2546 SourceFid.vnode = SourceFileId.Vnode;
2547 SourceFid.unique = SourceFileId.Unique;
2548 SourceFid.hash = SourceFileId.Hash;
2550 SourceParentFid.cell = SourceParentId.Cell;
2551 SourceParentFid.volume = SourceParentId.Volume;
2552 SourceParentFid.vnode = SourceParentId.Vnode;
2553 SourceParentFid.unique = SourceParentId.Unique;
2554 SourceParentFid.hash = SourceParentId.Hash;
2556 TargetParentFid.cell = TargetParentId.Cell;
2557 TargetParentFid.volume = TargetParentId.Volume;
2558 TargetParentFid.vnode = TargetParentId.Vnode;
2559 TargetParentFid.unique = TargetParentId.Unique;
2560 TargetParentFid.hash = TargetParentId.Hash;
2562 code = cm_GetSCache(&SourceFid, NULL, &srcScp, userp, &req);
2564 osi_Log1(afsd_logp, "RDR_HardLinkFileEntry cm_GetSCache source failed code 0x%x", code);
2565 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2566 (*ResultCB)->ResultStatus = status;
2570 code = cm_GetSCache(&TargetParentFid, NULL, &targetDscp, userp, &req);
2572 osi_Log1(afsd_logp, "RDR_HardLinkFileEntry cm_GetSCache target parent failed code 0x%x", code);
2573 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2574 (*ResultCB)->ResultStatus = status;
2575 cm_ReleaseSCache(srcScp);
2579 lock_ObtainWrite(&targetDscp->rw);
2580 code = cm_SyncOp(targetDscp, NULL, userp, &req, PRSFS_INSERT,
2581 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2583 osi_Log2(afsd_logp, "RDR_HardLinkFileEntry cm_SyncOp targetDscp 0x%p failed code 0x%x", targetDscp, code);
2584 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2585 (*ResultCB)->ResultStatus = status;
2586 lock_ReleaseWrite(&targetDscp->rw);
2587 cm_ReleaseSCache(srcScp);
2588 cm_ReleaseSCache(targetDscp);
2592 cm_SyncOpDone(targetDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2593 lock_ReleaseWrite(&targetDscp->rw);
2595 if (targetDscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2596 osi_Log1(afsd_logp, "RDR_HardLinkFileEntry targetDscp 0x%p not a directory", targetDscp);
2597 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2598 cm_ReleaseSCache(srcScp);
2599 cm_ReleaseSCache(targetDscp);
2603 if ( cm_FidCmp(&SourceParentFid, &TargetParentFid) ) {
2604 code = cm_GetSCache(&SourceParentFid, NULL, &srcDscp, userp, &req);
2606 osi_Log1(afsd_logp, "RDR_HardLinkFileEntry cm_GetSCache source parent failed code 0x%x", code);
2607 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2608 if ( status == STATUS_INVALID_HANDLE)
2609 status = STATUS_OBJECT_PATH_INVALID;
2610 (*ResultCB)->ResultStatus = status;
2611 cm_ReleaseSCache(srcScp);
2612 cm_ReleaseSCache(targetDscp);
2616 lock_ObtainWrite(&srcDscp->rw);
2617 code = cm_SyncOp(srcDscp, NULL, userp, &req, 0,
2618 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2620 osi_Log2(afsd_logp, "RDR_HardLinkFileEntry cm_SyncOp srcDscp 0x%p failed code 0x%x", srcDscp, code);
2621 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2622 if ( status == STATUS_INVALID_HANDLE)
2623 status = STATUS_OBJECT_PATH_INVALID;
2624 (*ResultCB)->ResultStatus = status;
2625 lock_ReleaseWrite(&srcDscp->rw);
2626 if (srcDscp != targetDscp)
2627 cm_ReleaseSCache(srcDscp);
2628 cm_ReleaseSCache(targetDscp);
2629 cm_ReleaseSCache(srcScp);
2633 cm_SyncOpDone(srcDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2634 lock_ReleaseWrite(&srcDscp->rw);
2636 if (srcDscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2637 osi_Log1(afsd_logp, "RDR_HardLinkFileEntry srcDscp 0x%p not a directory", srcDscp);
2638 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2639 if (srcDscp != targetDscp)
2640 cm_ReleaseSCache(srcDscp);
2641 cm_ReleaseSCache(targetDscp);
2642 cm_ReleaseSCache(srcScp);
2646 srcDscp = targetDscp;
2649 /* Obtain the target FID if it exists */
2650 code = cm_BeginDirOp( targetDscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
2652 code = cm_BPlusDirLookup(&dirop, TargetFileName, &OrigTargetFid);
2653 cm_EndDirOp(&dirop);
2656 if (OrigTargetFid.vnode) {
2658 /* An object exists with the target name */
2659 if (!pHardLinkCB->bReplaceIfExists) {
2660 osi_Log0(afsd_logp, "RDR_HardLinkFileEntry target name collision and !ReplaceIfExists");
2661 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_COLLISION;
2662 if (srcDscp != targetDscp)
2663 cm_ReleaseSCache(srcDscp);
2664 cm_ReleaseSCache(targetDscp);
2665 cm_ReleaseSCache(srcScp);
2669 lock_ObtainWrite(&targetDscp->rw);
2670 code = cm_SyncOp(targetDscp, NULL, userp, &req, PRSFS_INSERT | PRSFS_DELETE,
2671 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2673 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2674 (*ResultCB)->ResultStatus = status;
2675 lock_ReleaseWrite(&srcDscp->rw);
2676 if (srcDscp != targetDscp)
2677 cm_ReleaseSCache(srcDscp);
2678 cm_ReleaseSCache(targetDscp);
2679 cm_ReleaseSCache(srcScp);
2682 cm_SyncOpDone(targetDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2683 lock_ReleaseWrite(&targetDscp->rw);
2685 code = cm_Unlink(targetDscp, NULL, TargetFileName, userp, &req);
2687 osi_Log1(afsd_logp, "RDR_HardLinkFileEntry cm_Unlink code 0x%x", code);
2688 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2689 (*ResultCB)->ResultStatus = status;
2690 lock_ReleaseWrite(&srcDscp->rw);
2691 if (srcDscp != targetDscp)
2692 cm_ReleaseSCache(srcDscp);
2693 cm_ReleaseSCache(targetDscp);
2694 cm_ReleaseSCache(srcScp);
2699 code = cm_Link( targetDscp, TargetFileName, srcScp, 0, userp, &req);
2702 cm_scache_t *targetScp = 0;
2705 (*ResultCB)->ResultBufferLength = ResultBufferLength;
2706 dwRemaining = ResultBufferLength - sizeof( AFSFileHardLinkResultCB) + sizeof( AFSDirEnumEntry);
2707 (*ResultCB)->ResultStatus = 0;
2709 pResultCB->SourceParentDataVersion.QuadPart = srcDscp->dataVersion;
2710 pResultCB->TargetParentDataVersion.QuadPart = targetDscp->dataVersion;
2712 osi_Log2(afsd_logp, "RDR_HardLinkFileEntry cm_Link srcDscp 0x%p targetDscp 0x%p SUCCESS",
2713 srcDscp, targetDscp);
2715 code = cm_BeginDirOp( targetDscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
2717 code = cm_BPlusDirLookup(&dirop, TargetFileName, &TargetFid);
2718 cm_EndDirOp(&dirop);
2722 osi_Log1(afsd_logp, "RDR_HardLinkFileEntry cm_BPlusDirLookup failed code 0x%x",
2724 (*ResultCB)->ResultStatus = STATUS_OBJECT_PATH_INVALID;
2725 if (srcDscp != targetDscp)
2726 cm_ReleaseSCache(srcDscp);
2727 cm_ReleaseSCache(srcScp);
2728 cm_ReleaseSCache(targetDscp);
2732 osi_Log4(afsd_logp, "RDR_HardLinkFileEntry Target FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2733 TargetFid.cell, TargetFid.volume,
2734 TargetFid.vnode, TargetFid.unique);
2736 code = cm_GetSCache(&TargetFid, &targetDscp->fid, &targetScp, userp, &req);
2738 osi_Log1(afsd_logp, "RDR_HardLinkFileEntry cm_GetSCache target failed code 0x%x", code);
2739 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2740 (*ResultCB)->ResultStatus = status;
2741 if (srcDscp != targetDscp)
2742 cm_ReleaseSCache(srcDscp);
2743 cm_ReleaseSCache(srcScp);
2744 cm_ReleaseSCache(targetDscp);
2748 /* Make sure the source vnode is current */
2749 lock_ObtainWrite(&targetScp->rw);
2750 code = cm_SyncOp(targetScp, NULL, userp, &req, 0,
2751 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2753 osi_Log2(afsd_logp, "RDR_HardLinkFileEntry cm_SyncOp scp 0x%p failed code 0x%x",
2755 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2756 (*ResultCB)->ResultStatus = status;
2757 lock_ReleaseWrite(&targetScp->rw);
2758 cm_ReleaseSCache(targetScp);
2759 if (srcDscp != targetDscp)
2760 cm_ReleaseSCache(srcDscp);
2761 cm_ReleaseSCache(srcScp);
2762 cm_ReleaseSCache(targetDscp);
2766 cm_SyncOpDone(targetScp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2767 lock_ReleaseWrite(&targetScp->rw);
2769 if (cm_shortNames) {
2770 dfid.vnode = htonl(targetScp->fid.vnode);
2771 dfid.unique = htonl(targetScp->fid.unique);
2773 if (!cm_Is8Dot3(TargetFileName))
2774 cm_Gen8Dot3NameIntW(TargetFileName, &dfid, shortName, NULL);
2776 shortName[0] = '\0';
2779 RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
2780 targetDscp, targetScp, userp, &req, TargetFileName, shortName,
2781 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
2782 0, NULL, &dwRemaining);
2783 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
2784 cm_ReleaseSCache(targetScp);
2786 osi_Log0(afsd_logp, "RDR_HardLinkFileEntry SUCCESS");
2788 osi_Log3(afsd_logp, "RDR_HardLinkFileEntry cm_Link srcDscp 0x%p targetDscp 0x%p failed code 0x%x",
2789 srcDscp, targetDscp, code);
2790 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2791 (*ResultCB)->ResultStatus = status;
2792 (*ResultCB)->ResultBufferLength = 0;
2795 cm_ReleaseSCache(srcScp);
2796 if (srcDscp != targetDscp)
2797 cm_ReleaseSCache(srcDscp);
2798 cm_ReleaseSCache(targetDscp);
2803 RDR_FlushFileEntry( IN cm_user_t *userp,
2804 IN AFSFileID FileId,
2806 IN DWORD ResultBufferLength,
2807 IN OUT AFSCommResult **ResultCB)
2809 cm_scache_t *scp = NULL;
2818 RDR_InitReq(&req, bWow64);
2820 osi_Log4(afsd_logp, "RDR_FlushFileEntry File FID cell 0x%x vol 0x%x vno 0x%x uniq 0x%x",
2821 FileId.Cell, FileId.Volume,
2822 FileId.Vnode, FileId.Unique);
2824 snprintf( dbgstr, 1024,
2825 "RDR_FlushFileEntry File FID cell 0x%x vol 0x%x vno 0x%x uniq 0x%x\n",
2826 FileId.Cell, FileId.Volume,
2827 FileId.Vnode, FileId.Unique);
2828 OutputDebugStringA( dbgstr);
2831 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult));
2833 osi_Log0(afsd_logp, "RDR_FlushFileEntry out of memory");
2839 sizeof( AFSCommResult));
2841 /* Process the release */
2842 Fid.cell = FileId.Cell;
2843 Fid.volume = FileId.Volume;
2844 Fid.vnode = FileId.Vnode;
2845 Fid.unique = FileId.Unique;
2846 Fid.hash = FileId.Hash;
2848 code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
2850 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2851 (*ResultCB)->ResultStatus = status;
2852 osi_Log2(afsd_logp, "RDR_FlushFileEntry cm_GetSCache FID failure code=0x%x status=0x%x",
2857 lock_ObtainWrite(&scp->rw);
2858 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2859 lock_ReleaseWrite(&scp->rw);
2860 (*ResultCB)->ResultStatus = STATUS_INVALID_HANDLE;
2864 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2865 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2867 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2868 (*ResultCB)->ResultStatus = status;
2869 lock_ReleaseWrite(&scp->rw);
2870 cm_ReleaseSCache(scp);
2871 osi_Log3(afsd_logp, "RDR_FlushFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2876 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2877 lock_ReleaseWrite(&scp->rw);
2879 code = cm_FSync(scp, userp, &req, FALSE);
2880 cm_ReleaseSCache(scp);
2883 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2884 (*ResultCB)->ResultStatus = status;
2885 osi_Log2(afsd_logp, "RDR_FlushFileEntry FAILURE code=0x%x status=0x%x",
2888 (*ResultCB)->ResultStatus = 0;
2889 osi_Log0(afsd_logp, "RDR_FlushFileEntry SUCCESS");
2891 (*ResultCB)->ResultBufferLength = 0;
2897 RDR_CheckAccess( IN cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
2901 ULONG afs_acc, afs_gr;
2903 afs_uint32 code = 0;
2905 file = (scp->fileType == CM_SCACHETYPE_FILE);
2908 /* access definitions from prs_fs.h */
2910 if (access & FILE_READ_DATA)
2911 afs_acc |= PRSFS_READ;
2912 if (access & FILE_READ_EA || access & FILE_READ_ATTRIBUTES)
2913 afs_acc |= PRSFS_READ;
2914 if (file && ((access & FILE_WRITE_DATA) || (access & FILE_APPEND_DATA)))
2915 afs_acc |= PRSFS_WRITE;
2916 if (access & FILE_WRITE_EA || access & FILE_WRITE_ATTRIBUTES)
2917 afs_acc |= PRSFS_WRITE;
2918 if (dir && ((access & FILE_ADD_FILE) || (access & FILE_ADD_SUBDIRECTORY)))
2919 afs_acc |= PRSFS_INSERT;
2920 if (dir && (access & FILE_LIST_DIRECTORY))
2921 afs_acc |= PRSFS_LOOKUP;
2922 if (file && (access & FILE_EXECUTE))
2923 afs_acc |= PRSFS_WRITE;
2924 if (dir && (access & FILE_TRAVERSE))
2925 afs_acc |= PRSFS_READ;
2926 if (dir && (access & FILE_DELETE_CHILD))
2927 afs_acc |= PRSFS_DELETE;
2928 if ((access & DELETE))
2929 afs_acc |= PRSFS_DELETE;
2931 /* check ACL with server */
2932 lock_ObtainWrite(&scp->rw);
2935 if (cm_HaveAccessRights(scp, userp, reqp, afs_acc, &afs_gr))
2941 /* we don't know the required access rights */
2942 code = cm_GetAccessRights(scp, userp, reqp);
2948 lock_ReleaseWrite(&(scp->rw));
2952 if (afs_gr & PRSFS_READ)
2953 *granted |= FILE_READ_DATA | FILE_READ_EA | FILE_READ_ATTRIBUTES | FILE_EXECUTE;
2954 if (afs_gr & PRSFS_WRITE)
2955 *granted |= FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES | FILE_EXECUTE;
2956 if (afs_gr & PRSFS_INSERT)
2957 *granted |= (dir ? FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY : 0) | (file ? FILE_ADD_SUBDIRECTORY : 0);
2958 if (afs_gr & PRSFS_LOOKUP)
2959 *granted |= (dir ? FILE_LIST_DIRECTORY : 0);
2960 if (afs_gr & PRSFS_DELETE)
2961 *granted |= FILE_DELETE_CHILD | DELETE;
2962 if (afs_gr & PRSFS_LOCK)
2964 if (afs_gr & PRSFS_ADMINISTER)
2967 *granted |= SYNCHRONIZE | READ_CONTROL;
2969 /* don't give more access than what was requested */
2971 osi_Log3(afsd_logp, "RDR_CheckAccess SUCCESS scp=0x%p requested=0x%x granted=0x%x", scp, access, *granted);
2973 osi_Log2(afsd_logp, "RDR_CheckAccess FAILURE scp=0x%p code=0x%x",
2980 RDR_OpenFileEntry( IN cm_user_t *userp,
2981 IN AFSFileID FileId,
2982 IN AFSFileOpenCB *OpenCB,
2985 IN DWORD ResultBufferLength,
2986 IN OUT AFSCommResult **ResultCB)
2988 AFSFileOpenResultCB *pResultCB = NULL;
2989 cm_scache_t *scp = NULL;
2990 cm_user_t *sysUserp = NULL;
2992 cm_lock_data_t *ldp = NULL;
2997 RDR_InitReq(&req, bWow64);
2999 osi_Log4(afsd_logp, "RDR_OpenFileEntry File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
3000 FileId.Cell, FileId.Volume,
3001 FileId.Vnode, FileId.Unique);
3003 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult) + sizeof( AFSFileOpenResultCB));
3005 osi_Log0(afsd_logp, "RDR_OpenFileEntry out of memory");
3011 sizeof( AFSCommResult) + sizeof( AFSFileOpenResultCB));
3013 pResultCB = (AFSFileOpenResultCB *)(*ResultCB)->ResultData;
3015 /* Process the release */
3016 Fid.cell = FileId.Cell;
3017 Fid.volume = FileId.Volume;
3018 Fid.vnode = FileId.Vnode;
3019 Fid.unique = FileId.Unique;
3020 Fid.hash = FileId.Hash;
3022 code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
3024 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3025 (*ResultCB)->ResultStatus = status;
3026 osi_Log2(afsd_logp, "RDR_OpenFileEntry cm_GetSCache FID failure code=0x%x status=0x%x",
3031 lock_ObtainWrite(&scp->rw);
3032 code = cm_SyncOp(scp, NULL, userp, &req, 0,
3033 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
3035 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3036 (*ResultCB)->ResultStatus = status;
3037 lock_ReleaseWrite(&scp->rw);
3038 cm_ReleaseSCache(scp);
3039 osi_Log3(afsd_logp, "RDR_OpenFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
3044 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
3045 lock_ReleaseWrite(&scp->rw);
3047 sysUserp = RDR_GetLocalSystemUser();
3050 * Skip the open check if the request is coming from the local system account.
3051 * The local system has no tokens and therefore any requests sent to a file
3052 * server will fail. Unfortunately, there are special system processes that
3053 * perform actions on files and directories in preparation for memory mapping
3054 * executables. If the open check fails, the real request from the user process
3055 * will never be issued.
3057 * Permitting the file system to allow subsequent operations to proceed does
3058 * not compromise security. All requests to obtain file data or directory
3059 * enumerations will subsequently fail if they are not submitted under the
3060 * context of a process for that have access to the necessary credentials.
3063 if ( userp == sysUserp)
3065 osi_Log1(afsd_logp, "RDR_OpenFileEntry LOCAL_SYSTEM access check skipped scp=0x%p",
3067 pResultCB->GrantedAccess = OpenCB->DesiredAccess;
3068 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
3079 "RDR_OpenFileEntry repeating open check scp=0x%p userp=0x%p code=0x%x",
3082 code = cm_CheckNTOpen(scp, OpenCB->DesiredAccess, OpenCB->ShareAccess,
3084 OpenCB->ProcessId, OpenCB->Identifier,
3087 code = RDR_CheckAccess(scp, userp, &req, OpenCB->DesiredAccess, &pResultCB->GrantedAccess);
3088 cm_CheckNTOpenDone(scp, userp, &req, &ldp);
3089 } while (count < 100 && (code == CM_ERROR_RETRY || code == CM_ERROR_WOULDBLOCK));
3093 * If we are restricting sharing, we should do so with a suitable
3096 if (code == 0 && scp->fileType == CM_SCACHETYPE_FILE && !(OpenCB->ShareAccess & FILE_SHARE_WRITE)) {
3098 LARGE_INTEGER LOffset, LLength;
3101 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
3102 LOffset.LowPart = SMB_FID_QLOCK_LOW;
3103 LLength.HighPart = 0;
3104 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
3107 * If we are not opening the file for writing, then we don't
3108 * try to get an exclusive lock. No one else should be able to
3109 * get an exclusive lock on the file anyway, although someone
3110 * else can get a shared lock.
3112 if ((OpenCB->ShareAccess & FILE_SHARE_READ) || !(OpenCB->DesiredAccess & AFS_ACCESS_WRITE))
3114 sLockType = LOCKING_ANDX_SHARED_LOCK;
3119 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, OpenCB->Identifier);
3121 lock_ObtainWrite(&scp->rw);
3122 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, &req, NULL);
3123 lock_ReleaseWrite(&scp->rw);
3126 code = CM_ERROR_SHARING_VIOLATION;
3127 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
3129 if (sLockType == LOCKING_ANDX_SHARED_LOCK)
3130 pResultCB->FileAccess = AFS_FILE_ACCESS_SHARED;
3132 pResultCB->FileAccess = AFS_FILE_ACCESS_EXCLUSIVE;
3135 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
3138 cm_ReleaseUser(sysUserp);
3139 if (code == 0 && bHoldFid)
3140 RDR_FlagScpInUse( scp, FALSE );
3141 cm_ReleaseSCache(scp);
3144 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3145 (*ResultCB)->ResultStatus = status;
3146 osi_Log2(afsd_logp, "RDR_OpenFileEntry FAILURE code=0x%x status=0x%x",
3149 (*ResultCB)->ResultStatus = 0;
3150 (*ResultCB)->ResultBufferLength = sizeof( AFSFileOpenResultCB);
3151 osi_Log0(afsd_logp, "RDR_OpenFileEntry SUCCESS");
3157 RDR_ReleaseFileAccess( IN cm_user_t *userp,
3158 IN AFSFileID FileId,
3159 IN AFSFileAccessReleaseCB *ReleaseFileCB,
3161 IN DWORD ResultBufferLength,
3162 IN OUT AFSCommResult **ResultCB)
3165 unsigned int sLockType;
3166 LARGE_INTEGER LOffset, LLength;
3167 cm_scache_t *scp = NULL;
3173 RDR_InitReq(&req, bWow64);
3175 osi_Log4(afsd_logp, "RDR_ReleaseFileAccess File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
3176 FileId.Cell, FileId.Volume,
3177 FileId.Vnode, FileId.Unique);
3179 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult));
3181 osi_Log0(afsd_logp, "RDR_ReleaseFileAccess out of memory");
3185 memset( *ResultCB, '\0', sizeof( AFSCommResult));
3187 if (ReleaseFileCB->FileAccess == AFS_FILE_ACCESS_NOLOCK)
3190 /* Process the release */
3191 Fid.cell = FileId.Cell;
3192 Fid.volume = FileId.Volume;
3193 Fid.vnode = FileId.Vnode;
3194 Fid.unique = FileId.Unique;
3195 Fid.hash = FileId.Hash;
3197 code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
3199 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3200 (*ResultCB)->ResultStatus = status;
3201 osi_Log2(afsd_logp, "RDR_ReleaseFileAccess cm_GetSCache FID failure code=0x%x status=0x%x",
3206 if (ReleaseFileCB->FileAccess == AFS_FILE_ACCESS_SHARED)
3207 sLockType = LOCKING_ANDX_SHARED_LOCK;
3211 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, ReleaseFileCB->Identifier);
3213 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
3214 LOffset.LowPart = SMB_FID_QLOCK_LOW;
3215 LLength.HighPart = 0;
3216 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
3218 lock_ObtainWrite(&scp->rw);
3220 code = cm_SyncOp(scp, NULL, userp, &req, 0, CM_SCACHESYNC_LOCK);
3223 code = cm_Unlock(scp, sLockType, LOffset, LLength, key, 0, userp, &req);
3225 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
3227 if (code == CM_ERROR_RANGE_NOT_LOCKED)
3229 osi_Log3(afsd_logp, "RDR_ReleaseFileAccess Range Not Locked -- FileAccess 0x%x ProcessId 0x%x HandleId 0x%x",
3230 ReleaseFileCB->FileAccess, ReleaseFileCB->ProcessId, ReleaseFileCB->Identifier);
3234 lock_ReleaseWrite(&scp->rw);
3236 osi_Log0(afsd_logp, "RDR_ReleaseFileAccessEntry SUCCESS");
3240 HexCheckSum(unsigned char * buf, int buflen, unsigned char * md5cksum)
3243 static char tr[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
3246 return "buffer length too small to HexCheckSum";
3248 for (i=0;i<16;i++) {
3251 buf[i*2] = tr[k / 16];
3252 buf[i*2+1] = tr[k % 16];
3260 * Extent requests from the file system are triggered when a file
3261 * page is not resident in the Windows cache. The file system is
3262 * responsible for loading the page but cannot block the request
3263 * while doing so. The AFS Redirector forwards the requests to
3264 * the AFS cache manager while indicating to Windows that the page
3265 * is not yet available. A polling operation will then ensue with
3266 * the AFS Redirector issuing a RDR_RequestFileExtentsXXX call for
3267 * each poll attempt. As each request is received and processed
3268 * by a separate worker thread in the service, this can lead to
3269 * contention by multiple threads attempting to claim the same
3270 * cm_buf_t objects. Therefore, it is important that
3272 * (a) the service avoid processing more than one overlapping
3273 * extent request at a time
3274 * (b) background daemon processing be used to avoid blocking
3277 * Beginning with the 20091122 build of the redirector, the redirector
3278 * will not issue an additional RDR_RequestFileExtentsXXX call for
3279 * each poll request. Instead, afsd_service is required to track
3280 * the requests and return them to the redirector or fail the
3281 * portions of the request that cannot be satisfied.
3283 * The request processing returns any extents that can be returned
3284 * immediately to the redirector. The rest of the requested range(s)
3285 * are queued as background operations using RDR_BkgFetch().
3288 /* do the background fetch. */
3290 RDR_BkgFetch(cm_scache_t *scp, void *rockp, cm_user_t *userp, cm_req_t *reqp)
3296 osi_hyper_t fetched;
3297 osi_hyper_t tblocksize;
3300 cm_buf_t *bufp = NULL;
3301 DWORD dwResultBufferLength;
3302 AFSSetFileExtentsCB *pResultCB;
3306 int reportErrorToRedir = 0;
3307 int force_retry = 0;
3309 FileId.Cell = scp->fid.cell;
3310 FileId.Volume = scp->fid.volume;
3311 FileId.Vnode = scp->fid.vnode;
3312 FileId.Unique = scp->fid.unique;
3313 FileId.Hash = scp->fid.hash;
3315 fetched.LowPart = 0;
3316 fetched.HighPart = 0;
3317 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
3318 base = ((rock_BkgFetch_t *)rockp)->base;
3319 length = ((rock_BkgFetch_t *)rockp)->length;
3320 end = LargeIntegerAdd(base, length);
3322 osi_Log5(afsd_logp, "Starting BKG Fetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
3323 scp, base.HighPart, base.LowPart, length.HighPart, length.LowPart);
3326 * Make sure we have a callback.
3327 * This is necessary so that we can return access denied
3328 * if a callback cannot be granted.
3330 lock_ObtainWrite(&scp->rw);
3331 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_READ,
3332 CM_SCACHESYNC_NEEDCALLBA