-
Notifications
You must be signed in to change notification settings - Fork 3
/
mvfs_dncops.c
2913 lines (2584 loc) · 94.8 KB
/
mvfs_dncops.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
/* * (C) Copyright IBM Corporation 1991, 2010. */
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Author: IBM Corporation
This module is part of the IBM (R) Rational (R) ClearCase (R)
Multi-version file system (MVFS).
For support, please visit http://www.ibm.com/software/support
*/
/* mvfs_dncops.c */
#include "mvfs_systm.h"
#include "mvfs.h"
#include "mvfs_dnc.h"
/*
* THEORY OF OPERATION:
* Prolog:
* As of 05/01/95 this name cache got a lot more complicated
* to support case-insensitive entries for NT name-not-found
* caching. What follows will first describe the basic operation
* of the name cache, and then the additions/changes made to
* support case-insensitive lookups.
*
* Locking in the name cache is redesigned in v5.0. Previous
* versions use a single global spinlock for all cache operations.
* For improved concurrency and scalability on MP systems, this
* single lock is replaced with several different locks, described
* below in the section on locking.
*
* Case-sensitive name cache:
* Basic task:
* The basic task of the name cache is to accelerate the single
* component VOP_LOOKUP call in the core. This call takes
* (dvp, nm) and returns a (error, vp). The name cache caches
* both valid translations (name found, vp != NULL) and ENOENT
* translations (ENOENT, vp == NULL).
*
* This cache is the most heavily performance sensitive portion
* of the MVFS. The basic criteria are:
* 1) Ideally the cache should NEVER miss. Even tiny miss-rates
* rates can result in significantly degraded performance.
* This drives two major themes of this cache:
* - Cache size must be able to hold more translations
* than the system has active vnodes (for those systems
* that have a max number of active vnodes). This means
* the cache must not hold refcounts on large numbers
* of vnodes (only view-tag vnodes are held, because there
* are only a small number of view-tags in use at a time).
* - Misses should be avoided at all costs. Therefore, there
* are lots of optimizations to use entries as much
* possible. If you can think of new ones, please add them!
* 2) CPU time usage should not be excessive. This cache
* may be hit 5-6 times for a 5 or 6 component pathname
* to stat one object. Hence, spinlocks are used instead
* of sleep locks to make this cache reasonably CPU efficient.
* This desire, however, is MUCH LESS IMPORTANT that (1)
* above. A single miss can result in many RPC's that dwarf
* the time required to do all kinds of work (like activate
* vnodes).
*
* Partitioned name cache:
* The name cache is partitioned into 3 parts (just like Gaul for
* aficionados of Julius Caesar). The purpose of this partitioning
* is to minimize the effects of large working sets on overall
* cache miss rates. The three partitions are:
* 1) Name lookup that results in a dir object
* 2) Name lookup that results in a file object
* 3) Name lookup that results in ENOENT returns
*
* Here is how these work. (1) is the most important to be large
* enough. Even if the working set is 2000 files and won't fit
* in the cache, most lookups will be of the form /foo/bar/baz/file0000
* to /foo/bar/baz/file1999. Even with a 0% hit rate on file0000
* to file1999 (because the application goes through too many filenames
* in succession such that file0000 has been replaced in the cache
* by the time the app reaches file1999 and goes back to lookup file0000),
* you still have a 75% hit-rate in the name cache because
* the lookups of foo, bar, and baz weren't flushed from the separate
* partition of dir lookups. This effect depends on the normal
* use of a modest number of dirs in most builds.
*
* The regular file cache is also very important, and ideally is
* normally larger than the working set of files used in a single
* edit-compile-debug cycle, so that there are high hit rates on
* all translations.
*
* The ENOENT partition holds name translations that return name-not-found.
* Particularly in software development, the tools (compilers/linkers)
* tend to use search rules, and frequently lookup up names for which
* the answer is name-not-found. Because search rules can get long
* and thrash (working set larger than cache), this is also made
* a separate partition. For best performance, this cache
* should be very large (number of dirs searched in vain * number
* of files looked for.) As an example: for a 3-level search rule
* for header files, for which the header file is usually in the
* last dir, there will be twice as much demand for ENOENT
* entries as for found-file entries (first 2 dirs in search rule).
*
* Cache tags:
* Entries in the name cache are invalidated on lots of
* conditions. As a result there are a pile of cache tags
* that are checked on entries to validate entries:
* (to be added later)
*
* Case-insensitive entries
* At present (05/01/95) case-insensitive entries are supported
* only for ENOENT translations. (That is because I haven't
* resolved all the invalidation issues for other entries, and
* I can get enough of a performance improvement from ENOENT
* entries).
*
* The basic rules that have been added to the cache for
* case-insensitive entries are:
* a) Case-insensitive entries are actually a flag on
* a case-sensitive entry that indicates this is the
* correct item for a case-insensitive search. This
* saves storage for the case-correct name which must
* be returned in the pathname struct for the caller.
* b) Case-insensitive entries in the ENOENT cache are
* always stored downcased. This prevents the addition
* of multiple name-cache entries for a single translation
* (i.e. for case-insensitive lookups of "foo.DLL" and
* "FOO.dll" only a "foo.dll" name cache entry is used,
* using 1 entry instead of two. This improves cache
* hit rates for a given cache size.)
* As a result of (a) and (b) there should be only 1 valid
* entry for a case-insensitive translation for any given
* (dvp, nm) in the cache, and for a name-found translation,
* the entry is the same entry as the case-sensitive entry,
* so there is no extra cache demand.
*
* c) The name hash is not sensitive to case.
* All case-insensitive synonyms will be on a single hash
* chain in the cache, making invalidation simpler.
* d) Whenever an entry (case-sensitive or case-insensitive)
* is actually added or removed from the cache (explicitly), then
* all case-insensitive synonyms for this translation must be
* invalidated. The reason for this is that adding or
* removing a case-sensitive item may change the results
* of a case-insensitive lookup (add name to dir or remove
* a file from a dir).
*
* Locking:
* mvfs_dnc_rwlock: write lock (exclusive access) for cache
* invalidation functions, which need to be guaranteed that a single
* pass through the cache can find all pertinent entries; read lock
* (shared access) for operations which add/remove entries. This
* lock is not used for lookup operations
* hash chain lock(s): On NT, a mutex; on UNIX, a pool of SPLOCK_T locks.
* NT cannot use a spin lock here because some of the case-insensitive
* comparison operations get into trouble with wrong IRQ level when
* under a spin lock. On UNIX, the ratio of locks to hash chains is
* platform-specific, refer to each mdep header file.
* This lock is used to protect the sanity of each hash chain. Lookup
* operations will use this lock once the appropriate chain is
* identified. Operations which add or remove entries also use this
* when modifying the chain. Protocol is to acquire the mvfs_dnc_rwlock
* first if it is required.
* mvfs_dnc_lru_lock: a SPLOCK_T, used to protect the sanity of the
* cache's LRU list and each entry's in_trans bit.
* Protocol is to acquire the hash chain lock, if any, before acquiring
* the LRU SPLOCK.
*
* Statistics:
* This cache has a lot of statistics. The important stats
* are the miss-rate and the causes of the miss. Ideally,
* every miss should have a statistic, so that one can tell
* easily why misses are happening to see what can be done
* to minimize them. Every miss is a performance problem...
* so the goal of statistics is to quickly tell why misses
* are happening.
*
* With the changes of 05/01/95, I added the ability to track
* misses due to no entry in the cache from misses due to
* invalidations in the cache (previously, invalidate would
* purge the entry, so a miss would then look like an ordinary
* miss). The reason this is important, is because an ordinary
* miss might be helped by a larger cache, but an invalidate
* caused miss will never be helped by a larger cache. This should
* also help to tell developers quickly if folks in application
* space are getting a little invalidation happy to the detriment
* of product performance.
*/
/*
* Internal routine prototypes
*/
STATIC struct mfs_dncent *
mfs_dncfind(
mfs_fid_t *dvfidp,
VFS_T *dvfsp,
VNODE_T *vw,
char *nm,
int len,
tbs_boolean_t case_insensitive,
int hash,
CRED_T *cred
);
STATIC void
mvfs_dncadd_subr(
VNODE_T *dvp,
VNODE_T *vw,
u_int dnc_flags,
char *nm,
int len,
mfs_fid_t *vfidp,
VTYPE_T type,
struct timeval *evtp,
int hash,
CRED_T *cred
);
STATIC void
mfs_dnc_inval_case_synonyms(
mfs_fid_t *dvfidp,
VNODE_T *vw,
char *nm,
int len,
int hash
);
STATIC int
mvfs_dnclookup_subr(
struct mfs_dncent *dnp,
VNODE_T *vw,
VNODE_T **vpp,
CALL_DATA_T *cd
);
STATIC int
mvfs_dncflush_subr(VFS_T *vfsp);
STATIC void
mfs_dncbhadd(
mfs_dncent_t *dnp,
view_bhandle_t *bhp
);
STATIC void
mfs_dncbhset(
mfs_dncent_t *dnp,
view_bhandle_t *bhp
);
STATIC int
mfs_dncbhcheck(
mfs_dncent_t *dnp,
mvfs_thread_t *mth
);
STATIC int
mfs_dnc_nullbhcheck(
mfs_dncent_t *dnp,
mvfs_thread_t *mth
);
STATIC void
mfs_dncrele(struct mfs_dncent *dnp);
STATIC u_long
mfs_namehash(
char *nm,
int *lenp
);
STATIC int
mvfs_dnclist_init(void);
STATIC int
mvfs_find_dnchashsize(int maxentry);
/*
* Hash for name cache entries. All names are hashed in 1 table.
* Hash size should be a prime number, and the nmhash value should
* come from the "mfs_namehash()" routine (which also returns length of
* the string).
*/
#define MFS_DNCHASHMAX 18181 /* max hash size */
#define MFS_DNCHASHMIN 509 /* min hash size */
#define MFS_DNC_AVECHAIN 10 /*average chain length for each hash slot*/
#define MFS_DNCHASH(dvp, nmhash, len, _ncdp) \
((nmhash + len + \
VTOM(dvp)->mn_hdr.fid.mf_dbid + \
VTOM(dvp)->mn_hdr.fid.mf_gen) % _ncdp->mvfs_dnchashsize)
#define MVFS_RVCHASH(vw, vfsp, _ncdp) \
(((mfs_uuid_to_hash32(&(VTOM(vw)->mn_view.svr.uuid)) + \
mfs_uuid_to_hash32(&(VFS_TO_MMI(vfsp)->mmi_svr.uuid)))) \
% _ncdp->mvfs_dnchashsize)
mvfs_dnlc_data_t mvfs_dnlc_data_var;
#define NC_SPLOCK_LRU(_dp,_s) SPLOCK(*(((mfs_dnclru_t *)((_dp)->lruhead))->lruspl),(_s))
#define NC_SPUNLOCK_LRU(_dp,_s) SPUNLOCK(*(((mfs_dnclru_t *)((_dp)->lruhead))->lruspl),(_s))
/*
* Macros for list management. All macros insert "after" the
* element. These macros make a copy of the "element" to insert after
* to allow that macro arg to be a ptr in the linked list that is
* updated by the macro.
*/
#undef NC_INSLRU_LOCKED
#undef NC_RMLRU_LOCKED
#undef NC_INSHASH_LOCKED
#undef NC_RMHASH_LOCKED
#define NC_INSLRU_LOCKED(lrup, dp) { \
register mfs_dncent_t *LRUP = (mfs_dncent_t *)lrup; \
DEBUG_ASSERT((dp)->lrunext == NULL); \
DEBUG_ASSERT((dp)->lruprev == NULL); \
(dp)->lrunext = (LRUP)->lrunext; \
(dp)->lruprev = (LRUP); \
(LRUP)->lrunext->lruprev = (dp); \
(LRUP)->lrunext = (dp); \
}
#define NC_RMLRU_LOCKED(dp) { \
DEBUG_ASSERT((dp)->lrunext); \
DEBUG_ASSERT((dp)->lruprev); \
(dp)->lrunext->lruprev = (dp)->lruprev; \
(dp)->lruprev->lrunext = (dp)->lrunext; \
(dp)->lrunext = (dp)->lruprev = NULL; \
}
#define NC_INSHASH_LOCKED(hp, dp) { \
register mfs_dncent_t *HP = (mfs_dncent_t *)hp; \
DEBUG_ASSERT((dp)->next == NULL); \
DEBUG_ASSERT((dp)->prev == NULL); \
(dp)->next = (HP)->next; \
(dp)->prev = (HP); \
(HP)->next->prev = (dp); \
(HP)->next = (dp); \
}
#define NC_RMHASH_LOCKED(dp) { \
DEBUG_ASSERT((dp)->next); \
DEBUG_ASSERT((dp)->prev); \
(dp)->next->prev = (dp)->prev; \
(dp)->prev->next = (dp)->next; \
(dp)->next = (dp)->prev = NULL; \
(dp)->dnc_hash = -1; \
}
#define SET_IN_TRANS(dp) { \
DEBUG_ASSERT((dp)->in_trans == 0); \
(dp)->in_trans = 1; \
}
#define CLR_IN_TRANS(dp,_ncdp) { \
DEBUG_ASSERT((dp)->in_trans == 1); \
(dp)->in_trans = 0; \
} \
if (_ncdp->mvfs_old_dnc) { MDB_XLOG((MDB_DNC_REALLOC,"clrintrans %"KS_FMT_PTR_T, dp));} \
if (_ncdp->mvfs_old_dnc) /* caller provides {} */
#define DROP_REF_IN_TRANS(_ncdp) \
if (--(_ncdp->mvfs_dnc_nintransit) == 0) { \
struct mfs_dncent *odnc = _ncdp->mvfs_old_dnc; \
int odncmax = _ncdp->mvfs_old_dncmax; \
_ncdp->mvfs_old_dnc = 0; \
_ncdp->mvfs_old_dncmax = 0; \
MDB_XLOG((MDB_DNC_REALLOC,"freeing dnc %d @ %"KS_FMT_PTR_T"\n", \
odncmax, odnc)); \
KMEM_FREE(odnc, odncmax*sizeof(struct mfs_dncent)); \
}
#define UNUSED(dp) ((dp)->dvw == NULL && \
MVFS_FLAGOFF((dp)->flags,MVFS_DNC_RVC_ENT))
#define DNC_BUMPVW(vw, stat) \
if (vw) { \
BUMP_PVSTAT(vw, dncstat.stat); \
} \
BUMPSTAT(mfs_dncstat.stat);
/*
* Same as above, but handles two stats at a time while under lock.
* Care should be taken not to dereference the per-view stat pointer
* which could be in paged memory after taking the pvstatlock. For
* detailed information on this, check the comment above the pvstat
* declaration.
*/
#define DNC_BUMPVW_2(vw, stat1, stat2) { \
if (vw) { \
SPL_T _ss; \
struct mvfs_pvstat *pvp = VTOM(vw)->mn_view.pvstat; \
MVFS_PVSTATLOCK_LOCK(_ss, pvp); \
BUMP_PVSTAT_LOCKED(pvp, dncstat.stat1); \
BUMP_PVSTAT_LOCKED(pvp, dncstat.stat2); \
MVFS_PVSTATLOCK_UNLOCK(_ss, pvp); \
} \
BUMPSTAT(mfs_dncstat.stat1); \
BUMPSTAT(mfs_dncstat.stat2); \
}
/* find a proper hash size, which should be a prime number,
* and satisfy ave chain lenth */
STATIC int
mvfs_find_dnchashsize(int maxentry)
{
int num;
int sqnum, i, isprime;
num = maxentry/MFS_DNC_AVECHAIN;
if (num <= MFS_DNCHASHMIN)
return MFS_DNCHASHMIN;
if (num >= MFS_DNCHASHMAX)
return MFS_DNCHASHMAX;
while (1) {
isprime = 1;
sqnum = num/2;
for (i = 2; i <= sqnum; i++)
if (num%i == 0) {
isprime = 0;
break;
}
if (isprime) return num;
num++;
}
}
/*
* Routine to set up headers for LRU lists and hash chains, and
* put entries on LRU. May be called by setcaches as well as at
* initialization time, and so assumes that the global cache lock
* is held exclusively.
*/
STATIC int
mvfs_dnclist_init()
{
mvfs_common_data_t *mcdp = MDKI_COMMON_GET_DATAP();
register mvfs_dnlc_data_t *ncdp = MDKI_DNLC_GET_DATAP();
register int i;
ncdp->mfs_dncmax = mcdp->mvfs_dncdirmax + mcdp->mvfs_dncregmax + mcdp->mvfs_dncnoentmax;
ncdp->mfs_dnc_enoent_start = mcdp->mvfs_dncdirmax + mcdp->mvfs_dncregmax;
/* The following code dynamically chooses dnc hash table size (prime
* number) based on the max number of entries (mfs_dncmax). So, the average
* chain length will be kept in a proper range for good performance.
*/
if (ncdp->mfs_dnchash != NULL) {
/* free old hash table mem, if it's dynamically allocated */
KMEM_FREE(ncdp->mfs_dnchash, (ncdp->mvfs_dnchashsize)*sizeof(mfs_dnchash_slot_t));
ncdp->mfs_dnchash = NULL;
}
ncdp->mvfs_dnchashsize = mvfs_find_dnchashsize(ncdp->mfs_dncmax);
ncdp->mfs_dnchash = (mfs_dnchash_slot_t *)
KMEM_ALLOC((ncdp->mvfs_dnchashsize)*sizeof(mfs_dnchash_slot_t), KM_SLEEP);
if (ncdp->mfs_dnchash == NULL) {
mvfs_log(MFS_LOG_WARN,
"Failed to allocate %d bytes for DNC Hash, trying minimum (%d) instead.\n",
ncdp->mvfs_dnchashsize*sizeof(mfs_dnchash_slot_t),
MFS_DNCHASHMIN*sizeof(mfs_dnchash_slot_t));
ncdp->mvfs_dnchashsize = MFS_DNCHASHMIN;
ncdp->mfs_dnchash = (mfs_dnchash_slot_t *)
KMEM_ALLOC((ncdp->mvfs_dnchashsize)*sizeof(mfs_dnchash_slot_t), KM_SLEEP);
if (ncdp->mfs_dnchash == NULL) {
mvfs_log(MFS_LOG_ERR,
"Failed to allocate minimum memory for DNC Hash.\n");
return(ENOMEM);
}
}
NC_HASH_LOCK_INIT(ncdp);
/* Initialize list hdrs */
for (i=0; i < ncdp->mvfs_dnchashsize; i++) {
ncdp->mfs_dnchash[i].next = ncdp->mfs_dnchash[i].prev =
(mfs_dncent_t *)&(ncdp->mfs_dnchash[i]);
}
ncdp->mfs_dncdirlru.lrunext = ncdp->mfs_dncdirlru.lruprev =
(mfs_dncent_t *)&(ncdp->mfs_dncdirlru);
ncdp->mfs_dncdirlru.lruspl = &(ncdp->mvfs_dnc_dirlru_lock);
ncdp->mfs_dncreglru.lrunext = ncdp->mfs_dncreglru.lruprev =
(mfs_dncent_t *)&(ncdp->mfs_dncreglru);
ncdp->mfs_dncreglru.lruspl = &(ncdp->mvfs_dnc_reglru_lock);
ncdp->mfs_dncnoentlru.lrunext = ncdp->mfs_dncnoentlru.lruprev =
(mfs_dncent_t *)&(ncdp->mfs_dncnoentlru);
ncdp->mfs_dncnoentlru.lruspl = &(ncdp->mvfs_dnc_noentlru_lock);
/* Add the entries to the "free" list */
for (i=0; i < ncdp->mfs_dncmax; i++) {
if (i < mcdp->mvfs_dncdirmax) {
ncdp->mfs_dnc[i].lruhead = (mfs_dncent_t *)&(ncdp->mfs_dncdirlru);
NC_INSLRU_LOCKED(&(ncdp->mfs_dncdirlru), &(ncdp->mfs_dnc[i]));
} else if (i < (ncdp->mfs_dnc_enoent_start)) {
ncdp->mfs_dnc[i].lruhead = (mfs_dncent_t *)&(ncdp->mfs_dncreglru);
NC_INSLRU_LOCKED(&(ncdp->mfs_dncreglru), &(ncdp->mfs_dnc[i]));
} else {
ncdp->mfs_dnc[i].lruhead = (mfs_dncent_t *)&(ncdp->mfs_dncnoentlru);
NC_INSLRU_LOCKED(&(ncdp->mfs_dncnoentlru), &(ncdp->mfs_dnc[i]));
}
ncdp->mfs_dnc[i].nm_p = &(ncdp->mfs_dnc[i]).nm_inline[0];
ncdp->mfs_dnc[i].dnc_hash = -1; /* not on a hash chain yet */
}
return(0);
}
#define DIRMAX_FORMULA(scale) (200 * ((scale)+1))
#define REGMAX_FORMULA(scale) (800 * ((scale)+1))
#define NOENTMAX_FORMULA(scale) (800 * ((scale)+1))
/*
* Initialize the name lookup cache.
* Note that this function allocates memory for the dnc with the KM_SLEEP
* flag set. This means that, although there is code here to back off the
* size of the cache in case memory is not available, it will almost never
* be executed. In most cases this should not be a problem as we should
* already have our caches set to a reasonable size. The setcache code
* does use the KM_NOSLEEP flag so that an admin playing with the cache
* sizes will not cause a hang by oversubscribing memory.
*/
int
mvfs_dncinit(mvfs_cache_sizes_t *mma_sizes)
{
mvfs_common_data_t *mcdp = MDKI_COMMON_GET_DATAP();
register mvfs_dnlc_data_t *ncdp;
struct mfs_dncent *dnp;
SPL_T srw;
int err = 0;
MDKI_DNLC_ALLOC_DATA();
ncdp = MDKI_DNLC_GET_DATAP();
/* Default values for some variables. May not be strictly necessary,
* but used to be defaulted at declaration, which has been shuffled due
* to rearrangement of data definitions for virtualization support.
* ncdp (name cache data ptr) points to correct data area to use and
* all other data is within that structure.
*/
ncdp->mvfs_dnchashsize = MFS_DNCHASHMIN;
ncdp->mfs_dnchash = NULL;
ncdp->mfs_dncmax = 0; /* Total DNC ents - filled in on init */
ncdp->mfs_dnc_enoent_start = 0;
ncdp->mvfs_old_dnc = 0;
ncdp->mvfs_dnc_initialzed = FALSE;
ncdp->mvfs_dnc_noent_other = 0;
/* Initialize the global cache lock */
MVFS_RW_LOCK_INIT(&(ncdp->mvfs_dnc_rwlock), "mvfs_dnlc_lock");
/* Initialze the locks for LRUs */
INITSPLOCK(ncdp->mvfs_dnc_dirlru_lock, "mvfs_dirdnc_lru");
INITSPLOCK(ncdp->mvfs_dnc_reglru_lock, "mvfs_regdnc_lru");
INITSPLOCK(ncdp->mvfs_dnc_noentlru_lock, "mvfs_noentdnc_lru");
/*
* Set largeinit values
* Now any non-zero specific value overrides largeinit default
*/
mcdp->mvfs_init_sizes.size[MVFS_SETCACHE_DNCDIRMAX] = mcdp->mvfs_dncdirmax;
mcdp->mvfs_init_sizes.size[MVFS_SETCACHE_DNCREGMAX] = mcdp->mvfs_dncregmax;
mcdp->mvfs_init_sizes.size[MVFS_SETCACHE_DNCNOENTMAX] = mcdp->mvfs_dncnoentmax;
MVFS_SIZE_DEFLOAD(mcdp->mvfs_dncdirmax, mma_sizes, DNCDIRMAX, 0);
MVFS_SIZE_DEFLOAD(mcdp->mvfs_dncregmax, mma_sizes, DNCREGMAX, 0);
MVFS_SIZE_DEFLOAD(mcdp->mvfs_dncnoentmax, mma_sizes, DNCNOENTMAX, 0);
retry:
if (mcdp->mvfs_dncdirmax == 0) mcdp->mvfs_dncdirmax = DIRMAX_FORMULA(mcdp->mvfs_largeinit);
if (mcdp->mvfs_dncregmax == 0) mcdp->mvfs_dncregmax = REGMAX_FORMULA(mcdp->mvfs_largeinit);
if (mcdp->mvfs_dncnoentmax == 0)
mcdp->mvfs_dncnoentmax = NOENTMAX_FORMULA(mcdp->mvfs_largeinit);
MVFS_MDEP_DNC_CAP();
/* Allocate and zero the name cache */
ncdp->mfs_dncmax = mcdp->mvfs_dncdirmax + mcdp->mvfs_dncregmax + mcdp->mvfs_dncnoentmax;
if (ncdp->mfs_dncmax == 0) return 0; /* No name cache wanted */
ncdp->mfs_dnc = (struct mfs_dncent *)
KMEM_ALLOC(ncdp->mfs_dncmax*sizeof(struct mfs_dncent), KM_SLEEP);
/*
* If the memory allocation fails try lower large init values and
* if even 0 fails return with no cache enabled.
*/
if (ncdp->mfs_dnc == NULL) {
if (mcdp->mvfs_largeinit > 1) {
mvfs_log(MFS_LOG_ERR, "Failed to allocate %d bytes for DNC Cache lowering largeinit from %d to 1\n",
(ncdp->mfs_dncmax)*sizeof(struct mfs_dncent), mcdp->mvfs_largeinit);
mcdp->mvfs_largeinit = 1;
mcdp->mvfs_dncdirmax = mcdp->mvfs_dncregmax = mcdp->mvfs_dncnoentmax = 0;
goto retry;
} else if (mcdp->mvfs_largeinit) {
mvfs_log(MFS_LOG_WARN, "Failed to allocate %d bytes for DNC Cache lowering largeinit from %d to 0\n",
(ncdp->mfs_dncmax)*sizeof(struct mfs_dncent), mcdp->mvfs_largeinit, 0);
mcdp->mvfs_largeinit = 0;
mcdp->mvfs_dncdirmax = mcdp->mvfs_dncregmax = mcdp->mvfs_dncnoentmax = 0;
goto retry;
} else {
mvfs_log(MFS_LOG_ERR, "Failed to allocate %d bytes for DNC Cache with largeinit of %d\n",
(ncdp->mfs_dncmax)*sizeof(struct mfs_dncent), mcdp->mvfs_largeinit);
ncdp->mfs_dncmax = 0;
/* bail out */
mcdp->mvfs_dncdirmax = mcdp->mvfs_init_sizes.size[MVFS_SETCACHE_DNCDIRMAX];
mcdp->mvfs_dncregmax = mcdp->mvfs_init_sizes.size[MVFS_SETCACHE_DNCREGMAX];
mcdp->mvfs_dncnoentmax = mcdp->mvfs_init_sizes.size[MVFS_SETCACHE_DNCNOENTMAX];
err = ENOMEM;
goto nclockfree;
}
}
BZERO(ncdp->mfs_dnc, (ncdp->mfs_dncmax)*sizeof(struct mfs_dncent));
MVFS_RW_WRITE_LOCK(&(ncdp->mvfs_dnc_rwlock), srw);
if ((err = mvfs_dnclist_init()) == 0) {
ncdp->mvfs_dnc_initialzed = TRUE;
MVFS_RW_WRITE_UNLOCK(&(ncdp->mvfs_dnc_rwlock), srw);
return 0;
}
/* Not successful in initializing DNLC, clean up anything that had
* been allocated.
*/
ncdp->mvfs_dnc_initialzed = FALSE;
MVFS_RW_WRITE_UNLOCK(&(ncdp->mvfs_dnc_rwlock), srw);
/* XXXX check this clean up code */
KMEM_FREE(ncdp->mfs_dnc, (ncdp->mfs_dncmax)*sizeof(struct mfs_dncent));
nclockfree:
NC_HASH_LOCK_FREE(&(ncdp->mvfs_dnc_hash_lock));
FREESPLOCK(ncdp->mvfs_dnc_dirlru_lock);
FREESPLOCK(ncdp->mvfs_dnc_reglru_lock);
FREESPLOCK(ncdp->mvfs_dnc_noentlru_lock);
MVFS_RW_LOCK_DESTROY(&(ncdp->mvfs_dnc_rwlock));
return(err);
}
int
mvfs_dnc_setcaches(
mvfs_cache_sizes_t *szp
)
{
register mvfs_dnlc_data_t *ncdp = MDKI_DNLC_GET_DATAP();
mvfs_common_data_t *mcdp = MDKI_COMMON_GET_DATAP();
struct mfs_dncent *new_mvfs_dnc, *free_mvfs_dnc = 0;
int new_mvfs_dncmax, rval, free_mvfs_dncmax = 0 /* shut up GCC */;
SPL_T srw;
NC_HASH_LOCK_T free_hash_locks;
if ((!MVFS_SIZE_VALID(szp, DNCDIRMAX) ||
szp->size[MVFS_SETCACHE_DNCDIRMAX] == mcdp->mvfs_dncdirmax) &&
(!MVFS_SIZE_VALID(szp, DNCREGMAX) ||
szp->size[MVFS_SETCACHE_DNCREGMAX] == mcdp->mvfs_dncregmax) &&
(!MVFS_SIZE_VALID(szp, DNCNOENTMAX) ||
szp->size[MVFS_SETCACHE_DNCNOENTMAX] == mcdp->mvfs_dncnoentmax))
{
/* Nothing needs changing */
return 0;
}
new_mvfs_dncmax = MVFS_SIZE_VALID(szp, DNCDIRMAX) ?
szp->size[MVFS_SETCACHE_DNCDIRMAX] : mcdp->mvfs_dncdirmax;
new_mvfs_dncmax += MVFS_SIZE_VALID(szp, DNCREGMAX) ?
szp->size[MVFS_SETCACHE_DNCREGMAX] : mcdp->mvfs_dncregmax;
new_mvfs_dncmax += MVFS_SIZE_VALID(szp, DNCNOENTMAX) ?
szp->size[MVFS_SETCACHE_DNCNOENTMAX] : mcdp->mvfs_dncnoentmax;
/* Note that this has changed to use the KM_NOSLEEP flag to prevent
* possible hangs or panics if the user tries to overallocate memory.
* Now if the user sets the cache size too large, they can take what
* actions they need to free up memory. The initialization code will
* still sleep since it expects the system was properly tuned.
*/
new_mvfs_dnc = (struct mfs_dncent *)
KMEM_ALLOC(new_mvfs_dncmax*sizeof(struct mfs_dncent), KM_NOSLEEP);
if (new_mvfs_dnc == NULL)
return ENOMEM;
BZERO(new_mvfs_dnc, new_mvfs_dncmax*sizeof(struct mfs_dncent));
MDB_XLOG((MDB_DNC_REALLOC, "new dnc %d @ %"KS_FMT_PTR_T"\n",
new_mvfs_dncmax, new_mvfs_dnc));
/* To avoid the necessity of returning EBUSY here in some cases,
* we'll need instead to use a reader/writer lock to interlock
* with the mvfs_dncadd_subr() path so that nothing else could get
* added to the existing dnc table. For now, we'll require that
* cache size adjustments be handled on an idle ClearCase system
* (no new lookups in progress--processes can still have current
* directories or open files within the MVFS.)
*/
rval = mvfs_dncflush_subr(NULL);
if (rval == -1 || ncdp->mvfs_old_dnc != 0) {
KMEM_FREE(new_mvfs_dnc, new_mvfs_dncmax*sizeof(struct mfs_dncent));
MDB_XLOG((MDB_DNC_REALLOC, "EBUSY\n"));
return EBUSY;
}
MVFS_RW_WRITE_LOCK(&(ncdp->mvfs_dnc_rwlock), srw);
/* OK, the only things left in the cache are those marked as in
* transit. The processes manipulating in-transit entries clean
* up when clearing the in-transit bit if there is a pending DNC
* switch.
*/
ncdp->mvfs_old_dnc = ncdp->mfs_dnc;
ncdp->mfs_dnc = new_mvfs_dnc;
ncdp->mvfs_dnc_nintransit = rval;
ncdp->mvfs_old_dncmax = ncdp->mfs_dncmax;
if (ncdp->mvfs_dnc_nintransit == 0) {
free_mvfs_dnc = ncdp->mvfs_old_dnc;
free_mvfs_dncmax = ncdp->mvfs_old_dncmax;
ncdp->mvfs_old_dnc = 0;
}
free_hash_locks = ncdp->mvfs_dnc_hash_lock;
MVFS_SIZE_RUNTIME_SET(mcdp->mvfs_dncdirmax, szp, DNCDIRMAX);
MVFS_SIZE_RUNTIME_SET(mcdp->mvfs_dncregmax, szp, DNCREGMAX);
MVFS_SIZE_RUNTIME_SET(mcdp->mvfs_dncnoentmax, szp, DNCNOENTMAX);
mvfs_dnclist_init();
MVFS_RW_WRITE_UNLOCK(&(ncdp->mvfs_dnc_rwlock), srw);
if (free_mvfs_dnc) {
MDB_XLOG((MDB_DNC_REALLOC,"freeing dnc %d @ %"KS_FMT_PTR_T"\n",
free_mvfs_dncmax, free_mvfs_dnc));
KMEM_FREE(free_mvfs_dnc, free_mvfs_dncmax*sizeof(struct mfs_dncent));
}
NC_HASH_LOCK_FREE(&free_hash_locks);
if (MVFS_SIZE_PRESENT(szp, DNCDIRMAX))
szp->size[MVFS_SETCACHE_DNCDIRMAX] = mcdp->mvfs_dncdirmax;
if (MVFS_SIZE_PRESENT(szp, DNCREGMAX))
szp->size[MVFS_SETCACHE_DNCREGMAX] = mcdp->mvfs_dncregmax;
if (MVFS_SIZE_PRESENT(szp, DNCNOENTMAX))
szp->size[MVFS_SETCACHE_DNCNOENTMAX] = mcdp->mvfs_dncnoentmax;
return 0;
}
int
mvfs_dnc_getcaches(
mvfs_cache_sizes_t *szp
)
{
mvfs_dnlc_data_t *ncdp = MDKI_DNLC_GET_DATAP();
mvfs_common_data_t *mcdp = MDKI_COMMON_GET_DATAP();
szp->size[MVFS_SETCACHE_DNCDIRMAX] = mcdp->mvfs_dncdirmax;
szp->size[MVFS_SETCACHE_DNCREGMAX] = mcdp->mvfs_dncregmax;
szp->size[MVFS_SETCACHE_DNCNOENTMAX] = mcdp->mvfs_dncnoentmax;
szp->size[MVFS_SETCACHE_DNCHASHTAB_SZ] = ncdp->mvfs_dnchashsize;
return 0;
}
int
mvfs_dnc_compute_caches(
ks_int32_t scale_factor,
mvfs_cache_sizes_t *szp
)
{
#define FILL_IN(cacheval, formula) \
if ((szp->mask & MVFS_CACHEBIT(cacheval)) == 0) { \
szp->size[MVFS_SETCACHE_ ## cacheval] = formula; \
szp->mask |= MVFS_CACHEBIT(cacheval); \
}
FILL_IN(DNCDIRMAX, DIRMAX_FORMULA(scale_factor));
FILL_IN(DNCREGMAX, REGMAX_FORMULA(scale_factor));
FILL_IN(DNCNOENTMAX, NOENTMAX_FORMULA(scale_factor));
FILL_IN(DNCHASHTAB_SZ,
mvfs_find_dnchashsize(szp->size[MVFS_SETCACHE_DNCDIRMAX] +
szp->size[MVFS_SETCACHE_DNCREGMAX] +
szp->size[MVFS_SETCACHE_DNCNOENTMAX]));
#undef FILL_IN
return 0;
}
/*
* MVFS_DNCFREE() - free dnc resources
*/
void
mvfs_dncfree()
{
mvfs_dnlc_data_t *ncdp = MDKI_DNLC_GET_DATAP();
mvfs_common_data_t *mcdp = MDKI_COMMON_GET_DATAP();
ASSERT(ncdp->mvfs_old_dnc == NULL);
if (ncdp->mfs_dnc != NULL) { /* initialized */
/* all entries' long names should have been flushed when the last
MVFS mount was unmounted. */
KMEM_FREE(ncdp->mfs_dnc, (ncdp->mfs_dncmax)*sizeof(struct mfs_dncent));
NC_HASH_LOCK_FREE(&(ncdp->mvfs_dnc_hash_lock));
FREESPLOCK(ncdp->mvfs_dnc_dirlru_lock);
FREESPLOCK(ncdp->mvfs_dnc_reglru_lock);
FREESPLOCK(ncdp->mvfs_dnc_noentlru_lock);
MVFS_RW_LOCK_DESTROY(&(ncdp->mvfs_dnc_rwlock));
}
if (ncdp->mfs_dnchash != NULL) {
/* free hash table mem, if it's dynamically allocated */
KMEM_FREE(ncdp->mfs_dnchash, (ncdp->mvfs_dnchashsize)*sizeof(mfs_dnchash_slot_t));
ncdp->mfs_dnchash = NULL;
}
mcdp->mvfs_dncdirmax = mcdp->mvfs_init_sizes.size[MVFS_SETCACHE_DNCDIRMAX];
mcdp->mvfs_dncregmax = mcdp->mvfs_init_sizes.size[MVFS_SETCACHE_DNCREGMAX];
mcdp->mvfs_dncnoentmax = mcdp->mvfs_init_sizes.size[MVFS_SETCACHE_DNCNOENTMAX];
ncdp->mfs_dncmax = 0;
MDKI_DNLC_FREE_DATA();
return;
}
/*
* Read next NC entry for debug/testing ioctl to scan name cache
*/
int
mfs_dnc_getent(
struct mfs_ioncent *ncp
)
{
mvfs_dnlc_data_t *ncdp = MDKI_DNLC_GET_DATAP();
int i;
int got_one = FALSE;
int error = 0;
mfs_dncent_t *dncentp;
ncp->eocache = 1; /* Assume at end */
if (ncdp->mfs_dnc == NULL) return(0);
if ((dncentp = KMEM_ALLOC(sizeof(*dncentp), KM_SLEEP)) == NULL) {
return(0); /* Caller doesn't care. */
}
for (i=ncp->offset; i < ncdp->mfs_dncmax; i++) {
if (ncdp->mfs_dnc[i].in_trans) continue;
if (UNUSED(&(ncdp->mfs_dnc[i]))) continue;
*dncentp = ncdp->mfs_dnc[i];
ncp->offset = i+1;
got_one = TRUE;
break;
}
/* See if we got one */
if (!got_one) goto cleanup; /* Defaults to EOF */
ncp->eocache = 0; /* Not EOF */
ncp->flags = 0;
if (dncentp->nullbh) ncp->flags |= MVFS_IONC_NULLBH;
if (MFS_FIDNULL(dncentp->vfid)) ncp->flags |= MVFS_IONC_ENOENT;
/*
* Parse out the new DNC flags 05/06/95
* This is a pain that we can't just pass the flags out
* to user-space, but for compatibility, I didn't want
* to change the user-space structure, so I just added
* the new flags as new flags in the existing ioctl.
* As a result, I have to parse out each flag to its corresponding
* ioctl flag individually.
*/
if (dncentp->invalid) ncp->flags |= MVFS_IONC_INVALID;
if (MVFS_FLAGON(dncentp->flags, MFS_DNC_BHINVARIANT))
ncp->flags |= MVFS_IONC_BHINVARIANT;
if (MVFS_FLAGON(dncentp->flags, MFS_DNC_NOTINDIR))
ncp->flags |= MVFS_IONC_NOTINDIR;
if (MVFS_FLAGON(dncentp->flags, MFS_DNC_CASE_INSENSITIVE))
ncp->flags |= MVFS_IONC_CASE_INSENSITIVE;
if (MVFS_FLAGON(dncentp->flags, MVFS_DNC_RVC_ENT))
ncp->flags |= MVFS_IONC_RVC;
/* Copy out the time added to the cache */
ncp->addtime = dncentp->addtime;
error = mfs_copyout_viewtag(FALSE, ncp->dvw, dncentp->dvw);
if (error) goto cleanup;
ASSERT(dncentp->vfsp);
ASSERT(VFS_TO_MMI(dncentp->vfsp));
error = mfs_copyout_strbufpn(ncp->mp, VFS_TO_MMI(dncentp->vfsp)->mmi_mntpath);
if (error) goto cleanup;
ncp->dfid.dbid = dncentp->dfid.mf_dbid;
ncp->dfid.gen = dncentp->dfid.mf_gen;
/*
* FIXME: getdncent - name truncated
* Name may be truncated if too long. This code only
* copies out the name in nm_inline of the dncent, which
* may be truncated if the real leaf-name is longer
* than MFS_DNMAXSHORTNAME
*/
error = mfs_copyout_strbufpn(ncp->nm, dncentp->nm_inline);
if (error) goto cleanup;
/*
* First fill return BH list with -1.-1 and then
* fill up with as many bh's as will fit/have.
*/
for (i=0; i < MVFS_IONCBHMAX; i++) {
ncp->bhlist[i].target_id = 0xffffffff;
ncp->bhlist[i].build_session = 0xffffffff;
}
for (i=0; i < MVFS_IONCBHMAX && i < MFS_DNCBHMAX; i++) {
ncp->bhlist[i] = dncentp->bh[i];
}
error = mfs_copyout_viewtag(FALSE, ncp->vw, dncentp->vvw);
if (error) goto cleanup;
ncp->fid.dbid = dncentp->vfid.mf_dbid;
ncp->fid.gen = dncentp->vfid.mf_gen;
ncp->evtime = dncentp->vevtime;
cleanup:
KMEM_FREE(dncentp, sizeof(*dncentp));
return(error);
}
/*
* Hash and get len of a name
*/
STATIC u_long MVFS_NOINLINE
mfs_namehash(
char *nm,
int *lenp
)
{
int c;
u_long hash = 0;
u_long len = 0;
/*
* To make hash ANSI case-insensitive without worrying about
* multibyte character sets, we just ignore the bit that
* discriminates upper from lower-case ANSI 7-bit characters (0x20)
*/
while((c = *nm++) != '\0') {
len++;
hash = (hash >> 16 & 0xffff) + (hash << 3 & 0x7ffff) + (c & ~0x20);
}
*lenp = len;
return (hash);
}
/*
* MFS_DNCRELE - release resources held by a name cache ent
* The protocol for releasing resources is as follows:
* Under the SPINLOCK:
* a) Unlink from the hash and lru lists
* b) Set the "in_trans" bit using SET_IN_TRANS macro
* c) Release the SPINLOCK
* Call this routine with the (now private) item
* Reacquire the SPINLOCK:
* a) Clear the in_trans bit using CLR_IN_TRANS macro
* b) Return the cache entry to the appropriate lists
*/
STATIC void
mfs_dncrele( register struct mfs_dncent *dnp)
{
ASSERT(dnp->in_trans);
if (dnp->dvw != NULL) VN_RELE(dnp->dvw);
dnp->dvw = NULL;
if (dnp->vvw != NULL) VN_RELE(dnp->vvw);
dnp->vvw = NULL;
if (dnp->cred != NULL) MDKI_CRFREE(dnp->cred);
dnp->cred = NULL;
if (dnp->nm_p != NULL && dnp->nm_p != dnp->nm_inline) STRFREE(dnp->nm_p);
dnp->nm_p = NULL;
dnp->flags = 0;
}
/*
* Add a name cache entry. NULL value for vp indicates
* that this should be a "name not found" entry.
*/
void
mfs_dncadd(
register VNODE_T *dvp,
u_int dnc_flags,
register char *nm,
VNODE_T *vp,
CRED_T *cred
)
{
mvfs_dnlc_data_t *ncdp = MDKI_DNLC_GET_DATAP();