blob: d7697f6f3b7f696923451a0b3e238284e9db1679 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/fs.h>
17#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "internal.h"
19
20static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
21 struct nameidata *nd);
22static int afs_dir_open(struct inode *inode, struct file *file);
23static int afs_dir_readdir(struct file *file, void *dirent, filldir_t filldir);
24static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
25static int afs_d_delete(struct dentry *dentry);
26static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
David Howellsafefdbb2006-10-03 01:13:46 -070027 loff_t fpos, u64 ino, unsigned dtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080029const struct file_operations afs_dir_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .open = afs_dir_open,
31 .readdir = afs_dir_readdir,
32};
33
Arjan van de Ven754661f2007-02-12 00:55:38 -080034const struct inode_operations afs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .lookup = afs_dir_lookup,
36 .getattr = afs_inode_getattr,
37#if 0 /* TODO */
38 .create = afs_dir_create,
39 .link = afs_dir_link,
40 .unlink = afs_dir_unlink,
41 .symlink = afs_dir_symlink,
42 .mkdir = afs_dir_mkdir,
43 .rmdir = afs_dir_rmdir,
44 .mknod = afs_dir_mknod,
45 .rename = afs_dir_rename,
46#endif
47};
48
49static struct dentry_operations afs_fs_dentry_operations = {
50 .d_revalidate = afs_d_revalidate,
51 .d_delete = afs_d_delete,
52};
53
54#define AFS_DIR_HASHTBL_SIZE 128
55#define AFS_DIR_DIRENT_SIZE 32
56#define AFS_DIRENT_PER_BLOCK 64
57
58union afs_dirent {
59 struct {
60 uint8_t valid;
61 uint8_t unused[1];
62 __be16 hash_next;
63 __be32 vnode;
64 __be32 unique;
65 uint8_t name[16];
66 uint8_t overflow[4]; /* if any char of the name (inc
67 * NUL) reaches here, consume
68 * the next dirent too */
69 } u;
70 uint8_t extended_name[32];
71};
72
73/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
74struct afs_dir_pagehdr {
75 __be16 npages;
76 __be16 magic;
77#define AFS_DIR_MAGIC htons(1234)
78 uint8_t nentries;
79 uint8_t bitmap[8];
80 uint8_t pad[19];
81};
82
83/* directory block layout */
84union afs_dir_block {
85
86 struct afs_dir_pagehdr pagehdr;
87
88 struct {
89 struct afs_dir_pagehdr pagehdr;
90 uint8_t alloc_ctrs[128];
91 /* dir hash table */
92 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
93 } hdr;
94
95 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
96};
97
98/* layout on a linux VM page */
99struct afs_dir_page {
100 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
101};
102
103struct afs_dir_lookup_cookie {
104 struct afs_fid fid;
105 const char *name;
106 size_t nlen;
107 int found;
108};
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * check that a directory page is valid
112 */
113static inline void afs_dir_check_page(struct inode *dir, struct page *page)
114{
115 struct afs_dir_page *dbuf;
116 loff_t latter;
117 int tmp, qty;
118
119#if 0
120 /* check the page count */
121 qty = desc.size / sizeof(dbuf->blocks[0]);
122 if (qty == 0)
123 goto error;
124
David Howells08e0e7c2007-04-26 15:55:03 -0700125 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
David Howells08e0e7c2007-04-26 15:55:03 -0700127 __FUNCTION__, dir->i_ino, qty,
128 ntohs(dbuf->blocks[0].pagehdr.npages));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 goto error;
130 }
131#endif
132
133 /* determine how many magic numbers there should be in this page */
Andrew Morton54b21a72006-01-08 01:03:05 -0800134 latter = dir->i_size - page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (latter >= PAGE_SIZE)
136 qty = PAGE_SIZE;
137 else
138 qty = latter;
139 qty /= sizeof(union afs_dir_block);
140
141 /* check them */
142 dbuf = page_address(page);
143 for (tmp = 0; tmp < qty; tmp++) {
144 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
145 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
146 __FUNCTION__, dir->i_ino, tmp, qty,
147 ntohs(dbuf->blocks[tmp].pagehdr.magic));
148 goto error;
149 }
150 }
151
152 SetPageChecked(page);
153 return;
154
David Howellsec268152007-04-26 15:49:28 -0700155error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 SetPageChecked(page);
157 SetPageError(page);
David Howellsec268152007-04-26 15:49:28 -0700158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
161 * discard a page cached in the pagecache
162 */
163static inline void afs_dir_put_page(struct page *page)
164{
165 kunmap(page);
166 page_cache_release(page);
David Howellsec268152007-04-26 15:49:28 -0700167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
170 * get a page into the pagecache
171 */
172static struct page *afs_dir_get_page(struct inode *dir, unsigned long index)
173{
174 struct page *page;
175
176 _enter("{%lu},%lu", dir->i_ino, index);
177
Pekka Enberg090d2b12006-06-23 02:05:08 -0700178 page = read_mapping_page(dir->i_mapping, index, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!IS_ERR(page)) {
180 wait_on_page_locked(page);
181 kmap(page);
182 if (!PageUptodate(page))
183 goto fail;
184 if (!PageChecked(page))
185 afs_dir_check_page(dir, page);
186 if (PageError(page))
187 goto fail;
188 }
189 return page;
190
David Howellsec268152007-04-26 15:49:28 -0700191fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 afs_dir_put_page(page);
David Howells08e0e7c2007-04-26 15:55:03 -0700193 _leave(" = -EIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return ERR_PTR(-EIO);
David Howellsec268152007-04-26 15:49:28 -0700195}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/*
198 * open an AFS directory file
199 */
200static int afs_dir_open(struct inode *inode, struct file *file)
201{
202 _enter("{%lu}", inode->i_ino);
203
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700204 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
205 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
David Howells08e0e7c2007-04-26 15:55:03 -0700207 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -ENOENT;
209
210 _leave(" = 0");
211 return 0;
David Howellsec268152007-04-26 15:49:28 -0700212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
215 * deal with one block in an AFS directory
216 */
217static int afs_dir_iterate_block(unsigned *fpos,
218 union afs_dir_block *block,
219 unsigned blkoff,
220 void *cookie,
221 filldir_t filldir)
222{
223 union afs_dirent *dire;
224 unsigned offset, next, curr;
225 size_t nlen;
226 int tmp, ret;
227
228 _enter("%u,%x,%p,,",*fpos,blkoff,block);
229
230 curr = (*fpos - blkoff) / sizeof(union afs_dirent);
231
232 /* walk through the block, an entry at a time */
233 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
234 offset < AFS_DIRENT_PER_BLOCK;
235 offset = next
236 ) {
237 next = offset + 1;
238
239 /* skip entries marked unused in the bitmap */
240 if (!(block->pagehdr.bitmap[offset / 8] &
241 (1 << (offset % 8)))) {
David Howells08e0e7c2007-04-26 15:55:03 -0700242 _debug("ENT[%Zu.%u]: unused",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 blkoff / sizeof(union afs_dir_block), offset);
244 if (offset >= curr)
245 *fpos = blkoff +
246 next * sizeof(union afs_dirent);
247 continue;
248 }
249
250 /* got a valid entry */
251 dire = &block->dirents[offset];
252 nlen = strnlen(dire->u.name,
253 sizeof(*block) -
254 offset * sizeof(union afs_dirent));
255
David Howells08e0e7c2007-04-26 15:55:03 -0700256 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 blkoff / sizeof(union afs_dir_block), offset,
258 (offset < curr ? "skip" : "fill"),
259 nlen, dire->u.name);
260
261 /* work out where the next possible entry is */
262 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
263 if (next >= AFS_DIRENT_PER_BLOCK) {
264 _debug("ENT[%Zu.%u]:"
265 " %u travelled beyond end dir block"
David Howells08e0e7c2007-04-26 15:55:03 -0700266 " (len %u/%Zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 blkoff / sizeof(union afs_dir_block),
268 offset, next, tmp, nlen);
269 return -EIO;
270 }
271 if (!(block->pagehdr.bitmap[next / 8] &
272 (1 << (next % 8)))) {
273 _debug("ENT[%Zu.%u]:"
David Howells08e0e7c2007-04-26 15:55:03 -0700274 " %u unmarked extension (len %u/%Zu)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 blkoff / sizeof(union afs_dir_block),
276 offset, next, tmp, nlen);
277 return -EIO;
278 }
279
David Howells08e0e7c2007-04-26 15:55:03 -0700280 _debug("ENT[%Zu.%u]: ext %u/%Zu",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 blkoff / sizeof(union afs_dir_block),
282 next, tmp, nlen);
283 next++;
284 }
285
286 /* skip if starts before the current position */
287 if (offset < curr)
288 continue;
289
290 /* found the next entry */
291 ret = filldir(cookie,
292 dire->u.name,
293 nlen,
294 blkoff + offset * sizeof(union afs_dirent),
295 ntohl(dire->u.vnode),
296 filldir == afs_dir_lookup_filldir ?
297 ntohl(dire->u.unique) : DT_UNKNOWN);
298 if (ret < 0) {
299 _leave(" = 0 [full]");
300 return 0;
301 }
302
303 *fpos = blkoff + next * sizeof(union afs_dirent);
304 }
305
306 _leave(" = 1 [more]");
307 return 1;
David Howellsec268152007-04-26 15:49:28 -0700308}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*
David Howells08e0e7c2007-04-26 15:55:03 -0700311 * iterate through the data blob that lists the contents of an AFS directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
313static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
314 filldir_t filldir)
315{
David Howells08e0e7c2007-04-26 15:55:03 -0700316 union afs_dir_block *dblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct afs_dir_page *dbuf;
318 struct page *page;
319 unsigned blkoff, limit;
320 int ret;
321
322 _enter("{%lu},%u,,", dir->i_ino, *fpos);
323
David Howells08e0e7c2007-04-26 15:55:03 -0700324 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 _leave(" = -ESTALE");
326 return -ESTALE;
327 }
328
329 /* round the file position up to the next entry boundary */
330 *fpos += sizeof(union afs_dirent) - 1;
331 *fpos &= ~(sizeof(union afs_dirent) - 1);
332
333 /* walk through the blocks in sequence */
334 ret = 0;
335 while (*fpos < dir->i_size) {
336 blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
337
338 /* fetch the appropriate page from the directory */
339 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE);
340 if (IS_ERR(page)) {
341 ret = PTR_ERR(page);
342 break;
343 }
344
345 limit = blkoff & ~(PAGE_SIZE - 1);
346
347 dbuf = page_address(page);
348
349 /* deal with the individual blocks stashed on this page */
350 do {
351 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
352 sizeof(union afs_dir_block)];
353 ret = afs_dir_iterate_block(fpos, dblock, blkoff,
354 cookie, filldir);
355 if (ret != 1) {
356 afs_dir_put_page(page);
357 goto out;
358 }
359
360 blkoff += sizeof(union afs_dir_block);
361
362 } while (*fpos < dir->i_size && blkoff < limit);
363
364 afs_dir_put_page(page);
365 ret = 0;
366 }
367
David Howellsec268152007-04-26 15:49:28 -0700368out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 _leave(" = %d", ret);
370 return ret;
David Howellsec268152007-04-26 15:49:28 -0700371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/*
374 * read an AFS directory
375 */
376static int afs_dir_readdir(struct file *file, void *cookie, filldir_t filldir)
377{
378 unsigned fpos;
379 int ret;
380
David Howells08e0e7c2007-04-26 15:55:03 -0700381 _enter("{%Ld,{%lu}}",
382 file->f_pos, file->f_path.dentry->d_inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 fpos = file->f_pos;
David Howells08e0e7c2007-04-26 15:55:03 -0700385 ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos,
386 cookie, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 file->f_pos = fpos;
388
389 _leave(" = %d", ret);
390 return ret;
David Howellsec268152007-04-26 15:49:28 -0700391}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/*
394 * search the directory for a name
395 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
396 * uniquifier through dtype
397 */
398static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
David Howellsafefdbb2006-10-03 01:13:46 -0700399 loff_t fpos, u64 ino, unsigned dtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct afs_dir_lookup_cookie *cookie = _cookie;
402
David Howells08e0e7c2007-04-26 15:55:03 -0700403 _enter("{%s,%Zu},%s,%u,,%llu,%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 cookie->name, cookie->nlen, name, nlen, ino, dtype);
405
David Howells08e0e7c2007-04-26 15:55:03 -0700406 /* insanity checks first */
407 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
408 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
411 _leave(" = 0 [no]");
412 return 0;
413 }
414
415 cookie->fid.vnode = ino;
416 cookie->fid.unique = dtype;
417 cookie->found = 1;
418
419 _leave(" = -1 [found]");
420 return -1;
David Howellsec268152007-04-26 15:49:28 -0700421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
David Howells08e0e7c2007-04-26 15:55:03 -0700424 * do a lookup in a directory
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
David Howells08e0e7c2007-04-26 15:55:03 -0700426static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
427 struct afs_fid *fid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct afs_dir_lookup_cookie cookie;
430 struct afs_super_info *as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned fpos;
432 int ret;
433
David Howells08e0e7c2007-04-26 15:55:03 -0700434 _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 as = dir->i_sb->s_fs_info;
437
438 /* search the directory */
439 cookie.name = dentry->d_name.name;
440 cookie.nlen = dentry->d_name.len;
441 cookie.fid.vid = as->volume->vid;
442 cookie.found = 0;
443
444 fpos = 0;
445 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_dir_lookup_filldir);
446 if (ret < 0) {
David Howells08e0e7c2007-04-26 15:55:03 -0700447 _leave(" = %d [iter]", ret);
448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
451 ret = -ENOENT;
452 if (!cookie.found) {
David Howells08e0e7c2007-04-26 15:55:03 -0700453 _leave(" = -ENOENT [not found]");
454 return -ENOENT;
455 }
456
457 *fid = cookie.fid;
458 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
459 return 0;
460}
461
462/*
463 * look up an entry in a directory
464 */
465static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
466 struct nameidata *nd)
467{
468 struct afs_vnode *vnode;
469 struct afs_fid fid;
470 struct inode *inode;
471 int ret;
472
473 _enter("{%lu},%p{%s}", dir->i_ino, dentry, dentry->d_name.name);
474
475 if (dentry->d_name.len > 255) {
476 _leave(" = -ENAMETOOLONG");
477 return ERR_PTR(-ENAMETOOLONG);
478 }
479
480 vnode = AFS_FS_I(dir);
481 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
482 _leave(" = -ESTALE");
483 return ERR_PTR(-ESTALE);
484 }
485
486 ret = afs_do_lookup(dir, dentry, &fid);
487 if (ret < 0) {
488 _leave(" = %d [do]", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return ERR_PTR(ret);
490 }
491
492 /* instantiate the dentry */
David Howells08e0e7c2007-04-26 15:55:03 -0700493 inode = afs_iget(dir->i_sb, &fid);
494 if (IS_ERR(inode)) {
495 _leave(" = %ld", PTR_ERR(inode));
496 return ERR_PTR(PTR_ERR(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
499 dentry->d_op = &afs_fs_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 d_add(dentry, inode);
502 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
David Howells08e0e7c2007-04-26 15:55:03 -0700503 fid.vnode,
504 fid.unique,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 dentry->d_inode->i_ino,
506 dentry->d_inode->i_version);
507
508 return NULL;
David Howellsec268152007-04-26 15:49:28 -0700509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
David Howells08e0e7c2007-04-26 15:55:03 -0700512 * propagate changed and modified flags on a directory to all the children of
513 * that directory as they may indicate that the ACL on the dir has changed,
514 * potentially rendering the child inaccessible or that a file has been deleted
515 * or renamed
516 */
517static void afs_propagate_dir_changes(struct dentry *dir)
518{
519 struct dentry *child;
520 bool c, m;
521
522 c = test_bit(AFS_VNODE_CHANGED, &AFS_FS_I(dir->d_inode)->flags);
523 m = test_bit(AFS_VNODE_MODIFIED, &AFS_FS_I(dir->d_inode)->flags);
524
525 _enter("{%d,%d}", c, m);
526
527 spin_lock(&dir->d_lock);
528
529 list_for_each_entry(child, &dir->d_subdirs, d_u.d_child) {
530 if (child->d_inode) {
531 struct afs_vnode *vnode;
532
533 _debug("tag %s", child->d_name.name);
534 vnode = AFS_FS_I(child->d_inode);
535 if (c)
536 set_bit(AFS_VNODE_DIR_CHANGED, &vnode->flags);
537 if (m)
538 set_bit(AFS_VNODE_DIR_MODIFIED, &vnode->flags);
539 }
540 }
541
542 spin_unlock(&dir->d_lock);
543}
544
545/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * check that a dentry lookup hit has found a valid entry
547 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
548 * inode
David Howells08e0e7c2007-04-26 15:55:03 -0700549 * - there are several things we need to check
550 * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
551 * symlink)
552 * - parent dir metadata changed (security changes)
553 * - dentry data changed (write, truncate)
554 * - dentry metadata changed (security changes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
556static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
557{
David Howells08e0e7c2007-04-26 15:55:03 -0700558 struct afs_vnode *vnode;
559 struct afs_fid fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct dentry *parent;
561 struct inode *inode, *dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int ret;
563
David Howells08e0e7c2007-04-26 15:55:03 -0700564 vnode = AFS_FS_I(dentry->d_inode);
565
566 _enter("{sb=%p n=%s fl=%lx},",
567 dentry->d_sb, dentry->d_name.name, vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* lock down the parent dentry so we can peer at it */
David Howells08e0e7c2007-04-26 15:55:03 -0700570 parent = dget_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 dir = parent->d_inode;
573 inode = dentry->d_inode;
574
575 /* handle a negative dentry */
576 if (!inode)
577 goto out_bad;
578
579 /* handle a bad inode */
580 if (is_bad_inode(inode)) {
581 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
David Howells08e0e7c2007-04-26 15:55:03 -0700582 parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto out_bad;
584 }
585
David Howells08e0e7c2007-04-26 15:55:03 -0700586 /* check that this dirent still exists if the directory's contents were
587 * modified */
588 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 _debug("%s: parent dir deleted", dentry->d_name.name);
590 goto out_bad;
591 }
592
David Howells08e0e7c2007-04-26 15:55:03 -0700593 if (test_and_clear_bit(AFS_VNODE_DIR_MODIFIED, &vnode->flags)) {
594 /* rm/rmdir/rename may have occurred */
595 _debug("dir modified");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* search the directory for this vnode */
David Howells08e0e7c2007-04-26 15:55:03 -0700598 ret = afs_do_lookup(dir, dentry, &fid);
599 if (ret == -ENOENT) {
600 _debug("%s: dirent not found", dentry->d_name.name);
601 goto not_found;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (ret < 0) {
604 _debug("failed to iterate dir %s: %d",
605 parent->d_name.name, ret);
606 goto out_bad;
607 }
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* if the vnode ID has changed, then the dirent points to a
610 * different file */
David Howells08e0e7c2007-04-26 15:55:03 -0700611 if (fid.vnode != vnode->fid.vnode) {
612 _debug("%s: dirent changed [%u != %u]",
613 dentry->d_name.name, fid.vnode,
614 vnode->fid.vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 goto not_found;
616 }
617
618 /* if the vnode ID uniqifier has changed, then the file has
619 * been deleted */
David Howells08e0e7c2007-04-26 15:55:03 -0700620 if (fid.unique != vnode->fid.unique) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 _debug("%s: file deleted (uq %u -> %u I:%lu)",
David Howells08e0e7c2007-04-26 15:55:03 -0700622 dentry->d_name.name, fid.unique,
623 vnode->fid.unique, inode->i_version);
624 spin_lock(&vnode->lock);
625 set_bit(AFS_VNODE_DELETED, &vnode->flags);
626 spin_unlock(&vnode->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 invalidate_remote_inode(inode);
628 goto out_bad;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
David Howells08e0e7c2007-04-26 15:55:03 -0700632 /* if the directory's metadata were changed then the security may be
633 * different and we may no longer have access */
634 mutex_lock(&vnode->cb_broken_lock);
635
636 if (test_and_clear_bit(AFS_VNODE_DIR_CHANGED, &vnode->flags) ||
637 test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
638 _debug("%s: changed", dentry->d_name.name);
639 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
640 if (afs_vnode_fetch_status(vnode) < 0) {
641 mutex_unlock(&vnode->cb_broken_lock);
642 goto out_bad;
643 }
644 }
645
646 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
647 _debug("%s: file already deleted", dentry->d_name.name);
648 mutex_unlock(&vnode->cb_broken_lock);
649 goto out_bad;
650 }
651
652 /* if the vnode's data version number changed then its contents are
653 * different */
654 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
655 _debug("zap data");
656 invalidate_remote_inode(inode);
657 }
658
659 if (S_ISDIR(inode->i_mode) &&
660 (test_bit(AFS_VNODE_CHANGED, &vnode->flags) ||
661 test_bit(AFS_VNODE_MODIFIED, &vnode->flags)))
662 afs_propagate_dir_changes(dentry);
663
664 clear_bit(AFS_VNODE_CHANGED, &vnode->flags);
665 clear_bit(AFS_VNODE_MODIFIED, &vnode->flags);
666 mutex_unlock(&vnode->cb_broken_lock);
667
David Howellsec268152007-04-26 15:49:28 -0700668out_valid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 dput(parent);
670 _leave(" = 1 [valid]");
671 return 1;
672
673 /* the dirent, if it exists, now points to a different vnode */
David Howellsec268152007-04-26 15:49:28 -0700674not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 spin_lock(&dentry->d_lock);
676 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
677 spin_unlock(&dentry->d_lock);
678
David Howellsec268152007-04-26 15:49:28 -0700679out_bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (inode) {
681 /* don't unhash if we have submounts */
682 if (have_submounts(dentry))
683 goto out_valid;
684 }
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 _debug("dropping dentry %s/%s",
David Howells08e0e7c2007-04-26 15:55:03 -0700687 parent->d_name.name, dentry->d_name.name);
688 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 dput(parent);
691
692 _leave(" = 0 [bad]");
693 return 0;
David Howellsec268152007-04-26 15:49:28 -0700694}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/*
697 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
698 * sleep)
699 * - called from dput() when d_count is going to 0.
700 * - return 1 to request dentry be unhashed, 0 otherwise
701 */
702static int afs_d_delete(struct dentry *dentry)
703{
704 _enter("%s", dentry->d_name.name);
705
706 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
707 goto zap;
708
David Howells08e0e7c2007-04-26 15:55:03 -0700709 if (dentry->d_inode &&
710 test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 goto zap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 _leave(" = 0 [keep]");
714 return 0;
715
David Howellsec268152007-04-26 15:49:28 -0700716zap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 _leave(" = 1 [zap]");
718 return 1;
David Howellsec268152007-04-26 15:49:28 -0700719}