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>
18 #include <linux/smp_lock.h>
21 #include <rxrpc/call.h>
25 static struct dentry
*afs_dir_lookup(struct inode
*dir
, struct dentry
*dentry
,
26 struct nameidata
*nd
);
27 static int afs_dir_open(struct inode
*inode
, struct file
*file
);
28 static int afs_dir_readdir(struct file
*file
, void *dirent
, filldir_t filldir
);
29 static int afs_d_revalidate(struct dentry
*dentry
, struct nameidata
*nd
);
30 static int afs_d_delete(struct dentry
*dentry
);
31 static int afs_dir_lookup_filldir(void *_cookie
, const char *name
, int nlen
,
32 loff_t fpos
, u64 ino
, unsigned dtype
);
34 const struct file_operations afs_dir_file_operations
= {
36 .readdir
= afs_dir_readdir
,
39 const struct inode_operations afs_dir_inode_operations
= {
40 .lookup
= afs_dir_lookup
,
41 .getattr
= afs_inode_getattr
,
43 .create
= afs_dir_create
,
45 .unlink
= afs_dir_unlink
,
46 .symlink
= afs_dir_symlink
,
47 .mkdir
= afs_dir_mkdir
,
48 .rmdir
= afs_dir_rmdir
,
49 .mknod
= afs_dir_mknod
,
50 .rename
= afs_dir_rename
,
54 static struct dentry_operations afs_fs_dentry_operations
= {
55 .d_revalidate
= afs_d_revalidate
,
56 .d_delete
= afs_d_delete
,
59 #define AFS_DIR_HASHTBL_SIZE 128
60 #define AFS_DIR_DIRENT_SIZE 32
61 #define AFS_DIRENT_PER_BLOCK 64
71 uint8_t overflow
[4]; /* if any char of the name (inc
72 * NUL) reaches here, consume
73 * the next dirent too */
75 uint8_t extended_name
[32];
78 /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
79 struct afs_dir_pagehdr
{
82 #define AFS_DIR_MAGIC htons(1234)
88 /* directory block layout */
91 struct afs_dir_pagehdr pagehdr
;
94 struct afs_dir_pagehdr pagehdr
;
95 uint8_t alloc_ctrs
[128];
97 uint16_t hashtable
[AFS_DIR_HASHTBL_SIZE
];
100 union afs_dirent dirents
[AFS_DIRENT_PER_BLOCK
];
103 /* layout on a linux VM page */
104 struct afs_dir_page
{
105 union afs_dir_block blocks
[PAGE_SIZE
/ sizeof(union afs_dir_block
)];
108 struct afs_dir_lookup_cookie
{
116 * check that a directory page is valid
118 static inline void afs_dir_check_page(struct inode
*dir
, struct page
*page
)
120 struct afs_dir_page
*dbuf
;
125 /* check the page count */
126 qty
= desc
.size
/ sizeof(dbuf
->blocks
[0]);
130 if (page
->index
==0 && qty
!=ntohs(dbuf
->blocks
[0].pagehdr
.npages
)) {
131 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
132 __FUNCTION__
,dir
->i_ino
,qty
,ntohs(dbuf
->blocks
[0].pagehdr
.npages
));
137 /* determine how many magic numbers there should be in this page */
138 latter
= dir
->i_size
- page_offset(page
);
139 if (latter
>= PAGE_SIZE
)
143 qty
/= sizeof(union afs_dir_block
);
146 dbuf
= page_address(page
);
147 for (tmp
= 0; tmp
< qty
; tmp
++) {
148 if (dbuf
->blocks
[tmp
].pagehdr
.magic
!= AFS_DIR_MAGIC
) {
149 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
150 __FUNCTION__
, dir
->i_ino
, tmp
, qty
,
151 ntohs(dbuf
->blocks
[tmp
].pagehdr
.magic
));
156 SetPageChecked(page
);
160 SetPageChecked(page
);
165 * discard a page cached in the pagecache
167 static inline void afs_dir_put_page(struct page
*page
)
170 page_cache_release(page
);
174 * get a page into the pagecache
176 static struct page
*afs_dir_get_page(struct inode
*dir
, unsigned long index
)
180 _enter("{%lu},%lu", dir
->i_ino
, index
);
182 page
= read_mapping_page(dir
->i_mapping
, index
, NULL
);
184 wait_on_page_locked(page
);
186 if (!PageUptodate(page
))
188 if (!PageChecked(page
))
189 afs_dir_check_page(dir
, page
);
196 afs_dir_put_page(page
);
197 return ERR_PTR(-EIO
);
201 * open an AFS directory file
203 static int afs_dir_open(struct inode
*inode
, struct file
*file
)
205 _enter("{%lu}", inode
->i_ino
);
207 BUILD_BUG_ON(sizeof(union afs_dir_block
) != 2048);
208 BUILD_BUG_ON(sizeof(union afs_dirent
) != 32);
210 if (AFS_FS_I(inode
)->flags
& AFS_VNODE_DELETED
)
218 * deal with one block in an AFS directory
220 static int afs_dir_iterate_block(unsigned *fpos
,
221 union afs_dir_block
*block
,
226 union afs_dirent
*dire
;
227 unsigned offset
, next
, curr
;
231 _enter("%u,%x,%p,,",*fpos
,blkoff
,block
);
233 curr
= (*fpos
- blkoff
) / sizeof(union afs_dirent
);
235 /* walk through the block, an entry at a time */
236 for (offset
= AFS_DIRENT_PER_BLOCK
- block
->pagehdr
.nentries
;
237 offset
< AFS_DIRENT_PER_BLOCK
;
242 /* skip entries marked unused in the bitmap */
243 if (!(block
->pagehdr
.bitmap
[offset
/ 8] &
244 (1 << (offset
% 8)))) {
245 _debug("ENT[%Zu.%u]: unused\n",
246 blkoff
/ sizeof(union afs_dir_block
), offset
);
249 next
* sizeof(union afs_dirent
);
253 /* got a valid entry */
254 dire
= &block
->dirents
[offset
];
255 nlen
= strnlen(dire
->u
.name
,
257 offset
* sizeof(union afs_dirent
));
259 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"\n",
260 blkoff
/ sizeof(union afs_dir_block
), offset
,
261 (offset
< curr
? "skip" : "fill"),
264 /* work out where the next possible entry is */
265 for (tmp
= nlen
; tmp
> 15; tmp
-= sizeof(union afs_dirent
)) {
266 if (next
>= AFS_DIRENT_PER_BLOCK
) {
267 _debug("ENT[%Zu.%u]:"
268 " %u travelled beyond end dir block"
270 blkoff
/ sizeof(union afs_dir_block
),
271 offset
, next
, tmp
, nlen
);
274 if (!(block
->pagehdr
.bitmap
[next
/ 8] &
275 (1 << (next
% 8)))) {
276 _debug("ENT[%Zu.%u]:"
277 " %u unmarked extension (len %u/%Zu)\n",
278 blkoff
/ sizeof(union afs_dir_block
),
279 offset
, next
, tmp
, nlen
);
283 _debug("ENT[%Zu.%u]: ext %u/%Zu\n",
284 blkoff
/ sizeof(union afs_dir_block
),
289 /* skip if starts before the current position */
293 /* found the next entry */
294 ret
= filldir(cookie
,
297 blkoff
+ offset
* sizeof(union afs_dirent
),
298 ntohl(dire
->u
.vnode
),
299 filldir
== afs_dir_lookup_filldir
?
300 ntohl(dire
->u
.unique
) : DT_UNKNOWN
);
302 _leave(" = 0 [full]");
306 *fpos
= blkoff
+ next
* sizeof(union afs_dirent
);
309 _leave(" = 1 [more]");
314 * read an AFS directory
316 static int afs_dir_iterate(struct inode
*dir
, unsigned *fpos
, void *cookie
,
319 union afs_dir_block
*dblock
;
320 struct afs_dir_page
*dbuf
;
322 unsigned blkoff
, limit
;
325 _enter("{%lu},%u,,", dir
->i_ino
, *fpos
);
327 if (AFS_FS_I(dir
)->flags
& AFS_VNODE_DELETED
) {
328 _leave(" = -ESTALE");
332 /* round the file position up to the next entry boundary */
333 *fpos
+= sizeof(union afs_dirent
) - 1;
334 *fpos
&= ~(sizeof(union afs_dirent
) - 1);
336 /* walk through the blocks in sequence */
338 while (*fpos
< dir
->i_size
) {
339 blkoff
= *fpos
& ~(sizeof(union afs_dir_block
) - 1);
341 /* fetch the appropriate page from the directory */
342 page
= afs_dir_get_page(dir
, blkoff
/ PAGE_SIZE
);
348 limit
= blkoff
& ~(PAGE_SIZE
- 1);
350 dbuf
= page_address(page
);
352 /* deal with the individual blocks stashed on this page */
354 dblock
= &dbuf
->blocks
[(blkoff
% PAGE_SIZE
) /
355 sizeof(union afs_dir_block
)];
356 ret
= afs_dir_iterate_block(fpos
, dblock
, blkoff
,
359 afs_dir_put_page(page
);
363 blkoff
+= sizeof(union afs_dir_block
);
365 } while (*fpos
< dir
->i_size
&& blkoff
< limit
);
367 afs_dir_put_page(page
);
372 _leave(" = %d", ret
);
377 * read an AFS directory
379 static int afs_dir_readdir(struct file
*file
, void *cookie
, filldir_t filldir
)
384 _enter("{%Ld,{%lu}}", file
->f_pos
, file
->f_path
.dentry
->d_inode
->i_ino
);
387 ret
= afs_dir_iterate(file
->f_path
.dentry
->d_inode
, &fpos
, cookie
, filldir
);
390 _leave(" = %d", ret
);
395 * search the directory for a name
396 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
397 * uniquifier through dtype
399 static int afs_dir_lookup_filldir(void *_cookie
, const char *name
, int nlen
,
400 loff_t fpos
, u64 ino
, unsigned dtype
)
402 struct afs_dir_lookup_cookie
*cookie
= _cookie
;
404 _enter("{%s,%Zu},%s,%u,,%lu,%u",
405 cookie
->name
, cookie
->nlen
, name
, nlen
, ino
, dtype
);
407 if (cookie
->nlen
!= nlen
|| memcmp(cookie
->name
, name
, nlen
) != 0) {
412 cookie
->fid
.vnode
= ino
;
413 cookie
->fid
.unique
= dtype
;
416 _leave(" = -1 [found]");
421 * look up an entry in a directory
423 static struct dentry
*afs_dir_lookup(struct inode
*dir
, struct dentry
*dentry
,
424 struct nameidata
*nd
)
426 struct afs_dir_lookup_cookie cookie
;
427 struct afs_super_info
*as
;
428 struct afs_vnode
*vnode
;
433 _enter("{%lu},%p{%s}", dir
->i_ino
, dentry
, dentry
->d_name
.name
);
435 /* insanity checks first */
436 BUILD_BUG_ON(sizeof(union afs_dir_block
) != 2048);
437 BUILD_BUG_ON(sizeof(union afs_dirent
) != 32);
439 if (dentry
->d_name
.len
> 255) {
440 _leave(" = -ENAMETOOLONG");
441 return ERR_PTR(-ENAMETOOLONG
);
444 vnode
= AFS_FS_I(dir
);
445 if (vnode
->flags
& AFS_VNODE_DELETED
) {
446 _leave(" = -ESTALE");
447 return ERR_PTR(-ESTALE
);
450 as
= dir
->i_sb
->s_fs_info
;
452 /* search the directory */
453 cookie
.name
= dentry
->d_name
.name
;
454 cookie
.nlen
= dentry
->d_name
.len
;
455 cookie
.fid
.vid
= as
->volume
->vid
;
459 ret
= afs_dir_iterate(dir
, &fpos
, &cookie
, afs_dir_lookup_filldir
);
461 _leave(" = %d", ret
);
467 _leave(" = %d", ret
);
471 /* instantiate the dentry */
472 ret
= afs_iget(dir
->i_sb
, &cookie
.fid
, &inode
);
474 _leave(" = %d", ret
);
478 dentry
->d_op
= &afs_fs_dentry_operations
;
479 dentry
->d_fsdata
= (void *) (unsigned long) vnode
->status
.version
;
481 d_add(dentry
, inode
);
482 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
485 dentry
->d_inode
->i_ino
,
486 dentry
->d_inode
->i_version
);
492 * check that a dentry lookup hit has found a valid entry
493 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
495 * (derived from nfs_lookup_revalidate)
497 static int afs_d_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
499 struct afs_dir_lookup_cookie cookie
;
500 struct dentry
*parent
;
501 struct inode
*inode
, *dir
;
505 _enter("{sb=%p n=%s},", dentry
->d_sb
, dentry
->d_name
.name
);
507 /* lock down the parent dentry so we can peer at it */
508 parent
= dget_parent(dentry
->d_parent
);
510 dir
= parent
->d_inode
;
511 inode
= dentry
->d_inode
;
513 /* handle a negative dentry */
517 /* handle a bad inode */
518 if (is_bad_inode(inode
)) {
519 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
520 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
524 /* force a full look up if the parent directory changed since last the
525 * server was consulted
526 * - otherwise this inode must still exist, even if the inode details
527 * themselves have changed
529 if (AFS_FS_I(dir
)->flags
& AFS_VNODE_CHANGED
)
530 afs_vnode_fetch_status(AFS_FS_I(dir
));
532 if (AFS_FS_I(dir
)->flags
& AFS_VNODE_DELETED
) {
533 _debug("%s: parent dir deleted", dentry
->d_name
.name
);
537 if (AFS_FS_I(inode
)->flags
& AFS_VNODE_DELETED
) {
538 _debug("%s: file already deleted", dentry
->d_name
.name
);
542 if ((unsigned long) dentry
->d_fsdata
!=
543 (unsigned long) AFS_FS_I(dir
)->status
.version
) {
544 _debug("%s: parent changed %lu -> %u",
546 (unsigned long) dentry
->d_fsdata
,
547 (unsigned) AFS_FS_I(dir
)->status
.version
);
549 /* search the directory for this vnode */
550 cookie
.name
= dentry
->d_name
.name
;
551 cookie
.nlen
= dentry
->d_name
.len
;
552 cookie
.fid
.vid
= AFS_FS_I(inode
)->volume
->vid
;
556 ret
= afs_dir_iterate(dir
, &fpos
, &cookie
,
557 afs_dir_lookup_filldir
);
559 _debug("failed to iterate dir %s: %d",
560 parent
->d_name
.name
, ret
);
565 _debug("%s: dirent not found", dentry
->d_name
.name
);
569 /* if the vnode ID has changed, then the dirent points to a
571 if (cookie
.fid
.vnode
!= AFS_FS_I(inode
)->fid
.vnode
) {
572 _debug("%s: dirent changed", dentry
->d_name
.name
);
576 /* if the vnode ID uniqifier has changed, then the file has
578 if (cookie
.fid
.unique
!= AFS_FS_I(inode
)->fid
.unique
) {
579 _debug("%s: file deleted (uq %u -> %u I:%lu)",
582 AFS_FS_I(inode
)->fid
.unique
,
584 spin_lock(&AFS_FS_I(inode
)->lock
);
585 AFS_FS_I(inode
)->flags
|= AFS_VNODE_DELETED
;
586 spin_unlock(&AFS_FS_I(inode
)->lock
);
587 invalidate_remote_inode(inode
);
592 (void *) (unsigned long) AFS_FS_I(dir
)->status
.version
;
597 _leave(" = 1 [valid]");
600 /* the dirent, if it exists, now points to a different vnode */
602 spin_lock(&dentry
->d_lock
);
603 dentry
->d_flags
|= DCACHE_NFSFS_RENAMED
;
604 spin_unlock(&dentry
->d_lock
);
608 /* don't unhash if we have submounts */
609 if (have_submounts(dentry
))
613 shrink_dcache_parent(dentry
);
615 _debug("dropping dentry %s/%s",
616 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
);
621 _leave(" = 0 [bad]");
626 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
628 * - called from dput() when d_count is going to 0.
629 * - return 1 to request dentry be unhashed, 0 otherwise
631 static int afs_d_delete(struct dentry
*dentry
)
633 _enter("%s", dentry
->d_name
.name
);
635 if (dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
638 if (dentry
->d_inode
) {
639 if (AFS_FS_I(dentry
->d_inode
)->flags
& AFS_VNODE_DELETED
)
643 _leave(" = 0 [keep]");
647 _leave(" = 1 [zap]");