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 unp->flags |= SMB_USERNAMEFLAG_SID;
237 lock_ReleaseMutex(&unp->mx);
240 smb_ReleaseUsername(unp);
244 userp = cm_rootUserp;
248 osi_Log2(afsd_logp, "RDR_UserFromCommRequest Guid %S userp = 0x%p",
249 osi_LogSaveStringW(afsd_logp, UuidString),
253 RpcStringFreeW(&UuidString);
259 RDR_ReleaseUser( IN cm_user_t *userp )
261 osi_Log1(afsd_logp, "RDR_ReleaseUser userp = 0x%p", userp);
262 cm_ReleaseUser(userp);
267 * RDR_FlagScpInUse flags the scp with CM_SCACHEFLAG_RDR_IN_USE
270 RDR_FlagScpInUse( IN cm_scache_t *scp, IN BOOL bLocked )
273 lock_ObtainWrite(&scp->rw);
275 lock_AssertWrite(&scp->rw);
276 scp->flags |= CM_SCACHEFLAG_RDR_IN_USE;
279 lock_ReleaseWrite(&scp->rw);
283 * Obtain the status information for the specified object and
287 RDR_BulkStatLookup( cm_scache_t *dscp,
292 cm_direnum_t * enump = NULL;
296 code = cm_BeginDirOp(dscp, userp, reqp, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
298 code = cm_BPlusDirEnumerate(dscp, userp, reqp, TRUE, NULL, TRUE, &enump);
300 osi_Log1(afsd_logp, "RDR_BulkStatLookup cm_BPlusDirEnumerate failure code=0x%x",
305 osi_Log1(afsd_logp, "RDR_BulkStatLookup cm_BeginDirOp failure code=0x%x",
312 code = cm_BPlusDirEnumBulkStatOne(enump, scp);
314 osi_Log1(afsd_logp, "RDR_BulkStatLookup cm_BPlusDirEnumBulkStatOne failure code=0x%x",
317 cm_BPlusDirFreeEnumeration(enump);
324 #define RDR_POP_FOLLOW_MOUNTPOINTS 0x01
325 #define RDR_POP_EVALUATE_SYMLINKS 0x02
326 #define RDR_POP_WOW64 0x04
327 #define RDR_POP_NO_GETSTATUS 0x08
330 RDR_PopulateCurrentEntry( IN AFSDirEnumEntry * pCurrentEntry,
331 IN DWORD dwMaxEntryLength,
332 IN cm_scache_t * dscp,
333 IN cm_scache_t * scp,
334 IN cm_user_t * userp,
337 IN wchar_t * shortName,
339 OUT AFSDirEnumEntry **ppNextEntry,
340 OUT DWORD * pdwRemainingLength)
343 WCHAR * wname, *wtarget;
346 afs_uint32 code = 0, code2 = 0;
347 BOOL bMustFake = FALSE;
349 osi_Log5(afsd_logp, "RDR_PopulateCurrentEntry dscp=0x%p scp=0x%p name=%S short=%S flags=0x%x",
350 dscp, scp, osi_LogSaveStringW(afsd_logp, name),
351 osi_LogSaveStringW(afsd_logp, shortName), dwFlags);
352 osi_Log1(afsd_logp, "... maxLength=%d", dwMaxEntryLength);
354 if (dwMaxEntryLength < sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t)) {
356 *ppNextEntry = pCurrentEntry;
357 if (pdwRemainingLength)
358 *pdwRemainingLength = dwMaxEntryLength;
359 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry Not Enough Room for Entry %d < %d",
360 dwMaxEntryLength, sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t));
361 return CM_ERROR_TOOBIG;
369 dwEntryLength = sizeof(AFSDirEnumEntry);
371 lock_ObtainWrite(&scp->rw);
372 if (dwFlags & RDR_POP_NO_GETSTATUS) {
373 if (!cm_HaveCallback(scp))
376 #ifdef AFS_FREELANCE_CLIENT
377 if (scp->fid.cell == AFS_FAKE_ROOT_CELL_ID && scp->fid.volume == AFS_FAKE_ROOT_VOL_ID) {
379 * If the FID is from the Freelance Local Root always perform
380 * a single item status check.
382 code = cm_SyncOp( scp, NULL, userp, reqp, 0,
383 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
385 lock_ReleaseWrite(&scp->rw);
386 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_SyncOp failed for scp=0x%p code=0x%x",
394 * For non-Freelance objects, check to see if we have current
395 * status information. If not, perform a bulk status lookup of multiple
396 * entries in order to reduce the number of RPCs issued to the file server.
398 if ((scp->flags & CM_SCACHEFLAG_EACCESS))
400 else if (!cm_HaveCallback(scp)) {
401 lock_ReleaseWrite(&scp->rw);
402 code = RDR_BulkStatLookup(dscp, scp, userp, reqp);
404 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry RXR_BulkStatLookup failed for scp=0x%p code=0x%x",
408 lock_ObtainWrite(&scp->rw);
410 * RDR_BulkStatLookup can succeed but it may be the case that there
411 * still is not valid status info. If we get this far, generate fake
414 if (!cm_HaveCallback(scp))
421 /* Populate the real or fake data */
422 pCurrentEntry->FileId.Cell = scp->fid.cell;
423 pCurrentEntry->FileId.Volume = scp->fid.volume;
424 pCurrentEntry->FileId.Vnode = scp->fid.vnode;
425 pCurrentEntry->FileId.Unique = scp->fid.unique;
426 pCurrentEntry->FileId.Hash = scp->fid.hash;
428 pCurrentEntry->FileType = scp->fileType;
430 pCurrentEntry->DataVersion.QuadPart = scp->dataVersion;
432 if (scp->fid.cell==AFS_FAKE_ROOT_CELL_ID &&
433 scp->fid.volume==AFS_FAKE_ROOT_VOL_ID) {
434 cm_LargeSearchTimeFromUnixTime(&ft, MAX_AFS_UINT32);
436 cm_LargeSearchTimeFromUnixTime(&ft, scp->cbExpires);
438 pCurrentEntry->Expiration.LowPart = ft.dwLowDateTime;
439 pCurrentEntry->Expiration.HighPart = ft.dwHighDateTime;
442 /* 1969-12-31 23:59:59 +00 */
443 ft.dwHighDateTime = 0x19DB200;
444 ft.dwLowDateTime = 0x5BB78980;
446 cm_LargeSearchTimeFromUnixTime(&ft, scp->clientModTime);
447 pCurrentEntry->CreationTime.LowPart = ft.dwLowDateTime;
448 pCurrentEntry->CreationTime.HighPart = ft.dwHighDateTime;
449 pCurrentEntry->LastAccessTime = pCurrentEntry->CreationTime;
450 pCurrentEntry->LastWriteTime = pCurrentEntry->CreationTime;
451 pCurrentEntry->ChangeTime = pCurrentEntry->CreationTime;
453 pCurrentEntry->EndOfFile = scp->length;
454 pCurrentEntry->AllocationSize = scp->length;
457 switch (scp->fileType) {
458 case CM_SCACHETYPE_DIRECTORY:
459 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY;
461 case CM_SCACHETYPE_MOUNTPOINT:
462 case CM_SCACHETYPE_INVALID:
463 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY | SMB_ATTR_REPARSE_POINT;
465 case CM_SCACHETYPE_SYMLINK:
466 if (cm_TargetPerceivedAsDirectory(scp->mountPointStringp))
467 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY | SMB_ATTR_REPARSE_POINT;
469 pCurrentEntry->FileAttributes = SMB_ATTR_REPARSE_POINT;
472 /* if we get here we either have a normal file
473 * or we have a file for which we have never
474 * received status info. In this case, we can
475 * check the even/odd value of the entry's vnode.
476 * odd means it is to be treated as a directory
477 * and even means it is to be treated as a file.
479 if (scp->fid.vnode & 0x1)
480 pCurrentEntry->FileAttributes = SMB_ATTR_DIRECTORY;
482 pCurrentEntry->FileAttributes = SMB_ATTR_NORMAL;
485 pCurrentEntry->FileAttributes = smb_ExtAttributes(scp);
486 pCurrentEntry->EaSize = 0;
487 pCurrentEntry->Links = scp->linkCount;
489 len = wcslen(shortName);
490 wcsncpy(pCurrentEntry->ShortName, shortName, len);
491 pCurrentEntry->ShortNameLength = (CCHAR)(len * sizeof(WCHAR));
493 pCurrentEntry->FileNameOffset = sizeof(AFSDirEnumEntry);
495 wname = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->FileNameOffset);
496 wcsncpy(wname, name, len);
497 pCurrentEntry->FileNameLength = (ULONG)(sizeof(WCHAR) * len);
499 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry scp=0x%p fileType=%d",
502 if (!(dwFlags & RDR_POP_NO_GETSTATUS))
503 cm_SyncOpDone( scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
505 if ((dwFlags & RDR_POP_NO_GETSTATUS) || !cm_HaveCallback(scp)) {
506 pCurrentEntry->TargetNameOffset = 0;
507 pCurrentEntry->TargetNameLength = 0;
510 switch (scp->fileType) {
511 case CM_SCACHETYPE_MOUNTPOINT:
512 if (dwFlags & RDR_POP_FOLLOW_MOUNTPOINTS) {
513 if ((code2 = cm_ReadMountPoint(scp, userp, reqp)) == 0) {
514 cm_scache_t *targetScp = NULL;
516 pCurrentEntry->TargetNameOffset = pCurrentEntry->FileNameOffset + pCurrentEntry->FileNameLength;
517 len = strlen(scp->mountPointStringp);
518 wtarget = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->TargetNameOffset);
521 cch = MultiByteToWideChar( CP_UTF8, 0, scp->mountPointStringp,
524 len * sizeof(WCHAR));
526 mbstowcs(wtarget, scp->mountPointStringp, len);
528 pCurrentEntry->TargetNameLength = (ULONG)(sizeof(WCHAR) * len);
530 code2 = cm_FollowMountPoint(scp, dscp, userp, reqp, &targetScp);
533 pCurrentEntry->TargetFileId.Cell = targetScp->fid.cell;
534 pCurrentEntry->TargetFileId.Volume = targetScp->fid.volume;
535 pCurrentEntry->TargetFileId.Vnode = targetScp->fid.vnode;
536 pCurrentEntry->TargetFileId.Unique = targetScp->fid.unique;
537 pCurrentEntry->TargetFileId.Hash = targetScp->fid.hash;
539 osi_Log4(afsd_logp, "RDR_PopulateCurrentEntry target FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
540 pCurrentEntry->TargetFileId.Cell, pCurrentEntry->TargetFileId.Volume,
541 pCurrentEntry->TargetFileId.Vnode, pCurrentEntry->TargetFileId.Unique);
543 cm_ReleaseSCache(targetScp);
545 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_FollowMountPoint failed scp=0x%p code=0x%x",
549 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_ReadMountPoint failed scp=0x%p code=0x%x",
554 case CM_SCACHETYPE_SYMLINK:
555 case CM_SCACHETYPE_DFSLINK:
557 pCurrentEntry->TargetNameOffset = pCurrentEntry->FileNameOffset + pCurrentEntry->FileNameLength;
558 wtarget = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->TargetNameOffset);
560 if (dwFlags & RDR_POP_EVALUATE_SYMLINKS) {
563 code2 = cm_HandleLink(scp, userp, reqp);
565 mp = scp->mountPointStringp;
568 /* Strip off the msdfs: prefix from the target name for the file system */
569 if (scp->fileType == CM_SCACHETYPE_DFSLINK) {
570 osi_Log0(afsd_logp, "RDR_PopulateCurrentEntry DFSLink Detected");
571 pCurrentEntry->FileType = scp->fileType;
573 if (!strncmp("msdfs:", mp, 6)) {
578 /* only send one slash to the redirector */
579 if (mp[0] == '\\' && mp[1] == '\\') {
584 cch = MultiByteToWideChar( CP_UTF8, 0, mp,
587 len * sizeof(WCHAR));
589 mbstowcs(wtarget, mp, len);
592 pCurrentEntry->TargetNameLength = (ULONG)(sizeof(WCHAR) * len);
594 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntry cm_HandleLink failed scp=0x%p code=0x%x",
603 pCurrentEntry->TargetNameOffset = 0;
604 pCurrentEntry->TargetNameLength = 0;
606 lock_ReleaseWrite(&scp->rw);
608 dwEntryLength += pCurrentEntry->FileNameLength + pCurrentEntry->TargetNameLength;
609 dwEntryLength += (dwEntryLength % 8) ? 8 - (dwEntryLength % 8) : 0; /* quad align */
611 *ppNextEntry = (AFSDirEnumEntry *)((PBYTE)pCurrentEntry + dwEntryLength);
612 if (pdwRemainingLength)
613 *pdwRemainingLength = dwMaxEntryLength - dwEntryLength;
615 osi_Log3(afsd_logp, "RDR_PopulateCurrentEntry Success FileNameLength=%d TargetNameLength=%d RemainingLength=%d",
616 pCurrentEntry->FileNameLength, pCurrentEntry->TargetNameLength, *pdwRemainingLength);
622 RDR_PopulateCurrentEntryNoScp( IN AFSDirEnumEntry * pCurrentEntry,
623 IN DWORD dwMaxEntryLength,
624 IN cm_scache_t * dscp,
626 IN cm_user_t * userp,
629 IN wchar_t * shortName,
631 OUT AFSDirEnumEntry **ppNextEntry,
632 OUT DWORD * pdwRemainingLength)
638 afs_uint32 code = 0, code2 = 0;
640 osi_Log4(afsd_logp, "RDR_PopulateCurrentEntryNoEntry dscp=0x%p name=%S short=%S flags=0x%x",
641 dscp, osi_LogSaveStringW(afsd_logp, name),
642 osi_LogSaveStringW(afsd_logp, shortName), dwFlags);
643 osi_Log1(afsd_logp, "... maxLength=%d", dwMaxEntryLength);
645 if (dwMaxEntryLength < sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t)) {
647 *ppNextEntry = pCurrentEntry;
648 if (pdwRemainingLength)
649 *pdwRemainingLength = dwMaxEntryLength;
650 osi_Log2(afsd_logp, "RDR_PopulateCurrentEntryNoEntry Not Enough Room for Entry %d < %d",
651 dwMaxEntryLength, sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t));
652 return CM_ERROR_TOOBIG;
660 dwEntryLength = sizeof(AFSDirEnumEntry);
662 pCurrentEntry->FileId.Cell = fidp->cell;
663 pCurrentEntry->FileId.Volume = fidp->volume;
664 pCurrentEntry->FileId.Vnode = fidp->vnode;
665 pCurrentEntry->FileId.Unique = fidp->unique;
666 pCurrentEntry->FileId.Hash = fidp->hash;
668 pCurrentEntry->FileType = CM_SCACHETYPE_UNKNOWN;
670 pCurrentEntry->DataVersion.QuadPart = CM_SCACHE_VERSION_BAD;
672 cm_LargeSearchTimeFromUnixTime(&ft, 0);
673 pCurrentEntry->Expiration.LowPart = ft.dwLowDateTime;
674 pCurrentEntry->Expiration.HighPart = ft.dwHighDateTime;
676 cm_LargeSearchTimeFromUnixTime(&ft, 0);
677 pCurrentEntry->CreationTime.LowPart = ft.dwLowDateTime;
678 pCurrentEntry->CreationTime.HighPart = ft.dwHighDateTime;
679 pCurrentEntry->LastAccessTime = pCurrentEntry->CreationTime;
680 pCurrentEntry->LastWriteTime = pCurrentEntry->CreationTime;
681 pCurrentEntry->ChangeTime = pCurrentEntry->CreationTime;
683 pCurrentEntry->EndOfFile.QuadPart = 0;
684 pCurrentEntry->AllocationSize.QuadPart = 0;
685 pCurrentEntry->FileAttributes = 0;
686 pCurrentEntry->EaSize = 0;
687 pCurrentEntry->Links = 0;
689 len = wcslen(shortName);
690 wcsncpy(pCurrentEntry->ShortName, shortName, len);
691 pCurrentEntry->ShortNameLength = (CCHAR)(len * sizeof(WCHAR));
693 pCurrentEntry->FileNameOffset = sizeof(AFSDirEnumEntry);
695 wname = (WCHAR *)((PBYTE)pCurrentEntry + pCurrentEntry->FileNameOffset);
696 wcsncpy(wname, name, len);
697 pCurrentEntry->FileNameLength = (ULONG)(sizeof(WCHAR) * len);
699 pCurrentEntry->TargetNameOffset = 0;
700 pCurrentEntry->TargetNameLength = 0;
702 dwEntryLength += pCurrentEntry->FileNameLength + pCurrentEntry->TargetNameLength;
703 dwEntryLength += (dwEntryLength % 8) ? 8 - (dwEntryLength % 8) : 0; /* quad align */
705 *ppNextEntry = (AFSDirEnumEntry *)((PBYTE)pCurrentEntry + dwEntryLength);
706 if (pdwRemainingLength)
707 *pdwRemainingLength = dwMaxEntryLength - dwEntryLength;
709 osi_Log3(afsd_logp, "RDR_PopulateCurrentEntryNoScp Success FileNameLength=%d TargetNameLength=%d RemainingLength=%d",
710 pCurrentEntry->FileNameLength, pCurrentEntry->TargetNameLength, *pdwRemainingLength);
716 RDR_EnumerateDirectory( IN cm_user_t *userp,
718 IN AFSDirQueryCB *QueryCB,
721 IN DWORD ResultBufferLength,
722 IN OUT AFSCommResult **ResultCB)
725 cm_direnum_t * enump = NULL;
726 AFSDirEnumResp * pDirEnumResp;
727 AFSDirEnumEntry * pCurrentEntry;
728 size_t size = ResultBufferLength ? sizeof(AFSCommResult) + ResultBufferLength - 1 : sizeof(AFSCommResult);
729 DWORD dwMaxEntryLength;
732 cm_scache_t * dscp = NULL;
737 req.flags |= CM_REQ_WOW64;
739 osi_Log4(afsd_logp, "RDR_EnumerateDirectory FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
740 DirID.Cell, DirID.Volume, DirID.Vnode, DirID.Unique);
742 *ResultCB = (AFSCommResult *)malloc(size);
744 osi_Log0(afsd_logp, "RDR_EnumerateDirectory Out of Memory");
748 memset(*ResultCB, 0, size);
750 if (QueryCB->EnumHandle == (ULONG_PTR)-1) {
751 osi_Log0(afsd_logp, "RDR_EnumerateDirectory No More Entries");
752 (*ResultCB)->ResultStatus = STATUS_NO_MORE_ENTRIES;
753 (*ResultCB)->ResultBufferLength = 0;
757 (*ResultCB)->ResultBufferLength = dwMaxEntryLength = ResultBufferLength;
758 if (ResultBufferLength) {
759 pDirEnumResp = (AFSDirEnumResp *)&(*ResultCB)->ResultData;
760 pCurrentEntry = (AFSDirEnumEntry *)&pDirEnumResp->Entry;
761 dwMaxEntryLength -= FIELD_OFFSET( AFSDirEnumResp, Entry); /* AFSDirEnumResp */
764 if (DirID.Cell != 0) {
765 fid.cell = DirID.Cell;
766 fid.volume = DirID.Volume;
767 fid.vnode = DirID.Vnode;
768 fid.unique = DirID.Unique;
769 fid.hash = DirID.Hash;
771 code = cm_GetSCache(&fid, &dscp, userp, &req);
773 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
774 (*ResultCB)->ResultStatus = status;
775 osi_Log2(afsd_logp, "RDR_EnumerateDirectory cm_GetSCache failure code=0x%x status=0x%x",
780 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
781 osi_Log0(afsd_logp, "RDR_EnumerateDirectory Object Name Invalid - Cell = 0");
785 /* get the directory size */
786 lock_ObtainWrite(&dscp->rw);
787 code = cm_SyncOp(dscp, NULL, userp, &req, PRSFS_LOOKUP,
788 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
790 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
791 (*ResultCB)->ResultStatus = status;
792 lock_ReleaseWrite(&dscp->rw);
793 cm_ReleaseSCache(dscp);
794 osi_Log2(afsd_logp, "RDR_EnumerateDirectory cm_SyncOp failure code=0x%x status=0x%x",
799 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
800 lock_ReleaseWrite(&dscp->rw);
802 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
803 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
804 cm_ReleaseSCache(dscp);
805 osi_Log1(afsd_logp, "RDR_EnumerateDirectory Not a Directory dscp=0x%p",
811 * If there is no enumeration handle, then this is a new query
812 * and we must perform an enumeration for the specified object
814 if (QueryCB->EnumHandle == (ULONG_PTR)NULL) {
817 code = cm_BeginDirOp(dscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
819 code = cm_BPlusDirEnumerate(dscp, userp, &req, TRUE, NULL, !bSkipStatus, &enump);
821 osi_Log1(afsd_logp, "RDR_EnumerateDirectory cm_BPlusDirEnumerate failure code=0x%x",
826 osi_Log1(afsd_logp, "RDR_EnumerateDirectory cm_BeginDirOp failure code=0x%x",
830 enump = (cm_direnum_t *)QueryCB->EnumHandle;
833 if (enump && ResultBufferLength) {
834 cm_direnum_entry_t * entryp = NULL;
837 if (dwMaxEntryLength < sizeof(AFSDirEnumEntry) + (MAX_PATH + MOUNTPOINTLEN) * sizeof(wchar_t)) {
838 osi_Log0(afsd_logp, "RDR_EnumerateDirectory out of space, returning");
842 code = cm_BPlusDirNextEnumEntry(enump, &entryp);
844 if ((code == 0 || code == CM_ERROR_STOPNOW) && entryp) {
846 int stopnow = (code == CM_ERROR_STOPNOW);
848 if ( !wcscmp(L".", entryp->name) || !wcscmp(L"..", entryp->name) ) {
849 osi_Log0(afsd_logp, "RDR_EnumerateDirectory skipping . or ..");
855 if ( FALSE /* bSkipStatus */) {
856 scp = cm_FindSCache(&entryp->fid);
859 code = cm_GetSCache(&entryp->fid, &scp, userp, &req);
864 code = RDR_PopulateCurrentEntry(pCurrentEntry, dwMaxEntryLength,
865 dscp, scp, userp, &req,
867 cm_Is8Dot3(entryp->name) ? NULL : entryp->shortName,
868 (bWow64 ? RDR_POP_WOW64 : 0) |
869 (bSkipStatus ? RDR_POP_NO_GETSTATUS : 0),
870 &pCurrentEntry, &dwMaxEntryLength);
871 cm_ReleaseSCache(scp);
873 code = RDR_PopulateCurrentEntryNoScp( pCurrentEntry, dwMaxEntryLength,
874 dscp, &entryp->fid, userp, &req,
876 cm_Is8Dot3(entryp->name) ? NULL : entryp->shortName,
877 (bWow64 ? RDR_POP_WOW64 : 0),
878 &pCurrentEntry, &dwMaxEntryLength);
884 osi_Log2(afsd_logp, "RDR_EnumerateDirectory cm_GetSCache failure scp=0x%p code=0x%x",
893 if (enump && ResultBufferLength == 0) {
894 code = cm_BPlusDirEnumBulkStat(enump);
896 osi_Log1(afsd_logp, "RDR_EnumerateDirectory cm_BPlusDirEnumBulkStat failure code=0x%x",
902 if (code || enump->next == enump->count || ResultBufferLength == 0) {
903 cm_BPlusDirFreeEnumeration(enump);
904 enump = (cm_direnum_t *)(ULONG_PTR)-1;
907 if (code == 0 || code == CM_ERROR_STOPNOW) {
908 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
909 osi_Log0(afsd_logp, "RDR_EnumerateDirectory SUCCESS");
911 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
912 (*ResultCB)->ResultStatus = status;
913 osi_Log2(afsd_logp, "RDR_EnumerateDirectory Failure code=0x%x status=0x%x",
917 if (ResultBufferLength) {
918 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwMaxEntryLength;
920 pDirEnumResp->EnumHandle = (ULONG_PTR) enump;
924 cm_ReleaseSCache(dscp);
930 RDR_EvaluateNodeByName( IN cm_user_t *userp,
931 IN AFSFileID ParentID,
932 IN WCHAR *FileNameCounted,
933 IN DWORD FileNameLength,
934 IN BOOL CaseSensitive,
938 IN DWORD ResultBufferLength,
939 IN OUT AFSCommResult **ResultCB)
941 AFSDirEnumEntry * pCurrentEntry;
942 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
944 cm_scache_t * scp = NULL;
945 cm_scache_t * dscp = NULL;
950 WCHAR * wszName = NULL;
953 wchar_t FileName[260];
955 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
959 req.flags |= CM_REQ_WOW64;
961 osi_Log4(afsd_logp, "RDR_EvaluateNodeByName parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
962 ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
964 /* Allocate enough room to add a volume prefix if necessary */
965 cbName = FileNameLength + (CM_PREFIX_VOL_CCH + 1) * sizeof(WCHAR);
966 wszName = malloc(cbName);
968 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName Out of Memory");
971 StringCbCopyNW(wszName, cbName, FileName, FileNameLength);
972 osi_Log1(afsd_logp, "... name=%S", osi_LogSaveStringW(afsd_logp, wszName));
974 *ResultCB = (AFSCommResult *)malloc(size);
976 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName Out of Memory");
981 memset(*ResultCB, 0, size);
982 (*ResultCB)->ResultBufferLength = ResultBufferLength;
983 if (ResultBufferLength)
984 pCurrentEntry = (AFSDirEnumEntry *)&(*ResultCB)->ResultData;
986 if (ParentID.Cell != 0) {
987 parentFid.cell = ParentID.Cell;
988 parentFid.volume = ParentID.Volume;
989 parentFid.vnode = ParentID.Vnode;
990 parentFid.unique = ParentID.Unique;
991 parentFid.hash = ParentID.Hash;
993 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
995 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
996 (*ResultCB)->ResultStatus = status;
997 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName cm_GetSCache parentFID failure code=0x%x status=0x%x",
1003 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
1004 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName Object Name Invalid - Cell = 0");
1008 /* get the directory size */
1009 lock_ObtainWrite(&dscp->rw);
1010 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1011 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1013 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1014 (*ResultCB)->ResultStatus = status;
1015 lock_ReleaseWrite(&dscp->rw);
1016 cm_ReleaseSCache(dscp);
1017 osi_Log3(afsd_logp, "RDR_EvaluateNodeByName cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1018 dscp, code, status);
1022 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1023 lock_ReleaseWrite(&dscp->rw);
1025 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1026 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1027 cm_ReleaseSCache(dscp);
1028 osi_Log1(afsd_logp, "RDR_EvaluateNodeByName Not a Directory dscp=0x%p",
1034 code = cm_Lookup(dscp, wszName, CM_FLAG_CHECKPATH, userp, &req, &scp);
1036 if ((code == CM_ERROR_NOSUCHPATH || code == CM_ERROR_NOSUCHFILE || code == CM_ERROR_BPLUS_NOMATCH) &&
1037 (wcschr(wszName, '%') != NULL || wcschr(wszName, '#') != NULL)) {
1039 * A volume reference: <cell>{%,#}<volume> -> @vol:<cell>{%,#}<volume>
1041 StringCchCopyNW(wszName, cbName, _C(CM_PREFIX_VOL), CM_PREFIX_VOL_CCH);
1042 StringCbCatNW(wszName, cbName, FileName, FileNameLength);
1043 cm_strlwr_utf16(wszName);
1046 code = cm_EvaluateVolumeReference(wszName, CM_FLAG_CHECKPATH, userp, &req, &scp);
1049 if (code == 0 && scp) {
1050 wchar_t shortName[13]=L"";
1053 cm_Gen8Dot3VolNameW(scp->fid.cell, scp->fid.volume, shortName, NULL);
1054 } else if (!cm_Is8Dot3(wszName)) {
1057 dfid.vnode = htonl(scp->fid.vnode);
1058 dfid.unique = htonl(scp->fid.unique);
1060 cm_Gen8Dot3NameIntW(FileName, &dfid, shortName, NULL);
1062 shortName[0] = '\0';
1065 code = RDR_PopulateCurrentEntry(pCurrentEntry, ResultBufferLength,
1066 dscp, scp, userp, &req,
1067 FileName, shortName,
1068 (bWow64 ? RDR_POP_WOW64 : 0) |
1069 (bNoFollow ? 0 : (RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS)),
1070 NULL, &dwRemaining);
1072 RDR_FlagScpInUse( scp, FALSE );
1073 cm_ReleaseSCache(scp);
1076 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1077 (*ResultCB)->ResultStatus = status;
1078 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName FAILURE code=0x%x status=0x%x",
1081 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
1082 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1083 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName SUCCESS");
1086 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1087 (*ResultCB)->ResultStatus = status;
1088 osi_Log2(afsd_logp, "RDR_EvaluateNodeByName FAILURE code=0x%x status=0x%x",
1091 (*ResultCB)->ResultStatus = STATUS_NO_SUCH_FILE;
1092 osi_Log0(afsd_logp, "RDR_EvaluateNodeByName No Such File");
1094 cm_ReleaseSCache(dscp);
1101 RDR_EvaluateNodeByID( IN cm_user_t *userp,
1102 IN AFSFileID ParentID, /* not used */
1103 IN AFSFileID SourceID,
1107 IN DWORD ResultBufferLength,
1108 IN OUT AFSCommResult **ResultCB)
1110 AFSDirEnumEntry * pCurrentEntry;
1111 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1112 afs_uint32 code = 0;
1113 cm_scache_t * scp = NULL;
1114 cm_scache_t * dscp = NULL;
1121 osi_Log4(afsd_logp, "RDR_EvaluateNodeByID source FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1122 SourceID.Cell, SourceID.Volume, SourceID.Vnode, SourceID.Unique);
1123 osi_Log4(afsd_logp, "... parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1124 ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
1126 *ResultCB = (AFSCommResult *)malloc(size);
1128 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Out of Memory");
1132 memset(*ResultCB, 0, size);
1133 (*ResultCB)->ResultBufferLength = ResultBufferLength;
1134 dwRemaining = ResultBufferLength;
1135 if (ResultBufferLength)
1136 pCurrentEntry = (AFSDirEnumEntry *)&(*ResultCB)->ResultData;
1140 req.flags |= CM_REQ_WOW64;
1142 if (SourceID.Cell != 0) {
1143 Fid.cell = SourceID.Cell;
1144 Fid.volume = SourceID.Volume;
1145 Fid.vnode = SourceID.Vnode;
1146 Fid.unique = SourceID.Unique;
1147 Fid.hash = SourceID.Hash;
1149 code = cm_GetSCache(&Fid, &scp, userp, &req);
1151 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1152 (*ResultCB)->ResultStatus = status;
1153 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache SourceFID failure code=0x%x status=0x%x",
1158 (*ResultCB)->ResultStatus = STATUS_OBJECT_NAME_INVALID;
1159 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Object Name Invalid - Cell = 0");
1163 if (ParentID.Cell != 0) {
1164 cm_SetFid(&parentFid, ParentID.Cell, ParentID.Volume, ParentID.Vnode, ParentID.Unique);
1165 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
1167 cm_ReleaseSCache(scp);
1168 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1169 (*ResultCB)->ResultStatus = status;
1170 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache parentFID failure code=0x%x status=0x%x",
1174 } else if (SourceID.Vnode == 1) {
1176 cm_HoldSCache(dscp);
1177 } else if (scp->parentVnode) {
1178 cm_SetFid(&parentFid, SourceID.Cell, SourceID.Volume, scp->parentVnode, scp->parentUnique);
1179 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
1181 cm_ReleaseSCache(scp);
1182 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1183 (*ResultCB)->ResultStatus = status;
1184 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID cm_GetSCache parentFID failure code=0x%x status=0x%x",
1189 (*ResultCB)->ResultStatus = STATUS_OBJECT_PATH_INVALID;
1190 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID Object Path Invalid - Unknown Parent");
1194 /* Make sure the directory is current */
1195 lock_ObtainWrite(&dscp->rw);
1196 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1197 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1199 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1200 (*ResultCB)->ResultStatus = status;
1201 lock_ReleaseWrite(&dscp->rw);
1202 cm_ReleaseSCache(dscp);
1203 cm_ReleaseSCache(scp);
1204 osi_Log3(afsd_logp, "RDR_EvaluateNodeByID cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1205 dscp, code, status);
1209 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1210 lock_ReleaseWrite(&dscp->rw);
1212 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1213 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1214 cm_ReleaseSCache(dscp);
1215 cm_ReleaseSCache(scp);
1216 osi_Log1(afsd_logp, "RDR_EvaluateNodeByID Not a Directory dscp=0x%p", dscp);
1220 code = RDR_PopulateCurrentEntry(pCurrentEntry, ResultBufferLength,
1221 dscp, scp, userp, &req, NULL, NULL,
1222 (bWow64 ? RDR_POP_WOW64 : 0) |
1223 (bNoFollow ? 0 : (RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS)),
1224 NULL, &dwRemaining);
1227 RDR_FlagScpInUse( scp, FALSE );
1228 cm_ReleaseSCache(scp);
1229 cm_ReleaseSCache(dscp);
1232 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1233 (*ResultCB)->ResultStatus = status;
1234 osi_Log2(afsd_logp, "RDR_EvaluateNodeByID FAILURE code=0x%x status=0x%x",
1237 (*ResultCB)->ResultStatus = STATUS_SUCCESS;
1238 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1239 osi_Log0(afsd_logp, "RDR_EvaluateNodeByID SUCCESS");
1245 RDR_CreateFileEntry( IN cm_user_t *userp,
1246 IN WCHAR *FileNameCounted,
1247 IN DWORD FileNameLength,
1248 IN AFSFileCreateCB *CreateCB,
1251 IN DWORD ResultBufferLength,
1252 IN OUT AFSCommResult **ResultCB)
1254 AFSFileCreateResultCB *pResultCB = NULL;
1255 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1258 cm_scache_t * dscp = NULL;
1259 afs_uint32 flags = 0;
1261 cm_scache_t * scp = NULL;
1264 wchar_t FileName[260];
1266 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
1268 osi_Log4(afsd_logp, "RDR_CreateFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1269 CreateCB->ParentId.Cell, CreateCB->ParentId.Volume,
1270 CreateCB->ParentId.Vnode, CreateCB->ParentId.Unique);
1271 osi_Log1(afsd_logp, "... name=%S", osi_LogSaveStringW(afsd_logp, FileName));
1275 req.flags |= CM_REQ_WOW64;
1276 memset(&setAttr, 0, sizeof(cm_attr_t));
1278 *ResultCB = (AFSCommResult *)malloc(size);
1280 osi_Log0(afsd_logp, "RDR_CreateFileEntry out of memory");
1288 parentFid.cell = CreateCB->ParentId.Cell;
1289 parentFid.volume = CreateCB->ParentId.Volume;
1290 parentFid.vnode = CreateCB->ParentId.Vnode;
1291 parentFid.unique = CreateCB->ParentId.Unique;
1292 parentFid.hash = CreateCB->ParentId.Hash;
1294 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
1296 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1297 (*ResultCB)->ResultStatus = status;
1298 osi_Log2(afsd_logp, "RDR_CreateFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1303 lock_ObtainWrite(&dscp->rw);
1304 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1305 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1307 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1308 (*ResultCB)->ResultStatus = status;
1309 lock_ReleaseWrite(&dscp->rw);
1310 cm_ReleaseSCache(dscp);
1311 osi_Log3(afsd_logp, "RDR_CreateFileEntry cm_SyncOp failure (1) dscp=0x%p code=0x%x status=0x%x",
1312 dscp, code, status);
1316 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1317 lock_ReleaseWrite(&dscp->rw);
1319 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1320 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1321 cm_ReleaseSCache(dscp);
1322 osi_Log1(afsd_logp, "RDR_CreateFileEntry Not a Directory dscp=0x%p",
1327 /* Use current time */
1328 setAttr.mask = CM_ATTRMASK_CLIENTMODTIME;
1329 setAttr.clientModTime = time(NULL);
1331 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1332 if (smb_unixModeDefaultDir) {
1333 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1334 setAttr.unixModeBits = smb_unixModeDefaultDir;
1335 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)
1336 setAttr.unixModeBits &= ~0222; /* disable the write bits */
1339 code = cm_MakeDir(dscp, FileName, flags, &setAttr, userp, &req, &scp);
1341 if (smb_unixModeDefaultFile) {
1342 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1343 setAttr.unixModeBits = smb_unixModeDefaultFile;
1344 if (CreateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)
1345 setAttr.unixModeBits &= ~0222; /* disable the write bits */
1348 setAttr.mask |= CM_ATTRMASK_LENGTH;
1349 setAttr.length.LowPart = CreateCB->AllocationSize.LowPart;
1350 setAttr.length.HighPart = CreateCB->AllocationSize.HighPart;
1351 code = cm_Create(dscp, FileName, flags, &setAttr, &scp, userp, &req);
1354 wchar_t shortName[13]=L"";
1358 (*ResultCB)->ResultStatus = 0; // We will be able to fit all the data in here
1360 (*ResultCB)->ResultBufferLength = sizeof( AFSFileCreateResultCB);
1362 pResultCB = (AFSFileCreateResultCB *)(*ResultCB)->ResultData;
1364 dwRemaining = ResultBufferLength - sizeof( AFSFileCreateResultCB) + sizeof( AFSDirEnumEntry);
1366 lock_ObtainWrite(&dscp->rw);
1367 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1368 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1370 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1371 (*ResultCB)->ResultStatus = status;
1372 lock_ReleaseWrite(&dscp->rw);
1373 cm_ReleaseSCache(dscp);
1374 cm_ReleaseSCache(scp);
1375 osi_Log3(afsd_logp, "RDR_CreateFileEntry cm_SyncOp failure (2) dscp=0x%p code=0x%x status=0x%x",
1376 dscp, code, status);
1380 pResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
1382 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1383 lock_ReleaseWrite(&dscp->rw);
1385 dfid.vnode = htonl(scp->fid.vnode);
1386 dfid.unique = htonl(scp->fid.unique);
1388 if (!cm_Is8Dot3(FileName))
1389 cm_Gen8Dot3NameIntW(FileName, &dfid, shortName, NULL);
1391 shortName[0] = '\0';
1393 code = RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
1394 dscp, scp, userp, &req, FileName, shortName,
1395 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
1396 NULL, &dwRemaining);
1399 RDR_FlagScpInUse( scp, FALSE );
1400 cm_ReleaseSCache(scp);
1401 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1402 osi_Log0(afsd_logp, "RDR_CreateFileEntry SUCCESS");
1404 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1405 (*ResultCB)->ResultStatus = status;
1406 (*ResultCB)->ResultBufferLength = 0;
1407 osi_Log2(afsd_logp, "RDR_CreateFileEntry FAILURE code=0x%x status=0x%x",
1411 cm_ReleaseSCache(dscp);
1417 RDR_UpdateFileEntry( IN cm_user_t *userp,
1418 IN AFSFileID FileId,
1419 IN AFSFileUpdateCB *UpdateCB,
1421 IN DWORD ResultBufferLength,
1422 IN OUT AFSCommResult **ResultCB)
1424 AFSFileUpdateResultCB *pResultCB = NULL;
1425 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1429 afs_uint32 flags = 0;
1431 cm_scache_t * scp = NULL;
1432 cm_scache_t * dscp = NULL;
1434 time_t clientModTime;
1437 BOOL bScpLocked = FALSE;
1441 req.flags |= CM_REQ_WOW64;
1442 memset(&setAttr, 0, sizeof(cm_attr_t));
1444 osi_Log4(afsd_logp, "RDR_UpdateFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1445 UpdateCB->ParentId.Cell, UpdateCB->ParentId.Volume,
1446 UpdateCB->ParentId.Vnode, UpdateCB->ParentId.Unique);
1447 osi_Log4(afsd_logp, "... object FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1448 FileId.Cell, FileId.Volume,
1449 FileId.Vnode, FileId.Unique);
1451 *ResultCB = (AFSCommResult *)malloc( size);
1453 osi_Log0(afsd_logp, "RDR_UpdateFileEntry Out of Memory");
1461 parentFid.cell = UpdateCB->ParentId.Cell;
1462 parentFid.volume = UpdateCB->ParentId.Volume;
1463 parentFid.vnode = UpdateCB->ParentId.Vnode;
1464 parentFid.unique = UpdateCB->ParentId.Unique;
1465 parentFid.hash = UpdateCB->ParentId.Hash;
1467 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
1469 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1470 (*ResultCB)->ResultStatus = status;
1471 osi_Log2(afsd_logp, "RDR_UpdateFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1476 lock_ObtainWrite(&dscp->rw);
1478 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1479 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1481 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1482 (*ResultCB)->ResultStatus = status;
1483 lock_ReleaseWrite(&dscp->rw);
1484 cm_ReleaseSCache(dscp);
1485 osi_Log3(afsd_logp, "RDR_UpdateFileEntry cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
1486 dscp, code, status);
1490 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1491 lock_ReleaseWrite(&dscp->rw);
1494 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1495 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1496 cm_ReleaseSCache(dscp);
1497 osi_Log1(afsd_logp, "RDR_UpdateFileEntry Not a Directory dscp=0x%p",
1502 Fid.cell = FileId.Cell;
1503 Fid.volume = FileId.Volume;
1504 Fid.vnode = FileId.Vnode;
1505 Fid.unique = FileId.Unique;
1506 Fid.hash = FileId.Hash;
1508 code = cm_GetSCache(&Fid, &scp, userp, &req);
1510 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1511 (*ResultCB)->ResultStatus = status;
1512 cm_ReleaseSCache(dscp);
1513 osi_Log2(afsd_logp, "RDR_UpdateFileEntry cm_GetSCache object FID failure code=0x%x status=0x%x",
1518 lock_ObtainWrite(&scp->rw);
1520 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1521 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1523 lock_ReleaseWrite(&scp->rw);
1524 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1525 (*ResultCB)->ResultStatus = status;
1526 (*ResultCB)->ResultBufferLength = 0;
1527 cm_ReleaseSCache(dscp);
1528 cm_ReleaseSCache(scp);
1529 osi_Log3(afsd_logp, "RDR_UpdateFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
1533 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1535 if (UpdateCB->ChangeTime.QuadPart) {
1537 if (scp->fileType == CM_SCACHETYPE_FILE) {
1538 /* Do not set length and other attributes at the same time */
1539 if (scp->length.QuadPart != UpdateCB->AllocationSize.QuadPart) {
1540 osi_Log2(afsd_logp, "RDR_UpdateFileEntry Length Change 0x%x -> 0x%x",
1541 (afs_uint32)scp->length.QuadPart, (afs_uint32)UpdateCB->AllocationSize.QuadPart);
1542 setAttr.mask |= CM_ATTRMASK_LENGTH;
1543 setAttr.length.LowPart = UpdateCB->AllocationSize.LowPart;
1544 setAttr.length.HighPart = UpdateCB->AllocationSize.HighPart;
1545 lock_ReleaseWrite(&scp->rw);
1547 code = cm_SetAttr(scp, &setAttr, userp, &req);
1555 lock_ObtainWrite(&scp->rw);
1558 if ((scp->unixModeBits & 0200) && (UpdateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1559 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1560 setAttr.unixModeBits = scp->unixModeBits & ~0222;
1561 } else if (!(scp->unixModeBits & 0200) && !(UpdateCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1562 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1563 setAttr.unixModeBits = scp->unixModeBits | 0222;
1567 if (UpdateCB->LastWriteTime.QuadPart) {
1568 ft.dwLowDateTime = UpdateCB->LastWriteTime.LowPart;
1569 ft.dwHighDateTime = UpdateCB->LastWriteTime.HighPart;
1571 cm_UnixTimeFromLargeSearchTime(& clientModTime, &ft);
1574 lock_ObtainWrite(&scp->rw);
1577 if (scp->clientModTime != clientModTime) {
1578 setAttr.mask |= CM_ATTRMASK_CLIENTMODTIME;
1579 setAttr.clientModTime = clientModTime;
1584 lock_ReleaseWrite(&scp->rw);
1586 code = cm_SetAttr(scp, &setAttr, userp, &req);
1593 lock_ReleaseWrite(&scp->rw);
1597 DWORD dwRemaining = ResultBufferLength - sizeof( AFSFileUpdateResultCB) + sizeof( AFSDirEnumEntry);
1599 pResultCB = (AFSFileUpdateResultCB *)(*ResultCB)->ResultData;
1601 code = RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
1602 dscp, scp, userp, &req, NULL, NULL,
1603 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
1604 NULL, &dwRemaining);
1605 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
1606 osi_Log0(afsd_logp, "RDR_UpdateFileEntry SUCCESS");
1608 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1609 (*ResultCB)->ResultStatus = status;
1610 (*ResultCB)->ResultBufferLength = 0;
1611 osi_Log2(afsd_logp, "RDR_UpdateFileEntry FAILURE code=0x%x status=0x%x",
1614 cm_ReleaseSCache(scp);
1615 cm_ReleaseSCache(dscp);
1621 RDR_CleanupFileEntry( IN cm_user_t *userp,
1622 IN AFSFileID FileId,
1623 IN WCHAR *FileNameCounted,
1624 IN DWORD FileNameLength,
1625 IN AFSFileCleanupCB *CleanupCB,
1627 IN BOOL bLastHandle,
1628 IN BOOL bDeleteFile,
1629 IN BOOL bUnlockFile,
1630 IN DWORD ResultBufferLength,
1631 IN OUT AFSCommResult **ResultCB)
1633 size_t size = sizeof(AFSCommResult);
1636 afs_uint32 code = 0;
1637 afs_uint32 flags = 0;
1639 cm_scache_t * scp = NULL;
1640 cm_scache_t * dscp = NULL;
1642 time_t clientModTime;
1645 BOOL bScpLocked = FALSE;
1646 BOOL bDscpLocked = FALSE;
1647 BOOL bFlushFile = FALSE;
1652 req.flags |= CM_REQ_WOW64;
1653 memset(&setAttr, 0, sizeof(cm_attr_t));
1655 osi_Log4(afsd_logp, "RDR_CleanupFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1656 CleanupCB->ParentId.Cell, CleanupCB->ParentId.Volume,
1657 CleanupCB->ParentId.Vnode, CleanupCB->ParentId.Unique);
1658 osi_Log4(afsd_logp, "... object FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
1659 FileId.Cell, FileId.Volume,
1660 FileId.Vnode, FileId.Unique);
1662 *ResultCB = (AFSCommResult *)malloc( size);
1664 osi_Log0(afsd_logp, "RDR_CleanupFileEntry Out of Memory");
1672 parentFid.cell = CleanupCB->ParentId.Cell;
1673 parentFid.volume = CleanupCB->ParentId.Volume;
1674 parentFid.vnode = CleanupCB->ParentId.Vnode;
1675 parentFid.unique = CleanupCB->ParentId.Unique;
1676 parentFid.hash = CleanupCB->ParentId.Hash;
1678 if (parentFid.cell) {
1679 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
1681 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1682 (*ResultCB)->ResultStatus = status;
1683 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
1688 lock_ObtainWrite(&dscp->rw);
1690 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
1691 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1693 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp failure dscp=0x%p code=0x%x",
1699 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1700 lock_ReleaseWrite(&dscp->rw);
1701 bDscpLocked = FALSE;
1703 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
1704 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
1705 cm_ReleaseSCache(dscp);
1706 osi_Log1(afsd_logp, "RDR_CleanupFileEntry Not a Directory dscp=0x%p",
1713 Fid.cell = FileId.Cell;
1714 Fid.volume = FileId.Volume;
1715 Fid.vnode = FileId.Vnode;
1716 Fid.unique = FileId.Unique;
1717 Fid.hash = FileId.Hash;
1719 code = cm_GetSCache(&Fid, &scp, userp, &req);
1721 osi_Log1(afsd_logp, "RDR_CleanupFileEntry cm_GetSCache object FID failure code=0x%x",
1726 lock_ObtainWrite(&scp->rw);
1728 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1729 CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_NEEDCALLBACK);
1731 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp failure scp=0x%p code=0x%x",
1735 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1737 if ((bLastHandle || bFlushFile) &&
1738 scp->redirBufCount > 0)
1740 LARGE_INTEGER heldExtents;
1741 AFSFileExtentCB extentList[1024];
1742 DWORD extentCount = 0;
1747 heldExtents.QuadPart = 0;
1749 for ( srbp = redirq_to_cm_buf_t(scp->redirQueueT);
1751 srbp = redirq_to_cm_buf_t(osi_QPrev(&srbp->redirq)))
1753 extentList[extentCount].Flags = 0;
1754 extentList[extentCount].Length = cm_data.blockSize;
1755 extentList[extentCount].FileOffset.QuadPart = srbp->offset.QuadPart;
1756 extentList[extentCount].CacheOffset.QuadPart = srbp->datap - RDR_extentBaseAddress;
1757 lock_ObtainWrite(&buf_globalLock);
1758 srbp->redirReleaseRequested = now;
1759 lock_ReleaseWrite(&buf_globalLock);
1762 if (extentCount == 1024) {
1763 lock_ReleaseWrite(&scp->rw);
1764 code = RDR_RequestExtentRelease(&scp->fid, heldExtents, extentCount, extentList);
1766 if (code == CM_ERROR_RETRY) {
1768 * The redirector either is not holding the extents or cannot let them
1769 * go because they are otherwise in use. At the moment, do nothing.
1776 lock_ObtainWrite(&scp->rw);
1780 if (code == 0 && extentCount > 0) {
1782 lock_ReleaseWrite(&scp->rw);
1785 code = RDR_RequestExtentRelease(&scp->fid, heldExtents, extentCount, extentList);
1790 /* No longer in use by redirector */
1792 lock_ObtainWrite(&scp->rw);
1797 lock_AssertWrite(&scp->rw);
1798 scp->flags &= ~CM_SCACHEFLAG_RDR_IN_USE;
1801 if (bLastHandle || bFlushFile) {
1803 lock_ObtainWrite(&scp->rw);
1806 code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_WRITE,
1807 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1810 lock_ReleaseWrite(&scp->rw);
1814 code = cm_FSync(scp, userp, &req, bScpLocked);
1816 if (bLastHandle && code)
1820 if (bUnlockFile || bDeleteFile) {
1822 lock_ObtainWrite(&scp->rw);
1825 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1826 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
1828 osi_Log2(afsd_logp, "RDR_CleanupFileEntry cm_SyncOp (2) failure scp=0x%p code=0x%x",
1833 key = cm_GenerateKey(CM_SESSION_IFS, CleanupCB->ProcessId, 0);
1835 /* the scp is now locked and current */
1836 code = cm_UnlockByKey(scp, key,
1837 bDeleteFile ? CM_UNLOCK_FLAG_BY_FID : 0,
1840 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
1846 if (CleanupCB->ChangeTime.QuadPart) {
1848 if (scp->fileType == CM_SCACHETYPE_FILE) {
1849 /* Do not set length and other attributes at the same time */
1850 if (scp->length.QuadPart != CleanupCB->AllocationSize.QuadPart) {
1851 osi_Log2(afsd_logp, "RDR_CleanupFileEntry Length Change 0x%x -> 0x%x",
1852 (afs_uint32)scp->length.QuadPart, (afs_uint32)CleanupCB->AllocationSize.QuadPart);
1853 setAttr.mask |= CM_ATTRMASK_LENGTH;
1854 setAttr.length.LowPart = CleanupCB->AllocationSize.LowPart;
1855 setAttr.length.HighPart = CleanupCB->AllocationSize.HighPart;
1858 lock_ReleaseWrite(&scp->rw);
1861 code = cm_SetAttr(scp, &setAttr, userp, &req);
1869 lock_ObtainWrite(&scp->rw);
1873 if ((scp->unixModeBits & 0200) && (CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1874 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1875 setAttr.unixModeBits = scp->unixModeBits & ~0222;
1876 } else if (!(scp->unixModeBits & 0200) && !(CleanupCB->FileAttributes & FILE_ATTRIBUTE_READONLY)) {
1877 setAttr.mask |= CM_ATTRMASK_UNIXMODEBITS;
1878 setAttr.unixModeBits = scp->unixModeBits | 0222;
1882 if (CleanupCB->LastWriteTime.QuadPart) {
1883 ft.dwLowDateTime = CleanupCB->LastWriteTime.LowPart;
1884 ft.dwHighDateTime = CleanupCB->LastWriteTime.HighPart;
1886 cm_UnixTimeFromLargeSearchTime(&clientModTime, &ft);
1887 if (scp->clientModTime != clientModTime) {
1888 setAttr.mask |= CM_ATTRMASK_CLIENTMODTIME;
1889 setAttr.clientModTime = clientModTime;
1895 lock_ReleaseWrite(&scp->rw);
1897 code = cm_SetAttr(scp, &setAttr, userp, &req);
1901 /* Now drop the lock enforcing the share access */
1902 if ( CleanupCB->FileAccess != AFS_FILE_ACCESS_NOLOCK) {
1903 unsigned int sLockType;
1904 LARGE_INTEGER LOffset, LLength;
1906 if (CleanupCB->FileAccess == AFS_FILE_ACCESS_SHARED)
1907 sLockType = LOCKING_ANDX_SHARED_LOCK;
1911 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, CleanupCB->Identifier);
1913 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
1914 LOffset.LowPart = SMB_FID_QLOCK_LOW;
1915 LLength.HighPart = 0;
1916 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
1919 lock_ObtainWrite(&scp->rw);
1923 code = cm_SyncOp(scp, NULL, userp, &req, 0, CM_SCACHESYNC_LOCK);
1926 code = cm_Unlock(scp, sLockType, LOffset, LLength, key, 0, userp, &req);
1928 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
1930 if (code == CM_ERROR_RANGE_NOT_LOCKED)
1932 osi_Log3(afsd_logp, "RDR_CleanupFileEntry Range Not Locked -- FileAccess 0x%x ProcessId 0x%x HandleId 0x%x",
1933 CleanupCB->FileAccess, CleanupCB->ProcessId, CleanupCB->Identifier);
1941 lock_ReleaseWrite(&dscp->rw);
1943 lock_ReleaseWrite(&scp->rw);
1945 if (dscp && bDeleteFile) {
1946 WCHAR FileName[260];
1948 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
1950 if (scp->fileType == CM_SCACHETYPE_DIRECTORY)
1951 code = cm_RemoveDir(dscp, NULL, FileName, userp, &req);
1953 code = cm_Unlink(dscp, NULL, FileName, userp, &req);
1957 (*ResultCB)->ResultStatus = 0;
1958 (*ResultCB)->ResultBufferLength = 0;
1959 osi_Log0(afsd_logp, "RDR_CleanupFileEntry SUCCESS");
1961 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
1962 (*ResultCB)->ResultStatus = status;
1963 (*ResultCB)->ResultBufferLength = 0;
1964 osi_Log2(afsd_logp, "RDR_CleanupFileEntry FAILURE code=0x%x status=0x%x",
1968 cm_ReleaseSCache(scp);
1970 cm_ReleaseSCache(dscp);
1976 RDR_DeleteFileEntry( IN cm_user_t *userp,
1977 IN AFSFileID ParentId,
1978 IN ULONGLONG ProcessId,
1979 IN WCHAR *FileNameCounted,
1980 IN DWORD FileNameLength,
1983 IN DWORD ResultBufferLength,
1984 IN OUT AFSCommResult **ResultCB)
1987 AFSFileDeleteResultCB *pResultCB = NULL;
1988 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
1991 cm_scache_t * dscp = NULL;
1992 cm_scache_t * scp = NULL;
1993 afs_uint32 flags = 0;
1997 wchar_t FileName[260];
2000 StringCchCopyNW(FileName, 260, FileNameCounted, FileNameLength / sizeof(WCHAR));
2002 osi_Log4(afsd_logp, "RDR_DeleteFileEntry parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2003 ParentId.Cell, ParentId.Volume,
2004 ParentId.Vnode, ParentId.Unique);
2005 osi_Log2(afsd_logp, "... name=%S checkOnly=%x",
2006 osi_LogSaveStringW(afsd_logp, FileName),
2011 req.flags |= CM_REQ_WOW64;
2012 memset(&setAttr, 0, sizeof(cm_attr_t));
2014 *ResultCB = (AFSCommResult *)malloc( size);
2016 osi_Log0(afsd_logp, "RDR_DeleteFileEntry out of memory");
2024 parentFid.cell = ParentId.Cell;
2025 parentFid.volume = ParentId.Volume;
2026 parentFid.vnode = ParentId.Vnode;
2027 parentFid.unique = ParentId.Unique;
2028 parentFid.hash = ParentId.Hash;
2030 code = cm_GetSCache(&parentFid, &dscp, userp, &req);
2032 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2033 (*ResultCB)->ResultStatus = status;
2034 osi_Log2(afsd_logp, "RDR_DeleteFileEntry cm_GetSCache ParentFID failure code=0x%x status=0x%x",
2039 lock_ObtainWrite(&dscp->rw);
2041 code = cm_SyncOp(dscp, NULL, userp, &req, 0,
2042 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2044 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2045 (*ResultCB)->ResultStatus = status;
2046 (*ResultCB)->ResultBufferLength = 0;
2047 lock_ReleaseWrite(&dscp->rw);
2048 cm_ReleaseSCache(dscp);
2049 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp failure dscp=0x%p code=0x%x status=0x%x",
2050 dscp, code, status);
2054 cm_SyncOpDone(dscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2055 lock_ReleaseWrite(&dscp->rw);
2057 if (dscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2058 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2059 cm_ReleaseSCache(dscp);
2060 osi_Log1(afsd_logp, "RDR_DeleteFileEntry Not a Directory dscp=0x%p",
2065 code = cm_Lookup(dscp, FileName, 0, userp, &req, &scp);
2067 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2068 (*ResultCB)->ResultStatus = status;
2069 (*ResultCB)->ResultBufferLength = 0;
2070 cm_ReleaseSCache(dscp);
2071 osi_Log2(afsd_logp, "RDR_DeleteFileEntry cm_Lookup failure code=0x%x status=0x%x",
2076 lock_ObtainWrite(&scp->rw);
2077 code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_DELETE,
2078 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2080 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2081 (*ResultCB)->ResultStatus = status;
2082 (*ResultCB)->ResultBufferLength = 0;
2083 lock_ReleaseWrite(&scp->rw);
2084 cm_ReleaseSCache(scp);
2085 cm_ReleaseSCache(dscp);
2086 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2092 /* Drop all locks since the file is being deleted */
2093 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2094 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
2096 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2097 (*ResultCB)->ResultStatus = status;
2098 (*ResultCB)->ResultBufferLength = 0;
2099 lock_ReleaseWrite(&scp->rw);
2100 cm_ReleaseSCache(scp);
2101 cm_ReleaseSCache(dscp);
2102 osi_Log3(afsd_logp, "RDR_DeleteFileEntry cm_SyncOp Lock failure scp=0x%p code=0x%x status=0x%x",
2106 /* the scp is now locked and current */
2107 key = cm_GenerateKey(CM_SESSION_IFS, ProcessId, 0);
2109 code = cm_UnlockByKey(scp, key,
2110 CM_UNLOCK_FLAG_BY_FID,
2113 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS | CM_SCACHESYNC_LOCK);
2114 lock_ReleaseWrite(&scp->rw);
2116 if (scp->fileType == CM_SCACHETYPE_DIRECTORY)
2117 code = cm_RemoveDir(dscp, NULL, FileName, userp, &req);
2119 code = cm_Unlink(dscp, NULL, FileName, userp, &req);
2121 lock_ReleaseWrite(&scp->rw);
2125 (*ResultCB)->ResultStatus = 0; // We will be able to fit all the data in here
2127 (*ResultCB)->ResultBufferLength = sizeof( AFSFileDeleteResultCB);
2129 pResultCB = (AFSFileDeleteResultCB *)(*ResultCB)->ResultData;
2131 pResultCB->ParentDataVersion.QuadPart = dscp->dataVersion;
2132 osi_Log0(afsd_logp, "RDR_DeleteFileEntry SUCCESS");
2134 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2135 (*ResultCB)->ResultStatus = status;
2136 (*ResultCB)->ResultBufferLength = 0;
2137 osi_Log2(afsd_logp, "RDR_DeleteFileEntry FAILURE code=0x%x status=0x%x",
2141 cm_ReleaseSCache(dscp);
2142 cm_ReleaseSCache(scp);
2148 RDR_RenameFileEntry( IN cm_user_t *userp,
2149 IN WCHAR *SourceFileNameCounted,
2150 IN DWORD SourceFileNameLength,
2151 IN AFSFileID SourceFileId,
2152 IN AFSFileRenameCB *pRenameCB,
2154 IN DWORD ResultBufferLength,
2155 IN OUT AFSCommResult **ResultCB)
2158 AFSFileRenameResultCB *pResultCB = NULL;
2159 size_t size = sizeof(AFSCommResult) + ResultBufferLength - 1;
2160 AFSFileID SourceParentId = pRenameCB->SourceParentId;
2161 AFSFileID TargetParentId = pRenameCB->TargetParentId;
2162 WCHAR * TargetFileNameCounted = pRenameCB->TargetName;
2163 DWORD TargetFileNameLength = pRenameCB->TargetNameLength;
2164 cm_fid_t SourceParentFid;
2165 cm_fid_t TargetParentFid;
2166 cm_scache_t * oldDscp;
2167 cm_scache_t * newDscp;
2168 wchar_t shortName[13];
2169 wchar_t SourceFileName[260];
2170 wchar_t TargetFileName[260];
2178 req.flags |= CM_REQ_WOW64;
2180 StringCchCopyNW(SourceFileName, 260, SourceFileNameCounted, SourceFileNameLength / sizeof(WCHAR));
2181 StringCchCopyNW(TargetFileName, 260, TargetFileNameCounted, TargetFileNameLength / sizeof(WCHAR));
2183 osi_Log4(afsd_logp, "RDR_RenameFileEntry Source Parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2184 SourceParentId.Cell, SourceParentId.Volume,
2185 SourceParentId.Vnode, SourceParentId.Unique);
2186 osi_Log2(afsd_logp, "... Source Name=%S Length %u", osi_LogSaveStringW(afsd_logp, SourceFileName), SourceFileNameLength);
2187 osi_Log4(afsd_logp, "... Target Parent FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2188 TargetParentId.Cell, TargetParentId.Volume,
2189 TargetParentId.Vnode, TargetParentId.Unique);
2190 osi_Log2(afsd_logp, "... Target Name=%S Length %u", osi_LogSaveStringW(afsd_logp, TargetFileName), TargetFileNameLength);
2192 *ResultCB = (AFSCommResult *)malloc( size);
2200 pResultCB = (AFSFileRenameResultCB *)(*ResultCB)->ResultData;
2202 if (SourceFileNameLength == 0 || TargetFileNameLength == 0)
2204 osi_Log2(afsd_logp, "RDR_RenameFileEntry Invalid Name Length: src %u target %u",
2205 SourceFileNameLength, TargetFileNameLength);
2206 (*ResultCB)->ResultStatus = STATUS_INVALID_PARAMETER;
2210 SourceParentFid.cell = SourceParentId.Cell;
2211 SourceParentFid.volume = SourceParentId.Volume;
2212 SourceParentFid.vnode = SourceParentId.Vnode;
2213 SourceParentFid.unique = SourceParentId.Unique;
2214 SourceParentFid.hash = SourceParentId.Hash;
2216 TargetParentFid.cell = TargetParentId.Cell;
2217 TargetParentFid.volume = TargetParentId.Volume;
2218 TargetParentFid.vnode = TargetParentId.Vnode;
2219 TargetParentFid.unique = TargetParentId.Unique;
2220 TargetParentFid.hash = TargetParentId.Hash;
2222 code = cm_GetSCache(&SourceParentFid, &oldDscp, userp, &req);
2224 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache source parent failed code 0x%x", code);
2225 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2226 (*ResultCB)->ResultStatus = status;
2230 lock_ObtainWrite(&oldDscp->rw);
2231 code = cm_SyncOp(oldDscp, NULL, userp, &req, 0,
2232 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2234 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp oldDscp 0x%p failed code 0x%x", oldDscp, code);
2235 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2236 (*ResultCB)->ResultStatus = status;
2237 lock_ReleaseWrite(&oldDscp->rw);
2238 cm_ReleaseSCache(oldDscp);
2242 cm_SyncOpDone(oldDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2243 lock_ReleaseWrite(&oldDscp->rw);
2246 if (oldDscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2247 osi_Log1(afsd_logp, "RDR_RenameFileEntry oldDscp 0x%p not a directory", oldDscp);
2248 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2249 cm_ReleaseSCache(oldDscp);
2253 code = cm_GetSCache(&TargetParentFid, &newDscp, userp, &req);
2255 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache target parent failed code 0x%x", code);
2256 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2257 (*ResultCB)->ResultStatus = status;
2258 cm_ReleaseSCache(oldDscp);
2262 lock_ObtainWrite(&newDscp->rw);
2263 code = cm_SyncOp(newDscp, NULL, userp, &req, 0,
2264 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2266 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp newDscp 0x%p failed code 0x%x", newDscp, code);
2267 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2268 (*ResultCB)->ResultStatus = status;
2269 lock_ReleaseWrite(&newDscp->rw);
2270 cm_ReleaseSCache(oldDscp);
2271 cm_ReleaseSCache(newDscp);
2275 cm_SyncOpDone(newDscp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2276 lock_ReleaseWrite(&newDscp->rw);
2279 if (newDscp->fileType != CM_SCACHETYPE_DIRECTORY) {
2280 osi_Log1(afsd_logp, "RDR_RenameFileEntry newDscp 0x%p not a directory", newDscp);
2281 (*ResultCB)->ResultStatus = STATUS_NOT_A_DIRECTORY;
2282 cm_ReleaseSCache(oldDscp);
2283 cm_ReleaseSCache(newDscp);
2287 code = cm_Rename( oldDscp, NULL, SourceFileName,
2288 newDscp, TargetFileName, userp, &req);
2292 cm_scache_t *scp = 0;
2295 (*ResultCB)->ResultBufferLength = ResultBufferLength;
2296 dwRemaining = ResultBufferLength - sizeof( AFSFileRenameResultCB) + sizeof( AFSDirEnumEntry);
2297 (*ResultCB)->ResultStatus = 0;
2299 pResultCB->SourceParentDataVersion.QuadPart = oldDscp->dataVersion;
2300 pResultCB->TargetParentDataVersion.QuadPart = newDscp->dataVersion;
2302 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_Rename oldDscp 0x%p newDscp 0x%p SUCCESS",
2305 code = cm_BeginDirOp( newDscp, userp, &req, CM_DIRLOCK_READ, CM_DIROP_FLAG_NONE, &dirop);
2307 code = cm_BPlusDirLookup(&dirop, TargetFileName, &targetFid);
2308 cm_EndDirOp(&dirop);
2312 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_BPlusDirLookup failed code 0x%x",
2314 (*ResultCB)->ResultStatus = STATUS_OBJECT_PATH_INVALID;
2315 cm_ReleaseSCache(oldDscp);
2316 cm_ReleaseSCache(newDscp);
2320 osi_Log4(afsd_logp, "RDR_RenameFileEntry Target FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2321 targetFid.cell, targetFid.volume,
2322 targetFid.vnode, targetFid.unique);
2324 code = cm_GetSCache(&targetFid, &scp, userp, &req);
2326 osi_Log1(afsd_logp, "RDR_RenameFileEntry cm_GetSCache target failed code 0x%x", code);
2327 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2328 (*ResultCB)->ResultStatus = status;
2329 cm_ReleaseSCache(oldDscp);
2330 cm_ReleaseSCache(newDscp);
2334 /* Make sure the source vnode is current */
2335 lock_ObtainWrite(&scp->rw);
2336 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2337 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2339 osi_Log2(afsd_logp, "RDR_RenameFileEntry cm_SyncOp scp 0x%p failed code 0x%x", scp, code);
2340 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2341 (*ResultCB)->ResultStatus = status;
2342 lock_ReleaseWrite(&scp->rw);
2343 cm_ReleaseSCache(oldDscp);
2344 cm_ReleaseSCache(newDscp);
2345 cm_ReleaseSCache(scp);
2349 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2350 lock_ReleaseWrite(&scp->rw);
2352 dfid.vnode = htonl(scp->fid.vnode);
2353 dfid.unique = htonl(scp->fid.unique);
2355 if (!cm_Is8Dot3(TargetFileName))
2356 cm_Gen8Dot3NameIntW(TargetFileName, &dfid, shortName, NULL);
2358 shortName[0] = '\0';
2360 RDR_PopulateCurrentEntry(&pResultCB->DirEnum, dwRemaining,
2361 newDscp, scp, userp, &req, TargetFileName, shortName,
2362 RDR_POP_FOLLOW_MOUNTPOINTS | RDR_POP_EVALUATE_SYMLINKS,
2363 NULL, &dwRemaining);
2364 (*ResultCB)->ResultBufferLength = ResultBufferLength - dwRemaining;
2365 cm_ReleaseSCache(scp);
2367 osi_Log0(afsd_logp, "RDR_RenameFileEntry SUCCESS");
2369 osi_Log3(afsd_logp, "RDR_RenameFileEntry cm_Rename oldDscp 0x%p newDscp 0x%p failed code 0x%x",
2370 oldDscp, newDscp, code);
2371 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2372 (*ResultCB)->ResultStatus = status;
2373 (*ResultCB)->ResultBufferLength = 0;
2376 cm_ReleaseSCache(oldDscp);
2377 cm_ReleaseSCache(newDscp);
2382 RDR_FlushFileEntry( IN cm_user_t *userp,
2383 IN AFSFileID FileId,
2385 IN DWORD ResultBufferLength,
2386 IN OUT AFSCommResult **ResultCB)
2388 cm_scache_t *scp = NULL;
2399 req.flags |= CM_REQ_WOW64;
2401 osi_Log4(afsd_logp, "RDR_FlushFileEntry File FID cell 0x%x vol 0x%x vno 0x%x uniq 0x%x",
2402 FileId.Cell, FileId.Volume,
2403 FileId.Vnode, FileId.Unique);
2405 snprintf( dbgstr, 1024,
2406 "RDR_FlushFileEntry File FID cell 0x%x vol 0x%x vno 0x%x uniq 0x%x\n",
2407 FileId.Cell, FileId.Volume,
2408 FileId.Vnode, FileId.Unique);
2409 OutputDebugStringA( dbgstr);
2412 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult));
2414 osi_Log0(afsd_logp, "RDR_FlushFileEntry out of memory");
2420 sizeof( AFSCommResult));
2422 /* Process the release */
2423 Fid.cell = FileId.Cell;
2424 Fid.volume = FileId.Volume;
2425 Fid.vnode = FileId.Vnode;
2426 Fid.unique = FileId.Unique;
2427 Fid.hash = FileId.Hash;
2429 code = cm_GetSCache(&Fid, &scp, userp, &req);
2431 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2432 (*ResultCB)->ResultStatus = status;
2433 osi_Log2(afsd_logp, "RDR_FlushFileEntry cm_GetSCache FID failure code=0x%x status=0x%x",
2438 lock_ObtainWrite(&scp->rw);
2439 if (scp->flags & CM_SCACHEFLAG_DELETED) {
2440 lock_ReleaseWrite(&scp->rw);
2441 (*ResultCB)->ResultStatus = STATUS_INVALID_HANDLE;
2445 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2446 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2448 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2449 (*ResultCB)->ResultStatus = status;
2450 lock_ReleaseWrite(&scp->rw);
2451 cm_ReleaseSCache(scp);
2452 osi_Log3(afsd_logp, "RDR_FlushFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2457 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2458 lock_ReleaseWrite(&scp->rw);
2460 code = cm_FSync(scp, userp, &req, FALSE);
2461 cm_ReleaseSCache(scp);
2464 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2465 (*ResultCB)->ResultStatus = status;
2466 osi_Log2(afsd_logp, "RDR_FlushFileEntry FAILURE code=0x%x status=0x%x",
2469 (*ResultCB)->ResultStatus = 0;
2470 osi_Log0(afsd_logp, "RDR_FlushFileEntry SUCCESS");
2472 (*ResultCB)->ResultBufferLength = 0;
2478 RDR_CheckAccess( IN cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
2482 ULONG afs_acc, afs_gr;
2484 afs_uint32 code = 0;
2486 file = (scp->fileType == CM_SCACHETYPE_FILE);
2489 /* access definitions from prs_fs.h */
2491 if (access & FILE_READ_DATA)
2492 afs_acc |= PRSFS_READ;
2493 if (access & FILE_READ_EA || access & FILE_READ_ATTRIBUTES)
2494 afs_acc |= PRSFS_READ;
2495 if (file && ((access & FILE_WRITE_DATA) || (access & FILE_APPEND_DATA)))
2496 afs_acc |= PRSFS_WRITE;
2497 if (access & FILE_WRITE_EA || access & FILE_WRITE_ATTRIBUTES)
2498 afs_acc |= PRSFS_WRITE;
2499 if (dir && ((access & FILE_ADD_FILE) || (access & FILE_ADD_SUBDIRECTORY)))
2500 afs_acc |= PRSFS_INSERT;
2501 if (dir && (access & FILE_LIST_DIRECTORY))
2502 afs_acc |= PRSFS_LOOKUP;
2503 if (file && (access & FILE_EXECUTE))
2504 afs_acc |= PRSFS_WRITE;
2505 if (dir && (access & FILE_TRAVERSE))
2506 afs_acc |= PRSFS_READ;
2507 if (dir && (access & FILE_DELETE_CHILD))
2508 afs_acc |= PRSFS_DELETE;
2509 if ((access & DELETE))
2510 afs_acc |= PRSFS_DELETE;
2512 /* check ACL with server */
2513 lock_ObtainWrite(&scp->rw);
2516 if (cm_HaveAccessRights(scp, userp, reqp, afs_acc, &afs_gr))
2522 /* we don't know the required access rights */
2523 code = cm_GetAccessRights(scp, userp, reqp);
2529 lock_ReleaseWrite(&(scp->rw));
2533 if (afs_gr & PRSFS_READ)
2534 *granted |= FILE_READ_DATA | FILE_READ_EA | FILE_READ_ATTRIBUTES | FILE_EXECUTE;
2535 if (afs_gr & PRSFS_WRITE)
2536 *granted |= FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES | FILE_EXECUTE;
2537 if (afs_gr & PRSFS_INSERT)
2538 *granted |= (dir ? FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY : 0) | (file ? FILE_ADD_SUBDIRECTORY : 0);
2539 if (afs_gr & PRSFS_LOOKUP)
2540 *granted |= (dir ? FILE_LIST_DIRECTORY : 0);
2541 if (afs_gr & PRSFS_DELETE)
2542 *granted |= FILE_DELETE_CHILD | DELETE;
2543 if (afs_gr & PRSFS_LOCK)
2545 if (afs_gr & PRSFS_ADMINISTER)
2548 *granted |= SYNCHRONIZE | READ_CONTROL;
2550 /* don't give more access than what was requested */
2552 osi_Log3(afsd_logp, "RDR_CheckAccess SUCCESS scp=0x%p requested=0x%x granted=0x%x", scp, access, *granted);
2554 osi_Log2(afsd_logp, "RDR_CheckAccess FAILURE scp=0x%p code=0x%x",
2561 RDR_OpenFileEntry( IN cm_user_t *userp,
2562 IN AFSFileID FileId,
2563 IN AFSFileOpenCB *OpenCB,
2566 IN DWORD ResultBufferLength,
2567 IN OUT AFSCommResult **ResultCB)
2569 AFSFileOpenResultCB *pResultCB = NULL;
2570 cm_scache_t *scp = NULL;
2571 cm_user_t *sysUserp = NULL;
2573 cm_lock_data_t *ldp = NULL;
2580 req.flags |= CM_REQ_WOW64;
2582 osi_Log4(afsd_logp, "RDR_OpenFileEntry File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2583 FileId.Cell, FileId.Volume,
2584 FileId.Vnode, FileId.Unique);
2586 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult) + sizeof( AFSFileOpenResultCB));
2588 osi_Log0(afsd_logp, "RDR_OpenFileEntry out of memory");
2594 sizeof( AFSCommResult) + sizeof( AFSFileOpenResultCB));
2596 pResultCB = (AFSFileOpenResultCB *)(*ResultCB)->ResultData;
2598 /* Process the release */
2599 Fid.cell = FileId.Cell;
2600 Fid.volume = FileId.Volume;
2601 Fid.vnode = FileId.Vnode;
2602 Fid.unique = FileId.Unique;
2603 Fid.hash = FileId.Hash;
2605 code = cm_GetSCache(&Fid, &scp, userp, &req);
2607 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2608 (*ResultCB)->ResultStatus = status;
2609 osi_Log2(afsd_logp, "RDR_OpenFileEntry cm_GetSCache FID failure code=0x%x status=0x%x",
2614 lock_ObtainWrite(&scp->rw);
2615 code = cm_SyncOp(scp, NULL, userp, &req, 0,
2616 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2618 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2619 (*ResultCB)->ResultStatus = status;
2620 lock_ReleaseWrite(&scp->rw);
2621 cm_ReleaseSCache(scp);
2622 osi_Log3(afsd_logp, "RDR_OpenFileEntry cm_SyncOp failure scp=0x%p code=0x%x status=0x%x",
2627 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2628 lock_ReleaseWrite(&scp->rw);
2630 sysUserp = RDR_GetLocalSystemUser();
2633 * Skip the open check if the request is coming from the local system account.
2634 * The local system has no tokens and therefore any requests sent to a file
2635 * server will fail. Unfortunately, there are special system processes that
2636 * perform actions on files and directories in preparation for memory mapping
2637 * executables. If the open check fails, the real request from the user process
2638 * will never be issued.
2640 * Permitting the file system to allow subsequent operations to proceed does
2641 * not compromise security. All requests to obtain file data or directory
2642 * enumerations will subsequently fail if they are not submitted under the
2643 * context of a process for that have access to the necessary credentials.
2646 if ( userp == sysUserp)
2648 osi_Log1(afsd_logp, "RDR_OpenFileEntry LOCAL_SYSTEM access check skipped scp=0x%p",
2650 pResultCB->GrantedAccess = OpenCB->DesiredAccess;
2651 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
2662 "RDR_OpenFileEntry repeating open check scp=0x%p userp=0x%p code=0x%x",
2665 code = cm_CheckNTOpen(scp, OpenCB->DesiredAccess, OpenCB->ShareAccess,
2667 OpenCB->ProcessId, OpenCB->Identifier,
2670 code = RDR_CheckAccess(scp, userp, &req, OpenCB->DesiredAccess, &pResultCB->GrantedAccess);
2671 cm_CheckNTOpenDone(scp, userp, &req, &ldp);
2672 } while (count < 100 && (code == CM_ERROR_RETRY || code == CM_ERROR_WOULDBLOCK));
2676 * If we are restricting sharing, we should do so with a suitable
2679 if (code == 0 && scp->fileType == CM_SCACHETYPE_FILE && !(OpenCB->ShareAccess & FILE_SHARE_WRITE)) {
2681 LARGE_INTEGER LOffset, LLength;
2684 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
2685 LOffset.LowPart = SMB_FID_QLOCK_LOW;
2686 LLength.HighPart = 0;
2687 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
2690 * If we are not opening the file for writing, then we don't
2691 * try to get an exclusive lock. No one else should be able to
2692 * get an exclusive lock on the file anyway, although someone
2693 * else can get a shared lock.
2695 if ((OpenCB->ShareAccess & FILE_SHARE_READ) || !(OpenCB->DesiredAccess & AFS_ACCESS_WRITE))
2697 sLockType = LOCKING_ANDX_SHARED_LOCK;
2702 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, OpenCB->Identifier);
2704 lock_ObtainWrite(&scp->rw);
2705 code = cm_Lock(scp, sLockType, LOffset, LLength, key, 0, userp, &req, NULL);
2706 lock_ReleaseWrite(&scp->rw);
2709 code = CM_ERROR_SHARING_VIOLATION;
2710 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
2712 if (sLockType == LOCKING_ANDX_SHARED_LOCK)
2713 pResultCB->FileAccess = AFS_FILE_ACCESS_SHARED;
2715 pResultCB->FileAccess = AFS_FILE_ACCESS_EXCLUSIVE;
2718 pResultCB->FileAccess = AFS_FILE_ACCESS_NOLOCK;
2721 cm_ReleaseUser(sysUserp);
2722 if (code == 0 && bHoldFid)
2723 RDR_FlagScpInUse( scp, FALSE );
2724 cm_ReleaseSCache(scp);
2727 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2728 (*ResultCB)->ResultStatus = status;
2729 osi_Log2(afsd_logp, "RDR_OpenFileEntry FAILURE code=0x%x status=0x%x",
2732 (*ResultCB)->ResultStatus = 0;
2733 (*ResultCB)->ResultBufferLength = sizeof( AFSFileOpenResultCB);
2734 osi_Log0(afsd_logp, "RDR_OpenFileEntry SUCCESS");
2740 RDR_ReleaseFileAccess( IN cm_user_t *userp,
2741 IN AFSFileID FileId,
2742 IN AFSFileAccessReleaseCB *ReleaseFileCB,
2744 IN DWORD ResultBufferLength,
2745 IN OUT AFSCommResult **ResultCB)
2748 unsigned int sLockType;
2749 LARGE_INTEGER LOffset, LLength;
2750 cm_scache_t *scp = NULL;
2758 req.flags |= CM_REQ_WOW64;
2760 osi_Log4(afsd_logp, "RDR_ReleaseFileAccess File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
2761 FileId.Cell, FileId.Volume,
2762 FileId.Vnode, FileId.Unique);
2764 *ResultCB = (AFSCommResult *)malloc( sizeof( AFSCommResult));
2766 osi_Log0(afsd_logp, "RDR_ReleaseFileAccess out of memory");
2770 memset( *ResultCB, '\0', sizeof( AFSCommResult));
2772 if (ReleaseFileCB->FileAccess == AFS_FILE_ACCESS_NOLOCK)
2775 /* Process the release */
2776 Fid.cell = FileId.Cell;
2777 Fid.volume = FileId.Volume;
2778 Fid.vnode = FileId.Vnode;
2779 Fid.unique = FileId.Unique;
2780 Fid.hash = FileId.Hash;
2782 code = cm_GetSCache(&Fid, &scp, userp, &req);
2784 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
2785 (*ResultCB)->ResultStatus = status;
2786 osi_Log2(afsd_logp, "RDR_ReleaseFileAccess cm_GetSCache FID failure code=0x%x status=0x%x",
2791 if (ReleaseFileCB->FileAccess == AFS_FILE_ACCESS_SHARED)
2792 sLockType = LOCKING_ANDX_SHARED_LOCK;
2796 key = cm_GenerateKey(CM_SESSION_IFS, SMB_FID_QLOCK_PID, ReleaseFileCB->Identifier);
2798 LOffset.HighPart = SMB_FID_QLOCK_HIGH;
2799 LOffset.LowPart = SMB_FID_QLOCK_LOW;
2800 LLength.HighPart = 0;
2801 LLength.LowPart = SMB_FID_QLOCK_LENGTH;
2803 lock_ObtainWrite(&scp->rw);
2805 code = cm_SyncOp(scp, NULL, userp, &req, 0, CM_SCACHESYNC_LOCK);
2808 code = cm_Unlock(scp, sLockType, LOffset, LLength, key, 0, userp, &req);
2810 cm_SyncOpDone(scp, NULL, CM_SCACHESYNC_LOCK);
2812 if (code == CM_ERROR_RANGE_NOT_LOCKED)
2814 osi_Log3(afsd_logp, "RDR_ReleaseFileAccess Range Not Locked -- FileAccess 0x%x ProcessId 0x%x HandleId 0x%x",
2815 ReleaseFileCB->FileAccess, ReleaseFileCB->ProcessId, ReleaseFileCB->Identifier);
2819 lock_ReleaseWrite(&scp->rw);
2821 osi_Log0(afsd_logp, "RDR_ReleaseFileAccessEntry SUCCESS");
2825 HexCheckSum(unsigned char * buf, int buflen, unsigned char * md5cksum)
2828 static char tr[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
2831 return "buffer length too small to HexCheckSum";
2833 for (i=0;i<16;i++) {
2836 buf[i*2] = tr[k / 16];
2837 buf[i*2+1] = tr[k % 16];
2845 * Extent requests from the file system are triggered when a file
2846 * page is not resident in the Windows cache. The file system is
2847 * responsible for loading the page but cannot block the request
2848 * while doing so. The AFS Redirector forwards the requests to
2849 * the AFS cache manager while indicating to Windows that the page
2850 * is not yet available. A polling operation will then ensue with
2851 * the AFS Redirector issuing a RDR_RequestFileExtentsXXX call for
2852 * each poll attempt. As each request is received and processed
2853 * by a separate worker thread in the service, this can lead to
2854 * contention by multiple threads attempting to claim the same
2855 * cm_buf_t objects. Therefore, it is important that
2857 * (a) the service avoid processing more than one overlapping
2858 * extent request at a time
2859 * (b) background daemon processing be used to avoid blocking
2862 * Beginning with the 20091122 build of the redirector, the redirector
2863 * will not issue an additional RDR_RequestFileExtentsXXX call for
2864 * each poll request. Instead, afsd_service is required to track
2865 * the requests and return them to the redirector or fail the
2866 * portions of the request that cannot be satisfied.
2868 * The request processing returns any extents that can be returned
2869 * immediately to the redirector. The rest of the requested range(s)
2870 * are queued as background operations using RDR_BkgFetch().
2873 /* do the background fetch. */
2875 RDR_BkgFetch(cm_scache_t *scp, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
2876 cm_user_t *userp, cm_req_t *reqp)
2882 osi_hyper_t fetched;
2883 osi_hyper_t tblocksize;
2886 cm_buf_t *bufp = NULL;
2887 DWORD dwResultBufferLength;
2888 AFSSetFileExtentsCB *pResultCB;
2892 int reportErrorToRedir = 0;
2893 int force_retry = 0;
2895 FileId.Cell = scp->fid.cell;
2896 FileId.Volume = scp->fid.volume;
2897 FileId.Vnode = scp->fid.vnode;
2898 FileId.Unique = scp->fid.unique;
2899 FileId.Hash = scp->fid.hash;
2901 if ((GetTickCount() - reqp->startTime) / 1000 > HardDeadtimeout * 5) {
2902 RDR_SetFileStatus( &scp->fid, STATUS_IO_TIMEOUT);
2906 fetched.LowPart = 0;
2907 fetched.HighPart = 0;
2908 tblocksize = ConvertLongToLargeInteger(cm_data.buf_blockSize);
2911 length.LowPart = p3;
2912 length.HighPart = p4;
2914 end = LargeIntegerAdd(base, length);
2916 osi_Log5(afsd_logp, "Starting BKG Fetch scp 0x%p offset 0x%x:%x length 0x%x:%x",
2917 scp, p2, p1, p4, p3);
2920 * Make sure we have a callback.
2921 * This is necessary so that we can return access denied
2922 * if a callback cannot be granted.
2924 lock_ObtainWrite(&scp->rw);
2925 code = cm_SyncOp(scp, NULL, userp, reqp, PRSFS_READ,
2926 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
2928 lock_ReleaseWrite(&scp->rw);
2929 osi_Log2(afsd_logp, "RDR_BkgFetch cm_SyncOp failure scp=0x%p code=0x%x",
2931 smb_MapNTError(cm_MapRPCError(code, reqp), &status, TRUE);
2932 RDR_SetFileStatus( &scp->fid, status);
2935 lock_ReleaseWrite(&scp->rw);
2937 dwResultBufferLength = (DWORD)(sizeof( AFSSetFileExtentsCB) + sizeof( AFSSetFileExtentsCB) * (length.QuadPart / cm_data.blockSize + 1));
2938 pResultCB = (AFSSetFileExtentsCB *)malloc( dwResultBufferLength );
2940 return CM_ERROR_RETRY;
2942 memset( pResultCB, '\0', dwResultBufferLength );
2943 pResultCB->FileId = FileId;
2945 for ( code = 0, offset = base;
2946 code == 0 && LargeIntegerLessThan(offset, end);
2947 offset = LargeIntegerAdd(offset, tblocksize) )
2949 int bBufRelease = TRUE;
2952 lock_ReleaseWrite(&scp->rw);
2956 code = buf_Get(scp, &offset, reqp, &bufp);
2959 * any error from buf_Get() is non-fatal.
2960 * we need to re-queue this extent fetch.
2967 lock_ObtainWrite(&scp->rw);
2971 code = cm_GetBuffer(scp, bufp, NULL, userp, reqp);
2973 if (bufp->flags & CM_BUF_DIRTY)
2974 cm_BufWrite(scp, &bufp->offset, cm_chunkSize, CM_BUF_WRITE_SCP_LOCKED, userp, reqp);
2976 if (!(bufp->qFlags & CM_BUF_QREDIR)) {
2977 #ifdef VALIDATE_CHECK_SUM
2983 lock_ObtainWrite(&buf_globalLock);
2984 if (!(bufp->flags & CM_BUF_DIRTY) &&
2985 bufp->cmFlags == 0 &&
2986 !(bufp->qFlags & CM_BUF_QREDIR)) {
2987 buf_InsertToRedirQueue(scp, bufp);
2988 lock_ReleaseWrite(&buf_globalLock);
2990 #ifdef VALIDATE_CHECK_SUM
2991 buf_ComputeCheckSum(bufp);
2993 pResultCB->FileExtents[count].Flags = 0;
2994 pResultCB->FileExtents[count].FileOffset.QuadPart = bufp->offset.QuadPart;
2995 pResultCB->FileExtents[count].CacheOffset.QuadPart = bufp->datap - RDR_extentBaseAddress;
2996 pResultCB->FileExtents[count].Length = cm_data.blockSize;
2998 fetched = LargeIntegerAdd(fetched, tblocksize);
2999 bBufRelease = FALSE;
3001 #ifdef VALIDATE_CHECK_SUM
3003 HexCheckSum(md5dbg, sizeof(md5dbg), bufp->md5cksum);
3004 snprintf( dbgstr, 1024,
3005 "RDR_BkgFetch md5 %s vol 0x%x vno 0x%x uniq 0x%x foffset 0x%x:%x coffset 0x%x:%x\n",
3007 scp->fid.volume, scp->fid.vnode, scp->fid.unique,
3008 pResultCB->FileExtents[count].FileOffset.HighPart,
3009 pResultCB->FileExtents[count].FileOffset.LowPart,
3010 pResultCB->FileExtents[count].CacheOffset.HighPart,
3011 pResultCB->FileExtents[count].CacheOffset.LowPart);
3012 OutputDebugStringA( dbgstr);
3015 osi_Log4(afsd_logp, "RDR_BkgFetch Extent2FS bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3016 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3018 lock_ReleaseWrite(&buf_globalLock);
3019 if ((bufp->cmFlags != 0) || (bufp->flags & CM_BUF_DIRTY)) {
3020 /* An I/O operation is already in progress */
3022 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",
3023 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3025 osi_Log4(afsd_logp, "RDR_BkgFetch Extent2FS Already held by Redirector bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3026 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3030 osi_Log4(afsd_logp, "RDR_BkgFetch Extent2FS Already held by Redirector bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3031 bufp, bufp->offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3035 * depending on what the error from cm_GetBuffer is
3036 * it may or may not be fatal. Only return fatal errors.
3037 * Re-queue a request for others.
3039 osi_Log5(afsd_logp, "RDR_BkgFetch Extent2FS FAILURE bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x code 0x%x",
3040 bufp, offset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize, code);
3042 case CM_ERROR_NOACCESS:
3043 case CM_ERROR_NOSUCHFILE:
3044 case CM_ERROR_NOSUCHPATH:
3045 case CM_ERROR_NOSUCHVOLUME:
3046 case CM_ERROR_NOSUCHCELL:
3047 case CM_ERROR_INVAL:
3048 case CM_ERROR_BADFD:
3049 case CM_ERROR_CLOCKSKEW:
3051 case CM_ERROR_QUOTA:
3052 case CM_ERROR_LOCK_CONFLICT:
3054 * these are fatal errors. deliver what we can
3057 reportErrorToRedir = 1;
3061 * non-fatal errors. re-queue the exent
3063 code = CM_ERROR_RETRY;
3072 lock_ObtainWrite(&scp->rw);
3076 /* wakeup anyone who is waiting */
3077 if (scp->flags & CM_SCACHEFLAG_WAITING) {
3078 osi_Log1(afsd_logp, "RDR Bkg Fetch Waking scp 0x%p", scp);
3079 osi_Wakeup((LONG_PTR) &scp->flags);
3081 lock_ReleaseWrite(&scp->rw);
3084 pResultCB->ExtentCount = count;
3085 RDR_SetFileExtents( pResultCB, dwResultBufferLength);
3089 if (reportErrorToRedir) {
3090 smb_MapNTError(cm_MapRPCError(code, reqp), &status, TRUE);
3091 RDR_SetFileStatus( &scp->fid, status);
3094 osi_Log4(afsd_logp, "Ending BKG Fetch scp 0x%p code 0x%x fetched 0x%x:%x",
3095 scp, code, fetched.HighPart, fetched.LowPart);
3097 return force_retry ? CM_ERROR_RETRY : code;
3102 RDR_RequestFileExtentsAsync( IN cm_user_t *userp,
3103 IN AFSFileID FileId,
3104 IN AFSRequestExtentsCB *RequestExtentsCB,
3106 IN OUT DWORD * ResultBufferLength,
3107 IN OUT AFSSetFileExtentsCB **ResultCB)
3109 AFSSetFileExtentsCB *pResultCB = NULL;
3113 cm_scache_t *scp = NULL;
3116 afs_uint32 code = 0;
3118 LARGE_INTEGER ByteOffset, BeginOffset, EndOffset, QueueOffset;
3119 afs_uint32 QueueLength;
3121 BOOLEAN bBufRelease = TRUE;
3125 req.flags |= CM_REQ_WOW64;
3126 req.flags |= CM_REQ_NORETRY;
3128 osi_Log4(afsd_logp, "RDR_RequestFileExtentsAsync File FID cell=0x%x vol=0x%x vn=0x%x uniq=0x%x",
3129 FileId.Cell, FileId.Volume,
3130 FileId.Vnode, FileId.Unique);
3131 osi_Log4(afsd_logp, "... Flags 0x%x ByteOffset 0x%x:%x Length 0x%x",
3132 RequestExtentsCB->Flags,
3133 RequestExtentsCB->ByteOffset.HighPart, RequestExtentsCB->ByteOffset.LowPart,
3134 RequestExtentsCB->Length);
3135 Length = sizeof( AFSSetFileExtentsCB) + sizeof( AFSFileExtentCB) * (RequestExtentsCB->Length / cm_data.blockSize + 1);
3137 pResultCB = *ResultCB = (AFSSetFileExtentsCB *)malloc( Length );
3138 if (*ResultCB == NULL) {
3139 *ResultBufferLength = 0;
3142 *ResultBufferLength = Length;
3144 memset( pResultCB, '\0', Length );
3145 pResultCB->FileId = FileId;
3147 Fid.cell = FileId.Cell;
3148 Fid.volume = FileId.Volume;
3149 Fid.vnode = FileId.Vnode;
3150 Fid.unique = FileId.Unique;
3151 Fid.hash = FileId.Hash;
3153 code = cm_GetSCache(&Fid, &scp, userp, &req);
3155 osi_Log1(afsd_logp, "RDR_RequestFileExtentsAsync cm_GetSCache FID failure code=0x%x",
3157 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3162 * Make sure we have a callback.
3163 * This is necessary so that we can return access denied
3164 * if a callback cannot be granted.
3166 lock_ObtainWrite(&scp->rw);
3167 code = cm_SyncOp(scp, NULL, userp, &req, PRSFS_READ,
3168 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
3169 lock_ReleaseWrite(&scp->rw);
3171 cm_ReleaseSCache(scp);
3172 osi_Log2(afsd_logp, "RDR_RequestFileExtentsAsync cm_SyncOp failure scp=0x%p code=0x%x",
3174 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3175 RDR_SetFileStatus( &scp->fid, status);
3179 /* Allocate the extents from the buffer package */
3181 ByteOffset = BeginOffset = RequestExtentsCB->ByteOffset,
3182 EndOffset.QuadPart = ByteOffset.QuadPart + RequestExtentsCB->Length;
3183 code == 0 && ByteOffset.QuadPart < EndOffset.QuadPart;
3184 ByteOffset.QuadPart += cm_data.blockSize)
3186 BOOL bHaveBuffer = FALSE;
3189 thyper.QuadPart = ByteOffset.QuadPart;
3191 code = buf_Get(scp, &thyper, &req, &bufp);
3193 lock_ObtainMutex(&bufp->mx);
3196 if (bufp->qFlags & CM_BUF_QREDIR) {
3198 } else if (bufp->flags & CM_BUF_DIRTY) {
3199 bHaveBuffer = FALSE;
3201 code = buf_CleanAsyncLocked(scp, bufp, &req, 0, NULL);
3206 case CM_ERROR_RETRY:
3207 /* Couldn't flush it, obtain it asynchronously so we don't block the thread. */
3208 bHaveBuffer = FALSE;
3212 smb_MapNTError(cm_MapRPCError(code, &req), &status, TRUE);
3213 RDR_SetFileStatus(&FileId, status);
3214 bHaveBuffer = FALSE;
3219 osi_hyper_t minLength; /* effective end of file */
3221 lock_ObtainRead(&scp->rw);
3222 bHaveBuffer = cm_HaveBuffer(scp, bufp, TRUE);
3224 if (LargeIntegerGreaterThan(scp->length, scp->serverLength))
3225 minLength = scp->serverLength;
3227 minLength = scp->length;
3230 LargeIntegerGreaterThanOrEqualTo(bufp->offset, minLength)) {
3231 memset(bufp->datap, 0, cm_data.buf_blockSize);
3232 bufp->dataVersion = scp->dataVersion;
3235 else if ((RequestExtentsCB->Flags & AFS_EXTENT_FLAG_CLEAN) &&
3236 ByteOffset.QuadPart <= bufp->offset.QuadPart &&
3237 EndOffset.QuadPart >= bufp->offset.QuadPart + cm_data.blockSize)
3239 memset(bufp->datap, 0, cm_data.blockSize);
3240 bufp->dataVersion = scp->dataVersion;
3241 buf_SetDirty(bufp, &req, 0, cm_data.blockSize, userp);
3244 lock_ReleaseRead(&scp->rw);
3248 * if this buffer is already up to date, skip it.
3251 if (ByteOffset.QuadPart == BeginOffset.QuadPart) {
3252 BeginOffset.QuadPart += cm_data.blockSize;
3254 QueueLength = (afs_uint32)(ByteOffset.QuadPart - BeginOffset.QuadPart);
3255 QueueOffset = BeginOffset;
3256 BeginOffset = ByteOffset;
3259 if (!(bufp->qFlags & CM_BUF_QREDIR)) {
3260 #ifdef VALIDATE_CHECK_SUM
3266 lock_ObtainWrite(&buf_globalLock);
3267 if (!(bufp->qFlags & CM_BUF_QREDIR)) {
3268 buf_InsertToRedirQueue(scp, bufp);
3269 lock_ReleaseWrite(&buf_globalLock);
3271 #ifdef VALIDATE_CHECK_SUM
3272 buf_ComputeCheckSum(bufp);
3274 /* we already have the buffer, return it now */
3275 pResultCB->FileExtents[count].Flags = 0;
3276 pResultCB->FileExtents[count].FileOffset = ByteOffset;
3277 pResultCB->FileExtents[count].CacheOffset.QuadPart = bufp->datap - RDR_extentBaseAddress;
3278 pResultCB->FileExtents[count].Length = cm_data.blockSize;
3281 bBufRelease = FALSE;
3283 #ifdef VALIDATE_CHECK_SUM
3285 HexCheckSum(md5dbg, sizeof(md5dbg), bufp->md5cksum);
3286 snprintf( dbgstr, 1024,
3287 "RDR_RequestFileExtentsAsync md5 %s vol 0x%x vno 0x%x uniq 0x%x foffset 0x%x:%x coffset 0x%x:%x\n",
3289 scp->fid.volume, scp->fid.vnode, scp->fid.unique,
3290 pResultCB->FileExtents[count].FileOffset.HighPart,
3291 pResultCB->FileExtents[count].FileOffset.LowPart,
3292 pResultCB->FileExtents[count].CacheOffset.HighPart,
3293 pResultCB->FileExtents[count].CacheOffset.LowPart);
3294 OutputDebugStringA( dbgstr);
3297 osi_Log4(afsd_logp, "RDR_RequestFileExtentsAsync Extent2FS bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3298 bufp, ByteOffset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3300 lock_ReleaseWrite(&buf_globalLock);
3305 * The service is not handing off the extent to the redirector in this pass.
3306 * However, we know the buffer is in recent use so move the buffer to the
3307 * front of the queue
3309 lock_ObtainWrite(&buf_globalLock);
3310 buf_MoveToHeadOfRedirQueue(scp, bufp);
3311 lock_ReleaseWrite(&buf_globalLock);
3313 osi_Log4(afsd_logp, "RDR_RequestFileExtentsAsync Extent2FS Already held by Redirector bufp 0x%p foffset 0x%p coffset 0x%p len 0x%x",
3314 bufp, ByteOffset.QuadPart, bufp->datap - RDR_extentBaseAddress, cm_data.blockSize);
3318 lock_ReleaseMutex(&bufp->mx);
3323 cm_QueueBKGRequest(scp, RDR_BkgFetch, QueueOffset.LowPart, QueueOffset.HighPart,
3324 QueueLength, 0, userp, &req);
3325 osi_Log3(afsd_logp, "RDR_RequestFileExtentsAsync Queued a Background Fetch offset 0x%x:%x length 0x%x",
3326 QueueOffset.HighPart, QueueOffset.LowPart, QueueLength);
3329 /* No error from buf_Get() can be fatal */
3330 osi_Log3(afsd_logp, "RDR_RequestFileExtentsAsync buf_Get FAILURE offset 0x%x:%x code 0x%x",
3331 BeginOffset.HighPart, BeginOffset.LowPart, code);