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