ocfs2: Remove inode from ocfs2_xattr_bucket_get_name_value.
[linux-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
LT
15#include <linux/slab.h>
16#include <linux/fs.h>
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
LT
26static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
27static int afs_d_delete(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;
00d3b7a4
DH
193 struct file file = {
194 .private_data = key,
195 };
1da177e4
LT
196
197 _enter("{%lu},%lu", dir->i_ino, index);
198
00d3b7a4 199 page = read_mapping_page(dir->i_mapping, index, &file);
1da177e4 200 if (!IS_ERR(page)) {
1da177e4 201 kmap(page);
1da177e4
LT
202 if (!PageChecked(page))
203 afs_dir_check_page(dir, page);
204 if (PageError(page))
205 goto fail;
206 }
207 return page;
208
ec26815a 209fail:
1da177e4 210 afs_dir_put_page(page);
08e0e7c8 211 _leave(" = -EIO");
1da177e4 212 return ERR_PTR(-EIO);
ec26815a 213}
1da177e4 214
1da177e4
LT
215/*
216 * open an AFS directory file
217 */
218static int afs_dir_open(struct inode *inode, struct file *file)
219{
220 _enter("{%lu}", inode->i_ino);
221
2ecd05ae
AD
222 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
223 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
1da177e4 224
08e0e7c8 225 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
1da177e4
LT
226 return -ENOENT;
227
00d3b7a4 228 return afs_open(inode, file);
ec26815a 229}
1da177e4 230
1da177e4
LT
231/*
232 * deal with one block in an AFS directory
233 */
234static int afs_dir_iterate_block(unsigned *fpos,
235 union afs_dir_block *block,
236 unsigned blkoff,
237 void *cookie,
238 filldir_t filldir)
239{
240 union afs_dirent *dire;
241 unsigned offset, next, curr;
242 size_t nlen;
243 int tmp, ret;
244
245 _enter("%u,%x,%p,,",*fpos,blkoff,block);
246
247 curr = (*fpos - blkoff) / sizeof(union afs_dirent);
248
249 /* walk through the block, an entry at a time */
250 for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
251 offset < AFS_DIRENT_PER_BLOCK;
252 offset = next
253 ) {
254 next = offset + 1;
255
256 /* skip entries marked unused in the bitmap */
257 if (!(block->pagehdr.bitmap[offset / 8] &
258 (1 << (offset % 8)))) {
08e0e7c8 259 _debug("ENT[%Zu.%u]: unused",
1da177e4
LT
260 blkoff / sizeof(union afs_dir_block), offset);
261 if (offset >= curr)
262 *fpos = blkoff +
263 next * sizeof(union afs_dirent);
264 continue;
265 }
266
267 /* got a valid entry */
268 dire = &block->dirents[offset];
269 nlen = strnlen(dire->u.name,
270 sizeof(*block) -
271 offset * sizeof(union afs_dirent));
272
08e0e7c8 273 _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
1da177e4
LT
274 blkoff / sizeof(union afs_dir_block), offset,
275 (offset < curr ? "skip" : "fill"),
276 nlen, dire->u.name);
277
278 /* work out where the next possible entry is */
279 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
280 if (next >= AFS_DIRENT_PER_BLOCK) {
281 _debug("ENT[%Zu.%u]:"
282 " %u travelled beyond end dir block"
08e0e7c8 283 " (len %u/%Zu)",
1da177e4
LT
284 blkoff / sizeof(union afs_dir_block),
285 offset, next, tmp, nlen);
286 return -EIO;
287 }
288 if (!(block->pagehdr.bitmap[next / 8] &
289 (1 << (next % 8)))) {
290 _debug("ENT[%Zu.%u]:"
08e0e7c8 291 " %u unmarked extension (len %u/%Zu)",
1da177e4
LT
292 blkoff / sizeof(union afs_dir_block),
293 offset, next, tmp, nlen);
294 return -EIO;
295 }
296
08e0e7c8 297 _debug("ENT[%Zu.%u]: ext %u/%Zu",
1da177e4
LT
298 blkoff / sizeof(union afs_dir_block),
299 next, tmp, nlen);
300 next++;
301 }
302
303 /* skip if starts before the current position */
304 if (offset < curr)
305 continue;
306
307 /* found the next entry */
308 ret = filldir(cookie,
309 dire->u.name,
310 nlen,
311 blkoff + offset * sizeof(union afs_dirent),
312 ntohl(dire->u.vnode),
260a9803 313 filldir == afs_lookup_filldir ?
1da177e4
LT
314 ntohl(dire->u.unique) : DT_UNKNOWN);
315 if (ret < 0) {
316 _leave(" = 0 [full]");
317 return 0;
318 }
319
320 *fpos = blkoff + next * sizeof(union afs_dirent);
321 }
322
323 _leave(" = 1 [more]");
324 return 1;
ec26815a 325}
1da177e4 326
1da177e4 327/*
08e0e7c8 328 * iterate through the data blob that lists the contents of an AFS directory
1da177e4
LT
329 */
330static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
00d3b7a4 331 filldir_t filldir, struct key *key)
1da177e4 332{
08e0e7c8 333 union afs_dir_block *dblock;
1da177e4
LT
334 struct afs_dir_page *dbuf;
335 struct page *page;
336 unsigned blkoff, limit;
337 int ret;
338
339 _enter("{%lu},%u,,", dir->i_ino, *fpos);
340
08e0e7c8 341 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
1da177e4
LT
342 _leave(" = -ESTALE");
343 return -ESTALE;
344 }
345
346 /* round the file position up to the next entry boundary */
347 *fpos += sizeof(union afs_dirent) - 1;
348 *fpos &= ~(sizeof(union afs_dirent) - 1);
349
350 /* walk through the blocks in sequence */
351 ret = 0;
352 while (*fpos < dir->i_size) {
353 blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
354
355 /* fetch the appropriate page from the directory */
00d3b7a4 356 page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
1da177e4
LT
357 if (IS_ERR(page)) {
358 ret = PTR_ERR(page);
359 break;
360 }
361
362 limit = blkoff & ~(PAGE_SIZE - 1);
363
364 dbuf = page_address(page);
365
366 /* deal with the individual blocks stashed on this page */
367 do {
368 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
369 sizeof(union afs_dir_block)];
370 ret = afs_dir_iterate_block(fpos, dblock, blkoff,
371 cookie, filldir);
372 if (ret != 1) {
373 afs_dir_put_page(page);
374 goto out;
375 }
376
377 blkoff += sizeof(union afs_dir_block);
378
379 } while (*fpos < dir->i_size && blkoff < limit);
380
381 afs_dir_put_page(page);
382 ret = 0;
383 }
384
ec26815a 385out:
1da177e4
LT
386 _leave(" = %d", ret);
387 return ret;
ec26815a 388}
1da177e4 389
1da177e4
LT
390/*
391 * read an AFS directory
392 */
260a9803 393static int afs_readdir(struct file *file, void *cookie, filldir_t filldir)
1da177e4
LT
394{
395 unsigned fpos;
396 int ret;
397
08e0e7c8
DH
398 _enter("{%Ld,{%lu}}",
399 file->f_pos, file->f_path.dentry->d_inode->i_ino);
1da177e4 400
00d3b7a4
DH
401 ASSERT(file->private_data != NULL);
402
1da177e4 403 fpos = file->f_pos;
08e0e7c8 404 ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos,
00d3b7a4 405 cookie, filldir, file->private_data);
1da177e4
LT
406 file->f_pos = fpos;
407
408 _leave(" = %d", ret);
409 return ret;
ec26815a 410}
1da177e4 411
1da177e4
LT
412/*
413 * search the directory for a name
414 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
415 * uniquifier through dtype
416 */
260a9803
DH
417static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
418 loff_t fpos, u64 ino, unsigned dtype)
1da177e4 419{
260a9803 420 struct afs_lookup_cookie *cookie = _cookie;
1da177e4 421
08e0e7c8 422 _enter("{%s,%Zu},%s,%u,,%llu,%u",
ba3e0e1a
DM
423 cookie->name, cookie->nlen, name, nlen,
424 (unsigned long long) ino, dtype);
1da177e4 425
08e0e7c8
DH
426 /* insanity checks first */
427 BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
428 BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
429
1da177e4
LT
430 if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
431 _leave(" = 0 [no]");
432 return 0;
433 }
434
435 cookie->fid.vnode = ino;
436 cookie->fid.unique = dtype;
437 cookie->found = 1;
438
439 _leave(" = -1 [found]");
440 return -1;
ec26815a 441}
1da177e4 442
1da177e4 443/*
08e0e7c8 444 * do a lookup in a directory
260a9803 445 * - just returns the FID the dentry name maps to if found
1da177e4 446 */
08e0e7c8 447static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
00d3b7a4 448 struct afs_fid *fid, struct key *key)
1da177e4 449{
260a9803 450 struct afs_lookup_cookie cookie;
1da177e4 451 struct afs_super_info *as;
1da177e4
LT
452 unsigned fpos;
453 int ret;
454
08e0e7c8 455 _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
1da177e4
LT
456
457 as = dir->i_sb->s_fs_info;
458
459 /* search the directory */
460 cookie.name = dentry->d_name.name;
461 cookie.nlen = dentry->d_name.len;
462 cookie.fid.vid = as->volume->vid;
463 cookie.found = 0;
464
465 fpos = 0;
260a9803 466 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_lookup_filldir,
00d3b7a4 467 key);
1da177e4 468 if (ret < 0) {
08e0e7c8
DH
469 _leave(" = %d [iter]", ret);
470 return ret;
1da177e4
LT
471 }
472
473 ret = -ENOENT;
474 if (!cookie.found) {
08e0e7c8
DH
475 _leave(" = -ENOENT [not found]");
476 return -ENOENT;
1da177e4
LT
477 }
478
08e0e7c8
DH
479 *fid = cookie.fid;
480 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
481 return 0;
482}
483
484/*
485 * look up an entry in a directory
486 */
260a9803
DH
487static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
488 struct nameidata *nd)
08e0e7c8
DH
489{
490 struct afs_vnode *vnode;
491 struct afs_fid fid;
492 struct inode *inode;
00d3b7a4 493 struct key *key;
08e0e7c8
DH
494 int ret;
495
260a9803
DH
496 vnode = AFS_FS_I(dir);
497
416351f2 498 _enter("{%x:%u},%p{%s},",
260a9803
DH
499 vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
500
501 ASSERTCMP(dentry->d_inode, ==, NULL);
08e0e7c8 502
45222b9e 503 if (dentry->d_name.len >= AFSNAMEMAX) {
08e0e7c8
DH
504 _leave(" = -ENAMETOOLONG");
505 return ERR_PTR(-ENAMETOOLONG);
506 }
507
08e0e7c8
DH
508 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
509 _leave(" = -ESTALE");
510 return ERR_PTR(-ESTALE);
511 }
512
00d3b7a4
DH
513 key = afs_request_key(vnode->volume->cell);
514 if (IS_ERR(key)) {
515 _leave(" = %ld [key]", PTR_ERR(key));
e231c2ee 516 return ERR_CAST(key);
00d3b7a4
DH
517 }
518
260a9803
DH
519 ret = afs_validate(vnode, key);
520 if (ret < 0) {
521 key_put(key);
522 _leave(" = %d [val]", ret);
523 return ERR_PTR(ret);
524 }
525
00d3b7a4 526 ret = afs_do_lookup(dir, dentry, &fid, key);
1da177e4 527 if (ret < 0) {
00d3b7a4 528 key_put(key);
260a9803
DH
529 if (ret == -ENOENT) {
530 d_add(dentry, NULL);
531 _leave(" = NULL [negative]");
532 return NULL;
533 }
08e0e7c8 534 _leave(" = %d [do]", ret);
1da177e4
LT
535 return ERR_PTR(ret);
536 }
260a9803 537 dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
1da177e4 538
08e0e7c8 539 /* instantiate the dentry */
260a9803 540 inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
00d3b7a4 541 key_put(key);
08e0e7c8
DH
542 if (IS_ERR(inode)) {
543 _leave(" = %ld", PTR_ERR(inode));
e231c2ee 544 return ERR_CAST(inode);
08e0e7c8
DH
545 }
546
1da177e4 547 dentry->d_op = &afs_fs_dentry_operations;
1da177e4
LT
548
549 d_add(dentry, inode);
7a224228 550 _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%llu }",
08e0e7c8
DH
551 fid.vnode,
552 fid.unique,
1da177e4 553 dentry->d_inode->i_ino,
7a224228 554 (unsigned long long)dentry->d_inode->i_version);
1da177e4
LT
555
556 return NULL;
ec26815a 557}
1da177e4 558
1da177e4
LT
559/*
560 * check that a dentry lookup hit has found a valid entry
561 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
562 * inode
1da177e4
LT
563 */
564static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
565{
260a9803 566 struct afs_vnode *vnode, *dir;
08e0e7c8 567 struct afs_fid fid;
1da177e4 568 struct dentry *parent;
00d3b7a4 569 struct key *key;
260a9803 570 void *dir_version;
1da177e4
LT
571 int ret;
572
08e0e7c8
DH
573 vnode = AFS_FS_I(dentry->d_inode);
574
260a9803
DH
575 if (dentry->d_inode)
576 _enter("{v={%x:%u} n=%s fl=%lx},",
577 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
578 vnode->flags);
579 else
580 _enter("{neg n=%s}", dentry->d_name.name);
1da177e4 581
260a9803 582 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
00d3b7a4
DH
583 if (IS_ERR(key))
584 key = NULL;
585
1da177e4 586 /* lock down the parent dentry so we can peer at it */
08e0e7c8 587 parent = dget_parent(dentry);
260a9803 588 if (!parent->d_inode)
1da177e4
LT
589 goto out_bad;
590
260a9803 591 dir = AFS_FS_I(parent->d_inode);
1da177e4 592
260a9803
DH
593 /* validate the parent directory */
594 if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
595 afs_validate(dir, key);
596
597 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1da177e4
LT
598 _debug("%s: parent dir deleted", dentry->d_name.name);
599 goto out_bad;
600 }
601
260a9803
DH
602 dir_version = (void *) (unsigned long) dir->status.data_version;
603 if (dentry->d_fsdata == dir_version)
604 goto out_valid; /* the dir contents are unchanged */
1da177e4 605
260a9803
DH
606 _debug("dir modified");
607
608 /* search the directory for this vnode */
609 ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
610 switch (ret) {
611 case 0:
612 /* the filename maps to something */
613 if (!dentry->d_inode)
614 goto out_bad;
615 if (is_bad_inode(dentry->d_inode)) {
616 printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
617 parent->d_name.name, dentry->d_name.name);
1da177e4
LT
618 goto out_bad;
619 }
620
1da177e4
LT
621 /* if the vnode ID has changed, then the dirent points to a
622 * different file */
08e0e7c8
DH
623 if (fid.vnode != vnode->fid.vnode) {
624 _debug("%s: dirent changed [%u != %u]",
625 dentry->d_name.name, fid.vnode,
626 vnode->fid.vnode);
1da177e4
LT
627 goto not_found;
628 }
629
630 /* if the vnode ID uniqifier has changed, then the file has
260a9803
DH
631 * been deleted and replaced, and the original vnode ID has
632 * been reused */
08e0e7c8 633 if (fid.unique != vnode->fid.unique) {
7a224228 634 _debug("%s: file deleted (uq %u -> %u I:%llu)",
08e0e7c8 635 dentry->d_name.name, fid.unique,
7a224228
JNC
636 vnode->fid.unique,
637 (unsigned long long)dentry->d_inode->i_version);
08e0e7c8
DH
638 spin_lock(&vnode->lock);
639 set_bit(AFS_VNODE_DELETED, &vnode->flags);
640 spin_unlock(&vnode->lock);
260a9803 641 goto not_found;
1da177e4 642 }
260a9803 643 goto out_valid;
08e0e7c8 644
260a9803
DH
645 case -ENOENT:
646 /* the filename is unknown */
647 _debug("%s: dirent not found", dentry->d_name.name);
648 if (dentry->d_inode)
649 goto not_found;
650 goto out_valid;
1da177e4 651
260a9803
DH
652 default:
653 _debug("failed to iterate dir %s: %d",
654 parent->d_name.name, ret);
08e0e7c8
DH
655 goto out_bad;
656 }
657
ec26815a 658out_valid:
260a9803
DH
659 dentry->d_fsdata = dir_version;
660out_skip:
1da177e4 661 dput(parent);
00d3b7a4 662 key_put(key);
1da177e4
LT
663 _leave(" = 1 [valid]");
664 return 1;
665
666 /* the dirent, if it exists, now points to a different vnode */
ec26815a 667not_found:
1da177e4
LT
668 spin_lock(&dentry->d_lock);
669 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
670 spin_unlock(&dentry->d_lock);
671
ec26815a 672out_bad:
260a9803 673 if (dentry->d_inode) {
1da177e4
LT
674 /* don't unhash if we have submounts */
675 if (have_submounts(dentry))
260a9803 676 goto out_skip;
1da177e4
LT
677 }
678
1da177e4 679 _debug("dropping dentry %s/%s",
08e0e7c8
DH
680 parent->d_name.name, dentry->d_name.name);
681 shrink_dcache_parent(dentry);
1da177e4 682 d_drop(dentry);
1da177e4 683 dput(parent);
00d3b7a4 684 key_put(key);
1da177e4
LT
685
686 _leave(" = 0 [bad]");
687 return 0;
ec26815a 688}
1da177e4 689
1da177e4
LT
690/*
691 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
692 * sleep)
693 * - called from dput() when d_count is going to 0.
694 * - return 1 to request dentry be unhashed, 0 otherwise
695 */
696static int afs_d_delete(struct dentry *dentry)
697{
698 _enter("%s", dentry->d_name.name);
699
700 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
701 goto zap;
702
08e0e7c8
DH
703 if (dentry->d_inode &&
704 test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags))
1da177e4 705 goto zap;
1da177e4
LT
706
707 _leave(" = 0 [keep]");
708 return 0;
709
ec26815a 710zap:
1da177e4
LT
711 _leave(" = 1 [zap]");
712 return 1;
ec26815a 713}
260a9803
DH
714
715/*
716 * handle dentry release
717 */
718static void afs_d_release(struct dentry *dentry)
719{
720 _enter("%s", dentry->d_name.name);
721}
722
723/*
724 * create a directory on an AFS filesystem
725 */
726static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
727{
728 struct afs_file_status status;
729 struct afs_callback cb;
730 struct afs_server *server;
731 struct afs_vnode *dvnode, *vnode;
732 struct afs_fid fid;
733 struct inode *inode;
734 struct key *key;
735 int ret;
736
737 dvnode = AFS_FS_I(dir);
738
416351f2 739 _enter("{%x:%u},{%s},%o",
260a9803
DH
740 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
741
742 ret = -ENAMETOOLONG;
45222b9e 743 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
744 goto error;
745
746 key = afs_request_key(dvnode->volume->cell);
747 if (IS_ERR(key)) {
748 ret = PTR_ERR(key);
749 goto error;
750 }
751
752 mode |= S_IFDIR;
753 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
754 mode, &fid, &status, &cb, &server);
755 if (ret < 0)
756 goto mkdir_error;
757
758 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
759 if (IS_ERR(inode)) {
760 /* ENOMEM at a really inconvenient time - just abandon the new
761 * directory on the server */
762 ret = PTR_ERR(inode);
763 goto iget_error;
764 }
765
766 /* apply the status report we've got for the new vnode */
767 vnode = AFS_FS_I(inode);
768 spin_lock(&vnode->lock);
769 vnode->update_cnt++;
770 spin_unlock(&vnode->lock);
771 afs_vnode_finalise_status_update(vnode, server);
772 afs_put_server(server);
773
774 d_instantiate(dentry, inode);
775 if (d_unhashed(dentry)) {
776 _debug("not hashed");
777 d_rehash(dentry);
778 }
779 key_put(key);
780 _leave(" = 0");
781 return 0;
782
783iget_error:
784 afs_put_server(server);
785mkdir_error:
786 key_put(key);
787error:
788 d_drop(dentry);
789 _leave(" = %d", ret);
790 return ret;
791}
792
793/*
794 * remove a directory from an AFS filesystem
795 */
796static int afs_rmdir(struct inode *dir, struct dentry *dentry)
797{
798 struct afs_vnode *dvnode, *vnode;
799 struct key *key;
800 int ret;
801
802 dvnode = AFS_FS_I(dir);
803
416351f2 804 _enter("{%x:%u},{%s}",
260a9803
DH
805 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
806
807 ret = -ENAMETOOLONG;
45222b9e 808 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
809 goto error;
810
811 key = afs_request_key(dvnode->volume->cell);
812 if (IS_ERR(key)) {
813 ret = PTR_ERR(key);
814 goto error;
815 }
816
817 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
818 if (ret < 0)
819 goto rmdir_error;
820
821 if (dentry->d_inode) {
822 vnode = AFS_FS_I(dentry->d_inode);
823 clear_nlink(&vnode->vfs_inode);
824 set_bit(AFS_VNODE_DELETED, &vnode->flags);
825 afs_discard_callback_on_delete(vnode);
826 }
827
828 key_put(key);
829 _leave(" = 0");
830 return 0;
831
832rmdir_error:
833 key_put(key);
834error:
835 _leave(" = %d", ret);
836 return ret;
837}
838
839/*
840 * remove a file from an AFS filesystem
841 */
842static int afs_unlink(struct inode *dir, struct dentry *dentry)
843{
844 struct afs_vnode *dvnode, *vnode;
845 struct key *key;
846 int ret;
847
848 dvnode = AFS_FS_I(dir);
849
416351f2 850 _enter("{%x:%u},{%s}",
260a9803
DH
851 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
852
853 ret = -ENAMETOOLONG;
45222b9e 854 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
855 goto error;
856
857 key = afs_request_key(dvnode->volume->cell);
858 if (IS_ERR(key)) {
859 ret = PTR_ERR(key);
860 goto error;
861 }
862
863 if (dentry->d_inode) {
864 vnode = AFS_FS_I(dentry->d_inode);
865
866 /* make sure we have a callback promise on the victim */
867 ret = afs_validate(vnode, key);
868 if (ret < 0)
869 goto error;
870 }
871
872 ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
873 if (ret < 0)
874 goto remove_error;
875
876 if (dentry->d_inode) {
877 /* if the file wasn't deleted due to excess hard links, the
878 * fileserver will break the callback promise on the file - if
879 * it had one - before it returns to us, and if it was deleted,
880 * it won't
881 *
882 * however, if we didn't have a callback promise outstanding,
883 * or it was outstanding on a different server, then it won't
884 * break it either...
885 */
886 vnode = AFS_FS_I(dentry->d_inode);
887 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
888 _debug("AFS_VNODE_DELETED");
889 if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
890 _debug("AFS_VNODE_CB_BROKEN");
891 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
892 ret = afs_validate(vnode, key);
893 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
894 }
895
896 key_put(key);
897 _leave(" = 0");
898 return 0;
899
900remove_error:
901 key_put(key);
902error:
903 _leave(" = %d", ret);
904 return ret;
905}
906
907/*
908 * create a regular file on an AFS filesystem
909 */
910static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
911 struct nameidata *nd)
912{
913 struct afs_file_status status;
914 struct afs_callback cb;
915 struct afs_server *server;
916 struct afs_vnode *dvnode, *vnode;
917 struct afs_fid fid;
918 struct inode *inode;
919 struct key *key;
920 int ret;
921
922 dvnode = AFS_FS_I(dir);
923
416351f2 924 _enter("{%x:%u},{%s},%o,",
260a9803
DH
925 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
926
927 ret = -ENAMETOOLONG;
45222b9e 928 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
929 goto error;
930
931 key = afs_request_key(dvnode->volume->cell);
932 if (IS_ERR(key)) {
933 ret = PTR_ERR(key);
934 goto error;
935 }
936
937 mode |= S_IFREG;
938 ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
939 mode, &fid, &status, &cb, &server);
940 if (ret < 0)
941 goto create_error;
942
943 inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
944 if (IS_ERR(inode)) {
945 /* ENOMEM at a really inconvenient time - just abandon the new
946 * directory on the server */
947 ret = PTR_ERR(inode);
948 goto iget_error;
949 }
950
951 /* apply the status report we've got for the new vnode */
952 vnode = AFS_FS_I(inode);
953 spin_lock(&vnode->lock);
954 vnode->update_cnt++;
955 spin_unlock(&vnode->lock);
956 afs_vnode_finalise_status_update(vnode, server);
957 afs_put_server(server);
958
959 d_instantiate(dentry, inode);
960 if (d_unhashed(dentry)) {
961 _debug("not hashed");
962 d_rehash(dentry);
963 }
964 key_put(key);
965 _leave(" = 0");
966 return 0;
967
968iget_error:
969 afs_put_server(server);
970create_error:
971 key_put(key);
972error:
973 d_drop(dentry);
974 _leave(" = %d", ret);
975 return ret;
976}
977
978/*
979 * create a hard link between files in an AFS filesystem
980 */
981static int afs_link(struct dentry *from, struct inode *dir,
982 struct dentry *dentry)
983{
984 struct afs_vnode *dvnode, *vnode;
985 struct key *key;
986 int ret;
987
988 vnode = AFS_FS_I(from->d_inode);
989 dvnode = AFS_FS_I(dir);
990
416351f2 991 _enter("{%x:%u},{%x:%u},{%s}",
260a9803
DH
992 vnode->fid.vid, vnode->fid.vnode,
993 dvnode->fid.vid, dvnode->fid.vnode,
994 dentry->d_name.name);
995
996 ret = -ENAMETOOLONG;
45222b9e 997 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
998 goto error;
999
1000 key = afs_request_key(dvnode->volume->cell);
1001 if (IS_ERR(key)) {
1002 ret = PTR_ERR(key);
1003 goto error;
1004 }
1005
1006 ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
1007 if (ret < 0)
1008 goto link_error;
1009
1010 atomic_inc(&vnode->vfs_inode.i_count);
1011 d_instantiate(dentry, &vnode->vfs_inode);
1012 key_put(key);
1013 _leave(" = 0");
1014 return 0;
1015
1016link_error:
1017 key_put(key);
1018error:
1019 d_drop(dentry);
1020 _leave(" = %d", ret);
1021 return ret;
1022}
1023
1024/*
1025 * create a symlink in an AFS filesystem
1026 */
1027static int afs_symlink(struct inode *dir, struct dentry *dentry,
1028 const char *content)
1029{
1030 struct afs_file_status status;
1031 struct afs_server *server;
1032 struct afs_vnode *dvnode, *vnode;
1033 struct afs_fid fid;
1034 struct inode *inode;
1035 struct key *key;
1036 int ret;
1037
1038 dvnode = AFS_FS_I(dir);
1039
416351f2 1040 _enter("{%x:%u},{%s},%s",
260a9803
DH
1041 dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
1042 content);
1043
1044 ret = -ENAMETOOLONG;
45222b9e 1045 if (dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
1046 goto error;
1047
1048 ret = -EINVAL;
45222b9e 1049 if (strlen(content) >= AFSPATHMAX)
260a9803
DH
1050 goto error;
1051
1052 key = afs_request_key(dvnode->volume->cell);
1053 if (IS_ERR(key)) {
1054 ret = PTR_ERR(key);
1055 goto error;
1056 }
1057
1058 ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
1059 &fid, &status, &server);
1060 if (ret < 0)
1061 goto create_error;
1062
1063 inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
1064 if (IS_ERR(inode)) {
1065 /* ENOMEM at a really inconvenient time - just abandon the new
1066 * directory on the server */
1067 ret = PTR_ERR(inode);
1068 goto iget_error;
1069 }
1070
1071 /* apply the status report we've got for the new vnode */
1072 vnode = AFS_FS_I(inode);
1073 spin_lock(&vnode->lock);
1074 vnode->update_cnt++;
1075 spin_unlock(&vnode->lock);
1076 afs_vnode_finalise_status_update(vnode, server);
1077 afs_put_server(server);
1078
1079 d_instantiate(dentry, inode);
1080 if (d_unhashed(dentry)) {
1081 _debug("not hashed");
1082 d_rehash(dentry);
1083 }
1084 key_put(key);
1085 _leave(" = 0");
1086 return 0;
1087
1088iget_error:
1089 afs_put_server(server);
1090create_error:
1091 key_put(key);
1092error:
1093 d_drop(dentry);
1094 _leave(" = %d", ret);
1095 return ret;
1096}
1097
1098/*
1099 * rename a file in an AFS filesystem and/or move it between directories
1100 */
1101static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1102 struct inode *new_dir, struct dentry *new_dentry)
1103{
1104 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1105 struct key *key;
1106 int ret;
1107
1108 vnode = AFS_FS_I(old_dentry->d_inode);
1109 orig_dvnode = AFS_FS_I(old_dir);
1110 new_dvnode = AFS_FS_I(new_dir);
1111
416351f2 1112 _enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
260a9803
DH
1113 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1114 vnode->fid.vid, vnode->fid.vnode,
1115 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1116 new_dentry->d_name.name);
1117
1118 ret = -ENAMETOOLONG;
45222b9e 1119 if (new_dentry->d_name.len >= AFSNAMEMAX)
260a9803
DH
1120 goto error;
1121
1122 key = afs_request_key(orig_dvnode->volume->cell);
1123 if (IS_ERR(key)) {
1124 ret = PTR_ERR(key);
1125 goto error;
1126 }
1127
1128 ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
1129 old_dentry->d_name.name,
1130 new_dentry->d_name.name);
1131 if (ret < 0)
1132 goto rename_error;
1133 key_put(key);
1134 _leave(" = 0");
1135 return 0;
1136
1137rename_error:
1138 key_put(key);
1139error:
1140 d_drop(new_dentry);
1141 _leave(" = %d", ret);
1142 return ret;
1143}