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 {
115 /*****************************************************************************/
117 * check that a directory page is valid
119 static inline void afs_dir_check_page(struct inode *dir, struct page *page)
121 struct afs_dir_page *dbuf;
126 /* check the page count */
127 qty = desc.size / sizeof(dbuf->blocks[0]);
131 if (page->index==0 && qty!=ntohs(dbuf->blocks[0].pagehdr.npages)) {
132 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
133 __FUNCTION__,dir->i_ino,qty,ntohs(dbuf->blocks[0].pagehdr.npages));
138 /* determine how many magic numbers there should be in this page */
139 latter = dir->i_size - page_offset(page);
140 if (latter >= PAGE_SIZE)
144 qty /= sizeof(union afs_dir_block);
147 dbuf = page_address(page);
148 for (tmp = 0; tmp < qty; tmp++) {
149 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
150 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
151 __FUNCTION__, dir->i_ino, tmp, qty,
152 ntohs(dbuf->blocks[tmp].pagehdr.magic));
157 SetPageChecked(page);
161 SetPageChecked(page);
164 } /* end afs_dir_check_page() */
166 /*****************************************************************************/
168 * discard a page cached in the pagecache
170 static inline void afs_dir_put_page(struct page *page)
173 page_cache_release(page);
175 } /* end afs_dir_put_page() */
177 /*****************************************************************************/
179 * get a page into the pagecache
181 static struct page *afs_dir_get_page(struct inode *dir, unsigned long index)
185 _enter("{%lu},%lu", dir->i_ino, index);
187 page = read_mapping_page(dir->i_mapping, index, NULL);
189 wait_on_page_locked(page);
191 if (!PageUptodate(page))
193 if (!PageChecked(page))
194 afs_dir_check_page(dir, page);
201 afs_dir_put_page(page);
202 return ERR_PTR(-EIO);
203 } /* end afs_dir_get_page() */
205 /*****************************************************************************/
207 * open an AFS directory file
209 static int afs_dir_open(struct inode *inode, struct file *file)
211 _enter("{%lu}", inode->i_ino);
213 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
214 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
216 if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED)
222 } /* end afs_dir_open() */
224 /*****************************************************************************/
226 * deal with one block in an AFS directory
228 static int afs_dir_iterate_block(unsigned *fpos,
229 union afs_dir_block *block,
234 union afs_dirent *dire;
235 unsigned offset, next, curr;
239 _enter("%u,%x,%p,,",*fpos,blkoff,block);
241 curr = (*fpos - blkoff) / sizeof(union afs_dirent);
243 /* walk through the block, an entry at a time */
244 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
245 offset < AFS_DIRENT_PER_BLOCK;
250 /* skip entries marked unused in the bitmap */
251 if (!(block->pagehdr.bitmap[offset / 8] &
252 (1 << (offset % 8)))) {
253 _debug("ENT[%Zu.%u]: unused\n",
254 blkoff / sizeof(union afs_dir_block), offset);
257 next * sizeof(union afs_dirent);
261 /* got a valid entry */
262 dire = &block->dirents[offset];
263 nlen = strnlen(dire->u.name,
265 offset * sizeof(union afs_dirent));
267 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"\n",
268 blkoff / sizeof(union afs_dir_block), offset,
269 (offset < curr ? "skip" : "fill"),
272 /* work out where the next possible entry is */
273 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
274 if (next >= AFS_DIRENT_PER_BLOCK) {
275 _debug("ENT[%Zu.%u]:"
276 " %u travelled beyond end dir block"
278 blkoff / sizeof(union afs_dir_block),
279 offset, next, tmp, nlen);
282 if (!(block->pagehdr.bitmap[next / 8] &
283 (1 << (next % 8)))) {
284 _debug("ENT[%Zu.%u]:"
285 " %u unmarked extension (len %u/%Zu)\n",
286 blkoff / sizeof(union afs_dir_block),
287 offset, next, tmp, nlen);
291 _debug("ENT[%Zu.%u]: ext %u/%Zu\n",
292 blkoff / sizeof(union afs_dir_block),
297 /* skip if starts before the current position */
301 /* found the next entry */
302 ret = filldir(cookie,
305 blkoff + offset * sizeof(union afs_dirent),
306 ntohl(dire->u.vnode),
307 filldir == afs_dir_lookup_filldir ?
308 ntohl(dire->u.unique) : DT_UNKNOWN);
310 _leave(" = 0 [full]");
314 *fpos = blkoff + next * sizeof(union afs_dirent);
317 _leave(" = 1 [more]");
319 } /* end afs_dir_iterate_block() */
321 /*****************************************************************************/
323 * read an AFS directory
325 static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
328 union afs_dir_block *dblock;
329 struct afs_dir_page *dbuf;
331 unsigned blkoff, limit;
334 _enter("{%lu},%u,,", dir->i_ino, *fpos);
336 if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
337 _leave(" = -ESTALE");
341 /* round the file position up to the next entry boundary */
342 *fpos += sizeof(union afs_dirent) - 1;
343 *fpos &= ~(sizeof(union afs_dirent) - 1);
345 /* walk through the blocks in sequence */
347 while (*fpos < dir->i_size) {
348 blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
350 /* fetch the appropriate page from the directory */
351 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE);
357 limit = blkoff & ~(PAGE_SIZE - 1);
359 dbuf = page_address(page);
361 /* deal with the individual blocks stashed on this page */
363 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
364 sizeof(union afs_dir_block)];
365 ret = afs_dir_iterate_block(fpos, dblock, blkoff,
368 afs_dir_put_page(page);
372 blkoff += sizeof(union afs_dir_block);
374 } while (*fpos < dir->i_size && blkoff < limit);
376 afs_dir_put_page(page);
381 _leave(" = %d", ret);
383 } /* end afs_dir_iterate() */
385 /*****************************************************************************/
387 * read an AFS directory
389 static int afs_dir_readdir(struct file *file, void *cookie, filldir_t filldir)
394 _enter("{%Ld,{%lu}}", file->f_pos, file->f_path.dentry->d_inode->i_ino);
397 ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos, cookie, filldir);
400 _leave(" = %d", ret);
402 } /* end afs_dir_readdir() */
404 /*****************************************************************************/
406 * search the directory for a name
407 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
408 * uniquifier through dtype
410 static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
411 loff_t fpos, u64 ino, unsigned dtype)
413 struct afs_dir_lookup_cookie *cookie = _cookie;
415 _enter("{%s,%Zu},%s,%u,,%lu,%u",
416 cookie->name, cookie->nlen, name, nlen, ino, dtype);
418 if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
423 cookie->fid.vnode = ino;
424 cookie->fid.unique = dtype;
427 _leave(" = -1 [found]");
429 } /* end afs_dir_lookup_filldir() */
431 /*****************************************************************************/
433 * look up an entry in a directory
435 static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
436 struct nameidata *nd)
438 struct afs_dir_lookup_cookie cookie;
439 struct afs_super_info *as;
440 struct afs_vnode *vnode;
445 _enter("{%lu},%p{%s}", dir->i_ino, dentry, dentry->d_name.name);
447 /* insanity checks first */
448 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
449 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
451 if (dentry->d_name.len > 255) {
452 _leave(" = -ENAMETOOLONG");
453 return ERR_PTR(-ENAMETOOLONG);
456 vnode = AFS_FS_I(dir);
457 if (vnode->flags & AFS_VNODE_DELETED) {
458 _leave(" = -ESTALE");
459 return ERR_PTR(-ESTALE);
462 as = dir->i_sb->s_fs_info;
464 /* search the directory */
465 cookie.name = dentry->d_name.name;
466 cookie.nlen = dentry->d_name.len;
467 cookie.fid.vid = as->volume->vid;
471 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_dir_lookup_filldir);
473 _leave(" = %d", ret);
479 _leave(" = %d", ret);
483 /* instantiate the dentry */
484 ret = afs_iget(dir->i_sb, &cookie.fid, &inode);
486 _leave(" = %d", ret);
490 dentry->d_op = &afs_fs_dentry_operations;
491 dentry->d_fsdata = (void *) (unsigned long) vnode->status.version;
493 d_add(dentry, inode);
494 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
497 dentry->d_inode->i_ino,
498 dentry->d_inode->i_version);
501 } /* end afs_dir_lookup() */
503 /*****************************************************************************/
505 * check that a dentry lookup hit has found a valid entry
506 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
508 * (derived from nfs_lookup_revalidate)
510 static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
512 struct afs_dir_lookup_cookie cookie;
513 struct dentry *parent;
514 struct inode *inode, *dir;
518 _enter("{sb=%p n=%s},", dentry->d_sb, dentry->d_name.name);
520 /* lock down the parent dentry so we can peer at it */
521 parent = dget_parent(dentry->d_parent);
523 dir = parent->d_inode;
524 inode = dentry->d_inode;
526 /* handle a negative dentry */
530 /* handle a bad inode */
531 if (is_bad_inode(inode)) {
532 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
533 dentry->d_parent->d_name.name, dentry->d_name.name);
537 /* force a full look up if the parent directory changed since last the
538 * server was consulted
539 * - otherwise this inode must still exist, even if the inode details
540 * themselves have changed
542 if (AFS_FS_I(dir)->flags & AFS_VNODE_CHANGED)
543 afs_vnode_fetch_status(AFS_FS_I(dir));
545 if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
546 _debug("%s: parent dir deleted", dentry->d_name.name);
550 if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED) {
551 _debug("%s: file already deleted", dentry->d_name.name);
555 if ((unsigned long) dentry->d_fsdata !=
556 (unsigned long) AFS_FS_I(dir)->status.version) {
557 _debug("%s: parent changed %lu -> %u",
559 (unsigned long) dentry->d_fsdata,
560 (unsigned) AFS_FS_I(dir)->status.version);
562 /* search the directory for this vnode */
563 cookie.name = dentry->d_name.name;
564 cookie.nlen = dentry->d_name.len;
565 cookie.fid.vid = AFS_FS_I(inode)->volume->vid;
569 ret = afs_dir_iterate(dir, &fpos, &cookie,
570 afs_dir_lookup_filldir);
572 _debug("failed to iterate dir %s: %d",
573 parent->d_name.name, ret);
578 _debug("%s: dirent not found", dentry->d_name.name);
582 /* if the vnode ID has changed, then the dirent points to a
584 if (cookie.fid.vnode != AFS_FS_I(inode)->fid.vnode) {
585 _debug("%s: dirent changed", dentry->d_name.name);
589 /* if the vnode ID uniqifier has changed, then the file has
591 if (cookie.fid.unique != AFS_FS_I(inode)->fid.unique) {
592 _debug("%s: file deleted (uq %u -> %u I:%lu)",
595 AFS_FS_I(inode)->fid.unique,
597 spin_lock(&AFS_FS_I(inode)->lock);
598 AFS_FS_I(inode)->flags |= AFS_VNODE_DELETED;
599 spin_unlock(&AFS_FS_I(inode)->lock);
600 invalidate_remote_inode(inode);
605 (void *) (unsigned long) AFS_FS_I(dir)->status.version;
610 _leave(" = 1 [valid]");
613 /* the dirent, if it exists, now points to a different vnode */
615 spin_lock(&dentry->d_lock);
616 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
617 spin_unlock(&dentry->d_lock);
621 /* don't unhash if we have submounts */
622 if (have_submounts(dentry))
626 shrink_dcache_parent(dentry);
628 _debug("dropping dentry %s/%s",
629 dentry->d_parent->d_name.name, dentry->d_name.name);
634 _leave(" = 0 [bad]");
636 } /* end afs_d_revalidate() */
638 /*****************************************************************************/
640 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
642 * - called from dput() when d_count is going to 0.
643 * - return 1 to request dentry be unhashed, 0 otherwise
645 static int afs_d_delete(struct dentry *dentry)
647 _enter("%s", dentry->d_name.name);
649 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
652 if (dentry->d_inode) {
653 if (AFS_FS_I(dentry->d_inode)->flags & AFS_VNODE_DELETED)
657 _leave(" = 0 [keep]");
661 _leave(" = 1 [zap]");
663 } /* end afs_d_delete() */