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