xref: /openbmc/linux/fs/afs/dir.c (revision f52b83b0)
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);
314acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
32ebfc3b49SAl Viro 		      bool excl);
3318bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
34260a9803SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry);
35260a9803SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry);
36260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
37260a9803SDavid Howells 		    struct dentry *dentry);
38260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
39260a9803SDavid Howells 		       const char *content);
40260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
411cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
421cd66c93SMiklos Szeredi 		      unsigned int flags);
43f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
44f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
45f3ddee8dSDavid Howells 				   unsigned int length);
46f3ddee8dSDavid Howells 
47f3ddee8dSDavid Howells static int afs_dir_set_page_dirty(struct page *page)
48f3ddee8dSDavid Howells {
49f3ddee8dSDavid Howells 	BUG(); /* This should never happen. */
50f3ddee8dSDavid Howells }
511da177e4SLinus Torvalds 
524b6f5d20SArjan van de Ven const struct file_operations afs_dir_file_operations = {
531da177e4SLinus Torvalds 	.open		= afs_dir_open,
5400d3b7a4SDavid Howells 	.release	= afs_release,
5529884effSAl Viro 	.iterate_shared	= afs_readdir,
56e8d6c554SDavid Howells 	.lock		= afs_lock,
573222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
60754661f1SArjan van de Ven const struct inode_operations afs_dir_inode_operations = {
61260a9803SDavid Howells 	.create		= afs_create,
62260a9803SDavid Howells 	.lookup		= afs_lookup,
63260a9803SDavid Howells 	.link		= afs_link,
64260a9803SDavid Howells 	.unlink		= afs_unlink,
65260a9803SDavid Howells 	.symlink	= afs_symlink,
66260a9803SDavid Howells 	.mkdir		= afs_mkdir,
67260a9803SDavid Howells 	.rmdir		= afs_rmdir,
682773bf00SMiklos Szeredi 	.rename		= afs_rename,
6900d3b7a4SDavid Howells 	.permission	= afs_permission,
70416351f2SDavid Howells 	.getattr	= afs_getattr,
7131143d5dSDavid Howells 	.setattr	= afs_setattr,
72d3e3b7eaSDavid Howells 	.listxattr	= afs_listxattr,
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;
10239db9815SDavid Howells 	struct inode		**inodes;
10387182759SDavid Howells 	struct afs_status_cb	*statuses;
1045cf9dd55SDavid Howells 	struct afs_fid		fids[50];
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * check that a directory page is valid
1091da177e4SLinus Torvalds  */
110f3ddee8dSDavid Howells static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
111f3ddee8dSDavid Howells 			       loff_t i_size)
1121da177e4SLinus Torvalds {
11300317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
114f3ddee8dSDavid Howells 	loff_t latter, off;
1151da177e4SLinus Torvalds 	int tmp, qty;
1161da177e4SLinus Torvalds 
117dab17c1aSDavid Howells 	/* Determine how many magic numbers there should be in this page, but
118dab17c1aSDavid Howells 	 * we must take care because the directory may change size under us.
119dab17c1aSDavid Howells 	 */
120dab17c1aSDavid Howells 	off = page_offset(page);
121dab17c1aSDavid Howells 	if (i_size <= off)
122dab17c1aSDavid Howells 		goto checked;
123dab17c1aSDavid Howells 
124dab17c1aSDavid Howells 	latter = i_size - off;
1251da177e4SLinus Torvalds 	if (latter >= PAGE_SIZE)
1261da177e4SLinus Torvalds 		qty = PAGE_SIZE;
1271da177e4SLinus Torvalds 	else
1281da177e4SLinus Torvalds 		qty = latter;
12900317636SDavid Howells 	qty /= sizeof(union afs_xdr_dir_block);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	/* check them */
13263a4681fSDavid Howells 	dbuf = kmap(page);
1331da177e4SLinus Torvalds 	for (tmp = 0; tmp < qty; tmp++) {
13400317636SDavid Howells 		if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
135dab17c1aSDavid Howells 			printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
136f3ddee8dSDavid Howells 			       __func__, dvnode->vfs_inode.i_ino, tmp, qty,
13700317636SDavid Howells 			       ntohs(dbuf->blocks[tmp].hdr.magic));
138f3ddee8dSDavid Howells 			trace_afs_dir_check_failed(dvnode, off, i_size);
13963a4681fSDavid Howells 			kunmap(page);
140f51375cdSDavid Howells 			trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
1411da177e4SLinus Torvalds 			goto error;
1421da177e4SLinus Torvalds 		}
14363a4681fSDavid Howells 
14463a4681fSDavid Howells 		/* Make sure each block is NUL terminated so we can reasonably
14563a4681fSDavid Howells 		 * use string functions on it.  The filenames in the page
14663a4681fSDavid Howells 		 * *should* be NUL-terminated anyway.
14763a4681fSDavid Howells 		 */
14863a4681fSDavid Howells 		((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
1491da177e4SLinus Torvalds 	}
1501da177e4SLinus Torvalds 
15163a4681fSDavid Howells 	kunmap(page);
15263a4681fSDavid Howells 
153dab17c1aSDavid Howells checked:
154f3ddee8dSDavid Howells 	afs_stat_v(dvnode, n_read_dir);
155be5b82dbSAl Viro 	return true;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds error:
158be5b82dbSAl Viro 	return false;
159ec26815aSDavid Howells }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /*
162445b1028SDavid Howells  * Check the contents of a directory that we've just read.
163445b1028SDavid Howells  */
164445b1028SDavid Howells static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
165445b1028SDavid Howells {
166445b1028SDavid Howells 	struct afs_xdr_dir_page *dbuf;
167445b1028SDavid Howells 	unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
168445b1028SDavid Howells 
169445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++)
170445b1028SDavid Howells 		if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
171445b1028SDavid Howells 			goto bad;
172445b1028SDavid Howells 	return true;
173445b1028SDavid Howells 
174445b1028SDavid Howells bad:
175445b1028SDavid Howells 	pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
176445b1028SDavid Howells 		dvnode->fid.vid, dvnode->fid.vnode,
177445b1028SDavid Howells 		req->file_size, req->len, req->actual_len, req->remain);
178445b1028SDavid Howells 	pr_warn("DIR %llx %x %x %x\n",
179445b1028SDavid Howells 		req->pos, req->index, req->nr_pages, req->offset);
180445b1028SDavid Howells 
181445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++) {
182445b1028SDavid Howells 		dbuf = kmap(req->pages[i]);
183445b1028SDavid Howells 		for (j = 0; j < qty; j++) {
184445b1028SDavid Howells 			union afs_xdr_dir_block *block = &dbuf->blocks[j];
185445b1028SDavid Howells 
186445b1028SDavid Howells 			pr_warn("[%02x] %32phN\n", i * qty + j, block);
187445b1028SDavid Howells 		}
188445b1028SDavid Howells 		kunmap(req->pages[i]);
189445b1028SDavid Howells 	}
190445b1028SDavid Howells 	return false;
191445b1028SDavid Howells }
192445b1028SDavid Howells 
193445b1028SDavid Howells /*
1941da177e4SLinus Torvalds  * open an AFS directory file
1951da177e4SLinus Torvalds  */
1961da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	_enter("{%lu}", inode->i_ino);
1991da177e4SLinus Torvalds 
20000317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
20100317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
2021da177e4SLinus Torvalds 
20308e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
2041da177e4SLinus Torvalds 		return -ENOENT;
2051da177e4SLinus Torvalds 
20600d3b7a4SDavid Howells 	return afs_open(inode, file);
207ec26815aSDavid Howells }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds /*
210f3ddee8dSDavid Howells  * Read the directory into the pagecache in one go, scrubbing the previous
211f3ddee8dSDavid Howells  * contents.  The list of pages is returned, pinning them so that they don't
212f3ddee8dSDavid Howells  * get reclaimed during the iteration.
213f3ddee8dSDavid Howells  */
214f3ddee8dSDavid Howells static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
215b61f7dcfSDavid Howells 	__acquires(&dvnode->validate_lock)
216f3ddee8dSDavid Howells {
217f3ddee8dSDavid Howells 	struct afs_read *req;
218f3ddee8dSDavid Howells 	loff_t i_size;
219f3ddee8dSDavid Howells 	int nr_pages, nr_inline, i, n;
220f3ddee8dSDavid Howells 	int ret = -ENOMEM;
221f3ddee8dSDavid Howells 
222f3ddee8dSDavid Howells retry:
223f3ddee8dSDavid Howells 	i_size = i_size_read(&dvnode->vfs_inode);
224f3ddee8dSDavid Howells 	if (i_size < 2048)
225f51375cdSDavid Howells 		return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
226f51375cdSDavid Howells 	if (i_size > 2048 * 1024) {
227f51375cdSDavid Howells 		trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
228f3ddee8dSDavid Howells 		return ERR_PTR(-EFBIG);
229f51375cdSDavid Howells 	}
230f3ddee8dSDavid Howells 
231f3ddee8dSDavid Howells 	_enter("%llu", i_size);
232f3ddee8dSDavid Howells 
233f3ddee8dSDavid Howells 	/* Get a request record to hold the page list.  We want to hold it
234f3ddee8dSDavid Howells 	 * inline if we can, but we don't want to make an order 1 allocation.
235f3ddee8dSDavid Howells 	 */
236f3ddee8dSDavid Howells 	nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
237f3ddee8dSDavid Howells 	nr_inline = nr_pages;
238f3ddee8dSDavid Howells 	if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
239f3ddee8dSDavid Howells 		nr_inline = 0;
240f3ddee8dSDavid Howells 
241ee102584SZhengyuan Liu 	req = kzalloc(struct_size(req, array, nr_inline), GFP_KERNEL);
242f3ddee8dSDavid Howells 	if (!req)
243f3ddee8dSDavid Howells 		return ERR_PTR(-ENOMEM);
244f3ddee8dSDavid Howells 
245f3ddee8dSDavid Howells 	refcount_set(&req->usage, 1);
246f3ddee8dSDavid Howells 	req->nr_pages = nr_pages;
247f3ddee8dSDavid Howells 	req->actual_len = i_size; /* May change */
248f3ddee8dSDavid Howells 	req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
249f3ddee8dSDavid Howells 	req->data_version = dvnode->status.data_version; /* May change */
250f3ddee8dSDavid Howells 	if (nr_inline > 0) {
251f3ddee8dSDavid Howells 		req->pages = req->array;
252f3ddee8dSDavid Howells 	} else {
253f3ddee8dSDavid Howells 		req->pages = kcalloc(nr_pages, sizeof(struct page *),
254f3ddee8dSDavid Howells 				     GFP_KERNEL);
255f3ddee8dSDavid Howells 		if (!req->pages)
256f3ddee8dSDavid Howells 			goto error;
257f3ddee8dSDavid Howells 	}
258f3ddee8dSDavid Howells 
259f3ddee8dSDavid Howells 	/* Get a list of all the pages that hold or will hold the directory
260f3ddee8dSDavid Howells 	 * content.  We need to fill in any gaps that we might find where the
261f3ddee8dSDavid Howells 	 * memory reclaimer has been at work.  If there are any gaps, we will
262f3ddee8dSDavid Howells 	 * need to reread the entire directory contents.
263f3ddee8dSDavid Howells 	 */
264f3ddee8dSDavid Howells 	i = 0;
265f3ddee8dSDavid Howells 	do {
266f3ddee8dSDavid Howells 		n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
267f3ddee8dSDavid Howells 					  req->nr_pages - i,
268f3ddee8dSDavid Howells 					  req->pages + i);
269f3ddee8dSDavid Howells 		_debug("find %u at %u/%u", n, i, req->nr_pages);
270f3ddee8dSDavid Howells 		if (n == 0) {
271f3ddee8dSDavid Howells 			gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
272f3ddee8dSDavid Howells 
273f3ddee8dSDavid Howells 			if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
274f3ddee8dSDavid Howells 				afs_stat_v(dvnode, n_inval);
275f3ddee8dSDavid Howells 
276f3ddee8dSDavid Howells 			ret = -ENOMEM;
277f3ddee8dSDavid Howells 			req->pages[i] = __page_cache_alloc(gfp);
278f3ddee8dSDavid Howells 			if (!req->pages[i])
279f3ddee8dSDavid Howells 				goto error;
280f3ddee8dSDavid Howells 			ret = add_to_page_cache_lru(req->pages[i],
281f3ddee8dSDavid Howells 						    dvnode->vfs_inode.i_mapping,
282f3ddee8dSDavid Howells 						    i, gfp);
283f3ddee8dSDavid Howells 			if (ret < 0)
284f3ddee8dSDavid Howells 				goto error;
285f3ddee8dSDavid Howells 
286f3ddee8dSDavid Howells 			set_page_private(req->pages[i], 1);
287f3ddee8dSDavid Howells 			SetPagePrivate(req->pages[i]);
288f3ddee8dSDavid Howells 			unlock_page(req->pages[i]);
289f3ddee8dSDavid Howells 			i++;
290f3ddee8dSDavid Howells 		} else {
291f3ddee8dSDavid Howells 			i += n;
292f3ddee8dSDavid Howells 		}
293f3ddee8dSDavid Howells 	} while (i < req->nr_pages);
294f3ddee8dSDavid Howells 
295f3ddee8dSDavid Howells 	/* If we're going to reload, we need to lock all the pages to prevent
296f3ddee8dSDavid Howells 	 * races.
297f3ddee8dSDavid Howells 	 */
298f3ddee8dSDavid Howells 	ret = -ERESTARTSYS;
299b61f7dcfSDavid Howells 	if (down_read_killable(&dvnode->validate_lock) < 0)
300b61f7dcfSDavid Howells 		goto error;
301f3ddee8dSDavid Howells 
302f3ddee8dSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
303f3ddee8dSDavid Howells 		goto success;
304f3ddee8dSDavid Howells 
305b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
306b61f7dcfSDavid Howells 	if (down_write_killable(&dvnode->validate_lock) < 0)
307b61f7dcfSDavid Howells 		goto error;
308b61f7dcfSDavid Howells 
309b61f7dcfSDavid Howells 	if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
31099987c56SDavid Howells 		trace_afs_reload_dir(dvnode);
311f3ddee8dSDavid Howells 		ret = afs_fetch_data(dvnode, key, req);
312f3ddee8dSDavid Howells 		if (ret < 0)
313b61f7dcfSDavid Howells 			goto error_unlock;
314f3ddee8dSDavid Howells 
315f3ddee8dSDavid Howells 		task_io_account_read(PAGE_SIZE * req->nr_pages);
316f3ddee8dSDavid Howells 
317f3ddee8dSDavid Howells 		if (req->len < req->file_size)
318f3ddee8dSDavid Howells 			goto content_has_grown;
319f3ddee8dSDavid Howells 
320f3ddee8dSDavid Howells 		/* Validate the data we just read. */
321f3ddee8dSDavid Howells 		ret = -EIO;
322445b1028SDavid Howells 		if (!afs_dir_check_pages(dvnode, req))
323b61f7dcfSDavid Howells 			goto error_unlock;
324f3ddee8dSDavid Howells 
325f3ddee8dSDavid Howells 		// TODO: Trim excess pages
326f3ddee8dSDavid Howells 
327f3ddee8dSDavid Howells 		set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
328f3ddee8dSDavid Howells 	}
329f3ddee8dSDavid Howells 
330b61f7dcfSDavid Howells 	downgrade_write(&dvnode->validate_lock);
331f3ddee8dSDavid Howells success:
332f3ddee8dSDavid Howells 	return req;
333f3ddee8dSDavid Howells 
334f3ddee8dSDavid Howells error_unlock:
335b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
336f3ddee8dSDavid Howells error:
337f3ddee8dSDavid Howells 	afs_put_read(req);
338f3ddee8dSDavid Howells 	_leave(" = %d", ret);
339f3ddee8dSDavid Howells 	return ERR_PTR(ret);
340f3ddee8dSDavid Howells 
341f3ddee8dSDavid Howells content_has_grown:
342b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
343f3ddee8dSDavid Howells 	afs_put_read(req);
344f3ddee8dSDavid Howells 	goto retry;
345f3ddee8dSDavid Howells }
346f3ddee8dSDavid Howells 
347f3ddee8dSDavid Howells /*
3481da177e4SLinus Torvalds  * deal with one block in an AFS directory
3491da177e4SLinus Torvalds  */
350f51375cdSDavid Howells static int afs_dir_iterate_block(struct afs_vnode *dvnode,
351f51375cdSDavid Howells 				 struct dir_context *ctx,
35200317636SDavid Howells 				 union afs_xdr_dir_block *block,
3531bbae9f8SAl Viro 				 unsigned blkoff)
3541da177e4SLinus Torvalds {
35500317636SDavid Howells 	union afs_xdr_dirent *dire;
3561da177e4SLinus Torvalds 	unsigned offset, next, curr;
3571da177e4SLinus Torvalds 	size_t nlen;
3581bbae9f8SAl Viro 	int tmp;
3591da177e4SLinus Torvalds 
3601bbae9f8SAl Viro 	_enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
3611da177e4SLinus Torvalds 
36200317636SDavid Howells 	curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	/* walk through the block, an entry at a time */
3654ea219a8SDavid Howells 	for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
3664ea219a8SDavid Howells 	     offset < AFS_DIR_SLOTS_PER_BLOCK;
3671da177e4SLinus Torvalds 	     offset = next
3681da177e4SLinus Torvalds 	     ) {
3691da177e4SLinus Torvalds 		next = offset + 1;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 		/* skip entries marked unused in the bitmap */
37200317636SDavid Howells 		if (!(block->hdr.bitmap[offset / 8] &
3731da177e4SLinus Torvalds 		      (1 << (offset % 8)))) {
3745b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: unused",
37500317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block), offset);
3761da177e4SLinus Torvalds 			if (offset >= curr)
3771bbae9f8SAl Viro 				ctx->pos = blkoff +
37800317636SDavid Howells 					next * sizeof(union afs_xdr_dirent);
3791da177e4SLinus Torvalds 			continue;
3801da177e4SLinus Torvalds 		}
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 		/* got a valid entry */
3831da177e4SLinus Torvalds 		dire = &block->dirents[offset];
3841da177e4SLinus Torvalds 		nlen = strnlen(dire->u.name,
3851da177e4SLinus Torvalds 			       sizeof(*block) -
38600317636SDavid Howells 			       offset * sizeof(union afs_xdr_dirent));
3871da177e4SLinus Torvalds 
3885b5e0928SAlexey Dobriyan 		_debug("ENT[%zu.%u]: %s %zu \"%s\"",
38900317636SDavid Howells 		       blkoff / sizeof(union afs_xdr_dir_block), offset,
3901da177e4SLinus Torvalds 		       (offset < curr ? "skip" : "fill"),
3911da177e4SLinus Torvalds 		       nlen, dire->u.name);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 		/* work out where the next possible entry is */
39400317636SDavid Howells 		for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
3954ea219a8SDavid Howells 			if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
3965b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
3971da177e4SLinus Torvalds 				       " %u travelled beyond end dir block"
3985b5e0928SAlexey Dobriyan 				       " (len %u/%zu)",
39900317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4001da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
401f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_over_end);
4021da177e4SLinus Torvalds 			}
40300317636SDavid Howells 			if (!(block->hdr.bitmap[next / 8] &
4041da177e4SLinus Torvalds 			      (1 << (next % 8)))) {
4055b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
4065b5e0928SAlexey Dobriyan 				       " %u unmarked extension (len %u/%zu)",
40700317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4081da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
409f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
4101da177e4SLinus Torvalds 			}
4111da177e4SLinus Torvalds 
4125b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: ext %u/%zu",
41300317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block),
4141da177e4SLinus Torvalds 			       next, tmp, nlen);
4151da177e4SLinus Torvalds 			next++;
4161da177e4SLinus Torvalds 		}
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 		/* skip if starts before the current position */
4191da177e4SLinus Torvalds 		if (offset < curr)
4201da177e4SLinus Torvalds 			continue;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 		/* found the next entry */
4231bbae9f8SAl Viro 		if (!dir_emit(ctx, dire->u.name, nlen,
4241da177e4SLinus Torvalds 			      ntohl(dire->u.vnode),
4255cf9dd55SDavid Howells 			      (ctx->actor == afs_lookup_filldir ||
4265cf9dd55SDavid Howells 			       ctx->actor == afs_lookup_one_filldir)?
4271bbae9f8SAl Viro 			      ntohl(dire->u.unique) : DT_UNKNOWN)) {
4281da177e4SLinus Torvalds 			_leave(" = 0 [full]");
4291da177e4SLinus Torvalds 			return 0;
4301da177e4SLinus Torvalds 		}
4311da177e4SLinus Torvalds 
43200317636SDavid Howells 		ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	_leave(" = 1 [more]");
4361da177e4SLinus Torvalds 	return 1;
437ec26815aSDavid Howells }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds /*
44008e0e7c8SDavid Howells  * iterate through the data blob that lists the contents of an AFS directory
4411da177e4SLinus Torvalds  */
4421bbae9f8SAl Viro static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
4439dd0b82eSDavid Howells 			   struct key *key, afs_dataversion_t *_dir_version)
4441da177e4SLinus Torvalds {
445f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
44600317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
44700317636SDavid Howells 	union afs_xdr_dir_block *dblock;
448f3ddee8dSDavid Howells 	struct afs_read *req;
4491da177e4SLinus Torvalds 	struct page *page;
4501da177e4SLinus Torvalds 	unsigned blkoff, limit;
4511da177e4SLinus Torvalds 	int ret;
4521da177e4SLinus Torvalds 
4531bbae9f8SAl Viro 	_enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
4541da177e4SLinus Torvalds 
45508e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
4561da177e4SLinus Torvalds 		_leave(" = -ESTALE");
4571da177e4SLinus Torvalds 		return -ESTALE;
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 
460f3ddee8dSDavid Howells 	req = afs_read_dir(dvnode, key);
461f3ddee8dSDavid Howells 	if (IS_ERR(req))
462f3ddee8dSDavid Howells 		return PTR_ERR(req);
4639dd0b82eSDavid Howells 	*_dir_version = req->data_version;
464f3ddee8dSDavid Howells 
4651da177e4SLinus Torvalds 	/* round the file position up to the next entry boundary */
46600317636SDavid Howells 	ctx->pos += sizeof(union afs_xdr_dirent) - 1;
46700317636SDavid Howells 	ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	/* walk through the blocks in sequence */
4701da177e4SLinus Torvalds 	ret = 0;
471f3ddee8dSDavid Howells 	while (ctx->pos < req->actual_len) {
47200317636SDavid Howells 		blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
4731da177e4SLinus Torvalds 
474f3ddee8dSDavid Howells 		/* Fetch the appropriate page from the directory and re-add it
475f3ddee8dSDavid Howells 		 * to the LRU.
476f3ddee8dSDavid Howells 		 */
477f3ddee8dSDavid Howells 		page = req->pages[blkoff / PAGE_SIZE];
478f3ddee8dSDavid Howells 		if (!page) {
479f51375cdSDavid Howells 			ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
4801da177e4SLinus Torvalds 			break;
4811da177e4SLinus Torvalds 		}
482f3ddee8dSDavid Howells 		mark_page_accessed(page);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 		limit = blkoff & ~(PAGE_SIZE - 1);
4851da177e4SLinus Torvalds 
486f3ddee8dSDavid Howells 		dbuf = kmap(page);
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 		/* deal with the individual blocks stashed on this page */
4891da177e4SLinus Torvalds 		do {
4901da177e4SLinus Torvalds 			dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
49100317636SDavid Howells 					       sizeof(union afs_xdr_dir_block)];
492f51375cdSDavid Howells 			ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
4931da177e4SLinus Torvalds 			if (ret != 1) {
494f3ddee8dSDavid Howells 				kunmap(page);
4951da177e4SLinus Torvalds 				goto out;
4961da177e4SLinus Torvalds 			}
4971da177e4SLinus Torvalds 
49800317636SDavid Howells 			blkoff += sizeof(union afs_xdr_dir_block);
4991da177e4SLinus Torvalds 
5001bbae9f8SAl Viro 		} while (ctx->pos < dir->i_size && blkoff < limit);
5011da177e4SLinus Torvalds 
502f3ddee8dSDavid Howells 		kunmap(page);
5031da177e4SLinus Torvalds 		ret = 0;
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds out:
507b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
508f3ddee8dSDavid Howells 	afs_put_read(req);
5091da177e4SLinus Torvalds 	_leave(" = %d", ret);
5101da177e4SLinus Torvalds 	return ret;
511ec26815aSDavid Howells }
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds /*
5141da177e4SLinus Torvalds  * read an AFS directory
5151da177e4SLinus Torvalds  */
5161bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx)
5171da177e4SLinus Torvalds {
5189dd0b82eSDavid Howells 	afs_dataversion_t dir_version;
5199dd0b82eSDavid Howells 
5209dd0b82eSDavid Howells 	return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file),
5219dd0b82eSDavid Howells 			       &dir_version);
522ec26815aSDavid Howells }
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds /*
5255cf9dd55SDavid Howells  * Search the directory for a single name
5261da177e4SLinus Torvalds  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5271da177e4SLinus Torvalds  *   uniquifier through dtype
5281da177e4SLinus Torvalds  */
5295cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
530ac7576f4SMiklos Szeredi 				  int nlen, loff_t fpos, u64 ino, unsigned dtype)
5311da177e4SLinus Torvalds {
5325cf9dd55SDavid Howells 	struct afs_lookup_one_cookie *cookie =
5335cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_one_cookie, ctx);
5341da177e4SLinus Torvalds 
5351bbae9f8SAl Viro 	_enter("{%s,%u},%s,%u,,%llu,%u",
5361bbae9f8SAl Viro 	       cookie->name.name, cookie->name.len, name, nlen,
537ba3e0e1aSDavid S. Miller 	       (unsigned long long) ino, dtype);
5381da177e4SLinus Torvalds 
53908e0e7c8SDavid Howells 	/* insanity checks first */
54000317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
54100317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
54208e0e7c8SDavid Howells 
5431bbae9f8SAl Viro 	if (cookie->name.len != nlen ||
5441bbae9f8SAl Viro 	    memcmp(cookie->name.name, name, nlen) != 0) {
5451da177e4SLinus Torvalds 		_leave(" = 0 [no]");
5461da177e4SLinus Torvalds 		return 0;
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	cookie->fid.vnode = ino;
5501da177e4SLinus Torvalds 	cookie->fid.unique = dtype;
5511da177e4SLinus Torvalds 	cookie->found = 1;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	_leave(" = -1 [found]");
5541da177e4SLinus Torvalds 	return -1;
555ec26815aSDavid Howells }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /*
5585cf9dd55SDavid Howells  * Do a lookup of a single name in a directory
559260a9803SDavid Howells  * - just returns the FID the dentry name maps to if found
5601da177e4SLinus Torvalds  */
5615cf9dd55SDavid Howells static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
5629dd0b82eSDavid Howells 			     struct afs_fid *fid, struct key *key,
5639dd0b82eSDavid Howells 			     afs_dataversion_t *_dir_version)
5641da177e4SLinus Torvalds {
5651bbae9f8SAl Viro 	struct afs_super_info *as = dir->i_sb->s_fs_info;
5665cf9dd55SDavid Howells 	struct afs_lookup_one_cookie cookie = {
5675cf9dd55SDavid Howells 		.ctx.actor = afs_lookup_one_filldir,
5681bbae9f8SAl Viro 		.name = dentry->d_name,
5691bbae9f8SAl Viro 		.fid.vid = as->volume->vid
5701bbae9f8SAl Viro 	};
5711da177e4SLinus Torvalds 	int ret;
5721da177e4SLinus Torvalds 
573a455589fSAl Viro 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	/* search the directory */
5769dd0b82eSDavid Howells 	ret = afs_dir_iterate(dir, &cookie.ctx, key, _dir_version);
5771da177e4SLinus Torvalds 	if (ret < 0) {
57808e0e7c8SDavid Howells 		_leave(" = %d [iter]", ret);
57908e0e7c8SDavid Howells 		return ret;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	ret = -ENOENT;
5831da177e4SLinus Torvalds 	if (!cookie.found) {
58408e0e7c8SDavid Howells 		_leave(" = -ENOENT [not found]");
58508e0e7c8SDavid Howells 		return -ENOENT;
58608e0e7c8SDavid Howells 	}
58708e0e7c8SDavid Howells 
58808e0e7c8SDavid Howells 	*fid = cookie.fid;
5893b6492dfSDavid Howells 	_leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
59008e0e7c8SDavid Howells 	return 0;
59108e0e7c8SDavid Howells }
59208e0e7c8SDavid Howells 
59308e0e7c8SDavid Howells /*
5945cf9dd55SDavid Howells  * search the directory for a name
5955cf9dd55SDavid Howells  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5965cf9dd55SDavid Howells  *   uniquifier through dtype
5975cf9dd55SDavid Howells  */
5985cf9dd55SDavid Howells static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
5995cf9dd55SDavid Howells 			      int nlen, loff_t fpos, u64 ino, unsigned dtype)
6005cf9dd55SDavid Howells {
6015cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie =
6025cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_cookie, ctx);
6035cf9dd55SDavid Howells 	int ret;
6045cf9dd55SDavid Howells 
6055cf9dd55SDavid Howells 	_enter("{%s,%u},%s,%u,,%llu,%u",
6065cf9dd55SDavid Howells 	       cookie->name.name, cookie->name.len, name, nlen,
6075cf9dd55SDavid Howells 	       (unsigned long long) ino, dtype);
6085cf9dd55SDavid Howells 
6095cf9dd55SDavid Howells 	/* insanity checks first */
61000317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
61100317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
6125cf9dd55SDavid Howells 
6135cf9dd55SDavid Howells 	if (cookie->found) {
6145cf9dd55SDavid Howells 		if (cookie->nr_fids < 50) {
6155cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].vnode	= ino;
6165cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].unique	= dtype;
6175cf9dd55SDavid Howells 			cookie->nr_fids++;
6185cf9dd55SDavid Howells 		}
6195cf9dd55SDavid Howells 	} else if (cookie->name.len == nlen &&
6205cf9dd55SDavid Howells 		   memcmp(cookie->name.name, name, nlen) == 0) {
6215cf9dd55SDavid Howells 		cookie->fids[0].vnode	= ino;
6225cf9dd55SDavid Howells 		cookie->fids[0].unique	= dtype;
6235cf9dd55SDavid Howells 		cookie->found = 1;
6245cf9dd55SDavid Howells 		if (cookie->one_only)
6255cf9dd55SDavid Howells 			return -1;
6265cf9dd55SDavid Howells 	}
6275cf9dd55SDavid Howells 
6285cf9dd55SDavid Howells 	ret = cookie->nr_fids >= 50 ? -1 : 0;
6295cf9dd55SDavid Howells 	_leave(" = %d", ret);
6305cf9dd55SDavid Howells 	return ret;
6315cf9dd55SDavid Howells }
6325cf9dd55SDavid Howells 
6335cf9dd55SDavid Howells /*
6345cf9dd55SDavid Howells  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
6355cf9dd55SDavid Howells  * files in one go and create inodes for them.  The inode of the file we were
6365cf9dd55SDavid Howells  * asked for is returned.
6375cf9dd55SDavid Howells  */
6385cf9dd55SDavid Howells static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
6395cf9dd55SDavid Howells 				   struct key *key)
6405cf9dd55SDavid Howells {
6415cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie;
642f642404aSDavid Howells 	struct afs_cb_interest *dcbi, *cbi = NULL;
6435cf9dd55SDavid Howells 	struct afs_super_info *as = dir->i_sb->s_fs_info;
64487182759SDavid Howells 	struct afs_status_cb *scb;
645b8359153SDavid Howells 	struct afs_iget_data iget_data;
6465cf9dd55SDavid Howells 	struct afs_fs_cursor fc;
647f642404aSDavid Howells 	struct afs_server *server;
64839db9815SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
64939db9815SDavid Howells 	struct inode *inode = NULL, *ti;
6509dd0b82eSDavid Howells 	afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version);
6515cf9dd55SDavid Howells 	int ret, i;
6525cf9dd55SDavid Howells 
6535cf9dd55SDavid Howells 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
6545cf9dd55SDavid Howells 
6555cf9dd55SDavid Howells 	cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
6565cf9dd55SDavid Howells 	if (!cookie)
6575cf9dd55SDavid Howells 		return ERR_PTR(-ENOMEM);
6585cf9dd55SDavid Howells 
6595cf9dd55SDavid Howells 	cookie->ctx.actor = afs_lookup_filldir;
6605cf9dd55SDavid Howells 	cookie->name = dentry->d_name;
6615cf9dd55SDavid Howells 	cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
6625cf9dd55SDavid Howells 
6635cf9dd55SDavid Howells 	read_seqlock_excl(&dvnode->cb_lock);
664f642404aSDavid Howells 	dcbi = rcu_dereference_protected(dvnode->cb_interest,
665f642404aSDavid Howells 					 lockdep_is_held(&dvnode->cb_lock.lock));
666f642404aSDavid Howells 	if (dcbi) {
667f642404aSDavid Howells 		server = dcbi->server;
668f642404aSDavid Howells 		if (server &&
669f642404aSDavid Howells 		    test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
6705cf9dd55SDavid Howells 			cookie->one_only = true;
671f642404aSDavid Howells 	}
6725cf9dd55SDavid Howells 	read_sequnlock_excl(&dvnode->cb_lock);
6735cf9dd55SDavid Howells 
6745cf9dd55SDavid Howells 	for (i = 0; i < 50; i++)
6755cf9dd55SDavid Howells 		cookie->fids[i].vid = as->volume->vid;
6765cf9dd55SDavid Howells 
6775cf9dd55SDavid Howells 	/* search the directory */
6789dd0b82eSDavid Howells 	ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version);
6795cf9dd55SDavid Howells 	if (ret < 0) {
6805cf9dd55SDavid Howells 		inode = ERR_PTR(ret);
6815cf9dd55SDavid Howells 		goto out;
6825cf9dd55SDavid Howells 	}
6835cf9dd55SDavid Howells 
6849dd0b82eSDavid Howells 	dentry->d_fsdata = (void *)(unsigned long)data_version;
6859dd0b82eSDavid Howells 
6865cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOENT);
6875cf9dd55SDavid Howells 	if (!cookie->found)
6885cf9dd55SDavid Howells 		goto out;
6895cf9dd55SDavid Howells 
6905cf9dd55SDavid Howells 	/* Check to see if we already have an inode for the primary fid. */
691b8359153SDavid Howells 	iget_data.fid = cookie->fids[0];
692b8359153SDavid Howells 	iget_data.volume = dvnode->volume;
693b8359153SDavid Howells 	iget_data.cb_v_break = dvnode->volume->cb_v_break;
694b8359153SDavid Howells 	iget_data.cb_s_break = 0;
695b8359153SDavid Howells 	inode = ilookup5(dir->i_sb, cookie->fids[0].vnode,
696b8359153SDavid Howells 			 afs_iget5_test, &iget_data);
6975cf9dd55SDavid Howells 	if (inode)
6985cf9dd55SDavid Howells 		goto out;
6995cf9dd55SDavid Howells 
7005cf9dd55SDavid Howells 	/* Need space for examining all the selected files */
7015cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOMEM);
70287182759SDavid Howells 	cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb),
7035cf9dd55SDavid Howells 				    GFP_KERNEL);
7045cf9dd55SDavid Howells 	if (!cookie->statuses)
7055cf9dd55SDavid Howells 		goto out;
7065cf9dd55SDavid Howells 
70739db9815SDavid Howells 	cookie->inodes = kcalloc(cookie->nr_fids, sizeof(struct inode *),
70839db9815SDavid Howells 				 GFP_KERNEL);
70939db9815SDavid Howells 	if (!cookie->inodes)
71039db9815SDavid Howells 		goto out_s;
71139db9815SDavid Howells 
71239db9815SDavid Howells 	for (i = 1; i < cookie->nr_fids; i++) {
71339db9815SDavid Howells 		scb = &cookie->statuses[i];
71439db9815SDavid Howells 
71539db9815SDavid Howells 		/* Find any inodes that already exist and get their
71639db9815SDavid Howells 		 * callback counters.
71739db9815SDavid Howells 		 */
71839db9815SDavid Howells 		iget_data.fid = cookie->fids[i];
71939db9815SDavid Howells 		ti = ilookup5_nowait(dir->i_sb, iget_data.fid.vnode,
72039db9815SDavid Howells 				     afs_iget5_test, &iget_data);
72139db9815SDavid Howells 		if (!IS_ERR_OR_NULL(ti)) {
72239db9815SDavid Howells 			vnode = AFS_FS_I(ti);
72339db9815SDavid Howells 			scb->cb_break = afs_calc_vnode_cb_break(vnode);
72439db9815SDavid Howells 			cookie->inodes[i] = ti;
72539db9815SDavid Howells 		}
72639db9815SDavid Howells 	}
72739db9815SDavid Howells 
7285cf9dd55SDavid Howells 	/* Try FS.InlineBulkStatus first.  Abort codes for the individual
7295cf9dd55SDavid Howells 	 * lookups contained therein are stored in the reply without aborting
7305cf9dd55SDavid Howells 	 * the whole operation.
7315cf9dd55SDavid Howells 	 */
7325cf9dd55SDavid Howells 	if (cookie->one_only)
7335cf9dd55SDavid Howells 		goto no_inline_bulk_status;
7345cf9dd55SDavid Howells 
7355cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
73620b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
7375cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
7385cf9dd55SDavid Howells 			if (test_bit(AFS_SERVER_FL_NO_IBULK,
7395cf9dd55SDavid Howells 				      &fc.cbi->server->flags)) {
7405cf9dd55SDavid Howells 				fc.ac.abort_code = RX_INVALID_OPERATION;
7415cf9dd55SDavid Howells 				fc.ac.error = -ECONNABORTED;
7425cf9dd55SDavid Howells 				break;
7435cf9dd55SDavid Howells 			}
744b8359153SDavid Howells 			iget_data.cb_v_break = dvnode->volume->cb_v_break;
745b8359153SDavid Howells 			iget_data.cb_s_break = fc.cbi->server->cb_s_break;
7465cf9dd55SDavid Howells 			afs_fs_inline_bulk_status(&fc,
7475cf9dd55SDavid Howells 						  afs_v2net(dvnode),
7485cf9dd55SDavid Howells 						  cookie->fids,
7495cf9dd55SDavid Howells 						  cookie->statuses,
7505cf9dd55SDavid Howells 						  cookie->nr_fids, NULL);
7515cf9dd55SDavid Howells 		}
7525cf9dd55SDavid Howells 
7535cf9dd55SDavid Howells 		if (fc.ac.error == 0)
7545cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
7555cf9dd55SDavid Howells 		if (fc.ac.abort_code == RX_INVALID_OPERATION)
7565cf9dd55SDavid Howells 			set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
7575cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
7585cf9dd55SDavid Howells 	}
7595cf9dd55SDavid Howells 
7605cf9dd55SDavid Howells 	if (!IS_ERR(inode))
7615cf9dd55SDavid Howells 		goto success;
7625cf9dd55SDavid Howells 	if (fc.ac.abort_code != RX_INVALID_OPERATION)
7635cf9dd55SDavid Howells 		goto out_c;
7645cf9dd55SDavid Howells 
7655cf9dd55SDavid Howells no_inline_bulk_status:
7665cf9dd55SDavid Howells 	/* We could try FS.BulkStatus next, but this aborts the entire op if
7675cf9dd55SDavid Howells 	 * any of the lookups fails - so, for the moment, revert to
7685cf9dd55SDavid Howells 	 * FS.FetchStatus for just the primary fid.
7695cf9dd55SDavid Howells 	 */
7705cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
77120b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
7725cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
773b8359153SDavid Howells 			iget_data.cb_v_break = dvnode->volume->cb_v_break;
774b8359153SDavid Howells 			iget_data.cb_s_break = fc.cbi->server->cb_s_break;
77587182759SDavid Howells 			scb = &cookie->statuses[0];
7765cf9dd55SDavid Howells 			afs_fs_fetch_status(&fc,
7775cf9dd55SDavid Howells 					    afs_v2net(dvnode),
7785cf9dd55SDavid Howells 					    cookie->fids,
779a58823acSDavid Howells 					    scb,
7805cf9dd55SDavid Howells 					    NULL);
7815cf9dd55SDavid Howells 		}
7825cf9dd55SDavid Howells 
7835cf9dd55SDavid Howells 		if (fc.ac.error == 0)
7845cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
7855cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
7865cf9dd55SDavid Howells 	}
7875cf9dd55SDavid Howells 
7885cf9dd55SDavid Howells 	if (IS_ERR(inode))
7895cf9dd55SDavid Howells 		goto out_c;
7905cf9dd55SDavid Howells 
7915cf9dd55SDavid Howells success:
7925cf9dd55SDavid Howells 	/* Turn all the files into inodes and save the first one - which is the
7935cf9dd55SDavid Howells 	 * one we actually want.
7945cf9dd55SDavid Howells 	 */
79587182759SDavid Howells 	scb = &cookie->statuses[0];
79687182759SDavid Howells 	if (scb->status.abort_code != 0)
79787182759SDavid Howells 		inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code));
7985cf9dd55SDavid Howells 
7995cf9dd55SDavid Howells 	for (i = 0; i < cookie->nr_fids; i++) {
80087182759SDavid Howells 		struct afs_status_cb *scb = &cookie->statuses[i];
80139db9815SDavid Howells 
80239db9815SDavid Howells 		if (!scb->have_status && !scb->have_error)
80339db9815SDavid Howells 			continue;
80439db9815SDavid Howells 
80539db9815SDavid Howells 		if (cookie->inodes[i]) {
806a28f239eSDavid Howells 			struct afs_vnode *iv = AFS_FS_I(cookie->inodes[i]);
807a28f239eSDavid Howells 
808a28f239eSDavid Howells 			if (test_bit(AFS_VNODE_UNSET, &iv->flags))
809a28f239eSDavid Howells 				continue;
810a28f239eSDavid Howells 
811a28f239eSDavid Howells 			afs_vnode_commit_status(&fc, iv,
81239db9815SDavid Howells 						scb->cb_break, NULL, scb);
81339db9815SDavid Howells 			continue;
81439db9815SDavid Howells 		}
8155cf9dd55SDavid Howells 
81687182759SDavid Howells 		if (scb->status.abort_code != 0)
8175cf9dd55SDavid Howells 			continue;
8185cf9dd55SDavid Howells 
819b8359153SDavid Howells 		iget_data.fid = cookie->fids[i];
820b8359153SDavid Howells 		ti = afs_iget(dir->i_sb, key, &iget_data, scb, cbi, dvnode);
82139db9815SDavid Howells 		if (!IS_ERR(ti))
82239db9815SDavid Howells 			afs_cache_permit(AFS_FS_I(ti), key,
82339db9815SDavid Howells 					 0 /* Assume vnode->cb_break is 0 */ +
82439db9815SDavid Howells 					 iget_data.cb_v_break,
82539db9815SDavid Howells 					 scb);
8265cf9dd55SDavid Howells 		if (i == 0) {
8275cf9dd55SDavid Howells 			inode = ti;
8285cf9dd55SDavid Howells 		} else {
8295cf9dd55SDavid Howells 			if (!IS_ERR(ti))
8305cf9dd55SDavid Howells 				iput(ti);
8315cf9dd55SDavid Howells 		}
8325cf9dd55SDavid Howells 	}
8335cf9dd55SDavid Howells 
8345cf9dd55SDavid Howells out_c:
8355cf9dd55SDavid Howells 	afs_put_cb_interest(afs_v2net(dvnode), cbi);
83639db9815SDavid Howells 	if (cookie->inodes) {
83739db9815SDavid Howells 		for (i = 0; i < cookie->nr_fids; i++)
83839db9815SDavid Howells 			iput(cookie->inodes[i]);
83939db9815SDavid Howells 		kfree(cookie->inodes);
84039db9815SDavid Howells 	}
84139db9815SDavid Howells out_s:
84287182759SDavid Howells 	kvfree(cookie->statuses);
8435cf9dd55SDavid Howells out:
8445cf9dd55SDavid Howells 	kfree(cookie);
8455cf9dd55SDavid Howells 	return inode;
8465cf9dd55SDavid Howells }
8475cf9dd55SDavid Howells 
8485cf9dd55SDavid Howells /*
8496f8880d8SDavid Howells  * Look up an entry in a directory with @sys substitution.
8506f8880d8SDavid Howells  */
8516f8880d8SDavid Howells static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
8526f8880d8SDavid Howells 				       struct key *key)
8536f8880d8SDavid Howells {
8546f8880d8SDavid Howells 	struct afs_sysnames *subs;
8556f8880d8SDavid Howells 	struct afs_net *net = afs_i2net(dir);
8566f8880d8SDavid Howells 	struct dentry *ret;
8576f8880d8SDavid Howells 	char *buf, *p, *name;
8586f8880d8SDavid Howells 	int len, i;
8596f8880d8SDavid Howells 
8606f8880d8SDavid Howells 	_enter("");
8616f8880d8SDavid Howells 
8626f8880d8SDavid Howells 	ret = ERR_PTR(-ENOMEM);
8636f8880d8SDavid Howells 	p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
8646f8880d8SDavid Howells 	if (!buf)
8656f8880d8SDavid Howells 		goto out_p;
8666f8880d8SDavid Howells 	if (dentry->d_name.len > 4) {
8676f8880d8SDavid Howells 		memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
8686f8880d8SDavid Howells 		p += dentry->d_name.len - 4;
8696f8880d8SDavid Howells 	}
8706f8880d8SDavid Howells 
8716f8880d8SDavid Howells 	/* There is an ordered list of substitutes that we have to try. */
8726f8880d8SDavid Howells 	read_lock(&net->sysnames_lock);
8736f8880d8SDavid Howells 	subs = net->sysnames;
8746f8880d8SDavid Howells 	refcount_inc(&subs->usage);
8756f8880d8SDavid Howells 	read_unlock(&net->sysnames_lock);
8766f8880d8SDavid Howells 
8776f8880d8SDavid Howells 	for (i = 0; i < subs->nr; i++) {
8786f8880d8SDavid Howells 		name = subs->subs[i];
8796f8880d8SDavid Howells 		len = dentry->d_name.len - 4 + strlen(name);
8806f8880d8SDavid Howells 		if (len >= AFSNAMEMAX) {
8816f8880d8SDavid Howells 			ret = ERR_PTR(-ENAMETOOLONG);
8826f8880d8SDavid Howells 			goto out_s;
8836f8880d8SDavid Howells 		}
8846f8880d8SDavid Howells 
8856f8880d8SDavid Howells 		strcpy(p, name);
8866f8880d8SDavid Howells 		ret = lookup_one_len(buf, dentry->d_parent, len);
8876f8880d8SDavid Howells 		if (IS_ERR(ret) || d_is_positive(ret))
8886f8880d8SDavid Howells 			goto out_s;
8896f8880d8SDavid Howells 		dput(ret);
8906f8880d8SDavid Howells 	}
8916f8880d8SDavid Howells 
8926f8880d8SDavid Howells 	/* We don't want to d_add() the @sys dentry here as we don't want to
8936f8880d8SDavid Howells 	 * the cached dentry to hide changes to the sysnames list.
8946f8880d8SDavid Howells 	 */
8956f8880d8SDavid Howells 	ret = NULL;
8966f8880d8SDavid Howells out_s:
8976f8880d8SDavid Howells 	afs_put_sysnames(subs);
8986f8880d8SDavid Howells 	kfree(buf);
8996f8880d8SDavid Howells out_p:
9006f8880d8SDavid Howells 	key_put(key);
9016f8880d8SDavid Howells 	return ret;
9026f8880d8SDavid Howells }
9036f8880d8SDavid Howells 
9046f8880d8SDavid Howells /*
90508e0e7c8SDavid Howells  * look up an entry in a directory
90608e0e7c8SDavid Howells  */
907260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
90800cd8dd3SAl Viro 				 unsigned int flags)
90908e0e7c8SDavid Howells {
9105cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
91140a708bdSDavid Howells 	struct afs_fid fid = {};
91208e0e7c8SDavid Howells 	struct inode *inode;
91334b2a88fSAl Viro 	struct dentry *d;
91400d3b7a4SDavid Howells 	struct key *key;
91508e0e7c8SDavid Howells 	int ret;
91608e0e7c8SDavid Howells 
9173b6492dfSDavid Howells 	_enter("{%llx:%llu},%p{%pd},",
9185cf9dd55SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
919260a9803SDavid Howells 
9202b0143b5SDavid Howells 	ASSERTCMP(d_inode(dentry), ==, NULL);
92108e0e7c8SDavid Howells 
92245222b9eSDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX) {
92308e0e7c8SDavid Howells 		_leave(" = -ENAMETOOLONG");
92408e0e7c8SDavid Howells 		return ERR_PTR(-ENAMETOOLONG);
92508e0e7c8SDavid Howells 	}
92608e0e7c8SDavid Howells 
9275cf9dd55SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
92808e0e7c8SDavid Howells 		_leave(" = -ESTALE");
92908e0e7c8SDavid Howells 		return ERR_PTR(-ESTALE);
93008e0e7c8SDavid Howells 	}
93108e0e7c8SDavid Howells 
9325cf9dd55SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
93300d3b7a4SDavid Howells 	if (IS_ERR(key)) {
93400d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
935e231c2eeSDavid Howells 		return ERR_CAST(key);
93600d3b7a4SDavid Howells 	}
93700d3b7a4SDavid Howells 
9385cf9dd55SDavid Howells 	ret = afs_validate(dvnode, key);
93908e0e7c8SDavid Howells 	if (ret < 0) {
94000d3b7a4SDavid Howells 		key_put(key);
941260a9803SDavid Howells 		_leave(" = %d [val]", ret);
9421da177e4SLinus Torvalds 		return ERR_PTR(ret);
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 
9456f8880d8SDavid Howells 	if (dentry->d_name.len >= 4 &&
9466f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
9476f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
9486f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
9496f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 1] == 's')
9506f8880d8SDavid Howells 		return afs_lookup_atsys(dir, dentry, key);
9516f8880d8SDavid Howells 
952d55b4da4SDavid Howells 	afs_stat_v(dvnode, n_lookup);
9535cf9dd55SDavid Howells 	inode = afs_do_lookup(dir, dentry, key);
95434b2a88fSAl Viro 	key_put(key);
955f52b83b0SDavid Howells 	if (inode == ERR_PTR(-ENOENT))
9565cf9dd55SDavid Howells 		inode = afs_try_auto_mntpt(dentry, dir);
95740a708bdSDavid Howells 
95840a708bdSDavid Howells 	if (!IS_ERR_OR_NULL(inode))
95940a708bdSDavid Howells 		fid = AFS_FS_I(inode)->fid;
96040a708bdSDavid Howells 
96134b2a88fSAl Viro 	d = d_splice_alias(inode, dentry);
96280548b03SDavid Howells 	if (!IS_ERR_OR_NULL(d)) {
96334b2a88fSAl Viro 		d->d_fsdata = dentry->d_fsdata;
96440a708bdSDavid Howells 		trace_afs_lookup(dvnode, &d->d_name, &fid);
96580548b03SDavid Howells 	} else {
96640a708bdSDavid Howells 		trace_afs_lookup(dvnode, &dentry->d_name, &fid);
96780548b03SDavid Howells 	}
96834b2a88fSAl Viro 	return d;
969ec26815aSDavid Howells }
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds /*
972a0753c29SDavid Howells  * Check the validity of a dentry under RCU conditions.
973a0753c29SDavid Howells  */
974a0753c29SDavid Howells static int afs_d_revalidate_rcu(struct dentry *dentry)
975a0753c29SDavid Howells {
976a0753c29SDavid Howells 	struct afs_vnode *dvnode, *vnode;
977a0753c29SDavid Howells 	struct dentry *parent;
978a0753c29SDavid Howells 	struct inode *dir, *inode;
979a0753c29SDavid Howells 	long dir_version, de_version;
980a0753c29SDavid Howells 
981a0753c29SDavid Howells 	_enter("%p", dentry);
982a0753c29SDavid Howells 
983a0753c29SDavid Howells 	/* Check the parent directory is still valid first. */
984a0753c29SDavid Howells 	parent = READ_ONCE(dentry->d_parent);
985a0753c29SDavid Howells 	dir = d_inode_rcu(parent);
986a0753c29SDavid Howells 	if (!dir)
987a0753c29SDavid Howells 		return -ECHILD;
988a0753c29SDavid Howells 	dvnode = AFS_FS_I(dir);
989a0753c29SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags))
990a0753c29SDavid Howells 		return -ECHILD;
991a0753c29SDavid Howells 
992a0753c29SDavid Howells 	if (!afs_check_validity(dvnode))
993a0753c29SDavid Howells 		return -ECHILD;
994a0753c29SDavid Howells 
995a0753c29SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
996a0753c29SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
997a0753c29SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
998a0753c29SDavid Howells 	 * version.
999a0753c29SDavid Howells 	 */
1000a0753c29SDavid Howells 	dir_version = (long)READ_ONCE(dvnode->status.data_version);
1001a0753c29SDavid Howells 	de_version = (long)READ_ONCE(dentry->d_fsdata);
1002a0753c29SDavid Howells 	if (de_version != dir_version) {
1003a0753c29SDavid Howells 		dir_version = (long)READ_ONCE(dvnode->invalid_before);
1004a0753c29SDavid Howells 		if (de_version - dir_version < 0)
1005a0753c29SDavid Howells 			return -ECHILD;
1006a0753c29SDavid Howells 	}
1007a0753c29SDavid Howells 
1008a0753c29SDavid Howells 	/* Check to see if the vnode referred to by the dentry still
1009a0753c29SDavid Howells 	 * has a callback.
1010a0753c29SDavid Howells 	 */
1011a0753c29SDavid Howells 	if (d_really_is_positive(dentry)) {
1012a0753c29SDavid Howells 		inode = d_inode_rcu(dentry);
1013a0753c29SDavid Howells 		if (inode) {
1014a0753c29SDavid Howells 			vnode = AFS_FS_I(inode);
1015a0753c29SDavid Howells 			if (!afs_check_validity(vnode))
1016a0753c29SDavid Howells 				return -ECHILD;
1017a0753c29SDavid Howells 		}
1018a0753c29SDavid Howells 	}
1019a0753c29SDavid Howells 
1020a0753c29SDavid Howells 	return 1; /* Still valid */
1021a0753c29SDavid Howells }
1022a0753c29SDavid Howells 
1023a0753c29SDavid Howells /*
10241da177e4SLinus Torvalds  * check that a dentry lookup hit has found a valid entry
10251da177e4SLinus Torvalds  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
10261da177e4SLinus Torvalds  *   inode
10271da177e4SLinus Torvalds  */
10280b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
10291da177e4SLinus Torvalds {
1030260a9803SDavid Howells 	struct afs_vnode *vnode, *dir;
1031dd0d9a46SArtem Bityutskiy 	struct afs_fid uninitialized_var(fid);
10321da177e4SLinus Torvalds 	struct dentry *parent;
1033c435ee34SDavid Howells 	struct inode *inode;
103400d3b7a4SDavid Howells 	struct key *key;
10359dd0b82eSDavid Howells 	afs_dataversion_t dir_version;
10369dd0b82eSDavid Howells 	long de_version;
10371da177e4SLinus Torvalds 	int ret;
10381da177e4SLinus Torvalds 
10390b728e19SAl Viro 	if (flags & LOOKUP_RCU)
1040a0753c29SDavid Howells 		return afs_d_revalidate_rcu(dentry);
104134286d66SNick Piggin 
1042c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
10432b0143b5SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
10443b6492dfSDavid Howells 		_enter("{v={%llx:%llu} n=%pd fl=%lx},",
1045a455589fSAl Viro 		       vnode->fid.vid, vnode->fid.vnode, dentry,
1046260a9803SDavid Howells 		       vnode->flags);
1047c435ee34SDavid Howells 	} else {
1048a455589fSAl Viro 		_enter("{neg n=%pd}", dentry);
1049c435ee34SDavid Howells 	}
10501da177e4SLinus Torvalds 
1051260a9803SDavid Howells 	key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
105200d3b7a4SDavid Howells 	if (IS_ERR(key))
105300d3b7a4SDavid Howells 		key = NULL;
105400d3b7a4SDavid Howells 
1055c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
1056c435ee34SDavid Howells 		inode = d_inode(dentry);
1057c435ee34SDavid Howells 		if (inode) {
1058c435ee34SDavid Howells 			vnode = AFS_FS_I(inode);
1059c435ee34SDavid Howells 			afs_validate(vnode, key);
1060c435ee34SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1061c435ee34SDavid Howells 				goto out_bad;
1062c435ee34SDavid Howells 		}
1063c435ee34SDavid Howells 	}
1064c435ee34SDavid Howells 
10651da177e4SLinus Torvalds 	/* lock down the parent dentry so we can peer at it */
106608e0e7c8SDavid Howells 	parent = dget_parent(dentry);
10672b0143b5SDavid Howells 	dir = AFS_FS_I(d_inode(parent));
10681da177e4SLinus Torvalds 
1069260a9803SDavid Howells 	/* validate the parent directory */
1070260a9803SDavid Howells 	afs_validate(dir, key);
1071260a9803SDavid Howells 
1072260a9803SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1073a455589fSAl Viro 		_debug("%pd: parent dir deleted", dentry);
1074c435ee34SDavid Howells 		goto out_bad_parent;
10751da177e4SLinus Torvalds 	}
10761da177e4SLinus Torvalds 
1077a4ff7401SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
1078a4ff7401SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
1079a4ff7401SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
1080a4ff7401SDavid Howells 	 * version.
1081a4ff7401SDavid Howells 	 */
10829dd0b82eSDavid Howells 	dir_version = dir->status.data_version;
1083a4ff7401SDavid Howells 	de_version = (long)dentry->d_fsdata;
10849dd0b82eSDavid Howells 	if (de_version == (long)dir_version)
10855dc84855SDavid Howells 		goto out_valid_noupdate;
1086a4ff7401SDavid Howells 
10879dd0b82eSDavid Howells 	dir_version = dir->invalid_before;
10889dd0b82eSDavid Howells 	if (de_version - (long)dir_version >= 0)
1089a4ff7401SDavid Howells 		goto out_valid;
1090260a9803SDavid Howells 
109108e0e7c8SDavid Howells 	_debug("dir modified");
1092d55b4da4SDavid Howells 	afs_stat_v(dir, n_reval);
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	/* search the directory for this vnode */
10959dd0b82eSDavid Howells 	ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key, &dir_version);
1096260a9803SDavid Howells 	switch (ret) {
1097260a9803SDavid Howells 	case 0:
1098260a9803SDavid Howells 		/* the filename maps to something */
10992b0143b5SDavid Howells 		if (d_really_is_negative(dentry))
1100c435ee34SDavid Howells 			goto out_bad_parent;
1101c435ee34SDavid Howells 		inode = d_inode(dentry);
1102c435ee34SDavid Howells 		if (is_bad_inode(inode)) {
1103a455589fSAl Viro 			printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1104a455589fSAl Viro 			       dentry);
1105c435ee34SDavid Howells 			goto out_bad_parent;
11061da177e4SLinus Torvalds 		}
11071da177e4SLinus Torvalds 
1108c435ee34SDavid Howells 		vnode = AFS_FS_I(inode);
1109c435ee34SDavid Howells 
11101da177e4SLinus Torvalds 		/* if the vnode ID has changed, then the dirent points to a
11111da177e4SLinus Torvalds 		 * different file */
111208e0e7c8SDavid Howells 		if (fid.vnode != vnode->fid.vnode) {
11133b6492dfSDavid Howells 			_debug("%pd: dirent changed [%llu != %llu]",
1114a455589fSAl Viro 			       dentry, fid.vnode,
111508e0e7c8SDavid Howells 			       vnode->fid.vnode);
11161da177e4SLinus Torvalds 			goto not_found;
11171da177e4SLinus Torvalds 		}
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 		/* if the vnode ID uniqifier has changed, then the file has
1120260a9803SDavid Howells 		 * been deleted and replaced, and the original vnode ID has
1121260a9803SDavid Howells 		 * been reused */
112208e0e7c8SDavid Howells 		if (fid.unique != vnode->fid.unique) {
1123a455589fSAl Viro 			_debug("%pd: file deleted (uq %u -> %u I:%u)",
1124a455589fSAl Viro 			       dentry, fid.unique,
11257a224228SJean Noel Cordenner 			       vnode->fid.unique,
1126c435ee34SDavid Howells 			       vnode->vfs_inode.i_generation);
1127c435ee34SDavid Howells 			write_seqlock(&vnode->cb_lock);
112808e0e7c8SDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
1129c435ee34SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1130260a9803SDavid Howells 			goto not_found;
1131260a9803SDavid Howells 		}
1132260a9803SDavid Howells 		goto out_valid;
1133260a9803SDavid Howells 
1134260a9803SDavid Howells 	case -ENOENT:
1135260a9803SDavid Howells 		/* the filename is unknown */
1136a455589fSAl Viro 		_debug("%pd: dirent not found", dentry);
11372b0143b5SDavid Howells 		if (d_really_is_positive(dentry))
1138260a9803SDavid Howells 			goto not_found;
1139260a9803SDavid Howells 		goto out_valid;
1140260a9803SDavid Howells 
1141260a9803SDavid Howells 	default:
1142a455589fSAl Viro 		_debug("failed to iterate dir %pd: %d",
1143a455589fSAl Viro 		       parent, ret);
1144c435ee34SDavid Howells 		goto out_bad_parent;
11451da177e4SLinus Torvalds 	}
114608e0e7c8SDavid Howells 
11471da177e4SLinus Torvalds out_valid:
11489dd0b82eSDavid Howells 	dentry->d_fsdata = (void *)(unsigned long)dir_version;
11495dc84855SDavid Howells out_valid_noupdate:
11501da177e4SLinus Torvalds 	dput(parent);
115100d3b7a4SDavid Howells 	key_put(key);
11521da177e4SLinus Torvalds 	_leave(" = 1 [valid]");
11531da177e4SLinus Torvalds 	return 1;
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	/* the dirent, if it exists, now points to a different vnode */
11561da177e4SLinus Torvalds not_found:
11571da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
11581da177e4SLinus Torvalds 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
11591da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
11601da177e4SLinus Torvalds 
1161c435ee34SDavid Howells out_bad_parent:
1162a455589fSAl Viro 	_debug("dropping dentry %pd2", dentry);
11631da177e4SLinus Torvalds 	dput(parent);
1164c435ee34SDavid Howells out_bad:
116500d3b7a4SDavid Howells 	key_put(key);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	_leave(" = 0 [bad]");
11681da177e4SLinus Torvalds 	return 0;
1169ec26815aSDavid Howells }
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds /*
11721da177e4SLinus Torvalds  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
11731da177e4SLinus Torvalds  * sleep)
11741da177e4SLinus Torvalds  * - called from dput() when d_count is going to 0.
11751da177e4SLinus Torvalds  * - return 1 to request dentry be unhashed, 0 otherwise
11761da177e4SLinus Torvalds  */
1177fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry)
11781da177e4SLinus Torvalds {
1179a455589fSAl Viro 	_enter("%pd", dentry);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
11821da177e4SLinus Torvalds 		goto zap;
11831da177e4SLinus Torvalds 
11842b0143b5SDavid Howells 	if (d_really_is_positive(dentry) &&
11852b0143b5SDavid Howells 	    (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
11862b0143b5SDavid Howells 	     test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
11871da177e4SLinus Torvalds 		goto zap;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	_leave(" = 0 [keep]");
11901da177e4SLinus Torvalds 	return 0;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds zap:
11931da177e4SLinus Torvalds 	_leave(" = 1 [zap]");
11941da177e4SLinus Torvalds 	return 1;
1195ec26815aSDavid Howells }
1196260a9803SDavid Howells 
1197260a9803SDavid Howells /*
119879ddbfa5SDavid Howells  * Clean up sillyrename files on dentry removal.
119979ddbfa5SDavid Howells  */
120079ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode)
120179ddbfa5SDavid Howells {
120279ddbfa5SDavid Howells 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
120379ddbfa5SDavid Howells 		afs_silly_iput(dentry, inode);
120479ddbfa5SDavid Howells 	iput(inode);
120579ddbfa5SDavid Howells }
120679ddbfa5SDavid Howells 
120779ddbfa5SDavid Howells /*
1208260a9803SDavid Howells  * handle dentry release
1209260a9803SDavid Howells  */
121066c7e1d3SDavid Howells void afs_d_release(struct dentry *dentry)
1211260a9803SDavid Howells {
1212a455589fSAl Viro 	_enter("%pd", dentry);
1213260a9803SDavid Howells }
1214260a9803SDavid Howells 
1215260a9803SDavid Howells /*
1216d2ddc776SDavid Howells  * Create a new inode for create/mkdir/symlink
1217d2ddc776SDavid Howells  */
1218d2ddc776SDavid Howells static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1219d2ddc776SDavid Howells 				struct dentry *new_dentry,
1220b8359153SDavid Howells 				struct afs_iget_data *new_data,
1221a58823acSDavid Howells 				struct afs_status_cb *new_scb)
1222d2ddc776SDavid Howells {
12235a813276SDavid Howells 	struct afs_vnode *vnode;
1224d2ddc776SDavid Howells 	struct inode *inode;
1225d2ddc776SDavid Howells 
1226d2ddc776SDavid Howells 	if (fc->ac.error < 0)
1227d2ddc776SDavid Howells 		return;
1228d2ddc776SDavid Howells 
1229d2ddc776SDavid Howells 	inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1230b8359153SDavid Howells 			 new_data, new_scb, fc->cbi, fc->vnode);
1231d2ddc776SDavid Howells 	if (IS_ERR(inode)) {
1232d2ddc776SDavid Howells 		/* ENOMEM or EINTR at a really inconvenient time - just abandon
1233d2ddc776SDavid Howells 		 * the new directory on the server.
1234d2ddc776SDavid Howells 		 */
1235d2ddc776SDavid Howells 		fc->ac.error = PTR_ERR(inode);
1236d2ddc776SDavid Howells 		return;
1237d2ddc776SDavid Howells 	}
1238d2ddc776SDavid Howells 
12395a813276SDavid Howells 	vnode = AFS_FS_I(inode);
12405a813276SDavid Howells 	set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1241a58823acSDavid Howells 	if (fc->ac.error == 0)
1242a58823acSDavid Howells 		afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb);
124373116df7SDavid Howells 	d_instantiate(new_dentry, inode);
1244d2ddc776SDavid Howells }
1245d2ddc776SDavid Howells 
1246b8359153SDavid Howells static void afs_prep_for_new_inode(struct afs_fs_cursor *fc,
1247b8359153SDavid Howells 				   struct afs_iget_data *iget_data)
1248b8359153SDavid Howells {
1249b8359153SDavid Howells 	iget_data->volume = fc->vnode->volume;
1250b8359153SDavid Howells 	iget_data->cb_v_break = fc->vnode->volume->cb_v_break;
1251b8359153SDavid Howells 	iget_data->cb_s_break = fc->cbi->server->cb_s_break;
1252b8359153SDavid Howells }
1253b8359153SDavid Howells 
1254d2ddc776SDavid Howells /*
12559dd0b82eSDavid Howells  * Note that a dentry got changed.  We need to set d_fsdata to the data version
12569dd0b82eSDavid Howells  * number derived from the result of the operation.  It doesn't matter if
12579dd0b82eSDavid Howells  * d_fsdata goes backwards as we'll just revalidate.
12589dd0b82eSDavid Howells  */
12599dd0b82eSDavid Howells static void afs_update_dentry_version(struct afs_fs_cursor *fc,
12609dd0b82eSDavid Howells 				      struct dentry *dentry,
12619dd0b82eSDavid Howells 				      struct afs_status_cb *scb)
12629dd0b82eSDavid Howells {
12639dd0b82eSDavid Howells 	if (fc->ac.error == 0)
12649dd0b82eSDavid Howells 		dentry->d_fsdata =
12659dd0b82eSDavid Howells 			(void *)(unsigned long)scb->status.data_version;
12669dd0b82eSDavid Howells }
12679dd0b82eSDavid Howells 
12689dd0b82eSDavid Howells /*
1269260a9803SDavid Howells  * create a directory on an AFS filesystem
1270260a9803SDavid Howells  */
127118bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1272260a9803SDavid Howells {
1273b8359153SDavid Howells 	struct afs_iget_data iget_data;
1274a58823acSDavid Howells 	struct afs_status_cb *scb;
1275d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1276d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1277260a9803SDavid Howells 	struct key *key;
1278260a9803SDavid Howells 	int ret;
1279260a9803SDavid Howells 
1280d2ddc776SDavid Howells 	mode |= S_IFDIR;
1281260a9803SDavid Howells 
12823b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho",
1283a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1284260a9803SDavid Howells 
1285a58823acSDavid Howells 	ret = -ENOMEM;
1286a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1287a58823acSDavid Howells 	if (!scb)
1288a58823acSDavid Howells 		goto error;
1289a58823acSDavid Howells 
1290260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1291260a9803SDavid Howells 	if (IS_ERR(key)) {
1292260a9803SDavid Howells 		ret = PTR_ERR(key);
1293a58823acSDavid Howells 		goto error_scb;
1294260a9803SDavid Howells 	}
1295260a9803SDavid Howells 
1296d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
129720b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1298a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1299a58823acSDavid Howells 
1300d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
130168251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1302b8359153SDavid Howells 			afs_prep_for_new_inode(&fc, &iget_data);
1303a58823acSDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1304b8359153SDavid Howells 				      &scb[0], &iget_data.fid, &scb[1]);
1305d2ddc776SDavid Howells 		}
1306d2ddc776SDavid Howells 
1307a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1308a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1309a58823acSDavid Howells 					&data_version, &scb[0]);
13109dd0b82eSDavid Howells 		afs_update_dentry_version(&fc, dentry, &scb[0]);
1311b8359153SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1312d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1313260a9803SDavid Howells 		if (ret < 0)
1314d2ddc776SDavid Howells 			goto error_key;
13154433b691SDavid Howells 	} else {
13164433b691SDavid Howells 		goto error_key;
1317260a9803SDavid Howells 	}
1318260a9803SDavid Howells 
131963a4681fSDavid Howells 	if (ret == 0 &&
132063a4681fSDavid Howells 	    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1321b8359153SDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
132263a4681fSDavid Howells 				 afs_edit_dir_for_create);
132363a4681fSDavid Howells 
1324260a9803SDavid Howells 	key_put(key);
1325a58823acSDavid Howells 	kfree(scb);
1326260a9803SDavid Howells 	_leave(" = 0");
1327260a9803SDavid Howells 	return 0;
1328260a9803SDavid Howells 
1329d2ddc776SDavid Howells error_key:
1330260a9803SDavid Howells 	key_put(key);
1331a58823acSDavid Howells error_scb:
1332a58823acSDavid Howells 	kfree(scb);
1333260a9803SDavid Howells error:
1334260a9803SDavid Howells 	d_drop(dentry);
1335260a9803SDavid Howells 	_leave(" = %d", ret);
1336260a9803SDavid Howells 	return ret;
1337260a9803SDavid Howells }
1338260a9803SDavid Howells 
1339260a9803SDavid Howells /*
1340d2ddc776SDavid Howells  * Remove a subdir from a directory.
1341260a9803SDavid Howells  */
1342d2ddc776SDavid Howells static void afs_dir_remove_subdir(struct dentry *dentry)
1343260a9803SDavid Howells {
13442b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1345d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1346d2ddc776SDavid Howells 
1347260a9803SDavid Howells 		clear_nlink(&vnode->vfs_inode);
1348260a9803SDavid Howells 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1349c435ee34SDavid Howells 		clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
135063a4681fSDavid Howells 		clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1351260a9803SDavid Howells 	}
1352260a9803SDavid Howells }
1353260a9803SDavid Howells 
1354260a9803SDavid Howells /*
1355d2ddc776SDavid Howells  * remove a directory from an AFS filesystem
1356260a9803SDavid Howells  */
1357d2ddc776SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1358260a9803SDavid Howells {
1359a58823acSDavid Howells 	struct afs_status_cb *scb;
1360d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1361f58db83fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1362260a9803SDavid Howells 	struct key *key;
1363260a9803SDavid Howells 	int ret;
1364260a9803SDavid Howells 
13653b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1366a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1367260a9803SDavid Howells 
1368a58823acSDavid Howells 	scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
1369a58823acSDavid Howells 	if (!scb)
1370a58823acSDavid Howells 		return -ENOMEM;
1371a58823acSDavid Howells 
1372260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1373260a9803SDavid Howells 	if (IS_ERR(key)) {
1374260a9803SDavid Howells 		ret = PTR_ERR(key);
1375260a9803SDavid Howells 		goto error;
1376260a9803SDavid Howells 	}
1377260a9803SDavid Howells 
1378f58db83fSDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1379f58db83fSDavid Howells 	if (d_really_is_positive(dentry)) {
1380f58db83fSDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1381f58db83fSDavid Howells 		ret = afs_validate(vnode, key);
1382f58db83fSDavid Howells 		if (ret < 0)
1383f58db83fSDavid Howells 			goto error_key;
1384f58db83fSDavid Howells 	}
1385f58db83fSDavid Howells 
138679ddbfa5SDavid Howells 	if (vnode) {
138779ddbfa5SDavid Howells 		ret = down_write_killable(&vnode->rmdir_lock);
138879ddbfa5SDavid Howells 		if (ret < 0)
138979ddbfa5SDavid Howells 			goto error_key;
139079ddbfa5SDavid Howells 	}
139179ddbfa5SDavid Howells 
1392d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
139320b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1394a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1395a58823acSDavid Howells 
1396d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
139768251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1398a58823acSDavid Howells 			afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb);
1399260a9803SDavid Howells 		}
1400260a9803SDavid Howells 
1401a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1402a58823acSDavid Howells 					&data_version, scb);
14039dd0b82eSDavid Howells 		afs_update_dentry_version(&fc, dentry, scb);
1404d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
140563a4681fSDavid Howells 		if (ret == 0) {
1406d2ddc776SDavid Howells 			afs_dir_remove_subdir(dentry);
140763a4681fSDavid Howells 			if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
140863a4681fSDavid Howells 				afs_edit_dir_remove(dvnode, &dentry->d_name,
140963a4681fSDavid Howells 						    afs_edit_dir_for_rmdir);
141063a4681fSDavid Howells 		}
1411260a9803SDavid Howells 	}
1412260a9803SDavid Howells 
141379ddbfa5SDavid Howells 	if (vnode)
141479ddbfa5SDavid Howells 		up_write(&vnode->rmdir_lock);
1415f58db83fSDavid Howells error_key:
1416260a9803SDavid Howells 	key_put(key);
1417d2ddc776SDavid Howells error:
1418a58823acSDavid Howells 	kfree(scb);
1419d2ddc776SDavid Howells 	return ret;
1420d2ddc776SDavid Howells }
1421260a9803SDavid Howells 
1422d2ddc776SDavid Howells /*
1423d2ddc776SDavid Howells  * Remove a link to a file or symlink from a directory.
1424d2ddc776SDavid Howells  *
1425d2ddc776SDavid Howells  * If the file was not deleted due to excess hard links, the fileserver will
1426d2ddc776SDavid Howells  * break the callback promise on the file - if it had one - before it returns
1427d2ddc776SDavid Howells  * to us, and if it was deleted, it won't
1428d2ddc776SDavid Howells  *
1429d2ddc776SDavid Howells  * However, if we didn't have a callback promise outstanding, or it was
1430d2ddc776SDavid Howells  * outstanding on a different server, then it won't break it either...
1431d2ddc776SDavid Howells  */
1432a38a7558SDavid Howells static int afs_dir_remove_link(struct afs_vnode *dvnode, struct dentry *dentry,
1433a38a7558SDavid Howells 			       struct key *key)
1434d2ddc776SDavid Howells {
1435d2ddc776SDavid Howells 	int ret = 0;
1436d2ddc776SDavid Howells 
1437d2ddc776SDavid Howells 	if (d_really_is_positive(dentry)) {
1438d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1439d2ddc776SDavid Howells 
144030062bd1SDavid Howells 		if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
144130062bd1SDavid Howells 			/* Already done */
1442a38a7558SDavid Howells 		} else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1443a38a7558SDavid Howells 			write_seqlock(&vnode->cb_lock);
1444440fbc3aSDavid Howells 			drop_nlink(&vnode->vfs_inode);
1445440fbc3aSDavid Howells 			if (vnode->vfs_inode.i_nlink == 0) {
1446440fbc3aSDavid Howells 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
1447051d2525SDavid Howells 				__afs_break_callback(vnode, afs_cb_break_for_unlink);
1448440fbc3aSDavid Howells 			}
1449a38a7558SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1450440fbc3aSDavid Howells 			ret = 0;
1451440fbc3aSDavid Howells 		} else {
1452051d2525SDavid Howells 			afs_break_callback(vnode, afs_cb_break_for_unlink);
1453440fbc3aSDavid Howells 
1454d2ddc776SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1455d2ddc776SDavid Howells 				kdebug("AFS_VNODE_DELETED");
1456d2ddc776SDavid Howells 
1457d2ddc776SDavid Howells 			ret = afs_validate(vnode, key);
1458d2ddc776SDavid Howells 			if (ret == -ESTALE)
1459d2ddc776SDavid Howells 				ret = 0;
1460440fbc3aSDavid Howells 		}
1461d2ddc776SDavid Howells 		_debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1462d2ddc776SDavid Howells 	}
1463d2ddc776SDavid Howells 
1464d2ddc776SDavid Howells 	return ret;
1465d2ddc776SDavid Howells }
1466d2ddc776SDavid Howells 
1467d2ddc776SDavid Howells /*
1468d2ddc776SDavid Howells  * Remove a file or symlink from an AFS filesystem.
1469d2ddc776SDavid Howells  */
1470d2ddc776SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry)
1471d2ddc776SDavid Howells {
1472d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1473a58823acSDavid Howells 	struct afs_status_cb *scb;
1474fa59f52fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1475fa59f52fSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1476d2ddc776SDavid Howells 	struct key *key;
147779ddbfa5SDavid Howells 	bool need_rehash = false;
1478d2ddc776SDavid Howells 	int ret;
1479d2ddc776SDavid Howells 
14803b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1481d2ddc776SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1482d2ddc776SDavid Howells 
1483d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1484d2ddc776SDavid Howells 		return -ENAMETOOLONG;
1485d2ddc776SDavid Howells 
1486a58823acSDavid Howells 	ret = -ENOMEM;
1487a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1488a58823acSDavid Howells 	if (!scb)
1489a58823acSDavid Howells 		goto error;
1490a58823acSDavid Howells 
1491d2ddc776SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1492d2ddc776SDavid Howells 	if (IS_ERR(key)) {
1493d2ddc776SDavid Howells 		ret = PTR_ERR(key);
1494a58823acSDavid Howells 		goto error_scb;
1495d2ddc776SDavid Howells 	}
1496d2ddc776SDavid Howells 
1497d2ddc776SDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1498d2ddc776SDavid Howells 	ret = afs_validate(vnode, key);
1499d2ddc776SDavid Howells 	if (ret < 0)
1500d2ddc776SDavid Howells 		goto error_key;
1501d2ddc776SDavid Howells 
150279ddbfa5SDavid Howells 	spin_lock(&dentry->d_lock);
1503fa59f52fSDavid Howells 	if (d_count(dentry) > 1) {
150479ddbfa5SDavid Howells 		spin_unlock(&dentry->d_lock);
150579ddbfa5SDavid Howells 		/* Start asynchronous writeout of the inode */
150679ddbfa5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
150779ddbfa5SDavid Howells 		ret = afs_sillyrename(dvnode, vnode, dentry, key);
150879ddbfa5SDavid Howells 		goto error_key;
150979ddbfa5SDavid Howells 	}
151079ddbfa5SDavid Howells 	if (!d_unhashed(dentry)) {
151179ddbfa5SDavid Howells 		/* Prevent a race with RCU lookup. */
151279ddbfa5SDavid Howells 		__d_drop(dentry);
151379ddbfa5SDavid Howells 		need_rehash = true;
151479ddbfa5SDavid Howells 	}
151579ddbfa5SDavid Howells 	spin_unlock(&dentry->d_lock);
151679ddbfa5SDavid Howells 
1517d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
151820b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1519a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1520a38a7558SDavid Howells 		afs_dataversion_t data_version_2 = vnode->status.data_version;
1521a58823acSDavid Howells 
1522d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
152368251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1524a38a7558SDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
152530062bd1SDavid Howells 
152630062bd1SDavid Howells 			if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
152730062bd1SDavid Howells 			    !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
152830062bd1SDavid Howells 				yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1529a58823acSDavid Howells 						    &scb[0], &scb[1]);
153030062bd1SDavid Howells 				if (fc.ac.error != -ECONNABORTED ||
153130062bd1SDavid Howells 				    fc.ac.abort_code != RXGEN_OPCODE)
153230062bd1SDavid Howells 					continue;
153330062bd1SDavid Howells 				set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
153430062bd1SDavid Howells 			}
153530062bd1SDavid Howells 
1536a58823acSDavid Howells 			afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
1537d2ddc776SDavid Howells 		}
1538d2ddc776SDavid Howells 
1539a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1540a58823acSDavid Howells 					&data_version, &scb[0]);
1541a38a7558SDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1542a38a7558SDavid Howells 					&data_version_2, &scb[1]);
15439dd0b82eSDavid Howells 		afs_update_dentry_version(&fc, dentry, &scb[0]);
1544d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1545a38a7558SDavid Howells 		if (ret == 0 && !(scb[1].have_status || scb[1].have_error))
1546a38a7558SDavid Howells 			ret = afs_dir_remove_link(dvnode, dentry, key);
154763a4681fSDavid Howells 		if (ret == 0 &&
154863a4681fSDavid Howells 		    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
154963a4681fSDavid Howells 			afs_edit_dir_remove(dvnode, &dentry->d_name,
155063a4681fSDavid Howells 					    afs_edit_dir_for_unlink);
1551d2ddc776SDavid Howells 	}
1552d2ddc776SDavid Howells 
155379ddbfa5SDavid Howells 	if (need_rehash && ret < 0 && ret != -ENOENT)
155479ddbfa5SDavid Howells 		d_rehash(dentry);
155579ddbfa5SDavid Howells 
1556d2ddc776SDavid Howells error_key:
1557260a9803SDavid Howells 	key_put(key);
1558a58823acSDavid Howells error_scb:
1559a58823acSDavid Howells 	kfree(scb);
1560260a9803SDavid Howells error:
1561260a9803SDavid Howells 	_leave(" = %d", ret);
1562260a9803SDavid Howells 	return ret;
1563260a9803SDavid Howells }
1564260a9803SDavid Howells 
1565260a9803SDavid Howells /*
1566260a9803SDavid Howells  * create a regular file on an AFS filesystem
1567260a9803SDavid Howells  */
15684acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1569ebfc3b49SAl Viro 		      bool excl)
1570260a9803SDavid Howells {
1571b8359153SDavid Howells 	struct afs_iget_data iget_data;
1572d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1573a58823acSDavid Howells 	struct afs_status_cb *scb;
157443dd388bSColin Ian King 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1575260a9803SDavid Howells 	struct key *key;
1576260a9803SDavid Howells 	int ret;
1577260a9803SDavid Howells 
1578d2ddc776SDavid Howells 	mode |= S_IFREG;
1579260a9803SDavid Howells 
15803b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho,",
1581a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1582260a9803SDavid Howells 
1583d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1584d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1585d2ddc776SDavid Howells 		goto error;
1586d2ddc776SDavid Howells 
1587260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1588260a9803SDavid Howells 	if (IS_ERR(key)) {
1589260a9803SDavid Howells 		ret = PTR_ERR(key);
1590260a9803SDavid Howells 		goto error;
1591260a9803SDavid Howells 	}
1592260a9803SDavid Howells 
1593a58823acSDavid Howells 	ret = -ENOMEM;
1594a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1595a58823acSDavid Howells 	if (!scb)
1596a58823acSDavid Howells 		goto error_scb;
1597a58823acSDavid Howells 
1598d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
159920b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1600a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1601a58823acSDavid Howells 
1602d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
160368251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1604b8359153SDavid Howells 			afs_prep_for_new_inode(&fc, &iget_data);
1605a58823acSDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1606b8359153SDavid Howells 				      &scb[0], &iget_data.fid, &scb[1]);
1607d2ddc776SDavid Howells 		}
1608d2ddc776SDavid Howells 
1609a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1610a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1611a58823acSDavid Howells 					&data_version, &scb[0]);
16129dd0b82eSDavid Howells 		afs_update_dentry_version(&fc, dentry, &scb[0]);
1613b8359153SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1614d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1615260a9803SDavid Howells 		if (ret < 0)
1616d2ddc776SDavid Howells 			goto error_key;
16174433b691SDavid Howells 	} else {
16184433b691SDavid Howells 		goto error_key;
1619260a9803SDavid Howells 	}
1620260a9803SDavid Howells 
162163a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1622b8359153SDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
162363a4681fSDavid Howells 				 afs_edit_dir_for_create);
162463a4681fSDavid Howells 
1625a58823acSDavid Howells 	kfree(scb);
1626260a9803SDavid Howells 	key_put(key);
1627260a9803SDavid Howells 	_leave(" = 0");
1628260a9803SDavid Howells 	return 0;
1629260a9803SDavid Howells 
1630a58823acSDavid Howells error_scb:
1631a58823acSDavid Howells 	kfree(scb);
1632d2ddc776SDavid Howells error_key:
1633260a9803SDavid Howells 	key_put(key);
1634260a9803SDavid Howells error:
1635260a9803SDavid Howells 	d_drop(dentry);
1636260a9803SDavid Howells 	_leave(" = %d", ret);
1637260a9803SDavid Howells 	return ret;
1638260a9803SDavid Howells }
1639260a9803SDavid Howells 
1640260a9803SDavid Howells /*
1641260a9803SDavid Howells  * create a hard link between files in an AFS filesystem
1642260a9803SDavid Howells  */
1643260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
1644260a9803SDavid Howells 		    struct dentry *dentry)
1645260a9803SDavid Howells {
1646d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1647a58823acSDavid Howells 	struct afs_status_cb *scb;
1648a58823acSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1649a58823acSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1650260a9803SDavid Howells 	struct key *key;
1651260a9803SDavid Howells 	int ret;
1652260a9803SDavid Howells 
16533b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%pd}",
1654260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1655260a9803SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode,
1656a455589fSAl Viro 	       dentry);
1657260a9803SDavid Howells 
1658d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1659d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1660d2ddc776SDavid Howells 		goto error;
1661d2ddc776SDavid Howells 
1662a58823acSDavid Howells 	ret = -ENOMEM;
1663a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1664a58823acSDavid Howells 	if (!scb)
1665a58823acSDavid Howells 		goto error;
1666a58823acSDavid Howells 
1667260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1668260a9803SDavid Howells 	if (IS_ERR(key)) {
1669260a9803SDavid Howells 		ret = PTR_ERR(key);
1670a58823acSDavid Howells 		goto error_scb;
1671260a9803SDavid Howells 	}
1672260a9803SDavid Howells 
1673d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
167420b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1675a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1676a58823acSDavid Howells 
1677d2ddc776SDavid Howells 		if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1678d2ddc776SDavid Howells 			afs_end_vnode_operation(&fc);
1679bc1527dcSDavid Howells 			goto error_key;
1680d2ddc776SDavid Howells 		}
1681260a9803SDavid Howells 
1682d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
168368251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
168468251f0aSDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1685a58823acSDavid Howells 			afs_fs_link(&fc, vnode, dentry->d_name.name,
1686a58823acSDavid Howells 				    &scb[0], &scb[1]);
1687d2ddc776SDavid Howells 		}
1688d2ddc776SDavid Howells 
1689a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1690a58823acSDavid Howells 					&data_version, &scb[0]);
1691a58823acSDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1692a58823acSDavid Howells 					NULL, &scb[1]);
16937de9c6eeSAl Viro 		ihold(&vnode->vfs_inode);
16949dd0b82eSDavid Howells 		afs_update_dentry_version(&fc, dentry, &scb[0]);
1695260a9803SDavid Howells 		d_instantiate(dentry, &vnode->vfs_inode);
1696d2ddc776SDavid Howells 
1697d2ddc776SDavid Howells 		mutex_unlock(&vnode->io_lock);
1698d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1699d2ddc776SDavid Howells 		if (ret < 0)
1700d2ddc776SDavid Howells 			goto error_key;
17014433b691SDavid Howells 	} else {
17024433b691SDavid Howells 		goto error_key;
1703d2ddc776SDavid Howells 	}
1704d2ddc776SDavid Howells 
170563a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
170663a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
170763a4681fSDavid Howells 				 afs_edit_dir_for_link);
170863a4681fSDavid Howells 
1709260a9803SDavid Howells 	key_put(key);
1710a58823acSDavid Howells 	kfree(scb);
1711260a9803SDavid Howells 	_leave(" = 0");
1712260a9803SDavid Howells 	return 0;
1713260a9803SDavid Howells 
1714d2ddc776SDavid Howells error_key:
1715260a9803SDavid Howells 	key_put(key);
1716a58823acSDavid Howells error_scb:
1717a58823acSDavid Howells 	kfree(scb);
1718260a9803SDavid Howells error:
1719260a9803SDavid Howells 	d_drop(dentry);
1720260a9803SDavid Howells 	_leave(" = %d", ret);
1721260a9803SDavid Howells 	return ret;
1722260a9803SDavid Howells }
1723260a9803SDavid Howells 
1724260a9803SDavid Howells /*
1725260a9803SDavid Howells  * create a symlink in an AFS filesystem
1726260a9803SDavid Howells  */
1727260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
1728260a9803SDavid Howells 		       const char *content)
1729260a9803SDavid Howells {
1730b8359153SDavid Howells 	struct afs_iget_data iget_data;
1731d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1732a58823acSDavid Howells 	struct afs_status_cb *scb;
1733d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1734260a9803SDavid Howells 	struct key *key;
1735260a9803SDavid Howells 	int ret;
1736260a9803SDavid Howells 
17373b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%s",
1738a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry,
1739260a9803SDavid Howells 	       content);
1740260a9803SDavid Howells 
1741d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1742d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1743d2ddc776SDavid Howells 		goto error;
1744d2ddc776SDavid Howells 
1745260a9803SDavid Howells 	ret = -EINVAL;
174645222b9eSDavid Howells 	if (strlen(content) >= AFSPATHMAX)
1747260a9803SDavid Howells 		goto error;
1748260a9803SDavid Howells 
1749a58823acSDavid Howells 	ret = -ENOMEM;
1750a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1751a58823acSDavid Howells 	if (!scb)
1752a58823acSDavid Howells 		goto error;
1753a58823acSDavid Howells 
1754260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1755260a9803SDavid Howells 	if (IS_ERR(key)) {
1756260a9803SDavid Howells 		ret = PTR_ERR(key);
1757a58823acSDavid Howells 		goto error_scb;
1758260a9803SDavid Howells 	}
1759260a9803SDavid Howells 
1760d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
176120b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1762a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1763a58823acSDavid Howells 
1764d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
176568251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1766b8359153SDavid Howells 			afs_prep_for_new_inode(&fc, &iget_data);
1767a58823acSDavid Howells 			afs_fs_symlink(&fc, dentry->d_name.name, content,
1768b8359153SDavid Howells 				       &scb[0], &iget_data.fid, &scb[1]);
1769d2ddc776SDavid Howells 		}
1770d2ddc776SDavid Howells 
1771a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1772a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1773a58823acSDavid Howells 					&data_version, &scb[0]);
17749dd0b82eSDavid Howells 		afs_update_dentry_version(&fc, dentry, &scb[0]);
1775b8359153SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1776d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1777260a9803SDavid Howells 		if (ret < 0)
1778d2ddc776SDavid Howells 			goto error_key;
17794433b691SDavid Howells 	} else {
17804433b691SDavid Howells 		goto error_key;
1781260a9803SDavid Howells 	}
1782260a9803SDavid Howells 
178363a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1784b8359153SDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
178563a4681fSDavid Howells 				 afs_edit_dir_for_symlink);
178663a4681fSDavid Howells 
1787260a9803SDavid Howells 	key_put(key);
1788a58823acSDavid Howells 	kfree(scb);
1789260a9803SDavid Howells 	_leave(" = 0");
1790260a9803SDavid Howells 	return 0;
1791260a9803SDavid Howells 
1792d2ddc776SDavid Howells error_key:
1793260a9803SDavid Howells 	key_put(key);
1794a58823acSDavid Howells error_scb:
1795a58823acSDavid Howells 	kfree(scb);
1796260a9803SDavid Howells error:
1797260a9803SDavid Howells 	d_drop(dentry);
1798260a9803SDavid Howells 	_leave(" = %d", ret);
1799260a9803SDavid Howells 	return ret;
1800260a9803SDavid Howells }
1801260a9803SDavid Howells 
1802260a9803SDavid Howells /*
1803260a9803SDavid Howells  * rename a file in an AFS filesystem and/or move it between directories
1804260a9803SDavid Howells  */
1805260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
18061cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
18071cd66c93SMiklos Szeredi 		      unsigned int flags)
1808260a9803SDavid Howells {
1809d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1810a58823acSDavid Howells 	struct afs_status_cb *scb;
1811260a9803SDavid Howells 	struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
181279ddbfa5SDavid Howells 	struct dentry *tmp = NULL, *rehash = NULL;
181379ddbfa5SDavid Howells 	struct inode *new_inode;
1814260a9803SDavid Howells 	struct key *key;
181563a4681fSDavid Howells 	bool new_negative = d_is_negative(new_dentry);
1816260a9803SDavid Howells 	int ret;
1817260a9803SDavid Howells 
18181cd66c93SMiklos Szeredi 	if (flags)
18191cd66c93SMiklos Szeredi 		return -EINVAL;
18201cd66c93SMiklos Szeredi 
182179ddbfa5SDavid Howells 	/* Don't allow silly-rename files be moved around. */
182279ddbfa5SDavid Howells 	if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
182379ddbfa5SDavid Howells 		return -EINVAL;
182479ddbfa5SDavid Howells 
18252b0143b5SDavid Howells 	vnode = AFS_FS_I(d_inode(old_dentry));
1826260a9803SDavid Howells 	orig_dvnode = AFS_FS_I(old_dir);
1827260a9803SDavid Howells 	new_dvnode = AFS_FS_I(new_dir);
1828260a9803SDavid Howells 
18293b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1830260a9803SDavid Howells 	       orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1831260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1832260a9803SDavid Howells 	       new_dvnode->fid.vid, new_dvnode->fid.vnode,
1833a455589fSAl Viro 	       new_dentry);
1834260a9803SDavid Howells 
1835a58823acSDavid Howells 	ret = -ENOMEM;
1836a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1837a58823acSDavid Howells 	if (!scb)
1838a58823acSDavid Howells 		goto error;
1839a58823acSDavid Howells 
1840260a9803SDavid Howells 	key = afs_request_key(orig_dvnode->volume->cell);
1841260a9803SDavid Howells 	if (IS_ERR(key)) {
1842260a9803SDavid Howells 		ret = PTR_ERR(key);
1843a58823acSDavid Howells 		goto error_scb;
1844260a9803SDavid Howells 	}
1845260a9803SDavid Howells 
184679ddbfa5SDavid Howells 	/* For non-directories, check whether the target is busy and if so,
184779ddbfa5SDavid Howells 	 * make a copy of the dentry and then do a silly-rename.  If the
184879ddbfa5SDavid Howells 	 * silly-rename succeeds, the copied dentry is hashed and becomes the
184979ddbfa5SDavid Howells 	 * new target.
185079ddbfa5SDavid Howells 	 */
185179ddbfa5SDavid Howells 	if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
185279ddbfa5SDavid Howells 		/* To prevent any new references to the target during the
185379ddbfa5SDavid Howells 		 * rename, we unhash the dentry in advance.
185479ddbfa5SDavid Howells 		 */
185579ddbfa5SDavid Howells 		if (!d_unhashed(new_dentry)) {
185679ddbfa5SDavid Howells 			d_drop(new_dentry);
185779ddbfa5SDavid Howells 			rehash = new_dentry;
185879ddbfa5SDavid Howells 		}
185979ddbfa5SDavid Howells 
186079ddbfa5SDavid Howells 		if (d_count(new_dentry) > 2) {
186179ddbfa5SDavid Howells 			/* copy the target dentry's name */
186279ddbfa5SDavid Howells 			ret = -ENOMEM;
186379ddbfa5SDavid Howells 			tmp = d_alloc(new_dentry->d_parent,
186479ddbfa5SDavid Howells 				      &new_dentry->d_name);
186579ddbfa5SDavid Howells 			if (!tmp)
186679ddbfa5SDavid Howells 				goto error_rehash;
186779ddbfa5SDavid Howells 
186879ddbfa5SDavid Howells 			ret = afs_sillyrename(new_dvnode,
186979ddbfa5SDavid Howells 					      AFS_FS_I(d_inode(new_dentry)),
187079ddbfa5SDavid Howells 					      new_dentry, key);
187179ddbfa5SDavid Howells 			if (ret)
187279ddbfa5SDavid Howells 				goto error_rehash;
187379ddbfa5SDavid Howells 
187479ddbfa5SDavid Howells 			new_dentry = tmp;
187579ddbfa5SDavid Howells 			rehash = NULL;
187679ddbfa5SDavid Howells 			new_negative = true;
187779ddbfa5SDavid Howells 		}
187879ddbfa5SDavid Howells 	}
187979ddbfa5SDavid Howells 
18809dd0b82eSDavid Howells 	/* This bit is potentially nasty as there's a potential race with
18819dd0b82eSDavid Howells 	 * afs_d_revalidate{,_rcu}().  We have to change d_fsdata on the dentry
18829dd0b82eSDavid Howells 	 * to reflect it's new parent's new data_version after the op, but
18839dd0b82eSDavid Howells 	 * d_revalidate may see old_dentry between the op having taken place
18849dd0b82eSDavid Howells 	 * and the version being updated.
18859dd0b82eSDavid Howells 	 *
18869dd0b82eSDavid Howells 	 * So drop the old_dentry for now to make other threads go through
18879dd0b82eSDavid Howells 	 * lookup instead - which we hold a lock against.
18889dd0b82eSDavid Howells 	 */
18899dd0b82eSDavid Howells 	d_drop(old_dentry);
18909dd0b82eSDavid Howells 
1891d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
189220b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) {
1893a58823acSDavid Howells 		afs_dataversion_t orig_data_version;
1894a58823acSDavid Howells 		afs_dataversion_t new_data_version;
1895a58823acSDavid Howells 		struct afs_status_cb *new_scb = &scb[1];
1896a58823acSDavid Howells 
1897a58823acSDavid Howells 		orig_data_version = orig_dvnode->status.data_version + 1;
1898a58823acSDavid Howells 
1899d2ddc776SDavid Howells 		if (orig_dvnode != new_dvnode) {
1900d2ddc776SDavid Howells 			if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1901d2ddc776SDavid Howells 				afs_end_vnode_operation(&fc);
19029dd0b82eSDavid Howells 				goto error_rehash_old;
1903d2ddc776SDavid Howells 			}
190437c0bbb3SDavid Howells 			new_data_version = new_dvnode->status.data_version + 1;
1905a58823acSDavid Howells 		} else {
1906a58823acSDavid Howells 			new_data_version = orig_data_version;
1907a58823acSDavid Howells 			new_scb = &scb[0];
1908d2ddc776SDavid Howells 		}
1909a58823acSDavid Howells 
1910d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
191168251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
191268251f0aSDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
1913d2ddc776SDavid Howells 			afs_fs_rename(&fc, old_dentry->d_name.name,
191463a4681fSDavid Howells 				      new_dvnode, new_dentry->d_name.name,
1915a58823acSDavid Howells 				      &scb[0], new_scb);
1916d2ddc776SDavid Howells 		}
1917d2ddc776SDavid Howells 
1918a58823acSDavid Howells 		afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break,
1919a58823acSDavid Howells 					&orig_data_version, &scb[0]);
1920a58823acSDavid Howells 		if (new_dvnode != orig_dvnode) {
1921a58823acSDavid Howells 			afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2,
1922a58823acSDavid Howells 						&new_data_version, &scb[1]);
1923d2ddc776SDavid Howells 			mutex_unlock(&new_dvnode->io_lock);
1924a58823acSDavid Howells 		}
1925d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1926260a9803SDavid Howells 		if (ret < 0)
19279dd0b82eSDavid Howells 			goto error_rehash_old;
1928d2ddc776SDavid Howells 	}
1929d2ddc776SDavid Howells 
193063a4681fSDavid Howells 	if (ret == 0) {
193179ddbfa5SDavid Howells 		if (rehash)
193279ddbfa5SDavid Howells 			d_rehash(rehash);
193363a4681fSDavid Howells 		if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
193463a4681fSDavid Howells 		    afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
193579ddbfa5SDavid Howells 					afs_edit_dir_for_rename_0);
193663a4681fSDavid Howells 
193763a4681fSDavid Howells 		if (!new_negative &&
193863a4681fSDavid Howells 		    test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
193963a4681fSDavid Howells 			afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
194079ddbfa5SDavid Howells 					    afs_edit_dir_for_rename_1);
194163a4681fSDavid Howells 
194263a4681fSDavid Howells 		if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
194363a4681fSDavid Howells 			afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
194479ddbfa5SDavid Howells 					 &vnode->fid, afs_edit_dir_for_rename_2);
194579ddbfa5SDavid Howells 
194679ddbfa5SDavid Howells 		new_inode = d_inode(new_dentry);
194779ddbfa5SDavid Howells 		if (new_inode) {
194879ddbfa5SDavid Howells 			spin_lock(&new_inode->i_lock);
194979ddbfa5SDavid Howells 			if (new_inode->i_nlink > 0)
195079ddbfa5SDavid Howells 				drop_nlink(new_inode);
195179ddbfa5SDavid Howells 			spin_unlock(&new_inode->i_lock);
195279ddbfa5SDavid Howells 		}
19539dd0b82eSDavid Howells 
19549dd0b82eSDavid Howells 		/* Now we can update d_fsdata on the dentries to reflect their
19559dd0b82eSDavid Howells 		 * new parent's data_version.
19569dd0b82eSDavid Howells 		 *
19579dd0b82eSDavid Howells 		 * Note that if we ever implement RENAME_EXCHANGE, we'll have
19589dd0b82eSDavid Howells 		 * to update both dentries with opposing dir versions.
19599dd0b82eSDavid Howells 		 */
19609dd0b82eSDavid Howells 		if (new_dvnode != orig_dvnode) {
19619dd0b82eSDavid Howells 			afs_update_dentry_version(&fc, old_dentry, &scb[1]);
19629dd0b82eSDavid Howells 			afs_update_dentry_version(&fc, new_dentry, &scb[1]);
19639dd0b82eSDavid Howells 		} else {
19649dd0b82eSDavid Howells 			afs_update_dentry_version(&fc, old_dentry, &scb[0]);
19659dd0b82eSDavid Howells 			afs_update_dentry_version(&fc, new_dentry, &scb[0]);
19669dd0b82eSDavid Howells 		}
196779ddbfa5SDavid Howells 		d_move(old_dentry, new_dentry);
196879ddbfa5SDavid Howells 		goto error_tmp;
196963a4681fSDavid Howells 	}
197063a4681fSDavid Howells 
19719dd0b82eSDavid Howells error_rehash_old:
19729dd0b82eSDavid Howells 	d_rehash(new_dentry);
197379ddbfa5SDavid Howells error_rehash:
197479ddbfa5SDavid Howells 	if (rehash)
197579ddbfa5SDavid Howells 		d_rehash(rehash);
197679ddbfa5SDavid Howells error_tmp:
197779ddbfa5SDavid Howells 	if (tmp)
197879ddbfa5SDavid Howells 		dput(tmp);
1979260a9803SDavid Howells 	key_put(key);
1980a58823acSDavid Howells error_scb:
1981a58823acSDavid Howells 	kfree(scb);
1982260a9803SDavid Howells error:
1983260a9803SDavid Howells 	_leave(" = %d", ret);
1984260a9803SDavid Howells 	return ret;
1985260a9803SDavid Howells }
1986f3ddee8dSDavid Howells 
1987f3ddee8dSDavid Howells /*
1988f3ddee8dSDavid Howells  * Release a directory page and clean up its private state if it's not busy
1989f3ddee8dSDavid Howells  * - return true if the page can now be released, false if not
1990f3ddee8dSDavid Howells  */
1991f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1992f3ddee8dSDavid Howells {
1993f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1994f3ddee8dSDavid Howells 
19953b6492dfSDavid Howells 	_enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1996f3ddee8dSDavid Howells 
1997f3ddee8dSDavid Howells 	set_page_private(page, 0);
1998f3ddee8dSDavid Howells 	ClearPagePrivate(page);
1999f3ddee8dSDavid Howells 
2000f3ddee8dSDavid Howells 	/* The directory will need reloading. */
2001f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
2002f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_relpg);
2003f3ddee8dSDavid Howells 	return 1;
2004f3ddee8dSDavid Howells }
2005f3ddee8dSDavid Howells 
2006f3ddee8dSDavid Howells /*
2007f3ddee8dSDavid Howells  * invalidate part or all of a page
2008f3ddee8dSDavid Howells  * - release a page and clean up its private data if offset is 0 (indicating
2009f3ddee8dSDavid Howells  *   the entire page)
2010f3ddee8dSDavid Howells  */
2011f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
2012f3ddee8dSDavid Howells 				   unsigned int length)
2013f3ddee8dSDavid Howells {
2014f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
2015f3ddee8dSDavid Howells 
2016f3ddee8dSDavid Howells 	_enter("{%lu},%u,%u", page->index, offset, length);
2017f3ddee8dSDavid Howells 
2018f3ddee8dSDavid Howells 	BUG_ON(!PageLocked(page));
2019f3ddee8dSDavid Howells 
2020f3ddee8dSDavid Howells 	/* The directory will need reloading. */
2021f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
2022f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_inval);
2023f3ddee8dSDavid Howells 
2024f3ddee8dSDavid Howells 	/* we clean up only if the entire page is being invalidated */
2025f3ddee8dSDavid Howells 	if (offset == 0 && length == PAGE_SIZE) {
2026f3ddee8dSDavid Howells 		set_page_private(page, 0);
2027f3ddee8dSDavid Howells 		ClearPagePrivate(page);
2028f3ddee8dSDavid Howells 	}
2029f3ddee8dSDavid Howells }
2030