move internal-only parts of ncpfs headers to fs/ncpfs
[linux-2.6-block.git] / fs / afs / dir.c
CommitLineData
1da177e4
LT
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>
1da177e4 15#include <linux/fs.h>
34286d66 16#include <linux/namei.h>
1da177e4 17#include <linux/pagemap.h>
00d3b7a4 18#include <linux/ctype.h>
e8edc6e0 19#include <linux/sched.h>
1da177e4
LT
20#include "internal.h"
21
260a9803
DH
22static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
23 struct nameidata *nd);
1da177e4 24static int afs_dir_open(struct inode *inode, struct file *file);
260a9803 25static int afs_readdir(struct file *file, void *dirent, filldir_t filldir);
1da177e4 26static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
fe15ce44 27static int afs_d_delete(const struct dentry *dentry);
260a9803
DH
28static void afs_d_release(struct dentry *dentry);
29static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
afefdbb2 30 loff_t fpos, u64 ino, unsigned dtype);
260a9803
DH
31static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
32 struct nameidata *nd);
33static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
34static int afs_rmdir(struct inode *dir, struct dentry *dentry);
35static int afs_unlink(struct inode *dir, struct dentry *dentry);
36static int afs_link(struct dentry *from, struct inode *dir,
37 struct dentry *dentry);
38static int afs_symlink(struct inode *dir, struct dentry *dentry,
39 const char *content);
40static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
41 struct inode *new_dir, struct dentry *new_dentry);
1da177e4 42
4b6f5d20 43const struct file_operations afs_dir_file_operations = {
1da177e4 44 .open = afs_dir_open,
00d3b7a4 45 .release = afs_release,
260a9803 46 .readdir = afs_readdir,
e8d6c554 47 .lock = afs_lock,
3222a3e5 48 .llseek = generic_file_llseek,
1da177e4
LT
49};
50
754661f1 51const struct inode_operations afs_dir_inode_operations = {
260a9803
DH
52 .create = afs_create,
53 .lookup = afs_lookup,
54 .link = afs_link,
55 .unlink = afs_unlink,
56 .symlink = afs_symlink,
57 .mkdir = afs_mkdir,
58 .rmdir = afs_rmdir,
59 .rename = afs_rename,
00d3b7a4 60 .permission = afs_permission,
416351f2 61 .getattr = afs_getattr,
31143d5d 62 .setattr = afs_setattr,
1da177e4
LT
63};
64
79be57cc 65static const struct dentry_operations afs_fs_dentry_operations = {
1da177e4
LT
66 .d_revalidate = afs_d_revalidate,
67 .d_delete = afs_d_delete,
260a9803 68 .d_release = afs_d_release,
1da177e4
LT
69};
70
71#define AFS_DIR_HASHTBL_SIZE 128
72#define AFS_DIR_DIRENT_SIZE 32
73#define AFS_DIRENT_PER_BLOCK 64
74
75union afs_dirent {
76 struct {
77 uint8_t valid;
78 uint8_t unused[1];
79 __be16 hash_next;
80 __be32 vnode;
81 __be32 unique;
82 uint8_t name[16];
83 uint8_t overflow[4]; /* if any char of the name (inc
84 * NUL) reaches here, consume
85 * the next dirent too */
86 } u;
87 uint8_t extended_name[32];
88};
89
90/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
91struct afs_dir_pagehdr {
92 __be16 npages;
93 __be16 magic;
94#define AFS_DIR_MAGIC htons(1234)
95 uint8_t nentries;
96 uint8_t bitmap[8];
97 uint8_t pad[19];
98};
99
100/* directory block layout */
101union afs_dir_block {
102
103 struct afs_dir_pagehdr pagehdr;
104
105 struct {
106 struct afs_dir_pagehdr pagehdr;
107 uint8_t alloc_ctrs[128];
108 /* dir hash table */
109 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
110 } hdr;
111
112 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
113};
114
115/* layout on a linux VM page */
116struct afs_dir_page {
117 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
118};
119
260a9803 120struct afs_lookup_cookie {
1da177e4
LT
121 struct afs_fid fid;
122 const char *name;
123 size_t nlen;
124 int found;
125};
126
1da177e4
LT
127/*
128 * check that a directory page is valid
129 */
130static inline void afs_dir_check_page(struct inode *dir, struct page *page)
131{
132 struct afs_dir_page *dbuf;
133 loff_t latter;
134 int tmp, qty;
135
136#if 0
137 /* check the page count */
138 qty = desc.size / sizeof(dbuf->blocks[0]);
139 if (qty == 0)
140 goto error;
141
08e0e7c8 142 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
1da177e4 143 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
530b6412 144 __func__, dir->i_ino, qty,
08e0e7c8 145 ntohs(dbuf->blocks[0].pagehdr.npages));
1da177e4
LT
146 goto error;
147 }
148#endif
149
150 /* determine how many magic numbers there should be in this page */
54b21a79 151 latter = dir->i_size - page_offset(page);
1da177e4
LT
152 if (latter >= PAGE_SIZE)
153 qty = PAGE_SIZE;
154 else
155 qty = latter;
156 qty /= sizeof(union afs_dir_block);
157
158 /* check them */
159 dbuf = page_address(page);
160 for (tmp = 0; tmp < qty; tmp++) {
161 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
162 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
530b6412 163 __func__, dir->i_ino, tmp, qty,
1da177e4
LT
164 ntohs(dbuf->blocks[tmp].pagehdr.magic));
165 goto error;
166 }
167 }
168
169 SetPageChecked(page);
170 return;
171
ec26815a 172error:
1da177e4
LT
173 SetPageChecked(page);
174 SetPageError(page);
ec26815a 175}
1da177e4 176
1da177e4
LT
177/*
178 * discard a page cached in the pagecache
179 */
180static inline void afs_dir_put_page(struct page *page)
181{
182 kunmap(page);
183 page_cache_release(page);
ec26815a 184}
1da177e4 185
1da177e4
LT
186/*
187 * get a page into the pagecache
188 */
00d3b7a4
DH
189static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
190 struct key *key)
1da177e4
LT
191{
192 struct page *page;
1da177e4
LT
193 _enter("{%lu},%lu", dir->i_ino, index);
194
f6d335c0 195 page = read_cache_page(dir->i_mapping, index, afs_page_filler, key);
1da177e4 196 if (!IS_ERR(page)) {
1da177e4 197 kmap(page);
1da177e4
LT
198 if (!PageChecked(page))
199 afs_dir_check_page(dir, page);
200 if (PageError(page))
201 goto fail;
202 }
203 return page;
204
ec26815a 205fail:
1da177e4 206 afs_dir_put_page(page);
08e0e7c8 207 _leave(" = -EIO");
1da177e4 208 return ERR_PTR(-EIO);
ec26815a 209}
1da177e4 210
1da177e4
LT
211/*
212 * open an AFS directory file
213 */
214static int afs_dir_open(struct inode *inode, struct file *file)
215{
216 _enter("{%lu}", inode->i_ino);
217
2ecd05ae
AD
218 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
219 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
1da177e4 220
08e0e7c8 221 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
1da177e4
LT
222 return -ENOENT;
223
00d3b7a4 224 return afs_open(inode, file);
ec26815a 225}
1da177e4 226
1da177e4
LT
227/*
228 * deal with one block in an AFS directory
229 */
230static int afs_dir_iterate_block(unsigned *fpos,
231 union afs_dir_block *block,
232 unsigned blkoff,
233 void *cookie,
234 filldir_t filldir)
235{
236 union afs_dirent *dire;
237 unsigned offset, next, curr;
238 size_t nlen;
239 int tmp, ret;
240
241 _enter("%u,%x,%p,,",*fpos,blkoff,block);
242
243 curr = (*fpos - blkoff) / sizeof(union afs_dirent);
244
245 /* walk through the block, an entry at a time */
246 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
247 offset < AFS_DIRENT_PER_BLOCK;
248 offset = next
249 ) {
250 next = offset + 1;
251
252 /* skip entries marked unused in the bitmap */
253 if (!(block->pagehdr.bitmap[offset / 8] &
254 (1 << (offset % 8)))) {
08e0e7c8 255 _debug("ENT[%Zu.%u]: unused",
1da177e4
LT
256 blkoff / sizeof(union afs_dir_block), offset);
257 if (offset >= curr)
258 *fpos = blkoff +
259 next * sizeof(union afs_dirent);
260 continue;
261 }
262
263 /* got a valid entry */
264 dire = &block->dirents[offset];
265 nlen = strnlen(dire->u.name,
266 sizeof(*block) -
267 offset * sizeof(union afs_dirent));
268
08e0e7c8 269 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
1da177e4
LT
270 blkoff / sizeof(union afs_dir_block), offset,
271 (offset < curr ? "skip" : "fill"),
272 nlen, dire->u.name);
273
274 /* work out where the next possible entry is */
275 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
276 if (next >= AFS_DIRENT_PER_BLOCK) {
277 _debug("ENT[%Zu.%u]:"
278 " %u travelled beyond end dir block"
08e0e7c8 279 " (len %u/%Zu)",
1da177e4
LT
280 blkoff / sizeof(union afs_dir_block),
281 offset, next, tmp, nlen);
282 return -EIO;
283 }
284 if (!(block->pagehdr.bitmap[next / 8] &
285 (1 << (next % 8)))) {
286 _debug("ENT[%Zu.%u]:"
08e0e7c8 287 " %u unmarked extension (len %u/%Zu)",
1da177e4
LT
288 blkoff / sizeof(union afs_dir_block),
289 offset, next, tmp, nlen);
290 return -EIO;
291 }
292
08e0e7c8 293 _debug("ENT[%Zu.%u]: ext %u/%Zu",
1da177e4
LT
294 blkoff / sizeof(union afs_dir_block),
295 next, tmp, nlen);
296 next++;
297 }
298
299 /* skip if starts before the current position */
300 if (offset < curr)
301 continue;
302
303 /* found the next entry */
304 ret = filldir(cookie,
305 dire->u.name,
306 nlen,
307 blkoff + offset * sizeof(union afs_dirent),
308 ntohl(dire->u.vnode),
260a9803 309 filldir == afs_lookup_filldir ?
1da177e4
LT
310 ntohl(dire->u.unique) : DT_UNKNOWN);
311 if (ret < 0) {
312 _leave(" = 0 [full]");
313 return 0;
314 }
315
316 *fpos = blkoff + next * sizeof(union afs_dirent);
317 }
318
319 _leave(" = 1 [more]");
320 return 1;
ec26815a 321}
1da177e4 322
1da177e4 323/*
08e0e7c8 324 * iterate through the data blob that lists the contents of an AFS directory
1da177e4
LT
325 */
326static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
00d3b7a4 327 filldir_t filldir, struct key *key)
1da177e4 328{
08e0e7c8 329 union afs_dir_block *dblock;
1da177e4
LT
330 struct afs_dir_page *dbuf;
331 struct page *page;
332 unsigned blkoff, limit;
333 int ret;
334
335 _enter("{%lu},%u,,", dir->i_ino, *fpos);
336
08e0e7c8 337 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
1da177e4
LT
338 _leave(" = -ESTALE");
339 return -ESTALE;
340 }
341
342 /* round the file position up to the next entry boundary */
343 *fpos += sizeof(union afs_dirent) - 1;
344 *fpos &= ~(sizeof(union afs_dirent) - 1);
345
346 /* walk through the blocks in sequence */
347 ret = 0;
348 while (*fpos < dir->i_size) {
349 blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
350
351 /* fetch the appropriate page from the directory */
00d3b7a4 352 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
1da177e4
LT
353 if (IS_ERR(page)) {
354 ret = PTR_ERR(page);
355 break;
356 }
357
358 limit = blkoff & ~(PAGE_SIZE - 1);
359
360 dbuf = page_address(page);
361
362 /* deal with the individual blocks stashed on this page */
363 do {
364 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
365 sizeof(union afs_dir_block)];
366 ret = afs_dir_iterate_block(fpos, dblock, blkoff,
367 cookie, filldir);
368 if (ret != 1) {
369 afs_dir_put_page(page);
370 goto out;
371 }
372
373 blkoff += sizeof(union afs_dir_block);
374
375 } while (*fpos < dir->i_size && blkoff < limit);
376
377 afs_dir_put_page(page);
378 ret = 0;
379 }
380
ec26815a 381out:
1da177e4
LT
382 _leave(" = %d", ret);
383 return ret;
ec26815a 384}
1da177e4 385
1da177e4
LT
386/*
387 * read an AFS directory
388 */
260a9803 389static int afs_readdir(struct file *file, void *cookie, filldir_t filldir)
1da177e4
LT
390{
391 unsigned fpos;
392 int ret;
393
08e0e7c8
DH
394 _enter("{%Ld,{%lu}}",
395 file->f_pos, file->f_path.dentry->d_inode->i_ino);
1da177e4 396
00d3b7a4
DH
397 ASSERT(file->private_data != NULL);
398
1da177e4 399 fpos = file->f_pos;
08e0e7c8 400 ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos,
00d3b7a4 401 cookie, filldir, file->private_data);
1da177e4
LT
402 file->f_pos = fpos;
403
404 _leave(" = %d", ret);
405 return ret;
ec26815a 406}
1da177e4 407
1da177e4
LT
408/*
409 * search the directory for a name
410 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
411 * uniquifier through dtype
412 */
260a9803
DH
413static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
414 loff_t fpos, u64 ino, unsigned dtype)
1da177e4 415{
260a9803 416 struct afs_lookup_cookie *cookie = _cookie;
1da177e4 417
08e0e7c8 418 _enter("{%s,%Zu},%s,%u,,%llu,%u",
ba3e0e1a
DM
419 cookie->name, cookie->nlen, name, nlen,
420 (unsigned long long) ino, dtype);
1da177e4 421
08e0e7c8
DH
422 /* insanity checks first */
423 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
424 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
425
1da177e4
LT
426 if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
427 _leave(" = 0 [no]");
428 return 0;
429 }
430
431 cookie->fid.vnode = ino;
432 cookie->fid.unique = dtype;
433 cookie->found = 1;
434
435 _leave(" = -1 [found]");
436 return -1;
ec26815a 437}
1da177e4 438
1da177e4 439/*
08e0e7c8 440 * do a lookup in a directory
260a9803 441 * - just returns the FID the dentry name maps to if found
1da177e4 442 */
08e0e7c8 443static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
00d3b7a4 444 struct afs_fid *fid, struct key *key)
1da177e4 445{
260a9803 446 struct afs_lookup_cookie cookie;
1da177e4 447 struct afs_super_info *as;
1da177e4
LT
448 unsigned fpos;
449 int ret;
450
08e0e7c8 451 _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
1da177e4
LT
452
453 as = dir->i_sb->s_fs_info;
454
455 /* search the directory */
456 cookie.name = dentry->d_name.name;
457 cookie.nlen = dentry->d_name.len;
458 cookie.fid.vid = as->volume->vid;
459 cookie.found = 0;
460
461 fpos = 0;
260a9803 462 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_lookup_filldir,
00d3b7a4 463 key);
1da177e4 464 if (ret < 0) {
08e0e7c8
DH
465 _leave(" = %d [iter]", ret);
466 return ret;
1da177e4
LT
467 }
468
469 ret = -ENOENT;
470 if (!cookie.found) {
08e0e7c8
DH
471 _leave(" = -ENOENT [not found]");
472 return -ENOENT;
1da177e4
LT
473 }
474
08e0e7c8
DH
475 *fid = cookie.fid;
476 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
477 return 0;
478}
479
bec5eb61 480/*
481 * Try to auto mount the mountpoint with pseudo directory, if the autocell
482 * operation is setted.
483 */
484static struct inode *afs_try_auto_mntpt(
485 int ret, struct dentry *dentry, struct inode *dir, struct key *key,
486 struct afs_fid *fid)
487{
488 const char *devname = dentry->d_name.name;
489 struct afs_vnode *vnode = AFS_FS_I(dir);
490 struct inode *inode;
491
492 _enter("%d, %p{%s}, {%x:%u}, %p",
493 ret, dentry, devname, vnode->fid.vid, vnode->fid.vnode, key);
494
495 if (ret != -ENOENT ||
496 !test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
497 goto out;
498
499 inode = afs_iget_autocell(dir, devname, strlen(devname), key);
500 if (IS_ERR(inode)) {
501 ret = PTR_ERR(inode);
502 goto out;
503 }
504
505 *fid = AFS_FS_I(inode)->fid;
506 _leave("= %p", inode);
507 return inode;
508
509out:
510 _leave("= %d", ret);
511 return ERR_PTR(ret);
512}
513
08e0e7c8
DH
514/*
515 * look up an entry in a directory
516 */
260a9803
DH
517static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
518 struct nameidata *nd)
08e0e7c8
DH
519{
520 struct afs_vnode *vnode;
521 struct afs_fid fid;
522 struct inode *inode;
00d3b7a4 523 struct key *key;
08e0e7c8
DH
524 int ret;
525
260a9803
DH
526 vnode = AFS_FS_I(dir);
527
416351f2 528 _enter("{%x:%u},%p{%s},",
260a9803
DH
529 vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
530
531 ASSERTCMP(dentry->d_inode, ==, NULL);
08e0e7c8 532
45222b9e 533 if (dentry->d_name.len >= AFSNAMEMAX) {
08e0e7c8
DH
534 _leave(" = -ENAMETOOLONG");
535 return ERR_PTR(-ENAMETOOLONG);
536 }
537
08e0e7c8
DH
538 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
539 _leave(" = -ESTALE");
540 return ERR_PTR(-ESTALE);
541 }
542
00d3b7a4
DH
543 key = afs_request_key(vnode->volume->cell);
544 if (IS_ERR(key)) {
545 _leave(" = %ld [key]", PTR_ERR(key));
e231c2ee 546 return ERR_CAST(key);
00d3b7a4
DH
547 }
548
260a9803
DH
549 ret = afs_validate(vnode, key);
550 if (ret < 0) {
551 key_put(key);
552 _leave(" = %d [val]", ret);
553 return ERR_PTR(ret);
554 }
555
00d3b7a4 556 ret = afs_do_lookup(dir, dentry, &fid, key);
1da177e4 557 if (ret < 0) {
bec5eb61 558 inode = afs_try_auto_mntpt(ret, dentry, dir, key, &fid);
559 if (!IS_ERR(inode)) {
560 key_put(key);
561 goto success;
562 }
563
564 ret = PTR_ERR(inode);
00d3b7a4 565 key_put(key);
260a9803
DH
566 if (ret == -ENOENT) {
567 d_add(dentry, NULL);
568 _leave(" = NULL [negative]");
569 return NULL;
570 }
08e0e7c8 571 _leave(" = %d [do]", ret);
1da177e4
LT
572 return ERR_PTR(ret);
573 }
260a9803 574 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
1da177e4 575
08e0e7c8 576 /* instantiate the dentry */
260a9803 577 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
00d3b7a4 578 key_put(key);
08e0e7c8
DH
579 if (IS_ERR(inode)) {
580 _leave(" = %ld", PTR_ERR(inode));
e231c2ee 581 return ERR_CAST(inode);
08e0e7c8
DH
582 }
583
bec5eb61 584success:
fb045adb 585 d_set_d_op(dentry, &afs_fs_dentry_operations);
1da177e4
LT
586
587 d_add(dentry, inode);
7a224228 588 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%llu }",
08e0e7c8
DH
589 fid.vnode,
590 fid.unique,
1da177e4 591 dentry->d_inode->i_ino,
7a224228 592 (unsigned long long)dentry->d_inode->i_version);
1da177e4
LT
593
594 return NULL;
ec26815a 595}
1da177e4 596
1da177e4
LT
597/*
598 * check that a dentry lookup hit has found a valid entry
599 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
600 * inode
1da177e4
LT
601 */
602static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
603{
260a9803 604 struct afs_vnode *vnode, *dir;
dd0d9a46 605 struct afs_fid uninitialized_var(fid);
1da177e4 606 struct dentry *parent;
00d3b7a4 607 struct key *key;
260a9803 608 void *dir_version;
1da177e4
LT
609 int ret;
610
34286d66
NP
611 if (nd->flags & LOOKUP_RCU)
612 return -ECHILD;
613
08e0e7c8
DH
614 vnode = AFS_FS_I(dentry->d_inode);
615
260a9803
DH
616 if (dentry->d_inode)
617 _enter("{v={%x:%u} n=%s fl=%lx},",
618 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
619 vnode->flags);
620 else
621 _enter("{neg n=%s}", dentry->d_name.name);
1da177e4 622
260a9803 623 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
00d3b7a4
DH
624 if (IS_ERR(key))
625 key = NULL;
626
1da177e4 627 /* lock down the parent dentry so we can peer at it */
08e0e7c8 628 parent = dget_parent(dentry);
260a9803 629 if (!parent->d_inode)
1da177e4
LT
630 goto out_bad;
631
260a9803 632 dir = AFS_FS_I(parent->d_inode);
1da177e4 633
260a9803
DH
634 /* validate the parent directory */
635 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
636 afs_validate(dir, key);
637
638 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1da177e4
LT
639 _debug("%s: parent dir deleted", dentry->d_name.name);
640 goto out_bad;
641 }
642
260a9803
DH
643 dir_version = (void *) (unsigned long) dir->status.data_version;
644 if (dentry->d_fsdata == dir_version)
645 goto out_valid; /* the dir contents are unchanged */
1da177e4 646
260a9803
DH
647 _debug("dir modified");
648
649 /* search the directory for this vnode */
650 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
651 switch (ret) {
652 case 0:
653 /* the filename maps to something */
654 if (!dentry->d_inode)
655 goto out_bad;
656 if (is_bad_inode(dentry->d_inode)) {
657 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
658 parent->d_name.name, dentry->d_name.name);
1da177e4
LT
659 goto out_bad;
660 }
661
1da177e4
LT
662 /* if the vnode ID has changed, then the dirent points to a
663 * different file */
08e0e7c8
DH
664 if (fid.vnode != vnode->fid.vnode) {
665 _debug("%s: dirent changed [%u != %u]",
666 dentry->d_name.name, fid.vnode,
667 vnode->fid.vnode);
1da177e4
LT
668 goto not_found;
669 }
670
671 /* if the vnode ID uniqifier has changed, then the file has
260a9803
DH
672 * been deleted and replaced, and the original vnode ID has
673 * been reused */
08e0e7c8 674 if (fid.unique != vnode->fid.unique) {
7a224228 675 _debug("%s: file deleted (uq %u -> %u I:%llu)",
08e0e7c8 676 dentry->d_name.name, fid.unique,
7a224228
JNC
677 vnode->fid.unique,
678 (unsigned long long)dentry->d_inode->i_version);
08e0e7c8
DH
679 spin_lock(&vnode->lock);
680 set_bit(AFS_VNODE_DELETED, &vnode->flags);
681 spin_unlock(&vnode->lock);
260a9803 682 goto not_found;
1da177e4 683 }
260a9803 684 goto out_valid;
08e0e7c8 685
260a9803
DH
686 case -ENOENT:
687 /* the filename is unknown */
688 _debug("%s: dirent not found", dentry->d_name.name);
689 if (dentry->d_inode)
690 goto not_found;
691 goto out_valid;
1da177e4 692
260a9803
DH
693 default:
694 _debug("failed to iterate dir %s: %d",
695 parent->d_name.name, ret);
08e0e7c8
DH
696 goto out_bad;
697 }
698
ec26815a 699out_valid:
260a9803
DH
700 dentry->d_fsdata = dir_version;
701out_skip:
1da177e4 702 dput(parent);
00d3b7a4 703 key_put(key);
1da177e4
LT
704 _leave(" = 1 [valid]");
705 return 1;
706
707 /* the dirent, if it exists, now points to a different vnode */
ec26815a 708not_found:
1da177e4
LT
709 spin_lock(&dentry->d_lock);
710 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
711 spin_unlock(&dentry->d_lock);
712
ec26815a 713out_bad:
260a9803 714 if (dentry->d_inode) {
1da177e4
LT
715 /* don't unhash if we have submounts */
716 if (have_submounts(dentry))
260a9803 717 goto out_skip;
1da177e4
LT
718 }
719
1da177e4 720 _debug("dropping dentry %s/%s",
08e0e7c8
DH
721 parent->d_name.name, dentry->d_name.name);
722 shrink_dcache_parent(dentry);
1da177e4 723 d_drop(dentry);
1da177e4 724 dput(parent);
00d3b7a4 725 key_put(key);
1da177e4
LT
726
727 _leave(" = 0 [bad]");
728 return 0;
ec26815a 729}
1da177e4 730
1da177e4
LT
731/*
732 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
733 * sleep)
734 * - called from dput() when d_count is going to 0.
735 * - return 1 to request dentry be unhashed, 0 otherwise
736 */
fe15ce44 737static int afs_d_delete(const struct dentry *dentry)
1da177e4
LT
738{
739 _enter("%s", dentry->d_name.name);
740
741 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
742 goto zap;
743
08e0e7c8 744 if (dentry->d_inode &&
bec5eb61 745 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags) ||
746 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(dentry->d_inode)->flags)))
747 goto zap;
1da177e4
LT
748
749 _leave(" = 0 [keep]");
750 return 0;
751
ec26815a 752zap:
1da177e4
LT
753 _leave(" = 1 [zap]");
754 return 1;
ec26815a 755}
260a9803
DH
756
757/*
758 * handle dentry release
759 */
760static void afs_d_release(struct dentry *dentry)
761{
762 _enter("%s", dentry->d_name.name);
763}
764
765/*
766 * create a directory on an AFS filesystem
767 */
768static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
769{
770 struct afs_file_status status;
771 struct afs_callback cb;
772 struct afs_server *server;
773 struct afs_vnode *dvnode, *vnode;
774 struct afs_fid fid;
775 struct inode *inode;
776 struct key *key;
777 int ret;
778
779 dvnode = AFS_FS_I(dir);
780
416351f2 781 _enter("{%x:%u},{%s},%o",
260a9803
DH
782 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
783
784 ret = -ENAMETOOLONG;
45222b9e 785 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
786 goto error;
787
788 key = afs_request_key(dvnode->volume->cell);
789 if (IS_ERR(key)) {
790 ret = PTR_ERR(key);
791 goto error;
792 }
793
794 mode |= S_IFDIR;
795 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
796 mode, &fid, &status, &cb, &server);
797 if (ret < 0)
798 goto mkdir_error;
799
800 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
801 if (IS_ERR(inode)) {
802 /* ENOMEM at a really inconvenient time - just abandon the new
803 * directory on the server */
804 ret = PTR_ERR(inode);
805 goto iget_error;
806 }
807
808 /* apply the status report we've got for the new vnode */
809 vnode = AFS_FS_I(inode);
810 spin_lock(&vnode->lock);
811 vnode->update_cnt++;
812 spin_unlock(&vnode->lock);
813 afs_vnode_finalise_status_update(vnode, server);
814 afs_put_server(server);
815
816 d_instantiate(dentry, inode);
817 if (d_unhashed(dentry)) {
818 _debug("not hashed");
819 d_rehash(dentry);
820 }
821 key_put(key);
822 _leave(" = 0");
823 return 0;
824
825iget_error:
826 afs_put_server(server);
827mkdir_error:
828 key_put(key);
829error:
830 d_drop(dentry);
831 _leave(" = %d", ret);
832 return ret;
833}
834
835/*
836 * remove a directory from an AFS filesystem
837 */
838static int afs_rmdir(struct inode *dir, struct dentry *dentry)
839{
840 struct afs_vnode *dvnode, *vnode;
841 struct key *key;
842 int ret;
843
844 dvnode = AFS_FS_I(dir);
845
416351f2 846 _enter("{%x:%u},{%s}",
260a9803
DH
847 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
848
849 ret = -ENAMETOOLONG;
45222b9e 850 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
851 goto error;
852
853 key = afs_request_key(dvnode->volume->cell);
854 if (IS_ERR(key)) {
855 ret = PTR_ERR(key);
856 goto error;
857 }
858
859 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
860 if (ret < 0)
861 goto rmdir_error;
862
863 if (dentry->d_inode) {
864 vnode = AFS_FS_I(dentry->d_inode);
865 clear_nlink(&vnode->vfs_inode);
866 set_bit(AFS_VNODE_DELETED, &vnode->flags);
867 afs_discard_callback_on_delete(vnode);
868 }
869
870 key_put(key);
871 _leave(" = 0");
872 return 0;
873
874rmdir_error:
875 key_put(key);
876error:
877 _leave(" = %d", ret);
878 return ret;
879}
880
881/*
882 * remove a file from an AFS filesystem
883 */
884static int afs_unlink(struct inode *dir, struct dentry *dentry)
885{
886 struct afs_vnode *dvnode, *vnode;
887 struct key *key;
888 int ret;
889
890 dvnode = AFS_FS_I(dir);
891
416351f2 892 _enter("{%x:%u},{%s}",
260a9803
DH
893 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
894
895 ret = -ENAMETOOLONG;
45222b9e 896 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
897 goto error;
898
899 key = afs_request_key(dvnode->volume->cell);
900 if (IS_ERR(key)) {
901 ret = PTR_ERR(key);
902 goto error;
903 }
904
905 if (dentry->d_inode) {
906 vnode = AFS_FS_I(dentry->d_inode);
907
908 /* make sure we have a callback promise on the victim */
909 ret = afs_validate(vnode, key);
910 if (ret < 0)
911 goto error;
912 }
913
914 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
915 if (ret < 0)
916 goto remove_error;
917
918 if (dentry->d_inode) {
919 /* if the file wasn't deleted due to excess hard links, the
920 * fileserver will break the callback promise on the file - if
921 * it had one - before it returns to us, and if it was deleted,
922 * it won't
923 *
924 * however, if we didn't have a callback promise outstanding,
925 * or it was outstanding on a different server, then it won't
926 * break it either...
927 */
928 vnode = AFS_FS_I(dentry->d_inode);
929 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
930 _debug("AFS_VNODE_DELETED");
931 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
932 _debug("AFS_VNODE_CB_BROKEN");
933 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
934 ret = afs_validate(vnode, key);
935 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
936 }
937
938 key_put(key);
939 _leave(" = 0");
940 return 0;
941
942remove_error:
943 key_put(key);
944error:
945 _leave(" = %d", ret);
946 return ret;
947}
948
949/*
950 * create a regular file on an AFS filesystem
951 */
952static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
953 struct nameidata *nd)
954{
955 struct afs_file_status status;
956 struct afs_callback cb;
957 struct afs_server *server;
958 struct afs_vnode *dvnode, *vnode;
959 struct afs_fid fid;
960 struct inode *inode;
961 struct key *key;
962 int ret;
963
964 dvnode = AFS_FS_I(dir);
965
416351f2 966 _enter("{%x:%u},{%s},%o,",
260a9803
DH
967 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
968
969 ret = -ENAMETOOLONG;
45222b9e 970 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
971 goto error;
972
973 key = afs_request_key(dvnode->volume->cell);
974 if (IS_ERR(key)) {
975 ret = PTR_ERR(key);
976 goto error;
977 }
978
979 mode |= S_IFREG;
980 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
981 mode, &fid, &status, &cb, &server);
982 if (ret < 0)
983 goto create_error;
984
985 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
986 if (IS_ERR(inode)) {
987 /* ENOMEM at a really inconvenient time - just abandon the new
988 * directory on the server */
989 ret = PTR_ERR(inode);
990 goto iget_error;
991 }
992
993 /* apply the status report we've got for the new vnode */
994 vnode = AFS_FS_I(inode);
995 spin_lock(&vnode->lock);
996 vnode->update_cnt++;
997 spin_unlock(&vnode->lock);
998 afs_vnode_finalise_status_update(vnode, server);
999 afs_put_server(server);
1000
1001 d_instantiate(dentry, inode);
1002 if (d_unhashed(dentry)) {
1003 _debug("not hashed");
1004 d_rehash(dentry);
1005 }
1006 key_put(key);
1007 _leave(" = 0");
1008 return 0;
1009
1010iget_error:
1011 afs_put_server(server);
1012create_error:
1013 key_put(key);
1014error:
1015 d_drop(dentry);
1016 _leave(" = %d", ret);
1017 return ret;
1018}
1019
1020/*
1021 * create a hard link between files in an AFS filesystem
1022 */
1023static int afs_link(struct dentry *from, struct inode *dir,
1024 struct dentry *dentry)
1025{
1026 struct afs_vnode *dvnode, *vnode;
1027 struct key *key;
1028 int ret;
1029
1030 vnode = AFS_FS_I(from->d_inode);
1031 dvnode = AFS_FS_I(dir);
1032
416351f2 1033 _enter("{%x:%u},{%x:%u},{%s}",
260a9803
DH
1034 vnode->fid.vid, vnode->fid.vnode,
1035 dvnode->fid.vid, dvnode->fid.vnode,
1036 dentry->d_name.name);
1037
1038 ret = -ENAMETOOLONG;
45222b9e 1039 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
1040 goto error;
1041
1042 key = afs_request_key(dvnode->volume->cell);
1043 if (IS_ERR(key)) {
1044 ret = PTR_ERR(key);
1045 goto error;
1046 }
1047
1048 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
1049 if (ret < 0)
1050 goto link_error;
1051
7de9c6ee 1052 ihold(&vnode->vfs_inode);
260a9803
DH
1053 d_instantiate(dentry, &vnode->vfs_inode);
1054 key_put(key);
1055 _leave(" = 0");
1056 return 0;
1057
1058link_error:
1059 key_put(key);
1060error:
1061 d_drop(dentry);
1062 _leave(" = %d", ret);
1063 return ret;
1064}
1065
1066/*
1067 * create a symlink in an AFS filesystem
1068 */
1069static int afs_symlink(struct inode *dir, struct dentry *dentry,
1070 const char *content)
1071{
1072 struct afs_file_status status;
1073 struct afs_server *server;
1074 struct afs_vnode *dvnode, *vnode;
1075 struct afs_fid fid;
1076 struct inode *inode;
1077 struct key *key;
1078 int ret;
1079
1080 dvnode = AFS_FS_I(dir);
1081
416351f2 1082 _enter("{%x:%u},{%s},%s",
260a9803
DH
1083 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
1084 content);
1085
1086 ret = -ENAMETOOLONG;
45222b9e 1087 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
1088 goto error;
1089
1090 ret = -EINVAL;
45222b9e 1091 if (strlen(content) >= AFSPATHMAX)
260a9803
DH
1092 goto error;
1093
1094 key = afs_request_key(dvnode->volume->cell);
1095 if (IS_ERR(key)) {
1096 ret = PTR_ERR(key);
1097 goto error;
1098 }
1099
1100 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1101 &fid, &status, &server);
1102 if (ret < 0)
1103 goto create_error;
1104
1105 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1106 if (IS_ERR(inode)) {
1107 /* ENOMEM at a really inconvenient time - just abandon the new
1108 * directory on the server */
1109 ret = PTR_ERR(inode);
1110 goto iget_error;
1111 }
1112
1113 /* apply the status report we've got for the new vnode */
1114 vnode = AFS_FS_I(inode);
1115 spin_lock(&vnode->lock);
1116 vnode->update_cnt++;
1117 spin_unlock(&vnode->lock);
1118 afs_vnode_finalise_status_update(vnode, server);
1119 afs_put_server(server);
1120
1121 d_instantiate(dentry, inode);
1122 if (d_unhashed(dentry)) {
1123 _debug("not hashed");
1124 d_rehash(dentry);
1125 }
1126 key_put(key);
1127 _leave(" = 0");
1128 return 0;
1129
1130iget_error:
1131 afs_put_server(server);
1132create_error:
1133 key_put(key);
1134error:
1135 d_drop(dentry);
1136 _leave(" = %d", ret);
1137 return ret;
1138}
1139
1140/*
1141 * rename a file in an AFS filesystem and/or move it between directories
1142 */
1143static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1144 struct inode *new_dir, struct dentry *new_dentry)
1145{
1146 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1147 struct key *key;
1148 int ret;
1149
1150 vnode = AFS_FS_I(old_dentry->d_inode);
1151 orig_dvnode = AFS_FS_I(old_dir);
1152 new_dvnode = AFS_FS_I(new_dir);
1153
416351f2 1154 _enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
260a9803
DH
1155 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1156 vnode->fid.vid, vnode->fid.vnode,
1157 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1158 new_dentry->d_name.name);
1159
1160 ret = -ENAMETOOLONG;
45222b9e 1161 if (new_dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
1162 goto error;
1163
1164 key = afs_request_key(orig_dvnode->volume->cell);
1165 if (IS_ERR(key)) {
1166 ret = PTR_ERR(key);
1167 goto error;
1168 }
1169
1170 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1171 old_dentry->d_name.name,
1172 new_dentry->d_name.name);
1173 if (ret < 0)
1174 goto rename_error;
1175 key_put(key);
1176 _leave(" = 0");
1177 return 0;
1178
1179rename_error:
1180 key_put(key);
1181error:
1182 d_drop(new_dentry);
1183 _leave(" = %d", ret);
1184 return ret;
1185}