-
Notifications
You must be signed in to change notification settings - Fork 1
/
wincache_aplist.c
2161 lines (1750 loc) · 61.2 KB
/
wincache_aplist.c
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------------------------------+
| Windows Cache for PHP |
+----------------------------------------------------------------------------------------------+
| Copyright (c) 2009, Microsoft Corporation. All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without modification, are |
| permitted provided that the following conditions are met: |
| - Redistributions of source code must retain the above copyright notice, this list of |
| conditions and the following disclaimer. |
| - Redistributions in binary form must reproduce the above copyright notice, this list of |
| conditions and the following disclaimer in the documentation and/or other materials provided |
| with the distribution. |
| - Neither the name of the Microsoft Corporation nor the names of its contributors may be |
| used to endorse or promote products derived from this software without specific prior written|
| permission. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE|
| GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| OF THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------------------------+
| Module: wincache_aplist.c |
+----------------------------------------------------------------------------------------------+
| Author: Kanwaljeet Singla <[email protected]> |
| Updated: Eric Stenson <[email protected]> |
+----------------------------------------------------------------------------------------------+
*/
#include "precomp.h"
#define PER_RUN_SCAVENGE_COUNT 256
#define DO_PARTIAL_SCAVENGER_RUN 0
#define DO_FULL_SCAVENGER_RUN 1
#define SCAVENGER_STATUS_INVALID 255
#define SCAVENGER_STATUS_INACTIVE 0
#define SCAVENGER_STATUS_ACTIVE 1
#define DO_FILE_CHANGE_CHECK 1
#define NO_FILE_CHANGE_CHECK 0
#define FILE_IS_NOT_CHANGED 0
#define FILE_IS_CHANGED 1
#define APLIST_VALUE(p, o) ((aplist_value *)alloc_get_cachevalue(p, o))
static int find_aplist_entry(aplist_context * pcache, const char * filename, unsigned int index, unsigned char docheck, aplist_value ** ppvalue, aplist_value ** ppdelete);
static int is_file_changed(aplist_context * pcache, aplist_value * pvalue);
static int create_aplist_data(aplist_context * pcache, const char * filename, aplist_value ** ppvalue);
static void destroy_aplist_data(aplist_context * pcache, aplist_value * pvalue);
static void add_aplist_entry(aplist_context * pcache, unsigned int index, aplist_value * pvalue);
static void remove_aplist_entry(aplist_context * pcache, unsigned int index, aplist_value * pvalue);
static void run_aplist_scavenger(aplist_context * pcache, unsigned char ffull);
static int set_lastcheck_time(aplist_context * pcache, const char * filename, unsigned int newvalue);
/* Globals */
unsigned short glcacheid = 1;
/* Private methods */
/* Call this method atleast under a read lock */
/* If an entry for filename with is_deleted is found, return that in ppdelete */
static int find_aplist_entry(aplist_context * pcache, const char * filename, unsigned int index, unsigned char docheck, aplist_value ** ppvalue, aplist_value ** ppdelete)
{
int result = NONFATAL;
aplist_header * header = NULL;
aplist_value * pvalue = NULL;
dprintverbose("start find_aplist_entry");
_ASSERT(pcache != NULL);
_ASSERT(filename != NULL);
_ASSERT(ppvalue != NULL);
*ppvalue = NULL;
if(ppdelete != NULL)
{
*ppdelete = NULL;
}
header = pcache->apheader;
pvalue = APLIST_VALUE(pcache->apalloc, header->values[index]);
while(pvalue != NULL)
{
if(!_stricmp(pcache->apmemaddr + pvalue->file_path, filename))
{
/* Ignore entries which are marked deleted */
if(pvalue->is_deleted == 1)
{
/* If this process is good to delete the */
/* cache data tell caller to delete it */
if(ppdelete != NULL && *ppdelete == NULL &&
pcache->apctype == APLIST_TYPE_GLOBAL)
{
*ppdelete = pvalue;
}
pvalue = APLIST_VALUE(pcache->apalloc, pvalue->next_value);
continue;
}
if(docheck)
{
/* If fcnotify is 0, use traditional file change check */
if(pvalue->fcnotify == 0)
{
if(is_file_changed(pcache, pvalue))
{
result = FATAL_FCACHE_FILECHANGED;
}
}
else
{
if(pvalue->is_changed == FILE_IS_CHANGED)
{
result = FATAL_FCACHE_FILECHANGED;
}
else
{
/* If a listener is present, just check if listener is still active */
if(pcache->pnotify != NULL)
{
result = fcnotify_check(pcache->pnotify, NULL, &pvalue->fcnotify, &pvalue->fcncount);
if(FAILED(result))
{
/* Do a hard file change check if file listener is suggesting it */
if(result == WARNING_FCNOTIFY_FORCECHECK)
{
result = NONFATAL;
if(is_file_changed(pcache, pvalue))
{
result = FATAL_FCACHE_FILECHANGED;
}
}
else
{
goto Finished;
}
}
}
}
}
}
break;
}
pvalue = APLIST_VALUE(pcache->apalloc, pvalue->next_value);
}
*ppvalue = pvalue;
Finished:
dprintverbose("end find_aplist_entry");
return result;
}
/* Call this method atleast under a read lock */
static int is_file_changed(aplist_context * pcache, aplist_value * pvalue)
{
HRESULT hr = S_OK;
int retvalue = 0;
unsigned int tickcount = 0;
unsigned int difftime = 0;
WIN32_FILE_ATTRIBUTE_DATA fileData;
dprintverbose("start is_file_changed");
_ASSERT(pcache != NULL);
_ASSERT(pvalue != NULL);
/* If fchangefreq is set to 0, dont do the file change check */
/* last_check value of 0 means that a check must be done */
if(pvalue->last_check != 0 && pcache->fchangefreq == 0)
{
goto Finished;
}
/* Calculate difftime while taking care of rollover */
tickcount = GetTickCount();
difftime = utils_ticksdiff(tickcount, pvalue->last_check);
if(pvalue->last_check != 0 && difftime < pcache->fchangefreq)
{
goto Finished;
}
retvalue = 1;
if (!GetFileAttributesEx(pcache->apmemaddr + pvalue->file_path, GetFileExInfoStandard, &fileData))
{
hr = HRESULT_FROM_WIN32(GetLastError());
dprintimportant("GetFileAttributesEx returned error %d", hr);
if (hr == HRESULT_FROM_WIN32(ERROR_BAD_NET_NAME) ||
hr == HRESULT_FROM_WIN32(ERROR_BAD_NETPATH) ||
hr == HRESULT_FROM_WIN32(ERROR_NETNAME_DELETED) ||
hr == HRESULT_FROM_WIN32(ERROR_REM_NOT_LIST) ||
hr == HRESULT_FROM_WIN32(ERROR_SEM_TIMEOUT) ||
hr == HRESULT_FROM_WIN32(ERROR_NETWORK_BUSY) ||
hr == HRESULT_FROM_WIN32(ERROR_UNEXP_NET_ERR) ||
hr == HRESULT_FROM_WIN32(ERROR_NETWORK_UNREACHABLE))
{
retvalue = 0;
}
goto Finished;
}
_ASSERT(fileData.nFileSizeHigh == 0);
/* If the attributes, WriteTime, and if it is a file, size are same */
/* then the file has not changed. File sizes greater than 4GB won't */
/* get added to cache. So comparing only nFileSizeLow is sufficient */
if (fileData.dwFileAttributes == pvalue->attributes &&
*(__int64 *)&fileData.ftLastWriteTime / 10000000 ==
*(__int64 *)&pvalue->modified_time / 10000000 &&
((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
(fileData.nFileSizeLow == pvalue->file_size)))
{
retvalue = 0;
}
pvalue->last_check = tickcount;
Finished:
dprintverbose("end is_file_changed");
return retvalue;
}
/* Call this method without any lock */
static int create_aplist_data(aplist_context * pcache, const char * filename, aplist_value ** ppvalue)
{
int result = NONFATAL;
HRESULT hr = S_OK;
unsigned int ticks = 0;
char * filepath = NULL;
HANDLE hFile = INVALID_HANDLE_VALUE;
LARGE_INTEGER li = { 0 };
unsigned int openflags = 0;
BY_HANDLE_FILE_INFORMATION finfo;
aplist_value * pvalue = NULL;
size_t flength = 0;
size_t alloclen = 0;
char * pbaseadr = NULL;
char * pcurrent = NULL;
dprintverbose("start create_aplist_data");
_ASSERT(pcache != NULL);
_ASSERT(filename != NULL);
_ASSERT(ppvalue != NULL);
*ppvalue = NULL;
flength = strlen(filename);
alloclen = sizeof(aplist_value) + ALIGNDWORD(flength + 1);
/* Allocate memory for cache entry in shared memory */
pbaseadr = (char *)alloc_smalloc(pcache->apalloc, alloclen);
if(pbaseadr == NULL)
{
/* If scavenger is active, run it and try allocating again */
if(pcache->scstatus == SCAVENGER_STATUS_ACTIVE)
{
lock_lock(pcache->aplock);
run_aplist_scavenger(pcache, DO_FULL_SCAVENGER_RUN);
pcache->apheader->lscavenge = GetTickCount();
lock_unlock(pcache->aplock);
pbaseadr = (char *)alloc_smalloc(pcache->apalloc, alloclen);
if(pbaseadr == NULL)
{
result = FATAL_OUT_OF_SMEMORY;
goto Finished;
}
}
else
{
result = FATAL_OUT_OF_SMEMORY;
goto Finished;
}
}
pcurrent = pbaseadr;
pvalue = (aplist_value *)pcurrent;
pvalue->file_path = 0;
/* Open the file in shared read mode */
openflags |= FILE_ATTRIBUTE_ENCRYPTED;
openflags |= FILE_FLAG_OVERLAPPED;
openflags |= FILE_FLAG_BACKUP_SEMANTICS;
openflags |= FILE_FLAG_SEQUENTIAL_SCAN;
hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, openflags, NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
error_setlasterror();
result = FATAL_FCACHE_CREATEFILE;
goto Finished;
}
/* Fail if file type is not of type_disk */
if(GetFileType(hFile) != FILE_TYPE_DISK)
{
result = FATAL_FCACHE_GETFILETYPE;
goto Finished;
}
if(0 == GetFileSizeEx(hFile, &li))
{
error_setlasterror();
result = FATAL_FCACHE_GETFILESIZE;
goto Finished;
}
/* Fail if file is larger than 4GB */
if (li.HighPart != 0)
{
result = FATAL_FCACHE_FILE_TOO_BIG;
}
if(!GetFileInformationByHandle(hFile, &finfo))
{
error_setlasterror();
result = FATAL_FCACHE_FILEINFO;
goto Finished;
}
/* Point pcurrent to end of aplist_value */
/* No goto Finished should exist after this */
pcurrent += sizeof(aplist_value);
/* Fill the details in aplist_value */
/* memcpy_s error code ignored as buffer is of right size */
memcpy_s(pcurrent, flength, filename, flength);
*(pcurrent + flength) = 0;
pvalue->file_path = pcurrent - pcache->apmemaddr;
pvalue->file_size = li.LowPart;
pvalue->modified_time = finfo.ftLastWriteTime;
pvalue->attributes = finfo.dwFileAttributes;
ticks = GetTickCount();
pvalue->add_ticks = ticks;
pvalue->use_ticks = ticks;
pvalue->last_check = ticks;
pvalue->is_deleted = 0;
pvalue->is_changed = FILE_IS_NOT_CHANGED;
pvalue->fcacheval = 0;
pvalue->resentry = 0;
pvalue->fcnotify = 0;
pvalue->fcncount = 0;
pvalue->prev_value = 0;
pvalue->next_value = 0;
/* Add file listener only if the file is not pointing to a UNC share */
if(pcache->pnotify != NULL && IS_UNC_PATH(filename, flength) == 0)
{
result = fcnotify_check(pcache->pnotify, filename, &pvalue->fcnotify, &pvalue->fcncount);
if(FAILED(result))
{
goto Finished;
}
}
*ppvalue = pvalue;
_ASSERT(SUCCEEDED(result));
Finished:
if(hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
if(FAILED(result))
{
dprintimportant("failure %d in create_aplist_data", result);
if(pbaseadr != NULL)
{
alloc_sfree(pcache->apalloc, pbaseadr);
pbaseadr = NULL;
}
}
dprintverbose("end create_aplist_data");
return result;
}
/* Call this method under lock if resentry can be non-zero so that */
/* rplist can never have an offset of aplist which is not valid */
static void destroy_aplist_data(aplist_context * pcache, aplist_value * pvalue)
{
dprintverbose("start destroy_aplist_data");
if(pvalue != NULL)
{
if(pcache->pnotify != NULL && pvalue->fcnotify != 0)
{
fcnotify_close(pcache->pnotify, &pvalue->fcnotify, &pvalue->fcncount);
}
/* Resolve path cache and file cache entries */
/* should be deleted by a call to remove_aplist_entry */
_ASSERT(pvalue->resentry == 0);
_ASSERT(pvalue->fcacheval == 0);
alloc_sfree(pcache->apalloc, pvalue);
pvalue = NULL;
}
dprintverbose("end destroy_aplist_data");
}
/* Call this method under the lock */
static void add_aplist_entry(aplist_context * pcache, unsigned int index, aplist_value * pvalue)
{
aplist_header * header = NULL;
aplist_value * pcheck = NULL;
dprintverbose("start add_aplist_entry");
_ASSERT(pcache != NULL);
_ASSERT(pvalue != NULL);
_ASSERT(pvalue->file_path != 0);
header = pcache->apheader;
pcheck = APLIST_VALUE(pcache->apalloc, header->values[index]);
/* New entry gets added in the end. This is required for is_deleted to work correctly */
while(pcheck != NULL)
{
if(pcheck->next_value == 0)
{
break;
}
pcheck = APLIST_VALUE(pcache->apalloc, pcheck->next_value);
}
if(pcheck != NULL)
{
pcheck->next_value = alloc_get_valueoffset(pcache->apalloc, pvalue);
pvalue->next_value = 0;
pvalue->prev_value = alloc_get_valueoffset(pcache->apalloc, pcheck);
}
else
{
header->values[index] = alloc_get_valueoffset(pcache->apalloc, pvalue);
pvalue->next_value = 0;
pvalue->prev_value = 0;
}
header->itemcount++;
dprintverbose("end add_aplist_entry");
return;
}
/* Call this method under the lock */
static void remove_aplist_entry(aplist_context * pcache, unsigned int index, aplist_value * pvalue)
{
alloc_context * apalloc = NULL;
aplist_header * header = NULL;
aplist_value * ptemp = NULL;
fcache_value * pfvalue = NULL;
dprintverbose("start remove_aplist_entry");
_ASSERT(pcache != NULL);
_ASSERT(pvalue != NULL);
_ASSERT(pvalue->file_path != 0);
/* Delete resolve path cache entries */
if(pvalue->resentry != 0)
{
_ASSERT(pcache->prplist != NULL);
rplist_deleteval(pcache->prplist, pvalue->resentry);
pvalue->resentry = 0;
}
/* Delete file cache entry */
if(pvalue->fcacheval != 0)
{
_ASSERT(pcache->pfcache != NULL);
pfvalue = fcache_getvalue(pcache->pfcache, pvalue->fcacheval);
InterlockedExchange(&pfvalue->is_deleted, 1);
pvalue->fcacheval = 0;
if(InterlockedCompareExchange(&pfvalue->refcount, 0, 0) == 0)
{
fcache_destroyval(pcache->pfcache, pfvalue);
pfvalue = NULL;
}
}
apalloc = pcache->apalloc;
header = pcache->apheader;
/* Decrement itemcount and remove entry from hashtable */
header->itemcount--;
if(pvalue->prev_value == 0)
{
header->values[index] = pvalue->next_value;
if(pvalue->next_value != 0)
{
ptemp = APLIST_VALUE(apalloc, pvalue->next_value);
ptemp->prev_value = 0;
}
}
else
{
ptemp = APLIST_VALUE(apalloc, pvalue->prev_value);
ptemp->next_value = pvalue->next_value;
if(pvalue->next_value != 0)
{
ptemp = APLIST_VALUE(apalloc, pvalue->next_value);
ptemp->prev_value = pvalue->prev_value;
}
}
/* Destroy aplist data now that fcache is deleted */
destroy_aplist_data(pcache, pvalue);
pvalue = NULL;
dprintverbose("end remove_aplist_entry");
return;
}
/* Call this method under the lock */
static void run_aplist_scavenger(aplist_context * pcache, unsigned char ffull)
{
unsigned int sindex = 0;
unsigned int eindex = 0;
alloc_context * apalloc = NULL;
aplist_header * apheader = NULL;
aplist_value * ptemp = NULL;
aplist_value * pvalue = NULL;
unsigned int ticks = 0;
dprintverbose("start run_aplist_scavenger");
_ASSERT(pcache != NULL);
_ASSERT(pcache->scstatus == SCAVENGER_STATUS_ACTIVE);
ticks = GetTickCount();
apalloc = pcache->apalloc;
apheader = pcache->apheader;
if(ffull)
{
sindex = 0;
eindex = apheader->valuecount;
apheader->scstart = 0;
}
else
{
sindex = apheader->scstart;
eindex = sindex + PER_RUN_SCAVENGE_COUNT;
apheader->scstart = eindex;
if(eindex >= apheader->valuecount)
{
eindex = apheader->valuecount;
apheader->scstart = 0;
}
}
dprintimportant("aplist scavenger sindex = %d, eindex = %d", sindex, eindex);
for( ;sindex < eindex; sindex++)
{
if(apheader->values[sindex] == 0)
{
continue;
}
pvalue = APLIST_VALUE(apalloc, apheader->values[sindex]);
while(pvalue != NULL)
{
ptemp = pvalue;
pvalue = APLIST_VALUE(apalloc, pvalue->next_value);
/* Remove entry if its marked deleted or is unused for ttlmax time */
if(ptemp->is_deleted || (utils_ticksdiff(ticks, ptemp->use_ticks) > apheader->ttlmax))
{
remove_aplist_entry(pcache, sindex, ptemp);
ptemp = NULL;
}
}
}
dprintverbose("end run_aplist_scavenger");
return;
}
/* Public functions */
int aplist_create(aplist_context ** ppcache)
{
int result = NONFATAL;
aplist_context * pcache = NULL;
dprintverbose("start aplist_create");
_ASSERT(ppcache != NULL);
*ppcache = NULL;
pcache = (aplist_context *)alloc_pemalloc(sizeof(aplist_context));
if(pcache == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
pcache->id = glcacheid++;
pcache->islocal = 0;
pcache->apctype = APLIST_TYPE_INVALID;
pcache->scstatus = SCAVENGER_STATUS_INVALID;
pcache->hinitdone = NULL;
pcache->fchangefreq = 0;
pcache->apmemaddr = NULL;
pcache->apheader = NULL;
pcache->apfilemap = NULL;
pcache->aplock = NULL;
pcache->apalloc = NULL;
pcache->prplist = NULL;
pcache->pnotify = NULL;
pcache->pfcache = NULL;
pcache->resnumber = -1;
*ppcache = pcache;
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in aplist_create", result);
}
dprintverbose("end aplist_create");
return result;
}
void aplist_destroy(aplist_context * pcache)
{
dprintverbose("start aplist_destroy");
if(pcache != NULL)
{
alloc_pefree(pcache);
pcache = NULL;
}
dprintverbose("end aplist_destroy");
return;
}
int aplist_initialize(aplist_context * pcache, unsigned short apctype, unsigned int filecount, unsigned int fchangefreq, unsigned int ttlmax)
{
int result = NONFATAL;
unsigned int mapsize = 0;
size_t segsize = 0;
aplist_header * header = NULL;
unsigned int msize = 0;
unsigned short cachekey = 1;
unsigned int cticks = 0;
unsigned short mapclass = FILEMAP_MAP_SRANDOM;
unsigned short locktype = LOCK_TYPE_SHARED;
unsigned char isfirst = 1;
unsigned char islocked = 0;
unsigned int initmemory = 0;
char * prefix = NULL;
size_t cchprefix = 0;
DWORD ret = 0;
dprintverbose("start aplist_initialize");
_ASSERT(pcache != NULL);
_ASSERT(filecount >= NUM_FILES_MINIMUM && filecount <= NUM_FILES_MAXIMUM);
_ASSERT((fchangefreq >= FCHECK_FREQ_MINIMUM && fchangefreq <= FCHECK_FREQ_MAXIMUM) || fchangefreq == 0);
_ASSERT((ttlmax >= TTL_VALUE_MINIMUM && ttlmax <= TTL_VALUE_MAXIMUM) || ttlmax == 0);
/* If more APLIST_TYPE entries are added, add code to control value of islocal carefully */
pcache->apctype = apctype;
pcache->islocal = pcache->apctype;
/* Disable scavenger if ttlmax value is set to 0 */
pcache->scstatus = SCAVENGER_STATUS_ACTIVE;
if(ttlmax == 0)
{
pcache->scstatus = SCAVENGER_STATUS_INACTIVE;
}
/* Initialize memory map to store file list */
result = filemap_create(&pcache->apfilemap);
if(FAILED(result))
{
goto Finished;
}
/* Desired cache size 2MB for 1024 entries. Expression below gives that */
mapsize = ((filecount + 1023) * 2) / 1024;
if(pcache->islocal)
{
mapclass = FILEMAP_MAP_LRANDOM;
locktype = LOCK_TYPE_LOCAL;
}
/* get object name prefix */
result = lock_get_nameprefix("FILELIST_CACHE", cachekey, locktype, &prefix, &cchprefix);
if (FAILED(result))
{
goto Finished;
}
result = utils_create_init_event(prefix, "APLIST_INIT", &pcache->hinitdone, &isfirst);
if (FAILED(result))
{
result = FATAL_APLIST_INIT_EVENT;
goto Finished;
}
islocked = 1;
/* shmfilepath = NULL to make it use page file */
result = filemap_initialize(pcache->apfilemap, FILEMAP_TYPE_FILELIST, cachekey, mapclass, mapsize, isfirst, NULL);
if(FAILED(result))
{
goto Finished;
}
pcache->apmemaddr = (char *)pcache->apfilemap->mapaddr;
segsize = filemap_getsize(pcache->apfilemap);
initmemory = (pcache->apfilemap->existing == 0);
/* Create allocator for file list segment */
result = alloc_create(&pcache->apalloc);
if(FAILED(result))
{
goto Finished;
}
/* initmemory = 1 for all page file backed shared memory allocators */
result = alloc_initialize(pcache->apalloc, pcache->islocal, "FILELIST_SEGMENT", cachekey, pcache->apfilemap->mapaddr, segsize, 1);
if(FAILED(result))
{
goto Finished;
}
/* Get memory for cache header */
msize = sizeof(aplist_header) + ((filecount - 1) * sizeof(size_t));
pcache->apheader = (aplist_header *)alloc_get_cacheheader(pcache->apalloc, msize, CACHE_TYPE_FILELIST);
if(pcache->apheader == NULL)
{
result = FATAL_FCACHE_INITIALIZE;
goto Finished;
}
header = pcache->apheader;
/* Create reader writer locks for the aplist */
result = lock_create(&pcache->aplock);
if(FAILED(result))
{
goto Finished;
}
result = lock_initialize(pcache->aplock, "FILELIST_CACHE", cachekey, locktype, &header->last_owner);
if(FAILED(result))
{
goto Finished;
}
/* Create resolve path cache for global aplist cache */
if(apctype == APLIST_TYPE_GLOBAL)
{
result = rplist_create(&pcache->prplist);
if(FAILED(result))
{
goto Finished;
}
result = rplist_initialize(pcache->prplist, pcache->islocal, isfirst, cachekey, filecount);
if(FAILED(result))
{
goto Finished;
}
/* If file change notification is enabled, create fcnotify_context */
if(WCG(fcndetect))
{
result = fcnotify_create(&pcache->pnotify);
if(FAILED(result))
{
goto Finished;
}
/* Number of folders on which listeners will be active will */
/* be very small. Using filecount as 32 so that scavenger is quick */
result = fcnotify_initialize(pcache->pnotify, pcache->islocal, pcache, pcache->apalloc, 32);
if(FAILED(result))
{
goto Finished;
}
}
}
/* Initialize the aplist_header if this is the first process */
if(pcache->islocal || isfirst)
{
if (initmemory)
{
/* No need to get a lock as other processes */
/* are blocked waiting for hinitdone event */
/* Initialize resolve path cache header */
if(pcache->prplist != NULL)
{
rplist_initheader(pcache->prplist, filecount);
}
if(pcache->pnotify != NULL)
{
fcnotify_initheader(pcache->pnotify, 32);
}
cticks = GetTickCount();
/* We can set last_owner to 0 safely as other processes are */
/* blocked and this process is right now not using lock */
header->mapcount = 1;
header->init_ticks = cticks;
header->last_owner = 0;
header->itemcount = 0;
_ASSERT(filecount > PER_RUN_SCAVENGE_COUNT);
/* Calculate scavenger frequency if ttlmax is not 0 */
if(ttlmax != 0)
{
header->ttlmax = ttlmax * 1000;
header->scfreq = header->ttlmax/(filecount/PER_RUN_SCAVENGE_COUNT);
header->lscavenge = cticks;
header->scstart = 0;
_ASSERT(header->scfreq > 0);
}
header->valuecount = filecount;
memset((void *)header->values, 0, sizeof(size_t) * filecount);
ReleaseMutex(pcache->hinitdone);
islocked = 0;
}
}
else
{
/* Increment the mapcount */
InterlockedIncrement(&header->mapcount);
}
/* Keep fchangefreq in aplist_context */
pcache->fchangefreq = fchangefreq * 1000;
Finished:
if (islocked)
{
ReleaseMutex(pcache->hinitdone);
islocked = 0;
}
if (prefix != NULL)
{
alloc_pefree(prefix);
prefix = NULL;
}
if(FAILED(result))
{
dprintimportant("failure %d in aplist_initialize", result);
/* Must cleanup in exactly reverse order of creation. */
if(pcache->hinitdone != NULL)
{
CloseHandle(pcache->hinitdone);
pcache->hinitdone = NULL;
}
if(pcache->pnotify != NULL)
{
fcnotify_terminate(pcache->pnotify);
fcnotify_destroy(pcache->pnotify);
pcache->pnotify = NULL;
}
if(pcache->prplist != NULL)
{
rplist_terminate(pcache->prplist);
rplist_destroy(pcache->prplist);
pcache->prplist = NULL;
}
if(pcache->aplock != NULL)
{
lock_terminate(pcache->aplock);
lock_destroy(pcache->aplock);
pcache->aplock = NULL;
}
if (pcache->apheader != NULL)
{
/*
* We don't need to decrement the mapcount, since we never
* incremented it.
*/
pcache->apheader = NULL;
}
if(pcache->apalloc != NULL)
{
alloc_terminate(pcache->apalloc);
alloc_destroy(pcache->apalloc);
pcache->apalloc = NULL;
}
if(pcache->apfilemap != NULL)
{
filemap_terminate(pcache->apfilemap);
filemap_destroy(pcache->apfilemap);
pcache->apfilemap = NULL;
}
pcache->apheader = NULL;
}
dprintverbose("end aplist_initialize");
return result;
}
int aplist_fcache_initialize(aplist_context * plcache, unsigned int size, unsigned int maxfilesize)
{
int result = NONFATAL;
fcache_context * pfcache = NULL;
dprintverbose("start aplist_fcache_initialize");
_ASSERT(plcache != NULL);
result = fcache_create(&pfcache);
if(FAILED(result))
{
goto Finished;
}
result = fcache_initialize(pfcache, plcache->islocal, 1, size, maxfilesize);
if(FAILED(result))
{
goto Finished;
}
plcache->pfcache = pfcache;