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