summaryrefslogblamecommitdiffstats
path: root/private/ntos/cntfs/views/table.c
blob: eae79f15f9b6800aa0f02efae67a75a5fba50259 (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






























































































































































































































































































































































































































































































































































                                                                                                       
/*++

Copyright (c) 1989-1997  Microsoft Corporation

Module Name:

    table.c

Abstract:

    This module contains the routines to manipulate the property id table in
    a property set.


--*/

#include <viewprop.h>       //  needs propset.h and ntfsprop.h

#define Dbg DEBUG_TRACE_PROP_FSCTL


ULONG
BinarySearchIdTable (
    IN PPROPERTY_CONTEXT Context,
    IN PROPID PropertyId
    )

/*++

Routine Description:

    This routines performs a binary search on the Id table

Arguments:

    Context - property context of table

    PropertyId - PROPID to find

Return Value:

    Entry index where the specified property Id exists or where it should be
    inserted

--*/

{
    int Hi, Lo;

    //
    //  Set up for binary search.  Entries between 0 and count-1 are eligible
    //

    Lo = 0;
    Hi = (int) Context->IdTable->PropertyCount - 1;

    //
    //  While we have a valid range to search
    //

    while (Lo <= Hi) {
        int Mid;

        //
        //  Determine midpoint where we'll test
        //

        Mid = (Lo + Hi) / 2;

        //
        //  If we've found the item then return it
        //

        if (PropertyId == Context->IdTable->Entry[Mid].PropertyId) {

            PROPASSERT( Mid >= 0);

            return (ULONG) Mid;

            //
            //  If the property is in the upper range then move
            //  the lower boundary upward
            //

        } else if (PropertyId > Context->IdTable->Entry[Mid].PropertyId) {

            Lo = Mid + 1;

            //
            //  The property is in the lower range.  Move the upper boundary
            //  downward
            //

        } else {

            Hi = Mid - 1;

        }
    }

    //
    //  No exact match was found. Lo is the point before where the property Id
    //  must be inserted.
    //

    PROPASSERT( Lo >= 0 && Lo <= (int) Context->IdTable->PropertyCount );

    return (ULONG) Lo;
}


ULONG
FindIdInTable (
    IN PPROPERTY_CONTEXT Context,
    IN PROPID PropertyId
    )

/*++

Routine Description:

    This routines looks up a property by Id in the property set heap.

Arguments:

    Context - property context of table

    PropertyId - PROPID to find

Return Value:

    Offset to property value in heap.  0 if not found.

--*/

{
    ULONG Index;

    DebugTrace( +1, Dbg, ("FindInTable(%x)\n", PropertyId) );

    //
    //  Binary search table for Id
    //

    Index = BinarySearchIdTable( Context, PropertyId );

    //
    //  If found entry is legal and it matches the Id then return
    //  pointer to value header
    //

    if (Index < Context->IdTable->PropertyCount &&
        Context->IdTable->Entry[Index].PropertyId == PropertyId) {

        DebugTrace( -1, Dbg, ("FindIdInTable: return offset %x (found)\n",
                             Context->IdTable->Entry[Index].PropertyValueOffset ));

        return Context->IdTable->Entry[Index].PropertyValueOffset;
    }

    //
    //  entry not found, return
    //

    DebugTrace( -1, Dbg, ("FindIdInTable: not found\n") );

    return 0;
}


PROPID
FindFreeIdInTable (
    IN PPROPERTY_CONTEXT Context,
    IN PROPID Id
    )

/*++

Routine Description:

    This routines looks in the table to find the next propid beginning at Id
    that is not allocated

Arguments:

    Context - property context of table

    Id - PROPID to being free searc at

Return Value:

    PROPID of free Id

--*/

{
    ULONG Index;

    //
    //  Find location in table to begin search
    //

    Index = BinarySearchIdTable( Context, Id );

    //
    //  while location is valid and Id is the same
    //

    while (Index < Context->IdTable->PropertyCount &&
           Context->IdTable->Entry[Index].PropertyId == Id) {
        //
        //  advance location and Id
        //

        Index ++;
        Id ++;
    }

    return Id;
}


//
//  Local support routine
//

VOID
GrowIdTable (
    IN PPROPERTY_CONTEXT Context
    )

/*++

Routine Description:

    This routine grows the Id table in the property set.  It handles
    resizing the attribute and moving the property heap.  This means
    resetting the cached pointers inside of Context

Arguments:

    Context - property context for this action.

Return Value:

    Nothing.

--*/

{
    ULONG Count = Context->IdTable->PropertyCount;
    ULONG Offset = Context->Header->ValueHeapOffset;

    //
    //  We grow the property heap by max (PIT_PROPERTY_DELTA, Count / 16)
    //  entries
    //

    ULONG Delta = max( PIT_PROPERTY_DELTA, Count / 16 );

    ULONG NewSize = (ULONG) NtOfsQueryLength( Context->Attribute ) +
                    Delta * sizeof( PROPERTY_TABLE_ENTRY );

    DebugTrace( +1, Dbg, ("GrowIdTable growing by %x\n", Delta) );

    //
    //  Check for growing attribute too much.
    //  BUGBUG - define a better status code.
    //

    if (NewSize > VACB_MAPPING_GRANULARITY) {
        ExRaiseStatus( STATUS_DISK_FULL );
    }

    //
    //  Resize the attribute
    //

    DebugTrace( 0, Dbg, ("Setting size to %x\n", Context->Attribute->Header.FileSize.QuadPart +
                                                    Delta * sizeof( PROPERTY_TABLE_ENTRY )) );

    NtOfsSetLength(
        Context->IrpContext,
        Context->Attribute,
        Context->Attribute->Header.FileSize.QuadPart +
            Delta * sizeof( PROPERTY_TABLE_ENTRY )
        );

    //
    //  Move the heap upwards by Delta
    //

    DebugTrace( 0, Dbg, ("Moving heap from %x to %x\n",
                         ContextOffset( Context, Context->HeapHeader ),
                         ContextOffset( Context, Context->HeapHeader ) +
                            Delta * sizeof( PROPERTY_TABLE_ENTRY )) );

    LogFileFullFailCheck( Context->IrpContext );
    NtOfsPutData(
        Context->IrpContext,
        Context->Attribute,
        ContextOffset( Context, Context->HeapHeader ) +
            Delta * sizeof( PROPERTY_TABLE_ENTRY ),
        Context->HeapHeader->PropertyHeapLength,
        Context->HeapHeader
        );

    Offset += Delta * sizeof( PROPERTY_TABLE_ENTRY );

    DebugTrace( 0, Dbg, ("Setting ValueHeapOffset to %x\n", Offset) );

    LogFileFullFailCheck( Context->IrpContext );
    NtOfsPutData(
        Context->IrpContext,
        Context->Attribute,
        ContextOffset( Context, &Context->Header->ValueHeapOffset ),
        sizeof( ULONG ),
        &Offset
        );

    Context->HeapHeader = PROPERTY_HEAP_HEADER( Context->Header );

    //
    //  Update the max size by the delta
    //

    Count = Context->IdTable->MaximumPropertyCount + Delta;

    DebugTrace( 0, Dbg, ("Setting max table count to %x\n", Count) );

    LogFileFullFailCheck( Context->IrpContext );
    NtOfsPutData(
        Context->IrpContext,
        Context->Attribute,
        ContextOffset( Context, &Context->IdTable->MaximumPropertyCount ),
        sizeof( ULONG ),
        &Count
        );

    //
    //  Rebase the entry pointers in the propinfo if any
    //

    if (Context->Info != NULL) {
        ULONG i;

        for (i = 0; i < Context->Info->Count; i++) {
            if (Context->Info->Entries[i].Heap != EMPTY_PROPERTY) {
                Context->Info->Entries[i].Heap =
                    Add2Ptr( Context->Info->Entries[i].Heap, Delta );
            }
        }
    }

    DebugTrace( -1, Dbg, ("") );
}


VOID
ChangeTable (
    IN PPROPERTY_CONTEXT Context,
    IN PROPID Id,
    IN ULONG Offset
    )

/*++

Routine Description:

    This routine changes the table based on the new offset

Arguments:

    Context - property context for this action.

    Id - PROPID to find

    Offset - New property value offset

Return Value:

    Nothing.

--*/

{
    ULONG Count = Context->IdTable->PropertyCount;

    //
    //  Binary search table for Id
    //

    ULONG Index = BinarySearchIdTable( Context, Id );


    //
    //  If the offset is zero, then we are deleting the entry
    //

    if (Offset == 0) {

        //
        //  Make sure the returned value makes sense
        //

        ASSERT ( Index < Count && Context->IdTable->Entry[Index].PropertyId == Id );

        //
        //  We move all entries Index+1..PropertyCount down to Index.  Special case
        //  moving the last item.
        //

        if (Index != Count - 1) {

            DebugTrace( 0, Dbg, ("Ripple copy %x to %x length %x\n",
                                 ContextOffset( Context, &Context->IdTable->Entry[Index + 1] ),
                                 ContextOffset( Context, &Context->IdTable->Entry[Index] ),
                                 sizeof( PROPERTY_TABLE_ENTRY ) * (Count - (Index + 1))) );

            LogFileFullFailCheck( Context->IrpContext );
            NtOfsPutData(
                Context->IrpContext,
                Context->Attribute,
                ContextOffset( Context, &Context->IdTable->Entry[Index] ),
                sizeof( PROPERTY_TABLE_ENTRY ) * (Count - (Index + 1)),
                &Context->IdTable->Entry[Index + 1]
                );
        }

        //
        //  Change the count in use
        //

        Count--;

        DebugTrace( 0, Dbg, ("New count is %x\n", Count) );

        LogFileFullFailCheck( Context->IrpContext );
        NtOfsPutData(
            Context->IrpContext,
            Context->Attribute,
            ContextOffset( Context, &Context->IdTable->PropertyCount ),
            sizeof( ULONG ),
            &Count
            );

    } else {

        //
        //  if we found the propertyid in the table
        //

        if (Index < Count && Context->IdTable->Entry[Index].PropertyId == Id) {
            PROPERTY_TABLE_ENTRY Entry = { Id, Offset };

            //
            //  Replace the entry in the table with the new entry
            //

            LogFileFullFailCheck( Context->IrpContext );
            NtOfsPutData(
                Context->IrpContext,
                Context->Attribute,
                ContextOffset( Context, &Context->IdTable->Entry[Index] ),
                sizeof( PROPERTY_TABLE_ENTRY ),
                &Entry
                );

        } else {

            PROPERTY_TABLE_ENTRY Entry = { Id, Offset };

            //
            //  Add the new entry to the table
            //

            //
            //  If there is no more room in the table for a new Id then
            //  grow the IdTable
            //

            if (Count == Context->IdTable->MaximumPropertyCount) {
                GrowIdTable( Context );
            }

            //
            //  Index is the point where the insertion must occur.  We leave
            //  alone elements 0..Index-1, and ripple-copy elements Index..PropertyCount-1
            //  to Index+1.  We skip this case when we are simply appending a propid at the
            //  end.
            //

            if (Index < Count) {

                DebugTrace( 0, Dbg, ("Ripple copy table from %x to %x length %x\n",
                                     PtrOffset( Context->IdTable, &Context->IdTable->Entry[Index] ),
                                     PtrOffset( Context->IdTable, &Context->IdTable->Entry[Index + 1]),
                                     sizeof( PROPERTY_TABLE_ENTRY ) * (Count - Index)) );

                LogFileFullFailCheck( Context->IrpContext );
                NtOfsPutData(
                    Context->IrpContext,
                    Context->Attribute,
                    ContextOffset( Context, &Context->IdTable->Entry[Index + 1] ),
                    sizeof( PROPERTY_TABLE_ENTRY ) * (Count - Index),
                    &Context->IdTable->Entry[Index]
                    );
            }

            //
            //  Stick in the new property entry
            //

            DebugTrace( 0, Dbg, ("new entry %x:%x\n", Entry.PropertyId, Entry.PropertyValueOffset) );

            LogFileFullFailCheck( Context->IrpContext );
            NtOfsPutData(
                Context->IrpContext,
                Context->Attribute,
                ContextOffset( Context, &Context->IdTable->Entry[Index] ),
                sizeof( PROPERTY_TABLE_ENTRY ),
                &Entry
                );

            //
            //  Increment the usage count and log it
            //

            Count++;

            DebugTrace( 0, Dbg, ("new count in table is %x\n", Count) );

            LogFileFullFailCheck( Context->IrpContext );
            NtOfsPutData(
                Context->IrpContext,
                Context->Attribute,
                ContextOffset( Context, &Context->IdTable->PropertyCount ),
                sizeof( ULONG ),
                &Count
                );
        }
    }
}