xref: /openbmc/linux/fs/afs/dir.c (revision b8359153)
11da177e4SLinus Torvalds /* dir.c: AFS filesystem directory handling
21da177e4SLinus Torvalds  *
3f3ddee8dSDavid Howells  * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
41da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
1434286d66SNick Piggin #include <linux/namei.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
16f3ddee8dSDavid Howells #include <linux/swap.h>
1700d3b7a4SDavid Howells #include <linux/ctype.h>
18e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
19f3ddee8dSDavid Howells #include <linux/task_io_accounting_ops.h>
201da177e4SLinus Torvalds #include "internal.h"
21a58823acSDavid Howells #include "afs_fs.h"
224ea219a8SDavid Howells #include "xdr_fs.h"
231da177e4SLinus Torvalds 
24260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
2500cd8dd3SAl Viro 				 unsigned int flags);
261da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file);
271bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx);
280b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
29fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry);
3079ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode);
315cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
325cf9dd55SDavid Howells 				  loff_t fpos, u64 ino, unsigned dtype);
33ac7576f4SMiklos Szeredi static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
34afefdbb2SDavid Howells 			      loff_t fpos, u64 ino, unsigned dtype);
354acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
36ebfc3b49SAl Viro 		      bool excl);
3718bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
38260a9803SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry);
39260a9803SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry);
40260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
41260a9803SDavid Howells 		    struct dentry *dentry);
42260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
43260a9803SDavid Howells 		       const char *content);
44260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
451cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
461cd66c93SMiklos Szeredi 		      unsigned int flags);
47f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
48f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
49f3ddee8dSDavid Howells 				   unsigned int length);
50f3ddee8dSDavid Howells 
51f3ddee8dSDavid Howells static int afs_dir_set_page_dirty(struct page *page)
52f3ddee8dSDavid Howells {
53f3ddee8dSDavid Howells 	BUG(); /* This should never happen. */
54f3ddee8dSDavid Howells }
551da177e4SLinus Torvalds 
564b6f5d20SArjan van de Ven const struct file_operations afs_dir_file_operations = {
571da177e4SLinus Torvalds 	.open		= afs_dir_open,
5800d3b7a4SDavid Howells 	.release	= afs_release,
5929884effSAl Viro 	.iterate_shared	= afs_readdir,
60e8d6c554SDavid Howells 	.lock		= afs_lock,
613222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
64754661f1SArjan van de Ven const struct inode_operations afs_dir_inode_operations = {
65260a9803SDavid Howells 	.create		= afs_create,
66260a9803SDavid Howells 	.lookup		= afs_lookup,
67260a9803SDavid Howells 	.link		= afs_link,
68260a9803SDavid Howells 	.unlink		= afs_unlink,
69260a9803SDavid Howells 	.symlink	= afs_symlink,
70260a9803SDavid Howells 	.mkdir		= afs_mkdir,
71260a9803SDavid Howells 	.rmdir		= afs_rmdir,
722773bf00SMiklos Szeredi 	.rename		= afs_rename,
7300d3b7a4SDavid Howells 	.permission	= afs_permission,
74416351f2SDavid Howells 	.getattr	= afs_getattr,
7531143d5dSDavid Howells 	.setattr	= afs_setattr,
76d3e3b7eaSDavid Howells 	.listxattr	= afs_listxattr,
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
79f3ddee8dSDavid Howells const struct address_space_operations afs_dir_aops = {
80f3ddee8dSDavid Howells 	.set_page_dirty	= afs_dir_set_page_dirty,
81f3ddee8dSDavid Howells 	.releasepage	= afs_dir_releasepage,
82f3ddee8dSDavid Howells 	.invalidatepage	= afs_dir_invalidatepage,
83f3ddee8dSDavid Howells };
84f3ddee8dSDavid Howells 
85d61dcce2SAl Viro const struct dentry_operations afs_fs_dentry_operations = {
861da177e4SLinus Torvalds 	.d_revalidate	= afs_d_revalidate,
871da177e4SLinus Torvalds 	.d_delete	= afs_d_delete,
88260a9803SDavid Howells 	.d_release	= afs_d_release,
89d18610b0SDavid Howells 	.d_automount	= afs_d_automount,
9079ddbfa5SDavid Howells 	.d_iput		= afs_d_iput,
911da177e4SLinus Torvalds };
921da177e4SLinus Torvalds 
935cf9dd55SDavid Howells struct afs_lookup_one_cookie {
945cf9dd55SDavid Howells 	struct dir_context	ctx;
955cf9dd55SDavid Howells 	struct qstr		name;
965cf9dd55SDavid Howells 	bool			found;
975cf9dd55SDavid Howells 	struct afs_fid		fid;
985cf9dd55SDavid Howells };
995cf9dd55SDavid Howells 
100260a9803SDavid Howells struct afs_lookup_cookie {
1011bbae9f8SAl Viro 	struct dir_context	ctx;
1021bbae9f8SAl Viro 	struct qstr		name;
1035cf9dd55SDavid Howells 	bool			found;
1045cf9dd55SDavid Howells 	bool			one_only;
1055cf9dd55SDavid Howells 	unsigned short		nr_fids;
10687182759SDavid Howells 	struct afs_status_cb	*statuses;
1075cf9dd55SDavid Howells 	struct afs_fid		fids[50];
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * check that a directory page is valid
1121da177e4SLinus Torvalds  */
113f3ddee8dSDavid Howells static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
114f3ddee8dSDavid Howells 			       loff_t i_size)
1151da177e4SLinus Torvalds {
11600317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
117f3ddee8dSDavid Howells 	loff_t latter, off;
1181da177e4SLinus Torvalds 	int tmp, qty;
1191da177e4SLinus Torvalds 
120dab17c1aSDavid Howells 	/* Determine how many magic numbers there should be in this page, but
121dab17c1aSDavid Howells 	 * we must take care because the directory may change size under us.
122dab17c1aSDavid Howells 	 */
123dab17c1aSDavid Howells 	off = page_offset(page);
124dab17c1aSDavid Howells 	if (i_size <= off)
125dab17c1aSDavid Howells 		goto checked;
126dab17c1aSDavid Howells 
127dab17c1aSDavid Howells 	latter = i_size - off;
1281da177e4SLinus Torvalds 	if (latter >= PAGE_SIZE)
1291da177e4SLinus Torvalds 		qty = PAGE_SIZE;
1301da177e4SLinus Torvalds 	else
1311da177e4SLinus Torvalds 		qty = latter;
13200317636SDavid Howells 	qty /= sizeof(union afs_xdr_dir_block);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	/* check them */
13563a4681fSDavid Howells 	dbuf = kmap(page);
1361da177e4SLinus Torvalds 	for (tmp = 0; tmp < qty; tmp++) {
13700317636SDavid Howells 		if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
138dab17c1aSDavid Howells 			printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
139f3ddee8dSDavid Howells 			       __func__, dvnode->vfs_inode.i_ino, tmp, qty,
14000317636SDavid Howells 			       ntohs(dbuf->blocks[tmp].hdr.magic));
141f3ddee8dSDavid Howells 			trace_afs_dir_check_failed(dvnode, off, i_size);
14263a4681fSDavid Howells 			kunmap(page);
143f51375cdSDavid Howells 			trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
1441da177e4SLinus Torvalds 			goto error;
1451da177e4SLinus Torvalds 		}
14663a4681fSDavid Howells 
14763a4681fSDavid Howells 		/* Make sure each block is NUL terminated so we can reasonably
14863a4681fSDavid Howells 		 * use string functions on it.  The filenames in the page
14963a4681fSDavid Howells 		 * *should* be NUL-terminated anyway.
15063a4681fSDavid Howells 		 */
15163a4681fSDavid Howells 		((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 
15463a4681fSDavid Howells 	kunmap(page);
15563a4681fSDavid Howells 
156dab17c1aSDavid Howells checked:
157f3ddee8dSDavid Howells 	afs_stat_v(dvnode, n_read_dir);
158be5b82dbSAl Viro 	return true;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds error:
161be5b82dbSAl Viro 	return false;
162ec26815aSDavid Howells }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /*
165445b1028SDavid Howells  * Check the contents of a directory that we've just read.
166445b1028SDavid Howells  */
167445b1028SDavid Howells static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
168445b1028SDavid Howells {
169445b1028SDavid Howells 	struct afs_xdr_dir_page *dbuf;
170445b1028SDavid Howells 	unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
171445b1028SDavid Howells 
172445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++)
173445b1028SDavid Howells 		if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
174445b1028SDavid Howells 			goto bad;
175445b1028SDavid Howells 	return true;
176445b1028SDavid Howells 
177445b1028SDavid Howells bad:
178445b1028SDavid Howells 	pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
179445b1028SDavid Howells 		dvnode->fid.vid, dvnode->fid.vnode,
180445b1028SDavid Howells 		req->file_size, req->len, req->actual_len, req->remain);
181445b1028SDavid Howells 	pr_warn("DIR %llx %x %x %x\n",
182445b1028SDavid Howells 		req->pos, req->index, req->nr_pages, req->offset);
183445b1028SDavid Howells 
184445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++) {
185445b1028SDavid Howells 		dbuf = kmap(req->pages[i]);
186445b1028SDavid Howells 		for (j = 0; j < qty; j++) {
187445b1028SDavid Howells 			union afs_xdr_dir_block *block = &dbuf->blocks[j];
188445b1028SDavid Howells 
189445b1028SDavid Howells 			pr_warn("[%02x] %32phN\n", i * qty + j, block);
190445b1028SDavid Howells 		}
191445b1028SDavid Howells 		kunmap(req->pages[i]);
192445b1028SDavid Howells 	}
193445b1028SDavid Howells 	return false;
194445b1028SDavid Howells }
195445b1028SDavid Howells 
196445b1028SDavid Howells /*
1971da177e4SLinus Torvalds  * open an AFS directory file
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	_enter("{%lu}", inode->i_ino);
2021da177e4SLinus Torvalds 
20300317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
20400317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
2051da177e4SLinus Torvalds 
20608e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
2071da177e4SLinus Torvalds 		return -ENOENT;
2081da177e4SLinus Torvalds 
20900d3b7a4SDavid Howells 	return afs_open(inode, file);
210ec26815aSDavid Howells }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /*
213f3ddee8dSDavid Howells  * Read the directory into the pagecache in one go, scrubbing the previous
214f3ddee8dSDavid Howells  * contents.  The list of pages is returned, pinning them so that they don't
215f3ddee8dSDavid Howells  * get reclaimed during the iteration.
216f3ddee8dSDavid Howells  */
217f3ddee8dSDavid Howells static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
218b61f7dcfSDavid Howells 	__acquires(&dvnode->validate_lock)
219f3ddee8dSDavid Howells {
220f3ddee8dSDavid Howells 	struct afs_read *req;
221f3ddee8dSDavid Howells 	loff_t i_size;
222f3ddee8dSDavid Howells 	int nr_pages, nr_inline, i, n;
223f3ddee8dSDavid Howells 	int ret = -ENOMEM;
224f3ddee8dSDavid Howells 
225f3ddee8dSDavid Howells retry:
226f3ddee8dSDavid Howells 	i_size = i_size_read(&dvnode->vfs_inode);
227f3ddee8dSDavid Howells 	if (i_size < 2048)
228f51375cdSDavid Howells 		return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
229f51375cdSDavid Howells 	if (i_size > 2048 * 1024) {
230f51375cdSDavid Howells 		trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
231f3ddee8dSDavid Howells 		return ERR_PTR(-EFBIG);
232f51375cdSDavid Howells 	}
233f3ddee8dSDavid Howells 
234f3ddee8dSDavid Howells 	_enter("%llu", i_size);
235f3ddee8dSDavid Howells 
236f3ddee8dSDavid Howells 	/* Get a request record to hold the page list.  We want to hold it
237f3ddee8dSDavid Howells 	 * inline if we can, but we don't want to make an order 1 allocation.
238f3ddee8dSDavid Howells 	 */
239f3ddee8dSDavid Howells 	nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
240f3ddee8dSDavid Howells 	nr_inline = nr_pages;
241f3ddee8dSDavid Howells 	if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
242f3ddee8dSDavid Howells 		nr_inline = 0;
243f3ddee8dSDavid Howells 
244f3ddee8dSDavid Howells 	req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
245f3ddee8dSDavid Howells 		      GFP_KERNEL);
246f3ddee8dSDavid Howells 	if (!req)
247f3ddee8dSDavid Howells 		return ERR_PTR(-ENOMEM);
248f3ddee8dSDavid Howells 
249f3ddee8dSDavid Howells 	refcount_set(&req->usage, 1);
250f3ddee8dSDavid Howells 	req->nr_pages = nr_pages;
251f3ddee8dSDavid Howells 	req->actual_len = i_size; /* May change */
252f3ddee8dSDavid Howells 	req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
253f3ddee8dSDavid Howells 	req->data_version = dvnode->status.data_version; /* May change */
254f3ddee8dSDavid Howells 	if (nr_inline > 0) {
255f3ddee8dSDavid Howells 		req->pages = req->array;
256f3ddee8dSDavid Howells 	} else {
257f3ddee8dSDavid Howells 		req->pages = kcalloc(nr_pages, sizeof(struct page *),
258f3ddee8dSDavid Howells 				     GFP_KERNEL);
259f3ddee8dSDavid Howells 		if (!req->pages)
260f3ddee8dSDavid Howells 			goto error;
261f3ddee8dSDavid Howells 	}
262f3ddee8dSDavid Howells 
263f3ddee8dSDavid Howells 	/* Get a list of all the pages that hold or will hold the directory
264f3ddee8dSDavid Howells 	 * content.  We need to fill in any gaps that we might find where the
265f3ddee8dSDavid Howells 	 * memory reclaimer has been at work.  If there are any gaps, we will
266f3ddee8dSDavid Howells 	 * need to reread the entire directory contents.
267f3ddee8dSDavid Howells 	 */
268f3ddee8dSDavid Howells 	i = 0;
269f3ddee8dSDavid Howells 	do {
270f3ddee8dSDavid Howells 		n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
271f3ddee8dSDavid Howells 					  req->nr_pages - i,
272f3ddee8dSDavid Howells 					  req->pages + i);
273f3ddee8dSDavid Howells 		_debug("find %u at %u/%u", n, i, req->nr_pages);
274f3ddee8dSDavid Howells 		if (n == 0) {
275f3ddee8dSDavid Howells 			gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
276f3ddee8dSDavid Howells 
277f3ddee8dSDavid Howells 			if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
278f3ddee8dSDavid Howells 				afs_stat_v(dvnode, n_inval);
279f3ddee8dSDavid Howells 
280f3ddee8dSDavid Howells 			ret = -ENOMEM;
281f3ddee8dSDavid Howells 			req->pages[i] = __page_cache_alloc(gfp);
282f3ddee8dSDavid Howells 			if (!req->pages[i])
283f3ddee8dSDavid Howells 				goto error;
284f3ddee8dSDavid Howells 			ret = add_to_page_cache_lru(req->pages[i],
285f3ddee8dSDavid Howells 						    dvnode->vfs_inode.i_mapping,
286f3ddee8dSDavid Howells 						    i, gfp);
287f3ddee8dSDavid Howells 			if (ret < 0)
288f3ddee8dSDavid Howells 				goto error;
289f3ddee8dSDavid Howells 
290f3ddee8dSDavid Howells 			set_page_private(req->pages[i], 1);
291f3ddee8dSDavid Howells 			SetPagePrivate(req->pages[i]);
292f3ddee8dSDavid Howells 			unlock_page(req->pages[i]);
293f3ddee8dSDavid Howells 			i++;
294f3ddee8dSDavid Howells 		} else {
295f3ddee8dSDavid Howells 			i += n;
296f3ddee8dSDavid Howells 		}
297f3ddee8dSDavid Howells 	} while (i < req->nr_pages);
298f3ddee8dSDavid Howells 
299f3ddee8dSDavid Howells 	/* If we're going to reload, we need to lock all the pages to prevent
300f3ddee8dSDavid Howells 	 * races.
301f3ddee8dSDavid Howells 	 */
302f3ddee8dSDavid Howells 	ret = -ERESTARTSYS;
303b61f7dcfSDavid Howells 	if (down_read_killable(&dvnode->validate_lock) < 0)
304b61f7dcfSDavid Howells 		goto error;
305f3ddee8dSDavid Howells 
306f3ddee8dSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
307f3ddee8dSDavid Howells 		goto success;
308f3ddee8dSDavid Howells 
309b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
310b61f7dcfSDavid Howells 	if (down_write_killable(&dvnode->validate_lock) < 0)
311b61f7dcfSDavid Howells 		goto error;
312b61f7dcfSDavid Howells 
313b61f7dcfSDavid Howells 	if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
31499987c56SDavid Howells 		trace_afs_reload_dir(dvnode);
315f3ddee8dSDavid Howells 		ret = afs_fetch_data(dvnode, key, req);
316f3ddee8dSDavid Howells 		if (ret < 0)
317b61f7dcfSDavid Howells 			goto error_unlock;
318f3ddee8dSDavid Howells 
319f3ddee8dSDavid Howells 		task_io_account_read(PAGE_SIZE * req->nr_pages);
320f3ddee8dSDavid Howells 
321f3ddee8dSDavid Howells 		if (req->len < req->file_size)
322f3ddee8dSDavid Howells 			goto content_has_grown;
323f3ddee8dSDavid Howells 
324f3ddee8dSDavid Howells 		/* Validate the data we just read. */
325f3ddee8dSDavid Howells 		ret = -EIO;
326445b1028SDavid Howells 		if (!afs_dir_check_pages(dvnode, req))
327b61f7dcfSDavid Howells 			goto error_unlock;
328f3ddee8dSDavid Howells 
329f3ddee8dSDavid Howells 		// TODO: Trim excess pages
330f3ddee8dSDavid Howells 
331f3ddee8dSDavid Howells 		set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
332f3ddee8dSDavid Howells 	}
333f3ddee8dSDavid Howells 
334b61f7dcfSDavid Howells 	downgrade_write(&dvnode->validate_lock);
335f3ddee8dSDavid Howells success:
336f3ddee8dSDavid Howells 	return req;
337f3ddee8dSDavid Howells 
338f3ddee8dSDavid Howells error_unlock:
339b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
340f3ddee8dSDavid Howells error:
341f3ddee8dSDavid Howells 	afs_put_read(req);
342f3ddee8dSDavid Howells 	_leave(" = %d", ret);
343f3ddee8dSDavid Howells 	return ERR_PTR(ret);
344f3ddee8dSDavid Howells 
345f3ddee8dSDavid Howells content_has_grown:
346b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
347f3ddee8dSDavid Howells 	afs_put_read(req);
348f3ddee8dSDavid Howells 	goto retry;
349f3ddee8dSDavid Howells }
350f3ddee8dSDavid Howells 
351f3ddee8dSDavid Howells /*
3521da177e4SLinus Torvalds  * deal with one block in an AFS directory
3531da177e4SLinus Torvalds  */
354f51375cdSDavid Howells static int afs_dir_iterate_block(struct afs_vnode *dvnode,
355f51375cdSDavid Howells 				 struct dir_context *ctx,
35600317636SDavid Howells 				 union afs_xdr_dir_block *block,
3571bbae9f8SAl Viro 				 unsigned blkoff)
3581da177e4SLinus Torvalds {
35900317636SDavid Howells 	union afs_xdr_dirent *dire;
3601da177e4SLinus Torvalds 	unsigned offset, next, curr;
3611da177e4SLinus Torvalds 	size_t nlen;
3621bbae9f8SAl Viro 	int tmp;
3631da177e4SLinus Torvalds 
3641bbae9f8SAl Viro 	_enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
3651da177e4SLinus Torvalds 
36600317636SDavid Howells 	curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	/* walk through the block, an entry at a time */
3694ea219a8SDavid Howells 	for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
3704ea219a8SDavid Howells 	     offset < AFS_DIR_SLOTS_PER_BLOCK;
3711da177e4SLinus Torvalds 	     offset = next
3721da177e4SLinus Torvalds 	     ) {
3731da177e4SLinus Torvalds 		next = offset + 1;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 		/* skip entries marked unused in the bitmap */
37600317636SDavid Howells 		if (!(block->hdr.bitmap[offset / 8] &
3771da177e4SLinus Torvalds 		      (1 << (offset % 8)))) {
3785b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: unused",
37900317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block), offset);
3801da177e4SLinus Torvalds 			if (offset >= curr)
3811bbae9f8SAl Viro 				ctx->pos = blkoff +
38200317636SDavid Howells 					next * sizeof(union afs_xdr_dirent);
3831da177e4SLinus Torvalds 			continue;
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		/* got a valid entry */
3871da177e4SLinus Torvalds 		dire = &block->dirents[offset];
3881da177e4SLinus Torvalds 		nlen = strnlen(dire->u.name,
3891da177e4SLinus Torvalds 			       sizeof(*block) -
39000317636SDavid Howells 			       offset * sizeof(union afs_xdr_dirent));
3911da177e4SLinus Torvalds 
3925b5e0928SAlexey Dobriyan 		_debug("ENT[%zu.%u]: %s %zu \"%s\"",
39300317636SDavid Howells 		       blkoff / sizeof(union afs_xdr_dir_block), offset,
3941da177e4SLinus Torvalds 		       (offset < curr ? "skip" : "fill"),
3951da177e4SLinus Torvalds 		       nlen, dire->u.name);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 		/* work out where the next possible entry is */
39800317636SDavid Howells 		for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
3994ea219a8SDavid Howells 			if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
4005b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
4011da177e4SLinus Torvalds 				       " %u travelled beyond end dir block"
4025b5e0928SAlexey Dobriyan 				       " (len %u/%zu)",
40300317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4041da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
405f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_over_end);
4061da177e4SLinus Torvalds 			}
40700317636SDavid Howells 			if (!(block->hdr.bitmap[next / 8] &
4081da177e4SLinus Torvalds 			      (1 << (next % 8)))) {
4095b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
4105b5e0928SAlexey Dobriyan 				       " %u unmarked extension (len %u/%zu)",
41100317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4121da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
413f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
4141da177e4SLinus Torvalds 			}
4151da177e4SLinus Torvalds 
4165b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: ext %u/%zu",
41700317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block),
4181da177e4SLinus Torvalds 			       next, tmp, nlen);
4191da177e4SLinus Torvalds 			next++;
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 		/* skip if starts before the current position */
4231da177e4SLinus Torvalds 		if (offset < curr)
4241da177e4SLinus Torvalds 			continue;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 		/* found the next entry */
4271bbae9f8SAl Viro 		if (!dir_emit(ctx, dire->u.name, nlen,
4281da177e4SLinus Torvalds 			      ntohl(dire->u.vnode),
4295cf9dd55SDavid Howells 			      (ctx->actor == afs_lookup_filldir ||
4305cf9dd55SDavid Howells 			       ctx->actor == afs_lookup_one_filldir)?
4311bbae9f8SAl Viro 			      ntohl(dire->u.unique) : DT_UNKNOWN)) {
4321da177e4SLinus Torvalds 			_leave(" = 0 [full]");
4331da177e4SLinus Torvalds 			return 0;
4341da177e4SLinus Torvalds 		}
4351da177e4SLinus Torvalds 
43600317636SDavid Howells 		ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	_leave(" = 1 [more]");
4401da177e4SLinus Torvalds 	return 1;
441ec26815aSDavid Howells }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds /*
44408e0e7c8SDavid Howells  * iterate through the data blob that lists the contents of an AFS directory
4451da177e4SLinus Torvalds  */
4461bbae9f8SAl Viro static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
4471bbae9f8SAl Viro 			   struct key *key)
4481da177e4SLinus Torvalds {
449f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
45000317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
45100317636SDavid Howells 	union afs_xdr_dir_block *dblock;
452f3ddee8dSDavid Howells 	struct afs_read *req;
4531da177e4SLinus Torvalds 	struct page *page;
4541da177e4SLinus Torvalds 	unsigned blkoff, limit;
4551da177e4SLinus Torvalds 	int ret;
4561da177e4SLinus Torvalds 
4571bbae9f8SAl Viro 	_enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
4581da177e4SLinus Torvalds 
45908e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
4601da177e4SLinus Torvalds 		_leave(" = -ESTALE");
4611da177e4SLinus Torvalds 		return -ESTALE;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
464f3ddee8dSDavid Howells 	req = afs_read_dir(dvnode, key);
465f3ddee8dSDavid Howells 	if (IS_ERR(req))
466f3ddee8dSDavid Howells 		return PTR_ERR(req);
467f3ddee8dSDavid Howells 
4681da177e4SLinus Torvalds 	/* round the file position up to the next entry boundary */
46900317636SDavid Howells 	ctx->pos += sizeof(union afs_xdr_dirent) - 1;
47000317636SDavid Howells 	ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	/* walk through the blocks in sequence */
4731da177e4SLinus Torvalds 	ret = 0;
474f3ddee8dSDavid Howells 	while (ctx->pos < req->actual_len) {
47500317636SDavid Howells 		blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
4761da177e4SLinus Torvalds 
477f3ddee8dSDavid Howells 		/* Fetch the appropriate page from the directory and re-add it
478f3ddee8dSDavid Howells 		 * to the LRU.
479f3ddee8dSDavid Howells 		 */
480f3ddee8dSDavid Howells 		page = req->pages[blkoff / PAGE_SIZE];
481f3ddee8dSDavid Howells 		if (!page) {
482f51375cdSDavid Howells 			ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
4831da177e4SLinus Torvalds 			break;
4841da177e4SLinus Torvalds 		}
485f3ddee8dSDavid Howells 		mark_page_accessed(page);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 		limit = blkoff & ~(PAGE_SIZE - 1);
4881da177e4SLinus Torvalds 
489f3ddee8dSDavid Howells 		dbuf = kmap(page);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 		/* deal with the individual blocks stashed on this page */
4921da177e4SLinus Torvalds 		do {
4931da177e4SLinus Torvalds 			dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
49400317636SDavid Howells 					       sizeof(union afs_xdr_dir_block)];
495f51375cdSDavid Howells 			ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
4961da177e4SLinus Torvalds 			if (ret != 1) {
497f3ddee8dSDavid Howells 				kunmap(page);
4981da177e4SLinus Torvalds 				goto out;
4991da177e4SLinus Torvalds 			}
5001da177e4SLinus Torvalds 
50100317636SDavid Howells 			blkoff += sizeof(union afs_xdr_dir_block);
5021da177e4SLinus Torvalds 
5031bbae9f8SAl Viro 		} while (ctx->pos < dir->i_size && blkoff < limit);
5041da177e4SLinus Torvalds 
505f3ddee8dSDavid Howells 		kunmap(page);
5061da177e4SLinus Torvalds 		ret = 0;
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds out:
510b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
511f3ddee8dSDavid Howells 	afs_put_read(req);
5121da177e4SLinus Torvalds 	_leave(" = %d", ret);
5131da177e4SLinus Torvalds 	return ret;
514ec26815aSDavid Howells }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds /*
5171da177e4SLinus Torvalds  * read an AFS directory
5181da177e4SLinus Torvalds  */
5191bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx)
5201da177e4SLinus Torvalds {
521215804a9SDavid Howells 	return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
522ec26815aSDavid Howells }
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds /*
5255cf9dd55SDavid Howells  * Search the directory for a single name
5261da177e4SLinus Torvalds  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5271da177e4SLinus Torvalds  *   uniquifier through dtype
5281da177e4SLinus Torvalds  */
5295cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
530ac7576f4SMiklos Szeredi 				  int nlen, loff_t fpos, u64 ino, unsigned dtype)
5311da177e4SLinus Torvalds {
5325cf9dd55SDavid Howells 	struct afs_lookup_one_cookie *cookie =
5335cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_one_cookie, ctx);
5341da177e4SLinus Torvalds 
5351bbae9f8SAl Viro 	_enter("{%s,%u},%s,%u,,%llu,%u",
5361bbae9f8SAl Viro 	       cookie->name.name, cookie->name.len, name, nlen,
537ba3e0e1aSDavid S. Miller 	       (unsigned long long) ino, dtype);
5381da177e4SLinus Torvalds 
53908e0e7c8SDavid Howells 	/* insanity checks first */
54000317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
54100317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
54208e0e7c8SDavid Howells 
5431bbae9f8SAl Viro 	if (cookie->name.len != nlen ||
5441bbae9f8SAl Viro 	    memcmp(cookie->name.name, name, nlen) != 0) {
5451da177e4SLinus Torvalds 		_leave(" = 0 [no]");
5461da177e4SLinus Torvalds 		return 0;
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	cookie->fid.vnode = ino;
5501da177e4SLinus Torvalds 	cookie->fid.unique = dtype;
5511da177e4SLinus Torvalds 	cookie->found = 1;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	_leave(" = -1 [found]");
5541da177e4SLinus Torvalds 	return -1;
555ec26815aSDavid Howells }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /*
5585cf9dd55SDavid Howells  * Do a lookup of a single name in a directory
559260a9803SDavid Howells  * - just returns the FID the dentry name maps to if found
5601da177e4SLinus Torvalds  */
5615cf9dd55SDavid Howells static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
56200d3b7a4SDavid Howells 			     struct afs_fid *fid, struct key *key)
5631da177e4SLinus Torvalds {
5641bbae9f8SAl Viro 	struct afs_super_info *as = dir->i_sb->s_fs_info;
5655cf9dd55SDavid Howells 	struct afs_lookup_one_cookie cookie = {
5665cf9dd55SDavid Howells 		.ctx.actor = afs_lookup_one_filldir,
5671bbae9f8SAl Viro 		.name = dentry->d_name,
5681bbae9f8SAl Viro 		.fid.vid = as->volume->vid
5691bbae9f8SAl Viro 	};
5701da177e4SLinus Torvalds 	int ret;
5711da177e4SLinus Torvalds 
572a455589fSAl Viro 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	/* search the directory */
5751bbae9f8SAl Viro 	ret = afs_dir_iterate(dir, &cookie.ctx, key);
5761da177e4SLinus Torvalds 	if (ret < 0) {
57708e0e7c8SDavid Howells 		_leave(" = %d [iter]", ret);
57808e0e7c8SDavid Howells 		return ret;
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	ret = -ENOENT;
5821da177e4SLinus Torvalds 	if (!cookie.found) {
58308e0e7c8SDavid Howells 		_leave(" = -ENOENT [not found]");
58408e0e7c8SDavid Howells 		return -ENOENT;
58508e0e7c8SDavid Howells 	}
58608e0e7c8SDavid Howells 
58708e0e7c8SDavid Howells 	*fid = cookie.fid;
5883b6492dfSDavid Howells 	_leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
58908e0e7c8SDavid Howells 	return 0;
59008e0e7c8SDavid Howells }
59108e0e7c8SDavid Howells 
59208e0e7c8SDavid Howells /*
5935cf9dd55SDavid Howells  * search the directory for a name
5945cf9dd55SDavid Howells  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5955cf9dd55SDavid Howells  *   uniquifier through dtype
5965cf9dd55SDavid Howells  */
5975cf9dd55SDavid Howells static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
5985cf9dd55SDavid Howells 			      int nlen, loff_t fpos, u64 ino, unsigned dtype)
5995cf9dd55SDavid Howells {
6005cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie =
6015cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_cookie, ctx);
6025cf9dd55SDavid Howells 	int ret;
6035cf9dd55SDavid Howells 
6045cf9dd55SDavid Howells 	_enter("{%s,%u},%s,%u,,%llu,%u",
6055cf9dd55SDavid Howells 	       cookie->name.name, cookie->name.len, name, nlen,
6065cf9dd55SDavid Howells 	       (unsigned long long) ino, dtype);
6075cf9dd55SDavid Howells 
6085cf9dd55SDavid Howells 	/* insanity checks first */
60900317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
61000317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
6115cf9dd55SDavid Howells 
6125cf9dd55SDavid Howells 	if (cookie->found) {
6135cf9dd55SDavid Howells 		if (cookie->nr_fids < 50) {
6145cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].vnode	= ino;
6155cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].unique	= dtype;
6165cf9dd55SDavid Howells 			cookie->nr_fids++;
6175cf9dd55SDavid Howells 		}
6185cf9dd55SDavid Howells 	} else if (cookie->name.len == nlen &&
6195cf9dd55SDavid Howells 		   memcmp(cookie->name.name, name, nlen) == 0) {
6205cf9dd55SDavid Howells 		cookie->fids[0].vnode	= ino;
6215cf9dd55SDavid Howells 		cookie->fids[0].unique	= dtype;
6225cf9dd55SDavid Howells 		cookie->found = 1;
6235cf9dd55SDavid Howells 		if (cookie->one_only)
6245cf9dd55SDavid Howells 			return -1;
6255cf9dd55SDavid Howells 	}
6265cf9dd55SDavid Howells 
6275cf9dd55SDavid Howells 	ret = cookie->nr_fids >= 50 ? -1 : 0;
6285cf9dd55SDavid Howells 	_leave(" = %d", ret);
6295cf9dd55SDavid Howells 	return ret;
6305cf9dd55SDavid Howells }
6315cf9dd55SDavid Howells 
6325cf9dd55SDavid Howells /*
6335cf9dd55SDavid Howells  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
6345cf9dd55SDavid Howells  * files in one go and create inodes for them.  The inode of the file we were
6355cf9dd55SDavid Howells  * asked for is returned.
6365cf9dd55SDavid Howells  */
6375cf9dd55SDavid Howells static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
6385cf9dd55SDavid Howells 				   struct key *key)
6395cf9dd55SDavid Howells {
6405cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie;
641f642404aSDavid Howells 	struct afs_cb_interest *dcbi, *cbi = NULL;
6425cf9dd55SDavid Howells 	struct afs_super_info *as = dir->i_sb->s_fs_info;
64387182759SDavid Howells 	struct afs_status_cb *scb;
644b8359153SDavid Howells 	struct afs_iget_data iget_data;
6455cf9dd55SDavid Howells 	struct afs_fs_cursor fc;
646f642404aSDavid Howells 	struct afs_server *server;
6475cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
6485cf9dd55SDavid Howells 	struct inode *inode = NULL;
6495cf9dd55SDavid Howells 	int ret, i;
6505cf9dd55SDavid Howells 
6515cf9dd55SDavid Howells 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
6525cf9dd55SDavid Howells 
6535cf9dd55SDavid Howells 	cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
6545cf9dd55SDavid Howells 	if (!cookie)
6555cf9dd55SDavid Howells 		return ERR_PTR(-ENOMEM);
6565cf9dd55SDavid Howells 
6575cf9dd55SDavid Howells 	cookie->ctx.actor = afs_lookup_filldir;
6585cf9dd55SDavid Howells 	cookie->name = dentry->d_name;
6595cf9dd55SDavid Howells 	cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
6605cf9dd55SDavid Howells 
6615cf9dd55SDavid Howells 	read_seqlock_excl(&dvnode->cb_lock);
662f642404aSDavid Howells 	dcbi = rcu_dereference_protected(dvnode->cb_interest,
663f642404aSDavid Howells 					 lockdep_is_held(&dvnode->cb_lock.lock));
664f642404aSDavid Howells 	if (dcbi) {
665f642404aSDavid Howells 		server = dcbi->server;
666f642404aSDavid Howells 		if (server &&
667f642404aSDavid Howells 		    test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
6685cf9dd55SDavid Howells 			cookie->one_only = true;
669f642404aSDavid Howells 	}
6705cf9dd55SDavid Howells 	read_sequnlock_excl(&dvnode->cb_lock);
6715cf9dd55SDavid Howells 
6725cf9dd55SDavid Howells 	for (i = 0; i < 50; i++)
6735cf9dd55SDavid Howells 		cookie->fids[i].vid = as->volume->vid;
6745cf9dd55SDavid Howells 
6755cf9dd55SDavid Howells 	/* search the directory */
6765cf9dd55SDavid Howells 	ret = afs_dir_iterate(dir, &cookie->ctx, key);
6775cf9dd55SDavid Howells 	if (ret < 0) {
6785cf9dd55SDavid Howells 		inode = ERR_PTR(ret);
6795cf9dd55SDavid Howells 		goto out;
6805cf9dd55SDavid Howells 	}
6815cf9dd55SDavid Howells 
6825cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOENT);
6835cf9dd55SDavid Howells 	if (!cookie->found)
6845cf9dd55SDavid Howells 		goto out;
6855cf9dd55SDavid Howells 
6865cf9dd55SDavid Howells 	/* Check to see if we already have an inode for the primary fid. */
687b8359153SDavid Howells 	iget_data.fid = cookie->fids[0];
688b8359153SDavid Howells 	iget_data.volume = dvnode->volume;
689b8359153SDavid Howells 	iget_data.cb_v_break = dvnode->volume->cb_v_break;
690b8359153SDavid Howells 	iget_data.cb_s_break = 0;
691b8359153SDavid Howells 	inode = ilookup5(dir->i_sb, cookie->fids[0].vnode,
692b8359153SDavid Howells 			 afs_iget5_test, &iget_data);
6935cf9dd55SDavid Howells 	if (inode)
6945cf9dd55SDavid Howells 		goto out;
6955cf9dd55SDavid Howells 
6965cf9dd55SDavid Howells 	/* Need space for examining all the selected files */
6975cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOMEM);
69887182759SDavid Howells 	cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb),
6995cf9dd55SDavid Howells 				    GFP_KERNEL);
7005cf9dd55SDavid Howells 	if (!cookie->statuses)
7015cf9dd55SDavid Howells 		goto out;
7025cf9dd55SDavid Howells 
7035cf9dd55SDavid Howells 	/* Try FS.InlineBulkStatus first.  Abort codes for the individual
7045cf9dd55SDavid Howells 	 * lookups contained therein are stored in the reply without aborting
7055cf9dd55SDavid Howells 	 * the whole operation.
7065cf9dd55SDavid Howells 	 */
7075cf9dd55SDavid Howells 	if (cookie->one_only)
7085cf9dd55SDavid Howells 		goto no_inline_bulk_status;
7095cf9dd55SDavid Howells 
7105cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
71120b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
7125cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
7135cf9dd55SDavid Howells 			if (test_bit(AFS_SERVER_FL_NO_IBULK,
7145cf9dd55SDavid Howells 				      &fc.cbi->server->flags)) {
7155cf9dd55SDavid Howells 				fc.ac.abort_code = RX_INVALID_OPERATION;
7165cf9dd55SDavid Howells 				fc.ac.error = -ECONNABORTED;
7175cf9dd55SDavid Howells 				break;
7185cf9dd55SDavid Howells 			}
719b8359153SDavid Howells 			iget_data.cb_v_break = dvnode->volume->cb_v_break;
720b8359153SDavid Howells 			iget_data.cb_s_break = fc.cbi->server->cb_s_break;
7215cf9dd55SDavid Howells 			afs_fs_inline_bulk_status(&fc,
7225cf9dd55SDavid Howells 						  afs_v2net(dvnode),
7235cf9dd55SDavid Howells 						  cookie->fids,
7245cf9dd55SDavid Howells 						  cookie->statuses,
7255cf9dd55SDavid Howells 						  cookie->nr_fids, NULL);
7265cf9dd55SDavid Howells 		}
7275cf9dd55SDavid Howells 
7285cf9dd55SDavid Howells 		if (fc.ac.error == 0)
7295cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
7305cf9dd55SDavid Howells 		if (fc.ac.abort_code == RX_INVALID_OPERATION)
7315cf9dd55SDavid Howells 			set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
7325cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
7335cf9dd55SDavid Howells 	}
7345cf9dd55SDavid Howells 
7355cf9dd55SDavid Howells 	if (!IS_ERR(inode))
7365cf9dd55SDavid Howells 		goto success;
7375cf9dd55SDavid Howells 	if (fc.ac.abort_code != RX_INVALID_OPERATION)
7385cf9dd55SDavid Howells 		goto out_c;
7395cf9dd55SDavid Howells 
7405cf9dd55SDavid Howells no_inline_bulk_status:
7415cf9dd55SDavid Howells 	/* We could try FS.BulkStatus next, but this aborts the entire op if
7425cf9dd55SDavid Howells 	 * any of the lookups fails - so, for the moment, revert to
7435cf9dd55SDavid Howells 	 * FS.FetchStatus for just the primary fid.
7445cf9dd55SDavid Howells 	 */
7455cf9dd55SDavid Howells 	cookie->nr_fids = 1;
7465cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
74720b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
7485cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
749b8359153SDavid Howells 			iget_data.cb_v_break = dvnode->volume->cb_v_break;
750b8359153SDavid Howells 			iget_data.cb_s_break = fc.cbi->server->cb_s_break;
75187182759SDavid Howells 			scb = &cookie->statuses[0];
7525cf9dd55SDavid Howells 			afs_fs_fetch_status(&fc,
7535cf9dd55SDavid Howells 					    afs_v2net(dvnode),
7545cf9dd55SDavid Howells 					    cookie->fids,
755a58823acSDavid Howells 					    scb,
7565cf9dd55SDavid Howells 					    NULL);
7575cf9dd55SDavid Howells 		}
7585cf9dd55SDavid Howells 
7595cf9dd55SDavid Howells 		if (fc.ac.error == 0)
7605cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
7615cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
7625cf9dd55SDavid Howells 	}
7635cf9dd55SDavid Howells 
7645cf9dd55SDavid Howells 	if (IS_ERR(inode))
7655cf9dd55SDavid Howells 		goto out_c;
7665cf9dd55SDavid Howells 
7675cf9dd55SDavid Howells 	for (i = 0; i < cookie->nr_fids; i++)
76887182759SDavid Howells 		cookie->statuses[i].status.abort_code = 0;
7695cf9dd55SDavid Howells 
7705cf9dd55SDavid Howells success:
7715cf9dd55SDavid Howells 	/* Turn all the files into inodes and save the first one - which is the
7725cf9dd55SDavid Howells 	 * one we actually want.
7735cf9dd55SDavid Howells 	 */
77487182759SDavid Howells 	scb = &cookie->statuses[0];
77587182759SDavid Howells 	if (scb->status.abort_code != 0)
77687182759SDavid Howells 		inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code));
7775cf9dd55SDavid Howells 
7785cf9dd55SDavid Howells 	for (i = 0; i < cookie->nr_fids; i++) {
77987182759SDavid Howells 		struct afs_status_cb *scb = &cookie->statuses[i];
7805cf9dd55SDavid Howells 		struct inode *ti;
7815cf9dd55SDavid Howells 
78287182759SDavid Howells 		if (scb->status.abort_code != 0)
7835cf9dd55SDavid Howells 			continue;
7845cf9dd55SDavid Howells 
785b8359153SDavid Howells 		iget_data.fid = cookie->fids[i];
786b8359153SDavid Howells 		ti = afs_iget(dir->i_sb, key, &iget_data, scb, cbi, dvnode);
7875cf9dd55SDavid Howells 		if (i == 0) {
7885cf9dd55SDavid Howells 			inode = ti;
7895cf9dd55SDavid Howells 		} else {
7905cf9dd55SDavid Howells 			if (!IS_ERR(ti))
7915cf9dd55SDavid Howells 				iput(ti);
7925cf9dd55SDavid Howells 		}
7935cf9dd55SDavid Howells 	}
7945cf9dd55SDavid Howells 
7955cf9dd55SDavid Howells out_c:
7965cf9dd55SDavid Howells 	afs_put_cb_interest(afs_v2net(dvnode), cbi);
79787182759SDavid Howells 	kvfree(cookie->statuses);
7985cf9dd55SDavid Howells out:
7995cf9dd55SDavid Howells 	kfree(cookie);
8005cf9dd55SDavid Howells 	return inode;
8015cf9dd55SDavid Howells }
8025cf9dd55SDavid Howells 
8035cf9dd55SDavid Howells /*
8046f8880d8SDavid Howells  * Look up an entry in a directory with @sys substitution.
8056f8880d8SDavid Howells  */
8066f8880d8SDavid Howells static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
8076f8880d8SDavid Howells 				       struct key *key)
8086f8880d8SDavid Howells {
8096f8880d8SDavid Howells 	struct afs_sysnames *subs;
8106f8880d8SDavid Howells 	struct afs_net *net = afs_i2net(dir);
8116f8880d8SDavid Howells 	struct dentry *ret;
8126f8880d8SDavid Howells 	char *buf, *p, *name;
8136f8880d8SDavid Howells 	int len, i;
8146f8880d8SDavid Howells 
8156f8880d8SDavid Howells 	_enter("");
8166f8880d8SDavid Howells 
8176f8880d8SDavid Howells 	ret = ERR_PTR(-ENOMEM);
8186f8880d8SDavid Howells 	p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
8196f8880d8SDavid Howells 	if (!buf)
8206f8880d8SDavid Howells 		goto out_p;
8216f8880d8SDavid Howells 	if (dentry->d_name.len > 4) {
8226f8880d8SDavid Howells 		memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
8236f8880d8SDavid Howells 		p += dentry->d_name.len - 4;
8246f8880d8SDavid Howells 	}
8256f8880d8SDavid Howells 
8266f8880d8SDavid Howells 	/* There is an ordered list of substitutes that we have to try. */
8276f8880d8SDavid Howells 	read_lock(&net->sysnames_lock);
8286f8880d8SDavid Howells 	subs = net->sysnames;
8296f8880d8SDavid Howells 	refcount_inc(&subs->usage);
8306f8880d8SDavid Howells 	read_unlock(&net->sysnames_lock);
8316f8880d8SDavid Howells 
8326f8880d8SDavid Howells 	for (i = 0; i < subs->nr; i++) {
8336f8880d8SDavid Howells 		name = subs->subs[i];
8346f8880d8SDavid Howells 		len = dentry->d_name.len - 4 + strlen(name);
8356f8880d8SDavid Howells 		if (len >= AFSNAMEMAX) {
8366f8880d8SDavid Howells 			ret = ERR_PTR(-ENAMETOOLONG);
8376f8880d8SDavid Howells 			goto out_s;
8386f8880d8SDavid Howells 		}
8396f8880d8SDavid Howells 
8406f8880d8SDavid Howells 		strcpy(p, name);
8416f8880d8SDavid Howells 		ret = lookup_one_len(buf, dentry->d_parent, len);
8426f8880d8SDavid Howells 		if (IS_ERR(ret) || d_is_positive(ret))
8436f8880d8SDavid Howells 			goto out_s;
8446f8880d8SDavid Howells 		dput(ret);
8456f8880d8SDavid Howells 	}
8466f8880d8SDavid Howells 
8476f8880d8SDavid Howells 	/* We don't want to d_add() the @sys dentry here as we don't want to
8486f8880d8SDavid Howells 	 * the cached dentry to hide changes to the sysnames list.
8496f8880d8SDavid Howells 	 */
8506f8880d8SDavid Howells 	ret = NULL;
8516f8880d8SDavid Howells out_s:
8526f8880d8SDavid Howells 	afs_put_sysnames(subs);
8536f8880d8SDavid Howells 	kfree(buf);
8546f8880d8SDavid Howells out_p:
8556f8880d8SDavid Howells 	key_put(key);
8566f8880d8SDavid Howells 	return ret;
8576f8880d8SDavid Howells }
8586f8880d8SDavid Howells 
8596f8880d8SDavid Howells /*
86008e0e7c8SDavid Howells  * look up an entry in a directory
86108e0e7c8SDavid Howells  */
862260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
86300cd8dd3SAl Viro 				 unsigned int flags)
86408e0e7c8SDavid Howells {
8655cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
86608e0e7c8SDavid Howells 	struct inode *inode;
86734b2a88fSAl Viro 	struct dentry *d;
86800d3b7a4SDavid Howells 	struct key *key;
86908e0e7c8SDavid Howells 	int ret;
87008e0e7c8SDavid Howells 
8713b6492dfSDavid Howells 	_enter("{%llx:%llu},%p{%pd},",
8725cf9dd55SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
873260a9803SDavid Howells 
8742b0143b5SDavid Howells 	ASSERTCMP(d_inode(dentry), ==, NULL);
87508e0e7c8SDavid Howells 
87645222b9eSDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX) {
87708e0e7c8SDavid Howells 		_leave(" = -ENAMETOOLONG");
87808e0e7c8SDavid Howells 		return ERR_PTR(-ENAMETOOLONG);
87908e0e7c8SDavid Howells 	}
88008e0e7c8SDavid Howells 
8815cf9dd55SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
88208e0e7c8SDavid Howells 		_leave(" = -ESTALE");
88308e0e7c8SDavid Howells 		return ERR_PTR(-ESTALE);
88408e0e7c8SDavid Howells 	}
88508e0e7c8SDavid Howells 
8865cf9dd55SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
88700d3b7a4SDavid Howells 	if (IS_ERR(key)) {
88800d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
889e231c2eeSDavid Howells 		return ERR_CAST(key);
89000d3b7a4SDavid Howells 	}
89100d3b7a4SDavid Howells 
8925cf9dd55SDavid Howells 	ret = afs_validate(dvnode, key);
89308e0e7c8SDavid Howells 	if (ret < 0) {
89400d3b7a4SDavid Howells 		key_put(key);
895260a9803SDavid Howells 		_leave(" = %d [val]", ret);
8961da177e4SLinus Torvalds 		return ERR_PTR(ret);
8971da177e4SLinus Torvalds 	}
8981da177e4SLinus Torvalds 
8996f8880d8SDavid Howells 	if (dentry->d_name.len >= 4 &&
9006f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
9016f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
9026f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
9036f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 1] == 's')
9046f8880d8SDavid Howells 		return afs_lookup_atsys(dir, dentry, key);
9056f8880d8SDavid Howells 
906d55b4da4SDavid Howells 	afs_stat_v(dvnode, n_lookup);
9075cf9dd55SDavid Howells 	inode = afs_do_lookup(dir, dentry, key);
90834b2a88fSAl Viro 	key_put(key);
90934b2a88fSAl Viro 	if (inode == ERR_PTR(-ENOENT)) {
9105cf9dd55SDavid Howells 		inode = afs_try_auto_mntpt(dentry, dir);
91134b2a88fSAl Viro 	} else {
91234b2a88fSAl Viro 		dentry->d_fsdata =
91334b2a88fSAl Viro 			(void *)(unsigned long)dvnode->status.data_version;
914bec5eb61Swanglei 	}
91534b2a88fSAl Viro 	d = d_splice_alias(inode, dentry);
91680548b03SDavid Howells 	if (!IS_ERR_OR_NULL(d)) {
91734b2a88fSAl Viro 		d->d_fsdata = dentry->d_fsdata;
91880548b03SDavid Howells 		trace_afs_lookup(dvnode, &d->d_name,
91980548b03SDavid Howells 				 inode ? AFS_FS_I(inode) : NULL);
92080548b03SDavid Howells 	} else {
92180548b03SDavid Howells 		trace_afs_lookup(dvnode, &dentry->d_name,
92280548b03SDavid Howells 				 inode ? AFS_FS_I(inode) : NULL);
92380548b03SDavid Howells 	}
92434b2a88fSAl Viro 	return d;
925ec26815aSDavid Howells }
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds /*
9281da177e4SLinus Torvalds  * check that a dentry lookup hit has found a valid entry
9291da177e4SLinus Torvalds  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
9301da177e4SLinus Torvalds  *   inode
9311da177e4SLinus Torvalds  */
9320b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
9331da177e4SLinus Torvalds {
934260a9803SDavid Howells 	struct afs_vnode *vnode, *dir;
935dd0d9a46SArtem Bityutskiy 	struct afs_fid uninitialized_var(fid);
9361da177e4SLinus Torvalds 	struct dentry *parent;
937c435ee34SDavid Howells 	struct inode *inode;
93800d3b7a4SDavid Howells 	struct key *key;
939a4ff7401SDavid Howells 	long dir_version, de_version;
9401da177e4SLinus Torvalds 	int ret;
9411da177e4SLinus Torvalds 
9420b728e19SAl Viro 	if (flags & LOOKUP_RCU)
94334286d66SNick Piggin 		return -ECHILD;
94434286d66SNick Piggin 
945c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
9462b0143b5SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
9473b6492dfSDavid Howells 		_enter("{v={%llx:%llu} n=%pd fl=%lx},",
948a455589fSAl Viro 		       vnode->fid.vid, vnode->fid.vnode, dentry,
949260a9803SDavid Howells 		       vnode->flags);
950c435ee34SDavid Howells 	} else {
951a455589fSAl Viro 		_enter("{neg n=%pd}", dentry);
952c435ee34SDavid Howells 	}
9531da177e4SLinus Torvalds 
954260a9803SDavid Howells 	key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
95500d3b7a4SDavid Howells 	if (IS_ERR(key))
95600d3b7a4SDavid Howells 		key = NULL;
95700d3b7a4SDavid Howells 
958c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
959c435ee34SDavid Howells 		inode = d_inode(dentry);
960c435ee34SDavid Howells 		if (inode) {
961c435ee34SDavid Howells 			vnode = AFS_FS_I(inode);
962c435ee34SDavid Howells 			afs_validate(vnode, key);
963c435ee34SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
964c435ee34SDavid Howells 				goto out_bad;
965c435ee34SDavid Howells 		}
966c435ee34SDavid Howells 	}
967c435ee34SDavid Howells 
9681da177e4SLinus Torvalds 	/* lock down the parent dentry so we can peer at it */
96908e0e7c8SDavid Howells 	parent = dget_parent(dentry);
9702b0143b5SDavid Howells 	dir = AFS_FS_I(d_inode(parent));
9711da177e4SLinus Torvalds 
972260a9803SDavid Howells 	/* validate the parent directory */
973260a9803SDavid Howells 	afs_validate(dir, key);
974260a9803SDavid Howells 
975260a9803SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
976a455589fSAl Viro 		_debug("%pd: parent dir deleted", dentry);
977c435ee34SDavid Howells 		goto out_bad_parent;
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 
980a4ff7401SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
981a4ff7401SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
982a4ff7401SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
983a4ff7401SDavid Howells 	 * version.
984a4ff7401SDavid Howells 	 */
985a4ff7401SDavid Howells 	dir_version = (long)dir->status.data_version;
986a4ff7401SDavid Howells 	de_version = (long)dentry->d_fsdata;
987a4ff7401SDavid Howells 	if (de_version == dir_version)
988a4ff7401SDavid Howells 		goto out_valid;
989a4ff7401SDavid Howells 
990a4ff7401SDavid Howells 	dir_version = (long)dir->invalid_before;
991a4ff7401SDavid Howells 	if (de_version - dir_version >= 0)
992a4ff7401SDavid Howells 		goto out_valid;
993260a9803SDavid Howells 
99408e0e7c8SDavid Howells 	_debug("dir modified");
995d55b4da4SDavid Howells 	afs_stat_v(dir, n_reval);
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	/* search the directory for this vnode */
9985cf9dd55SDavid Howells 	ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
999260a9803SDavid Howells 	switch (ret) {
1000260a9803SDavid Howells 	case 0:
1001260a9803SDavid Howells 		/* the filename maps to something */
10022b0143b5SDavid Howells 		if (d_really_is_negative(dentry))
1003c435ee34SDavid Howells 			goto out_bad_parent;
1004c435ee34SDavid Howells 		inode = d_inode(dentry);
1005c435ee34SDavid Howells 		if (is_bad_inode(inode)) {
1006a455589fSAl Viro 			printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1007a455589fSAl Viro 			       dentry);
1008c435ee34SDavid Howells 			goto out_bad_parent;
10091da177e4SLinus Torvalds 		}
10101da177e4SLinus Torvalds 
1011c435ee34SDavid Howells 		vnode = AFS_FS_I(inode);
1012c435ee34SDavid Howells 
10131da177e4SLinus Torvalds 		/* if the vnode ID has changed, then the dirent points to a
10141da177e4SLinus Torvalds 		 * different file */
101508e0e7c8SDavid Howells 		if (fid.vnode != vnode->fid.vnode) {
10163b6492dfSDavid Howells 			_debug("%pd: dirent changed [%llu != %llu]",
1017a455589fSAl Viro 			       dentry, fid.vnode,
101808e0e7c8SDavid Howells 			       vnode->fid.vnode);
10191da177e4SLinus Torvalds 			goto not_found;
10201da177e4SLinus Torvalds 		}
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 		/* if the vnode ID uniqifier has changed, then the file has
1023260a9803SDavid Howells 		 * been deleted and replaced, and the original vnode ID has
1024260a9803SDavid Howells 		 * been reused */
102508e0e7c8SDavid Howells 		if (fid.unique != vnode->fid.unique) {
1026a455589fSAl Viro 			_debug("%pd: file deleted (uq %u -> %u I:%u)",
1027a455589fSAl Viro 			       dentry, fid.unique,
10287a224228SJean Noel Cordenner 			       vnode->fid.unique,
1029c435ee34SDavid Howells 			       vnode->vfs_inode.i_generation);
1030c435ee34SDavid Howells 			write_seqlock(&vnode->cb_lock);
103108e0e7c8SDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
1032c435ee34SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1033260a9803SDavid Howells 			goto not_found;
1034260a9803SDavid Howells 		}
1035260a9803SDavid Howells 		goto out_valid;
1036260a9803SDavid Howells 
1037260a9803SDavid Howells 	case -ENOENT:
1038260a9803SDavid Howells 		/* the filename is unknown */
1039a455589fSAl Viro 		_debug("%pd: dirent not found", dentry);
10402b0143b5SDavid Howells 		if (d_really_is_positive(dentry))
1041260a9803SDavid Howells 			goto not_found;
1042260a9803SDavid Howells 		goto out_valid;
1043260a9803SDavid Howells 
1044260a9803SDavid Howells 	default:
1045a455589fSAl Viro 		_debug("failed to iterate dir %pd: %d",
1046a455589fSAl Viro 		       parent, ret);
1047c435ee34SDavid Howells 		goto out_bad_parent;
10481da177e4SLinus Torvalds 	}
104908e0e7c8SDavid Howells 
10501da177e4SLinus Torvalds out_valid:
1051a4ff7401SDavid Howells 	dentry->d_fsdata = (void *)dir_version;
10521da177e4SLinus Torvalds 	dput(parent);
105300d3b7a4SDavid Howells 	key_put(key);
10541da177e4SLinus Torvalds 	_leave(" = 1 [valid]");
10551da177e4SLinus Torvalds 	return 1;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	/* the dirent, if it exists, now points to a different vnode */
10581da177e4SLinus Torvalds not_found:
10591da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
10601da177e4SLinus Torvalds 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
10611da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
10621da177e4SLinus Torvalds 
1063c435ee34SDavid Howells out_bad_parent:
1064a455589fSAl Viro 	_debug("dropping dentry %pd2", dentry);
10651da177e4SLinus Torvalds 	dput(parent);
1066c435ee34SDavid Howells out_bad:
106700d3b7a4SDavid Howells 	key_put(key);
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	_leave(" = 0 [bad]");
10701da177e4SLinus Torvalds 	return 0;
1071ec26815aSDavid Howells }
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds /*
10741da177e4SLinus Torvalds  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
10751da177e4SLinus Torvalds  * sleep)
10761da177e4SLinus Torvalds  * - called from dput() when d_count is going to 0.
10771da177e4SLinus Torvalds  * - return 1 to request dentry be unhashed, 0 otherwise
10781da177e4SLinus Torvalds  */
1079fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry)
10801da177e4SLinus Torvalds {
1081a455589fSAl Viro 	_enter("%pd", dentry);
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
10841da177e4SLinus Torvalds 		goto zap;
10851da177e4SLinus Torvalds 
10862b0143b5SDavid Howells 	if (d_really_is_positive(dentry) &&
10872b0143b5SDavid Howells 	    (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
10882b0143b5SDavid Howells 	     test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
10891da177e4SLinus Torvalds 		goto zap;
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	_leave(" = 0 [keep]");
10921da177e4SLinus Torvalds 	return 0;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds zap:
10951da177e4SLinus Torvalds 	_leave(" = 1 [zap]");
10961da177e4SLinus Torvalds 	return 1;
1097ec26815aSDavid Howells }
1098260a9803SDavid Howells 
1099260a9803SDavid Howells /*
110079ddbfa5SDavid Howells  * Clean up sillyrename files on dentry removal.
110179ddbfa5SDavid Howells  */
110279ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode)
110379ddbfa5SDavid Howells {
110479ddbfa5SDavid Howells 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
110579ddbfa5SDavid Howells 		afs_silly_iput(dentry, inode);
110679ddbfa5SDavid Howells 	iput(inode);
110779ddbfa5SDavid Howells }
110879ddbfa5SDavid Howells 
110979ddbfa5SDavid Howells /*
1110260a9803SDavid Howells  * handle dentry release
1111260a9803SDavid Howells  */
111266c7e1d3SDavid Howells void afs_d_release(struct dentry *dentry)
1113260a9803SDavid Howells {
1114a455589fSAl Viro 	_enter("%pd", dentry);
1115260a9803SDavid Howells }
1116260a9803SDavid Howells 
1117260a9803SDavid Howells /*
1118d2ddc776SDavid Howells  * Create a new inode for create/mkdir/symlink
1119d2ddc776SDavid Howells  */
1120d2ddc776SDavid Howells static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1121d2ddc776SDavid Howells 				struct dentry *new_dentry,
1122b8359153SDavid Howells 				struct afs_iget_data *new_data,
1123a58823acSDavid Howells 				struct afs_status_cb *new_scb)
1124d2ddc776SDavid Howells {
11255a813276SDavid Howells 	struct afs_vnode *vnode;
1126d2ddc776SDavid Howells 	struct inode *inode;
1127d2ddc776SDavid Howells 
1128d2ddc776SDavid Howells 	if (fc->ac.error < 0)
1129d2ddc776SDavid Howells 		return;
1130d2ddc776SDavid Howells 
1131d2ddc776SDavid Howells 	inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1132b8359153SDavid Howells 			 new_data, new_scb, fc->cbi, fc->vnode);
1133d2ddc776SDavid Howells 	if (IS_ERR(inode)) {
1134d2ddc776SDavid Howells 		/* ENOMEM or EINTR at a really inconvenient time - just abandon
1135d2ddc776SDavid Howells 		 * the new directory on the server.
1136d2ddc776SDavid Howells 		 */
1137d2ddc776SDavid Howells 		fc->ac.error = PTR_ERR(inode);
1138d2ddc776SDavid Howells 		return;
1139d2ddc776SDavid Howells 	}
1140d2ddc776SDavid Howells 
11415a813276SDavid Howells 	vnode = AFS_FS_I(inode);
11425a813276SDavid Howells 	set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1143a58823acSDavid Howells 	if (fc->ac.error == 0)
1144a58823acSDavid Howells 		afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb);
114573116df7SDavid Howells 	d_instantiate(new_dentry, inode);
1146d2ddc776SDavid Howells }
1147d2ddc776SDavid Howells 
1148b8359153SDavid Howells static void afs_prep_for_new_inode(struct afs_fs_cursor *fc,
1149b8359153SDavid Howells 				   struct afs_iget_data *iget_data)
1150b8359153SDavid Howells {
1151b8359153SDavid Howells 	iget_data->volume = fc->vnode->volume;
1152b8359153SDavid Howells 	iget_data->cb_v_break = fc->vnode->volume->cb_v_break;
1153b8359153SDavid Howells 	iget_data->cb_s_break = fc->cbi->server->cb_s_break;
1154b8359153SDavid Howells }
1155b8359153SDavid Howells 
1156d2ddc776SDavid Howells /*
1157260a9803SDavid Howells  * create a directory on an AFS filesystem
1158260a9803SDavid Howells  */
115918bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1160260a9803SDavid Howells {
1161b8359153SDavid Howells 	struct afs_iget_data iget_data;
1162a58823acSDavid Howells 	struct afs_status_cb *scb;
1163d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1164d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1165260a9803SDavid Howells 	struct key *key;
1166260a9803SDavid Howells 	int ret;
1167260a9803SDavid Howells 
1168d2ddc776SDavid Howells 	mode |= S_IFDIR;
1169260a9803SDavid Howells 
11703b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho",
1171a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1172260a9803SDavid Howells 
1173a58823acSDavid Howells 	ret = -ENOMEM;
1174a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1175a58823acSDavid Howells 	if (!scb)
1176a58823acSDavid Howells 		goto error;
1177a58823acSDavid Howells 
1178260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1179260a9803SDavid Howells 	if (IS_ERR(key)) {
1180260a9803SDavid Howells 		ret = PTR_ERR(key);
1181a58823acSDavid Howells 		goto error_scb;
1182260a9803SDavid Howells 	}
1183260a9803SDavid Howells 
1184d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
118520b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1186a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1187a58823acSDavid Howells 
1188d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
118968251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1190b8359153SDavid Howells 			afs_prep_for_new_inode(&fc, &iget_data);
1191a58823acSDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1192b8359153SDavid Howells 				      &scb[0], &iget_data.fid, &scb[1]);
1193d2ddc776SDavid Howells 		}
1194d2ddc776SDavid Howells 
1195a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1196a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1197a58823acSDavid Howells 					&data_version, &scb[0]);
1198b8359153SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1199d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1200260a9803SDavid Howells 		if (ret < 0)
1201d2ddc776SDavid Howells 			goto error_key;
12024433b691SDavid Howells 	} else {
12034433b691SDavid Howells 		goto error_key;
1204260a9803SDavid Howells 	}
1205260a9803SDavid Howells 
120663a4681fSDavid Howells 	if (ret == 0 &&
120763a4681fSDavid Howells 	    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1208b8359153SDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
120963a4681fSDavid Howells 				 afs_edit_dir_for_create);
121063a4681fSDavid Howells 
1211260a9803SDavid Howells 	key_put(key);
1212a58823acSDavid Howells 	kfree(scb);
1213260a9803SDavid Howells 	_leave(" = 0");
1214260a9803SDavid Howells 	return 0;
1215260a9803SDavid Howells 
1216d2ddc776SDavid Howells error_key:
1217260a9803SDavid Howells 	key_put(key);
1218a58823acSDavid Howells error_scb:
1219a58823acSDavid Howells 	kfree(scb);
1220260a9803SDavid Howells error:
1221260a9803SDavid Howells 	d_drop(dentry);
1222260a9803SDavid Howells 	_leave(" = %d", ret);
1223260a9803SDavid Howells 	return ret;
1224260a9803SDavid Howells }
1225260a9803SDavid Howells 
1226260a9803SDavid Howells /*
1227d2ddc776SDavid Howells  * Remove a subdir from a directory.
1228260a9803SDavid Howells  */
1229d2ddc776SDavid Howells static void afs_dir_remove_subdir(struct dentry *dentry)
1230260a9803SDavid Howells {
12312b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1232d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1233d2ddc776SDavid Howells 
1234260a9803SDavid Howells 		clear_nlink(&vnode->vfs_inode);
1235260a9803SDavid Howells 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1236c435ee34SDavid Howells 		clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
123763a4681fSDavid Howells 		clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1238260a9803SDavid Howells 	}
1239260a9803SDavid Howells }
1240260a9803SDavid Howells 
1241260a9803SDavid Howells /*
1242d2ddc776SDavid Howells  * remove a directory from an AFS filesystem
1243260a9803SDavid Howells  */
1244d2ddc776SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1245260a9803SDavid Howells {
1246a58823acSDavid Howells 	struct afs_status_cb *scb;
1247d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1248f58db83fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1249260a9803SDavid Howells 	struct key *key;
1250260a9803SDavid Howells 	int ret;
1251260a9803SDavid Howells 
12523b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1253a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1254260a9803SDavid Howells 
1255a58823acSDavid Howells 	scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
1256a58823acSDavid Howells 	if (!scb)
1257a58823acSDavid Howells 		return -ENOMEM;
1258a58823acSDavid Howells 
1259260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1260260a9803SDavid Howells 	if (IS_ERR(key)) {
1261260a9803SDavid Howells 		ret = PTR_ERR(key);
1262260a9803SDavid Howells 		goto error;
1263260a9803SDavid Howells 	}
1264260a9803SDavid Howells 
1265f58db83fSDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1266f58db83fSDavid Howells 	if (d_really_is_positive(dentry)) {
1267f58db83fSDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1268f58db83fSDavid Howells 		ret = afs_validate(vnode, key);
1269f58db83fSDavid Howells 		if (ret < 0)
1270f58db83fSDavid Howells 			goto error_key;
1271f58db83fSDavid Howells 	}
1272f58db83fSDavid Howells 
127379ddbfa5SDavid Howells 	if (vnode) {
127479ddbfa5SDavid Howells 		ret = down_write_killable(&vnode->rmdir_lock);
127579ddbfa5SDavid Howells 		if (ret < 0)
127679ddbfa5SDavid Howells 			goto error_key;
127779ddbfa5SDavid Howells 	}
127879ddbfa5SDavid Howells 
1279d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
128020b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1281a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1282a58823acSDavid Howells 
1283d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
128468251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1285a58823acSDavid Howells 			afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb);
1286260a9803SDavid Howells 		}
1287260a9803SDavid Howells 
1288a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1289a58823acSDavid Howells 					&data_version, scb);
1290d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
129163a4681fSDavid Howells 		if (ret == 0) {
1292d2ddc776SDavid Howells 			afs_dir_remove_subdir(dentry);
129363a4681fSDavid Howells 			if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
129463a4681fSDavid Howells 				afs_edit_dir_remove(dvnode, &dentry->d_name,
129563a4681fSDavid Howells 						    afs_edit_dir_for_rmdir);
129663a4681fSDavid Howells 		}
1297260a9803SDavid Howells 	}
1298260a9803SDavid Howells 
129979ddbfa5SDavid Howells 	if (vnode)
130079ddbfa5SDavid Howells 		up_write(&vnode->rmdir_lock);
1301f58db83fSDavid Howells error_key:
1302260a9803SDavid Howells 	key_put(key);
1303d2ddc776SDavid Howells error:
1304a58823acSDavid Howells 	kfree(scb);
1305d2ddc776SDavid Howells 	return ret;
1306d2ddc776SDavid Howells }
1307260a9803SDavid Howells 
1308d2ddc776SDavid Howells /*
1309d2ddc776SDavid Howells  * Remove a link to a file or symlink from a directory.
1310d2ddc776SDavid Howells  *
1311d2ddc776SDavid Howells  * If the file was not deleted due to excess hard links, the fileserver will
1312d2ddc776SDavid Howells  * break the callback promise on the file - if it had one - before it returns
1313d2ddc776SDavid Howells  * to us, and if it was deleted, it won't
1314d2ddc776SDavid Howells  *
1315d2ddc776SDavid Howells  * However, if we didn't have a callback promise outstanding, or it was
1316d2ddc776SDavid Howells  * outstanding on a different server, then it won't break it either...
1317d2ddc776SDavid Howells  */
1318a38a7558SDavid Howells static int afs_dir_remove_link(struct afs_vnode *dvnode, struct dentry *dentry,
1319a38a7558SDavid Howells 			       struct key *key)
1320d2ddc776SDavid Howells {
1321d2ddc776SDavid Howells 	int ret = 0;
1322d2ddc776SDavid Howells 
1323d2ddc776SDavid Howells 	if (d_really_is_positive(dentry)) {
1324d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1325d2ddc776SDavid Howells 
132630062bd1SDavid Howells 		if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
132730062bd1SDavid Howells 			/* Already done */
1328a38a7558SDavid Howells 		} else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1329a38a7558SDavid Howells 			write_seqlock(&vnode->cb_lock);
1330440fbc3aSDavid Howells 			drop_nlink(&vnode->vfs_inode);
1331440fbc3aSDavid Howells 			if (vnode->vfs_inode.i_nlink == 0) {
1332440fbc3aSDavid Howells 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
1333a38a7558SDavid Howells 				__afs_break_callback(vnode);
1334440fbc3aSDavid Howells 			}
1335a38a7558SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1336440fbc3aSDavid Howells 			ret = 0;
1337440fbc3aSDavid Howells 		} else {
1338a38a7558SDavid Howells 			afs_break_callback(vnode);
1339440fbc3aSDavid Howells 
1340d2ddc776SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1341d2ddc776SDavid Howells 				kdebug("AFS_VNODE_DELETED");
1342d2ddc776SDavid Howells 
1343d2ddc776SDavid Howells 			ret = afs_validate(vnode, key);
1344d2ddc776SDavid Howells 			if (ret == -ESTALE)
1345d2ddc776SDavid Howells 				ret = 0;
1346440fbc3aSDavid Howells 		}
1347d2ddc776SDavid Howells 		_debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1348d2ddc776SDavid Howells 	}
1349d2ddc776SDavid Howells 
1350d2ddc776SDavid Howells 	return ret;
1351d2ddc776SDavid Howells }
1352d2ddc776SDavid Howells 
1353d2ddc776SDavid Howells /*
1354d2ddc776SDavid Howells  * Remove a file or symlink from an AFS filesystem.
1355d2ddc776SDavid Howells  */
1356d2ddc776SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry)
1357d2ddc776SDavid Howells {
1358d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1359a58823acSDavid Howells 	struct afs_status_cb *scb;
136030062bd1SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1361d2ddc776SDavid Howells 	struct key *key;
136279ddbfa5SDavid Howells 	bool need_rehash = false;
1363d2ddc776SDavid Howells 	int ret;
1364d2ddc776SDavid Howells 
13653b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1366d2ddc776SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1367d2ddc776SDavid Howells 
1368d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1369d2ddc776SDavid Howells 		return -ENAMETOOLONG;
1370d2ddc776SDavid Howells 
1371a58823acSDavid Howells 	ret = -ENOMEM;
1372a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1373a58823acSDavid Howells 	if (!scb)
1374a58823acSDavid Howells 		goto error;
1375a58823acSDavid Howells 
1376d2ddc776SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1377d2ddc776SDavid Howells 	if (IS_ERR(key)) {
1378d2ddc776SDavid Howells 		ret = PTR_ERR(key);
1379a58823acSDavid Howells 		goto error_scb;
1380d2ddc776SDavid Howells 	}
1381d2ddc776SDavid Howells 
1382d2ddc776SDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1383d2ddc776SDavid Howells 	if (d_really_is_positive(dentry)) {
1384d2ddc776SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1385d2ddc776SDavid Howells 		ret = afs_validate(vnode, key);
1386d2ddc776SDavid Howells 		if (ret < 0)
1387d2ddc776SDavid Howells 			goto error_key;
1388d2ddc776SDavid Howells 	}
1389d2ddc776SDavid Howells 
139079ddbfa5SDavid Howells 	spin_lock(&dentry->d_lock);
139179ddbfa5SDavid Howells 	if (vnode && d_count(dentry) > 1) {
139279ddbfa5SDavid Howells 		spin_unlock(&dentry->d_lock);
139379ddbfa5SDavid Howells 		/* Start asynchronous writeout of the inode */
139479ddbfa5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
139579ddbfa5SDavid Howells 		ret = afs_sillyrename(dvnode, vnode, dentry, key);
139679ddbfa5SDavid Howells 		goto error_key;
139779ddbfa5SDavid Howells 	}
139879ddbfa5SDavid Howells 	if (!d_unhashed(dentry)) {
139979ddbfa5SDavid Howells 		/* Prevent a race with RCU lookup. */
140079ddbfa5SDavid Howells 		__d_drop(dentry);
140179ddbfa5SDavid Howells 		need_rehash = true;
140279ddbfa5SDavid Howells 	}
140379ddbfa5SDavid Howells 	spin_unlock(&dentry->d_lock);
140479ddbfa5SDavid Howells 
1405d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
140620b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1407a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1408a38a7558SDavid Howells 		afs_dataversion_t data_version_2 = vnode->status.data_version;
1409a58823acSDavid Howells 
1410d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
141168251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1412a38a7558SDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
141330062bd1SDavid Howells 
141430062bd1SDavid Howells 			if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
141530062bd1SDavid Howells 			    !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
141630062bd1SDavid Howells 				yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1417a58823acSDavid Howells 						    &scb[0], &scb[1]);
141830062bd1SDavid Howells 				if (fc.ac.error != -ECONNABORTED ||
141930062bd1SDavid Howells 				    fc.ac.abort_code != RXGEN_OPCODE)
142030062bd1SDavid Howells 					continue;
142130062bd1SDavid Howells 				set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
142230062bd1SDavid Howells 			}
142330062bd1SDavid Howells 
1424a58823acSDavid Howells 			afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
1425d2ddc776SDavid Howells 		}
1426d2ddc776SDavid Howells 
1427a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1428a58823acSDavid Howells 					&data_version, &scb[0]);
1429a38a7558SDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1430a38a7558SDavid Howells 					&data_version_2, &scb[1]);
1431d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1432a38a7558SDavid Howells 		if (ret == 0 && !(scb[1].have_status || scb[1].have_error))
1433a38a7558SDavid Howells 			ret = afs_dir_remove_link(dvnode, dentry, key);
143463a4681fSDavid Howells 		if (ret == 0 &&
143563a4681fSDavid Howells 		    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
143663a4681fSDavid Howells 			afs_edit_dir_remove(dvnode, &dentry->d_name,
143763a4681fSDavid Howells 					    afs_edit_dir_for_unlink);
1438d2ddc776SDavid Howells 	}
1439d2ddc776SDavid Howells 
144079ddbfa5SDavid Howells 	if (need_rehash && ret < 0 && ret != -ENOENT)
144179ddbfa5SDavid Howells 		d_rehash(dentry);
144279ddbfa5SDavid Howells 
1443d2ddc776SDavid Howells error_key:
1444260a9803SDavid Howells 	key_put(key);
1445a58823acSDavid Howells error_scb:
1446a58823acSDavid Howells 	kfree(scb);
1447260a9803SDavid Howells error:
1448260a9803SDavid Howells 	_leave(" = %d", ret);
1449260a9803SDavid Howells 	return ret;
1450260a9803SDavid Howells }
1451260a9803SDavid Howells 
1452260a9803SDavid Howells /*
1453260a9803SDavid Howells  * create a regular file on an AFS filesystem
1454260a9803SDavid Howells  */
14554acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1456ebfc3b49SAl Viro 		      bool excl)
1457260a9803SDavid Howells {
1458b8359153SDavid Howells 	struct afs_iget_data iget_data;
1459d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1460a58823acSDavid Howells 	struct afs_status_cb *scb;
146143dd388bSColin Ian King 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1462260a9803SDavid Howells 	struct key *key;
1463260a9803SDavid Howells 	int ret;
1464260a9803SDavid Howells 
1465d2ddc776SDavid Howells 	mode |= S_IFREG;
1466260a9803SDavid Howells 
14673b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho,",
1468a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1469260a9803SDavid Howells 
1470d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1471d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1472d2ddc776SDavid Howells 		goto error;
1473d2ddc776SDavid Howells 
1474260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1475260a9803SDavid Howells 	if (IS_ERR(key)) {
1476260a9803SDavid Howells 		ret = PTR_ERR(key);
1477260a9803SDavid Howells 		goto error;
1478260a9803SDavid Howells 	}
1479260a9803SDavid Howells 
1480a58823acSDavid Howells 	ret = -ENOMEM;
1481a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1482a58823acSDavid Howells 	if (!scb)
1483a58823acSDavid Howells 		goto error_scb;
1484a58823acSDavid Howells 
1485d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
148620b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1487a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1488a58823acSDavid Howells 
1489d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
149068251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1491b8359153SDavid Howells 			afs_prep_for_new_inode(&fc, &iget_data);
1492a58823acSDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1493b8359153SDavid Howells 				      &scb[0], &iget_data.fid, &scb[1]);
1494d2ddc776SDavid Howells 		}
1495d2ddc776SDavid Howells 
1496a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1497a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1498a58823acSDavid Howells 					&data_version, &scb[0]);
1499b8359153SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1500d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1501260a9803SDavid Howells 		if (ret < 0)
1502d2ddc776SDavid Howells 			goto error_key;
15034433b691SDavid Howells 	} else {
15044433b691SDavid Howells 		goto error_key;
1505260a9803SDavid Howells 	}
1506260a9803SDavid Howells 
150763a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1508b8359153SDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
150963a4681fSDavid Howells 				 afs_edit_dir_for_create);
151063a4681fSDavid Howells 
1511a58823acSDavid Howells 	kfree(scb);
1512260a9803SDavid Howells 	key_put(key);
1513260a9803SDavid Howells 	_leave(" = 0");
1514260a9803SDavid Howells 	return 0;
1515260a9803SDavid Howells 
1516a58823acSDavid Howells error_scb:
1517a58823acSDavid Howells 	kfree(scb);
1518d2ddc776SDavid Howells error_key:
1519260a9803SDavid Howells 	key_put(key);
1520260a9803SDavid Howells error:
1521260a9803SDavid Howells 	d_drop(dentry);
1522260a9803SDavid Howells 	_leave(" = %d", ret);
1523260a9803SDavid Howells 	return ret;
1524260a9803SDavid Howells }
1525260a9803SDavid Howells 
1526260a9803SDavid Howells /*
1527260a9803SDavid Howells  * create a hard link between files in an AFS filesystem
1528260a9803SDavid Howells  */
1529260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
1530260a9803SDavid Howells 		    struct dentry *dentry)
1531260a9803SDavid Howells {
1532d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1533a58823acSDavid Howells 	struct afs_status_cb *scb;
1534a58823acSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1535a58823acSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1536260a9803SDavid Howells 	struct key *key;
1537260a9803SDavid Howells 	int ret;
1538260a9803SDavid Howells 
15393b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%pd}",
1540260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1541260a9803SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode,
1542a455589fSAl Viro 	       dentry);
1543260a9803SDavid Howells 
1544d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1545d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1546d2ddc776SDavid Howells 		goto error;
1547d2ddc776SDavid Howells 
1548a58823acSDavid Howells 	ret = -ENOMEM;
1549a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1550a58823acSDavid Howells 	if (!scb)
1551a58823acSDavid Howells 		goto error;
1552a58823acSDavid Howells 
1553260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1554260a9803SDavid Howells 	if (IS_ERR(key)) {
1555260a9803SDavid Howells 		ret = PTR_ERR(key);
1556a58823acSDavid Howells 		goto error_scb;
1557260a9803SDavid Howells 	}
1558260a9803SDavid Howells 
1559d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
156020b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1561a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1562a58823acSDavid Howells 
1563d2ddc776SDavid Howells 		if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1564d2ddc776SDavid Howells 			afs_end_vnode_operation(&fc);
1565bc1527dcSDavid Howells 			goto error_key;
1566d2ddc776SDavid Howells 		}
1567260a9803SDavid Howells 
1568d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
156968251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
157068251f0aSDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1571a58823acSDavid Howells 			afs_fs_link(&fc, vnode, dentry->d_name.name,
1572a58823acSDavid Howells 				    &scb[0], &scb[1]);
1573d2ddc776SDavid Howells 		}
1574d2ddc776SDavid Howells 
1575a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1576a58823acSDavid Howells 					&data_version, &scb[0]);
1577a58823acSDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1578a58823acSDavid Howells 					NULL, &scb[1]);
15797de9c6eeSAl Viro 		ihold(&vnode->vfs_inode);
1580260a9803SDavid Howells 		d_instantiate(dentry, &vnode->vfs_inode);
1581d2ddc776SDavid Howells 
1582d2ddc776SDavid Howells 		mutex_unlock(&vnode->io_lock);
1583d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1584d2ddc776SDavid Howells 		if (ret < 0)
1585d2ddc776SDavid Howells 			goto error_key;
15864433b691SDavid Howells 	} else {
15874433b691SDavid Howells 		goto error_key;
1588d2ddc776SDavid Howells 	}
1589d2ddc776SDavid Howells 
159063a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
159163a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
159263a4681fSDavid Howells 				 afs_edit_dir_for_link);
159363a4681fSDavid Howells 
1594260a9803SDavid Howells 	key_put(key);
1595a58823acSDavid Howells 	kfree(scb);
1596260a9803SDavid Howells 	_leave(" = 0");
1597260a9803SDavid Howells 	return 0;
1598260a9803SDavid Howells 
1599d2ddc776SDavid Howells error_key:
1600260a9803SDavid Howells 	key_put(key);
1601a58823acSDavid Howells error_scb:
1602a58823acSDavid Howells 	kfree(scb);
1603260a9803SDavid Howells error:
1604260a9803SDavid Howells 	d_drop(dentry);
1605260a9803SDavid Howells 	_leave(" = %d", ret);
1606260a9803SDavid Howells 	return ret;
1607260a9803SDavid Howells }
1608260a9803SDavid Howells 
1609260a9803SDavid Howells /*
1610260a9803SDavid Howells  * create a symlink in an AFS filesystem
1611260a9803SDavid Howells  */
1612260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
1613260a9803SDavid Howells 		       const char *content)
1614260a9803SDavid Howells {
1615b8359153SDavid Howells 	struct afs_iget_data iget_data;
1616d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1617a58823acSDavid Howells 	struct afs_status_cb *scb;
1618d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1619260a9803SDavid Howells 	struct key *key;
1620260a9803SDavid Howells 	int ret;
1621260a9803SDavid Howells 
16223b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%s",
1623a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry,
1624260a9803SDavid Howells 	       content);
1625260a9803SDavid Howells 
1626d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1627d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1628d2ddc776SDavid Howells 		goto error;
1629d2ddc776SDavid Howells 
1630260a9803SDavid Howells 	ret = -EINVAL;
163145222b9eSDavid Howells 	if (strlen(content) >= AFSPATHMAX)
1632260a9803SDavid Howells 		goto error;
1633260a9803SDavid Howells 
1634a58823acSDavid Howells 	ret = -ENOMEM;
1635a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1636a58823acSDavid Howells 	if (!scb)
1637a58823acSDavid Howells 		goto error;
1638a58823acSDavid Howells 
1639260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1640260a9803SDavid Howells 	if (IS_ERR(key)) {
1641260a9803SDavid Howells 		ret = PTR_ERR(key);
1642a58823acSDavid Howells 		goto error_scb;
1643260a9803SDavid Howells 	}
1644260a9803SDavid Howells 
1645d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
164620b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1647a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1648a58823acSDavid Howells 
1649d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
165068251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1651b8359153SDavid Howells 			afs_prep_for_new_inode(&fc, &iget_data);
1652a58823acSDavid Howells 			afs_fs_symlink(&fc, dentry->d_name.name, content,
1653b8359153SDavid Howells 				       &scb[0], &iget_data.fid, &scb[1]);
1654d2ddc776SDavid Howells 		}
1655d2ddc776SDavid Howells 
1656a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1657a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1658a58823acSDavid Howells 					&data_version, &scb[0]);
1659b8359153SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1660d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1661260a9803SDavid Howells 		if (ret < 0)
1662d2ddc776SDavid Howells 			goto error_key;
16634433b691SDavid Howells 	} else {
16644433b691SDavid Howells 		goto error_key;
1665260a9803SDavid Howells 	}
1666260a9803SDavid Howells 
166763a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1668b8359153SDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
166963a4681fSDavid Howells 				 afs_edit_dir_for_symlink);
167063a4681fSDavid Howells 
1671260a9803SDavid Howells 	key_put(key);
1672a58823acSDavid Howells 	kfree(scb);
1673260a9803SDavid Howells 	_leave(" = 0");
1674260a9803SDavid Howells 	return 0;
1675260a9803SDavid Howells 
1676d2ddc776SDavid Howells error_key:
1677260a9803SDavid Howells 	key_put(key);
1678a58823acSDavid Howells error_scb:
1679a58823acSDavid Howells 	kfree(scb);
1680260a9803SDavid Howells error:
1681260a9803SDavid Howells 	d_drop(dentry);
1682260a9803SDavid Howells 	_leave(" = %d", ret);
1683260a9803SDavid Howells 	return ret;
1684260a9803SDavid Howells }
1685260a9803SDavid Howells 
1686260a9803SDavid Howells /*
1687260a9803SDavid Howells  * rename a file in an AFS filesystem and/or move it between directories
1688260a9803SDavid Howells  */
1689260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
16901cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
16911cd66c93SMiklos Szeredi 		      unsigned int flags)
1692260a9803SDavid Howells {
1693d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1694a58823acSDavid Howells 	struct afs_status_cb *scb;
1695260a9803SDavid Howells 	struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
169679ddbfa5SDavid Howells 	struct dentry *tmp = NULL, *rehash = NULL;
169779ddbfa5SDavid Howells 	struct inode *new_inode;
1698260a9803SDavid Howells 	struct key *key;
169963a4681fSDavid Howells 	bool new_negative = d_is_negative(new_dentry);
1700260a9803SDavid Howells 	int ret;
1701260a9803SDavid Howells 
17021cd66c93SMiklos Szeredi 	if (flags)
17031cd66c93SMiklos Szeredi 		return -EINVAL;
17041cd66c93SMiklos Szeredi 
170579ddbfa5SDavid Howells 	/* Don't allow silly-rename files be moved around. */
170679ddbfa5SDavid Howells 	if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
170779ddbfa5SDavid Howells 		return -EINVAL;
170879ddbfa5SDavid Howells 
17092b0143b5SDavid Howells 	vnode = AFS_FS_I(d_inode(old_dentry));
1710260a9803SDavid Howells 	orig_dvnode = AFS_FS_I(old_dir);
1711260a9803SDavid Howells 	new_dvnode = AFS_FS_I(new_dir);
1712260a9803SDavid Howells 
17133b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1714260a9803SDavid Howells 	       orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1715260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1716260a9803SDavid Howells 	       new_dvnode->fid.vid, new_dvnode->fid.vnode,
1717a455589fSAl Viro 	       new_dentry);
1718260a9803SDavid Howells 
1719a58823acSDavid Howells 	ret = -ENOMEM;
1720a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1721a58823acSDavid Howells 	if (!scb)
1722a58823acSDavid Howells 		goto error;
1723a58823acSDavid Howells 
1724260a9803SDavid Howells 	key = afs_request_key(orig_dvnode->volume->cell);
1725260a9803SDavid Howells 	if (IS_ERR(key)) {
1726260a9803SDavid Howells 		ret = PTR_ERR(key);
1727a58823acSDavid Howells 		goto error_scb;
1728260a9803SDavid Howells 	}
1729260a9803SDavid Howells 
173079ddbfa5SDavid Howells 	/* For non-directories, check whether the target is busy and if so,
173179ddbfa5SDavid Howells 	 * make a copy of the dentry and then do a silly-rename.  If the
173279ddbfa5SDavid Howells 	 * silly-rename succeeds, the copied dentry is hashed and becomes the
173379ddbfa5SDavid Howells 	 * new target.
173479ddbfa5SDavid Howells 	 */
173579ddbfa5SDavid Howells 	if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
173679ddbfa5SDavid Howells 		/* To prevent any new references to the target during the
173779ddbfa5SDavid Howells 		 * rename, we unhash the dentry in advance.
173879ddbfa5SDavid Howells 		 */
173979ddbfa5SDavid Howells 		if (!d_unhashed(new_dentry)) {
174079ddbfa5SDavid Howells 			d_drop(new_dentry);
174179ddbfa5SDavid Howells 			rehash = new_dentry;
174279ddbfa5SDavid Howells 		}
174379ddbfa5SDavid Howells 
174479ddbfa5SDavid Howells 		if (d_count(new_dentry) > 2) {
174579ddbfa5SDavid Howells 			/* copy the target dentry's name */
174679ddbfa5SDavid Howells 			ret = -ENOMEM;
174779ddbfa5SDavid Howells 			tmp = d_alloc(new_dentry->d_parent,
174879ddbfa5SDavid Howells 				      &new_dentry->d_name);
174979ddbfa5SDavid Howells 			if (!tmp)
175079ddbfa5SDavid Howells 				goto error_rehash;
175179ddbfa5SDavid Howells 
175279ddbfa5SDavid Howells 			ret = afs_sillyrename(new_dvnode,
175379ddbfa5SDavid Howells 					      AFS_FS_I(d_inode(new_dentry)),
175479ddbfa5SDavid Howells 					      new_dentry, key);
175579ddbfa5SDavid Howells 			if (ret)
175679ddbfa5SDavid Howells 				goto error_rehash;
175779ddbfa5SDavid Howells 
175879ddbfa5SDavid Howells 			new_dentry = tmp;
175979ddbfa5SDavid Howells 			rehash = NULL;
176079ddbfa5SDavid Howells 			new_negative = true;
176179ddbfa5SDavid Howells 		}
176279ddbfa5SDavid Howells 	}
176379ddbfa5SDavid Howells 
1764d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
176520b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) {
1766a58823acSDavid Howells 		afs_dataversion_t orig_data_version;
1767a58823acSDavid Howells 		afs_dataversion_t new_data_version;
1768a58823acSDavid Howells 		struct afs_status_cb *new_scb = &scb[1];
1769a58823acSDavid Howells 
1770a58823acSDavid Howells 		orig_data_version = orig_dvnode->status.data_version + 1;
1771a58823acSDavid Howells 
1772d2ddc776SDavid Howells 		if (orig_dvnode != new_dvnode) {
1773d2ddc776SDavid Howells 			if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1774d2ddc776SDavid Howells 				afs_end_vnode_operation(&fc);
177579ddbfa5SDavid Howells 				goto error_rehash;
1776d2ddc776SDavid Howells 			}
1777a58823acSDavid Howells 			new_data_version = new_dvnode->status.data_version;
1778a58823acSDavid Howells 		} else {
1779a58823acSDavid Howells 			new_data_version = orig_data_version;
1780a58823acSDavid Howells 			new_scb = &scb[0];
1781d2ddc776SDavid Howells 		}
1782a58823acSDavid Howells 
1783d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
178468251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
178568251f0aSDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
1786d2ddc776SDavid Howells 			afs_fs_rename(&fc, old_dentry->d_name.name,
178763a4681fSDavid Howells 				      new_dvnode, new_dentry->d_name.name,
1788a58823acSDavid Howells 				      &scb[0], new_scb);
1789d2ddc776SDavid Howells 		}
1790d2ddc776SDavid Howells 
1791a58823acSDavid Howells 		afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break,
1792a58823acSDavid Howells 					&orig_data_version, &scb[0]);
1793a58823acSDavid Howells 		if (new_dvnode != orig_dvnode) {
1794a58823acSDavid Howells 			afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2,
1795a58823acSDavid Howells 						&new_data_version, &scb[1]);
1796d2ddc776SDavid Howells 			mutex_unlock(&new_dvnode->io_lock);
1797a58823acSDavid Howells 		}
1798d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1799260a9803SDavid Howells 		if (ret < 0)
180079ddbfa5SDavid Howells 			goto error_rehash;
1801d2ddc776SDavid Howells 	}
1802d2ddc776SDavid Howells 
180363a4681fSDavid Howells 	if (ret == 0) {
180479ddbfa5SDavid Howells 		if (rehash)
180579ddbfa5SDavid Howells 			d_rehash(rehash);
180663a4681fSDavid Howells 		if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
180763a4681fSDavid Howells 		    afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
180879ddbfa5SDavid Howells 					afs_edit_dir_for_rename_0);
180963a4681fSDavid Howells 
181063a4681fSDavid Howells 		if (!new_negative &&
181163a4681fSDavid Howells 		    test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
181263a4681fSDavid Howells 			afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
181379ddbfa5SDavid Howells 					    afs_edit_dir_for_rename_1);
181463a4681fSDavid Howells 
181563a4681fSDavid Howells 		if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
181663a4681fSDavid Howells 			afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
181779ddbfa5SDavid Howells 					 &vnode->fid, afs_edit_dir_for_rename_2);
181879ddbfa5SDavid Howells 
181979ddbfa5SDavid Howells 		new_inode = d_inode(new_dentry);
182079ddbfa5SDavid Howells 		if (new_inode) {
182179ddbfa5SDavid Howells 			spin_lock(&new_inode->i_lock);
182279ddbfa5SDavid Howells 			if (new_inode->i_nlink > 0)
182379ddbfa5SDavid Howells 				drop_nlink(new_inode);
182479ddbfa5SDavid Howells 			spin_unlock(&new_inode->i_lock);
182579ddbfa5SDavid Howells 		}
182679ddbfa5SDavid Howells 		d_move(old_dentry, new_dentry);
182779ddbfa5SDavid Howells 		goto error_tmp;
182863a4681fSDavid Howells 	}
182963a4681fSDavid Howells 
183079ddbfa5SDavid Howells error_rehash:
183179ddbfa5SDavid Howells 	if (rehash)
183279ddbfa5SDavid Howells 		d_rehash(rehash);
183379ddbfa5SDavid Howells error_tmp:
183479ddbfa5SDavid Howells 	if (tmp)
183579ddbfa5SDavid Howells 		dput(tmp);
1836260a9803SDavid Howells 	key_put(key);
1837a58823acSDavid Howells error_scb:
1838a58823acSDavid Howells 	kfree(scb);
1839260a9803SDavid Howells error:
1840260a9803SDavid Howells 	_leave(" = %d", ret);
1841260a9803SDavid Howells 	return ret;
1842260a9803SDavid Howells }
1843f3ddee8dSDavid Howells 
1844f3ddee8dSDavid Howells /*
1845f3ddee8dSDavid Howells  * Release a directory page and clean up its private state if it's not busy
1846f3ddee8dSDavid Howells  * - return true if the page can now be released, false if not
1847f3ddee8dSDavid Howells  */
1848f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1849f3ddee8dSDavid Howells {
1850f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1851f3ddee8dSDavid Howells 
18523b6492dfSDavid Howells 	_enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1853f3ddee8dSDavid Howells 
1854f3ddee8dSDavid Howells 	set_page_private(page, 0);
1855f3ddee8dSDavid Howells 	ClearPagePrivate(page);
1856f3ddee8dSDavid Howells 
1857f3ddee8dSDavid Howells 	/* The directory will need reloading. */
1858f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1859f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_relpg);
1860f3ddee8dSDavid Howells 	return 1;
1861f3ddee8dSDavid Howells }
1862f3ddee8dSDavid Howells 
1863f3ddee8dSDavid Howells /*
1864f3ddee8dSDavid Howells  * invalidate part or all of a page
1865f3ddee8dSDavid Howells  * - release a page and clean up its private data if offset is 0 (indicating
1866f3ddee8dSDavid Howells  *   the entire page)
1867f3ddee8dSDavid Howells  */
1868f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1869f3ddee8dSDavid Howells 				   unsigned int length)
1870f3ddee8dSDavid Howells {
1871f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1872f3ddee8dSDavid Howells 
1873f3ddee8dSDavid Howells 	_enter("{%lu},%u,%u", page->index, offset, length);
1874f3ddee8dSDavid Howells 
1875f3ddee8dSDavid Howells 	BUG_ON(!PageLocked(page));
1876f3ddee8dSDavid Howells 
1877f3ddee8dSDavid Howells 	/* The directory will need reloading. */
1878f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1879f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_inval);
1880f3ddee8dSDavid Howells 
1881f3ddee8dSDavid Howells 	/* we clean up only if the entire page is being invalidated */
1882f3ddee8dSDavid Howells 	if (offset == 0 && length == PAGE_SIZE) {
1883f3ddee8dSDavid Howells 		set_page_private(page, 0);
1884f3ddee8dSDavid Howells 		ClearPagePrivate(page);
1885f3ddee8dSDavid Howells 	}
1886f3ddee8dSDavid Howells }
1887