1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* dir.c: AFS filesystem directory handling
4 * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/kernel.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>
20 static struct dentry
*afs_lookup(struct inode
*dir
, struct dentry
*dentry
,
22 static int afs_dir_open(struct inode
*inode
, struct file
*file
);
23 static int afs_readdir(struct file
*file
, struct dir_context
*ctx
);
24 static int afs_d_revalidate(struct dentry
*dentry
, unsigned int flags
);
25 static int afs_d_delete(const struct dentry
*dentry
);
26 static void afs_d_iput(struct dentry
*dentry
, struct inode
*inode
);
27 static int afs_lookup_one_filldir(struct dir_context
*ctx
, const char *name
, int nlen
,
28 loff_t fpos
, u64 ino
, unsigned dtype
);
29 static int afs_lookup_filldir(struct dir_context
*ctx
, const char *name
, int nlen
,
30 loff_t fpos
, u64 ino
, unsigned dtype
);
31 static int afs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
33 static int afs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
34 static int afs_rmdir(struct inode
*dir
, struct dentry
*dentry
);
35 static int afs_unlink(struct inode
*dir
, struct dentry
*dentry
);
36 static int afs_link(struct dentry
*from
, struct inode
*dir
,
37 struct dentry
*dentry
);
38 static int afs_symlink(struct inode
*dir
, struct dentry
*dentry
,
40 static int afs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
41 struct inode
*new_dir
, struct dentry
*new_dentry
,
43 static int afs_dir_releasepage(struct page
*page
, gfp_t gfp_flags
);
44 static void afs_dir_invalidatepage(struct page
*page
, unsigned int offset
,
47 static int afs_dir_set_page_dirty(struct page
*page
)
49 BUG(); /* This should never happen. */
52 const struct file_operations afs_dir_file_operations
= {
54 .release
= afs_release
,
55 .iterate_shared
= afs_readdir
,
57 .llseek
= generic_file_llseek
,
60 const struct inode_operations afs_dir_inode_operations
= {
65 .symlink
= afs_symlink
,
69 .permission
= afs_permission
,
70 .getattr
= afs_getattr
,
71 .setattr
= afs_setattr
,
72 .listxattr
= afs_listxattr
,
75 const 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
,
81 const 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
,
89 struct afs_lookup_one_cookie
{
90 struct dir_context ctx
;
96 struct afs_lookup_cookie
{
97 struct dir_context ctx
;
101 unsigned short nr_fids
;
102 struct inode
**inodes
;
103 struct afs_status_cb
*statuses
;
104 struct afs_fid fids
[50];
108 * check that a directory page is valid
110 static bool afs_dir_check_page(struct afs_vnode
*dvnode
, struct page
*page
,
113 struct afs_xdr_dir_page
*dbuf
;
117 /* Determine how many magic numbers there should be in this page, but
118 * we must take care because the directory may change size under us.
120 off
= page_offset(page
);
124 latter
= i_size
- off
;
125 if (latter
>= PAGE_SIZE
)
129 qty
/= sizeof(union afs_xdr_dir_block
);
133 for (tmp
= 0; tmp
< qty
; tmp
++) {
134 if (dbuf
->blocks
[tmp
].hdr
.magic
!= AFS_DIR_MAGIC
) {
135 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
136 __func__
, dvnode
->vfs_inode
.i_ino
, tmp
, qty
,
137 ntohs(dbuf
->blocks
[tmp
].hdr
.magic
));
138 trace_afs_dir_check_failed(dvnode
, off
, i_size
);
140 trace_afs_file_error(dvnode
, -EIO
, afs_file_error_dir_bad_magic
);
144 /* Make sure each block is NUL terminated so we can reasonably
145 * use string functions on it. The filenames in the page
146 * *should* be NUL-terminated anyway.
148 ((u8
*)&dbuf
->blocks
[tmp
])[AFS_DIR_BLOCK_SIZE
- 1] = 0;
154 afs_stat_v(dvnode
, n_read_dir
);
162 * Check the contents of a directory that we've just read.
164 static bool afs_dir_check_pages(struct afs_vnode
*dvnode
, struct afs_read
*req
)
166 struct afs_xdr_dir_page
*dbuf
;
167 unsigned int i
, j
, qty
= PAGE_SIZE
/ sizeof(union afs_xdr_dir_block
);
169 for (i
= 0; i
< req
->nr_pages
; i
++)
170 if (!afs_dir_check_page(dvnode
, req
->pages
[i
], req
->actual_len
))
175 pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
176 dvnode
->fid
.vid
, dvnode
->fid
.vnode
,
177 req
->file_size
, req
->len
, req
->actual_len
, req
->remain
);
178 pr_warn("DIR %llx %x %x %x\n",
179 req
->pos
, req
->index
, req
->nr_pages
, req
->offset
);
181 for (i
= 0; i
< req
->nr_pages
; i
++) {
182 dbuf
= kmap(req
->pages
[i
]);
183 for (j
= 0; j
< qty
; j
++) {
184 union afs_xdr_dir_block
*block
= &dbuf
->blocks
[j
];
186 pr_warn("[%02x] %32phN\n", i
* qty
+ j
, block
);
188 kunmap(req
->pages
[i
]);
194 * open an AFS directory file
196 static int afs_dir_open(struct inode
*inode
, struct file
*file
)
198 _enter("{%lu}", inode
->i_ino
);
200 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block
) != 2048);
201 BUILD_BUG_ON(sizeof(union afs_xdr_dirent
) != 32);
203 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(inode
)->flags
))
206 return afs_open(inode
, file
);
210 * Read the directory into the pagecache in one go, scrubbing the previous
211 * contents. The list of pages is returned, pinning them so that they don't
212 * get reclaimed during the iteration.
214 static struct afs_read
*afs_read_dir(struct afs_vnode
*dvnode
, struct key
*key
)
215 __acquires(&dvnode
->validate_lock
)
217 struct afs_read
*req
;
219 int nr_pages
, nr_inline
, i
, n
;
223 i_size
= i_size_read(&dvnode
->vfs_inode
);
225 return ERR_PTR(afs_bad(dvnode
, afs_file_error_dir_small
));
226 if (i_size
> 2048 * 1024) {
227 trace_afs_file_error(dvnode
, -EFBIG
, afs_file_error_dir_big
);
228 return ERR_PTR(-EFBIG
);
231 _enter("%llu", i_size
);
233 /* Get a request record to hold the page list. We want to hold it
234 * inline if we can, but we don't want to make an order 1 allocation.
236 nr_pages
= (i_size
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
237 nr_inline
= nr_pages
;
238 if (nr_inline
> (PAGE_SIZE
- sizeof(*req
)) / sizeof(struct page
*))
241 req
= kzalloc(struct_size(req
, array
, nr_inline
), GFP_KERNEL
);
243 return ERR_PTR(-ENOMEM
);
245 refcount_set(&req
->usage
, 1);
246 req
->nr_pages
= nr_pages
;
247 req
->actual_len
= i_size
; /* May change */
248 req
->len
= nr_pages
* PAGE_SIZE
; /* We can ask for more than there is */
249 req
->data_version
= dvnode
->status
.data_version
; /* May change */
251 req
->pages
= req
->array
;
253 req
->pages
= kcalloc(nr_pages
, sizeof(struct page
*),
259 /* Get a list of all the pages that hold or will hold the directory
260 * content. We need to fill in any gaps that we might find where the
261 * memory reclaimer has been at work. If there are any gaps, we will
262 * need to reread the entire directory contents.
266 n
= find_get_pages_contig(dvnode
->vfs_inode
.i_mapping
, i
,
269 _debug("find %u at %u/%u", n
, i
, req
->nr_pages
);
271 gfp_t gfp
= dvnode
->vfs_inode
.i_mapping
->gfp_mask
;
273 if (test_and_clear_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
))
274 afs_stat_v(dvnode
, n_inval
);
277 req
->pages
[i
] = __page_cache_alloc(gfp
);
280 ret
= add_to_page_cache_lru(req
->pages
[i
],
281 dvnode
->vfs_inode
.i_mapping
,
286 set_page_private(req
->pages
[i
], 1);
287 SetPagePrivate(req
->pages
[i
]);
288 unlock_page(req
->pages
[i
]);
293 } while (i
< req
->nr_pages
);
295 /* If we're going to reload, we need to lock all the pages to prevent
299 if (down_read_killable(&dvnode
->validate_lock
) < 0)
302 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
))
305 up_read(&dvnode
->validate_lock
);
306 if (down_write_killable(&dvnode
->validate_lock
) < 0)
309 if (!test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
)) {
310 trace_afs_reload_dir(dvnode
);
311 ret
= afs_fetch_data(dvnode
, key
, req
);
315 task_io_account_read(PAGE_SIZE
* req
->nr_pages
);
317 if (req
->len
< req
->file_size
)
318 goto content_has_grown
;
320 /* Validate the data we just read. */
322 if (!afs_dir_check_pages(dvnode
, req
))
325 // TODO: Trim excess pages
327 set_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
);
330 downgrade_write(&dvnode
->validate_lock
);
335 up_write(&dvnode
->validate_lock
);
338 _leave(" = %d", ret
);
342 up_write(&dvnode
->validate_lock
);
348 * deal with one block in an AFS directory
350 static int afs_dir_iterate_block(struct afs_vnode
*dvnode
,
351 struct dir_context
*ctx
,
352 union afs_xdr_dir_block
*block
,
355 union afs_xdr_dirent
*dire
;
356 unsigned offset
, next
, curr
;
360 _enter("%u,%x,%p,,",(unsigned)ctx
->pos
,blkoff
,block
);
362 curr
= (ctx
->pos
- blkoff
) / sizeof(union afs_xdr_dirent
);
364 /* walk through the block, an entry at a time */
365 for (offset
= (blkoff
== 0 ? AFS_DIR_RESV_BLOCKS0
: AFS_DIR_RESV_BLOCKS
);
366 offset
< AFS_DIR_SLOTS_PER_BLOCK
;
371 /* skip entries marked unused in the bitmap */
372 if (!(block
->hdr
.bitmap
[offset
/ 8] &
373 (1 << (offset
% 8)))) {
374 _debug("ENT[%zu.%u]: unused",
375 blkoff
/ sizeof(union afs_xdr_dir_block
), offset
);
378 next
* sizeof(union afs_xdr_dirent
);
382 /* got a valid entry */
383 dire
= &block
->dirents
[offset
];
384 nlen
= strnlen(dire
->u
.name
,
386 offset
* sizeof(union afs_xdr_dirent
));
388 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
389 blkoff
/ sizeof(union afs_xdr_dir_block
), offset
,
390 (offset
< curr
? "skip" : "fill"),
393 /* work out where the next possible entry is */
394 for (tmp
= nlen
; tmp
> 15; tmp
-= sizeof(union afs_xdr_dirent
)) {
395 if (next
>= AFS_DIR_SLOTS_PER_BLOCK
) {
396 _debug("ENT[%zu.%u]:"
397 " %u travelled beyond end dir block"
399 blkoff
/ sizeof(union afs_xdr_dir_block
),
400 offset
, next
, tmp
, nlen
);
401 return afs_bad(dvnode
, afs_file_error_dir_over_end
);
403 if (!(block
->hdr
.bitmap
[next
/ 8] &
404 (1 << (next
% 8)))) {
405 _debug("ENT[%zu.%u]:"
406 " %u unmarked extension (len %u/%zu)",
407 blkoff
/ sizeof(union afs_xdr_dir_block
),
408 offset
, next
, tmp
, nlen
);
409 return afs_bad(dvnode
, afs_file_error_dir_unmarked_ext
);
412 _debug("ENT[%zu.%u]: ext %u/%zu",
413 blkoff
/ sizeof(union afs_xdr_dir_block
),
418 /* skip if starts before the current position */
422 /* found the next entry */
423 if (!dir_emit(ctx
, dire
->u
.name
, nlen
,
424 ntohl(dire
->u
.vnode
),
425 (ctx
->actor
== afs_lookup_filldir
||
426 ctx
->actor
== afs_lookup_one_filldir
)?
427 ntohl(dire
->u
.unique
) : DT_UNKNOWN
)) {
428 _leave(" = 0 [full]");
432 ctx
->pos
= blkoff
+ next
* sizeof(union afs_xdr_dirent
);
435 _leave(" = 1 [more]");
440 * iterate through the data blob that lists the contents of an AFS directory
442 static int afs_dir_iterate(struct inode
*dir
, struct dir_context
*ctx
,
443 struct key
*key
, afs_dataversion_t
*_dir_version
)
445 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
446 struct afs_xdr_dir_page
*dbuf
;
447 union afs_xdr_dir_block
*dblock
;
448 struct afs_read
*req
;
450 unsigned blkoff
, limit
;
453 _enter("{%lu},%u,,", dir
->i_ino
, (unsigned)ctx
->pos
);
455 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(dir
)->flags
)) {
456 _leave(" = -ESTALE");
460 req
= afs_read_dir(dvnode
, key
);
463 *_dir_version
= req
->data_version
;
465 /* round the file position up to the next entry boundary */
466 ctx
->pos
+= sizeof(union afs_xdr_dirent
) - 1;
467 ctx
->pos
&= ~(sizeof(union afs_xdr_dirent
) - 1);
469 /* walk through the blocks in sequence */
471 while (ctx
->pos
< req
->actual_len
) {
472 blkoff
= ctx
->pos
& ~(sizeof(union afs_xdr_dir_block
) - 1);
474 /* Fetch the appropriate page from the directory and re-add it
477 page
= req
->pages
[blkoff
/ PAGE_SIZE
];
479 ret
= afs_bad(dvnode
, afs_file_error_dir_missing_page
);
482 mark_page_accessed(page
);
484 limit
= blkoff
& ~(PAGE_SIZE
- 1);
488 /* deal with the individual blocks stashed on this page */
490 dblock
= &dbuf
->blocks
[(blkoff
% PAGE_SIZE
) /
491 sizeof(union afs_xdr_dir_block
)];
492 ret
= afs_dir_iterate_block(dvnode
, ctx
, dblock
, blkoff
);
498 blkoff
+= sizeof(union afs_xdr_dir_block
);
500 } while (ctx
->pos
< dir
->i_size
&& blkoff
< limit
);
507 up_read(&dvnode
->validate_lock
);
509 _leave(" = %d", ret
);
514 * read an AFS directory
516 static int afs_readdir(struct file
*file
, struct dir_context
*ctx
)
518 afs_dataversion_t dir_version
;
520 return afs_dir_iterate(file_inode(file
), ctx
, afs_file_key(file
),
525 * Search the directory for a single name
526 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
527 * uniquifier through dtype
529 static int afs_lookup_one_filldir(struct dir_context
*ctx
, const char *name
,
530 int nlen
, loff_t fpos
, u64 ino
, unsigned dtype
)
532 struct afs_lookup_one_cookie
*cookie
=
533 container_of(ctx
, struct afs_lookup_one_cookie
, ctx
);
535 _enter("{%s,%u},%s,%u,,%llu,%u",
536 cookie
->name
.name
, cookie
->name
.len
, name
, nlen
,
537 (unsigned long long) ino
, dtype
);
539 /* insanity checks first */
540 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block
) != 2048);
541 BUILD_BUG_ON(sizeof(union afs_xdr_dirent
) != 32);
543 if (cookie
->name
.len
!= nlen
||
544 memcmp(cookie
->name
.name
, name
, nlen
) != 0) {
549 cookie
->fid
.vnode
= ino
;
550 cookie
->fid
.unique
= dtype
;
553 _leave(" = -1 [found]");
558 * Do a lookup of a single name in a directory
559 * - just returns the FID the dentry name maps to if found
561 static int afs_do_lookup_one(struct inode
*dir
, struct dentry
*dentry
,
562 struct afs_fid
*fid
, struct key
*key
,
563 afs_dataversion_t
*_dir_version
)
565 struct afs_super_info
*as
= dir
->i_sb
->s_fs_info
;
566 struct afs_lookup_one_cookie cookie
= {
567 .ctx
.actor
= afs_lookup_one_filldir
,
568 .name
= dentry
->d_name
,
569 .fid
.vid
= as
->volume
->vid
573 _enter("{%lu},%p{%pd},", dir
->i_ino
, dentry
, dentry
);
575 /* search the directory */
576 ret
= afs_dir_iterate(dir
, &cookie
.ctx
, key
, _dir_version
);
578 _leave(" = %d [iter]", ret
);
584 _leave(" = -ENOENT [not found]");
589 _leave(" = 0 { vn=%llu u=%u }", fid
->vnode
, fid
->unique
);
594 * search the directory for a name
595 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
596 * uniquifier through dtype
598 static int afs_lookup_filldir(struct dir_context
*ctx
, const char *name
,
599 int nlen
, loff_t fpos
, u64 ino
, unsigned dtype
)
601 struct afs_lookup_cookie
*cookie
=
602 container_of(ctx
, struct afs_lookup_cookie
, ctx
);
605 _enter("{%s,%u},%s,%u,,%llu,%u",
606 cookie
->name
.name
, cookie
->name
.len
, name
, nlen
,
607 (unsigned long long) ino
, dtype
);
609 /* insanity checks first */
610 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block
) != 2048);
611 BUILD_BUG_ON(sizeof(union afs_xdr_dirent
) != 32);
614 if (cookie
->nr_fids
< 50) {
615 cookie
->fids
[cookie
->nr_fids
].vnode
= ino
;
616 cookie
->fids
[cookie
->nr_fids
].unique
= dtype
;
619 } else if (cookie
->name
.len
== nlen
&&
620 memcmp(cookie
->name
.name
, name
, nlen
) == 0) {
621 cookie
->fids
[0].vnode
= ino
;
622 cookie
->fids
[0].unique
= dtype
;
624 if (cookie
->one_only
)
628 ret
= cookie
->nr_fids
>= 50 ? -1 : 0;
629 _leave(" = %d", ret
);
634 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
635 * files in one go and create inodes for them. The inode of the file we were
636 * asked for is returned.
638 static struct inode
*afs_do_lookup(struct inode
*dir
, struct dentry
*dentry
,
641 struct afs_lookup_cookie
*cookie
;
642 struct afs_cb_interest
*dcbi
, *cbi
= NULL
;
643 struct afs_super_info
*as
= dir
->i_sb
->s_fs_info
;
644 struct afs_status_cb
*scb
;
645 struct afs_iget_data iget_data
;
646 struct afs_fs_cursor fc
;
647 struct afs_server
*server
;
648 struct afs_vnode
*dvnode
= AFS_FS_I(dir
), *vnode
;
649 struct inode
*inode
= NULL
, *ti
;
650 afs_dataversion_t data_version
= READ_ONCE(dvnode
->status
.data_version
);
653 _enter("{%lu},%p{%pd},", dir
->i_ino
, dentry
, dentry
);
655 cookie
= kzalloc(sizeof(struct afs_lookup_cookie
), GFP_KERNEL
);
657 return ERR_PTR(-ENOMEM
);
659 cookie
->ctx
.actor
= afs_lookup_filldir
;
660 cookie
->name
= dentry
->d_name
;
661 cookie
->nr_fids
= 2; /* slot 0 is saved for the fid we actually want
662 * and slot 1 for the directory */
664 read_seqlock_excl(&dvnode
->cb_lock
);
665 dcbi
= rcu_dereference_protected(dvnode
->cb_interest
,
666 lockdep_is_held(&dvnode
->cb_lock
.lock
));
668 server
= dcbi
->server
;
670 test_bit(AFS_SERVER_FL_NO_IBULK
, &server
->flags
))
671 cookie
->one_only
= true;
673 read_sequnlock_excl(&dvnode
->cb_lock
);
675 for (i
= 0; i
< 50; i
++)
676 cookie
->fids
[i
].vid
= as
->volume
->vid
;
678 /* search the directory */
679 ret
= afs_dir_iterate(dir
, &cookie
->ctx
, key
, &data_version
);
681 inode
= ERR_PTR(ret
);
685 dentry
->d_fsdata
= (void *)(unsigned long)data_version
;
687 inode
= ERR_PTR(-ENOENT
);
691 /* Check to see if we already have an inode for the primary fid. */
692 iget_data
.fid
= cookie
->fids
[0];
693 iget_data
.volume
= dvnode
->volume
;
694 iget_data
.cb_v_break
= dvnode
->volume
->cb_v_break
;
695 iget_data
.cb_s_break
= 0;
696 inode
= ilookup5(dir
->i_sb
, cookie
->fids
[0].vnode
,
697 afs_iget5_test
, &iget_data
);
701 /* Need space for examining all the selected files */
702 inode
= ERR_PTR(-ENOMEM
);
703 cookie
->statuses
= kvcalloc(cookie
->nr_fids
, sizeof(struct afs_status_cb
),
705 if (!cookie
->statuses
)
708 cookie
->inodes
= kcalloc(cookie
->nr_fids
, sizeof(struct inode
*),
713 cookie
->fids
[1] = dvnode
->fid
;
714 cookie
->statuses
[1].cb_break
= afs_calc_vnode_cb_break(dvnode
);
715 cookie
->inodes
[1] = igrab(&dvnode
->vfs_inode
);
717 for (i
= 2; i
< cookie
->nr_fids
; i
++) {
718 scb
= &cookie
->statuses
[i
];
720 /* Find any inodes that already exist and get their
723 iget_data
.fid
= cookie
->fids
[i
];
724 ti
= ilookup5_nowait(dir
->i_sb
, iget_data
.fid
.vnode
,
725 afs_iget5_test
, &iget_data
);
726 if (!IS_ERR_OR_NULL(ti
)) {
727 vnode
= AFS_FS_I(ti
);
728 scb
->cb_break
= afs_calc_vnode_cb_break(vnode
);
729 cookie
->inodes
[i
] = ti
;
733 /* Try FS.InlineBulkStatus first. Abort codes for the individual
734 * lookups contained therein are stored in the reply without aborting
735 * the whole operation.
737 if (cookie
->one_only
)
738 goto no_inline_bulk_status
;
740 inode
= ERR_PTR(-ERESTARTSYS
);
741 if (afs_begin_vnode_operation(&fc
, dvnode
, key
, true)) {
742 while (afs_select_fileserver(&fc
)) {
743 if (test_bit(AFS_SERVER_FL_NO_IBULK
,
744 &fc
.cbi
->server
->flags
)) {
745 fc
.ac
.abort_code
= RX_INVALID_OPERATION
;
746 fc
.ac
.error
= -ECONNABORTED
;
749 iget_data
.cb_v_break
= dvnode
->volume
->cb_v_break
;
750 iget_data
.cb_s_break
= fc
.cbi
->server
->cb_s_break
;
751 afs_fs_inline_bulk_status(&fc
,
755 cookie
->nr_fids
, NULL
);
758 if (fc
.ac
.error
== 0)
759 cbi
= afs_get_cb_interest(fc
.cbi
);
760 if (fc
.ac
.abort_code
== RX_INVALID_OPERATION
)
761 set_bit(AFS_SERVER_FL_NO_IBULK
, &fc
.cbi
->server
->flags
);
762 inode
= ERR_PTR(afs_end_vnode_operation(&fc
));
767 if (fc
.ac
.abort_code
!= RX_INVALID_OPERATION
)
770 no_inline_bulk_status
:
771 /* We could try FS.BulkStatus next, but this aborts the entire op if
772 * any of the lookups fails - so, for the moment, revert to
773 * FS.FetchStatus for just the primary fid.
775 inode
= ERR_PTR(-ERESTARTSYS
);
776 if (afs_begin_vnode_operation(&fc
, dvnode
, key
, true)) {
777 while (afs_select_fileserver(&fc
)) {
778 iget_data
.cb_v_break
= dvnode
->volume
->cb_v_break
;
779 iget_data
.cb_s_break
= fc
.cbi
->server
->cb_s_break
;
780 scb
= &cookie
->statuses
[0];
781 afs_fs_fetch_status(&fc
,
788 if (fc
.ac
.error
== 0)
789 cbi
= afs_get_cb_interest(fc
.cbi
);
790 inode
= ERR_PTR(afs_end_vnode_operation(&fc
));
797 /* Turn all the files into inodes and save the first one - which is the
798 * one we actually want.
800 scb
= &cookie
->statuses
[0];
801 if (scb
->status
.abort_code
!= 0)
802 inode
= ERR_PTR(afs_abort_to_error(scb
->status
.abort_code
));
804 for (i
= 0; i
< cookie
->nr_fids
; i
++) {
805 struct afs_status_cb
*scb
= &cookie
->statuses
[i
];
807 if (!scb
->have_status
&& !scb
->have_error
)
810 if (cookie
->inodes
[i
]) {
811 struct afs_vnode
*iv
= AFS_FS_I(cookie
->inodes
[i
]);
813 if (test_bit(AFS_VNODE_UNSET
, &iv
->flags
))
816 afs_vnode_commit_status(&fc
, iv
,
817 scb
->cb_break
, NULL
, scb
);
821 if (scb
->status
.abort_code
!= 0)
824 iget_data
.fid
= cookie
->fids
[i
];
825 ti
= afs_iget(dir
->i_sb
, key
, &iget_data
, scb
, cbi
, dvnode
);
827 afs_cache_permit(AFS_FS_I(ti
), key
,
828 0 /* Assume vnode->cb_break is 0 */ +
829 iget_data
.cb_v_break
,
840 afs_put_cb_interest(afs_v2net(dvnode
), cbi
);
841 if (cookie
->inodes
) {
842 for (i
= 0; i
< cookie
->nr_fids
; i
++)
843 iput(cookie
->inodes
[i
]);
844 kfree(cookie
->inodes
);
847 kvfree(cookie
->statuses
);
854 * Look up an entry in a directory with @sys substitution.
856 static struct dentry
*afs_lookup_atsys(struct inode
*dir
, struct dentry
*dentry
,
859 struct afs_sysnames
*subs
;
860 struct afs_net
*net
= afs_i2net(dir
);
862 char *buf
, *p
, *name
;
867 ret
= ERR_PTR(-ENOMEM
);
868 p
= buf
= kmalloc(AFSNAMEMAX
, GFP_KERNEL
);
871 if (dentry
->d_name
.len
> 4) {
872 memcpy(p
, dentry
->d_name
.name
, dentry
->d_name
.len
- 4);
873 p
+= dentry
->d_name
.len
- 4;
876 /* There is an ordered list of substitutes that we have to try. */
877 read_lock(&net
->sysnames_lock
);
878 subs
= net
->sysnames
;
879 refcount_inc(&subs
->usage
);
880 read_unlock(&net
->sysnames_lock
);
882 for (i
= 0; i
< subs
->nr
; i
++) {
883 name
= subs
->subs
[i
];
884 len
= dentry
->d_name
.len
- 4 + strlen(name
);
885 if (len
>= AFSNAMEMAX
) {
886 ret
= ERR_PTR(-ENAMETOOLONG
);
891 ret
= lookup_one_len(buf
, dentry
->d_parent
, len
);
892 if (IS_ERR(ret
) || d_is_positive(ret
))
897 /* We don't want to d_add() the @sys dentry here as we don't want to
898 * the cached dentry to hide changes to the sysnames list.
902 afs_put_sysnames(subs
);
910 * look up an entry in a directory
912 static struct dentry
*afs_lookup(struct inode
*dir
, struct dentry
*dentry
,
915 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
916 struct afs_fid fid
= {};
922 _enter("{%llx:%llu},%p{%pd},",
923 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
, dentry
);
925 ASSERTCMP(d_inode(dentry
), ==, NULL
);
927 if (dentry
->d_name
.len
>= AFSNAMEMAX
) {
928 _leave(" = -ENAMETOOLONG");
929 return ERR_PTR(-ENAMETOOLONG
);
932 if (test_bit(AFS_VNODE_DELETED
, &dvnode
->flags
)) {
933 _leave(" = -ESTALE");
934 return ERR_PTR(-ESTALE
);
937 key
= afs_request_key(dvnode
->volume
->cell
);
939 _leave(" = %ld [key]", PTR_ERR(key
));
940 return ERR_CAST(key
);
943 ret
= afs_validate(dvnode
, key
);
946 _leave(" = %d [val]", ret
);
950 if (dentry
->d_name
.len
>= 4 &&
951 dentry
->d_name
.name
[dentry
->d_name
.len
- 4] == '@' &&
952 dentry
->d_name
.name
[dentry
->d_name
.len
- 3] == 's' &&
953 dentry
->d_name
.name
[dentry
->d_name
.len
- 2] == 'y' &&
954 dentry
->d_name
.name
[dentry
->d_name
.len
- 1] == 's')
955 return afs_lookup_atsys(dir
, dentry
, key
);
957 afs_stat_v(dvnode
, n_lookup
);
958 inode
= afs_do_lookup(dir
, dentry
, key
);
960 if (inode
== ERR_PTR(-ENOENT
))
961 inode
= afs_try_auto_mntpt(dentry
, dir
);
963 if (!IS_ERR_OR_NULL(inode
))
964 fid
= AFS_FS_I(inode
)->fid
;
966 d
= d_splice_alias(inode
, dentry
);
967 if (!IS_ERR_OR_NULL(d
)) {
968 d
->d_fsdata
= dentry
->d_fsdata
;
969 trace_afs_lookup(dvnode
, &d
->d_name
, &fid
);
971 trace_afs_lookup(dvnode
, &dentry
->d_name
, &fid
);
977 * Check the validity of a dentry under RCU conditions.
979 static int afs_d_revalidate_rcu(struct dentry
*dentry
)
981 struct afs_vnode
*dvnode
, *vnode
;
982 struct dentry
*parent
;
983 struct inode
*dir
, *inode
;
984 long dir_version
, de_version
;
986 _enter("%p", dentry
);
988 /* Check the parent directory is still valid first. */
989 parent
= READ_ONCE(dentry
->d_parent
);
990 dir
= d_inode_rcu(parent
);
993 dvnode
= AFS_FS_I(dir
);
994 if (test_bit(AFS_VNODE_DELETED
, &dvnode
->flags
))
997 if (!afs_check_validity(dvnode
))
1000 /* We only need to invalidate a dentry if the server's copy changed
1001 * behind our back. If we made the change, it's no problem. Note that
1002 * on a 32-bit system, we only have 32 bits in the dentry to store the
1005 dir_version
= (long)READ_ONCE(dvnode
->status
.data_version
);
1006 de_version
= (long)READ_ONCE(dentry
->d_fsdata
);
1007 if (de_version
!= dir_version
) {
1008 dir_version
= (long)READ_ONCE(dvnode
->invalid_before
);
1009 if (de_version
- dir_version
< 0)
1013 /* Check to see if the vnode referred to by the dentry still
1016 if (d_really_is_positive(dentry
)) {
1017 inode
= d_inode_rcu(dentry
);
1019 vnode
= AFS_FS_I(inode
);
1020 if (!afs_check_validity(vnode
))
1025 return 1; /* Still valid */
1029 * check that a dentry lookup hit has found a valid entry
1030 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
1033 static int afs_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1035 struct afs_vnode
*vnode
, *dir
;
1036 struct afs_fid
uninitialized_var(fid
);
1037 struct dentry
*parent
;
1038 struct inode
*inode
;
1040 afs_dataversion_t dir_version
, invalid_before
;
1044 if (flags
& LOOKUP_RCU
)
1045 return afs_d_revalidate_rcu(dentry
);
1047 if (d_really_is_positive(dentry
)) {
1048 vnode
= AFS_FS_I(d_inode(dentry
));
1049 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
1050 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
,
1053 _enter("{neg n=%pd}", dentry
);
1056 key
= afs_request_key(AFS_FS_S(dentry
->d_sb
)->volume
->cell
);
1060 if (d_really_is_positive(dentry
)) {
1061 inode
= d_inode(dentry
);
1063 vnode
= AFS_FS_I(inode
);
1064 afs_validate(vnode
, key
);
1065 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
1070 /* lock down the parent dentry so we can peer at it */
1071 parent
= dget_parent(dentry
);
1072 dir
= AFS_FS_I(d_inode(parent
));
1074 /* validate the parent directory */
1075 afs_validate(dir
, key
);
1077 if (test_bit(AFS_VNODE_DELETED
, &dir
->flags
)) {
1078 _debug("%pd: parent dir deleted", dentry
);
1079 goto out_bad_parent
;
1082 /* We only need to invalidate a dentry if the server's copy changed
1083 * behind our back. If we made the change, it's no problem. Note that
1084 * on a 32-bit system, we only have 32 bits in the dentry to store the
1087 dir_version
= dir
->status
.data_version
;
1088 de_version
= (long)dentry
->d_fsdata
;
1089 if (de_version
== (long)dir_version
)
1090 goto out_valid_noupdate
;
1092 invalid_before
= dir
->invalid_before
;
1093 if (de_version
- (long)invalid_before
>= 0)
1096 _debug("dir modified");
1097 afs_stat_v(dir
, n_reval
);
1099 /* search the directory for this vnode */
1100 ret
= afs_do_lookup_one(&dir
->vfs_inode
, dentry
, &fid
, key
, &dir_version
);
1103 /* the filename maps to something */
1104 if (d_really_is_negative(dentry
))
1105 goto out_bad_parent
;
1106 inode
= d_inode(dentry
);
1107 if (is_bad_inode(inode
)) {
1108 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1110 goto out_bad_parent
;
1113 vnode
= AFS_FS_I(inode
);
1115 /* if the vnode ID has changed, then the dirent points to a
1117 if (fid
.vnode
!= vnode
->fid
.vnode
) {
1118 _debug("%pd: dirent changed [%llu != %llu]",
1124 /* if the vnode ID uniqifier has changed, then the file has
1125 * been deleted and replaced, and the original vnode ID has
1127 if (fid
.unique
!= vnode
->fid
.unique
) {
1128 _debug("%pd: file deleted (uq %u -> %u I:%u)",
1131 vnode
->vfs_inode
.i_generation
);
1132 write_seqlock(&vnode
->cb_lock
);
1133 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
1134 write_sequnlock(&vnode
->cb_lock
);
1140 /* the filename is unknown */
1141 _debug("%pd: dirent not found", dentry
);
1142 if (d_really_is_positive(dentry
))
1147 _debug("failed to iterate dir %pd: %d",
1149 goto out_bad_parent
;
1153 dentry
->d_fsdata
= (void *)(unsigned long)dir_version
;
1157 _leave(" = 1 [valid]");
1160 /* the dirent, if it exists, now points to a different vnode */
1162 spin_lock(&dentry
->d_lock
);
1163 dentry
->d_flags
|= DCACHE_NFSFS_RENAMED
;
1164 spin_unlock(&dentry
->d_lock
);
1167 _debug("dropping dentry %pd2", dentry
);
1172 _leave(" = 0 [bad]");
1177 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1179 * - called from dput() when d_count is going to 0.
1180 * - return 1 to request dentry be unhashed, 0 otherwise
1182 static int afs_d_delete(const struct dentry
*dentry
)
1184 _enter("%pd", dentry
);
1186 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
1189 if (d_really_is_positive(dentry
) &&
1190 (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(d_inode(dentry
))->flags
) ||
1191 test_bit(AFS_VNODE_PSEUDODIR
, &AFS_FS_I(d_inode(dentry
))->flags
)))
1194 _leave(" = 0 [keep]");
1198 _leave(" = 1 [zap]");
1203 * Clean up sillyrename files on dentry removal.
1205 static void afs_d_iput(struct dentry
*dentry
, struct inode
*inode
)
1207 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
1208 afs_silly_iput(dentry
, inode
);
1213 * handle dentry release
1215 void afs_d_release(struct dentry
*dentry
)
1217 _enter("%pd", dentry
);
1221 * Create a new inode for create/mkdir/symlink
1223 static void afs_vnode_new_inode(struct afs_fs_cursor
*fc
,
1224 struct dentry
*new_dentry
,
1225 struct afs_iget_data
*new_data
,
1226 struct afs_status_cb
*new_scb
)
1228 struct afs_vnode
*vnode
;
1229 struct inode
*inode
;
1231 if (fc
->ac
.error
< 0)
1234 inode
= afs_iget(fc
->vnode
->vfs_inode
.i_sb
, fc
->key
,
1235 new_data
, new_scb
, fc
->cbi
, fc
->vnode
);
1236 if (IS_ERR(inode
)) {
1237 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1238 * the new directory on the server.
1240 fc
->ac
.error
= PTR_ERR(inode
);
1244 vnode
= AFS_FS_I(inode
);
1245 set_bit(AFS_VNODE_NEW_CONTENT
, &vnode
->flags
);
1246 if (fc
->ac
.error
== 0)
1247 afs_cache_permit(vnode
, fc
->key
, vnode
->cb_break
, new_scb
);
1248 d_instantiate(new_dentry
, inode
);
1251 static void afs_prep_for_new_inode(struct afs_fs_cursor
*fc
,
1252 struct afs_iget_data
*iget_data
)
1254 iget_data
->volume
= fc
->vnode
->volume
;
1255 iget_data
->cb_v_break
= fc
->vnode
->volume
->cb_v_break
;
1256 iget_data
->cb_s_break
= fc
->cbi
->server
->cb_s_break
;
1260 * Note that a dentry got changed. We need to set d_fsdata to the data version
1261 * number derived from the result of the operation. It doesn't matter if
1262 * d_fsdata goes backwards as we'll just revalidate.
1264 static void afs_update_dentry_version(struct afs_fs_cursor
*fc
,
1265 struct dentry
*dentry
,
1266 struct afs_status_cb
*scb
)
1268 if (fc
->ac
.error
== 0)
1270 (void *)(unsigned long)scb
->status
.data_version
;
1274 * create a directory on an AFS filesystem
1276 static int afs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
1278 struct afs_iget_data iget_data
;
1279 struct afs_status_cb
*scb
;
1280 struct afs_fs_cursor fc
;
1281 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1283 afs_dataversion_t data_version
;
1288 _enter("{%llx:%llu},{%pd},%ho",
1289 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
, mode
);
1292 scb
= kcalloc(2, sizeof(struct afs_status_cb
), GFP_KERNEL
);
1296 key
= afs_request_key(dvnode
->volume
->cell
);
1303 if (afs_begin_vnode_operation(&fc
, dvnode
, key
, true)) {
1304 data_version
= dvnode
->status
.data_version
+ 1;
1306 while (afs_select_fileserver(&fc
)) {
1307 fc
.cb_break
= afs_calc_vnode_cb_break(dvnode
);
1308 afs_prep_for_new_inode(&fc
, &iget_data
);
1309 afs_fs_create(&fc
, dentry
->d_name
.name
, mode
,
1310 &scb
[0], &iget_data
.fid
, &scb
[1]);
1313 afs_check_for_remote_deletion(&fc
, dvnode
);
1314 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
,
1315 &data_version
, &scb
[0]);
1316 afs_update_dentry_version(&fc
, dentry
, &scb
[0]);
1317 afs_vnode_new_inode(&fc
, dentry
, &iget_data
, &scb
[1]);
1318 ret
= afs_end_vnode_operation(&fc
);
1326 down_write(&dvnode
->validate_lock
);
1327 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) &&
1328 dvnode
->status
.data_version
== data_version
)
1329 afs_edit_dir_add(dvnode
, &dentry
->d_name
, &iget_data
.fid
,
1330 afs_edit_dir_for_create
);
1331 up_write(&dvnode
->validate_lock
);
1345 _leave(" = %d", ret
);
1350 * Remove a subdir from a directory.
1352 static void afs_dir_remove_subdir(struct dentry
*dentry
)
1354 if (d_really_is_positive(dentry
)) {
1355 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
1357 clear_nlink(&vnode
->vfs_inode
);
1358 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
1359 clear_bit(AFS_VNODE_CB_PROMISED
, &vnode
->flags
);
1360 clear_bit(AFS_VNODE_DIR_VALID
, &vnode
->flags
);
1365 * remove a directory from an AFS filesystem
1367 static int afs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1369 struct afs_status_cb
*scb
;
1370 struct afs_fs_cursor fc
;
1371 struct afs_vnode
*dvnode
= AFS_FS_I(dir
), *vnode
= NULL
;
1373 afs_dataversion_t data_version
;
1376 _enter("{%llx:%llu},{%pd}",
1377 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
);
1379 scb
= kzalloc(sizeof(struct afs_status_cb
), GFP_KERNEL
);
1383 key
= afs_request_key(dvnode
->volume
->cell
);
1389 /* Try to make sure we have a callback promise on the victim. */
1390 if (d_really_is_positive(dentry
)) {
1391 vnode
= AFS_FS_I(d_inode(dentry
));
1392 ret
= afs_validate(vnode
, key
);
1398 ret
= down_write_killable(&vnode
->rmdir_lock
);
1404 if (afs_begin_vnode_operation(&fc
, dvnode
, key
, true)) {
1405 data_version
= dvnode
->status
.data_version
+ 1;
1407 while (afs_select_fileserver(&fc
)) {
1408 fc
.cb_break
= afs_calc_vnode_cb_break(dvnode
);
1409 afs_fs_remove(&fc
, vnode
, dentry
->d_name
.name
, true, scb
);
1412 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
,
1413 &data_version
, scb
);
1414 afs_update_dentry_version(&fc
, dentry
, scb
);
1415 ret
= afs_end_vnode_operation(&fc
);
1417 afs_dir_remove_subdir(dentry
);
1418 down_write(&dvnode
->validate_lock
);
1419 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) &&
1420 dvnode
->status
.data_version
== data_version
)
1421 afs_edit_dir_remove(dvnode
, &dentry
->d_name
,
1422 afs_edit_dir_for_rmdir
);
1423 up_write(&dvnode
->validate_lock
);
1428 up_write(&vnode
->rmdir_lock
);
1437 * Remove a link to a file or symlink from a directory.
1439 * If the file was not deleted due to excess hard links, the fileserver will
1440 * break the callback promise on the file - if it had one - before it returns
1441 * to us, and if it was deleted, it won't
1443 * However, if we didn't have a callback promise outstanding, or it was
1444 * outstanding on a different server, then it won't break it either...
1446 static int afs_dir_remove_link(struct afs_vnode
*dvnode
, struct dentry
*dentry
,
1451 if (d_really_is_positive(dentry
)) {
1452 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
1454 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
1456 } else if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
)) {
1457 write_seqlock(&vnode
->cb_lock
);
1458 drop_nlink(&vnode
->vfs_inode
);
1459 if (vnode
->vfs_inode
.i_nlink
== 0) {
1460 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
1461 __afs_break_callback(vnode
, afs_cb_break_for_unlink
);
1463 write_sequnlock(&vnode
->cb_lock
);
1466 afs_break_callback(vnode
, afs_cb_break_for_unlink
);
1468 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
1469 kdebug("AFS_VNODE_DELETED");
1471 ret
= afs_validate(vnode
, key
);
1475 _debug("nlink %d [val %d]", vnode
->vfs_inode
.i_nlink
, ret
);
1482 * Remove a file or symlink from an AFS filesystem.
1484 static int afs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1486 struct afs_fs_cursor fc
;
1487 struct afs_status_cb
*scb
;
1488 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1489 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
1491 bool need_rehash
= false;
1494 _enter("{%llx:%llu},{%pd}",
1495 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
);
1497 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1498 return -ENAMETOOLONG
;
1501 scb
= kcalloc(2, sizeof(struct afs_status_cb
), GFP_KERNEL
);
1505 key
= afs_request_key(dvnode
->volume
->cell
);
1511 /* Try to make sure we have a callback promise on the victim. */
1512 ret
= afs_validate(vnode
, key
);
1516 spin_lock(&dentry
->d_lock
);
1517 if (d_count(dentry
) > 1) {
1518 spin_unlock(&dentry
->d_lock
);
1519 /* Start asynchronous writeout of the inode */
1520 write_inode_now(d_inode(dentry
), 0);
1521 ret
= afs_sillyrename(dvnode
, vnode
, dentry
, key
);
1524 if (!d_unhashed(dentry
)) {
1525 /* Prevent a race with RCU lookup. */
1529 spin_unlock(&dentry
->d_lock
);
1532 if (afs_begin_vnode_operation(&fc
, dvnode
, key
, true)) {
1533 afs_dataversion_t data_version
= dvnode
->status
.data_version
+ 1;
1534 afs_dataversion_t data_version_2
= vnode
->status
.data_version
;
1536 while (afs_select_fileserver(&fc
)) {
1537 fc
.cb_break
= afs_calc_vnode_cb_break(dvnode
);
1538 fc
.cb_break_2
= afs_calc_vnode_cb_break(vnode
);
1540 if (test_bit(AFS_SERVER_FL_IS_YFS
, &fc
.cbi
->server
->flags
) &&
1541 !test_bit(AFS_SERVER_FL_NO_RM2
, &fc
.cbi
->server
->flags
)) {
1542 yfs_fs_remove_file2(&fc
, vnode
, dentry
->d_name
.name
,
1544 if (fc
.ac
.error
!= -ECONNABORTED
||
1545 fc
.ac
.abort_code
!= RXGEN_OPCODE
)
1547 set_bit(AFS_SERVER_FL_NO_RM2
, &fc
.cbi
->server
->flags
);
1550 afs_fs_remove(&fc
, vnode
, dentry
->d_name
.name
, false, &scb
[0]);
1553 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
,
1554 &data_version
, &scb
[0]);
1555 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break_2
,
1556 &data_version_2
, &scb
[1]);
1557 afs_update_dentry_version(&fc
, dentry
, &scb
[0]);
1558 ret
= afs_end_vnode_operation(&fc
);
1559 if (ret
== 0 && !(scb
[1].have_status
|| scb
[1].have_error
))
1560 ret
= afs_dir_remove_link(dvnode
, dentry
, key
);
1563 down_write(&dvnode
->validate_lock
);
1564 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) &&
1565 dvnode
->status
.data_version
== data_version
)
1566 afs_edit_dir_remove(dvnode
, &dentry
->d_name
,
1567 afs_edit_dir_for_unlink
);
1568 up_write(&dvnode
->validate_lock
);
1572 if (need_rehash
&& ret
< 0 && ret
!= -ENOENT
)
1580 _leave(" = %d", ret
);
1585 * create a regular file on an AFS filesystem
1587 static int afs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
1590 struct afs_iget_data iget_data
;
1591 struct afs_fs_cursor fc
;
1592 struct afs_status_cb
*scb
;
1593 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1595 afs_dataversion_t data_version
;
1600 _enter("{%llx:%llu},{%pd},%ho,",
1601 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
, mode
);
1603 ret
= -ENAMETOOLONG
;
1604 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1607 key
= afs_request_key(dvnode
->volume
->cell
);
1614 scb
= kcalloc(2, sizeof(struct afs_status_cb
), GFP_KERNEL
);
1619 if (afs_begin_vnode_operation(&fc
, dvnode
, key
, true)) {
1620 data_version
= dvnode
->status
.data_version
+ 1;
1622 while (afs_select_fileserver(&fc
)) {
1623 fc
.cb_break
= afs_calc_vnode_cb_break(dvnode
);
1624 afs_prep_for_new_inode(&fc
, &iget_data
);
1625 afs_fs_create(&fc
, dentry
->d_name
.name
, mode
,
1626 &scb
[0], &iget_data
.fid
, &scb
[1]);
1629 afs_check_for_remote_deletion(&fc
, dvnode
);
1630 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
,
1631 &data_version
, &scb
[0]);
1632 afs_update_dentry_version(&fc
, dentry
, &scb
[0]);
1633 afs_vnode_new_inode(&fc
, dentry
, &iget_data
, &scb
[1]);
1634 ret
= afs_end_vnode_operation(&fc
);
1641 down_write(&dvnode
->validate_lock
);
1642 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) &&
1643 dvnode
->status
.data_version
== data_version
)
1644 afs_edit_dir_add(dvnode
, &dentry
->d_name
, &iget_data
.fid
,
1645 afs_edit_dir_for_create
);
1646 up_write(&dvnode
->validate_lock
);
1659 _leave(" = %d", ret
);
1664 * create a hard link between files in an AFS filesystem
1666 static int afs_link(struct dentry
*from
, struct inode
*dir
,
1667 struct dentry
*dentry
)
1669 struct afs_fs_cursor fc
;
1670 struct afs_status_cb
*scb
;
1671 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1672 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(from
));
1674 afs_dataversion_t data_version
;
1677 _enter("{%llx:%llu},{%llx:%llu},{%pd}",
1678 vnode
->fid
.vid
, vnode
->fid
.vnode
,
1679 dvnode
->fid
.vid
, dvnode
->fid
.vnode
,
1682 ret
= -ENAMETOOLONG
;
1683 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1687 scb
= kcalloc(2, sizeof(struct afs_status_cb
), GFP_KERNEL
);
1691 key
= afs_request_key(dvnode
->volume
->cell
);
1698 if (afs_begin_vnode_operation(&fc
, dvnode
, key
, true)) {
1699 data_version
= dvnode
->status
.data_version
+ 1;
1701 if (mutex_lock_interruptible_nested(&vnode
->io_lock
, 1) < 0) {
1702 afs_end_vnode_operation(&fc
);
1706 while (afs_select_fileserver(&fc
)) {
1707 fc
.cb_break
= afs_calc_vnode_cb_break(dvnode
);
1708 fc
.cb_break_2
= afs_calc_vnode_cb_break(vnode
);
1709 afs_fs_link(&fc
, vnode
, dentry
->d_name
.name
,
1713 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
,
1714 &data_version
, &scb
[0]);
1715 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break_2
,
1717 ihold(&vnode
->vfs_inode
);
1718 afs_update_dentry_version(&fc
, dentry
, &scb
[0]);
1719 d_instantiate(dentry
, &vnode
->vfs_inode
);
1721 mutex_unlock(&vnode
->io_lock
);
1722 ret
= afs_end_vnode_operation(&fc
);
1729 down_write(&dvnode
->validate_lock
);
1730 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) &&
1731 dvnode
->status
.data_version
== data_version
)
1732 afs_edit_dir_add(dvnode
, &dentry
->d_name
, &vnode
->fid
,
1733 afs_edit_dir_for_link
);
1734 up_write(&dvnode
->validate_lock
);
1747 _leave(" = %d", ret
);
1752 * create a symlink in an AFS filesystem
1754 static int afs_symlink(struct inode
*dir
, struct dentry
*dentry
,
1755 const char *content
)
1757 struct afs_iget_data iget_data
;
1758 struct afs_fs_cursor fc
;
1759 struct afs_status_cb
*scb
;
1760 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1762 afs_dataversion_t data_version
;
1765 _enter("{%llx:%llu},{%pd},%s",
1766 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
,
1769 ret
= -ENAMETOOLONG
;
1770 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1774 if (strlen(content
) >= AFSPATHMAX
)
1778 scb
= kcalloc(2, sizeof(struct afs_status_cb
), GFP_KERNEL
);
1782 key
= afs_request_key(dvnode
->volume
->cell
);
1789 if (afs_begin_vnode_operation(&fc
, dvnode
, key
, true)) {
1790 data_version
= dvnode
->status
.data_version
+ 1;
1792 while (afs_select_fileserver(&fc
)) {
1793 fc
.cb_break
= afs_calc_vnode_cb_break(dvnode
);
1794 afs_prep_for_new_inode(&fc
, &iget_data
);
1795 afs_fs_symlink(&fc
, dentry
->d_name
.name
, content
,
1796 &scb
[0], &iget_data
.fid
, &scb
[1]);
1799 afs_check_for_remote_deletion(&fc
, dvnode
);
1800 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
,
1801 &data_version
, &scb
[0]);
1802 afs_update_dentry_version(&fc
, dentry
, &scb
[0]);
1803 afs_vnode_new_inode(&fc
, dentry
, &iget_data
, &scb
[1]);
1804 ret
= afs_end_vnode_operation(&fc
);
1811 down_write(&dvnode
->validate_lock
);
1812 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) &&
1813 dvnode
->status
.data_version
== data_version
)
1814 afs_edit_dir_add(dvnode
, &dentry
->d_name
, &iget_data
.fid
,
1815 afs_edit_dir_for_symlink
);
1816 up_write(&dvnode
->validate_lock
);
1829 _leave(" = %d", ret
);
1834 * rename a file in an AFS filesystem and/or move it between directories
1836 static int afs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1837 struct inode
*new_dir
, struct dentry
*new_dentry
,
1840 struct afs_fs_cursor fc
;
1841 struct afs_status_cb
*scb
;
1842 struct afs_vnode
*orig_dvnode
, *new_dvnode
, *vnode
;
1843 struct dentry
*tmp
= NULL
, *rehash
= NULL
;
1844 struct inode
*new_inode
;
1846 afs_dataversion_t orig_data_version
;
1847 afs_dataversion_t new_data_version
;
1848 bool new_negative
= d_is_negative(new_dentry
);
1854 /* Don't allow silly-rename files be moved around. */
1855 if (old_dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
1858 vnode
= AFS_FS_I(d_inode(old_dentry
));
1859 orig_dvnode
= AFS_FS_I(old_dir
);
1860 new_dvnode
= AFS_FS_I(new_dir
);
1862 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1863 orig_dvnode
->fid
.vid
, orig_dvnode
->fid
.vnode
,
1864 vnode
->fid
.vid
, vnode
->fid
.vnode
,
1865 new_dvnode
->fid
.vid
, new_dvnode
->fid
.vnode
,
1869 scb
= kcalloc(2, sizeof(struct afs_status_cb
), GFP_KERNEL
);
1873 key
= afs_request_key(orig_dvnode
->volume
->cell
);
1879 /* For non-directories, check whether the target is busy and if so,
1880 * make a copy of the dentry and then do a silly-rename. If the
1881 * silly-rename succeeds, the copied dentry is hashed and becomes the
1884 if (d_is_positive(new_dentry
) && !d_is_dir(new_dentry
)) {
1885 /* To prevent any new references to the target during the
1886 * rename, we unhash the dentry in advance.
1888 if (!d_unhashed(new_dentry
)) {
1890 rehash
= new_dentry
;
1893 if (d_count(new_dentry
) > 2) {
1894 /* copy the target dentry's name */
1896 tmp
= d_alloc(new_dentry
->d_parent
,
1897 &new_dentry
->d_name
);
1901 ret
= afs_sillyrename(new_dvnode
,
1902 AFS_FS_I(d_inode(new_dentry
)),
1909 new_negative
= true;
1913 /* This bit is potentially nasty as there's a potential race with
1914 * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry
1915 * to reflect it's new parent's new data_version after the op, but
1916 * d_revalidate may see old_dentry between the op having taken place
1917 * and the version being updated.
1919 * So drop the old_dentry for now to make other threads go through
1920 * lookup instead - which we hold a lock against.
1925 if (afs_begin_vnode_operation(&fc
, orig_dvnode
, key
, true)) {
1926 orig_data_version
= orig_dvnode
->status
.data_version
+ 1;
1928 if (orig_dvnode
!= new_dvnode
) {
1929 if (mutex_lock_interruptible_nested(&new_dvnode
->io_lock
, 1) < 0) {
1930 afs_end_vnode_operation(&fc
);
1931 goto error_rehash_old
;
1933 new_data_version
= new_dvnode
->status
.data_version
+ 1;
1935 new_data_version
= orig_data_version
;
1938 while (afs_select_fileserver(&fc
)) {
1939 fc
.cb_break
= afs_calc_vnode_cb_break(orig_dvnode
);
1940 fc
.cb_break_2
= afs_calc_vnode_cb_break(new_dvnode
);
1941 afs_fs_rename(&fc
, old_dentry
->d_name
.name
,
1942 new_dvnode
, new_dentry
->d_name
.name
,
1946 afs_vnode_commit_status(&fc
, orig_dvnode
, fc
.cb_break
,
1947 &orig_data_version
, &scb
[0]);
1948 if (new_dvnode
!= orig_dvnode
) {
1949 afs_vnode_commit_status(&fc
, new_dvnode
, fc
.cb_break_2
,
1950 &new_data_version
, &scb
[1]);
1951 mutex_unlock(&new_dvnode
->io_lock
);
1953 ret
= afs_end_vnode_operation(&fc
);
1955 goto error_rehash_old
;
1961 down_write(&orig_dvnode
->validate_lock
);
1962 if (test_bit(AFS_VNODE_DIR_VALID
, &orig_dvnode
->flags
) &&
1963 orig_dvnode
->status
.data_version
== orig_data_version
)
1964 afs_edit_dir_remove(orig_dvnode
, &old_dentry
->d_name
,
1965 afs_edit_dir_for_rename_0
);
1966 if (orig_dvnode
!= new_dvnode
) {
1967 up_write(&orig_dvnode
->validate_lock
);
1969 down_write(&new_dvnode
->validate_lock
);
1971 if (test_bit(AFS_VNODE_DIR_VALID
, &new_dvnode
->flags
) &&
1972 orig_dvnode
->status
.data_version
== new_data_version
) {
1974 afs_edit_dir_remove(new_dvnode
, &new_dentry
->d_name
,
1975 afs_edit_dir_for_rename_1
);
1977 afs_edit_dir_add(new_dvnode
, &new_dentry
->d_name
,
1978 &vnode
->fid
, afs_edit_dir_for_rename_2
);
1981 new_inode
= d_inode(new_dentry
);
1983 spin_lock(&new_inode
->i_lock
);
1984 if (new_inode
->i_nlink
> 0)
1985 drop_nlink(new_inode
);
1986 spin_unlock(&new_inode
->i_lock
);
1989 /* Now we can update d_fsdata on the dentries to reflect their
1990 * new parent's data_version.
1992 * Note that if we ever implement RENAME_EXCHANGE, we'll have
1993 * to update both dentries with opposing dir versions.
1995 afs_update_dentry_version(&fc
, old_dentry
, &scb
[1]);
1996 afs_update_dentry_version(&fc
, new_dentry
, &scb
[1]);
1997 d_move(old_dentry
, new_dentry
);
1998 up_write(&new_dvnode
->validate_lock
);
2003 d_rehash(new_dentry
);
2014 _leave(" = %d", ret
);
2019 * Release a directory page and clean up its private state if it's not busy
2020 * - return true if the page can now be released, false if not
2022 static int afs_dir_releasepage(struct page
*page
, gfp_t gfp_flags
)
2024 struct afs_vnode
*dvnode
= AFS_FS_I(page
->mapping
->host
);
2026 _enter("{{%llx:%llu}[%lu]}", dvnode
->fid
.vid
, dvnode
->fid
.vnode
, page
->index
);
2028 set_page_private(page
, 0);
2029 ClearPagePrivate(page
);
2031 /* The directory will need reloading. */
2032 if (test_and_clear_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
))
2033 afs_stat_v(dvnode
, n_relpg
);
2038 * invalidate part or all of a page
2039 * - release a page and clean up its private data if offset is 0 (indicating
2042 static void afs_dir_invalidatepage(struct page
*page
, unsigned int offset
,
2043 unsigned int length
)
2045 struct afs_vnode
*dvnode
= AFS_FS_I(page
->mapping
->host
);
2047 _enter("{%lu},%u,%u", page
->index
, offset
, length
);
2049 BUG_ON(!PageLocked(page
));
2051 /* The directory will need reloading. */
2052 if (test_and_clear_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
))
2053 afs_stat_v(dvnode
, n_inval
);
2055 /* we clean up only if the entire page is being invalidated */
2056 if (offset
== 0 && length
== PAGE_SIZE
) {
2057 set_page_private(page
, 0);
2058 ClearPagePrivate(page
);