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 + 1) * 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 (wcschr(wszName, '%') != NULL || wcschr(wszName, '#') != NULL)) {
1067 * A volume reference: <cell>{%,#}<volume> -> @vol:<cell>{%,#}<volume>
1069 StringCchCopyNW(wszName, cbName, _C(CM_PREFIX_VOL), CM_PREFIX_VOL_CCH);
1070 StringCbCatNW(wszName, cbName, FileName, FileNameLength);
1073 code = cm_EvaluateVolumeReference(wszName, CM_FLAG_CHECKPATH, userp, &req, &scp);
1076 if (code == 0 && scp) {
1077 wchar_t shortName[13]=L"";
1079 if (!cm_shortNames) {
1080 shortName[0] = L'\0';
1082 cm_Gen8Dot3VolNameW(scp->fid.cell, scp->fid.volume, shortName, NULL);
1083 } else if (!cm_Is8Dot3(wszName)) {
1086 dfid.vnode = htonl(scp->fid.vnode);
1087 dfid.unique = htonl(scp->fid.unique);
1089 cm_Gen8Dot3NameIntW(FileName, &dfid, shortName, NULL);
1091 shortName[0] = L'\0';
1094 code = RDR_PopulateCurrentEntry(pCurrentEntry, dwRemaining,
1095 dscp, scp, userp, &req,
1096 FileName, shortName,
1097 (bWow64 ? RDR_POP_WOW64 : 0) |
1098 (bNoFollow ? 0 : (RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS)),
1099 0, NULL, &dwRemaining);
1101 RDR_FlagScpInUse( scp, FALSE );
1102 cm_ReleaseSCache(scp);
1105 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1106 (*ResultCB)->ResultStatus = status;
1107 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName FAILURE code=0x%x status=0x%x",
1110 pEvalResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
1111 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
1112 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1113 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName SUCCESS");
1116 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1117 (*ResultCB)->ResultStatus = status;
1118 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName FAILURE code=0x%x status=0x%x",
1121 (*ResultCB)->ResultStatus = STATUS_NO_SUCH_FILE;
1122 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName No Such File");
1124 cm_ReleaseSCache(dscp);
1131 RDR_EvaluateNodeByID( IN cm_user_t *userp,
1132 IN AFSFileID ParentID, /* not used */
1133 IN AFSFileID SourceID,
1137 IN DWORD ResultBufferLength,
1138 IN OUT AFSCommResult **ResultCB)
1140 AFSFileEvalResultCB *pEvalResultCB = NULL;
1141 AFSDirEnumEntry * pCurrentEntry = NULL;
1142 size_t size = ResultBufferLength ? sizeof(AFSCommResult) + ResultBufferLength - 1 : sizeof(AFSCommResult);
1143 afs_uint32 code = 0;
1144 cm_scache_t * scp = NULL;
1145 cm_scache_t * dscp = NULL;
1152 osi_Log4(afsd_logp, "RDR_EvaluateNodeByID source FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1153 SourceID.Cell, SourceID.Volume, SourceID.Vnode, SourceID.Unique);
1154 osi_Log4(afsd_logp, "... parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1155 ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
1157 *ResultCB = (AFSCommResult *)malloc(size);
1159 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Out of Memory");
1163 memset(*ResultCB, 0, size);
1164 (*ResultCB)->ResultBufferLength = 0;
1165 dwRemaining = ResultBufferLength;
1166 if (ResultBufferLength >= sizeof( AFSFileEvalResultCB)) {
1167 pEvalResultCB = (AFSFileEvalResultCB *)&(*ResultCB)->ResultData;
1168 pCurrentEntry = &pEvalResultCB->DirEnum;
1169 dwRemaining -= (sizeof( AFSFileEvalResultCB) - sizeof( AFSDirEnumEntry));
1172 RDR_InitReq(&req, bWow64);
1174 if (SourceID.Cell != 0) {
1175 cm_SetFid(&Fid, SourceID.Cell, SourceID.Volume, SourceID.Vnode, SourceID.Unique);
1176 code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
1178 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1179 (*ResultCB)->ResultStatus = status;
1180 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache SourceFID failure code=0x%x status=0x%x",
1185 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
1186 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Object Name Invalid - Cell = 0");
1190 if (ParentID.Cell != 0) {
1191 cm_SetFid(&parentFid, ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
1192 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1194 cm_ReleaseSCache(scp);
1195 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1196 if ( status == STATUS_INVALID_HANDLE)
1197 status = STATUS_OBJECT_PATH_INVALID;
1198 (*ResultCB)->ResultStatus = status;
1199 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache parentFID failure code=0x%x status=0x%x",
1203 } else if (SourceID.Vnode == 1) {
1205 cm_HoldSCache(dscp);
1206 } else if (scp->parentVnode) {
1207 cm_SetFid(&parentFid, SourceID.Cell, SourceID.Volume, scp->parentVnode, scp->parentUnique);
1208 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1210 cm_ReleaseSCache(scp);
1211 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1212 if ( status == STATUS_INVALID_HANDLE)
1213 status = STATUS_OBJECT_PATH_INVALID;
1214 (*ResultCB)->ResultStatus = status;
1215 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache parentFID failure code=0x%x status=0x%x",
1220 (*ResultCB)->ResultStatus = STATUS_OBJECT_PATH_INVALID;
1221 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Object Path Invalid - Unknown Parent");
1225 /* Make sure the directory is current */
1226 lock_ObtainWrite(&dscp->rw);
1227 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1228 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1230 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1231 (*ResultCB)->ResultStatus = status;
1232 lock_ReleaseWrite(&dscp->rw);
1233 cm_ReleaseSCache(dscp);
1234 cm_ReleaseSCache(scp);
1235 osi_Log3(afsd_logp, "RDR_EvaluateNodeByID cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1236 dscp, code, status);
1240 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1241 lock_ReleaseWrite(&dscp->rw);
1243 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1244 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1245 cm_ReleaseSCache(dscp);
1246 cm_ReleaseSCache(scp);
1247 osi_Log1(afsd_logp, "RDR_EvaluateNodeByID Not a Directory dscp=0x%p", dscp);
1251 code = RDR_PopulateCurrentEntry(pCurrentEntry, dwRemaining,
1252 dscp, scp, userp, &req, NULL, NULL,
1253 (bWow64 ? RDR_POP_WOW64 : 0) |
1254 (bNoFollow ? 0 : (RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS)),
1255 0, NULL, &dwRemaining);
1258 RDR_FlagScpInUse( scp, FALSE );
1259 cm_ReleaseSCache(scp);
1260 cm_ReleaseSCache(dscp);
1263 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1264 (*ResultCB)->ResultStatus = status;
1265 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID FAILURE code=0x%x status=0x%x",
1268 pEvalResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
1270 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
1271 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1272 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID SUCCESS");
1278 RDR_CreateFileEntry( IN cm_user_t *userp,
1279 IN WCHAR *FileNameCounted,
1280 IN DWORD FileNameLength,
1281 IN AFSFileCreateCB *CreateCB,
1284 IN DWORD ResultBufferLength,
1285 IN OUT AFSCommResult **ResultCB)
1287 AFSFileCreateResultCB *pResultCB = NULL;
1288 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1291 cm_scache_t * dscp = NULL;
1292 afs_uint32 flags = 0;
1294 cm_scache_t * scp = NULL;
1297 wchar_t FileName[260];
1299 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
1301 osi_Log4(afsd_logp, "RDR_CreateFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1302 CreateCB->ParentId.Cell, CreateCB->ParentId.Volume,
1303 CreateCB->ParentId.Vnode, CreateCB->ParentId.Unique);
1304 osi_Log1(afsd_logp, "... name=%S", osi_LogSaveStringW(afsd_logp, FileName));
1306 RDR_InitReq(&req, bWow64);
1307 memset(&setAttr, 0, sizeof(cm_attr_t));
1309 *ResultCB = (AFSCommResult *)malloc(size);
1311 osi_Log0(afsd_logp, "RDR_CreateFileEntry out of memory");
1319 parentFid.cell = CreateCB->ParentId.Cell;
1320 parentFid.volume = CreateCB->ParentId.Volume;
1321 parentFid.vnode = CreateCB->ParentId.Vnode;
1322 parentFid.unique = CreateCB->ParentId.Unique;
1323 parentFid.hash = CreateCB->ParentId.Hash;
1325 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1327 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1328 (*ResultCB)->ResultStatus = status;
1329 if ( status == STATUS_INVALID_HANDLE)
1330 status = STATUS_OBJECT_PATH_INVALID;
1331 osi_Log2(afsd_logp, "RDR_CreateFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1336 lock_ObtainWrite(&dscp->rw);
1337 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1338 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1340 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1341 (*ResultCB)->ResultStatus = status;
1342 lock_ReleaseWrite(&dscp->rw);
1343 cm_ReleaseSCache(dscp);
1344 osi_Log3(afsd_logp, "RDR_CreateFileEntry cm_SyncOp failure (1) dscp=0x%p code=0x%x status=0x%x",
1345 dscp, code, status);
1349 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1350 lock_ReleaseWrite(&dscp->rw);
1352 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1353 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1354 cm_ReleaseSCache(dscp);
1355 osi_Log1(afsd_logp, "RDR_CreateFileEntry Not a Directory dscp=0x%p",
1360 /* Use current time */
1361 setAttr.mask = CM_ATTRMASK_CLIENTMODTIME;
1362 setAttr.clientModTime = time(NULL);
1364 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1365 if (smb_unixModeDefaultDir) {
1366 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1367 setAttr.unixModeBits = smb_unixModeDefaultDir;
1368 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)
1369 setAttr.unixModeBits &= ~0222; /* disable the write bits */
1372 code = cm_MakeDir(dscp, FileName, flags, &setAttr, userp, &req, &scp);
1374 if (smb_unixModeDefaultFile) {
1375 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1376 setAttr.unixModeBits = smb_unixModeDefaultFile;
1377 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)
1378 setAttr.unixModeBits &= ~0222; /* disable the write bits */
1381 setAttr.mask |= CM_ATTRMASK_LENGTH;
1382 setAttr.length.LowPart = CreateCB->AllocationSize.LowPart;
1383 setAttr.length.HighPart = CreateCB->AllocationSize.HighPart;
1384 code = cm_Create(dscp, FileName, flags, &setAttr, &scp, userp, &req);
1387 wchar_t shortName[13]=L"";
1391 (*ResultCB)->ResultStatus = 0; // We will be able to fit all the data in here
1393 (*ResultCB)->ResultBufferLength = sizeof( AFSFileCreateResultCB);
1395 pResultCB = (AFSFileCreateResultCB *)(*ResultCB)->ResultData;
1397 dwRemaining = ResultBufferLength - sizeof( AFSFileCreateResultCB) + sizeof( AFSDirEnumEntry);
1399 lock_ObtainWrite(&dscp->rw);
1400 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1401 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1403 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1404 (*ResultCB)->ResultStatus = status;
1405 lock_ReleaseWrite(&dscp->rw);
1406 cm_ReleaseSCache(dscp);
1407 cm_ReleaseSCache(scp);
1408 osi_Log3(afsd_logp, "RDR_CreateFileEntry cm_SyncOp failure (2) dscp=0x%p code=0x%x status=0x%x",
1409 dscp, code, status);
1413 pResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
1415 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1416 lock_ReleaseWrite(&dscp->rw);
1418 if (cm_shortNames) {
1419 dfid.vnode = htonl(scp->fid.vnode);
1420 dfid.unique = htonl(scp->fid.unique);
1422 if (!cm_Is8Dot3(FileName))
1423 cm_Gen8Dot3NameIntW(FileName, &dfid, shortName, NULL);
1425 shortName[0] = '\0';
1428 code = RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
1429 dscp, scp, userp, &req, FileName, shortName,
1430 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
1431 0, NULL, &dwRemaining);
1434 RDR_FlagScpInUse( scp, FALSE );
1435 cm_ReleaseSCache(scp);
1436 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1437 osi_Log0(afsd_logp, "RDR_CreateFileEntry SUCCESS");
1439 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1440 (*ResultCB)->ResultStatus = status;
1441 (*ResultCB)->ResultBufferLength = 0;
1442 osi_Log2(afsd_logp, "RDR_CreateFileEntry FAILURE code=0x%x status=0x%x",
1446 cm_ReleaseSCache(dscp);
1452 RDR_UpdateFileEntry( IN cm_user_t *userp,
1453 IN AFSFileID FileId,
1454 IN AFSFileUpdateCB *UpdateCB,
1456 IN DWORD ResultBufferLength,
1457 IN OUT AFSCommResult **ResultCB)
1459 AFSFileUpdateResultCB *pResultCB = NULL;
1460 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1464 afs_uint32 flags = 0;
1466 cm_scache_t * scp = NULL;
1467 cm_scache_t * dscp = NULL;
1469 time_t clientModTime;
1472 BOOL bScpLocked = FALSE;
1474 RDR_InitReq(&req, bWow64);
1475 memset(&setAttr, 0, sizeof(cm_attr_t));
1477 osi_Log4(afsd_logp, "RDR_UpdateFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1478 UpdateCB->ParentId.Cell, UpdateCB->ParentId.Volume,
1479 UpdateCB->ParentId.Vnode, UpdateCB->ParentId.Unique);
1480 osi_Log4(afsd_logp, "... object FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1481 FileId.Cell, FileId.Volume,
1482 FileId.Vnode, FileId.Unique);
1484 *ResultCB = (AFSCommResult *)malloc( size);
1486 osi_Log0(afsd_logp, "RDR_UpdateFileEntry Out of Memory");
1494 parentFid.cell = UpdateCB->ParentId.Cell;
1495 parentFid.volume = UpdateCB->ParentId.Volume;
1496 parentFid.vnode = UpdateCB->ParentId.Vnode;
1497 parentFid.unique = UpdateCB->ParentId.Unique;
1498 parentFid.hash = UpdateCB->ParentId.Hash;
1500 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1502 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1503 (*ResultCB)->ResultStatus = status;
1504 if ( status == STATUS_INVALID_HANDLE)
1505 status = STATUS_OBJECT_PATH_INVALID;
1506 osi_Log2(afsd_logp, "RDR_UpdateFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1511 lock_ObtainWrite(&dscp->rw);
1513 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1514 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1516 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1517 (*ResultCB)->ResultStatus = status;
1518 lock_ReleaseWrite(&dscp->rw);
1519 cm_ReleaseSCache(dscp);
1520 osi_Log3(afsd_logp, "RDR_UpdateFileEntry cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1521 dscp, code, status);
1525 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1526 lock_ReleaseWrite(&dscp->rw);
1529 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1530 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1531 cm_ReleaseSCache(dscp);
1532 osi_Log1(afsd_logp, "RDR_UpdateFileEntry Not a Directory dscp=0x%p",
1537 Fid.cell = FileId.Cell;
1538 Fid.volume = FileId.Volume;
1539 Fid.vnode = FileId.Vnode;
1540 Fid.unique = FileId.Unique;
1541 Fid.hash = FileId.Hash;
1543 code = cm_GetSCache(&Fid, &dscp->fid, &scp, userp, &req);
1545 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1546 (*ResultCB)->ResultStatus = status;
1547 cm_ReleaseSCache(dscp);
1548 osi_Log2(afsd_logp, "RDR_UpdateFileEntry cm_GetSCache object FID failure code=0x%x status=0x%x",
1553 lock_ObtainWrite(&scp->rw);
1555 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1556 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1558 lock_ReleaseWrite(&scp->rw);
1559 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1560 (*ResultCB)->ResultStatus = status;
1561 (*ResultCB)->ResultBufferLength = 0;
1562 cm_ReleaseSCache(dscp);
1563 cm_ReleaseSCache(scp);
1564 osi_Log3(afsd_logp, "RDR_UpdateFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
1568 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1570 if (UpdateCB->ChangeTime.QuadPart) {
1572 if (scp->fileType == CM_SCACHETYPE_FILE) {
1573 /* Do not set length and other attributes at the same time */
1574 if (scp->length.QuadPart != UpdateCB->AllocationSize.QuadPart) {
1575 osi_Log2(afsd_logp, "RDR_UpdateFileEntry Length Change 0x%x -> 0x%x",
1576 (afs_uint32)scp->length.QuadPart, (afs_uint32)UpdateCB->AllocationSize.QuadPart);
1577 setAttr.mask |= CM_ATTRMASK_LENGTH;
1578 setAttr.length.LowPart = UpdateCB->AllocationSize.LowPart;
1579 setAttr.length.HighPart = UpdateCB->AllocationSize.HighPart;
1580 lock_ReleaseWrite(&scp->rw);
1582 code = cm_SetAttr(scp, &setAttr, userp, &req);
1590 lock_ObtainWrite(&scp->rw);
1593 if ((scp->unixModeBits & 0200) && (UpdateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1594 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1595 setAttr.unixModeBits = scp->unixModeBits & ~0222;
1596 } else if (!(scp->unixModeBits & 0200) && !(UpdateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1597 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1598 setAttr.unixModeBits = scp->unixModeBits | 0222;
1602 if (UpdateCB->LastWriteTime.QuadPart) {
1603 ft.dwLowDateTime = UpdateCB->LastWriteTime.LowPart;
1604 ft.dwHighDateTime = UpdateCB->LastWriteTime.HighPart;
1606 cm_UnixTimeFromLargeSearchTime(& clientModTime, &ft);
1609 lock_ObtainWrite(&scp->rw);
1612 if (scp->clientModTime != clientModTime) {
1613 setAttr.mask |= CM_ATTRMASK_CLIENTMODTIME;
1614 setAttr.clientModTime = clientModTime;
1619 lock_ReleaseWrite(&scp->rw);
1621 code = cm_SetAttr(scp, &setAttr, userp, &req);
1628 lock_ReleaseWrite(&scp->rw);
1632 DWORD dwRemaining = ResultBufferLength - sizeof( AFSFileUpdateResultCB) + sizeof( AFSDirEnumEntry);
1634 pResultCB = (AFSFileUpdateResultCB *)(*ResultCB)->ResultData;
1636 pResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
1638 code = RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
1639 dscp, scp, userp, &req, NULL, NULL,
1640 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
1641 0, NULL, &dwRemaining);
1642 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1643 osi_Log0(afsd_logp, "RDR_UpdateFileEntry SUCCESS");
1645 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1646 (*ResultCB)->ResultStatus = status;
1647 (*ResultCB)->ResultBufferLength = 0;
1648 osi_Log2(afsd_logp, "RDR_UpdateFileEntry FAILURE code=0x%x status=0x%x",
1651 cm_ReleaseSCache(scp);
1652 cm_ReleaseSCache(dscp);
1658 RDR_CleanupFileEntry( IN cm_user_t *userp,
1659 IN AFSFileID FileId,
1660 IN WCHAR *FileNameCounted,
1661 IN DWORD FileNameLength,
1662 IN AFSFileCleanupCB *CleanupCB,
1664 IN BOOL bLastHandle,
1665 IN BOOL bDeleteFile,
1666 IN BOOL bUnlockFile,
1667 IN DWORD ResultBufferLength,
1668 IN OUT AFSCommResult **ResultCB)
1670 AFSFileCleanupResultCB *pResultCB = NULL;
1671 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1674 afs_uint32 code = 0;
1675 afs_uint32 flags = 0;
1677 cm_scache_t * scp = NULL;
1678 cm_scache_t * dscp = NULL;
1680 time_t clientModTime;
1683 BOOL bScpLocked = FALSE;
1684 BOOL bDscpLocked = FALSE;
1685 BOOL bFlushFile = FALSE;
1688 RDR_InitReq(&req, bWow64);
1689 memset(&setAttr, 0, sizeof(cm_attr_t));
1691 osi_Log4(afsd_logp, "RDR_CleanupFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1692 CleanupCB->ParentId.Cell, CleanupCB->ParentId.Volume,
1693 CleanupCB->ParentId.Vnode, CleanupCB->ParentId.Unique);
1694 osi_Log4(afsd_logp, "... object FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1695 FileId.Cell, FileId.Volume,
1696 FileId.Vnode, FileId.Unique);
1698 *ResultCB = (AFSCommResult *)malloc( size);
1700 osi_Log0(afsd_logp, "RDR_CleanupFileEntry Out of Memory");
1708 parentFid.cell = CleanupCB->ParentId.Cell;
1709 parentFid.volume = CleanupCB->ParentId.Volume;
1710 parentFid.vnode = CleanupCB->ParentId.Vnode;
1711 parentFid.unique = CleanupCB->ParentId.Unique;
1712 parentFid.hash = CleanupCB->ParentId.Hash;
1714 if (parentFid.cell) {
1715 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
1717 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1718 if ( status == STATUS_INVALID_HANDLE)
1719 status = STATUS_OBJECT_PATH_INVALID;
1720 (*ResultCB)->ResultStatus = status;
1721 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1726 lock_ObtainWrite(&dscp->rw);
1728 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1729 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1731 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp failure dscp=0x%p code=0x%x",
1737 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1738 lock_ReleaseWrite(&dscp->rw);
1739 bDscpLocked = FALSE;
1741 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1742 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1743 cm_ReleaseSCache(dscp);
1744 osi_Log1(afsd_logp, "RDR_CleanupFileEntry Not a Directory dscp=0x%p",
1751 Fid.cell = FileId.Cell;
1752 Fid.volume = FileId.Volume;
1753 Fid.vnode = FileId.Vnode;
1754 Fid.unique = FileId.Unique;
1755 Fid.hash = FileId.Hash;
1757 code = cm_GetSCache(&Fid, dscp ? &dscp->fid : NULL, &scp, userp, &req);
1759 osi_Log1(afsd_logp, "RDR_CleanupFileEntry cm_GetSCache object FID failure code=0x%x",
1764 lock_ObtainWrite(&scp->rw);
1766 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1767 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1769 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp failure scp=0x%p code=0x%x",
1773 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1775 if ((bLastHandle || bFlushFile) &&
1776 scp->redirBufCount > 0)
1778 LARGE_INTEGER heldExtents;
1779 AFSFileExtentCB extentList[1024];
1780 DWORD extentCount = 0;
1785 heldExtents.QuadPart = 0;
1787 for ( srbp = redirq_to_cm_buf_t(scp->redirQueueT);
1789 srbp = redirq_to_cm_buf_t(osi_QPrev(&srbp->redirq)))
1791 extentList[extentCount].Flags = 0;
1792 extentList[extentCount].Length = cm_data.blockSize;
1793 extentList[extentCount].FileOffset.QuadPart = srbp->offset.QuadPart;
1794 extentList[extentCount].CacheOffset.QuadPart = srbp->datap - RDR_extentBaseAddress;
1795 lock_ObtainWrite(&buf_globalLock);
1796 srbp->redirReleaseRequested = now;
1797 lock_ReleaseWrite(&buf_globalLock);
1800 if (extentCount == 1024) {
1801 lock_ReleaseWrite(&scp->rw);
1802 code = RDR_RequestExtentRelease(&scp->fid, heldExtents, extentCount, extentList);
1804 if (code == CM_ERROR_RETRY) {
1806 * The redirector either is not holding the extents or cannot let them
1807 * go because they are otherwise in use. At the moment, do nothing.
1814 lock_ObtainWrite(&scp->rw);
1818 if (code == 0 && extentCount > 0) {
1820 lock_ReleaseWrite(&scp->rw);
1823 code = RDR_RequestExtentRelease(&scp->fid, heldExtents, extentCount, extentList);
1828 /* No longer in use by redirector */
1830 lock_ObtainWrite(&scp->rw);
1835 lock_AssertWrite(&scp->rw);
1836 scp->flags &= ~CM_SCACHEFLAG_RDR_IN_USE;
1839 /* If not a readonly object, flush dirty data and update metadata */
1840 if (!(scp->flags & CM_SCACHEFLAG_RO)) {
1841 if ((bLastHandle || bFlushFile) &&
1842 buf_DirtyBuffersExist(&scp->fid)) {
1844 lock_ObtainWrite(&scp->rw);
1847 code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_WRITE,
1848 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1851 lock_ReleaseWrite(&scp->rw);
1855 code = cm_FSync(scp, userp, &req, bScpLocked);
1857 if (bLastHandle && code)
1861 if (CleanupCB->ChangeTime.QuadPart) {
1863 if (scp->fileType == CM_SCACHETYPE_FILE) {
1864 /* Do not set length and other attributes at the same time */
1865 if (scp->length.QuadPart != CleanupCB->AllocationSize.QuadPart) {
1866 osi_Log2(afsd_logp, "RDR_CleanupFileEntry Length Change 0x%x -> 0x%x",
1867 (afs_uint32)scp->length.QuadPart, (afs_uint32)CleanupCB->AllocationSize.QuadPart);
1868 setAttr.mask |= CM_ATTRMASK_LENGTH;
1869 setAttr.length.LowPart = CleanupCB->AllocationSize.LowPart;
1870 setAttr.length.HighPart = CleanupCB->AllocationSize.HighPart;
1873 lock_ReleaseWrite(&scp->rw);
1876 code = cm_SetAttr(scp, &setAttr, userp, &req);
1884 lock_ObtainWrite(&scp->rw);
1888 if ((scp->unixModeBits & 0200) && (CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1889 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1890 setAttr.unixModeBits = scp->unixModeBits & ~0222;
1891 } else if (!(scp->unixModeBits & 0200) && !(CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1892 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1893 setAttr.unixModeBits = scp->unixModeBits | 0222;
1897 if (CleanupCB->LastWriteTime.QuadPart) {
1898 ft.dwLowDateTime = CleanupCB->LastWriteTime.LowPart;
1899 ft.dwHighDateTime = CleanupCB->LastWriteTime.HighPart;
1901 cm_UnixTimeFromLargeSearchTime(&clientModTime, &ft);
1902 if (scp->clientModTime != clientModTime) {
1903 setAttr.mask |= CM_ATTRMASK_CLIENTMODTIME;
1904 setAttr.clientModTime = clientModTime;
1910 lock_ReleaseWrite(&scp->rw);
1912 code = cm_SetAttr(scp, &setAttr, userp, &req);
1918 /* Now drop the lock enforcing the share access */
1919 if ( CleanupCB->FileAccess != AFS_FILE_ACCESS_NOLOCK) {
1920 unsigned int sLockType;
1921 LARGE_INTEGER LOffset, LLength;
1923 if (CleanupCB->FileAccess == AFS_FILE_ACCESS_SHARED)
1924 sLockType = LOCKING_ANDX_SHARED_LOCK;
1928 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, CleanupCB->Identifier);
1930 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
1931 LOffset.LowPart = SMB_FID_QLOCK_LOW;
1932 LLength.HighPart = 0;
1933 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
1936 lock_ObtainWrite(&scp->rw);
1940 code = cm_SyncOp(scp, NULL, userp, &req, 0, CM_SCACHESYNC_LOCK);
1943 code = cm_Unlock(scp, sLockType, LOffset, LLength, key, 0, userp, &req);
1945 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
1947 if (code == CM_ERROR_RANGE_NOT_LOCKED)
1949 osi_Log3(afsd_logp, "RDR_CleanupFileEntry Range Not Locked -- FileAccess 0x%x ProcessId 0x%x HandleId 0x%x",
1950 CleanupCB->FileAccess, CleanupCB->ProcessId, CleanupCB->Identifier);
1956 if (bUnlockFile || bDeleteFile) {
1958 lock_ObtainWrite(&scp->rw);
1961 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1962 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
1964 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp (2) failure scp=0x%p code=0x%x",
1969 key = cm_GenerateKey(CM_SESSION_IFS, CleanupCB->ProcessId, 0);
1971 /* the scp is now locked and current */
1972 code = cm_UnlockByKey(scp, key,
1973 bDeleteFile ? CM_UNLOCK_FLAG_BY_FID : 0,
1976 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
1984 lock_ReleaseWrite(&dscp->rw);
1986 lock_ReleaseWrite(&scp->rw);
1988 if (code == 0 && dscp && bDeleteFile) {
1989 WCHAR FileName[260];
1991 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
1993 if (scp->fileType == CM_SCACHETYPE_DIRECTORY)
1994 code = cm_RemoveDir(dscp, NULL, FileName, userp, &req);
1996 code = cm_Unlink(dscp, NULL, FileName, userp, &req);
2000 if ( ResultBufferLength >= sizeof( AFSFileCleanupResultCB))
2002 (*ResultCB)->ResultBufferLength = sizeof( AFSFileCleanupResultCB);
2003 pResultCB = (AFSFileCleanupResultCB *)&(*ResultCB)->ResultData;
2004 pResultCB->ParentDataVersion.QuadPart = dscp ? dscp->dataVersion : 0;
2006 (*ResultCB)->ResultBufferLength = 0;
2009 (*ResultCB)->ResultStatus = 0;
2010 osi_Log0(afsd_logp, "RDR_CleanupFileEntry SUCCESS");
2012 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2013 (*ResultCB)->ResultStatus = status;
2014 osi_Log2(afsd_logp, "RDR_CleanupFileEntry FAILURE code=0x%x status=0x%x",
2019 cm_ReleaseSCache(scp);
2021 cm_ReleaseSCache(dscp);
2027 RDR_DeleteFileEntry( IN cm_user_t *userp,
2028 IN AFSFileID ParentId,
2029 IN ULONGLONG ProcessId,
2030 IN WCHAR *FileNameCounted,
2031 IN DWORD FileNameLength,
2034 IN DWORD ResultBufferLength,
2035 IN OUT AFSCommResult **ResultCB)
2038 AFSFileDeleteResultCB *pResultCB = NULL;
2039 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
2042 cm_scache_t * dscp = NULL;
2043 cm_scache_t * scp = NULL;
2044 afs_uint32 flags = 0;
2048 wchar_t FileName[260];
2051 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
2053 osi_Log4(afsd_logp, "RDR_DeleteFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2054 ParentId.Cell, ParentId.Volume,
2055 ParentId.Vnode, ParentId.Unique);
2056 osi_Log2(afsd_logp, "... name=%S checkOnly=%x",
2057 osi_LogSaveStringW(afsd_logp, FileName),
2060 RDR_InitReq(&req, bWow64);
2061 memset(&setAttr, 0, sizeof(cm_attr_t));
2063 *ResultCB = (AFSCommResult *)malloc( size);
2065 osi_Log0(afsd_logp, "RDR_DeleteFileEntry out of memory");
2073 parentFid.cell = ParentId.Cell;
2074 parentFid.volume = ParentId.Volume;
2075 parentFid.vnode = ParentId.Vnode;
2076 parentFid.unique = ParentId.Unique;
2077 parentFid.hash = ParentId.Hash;
2079 code = cm_GetSCache(&parentFid, NULL, &dscp, userp, &req);
2081 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2082 if ( status == STATUS_INVALID_HANDLE)
2083 status = STATUS_OBJECT_PATH_INVALID;
2084 (*ResultCB)->ResultStatus = status;
2085 osi_Log2(afsd_logp, "RDR_DeleteFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
2090 lock_ObtainWrite(&dscp->rw);
2092 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
2093 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2095 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2096 (*ResultCB)->ResultStatus = status;
2097 (*ResultCB)->ResultBufferLength = 0;
2098 lock_ReleaseWrite(&dscp->rw);
2099 cm_ReleaseSCache(dscp);
2100 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
2101 dscp, code, status);
2105 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2106 lock_ReleaseWrite(&dscp->rw);
2108 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2109 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2110 cm_ReleaseSCache(dscp);
2111 osi_Log1(afsd_logp, "RDR_DeleteFileEntry Not a Directory dscp=0x%p",
2116 code = cm_Lookup(dscp, FileName, 0, userp, &req, &scp);
2118 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2119 (*ResultCB)->ResultStatus = status;
2120 (*ResultCB)->ResultBufferLength = 0;
2121 cm_ReleaseSCache(dscp);
2122 osi_Log2(afsd_logp, "RDR_DeleteFileEntry cm_Lookup failure code=0x%x status=0x%x",
2127 lock_ObtainWrite(&scp->rw);
2128 code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_DELETE,
2129 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2131 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2132 (*ResultCB)->ResultStatus = status;
2133 (*ResultCB)->ResultBufferLength = 0;
2134 lock_ReleaseWrite(&scp->rw);
2135 cm_ReleaseSCache(scp);
2136 cm_ReleaseSCache(dscp);
2137 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2143 /* Drop all locks since the file is being deleted */
2144 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2145 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
2147 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2148 (*ResultCB)->ResultStatus = status;
2149 (*ResultCB)->ResultBufferLength = 0;
2150 lock_ReleaseWrite(&scp->rw);
2151 cm_ReleaseSCache(scp);
2152 cm_ReleaseSCache(dscp);
2153 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp Lock failure scp=0x%p code=0x%x status=0x%x",
2157 /* the scp is now locked and current */
2158 key = cm_GenerateKey(CM_SESSION_IFS, ProcessId, 0);
2160 code = cm_UnlockByKey(scp, key,
2161 CM_UNLOCK_FLAG_BY_FID,
2164 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
2165 lock_ReleaseWrite(&scp->rw);
2167 if (scp->fileType == CM_SCACHETYPE_DIRECTORY)
2168 code = cm_RemoveDir(dscp, NULL, FileName, userp, &req);
2170 code = cm_Unlink(dscp, NULL, FileName, userp, &req);
2172 lock_ReleaseWrite(&scp->rw);
2176 (*ResultCB)->ResultStatus = 0; // We will be able to fit all the data in here
2178 (*ResultCB)->ResultBufferLength = sizeof( AFSFileDeleteResultCB);
2180 pResultCB = (AFSFileDeleteResultCB *)(*ResultCB)->ResultData;
2182 pResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
2183 osi_Log0(afsd_logp, "RDR_DeleteFileEntry SUCCESS");
2185 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2186 (*ResultCB)->ResultStatus = status;
2187 (*ResultCB)->ResultBufferLength = 0;
2188 osi_Log2(afsd_logp, "RDR_DeleteFileEntry FAILURE code=0x%x status=0x%x",
2192 cm_ReleaseSCache(dscp);
2193 cm_ReleaseSCache(scp);
2199 RDR_RenameFileEntry( IN cm_user_t *userp,
2200 IN WCHAR *SourceFileNameCounted,
2201 IN DWORD SourceFileNameLength,
2202 IN AFSFileID SourceFileId,
2203 IN AFSFileRenameCB *pRenameCB,
2205 IN DWORD ResultBufferLength,
2206 IN OUT AFSCommResult **ResultCB)
2209 AFSFileRenameResultCB *pResultCB = NULL;
2210 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
2211 AFSFileID SourceParentId = pRenameCB->SourceParentId;
2212 AFSFileID TargetParentId = pRenameCB->TargetParentId;
2213 WCHAR * TargetFileNameCounted = pRenameCB->TargetName;
2214 DWORD TargetFileNameLength = pRenameCB->TargetNameLength;
2215 cm_fid_t SourceParentFid;
2216 cm_fid_t TargetParentFid;
2218 cm_fid_t OrigTargetFid = {0,0,0,0,0};
2220 cm_scache_t * oldDscp;
2221 cm_scache_t * newDscp;
2223 wchar_t shortName[13];
2224 wchar_t SourceFileName[260];
2225 wchar_t TargetFileName[260];
2231 RDR_InitReq(&req, bWow64);
2233 StringCchCopyNW(SourceFileName, 260, SourceFileNameCounted, SourceFileNameLength / sizeof(WCHAR));
2234 StringCchCopyNW(TargetFileName, 260, TargetFileNameCounted, TargetFileNameLength / sizeof(WCHAR));
2236 osi_Log4(afsd_logp, "RDR_RenameFileEntry Source Parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2237 SourceParentId.Cell, SourceParentId.Volume,
2238 SourceParentId.Vnode, SourceParentId.Unique);
2239 osi_Log2(afsd_logp, "... Source Name=%S Length %u", osi_LogSaveStringW(afsd_logp, SourceFileName), SourceFileNameLength);
2240 osi_Log4(afsd_logp, "... Target Parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2241 TargetParentId.Cell, TargetParentId.Volume,
2242 TargetParentId.Vnode, TargetParentId.Unique);
2243 osi_Log2(afsd_logp, "... Target Name=%S Length %u", osi_LogSaveStringW(afsd_logp, TargetFileName), TargetFileNameLength);
2245 *ResultCB = (AFSCommResult *)malloc( size);
2253 pResultCB = (AFSFileRenameResultCB *)(*ResultCB)->ResultData;
2255 if (SourceFileNameLength == 0 || TargetFileNameLength == 0)
2257 osi_Log2(afsd_logp, "RDR_RenameFileEntry Invalid Name Length: src %u target %u",
2258 SourceFileNameLength, TargetFileNameLength);
2259 (*ResultCB)->ResultStatus = STATUS_INVALID_PARAMETER;
2263 SourceParentFid.cell = SourceParentId.Cell;
2264 SourceParentFid.volume = SourceParentId.Volume;
2265 SourceParentFid.vnode = SourceParentId.Vnode;
2266 SourceParentFid.unique = SourceParentId.Unique;
2267 SourceParentFid.hash = SourceParentId.Hash;
2269 TargetParentFid.cell = TargetParentId.Cell;
2270 TargetParentFid.volume = TargetParentId.Volume;
2271 TargetParentFid.vnode = TargetParentId.Vnode;
2272 TargetParentFid.unique = TargetParentId.Unique;
2273 TargetParentFid.hash = TargetParentId.Hash;
2275 code = cm_GetSCache(&SourceParentFid, NULL, &oldDscp, userp, &req);
2277 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache source parent failed code 0x%x", code);
2278 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2279 if ( status == STATUS_INVALID_HANDLE)
2280 status = STATUS_OBJECT_PATH_INVALID;
2281 (*ResultCB)->ResultStatus = status;
2285 lock_ObtainWrite(&oldDscp->rw);
2286 code = cm_SyncOp(oldDscp, NULL, userp, &req, 0,
2287 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2289 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp oldDscp 0x%p failed code 0x%x", oldDscp, code);
2290 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2291 if ( status == STATUS_INVALID_HANDLE)
2292 status = STATUS_OBJECT_PATH_INVALID;
2293 (*ResultCB)->ResultStatus = status;
2294 lock_ReleaseWrite(&oldDscp->rw);
2295 cm_ReleaseSCache(oldDscp);
2299 cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2300 lock_ReleaseWrite(&oldDscp->rw);
2303 if (oldDscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2304 osi_Log1(afsd_logp, "RDR_RenameFileEntry oldDscp 0x%p not a directory", oldDscp);
2305 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2306 cm_ReleaseSCache(oldDscp);
2310 code = cm_GetSCache(&TargetParentFid, NULL, &newDscp, userp, &req);
2312 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache target parent failed code 0x%x", code);
2313 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2314 (*ResultCB)->ResultStatus = status;
2315 cm_ReleaseSCache(oldDscp);
2319 lock_ObtainWrite(&newDscp->rw);
2320 code = cm_SyncOp(newDscp, NULL, userp, &req, 0,
2321 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2323 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp newDscp 0x%p failed code 0x%x", newDscp, code);
2324 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2325 (*ResultCB)->ResultStatus = status;
2326 lock_ReleaseWrite(&newDscp->rw);
2327 cm_ReleaseSCache(oldDscp);
2328 cm_ReleaseSCache(newDscp);
2332 cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2333 lock_ReleaseWrite(&newDscp->rw);
2336 if (newDscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2337 osi_Log1(afsd_logp, "RDR_RenameFileEntry newDscp 0x%p not a directory", newDscp);
2338 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2339 cm_ReleaseSCache(oldDscp);
2340 cm_ReleaseSCache(newDscp);
2344 /* Obtain the original FID just for debugging purposes */
2345 code = cm_BeginDirOp( oldDscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
2347 code = cm_BPlusDirLookup(&dirop, SourceFileName, &SourceFid);
2348 code = cm_BPlusDirLookup(&dirop, TargetFileName, &OrigTargetFid);
2349 cm_EndDirOp(&dirop);
2352 code = cm_Rename( oldDscp, NULL, SourceFileName,
2353 newDscp, TargetFileName, userp, &req);
2355 cm_scache_t *scp = 0;
2358 (*ResultCB)->ResultBufferLength = ResultBufferLength;
2359 dwRemaining = ResultBufferLength - sizeof( AFSFileRenameResultCB) + sizeof( AFSDirEnumEntry);
2360 (*ResultCB)->ResultStatus = 0;
2362 pResultCB->SourceParentDataVersion.QuadPart = oldDscp->dataVersion;
2363 pResultCB->TargetParentDataVersion.QuadPart = newDscp->dataVersion;
2365 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_Rename oldDscp 0x%p newDscp 0x%p SUCCESS",
2368 code = cm_BeginDirOp( newDscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
2370 code = cm_BPlusDirLookup(&dirop, TargetFileName, &TargetFid);
2371 cm_EndDirOp(&dirop);
2375 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_BPlusDirLookup failed code 0x%x",
2377 (*ResultCB)->ResultStatus = STATUS_OBJECT_PATH_INVALID;
2378 cm_ReleaseSCache(oldDscp);
2379 cm_ReleaseSCache(newDscp);
2383 osi_Log4(afsd_logp, "RDR_RenameFileEntry Target FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2384 TargetFid.cell, TargetFid.volume,
2385 TargetFid.vnode, TargetFid.unique);
2387 code = cm_GetSCache(&TargetFid, &newDscp->fid, &scp, userp, &req);
2389 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache target failed code 0x%x", code);
2390 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2391 (*ResultCB)->ResultStatus = status;
2392 cm_ReleaseSCache(oldDscp);
2393 cm_ReleaseSCache(newDscp);
2397 /* Make sure the source vnode is current */
2398 lock_ObtainWrite(&scp->rw);
2399 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2400 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2402 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp scp 0x%p failed code 0x%x", scp, code);
2403 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2404 (*ResultCB)->ResultStatus = status;
2405 lock_ReleaseWrite(&scp->rw);
2406 cm_ReleaseSCache(oldDscp);
2407 cm_ReleaseSCache(newDscp);
2408 cm_ReleaseSCache(scp);
2412 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2413 lock_ReleaseWrite(&scp->rw);
2415 if (cm_shortNames) {
2416 dfid.vnode = htonl(scp->fid.vnode);
2417 dfid.unique = htonl(scp->fid.unique);
2419 if (!cm_Is8Dot3(TargetFileName))
2420 cm_Gen8Dot3NameIntW(TargetFileName, &dfid, shortName, NULL);
2422 shortName[0] = '\0';
2425 RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
2426 newDscp, scp, userp, &req, TargetFileName, shortName,
2427 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
2428 0, NULL, &dwRemaining);
2429 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
2430 cm_ReleaseSCache(scp);
2432 osi_Log0(afsd_logp, "RDR_RenameFileEntry SUCCESS");
2434 osi_Log3(afsd_logp, "RDR_RenameFileEntry cm_Rename oldDscp 0x%p newDscp 0x%p failed code 0x%x",
2435 oldDscp, newDscp, code);
2436 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2437 (*ResultCB)->ResultStatus = status;
2438 (*ResultCB)->ResultBufferLength = 0;
2441 cm_ReleaseSCache(oldDscp);
2442 cm_ReleaseSCache(newDscp);
2447 RDR_FlushFileEntry( IN cm_user_t *userp,
2448 IN AFSFileID FileId,
2450 IN DWORD ResultBufferLength,
2451 IN OUT AFSCommResult **ResultCB)
2453 cm_scache_t *scp = NULL;
2462 RDR_InitReq(&req, bWow64);
2464 osi_Log4(afsd_logp, "RDR_FlushFileEntry File FID cell 0x%x vol 0x%x vno 0x%x uniq 0x%x",
2465 FileId.Cell, FileId.Volume,
2466 FileId.Vnode, FileId.Unique);
2468 snprintf( dbgstr, 1024,
2469 "RDR_FlushFileEntry File FID cell 0x%x vol 0x%x vno 0x%x uniq 0x%x\n",
2470 FileId.Cell, FileId.Volume,
2471 FileId.Vnode, FileId.Unique);
2472 OutputDebugStringA( dbgstr);
2475 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult));
2477 osi_Log0(afsd_logp, "RDR_FlushFileEntry out of memory");
2483 sizeof( AFSCommResult));
2485 /* Process the release */
2486 Fid.cell = FileId.Cell;
2487 Fid.volume = FileId.Volume;
2488 Fid.vnode = FileId.Vnode;
2489 Fid.unique = FileId.Unique;
2490 Fid.hash = FileId.Hash;
2492 code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
2494 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2495 (*ResultCB)->ResultStatus = status;
2496 osi_Log2(afsd_logp, "RDR_FlushFileEntry cm_GetSCache FID failure code=0x%x status=0x%x",
2501 lock_ObtainWrite(&scp->rw);
2502 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2503 lock_ReleaseWrite(&scp->rw);
2504 (*ResultCB)->ResultStatus = STATUS_INVALID_HANDLE;
2508 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2509 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2511 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2512 (*ResultCB)->ResultStatus = status;
2513 lock_ReleaseWrite(&scp->rw);
2514 cm_ReleaseSCache(scp);
2515 osi_Log3(afsd_logp, "RDR_FlushFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2520 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2521 lock_ReleaseWrite(&scp->rw);
2523 code = cm_FSync(scp, userp, &req, FALSE);
2524 cm_ReleaseSCache(scp);
2527 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2528 (*ResultCB)->ResultStatus = status;
2529 osi_Log2(afsd_logp, "RDR_FlushFileEntry FAILURE code=0x%x status=0x%x",
2532 (*ResultCB)->ResultStatus = 0;
2533 osi_Log0(afsd_logp, "RDR_FlushFileEntry SUCCESS");
2535 (*ResultCB)->ResultBufferLength = 0;
2541 RDR_CheckAccess( IN cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
2545 ULONG afs_acc, afs_gr;
2547 afs_uint32 code = 0;
2549 file = (scp->fileType == CM_SCACHETYPE_FILE);
2552 /* access definitions from prs_fs.h */
2554 if (access & FILE_READ_DATA)
2555 afs_acc |= PRSFS_READ;
2556 if (access & FILE_READ_EA || access & FILE_READ_ATTRIBUTES)
2557 afs_acc |= PRSFS_READ;
2558 if (file && ((access & FILE_WRITE_DATA) || (access & FILE_APPEND_DATA)))
2559 afs_acc |= PRSFS_WRITE;
2560 if (access & FILE_WRITE_EA || access & FILE_WRITE_ATTRIBUTES)
2561 afs_acc |= PRSFS_WRITE;
2562 if (dir && ((access & FILE_ADD_FILE) || (access & FILE_ADD_SUBDIRECTORY)))
2563 afs_acc |= PRSFS_INSERT;
2564 if (dir && (access & FILE_LIST_DIRECTORY))
2565 afs_acc |= PRSFS_LOOKUP;
2566 if (file && (access & FILE_EXECUTE))
2567 afs_acc |= PRSFS_WRITE;
2568 if (dir && (access & FILE_TRAVERSE))
2569 afs_acc |= PRSFS_READ;
2570 if (dir && (access & FILE_DELETE_CHILD))
2571 afs_acc |= PRSFS_DELETE;
2572 if ((access & DELETE))
2573 afs_acc |= PRSFS_DELETE;
2575 /* check ACL with server */
2576 lock_ObtainWrite(&scp->rw);
2579 if (cm_HaveAccessRights(scp, userp, reqp, afs_acc, &afs_gr))
2585 /* we don't know the required access rights */
2586 code = cm_GetAccessRights(scp, userp, reqp);
2592 lock_ReleaseWrite(&(scp->rw));
2596 if (afs_gr & PRSFS_READ)
2597 *granted |= FILE_READ_DATA | FILE_READ_EA | FILE_READ_ATTRIBUTES | FILE_EXECUTE;
2598 if (afs_gr & PRSFS_WRITE)
2599 *granted |= FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES | FILE_EXECUTE;
2600 if (afs_gr & PRSFS_INSERT)
2601 *granted |= (dir ? FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY : 0) | (file ? FILE_ADD_SUBDIRECTORY : 0);
2602 if (afs_gr & PRSFS_LOOKUP)
2603 *granted |= (dir ? FILE_LIST_DIRECTORY : 0);
2604 if (afs_gr & PRSFS_DELETE)
2605 *granted |= FILE_DELETE_CHILD | DELETE;
2606 if (afs_gr & PRSFS_LOCK)
2608 if (afs_gr & PRSFS_ADMINISTER)
2611 *granted |= SYNCHRONIZE | READ_CONTROL;
2613 /* don't give more access than what was requested */
2615 osi_Log3(afsd_logp, "RDR_CheckAccess SUCCESS scp=0x%p requested=0x%x granted=0x%x", scp, access, *granted);
2617 osi_Log2(afsd_logp, "RDR_CheckAccess FAILURE scp=0x%p code=0x%x",
2624 RDR_OpenFileEntry( IN cm_user_t *userp,
2625 IN AFSFileID FileId,
2626 IN AFSFileOpenCB *OpenCB,
2629 IN DWORD ResultBufferLength,
2630 IN OUT AFSCommResult **ResultCB)
2632 AFSFileOpenResultCB *pResultCB = NULL;
2633 cm_scache_t *scp = NULL;
2634 cm_user_t *sysUserp = NULL;
2636 cm_lock_data_t *ldp = NULL;
2641 RDR_InitReq(&req, bWow64);
2643 osi_Log4(afsd_logp, "RDR_OpenFileEntry File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2644 FileId.Cell, FileId.Volume,
2645 FileId.Vnode, FileId.Unique);
2647 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult) + sizeof( AFSFileOpenResultCB));
2649 osi_Log0(afsd_logp, "RDR_OpenFileEntry out of memory");
2655 sizeof( AFSCommResult) + sizeof( AFSFileOpenResultCB));
2657 pResultCB = (AFSFileOpenResultCB *)(*ResultCB)->ResultData;
2659 /* Process the release */
2660 Fid.cell = FileId.Cell;
2661 Fid.volume = FileId.Volume;
2662 Fid.vnode = FileId.Vnode;
2663 Fid.unique = FileId.Unique;
2664 Fid.hash = FileId.Hash;
2666 code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
2668 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2669 (*ResultCB)->ResultStatus = status;
2670 osi_Log2(afsd_logp, "RDR_OpenFileEntry cm_GetSCache FID failure code=0x%x status=0x%x",
2675 lock_ObtainWrite(&scp->rw);
2676 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2677 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2679 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2680 (*ResultCB)->ResultStatus = status;
2681 lock_ReleaseWrite(&scp->rw);
2682 cm_ReleaseSCache(scp);
2683 osi_Log3(afsd_logp, "RDR_OpenFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2688 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2689 lock_ReleaseWrite(&scp->rw);
2691 sysUserp = RDR_GetLocalSystemUser();
2694 * Skip the open check if the request is coming from the local system account.
2695 * The local system has no tokens and therefore any requests sent to a file
2696 * server will fail. Unfortunately, there are special system processes that
2697 * perform actions on files and directories in preparation for memory mapping
2698 * executables. If the open check fails, the real request from the user process
2699 * will never be issued.
2701 * Permitting the file system to allow subsequent operations to proceed does
2702 * not compromise security. All requests to obtain file data or directory
2703 * enumerations will subsequently fail if they are not submitted under the
2704 * context of a process for that have access to the necessary credentials.
2707 if ( userp == sysUserp)
2709 osi_Log1(afsd_logp, "RDR_OpenFileEntry LOCAL_SYSTEM access check skipped scp=0x%p",
2711 pResultCB->GrantedAccess = OpenCB->DesiredAccess;
2712 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
2723 "RDR_OpenFileEntry repeating open check scp=0x%p userp=0x%p code=0x%x",
2726 code = cm_CheckNTOpen(scp, OpenCB->DesiredAccess, OpenCB->ShareAccess,
2728 OpenCB->ProcessId, OpenCB->Identifier,
2731 code = RDR_CheckAccess(scp, userp, &req, OpenCB->DesiredAccess, &pResultCB->GrantedAccess);
2732 cm_CheckNTOpenDone(scp, userp, &req, &ldp);
2733 } while (count < 100 && (code == CM_ERROR_RETRY || code == CM_ERROR_WOULDBLOCK));
2737 * If we are restricting sharing, we should do so with a suitable
2740 if (code == 0 && scp->fileType == CM_SCACHETYPE_FILE && !(OpenCB->ShareAccess & FILE_SHARE_WRITE)) {
2742 LARGE_INTEGER LOffset, LLength;
2745 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
2746 LOffset.LowPart = SMB_FID_QLOCK_LOW;
2747 LLength.HighPart = 0;
2748 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
2751 * If we are not opening the file for writing, then we don't
2752 * try to get an exclusive lock. No one else should be able to
2753 * get an exclusive lock on the file anyway, although someone
2754 * else can get a shared lock.
2756 if ((OpenCB->ShareAccess & FILE_SHARE_READ) || !(OpenCB->DesiredAccess & AFS_ACCESS_WRITE))
2758 sLockType = LOCKING_ANDX_SHARED_LOCK;
2763 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, OpenCB->Identifier);
2765 lock_ObtainWrite(&scp->rw);
2766 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, &req, NULL);
2767 lock_ReleaseWrite(&scp->rw);
2770 code = CM_ERROR_SHARING_VIOLATION;
2771 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
2773 if (sLockType == LOCKING_ANDX_SHARED_LOCK)
2774 pResultCB->FileAccess = AFS_FILE_ACCESS_SHARED;
2776 pResultCB->FileAccess = AFS_FILE_ACCESS_EXCLUSIVE;
2779 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
2782 cm_ReleaseUser(sysUserp);
2783 if (code == 0 && bHoldFid)
2784 RDR_FlagScpInUse( scp, FALSE );
2785 cm_ReleaseSCache(scp);
2788 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2789 (*ResultCB)->ResultStatus = status;
2790 osi_Log2(afsd_logp, "RDR_OpenFileEntry FAILURE code=0x%x status=0x%x",
2793 (*ResultCB)->ResultStatus = 0;
2794 (*ResultCB)->ResultBufferLength = sizeof( AFSFileOpenResultCB);
2795 osi_Log0(afsd_logp, "RDR_OpenFileEntry SUCCESS");
2801 RDR_ReleaseFileAccess( IN cm_user_t *userp,
2802 IN AFSFileID FileId,
2803 IN AFSFileAccessReleaseCB *ReleaseFileCB,
2805 IN DWORD ResultBufferLength,
2806 IN OUT AFSCommResult **ResultCB)
2809 unsigned int sLockType;
2810 LARGE_INTEGER LOffset, LLength;
2811 cm_scache_t *scp = NULL;
2817 RDR_InitReq(&req, bWow64);
2819 osi_Log4(afsd_logp, "RDR_ReleaseFileAccess File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2820 FileId.Cell, FileId.Volume,
2821 FileId.Vnode, FileId.Unique);
2823 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult));
2825 osi_Log0(afsd_logp, "RDR_ReleaseFileAccess out of memory");
2829 memset( *ResultCB, '\0', sizeof( AFSCommResult));
2831 if (ReleaseFileCB->FileAccess == AFS_FILE_ACCESS_NOLOCK)
2834 /* Process the release */
2835 Fid.cell = FileId.Cell;
2836 Fid.volume = FileId.Volume;
2837 Fid.vnode = FileId.Vnode;
2838 Fid.unique = FileId.Unique;
2839 Fid.hash = FileId.Hash;
2841 code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
2843 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2844 (*ResultCB)->ResultStatus = status;
2845 osi_Log2(afsd_logp, "RDR_ReleaseFileAccess cm_GetSCache FID failure code=0x%x status=0x%x",
2850 if (ReleaseFileCB->FileAccess == AFS_FILE_ACCESS_SHARED)
2851 sLockType = LOCKING_ANDX_SHARED_LOCK;
2855 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, ReleaseFileCB->Identifier);
2857 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
2858 LOffset.LowPart = SMB_FID_QLOCK_LOW;
2859 LLength.HighPart = 0;
2860 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
2862 lock_ObtainWrite(&scp->rw);
2864 code = cm_SyncOp(scp, NULL, userp, &req, 0, CM_SCACHESYNC_LOCK);
2867 code = cm_Unlock(scp, sLockType, LOffset, LLength, key, 0, userp, &req);
2869 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
2871 if (code == CM_ERROR_RANGE_NOT_LOCKED)
2873 osi_Log3(afsd_logp, "RDR_ReleaseFileAccess Range Not Locked -- FileAccess 0x%x ProcessId 0x%x HandleId 0x%x",
2874 ReleaseFileCB->FileAccess, ReleaseFileCB->ProcessId, ReleaseFileCB->Identifier);
2878 lock_ReleaseWrite(&scp->rw);
2880 osi_Log0(afsd_logp, "RDR_ReleaseFileAccessEntry SUCCESS");
2884 HexCheckSum(unsigned char * buf, int buflen, unsigned char * md5cksum)
2887 static char tr[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
2890 return "buffer length too small to HexCheckSum";
2892 for (i=0;i<16;i++) {
2895 buf[i*2] = tr[k / 16];
2896 buf[i*2+1] = tr[k % 16];
2904 * Extent requests from the file system are triggered when a file
2905 * page is not resident in the Windows cache. The file system is
2906 * responsible for loading the page but cannot block the request
2907 * while doing so. The AFS Redirector forwards the requests to
2908 * the AFS cache manager while indicating to Windows that the page
2909 * is not yet available. A polling operation will then ensue with
2910 * the AFS Redirector issuing a RDR_RequestFileExtentsXXX call for
2911 * each poll attempt. As each request is received and processed
2912 * by a separate worker thread in the service, this can lead to
2913 * contention by multiple threads attempting to claim the same
2914 * cm_buf_t objects. Therefore, it is important that
2916 * (a) the service avoid processing more than one overlapping
2917 * extent request at a time
2918 * (b) background daemon processing be used to avoid blocking
2921 * Beginning with the 20091122 build of the redirector, the redirector
2922 * will not issue an additional RDR_RequestFileExtentsXXX call for
2923 * each poll request. Instead, afsd_service is required to track
2924 * the requests and return them to the redirector or fail the
2925 * portions of the request that cannot be satisfied.
2927 * The request processing returns any extents that can be returned
2928 * immediately to the redirector. The rest of the requested range(s)
2929 * are queued as background operations using RDR_BkgFetch().
2932 /* do the background fetch. */
2934 RDR_BkgFetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
2935 cm_user_t *userp, cm_req_t *reqp)
2941 osi_hyper_t fetched;
2942 osi_hyper_t tblocksize;
2945 cm_buf_t *bufp = NULL;
2946 DWORD dwResultBufferLength;
2947 AFSSetFileExtentsCB *pResultCB;
2951 int reportErrorToRedir = 0;
2952 int force_retry = 0;
2954 FileId.Cell = scp->fid.cell;
2955 FileId.Volume = scp->fid.volume;
2956 FileId.Vnode = scp->fid.vnode;
2957 FileId.Unique = scp->fid.unique;
2958 FileId.Hash = scp->fid.hash;
2960 fetched.LowPart = 0;
2961 fetched.HighPart = 0;
2962 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
2965 length.LowPart = p3;
2966 length.HighPart = p4;
2968 end = LargeIntegerAdd(base, length);
2970 osi_Log5(afsd_logp, "Starting BKG Fetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
2971 scp, p2, p1, p4, p3);
2974 * Make sure we have a callback.
2975 * This is necessary so that we can return access denied
2976 * if a callback cannot be granted.
2978 lock_ObtainWrite(&scp->rw);
2979 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_READ,
2980 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2982 lock_ReleaseWrite(&scp->rw);
2983 osi_Log2(afsd_logp, "RDR_BkgFetch cm_SyncOp failure scp=0x%p code=0x%x",
2985 smb_MapNTError(cm_MapRPCError(code, reqp), &status, TRUE);
2986 RDR_SetFileStatus( &scp->fid, &userp->authgroup, status);
2989 lock_ReleaseWrite(&scp->rw);
2991 dwResultBufferLength = (DWORD)(sizeof( AFSSetFileExtentsCB) + sizeof( AFSSetFileExtentsCB) * (length.QuadPart / cm_data.blockSize + 1));
2992 pResultCB = (AFSSetFileExtentsCB *)malloc( dwResultBufferLength );
2994 return CM_ERROR_RETRY;
2996 memset( pResultCB, '\0', dwResultBufferLength );
2997 pResultCB->FileId = FileId;
2999 for ( code = 0, offset = base;
3000 code == 0 && LargeIntegerLessThan(offset, end);
3001 offset = LargeIntegerAdd(offset, tblocksize) )
3003 int bBufRelease = TRUE;
3006 lock_ReleaseWrite(&scp->rw);
3010 code = buf_Get(scp, &offset, reqp, &bufp);
3013 * any error from buf_Get() is non-fatal.
3014 * we need to re-queue this extent fetch.
3021 lock_ObtainWrite(&scp->rw);
3025 code = cm_GetBuffer(scp, bufp, NULL, userp, reqp);
3027 if (!(bufp->qFlags & CM_BUF_QREDIR)) {
3028 #ifdef VALIDATE_CHECK_SUM
3034 if (bufp->flags & CM_BUF_DIRTY)
3035 cm_BufWrite(scp, &bufp->offset, cm_data.buf_blockSize, CM_BUF_WRITE_SCP_LOCKED, userp, reqp);
3037 lock_ObtainWrite(&buf_globalLock);
3038 if (!(bufp->flags & CM_BUF_DIRTY) &&
3039 bufp->cmFlags == 0 &&
3040 !(bufp->qFlags & CM_BUF_QREDIR)) {
3041 buf_InsertToRedirQueue(scp, bufp);
3042 lock_ReleaseWrite(&buf_globalLock);
3044 #ifdef VALIDATE_CHECK_SUM
3045 buf_ComputeCheckSum(bufp);
3047 pResultCB->FileExtents[count].Flags = 0;
3048 pResultCB->FileExtents[count].FileOffset.QuadPart = bufp->offset.QuadPart;
3049 pResultCB->FileExtents[count].CacheOffset.QuadPart = bufp->datap - RDR_extentBaseAddress;
3050 pResultCB->FileExtents[count].Length = cm_data.blockSize;
3052 fetched = LargeIntegerAdd(fetched, tblocksize);
3053 bBufRelease = FALSE;
3055 #ifdef VALIDATE_CHECK_SUM
3057 HexCheckSum(md5dbg, sizeof(md5dbg), bufp->md5cksum);
3058 snprintf( dbgstr, 1024,
3059 "RDR_BkgFetch md5 %s vol 0x%x vno 0x%x uniq 0x%x foffset 0x%x:%x coffset 0x%x:%x\n",
3061 scp->fid.volume, scp->fid.vnode, scp->fid.unique,
3062 pResultCB->FileExtents[count].FileOffset.HighPart,
3063 pResultCB->FileExtents[count].FileOffset.LowPart,
3064 pResultCB->FileExtents[count].CacheOffset.HighPart,
3065 pResultCB->FileExtents[count].CacheOffset.LowPart);
3066 OutputDebugStringA( dbgstr);
3069 osi_Log4(afsd_logp, "RDR_BkgFetch Extent2FS bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3070 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3072 lock_ReleaseWrite(&buf_globalLock);
3073 if ((bufp->cmFlags != 0) || (bufp->flags & CM_BUF_DIRTY)) {
3074 /* An I/O operation is already in progress */
3076 osi_Log4(afsd_logp, "RDR_BkgFetch Extent2FS Not delivering to Redirector Dirty or Busy bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3077 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3079 osi_Log4(afsd_logp, "RDR_BkgFetch Extent2FS Already held by Redirector bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3080 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3084 osi_Log4(afsd_logp, "RDR_BkgFetch Extent2FS Already held by Redirector bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3085 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3089 * depending on what the error from cm_GetBuffer is
3090 * it may or may not be fatal. Only return fatal errors.
3091 * Re-queue a request for others.
3093 osi_Log5(afsd_logp, "RDR_BkgFetch Extent2FS FAILURE bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x code 0x%x",
3094 bufp, offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize, code);
3096 case CM_ERROR_NOACCESS:
3097 case CM_ERROR_NOSUCHFILE:
3098 case CM_ERROR_NOSUCHPATH:
3099 case CM_ERROR_NOSUCHVOLUME:
3100 case CM_ERROR_NOSUCHCELL:
3101 case CM_ERROR_INVAL:
3102 case CM_ERROR_BADFD:
3103 case CM_ERROR_CLOCKSKEW:
3105 case CM_ERROR_QUOTA:
3106 case CM_ERROR_LOCK_CONFLICT:
3108 * these are fatal errors. deliver what we can
3111 reportErrorToRedir = 1;
3115 * non-fatal errors. re-queue the exent
3117 code = CM_ERROR_RETRY;
3127 lock_ObtainWrite(&scp->rw);
3131 /* wakeup anyone who is waiting */
3132 if (scp->flags & CM_SCACHEFLAG_WAITING) {
3133 osi_Log1(afsd_logp, "RDR Bkg Fetch Waking scp 0x%p", scp);
3134 osi_Wakeup((LONG_PTR) &scp->flags);
3136 lock_ReleaseWrite(&scp->rw);
3139 pResultCB->ExtentCount = count;
3140 RDR_SetFileExtents( pResultCB, dwResultBufferLength);
3144 if (reportErrorToRedir) {
3145 smb_MapNTError(cm_MapRPCError(code, reqp), &status, TRUE);
3146 RDR_SetFileStatus( &scp->fid, &userp->authgroup, status);
3149 osi_Log4(afsd_logp, "Ending BKG Fetch scp 0x%p code 0x%x fetched 0x%x:%x",
3150 scp, code, fetched.HighPart, fetched.LowPart);
3152 return force_retry ? CM_ERROR_RETRY : code;
3157 RDR_RequestFileExtentsAsync( IN cm_user_t *userp,
3158 IN AFSFileID FileId,
3159 IN AFSRequestExtentsCB *RequestExtentsCB,
3161 IN OUT DWORD * ResultBufferLength,
3162 IN OUT AFSSetFileExtentsCB **ResultCB)
3164 AFSSetFileExtentsCB *pResultCB = NULL;
3168 cm_scache_t *scp = NULL;
3171 afs_uint32 code = 0;
3173 LARGE_INTEGER ByteOffset, BeginOffset, EndOffset, QueueOffset;
3174 afs_uint32 QueueLength;
3176 BOOLEAN bBufRelease = TRUE;
3178 RDR_InitReq(&req, bWow64);
3179 req.flags |= CM_REQ_NORETRY;
3181 osi_Log4(afsd_logp, "RDR_RequestFileExtentsAsync File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
3182 FileId.Cell, FileId.Volume,
3183 FileId.Vnode, FileId.Unique);
3184 osi_Log4(afsd_logp, "... Flags 0x%x ByteOffset 0x%x:%x Length 0x%x",
3185 RequestExtentsCB->Flags,
3186 RequestExtentsCB->ByteOffset.HighPart, RequestExtentsCB->ByteOffset.LowPart,
3187 RequestExtentsCB->Length);
3188 Length = sizeof( AFSSetFileExtentsCB) + sizeof( AFSFileExtentCB) * (RequestExtentsCB->Length / cm_data.blockSize + 1);
3190 pResultCB = *ResultCB = (AFSSetFileExtentsCB *)malloc( Length );
3191 if (*ResultCB == NULL) {
3192 *ResultBufferLength = 0;
3195 *ResultBufferLength = Length;
3197 memset( pResultCB, '\0', Length );
3198 pResultCB->FileId = FileId;
3200 Fid.cell = FileId.Cell;
3201 Fid.volume = FileId.Volume;
3202 Fid.vnode = FileId.Vnode;
3203 Fid.unique = FileId.Unique;
3204 Fid.hash = FileId.Hash;
3206 code = cm_GetSCache(&Fid, NULL, &scp, userp, &req);
3208 osi_Log1(afsd_logp, "RDR_RequestFileExtentsAsync cm_GetSCache FID failure code=0x%x",
3210 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3215 * Make sure we have a callback.
3216 * This is necessary so that we can return access denied
3217 * if a callback cannot be granted.
3219 lock_ObtainWrite(&scp->rw);
3220 code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_READ,
3221 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
3222 lock_ReleaseWrite(&scp->rw);
3224 cm_ReleaseSCache(scp);
3225 osi_Log2(afsd_logp, "RDR_RequestFileExtentsAsync cm_SyncOp failure scp=0x%p code=0x%x",
3227 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3228 RDR_SetFileStatus( &scp->fid, &userp->authgroup, status);
3232 /* Allocate the extents from the buffer package */
3234 ByteOffset = BeginOffset = RequestExtentsCB->ByteOffset,
3235 EndOffset.QuadPart = ByteOffset.QuadPart + RequestExtentsCB->Length;
3236 code == 0 && ByteOffset.QuadPart < EndOffset.QuadPart;
3237 ByteOffset.QuadPart += cm_data.blockSize)
3239 BOOL bHaveBuffer = FALSE;
3242 thyper.QuadPart = ByteOffset.QuadPart;
3244 code = buf_Get(scp, &thyper, &req, &bufp);
3246 lock_ObtainMutex(&bufp->mx);
3249 if (bufp->qFlags & CM_BUF_QREDIR) {
3251 } else if (bufp->flags & CM_BUF_DIRTY) {
3252 bHaveBuffer = FALSE;
3254 code = buf_CleanAsyncLocked(scp, bufp, &req, 0, NULL);
3259 case CM_ERROR_RETRY:
3260 /* Couldn't flush it, obtain it asynchronously so we don't block the thread. */
3261 bHaveBuffer = FALSE;
3265 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3266 RDR_SetFileStatus(&FileId, &userp->authgroup, status);
3267 bHaveBuffer = FALSE;
3272 osi_hyper_t minLength; /* effective end of file */
3274 lock_ObtainRead(&scp->rw);
3275 bHaveBuffer = cm_HaveBuffer(scp, bufp, TRUE);
3277 if (LargeIntegerGreaterThan(scp->length, scp->serverLength))
3278 minLength = scp->serverLength;
3280 minLength = scp->length;
3282 if (LargeIntegerGreaterThanOrEqualTo(bufp->offset, minLength)) {
3284 memset(bufp->datap, 0, cm_data.buf_blockSize);
3285 bufp->dataVersion = scp->dataVersion;
3288 else if (bufp->dataVersion == CM_BUF_VERSION_BAD) {
3289 bufp->dataVersion = scp->dataVersion;
3292 else if ((RequestExtentsCB->Flags & AFS_EXTENT_FLAG_CLEAN) &&
3293 ByteOffset.QuadPart <= bufp->offset.QuadPart &&
3294 EndOffset.QuadPart >= bufp->offset.QuadPart + cm_data.blockSize)
3296 memset(bufp->datap, 0, cm_data.blockSize);
3297 bufp->dataVersion = scp->dataVersion;
3298 buf_SetDirty(bufp, &req, 0, cm_data.blockSize, userp);
3301 lock_ReleaseRead(&scp->rw);
3305 * if this buffer is already up to date, skip it.
3308 if (ByteOffset.QuadPart == BeginOffset.QuadPart) {
3309 BeginOffset.QuadPart += cm_data.blockSize;
3311 QueueLength = (afs_uint32)(ByteOffset.QuadPart - BeginOffset.QuadPart);
3312 QueueOffset = BeginOffset;
3313 BeginOffset = ByteOffset;
3316 if (!(bufp->qFlags & CM_BUF_QREDIR)) {
3317 #ifdef VALIDATE_CHECK_SUM
3323 lock_ObtainWrite(&buf_globalLock);
3324 if (!(bufp->qFlags & CM_BUF_QREDIR)) {