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