]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/afs/dir.c
afs: Split afs_validate() so first part can be used under LOOKUP_RCU
[mirror_ubuntu-hirsute-kernel.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"
a58823ac 21#include "afs_fs.h"
4ea219a8 22#include "xdr_fs.h"
1da177e4 23
260a9803 24static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 25 unsigned int flags);
1da177e4 26static int afs_dir_open(struct inode *inode, struct file *file);
1bbae9f8 27static int afs_readdir(struct file *file, struct dir_context *ctx);
0b728e19 28static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
fe15ce44 29static int afs_d_delete(const struct dentry *dentry);
79ddbfa5 30static void afs_d_iput(struct dentry *dentry, struct inode *inode);
5cf9dd55 31static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
afefdbb2 32 loff_t fpos, u64 ino, unsigned dtype);
5cf9dd55
DH
33static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
34 loff_t fpos, u64 ino, unsigned dtype);
4acdaf27 35static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 36 bool excl);
18bb1db3 37static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
260a9803
DH
38static int afs_rmdir(struct inode *dir, struct dentry *dentry);
39static int afs_unlink(struct inode *dir, struct dentry *dentry);
40static int afs_link(struct dentry *from, struct inode *dir,
41 struct dentry *dentry);
42static int afs_symlink(struct inode *dir, struct dentry *dentry,
43 const char *content);
44static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1cd66c93
MS
45 struct inode *new_dir, struct dentry *new_dentry,
46 unsigned int flags);
f3ddee8d
DH
47static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
48static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
49 unsigned int length);
50
51static int afs_dir_set_page_dirty(struct page *page)
52{
53 BUG(); /* This should never happen. */
54}
1da177e4 55
4b6f5d20 56const struct file_operations afs_dir_file_operations = {
1da177e4 57 .open = afs_dir_open,
00d3b7a4 58 .release = afs_release,
29884eff 59 .iterate_shared = afs_readdir,
e8d6c554 60 .lock = afs_lock,
3222a3e5 61 .llseek = generic_file_llseek,
1da177e4
LT
62};
63
754661f1 64const struct inode_operations afs_dir_inode_operations = {
260a9803
DH
65 .create = afs_create,
66 .lookup = afs_lookup,
67 .link = afs_link,
68 .unlink = afs_unlink,
69 .symlink = afs_symlink,
70 .mkdir = afs_mkdir,
71 .rmdir = afs_rmdir,
2773bf00 72 .rename = afs_rename,
00d3b7a4 73 .permission = afs_permission,
416351f2 74 .getattr = afs_getattr,
31143d5d 75 .setattr = afs_setattr,
d3e3b7ea 76 .listxattr = afs_listxattr,
1da177e4
LT
77};
78
f3ddee8d
DH
79const struct address_space_operations afs_dir_aops = {
80 .set_page_dirty = afs_dir_set_page_dirty,
81 .releasepage = afs_dir_releasepage,
82 .invalidatepage = afs_dir_invalidatepage,
83};
84
d61dcce2 85const struct dentry_operations afs_fs_dentry_operations = {
1da177e4
LT
86 .d_revalidate = afs_d_revalidate,
87 .d_delete = afs_d_delete,
260a9803 88 .d_release = afs_d_release,
d18610b0 89 .d_automount = afs_d_automount,
79ddbfa5 90 .d_iput = afs_d_iput,
1da177e4
LT
91};
92
5cf9dd55
DH
93struct afs_lookup_one_cookie {
94 struct dir_context ctx;
95 struct qstr name;
96 bool found;
97 struct afs_fid fid;
98};
99
260a9803 100struct afs_lookup_cookie {
5cf9dd55
DH
101 struct dir_context ctx;
102 struct qstr name;
103 bool found;
104 bool one_only;
105 unsigned short nr_fids;
87182759 106 struct afs_status_cb *statuses;
5cf9dd55 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;
87182759 643 struct afs_status_cb *scb;
5cf9dd55
DH
644 struct afs_iget_data data;
645 struct afs_fs_cursor fc;
646 struct afs_vnode *dvnode = AFS_FS_I(dir);
647 struct inode *inode = NULL;
648 int ret, i;
649
650 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
651
652 cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
653 if (!cookie)
654 return ERR_PTR(-ENOMEM);
655
656 cookie->ctx.actor = afs_lookup_filldir;
657 cookie->name = dentry->d_name;
658 cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
659
660 read_seqlock_excl(&dvnode->cb_lock);
661 if (dvnode->cb_interest &&
662 dvnode->cb_interest->server &&
663 test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
664 cookie->one_only = true;
665 read_sequnlock_excl(&dvnode->cb_lock);
666
667 for (i = 0; i < 50; i++)
668 cookie->fids[i].vid = as->volume->vid;
669
670 /* search the directory */
671 ret = afs_dir_iterate(dir, &cookie->ctx, key);
672 if (ret < 0) {
673 inode = ERR_PTR(ret);
674 goto out;
675 }
676
677 inode = ERR_PTR(-ENOENT);
678 if (!cookie->found)
679 goto out;
680
681 /* Check to see if we already have an inode for the primary fid. */
682 data.volume = dvnode->volume;
683 data.fid = cookie->fids[0];
684 inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
685 if (inode)
686 goto out;
687
688 /* Need space for examining all the selected files */
689 inode = ERR_PTR(-ENOMEM);
87182759
DH
690 cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb),
691 GFP_KERNEL);
5cf9dd55
DH
692 if (!cookie->statuses)
693 goto out;
694
5cf9dd55
DH
695 /* Try FS.InlineBulkStatus first. Abort codes for the individual
696 * lookups contained therein are stored in the reply without aborting
697 * the whole operation.
698 */
699 if (cookie->one_only)
700 goto no_inline_bulk_status;
701
702 inode = ERR_PTR(-ERESTARTSYS);
20b8391f 703 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
5cf9dd55
DH
704 while (afs_select_fileserver(&fc)) {
705 if (test_bit(AFS_SERVER_FL_NO_IBULK,
706 &fc.cbi->server->flags)) {
707 fc.ac.abort_code = RX_INVALID_OPERATION;
708 fc.ac.error = -ECONNABORTED;
709 break;
710 }
711 afs_fs_inline_bulk_status(&fc,
712 afs_v2net(dvnode),
713 cookie->fids,
714 cookie->statuses,
5cf9dd55
DH
715 cookie->nr_fids, NULL);
716 }
717
718 if (fc.ac.error == 0)
719 cbi = afs_get_cb_interest(fc.cbi);
720 if (fc.ac.abort_code == RX_INVALID_OPERATION)
721 set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
722 inode = ERR_PTR(afs_end_vnode_operation(&fc));
723 }
724
725 if (!IS_ERR(inode))
726 goto success;
727 if (fc.ac.abort_code != RX_INVALID_OPERATION)
728 goto out_c;
729
730no_inline_bulk_status:
731 /* We could try FS.BulkStatus next, but this aborts the entire op if
732 * any of the lookups fails - so, for the moment, revert to
733 * FS.FetchStatus for just the primary fid.
734 */
735 cookie->nr_fids = 1;
736 inode = ERR_PTR(-ERESTARTSYS);
20b8391f 737 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
5cf9dd55 738 while (afs_select_fileserver(&fc)) {
87182759 739 scb = &cookie->statuses[0];
5cf9dd55
DH
740 afs_fs_fetch_status(&fc,
741 afs_v2net(dvnode),
742 cookie->fids,
a58823ac 743 scb,
5cf9dd55
DH
744 NULL);
745 }
746
747 if (fc.ac.error == 0)
748 cbi = afs_get_cb_interest(fc.cbi);
749 inode = ERR_PTR(afs_end_vnode_operation(&fc));
750 }
751
752 if (IS_ERR(inode))
753 goto out_c;
754
755 for (i = 0; i < cookie->nr_fids; i++)
87182759 756 cookie->statuses[i].status.abort_code = 0;
5cf9dd55
DH
757
758success:
759 /* Turn all the files into inodes and save the first one - which is the
760 * one we actually want.
761 */
87182759
DH
762 scb = &cookie->statuses[0];
763 if (scb->status.abort_code != 0)
764 inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code));
5cf9dd55
DH
765
766 for (i = 0; i < cookie->nr_fids; i++) {
87182759 767 struct afs_status_cb *scb = &cookie->statuses[i];
5cf9dd55
DH
768 struct inode *ti;
769
87182759 770 if (scb->status.abort_code != 0)
5cf9dd55
DH
771 continue;
772
773 ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
a58823ac 774 scb, cbi, dvnode);
5cf9dd55
DH
775 if (i == 0) {
776 inode = ti;
777 } else {
778 if (!IS_ERR(ti))
779 iput(ti);
780 }
781 }
782
783out_c:
784 afs_put_cb_interest(afs_v2net(dvnode), cbi);
87182759 785 kvfree(cookie->statuses);
5cf9dd55
DH
786out:
787 kfree(cookie);
788 return inode;
789}
790
6f8880d8
DH
791/*
792 * Look up an entry in a directory with @sys substitution.
793 */
794static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
795 struct key *key)
796{
797 struct afs_sysnames *subs;
798 struct afs_net *net = afs_i2net(dir);
799 struct dentry *ret;
800 char *buf, *p, *name;
801 int len, i;
802
803 _enter("");
804
805 ret = ERR_PTR(-ENOMEM);
806 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
807 if (!buf)
808 goto out_p;
809 if (dentry->d_name.len > 4) {
810 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
811 p += dentry->d_name.len - 4;
812 }
813
814 /* There is an ordered list of substitutes that we have to try. */
815 read_lock(&net->sysnames_lock);
816 subs = net->sysnames;
817 refcount_inc(&subs->usage);
818 read_unlock(&net->sysnames_lock);
819
820 for (i = 0; i < subs->nr; i++) {
821 name = subs->subs[i];
822 len = dentry->d_name.len - 4 + strlen(name);
823 if (len >= AFSNAMEMAX) {
824 ret = ERR_PTR(-ENAMETOOLONG);
825 goto out_s;
826 }
827
828 strcpy(p, name);
829 ret = lookup_one_len(buf, dentry->d_parent, len);
830 if (IS_ERR(ret) || d_is_positive(ret))
831 goto out_s;
832 dput(ret);
833 }
834
835 /* We don't want to d_add() the @sys dentry here as we don't want to
836 * the cached dentry to hide changes to the sysnames list.
837 */
838 ret = NULL;
839out_s:
840 afs_put_sysnames(subs);
841 kfree(buf);
842out_p:
843 key_put(key);
844 return ret;
845}
846
08e0e7c8
DH
847/*
848 * look up an entry in a directory
849 */
260a9803 850static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 851 unsigned int flags)
08e0e7c8 852{
5cf9dd55 853 struct afs_vnode *dvnode = AFS_FS_I(dir);
08e0e7c8 854 struct inode *inode;
34b2a88f 855 struct dentry *d;
00d3b7a4 856 struct key *key;
08e0e7c8
DH
857 int ret;
858
3b6492df 859 _enter("{%llx:%llu},%p{%pd},",
5cf9dd55 860 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
260a9803 861
2b0143b5 862 ASSERTCMP(d_inode(dentry), ==, NULL);
08e0e7c8 863
45222b9e 864 if (dentry->d_name.len >= AFSNAMEMAX) {
08e0e7c8
DH
865 _leave(" = -ENAMETOOLONG");
866 return ERR_PTR(-ENAMETOOLONG);
867 }
868
5cf9dd55 869 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
08e0e7c8
DH
870 _leave(" = -ESTALE");
871 return ERR_PTR(-ESTALE);
872 }
873
5cf9dd55 874 key = afs_request_key(dvnode->volume->cell);
00d3b7a4
DH
875 if (IS_ERR(key)) {
876 _leave(" = %ld [key]", PTR_ERR(key));
e231c2ee 877 return ERR_CAST(key);
00d3b7a4
DH
878 }
879
5cf9dd55 880 ret = afs_validate(dvnode, key);
260a9803
DH
881 if (ret < 0) {
882 key_put(key);
883 _leave(" = %d [val]", ret);
884 return ERR_PTR(ret);
885 }
886
6f8880d8
DH
887 if (dentry->d_name.len >= 4 &&
888 dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
889 dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
890 dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
891 dentry->d_name.name[dentry->d_name.len - 1] == 's')
892 return afs_lookup_atsys(dir, dentry, key);
893
d55b4da4 894 afs_stat_v(dvnode, n_lookup);
5cf9dd55 895 inode = afs_do_lookup(dir, dentry, key);
00d3b7a4 896 key_put(key);
34b2a88f
AV
897 if (inode == ERR_PTR(-ENOENT)) {
898 inode = afs_try_auto_mntpt(dentry, dir);
34b2a88f
AV
899 } else {
900 dentry->d_fsdata =
901 (void *)(unsigned long)dvnode->status.data_version;
08e0e7c8 902 }
34b2a88f 903 d = d_splice_alias(inode, dentry);
80548b03 904 if (!IS_ERR_OR_NULL(d)) {
34b2a88f 905 d->d_fsdata = dentry->d_fsdata;
80548b03
DH
906 trace_afs_lookup(dvnode, &d->d_name,
907 inode ? AFS_FS_I(inode) : NULL);
908 } else {
909 trace_afs_lookup(dvnode, &dentry->d_name,
910 inode ? AFS_FS_I(inode) : NULL);
911 }
34b2a88f 912 return d;
ec26815a 913}
1da177e4 914
1da177e4
LT
915/*
916 * check that a dentry lookup hit has found a valid entry
917 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
918 * inode
1da177e4 919 */
0b728e19 920static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
1da177e4 921{
260a9803 922 struct afs_vnode *vnode, *dir;
dd0d9a46 923 struct afs_fid uninitialized_var(fid);
1da177e4 924 struct dentry *parent;
c435ee34 925 struct inode *inode;
00d3b7a4 926 struct key *key;
a4ff7401 927 long dir_version, de_version;
1da177e4
LT
928 int ret;
929
0b728e19 930 if (flags & LOOKUP_RCU)
34286d66
NP
931 return -ECHILD;
932
c435ee34
DH
933 if (d_really_is_positive(dentry)) {
934 vnode = AFS_FS_I(d_inode(dentry));
3b6492df 935 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
a455589f 936 vnode->fid.vid, vnode->fid.vnode, dentry,
260a9803 937 vnode->flags);
c435ee34 938 } else {
a455589f 939 _enter("{neg n=%pd}", dentry);
c435ee34 940 }
1da177e4 941
260a9803 942 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
00d3b7a4
DH
943 if (IS_ERR(key))
944 key = NULL;
945
c435ee34
DH
946 if (d_really_is_positive(dentry)) {
947 inode = d_inode(dentry);
948 if (inode) {
949 vnode = AFS_FS_I(inode);
950 afs_validate(vnode, key);
951 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
952 goto out_bad;
953 }
954 }
955
1da177e4 956 /* lock down the parent dentry so we can peer at it */
08e0e7c8 957 parent = dget_parent(dentry);
2b0143b5 958 dir = AFS_FS_I(d_inode(parent));
1da177e4 959
260a9803 960 /* validate the parent directory */
c435ee34 961 afs_validate(dir, key);
260a9803
DH
962
963 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
a455589f 964 _debug("%pd: parent dir deleted", dentry);
c435ee34 965 goto out_bad_parent;
1da177e4
LT
966 }
967
a4ff7401
DH
968 /* We only need to invalidate a dentry if the server's copy changed
969 * behind our back. If we made the change, it's no problem. Note that
970 * on a 32-bit system, we only have 32 bits in the dentry to store the
971 * version.
972 */
973 dir_version = (long)dir->status.data_version;
974 de_version = (long)dentry->d_fsdata;
975 if (de_version == dir_version)
976 goto out_valid;
977
978 dir_version = (long)dir->invalid_before;
979 if (de_version - dir_version >= 0)
980 goto out_valid;
1da177e4 981
260a9803 982 _debug("dir modified");
d55b4da4 983 afs_stat_v(dir, n_reval);
260a9803
DH
984
985 /* search the directory for this vnode */
5cf9dd55 986 ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
260a9803
DH
987 switch (ret) {
988 case 0:
989 /* the filename maps to something */
2b0143b5 990 if (d_really_is_negative(dentry))
c435ee34
DH
991 goto out_bad_parent;
992 inode = d_inode(dentry);
993 if (is_bad_inode(inode)) {
a455589f
AV
994 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
995 dentry);
c435ee34 996 goto out_bad_parent;
1da177e4
LT
997 }
998
c435ee34
DH
999 vnode = AFS_FS_I(inode);
1000
1da177e4
LT
1001 /* if the vnode ID has changed, then the dirent points to a
1002 * different file */
08e0e7c8 1003 if (fid.vnode != vnode->fid.vnode) {
3b6492df 1004 _debug("%pd: dirent changed [%llu != %llu]",
a455589f 1005 dentry, fid.vnode,
08e0e7c8 1006 vnode->fid.vnode);
1da177e4
LT
1007 goto not_found;
1008 }
1009
1010 /* if the vnode ID uniqifier has changed, then the file has
260a9803
DH
1011 * been deleted and replaced, and the original vnode ID has
1012 * been reused */
08e0e7c8 1013 if (fid.unique != vnode->fid.unique) {
a455589f
AV
1014 _debug("%pd: file deleted (uq %u -> %u I:%u)",
1015 dentry, fid.unique,
7a224228 1016 vnode->fid.unique,
c435ee34
DH
1017 vnode->vfs_inode.i_generation);
1018 write_seqlock(&vnode->cb_lock);
08e0e7c8 1019 set_bit(AFS_VNODE_DELETED, &vnode->flags);
c435ee34 1020 write_sequnlock(&vnode->cb_lock);
260a9803 1021 goto not_found;
1da177e4 1022 }
260a9803 1023 goto out_valid;
08e0e7c8 1024
260a9803
DH
1025 case -ENOENT:
1026 /* the filename is unknown */
a455589f 1027 _debug("%pd: dirent not found", dentry);
2b0143b5 1028 if (d_really_is_positive(dentry))
260a9803
DH
1029 goto not_found;
1030 goto out_valid;
1da177e4 1031
260a9803 1032 default:
a455589f
AV
1033 _debug("failed to iterate dir %pd: %d",
1034 parent, ret);
c435ee34 1035 goto out_bad_parent;
08e0e7c8
DH
1036 }
1037
ec26815a 1038out_valid:
a4ff7401 1039 dentry->d_fsdata = (void *)dir_version;
1da177e4 1040 dput(parent);
00d3b7a4 1041 key_put(key);
1da177e4
LT
1042 _leave(" = 1 [valid]");
1043 return 1;
1044
1045 /* the dirent, if it exists, now points to a different vnode */
ec26815a 1046not_found:
1da177e4
LT
1047 spin_lock(&dentry->d_lock);
1048 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1049 spin_unlock(&dentry->d_lock);
1050
c435ee34 1051out_bad_parent:
a455589f 1052 _debug("dropping dentry %pd2", dentry);
1da177e4 1053 dput(parent);
c435ee34 1054out_bad:
00d3b7a4 1055 key_put(key);
1da177e4
LT
1056
1057 _leave(" = 0 [bad]");
1058 return 0;
ec26815a 1059}
1da177e4 1060
1da177e4
LT
1061/*
1062 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1063 * sleep)
1064 * - called from dput() when d_count is going to 0.
1065 * - return 1 to request dentry be unhashed, 0 otherwise
1066 */
fe15ce44 1067static int afs_d_delete(const struct dentry *dentry)
1da177e4 1068{
a455589f 1069 _enter("%pd", dentry);
1da177e4
LT
1070
1071 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1072 goto zap;
1073
2b0143b5
DH
1074 if (d_really_is_positive(dentry) &&
1075 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
1076 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
bec5eb61 1077 goto zap;
1da177e4
LT
1078
1079 _leave(" = 0 [keep]");
1080 return 0;
1081
ec26815a 1082zap:
1da177e4
LT
1083 _leave(" = 1 [zap]");
1084 return 1;
ec26815a 1085}
260a9803 1086
79ddbfa5
DH
1087/*
1088 * Clean up sillyrename files on dentry removal.
1089 */
1090static void afs_d_iput(struct dentry *dentry, struct inode *inode)
1091{
1092 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1093 afs_silly_iput(dentry, inode);
1094 iput(inode);
1095}
1096
260a9803
DH
1097/*
1098 * handle dentry release
1099 */
66c7e1d3 1100void afs_d_release(struct dentry *dentry)
260a9803 1101{
a455589f 1102 _enter("%pd", dentry);
260a9803
DH
1103}
1104
d2ddc776
DH
1105/*
1106 * Create a new inode for create/mkdir/symlink
1107 */
1108static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1109 struct dentry *new_dentry,
1110 struct afs_fid *newfid,
a58823ac 1111 struct afs_status_cb *new_scb)
d2ddc776 1112{
5a813276 1113 struct afs_vnode *vnode;
d2ddc776
DH
1114 struct inode *inode;
1115
1116 if (fc->ac.error < 0)
1117 return;
1118
1119 inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
a58823ac 1120 newfid, new_scb, fc->cbi, fc->vnode);
d2ddc776
DH
1121 if (IS_ERR(inode)) {
1122 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1123 * the new directory on the server.
1124 */
1125 fc->ac.error = PTR_ERR(inode);
1126 return;
1127 }
1128
5a813276
DH
1129 vnode = AFS_FS_I(inode);
1130 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
a58823ac
DH
1131 if (fc->ac.error == 0)
1132 afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb);
73116df7 1133 d_instantiate(new_dentry, inode);
d2ddc776
DH
1134}
1135
260a9803
DH
1136/*
1137 * create a directory on an AFS filesystem
1138 */
18bb1db3 1139static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
260a9803 1140{
a58823ac 1141 struct afs_status_cb *scb;
d2ddc776 1142 struct afs_fs_cursor fc;
d2ddc776
DH
1143 struct afs_vnode *dvnode = AFS_FS_I(dir);
1144 struct afs_fid newfid;
260a9803
DH
1145 struct key *key;
1146 int ret;
1147
d2ddc776 1148 mode |= S_IFDIR;
260a9803 1149
3b6492df 1150 _enter("{%llx:%llu},{%pd},%ho",
a455589f 1151 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
260a9803 1152
a58823ac
DH
1153 ret = -ENOMEM;
1154 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1155 if (!scb)
1156 goto error;
1157
260a9803
DH
1158 key = afs_request_key(dvnode->volume->cell);
1159 if (IS_ERR(key)) {
1160 ret = PTR_ERR(key);
a58823ac 1161 goto error_scb;
260a9803
DH
1162 }
1163
d2ddc776 1164 ret = -ERESTARTSYS;
20b8391f 1165 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
a58823ac
DH
1166 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1167
d2ddc776 1168 while (afs_select_fileserver(&fc)) {
68251f0a 1169 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
a58823ac
DH
1170 afs_fs_create(&fc, dentry->d_name.name, mode,
1171 &scb[0], &newfid, &scb[1]);
d2ddc776 1172 }
260a9803 1173
a58823ac
DH
1174 afs_check_for_remote_deletion(&fc, dvnode);
1175 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1176 &data_version, &scb[0]);
1177 afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
d2ddc776
DH
1178 ret = afs_end_vnode_operation(&fc);
1179 if (ret < 0)
1180 goto error_key;
4433b691
DH
1181 } else {
1182 goto error_key;
260a9803
DH
1183 }
1184
63a4681f
DH
1185 if (ret == 0 &&
1186 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1187 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1188 afs_edit_dir_for_create);
1189
260a9803 1190 key_put(key);
a58823ac 1191 kfree(scb);
260a9803
DH
1192 _leave(" = 0");
1193 return 0;
1194
d2ddc776 1195error_key:
260a9803 1196 key_put(key);
a58823ac
DH
1197error_scb:
1198 kfree(scb);
260a9803
DH
1199error:
1200 d_drop(dentry);
1201 _leave(" = %d", ret);
1202 return ret;
1203}
1204
d2ddc776
DH
1205/*
1206 * Remove a subdir from a directory.
1207 */
1208static void afs_dir_remove_subdir(struct dentry *dentry)
1209{
1210 if (d_really_is_positive(dentry)) {
1211 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1212
1213 clear_nlink(&vnode->vfs_inode);
1214 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1215 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
63a4681f 1216 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
d2ddc776
DH
1217 }
1218}
1219
260a9803
DH
1220/*
1221 * remove a directory from an AFS filesystem
1222 */
1223static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1224{
a58823ac 1225 struct afs_status_cb *scb;
d2ddc776 1226 struct afs_fs_cursor fc;
f58db83f 1227 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
260a9803
DH
1228 struct key *key;
1229 int ret;
1230
3b6492df 1231 _enter("{%llx:%llu},{%pd}",
a455589f 1232 dvnode->fid.vid, dvnode->fid.vnode, dentry);
260a9803 1233
a58823ac
DH
1234 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
1235 if (!scb)
1236 return -ENOMEM;
1237
260a9803
DH
1238 key = afs_request_key(dvnode->volume->cell);
1239 if (IS_ERR(key)) {
1240 ret = PTR_ERR(key);
1241 goto error;
1242 }
1243
f58db83f
DH
1244 /* Try to make sure we have a callback promise on the victim. */
1245 if (d_really_is_positive(dentry)) {
1246 vnode = AFS_FS_I(d_inode(dentry));
1247 ret = afs_validate(vnode, key);
1248 if (ret < 0)
1249 goto error_key;
1250 }
1251
79ddbfa5
DH
1252 if (vnode) {
1253 ret = down_write_killable(&vnode->rmdir_lock);
1254 if (ret < 0)
1255 goto error_key;
1256 }
1257
d2ddc776 1258 ret = -ERESTARTSYS;
20b8391f 1259 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
a58823ac
DH
1260 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1261
d2ddc776 1262 while (afs_select_fileserver(&fc)) {
68251f0a 1263 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
a58823ac 1264 afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb);
d2ddc776 1265 }
260a9803 1266
a58823ac
DH
1267 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1268 &data_version, scb);
d2ddc776 1269 ret = afs_end_vnode_operation(&fc);
63a4681f 1270 if (ret == 0) {
d2ddc776 1271 afs_dir_remove_subdir(dentry);
63a4681f
DH
1272 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1273 afs_edit_dir_remove(dvnode, &dentry->d_name,
1274 afs_edit_dir_for_rmdir);
1275 }
260a9803
DH
1276 }
1277
79ddbfa5
DH
1278 if (vnode)
1279 up_write(&vnode->rmdir_lock);
f58db83f 1280error_key:
260a9803 1281 key_put(key);
260a9803 1282error:
a58823ac 1283 kfree(scb);
260a9803
DH
1284 return ret;
1285}
1286
1287/*
d2ddc776
DH
1288 * Remove a link to a file or symlink from a directory.
1289 *
1290 * If the file was not deleted due to excess hard links, the fileserver will
1291 * break the callback promise on the file - if it had one - before it returns
1292 * to us, and if it was deleted, it won't
1293 *
1294 * However, if we didn't have a callback promise outstanding, or it was
1295 * outstanding on a different server, then it won't break it either...
1296 */
79ddbfa5
DH
1297int afs_dir_remove_link(struct dentry *dentry, struct key *key,
1298 unsigned long d_version_before,
1299 unsigned long d_version_after)
d2ddc776 1300{
440fbc3a 1301 bool dir_valid;
d2ddc776
DH
1302 int ret = 0;
1303
440fbc3a
DH
1304 /* There were no intervening changes on the server if the version
1305 * number we got back was incremented by exactly 1.
1306 */
1307 dir_valid = (d_version_after == d_version_before + 1);
1308
d2ddc776
DH
1309 if (d_really_is_positive(dentry)) {
1310 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1311
30062bd1
DH
1312 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
1313 /* Already done */
1314 } else if (dir_valid) {
440fbc3a
DH
1315 drop_nlink(&vnode->vfs_inode);
1316 if (vnode->vfs_inode.i_nlink == 0) {
1317 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1318 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1319 }
d2ddc776 1320 ret = 0;
440fbc3a
DH
1321 } else {
1322 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1323
1324 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1325 kdebug("AFS_VNODE_DELETED");
1326
1327 ret = afs_validate(vnode, key);
1328 if (ret == -ESTALE)
1329 ret = 0;
1330 }
d2ddc776
DH
1331 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1332 }
1333
1334 return ret;
1335}
1336
1337/*
1338 * Remove a file or symlink from an AFS filesystem.
260a9803
DH
1339 */
1340static int afs_unlink(struct inode *dir, struct dentry *dentry)
1341{
d2ddc776 1342 struct afs_fs_cursor fc;
a58823ac 1343 struct afs_status_cb *scb;
30062bd1 1344 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
260a9803 1345 struct key *key;
440fbc3a 1346 unsigned long d_version = (unsigned long)dentry->d_fsdata;
79ddbfa5 1347 bool need_rehash = false;
260a9803
DH
1348 int ret;
1349
3b6492df 1350 _enter("{%llx:%llu},{%pd}",
a455589f 1351 dvnode->fid.vid, dvnode->fid.vnode, dentry);
260a9803 1352
45222b9e 1353 if (dentry->d_name.len >= AFSNAMEMAX)
d2ddc776 1354 return -ENAMETOOLONG;
260a9803 1355
a58823ac
DH
1356 ret = -ENOMEM;
1357 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1358 if (!scb)
1359 goto error;
1360
260a9803
DH
1361 key = afs_request_key(dvnode->volume->cell);
1362 if (IS_ERR(key)) {
1363 ret = PTR_ERR(key);
a58823ac 1364 goto error_scb;
260a9803
DH
1365 }
1366
d2ddc776 1367 /* Try to make sure we have a callback promise on the victim. */
2b0143b5
DH
1368 if (d_really_is_positive(dentry)) {
1369 vnode = AFS_FS_I(d_inode(dentry));
260a9803
DH
1370 ret = afs_validate(vnode, key);
1371 if (ret < 0)
d2ddc776 1372 goto error_key;
260a9803
DH
1373 }
1374
79ddbfa5
DH
1375 spin_lock(&dentry->d_lock);
1376 if (vnode && d_count(dentry) > 1) {
1377 spin_unlock(&dentry->d_lock);
1378 /* Start asynchronous writeout of the inode */
1379 write_inode_now(d_inode(dentry), 0);
1380 ret = afs_sillyrename(dvnode, vnode, dentry, key);
1381 goto error_key;
1382 }
1383 if (!d_unhashed(dentry)) {
1384 /* Prevent a race with RCU lookup. */
1385 __d_drop(dentry);
1386 need_rehash = true;
1387 }
1388 spin_unlock(&dentry->d_lock);
1389
d2ddc776 1390 ret = -ERESTARTSYS;
20b8391f 1391 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
a58823ac
DH
1392 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1393
d2ddc776 1394 while (afs_select_fileserver(&fc)) {
68251f0a 1395 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
30062bd1
DH
1396
1397 if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
1398 !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
1399 yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
a58823ac
DH
1400 &scb[0], &scb[1]);
1401 if (fc.ac.error == 0 &&
1402 scb[1].status.abort_code == VNOVNODE) {
1403 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1404 afs_break_callback(vnode);
1405 }
1406
30062bd1
DH
1407 if (fc.ac.error != -ECONNABORTED ||
1408 fc.ac.abort_code != RXGEN_OPCODE)
1409 continue;
1410 set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
1411 }
1412
a58823ac 1413 afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
d2ddc776 1414 }
260a9803 1415
a58823ac
DH
1416 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1417 &data_version, &scb[0]);
d2ddc776
DH
1418 ret = afs_end_vnode_operation(&fc);
1419 if (ret == 0)
440fbc3a
DH
1420 ret = afs_dir_remove_link(
1421 dentry, key, d_version,
1422 (unsigned long)dvnode->status.data_version);
63a4681f
DH
1423 if (ret == 0 &&
1424 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1425 afs_edit_dir_remove(dvnode, &dentry->d_name,
1426 afs_edit_dir_for_unlink);
260a9803
DH
1427 }
1428
79ddbfa5
DH
1429 if (need_rehash && ret < 0 && ret != -ENOENT)
1430 d_rehash(dentry);
1431
d2ddc776 1432error_key:
260a9803 1433 key_put(key);
a58823ac
DH
1434error_scb:
1435 kfree(scb);
260a9803
DH
1436error:
1437 _leave(" = %d", ret);
1438 return ret;
1439}
1440
1441/*
1442 * create a regular file on an AFS filesystem
1443 */
4acdaf27 1444static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 1445 bool excl)
260a9803 1446{
d2ddc776 1447 struct afs_fs_cursor fc;
a58823ac 1448 struct afs_status_cb *scb;
43dd388b 1449 struct afs_vnode *dvnode = AFS_FS_I(dir);
d2ddc776 1450 struct afs_fid newfid;
260a9803
DH
1451 struct key *key;
1452 int ret;
1453
d2ddc776 1454 mode |= S_IFREG;
260a9803 1455
3b6492df 1456 _enter("{%llx:%llu},{%pd},%ho,",
a455589f 1457 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
260a9803 1458
d2ddc776
DH
1459 ret = -ENAMETOOLONG;
1460 if (dentry->d_name.len >= AFSNAMEMAX)
1461 goto error;
1462
260a9803
DH
1463 key = afs_request_key(dvnode->volume->cell);
1464 if (IS_ERR(key)) {
1465 ret = PTR_ERR(key);
1466 goto error;
1467 }
1468
a58823ac
DH
1469 ret = -ENOMEM;
1470 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1471 if (!scb)
1472 goto error_scb;
1473
d2ddc776 1474 ret = -ERESTARTSYS;
20b8391f 1475 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
a58823ac
DH
1476 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1477
d2ddc776 1478 while (afs_select_fileserver(&fc)) {
68251f0a 1479 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
a58823ac
DH
1480 afs_fs_create(&fc, dentry->d_name.name, mode,
1481 &scb[0], &newfid, &scb[1]);
d2ddc776 1482 }
260a9803 1483
a58823ac
DH
1484 afs_check_for_remote_deletion(&fc, dvnode);
1485 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1486 &data_version, &scb[0]);
1487 afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
d2ddc776
DH
1488 ret = afs_end_vnode_operation(&fc);
1489 if (ret < 0)
1490 goto error_key;
4433b691
DH
1491 } else {
1492 goto error_key;
260a9803
DH
1493 }
1494
63a4681f
DH
1495 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1496 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1497 afs_edit_dir_for_create);
1498
a58823ac 1499 kfree(scb);
260a9803
DH
1500 key_put(key);
1501 _leave(" = 0");
1502 return 0;
1503
a58823ac
DH
1504error_scb:
1505 kfree(scb);
d2ddc776 1506error_key:
260a9803
DH
1507 key_put(key);
1508error:
1509 d_drop(dentry);
1510 _leave(" = %d", ret);
1511 return ret;
1512}
1513
1514/*
1515 * create a hard link between files in an AFS filesystem
1516 */
1517static int afs_link(struct dentry *from, struct inode *dir,
1518 struct dentry *dentry)
1519{
d2ddc776 1520 struct afs_fs_cursor fc;
a58823ac
DH
1521 struct afs_status_cb *scb;
1522 struct afs_vnode *dvnode = AFS_FS_I(dir);
1523 struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
260a9803
DH
1524 struct key *key;
1525 int ret;
1526
3b6492df 1527 _enter("{%llx:%llu},{%llx:%llu},{%pd}",
260a9803
DH
1528 vnode->fid.vid, vnode->fid.vnode,
1529 dvnode->fid.vid, dvnode->fid.vnode,
a455589f 1530 dentry);
260a9803 1531
d2ddc776
DH
1532 ret = -ENAMETOOLONG;
1533 if (dentry->d_name.len >= AFSNAMEMAX)
1534 goto error;
1535
a58823ac
DH
1536 ret = -ENOMEM;
1537 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1538 if (!scb)
1539 goto error;
1540
260a9803
DH
1541 key = afs_request_key(dvnode->volume->cell);
1542 if (IS_ERR(key)) {
1543 ret = PTR_ERR(key);
a58823ac 1544 goto error_scb;
260a9803
DH
1545 }
1546
d2ddc776 1547 ret = -ERESTARTSYS;
20b8391f 1548 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
a58823ac
DH
1549 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1550
d2ddc776
DH
1551 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1552 afs_end_vnode_operation(&fc);
bc1527dc 1553 goto error_key;
d2ddc776
DH
1554 }
1555
1556 while (afs_select_fileserver(&fc)) {
68251f0a
DH
1557 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1558 fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
a58823ac
DH
1559 afs_fs_link(&fc, vnode, dentry->d_name.name,
1560 &scb[0], &scb[1]);
d2ddc776
DH
1561 }
1562
a58823ac
DH
1563 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1564 &data_version, &scb[0]);
1565 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1566 NULL, &scb[1]);
d2ddc776
DH
1567 ihold(&vnode->vfs_inode);
1568 d_instantiate(dentry, &vnode->vfs_inode);
1569
1570 mutex_unlock(&vnode->io_lock);
1571 ret = afs_end_vnode_operation(&fc);
1572 if (ret < 0)
1573 goto error_key;
4433b691
DH
1574 } else {
1575 goto error_key;
d2ddc776 1576 }
260a9803 1577
63a4681f
DH
1578 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1579 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1580 afs_edit_dir_for_link);
1581
260a9803 1582 key_put(key);
a58823ac 1583 kfree(scb);
260a9803
DH
1584 _leave(" = 0");
1585 return 0;
1586
d2ddc776 1587error_key:
260a9803 1588 key_put(key);
a58823ac
DH
1589error_scb:
1590 kfree(scb);
260a9803
DH
1591error:
1592 d_drop(dentry);
1593 _leave(" = %d", ret);
1594 return ret;
1595}
1596
1597/*
1598 * create a symlink in an AFS filesystem
1599 */
1600static int afs_symlink(struct inode *dir, struct dentry *dentry,
1601 const char *content)
1602{
d2ddc776 1603 struct afs_fs_cursor fc;
a58823ac 1604 struct afs_status_cb *scb;
d2ddc776
DH
1605 struct afs_vnode *dvnode = AFS_FS_I(dir);
1606 struct afs_fid newfid;
260a9803
DH
1607 struct key *key;
1608 int ret;
1609
3b6492df 1610 _enter("{%llx:%llu},{%pd},%s",
a455589f 1611 dvnode->fid.vid, dvnode->fid.vnode, dentry,
260a9803
DH
1612 content);
1613
d2ddc776
DH
1614 ret = -ENAMETOOLONG;
1615 if (dentry->d_name.len >= AFSNAMEMAX)
1616 goto error;
1617
260a9803 1618 ret = -EINVAL;
45222b9e 1619 if (strlen(content) >= AFSPATHMAX)
260a9803
DH
1620 goto error;
1621
a58823ac
DH
1622 ret = -ENOMEM;
1623 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1624 if (!scb)
1625 goto error;
1626
260a9803
DH
1627 key = afs_request_key(dvnode->volume->cell);
1628 if (IS_ERR(key)) {
1629 ret = PTR_ERR(key);
a58823ac 1630 goto error_scb;
260a9803
DH
1631 }
1632
d2ddc776 1633 ret = -ERESTARTSYS;
20b8391f 1634 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
a58823ac
DH
1635 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1636
d2ddc776 1637 while (afs_select_fileserver(&fc)) {
68251f0a 1638 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
a58823ac
DH
1639 afs_fs_symlink(&fc, dentry->d_name.name, content,
1640 &scb[0], &newfid, &scb[1]);
d2ddc776 1641 }
260a9803 1642
a58823ac
DH
1643 afs_check_for_remote_deletion(&fc, dvnode);
1644 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1645 &data_version, &scb[0]);
1646 afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
d2ddc776
DH
1647 ret = afs_end_vnode_operation(&fc);
1648 if (ret < 0)
1649 goto error_key;
4433b691
DH
1650 } else {
1651 goto error_key;
260a9803
DH
1652 }
1653
63a4681f
DH
1654 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1655 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1656 afs_edit_dir_for_symlink);
1657
260a9803 1658 key_put(key);
a58823ac 1659 kfree(scb);
260a9803
DH
1660 _leave(" = 0");
1661 return 0;
1662
d2ddc776 1663error_key:
260a9803 1664 key_put(key);
a58823ac
DH
1665error_scb:
1666 kfree(scb);
260a9803
DH
1667error:
1668 d_drop(dentry);
1669 _leave(" = %d", ret);
1670 return ret;
1671}
1672
1673/*
1674 * rename a file in an AFS filesystem and/or move it between directories
1675 */
1676static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1cd66c93
MS
1677 struct inode *new_dir, struct dentry *new_dentry,
1678 unsigned int flags)
260a9803 1679{
d2ddc776 1680 struct afs_fs_cursor fc;
a58823ac 1681 struct afs_status_cb *scb;
260a9803 1682 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
79ddbfa5
DH
1683 struct dentry *tmp = NULL, *rehash = NULL;
1684 struct inode *new_inode;
260a9803 1685 struct key *key;
63a4681f 1686 bool new_negative = d_is_negative(new_dentry);
260a9803
DH
1687 int ret;
1688
1cd66c93
MS
1689 if (flags)
1690 return -EINVAL;
1691
79ddbfa5
DH
1692 /* Don't allow silly-rename files be moved around. */
1693 if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
1694 return -EINVAL;
1695
2b0143b5 1696 vnode = AFS_FS_I(d_inode(old_dentry));
260a9803
DH
1697 orig_dvnode = AFS_FS_I(old_dir);
1698 new_dvnode = AFS_FS_I(new_dir);
1699
3b6492df 1700 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
260a9803
DH
1701 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1702 vnode->fid.vid, vnode->fid.vnode,
1703 new_dvnode->fid.vid, new_dvnode->fid.vnode,
a455589f 1704 new_dentry);
260a9803 1705
a58823ac
DH
1706 ret = -ENOMEM;
1707 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1708 if (!scb)
1709 goto error;
1710
260a9803
DH
1711 key = afs_request_key(orig_dvnode->volume->cell);
1712 if (IS_ERR(key)) {
1713 ret = PTR_ERR(key);
a58823ac 1714 goto error_scb;
260a9803
DH
1715 }
1716
79ddbfa5
DH
1717 /* For non-directories, check whether the target is busy and if so,
1718 * make a copy of the dentry and then do a silly-rename. If the
1719 * silly-rename succeeds, the copied dentry is hashed and becomes the
1720 * new target.
1721 */
1722 if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
1723 /* To prevent any new references to the target during the
1724 * rename, we unhash the dentry in advance.
1725 */
1726 if (!d_unhashed(new_dentry)) {
1727 d_drop(new_dentry);
1728 rehash = new_dentry;
1729 }
1730
1731 if (d_count(new_dentry) > 2) {
1732 /* copy the target dentry's name */
1733 ret = -ENOMEM;
1734 tmp = d_alloc(new_dentry->d_parent,
1735 &new_dentry->d_name);
1736 if (!tmp)
1737 goto error_rehash;
1738
1739 ret = afs_sillyrename(new_dvnode,
1740 AFS_FS_I(d_inode(new_dentry)),
1741 new_dentry, key);
1742 if (ret)
1743 goto error_rehash;
1744
1745 new_dentry = tmp;
1746 rehash = NULL;
1747 new_negative = true;
79ddbfa5
DH
1748 }
1749 }
1750
d2ddc776 1751 ret = -ERESTARTSYS;
20b8391f 1752 if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) {
a58823ac
DH
1753 afs_dataversion_t orig_data_version;
1754 afs_dataversion_t new_data_version;
1755 struct afs_status_cb *new_scb = &scb[1];
1756
1757 orig_data_version = orig_dvnode->status.data_version + 1;
1758
d2ddc776
DH
1759 if (orig_dvnode != new_dvnode) {
1760 if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1761 afs_end_vnode_operation(&fc);
79ddbfa5 1762 goto error_rehash;
d2ddc776 1763 }
a58823ac
DH
1764 new_data_version = new_dvnode->status.data_version;
1765 } else {
1766 new_data_version = orig_data_version;
1767 new_scb = &scb[0];
d2ddc776 1768 }
a58823ac 1769
d2ddc776 1770 while (afs_select_fileserver(&fc)) {
68251f0a
DH
1771 fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
1772 fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
d2ddc776 1773 afs_fs_rename(&fc, old_dentry->d_name.name,
63a4681f 1774 new_dvnode, new_dentry->d_name.name,
a58823ac 1775 &scb[0], new_scb);
d2ddc776
DH
1776 }
1777
a58823ac
DH
1778 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break,
1779 &orig_data_version, &scb[0]);
1780 if (new_dvnode != orig_dvnode) {
1781 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2,
1782 &new_data_version, &scb[1]);
d2ddc776 1783 mutex_unlock(&new_dvnode->io_lock);
a58823ac 1784 }
d2ddc776
DH
1785 ret = afs_end_vnode_operation(&fc);
1786 if (ret < 0)
79ddbfa5 1787 goto error_rehash;
d2ddc776
DH
1788 }
1789
63a4681f 1790 if (ret == 0) {
79ddbfa5
DH
1791 if (rehash)
1792 d_rehash(rehash);
63a4681f
DH
1793 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1794 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
79ddbfa5 1795 afs_edit_dir_for_rename_0);
63a4681f
DH
1796
1797 if (!new_negative &&
1798 test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1799 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
79ddbfa5 1800 afs_edit_dir_for_rename_1);
63a4681f
DH
1801
1802 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1803 afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
79ddbfa5
DH
1804 &vnode->fid, afs_edit_dir_for_rename_2);
1805
1806 new_inode = d_inode(new_dentry);
1807 if (new_inode) {
1808 spin_lock(&new_inode->i_lock);
1809 if (new_inode->i_nlink > 0)
1810 drop_nlink(new_inode);
1811 spin_unlock(&new_inode->i_lock);
1812 }
1813 d_move(old_dentry, new_dentry);
1814 goto error_tmp;
63a4681f
DH
1815 }
1816
79ddbfa5
DH
1817error_rehash:
1818 if (rehash)
1819 d_rehash(rehash);
1820error_tmp:
1821 if (tmp)
1822 dput(tmp);
260a9803 1823 key_put(key);
a58823ac
DH
1824error_scb:
1825 kfree(scb);
260a9803 1826error:
260a9803
DH
1827 _leave(" = %d", ret);
1828 return ret;
1829}
f3ddee8d
DH
1830
1831/*
1832 * Release a directory page and clean up its private state if it's not busy
1833 * - return true if the page can now be released, false if not
1834 */
1835static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1836{
1837 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1838
3b6492df 1839 _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
f3ddee8d
DH
1840
1841 set_page_private(page, 0);
1842 ClearPagePrivate(page);
1843
1844 /* The directory will need reloading. */
1845 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1846 afs_stat_v(dvnode, n_relpg);
1847 return 1;
1848}
1849
1850/*
1851 * invalidate part or all of a page
1852 * - release a page and clean up its private data if offset is 0 (indicating
1853 * the entire page)
1854 */
1855static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1856 unsigned int length)
1857{
1858 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1859
1860 _enter("{%lu},%u,%u", page->index, offset, length);
1861
1862 BUG_ON(!PageLocked(page));
1863
1864 /* The directory will need reloading. */
1865 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1866 afs_stat_v(dvnode, n_inval);
1867
1868 /* we clean up only if the entire page is being invalidated */
1869 if (offset == 0 && length == PAGE_SIZE) {
1870 set_page_private(page, 0);
1871 ClearPagePrivate(page);
1872 }
1873}