1 /* dir.c: AFS filesystem directory handling
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
16 #include <linux/namei.h>
17 #include <linux/pagemap.h>
18 #include <linux/ctype.h>
19 #include <linux/sched.h>
20 #include <linux/dns_resolver.h>
23 static struct dentry
*afs_lookup(struct inode
*dir
, struct dentry
*dentry
,
25 static struct dentry
*afs_dynroot_lookup(struct inode
*dir
, struct dentry
*dentry
,
27 static int afs_dir_open(struct inode
*inode
, struct file
*file
);
28 static int afs_readdir(struct file
*file
, struct dir_context
*ctx
);
29 static int afs_d_revalidate(struct dentry
*dentry
, unsigned int flags
);
30 static int afs_d_delete(const struct dentry
*dentry
);
31 static void afs_d_release(struct dentry
*dentry
);
32 static int 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 inode
*dir
, struct dentry
*dentry
, umode_t mode
,
36 static int afs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
37 static int afs_rmdir(struct inode
*dir
, struct dentry
*dentry
);
38 static int afs_unlink(struct inode
*dir
, struct dentry
*dentry
);
39 static int afs_link(struct dentry
*from
, struct inode
*dir
,
40 struct dentry
*dentry
);
41 static int afs_symlink(struct inode
*dir
, struct dentry
*dentry
,
43 static int afs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
44 struct inode
*new_dir
, struct dentry
*new_dentry
,
47 const struct file_operations afs_dir_file_operations
= {
49 .release
= afs_release
,
50 .iterate_shared
= afs_readdir
,
52 .llseek
= generic_file_llseek
,
55 const struct inode_operations afs_dir_inode_operations
= {
60 .symlink
= afs_symlink
,
64 .permission
= afs_permission
,
65 .getattr
= afs_getattr
,
66 .setattr
= afs_setattr
,
67 .listxattr
= afs_listxattr
,
70 const struct file_operations afs_dynroot_file_operations
= {
71 .open
= dcache_dir_open
,
72 .release
= dcache_dir_close
,
73 .iterate_shared
= dcache_readdir
,
74 .llseek
= dcache_dir_lseek
,
77 const struct inode_operations afs_dynroot_inode_operations
= {
78 .lookup
= afs_dynroot_lookup
,
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
,
88 #define AFS_DIR_HASHTBL_SIZE 128
89 #define AFS_DIR_DIRENT_SIZE 32
90 #define AFS_DIRENT_PER_BLOCK 64
100 uint8_t overflow
[4]; /* if any char of the name (inc
101 * NUL) reaches here, consume
102 * the next dirent too */
104 uint8_t extended_name
[32];
107 /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
108 struct afs_dir_pagehdr
{
111 #define AFS_DIR_MAGIC htons(1234)
117 /* directory block layout */
118 union afs_dir_block
{
120 struct afs_dir_pagehdr pagehdr
;
123 struct afs_dir_pagehdr pagehdr
;
124 uint8_t alloc_ctrs
[128];
126 uint16_t hashtable
[AFS_DIR_HASHTBL_SIZE
];
129 union afs_dirent dirents
[AFS_DIRENT_PER_BLOCK
];
132 /* layout on a linux VM page */
133 struct afs_dir_page
{
134 union afs_dir_block blocks
[PAGE_SIZE
/ sizeof(union afs_dir_block
)];
137 struct afs_lookup_cookie
{
138 struct dir_context ctx
;
145 * check that a directory page is valid
147 bool afs_dir_check_page(struct inode
*dir
, struct page
*page
)
149 struct afs_dir_page
*dbuf
;
150 struct afs_vnode
*vnode
= AFS_FS_I(dir
);
151 loff_t latter
, i_size
, off
;
155 /* check the page count */
156 qty
= desc
.size
/ sizeof(dbuf
->blocks
[0]);
160 if (page
->index
== 0 && qty
!= ntohs(dbuf
->blocks
[0].pagehdr
.npages
)) {
161 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
162 __func__
, dir
->i_ino
, qty
,
163 ntohs(dbuf
->blocks
[0].pagehdr
.npages
));
168 /* Determine how many magic numbers there should be in this page, but
169 * we must take care because the directory may change size under us.
171 off
= page_offset(page
);
172 i_size
= i_size_read(dir
);
176 latter
= i_size
- off
;
177 if (latter
>= PAGE_SIZE
)
181 qty
/= sizeof(union afs_dir_block
);
184 dbuf
= page_address(page
);
185 for (tmp
= 0; tmp
< qty
; tmp
++) {
186 if (dbuf
->blocks
[tmp
].pagehdr
.magic
!= AFS_DIR_MAGIC
) {
187 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
188 __func__
, dir
->i_ino
, tmp
, qty
,
189 ntohs(dbuf
->blocks
[tmp
].pagehdr
.magic
));
190 trace_afs_dir_check_failed(vnode
, off
, i_size
);
196 SetPageChecked(page
);
205 * discard a page cached in the pagecache
207 static inline void afs_dir_put_page(struct page
*page
)
215 * get a page into the pagecache
217 static struct page
*afs_dir_get_page(struct inode
*dir
, unsigned long index
,
221 _enter("{%lu},%lu", dir
->i_ino
, index
);
223 page
= read_cache_page(dir
->i_mapping
, index
, afs_page_filler
, key
);
227 if (unlikely(!PageChecked(page
))) {
235 afs_dir_put_page(page
);
237 return ERR_PTR(-EIO
);
241 * open an AFS directory file
243 static int afs_dir_open(struct inode
*inode
, struct file
*file
)
245 _enter("{%lu}", inode
->i_ino
);
247 BUILD_BUG_ON(sizeof(union afs_dir_block
) != 2048);
248 BUILD_BUG_ON(sizeof(union afs_dirent
) != 32);
250 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(inode
)->flags
))
253 return afs_open(inode
, file
);
257 * deal with one block in an AFS directory
259 static int afs_dir_iterate_block(struct dir_context
*ctx
,
260 union afs_dir_block
*block
,
263 union afs_dirent
*dire
;
264 unsigned offset
, next
, curr
;
268 _enter("%u,%x,%p,,",(unsigned)ctx
->pos
,blkoff
,block
);
270 curr
= (ctx
->pos
- blkoff
) / sizeof(union afs_dirent
);
272 /* walk through the block, an entry at a time */
273 for (offset
= AFS_DIRENT_PER_BLOCK
- block
->pagehdr
.nentries
;
274 offset
< AFS_DIRENT_PER_BLOCK
;
279 /* skip entries marked unused in the bitmap */
280 if (!(block
->pagehdr
.bitmap
[offset
/ 8] &
281 (1 << (offset
% 8)))) {
282 _debug("ENT[%zu.%u]: unused",
283 blkoff
/ sizeof(union afs_dir_block
), offset
);
286 next
* sizeof(union afs_dirent
);
290 /* got a valid entry */
291 dire
= &block
->dirents
[offset
];
292 nlen
= strnlen(dire
->u
.name
,
294 offset
* sizeof(union afs_dirent
));
296 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
297 blkoff
/ sizeof(union afs_dir_block
), offset
,
298 (offset
< curr
? "skip" : "fill"),
301 /* work out where the next possible entry is */
302 for (tmp
= nlen
; tmp
> 15; tmp
-= sizeof(union afs_dirent
)) {
303 if (next
>= AFS_DIRENT_PER_BLOCK
) {
304 _debug("ENT[%zu.%u]:"
305 " %u travelled beyond end dir block"
307 blkoff
/ sizeof(union afs_dir_block
),
308 offset
, next
, tmp
, nlen
);
311 if (!(block
->pagehdr
.bitmap
[next
/ 8] &
312 (1 << (next
% 8)))) {
313 _debug("ENT[%zu.%u]:"
314 " %u unmarked extension (len %u/%zu)",
315 blkoff
/ sizeof(union afs_dir_block
),
316 offset
, next
, tmp
, nlen
);
320 _debug("ENT[%zu.%u]: ext %u/%zu",
321 blkoff
/ sizeof(union afs_dir_block
),
326 /* skip if starts before the current position */
330 /* found the next entry */
331 if (!dir_emit(ctx
, dire
->u
.name
, nlen
,
332 ntohl(dire
->u
.vnode
),
333 ctx
->actor
== afs_lookup_filldir
?
334 ntohl(dire
->u
.unique
) : DT_UNKNOWN
)) {
335 _leave(" = 0 [full]");
339 ctx
->pos
= blkoff
+ next
* sizeof(union afs_dirent
);
342 _leave(" = 1 [more]");
347 * iterate through the data blob that lists the contents of an AFS directory
349 static int afs_dir_iterate(struct inode
*dir
, struct dir_context
*ctx
,
352 union afs_dir_block
*dblock
;
353 struct afs_dir_page
*dbuf
;
355 unsigned blkoff
, limit
;
358 _enter("{%lu},%u,,", dir
->i_ino
, (unsigned)ctx
->pos
);
360 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(dir
)->flags
)) {
361 _leave(" = -ESTALE");
365 /* round the file position up to the next entry boundary */
366 ctx
->pos
+= sizeof(union afs_dirent
) - 1;
367 ctx
->pos
&= ~(sizeof(union afs_dirent
) - 1);
369 /* walk through the blocks in sequence */
371 while (ctx
->pos
< dir
->i_size
) {
372 blkoff
= ctx
->pos
& ~(sizeof(union afs_dir_block
) - 1);
374 /* fetch the appropriate page from the directory */
375 page
= afs_dir_get_page(dir
, blkoff
/ PAGE_SIZE
, key
);
381 limit
= blkoff
& ~(PAGE_SIZE
- 1);
383 dbuf
= page_address(page
);
385 /* deal with the individual blocks stashed on this page */
387 dblock
= &dbuf
->blocks
[(blkoff
% PAGE_SIZE
) /
388 sizeof(union afs_dir_block
)];
389 ret
= afs_dir_iterate_block(ctx
, dblock
, blkoff
);
391 afs_dir_put_page(page
);
395 blkoff
+= sizeof(union afs_dir_block
);
397 } while (ctx
->pos
< dir
->i_size
&& blkoff
< limit
);
399 afs_dir_put_page(page
);
404 _leave(" = %d", ret
);
409 * read an AFS directory
411 static int afs_readdir(struct file
*file
, struct dir_context
*ctx
)
413 return afs_dir_iterate(file_inode(file
), ctx
, afs_file_key(file
));
417 * search the directory for a name
418 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
419 * uniquifier through dtype
421 static int afs_lookup_filldir(struct dir_context
*ctx
, const char *name
,
422 int nlen
, loff_t fpos
, u64 ino
, unsigned dtype
)
424 struct afs_lookup_cookie
*cookie
=
425 container_of(ctx
, struct afs_lookup_cookie
, ctx
);
427 _enter("{%s,%u},%s,%u,,%llu,%u",
428 cookie
->name
.name
, cookie
->name
.len
, name
, nlen
,
429 (unsigned long long) ino
, dtype
);
431 /* insanity checks first */
432 BUILD_BUG_ON(sizeof(union afs_dir_block
) != 2048);
433 BUILD_BUG_ON(sizeof(union afs_dirent
) != 32);
435 if (cookie
->name
.len
!= nlen
||
436 memcmp(cookie
->name
.name
, name
, nlen
) != 0) {
441 cookie
->fid
.vnode
= ino
;
442 cookie
->fid
.unique
= dtype
;
445 _leave(" = -1 [found]");
450 * do a lookup in a directory
451 * - just returns the FID the dentry name maps to if found
453 static int afs_do_lookup(struct inode
*dir
, struct dentry
*dentry
,
454 struct afs_fid
*fid
, struct key
*key
)
456 struct afs_super_info
*as
= dir
->i_sb
->s_fs_info
;
457 struct afs_lookup_cookie cookie
= {
458 .ctx
.actor
= afs_lookup_filldir
,
459 .name
= dentry
->d_name
,
460 .fid
.vid
= as
->volume
->vid
464 _enter("{%lu},%p{%pd},", dir
->i_ino
, dentry
, dentry
);
466 /* search the directory */
467 ret
= afs_dir_iterate(dir
, &cookie
.ctx
, key
);
469 _leave(" = %d [iter]", ret
);
475 _leave(" = -ENOENT [not found]");
480 _leave(" = 0 { vn=%u u=%u }", fid
->vnode
, fid
->unique
);
485 * Probe to see if a cell may exist. This prevents positive dentries from
486 * being created unnecessarily.
488 static int afs_probe_cell_name(struct dentry
*dentry
)
490 struct afs_cell
*cell
;
491 const char *name
= dentry
->d_name
.name
;
492 size_t len
= dentry
->d_name
.len
;
495 /* Names prefixed with a dot are R/W mounts. */
496 if (name
[0] == '.') {
503 cell
= afs_lookup_cell_rcu(afs_d2net(dentry
), name
, len
);
505 afs_put_cell(afs_d2net(dentry
), cell
);
509 ret
= dns_query("afsdb", name
, len
, "ipv4", NULL
, NULL
);
516 * Try to auto mount the mountpoint with pseudo directory, if the autocell
517 * operation is setted.
519 static struct inode
*afs_try_auto_mntpt(struct dentry
*dentry
,
520 struct inode
*dir
, struct afs_fid
*fid
)
522 struct afs_vnode
*vnode
= AFS_FS_I(dir
);
526 _enter("%p{%pd}, {%x:%u}",
527 dentry
, dentry
, vnode
->fid
.vid
, vnode
->fid
.vnode
);
529 if (!test_bit(AFS_VNODE_AUTOCELL
, &vnode
->flags
))
532 ret
= afs_probe_cell_name(dentry
);
536 inode
= afs_iget_pseudo_dir(dir
->i_sb
, false);
538 ret
= PTR_ERR(inode
);
542 *fid
= AFS_FS_I(inode
)->fid
;
543 _leave("= %p", inode
);
552 * look up an entry in a directory
554 static struct dentry
*afs_lookup(struct inode
*dir
, struct dentry
*dentry
,
557 struct afs_vnode
*vnode
;
563 vnode
= AFS_FS_I(dir
);
565 _enter("{%x:%u},%p{%pd},",
566 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
, dentry
);
568 ASSERTCMP(d_inode(dentry
), ==, NULL
);
570 if (dentry
->d_name
.len
>= AFSNAMEMAX
) {
571 _leave(" = -ENAMETOOLONG");
572 return ERR_PTR(-ENAMETOOLONG
);
575 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
576 _leave(" = -ESTALE");
577 return ERR_PTR(-ESTALE
);
580 key
= afs_request_key(vnode
->volume
->cell
);
582 _leave(" = %ld [key]", PTR_ERR(key
));
583 return ERR_CAST(key
);
586 ret
= afs_validate(vnode
, key
);
589 _leave(" = %d [val]", ret
);
593 ret
= afs_do_lookup(dir
, dentry
, &fid
, key
);
595 if (ret
== -ENOENT
) {
596 inode
= afs_try_auto_mntpt(dentry
, dir
, &fid
);
597 if (!IS_ERR(inode
)) {
602 ret
= PTR_ERR(inode
);
606 if (ret
== -ENOENT
) {
608 _leave(" = NULL [negative]");
611 _leave(" = %d [do]", ret
);
614 dentry
->d_fsdata
= (void *)(unsigned long) vnode
->status
.data_version
;
616 /* instantiate the dentry */
617 inode
= afs_iget(dir
->i_sb
, key
, &fid
, NULL
, NULL
, NULL
);
620 _leave(" = %ld", PTR_ERR(inode
));
621 return ERR_CAST(inode
);
625 d_add(dentry
, inode
);
626 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%u }",
629 d_inode(dentry
)->i_ino
,
630 d_inode(dentry
)->i_generation
);
636 * Look up an entry in a dynroot directory.
638 static struct dentry
*afs_dynroot_lookup(struct inode
*dir
, struct dentry
*dentry
,
641 struct afs_vnode
*vnode
;
646 vnode
= AFS_FS_I(dir
);
648 _enter("%pd", dentry
);
650 ASSERTCMP(d_inode(dentry
), ==, NULL
);
652 if (dentry
->d_name
.len
>= AFSNAMEMAX
) {
653 _leave(" = -ENAMETOOLONG");
654 return ERR_PTR(-ENAMETOOLONG
);
657 inode
= afs_try_auto_mntpt(dentry
, dir
, &fid
);
659 ret
= PTR_ERR(inode
);
660 if (ret
== -ENOENT
) {
662 _leave(" = NULL [negative]");
665 _leave(" = %d [do]", ret
);
669 d_add(dentry
, inode
);
670 _leave(" = 0 { ino=%lu v=%u }",
671 d_inode(dentry
)->i_ino
, d_inode(dentry
)->i_generation
);
676 * check that a dentry lookup hit has found a valid entry
677 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
680 static int afs_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
682 struct afs_super_info
*as
= dentry
->d_sb
->s_fs_info
;
683 struct afs_vnode
*vnode
, *dir
;
684 struct afs_fid
uninitialized_var(fid
);
685 struct dentry
*parent
;
691 if (flags
& LOOKUP_RCU
)
697 if (d_really_is_positive(dentry
)) {
698 vnode
= AFS_FS_I(d_inode(dentry
));
699 _enter("{v={%x:%u} n=%pd fl=%lx},",
700 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
,
703 _enter("{neg n=%pd}", dentry
);
706 key
= afs_request_key(AFS_FS_S(dentry
->d_sb
)->volume
->cell
);
710 if (d_really_is_positive(dentry
)) {
711 inode
= d_inode(dentry
);
713 vnode
= AFS_FS_I(inode
);
714 afs_validate(vnode
, key
);
715 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
720 /* lock down the parent dentry so we can peer at it */
721 parent
= dget_parent(dentry
);
722 dir
= AFS_FS_I(d_inode(parent
));
724 /* validate the parent directory */
725 afs_validate(dir
, key
);
727 if (test_bit(AFS_VNODE_DELETED
, &dir
->flags
)) {
728 _debug("%pd: parent dir deleted", dentry
);
732 dir_version
= (void *) (unsigned long) dir
->status
.data_version
;
733 if (dentry
->d_fsdata
== dir_version
)
734 goto out_valid
; /* the dir contents are unchanged */
736 _debug("dir modified");
738 /* search the directory for this vnode */
739 ret
= afs_do_lookup(&dir
->vfs_inode
, dentry
, &fid
, key
);
742 /* the filename maps to something */
743 if (d_really_is_negative(dentry
))
745 inode
= d_inode(dentry
);
746 if (is_bad_inode(inode
)) {
747 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
752 vnode
= AFS_FS_I(inode
);
754 /* if the vnode ID has changed, then the dirent points to a
756 if (fid
.vnode
!= vnode
->fid
.vnode
) {
757 _debug("%pd: dirent changed [%u != %u]",
763 /* if the vnode ID uniqifier has changed, then the file has
764 * been deleted and replaced, and the original vnode ID has
766 if (fid
.unique
!= vnode
->fid
.unique
) {
767 _debug("%pd: file deleted (uq %u -> %u I:%u)",
770 vnode
->vfs_inode
.i_generation
);
771 write_seqlock(&vnode
->cb_lock
);
772 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
773 write_sequnlock(&vnode
->cb_lock
);
779 /* the filename is unknown */
780 _debug("%pd: dirent not found", dentry
);
781 if (d_really_is_positive(dentry
))
786 _debug("failed to iterate dir %pd: %d",
792 dentry
->d_fsdata
= dir_version
;
795 _leave(" = 1 [valid]");
798 /* the dirent, if it exists, now points to a different vnode */
800 spin_lock(&dentry
->d_lock
);
801 dentry
->d_flags
|= DCACHE_NFSFS_RENAMED
;
802 spin_unlock(&dentry
->d_lock
);
805 _debug("dropping dentry %pd2", dentry
);
810 _leave(" = 0 [bad]");
815 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
817 * - called from dput() when d_count is going to 0.
818 * - return 1 to request dentry be unhashed, 0 otherwise
820 static int afs_d_delete(const struct dentry
*dentry
)
822 _enter("%pd", dentry
);
824 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
827 if (d_really_is_positive(dentry
) &&
828 (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(d_inode(dentry
))->flags
) ||
829 test_bit(AFS_VNODE_PSEUDODIR
, &AFS_FS_I(d_inode(dentry
))->flags
)))
832 _leave(" = 0 [keep]");
836 _leave(" = 1 [zap]");
841 * handle dentry release
843 static void afs_d_release(struct dentry
*dentry
)
845 _enter("%pd", dentry
);
849 * Create a new inode for create/mkdir/symlink
851 static void afs_vnode_new_inode(struct afs_fs_cursor
*fc
,
852 struct dentry
*new_dentry
,
853 struct afs_fid
*newfid
,
854 struct afs_file_status
*newstatus
,
855 struct afs_callback
*newcb
)
859 if (fc
->ac
.error
< 0)
864 inode
= afs_iget(fc
->vnode
->vfs_inode
.i_sb
, fc
->key
,
865 newfid
, newstatus
, newcb
, fc
->cbi
);
867 /* ENOMEM or EINTR at a really inconvenient time - just abandon
868 * the new directory on the server.
870 fc
->ac
.error
= PTR_ERR(inode
);
874 d_add(new_dentry
, inode
);
878 * create a directory on an AFS filesystem
880 static int afs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
882 struct afs_file_status newstatus
;
883 struct afs_fs_cursor fc
;
884 struct afs_callback newcb
;
885 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
886 struct afs_fid newfid
;
892 _enter("{%x:%u},{%pd},%ho",
893 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
, mode
);
895 key
= afs_request_key(dvnode
->volume
->cell
);
902 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
903 while (afs_select_fileserver(&fc
)) {
904 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
905 afs_fs_create(&fc
, dentry
->d_name
.name
, mode
,
906 &newfid
, &newstatus
, &newcb
);
909 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
910 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
911 afs_vnode_new_inode(&fc
, dentry
, &newfid
, &newstatus
, &newcb
);
912 ret
= afs_end_vnode_operation(&fc
);
927 _leave(" = %d", ret
);
932 * Remove a subdir from a directory.
934 static void afs_dir_remove_subdir(struct dentry
*dentry
)
936 if (d_really_is_positive(dentry
)) {
937 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
939 clear_nlink(&vnode
->vfs_inode
);
940 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
941 clear_bit(AFS_VNODE_CB_PROMISED
, &vnode
->flags
);
946 * remove a directory from an AFS filesystem
948 static int afs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
950 struct afs_fs_cursor fc
;
951 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
955 _enter("{%x:%u},{%pd}",
956 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
);
958 key
= afs_request_key(dvnode
->volume
->cell
);
965 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
966 while (afs_select_fileserver(&fc
)) {
967 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
968 afs_fs_remove(&fc
, dentry
->d_name
.name
, true);
971 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
972 ret
= afs_end_vnode_operation(&fc
);
974 afs_dir_remove_subdir(dentry
);
983 * Remove a link to a file or symlink from a directory.
985 * If the file was not deleted due to excess hard links, the fileserver will
986 * break the callback promise on the file - if it had one - before it returns
987 * to us, and if it was deleted, it won't
989 * However, if we didn't have a callback promise outstanding, or it was
990 * outstanding on a different server, then it won't break it either...
992 static int afs_dir_remove_link(struct dentry
*dentry
, struct key
*key
,
993 unsigned long d_version_before
,
994 unsigned long d_version_after
)
999 /* There were no intervening changes on the server if the version
1000 * number we got back was incremented by exactly 1.
1002 dir_valid
= (d_version_after
== d_version_before
+ 1);
1004 if (d_really_is_positive(dentry
)) {
1005 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
1008 drop_nlink(&vnode
->vfs_inode
);
1009 if (vnode
->vfs_inode
.i_nlink
== 0) {
1010 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
1011 clear_bit(AFS_VNODE_CB_PROMISED
, &vnode
->flags
);
1015 clear_bit(AFS_VNODE_CB_PROMISED
, &vnode
->flags
);
1017 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
1018 kdebug("AFS_VNODE_DELETED");
1020 ret
= afs_validate(vnode
, key
);
1024 _debug("nlink %d [val %d]", vnode
->vfs_inode
.i_nlink
, ret
);
1031 * Remove a file or symlink from an AFS filesystem.
1033 static int afs_unlink(struct inode
*dir
, struct dentry
*dentry
)
1035 struct afs_fs_cursor fc
;
1036 struct afs_vnode
*dvnode
= AFS_FS_I(dir
), *vnode
;
1038 unsigned long d_version
= (unsigned long)dentry
->d_fsdata
;
1041 _enter("{%x:%u},{%pd}",
1042 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
);
1044 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1045 return -ENAMETOOLONG
;
1047 key
= afs_request_key(dvnode
->volume
->cell
);
1053 /* Try to make sure we have a callback promise on the victim. */
1054 if (d_really_is_positive(dentry
)) {
1055 vnode
= AFS_FS_I(d_inode(dentry
));
1056 ret
= afs_validate(vnode
, key
);
1062 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
1063 while (afs_select_fileserver(&fc
)) {
1064 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
1065 afs_fs_remove(&fc
, dentry
->d_name
.name
, false);
1068 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
1069 ret
= afs_end_vnode_operation(&fc
);
1071 ret
= afs_dir_remove_link(
1072 dentry
, key
, d_version
,
1073 (unsigned long)dvnode
->status
.data_version
);
1079 _leave(" = %d", ret
);
1084 * create a regular file on an AFS filesystem
1086 static int afs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
1089 struct afs_fs_cursor fc
;
1090 struct afs_file_status newstatus
;
1091 struct afs_callback newcb
;
1092 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1093 struct afs_fid newfid
;
1099 _enter("{%x:%u},{%pd},%ho,",
1100 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
, mode
);
1102 ret
= -ENAMETOOLONG
;
1103 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1106 key
= afs_request_key(dvnode
->volume
->cell
);
1113 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
1114 while (afs_select_fileserver(&fc
)) {
1115 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
1116 afs_fs_create(&fc
, dentry
->d_name
.name
, mode
,
1117 &newfid
, &newstatus
, &newcb
);
1120 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
1121 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
1122 afs_vnode_new_inode(&fc
, dentry
, &newfid
, &newstatus
, &newcb
);
1123 ret
= afs_end_vnode_operation(&fc
);
1138 _leave(" = %d", ret
);
1143 * create a hard link between files in an AFS filesystem
1145 static int afs_link(struct dentry
*from
, struct inode
*dir
,
1146 struct dentry
*dentry
)
1148 struct afs_fs_cursor fc
;
1149 struct afs_vnode
*dvnode
, *vnode
;
1153 vnode
= AFS_FS_I(d_inode(from
));
1154 dvnode
= AFS_FS_I(dir
);
1156 _enter("{%x:%u},{%x:%u},{%pd}",
1157 vnode
->fid
.vid
, vnode
->fid
.vnode
,
1158 dvnode
->fid
.vid
, dvnode
->fid
.vnode
,
1161 ret
= -ENAMETOOLONG
;
1162 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1165 key
= afs_request_key(dvnode
->volume
->cell
);
1172 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
1173 if (mutex_lock_interruptible_nested(&vnode
->io_lock
, 1) < 0) {
1174 afs_end_vnode_operation(&fc
);
1178 while (afs_select_fileserver(&fc
)) {
1179 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
1180 fc
.cb_break_2
= vnode
->cb_break
+ vnode
->cb_s_break
;
1181 afs_fs_link(&fc
, vnode
, dentry
->d_name
.name
);
1184 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
1185 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break_2
);
1186 ihold(&vnode
->vfs_inode
);
1187 d_instantiate(dentry
, &vnode
->vfs_inode
);
1189 mutex_unlock(&vnode
->io_lock
);
1190 ret
= afs_end_vnode_operation(&fc
);
1205 _leave(" = %d", ret
);
1210 * create a symlink in an AFS filesystem
1212 static int afs_symlink(struct inode
*dir
, struct dentry
*dentry
,
1213 const char *content
)
1215 struct afs_fs_cursor fc
;
1216 struct afs_file_status newstatus
;
1217 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1218 struct afs_fid newfid
;
1222 _enter("{%x:%u},{%pd},%s",
1223 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
,
1226 ret
= -ENAMETOOLONG
;
1227 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1231 if (strlen(content
) >= AFSPATHMAX
)
1234 key
= afs_request_key(dvnode
->volume
->cell
);
1241 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
1242 while (afs_select_fileserver(&fc
)) {
1243 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
1244 afs_fs_symlink(&fc
, dentry
->d_name
.name
, content
,
1245 &newfid
, &newstatus
);
1248 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
1249 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
1250 afs_vnode_new_inode(&fc
, dentry
, &newfid
, &newstatus
, NULL
);
1251 ret
= afs_end_vnode_operation(&fc
);
1266 _leave(" = %d", ret
);
1271 * rename a file in an AFS filesystem and/or move it between directories
1273 static int afs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1274 struct inode
*new_dir
, struct dentry
*new_dentry
,
1277 struct afs_fs_cursor fc
;
1278 struct afs_vnode
*orig_dvnode
, *new_dvnode
, *vnode
;
1285 vnode
= AFS_FS_I(d_inode(old_dentry
));
1286 orig_dvnode
= AFS_FS_I(old_dir
);
1287 new_dvnode
= AFS_FS_I(new_dir
);
1289 _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
1290 orig_dvnode
->fid
.vid
, orig_dvnode
->fid
.vnode
,
1291 vnode
->fid
.vid
, vnode
->fid
.vnode
,
1292 new_dvnode
->fid
.vid
, new_dvnode
->fid
.vnode
,
1295 key
= afs_request_key(orig_dvnode
->volume
->cell
);
1302 if (afs_begin_vnode_operation(&fc
, orig_dvnode
, key
)) {
1303 if (orig_dvnode
!= new_dvnode
) {
1304 if (mutex_lock_interruptible_nested(&new_dvnode
->io_lock
, 1) < 0) {
1305 afs_end_vnode_operation(&fc
);
1309 while (afs_select_fileserver(&fc
)) {
1310 fc
.cb_break
= orig_dvnode
->cb_break
+ orig_dvnode
->cb_s_break
;
1311 fc
.cb_break_2
= new_dvnode
->cb_break
+ new_dvnode
->cb_s_break
;
1312 afs_fs_rename(&fc
, old_dentry
->d_name
.name
,
1313 new_dvnode
, new_dentry
->d_name
.name
);
1316 afs_vnode_commit_status(&fc
, orig_dvnode
, fc
.cb_break
);
1317 afs_vnode_commit_status(&fc
, new_dvnode
, fc
.cb_break_2
);
1318 if (orig_dvnode
!= new_dvnode
)
1319 mutex_unlock(&new_dvnode
->io_lock
);
1320 ret
= afs_end_vnode_operation(&fc
);
1328 _leave(" = %d", ret
);