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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
|
/*
* Copyright (c) 2017 Red Hat, Inc. <http://www.redhat.com>
* This file is part of GlusterFS.
*
* This file is licensed to you under your choice of the GNU Lesser
* General Public License, version 3 or any later version (LGPLv3 or
* later), or the GNU General Public License, version 2 (GPLv2), in all
* cases as published by the Free Software Foundation.
*/
#include "nl-cache.h"
#include "timer-wheel.h"
#include "statedump.h"
/* Caching guidelines:
* This xlator serves negative lookup(ENOENT lookups) from the cache,
* there by making create faster.
* What is cached?
* Negative lookup cache is stored for each directory, and has 2 entries:
* - Negative entries: Populated only when lookup/stat returns ENOENT.
* Fuse mostly sends only one lookup before create, hence negative entry
* cache is almost useless. But for SMB access, multiple lookups/stats
* are sent before creating the file. Hence the negative entry cache.
* It can exist even when the positive entry cache is invalid. It also
* has the entries that were deleted from this directory.
* Freed on recieving upcall(with dentry change flag) or on expiring
* timeout of the cache.
*
* - Positive entries: Populated as a part of readdirp, and as a part of
* mkdir followed by creates inside that directory. Lookups and other
* fops do not populate the positive entry (as it can grow long and is
* of no value add)
* Freed on recieving upcall(with dentry change flag) or on expiring
* timeout of the cache.
*
* Data structures to store cache?
* The cache of any directory is stored in the inode_ctx of the directory.
* Negative entries are stored as list of strings.
* Search - O(n)
* Add - O(1)
* Delete - O(n) - as it has to be searched before deleting
* Positive entries are stored as a list, each list node has a pointer
* to the inode of the positive entry or the name of the entry.
* Since the client side inode table already will have inodes for
* positive entries, we just take a ref of that inode and store as
* positive entry cache. In cases like hardlinks and readdirp where
* inode is NULL, we store the names.
* Name Search - O(n)
* Inode Search - O(1) - Actually complexity of inode_find()
* Name/inode Add - O(1)
* Name Delete - O(n)
* Inode Delete - O(1)
*
* Locking order:
*
* TODO:
* - Fill Positive entries on readdir/p, after which in lookup_cbk check if the
* name is in PE and replace it with inode.
* - fini, PARENET_DOWN, disable caching
* - Virtual setxattr to dump the inode_ctx, to ease debugging
* - Handle dht_nuke xattr: clear all cache
* - Special handling for .meta and .trashcan?
*/
int __nlc_inode_ctx_timer_start (xlator_t *this, inode_t *inode, nlc_ctx_t *nlc_ctx);
int __nlc_add_to_lru (xlator_t *this, inode_t *inode, nlc_ctx_t *nlc_ctx);
void nlc_remove_from_lru (xlator_t *this, inode_t *inode);
void __nlc_inode_ctx_timer_delete (xlator_t *this, nlc_ctx_t *nlc_ctx);
gf_boolean_t __nlc_search_ne (nlc_ctx_t *nlc_ctx, const char *name);
void __nlc_free_pe (xlator_t *this, nlc_ctx_t *nlc_ctx, nlc_pe_t *pe);
void __nlc_free_ne (xlator_t *this, nlc_ctx_t *nlc_ctx, nlc_ne_t *ne);
static int32_t
nlc_get_cache_timeout (xlator_t *this)
{
nlc_conf_t *conf = NULL;
conf = this->private;
/* Cache timeout is generally not meant to be changed often,
* once set, hence not within locks */
return conf->cache_timeout;
}
static gf_boolean_t
__nlc_is_cache_valid (xlator_t *this, nlc_ctx_t *nlc_ctx)
{
nlc_conf_t *conf = NULL;
time_t last_val_time;
gf_boolean_t ret = _gf_false;
GF_VALIDATE_OR_GOTO (this->name, nlc_ctx, out);
conf = this->private;
LOCK (&conf->lock);
{
last_val_time = conf->last_child_down;
}
UNLOCK (&conf->lock);
if ((last_val_time <= nlc_ctx->cache_time) &&
(nlc_ctx->cache_time != 0))
ret = _gf_true;
out:
return ret;
}
void
nlc_update_child_down_time (xlator_t *this, time_t *now)
{
nlc_conf_t *conf = NULL;
conf = this->private;
LOCK (&conf->lock);
{
conf->last_child_down = *now;
}
UNLOCK (&conf->lock);
return;
}
void
nlc_disable_cache (xlator_t *this)
{
nlc_conf_t *conf = NULL;
conf = this->private;
LOCK (&conf->lock);
{
conf->disable_cache = _gf_true;
}
UNLOCK (&conf->lock);
return;
}
static int
__nlc_inode_ctx_get (xlator_t *this, inode_t *inode, nlc_ctx_t **nlc_ctx_p,
nlc_pe_t **nlc_pe_p)
{
int ret = 0;
nlc_ctx_t *nlc_ctx = NULL;
nlc_pe_t *nlc_pe = NULL;
uint64_t nlc_ctx_int = 0;
uint64_t nlc_pe_int = 0;
ret = __inode_ctx_get2 (inode, this, &nlc_ctx_int, &nlc_pe_int);
if (ret == 0 && nlc_ctx_p) {
nlc_ctx = (void *) (long) (nlc_ctx_int);
*nlc_ctx_p = nlc_ctx;
}
if (ret == 0 && nlc_pe_p) {
nlc_pe = (void *) (long) (nlc_pe_int);
*nlc_pe_p = nlc_pe;
}
return ret;
}
static int
nlc_inode_ctx_set (xlator_t *this, inode_t *inode, nlc_ctx_t *nlc_ctx,
nlc_pe_t *nlc_pe_p)
{
int ret = -1;
/* The caller may choose to set one of the ctxs, hence check
* if the ctx1/2 is non zero and then send the adress. If we
* blindly send the address of both the ctxs, it may reset the
* ctx the caller had sent NULL(intended as leave untouched) for.*/
LOCK(&inode->lock);
{
ret = __inode_ctx_set2 (inode, this,
nlc_ctx ? (uint64_t *) &nlc_ctx : 0,
nlc_pe_p ? (uint64_t *) &nlc_pe_p : 0);
}
UNLOCK(&inode->lock);
return ret;
}
static void
nlc_inode_ctx_get (xlator_t *this, inode_t *inode, nlc_ctx_t **nlc_ctx_p,
nlc_pe_t **nlc_pe_p)
{
int ret = 0;
LOCK (&inode->lock);
{
ret = __nlc_inode_ctx_get (this, inode, nlc_ctx_p, nlc_pe_p);
if (ret < 0)
gf_msg_debug (this->name, 0, "inode ctx get failed for "
"inode:%p", inode);
}
UNLOCK (&inode->lock);
return;
}
static void
__nlc_inode_clear_entries (xlator_t *this, nlc_ctx_t *nlc_ctx)
{
nlc_pe_t *pe = NULL;
nlc_pe_t *tmp = NULL;
nlc_ne_t *ne = NULL;
nlc_ne_t *tmp1 = NULL;
if (!nlc_ctx)
goto out;
if (IS_PE_VALID (nlc_ctx->state))
list_for_each_entry_safe (pe, tmp, &nlc_ctx->pe, list) {
__nlc_free_pe (this, nlc_ctx, pe);
}
if (IS_NE_VALID (nlc_ctx->state))
list_for_each_entry_safe (ne, tmp1, &nlc_ctx->ne, list) {
__nlc_free_ne (this, nlc_ctx, ne);
}
nlc_ctx->cache_time = 0;
nlc_ctx->state = 0;
GF_ASSERT (nlc_ctx->cache_size == sizeof (*nlc_ctx));
GF_ASSERT (nlc_ctx->refd_inodes == 0);
out:
return;
}
static void
nlc_init_invalid_ctx (xlator_t *this, inode_t *inode, nlc_ctx_t *nlc_ctx)
{
nlc_conf_t *conf = NULL;
int ret = -1;
conf = this->private;
LOCK (&nlc_ctx->lock);
{
if (__nlc_is_cache_valid (this, nlc_ctx))
goto unlock;
/* The cache/nlc_ctx can be invalid for 2 reasons:
* - Because of a child-down/timer expiry, cache is
* invalid but the nlc_ctx is not yet cleaned up.
* - nlc_ctx is cleaned up, because of invalidations
* or lru prune etc.*/
/* If the cache is present but invalid, clear the cache and
* reset the timer. */
__nlc_inode_clear_entries (this, nlc_ctx);
/* If timer is present, then it is already part of lru as well
* Hence reset the timer and return.*/
if (nlc_ctx->timer) {
gf_tw_mod_timer_pending (conf->timer_wheel,
nlc_ctx->timer,
conf->cache_timeout);
time (&nlc_ctx->cache_time);
goto unlock;
}
/* If timer was NULL, the nlc_ctx is already cleanedup,
* and we need to start timer and add to lru, so that it is
* ready to cache entries a fresh */
ret = __nlc_inode_ctx_timer_start (this, inode, nlc_ctx);
if (ret < 0)
goto unlock;
ret = __nlc_add_to_lru (this, inode, nlc_ctx);
if (ret < 0) {
__nlc_inode_ctx_timer_delete (this, nlc_ctx);
goto unlock;
}
}
unlock:
UNLOCK (&nlc_ctx->lock);
return;
}
static nlc_ctx_t *
nlc_inode_ctx_get_set (xlator_t *this, inode_t *inode, nlc_ctx_t **nlc_ctx_p,
nlc_pe_t **nlc_pe_p)
{
int ret = 0;
nlc_ctx_t *nlc_ctx = NULL;
nlc_conf_t *conf = NULL;
conf = this->private;
LOCK (&inode->lock);
{
ret = __nlc_inode_ctx_get (this, inode, &nlc_ctx, nlc_pe_p);
if (nlc_ctx)
goto unlock;
nlc_ctx = GF_CALLOC (sizeof (*nlc_ctx), 1, gf_nlc_mt_nlc_ctx_t);
if (!nlc_ctx)
goto unlock;
LOCK_INIT (&nlc_ctx->lock);
INIT_LIST_HEAD (&nlc_ctx->pe);
INIT_LIST_HEAD (&nlc_ctx->ne);
ret = __nlc_inode_ctx_timer_start (this, inode, nlc_ctx);
if (ret < 0)
goto unlock;
ret = __nlc_add_to_lru (this, inode, nlc_ctx);
if (ret < 0) {
__nlc_inode_ctx_timer_delete (this, nlc_ctx);
goto unlock;
}
ret = __inode_ctx_set2 (inode, this, (uint64_t *) &nlc_ctx, NULL);
if (ret) {
gf_msg (this->name, GF_LOG_ERROR, ENOMEM,
NLC_MSG_NO_MEMORY, "inode ctx set failed");
__nlc_inode_ctx_timer_delete (this, nlc_ctx);
nlc_remove_from_lru (this, inode);
goto unlock;
}
/*TODO: also sizeof (gf_tw_timer_list) + nlc_timer_data_t ?*/
nlc_ctx->cache_size = sizeof (*nlc_ctx);
GF_ATOMIC_ADD (conf->current_cache_size, nlc_ctx->cache_size);
}
unlock:
UNLOCK (&inode->lock);
if (ret == 0 && nlc_ctx_p) {
*nlc_ctx_p = nlc_ctx;
nlc_init_invalid_ctx (this, inode, nlc_ctx);
}
if (ret < 0 && nlc_ctx) {
LOCK_DESTROY (&nlc_ctx->lock);
GF_FREE (nlc_ctx);
nlc_ctx = NULL;
goto out;
}
out:
return nlc_ctx;
}
nlc_local_t *
nlc_local_init (call_frame_t *frame, xlator_t *this, glusterfs_fop_t fop,
loc_t *loc, loc_t *loc2)
{
nlc_local_t *local = NULL;
local = GF_CALLOC (sizeof (*local), 1, gf_nlc_mt_nlc_local_t);
if (!local)
goto out;
if (loc)
loc_copy (&local->loc, loc);
if (loc2)
loc_copy (&local->loc2, loc2);
local->fop = fop;
frame->local = local;
out:
return local;
}
void
nlc_local_wipe (xlator_t *this, nlc_local_t *local)
{
if (!local)
goto out;
loc_wipe (&local->loc);
loc_wipe (&local->loc2);
GF_FREE (local);
out:
return;
}
static void
__nlc_set_dir_state (nlc_ctx_t *nlc_ctx, uint64_t new_state)
{
nlc_ctx->state |= new_state;
return;
}
void
nlc_set_dir_state (xlator_t *this, inode_t *inode, uint64_t state)
{
nlc_ctx_t *nlc_ctx = NULL;
if (inode->ia_type != IA_IFDIR) {
gf_msg_callingfn (this->name, GF_LOG_ERROR, EINVAL,
NLC_MSG_EINVAL, "inode is not of type dir");
goto out;
}
nlc_inode_ctx_get_set (this, inode, &nlc_ctx, NULL);
if (!nlc_ctx)
goto out;
LOCK (&nlc_ctx->lock);
{
__nlc_set_dir_state (nlc_ctx, state);
}
UNLOCK (&nlc_ctx->lock);
out:
return;
}
static void
nlc_cache_timeout_handler (struct gf_tw_timer_list *timer,
void *data, unsigned long calltime)
{
nlc_timer_data_t *tmp = data;
nlc_ctx_t *nlc_ctx = NULL;
nlc_inode_ctx_get (tmp->this, tmp->inode, &nlc_ctx, NULL);
if (!nlc_ctx)
goto out;
/* Taking nlc_ctx->lock will lead to deadlock, hence updating
* the cache is invalid outside of lock, instead of clear_cache.
* Since cache_time is assigned outside of lock, the value can
* be invalid for short time, this may result in false negative
* which is better than deadlock */
nlc_ctx->cache_time = 0;
out:
return;
}
void
__nlc_inode_ctx_timer_delete (xlator_t *this, nlc_ctx_t *nlc_ctx)
{
nlc_conf_t *conf = NULL;
conf = this->private;
if (nlc_ctx->timer)
gf_tw_del_timer (conf->timer_wheel, nlc_ctx->timer);
if (nlc_ctx->timer_data) {
inode_unref (nlc_ctx->timer_data->inode);
GF_FREE (nlc_ctx->timer_data);
nlc_ctx->timer_data = NULL;
}
GF_FREE (nlc_ctx->timer);
nlc_ctx->timer = NULL;
return;
}
int
__nlc_inode_ctx_timer_start (xlator_t *this, inode_t *inode, nlc_ctx_t *nlc_ctx)
{
struct gf_tw_timer_list *timer = NULL;
nlc_timer_data_t *tmp = NULL;
nlc_conf_t *conf = NULL;
int ret = -1;
conf = this->private;
/* We are taking inode_table->lock within inode->lock
* as the only other caller which takes inode->lock within
* inode_table->lock and cause deadlock is inode_table_destroy.
* Hopefully, there can be no fop when inode_table_destroy is
* being called. */
tmp = GF_CALLOC (1, sizeof (*tmp), gf_nlc_mt_nlc_timer_data_t);
if (!tmp)
goto out;
tmp->inode = inode_ref (inode);
tmp->this = this;
timer = GF_CALLOC (1, sizeof (*timer),
gf_common_mt_tw_timer_list);
if (!timer)
goto out;
INIT_LIST_HEAD (&timer->entry);
timer->expires = nlc_get_cache_timeout (this);
timer->function = nlc_cache_timeout_handler;
timer->data = tmp;
nlc_ctx->timer = timer;
nlc_ctx->timer_data = tmp;
gf_tw_add_timer (conf->timer_wheel, timer);
time (&nlc_ctx->cache_time);
gf_msg_trace (this->name, 0, "Registering timer:%p, inode:%p, "
"gfid:%s", timer, inode, uuid_utoa (inode->gfid));
ret = 0;
out:
if (ret < 0) {
if (tmp && tmp->inode)
inode_unref (tmp->inode);
GF_FREE (tmp);
GF_FREE (timer);
}
return ret;
}
int
__nlc_add_to_lru (xlator_t *this, inode_t *inode, nlc_ctx_t *nlc_ctx)
{
nlc_lru_node_t *lru_ino = NULL;
uint64_t nlc_pe_int = 0;
nlc_conf_t *conf = NULL;
int ret = -1;
conf = this->private;
lru_ino = GF_CALLOC (1, sizeof (*lru_ino), gf_nlc_mt_nlc_lru_node);
if (!lru_ino)
goto out;
INIT_LIST_HEAD (&lru_ino->list);
lru_ino->inode = inode_ref (inode);
LOCK (&conf->lock);
{
list_add_tail (&lru_ino->list, &conf->lru);
}
UNLOCK (&conf->lock);
nlc_ctx->refd_inodes = 0;
ret = __inode_ctx_get2 (inode, this, NULL, &nlc_pe_int);
if (nlc_pe_int == 0)
GF_ATOMIC_ADD (conf->refd_inodes, 1);
ret = 0;
out:
return ret;
}
void
nlc_remove_from_lru (xlator_t *this, inode_t *inode)
{
nlc_lru_node_t *lru_node = NULL;
nlc_lru_node_t *tmp = NULL;
nlc_lru_node_t *tmp1 = NULL;
nlc_conf_t *conf = NULL;
conf = this->private;
LOCK (&conf->lock);
{
list_for_each_entry_safe (lru_node, tmp, &conf->lru, list) {
if (inode == lru_node->inode) {
list_del (&lru_node->list);
tmp1 = lru_node;
break;
}
}
}
UNLOCK (&conf->lock);
if (tmp1) {
inode_unref (tmp1->inode);
GF_FREE (tmp1);
}
return;
}
void
nlc_lru_prune (xlator_t *this, inode_t *inode)
{
nlc_lru_node_t *lru_node = NULL;
nlc_lru_node_t *prune_node = NULL;
nlc_lru_node_t *tmp = NULL;
nlc_conf_t *conf = NULL;
conf = this->private;
LOCK (&conf->lock);
{
if ((GF_ATOMIC_GET(conf->refd_inodes) < conf->inode_limit) &&
(GF_ATOMIC_GET(conf->current_cache_size) < conf->cache_size))
goto unlock;
list_for_each_entry_safe (lru_node, tmp, &conf->lru, list) {
list_del (&lru_node->list);
prune_node = lru_node;
goto unlock;
}
}
unlock:
UNLOCK (&conf->lock);
if (prune_node) {
nlc_inode_clear_cache (this, prune_node->inode, NLC_LRU_PRUNE);
inode_unref (prune_node->inode);
GF_FREE (prune_node);
}
return;
}
void
nlc_clear_all_cache (xlator_t *this)
{
nlc_conf_t *conf = NULL;
struct list_head clear_list;
nlc_lru_node_t *prune_node = NULL;
nlc_lru_node_t *tmp = NULL;
conf = this->private;
INIT_LIST_HEAD (&clear_list);
LOCK (&conf->lock);
{
list_replace_init (&conf->lru, &clear_list);
}
UNLOCK (&conf->lock);
list_for_each_entry_safe (prune_node, tmp, &clear_list, list) {
list_del (&prune_node->list);
nlc_inode_clear_cache (this, prune_node->inode, NLC_LRU_PRUNE);
inode_unref (prune_node->inode);
GF_FREE (prune_node);
}
return;
}
void
__nlc_free_pe (xlator_t *this, nlc_ctx_t *nlc_ctx, nlc_pe_t *pe)
{
uint64_t pe_int = 0;
nlc_conf_t *conf = NULL;
uint64_t nlc_ctx_int = 0;
conf = this->private;
if (pe->inode) {
inode_ctx_reset1 (pe->inode, this, &pe_int);
inode_ctx_get2 (pe->inode, this, &nlc_ctx_int, NULL);
inode_unref (pe->inode);
}
list_del (&pe->list);
nlc_ctx->cache_size -= sizeof (*pe) + sizeof (pe->name);
GF_ATOMIC_SUB (conf->current_cache_size,
(sizeof (*pe) + sizeof (pe->name)));
nlc_ctx->refd_inodes -= 1;
if (nlc_ctx_int == 0)
GF_ATOMIC_SUB (conf->refd_inodes, 1);
GF_FREE (pe->name);
GF_FREE (pe);
return;
}
void
__nlc_free_ne (xlator_t *this, nlc_ctx_t *nlc_ctx, nlc_ne_t *ne)
{
nlc_conf_t *conf = NULL;
conf = this->private;
list_del (&ne->list);
GF_FREE (ne->name);
GF_FREE (ne);
nlc_ctx->cache_size -= sizeof (*ne) + sizeof (ne->name);
GF_ATOMIC_SUB (conf->current_cache_size,
(sizeof (*ne) + sizeof (ne->name)));
return;
}
void
nlc_inode_clear_cache (xlator_t *this, inode_t *inode, int reason)
{
nlc_ctx_t *nlc_ctx = NULL;
nlc_inode_ctx_get (this, inode, &nlc_ctx, NULL);
if (!nlc_ctx)
goto out;
LOCK (&nlc_ctx->lock);
{
__nlc_inode_ctx_timer_delete (this, nlc_ctx);
__nlc_inode_clear_entries (this, nlc_ctx);
}
UNLOCK (&nlc_ctx->lock);
if (reason != NLC_LRU_PRUNE)
nlc_remove_from_lru (this, inode);
out:
return;
}
static void
__nlc_del_pe (xlator_t *this, nlc_ctx_t *nlc_ctx, inode_t *entry_ino,
const char *name, gf_boolean_t multilink)
{
nlc_pe_t *pe = NULL;
nlc_pe_t *tmp = NULL;
gf_boolean_t found = _gf_false;
uint64_t pe_int = 0;
if (!IS_PE_VALID (nlc_ctx->state))
goto out;
if (!entry_ino)
goto name_search;
/* If there are hardlinks first search names, followed by inodes */
if (multilink) {
list_for_each_entry_safe (pe, tmp, &nlc_ctx->pe, list) {
if (pe->name && (strcmp (pe->name, name) == 0)) {
found = _gf_true;
goto out;
}
}
inode_ctx_reset1 (entry_ino, this, &pe_int);
if (pe_int) {
pe = (void *) (long) (pe_int);
found = _gf_true;
goto out;
}
goto out;
}
inode_ctx_reset1 (entry_ino, this, &pe_int);
if (pe_int) {
pe = (void *) (long) (pe_int);
found = _gf_true;
goto out;
}
name_search:
list_for_each_entry_safe (pe, tmp, &nlc_ctx->pe, list) {
if (pe->name && (strcmp (pe->name, name) == 0)) {
found = _gf_true;
break;
/* TODO: can there be duplicates? */
}
}
out:
if (found)
__nlc_free_pe (this, nlc_ctx, pe);
return;
}
static void
__nlc_del_ne (xlator_t *this, nlc_ctx_t *nlc_ctx, const char *name)
{
nlc_ne_t *ne = NULL;
nlc_ne_t *tmp = NULL;
if (!IS_NE_VALID (nlc_ctx->state))
goto out;
list_for_each_entry_safe (ne, tmp, &nlc_ctx->ne, list) {
if (strcmp (ne->name, name) == 0) {
__nlc_free_ne (this, nlc_ctx, ne);
break;
}
}
out:
return;
}
static void
__nlc_add_pe (xlator_t *this, nlc_ctx_t *nlc_ctx, inode_t *entry_ino,
const char *name)
{
nlc_pe_t *pe = NULL;
int ret = -1;
nlc_conf_t *conf = NULL;
uint64_t nlc_ctx_int = 0;
conf = this->private;
/* TODO: There can be no duplicate entries, as it is added only
during create. In case there arises duplicate entries, search PE
found = __nlc_search (entries, name, _gf_false);
can use bit vector to have simple search than sequential search */
pe = GF_CALLOC (sizeof (*pe), 1, gf_nlc_mt_nlc_pe_t);
if (!pe)
goto out;
if (entry_ino) {
pe->inode = inode_ref (entry_ino);
nlc_inode_ctx_set (this, entry_ino, NULL, pe);
} else if (name) {
pe->name = gf_strdup (name);
if (!pe->name)
goto out;
}
list_add (&pe->list, &nlc_ctx->pe);
nlc_ctx->cache_size += sizeof (*pe) + sizeof (pe->name);
GF_ATOMIC_ADD (conf->current_cache_size,
(sizeof (*pe) + sizeof (pe->name)));
nlc_ctx->refd_inodes += 1;
inode_ctx_get2 (entry_ino, this, &nlc_ctx_int, NULL);
if (nlc_ctx_int == 0)
GF_ATOMIC_ADD (conf->refd_inodes, 1);
ret = 0;
out:
if (ret)
GF_FREE (pe);
return;
}
static void
__nlc_add_ne (xlator_t *this, nlc_ctx_t *nlc_ctx, const char *name)
{
nlc_ne_t *ne = NULL;
int ret = -1;
nlc_conf_t *conf = NULL;
conf = this->private;
/* TODO: search ne before adding to get rid of duplicate entries
found = __nlc_search (entries, name, _gf_false);
can use bit vector to have faster search than sequential search */
ne = GF_CALLOC (sizeof (*ne), 1, gf_nlc_mt_nlc_ne_t);
if (!ne)
goto out;
ne->name = gf_strdup (name);
if (!ne->name)
goto out;
list_add (&ne->list, &nlc_ctx->ne);
nlc_ctx->cache_size += sizeof (*ne) + sizeof (ne->name);
GF_ATOMIC_ADD (conf->current_cache_size,
(sizeof (*ne) + sizeof (ne->name)));
ret = 0;
out:
if (ret)
GF_FREE (ne);
return;
}
void
nlc_dir_add_ne (xlator_t *this, inode_t *inode, const char *name)
{
nlc_ctx_t *nlc_ctx = NULL;
if (inode->ia_type != IA_IFDIR) {
gf_msg_callingfn (this->name, GF_LOG_ERROR, EINVAL,
NLC_MSG_EINVAL, "inode is not of type dir");
goto out;
}
nlc_inode_ctx_get_set (this, inode, &nlc_ctx, NULL);
if (!nlc_ctx)
goto out;
LOCK (&nlc_ctx->lock);
{
/* There is one possiblility where we need to search before
* adding NE: when there are two parallel lookups on a non
* existent file */
if (!__nlc_search_ne (nlc_ctx, name)) {
__nlc_add_ne (this, nlc_ctx, name);
__nlc_set_dir_state (nlc_ctx, NLC_NE_VALID);
}
}
UNLOCK (&nlc_ctx->lock);
out:
return;
}
void
nlc_dir_remove_pe (xlator_t *this, inode_t *parent, inode_t *entry_ino,
const char *name, gf_boolean_t multilink)
{
nlc_ctx_t *nlc_ctx = NULL;
if (parent->ia_type != IA_IFDIR) {
gf_msg_callingfn (this->name, GF_LOG_ERROR, EINVAL,
NLC_MSG_EINVAL, "inode is not of type dir");
goto out;
}
nlc_inode_ctx_get (this, parent, &nlc_ctx, NULL);
if (!nlc_ctx)
goto out;
LOCK (&nlc_ctx->lock);
{
if (!__nlc_is_cache_valid (this, nlc_ctx))
goto unlock;
__nlc_del_pe (this, nlc_ctx, entry_ino, name, multilink);
__nlc_add_ne (this, nlc_ctx, name);
__nlc_set_dir_state (nlc_ctx, NLC_NE_VALID);
}
unlock:
UNLOCK (&nlc_ctx->lock);
out:
return;
}
void
nlc_dir_add_pe (xlator_t *this, inode_t *inode, inode_t *entry_ino,
const char *name)
{
nlc_ctx_t *nlc_ctx = NULL;
if (inode->ia_type != IA_IFDIR) {
gf_msg_callingfn (this->name, GF_LOG_ERROR, EINVAL,
NLC_MSG_EINVAL, "inode is not of type dir");
goto out;
}
nlc_inode_ctx_get_set (this, inode, &nlc_ctx, NULL);
if (!nlc_ctx)
goto out;
LOCK (&nlc_ctx->lock);
{
__nlc_del_ne (this, nlc_ctx, name);
__nlc_add_pe (this, nlc_ctx, entry_ino, name);
if (!IS_PE_VALID (nlc_ctx->state))
__nlc_set_dir_state (nlc_ctx, NLC_PE_PARTIAL);
}
UNLOCK (&nlc_ctx->lock);
out:
return;
}
gf_boolean_t
__nlc_search_ne (nlc_ctx_t *nlc_ctx, const char *name)
{
gf_boolean_t found = _gf_false;
nlc_ne_t *ne = NULL;
nlc_ne_t *tmp = NULL;
if (!IS_NE_VALID (nlc_ctx->state))
goto out;
list_for_each_entry_safe (ne, tmp, &nlc_ctx->ne, list) {
if (strcmp (ne->name, name) == 0) {
found = _gf_true;
break;
}
}
out:
return found;
}
static gf_boolean_t
__nlc_search_pe (nlc_ctx_t *nlc_ctx, const char *name)
{
gf_boolean_t found = _gf_false;
nlc_pe_t *pe = NULL;
nlc_pe_t *tmp = NULL;
if (!IS_PE_VALID (nlc_ctx->state))
goto out;
list_for_each_entry_safe (pe, tmp, &nlc_ctx->pe, list) {
if (pe->name && (strcmp (pe->name, name) == 0)) {
found = _gf_true;
break;
}
}
out:
return found;
}
static char *
__nlc_get_pe (nlc_ctx_t *nlc_ctx, const char *name, gf_boolean_t case_insensitive)
{
char *found = NULL;
nlc_pe_t *pe = NULL;
nlc_pe_t *tmp = NULL;
if (!IS_PE_VALID (nlc_ctx->state))
goto out;
if (case_insensitive) {
list_for_each_entry_safe (pe, tmp, &nlc_ctx->pe, list) {
if (pe->name &&
(strcasecmp (pe->name, name) == 0)) {
found = pe->name;
break;
}
}
} else {
list_for_each_entry_safe (pe, tmp, &nlc_ctx->pe, list) {
if (pe->name &&
(strcmp (pe->name, name) == 0)) {
found = pe->name;
break;
}
}
}
out:
return found;
}
gf_boolean_t
nlc_is_negative_lookup (xlator_t *this, loc_t *loc)
{
nlc_ctx_t *nlc_ctx = NULL;
inode_t *inode = NULL;
gf_boolean_t neg_entry = _gf_false;
inode = loc->parent;
GF_VALIDATE_OR_GOTO (this->name, inode, out);
if (inode->ia_type != IA_IFDIR) {
gf_msg_callingfn (this->name, GF_LOG_ERROR, EINVAL,
NLC_MSG_EINVAL, "inode is not of type dir");
goto out;
}
nlc_inode_ctx_get (this, inode, &nlc_ctx, NULL);
if (!nlc_ctx)
goto out;
LOCK (&nlc_ctx->lock);
{
if (!__nlc_is_cache_valid (this, nlc_ctx))
goto unlock;
if (__nlc_search_ne (nlc_ctx, loc->name)) {
neg_entry = _gf_true;
goto unlock;
}
if ((nlc_ctx->state & NLC_PE_FULL) &&
!__nlc_search_pe (nlc_ctx, loc->name)) {
neg_entry = _gf_true;
goto unlock;
}
}
unlock:
UNLOCK (&nlc_ctx->lock);
out:
return neg_entry;
}
gf_boolean_t
nlc_get_real_file_name (xlator_t *this, loc_t *loc, const char *fname,
int32_t *op_ret, int32_t *op_errno, dict_t *dict)
{
nlc_ctx_t *nlc_ctx = NULL;
inode_t *inode = NULL;
gf_boolean_t hit = _gf_false;
char *found_file = NULL;
int ret = 0;
GF_VALIDATE_OR_GOTO (this->name, loc, out);
GF_VALIDATE_OR_GOTO (this->name, fname, out);
GF_VALIDATE_OR_GOTO (this->name, op_ret, out);
GF_VALIDATE_OR_GOTO (this->name, op_errno, out);
GF_VALIDATE_OR_GOTO (this->name, dict, out);
inode = loc->inode;
GF_VALIDATE_OR_GOTO (this->name, inode, out);
if (inode->ia_type != IA_IFDIR) {
gf_msg_callingfn (this->name, GF_LOG_ERROR, EINVAL,
NLC_MSG_EINVAL, "inode is not of type dir");
goto out;
}
nlc_inode_ctx_get (this, inode, &nlc_ctx, NULL);
if (!nlc_ctx)
goto out;
LOCK (&nlc_ctx->lock);
{
if (!__nlc_is_cache_valid (this, nlc_ctx))
goto unlock;
found_file = __nlc_get_pe (nlc_ctx, fname, _gf_true);
if (found_file) {
ret = dict_set_dynstr (dict, GF_XATTR_GET_REAL_FILENAME_KEY,
gf_strdup (found_file));
if (ret < 0)
goto unlock;
*op_ret = strlen (found_file) + 1;
hit = _gf_true;
goto unlock;
}
if (!found_file && (nlc_ctx->state & NLC_PE_FULL)) {
*op_ret = -1;
*op_errno = ENOENT;
hit = _gf_true;
goto unlock;
}
}
unlock:
UNLOCK (&nlc_ctx->lock);
out:
return hit;
}
void
nlc_dump_inodectx (xlator_t *this, inode_t *inode)
{
int32_t ret = -1;
char *path = NULL;
char key_prefix[GF_DUMP_MAX_BUF_LEN] = {0, };
char uuid_str[64] = {0,};
nlc_ctx_t *nlc_ctx = NULL;
nlc_pe_t *pe = NULL;
nlc_pe_t *tmp = NULL;
nlc_ne_t *ne = NULL;
nlc_ne_t *tmp1 = NULL;
nlc_inode_ctx_get (this, inode, &nlc_ctx, NULL);
if (!nlc_ctx)
goto out;
ret = TRY_LOCK (&nlc_ctx->lock);
if (!ret) {
gf_proc_dump_build_key (key_prefix,
"xlator.performance.nl-cache",
"nlc_inode");
gf_proc_dump_add_section (key_prefix);
__inode_path (inode, NULL, &path);
if (path != NULL) {
gf_proc_dump_write ("path", "%s", path);
GF_FREE (path);
}
uuid_utoa_r (inode->gfid, uuid_str);
gf_proc_dump_write ("inode", "%p", inode);
gf_proc_dump_write ("gfid", "%s", uuid_str);
gf_proc_dump_write ("state", "%"PRIu64, nlc_ctx->state);
gf_proc_dump_write ("timer", "%p", nlc_ctx->timer);
gf_proc_dump_write ("cache-time", "%lld", nlc_ctx->cache_time);
gf_proc_dump_write ("cache-size", "%zu", nlc_ctx->cache_size);
gf_proc_dump_write ("refd-inodes", "%"PRIu64, nlc_ctx->refd_inodes);
if (IS_PE_VALID (nlc_ctx->state))
list_for_each_entry_safe (pe, tmp, &nlc_ctx->pe, list) {
gf_proc_dump_write ("pe", "%p, %s", pe,
pe->inode, pe->name);
}
if (IS_NE_VALID (nlc_ctx->state))
list_for_each_entry_safe (ne, tmp1, &nlc_ctx->ne, list) {
gf_proc_dump_write ("ne", "%s", ne->name);
}
UNLOCK (&nlc_ctx->lock);
}
if (ret && nlc_ctx)
gf_proc_dump_write ("Unable to dump the inode information",
"(Lock acquisition failed) %p (gfid: %s)",
nlc_ctx, uuid_str);
out:
return;
}
|