Merge branch 'reset-seq' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik/libat...
[GitHub/mt8127/android_kernel_alcatel_ttab.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
LT
15#include <linux/slab.h>
16#include <linux/fs.h>
17#include <linux/pagemap.h>
00d3b7a4 18#include <linux/ctype.h>
1da177e4
LT
19#include "internal.h"
20
260a9803
DH
21static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
22 struct nameidata *nd);
1da177e4 23static int afs_dir_open(struct inode *inode, struct file *file);
260a9803 24static int afs_readdir(struct file *file, void *dirent, filldir_t filldir);
1da177e4
LT
25static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
26static int afs_d_delete(struct dentry *dentry);
260a9803
DH
27static void afs_d_release(struct dentry *dentry);
28static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
afefdbb2 29 loff_t fpos, u64 ino, unsigned dtype);
260a9803
DH
30static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
31 struct nameidata *nd);
32static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
33static int afs_rmdir(struct inode *dir, struct dentry *dentry);
34static int afs_unlink(struct inode *dir, struct dentry *dentry);
35static int afs_link(struct dentry *from, struct inode *dir,
36 struct dentry *dentry);
37static int afs_symlink(struct inode *dir, struct dentry *dentry,
38 const char *content);
39static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
40 struct inode *new_dir, struct dentry *new_dentry);
1da177e4 41
4b6f5d20 42const struct file_operations afs_dir_file_operations = {
1da177e4 43 .open = afs_dir_open,
00d3b7a4 44 .release = afs_release,
260a9803 45 .readdir = afs_readdir,
1da177e4
LT
46};
47
754661f1 48const struct inode_operations afs_dir_inode_operations = {
260a9803
DH
49 .create = afs_create,
50 .lookup = afs_lookup,
51 .link = afs_link,
52 .unlink = afs_unlink,
53 .symlink = afs_symlink,
54 .mkdir = afs_mkdir,
55 .rmdir = afs_rmdir,
56 .rename = afs_rename,
00d3b7a4 57 .permission = afs_permission,
1da177e4 58 .getattr = afs_inode_getattr,
1da177e4
LT
59};
60
61static struct dentry_operations afs_fs_dentry_operations = {
62 .d_revalidate = afs_d_revalidate,
63 .d_delete = afs_d_delete,
260a9803 64 .d_release = afs_d_release,
1da177e4
LT
65};
66
67#define AFS_DIR_HASHTBL_SIZE 128
68#define AFS_DIR_DIRENT_SIZE 32
69#define AFS_DIRENT_PER_BLOCK 64
70
71union afs_dirent {
72 struct {
73 uint8_t valid;
74 uint8_t unused[1];
75 __be16 hash_next;
76 __be32 vnode;
77 __be32 unique;
78 uint8_t name[16];
79 uint8_t overflow[4]; /* if any char of the name (inc
80 * NUL) reaches here, consume
81 * the next dirent too */
82 } u;
83 uint8_t extended_name[32];
84};
85
86/* AFS directory page header (one at the beginning of every 2048-byte chunk) */
87struct afs_dir_pagehdr {
88 __be16 npages;
89 __be16 magic;
90#define AFS_DIR_MAGIC htons(1234)
91 uint8_t nentries;
92 uint8_t bitmap[8];
93 uint8_t pad[19];
94};
95
96/* directory block layout */
97union afs_dir_block {
98
99 struct afs_dir_pagehdr pagehdr;
100
101 struct {
102 struct afs_dir_pagehdr pagehdr;
103 uint8_t alloc_ctrs[128];
104 /* dir hash table */
105 uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
106 } hdr;
107
108 union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
109};
110
111/* layout on a linux VM page */
112struct afs_dir_page {
113 union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
114};
115
260a9803 116struct afs_lookup_cookie {
1da177e4
LT
117 struct afs_fid fid;
118 const char *name;
119 size_t nlen;
120 int found;
121};
122
1da177e4
LT
123/*
124 * check that a directory page is valid
125 */
126static inline void afs_dir_check_page(struct inode *dir, struct page *page)
127{
128 struct afs_dir_page *dbuf;
129 loff_t latter;
130 int tmp, qty;
131
132#if 0
133 /* check the page count */
134 qty = desc.size / sizeof(dbuf->blocks[0]);
135 if (qty == 0)
136 goto error;
137
08e0e7c8 138 if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
1da177e4 139 printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
08e0e7c8
DH
140 __FUNCTION__, dir->i_ino, qty,
141 ntohs(dbuf->blocks[0].pagehdr.npages));
1da177e4
LT
142 goto error;
143 }
144#endif
145
146 /* determine how many magic numbers there should be in this page */
54b21a79 147 latter = dir->i_size - page_offset(page);
1da177e4
LT
148 if (latter >= PAGE_SIZE)
149 qty = PAGE_SIZE;
150 else
151 qty = latter;
152 qty /= sizeof(union afs_dir_block);
153
154 /* check them */
155 dbuf = page_address(page);
156 for (tmp = 0; tmp < qty; tmp++) {
157 if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
158 printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
159 __FUNCTION__, dir->i_ino, tmp, qty,
160 ntohs(dbuf->blocks[tmp].pagehdr.magic));
161 goto error;
162 }
163 }
164
165 SetPageChecked(page);
166 return;
167
ec26815a 168error:
1da177e4
LT
169 SetPageChecked(page);
170 SetPageError(page);
ec26815a 171}
1da177e4 172
1da177e4
LT
173/*
174 * discard a page cached in the pagecache
175 */
176static inline void afs_dir_put_page(struct page *page)
177{
178 kunmap(page);
179 page_cache_release(page);
ec26815a 180}
1da177e4 181
1da177e4
LT
182/*
183 * get a page into the pagecache
184 */
00d3b7a4
DH
185static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
186 struct key *key)
1da177e4
LT
187{
188 struct page *page;
00d3b7a4
DH
189 struct file file = {
190 .private_data = key,
191 };
1da177e4
LT
192
193 _enter("{%lu},%lu", dir->i_ino, index);
194
00d3b7a4 195 page = read_mapping_page(dir->i_mapping, index, &file);
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
480/*
481 * look up an entry in a directory
482 */
260a9803
DH
483static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
484 struct nameidata *nd)
08e0e7c8
DH
485{
486 struct afs_vnode *vnode;
487 struct afs_fid fid;
488 struct inode *inode;
00d3b7a4 489 struct key *key;
08e0e7c8
DH
490 int ret;
491
260a9803
DH
492 vnode = AFS_FS_I(dir);
493
494 _enter("{%x:%d},%p{%s},",
495 vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
496
497 ASSERTCMP(dentry->d_inode, ==, NULL);
08e0e7c8
DH
498
499 if (dentry->d_name.len > 255) {
500 _leave(" = -ENAMETOOLONG");
501 return ERR_PTR(-ENAMETOOLONG);
502 }
503
08e0e7c8
DH
504 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
505 _leave(" = -ESTALE");
506 return ERR_PTR(-ESTALE);
507 }
508
00d3b7a4
DH
509 key = afs_request_key(vnode->volume->cell);
510 if (IS_ERR(key)) {
511 _leave(" = %ld [key]", PTR_ERR(key));
512 return ERR_PTR(PTR_ERR(key));
513 }
514
260a9803
DH
515 ret = afs_validate(vnode, key);
516 if (ret < 0) {
517 key_put(key);
518 _leave(" = %d [val]", ret);
519 return ERR_PTR(ret);
520 }
521
00d3b7a4 522 ret = afs_do_lookup(dir, dentry, &fid, key);
1da177e4 523 if (ret < 0) {
00d3b7a4 524 key_put(key);
260a9803
DH
525 if (ret == -ENOENT) {
526 d_add(dentry, NULL);
527 _leave(" = NULL [negative]");
528 return NULL;
529 }
08e0e7c8 530 _leave(" = %d [do]", ret);
1da177e4
LT
531 return ERR_PTR(ret);
532 }
260a9803 533 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
1da177e4 534
08e0e7c8 535 /* instantiate the dentry */
260a9803 536 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
00d3b7a4 537 key_put(key);
08e0e7c8
DH
538 if (IS_ERR(inode)) {
539 _leave(" = %ld", PTR_ERR(inode));
540 return ERR_PTR(PTR_ERR(inode));
541 }
542
1da177e4 543 dentry->d_op = &afs_fs_dentry_operations;
1da177e4
LT
544
545 d_add(dentry, inode);
546 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
08e0e7c8
DH
547 fid.vnode,
548 fid.unique,
1da177e4
LT
549 dentry->d_inode->i_ino,
550 dentry->d_inode->i_version);
551
552 return NULL;
ec26815a 553}
1da177e4 554
1da177e4
LT
555/*
556 * check that a dentry lookup hit has found a valid entry
557 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
558 * inode
1da177e4
LT
559 */
560static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
561{
260a9803 562 struct afs_vnode *vnode, *dir;
08e0e7c8 563 struct afs_fid fid;
1da177e4 564 struct dentry *parent;
00d3b7a4 565 struct key *key;
260a9803 566 void *dir_version;
1da177e4
LT
567 int ret;
568
08e0e7c8
DH
569 vnode = AFS_FS_I(dentry->d_inode);
570
260a9803
DH
571 if (dentry->d_inode)
572 _enter("{v={%x:%u} n=%s fl=%lx},",
573 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
574 vnode->flags);
575 else
576 _enter("{neg n=%s}", dentry->d_name.name);
1da177e4 577
260a9803 578 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
00d3b7a4
DH
579 if (IS_ERR(key))
580 key = NULL;
581
1da177e4 582 /* lock down the parent dentry so we can peer at it */
08e0e7c8 583 parent = dget_parent(dentry);
260a9803 584 if (!parent->d_inode)
1da177e4
LT
585 goto out_bad;
586
260a9803 587 dir = AFS_FS_I(parent->d_inode);
1da177e4 588
260a9803
DH
589 /* validate the parent directory */
590 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
591 afs_validate(dir, key);
592
593 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1da177e4
LT
594 _debug("%s: parent dir deleted", dentry->d_name.name);
595 goto out_bad;
596 }
597
260a9803
DH
598 dir_version = (void *) (unsigned long) dir->status.data_version;
599 if (dentry->d_fsdata == dir_version)
600 goto out_valid; /* the dir contents are unchanged */
1da177e4 601
260a9803
DH
602 _debug("dir modified");
603
604 /* search the directory for this vnode */
605 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
606 switch (ret) {
607 case 0:
608 /* the filename maps to something */
609 if (!dentry->d_inode)
610 goto out_bad;
611 if (is_bad_inode(dentry->d_inode)) {
612 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
613 parent->d_name.name, dentry->d_name.name);
1da177e4
LT
614 goto out_bad;
615 }
616
1da177e4
LT
617 /* if the vnode ID has changed, then the dirent points to a
618 * different file */
08e0e7c8
DH
619 if (fid.vnode != vnode->fid.vnode) {
620 _debug("%s: dirent changed [%u != %u]",
621 dentry->d_name.name, fid.vnode,
622 vnode->fid.vnode);
1da177e4
LT
623 goto not_found;
624 }
625
626 /* if the vnode ID uniqifier has changed, then the file has
260a9803
DH
627 * been deleted and replaced, and the original vnode ID has
628 * been reused */
08e0e7c8 629 if (fid.unique != vnode->fid.unique) {
1da177e4 630 _debug("%s: file deleted (uq %u -> %u I:%lu)",
08e0e7c8 631 dentry->d_name.name, fid.unique,
260a9803 632 vnode->fid.unique, dentry->d_inode->i_version);
08e0e7c8
DH
633 spin_lock(&vnode->lock);
634 set_bit(AFS_VNODE_DELETED, &vnode->flags);
635 spin_unlock(&vnode->lock);
260a9803 636 goto not_found;
1da177e4 637 }
260a9803 638 goto out_valid;
08e0e7c8 639
260a9803
DH
640 case -ENOENT:
641 /* the filename is unknown */
642 _debug("%s: dirent not found", dentry->d_name.name);
643 if (dentry->d_inode)
644 goto not_found;
645 goto out_valid;
1da177e4 646
260a9803
DH
647 default:
648 _debug("failed to iterate dir %s: %d",
649 parent->d_name.name, ret);
08e0e7c8
DH
650 goto out_bad;
651 }
652
ec26815a 653out_valid:
260a9803
DH
654 dentry->d_fsdata = dir_version;
655out_skip:
1da177e4 656 dput(parent);
00d3b7a4 657 key_put(key);
1da177e4
LT
658 _leave(" = 1 [valid]");
659 return 1;
660
661 /* the dirent, if it exists, now points to a different vnode */
ec26815a 662not_found:
1da177e4
LT
663 spin_lock(&dentry->d_lock);
664 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
665 spin_unlock(&dentry->d_lock);
666
ec26815a 667out_bad:
260a9803 668 if (dentry->d_inode) {
1da177e4
LT
669 /* don't unhash if we have submounts */
670 if (have_submounts(dentry))
260a9803 671 goto out_skip;
1da177e4
LT
672 }
673
1da177e4 674 _debug("dropping dentry %s/%s",
08e0e7c8
DH
675 parent->d_name.name, dentry->d_name.name);
676 shrink_dcache_parent(dentry);
1da177e4 677 d_drop(dentry);
1da177e4 678 dput(parent);
00d3b7a4 679 key_put(key);
1da177e4
LT
680
681 _leave(" = 0 [bad]");
682 return 0;
ec26815a 683}
1da177e4 684
1da177e4
LT
685/*
686 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
687 * sleep)
688 * - called from dput() when d_count is going to 0.
689 * - return 1 to request dentry be unhashed, 0 otherwise
690 */
691static int afs_d_delete(struct dentry *dentry)
692{
693 _enter("%s", dentry->d_name.name);
694
695 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
696 goto zap;
697
08e0e7c8
DH
698 if (dentry->d_inode &&
699 test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags))
1da177e4 700 goto zap;
1da177e4
LT
701
702 _leave(" = 0 [keep]");
703 return 0;
704
ec26815a 705zap:
1da177e4
LT
706 _leave(" = 1 [zap]");
707 return 1;
ec26815a 708}
260a9803
DH
709
710/*
711 * handle dentry release
712 */
713static void afs_d_release(struct dentry *dentry)
714{
715 _enter("%s", dentry->d_name.name);
716}
717
718/*
719 * create a directory on an AFS filesystem
720 */
721static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
722{
723 struct afs_file_status status;
724 struct afs_callback cb;
725 struct afs_server *server;
726 struct afs_vnode *dvnode, *vnode;
727 struct afs_fid fid;
728 struct inode *inode;
729 struct key *key;
730 int ret;
731
732 dvnode = AFS_FS_I(dir);
733
734 _enter("{%x:%d},{%s},%o",
735 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
736
737 ret = -ENAMETOOLONG;
738 if (dentry->d_name.len > 255)
739 goto error;
740
741 key = afs_request_key(dvnode->volume->cell);
742 if (IS_ERR(key)) {
743 ret = PTR_ERR(key);
744 goto error;
745 }
746
747 mode |= S_IFDIR;
748 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
749 mode, &fid, &status, &cb, &server);
750 if (ret < 0)
751 goto mkdir_error;
752
753 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
754 if (IS_ERR(inode)) {
755 /* ENOMEM at a really inconvenient time - just abandon the new
756 * directory on the server */
757 ret = PTR_ERR(inode);
758 goto iget_error;
759 }
760
761 /* apply the status report we've got for the new vnode */
762 vnode = AFS_FS_I(inode);
763 spin_lock(&vnode->lock);
764 vnode->update_cnt++;
765 spin_unlock(&vnode->lock);
766 afs_vnode_finalise_status_update(vnode, server);
767 afs_put_server(server);
768
769 d_instantiate(dentry, inode);
770 if (d_unhashed(dentry)) {
771 _debug("not hashed");
772 d_rehash(dentry);
773 }
774 key_put(key);
775 _leave(" = 0");
776 return 0;
777
778iget_error:
779 afs_put_server(server);
780mkdir_error:
781 key_put(key);
782error:
783 d_drop(dentry);
784 _leave(" = %d", ret);
785 return ret;
786}
787
788/*
789 * remove a directory from an AFS filesystem
790 */
791static int afs_rmdir(struct inode *dir, struct dentry *dentry)
792{
793 struct afs_vnode *dvnode, *vnode;
794 struct key *key;
795 int ret;
796
797 dvnode = AFS_FS_I(dir);
798
799 _enter("{%x:%d},{%s}",
800 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
801
802 ret = -ENAMETOOLONG;
803 if (dentry->d_name.len > 255)
804 goto error;
805
806 key = afs_request_key(dvnode->volume->cell);
807 if (IS_ERR(key)) {
808 ret = PTR_ERR(key);
809 goto error;
810 }
811
812 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
813 if (ret < 0)
814 goto rmdir_error;
815
816 if (dentry->d_inode) {
817 vnode = AFS_FS_I(dentry->d_inode);
818 clear_nlink(&vnode->vfs_inode);
819 set_bit(AFS_VNODE_DELETED, &vnode->flags);
820 afs_discard_callback_on_delete(vnode);
821 }
822
823 key_put(key);
824 _leave(" = 0");
825 return 0;
826
827rmdir_error:
828 key_put(key);
829error:
830 _leave(" = %d", ret);
831 return ret;
832}
833
834/*
835 * remove a file from an AFS filesystem
836 */
837static int afs_unlink(struct inode *dir, struct dentry *dentry)
838{
839 struct afs_vnode *dvnode, *vnode;
840 struct key *key;
841 int ret;
842
843 dvnode = AFS_FS_I(dir);
844
845 _enter("{%x:%d},{%s}",
846 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
847
848 ret = -ENAMETOOLONG;
849 if (dentry->d_name.len > 255)
850 goto error;
851
852 key = afs_request_key(dvnode->volume->cell);
853 if (IS_ERR(key)) {
854 ret = PTR_ERR(key);
855 goto error;
856 }
857
858 if (dentry->d_inode) {
859 vnode = AFS_FS_I(dentry->d_inode);
860
861 /* make sure we have a callback promise on the victim */
862 ret = afs_validate(vnode, key);
863 if (ret < 0)
864 goto error;
865 }
866
867 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
868 if (ret < 0)
869 goto remove_error;
870
871 if (dentry->d_inode) {
872 /* if the file wasn't deleted due to excess hard links, the
873 * fileserver will break the callback promise on the file - if
874 * it had one - before it returns to us, and if it was deleted,
875 * it won't
876 *
877 * however, if we didn't have a callback promise outstanding,
878 * or it was outstanding on a different server, then it won't
879 * break it either...
880 */
881 vnode = AFS_FS_I(dentry->d_inode);
882 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
883 _debug("AFS_VNODE_DELETED");
884 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
885 _debug("AFS_VNODE_CB_BROKEN");
886 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
887 ret = afs_validate(vnode, key);
888 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
889 }
890
891 key_put(key);
892 _leave(" = 0");
893 return 0;
894
895remove_error:
896 key_put(key);
897error:
898 _leave(" = %d", ret);
899 return ret;
900}
901
902/*
903 * create a regular file on an AFS filesystem
904 */
905static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
906 struct nameidata *nd)
907{
908 struct afs_file_status status;
909 struct afs_callback cb;
910 struct afs_server *server;
911 struct afs_vnode *dvnode, *vnode;
912 struct afs_fid fid;
913 struct inode *inode;
914 struct key *key;
915 int ret;
916
917 dvnode = AFS_FS_I(dir);
918
919 _enter("{%x:%d},{%s},%o,",
920 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
921
922 ret = -ENAMETOOLONG;
923 if (dentry->d_name.len > 255)
924 goto error;
925
926 key = afs_request_key(dvnode->volume->cell);
927 if (IS_ERR(key)) {
928 ret = PTR_ERR(key);
929 goto error;
930 }
931
932 mode |= S_IFREG;
933 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
934 mode, &fid, &status, &cb, &server);
935 if (ret < 0)
936 goto create_error;
937
938 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
939 if (IS_ERR(inode)) {
940 /* ENOMEM at a really inconvenient time - just abandon the new
941 * directory on the server */
942 ret = PTR_ERR(inode);
943 goto iget_error;
944 }
945
946 /* apply the status report we've got for the new vnode */
947 vnode = AFS_FS_I(inode);
948 spin_lock(&vnode->lock);
949 vnode->update_cnt++;
950 spin_unlock(&vnode->lock);
951 afs_vnode_finalise_status_update(vnode, server);
952 afs_put_server(server);
953
954 d_instantiate(dentry, inode);
955 if (d_unhashed(dentry)) {
956 _debug("not hashed");
957 d_rehash(dentry);
958 }
959 key_put(key);
960 _leave(" = 0");
961 return 0;
962
963iget_error:
964 afs_put_server(server);
965create_error:
966 key_put(key);
967error:
968 d_drop(dentry);
969 _leave(" = %d", ret);
970 return ret;
971}
972
973/*
974 * create a hard link between files in an AFS filesystem
975 */
976static int afs_link(struct dentry *from, struct inode *dir,
977 struct dentry *dentry)
978{
979 struct afs_vnode *dvnode, *vnode;
980 struct key *key;
981 int ret;
982
983 vnode = AFS_FS_I(from->d_inode);
984 dvnode = AFS_FS_I(dir);
985
986 _enter("{%x:%d},{%x:%d},{%s}",
987 vnode->fid.vid, vnode->fid.vnode,
988 dvnode->fid.vid, dvnode->fid.vnode,
989 dentry->d_name.name);
990
991 ret = -ENAMETOOLONG;
992 if (dentry->d_name.len > 255)
993 goto error;
994
995 key = afs_request_key(dvnode->volume->cell);
996 if (IS_ERR(key)) {
997 ret = PTR_ERR(key);
998 goto error;
999 }
1000
1001 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
1002 if (ret < 0)
1003 goto link_error;
1004
1005 atomic_inc(&vnode->vfs_inode.i_count);
1006 d_instantiate(dentry, &vnode->vfs_inode);
1007 key_put(key);
1008 _leave(" = 0");
1009 return 0;
1010
1011link_error:
1012 key_put(key);
1013error:
1014 d_drop(dentry);
1015 _leave(" = %d", ret);
1016 return ret;
1017}
1018
1019/*
1020 * create a symlink in an AFS filesystem
1021 */
1022static int afs_symlink(struct inode *dir, struct dentry *dentry,
1023 const char *content)
1024{
1025 struct afs_file_status status;
1026 struct afs_server *server;
1027 struct afs_vnode *dvnode, *vnode;
1028 struct afs_fid fid;
1029 struct inode *inode;
1030 struct key *key;
1031 int ret;
1032
1033 dvnode = AFS_FS_I(dir);
1034
1035 _enter("{%x:%d},{%s},%s",
1036 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
1037 content);
1038
1039 ret = -ENAMETOOLONG;
1040 if (dentry->d_name.len > 255)
1041 goto error;
1042
1043 ret = -EINVAL;
1044 if (strlen(content) > 1023)
1045 goto error;
1046
1047 key = afs_request_key(dvnode->volume->cell);
1048 if (IS_ERR(key)) {
1049 ret = PTR_ERR(key);
1050 goto error;
1051 }
1052
1053 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1054 &fid, &status, &server);
1055 if (ret < 0)
1056 goto create_error;
1057
1058 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1059 if (IS_ERR(inode)) {
1060 /* ENOMEM at a really inconvenient time - just abandon the new
1061 * directory on the server */
1062 ret = PTR_ERR(inode);
1063 goto iget_error;
1064 }
1065
1066 /* apply the status report we've got for the new vnode */
1067 vnode = AFS_FS_I(inode);
1068 spin_lock(&vnode->lock);
1069 vnode->update_cnt++;
1070 spin_unlock(&vnode->lock);
1071 afs_vnode_finalise_status_update(vnode, server);
1072 afs_put_server(server);
1073
1074 d_instantiate(dentry, inode);
1075 if (d_unhashed(dentry)) {
1076 _debug("not hashed");
1077 d_rehash(dentry);
1078 }
1079 key_put(key);
1080 _leave(" = 0");
1081 return 0;
1082
1083iget_error:
1084 afs_put_server(server);
1085create_error:
1086 key_put(key);
1087error:
1088 d_drop(dentry);
1089 _leave(" = %d", ret);
1090 return ret;
1091}
1092
1093/*
1094 * rename a file in an AFS filesystem and/or move it between directories
1095 */
1096static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1097 struct inode *new_dir, struct dentry *new_dentry)
1098{
1099 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1100 struct key *key;
1101 int ret;
1102
1103 vnode = AFS_FS_I(old_dentry->d_inode);
1104 orig_dvnode = AFS_FS_I(old_dir);
1105 new_dvnode = AFS_FS_I(new_dir);
1106
1107 _enter("{%x:%d},{%x:%d},{%x:%d},{%s}",
1108 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1109 vnode->fid.vid, vnode->fid.vnode,
1110 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1111 new_dentry->d_name.name);
1112
1113 ret = -ENAMETOOLONG;
1114 if (new_dentry->d_name.len > 255)
1115 goto error;
1116
1117 key = afs_request_key(orig_dvnode->volume->cell);
1118 if (IS_ERR(key)) {
1119 ret = PTR_ERR(key);
1120 goto error;
1121 }
1122
1123 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1124 old_dentry->d_name.name,
1125 new_dentry->d_name.name);
1126 if (ret < 0)
1127 goto rename_error;
1128 key_put(key);
1129 _leave(" = 0");
1130 return 0;
1131
1132rename_error:
1133 key_put(key);
1134error:
1135 d_drop(new_dentry);
1136 _leave(" = %d", ret);
1137 return ret;
1138}