Loading...
1/* dir.c: AFS filesystem directory handling
2 *
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/fs.h>
16#include <linux/namei.h>
17#include <linux/pagemap.h>
18#include <linux/ctype.h>
19#include <linux/sched.h>
20#include "internal.h"
21
22static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
23 unsigned int flags);
24static int afs_dir_open(struct inode *inode, struct file *file);
25static int afs_readdir(struct file *file, struct dir_context *ctx);
26static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
27static int afs_d_delete(const struct dentry *dentry);
28static void afs_d_release(struct dentry *dentry);
29static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
30 loff_t fpos, u64 ino, unsigned dtype);
31static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
32 bool excl);
33static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
34static int afs_rmdir(struct inode *dir, struct dentry *dentry);
35static int afs_unlink(struct inode *dir, struct dentry *dentry);
36static int afs_link(struct dentry *from, struct inode *dir,
37 struct dentry *dentry);
38static int afs_symlink(struct inode *dir, struct dentry *dentry,
39 const char *content);
40static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
41 struct inode *new_dir, struct dentry *new_dentry);
42
43const struct file_operations afs_dir_file_operations = {
44 .open = afs_dir_open,
45 .release = afs_release,
46 .iterate = afs_readdir,
47 .lock = afs_lock,
48 .llseek = generic_file_llseek,
49};
50
51const struct inode_operations afs_dir_inode_operations = {
52 .create = afs_create,
53 .lookup = afs_lookup,
54 .link = afs_link,
55 .unlink = afs_unlink,
56 .symlink = afs_symlink,
57 .mkdir = afs_mkdir,
58 .rmdir = afs_rmdir,
59 .rename = afs_rename,
60 .permission = afs_permission,
61 .getattr = afs_getattr,
62 .setattr = afs_setattr,
63};
64
65const struct dentry_operations afs_fs_dentry_operations = {
66 .d_revalidate = afs_d_revalidate,
67 .d_delete = afs_d_delete,
68 .d_release = afs_d_release,
69 .d_automount = afs_d_automount,
70};
71
72#define AFS_DIR_HASHTBL_SIZE 128
73#define AFS_DIR_DIRENT_SIZE 32
74#define AFS_DIRENT_PER_BLOCK 64
75
76union afs_dirent {
77 struct {
78 uint8_t valid;
79 uint8_t unused[1];
80 __be16 hash_next;
81 __be32 vnode;
82 __be32 unique;
83 uint8_t name[16];
84 uint8_t overflow[4]; /* if any char of the name (inc
85 * NUL) reaches here, consume
86 * the next dirent too */
87 } u;
88 uint8_t extended_name[32];
89};
90
91/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
92struct afs_dir_pagehdr {
93 __be16 npages;
94 __be16 magic;
95#define AFS_DIR_MAGIC htons(1234)
96 uint8_t nentries;
97 uint8_t bitmap[8];
98 uint8_t pad[19];
99};
100
101/* directory block layout */
102union afs_dir_block {
103
104 struct afs_dir_pagehdr pagehdr;
105
106 struct {
107 struct afs_dir_pagehdr pagehdr;
108 uint8_t alloc_ctrs[128];
109 /* dir hash table */
110 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
111 } hdr;
112
113 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
114};
115
116/* layout on a linux VM page */
117struct afs_dir_page {
118 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
119};
120
121struct afs_lookup_cookie {
122 struct dir_context ctx;
123 struct afs_fid fid;
124 struct qstr name;
125 int found;
126};
127
128/*
129 * check that a directory page is valid
130 */
131static inline void afs_dir_check_page(struct inode *dir, struct page *page)
132{
133 struct afs_dir_page *dbuf;
134 loff_t latter;
135 int tmp, qty;
136
137#if 0
138 /* check the page count */
139 qty = desc.size / sizeof(dbuf->blocks[0]);
140 if (qty == 0)
141 goto error;
142
143 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
144 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
145 __func__, dir->i_ino, qty,
146 ntohs(dbuf->blocks[0].pagehdr.npages));
147 goto error;
148 }
149#endif
150
151 /* determine how many magic numbers there should be in this page */
152 latter = dir->i_size - page_offset(page);
153 if (latter >= PAGE_SIZE)
154 qty = PAGE_SIZE;
155 else
156 qty = latter;
157 qty /= sizeof(union afs_dir_block);
158
159 /* check them */
160 dbuf = page_address(page);
161 for (tmp = 0; tmp < qty; tmp++) {
162 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
163 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
164 __func__, dir->i_ino, tmp, qty,
165 ntohs(dbuf->blocks[tmp].pagehdr.magic));
166 goto error;
167 }
168 }
169
170 SetPageChecked(page);
171 return;
172
173error:
174 SetPageChecked(page);
175 SetPageError(page);
176}
177
178/*
179 * discard a page cached in the pagecache
180 */
181static inline void afs_dir_put_page(struct page *page)
182{
183 kunmap(page);
184 page_cache_release(page);
185}
186
187/*
188 * get a page into the pagecache
189 */
190static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
191 struct key *key)
192{
193 struct page *page;
194 _enter("{%lu},%lu", dir->i_ino, index);
195
196 page = read_cache_page(dir->i_mapping, index, afs_page_filler, key);
197 if (!IS_ERR(page)) {
198 kmap(page);
199 if (!PageChecked(page))
200 afs_dir_check_page(dir, page);
201 if (PageError(page))
202 goto fail;
203 }
204 return page;
205
206fail:
207 afs_dir_put_page(page);
208 _leave(" = -EIO");
209 return ERR_PTR(-EIO);
210}
211
212/*
213 * open an AFS directory file
214 */
215static int afs_dir_open(struct inode *inode, struct file *file)
216{
217 _enter("{%lu}", inode->i_ino);
218
219 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
220 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
221
222 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
223 return -ENOENT;
224
225 return afs_open(inode, file);
226}
227
228/*
229 * deal with one block in an AFS directory
230 */
231static int afs_dir_iterate_block(struct dir_context *ctx,
232 union afs_dir_block *block,
233 unsigned blkoff)
234{
235 union afs_dirent *dire;
236 unsigned offset, next, curr;
237 size_t nlen;
238 int tmp;
239
240 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
241
242 curr = (ctx->pos - blkoff) / sizeof(union afs_dirent);
243
244 /* walk through the block, an entry at a time */
245 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
246 offset < AFS_DIRENT_PER_BLOCK;
247 offset = next
248 ) {
249 next = offset + 1;
250
251 /* skip entries marked unused in the bitmap */
252 if (!(block->pagehdr.bitmap[offset / 8] &
253 (1 << (offset % 8)))) {
254 _debug("ENT[%Zu.%u]: unused",
255 blkoff / sizeof(union afs_dir_block), offset);
256 if (offset >= curr)
257 ctx->pos = blkoff +
258 next * sizeof(union afs_dirent);
259 continue;
260 }
261
262 /* got a valid entry */
263 dire = &block->dirents[offset];
264 nlen = strnlen(dire->u.name,
265 sizeof(*block) -
266 offset * sizeof(union afs_dirent));
267
268 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
269 blkoff / sizeof(union afs_dir_block), offset,
270 (offset < curr ? "skip" : "fill"),
271 nlen, dire->u.name);
272
273 /* work out where the next possible entry is */
274 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
275 if (next >= AFS_DIRENT_PER_BLOCK) {
276 _debug("ENT[%Zu.%u]:"
277 " %u travelled beyond end dir block"
278 " (len %u/%Zu)",
279 blkoff / sizeof(union afs_dir_block),
280 offset, next, tmp, nlen);
281 return -EIO;
282 }
283 if (!(block->pagehdr.bitmap[next / 8] &
284 (1 << (next % 8)))) {
285 _debug("ENT[%Zu.%u]:"
286 " %u unmarked extension (len %u/%Zu)",
287 blkoff / sizeof(union afs_dir_block),
288 offset, next, tmp, nlen);
289 return -EIO;
290 }
291
292 _debug("ENT[%Zu.%u]: ext %u/%Zu",
293 blkoff / sizeof(union afs_dir_block),
294 next, tmp, nlen);
295 next++;
296 }
297
298 /* skip if starts before the current position */
299 if (offset < curr)
300 continue;
301
302 /* found the next entry */
303 if (!dir_emit(ctx, dire->u.name, nlen,
304 ntohl(dire->u.vnode),
305 ctx->actor == afs_lookup_filldir ?
306 ntohl(dire->u.unique) : DT_UNKNOWN)) {
307 _leave(" = 0 [full]");
308 return 0;
309 }
310
311 ctx->pos = blkoff + next * sizeof(union afs_dirent);
312 }
313
314 _leave(" = 1 [more]");
315 return 1;
316}
317
318/*
319 * iterate through the data blob that lists the contents of an AFS directory
320 */
321static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
322 struct key *key)
323{
324 union afs_dir_block *dblock;
325 struct afs_dir_page *dbuf;
326 struct page *page;
327 unsigned blkoff, limit;
328 int ret;
329
330 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
331
332 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
333 _leave(" = -ESTALE");
334 return -ESTALE;
335 }
336
337 /* round the file position up to the next entry boundary */
338 ctx->pos += sizeof(union afs_dirent) - 1;
339 ctx->pos &= ~(sizeof(union afs_dirent) - 1);
340
341 /* walk through the blocks in sequence */
342 ret = 0;
343 while (ctx->pos < dir->i_size) {
344 blkoff = ctx->pos & ~(sizeof(union afs_dir_block) - 1);
345
346 /* fetch the appropriate page from the directory */
347 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
348 if (IS_ERR(page)) {
349 ret = PTR_ERR(page);
350 break;
351 }
352
353 limit = blkoff & ~(PAGE_SIZE - 1);
354
355 dbuf = page_address(page);
356
357 /* deal with the individual blocks stashed on this page */
358 do {
359 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
360 sizeof(union afs_dir_block)];
361 ret = afs_dir_iterate_block(ctx, dblock, blkoff);
362 if (ret != 1) {
363 afs_dir_put_page(page);
364 goto out;
365 }
366
367 blkoff += sizeof(union afs_dir_block);
368
369 } while (ctx->pos < dir->i_size && blkoff < limit);
370
371 afs_dir_put_page(page);
372 ret = 0;
373 }
374
375out:
376 _leave(" = %d", ret);
377 return ret;
378}
379
380/*
381 * read an AFS directory
382 */
383static int afs_readdir(struct file *file, struct dir_context *ctx)
384{
385 return afs_dir_iterate(file_inode(file),
386 ctx, file->private_data);
387}
388
389/*
390 * search the directory for a name
391 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
392 * uniquifier through dtype
393 */
394static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
395 loff_t fpos, u64 ino, unsigned dtype)
396{
397 struct afs_lookup_cookie *cookie = _cookie;
398
399 _enter("{%s,%u},%s,%u,,%llu,%u",
400 cookie->name.name, cookie->name.len, name, nlen,
401 (unsigned long long) ino, dtype);
402
403 /* insanity checks first */
404 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
405 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
406
407 if (cookie->name.len != nlen ||
408 memcmp(cookie->name.name, name, nlen) != 0) {
409 _leave(" = 0 [no]");
410 return 0;
411 }
412
413 cookie->fid.vnode = ino;
414 cookie->fid.unique = dtype;
415 cookie->found = 1;
416
417 _leave(" = -1 [found]");
418 return -1;
419}
420
421/*
422 * do a lookup in a directory
423 * - just returns the FID the dentry name maps to if found
424 */
425static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
426 struct afs_fid *fid, struct key *key)
427{
428 struct afs_super_info *as = dir->i_sb->s_fs_info;
429 struct afs_lookup_cookie cookie = {
430 .ctx.actor = afs_lookup_filldir,
431 .name = dentry->d_name,
432 .fid.vid = as->volume->vid
433 };
434 int ret;
435
436 _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
437
438 /* search the directory */
439 ret = afs_dir_iterate(dir, &cookie.ctx, key);
440 if (ret < 0) {
441 _leave(" = %d [iter]", ret);
442 return ret;
443 }
444
445 ret = -ENOENT;
446 if (!cookie.found) {
447 _leave(" = -ENOENT [not found]");
448 return -ENOENT;
449 }
450
451 *fid = cookie.fid;
452 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
453 return 0;
454}
455
456/*
457 * Try to auto mount the mountpoint with pseudo directory, if the autocell
458 * operation is setted.
459 */
460static struct inode *afs_try_auto_mntpt(
461 int ret, struct dentry *dentry, struct inode *dir, struct key *key,
462 struct afs_fid *fid)
463{
464 const char *devname = dentry->d_name.name;
465 struct afs_vnode *vnode = AFS_FS_I(dir);
466 struct inode *inode;
467
468 _enter("%d, %p{%s}, {%x:%u}, %p",
469 ret, dentry, devname, vnode->fid.vid, vnode->fid.vnode, key);
470
471 if (ret != -ENOENT ||
472 !test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
473 goto out;
474
475 inode = afs_iget_autocell(dir, devname, strlen(devname), key);
476 if (IS_ERR(inode)) {
477 ret = PTR_ERR(inode);
478 goto out;
479 }
480
481 *fid = AFS_FS_I(inode)->fid;
482 _leave("= %p", inode);
483 return inode;
484
485out:
486 _leave("= %d", ret);
487 return ERR_PTR(ret);
488}
489
490/*
491 * look up an entry in a directory
492 */
493static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
494 unsigned int flags)
495{
496 struct afs_vnode *vnode;
497 struct afs_fid fid;
498 struct inode *inode;
499 struct key *key;
500 int ret;
501
502 vnode = AFS_FS_I(dir);
503
504 _enter("{%x:%u},%p{%s},",
505 vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
506
507 ASSERTCMP(dentry->d_inode, ==, NULL);
508
509 if (dentry->d_name.len >= AFSNAMEMAX) {
510 _leave(" = -ENAMETOOLONG");
511 return ERR_PTR(-ENAMETOOLONG);
512 }
513
514 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
515 _leave(" = -ESTALE");
516 return ERR_PTR(-ESTALE);
517 }
518
519 key = afs_request_key(vnode->volume->cell);
520 if (IS_ERR(key)) {
521 _leave(" = %ld [key]", PTR_ERR(key));
522 return ERR_CAST(key);
523 }
524
525 ret = afs_validate(vnode, key);
526 if (ret < 0) {
527 key_put(key);
528 _leave(" = %d [val]", ret);
529 return ERR_PTR(ret);
530 }
531
532 ret = afs_do_lookup(dir, dentry, &fid, key);
533 if (ret < 0) {
534 inode = afs_try_auto_mntpt(ret, dentry, dir, key, &fid);
535 if (!IS_ERR(inode)) {
536 key_put(key);
537 goto success;
538 }
539
540 ret = PTR_ERR(inode);
541 key_put(key);
542 if (ret == -ENOENT) {
543 d_add(dentry, NULL);
544 _leave(" = NULL [negative]");
545 return NULL;
546 }
547 _leave(" = %d [do]", ret);
548 return ERR_PTR(ret);
549 }
550 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
551
552 /* instantiate the dentry */
553 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
554 key_put(key);
555 if (IS_ERR(inode)) {
556 _leave(" = %ld", PTR_ERR(inode));
557 return ERR_CAST(inode);
558 }
559
560success:
561 d_add(dentry, inode);
562 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%u }",
563 fid.vnode,
564 fid.unique,
565 dentry->d_inode->i_ino,
566 dentry->d_inode->i_generation);
567
568 return NULL;
569}
570
571/*
572 * check that a dentry lookup hit has found a valid entry
573 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
574 * inode
575 */
576static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
577{
578 struct afs_vnode *vnode, *dir;
579 struct afs_fid uninitialized_var(fid);
580 struct dentry *parent;
581 struct key *key;
582 void *dir_version;
583 int ret;
584
585 if (flags & LOOKUP_RCU)
586 return -ECHILD;
587
588 vnode = AFS_FS_I(dentry->d_inode);
589
590 if (dentry->d_inode)
591 _enter("{v={%x:%u} n=%s fl=%lx},",
592 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
593 vnode->flags);
594 else
595 _enter("{neg n=%s}", dentry->d_name.name);
596
597 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
598 if (IS_ERR(key))
599 key = NULL;
600
601 /* lock down the parent dentry so we can peer at it */
602 parent = dget_parent(dentry);
603 dir = AFS_FS_I(parent->d_inode);
604
605 /* validate the parent directory */
606 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
607 afs_validate(dir, key);
608
609 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
610 _debug("%s: parent dir deleted", dentry->d_name.name);
611 goto out_bad;
612 }
613
614 dir_version = (void *) (unsigned long) dir->status.data_version;
615 if (dentry->d_fsdata == dir_version)
616 goto out_valid; /* the dir contents are unchanged */
617
618 _debug("dir modified");
619
620 /* search the directory for this vnode */
621 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
622 switch (ret) {
623 case 0:
624 /* the filename maps to something */
625 if (!dentry->d_inode)
626 goto out_bad;
627 if (is_bad_inode(dentry->d_inode)) {
628 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
629 parent->d_name.name, dentry->d_name.name);
630 goto out_bad;
631 }
632
633 /* if the vnode ID has changed, then the dirent points to a
634 * different file */
635 if (fid.vnode != vnode->fid.vnode) {
636 _debug("%s: dirent changed [%u != %u]",
637 dentry->d_name.name, fid.vnode,
638 vnode->fid.vnode);
639 goto not_found;
640 }
641
642 /* if the vnode ID uniqifier has changed, then the file has
643 * been deleted and replaced, and the original vnode ID has
644 * been reused */
645 if (fid.unique != vnode->fid.unique) {
646 _debug("%s: file deleted (uq %u -> %u I:%u)",
647 dentry->d_name.name, fid.unique,
648 vnode->fid.unique,
649 dentry->d_inode->i_generation);
650 spin_lock(&vnode->lock);
651 set_bit(AFS_VNODE_DELETED, &vnode->flags);
652 spin_unlock(&vnode->lock);
653 goto not_found;
654 }
655 goto out_valid;
656
657 case -ENOENT:
658 /* the filename is unknown */
659 _debug("%s: dirent not found", dentry->d_name.name);
660 if (dentry->d_inode)
661 goto not_found;
662 goto out_valid;
663
664 default:
665 _debug("failed to iterate dir %s: %d",
666 parent->d_name.name, ret);
667 goto out_bad;
668 }
669
670out_valid:
671 dentry->d_fsdata = dir_version;
672out_skip:
673 dput(parent);
674 key_put(key);
675 _leave(" = 1 [valid]");
676 return 1;
677
678 /* the dirent, if it exists, now points to a different vnode */
679not_found:
680 spin_lock(&dentry->d_lock);
681 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
682 spin_unlock(&dentry->d_lock);
683
684out_bad:
685 /* don't unhash if we have submounts */
686 if (check_submounts_and_drop(dentry) != 0)
687 goto out_skip;
688
689 _debug("dropping dentry %s/%s",
690 parent->d_name.name, dentry->d_name.name);
691 dput(parent);
692 key_put(key);
693
694 _leave(" = 0 [bad]");
695 return 0;
696}
697
698/*
699 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
700 * sleep)
701 * - called from dput() when d_count is going to 0.
702 * - return 1 to request dentry be unhashed, 0 otherwise
703 */
704static int afs_d_delete(const struct dentry *dentry)
705{
706 _enter("%s", dentry->d_name.name);
707
708 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
709 goto zap;
710
711 if (dentry->d_inode &&
712 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags) ||
713 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(dentry->d_inode)->flags)))
714 goto zap;
715
716 _leave(" = 0 [keep]");
717 return 0;
718
719zap:
720 _leave(" = 1 [zap]");
721 return 1;
722}
723
724/*
725 * handle dentry release
726 */
727static void afs_d_release(struct dentry *dentry)
728{
729 _enter("%s", dentry->d_name.name);
730}
731
732/*
733 * create a directory on an AFS filesystem
734 */
735static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
736{
737 struct afs_file_status status;
738 struct afs_callback cb;
739 struct afs_server *server;
740 struct afs_vnode *dvnode, *vnode;
741 struct afs_fid fid;
742 struct inode *inode;
743 struct key *key;
744 int ret;
745
746 dvnode = AFS_FS_I(dir);
747
748 _enter("{%x:%u},{%s},%ho",
749 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
750
751 key = afs_request_key(dvnode->volume->cell);
752 if (IS_ERR(key)) {
753 ret = PTR_ERR(key);
754 goto error;
755 }
756
757 mode |= S_IFDIR;
758 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
759 mode, &fid, &status, &cb, &server);
760 if (ret < 0)
761 goto mkdir_error;
762
763 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
764 if (IS_ERR(inode)) {
765 /* ENOMEM at a really inconvenient time - just abandon the new
766 * directory on the server */
767 ret = PTR_ERR(inode);
768 goto iget_error;
769 }
770
771 /* apply the status report we've got for the new vnode */
772 vnode = AFS_FS_I(inode);
773 spin_lock(&vnode->lock);
774 vnode->update_cnt++;
775 spin_unlock(&vnode->lock);
776 afs_vnode_finalise_status_update(vnode, server);
777 afs_put_server(server);
778
779 d_instantiate(dentry, inode);
780 if (d_unhashed(dentry)) {
781 _debug("not hashed");
782 d_rehash(dentry);
783 }
784 key_put(key);
785 _leave(" = 0");
786 return 0;
787
788iget_error:
789 afs_put_server(server);
790mkdir_error:
791 key_put(key);
792error:
793 d_drop(dentry);
794 _leave(" = %d", ret);
795 return ret;
796}
797
798/*
799 * remove a directory from an AFS filesystem
800 */
801static int afs_rmdir(struct inode *dir, struct dentry *dentry)
802{
803 struct afs_vnode *dvnode, *vnode;
804 struct key *key;
805 int ret;
806
807 dvnode = AFS_FS_I(dir);
808
809 _enter("{%x:%u},{%s}",
810 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
811
812 key = afs_request_key(dvnode->volume->cell);
813 if (IS_ERR(key)) {
814 ret = PTR_ERR(key);
815 goto error;
816 }
817
818 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
819 if (ret < 0)
820 goto rmdir_error;
821
822 if (dentry->d_inode) {
823 vnode = AFS_FS_I(dentry->d_inode);
824 clear_nlink(&vnode->vfs_inode);
825 set_bit(AFS_VNODE_DELETED, &vnode->flags);
826 afs_discard_callback_on_delete(vnode);
827 }
828
829 key_put(key);
830 _leave(" = 0");
831 return 0;
832
833rmdir_error:
834 key_put(key);
835error:
836 _leave(" = %d", ret);
837 return ret;
838}
839
840/*
841 * remove a file from an AFS filesystem
842 */
843static int afs_unlink(struct inode *dir, struct dentry *dentry)
844{
845 struct afs_vnode *dvnode, *vnode;
846 struct key *key;
847 int ret;
848
849 dvnode = AFS_FS_I(dir);
850
851 _enter("{%x:%u},{%s}",
852 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
853
854 ret = -ENAMETOOLONG;
855 if (dentry->d_name.len >= AFSNAMEMAX)
856 goto error;
857
858 key = afs_request_key(dvnode->volume->cell);
859 if (IS_ERR(key)) {
860 ret = PTR_ERR(key);
861 goto error;
862 }
863
864 if (dentry->d_inode) {
865 vnode = AFS_FS_I(dentry->d_inode);
866
867 /* make sure we have a callback promise on the victim */
868 ret = afs_validate(vnode, key);
869 if (ret < 0)
870 goto error;
871 }
872
873 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
874 if (ret < 0)
875 goto remove_error;
876
877 if (dentry->d_inode) {
878 /* if the file wasn't deleted due to excess hard links, the
879 * fileserver will break the callback promise on the file - if
880 * it had one - before it returns to us, and if it was deleted,
881 * it won't
882 *
883 * however, if we didn't have a callback promise outstanding,
884 * or it was outstanding on a different server, then it won't
885 * break it either...
886 */
887 vnode = AFS_FS_I(dentry->d_inode);
888 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
889 _debug("AFS_VNODE_DELETED");
890 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
891 _debug("AFS_VNODE_CB_BROKEN");
892 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
893 ret = afs_validate(vnode, key);
894 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
895 }
896
897 key_put(key);
898 _leave(" = 0");
899 return 0;
900
901remove_error:
902 key_put(key);
903error:
904 _leave(" = %d", ret);
905 return ret;
906}
907
908/*
909 * create a regular file on an AFS filesystem
910 */
911static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
912 bool excl)
913{
914 struct afs_file_status status;
915 struct afs_callback cb;
916 struct afs_server *server;
917 struct afs_vnode *dvnode, *vnode;
918 struct afs_fid fid;
919 struct inode *inode;
920 struct key *key;
921 int ret;
922
923 dvnode = AFS_FS_I(dir);
924
925 _enter("{%x:%u},{%s},%ho,",
926 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
927
928 key = afs_request_key(dvnode->volume->cell);
929 if (IS_ERR(key)) {
930 ret = PTR_ERR(key);
931 goto error;
932 }
933
934 mode |= S_IFREG;
935 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
936 mode, &fid, &status, &cb, &server);
937 if (ret < 0)
938 goto create_error;
939
940 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
941 if (IS_ERR(inode)) {
942 /* ENOMEM at a really inconvenient time - just abandon the new
943 * directory on the server */
944 ret = PTR_ERR(inode);
945 goto iget_error;
946 }
947
948 /* apply the status report we've got for the new vnode */
949 vnode = AFS_FS_I(inode);
950 spin_lock(&vnode->lock);
951 vnode->update_cnt++;
952 spin_unlock(&vnode->lock);
953 afs_vnode_finalise_status_update(vnode, server);
954 afs_put_server(server);
955
956 d_instantiate(dentry, inode);
957 if (d_unhashed(dentry)) {
958 _debug("not hashed");
959 d_rehash(dentry);
960 }
961 key_put(key);
962 _leave(" = 0");
963 return 0;
964
965iget_error:
966 afs_put_server(server);
967create_error:
968 key_put(key);
969error:
970 d_drop(dentry);
971 _leave(" = %d", ret);
972 return ret;
973}
974
975/*
976 * create a hard link between files in an AFS filesystem
977 */
978static int afs_link(struct dentry *from, struct inode *dir,
979 struct dentry *dentry)
980{
981 struct afs_vnode *dvnode, *vnode;
982 struct key *key;
983 int ret;
984
985 vnode = AFS_FS_I(from->d_inode);
986 dvnode = AFS_FS_I(dir);
987
988 _enter("{%x:%u},{%x:%u},{%s}",
989 vnode->fid.vid, vnode->fid.vnode,
990 dvnode->fid.vid, dvnode->fid.vnode,
991 dentry->d_name.name);
992
993 key = afs_request_key(dvnode->volume->cell);
994 if (IS_ERR(key)) {
995 ret = PTR_ERR(key);
996 goto error;
997 }
998
999 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
1000 if (ret < 0)
1001 goto link_error;
1002
1003 ihold(&vnode->vfs_inode);
1004 d_instantiate(dentry, &vnode->vfs_inode);
1005 key_put(key);
1006 _leave(" = 0");
1007 return 0;
1008
1009link_error:
1010 key_put(key);
1011error:
1012 d_drop(dentry);
1013 _leave(" = %d", ret);
1014 return ret;
1015}
1016
1017/*
1018 * create a symlink in an AFS filesystem
1019 */
1020static int afs_symlink(struct inode *dir, struct dentry *dentry,
1021 const char *content)
1022{
1023 struct afs_file_status status;
1024 struct afs_server *server;
1025 struct afs_vnode *dvnode, *vnode;
1026 struct afs_fid fid;
1027 struct inode *inode;
1028 struct key *key;
1029 int ret;
1030
1031 dvnode = AFS_FS_I(dir);
1032
1033 _enter("{%x:%u},{%s},%s",
1034 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
1035 content);
1036
1037 ret = -EINVAL;
1038 if (strlen(content) >= AFSPATHMAX)
1039 goto error;
1040
1041 key = afs_request_key(dvnode->volume->cell);
1042 if (IS_ERR(key)) {
1043 ret = PTR_ERR(key);
1044 goto error;
1045 }
1046
1047 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1048 &fid, &status, &server);
1049 if (ret < 0)
1050 goto create_error;
1051
1052 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1053 if (IS_ERR(inode)) {
1054 /* ENOMEM at a really inconvenient time - just abandon the new
1055 * directory on the server */
1056 ret = PTR_ERR(inode);
1057 goto iget_error;
1058 }
1059
1060 /* apply the status report we've got for the new vnode */
1061 vnode = AFS_FS_I(inode);
1062 spin_lock(&vnode->lock);
1063 vnode->update_cnt++;
1064 spin_unlock(&vnode->lock);
1065 afs_vnode_finalise_status_update(vnode, server);
1066 afs_put_server(server);
1067
1068 d_instantiate(dentry, inode);
1069 if (d_unhashed(dentry)) {
1070 _debug("not hashed");
1071 d_rehash(dentry);
1072 }
1073 key_put(key);
1074 _leave(" = 0");
1075 return 0;
1076
1077iget_error:
1078 afs_put_server(server);
1079create_error:
1080 key_put(key);
1081error:
1082 d_drop(dentry);
1083 _leave(" = %d", ret);
1084 return ret;
1085}
1086
1087/*
1088 * rename a file in an AFS filesystem and/or move it between directories
1089 */
1090static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1091 struct inode *new_dir, struct dentry *new_dentry)
1092{
1093 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1094 struct key *key;
1095 int ret;
1096
1097 vnode = AFS_FS_I(old_dentry->d_inode);
1098 orig_dvnode = AFS_FS_I(old_dir);
1099 new_dvnode = AFS_FS_I(new_dir);
1100
1101 _enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
1102 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1103 vnode->fid.vid, vnode->fid.vnode,
1104 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1105 new_dentry->d_name.name);
1106
1107 key = afs_request_key(orig_dvnode->volume->cell);
1108 if (IS_ERR(key)) {
1109 ret = PTR_ERR(key);
1110 goto error;
1111 }
1112
1113 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1114 old_dentry->d_name.name,
1115 new_dentry->d_name.name);
1116 if (ret < 0)
1117 goto rename_error;
1118 key_put(key);
1119 _leave(" = 0");
1120 return 0;
1121
1122rename_error:
1123 key_put(key);
1124error:
1125 d_drop(new_dentry);
1126 _leave(" = %d", ret);
1127 return ret;
1128}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* dir.c: AFS filesystem directory handling
3 *
4 * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/kernel.h>
9#include <linux/fs.h>
10#include <linux/namei.h>
11#include <linux/pagemap.h>
12#include <linux/swap.h>
13#include <linux/ctype.h>
14#include <linux/sched.h>
15#include <linux/task_io_accounting_ops.h>
16#include "internal.h"
17#include "afs_fs.h"
18#include "xdr_fs.h"
19
20static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
21 unsigned int flags);
22static int afs_dir_open(struct inode *inode, struct file *file);
23static int afs_readdir(struct file *file, struct dir_context *ctx);
24static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
25static int afs_d_delete(const struct dentry *dentry);
26static void afs_d_iput(struct dentry *dentry, struct inode *inode);
27static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
28 loff_t fpos, u64 ino, unsigned dtype);
29static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
30 loff_t fpos, u64 ino, unsigned dtype);
31static int afs_create(struct user_namespace *mnt_userns, struct inode *dir,
32 struct dentry *dentry, umode_t mode, bool excl);
33static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
34 struct dentry *dentry, umode_t mode);
35static int afs_rmdir(struct inode *dir, struct dentry *dentry);
36static int afs_unlink(struct inode *dir, struct dentry *dentry);
37static int afs_link(struct dentry *from, struct inode *dir,
38 struct dentry *dentry);
39static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
40 struct dentry *dentry, const char *content);
41static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
42 struct dentry *old_dentry, struct inode *new_dir,
43 struct dentry *new_dentry, unsigned int flags);
44static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
45static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
46 unsigned int length);
47
48static int afs_dir_set_page_dirty(struct page *page)
49{
50 BUG(); /* This should never happen. */
51}
52
53const struct file_operations afs_dir_file_operations = {
54 .open = afs_dir_open,
55 .release = afs_release,
56 .iterate_shared = afs_readdir,
57 .lock = afs_lock,
58 .llseek = generic_file_llseek,
59};
60
61const struct inode_operations afs_dir_inode_operations = {
62 .create = afs_create,
63 .lookup = afs_lookup,
64 .link = afs_link,
65 .unlink = afs_unlink,
66 .symlink = afs_symlink,
67 .mkdir = afs_mkdir,
68 .rmdir = afs_rmdir,
69 .rename = afs_rename,
70 .permission = afs_permission,
71 .getattr = afs_getattr,
72 .setattr = afs_setattr,
73};
74
75const struct address_space_operations afs_dir_aops = {
76 .set_page_dirty = afs_dir_set_page_dirty,
77 .releasepage = afs_dir_releasepage,
78 .invalidatepage = afs_dir_invalidatepage,
79};
80
81const struct dentry_operations afs_fs_dentry_operations = {
82 .d_revalidate = afs_d_revalidate,
83 .d_delete = afs_d_delete,
84 .d_release = afs_d_release,
85 .d_automount = afs_d_automount,
86 .d_iput = afs_d_iput,
87};
88
89struct afs_lookup_one_cookie {
90 struct dir_context ctx;
91 struct qstr name;
92 bool found;
93 struct afs_fid fid;
94};
95
96struct afs_lookup_cookie {
97 struct dir_context ctx;
98 struct qstr name;
99 bool found;
100 bool one_only;
101 unsigned short nr_fids;
102 struct afs_fid fids[50];
103};
104
105/*
106 * Drop the refs that we're holding on the pages we were reading into. We've
107 * got refs on the first nr_pages pages.
108 */
109static void afs_dir_read_cleanup(struct afs_read *req)
110{
111 struct address_space *mapping = req->vnode->vfs_inode.i_mapping;
112 struct page *page;
113 pgoff_t last = req->nr_pages - 1;
114
115 XA_STATE(xas, &mapping->i_pages, 0);
116
117 if (unlikely(!req->nr_pages))
118 return;
119
120 rcu_read_lock();
121 xas_for_each(&xas, page, last) {
122 if (xas_retry(&xas, page))
123 continue;
124 BUG_ON(xa_is_value(page));
125 BUG_ON(PageCompound(page));
126 ASSERTCMP(page->mapping, ==, mapping);
127
128 put_page(page);
129 }
130
131 rcu_read_unlock();
132}
133
134/*
135 * check that a directory page is valid
136 */
137static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
138 loff_t i_size)
139{
140 struct afs_xdr_dir_page *dbuf;
141 loff_t latter, off;
142 int tmp, qty;
143
144 /* Determine how many magic numbers there should be in this page, but
145 * we must take care because the directory may change size under us.
146 */
147 off = page_offset(page);
148 if (i_size <= off)
149 goto checked;
150
151 latter = i_size - off;
152 if (latter >= PAGE_SIZE)
153 qty = PAGE_SIZE;
154 else
155 qty = latter;
156 qty /= sizeof(union afs_xdr_dir_block);
157
158 /* check them */
159 dbuf = kmap_atomic(page);
160 for (tmp = 0; tmp < qty; tmp++) {
161 if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
162 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
163 __func__, dvnode->vfs_inode.i_ino, tmp, qty,
164 ntohs(dbuf->blocks[tmp].hdr.magic));
165 trace_afs_dir_check_failed(dvnode, off, i_size);
166 kunmap(page);
167 trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
168 goto error;
169 }
170
171 /* Make sure each block is NUL terminated so we can reasonably
172 * use string functions on it. The filenames in the page
173 * *should* be NUL-terminated anyway.
174 */
175 ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
176 }
177
178 kunmap_atomic(dbuf);
179
180checked:
181 afs_stat_v(dvnode, n_read_dir);
182 return true;
183
184error:
185 return false;
186}
187
188/*
189 * Dump the contents of a directory.
190 */
191static void afs_dir_dump(struct afs_vnode *dvnode, struct afs_read *req)
192{
193 struct afs_xdr_dir_page *dbuf;
194 struct address_space *mapping = dvnode->vfs_inode.i_mapping;
195 struct page *page;
196 unsigned int i, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
197 pgoff_t last = req->nr_pages - 1;
198
199 XA_STATE(xas, &mapping->i_pages, 0);
200
201 pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx\n",
202 dvnode->fid.vid, dvnode->fid.vnode,
203 req->file_size, req->len, req->actual_len);
204 pr_warn("DIR %llx %x %zx %zx\n",
205 req->pos, req->nr_pages,
206 req->iter->iov_offset, iov_iter_count(req->iter));
207
208 xas_for_each(&xas, page, last) {
209 if (xas_retry(&xas, page))
210 continue;
211
212 BUG_ON(PageCompound(page));
213 BUG_ON(page->mapping != mapping);
214
215 dbuf = kmap_atomic(page);
216 for (i = 0; i < qty; i++) {
217 union afs_xdr_dir_block *block = &dbuf->blocks[i];
218
219 pr_warn("[%02lx] %32phN\n", page->index * qty + i, block);
220 }
221 kunmap_atomic(dbuf);
222 }
223}
224
225/*
226 * Check all the pages in a directory. All the pages are held pinned.
227 */
228static int afs_dir_check(struct afs_vnode *dvnode, struct afs_read *req)
229{
230 struct address_space *mapping = dvnode->vfs_inode.i_mapping;
231 struct page *page;
232 pgoff_t last = req->nr_pages - 1;
233 int ret = 0;
234
235 XA_STATE(xas, &mapping->i_pages, 0);
236
237 if (unlikely(!req->nr_pages))
238 return 0;
239
240 rcu_read_lock();
241 xas_for_each(&xas, page, last) {
242 if (xas_retry(&xas, page))
243 continue;
244
245 BUG_ON(PageCompound(page));
246 BUG_ON(page->mapping != mapping);
247
248 if (!afs_dir_check_page(dvnode, page, req->file_size)) {
249 afs_dir_dump(dvnode, req);
250 ret = -EIO;
251 break;
252 }
253 }
254
255 rcu_read_unlock();
256 return ret;
257}
258
259/*
260 * open an AFS directory file
261 */
262static int afs_dir_open(struct inode *inode, struct file *file)
263{
264 _enter("{%lu}", inode->i_ino);
265
266 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
267 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
268
269 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
270 return -ENOENT;
271
272 return afs_open(inode, file);
273}
274
275/*
276 * Read the directory into the pagecache in one go, scrubbing the previous
277 * contents. The list of pages is returned, pinning them so that they don't
278 * get reclaimed during the iteration.
279 */
280static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
281 __acquires(&dvnode->validate_lock)
282{
283 struct afs_read *req;
284 loff_t i_size;
285 int nr_pages, i, n;
286 int ret;
287
288 _enter("");
289
290 req = kzalloc(sizeof(*req), GFP_KERNEL);
291 if (!req)
292 return ERR_PTR(-ENOMEM);
293
294 refcount_set(&req->usage, 1);
295 req->vnode = dvnode;
296 req->key = key_get(key);
297 req->cleanup = afs_dir_read_cleanup;
298
299expand:
300 i_size = i_size_read(&dvnode->vfs_inode);
301 if (i_size < 2048) {
302 ret = afs_bad(dvnode, afs_file_error_dir_small);
303 goto error;
304 }
305 if (i_size > 2048 * 1024) {
306 trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
307 ret = -EFBIG;
308 goto error;
309 }
310
311 _enter("%llu", i_size);
312
313 nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
314
315 req->actual_len = i_size; /* May change */
316 req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
317 req->data_version = dvnode->status.data_version; /* May change */
318 iov_iter_xarray(&req->def_iter, READ, &dvnode->vfs_inode.i_mapping->i_pages,
319 0, i_size);
320 req->iter = &req->def_iter;
321
322 /* Fill in any gaps that we might find where the memory reclaimer has
323 * been at work and pin all the pages. If there are any gaps, we will
324 * need to reread the entire directory contents.
325 */
326 i = req->nr_pages;
327 while (i < nr_pages) {
328 struct page *pages[8], *page;
329
330 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
331 min_t(unsigned int, nr_pages - i,
332 ARRAY_SIZE(pages)),
333 pages);
334 _debug("find %u at %u/%u", n, i, nr_pages);
335
336 if (n == 0) {
337 gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
338
339 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
340 afs_stat_v(dvnode, n_inval);
341
342 ret = -ENOMEM;
343 page = __page_cache_alloc(gfp);
344 if (!page)
345 goto error;
346 ret = add_to_page_cache_lru(page,
347 dvnode->vfs_inode.i_mapping,
348 i, gfp);
349 if (ret < 0)
350 goto error;
351
352 attach_page_private(page, (void *)1);
353 unlock_page(page);
354 req->nr_pages++;
355 i++;
356 } else {
357 req->nr_pages += n;
358 i += n;
359 }
360 }
361
362 /* If we're going to reload, we need to lock all the pages to prevent
363 * races.
364 */
365 ret = -ERESTARTSYS;
366 if (down_read_killable(&dvnode->validate_lock) < 0)
367 goto error;
368
369 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
370 goto success;
371
372 up_read(&dvnode->validate_lock);
373 if (down_write_killable(&dvnode->validate_lock) < 0)
374 goto error;
375
376 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
377 trace_afs_reload_dir(dvnode);
378 ret = afs_fetch_data(dvnode, req);
379 if (ret < 0)
380 goto error_unlock;
381
382 task_io_account_read(PAGE_SIZE * req->nr_pages);
383
384 if (req->len < req->file_size) {
385 /* The content has grown, so we need to expand the
386 * buffer.
387 */
388 up_write(&dvnode->validate_lock);
389 goto expand;
390 }
391
392 /* Validate the data we just read. */
393 ret = afs_dir_check(dvnode, req);
394 if (ret < 0)
395 goto error_unlock;
396
397 // TODO: Trim excess pages
398
399 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
400 }
401
402 downgrade_write(&dvnode->validate_lock);
403success:
404 return req;
405
406error_unlock:
407 up_write(&dvnode->validate_lock);
408error:
409 afs_put_read(req);
410 _leave(" = %d", ret);
411 return ERR_PTR(ret);
412}
413
414/*
415 * deal with one block in an AFS directory
416 */
417static int afs_dir_iterate_block(struct afs_vnode *dvnode,
418 struct dir_context *ctx,
419 union afs_xdr_dir_block *block,
420 unsigned blkoff)
421{
422 union afs_xdr_dirent *dire;
423 unsigned offset, next, curr, nr_slots;
424 size_t nlen;
425 int tmp;
426
427 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
428
429 curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
430
431 /* walk through the block, an entry at a time */
432 for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
433 offset < AFS_DIR_SLOTS_PER_BLOCK;
434 offset = next
435 ) {
436 /* skip entries marked unused in the bitmap */
437 if (!(block->hdr.bitmap[offset / 8] &
438 (1 << (offset % 8)))) {
439 _debug("ENT[%zu.%u]: unused",
440 blkoff / sizeof(union afs_xdr_dir_block), offset);
441 next = offset + 1;
442 if (offset >= curr)
443 ctx->pos = blkoff +
444 next * sizeof(union afs_xdr_dirent);
445 continue;
446 }
447
448 /* got a valid entry */
449 dire = &block->dirents[offset];
450 nlen = strnlen(dire->u.name,
451 sizeof(*block) -
452 offset * sizeof(union afs_xdr_dirent));
453 if (nlen > AFSNAMEMAX - 1) {
454 _debug("ENT[%zu]: name too long (len %u/%zu)",
455 blkoff / sizeof(union afs_xdr_dir_block),
456 offset, nlen);
457 return afs_bad(dvnode, afs_file_error_dir_name_too_long);
458 }
459
460 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
461 blkoff / sizeof(union afs_xdr_dir_block), offset,
462 (offset < curr ? "skip" : "fill"),
463 nlen, dire->u.name);
464
465 nr_slots = afs_dir_calc_slots(nlen);
466 next = offset + nr_slots;
467 if (next > AFS_DIR_SLOTS_PER_BLOCK) {
468 _debug("ENT[%zu.%u]:"
469 " %u extends beyond end dir block"
470 " (len %zu)",
471 blkoff / sizeof(union afs_xdr_dir_block),
472 offset, next, nlen);
473 return afs_bad(dvnode, afs_file_error_dir_over_end);
474 }
475
476 /* Check that the name-extension dirents are all allocated */
477 for (tmp = 1; tmp < nr_slots; tmp++) {
478 unsigned int ix = offset + tmp;
479 if (!(block->hdr.bitmap[ix / 8] & (1 << (ix % 8)))) {
480 _debug("ENT[%zu.u]:"
481 " %u unmarked extension (%u/%u)",
482 blkoff / sizeof(union afs_xdr_dir_block),
483 offset, tmp, nr_slots);
484 return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
485 }
486 }
487
488 /* skip if starts before the current position */
489 if (offset < curr)
490 continue;
491
492 /* found the next entry */
493 if (!dir_emit(ctx, dire->u.name, nlen,
494 ntohl(dire->u.vnode),
495 (ctx->actor == afs_lookup_filldir ||
496 ctx->actor == afs_lookup_one_filldir)?
497 ntohl(dire->u.unique) : DT_UNKNOWN)) {
498 _leave(" = 0 [full]");
499 return 0;
500 }
501
502 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
503 }
504
505 _leave(" = 1 [more]");
506 return 1;
507}
508
509/*
510 * iterate through the data blob that lists the contents of an AFS directory
511 */
512static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
513 struct key *key, afs_dataversion_t *_dir_version)
514{
515 struct afs_vnode *dvnode = AFS_FS_I(dir);
516 struct afs_xdr_dir_page *dbuf;
517 union afs_xdr_dir_block *dblock;
518 struct afs_read *req;
519 struct page *page;
520 unsigned blkoff, limit;
521 void __rcu **slot;
522 int ret;
523
524 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
525
526 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
527 _leave(" = -ESTALE");
528 return -ESTALE;
529 }
530
531 req = afs_read_dir(dvnode, key);
532 if (IS_ERR(req))
533 return PTR_ERR(req);
534 *_dir_version = req->data_version;
535
536 /* round the file position up to the next entry boundary */
537 ctx->pos += sizeof(union afs_xdr_dirent) - 1;
538 ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
539
540 /* walk through the blocks in sequence */
541 ret = 0;
542 while (ctx->pos < req->actual_len) {
543 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
544
545 /* Fetch the appropriate page from the directory and re-add it
546 * to the LRU. We have all the pages pinned with an extra ref.
547 */
548 rcu_read_lock();
549 page = NULL;
550 slot = radix_tree_lookup_slot(&dvnode->vfs_inode.i_mapping->i_pages,
551 blkoff / PAGE_SIZE);
552 if (slot)
553 page = radix_tree_deref_slot(slot);
554 rcu_read_unlock();
555 if (!page) {
556 ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
557 break;
558 }
559 mark_page_accessed(page);
560
561 limit = blkoff & ~(PAGE_SIZE - 1);
562
563 dbuf = kmap(page);
564
565 /* deal with the individual blocks stashed on this page */
566 do {
567 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
568 sizeof(union afs_xdr_dir_block)];
569 ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
570 if (ret != 1) {
571 kunmap(page);
572 goto out;
573 }
574
575 blkoff += sizeof(union afs_xdr_dir_block);
576
577 } while (ctx->pos < dir->i_size && blkoff < limit);
578
579 kunmap(page);
580 ret = 0;
581 }
582
583out:
584 up_read(&dvnode->validate_lock);
585 afs_put_read(req);
586 _leave(" = %d", ret);
587 return ret;
588}
589
590/*
591 * read an AFS directory
592 */
593static int afs_readdir(struct file *file, struct dir_context *ctx)
594{
595 afs_dataversion_t dir_version;
596
597 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file),
598 &dir_version);
599}
600
601/*
602 * Search the directory for a single name
603 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
604 * uniquifier through dtype
605 */
606static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
607 int nlen, loff_t fpos, u64 ino, unsigned dtype)
608{
609 struct afs_lookup_one_cookie *cookie =
610 container_of(ctx, struct afs_lookup_one_cookie, ctx);
611
612 _enter("{%s,%u},%s,%u,,%llu,%u",
613 cookie->name.name, cookie->name.len, name, nlen,
614 (unsigned long long) ino, dtype);
615
616 /* insanity checks first */
617 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
618 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
619
620 if (cookie->name.len != nlen ||
621 memcmp(cookie->name.name, name, nlen) != 0) {
622 _leave(" = 0 [no]");
623 return 0;
624 }
625
626 cookie->fid.vnode = ino;
627 cookie->fid.unique = dtype;
628 cookie->found = 1;
629
630 _leave(" = -1 [found]");
631 return -1;
632}
633
634/*
635 * Do a lookup of a single name in a directory
636 * - just returns the FID the dentry name maps to if found
637 */
638static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
639 struct afs_fid *fid, struct key *key,
640 afs_dataversion_t *_dir_version)
641{
642 struct afs_super_info *as = dir->i_sb->s_fs_info;
643 struct afs_lookup_one_cookie cookie = {
644 .ctx.actor = afs_lookup_one_filldir,
645 .name = dentry->d_name,
646 .fid.vid = as->volume->vid
647 };
648 int ret;
649
650 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
651
652 /* search the directory */
653 ret = afs_dir_iterate(dir, &cookie.ctx, key, _dir_version);
654 if (ret < 0) {
655 _leave(" = %d [iter]", ret);
656 return ret;
657 }
658
659 if (!cookie.found) {
660 _leave(" = -ENOENT [not found]");
661 return -ENOENT;
662 }
663
664 *fid = cookie.fid;
665 _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
666 return 0;
667}
668
669/*
670 * search the directory for a name
671 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
672 * uniquifier through dtype
673 */
674static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
675 int nlen, loff_t fpos, u64 ino, unsigned dtype)
676{
677 struct afs_lookup_cookie *cookie =
678 container_of(ctx, struct afs_lookup_cookie, ctx);
679 int ret;
680
681 _enter("{%s,%u},%s,%u,,%llu,%u",
682 cookie->name.name, cookie->name.len, name, nlen,
683 (unsigned long long) ino, dtype);
684
685 /* insanity checks first */
686 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
687 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
688
689 if (cookie->found) {
690 if (cookie->nr_fids < 50) {
691 cookie->fids[cookie->nr_fids].vnode = ino;
692 cookie->fids[cookie->nr_fids].unique = dtype;
693 cookie->nr_fids++;
694 }
695 } else if (cookie->name.len == nlen &&
696 memcmp(cookie->name.name, name, nlen) == 0) {
697 cookie->fids[1].vnode = ino;
698 cookie->fids[1].unique = dtype;
699 cookie->found = 1;
700 if (cookie->one_only)
701 return -1;
702 }
703
704 ret = cookie->nr_fids >= 50 ? -1 : 0;
705 _leave(" = %d", ret);
706 return ret;
707}
708
709/*
710 * Deal with the result of a successful lookup operation. Turn all the files
711 * into inodes and save the first one - which is the one we actually want.
712 */
713static void afs_do_lookup_success(struct afs_operation *op)
714{
715 struct afs_vnode_param *vp;
716 struct afs_vnode *vnode;
717 struct inode *inode;
718 u32 abort_code;
719 int i;
720
721 _enter("");
722
723 for (i = 0; i < op->nr_files; i++) {
724 switch (i) {
725 case 0:
726 vp = &op->file[0];
727 abort_code = vp->scb.status.abort_code;
728 if (abort_code != 0) {
729 op->ac.abort_code = abort_code;
730 op->error = afs_abort_to_error(abort_code);
731 }
732 break;
733
734 case 1:
735 vp = &op->file[1];
736 break;
737
738 default:
739 vp = &op->more_files[i - 2];
740 break;
741 }
742
743 if (!vp->scb.have_status && !vp->scb.have_error)
744 continue;
745
746 _debug("do [%u]", i);
747 if (vp->vnode) {
748 if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags))
749 afs_vnode_commit_status(op, vp);
750 } else if (vp->scb.status.abort_code == 0) {
751 inode = afs_iget(op, vp);
752 if (!IS_ERR(inode)) {
753 vnode = AFS_FS_I(inode);
754 afs_cache_permit(vnode, op->key,
755 0 /* Assume vnode->cb_break is 0 */ +
756 op->cb_v_break,
757 &vp->scb);
758 vp->vnode = vnode;
759 vp->put_vnode = true;
760 }
761 } else {
762 _debug("- abort %d %llx:%llx.%x",
763 vp->scb.status.abort_code,
764 vp->fid.vid, vp->fid.vnode, vp->fid.unique);
765 }
766 }
767
768 _leave("");
769}
770
771static const struct afs_operation_ops afs_inline_bulk_status_operation = {
772 .issue_afs_rpc = afs_fs_inline_bulk_status,
773 .issue_yfs_rpc = yfs_fs_inline_bulk_status,
774 .success = afs_do_lookup_success,
775};
776
777static const struct afs_operation_ops afs_lookup_fetch_status_operation = {
778 .issue_afs_rpc = afs_fs_fetch_status,
779 .issue_yfs_rpc = yfs_fs_fetch_status,
780 .success = afs_do_lookup_success,
781 .aborted = afs_check_for_remote_deletion,
782};
783
784/*
785 * See if we know that the server we expect to use doesn't support
786 * FS.InlineBulkStatus.
787 */
788static bool afs_server_supports_ibulk(struct afs_vnode *dvnode)
789{
790 struct afs_server_list *slist;
791 struct afs_volume *volume = dvnode->volume;
792 struct afs_server *server;
793 bool ret = true;
794 int i;
795
796 if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags))
797 return true;
798
799 rcu_read_lock();
800 slist = rcu_dereference(volume->servers);
801
802 for (i = 0; i < slist->nr_servers; i++) {
803 server = slist->servers[i].server;
804 if (server == dvnode->cb_server) {
805 if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
806 ret = false;
807 break;
808 }
809 }
810
811 rcu_read_unlock();
812 return ret;
813}
814
815/*
816 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
817 * files in one go and create inodes for them. The inode of the file we were
818 * asked for is returned.
819 */
820static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
821 struct key *key)
822{
823 struct afs_lookup_cookie *cookie;
824 struct afs_vnode_param *vp;
825 struct afs_operation *op;
826 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
827 struct inode *inode = NULL, *ti;
828 afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version);
829 long ret;
830 int i;
831
832 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
833
834 cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
835 if (!cookie)
836 return ERR_PTR(-ENOMEM);
837
838 for (i = 0; i < ARRAY_SIZE(cookie->fids); i++)
839 cookie->fids[i].vid = dvnode->fid.vid;
840 cookie->ctx.actor = afs_lookup_filldir;
841 cookie->name = dentry->d_name;
842 cookie->nr_fids = 2; /* slot 0 is saved for the fid we actually want
843 * and slot 1 for the directory */
844
845 if (!afs_server_supports_ibulk(dvnode))
846 cookie->one_only = true;
847
848 /* search the directory */
849 ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version);
850 if (ret < 0)
851 goto out;
852
853 dentry->d_fsdata = (void *)(unsigned long)data_version;
854
855 ret = -ENOENT;
856 if (!cookie->found)
857 goto out;
858
859 /* Check to see if we already have an inode for the primary fid. */
860 inode = ilookup5(dir->i_sb, cookie->fids[1].vnode,
861 afs_ilookup5_test_by_fid, &cookie->fids[1]);
862 if (inode)
863 goto out; /* We do */
864
865 /* Okay, we didn't find it. We need to query the server - and whilst
866 * we're doing that, we're going to attempt to look up a bunch of other
867 * vnodes also.
868 */
869 op = afs_alloc_operation(NULL, dvnode->volume);
870 if (IS_ERR(op)) {
871 ret = PTR_ERR(op);
872 goto out;
873 }
874
875 afs_op_set_vnode(op, 0, dvnode);
876 afs_op_set_fid(op, 1, &cookie->fids[1]);
877
878 op->nr_files = cookie->nr_fids;
879 _debug("nr_files %u", op->nr_files);
880
881 /* Need space for examining all the selected files */
882 op->error = -ENOMEM;
883 if (op->nr_files > 2) {
884 op->more_files = kvcalloc(op->nr_files - 2,
885 sizeof(struct afs_vnode_param),
886 GFP_KERNEL);
887 if (!op->more_files)
888 goto out_op;
889
890 for (i = 2; i < op->nr_files; i++) {
891 vp = &op->more_files[i - 2];
892 vp->fid = cookie->fids[i];
893
894 /* Find any inodes that already exist and get their
895 * callback counters.
896 */
897 ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode,
898 afs_ilookup5_test_by_fid, &vp->fid);
899 if (!IS_ERR_OR_NULL(ti)) {
900 vnode = AFS_FS_I(ti);
901 vp->dv_before = vnode->status.data_version;
902 vp->cb_break_before = afs_calc_vnode_cb_break(vnode);
903 vp->vnode = vnode;
904 vp->put_vnode = true;
905 vp->speculative = true; /* vnode not locked */
906 }
907 }
908 }
909
910 /* Try FS.InlineBulkStatus first. Abort codes for the individual
911 * lookups contained therein are stored in the reply without aborting
912 * the whole operation.
913 */
914 op->error = -ENOTSUPP;
915 if (!cookie->one_only) {
916 op->ops = &afs_inline_bulk_status_operation;
917 afs_begin_vnode_operation(op);
918 afs_wait_for_operation(op);
919 }
920
921 if (op->error == -ENOTSUPP) {
922 /* We could try FS.BulkStatus next, but this aborts the entire
923 * op if any of the lookups fails - so, for the moment, revert
924 * to FS.FetchStatus for op->file[1].
925 */
926 op->fetch_status.which = 1;
927 op->ops = &afs_lookup_fetch_status_operation;
928 afs_begin_vnode_operation(op);
929 afs_wait_for_operation(op);
930 }
931 inode = ERR_PTR(op->error);
932
933out_op:
934 if (op->error == 0) {
935 inode = &op->file[1].vnode->vfs_inode;
936 op->file[1].vnode = NULL;
937 }
938
939 if (op->file[0].scb.have_status)
940 dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version;
941 else
942 dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before;
943 ret = afs_put_operation(op);
944out:
945 kfree(cookie);
946 _leave("");
947 return inode ?: ERR_PTR(ret);
948}
949
950/*
951 * Look up an entry in a directory with @sys substitution.
952 */
953static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
954 struct key *key)
955{
956 struct afs_sysnames *subs;
957 struct afs_net *net = afs_i2net(dir);
958 struct dentry *ret;
959 char *buf, *p, *name;
960 int len, i;
961
962 _enter("");
963
964 ret = ERR_PTR(-ENOMEM);
965 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
966 if (!buf)
967 goto out_p;
968 if (dentry->d_name.len > 4) {
969 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
970 p += dentry->d_name.len - 4;
971 }
972
973 /* There is an ordered list of substitutes that we have to try. */
974 read_lock(&net->sysnames_lock);
975 subs = net->sysnames;
976 refcount_inc(&subs->usage);
977 read_unlock(&net->sysnames_lock);
978
979 for (i = 0; i < subs->nr; i++) {
980 name = subs->subs[i];
981 len = dentry->d_name.len - 4 + strlen(name);
982 if (len >= AFSNAMEMAX) {
983 ret = ERR_PTR(-ENAMETOOLONG);
984 goto out_s;
985 }
986
987 strcpy(p, name);
988 ret = lookup_one_len(buf, dentry->d_parent, len);
989 if (IS_ERR(ret) || d_is_positive(ret))
990 goto out_s;
991 dput(ret);
992 }
993
994 /* We don't want to d_add() the @sys dentry here as we don't want to
995 * the cached dentry to hide changes to the sysnames list.
996 */
997 ret = NULL;
998out_s:
999 afs_put_sysnames(subs);
1000 kfree(buf);
1001out_p:
1002 key_put(key);
1003 return ret;
1004}
1005
1006/*
1007 * look up an entry in a directory
1008 */
1009static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
1010 unsigned int flags)
1011{
1012 struct afs_vnode *dvnode = AFS_FS_I(dir);
1013 struct afs_fid fid = {};
1014 struct inode *inode;
1015 struct dentry *d;
1016 struct key *key;
1017 int ret;
1018
1019 _enter("{%llx:%llu},%p{%pd},",
1020 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
1021
1022 ASSERTCMP(d_inode(dentry), ==, NULL);
1023
1024 if (dentry->d_name.len >= AFSNAMEMAX) {
1025 _leave(" = -ENAMETOOLONG");
1026 return ERR_PTR(-ENAMETOOLONG);
1027 }
1028
1029 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
1030 _leave(" = -ESTALE");
1031 return ERR_PTR(-ESTALE);
1032 }
1033
1034 key = afs_request_key(dvnode->volume->cell);
1035 if (IS_ERR(key)) {
1036 _leave(" = %ld [key]", PTR_ERR(key));
1037 return ERR_CAST(key);
1038 }
1039
1040 ret = afs_validate(dvnode, key);
1041 if (ret < 0) {
1042 key_put(key);
1043 _leave(" = %d [val]", ret);
1044 return ERR_PTR(ret);
1045 }
1046
1047 if (dentry->d_name.len >= 4 &&
1048 dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
1049 dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
1050 dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
1051 dentry->d_name.name[dentry->d_name.len - 1] == 's')
1052 return afs_lookup_atsys(dir, dentry, key);
1053
1054 afs_stat_v(dvnode, n_lookup);
1055 inode = afs_do_lookup(dir, dentry, key);
1056 key_put(key);
1057 if (inode == ERR_PTR(-ENOENT))
1058 inode = afs_try_auto_mntpt(dentry, dir);
1059
1060 if (!IS_ERR_OR_NULL(inode))
1061 fid = AFS_FS_I(inode)->fid;
1062
1063 _debug("splice %p", dentry->d_inode);
1064 d = d_splice_alias(inode, dentry);
1065 if (!IS_ERR_OR_NULL(d)) {
1066 d->d_fsdata = dentry->d_fsdata;
1067 trace_afs_lookup(dvnode, &d->d_name, &fid);
1068 } else {
1069 trace_afs_lookup(dvnode, &dentry->d_name, &fid);
1070 }
1071 _leave("");
1072 return d;
1073}
1074
1075/*
1076 * Check the validity of a dentry under RCU conditions.
1077 */
1078static int afs_d_revalidate_rcu(struct dentry *dentry)
1079{
1080 struct afs_vnode *dvnode;
1081 struct dentry *parent;
1082 struct inode *dir;
1083 long dir_version, de_version;
1084
1085 _enter("%p", dentry);
1086
1087 /* Check the parent directory is still valid first. */
1088 parent = READ_ONCE(dentry->d_parent);
1089 dir = d_inode_rcu(parent);
1090 if (!dir)
1091 return -ECHILD;
1092 dvnode = AFS_FS_I(dir);
1093 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags))
1094 return -ECHILD;
1095
1096 if (!afs_check_validity(dvnode))
1097 return -ECHILD;
1098
1099 /* We only need to invalidate a dentry if the server's copy changed
1100 * behind our back. If we made the change, it's no problem. Note that
1101 * on a 32-bit system, we only have 32 bits in the dentry to store the
1102 * version.
1103 */
1104 dir_version = (long)READ_ONCE(dvnode->status.data_version);
1105 de_version = (long)READ_ONCE(dentry->d_fsdata);
1106 if (de_version != dir_version) {
1107 dir_version = (long)READ_ONCE(dvnode->invalid_before);
1108 if (de_version - dir_version < 0)
1109 return -ECHILD;
1110 }
1111
1112 return 1; /* Still valid */
1113}
1114
1115/*
1116 * check that a dentry lookup hit has found a valid entry
1117 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
1118 * inode
1119 */
1120static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
1121{
1122 struct afs_vnode *vnode, *dir;
1123 struct afs_fid fid;
1124 struct dentry *parent;
1125 struct inode *inode;
1126 struct key *key;
1127 afs_dataversion_t dir_version, invalid_before;
1128 long de_version;
1129 int ret;
1130
1131 if (flags & LOOKUP_RCU)
1132 return afs_d_revalidate_rcu(dentry);
1133
1134 if (d_really_is_positive(dentry)) {
1135 vnode = AFS_FS_I(d_inode(dentry));
1136 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
1137 vnode->fid.vid, vnode->fid.vnode, dentry,
1138 vnode->flags);
1139 } else {
1140 _enter("{neg n=%pd}", dentry);
1141 }
1142
1143 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
1144 if (IS_ERR(key))
1145 key = NULL;
1146
1147 /* Hold the parent dentry so we can peer at it */
1148 parent = dget_parent(dentry);
1149 dir = AFS_FS_I(d_inode(parent));
1150
1151 /* validate the parent directory */
1152 afs_validate(dir, key);
1153
1154 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1155 _debug("%pd: parent dir deleted", dentry);
1156 goto not_found;
1157 }
1158
1159 /* We only need to invalidate a dentry if the server's copy changed
1160 * behind our back. If we made the change, it's no problem. Note that
1161 * on a 32-bit system, we only have 32 bits in the dentry to store the
1162 * version.
1163 */
1164 dir_version = dir->status.data_version;
1165 de_version = (long)dentry->d_fsdata;
1166 if (de_version == (long)dir_version)
1167 goto out_valid_noupdate;
1168
1169 invalid_before = dir->invalid_before;
1170 if (de_version - (long)invalid_before >= 0)
1171 goto out_valid;
1172
1173 _debug("dir modified");
1174 afs_stat_v(dir, n_reval);
1175
1176 /* search the directory for this vnode */
1177 ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key, &dir_version);
1178 switch (ret) {
1179 case 0:
1180 /* the filename maps to something */
1181 if (d_really_is_negative(dentry))
1182 goto not_found;
1183 inode = d_inode(dentry);
1184 if (is_bad_inode(inode)) {
1185 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1186 dentry);
1187 goto not_found;
1188 }
1189
1190 vnode = AFS_FS_I(inode);
1191
1192 /* if the vnode ID has changed, then the dirent points to a
1193 * different file */
1194 if (fid.vnode != vnode->fid.vnode) {
1195 _debug("%pd: dirent changed [%llu != %llu]",
1196 dentry, fid.vnode,
1197 vnode->fid.vnode);
1198 goto not_found;
1199 }
1200
1201 /* if the vnode ID uniqifier has changed, then the file has
1202 * been deleted and replaced, and the original vnode ID has
1203 * been reused */
1204 if (fid.unique != vnode->fid.unique) {
1205 _debug("%pd: file deleted (uq %u -> %u I:%u)",
1206 dentry, fid.unique,
1207 vnode->fid.unique,
1208 vnode->vfs_inode.i_generation);
1209 goto not_found;
1210 }
1211 goto out_valid;
1212
1213 case -ENOENT:
1214 /* the filename is unknown */
1215 _debug("%pd: dirent not found", dentry);
1216 if (d_really_is_positive(dentry))
1217 goto not_found;
1218 goto out_valid;
1219
1220 default:
1221 _debug("failed to iterate dir %pd: %d",
1222 parent, ret);
1223 goto not_found;
1224 }
1225
1226out_valid:
1227 dentry->d_fsdata = (void *)(unsigned long)dir_version;
1228out_valid_noupdate:
1229 dput(parent);
1230 key_put(key);
1231 _leave(" = 1 [valid]");
1232 return 1;
1233
1234not_found:
1235 _debug("dropping dentry %pd2", dentry);
1236 dput(parent);
1237 key_put(key);
1238
1239 _leave(" = 0 [bad]");
1240 return 0;
1241}
1242
1243/*
1244 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1245 * sleep)
1246 * - called from dput() when d_count is going to 0.
1247 * - return 1 to request dentry be unhashed, 0 otherwise
1248 */
1249static int afs_d_delete(const struct dentry *dentry)
1250{
1251 _enter("%pd", dentry);
1252
1253 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1254 goto zap;
1255
1256 if (d_really_is_positive(dentry) &&
1257 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
1258 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
1259 goto zap;
1260
1261 _leave(" = 0 [keep]");
1262 return 0;
1263
1264zap:
1265 _leave(" = 1 [zap]");
1266 return 1;
1267}
1268
1269/*
1270 * Clean up sillyrename files on dentry removal.
1271 */
1272static void afs_d_iput(struct dentry *dentry, struct inode *inode)
1273{
1274 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1275 afs_silly_iput(dentry, inode);
1276 iput(inode);
1277}
1278
1279/*
1280 * handle dentry release
1281 */
1282void afs_d_release(struct dentry *dentry)
1283{
1284 _enter("%pd", dentry);
1285}
1286
1287void afs_check_for_remote_deletion(struct afs_operation *op)
1288{
1289 struct afs_vnode *vnode = op->file[0].vnode;
1290
1291 switch (op->ac.abort_code) {
1292 case VNOVNODE:
1293 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1294 afs_break_callback(vnode, afs_cb_break_for_deleted);
1295 }
1296}
1297
1298/*
1299 * Create a new inode for create/mkdir/symlink
1300 */
1301static void afs_vnode_new_inode(struct afs_operation *op)
1302{
1303 struct afs_vnode_param *vp = &op->file[1];
1304 struct afs_vnode *vnode;
1305 struct inode *inode;
1306
1307 _enter("");
1308
1309 ASSERTCMP(op->error, ==, 0);
1310
1311 inode = afs_iget(op, vp);
1312 if (IS_ERR(inode)) {
1313 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1314 * the new directory on the server.
1315 */
1316 op->error = PTR_ERR(inode);
1317 return;
1318 }
1319
1320 vnode = AFS_FS_I(inode);
1321 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1322 if (!op->error)
1323 afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb);
1324 d_instantiate(op->dentry, inode);
1325}
1326
1327static void afs_create_success(struct afs_operation *op)
1328{
1329 _enter("op=%08x", op->debug_id);
1330 op->ctime = op->file[0].scb.status.mtime_client;
1331 afs_vnode_commit_status(op, &op->file[0]);
1332 afs_update_dentry_version(op, &op->file[0], op->dentry);
1333 afs_vnode_new_inode(op);
1334}
1335
1336static void afs_create_edit_dir(struct afs_operation *op)
1337{
1338 struct afs_vnode_param *dvp = &op->file[0];
1339 struct afs_vnode_param *vp = &op->file[1];
1340 struct afs_vnode *dvnode = dvp->vnode;
1341
1342 _enter("op=%08x", op->debug_id);
1343
1344 down_write(&dvnode->validate_lock);
1345 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1346 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1347 afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid,
1348 op->create.reason);
1349 up_write(&dvnode->validate_lock);
1350}
1351
1352static void afs_create_put(struct afs_operation *op)
1353{
1354 _enter("op=%08x", op->debug_id);
1355
1356 if (op->error)
1357 d_drop(op->dentry);
1358}
1359
1360static const struct afs_operation_ops afs_mkdir_operation = {
1361 .issue_afs_rpc = afs_fs_make_dir,
1362 .issue_yfs_rpc = yfs_fs_make_dir,
1363 .success = afs_create_success,
1364 .aborted = afs_check_for_remote_deletion,
1365 .edit_dir = afs_create_edit_dir,
1366 .put = afs_create_put,
1367};
1368
1369/*
1370 * create a directory on an AFS filesystem
1371 */
1372static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
1373 struct dentry *dentry, umode_t mode)
1374{
1375 struct afs_operation *op;
1376 struct afs_vnode *dvnode = AFS_FS_I(dir);
1377
1378 _enter("{%llx:%llu},{%pd},%ho",
1379 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1380
1381 op = afs_alloc_operation(NULL, dvnode->volume);
1382 if (IS_ERR(op)) {
1383 d_drop(dentry);
1384 return PTR_ERR(op);
1385 }
1386
1387 afs_op_set_vnode(op, 0, dvnode);
1388 op->file[0].dv_delta = 1;
1389 op->file[0].modification = true;
1390 op->file[0].update_ctime = true;
1391 op->dentry = dentry;
1392 op->create.mode = S_IFDIR | mode;
1393 op->create.reason = afs_edit_dir_for_mkdir;
1394 op->ops = &afs_mkdir_operation;
1395 return afs_do_sync_operation(op);
1396}
1397
1398/*
1399 * Remove a subdir from a directory.
1400 */
1401static void afs_dir_remove_subdir(struct dentry *dentry)
1402{
1403 if (d_really_is_positive(dentry)) {
1404 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1405
1406 clear_nlink(&vnode->vfs_inode);
1407 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1408 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1409 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1410 }
1411}
1412
1413static void afs_rmdir_success(struct afs_operation *op)
1414{
1415 _enter("op=%08x", op->debug_id);
1416 op->ctime = op->file[0].scb.status.mtime_client;
1417 afs_vnode_commit_status(op, &op->file[0]);
1418 afs_update_dentry_version(op, &op->file[0], op->dentry);
1419}
1420
1421static void afs_rmdir_edit_dir(struct afs_operation *op)
1422{
1423 struct afs_vnode_param *dvp = &op->file[0];
1424 struct afs_vnode *dvnode = dvp->vnode;
1425
1426 _enter("op=%08x", op->debug_id);
1427 afs_dir_remove_subdir(op->dentry);
1428
1429 down_write(&dvnode->validate_lock);
1430 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1431 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1432 afs_edit_dir_remove(dvnode, &op->dentry->d_name,
1433 afs_edit_dir_for_rmdir);
1434 up_write(&dvnode->validate_lock);
1435}
1436
1437static void afs_rmdir_put(struct afs_operation *op)
1438{
1439 _enter("op=%08x", op->debug_id);
1440 if (op->file[1].vnode)
1441 up_write(&op->file[1].vnode->rmdir_lock);
1442}
1443
1444static const struct afs_operation_ops afs_rmdir_operation = {
1445 .issue_afs_rpc = afs_fs_remove_dir,
1446 .issue_yfs_rpc = yfs_fs_remove_dir,
1447 .success = afs_rmdir_success,
1448 .aborted = afs_check_for_remote_deletion,
1449 .edit_dir = afs_rmdir_edit_dir,
1450 .put = afs_rmdir_put,
1451};
1452
1453/*
1454 * remove a directory from an AFS filesystem
1455 */
1456static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1457{
1458 struct afs_operation *op;
1459 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1460 int ret;
1461
1462 _enter("{%llx:%llu},{%pd}",
1463 dvnode->fid.vid, dvnode->fid.vnode, dentry);
1464
1465 op = afs_alloc_operation(NULL, dvnode->volume);
1466 if (IS_ERR(op))
1467 return PTR_ERR(op);
1468
1469 afs_op_set_vnode(op, 0, dvnode);
1470 op->file[0].dv_delta = 1;
1471 op->file[0].modification = true;
1472 op->file[0].update_ctime = true;
1473
1474 op->dentry = dentry;
1475 op->ops = &afs_rmdir_operation;
1476
1477 /* Try to make sure we have a callback promise on the victim. */
1478 if (d_really_is_positive(dentry)) {
1479 vnode = AFS_FS_I(d_inode(dentry));
1480 ret = afs_validate(vnode, op->key);
1481 if (ret < 0)
1482 goto error;
1483 }
1484
1485 if (vnode) {
1486 ret = down_write_killable(&vnode->rmdir_lock);
1487 if (ret < 0)
1488 goto error;
1489 op->file[1].vnode = vnode;
1490 }
1491
1492 return afs_do_sync_operation(op);
1493
1494error:
1495 return afs_put_operation(op);
1496}
1497
1498/*
1499 * Remove a link to a file or symlink from a directory.
1500 *
1501 * If the file was not deleted due to excess hard links, the fileserver will
1502 * break the callback promise on the file - if it had one - before it returns
1503 * to us, and if it was deleted, it won't
1504 *
1505 * However, if we didn't have a callback promise outstanding, or it was
1506 * outstanding on a different server, then it won't break it either...
1507 */
1508static void afs_dir_remove_link(struct afs_operation *op)
1509{
1510 struct afs_vnode *dvnode = op->file[0].vnode;
1511 struct afs_vnode *vnode = op->file[1].vnode;
1512 struct dentry *dentry = op->dentry;
1513 int ret;
1514
1515 if (op->error != 0 ||
1516 (op->file[1].scb.have_status && op->file[1].scb.have_error))
1517 return;
1518 if (d_really_is_positive(dentry))
1519 return;
1520
1521 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
1522 /* Already done */
1523 } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1524 write_seqlock(&vnode->cb_lock);
1525 drop_nlink(&vnode->vfs_inode);
1526 if (vnode->vfs_inode.i_nlink == 0) {
1527 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1528 __afs_break_callback(vnode, afs_cb_break_for_unlink);
1529 }
1530 write_sequnlock(&vnode->cb_lock);
1531 } else {
1532 afs_break_callback(vnode, afs_cb_break_for_unlink);
1533
1534 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1535 _debug("AFS_VNODE_DELETED");
1536
1537 ret = afs_validate(vnode, op->key);
1538 if (ret != -ESTALE)
1539 op->error = ret;
1540 }
1541
1542 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, op->error);
1543}
1544
1545static void afs_unlink_success(struct afs_operation *op)
1546{
1547 _enter("op=%08x", op->debug_id);
1548 op->ctime = op->file[0].scb.status.mtime_client;
1549 afs_check_dir_conflict(op, &op->file[0]);
1550 afs_vnode_commit_status(op, &op->file[0]);
1551 afs_vnode_commit_status(op, &op->file[1]);
1552 afs_update_dentry_version(op, &op->file[0], op->dentry);
1553 afs_dir_remove_link(op);
1554}
1555
1556static void afs_unlink_edit_dir(struct afs_operation *op)
1557{
1558 struct afs_vnode_param *dvp = &op->file[0];
1559 struct afs_vnode *dvnode = dvp->vnode;
1560
1561 _enter("op=%08x", op->debug_id);
1562 down_write(&dvnode->validate_lock);
1563 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1564 dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1565 afs_edit_dir_remove(dvnode, &op->dentry->d_name,
1566 afs_edit_dir_for_unlink);
1567 up_write(&dvnode->validate_lock);
1568}
1569
1570static void afs_unlink_put(struct afs_operation *op)
1571{
1572 _enter("op=%08x", op->debug_id);
1573 if (op->unlink.need_rehash && op->error < 0 && op->error != -ENOENT)
1574 d_rehash(op->dentry);
1575}
1576
1577static const struct afs_operation_ops afs_unlink_operation = {
1578 .issue_afs_rpc = afs_fs_remove_file,
1579 .issue_yfs_rpc = yfs_fs_remove_file,
1580 .success = afs_unlink_success,
1581 .aborted = afs_check_for_remote_deletion,
1582 .edit_dir = afs_unlink_edit_dir,
1583 .put = afs_unlink_put,
1584};
1585
1586/*
1587 * Remove a file or symlink from an AFS filesystem.
1588 */
1589static int afs_unlink(struct inode *dir, struct dentry *dentry)
1590{
1591 struct afs_operation *op;
1592 struct afs_vnode *dvnode = AFS_FS_I(dir);
1593 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1594 int ret;
1595
1596 _enter("{%llx:%llu},{%pd}",
1597 dvnode->fid.vid, dvnode->fid.vnode, dentry);
1598
1599 if (dentry->d_name.len >= AFSNAMEMAX)
1600 return -ENAMETOOLONG;
1601
1602 op = afs_alloc_operation(NULL, dvnode->volume);
1603 if (IS_ERR(op))
1604 return PTR_ERR(op);
1605
1606 afs_op_set_vnode(op, 0, dvnode);
1607 op->file[0].dv_delta = 1;
1608 op->file[0].modification = true;
1609 op->file[0].update_ctime = true;
1610
1611 /* Try to make sure we have a callback promise on the victim. */
1612 ret = afs_validate(vnode, op->key);
1613 if (ret < 0) {
1614 op->error = ret;
1615 goto error;
1616 }
1617
1618 spin_lock(&dentry->d_lock);
1619 if (d_count(dentry) > 1) {
1620 spin_unlock(&dentry->d_lock);
1621 /* Start asynchronous writeout of the inode */
1622 write_inode_now(d_inode(dentry), 0);
1623 op->error = afs_sillyrename(dvnode, vnode, dentry, op->key);
1624 goto error;
1625 }
1626 if (!d_unhashed(dentry)) {
1627 /* Prevent a race with RCU lookup. */
1628 __d_drop(dentry);
1629 op->unlink.need_rehash = true;
1630 }
1631 spin_unlock(&dentry->d_lock);
1632
1633 op->file[1].vnode = vnode;
1634 op->file[1].update_ctime = true;
1635 op->file[1].op_unlinked = true;
1636 op->dentry = dentry;
1637 op->ops = &afs_unlink_operation;
1638 afs_begin_vnode_operation(op);
1639 afs_wait_for_operation(op);
1640
1641 /* If there was a conflict with a third party, check the status of the
1642 * unlinked vnode.
1643 */
1644 if (op->error == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) {
1645 op->file[1].update_ctime = false;
1646 op->fetch_status.which = 1;
1647 op->ops = &afs_fetch_status_operation;
1648 afs_begin_vnode_operation(op);
1649 afs_wait_for_operation(op);
1650 }
1651
1652 return afs_put_operation(op);
1653
1654error:
1655 return afs_put_operation(op);
1656}
1657
1658static const struct afs_operation_ops afs_create_operation = {
1659 .issue_afs_rpc = afs_fs_create_file,
1660 .issue_yfs_rpc = yfs_fs_create_file,
1661 .success = afs_create_success,
1662 .aborted = afs_check_for_remote_deletion,
1663 .edit_dir = afs_create_edit_dir,
1664 .put = afs_create_put,
1665};
1666
1667/*
1668 * create a regular file on an AFS filesystem
1669 */
1670static int afs_create(struct user_namespace *mnt_userns, struct inode *dir,
1671 struct dentry *dentry, umode_t mode, bool excl)
1672{
1673 struct afs_operation *op;
1674 struct afs_vnode *dvnode = AFS_FS_I(dir);
1675 int ret = -ENAMETOOLONG;
1676
1677 _enter("{%llx:%llu},{%pd},%ho",
1678 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1679
1680 if (dentry->d_name.len >= AFSNAMEMAX)
1681 goto error;
1682
1683 op = afs_alloc_operation(NULL, dvnode->volume);
1684 if (IS_ERR(op)) {
1685 ret = PTR_ERR(op);
1686 goto error;
1687 }
1688
1689 afs_op_set_vnode(op, 0, dvnode);
1690 op->file[0].dv_delta = 1;
1691 op->file[0].modification = true;
1692 op->file[0].update_ctime = true;
1693
1694 op->dentry = dentry;
1695 op->create.mode = S_IFREG | mode;
1696 op->create.reason = afs_edit_dir_for_create;
1697 op->ops = &afs_create_operation;
1698 return afs_do_sync_operation(op);
1699
1700error:
1701 d_drop(dentry);
1702 _leave(" = %d", ret);
1703 return ret;
1704}
1705
1706static void afs_link_success(struct afs_operation *op)
1707{
1708 struct afs_vnode_param *dvp = &op->file[0];
1709 struct afs_vnode_param *vp = &op->file[1];
1710
1711 _enter("op=%08x", op->debug_id);
1712 op->ctime = dvp->scb.status.mtime_client;
1713 afs_vnode_commit_status(op, dvp);
1714 afs_vnode_commit_status(op, vp);
1715 afs_update_dentry_version(op, dvp, op->dentry);
1716 if (op->dentry_2->d_parent == op->dentry->d_parent)
1717 afs_update_dentry_version(op, dvp, op->dentry_2);
1718 ihold(&vp->vnode->vfs_inode);
1719 d_instantiate(op->dentry, &vp->vnode->vfs_inode);
1720}
1721
1722static void afs_link_put(struct afs_operation *op)
1723{
1724 _enter("op=%08x", op->debug_id);
1725 if (op->error)
1726 d_drop(op->dentry);
1727}
1728
1729static const struct afs_operation_ops afs_link_operation = {
1730 .issue_afs_rpc = afs_fs_link,
1731 .issue_yfs_rpc = yfs_fs_link,
1732 .success = afs_link_success,
1733 .aborted = afs_check_for_remote_deletion,
1734 .edit_dir = afs_create_edit_dir,
1735 .put = afs_link_put,
1736};
1737
1738/*
1739 * create a hard link between files in an AFS filesystem
1740 */
1741static int afs_link(struct dentry *from, struct inode *dir,
1742 struct dentry *dentry)
1743{
1744 struct afs_operation *op;
1745 struct afs_vnode *dvnode = AFS_FS_I(dir);
1746 struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1747 int ret = -ENAMETOOLONG;
1748
1749 _enter("{%llx:%llu},{%llx:%llu},{%pd}",
1750 vnode->fid.vid, vnode->fid.vnode,
1751 dvnode->fid.vid, dvnode->fid.vnode,
1752 dentry);
1753
1754 if (dentry->d_name.len >= AFSNAMEMAX)
1755 goto error;
1756
1757 op = afs_alloc_operation(NULL, dvnode->volume);
1758 if (IS_ERR(op)) {
1759 ret = PTR_ERR(op);
1760 goto error;
1761 }
1762
1763 ret = afs_validate(vnode, op->key);
1764 if (ret < 0)
1765 goto error_op;
1766
1767 afs_op_set_vnode(op, 0, dvnode);
1768 afs_op_set_vnode(op, 1, vnode);
1769 op->file[0].dv_delta = 1;
1770 op->file[0].modification = true;
1771 op->file[0].update_ctime = true;
1772 op->file[1].update_ctime = true;
1773
1774 op->dentry = dentry;
1775 op->dentry_2 = from;
1776 op->ops = &afs_link_operation;
1777 op->create.reason = afs_edit_dir_for_link;
1778 return afs_do_sync_operation(op);
1779
1780error_op:
1781 afs_put_operation(op);
1782error:
1783 d_drop(dentry);
1784 _leave(" = %d", ret);
1785 return ret;
1786}
1787
1788static const struct afs_operation_ops afs_symlink_operation = {
1789 .issue_afs_rpc = afs_fs_symlink,
1790 .issue_yfs_rpc = yfs_fs_symlink,
1791 .success = afs_create_success,
1792 .aborted = afs_check_for_remote_deletion,
1793 .edit_dir = afs_create_edit_dir,
1794 .put = afs_create_put,
1795};
1796
1797/*
1798 * create a symlink in an AFS filesystem
1799 */
1800static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
1801 struct dentry *dentry, const char *content)
1802{
1803 struct afs_operation *op;
1804 struct afs_vnode *dvnode = AFS_FS_I(dir);
1805 int ret;
1806
1807 _enter("{%llx:%llu},{%pd},%s",
1808 dvnode->fid.vid, dvnode->fid.vnode, dentry,
1809 content);
1810
1811 ret = -ENAMETOOLONG;
1812 if (dentry->d_name.len >= AFSNAMEMAX)
1813 goto error;
1814
1815 ret = -EINVAL;
1816 if (strlen(content) >= AFSPATHMAX)
1817 goto error;
1818
1819 op = afs_alloc_operation(NULL, dvnode->volume);
1820 if (IS_ERR(op)) {
1821 ret = PTR_ERR(op);
1822 goto error;
1823 }
1824
1825 afs_op_set_vnode(op, 0, dvnode);
1826 op->file[0].dv_delta = 1;
1827
1828 op->dentry = dentry;
1829 op->ops = &afs_symlink_operation;
1830 op->create.reason = afs_edit_dir_for_symlink;
1831 op->create.symlink = content;
1832 return afs_do_sync_operation(op);
1833
1834error:
1835 d_drop(dentry);
1836 _leave(" = %d", ret);
1837 return ret;
1838}
1839
1840static void afs_rename_success(struct afs_operation *op)
1841{
1842 _enter("op=%08x", op->debug_id);
1843
1844 op->ctime = op->file[0].scb.status.mtime_client;
1845 afs_check_dir_conflict(op, &op->file[1]);
1846 afs_vnode_commit_status(op, &op->file[0]);
1847 if (op->file[1].vnode != op->file[0].vnode) {
1848 op->ctime = op->file[1].scb.status.mtime_client;
1849 afs_vnode_commit_status(op, &op->file[1]);
1850 }
1851}
1852
1853static void afs_rename_edit_dir(struct afs_operation *op)
1854{
1855 struct afs_vnode_param *orig_dvp = &op->file[0];
1856 struct afs_vnode_param *new_dvp = &op->file[1];
1857 struct afs_vnode *orig_dvnode = orig_dvp->vnode;
1858 struct afs_vnode *new_dvnode = new_dvp->vnode;
1859 struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry));
1860 struct dentry *old_dentry = op->dentry;
1861 struct dentry *new_dentry = op->dentry_2;
1862 struct inode *new_inode;
1863
1864 _enter("op=%08x", op->debug_id);
1865
1866 if (op->rename.rehash) {
1867 d_rehash(op->rename.rehash);
1868 op->rename.rehash = NULL;
1869 }
1870
1871 down_write(&orig_dvnode->validate_lock);
1872 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) &&
1873 orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta)
1874 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
1875 afs_edit_dir_for_rename_0);
1876
1877 if (new_dvnode != orig_dvnode) {
1878 up_write(&orig_dvnode->validate_lock);
1879 down_write(&new_dvnode->validate_lock);
1880 }
1881
1882 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) &&
1883 new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) {
1884 if (!op->rename.new_negative)
1885 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
1886 afs_edit_dir_for_rename_1);
1887
1888 afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
1889 &vnode->fid, afs_edit_dir_for_rename_2);
1890 }
1891
1892 new_inode = d_inode(new_dentry);
1893 if (new_inode) {
1894 spin_lock(&new_inode->i_lock);
1895 if (S_ISDIR(new_inode->i_mode))
1896 clear_nlink(new_inode);
1897 else if (new_inode->i_nlink > 0)
1898 drop_nlink(new_inode);
1899 spin_unlock(&new_inode->i_lock);
1900 }
1901
1902 /* Now we can update d_fsdata on the dentries to reflect their
1903 * new parent's data_version.
1904 *
1905 * Note that if we ever implement RENAME_EXCHANGE, we'll have
1906 * to update both dentries with opposing dir versions.
1907 */
1908 afs_update_dentry_version(op, new_dvp, op->dentry);
1909 afs_update_dentry_version(op, new_dvp, op->dentry_2);
1910
1911 d_move(old_dentry, new_dentry);
1912
1913 up_write(&new_dvnode->validate_lock);
1914}
1915
1916static void afs_rename_put(struct afs_operation *op)
1917{
1918 _enter("op=%08x", op->debug_id);
1919 if (op->rename.rehash)
1920 d_rehash(op->rename.rehash);
1921 dput(op->rename.tmp);
1922 if (op->error)
1923 d_rehash(op->dentry);
1924}
1925
1926static const struct afs_operation_ops afs_rename_operation = {
1927 .issue_afs_rpc = afs_fs_rename,
1928 .issue_yfs_rpc = yfs_fs_rename,
1929 .success = afs_rename_success,
1930 .edit_dir = afs_rename_edit_dir,
1931 .put = afs_rename_put,
1932};
1933
1934/*
1935 * rename a file in an AFS filesystem and/or move it between directories
1936 */
1937static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
1938 struct dentry *old_dentry, struct inode *new_dir,
1939 struct dentry *new_dentry, unsigned int flags)
1940{
1941 struct afs_operation *op;
1942 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1943 int ret;
1944
1945 if (flags)
1946 return -EINVAL;
1947
1948 /* Don't allow silly-rename files be moved around. */
1949 if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
1950 return -EINVAL;
1951
1952 vnode = AFS_FS_I(d_inode(old_dentry));
1953 orig_dvnode = AFS_FS_I(old_dir);
1954 new_dvnode = AFS_FS_I(new_dir);
1955
1956 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1957 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1958 vnode->fid.vid, vnode->fid.vnode,
1959 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1960 new_dentry);
1961
1962 op = afs_alloc_operation(NULL, orig_dvnode->volume);
1963 if (IS_ERR(op))
1964 return PTR_ERR(op);
1965
1966 ret = afs_validate(vnode, op->key);
1967 op->error = ret;
1968 if (ret < 0)
1969 goto error;
1970
1971 afs_op_set_vnode(op, 0, orig_dvnode);
1972 afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */
1973 op->file[0].dv_delta = 1;
1974 op->file[1].dv_delta = 1;
1975 op->file[0].modification = true;
1976 op->file[1].modification = true;
1977 op->file[0].update_ctime = true;
1978 op->file[1].update_ctime = true;
1979
1980 op->dentry = old_dentry;
1981 op->dentry_2 = new_dentry;
1982 op->rename.new_negative = d_is_negative(new_dentry);
1983 op->ops = &afs_rename_operation;
1984
1985 /* For non-directories, check whether the target is busy and if so,
1986 * make a copy of the dentry and then do a silly-rename. If the
1987 * silly-rename succeeds, the copied dentry is hashed and becomes the
1988 * new target.
1989 */
1990 if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
1991 /* To prevent any new references to the target during the
1992 * rename, we unhash the dentry in advance.
1993 */
1994 if (!d_unhashed(new_dentry)) {
1995 d_drop(new_dentry);
1996 op->rename.rehash = new_dentry;
1997 }
1998
1999 if (d_count(new_dentry) > 2) {
2000 /* copy the target dentry's name */
2001 op->rename.tmp = d_alloc(new_dentry->d_parent,
2002 &new_dentry->d_name);
2003 if (!op->rename.tmp) {
2004 op->error = -ENOMEM;
2005 goto error;
2006 }
2007
2008 ret = afs_sillyrename(new_dvnode,
2009 AFS_FS_I(d_inode(new_dentry)),
2010 new_dentry, op->key);
2011 if (ret) {
2012 op->error = ret;
2013 goto error;
2014 }
2015
2016 op->dentry_2 = op->rename.tmp;
2017 op->rename.rehash = NULL;
2018 op->rename.new_negative = true;
2019 }
2020 }
2021
2022 /* This bit is potentially nasty as there's a potential race with
2023 * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry
2024 * to reflect it's new parent's new data_version after the op, but
2025 * d_revalidate may see old_dentry between the op having taken place
2026 * and the version being updated.
2027 *
2028 * So drop the old_dentry for now to make other threads go through
2029 * lookup instead - which we hold a lock against.
2030 */
2031 d_drop(old_dentry);
2032
2033 return afs_do_sync_operation(op);
2034
2035error:
2036 return afs_put_operation(op);
2037}
2038
2039/*
2040 * Release a directory page and clean up its private state if it's not busy
2041 * - return true if the page can now be released, false if not
2042 */
2043static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
2044{
2045 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
2046
2047 _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
2048
2049 detach_page_private(page);
2050
2051 /* The directory will need reloading. */
2052 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
2053 afs_stat_v(dvnode, n_relpg);
2054 return 1;
2055}
2056
2057/*
2058 * invalidate part or all of a page
2059 * - release a page and clean up its private data if offset is 0 (indicating
2060 * the entire page)
2061 */
2062static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
2063 unsigned int length)
2064{
2065 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
2066
2067 _enter("{%lu},%u,%u", page->index, offset, length);
2068
2069 BUG_ON(!PageLocked(page));
2070
2071 /* The directory will need reloading. */
2072 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
2073 afs_stat_v(dvnode, n_inval);
2074
2075 /* we clean up only if the entire page is being invalidated */
2076 if (offset == 0 && length == thp_size(page))
2077 detach_page_private(page);
2078}