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>
15 #include <linux/slab.h>
17 #include <linux/pagemap.h>
20 static struct dentry
*afs_dir_lookup(struct inode
*dir
, struct dentry
*dentry
,
21 struct nameidata
*nd
);
22 static int afs_dir_open(struct inode
*inode
, struct file
*file
);
23 static int afs_dir_readdir(struct file
*file
, void *dirent
, filldir_t filldir
);
24 static int afs_d_revalidate(struct dentry
*dentry
, struct nameidata
*nd
);
25 static int afs_d_delete(struct dentry
*dentry
);
26 static int afs_dir_lookup_filldir(void *_cookie
, const char *name
, int nlen
,
27 loff_t fpos
, u64 ino
, unsigned dtype
);
29 const struct file_operations afs_dir_file_operations
= {
31 .readdir
= afs_dir_readdir
,
34 const struct inode_operations afs_dir_inode_operations
= {
35 .lookup
= afs_dir_lookup
,
36 .getattr
= afs_inode_getattr
,
38 .create
= afs_dir_create
,
40 .unlink
= afs_dir_unlink
,
41 .symlink
= afs_dir_symlink
,
42 .mkdir
= afs_dir_mkdir
,
43 .rmdir
= afs_dir_rmdir
,
44 .mknod
= afs_dir_mknod
,
45 .rename
= afs_dir_rename
,
49 static struct dentry_operations afs_fs_dentry_operations
= {
50 .d_revalidate
= afs_d_revalidate
,
51 .d_delete
= afs_d_delete
,
54 #define AFS_DIR_HASHTBL_SIZE 128
55 #define AFS_DIR_DIRENT_SIZE 32
56 #define AFS_DIRENT_PER_BLOCK 64
66 uint8_t overflow
[4]; /* if any char of the name (inc
67 * NUL) reaches here, consume
68 * the next dirent too */
70 uint8_t extended_name
[32];
73 /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
74 struct afs_dir_pagehdr
{
77 #define AFS_DIR_MAGIC htons(1234)
83 /* directory block layout */
86 struct afs_dir_pagehdr pagehdr
;
89 struct afs_dir_pagehdr pagehdr
;
90 uint8_t alloc_ctrs
[128];
92 uint16_t hashtable
[AFS_DIR_HASHTBL_SIZE
];
95 union afs_dirent dirents
[AFS_DIRENT_PER_BLOCK
];
98 /* layout on a linux VM page */
100 union afs_dir_block blocks
[PAGE_SIZE
/ sizeof(union afs_dir_block
)];
103 struct afs_dir_lookup_cookie
{
111 * check that a directory page is valid
113 static inline void afs_dir_check_page(struct inode
*dir
, struct page
*page
)
115 struct afs_dir_page
*dbuf
;
120 /* check the page count */
121 qty
= desc
.size
/ sizeof(dbuf
->blocks
[0]);
125 if (page
->index
== 0 && qty
!= ntohs(dbuf
->blocks
[0].pagehdr
.npages
)) {
126 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
127 __FUNCTION__
, dir
->i_ino
, qty
,
128 ntohs(dbuf
->blocks
[0].pagehdr
.npages
));
133 /* determine how many magic numbers there should be in this page */
134 latter
= dir
->i_size
- page_offset(page
);
135 if (latter
>= PAGE_SIZE
)
139 qty
/= sizeof(union afs_dir_block
);
142 dbuf
= page_address(page
);
143 for (tmp
= 0; tmp
< qty
; tmp
++) {
144 if (dbuf
->blocks
[tmp
].pagehdr
.magic
!= AFS_DIR_MAGIC
) {
145 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
146 __FUNCTION__
, dir
->i_ino
, tmp
, qty
,
147 ntohs(dbuf
->blocks
[tmp
].pagehdr
.magic
));
152 SetPageChecked(page
);
156 SetPageChecked(page
);
161 * discard a page cached in the pagecache
163 static inline void afs_dir_put_page(struct page
*page
)
166 page_cache_release(page
);
170 * get a page into the pagecache
172 static struct page
*afs_dir_get_page(struct inode
*dir
, unsigned long index
)
176 _enter("{%lu},%lu", dir
->i_ino
, index
);
178 page
= read_mapping_page(dir
->i_mapping
, index
, NULL
);
180 wait_on_page_locked(page
);
182 if (!PageUptodate(page
))
184 if (!PageChecked(page
))
185 afs_dir_check_page(dir
, page
);
192 afs_dir_put_page(page
);
194 return ERR_PTR(-EIO
);
198 * open an AFS directory file
200 static int afs_dir_open(struct inode
*inode
, struct file
*file
)
202 _enter("{%lu}", inode
->i_ino
);
204 BUILD_BUG_ON(sizeof(union afs_dir_block
) != 2048);
205 BUILD_BUG_ON(sizeof(union afs_dirent
) != 32);
207 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(inode
)->flags
))
215 * deal with one block in an AFS directory
217 static int afs_dir_iterate_block(unsigned *fpos
,
218 union afs_dir_block
*block
,
223 union afs_dirent
*dire
;
224 unsigned offset
, next
, curr
;
228 _enter("%u,%x,%p,,",*fpos
,blkoff
,block
);
230 curr
= (*fpos
- blkoff
) / sizeof(union afs_dirent
);
232 /* walk through the block, an entry at a time */
233 for (offset
= AFS_DIRENT_PER_BLOCK
- block
->pagehdr
.nentries
;
234 offset
< AFS_DIRENT_PER_BLOCK
;
239 /* skip entries marked unused in the bitmap */
240 if (!(block
->pagehdr
.bitmap
[offset
/ 8] &
241 (1 << (offset
% 8)))) {
242 _debug("ENT[%Zu.%u]: unused",
243 blkoff
/ sizeof(union afs_dir_block
), offset
);
246 next
* sizeof(union afs_dirent
);
250 /* got a valid entry */
251 dire
= &block
->dirents
[offset
];
252 nlen
= strnlen(dire
->u
.name
,
254 offset
* sizeof(union afs_dirent
));
256 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
257 blkoff
/ sizeof(union afs_dir_block
), offset
,
258 (offset
< curr
? "skip" : "fill"),
261 /* work out where the next possible entry is */
262 for (tmp
= nlen
; tmp
> 15; tmp
-= sizeof(union afs_dirent
)) {
263 if (next
>= AFS_DIRENT_PER_BLOCK
) {
264 _debug("ENT[%Zu.%u]:"
265 " %u travelled beyond end dir block"
267 blkoff
/ sizeof(union afs_dir_block
),
268 offset
, next
, tmp
, nlen
);
271 if (!(block
->pagehdr
.bitmap
[next
/ 8] &
272 (1 << (next
% 8)))) {
273 _debug("ENT[%Zu.%u]:"
274 " %u unmarked extension (len %u/%Zu)",
275 blkoff
/ sizeof(union afs_dir_block
),
276 offset
, next
, tmp
, nlen
);
280 _debug("ENT[%Zu.%u]: ext %u/%Zu",
281 blkoff
/ sizeof(union afs_dir_block
),
286 /* skip if starts before the current position */
290 /* found the next entry */
291 ret
= filldir(cookie
,
294 blkoff
+ offset
* sizeof(union afs_dirent
),
295 ntohl(dire
->u
.vnode
),
296 filldir
== afs_dir_lookup_filldir
?
297 ntohl(dire
->u
.unique
) : DT_UNKNOWN
);
299 _leave(" = 0 [full]");
303 *fpos
= blkoff
+ next
* sizeof(union afs_dirent
);
306 _leave(" = 1 [more]");
311 * iterate through the data blob that lists the contents of an AFS directory
313 static int afs_dir_iterate(struct inode
*dir
, unsigned *fpos
, void *cookie
,
316 union afs_dir_block
*dblock
;
317 struct afs_dir_page
*dbuf
;
319 unsigned blkoff
, limit
;
322 _enter("{%lu},%u,,", dir
->i_ino
, *fpos
);
324 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(dir
)->flags
)) {
325 _leave(" = -ESTALE");
329 /* round the file position up to the next entry boundary */
330 *fpos
+= sizeof(union afs_dirent
) - 1;
331 *fpos
&= ~(sizeof(union afs_dirent
) - 1);
333 /* walk through the blocks in sequence */
335 while (*fpos
< dir
->i_size
) {
336 blkoff
= *fpos
& ~(sizeof(union afs_dir_block
) - 1);
338 /* fetch the appropriate page from the directory */
339 page
= afs_dir_get_page(dir
, blkoff
/ PAGE_SIZE
);
345 limit
= blkoff
& ~(PAGE_SIZE
- 1);
347 dbuf
= page_address(page
);
349 /* deal with the individual blocks stashed on this page */
351 dblock
= &dbuf
->blocks
[(blkoff
% PAGE_SIZE
) /
352 sizeof(union afs_dir_block
)];
353 ret
= afs_dir_iterate_block(fpos
, dblock
, blkoff
,
356 afs_dir_put_page(page
);
360 blkoff
+= sizeof(union afs_dir_block
);
362 } while (*fpos
< dir
->i_size
&& blkoff
< limit
);
364 afs_dir_put_page(page
);
369 _leave(" = %d", ret
);
374 * read an AFS directory
376 static int afs_dir_readdir(struct file
*file
, void *cookie
, filldir_t filldir
)
381 _enter("{%Ld,{%lu}}",
382 file
->f_pos
, file
->f_path
.dentry
->d_inode
->i_ino
);
385 ret
= afs_dir_iterate(file
->f_path
.dentry
->d_inode
, &fpos
,
389 _leave(" = %d", ret
);
394 * search the directory for a name
395 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
396 * uniquifier through dtype
398 static int afs_dir_lookup_filldir(void *_cookie
, const char *name
, int nlen
,
399 loff_t fpos
, u64 ino
, unsigned dtype
)
401 struct afs_dir_lookup_cookie
*cookie
= _cookie
;
403 _enter("{%s,%Zu},%s,%u,,%llu,%u",
404 cookie
->name
, cookie
->nlen
, name
, nlen
, ino
, dtype
);
406 /* insanity checks first */
407 BUILD_BUG_ON(sizeof(union afs_dir_block
) != 2048);
408 BUILD_BUG_ON(sizeof(union afs_dirent
) != 32);
410 if (cookie
->nlen
!= nlen
|| memcmp(cookie
->name
, name
, nlen
) != 0) {
415 cookie
->fid
.vnode
= ino
;
416 cookie
->fid
.unique
= dtype
;
419 _leave(" = -1 [found]");
424 * do a lookup in a directory
426 static int afs_do_lookup(struct inode
*dir
, struct dentry
*dentry
,
429 struct afs_dir_lookup_cookie cookie
;
430 struct afs_super_info
*as
;
434 _enter("{%lu},%p{%s},", dir
->i_ino
, dentry
, dentry
->d_name
.name
);
436 as
= dir
->i_sb
->s_fs_info
;
438 /* search the directory */
439 cookie
.name
= dentry
->d_name
.name
;
440 cookie
.nlen
= dentry
->d_name
.len
;
441 cookie
.fid
.vid
= as
->volume
->vid
;
445 ret
= afs_dir_iterate(dir
, &fpos
, &cookie
, afs_dir_lookup_filldir
);
447 _leave(" = %d [iter]", ret
);
453 _leave(" = -ENOENT [not found]");
458 _leave(" = 0 { vn=%u u=%u }", fid
->vnode
, fid
->unique
);
463 * look up an entry in a directory
465 static struct dentry
*afs_dir_lookup(struct inode
*dir
, struct dentry
*dentry
,
466 struct nameidata
*nd
)
468 struct afs_vnode
*vnode
;
473 _enter("{%lu},%p{%s}", dir
->i_ino
, dentry
, dentry
->d_name
.name
);
475 if (dentry
->d_name
.len
> 255) {
476 _leave(" = -ENAMETOOLONG");
477 return ERR_PTR(-ENAMETOOLONG
);
480 vnode
= AFS_FS_I(dir
);
481 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
482 _leave(" = -ESTALE");
483 return ERR_PTR(-ESTALE
);
486 ret
= afs_do_lookup(dir
, dentry
, &fid
);
488 _leave(" = %d [do]", ret
);
492 /* instantiate the dentry */
493 inode
= afs_iget(dir
->i_sb
, &fid
);
495 _leave(" = %ld", PTR_ERR(inode
));
496 return ERR_PTR(PTR_ERR(inode
));
499 dentry
->d_op
= &afs_fs_dentry_operations
;
501 d_add(dentry
, inode
);
502 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
505 dentry
->d_inode
->i_ino
,
506 dentry
->d_inode
->i_version
);
512 * propagate changed and modified flags on a directory to all the children of
513 * that directory as they may indicate that the ACL on the dir has changed,
514 * potentially rendering the child inaccessible or that a file has been deleted
517 static void afs_propagate_dir_changes(struct dentry
*dir
)
519 struct dentry
*child
;
522 c
= test_bit(AFS_VNODE_CHANGED
, &AFS_FS_I(dir
->d_inode
)->flags
);
523 m
= test_bit(AFS_VNODE_MODIFIED
, &AFS_FS_I(dir
->d_inode
)->flags
);
525 _enter("{%d,%d}", c
, m
);
527 spin_lock(&dir
->d_lock
);
529 list_for_each_entry(child
, &dir
->d_subdirs
, d_u
.d_child
) {
530 if (child
->d_inode
) {
531 struct afs_vnode
*vnode
;
533 _debug("tag %s", child
->d_name
.name
);
534 vnode
= AFS_FS_I(child
->d_inode
);
536 set_bit(AFS_VNODE_DIR_CHANGED
, &vnode
->flags
);
538 set_bit(AFS_VNODE_DIR_MODIFIED
, &vnode
->flags
);
542 spin_unlock(&dir
->d_lock
);
546 * check that a dentry lookup hit has found a valid entry
547 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
549 * - there are several things we need to check
550 * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
552 * - parent dir metadata changed (security changes)
553 * - dentry data changed (write, truncate)
554 * - dentry metadata changed (security changes)
556 static int afs_d_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
558 struct afs_vnode
*vnode
;
560 struct dentry
*parent
;
561 struct inode
*inode
, *dir
;
564 vnode
= AFS_FS_I(dentry
->d_inode
);
566 _enter("{sb=%p n=%s fl=%lx},",
567 dentry
->d_sb
, dentry
->d_name
.name
, vnode
->flags
);
569 /* lock down the parent dentry so we can peer at it */
570 parent
= dget_parent(dentry
);
572 dir
= parent
->d_inode
;
573 inode
= dentry
->d_inode
;
575 /* handle a negative dentry */
579 /* handle a bad inode */
580 if (is_bad_inode(inode
)) {
581 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
582 parent
->d_name
.name
, dentry
->d_name
.name
);
586 /* check that this dirent still exists if the directory's contents were
588 if (test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(dir
)->flags
)) {
589 _debug("%s: parent dir deleted", dentry
->d_name
.name
);
593 if (test_and_clear_bit(AFS_VNODE_DIR_MODIFIED
, &vnode
->flags
)) {
594 /* rm/rmdir/rename may have occurred */
595 _debug("dir modified");
597 /* search the directory for this vnode */
598 ret
= afs_do_lookup(dir
, dentry
, &fid
);
599 if (ret
== -ENOENT
) {
600 _debug("%s: dirent not found", dentry
->d_name
.name
);
604 _debug("failed to iterate dir %s: %d",
605 parent
->d_name
.name
, ret
);
609 /* if the vnode ID has changed, then the dirent points to a
611 if (fid
.vnode
!= vnode
->fid
.vnode
) {
612 _debug("%s: dirent changed [%u != %u]",
613 dentry
->d_name
.name
, fid
.vnode
,
618 /* if the vnode ID uniqifier has changed, then the file has
620 if (fid
.unique
!= vnode
->fid
.unique
) {
621 _debug("%s: file deleted (uq %u -> %u I:%lu)",
622 dentry
->d_name
.name
, fid
.unique
,
623 vnode
->fid
.unique
, inode
->i_version
);
624 spin_lock(&vnode
->lock
);
625 set_bit(AFS_VNODE_DELETED
, &vnode
->flags
);
626 spin_unlock(&vnode
->lock
);
627 invalidate_remote_inode(inode
);
632 /* if the directory's metadata were changed then the security may be
633 * different and we may no longer have access */
634 mutex_lock(&vnode
->cb_broken_lock
);
636 if (test_and_clear_bit(AFS_VNODE_DIR_CHANGED
, &vnode
->flags
) ||
637 test_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
)) {
638 _debug("%s: changed", dentry
->d_name
.name
);
639 set_bit(AFS_VNODE_CB_BROKEN
, &vnode
->flags
);
640 if (afs_vnode_fetch_status(vnode
) < 0) {
641 mutex_unlock(&vnode
->cb_broken_lock
);
646 if (test_bit(AFS_VNODE_DELETED
, &vnode
->flags
)) {
647 _debug("%s: file already deleted", dentry
->d_name
.name
);
648 mutex_unlock(&vnode
->cb_broken_lock
);
652 /* if the vnode's data version number changed then its contents are
654 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA
, &vnode
->flags
)) {
656 invalidate_remote_inode(inode
);
659 if (S_ISDIR(inode
->i_mode
) &&
660 (test_bit(AFS_VNODE_CHANGED
, &vnode
->flags
) ||
661 test_bit(AFS_VNODE_MODIFIED
, &vnode
->flags
)))
662 afs_propagate_dir_changes(dentry
);
664 clear_bit(AFS_VNODE_CHANGED
, &vnode
->flags
);
665 clear_bit(AFS_VNODE_MODIFIED
, &vnode
->flags
);
666 mutex_unlock(&vnode
->cb_broken_lock
);
670 _leave(" = 1 [valid]");
673 /* the dirent, if it exists, now points to a different vnode */
675 spin_lock(&dentry
->d_lock
);
676 dentry
->d_flags
|= DCACHE_NFSFS_RENAMED
;
677 spin_unlock(&dentry
->d_lock
);
681 /* don't unhash if we have submounts */
682 if (have_submounts(dentry
))
686 _debug("dropping dentry %s/%s",
687 parent
->d_name
.name
, dentry
->d_name
.name
);
688 shrink_dcache_parent(dentry
);
692 _leave(" = 0 [bad]");
697 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
699 * - called from dput() when d_count is going to 0.
700 * - return 1 to request dentry be unhashed, 0 otherwise
702 static int afs_d_delete(struct dentry
*dentry
)
704 _enter("%s", dentry
->d_name
.name
);
706 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
709 if (dentry
->d_inode
&&
710 test_bit(AFS_VNODE_DELETED
, &AFS_FS_I(dentry
->d_inode
)->flags
))
713 _leave(" = 0 [keep]");
717 _leave(" = 1 [zap]");