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/iversion.h>
16 #include <linux/iov_iter.h>
17 #include <linux/task_io_accounting_ops.h>
22 static struct dentry
*afs_lookup(struct inode
*dir
, struct dentry
*dentry
,
24 static int afs_dir_open(struct inode
*inode
, struct file
*file
);
25 static int afs_readdir(struct file
*file
, struct dir_context
*ctx
);
26 static int afs_d_revalidate(struct inode
*dir
, const struct qstr
*name
,
27 struct dentry
*dentry
, unsigned int flags
);
28 static int afs_d_delete(const struct dentry
*dentry
);
29 static void afs_d_iput(struct dentry
*dentry
, struct inode
*inode
);
30 static bool afs_lookup_one_filldir(struct dir_context
*ctx
, const char *name
, int nlen
,
31 loff_t fpos
, u64 ino
, unsigned dtype
);
32 static bool afs_lookup_filldir(struct dir_context
*ctx
, const char *name
, int nlen
,
33 loff_t fpos
, u64 ino
, unsigned dtype
);
34 static int afs_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
35 struct dentry
*dentry
, umode_t mode
, bool excl
);
36 static int afs_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
37 struct dentry
*dentry
, umode_t mode
);
38 static int afs_rmdir(struct inode
*dir
, struct dentry
*dentry
);
39 static int afs_unlink(struct inode
*dir
, struct dentry
*dentry
);
40 static int afs_link(struct dentry
*from
, struct inode
*dir
,
41 struct dentry
*dentry
);
42 static int afs_symlink(struct mnt_idmap
*idmap
, struct inode
*dir
,
43 struct dentry
*dentry
, const char *content
);
44 static int afs_rename(struct mnt_idmap
*idmap
, struct inode
*old_dir
,
45 struct dentry
*old_dentry
, struct inode
*new_dir
,
46 struct dentry
*new_dentry
, unsigned int flags
);
48 const struct file_operations afs_dir_file_operations
= {
50 .release
= afs_release
,
51 .iterate_shared
= afs_readdir
,
53 .llseek
= generic_file_llseek
,
56 const struct inode_operations afs_dir_inode_operations
= {
61 .symlink
= afs_symlink
,
65 .permission
= afs_permission
,
66 .getattr
= afs_getattr
,
67 .setattr
= afs_setattr
,
70 const struct address_space_operations afs_dir_aops
= {
71 .writepages
= afs_single_writepages
,
74 const struct dentry_operations afs_fs_dentry_operations
= {
75 .d_revalidate
= afs_d_revalidate
,
76 .d_delete
= afs_d_delete
,
77 .d_release
= afs_d_release
,
78 .d_automount
= afs_d_automount
,
82 struct afs_lookup_one_cookie
{
83 struct dir_context ctx
;
89 struct afs_lookup_cookie
{
90 struct dir_context ctx
;
92 unsigned short nr_fids
;
93 struct afs_fid fids
[50];
96 static void afs_dir_unuse_cookie(struct afs_vnode
*dvnode
, int ret
)
99 struct afs_vnode_cache_aux aux
;
100 loff_t i_size
= i_size_read(&dvnode
->netfs
.inode
);
102 afs_set_cache_aux(dvnode
, &aux
);
103 fscache_unuse_cookie(afs_vnode_cache(dvnode
), &aux
, &i_size
);
105 fscache_unuse_cookie(afs_vnode_cache(dvnode
), NULL
, NULL
);
110 * Iterate through a kmapped directory segment, dumping a summary of
113 static size_t afs_dir_dump_step(void *iter_base
, size_t progress
, size_t len
,
114 void *priv
, void *priv2
)
117 union afs_xdr_dir_block
*block
= iter_base
;
119 pr_warn("[%05zx] %32phN\n", progress
, block
);
120 iter_base
+= AFS_DIR_BLOCK_SIZE
;
121 progress
+= AFS_DIR_BLOCK_SIZE
;
122 len
-= AFS_DIR_BLOCK_SIZE
;
129 * Dump the contents of a directory.
131 static void afs_dir_dump(struct afs_vnode
*dvnode
)
133 struct iov_iter iter
;
134 unsigned long long i_size
= i_size_read(&dvnode
->netfs
.inode
);
136 pr_warn("DIR %llx:%llx is=%llx\n",
137 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, i_size
);
139 iov_iter_folio_queue(&iter
, ITER_SOURCE
, dvnode
->directory
, 0, 0, i_size
);
140 iterate_folioq(&iter
, iov_iter_count(&iter
), NULL
, NULL
,
145 * check that a directory folio is valid
147 static bool afs_dir_check_block(struct afs_vnode
*dvnode
, size_t progress
,
148 union afs_xdr_dir_block
*block
)
150 if (block
->hdr
.magic
!= AFS_DIR_MAGIC
) {
151 pr_warn("%s(%lx): [%zx] bad magic %04x\n",
152 __func__
, dvnode
->netfs
.inode
.i_ino
,
153 progress
, ntohs(block
->hdr
.magic
));
154 trace_afs_dir_check_failed(dvnode
, progress
);
155 trace_afs_file_error(dvnode
, -EIO
, afs_file_error_dir_bad_magic
);
159 /* Make sure each block is NUL terminated so we can reasonably
160 * use string functions on it. The filenames in the folio
161 * *should* be NUL-terminated anyway.
163 ((u8
*)block
)[AFS_DIR_BLOCK_SIZE
- 1] = 0;
164 afs_stat_v(dvnode
, n_read_dir
);
169 * Iterate through a kmapped directory segment, checking the content.
171 static size_t afs_dir_check_step(void *iter_base
, size_t progress
, size_t len
,
172 void *priv
, void *priv2
)
174 struct afs_vnode
*dvnode
= priv
;
176 if (WARN_ON_ONCE(progress
% AFS_DIR_BLOCK_SIZE
||
177 len
% AFS_DIR_BLOCK_SIZE
))
181 if (!afs_dir_check_block(dvnode
, progress
, iter_base
))
183 iter_base
+= AFS_DIR_BLOCK_SIZE
;
184 len
-= AFS_DIR_BLOCK_SIZE
;
191 * Check all the blocks in a directory.
193 static int afs_dir_check(struct afs_vnode
*dvnode
)
195 struct iov_iter iter
;
196 unsigned long long i_size
= i_size_read(&dvnode
->netfs
.inode
);
199 if (unlikely(!i_size
))
202 iov_iter_folio_queue(&iter
, ITER_SOURCE
, dvnode
->directory
, 0, 0, i_size
);
203 checked
= iterate_folioq(&iter
, iov_iter_count(&iter
), dvnode
, NULL
,
205 if (checked
!= i_size
) {
206 afs_dir_dump(dvnode
);
213 * open an AFS directory file
215 static int afs_dir_open(struct inode
*inode
, struct file
*file
)
217 _enter("{%lu}", inode
->i_ino
);
219 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block
) != 2048);
220 BUILD_BUG_ON(sizeof(union afs_xdr_dirent
) != 32);
222 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(inode
)->flags
))
225 return afs_open(inode
, file
);
229 * Read a file in a single download.
231 static ssize_t
afs_do_read_single(struct afs_vnode
*dvnode
, struct file
*file
)
233 struct iov_iter iter
;
236 bool is_dir
= (S_ISDIR(dvnode
->netfs
.inode
.i_mode
) &&
237 !test_bit(AFS_VNODE_MOUNTPOINT
, &dvnode
->flags
));
239 i_size
= i_size_read(&dvnode
->netfs
.inode
);
241 if (i_size
< AFS_DIR_BLOCK_SIZE
)
242 return afs_bad(dvnode
, afs_file_error_dir_small
);
243 if (i_size
> AFS_DIR_BLOCK_SIZE
* 1024) {
244 trace_afs_file_error(dvnode
, -EFBIG
, afs_file_error_dir_big
);
248 if (i_size
> AFSPATHMAX
) {
249 trace_afs_file_error(dvnode
, -EFBIG
, afs_file_error_dir_big
);
254 /* Expand the storage. TODO: Shrink the storage too. */
255 if (dvnode
->directory_size
< i_size
) {
256 size_t cur_size
= dvnode
->directory_size
;
258 ret
= netfs_alloc_folioq_buffer(NULL
,
259 &dvnode
->directory
, &cur_size
, i_size
,
260 mapping_gfp_mask(dvnode
->netfs
.inode
.i_mapping
));
261 dvnode
->directory_size
= cur_size
;
266 iov_iter_folio_queue(&iter
, ITER_DEST
, dvnode
->directory
, 0, 0, dvnode
->directory_size
);
268 /* AFS requires us to perform the read of a directory synchronously as
269 * a single unit to avoid issues with the directory contents being
270 * changed between reads.
272 ret
= netfs_read_single(&dvnode
->netfs
.inode
, file
, &iter
);
274 i_size
= i_size_read(&dvnode
->netfs
.inode
);
276 /* The content has grown, so we need to expand the
281 int ret2
= afs_dir_check(dvnode
);
285 } else if (i_size
< folioq_folio_size(dvnode
->directory
, 0)) {
286 /* NUL-terminate a symlink. */
287 char *symlink
= kmap_local_folio(folioq_folio(dvnode
->directory
, 0), 0);
290 kunmap_local(symlink
);
297 ssize_t
afs_read_single(struct afs_vnode
*dvnode
, struct file
*file
)
301 fscache_use_cookie(afs_vnode_cache(dvnode
), false);
302 ret
= afs_do_read_single(dvnode
, file
);
303 fscache_unuse_cookie(afs_vnode_cache(dvnode
), NULL
, NULL
);
308 * Read the directory into a folio_queue buffer in one go, scrubbing the
309 * previous contents. We return -ESTALE if the caller needs to call us again.
311 ssize_t
afs_read_dir(struct afs_vnode
*dvnode
, struct file
*file
)
312 __acquires(&dvnode
->validate_lock
)
317 i_size
= i_size_read(&dvnode
->netfs
.inode
);
320 if (down_read_killable(&dvnode
->validate_lock
) < 0)
323 /* We only need to reread the data if it became invalid - or if we
324 * haven't read it yet.
326 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) &&
327 test_bit(AFS_VNODE_DIR_READ
, &dvnode
->flags
)) {
332 up_read(&dvnode
->validate_lock
);
333 if (down_write_killable(&dvnode
->validate_lock
) < 0)
336 if (!test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
))
337 afs_invalidate_cache(dvnode
, 0);
339 if (!test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) ||
340 !test_bit(AFS_VNODE_DIR_READ
, &dvnode
->flags
)) {
341 trace_afs_reload_dir(dvnode
);
342 ret
= afs_read_single(dvnode
, file
);
346 // TODO: Trim excess pages
348 set_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
);
349 set_bit(AFS_VNODE_DIR_READ
, &dvnode
->flags
);
354 downgrade_write(&dvnode
->validate_lock
);
359 up_write(&dvnode
->validate_lock
);
361 _leave(" = %zd", ret
);
366 * deal with one block in an AFS directory
368 static int afs_dir_iterate_block(struct afs_vnode
*dvnode
,
369 struct dir_context
*ctx
,
370 union afs_xdr_dir_block
*block
)
372 union afs_xdr_dirent
*dire
;
373 unsigned int blknum
, base
, hdr
, pos
, next
, nr_slots
;
377 blknum
= ctx
->pos
/ AFS_DIR_BLOCK_SIZE
;
378 base
= blknum
* AFS_DIR_SLOTS_PER_BLOCK
;
379 hdr
= (blknum
== 0 ? AFS_DIR_RESV_BLOCKS0
: AFS_DIR_RESV_BLOCKS
);
380 pos
= DIV_ROUND_UP(ctx
->pos
, AFS_DIR_DIRENT_SIZE
) - base
;
382 _enter("%llx,%x", ctx
->pos
, blknum
);
384 /* walk through the block, an entry at a time */
385 for (unsigned int slot
= hdr
; slot
< AFS_DIR_SLOTS_PER_BLOCK
; slot
= next
) {
386 /* skip entries marked unused in the bitmap */
387 if (!(block
->hdr
.bitmap
[slot
/ 8] &
388 (1 << (slot
% 8)))) {
389 _debug("ENT[%x]: Unused", base
+ slot
);
392 ctx
->pos
= (base
+ next
) * sizeof(union afs_xdr_dirent
);
396 /* got a valid entry */
397 dire
= &block
->dirents
[slot
];
398 nlen
= strnlen(dire
->u
.name
,
399 (unsigned long)(block
+ 1) - (unsigned long)dire
->u
.name
- 1);
400 if (nlen
> AFSNAMEMAX
- 1) {
401 _debug("ENT[%x]: Name too long (len %zx)",
403 return afs_bad(dvnode
, afs_file_error_dir_name_too_long
);
406 _debug("ENT[%x]: %s %zx \"%s\"",
407 base
+ slot
, (slot
< pos
? "skip" : "fill"),
410 nr_slots
= afs_dir_calc_slots(nlen
);
411 next
= slot
+ nr_slots
;
412 if (next
> AFS_DIR_SLOTS_PER_BLOCK
) {
413 _debug("ENT[%x]: extends beyond end dir block (len %zx)",
415 return afs_bad(dvnode
, afs_file_error_dir_over_end
);
418 /* Check that the name-extension dirents are all allocated */
419 for (tmp
= 1; tmp
< nr_slots
; tmp
++) {
420 unsigned int xslot
= slot
+ tmp
;
422 if (!(block
->hdr
.bitmap
[xslot
/ 8] & (1 << (xslot
% 8)))) {
423 _debug("ENT[%x]: Unmarked extension (%x/%x)",
424 base
+ slot
, tmp
, nr_slots
);
425 return afs_bad(dvnode
, afs_file_error_dir_unmarked_ext
);
429 /* skip if starts before the current position */
432 ctx
->pos
= (base
+ next
) * sizeof(union afs_xdr_dirent
);
436 /* found the next entry */
437 if (!dir_emit(ctx
, dire
->u
.name
, nlen
,
438 ntohl(dire
->u
.vnode
),
439 (ctx
->actor
== afs_lookup_filldir
||
440 ctx
->actor
== afs_lookup_one_filldir
)?
441 ntohl(dire
->u
.unique
) : DT_UNKNOWN
)) {
442 _leave(" = 0 [full]");
446 ctx
->pos
= (base
+ next
) * sizeof(union afs_xdr_dirent
);
449 _leave(" = 1 [more]");
453 struct afs_dir_iteration_ctx
{
454 struct dir_context
*dir_ctx
;
459 * Iterate through a kmapped directory segment.
461 static size_t afs_dir_iterate_step(void *iter_base
, size_t progress
, size_t len
,
462 void *priv
, void *priv2
)
464 struct afs_dir_iteration_ctx
*ctx
= priv2
;
465 struct afs_vnode
*dvnode
= priv
;
468 if (WARN_ON_ONCE(progress
% AFS_DIR_BLOCK_SIZE
||
469 len
% AFS_DIR_BLOCK_SIZE
)) {
470 pr_err("Mis-iteration prog=%zx len=%zx\n",
471 progress
% AFS_DIR_BLOCK_SIZE
,
472 len
% AFS_DIR_BLOCK_SIZE
);
477 ret
= afs_dir_iterate_block(dvnode
, ctx
->dir_ctx
, iter_base
);
481 ctx
->dir_ctx
->pos
= round_up(ctx
->dir_ctx
->pos
, AFS_DIR_BLOCK_SIZE
);
482 iter_base
+= AFS_DIR_BLOCK_SIZE
;
483 len
-= AFS_DIR_BLOCK_SIZE
;
490 * Iterate through the directory folios.
492 static int afs_dir_iterate_contents(struct inode
*dir
, struct dir_context
*dir_ctx
)
494 struct afs_dir_iteration_ctx ctx
= { .dir_ctx
= dir_ctx
};
495 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
496 struct iov_iter iter
;
497 unsigned long long i_size
= i_size_read(dir
);
499 /* Round the file position up to the next entry boundary */
500 dir_ctx
->pos
= round_up(dir_ctx
->pos
, sizeof(union afs_xdr_dirent
));
502 if (i_size
<= 0 || dir_ctx
->pos
>= i_size
)
505 iov_iter_folio_queue(&iter
, ITER_SOURCE
, dvnode
->directory
, 0, 0, i_size
);
506 iov_iter_advance(&iter
, round_down(dir_ctx
->pos
, AFS_DIR_BLOCK_SIZE
));
508 iterate_folioq(&iter
, iov_iter_count(&iter
), dvnode
, &ctx
,
509 afs_dir_iterate_step
);
511 if (ctx
.error
== -ESTALE
)
512 afs_invalidate_dir(dvnode
, afs_dir_invalid_iter_stale
);
517 * iterate through the data blob that lists the contents of an AFS directory
519 static int afs_dir_iterate(struct inode
*dir
, struct dir_context
*ctx
,
520 struct file
*file
, afs_dataversion_t
*_dir_version
)
522 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
523 int retry_limit
= 100;
526 _enter("{%lu},%llx,,", dir
->i_ino
, ctx
->pos
);
529 if (--retry_limit
< 0) {
530 pr_warn("afs_read_dir(): Too many retries\n");
534 ret
= afs_read_dir(dvnode
, file
);
538 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(dir
)->flags
)) {
544 *_dir_version
= inode_peek_iversion_raw(dir
);
546 ret
= afs_dir_iterate_contents(dir
, ctx
);
547 up_read(&dvnode
->validate_lock
);
548 } while (ret
== -ESTALE
);
550 _leave(" = %d", ret
);
555 * read an AFS directory
557 static int afs_readdir(struct file
*file
, struct dir_context
*ctx
)
559 afs_dataversion_t dir_version
;
561 return afs_dir_iterate(file_inode(file
), ctx
, file
, &dir_version
);
565 * Search the directory for a single name
566 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
567 * uniquifier through dtype
569 static bool afs_lookup_one_filldir(struct dir_context
*ctx
, const char *name
,
570 int nlen
, loff_t fpos
, u64 ino
, unsigned dtype
)
572 struct afs_lookup_one_cookie
*cookie
=
573 container_of(ctx
, struct afs_lookup_one_cookie
, ctx
);
575 _enter("{%s,%u},%s,%u,,%llu,%u",
576 cookie
->name
.name
, cookie
->name
.len
, name
, nlen
,
577 (unsigned long long) ino
, dtype
);
579 /* insanity checks first */
580 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block
) != 2048);
581 BUILD_BUG_ON(sizeof(union afs_xdr_dirent
) != 32);
583 if (cookie
->name
.len
!= nlen
||
584 memcmp(cookie
->name
.name
, name
, nlen
) != 0) {
585 _leave(" = true [keep looking]");
589 cookie
->fid
.vnode
= ino
;
590 cookie
->fid
.unique
= dtype
;
593 _leave(" = false [found]");
598 * Do a lookup of a single name in a directory
599 * - just returns the FID the dentry name maps to if found
601 static int afs_do_lookup_one(struct inode
*dir
, const struct qstr
*name
,
603 afs_dataversion_t
*_dir_version
)
605 struct afs_super_info
*as
= dir
->i_sb
->s_fs_info
;
606 struct afs_lookup_one_cookie cookie
= {
607 .ctx
.actor
= afs_lookup_one_filldir
,
609 .fid
.vid
= as
->volume
->vid
613 _enter("{%lu},{%.*s},", dir
->i_ino
, name
->len
, name
->name
);
615 /* search the directory */
616 ret
= afs_dir_iterate(dir
, &cookie
.ctx
, NULL
, _dir_version
);
618 _leave(" = %d [iter]", ret
);
623 _leave(" = -ENOENT [not found]");
628 _leave(" = 0 { vn=%llu u=%u }", fid
->vnode
, fid
->unique
);
633 * search the directory for a name
634 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
635 * uniquifier through dtype
637 static bool afs_lookup_filldir(struct dir_context
*ctx
, const char *name
,
638 int nlen
, loff_t fpos
, u64 ino
, unsigned dtype
)
640 struct afs_lookup_cookie
*cookie
=
641 container_of(ctx
, struct afs_lookup_cookie
, ctx
);
643 _enter("{%s,%u},%s,%u,,%llu,%u",
644 cookie
->name
.name
, cookie
->name
.len
, name
, nlen
,
645 (unsigned long long) ino
, dtype
);
647 /* insanity checks first */
648 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block
) != 2048);
649 BUILD_BUG_ON(sizeof(union afs_xdr_dirent
) != 32);
651 if (cookie
->nr_fids
< 50) {
652 cookie
->fids
[cookie
->nr_fids
].vnode
= ino
;
653 cookie
->fids
[cookie
->nr_fids
].unique
= dtype
;
657 return cookie
->nr_fids
< 50;
661 * Deal with the result of a successful lookup operation. Turn all the files
662 * into inodes and save the first one - which is the one we actually want.
664 static void afs_do_lookup_success(struct afs_operation
*op
)
666 struct afs_vnode_param
*vp
;
667 struct afs_vnode
*vnode
;
674 for (i
= 0; i
< op
->nr_files
; i
++) {
678 abort_code
= vp
->scb
.status
.abort_code
;
679 if (abort_code
!= 0) {
680 op
->call_abort_code
= abort_code
;
681 afs_op_set_error(op
, afs_abort_to_error(abort_code
));
682 op
->cumul_error
.abort_code
= abort_code
;
691 vp
= &op
->more_files
[i
- 2];
695 if (vp
->scb
.status
.abort_code
)
696 trace_afs_bulkstat_error(op
, &vp
->fid
, i
, vp
->scb
.status
.abort_code
);
697 if (!vp
->scb
.have_status
&& !vp
->scb
.have_error
)
700 _debug("do [%u]", i
);
702 if (!test_bit(AFS_VNODE_UNSET
, &vp
->vnode
->flags
))
703 afs_vnode_commit_status(op
, vp
);
704 } else if (vp
->scb
.status
.abort_code
== 0) {
705 inode
= afs_iget(op
, vp
);
706 if (!IS_ERR(inode
)) {
707 vnode
= AFS_FS_I(inode
);
708 afs_cache_permit(vnode
, op
->key
,
709 0 /* Assume vnode->cb_break is 0 */ +
713 vp
->put_vnode
= true;
716 _debug("- abort %d %llx:%llx.%x",
717 vp
->scb
.status
.abort_code
,
718 vp
->fid
.vid
, vp
->fid
.vnode
, vp
->fid
.unique
);
725 static const struct afs_operation_ops afs_inline_bulk_status_operation
= {
726 .issue_afs_rpc
= afs_fs_inline_bulk_status
,
727 .issue_yfs_rpc
= yfs_fs_inline_bulk_status
,
728 .success
= afs_do_lookup_success
,
731 static const struct afs_operation_ops afs_lookup_fetch_status_operation
= {
732 .issue_afs_rpc
= afs_fs_fetch_status
,
733 .issue_yfs_rpc
= yfs_fs_fetch_status
,
734 .success
= afs_do_lookup_success
,
735 .aborted
= afs_check_for_remote_deletion
,
739 * See if we know that the server we expect to use doesn't support
740 * FS.InlineBulkStatus.
742 static bool afs_server_supports_ibulk(struct afs_vnode
*dvnode
)
744 struct afs_server_list
*slist
;
745 struct afs_volume
*volume
= dvnode
->volume
;
746 struct afs_server
*server
;
750 if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK
, &volume
->flags
))
754 slist
= rcu_dereference(volume
->servers
);
756 for (i
= 0; i
< slist
->nr_servers
; i
++) {
757 server
= slist
->servers
[i
].server
;
758 if (server
== dvnode
->cb_server
) {
759 if (test_bit(AFS_SERVER_FL_NO_IBULK
, &server
->flags
))
770 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
771 * files in one go and create inodes for them. The inode of the file we were
772 * asked for is returned.
774 static struct inode
*afs_do_lookup(struct inode
*dir
, struct dentry
*dentry
)
776 struct afs_lookup_cookie
*cookie
;
777 struct afs_vnode_param
*vp
;
778 struct afs_operation
*op
;
779 struct afs_vnode
*dvnode
= AFS_FS_I(dir
), *vnode
;
780 struct inode
*inode
= NULL
, *ti
;
781 afs_dataversion_t data_version
= READ_ONCE(dvnode
->status
.data_version
);
786 _enter("{%lu},%p{%pd},", dir
->i_ino
, dentry
, dentry
);
788 cookie
= kzalloc(sizeof(struct afs_lookup_cookie
), GFP_KERNEL
);
790 return ERR_PTR(-ENOMEM
);
792 for (i
= 0; i
< ARRAY_SIZE(cookie
->fids
); i
++)
793 cookie
->fids
[i
].vid
= dvnode
->fid
.vid
;
794 cookie
->ctx
.actor
= afs_lookup_filldir
;
795 cookie
->name
= dentry
->d_name
;
796 cookie
->nr_fids
= 2; /* slot 1 is saved for the fid we actually want
797 * and slot 0 for the directory */
799 /* Search the directory for the named entry using the hash table... */
800 ret
= afs_dir_search(dvnode
, &dentry
->d_name
, &cookie
->fids
[1], &data_version
);
804 supports_ibulk
= afs_server_supports_ibulk(dvnode
);
805 if (supports_ibulk
) {
806 /* ...then scan linearly from that point for entries to lookup-ahead. */
807 cookie
->ctx
.pos
= (ret
+ 1) * AFS_DIR_DIRENT_SIZE
;
808 afs_dir_iterate(dir
, &cookie
->ctx
, NULL
, &data_version
);
811 dentry
->d_fsdata
= (void *)(unsigned long)data_version
;
813 /* Check to see if we already have an inode for the primary fid. */
814 inode
= ilookup5(dir
->i_sb
, cookie
->fids
[1].vnode
,
815 afs_ilookup5_test_by_fid
, &cookie
->fids
[1]);
817 goto out
; /* We do */
819 /* Okay, we didn't find it. We need to query the server - and whilst
820 * we're doing that, we're going to attempt to look up a bunch of other
823 op
= afs_alloc_operation(NULL
, dvnode
->volume
);
829 afs_op_set_vnode(op
, 0, dvnode
);
830 afs_op_set_fid(op
, 1, &cookie
->fids
[1]);
832 op
->nr_files
= cookie
->nr_fids
;
833 _debug("nr_files %u", op
->nr_files
);
835 /* Need space for examining all the selected files */
836 if (op
->nr_files
> 2) {
837 op
->more_files
= kvcalloc(op
->nr_files
- 2,
838 sizeof(struct afs_vnode_param
),
840 if (!op
->more_files
) {
845 for (i
= 2; i
< op
->nr_files
; i
++) {
846 vp
= &op
->more_files
[i
- 2];
847 vp
->fid
= cookie
->fids
[i
];
849 /* Find any inodes that already exist and get their
852 ti
= ilookup5_nowait(dir
->i_sb
, vp
->fid
.vnode
,
853 afs_ilookup5_test_by_fid
, &vp
->fid
);
854 if (!IS_ERR_OR_NULL(ti
)) {
855 vnode
= AFS_FS_I(ti
);
856 vp
->dv_before
= vnode
->status
.data_version
;
857 vp
->cb_break_before
= afs_calc_vnode_cb_break(vnode
);
859 vp
->put_vnode
= true;
860 vp
->speculative
= true; /* vnode not locked */
865 /* Try FS.InlineBulkStatus first. Abort codes for the individual
866 * lookups contained therein are stored in the reply without aborting
867 * the whole operation.
869 afs_op_set_error(op
, -ENOTSUPP
);
870 if (supports_ibulk
) {
871 op
->ops
= &afs_inline_bulk_status_operation
;
872 afs_begin_vnode_operation(op
);
873 afs_wait_for_operation(op
);
876 if (afs_op_error(op
) == -ENOTSUPP
) {
877 /* We could try FS.BulkStatus next, but this aborts the entire
878 * op if any of the lookups fails - so, for the moment, revert
879 * to FS.FetchStatus for op->file[1].
881 op
->fetch_status
.which
= 1;
882 op
->ops
= &afs_lookup_fetch_status_operation
;
883 afs_begin_vnode_operation(op
);
884 afs_wait_for_operation(op
);
888 if (!afs_op_error(op
)) {
889 if (op
->file
[1].scb
.status
.abort_code
) {
890 afs_op_accumulate_error(op
, -ECONNABORTED
,
891 op
->file
[1].scb
.status
.abort_code
);
893 inode
= &op
->file
[1].vnode
->netfs
.inode
;
894 op
->file
[1].vnode
= NULL
;
898 if (op
->file
[0].scb
.have_status
)
899 dentry
->d_fsdata
= (void *)(unsigned long)op
->file
[0].scb
.status
.data_version
;
901 dentry
->d_fsdata
= (void *)(unsigned long)op
->file
[0].dv_before
;
902 ret
= afs_put_operation(op
);
906 return inode
?: ERR_PTR(ret
);
910 * Look up an entry in a directory with @sys substitution.
912 static struct dentry
*afs_lookup_atsys(struct inode
*dir
, struct dentry
*dentry
)
914 struct afs_sysnames
*subs
;
915 struct afs_net
*net
= afs_i2net(dir
);
917 char *buf
, *p
, *name
;
922 ret
= ERR_PTR(-ENOMEM
);
923 p
= buf
= kmalloc(AFSNAMEMAX
, GFP_KERNEL
);
926 if (dentry
->d_name
.len
> 4) {
927 memcpy(p
, dentry
->d_name
.name
, dentry
->d_name
.len
- 4);
928 p
+= dentry
->d_name
.len
- 4;
931 /* There is an ordered list of substitutes that we have to try. */
932 read_lock(&net
->sysnames_lock
);
933 subs
= net
->sysnames
;
934 refcount_inc(&subs
->usage
);
935 read_unlock(&net
->sysnames_lock
);
937 for (i
= 0; i
< subs
->nr
; i
++) {
938 name
= subs
->subs
[i
];
939 len
= dentry
->d_name
.len
- 4 + strlen(name
);
940 if (len
>= AFSNAMEMAX
) {
941 ret
= ERR_PTR(-ENAMETOOLONG
);
946 ret
= lookup_one_len(buf
, dentry
->d_parent
, len
);
947 if (IS_ERR(ret
) || d_is_positive(ret
))
952 /* We don't want to d_add() the @sys dentry here as we don't want to
953 * the cached dentry to hide changes to the sysnames list.
957 afs_put_sysnames(subs
);
964 * look up an entry in a directory
966 static struct dentry
*afs_lookup(struct inode
*dir
, struct dentry
*dentry
,
969 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
970 struct afs_fid fid
= {};
975 _enter("{%llx:%llu},%p{%pd},",
976 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
, dentry
);
978 ASSERTCMP(d_inode(dentry
), ==, NULL
);
980 if (dentry
->d_name
.len
>= AFSNAMEMAX
) {
981 _leave(" = -ENAMETOOLONG");
982 return ERR_PTR(-ENAMETOOLONG
);
985 if (test_bit(AFS_VNODE_DELETED
, &dvnode
->flags
)) {
986 _leave(" = -ESTALE");
987 return ERR_PTR(-ESTALE
);
990 ret
= afs_validate(dvnode
, NULL
);
992 afs_dir_unuse_cookie(dvnode
, ret
);
993 _leave(" = %d [val]", ret
);
997 if (dentry
->d_name
.len
>= 4 &&
998 dentry
->d_name
.name
[dentry
->d_name
.len
- 4] == '@' &&
999 dentry
->d_name
.name
[dentry
->d_name
.len
- 3] == 's' &&
1000 dentry
->d_name
.name
[dentry
->d_name
.len
- 2] == 'y' &&
1001 dentry
->d_name
.name
[dentry
->d_name
.len
- 1] == 's')
1002 return afs_lookup_atsys(dir
, dentry
);
1004 afs_stat_v(dvnode
, n_lookup
);
1005 inode
= afs_do_lookup(dir
, dentry
);
1006 if (inode
== ERR_PTR(-ENOENT
))
1007 inode
= afs_try_auto_mntpt(dentry
, dir
);
1009 if (!IS_ERR_OR_NULL(inode
))
1010 fid
= AFS_FS_I(inode
)->fid
;
1012 _debug("splice %p", dentry
->d_inode
);
1013 d
= d_splice_alias(inode
, dentry
);
1014 if (!IS_ERR_OR_NULL(d
)) {
1015 d
->d_fsdata
= dentry
->d_fsdata
;
1016 trace_afs_lookup(dvnode
, &d
->d_name
, &fid
);
1018 trace_afs_lookup(dvnode
, &dentry
->d_name
, &fid
);
1025 * Check the validity of a dentry under RCU conditions.
1027 static int afs_d_revalidate_rcu(struct afs_vnode
*dvnode
, struct dentry
*dentry
)
1029 long dir_version
, de_version
;
1031 _enter("%p", dentry
);
1033 if (test_bit(AFS_VNODE_DELETED
, &dvnode
->flags
))
1036 if (!afs_check_validity(dvnode
))
1039 /* We only need to invalidate a dentry if the server's copy changed
1040 * behind our back. If we made the change, it's no problem. Note that
1041 * on a 32-bit system, we only have 32 bits in the dentry to store the
1044 dir_version
= (long)READ_ONCE(dvnode
->status
.data_version
);
1045 de_version
= (long)READ_ONCE(dentry
->d_fsdata
);
1046 if (de_version
!= dir_version
) {
1047 dir_version
= (long)READ_ONCE(dvnode
->invalid_before
);
1048 if (de_version
- dir_version
< 0)
1052 return 1; /* Still valid */
1056 * check that a dentry lookup hit has found a valid entry
1057 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
1060 static int afs_d_revalidate(struct inode
*parent_dir
, const struct qstr
*name
,
1061 struct dentry
*dentry
, unsigned int flags
)
1063 struct afs_vnode
*vnode
, *dir
= AFS_FS_I(parent_dir
);
1065 struct inode
*inode
;
1067 afs_dataversion_t dir_version
, invalid_before
;
1071 if (flags
& LOOKUP_RCU
)
1072 return afs_d_revalidate_rcu(dir
, dentry
);
1074 if (d_really_is_positive(dentry
)) {
1075 vnode
= AFS_FS_I(d_inode(dentry
));
1076 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
1077 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
,
1080 _enter("{neg n=%pd}", dentry
);
1083 key
= afs_request_key(AFS_FS_S(dentry
->d_sb
)->volume
->cell
);
1087 /* validate the parent directory */
1088 ret
= afs_validate(dir
, key
);
1089 if (ret
== -ERESTARTSYS
) {
1094 if (test_bit(AFS_VNODE_DELETED
, &dir
->flags
)) {
1095 _debug("%pd: parent dir deleted", dentry
);
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
1104 dir_version
= dir
->status
.data_version
;
1105 de_version
= (long)dentry
->d_fsdata
;
1106 if (de_version
== (long)dir_version
)
1107 goto out_valid_noupdate
;
1109 invalid_before
= dir
->invalid_before
;
1110 if (de_version
- (long)invalid_before
>= 0)
1113 _debug("dir modified");
1114 afs_stat_v(dir
, n_reval
);
1116 /* search the directory for this vnode */
1117 ret
= afs_do_lookup_one(&dir
->netfs
.inode
, name
, &fid
, &dir_version
);
1120 /* the filename maps to something */
1121 if (d_really_is_negative(dentry
))
1123 inode
= d_inode(dentry
);
1124 if (is_bad_inode(inode
)) {
1125 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1130 vnode
= AFS_FS_I(inode
);
1132 /* if the vnode ID has changed, then the dirent points to a
1134 if (fid
.vnode
!= vnode
->fid
.vnode
) {
1135 _debug("%pd: dirent changed [%llu != %llu]",
1141 /* if the vnode ID uniqifier has changed, then the file has
1142 * been deleted and replaced, and the original vnode ID has
1144 if (fid
.unique
!= vnode
->fid
.unique
) {
1145 _debug("%pd: file deleted (uq %u -> %u I:%u)",
1148 vnode
->netfs
.inode
.i_generation
);
1154 /* the filename is unknown */
1155 _debug("%pd: dirent not found", dentry
);
1156 if (d_really_is_positive(dentry
))
1161 _debug("failed to iterate parent %pd2: %d", dentry
, ret
);
1166 dentry
->d_fsdata
= (void *)(unsigned long)dir_version
;
1169 _leave(" = 1 [valid]");
1173 _debug("dropping dentry %pd2", dentry
);
1176 _leave(" = 0 [bad]");
1181 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1183 * - called from dput() when d_count is going to 0.
1184 * - return 1 to request dentry be unhashed, 0 otherwise
1186 static int afs_d_delete(const struct dentry
*dentry
)
1188 _enter("%pd", dentry
);
1190 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
1193 if (d_really_is_positive(dentry
) &&
1194 (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(d_inode(dentry
))->flags
) ||
1195 test_bit(AFS_VNODE_PSEUDODIR
, &AFS_FS_I(d_inode(dentry
))->flags
)))
1198 _leave(" = 0 [keep]");
1202 _leave(" = 1 [zap]");
1207 * Clean up sillyrename files on dentry removal.
1209 static void afs_d_iput(struct dentry
*dentry
, struct inode
*inode
)
1211 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
1212 afs_silly_iput(dentry
, inode
);
1217 * handle dentry release
1219 void afs_d_release(struct dentry
*dentry
)
1221 _enter("%pd", dentry
);
1224 void afs_check_for_remote_deletion(struct afs_operation
*op
)
1226 struct afs_vnode
*vnode
= op
->file
[0].vnode
;
1228 switch (afs_op_abort_code(op
)) {
1230 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
1231 clear_nlink(&vnode
->netfs
.inode
);
1232 afs_break_callback(vnode
, afs_cb_break_for_deleted
);
1237 * Create a new inode for create/mkdir/symlink
1239 static void afs_vnode_new_inode(struct afs_operation
*op
)
1241 struct afs_vnode_param
*dvp
= &op
->file
[0];
1242 struct afs_vnode_param
*vp
= &op
->file
[1];
1243 struct afs_vnode
*vnode
;
1244 struct inode
*inode
;
1248 ASSERTCMP(afs_op_error(op
), ==, 0);
1250 inode
= afs_iget(op
, vp
);
1251 if (IS_ERR(inode
)) {
1252 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1253 * the new directory on the server.
1255 afs_op_accumulate_error(op
, PTR_ERR(inode
), 0);
1259 vnode
= AFS_FS_I(inode
);
1260 set_bit(AFS_VNODE_NEW_CONTENT
, &vnode
->flags
);
1261 if (S_ISDIR(inode
->i_mode
))
1262 afs_mkdir_init_dir(vnode
, dvp
->vnode
);
1263 else if (S_ISLNK(inode
->i_mode
))
1264 afs_init_new_symlink(vnode
, op
);
1265 if (!afs_op_error(op
))
1266 afs_cache_permit(vnode
, op
->key
, vnode
->cb_break
, &vp
->scb
);
1267 d_instantiate(op
->dentry
, inode
);
1270 static void afs_create_success(struct afs_operation
*op
)
1272 _enter("op=%08x", op
->debug_id
);
1273 op
->ctime
= op
->file
[0].scb
.status
.mtime_client
;
1274 afs_vnode_commit_status(op
, &op
->file
[0]);
1275 afs_update_dentry_version(op
, &op
->file
[0], op
->dentry
);
1276 afs_vnode_new_inode(op
);
1279 static void afs_create_edit_dir(struct afs_operation
*op
)
1281 struct netfs_cache_resources cres
= {};
1282 struct afs_vnode_param
*dvp
= &op
->file
[0];
1283 struct afs_vnode_param
*vp
= &op
->file
[1];
1284 struct afs_vnode
*dvnode
= dvp
->vnode
;
1286 _enter("op=%08x", op
->debug_id
);
1288 fscache_begin_write_operation(&cres
, afs_vnode_cache(dvnode
));
1289 down_write(&dvnode
->validate_lock
);
1290 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) &&
1291 dvnode
->status
.data_version
== dvp
->dv_before
+ dvp
->dv_delta
)
1292 afs_edit_dir_add(dvnode
, &op
->dentry
->d_name
, &vp
->fid
,
1294 up_write(&dvnode
->validate_lock
);
1295 fscache_end_operation(&cres
);
1298 static void afs_create_put(struct afs_operation
*op
)
1300 _enter("op=%08x", op
->debug_id
);
1302 if (afs_op_error(op
))
1306 static const struct afs_operation_ops afs_mkdir_operation
= {
1307 .issue_afs_rpc
= afs_fs_make_dir
,
1308 .issue_yfs_rpc
= yfs_fs_make_dir
,
1309 .success
= afs_create_success
,
1310 .aborted
= afs_check_for_remote_deletion
,
1311 .edit_dir
= afs_create_edit_dir
,
1312 .put
= afs_create_put
,
1316 * create a directory on an AFS filesystem
1318 static int afs_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
1319 struct dentry
*dentry
, umode_t mode
)
1321 struct afs_operation
*op
;
1322 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1325 _enter("{%llx:%llu},{%pd},%ho",
1326 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
, mode
);
1328 op
= afs_alloc_operation(NULL
, dvnode
->volume
);
1334 fscache_use_cookie(afs_vnode_cache(dvnode
), true);
1336 afs_op_set_vnode(op
, 0, dvnode
);
1337 op
->file
[0].dv_delta
= 1;
1338 op
->file
[0].modification
= true;
1339 op
->file
[0].update_ctime
= true;
1340 op
->dentry
= dentry
;
1341 op
->create
.mode
= S_IFDIR
| mode
;
1342 op
->create
.reason
= afs_edit_dir_for_mkdir
;
1343 op
->mtime
= current_time(dir
);
1344 op
->ops
= &afs_mkdir_operation
;
1345 ret
= afs_do_sync_operation(op
);
1346 afs_dir_unuse_cookie(dvnode
, ret
);
1351 * Remove a subdir from a directory.
1353 static void afs_dir_remove_subdir(struct dentry
*dentry
)
1355 if (d_really_is_positive(dentry
)) {
1356 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
1358 clear_nlink(&vnode
->netfs
.inode
);
1359 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
1360 afs_clear_cb_promise(vnode
, afs_cb_promise_clear_rmdir
);
1361 afs_invalidate_dir(vnode
, afs_dir_invalid_subdir_removed
);
1365 static void afs_rmdir_success(struct afs_operation
*op
)
1367 _enter("op=%08x", op
->debug_id
);
1368 op
->ctime
= op
->file
[0].scb
.status
.mtime_client
;
1369 afs_vnode_commit_status(op
, &op
->file
[0]);
1370 afs_update_dentry_version(op
, &op
->file
[0], op
->dentry
);
1373 static void afs_rmdir_edit_dir(struct afs_operation
*op
)
1375 struct netfs_cache_resources cres
= {};
1376 struct afs_vnode_param
*dvp
= &op
->file
[0];
1377 struct afs_vnode
*dvnode
= dvp
->vnode
;
1379 _enter("op=%08x", op
->debug_id
);
1380 afs_dir_remove_subdir(op
->dentry
);
1382 fscache_begin_write_operation(&cres
, afs_vnode_cache(dvnode
));
1383 down_write(&dvnode
->validate_lock
);
1384 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) &&
1385 dvnode
->status
.data_version
== dvp
->dv_before
+ dvp
->dv_delta
)
1386 afs_edit_dir_remove(dvnode
, &op
->dentry
->d_name
,
1387 afs_edit_dir_for_rmdir
);
1388 up_write(&dvnode
->validate_lock
);
1389 fscache_end_operation(&cres
);
1392 static void afs_rmdir_put(struct afs_operation
*op
)
1394 _enter("op=%08x", op
->debug_id
);
1395 if (op
->file
[1].vnode
)
1396 up_write(&op
->file
[1].vnode
->rmdir_lock
);
1399 static const struct afs_operation_ops afs_rmdir_operation
= {
1400 .issue_afs_rpc
= afs_fs_remove_dir
,
1401 .issue_yfs_rpc
= yfs_fs_remove_dir
,
1402 .success
= afs_rmdir_success
,
1403 .aborted
= afs_check_for_remote_deletion
,
1404 .edit_dir
= afs_rmdir_edit_dir
,
1405 .put
= afs_rmdir_put
,
1409 * remove a directory from an AFS filesystem
1411 static int afs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1413 struct afs_operation
*op
;
1414 struct afs_vnode
*dvnode
= AFS_FS_I(dir
), *vnode
= NULL
;
1417 _enter("{%llx:%llu},{%pd}",
1418 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
);
1420 op
= afs_alloc_operation(NULL
, dvnode
->volume
);
1424 fscache_use_cookie(afs_vnode_cache(dvnode
), true);
1426 afs_op_set_vnode(op
, 0, dvnode
);
1427 op
->file
[0].dv_delta
= 1;
1428 op
->file
[0].modification
= true;
1429 op
->file
[0].update_ctime
= true;
1431 op
->dentry
= dentry
;
1432 op
->ops
= &afs_rmdir_operation
;
1434 /* Try to make sure we have a callback promise on the victim. */
1435 if (d_really_is_positive(dentry
)) {
1436 vnode
= AFS_FS_I(d_inode(dentry
));
1437 ret
= afs_validate(vnode
, op
->key
);
1443 ret
= down_write_killable(&vnode
->rmdir_lock
);
1446 op
->file
[1].vnode
= vnode
;
1449 ret
= afs_do_sync_operation(op
);
1451 /* Not all systems that can host afs servers have ENOTEMPTY. */
1455 afs_dir_unuse_cookie(dvnode
, ret
);
1459 ret
= afs_put_operation(op
);
1464 * Remove a link to a file or symlink from a directory.
1466 * If the file was not deleted due to excess hard links, the fileserver will
1467 * break the callback promise on the file - if it had one - before it returns
1468 * to us, and if it was deleted, it won't
1470 * However, if we didn't have a callback promise outstanding, or it was
1471 * outstanding on a different server, then it won't break it either...
1473 static void afs_dir_remove_link(struct afs_operation
*op
)
1475 struct afs_vnode
*dvnode
= op
->file
[0].vnode
;
1476 struct afs_vnode
*vnode
= op
->file
[1].vnode
;
1477 struct dentry
*dentry
= op
->dentry
;
1480 if (afs_op_error(op
) ||
1481 (op
->file
[1].scb
.have_status
&& op
->file
[1].scb
.have_error
))
1483 if (d_really_is_positive(dentry
))
1486 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
1488 } else if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
)) {
1489 write_seqlock(&vnode
->cb_lock
);
1490 drop_nlink(&vnode
->netfs
.inode
);
1491 if (vnode
->netfs
.inode
.i_nlink
== 0) {
1492 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
1493 __afs_break_callback(vnode
, afs_cb_break_for_unlink
);
1495 write_sequnlock(&vnode
->cb_lock
);
1497 afs_break_callback(vnode
, afs_cb_break_for_unlink
);
1499 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
1500 _debug("AFS_VNODE_DELETED");
1502 ret
= afs_validate(vnode
, op
->key
);
1504 afs_op_set_error(op
, ret
);
1507 _debug("nlink %d [val %d]", vnode
->netfs
.inode
.i_nlink
, afs_op_error(op
));
1510 static void afs_unlink_success(struct afs_operation
*op
)
1512 _enter("op=%08x", op
->debug_id
);
1513 op
->ctime
= op
->file
[0].scb
.status
.mtime_client
;
1514 afs_check_dir_conflict(op
, &op
->file
[0]);
1515 afs_vnode_commit_status(op
, &op
->file
[0]);
1516 afs_vnode_commit_status(op
, &op
->file
[1]);
1517 afs_update_dentry_version(op
, &op
->file
[0], op
->dentry
);
1518 afs_dir_remove_link(op
);
1521 static void afs_unlink_edit_dir(struct afs_operation
*op
)
1523 struct netfs_cache_resources cres
= {};
1524 struct afs_vnode_param
*dvp
= &op
->file
[0];
1525 struct afs_vnode
*dvnode
= dvp
->vnode
;
1527 _enter("op=%08x", op
->debug_id
);
1528 fscache_begin_write_operation(&cres
, afs_vnode_cache(dvnode
));
1529 down_write(&dvnode
->validate_lock
);
1530 if (test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) &&
1531 dvnode
->status
.data_version
== dvp
->dv_before
+ dvp
->dv_delta
)
1532 afs_edit_dir_remove(dvnode
, &op
->dentry
->d_name
,
1533 afs_edit_dir_for_unlink
);
1534 up_write(&dvnode
->validate_lock
);
1535 fscache_end_operation(&cres
);
1538 static void afs_unlink_put(struct afs_operation
*op
)
1540 _enter("op=%08x", op
->debug_id
);
1541 if (op
->unlink
.need_rehash
&& afs_op_error(op
) < 0 && afs_op_error(op
) != -ENOENT
)
1542 d_rehash(op
->dentry
);
1545 static const struct afs_operation_ops afs_unlink_operation
= {
1546 .issue_afs_rpc
= afs_fs_remove_file
,
1547 .issue_yfs_rpc
= yfs_fs_remove_file
,
1548 .success
= afs_unlink_success
,
1549 .aborted
= afs_check_for_remote_deletion
,
1550 .edit_dir
= afs_unlink_edit_dir
,
1551 .put
= afs_unlink_put
,
1555 * Remove a file or symlink from an AFS filesystem.
1557 static int afs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1559 struct afs_operation
*op
;
1560 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1561 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
1564 _enter("{%llx:%llu},{%pd}",
1565 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
);
1567 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1568 return -ENAMETOOLONG
;
1570 op
= afs_alloc_operation(NULL
, dvnode
->volume
);
1574 fscache_use_cookie(afs_vnode_cache(dvnode
), true);
1576 afs_op_set_vnode(op
, 0, dvnode
);
1577 op
->file
[0].dv_delta
= 1;
1578 op
->file
[0].modification
= true;
1579 op
->file
[0].update_ctime
= true;
1581 /* Try to make sure we have a callback promise on the victim. */
1582 ret
= afs_validate(vnode
, op
->key
);
1584 afs_op_set_error(op
, ret
);
1588 spin_lock(&dentry
->d_lock
);
1589 if (d_count(dentry
) > 1) {
1590 spin_unlock(&dentry
->d_lock
);
1591 /* Start asynchronous writeout of the inode */
1592 write_inode_now(d_inode(dentry
), 0);
1593 afs_op_set_error(op
, afs_sillyrename(dvnode
, vnode
, dentry
, op
->key
));
1596 if (!d_unhashed(dentry
)) {
1597 /* Prevent a race with RCU lookup. */
1599 op
->unlink
.need_rehash
= true;
1601 spin_unlock(&dentry
->d_lock
);
1603 op
->file
[1].vnode
= vnode
;
1604 op
->file
[1].update_ctime
= true;
1605 op
->file
[1].op_unlinked
= true;
1606 op
->dentry
= dentry
;
1607 op
->ops
= &afs_unlink_operation
;
1608 afs_begin_vnode_operation(op
);
1609 afs_wait_for_operation(op
);
1611 /* If there was a conflict with a third party, check the status of the
1614 if (afs_op_error(op
) == 0 && (op
->flags
& AFS_OPERATION_DIR_CONFLICT
)) {
1615 op
->file
[1].update_ctime
= false;
1616 op
->fetch_status
.which
= 1;
1617 op
->ops
= &afs_fetch_status_operation
;
1618 afs_begin_vnode_operation(op
);
1619 afs_wait_for_operation(op
);
1623 ret
= afs_put_operation(op
);
1624 afs_dir_unuse_cookie(dvnode
, ret
);
1628 static const struct afs_operation_ops afs_create_operation
= {
1629 .issue_afs_rpc
= afs_fs_create_file
,
1630 .issue_yfs_rpc
= yfs_fs_create_file
,
1631 .success
= afs_create_success
,
1632 .aborted
= afs_check_for_remote_deletion
,
1633 .edit_dir
= afs_create_edit_dir
,
1634 .put
= afs_create_put
,
1638 * create a regular file on an AFS filesystem
1640 static int afs_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
1641 struct dentry
*dentry
, umode_t mode
, bool excl
)
1643 struct afs_operation
*op
;
1644 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1645 int ret
= -ENAMETOOLONG
;
1647 _enter("{%llx:%llu},{%pd},%ho",
1648 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
, mode
);
1650 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1653 op
= afs_alloc_operation(NULL
, dvnode
->volume
);
1659 fscache_use_cookie(afs_vnode_cache(dvnode
), true);
1661 afs_op_set_vnode(op
, 0, dvnode
);
1662 op
->file
[0].dv_delta
= 1;
1663 op
->file
[0].modification
= true;
1664 op
->file
[0].update_ctime
= true;
1666 op
->dentry
= dentry
;
1667 op
->create
.mode
= S_IFREG
| mode
;
1668 op
->create
.reason
= afs_edit_dir_for_create
;
1669 op
->mtime
= current_time(dir
);
1670 op
->ops
= &afs_create_operation
;
1671 ret
= afs_do_sync_operation(op
);
1672 afs_dir_unuse_cookie(dvnode
, ret
);
1677 _leave(" = %d", ret
);
1681 static void afs_link_success(struct afs_operation
*op
)
1683 struct afs_vnode_param
*dvp
= &op
->file
[0];
1684 struct afs_vnode_param
*vp
= &op
->file
[1];
1686 _enter("op=%08x", op
->debug_id
);
1687 op
->ctime
= dvp
->scb
.status
.mtime_client
;
1688 afs_vnode_commit_status(op
, dvp
);
1689 afs_vnode_commit_status(op
, vp
);
1690 afs_update_dentry_version(op
, dvp
, op
->dentry
);
1691 if (op
->dentry_2
->d_parent
== op
->dentry
->d_parent
)
1692 afs_update_dentry_version(op
, dvp
, op
->dentry_2
);
1693 ihold(&vp
->vnode
->netfs
.inode
);
1694 d_instantiate(op
->dentry
, &vp
->vnode
->netfs
.inode
);
1697 static void afs_link_put(struct afs_operation
*op
)
1699 _enter("op=%08x", op
->debug_id
);
1700 if (afs_op_error(op
))
1704 static const struct afs_operation_ops afs_link_operation
= {
1705 .issue_afs_rpc
= afs_fs_link
,
1706 .issue_yfs_rpc
= yfs_fs_link
,
1707 .success
= afs_link_success
,
1708 .aborted
= afs_check_for_remote_deletion
,
1709 .edit_dir
= afs_create_edit_dir
,
1710 .put
= afs_link_put
,
1714 * create a hard link between files in an AFS filesystem
1716 static int afs_link(struct dentry
*from
, struct inode
*dir
,
1717 struct dentry
*dentry
)
1719 struct afs_operation
*op
;
1720 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1721 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(from
));
1722 int ret
= -ENAMETOOLONG
;
1724 _enter("{%llx:%llu},{%llx:%llu},{%pd}",
1725 vnode
->fid
.vid
, vnode
->fid
.vnode
,
1726 dvnode
->fid
.vid
, dvnode
->fid
.vnode
,
1729 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1732 op
= afs_alloc_operation(NULL
, dvnode
->volume
);
1738 fscache_use_cookie(afs_vnode_cache(dvnode
), true);
1740 ret
= afs_validate(vnode
, op
->key
);
1744 afs_op_set_vnode(op
, 0, dvnode
);
1745 afs_op_set_vnode(op
, 1, vnode
);
1746 op
->file
[0].dv_delta
= 1;
1747 op
->file
[0].modification
= true;
1748 op
->file
[0].update_ctime
= true;
1749 op
->file
[1].update_ctime
= true;
1751 op
->dentry
= dentry
;
1752 op
->dentry_2
= from
;
1753 op
->ops
= &afs_link_operation
;
1754 op
->create
.reason
= afs_edit_dir_for_link
;
1755 ret
= afs_do_sync_operation(op
);
1756 afs_dir_unuse_cookie(dvnode
, ret
);
1760 afs_put_operation(op
);
1761 afs_dir_unuse_cookie(dvnode
, ret
);
1764 _leave(" = %d", ret
);
1768 static const struct afs_operation_ops afs_symlink_operation
= {
1769 .issue_afs_rpc
= afs_fs_symlink
,
1770 .issue_yfs_rpc
= yfs_fs_symlink
,
1771 .success
= afs_create_success
,
1772 .aborted
= afs_check_for_remote_deletion
,
1773 .edit_dir
= afs_create_edit_dir
,
1774 .put
= afs_create_put
,
1778 * create a symlink in an AFS filesystem
1780 static int afs_symlink(struct mnt_idmap
*idmap
, struct inode
*dir
,
1781 struct dentry
*dentry
, const char *content
)
1783 struct afs_operation
*op
;
1784 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1787 _enter("{%llx:%llu},{%pd},%s",
1788 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
,
1791 ret
= -ENAMETOOLONG
;
1792 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1796 if (strlen(content
) >= AFSPATHMAX
)
1799 op
= afs_alloc_operation(NULL
, dvnode
->volume
);
1805 fscache_use_cookie(afs_vnode_cache(dvnode
), true);
1807 afs_op_set_vnode(op
, 0, dvnode
);
1808 op
->file
[0].dv_delta
= 1;
1810 op
->dentry
= dentry
;
1811 op
->ops
= &afs_symlink_operation
;
1812 op
->create
.reason
= afs_edit_dir_for_symlink
;
1813 op
->create
.symlink
= content
;
1814 op
->mtime
= current_time(dir
);
1815 ret
= afs_do_sync_operation(op
);
1816 afs_dir_unuse_cookie(dvnode
, ret
);
1821 _leave(" = %d", ret
);
1825 static void afs_rename_success(struct afs_operation
*op
)
1827 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(op
->dentry
));
1829 _enter("op=%08x", op
->debug_id
);
1831 op
->ctime
= op
->file
[0].scb
.status
.mtime_client
;
1832 afs_check_dir_conflict(op
, &op
->file
[1]);
1833 afs_vnode_commit_status(op
, &op
->file
[0]);
1834 if (op
->file
[1].vnode
!= op
->file
[0].vnode
) {
1835 op
->ctime
= op
->file
[1].scb
.status
.mtime_client
;
1836 afs_vnode_commit_status(op
, &op
->file
[1]);
1839 /* If we're moving a subdir between dirs, we need to update
1840 * its DV counter too as the ".." will be altered.
1842 if (S_ISDIR(vnode
->netfs
.inode
.i_mode
) &&
1843 op
->file
[0].vnode
!= op
->file
[1].vnode
) {
1846 write_seqlock(&vnode
->cb_lock
);
1848 new_dv
= vnode
->status
.data_version
+ 1;
1849 trace_afs_set_dv(vnode
, new_dv
);
1850 vnode
->status
.data_version
= new_dv
;
1851 inode_set_iversion_raw(&vnode
->netfs
.inode
, new_dv
);
1853 write_sequnlock(&vnode
->cb_lock
);
1857 static void afs_rename_edit_dir(struct afs_operation
*op
)
1859 struct netfs_cache_resources orig_cres
= {}, new_cres
= {};
1860 struct afs_vnode_param
*orig_dvp
= &op
->file
[0];
1861 struct afs_vnode_param
*new_dvp
= &op
->file
[1];
1862 struct afs_vnode
*orig_dvnode
= orig_dvp
->vnode
;
1863 struct afs_vnode
*new_dvnode
= new_dvp
->vnode
;
1864 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(op
->dentry
));
1865 struct dentry
*old_dentry
= op
->dentry
;
1866 struct dentry
*new_dentry
= op
->dentry_2
;
1867 struct inode
*new_inode
;
1869 _enter("op=%08x", op
->debug_id
);
1871 if (op
->rename
.rehash
) {
1872 d_rehash(op
->rename
.rehash
);
1873 op
->rename
.rehash
= NULL
;
1876 fscache_begin_write_operation(&orig_cres
, afs_vnode_cache(orig_dvnode
));
1877 if (new_dvnode
!= orig_dvnode
)
1878 fscache_begin_write_operation(&new_cres
, afs_vnode_cache(new_dvnode
));
1880 down_write(&orig_dvnode
->validate_lock
);
1881 if (test_bit(AFS_VNODE_DIR_VALID
, &orig_dvnode
->flags
) &&
1882 orig_dvnode
->status
.data_version
== orig_dvp
->dv_before
+ orig_dvp
->dv_delta
)
1883 afs_edit_dir_remove(orig_dvnode
, &old_dentry
->d_name
,
1884 afs_edit_dir_for_rename_0
);
1886 if (new_dvnode
!= orig_dvnode
) {
1887 up_write(&orig_dvnode
->validate_lock
);
1888 down_write(&new_dvnode
->validate_lock
);
1891 if (test_bit(AFS_VNODE_DIR_VALID
, &new_dvnode
->flags
) &&
1892 new_dvnode
->status
.data_version
== new_dvp
->dv_before
+ new_dvp
->dv_delta
) {
1893 if (!op
->rename
.new_negative
)
1894 afs_edit_dir_remove(new_dvnode
, &new_dentry
->d_name
,
1895 afs_edit_dir_for_rename_1
);
1897 afs_edit_dir_add(new_dvnode
, &new_dentry
->d_name
,
1898 &vnode
->fid
, afs_edit_dir_for_rename_2
);
1901 if (S_ISDIR(vnode
->netfs
.inode
.i_mode
) &&
1902 new_dvnode
!= orig_dvnode
&&
1903 test_bit(AFS_VNODE_DIR_VALID
, &vnode
->flags
))
1904 afs_edit_dir_update_dotdot(vnode
, new_dvnode
,
1905 afs_edit_dir_for_rename_sub
);
1907 new_inode
= d_inode(new_dentry
);
1909 spin_lock(&new_inode
->i_lock
);
1910 if (S_ISDIR(new_inode
->i_mode
))
1911 clear_nlink(new_inode
);
1912 else if (new_inode
->i_nlink
> 0)
1913 drop_nlink(new_inode
);
1914 spin_unlock(&new_inode
->i_lock
);
1917 /* Now we can update d_fsdata on the dentries to reflect their
1918 * new parent's data_version.
1920 * Note that if we ever implement RENAME_EXCHANGE, we'll have
1921 * to update both dentries with opposing dir versions.
1923 afs_update_dentry_version(op
, new_dvp
, op
->dentry
);
1924 afs_update_dentry_version(op
, new_dvp
, op
->dentry_2
);
1926 d_move(old_dentry
, new_dentry
);
1928 up_write(&new_dvnode
->validate_lock
);
1929 fscache_end_operation(&orig_cres
);
1930 if (new_dvnode
!= orig_dvnode
)
1931 fscache_end_operation(&new_cres
);
1934 static void afs_rename_put(struct afs_operation
*op
)
1936 _enter("op=%08x", op
->debug_id
);
1937 if (op
->rename
.rehash
)
1938 d_rehash(op
->rename
.rehash
);
1939 dput(op
->rename
.tmp
);
1940 if (afs_op_error(op
))
1941 d_rehash(op
->dentry
);
1944 static const struct afs_operation_ops afs_rename_operation
= {
1945 .issue_afs_rpc
= afs_fs_rename
,
1946 .issue_yfs_rpc
= yfs_fs_rename
,
1947 .success
= afs_rename_success
,
1948 .edit_dir
= afs_rename_edit_dir
,
1949 .put
= afs_rename_put
,
1953 * rename a file in an AFS filesystem and/or move it between directories
1955 static int afs_rename(struct mnt_idmap
*idmap
, struct inode
*old_dir
,
1956 struct dentry
*old_dentry
, struct inode
*new_dir
,
1957 struct dentry
*new_dentry
, unsigned int flags
)
1959 struct afs_operation
*op
;
1960 struct afs_vnode
*orig_dvnode
, *new_dvnode
, *vnode
;
1966 /* Don't allow silly-rename files be moved around. */
1967 if (old_dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
1970 vnode
= AFS_FS_I(d_inode(old_dentry
));
1971 orig_dvnode
= AFS_FS_I(old_dir
);
1972 new_dvnode
= AFS_FS_I(new_dir
);
1974 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1975 orig_dvnode
->fid
.vid
, orig_dvnode
->fid
.vnode
,
1976 vnode
->fid
.vid
, vnode
->fid
.vnode
,
1977 new_dvnode
->fid
.vid
, new_dvnode
->fid
.vnode
,
1980 op
= afs_alloc_operation(NULL
, orig_dvnode
->volume
);
1984 fscache_use_cookie(afs_vnode_cache(orig_dvnode
), true);
1985 if (new_dvnode
!= orig_dvnode
)
1986 fscache_use_cookie(afs_vnode_cache(new_dvnode
), true);
1988 ret
= afs_validate(vnode
, op
->key
);
1989 afs_op_set_error(op
, ret
);
1993 afs_op_set_vnode(op
, 0, orig_dvnode
);
1994 afs_op_set_vnode(op
, 1, new_dvnode
); /* May be same as orig_dvnode */
1995 op
->file
[0].dv_delta
= 1;
1996 op
->file
[1].dv_delta
= 1;
1997 op
->file
[0].modification
= true;
1998 op
->file
[1].modification
= true;
1999 op
->file
[0].update_ctime
= true;
2000 op
->file
[1].update_ctime
= true;
2002 op
->dentry
= old_dentry
;
2003 op
->dentry_2
= new_dentry
;
2004 op
->rename
.new_negative
= d_is_negative(new_dentry
);
2005 op
->ops
= &afs_rename_operation
;
2007 /* For non-directories, check whether the target is busy and if so,
2008 * make a copy of the dentry and then do a silly-rename. If the
2009 * silly-rename succeeds, the copied dentry is hashed and becomes the
2012 if (d_is_positive(new_dentry
) && !d_is_dir(new_dentry
)) {
2013 /* To prevent any new references to the target during the
2014 * rename, we unhash the dentry in advance.
2016 if (!d_unhashed(new_dentry
)) {
2018 op
->rename
.rehash
= new_dentry
;
2021 if (d_count(new_dentry
) > 2) {
2022 /* copy the target dentry's name */
2023 op
->rename
.tmp
= d_alloc(new_dentry
->d_parent
,
2024 &new_dentry
->d_name
);
2025 if (!op
->rename
.tmp
) {
2030 ret
= afs_sillyrename(new_dvnode
,
2031 AFS_FS_I(d_inode(new_dentry
)),
2032 new_dentry
, op
->key
);
2034 afs_op_set_error(op
, ret
);
2038 op
->dentry_2
= op
->rename
.tmp
;
2039 op
->rename
.rehash
= NULL
;
2040 op
->rename
.new_negative
= true;
2044 /* This bit is potentially nasty as there's a potential race with
2045 * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry
2046 * to reflect it's new parent's new data_version after the op, but
2047 * d_revalidate may see old_dentry between the op having taken place
2048 * and the version being updated.
2050 * So drop the old_dentry for now to make other threads go through
2051 * lookup instead - which we hold a lock against.
2055 ret
= afs_do_sync_operation(op
);
2057 afs_dir_unuse_cookie(orig_dvnode
, ret
);
2058 if (new_dvnode
!= orig_dvnode
)
2059 afs_dir_unuse_cookie(new_dvnode
, ret
);
2063 ret
= afs_put_operation(op
);
2068 * Write the file contents to the cache as a single blob.
2070 int afs_single_writepages(struct address_space
*mapping
,
2071 struct writeback_control
*wbc
)
2073 struct afs_vnode
*dvnode
= AFS_FS_I(mapping
->host
);
2074 struct iov_iter iter
;
2075 bool is_dir
= (S_ISDIR(dvnode
->netfs
.inode
.i_mode
) &&
2076 !test_bit(AFS_VNODE_MOUNTPOINT
, &dvnode
->flags
));
2079 /* Need to lock to prevent the folio queue and folios from being thrown
2082 down_read(&dvnode
->validate_lock
);
2085 test_bit(AFS_VNODE_DIR_VALID
, &dvnode
->flags
) :
2086 atomic64_read(&dvnode
->cb_expires_at
) != AFS_NO_CB_PROMISE
) {
2087 iov_iter_folio_queue(&iter
, ITER_SOURCE
, dvnode
->directory
, 0, 0,
2088 i_size_read(&dvnode
->netfs
.inode
));
2089 ret
= netfs_writeback_single(mapping
, wbc
, &iter
);
2092 up_read(&dvnode
->validate_lock
);