xref: /openbmc/linux/fs/afs/dir.c (revision 255ed636)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /* dir.c: AFS filesystem directory handling
31da177e4SLinus Torvalds  *
4f3ddee8dSDavid Howells  * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
51da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/fs.h>
1034286d66SNick Piggin #include <linux/namei.h>
111da177e4SLinus Torvalds #include <linux/pagemap.h>
12f3ddee8dSDavid Howells #include <linux/swap.h>
1300d3b7a4SDavid Howells #include <linux/ctype.h>
14e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
15f3ddee8dSDavid Howells #include <linux/task_io_accounting_ops.h>
161da177e4SLinus Torvalds #include "internal.h"
17a58823acSDavid Howells #include "afs_fs.h"
184ea219a8SDavid Howells #include "xdr_fs.h"
191da177e4SLinus Torvalds 
20260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
2100cd8dd3SAl Viro 				 unsigned int flags);
221da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file);
231bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx);
240b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
25fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry);
2679ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode);
275cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
285cf9dd55SDavid Howells 				  loff_t fpos, u64 ino, unsigned dtype);
29ac7576f4SMiklos Szeredi static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
30afefdbb2SDavid Howells 			      loff_t fpos, u64 ino, unsigned dtype);
31549c7297SChristian Brauner static int afs_create(struct user_namespace *mnt_userns, struct inode *dir,
32549c7297SChristian Brauner 		      struct dentry *dentry, umode_t mode, bool excl);
33549c7297SChristian Brauner static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
34549c7297SChristian Brauner 		     struct dentry *dentry, umode_t mode);
35260a9803SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry);
36260a9803SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry);
37260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
38260a9803SDavid Howells 		    struct dentry *dentry);
39549c7297SChristian Brauner static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
40549c7297SChristian Brauner 		       struct dentry *dentry, const char *content);
41549c7297SChristian Brauner static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
42549c7297SChristian Brauner 		      struct dentry *old_dentry, struct inode *new_dir,
43549c7297SChristian Brauner 		      struct dentry *new_dentry, unsigned int flags);
44f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
45f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
46f3ddee8dSDavid Howells 				   unsigned int length);
47f3ddee8dSDavid Howells 
48f3ddee8dSDavid Howells static int afs_dir_set_page_dirty(struct page *page)
49f3ddee8dSDavid Howells {
50f3ddee8dSDavid Howells 	BUG(); /* This should never happen. */
51f3ddee8dSDavid Howells }
521da177e4SLinus Torvalds 
534b6f5d20SArjan van de Ven const struct file_operations afs_dir_file_operations = {
541da177e4SLinus Torvalds 	.open		= afs_dir_open,
5500d3b7a4SDavid Howells 	.release	= afs_release,
5629884effSAl Viro 	.iterate_shared	= afs_readdir,
57e8d6c554SDavid Howells 	.lock		= afs_lock,
583222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
591da177e4SLinus Torvalds };
601da177e4SLinus Torvalds 
61754661f1SArjan van de Ven const struct inode_operations afs_dir_inode_operations = {
62260a9803SDavid Howells 	.create		= afs_create,
63260a9803SDavid Howells 	.lookup		= afs_lookup,
64260a9803SDavid Howells 	.link		= afs_link,
65260a9803SDavid Howells 	.unlink		= afs_unlink,
66260a9803SDavid Howells 	.symlink	= afs_symlink,
67260a9803SDavid Howells 	.mkdir		= afs_mkdir,
68260a9803SDavid Howells 	.rmdir		= afs_rmdir,
692773bf00SMiklos Szeredi 	.rename		= afs_rename,
7000d3b7a4SDavid Howells 	.permission	= afs_permission,
71416351f2SDavid Howells 	.getattr	= afs_getattr,
7231143d5dSDavid Howells 	.setattr	= afs_setattr,
731da177e4SLinus Torvalds };
741da177e4SLinus Torvalds 
75f3ddee8dSDavid Howells const struct address_space_operations afs_dir_aops = {
76f3ddee8dSDavid Howells 	.set_page_dirty	= afs_dir_set_page_dirty,
77f3ddee8dSDavid Howells 	.releasepage	= afs_dir_releasepage,
78f3ddee8dSDavid Howells 	.invalidatepage	= afs_dir_invalidatepage,
79f3ddee8dSDavid Howells };
80f3ddee8dSDavid Howells 
81d61dcce2SAl Viro const struct dentry_operations afs_fs_dentry_operations = {
821da177e4SLinus Torvalds 	.d_revalidate	= afs_d_revalidate,
831da177e4SLinus Torvalds 	.d_delete	= afs_d_delete,
84260a9803SDavid Howells 	.d_release	= afs_d_release,
85d18610b0SDavid Howells 	.d_automount	= afs_d_automount,
8679ddbfa5SDavid Howells 	.d_iput		= afs_d_iput,
871da177e4SLinus Torvalds };
881da177e4SLinus Torvalds 
895cf9dd55SDavid Howells struct afs_lookup_one_cookie {
905cf9dd55SDavid Howells 	struct dir_context	ctx;
915cf9dd55SDavid Howells 	struct qstr		name;
925cf9dd55SDavid Howells 	bool			found;
935cf9dd55SDavid Howells 	struct afs_fid		fid;
945cf9dd55SDavid Howells };
955cf9dd55SDavid Howells 
96260a9803SDavid Howells struct afs_lookup_cookie {
971bbae9f8SAl Viro 	struct dir_context	ctx;
981bbae9f8SAl Viro 	struct qstr		name;
995cf9dd55SDavid Howells 	bool			found;
1005cf9dd55SDavid Howells 	bool			one_only;
1015cf9dd55SDavid Howells 	unsigned short		nr_fids;
1025cf9dd55SDavid Howells 	struct afs_fid		fids[50];
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /*
106*255ed636SDavid Howells  * Drop the refs that we're holding on the folios we were reading into.  We've
107c4508464SDavid Howells  * got refs on the first nr_pages pages.
108c4508464SDavid Howells  */
109c4508464SDavid Howells static void afs_dir_read_cleanup(struct afs_read *req)
110c4508464SDavid Howells {
111c4508464SDavid Howells 	struct address_space *mapping = req->vnode->vfs_inode.i_mapping;
112*255ed636SDavid Howells 	struct folio *folio;
113c4508464SDavid Howells 	pgoff_t last = req->nr_pages - 1;
114c4508464SDavid Howells 
115c4508464SDavid Howells 	XA_STATE(xas, &mapping->i_pages, 0);
116c4508464SDavid Howells 
117c4508464SDavid Howells 	if (unlikely(!req->nr_pages))
118c4508464SDavid Howells 		return;
119c4508464SDavid Howells 
120c4508464SDavid Howells 	rcu_read_lock();
121*255ed636SDavid Howells 	xas_for_each(&xas, folio, last) {
122*255ed636SDavid Howells 		if (xas_retry(&xas, folio))
123c4508464SDavid Howells 			continue;
124*255ed636SDavid Howells 		BUG_ON(xa_is_value(folio));
125*255ed636SDavid Howells 		ASSERTCMP(folio_file_mapping(folio), ==, mapping);
126c4508464SDavid Howells 
127*255ed636SDavid Howells 		folio_put(folio);
128c4508464SDavid Howells 	}
129c4508464SDavid Howells 
130c4508464SDavid Howells 	rcu_read_unlock();
131c4508464SDavid Howells }
132c4508464SDavid Howells 
133c4508464SDavid Howells /*
134*255ed636SDavid Howells  * check that a directory folio is valid
1351da177e4SLinus Torvalds  */
136*255ed636SDavid Howells static bool afs_dir_check_folio(struct afs_vnode *dvnode, struct folio *folio,
137f3ddee8dSDavid Howells 				loff_t i_size)
1381da177e4SLinus Torvalds {
139*255ed636SDavid Howells 	union afs_xdr_dir_block *block;
140*255ed636SDavid Howells 	size_t offset, size;
141*255ed636SDavid Howells 	loff_t pos;
1421da177e4SLinus Torvalds 
143*255ed636SDavid Howells 	/* Determine how many magic numbers there should be in this folio, but
144dab17c1aSDavid Howells 	 * we must take care because the directory may change size under us.
145dab17c1aSDavid Howells 	 */
146*255ed636SDavid Howells 	pos = folio_pos(folio);
147*255ed636SDavid Howells 	if (i_size <= pos)
148dab17c1aSDavid Howells 		goto checked;
149dab17c1aSDavid Howells 
150*255ed636SDavid Howells 	size = min_t(loff_t, folio_size(folio), i_size - pos);
151*255ed636SDavid Howells 	for (offset = 0; offset < size; offset += sizeof(*block)) {
152*255ed636SDavid Howells 		block = kmap_local_folio(folio, offset);
153*255ed636SDavid Howells 		if (block->hdr.magic != AFS_DIR_MAGIC) {
154*255ed636SDavid Howells 			printk("kAFS: %s(%lx): [%llx] bad magic %zx/%zx is %04hx\n",
155*255ed636SDavid Howells 			       __func__, dvnode->vfs_inode.i_ino,
156*255ed636SDavid Howells 			       pos, offset, size, ntohs(block->hdr.magic));
157*255ed636SDavid Howells 			trace_afs_dir_check_failed(dvnode, pos + offset, i_size);
158*255ed636SDavid Howells 			kunmap_local(block);
159f51375cdSDavid Howells 			trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
1601da177e4SLinus Torvalds 			goto error;
1611da177e4SLinus Torvalds 		}
16263a4681fSDavid Howells 
16363a4681fSDavid Howells 		/* Make sure each block is NUL terminated so we can reasonably
164*255ed636SDavid Howells 		 * use string functions on it.  The filenames in the folio
16563a4681fSDavid Howells 		 * *should* be NUL-terminated anyway.
16663a4681fSDavid Howells 		 */
167*255ed636SDavid Howells 		((u8 *)block)[AFS_DIR_BLOCK_SIZE - 1] = 0;
168*255ed636SDavid Howells 
169*255ed636SDavid Howells 		kunmap_local(block);
1701da177e4SLinus Torvalds 	}
171dab17c1aSDavid Howells checked:
172f3ddee8dSDavid Howells 	afs_stat_v(dvnode, n_read_dir);
173be5b82dbSAl Viro 	return true;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds error:
176be5b82dbSAl Viro 	return false;
177ec26815aSDavid Howells }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds /*
180c4508464SDavid Howells  * Dump the contents of a directory.
181445b1028SDavid Howells  */
182c4508464SDavid Howells static void afs_dir_dump(struct afs_vnode *dvnode, struct afs_read *req)
183445b1028SDavid Howells {
184*255ed636SDavid Howells 	union afs_xdr_dir_block *block;
185c4508464SDavid Howells 	struct address_space *mapping = dvnode->vfs_inode.i_mapping;
186*255ed636SDavid Howells 	struct folio *folio;
187c4508464SDavid Howells 	pgoff_t last = req->nr_pages - 1;
188*255ed636SDavid Howells 	size_t offset, size;
189445b1028SDavid Howells 
190c4508464SDavid Howells 	XA_STATE(xas, &mapping->i_pages, 0);
191445b1028SDavid Howells 
192c4508464SDavid Howells 	pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx\n",
193445b1028SDavid Howells 		dvnode->fid.vid, dvnode->fid.vnode,
194c4508464SDavid Howells 		req->file_size, req->len, req->actual_len);
195c4508464SDavid Howells 	pr_warn("DIR %llx %x %zx %zx\n",
196c4508464SDavid Howells 		req->pos, req->nr_pages,
197c4508464SDavid Howells 		req->iter->iov_offset,  iov_iter_count(req->iter));
198445b1028SDavid Howells 
199*255ed636SDavid Howells 	xas_for_each(&xas, folio, last) {
200*255ed636SDavid Howells 		if (xas_retry(&xas, folio))
201c4508464SDavid Howells 			continue;
202445b1028SDavid Howells 
203*255ed636SDavid Howells 		BUG_ON(folio_file_mapping(folio) != mapping);
204c4508464SDavid Howells 
205*255ed636SDavid Howells 		size = min_t(loff_t, folio_size(folio), req->actual_len - folio_pos(folio));
206*255ed636SDavid Howells 		for (offset = 0; offset < size; offset += sizeof(*block)) {
207*255ed636SDavid Howells 			block = kmap_local_folio(folio, offset);
208*255ed636SDavid Howells 			pr_warn("[%02lx] %32phN\n", folio_index(folio) + offset, block);
209*255ed636SDavid Howells 			kunmap_local(block);
210445b1028SDavid Howells 		}
211445b1028SDavid Howells 	}
212c4508464SDavid Howells }
213c4508464SDavid Howells 
214c4508464SDavid Howells /*
215*255ed636SDavid Howells  * Check all the blocks in a directory.  All the folios are held pinned.
216c4508464SDavid Howells  */
217c4508464SDavid Howells static int afs_dir_check(struct afs_vnode *dvnode, struct afs_read *req)
218c4508464SDavid Howells {
219c4508464SDavid Howells 	struct address_space *mapping = dvnode->vfs_inode.i_mapping;
220*255ed636SDavid Howells 	struct folio *folio;
221c4508464SDavid Howells 	pgoff_t last = req->nr_pages - 1;
222c4508464SDavid Howells 	int ret = 0;
223c4508464SDavid Howells 
224c4508464SDavid Howells 	XA_STATE(xas, &mapping->i_pages, 0);
225c4508464SDavid Howells 
226c4508464SDavid Howells 	if (unlikely(!req->nr_pages))
227c4508464SDavid Howells 		return 0;
228c4508464SDavid Howells 
229c4508464SDavid Howells 	rcu_read_lock();
230*255ed636SDavid Howells 	xas_for_each(&xas, folio, last) {
231*255ed636SDavid Howells 		if (xas_retry(&xas, folio))
232c4508464SDavid Howells 			continue;
233c4508464SDavid Howells 
234*255ed636SDavid Howells 		BUG_ON(folio_file_mapping(folio) != mapping);
235c4508464SDavid Howells 
236*255ed636SDavid Howells 		if (!afs_dir_check_folio(dvnode, folio, req->actual_len)) {
237c4508464SDavid Howells 			afs_dir_dump(dvnode, req);
238c4508464SDavid Howells 			ret = -EIO;
239c4508464SDavid Howells 			break;
240c4508464SDavid Howells 		}
241c4508464SDavid Howells 	}
242c4508464SDavid Howells 
243c4508464SDavid Howells 	rcu_read_unlock();
244c4508464SDavid Howells 	return ret;
245445b1028SDavid Howells }
246445b1028SDavid Howells 
247445b1028SDavid Howells /*
2481da177e4SLinus Torvalds  * open an AFS directory file
2491da177e4SLinus Torvalds  */
2501da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	_enter("{%lu}", inode->i_ino);
2531da177e4SLinus Torvalds 
25400317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
25500317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
2561da177e4SLinus Torvalds 
25708e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
2581da177e4SLinus Torvalds 		return -ENOENT;
2591da177e4SLinus Torvalds 
26000d3b7a4SDavid Howells 	return afs_open(inode, file);
261ec26815aSDavid Howells }
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds /*
264f3ddee8dSDavid Howells  * Read the directory into the pagecache in one go, scrubbing the previous
265*255ed636SDavid Howells  * contents.  The list of folios is returned, pinning them so that they don't
266f3ddee8dSDavid Howells  * get reclaimed during the iteration.
267f3ddee8dSDavid Howells  */
268f3ddee8dSDavid Howells static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
269b61f7dcfSDavid Howells 	__acquires(&dvnode->validate_lock)
270f3ddee8dSDavid Howells {
271*255ed636SDavid Howells 	struct address_space *mapping = dvnode->vfs_inode.i_mapping;
272f3ddee8dSDavid Howells 	struct afs_read *req;
273f3ddee8dSDavid Howells 	loff_t i_size;
274*255ed636SDavid Howells 	int nr_pages, i;
275c4508464SDavid Howells 	int ret;
276f3ddee8dSDavid Howells 
277c4508464SDavid Howells 	_enter("");
278f3ddee8dSDavid Howells 
279c4508464SDavid Howells 	req = kzalloc(sizeof(*req), GFP_KERNEL);
280f3ddee8dSDavid Howells 	if (!req)
281f3ddee8dSDavid Howells 		return ERR_PTR(-ENOMEM);
282f3ddee8dSDavid Howells 
283f3ddee8dSDavid Howells 	refcount_set(&req->usage, 1);
284c4508464SDavid Howells 	req->vnode = dvnode;
285c69bf479SDavid Howells 	req->key = key_get(key);
286c4508464SDavid Howells 	req->cleanup = afs_dir_read_cleanup;
287c4508464SDavid Howells 
288c4508464SDavid Howells expand:
289c4508464SDavid Howells 	i_size = i_size_read(&dvnode->vfs_inode);
290c4508464SDavid Howells 	if (i_size < 2048) {
291c4508464SDavid Howells 		ret = afs_bad(dvnode, afs_file_error_dir_small);
292c4508464SDavid Howells 		goto error;
293c4508464SDavid Howells 	}
294c4508464SDavid Howells 	if (i_size > 2048 * 1024) {
295c4508464SDavid Howells 		trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
296c4508464SDavid Howells 		ret = -EFBIG;
297f3ddee8dSDavid Howells 		goto error;
298f3ddee8dSDavid Howells 	}
299f3ddee8dSDavid Howells 
300c4508464SDavid Howells 	_enter("%llu", i_size);
301c4508464SDavid Howells 
302c4508464SDavid Howells 	nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
303c4508464SDavid Howells 
304c4508464SDavid Howells 	req->actual_len = i_size; /* May change */
305c4508464SDavid Howells 	req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
306c4508464SDavid Howells 	req->data_version = dvnode->status.data_version; /* May change */
307c4508464SDavid Howells 	iov_iter_xarray(&req->def_iter, READ, &dvnode->vfs_inode.i_mapping->i_pages,
308c4508464SDavid Howells 			0, i_size);
309c4508464SDavid Howells 	req->iter = &req->def_iter;
310c4508464SDavid Howells 
311c4508464SDavid Howells 	/* Fill in any gaps that we might find where the memory reclaimer has
312*255ed636SDavid Howells 	 * been at work and pin all the folios.  If there are any gaps, we will
313f3ddee8dSDavid Howells 	 * need to reread the entire directory contents.
314f3ddee8dSDavid Howells 	 */
315c4508464SDavid Howells 	i = req->nr_pages;
316c4508464SDavid Howells 	while (i < nr_pages) {
317*255ed636SDavid Howells 		struct folio *folio;
318c4508464SDavid Howells 
319*255ed636SDavid Howells 		folio = filemap_get_folio(mapping, i);
320*255ed636SDavid Howells 		if (!folio) {
321f3ddee8dSDavid Howells 			if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
322f3ddee8dSDavid Howells 				afs_stat_v(dvnode, n_inval);
323f3ddee8dSDavid Howells 
324f3ddee8dSDavid Howells 			ret = -ENOMEM;
325*255ed636SDavid Howells 			folio = __filemap_get_folio(mapping,
326*255ed636SDavid Howells 						    i, FGP_LOCK | FGP_CREAT,
327*255ed636SDavid Howells 						    mapping->gfp_mask);
328*255ed636SDavid Howells 			if (!folio)
329f3ddee8dSDavid Howells 				goto error;
330*255ed636SDavid Howells 			folio_attach_private(folio, (void *)1);
331*255ed636SDavid Howells 			folio_unlock(folio);
332f3ddee8dSDavid Howells 		}
333*255ed636SDavid Howells 
334*255ed636SDavid Howells 		req->nr_pages += folio_nr_pages(folio);
335*255ed636SDavid Howells 		i += folio_nr_pages(folio);
336c4508464SDavid Howells 	}
337f3ddee8dSDavid Howells 
338f3ddee8dSDavid Howells 	/* If we're going to reload, we need to lock all the pages to prevent
339f3ddee8dSDavid Howells 	 * races.
340f3ddee8dSDavid Howells 	 */
341f3ddee8dSDavid Howells 	ret = -ERESTARTSYS;
342b61f7dcfSDavid Howells 	if (down_read_killable(&dvnode->validate_lock) < 0)
343b61f7dcfSDavid Howells 		goto error;
344f3ddee8dSDavid Howells 
345f3ddee8dSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
346f3ddee8dSDavid Howells 		goto success;
347f3ddee8dSDavid Howells 
348b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
349b61f7dcfSDavid Howells 	if (down_write_killable(&dvnode->validate_lock) < 0)
350b61f7dcfSDavid Howells 		goto error;
351b61f7dcfSDavid Howells 
352b61f7dcfSDavid Howells 	if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
35399987c56SDavid Howells 		trace_afs_reload_dir(dvnode);
354c69bf479SDavid Howells 		ret = afs_fetch_data(dvnode, req);
355f3ddee8dSDavid Howells 		if (ret < 0)
356b61f7dcfSDavid Howells 			goto error_unlock;
357f3ddee8dSDavid Howells 
358f3ddee8dSDavid Howells 		task_io_account_read(PAGE_SIZE * req->nr_pages);
359f3ddee8dSDavid Howells 
360c4508464SDavid Howells 		if (req->len < req->file_size) {
361c4508464SDavid Howells 			/* The content has grown, so we need to expand the
362c4508464SDavid Howells 			 * buffer.
363c4508464SDavid Howells 			 */
364c4508464SDavid Howells 			up_write(&dvnode->validate_lock);
365c4508464SDavid Howells 			goto expand;
366c4508464SDavid Howells 		}
367f3ddee8dSDavid Howells 
368f3ddee8dSDavid Howells 		/* Validate the data we just read. */
369c4508464SDavid Howells 		ret = afs_dir_check(dvnode, req);
370c4508464SDavid Howells 		if (ret < 0)
371b61f7dcfSDavid Howells 			goto error_unlock;
372f3ddee8dSDavid Howells 
373f3ddee8dSDavid Howells 		// TODO: Trim excess pages
374f3ddee8dSDavid Howells 
375f3ddee8dSDavid Howells 		set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
376f3ddee8dSDavid Howells 	}
377f3ddee8dSDavid Howells 
378b61f7dcfSDavid Howells 	downgrade_write(&dvnode->validate_lock);
379f3ddee8dSDavid Howells success:
380f3ddee8dSDavid Howells 	return req;
381f3ddee8dSDavid Howells 
382f3ddee8dSDavid Howells error_unlock:
383b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
384f3ddee8dSDavid Howells error:
385f3ddee8dSDavid Howells 	afs_put_read(req);
386f3ddee8dSDavid Howells 	_leave(" = %d", ret);
387f3ddee8dSDavid Howells 	return ERR_PTR(ret);
388f3ddee8dSDavid Howells }
389f3ddee8dSDavid Howells 
390f3ddee8dSDavid Howells /*
3911da177e4SLinus Torvalds  * deal with one block in an AFS directory
3921da177e4SLinus Torvalds  */
393f51375cdSDavid Howells static int afs_dir_iterate_block(struct afs_vnode *dvnode,
394f51375cdSDavid Howells 				 struct dir_context *ctx,
39500317636SDavid Howells 				 union afs_xdr_dir_block *block,
3961bbae9f8SAl Viro 				 unsigned blkoff)
3971da177e4SLinus Torvalds {
39800317636SDavid Howells 	union afs_xdr_dirent *dire;
399366911cdSDavid Howells 	unsigned offset, next, curr, nr_slots;
4001da177e4SLinus Torvalds 	size_t nlen;
4011bbae9f8SAl Viro 	int tmp;
4021da177e4SLinus Torvalds 
403*255ed636SDavid Howells 	_enter("%llx,%x", ctx->pos, blkoff);
4041da177e4SLinus Torvalds 
40500317636SDavid Howells 	curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	/* walk through the block, an entry at a time */
4084ea219a8SDavid Howells 	for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
4094ea219a8SDavid Howells 	     offset < AFS_DIR_SLOTS_PER_BLOCK;
4101da177e4SLinus Torvalds 	     offset = next
4111da177e4SLinus Torvalds 	     ) {
4121da177e4SLinus Torvalds 		/* skip entries marked unused in the bitmap */
41300317636SDavid Howells 		if (!(block->hdr.bitmap[offset / 8] &
4141da177e4SLinus Torvalds 		      (1 << (offset % 8)))) {
4155b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: unused",
41600317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block), offset);
417366911cdSDavid Howells 			next = offset + 1;
4181da177e4SLinus Torvalds 			if (offset >= curr)
4191bbae9f8SAl Viro 				ctx->pos = blkoff +
42000317636SDavid Howells 					next * sizeof(union afs_xdr_dirent);
4211da177e4SLinus Torvalds 			continue;
4221da177e4SLinus Torvalds 		}
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 		/* got a valid entry */
4251da177e4SLinus Torvalds 		dire = &block->dirents[offset];
4261da177e4SLinus Torvalds 		nlen = strnlen(dire->u.name,
4271da177e4SLinus Torvalds 			       sizeof(*block) -
42800317636SDavid Howells 			       offset * sizeof(union afs_xdr_dirent));
429366911cdSDavid Howells 		if (nlen > AFSNAMEMAX - 1) {
430366911cdSDavid Howells 			_debug("ENT[%zu]: name too long (len %u/%zu)",
431366911cdSDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block),
432366911cdSDavid Howells 			       offset, nlen);
433366911cdSDavid Howells 			return afs_bad(dvnode, afs_file_error_dir_name_too_long);
434366911cdSDavid Howells 		}
4351da177e4SLinus Torvalds 
4365b5e0928SAlexey Dobriyan 		_debug("ENT[%zu.%u]: %s %zu \"%s\"",
43700317636SDavid Howells 		       blkoff / sizeof(union afs_xdr_dir_block), offset,
4381da177e4SLinus Torvalds 		       (offset < curr ? "skip" : "fill"),
4391da177e4SLinus Torvalds 		       nlen, dire->u.name);
4401da177e4SLinus Torvalds 
441366911cdSDavid Howells 		nr_slots = afs_dir_calc_slots(nlen);
442366911cdSDavid Howells 		next = offset + nr_slots;
443366911cdSDavid Howells 		if (next > AFS_DIR_SLOTS_PER_BLOCK) {
4445b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]:"
445366911cdSDavid Howells 			       " %u extends beyond end dir block"
446366911cdSDavid Howells 			       " (len %zu)",
44700317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block),
448366911cdSDavid Howells 			       offset, next, nlen);
449f51375cdSDavid Howells 			return afs_bad(dvnode, afs_file_error_dir_over_end);
4501da177e4SLinus Torvalds 		}
451366911cdSDavid Howells 
452366911cdSDavid Howells 		/* Check that the name-extension dirents are all allocated */
453366911cdSDavid Howells 		for (tmp = 1; tmp < nr_slots; tmp++) {
454366911cdSDavid Howells 			unsigned int ix = offset + tmp;
455366911cdSDavid Howells 			if (!(block->hdr.bitmap[ix / 8] & (1 << (ix % 8)))) {
456366911cdSDavid Howells 				_debug("ENT[%zu.u]:"
457366911cdSDavid Howells 				       " %u unmarked extension (%u/%u)",
45800317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
459366911cdSDavid Howells 				       offset, tmp, nr_slots);
460f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
4611da177e4SLinus Torvalds 			}
4621da177e4SLinus Torvalds 		}
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 		/* skip if starts before the current position */
4651da177e4SLinus Torvalds 		if (offset < curr)
4661da177e4SLinus Torvalds 			continue;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 		/* found the next entry */
4691bbae9f8SAl Viro 		if (!dir_emit(ctx, dire->u.name, nlen,
4701da177e4SLinus Torvalds 			      ntohl(dire->u.vnode),
4715cf9dd55SDavid Howells 			      (ctx->actor == afs_lookup_filldir ||
4725cf9dd55SDavid Howells 			       ctx->actor == afs_lookup_one_filldir)?
4731bbae9f8SAl Viro 			      ntohl(dire->u.unique) : DT_UNKNOWN)) {
4741da177e4SLinus Torvalds 			_leave(" = 0 [full]");
4751da177e4SLinus Torvalds 			return 0;
4761da177e4SLinus Torvalds 		}
4771da177e4SLinus Torvalds 
47800317636SDavid Howells 		ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	_leave(" = 1 [more]");
4821da177e4SLinus Torvalds 	return 1;
483ec26815aSDavid Howells }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /*
48608e0e7c8SDavid Howells  * iterate through the data blob that lists the contents of an AFS directory
4871da177e4SLinus Torvalds  */
4881bbae9f8SAl Viro static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
4899dd0b82eSDavid Howells 			   struct key *key, afs_dataversion_t *_dir_version)
4901da177e4SLinus Torvalds {
491f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
49200317636SDavid Howells 	union afs_xdr_dir_block *dblock;
493f3ddee8dSDavid Howells 	struct afs_read *req;
494*255ed636SDavid Howells 	struct folio *folio;
495*255ed636SDavid Howells 	unsigned offset, size;
4961da177e4SLinus Torvalds 	int ret;
4971da177e4SLinus Torvalds 
4981bbae9f8SAl Viro 	_enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
4991da177e4SLinus Torvalds 
50008e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
5011da177e4SLinus Torvalds 		_leave(" = -ESTALE");
5021da177e4SLinus Torvalds 		return -ESTALE;
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 
505f3ddee8dSDavid Howells 	req = afs_read_dir(dvnode, key);
506f3ddee8dSDavid Howells 	if (IS_ERR(req))
507f3ddee8dSDavid Howells 		return PTR_ERR(req);
5089dd0b82eSDavid Howells 	*_dir_version = req->data_version;
509f3ddee8dSDavid Howells 
5101da177e4SLinus Torvalds 	/* round the file position up to the next entry boundary */
51100317636SDavid Howells 	ctx->pos += sizeof(union afs_xdr_dirent) - 1;
51200317636SDavid Howells 	ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	/* walk through the blocks in sequence */
5151da177e4SLinus Torvalds 	ret = 0;
516f3ddee8dSDavid Howells 	while (ctx->pos < req->actual_len) {
517*255ed636SDavid Howells 		/* Fetch the appropriate folio from the directory and re-add it
518c4508464SDavid Howells 		 * to the LRU.  We have all the pages pinned with an extra ref.
519f3ddee8dSDavid Howells 		 */
520*255ed636SDavid Howells 		folio = __filemap_get_folio(dir->i_mapping, ctx->pos / PAGE_SIZE,
521*255ed636SDavid Howells 					    FGP_ACCESSED, 0);
522*255ed636SDavid Howells 		if (!folio) {
523f51375cdSDavid Howells 			ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
5241da177e4SLinus Torvalds 			break;
5251da177e4SLinus Torvalds 		}
5261da177e4SLinus Torvalds 
527*255ed636SDavid Howells 		offset = round_down(ctx->pos, sizeof(*dblock)) - folio_file_pos(folio);
528*255ed636SDavid Howells 		size = min_t(loff_t, folio_size(folio),
529*255ed636SDavid Howells 			     req->actual_len - folio_file_pos(folio));
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 		do {
532*255ed636SDavid Howells 			dblock = kmap_local_folio(folio, offset);
533*255ed636SDavid Howells 			ret = afs_dir_iterate_block(dvnode, ctx, dblock,
534*255ed636SDavid Howells 						    folio_file_pos(folio) + offset);
535*255ed636SDavid Howells 			kunmap_local(dblock);
536*255ed636SDavid Howells 			if (ret != 1)
5371da177e4SLinus Torvalds 				goto out;
5381da177e4SLinus Torvalds 
539*255ed636SDavid Howells 		} while (offset += sizeof(*dblock), offset < size);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 		ret = 0;
5421da177e4SLinus Torvalds 	}
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds out:
545b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
546f3ddee8dSDavid Howells 	afs_put_read(req);
5471da177e4SLinus Torvalds 	_leave(" = %d", ret);
5481da177e4SLinus Torvalds 	return ret;
549ec26815aSDavid Howells }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /*
5521da177e4SLinus Torvalds  * read an AFS directory
5531da177e4SLinus Torvalds  */
5541bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx)
5551da177e4SLinus Torvalds {
5569dd0b82eSDavid Howells 	afs_dataversion_t dir_version;
5579dd0b82eSDavid Howells 
5589dd0b82eSDavid Howells 	return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file),
5599dd0b82eSDavid Howells 			       &dir_version);
560ec26815aSDavid Howells }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds /*
5635cf9dd55SDavid Howells  * Search the directory for a single name
5641da177e4SLinus Torvalds  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5651da177e4SLinus Torvalds  *   uniquifier through dtype
5661da177e4SLinus Torvalds  */
5675cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
568ac7576f4SMiklos Szeredi 				  int nlen, loff_t fpos, u64 ino, unsigned dtype)
5691da177e4SLinus Torvalds {
5705cf9dd55SDavid Howells 	struct afs_lookup_one_cookie *cookie =
5715cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_one_cookie, ctx);
5721da177e4SLinus Torvalds 
5731bbae9f8SAl Viro 	_enter("{%s,%u},%s,%u,,%llu,%u",
5741bbae9f8SAl Viro 	       cookie->name.name, cookie->name.len, name, nlen,
575ba3e0e1aSDavid S. Miller 	       (unsigned long long) ino, dtype);
5761da177e4SLinus Torvalds 
57708e0e7c8SDavid Howells 	/* insanity checks first */
57800317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
57900317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
58008e0e7c8SDavid Howells 
5811bbae9f8SAl Viro 	if (cookie->name.len != nlen ||
5821bbae9f8SAl Viro 	    memcmp(cookie->name.name, name, nlen) != 0) {
5831da177e4SLinus Torvalds 		_leave(" = 0 [no]");
5841da177e4SLinus Torvalds 		return 0;
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	cookie->fid.vnode = ino;
5881da177e4SLinus Torvalds 	cookie->fid.unique = dtype;
5891da177e4SLinus Torvalds 	cookie->found = 1;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	_leave(" = -1 [found]");
5921da177e4SLinus Torvalds 	return -1;
593ec26815aSDavid Howells }
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds /*
5965cf9dd55SDavid Howells  * Do a lookup of a single name in a directory
597260a9803SDavid Howells  * - just returns the FID the dentry name maps to if found
5981da177e4SLinus Torvalds  */
5995cf9dd55SDavid Howells static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
6009dd0b82eSDavid Howells 			     struct afs_fid *fid, struct key *key,
6019dd0b82eSDavid Howells 			     afs_dataversion_t *_dir_version)
6021da177e4SLinus Torvalds {
6031bbae9f8SAl Viro 	struct afs_super_info *as = dir->i_sb->s_fs_info;
6045cf9dd55SDavid Howells 	struct afs_lookup_one_cookie cookie = {
6055cf9dd55SDavid Howells 		.ctx.actor = afs_lookup_one_filldir,
6061bbae9f8SAl Viro 		.name = dentry->d_name,
6071bbae9f8SAl Viro 		.fid.vid = as->volume->vid
6081bbae9f8SAl Viro 	};
6091da177e4SLinus Torvalds 	int ret;
6101da177e4SLinus Torvalds 
611a455589fSAl Viro 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	/* search the directory */
6149dd0b82eSDavid Howells 	ret = afs_dir_iterate(dir, &cookie.ctx, key, _dir_version);
6151da177e4SLinus Torvalds 	if (ret < 0) {
61608e0e7c8SDavid Howells 		_leave(" = %d [iter]", ret);
61708e0e7c8SDavid Howells 		return ret;
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	if (!cookie.found) {
62108e0e7c8SDavid Howells 		_leave(" = -ENOENT [not found]");
62208e0e7c8SDavid Howells 		return -ENOENT;
62308e0e7c8SDavid Howells 	}
62408e0e7c8SDavid Howells 
62508e0e7c8SDavid Howells 	*fid = cookie.fid;
6263b6492dfSDavid Howells 	_leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
62708e0e7c8SDavid Howells 	return 0;
62808e0e7c8SDavid Howells }
62908e0e7c8SDavid Howells 
63008e0e7c8SDavid Howells /*
6315cf9dd55SDavid Howells  * search the directory for a name
6325cf9dd55SDavid Howells  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
6335cf9dd55SDavid Howells  *   uniquifier through dtype
6345cf9dd55SDavid Howells  */
6355cf9dd55SDavid Howells static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
6365cf9dd55SDavid Howells 			      int nlen, loff_t fpos, u64 ino, unsigned dtype)
6375cf9dd55SDavid Howells {
6385cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie =
6395cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_cookie, ctx);
6405cf9dd55SDavid Howells 	int ret;
6415cf9dd55SDavid Howells 
6425cf9dd55SDavid Howells 	_enter("{%s,%u},%s,%u,,%llu,%u",
6435cf9dd55SDavid Howells 	       cookie->name.name, cookie->name.len, name, nlen,
6445cf9dd55SDavid Howells 	       (unsigned long long) ino, dtype);
6455cf9dd55SDavid Howells 
6465cf9dd55SDavid Howells 	/* insanity checks first */
64700317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
64800317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
6495cf9dd55SDavid Howells 
6505cf9dd55SDavid Howells 	if (cookie->found) {
6515cf9dd55SDavid Howells 		if (cookie->nr_fids < 50) {
6525cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].vnode	= ino;
6535cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].unique	= dtype;
6545cf9dd55SDavid Howells 			cookie->nr_fids++;
6555cf9dd55SDavid Howells 		}
6565cf9dd55SDavid Howells 	} else if (cookie->name.len == nlen &&
6575cf9dd55SDavid Howells 		   memcmp(cookie->name.name, name, nlen) == 0) {
658e49c7b2fSDavid Howells 		cookie->fids[1].vnode	= ino;
659e49c7b2fSDavid Howells 		cookie->fids[1].unique	= dtype;
6605cf9dd55SDavid Howells 		cookie->found = 1;
6615cf9dd55SDavid Howells 		if (cookie->one_only)
6625cf9dd55SDavid Howells 			return -1;
6635cf9dd55SDavid Howells 	}
6645cf9dd55SDavid Howells 
6655cf9dd55SDavid Howells 	ret = cookie->nr_fids >= 50 ? -1 : 0;
6665cf9dd55SDavid Howells 	_leave(" = %d", ret);
6675cf9dd55SDavid Howells 	return ret;
6685cf9dd55SDavid Howells }
6695cf9dd55SDavid Howells 
6705cf9dd55SDavid Howells /*
671e49c7b2fSDavid Howells  * Deal with the result of a successful lookup operation.  Turn all the files
672e49c7b2fSDavid Howells  * into inodes and save the first one - which is the one we actually want.
673e49c7b2fSDavid Howells  */
674e49c7b2fSDavid Howells static void afs_do_lookup_success(struct afs_operation *op)
675e49c7b2fSDavid Howells {
676e49c7b2fSDavid Howells 	struct afs_vnode_param *vp;
677e49c7b2fSDavid Howells 	struct afs_vnode *vnode;
678e49c7b2fSDavid Howells 	struct inode *inode;
679e49c7b2fSDavid Howells 	u32 abort_code;
680e49c7b2fSDavid Howells 	int i;
681e49c7b2fSDavid Howells 
682e49c7b2fSDavid Howells 	_enter("");
683e49c7b2fSDavid Howells 
684e49c7b2fSDavid Howells 	for (i = 0; i < op->nr_files; i++) {
685e49c7b2fSDavid Howells 		switch (i) {
686e49c7b2fSDavid Howells 		case 0:
687e49c7b2fSDavid Howells 			vp = &op->file[0];
688e49c7b2fSDavid Howells 			abort_code = vp->scb.status.abort_code;
689e49c7b2fSDavid Howells 			if (abort_code != 0) {
69044767c35SDavid Howells 				op->ac.abort_code = abort_code;
691e49c7b2fSDavid Howells 				op->error = afs_abort_to_error(abort_code);
692e49c7b2fSDavid Howells 			}
693e49c7b2fSDavid Howells 			break;
694e49c7b2fSDavid Howells 
695e49c7b2fSDavid Howells 		case 1:
696e49c7b2fSDavid Howells 			vp = &op->file[1];
697e49c7b2fSDavid Howells 			break;
698e49c7b2fSDavid Howells 
699e49c7b2fSDavid Howells 		default:
700e49c7b2fSDavid Howells 			vp = &op->more_files[i - 2];
701e49c7b2fSDavid Howells 			break;
702e49c7b2fSDavid Howells 		}
703e49c7b2fSDavid Howells 
704e49c7b2fSDavid Howells 		if (!vp->scb.have_status && !vp->scb.have_error)
705e49c7b2fSDavid Howells 			continue;
706e49c7b2fSDavid Howells 
707e49c7b2fSDavid Howells 		_debug("do [%u]", i);
708e49c7b2fSDavid Howells 		if (vp->vnode) {
709e49c7b2fSDavid Howells 			if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags))
710e49c7b2fSDavid Howells 				afs_vnode_commit_status(op, vp);
711e49c7b2fSDavid Howells 		} else if (vp->scb.status.abort_code == 0) {
712e49c7b2fSDavid Howells 			inode = afs_iget(op, vp);
713e49c7b2fSDavid Howells 			if (!IS_ERR(inode)) {
714e49c7b2fSDavid Howells 				vnode = AFS_FS_I(inode);
715e49c7b2fSDavid Howells 				afs_cache_permit(vnode, op->key,
716e49c7b2fSDavid Howells 						 0 /* Assume vnode->cb_break is 0 */ +
717e49c7b2fSDavid Howells 						 op->cb_v_break,
718e49c7b2fSDavid Howells 						 &vp->scb);
719e49c7b2fSDavid Howells 				vp->vnode = vnode;
720e49c7b2fSDavid Howells 				vp->put_vnode = true;
721e49c7b2fSDavid Howells 			}
722e49c7b2fSDavid Howells 		} else {
723e49c7b2fSDavid Howells 			_debug("- abort %d %llx:%llx.%x",
724e49c7b2fSDavid Howells 			       vp->scb.status.abort_code,
725e49c7b2fSDavid Howells 			       vp->fid.vid, vp->fid.vnode, vp->fid.unique);
726e49c7b2fSDavid Howells 		}
727e49c7b2fSDavid Howells 	}
728e49c7b2fSDavid Howells 
729e49c7b2fSDavid Howells 	_leave("");
730e49c7b2fSDavid Howells }
731e49c7b2fSDavid Howells 
732e49c7b2fSDavid Howells static const struct afs_operation_ops afs_inline_bulk_status_operation = {
733e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_inline_bulk_status,
734e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_inline_bulk_status,
735e49c7b2fSDavid Howells 	.success	= afs_do_lookup_success,
736e49c7b2fSDavid Howells };
737e49c7b2fSDavid Howells 
738b6489a49SDavid Howells static const struct afs_operation_ops afs_lookup_fetch_status_operation = {
739e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_fetch_status,
740e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_fetch_status,
741e49c7b2fSDavid Howells 	.success	= afs_do_lookup_success,
742728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
743e49c7b2fSDavid Howells };
744e49c7b2fSDavid Howells 
745e49c7b2fSDavid Howells /*
74620325960SDavid Howells  * See if we know that the server we expect to use doesn't support
74720325960SDavid Howells  * FS.InlineBulkStatus.
74820325960SDavid Howells  */
74920325960SDavid Howells static bool afs_server_supports_ibulk(struct afs_vnode *dvnode)
75020325960SDavid Howells {
75120325960SDavid Howells 	struct afs_server_list *slist;
75220325960SDavid Howells 	struct afs_volume *volume = dvnode->volume;
75320325960SDavid Howells 	struct afs_server *server;
75420325960SDavid Howells 	bool ret = true;
75520325960SDavid Howells 	int i;
75620325960SDavid Howells 
75720325960SDavid Howells 	if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags))
75820325960SDavid Howells 		return true;
75920325960SDavid Howells 
76020325960SDavid Howells 	rcu_read_lock();
76120325960SDavid Howells 	slist = rcu_dereference(volume->servers);
76220325960SDavid Howells 
76320325960SDavid Howells 	for (i = 0; i < slist->nr_servers; i++) {
76420325960SDavid Howells 		server = slist->servers[i].server;
76520325960SDavid Howells 		if (server == dvnode->cb_server) {
76620325960SDavid Howells 			if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
76720325960SDavid Howells 				ret = false;
76820325960SDavid Howells 			break;
76920325960SDavid Howells 		}
77020325960SDavid Howells 	}
77120325960SDavid Howells 
77220325960SDavid Howells 	rcu_read_unlock();
77320325960SDavid Howells 	return ret;
77420325960SDavid Howells }
77520325960SDavid Howells 
77620325960SDavid Howells /*
7775cf9dd55SDavid Howells  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
7785cf9dd55SDavid Howells  * files in one go and create inodes for them.  The inode of the file we were
7795cf9dd55SDavid Howells  * asked for is returned.
7805cf9dd55SDavid Howells  */
7815cf9dd55SDavid Howells static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
7825cf9dd55SDavid Howells 				   struct key *key)
7835cf9dd55SDavid Howells {
7845cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie;
785e49c7b2fSDavid Howells 	struct afs_vnode_param *vp;
786e49c7b2fSDavid Howells 	struct afs_operation *op;
78739db9815SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
78839db9815SDavid Howells 	struct inode *inode = NULL, *ti;
7899dd0b82eSDavid Howells 	afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version);
790e49c7b2fSDavid Howells 	long ret;
791e49c7b2fSDavid Howells 	int i;
7925cf9dd55SDavid Howells 
7935cf9dd55SDavid Howells 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
7945cf9dd55SDavid Howells 
7955cf9dd55SDavid Howells 	cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
7965cf9dd55SDavid Howells 	if (!cookie)
7975cf9dd55SDavid Howells 		return ERR_PTR(-ENOMEM);
7985cf9dd55SDavid Howells 
799e49c7b2fSDavid Howells 	for (i = 0; i < ARRAY_SIZE(cookie->fids); i++)
800e49c7b2fSDavid Howells 		cookie->fids[i].vid = dvnode->fid.vid;
8015cf9dd55SDavid Howells 	cookie->ctx.actor = afs_lookup_filldir;
8025cf9dd55SDavid Howells 	cookie->name = dentry->d_name;
80313fcc635SDavid Howells 	cookie->nr_fids = 2; /* slot 0 is saved for the fid we actually want
80413fcc635SDavid Howells 			      * and slot 1 for the directory */
8055cf9dd55SDavid Howells 
80620325960SDavid Howells 	if (!afs_server_supports_ibulk(dvnode))
8075cf9dd55SDavid Howells 		cookie->one_only = true;
8085cf9dd55SDavid Howells 
8095cf9dd55SDavid Howells 	/* search the directory */
8109dd0b82eSDavid Howells 	ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version);
811e49c7b2fSDavid Howells 	if (ret < 0)
8125cf9dd55SDavid Howells 		goto out;
8135cf9dd55SDavid Howells 
8149dd0b82eSDavid Howells 	dentry->d_fsdata = (void *)(unsigned long)data_version;
8159dd0b82eSDavid Howells 
816e49c7b2fSDavid Howells 	ret = -ENOENT;
8175cf9dd55SDavid Howells 	if (!cookie->found)
8185cf9dd55SDavid Howells 		goto out;
8195cf9dd55SDavid Howells 
8205cf9dd55SDavid Howells 	/* Check to see if we already have an inode for the primary fid. */
821e49c7b2fSDavid Howells 	inode = ilookup5(dir->i_sb, cookie->fids[1].vnode,
822e49c7b2fSDavid Howells 			 afs_ilookup5_test_by_fid, &cookie->fids[1]);
8235cf9dd55SDavid Howells 	if (inode)
824e49c7b2fSDavid Howells 		goto out; /* We do */
825e49c7b2fSDavid Howells 
826e49c7b2fSDavid Howells 	/* Okay, we didn't find it.  We need to query the server - and whilst
827e49c7b2fSDavid Howells 	 * we're doing that, we're going to attempt to look up a bunch of other
828e49c7b2fSDavid Howells 	 * vnodes also.
829e49c7b2fSDavid Howells 	 */
830e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
831e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
832e49c7b2fSDavid Howells 		ret = PTR_ERR(op);
8335cf9dd55SDavid Howells 		goto out;
834e49c7b2fSDavid Howells 	}
835e49c7b2fSDavid Howells 
836e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
837e49c7b2fSDavid Howells 	afs_op_set_fid(op, 1, &cookie->fids[1]);
838e49c7b2fSDavid Howells 
839e49c7b2fSDavid Howells 	op->nr_files = cookie->nr_fids;
840e49c7b2fSDavid Howells 	_debug("nr_files %u", op->nr_files);
8415cf9dd55SDavid Howells 
8425cf9dd55SDavid Howells 	/* Need space for examining all the selected files */
843e49c7b2fSDavid Howells 	op->error = -ENOMEM;
844e49c7b2fSDavid Howells 	if (op->nr_files > 2) {
845e49c7b2fSDavid Howells 		op->more_files = kvcalloc(op->nr_files - 2,
846e49c7b2fSDavid Howells 					  sizeof(struct afs_vnode_param),
8475cf9dd55SDavid Howells 					  GFP_KERNEL);
848e49c7b2fSDavid Howells 		if (!op->more_files)
849e49c7b2fSDavid Howells 			goto out_op;
8505cf9dd55SDavid Howells 
851e49c7b2fSDavid Howells 		for (i = 2; i < op->nr_files; i++) {
852e49c7b2fSDavid Howells 			vp = &op->more_files[i - 2];
853e49c7b2fSDavid Howells 			vp->fid = cookie->fids[i];
85439db9815SDavid Howells 
85539db9815SDavid Howells 			/* Find any inodes that already exist and get their
85639db9815SDavid Howells 			 * callback counters.
85739db9815SDavid Howells 			 */
858e49c7b2fSDavid Howells 			ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode,
859e49c7b2fSDavid Howells 					     afs_ilookup5_test_by_fid, &vp->fid);
86039db9815SDavid Howells 			if (!IS_ERR_OR_NULL(ti)) {
86139db9815SDavid Howells 				vnode = AFS_FS_I(ti);
862e49c7b2fSDavid Howells 				vp->dv_before = vnode->status.data_version;
863e49c7b2fSDavid Howells 				vp->cb_break_before = afs_calc_vnode_cb_break(vnode);
864e49c7b2fSDavid Howells 				vp->vnode = vnode;
865e49c7b2fSDavid Howells 				vp->put_vnode = true;
866a9e5c87cSDavid Howells 				vp->speculative = true; /* vnode not locked */
867e49c7b2fSDavid Howells 			}
86839db9815SDavid Howells 		}
86939db9815SDavid Howells 	}
87039db9815SDavid Howells 
8715cf9dd55SDavid Howells 	/* Try FS.InlineBulkStatus first.  Abort codes for the individual
8725cf9dd55SDavid Howells 	 * lookups contained therein are stored in the reply without aborting
8735cf9dd55SDavid Howells 	 * the whole operation.
8745cf9dd55SDavid Howells 	 */
875e49c7b2fSDavid Howells 	op->error = -ENOTSUPP;
876e49c7b2fSDavid Howells 	if (!cookie->one_only) {
877e49c7b2fSDavid Howells 		op->ops = &afs_inline_bulk_status_operation;
878e49c7b2fSDavid Howells 		afs_begin_vnode_operation(op);
879e49c7b2fSDavid Howells 		afs_wait_for_operation(op);
8805cf9dd55SDavid Howells 	}
8815cf9dd55SDavid Howells 
882e49c7b2fSDavid Howells 	if (op->error == -ENOTSUPP) {
883e49c7b2fSDavid Howells 		/* We could try FS.BulkStatus next, but this aborts the entire
884e49c7b2fSDavid Howells 		 * op if any of the lookups fails - so, for the moment, revert
885e49c7b2fSDavid Howells 		 * to FS.FetchStatus for op->file[1].
8865cf9dd55SDavid Howells 		 */
887e49c7b2fSDavid Howells 		op->fetch_status.which = 1;
888f8ea5c7bSDavid Howells 		op->ops = &afs_lookup_fetch_status_operation;
889e49c7b2fSDavid Howells 		afs_begin_vnode_operation(op);
890e49c7b2fSDavid Howells 		afs_wait_for_operation(op);
891e49c7b2fSDavid Howells 	}
892e49c7b2fSDavid Howells 	inode = ERR_PTR(op->error);
893e49c7b2fSDavid Howells 
894e49c7b2fSDavid Howells out_op:
895e49c7b2fSDavid Howells 	if (op->error == 0) {
896e49c7b2fSDavid Howells 		inode = &op->file[1].vnode->vfs_inode;
897e49c7b2fSDavid Howells 		op->file[1].vnode = NULL;
8985cf9dd55SDavid Howells 	}
8995cf9dd55SDavid Howells 
900e49c7b2fSDavid Howells 	if (op->file[0].scb.have_status)
901e49c7b2fSDavid Howells 		dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version;
902e49c7b2fSDavid Howells 	else
903e49c7b2fSDavid Howells 		dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before;
904e49c7b2fSDavid Howells 	ret = afs_put_operation(op);
9055cf9dd55SDavid Howells out:
9065cf9dd55SDavid Howells 	kfree(cookie);
907e49c7b2fSDavid Howells 	_leave("");
908e49c7b2fSDavid Howells 	return inode ?: ERR_PTR(ret);
9095cf9dd55SDavid Howells }
9105cf9dd55SDavid Howells 
9115cf9dd55SDavid Howells /*
9126f8880d8SDavid Howells  * Look up an entry in a directory with @sys substitution.
9136f8880d8SDavid Howells  */
9146f8880d8SDavid Howells static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
9156f8880d8SDavid Howells 				       struct key *key)
9166f8880d8SDavid Howells {
9176f8880d8SDavid Howells 	struct afs_sysnames *subs;
9186f8880d8SDavid Howells 	struct afs_net *net = afs_i2net(dir);
9196f8880d8SDavid Howells 	struct dentry *ret;
9206f8880d8SDavid Howells 	char *buf, *p, *name;
9216f8880d8SDavid Howells 	int len, i;
9226f8880d8SDavid Howells 
9236f8880d8SDavid Howells 	_enter("");
9246f8880d8SDavid Howells 
9256f8880d8SDavid Howells 	ret = ERR_PTR(-ENOMEM);
9266f8880d8SDavid Howells 	p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
9276f8880d8SDavid Howells 	if (!buf)
9286f8880d8SDavid Howells 		goto out_p;
9296f8880d8SDavid Howells 	if (dentry->d_name.len > 4) {
9306f8880d8SDavid Howells 		memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
9316f8880d8SDavid Howells 		p += dentry->d_name.len - 4;
9326f8880d8SDavid Howells 	}
9336f8880d8SDavid Howells 
9346f8880d8SDavid Howells 	/* There is an ordered list of substitutes that we have to try. */
9356f8880d8SDavid Howells 	read_lock(&net->sysnames_lock);
9366f8880d8SDavid Howells 	subs = net->sysnames;
9376f8880d8SDavid Howells 	refcount_inc(&subs->usage);
9386f8880d8SDavid Howells 	read_unlock(&net->sysnames_lock);
9396f8880d8SDavid Howells 
9406f8880d8SDavid Howells 	for (i = 0; i < subs->nr; i++) {
9416f8880d8SDavid Howells 		name = subs->subs[i];
9426f8880d8SDavid Howells 		len = dentry->d_name.len - 4 + strlen(name);
9436f8880d8SDavid Howells 		if (len >= AFSNAMEMAX) {
9446f8880d8SDavid Howells 			ret = ERR_PTR(-ENAMETOOLONG);
9456f8880d8SDavid Howells 			goto out_s;
9466f8880d8SDavid Howells 		}
9476f8880d8SDavid Howells 
9486f8880d8SDavid Howells 		strcpy(p, name);
9496f8880d8SDavid Howells 		ret = lookup_one_len(buf, dentry->d_parent, len);
9506f8880d8SDavid Howells 		if (IS_ERR(ret) || d_is_positive(ret))
9516f8880d8SDavid Howells 			goto out_s;
9526f8880d8SDavid Howells 		dput(ret);
9536f8880d8SDavid Howells 	}
9546f8880d8SDavid Howells 
9556f8880d8SDavid Howells 	/* We don't want to d_add() the @sys dentry here as we don't want to
9566f8880d8SDavid Howells 	 * the cached dentry to hide changes to the sysnames list.
9576f8880d8SDavid Howells 	 */
9586f8880d8SDavid Howells 	ret = NULL;
9596f8880d8SDavid Howells out_s:
9606f8880d8SDavid Howells 	afs_put_sysnames(subs);
9616f8880d8SDavid Howells 	kfree(buf);
9626f8880d8SDavid Howells out_p:
9636f8880d8SDavid Howells 	key_put(key);
9646f8880d8SDavid Howells 	return ret;
9656f8880d8SDavid Howells }
9666f8880d8SDavid Howells 
9676f8880d8SDavid Howells /*
96808e0e7c8SDavid Howells  * look up an entry in a directory
96908e0e7c8SDavid Howells  */
970260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
97100cd8dd3SAl Viro 				 unsigned int flags)
97208e0e7c8SDavid Howells {
9735cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
97440a708bdSDavid Howells 	struct afs_fid fid = {};
97508e0e7c8SDavid Howells 	struct inode *inode;
97634b2a88fSAl Viro 	struct dentry *d;
97700d3b7a4SDavid Howells 	struct key *key;
97808e0e7c8SDavid Howells 	int ret;
97908e0e7c8SDavid Howells 
9803b6492dfSDavid Howells 	_enter("{%llx:%llu},%p{%pd},",
9815cf9dd55SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
982260a9803SDavid Howells 
9832b0143b5SDavid Howells 	ASSERTCMP(d_inode(dentry), ==, NULL);
98408e0e7c8SDavid Howells 
98545222b9eSDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX) {
98608e0e7c8SDavid Howells 		_leave(" = -ENAMETOOLONG");
98708e0e7c8SDavid Howells 		return ERR_PTR(-ENAMETOOLONG);
98808e0e7c8SDavid Howells 	}
98908e0e7c8SDavid Howells 
9905cf9dd55SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
99108e0e7c8SDavid Howells 		_leave(" = -ESTALE");
99208e0e7c8SDavid Howells 		return ERR_PTR(-ESTALE);
99308e0e7c8SDavid Howells 	}
99408e0e7c8SDavid Howells 
9955cf9dd55SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
99600d3b7a4SDavid Howells 	if (IS_ERR(key)) {
99700d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
998e231c2eeSDavid Howells 		return ERR_CAST(key);
99900d3b7a4SDavid Howells 	}
100000d3b7a4SDavid Howells 
10015cf9dd55SDavid Howells 	ret = afs_validate(dvnode, key);
100208e0e7c8SDavid Howells 	if (ret < 0) {
100300d3b7a4SDavid Howells 		key_put(key);
1004260a9803SDavid Howells 		_leave(" = %d [val]", ret);
10051da177e4SLinus Torvalds 		return ERR_PTR(ret);
10061da177e4SLinus Torvalds 	}
10071da177e4SLinus Torvalds 
10086f8880d8SDavid Howells 	if (dentry->d_name.len >= 4 &&
10096f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
10106f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
10116f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
10126f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 1] == 's')
10136f8880d8SDavid Howells 		return afs_lookup_atsys(dir, dentry, key);
10146f8880d8SDavid Howells 
1015d55b4da4SDavid Howells 	afs_stat_v(dvnode, n_lookup);
10165cf9dd55SDavid Howells 	inode = afs_do_lookup(dir, dentry, key);
101734b2a88fSAl Viro 	key_put(key);
1018f52b83b0SDavid Howells 	if (inode == ERR_PTR(-ENOENT))
10195cf9dd55SDavid Howells 		inode = afs_try_auto_mntpt(dentry, dir);
102040a708bdSDavid Howells 
102140a708bdSDavid Howells 	if (!IS_ERR_OR_NULL(inode))
102240a708bdSDavid Howells 		fid = AFS_FS_I(inode)->fid;
102340a708bdSDavid Howells 
1024fed79fd7SDavid Howells 	_debug("splice %p", dentry->d_inode);
102534b2a88fSAl Viro 	d = d_splice_alias(inode, dentry);
102680548b03SDavid Howells 	if (!IS_ERR_OR_NULL(d)) {
102734b2a88fSAl Viro 		d->d_fsdata = dentry->d_fsdata;
102840a708bdSDavid Howells 		trace_afs_lookup(dvnode, &d->d_name, &fid);
102980548b03SDavid Howells 	} else {
103040a708bdSDavid Howells 		trace_afs_lookup(dvnode, &dentry->d_name, &fid);
103180548b03SDavid Howells 	}
1032e49c7b2fSDavid Howells 	_leave("");
103334b2a88fSAl Viro 	return d;
1034ec26815aSDavid Howells }
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds /*
1037a0753c29SDavid Howells  * Check the validity of a dentry under RCU conditions.
1038a0753c29SDavid Howells  */
1039a0753c29SDavid Howells static int afs_d_revalidate_rcu(struct dentry *dentry)
1040a0753c29SDavid Howells {
104163d49d84SDavid Howells 	struct afs_vnode *dvnode;
1042a0753c29SDavid Howells 	struct dentry *parent;
104363d49d84SDavid Howells 	struct inode *dir;
1044a0753c29SDavid Howells 	long dir_version, de_version;
1045a0753c29SDavid Howells 
1046a0753c29SDavid Howells 	_enter("%p", dentry);
1047a0753c29SDavid Howells 
1048a0753c29SDavid Howells 	/* Check the parent directory is still valid first. */
1049a0753c29SDavid Howells 	parent = READ_ONCE(dentry->d_parent);
1050a0753c29SDavid Howells 	dir = d_inode_rcu(parent);
1051a0753c29SDavid Howells 	if (!dir)
1052a0753c29SDavid Howells 		return -ECHILD;
1053a0753c29SDavid Howells 	dvnode = AFS_FS_I(dir);
1054a0753c29SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags))
1055a0753c29SDavid Howells 		return -ECHILD;
1056a0753c29SDavid Howells 
1057a0753c29SDavid Howells 	if (!afs_check_validity(dvnode))
1058a0753c29SDavid Howells 		return -ECHILD;
1059a0753c29SDavid Howells 
1060a0753c29SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
1061a0753c29SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
1062a0753c29SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
1063a0753c29SDavid Howells 	 * version.
1064a0753c29SDavid Howells 	 */
1065a0753c29SDavid Howells 	dir_version = (long)READ_ONCE(dvnode->status.data_version);
1066a0753c29SDavid Howells 	de_version = (long)READ_ONCE(dentry->d_fsdata);
1067a0753c29SDavid Howells 	if (de_version != dir_version) {
1068a0753c29SDavid Howells 		dir_version = (long)READ_ONCE(dvnode->invalid_before);
1069a0753c29SDavid Howells 		if (de_version - dir_version < 0)
1070a0753c29SDavid Howells 			return -ECHILD;
1071a0753c29SDavid Howells 	}
1072a0753c29SDavid Howells 
1073a0753c29SDavid Howells 	return 1; /* Still valid */
1074a0753c29SDavid Howells }
1075a0753c29SDavid Howells 
1076a0753c29SDavid Howells /*
10771da177e4SLinus Torvalds  * check that a dentry lookup hit has found a valid entry
10781da177e4SLinus Torvalds  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
10791da177e4SLinus Torvalds  *   inode
10801da177e4SLinus Torvalds  */
10810b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
10821da177e4SLinus Torvalds {
1083260a9803SDavid Howells 	struct afs_vnode *vnode, *dir;
10843f649ab7SKees Cook 	struct afs_fid fid;
10851da177e4SLinus Torvalds 	struct dentry *parent;
1086c435ee34SDavid Howells 	struct inode *inode;
108700d3b7a4SDavid Howells 	struct key *key;
108840fc8102SDavid Howells 	afs_dataversion_t dir_version, invalid_before;
10899dd0b82eSDavid Howells 	long de_version;
10901da177e4SLinus Torvalds 	int ret;
10911da177e4SLinus Torvalds 
10920b728e19SAl Viro 	if (flags & LOOKUP_RCU)
1093a0753c29SDavid Howells 		return afs_d_revalidate_rcu(dentry);
109434286d66SNick Piggin 
1095c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
10962b0143b5SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
10973b6492dfSDavid Howells 		_enter("{v={%llx:%llu} n=%pd fl=%lx},",
1098a455589fSAl Viro 		       vnode->fid.vid, vnode->fid.vnode, dentry,
1099260a9803SDavid Howells 		       vnode->flags);
1100c435ee34SDavid Howells 	} else {
1101a455589fSAl Viro 		_enter("{neg n=%pd}", dentry);
1102c435ee34SDavid Howells 	}
11031da177e4SLinus Torvalds 
1104260a9803SDavid Howells 	key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
110500d3b7a4SDavid Howells 	if (IS_ERR(key))
110600d3b7a4SDavid Howells 		key = NULL;
110700d3b7a4SDavid Howells 
110863d49d84SDavid Howells 	/* Hold the parent dentry so we can peer at it */
110908e0e7c8SDavid Howells 	parent = dget_parent(dentry);
11102b0143b5SDavid Howells 	dir = AFS_FS_I(d_inode(parent));
11111da177e4SLinus Torvalds 
1112260a9803SDavid Howells 	/* validate the parent directory */
1113260a9803SDavid Howells 	afs_validate(dir, key);
1114260a9803SDavid Howells 
1115260a9803SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1116a455589fSAl Viro 		_debug("%pd: parent dir deleted", dentry);
111763d49d84SDavid Howells 		goto not_found;
11181da177e4SLinus Torvalds 	}
11191da177e4SLinus Torvalds 
1120a4ff7401SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
1121a4ff7401SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
1122a4ff7401SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
1123a4ff7401SDavid Howells 	 * version.
1124a4ff7401SDavid Howells 	 */
11259dd0b82eSDavid Howells 	dir_version = dir->status.data_version;
1126a4ff7401SDavid Howells 	de_version = (long)dentry->d_fsdata;
11279dd0b82eSDavid Howells 	if (de_version == (long)dir_version)
11285dc84855SDavid Howells 		goto out_valid_noupdate;
1129a4ff7401SDavid Howells 
113040fc8102SDavid Howells 	invalid_before = dir->invalid_before;
113140fc8102SDavid Howells 	if (de_version - (long)invalid_before >= 0)
1132a4ff7401SDavid Howells 		goto out_valid;
1133260a9803SDavid Howells 
113408e0e7c8SDavid Howells 	_debug("dir modified");
1135d55b4da4SDavid Howells 	afs_stat_v(dir, n_reval);
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	/* search the directory for this vnode */
11389dd0b82eSDavid Howells 	ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key, &dir_version);
1139260a9803SDavid Howells 	switch (ret) {
1140260a9803SDavid Howells 	case 0:
1141260a9803SDavid Howells 		/* the filename maps to something */
11422b0143b5SDavid Howells 		if (d_really_is_negative(dentry))
114363d49d84SDavid Howells 			goto not_found;
1144c435ee34SDavid Howells 		inode = d_inode(dentry);
1145c435ee34SDavid Howells 		if (is_bad_inode(inode)) {
1146a455589fSAl Viro 			printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1147a455589fSAl Viro 			       dentry);
114863d49d84SDavid Howells 			goto not_found;
11491da177e4SLinus Torvalds 		}
11501da177e4SLinus Torvalds 
1151c435ee34SDavid Howells 		vnode = AFS_FS_I(inode);
1152c435ee34SDavid Howells 
11531da177e4SLinus Torvalds 		/* if the vnode ID has changed, then the dirent points to a
11541da177e4SLinus Torvalds 		 * different file */
115508e0e7c8SDavid Howells 		if (fid.vnode != vnode->fid.vnode) {
11563b6492dfSDavid Howells 			_debug("%pd: dirent changed [%llu != %llu]",
1157a455589fSAl Viro 			       dentry, fid.vnode,
115808e0e7c8SDavid Howells 			       vnode->fid.vnode);
11591da177e4SLinus Torvalds 			goto not_found;
11601da177e4SLinus Torvalds 		}
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 		/* if the vnode ID uniqifier has changed, then the file has
1163260a9803SDavid Howells 		 * been deleted and replaced, and the original vnode ID has
1164260a9803SDavid Howells 		 * been reused */
116508e0e7c8SDavid Howells 		if (fid.unique != vnode->fid.unique) {
1166a455589fSAl Viro 			_debug("%pd: file deleted (uq %u -> %u I:%u)",
1167a455589fSAl Viro 			       dentry, fid.unique,
11687a224228SJean Noel Cordenner 			       vnode->fid.unique,
1169c435ee34SDavid Howells 			       vnode->vfs_inode.i_generation);
1170260a9803SDavid Howells 			goto not_found;
1171260a9803SDavid Howells 		}
1172260a9803SDavid Howells 		goto out_valid;
1173260a9803SDavid Howells 
1174260a9803SDavid Howells 	case -ENOENT:
1175260a9803SDavid Howells 		/* the filename is unknown */
1176a455589fSAl Viro 		_debug("%pd: dirent not found", dentry);
11772b0143b5SDavid Howells 		if (d_really_is_positive(dentry))
1178260a9803SDavid Howells 			goto not_found;
1179260a9803SDavid Howells 		goto out_valid;
1180260a9803SDavid Howells 
1181260a9803SDavid Howells 	default:
1182a455589fSAl Viro 		_debug("failed to iterate dir %pd: %d",
1183a455589fSAl Viro 		       parent, ret);
118463d49d84SDavid Howells 		goto not_found;
11851da177e4SLinus Torvalds 	}
118608e0e7c8SDavid Howells 
11871da177e4SLinus Torvalds out_valid:
11889dd0b82eSDavid Howells 	dentry->d_fsdata = (void *)(unsigned long)dir_version;
11895dc84855SDavid Howells out_valid_noupdate:
11901da177e4SLinus Torvalds 	dput(parent);
119100d3b7a4SDavid Howells 	key_put(key);
11921da177e4SLinus Torvalds 	_leave(" = 1 [valid]");
11931da177e4SLinus Torvalds 	return 1;
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds not_found:
1196a455589fSAl Viro 	_debug("dropping dentry %pd2", dentry);
11971da177e4SLinus Torvalds 	dput(parent);
119800d3b7a4SDavid Howells 	key_put(key);
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	_leave(" = 0 [bad]");
12011da177e4SLinus Torvalds 	return 0;
1202ec26815aSDavid Howells }
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds /*
12051da177e4SLinus Torvalds  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
12061da177e4SLinus Torvalds  * sleep)
12071da177e4SLinus Torvalds  * - called from dput() when d_count is going to 0.
12081da177e4SLinus Torvalds  * - return 1 to request dentry be unhashed, 0 otherwise
12091da177e4SLinus Torvalds  */
1210fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry)
12111da177e4SLinus Torvalds {
1212a455589fSAl Viro 	_enter("%pd", dentry);
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
12151da177e4SLinus Torvalds 		goto zap;
12161da177e4SLinus Torvalds 
12172b0143b5SDavid Howells 	if (d_really_is_positive(dentry) &&
12182b0143b5SDavid Howells 	    (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
12192b0143b5SDavid Howells 	     test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
12201da177e4SLinus Torvalds 		goto zap;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	_leave(" = 0 [keep]");
12231da177e4SLinus Torvalds 	return 0;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds zap:
12261da177e4SLinus Torvalds 	_leave(" = 1 [zap]");
12271da177e4SLinus Torvalds 	return 1;
1228ec26815aSDavid Howells }
1229260a9803SDavid Howells 
1230260a9803SDavid Howells /*
123179ddbfa5SDavid Howells  * Clean up sillyrename files on dentry removal.
123279ddbfa5SDavid Howells  */
123379ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode)
123479ddbfa5SDavid Howells {
123579ddbfa5SDavid Howells 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
123679ddbfa5SDavid Howells 		afs_silly_iput(dentry, inode);
123779ddbfa5SDavid Howells 	iput(inode);
123879ddbfa5SDavid Howells }
123979ddbfa5SDavid Howells 
124079ddbfa5SDavid Howells /*
1241260a9803SDavid Howells  * handle dentry release
1242260a9803SDavid Howells  */
124366c7e1d3SDavid Howells void afs_d_release(struct dentry *dentry)
1244260a9803SDavid Howells {
1245a455589fSAl Viro 	_enter("%pd", dentry);
1246260a9803SDavid Howells }
1247260a9803SDavid Howells 
1248728279a5SDavid Howells void afs_check_for_remote_deletion(struct afs_operation *op)
1249728279a5SDavid Howells {
1250728279a5SDavid Howells 	struct afs_vnode *vnode = op->file[0].vnode;
1251728279a5SDavid Howells 
1252728279a5SDavid Howells 	switch (op->ac.abort_code) {
1253728279a5SDavid Howells 	case VNOVNODE:
1254728279a5SDavid Howells 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1255728279a5SDavid Howells 		afs_break_callback(vnode, afs_cb_break_for_deleted);
1256728279a5SDavid Howells 	}
1257728279a5SDavid Howells }
1258728279a5SDavid Howells 
1259260a9803SDavid Howells /*
1260d2ddc776SDavid Howells  * Create a new inode for create/mkdir/symlink
1261d2ddc776SDavid Howells  */
1262e49c7b2fSDavid Howells static void afs_vnode_new_inode(struct afs_operation *op)
1263d2ddc776SDavid Howells {
1264e49c7b2fSDavid Howells 	struct afs_vnode_param *vp = &op->file[1];
12655a813276SDavid Howells 	struct afs_vnode *vnode;
1266d2ddc776SDavid Howells 	struct inode *inode;
1267d2ddc776SDavid Howells 
1268e49c7b2fSDavid Howells 	_enter("");
1269d2ddc776SDavid Howells 
1270e49c7b2fSDavid Howells 	ASSERTCMP(op->error, ==, 0);
1271e49c7b2fSDavid Howells 
1272e49c7b2fSDavid Howells 	inode = afs_iget(op, vp);
1273d2ddc776SDavid Howells 	if (IS_ERR(inode)) {
1274d2ddc776SDavid Howells 		/* ENOMEM or EINTR at a really inconvenient time - just abandon
1275d2ddc776SDavid Howells 		 * the new directory on the server.
1276d2ddc776SDavid Howells 		 */
1277e49c7b2fSDavid Howells 		op->error = PTR_ERR(inode);
1278d2ddc776SDavid Howells 		return;
1279d2ddc776SDavid Howells 	}
1280d2ddc776SDavid Howells 
12815a813276SDavid Howells 	vnode = AFS_FS_I(inode);
12825a813276SDavid Howells 	set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1283e49c7b2fSDavid Howells 	if (!op->error)
1284e49c7b2fSDavid Howells 		afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb);
1285e49c7b2fSDavid Howells 	d_instantiate(op->dentry, inode);
1286d2ddc776SDavid Howells }
1287d2ddc776SDavid Howells 
1288e49c7b2fSDavid Howells static void afs_create_success(struct afs_operation *op)
1289b8359153SDavid Howells {
1290e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1291da8d0755SDavid Howells 	op->ctime = op->file[0].scb.status.mtime_client;
1292e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
1293e49c7b2fSDavid Howells 	afs_update_dentry_version(op, &op->file[0], op->dentry);
1294e49c7b2fSDavid Howells 	afs_vnode_new_inode(op);
1295b8359153SDavid Howells }
1296b8359153SDavid Howells 
1297e49c7b2fSDavid Howells static void afs_create_edit_dir(struct afs_operation *op)
12989dd0b82eSDavid Howells {
1299e49c7b2fSDavid Howells 	struct afs_vnode_param *dvp = &op->file[0];
1300e49c7b2fSDavid Howells 	struct afs_vnode_param *vp = &op->file[1];
1301e49c7b2fSDavid Howells 	struct afs_vnode *dvnode = dvp->vnode;
1302e49c7b2fSDavid Howells 
1303e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1304e49c7b2fSDavid Howells 
1305e49c7b2fSDavid Howells 	down_write(&dvnode->validate_lock);
1306e49c7b2fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1307e49c7b2fSDavid Howells 	    dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1308e49c7b2fSDavid Howells 		afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid,
1309e49c7b2fSDavid Howells 				 op->create.reason);
1310e49c7b2fSDavid Howells 	up_write(&dvnode->validate_lock);
13119dd0b82eSDavid Howells }
13129dd0b82eSDavid Howells 
1313e49c7b2fSDavid Howells static void afs_create_put(struct afs_operation *op)
1314e49c7b2fSDavid Howells {
1315e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1316e49c7b2fSDavid Howells 
1317e49c7b2fSDavid Howells 	if (op->error)
1318e49c7b2fSDavid Howells 		d_drop(op->dentry);
1319e49c7b2fSDavid Howells }
1320e49c7b2fSDavid Howells 
1321e49c7b2fSDavid Howells static const struct afs_operation_ops afs_mkdir_operation = {
1322e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_make_dir,
1323e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_make_dir,
1324e49c7b2fSDavid Howells 	.success	= afs_create_success,
1325728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1326e49c7b2fSDavid Howells 	.edit_dir	= afs_create_edit_dir,
1327e49c7b2fSDavid Howells 	.put		= afs_create_put,
1328e49c7b2fSDavid Howells };
1329e49c7b2fSDavid Howells 
13309dd0b82eSDavid Howells /*
1331260a9803SDavid Howells  * create a directory on an AFS filesystem
1332260a9803SDavid Howells  */
1333549c7297SChristian Brauner static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
1334549c7297SChristian Brauner 		     struct dentry *dentry, umode_t mode)
1335260a9803SDavid Howells {
1336e49c7b2fSDavid Howells 	struct afs_operation *op;
1337d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1338260a9803SDavid Howells 
13393b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho",
1340a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1341260a9803SDavid Howells 
1342e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1343e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
1344260a9803SDavid Howells 		d_drop(dentry);
1345e49c7b2fSDavid Howells 		return PTR_ERR(op);
1346e49c7b2fSDavid Howells 	}
1347e49c7b2fSDavid Howells 
1348e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1349e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
135022650f14SDavid Howells 	op->file[0].modification = true;
1351da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1352e49c7b2fSDavid Howells 	op->dentry	= dentry;
1353e49c7b2fSDavid Howells 	op->create.mode	= S_IFDIR | mode;
1354e49c7b2fSDavid Howells 	op->create.reason = afs_edit_dir_for_mkdir;
1355e49c7b2fSDavid Howells 	op->ops		= &afs_mkdir_operation;
1356e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1357260a9803SDavid Howells }
1358260a9803SDavid Howells 
1359260a9803SDavid Howells /*
1360d2ddc776SDavid Howells  * Remove a subdir from a directory.
1361260a9803SDavid Howells  */
1362d2ddc776SDavid Howells static void afs_dir_remove_subdir(struct dentry *dentry)
1363260a9803SDavid Howells {
13642b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1365d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1366d2ddc776SDavid Howells 
1367260a9803SDavid Howells 		clear_nlink(&vnode->vfs_inode);
1368260a9803SDavid Howells 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1369c435ee34SDavid Howells 		clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
137063a4681fSDavid Howells 		clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1371260a9803SDavid Howells 	}
1372260a9803SDavid Howells }
1373260a9803SDavid Howells 
1374e49c7b2fSDavid Howells static void afs_rmdir_success(struct afs_operation *op)
1375e49c7b2fSDavid Howells {
1376e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1377da8d0755SDavid Howells 	op->ctime = op->file[0].scb.status.mtime_client;
1378e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
1379e49c7b2fSDavid Howells 	afs_update_dentry_version(op, &op->file[0], op->dentry);
1380e49c7b2fSDavid Howells }
1381e49c7b2fSDavid Howells 
1382e49c7b2fSDavid Howells static void afs_rmdir_edit_dir(struct afs_operation *op)
1383e49c7b2fSDavid Howells {
1384e49c7b2fSDavid Howells 	struct afs_vnode_param *dvp = &op->file[0];
1385e49c7b2fSDavid Howells 	struct afs_vnode *dvnode = dvp->vnode;
1386e49c7b2fSDavid Howells 
1387e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1388e49c7b2fSDavid Howells 	afs_dir_remove_subdir(op->dentry);
1389e49c7b2fSDavid Howells 
1390e49c7b2fSDavid Howells 	down_write(&dvnode->validate_lock);
1391e49c7b2fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1392e49c7b2fSDavid Howells 	    dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1393e49c7b2fSDavid Howells 		afs_edit_dir_remove(dvnode, &op->dentry->d_name,
1394e49c7b2fSDavid Howells 				    afs_edit_dir_for_rmdir);
1395e49c7b2fSDavid Howells 	up_write(&dvnode->validate_lock);
1396e49c7b2fSDavid Howells }
1397e49c7b2fSDavid Howells 
1398e49c7b2fSDavid Howells static void afs_rmdir_put(struct afs_operation *op)
1399e49c7b2fSDavid Howells {
1400e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1401e49c7b2fSDavid Howells 	if (op->file[1].vnode)
1402e49c7b2fSDavid Howells 		up_write(&op->file[1].vnode->rmdir_lock);
1403e49c7b2fSDavid Howells }
1404e49c7b2fSDavid Howells 
1405e49c7b2fSDavid Howells static const struct afs_operation_ops afs_rmdir_operation = {
1406e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_remove_dir,
1407e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_remove_dir,
1408e49c7b2fSDavid Howells 	.success	= afs_rmdir_success,
1409728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1410e49c7b2fSDavid Howells 	.edit_dir	= afs_rmdir_edit_dir,
1411e49c7b2fSDavid Howells 	.put		= afs_rmdir_put,
1412e49c7b2fSDavid Howells };
1413e49c7b2fSDavid Howells 
1414260a9803SDavid Howells /*
1415d2ddc776SDavid Howells  * remove a directory from an AFS filesystem
1416260a9803SDavid Howells  */
1417d2ddc776SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1418260a9803SDavid Howells {
1419e49c7b2fSDavid Howells 	struct afs_operation *op;
1420f58db83fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1421260a9803SDavid Howells 	int ret;
1422260a9803SDavid Howells 
14233b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1424a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1425260a9803SDavid Howells 
1426e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1427e49c7b2fSDavid Howells 	if (IS_ERR(op))
1428e49c7b2fSDavid Howells 		return PTR_ERR(op);
1429a58823acSDavid Howells 
1430e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1431e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
143222650f14SDavid Howells 	op->file[0].modification = true;
1433da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1434e49c7b2fSDavid Howells 
1435e49c7b2fSDavid Howells 	op->dentry	= dentry;
1436e49c7b2fSDavid Howells 	op->ops		= &afs_rmdir_operation;
1437260a9803SDavid Howells 
1438f58db83fSDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1439f58db83fSDavid Howells 	if (d_really_is_positive(dentry)) {
1440f58db83fSDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1441e49c7b2fSDavid Howells 		ret = afs_validate(vnode, op->key);
1442f58db83fSDavid Howells 		if (ret < 0)
1443e49c7b2fSDavid Howells 			goto error;
1444f58db83fSDavid Howells 	}
1445f58db83fSDavid Howells 
144679ddbfa5SDavid Howells 	if (vnode) {
144779ddbfa5SDavid Howells 		ret = down_write_killable(&vnode->rmdir_lock);
144879ddbfa5SDavid Howells 		if (ret < 0)
1449e49c7b2fSDavid Howells 			goto error;
1450e49c7b2fSDavid Howells 		op->file[1].vnode = vnode;
145179ddbfa5SDavid Howells 	}
145279ddbfa5SDavid Howells 
1453e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1454a58823acSDavid Howells 
1455d2ddc776SDavid Howells error:
1456e49c7b2fSDavid Howells 	return afs_put_operation(op);
1457d2ddc776SDavid Howells }
1458260a9803SDavid Howells 
1459d2ddc776SDavid Howells /*
1460d2ddc776SDavid Howells  * Remove a link to a file or symlink from a directory.
1461d2ddc776SDavid Howells  *
1462d2ddc776SDavid Howells  * If the file was not deleted due to excess hard links, the fileserver will
1463d2ddc776SDavid Howells  * break the callback promise on the file - if it had one - before it returns
1464d2ddc776SDavid Howells  * to us, and if it was deleted, it won't
1465d2ddc776SDavid Howells  *
1466d2ddc776SDavid Howells  * However, if we didn't have a callback promise outstanding, or it was
1467d2ddc776SDavid Howells  * outstanding on a different server, then it won't break it either...
1468d2ddc776SDavid Howells  */
1469e49c7b2fSDavid Howells static void afs_dir_remove_link(struct afs_operation *op)
1470d2ddc776SDavid Howells {
1471e49c7b2fSDavid Howells 	struct afs_vnode *dvnode = op->file[0].vnode;
1472e49c7b2fSDavid Howells 	struct afs_vnode *vnode = op->file[1].vnode;
1473e49c7b2fSDavid Howells 	struct dentry *dentry = op->dentry;
1474e49c7b2fSDavid Howells 	int ret;
1475d2ddc776SDavid Howells 
1476e49c7b2fSDavid Howells 	if (op->error != 0 ||
1477e49c7b2fSDavid Howells 	    (op->file[1].scb.have_status && op->file[1].scb.have_error))
1478e49c7b2fSDavid Howells 		return;
1479e49c7b2fSDavid Howells 	if (d_really_is_positive(dentry))
1480e49c7b2fSDavid Howells 		return;
1481d2ddc776SDavid Howells 
148230062bd1SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
148330062bd1SDavid Howells 		/* Already done */
1484a38a7558SDavid Howells 	} else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1485a38a7558SDavid Howells 		write_seqlock(&vnode->cb_lock);
1486440fbc3aSDavid Howells 		drop_nlink(&vnode->vfs_inode);
1487440fbc3aSDavid Howells 		if (vnode->vfs_inode.i_nlink == 0) {
1488440fbc3aSDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
1489051d2525SDavid Howells 			__afs_break_callback(vnode, afs_cb_break_for_unlink);
1490440fbc3aSDavid Howells 		}
1491a38a7558SDavid Howells 		write_sequnlock(&vnode->cb_lock);
1492440fbc3aSDavid Howells 	} else {
1493051d2525SDavid Howells 		afs_break_callback(vnode, afs_cb_break_for_unlink);
1494440fbc3aSDavid Howells 
1495d2ddc776SDavid Howells 		if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1496e49c7b2fSDavid Howells 			_debug("AFS_VNODE_DELETED");
1497d2ddc776SDavid Howells 
1498e49c7b2fSDavid Howells 		ret = afs_validate(vnode, op->key);
1499e49c7b2fSDavid Howells 		if (ret != -ESTALE)
1500e49c7b2fSDavid Howells 			op->error = ret;
1501d2ddc776SDavid Howells 	}
1502d2ddc776SDavid Howells 
1503e49c7b2fSDavid Howells 	_debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, op->error);
1504d2ddc776SDavid Howells }
1505d2ddc776SDavid Howells 
1506e49c7b2fSDavid Howells static void afs_unlink_success(struct afs_operation *op)
1507e49c7b2fSDavid Howells {
1508e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1509da8d0755SDavid Howells 	op->ctime = op->file[0].scb.status.mtime_client;
1510b6489a49SDavid Howells 	afs_check_dir_conflict(op, &op->file[0]);
1511e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
1512e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[1]);
1513e49c7b2fSDavid Howells 	afs_update_dentry_version(op, &op->file[0], op->dentry);
1514e49c7b2fSDavid Howells 	afs_dir_remove_link(op);
1515e49c7b2fSDavid Howells }
1516e49c7b2fSDavid Howells 
1517e49c7b2fSDavid Howells static void afs_unlink_edit_dir(struct afs_operation *op)
1518e49c7b2fSDavid Howells {
1519e49c7b2fSDavid Howells 	struct afs_vnode_param *dvp = &op->file[0];
1520e49c7b2fSDavid Howells 	struct afs_vnode *dvnode = dvp->vnode;
1521e49c7b2fSDavid Howells 
1522e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1523e49c7b2fSDavid Howells 	down_write(&dvnode->validate_lock);
1524e49c7b2fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1525e49c7b2fSDavid Howells 	    dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1526e49c7b2fSDavid Howells 		afs_edit_dir_remove(dvnode, &op->dentry->d_name,
1527e49c7b2fSDavid Howells 				    afs_edit_dir_for_unlink);
1528e49c7b2fSDavid Howells 	up_write(&dvnode->validate_lock);
1529e49c7b2fSDavid Howells }
1530e49c7b2fSDavid Howells 
1531e49c7b2fSDavid Howells static void afs_unlink_put(struct afs_operation *op)
1532e49c7b2fSDavid Howells {
1533e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1534e49c7b2fSDavid Howells 	if (op->unlink.need_rehash && op->error < 0 && op->error != -ENOENT)
1535e49c7b2fSDavid Howells 		d_rehash(op->dentry);
1536e49c7b2fSDavid Howells }
1537e49c7b2fSDavid Howells 
1538e49c7b2fSDavid Howells static const struct afs_operation_ops afs_unlink_operation = {
1539e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_remove_file,
1540e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_remove_file,
1541e49c7b2fSDavid Howells 	.success	= afs_unlink_success,
1542728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1543e49c7b2fSDavid Howells 	.edit_dir	= afs_unlink_edit_dir,
1544e49c7b2fSDavid Howells 	.put		= afs_unlink_put,
1545e49c7b2fSDavid Howells };
1546e49c7b2fSDavid Howells 
1547d2ddc776SDavid Howells /*
1548d2ddc776SDavid Howells  * Remove a file or symlink from an AFS filesystem.
1549d2ddc776SDavid Howells  */
1550d2ddc776SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry)
1551d2ddc776SDavid Howells {
1552e49c7b2fSDavid Howells 	struct afs_operation *op;
1553fa59f52fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1554fa59f52fSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1555d2ddc776SDavid Howells 	int ret;
1556d2ddc776SDavid Howells 
15573b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1558d2ddc776SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1559d2ddc776SDavid Howells 
1560d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1561d2ddc776SDavid Howells 		return -ENAMETOOLONG;
1562d2ddc776SDavid Howells 
1563e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1564e49c7b2fSDavid Howells 	if (IS_ERR(op))
1565e49c7b2fSDavid Howells 		return PTR_ERR(op);
1566a58823acSDavid Howells 
1567e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1568e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
156922650f14SDavid Howells 	op->file[0].modification = true;
1570da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1571d2ddc776SDavid Howells 
1572d2ddc776SDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1573e49c7b2fSDavid Howells 	ret = afs_validate(vnode, op->key);
1574e49c7b2fSDavid Howells 	if (ret < 0) {
1575e49c7b2fSDavid Howells 		op->error = ret;
1576e49c7b2fSDavid Howells 		goto error;
1577e49c7b2fSDavid Howells 	}
1578d2ddc776SDavid Howells 
157979ddbfa5SDavid Howells 	spin_lock(&dentry->d_lock);
1580fa59f52fSDavid Howells 	if (d_count(dentry) > 1) {
158179ddbfa5SDavid Howells 		spin_unlock(&dentry->d_lock);
158279ddbfa5SDavid Howells 		/* Start asynchronous writeout of the inode */
158379ddbfa5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
1584e49c7b2fSDavid Howells 		op->error = afs_sillyrename(dvnode, vnode, dentry, op->key);
1585e49c7b2fSDavid Howells 		goto error;
158679ddbfa5SDavid Howells 	}
158779ddbfa5SDavid Howells 	if (!d_unhashed(dentry)) {
158879ddbfa5SDavid Howells 		/* Prevent a race with RCU lookup. */
158979ddbfa5SDavid Howells 		__d_drop(dentry);
1590e49c7b2fSDavid Howells 		op->unlink.need_rehash = true;
159179ddbfa5SDavid Howells 	}
159279ddbfa5SDavid Howells 	spin_unlock(&dentry->d_lock);
159379ddbfa5SDavid Howells 
1594e49c7b2fSDavid Howells 	op->file[1].vnode = vnode;
1595da8d0755SDavid Howells 	op->file[1].update_ctime = true;
1596b6489a49SDavid Howells 	op->file[1].op_unlinked = true;
1597e49c7b2fSDavid Howells 	op->dentry	= dentry;
1598e49c7b2fSDavid Howells 	op->ops		= &afs_unlink_operation;
1599b6489a49SDavid Howells 	afs_begin_vnode_operation(op);
1600b6489a49SDavid Howells 	afs_wait_for_operation(op);
1601b6489a49SDavid Howells 
1602b6489a49SDavid Howells 	/* If there was a conflict with a third party, check the status of the
1603b6489a49SDavid Howells 	 * unlinked vnode.
1604b6489a49SDavid Howells 	 */
1605b6489a49SDavid Howells 	if (op->error == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) {
1606b6489a49SDavid Howells 		op->file[1].update_ctime = false;
1607b6489a49SDavid Howells 		op->fetch_status.which = 1;
1608b6489a49SDavid Howells 		op->ops = &afs_fetch_status_operation;
1609b6489a49SDavid Howells 		afs_begin_vnode_operation(op);
1610b6489a49SDavid Howells 		afs_wait_for_operation(op);
1611b6489a49SDavid Howells 	}
1612b6489a49SDavid Howells 
1613b6489a49SDavid Howells 	return afs_put_operation(op);
1614a58823acSDavid Howells 
1615260a9803SDavid Howells error:
1616e49c7b2fSDavid Howells 	return afs_put_operation(op);
1617260a9803SDavid Howells }
1618260a9803SDavid Howells 
1619e49c7b2fSDavid Howells static const struct afs_operation_ops afs_create_operation = {
1620e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_create_file,
1621e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_create_file,
1622e49c7b2fSDavid Howells 	.success	= afs_create_success,
1623728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1624e49c7b2fSDavid Howells 	.edit_dir	= afs_create_edit_dir,
1625e49c7b2fSDavid Howells 	.put		= afs_create_put,
1626e49c7b2fSDavid Howells };
1627e49c7b2fSDavid Howells 
1628260a9803SDavid Howells /*
1629260a9803SDavid Howells  * create a regular file on an AFS filesystem
1630260a9803SDavid Howells  */
1631549c7297SChristian Brauner static int afs_create(struct user_namespace *mnt_userns, struct inode *dir,
1632549c7297SChristian Brauner 		      struct dentry *dentry, umode_t mode, bool excl)
1633260a9803SDavid Howells {
1634e49c7b2fSDavid Howells 	struct afs_operation *op;
163543dd388bSColin Ian King 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1636e49c7b2fSDavid Howells 	int ret = -ENAMETOOLONG;
1637260a9803SDavid Howells 
1638e49c7b2fSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho",
1639a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1640260a9803SDavid Howells 
1641d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1642d2ddc776SDavid Howells 		goto error;
1643d2ddc776SDavid Howells 
1644e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1645e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
1646e49c7b2fSDavid Howells 		ret = PTR_ERR(op);
1647260a9803SDavid Howells 		goto error;
1648260a9803SDavid Howells 	}
1649260a9803SDavid Howells 
1650e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1651e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
165222650f14SDavid Howells 	op->file[0].modification = true;
1653da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1654a58823acSDavid Howells 
1655e49c7b2fSDavid Howells 	op->dentry	= dentry;
1656e49c7b2fSDavid Howells 	op->create.mode	= S_IFREG | mode;
1657e49c7b2fSDavid Howells 	op->create.reason = afs_edit_dir_for_create;
1658e49c7b2fSDavid Howells 	op->ops		= &afs_create_operation;
1659e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1660a58823acSDavid Howells 
1661260a9803SDavid Howells error:
1662260a9803SDavid Howells 	d_drop(dentry);
1663260a9803SDavid Howells 	_leave(" = %d", ret);
1664260a9803SDavid Howells 	return ret;
1665260a9803SDavid Howells }
1666260a9803SDavid Howells 
1667e49c7b2fSDavid Howells static void afs_link_success(struct afs_operation *op)
1668e49c7b2fSDavid Howells {
1669e49c7b2fSDavid Howells 	struct afs_vnode_param *dvp = &op->file[0];
1670e49c7b2fSDavid Howells 	struct afs_vnode_param *vp = &op->file[1];
1671e49c7b2fSDavid Howells 
1672e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1673da8d0755SDavid Howells 	op->ctime = dvp->scb.status.mtime_client;
1674e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, dvp);
1675e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, vp);
1676e49c7b2fSDavid Howells 	afs_update_dentry_version(op, dvp, op->dentry);
1677e49c7b2fSDavid Howells 	if (op->dentry_2->d_parent == op->dentry->d_parent)
1678e49c7b2fSDavid Howells 		afs_update_dentry_version(op, dvp, op->dentry_2);
1679e49c7b2fSDavid Howells 	ihold(&vp->vnode->vfs_inode);
1680e49c7b2fSDavid Howells 	d_instantiate(op->dentry, &vp->vnode->vfs_inode);
1681e49c7b2fSDavid Howells }
1682e49c7b2fSDavid Howells 
1683e49c7b2fSDavid Howells static void afs_link_put(struct afs_operation *op)
1684e49c7b2fSDavid Howells {
1685e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1686e49c7b2fSDavid Howells 	if (op->error)
1687e49c7b2fSDavid Howells 		d_drop(op->dentry);
1688e49c7b2fSDavid Howells }
1689e49c7b2fSDavid Howells 
1690e49c7b2fSDavid Howells static const struct afs_operation_ops afs_link_operation = {
1691e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_link,
1692e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_link,
1693e49c7b2fSDavid Howells 	.success	= afs_link_success,
1694728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1695e49c7b2fSDavid Howells 	.edit_dir	= afs_create_edit_dir,
1696e49c7b2fSDavid Howells 	.put		= afs_link_put,
1697e49c7b2fSDavid Howells };
1698e49c7b2fSDavid Howells 
1699260a9803SDavid Howells /*
1700260a9803SDavid Howells  * create a hard link between files in an AFS filesystem
1701260a9803SDavid Howells  */
1702260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
1703260a9803SDavid Howells 		    struct dentry *dentry)
1704260a9803SDavid Howells {
1705e49c7b2fSDavid Howells 	struct afs_operation *op;
1706a58823acSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1707a58823acSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1708e49c7b2fSDavid Howells 	int ret = -ENAMETOOLONG;
1709260a9803SDavid Howells 
17103b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%pd}",
1711260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1712260a9803SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode,
1713a455589fSAl Viro 	       dentry);
1714260a9803SDavid Howells 
1715d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1716d2ddc776SDavid Howells 		goto error;
1717d2ddc776SDavid Howells 
1718e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1719e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
1720e49c7b2fSDavid Howells 		ret = PTR_ERR(op);
1721a58823acSDavid Howells 		goto error;
1722260a9803SDavid Howells 	}
1723260a9803SDavid Howells 
17243978d816SDavid Howells 	ret = afs_validate(vnode, op->key);
17253978d816SDavid Howells 	if (ret < 0)
17263978d816SDavid Howells 		goto error_op;
17273978d816SDavid Howells 
1728e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1729e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 1, vnode);
1730e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
173122650f14SDavid Howells 	op->file[0].modification = true;
1732da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1733da8d0755SDavid Howells 	op->file[1].update_ctime = true;
1734a58823acSDavid Howells 
1735e49c7b2fSDavid Howells 	op->dentry		= dentry;
1736e49c7b2fSDavid Howells 	op->dentry_2		= from;
1737e49c7b2fSDavid Howells 	op->ops			= &afs_link_operation;
1738e49c7b2fSDavid Howells 	op->create.reason	= afs_edit_dir_for_link;
1739e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1740260a9803SDavid Howells 
17413978d816SDavid Howells error_op:
17423978d816SDavid Howells 	afs_put_operation(op);
1743260a9803SDavid Howells error:
1744260a9803SDavid Howells 	d_drop(dentry);
1745260a9803SDavid Howells 	_leave(" = %d", ret);
1746260a9803SDavid Howells 	return ret;
1747260a9803SDavid Howells }
1748260a9803SDavid Howells 
1749e49c7b2fSDavid Howells static const struct afs_operation_ops afs_symlink_operation = {
1750e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_symlink,
1751e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_symlink,
1752e49c7b2fSDavid Howells 	.success	= afs_create_success,
1753728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1754e49c7b2fSDavid Howells 	.edit_dir	= afs_create_edit_dir,
1755e49c7b2fSDavid Howells 	.put		= afs_create_put,
1756e49c7b2fSDavid Howells };
1757e49c7b2fSDavid Howells 
1758260a9803SDavid Howells /*
1759260a9803SDavid Howells  * create a symlink in an AFS filesystem
1760260a9803SDavid Howells  */
1761549c7297SChristian Brauner static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
1762549c7297SChristian Brauner 		       struct dentry *dentry, const char *content)
1763260a9803SDavid Howells {
1764e49c7b2fSDavid Howells 	struct afs_operation *op;
1765d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1766260a9803SDavid Howells 	int ret;
1767260a9803SDavid Howells 
17683b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%s",
1769a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry,
1770260a9803SDavid Howells 	       content);
1771260a9803SDavid Howells 
1772d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1773d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1774d2ddc776SDavid Howells 		goto error;
1775d2ddc776SDavid Howells 
1776260a9803SDavid Howells 	ret = -EINVAL;
177745222b9eSDavid Howells 	if (strlen(content) >= AFSPATHMAX)
1778260a9803SDavid Howells 		goto error;
1779260a9803SDavid Howells 
1780e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1781e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
1782e49c7b2fSDavid Howells 		ret = PTR_ERR(op);
1783a58823acSDavid Howells 		goto error;
1784260a9803SDavid Howells 	}
1785260a9803SDavid Howells 
1786e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1787e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
1788a58823acSDavid Howells 
1789e49c7b2fSDavid Howells 	op->dentry		= dentry;
1790e49c7b2fSDavid Howells 	op->ops			= &afs_symlink_operation;
1791e49c7b2fSDavid Howells 	op->create.reason	= afs_edit_dir_for_symlink;
1792e49c7b2fSDavid Howells 	op->create.symlink	= content;
1793e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1794d2ddc776SDavid Howells 
1795260a9803SDavid Howells error:
1796260a9803SDavid Howells 	d_drop(dentry);
1797260a9803SDavid Howells 	_leave(" = %d", ret);
1798260a9803SDavid Howells 	return ret;
1799260a9803SDavid Howells }
1800260a9803SDavid Howells 
1801e49c7b2fSDavid Howells static void afs_rename_success(struct afs_operation *op)
1802260a9803SDavid Howells {
1803e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1804e49c7b2fSDavid Howells 
1805da8d0755SDavid Howells 	op->ctime = op->file[0].scb.status.mtime_client;
1806b6489a49SDavid Howells 	afs_check_dir_conflict(op, &op->file[1]);
1807e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
1808da8d0755SDavid Howells 	if (op->file[1].vnode != op->file[0].vnode) {
1809da8d0755SDavid Howells 		op->ctime = op->file[1].scb.status.mtime_client;
1810e49c7b2fSDavid Howells 		afs_vnode_commit_status(op, &op->file[1]);
1811e49c7b2fSDavid Howells 	}
1812da8d0755SDavid Howells }
1813e49c7b2fSDavid Howells 
1814e49c7b2fSDavid Howells static void afs_rename_edit_dir(struct afs_operation *op)
1815e49c7b2fSDavid Howells {
1816e49c7b2fSDavid Howells 	struct afs_vnode_param *orig_dvp = &op->file[0];
1817e49c7b2fSDavid Howells 	struct afs_vnode_param *new_dvp = &op->file[1];
1818e49c7b2fSDavid Howells 	struct afs_vnode *orig_dvnode = orig_dvp->vnode;
1819e49c7b2fSDavid Howells 	struct afs_vnode *new_dvnode = new_dvp->vnode;
1820e49c7b2fSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry));
1821e49c7b2fSDavid Howells 	struct dentry *old_dentry = op->dentry;
1822e49c7b2fSDavid Howells 	struct dentry *new_dentry = op->dentry_2;
182379ddbfa5SDavid Howells 	struct inode *new_inode;
1824260a9803SDavid Howells 
1825e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
18261cd66c93SMiklos Szeredi 
1827e49c7b2fSDavid Howells 	if (op->rename.rehash) {
1828e49c7b2fSDavid Howells 		d_rehash(op->rename.rehash);
1829e49c7b2fSDavid Howells 		op->rename.rehash = NULL;
1830260a9803SDavid Howells 	}
1831260a9803SDavid Howells 
18322105c282SDavid Howells 	down_write(&orig_dvnode->validate_lock);
18332105c282SDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) &&
1834e49c7b2fSDavid Howells 	    orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta)
183563a4681fSDavid Howells 		afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
183679ddbfa5SDavid Howells 				    afs_edit_dir_for_rename_0);
183763a4681fSDavid Howells 
1838e49c7b2fSDavid Howells 	if (new_dvnode != orig_dvnode) {
1839e49c7b2fSDavid Howells 		up_write(&orig_dvnode->validate_lock);
18402105c282SDavid Howells 		down_write(&new_dvnode->validate_lock);
18412105c282SDavid Howells 	}
1842e49c7b2fSDavid Howells 
18432105c282SDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) &&
1844e49c7b2fSDavid Howells 	    new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) {
1845e49c7b2fSDavid Howells 		if (!op->rename.new_negative)
184663a4681fSDavid Howells 			afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
184779ddbfa5SDavid Howells 					    afs_edit_dir_for_rename_1);
184863a4681fSDavid Howells 
184963a4681fSDavid Howells 		afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
185079ddbfa5SDavid Howells 				 &vnode->fid, afs_edit_dir_for_rename_2);
18512105c282SDavid Howells 	}
185279ddbfa5SDavid Howells 
185379ddbfa5SDavid Howells 	new_inode = d_inode(new_dentry);
185479ddbfa5SDavid Howells 	if (new_inode) {
185579ddbfa5SDavid Howells 		spin_lock(&new_inode->i_lock);
1856f610a5a2SDavid Howells 		if (S_ISDIR(new_inode->i_mode))
1857f610a5a2SDavid Howells 			clear_nlink(new_inode);
1858f610a5a2SDavid Howells 		else if (new_inode->i_nlink > 0)
185979ddbfa5SDavid Howells 			drop_nlink(new_inode);
186079ddbfa5SDavid Howells 		spin_unlock(&new_inode->i_lock);
186179ddbfa5SDavid Howells 	}
18629dd0b82eSDavid Howells 
18639dd0b82eSDavid Howells 	/* Now we can update d_fsdata on the dentries to reflect their
18649dd0b82eSDavid Howells 	 * new parent's data_version.
18659dd0b82eSDavid Howells 	 *
18669dd0b82eSDavid Howells 	 * Note that if we ever implement RENAME_EXCHANGE, we'll have
18679dd0b82eSDavid Howells 	 * to update both dentries with opposing dir versions.
18689dd0b82eSDavid Howells 	 */
1869e49c7b2fSDavid Howells 	afs_update_dentry_version(op, new_dvp, op->dentry);
1870e49c7b2fSDavid Howells 	afs_update_dentry_version(op, new_dvp, op->dentry_2);
1871e49c7b2fSDavid Howells 
187279ddbfa5SDavid Howells 	d_move(old_dentry, new_dentry);
1873e49c7b2fSDavid Howells 
18742105c282SDavid Howells 	up_write(&new_dvnode->validate_lock);
187563a4681fSDavid Howells }
187663a4681fSDavid Howells 
1877e49c7b2fSDavid Howells static void afs_rename_put(struct afs_operation *op)
1878e49c7b2fSDavid Howells {
1879e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1880e49c7b2fSDavid Howells 	if (op->rename.rehash)
1881e49c7b2fSDavid Howells 		d_rehash(op->rename.rehash);
1882e49c7b2fSDavid Howells 	dput(op->rename.tmp);
1883e49c7b2fSDavid Howells 	if (op->error)
1884e49c7b2fSDavid Howells 		d_rehash(op->dentry);
1885e49c7b2fSDavid Howells }
1886e49c7b2fSDavid Howells 
1887e49c7b2fSDavid Howells static const struct afs_operation_ops afs_rename_operation = {
1888e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_rename,
1889e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_rename,
1890e49c7b2fSDavid Howells 	.success	= afs_rename_success,
1891e49c7b2fSDavid Howells 	.edit_dir	= afs_rename_edit_dir,
1892e49c7b2fSDavid Howells 	.put		= afs_rename_put,
1893e49c7b2fSDavid Howells };
1894e49c7b2fSDavid Howells 
1895e49c7b2fSDavid Howells /*
1896e49c7b2fSDavid Howells  * rename a file in an AFS filesystem and/or move it between directories
1897e49c7b2fSDavid Howells  */
1898549c7297SChristian Brauner static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
1899549c7297SChristian Brauner 		      struct dentry *old_dentry, struct inode *new_dir,
1900549c7297SChristian Brauner 		      struct dentry *new_dentry, unsigned int flags)
1901e49c7b2fSDavid Howells {
1902e49c7b2fSDavid Howells 	struct afs_operation *op;
1903e49c7b2fSDavid Howells 	struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1904e49c7b2fSDavid Howells 	int ret;
1905e49c7b2fSDavid Howells 
1906e49c7b2fSDavid Howells 	if (flags)
1907e49c7b2fSDavid Howells 		return -EINVAL;
1908e49c7b2fSDavid Howells 
1909e49c7b2fSDavid Howells 	/* Don't allow silly-rename files be moved around. */
1910e49c7b2fSDavid Howells 	if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
1911e49c7b2fSDavid Howells 		return -EINVAL;
1912e49c7b2fSDavid Howells 
1913e49c7b2fSDavid Howells 	vnode = AFS_FS_I(d_inode(old_dentry));
1914e49c7b2fSDavid Howells 	orig_dvnode = AFS_FS_I(old_dir);
1915e49c7b2fSDavid Howells 	new_dvnode = AFS_FS_I(new_dir);
1916e49c7b2fSDavid Howells 
1917e49c7b2fSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1918e49c7b2fSDavid Howells 	       orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1919e49c7b2fSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1920e49c7b2fSDavid Howells 	       new_dvnode->fid.vid, new_dvnode->fid.vnode,
1921e49c7b2fSDavid Howells 	       new_dentry);
1922e49c7b2fSDavid Howells 
1923e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, orig_dvnode->volume);
1924e49c7b2fSDavid Howells 	if (IS_ERR(op))
1925e49c7b2fSDavid Howells 		return PTR_ERR(op);
1926e49c7b2fSDavid Howells 
19273978d816SDavid Howells 	ret = afs_validate(vnode, op->key);
19283978d816SDavid Howells 	op->error = ret;
19293978d816SDavid Howells 	if (ret < 0)
19303978d816SDavid Howells 		goto error;
19313978d816SDavid Howells 
1932e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, orig_dvnode);
1933e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */
1934e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
1935e49c7b2fSDavid Howells 	op->file[1].dv_delta = 1;
193622650f14SDavid Howells 	op->file[0].modification = true;
193722650f14SDavid Howells 	op->file[1].modification = true;
1938da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1939da8d0755SDavid Howells 	op->file[1].update_ctime = true;
1940e49c7b2fSDavid Howells 
1941e49c7b2fSDavid Howells 	op->dentry		= old_dentry;
1942e49c7b2fSDavid Howells 	op->dentry_2		= new_dentry;
1943e49c7b2fSDavid Howells 	op->rename.new_negative	= d_is_negative(new_dentry);
1944e49c7b2fSDavid Howells 	op->ops			= &afs_rename_operation;
1945e49c7b2fSDavid Howells 
1946e49c7b2fSDavid Howells 	/* For non-directories, check whether the target is busy and if so,
1947e49c7b2fSDavid Howells 	 * make a copy of the dentry and then do a silly-rename.  If the
1948e49c7b2fSDavid Howells 	 * silly-rename succeeds, the copied dentry is hashed and becomes the
1949e49c7b2fSDavid Howells 	 * new target.
1950e49c7b2fSDavid Howells 	 */
1951e49c7b2fSDavid Howells 	if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
1952e49c7b2fSDavid Howells 		/* To prevent any new references to the target during the
1953e49c7b2fSDavid Howells 		 * rename, we unhash the dentry in advance.
1954e49c7b2fSDavid Howells 		 */
1955e49c7b2fSDavid Howells 		if (!d_unhashed(new_dentry)) {
1956e49c7b2fSDavid Howells 			d_drop(new_dentry);
1957e49c7b2fSDavid Howells 			op->rename.rehash = new_dentry;
1958e49c7b2fSDavid Howells 		}
1959e49c7b2fSDavid Howells 
1960e49c7b2fSDavid Howells 		if (d_count(new_dentry) > 2) {
1961e49c7b2fSDavid Howells 			/* copy the target dentry's name */
1962e49c7b2fSDavid Howells 			op->rename.tmp = d_alloc(new_dentry->d_parent,
1963e49c7b2fSDavid Howells 						 &new_dentry->d_name);
1964b4280812SJiapeng Chong 			if (!op->rename.tmp) {
1965b4280812SJiapeng Chong 				op->error = -ENOMEM;
1966e49c7b2fSDavid Howells 				goto error;
1967b4280812SJiapeng Chong 			}
1968e49c7b2fSDavid Howells 
1969e49c7b2fSDavid Howells 			ret = afs_sillyrename(new_dvnode,
1970e49c7b2fSDavid Howells 					      AFS_FS_I(d_inode(new_dentry)),
1971e49c7b2fSDavid Howells 					      new_dentry, op->key);
1972b4280812SJiapeng Chong 			if (ret) {
1973b4280812SJiapeng Chong 				op->error = ret;
1974e49c7b2fSDavid Howells 				goto error;
1975b4280812SJiapeng Chong 			}
1976e49c7b2fSDavid Howells 
1977e49c7b2fSDavid Howells 			op->dentry_2 = op->rename.tmp;
1978e49c7b2fSDavid Howells 			op->rename.rehash = NULL;
1979e49c7b2fSDavid Howells 			op->rename.new_negative = true;
1980e49c7b2fSDavid Howells 		}
1981e49c7b2fSDavid Howells 	}
1982e49c7b2fSDavid Howells 
1983e49c7b2fSDavid Howells 	/* This bit is potentially nasty as there's a potential race with
1984e49c7b2fSDavid Howells 	 * afs_d_revalidate{,_rcu}().  We have to change d_fsdata on the dentry
1985e49c7b2fSDavid Howells 	 * to reflect it's new parent's new data_version after the op, but
1986e49c7b2fSDavid Howells 	 * d_revalidate may see old_dentry between the op having taken place
1987e49c7b2fSDavid Howells 	 * and the version being updated.
1988e49c7b2fSDavid Howells 	 *
1989e49c7b2fSDavid Howells 	 * So drop the old_dentry for now to make other threads go through
1990e49c7b2fSDavid Howells 	 * lookup instead - which we hold a lock against.
1991e49c7b2fSDavid Howells 	 */
1992e49c7b2fSDavid Howells 	d_drop(old_dentry);
1993e49c7b2fSDavid Howells 
1994e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1995e49c7b2fSDavid Howells 
1996260a9803SDavid Howells error:
1997e49c7b2fSDavid Howells 	return afs_put_operation(op);
1998260a9803SDavid Howells }
1999f3ddee8dSDavid Howells 
2000f3ddee8dSDavid Howells /*
2001*255ed636SDavid Howells  * Release a directory folio and clean up its private state if it's not busy
2002*255ed636SDavid Howells  * - return true if the folio can now be released, false if not
2003f3ddee8dSDavid Howells  */
2004*255ed636SDavid Howells static int afs_dir_releasepage(struct page *subpage, gfp_t gfp_flags)
2005f3ddee8dSDavid Howells {
2006*255ed636SDavid Howells 	struct folio *folio = page_folio(subpage);
2007*255ed636SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(folio_inode(folio));
2008f3ddee8dSDavid Howells 
2009*255ed636SDavid Howells 	_enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, folio_index(folio));
2010f3ddee8dSDavid Howells 
2011*255ed636SDavid Howells 	folio_detach_private(folio);
2012f3ddee8dSDavid Howells 
2013f3ddee8dSDavid Howells 	/* The directory will need reloading. */
2014f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
2015f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_relpg);
2016*255ed636SDavid Howells 	return true;
2017f3ddee8dSDavid Howells }
2018f3ddee8dSDavid Howells 
2019f3ddee8dSDavid Howells /*
2020*255ed636SDavid Howells  * Invalidate part or all of a folio.
2021f3ddee8dSDavid Howells  */
2022*255ed636SDavid Howells static void afs_dir_invalidatepage(struct page *subpage, unsigned int offset,
2023f3ddee8dSDavid Howells 				   unsigned int length)
2024f3ddee8dSDavid Howells {
2025*255ed636SDavid Howells 	struct folio *folio = page_folio(subpage);
2026*255ed636SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(folio_inode(folio));
2027f3ddee8dSDavid Howells 
2028*255ed636SDavid Howells 	_enter("{%lu},%u,%u", folio_index(folio), offset, length);
2029f3ddee8dSDavid Howells 
2030*255ed636SDavid Howells 	BUG_ON(!folio_test_locked(folio));
2031f3ddee8dSDavid Howells 
2032f3ddee8dSDavid Howells 	/* The directory will need reloading. */
2033f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
2034f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_inval);
2035f3ddee8dSDavid Howells 
2036*255ed636SDavid Howells 	/* we clean up only if the entire folio is being invalidated */
2037*255ed636SDavid Howells 	if (offset == 0 && length == folio_size(folio))
2038*255ed636SDavid Howells 		folio_detach_private(folio);
2039f3ddee8dSDavid Howells }
2040