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)
86 reqp->flags |= CM_REQ_SOURCE_REDIR;
90 RDR_fid2FID( cm_fid_t *fid, AFSFileID *FileId)
92 FileId->Cell = fid->cell;
93 FileId->Volume = fid->volume;
94 FileId->Vnode = fid->vnode;
95 FileId->Unique = fid->unique;
96 FileId->Hash = fid->hash;
100 RDR_FID2fid( AFSFileID *FileId, cm_fid_t *fid)
102 fid->cell = FileId->Cell;
103 fid->volume = FileId->Volume;
104 fid->vnode = FileId->Vnode;
105 fid->unique = FileId->Unique;
106 fid->hash = FileId->Hash;
110 RDR_SetInitParams( OUT AFSRedirectorInitInfo **ppRedirInitInfo, OUT DWORD * pRedirInitInfoLen )
112 extern char cm_CachePath[];
113 extern cm_config_data_t cm_data;
114 extern int smb_hideDotFiles;
115 size_t cm_CachePathLen = strlen(cm_CachePath);
117 DWORD TempPathLen = ExpandEnvironmentStringsW(L"%TEMP%", NULL, 0);
118 MEMORYSTATUSEX memStatus;
119 DWORD maxMemoryCacheSize;
121 memStatus.dwLength = sizeof(memStatus);
122 if (GlobalMemoryStatusEx(&memStatus)) {
124 * Use the memory extent interface in the afs redirector
125 * whenever the cache size is less than equal to 10% of
126 * physical memory. Do not use too much because this memory
127 * will be locked by the redirector so it can't be swapped
130 maxMemoryCacheSize = (DWORD)(memStatus.ullTotalPhys / 1024 / 10);
133 * If we can't determine the amount of physical memory
134 * in the system, be conservative and limit the use of
135 * memory extent interface to 64MB data caches.
137 maxMemoryCacheSize = 65536;
140 *pRedirInitInfoLen = (DWORD) (sizeof(AFSRedirectorInitInfo) + (cm_CachePathLen + TempPathLen) * sizeof(WCHAR));
141 *ppRedirInitInfo = (AFSRedirectorInitInfo *)malloc(*pRedirInitInfoLen);
142 (*ppRedirInitInfo)->Flags = smb_hideDotFiles ? AFS_REDIR_INIT_FLAG_HIDE_DOT_FILES : 0;
143 (*ppRedirInitInfo)->MaximumChunkLength = cm_data.chunkSize;
144 (*ppRedirInitInfo)->GlobalFileId.Cell = cm_data.rootFid.cell;
145 (*ppRedirInitInfo)->GlobalFileId.Volume = cm_data.rootFid.volume;
146 (*ppRedirInitInfo)->GlobalFileId.Vnode = cm_data.rootFid.vnode;
147 (*ppRedirInitInfo)->GlobalFileId.Unique = cm_data.rootFid.unique;
148 (*ppRedirInitInfo)->GlobalFileId.Hash = cm_data.rootFid.hash;
149 (*ppRedirInitInfo)->ExtentCount.QuadPart = cm_data.buf_nbuffers;
150 (*ppRedirInitInfo)->CacheBlockSize = cm_data.blockSize;
151 (*ppRedirInitInfo)->MaxPathLinkCount = 512; /* this needs to become a registry value */
152 (*ppRedirInitInfo)->NameArrayLength = 32; /* this needs to become a registry value */
153 if (cm_virtualCache || cm_data.bufferSize <= maxMemoryCacheSize) {
154 osi_Log0(afsd_logp, "RDR_SetInitParams Initializing Memory Extent Interface");
155 (*ppRedirInitInfo)->MemoryCacheOffset.QuadPart = (LONGLONG)cm_data.bufDataBaseAddress;
156 (*ppRedirInitInfo)->MemoryCacheLength.QuadPart = cm_data.bufEndOfData - cm_data.bufDataBaseAddress;
157 (*ppRedirInitInfo)->CacheFileNameLength = 0;
158 RDR_extentBaseAddress = cm_data.bufDataBaseAddress;
160 (*ppRedirInitInfo)->MemoryCacheOffset.QuadPart = 0;
161 (*ppRedirInitInfo)->MemoryCacheLength.QuadPart = 0;
162 (*ppRedirInitInfo)->CacheFileNameLength = (ULONG) (cm_CachePathLen * sizeof(WCHAR));
163 err = mbstowcs((*ppRedirInitInfo)->CacheFileName, cm_CachePath, (cm_CachePathLen + 1) *sizeof(WCHAR));
165 free(*ppRedirInitInfo);
166 osi_Log0(afsd_logp, "RDR_SetInitParams Invalid Object Name");
167 return STATUS_OBJECT_NAME_INVALID;
169 RDR_extentBaseAddress = cm_data.baseAddress;
171 (*ppRedirInitInfo)->DumpFileLocationOffset = FIELD_OFFSET(AFSRedirectorInitInfo, CacheFileName) + (*ppRedirInitInfo)->CacheFileNameLength;
172 (*ppRedirInitInfo)->DumpFileLocationLength = (TempPathLen - 1) * sizeof(WCHAR);
173 ExpandEnvironmentStringsW(L"%TEMP%",
174 (LPWSTR)(((PBYTE)(*ppRedirInitInfo)) + (*ppRedirInitInfo)->DumpFileLocationOffset),
177 osi_Log0(afsd_logp,"RDR_SetInitParams Success");
182 RDR_GetLocalSystemUser( void)
185 cm_user_t *userp = NULL;
186 wchar_t cname[MAX_COMPUTERNAME_LENGTH+1];
187 int cnamelen = MAX_COMPUTERNAME_LENGTH+1;
189 GetComputerNameW(cname, &cnamelen);
192 unp = smb_FindUserByName(NTSID_LOCAL_SYSTEM, cname, SMB_FLAG_CREATE);
193 lock_ObtainMutex(&unp->mx);
195 unp->userp = cm_NewUser();
196 unp->flags |= SMB_USERNAMEFLAG_SID;
197 lock_ReleaseMutex(&unp->mx);
200 smb_ReleaseUsername(unp);
203 userp = cm_rootUserp;
211 RDR_UserFromCommRequest( IN AFSCommRequest *RequestBuffer)
214 return RDR_UserFromAuthGroup( &RequestBuffer->AuthGroup);
218 RDR_UserFromAuthGroup( IN GUID *pGuid)
221 cm_user_t * userp = NULL;
222 RPC_WSTR UuidString = NULL;
223 wchar_t cname[MAX_COMPUTERNAME_LENGTH+1];
224 int cnamelen = MAX_COMPUTERNAME_LENGTH+1;
226 if (UuidToStringW((UUID *)pGuid, &UuidString) != RPC_S_OK)
229 GetComputerNameW(cname, &cnamelen);
232 unp = smb_FindUserByName(UuidString, cname, SMB_FLAG_CREATE);
233 lock_ObtainMutex(&unp->mx);
235 unp->userp = cm_NewUser();
236 memcpy(&unp->userp->authgroup, pGuid, sizeof(GUID));
238 unp->flags |= SMB_USERNAMEFLAG_SID;
239 lock_ReleaseMutex(&unp->mx);
242 smb_ReleaseUsername(unp);
246 userp = cm_rootUserp;
250 osi_Log2(afsd_logp, "RDR_UserFromCommRequest Guid %S userp = 0x%p",
251 osi_LogSaveStringW(afsd_logp, UuidString),
255 RpcStringFreeW(&UuidString);
261 RDR_ReleaseUser( IN cm_user_t *userp )
263 osi_Log1(afsd_logp, "RDR_ReleaseUser userp = 0x%p", userp);
264 cm_ReleaseUser(userp);
269 * RDR_FlagScpInUse flags the scp with CM_SCACHEFLAG_RDR_IN_USE
272 RDR_FlagScpInUse( IN cm_scache_t *scp, IN BOOL bLocked )
275 lock_ObtainWrite(&scp->rw);
277 lock_AssertWrite(&scp->rw);
278 scp->flags |= CM_SCACHEFLAG_RDR_IN_USE;
281 lock_ReleaseWrite(&scp->rw);
285 * Obtain the status information for the specified object and
289 RDR_BulkStatLookup( cm_scache_t *dscp,
294 cm_direnum_t * enump = NULL;
298 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
300 code = cm_BPlusDirEnumerate(dscp, userp, reqp, TRUE, NULL, TRUE, &enump);
302 osi_Log1(afsd_logp, "RDR_BulkStatLookup cm_BPlusDirEnumerate failure code=0x%x",
307 osi_Log1(afsd_logp, "RDR_BulkStatLookup cm_BeginDirOp failure code=0x%x",
314 code = cm_BPlusDirEnumBulkStatOne(enump, scp);
316 osi_Log1(afsd_logp, "RDR_BulkStatLookup cm_BPlusDirEnumBulkStatOne failure code=0x%x",
319 cm_BPlusDirFreeEnumeration(enump);
326 #define RDR_POP_FOLLOW_MOUNTPOINTS 0x01
327 #define RDR_POP_EVALUATE_SYMLINKS 0x02
328 #define RDR_POP_WOW64 0x04
329 #define RDR_POP_NO_GETSTATUS 0x08
332 RDR_PopulateCurrentEntry( IN AFSDirEnumEntry * pCurrentEntry,
333 IN DWORD dwMaxEntryLength,
334 IN cm_scache_t * dscp,
335 IN cm_scache_t * scp,
336 IN cm_user_t * userp,
339 IN wchar_t * shortName,
341 OUT AFSDirEnumEntry **ppNextEntry,
342 OUT DWORD * pdwRemainingLength)
345 WCHAR * wname, *wtarget;
348 afs_uint32 code = 0, code2 = 0;
349 BOOL bMustFake = FALSE;
351 osi_Log5(afsd_logp, "RDR_PopulateCurrentEntry dscp=0x%p scp=0x%p name=%S short=%S flags=0x%x",
352 dscp, scp, osi_LogSaveStringW(afsd_logp, name),
353 osi_LogSaveStringW(afsd_logp, shortName), dwFlags);
354 osi_Log1(afsd_logp, "... maxLength=%d", dwMaxEntryLength);
356 if (dwMaxEntryLength < sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t)) {
358 *ppNextEntry = pCurrentEntry;
359 if (pdwRemainingLength)
360 *pdwRemainingLength = dwMaxEntryLength;
361 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry Not Enough Room for Entry %d < %d",
362 dwMaxEntryLength, sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t));
363 return CM_ERROR_TOOBIG;
371 dwEntryLength = sizeof(AFSDirEnumEntry);
373 lock_ObtainWrite(&scp->rw);
374 if (dwFlags & RDR_POP_NO_GETSTATUS) {
375 if (!cm_HaveCallback(scp))
378 #ifdef AFS_FREELANCE_CLIENT
379 if (scp->fid.cell == AFS_FAKE_ROOT_CELL_ID && scp->fid.volume == AFS_FAKE_ROOT_VOL_ID) {
381 * If the FID is from the Freelance Local Root always perform
382 * a single item status check.
384 code = cm_SyncOp( scp, NULL, userp, reqp, 0,
385 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
387 lock_ReleaseWrite(&scp->rw);
388 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_SyncOp failed for scp=0x%p code=0x%x",
396 * For non-Freelance objects, check to see if we have current
397 * status information. If not, perform a bulk status lookup of multiple
398 * entries in order to reduce the number of RPCs issued to the file server.
400 if ((scp->flags & CM_SCACHEFLAG_EACCESS))
402 else if (!cm_HaveCallback(scp)) {
403 lock_ReleaseWrite(&scp->rw);
404 code = RDR_BulkStatLookup(dscp, scp, userp, reqp);
406 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry RXR_BulkStatLookup failed for scp=0x%p code=0x%x",
410 lock_ObtainWrite(&scp->rw);
412 * RDR_BulkStatLookup can succeed but it may be the case that there
413 * still is not valid status info. If we get this far, generate fake
416 if (!cm_HaveCallback(scp))
423 /* Populate the real or fake data */
424 pCurrentEntry->FileId.Cell = scp->fid.cell;
425 pCurrentEntry->FileId.Volume = scp->fid.volume;
426 pCurrentEntry->FileId.Vnode = scp->fid.vnode;
427 pCurrentEntry->FileId.Unique = scp->fid.unique;
428 pCurrentEntry->FileId.Hash = scp->fid.hash;
430 pCurrentEntry->FileType = scp->fileType;
432 pCurrentEntry->DataVersion.QuadPart = scp->dataVersion;
434 if (scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
435 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
436 cm_LargeSearchTimeFromUnixTime(&ft, MAX_AFS_UINT32);
438 cm_LargeSearchTimeFromUnixTime(&ft, scp->cbExpires);
440 pCurrentEntry->Expiration.LowPart = ft.dwLowDateTime;
441 pCurrentEntry->Expiration.HighPart = ft.dwHighDateTime;
444 /* 1969-12-31 23:59:59 +00 */
445 ft.dwHighDateTime = 0x19DB200;
446 ft.dwLowDateTime = 0x5BB78980;
448 cm_LargeSearchTimeFromUnixTime(&ft, scp->clientModTime);
449 pCurrentEntry->CreationTime.LowPart = ft.dwLowDateTime;
450 pCurrentEntry->CreationTime.HighPart = ft.dwHighDateTime;
451 pCurrentEntry->LastAccessTime = pCurrentEntry->CreationTime;
452 pCurrentEntry->LastWriteTime = pCurrentEntry->CreationTime;
453 pCurrentEntry->ChangeTime = pCurrentEntry->CreationTime;
455 pCurrentEntry->EndOfFile = scp->length;
456 pCurrentEntry->AllocationSize = scp->length;
459 switch (scp->fileType) {
460 case CM_SCACHETYPE_DIRECTORY:
461 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY;
463 case CM_SCACHETYPE_MOUNTPOINT:
464 case CM_SCACHETYPE_INVALID:
465 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY | SMB_ATTR_REPARSE_POINT;
467 case CM_SCACHETYPE_SYMLINK:
468 if (cm_TargetPerceivedAsDirectory(scp->mountPointStringp))
469 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY | SMB_ATTR_REPARSE_POINT;
471 pCurrentEntry->FileAttributes = SMB_ATTR_REPARSE_POINT;
474 /* if we get here we either have a normal file
475 * or we have a file for which we have never
476 * received status info. In this case, we can
477 * check the even/odd value of the entry's vnode.
478 * odd means it is to be treated as a directory
479 * and even means it is to be treated as a file.
481 if (scp->fid.vnode & 0x1)
482 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY;
484 pCurrentEntry->FileAttributes = SMB_ATTR_NORMAL;
487 pCurrentEntry->FileAttributes = smb_ExtAttributes(scp);
488 pCurrentEntry->EaSize = 0;
489 pCurrentEntry->Links = scp->linkCount;
491 len = wcslen(shortName);
492 wcsncpy(pCurrentEntry->ShortName, shortName, len);
493 pCurrentEntry->ShortNameLength = (CCHAR)(len * sizeof(WCHAR));
495 pCurrentEntry->FileNameOffset = sizeof(AFSDirEnumEntry);
497 wname = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->FileNameOffset);
498 wcsncpy(wname, name, len);
499 pCurrentEntry->FileNameLength = (ULONG)(sizeof(WCHAR) * len);
501 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry scp=0x%p fileType=%d",
504 if (!(dwFlags & RDR_POP_NO_GETSTATUS))
505 cm_SyncOpDone( scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
507 if ((dwFlags & RDR_POP_NO_GETSTATUS) || !cm_HaveCallback(scp)) {
508 pCurrentEntry->TargetNameOffset = 0;
509 pCurrentEntry->TargetNameLength = 0;
512 switch (scp->fileType) {
513 case CM_SCACHETYPE_MOUNTPOINT:
514 if (dwFlags & RDR_POP_FOLLOW_MOUNTPOINTS) {
515 if ((code2 = cm_ReadMountPoint(scp, userp, reqp)) == 0) {
516 cm_scache_t *targetScp = NULL;
518 pCurrentEntry->TargetNameOffset = pCurrentEntry->FileNameOffset + pCurrentEntry->FileNameLength;
519 len = strlen(scp->mountPointStringp);
520 wtarget = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->TargetNameOffset);
523 cch = MultiByteToWideChar( CP_UTF8, 0, scp->mountPointStringp,
526 len * sizeof(WCHAR));
528 mbstowcs(wtarget, scp->mountPointStringp, len);
530 pCurrentEntry->TargetNameLength = (ULONG)(sizeof(WCHAR) * len);
532 code2 = cm_FollowMountPoint(scp, dscp, userp, reqp, &targetScp);
535 pCurrentEntry->TargetFileId.Cell = targetScp->fid.cell;
536 pCurrentEntry->TargetFileId.Volume = targetScp->fid.volume;
537 pCurrentEntry->TargetFileId.Vnode = targetScp->fid.vnode;
538 pCurrentEntry->TargetFileId.Unique = targetScp->fid.unique;
539 pCurrentEntry->TargetFileId.Hash = targetScp->fid.hash;
541 osi_Log4(afsd_logp, "RDR_PopulateCurrentEntry target FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
542 pCurrentEntry->TargetFileId.Cell, pCurrentEntry->TargetFileId.Volume,
543 pCurrentEntry->TargetFileId.Vnode, pCurrentEntry->TargetFileId.Unique);
545 cm_ReleaseSCache(targetScp);
547 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_FollowMountPoint failed scp=0x%p code=0x%x",
551 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_ReadMountPoint failed scp=0x%p code=0x%x",
556 case CM_SCACHETYPE_SYMLINK:
557 case CM_SCACHETYPE_DFSLINK:
559 pCurrentEntry->TargetNameOffset = pCurrentEntry->FileNameOffset + pCurrentEntry->FileNameLength;
560 wtarget = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->TargetNameOffset);
562 if (dwFlags & RDR_POP_EVALUATE_SYMLINKS) {
565 code2 = cm_HandleLink(scp, userp, reqp);
567 mp = scp->mountPointStringp;
570 /* Strip off the msdfs: prefix from the target name for the file system */
571 if (scp->fileType == CM_SCACHETYPE_DFSLINK) {
572 osi_Log0(afsd_logp, "RDR_PopulateCurrentEntry DFSLink Detected");
573 pCurrentEntry->FileType = scp->fileType;
575 if (!strncmp("msdfs:", mp, 6)) {
580 /* only send one slash to the redirector */
581 if (mp[0] == '\\' && mp[1] == '\\') {
586 cch = MultiByteToWideChar( CP_UTF8, 0, mp,
589 len * sizeof(WCHAR));
591 mbstowcs(wtarget, mp, len);
594 pCurrentEntry->TargetNameLength = (ULONG)(sizeof(WCHAR) * len);
596 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_HandleLink failed scp=0x%p code=0x%x",
605 pCurrentEntry->TargetNameOffset = 0;
606 pCurrentEntry->TargetNameLength = 0;
608 lock_ReleaseWrite(&scp->rw);
610 dwEntryLength += pCurrentEntry->FileNameLength + pCurrentEntry->TargetNameLength;
611 dwEntryLength += (dwEntryLength % 8) ? 8 - (dwEntryLength % 8) : 0; /* quad align */
613 *ppNextEntry = (AFSDirEnumEntry *)((PBYTE)pCurrentEntry + dwEntryLength);
614 if (pdwRemainingLength)
615 *pdwRemainingLength = dwMaxEntryLength - dwEntryLength;
617 osi_Log3(afsd_logp, "RDR_PopulateCurrentEntry Success FileNameLength=%d TargetNameLength=%d RemainingLength=%d",
618 pCurrentEntry->FileNameLength, pCurrentEntry->TargetNameLength, *pdwRemainingLength);
624 RDR_PopulateCurrentEntryNoScp( IN AFSDirEnumEntry * pCurrentEntry,
625 IN DWORD dwMaxEntryLength,
626 IN cm_scache_t * dscp,
628 IN cm_user_t * userp,
631 IN wchar_t * shortName,
633 OUT AFSDirEnumEntry **ppNextEntry,
634 OUT DWORD * pdwRemainingLength)
640 afs_uint32 code = 0, code2 = 0;
642 osi_Log4(afsd_logp, "RDR_PopulateCurrentEntryNoEntry dscp=0x%p name=%S short=%S flags=0x%x",
643 dscp, osi_LogSaveStringW(afsd_logp, name),
644 osi_LogSaveStringW(afsd_logp, shortName), dwFlags);
645 osi_Log1(afsd_logp, "... maxLength=%d", dwMaxEntryLength);
647 if (dwMaxEntryLength < sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t)) {
649 *ppNextEntry = pCurrentEntry;
650 if (pdwRemainingLength)
651 *pdwRemainingLength = dwMaxEntryLength;
652 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntryNoEntry Not Enough Room for Entry %d < %d",
653 dwMaxEntryLength, sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t));
654 return CM_ERROR_TOOBIG;
662 dwEntryLength = sizeof(AFSDirEnumEntry);
664 pCurrentEntry->FileId.Cell = fidp->cell;
665 pCurrentEntry->FileId.Volume = fidp->volume;
666 pCurrentEntry->FileId.Vnode = fidp->vnode;
667 pCurrentEntry->FileId.Unique = fidp->unique;
668 pCurrentEntry->FileId.Hash = fidp->hash;
670 pCurrentEntry->FileType = CM_SCACHETYPE_UNKNOWN;
672 pCurrentEntry->DataVersion.QuadPart = CM_SCACHE_VERSION_BAD;
674 cm_LargeSearchTimeFromUnixTime(&ft, 0);
675 pCurrentEntry->Expiration.LowPart = ft.dwLowDateTime;
676 pCurrentEntry->Expiration.HighPart = ft.dwHighDateTime;
678 cm_LargeSearchTimeFromUnixTime(&ft, 0);
679 pCurrentEntry->CreationTime.LowPart = ft.dwLowDateTime;
680 pCurrentEntry->CreationTime.HighPart = ft.dwHighDateTime;
681 pCurrentEntry->LastAccessTime = pCurrentEntry->CreationTime;
682 pCurrentEntry->LastWriteTime = pCurrentEntry->CreationTime;
683 pCurrentEntry->ChangeTime = pCurrentEntry->CreationTime;
685 pCurrentEntry->EndOfFile.QuadPart = 0;
686 pCurrentEntry->AllocationSize.QuadPart = 0;
687 pCurrentEntry->FileAttributes = 0;
688 pCurrentEntry->EaSize = 0;
689 pCurrentEntry->Links = 0;
691 len = wcslen(shortName);
692 wcsncpy(pCurrentEntry->ShortName, shortName, len);
693 pCurrentEntry->ShortNameLength = (CCHAR)(len * sizeof(WCHAR));
695 pCurrentEntry->FileNameOffset = sizeof(AFSDirEnumEntry);
697 wname = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->FileNameOffset);
698 wcsncpy(wname, name, len);
699 pCurrentEntry->FileNameLength = (ULONG)(sizeof(WCHAR) * len);
701 pCurrentEntry->TargetNameOffset = 0;
702 pCurrentEntry->TargetNameLength = 0;
704 dwEntryLength += pCurrentEntry->FileNameLength + pCurrentEntry->TargetNameLength;
705 dwEntryLength += (dwEntryLength % 8) ? 8 - (dwEntryLength % 8) : 0; /* quad align */
707 *ppNextEntry = (AFSDirEnumEntry *)((PBYTE)pCurrentEntry + dwEntryLength);
708 if (pdwRemainingLength)
709 *pdwRemainingLength = dwMaxEntryLength - dwEntryLength;
711 osi_Log3(afsd_logp, "RDR_PopulateCurrentEntryNoScp Success FileNameLength=%d TargetNameLength=%d RemainingLength=%d",
712 pCurrentEntry->FileNameLength, pCurrentEntry->TargetNameLength, *pdwRemainingLength);
718 RDR_EnumerateDirectory( IN cm_user_t *userp,
720 IN AFSDirQueryCB *QueryCB,
723 IN DWORD ResultBufferLength,
724 IN OUT AFSCommResult **ResultCB)
727 cm_direnum_t * enump = NULL;
728 AFSDirEnumResp * pDirEnumResp;
729 AFSDirEnumEntry * pCurrentEntry;
730 size_t size = ResultBufferLength ? sizeof(AFSCommResult) + ResultBufferLength - 1 : sizeof(AFSCommResult);
731 DWORD dwMaxEntryLength;
734 cm_scache_t * dscp = NULL;
739 req.flags |= CM_REQ_WOW64;
741 osi_Log4(afsd_logp, "RDR_EnumerateDirectory FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
742 DirID.Cell, DirID.Volume, DirID.Vnode, DirID.Unique);
744 *ResultCB = (AFSCommResult *)malloc(size);
746 osi_Log0(afsd_logp, "RDR_EnumerateDirectory Out of Memory");
750 memset(*ResultCB, 0, size);
752 if (QueryCB->EnumHandle == (ULONG_PTR)-1) {
753 osi_Log0(afsd_logp, "RDR_EnumerateDirectory No More Entries");
754 (*ResultCB)->ResultStatus = STATUS_NO_MORE_ENTRIES;
755 (*ResultCB)->ResultBufferLength = 0;
759 (*ResultCB)->ResultBufferLength = dwMaxEntryLength = ResultBufferLength;
760 if (ResultBufferLength) {
761 pDirEnumResp = (AFSDirEnumResp *)&(*ResultCB)->ResultData;
762 pCurrentEntry = (AFSDirEnumEntry *)&pDirEnumResp->Entry;
763 dwMaxEntryLength -= FIELD_OFFSET( AFSDirEnumResp, Entry); /* AFSDirEnumResp */
766 if (DirID.Cell != 0) {
767 fid.cell = DirID.Cell;
768 fid.volume = DirID.Volume;
769 fid.vnode = DirID.Vnode;
770 fid.unique = DirID.Unique;
771 fid.hash = DirID.Hash;
773 code = cm_GetSCache(&fid, &dscp, userp, &req);
775 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
776 (*ResultCB)->ResultStatus = status;
777 osi_Log2(afsd_logp, "RDR_EnumerateDirectory cm_GetSCache failure code=0x%x status=0x%x",
782 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
783 osi_Log0(afsd_logp, "RDR_EnumerateDirectory Object Name Invalid - Cell = 0");
787 /* get the directory size */
788 lock_ObtainWrite(&dscp->rw);
789 code = cm_SyncOp(dscp, NULL, userp, &req, PRSFS_LOOKUP,
790 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
792 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
793 (*ResultCB)->ResultStatus = status;
794 lock_ReleaseWrite(&dscp->rw);
795 cm_ReleaseSCache(dscp);
796 osi_Log2(afsd_logp, "RDR_EnumerateDirectory cm_SyncOp failure code=0x%x status=0x%x",
801 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
802 lock_ReleaseWrite(&dscp->rw);
804 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
805 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
806 cm_ReleaseSCache(dscp);
807 osi_Log1(afsd_logp, "RDR_EnumerateDirectory Not a Directory dscp=0x%p",
813 * If there is no enumeration handle, then this is a new query
814 * and we must perform an enumeration for the specified object
816 if (QueryCB->EnumHandle == (ULONG_PTR)NULL) {
819 code = cm_BeginDirOp(dscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
821 code = cm_BPlusDirEnumerate(dscp, userp, &req, TRUE, NULL, !bSkipStatus, &enump);
823 osi_Log1(afsd_logp, "RDR_EnumerateDirectory cm_BPlusDirEnumerate failure code=0x%x",
828 osi_Log1(afsd_logp, "RDR_EnumerateDirectory cm_BeginDirOp failure code=0x%x",
832 enump = (cm_direnum_t *)QueryCB->EnumHandle;
835 if (enump && ResultBufferLength) {
836 cm_direnum_entry_t * entryp = NULL;
839 if (dwMaxEntryLength < sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t)) {
840 osi_Log0(afsd_logp, "RDR_EnumerateDirectory out of space, returning");
844 code = cm_BPlusDirNextEnumEntry(enump, &entryp);
846 if ((code == 0 || code == CM_ERROR_STOPNOW) && entryp) {
848 int stopnow = (code == CM_ERROR_STOPNOW);
850 if ( !wcscmp(L".", entryp->name) || !wcscmp(L"..", entryp->name) ) {
851 osi_Log0(afsd_logp, "RDR_EnumerateDirectory skipping . or ..");
857 if ( FALSE /* bSkipStatus */) {
858 scp = cm_FindSCache(&entryp->fid);
861 code = cm_GetSCache(&entryp->fid, &scp, userp, &req);
866 code = RDR_PopulateCurrentEntry(pCurrentEntry, dwMaxEntryLength,
867 dscp, scp, userp, &req,
869 cm_Is8Dot3(entryp->name) ? NULL : entryp->shortName,
870 (bWow64 ? RDR_POP_WOW64 : 0) |
871 (bSkipStatus ? RDR_POP_NO_GETSTATUS : 0),
872 &pCurrentEntry, &dwMaxEntryLength);
873 cm_ReleaseSCache(scp);
875 code = RDR_PopulateCurrentEntryNoScp( pCurrentEntry, dwMaxEntryLength,
876 dscp, &entryp->fid, userp, &req,
878 cm_Is8Dot3(entryp->name) ? NULL : entryp->shortName,
879 (bWow64 ? RDR_POP_WOW64 : 0),
880 &pCurrentEntry, &dwMaxEntryLength);
886 osi_Log2(afsd_logp, "RDR_EnumerateDirectory cm_GetSCache failure scp=0x%p code=0x%x",
895 if (enump && ResultBufferLength == 0) {
896 code = cm_BPlusDirEnumBulkStat(enump);
898 osi_Log1(afsd_logp, "RDR_EnumerateDirectory cm_BPlusDirEnumBulkStat failure code=0x%x",
904 if (code || enump->next == enump->count || ResultBufferLength == 0) {
905 cm_BPlusDirFreeEnumeration(enump);
906 enump = (cm_direnum_t *)(ULONG_PTR)-1;
909 if (code == 0 || code == CM_ERROR_STOPNOW) {
910 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
911 osi_Log0(afsd_logp, "RDR_EnumerateDirectory SUCCESS");
913 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
914 (*ResultCB)->ResultStatus = status;
915 osi_Log2(afsd_logp, "RDR_EnumerateDirectory Failure code=0x%x status=0x%x",
919 if (ResultBufferLength) {
920 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwMaxEntryLength;
922 pDirEnumResp->EnumHandle = (ULONG_PTR) enump;
926 cm_ReleaseSCache(dscp);
932 RDR_EvaluateNodeByName( IN cm_user_t *userp,
933 IN AFSFileID ParentID,
934 IN WCHAR *FileNameCounted,
935 IN DWORD FileNameLength,
936 IN BOOL CaseSensitive,
940 IN DWORD ResultBufferLength,
941 IN OUT AFSCommResult **ResultCB)
943 AFSDirEnumEntry * pCurrentEntry;
944 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
946 cm_scache_t * scp = NULL;
947 cm_scache_t * dscp = NULL;
952 WCHAR * wszName = NULL;
955 wchar_t FileName[260];
957 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
961 req.flags |= CM_REQ_WOW64;
963 osi_Log4(afsd_logp, "RDR_EvaluateNodeByName parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
964 ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
966 /* Allocate enough room to add a volume prefix if necessary */
967 cbName = FileNameLength + (CM_PREFIX_VOL_CCH + 1) * sizeof(WCHAR);
968 wszName = malloc(cbName);
970 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName Out of Memory");
973 StringCbCopyNW(wszName, cbName, FileName, FileNameLength);
974 osi_Log1(afsd_logp, "... name=%S", osi_LogSaveStringW(afsd_logp, wszName));
976 *ResultCB = (AFSCommResult *)malloc(size);
978 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName Out of Memory");
983 memset(*ResultCB, 0, size);
984 (*ResultCB)->ResultBufferLength = ResultBufferLength;
985 if (ResultBufferLength)
986 pCurrentEntry = (AFSDirEnumEntry *)&(*ResultCB)->ResultData;
988 if (ParentID.Cell != 0) {
989 parentFid.cell = ParentID.Cell;
990 parentFid.volume = ParentID.Volume;
991 parentFid.vnode = ParentID.Vnode;
992 parentFid.unique = ParentID.Unique;
993 parentFid.hash = ParentID.Hash;
995 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
997 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
998 (*ResultCB)->ResultStatus = status;
999 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName cm_GetSCache parentFID failure code=0x%x status=0x%x",
1005 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
1006 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName Object Name Invalid - Cell = 0");
1010 /* get the directory size */
1011 lock_ObtainWrite(&dscp->rw);
1012 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1013 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1015 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1016 (*ResultCB)->ResultStatus = status;
1017 lock_ReleaseWrite(&dscp->rw);
1018 cm_ReleaseSCache(dscp);
1019 osi_Log3(afsd_logp, "RDR_EvaluateNodeByName cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1020 dscp, code, status);
1024 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1025 lock_ReleaseWrite(&dscp->rw);
1027 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1028 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1029 cm_ReleaseSCache(dscp);
1030 osi_Log1(afsd_logp, "RDR_EvaluateNodeByName Not a Directory dscp=0x%p",
1036 code = cm_Lookup(dscp, wszName, CM_FLAG_CHECKPATH, userp, &req, &scp);
1038 if ((code == CM_ERROR_NOSUCHPATH || code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) &&
1039 (wcschr(wszName, '%') != NULL || wcschr(wszName, '#') != NULL)) {
1041 * A volume reference: <cell>{%,#}<volume> -> @vol:<cell>{%,#}<volume>
1043 StringCchCopyNW(wszName, cbName, _C(CM_PREFIX_VOL), CM_PREFIX_VOL_CCH);
1044 StringCbCatNW(wszName, cbName, FileName, FileNameLength);
1045 cm_strlwr_utf16(wszName);
1048 code = cm_EvaluateVolumeReference(wszName, CM_FLAG_CHECKPATH, userp, &req, &scp);
1051 if (code == 0 && scp) {
1052 wchar_t shortName[13]=L"";
1055 cm_Gen8Dot3VolNameW(scp->fid.cell, scp->fid.volume, shortName, NULL);
1056 } else if (!cm_Is8Dot3(wszName)) {
1059 dfid.vnode = htonl(scp->fid.vnode);
1060 dfid.unique = htonl(scp->fid.unique);
1062 cm_Gen8Dot3NameIntW(FileName, &dfid, shortName, NULL);
1064 shortName[0] = '\0';
1067 code = RDR_PopulateCurrentEntry(pCurrentEntry, ResultBufferLength,
1068 dscp, scp, userp, &req,
1069 FileName, shortName,
1070 (bWow64 ? RDR_POP_WOW64 : 0) |
1071 (bNoFollow ? 0 : (RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS)),
1072 NULL, &dwRemaining);
1074 RDR_FlagScpInUse( scp, FALSE );
1075 cm_ReleaseSCache(scp);
1078 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1079 (*ResultCB)->ResultStatus = status;
1080 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName FAILURE code=0x%x status=0x%x",
1083 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
1084 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1085 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName SUCCESS");
1088 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1089 (*ResultCB)->ResultStatus = status;
1090 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName FAILURE code=0x%x status=0x%x",
1093 (*ResultCB)->ResultStatus = STATUS_NO_SUCH_FILE;
1094 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName No Such File");
1096 cm_ReleaseSCache(dscp);
1103 RDR_EvaluateNodeByID( IN cm_user_t *userp,
1104 IN AFSFileID ParentID, /* not used */
1105 IN AFSFileID SourceID,
1109 IN DWORD ResultBufferLength,
1110 IN OUT AFSCommResult **ResultCB)
1112 AFSDirEnumEntry * pCurrentEntry;
1113 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1114 afs_uint32 code = 0;
1115 cm_scache_t * scp = NULL;
1116 cm_scache_t * dscp = NULL;
1123 osi_Log4(afsd_logp, "RDR_EvaluateNodeByID source FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1124 SourceID.Cell, SourceID.Volume, SourceID.Vnode, SourceID.Unique);
1125 osi_Log4(afsd_logp, "... parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1126 ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
1128 *ResultCB = (AFSCommResult *)malloc(size);
1130 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Out of Memory");
1134 memset(*ResultCB, 0, size);
1135 (*ResultCB)->ResultBufferLength = ResultBufferLength;
1136 dwRemaining = ResultBufferLength;
1137 if (ResultBufferLength)
1138 pCurrentEntry = (AFSDirEnumEntry *)&(*ResultCB)->ResultData;
1142 req.flags |= CM_REQ_WOW64;
1144 if (SourceID.Cell != 0) {
1145 Fid.cell = SourceID.Cell;
1146 Fid.volume = SourceID.Volume;
1147 Fid.vnode = SourceID.Vnode;
1148 Fid.unique = SourceID.Unique;
1149 Fid.hash = SourceID.Hash;
1151 code = cm_GetSCache(&Fid, &scp, userp, &req);
1153 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1154 (*ResultCB)->ResultStatus = status;
1155 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache SourceFID failure code=0x%x status=0x%x",
1160 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
1161 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Object Name Invalid - Cell = 0");
1165 if (ParentID.Cell != 0) {
1166 cm_SetFid(&parentFid, ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
1167 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
1169 cm_ReleaseSCache(scp);
1170 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1171 (*ResultCB)->ResultStatus = status;
1172 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache parentFID failure code=0x%x status=0x%x",
1176 } else if (SourceID.Vnode == 1) {
1178 cm_HoldSCache(dscp);
1179 } else if (scp->parentVnode) {
1180 cm_SetFid(&parentFid, SourceID.Cell, SourceID.Volume, scp->parentVnode, scp->parentUnique);
1181 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
1183 cm_ReleaseSCache(scp);
1184 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1185 (*ResultCB)->ResultStatus = status;
1186 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache parentFID failure code=0x%x status=0x%x",
1191 (*ResultCB)->ResultStatus = STATUS_OBJECT_PATH_INVALID;
1192 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Object Path Invalid - Unknown Parent");
1196 /* Make sure the directory is current */
1197 lock_ObtainWrite(&dscp->rw);
1198 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1199 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1201 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1202 (*ResultCB)->ResultStatus = status;
1203 lock_ReleaseWrite(&dscp->rw);
1204 cm_ReleaseSCache(dscp);
1205 cm_ReleaseSCache(scp);
1206 osi_Log3(afsd_logp, "RDR_EvaluateNodeByID cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1207 dscp, code, status);
1211 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1212 lock_ReleaseWrite(&dscp->rw);
1214 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1215 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1216 cm_ReleaseSCache(dscp);
1217 cm_ReleaseSCache(scp);
1218 osi_Log1(afsd_logp, "RDR_EvaluateNodeByID Not a Directory dscp=0x%p", dscp);
1222 code = RDR_PopulateCurrentEntry(pCurrentEntry, ResultBufferLength,
1223 dscp, scp, userp, &req, NULL, NULL,
1224 (bWow64 ? RDR_POP_WOW64 : 0) |
1225 (bNoFollow ? 0 : (RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS)),
1226 NULL, &dwRemaining);
1229 RDR_FlagScpInUse( scp, FALSE );
1230 cm_ReleaseSCache(scp);
1231 cm_ReleaseSCache(dscp);
1234 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1235 (*ResultCB)->ResultStatus = status;
1236 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID FAILURE code=0x%x status=0x%x",
1239 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
1240 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1241 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID SUCCESS");
1247 RDR_CreateFileEntry( IN cm_user_t *userp,
1248 IN WCHAR *FileNameCounted,
1249 IN DWORD FileNameLength,
1250 IN AFSFileCreateCB *CreateCB,
1253 IN DWORD ResultBufferLength,
1254 IN OUT AFSCommResult **ResultCB)
1256 AFSFileCreateResultCB *pResultCB = NULL;
1257 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1260 cm_scache_t * dscp = NULL;
1261 afs_uint32 flags = 0;
1263 cm_scache_t * scp = NULL;
1266 wchar_t FileName[260];
1268 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
1270 osi_Log4(afsd_logp, "RDR_CreateFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1271 CreateCB->ParentId.Cell, CreateCB->ParentId.Volume,
1272 CreateCB->ParentId.Vnode, CreateCB->ParentId.Unique);
1273 osi_Log1(afsd_logp, "... name=%S", osi_LogSaveStringW(afsd_logp, FileName));
1277 req.flags |= CM_REQ_WOW64;
1278 memset(&setAttr, 0, sizeof(cm_attr_t));
1280 *ResultCB = (AFSCommResult *)malloc(size);
1282 osi_Log0(afsd_logp, "RDR_CreateFileEntry out of memory");
1290 parentFid.cell = CreateCB->ParentId.Cell;
1291 parentFid.volume = CreateCB->ParentId.Volume;
1292 parentFid.vnode = CreateCB->ParentId.Vnode;
1293 parentFid.unique = CreateCB->ParentId.Unique;
1294 parentFid.hash = CreateCB->ParentId.Hash;
1296 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
1298 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1299 (*ResultCB)->ResultStatus = status;
1300 osi_Log2(afsd_logp, "RDR_CreateFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1305 lock_ObtainWrite(&dscp->rw);
1306 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1307 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1309 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1310 (*ResultCB)->ResultStatus = status;
1311 lock_ReleaseWrite(&dscp->rw);
1312 cm_ReleaseSCache(dscp);
1313 osi_Log3(afsd_logp, "RDR_CreateFileEntry cm_SyncOp failure (1) dscp=0x%p code=0x%x status=0x%x",
1314 dscp, code, status);
1318 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1319 lock_ReleaseWrite(&dscp->rw);
1321 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1322 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1323 cm_ReleaseSCache(dscp);
1324 osi_Log1(afsd_logp, "RDR_CreateFileEntry Not a Directory dscp=0x%p",
1329 /* Use current time */
1330 setAttr.mask = CM_ATTRMASK_CLIENTMODTIME;
1331 setAttr.clientModTime = time(NULL);
1333 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1334 if (smb_unixModeDefaultDir) {
1335 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1336 setAttr.unixModeBits = smb_unixModeDefaultDir;
1337 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)
1338 setAttr.unixModeBits &= ~0222; /* disable the write bits */
1341 code = cm_MakeDir(dscp, FileName, flags, &setAttr, userp, &req, &scp);
1343 if (smb_unixModeDefaultFile) {
1344 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1345 setAttr.unixModeBits = smb_unixModeDefaultFile;
1346 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)
1347 setAttr.unixModeBits &= ~0222; /* disable the write bits */
1350 setAttr.mask |= CM_ATTRMASK_LENGTH;
1351 setAttr.length.LowPart = CreateCB->AllocationSize.LowPart;
1352 setAttr.length.HighPart = CreateCB->AllocationSize.HighPart;
1353 code = cm_Create(dscp, FileName, flags, &setAttr, &scp, userp, &req);
1356 wchar_t shortName[13]=L"";
1360 (*ResultCB)->ResultStatus = 0; // We will be able to fit all the data in here
1362 (*ResultCB)->ResultBufferLength = sizeof( AFSFileCreateResultCB);
1364 pResultCB = (AFSFileCreateResultCB *)(*ResultCB)->ResultData;
1366 dwRemaining = ResultBufferLength - sizeof( AFSFileCreateResultCB) + sizeof( AFSDirEnumEntry);
1368 lock_ObtainWrite(&dscp->rw);
1369 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1370 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1372 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1373 (*ResultCB)->ResultStatus = status;
1374 lock_ReleaseWrite(&dscp->rw);
1375 cm_ReleaseSCache(dscp);
1376 cm_ReleaseSCache(scp);
1377 osi_Log3(afsd_logp, "RDR_CreateFileEntry cm_SyncOp failure (2) dscp=0x%p code=0x%x status=0x%x",
1378 dscp, code, status);
1382 pResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
1384 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1385 lock_ReleaseWrite(&dscp->rw);
1387 dfid.vnode = htonl(scp->fid.vnode);
1388 dfid.unique = htonl(scp->fid.unique);
1390 if (!cm_Is8Dot3(FileName))
1391 cm_Gen8Dot3NameIntW(FileName, &dfid, shortName, NULL);
1393 shortName[0] = '\0';
1395 code = RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
1396 dscp, scp, userp, &req, FileName, shortName,
1397 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
1398 NULL, &dwRemaining);
1401 RDR_FlagScpInUse( scp, FALSE );
1402 cm_ReleaseSCache(scp);
1403 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1404 osi_Log0(afsd_logp, "RDR_CreateFileEntry SUCCESS");
1406 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1407 (*ResultCB)->ResultStatus = status;
1408 (*ResultCB)->ResultBufferLength = 0;
1409 osi_Log2(afsd_logp, "RDR_CreateFileEntry FAILURE code=0x%x status=0x%x",
1413 cm_ReleaseSCache(dscp);
1419 RDR_UpdateFileEntry( IN cm_user_t *userp,
1420 IN AFSFileID FileId,
1421 IN AFSFileUpdateCB *UpdateCB,
1423 IN DWORD ResultBufferLength,
1424 IN OUT AFSCommResult **ResultCB)
1426 AFSFileUpdateResultCB *pResultCB = NULL;
1427 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1431 afs_uint32 flags = 0;
1433 cm_scache_t * scp = NULL;
1434 cm_scache_t * dscp = NULL;
1436 time_t clientModTime;
1439 BOOL bScpLocked = FALSE;
1443 req.flags |= CM_REQ_WOW64;
1444 memset(&setAttr, 0, sizeof(cm_attr_t));
1446 osi_Log4(afsd_logp, "RDR_UpdateFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1447 UpdateCB->ParentId.Cell, UpdateCB->ParentId.Volume,
1448 UpdateCB->ParentId.Vnode, UpdateCB->ParentId.Unique);
1449 osi_Log4(afsd_logp, "... object FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1450 FileId.Cell, FileId.Volume,
1451 FileId.Vnode, FileId.Unique);
1453 *ResultCB = (AFSCommResult *)malloc( size);
1455 osi_Log0(afsd_logp, "RDR_UpdateFileEntry Out of Memory");
1463 parentFid.cell = UpdateCB->ParentId.Cell;
1464 parentFid.volume = UpdateCB->ParentId.Volume;
1465 parentFid.vnode = UpdateCB->ParentId.Vnode;
1466 parentFid.unique = UpdateCB->ParentId.Unique;
1467 parentFid.hash = UpdateCB->ParentId.Hash;
1469 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
1471 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1472 (*ResultCB)->ResultStatus = status;
1473 osi_Log2(afsd_logp, "RDR_UpdateFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1478 lock_ObtainWrite(&dscp->rw);
1480 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1481 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1483 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1484 (*ResultCB)->ResultStatus = status;
1485 lock_ReleaseWrite(&dscp->rw);
1486 cm_ReleaseSCache(dscp);
1487 osi_Log3(afsd_logp, "RDR_UpdateFileEntry cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1488 dscp, code, status);
1492 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1493 lock_ReleaseWrite(&dscp->rw);
1496 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1497 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1498 cm_ReleaseSCache(dscp);
1499 osi_Log1(afsd_logp, "RDR_UpdateFileEntry Not a Directory dscp=0x%p",
1504 Fid.cell = FileId.Cell;
1505 Fid.volume = FileId.Volume;
1506 Fid.vnode = FileId.Vnode;
1507 Fid.unique = FileId.Unique;
1508 Fid.hash = FileId.Hash;
1510 code = cm_GetSCache(&Fid, &scp, userp, &req);
1512 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1513 (*ResultCB)->ResultStatus = status;
1514 cm_ReleaseSCache(dscp);
1515 osi_Log2(afsd_logp, "RDR_UpdateFileEntry cm_GetSCache object FID failure code=0x%x status=0x%x",
1520 lock_ObtainWrite(&scp->rw);
1522 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1523 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1525 lock_ReleaseWrite(&scp->rw);
1526 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1527 (*ResultCB)->ResultStatus = status;
1528 (*ResultCB)->ResultBufferLength = 0;
1529 cm_ReleaseSCache(dscp);
1530 cm_ReleaseSCache(scp);
1531 osi_Log3(afsd_logp, "RDR_UpdateFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
1535 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1537 if (UpdateCB->ChangeTime.QuadPart) {
1539 if (scp->fileType == CM_SCACHETYPE_FILE) {
1540 /* Do not set length and other attributes at the same time */
1541 if (scp->length.QuadPart != UpdateCB->AllocationSize.QuadPart) {
1542 osi_Log2(afsd_logp, "RDR_UpdateFileEntry Length Change 0x%x -> 0x%x",
1543 (afs_uint32)scp->length.QuadPart, (afs_uint32)UpdateCB->AllocationSize.QuadPart);
1544 setAttr.mask |= CM_ATTRMASK_LENGTH;
1545 setAttr.length.LowPart = UpdateCB->AllocationSize.LowPart;
1546 setAttr.length.HighPart = UpdateCB->AllocationSize.HighPart;
1547 lock_ReleaseWrite(&scp->rw);
1549 code = cm_SetAttr(scp, &setAttr, userp, &req);
1557 lock_ObtainWrite(&scp->rw);
1560 if ((scp->unixModeBits & 0200) && (UpdateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1561 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1562 setAttr.unixModeBits = scp->unixModeBits & ~0222;
1563 } else if (!(scp->unixModeBits & 0200) && !(UpdateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1564 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1565 setAttr.unixModeBits = scp->unixModeBits | 0222;
1569 if (UpdateCB->LastWriteTime.QuadPart) {
1570 ft.dwLowDateTime = UpdateCB->LastWriteTime.LowPart;
1571 ft.dwHighDateTime = UpdateCB->LastWriteTime.HighPart;
1573 cm_UnixTimeFromLargeSearchTime(& clientModTime, &ft);
1576 lock_ObtainWrite(&scp->rw);
1579 if (scp->clientModTime != clientModTime) {
1580 setAttr.mask |= CM_ATTRMASK_CLIENTMODTIME;
1581 setAttr.clientModTime = clientModTime;
1586 lock_ReleaseWrite(&scp->rw);
1588 code = cm_SetAttr(scp, &setAttr, userp, &req);
1595 lock_ReleaseWrite(&scp->rw);
1599 DWORD dwRemaining = ResultBufferLength - sizeof( AFSFileUpdateResultCB) + sizeof( AFSDirEnumEntry);
1601 pResultCB = (AFSFileUpdateResultCB *)(*ResultCB)->ResultData;
1603 code = RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
1604 dscp, scp, userp, &req, NULL, NULL,
1605 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
1606 NULL, &dwRemaining);
1607 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1608 osi_Log0(afsd_logp, "RDR_UpdateFileEntry SUCCESS");
1610 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1611 (*ResultCB)->ResultStatus = status;
1612 (*ResultCB)->ResultBufferLength = 0;
1613 osi_Log2(afsd_logp, "RDR_UpdateFileEntry FAILURE code=0x%x status=0x%x",
1616 cm_ReleaseSCache(scp);
1617 cm_ReleaseSCache(dscp);
1623 RDR_CleanupFileEntry( IN cm_user_t *userp,
1624 IN AFSFileID FileId,
1625 IN WCHAR *FileNameCounted,
1626 IN DWORD FileNameLength,
1627 IN AFSFileCleanupCB *CleanupCB,
1629 IN BOOL bLastHandle,
1630 IN BOOL bDeleteFile,
1631 IN BOOL bUnlockFile,
1632 IN DWORD ResultBufferLength,
1633 IN OUT AFSCommResult **ResultCB)
1635 size_t size = sizeof(AFSCommResult);
1638 afs_uint32 code = 0;
1639 afs_uint32 flags = 0;
1641 cm_scache_t * scp = NULL;
1642 cm_scache_t * dscp = NULL;
1644 time_t clientModTime;
1647 BOOL bScpLocked = FALSE;
1648 BOOL bDscpLocked = FALSE;
1649 BOOL bFlushFile = FALSE;
1654 req.flags |= CM_REQ_WOW64;
1655 memset(&setAttr, 0, sizeof(cm_attr_t));
1657 osi_Log4(afsd_logp, "RDR_CleanupFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1658 CleanupCB->ParentId.Cell, CleanupCB->ParentId.Volume,
1659 CleanupCB->ParentId.Vnode, CleanupCB->ParentId.Unique);
1660 osi_Log4(afsd_logp, "... object FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1661 FileId.Cell, FileId.Volume,
1662 FileId.Vnode, FileId.Unique);
1664 *ResultCB = (AFSCommResult *)malloc( size);
1666 osi_Log0(afsd_logp, "RDR_CleanupFileEntry Out of Memory");
1674 parentFid.cell = CleanupCB->ParentId.Cell;
1675 parentFid.volume = CleanupCB->ParentId.Volume;
1676 parentFid.vnode = CleanupCB->ParentId.Vnode;
1677 parentFid.unique = CleanupCB->ParentId.Unique;
1678 parentFid.hash = CleanupCB->ParentId.Hash;
1680 if (parentFid.cell) {
1681 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
1683 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1684 (*ResultCB)->ResultStatus = status;
1685 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1690 lock_ObtainWrite(&dscp->rw);
1692 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1693 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1695 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp failure dscp=0x%p code=0x%x",
1701 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1702 lock_ReleaseWrite(&dscp->rw);
1703 bDscpLocked = FALSE;
1705 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1706 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1707 cm_ReleaseSCache(dscp);
1708 osi_Log1(afsd_logp, "RDR_CleanupFileEntry Not a Directory dscp=0x%p",
1715 Fid.cell = FileId.Cell;
1716 Fid.volume = FileId.Volume;
1717 Fid.vnode = FileId.Vnode;
1718 Fid.unique = FileId.Unique;
1719 Fid.hash = FileId.Hash;
1721 code = cm_GetSCache(&Fid, &scp, userp, &req);
1723 osi_Log1(afsd_logp, "RDR_CleanupFileEntry cm_GetSCache object FID failure code=0x%x",
1728 lock_ObtainWrite(&scp->rw);
1730 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1731 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1733 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp failure scp=0x%p code=0x%x",
1737 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1739 if ((bLastHandle || bFlushFile) &&
1740 scp->redirBufCount > 0)
1742 LARGE_INTEGER heldExtents;
1743 AFSFileExtentCB extentList[1024];
1744 DWORD extentCount = 0;
1749 heldExtents.QuadPart = 0;
1751 for ( srbp = redirq_to_cm_buf_t(scp->redirQueueT);
1753 srbp = redirq_to_cm_buf_t(osi_QPrev(&srbp->redirq)))
1755 extentList[extentCount].Flags = 0;
1756 extentList[extentCount].Length = cm_data.blockSize;
1757 extentList[extentCount].FileOffset.QuadPart = srbp->offset.QuadPart;
1758 extentList[extentCount].CacheOffset.QuadPart = srbp->datap - RDR_extentBaseAddress;
1759 lock_ObtainWrite(&buf_globalLock);
1760 srbp->redirReleaseRequested = now;
1761 lock_ReleaseWrite(&buf_globalLock);
1764 if (extentCount == 1024) {
1765 lock_ReleaseWrite(&scp->rw);
1766 code = RDR_RequestExtentRelease(&scp->fid, heldExtents, extentCount, extentList);
1768 if (code == CM_ERROR_RETRY) {
1770 * The redirector either is not holding the extents or cannot let them
1771 * go because they are otherwise in use. At the moment, do nothing.
1778 lock_ObtainWrite(&scp->rw);
1782 if (code == 0 && extentCount > 0) {
1784 lock_ReleaseWrite(&scp->rw);
1787 code = RDR_RequestExtentRelease(&scp->fid, heldExtents, extentCount, extentList);
1792 /* No longer in use by redirector */
1794 lock_ObtainWrite(&scp->rw);
1799 lock_AssertWrite(&scp->rw);
1800 scp->flags &= ~CM_SCACHEFLAG_RDR_IN_USE;
1803 if (bLastHandle || bFlushFile) {
1805 lock_ObtainWrite(&scp->rw);
1808 code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_WRITE,
1809 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1812 lock_ReleaseWrite(&scp->rw);
1816 code = cm_FSync(scp, userp, &req, bScpLocked);
1818 if (bLastHandle && code)
1822 if (bUnlockFile || bDeleteFile) {
1824 lock_ObtainWrite(&scp->rw);
1827 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1828 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
1830 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp (2) failure scp=0x%p code=0x%x",
1835 key = cm_GenerateKey(CM_SESSION_IFS, CleanupCB->ProcessId, 0);
1837 /* the scp is now locked and current */
1838 code = cm_UnlockByKey(scp, key,
1839 bDeleteFile ? CM_UNLOCK_FLAG_BY_FID : 0,
1842 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
1848 if (CleanupCB->ChangeTime.QuadPart) {
1850 if (scp->fileType == CM_SCACHETYPE_FILE) {
1851 /* Do not set length and other attributes at the same time */
1852 if (scp->length.QuadPart != CleanupCB->AllocationSize.QuadPart) {
1853 osi_Log2(afsd_logp, "RDR_CleanupFileEntry Length Change 0x%x -> 0x%x",
1854 (afs_uint32)scp->length.QuadPart, (afs_uint32)CleanupCB->AllocationSize.QuadPart);
1855 setAttr.mask |= CM_ATTRMASK_LENGTH;
1856 setAttr.length.LowPart = CleanupCB->AllocationSize.LowPart;
1857 setAttr.length.HighPart = CleanupCB->AllocationSize.HighPart;
1860 lock_ReleaseWrite(&scp->rw);
1863 code = cm_SetAttr(scp, &setAttr, userp, &req);
1871 lock_ObtainWrite(&scp->rw);
1875 if ((scp->unixModeBits & 0200) && (CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1876 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1877 setAttr.unixModeBits = scp->unixModeBits & ~0222;
1878 } else if (!(scp->unixModeBits & 0200) && !(CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1879 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1880 setAttr.unixModeBits = scp->unixModeBits | 0222;
1884 if (CleanupCB->LastWriteTime.QuadPart) {
1885 ft.dwLowDateTime = CleanupCB->LastWriteTime.LowPart;
1886 ft.dwHighDateTime = CleanupCB->LastWriteTime.HighPart;
1888 cm_UnixTimeFromLargeSearchTime(&clientModTime, &ft);
1889 if (scp->clientModTime != clientModTime) {
1890 setAttr.mask |= CM_ATTRMASK_CLIENTMODTIME;
1891 setAttr.clientModTime = clientModTime;
1897 lock_ReleaseWrite(&scp->rw);
1899 code = cm_SetAttr(scp, &setAttr, userp, &req);
1903 /* Now drop the lock enforcing the share access */
1904 if ( CleanupCB->FileAccess != AFS_FILE_ACCESS_NOLOCK) {
1905 unsigned int sLockType;
1906 LARGE_INTEGER LOffset, LLength;
1908 if (CleanupCB->FileAccess == AFS_FILE_ACCESS_SHARED)
1909 sLockType = LOCKING_ANDX_SHARED_LOCK;
1913 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, CleanupCB->Identifier);
1915 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
1916 LOffset.LowPart = SMB_FID_QLOCK_LOW;
1917 LLength.HighPart = 0;
1918 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
1921 lock_ObtainWrite(&scp->rw);
1925 code = cm_SyncOp(scp, NULL, userp, &req, 0, CM_SCACHESYNC_LOCK);
1928 code = cm_Unlock(scp, sLockType, LOffset, LLength, key, 0, userp, &req);
1930 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
1932 if (code == CM_ERROR_RANGE_NOT_LOCKED)
1934 osi_Log3(afsd_logp, "RDR_CleanupFileEntry Range Not Locked -- FileAccess 0x%x ProcessId 0x%x HandleId 0x%x",
1935 CleanupCB->FileAccess, CleanupCB->ProcessId, CleanupCB->Identifier);
1943 lock_ReleaseWrite(&dscp->rw);
1945 lock_ReleaseWrite(&scp->rw);
1947 if (dscp && bDeleteFile) {
1948 WCHAR FileName[260];
1950 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
1952 if (scp->fileType == CM_SCACHETYPE_DIRECTORY)
1953 code = cm_RemoveDir(dscp, NULL, FileName, userp, &req);
1955 code = cm_Unlink(dscp, NULL, FileName, userp, &req);
1959 (*ResultCB)->ResultStatus = 0;
1960 (*ResultCB)->ResultBufferLength = 0;
1961 osi_Log0(afsd_logp, "RDR_CleanupFileEntry SUCCESS");
1963 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1964 (*ResultCB)->ResultStatus = status;
1965 (*ResultCB)->ResultBufferLength = 0;
1966 osi_Log2(afsd_logp, "RDR_CleanupFileEntry FAILURE code=0x%x status=0x%x",
1970 cm_ReleaseSCache(scp);
1972 cm_ReleaseSCache(dscp);
1978 RDR_DeleteFileEntry( IN cm_user_t *userp,
1979 IN AFSFileID ParentId,
1980 IN ULONGLONG ProcessId,
1981 IN WCHAR *FileNameCounted,
1982 IN DWORD FileNameLength,
1985 IN DWORD ResultBufferLength,
1986 IN OUT AFSCommResult **ResultCB)
1989 AFSFileDeleteResultCB *pResultCB = NULL;
1990 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1993 cm_scache_t * dscp = NULL;
1994 cm_scache_t * scp = NULL;
1995 afs_uint32 flags = 0;
1999 wchar_t FileName[260];
2002 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
2004 osi_Log4(afsd_logp, "RDR_DeleteFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2005 ParentId.Cell, ParentId.Volume,
2006 ParentId.Vnode, ParentId.Unique);
2007 osi_Log2(afsd_logp, "... name=%S checkOnly=%x",
2008 osi_LogSaveStringW(afsd_logp, FileName),
2013 req.flags |= CM_REQ_WOW64;
2014 memset(&setAttr, 0, sizeof(cm_attr_t));
2016 *ResultCB = (AFSCommResult *)malloc( size);
2018 osi_Log0(afsd_logp, "RDR_DeleteFileEntry out of memory");
2026 parentFid.cell = ParentId.Cell;
2027 parentFid.volume = ParentId.Volume;
2028 parentFid.vnode = ParentId.Vnode;
2029 parentFid.unique = ParentId.Unique;
2030 parentFid.hash = ParentId.Hash;
2032 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
2034 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2035 (*ResultCB)->ResultStatus = status;
2036 osi_Log2(afsd_logp, "RDR_DeleteFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
2041 lock_ObtainWrite(&dscp->rw);
2043 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
2044 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2046 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2047 (*ResultCB)->ResultStatus = status;
2048 (*ResultCB)->ResultBufferLength = 0;
2049 lock_ReleaseWrite(&dscp->rw);
2050 cm_ReleaseSCache(dscp);
2051 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
2052 dscp, code, status);
2056 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2057 lock_ReleaseWrite(&dscp->rw);
2059 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2060 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2061 cm_ReleaseSCache(dscp);
2062 osi_Log1(afsd_logp, "RDR_DeleteFileEntry Not a Directory dscp=0x%p",
2067 code = cm_Lookup(dscp, FileName, 0, userp, &req, &scp);
2069 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2070 (*ResultCB)->ResultStatus = status;
2071 (*ResultCB)->ResultBufferLength = 0;
2072 cm_ReleaseSCache(dscp);
2073 osi_Log2(afsd_logp, "RDR_DeleteFileEntry cm_Lookup failure code=0x%x status=0x%x",
2078 lock_ObtainWrite(&scp->rw);
2079 code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_DELETE,
2080 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2082 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2083 (*ResultCB)->ResultStatus = status;
2084 (*ResultCB)->ResultBufferLength = 0;
2085 lock_ReleaseWrite(&scp->rw);
2086 cm_ReleaseSCache(scp);
2087 cm_ReleaseSCache(dscp);
2088 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2094 /* Drop all locks since the file is being deleted */
2095 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2096 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
2098 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2099 (*ResultCB)->ResultStatus = status;
2100 (*ResultCB)->ResultBufferLength = 0;
2101 lock_ReleaseWrite(&scp->rw);
2102 cm_ReleaseSCache(scp);
2103 cm_ReleaseSCache(dscp);
2104 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp Lock failure scp=0x%p code=0x%x status=0x%x",
2108 /* the scp is now locked and current */
2109 key = cm_GenerateKey(CM_SESSION_IFS, ProcessId, 0);
2111 code = cm_UnlockByKey(scp, key,
2112 CM_UNLOCK_FLAG_BY_FID,
2115 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
2116 lock_ReleaseWrite(&scp->rw);
2118 if (scp->fileType == CM_SCACHETYPE_DIRECTORY)
2119 code = cm_RemoveDir(dscp, NULL, FileName, userp, &req);
2121 code = cm_Unlink(dscp, NULL, FileName, userp, &req);
2123 lock_ReleaseWrite(&scp->rw);
2127 (*ResultCB)->ResultStatus = 0; // We will be able to fit all the data in here
2129 (*ResultCB)->ResultBufferLength = sizeof( AFSFileDeleteResultCB);
2131 pResultCB = (AFSFileDeleteResultCB *)(*ResultCB)->ResultData;
2133 pResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
2134 osi_Log0(afsd_logp, "RDR_DeleteFileEntry SUCCESS");
2136 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2137 (*ResultCB)->ResultStatus = status;
2138 (*ResultCB)->ResultBufferLength = 0;
2139 osi_Log2(afsd_logp, "RDR_DeleteFileEntry FAILURE code=0x%x status=0x%x",
2143 cm_ReleaseSCache(dscp);
2144 cm_ReleaseSCache(scp);
2150 RDR_RenameFileEntry( IN cm_user_t *userp,
2151 IN WCHAR *SourceFileNameCounted,
2152 IN DWORD SourceFileNameLength,
2153 IN AFSFileID SourceFileId,
2154 IN AFSFileRenameCB *pRenameCB,
2156 IN DWORD ResultBufferLength,
2157 IN OUT AFSCommResult **ResultCB)
2160 AFSFileRenameResultCB *pResultCB = NULL;
2161 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
2162 AFSFileID SourceParentId = pRenameCB->SourceParentId;
2163 AFSFileID TargetParentId = pRenameCB->TargetParentId;
2164 WCHAR * TargetFileNameCounted = pRenameCB->TargetName;
2165 DWORD TargetFileNameLength = pRenameCB->TargetNameLength;
2166 cm_fid_t SourceParentFid;
2167 cm_fid_t TargetParentFid;
2168 cm_scache_t * oldDscp;
2169 cm_scache_t * newDscp;
2170 wchar_t shortName[13];
2171 wchar_t SourceFileName[260];
2172 wchar_t TargetFileName[260];
2180 req.flags |= CM_REQ_WOW64;
2182 StringCchCopyNW(SourceFileName, 260, SourceFileNameCounted, SourceFileNameLength / sizeof(WCHAR));
2183 StringCchCopyNW(TargetFileName, 260, TargetFileNameCounted, TargetFileNameLength / sizeof(WCHAR));
2185 osi_Log4(afsd_logp, "RDR_RenameFileEntry Source Parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2186 SourceParentId.Cell, SourceParentId.Volume,
2187 SourceParentId.Vnode, SourceParentId.Unique);
2188 osi_Log2(afsd_logp, "... Source Name=%S Length %u", osi_LogSaveStringW(afsd_logp, SourceFileName), SourceFileNameLength);
2189 osi_Log4(afsd_logp, "... Target Parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2190 TargetParentId.Cell, TargetParentId.Volume,
2191 TargetParentId.Vnode, TargetParentId.Unique);
2192 osi_Log2(afsd_logp, "... Target Name=%S Length %u", osi_LogSaveStringW(afsd_logp, TargetFileName), TargetFileNameLength);
2194 *ResultCB = (AFSCommResult *)malloc( size);
2202 pResultCB = (AFSFileRenameResultCB *)(*ResultCB)->ResultData;
2204 if (SourceFileNameLength == 0 || TargetFileNameLength == 0)
2206 osi_Log2(afsd_logp, "RDR_RenameFileEntry Invalid Name Length: src %u target %u",
2207 SourceFileNameLength, TargetFileNameLength);
2208 (*ResultCB)->ResultStatus = STATUS_INVALID_PARAMETER;
2212 SourceParentFid.cell = SourceParentId.Cell;
2213 SourceParentFid.volume = SourceParentId.Volume;
2214 SourceParentFid.vnode = SourceParentId.Vnode;
2215 SourceParentFid.unique = SourceParentId.Unique;
2216 SourceParentFid.hash = SourceParentId.Hash;
2218 TargetParentFid.cell = TargetParentId.Cell;
2219 TargetParentFid.volume = TargetParentId.Volume;
2220 TargetParentFid.vnode = TargetParentId.Vnode;
2221 TargetParentFid.unique = TargetParentId.Unique;
2222 TargetParentFid.hash = TargetParentId.Hash;
2224 code = cm_GetSCache(&SourceParentFid, &oldDscp, userp, &req);
2226 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache source parent failed code 0x%x", code);
2227 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2228 (*ResultCB)->ResultStatus = status;
2232 lock_ObtainWrite(&oldDscp->rw);
2233 code = cm_SyncOp(oldDscp, NULL, userp, &req, 0,
2234 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2236 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp oldDscp 0x%p failed code 0x%x", oldDscp, code);
2237 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2238 (*ResultCB)->ResultStatus = status;
2239 lock_ReleaseWrite(&oldDscp->rw);
2240 cm_ReleaseSCache(oldDscp);
2244 cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2245 lock_ReleaseWrite(&oldDscp->rw);
2248 if (oldDscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2249 osi_Log1(afsd_logp, "RDR_RenameFileEntry oldDscp 0x%p not a directory", oldDscp);
2250 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2251 cm_ReleaseSCache(oldDscp);
2255 code = cm_GetSCache(&TargetParentFid, &newDscp, userp, &req);
2257 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache target parent failed code 0x%x", code);
2258 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2259 (*ResultCB)->ResultStatus = status;
2260 cm_ReleaseSCache(oldDscp);
2264 lock_ObtainWrite(&newDscp->rw);
2265 code = cm_SyncOp(newDscp, NULL, userp, &req, 0,
2266 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2268 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp newDscp 0x%p failed code 0x%x", newDscp, code);
2269 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2270 (*ResultCB)->ResultStatus = status;
2271 lock_ReleaseWrite(&newDscp->rw);
2272 cm_ReleaseSCache(oldDscp);
2273 cm_ReleaseSCache(newDscp);
2277 cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2278 lock_ReleaseWrite(&newDscp->rw);
2281 if (newDscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2282 osi_Log1(afsd_logp, "RDR_RenameFileEntry newDscp 0x%p not a directory", newDscp);
2283 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2284 cm_ReleaseSCache(oldDscp);
2285 cm_ReleaseSCache(newDscp);
2289 code = cm_Rename( oldDscp, NULL, SourceFileName,
2290 newDscp, TargetFileName, userp, &req);
2294 cm_scache_t *scp = 0;
2297 (*ResultCB)->ResultBufferLength = ResultBufferLength;
2298 dwRemaining = ResultBufferLength - sizeof( AFSFileRenameResultCB) + sizeof( AFSDirEnumEntry);
2299 (*ResultCB)->ResultStatus = 0;
2301 pResultCB->SourceParentDataVersion.QuadPart = oldDscp->dataVersion;
2302 pResultCB->TargetParentDataVersion.QuadPart = newDscp->dataVersion;
2304 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_Rename oldDscp 0x%p newDscp 0x%p SUCCESS",
2307 code = cm_BeginDirOp( newDscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
2309 code = cm_BPlusDirLookup(&dirop, TargetFileName, &targetFid);
2310 cm_EndDirOp(&dirop);
2314 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_BPlusDirLookup failed code 0x%x",
2316 (*ResultCB)->ResultStatus = STATUS_OBJECT_PATH_INVALID;
2317 cm_ReleaseSCache(oldDscp);
2318 cm_ReleaseSCache(newDscp);
2322 osi_Log4(afsd_logp, "RDR_RenameFileEntry Target FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2323 targetFid.cell, targetFid.volume,
2324 targetFid.vnode, targetFid.unique);
2326 code = cm_GetSCache(&targetFid, &scp, userp, &req);
2328 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache target failed code 0x%x", code);
2329 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2330 (*ResultCB)->ResultStatus = status;
2331 cm_ReleaseSCache(oldDscp);
2332 cm_ReleaseSCache(newDscp);
2336 /* Make sure the source vnode is current */
2337 lock_ObtainWrite(&scp->rw);
2338 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2339 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2341 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp scp 0x%p failed code 0x%x", scp, code);
2342 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2343 (*ResultCB)->ResultStatus = status;
2344 lock_ReleaseWrite(&scp->rw);
2345 cm_ReleaseSCache(oldDscp);
2346 cm_ReleaseSCache(newDscp);
2347 cm_ReleaseSCache(scp);
2351 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2352 lock_ReleaseWrite(&scp->rw);
2354 dfid.vnode = htonl(scp->fid.vnode);
2355 dfid.unique = htonl(scp->fid.unique);
2357 if (!cm_Is8Dot3(TargetFileName))
2358 cm_Gen8Dot3NameIntW(TargetFileName, &dfid, shortName, NULL);
2360 shortName[0] = '\0';
2362 RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
2363 newDscp, scp, userp, &req, TargetFileName, shortName,
2364 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
2365 NULL, &dwRemaining);
2366 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
2367 cm_ReleaseSCache(scp);
2369 osi_Log0(afsd_logp, "RDR_RenameFileEntry SUCCESS");
2371 osi_Log3(afsd_logp, "RDR_RenameFileEntry cm_Rename oldDscp 0x%p newDscp 0x%p failed code 0x%x",
2372 oldDscp, newDscp, code);
2373 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2374 (*ResultCB)->ResultStatus = status;
2375 (*ResultCB)->ResultBufferLength = 0;
2378 cm_ReleaseSCache(oldDscp);
2379 cm_ReleaseSCache(newDscp);
2384 RDR_FlushFileEntry( IN cm_user_t *userp,
2385 IN AFSFileID FileId,
2387 IN DWORD ResultBufferLength,
2388 IN OUT AFSCommResult **ResultCB)
2390 cm_scache_t *scp = NULL;
2401 req.flags |= CM_REQ_WOW64;
2403 osi_Log4(afsd_logp, "RDR_FlushFileEntry File FID cell 0x%x vol 0x%x vno 0x%x uniq 0x%x",
2404 FileId.Cell, FileId.Volume,
2405 FileId.Vnode, FileId.Unique);
2407 snprintf( dbgstr, 1024,
2408 "RDR_FlushFileEntry File FID cell 0x%x vol 0x%x vno 0x%x uniq 0x%x\n",
2409 FileId.Cell, FileId.Volume,
2410 FileId.Vnode, FileId.Unique);
2411 OutputDebugStringA( dbgstr);
2414 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult));
2416 osi_Log0(afsd_logp, "RDR_FlushFileEntry out of memory");
2422 sizeof( AFSCommResult));
2424 /* Process the release */
2425 Fid.cell = FileId.Cell;
2426 Fid.volume = FileId.Volume;
2427 Fid.vnode = FileId.Vnode;
2428 Fid.unique = FileId.Unique;
2429 Fid.hash = FileId.Hash;
2431 code = cm_GetSCache(&Fid, &scp, userp, &req);
2433 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2434 (*ResultCB)->ResultStatus = status;
2435 osi_Log2(afsd_logp, "RDR_FlushFileEntry cm_GetSCache FID failure code=0x%x status=0x%x",
2440 lock_ObtainWrite(&scp->rw);
2441 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2442 lock_ReleaseWrite(&scp->rw);
2443 (*ResultCB)->ResultStatus = STATUS_INVALID_HANDLE;
2447 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2448 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2450 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2451 (*ResultCB)->ResultStatus = status;
2452 lock_ReleaseWrite(&scp->rw);
2453 cm_ReleaseSCache(scp);
2454 osi_Log3(afsd_logp, "RDR_FlushFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2459 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2460 lock_ReleaseWrite(&scp->rw);
2462 code = cm_FSync(scp, userp, &req, FALSE);
2463 cm_ReleaseSCache(scp);
2466 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2467 (*ResultCB)->ResultStatus = status;
2468 osi_Log2(afsd_logp, "RDR_FlushFileEntry FAILURE code=0x%x status=0x%x",
2471 (*ResultCB)->ResultStatus = 0;
2472 osi_Log0(afsd_logp, "RDR_FlushFileEntry SUCCESS");
2474 (*ResultCB)->ResultBufferLength = 0;
2480 RDR_CheckAccess( IN cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
2484 ULONG afs_acc, afs_gr;
2486 afs_uint32 code = 0;
2488 file = (scp->fileType == CM_SCACHETYPE_FILE);
2491 /* access definitions from prs_fs.h */
2493 if (access & FILE_READ_DATA)
2494 afs_acc |= PRSFS_READ;
2495 if (access & FILE_READ_EA || access & FILE_READ_ATTRIBUTES)
2496 afs_acc |= PRSFS_READ;
2497 if (file && ((access & FILE_WRITE_DATA) || (access & FILE_APPEND_DATA)))
2498 afs_acc |= PRSFS_WRITE;
2499 if (access & FILE_WRITE_EA || access & FILE_WRITE_ATTRIBUTES)
2500 afs_acc |= PRSFS_WRITE;
2501 if (dir && ((access & FILE_ADD_FILE) || (access & FILE_ADD_SUBDIRECTORY)))
2502 afs_acc |= PRSFS_INSERT;
2503 if (dir && (access & FILE_LIST_DIRECTORY))
2504 afs_acc |= PRSFS_LOOKUP;
2505 if (file && (access & FILE_EXECUTE))
2506 afs_acc |= PRSFS_WRITE;
2507 if (dir && (access & FILE_TRAVERSE))
2508 afs_acc |= PRSFS_READ;
2509 if (dir && (access & FILE_DELETE_CHILD))
2510 afs_acc |= PRSFS_DELETE;
2511 if ((access & DELETE))
2512 afs_acc |= PRSFS_DELETE;
2514 /* check ACL with server */
2515 lock_ObtainWrite(&scp->rw);
2518 if (cm_HaveAccessRights(scp, userp, reqp, afs_acc, &afs_gr))
2524 /* we don't know the required access rights */
2525 code = cm_GetAccessRights(scp, userp, reqp);
2531 lock_ReleaseWrite(&(scp->rw));
2535 if (afs_gr & PRSFS_READ)
2536 *granted |= FILE_READ_DATA | FILE_READ_EA | FILE_READ_ATTRIBUTES | FILE_EXECUTE;
2537 if (afs_gr & PRSFS_WRITE)
2538 *granted |= FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES | FILE_EXECUTE;
2539 if (afs_gr & PRSFS_INSERT)
2540 *granted |= (dir ? FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY : 0) | (file ? FILE_ADD_SUBDIRECTORY : 0);
2541 if (afs_gr & PRSFS_LOOKUP)
2542 *granted |= (dir ? FILE_LIST_DIRECTORY : 0);
2543 if (afs_gr & PRSFS_DELETE)
2544 *granted |= FILE_DELETE_CHILD | DELETE;
2545 if (afs_gr & PRSFS_LOCK)
2547 if (afs_gr & PRSFS_ADMINISTER)
2550 *granted |= SYNCHRONIZE | READ_CONTROL;
2552 /* don't give more access than what was requested */
2554 osi_Log3(afsd_logp, "RDR_CheckAccess SUCCESS scp=0x%p requested=0x%x granted=0x%x", scp, access, *granted);
2556 osi_Log2(afsd_logp, "RDR_CheckAccess FAILURE scp=0x%p code=0x%x",
2563 RDR_OpenFileEntry( IN cm_user_t *userp,
2564 IN AFSFileID FileId,
2565 IN AFSFileOpenCB *OpenCB,
2568 IN DWORD ResultBufferLength,
2569 IN OUT AFSCommResult **ResultCB)
2571 AFSFileOpenResultCB *pResultCB = NULL;
2572 cm_scache_t *scp = NULL;
2573 cm_user_t *sysUserp = NULL;
2575 cm_lock_data_t *ldp = NULL;
2582 req.flags |= CM_REQ_WOW64;
2584 osi_Log4(afsd_logp, "RDR_OpenFileEntry File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2585 FileId.Cell, FileId.Volume,
2586 FileId.Vnode, FileId.Unique);
2588 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult) + sizeof( AFSFileOpenResultCB));
2590 osi_Log0(afsd_logp, "RDR_OpenFileEntry out of memory");
2596 sizeof( AFSCommResult) + sizeof( AFSFileOpenResultCB));
2598 pResultCB = (AFSFileOpenResultCB *)(*ResultCB)->ResultData;
2600 /* Process the release */
2601 Fid.cell = FileId.Cell;
2602 Fid.volume = FileId.Volume;
2603 Fid.vnode = FileId.Vnode;
2604 Fid.unique = FileId.Unique;
2605 Fid.hash = FileId.Hash;
2607 code = cm_GetSCache(&Fid, &scp, userp, &req);
2609 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2610 (*ResultCB)->ResultStatus = status;
2611 osi_Log2(afsd_logp, "RDR_OpenFileEntry cm_GetSCache FID failure code=0x%x status=0x%x",
2616 lock_ObtainWrite(&scp->rw);
2617 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2618 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2620 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2621 (*ResultCB)->ResultStatus = status;
2622 lock_ReleaseWrite(&scp->rw);
2623 cm_ReleaseSCache(scp);
2624 osi_Log3(afsd_logp, "RDR_OpenFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2629 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2630 lock_ReleaseWrite(&scp->rw);
2632 sysUserp = RDR_GetLocalSystemUser();
2635 * Skip the open check if the request is coming from the local system account.
2636 * The local system has no tokens and therefore any requests sent to a file
2637 * server will fail. Unfortunately, there are special system processes that
2638 * perform actions on files and directories in preparation for memory mapping
2639 * executables. If the open check fails, the real request from the user process
2640 * will never be issued.
2642 * Permitting the file system to allow subsequent operations to proceed does
2643 * not compromise security. All requests to obtain file data or directory
2644 * enumerations will subsequently fail if they are not submitted under the
2645 * context of a process for that have access to the necessary credentials.
2648 if ( userp == sysUserp)
2650 osi_Log1(afsd_logp, "RDR_OpenFileEntry LOCAL_SYSTEM access check skipped scp=0x%p",
2652 pResultCB->GrantedAccess = OpenCB->DesiredAccess;
2653 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
2664 "RDR_OpenFileEntry repeating open check scp=0x%p userp=0x%p code=0x%x",
2667 code = cm_CheckNTOpen(scp, OpenCB->DesiredAccess, OpenCB->ShareAccess,
2669 OpenCB->ProcessId, OpenCB->Identifier,
2672 code = RDR_CheckAccess(scp, userp, &req, OpenCB->DesiredAccess, &pResultCB->GrantedAccess);
2673 cm_CheckNTOpenDone(scp, userp, &req, &ldp);
2674 } while (count < 100 && (code == CM_ERROR_RETRY || code == CM_ERROR_WOULDBLOCK));
2678 * If we are restricting sharing, we should do so with a suitable
2681 if (code == 0 && scp->fileType == CM_SCACHETYPE_FILE && !(OpenCB->ShareAccess & FILE_SHARE_WRITE)) {
2683 LARGE_INTEGER LOffset, LLength;
2686 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
2687 LOffset.LowPart = SMB_FID_QLOCK_LOW;
2688 LLength.HighPart = 0;
2689 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
2692 * If we are not opening the file for writing, then we don't
2693 * try to get an exclusive lock. No one else should be able to
2694 * get an exclusive lock on the file anyway, although someone
2695 * else can get a shared lock.
2697 if ((OpenCB->ShareAccess & FILE_SHARE_READ) || !(OpenCB->DesiredAccess & AFS_ACCESS_WRITE))
2699 sLockType = LOCKING_ANDX_SHARED_LOCK;
2704 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, OpenCB->Identifier);
2706 lock_ObtainWrite(&scp->rw);
2707 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, &req, NULL);
2708 lock_ReleaseWrite(&scp->rw);
2711 code = CM_ERROR_SHARING_VIOLATION;
2712 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
2714 if (sLockType == LOCKING_ANDX_SHARED_LOCK)
2715 pResultCB->FileAccess = AFS_FILE_ACCESS_SHARED;
2717 pResultCB->FileAccess = AFS_FILE_ACCESS_EXCLUSIVE;
2720 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
2723 cm_ReleaseUser(sysUserp);
2724 if (code == 0 && bHoldFid)
2725 RDR_FlagScpInUse( scp, FALSE );
2726 cm_ReleaseSCache(scp);
2729 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2730 (*ResultCB)->ResultStatus = status;
2731 osi_Log2(afsd_logp, "RDR_OpenFileEntry FAILURE code=0x%x status=0x%x",
2734 (*ResultCB)->ResultStatus = 0;
2735 (*ResultCB)->ResultBufferLength = sizeof( AFSFileOpenResultCB);
2736 osi_Log0(afsd_logp, "RDR_OpenFileEntry SUCCESS");
2742 RDR_ReleaseFileAccess( IN cm_user_t *userp,
2743 IN AFSFileID FileId,
2744 IN AFSFileAccessReleaseCB *ReleaseFileCB,
2746 IN DWORD ResultBufferLength,
2747 IN OUT AFSCommResult **ResultCB)
2750 unsigned int sLockType;
2751 LARGE_INTEGER LOffset, LLength;
2752 cm_scache_t *scp = NULL;
2760 req.flags |= CM_REQ_WOW64;
2762 osi_Log4(afsd_logp, "RDR_ReleaseFileAccess File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2763 FileId.Cell, FileId.Volume,
2764 FileId.Vnode, FileId.Unique);
2766 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult));
2768 osi_Log0(afsd_logp, "RDR_ReleaseFileAccess out of memory");
2772 memset( *ResultCB, '\0', sizeof( AFSCommResult));
2774 if (ReleaseFileCB->FileAccess == AFS_FILE_ACCESS_NOLOCK)
2777 /* Process the release */
2778 Fid.cell = FileId.Cell;
2779 Fid.volume = FileId.Volume;
2780 Fid.vnode = FileId.Vnode;
2781 Fid.unique = FileId.Unique;
2782 Fid.hash = FileId.Hash;
2784 code = cm_GetSCache(&Fid, &scp, userp, &req);
2786 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2787 (*ResultCB)->ResultStatus = status;
2788 osi_Log2(afsd_logp, "RDR_ReleaseFileAccess cm_GetSCache FID failure code=0x%x status=0x%x",
2793 if (ReleaseFileCB->FileAccess == AFS_FILE_ACCESS_SHARED)
2794 sLockType = LOCKING_ANDX_SHARED_LOCK;
2798 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, ReleaseFileCB->Identifier);
2800 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
2801 LOffset.LowPart = SMB_FID_QLOCK_LOW;
2802 LLength.HighPart = 0;
2803 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
2805 lock_ObtainWrite(&scp->rw);
2807 code = cm_SyncOp(scp, NULL, userp, &req, 0, CM_SCACHESYNC_LOCK);
2810 code = cm_Unlock(scp, sLockType, LOffset, LLength, key, 0, userp, &req);
2812 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
2814 if (code == CM_ERROR_RANGE_NOT_LOCKED)
2816 osi_Log3(afsd_logp, "RDR_ReleaseFileAccess Range Not Locked -- FileAccess 0x%x ProcessId 0x%x HandleId 0x%x",
2817 ReleaseFileCB->FileAccess, ReleaseFileCB->ProcessId, ReleaseFileCB->Identifier);
2821 lock_ReleaseWrite(&scp->rw);
2823 osi_Log0(afsd_logp, "RDR_ReleaseFileAccessEntry SUCCESS");
2827 HexCheckSum(unsigned char * buf, int buflen, unsigned char * md5cksum)
2830 static char tr[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
2833 return "buffer length too small to HexCheckSum";
2835 for (i=0;i<16;i++) {
2838 buf[i*2] = tr[k / 16];
2839 buf[i*2+1] = tr[k % 16];
2847 * Extent requests from the file system are triggered when a file
2848 * page is not resident in the Windows cache. The file system is
2849 * responsible for loading the page but cannot block the request
2850 * while doing so. The AFS Redirector forwards the requests to
2851 * the AFS cache manager while indicating to Windows that the page
2852 * is not yet available. A polling operation will then ensue with
2853 * the AFS Redirector issuing a RDR_RequestFileExtentsXXX call for
2854 * each poll attempt. As each request is received and processed
2855 * by a separate worker thread in the service, this can lead to
2856 * contention by multiple threads attempting to claim the same
2857 * cm_buf_t objects. Therefore, it is important that
2859 * (a) the service avoid processing more than one overlapping
2860 * extent request at a time
2861 * (b) background daemon processing be used to avoid blocking
2864 * Beginning with the 20091122 build of the redirector, the redirector
2865 * will not issue an additional RDR_RequestFileExtentsXXX call for
2866 * each poll request. Instead, afsd_service is required to track
2867 * the requests and return them to the redirector or fail the
2868 * portions of the request that cannot be satisfied.
2870 * The request processing returns any extents that can be returned
2871 * immediately to the redirector. The rest of the requested range(s)
2872 * are queued as background operations using RDR_BkgFetch().
2875 /* do the background fetch. */
2877 RDR_BkgFetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
2878 cm_user_t *userp, cm_req_t *reqp)
2884 osi_hyper_t fetched;
2885 osi_hyper_t tblocksize;
2888 cm_buf_t *bufp = NULL;
2889 DWORD dwResultBufferLength;
2890 AFSSetFileExtentsCB *pResultCB;
2894 int reportErrorToRedir = 0;
2895 int force_retry = 0;
2897 FileId.Cell = scp->fid.cell;
2898 FileId.Volume = scp->fid.volume;
2899 FileId.Vnode = scp->fid.vnode;
2900 FileId.Unique = scp->fid.unique;
2901 FileId.Hash = scp->fid.hash;
2903 if ((GetTickCount() - reqp->startTime) / 1000 > HardDeadtimeout * 5) {
2904 RDR_SetFileStatus( &scp->fid, &userp->authgroup, STATUS_IO_TIMEOUT);
2908 fetched.LowPart = 0;
2909 fetched.HighPart = 0;
2910 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
2913 length.LowPart = p3;
2914 length.HighPart = p4;
2916 end = LargeIntegerAdd(base, length);
2918 osi_Log5(afsd_logp, "Starting BKG Fetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
2919 scp, p2, p1, p4, p3);
2922 * Make sure we have a callback.
2923 * This is necessary so that we can return access denied
2924 * if a callback cannot be granted.
2926 lock_ObtainWrite(&scp->rw);
2927 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_READ,
2928 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2930 lock_ReleaseWrite(&scp->rw);
2931 osi_Log2(afsd_logp, "RDR_BkgFetch cm_SyncOp failure scp=0x%p code=0x%x",
2933 smb_MapNTError(cm_MapRPCError(code, reqp), &status, TRUE);
2934 RDR_SetFileStatus( &scp->fid, &userp->authgroup, status);
2937 lock_ReleaseWrite(&scp->rw);
2939 dwResultBufferLength = (DWORD)(sizeof( AFSSetFileExtentsCB) + sizeof( AFSSetFileExtentsCB) * (length.QuadPart / cm_data.blockSize + 1));
2940 pResultCB = (AFSSetFileExtentsCB *)malloc( dwResultBufferLength );
2942 return CM_ERROR_RETRY;
2944 memset( pResultCB, '\0', dwResultBufferLength );
2945 pResultCB->FileId = FileId;
2947 for ( code = 0, offset = base;
2948 code == 0 && LargeIntegerLessThan(offset, end);
2949 offset = LargeIntegerAdd(offset, tblocksize) )
2951 int bBufRelease = TRUE;
2954 lock_ReleaseWrite(&scp->rw);
2958 code = buf_Get(scp, &offset, reqp, &bufp);
2961 * any error from buf_Get() is non-fatal.
2962 * we need to re-queue this extent fetch.
2969 lock_ObtainWrite(&scp->rw);
2973 code = cm_GetBuffer(scp, bufp, NULL, userp, reqp);
2975 if (!(bufp->qFlags & CM_BUF_QREDIR)) {
2976 #ifdef VALIDATE_CHECK_SUM
2982 if (bufp->flags & CM_BUF_DIRTY)
2983 cm_BufWrite(scp, &bufp->offset, cm_data.buf_blockSize, CM_BUF_WRITE_SCP_LOCKED, userp, reqp);
2985 lock_ObtainWrite(&buf_globalLock);
2986 if (!(bufp->flags & CM_BUF_DIRTY) &&
2987 bufp->cmFlags == 0 &&
2988 !(bufp->qFlags & CM_BUF_QREDIR)) {
2989 buf_InsertToRedirQueue(scp, bufp);
2990 lock_ReleaseWrite(&buf_globalLock);
2992 #ifdef VALIDATE_CHECK_SUM
2993 buf_ComputeCheckSum(bufp);
2995 pResultCB->FileExtents[count].Flags = 0;
2996 pResultCB->FileExtents[count].FileOffset.QuadPart = bufp->offset.QuadPart;
2997 pResultCB->FileExtents[count].CacheOffset.QuadPart = bufp->datap - RDR_extentBaseAddress;
2998 pResultCB->FileExtents[count].Length = cm_data.blockSize;
3000 fetched = LargeIntegerAdd(fetched, tblocksize);
3001 bBufRelease = FALSE;
3003 #ifdef VALIDATE_CHECK_SUM
3005 HexCheckSum(md5dbg, sizeof(md5dbg), bufp->md5cksum);
3006 snprintf( dbgstr, 1024,
3007 "RDR_BkgFetch md5 %s vol 0x%x vno 0x%x uniq 0x%x foffset 0x%x:%x coffset 0x%x:%x\n",
3009 scp->fid.volume, scp->fid.vnode, scp->fid.unique,
3010 pResultCB->FileExtents[count].FileOffset.HighPart,
3011 pResultCB->FileExtents[count].FileOffset.LowPart,
3012 pResultCB->FileExtents[count].CacheOffset.HighPart,
3013 pResultCB->FileExtents[count].CacheOffset.LowPart);
3014 OutputDebugStringA( dbgstr);
3017 osi_Log4(afsd_logp, "RDR_BkgFetch Extent2FS bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3018 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3020 lock_ReleaseWrite(&buf_globalLock);
3021 if ((bufp->cmFlags != 0) || (bufp->flags & CM_BUF_DIRTY)) {
3022 /* An I/O operation is already in progress */
3024 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",
3025 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3027 osi_Log4(afsd_logp, "RDR_BkgFetch Extent2FS Already held by Redirector bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3028 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3032 osi_Log4(afsd_logp, "RDR_BkgFetch Extent2FS Already held by Redirector bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3033 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3037 * depending on what the error from cm_GetBuffer is
3038 * it may or may not be fatal. Only return fatal errors.
3039 * Re-queue a request for others.
3041 osi_Log5(afsd_logp, "RDR_BkgFetch Extent2FS FAILURE bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x code 0x%x",
3042 bufp, offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize, code);
3044 case CM_ERROR_NOACCESS:
3045 case CM_ERROR_NOSUCHFILE:
3046 case CM_ERROR_NOSUCHPATH:
3047 case CM_ERROR_NOSUCHVOLUME:
3048 case CM_ERROR_NOSUCHCELL:
3049 case CM_ERROR_INVAL:
3050 case CM_ERROR_BADFD:
3051 case CM_ERROR_CLOCKSKEW:
3053 case CM_ERROR_QUOTA:
3054 case CM_ERROR_LOCK_CONFLICT:
3056 * these are fatal errors. deliver what we can
3059 reportErrorToRedir = 1;
3063 * non-fatal errors. re-queue the exent
3065 code = CM_ERROR_RETRY;
3075 lock_ObtainWrite(&scp->rw);
3079 /* wakeup anyone who is waiting */
3080 if (scp->flags & CM_SCACHEFLAG_WAITING) {
3081 osi_Log1(afsd_logp, "RDR Bkg Fetch Waking scp 0x%p", scp);
3082 osi_Wakeup((LONG_PTR) &scp->flags);
3084 lock_ReleaseWrite(&scp->rw);
3087 pResultCB->ExtentCount = count;
3088 RDR_SetFileExtents( pResultCB, dwResultBufferLength);
3092 if (reportErrorToRedir) {
3093 smb_MapNTError(cm_MapRPCError(code, reqp), &status, TRUE);
3094 RDR_SetFileStatus( &scp->fid, &userp->authgroup, status);
3097 osi_Log4(afsd_logp, "Ending BKG Fetch scp 0x%p code 0x%x fetched 0x%x:%x",
3098 scp, code, fetched.HighPart, fetched.LowPart);
3100 return force_retry ? CM_ERROR_RETRY : code;
3105 RDR_RequestFileExtentsAsync( IN cm_user_t *userp,
3106 IN AFSFileID FileId,
3107 IN AFSRequestExtentsCB *RequestExtentsCB,
3109 IN OUT DWORD * ResultBufferLength,
3110 IN OUT AFSSetFileExtentsCB **ResultCB)
3112 AFSSetFileExtentsCB *pResultCB = NULL;
3116 cm_scache_t *scp = NULL;
3119 afs_uint32 code = 0;
3121 LARGE_INTEGER ByteOffset, BeginOffset, EndOffset, QueueOffset;
3122 afs_uint32 QueueLength;
3124 BOOLEAN bBufRelease = TRUE;
3128 req.flags |= CM_REQ_WOW64;
3129 req.flags |= CM_REQ_NORETRY;
3131 osi_Log4(afsd_logp, "RDR_RequestFileExtentsAsync File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
3132 FileId.Cell, FileId.Volume,
3133 FileId.Vnode, FileId.Unique);
3134 osi_Log4(afsd_logp, "... Flags 0x%x ByteOffset 0x%x:%x Length 0x%x",
3135 RequestExtentsCB->Flags,
3136 RequestExtentsCB->ByteOffset.HighPart, RequestExtentsCB->ByteOffset.LowPart,
3137 RequestExtentsCB->Length);
3138 Length = sizeof( AFSSetFileExtentsCB) + sizeof( AFSFileExtentCB) * (RequestExtentsCB->Length / cm_data.blockSize + 1);
3140 pResultCB = *ResultCB = (AFSSetFileExtentsCB *)malloc( Length );
3141 if (*ResultCB == NULL) {
3142 *ResultBufferLength = 0;
3145 *ResultBufferLength = Length;
3147 memset( pResultCB, '\0', Length );
3148 pResultCB->FileId = FileId;
3150 Fid.cell = FileId.Cell;
3151 Fid.volume = FileId.Volume;
3152 Fid.vnode = FileId.Vnode;
3153 Fid.unique = FileId.Unique;
3154 Fid.hash = FileId.Hash;
3156 code = cm_GetSCache(&Fid, &scp, userp, &req);
3158 osi_Log1(afsd_logp, "RDR_RequestFileExtentsAsync cm_GetSCache FID failure code=0x%x",
3160 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3165 * Make sure we have a callback.
3166 * This is necessary so that we can return access denied
3167 * if a callback cannot be granted.
3169 lock_ObtainWrite(&scp->rw);
3170 code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_READ,
3171 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
3172 lock_ReleaseWrite(&scp->rw);
3174 cm_ReleaseSCache(scp);
3175 osi_Log2(afsd_logp, "RDR_RequestFileExtentsAsync cm_SyncOp failure scp=0x%p code=0x%x",
3177 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3178 RDR_SetFileStatus( &scp->fid, &userp->authgroup, status);
3182 /* Allocate the extents from the buffer package */
3184 ByteOffset = BeginOffset = RequestExtentsCB->ByteOffset,
3185 EndOffset.QuadPart = ByteOffset.QuadPart + RequestExtentsCB->Length;
3186 code == 0 && ByteOffset.QuadPart < EndOffset.QuadPart;
3187 ByteOffset.QuadPart += cm_data.blockSize)
3189 BOOL bHaveBuffer = FALSE;
3192 thyper.QuadPart = ByteOffset.QuadPart;
3194 code = buf_Get(scp, &thyper, &req, &bufp);
3196 lock_ObtainMutex(&bufp->mx);
3199 if (bufp->qFlags & CM_BUF_QREDIR) {
3201 } else if (bufp->flags & CM_BUF_DIRTY) {
3202 bHaveBuffer = FALSE;
3204 code = buf_CleanAsyncLocked(scp, bufp, &req, 0, NULL);
3209 case CM_ERROR_RETRY:
3210 /* Couldn't flush it, obtain it asynchronously so we don't block the thread. */
3211 bHaveBuffer = FALSE;
3215 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3216 RDR_SetFileStatus(&FileId, &userp->authgroup, status);
3217 bHaveBuffer = FALSE;
3222 osi_hyper_t minLength; /* effective end of file */
3224 lock_ObtainRead(&scp->rw);
3225 bHaveBuffer = cm_HaveBuffer(scp, bufp, TRUE);
3227 if (LargeIntegerGreaterThan(scp->length, scp->serverLength))
3228 minLength = scp->serverLength;
3230 minLength = scp->length;
3233 LargeIntegerGreaterThanOrEqualTo(bufp->offset, minLength)) {
3234 memset(bufp->datap, 0, cm_data.buf_blockSize);
3235 bufp->dataVersion = scp->dataVersion;
3238 else if ((RequestExtentsCB->Flags & AFS_EXTENT_FLAG_CLEAN) &&
3239 ByteOffset.QuadPart <= bufp->offset.QuadPart &&
3240 EndOffset.QuadPart >= bufp->offset.QuadPart + cm_data.blockSize)
3242 memset(bufp->datap, 0, cm_data.blockSize);
3243 bufp->dataVersion = scp->dataVersion;
3244 buf_SetDirty(bufp, &req, 0, cm_data.blockSize, userp);
3247 lock_ReleaseRead(&scp->rw);
3251 * if this buffer is already up to date, skip it.
3254 if (ByteOffset.QuadPart == BeginOffset.QuadPart) {
3255 BeginOffset.QuadPart += cm_data.blockSize;
3257 QueueLength = (afs_uint32)(ByteOffset.QuadPart - BeginOffset.QuadPart);
3258 QueueOffset = BeginOffset;
3259 BeginOffset = ByteOffset;
3262 if (!(bufp->qFlags & CM_BUF_QREDIR)) {
3263 #ifdef VALIDATE_CHECK_SUM
3269 lock_ObtainWrite(&buf_globalLock);
3270 if (!(bufp->qFlags & CM_BUF_QREDIR)) {
3271 buf_InsertToRedirQueue(scp, bufp);
3272 lock_ReleaseWrite(&buf_globalLock);
3274 #ifdef VALIDATE_CHECK_SUM
3275 buf_ComputeCheckSum(bufp);
3277 /* we already have the buffer, return it now */
3278 pResultCB->FileExtents[count].Flags = 0;
3279 pResultCB->FileExtents[count].FileOffset = ByteOffset;
3280 pResultCB->FileExtents[count].CacheOffset.QuadPart = bufp->datap - RDR_extentBaseAddress;
3281 pResultCB->FileExtents[count].Length = cm_data.blockSize;
3284 bBufRelease = FALSE;
3286 #ifdef VALIDATE_CHECK_SUM
3288 HexCheckSum(md5dbg, sizeof(md5dbg), bufp->md5cksum);
3289 snprintf( dbgstr, 1024,
3290 "RDR_RequestFileExtentsAsync md5 %s vol 0x%x vno 0x%x uniq 0x%x foffset 0x%x:%x coffset 0x%x:%x\n",
3292 scp->fid.volume, scp->fid.vnode, scp->fid.unique,
3293 pResultCB->FileExtents[count].FileOffset.HighPart,
3294 pResultCB->FileExtents[count].FileOffset.LowPart,
3295 pResultCB->FileExtents[count].CacheOffset.HighPart,
3296 pResultCB->FileExtents[count].CacheOffset.LowPart);
3297 OutputDebugStringA( dbgstr);
3300 osi_Log4(afsd_logp, "RDR_RequestFileExtentsAsync Extent2FS bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3301 bufp, ByteOffset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3303 lock_ReleaseWrite(&buf_globalLock);
3308 * The service is not handing off the extent to the redirector in this pass.
3309 * However, we know the buffer is in recent use so move the buffer to the
3310 * front of the queue
3312 lock_ObtainWrite(&buf_globalLock);
3313 buf_MoveToHeadOfRedirQueue(scp, bufp);
3314 lock_ReleaseWrite(&buf_globalLock);
3316 osi_Log4(afsd_logp, "RDR_RequestFileExtentsAsync Extent2FS Already held by Redirector bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3317 bufp, ByteOffset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3321 lock_ReleaseMutex(&bufp->mx);
3326 cm_QueueBKGRequest(scp, RDR_BkgFetch, QueueOffset.LowPart, QueueOffset.HighPart,
3327 QueueLength, 0, userp, &req);
3328 osi_Log3(afsd_logp, "RDR_RequestFileExtentsAsync Queued a Background Fetch offset 0x%x:%x length 0x%x",
3329 QueueOffset.HighPart, QueueOffset.LowPart, QueueLength);