[AFS]: Clean up the AFS sources
[safe/jmp/linux-2.6] / fs / afs / dir.c
1 /* dir.c: AFS filesystem directory handling
2  *
3  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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.
10  */
11
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"
24
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);
33
34 const struct file_operations afs_dir_file_operations = {
35         .open           = afs_dir_open,
36         .readdir        = afs_dir_readdir,
37 };
38
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
52 };
53
54 static struct dentry_operations afs_fs_dentry_operations = {
55         .d_revalidate   = afs_d_revalidate,
56         .d_delete       = afs_d_delete,
57 };
58
59 #define AFS_DIR_HASHTBL_SIZE    128
60 #define AFS_DIR_DIRENT_SIZE     32
61 #define AFS_DIRENT_PER_BLOCK    64
62
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];
76 };
77
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];
86 };
87
88 /* directory block layout */
89 union afs_dir_block {
90
91         struct afs_dir_pagehdr pagehdr;
92
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;
99
100         union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
101 };
102
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)];
106 };
107
108 struct afs_dir_lookup_cookie {
109         struct afs_fid  fid;
110         const char      *name;
111         size_t          nlen;
112         int             found;
113 };
114
115 /*
116  * check that a directory page is valid
117  */
118 static inline void afs_dir_check_page(struct inode *dir, struct page *page)
119 {
120         struct afs_dir_page *dbuf;
121         loff_t latter;
122         int tmp, qty;
123
124 #if 0
125         /* check the page count */
126         qty = desc.size / sizeof(dbuf->blocks[0]);
127         if (qty == 0)
128                 goto error;
129
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;
134         }
135 #endif
136
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);
144
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;
153                 }
154         }
155
156         SetPageChecked(page);
157         return;
158
159 error:
160         SetPageChecked(page);
161         SetPageError(page);
162 }
163
164 /*
165  * discard a page cached in the pagecache
166  */
167 static inline void afs_dir_put_page(struct page *page)
168 {
169         kunmap(page);
170         page_cache_release(page);
171 }
172
173 /*
174  * get a page into the pagecache
175  */
176 static struct page *afs_dir_get_page(struct inode *dir, unsigned long index)
177 {
178         struct page *page;
179
180         _enter("{%lu},%lu", dir->i_ino, index);
181
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;
192         }
193         return page;
194
195 fail:
196         afs_dir_put_page(page);
197         return ERR_PTR(-EIO);
198 }
199
200 /*
201  * open an AFS directory file
202  */
203 static int afs_dir_open(struct inode *inode, struct file *file)
204 {
205         _enter("{%lu}", inode->i_ino);
206
207         BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
208         BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
209
210         if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED)
211                 return -ENOENT;
212
213         _leave(" = 0");
214         return 0;
215 }
216
217 /*
218  * deal with one block in an AFS directory
219  */
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)
225 {
226         union afs_dirent *dire;
227         unsigned offset, next, curr;
228         size_t nlen;
229         int tmp, ret;
230
231         _enter("%u,%x,%p,,",*fpos,blkoff,block);
232
233         curr = (*fpos - blkoff) / sizeof(union afs_dirent);
234
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
239              ) {
240                 next = offset + 1;
241
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;
251                 }
252
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));
258
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);
263
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;
273                         }
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;
281                         }
282
283                         _debug("ENT[%Zu.%u]: ext %u/%Zu\n",
284                                blkoff / sizeof(union afs_dir_block),
285                                next, tmp, nlen);
286                         next++;
287                 }
288
289                 /* skip if starts before the current position */
290                 if (offset < curr)
291                         continue;
292
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;
304                 }
305
306                 *fpos = blkoff + next * sizeof(union afs_dirent);
307         }
308
309         _leave(" = 1 [more]");
310         return 1;
311 }
312
313 /*
314  * read an AFS directory
315  */
316 static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
317                            filldir_t filldir)
318 {
319         union afs_dir_block     *dblock;
320         struct afs_dir_page *dbuf;
321         struct page *page;
322         unsigned blkoff, limit;
323         int ret;
324
325         _enter("{%lu},%u,,", dir->i_ino, *fpos);
326
327         if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
328                 _leave(" = -ESTALE");
329                 return -ESTALE;
330         }
331
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);
335
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);
340
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;
346                 }
347
348                 limit = blkoff & ~(PAGE_SIZE - 1);
349
350                 dbuf = page_address(page);
351
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;
361                         }
362
363                         blkoff += sizeof(union afs_dir_block);
364
365                 } while (*fpos < dir->i_size && blkoff < limit);
366
367                 afs_dir_put_page(page);
368                 ret = 0;
369         }
370
371 out:
372         _leave(" = %d", ret);
373         return ret;
374 }
375
376 /*
377  * read an AFS directory
378  */
379 static int afs_dir_readdir(struct file *file, void *cookie, filldir_t filldir)
380 {
381         unsigned fpos;
382         int ret;
383
384         _enter("{%Ld,{%lu}}", file->f_pos, file->f_path.dentry->d_inode->i_ino);
385
386         fpos = file->f_pos;
387         ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos, cookie, filldir);
388         file->f_pos = fpos;
389
390         _leave(" = %d", ret);
391         return ret;
392 }
393
394 /*
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
398  */
399 static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
400                                   loff_t fpos, u64 ino, unsigned dtype)
401 {
402         struct afs_dir_lookup_cookie *cookie = _cookie;
403
404         _enter("{%s,%Zu},%s,%u,,%lu,%u",
405                cookie->name, cookie->nlen, name, nlen, ino, dtype);
406
407         if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
408                 _leave(" = 0 [no]");
409                 return 0;
410         }
411
412         cookie->fid.vnode = ino;
413         cookie->fid.unique = dtype;
414         cookie->found = 1;
415
416         _leave(" = -1 [found]");
417         return -1;
418 }
419
420 /*
421  * look up an entry in a directory
422  */
423 static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
424                                      struct nameidata *nd)
425 {
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;
432
433         _enter("{%lu},%p{%s}", dir->i_ino, dentry, dentry->d_name.name);
434
435         /* insanity checks first */
436         BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
437         BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
438
439         if (dentry->d_name.len > 255) {
440                 _leave(" = -ENAMETOOLONG");
441                 return ERR_PTR(-ENAMETOOLONG);
442         }
443
444         vnode = AFS_FS_I(dir);
445         if (vnode->flags & AFS_VNODE_DELETED) {
446                 _leave(" = -ESTALE");
447                 return ERR_PTR(-ESTALE);
448         }
449
450         as = dir->i_sb->s_fs_info;
451
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;
457
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);
463         }
464
465         ret = -ENOENT;
466         if (!cookie.found) {
467                 _leave(" = %d", ret);
468                 return ERR_PTR(ret);
469         }
470
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);
476         }
477
478         dentry->d_op = &afs_fs_dentry_operations;
479         dentry->d_fsdata = (void *) (unsigned long) vnode->status.version;
480
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);
487
488         return NULL;
489 }
490
491 /*
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)
496  */
497 static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
498 {
499         struct afs_dir_lookup_cookie cookie;
500         struct dentry *parent;
501         struct inode *inode, *dir;
502         unsigned fpos;
503         int ret;
504
505         _enter("{sb=%p n=%s},", dentry->d_sb, dentry->d_name.name);
506
507         /* lock down the parent dentry so we can peer at it */
508         parent = dget_parent(dentry->d_parent);
509
510         dir = parent->d_inode;
511         inode = dentry->d_inode;
512
513         /* handle a negative dentry */
514         if (!inode)
515                 goto out_bad;
516
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;
522         }
523
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
528          */
529         if (AFS_FS_I(dir)->flags & AFS_VNODE_CHANGED)
530                 afs_vnode_fetch_status(AFS_FS_I(dir));
531
532         if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
533                 _debug("%s: parent dir deleted", dentry->d_name.name);
534                 goto out_bad;
535         }
536
537         if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED) {
538                 _debug("%s: file already deleted", dentry->d_name.name);
539                 goto out_bad;
540         }
541
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);
548
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;
554
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;
562                 }
563
564                 if (!cookie.found) {
565                         _debug("%s: dirent not found", dentry->d_name.name);
566                         goto not_found;
567                 }
568
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;
574                 }
575
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;
589                 }
590
591                 dentry->d_fsdata =
592                         (void *) (unsigned long) AFS_FS_I(dir)->status.version;
593         }
594
595 out_valid:
596         dput(parent);
597         _leave(" = 1 [valid]");
598         return 1;
599
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);
605
606 out_bad:
607         if (inode) {
608                 /* don't unhash if we have submounts */
609                 if (have_submounts(dentry))
610                         goto out_valid;
611         }
612
613         shrink_dcache_parent(dentry);
614
615         _debug("dropping dentry %s/%s",
616                dentry->d_parent->d_name.name, dentry->d_name.name);
617         d_drop(dentry);
618
619         dput(parent);
620
621         _leave(" = 0 [bad]");
622         return 0;
623 }
624
625 /*
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
630  */
631 static int afs_d_delete(struct dentry *dentry)
632 {
633         _enter("%s", dentry->d_name.name);
634
635         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
636                 goto zap;
637
638         if (dentry->d_inode) {
639                 if (AFS_FS_I(dentry->d_inode)->flags & AFS_VNODE_DELETED)
640                         goto zap;
641         }
642
643         _leave(" = 0 [keep]");
644         return 0;
645
646 zap:
647         _leave(" = 1 [zap]");
648         return 1;
649 }