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