summaryrefslogblamecommitdiffstats
path: root/private/ntos/dll/ofsmisc.c
blob: 4951fb2dad7e2286a89a4ffafededb97fecf9244 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626

















































































































































































































































































































































































































































































































































































































































                                                                                     
//+--------------------------------------------------------------------------- 
// 
// Microsoft Windows 
// Copyright (C) Microsoft Corporation, 1992-1992 
// 
// File:        ofsmisc.c
//
// Contents:    Miscellaneous OFS interfaces
//
//---------------------------------------------------------------------------

#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <windows.h>
#include <ole2.h>


//+---------------------------------------------------------------------------
// Function:    SynchronousNtFsControlFile
//
// Synopsis:    Call NtFsControlFile and wait if handle is asynchronous
//
// Arguments:   [h]             -- handle to file/directory/volume
//              [pisb]          -- pointer to IO_STATUS_BLOCK
//              [FsControlCode] -- FsControl code
//              [pvIn]          -- optional input buffer
//              [cbIn]          -- input buffer size
//              [pvOut]         -- optional output buffer
//              [cbOut]         -- output buffer size
//
// Returns:     Status code
//---------------------------------------------------------------------------

NTSTATUS
SynchronousNtFsControlFile(
    IN HANDLE h,
    OUT IO_STATUS_BLOCK *pisb,
    IN ULONG FsControlCode,
    IN VOID *pvIn OPTIONAL,
    IN ULONG cbIn,
    OUT VOID *pvOut OPTIONAL,
    IN ULONG cbOut)
{
    NTSTATUS Status;

    Status = NtFsControlFile(
                    h,
                    NULL,
                    NULL,
                    NULL,
                    pisb,
                    FsControlCode,
                    pvIn,
                    cbIn,
                    pvOut,
                    cbOut);

    if (Status == STATUS_PENDING)
    {
        Status = NtWaitForSingleObject(h, TRUE, NULL);
    }
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    UsnFsctl, private
//
// Synopsis:    Return a USN according to the passed FsControlCode
//
// Arguments:   [hf]            -- handle to any file/directory in volume
//              [pusn]          -- pointer to USN to be returned
//              [FsControlCode] -- FSCTL_OFS_USN_GENERATE or FSCTL_OFS_USN_GET_CLOSE.
//
// Returns:     Status code
//---------------------------------------------------------------------------

#if defined(_CAIRO_) || DBG

#include "iofs.h"

NTSTATUS
UsnFsctl(
    IN HANDLE hf,
    OUT USN *pusn,
    IN ULONG FsControlCode)
{
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;

    Status = SynchronousNtFsControlFile(
                hf,
                &isb,
                FsControlCode,
                NULL,                           // input buffer
                0,                              // input buffer length
                pusn,                           // output buffer
                sizeof(*pusn));                 // output buffer length
    ASSERT(!NT_SUCCESS(Status) || isb.Information == sizeof(*pusn));
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    OFSGetCloseUsn, public
//
// Synopsis:    Determine the USN associated with the passed handle.
//
// Arguments:   [hf]            -- handle to any file/directory in volume
//              [pusn]          -- pointer to USN to be returned
//
// Returns:     Status code
//---------------------------------------------------------------------------

NTSTATUS NTSYSAPI NTAPI
OFSGetCloseUsn(
    IN HANDLE hf,
    OUT USN *pusn)
{
    return(UsnFsctl(hf, pusn, FSCTL_OFS_USN_GET_CLOSE));
}


//+---------------------------------------------------------------------------
// Function:    RtlGenerateUsn, public
//
// Synopsis:    Generate and return the next USN associated with a volume.
//
// Arguments:   [hf]            -- handle to any file/directory in volume
//              [pusn]          -- pointer to USN to be returned
//
// Returns:     Status code
//---------------------------------------------------------------------------

NTSTATUS NTSYSAPI NTAPI
RtlGenerateUsn(
    IN HANDLE hf,
    OUT USN *pusn)
{
    return(UsnFsctl(hf, pusn, FSCTL_OFS_USN_GENERATE));
}


//+---------------------------------------------------------------------------
// Function:    OFSGetVersion, public
//
// Synopsis:    Determine if the passed handle resides on an OFS volume.
//              If pversion != NULL, return the format version number.
//
// Arguments:   [hf]            -- handle to any file/directory in volume
//              [pversion]      -- pointer to version (may be NULL)
//
// Returns:     Status code
//
//---------------------------------------------------------------------------

#define QuadAlign(n) (((n) + (sizeof(LONGLONG) - 1)) & ~(sizeof(LONGLONG) - 1))

#define CSTRUCT(n, type, cchname)                                       \
        (((n) * QuadAlign(sizeof(type) + (cchname) * sizeof(WCHAR)) +   \
          sizeof(type) - 1) /                                           \
         sizeof(type))

NTSTATUS NTSYSAPI NTAPI
OFSGetVersion(HANDLE hf, ULONG *pversion)
{
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;

    FILE_FS_ATTRIBUTE_INFORMATION *pfai;
    LARGE_INTEGER faibuf[CSTRUCT(1, FILE_FS_ATTRIBUTE_INFORMATION, 32)];

    pfai = (FILE_FS_ATTRIBUTE_INFORMATION *) faibuf;

    Status = NtQueryVolumeInformationFile(
                 hf,
                 &isb,
                 pfai,
                 sizeof(faibuf),
                 FileFsAttributeInformation);
    if (NT_SUCCESS(Status))
    {
        if (pfai->FileSystemNameLength != 3 * sizeof(WCHAR) ||
            pfai->FileSystemName[0] != L'O' ||
            pfai->FileSystemName[1] != L'F' ||
            pfai->FileSystemName[2] != L'S')
        {
            Status = STATUS_UNRECOGNIZED_VOLUME;
        }
        else if (pversion != NULL)
        {

            Status = SynchronousNtFsControlFile(
                hf,
                &isb,
                FSCTL_OFS_VERSION,
                NULL,                           // input buffer
                0,                              // input buffer length
                pversion,                       // output buffer
                sizeof(*pversion));             // output buffer length

            ASSERT(!NT_SUCCESS(Status) || isb.Information == sizeof(*pversion));
        }
    }
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    RtlNameToOleId, public
//
// Synopsis:    Translate OLENAMEs to ids
//
// Arguments:   [hf]            -- handle to *volume*
//              [cbNames]       -- OLENAMES size
//              [pon]           -- pointer to OLENAMES
//              [pOleId]        -- pointer to output array of OleIds
//
// Returns:     Status code
//
//---------------------------------------------------------------------------

NTSTATUS NTSYSAPI NTAPI
RtlNameToOleId(
    IN HANDLE hf,                               // must be volume handle
    IN ULONG cbNames,
    IN OLENAMES const *pon,
    OUT ULONG *pOleId)
{
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;

    Status = SynchronousNtFsControlFile(
                hf,
                &isb,
                FSCTL_OFS_TRANSLATE_OLENAMES,
                (VOID *) pon,                   // input buffer
                cbNames,                        // input buffer length
                pOleId,                         // output buffer
                pon->cNames * sizeof(*pOleId)); // output buffer length
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    RtlOleIdToName, public
//
// Synopsis:    Translate OLEIDs to names
//
// Arguments:   [hf]            -- handle to *volume*
//              [cOleId]        -- count of OleIds in array
//              [pOleId]        -- pointer to array of OleIds
//              [pcbNameBuf]    -- pointer to OLENAMES buffer size (updated)
//              [pon]           -- pointer to OLENAMES buffer
//
// Returns:     Status code
//
//---------------------------------------------------------------------------

NTSTATUS NTSYSAPI NTAPI
RtlOleIdToName(
    IN HANDLE hf,                               // must be volume handle
    IN ULONG cOleId,
    IN ULONG const *pOleId,
    IN OUT ULONG *pcbNameBuf,
    OUT OLENAMES *pon)
{
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;

    Status = SynchronousNtFsControlFile(
                hf,
                &isb,
                FSCTL_OFS_TRANSLATE_OLEIDS,
                (VOID *) pOleId,          // input buffer
                cOleId * sizeof(*pOleId), // input buffer length
                pon,                      // output buffer
                *pcbNameBuf);             // output buffer length
    *pcbNameBuf = isb.Information;
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    RtlQueryQuota, public
//
// Synopsis:    Query Quota information for specific users
//
// Arguments:   [hf]    -- handle to *volume*
//              [pcb]   -- length of buffer on input and output
//              [pfqi]  -- pointer quota information to be filled in
//
// Returns:     Status code
//
//---------------------------------------------------------------------------

NTSTATUS NTSYSAPI NTAPI
RtlQueryQuota(
    IN HANDLE hf,                               // must be volume handle
    IN OUT ULONG *pcb,
    IN OUT FILE_QUOTA_INFORMATION *pfqi)
{
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;

    Status = SynchronousNtFsControlFile(
                hf,
                &isb,
                FSCTL_OFS_QUERY_QUOTA,
                pfqi,                   // input buffer
                *pcb,                   // input buffer length
                pfqi,                   // output buffer
                *pcb);                  // output buffer length

    *pcb = isb.Information;
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    RtlQueryClassId, public
//
// Synopsis:    Fetch the ClassId for an open handle
//
// Arguments:   [hf]            -- handle to file
//              [pclsid]        -- pointer to ClassId buffer
//
// Returns:     Status code
//---------------------------------------------------------------------------

FILE_OBJECTID_INFORMATION foiiZero;

NTSTATUS NTSYSAPI NTAPI
RtlQueryClassId(
    IN HANDLE hf,
    OUT GUID *pclsid)
{
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;
    FILE_OLE_INFORMATION foi;

    ASSERT(sizeof(GUID) == sizeof(foiiZero.ObjectId.Lineage));
    Status = NtQueryInformationFile(
                 hf,
                 &isb,
                 &foi,
                 sizeof(foi),
                 FileOleInformation);
    if (NT_SUCCESS(Status))
    {
        if (RtlCompareMemory(
                &foiiZero.ObjectId.Lineage,
                &foi.OleClassIdInformation.ClassId,
                sizeof(foiiZero.ObjectId.Lineage)) ==
            sizeof(foiiZero.ObjectId.Lineage))
        {
            Status = STATUS_NOT_FOUND;
        }
        else
        {
            *pclsid = foi.OleClassIdInformation.ClassId;
        }
    }
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    RtlSetClassId, public
//
// Synopsis:    Set the ClassId for an open handle
//
// Arguments:   [hf]            -- handle to file
//              [pclsid]        -- pointer to ClassId -- NULL means delete
//
// Returns:     Status code
//---------------------------------------------------------------------------

NTSTATUS NTSYSAPI NTAPI
RtlSetClassId(
    IN HANDLE hf,
    OPTIONAL IN GUID const *pclsid)
{
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;

    ASSERT(sizeof(foiiZero.ObjectId.Lineage) == sizeof(*pclsid));
    Status = NtSetInformationFile(
                 hf,
                 &isb,
                 pclsid == NULL? &foiiZero.ObjectId.Lineage : (VOID *) pclsid,
                 sizeof(foiiZero.ObjectId.Lineage),
                 FileOleClassIdInformation);
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    RtlQueryObjectId, public
//
// Synopsis:    Fetch the ObjectId for an open handle
//
// Arguments:   [hf]            -- handle to file
//              [poid]          -- pointer to ObjectId buffer
//
// Returns:     Status code
//---------------------------------------------------------------------------

NTSTATUS NTSYSAPI NTAPI
RtlQueryObjectId(
    IN HANDLE hf,
    OUT OBJECTID *poid)
{
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;
    FILE_OLE_INFORMATION foi;

    Status = NtQueryInformationFile(
                 hf,
                 &isb,
                 &foi,
                 sizeof(foi),
                 FileOleInformation);
    if (NT_SUCCESS(Status))
    {
        if (RtlCompareMemory(
                &foiiZero.ObjectId,
                &foi.ObjectIdInformation.ObjectId,
                sizeof(foiiZero.ObjectId)) == sizeof(foiiZero.ObjectId))
        {
            Status = STATUS_NOT_FOUND;
        }
        else
        {
            *poid = foi.ObjectIdInformation.ObjectId;
        }
    }
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    RtlSetObjectId, public
//
// Synopsis:    Set the ObjectId for an open handle
//
// Arguments:   [hf]            -- handle to file
//              [poid]          -- pointer to ObjectId -- NULL means delete
//
// Returns:     Status code
//---------------------------------------------------------------------------

NTSTATUS NTSYSAPI NTAPI
RtlSetObjectId(
    IN HANDLE hf,
    OPTIONAL IN OBJECTID const *poid)
{
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;

    ASSERT(sizeof(foiiZero) == sizeof(*poid));
    Status = NtSetInformationFile(
                 hf,
                 &isb,
                 poid == NULL? &foiiZero : (VOID *) poid,
                 sizeof(foiiZero),
                 FileObjectIdInformation);
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    RtlSetTunnelMode, public
//
// Synopsis:    Set the ObjectId tunnel mode for an OFS volume.
//
// Arguments:   [hVolume]       -- handle to volume
//              [ulFlags]       -- Bits to set
//              [ulMask]        -- Mask of bits to change
//              [pulOld]        -- pointer to store Old flags
//
// Returns:     Status code
//---------------------------------------------------------------------------

NTSTATUS
RtlSetTunnelMode(
    IN HANDLE hVolume,
    IN ULONG ulFlags,
    IN ULONG ulMask,
    OUT ULONG *pulOld)
{
    TUNNELMODE tm;
    TUNNELMODEOUT tmo;
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;

    tm.ulFlags = ulFlags;
    tm.ulMask = ulMask;

    Status = SynchronousNtFsControlFile(
                    hVolume,                    // Handle
                    &isb,                       // I/O Status block
                    FSCTL_OFS_TUNNEL_MODE,      // Control code
                    &tm,                        // Input buffer
                    sizeof(tm),                 // Input length
                    &tmo,                       // Output buffer
                    sizeof(tmo));               // Output length

    *pulOld = tmo.ulFlags;
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    RtlSearchVolume, public
//
// Synopsis:    Search an OFS volume for files with passed ObjectId
//
// Arguments:   [hAncestor]     -- handle to file
//              [poid]          -- pointer to ObjectId
//              [cLineage]      -- Lineage count/flags
//              [fContinue]     -- TRUE if should continue
//              [usBufLen]      -- output buffer length
//              [pResults]      -- output buffer
//
// Returns:     Status code
//---------------------------------------------------------------------------

NTSTATUS NTSYSAPI NTAPI
RtlSearchVolume(
    IN HANDLE hAncestor,
    IN OBJECTID const *poid,
    IN USHORT cLineage,
    IN BOOLEAN fContinue,
    IN ULONG usBufLen,
    OUT FINDOBJECTOUT *pfoo)
{
    NTSTATUS Status;
    IO_STATUS_BLOCK isb;

    ((FINDOBJECT *) pfoo)->oid = *poid;
    ((FINDOBJECT *) pfoo)->cLineage = cLineage;
    ((FINDOBJECT *) pfoo)->ulFlags = fContinue? FO_CONTINUE_ENUM : 0;

    Status = SynchronousNtFsControlFile(
                    hAncestor,                  // Handle
                    &isb,                       // I/O Status block
                    FSCTL_OFS_FINDOBJECT,       // Control code
                    pfoo,                       // Input buffer
                    sizeof(FINDOBJECT),         // Input length
                    pfoo,                       // Output buffer
                    usBufLen);                  // Output length
    return(Status);
}


//+---------------------------------------------------------------------------
// Function:    RtlGenerateRelatedObjectId, public
//
// Synopsis:    Generate an object id which is related to the one passed in.
//
// Arguments:   [poidOld]       -- pointer to original ObjectId
//              [poidNew]       -- pointer to buffer for new ObjectId
//
// Returns:     Status code
//---------------------------------------------------------------------------

VOID NTSYSAPI NTAPI
RtlGenerateRelatedObjectId(
    IN OBJECTID const *poidOld,
    OUT OBJECTID *poidNew)
{       
    poidNew->Lineage = poidOld->Lineage;
    do
    {
        poidNew->Uniquifier = rand();
    } while (poidNew->Uniquifier == poidOld->Uniquifier);
}


//+---------------------------------------------------------------------------
// Function:    RtlSetReplicationState, public
//
// Synopsis:    Generate an object id which is related to the one passed in.
//
// Arguments:   [hf]            -- handle
//
// Returns:     Status code
//---------------------------------------------------------------------------

NTSTATUS NTSYSAPI NTAPI
RtlSetReplicationState(
    IN HANDLE hf)
{       
    IO_STATUS_BLOCK isb;

    return(SynchronousNtFsControlFile(
                    hf,                         // Handle
                    &isb,                       // I/O Status block
                    FSCTL_SET_REPLICATION_STATE,// Control code
                    NULL,                       // Input buffer
                    0,                          // Input length
                    NULL,                       // Output buffer
                    0));                        // Output length
}

#endif // _CAIRO_

//+---------------------------------------------------------------------------
// Function:    _purecall, private
//
// Synopsis:    C++ stub
//
// Arguments:   NONE
//
// Returns:     NONE
//
//---------------------------------------------------------------------------

VOID _CRTAPI1
_purecall(VOID)
{
    ASSERTMSG("_purecall() was called", FALSE);
    RtlRaiseStatus(STATUS_NOT_IMPLEMENTED);
}