Merge tag 'mvebu-arm-5.2-1' of git://git.infradead.org/linux-mvebu into arm/late
[linux-2.6-block.git] / fs / afs / dir.c
CommitLineData
1da177e4
LT
1/* dir.c: AFS filesystem directory handling
2 *
f3ddee8d 3 * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
1da177e4
LT
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>
1da177e4 13#include <linux/fs.h>
34286d66 14#include <linux/namei.h>
1da177e4 15#include <linux/pagemap.h>
f3ddee8d 16#include <linux/swap.h>
00d3b7a4 17#include <linux/ctype.h>
e8edc6e0 18#include <linux/sched.h>
f3ddee8d 19#include <linux/task_io_accounting_ops.h>
1da177e4 20#include "internal.h"
4ea219a8 21#include "xdr_fs.h"
1da177e4 22
260a9803 23static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 24 unsigned int flags);
1da177e4 25static int afs_dir_open(struct inode *inode, struct file *file);
1bbae9f8 26static int afs_readdir(struct file *file, struct dir_context *ctx);
0b728e19 27static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
fe15ce44 28static int afs_d_delete(const struct dentry *dentry);
79ddbfa5 29static void afs_d_iput(struct dentry *dentry, struct inode *inode);
5cf9dd55 30static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
afefdbb2 31 loff_t fpos, u64 ino, unsigned dtype);
5cf9dd55
DH
32static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
33 loff_t fpos, u64 ino, unsigned dtype);
4acdaf27 34static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 35 bool excl);
18bb1db3 36static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
260a9803
DH
37static int afs_rmdir(struct inode *dir, struct dentry *dentry);
38static int afs_unlink(struct inode *dir, struct dentry *dentry);
39static int afs_link(struct dentry *from, struct inode *dir,
40 struct dentry *dentry);
41static int afs_symlink(struct inode *dir, struct dentry *dentry,
42 const char *content);
43static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1cd66c93
MS
44 struct inode *new_dir, struct dentry *new_dentry,
45 unsigned int flags);
f3ddee8d
DH
46static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
47static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
48 unsigned int length);
49
50static int afs_dir_set_page_dirty(struct page *page)
51{
52 BUG(); /* This should never happen. */
53}
1da177e4 54
4b6f5d20 55const struct file_operations afs_dir_file_operations = {
1da177e4 56 .open = afs_dir_open,
00d3b7a4 57 .release = afs_release,
29884eff 58 .iterate_shared = afs_readdir,
e8d6c554 59 .lock = afs_lock,
3222a3e5 60 .llseek = generic_file_llseek,
1da177e4
LT
61};
62
754661f1 63const struct inode_operations afs_dir_inode_operations = {
260a9803
DH
64 .create = afs_create,
65 .lookup = afs_lookup,
66 .link = afs_link,
67 .unlink = afs_unlink,
68 .symlink = afs_symlink,
69 .mkdir = afs_mkdir,
70 .rmdir = afs_rmdir,
2773bf00 71 .rename = afs_rename,
00d3b7a4 72 .permission = afs_permission,
416351f2 73 .getattr = afs_getattr,
31143d5d 74 .setattr = afs_setattr,
d3e3b7ea 75 .listxattr = afs_listxattr,
1da177e4
LT
76};
77
f3ddee8d
DH
78const struct address_space_operations afs_dir_aops = {
79 .set_page_dirty = afs_dir_set_page_dirty,
80 .releasepage = afs_dir_releasepage,
81 .invalidatepage = afs_dir_invalidatepage,
82};
83
d61dcce2 84const struct dentry_operations afs_fs_dentry_operations = {
1da177e4
LT
85 .d_revalidate = afs_d_revalidate,
86 .d_delete = afs_d_delete,
260a9803 87 .d_release = afs_d_release,
d18610b0 88 .d_automount = afs_d_automount,
79ddbfa5 89 .d_iput = afs_d_iput,
1da177e4
LT
90};
91
5cf9dd55
DH
92struct afs_lookup_one_cookie {
93 struct dir_context ctx;
94 struct qstr name;
95 bool found;
96 struct afs_fid fid;
97};
98
260a9803 99struct afs_lookup_cookie {
5cf9dd55
DH
100 struct dir_context ctx;
101 struct qstr name;
102 bool found;
103 bool one_only;
104 unsigned short nr_fids;
105 struct afs_file_status *statuses;
106 struct afs_callback *callbacks;
107 struct afs_fid fids[50];
1da177e4
LT
108};
109
1da177e4
LT
110/*
111 * check that a directory page is valid
112 */
f3ddee8d
DH
113static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
114 loff_t i_size)
1da177e4 115{
00317636 116 struct afs_xdr_dir_page *dbuf;
f3ddee8d 117 loff_t latter, off;
1da177e4
LT
118 int tmp, qty;
119
dab17c1a
DH
120 /* Determine how many magic numbers there should be in this page, but
121 * we must take care because the directory may change size under us.
122 */
123 off = page_offset(page);
dab17c1a
DH
124 if (i_size <= off)
125 goto checked;
126
127 latter = i_size - off;
1da177e4
LT
128 if (latter >= PAGE_SIZE)
129 qty = PAGE_SIZE;
130 else
131 qty = latter;
00317636 132 qty /= sizeof(union afs_xdr_dir_block);
1da177e4
LT
133
134 /* check them */
63a4681f 135 dbuf = kmap(page);
1da177e4 136 for (tmp = 0; tmp < qty; tmp++) {
00317636 137 if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
dab17c1a 138 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
f3ddee8d 139 __func__, dvnode->vfs_inode.i_ino, tmp, qty,
00317636 140 ntohs(dbuf->blocks[tmp].hdr.magic));
f3ddee8d 141 trace_afs_dir_check_failed(dvnode, off, i_size);
63a4681f 142 kunmap(page);
f51375cd 143 trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
1da177e4
LT
144 goto error;
145 }
63a4681f
DH
146
147 /* Make sure each block is NUL terminated so we can reasonably
148 * use string functions on it. The filenames in the page
149 * *should* be NUL-terminated anyway.
150 */
151 ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
1da177e4
LT
152 }
153
63a4681f
DH
154 kunmap(page);
155
dab17c1a 156checked:
f3ddee8d 157 afs_stat_v(dvnode, n_read_dir);
be5b82db 158 return true;
1da177e4 159
ec26815a 160error:
be5b82db 161 return false;
ec26815a 162}
1da177e4 163
445b1028
DH
164/*
165 * Check the contents of a directory that we've just read.
166 */
167static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
168{
169 struct afs_xdr_dir_page *dbuf;
170 unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
171
172 for (i = 0; i < req->nr_pages; i++)
173 if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
174 goto bad;
175 return true;
176
177bad:
178 pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
179 dvnode->fid.vid, dvnode->fid.vnode,
180 req->file_size, req->len, req->actual_len, req->remain);
181 pr_warn("DIR %llx %x %x %x\n",
182 req->pos, req->index, req->nr_pages, req->offset);
183
184 for (i = 0; i < req->nr_pages; i++) {
185 dbuf = kmap(req->pages[i]);
186 for (j = 0; j < qty; j++) {
187 union afs_xdr_dir_block *block = &dbuf->blocks[j];
188
189 pr_warn("[%02x] %32phN\n", i * qty + j, block);
190 }
191 kunmap(req->pages[i]);
192 }
193 return false;
194}
195
1da177e4
LT
196/*
197 * open an AFS directory file
198 */
199static int afs_dir_open(struct inode *inode, struct file *file)
200{
201 _enter("{%lu}", inode->i_ino);
202
00317636
DH
203 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
204 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
1da177e4 205
08e0e7c8 206 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
1da177e4
LT
207 return -ENOENT;
208
00d3b7a4 209 return afs_open(inode, file);
ec26815a 210}
1da177e4 211
f3ddee8d
DH
212/*
213 * Read the directory into the pagecache in one go, scrubbing the previous
214 * contents. The list of pages is returned, pinning them so that they don't
215 * get reclaimed during the iteration.
216 */
217static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
b61f7dcf 218 __acquires(&dvnode->validate_lock)
f3ddee8d
DH
219{
220 struct afs_read *req;
221 loff_t i_size;
222 int nr_pages, nr_inline, i, n;
223 int ret = -ENOMEM;
224
225retry:
226 i_size = i_size_read(&dvnode->vfs_inode);
227 if (i_size < 2048)
f51375cd
DH
228 return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
229 if (i_size > 2048 * 1024) {
230 trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
f3ddee8d 231 return ERR_PTR(-EFBIG);
f51375cd 232 }
f3ddee8d
DH
233
234 _enter("%llu", i_size);
235
236 /* Get a request record to hold the page list. We want to hold it
237 * inline if we can, but we don't want to make an order 1 allocation.
238 */
239 nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
240 nr_inline = nr_pages;
241 if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
242 nr_inline = 0;
243
244 req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
245 GFP_KERNEL);
246 if (!req)
247 return ERR_PTR(-ENOMEM);
248
249 refcount_set(&req->usage, 1);
250 req->nr_pages = nr_pages;
251 req->actual_len = i_size; /* May change */
252 req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
253 req->data_version = dvnode->status.data_version; /* May change */
254 if (nr_inline > 0) {
255 req->pages = req->array;
256 } else {
257 req->pages = kcalloc(nr_pages, sizeof(struct page *),
258 GFP_KERNEL);
259 if (!req->pages)
260 goto error;
261 }
262
263 /* Get a list of all the pages that hold or will hold the directory
264 * content. We need to fill in any gaps that we might find where the
265 * memory reclaimer has been at work. If there are any gaps, we will
266 * need to reread the entire directory contents.
267 */
268 i = 0;
269 do {
270 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
271 req->nr_pages - i,
272 req->pages + i);
273 _debug("find %u at %u/%u", n, i, req->nr_pages);
274 if (n == 0) {
275 gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
276
277 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
278 afs_stat_v(dvnode, n_inval);
279
280 ret = -ENOMEM;
281 req->pages[i] = __page_cache_alloc(gfp);
282 if (!req->pages[i])
283 goto error;
284 ret = add_to_page_cache_lru(req->pages[i],
285 dvnode->vfs_inode.i_mapping,
286 i, gfp);
287 if (ret < 0)
288 goto error;
289
290 set_page_private(req->pages[i], 1);
291 SetPagePrivate(req->pages[i]);
292 unlock_page(req->pages[i]);
293 i++;
294 } else {
295 i += n;
296 }
297 } while (i < req->nr_pages);
298
299 /* If we're going to reload, we need to lock all the pages to prevent
300 * races.
301 */
b61f7dcf
DH
302 ret = -ERESTARTSYS;
303 if (down_read_killable(&dvnode->validate_lock) < 0)
304 goto error;
f3ddee8d 305
b61f7dcf
DH
306 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
307 goto success;
f3ddee8d 308
b61f7dcf
DH
309 up_read(&dvnode->validate_lock);
310 if (down_write_killable(&dvnode->validate_lock) < 0)
311 goto error;
312
313 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
99987c56 314 trace_afs_reload_dir(dvnode);
f3ddee8d
DH
315 ret = afs_fetch_data(dvnode, key, req);
316 if (ret < 0)
b61f7dcf 317 goto error_unlock;
f3ddee8d
DH
318
319 task_io_account_read(PAGE_SIZE * req->nr_pages);
320
321 if (req->len < req->file_size)
322 goto content_has_grown;
323
324 /* Validate the data we just read. */
325 ret = -EIO;
445b1028
DH
326 if (!afs_dir_check_pages(dvnode, req))
327 goto error_unlock;
f3ddee8d
DH
328
329 // TODO: Trim excess pages
330
331 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
332 }
333
b61f7dcf 334 downgrade_write(&dvnode->validate_lock);
f3ddee8d 335success:
f3ddee8d
DH
336 return req;
337
f3ddee8d 338error_unlock:
b61f7dcf 339 up_write(&dvnode->validate_lock);
f3ddee8d
DH
340error:
341 afs_put_read(req);
342 _leave(" = %d", ret);
343 return ERR_PTR(ret);
344
345content_has_grown:
b61f7dcf 346 up_write(&dvnode->validate_lock);
f3ddee8d
DH
347 afs_put_read(req);
348 goto retry;
349}
350
1da177e4
LT
351/*
352 * deal with one block in an AFS directory
353 */
f51375cd
DH
354static int afs_dir_iterate_block(struct afs_vnode *dvnode,
355 struct dir_context *ctx,
00317636 356 union afs_xdr_dir_block *block,
1bbae9f8 357 unsigned blkoff)
1da177e4 358{
00317636 359 union afs_xdr_dirent *dire;
1da177e4
LT
360 unsigned offset, next, curr;
361 size_t nlen;
1bbae9f8 362 int tmp;
1da177e4 363
1bbae9f8 364 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
1da177e4 365
00317636 366 curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
1da177e4
LT
367
368 /* walk through the block, an entry at a time */
4ea219a8
DH
369 for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
370 offset < AFS_DIR_SLOTS_PER_BLOCK;
1da177e4
LT
371 offset = next
372 ) {
373 next = offset + 1;
374
375 /* skip entries marked unused in the bitmap */
00317636 376 if (!(block->hdr.bitmap[offset / 8] &
1da177e4 377 (1 << (offset % 8)))) {
5b5e0928 378 _debug("ENT[%zu.%u]: unused",
00317636 379 blkoff / sizeof(union afs_xdr_dir_block), offset);
1da177e4 380 if (offset >= curr)
1bbae9f8 381 ctx->pos = blkoff +
00317636 382 next * sizeof(union afs_xdr_dirent);
1da177e4
LT
383 continue;
384 }
385
386 /* got a valid entry */
387 dire = &block->dirents[offset];
388 nlen = strnlen(dire->u.name,
389 sizeof(*block) -
00317636 390 offset * sizeof(union afs_xdr_dirent));
1da177e4 391
5b5e0928 392 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
00317636 393 blkoff / sizeof(union afs_xdr_dir_block), offset,
1da177e4
LT
394 (offset < curr ? "skip" : "fill"),
395 nlen, dire->u.name);
396
397 /* work out where the next possible entry is */
00317636 398 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
4ea219a8 399 if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
5b5e0928 400 _debug("ENT[%zu.%u]:"
1da177e4 401 " %u travelled beyond end dir block"
5b5e0928 402 " (len %u/%zu)",
00317636 403 blkoff / sizeof(union afs_xdr_dir_block),
1da177e4 404 offset, next, tmp, nlen);
f51375cd 405 return afs_bad(dvnode, afs_file_error_dir_over_end);
1da177e4 406 }
00317636 407 if (!(block->hdr.bitmap[next / 8] &
1da177e4 408 (1 << (next % 8)))) {
5b5e0928
AD
409 _debug("ENT[%zu.%u]:"
410 " %u unmarked extension (len %u/%zu)",
00317636 411 blkoff / sizeof(union afs_xdr_dir_block),
1da177e4 412 offset, next, tmp, nlen);
f51375cd 413 return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
1da177e4
LT
414 }
415
5b5e0928 416 _debug("ENT[%zu.%u]: ext %u/%zu",
00317636 417 blkoff / sizeof(union afs_xdr_dir_block),
1da177e4
LT
418 next, tmp, nlen);
419 next++;
420 }
421
422 /* skip if starts before the current position */
423 if (offset < curr)
424 continue;
425
426 /* found the next entry */
1bbae9f8 427 if (!dir_emit(ctx, dire->u.name, nlen,
1da177e4 428 ntohl(dire->u.vnode),
5cf9dd55
DH
429 (ctx->actor == afs_lookup_filldir ||
430 ctx->actor == afs_lookup_one_filldir)?
1bbae9f8 431 ntohl(dire->u.unique) : DT_UNKNOWN)) {
1da177e4
LT
432 _leave(" = 0 [full]");
433 return 0;
434 }
435
00317636 436 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
1da177e4
LT
437 }
438
439 _leave(" = 1 [more]");
440 return 1;
ec26815a 441}
1da177e4 442
1da177e4 443/*
08e0e7c8 444 * iterate through the data blob that lists the contents of an AFS directory
1da177e4 445 */
1bbae9f8
AV
446static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
447 struct key *key)
1da177e4 448{
f3ddee8d 449 struct afs_vnode *dvnode = AFS_FS_I(dir);
00317636
DH
450 struct afs_xdr_dir_page *dbuf;
451 union afs_xdr_dir_block *dblock;
f3ddee8d 452 struct afs_read *req;
1da177e4
LT
453 struct page *page;
454 unsigned blkoff, limit;
455 int ret;
456
1bbae9f8 457 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
1da177e4 458
08e0e7c8 459 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
1da177e4
LT
460 _leave(" = -ESTALE");
461 return -ESTALE;
462 }
463
f3ddee8d
DH
464 req = afs_read_dir(dvnode, key);
465 if (IS_ERR(req))
466 return PTR_ERR(req);
467
1da177e4 468 /* round the file position up to the next entry boundary */
00317636
DH
469 ctx->pos += sizeof(union afs_xdr_dirent) - 1;
470 ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
1da177e4
LT
471
472 /* walk through the blocks in sequence */
473 ret = 0;
f3ddee8d 474 while (ctx->pos < req->actual_len) {
00317636 475 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
1da177e4 476
f3ddee8d
DH
477 /* Fetch the appropriate page from the directory and re-add it
478 * to the LRU.
479 */
480 page = req->pages[blkoff / PAGE_SIZE];
481 if (!page) {
f51375cd 482 ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
1da177e4
LT
483 break;
484 }
f3ddee8d 485 mark_page_accessed(page);
1da177e4
LT
486
487 limit = blkoff & ~(PAGE_SIZE - 1);
488
f3ddee8d 489 dbuf = kmap(page);
1da177e4
LT
490
491 /* deal with the individual blocks stashed on this page */
492 do {
493 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
00317636 494 sizeof(union afs_xdr_dir_block)];
f51375cd 495 ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
1da177e4 496 if (ret != 1) {
f3ddee8d 497 kunmap(page);
1da177e4
LT
498 goto out;
499 }
500
00317636 501 blkoff += sizeof(union afs_xdr_dir_block);
1da177e4 502
1bbae9f8 503 } while (ctx->pos < dir->i_size && blkoff < limit);
1da177e4 504
f3ddee8d 505 kunmap(page);
1da177e4
LT
506 ret = 0;
507 }
508
ec26815a 509out:
b61f7dcf 510 up_read(&dvnode->validate_lock);
f3ddee8d 511 afs_put_read(req);
1da177e4
LT
512 _leave(" = %d", ret);
513 return ret;
ec26815a 514}
1da177e4 515
1da177e4
LT
516/*
517 * read an AFS directory
518 */
1bbae9f8 519static int afs_readdir(struct file *file, struct dir_context *ctx)
1da177e4 520{
215804a9 521 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
ec26815a 522}
1da177e4 523
1da177e4 524/*
5cf9dd55 525 * Search the directory for a single name
1da177e4
LT
526 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
527 * uniquifier through dtype
528 */
5cf9dd55
DH
529static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
530 int nlen, loff_t fpos, u64 ino, unsigned dtype)
1da177e4 531{
5cf9dd55
DH
532 struct afs_lookup_one_cookie *cookie =
533 container_of(ctx, struct afs_lookup_one_cookie, ctx);
1da177e4 534
1bbae9f8
AV
535 _enter("{%s,%u},%s,%u,,%llu,%u",
536 cookie->name.name, cookie->name.len, name, nlen,
ba3e0e1a 537 (unsigned long long) ino, dtype);
1da177e4 538
08e0e7c8 539 /* insanity checks first */
00317636
DH
540 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
541 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
08e0e7c8 542
1bbae9f8
AV
543 if (cookie->name.len != nlen ||
544 memcmp(cookie->name.name, name, nlen) != 0) {
1da177e4
LT
545 _leave(" = 0 [no]");
546 return 0;
547 }
548
549 cookie->fid.vnode = ino;
550 cookie->fid.unique = dtype;
551 cookie->found = 1;
552
553 _leave(" = -1 [found]");
554 return -1;
ec26815a 555}
1da177e4 556
1da177e4 557/*
5cf9dd55 558 * Do a lookup of a single name in a directory
260a9803 559 * - just returns the FID the dentry name maps to if found
1da177e4 560 */
5cf9dd55
DH
561static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
562 struct afs_fid *fid, struct key *key)
1da177e4 563{
1bbae9f8 564 struct afs_super_info *as = dir->i_sb->s_fs_info;
5cf9dd55
DH
565 struct afs_lookup_one_cookie cookie = {
566 .ctx.actor = afs_lookup_one_filldir,
1bbae9f8
AV
567 .name = dentry->d_name,
568 .fid.vid = as->volume->vid
569 };
1da177e4
LT
570 int ret;
571
a455589f 572 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
1da177e4 573
1da177e4 574 /* search the directory */
1bbae9f8 575 ret = afs_dir_iterate(dir, &cookie.ctx, key);
1da177e4 576 if (ret < 0) {
08e0e7c8
DH
577 _leave(" = %d [iter]", ret);
578 return ret;
1da177e4
LT
579 }
580
581 ret = -ENOENT;
582 if (!cookie.found) {
08e0e7c8
DH
583 _leave(" = -ENOENT [not found]");
584 return -ENOENT;
1da177e4
LT
585 }
586
08e0e7c8 587 *fid = cookie.fid;
3b6492df 588 _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
08e0e7c8
DH
589 return 0;
590}
591
5cf9dd55
DH
592/*
593 * search the directory for a name
594 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
595 * uniquifier through dtype
596 */
597static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
598 int nlen, loff_t fpos, u64 ino, unsigned dtype)
599{
600 struct afs_lookup_cookie *cookie =
601 container_of(ctx, struct afs_lookup_cookie, ctx);
602 int ret;
603
604 _enter("{%s,%u},%s,%u,,%llu,%u",
605 cookie->name.name, cookie->name.len, name, nlen,
606 (unsigned long long) ino, dtype);
607
608 /* insanity checks first */
00317636
DH
609 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
610 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
5cf9dd55
DH
611
612 if (cookie->found) {
613 if (cookie->nr_fids < 50) {
614 cookie->fids[cookie->nr_fids].vnode = ino;
615 cookie->fids[cookie->nr_fids].unique = dtype;
616 cookie->nr_fids++;
617 }
618 } else if (cookie->name.len == nlen &&
619 memcmp(cookie->name.name, name, nlen) == 0) {
620 cookie->fids[0].vnode = ino;
621 cookie->fids[0].unique = dtype;
622 cookie->found = 1;
623 if (cookie->one_only)
624 return -1;
625 }
626
627 ret = cookie->nr_fids >= 50 ? -1 : 0;
628 _leave(" = %d", ret);
629 return ret;
630}
631
632/*
633 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
634 * files in one go and create inodes for them. The inode of the file we were
635 * asked for is returned.
636 */
637static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
638 struct key *key)
639{
640 struct afs_lookup_cookie *cookie;
641 struct afs_cb_interest *cbi = NULL;
642 struct afs_super_info *as = dir->i_sb->s_fs_info;
643 struct afs_iget_data data;
644 struct afs_fs_cursor fc;
645 struct afs_vnode *dvnode = AFS_FS_I(dir);
646 struct inode *inode = NULL;
647 int ret, i;
648
649 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
650
651 cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
652 if (!cookie)
653 return ERR_PTR(-ENOMEM);
654
655 cookie->ctx.actor = afs_lookup_filldir;
656 cookie->name = dentry->d_name;
657 cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
658
659 read_seqlock_excl(&dvnode->cb_lock);
660 if (dvnode->cb_interest &&
661 dvnode->cb_interest->server &&
662 test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
663 cookie->one_only = true;
664 read_sequnlock_excl(&dvnode->cb_lock);
665
666 for (i = 0; i < 50; i++)
667 cookie->fids[i].vid = as->volume->vid;
668
669 /* search the directory */
670 ret = afs_dir_iterate(dir, &cookie->ctx, key);
671 if (ret < 0) {
672 inode = ERR_PTR(ret);
673 goto out;
674 }
675
676 inode = ERR_PTR(-ENOENT);
677 if (!cookie->found)
678 goto out;
679
680 /* Check to see if we already have an inode for the primary fid. */
681 data.volume = dvnode->volume;
682 data.fid = cookie->fids[0];
683 inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
684 if (inode)
685 goto out;
686
687 /* Need space for examining all the selected files */
688 inode = ERR_PTR(-ENOMEM);
689 cookie->statuses = kcalloc(cookie->nr_fids, sizeof(struct afs_file_status),
690 GFP_KERNEL);
691 if (!cookie->statuses)
692 goto out;
693
694 cookie->callbacks = kcalloc(cookie->nr_fids, sizeof(struct afs_callback),
695 GFP_KERNEL);
696 if (!cookie->callbacks)
697 goto out_s;
698
699 /* Try FS.InlineBulkStatus first. Abort codes for the individual
700 * lookups contained therein are stored in the reply without aborting
701 * the whole operation.
702 */
703 if (cookie->one_only)
704 goto no_inline_bulk_status;
705
706 inode = ERR_PTR(-ERESTARTSYS);
707 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
708 while (afs_select_fileserver(&fc)) {
709 if (test_bit(AFS_SERVER_FL_NO_IBULK,
710 &fc.cbi->server->flags)) {
711 fc.ac.abort_code = RX_INVALID_OPERATION;
712 fc.ac.error = -ECONNABORTED;
713 break;
714 }
715 afs_fs_inline_bulk_status(&fc,
716 afs_v2net(dvnode),
717 cookie->fids,
718 cookie->statuses,
719 cookie->callbacks,
720 cookie->nr_fids, NULL);
721 }
722
723 if (fc.ac.error == 0)
724 cbi = afs_get_cb_interest(fc.cbi);
725 if (fc.ac.abort_code == RX_INVALID_OPERATION)
726 set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
727 inode = ERR_PTR(afs_end_vnode_operation(&fc));
728 }
729
730 if (!IS_ERR(inode))
731 goto success;
732 if (fc.ac.abort_code != RX_INVALID_OPERATION)
733 goto out_c;
734
735no_inline_bulk_status:
736 /* We could try FS.BulkStatus next, but this aborts the entire op if
737 * any of the lookups fails - so, for the moment, revert to
738 * FS.FetchStatus for just the primary fid.
739 */
740 cookie->nr_fids = 1;
741 inode = ERR_PTR(-ERESTARTSYS);
742 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
743 while (afs_select_fileserver(&fc)) {
744 afs_fs_fetch_status(&fc,
745 afs_v2net(dvnode),
746 cookie->fids,
747 cookie->statuses,
748 cookie->callbacks,
749 NULL);
750 }
751
752 if (fc.ac.error == 0)
753 cbi = afs_get_cb_interest(fc.cbi);
754 inode = ERR_PTR(afs_end_vnode_operation(&fc));
755 }
756
757 if (IS_ERR(inode))
758 goto out_c;
759
760 for (i = 0; i < cookie->nr_fids; i++)
761 cookie->statuses[i].abort_code = 0;
762
763success:
764 /* Turn all the files into inodes and save the first one - which is the
765 * one we actually want.
766 */
767 if (cookie->statuses[0].abort_code != 0)
768 inode = ERR_PTR(afs_abort_to_error(cookie->statuses[0].abort_code));
769
770 for (i = 0; i < cookie->nr_fids; i++) {
771 struct inode *ti;
772
773 if (cookie->statuses[i].abort_code != 0)
774 continue;
775
776 ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
777 &cookie->statuses[i],
778 &cookie->callbacks[i],
b134d687 779 cbi, dvnode);
5cf9dd55
DH
780 if (i == 0) {
781 inode = ti;
782 } else {
783 if (!IS_ERR(ti))
784 iput(ti);
785 }
786 }
787
788out_c:
789 afs_put_cb_interest(afs_v2net(dvnode), cbi);
790 kfree(cookie->callbacks);
791out_s:
792 kfree(cookie->statuses);
793out:
794 kfree(cookie);
795 return inode;
796}
797
6f8880d8
DH
798/*
799 * Look up an entry in a directory with @sys substitution.
800 */
801static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
802 struct key *key)
803{
804 struct afs_sysnames *subs;
805 struct afs_net *net = afs_i2net(dir);
806 struct dentry *ret;
807 char *buf, *p, *name;
808 int len, i;
809
810 _enter("");
811
812 ret = ERR_PTR(-ENOMEM);
813 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
814 if (!buf)
815 goto out_p;
816 if (dentry->d_name.len > 4) {
817 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
818 p += dentry->d_name.len - 4;
819 }
820
821 /* There is an ordered list of substitutes that we have to try. */
822 read_lock(&net->sysnames_lock);
823 subs = net->sysnames;
824 refcount_inc(&subs->usage);
825 read_unlock(&net->sysnames_lock);
826
827 for (i = 0; i < subs->nr; i++) {
828 name = subs->subs[i];
829 len = dentry->d_name.len - 4 + strlen(name);
830 if (len >= AFSNAMEMAX) {
831 ret = ERR_PTR(-ENAMETOOLONG);
832 goto out_s;
833 }
834
835 strcpy(p, name);
836 ret = lookup_one_len(buf, dentry->d_parent, len);
837 if (IS_ERR(ret) || d_is_positive(ret))
838 goto out_s;
839 dput(ret);
840 }
841
842 /* We don't want to d_add() the @sys dentry here as we don't want to
843 * the cached dentry to hide changes to the sysnames list.
844 */
845 ret = NULL;
846out_s:
847 afs_put_sysnames(subs);
848 kfree(buf);
849out_p:
850 key_put(key);
851 return ret;
852}
853
08e0e7c8
DH
854/*
855 * look up an entry in a directory
856 */
260a9803 857static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 858 unsigned int flags)
08e0e7c8 859{
5cf9dd55 860 struct afs_vnode *dvnode = AFS_FS_I(dir);
08e0e7c8 861 struct inode *inode;
34b2a88f 862 struct dentry *d;
00d3b7a4 863 struct key *key;
08e0e7c8
DH
864 int ret;
865
3b6492df 866 _enter("{%llx:%llu},%p{%pd},",
5cf9dd55 867 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
260a9803 868
2b0143b5 869 ASSERTCMP(d_inode(dentry), ==, NULL);
08e0e7c8 870
45222b9e 871 if (dentry->d_name.len >= AFSNAMEMAX) {
08e0e7c8
DH
872 _leave(" = -ENAMETOOLONG");
873 return ERR_PTR(-ENAMETOOLONG);
874 }
875
5cf9dd55 876 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
08e0e7c8
DH
877 _leave(" = -ESTALE");
878 return ERR_PTR(-ESTALE);
879 }
880
5cf9dd55 881 key = afs_request_key(dvnode->volume->cell);
00d3b7a4
DH
882 if (IS_ERR(key)) {
883 _leave(" = %ld [key]", PTR_ERR(key));
e231c2ee 884 return ERR_CAST(key);
00d3b7a4
DH
885 }
886
5cf9dd55 887 ret = afs_validate(dvnode, key);
260a9803
DH
888 if (ret < 0) {
889 key_put(key);
890 _leave(" = %d [val]", ret);
891 return ERR_PTR(ret);
892 }
893
6f8880d8
DH
894 if (dentry->d_name.len >= 4 &&
895 dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
896 dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
897 dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
898 dentry->d_name.name[dentry->d_name.len - 1] == 's')
899 return afs_lookup_atsys(dir, dentry, key);
900
d55b4da4 901 afs_stat_v(dvnode, n_lookup);
5cf9dd55 902 inode = afs_do_lookup(dir, dentry, key);
00d3b7a4 903 key_put(key);
34b2a88f
AV
904 if (inode == ERR_PTR(-ENOENT)) {
905 inode = afs_try_auto_mntpt(dentry, dir);
34b2a88f
AV
906 } else {
907 dentry->d_fsdata =
908 (void *)(unsigned long)dvnode->status.data_version;
08e0e7c8 909 }
34b2a88f 910 d = d_splice_alias(inode, dentry);
80548b03 911 if (!IS_ERR_OR_NULL(d)) {
34b2a88f 912 d->d_fsdata = dentry->d_fsdata;
80548b03
DH
913 trace_afs_lookup(dvnode, &d->d_name,
914 inode ? AFS_FS_I(inode) : NULL);
915 } else {
916 trace_afs_lookup(dvnode, &dentry->d_name,
917 inode ? AFS_FS_I(inode) : NULL);
918 }
34b2a88f 919 return d;
ec26815a 920}
1da177e4 921
1da177e4
LT
922/*
923 * check that a dentry lookup hit has found a valid entry
924 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
925 * inode
1da177e4 926 */
0b728e19 927static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
1da177e4 928{
260a9803 929 struct afs_vnode *vnode, *dir;
dd0d9a46 930 struct afs_fid uninitialized_var(fid);
1da177e4 931 struct dentry *parent;
c435ee34 932 struct inode *inode;
00d3b7a4 933 struct key *key;
a4ff7401 934 long dir_version, de_version;
1da177e4
LT
935 int ret;
936
0b728e19 937 if (flags & LOOKUP_RCU)
34286d66
NP
938 return -ECHILD;
939
c435ee34
DH
940 if (d_really_is_positive(dentry)) {
941 vnode = AFS_FS_I(d_inode(dentry));
3b6492df 942 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
a455589f 943 vnode->fid.vid, vnode->fid.vnode, dentry,
260a9803 944 vnode->flags);
c435ee34 945 } else {
a455589f 946 _enter("{neg n=%pd}", dentry);
c435ee34 947 }
1da177e4 948
260a9803 949 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
00d3b7a4
DH
950 if (IS_ERR(key))
951 key = NULL;
952
c435ee34
DH
953 if (d_really_is_positive(dentry)) {
954 inode = d_inode(dentry);
955 if (inode) {
956 vnode = AFS_FS_I(inode);
957 afs_validate(vnode, key);
958 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
959 goto out_bad;
960 }
961 }
962
1da177e4 963 /* lock down the parent dentry so we can peer at it */
08e0e7c8 964 parent = dget_parent(dentry);
2b0143b5 965 dir = AFS_FS_I(d_inode(parent));
1da177e4 966
260a9803 967 /* validate the parent directory */
c435ee34 968 afs_validate(dir, key);
260a9803
DH
969
970 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
a455589f 971 _debug("%pd: parent dir deleted", dentry);
c435ee34 972 goto out_bad_parent;
1da177e4
LT
973 }
974
a4ff7401
DH
975 /* We only need to invalidate a dentry if the server's copy changed
976 * behind our back. If we made the change, it's no problem. Note that
977 * on a 32-bit system, we only have 32 bits in the dentry to store the
978 * version.
979 */
980 dir_version = (long)dir->status.data_version;
981 de_version = (long)dentry->d_fsdata;
982 if (de_version == dir_version)
983 goto out_valid;
984
985 dir_version = (long)dir->invalid_before;
986 if (de_version - dir_version >= 0)
987 goto out_valid;
1da177e4 988
260a9803 989 _debug("dir modified");
d55b4da4 990 afs_stat_v(dir, n_reval);
260a9803
DH
991
992 /* search the directory for this vnode */
5cf9dd55 993 ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
260a9803
DH
994 switch (ret) {
995 case 0:
996 /* the filename maps to something */
2b0143b5 997 if (d_really_is_negative(dentry))
c435ee34
DH
998 goto out_bad_parent;
999 inode = d_inode(dentry);
1000 if (is_bad_inode(inode)) {
a455589f
AV
1001 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1002 dentry);
c435ee34 1003 goto out_bad_parent;
1da177e4
LT
1004 }
1005
c435ee34
DH
1006 vnode = AFS_FS_I(inode);
1007
1da177e4
LT
1008 /* if the vnode ID has changed, then the dirent points to a
1009 * different file */
08e0e7c8 1010 if (fid.vnode != vnode->fid.vnode) {
3b6492df 1011 _debug("%pd: dirent changed [%llu != %llu]",
a455589f 1012 dentry, fid.vnode,
08e0e7c8 1013 vnode->fid.vnode);
1da177e4
LT
1014 goto not_found;
1015 }
1016
1017 /* if the vnode ID uniqifier has changed, then the file has
260a9803
DH
1018 * been deleted and replaced, and the original vnode ID has
1019 * been reused */
08e0e7c8 1020 if (fid.unique != vnode->fid.unique) {
a455589f
AV
1021 _debug("%pd: file deleted (uq %u -> %u I:%u)",
1022 dentry, fid.unique,
7a224228 1023 vnode->fid.unique,
c435ee34
DH
1024 vnode->vfs_inode.i_generation);
1025 write_seqlock(&vnode->cb_lock);
08e0e7c8 1026 set_bit(AFS_VNODE_DELETED, &vnode->flags);
c435ee34 1027 write_sequnlock(&vnode->cb_lock);
260a9803 1028 goto not_found;
1da177e4 1029 }
260a9803 1030 goto out_valid;
08e0e7c8 1031
260a9803
DH
1032 case -ENOENT:
1033 /* the filename is unknown */
a455589f 1034 _debug("%pd: dirent not found", dentry);
2b0143b5 1035 if (d_really_is_positive(dentry))
260a9803
DH
1036 goto not_found;
1037 goto out_valid;
1da177e4 1038
260a9803 1039 default:
a455589f
AV
1040 _debug("failed to iterate dir %pd: %d",
1041 parent, ret);
c435ee34 1042 goto out_bad_parent;
08e0e7c8
DH
1043 }
1044
ec26815a 1045out_valid:
a4ff7401 1046 dentry->d_fsdata = (void *)dir_version;
1da177e4 1047 dput(parent);
00d3b7a4 1048 key_put(key);
1da177e4
LT
1049 _leave(" = 1 [valid]");
1050 return 1;
1051
1052 /* the dirent, if it exists, now points to a different vnode */
ec26815a 1053not_found:
1da177e4
LT
1054 spin_lock(&dentry->d_lock);
1055 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1056 spin_unlock(&dentry->d_lock);
1057
c435ee34 1058out_bad_parent:
a455589f 1059 _debug("dropping dentry %pd2", dentry);
1da177e4 1060 dput(parent);
c435ee34 1061out_bad:
00d3b7a4 1062 key_put(key);
1da177e4
LT
1063
1064 _leave(" = 0 [bad]");
1065 return 0;
ec26815a 1066}
1da177e4 1067
1da177e4
LT
1068/*
1069 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1070 * sleep)
1071 * - called from dput() when d_count is going to 0.
1072 * - return 1 to request dentry be unhashed, 0 otherwise
1073 */
fe15ce44 1074static int afs_d_delete(const struct dentry *dentry)
1da177e4 1075{
a455589f 1076 _enter("%pd", dentry);
1da177e4
LT
1077
1078 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1079 goto zap;
1080
2b0143b5
DH
1081 if (d_really_is_positive(dentry) &&
1082 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
1083 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
bec5eb61 1084 goto zap;
1da177e4
LT
1085
1086 _leave(" = 0 [keep]");
1087 return 0;
1088
ec26815a 1089zap:
1da177e4
LT
1090 _leave(" = 1 [zap]");
1091 return 1;
ec26815a 1092}
260a9803 1093
79ddbfa5
DH
1094/*
1095 * Clean up sillyrename files on dentry removal.
1096 */
1097static void afs_d_iput(struct dentry *dentry, struct inode *inode)
1098{
1099 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1100 afs_silly_iput(dentry, inode);
1101 iput(inode);
1102}
1103
260a9803
DH
1104/*
1105 * handle dentry release
1106 */
66c7e1d3 1107void afs_d_release(struct dentry *dentry)
260a9803 1108{
a455589f 1109 _enter("%pd", dentry);
260a9803
DH
1110}
1111
d2ddc776
DH
1112/*
1113 * Create a new inode for create/mkdir/symlink
1114 */
1115static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1116 struct dentry *new_dentry,
1117 struct afs_fid *newfid,
1118 struct afs_file_status *newstatus,
1119 struct afs_callback *newcb)
1120{
5a813276 1121 struct afs_vnode *vnode;
d2ddc776
DH
1122 struct inode *inode;
1123
1124 if (fc->ac.error < 0)
1125 return;
1126
1127 inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
b134d687 1128 newfid, newstatus, newcb, fc->cbi, fc->vnode);
d2ddc776
DH
1129 if (IS_ERR(inode)) {
1130 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1131 * the new directory on the server.
1132 */
1133 fc->ac.error = PTR_ERR(inode);
1134 return;
1135 }
1136
5a813276
DH
1137 vnode = AFS_FS_I(inode);
1138 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
00671912 1139 afs_vnode_commit_status(fc, vnode, 0);
73116df7 1140 d_instantiate(new_dentry, inode);
d2ddc776
DH
1141}
1142
260a9803
DH
1143/*
1144 * create a directory on an AFS filesystem
1145 */
18bb1db3 1146static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
260a9803 1147{
d2ddc776
DH
1148 struct afs_file_status newstatus;
1149 struct afs_fs_cursor fc;
1150 struct afs_callback newcb;
1151 struct afs_vnode *dvnode = AFS_FS_I(dir);
1152 struct afs_fid newfid;
260a9803 1153 struct key *key;
63a4681f 1154 u64 data_version = dvnode->status.data_version;
260a9803
DH
1155 int ret;
1156
d2ddc776 1157 mode |= S_IFDIR;
260a9803 1158
3b6492df 1159 _enter("{%llx:%llu},{%pd},%ho",
a455589f 1160 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
260a9803 1161
260a9803
DH
1162 key = afs_request_key(dvnode->volume->cell);
1163 if (IS_ERR(key)) {
1164 ret = PTR_ERR(key);
1165 goto error;
1166 }
1167
d2ddc776
DH
1168 ret = -ERESTARTSYS;
1169 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1170 while (afs_select_fileserver(&fc)) {
68251f0a 1171 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
63a4681f 1172 afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
d2ddc776
DH
1173 &newfid, &newstatus, &newcb);
1174 }
260a9803 1175
d2ddc776
DH
1176 afs_check_for_remote_deletion(&fc, fc.vnode);
1177 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1178 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1179 ret = afs_end_vnode_operation(&fc);
1180 if (ret < 0)
1181 goto error_key;
4433b691
DH
1182 } else {
1183 goto error_key;
260a9803
DH
1184 }
1185
63a4681f
DH
1186 if (ret == 0 &&
1187 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1188 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1189 afs_edit_dir_for_create);
1190
260a9803
DH
1191 key_put(key);
1192 _leave(" = 0");
1193 return 0;
1194
d2ddc776 1195error_key:
260a9803
DH
1196 key_put(key);
1197error:
1198 d_drop(dentry);
1199 _leave(" = %d", ret);
1200 return ret;
1201}
1202
d2ddc776
DH
1203/*
1204 * Remove a subdir from a directory.
1205 */
1206static void afs_dir_remove_subdir(struct dentry *dentry)
1207{
1208 if (d_really_is_positive(dentry)) {
1209 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1210
1211 clear_nlink(&vnode->vfs_inode);
1212 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1213 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
63a4681f 1214 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
d2ddc776
DH
1215 }
1216}
1217
260a9803
DH
1218/*
1219 * remove a directory from an AFS filesystem
1220 */
1221static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1222{
d2ddc776 1223 struct afs_fs_cursor fc;
f58db83f 1224 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
260a9803 1225 struct key *key;
63a4681f 1226 u64 data_version = dvnode->status.data_version;
260a9803
DH
1227 int ret;
1228
3b6492df 1229 _enter("{%llx:%llu},{%pd}",
a455589f 1230 dvnode->fid.vid, dvnode->fid.vnode, dentry);
260a9803 1231
260a9803
DH
1232 key = afs_request_key(dvnode->volume->cell);
1233 if (IS_ERR(key)) {
1234 ret = PTR_ERR(key);
1235 goto error;
1236 }
1237
f58db83f
DH
1238 /* Try to make sure we have a callback promise on the victim. */
1239 if (d_really_is_positive(dentry)) {
1240 vnode = AFS_FS_I(d_inode(dentry));
1241 ret = afs_validate(vnode, key);
1242 if (ret < 0)
1243 goto error_key;
1244 }
1245
79ddbfa5
DH
1246 if (vnode) {
1247 ret = down_write_killable(&vnode->rmdir_lock);
1248 if (ret < 0)
1249 goto error_key;
1250 }
1251
d2ddc776
DH
1252 ret = -ERESTARTSYS;
1253 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1254 while (afs_select_fileserver(&fc)) {
68251f0a 1255 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
30062bd1 1256 afs_fs_remove(&fc, vnode, dentry->d_name.name, true,
63a4681f 1257 data_version);
d2ddc776 1258 }
260a9803 1259
d2ddc776
DH
1260 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1261 ret = afs_end_vnode_operation(&fc);
63a4681f 1262 if (ret == 0) {
d2ddc776 1263 afs_dir_remove_subdir(dentry);
63a4681f
DH
1264 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1265 afs_edit_dir_remove(dvnode, &dentry->d_name,
1266 afs_edit_dir_for_rmdir);
1267 }
260a9803
DH
1268 }
1269
79ddbfa5
DH
1270 if (vnode)
1271 up_write(&vnode->rmdir_lock);
f58db83f 1272error_key:
260a9803 1273 key_put(key);
260a9803 1274error:
260a9803
DH
1275 return ret;
1276}
1277
1278/*
d2ddc776
DH
1279 * Remove a link to a file or symlink from a directory.
1280 *
1281 * If the file was not deleted due to excess hard links, the fileserver will
1282 * break the callback promise on the file - if it had one - before it returns
1283 * to us, and if it was deleted, it won't
1284 *
1285 * However, if we didn't have a callback promise outstanding, or it was
1286 * outstanding on a different server, then it won't break it either...
1287 */
79ddbfa5
DH
1288int afs_dir_remove_link(struct dentry *dentry, struct key *key,
1289 unsigned long d_version_before,
1290 unsigned long d_version_after)
d2ddc776 1291{
440fbc3a 1292 bool dir_valid;
d2ddc776
DH
1293 int ret = 0;
1294
440fbc3a
DH
1295 /* There were no intervening changes on the server if the version
1296 * number we got back was incremented by exactly 1.
1297 */
1298 dir_valid = (d_version_after == d_version_before + 1);
1299
d2ddc776
DH
1300 if (d_really_is_positive(dentry)) {
1301 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1302
30062bd1
DH
1303 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
1304 /* Already done */
1305 } else if (dir_valid) {
440fbc3a
DH
1306 drop_nlink(&vnode->vfs_inode);
1307 if (vnode->vfs_inode.i_nlink == 0) {
1308 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1309 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1310 }
d2ddc776 1311 ret = 0;
440fbc3a
DH
1312 } else {
1313 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1314
1315 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1316 kdebug("AFS_VNODE_DELETED");
1317
1318 ret = afs_validate(vnode, key);
1319 if (ret == -ESTALE)
1320 ret = 0;
1321 }
d2ddc776
DH
1322 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1323 }
1324
1325 return ret;
1326}
1327
1328/*
1329 * Remove a file or symlink from an AFS filesystem.
260a9803
DH
1330 */
1331static int afs_unlink(struct inode *dir, struct dentry *dentry)
1332{
d2ddc776 1333 struct afs_fs_cursor fc;
30062bd1 1334 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
260a9803 1335 struct key *key;
440fbc3a 1336 unsigned long d_version = (unsigned long)dentry->d_fsdata;
79ddbfa5 1337 bool need_rehash = false;
63a4681f 1338 u64 data_version = dvnode->status.data_version;
260a9803
DH
1339 int ret;
1340
3b6492df 1341 _enter("{%llx:%llu},{%pd}",
a455589f 1342 dvnode->fid.vid, dvnode->fid.vnode, dentry);
260a9803 1343
45222b9e 1344 if (dentry->d_name.len >= AFSNAMEMAX)
d2ddc776 1345 return -ENAMETOOLONG;
260a9803
DH
1346
1347 key = afs_request_key(dvnode->volume->cell);
1348 if (IS_ERR(key)) {
1349 ret = PTR_ERR(key);
1350 goto error;
1351 }
1352
d2ddc776 1353 /* Try to make sure we have a callback promise on the victim. */
2b0143b5
DH
1354 if (d_really_is_positive(dentry)) {
1355 vnode = AFS_FS_I(d_inode(dentry));
260a9803
DH
1356 ret = afs_validate(vnode, key);
1357 if (ret < 0)
d2ddc776 1358 goto error_key;
260a9803
DH
1359 }
1360
79ddbfa5
DH
1361 spin_lock(&dentry->d_lock);
1362 if (vnode && d_count(dentry) > 1) {
1363 spin_unlock(&dentry->d_lock);
1364 /* Start asynchronous writeout of the inode */
1365 write_inode_now(d_inode(dentry), 0);
1366 ret = afs_sillyrename(dvnode, vnode, dentry, key);
1367 goto error_key;
1368 }
1369 if (!d_unhashed(dentry)) {
1370 /* Prevent a race with RCU lookup. */
1371 __d_drop(dentry);
1372 need_rehash = true;
1373 }
1374 spin_unlock(&dentry->d_lock);
1375
d2ddc776
DH
1376 ret = -ERESTARTSYS;
1377 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1378 while (afs_select_fileserver(&fc)) {
68251f0a 1379 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
30062bd1
DH
1380
1381 if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
1382 !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
1383 yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1384 data_version);
1385 if (fc.ac.error != -ECONNABORTED ||
1386 fc.ac.abort_code != RXGEN_OPCODE)
1387 continue;
1388 set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
1389 }
1390
1391 afs_fs_remove(&fc, vnode, dentry->d_name.name, false,
63a4681f 1392 data_version);
d2ddc776 1393 }
260a9803 1394
d2ddc776
DH
1395 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1396 ret = afs_end_vnode_operation(&fc);
1397 if (ret == 0)
440fbc3a
DH
1398 ret = afs_dir_remove_link(
1399 dentry, key, d_version,
1400 (unsigned long)dvnode->status.data_version);
63a4681f
DH
1401 if (ret == 0 &&
1402 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1403 afs_edit_dir_remove(dvnode, &dentry->d_name,
1404 afs_edit_dir_for_unlink);
260a9803
DH
1405 }
1406
79ddbfa5
DH
1407 if (need_rehash && ret < 0 && ret != -ENOENT)
1408 d_rehash(dentry);
1409
d2ddc776 1410error_key:
260a9803
DH
1411 key_put(key);
1412error:
1413 _leave(" = %d", ret);
1414 return ret;
1415}
1416
1417/*
1418 * create a regular file on an AFS filesystem
1419 */
4acdaf27 1420static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 1421 bool excl)
260a9803 1422{
d2ddc776
DH
1423 struct afs_fs_cursor fc;
1424 struct afs_file_status newstatus;
1425 struct afs_callback newcb;
43dd388b 1426 struct afs_vnode *dvnode = AFS_FS_I(dir);
d2ddc776 1427 struct afs_fid newfid;
260a9803 1428 struct key *key;
63a4681f 1429 u64 data_version = dvnode->status.data_version;
260a9803
DH
1430 int ret;
1431
d2ddc776 1432 mode |= S_IFREG;
260a9803 1433
3b6492df 1434 _enter("{%llx:%llu},{%pd},%ho,",
a455589f 1435 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
260a9803 1436
d2ddc776
DH
1437 ret = -ENAMETOOLONG;
1438 if (dentry->d_name.len >= AFSNAMEMAX)
1439 goto error;
1440
260a9803
DH
1441 key = afs_request_key(dvnode->volume->cell);
1442 if (IS_ERR(key)) {
1443 ret = PTR_ERR(key);
1444 goto error;
1445 }
1446
d2ddc776
DH
1447 ret = -ERESTARTSYS;
1448 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1449 while (afs_select_fileserver(&fc)) {
68251f0a 1450 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
63a4681f 1451 afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
d2ddc776
DH
1452 &newfid, &newstatus, &newcb);
1453 }
260a9803 1454
d2ddc776
DH
1455 afs_check_for_remote_deletion(&fc, fc.vnode);
1456 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1457 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1458 ret = afs_end_vnode_operation(&fc);
1459 if (ret < 0)
1460 goto error_key;
4433b691
DH
1461 } else {
1462 goto error_key;
260a9803
DH
1463 }
1464
63a4681f
DH
1465 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1466 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1467 afs_edit_dir_for_create);
1468
260a9803
DH
1469 key_put(key);
1470 _leave(" = 0");
1471 return 0;
1472
d2ddc776 1473error_key:
260a9803
DH
1474 key_put(key);
1475error:
1476 d_drop(dentry);
1477 _leave(" = %d", ret);
1478 return ret;
1479}
1480
1481/*
1482 * create a hard link between files in an AFS filesystem
1483 */
1484static int afs_link(struct dentry *from, struct inode *dir,
1485 struct dentry *dentry)
1486{
d2ddc776 1487 struct afs_fs_cursor fc;
260a9803
DH
1488 struct afs_vnode *dvnode, *vnode;
1489 struct key *key;
63a4681f 1490 u64 data_version;
260a9803
DH
1491 int ret;
1492
2b0143b5 1493 vnode = AFS_FS_I(d_inode(from));
260a9803 1494 dvnode = AFS_FS_I(dir);
63a4681f 1495 data_version = dvnode->status.data_version;
260a9803 1496
3b6492df 1497 _enter("{%llx:%llu},{%llx:%llu},{%pd}",
260a9803
DH
1498 vnode->fid.vid, vnode->fid.vnode,
1499 dvnode->fid.vid, dvnode->fid.vnode,
a455589f 1500 dentry);
260a9803 1501
d2ddc776
DH
1502 ret = -ENAMETOOLONG;
1503 if (dentry->d_name.len >= AFSNAMEMAX)
1504 goto error;
1505
260a9803
DH
1506 key = afs_request_key(dvnode->volume->cell);
1507 if (IS_ERR(key)) {
1508 ret = PTR_ERR(key);
1509 goto error;
1510 }
1511
d2ddc776
DH
1512 ret = -ERESTARTSYS;
1513 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1514 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1515 afs_end_vnode_operation(&fc);
bc1527dc 1516 goto error_key;
d2ddc776
DH
1517 }
1518
1519 while (afs_select_fileserver(&fc)) {
68251f0a
DH
1520 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1521 fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
63a4681f 1522 afs_fs_link(&fc, vnode, dentry->d_name.name, data_version);
d2ddc776
DH
1523 }
1524
1525 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1526 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
1527 ihold(&vnode->vfs_inode);
1528 d_instantiate(dentry, &vnode->vfs_inode);
1529
1530 mutex_unlock(&vnode->io_lock);
1531 ret = afs_end_vnode_operation(&fc);
1532 if (ret < 0)
1533 goto error_key;
4433b691
DH
1534 } else {
1535 goto error_key;
d2ddc776 1536 }
260a9803 1537
63a4681f
DH
1538 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1539 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1540 afs_edit_dir_for_link);
1541
260a9803
DH
1542 key_put(key);
1543 _leave(" = 0");
1544 return 0;
1545
d2ddc776 1546error_key:
260a9803
DH
1547 key_put(key);
1548error:
1549 d_drop(dentry);
1550 _leave(" = %d", ret);
1551 return ret;
1552}
1553
1554/*
1555 * create a symlink in an AFS filesystem
1556 */
1557static int afs_symlink(struct inode *dir, struct dentry *dentry,
1558 const char *content)
1559{
d2ddc776
DH
1560 struct afs_fs_cursor fc;
1561 struct afs_file_status newstatus;
1562 struct afs_vnode *dvnode = AFS_FS_I(dir);
1563 struct afs_fid newfid;
260a9803 1564 struct key *key;
63a4681f 1565 u64 data_version = dvnode->status.data_version;
260a9803
DH
1566 int ret;
1567
3b6492df 1568 _enter("{%llx:%llu},{%pd},%s",
a455589f 1569 dvnode->fid.vid, dvnode->fid.vnode, dentry,
260a9803
DH
1570 content);
1571
d2ddc776
DH
1572 ret = -ENAMETOOLONG;
1573 if (dentry->d_name.len >= AFSNAMEMAX)
1574 goto error;
1575
260a9803 1576 ret = -EINVAL;
45222b9e 1577 if (strlen(content) >= AFSPATHMAX)
260a9803
DH
1578 goto error;
1579
1580 key = afs_request_key(dvnode->volume->cell);
1581 if (IS_ERR(key)) {
1582 ret = PTR_ERR(key);
1583 goto error;
1584 }
1585
d2ddc776
DH
1586 ret = -ERESTARTSYS;
1587 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1588 while (afs_select_fileserver(&fc)) {
68251f0a 1589 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
63a4681f
DH
1590 afs_fs_symlink(&fc, dentry->d_name.name,
1591 content, data_version,
d2ddc776
DH
1592 &newfid, &newstatus);
1593 }
260a9803 1594
d2ddc776
DH
1595 afs_check_for_remote_deletion(&fc, fc.vnode);
1596 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1597 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
1598 ret = afs_end_vnode_operation(&fc);
1599 if (ret < 0)
1600 goto error_key;
4433b691
DH
1601 } else {
1602 goto error_key;
260a9803
DH
1603 }
1604
63a4681f
DH
1605 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1606 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1607 afs_edit_dir_for_symlink);
1608
260a9803
DH
1609 key_put(key);
1610 _leave(" = 0");
1611 return 0;
1612
d2ddc776 1613error_key:
260a9803
DH
1614 key_put(key);
1615error:
1616 d_drop(dentry);
1617 _leave(" = %d", ret);
1618 return ret;
1619}
1620
1621/*
1622 * rename a file in an AFS filesystem and/or move it between directories
1623 */
1624static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1cd66c93
MS
1625 struct inode *new_dir, struct dentry *new_dentry,
1626 unsigned int flags)
260a9803 1627{
d2ddc776 1628 struct afs_fs_cursor fc;
260a9803 1629 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
79ddbfa5
DH
1630 struct dentry *tmp = NULL, *rehash = NULL;
1631 struct inode *new_inode;
260a9803 1632 struct key *key;
63a4681f
DH
1633 u64 orig_data_version, new_data_version;
1634 bool new_negative = d_is_negative(new_dentry);
260a9803
DH
1635 int ret;
1636
1cd66c93
MS
1637 if (flags)
1638 return -EINVAL;
1639
79ddbfa5
DH
1640 /* Don't allow silly-rename files be moved around. */
1641 if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
1642 return -EINVAL;
1643
2b0143b5 1644 vnode = AFS_FS_I(d_inode(old_dentry));
260a9803
DH
1645 orig_dvnode = AFS_FS_I(old_dir);
1646 new_dvnode = AFS_FS_I(new_dir);
63a4681f
DH
1647 orig_data_version = orig_dvnode->status.data_version;
1648 new_data_version = new_dvnode->status.data_version;
260a9803 1649
3b6492df 1650 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
260a9803
DH
1651 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1652 vnode->fid.vid, vnode->fid.vnode,
1653 new_dvnode->fid.vid, new_dvnode->fid.vnode,
a455589f 1654 new_dentry);
260a9803 1655
260a9803
DH
1656 key = afs_request_key(orig_dvnode->volume->cell);
1657 if (IS_ERR(key)) {
1658 ret = PTR_ERR(key);
1659 goto error;
1660 }
1661
79ddbfa5
DH
1662 /* For non-directories, check whether the target is busy and if so,
1663 * make a copy of the dentry and then do a silly-rename. If the
1664 * silly-rename succeeds, the copied dentry is hashed and becomes the
1665 * new target.
1666 */
1667 if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
1668 /* To prevent any new references to the target during the
1669 * rename, we unhash the dentry in advance.
1670 */
1671 if (!d_unhashed(new_dentry)) {
1672 d_drop(new_dentry);
1673 rehash = new_dentry;
1674 }
1675
1676 if (d_count(new_dentry) > 2) {
1677 /* copy the target dentry's name */
1678 ret = -ENOMEM;
1679 tmp = d_alloc(new_dentry->d_parent,
1680 &new_dentry->d_name);
1681 if (!tmp)
1682 goto error_rehash;
1683
1684 ret = afs_sillyrename(new_dvnode,
1685 AFS_FS_I(d_inode(new_dentry)),
1686 new_dentry, key);
1687 if (ret)
1688 goto error_rehash;
1689
1690 new_dentry = tmp;
1691 rehash = NULL;
1692 new_negative = true;
1693 orig_data_version = orig_dvnode->status.data_version;
1694 new_data_version = new_dvnode->status.data_version;
1695 }
1696 }
1697
d2ddc776
DH
1698 ret = -ERESTARTSYS;
1699 if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
1700 if (orig_dvnode != new_dvnode) {
1701 if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1702 afs_end_vnode_operation(&fc);
79ddbfa5 1703 goto error_rehash;
d2ddc776
DH
1704 }
1705 }
1706 while (afs_select_fileserver(&fc)) {
68251f0a
DH
1707 fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
1708 fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
d2ddc776 1709 afs_fs_rename(&fc, old_dentry->d_name.name,
63a4681f
DH
1710 new_dvnode, new_dentry->d_name.name,
1711 orig_data_version, new_data_version);
d2ddc776
DH
1712 }
1713
1714 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
1715 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
1716 if (orig_dvnode != new_dvnode)
1717 mutex_unlock(&new_dvnode->io_lock);
1718 ret = afs_end_vnode_operation(&fc);
1719 if (ret < 0)
79ddbfa5 1720 goto error_rehash;
d2ddc776
DH
1721 }
1722
63a4681f 1723 if (ret == 0) {
79ddbfa5
DH
1724 if (rehash)
1725 d_rehash(rehash);
63a4681f
DH
1726 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1727 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
79ddbfa5 1728 afs_edit_dir_for_rename_0);
63a4681f
DH
1729
1730 if (!new_negative &&
1731 test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1732 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
79ddbfa5 1733 afs_edit_dir_for_rename_1);
63a4681f
DH
1734
1735 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1736 afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
79ddbfa5
DH
1737 &vnode->fid, afs_edit_dir_for_rename_2);
1738
1739 new_inode = d_inode(new_dentry);
1740 if (new_inode) {
1741 spin_lock(&new_inode->i_lock);
1742 if (new_inode->i_nlink > 0)
1743 drop_nlink(new_inode);
1744 spin_unlock(&new_inode->i_lock);
1745 }
1746 d_move(old_dentry, new_dentry);
1747 goto error_tmp;
63a4681f
DH
1748 }
1749
79ddbfa5
DH
1750error_rehash:
1751 if (rehash)
1752 d_rehash(rehash);
1753error_tmp:
1754 if (tmp)
1755 dput(tmp);
260a9803
DH
1756 key_put(key);
1757error:
260a9803
DH
1758 _leave(" = %d", ret);
1759 return ret;
1760}
f3ddee8d
DH
1761
1762/*
1763 * Release a directory page and clean up its private state if it's not busy
1764 * - return true if the page can now be released, false if not
1765 */
1766static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1767{
1768 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1769
3b6492df 1770 _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
f3ddee8d
DH
1771
1772 set_page_private(page, 0);
1773 ClearPagePrivate(page);
1774
1775 /* The directory will need reloading. */
1776 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1777 afs_stat_v(dvnode, n_relpg);
1778 return 1;
1779}
1780
1781/*
1782 * invalidate part or all of a page
1783 * - release a page and clean up its private data if offset is 0 (indicating
1784 * the entire page)
1785 */
1786static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1787 unsigned int length)
1788{
1789 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1790
1791 _enter("{%lu},%u,%u", page->index, offset, length);
1792
1793 BUG_ON(!PageLocked(page));
1794
1795 /* The directory will need reloading. */
1796 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1797 afs_stat_v(dvnode, n_inval);
1798
1799 /* we clean up only if the entire page is being invalidated */
1800 if (offset == 0 && length == PAGE_SIZE) {
1801 set_page_private(page, 0);
1802 ClearPagePrivate(page);
1803 }
1804}