xref: /openbmc/linux/fs/afs/dir.c (revision fa59f52f)
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;
10639db9815SDavid Howells 	struct inode		**inodes;
10787182759SDavid Howells 	struct afs_status_cb	*statuses;
1085cf9dd55SDavid Howells 	struct afs_fid		fids[50];
1091da177e4SLinus Torvalds };
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * check that a directory page is valid
1131da177e4SLinus Torvalds  */
114f3ddee8dSDavid Howells static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
115f3ddee8dSDavid Howells 			       loff_t i_size)
1161da177e4SLinus Torvalds {
11700317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
118f3ddee8dSDavid Howells 	loff_t latter, off;
1191da177e4SLinus Torvalds 	int tmp, qty;
1201da177e4SLinus Torvalds 
121dab17c1aSDavid Howells 	/* Determine how many magic numbers there should be in this page, but
122dab17c1aSDavid Howells 	 * we must take care because the directory may change size under us.
123dab17c1aSDavid Howells 	 */
124dab17c1aSDavid Howells 	off = page_offset(page);
125dab17c1aSDavid Howells 	if (i_size <= off)
126dab17c1aSDavid Howells 		goto checked;
127dab17c1aSDavid Howells 
128dab17c1aSDavid Howells 	latter = i_size - off;
1291da177e4SLinus Torvalds 	if (latter >= PAGE_SIZE)
1301da177e4SLinus Torvalds 		qty = PAGE_SIZE;
1311da177e4SLinus Torvalds 	else
1321da177e4SLinus Torvalds 		qty = latter;
13300317636SDavid Howells 	qty /= sizeof(union afs_xdr_dir_block);
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	/* check them */
13663a4681fSDavid Howells 	dbuf = kmap(page);
1371da177e4SLinus Torvalds 	for (tmp = 0; tmp < qty; tmp++) {
13800317636SDavid Howells 		if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
139dab17c1aSDavid Howells 			printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
140f3ddee8dSDavid Howells 			       __func__, dvnode->vfs_inode.i_ino, tmp, qty,
14100317636SDavid Howells 			       ntohs(dbuf->blocks[tmp].hdr.magic));
142f3ddee8dSDavid Howells 			trace_afs_dir_check_failed(dvnode, off, i_size);
14363a4681fSDavid Howells 			kunmap(page);
144f51375cdSDavid Howells 			trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
1451da177e4SLinus Torvalds 			goto error;
1461da177e4SLinus Torvalds 		}
14763a4681fSDavid Howells 
14863a4681fSDavid Howells 		/* Make sure each block is NUL terminated so we can reasonably
14963a4681fSDavid Howells 		 * use string functions on it.  The filenames in the page
15063a4681fSDavid Howells 		 * *should* be NUL-terminated anyway.
15163a4681fSDavid Howells 		 */
15263a4681fSDavid Howells 		((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 
15563a4681fSDavid Howells 	kunmap(page);
15663a4681fSDavid Howells 
157dab17c1aSDavid Howells checked:
158f3ddee8dSDavid Howells 	afs_stat_v(dvnode, n_read_dir);
159be5b82dbSAl Viro 	return true;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds error:
162be5b82dbSAl Viro 	return false;
163ec26815aSDavid Howells }
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds /*
166445b1028SDavid Howells  * Check the contents of a directory that we've just read.
167445b1028SDavid Howells  */
168445b1028SDavid Howells static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
169445b1028SDavid Howells {
170445b1028SDavid Howells 	struct afs_xdr_dir_page *dbuf;
171445b1028SDavid Howells 	unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
172445b1028SDavid Howells 
173445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++)
174445b1028SDavid Howells 		if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
175445b1028SDavid Howells 			goto bad;
176445b1028SDavid Howells 	return true;
177445b1028SDavid Howells 
178445b1028SDavid Howells bad:
179445b1028SDavid Howells 	pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
180445b1028SDavid Howells 		dvnode->fid.vid, dvnode->fid.vnode,
181445b1028SDavid Howells 		req->file_size, req->len, req->actual_len, req->remain);
182445b1028SDavid Howells 	pr_warn("DIR %llx %x %x %x\n",
183445b1028SDavid Howells 		req->pos, req->index, req->nr_pages, req->offset);
184445b1028SDavid Howells 
185445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++) {
186445b1028SDavid Howells 		dbuf = kmap(req->pages[i]);
187445b1028SDavid Howells 		for (j = 0; j < qty; j++) {
188445b1028SDavid Howells 			union afs_xdr_dir_block *block = &dbuf->blocks[j];
189445b1028SDavid Howells 
190445b1028SDavid Howells 			pr_warn("[%02x] %32phN\n", i * qty + j, block);
191445b1028SDavid Howells 		}
192445b1028SDavid Howells 		kunmap(req->pages[i]);
193445b1028SDavid Howells 	}
194445b1028SDavid Howells 	return false;
195445b1028SDavid Howells }
196445b1028SDavid Howells 
197445b1028SDavid Howells /*
1981da177e4SLinus Torvalds  * open an AFS directory file
1991da177e4SLinus Torvalds  */
2001da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	_enter("{%lu}", inode->i_ino);
2031da177e4SLinus Torvalds 
20400317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
20500317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
2061da177e4SLinus Torvalds 
20708e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
2081da177e4SLinus Torvalds 		return -ENOENT;
2091da177e4SLinus Torvalds 
21000d3b7a4SDavid Howells 	return afs_open(inode, file);
211ec26815aSDavid Howells }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds /*
214f3ddee8dSDavid Howells  * Read the directory into the pagecache in one go, scrubbing the previous
215f3ddee8dSDavid Howells  * contents.  The list of pages is returned, pinning them so that they don't
216f3ddee8dSDavid Howells  * get reclaimed during the iteration.
217f3ddee8dSDavid Howells  */
218f3ddee8dSDavid Howells static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
219b61f7dcfSDavid Howells 	__acquires(&dvnode->validate_lock)
220f3ddee8dSDavid Howells {
221f3ddee8dSDavid Howells 	struct afs_read *req;
222f3ddee8dSDavid Howells 	loff_t i_size;
223f3ddee8dSDavid Howells 	int nr_pages, nr_inline, i, n;
224f3ddee8dSDavid Howells 	int ret = -ENOMEM;
225f3ddee8dSDavid Howells 
226f3ddee8dSDavid Howells retry:
227f3ddee8dSDavid Howells 	i_size = i_size_read(&dvnode->vfs_inode);
228f3ddee8dSDavid Howells 	if (i_size < 2048)
229f51375cdSDavid Howells 		return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
230f51375cdSDavid Howells 	if (i_size > 2048 * 1024) {
231f51375cdSDavid Howells 		trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
232f3ddee8dSDavid Howells 		return ERR_PTR(-EFBIG);
233f51375cdSDavid Howells 	}
234f3ddee8dSDavid Howells 
235f3ddee8dSDavid Howells 	_enter("%llu", i_size);
236f3ddee8dSDavid Howells 
237f3ddee8dSDavid Howells 	/* Get a request record to hold the page list.  We want to hold it
238f3ddee8dSDavid Howells 	 * inline if we can, but we don't want to make an order 1 allocation.
239f3ddee8dSDavid Howells 	 */
240f3ddee8dSDavid Howells 	nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
241f3ddee8dSDavid Howells 	nr_inline = nr_pages;
242f3ddee8dSDavid Howells 	if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
243f3ddee8dSDavid Howells 		nr_inline = 0;
244f3ddee8dSDavid Howells 
245f3ddee8dSDavid Howells 	req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
246f3ddee8dSDavid Howells 		      GFP_KERNEL);
247f3ddee8dSDavid Howells 	if (!req)
248f3ddee8dSDavid Howells 		return ERR_PTR(-ENOMEM);
249f3ddee8dSDavid Howells 
250f3ddee8dSDavid Howells 	refcount_set(&req->usage, 1);
251f3ddee8dSDavid Howells 	req->nr_pages = nr_pages;
252f3ddee8dSDavid Howells 	req->actual_len = i_size; /* May change */
253f3ddee8dSDavid Howells 	req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
254f3ddee8dSDavid Howells 	req->data_version = dvnode->status.data_version; /* May change */
255f3ddee8dSDavid Howells 	if (nr_inline > 0) {
256f3ddee8dSDavid Howells 		req->pages = req->array;
257f3ddee8dSDavid Howells 	} else {
258f3ddee8dSDavid Howells 		req->pages = kcalloc(nr_pages, sizeof(struct page *),
259f3ddee8dSDavid Howells 				     GFP_KERNEL);
260f3ddee8dSDavid Howells 		if (!req->pages)
261f3ddee8dSDavid Howells 			goto error;
262f3ddee8dSDavid Howells 	}
263f3ddee8dSDavid Howells 
264f3ddee8dSDavid Howells 	/* Get a list of all the pages that hold or will hold the directory
265f3ddee8dSDavid Howells 	 * content.  We need to fill in any gaps that we might find where the
266f3ddee8dSDavid Howells 	 * memory reclaimer has been at work.  If there are any gaps, we will
267f3ddee8dSDavid Howells 	 * need to reread the entire directory contents.
268f3ddee8dSDavid Howells 	 */
269f3ddee8dSDavid Howells 	i = 0;
270f3ddee8dSDavid Howells 	do {
271f3ddee8dSDavid Howells 		n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
272f3ddee8dSDavid Howells 					  req->nr_pages - i,
273f3ddee8dSDavid Howells 					  req->pages + i);
274f3ddee8dSDavid Howells 		_debug("find %u at %u/%u", n, i, req->nr_pages);
275f3ddee8dSDavid Howells 		if (n == 0) {
276f3ddee8dSDavid Howells 			gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
277f3ddee8dSDavid Howells 
278f3ddee8dSDavid Howells 			if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
279f3ddee8dSDavid Howells 				afs_stat_v(dvnode, n_inval);
280f3ddee8dSDavid Howells 
281f3ddee8dSDavid Howells 			ret = -ENOMEM;
282f3ddee8dSDavid Howells 			req->pages[i] = __page_cache_alloc(gfp);
283f3ddee8dSDavid Howells 			if (!req->pages[i])
284f3ddee8dSDavid Howells 				goto error;
285f3ddee8dSDavid Howells 			ret = add_to_page_cache_lru(req->pages[i],
286f3ddee8dSDavid Howells 						    dvnode->vfs_inode.i_mapping,
287f3ddee8dSDavid Howells 						    i, gfp);
288f3ddee8dSDavid Howells 			if (ret < 0)
289f3ddee8dSDavid Howells 				goto error;
290f3ddee8dSDavid Howells 
291f3ddee8dSDavid Howells 			set_page_private(req->pages[i], 1);
292f3ddee8dSDavid Howells 			SetPagePrivate(req->pages[i]);
293f3ddee8dSDavid Howells 			unlock_page(req->pages[i]);
294f3ddee8dSDavid Howells 			i++;
295f3ddee8dSDavid Howells 		} else {
296f3ddee8dSDavid Howells 			i += n;
297f3ddee8dSDavid Howells 		}
298f3ddee8dSDavid Howells 	} while (i < req->nr_pages);
299f3ddee8dSDavid Howells 
300f3ddee8dSDavid Howells 	/* If we're going to reload, we need to lock all the pages to prevent
301f3ddee8dSDavid Howells 	 * races.
302f3ddee8dSDavid Howells 	 */
303f3ddee8dSDavid Howells 	ret = -ERESTARTSYS;
304b61f7dcfSDavid Howells 	if (down_read_killable(&dvnode->validate_lock) < 0)
305b61f7dcfSDavid Howells 		goto error;
306f3ddee8dSDavid Howells 
307f3ddee8dSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
308f3ddee8dSDavid Howells 		goto success;
309f3ddee8dSDavid Howells 
310b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
311b61f7dcfSDavid Howells 	if (down_write_killable(&dvnode->validate_lock) < 0)
312b61f7dcfSDavid Howells 		goto error;
313b61f7dcfSDavid Howells 
314b61f7dcfSDavid Howells 	if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
31599987c56SDavid Howells 		trace_afs_reload_dir(dvnode);
316f3ddee8dSDavid Howells 		ret = afs_fetch_data(dvnode, key, req);
317f3ddee8dSDavid Howells 		if (ret < 0)
318b61f7dcfSDavid Howells 			goto error_unlock;
319f3ddee8dSDavid Howells 
320f3ddee8dSDavid Howells 		task_io_account_read(PAGE_SIZE * req->nr_pages);
321f3ddee8dSDavid Howells 
322f3ddee8dSDavid Howells 		if (req->len < req->file_size)
323f3ddee8dSDavid Howells 			goto content_has_grown;
324f3ddee8dSDavid Howells 
325f3ddee8dSDavid Howells 		/* Validate the data we just read. */
326f3ddee8dSDavid Howells 		ret = -EIO;
327445b1028SDavid Howells 		if (!afs_dir_check_pages(dvnode, req))
328b61f7dcfSDavid Howells 			goto error_unlock;
329f3ddee8dSDavid Howells 
330f3ddee8dSDavid Howells 		// TODO: Trim excess pages
331f3ddee8dSDavid Howells 
332f3ddee8dSDavid Howells 		set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
333f3ddee8dSDavid Howells 	}
334f3ddee8dSDavid Howells 
335b61f7dcfSDavid Howells 	downgrade_write(&dvnode->validate_lock);
336f3ddee8dSDavid Howells success:
337f3ddee8dSDavid Howells 	return req;
338f3ddee8dSDavid Howells 
339f3ddee8dSDavid Howells error_unlock:
340b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
341f3ddee8dSDavid Howells error:
342f3ddee8dSDavid Howells 	afs_put_read(req);
343f3ddee8dSDavid Howells 	_leave(" = %d", ret);
344f3ddee8dSDavid Howells 	return ERR_PTR(ret);
345f3ddee8dSDavid Howells 
346f3ddee8dSDavid Howells content_has_grown:
347b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
348f3ddee8dSDavid Howells 	afs_put_read(req);
349f3ddee8dSDavid Howells 	goto retry;
350f3ddee8dSDavid Howells }
351f3ddee8dSDavid Howells 
352f3ddee8dSDavid Howells /*
3531da177e4SLinus Torvalds  * deal with one block in an AFS directory
3541da177e4SLinus Torvalds  */
355f51375cdSDavid Howells static int afs_dir_iterate_block(struct afs_vnode *dvnode,
356f51375cdSDavid Howells 				 struct dir_context *ctx,
35700317636SDavid Howells 				 union afs_xdr_dir_block *block,
3581bbae9f8SAl Viro 				 unsigned blkoff)
3591da177e4SLinus Torvalds {
36000317636SDavid Howells 	union afs_xdr_dirent *dire;
3611da177e4SLinus Torvalds 	unsigned offset, next, curr;
3621da177e4SLinus Torvalds 	size_t nlen;
3631bbae9f8SAl Viro 	int tmp;
3641da177e4SLinus Torvalds 
3651bbae9f8SAl Viro 	_enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
3661da177e4SLinus Torvalds 
36700317636SDavid Howells 	curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	/* walk through the block, an entry at a time */
3704ea219a8SDavid Howells 	for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
3714ea219a8SDavid Howells 	     offset < AFS_DIR_SLOTS_PER_BLOCK;
3721da177e4SLinus Torvalds 	     offset = next
3731da177e4SLinus Torvalds 	     ) {
3741da177e4SLinus Torvalds 		next = offset + 1;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 		/* skip entries marked unused in the bitmap */
37700317636SDavid Howells 		if (!(block->hdr.bitmap[offset / 8] &
3781da177e4SLinus Torvalds 		      (1 << (offset % 8)))) {
3795b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: unused",
38000317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block), offset);
3811da177e4SLinus Torvalds 			if (offset >= curr)
3821bbae9f8SAl Viro 				ctx->pos = blkoff +
38300317636SDavid Howells 					next * sizeof(union afs_xdr_dirent);
3841da177e4SLinus Torvalds 			continue;
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 		/* got a valid entry */
3881da177e4SLinus Torvalds 		dire = &block->dirents[offset];
3891da177e4SLinus Torvalds 		nlen = strnlen(dire->u.name,
3901da177e4SLinus Torvalds 			       sizeof(*block) -
39100317636SDavid Howells 			       offset * sizeof(union afs_xdr_dirent));
3921da177e4SLinus Torvalds 
3935b5e0928SAlexey Dobriyan 		_debug("ENT[%zu.%u]: %s %zu \"%s\"",
39400317636SDavid Howells 		       blkoff / sizeof(union afs_xdr_dir_block), offset,
3951da177e4SLinus Torvalds 		       (offset < curr ? "skip" : "fill"),
3961da177e4SLinus Torvalds 		       nlen, dire->u.name);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 		/* work out where the next possible entry is */
39900317636SDavid Howells 		for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
4004ea219a8SDavid Howells 			if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
4015b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
4021da177e4SLinus Torvalds 				       " %u travelled beyond end dir block"
4035b5e0928SAlexey Dobriyan 				       " (len %u/%zu)",
40400317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4051da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
406f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_over_end);
4071da177e4SLinus Torvalds 			}
40800317636SDavid Howells 			if (!(block->hdr.bitmap[next / 8] &
4091da177e4SLinus Torvalds 			      (1 << (next % 8)))) {
4105b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
4115b5e0928SAlexey Dobriyan 				       " %u unmarked extension (len %u/%zu)",
41200317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4131da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
414f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
4151da177e4SLinus Torvalds 			}
4161da177e4SLinus Torvalds 
4175b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: ext %u/%zu",
41800317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block),
4191da177e4SLinus Torvalds 			       next, tmp, nlen);
4201da177e4SLinus Torvalds 			next++;
4211da177e4SLinus Torvalds 		}
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 		/* skip if starts before the current position */
4241da177e4SLinus Torvalds 		if (offset < curr)
4251da177e4SLinus Torvalds 			continue;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 		/* found the next entry */
4281bbae9f8SAl Viro 		if (!dir_emit(ctx, dire->u.name, nlen,
4291da177e4SLinus Torvalds 			      ntohl(dire->u.vnode),
4305cf9dd55SDavid Howells 			      (ctx->actor == afs_lookup_filldir ||
4315cf9dd55SDavid Howells 			       ctx->actor == afs_lookup_one_filldir)?
4321bbae9f8SAl Viro 			      ntohl(dire->u.unique) : DT_UNKNOWN)) {
4331da177e4SLinus Torvalds 			_leave(" = 0 [full]");
4341da177e4SLinus Torvalds 			return 0;
4351da177e4SLinus Torvalds 		}
4361da177e4SLinus Torvalds 
43700317636SDavid Howells 		ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	_leave(" = 1 [more]");
4411da177e4SLinus Torvalds 	return 1;
442ec26815aSDavid Howells }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds /*
44508e0e7c8SDavid Howells  * iterate through the data blob that lists the contents of an AFS directory
4461da177e4SLinus Torvalds  */
4471bbae9f8SAl Viro static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
4481bbae9f8SAl Viro 			   struct key *key)
4491da177e4SLinus Torvalds {
450f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
45100317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
45200317636SDavid Howells 	union afs_xdr_dir_block *dblock;
453f3ddee8dSDavid Howells 	struct afs_read *req;
4541da177e4SLinus Torvalds 	struct page *page;
4551da177e4SLinus Torvalds 	unsigned blkoff, limit;
4561da177e4SLinus Torvalds 	int ret;
4571da177e4SLinus Torvalds 
4581bbae9f8SAl Viro 	_enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
4591da177e4SLinus Torvalds 
46008e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
4611da177e4SLinus Torvalds 		_leave(" = -ESTALE");
4621da177e4SLinus Torvalds 		return -ESTALE;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 
465f3ddee8dSDavid Howells 	req = afs_read_dir(dvnode, key);
466f3ddee8dSDavid Howells 	if (IS_ERR(req))
467f3ddee8dSDavid Howells 		return PTR_ERR(req);
468f3ddee8dSDavid Howells 
4691da177e4SLinus Torvalds 	/* round the file position up to the next entry boundary */
47000317636SDavid Howells 	ctx->pos += sizeof(union afs_xdr_dirent) - 1;
47100317636SDavid Howells 	ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	/* walk through the blocks in sequence */
4741da177e4SLinus Torvalds 	ret = 0;
475f3ddee8dSDavid Howells 	while (ctx->pos < req->actual_len) {
47600317636SDavid Howells 		blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
4771da177e4SLinus Torvalds 
478f3ddee8dSDavid Howells 		/* Fetch the appropriate page from the directory and re-add it
479f3ddee8dSDavid Howells 		 * to the LRU.
480f3ddee8dSDavid Howells 		 */
481f3ddee8dSDavid Howells 		page = req->pages[blkoff / PAGE_SIZE];
482f3ddee8dSDavid Howells 		if (!page) {
483f51375cdSDavid Howells 			ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
4841da177e4SLinus Torvalds 			break;
4851da177e4SLinus Torvalds 		}
486f3ddee8dSDavid Howells 		mark_page_accessed(page);
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 		limit = blkoff & ~(PAGE_SIZE - 1);
4891da177e4SLinus Torvalds 
490f3ddee8dSDavid Howells 		dbuf = kmap(page);
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 		/* deal with the individual blocks stashed on this page */
4931da177e4SLinus Torvalds 		do {
4941da177e4SLinus Torvalds 			dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
49500317636SDavid Howells 					       sizeof(union afs_xdr_dir_block)];
496f51375cdSDavid Howells 			ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
4971da177e4SLinus Torvalds 			if (ret != 1) {
498f3ddee8dSDavid Howells 				kunmap(page);
4991da177e4SLinus Torvalds 				goto out;
5001da177e4SLinus Torvalds 			}
5011da177e4SLinus Torvalds 
50200317636SDavid Howells 			blkoff += sizeof(union afs_xdr_dir_block);
5031da177e4SLinus Torvalds 
5041bbae9f8SAl Viro 		} while (ctx->pos < dir->i_size && blkoff < limit);
5051da177e4SLinus Torvalds 
506f3ddee8dSDavid Howells 		kunmap(page);
5071da177e4SLinus Torvalds 		ret = 0;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds out:
511b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
512f3ddee8dSDavid Howells 	afs_put_read(req);
5131da177e4SLinus Torvalds 	_leave(" = %d", ret);
5141da177e4SLinus Torvalds 	return ret;
515ec26815aSDavid Howells }
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds /*
5181da177e4SLinus Torvalds  * read an AFS directory
5191da177e4SLinus Torvalds  */
5201bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx)
5211da177e4SLinus Torvalds {
522215804a9SDavid Howells 	return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
523ec26815aSDavid Howells }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds /*
5265cf9dd55SDavid Howells  * Search the directory for a single name
5271da177e4SLinus Torvalds  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5281da177e4SLinus Torvalds  *   uniquifier through dtype
5291da177e4SLinus Torvalds  */
5305cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
531ac7576f4SMiklos Szeredi 				  int nlen, loff_t fpos, u64 ino, unsigned dtype)
5321da177e4SLinus Torvalds {
5335cf9dd55SDavid Howells 	struct afs_lookup_one_cookie *cookie =
5345cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_one_cookie, ctx);
5351da177e4SLinus Torvalds 
5361bbae9f8SAl Viro 	_enter("{%s,%u},%s,%u,,%llu,%u",
5371bbae9f8SAl Viro 	       cookie->name.name, cookie->name.len, name, nlen,
538ba3e0e1aSDavid S. Miller 	       (unsigned long long) ino, dtype);
5391da177e4SLinus Torvalds 
54008e0e7c8SDavid Howells 	/* insanity checks first */
54100317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
54200317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
54308e0e7c8SDavid Howells 
5441bbae9f8SAl Viro 	if (cookie->name.len != nlen ||
5451bbae9f8SAl Viro 	    memcmp(cookie->name.name, name, nlen) != 0) {
5461da177e4SLinus Torvalds 		_leave(" = 0 [no]");
5471da177e4SLinus Torvalds 		return 0;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	cookie->fid.vnode = ino;
5511da177e4SLinus Torvalds 	cookie->fid.unique = dtype;
5521da177e4SLinus Torvalds 	cookie->found = 1;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	_leave(" = -1 [found]");
5551da177e4SLinus Torvalds 	return -1;
556ec26815aSDavid Howells }
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds /*
5595cf9dd55SDavid Howells  * Do a lookup of a single name in a directory
560260a9803SDavid Howells  * - just returns the FID the dentry name maps to if found
5611da177e4SLinus Torvalds  */
5625cf9dd55SDavid Howells static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
56300d3b7a4SDavid Howells 			     struct afs_fid *fid, struct key *key)
5641da177e4SLinus Torvalds {
5651bbae9f8SAl Viro 	struct afs_super_info *as = dir->i_sb->s_fs_info;
5665cf9dd55SDavid Howells 	struct afs_lookup_one_cookie cookie = {
5675cf9dd55SDavid Howells 		.ctx.actor = afs_lookup_one_filldir,
5681bbae9f8SAl Viro 		.name = dentry->d_name,
5691bbae9f8SAl Viro 		.fid.vid = as->volume->vid
5701bbae9f8SAl Viro 	};
5711da177e4SLinus Torvalds 	int ret;
5721da177e4SLinus Torvalds 
573a455589fSAl Viro 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	/* search the directory */
5761bbae9f8SAl Viro 	ret = afs_dir_iterate(dir, &cookie.ctx, key);
5771da177e4SLinus Torvalds 	if (ret < 0) {
57808e0e7c8SDavid Howells 		_leave(" = %d [iter]", ret);
57908e0e7c8SDavid Howells 		return ret;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	ret = -ENOENT;
5831da177e4SLinus Torvalds 	if (!cookie.found) {
58408e0e7c8SDavid Howells 		_leave(" = -ENOENT [not found]");
58508e0e7c8SDavid Howells 		return -ENOENT;
58608e0e7c8SDavid Howells 	}
58708e0e7c8SDavid Howells 
58808e0e7c8SDavid Howells 	*fid = cookie.fid;
5893b6492dfSDavid Howells 	_leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
59008e0e7c8SDavid Howells 	return 0;
59108e0e7c8SDavid Howells }
59208e0e7c8SDavid Howells 
59308e0e7c8SDavid Howells /*
5945cf9dd55SDavid Howells  * search the directory for a name
5955cf9dd55SDavid Howells  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5965cf9dd55SDavid Howells  *   uniquifier through dtype
5975cf9dd55SDavid Howells  */
5985cf9dd55SDavid Howells static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
5995cf9dd55SDavid Howells 			      int nlen, loff_t fpos, u64 ino, unsigned dtype)
6005cf9dd55SDavid Howells {
6015cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie =
6025cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_cookie, ctx);
6035cf9dd55SDavid Howells 	int ret;
6045cf9dd55SDavid Howells 
6055cf9dd55SDavid Howells 	_enter("{%s,%u},%s,%u,,%llu,%u",
6065cf9dd55SDavid Howells 	       cookie->name.name, cookie->name.len, name, nlen,
6075cf9dd55SDavid Howells 	       (unsigned long long) ino, dtype);
6085cf9dd55SDavid Howells 
6095cf9dd55SDavid Howells 	/* insanity checks first */
61000317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
61100317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
6125cf9dd55SDavid Howells 
6135cf9dd55SDavid Howells 	if (cookie->found) {
6145cf9dd55SDavid Howells 		if (cookie->nr_fids < 50) {
6155cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].vnode	= ino;
6165cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].unique	= dtype;
6175cf9dd55SDavid Howells 			cookie->nr_fids++;
6185cf9dd55SDavid Howells 		}
6195cf9dd55SDavid Howells 	} else if (cookie->name.len == nlen &&
6205cf9dd55SDavid Howells 		   memcmp(cookie->name.name, name, nlen) == 0) {
6215cf9dd55SDavid Howells 		cookie->fids[0].vnode	= ino;
6225cf9dd55SDavid Howells 		cookie->fids[0].unique	= dtype;
6235cf9dd55SDavid Howells 		cookie->found = 1;
6245cf9dd55SDavid Howells 		if (cookie->one_only)
6255cf9dd55SDavid Howells 			return -1;
6265cf9dd55SDavid Howells 	}
6275cf9dd55SDavid Howells 
6285cf9dd55SDavid Howells 	ret = cookie->nr_fids >= 50 ? -1 : 0;
6295cf9dd55SDavid Howells 	_leave(" = %d", ret);
6305cf9dd55SDavid Howells 	return ret;
6315cf9dd55SDavid Howells }
6325cf9dd55SDavid Howells 
6335cf9dd55SDavid Howells /*
6345cf9dd55SDavid Howells  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
6355cf9dd55SDavid Howells  * files in one go and create inodes for them.  The inode of the file we were
6365cf9dd55SDavid Howells  * asked for is returned.
6375cf9dd55SDavid Howells  */
6385cf9dd55SDavid Howells static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
6395cf9dd55SDavid Howells 				   struct key *key)
6405cf9dd55SDavid Howells {
6415cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie;
642f642404aSDavid Howells 	struct afs_cb_interest *dcbi, *cbi = NULL;
6435cf9dd55SDavid Howells 	struct afs_super_info *as = dir->i_sb->s_fs_info;
64487182759SDavid Howells 	struct afs_status_cb *scb;
645b8359153SDavid Howells 	struct afs_iget_data iget_data;
6465cf9dd55SDavid Howells 	struct afs_fs_cursor fc;
647f642404aSDavid Howells 	struct afs_server *server;
64839db9815SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
64939db9815SDavid Howells 	struct inode *inode = NULL, *ti;
6505cf9dd55SDavid Howells 	int ret, i;
6515cf9dd55SDavid Howells 
6525cf9dd55SDavid Howells 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
6535cf9dd55SDavid Howells 
6545cf9dd55SDavid Howells 	cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
6555cf9dd55SDavid Howells 	if (!cookie)
6565cf9dd55SDavid Howells 		return ERR_PTR(-ENOMEM);
6575cf9dd55SDavid Howells 
6585cf9dd55SDavid Howells 	cookie->ctx.actor = afs_lookup_filldir;
6595cf9dd55SDavid Howells 	cookie->name = dentry->d_name;
6605cf9dd55SDavid Howells 	cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
6615cf9dd55SDavid Howells 
6625cf9dd55SDavid Howells 	read_seqlock_excl(&dvnode->cb_lock);
663f642404aSDavid Howells 	dcbi = rcu_dereference_protected(dvnode->cb_interest,
664f642404aSDavid Howells 					 lockdep_is_held(&dvnode->cb_lock.lock));
665f642404aSDavid Howells 	if (dcbi) {
666f642404aSDavid Howells 		server = dcbi->server;
667f642404aSDavid Howells 		if (server &&
668f642404aSDavid Howells 		    test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
6695cf9dd55SDavid Howells 			cookie->one_only = true;
670f642404aSDavid Howells 	}
6715cf9dd55SDavid Howells 	read_sequnlock_excl(&dvnode->cb_lock);
6725cf9dd55SDavid Howells 
6735cf9dd55SDavid Howells 	for (i = 0; i < 50; i++)
6745cf9dd55SDavid Howells 		cookie->fids[i].vid = as->volume->vid;
6755cf9dd55SDavid Howells 
6765cf9dd55SDavid Howells 	/* search the directory */
6775cf9dd55SDavid Howells 	ret = afs_dir_iterate(dir, &cookie->ctx, key);
6785cf9dd55SDavid Howells 	if (ret < 0) {
6795cf9dd55SDavid Howells 		inode = ERR_PTR(ret);
6805cf9dd55SDavid Howells 		goto out;
6815cf9dd55SDavid Howells 	}
6825cf9dd55SDavid Howells 
6835cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOENT);
6845cf9dd55SDavid Howells 	if (!cookie->found)
6855cf9dd55SDavid Howells 		goto out;
6865cf9dd55SDavid Howells 
6875cf9dd55SDavid Howells 	/* Check to see if we already have an inode for the primary fid. */
688b8359153SDavid Howells 	iget_data.fid = cookie->fids[0];
689b8359153SDavid Howells 	iget_data.volume = dvnode->volume;
690b8359153SDavid Howells 	iget_data.cb_v_break = dvnode->volume->cb_v_break;
691b8359153SDavid Howells 	iget_data.cb_s_break = 0;
692b8359153SDavid Howells 	inode = ilookup5(dir->i_sb, cookie->fids[0].vnode,
693b8359153SDavid Howells 			 afs_iget5_test, &iget_data);
6945cf9dd55SDavid Howells 	if (inode)
6955cf9dd55SDavid Howells 		goto out;
6965cf9dd55SDavid Howells 
6975cf9dd55SDavid Howells 	/* Need space for examining all the selected files */
6985cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOMEM);
69987182759SDavid Howells 	cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb),
7005cf9dd55SDavid Howells 				    GFP_KERNEL);
7015cf9dd55SDavid Howells 	if (!cookie->statuses)
7025cf9dd55SDavid Howells 		goto out;
7035cf9dd55SDavid Howells 
70439db9815SDavid Howells 	cookie->inodes = kcalloc(cookie->nr_fids, sizeof(struct inode *),
70539db9815SDavid Howells 				 GFP_KERNEL);
70639db9815SDavid Howells 	if (!cookie->inodes)
70739db9815SDavid Howells 		goto out_s;
70839db9815SDavid Howells 
70939db9815SDavid Howells 	for (i = 1; i < cookie->nr_fids; i++) {
71039db9815SDavid Howells 		scb = &cookie->statuses[i];
71139db9815SDavid Howells 
71239db9815SDavid Howells 		/* Find any inodes that already exist and get their
71339db9815SDavid Howells 		 * callback counters.
71439db9815SDavid Howells 		 */
71539db9815SDavid Howells 		iget_data.fid = cookie->fids[i];
71639db9815SDavid Howells 		ti = ilookup5_nowait(dir->i_sb, iget_data.fid.vnode,
71739db9815SDavid Howells 				     afs_iget5_test, &iget_data);
71839db9815SDavid Howells 		if (!IS_ERR_OR_NULL(ti)) {
71939db9815SDavid Howells 			vnode = AFS_FS_I(ti);
72039db9815SDavid Howells 			scb->cb_break = afs_calc_vnode_cb_break(vnode);
72139db9815SDavid Howells 			cookie->inodes[i] = ti;
72239db9815SDavid Howells 		}
72339db9815SDavid Howells 	}
72439db9815SDavid Howells 
7255cf9dd55SDavid Howells 	/* Try FS.InlineBulkStatus first.  Abort codes for the individual
7265cf9dd55SDavid Howells 	 * lookups contained therein are stored in the reply without aborting
7275cf9dd55SDavid Howells 	 * the whole operation.
7285cf9dd55SDavid Howells 	 */
7295cf9dd55SDavid Howells 	if (cookie->one_only)
7305cf9dd55SDavid Howells 		goto no_inline_bulk_status;
7315cf9dd55SDavid Howells 
7325cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
73320b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
7345cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
7355cf9dd55SDavid Howells 			if (test_bit(AFS_SERVER_FL_NO_IBULK,
7365cf9dd55SDavid Howells 				      &fc.cbi->server->flags)) {
7375cf9dd55SDavid Howells 				fc.ac.abort_code = RX_INVALID_OPERATION;
7385cf9dd55SDavid Howells 				fc.ac.error = -ECONNABORTED;
7395cf9dd55SDavid Howells 				break;
7405cf9dd55SDavid Howells 			}
741b8359153SDavid Howells 			iget_data.cb_v_break = dvnode->volume->cb_v_break;
742b8359153SDavid Howells 			iget_data.cb_s_break = fc.cbi->server->cb_s_break;
7435cf9dd55SDavid Howells 			afs_fs_inline_bulk_status(&fc,
7445cf9dd55SDavid Howells 						  afs_v2net(dvnode),
7455cf9dd55SDavid Howells 						  cookie->fids,
7465cf9dd55SDavid Howells 						  cookie->statuses,
7475cf9dd55SDavid Howells 						  cookie->nr_fids, NULL);
7485cf9dd55SDavid Howells 		}
7495cf9dd55SDavid Howells 
7505cf9dd55SDavid Howells 		if (fc.ac.error == 0)
7515cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
7525cf9dd55SDavid Howells 		if (fc.ac.abort_code == RX_INVALID_OPERATION)
7535cf9dd55SDavid Howells 			set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
7545cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
7555cf9dd55SDavid Howells 	}
7565cf9dd55SDavid Howells 
7575cf9dd55SDavid Howells 	if (!IS_ERR(inode))
7585cf9dd55SDavid Howells 		goto success;
7595cf9dd55SDavid Howells 	if (fc.ac.abort_code != RX_INVALID_OPERATION)
7605cf9dd55SDavid Howells 		goto out_c;
7615cf9dd55SDavid Howells 
7625cf9dd55SDavid Howells no_inline_bulk_status:
7635cf9dd55SDavid Howells 	/* We could try FS.BulkStatus next, but this aborts the entire op if
7645cf9dd55SDavid Howells 	 * any of the lookups fails - so, for the moment, revert to
7655cf9dd55SDavid Howells 	 * FS.FetchStatus for just the primary fid.
7665cf9dd55SDavid Howells 	 */
7675cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
76820b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
7695cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
770b8359153SDavid Howells 			iget_data.cb_v_break = dvnode->volume->cb_v_break;
771b8359153SDavid Howells 			iget_data.cb_s_break = fc.cbi->server->cb_s_break;
77287182759SDavid Howells 			scb = &cookie->statuses[0];
7735cf9dd55SDavid Howells 			afs_fs_fetch_status(&fc,
7745cf9dd55SDavid Howells 					    afs_v2net(dvnode),
7755cf9dd55SDavid Howells 					    cookie->fids,
776a58823acSDavid Howells 					    scb,
7775cf9dd55SDavid Howells 					    NULL);
7785cf9dd55SDavid Howells 		}
7795cf9dd55SDavid Howells 
7805cf9dd55SDavid Howells 		if (fc.ac.error == 0)
7815cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
7825cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
7835cf9dd55SDavid Howells 	}
7845cf9dd55SDavid Howells 
7855cf9dd55SDavid Howells 	if (IS_ERR(inode))
7865cf9dd55SDavid Howells 		goto out_c;
7875cf9dd55SDavid Howells 
7885cf9dd55SDavid Howells success:
7895cf9dd55SDavid Howells 	/* Turn all the files into inodes and save the first one - which is the
7905cf9dd55SDavid Howells 	 * one we actually want.
7915cf9dd55SDavid Howells 	 */
79287182759SDavid Howells 	scb = &cookie->statuses[0];
79387182759SDavid Howells 	if (scb->status.abort_code != 0)
79487182759SDavid Howells 		inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code));
7955cf9dd55SDavid Howells 
7965cf9dd55SDavid Howells 	for (i = 0; i < cookie->nr_fids; i++) {
79787182759SDavid Howells 		struct afs_status_cb *scb = &cookie->statuses[i];
79839db9815SDavid Howells 
79939db9815SDavid Howells 		if (!scb->have_status && !scb->have_error)
80039db9815SDavid Howells 			continue;
80139db9815SDavid Howells 
80239db9815SDavid Howells 		if (cookie->inodes[i]) {
80339db9815SDavid Howells 			afs_vnode_commit_status(&fc, AFS_FS_I(cookie->inodes[i]),
80439db9815SDavid Howells 						scb->cb_break, NULL, scb);
80539db9815SDavid Howells 			continue;
80639db9815SDavid Howells 		}
8075cf9dd55SDavid Howells 
80887182759SDavid Howells 		if (scb->status.abort_code != 0)
8095cf9dd55SDavid Howells 			continue;
8105cf9dd55SDavid Howells 
811b8359153SDavid Howells 		iget_data.fid = cookie->fids[i];
812b8359153SDavid Howells 		ti = afs_iget(dir->i_sb, key, &iget_data, scb, cbi, dvnode);
81339db9815SDavid Howells 		if (!IS_ERR(ti))
81439db9815SDavid Howells 			afs_cache_permit(AFS_FS_I(ti), key,
81539db9815SDavid Howells 					 0 /* Assume vnode->cb_break is 0 */ +
81639db9815SDavid Howells 					 iget_data.cb_v_break,
81739db9815SDavid Howells 					 scb);
8185cf9dd55SDavid Howells 		if (i == 0) {
8195cf9dd55SDavid Howells 			inode = ti;
8205cf9dd55SDavid Howells 		} else {
8215cf9dd55SDavid Howells 			if (!IS_ERR(ti))
8225cf9dd55SDavid Howells 				iput(ti);
8235cf9dd55SDavid Howells 		}
8245cf9dd55SDavid Howells 	}
8255cf9dd55SDavid Howells 
8265cf9dd55SDavid Howells out_c:
8275cf9dd55SDavid Howells 	afs_put_cb_interest(afs_v2net(dvnode), cbi);
82839db9815SDavid Howells 	if (cookie->inodes) {
82939db9815SDavid Howells 		for (i = 0; i < cookie->nr_fids; i++)
83039db9815SDavid Howells 			iput(cookie->inodes[i]);
83139db9815SDavid Howells 		kfree(cookie->inodes);
83239db9815SDavid Howells 	}
83339db9815SDavid Howells out_s:
83487182759SDavid Howells 	kvfree(cookie->statuses);
8355cf9dd55SDavid Howells out:
8365cf9dd55SDavid Howells 	kfree(cookie);
8375cf9dd55SDavid Howells 	return inode;
8385cf9dd55SDavid Howells }
8395cf9dd55SDavid Howells 
8405cf9dd55SDavid Howells /*
8416f8880d8SDavid Howells  * Look up an entry in a directory with @sys substitution.
8426f8880d8SDavid Howells  */
8436f8880d8SDavid Howells static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
8446f8880d8SDavid Howells 				       struct key *key)
8456f8880d8SDavid Howells {
8466f8880d8SDavid Howells 	struct afs_sysnames *subs;
8476f8880d8SDavid Howells 	struct afs_net *net = afs_i2net(dir);
8486f8880d8SDavid Howells 	struct dentry *ret;
8496f8880d8SDavid Howells 	char *buf, *p, *name;
8506f8880d8SDavid Howells 	int len, i;
8516f8880d8SDavid Howells 
8526f8880d8SDavid Howells 	_enter("");
8536f8880d8SDavid Howells 
8546f8880d8SDavid Howells 	ret = ERR_PTR(-ENOMEM);
8556f8880d8SDavid Howells 	p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
8566f8880d8SDavid Howells 	if (!buf)
8576f8880d8SDavid Howells 		goto out_p;
8586f8880d8SDavid Howells 	if (dentry->d_name.len > 4) {
8596f8880d8SDavid Howells 		memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
8606f8880d8SDavid Howells 		p += dentry->d_name.len - 4;
8616f8880d8SDavid Howells 	}
8626f8880d8SDavid Howells 
8636f8880d8SDavid Howells 	/* There is an ordered list of substitutes that we have to try. */
8646f8880d8SDavid Howells 	read_lock(&net->sysnames_lock);
8656f8880d8SDavid Howells 	subs = net->sysnames;
8666f8880d8SDavid Howells 	refcount_inc(&subs->usage);
8676f8880d8SDavid Howells 	read_unlock(&net->sysnames_lock);
8686f8880d8SDavid Howells 
8696f8880d8SDavid Howells 	for (i = 0; i < subs->nr; i++) {
8706f8880d8SDavid Howells 		name = subs->subs[i];
8716f8880d8SDavid Howells 		len = dentry->d_name.len - 4 + strlen(name);
8726f8880d8SDavid Howells 		if (len >= AFSNAMEMAX) {
8736f8880d8SDavid Howells 			ret = ERR_PTR(-ENAMETOOLONG);
8746f8880d8SDavid Howells 			goto out_s;
8756f8880d8SDavid Howells 		}
8766f8880d8SDavid Howells 
8776f8880d8SDavid Howells 		strcpy(p, name);
8786f8880d8SDavid Howells 		ret = lookup_one_len(buf, dentry->d_parent, len);
8796f8880d8SDavid Howells 		if (IS_ERR(ret) || d_is_positive(ret))
8806f8880d8SDavid Howells 			goto out_s;
8816f8880d8SDavid Howells 		dput(ret);
8826f8880d8SDavid Howells 	}
8836f8880d8SDavid Howells 
8846f8880d8SDavid Howells 	/* We don't want to d_add() the @sys dentry here as we don't want to
8856f8880d8SDavid Howells 	 * the cached dentry to hide changes to the sysnames list.
8866f8880d8SDavid Howells 	 */
8876f8880d8SDavid Howells 	ret = NULL;
8886f8880d8SDavid Howells out_s:
8896f8880d8SDavid Howells 	afs_put_sysnames(subs);
8906f8880d8SDavid Howells 	kfree(buf);
8916f8880d8SDavid Howells out_p:
8926f8880d8SDavid Howells 	key_put(key);
8936f8880d8SDavid Howells 	return ret;
8946f8880d8SDavid Howells }
8956f8880d8SDavid Howells 
8966f8880d8SDavid Howells /*
89708e0e7c8SDavid Howells  * look up an entry in a directory
89808e0e7c8SDavid Howells  */
899260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
90000cd8dd3SAl Viro 				 unsigned int flags)
90108e0e7c8SDavid Howells {
9025cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
90308e0e7c8SDavid Howells 	struct inode *inode;
90434b2a88fSAl Viro 	struct dentry *d;
90500d3b7a4SDavid Howells 	struct key *key;
90608e0e7c8SDavid Howells 	int ret;
90708e0e7c8SDavid Howells 
9083b6492dfSDavid Howells 	_enter("{%llx:%llu},%p{%pd},",
9095cf9dd55SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
910260a9803SDavid Howells 
9112b0143b5SDavid Howells 	ASSERTCMP(d_inode(dentry), ==, NULL);
91208e0e7c8SDavid Howells 
91345222b9eSDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX) {
91408e0e7c8SDavid Howells 		_leave(" = -ENAMETOOLONG");
91508e0e7c8SDavid Howells 		return ERR_PTR(-ENAMETOOLONG);
91608e0e7c8SDavid Howells 	}
91708e0e7c8SDavid Howells 
9185cf9dd55SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
91908e0e7c8SDavid Howells 		_leave(" = -ESTALE");
92008e0e7c8SDavid Howells 		return ERR_PTR(-ESTALE);
92108e0e7c8SDavid Howells 	}
92208e0e7c8SDavid Howells 
9235cf9dd55SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
92400d3b7a4SDavid Howells 	if (IS_ERR(key)) {
92500d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
926e231c2eeSDavid Howells 		return ERR_CAST(key);
92700d3b7a4SDavid Howells 	}
92800d3b7a4SDavid Howells 
9295cf9dd55SDavid Howells 	ret = afs_validate(dvnode, key);
93008e0e7c8SDavid Howells 	if (ret < 0) {
93100d3b7a4SDavid Howells 		key_put(key);
932260a9803SDavid Howells 		_leave(" = %d [val]", ret);
9331da177e4SLinus Torvalds 		return ERR_PTR(ret);
9341da177e4SLinus Torvalds 	}
9351da177e4SLinus Torvalds 
9366f8880d8SDavid Howells 	if (dentry->d_name.len >= 4 &&
9376f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
9386f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
9396f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
9406f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 1] == 's')
9416f8880d8SDavid Howells 		return afs_lookup_atsys(dir, dentry, key);
9426f8880d8SDavid Howells 
943d55b4da4SDavid Howells 	afs_stat_v(dvnode, n_lookup);
9445cf9dd55SDavid Howells 	inode = afs_do_lookup(dir, dentry, key);
94534b2a88fSAl Viro 	key_put(key);
94634b2a88fSAl Viro 	if (inode == ERR_PTR(-ENOENT)) {
9475cf9dd55SDavid Howells 		inode = afs_try_auto_mntpt(dentry, dir);
94834b2a88fSAl Viro 	} else {
94934b2a88fSAl Viro 		dentry->d_fsdata =
95034b2a88fSAl Viro 			(void *)(unsigned long)dvnode->status.data_version;
951bec5eb61Swanglei 	}
95234b2a88fSAl Viro 	d = d_splice_alias(inode, dentry);
95380548b03SDavid Howells 	if (!IS_ERR_OR_NULL(d)) {
95434b2a88fSAl Viro 		d->d_fsdata = dentry->d_fsdata;
95580548b03SDavid Howells 		trace_afs_lookup(dvnode, &d->d_name,
95680548b03SDavid Howells 				 inode ? AFS_FS_I(inode) : NULL);
95780548b03SDavid Howells 	} else {
95880548b03SDavid Howells 		trace_afs_lookup(dvnode, &dentry->d_name,
95980548b03SDavid Howells 				 inode ? AFS_FS_I(inode) : NULL);
96080548b03SDavid Howells 	}
96134b2a88fSAl Viro 	return d;
962ec26815aSDavid Howells }
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds /*
9651da177e4SLinus Torvalds  * check that a dentry lookup hit has found a valid entry
9661da177e4SLinus Torvalds  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
9671da177e4SLinus Torvalds  *   inode
9681da177e4SLinus Torvalds  */
9690b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
9701da177e4SLinus Torvalds {
971260a9803SDavid Howells 	struct afs_vnode *vnode, *dir;
972dd0d9a46SArtem Bityutskiy 	struct afs_fid uninitialized_var(fid);
9731da177e4SLinus Torvalds 	struct dentry *parent;
974c435ee34SDavid Howells 	struct inode *inode;
97500d3b7a4SDavid Howells 	struct key *key;
976a4ff7401SDavid Howells 	long dir_version, de_version;
9771da177e4SLinus Torvalds 	int ret;
9781da177e4SLinus Torvalds 
9790b728e19SAl Viro 	if (flags & LOOKUP_RCU)
98034286d66SNick Piggin 		return -ECHILD;
98134286d66SNick Piggin 
982c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
9832b0143b5SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
9843b6492dfSDavid Howells 		_enter("{v={%llx:%llu} n=%pd fl=%lx},",
985a455589fSAl Viro 		       vnode->fid.vid, vnode->fid.vnode, dentry,
986260a9803SDavid Howells 		       vnode->flags);
987c435ee34SDavid Howells 	} else {
988a455589fSAl Viro 		_enter("{neg n=%pd}", dentry);
989c435ee34SDavid Howells 	}
9901da177e4SLinus Torvalds 
991260a9803SDavid Howells 	key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
99200d3b7a4SDavid Howells 	if (IS_ERR(key))
99300d3b7a4SDavid Howells 		key = NULL;
99400d3b7a4SDavid Howells 
995c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
996c435ee34SDavid Howells 		inode = d_inode(dentry);
997c435ee34SDavid Howells 		if (inode) {
998c435ee34SDavid Howells 			vnode = AFS_FS_I(inode);
999c435ee34SDavid Howells 			afs_validate(vnode, key);
1000c435ee34SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1001c435ee34SDavid Howells 				goto out_bad;
1002c435ee34SDavid Howells 		}
1003c435ee34SDavid Howells 	}
1004c435ee34SDavid Howells 
10051da177e4SLinus Torvalds 	/* lock down the parent dentry so we can peer at it */
100608e0e7c8SDavid Howells 	parent = dget_parent(dentry);
10072b0143b5SDavid Howells 	dir = AFS_FS_I(d_inode(parent));
10081da177e4SLinus Torvalds 
1009260a9803SDavid Howells 	/* validate the parent directory */
1010260a9803SDavid Howells 	afs_validate(dir, key);
1011260a9803SDavid Howells 
1012260a9803SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1013a455589fSAl Viro 		_debug("%pd: parent dir deleted", dentry);
1014c435ee34SDavid Howells 		goto out_bad_parent;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 
1017a4ff7401SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
1018a4ff7401SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
1019a4ff7401SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
1020a4ff7401SDavid Howells 	 * version.
1021a4ff7401SDavid Howells 	 */
1022a4ff7401SDavid Howells 	dir_version = (long)dir->status.data_version;
1023a4ff7401SDavid Howells 	de_version = (long)dentry->d_fsdata;
1024a4ff7401SDavid Howells 	if (de_version == dir_version)
1025a4ff7401SDavid Howells 		goto out_valid;
1026a4ff7401SDavid Howells 
1027a4ff7401SDavid Howells 	dir_version = (long)dir->invalid_before;
1028a4ff7401SDavid Howells 	if (de_version - dir_version >= 0)
1029a4ff7401SDavid Howells 		goto out_valid;
1030260a9803SDavid Howells 
103108e0e7c8SDavid Howells 	_debug("dir modified");
1032d55b4da4SDavid Howells 	afs_stat_v(dir, n_reval);
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	/* search the directory for this vnode */
10355cf9dd55SDavid Howells 	ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
1036260a9803SDavid Howells 	switch (ret) {
1037260a9803SDavid Howells 	case 0:
1038260a9803SDavid Howells 		/* the filename maps to something */
10392b0143b5SDavid Howells 		if (d_really_is_negative(dentry))
1040c435ee34SDavid Howells 			goto out_bad_parent;
1041c435ee34SDavid Howells 		inode = d_inode(dentry);
1042c435ee34SDavid Howells 		if (is_bad_inode(inode)) {
1043a455589fSAl Viro 			printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1044a455589fSAl Viro 			       dentry);
1045c435ee34SDavid Howells 			goto out_bad_parent;
10461da177e4SLinus Torvalds 		}
10471da177e4SLinus Torvalds 
1048c435ee34SDavid Howells 		vnode = AFS_FS_I(inode);
1049c435ee34SDavid Howells 
10501da177e4SLinus Torvalds 		/* if the vnode ID has changed, then the dirent points to a
10511da177e4SLinus Torvalds 		 * different file */
105208e0e7c8SDavid Howells 		if (fid.vnode != vnode->fid.vnode) {
10533b6492dfSDavid Howells 			_debug("%pd: dirent changed [%llu != %llu]",
1054a455589fSAl Viro 			       dentry, fid.vnode,
105508e0e7c8SDavid Howells 			       vnode->fid.vnode);
10561da177e4SLinus Torvalds 			goto not_found;
10571da177e4SLinus Torvalds 		}
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 		/* if the vnode ID uniqifier has changed, then the file has
1060260a9803SDavid Howells 		 * been deleted and replaced, and the original vnode ID has
1061260a9803SDavid Howells 		 * been reused */
106208e0e7c8SDavid Howells 		if (fid.unique != vnode->fid.unique) {
1063a455589fSAl Viro 			_debug("%pd: file deleted (uq %u -> %u I:%u)",
1064a455589fSAl Viro 			       dentry, fid.unique,
10657a224228SJean Noel Cordenner 			       vnode->fid.unique,
1066c435ee34SDavid Howells 			       vnode->vfs_inode.i_generation);
1067c435ee34SDavid Howells 			write_seqlock(&vnode->cb_lock);
106808e0e7c8SDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
1069c435ee34SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1070260a9803SDavid Howells 			goto not_found;
1071260a9803SDavid Howells 		}
1072260a9803SDavid Howells 		goto out_valid;
1073260a9803SDavid Howells 
1074260a9803SDavid Howells 	case -ENOENT:
1075260a9803SDavid Howells 		/* the filename is unknown */
1076a455589fSAl Viro 		_debug("%pd: dirent not found", dentry);
10772b0143b5SDavid Howells 		if (d_really_is_positive(dentry))
1078260a9803SDavid Howells 			goto not_found;
1079260a9803SDavid Howells 		goto out_valid;
1080260a9803SDavid Howells 
1081260a9803SDavid Howells 	default:
1082a455589fSAl Viro 		_debug("failed to iterate dir %pd: %d",
1083a455589fSAl Viro 		       parent, ret);
1084c435ee34SDavid Howells 		goto out_bad_parent;
10851da177e4SLinus Torvalds 	}
108608e0e7c8SDavid Howells 
10871da177e4SLinus Torvalds out_valid:
1088a4ff7401SDavid Howells 	dentry->d_fsdata = (void *)dir_version;
10891da177e4SLinus Torvalds 	dput(parent);
109000d3b7a4SDavid Howells 	key_put(key);
10911da177e4SLinus Torvalds 	_leave(" = 1 [valid]");
10921da177e4SLinus Torvalds 	return 1;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	/* the dirent, if it exists, now points to a different vnode */
10951da177e4SLinus Torvalds not_found:
10961da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
10971da177e4SLinus Torvalds 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
10981da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
10991da177e4SLinus Torvalds 
1100c435ee34SDavid Howells out_bad_parent:
1101a455589fSAl Viro 	_debug("dropping dentry %pd2", dentry);
11021da177e4SLinus Torvalds 	dput(parent);
1103c435ee34SDavid Howells out_bad:
110400d3b7a4SDavid Howells 	key_put(key);
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	_leave(" = 0 [bad]");
11071da177e4SLinus Torvalds 	return 0;
1108ec26815aSDavid Howells }
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds /*
11111da177e4SLinus Torvalds  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
11121da177e4SLinus Torvalds  * sleep)
11131da177e4SLinus Torvalds  * - called from dput() when d_count is going to 0.
11141da177e4SLinus Torvalds  * - return 1 to request dentry be unhashed, 0 otherwise
11151da177e4SLinus Torvalds  */
1116fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry)
11171da177e4SLinus Torvalds {
1118a455589fSAl Viro 	_enter("%pd", dentry);
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
11211da177e4SLinus Torvalds 		goto zap;
11221da177e4SLinus Torvalds 
11232b0143b5SDavid Howells 	if (d_really_is_positive(dentry) &&
11242b0143b5SDavid Howells 	    (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
11252b0143b5SDavid Howells 	     test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
11261da177e4SLinus Torvalds 		goto zap;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	_leave(" = 0 [keep]");
11291da177e4SLinus Torvalds 	return 0;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds zap:
11321da177e4SLinus Torvalds 	_leave(" = 1 [zap]");
11331da177e4SLinus Torvalds 	return 1;
1134ec26815aSDavid Howells }
1135260a9803SDavid Howells 
1136260a9803SDavid Howells /*
113779ddbfa5SDavid Howells  * Clean up sillyrename files on dentry removal.
113879ddbfa5SDavid Howells  */
113979ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode)
114079ddbfa5SDavid Howells {
114179ddbfa5SDavid Howells 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
114279ddbfa5SDavid Howells 		afs_silly_iput(dentry, inode);
114379ddbfa5SDavid Howells 	iput(inode);
114479ddbfa5SDavid Howells }
114579ddbfa5SDavid Howells 
114679ddbfa5SDavid Howells /*
1147260a9803SDavid Howells  * handle dentry release
1148260a9803SDavid Howells  */
114966c7e1d3SDavid Howells void afs_d_release(struct dentry *dentry)
1150260a9803SDavid Howells {
1151a455589fSAl Viro 	_enter("%pd", dentry);
1152260a9803SDavid Howells }
1153260a9803SDavid Howells 
1154260a9803SDavid Howells /*
1155d2ddc776SDavid Howells  * Create a new inode for create/mkdir/symlink
1156d2ddc776SDavid Howells  */
1157d2ddc776SDavid Howells static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1158d2ddc776SDavid Howells 				struct dentry *new_dentry,
1159b8359153SDavid Howells 				struct afs_iget_data *new_data,
1160a58823acSDavid Howells 				struct afs_status_cb *new_scb)
1161d2ddc776SDavid Howells {
11625a813276SDavid Howells 	struct afs_vnode *vnode;
1163d2ddc776SDavid Howells 	struct inode *inode;
1164d2ddc776SDavid Howells 
1165d2ddc776SDavid Howells 	if (fc->ac.error < 0)
1166d2ddc776SDavid Howells 		return;
1167d2ddc776SDavid Howells 
1168d2ddc776SDavid Howells 	inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1169b8359153SDavid Howells 			 new_data, new_scb, fc->cbi, fc->vnode);
1170d2ddc776SDavid Howells 	if (IS_ERR(inode)) {
1171d2ddc776SDavid Howells 		/* ENOMEM or EINTR at a really inconvenient time - just abandon
1172d2ddc776SDavid Howells 		 * the new directory on the server.
1173d2ddc776SDavid Howells 		 */
1174d2ddc776SDavid Howells 		fc->ac.error = PTR_ERR(inode);
1175d2ddc776SDavid Howells 		return;
1176d2ddc776SDavid Howells 	}
1177d2ddc776SDavid Howells 
11785a813276SDavid Howells 	vnode = AFS_FS_I(inode);
11795a813276SDavid Howells 	set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1180a58823acSDavid Howells 	if (fc->ac.error == 0)
1181a58823acSDavid Howells 		afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb);
118273116df7SDavid Howells 	d_instantiate(new_dentry, inode);
1183d2ddc776SDavid Howells }
1184d2ddc776SDavid Howells 
1185b8359153SDavid Howells static void afs_prep_for_new_inode(struct afs_fs_cursor *fc,
1186b8359153SDavid Howells 				   struct afs_iget_data *iget_data)
1187b8359153SDavid Howells {
1188b8359153SDavid Howells 	iget_data->volume = fc->vnode->volume;
1189b8359153SDavid Howells 	iget_data->cb_v_break = fc->vnode->volume->cb_v_break;
1190b8359153SDavid Howells 	iget_data->cb_s_break = fc->cbi->server->cb_s_break;
1191b8359153SDavid Howells }
1192b8359153SDavid Howells 
1193d2ddc776SDavid Howells /*
1194260a9803SDavid Howells  * create a directory on an AFS filesystem
1195260a9803SDavid Howells  */
119618bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1197260a9803SDavid Howells {
1198b8359153SDavid Howells 	struct afs_iget_data iget_data;
1199a58823acSDavid Howells 	struct afs_status_cb *scb;
1200d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1201d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1202260a9803SDavid Howells 	struct key *key;
1203260a9803SDavid Howells 	int ret;
1204260a9803SDavid Howells 
1205d2ddc776SDavid Howells 	mode |= S_IFDIR;
1206260a9803SDavid Howells 
12073b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho",
1208a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1209260a9803SDavid Howells 
1210a58823acSDavid Howells 	ret = -ENOMEM;
1211a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1212a58823acSDavid Howells 	if (!scb)
1213a58823acSDavid Howells 		goto error;
1214a58823acSDavid Howells 
1215260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1216260a9803SDavid Howells 	if (IS_ERR(key)) {
1217260a9803SDavid Howells 		ret = PTR_ERR(key);
1218a58823acSDavid Howells 		goto error_scb;
1219260a9803SDavid Howells 	}
1220260a9803SDavid Howells 
1221d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
122220b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1223a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1224a58823acSDavid Howells 
1225d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
122668251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1227b8359153SDavid Howells 			afs_prep_for_new_inode(&fc, &iget_data);
1228a58823acSDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1229b8359153SDavid Howells 				      &scb[0], &iget_data.fid, &scb[1]);
1230d2ddc776SDavid Howells 		}
1231d2ddc776SDavid Howells 
1232a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1233a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1234a58823acSDavid Howells 					&data_version, &scb[0]);
1235b8359153SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1236d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1237260a9803SDavid Howells 		if (ret < 0)
1238d2ddc776SDavid Howells 			goto error_key;
12394433b691SDavid Howells 	} else {
12404433b691SDavid Howells 		goto error_key;
1241260a9803SDavid Howells 	}
1242260a9803SDavid Howells 
124363a4681fSDavid Howells 	if (ret == 0 &&
124463a4681fSDavid Howells 	    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1245b8359153SDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
124663a4681fSDavid Howells 				 afs_edit_dir_for_create);
124763a4681fSDavid Howells 
1248260a9803SDavid Howells 	key_put(key);
1249a58823acSDavid Howells 	kfree(scb);
1250260a9803SDavid Howells 	_leave(" = 0");
1251260a9803SDavid Howells 	return 0;
1252260a9803SDavid Howells 
1253d2ddc776SDavid Howells error_key:
1254260a9803SDavid Howells 	key_put(key);
1255a58823acSDavid Howells error_scb:
1256a58823acSDavid Howells 	kfree(scb);
1257260a9803SDavid Howells error:
1258260a9803SDavid Howells 	d_drop(dentry);
1259260a9803SDavid Howells 	_leave(" = %d", ret);
1260260a9803SDavid Howells 	return ret;
1261260a9803SDavid Howells }
1262260a9803SDavid Howells 
1263260a9803SDavid Howells /*
1264d2ddc776SDavid Howells  * Remove a subdir from a directory.
1265260a9803SDavid Howells  */
1266d2ddc776SDavid Howells static void afs_dir_remove_subdir(struct dentry *dentry)
1267260a9803SDavid Howells {
12682b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1269d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1270d2ddc776SDavid Howells 
1271260a9803SDavid Howells 		clear_nlink(&vnode->vfs_inode);
1272260a9803SDavid Howells 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1273c435ee34SDavid Howells 		clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
127463a4681fSDavid Howells 		clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1275260a9803SDavid Howells 	}
1276260a9803SDavid Howells }
1277260a9803SDavid Howells 
1278260a9803SDavid Howells /*
1279d2ddc776SDavid Howells  * remove a directory from an AFS filesystem
1280260a9803SDavid Howells  */
1281d2ddc776SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1282260a9803SDavid Howells {
1283a58823acSDavid Howells 	struct afs_status_cb *scb;
1284d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1285f58db83fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1286260a9803SDavid Howells 	struct key *key;
1287260a9803SDavid Howells 	int ret;
1288260a9803SDavid Howells 
12893b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1290a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1291260a9803SDavid Howells 
1292a58823acSDavid Howells 	scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
1293a58823acSDavid Howells 	if (!scb)
1294a58823acSDavid Howells 		return -ENOMEM;
1295a58823acSDavid Howells 
1296260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1297260a9803SDavid Howells 	if (IS_ERR(key)) {
1298260a9803SDavid Howells 		ret = PTR_ERR(key);
1299260a9803SDavid Howells 		goto error;
1300260a9803SDavid Howells 	}
1301260a9803SDavid Howells 
1302f58db83fSDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1303f58db83fSDavid Howells 	if (d_really_is_positive(dentry)) {
1304f58db83fSDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1305f58db83fSDavid Howells 		ret = afs_validate(vnode, key);
1306f58db83fSDavid Howells 		if (ret < 0)
1307f58db83fSDavid Howells 			goto error_key;
1308f58db83fSDavid Howells 	}
1309f58db83fSDavid Howells 
131079ddbfa5SDavid Howells 	if (vnode) {
131179ddbfa5SDavid Howells 		ret = down_write_killable(&vnode->rmdir_lock);
131279ddbfa5SDavid Howells 		if (ret < 0)
131379ddbfa5SDavid Howells 			goto error_key;
131479ddbfa5SDavid Howells 	}
131579ddbfa5SDavid Howells 
1316d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
131720b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1318a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1319a58823acSDavid Howells 
1320d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
132168251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1322a58823acSDavid Howells 			afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb);
1323260a9803SDavid Howells 		}
1324260a9803SDavid Howells 
1325a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1326a58823acSDavid Howells 					&data_version, scb);
1327d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
132863a4681fSDavid Howells 		if (ret == 0) {
1329d2ddc776SDavid Howells 			afs_dir_remove_subdir(dentry);
133063a4681fSDavid Howells 			if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
133163a4681fSDavid Howells 				afs_edit_dir_remove(dvnode, &dentry->d_name,
133263a4681fSDavid Howells 						    afs_edit_dir_for_rmdir);
133363a4681fSDavid Howells 		}
1334260a9803SDavid Howells 	}
1335260a9803SDavid Howells 
133679ddbfa5SDavid Howells 	if (vnode)
133779ddbfa5SDavid Howells 		up_write(&vnode->rmdir_lock);
1338f58db83fSDavid Howells error_key:
1339260a9803SDavid Howells 	key_put(key);
1340d2ddc776SDavid Howells error:
1341a58823acSDavid Howells 	kfree(scb);
1342d2ddc776SDavid Howells 	return ret;
1343d2ddc776SDavid Howells }
1344260a9803SDavid Howells 
1345d2ddc776SDavid Howells /*
1346d2ddc776SDavid Howells  * Remove a link to a file or symlink from a directory.
1347d2ddc776SDavid Howells  *
1348d2ddc776SDavid Howells  * If the file was not deleted due to excess hard links, the fileserver will
1349d2ddc776SDavid Howells  * break the callback promise on the file - if it had one - before it returns
1350d2ddc776SDavid Howells  * to us, and if it was deleted, it won't
1351d2ddc776SDavid Howells  *
1352d2ddc776SDavid Howells  * However, if we didn't have a callback promise outstanding, or it was
1353d2ddc776SDavid Howells  * outstanding on a different server, then it won't break it either...
1354d2ddc776SDavid Howells  */
1355a38a7558SDavid Howells static int afs_dir_remove_link(struct afs_vnode *dvnode, struct dentry *dentry,
1356a38a7558SDavid Howells 			       struct key *key)
1357d2ddc776SDavid Howells {
1358d2ddc776SDavid Howells 	int ret = 0;
1359d2ddc776SDavid Howells 
1360d2ddc776SDavid Howells 	if (d_really_is_positive(dentry)) {
1361d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1362d2ddc776SDavid Howells 
136330062bd1SDavid Howells 		if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
136430062bd1SDavid Howells 			/* Already done */
1365a38a7558SDavid Howells 		} else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1366a38a7558SDavid Howells 			write_seqlock(&vnode->cb_lock);
1367440fbc3aSDavid Howells 			drop_nlink(&vnode->vfs_inode);
1368440fbc3aSDavid Howells 			if (vnode->vfs_inode.i_nlink == 0) {
1369440fbc3aSDavid Howells 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
1370a38a7558SDavid Howells 				__afs_break_callback(vnode);
1371440fbc3aSDavid Howells 			}
1372a38a7558SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1373440fbc3aSDavid Howells 			ret = 0;
1374440fbc3aSDavid Howells 		} else {
1375a38a7558SDavid Howells 			afs_break_callback(vnode);
1376440fbc3aSDavid Howells 
1377d2ddc776SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1378d2ddc776SDavid Howells 				kdebug("AFS_VNODE_DELETED");
1379d2ddc776SDavid Howells 
1380d2ddc776SDavid Howells 			ret = afs_validate(vnode, key);
1381d2ddc776SDavid Howells 			if (ret == -ESTALE)
1382d2ddc776SDavid Howells 				ret = 0;
1383440fbc3aSDavid Howells 		}
1384d2ddc776SDavid Howells 		_debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1385d2ddc776SDavid Howells 	}
1386d2ddc776SDavid Howells 
1387d2ddc776SDavid Howells 	return ret;
1388d2ddc776SDavid Howells }
1389d2ddc776SDavid Howells 
1390d2ddc776SDavid Howells /*
1391d2ddc776SDavid Howells  * Remove a file or symlink from an AFS filesystem.
1392d2ddc776SDavid Howells  */
1393d2ddc776SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry)
1394d2ddc776SDavid Howells {
1395d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1396a58823acSDavid Howells 	struct afs_status_cb *scb;
1397fa59f52fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1398fa59f52fSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1399d2ddc776SDavid Howells 	struct key *key;
140079ddbfa5SDavid Howells 	bool need_rehash = false;
1401d2ddc776SDavid Howells 	int ret;
1402d2ddc776SDavid Howells 
14033b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1404d2ddc776SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1405d2ddc776SDavid Howells 
1406d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1407d2ddc776SDavid Howells 		return -ENAMETOOLONG;
1408d2ddc776SDavid Howells 
1409a58823acSDavid Howells 	ret = -ENOMEM;
1410a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1411a58823acSDavid Howells 	if (!scb)
1412a58823acSDavid Howells 		goto error;
1413a58823acSDavid Howells 
1414d2ddc776SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1415d2ddc776SDavid Howells 	if (IS_ERR(key)) {
1416d2ddc776SDavid Howells 		ret = PTR_ERR(key);
1417a58823acSDavid Howells 		goto error_scb;
1418d2ddc776SDavid Howells 	}
1419d2ddc776SDavid Howells 
1420d2ddc776SDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1421d2ddc776SDavid Howells 	ret = afs_validate(vnode, key);
1422d2ddc776SDavid Howells 	if (ret < 0)
1423d2ddc776SDavid Howells 		goto error_key;
1424d2ddc776SDavid Howells 
142579ddbfa5SDavid Howells 	spin_lock(&dentry->d_lock);
1426fa59f52fSDavid Howells 	if (d_count(dentry) > 1) {
142779ddbfa5SDavid Howells 		spin_unlock(&dentry->d_lock);
142879ddbfa5SDavid Howells 		/* Start asynchronous writeout of the inode */
142979ddbfa5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
143079ddbfa5SDavid Howells 		ret = afs_sillyrename(dvnode, vnode, dentry, key);
143179ddbfa5SDavid Howells 		goto error_key;
143279ddbfa5SDavid Howells 	}
143379ddbfa5SDavid Howells 	if (!d_unhashed(dentry)) {
143479ddbfa5SDavid Howells 		/* Prevent a race with RCU lookup. */
143579ddbfa5SDavid Howells 		__d_drop(dentry);
143679ddbfa5SDavid Howells 		need_rehash = true;
143779ddbfa5SDavid Howells 	}
143879ddbfa5SDavid Howells 	spin_unlock(&dentry->d_lock);
143979ddbfa5SDavid Howells 
1440d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
144120b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1442a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1443a38a7558SDavid Howells 		afs_dataversion_t data_version_2 = vnode->status.data_version;
1444a58823acSDavid Howells 
1445d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
144668251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1447a38a7558SDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
144830062bd1SDavid Howells 
144930062bd1SDavid Howells 			if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
145030062bd1SDavid Howells 			    !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
145130062bd1SDavid Howells 				yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1452a58823acSDavid Howells 						    &scb[0], &scb[1]);
145330062bd1SDavid Howells 				if (fc.ac.error != -ECONNABORTED ||
145430062bd1SDavid Howells 				    fc.ac.abort_code != RXGEN_OPCODE)
145530062bd1SDavid Howells 					continue;
145630062bd1SDavid Howells 				set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
145730062bd1SDavid Howells 			}
145830062bd1SDavid Howells 
1459a58823acSDavid Howells 			afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
1460d2ddc776SDavid Howells 		}
1461d2ddc776SDavid Howells 
1462a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1463a58823acSDavid Howells 					&data_version, &scb[0]);
1464a38a7558SDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1465a38a7558SDavid Howells 					&data_version_2, &scb[1]);
1466d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1467a38a7558SDavid Howells 		if (ret == 0 && !(scb[1].have_status || scb[1].have_error))
1468a38a7558SDavid Howells 			ret = afs_dir_remove_link(dvnode, dentry, key);
146963a4681fSDavid Howells 		if (ret == 0 &&
147063a4681fSDavid Howells 		    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
147163a4681fSDavid Howells 			afs_edit_dir_remove(dvnode, &dentry->d_name,
147263a4681fSDavid Howells 					    afs_edit_dir_for_unlink);
1473d2ddc776SDavid Howells 	}
1474d2ddc776SDavid Howells 
147579ddbfa5SDavid Howells 	if (need_rehash && ret < 0 && ret != -ENOENT)
147679ddbfa5SDavid Howells 		d_rehash(dentry);
147779ddbfa5SDavid Howells 
1478d2ddc776SDavid Howells error_key:
1479260a9803SDavid Howells 	key_put(key);
1480a58823acSDavid Howells error_scb:
1481a58823acSDavid Howells 	kfree(scb);
1482260a9803SDavid Howells error:
1483260a9803SDavid Howells 	_leave(" = %d", ret);
1484260a9803SDavid Howells 	return ret;
1485260a9803SDavid Howells }
1486260a9803SDavid Howells 
1487260a9803SDavid Howells /*
1488260a9803SDavid Howells  * create a regular file on an AFS filesystem
1489260a9803SDavid Howells  */
14904acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1491ebfc3b49SAl Viro 		      bool excl)
1492260a9803SDavid Howells {
1493b8359153SDavid Howells 	struct afs_iget_data iget_data;
1494d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1495a58823acSDavid Howells 	struct afs_status_cb *scb;
149643dd388bSColin Ian King 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1497260a9803SDavid Howells 	struct key *key;
1498260a9803SDavid Howells 	int ret;
1499260a9803SDavid Howells 
1500d2ddc776SDavid Howells 	mode |= S_IFREG;
1501260a9803SDavid Howells 
15023b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho,",
1503a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1504260a9803SDavid Howells 
1505d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1506d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1507d2ddc776SDavid Howells 		goto error;
1508d2ddc776SDavid Howells 
1509260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1510260a9803SDavid Howells 	if (IS_ERR(key)) {
1511260a9803SDavid Howells 		ret = PTR_ERR(key);
1512260a9803SDavid Howells 		goto error;
1513260a9803SDavid Howells 	}
1514260a9803SDavid Howells 
1515a58823acSDavid Howells 	ret = -ENOMEM;
1516a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1517a58823acSDavid Howells 	if (!scb)
1518a58823acSDavid Howells 		goto error_scb;
1519a58823acSDavid Howells 
1520d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
152120b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1522a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1523a58823acSDavid Howells 
1524d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
152568251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1526b8359153SDavid Howells 			afs_prep_for_new_inode(&fc, &iget_data);
1527a58823acSDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1528b8359153SDavid Howells 				      &scb[0], &iget_data.fid, &scb[1]);
1529d2ddc776SDavid Howells 		}
1530d2ddc776SDavid Howells 
1531a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1532a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1533a58823acSDavid Howells 					&data_version, &scb[0]);
1534b8359153SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1535d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1536260a9803SDavid Howells 		if (ret < 0)
1537d2ddc776SDavid Howells 			goto error_key;
15384433b691SDavid Howells 	} else {
15394433b691SDavid Howells 		goto error_key;
1540260a9803SDavid Howells 	}
1541260a9803SDavid Howells 
154263a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1543b8359153SDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
154463a4681fSDavid Howells 				 afs_edit_dir_for_create);
154563a4681fSDavid Howells 
1546a58823acSDavid Howells 	kfree(scb);
1547260a9803SDavid Howells 	key_put(key);
1548260a9803SDavid Howells 	_leave(" = 0");
1549260a9803SDavid Howells 	return 0;
1550260a9803SDavid Howells 
1551a58823acSDavid Howells error_scb:
1552a58823acSDavid Howells 	kfree(scb);
1553d2ddc776SDavid Howells error_key:
1554260a9803SDavid Howells 	key_put(key);
1555260a9803SDavid Howells error:
1556260a9803SDavid Howells 	d_drop(dentry);
1557260a9803SDavid Howells 	_leave(" = %d", ret);
1558260a9803SDavid Howells 	return ret;
1559260a9803SDavid Howells }
1560260a9803SDavid Howells 
1561260a9803SDavid Howells /*
1562260a9803SDavid Howells  * create a hard link between files in an AFS filesystem
1563260a9803SDavid Howells  */
1564260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
1565260a9803SDavid Howells 		    struct dentry *dentry)
1566260a9803SDavid Howells {
1567d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1568a58823acSDavid Howells 	struct afs_status_cb *scb;
1569a58823acSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1570a58823acSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1571260a9803SDavid Howells 	struct key *key;
1572260a9803SDavid Howells 	int ret;
1573260a9803SDavid Howells 
15743b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%pd}",
1575260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1576260a9803SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode,
1577a455589fSAl Viro 	       dentry);
1578260a9803SDavid Howells 
1579d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1580d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1581d2ddc776SDavid Howells 		goto error;
1582d2ddc776SDavid Howells 
1583a58823acSDavid Howells 	ret = -ENOMEM;
1584a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1585a58823acSDavid Howells 	if (!scb)
1586a58823acSDavid Howells 		goto error;
1587a58823acSDavid Howells 
1588260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1589260a9803SDavid Howells 	if (IS_ERR(key)) {
1590260a9803SDavid Howells 		ret = PTR_ERR(key);
1591a58823acSDavid Howells 		goto error_scb;
1592260a9803SDavid Howells 	}
1593260a9803SDavid Howells 
1594d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
159520b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1596a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1597a58823acSDavid Howells 
1598d2ddc776SDavid Howells 		if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1599d2ddc776SDavid Howells 			afs_end_vnode_operation(&fc);
1600bc1527dcSDavid Howells 			goto error_key;
1601d2ddc776SDavid Howells 		}
1602260a9803SDavid Howells 
1603d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
160468251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
160568251f0aSDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1606a58823acSDavid Howells 			afs_fs_link(&fc, vnode, dentry->d_name.name,
1607a58823acSDavid Howells 				    &scb[0], &scb[1]);
1608d2ddc776SDavid Howells 		}
1609d2ddc776SDavid Howells 
1610a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1611a58823acSDavid Howells 					&data_version, &scb[0]);
1612a58823acSDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1613a58823acSDavid Howells 					NULL, &scb[1]);
16147de9c6eeSAl Viro 		ihold(&vnode->vfs_inode);
1615260a9803SDavid Howells 		d_instantiate(dentry, &vnode->vfs_inode);
1616d2ddc776SDavid Howells 
1617d2ddc776SDavid Howells 		mutex_unlock(&vnode->io_lock);
1618d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1619d2ddc776SDavid Howells 		if (ret < 0)
1620d2ddc776SDavid Howells 			goto error_key;
16214433b691SDavid Howells 	} else {
16224433b691SDavid Howells 		goto error_key;
1623d2ddc776SDavid Howells 	}
1624d2ddc776SDavid Howells 
162563a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
162663a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
162763a4681fSDavid Howells 				 afs_edit_dir_for_link);
162863a4681fSDavid Howells 
1629260a9803SDavid Howells 	key_put(key);
1630a58823acSDavid Howells 	kfree(scb);
1631260a9803SDavid Howells 	_leave(" = 0");
1632260a9803SDavid Howells 	return 0;
1633260a9803SDavid Howells 
1634d2ddc776SDavid Howells error_key:
1635260a9803SDavid Howells 	key_put(key);
1636a58823acSDavid Howells error_scb:
1637a58823acSDavid Howells 	kfree(scb);
1638260a9803SDavid Howells error:
1639260a9803SDavid Howells 	d_drop(dentry);
1640260a9803SDavid Howells 	_leave(" = %d", ret);
1641260a9803SDavid Howells 	return ret;
1642260a9803SDavid Howells }
1643260a9803SDavid Howells 
1644260a9803SDavid Howells /*
1645260a9803SDavid Howells  * create a symlink in an AFS filesystem
1646260a9803SDavid Howells  */
1647260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
1648260a9803SDavid Howells 		       const char *content)
1649260a9803SDavid Howells {
1650b8359153SDavid Howells 	struct afs_iget_data iget_data;
1651d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1652a58823acSDavid Howells 	struct afs_status_cb *scb;
1653d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1654260a9803SDavid Howells 	struct key *key;
1655260a9803SDavid Howells 	int ret;
1656260a9803SDavid Howells 
16573b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%s",
1658a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry,
1659260a9803SDavid Howells 	       content);
1660260a9803SDavid Howells 
1661d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1662d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1663d2ddc776SDavid Howells 		goto error;
1664d2ddc776SDavid Howells 
1665260a9803SDavid Howells 	ret = -EINVAL;
166645222b9eSDavid Howells 	if (strlen(content) >= AFSPATHMAX)
1667260a9803SDavid Howells 		goto error;
1668260a9803SDavid Howells 
1669a58823acSDavid Howells 	ret = -ENOMEM;
1670a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1671a58823acSDavid Howells 	if (!scb)
1672a58823acSDavid Howells 		goto error;
1673a58823acSDavid Howells 
1674260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1675260a9803SDavid Howells 	if (IS_ERR(key)) {
1676260a9803SDavid Howells 		ret = PTR_ERR(key);
1677a58823acSDavid Howells 		goto error_scb;
1678260a9803SDavid Howells 	}
1679260a9803SDavid Howells 
1680d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
168120b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1682a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1683a58823acSDavid Howells 
1684d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
168568251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1686b8359153SDavid Howells 			afs_prep_for_new_inode(&fc, &iget_data);
1687a58823acSDavid Howells 			afs_fs_symlink(&fc, dentry->d_name.name, content,
1688b8359153SDavid Howells 				       &scb[0], &iget_data.fid, &scb[1]);
1689d2ddc776SDavid Howells 		}
1690d2ddc776SDavid Howells 
1691a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1692a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1693a58823acSDavid Howells 					&data_version, &scb[0]);
1694b8359153SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1695d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1696260a9803SDavid Howells 		if (ret < 0)
1697d2ddc776SDavid Howells 			goto error_key;
16984433b691SDavid Howells 	} else {
16994433b691SDavid Howells 		goto error_key;
1700260a9803SDavid Howells 	}
1701260a9803SDavid Howells 
170263a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1703b8359153SDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
170463a4681fSDavid Howells 				 afs_edit_dir_for_symlink);
170563a4681fSDavid Howells 
1706260a9803SDavid Howells 	key_put(key);
1707a58823acSDavid Howells 	kfree(scb);
1708260a9803SDavid Howells 	_leave(" = 0");
1709260a9803SDavid Howells 	return 0;
1710260a9803SDavid Howells 
1711d2ddc776SDavid Howells error_key:
1712260a9803SDavid Howells 	key_put(key);
1713a58823acSDavid Howells error_scb:
1714a58823acSDavid Howells 	kfree(scb);
1715260a9803SDavid Howells error:
1716260a9803SDavid Howells 	d_drop(dentry);
1717260a9803SDavid Howells 	_leave(" = %d", ret);
1718260a9803SDavid Howells 	return ret;
1719260a9803SDavid Howells }
1720260a9803SDavid Howells 
1721260a9803SDavid Howells /*
1722260a9803SDavid Howells  * rename a file in an AFS filesystem and/or move it between directories
1723260a9803SDavid Howells  */
1724260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
17251cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
17261cd66c93SMiklos Szeredi 		      unsigned int flags)
1727260a9803SDavid Howells {
1728d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1729a58823acSDavid Howells 	struct afs_status_cb *scb;
1730260a9803SDavid Howells 	struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
173179ddbfa5SDavid Howells 	struct dentry *tmp = NULL, *rehash = NULL;
173279ddbfa5SDavid Howells 	struct inode *new_inode;
1733260a9803SDavid Howells 	struct key *key;
173463a4681fSDavid Howells 	bool new_negative = d_is_negative(new_dentry);
1735260a9803SDavid Howells 	int ret;
1736260a9803SDavid Howells 
17371cd66c93SMiklos Szeredi 	if (flags)
17381cd66c93SMiklos Szeredi 		return -EINVAL;
17391cd66c93SMiklos Szeredi 
174079ddbfa5SDavid Howells 	/* Don't allow silly-rename files be moved around. */
174179ddbfa5SDavid Howells 	if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
174279ddbfa5SDavid Howells 		return -EINVAL;
174379ddbfa5SDavid Howells 
17442b0143b5SDavid Howells 	vnode = AFS_FS_I(d_inode(old_dentry));
1745260a9803SDavid Howells 	orig_dvnode = AFS_FS_I(old_dir);
1746260a9803SDavid Howells 	new_dvnode = AFS_FS_I(new_dir);
1747260a9803SDavid Howells 
17483b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1749260a9803SDavid Howells 	       orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1750260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1751260a9803SDavid Howells 	       new_dvnode->fid.vid, new_dvnode->fid.vnode,
1752a455589fSAl Viro 	       new_dentry);
1753260a9803SDavid Howells 
1754a58823acSDavid Howells 	ret = -ENOMEM;
1755a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1756a58823acSDavid Howells 	if (!scb)
1757a58823acSDavid Howells 		goto error;
1758a58823acSDavid Howells 
1759260a9803SDavid Howells 	key = afs_request_key(orig_dvnode->volume->cell);
1760260a9803SDavid Howells 	if (IS_ERR(key)) {
1761260a9803SDavid Howells 		ret = PTR_ERR(key);
1762a58823acSDavid Howells 		goto error_scb;
1763260a9803SDavid Howells 	}
1764260a9803SDavid Howells 
176579ddbfa5SDavid Howells 	/* For non-directories, check whether the target is busy and if so,
176679ddbfa5SDavid Howells 	 * make a copy of the dentry and then do a silly-rename.  If the
176779ddbfa5SDavid Howells 	 * silly-rename succeeds, the copied dentry is hashed and becomes the
176879ddbfa5SDavid Howells 	 * new target.
176979ddbfa5SDavid Howells 	 */
177079ddbfa5SDavid Howells 	if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
177179ddbfa5SDavid Howells 		/* To prevent any new references to the target during the
177279ddbfa5SDavid Howells 		 * rename, we unhash the dentry in advance.
177379ddbfa5SDavid Howells 		 */
177479ddbfa5SDavid Howells 		if (!d_unhashed(new_dentry)) {
177579ddbfa5SDavid Howells 			d_drop(new_dentry);
177679ddbfa5SDavid Howells 			rehash = new_dentry;
177779ddbfa5SDavid Howells 		}
177879ddbfa5SDavid Howells 
177979ddbfa5SDavid Howells 		if (d_count(new_dentry) > 2) {
178079ddbfa5SDavid Howells 			/* copy the target dentry's name */
178179ddbfa5SDavid Howells 			ret = -ENOMEM;
178279ddbfa5SDavid Howells 			tmp = d_alloc(new_dentry->d_parent,
178379ddbfa5SDavid Howells 				      &new_dentry->d_name);
178479ddbfa5SDavid Howells 			if (!tmp)
178579ddbfa5SDavid Howells 				goto error_rehash;
178679ddbfa5SDavid Howells 
178779ddbfa5SDavid Howells 			ret = afs_sillyrename(new_dvnode,
178879ddbfa5SDavid Howells 					      AFS_FS_I(d_inode(new_dentry)),
178979ddbfa5SDavid Howells 					      new_dentry, key);
179079ddbfa5SDavid Howells 			if (ret)
179179ddbfa5SDavid Howells 				goto error_rehash;
179279ddbfa5SDavid Howells 
179379ddbfa5SDavid Howells 			new_dentry = tmp;
179479ddbfa5SDavid Howells 			rehash = NULL;
179579ddbfa5SDavid Howells 			new_negative = true;
179679ddbfa5SDavid Howells 		}
179779ddbfa5SDavid Howells 	}
179879ddbfa5SDavid Howells 
1799d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
180020b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) {
1801a58823acSDavid Howells 		afs_dataversion_t orig_data_version;
1802a58823acSDavid Howells 		afs_dataversion_t new_data_version;
1803a58823acSDavid Howells 		struct afs_status_cb *new_scb = &scb[1];
1804a58823acSDavid Howells 
1805a58823acSDavid Howells 		orig_data_version = orig_dvnode->status.data_version + 1;
1806a58823acSDavid Howells 
1807d2ddc776SDavid Howells 		if (orig_dvnode != new_dvnode) {
1808d2ddc776SDavid Howells 			if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1809d2ddc776SDavid Howells 				afs_end_vnode_operation(&fc);
181079ddbfa5SDavid Howells 				goto error_rehash;
1811d2ddc776SDavid Howells 			}
1812a58823acSDavid Howells 			new_data_version = new_dvnode->status.data_version;
1813a58823acSDavid Howells 		} else {
1814a58823acSDavid Howells 			new_data_version = orig_data_version;
1815a58823acSDavid Howells 			new_scb = &scb[0];
1816d2ddc776SDavid Howells 		}
1817a58823acSDavid Howells 
1818d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
181968251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
182068251f0aSDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
1821d2ddc776SDavid Howells 			afs_fs_rename(&fc, old_dentry->d_name.name,
182263a4681fSDavid Howells 				      new_dvnode, new_dentry->d_name.name,
1823a58823acSDavid Howells 				      &scb[0], new_scb);
1824d2ddc776SDavid Howells 		}
1825d2ddc776SDavid Howells 
1826a58823acSDavid Howells 		afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break,
1827a58823acSDavid Howells 					&orig_data_version, &scb[0]);
1828a58823acSDavid Howells 		if (new_dvnode != orig_dvnode) {
1829a58823acSDavid Howells 			afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2,
1830a58823acSDavid Howells 						&new_data_version, &scb[1]);
1831d2ddc776SDavid Howells 			mutex_unlock(&new_dvnode->io_lock);
1832a58823acSDavid Howells 		}
1833d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1834260a9803SDavid Howells 		if (ret < 0)
183579ddbfa5SDavid Howells 			goto error_rehash;
1836d2ddc776SDavid Howells 	}
1837d2ddc776SDavid Howells 
183863a4681fSDavid Howells 	if (ret == 0) {
183979ddbfa5SDavid Howells 		if (rehash)
184079ddbfa5SDavid Howells 			d_rehash(rehash);
184163a4681fSDavid Howells 		if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
184263a4681fSDavid Howells 		    afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
184379ddbfa5SDavid Howells 					afs_edit_dir_for_rename_0);
184463a4681fSDavid Howells 
184563a4681fSDavid Howells 		if (!new_negative &&
184663a4681fSDavid Howells 		    test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
184763a4681fSDavid Howells 			afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
184879ddbfa5SDavid Howells 					    afs_edit_dir_for_rename_1);
184963a4681fSDavid Howells 
185063a4681fSDavid Howells 		if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
185163a4681fSDavid Howells 			afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
185279ddbfa5SDavid Howells 					 &vnode->fid, afs_edit_dir_for_rename_2);
185379ddbfa5SDavid Howells 
185479ddbfa5SDavid Howells 		new_inode = d_inode(new_dentry);
185579ddbfa5SDavid Howells 		if (new_inode) {
185679ddbfa5SDavid Howells 			spin_lock(&new_inode->i_lock);
185779ddbfa5SDavid Howells 			if (new_inode->i_nlink > 0)
185879ddbfa5SDavid Howells 				drop_nlink(new_inode);
185979ddbfa5SDavid Howells 			spin_unlock(&new_inode->i_lock);
186079ddbfa5SDavid Howells 		}
186179ddbfa5SDavid Howells 		d_move(old_dentry, new_dentry);
186279ddbfa5SDavid Howells 		goto error_tmp;
186363a4681fSDavid Howells 	}
186463a4681fSDavid Howells 
186579ddbfa5SDavid Howells error_rehash:
186679ddbfa5SDavid Howells 	if (rehash)
186779ddbfa5SDavid Howells 		d_rehash(rehash);
186879ddbfa5SDavid Howells error_tmp:
186979ddbfa5SDavid Howells 	if (tmp)
187079ddbfa5SDavid Howells 		dput(tmp);
1871260a9803SDavid Howells 	key_put(key);
1872a58823acSDavid Howells error_scb:
1873a58823acSDavid Howells 	kfree(scb);
1874260a9803SDavid Howells error:
1875260a9803SDavid Howells 	_leave(" = %d", ret);
1876260a9803SDavid Howells 	return ret;
1877260a9803SDavid Howells }
1878f3ddee8dSDavid Howells 
1879f3ddee8dSDavid Howells /*
1880f3ddee8dSDavid Howells  * Release a directory page and clean up its private state if it's not busy
1881f3ddee8dSDavid Howells  * - return true if the page can now be released, false if not
1882f3ddee8dSDavid Howells  */
1883f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1884f3ddee8dSDavid Howells {
1885f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1886f3ddee8dSDavid Howells 
18873b6492dfSDavid Howells 	_enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1888f3ddee8dSDavid Howells 
1889f3ddee8dSDavid Howells 	set_page_private(page, 0);
1890f3ddee8dSDavid Howells 	ClearPagePrivate(page);
1891f3ddee8dSDavid Howells 
1892f3ddee8dSDavid Howells 	/* The directory will need reloading. */
1893f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1894f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_relpg);
1895f3ddee8dSDavid Howells 	return 1;
1896f3ddee8dSDavid Howells }
1897f3ddee8dSDavid Howells 
1898f3ddee8dSDavid Howells /*
1899f3ddee8dSDavid Howells  * invalidate part or all of a page
1900f3ddee8dSDavid Howells  * - release a page and clean up its private data if offset is 0 (indicating
1901f3ddee8dSDavid Howells  *   the entire page)
1902f3ddee8dSDavid Howells  */
1903f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1904f3ddee8dSDavid Howells 				   unsigned int length)
1905f3ddee8dSDavid Howells {
1906f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1907f3ddee8dSDavid Howells 
1908f3ddee8dSDavid Howells 	_enter("{%lu},%u,%u", page->index, offset, length);
1909f3ddee8dSDavid Howells 
1910f3ddee8dSDavid Howells 	BUG_ON(!PageLocked(page));
1911f3ddee8dSDavid Howells 
1912f3ddee8dSDavid Howells 	/* The directory will need reloading. */
1913f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1914f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_inval);
1915f3ddee8dSDavid Howells 
1916f3ddee8dSDavid Howells 	/* we clean up only if the entire page is being invalidated */
1917f3ddee8dSDavid Howells 	if (offset == 0 && length == PAGE_SIZE) {
1918f3ddee8dSDavid Howells 		set_page_private(page, 0);
1919f3ddee8dSDavid Howells 		ClearPagePrivate(page);
1920f3ddee8dSDavid Howells 	}
1921f3ddee8dSDavid Howells }
1922