Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / 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/sched.h>
16 #include <linux/slab.h>
17 #include <linux/fs.h>
18 #include <linux/pagemap.h>
19 #include <linux/smp_lock.h>
20 #include "vnode.h"
21 #include "volume.h"
22 #include <rxrpc/call.h>
23 #include "super.h"
24 #include "internal.h"
25
26 static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
27                                      struct nameidata *nd);
28 static int afs_dir_open(struct inode *inode, struct file *file);
29 static int afs_dir_readdir(struct file *file, void *dirent, filldir_t filldir);
30 static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
31 static int afs_d_delete(struct dentry *dentry);
32 static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
33                                   loff_t fpos, u64 ino, unsigned dtype);
34
35 const struct file_operations afs_dir_file_operations = {
36         .open           = afs_dir_open,
37         .readdir        = afs_dir_readdir,
38 };
39
40 struct inode_operations afs_dir_inode_operations = {
41         .lookup         = afs_dir_lookup,
42         .getattr        = afs_inode_getattr,
43 #if 0 /* TODO */
44         .create         = afs_dir_create,
45         .link           = afs_dir_link,
46         .unlink         = afs_dir_unlink,
47         .symlink        = afs_dir_symlink,
48         .mkdir          = afs_dir_mkdir,
49         .rmdir          = afs_dir_rmdir,
50         .mknod          = afs_dir_mknod,
51         .rename         = afs_dir_rename,
52 #endif
53 };
54
55 static struct dentry_operations afs_fs_dentry_operations = {
56         .d_revalidate   = afs_d_revalidate,
57         .d_delete       = afs_d_delete,
58 };
59
60 #define AFS_DIR_HASHTBL_SIZE    128
61 #define AFS_DIR_DIRENT_SIZE     32
62 #define AFS_DIRENT_PER_BLOCK    64
63
64 union afs_dirent {
65         struct {
66                 uint8_t         valid;
67                 uint8_t         unused[1];
68                 __be16          hash_next;
69                 __be32          vnode;
70                 __be32          unique;
71                 uint8_t         name[16];
72                 uint8_t         overflow[4];    /* if any char of the name (inc
73                                                  * NUL) reaches here, consume
74                                                  * the next dirent too */
75         } u;
76         uint8_t extended_name[32];
77 };
78
79 /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
80 struct afs_dir_pagehdr {
81         __be16          npages;
82         __be16          magic;
83 #define AFS_DIR_MAGIC htons(1234)
84         uint8_t         nentries;
85         uint8_t         bitmap[8];
86         uint8_t         pad[19];
87 };
88
89 /* directory block layout */
90 union afs_dir_block {
91
92         struct afs_dir_pagehdr pagehdr;
93
94         struct {
95                 struct afs_dir_pagehdr  pagehdr;
96                 uint8_t                 alloc_ctrs[128];
97                 /* dir hash table */
98                 uint16_t                hashtable[AFS_DIR_HASHTBL_SIZE];
99         } hdr;
100
101         union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
102 };
103
104 /* layout on a linux VM page */
105 struct afs_dir_page {
106         union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
107 };
108
109 struct afs_dir_lookup_cookie {
110         struct afs_fid  fid;
111         const char      *name;
112         size_t          nlen;
113         int             found;
114 };
115
116 /*****************************************************************************/
117 /*
118  * check that a directory page is valid
119  */
120 static inline void afs_dir_check_page(struct inode *dir, struct page *page)
121 {
122         struct afs_dir_page *dbuf;
123         loff_t latter;
124         int tmp, qty;
125
126 #if 0
127         /* check the page count */
128         qty = desc.size / sizeof(dbuf->blocks[0]);
129         if (qty == 0)
130                 goto error;
131
132         if (page->index==0 && qty!=ntohs(dbuf->blocks[0].pagehdr.npages)) {
133                 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
134                        __FUNCTION__,dir->i_ino,qty,ntohs(dbuf->blocks[0].pagehdr.npages));
135                 goto error;
136         }
137 #endif
138
139         /* determine how many magic numbers there should be in this page */
140         latter = dir->i_size - page_offset(page);
141         if (latter >= PAGE_SIZE)
142                 qty = PAGE_SIZE;
143         else
144                 qty = latter;
145         qty /= sizeof(union afs_dir_block);
146
147         /* check them */
148         dbuf = kmap_atomic(page, KM_USER0);
149         for (tmp = 0; tmp < qty; tmp++) {
150                 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
151                         printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
152                                __FUNCTION__, dir->i_ino, tmp, qty,
153                                ntohs(dbuf->blocks[tmp].pagehdr.magic));
154                         goto error;
155                 }
156         }
157         kunmap_atomic(dbuf, KM_USER0);
158
159         return;
160
161  error:
162         kunmap_atomic(dbuf, KM_USER0);
163         SetPageError(page);
164
165 } /* end afs_dir_check_page() */
166
167 /*****************************************************************************/
168 /*
169  * discard a page cached in the pagecache
170  */
171 static inline void afs_dir_put_page(struct page *page)
172 {
173         page_cache_release(page);
174
175 } /* end afs_dir_put_page() */
176
177 /*****************************************************************************/
178 /*
179  * get a page into the pagecache
180  */
181 static struct page *afs_dir_get_page(struct inode *dir, unsigned long index)
182 {
183         struct page *page;
184
185         _enter("{%lu},%lu", dir->i_ino, index);
186
187         page = read_mapping_page(dir->i_mapping, index, NULL);
188         if (!IS_ERR(page)) {
189                 wait_on_page_locked(page);
190                 if (!PageUptodate(page))
191                         goto fail;
192                 afs_dir_check_page(dir, page);
193                 if (PageError(page))
194                         goto fail;
195         }
196         return page;
197
198  fail:
199         afs_dir_put_page(page);
200         return ERR_PTR(-EIO);
201 } /* end afs_dir_get_page() */
202
203 /*****************************************************************************/
204 /*
205  * open an AFS directory file
206  */
207 static int afs_dir_open(struct inode *inode, struct file *file)
208 {
209         _enter("{%lu}", inode->i_ino);
210
211         BUG_ON(sizeof(union afs_dir_block) != 2048);
212         BUG_ON(sizeof(union afs_dirent) != 32);
213
214         if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED)
215                 return -ENOENT;
216
217         _leave(" = 0");
218         return 0;
219
220 } /* end afs_dir_open() */
221
222 /*****************************************************************************/
223 /*
224  * deal with one block in an AFS directory
225  */
226 static int afs_dir_iterate_block(unsigned *fpos,
227                                  union afs_dir_block *block,
228                                  unsigned blkoff,
229                                  void *cookie,
230                                  filldir_t filldir)
231 {
232         union afs_dirent *dire;
233         unsigned offset, next, curr;
234         size_t nlen;
235         int tmp, ret;
236
237         _enter("%u,%x,%p,,",*fpos,blkoff,block);
238
239         curr = (*fpos - blkoff) / sizeof(union afs_dirent);
240
241         /* walk through the block, an entry at a time */
242         for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
243              offset < AFS_DIRENT_PER_BLOCK;
244              offset = next
245              ) {
246                 next = offset + 1;
247
248                 /* skip entries marked unused in the bitmap */
249                 if (!(block->pagehdr.bitmap[offset / 8] &
250                       (1 << (offset % 8)))) {
251                         _debug("ENT[%Zu.%u]: unused\n",
252                                blkoff / sizeof(union afs_dir_block), offset);
253                         if (offset >= curr)
254                                 *fpos = blkoff +
255                                         next * sizeof(union afs_dirent);
256                         continue;
257                 }
258
259                 /* got a valid entry */
260                 dire = &block->dirents[offset];
261                 nlen = strnlen(dire->u.name,
262                                sizeof(*block) -
263                                offset * sizeof(union afs_dirent));
264
265                 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"\n",
266                        blkoff / sizeof(union afs_dir_block), offset,
267                        (offset < curr ? "skip" : "fill"),
268                        nlen, dire->u.name);
269
270                 /* work out where the next possible entry is */
271                 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
272                         if (next >= AFS_DIRENT_PER_BLOCK) {
273                                 _debug("ENT[%Zu.%u]:"
274                                        " %u travelled beyond end dir block"
275                                        " (len %u/%Zu)\n",
276                                        blkoff / sizeof(union afs_dir_block),
277                                        offset, next, tmp, nlen);
278                                 return -EIO;
279                         }
280                         if (!(block->pagehdr.bitmap[next / 8] &
281                               (1 << (next % 8)))) {
282                                 _debug("ENT[%Zu.%u]:"
283                                        " %u unmarked extension (len %u/%Zu)\n",
284                                        blkoff / sizeof(union afs_dir_block),
285                                        offset, next, tmp, nlen);
286                                 return -EIO;
287                         }
288
289                         _debug("ENT[%Zu.%u]: ext %u/%Zu\n",
290                                blkoff / sizeof(union afs_dir_block),
291                                next, tmp, nlen);
292                         next++;
293                 }
294
295                 /* skip if starts before the current position */
296                 if (offset < curr)
297                         continue;
298
299                 /* found the next entry */
300                 ret = filldir(cookie,
301                               dire->u.name,
302                               nlen,
303                               blkoff + offset * sizeof(union afs_dirent),
304                               ntohl(dire->u.vnode),
305                               filldir == afs_dir_lookup_filldir ?
306                               ntohl(dire->u.unique) : DT_UNKNOWN);
307                 if (ret < 0) {
308                         _leave(" = 0 [full]");
309                         return 0;
310                 }
311
312                 *fpos = blkoff + next * sizeof(union afs_dirent);
313         }
314
315         _leave(" = 1 [more]");
316         return 1;
317 } /* end afs_dir_iterate_block() */
318
319 /*****************************************************************************/
320 /*
321  * read an AFS directory
322  */
323 static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
324                            filldir_t filldir)
325 {
326         union afs_dir_block     *dblock;
327         struct afs_dir_page *dbuf;
328         struct page *page;
329         unsigned blkoff, limit;
330         int ret;
331
332         _enter("{%lu},%u,,", dir->i_ino, *fpos);
333
334         if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
335                 _leave(" = -ESTALE");
336                 return -ESTALE;
337         }
338
339         /* round the file position up to the next entry boundary */
340         *fpos += sizeof(union afs_dirent) - 1;
341         *fpos &= ~(sizeof(union afs_dirent) - 1);
342
343         /* walk through the blocks in sequence */
344         ret = 0;
345         while (*fpos < dir->i_size) {
346                 blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
347
348                 /* fetch the appropriate page from the directory */
349                 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE);
350                 if (IS_ERR(page)) {
351                         ret = PTR_ERR(page);
352                         break;
353                 }
354
355                 limit = blkoff & ~(PAGE_SIZE - 1);
356
357                 dbuf = kmap_atomic(page, KM_USER0);
358
359                 /* deal with the individual blocks stashed on this page */
360                 do {
361                         dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
362                                                sizeof(union afs_dir_block)];
363                         ret = afs_dir_iterate_block(fpos, dblock, blkoff,
364                                                     cookie, filldir);
365                         if (ret != 1) {
366                                 kunmap_atomic(dbuf, KM_USER0);
367                                 afs_dir_put_page(page);
368                                 goto out;
369                         }
370
371                         blkoff += sizeof(union afs_dir_block);
372
373                 } while (*fpos < dir->i_size && blkoff < limit);
374
375                 kunmap_atomic(dbuf, KM_USER0);
376                 afs_dir_put_page(page);
377                 ret = 0;
378         }
379
380  out:
381         _leave(" = %d", ret);
382         return ret;
383 } /* end afs_dir_iterate() */
384
385 /*****************************************************************************/
386 /*
387  * read an AFS directory
388  */
389 static int afs_dir_readdir(struct file *file, void *cookie, filldir_t filldir)
390 {
391         unsigned fpos;
392         int ret;
393
394         _enter("{%Ld,{%lu}}", file->f_pos, file->f_dentry->d_inode->i_ino);
395
396         fpos = file->f_pos;
397         ret = afs_dir_iterate(file->f_dentry->d_inode, &fpos, cookie, filldir);
398         file->f_pos = fpos;
399
400         _leave(" = %d", ret);
401         return ret;
402 } /* end afs_dir_readdir() */
403
404 /*****************************************************************************/
405 /*
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
409  */
410 static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
411                                   loff_t fpos, u64 ino, unsigned dtype)
412 {
413         struct afs_dir_lookup_cookie *cookie = _cookie;
414
415         _enter("{%s,%Zu},%s,%u,,%lu,%u",
416                cookie->name, cookie->nlen, name, nlen, ino, dtype);
417
418         if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
419                 _leave(" = 0 [no]");
420                 return 0;
421         }
422
423         cookie->fid.vnode = ino;
424         cookie->fid.unique = dtype;
425         cookie->found = 1;
426
427         _leave(" = -1 [found]");
428         return -1;
429 } /* end afs_dir_lookup_filldir() */
430
431 /*****************************************************************************/
432 /*
433  * look up an entry in a directory
434  */
435 static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
436                                      struct nameidata *nd)
437 {
438         struct afs_dir_lookup_cookie cookie;
439         struct afs_super_info *as;
440         struct afs_vnode *vnode;
441         struct inode *inode;
442         unsigned fpos;
443         int ret;
444
445         _enter("{%lu},%p{%s}", dir->i_ino, dentry, dentry->d_name.name);
446
447         /* insanity checks first */
448         BUG_ON(sizeof(union afs_dir_block) != 2048);
449         BUG_ON(sizeof(union afs_dirent) != 32);
450
451         if (dentry->d_name.len > 255) {
452                 _leave(" = -ENAMETOOLONG");
453                 return ERR_PTR(-ENAMETOOLONG);
454         }
455
456         vnode = AFS_FS_I(dir);
457         if (vnode->flags & AFS_VNODE_DELETED) {
458                 _leave(" = -ESTALE");
459                 return ERR_PTR(-ESTALE);
460         }
461
462         as = dir->i_sb->s_fs_info;
463
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;
468         cookie.found    = 0;
469
470         fpos = 0;
471         ret = afs_dir_iterate(dir, &fpos, &cookie, afs_dir_lookup_filldir);
472         if (ret < 0) {
473                 _leave(" = %d", ret);
474                 return ERR_PTR(ret);
475         }
476
477         ret = -ENOENT;
478         if (!cookie.found) {
479                 _leave(" = %d", ret);
480                 return ERR_PTR(ret);
481         }
482
483         /* instantiate the dentry */
484         ret = afs_iget(dir->i_sb, &cookie.fid, &inode);
485         if (ret < 0) {
486                 _leave(" = %d", ret);
487                 return ERR_PTR(ret);
488         }
489
490         dentry->d_op = &afs_fs_dentry_operations;
491         dentry->d_fsdata = (void *) (unsigned long) vnode->status.version;
492
493         d_add(dentry, inode);
494         _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
495                cookie.fid.vnode,
496                cookie.fid.unique,
497                dentry->d_inode->i_ino,
498                dentry->d_inode->i_version);
499
500         return NULL;
501 } /* end afs_dir_lookup() */
502
503 /*****************************************************************************/
504 /*
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
507  *   inode
508  * (derived from nfs_lookup_revalidate)
509  */
510 static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
511 {
512         struct afs_dir_lookup_cookie cookie;
513         struct dentry *parent;
514         struct inode *inode, *dir;
515         unsigned fpos;
516         int ret;
517
518         _enter("{sb=%p n=%s},", dentry->d_sb, dentry->d_name.name);
519
520         /* lock down the parent dentry so we can peer at it */
521         parent = dget_parent(dentry->d_parent);
522
523         dir = parent->d_inode;
524         inode = dentry->d_inode;
525
526         /* handle a negative dentry */
527         if (!inode)
528                 goto out_bad;
529
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);
534                 goto out_bad;
535         }
536
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
541          */
542         if (AFS_FS_I(dir)->flags & AFS_VNODE_CHANGED)
543                 afs_vnode_fetch_status(AFS_FS_I(dir));
544
545         if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
546                 _debug("%s: parent dir deleted", dentry->d_name.name);
547                 goto out_bad;
548         }
549
550         if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED) {
551                 _debug("%s: file already deleted", dentry->d_name.name);
552                 goto out_bad;
553         }
554
555         if ((unsigned long) dentry->d_fsdata !=
556             (unsigned long) AFS_FS_I(dir)->status.version) {
557                 _debug("%s: parent changed %lu -> %u",
558                        dentry->d_name.name,
559                        (unsigned long) dentry->d_fsdata,
560                        (unsigned) AFS_FS_I(dir)->status.version);
561
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;
566                 cookie.found    = 0;
567
568                 fpos = 0;
569                 ret = afs_dir_iterate(dir, &fpos, &cookie,
570                                       afs_dir_lookup_filldir);
571                 if (ret < 0) {
572                         _debug("failed to iterate dir %s: %d",
573                                parent->d_name.name, ret);
574                         goto out_bad;
575                 }
576
577                 if (!cookie.found) {
578                         _debug("%s: dirent not found", dentry->d_name.name);
579                         goto not_found;
580                 }
581
582                 /* if the vnode ID has changed, then the dirent points to a
583                  * different file */
584                 if (cookie.fid.vnode != AFS_FS_I(inode)->fid.vnode) {
585                         _debug("%s: dirent changed", dentry->d_name.name);
586                         goto not_found;
587                 }
588
589                 /* if the vnode ID uniqifier has changed, then the file has
590                  * been deleted */
591                 if (cookie.fid.unique != AFS_FS_I(inode)->fid.unique) {
592                         _debug("%s: file deleted (uq %u -> %u I:%lu)",
593                                dentry->d_name.name,
594                                cookie.fid.unique,
595                                AFS_FS_I(inode)->fid.unique,
596                                inode->i_version);
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);
601                         goto out_bad;
602                 }
603
604                 dentry->d_fsdata =
605                         (void *) (unsigned long) AFS_FS_I(dir)->status.version;
606         }
607
608  out_valid:
609         dput(parent);
610         _leave(" = 1 [valid]");
611         return 1;
612
613         /* the dirent, if it exists, now points to a different vnode */
614  not_found:
615         spin_lock(&dentry->d_lock);
616         dentry->d_flags |= DCACHE_NFSFS_RENAMED;
617         spin_unlock(&dentry->d_lock);
618
619  out_bad:
620         if (inode) {
621                 /* don't unhash if we have submounts */
622                 if (have_submounts(dentry))
623                         goto out_valid;
624         }
625
626         shrink_dcache_parent(dentry);
627
628         _debug("dropping dentry %s/%s",
629                dentry->d_parent->d_name.name, dentry->d_name.name);
630         d_drop(dentry);
631
632         dput(parent);
633
634         _leave(" = 0 [bad]");
635         return 0;
636 } /* end afs_d_revalidate() */
637
638 /*****************************************************************************/
639 /*
640  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
641  * sleep)
642  * - called from dput() when d_count is going to 0.
643  * - return 1 to request dentry be unhashed, 0 otherwise
644  */
645 static int afs_d_delete(struct dentry *dentry)
646 {
647         _enter("%s", dentry->d_name.name);
648
649         if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
650                 goto zap;
651
652         if (dentry->d_inode) {
653                 if (AFS_FS_I(dentry->d_inode)->flags & AFS_VNODE_DELETED)
654                         goto zap;
655         }
656
657         _leave(" = 0 [keep]");
658         return 0;
659
660  zap:
661         _leave(" = 1 [zap]");
662         return 1;
663 } /* end afs_d_delete() */