xref: /openbmc/linux/fs/afs/dir.c (revision b6489a49)
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;
1025cf9dd55SDavid Howells 	struct afs_fid		fids[50];
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * check that a directory page is valid
1071da177e4SLinus Torvalds  */
108f3ddee8dSDavid Howells static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
109f3ddee8dSDavid Howells 			       loff_t i_size)
1101da177e4SLinus Torvalds {
11100317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
112f3ddee8dSDavid Howells 	loff_t latter, off;
1131da177e4SLinus Torvalds 	int tmp, qty;
1141da177e4SLinus Torvalds 
115dab17c1aSDavid Howells 	/* Determine how many magic numbers there should be in this page, but
116dab17c1aSDavid Howells 	 * we must take care because the directory may change size under us.
117dab17c1aSDavid Howells 	 */
118dab17c1aSDavid Howells 	off = page_offset(page);
119dab17c1aSDavid Howells 	if (i_size <= off)
120dab17c1aSDavid Howells 		goto checked;
121dab17c1aSDavid Howells 
122dab17c1aSDavid Howells 	latter = i_size - off;
1231da177e4SLinus Torvalds 	if (latter >= PAGE_SIZE)
1241da177e4SLinus Torvalds 		qty = PAGE_SIZE;
1251da177e4SLinus Torvalds 	else
1261da177e4SLinus Torvalds 		qty = latter;
12700317636SDavid Howells 	qty /= sizeof(union afs_xdr_dir_block);
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	/* check them */
13063a4681fSDavid Howells 	dbuf = kmap(page);
1311da177e4SLinus Torvalds 	for (tmp = 0; tmp < qty; tmp++) {
13200317636SDavid Howells 		if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
133dab17c1aSDavid Howells 			printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
134f3ddee8dSDavid Howells 			       __func__, dvnode->vfs_inode.i_ino, tmp, qty,
13500317636SDavid Howells 			       ntohs(dbuf->blocks[tmp].hdr.magic));
136f3ddee8dSDavid Howells 			trace_afs_dir_check_failed(dvnode, off, i_size);
13763a4681fSDavid Howells 			kunmap(page);
138f51375cdSDavid Howells 			trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
1391da177e4SLinus Torvalds 			goto error;
1401da177e4SLinus Torvalds 		}
14163a4681fSDavid Howells 
14263a4681fSDavid Howells 		/* Make sure each block is NUL terminated so we can reasonably
14363a4681fSDavid Howells 		 * use string functions on it.  The filenames in the page
14463a4681fSDavid Howells 		 * *should* be NUL-terminated anyway.
14563a4681fSDavid Howells 		 */
14663a4681fSDavid Howells 		((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
1471da177e4SLinus Torvalds 	}
1481da177e4SLinus Torvalds 
14963a4681fSDavid Howells 	kunmap(page);
15063a4681fSDavid Howells 
151dab17c1aSDavid Howells checked:
152f3ddee8dSDavid Howells 	afs_stat_v(dvnode, n_read_dir);
153be5b82dbSAl Viro 	return true;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds error:
156be5b82dbSAl Viro 	return false;
157ec26815aSDavid Howells }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds /*
160445b1028SDavid Howells  * Check the contents of a directory that we've just read.
161445b1028SDavid Howells  */
162445b1028SDavid Howells static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
163445b1028SDavid Howells {
164445b1028SDavid Howells 	struct afs_xdr_dir_page *dbuf;
165445b1028SDavid Howells 	unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
166445b1028SDavid Howells 
167445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++)
168445b1028SDavid Howells 		if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
169445b1028SDavid Howells 			goto bad;
170445b1028SDavid Howells 	return true;
171445b1028SDavid Howells 
172445b1028SDavid Howells bad:
173445b1028SDavid Howells 	pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
174445b1028SDavid Howells 		dvnode->fid.vid, dvnode->fid.vnode,
175445b1028SDavid Howells 		req->file_size, req->len, req->actual_len, req->remain);
176445b1028SDavid Howells 	pr_warn("DIR %llx %x %x %x\n",
177445b1028SDavid Howells 		req->pos, req->index, req->nr_pages, req->offset);
178445b1028SDavid Howells 
179445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++) {
180445b1028SDavid Howells 		dbuf = kmap(req->pages[i]);
181445b1028SDavid Howells 		for (j = 0; j < qty; j++) {
182445b1028SDavid Howells 			union afs_xdr_dir_block *block = &dbuf->blocks[j];
183445b1028SDavid Howells 
184445b1028SDavid Howells 			pr_warn("[%02x] %32phN\n", i * qty + j, block);
185445b1028SDavid Howells 		}
186445b1028SDavid Howells 		kunmap(req->pages[i]);
187445b1028SDavid Howells 	}
188445b1028SDavid Howells 	return false;
189445b1028SDavid Howells }
190445b1028SDavid Howells 
191445b1028SDavid Howells /*
1921da177e4SLinus Torvalds  * open an AFS directory file
1931da177e4SLinus Torvalds  */
1941da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	_enter("{%lu}", inode->i_ino);
1971da177e4SLinus Torvalds 
19800317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
19900317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
2001da177e4SLinus Torvalds 
20108e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
2021da177e4SLinus Torvalds 		return -ENOENT;
2031da177e4SLinus Torvalds 
20400d3b7a4SDavid Howells 	return afs_open(inode, file);
205ec26815aSDavid Howells }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /*
208f3ddee8dSDavid Howells  * Read the directory into the pagecache in one go, scrubbing the previous
209f3ddee8dSDavid Howells  * contents.  The list of pages is returned, pinning them so that they don't
210f3ddee8dSDavid Howells  * get reclaimed during the iteration.
211f3ddee8dSDavid Howells  */
212f3ddee8dSDavid Howells static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
213b61f7dcfSDavid Howells 	__acquires(&dvnode->validate_lock)
214f3ddee8dSDavid Howells {
215f3ddee8dSDavid Howells 	struct afs_read *req;
216f3ddee8dSDavid Howells 	loff_t i_size;
217f3ddee8dSDavid Howells 	int nr_pages, nr_inline, i, n;
218f3ddee8dSDavid Howells 	int ret = -ENOMEM;
219f3ddee8dSDavid Howells 
220f3ddee8dSDavid Howells retry:
221f3ddee8dSDavid Howells 	i_size = i_size_read(&dvnode->vfs_inode);
222f3ddee8dSDavid Howells 	if (i_size < 2048)
223f51375cdSDavid Howells 		return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
224f51375cdSDavid Howells 	if (i_size > 2048 * 1024) {
225f51375cdSDavid Howells 		trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
226f3ddee8dSDavid Howells 		return ERR_PTR(-EFBIG);
227f51375cdSDavid Howells 	}
228f3ddee8dSDavid Howells 
229f3ddee8dSDavid Howells 	_enter("%llu", i_size);
230f3ddee8dSDavid Howells 
231f3ddee8dSDavid Howells 	/* Get a request record to hold the page list.  We want to hold it
232f3ddee8dSDavid Howells 	 * inline if we can, but we don't want to make an order 1 allocation.
233f3ddee8dSDavid Howells 	 */
234f3ddee8dSDavid Howells 	nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
235f3ddee8dSDavid Howells 	nr_inline = nr_pages;
236f3ddee8dSDavid Howells 	if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
237f3ddee8dSDavid Howells 		nr_inline = 0;
238f3ddee8dSDavid Howells 
239ee102584SZhengyuan Liu 	req = kzalloc(struct_size(req, array, nr_inline), GFP_KERNEL);
240f3ddee8dSDavid Howells 	if (!req)
241f3ddee8dSDavid Howells 		return ERR_PTR(-ENOMEM);
242f3ddee8dSDavid Howells 
243f3ddee8dSDavid Howells 	refcount_set(&req->usage, 1);
244f3ddee8dSDavid Howells 	req->nr_pages = nr_pages;
245f3ddee8dSDavid Howells 	req->actual_len = i_size; /* May change */
246f3ddee8dSDavid Howells 	req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
247f3ddee8dSDavid Howells 	req->data_version = dvnode->status.data_version; /* May change */
248f3ddee8dSDavid Howells 	if (nr_inline > 0) {
249f3ddee8dSDavid Howells 		req->pages = req->array;
250f3ddee8dSDavid Howells 	} else {
251f3ddee8dSDavid Howells 		req->pages = kcalloc(nr_pages, sizeof(struct page *),
252f3ddee8dSDavid Howells 				     GFP_KERNEL);
253f3ddee8dSDavid Howells 		if (!req->pages)
254f3ddee8dSDavid Howells 			goto error;
255f3ddee8dSDavid Howells 	}
256f3ddee8dSDavid Howells 
257f3ddee8dSDavid Howells 	/* Get a list of all the pages that hold or will hold the directory
258f3ddee8dSDavid Howells 	 * content.  We need to fill in any gaps that we might find where the
259f3ddee8dSDavid Howells 	 * memory reclaimer has been at work.  If there are any gaps, we will
260f3ddee8dSDavid Howells 	 * need to reread the entire directory contents.
261f3ddee8dSDavid Howells 	 */
262f3ddee8dSDavid Howells 	i = 0;
263f3ddee8dSDavid Howells 	do {
264f3ddee8dSDavid Howells 		n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
265f3ddee8dSDavid Howells 					  req->nr_pages - i,
266f3ddee8dSDavid Howells 					  req->pages + i);
267f3ddee8dSDavid Howells 		_debug("find %u at %u/%u", n, i, req->nr_pages);
268f3ddee8dSDavid Howells 		if (n == 0) {
269f3ddee8dSDavid Howells 			gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
270f3ddee8dSDavid Howells 
271f3ddee8dSDavid Howells 			if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
272f3ddee8dSDavid Howells 				afs_stat_v(dvnode, n_inval);
273f3ddee8dSDavid Howells 
274f3ddee8dSDavid Howells 			ret = -ENOMEM;
275f3ddee8dSDavid Howells 			req->pages[i] = __page_cache_alloc(gfp);
276f3ddee8dSDavid Howells 			if (!req->pages[i])
277f3ddee8dSDavid Howells 				goto error;
278f3ddee8dSDavid Howells 			ret = add_to_page_cache_lru(req->pages[i],
279f3ddee8dSDavid Howells 						    dvnode->vfs_inode.i_mapping,
280f3ddee8dSDavid Howells 						    i, gfp);
281f3ddee8dSDavid Howells 			if (ret < 0)
282f3ddee8dSDavid Howells 				goto error;
283f3ddee8dSDavid Howells 
284f3ddee8dSDavid Howells 			set_page_private(req->pages[i], 1);
285f3ddee8dSDavid Howells 			SetPagePrivate(req->pages[i]);
286f3ddee8dSDavid Howells 			unlock_page(req->pages[i]);
287f3ddee8dSDavid Howells 			i++;
288f3ddee8dSDavid Howells 		} else {
289f3ddee8dSDavid Howells 			i += n;
290f3ddee8dSDavid Howells 		}
291f3ddee8dSDavid Howells 	} while (i < req->nr_pages);
292f3ddee8dSDavid Howells 
293f3ddee8dSDavid Howells 	/* If we're going to reload, we need to lock all the pages to prevent
294f3ddee8dSDavid Howells 	 * races.
295f3ddee8dSDavid Howells 	 */
296f3ddee8dSDavid Howells 	ret = -ERESTARTSYS;
297b61f7dcfSDavid Howells 	if (down_read_killable(&dvnode->validate_lock) < 0)
298b61f7dcfSDavid Howells 		goto error;
299f3ddee8dSDavid Howells 
300f3ddee8dSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
301f3ddee8dSDavid Howells 		goto success;
302f3ddee8dSDavid Howells 
303b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
304b61f7dcfSDavid Howells 	if (down_write_killable(&dvnode->validate_lock) < 0)
305b61f7dcfSDavid Howells 		goto error;
306b61f7dcfSDavid Howells 
307b61f7dcfSDavid Howells 	if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
30899987c56SDavid Howells 		trace_afs_reload_dir(dvnode);
309f3ddee8dSDavid Howells 		ret = afs_fetch_data(dvnode, key, req);
310f3ddee8dSDavid Howells 		if (ret < 0)
311b61f7dcfSDavid Howells 			goto error_unlock;
312f3ddee8dSDavid Howells 
313f3ddee8dSDavid Howells 		task_io_account_read(PAGE_SIZE * req->nr_pages);
314f3ddee8dSDavid Howells 
315f3ddee8dSDavid Howells 		if (req->len < req->file_size)
316f3ddee8dSDavid Howells 			goto content_has_grown;
317f3ddee8dSDavid Howells 
318f3ddee8dSDavid Howells 		/* Validate the data we just read. */
319f3ddee8dSDavid Howells 		ret = -EIO;
320445b1028SDavid Howells 		if (!afs_dir_check_pages(dvnode, req))
321b61f7dcfSDavid Howells 			goto error_unlock;
322f3ddee8dSDavid Howells 
323f3ddee8dSDavid Howells 		// TODO: Trim excess pages
324f3ddee8dSDavid Howells 
325f3ddee8dSDavid Howells 		set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
326f3ddee8dSDavid Howells 	}
327f3ddee8dSDavid Howells 
328b61f7dcfSDavid Howells 	downgrade_write(&dvnode->validate_lock);
329f3ddee8dSDavid Howells success:
330f3ddee8dSDavid Howells 	return req;
331f3ddee8dSDavid Howells 
332f3ddee8dSDavid Howells error_unlock:
333b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
334f3ddee8dSDavid Howells error:
335f3ddee8dSDavid Howells 	afs_put_read(req);
336f3ddee8dSDavid Howells 	_leave(" = %d", ret);
337f3ddee8dSDavid Howells 	return ERR_PTR(ret);
338f3ddee8dSDavid Howells 
339f3ddee8dSDavid Howells content_has_grown:
340b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
341f3ddee8dSDavid Howells 	afs_put_read(req);
342f3ddee8dSDavid Howells 	goto retry;
343f3ddee8dSDavid Howells }
344f3ddee8dSDavid Howells 
345f3ddee8dSDavid Howells /*
3461da177e4SLinus Torvalds  * deal with one block in an AFS directory
3471da177e4SLinus Torvalds  */
348f51375cdSDavid Howells static int afs_dir_iterate_block(struct afs_vnode *dvnode,
349f51375cdSDavid Howells 				 struct dir_context *ctx,
35000317636SDavid Howells 				 union afs_xdr_dir_block *block,
3511bbae9f8SAl Viro 				 unsigned blkoff)
3521da177e4SLinus Torvalds {
35300317636SDavid Howells 	union afs_xdr_dirent *dire;
3541da177e4SLinus Torvalds 	unsigned offset, next, curr;
3551da177e4SLinus Torvalds 	size_t nlen;
3561bbae9f8SAl Viro 	int tmp;
3571da177e4SLinus Torvalds 
3581bbae9f8SAl Viro 	_enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
3591da177e4SLinus Torvalds 
36000317636SDavid Howells 	curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	/* walk through the block, an entry at a time */
3634ea219a8SDavid Howells 	for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
3644ea219a8SDavid Howells 	     offset < AFS_DIR_SLOTS_PER_BLOCK;
3651da177e4SLinus Torvalds 	     offset = next
3661da177e4SLinus Torvalds 	     ) {
3671da177e4SLinus Torvalds 		next = offset + 1;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 		/* skip entries marked unused in the bitmap */
37000317636SDavid Howells 		if (!(block->hdr.bitmap[offset / 8] &
3711da177e4SLinus Torvalds 		      (1 << (offset % 8)))) {
3725b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: unused",
37300317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block), offset);
3741da177e4SLinus Torvalds 			if (offset >= curr)
3751bbae9f8SAl Viro 				ctx->pos = blkoff +
37600317636SDavid Howells 					next * sizeof(union afs_xdr_dirent);
3771da177e4SLinus Torvalds 			continue;
3781da177e4SLinus Torvalds 		}
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 		/* got a valid entry */
3811da177e4SLinus Torvalds 		dire = &block->dirents[offset];
3821da177e4SLinus Torvalds 		nlen = strnlen(dire->u.name,
3831da177e4SLinus Torvalds 			       sizeof(*block) -
38400317636SDavid Howells 			       offset * sizeof(union afs_xdr_dirent));
3851da177e4SLinus Torvalds 
3865b5e0928SAlexey Dobriyan 		_debug("ENT[%zu.%u]: %s %zu \"%s\"",
38700317636SDavid Howells 		       blkoff / sizeof(union afs_xdr_dir_block), offset,
3881da177e4SLinus Torvalds 		       (offset < curr ? "skip" : "fill"),
3891da177e4SLinus Torvalds 		       nlen, dire->u.name);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 		/* work out where the next possible entry is */
39200317636SDavid Howells 		for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
3934ea219a8SDavid Howells 			if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
3945b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
3951da177e4SLinus Torvalds 				       " %u travelled beyond end dir block"
3965b5e0928SAlexey Dobriyan 				       " (len %u/%zu)",
39700317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
3981da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
399f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_over_end);
4001da177e4SLinus Torvalds 			}
40100317636SDavid Howells 			if (!(block->hdr.bitmap[next / 8] &
4021da177e4SLinus Torvalds 			      (1 << (next % 8)))) {
4035b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
4045b5e0928SAlexey Dobriyan 				       " %u unmarked extension (len %u/%zu)",
40500317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4061da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
407f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
4081da177e4SLinus Torvalds 			}
4091da177e4SLinus Torvalds 
4105b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: ext %u/%zu",
41100317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block),
4121da177e4SLinus Torvalds 			       next, tmp, nlen);
4131da177e4SLinus Torvalds 			next++;
4141da177e4SLinus Torvalds 		}
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 		/* skip if starts before the current position */
4171da177e4SLinus Torvalds 		if (offset < curr)
4181da177e4SLinus Torvalds 			continue;
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 		/* found the next entry */
4211bbae9f8SAl Viro 		if (!dir_emit(ctx, dire->u.name, nlen,
4221da177e4SLinus Torvalds 			      ntohl(dire->u.vnode),
4235cf9dd55SDavid Howells 			      (ctx->actor == afs_lookup_filldir ||
4245cf9dd55SDavid Howells 			       ctx->actor == afs_lookup_one_filldir)?
4251bbae9f8SAl Viro 			      ntohl(dire->u.unique) : DT_UNKNOWN)) {
4261da177e4SLinus Torvalds 			_leave(" = 0 [full]");
4271da177e4SLinus Torvalds 			return 0;
4281da177e4SLinus Torvalds 		}
4291da177e4SLinus Torvalds 
43000317636SDavid Howells 		ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	_leave(" = 1 [more]");
4341da177e4SLinus Torvalds 	return 1;
435ec26815aSDavid Howells }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds /*
43808e0e7c8SDavid Howells  * iterate through the data blob that lists the contents of an AFS directory
4391da177e4SLinus Torvalds  */
4401bbae9f8SAl Viro static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
4419dd0b82eSDavid Howells 			   struct key *key, afs_dataversion_t *_dir_version)
4421da177e4SLinus Torvalds {
443f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
44400317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
44500317636SDavid Howells 	union afs_xdr_dir_block *dblock;
446f3ddee8dSDavid Howells 	struct afs_read *req;
4471da177e4SLinus Torvalds 	struct page *page;
4481da177e4SLinus Torvalds 	unsigned blkoff, limit;
4491da177e4SLinus Torvalds 	int ret;
4501da177e4SLinus Torvalds 
4511bbae9f8SAl Viro 	_enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
4521da177e4SLinus Torvalds 
45308e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
4541da177e4SLinus Torvalds 		_leave(" = -ESTALE");
4551da177e4SLinus Torvalds 		return -ESTALE;
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds 
458f3ddee8dSDavid Howells 	req = afs_read_dir(dvnode, key);
459f3ddee8dSDavid Howells 	if (IS_ERR(req))
460f3ddee8dSDavid Howells 		return PTR_ERR(req);
4619dd0b82eSDavid Howells 	*_dir_version = req->data_version;
462f3ddee8dSDavid Howells 
4631da177e4SLinus Torvalds 	/* round the file position up to the next entry boundary */
46400317636SDavid Howells 	ctx->pos += sizeof(union afs_xdr_dirent) - 1;
46500317636SDavid Howells 	ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	/* walk through the blocks in sequence */
4681da177e4SLinus Torvalds 	ret = 0;
469f3ddee8dSDavid Howells 	while (ctx->pos < req->actual_len) {
47000317636SDavid Howells 		blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
4711da177e4SLinus Torvalds 
472f3ddee8dSDavid Howells 		/* Fetch the appropriate page from the directory and re-add it
473f3ddee8dSDavid Howells 		 * to the LRU.
474f3ddee8dSDavid Howells 		 */
475f3ddee8dSDavid Howells 		page = req->pages[blkoff / PAGE_SIZE];
476f3ddee8dSDavid Howells 		if (!page) {
477f51375cdSDavid Howells 			ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
4781da177e4SLinus Torvalds 			break;
4791da177e4SLinus Torvalds 		}
480f3ddee8dSDavid Howells 		mark_page_accessed(page);
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 		limit = blkoff & ~(PAGE_SIZE - 1);
4831da177e4SLinus Torvalds 
484f3ddee8dSDavid Howells 		dbuf = kmap(page);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 		/* deal with the individual blocks stashed on this page */
4871da177e4SLinus Torvalds 		do {
4881da177e4SLinus Torvalds 			dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
48900317636SDavid Howells 					       sizeof(union afs_xdr_dir_block)];
490f51375cdSDavid Howells 			ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
4911da177e4SLinus Torvalds 			if (ret != 1) {
492f3ddee8dSDavid Howells 				kunmap(page);
4931da177e4SLinus Torvalds 				goto out;
4941da177e4SLinus Torvalds 			}
4951da177e4SLinus Torvalds 
49600317636SDavid Howells 			blkoff += sizeof(union afs_xdr_dir_block);
4971da177e4SLinus Torvalds 
4981bbae9f8SAl Viro 		} while (ctx->pos < dir->i_size && blkoff < limit);
4991da177e4SLinus Torvalds 
500f3ddee8dSDavid Howells 		kunmap(page);
5011da177e4SLinus Torvalds 		ret = 0;
5021da177e4SLinus Torvalds 	}
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds out:
505b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
506f3ddee8dSDavid Howells 	afs_put_read(req);
5071da177e4SLinus Torvalds 	_leave(" = %d", ret);
5081da177e4SLinus Torvalds 	return ret;
509ec26815aSDavid Howells }
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds /*
5121da177e4SLinus Torvalds  * read an AFS directory
5131da177e4SLinus Torvalds  */
5141bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx)
5151da177e4SLinus Torvalds {
5169dd0b82eSDavid Howells 	afs_dataversion_t dir_version;
5179dd0b82eSDavid Howells 
5189dd0b82eSDavid Howells 	return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file),
5199dd0b82eSDavid Howells 			       &dir_version);
520ec26815aSDavid Howells }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds /*
5235cf9dd55SDavid Howells  * Search the directory for a single name
5241da177e4SLinus Torvalds  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5251da177e4SLinus Torvalds  *   uniquifier through dtype
5261da177e4SLinus Torvalds  */
5275cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
528ac7576f4SMiklos Szeredi 				  int nlen, loff_t fpos, u64 ino, unsigned dtype)
5291da177e4SLinus Torvalds {
5305cf9dd55SDavid Howells 	struct afs_lookup_one_cookie *cookie =
5315cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_one_cookie, ctx);
5321da177e4SLinus Torvalds 
5331bbae9f8SAl Viro 	_enter("{%s,%u},%s,%u,,%llu,%u",
5341bbae9f8SAl Viro 	       cookie->name.name, cookie->name.len, name, nlen,
535ba3e0e1aSDavid S. Miller 	       (unsigned long long) ino, dtype);
5361da177e4SLinus Torvalds 
53708e0e7c8SDavid Howells 	/* insanity checks first */
53800317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
53900317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
54008e0e7c8SDavid Howells 
5411bbae9f8SAl Viro 	if (cookie->name.len != nlen ||
5421bbae9f8SAl Viro 	    memcmp(cookie->name.name, name, nlen) != 0) {
5431da177e4SLinus Torvalds 		_leave(" = 0 [no]");
5441da177e4SLinus Torvalds 		return 0;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	cookie->fid.vnode = ino;
5481da177e4SLinus Torvalds 	cookie->fid.unique = dtype;
5491da177e4SLinus Torvalds 	cookie->found = 1;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	_leave(" = -1 [found]");
5521da177e4SLinus Torvalds 	return -1;
553ec26815aSDavid Howells }
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds /*
5565cf9dd55SDavid Howells  * Do a lookup of a single name in a directory
557260a9803SDavid Howells  * - just returns the FID the dentry name maps to if found
5581da177e4SLinus Torvalds  */
5595cf9dd55SDavid Howells static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
5609dd0b82eSDavid Howells 			     struct afs_fid *fid, struct key *key,
5619dd0b82eSDavid Howells 			     afs_dataversion_t *_dir_version)
5621da177e4SLinus Torvalds {
5631bbae9f8SAl Viro 	struct afs_super_info *as = dir->i_sb->s_fs_info;
5645cf9dd55SDavid Howells 	struct afs_lookup_one_cookie cookie = {
5655cf9dd55SDavid Howells 		.ctx.actor = afs_lookup_one_filldir,
5661bbae9f8SAl Viro 		.name = dentry->d_name,
5671bbae9f8SAl Viro 		.fid.vid = as->volume->vid
5681bbae9f8SAl Viro 	};
5691da177e4SLinus Torvalds 	int ret;
5701da177e4SLinus Torvalds 
571a455589fSAl Viro 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	/* search the directory */
5749dd0b82eSDavid Howells 	ret = afs_dir_iterate(dir, &cookie.ctx, key, _dir_version);
5751da177e4SLinus Torvalds 	if (ret < 0) {
57608e0e7c8SDavid Howells 		_leave(" = %d [iter]", ret);
57708e0e7c8SDavid Howells 		return ret;
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	ret = -ENOENT;
5811da177e4SLinus Torvalds 	if (!cookie.found) {
58208e0e7c8SDavid Howells 		_leave(" = -ENOENT [not found]");
58308e0e7c8SDavid Howells 		return -ENOENT;
58408e0e7c8SDavid Howells 	}
58508e0e7c8SDavid Howells 
58608e0e7c8SDavid Howells 	*fid = cookie.fid;
5873b6492dfSDavid Howells 	_leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
58808e0e7c8SDavid Howells 	return 0;
58908e0e7c8SDavid Howells }
59008e0e7c8SDavid Howells 
59108e0e7c8SDavid Howells /*
5925cf9dd55SDavid Howells  * search the directory for a name
5935cf9dd55SDavid Howells  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5945cf9dd55SDavid Howells  *   uniquifier through dtype
5955cf9dd55SDavid Howells  */
5965cf9dd55SDavid Howells static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
5975cf9dd55SDavid Howells 			      int nlen, loff_t fpos, u64 ino, unsigned dtype)
5985cf9dd55SDavid Howells {
5995cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie =
6005cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_cookie, ctx);
6015cf9dd55SDavid Howells 	int ret;
6025cf9dd55SDavid Howells 
6035cf9dd55SDavid Howells 	_enter("{%s,%u},%s,%u,,%llu,%u",
6045cf9dd55SDavid Howells 	       cookie->name.name, cookie->name.len, name, nlen,
6055cf9dd55SDavid Howells 	       (unsigned long long) ino, dtype);
6065cf9dd55SDavid Howells 
6075cf9dd55SDavid Howells 	/* insanity checks first */
60800317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
60900317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
6105cf9dd55SDavid Howells 
6115cf9dd55SDavid Howells 	if (cookie->found) {
6125cf9dd55SDavid Howells 		if (cookie->nr_fids < 50) {
6135cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].vnode	= ino;
6145cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].unique	= dtype;
6155cf9dd55SDavid Howells 			cookie->nr_fids++;
6165cf9dd55SDavid Howells 		}
6175cf9dd55SDavid Howells 	} else if (cookie->name.len == nlen &&
6185cf9dd55SDavid Howells 		   memcmp(cookie->name.name, name, nlen) == 0) {
619e49c7b2fSDavid Howells 		cookie->fids[1].vnode	= ino;
620e49c7b2fSDavid Howells 		cookie->fids[1].unique	= dtype;
6215cf9dd55SDavid Howells 		cookie->found = 1;
6225cf9dd55SDavid Howells 		if (cookie->one_only)
6235cf9dd55SDavid Howells 			return -1;
6245cf9dd55SDavid Howells 	}
6255cf9dd55SDavid Howells 
6265cf9dd55SDavid Howells 	ret = cookie->nr_fids >= 50 ? -1 : 0;
6275cf9dd55SDavid Howells 	_leave(" = %d", ret);
6285cf9dd55SDavid Howells 	return ret;
6295cf9dd55SDavid Howells }
6305cf9dd55SDavid Howells 
6315cf9dd55SDavid Howells /*
632e49c7b2fSDavid Howells  * Deal with the result of a successful lookup operation.  Turn all the files
633e49c7b2fSDavid Howells  * into inodes and save the first one - which is the one we actually want.
634e49c7b2fSDavid Howells  */
635e49c7b2fSDavid Howells static void afs_do_lookup_success(struct afs_operation *op)
636e49c7b2fSDavid Howells {
637e49c7b2fSDavid Howells 	struct afs_vnode_param *vp;
638e49c7b2fSDavid Howells 	struct afs_vnode *vnode;
639e49c7b2fSDavid Howells 	struct inode *inode;
640e49c7b2fSDavid Howells 	u32 abort_code;
641e49c7b2fSDavid Howells 	int i;
642e49c7b2fSDavid Howells 
643e49c7b2fSDavid Howells 	_enter("");
644e49c7b2fSDavid Howells 
645e49c7b2fSDavid Howells 	for (i = 0; i < op->nr_files; i++) {
646e49c7b2fSDavid Howells 		switch (i) {
647e49c7b2fSDavid Howells 		case 0:
648e49c7b2fSDavid Howells 			vp = &op->file[0];
649e49c7b2fSDavid Howells 			abort_code = vp->scb.status.abort_code;
650e49c7b2fSDavid Howells 			if (abort_code != 0) {
65144767c35SDavid Howells 				op->ac.abort_code = abort_code;
652e49c7b2fSDavid Howells 				op->error = afs_abort_to_error(abort_code);
653e49c7b2fSDavid Howells 			}
654e49c7b2fSDavid Howells 			break;
655e49c7b2fSDavid Howells 
656e49c7b2fSDavid Howells 		case 1:
657e49c7b2fSDavid Howells 			vp = &op->file[1];
658e49c7b2fSDavid Howells 			break;
659e49c7b2fSDavid Howells 
660e49c7b2fSDavid Howells 		default:
661e49c7b2fSDavid Howells 			vp = &op->more_files[i - 2];
662e49c7b2fSDavid Howells 			break;
663e49c7b2fSDavid Howells 		}
664e49c7b2fSDavid Howells 
665e49c7b2fSDavid Howells 		if (!vp->scb.have_status && !vp->scb.have_error)
666e49c7b2fSDavid Howells 			continue;
667e49c7b2fSDavid Howells 
668e49c7b2fSDavid Howells 		_debug("do [%u]", i);
669e49c7b2fSDavid Howells 		if (vp->vnode) {
670e49c7b2fSDavid Howells 			if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags))
671e49c7b2fSDavid Howells 				afs_vnode_commit_status(op, vp);
672e49c7b2fSDavid Howells 		} else if (vp->scb.status.abort_code == 0) {
673e49c7b2fSDavid Howells 			inode = afs_iget(op, vp);
674e49c7b2fSDavid Howells 			if (!IS_ERR(inode)) {
675e49c7b2fSDavid Howells 				vnode = AFS_FS_I(inode);
676e49c7b2fSDavid Howells 				afs_cache_permit(vnode, op->key,
677e49c7b2fSDavid Howells 						 0 /* Assume vnode->cb_break is 0 */ +
678e49c7b2fSDavid Howells 						 op->cb_v_break,
679e49c7b2fSDavid Howells 						 &vp->scb);
680e49c7b2fSDavid Howells 				vp->vnode = vnode;
681e49c7b2fSDavid Howells 				vp->put_vnode = true;
682e49c7b2fSDavid Howells 			}
683e49c7b2fSDavid Howells 		} else {
684e49c7b2fSDavid Howells 			_debug("- abort %d %llx:%llx.%x",
685e49c7b2fSDavid Howells 			       vp->scb.status.abort_code,
686e49c7b2fSDavid Howells 			       vp->fid.vid, vp->fid.vnode, vp->fid.unique);
687e49c7b2fSDavid Howells 		}
688e49c7b2fSDavid Howells 	}
689e49c7b2fSDavid Howells 
690e49c7b2fSDavid Howells 	_leave("");
691e49c7b2fSDavid Howells }
692e49c7b2fSDavid Howells 
693e49c7b2fSDavid Howells static const struct afs_operation_ops afs_inline_bulk_status_operation = {
694e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_inline_bulk_status,
695e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_inline_bulk_status,
696e49c7b2fSDavid Howells 	.success	= afs_do_lookup_success,
697e49c7b2fSDavid Howells };
698e49c7b2fSDavid Howells 
699b6489a49SDavid Howells static const struct afs_operation_ops afs_lookup_fetch_status_operation = {
700e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_fetch_status,
701e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_fetch_status,
702e49c7b2fSDavid Howells 	.success	= afs_do_lookup_success,
703728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
704e49c7b2fSDavid Howells };
705e49c7b2fSDavid Howells 
706e49c7b2fSDavid Howells /*
70720325960SDavid Howells  * See if we know that the server we expect to use doesn't support
70820325960SDavid Howells  * FS.InlineBulkStatus.
70920325960SDavid Howells  */
71020325960SDavid Howells static bool afs_server_supports_ibulk(struct afs_vnode *dvnode)
71120325960SDavid Howells {
71220325960SDavid Howells 	struct afs_server_list *slist;
71320325960SDavid Howells 	struct afs_volume *volume = dvnode->volume;
71420325960SDavid Howells 	struct afs_server *server;
71520325960SDavid Howells 	bool ret = true;
71620325960SDavid Howells 	int i;
71720325960SDavid Howells 
71820325960SDavid Howells 	if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags))
71920325960SDavid Howells 		return true;
72020325960SDavid Howells 
72120325960SDavid Howells 	rcu_read_lock();
72220325960SDavid Howells 	slist = rcu_dereference(volume->servers);
72320325960SDavid Howells 
72420325960SDavid Howells 	for (i = 0; i < slist->nr_servers; i++) {
72520325960SDavid Howells 		server = slist->servers[i].server;
72620325960SDavid Howells 		if (server == dvnode->cb_server) {
72720325960SDavid Howells 			if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
72820325960SDavid Howells 				ret = false;
72920325960SDavid Howells 			break;
73020325960SDavid Howells 		}
73120325960SDavid Howells 	}
73220325960SDavid Howells 
73320325960SDavid Howells 	rcu_read_unlock();
73420325960SDavid Howells 	return ret;
73520325960SDavid Howells }
73620325960SDavid Howells 
73720325960SDavid Howells /*
7385cf9dd55SDavid Howells  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
7395cf9dd55SDavid Howells  * files in one go and create inodes for them.  The inode of the file we were
7405cf9dd55SDavid Howells  * asked for is returned.
7415cf9dd55SDavid Howells  */
7425cf9dd55SDavid Howells static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
7435cf9dd55SDavid Howells 				   struct key *key)
7445cf9dd55SDavid Howells {
7455cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie;
746e49c7b2fSDavid Howells 	struct afs_vnode_param *vp;
747e49c7b2fSDavid Howells 	struct afs_operation *op;
74839db9815SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
74939db9815SDavid Howells 	struct inode *inode = NULL, *ti;
7509dd0b82eSDavid Howells 	afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version);
751e49c7b2fSDavid Howells 	long ret;
752e49c7b2fSDavid Howells 	int i;
7535cf9dd55SDavid Howells 
7545cf9dd55SDavid Howells 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
7555cf9dd55SDavid Howells 
7565cf9dd55SDavid Howells 	cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
7575cf9dd55SDavid Howells 	if (!cookie)
7585cf9dd55SDavid Howells 		return ERR_PTR(-ENOMEM);
7595cf9dd55SDavid Howells 
760e49c7b2fSDavid Howells 	for (i = 0; i < ARRAY_SIZE(cookie->fids); i++)
761e49c7b2fSDavid Howells 		cookie->fids[i].vid = dvnode->fid.vid;
7625cf9dd55SDavid Howells 	cookie->ctx.actor = afs_lookup_filldir;
7635cf9dd55SDavid Howells 	cookie->name = dentry->d_name;
76413fcc635SDavid Howells 	cookie->nr_fids = 2; /* slot 0 is saved for the fid we actually want
76513fcc635SDavid Howells 			      * and slot 1 for the directory */
7665cf9dd55SDavid Howells 
76720325960SDavid Howells 	if (!afs_server_supports_ibulk(dvnode))
7685cf9dd55SDavid Howells 		cookie->one_only = true;
7695cf9dd55SDavid Howells 
7705cf9dd55SDavid Howells 	/* search the directory */
7719dd0b82eSDavid Howells 	ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version);
772e49c7b2fSDavid Howells 	if (ret < 0)
7735cf9dd55SDavid Howells 		goto out;
7745cf9dd55SDavid Howells 
7759dd0b82eSDavid Howells 	dentry->d_fsdata = (void *)(unsigned long)data_version;
7769dd0b82eSDavid Howells 
777e49c7b2fSDavid Howells 	ret = -ENOENT;
7785cf9dd55SDavid Howells 	if (!cookie->found)
7795cf9dd55SDavid Howells 		goto out;
7805cf9dd55SDavid Howells 
7815cf9dd55SDavid Howells 	/* Check to see if we already have an inode for the primary fid. */
782e49c7b2fSDavid Howells 	inode = ilookup5(dir->i_sb, cookie->fids[1].vnode,
783e49c7b2fSDavid Howells 			 afs_ilookup5_test_by_fid, &cookie->fids[1]);
7845cf9dd55SDavid Howells 	if (inode)
785e49c7b2fSDavid Howells 		goto out; /* We do */
786e49c7b2fSDavid Howells 
787e49c7b2fSDavid Howells 	/* Okay, we didn't find it.  We need to query the server - and whilst
788e49c7b2fSDavid Howells 	 * we're doing that, we're going to attempt to look up a bunch of other
789e49c7b2fSDavid Howells 	 * vnodes also.
790e49c7b2fSDavid Howells 	 */
791e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
792e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
793e49c7b2fSDavid Howells 		ret = PTR_ERR(op);
7945cf9dd55SDavid Howells 		goto out;
795e49c7b2fSDavid Howells 	}
796e49c7b2fSDavid Howells 
797e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
798e49c7b2fSDavid Howells 	afs_op_set_fid(op, 1, &cookie->fids[1]);
799e49c7b2fSDavid Howells 
800e49c7b2fSDavid Howells 	op->nr_files = cookie->nr_fids;
801e49c7b2fSDavid Howells 	_debug("nr_files %u", op->nr_files);
8025cf9dd55SDavid Howells 
8035cf9dd55SDavid Howells 	/* Need space for examining all the selected files */
804e49c7b2fSDavid Howells 	op->error = -ENOMEM;
805e49c7b2fSDavid Howells 	if (op->nr_files > 2) {
806e49c7b2fSDavid Howells 		op->more_files = kvcalloc(op->nr_files - 2,
807e49c7b2fSDavid Howells 					  sizeof(struct afs_vnode_param),
8085cf9dd55SDavid Howells 					  GFP_KERNEL);
809e49c7b2fSDavid Howells 		if (!op->more_files)
810e49c7b2fSDavid Howells 			goto out_op;
8115cf9dd55SDavid Howells 
812e49c7b2fSDavid Howells 		for (i = 2; i < op->nr_files; i++) {
813e49c7b2fSDavid Howells 			vp = &op->more_files[i - 2];
814e49c7b2fSDavid Howells 			vp->fid = cookie->fids[i];
81539db9815SDavid Howells 
81639db9815SDavid Howells 			/* Find any inodes that already exist and get their
81739db9815SDavid Howells 			 * callback counters.
81839db9815SDavid Howells 			 */
819e49c7b2fSDavid Howells 			ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode,
820e49c7b2fSDavid Howells 					     afs_ilookup5_test_by_fid, &vp->fid);
82139db9815SDavid Howells 			if (!IS_ERR_OR_NULL(ti)) {
82239db9815SDavid Howells 				vnode = AFS_FS_I(ti);
823e49c7b2fSDavid Howells 				vp->dv_before = vnode->status.data_version;
824e49c7b2fSDavid Howells 				vp->cb_break_before = afs_calc_vnode_cb_break(vnode);
825e49c7b2fSDavid Howells 				vp->vnode = vnode;
826e49c7b2fSDavid Howells 				vp->put_vnode = true;
827e49c7b2fSDavid Howells 			}
82839db9815SDavid Howells 		}
82939db9815SDavid Howells 	}
83039db9815SDavid Howells 
8315cf9dd55SDavid Howells 	/* Try FS.InlineBulkStatus first.  Abort codes for the individual
8325cf9dd55SDavid Howells 	 * lookups contained therein are stored in the reply without aborting
8335cf9dd55SDavid Howells 	 * the whole operation.
8345cf9dd55SDavid Howells 	 */
835e49c7b2fSDavid Howells 	op->error = -ENOTSUPP;
836e49c7b2fSDavid Howells 	if (!cookie->one_only) {
837e49c7b2fSDavid Howells 		op->ops = &afs_inline_bulk_status_operation;
838e49c7b2fSDavid Howells 		afs_begin_vnode_operation(op);
839e49c7b2fSDavid Howells 		afs_wait_for_operation(op);
8405cf9dd55SDavid Howells 	}
8415cf9dd55SDavid Howells 
842e49c7b2fSDavid Howells 	if (op->error == -ENOTSUPP) {
843e49c7b2fSDavid Howells 		/* We could try FS.BulkStatus next, but this aborts the entire
844e49c7b2fSDavid Howells 		 * op if any of the lookups fails - so, for the moment, revert
845e49c7b2fSDavid Howells 		 * to FS.FetchStatus for op->file[1].
8465cf9dd55SDavid Howells 		 */
847e49c7b2fSDavid Howells 		op->fetch_status.which = 1;
848e49c7b2fSDavid Howells 		op->ops = &afs_fetch_status_operation;
849e49c7b2fSDavid Howells 		afs_begin_vnode_operation(op);
850e49c7b2fSDavid Howells 		afs_wait_for_operation(op);
851e49c7b2fSDavid Howells 	}
852e49c7b2fSDavid Howells 	inode = ERR_PTR(op->error);
853e49c7b2fSDavid Howells 
854e49c7b2fSDavid Howells out_op:
855e49c7b2fSDavid Howells 	if (op->error == 0) {
856e49c7b2fSDavid Howells 		inode = &op->file[1].vnode->vfs_inode;
857e49c7b2fSDavid Howells 		op->file[1].vnode = NULL;
8585cf9dd55SDavid Howells 	}
8595cf9dd55SDavid Howells 
860e49c7b2fSDavid Howells 	if (op->file[0].scb.have_status)
861e49c7b2fSDavid Howells 		dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version;
862e49c7b2fSDavid Howells 	else
863e49c7b2fSDavid Howells 		dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before;
864e49c7b2fSDavid Howells 	ret = afs_put_operation(op);
8655cf9dd55SDavid Howells out:
8665cf9dd55SDavid Howells 	kfree(cookie);
867e49c7b2fSDavid Howells 	_leave("");
868e49c7b2fSDavid Howells 	return inode ?: ERR_PTR(ret);
8695cf9dd55SDavid Howells }
8705cf9dd55SDavid Howells 
8715cf9dd55SDavid Howells /*
8726f8880d8SDavid Howells  * Look up an entry in a directory with @sys substitution.
8736f8880d8SDavid Howells  */
8746f8880d8SDavid Howells static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
8756f8880d8SDavid Howells 				       struct key *key)
8766f8880d8SDavid Howells {
8776f8880d8SDavid Howells 	struct afs_sysnames *subs;
8786f8880d8SDavid Howells 	struct afs_net *net = afs_i2net(dir);
8796f8880d8SDavid Howells 	struct dentry *ret;
8806f8880d8SDavid Howells 	char *buf, *p, *name;
8816f8880d8SDavid Howells 	int len, i;
8826f8880d8SDavid Howells 
8836f8880d8SDavid Howells 	_enter("");
8846f8880d8SDavid Howells 
8856f8880d8SDavid Howells 	ret = ERR_PTR(-ENOMEM);
8866f8880d8SDavid Howells 	p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
8876f8880d8SDavid Howells 	if (!buf)
8886f8880d8SDavid Howells 		goto out_p;
8896f8880d8SDavid Howells 	if (dentry->d_name.len > 4) {
8906f8880d8SDavid Howells 		memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
8916f8880d8SDavid Howells 		p += dentry->d_name.len - 4;
8926f8880d8SDavid Howells 	}
8936f8880d8SDavid Howells 
8946f8880d8SDavid Howells 	/* There is an ordered list of substitutes that we have to try. */
8956f8880d8SDavid Howells 	read_lock(&net->sysnames_lock);
8966f8880d8SDavid Howells 	subs = net->sysnames;
8976f8880d8SDavid Howells 	refcount_inc(&subs->usage);
8986f8880d8SDavid Howells 	read_unlock(&net->sysnames_lock);
8996f8880d8SDavid Howells 
9006f8880d8SDavid Howells 	for (i = 0; i < subs->nr; i++) {
9016f8880d8SDavid Howells 		name = subs->subs[i];
9026f8880d8SDavid Howells 		len = dentry->d_name.len - 4 + strlen(name);
9036f8880d8SDavid Howells 		if (len >= AFSNAMEMAX) {
9046f8880d8SDavid Howells 			ret = ERR_PTR(-ENAMETOOLONG);
9056f8880d8SDavid Howells 			goto out_s;
9066f8880d8SDavid Howells 		}
9076f8880d8SDavid Howells 
9086f8880d8SDavid Howells 		strcpy(p, name);
9096f8880d8SDavid Howells 		ret = lookup_one_len(buf, dentry->d_parent, len);
9106f8880d8SDavid Howells 		if (IS_ERR(ret) || d_is_positive(ret))
9116f8880d8SDavid Howells 			goto out_s;
9126f8880d8SDavid Howells 		dput(ret);
9136f8880d8SDavid Howells 	}
9146f8880d8SDavid Howells 
9156f8880d8SDavid Howells 	/* We don't want to d_add() the @sys dentry here as we don't want to
9166f8880d8SDavid Howells 	 * the cached dentry to hide changes to the sysnames list.
9176f8880d8SDavid Howells 	 */
9186f8880d8SDavid Howells 	ret = NULL;
9196f8880d8SDavid Howells out_s:
9206f8880d8SDavid Howells 	afs_put_sysnames(subs);
9216f8880d8SDavid Howells 	kfree(buf);
9226f8880d8SDavid Howells out_p:
9236f8880d8SDavid Howells 	key_put(key);
9246f8880d8SDavid Howells 	return ret;
9256f8880d8SDavid Howells }
9266f8880d8SDavid Howells 
9276f8880d8SDavid Howells /*
92808e0e7c8SDavid Howells  * look up an entry in a directory
92908e0e7c8SDavid Howells  */
930260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
93100cd8dd3SAl Viro 				 unsigned int flags)
93208e0e7c8SDavid Howells {
9335cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
93440a708bdSDavid Howells 	struct afs_fid fid = {};
93508e0e7c8SDavid Howells 	struct inode *inode;
93634b2a88fSAl Viro 	struct dentry *d;
93700d3b7a4SDavid Howells 	struct key *key;
93808e0e7c8SDavid Howells 	int ret;
93908e0e7c8SDavid Howells 
9403b6492dfSDavid Howells 	_enter("{%llx:%llu},%p{%pd},",
9415cf9dd55SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
942260a9803SDavid Howells 
9432b0143b5SDavid Howells 	ASSERTCMP(d_inode(dentry), ==, NULL);
94408e0e7c8SDavid Howells 
94545222b9eSDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX) {
94608e0e7c8SDavid Howells 		_leave(" = -ENAMETOOLONG");
94708e0e7c8SDavid Howells 		return ERR_PTR(-ENAMETOOLONG);
94808e0e7c8SDavid Howells 	}
94908e0e7c8SDavid Howells 
9505cf9dd55SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
95108e0e7c8SDavid Howells 		_leave(" = -ESTALE");
95208e0e7c8SDavid Howells 		return ERR_PTR(-ESTALE);
95308e0e7c8SDavid Howells 	}
95408e0e7c8SDavid Howells 
9555cf9dd55SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
95600d3b7a4SDavid Howells 	if (IS_ERR(key)) {
95700d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
958e231c2eeSDavid Howells 		return ERR_CAST(key);
95900d3b7a4SDavid Howells 	}
96000d3b7a4SDavid Howells 
9615cf9dd55SDavid Howells 	ret = afs_validate(dvnode, key);
96208e0e7c8SDavid Howells 	if (ret < 0) {
96300d3b7a4SDavid Howells 		key_put(key);
964260a9803SDavid Howells 		_leave(" = %d [val]", ret);
9651da177e4SLinus Torvalds 		return ERR_PTR(ret);
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 
9686f8880d8SDavid Howells 	if (dentry->d_name.len >= 4 &&
9696f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
9706f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
9716f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
9726f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 1] == 's')
9736f8880d8SDavid Howells 		return afs_lookup_atsys(dir, dentry, key);
9746f8880d8SDavid Howells 
975d55b4da4SDavid Howells 	afs_stat_v(dvnode, n_lookup);
9765cf9dd55SDavid Howells 	inode = afs_do_lookup(dir, dentry, key);
97734b2a88fSAl Viro 	key_put(key);
978f52b83b0SDavid Howells 	if (inode == ERR_PTR(-ENOENT))
9795cf9dd55SDavid Howells 		inode = afs_try_auto_mntpt(dentry, dir);
98040a708bdSDavid Howells 
98140a708bdSDavid Howells 	if (!IS_ERR_OR_NULL(inode))
98240a708bdSDavid Howells 		fid = AFS_FS_I(inode)->fid;
98340a708bdSDavid Howells 
984fed79fd7SDavid Howells 	_debug("splice %p", dentry->d_inode);
98534b2a88fSAl Viro 	d = d_splice_alias(inode, dentry);
98680548b03SDavid Howells 	if (!IS_ERR_OR_NULL(d)) {
98734b2a88fSAl Viro 		d->d_fsdata = dentry->d_fsdata;
98840a708bdSDavid Howells 		trace_afs_lookup(dvnode, &d->d_name, &fid);
98980548b03SDavid Howells 	} else {
99040a708bdSDavid Howells 		trace_afs_lookup(dvnode, &dentry->d_name, &fid);
99180548b03SDavid Howells 	}
992e49c7b2fSDavid Howells 	_leave("");
99334b2a88fSAl Viro 	return d;
994ec26815aSDavid Howells }
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds /*
997a0753c29SDavid Howells  * Check the validity of a dentry under RCU conditions.
998a0753c29SDavid Howells  */
999a0753c29SDavid Howells static int afs_d_revalidate_rcu(struct dentry *dentry)
1000a0753c29SDavid Howells {
1001a0753c29SDavid Howells 	struct afs_vnode *dvnode, *vnode;
1002a0753c29SDavid Howells 	struct dentry *parent;
1003a0753c29SDavid Howells 	struct inode *dir, *inode;
1004a0753c29SDavid Howells 	long dir_version, de_version;
1005a0753c29SDavid Howells 
1006a0753c29SDavid Howells 	_enter("%p", dentry);
1007a0753c29SDavid Howells 
1008a0753c29SDavid Howells 	/* Check the parent directory is still valid first. */
1009a0753c29SDavid Howells 	parent = READ_ONCE(dentry->d_parent);
1010a0753c29SDavid Howells 	dir = d_inode_rcu(parent);
1011a0753c29SDavid Howells 	if (!dir)
1012a0753c29SDavid Howells 		return -ECHILD;
1013a0753c29SDavid Howells 	dvnode = AFS_FS_I(dir);
1014a0753c29SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags))
1015a0753c29SDavid Howells 		return -ECHILD;
1016a0753c29SDavid Howells 
1017a0753c29SDavid Howells 	if (!afs_check_validity(dvnode))
1018a0753c29SDavid Howells 		return -ECHILD;
1019a0753c29SDavid Howells 
1020a0753c29SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
1021a0753c29SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
1022a0753c29SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
1023a0753c29SDavid Howells 	 * version.
1024a0753c29SDavid Howells 	 */
1025a0753c29SDavid Howells 	dir_version = (long)READ_ONCE(dvnode->status.data_version);
1026a0753c29SDavid Howells 	de_version = (long)READ_ONCE(dentry->d_fsdata);
1027a0753c29SDavid Howells 	if (de_version != dir_version) {
1028a0753c29SDavid Howells 		dir_version = (long)READ_ONCE(dvnode->invalid_before);
1029a0753c29SDavid Howells 		if (de_version - dir_version < 0)
1030a0753c29SDavid Howells 			return -ECHILD;
1031a0753c29SDavid Howells 	}
1032a0753c29SDavid Howells 
1033a0753c29SDavid Howells 	/* Check to see if the vnode referred to by the dentry still
1034a0753c29SDavid Howells 	 * has a callback.
1035a0753c29SDavid Howells 	 */
1036a0753c29SDavid Howells 	if (d_really_is_positive(dentry)) {
1037a0753c29SDavid Howells 		inode = d_inode_rcu(dentry);
1038a0753c29SDavid Howells 		if (inode) {
1039a0753c29SDavid Howells 			vnode = AFS_FS_I(inode);
1040a0753c29SDavid Howells 			if (!afs_check_validity(vnode))
1041a0753c29SDavid Howells 				return -ECHILD;
1042a0753c29SDavid Howells 		}
1043a0753c29SDavid Howells 	}
1044a0753c29SDavid Howells 
1045a0753c29SDavid Howells 	return 1; /* Still valid */
1046a0753c29SDavid Howells }
1047a0753c29SDavid Howells 
1048a0753c29SDavid Howells /*
10491da177e4SLinus Torvalds  * check that a dentry lookup hit has found a valid entry
10501da177e4SLinus Torvalds  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
10511da177e4SLinus Torvalds  *   inode
10521da177e4SLinus Torvalds  */
10530b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
10541da177e4SLinus Torvalds {
1055260a9803SDavid Howells 	struct afs_vnode *vnode, *dir;
1056dd0d9a46SArtem Bityutskiy 	struct afs_fid uninitialized_var(fid);
10571da177e4SLinus Torvalds 	struct dentry *parent;
1058c435ee34SDavid Howells 	struct inode *inode;
105900d3b7a4SDavid Howells 	struct key *key;
106040fc8102SDavid Howells 	afs_dataversion_t dir_version, invalid_before;
10619dd0b82eSDavid Howells 	long de_version;
10621da177e4SLinus Torvalds 	int ret;
10631da177e4SLinus Torvalds 
10640b728e19SAl Viro 	if (flags & LOOKUP_RCU)
1065a0753c29SDavid Howells 		return afs_d_revalidate_rcu(dentry);
106634286d66SNick Piggin 
1067c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
10682b0143b5SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
10693b6492dfSDavid Howells 		_enter("{v={%llx:%llu} n=%pd fl=%lx},",
1070a455589fSAl Viro 		       vnode->fid.vid, vnode->fid.vnode, dentry,
1071260a9803SDavid Howells 		       vnode->flags);
1072c435ee34SDavid Howells 	} else {
1073a455589fSAl Viro 		_enter("{neg n=%pd}", dentry);
1074c435ee34SDavid Howells 	}
10751da177e4SLinus Torvalds 
1076260a9803SDavid Howells 	key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
107700d3b7a4SDavid Howells 	if (IS_ERR(key))
107800d3b7a4SDavid Howells 		key = NULL;
107900d3b7a4SDavid Howells 
1080c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
1081c435ee34SDavid Howells 		inode = d_inode(dentry);
1082c435ee34SDavid Howells 		if (inode) {
1083c435ee34SDavid Howells 			vnode = AFS_FS_I(inode);
1084c435ee34SDavid Howells 			afs_validate(vnode, key);
1085c435ee34SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1086c435ee34SDavid Howells 				goto out_bad;
1087c435ee34SDavid Howells 		}
1088c435ee34SDavid Howells 	}
1089c435ee34SDavid Howells 
10901da177e4SLinus Torvalds 	/* lock down the parent dentry so we can peer at it */
109108e0e7c8SDavid Howells 	parent = dget_parent(dentry);
10922b0143b5SDavid Howells 	dir = AFS_FS_I(d_inode(parent));
10931da177e4SLinus Torvalds 
1094260a9803SDavid Howells 	/* validate the parent directory */
1095260a9803SDavid Howells 	afs_validate(dir, key);
1096260a9803SDavid Howells 
1097260a9803SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1098a455589fSAl Viro 		_debug("%pd: parent dir deleted", dentry);
1099c435ee34SDavid Howells 		goto out_bad_parent;
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds 
1102a4ff7401SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
1103a4ff7401SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
1104a4ff7401SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
1105a4ff7401SDavid Howells 	 * version.
1106a4ff7401SDavid Howells 	 */
11079dd0b82eSDavid Howells 	dir_version = dir->status.data_version;
1108a4ff7401SDavid Howells 	de_version = (long)dentry->d_fsdata;
11099dd0b82eSDavid Howells 	if (de_version == (long)dir_version)
11105dc84855SDavid Howells 		goto out_valid_noupdate;
1111a4ff7401SDavid Howells 
111240fc8102SDavid Howells 	invalid_before = dir->invalid_before;
111340fc8102SDavid Howells 	if (de_version - (long)invalid_before >= 0)
1114a4ff7401SDavid Howells 		goto out_valid;
1115260a9803SDavid Howells 
111608e0e7c8SDavid Howells 	_debug("dir modified");
1117d55b4da4SDavid Howells 	afs_stat_v(dir, n_reval);
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	/* search the directory for this vnode */
11209dd0b82eSDavid Howells 	ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key, &dir_version);
1121260a9803SDavid Howells 	switch (ret) {
1122260a9803SDavid Howells 	case 0:
1123260a9803SDavid Howells 		/* the filename maps to something */
11242b0143b5SDavid Howells 		if (d_really_is_negative(dentry))
1125c435ee34SDavid Howells 			goto out_bad_parent;
1126c435ee34SDavid Howells 		inode = d_inode(dentry);
1127c435ee34SDavid Howells 		if (is_bad_inode(inode)) {
1128a455589fSAl Viro 			printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1129a455589fSAl Viro 			       dentry);
1130c435ee34SDavid Howells 			goto out_bad_parent;
11311da177e4SLinus Torvalds 		}
11321da177e4SLinus Torvalds 
1133c435ee34SDavid Howells 		vnode = AFS_FS_I(inode);
1134c435ee34SDavid Howells 
11351da177e4SLinus Torvalds 		/* if the vnode ID has changed, then the dirent points to a
11361da177e4SLinus Torvalds 		 * different file */
113708e0e7c8SDavid Howells 		if (fid.vnode != vnode->fid.vnode) {
11383b6492dfSDavid Howells 			_debug("%pd: dirent changed [%llu != %llu]",
1139a455589fSAl Viro 			       dentry, fid.vnode,
114008e0e7c8SDavid Howells 			       vnode->fid.vnode);
11411da177e4SLinus Torvalds 			goto not_found;
11421da177e4SLinus Torvalds 		}
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 		/* if the vnode ID uniqifier has changed, then the file has
1145260a9803SDavid Howells 		 * been deleted and replaced, and the original vnode ID has
1146260a9803SDavid Howells 		 * been reused */
114708e0e7c8SDavid Howells 		if (fid.unique != vnode->fid.unique) {
1148a455589fSAl Viro 			_debug("%pd: file deleted (uq %u -> %u I:%u)",
1149a455589fSAl Viro 			       dentry, fid.unique,
11507a224228SJean Noel Cordenner 			       vnode->fid.unique,
1151c435ee34SDavid Howells 			       vnode->vfs_inode.i_generation);
1152c435ee34SDavid Howells 			write_seqlock(&vnode->cb_lock);
115308e0e7c8SDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
1154c435ee34SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1155260a9803SDavid Howells 			goto not_found;
1156260a9803SDavid Howells 		}
1157260a9803SDavid Howells 		goto out_valid;
1158260a9803SDavid Howells 
1159260a9803SDavid Howells 	case -ENOENT:
1160260a9803SDavid Howells 		/* the filename is unknown */
1161a455589fSAl Viro 		_debug("%pd: dirent not found", dentry);
11622b0143b5SDavid Howells 		if (d_really_is_positive(dentry))
1163260a9803SDavid Howells 			goto not_found;
1164260a9803SDavid Howells 		goto out_valid;
1165260a9803SDavid Howells 
1166260a9803SDavid Howells 	default:
1167a455589fSAl Viro 		_debug("failed to iterate dir %pd: %d",
1168a455589fSAl Viro 		       parent, ret);
1169c435ee34SDavid Howells 		goto out_bad_parent;
11701da177e4SLinus Torvalds 	}
117108e0e7c8SDavid Howells 
11721da177e4SLinus Torvalds out_valid:
11739dd0b82eSDavid Howells 	dentry->d_fsdata = (void *)(unsigned long)dir_version;
11745dc84855SDavid Howells out_valid_noupdate:
11751da177e4SLinus Torvalds 	dput(parent);
117600d3b7a4SDavid Howells 	key_put(key);
11771da177e4SLinus Torvalds 	_leave(" = 1 [valid]");
11781da177e4SLinus Torvalds 	return 1;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	/* the dirent, if it exists, now points to a different vnode */
11811da177e4SLinus Torvalds not_found:
11821da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
11831da177e4SLinus Torvalds 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
11841da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
11851da177e4SLinus Torvalds 
1186c435ee34SDavid Howells out_bad_parent:
1187a455589fSAl Viro 	_debug("dropping dentry %pd2", dentry);
11881da177e4SLinus Torvalds 	dput(parent);
1189c435ee34SDavid Howells out_bad:
119000d3b7a4SDavid Howells 	key_put(key);
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	_leave(" = 0 [bad]");
11931da177e4SLinus Torvalds 	return 0;
1194ec26815aSDavid Howells }
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds /*
11971da177e4SLinus Torvalds  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
11981da177e4SLinus Torvalds  * sleep)
11991da177e4SLinus Torvalds  * - called from dput() when d_count is going to 0.
12001da177e4SLinus Torvalds  * - return 1 to request dentry be unhashed, 0 otherwise
12011da177e4SLinus Torvalds  */
1202fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry)
12031da177e4SLinus Torvalds {
1204a455589fSAl Viro 	_enter("%pd", dentry);
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
12071da177e4SLinus Torvalds 		goto zap;
12081da177e4SLinus Torvalds 
12092b0143b5SDavid Howells 	if (d_really_is_positive(dentry) &&
12102b0143b5SDavid Howells 	    (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
12112b0143b5SDavid Howells 	     test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
12121da177e4SLinus Torvalds 		goto zap;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	_leave(" = 0 [keep]");
12151da177e4SLinus Torvalds 	return 0;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds zap:
12181da177e4SLinus Torvalds 	_leave(" = 1 [zap]");
12191da177e4SLinus Torvalds 	return 1;
1220ec26815aSDavid Howells }
1221260a9803SDavid Howells 
1222260a9803SDavid Howells /*
122379ddbfa5SDavid Howells  * Clean up sillyrename files on dentry removal.
122479ddbfa5SDavid Howells  */
122579ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode)
122679ddbfa5SDavid Howells {
122779ddbfa5SDavid Howells 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
122879ddbfa5SDavid Howells 		afs_silly_iput(dentry, inode);
122979ddbfa5SDavid Howells 	iput(inode);
123079ddbfa5SDavid Howells }
123179ddbfa5SDavid Howells 
123279ddbfa5SDavid Howells /*
1233260a9803SDavid Howells  * handle dentry release
1234260a9803SDavid Howells  */
123566c7e1d3SDavid Howells void afs_d_release(struct dentry *dentry)
1236260a9803SDavid Howells {
1237a455589fSAl Viro 	_enter("%pd", dentry);
1238260a9803SDavid Howells }
1239260a9803SDavid Howells 
1240728279a5SDavid Howells void afs_check_for_remote_deletion(struct afs_operation *op)
1241728279a5SDavid Howells {
1242728279a5SDavid Howells 	struct afs_vnode *vnode = op->file[0].vnode;
1243728279a5SDavid Howells 
1244728279a5SDavid Howells 	switch (op->ac.abort_code) {
1245728279a5SDavid Howells 	case VNOVNODE:
1246728279a5SDavid Howells 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1247728279a5SDavid Howells 		afs_break_callback(vnode, afs_cb_break_for_deleted);
1248728279a5SDavid Howells 	}
1249728279a5SDavid Howells }
1250728279a5SDavid Howells 
1251260a9803SDavid Howells /*
1252d2ddc776SDavid Howells  * Create a new inode for create/mkdir/symlink
1253d2ddc776SDavid Howells  */
1254e49c7b2fSDavid Howells static void afs_vnode_new_inode(struct afs_operation *op)
1255d2ddc776SDavid Howells {
1256e49c7b2fSDavid Howells 	struct afs_vnode_param *vp = &op->file[1];
12575a813276SDavid Howells 	struct afs_vnode *vnode;
1258d2ddc776SDavid Howells 	struct inode *inode;
1259d2ddc776SDavid Howells 
1260e49c7b2fSDavid Howells 	_enter("");
1261d2ddc776SDavid Howells 
1262e49c7b2fSDavid Howells 	ASSERTCMP(op->error, ==, 0);
1263e49c7b2fSDavid Howells 
1264e49c7b2fSDavid Howells 	inode = afs_iget(op, vp);
1265d2ddc776SDavid Howells 	if (IS_ERR(inode)) {
1266d2ddc776SDavid Howells 		/* ENOMEM or EINTR at a really inconvenient time - just abandon
1267d2ddc776SDavid Howells 		 * the new directory on the server.
1268d2ddc776SDavid Howells 		 */
1269e49c7b2fSDavid Howells 		op->error = PTR_ERR(inode);
1270d2ddc776SDavid Howells 		return;
1271d2ddc776SDavid Howells 	}
1272d2ddc776SDavid Howells 
12735a813276SDavid Howells 	vnode = AFS_FS_I(inode);
12745a813276SDavid Howells 	set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1275e49c7b2fSDavid Howells 	if (!op->error)
1276e49c7b2fSDavid Howells 		afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb);
1277e49c7b2fSDavid Howells 	d_instantiate(op->dentry, inode);
1278d2ddc776SDavid Howells }
1279d2ddc776SDavid Howells 
1280e49c7b2fSDavid Howells static void afs_create_success(struct afs_operation *op)
1281b8359153SDavid Howells {
1282e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1283da8d0755SDavid Howells 	op->ctime = op->file[0].scb.status.mtime_client;
1284e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
1285e49c7b2fSDavid Howells 	afs_update_dentry_version(op, &op->file[0], op->dentry);
1286e49c7b2fSDavid Howells 	afs_vnode_new_inode(op);
1287b8359153SDavid Howells }
1288b8359153SDavid Howells 
1289e49c7b2fSDavid Howells static void afs_create_edit_dir(struct afs_operation *op)
12909dd0b82eSDavid Howells {
1291e49c7b2fSDavid Howells 	struct afs_vnode_param *dvp = &op->file[0];
1292e49c7b2fSDavid Howells 	struct afs_vnode_param *vp = &op->file[1];
1293e49c7b2fSDavid Howells 	struct afs_vnode *dvnode = dvp->vnode;
1294e49c7b2fSDavid Howells 
1295e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1296e49c7b2fSDavid Howells 
1297e49c7b2fSDavid Howells 	down_write(&dvnode->validate_lock);
1298e49c7b2fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1299e49c7b2fSDavid Howells 	    dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1300e49c7b2fSDavid Howells 		afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid,
1301e49c7b2fSDavid Howells 				 op->create.reason);
1302e49c7b2fSDavid Howells 	up_write(&dvnode->validate_lock);
13039dd0b82eSDavid Howells }
13049dd0b82eSDavid Howells 
1305e49c7b2fSDavid Howells static void afs_create_put(struct afs_operation *op)
1306e49c7b2fSDavid Howells {
1307e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1308e49c7b2fSDavid Howells 
1309e49c7b2fSDavid Howells 	if (op->error)
1310e49c7b2fSDavid Howells 		d_drop(op->dentry);
1311e49c7b2fSDavid Howells }
1312e49c7b2fSDavid Howells 
1313e49c7b2fSDavid Howells static const struct afs_operation_ops afs_mkdir_operation = {
1314e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_make_dir,
1315e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_make_dir,
1316e49c7b2fSDavid Howells 	.success	= afs_create_success,
1317728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1318e49c7b2fSDavid Howells 	.edit_dir	= afs_create_edit_dir,
1319e49c7b2fSDavid Howells 	.put		= afs_create_put,
1320e49c7b2fSDavid Howells };
1321e49c7b2fSDavid Howells 
13229dd0b82eSDavid Howells /*
1323260a9803SDavid Howells  * create a directory on an AFS filesystem
1324260a9803SDavid Howells  */
132518bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1326260a9803SDavid Howells {
1327e49c7b2fSDavid Howells 	struct afs_operation *op;
1328d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1329260a9803SDavid Howells 
13303b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho",
1331a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1332260a9803SDavid Howells 
1333e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1334e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
1335260a9803SDavid Howells 		d_drop(dentry);
1336e49c7b2fSDavid Howells 		return PTR_ERR(op);
1337e49c7b2fSDavid Howells 	}
1338e49c7b2fSDavid Howells 
1339e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1340e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
1341da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1342e49c7b2fSDavid Howells 	op->dentry	= dentry;
1343e49c7b2fSDavid Howells 	op->create.mode	= S_IFDIR | mode;
1344e49c7b2fSDavid Howells 	op->create.reason = afs_edit_dir_for_mkdir;
1345e49c7b2fSDavid Howells 	op->ops		= &afs_mkdir_operation;
1346e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1347260a9803SDavid Howells }
1348260a9803SDavid Howells 
1349260a9803SDavid Howells /*
1350d2ddc776SDavid Howells  * Remove a subdir from a directory.
1351260a9803SDavid Howells  */
1352d2ddc776SDavid Howells static void afs_dir_remove_subdir(struct dentry *dentry)
1353260a9803SDavid Howells {
13542b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1355d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1356d2ddc776SDavid Howells 
1357260a9803SDavid Howells 		clear_nlink(&vnode->vfs_inode);
1358260a9803SDavid Howells 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1359c435ee34SDavid Howells 		clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
136063a4681fSDavid Howells 		clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1361260a9803SDavid Howells 	}
1362260a9803SDavid Howells }
1363260a9803SDavid Howells 
1364e49c7b2fSDavid Howells static void afs_rmdir_success(struct afs_operation *op)
1365e49c7b2fSDavid Howells {
1366e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1367da8d0755SDavid Howells 	op->ctime = op->file[0].scb.status.mtime_client;
1368e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
1369e49c7b2fSDavid Howells 	afs_update_dentry_version(op, &op->file[0], op->dentry);
1370e49c7b2fSDavid Howells }
1371e49c7b2fSDavid Howells 
1372e49c7b2fSDavid Howells static void afs_rmdir_edit_dir(struct afs_operation *op)
1373e49c7b2fSDavid Howells {
1374e49c7b2fSDavid Howells 	struct afs_vnode_param *dvp = &op->file[0];
1375e49c7b2fSDavid Howells 	struct afs_vnode *dvnode = dvp->vnode;
1376e49c7b2fSDavid Howells 
1377e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1378e49c7b2fSDavid Howells 	afs_dir_remove_subdir(op->dentry);
1379e49c7b2fSDavid Howells 
1380e49c7b2fSDavid Howells 	down_write(&dvnode->validate_lock);
1381e49c7b2fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1382e49c7b2fSDavid Howells 	    dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1383e49c7b2fSDavid Howells 		afs_edit_dir_remove(dvnode, &op->dentry->d_name,
1384e49c7b2fSDavid Howells 				    afs_edit_dir_for_rmdir);
1385e49c7b2fSDavid Howells 	up_write(&dvnode->validate_lock);
1386e49c7b2fSDavid Howells }
1387e49c7b2fSDavid Howells 
1388e49c7b2fSDavid Howells static void afs_rmdir_put(struct afs_operation *op)
1389e49c7b2fSDavid Howells {
1390e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1391e49c7b2fSDavid Howells 	if (op->file[1].vnode)
1392e49c7b2fSDavid Howells 		up_write(&op->file[1].vnode->rmdir_lock);
1393e49c7b2fSDavid Howells }
1394e49c7b2fSDavid Howells 
1395e49c7b2fSDavid Howells static const struct afs_operation_ops afs_rmdir_operation = {
1396e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_remove_dir,
1397e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_remove_dir,
1398e49c7b2fSDavid Howells 	.success	= afs_rmdir_success,
1399728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1400e49c7b2fSDavid Howells 	.edit_dir	= afs_rmdir_edit_dir,
1401e49c7b2fSDavid Howells 	.put		= afs_rmdir_put,
1402e49c7b2fSDavid Howells };
1403e49c7b2fSDavid Howells 
1404260a9803SDavid Howells /*
1405d2ddc776SDavid Howells  * remove a directory from an AFS filesystem
1406260a9803SDavid Howells  */
1407d2ddc776SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1408260a9803SDavid Howells {
1409e49c7b2fSDavid Howells 	struct afs_operation *op;
1410f58db83fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1411260a9803SDavid Howells 	int ret;
1412260a9803SDavid Howells 
14133b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1414a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1415260a9803SDavid Howells 
1416e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1417e49c7b2fSDavid Howells 	if (IS_ERR(op))
1418e49c7b2fSDavid Howells 		return PTR_ERR(op);
1419a58823acSDavid Howells 
1420e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1421e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
1422da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1423e49c7b2fSDavid Howells 
1424e49c7b2fSDavid Howells 	op->dentry	= dentry;
1425e49c7b2fSDavid Howells 	op->ops		= &afs_rmdir_operation;
1426260a9803SDavid Howells 
1427f58db83fSDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1428f58db83fSDavid Howells 	if (d_really_is_positive(dentry)) {
1429f58db83fSDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1430e49c7b2fSDavid Howells 		ret = afs_validate(vnode, op->key);
1431f58db83fSDavid Howells 		if (ret < 0)
1432e49c7b2fSDavid Howells 			goto error;
1433f58db83fSDavid Howells 	}
1434f58db83fSDavid Howells 
143579ddbfa5SDavid Howells 	if (vnode) {
143679ddbfa5SDavid Howells 		ret = down_write_killable(&vnode->rmdir_lock);
143779ddbfa5SDavid Howells 		if (ret < 0)
1438e49c7b2fSDavid Howells 			goto error;
1439e49c7b2fSDavid Howells 		op->file[1].vnode = vnode;
144079ddbfa5SDavid Howells 	}
144179ddbfa5SDavid Howells 
1442e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1443a58823acSDavid Howells 
1444d2ddc776SDavid Howells error:
1445e49c7b2fSDavid Howells 	return afs_put_operation(op);
1446d2ddc776SDavid Howells }
1447260a9803SDavid Howells 
1448d2ddc776SDavid Howells /*
1449d2ddc776SDavid Howells  * Remove a link to a file or symlink from a directory.
1450d2ddc776SDavid Howells  *
1451d2ddc776SDavid Howells  * If the file was not deleted due to excess hard links, the fileserver will
1452d2ddc776SDavid Howells  * break the callback promise on the file - if it had one - before it returns
1453d2ddc776SDavid Howells  * to us, and if it was deleted, it won't
1454d2ddc776SDavid Howells  *
1455d2ddc776SDavid Howells  * However, if we didn't have a callback promise outstanding, or it was
1456d2ddc776SDavid Howells  * outstanding on a different server, then it won't break it either...
1457d2ddc776SDavid Howells  */
1458e49c7b2fSDavid Howells static void afs_dir_remove_link(struct afs_operation *op)
1459d2ddc776SDavid Howells {
1460e49c7b2fSDavid Howells 	struct afs_vnode *dvnode = op->file[0].vnode;
1461e49c7b2fSDavid Howells 	struct afs_vnode *vnode = op->file[1].vnode;
1462e49c7b2fSDavid Howells 	struct dentry *dentry = op->dentry;
1463e49c7b2fSDavid Howells 	int ret;
1464d2ddc776SDavid Howells 
1465e49c7b2fSDavid Howells 	if (op->error != 0 ||
1466e49c7b2fSDavid Howells 	    (op->file[1].scb.have_status && op->file[1].scb.have_error))
1467e49c7b2fSDavid Howells 		return;
1468e49c7b2fSDavid Howells 	if (d_really_is_positive(dentry))
1469e49c7b2fSDavid Howells 		return;
1470d2ddc776SDavid Howells 
147130062bd1SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
147230062bd1SDavid Howells 		/* Already done */
1473a38a7558SDavid Howells 	} else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1474a38a7558SDavid Howells 		write_seqlock(&vnode->cb_lock);
1475440fbc3aSDavid Howells 		drop_nlink(&vnode->vfs_inode);
1476440fbc3aSDavid Howells 		if (vnode->vfs_inode.i_nlink == 0) {
1477440fbc3aSDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
1478051d2525SDavid Howells 			__afs_break_callback(vnode, afs_cb_break_for_unlink);
1479440fbc3aSDavid Howells 		}
1480a38a7558SDavid Howells 		write_sequnlock(&vnode->cb_lock);
1481440fbc3aSDavid Howells 	} else {
1482051d2525SDavid Howells 		afs_break_callback(vnode, afs_cb_break_for_unlink);
1483440fbc3aSDavid Howells 
1484d2ddc776SDavid Howells 		if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1485e49c7b2fSDavid Howells 			_debug("AFS_VNODE_DELETED");
1486d2ddc776SDavid Howells 
1487e49c7b2fSDavid Howells 		ret = afs_validate(vnode, op->key);
1488e49c7b2fSDavid Howells 		if (ret != -ESTALE)
1489e49c7b2fSDavid Howells 			op->error = ret;
1490d2ddc776SDavid Howells 	}
1491d2ddc776SDavid Howells 
1492e49c7b2fSDavid Howells 	_debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, op->error);
1493d2ddc776SDavid Howells }
1494d2ddc776SDavid Howells 
1495e49c7b2fSDavid Howells static void afs_unlink_success(struct afs_operation *op)
1496e49c7b2fSDavid Howells {
1497e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1498da8d0755SDavid Howells 	op->ctime = op->file[0].scb.status.mtime_client;
1499b6489a49SDavid Howells 	afs_check_dir_conflict(op, &op->file[0]);
1500e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
1501e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[1]);
1502e49c7b2fSDavid Howells 	afs_update_dentry_version(op, &op->file[0], op->dentry);
1503e49c7b2fSDavid Howells 	afs_dir_remove_link(op);
1504e49c7b2fSDavid Howells }
1505e49c7b2fSDavid Howells 
1506e49c7b2fSDavid Howells static void afs_unlink_edit_dir(struct afs_operation *op)
1507e49c7b2fSDavid Howells {
1508e49c7b2fSDavid Howells 	struct afs_vnode_param *dvp = &op->file[0];
1509e49c7b2fSDavid Howells 	struct afs_vnode *dvnode = dvp->vnode;
1510e49c7b2fSDavid Howells 
1511e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1512e49c7b2fSDavid Howells 	down_write(&dvnode->validate_lock);
1513e49c7b2fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) &&
1514e49c7b2fSDavid Howells 	    dvnode->status.data_version == dvp->dv_before + dvp->dv_delta)
1515e49c7b2fSDavid Howells 		afs_edit_dir_remove(dvnode, &op->dentry->d_name,
1516e49c7b2fSDavid Howells 				    afs_edit_dir_for_unlink);
1517e49c7b2fSDavid Howells 	up_write(&dvnode->validate_lock);
1518e49c7b2fSDavid Howells }
1519e49c7b2fSDavid Howells 
1520e49c7b2fSDavid Howells static void afs_unlink_put(struct afs_operation *op)
1521e49c7b2fSDavid Howells {
1522e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1523e49c7b2fSDavid Howells 	if (op->unlink.need_rehash && op->error < 0 && op->error != -ENOENT)
1524e49c7b2fSDavid Howells 		d_rehash(op->dentry);
1525e49c7b2fSDavid Howells }
1526e49c7b2fSDavid Howells 
1527e49c7b2fSDavid Howells static const struct afs_operation_ops afs_unlink_operation = {
1528e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_remove_file,
1529e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_remove_file,
1530e49c7b2fSDavid Howells 	.success	= afs_unlink_success,
1531728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1532e49c7b2fSDavid Howells 	.edit_dir	= afs_unlink_edit_dir,
1533e49c7b2fSDavid Howells 	.put		= afs_unlink_put,
1534e49c7b2fSDavid Howells };
1535e49c7b2fSDavid Howells 
1536d2ddc776SDavid Howells /*
1537d2ddc776SDavid Howells  * Remove a file or symlink from an AFS filesystem.
1538d2ddc776SDavid Howells  */
1539d2ddc776SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry)
1540d2ddc776SDavid Howells {
1541e49c7b2fSDavid Howells 	struct afs_operation *op;
1542fa59f52fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1543fa59f52fSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1544d2ddc776SDavid Howells 	int ret;
1545d2ddc776SDavid Howells 
15463b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1547d2ddc776SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1548d2ddc776SDavid Howells 
1549d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1550d2ddc776SDavid Howells 		return -ENAMETOOLONG;
1551d2ddc776SDavid Howells 
1552e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1553e49c7b2fSDavid Howells 	if (IS_ERR(op))
1554e49c7b2fSDavid Howells 		return PTR_ERR(op);
1555a58823acSDavid Howells 
1556e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1557e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
1558da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1559d2ddc776SDavid Howells 
1560d2ddc776SDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1561e49c7b2fSDavid Howells 	ret = afs_validate(vnode, op->key);
1562e49c7b2fSDavid Howells 	if (ret < 0) {
1563e49c7b2fSDavid Howells 		op->error = ret;
1564e49c7b2fSDavid Howells 		goto error;
1565e49c7b2fSDavid Howells 	}
1566d2ddc776SDavid Howells 
156779ddbfa5SDavid Howells 	spin_lock(&dentry->d_lock);
1568fa59f52fSDavid Howells 	if (d_count(dentry) > 1) {
156979ddbfa5SDavid Howells 		spin_unlock(&dentry->d_lock);
157079ddbfa5SDavid Howells 		/* Start asynchronous writeout of the inode */
157179ddbfa5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
1572e49c7b2fSDavid Howells 		op->error = afs_sillyrename(dvnode, vnode, dentry, op->key);
1573e49c7b2fSDavid Howells 		goto error;
157479ddbfa5SDavid Howells 	}
157579ddbfa5SDavid Howells 	if (!d_unhashed(dentry)) {
157679ddbfa5SDavid Howells 		/* Prevent a race with RCU lookup. */
157779ddbfa5SDavid Howells 		__d_drop(dentry);
1578e49c7b2fSDavid Howells 		op->unlink.need_rehash = true;
157979ddbfa5SDavid Howells 	}
158079ddbfa5SDavid Howells 	spin_unlock(&dentry->d_lock);
158179ddbfa5SDavid Howells 
1582e49c7b2fSDavid Howells 	op->file[1].vnode = vnode;
1583da8d0755SDavid Howells 	op->file[1].update_ctime = true;
1584b6489a49SDavid Howells 	op->file[1].op_unlinked = true;
1585e49c7b2fSDavid Howells 	op->dentry	= dentry;
1586e49c7b2fSDavid Howells 	op->ops		= &afs_unlink_operation;
1587b6489a49SDavid Howells 	afs_begin_vnode_operation(op);
1588b6489a49SDavid Howells 	afs_wait_for_operation(op);
1589b6489a49SDavid Howells 
1590b6489a49SDavid Howells 	/* If there was a conflict with a third party, check the status of the
1591b6489a49SDavid Howells 	 * unlinked vnode.
1592b6489a49SDavid Howells 	 */
1593b6489a49SDavid Howells 	if (op->error == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) {
1594b6489a49SDavid Howells 		op->file[1].update_ctime = false;
1595b6489a49SDavid Howells 		op->fetch_status.which = 1;
1596b6489a49SDavid Howells 		op->ops = &afs_fetch_status_operation;
1597b6489a49SDavid Howells 		afs_begin_vnode_operation(op);
1598b6489a49SDavid Howells 		afs_wait_for_operation(op);
1599b6489a49SDavid Howells 	}
1600b6489a49SDavid Howells 
1601b6489a49SDavid Howells 	return afs_put_operation(op);
1602a58823acSDavid Howells 
1603260a9803SDavid Howells error:
1604e49c7b2fSDavid Howells 	return afs_put_operation(op);
1605260a9803SDavid Howells }
1606260a9803SDavid Howells 
1607e49c7b2fSDavid Howells static const struct afs_operation_ops afs_create_operation = {
1608e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_create_file,
1609e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_create_file,
1610e49c7b2fSDavid Howells 	.success	= afs_create_success,
1611728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1612e49c7b2fSDavid Howells 	.edit_dir	= afs_create_edit_dir,
1613e49c7b2fSDavid Howells 	.put		= afs_create_put,
1614e49c7b2fSDavid Howells };
1615e49c7b2fSDavid Howells 
1616260a9803SDavid Howells /*
1617260a9803SDavid Howells  * create a regular file on an AFS filesystem
1618260a9803SDavid Howells  */
16194acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1620ebfc3b49SAl Viro 		      bool excl)
1621260a9803SDavid Howells {
1622e49c7b2fSDavid Howells 	struct afs_operation *op;
162343dd388bSColin Ian King 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1624e49c7b2fSDavid Howells 	int ret = -ENAMETOOLONG;
1625260a9803SDavid Howells 
1626e49c7b2fSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho",
1627a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1628260a9803SDavid Howells 
1629d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1630d2ddc776SDavid Howells 		goto error;
1631d2ddc776SDavid Howells 
1632e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1633e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
1634e49c7b2fSDavid Howells 		ret = PTR_ERR(op);
1635260a9803SDavid Howells 		goto error;
1636260a9803SDavid Howells 	}
1637260a9803SDavid Howells 
1638e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1639e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
1640da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1641a58823acSDavid Howells 
1642e49c7b2fSDavid Howells 	op->dentry	= dentry;
1643e49c7b2fSDavid Howells 	op->create.mode	= S_IFREG | mode;
1644e49c7b2fSDavid Howells 	op->create.reason = afs_edit_dir_for_create;
1645e49c7b2fSDavid Howells 	op->ops		= &afs_create_operation;
1646e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1647a58823acSDavid Howells 
1648260a9803SDavid Howells error:
1649260a9803SDavid Howells 	d_drop(dentry);
1650260a9803SDavid Howells 	_leave(" = %d", ret);
1651260a9803SDavid Howells 	return ret;
1652260a9803SDavid Howells }
1653260a9803SDavid Howells 
1654e49c7b2fSDavid Howells static void afs_link_success(struct afs_operation *op)
1655e49c7b2fSDavid Howells {
1656e49c7b2fSDavid Howells 	struct afs_vnode_param *dvp = &op->file[0];
1657e49c7b2fSDavid Howells 	struct afs_vnode_param *vp = &op->file[1];
1658e49c7b2fSDavid Howells 
1659e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1660da8d0755SDavid Howells 	op->ctime = dvp->scb.status.mtime_client;
1661e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, dvp);
1662e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, vp);
1663e49c7b2fSDavid Howells 	afs_update_dentry_version(op, dvp, op->dentry);
1664e49c7b2fSDavid Howells 	if (op->dentry_2->d_parent == op->dentry->d_parent)
1665e49c7b2fSDavid Howells 		afs_update_dentry_version(op, dvp, op->dentry_2);
1666e49c7b2fSDavid Howells 	ihold(&vp->vnode->vfs_inode);
1667e49c7b2fSDavid Howells 	d_instantiate(op->dentry, &vp->vnode->vfs_inode);
1668e49c7b2fSDavid Howells }
1669e49c7b2fSDavid Howells 
1670e49c7b2fSDavid Howells static void afs_link_put(struct afs_operation *op)
1671e49c7b2fSDavid Howells {
1672e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1673e49c7b2fSDavid Howells 	if (op->error)
1674e49c7b2fSDavid Howells 		d_drop(op->dentry);
1675e49c7b2fSDavid Howells }
1676e49c7b2fSDavid Howells 
1677e49c7b2fSDavid Howells static const struct afs_operation_ops afs_link_operation = {
1678e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_link,
1679e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_link,
1680e49c7b2fSDavid Howells 	.success	= afs_link_success,
1681728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1682e49c7b2fSDavid Howells 	.edit_dir	= afs_create_edit_dir,
1683e49c7b2fSDavid Howells 	.put		= afs_link_put,
1684e49c7b2fSDavid Howells };
1685e49c7b2fSDavid Howells 
1686260a9803SDavid Howells /*
1687260a9803SDavid Howells  * create a hard link between files in an AFS filesystem
1688260a9803SDavid Howells  */
1689260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
1690260a9803SDavid Howells 		    struct dentry *dentry)
1691260a9803SDavid Howells {
1692e49c7b2fSDavid Howells 	struct afs_operation *op;
1693a58823acSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1694a58823acSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1695e49c7b2fSDavid Howells 	int ret = -ENAMETOOLONG;
1696260a9803SDavid Howells 
16973b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%pd}",
1698260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1699260a9803SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode,
1700a455589fSAl Viro 	       dentry);
1701260a9803SDavid Howells 
1702d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1703d2ddc776SDavid Howells 		goto error;
1704d2ddc776SDavid Howells 
1705e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1706e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
1707e49c7b2fSDavid Howells 		ret = PTR_ERR(op);
1708a58823acSDavid Howells 		goto error;
1709260a9803SDavid Howells 	}
1710260a9803SDavid Howells 
1711e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1712e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 1, vnode);
1713e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
1714da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1715da8d0755SDavid Howells 	op->file[1].update_ctime = true;
1716a58823acSDavid Howells 
1717e49c7b2fSDavid Howells 	op->dentry		= dentry;
1718e49c7b2fSDavid Howells 	op->dentry_2		= from;
1719e49c7b2fSDavid Howells 	op->ops			= &afs_link_operation;
1720e49c7b2fSDavid Howells 	op->create.reason	= afs_edit_dir_for_link;
1721e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1722260a9803SDavid Howells 
1723260a9803SDavid Howells error:
1724260a9803SDavid Howells 	d_drop(dentry);
1725260a9803SDavid Howells 	_leave(" = %d", ret);
1726260a9803SDavid Howells 	return ret;
1727260a9803SDavid Howells }
1728260a9803SDavid Howells 
1729e49c7b2fSDavid Howells static const struct afs_operation_ops afs_symlink_operation = {
1730e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_symlink,
1731e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_symlink,
1732e49c7b2fSDavid Howells 	.success	= afs_create_success,
1733728279a5SDavid Howells 	.aborted	= afs_check_for_remote_deletion,
1734e49c7b2fSDavid Howells 	.edit_dir	= afs_create_edit_dir,
1735e49c7b2fSDavid Howells 	.put		= afs_create_put,
1736e49c7b2fSDavid Howells };
1737e49c7b2fSDavid Howells 
1738260a9803SDavid Howells /*
1739260a9803SDavid Howells  * create a symlink in an AFS filesystem
1740260a9803SDavid Howells  */
1741260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
1742260a9803SDavid Howells 		       const char *content)
1743260a9803SDavid Howells {
1744e49c7b2fSDavid Howells 	struct afs_operation *op;
1745d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1746260a9803SDavid Howells 	int ret;
1747260a9803SDavid Howells 
17483b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%s",
1749a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry,
1750260a9803SDavid Howells 	       content);
1751260a9803SDavid Howells 
1752d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1753d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1754d2ddc776SDavid Howells 		goto error;
1755d2ddc776SDavid Howells 
1756260a9803SDavid Howells 	ret = -EINVAL;
175745222b9eSDavid Howells 	if (strlen(content) >= AFSPATHMAX)
1758260a9803SDavid Howells 		goto error;
1759260a9803SDavid Howells 
1760e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, dvnode->volume);
1761e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
1762e49c7b2fSDavid Howells 		ret = PTR_ERR(op);
1763a58823acSDavid Howells 		goto error;
1764260a9803SDavid Howells 	}
1765260a9803SDavid Howells 
1766e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, dvnode);
1767e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
1768a58823acSDavid Howells 
1769e49c7b2fSDavid Howells 	op->dentry		= dentry;
1770e49c7b2fSDavid Howells 	op->ops			= &afs_symlink_operation;
1771e49c7b2fSDavid Howells 	op->create.reason	= afs_edit_dir_for_symlink;
1772e49c7b2fSDavid Howells 	op->create.symlink	= content;
1773e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1774d2ddc776SDavid Howells 
1775260a9803SDavid Howells error:
1776260a9803SDavid Howells 	d_drop(dentry);
1777260a9803SDavid Howells 	_leave(" = %d", ret);
1778260a9803SDavid Howells 	return ret;
1779260a9803SDavid Howells }
1780260a9803SDavid Howells 
1781e49c7b2fSDavid Howells static void afs_rename_success(struct afs_operation *op)
1782260a9803SDavid Howells {
1783e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1784e49c7b2fSDavid Howells 
1785da8d0755SDavid Howells 	op->ctime = op->file[0].scb.status.mtime_client;
1786b6489a49SDavid Howells 	afs_check_dir_conflict(op, &op->file[1]);
1787e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
1788da8d0755SDavid Howells 	if (op->file[1].vnode != op->file[0].vnode) {
1789da8d0755SDavid Howells 		op->ctime = op->file[1].scb.status.mtime_client;
1790e49c7b2fSDavid Howells 		afs_vnode_commit_status(op, &op->file[1]);
1791e49c7b2fSDavid Howells 	}
1792da8d0755SDavid Howells }
1793e49c7b2fSDavid Howells 
1794e49c7b2fSDavid Howells static void afs_rename_edit_dir(struct afs_operation *op)
1795e49c7b2fSDavid Howells {
1796e49c7b2fSDavid Howells 	struct afs_vnode_param *orig_dvp = &op->file[0];
1797e49c7b2fSDavid Howells 	struct afs_vnode_param *new_dvp = &op->file[1];
1798e49c7b2fSDavid Howells 	struct afs_vnode *orig_dvnode = orig_dvp->vnode;
1799e49c7b2fSDavid Howells 	struct afs_vnode *new_dvnode = new_dvp->vnode;
1800e49c7b2fSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry));
1801e49c7b2fSDavid Howells 	struct dentry *old_dentry = op->dentry;
1802e49c7b2fSDavid Howells 	struct dentry *new_dentry = op->dentry_2;
180379ddbfa5SDavid Howells 	struct inode *new_inode;
1804260a9803SDavid Howells 
1805e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
18061cd66c93SMiklos Szeredi 
1807e49c7b2fSDavid Howells 	if (op->rename.rehash) {
1808e49c7b2fSDavid Howells 		d_rehash(op->rename.rehash);
1809e49c7b2fSDavid Howells 		op->rename.rehash = NULL;
1810260a9803SDavid Howells 	}
1811260a9803SDavid Howells 
18122105c282SDavid Howells 	down_write(&orig_dvnode->validate_lock);
18132105c282SDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) &&
1814e49c7b2fSDavid Howells 	    orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta)
181563a4681fSDavid Howells 		afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
181679ddbfa5SDavid Howells 				    afs_edit_dir_for_rename_0);
181763a4681fSDavid Howells 
1818e49c7b2fSDavid Howells 	if (new_dvnode != orig_dvnode) {
1819e49c7b2fSDavid Howells 		up_write(&orig_dvnode->validate_lock);
18202105c282SDavid Howells 		down_write(&new_dvnode->validate_lock);
18212105c282SDavid Howells 	}
1822e49c7b2fSDavid Howells 
18232105c282SDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) &&
1824e49c7b2fSDavid Howells 	    new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) {
1825e49c7b2fSDavid Howells 		if (!op->rename.new_negative)
182663a4681fSDavid Howells 			afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
182779ddbfa5SDavid Howells 					    afs_edit_dir_for_rename_1);
182863a4681fSDavid Howells 
182963a4681fSDavid Howells 		afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
183079ddbfa5SDavid Howells 				 &vnode->fid, afs_edit_dir_for_rename_2);
18312105c282SDavid Howells 	}
183279ddbfa5SDavid Howells 
183379ddbfa5SDavid Howells 	new_inode = d_inode(new_dentry);
183479ddbfa5SDavid Howells 	if (new_inode) {
183579ddbfa5SDavid Howells 		spin_lock(&new_inode->i_lock);
183679ddbfa5SDavid Howells 		if (new_inode->i_nlink > 0)
183779ddbfa5SDavid Howells 			drop_nlink(new_inode);
183879ddbfa5SDavid Howells 		spin_unlock(&new_inode->i_lock);
183979ddbfa5SDavid Howells 	}
18409dd0b82eSDavid Howells 
18419dd0b82eSDavid Howells 	/* Now we can update d_fsdata on the dentries to reflect their
18429dd0b82eSDavid Howells 	 * new parent's data_version.
18439dd0b82eSDavid Howells 	 *
18449dd0b82eSDavid Howells 	 * Note that if we ever implement RENAME_EXCHANGE, we'll have
18459dd0b82eSDavid Howells 	 * to update both dentries with opposing dir versions.
18469dd0b82eSDavid Howells 	 */
1847e49c7b2fSDavid Howells 	afs_update_dentry_version(op, new_dvp, op->dentry);
1848e49c7b2fSDavid Howells 	afs_update_dentry_version(op, new_dvp, op->dentry_2);
1849e49c7b2fSDavid Howells 
185079ddbfa5SDavid Howells 	d_move(old_dentry, new_dentry);
1851e49c7b2fSDavid Howells 
18522105c282SDavid Howells 	up_write(&new_dvnode->validate_lock);
185363a4681fSDavid Howells }
185463a4681fSDavid Howells 
1855e49c7b2fSDavid Howells static void afs_rename_put(struct afs_operation *op)
1856e49c7b2fSDavid Howells {
1857e49c7b2fSDavid Howells 	_enter("op=%08x", op->debug_id);
1858e49c7b2fSDavid Howells 	if (op->rename.rehash)
1859e49c7b2fSDavid Howells 		d_rehash(op->rename.rehash);
1860e49c7b2fSDavid Howells 	dput(op->rename.tmp);
1861e49c7b2fSDavid Howells 	if (op->error)
1862e49c7b2fSDavid Howells 		d_rehash(op->dentry);
1863e49c7b2fSDavid Howells }
1864e49c7b2fSDavid Howells 
1865e49c7b2fSDavid Howells static const struct afs_operation_ops afs_rename_operation = {
1866e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_rename,
1867e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_rename,
1868e49c7b2fSDavid Howells 	.success	= afs_rename_success,
1869e49c7b2fSDavid Howells 	.edit_dir	= afs_rename_edit_dir,
1870e49c7b2fSDavid Howells 	.put		= afs_rename_put,
1871e49c7b2fSDavid Howells };
1872e49c7b2fSDavid Howells 
1873e49c7b2fSDavid Howells /*
1874e49c7b2fSDavid Howells  * rename a file in an AFS filesystem and/or move it between directories
1875e49c7b2fSDavid Howells  */
1876e49c7b2fSDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1877e49c7b2fSDavid Howells 		      struct inode *new_dir, struct dentry *new_dentry,
1878e49c7b2fSDavid Howells 		      unsigned int flags)
1879e49c7b2fSDavid Howells {
1880e49c7b2fSDavid Howells 	struct afs_operation *op;
1881e49c7b2fSDavid Howells 	struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1882e49c7b2fSDavid Howells 	int ret;
1883e49c7b2fSDavid Howells 
1884e49c7b2fSDavid Howells 	if (flags)
1885e49c7b2fSDavid Howells 		return -EINVAL;
1886e49c7b2fSDavid Howells 
1887e49c7b2fSDavid Howells 	/* Don't allow silly-rename files be moved around. */
1888e49c7b2fSDavid Howells 	if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
1889e49c7b2fSDavid Howells 		return -EINVAL;
1890e49c7b2fSDavid Howells 
1891e49c7b2fSDavid Howells 	vnode = AFS_FS_I(d_inode(old_dentry));
1892e49c7b2fSDavid Howells 	orig_dvnode = AFS_FS_I(old_dir);
1893e49c7b2fSDavid Howells 	new_dvnode = AFS_FS_I(new_dir);
1894e49c7b2fSDavid Howells 
1895e49c7b2fSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1896e49c7b2fSDavid Howells 	       orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1897e49c7b2fSDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1898e49c7b2fSDavid Howells 	       new_dvnode->fid.vid, new_dvnode->fid.vnode,
1899e49c7b2fSDavid Howells 	       new_dentry);
1900e49c7b2fSDavid Howells 
1901e49c7b2fSDavid Howells 	op = afs_alloc_operation(NULL, orig_dvnode->volume);
1902e49c7b2fSDavid Howells 	if (IS_ERR(op))
1903e49c7b2fSDavid Howells 		return PTR_ERR(op);
1904e49c7b2fSDavid Howells 
1905e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, orig_dvnode);
1906e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */
1907e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
1908e49c7b2fSDavid Howells 	op->file[1].dv_delta = 1;
1909da8d0755SDavid Howells 	op->file[0].update_ctime = true;
1910da8d0755SDavid Howells 	op->file[1].update_ctime = true;
1911e49c7b2fSDavid Howells 
1912e49c7b2fSDavid Howells 	op->dentry		= old_dentry;
1913e49c7b2fSDavid Howells 	op->dentry_2		= new_dentry;
1914e49c7b2fSDavid Howells 	op->rename.new_negative	= d_is_negative(new_dentry);
1915e49c7b2fSDavid Howells 	op->ops			= &afs_rename_operation;
1916e49c7b2fSDavid Howells 
1917e49c7b2fSDavid Howells 	/* For non-directories, check whether the target is busy and if so,
1918e49c7b2fSDavid Howells 	 * make a copy of the dentry and then do a silly-rename.  If the
1919e49c7b2fSDavid Howells 	 * silly-rename succeeds, the copied dentry is hashed and becomes the
1920e49c7b2fSDavid Howells 	 * new target.
1921e49c7b2fSDavid Howells 	 */
1922e49c7b2fSDavid Howells 	if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
1923e49c7b2fSDavid Howells 		/* To prevent any new references to the target during the
1924e49c7b2fSDavid Howells 		 * rename, we unhash the dentry in advance.
1925e49c7b2fSDavid Howells 		 */
1926e49c7b2fSDavid Howells 		if (!d_unhashed(new_dentry)) {
1927e49c7b2fSDavid Howells 			d_drop(new_dentry);
1928e49c7b2fSDavid Howells 			op->rename.rehash = new_dentry;
1929e49c7b2fSDavid Howells 		}
1930e49c7b2fSDavid Howells 
1931e49c7b2fSDavid Howells 		if (d_count(new_dentry) > 2) {
1932e49c7b2fSDavid Howells 			/* copy the target dentry's name */
1933e49c7b2fSDavid Howells 			ret = -ENOMEM;
1934e49c7b2fSDavid Howells 			op->rename.tmp = d_alloc(new_dentry->d_parent,
1935e49c7b2fSDavid Howells 						 &new_dentry->d_name);
1936e49c7b2fSDavid Howells 			if (!op->rename.tmp)
1937e49c7b2fSDavid Howells 				goto error;
1938e49c7b2fSDavid Howells 
1939e49c7b2fSDavid Howells 			ret = afs_sillyrename(new_dvnode,
1940e49c7b2fSDavid Howells 					      AFS_FS_I(d_inode(new_dentry)),
1941e49c7b2fSDavid Howells 					      new_dentry, op->key);
1942e49c7b2fSDavid Howells 			if (ret)
1943e49c7b2fSDavid Howells 				goto error;
1944e49c7b2fSDavid Howells 
1945e49c7b2fSDavid Howells 			op->dentry_2 = op->rename.tmp;
1946e49c7b2fSDavid Howells 			op->rename.rehash = NULL;
1947e49c7b2fSDavid Howells 			op->rename.new_negative = true;
1948e49c7b2fSDavid Howells 		}
1949e49c7b2fSDavid Howells 	}
1950e49c7b2fSDavid Howells 
1951e49c7b2fSDavid Howells 	/* This bit is potentially nasty as there's a potential race with
1952e49c7b2fSDavid Howells 	 * afs_d_revalidate{,_rcu}().  We have to change d_fsdata on the dentry
1953e49c7b2fSDavid Howells 	 * to reflect it's new parent's new data_version after the op, but
1954e49c7b2fSDavid Howells 	 * d_revalidate may see old_dentry between the op having taken place
1955e49c7b2fSDavid Howells 	 * and the version being updated.
1956e49c7b2fSDavid Howells 	 *
1957e49c7b2fSDavid Howells 	 * So drop the old_dentry for now to make other threads go through
1958e49c7b2fSDavid Howells 	 * lookup instead - which we hold a lock against.
1959e49c7b2fSDavid Howells 	 */
1960e49c7b2fSDavid Howells 	d_drop(old_dentry);
1961e49c7b2fSDavid Howells 
1962e49c7b2fSDavid Howells 	return afs_do_sync_operation(op);
1963e49c7b2fSDavid Howells 
1964260a9803SDavid Howells error:
1965e49c7b2fSDavid Howells 	return afs_put_operation(op);
1966260a9803SDavid Howells }
1967f3ddee8dSDavid Howells 
1968f3ddee8dSDavid Howells /*
1969f3ddee8dSDavid Howells  * Release a directory page and clean up its private state if it's not busy
1970f3ddee8dSDavid Howells  * - return true if the page can now be released, false if not
1971f3ddee8dSDavid Howells  */
1972f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1973f3ddee8dSDavid Howells {
1974f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1975f3ddee8dSDavid Howells 
19763b6492dfSDavid Howells 	_enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1977f3ddee8dSDavid Howells 
1978f3ddee8dSDavid Howells 	set_page_private(page, 0);
1979f3ddee8dSDavid Howells 	ClearPagePrivate(page);
1980f3ddee8dSDavid Howells 
1981f3ddee8dSDavid Howells 	/* The directory will need reloading. */
1982f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1983f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_relpg);
1984f3ddee8dSDavid Howells 	return 1;
1985f3ddee8dSDavid Howells }
1986f3ddee8dSDavid Howells 
1987f3ddee8dSDavid Howells /*
1988f3ddee8dSDavid Howells  * invalidate part or all of a page
1989f3ddee8dSDavid Howells  * - release a page and clean up its private data if offset is 0 (indicating
1990f3ddee8dSDavid Howells  *   the entire page)
1991f3ddee8dSDavid Howells  */
1992f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1993f3ddee8dSDavid Howells 				   unsigned int length)
1994f3ddee8dSDavid Howells {
1995f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1996f3ddee8dSDavid Howells 
1997f3ddee8dSDavid Howells 	_enter("{%lu},%u,%u", page->index, offset, length);
1998f3ddee8dSDavid Howells 
1999f3ddee8dSDavid Howells 	BUG_ON(!PageLocked(page));
2000f3ddee8dSDavid Howells 
2001f3ddee8dSDavid Howells 	/* The directory will need reloading. */
2002f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
2003f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_inval);
2004f3ddee8dSDavid Howells 
2005f3ddee8dSDavid Howells 	/* we clean up only if the entire page is being invalidated */
2006f3ddee8dSDavid Howells 	if (offset == 0 && length == PAGE_SIZE) {
2007f3ddee8dSDavid Howells 		set_page_private(page, 0);
2008f3ddee8dSDavid Howells 		ClearPagePrivate(page);
2009f3ddee8dSDavid Howells 	}
2010f3ddee8dSDavid Howells }
2011