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>
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 dentry
*dentry
, unsigned int flags
);
27 static int afs_d_delete(const struct dentry
*dentry
);
28 static void afs_d_release(struct dentry
*dentry
);
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
,
44 const struct file_operations afs_dir_file_operations
= {
46 .release
= afs_release
,
47 .iterate_shared
= afs_readdir
,
49 .llseek
= generic_file_llseek
,
52 const struct inode_operations afs_dir_inode_operations
= {
57 .symlink
= afs_symlink
,
61 .permission
= afs_permission
,
62 .getattr
= afs_getattr
,
63 .setattr
= afs_setattr
,
64 .listxattr
= afs_listxattr
,
67 const struct dentry_operations afs_fs_dentry_operations
= {
68 .d_revalidate
= afs_d_revalidate
,
69 .d_delete
= afs_d_delete
,
70 .d_release
= afs_d_release
,
71 .d_automount
= afs_d_automount
,
74 #define AFS_DIR_HASHTBL_SIZE 128
75 #define AFS_DIR_DIRENT_SIZE 32
76 #define AFS_DIRENT_PER_BLOCK 64
86 uint8_t overflow
[4]; /* if any char of the name (inc
87 * NUL) reaches here, consume
88 * the next dirent too */
90 uint8_t extended_name
[32];
93 /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
94 struct afs_dir_pagehdr
{
97 #define AFS_DIR_MAGIC htons(1234)
103 /* directory block layout */
104 union afs_dir_block
{
106 struct afs_dir_pagehdr pagehdr
;
109 struct afs_dir_pagehdr pagehdr
;
110 uint8_t alloc_ctrs
[128];
112 uint16_t hashtable
[AFS_DIR_HASHTBL_SIZE
];
115 union afs_dirent dirents
[AFS_DIRENT_PER_BLOCK
];
118 /* layout on a linux VM page */
119 struct afs_dir_page
{
120 union afs_dir_block blocks
[PAGE_SIZE
/ sizeof(union afs_dir_block
)];
123 struct afs_lookup_cookie
{
124 struct dir_context ctx
;
131 * check that a directory page is valid
133 bool afs_dir_check_page(struct inode
*dir
, struct page
*page
)
135 struct afs_dir_page
*dbuf
;
136 struct afs_vnode
*vnode
= AFS_FS_I(dir
);
137 loff_t latter
, i_size
, off
;
141 /* check the page count */
142 qty
= desc
.size
/ sizeof(dbuf
->blocks
[0]);
146 if (page
->index
== 0 && qty
!= ntohs(dbuf
->blocks
[0].pagehdr
.npages
)) {
147 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
148 __func__
, dir
->i_ino
, qty
,
149 ntohs(dbuf
->blocks
[0].pagehdr
.npages
));
154 /* Determine how many magic numbers there should be in this page, but
155 * we must take care because the directory may change size under us.
157 off
= page_offset(page
);
158 i_size
= i_size_read(dir
);
162 latter
= i_size
- off
;
163 if (latter
>= PAGE_SIZE
)
167 qty
/= sizeof(union afs_dir_block
);
170 dbuf
= page_address(page
);
171 for (tmp
= 0; tmp
< qty
; tmp
++) {
172 if (dbuf
->blocks
[tmp
].pagehdr
.magic
!= AFS_DIR_MAGIC
) {
173 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
174 __func__
, dir
->i_ino
, tmp
, qty
,
175 ntohs(dbuf
->blocks
[tmp
].pagehdr
.magic
));
176 trace_afs_dir_check_failed(vnode
, off
, i_size
);
182 SetPageChecked(page
);
191 * discard a page cached in the pagecache
193 static inline void afs_dir_put_page(struct page
*page
)
201 * get a page into the pagecache
203 static struct page
*afs_dir_get_page(struct inode
*dir
, unsigned long index
,
207 _enter("{%lu},%lu", dir
->i_ino
, index
);
209 page
= read_cache_page(dir
->i_mapping
, index
, afs_page_filler
, key
);
213 if (unlikely(!PageChecked(page
))) {
221 afs_dir_put_page(page
);
223 return ERR_PTR(-EIO
);
227 * open an AFS directory file
229 static int afs_dir_open(struct inode
*inode
, struct file
*file
)
231 _enter("{%lu}", inode
->i_ino
);
233 BUILD_BUG_ON(sizeof(union afs_dir_block
) != 2048);
234 BUILD_BUG_ON(sizeof(union afs_dirent
) != 32);
236 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(inode
)->flags
))
239 return afs_open(inode
, file
);
243 * deal with one block in an AFS directory
245 static int afs_dir_iterate_block(struct dir_context
*ctx
,
246 union afs_dir_block
*block
,
249 union afs_dirent
*dire
;
250 unsigned offset
, next
, curr
;
254 _enter("%u,%x,%p,,",(unsigned)ctx
->pos
,blkoff
,block
);
256 curr
= (ctx
->pos
- blkoff
) / sizeof(union afs_dirent
);
258 /* walk through the block, an entry at a time */
259 for (offset
= AFS_DIRENT_PER_BLOCK
- block
->pagehdr
.nentries
;
260 offset
< AFS_DIRENT_PER_BLOCK
;
265 /* skip entries marked unused in the bitmap */
266 if (!(block
->pagehdr
.bitmap
[offset
/ 8] &
267 (1 << (offset
% 8)))) {
268 _debug("ENT[%zu.%u]: unused",
269 blkoff
/ sizeof(union afs_dir_block
), offset
);
272 next
* sizeof(union afs_dirent
);
276 /* got a valid entry */
277 dire
= &block
->dirents
[offset
];
278 nlen
= strnlen(dire
->u
.name
,
280 offset
* sizeof(union afs_dirent
));
282 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
283 blkoff
/ sizeof(union afs_dir_block
), offset
,
284 (offset
< curr
? "skip" : "fill"),
287 /* work out where the next possible entry is */
288 for (tmp
= nlen
; tmp
> 15; tmp
-= sizeof(union afs_dirent
)) {
289 if (next
>= AFS_DIRENT_PER_BLOCK
) {
290 _debug("ENT[%zu.%u]:"
291 " %u travelled beyond end dir block"
293 blkoff
/ sizeof(union afs_dir_block
),
294 offset
, next
, tmp
, nlen
);
297 if (!(block
->pagehdr
.bitmap
[next
/ 8] &
298 (1 << (next
% 8)))) {
299 _debug("ENT[%zu.%u]:"
300 " %u unmarked extension (len %u/%zu)",
301 blkoff
/ sizeof(union afs_dir_block
),
302 offset
, next
, tmp
, nlen
);
306 _debug("ENT[%zu.%u]: ext %u/%zu",
307 blkoff
/ sizeof(union afs_dir_block
),
312 /* skip if starts before the current position */
316 /* found the next entry */
317 if (!dir_emit(ctx
, dire
->u
.name
, nlen
,
318 ntohl(dire
->u
.vnode
),
319 ctx
->actor
== afs_lookup_filldir
?
320 ntohl(dire
->u
.unique
) : DT_UNKNOWN
)) {
321 _leave(" = 0 [full]");
325 ctx
->pos
= blkoff
+ next
* sizeof(union afs_dirent
);
328 _leave(" = 1 [more]");
333 * iterate through the data blob that lists the contents of an AFS directory
335 static int afs_dir_iterate(struct inode
*dir
, struct dir_context
*ctx
,
338 union afs_dir_block
*dblock
;
339 struct afs_dir_page
*dbuf
;
341 unsigned blkoff
, limit
;
344 _enter("{%lu},%u,,", dir
->i_ino
, (unsigned)ctx
->pos
);
346 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(dir
)->flags
)) {
347 _leave(" = -ESTALE");
351 /* round the file position up to the next entry boundary */
352 ctx
->pos
+= sizeof(union afs_dirent
) - 1;
353 ctx
->pos
&= ~(sizeof(union afs_dirent
) - 1);
355 /* walk through the blocks in sequence */
357 while (ctx
->pos
< dir
->i_size
) {
358 blkoff
= ctx
->pos
& ~(sizeof(union afs_dir_block
) - 1);
360 /* fetch the appropriate page from the directory */
361 page
= afs_dir_get_page(dir
, blkoff
/ PAGE_SIZE
, key
);
367 limit
= blkoff
& ~(PAGE_SIZE
- 1);
369 dbuf
= page_address(page
);
371 /* deal with the individual blocks stashed on this page */
373 dblock
= &dbuf
->blocks
[(blkoff
% PAGE_SIZE
) /
374 sizeof(union afs_dir_block
)];
375 ret
= afs_dir_iterate_block(ctx
, dblock
, blkoff
);
377 afs_dir_put_page(page
);
381 blkoff
+= sizeof(union afs_dir_block
);
383 } while (ctx
->pos
< dir
->i_size
&& blkoff
< limit
);
385 afs_dir_put_page(page
);
390 _leave(" = %d", ret
);
395 * read an AFS directory
397 static int afs_readdir(struct file
*file
, struct dir_context
*ctx
)
399 return afs_dir_iterate(file_inode(file
), ctx
, afs_file_key(file
));
403 * search the directory for a name
404 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
405 * uniquifier through dtype
407 static int afs_lookup_filldir(struct dir_context
*ctx
, const char *name
,
408 int nlen
, loff_t fpos
, u64 ino
, unsigned dtype
)
410 struct afs_lookup_cookie
*cookie
=
411 container_of(ctx
, struct afs_lookup_cookie
, ctx
);
413 _enter("{%s,%u},%s,%u,,%llu,%u",
414 cookie
->name
.name
, cookie
->name
.len
, name
, nlen
,
415 (unsigned long long) ino
, dtype
);
417 /* insanity checks first */
418 BUILD_BUG_ON(sizeof(union afs_dir_block
) != 2048);
419 BUILD_BUG_ON(sizeof(union afs_dirent
) != 32);
421 if (cookie
->name
.len
!= nlen
||
422 memcmp(cookie
->name
.name
, name
, nlen
) != 0) {
427 cookie
->fid
.vnode
= ino
;
428 cookie
->fid
.unique
= dtype
;
431 _leave(" = -1 [found]");
436 * do a lookup in a directory
437 * - just returns the FID the dentry name maps to if found
439 static int afs_do_lookup(struct inode
*dir
, struct dentry
*dentry
,
440 struct afs_fid
*fid
, struct key
*key
)
442 struct afs_super_info
*as
= dir
->i_sb
->s_fs_info
;
443 struct afs_lookup_cookie cookie
= {
444 .ctx
.actor
= afs_lookup_filldir
,
445 .name
= dentry
->d_name
,
446 .fid
.vid
= as
->volume
->vid
450 _enter("{%lu},%p{%pd},", dir
->i_ino
, dentry
, dentry
);
452 /* search the directory */
453 ret
= afs_dir_iterate(dir
, &cookie
.ctx
, key
);
455 _leave(" = %d [iter]", ret
);
461 _leave(" = -ENOENT [not found]");
466 _leave(" = 0 { vn=%u u=%u }", fid
->vnode
, fid
->unique
);
471 * Try to auto mount the mountpoint with pseudo directory, if the autocell
472 * operation is setted.
474 static struct inode
*afs_try_auto_mntpt(
475 int ret
, struct dentry
*dentry
, struct inode
*dir
, struct key
*key
,
478 const char *devname
= dentry
->d_name
.name
;
479 struct afs_vnode
*vnode
= AFS_FS_I(dir
);
482 _enter("%d, %p{%pd}, {%x:%u}, %p",
483 ret
, dentry
, dentry
, vnode
->fid
.vid
, vnode
->fid
.vnode
, key
);
485 if (ret
!= -ENOENT
||
486 !test_bit(AFS_VNODE_AUTOCELL
, &vnode
->flags
))
489 inode
= afs_iget_autocell(dir
, devname
, strlen(devname
), key
);
491 ret
= PTR_ERR(inode
);
495 *fid
= AFS_FS_I(inode
)->fid
;
496 _leave("= %p", inode
);
505 * look up an entry in a directory
507 static struct dentry
*afs_lookup(struct inode
*dir
, struct dentry
*dentry
,
510 struct afs_vnode
*vnode
;
516 vnode
= AFS_FS_I(dir
);
518 _enter("{%x:%u},%p{%pd},",
519 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
, dentry
);
521 ASSERTCMP(d_inode(dentry
), ==, NULL
);
523 if (dentry
->d_name
.len
>= AFSNAMEMAX
) {
524 _leave(" = -ENAMETOOLONG");
525 return ERR_PTR(-ENAMETOOLONG
);
528 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
529 _leave(" = -ESTALE");
530 return ERR_PTR(-ESTALE
);
533 key
= afs_request_key(vnode
->volume
->cell
);
535 _leave(" = %ld [key]", PTR_ERR(key
));
536 return ERR_CAST(key
);
539 ret
= afs_validate(vnode
, key
);
542 _leave(" = %d [val]", ret
);
546 ret
= afs_do_lookup(dir
, dentry
, &fid
, key
);
548 inode
= afs_try_auto_mntpt(ret
, dentry
, dir
, key
, &fid
);
549 if (!IS_ERR(inode
)) {
554 ret
= PTR_ERR(inode
);
556 if (ret
== -ENOENT
) {
558 _leave(" = NULL [negative]");
561 _leave(" = %d [do]", ret
);
564 dentry
->d_fsdata
= (void *)(unsigned long) vnode
->status
.data_version
;
566 /* instantiate the dentry */
567 inode
= afs_iget(dir
->i_sb
, key
, &fid
, NULL
, NULL
, NULL
);
570 _leave(" = %ld", PTR_ERR(inode
));
571 return ERR_CAST(inode
);
575 d_add(dentry
, inode
);
576 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%u }",
579 d_inode(dentry
)->i_ino
,
580 d_inode(dentry
)->i_generation
);
586 * check that a dentry lookup hit has found a valid entry
587 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
590 static int afs_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
592 struct afs_vnode
*vnode
, *dir
;
593 struct afs_fid
uninitialized_var(fid
);
594 struct dentry
*parent
;
600 if (flags
& LOOKUP_RCU
)
603 if (d_really_is_positive(dentry
)) {
604 vnode
= AFS_FS_I(d_inode(dentry
));
605 _enter("{v={%x:%u} n=%pd fl=%lx},",
606 vnode
->fid
.vid
, vnode
->fid
.vnode
, dentry
,
609 _enter("{neg n=%pd}", dentry
);
612 key
= afs_request_key(AFS_FS_S(dentry
->d_sb
)->volume
->cell
);
616 if (d_really_is_positive(dentry
)) {
617 inode
= d_inode(dentry
);
619 vnode
= AFS_FS_I(inode
);
620 afs_validate(vnode
, key
);
621 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
626 /* lock down the parent dentry so we can peer at it */
627 parent
= dget_parent(dentry
);
628 dir
= AFS_FS_I(d_inode(parent
));
630 /* validate the parent directory */
631 afs_validate(dir
, key
);
633 if (test_bit(AFS_VNODE_DELETED
, &dir
->flags
)) {
634 _debug("%pd: parent dir deleted", dentry
);
638 dir_version
= (void *) (unsigned long) dir
->status
.data_version
;
639 if (dentry
->d_fsdata
== dir_version
)
640 goto out_valid
; /* the dir contents are unchanged */
642 _debug("dir modified");
644 /* search the directory for this vnode */
645 ret
= afs_do_lookup(&dir
->vfs_inode
, dentry
, &fid
, key
);
648 /* the filename maps to something */
649 if (d_really_is_negative(dentry
))
651 inode
= d_inode(dentry
);
652 if (is_bad_inode(inode
)) {
653 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
658 vnode
= AFS_FS_I(inode
);
660 /* if the vnode ID has changed, then the dirent points to a
662 if (fid
.vnode
!= vnode
->fid
.vnode
) {
663 _debug("%pd: dirent changed [%u != %u]",
669 /* if the vnode ID uniqifier has changed, then the file has
670 * been deleted and replaced, and the original vnode ID has
672 if (fid
.unique
!= vnode
->fid
.unique
) {
673 _debug("%pd: file deleted (uq %u -> %u I:%u)",
676 vnode
->vfs_inode
.i_generation
);
677 write_seqlock(&vnode
->cb_lock
);
678 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
679 write_sequnlock(&vnode
->cb_lock
);
685 /* the filename is unknown */
686 _debug("%pd: dirent not found", dentry
);
687 if (d_really_is_positive(dentry
))
692 _debug("failed to iterate dir %pd: %d",
698 dentry
->d_fsdata
= dir_version
;
701 _leave(" = 1 [valid]");
704 /* the dirent, if it exists, now points to a different vnode */
706 spin_lock(&dentry
->d_lock
);
707 dentry
->d_flags
|= DCACHE_NFSFS_RENAMED
;
708 spin_unlock(&dentry
->d_lock
);
711 _debug("dropping dentry %pd2", dentry
);
716 _leave(" = 0 [bad]");
721 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
723 * - called from dput() when d_count is going to 0.
724 * - return 1 to request dentry be unhashed, 0 otherwise
726 static int afs_d_delete(const struct dentry
*dentry
)
728 _enter("%pd", dentry
);
730 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
733 if (d_really_is_positive(dentry
) &&
734 (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(d_inode(dentry
))->flags
) ||
735 test_bit(AFS_VNODE_PSEUDODIR
, &AFS_FS_I(d_inode(dentry
))->flags
)))
738 _leave(" = 0 [keep]");
742 _leave(" = 1 [zap]");
747 * handle dentry release
749 static void afs_d_release(struct dentry
*dentry
)
751 _enter("%pd", dentry
);
755 * Create a new inode for create/mkdir/symlink
757 static void afs_vnode_new_inode(struct afs_fs_cursor
*fc
,
758 struct dentry
*new_dentry
,
759 struct afs_fid
*newfid
,
760 struct afs_file_status
*newstatus
,
761 struct afs_callback
*newcb
)
765 if (fc
->ac
.error
< 0)
768 inode
= afs_iget(fc
->vnode
->vfs_inode
.i_sb
, fc
->key
,
769 newfid
, newstatus
, newcb
, fc
->cbi
);
771 /* ENOMEM or EINTR at a really inconvenient time - just abandon
772 * the new directory on the server.
774 fc
->ac
.error
= PTR_ERR(inode
);
778 d_instantiate(new_dentry
, inode
);
779 if (d_unhashed(new_dentry
))
780 d_rehash(new_dentry
);
784 * create a directory on an AFS filesystem
786 static int afs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
788 struct afs_file_status newstatus
;
789 struct afs_fs_cursor fc
;
790 struct afs_callback newcb
;
791 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
792 struct afs_fid newfid
;
798 _enter("{%x:%u},{%pd},%ho",
799 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
, mode
);
801 key
= afs_request_key(dvnode
->volume
->cell
);
808 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
809 while (afs_select_fileserver(&fc
)) {
810 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
811 afs_fs_create(&fc
, dentry
->d_name
.name
, mode
,
812 &newfid
, &newstatus
, &newcb
);
815 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
816 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
817 afs_vnode_new_inode(&fc
, dentry
, &newfid
, &newstatus
, &newcb
);
818 ret
= afs_end_vnode_operation(&fc
);
831 _leave(" = %d", ret
);
836 * Remove a subdir from a directory.
838 static void afs_dir_remove_subdir(struct dentry
*dentry
)
840 if (d_really_is_positive(dentry
)) {
841 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
843 clear_nlink(&vnode
->vfs_inode
);
844 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
845 clear_bit(AFS_VNODE_CB_PROMISED
, &vnode
->flags
);
850 * remove a directory from an AFS filesystem
852 static int afs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
854 struct afs_fs_cursor fc
;
855 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
859 _enter("{%x:%u},{%pd}",
860 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
);
862 key
= afs_request_key(dvnode
->volume
->cell
);
869 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
870 while (afs_select_fileserver(&fc
)) {
871 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
872 afs_fs_remove(&fc
, dentry
->d_name
.name
, true);
875 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
876 ret
= afs_end_vnode_operation(&fc
);
878 afs_dir_remove_subdir(dentry
);
887 * Remove a link to a file or symlink from a directory.
889 * If the file was not deleted due to excess hard links, the fileserver will
890 * break the callback promise on the file - if it had one - before it returns
891 * to us, and if it was deleted, it won't
893 * However, if we didn't have a callback promise outstanding, or it was
894 * outstanding on a different server, then it won't break it either...
896 static int afs_dir_remove_link(struct dentry
*dentry
, struct key
*key
)
900 if (d_really_is_positive(dentry
)) {
901 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
903 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
))
904 kdebug("AFS_VNODE_DELETED");
905 clear_bit(AFS_VNODE_CB_PROMISED
, &vnode
->flags
);
907 ret
= afs_validate(vnode
, key
);
910 _debug("nlink %d [val %d]", vnode
->vfs_inode
.i_nlink
, ret
);
917 * Remove a file or symlink from an AFS filesystem.
919 static int afs_unlink(struct inode
*dir
, struct dentry
*dentry
)
921 struct afs_fs_cursor fc
;
922 struct afs_vnode
*dvnode
= AFS_FS_I(dir
), *vnode
;
926 _enter("{%x:%u},{%pd}",
927 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
);
929 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
930 return -ENAMETOOLONG
;
932 key
= afs_request_key(dvnode
->volume
->cell
);
938 /* Try to make sure we have a callback promise on the victim. */
939 if (d_really_is_positive(dentry
)) {
940 vnode
= AFS_FS_I(d_inode(dentry
));
941 ret
= afs_validate(vnode
, key
);
947 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
948 while (afs_select_fileserver(&fc
)) {
949 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
950 afs_fs_remove(&fc
, dentry
->d_name
.name
, false);
953 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
954 ret
= afs_end_vnode_operation(&fc
);
956 ret
= afs_dir_remove_link(dentry
, key
);
962 _leave(" = %d", ret
);
967 * create a regular file on an AFS filesystem
969 static int afs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
972 struct afs_fs_cursor fc
;
973 struct afs_file_status newstatus
;
974 struct afs_callback newcb
;
975 struct afs_vnode
*dvnode
= dvnode
= AFS_FS_I(dir
);
976 struct afs_fid newfid
;
982 _enter("{%x:%u},{%pd},%ho,",
983 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
, mode
);
986 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
989 key
= afs_request_key(dvnode
->volume
->cell
);
996 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
997 while (afs_select_fileserver(&fc
)) {
998 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
999 afs_fs_create(&fc
, dentry
->d_name
.name
, mode
,
1000 &newfid
, &newstatus
, &newcb
);
1003 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
1004 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
1005 afs_vnode_new_inode(&fc
, dentry
, &newfid
, &newstatus
, &newcb
);
1006 ret
= afs_end_vnode_operation(&fc
);
1019 _leave(" = %d", ret
);
1024 * create a hard link between files in an AFS filesystem
1026 static int afs_link(struct dentry
*from
, struct inode
*dir
,
1027 struct dentry
*dentry
)
1029 struct afs_fs_cursor fc
;
1030 struct afs_vnode
*dvnode
, *vnode
;
1034 vnode
= AFS_FS_I(d_inode(from
));
1035 dvnode
= AFS_FS_I(dir
);
1037 _enter("{%x:%u},{%x:%u},{%pd}",
1038 vnode
->fid
.vid
, vnode
->fid
.vnode
,
1039 dvnode
->fid
.vid
, dvnode
->fid
.vnode
,
1042 ret
= -ENAMETOOLONG
;
1043 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1046 key
= afs_request_key(dvnode
->volume
->cell
);
1053 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
1054 if (mutex_lock_interruptible_nested(&vnode
->io_lock
, 1) < 0) {
1055 afs_end_vnode_operation(&fc
);
1056 return -ERESTARTSYS
;
1059 while (afs_select_fileserver(&fc
)) {
1060 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
1061 fc
.cb_break_2
= vnode
->cb_break
+ vnode
->cb_s_break
;
1062 afs_fs_link(&fc
, vnode
, dentry
->d_name
.name
);
1065 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
1066 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break_2
);
1067 ihold(&vnode
->vfs_inode
);
1068 d_instantiate(dentry
, &vnode
->vfs_inode
);
1070 mutex_unlock(&vnode
->io_lock
);
1071 ret
= afs_end_vnode_operation(&fc
);
1084 _leave(" = %d", ret
);
1089 * create a symlink in an AFS filesystem
1091 static int afs_symlink(struct inode
*dir
, struct dentry
*dentry
,
1092 const char *content
)
1094 struct afs_fs_cursor fc
;
1095 struct afs_file_status newstatus
;
1096 struct afs_vnode
*dvnode
= AFS_FS_I(dir
);
1097 struct afs_fid newfid
;
1101 _enter("{%x:%u},{%pd},%s",
1102 dvnode
->fid
.vid
, dvnode
->fid
.vnode
, dentry
,
1105 ret
= -ENAMETOOLONG
;
1106 if (dentry
->d_name
.len
>= AFSNAMEMAX
)
1110 if (strlen(content
) >= AFSPATHMAX
)
1113 key
= afs_request_key(dvnode
->volume
->cell
);
1120 if (afs_begin_vnode_operation(&fc
, dvnode
, key
)) {
1121 while (afs_select_fileserver(&fc
)) {
1122 fc
.cb_break
= dvnode
->cb_break
+ dvnode
->cb_s_break
;
1123 afs_fs_symlink(&fc
, dentry
->d_name
.name
, content
,
1124 &newfid
, &newstatus
);
1127 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
1128 afs_vnode_commit_status(&fc
, dvnode
, fc
.cb_break
);
1129 afs_vnode_new_inode(&fc
, dentry
, &newfid
, &newstatus
, NULL
);
1130 ret
= afs_end_vnode_operation(&fc
);
1143 _leave(" = %d", ret
);
1148 * rename a file in an AFS filesystem and/or move it between directories
1150 static int afs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1151 struct inode
*new_dir
, struct dentry
*new_dentry
,
1154 struct afs_fs_cursor fc
;
1155 struct afs_vnode
*orig_dvnode
, *new_dvnode
, *vnode
;
1162 vnode
= AFS_FS_I(d_inode(old_dentry
));
1163 orig_dvnode
= AFS_FS_I(old_dir
);
1164 new_dvnode
= AFS_FS_I(new_dir
);
1166 _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
1167 orig_dvnode
->fid
.vid
, orig_dvnode
->fid
.vnode
,
1168 vnode
->fid
.vid
, vnode
->fid
.vnode
,
1169 new_dvnode
->fid
.vid
, new_dvnode
->fid
.vnode
,
1172 key
= afs_request_key(orig_dvnode
->volume
->cell
);
1179 if (afs_begin_vnode_operation(&fc
, orig_dvnode
, key
)) {
1180 if (orig_dvnode
!= new_dvnode
) {
1181 if (mutex_lock_interruptible_nested(&new_dvnode
->io_lock
, 1) < 0) {
1182 afs_end_vnode_operation(&fc
);
1183 return -ERESTARTSYS
;
1186 while (afs_select_fileserver(&fc
)) {
1187 fc
.cb_break
= orig_dvnode
->cb_break
+ orig_dvnode
->cb_s_break
;
1188 fc
.cb_break_2
= new_dvnode
->cb_break
+ new_dvnode
->cb_s_break
;
1189 afs_fs_rename(&fc
, old_dentry
->d_name
.name
,
1190 new_dvnode
, new_dentry
->d_name
.name
);
1193 afs_vnode_commit_status(&fc
, orig_dvnode
, fc
.cb_break
);
1194 afs_vnode_commit_status(&fc
, new_dvnode
, fc
.cb_break_2
);
1195 if (orig_dvnode
!= new_dvnode
)
1196 mutex_unlock(&new_dvnode
->io_lock
);
1197 ret
= afs_end_vnode_operation(&fc
);
1210 _leave(" = %d", ret
);