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