[AFS]: Clean up the AFS sources
[linux-2.6/kvm.git] / fs / afs / dir.c
blob2f6d923764618bc9bce8c09a29b80b9cc24c9d43
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>
16 #include <linux/fs.h>
17 #include <linux/pagemap.h>
18 #include <linux/smp_lock.h>
19 #include "vnode.h"
20 #include "volume.h"
21 #include <rxrpc/call.h>
22 #include "super.h"
23 #include "internal.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 = {
35 .open = afs_dir_open,
36 .readdir = afs_dir_readdir,
39 const struct inode_operations afs_dir_inode_operations = {
40 .lookup = afs_dir_lookup,
41 .getattr = afs_inode_getattr,
42 #if 0 /* TODO */
43 .create = afs_dir_create,
44 .link = afs_dir_link,
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,
51 #endif
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
63 union afs_dirent {
64 struct {
65 uint8_t valid;
66 uint8_t unused[1];
67 __be16 hash_next;
68 __be32 vnode;
69 __be32 unique;
70 uint8_t name[16];
71 uint8_t overflow[4]; /* if any char of the name (inc
72 * NUL) reaches here, consume
73 * the next dirent too */
74 } u;
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 {
80 __be16 npages;
81 __be16 magic;
82 #define AFS_DIR_MAGIC htons(1234)
83 uint8_t nentries;
84 uint8_t bitmap[8];
85 uint8_t pad[19];
88 /* directory block layout */
89 union afs_dir_block {
91 struct afs_dir_pagehdr pagehdr;
93 struct {
94 struct afs_dir_pagehdr pagehdr;
95 uint8_t alloc_ctrs[128];
96 /* dir hash table */
97 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
98 } hdr;
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 {
109 struct afs_fid fid;
110 const char *name;
111 size_t nlen;
112 int found;
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;
121 loff_t latter;
122 int tmp, qty;
124 #if 0
125 /* check the page count */
126 qty = desc.size / sizeof(dbuf->blocks[0]);
127 if (qty == 0)
128 goto error;
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));
133 goto error;
135 #endif
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)
140 qty = PAGE_SIZE;
141 else
142 qty = latter;
143 qty /= sizeof(union afs_dir_block);
145 /* check them */
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));
152 goto error;
156 SetPageChecked(page);
157 return;
159 error:
160 SetPageChecked(page);
161 SetPageError(page);
165 * discard a page cached in the pagecache
167 static inline void afs_dir_put_page(struct page *page)
169 kunmap(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)
178 struct page *page;
180 _enter("{%lu},%lu", dir->i_ino, index);
182 page = read_mapping_page(dir->i_mapping, index, NULL);
183 if (!IS_ERR(page)) {
184 wait_on_page_locked(page);
185 kmap(page);
186 if (!PageUptodate(page))
187 goto fail;
188 if (!PageChecked(page))
189 afs_dir_check_page(dir, page);
190 if (PageError(page))
191 goto fail;
193 return page;
195 fail:
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)
211 return -ENOENT;
213 _leave(" = 0");
214 return 0;
218 * deal with one block in an AFS directory
220 static int afs_dir_iterate_block(unsigned *fpos,
221 union afs_dir_block *block,
222 unsigned blkoff,
223 void *cookie,
224 filldir_t filldir)
226 union afs_dirent *dire;
227 unsigned offset, next, curr;
228 size_t nlen;
229 int tmp, ret;
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;
238 offset = next
240 next = offset + 1;
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);
247 if (offset >= curr)
248 *fpos = blkoff +
249 next * sizeof(union afs_dirent);
250 continue;
253 /* got a valid entry */
254 dire = &block->dirents[offset];
255 nlen = strnlen(dire->u.name,
256 sizeof(*block) -
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"),
262 nlen, dire->u.name);
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"
269 " (len %u/%Zu)\n",
270 blkoff / sizeof(union afs_dir_block),
271 offset, next, tmp, nlen);
272 return -EIO;
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);
280 return -EIO;
283 _debug("ENT[%Zu.%u]: ext %u/%Zu\n",
284 blkoff / sizeof(union afs_dir_block),
285 next, tmp, nlen);
286 next++;
289 /* skip if starts before the current position */
290 if (offset < curr)
291 continue;
293 /* found the next entry */
294 ret = filldir(cookie,
295 dire->u.name,
296 nlen,
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);
301 if (ret < 0) {
302 _leave(" = 0 [full]");
303 return 0;
306 *fpos = blkoff + next * sizeof(union afs_dirent);
309 _leave(" = 1 [more]");
310 return 1;
314 * read an AFS directory
316 static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
317 filldir_t filldir)
319 union afs_dir_block *dblock;
320 struct afs_dir_page *dbuf;
321 struct page *page;
322 unsigned blkoff, limit;
323 int ret;
325 _enter("{%lu},%u,,", dir->i_ino, *fpos);
327 if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
328 _leave(" = -ESTALE");
329 return -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 */
337 ret = 0;
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);
343 if (IS_ERR(page)) {
344 ret = PTR_ERR(page);
345 break;
348 limit = blkoff & ~(PAGE_SIZE - 1);
350 dbuf = page_address(page);
352 /* deal with the individual blocks stashed on this page */
353 do {
354 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
355 sizeof(union afs_dir_block)];
356 ret = afs_dir_iterate_block(fpos, dblock, blkoff,
357 cookie, filldir);
358 if (ret != 1) {
359 afs_dir_put_page(page);
360 goto out;
363 blkoff += sizeof(union afs_dir_block);
365 } while (*fpos < dir->i_size && blkoff < limit);
367 afs_dir_put_page(page);
368 ret = 0;
371 out:
372 _leave(" = %d", ret);
373 return ret;
377 * read an AFS directory
379 static int afs_dir_readdir(struct file *file, void *cookie, filldir_t filldir)
381 unsigned fpos;
382 int ret;
384 _enter("{%Ld,{%lu}}", file->f_pos, file->f_path.dentry->d_inode->i_ino);
386 fpos = file->f_pos;
387 ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos, cookie, filldir);
388 file->f_pos = fpos;
390 _leave(" = %d", ret);
391 return 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) {
408 _leave(" = 0 [no]");
409 return 0;
412 cookie->fid.vnode = ino;
413 cookie->fid.unique = dtype;
414 cookie->found = 1;
416 _leave(" = -1 [found]");
417 return -1;
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;
429 struct inode *inode;
430 unsigned fpos;
431 int ret;
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;
456 cookie.found = 0;
458 fpos = 0;
459 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_dir_lookup_filldir);
460 if (ret < 0) {
461 _leave(" = %d", ret);
462 return ERR_PTR(ret);
465 ret = -ENOENT;
466 if (!cookie.found) {
467 _leave(" = %d", ret);
468 return ERR_PTR(ret);
471 /* instantiate the dentry */
472 ret = afs_iget(dir->i_sb, &cookie.fid, &inode);
473 if (ret < 0) {
474 _leave(" = %d", ret);
475 return ERR_PTR(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 }",
483 cookie.fid.vnode,
484 cookie.fid.unique,
485 dentry->d_inode->i_ino,
486 dentry->d_inode->i_version);
488 return NULL;
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
494 * inode
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;
502 unsigned fpos;
503 int ret;
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 */
514 if (!inode)
515 goto out_bad;
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);
521 goto out_bad;
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);
534 goto out_bad;
537 if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED) {
538 _debug("%s: file already deleted", dentry->d_name.name);
539 goto out_bad;
542 if ((unsigned long) dentry->d_fsdata !=
543 (unsigned long) AFS_FS_I(dir)->status.version) {
544 _debug("%s: parent changed %lu -> %u",
545 dentry->d_name.name,
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;
553 cookie.found = 0;
555 fpos = 0;
556 ret = afs_dir_iterate(dir, &fpos, &cookie,
557 afs_dir_lookup_filldir);
558 if (ret < 0) {
559 _debug("failed to iterate dir %s: %d",
560 parent->d_name.name, ret);
561 goto out_bad;
564 if (!cookie.found) {
565 _debug("%s: dirent not found", dentry->d_name.name);
566 goto not_found;
569 /* if the vnode ID has changed, then the dirent points to a
570 * different file */
571 if (cookie.fid.vnode != AFS_FS_I(inode)->fid.vnode) {
572 _debug("%s: dirent changed", dentry->d_name.name);
573 goto not_found;
576 /* if the vnode ID uniqifier has changed, then the file has
577 * been deleted */
578 if (cookie.fid.unique != AFS_FS_I(inode)->fid.unique) {
579 _debug("%s: file deleted (uq %u -> %u I:%lu)",
580 dentry->d_name.name,
581 cookie.fid.unique,
582 AFS_FS_I(inode)->fid.unique,
583 inode->i_version);
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);
588 goto out_bad;
591 dentry->d_fsdata =
592 (void *) (unsigned long) AFS_FS_I(dir)->status.version;
595 out_valid:
596 dput(parent);
597 _leave(" = 1 [valid]");
598 return 1;
600 /* the dirent, if it exists, now points to a different vnode */
601 not_found:
602 spin_lock(&dentry->d_lock);
603 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
604 spin_unlock(&dentry->d_lock);
606 out_bad:
607 if (inode) {
608 /* don't unhash if we have submounts */
609 if (have_submounts(dentry))
610 goto out_valid;
613 shrink_dcache_parent(dentry);
615 _debug("dropping dentry %s/%s",
616 dentry->d_parent->d_name.name, dentry->d_name.name);
617 d_drop(dentry);
619 dput(parent);
621 _leave(" = 0 [bad]");
622 return 0;
626 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
627 * sleep)
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)
636 goto zap;
638 if (dentry->d_inode) {
639 if (AFS_FS_I(dentry->d_inode)->flags & AFS_VNODE_DELETED)
640 goto zap;
643 _leave(" = 0 [keep]");
644 return 0;
646 zap:
647 _leave(" = 1 [zap]");
648 return 1;