forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.c
2097 lines (1776 loc) · 53.9 KB
/
dir.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
// SPDX-License-Identifier: GPL-2.0-or-later
/* dir.c: AFS filesystem directory handling
*
* Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
* Written by David Howells ([email protected])
*/
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/namei.h>
#include <linux/pagemap.h>
#include <linux/swap.h>
#include <linux/ctype.h>
#include <linux/sched.h>
#include <linux/task_io_accounting_ops.h>
#include "internal.h"
#include "afs_fs.h"
#include "xdr_fs.h"
static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
unsigned int flags);
static int afs_dir_open(struct inode *inode, struct file *file);
static int afs_readdir(struct file *file, struct dir_context *ctx);
static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
static int afs_d_delete(const struct dentry *dentry);
static void afs_d_iput(struct dentry *dentry, struct inode *inode);
static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
loff_t fpos, u64 ino, unsigned dtype);
static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
loff_t fpos, u64 ino, unsigned dtype);
static int afs_create(struct user_namespace *mnt_userns, struct inode *dir,
struct dentry *dentry, umode_t mode, bool excl);
static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
struct dentry *dentry, umode_t mode);
static int afs_rmdir(struct inode *dir, struct dentry *dentry);
static int afs_unlink(struct inode *dir, struct dentry *dentry);
static int afs_link(struct dentry *from, struct inode *dir,
struct dentry *dentry);
static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
struct dentry *dentry, const char *content);
static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
struct dentry *old_dentry, struct inode *new_dir,
struct dentry *new_dentry, unsigned int flags);
static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
unsigned int length);
static int afs_dir_set_page_dirty(struct page *page)
{
BUG(); /* This should never happen. */
}
const struct file_operations afs_dir_file_operations = {
.open = afs_dir_open,
.release = afs_release,
.iterate_shared = afs_readdir,
.lock = afs_lock,
.llseek = generic_file_llseek,
};
const struct inode_operations afs_dir_inode_operations = {
.create = afs_create,
.lookup = afs_lookup,
.link = afs_link,
.unlink = afs_unlink,
.symlink = afs_symlink,
.mkdir = afs_mkdir,
.rmdir = afs_rmdir,
.rename = afs_rename,
.permission = afs_permission,
.getattr = afs_getattr,
.setattr = afs_setattr,
};
const struct address_space_operations afs_dir_aops = {
.set_page_dirty = afs_dir_set_page_dirty,
.releasepage = afs_dir_releasepage,
.invalidatepage = afs_dir_invalidatepage,
};
const struct dentry_operations afs_fs_dentry_operations = {
.d_revalidate = afs_d_revalidate,
.d_delete = afs_d_delete,
.d_release = afs_d_release,
.d_automount = afs_d_automount,
.d_iput = afs_d_iput,
};
struct afs_lookup_one_cookie {
struct dir_context ctx;
struct qstr name;
bool found;
struct afs_fid fid;
};
struct afs_lookup_cookie {
struct dir_context ctx;
struct qstr name;
bool found;
bool one_only;
unsigned short nr_fids;
struct afs_fid fids[50];
};
/*
* Drop the refs that we're holding on the pages we were reading into. We've
* got refs on the first nr_pages pages.
*/
static void afs_dir_read_cleanup(struct afs_read *req)
{
struct address_space *mapping = req->vnode->vfs_inode.i_mapping;
struct page *page;
pgoff_t last = req->nr_pages - 1;
XA_STATE(xas, &mapping->i_pages, 0);
if (unlikely(!req->nr_pages))
return;
rcu_read_lock();
xas_for_each(&xas, page, last) {
if (xas_retry(&xas, page))
continue;
BUG_ON(xa_is_value(page));
BUG_ON(PageCompound(page));
ASSERTCMP(page->mapping, ==, mapping);
put_page(page);
}
rcu_read_unlock();
}
/*
* check that a directory page is valid
*/
static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
loff_t i_size)
{
struct afs_xdr_dir_page *dbuf;
loff_t latter, off;
int tmp, qty;
/* Determine how many magic numbers there should be in this page, but
* we must take care because the directory may change size under us.
*/
off = page_offset(page);
if (i_size <= off)
goto checked;
latter = i_size - off;
if (latter >= PAGE_SIZE)
qty = PAGE_SIZE;
else
qty = latter;
qty /= sizeof(union afs_xdr_dir_block);
/* check them */
dbuf = kmap_atomic(page);
for (tmp = 0; tmp < qty; tmp++) {
if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
__func__, dvnode->vfs_inode.i_ino, tmp, qty,
ntohs(dbuf->blocks[tmp].hdr.magic));
trace_afs_dir_check_failed(dvnode, off, i_size);
kunmap(page);
trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
goto error;
}
/* Make sure each block is NUL terminated so we can reasonably
* use string functions on it. The filenames in the page
* *should* be NUL-terminated anyway.
*/
((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
}
kunmap_atomic(dbuf);
checked:
afs_stat_v(dvnode, n_read_dir);
return true;
error:
return false;
}
/*
* Dump the contents of a directory.
*/
static void afs_dir_dump(struct afs_vnode *dvnode, struct afs_read *req)
{
struct afs_xdr_dir_page *dbuf;
struct address_space *mapping = dvnode->vfs_inode.i_mapping;
struct page *page;
unsigned int i, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
pgoff_t last = req->nr_pages - 1;
XA_STATE(xas, &mapping->i_pages, 0);
pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx\n",
dvnode->fid.vid, dvnode->fid.vnode,
req->file_size, req->len, req->actual_len);
pr_warn("DIR %llx %x %zx %zx\n",
req->pos, req->nr_pages,
req->iter->iov_offset, iov_iter_count(req->iter));
xas_for_each(&xas, page, last) {
if (xas_retry(&xas, page))
continue;
BUG_ON(PageCompound(page));
BUG_ON(page->mapping != mapping);
dbuf = kmap_atomic(page);
for (i = 0; i < qty; i++) {
union afs_xdr_dir_block *block = &dbuf->blocks[i];
pr_warn("[%02lx] %32phN\n", page->index * qty + i, block);
}
kunmap_atomic(dbuf);
}
}
/*
* Check all the pages in a directory. All the pages are held pinned.
*/
static int afs_dir_check(struct afs_vnode *dvnode, struct afs_read *req)
{
struct address_space *mapping = dvnode->vfs_inode.i_mapping;
struct page *page;
pgoff_t last = req->nr_pages - 1;
int ret = 0;
XA_STATE(xas, &mapping->i_pages, 0);
if (unlikely(!req->nr_pages))
return 0;
rcu_read_lock();
xas_for_each(&xas, page, last) {
if (xas_retry(&xas, page))
continue;
BUG_ON(PageCompound(page));
BUG_ON(page->mapping != mapping);
if (!afs_dir_check_page(dvnode, page, req->file_size)) {
afs_dir_dump(dvnode, req);
ret = -EIO;
break;
}
}
rcu_read_unlock();
return ret;
}
/*
* open an AFS directory file
*/
static int afs_dir_open(struct inode *inode, struct file *file)
{
_enter("{%lu}", inode->i_ino);
BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
return -ENOENT;
return afs_open(inode, file);
}
/*
* Read the directory into the pagecache in one go, scrubbing the previous
* contents. The list of pages is returned, pinning them so that they don't
* get reclaimed during the iteration.
*/
static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
__acquires(&dvnode->validate_lock)
{
struct afs_read *req;
loff_t i_size;
int nr_pages, i, n;
int ret;
_enter("");
req = kzalloc(sizeof(*req), GFP_KERNEL);
if (!req)
return ERR_PTR(-ENOMEM);
refcount_set(&req->usage, 1);
req->vnode = dvnode;
req->key = key_get(key);
req->cleanup = afs_dir_read_cleanup;
expand:
i_size = i_size_read(&dvnode->vfs_inode);
if (i_size < 2048) {
ret = afs_bad(dvnode, afs_file_error_dir_small);
goto error;
}
if (i_size > 2048 * 1024) {
trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
ret = -EFBIG;
goto error;
}
_enter("%llu", i_size);
nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
req->actual_len = i_size; /* May change */
req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
req->data_version = dvnode->status.data_version; /* May change */
iov_iter_xarray(&req->def_iter, READ, &dvnode->vfs_inode.i_mapping->i_pages,
0, i_size);
req->iter = &req->def_iter;
/* Fill in any gaps that we might find where the memory reclaimer has
* been at work and pin all the pages. If there are any gaps, we will
* need to reread the entire directory contents.
*/
i = req->nr_pages;
while (i < nr_pages) {
struct page *pages[8], *page;
n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
min_t(unsigned int, nr_pages - i,
ARRAY_SIZE(pages)),
pages);
_debug("find %u at %u/%u", n, i, nr_pages);
if (n == 0) {
gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
afs_stat_v(dvnode, n_inval);
ret = -ENOMEM;
page = __page_cache_alloc(gfp);
if (!page)
goto error;
ret = add_to_page_cache_lru(page,
dvnode->vfs_inode.i_mapping,
i, gfp);
if (ret < 0)
goto error;
attach_page_private(page, (void *)1);
unlock_page(page);
req->nr_pages++;
i++;
} else {
req->nr_pages += n;
i += n;
}
}
/* If we're going to reload, we need to lock all the pages to prevent
* races.
*/
ret = -ERESTARTSYS;
if (down_read_killable(&dvnode->validate_lock) < 0)
goto error;
if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
goto success;
up_read(&dvnode->validate_lock);
if (down_write_killable(&dvnode->validate_lock) < 0)
goto error;
if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
trace_afs_reload_dir(dvnode);
ret = afs_fetch_data(dvnode, req);
if (ret < 0)
goto error_unlock;
task_io_account_read(PAGE_SIZE * req->nr_pages);
if (req->len < req->file_size) {
/* The content has grown, so we need to expand the
* buffer.
*/
up_write(&dvnode->validate_lock);
goto expand;
}
/* Validate the data we just read. */
ret = afs_dir_check(dvnode, req);
if (ret < 0)
goto error_unlock;
// TODO: Trim excess pages
set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
}
downgrade_write(&dvnode->validate_lock);
success:
return req;
error_unlock:
up_write(&dvnode->validate_lock);
error:
afs_put_read(req);
_leave(" = %d", ret);
return ERR_PTR(ret);
}
/*
* deal with one block in an AFS directory
*/
static int afs_dir_iterate_block(struct afs_vnode *dvnode,
struct dir_context *ctx,
union afs_xdr_dir_block *block,
unsigned blkoff)
{
union afs_xdr_dirent *dire;
unsigned offset, next, curr, nr_slots;
size_t nlen;
int tmp;
_enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
/* walk through the block, an entry at a time */
for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
offset < AFS_DIR_SLOTS_PER_BLOCK;
offset = next
) {
/* skip entries marked unused in the bitmap */
if (!(block->hdr.bitmap[offset / 8] &
(1 << (offset % 8)))) {
_debug("ENT[%zu.%u]: unused",
blkoff / sizeof(union afs_xdr_dir_block), offset);
next = offset + 1;
if (offset >= curr)
ctx->pos = blkoff +
next * sizeof(union afs_xdr_dirent);
continue;
}
/* got a valid entry */
dire = &block->dirents[offset];
nlen = strnlen(dire->u.name,
sizeof(*block) -
offset * sizeof(union afs_xdr_dirent));
if (nlen > AFSNAMEMAX - 1) {
_debug("ENT[%zu]: name too long (len %u/%zu)",
blkoff / sizeof(union afs_xdr_dir_block),
offset, nlen);
return afs_bad(dvnode, afs_file_error_dir_name_too_long);
}
_debug("ENT[%zu.%u]: %s %zu \"%s\"",
blkoff / sizeof(union afs_xdr_dir_block), offset,
(offset < curr ? "skip" : "fill"),
nlen, dire->u.name);
nr_slots = afs_dir_calc_slots(nlen);
next = offset + nr_slots;
if (next > AFS_DIR_SLOTS_PER_BLOCK) {
_debug("ENT[%zu.%u]:"
" %u extends beyond end dir block"
" (len %zu)",
blkoff / sizeof(union afs_xdr_dir_block),
offset, next, nlen);
return afs_bad(dvnode, afs_file_error_dir_over_end);
}
/* Check that the name-extension dirents are all allocated */
for (tmp = 1; tmp < nr_slots; tmp++) {
unsigned int ix = offset + tmp;
if (!(block->hdr.bitmap[ix / 8] & (1 << (ix % 8)))) {
_debug("ENT[%zu.u]:"
" %u unmarked extension (%u/%u)",
blkoff / sizeof(union afs_xdr_dir_block),
offset, tmp, nr_slots);
return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
}
}
/* skip if starts before the current position */
if (offset < curr)
continue;
/* found the next entry */
if (!dir_emit(ctx, dire->u.name, nlen,
ntohl(dire->u.vnode),
(ctx->actor == afs_lookup_filldir ||
ctx->actor == afs_lookup_one_filldir)?
ntohl(dire->u.unique) : DT_UNKNOWN)) {
_leave(" = 0 [full]");
return 0;
}
ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
}
_leave(" = 1 [more]");
return 1;
}
/*
* iterate through the data blob that lists the contents of an AFS directory
*/
static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
struct key *key, afs_dataversion_t *_dir_version)
{
struct afs_vnode *dvnode = AFS_FS_I(dir);
struct afs_xdr_dir_page *dbuf;
union afs_xdr_dir_block *dblock;
struct afs_read *req;
struct page *page;
unsigned blkoff, limit;
void __rcu **slot;
int ret;
_enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
_leave(" = -ESTALE");
return -ESTALE;
}
req = afs_read_dir(dvnode, key);
if (IS_ERR(req))
return PTR_ERR(req);
*_dir_version = req->data_version;
/* round the file position up to the next entry boundary */
ctx->pos += sizeof(union afs_xdr_dirent) - 1;
ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
/* walk through the blocks in sequence */
ret = 0;
while (ctx->pos < req->actual_len) {
blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
/* Fetch the appropriate page from the directory and re-add it
* to the LRU. We have all the pages pinned with an extra ref.
*/
rcu_read_lock();
page = NULL;
slot = radix_tree_lookup_slot(&dvnode->vfs_inode.i_mapping->i_pages,
blkoff / PAGE_SIZE);
if (slot)
page = radix_tree_deref_slot(slot);
rcu_read_unlock();
if (!page) {
ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
break;
}
mark_page_accessed(page);
limit = blkoff & ~(PAGE_SIZE - 1);
dbuf = kmap(page);
/* deal with the individual blocks stashed on this page */
do {
dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
sizeof(union afs_xdr_dir_block)];
ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
if (ret != 1) {
kunmap(page);
goto out;
}
blkoff += sizeof(union afs_xdr_dir_block);
} while (ctx->pos < dir->i_size && blkoff < limit);
kunmap(page);
ret = 0;
}
out:
up_read(&dvnode->validate_lock);
afs_put_read(req);
_leave(" = %d", ret);
return ret;
}
/*
* read an AFS directory
*/
static int afs_readdir(struct file *file, struct dir_context *ctx)
{
afs_dataversion_t dir_version;
return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file),
&dir_version);
}
/*
* Search the directory for a single name
* - if afs_dir_iterate_block() spots this function, it'll pass the FID
* uniquifier through dtype
*/
static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
int nlen, loff_t fpos, u64 ino, unsigned dtype)
{
struct afs_lookup_one_cookie *cookie =
container_of(ctx, struct afs_lookup_one_cookie, ctx);
_enter("{%s,%u},%s,%u,,%llu,%u",
cookie->name.name, cookie->name.len, name, nlen,
(unsigned long long) ino, dtype);
/* insanity checks first */
BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
if (cookie->name.len != nlen ||
memcmp(cookie->name.name, name, nlen) != 0) {
_leave(" = 0 [no]");
return 0;
}
cookie->fid.vnode = ino;
cookie->fid.unique = dtype;
cookie->found = 1;
_leave(" = -1 [found]");
return -1;
}
/*
* Do a lookup of a single name in a directory
* - just returns the FID the dentry name maps to if found
*/
static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
struct afs_fid *fid, struct key *key,
afs_dataversion_t *_dir_version)
{
struct afs_super_info *as = dir->i_sb->s_fs_info;
struct afs_lookup_one_cookie cookie = {
.ctx.actor = afs_lookup_one_filldir,
.name = dentry->d_name,
.fid.vid = as->volume->vid
};
int ret;
_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
/* search the directory */
ret = afs_dir_iterate(dir, &cookie.ctx, key, _dir_version);
if (ret < 0) {
_leave(" = %d [iter]", ret);
return ret;
}
ret = -ENOENT;
if (!cookie.found) {
_leave(" = -ENOENT [not found]");
return -ENOENT;
}
*fid = cookie.fid;
_leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
return 0;
}
/*
* search the directory for a name
* - if afs_dir_iterate_block() spots this function, it'll pass the FID
* uniquifier through dtype
*/
static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
int nlen, loff_t fpos, u64 ino, unsigned dtype)
{
struct afs_lookup_cookie *cookie =
container_of(ctx, struct afs_lookup_cookie, ctx);
int ret;
_enter("{%s,%u},%s,%u,,%llu,%u",
cookie->name.name, cookie->name.len, name, nlen,
(unsigned long long) ino, dtype);
/* insanity checks first */
BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
if (cookie->found) {
if (cookie->nr_fids < 50) {
cookie->fids[cookie->nr_fids].vnode = ino;
cookie->fids[cookie->nr_fids].unique = dtype;
cookie->nr_fids++;
}
} else if (cookie->name.len == nlen &&
memcmp(cookie->name.name, name, nlen) == 0) {
cookie->fids[1].vnode = ino;
cookie->fids[1].unique = dtype;
cookie->found = 1;
if (cookie->one_only)
return -1;
}
ret = cookie->nr_fids >= 50 ? -1 : 0;
_leave(" = %d", ret);
return ret;
}
/*
* Deal with the result of a successful lookup operation. Turn all the files
* into inodes and save the first one - which is the one we actually want.
*/
static void afs_do_lookup_success(struct afs_operation *op)
{
struct afs_vnode_param *vp;
struct afs_vnode *vnode;
struct inode *inode;
u32 abort_code;
int i;
_enter("");
for (i = 0; i < op->nr_files; i++) {
switch (i) {
case 0:
vp = &op->file[0];
abort_code = vp->scb.status.abort_code;
if (abort_code != 0) {
op->ac.abort_code = abort_code;
op->error = afs_abort_to_error(abort_code);
}
break;
case 1:
vp = &op->file[1];
break;
default:
vp = &op->more_files[i - 2];
break;
}
if (!vp->scb.have_status && !vp->scb.have_error)
continue;
_debug("do [%u]", i);
if (vp->vnode) {
if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags))
afs_vnode_commit_status(op, vp);
} else if (vp->scb.status.abort_code == 0) {
inode = afs_iget(op, vp);
if (!IS_ERR(inode)) {
vnode = AFS_FS_I(inode);
afs_cache_permit(vnode, op->key,
0 /* Assume vnode->cb_break is 0 */ +
op->cb_v_break,
&vp->scb);
vp->vnode = vnode;
vp->put_vnode = true;
}
} else {
_debug("- abort %d %llx:%llx.%x",
vp->scb.status.abort_code,
vp->fid.vid, vp->fid.vnode, vp->fid.unique);
}
}
_leave("");
}
static const struct afs_operation_ops afs_inline_bulk_status_operation = {
.issue_afs_rpc = afs_fs_inline_bulk_status,
.issue_yfs_rpc = yfs_fs_inline_bulk_status,
.success = afs_do_lookup_success,
};
static const struct afs_operation_ops afs_lookup_fetch_status_operation = {
.issue_afs_rpc = afs_fs_fetch_status,
.issue_yfs_rpc = yfs_fs_fetch_status,
.success = afs_do_lookup_success,
.aborted = afs_check_for_remote_deletion,
};
/*
* See if we know that the server we expect to use doesn't support
* FS.InlineBulkStatus.
*/
static bool afs_server_supports_ibulk(struct afs_vnode *dvnode)
{
struct afs_server_list *slist;
struct afs_volume *volume = dvnode->volume;
struct afs_server *server;
bool ret = true;
int i;
if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags))
return true;
rcu_read_lock();
slist = rcu_dereference(volume->servers);
for (i = 0; i < slist->nr_servers; i++) {
server = slist->servers[i].server;
if (server == dvnode->cb_server) {
if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
ret = false;
break;
}
}
rcu_read_unlock();
return ret;
}
/*
* Do a lookup in a directory. We make use of bulk lookup to query a slew of
* files in one go and create inodes for them. The inode of the file we were
* asked for is returned.
*/
static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
struct key *key)
{
struct afs_lookup_cookie *cookie;
struct afs_vnode_param *vp;
struct afs_operation *op;
struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
struct inode *inode = NULL, *ti;
afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version);
long ret;
int i;
_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
if (!cookie)
return ERR_PTR(-ENOMEM);
for (i = 0; i < ARRAY_SIZE(cookie->fids); i++)
cookie->fids[i].vid = dvnode->fid.vid;
cookie->ctx.actor = afs_lookup_filldir;
cookie->name = dentry->d_name;
cookie->nr_fids = 2; /* slot 0 is saved for the fid we actually want
* and slot 1 for the directory */
if (!afs_server_supports_ibulk(dvnode))
cookie->one_only = true;
/* search the directory */
ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version);
if (ret < 0)
goto out;
dentry->d_fsdata = (void *)(unsigned long)data_version;
ret = -ENOENT;
if (!cookie->found)
goto out;
/* Check to see if we already have an inode for the primary fid. */
inode = ilookup5(dir->i_sb, cookie->fids[1].vnode,
afs_ilookup5_test_by_fid, &cookie->fids[1]);
if (inode)
goto out; /* We do */
/* Okay, we didn't find it. We need to query the server - and whilst
* we're doing that, we're going to attempt to look up a bunch of other
* vnodes also.
*/
op = afs_alloc_operation(NULL, dvnode->volume);
if (IS_ERR(op)) {
ret = PTR_ERR(op);
goto out;
}
afs_op_set_vnode(op, 0, dvnode);
afs_op_set_fid(op, 1, &cookie->fids[1]);
op->nr_files = cookie->nr_fids;
_debug("nr_files %u", op->nr_files);
/* Need space for examining all the selected files */
op->error = -ENOMEM;
if (op->nr_files > 2) {
op->more_files = kvcalloc(op->nr_files - 2,
sizeof(struct afs_vnode_param),
GFP_KERNEL);
if (!op->more_files)
goto out_op;
for (i = 2; i < op->nr_files; i++) {
vp = &op->more_files[i - 2];
vp->fid = cookie->fids[i];
/* Find any inodes that already exist and get their
* callback counters.
*/
ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode,
afs_ilookup5_test_by_fid, &vp->fid);
if (!IS_ERR_OR_NULL(ti)) {
vnode = AFS_FS_I(ti);
vp->dv_before = vnode->status.data_version;
vp->cb_break_before = afs_calc_vnode_cb_break(vnode);
vp->vnode = vnode;
vp->put_vnode = true;
vp->speculative = true; /* vnode not locked */
}
}
}
/* Try FS.InlineBulkStatus first. Abort codes for the individual
* lookups contained therein are stored in the reply without aborting
* the whole operation.
*/
op->error = -ENOTSUPP;
if (!cookie->one_only) {
op->ops = &afs_inline_bulk_status_operation;
afs_begin_vnode_operation(op);
afs_wait_for_operation(op);
}
if (op->error == -ENOTSUPP) {
/* We could try FS.BulkStatus next, but this aborts the entire
* op if any of the lookups fails - so, for the moment, revert
* to FS.FetchStatus for op->file[1].
*/
op->fetch_status.which = 1;
op->ops = &afs_lookup_fetch_status_operation;
afs_begin_vnode_operation(op);
afs_wait_for_operation(op);
}
inode = ERR_PTR(op->error);
out_op:
if (op->error == 0) {
inode = &op->file[1].vnode->vfs_inode;
op->file[1].vnode = NULL;
}
if (op->file[0].scb.have_status)
dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version;
else
dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before;
ret = afs_put_operation(op);
out:
kfree(cookie);
_leave("");
return inode ?: ERR_PTR(ret);
}
/*
* Look up an entry in a directory with @sys substitution.
*/
static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
struct key *key)
{
struct afs_sysnames *subs;
struct afs_net *net = afs_i2net(dir);
struct dentry *ret;
char *buf, *p, *name;
int len, i;
_enter("");
ret = ERR_PTR(-ENOMEM);
p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
if (!buf)
goto out_p;
if (dentry->d_name.len > 4) {
memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
p += dentry->d_name.len - 4;
}
/* There is an ordered list of substitutes that we have to try. */
read_lock(&net->sysnames_lock);
subs = net->sysnames;
refcount_inc(&subs->usage);
read_unlock(&net->sysnames_lock);
for (i = 0; i < subs->nr; i++) {
name = subs->subs[i];
len = dentry->d_name.len - 4 + strlen(name);
if (len >= AFSNAMEMAX) {
ret = ERR_PTR(-ENAMETOOLONG);
goto out_s;
}
strcpy(p, name);
ret = lookup_one_len(buf, dentry->d_parent, len);
if (IS_ERR(ret) || d_is_positive(ret))
goto out_s;
dput(ret);
}
/* We don't want to d_add() the @sys dentry here as we don't want to
* the cached dentry to hide changes to the sysnames list.
*/
ret = NULL;
out_s:
afs_put_sysnames(subs);