xref: /openbmc/linux/fs/afs/dir.c (revision a58823ac)
11da177e4SLinus Torvalds /* dir.c: AFS filesystem directory handling
21da177e4SLinus Torvalds  *
3f3ddee8dSDavid Howells  * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
41da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
1434286d66SNick Piggin #include <linux/namei.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
16f3ddee8dSDavid Howells #include <linux/swap.h>
1700d3b7a4SDavid Howells #include <linux/ctype.h>
18e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
19f3ddee8dSDavid Howells #include <linux/task_io_accounting_ops.h>
201da177e4SLinus Torvalds #include "internal.h"
21a58823acSDavid Howells #include "afs_fs.h"
224ea219a8SDavid Howells #include "xdr_fs.h"
231da177e4SLinus Torvalds 
24260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
2500cd8dd3SAl Viro 				 unsigned int flags);
261da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file);
271bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx);
280b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
29fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry);
3079ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode);
315cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
325cf9dd55SDavid Howells 				  loff_t fpos, u64 ino, unsigned dtype);
33ac7576f4SMiklos Szeredi static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
34afefdbb2SDavid Howells 			      loff_t fpos, u64 ino, unsigned dtype);
354acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
36ebfc3b49SAl Viro 		      bool excl);
3718bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
38260a9803SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry);
39260a9803SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry);
40260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
41260a9803SDavid Howells 		    struct dentry *dentry);
42260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
43260a9803SDavid Howells 		       const char *content);
44260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
451cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
461cd66c93SMiklos Szeredi 		      unsigned int flags);
47f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
48f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
49f3ddee8dSDavid Howells 				   unsigned int length);
50f3ddee8dSDavid Howells 
51f3ddee8dSDavid Howells static int afs_dir_set_page_dirty(struct page *page)
52f3ddee8dSDavid Howells {
53f3ddee8dSDavid Howells 	BUG(); /* This should never happen. */
54f3ddee8dSDavid Howells }
551da177e4SLinus Torvalds 
564b6f5d20SArjan van de Ven const struct file_operations afs_dir_file_operations = {
571da177e4SLinus Torvalds 	.open		= afs_dir_open,
5800d3b7a4SDavid Howells 	.release	= afs_release,
5929884effSAl Viro 	.iterate_shared	= afs_readdir,
60e8d6c554SDavid Howells 	.lock		= afs_lock,
613222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
64754661f1SArjan van de Ven const struct inode_operations afs_dir_inode_operations = {
65260a9803SDavid Howells 	.create		= afs_create,
66260a9803SDavid Howells 	.lookup		= afs_lookup,
67260a9803SDavid Howells 	.link		= afs_link,
68260a9803SDavid Howells 	.unlink		= afs_unlink,
69260a9803SDavid Howells 	.symlink	= afs_symlink,
70260a9803SDavid Howells 	.mkdir		= afs_mkdir,
71260a9803SDavid Howells 	.rmdir		= afs_rmdir,
722773bf00SMiklos Szeredi 	.rename		= afs_rename,
7300d3b7a4SDavid Howells 	.permission	= afs_permission,
74416351f2SDavid Howells 	.getattr	= afs_getattr,
7531143d5dSDavid Howells 	.setattr	= afs_setattr,
76d3e3b7eaSDavid Howells 	.listxattr	= afs_listxattr,
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
79f3ddee8dSDavid Howells const struct address_space_operations afs_dir_aops = {
80f3ddee8dSDavid Howells 	.set_page_dirty	= afs_dir_set_page_dirty,
81f3ddee8dSDavid Howells 	.releasepage	= afs_dir_releasepage,
82f3ddee8dSDavid Howells 	.invalidatepage	= afs_dir_invalidatepage,
83f3ddee8dSDavid Howells };
84f3ddee8dSDavid Howells 
85d61dcce2SAl Viro const struct dentry_operations afs_fs_dentry_operations = {
861da177e4SLinus Torvalds 	.d_revalidate	= afs_d_revalidate,
871da177e4SLinus Torvalds 	.d_delete	= afs_d_delete,
88260a9803SDavid Howells 	.d_release	= afs_d_release,
89d18610b0SDavid Howells 	.d_automount	= afs_d_automount,
9079ddbfa5SDavid Howells 	.d_iput		= afs_d_iput,
911da177e4SLinus Torvalds };
921da177e4SLinus Torvalds 
935cf9dd55SDavid Howells struct afs_lookup_one_cookie {
945cf9dd55SDavid Howells 	struct dir_context	ctx;
955cf9dd55SDavid Howells 	struct qstr		name;
965cf9dd55SDavid Howells 	bool			found;
975cf9dd55SDavid Howells 	struct afs_fid		fid;
985cf9dd55SDavid Howells };
995cf9dd55SDavid Howells 
100260a9803SDavid Howells struct afs_lookup_cookie {
1011bbae9f8SAl Viro 	struct dir_context	ctx;
1021bbae9f8SAl Viro 	struct qstr		name;
1035cf9dd55SDavid Howells 	bool			found;
1045cf9dd55SDavid Howells 	bool			one_only;
1055cf9dd55SDavid Howells 	unsigned short		nr_fids;
10687182759SDavid Howells 	struct afs_status_cb	*statuses;
1075cf9dd55SDavid Howells 	struct afs_fid		fids[50];
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * check that a directory page is valid
1121da177e4SLinus Torvalds  */
113f3ddee8dSDavid Howells static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
114f3ddee8dSDavid Howells 			       loff_t i_size)
1151da177e4SLinus Torvalds {
11600317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
117f3ddee8dSDavid Howells 	loff_t latter, off;
1181da177e4SLinus Torvalds 	int tmp, qty;
1191da177e4SLinus Torvalds 
120dab17c1aSDavid Howells 	/* Determine how many magic numbers there should be in this page, but
121dab17c1aSDavid Howells 	 * we must take care because the directory may change size under us.
122dab17c1aSDavid Howells 	 */
123dab17c1aSDavid Howells 	off = page_offset(page);
124dab17c1aSDavid Howells 	if (i_size <= off)
125dab17c1aSDavid Howells 		goto checked;
126dab17c1aSDavid Howells 
127dab17c1aSDavid Howells 	latter = i_size - off;
1281da177e4SLinus Torvalds 	if (latter >= PAGE_SIZE)
1291da177e4SLinus Torvalds 		qty = PAGE_SIZE;
1301da177e4SLinus Torvalds 	else
1311da177e4SLinus Torvalds 		qty = latter;
13200317636SDavid Howells 	qty /= sizeof(union afs_xdr_dir_block);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	/* check them */
13563a4681fSDavid Howells 	dbuf = kmap(page);
1361da177e4SLinus Torvalds 	for (tmp = 0; tmp < qty; tmp++) {
13700317636SDavid Howells 		if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
138dab17c1aSDavid Howells 			printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
139f3ddee8dSDavid Howells 			       __func__, dvnode->vfs_inode.i_ino, tmp, qty,
14000317636SDavid Howells 			       ntohs(dbuf->blocks[tmp].hdr.magic));
141f3ddee8dSDavid Howells 			trace_afs_dir_check_failed(dvnode, off, i_size);
14263a4681fSDavid Howells 			kunmap(page);
143f51375cdSDavid Howells 			trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
1441da177e4SLinus Torvalds 			goto error;
1451da177e4SLinus Torvalds 		}
14663a4681fSDavid Howells 
14763a4681fSDavid Howells 		/* Make sure each block is NUL terminated so we can reasonably
14863a4681fSDavid Howells 		 * use string functions on it.  The filenames in the page
14963a4681fSDavid Howells 		 * *should* be NUL-terminated anyway.
15063a4681fSDavid Howells 		 */
15163a4681fSDavid Howells 		((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 
15463a4681fSDavid Howells 	kunmap(page);
15563a4681fSDavid Howells 
156dab17c1aSDavid Howells checked:
157f3ddee8dSDavid Howells 	afs_stat_v(dvnode, n_read_dir);
158be5b82dbSAl Viro 	return true;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds error:
161be5b82dbSAl Viro 	return false;
162ec26815aSDavid Howells }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /*
165445b1028SDavid Howells  * Check the contents of a directory that we've just read.
166445b1028SDavid Howells  */
167445b1028SDavid Howells static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
168445b1028SDavid Howells {
169445b1028SDavid Howells 	struct afs_xdr_dir_page *dbuf;
170445b1028SDavid Howells 	unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
171445b1028SDavid Howells 
172445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++)
173445b1028SDavid Howells 		if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
174445b1028SDavid Howells 			goto bad;
175445b1028SDavid Howells 	return true;
176445b1028SDavid Howells 
177445b1028SDavid Howells bad:
178445b1028SDavid Howells 	pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
179445b1028SDavid Howells 		dvnode->fid.vid, dvnode->fid.vnode,
180445b1028SDavid Howells 		req->file_size, req->len, req->actual_len, req->remain);
181445b1028SDavid Howells 	pr_warn("DIR %llx %x %x %x\n",
182445b1028SDavid Howells 		req->pos, req->index, req->nr_pages, req->offset);
183445b1028SDavid Howells 
184445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++) {
185445b1028SDavid Howells 		dbuf = kmap(req->pages[i]);
186445b1028SDavid Howells 		for (j = 0; j < qty; j++) {
187445b1028SDavid Howells 			union afs_xdr_dir_block *block = &dbuf->blocks[j];
188445b1028SDavid Howells 
189445b1028SDavid Howells 			pr_warn("[%02x] %32phN\n", i * qty + j, block);
190445b1028SDavid Howells 		}
191445b1028SDavid Howells 		kunmap(req->pages[i]);
192445b1028SDavid Howells 	}
193445b1028SDavid Howells 	return false;
194445b1028SDavid Howells }
195445b1028SDavid Howells 
196445b1028SDavid Howells /*
1971da177e4SLinus Torvalds  * open an AFS directory file
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	_enter("{%lu}", inode->i_ino);
2021da177e4SLinus Torvalds 
20300317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
20400317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
2051da177e4SLinus Torvalds 
20608e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
2071da177e4SLinus Torvalds 		return -ENOENT;
2081da177e4SLinus Torvalds 
20900d3b7a4SDavid Howells 	return afs_open(inode, file);
210ec26815aSDavid Howells }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /*
213f3ddee8dSDavid Howells  * Read the directory into the pagecache in one go, scrubbing the previous
214f3ddee8dSDavid Howells  * contents.  The list of pages is returned, pinning them so that they don't
215f3ddee8dSDavid Howells  * get reclaimed during the iteration.
216f3ddee8dSDavid Howells  */
217f3ddee8dSDavid Howells static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
218b61f7dcfSDavid Howells 	__acquires(&dvnode->validate_lock)
219f3ddee8dSDavid Howells {
220f3ddee8dSDavid Howells 	struct afs_read *req;
221f3ddee8dSDavid Howells 	loff_t i_size;
222f3ddee8dSDavid Howells 	int nr_pages, nr_inline, i, n;
223f3ddee8dSDavid Howells 	int ret = -ENOMEM;
224f3ddee8dSDavid Howells 
225f3ddee8dSDavid Howells retry:
226f3ddee8dSDavid Howells 	i_size = i_size_read(&dvnode->vfs_inode);
227f3ddee8dSDavid Howells 	if (i_size < 2048)
228f51375cdSDavid Howells 		return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
229f51375cdSDavid Howells 	if (i_size > 2048 * 1024) {
230f51375cdSDavid Howells 		trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
231f3ddee8dSDavid Howells 		return ERR_PTR(-EFBIG);
232f51375cdSDavid Howells 	}
233f3ddee8dSDavid Howells 
234f3ddee8dSDavid Howells 	_enter("%llu", i_size);
235f3ddee8dSDavid Howells 
236f3ddee8dSDavid Howells 	/* Get a request record to hold the page list.  We want to hold it
237f3ddee8dSDavid Howells 	 * inline if we can, but we don't want to make an order 1 allocation.
238f3ddee8dSDavid Howells 	 */
239f3ddee8dSDavid Howells 	nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
240f3ddee8dSDavid Howells 	nr_inline = nr_pages;
241f3ddee8dSDavid Howells 	if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
242f3ddee8dSDavid Howells 		nr_inline = 0;
243f3ddee8dSDavid Howells 
244f3ddee8dSDavid Howells 	req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
245f3ddee8dSDavid Howells 		      GFP_KERNEL);
246f3ddee8dSDavid Howells 	if (!req)
247f3ddee8dSDavid Howells 		return ERR_PTR(-ENOMEM);
248f3ddee8dSDavid Howells 
249f3ddee8dSDavid Howells 	refcount_set(&req->usage, 1);
250f3ddee8dSDavid Howells 	req->nr_pages = nr_pages;
251f3ddee8dSDavid Howells 	req->actual_len = i_size; /* May change */
252f3ddee8dSDavid Howells 	req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
253f3ddee8dSDavid Howells 	req->data_version = dvnode->status.data_version; /* May change */
254f3ddee8dSDavid Howells 	if (nr_inline > 0) {
255f3ddee8dSDavid Howells 		req->pages = req->array;
256f3ddee8dSDavid Howells 	} else {
257f3ddee8dSDavid Howells 		req->pages = kcalloc(nr_pages, sizeof(struct page *),
258f3ddee8dSDavid Howells 				     GFP_KERNEL);
259f3ddee8dSDavid Howells 		if (!req->pages)
260f3ddee8dSDavid Howells 			goto error;
261f3ddee8dSDavid Howells 	}
262f3ddee8dSDavid Howells 
263f3ddee8dSDavid Howells 	/* Get a list of all the pages that hold or will hold the directory
264f3ddee8dSDavid Howells 	 * content.  We need to fill in any gaps that we might find where the
265f3ddee8dSDavid Howells 	 * memory reclaimer has been at work.  If there are any gaps, we will
266f3ddee8dSDavid Howells 	 * need to reread the entire directory contents.
267f3ddee8dSDavid Howells 	 */
268f3ddee8dSDavid Howells 	i = 0;
269f3ddee8dSDavid Howells 	do {
270f3ddee8dSDavid Howells 		n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
271f3ddee8dSDavid Howells 					  req->nr_pages - i,
272f3ddee8dSDavid Howells 					  req->pages + i);
273f3ddee8dSDavid Howells 		_debug("find %u at %u/%u", n, i, req->nr_pages);
274f3ddee8dSDavid Howells 		if (n == 0) {
275f3ddee8dSDavid Howells 			gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
276f3ddee8dSDavid Howells 
277f3ddee8dSDavid Howells 			if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
278f3ddee8dSDavid Howells 				afs_stat_v(dvnode, n_inval);
279f3ddee8dSDavid Howells 
280f3ddee8dSDavid Howells 			ret = -ENOMEM;
281f3ddee8dSDavid Howells 			req->pages[i] = __page_cache_alloc(gfp);
282f3ddee8dSDavid Howells 			if (!req->pages[i])
283f3ddee8dSDavid Howells 				goto error;
284f3ddee8dSDavid Howells 			ret = add_to_page_cache_lru(req->pages[i],
285f3ddee8dSDavid Howells 						    dvnode->vfs_inode.i_mapping,
286f3ddee8dSDavid Howells 						    i, gfp);
287f3ddee8dSDavid Howells 			if (ret < 0)
288f3ddee8dSDavid Howells 				goto error;
289f3ddee8dSDavid Howells 
290f3ddee8dSDavid Howells 			set_page_private(req->pages[i], 1);
291f3ddee8dSDavid Howells 			SetPagePrivate(req->pages[i]);
292f3ddee8dSDavid Howells 			unlock_page(req->pages[i]);
293f3ddee8dSDavid Howells 			i++;
294f3ddee8dSDavid Howells 		} else {
295f3ddee8dSDavid Howells 			i += n;
296f3ddee8dSDavid Howells 		}
297f3ddee8dSDavid Howells 	} while (i < req->nr_pages);
298f3ddee8dSDavid Howells 
299f3ddee8dSDavid Howells 	/* If we're going to reload, we need to lock all the pages to prevent
300f3ddee8dSDavid Howells 	 * races.
301f3ddee8dSDavid Howells 	 */
302f3ddee8dSDavid Howells 	ret = -ERESTARTSYS;
303b61f7dcfSDavid Howells 	if (down_read_killable(&dvnode->validate_lock) < 0)
304b61f7dcfSDavid Howells 		goto error;
305f3ddee8dSDavid Howells 
306f3ddee8dSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
307f3ddee8dSDavid Howells 		goto success;
308f3ddee8dSDavid Howells 
309b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
310b61f7dcfSDavid Howells 	if (down_write_killable(&dvnode->validate_lock) < 0)
311b61f7dcfSDavid Howells 		goto error;
312b61f7dcfSDavid Howells 
313b61f7dcfSDavid Howells 	if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
31499987c56SDavid Howells 		trace_afs_reload_dir(dvnode);
315f3ddee8dSDavid Howells 		ret = afs_fetch_data(dvnode, key, req);
316f3ddee8dSDavid Howells 		if (ret < 0)
317b61f7dcfSDavid Howells 			goto error_unlock;
318f3ddee8dSDavid Howells 
319f3ddee8dSDavid Howells 		task_io_account_read(PAGE_SIZE * req->nr_pages);
320f3ddee8dSDavid Howells 
321f3ddee8dSDavid Howells 		if (req->len < req->file_size)
322f3ddee8dSDavid Howells 			goto content_has_grown;
323f3ddee8dSDavid Howells 
324f3ddee8dSDavid Howells 		/* Validate the data we just read. */
325f3ddee8dSDavid Howells 		ret = -EIO;
326445b1028SDavid Howells 		if (!afs_dir_check_pages(dvnode, req))
327b61f7dcfSDavid Howells 			goto error_unlock;
328f3ddee8dSDavid Howells 
329f3ddee8dSDavid Howells 		// TODO: Trim excess pages
330f3ddee8dSDavid Howells 
331f3ddee8dSDavid Howells 		set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
332f3ddee8dSDavid Howells 	}
333f3ddee8dSDavid Howells 
334b61f7dcfSDavid Howells 	downgrade_write(&dvnode->validate_lock);
335f3ddee8dSDavid Howells success:
336f3ddee8dSDavid Howells 	return req;
337f3ddee8dSDavid Howells 
338f3ddee8dSDavid Howells error_unlock:
339b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
340f3ddee8dSDavid Howells error:
341f3ddee8dSDavid Howells 	afs_put_read(req);
342f3ddee8dSDavid Howells 	_leave(" = %d", ret);
343f3ddee8dSDavid Howells 	return ERR_PTR(ret);
344f3ddee8dSDavid Howells 
345f3ddee8dSDavid Howells content_has_grown:
346b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
347f3ddee8dSDavid Howells 	afs_put_read(req);
348f3ddee8dSDavid Howells 	goto retry;
349f3ddee8dSDavid Howells }
350f3ddee8dSDavid Howells 
351f3ddee8dSDavid Howells /*
3521da177e4SLinus Torvalds  * deal with one block in an AFS directory
3531da177e4SLinus Torvalds  */
354f51375cdSDavid Howells static int afs_dir_iterate_block(struct afs_vnode *dvnode,
355f51375cdSDavid Howells 				 struct dir_context *ctx,
35600317636SDavid Howells 				 union afs_xdr_dir_block *block,
3571bbae9f8SAl Viro 				 unsigned blkoff)
3581da177e4SLinus Torvalds {
35900317636SDavid Howells 	union afs_xdr_dirent *dire;
3601da177e4SLinus Torvalds 	unsigned offset, next, curr;
3611da177e4SLinus Torvalds 	size_t nlen;
3621bbae9f8SAl Viro 	int tmp;
3631da177e4SLinus Torvalds 
3641bbae9f8SAl Viro 	_enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
3651da177e4SLinus Torvalds 
36600317636SDavid Howells 	curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	/* walk through the block, an entry at a time */
3694ea219a8SDavid Howells 	for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
3704ea219a8SDavid Howells 	     offset < AFS_DIR_SLOTS_PER_BLOCK;
3711da177e4SLinus Torvalds 	     offset = next
3721da177e4SLinus Torvalds 	     ) {
3731da177e4SLinus Torvalds 		next = offset + 1;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 		/* skip entries marked unused in the bitmap */
37600317636SDavid Howells 		if (!(block->hdr.bitmap[offset / 8] &
3771da177e4SLinus Torvalds 		      (1 << (offset % 8)))) {
3785b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: unused",
37900317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block), offset);
3801da177e4SLinus Torvalds 			if (offset >= curr)
3811bbae9f8SAl Viro 				ctx->pos = blkoff +
38200317636SDavid Howells 					next * sizeof(union afs_xdr_dirent);
3831da177e4SLinus Torvalds 			continue;
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		/* got a valid entry */
3871da177e4SLinus Torvalds 		dire = &block->dirents[offset];
3881da177e4SLinus Torvalds 		nlen = strnlen(dire->u.name,
3891da177e4SLinus Torvalds 			       sizeof(*block) -
39000317636SDavid Howells 			       offset * sizeof(union afs_xdr_dirent));
3911da177e4SLinus Torvalds 
3925b5e0928SAlexey Dobriyan 		_debug("ENT[%zu.%u]: %s %zu \"%s\"",
39300317636SDavid Howells 		       blkoff / sizeof(union afs_xdr_dir_block), offset,
3941da177e4SLinus Torvalds 		       (offset < curr ? "skip" : "fill"),
3951da177e4SLinus Torvalds 		       nlen, dire->u.name);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 		/* work out where the next possible entry is */
39800317636SDavid Howells 		for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
3994ea219a8SDavid Howells 			if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
4005b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
4011da177e4SLinus Torvalds 				       " %u travelled beyond end dir block"
4025b5e0928SAlexey Dobriyan 				       " (len %u/%zu)",
40300317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4041da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
405f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_over_end);
4061da177e4SLinus Torvalds 			}
40700317636SDavid Howells 			if (!(block->hdr.bitmap[next / 8] &
4081da177e4SLinus Torvalds 			      (1 << (next % 8)))) {
4095b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
4105b5e0928SAlexey Dobriyan 				       " %u unmarked extension (len %u/%zu)",
41100317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4121da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
413f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
4141da177e4SLinus Torvalds 			}
4151da177e4SLinus Torvalds 
4165b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: ext %u/%zu",
41700317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block),
4181da177e4SLinus Torvalds 			       next, tmp, nlen);
4191da177e4SLinus Torvalds 			next++;
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 		/* skip if starts before the current position */
4231da177e4SLinus Torvalds 		if (offset < curr)
4241da177e4SLinus Torvalds 			continue;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 		/* found the next entry */
4271bbae9f8SAl Viro 		if (!dir_emit(ctx, dire->u.name, nlen,
4281da177e4SLinus Torvalds 			      ntohl(dire->u.vnode),
4295cf9dd55SDavid Howells 			      (ctx->actor == afs_lookup_filldir ||
4305cf9dd55SDavid Howells 			       ctx->actor == afs_lookup_one_filldir)?
4311bbae9f8SAl Viro 			      ntohl(dire->u.unique) : DT_UNKNOWN)) {
4321da177e4SLinus Torvalds 			_leave(" = 0 [full]");
4331da177e4SLinus Torvalds 			return 0;
4341da177e4SLinus Torvalds 		}
4351da177e4SLinus Torvalds 
43600317636SDavid Howells 		ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	_leave(" = 1 [more]");
4401da177e4SLinus Torvalds 	return 1;
441ec26815aSDavid Howells }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds /*
44408e0e7c8SDavid Howells  * iterate through the data blob that lists the contents of an AFS directory
4451da177e4SLinus Torvalds  */
4461bbae9f8SAl Viro static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
4471bbae9f8SAl Viro 			   struct key *key)
4481da177e4SLinus Torvalds {
449f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
45000317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
45100317636SDavid Howells 	union afs_xdr_dir_block *dblock;
452f3ddee8dSDavid Howells 	struct afs_read *req;
4531da177e4SLinus Torvalds 	struct page *page;
4541da177e4SLinus Torvalds 	unsigned blkoff, limit;
4551da177e4SLinus Torvalds 	int ret;
4561da177e4SLinus Torvalds 
4571bbae9f8SAl Viro 	_enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
4581da177e4SLinus Torvalds 
45908e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
4601da177e4SLinus Torvalds 		_leave(" = -ESTALE");
4611da177e4SLinus Torvalds 		return -ESTALE;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
464f3ddee8dSDavid Howells 	req = afs_read_dir(dvnode, key);
465f3ddee8dSDavid Howells 	if (IS_ERR(req))
466f3ddee8dSDavid Howells 		return PTR_ERR(req);
467f3ddee8dSDavid Howells 
4681da177e4SLinus Torvalds 	/* round the file position up to the next entry boundary */
46900317636SDavid Howells 	ctx->pos += sizeof(union afs_xdr_dirent) - 1;
47000317636SDavid Howells 	ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	/* walk through the blocks in sequence */
4731da177e4SLinus Torvalds 	ret = 0;
474f3ddee8dSDavid Howells 	while (ctx->pos < req->actual_len) {
47500317636SDavid Howells 		blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
4761da177e4SLinus Torvalds 
477f3ddee8dSDavid Howells 		/* Fetch the appropriate page from the directory and re-add it
478f3ddee8dSDavid Howells 		 * to the LRU.
479f3ddee8dSDavid Howells 		 */
480f3ddee8dSDavid Howells 		page = req->pages[blkoff / PAGE_SIZE];
481f3ddee8dSDavid Howells 		if (!page) {
482f51375cdSDavid Howells 			ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
4831da177e4SLinus Torvalds 			break;
4841da177e4SLinus Torvalds 		}
485f3ddee8dSDavid Howells 		mark_page_accessed(page);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 		limit = blkoff & ~(PAGE_SIZE - 1);
4881da177e4SLinus Torvalds 
489f3ddee8dSDavid Howells 		dbuf = kmap(page);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 		/* deal with the individual blocks stashed on this page */
4921da177e4SLinus Torvalds 		do {
4931da177e4SLinus Torvalds 			dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
49400317636SDavid Howells 					       sizeof(union afs_xdr_dir_block)];
495f51375cdSDavid Howells 			ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
4961da177e4SLinus Torvalds 			if (ret != 1) {
497f3ddee8dSDavid Howells 				kunmap(page);
4981da177e4SLinus Torvalds 				goto out;
4991da177e4SLinus Torvalds 			}
5001da177e4SLinus Torvalds 
50100317636SDavid Howells 			blkoff += sizeof(union afs_xdr_dir_block);
5021da177e4SLinus Torvalds 
5031bbae9f8SAl Viro 		} while (ctx->pos < dir->i_size && blkoff < limit);
5041da177e4SLinus Torvalds 
505f3ddee8dSDavid Howells 		kunmap(page);
5061da177e4SLinus Torvalds 		ret = 0;
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds out:
510b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
511f3ddee8dSDavid Howells 	afs_put_read(req);
5121da177e4SLinus Torvalds 	_leave(" = %d", ret);
5131da177e4SLinus Torvalds 	return ret;
514ec26815aSDavid Howells }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds /*
5171da177e4SLinus Torvalds  * read an AFS directory
5181da177e4SLinus Torvalds  */
5191bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx)
5201da177e4SLinus Torvalds {
521215804a9SDavid Howells 	return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
522ec26815aSDavid Howells }
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds /*
5255cf9dd55SDavid Howells  * Search the directory for a single name
5261da177e4SLinus Torvalds  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5271da177e4SLinus Torvalds  *   uniquifier through dtype
5281da177e4SLinus Torvalds  */
5295cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
530ac7576f4SMiklos Szeredi 				  int nlen, loff_t fpos, u64 ino, unsigned dtype)
5311da177e4SLinus Torvalds {
5325cf9dd55SDavid Howells 	struct afs_lookup_one_cookie *cookie =
5335cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_one_cookie, ctx);
5341da177e4SLinus Torvalds 
5351bbae9f8SAl Viro 	_enter("{%s,%u},%s,%u,,%llu,%u",
5361bbae9f8SAl Viro 	       cookie->name.name, cookie->name.len, name, nlen,
537ba3e0e1aSDavid S. Miller 	       (unsigned long long) ino, dtype);
5381da177e4SLinus Torvalds 
53908e0e7c8SDavid Howells 	/* insanity checks first */
54000317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
54100317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
54208e0e7c8SDavid Howells 
5431bbae9f8SAl Viro 	if (cookie->name.len != nlen ||
5441bbae9f8SAl Viro 	    memcmp(cookie->name.name, name, nlen) != 0) {
5451da177e4SLinus Torvalds 		_leave(" = 0 [no]");
5461da177e4SLinus Torvalds 		return 0;
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	cookie->fid.vnode = ino;
5501da177e4SLinus Torvalds 	cookie->fid.unique = dtype;
5511da177e4SLinus Torvalds 	cookie->found = 1;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	_leave(" = -1 [found]");
5541da177e4SLinus Torvalds 	return -1;
555ec26815aSDavid Howells }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /*
5585cf9dd55SDavid Howells  * Do a lookup of a single name in a directory
559260a9803SDavid Howells  * - just returns the FID the dentry name maps to if found
5601da177e4SLinus Torvalds  */
5615cf9dd55SDavid Howells static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
56200d3b7a4SDavid Howells 			     struct afs_fid *fid, struct key *key)
5631da177e4SLinus Torvalds {
5641bbae9f8SAl Viro 	struct afs_super_info *as = dir->i_sb->s_fs_info;
5655cf9dd55SDavid Howells 	struct afs_lookup_one_cookie cookie = {
5665cf9dd55SDavid Howells 		.ctx.actor = afs_lookup_one_filldir,
5671bbae9f8SAl Viro 		.name = dentry->d_name,
5681bbae9f8SAl Viro 		.fid.vid = as->volume->vid
5691bbae9f8SAl Viro 	};
5701da177e4SLinus Torvalds 	int ret;
5711da177e4SLinus Torvalds 
572a455589fSAl Viro 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	/* search the directory */
5751bbae9f8SAl Viro 	ret = afs_dir_iterate(dir, &cookie.ctx, key);
5761da177e4SLinus Torvalds 	if (ret < 0) {
57708e0e7c8SDavid Howells 		_leave(" = %d [iter]", ret);
57808e0e7c8SDavid Howells 		return ret;
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	ret = -ENOENT;
5821da177e4SLinus Torvalds 	if (!cookie.found) {
58308e0e7c8SDavid Howells 		_leave(" = -ENOENT [not found]");
58408e0e7c8SDavid Howells 		return -ENOENT;
58508e0e7c8SDavid Howells 	}
58608e0e7c8SDavid Howells 
58708e0e7c8SDavid Howells 	*fid = cookie.fid;
5883b6492dfSDavid Howells 	_leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
58908e0e7c8SDavid Howells 	return 0;
59008e0e7c8SDavid Howells }
59108e0e7c8SDavid Howells 
59208e0e7c8SDavid Howells /*
5935cf9dd55SDavid Howells  * search the directory for a name
5945cf9dd55SDavid Howells  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5955cf9dd55SDavid Howells  *   uniquifier through dtype
5965cf9dd55SDavid Howells  */
5975cf9dd55SDavid Howells static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
5985cf9dd55SDavid Howells 			      int nlen, loff_t fpos, u64 ino, unsigned dtype)
5995cf9dd55SDavid Howells {
6005cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie =
6015cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_cookie, ctx);
6025cf9dd55SDavid Howells 	int ret;
6035cf9dd55SDavid Howells 
6045cf9dd55SDavid Howells 	_enter("{%s,%u},%s,%u,,%llu,%u",
6055cf9dd55SDavid Howells 	       cookie->name.name, cookie->name.len, name, nlen,
6065cf9dd55SDavid Howells 	       (unsigned long long) ino, dtype);
6075cf9dd55SDavid Howells 
6085cf9dd55SDavid Howells 	/* insanity checks first */
60900317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
61000317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
6115cf9dd55SDavid Howells 
6125cf9dd55SDavid Howells 	if (cookie->found) {
6135cf9dd55SDavid Howells 		if (cookie->nr_fids < 50) {
6145cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].vnode	= ino;
6155cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].unique	= dtype;
6165cf9dd55SDavid Howells 			cookie->nr_fids++;
6175cf9dd55SDavid Howells 		}
6185cf9dd55SDavid Howells 	} else if (cookie->name.len == nlen &&
6195cf9dd55SDavid Howells 		   memcmp(cookie->name.name, name, nlen) == 0) {
6205cf9dd55SDavid Howells 		cookie->fids[0].vnode	= ino;
6215cf9dd55SDavid Howells 		cookie->fids[0].unique	= dtype;
6225cf9dd55SDavid Howells 		cookie->found = 1;
6235cf9dd55SDavid Howells 		if (cookie->one_only)
6245cf9dd55SDavid Howells 			return -1;
6255cf9dd55SDavid Howells 	}
6265cf9dd55SDavid Howells 
6275cf9dd55SDavid Howells 	ret = cookie->nr_fids >= 50 ? -1 : 0;
6285cf9dd55SDavid Howells 	_leave(" = %d", ret);
6295cf9dd55SDavid Howells 	return ret;
6305cf9dd55SDavid Howells }
6315cf9dd55SDavid Howells 
6325cf9dd55SDavid Howells /*
6335cf9dd55SDavid Howells  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
6345cf9dd55SDavid Howells  * files in one go and create inodes for them.  The inode of the file we were
6355cf9dd55SDavid Howells  * asked for is returned.
6365cf9dd55SDavid Howells  */
6375cf9dd55SDavid Howells static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
6385cf9dd55SDavid Howells 				   struct key *key)
6395cf9dd55SDavid Howells {
6405cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie;
6415cf9dd55SDavid Howells 	struct afs_cb_interest *cbi = NULL;
6425cf9dd55SDavid Howells 	struct afs_super_info *as = dir->i_sb->s_fs_info;
64387182759SDavid Howells 	struct afs_status_cb *scb;
6445cf9dd55SDavid Howells 	struct afs_iget_data data;
6455cf9dd55SDavid Howells 	struct afs_fs_cursor fc;
6465cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
6475cf9dd55SDavid Howells 	struct inode *inode = NULL;
6485cf9dd55SDavid Howells 	int ret, i;
6495cf9dd55SDavid Howells 
6505cf9dd55SDavid Howells 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
6515cf9dd55SDavid Howells 
6525cf9dd55SDavid Howells 	cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
6535cf9dd55SDavid Howells 	if (!cookie)
6545cf9dd55SDavid Howells 		return ERR_PTR(-ENOMEM);
6555cf9dd55SDavid Howells 
6565cf9dd55SDavid Howells 	cookie->ctx.actor = afs_lookup_filldir;
6575cf9dd55SDavid Howells 	cookie->name = dentry->d_name;
6585cf9dd55SDavid Howells 	cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
6595cf9dd55SDavid Howells 
6605cf9dd55SDavid Howells 	read_seqlock_excl(&dvnode->cb_lock);
6615cf9dd55SDavid Howells 	if (dvnode->cb_interest &&
6625cf9dd55SDavid Howells 	    dvnode->cb_interest->server &&
6635cf9dd55SDavid Howells 	    test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
6645cf9dd55SDavid Howells 		cookie->one_only = true;
6655cf9dd55SDavid Howells 	read_sequnlock_excl(&dvnode->cb_lock);
6665cf9dd55SDavid Howells 
6675cf9dd55SDavid Howells 	for (i = 0; i < 50; i++)
6685cf9dd55SDavid Howells 		cookie->fids[i].vid = as->volume->vid;
6695cf9dd55SDavid Howells 
6705cf9dd55SDavid Howells 	/* search the directory */
6715cf9dd55SDavid Howells 	ret = afs_dir_iterate(dir, &cookie->ctx, key);
6725cf9dd55SDavid Howells 	if (ret < 0) {
6735cf9dd55SDavid Howells 		inode = ERR_PTR(ret);
6745cf9dd55SDavid Howells 		goto out;
6755cf9dd55SDavid Howells 	}
6765cf9dd55SDavid Howells 
6775cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOENT);
6785cf9dd55SDavid Howells 	if (!cookie->found)
6795cf9dd55SDavid Howells 		goto out;
6805cf9dd55SDavid Howells 
6815cf9dd55SDavid Howells 	/* Check to see if we already have an inode for the primary fid. */
6825cf9dd55SDavid Howells 	data.volume = dvnode->volume;
6835cf9dd55SDavid Howells 	data.fid = cookie->fids[0];
6845cf9dd55SDavid Howells 	inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
6855cf9dd55SDavid Howells 	if (inode)
6865cf9dd55SDavid Howells 		goto out;
6875cf9dd55SDavid Howells 
6885cf9dd55SDavid Howells 	/* Need space for examining all the selected files */
6895cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOMEM);
69087182759SDavid Howells 	cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb),
6915cf9dd55SDavid Howells 				    GFP_KERNEL);
6925cf9dd55SDavid Howells 	if (!cookie->statuses)
6935cf9dd55SDavid Howells 		goto out;
6945cf9dd55SDavid Howells 
6955cf9dd55SDavid Howells 	/* Try FS.InlineBulkStatus first.  Abort codes for the individual
6965cf9dd55SDavid Howells 	 * lookups contained therein are stored in the reply without aborting
6975cf9dd55SDavid Howells 	 * the whole operation.
6985cf9dd55SDavid Howells 	 */
6995cf9dd55SDavid Howells 	if (cookie->one_only)
7005cf9dd55SDavid Howells 		goto no_inline_bulk_status;
7015cf9dd55SDavid Howells 
7025cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
70320b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
7045cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
7055cf9dd55SDavid Howells 			if (test_bit(AFS_SERVER_FL_NO_IBULK,
7065cf9dd55SDavid Howells 				      &fc.cbi->server->flags)) {
7075cf9dd55SDavid Howells 				fc.ac.abort_code = RX_INVALID_OPERATION;
7085cf9dd55SDavid Howells 				fc.ac.error = -ECONNABORTED;
7095cf9dd55SDavid Howells 				break;
7105cf9dd55SDavid Howells 			}
7115cf9dd55SDavid Howells 			afs_fs_inline_bulk_status(&fc,
7125cf9dd55SDavid Howells 						  afs_v2net(dvnode),
7135cf9dd55SDavid Howells 						  cookie->fids,
7145cf9dd55SDavid Howells 						  cookie->statuses,
7155cf9dd55SDavid Howells 						  cookie->nr_fids, NULL);
7165cf9dd55SDavid Howells 		}
7175cf9dd55SDavid Howells 
7185cf9dd55SDavid Howells 		if (fc.ac.error == 0)
7195cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
7205cf9dd55SDavid Howells 		if (fc.ac.abort_code == RX_INVALID_OPERATION)
7215cf9dd55SDavid Howells 			set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
7225cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
7235cf9dd55SDavid Howells 	}
7245cf9dd55SDavid Howells 
7255cf9dd55SDavid Howells 	if (!IS_ERR(inode))
7265cf9dd55SDavid Howells 		goto success;
7275cf9dd55SDavid Howells 	if (fc.ac.abort_code != RX_INVALID_OPERATION)
7285cf9dd55SDavid Howells 		goto out_c;
7295cf9dd55SDavid Howells 
7305cf9dd55SDavid Howells no_inline_bulk_status:
7315cf9dd55SDavid Howells 	/* We could try FS.BulkStatus next, but this aborts the entire op if
7325cf9dd55SDavid Howells 	 * any of the lookups fails - so, for the moment, revert to
7335cf9dd55SDavid Howells 	 * FS.FetchStatus for just the primary fid.
7345cf9dd55SDavid Howells 	 */
7355cf9dd55SDavid Howells 	cookie->nr_fids = 1;
7365cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
73720b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
7385cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
73987182759SDavid Howells 			scb = &cookie->statuses[0];
7405cf9dd55SDavid Howells 			afs_fs_fetch_status(&fc,
7415cf9dd55SDavid Howells 					    afs_v2net(dvnode),
7425cf9dd55SDavid Howells 					    cookie->fids,
743a58823acSDavid Howells 					    scb,
7445cf9dd55SDavid Howells 					    NULL);
7455cf9dd55SDavid Howells 		}
7465cf9dd55SDavid Howells 
7475cf9dd55SDavid Howells 		if (fc.ac.error == 0)
7485cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
7495cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
7505cf9dd55SDavid Howells 	}
7515cf9dd55SDavid Howells 
7525cf9dd55SDavid Howells 	if (IS_ERR(inode))
7535cf9dd55SDavid Howells 		goto out_c;
7545cf9dd55SDavid Howells 
7555cf9dd55SDavid Howells 	for (i = 0; i < cookie->nr_fids; i++)
75687182759SDavid Howells 		cookie->statuses[i].status.abort_code = 0;
7575cf9dd55SDavid Howells 
7585cf9dd55SDavid Howells success:
7595cf9dd55SDavid Howells 	/* Turn all the files into inodes and save the first one - which is the
7605cf9dd55SDavid Howells 	 * one we actually want.
7615cf9dd55SDavid Howells 	 */
76287182759SDavid Howells 	scb = &cookie->statuses[0];
76387182759SDavid Howells 	if (scb->status.abort_code != 0)
76487182759SDavid Howells 		inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code));
7655cf9dd55SDavid Howells 
7665cf9dd55SDavid Howells 	for (i = 0; i < cookie->nr_fids; i++) {
76787182759SDavid Howells 		struct afs_status_cb *scb = &cookie->statuses[i];
7685cf9dd55SDavid Howells 		struct inode *ti;
7695cf9dd55SDavid Howells 
77087182759SDavid Howells 		if (scb->status.abort_code != 0)
7715cf9dd55SDavid Howells 			continue;
7725cf9dd55SDavid Howells 
7735cf9dd55SDavid Howells 		ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
774a58823acSDavid Howells 			      scb, cbi, dvnode);
7755cf9dd55SDavid Howells 		if (i == 0) {
7765cf9dd55SDavid Howells 			inode = ti;
7775cf9dd55SDavid Howells 		} else {
7785cf9dd55SDavid Howells 			if (!IS_ERR(ti))
7795cf9dd55SDavid Howells 				iput(ti);
7805cf9dd55SDavid Howells 		}
7815cf9dd55SDavid Howells 	}
7825cf9dd55SDavid Howells 
7835cf9dd55SDavid Howells out_c:
7845cf9dd55SDavid Howells 	afs_put_cb_interest(afs_v2net(dvnode), cbi);
78587182759SDavid Howells 	kvfree(cookie->statuses);
7865cf9dd55SDavid Howells out:
7875cf9dd55SDavid Howells 	kfree(cookie);
7885cf9dd55SDavid Howells 	return inode;
7895cf9dd55SDavid Howells }
7905cf9dd55SDavid Howells 
7915cf9dd55SDavid Howells /*
7926f8880d8SDavid Howells  * Look up an entry in a directory with @sys substitution.
7936f8880d8SDavid Howells  */
7946f8880d8SDavid Howells static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
7956f8880d8SDavid Howells 				       struct key *key)
7966f8880d8SDavid Howells {
7976f8880d8SDavid Howells 	struct afs_sysnames *subs;
7986f8880d8SDavid Howells 	struct afs_net *net = afs_i2net(dir);
7996f8880d8SDavid Howells 	struct dentry *ret;
8006f8880d8SDavid Howells 	char *buf, *p, *name;
8016f8880d8SDavid Howells 	int len, i;
8026f8880d8SDavid Howells 
8036f8880d8SDavid Howells 	_enter("");
8046f8880d8SDavid Howells 
8056f8880d8SDavid Howells 	ret = ERR_PTR(-ENOMEM);
8066f8880d8SDavid Howells 	p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
8076f8880d8SDavid Howells 	if (!buf)
8086f8880d8SDavid Howells 		goto out_p;
8096f8880d8SDavid Howells 	if (dentry->d_name.len > 4) {
8106f8880d8SDavid Howells 		memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
8116f8880d8SDavid Howells 		p += dentry->d_name.len - 4;
8126f8880d8SDavid Howells 	}
8136f8880d8SDavid Howells 
8146f8880d8SDavid Howells 	/* There is an ordered list of substitutes that we have to try. */
8156f8880d8SDavid Howells 	read_lock(&net->sysnames_lock);
8166f8880d8SDavid Howells 	subs = net->sysnames;
8176f8880d8SDavid Howells 	refcount_inc(&subs->usage);
8186f8880d8SDavid Howells 	read_unlock(&net->sysnames_lock);
8196f8880d8SDavid Howells 
8206f8880d8SDavid Howells 	for (i = 0; i < subs->nr; i++) {
8216f8880d8SDavid Howells 		name = subs->subs[i];
8226f8880d8SDavid Howells 		len = dentry->d_name.len - 4 + strlen(name);
8236f8880d8SDavid Howells 		if (len >= AFSNAMEMAX) {
8246f8880d8SDavid Howells 			ret = ERR_PTR(-ENAMETOOLONG);
8256f8880d8SDavid Howells 			goto out_s;
8266f8880d8SDavid Howells 		}
8276f8880d8SDavid Howells 
8286f8880d8SDavid Howells 		strcpy(p, name);
8296f8880d8SDavid Howells 		ret = lookup_one_len(buf, dentry->d_parent, len);
8306f8880d8SDavid Howells 		if (IS_ERR(ret) || d_is_positive(ret))
8316f8880d8SDavid Howells 			goto out_s;
8326f8880d8SDavid Howells 		dput(ret);
8336f8880d8SDavid Howells 	}
8346f8880d8SDavid Howells 
8356f8880d8SDavid Howells 	/* We don't want to d_add() the @sys dentry here as we don't want to
8366f8880d8SDavid Howells 	 * the cached dentry to hide changes to the sysnames list.
8376f8880d8SDavid Howells 	 */
8386f8880d8SDavid Howells 	ret = NULL;
8396f8880d8SDavid Howells out_s:
8406f8880d8SDavid Howells 	afs_put_sysnames(subs);
8416f8880d8SDavid Howells 	kfree(buf);
8426f8880d8SDavid Howells out_p:
8436f8880d8SDavid Howells 	key_put(key);
8446f8880d8SDavid Howells 	return ret;
8456f8880d8SDavid Howells }
8466f8880d8SDavid Howells 
8476f8880d8SDavid Howells /*
84808e0e7c8SDavid Howells  * look up an entry in a directory
84908e0e7c8SDavid Howells  */
850260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
85100cd8dd3SAl Viro 				 unsigned int flags)
85208e0e7c8SDavid Howells {
8535cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
85408e0e7c8SDavid Howells 	struct inode *inode;
85534b2a88fSAl Viro 	struct dentry *d;
85600d3b7a4SDavid Howells 	struct key *key;
85708e0e7c8SDavid Howells 	int ret;
85808e0e7c8SDavid Howells 
8593b6492dfSDavid Howells 	_enter("{%llx:%llu},%p{%pd},",
8605cf9dd55SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
861260a9803SDavid Howells 
8622b0143b5SDavid Howells 	ASSERTCMP(d_inode(dentry), ==, NULL);
86308e0e7c8SDavid Howells 
86445222b9eSDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX) {
86508e0e7c8SDavid Howells 		_leave(" = -ENAMETOOLONG");
86608e0e7c8SDavid Howells 		return ERR_PTR(-ENAMETOOLONG);
86708e0e7c8SDavid Howells 	}
86808e0e7c8SDavid Howells 
8695cf9dd55SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
87008e0e7c8SDavid Howells 		_leave(" = -ESTALE");
87108e0e7c8SDavid Howells 		return ERR_PTR(-ESTALE);
87208e0e7c8SDavid Howells 	}
87308e0e7c8SDavid Howells 
8745cf9dd55SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
87500d3b7a4SDavid Howells 	if (IS_ERR(key)) {
87600d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
877e231c2eeSDavid Howells 		return ERR_CAST(key);
87800d3b7a4SDavid Howells 	}
87900d3b7a4SDavid Howells 
8805cf9dd55SDavid Howells 	ret = afs_validate(dvnode, key);
88108e0e7c8SDavid Howells 	if (ret < 0) {
88200d3b7a4SDavid Howells 		key_put(key);
883260a9803SDavid Howells 		_leave(" = %d [val]", ret);
8841da177e4SLinus Torvalds 		return ERR_PTR(ret);
8851da177e4SLinus Torvalds 	}
8861da177e4SLinus Torvalds 
8876f8880d8SDavid Howells 	if (dentry->d_name.len >= 4 &&
8886f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
8896f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
8906f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
8916f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 1] == 's')
8926f8880d8SDavid Howells 		return afs_lookup_atsys(dir, dentry, key);
8936f8880d8SDavid Howells 
894d55b4da4SDavid Howells 	afs_stat_v(dvnode, n_lookup);
8955cf9dd55SDavid Howells 	inode = afs_do_lookup(dir, dentry, key);
89634b2a88fSAl Viro 	key_put(key);
89734b2a88fSAl Viro 	if (inode == ERR_PTR(-ENOENT)) {
8985cf9dd55SDavid Howells 		inode = afs_try_auto_mntpt(dentry, dir);
89934b2a88fSAl Viro 	} else {
90034b2a88fSAl Viro 		dentry->d_fsdata =
90134b2a88fSAl Viro 			(void *)(unsigned long)dvnode->status.data_version;
902bec5eb61Swanglei 	}
90334b2a88fSAl Viro 	d = d_splice_alias(inode, dentry);
90480548b03SDavid Howells 	if (!IS_ERR_OR_NULL(d)) {
90534b2a88fSAl Viro 		d->d_fsdata = dentry->d_fsdata;
90680548b03SDavid Howells 		trace_afs_lookup(dvnode, &d->d_name,
90780548b03SDavid Howells 				 inode ? AFS_FS_I(inode) : NULL);
90880548b03SDavid Howells 	} else {
90980548b03SDavid Howells 		trace_afs_lookup(dvnode, &dentry->d_name,
91080548b03SDavid Howells 				 inode ? AFS_FS_I(inode) : NULL);
91180548b03SDavid Howells 	}
91234b2a88fSAl Viro 	return d;
913ec26815aSDavid Howells }
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds /*
9161da177e4SLinus Torvalds  * check that a dentry lookup hit has found a valid entry
9171da177e4SLinus Torvalds  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
9181da177e4SLinus Torvalds  *   inode
9191da177e4SLinus Torvalds  */
9200b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
9211da177e4SLinus Torvalds {
922260a9803SDavid Howells 	struct afs_vnode *vnode, *dir;
923dd0d9a46SArtem Bityutskiy 	struct afs_fid uninitialized_var(fid);
9241da177e4SLinus Torvalds 	struct dentry *parent;
925c435ee34SDavid Howells 	struct inode *inode;
92600d3b7a4SDavid Howells 	struct key *key;
927a4ff7401SDavid Howells 	long dir_version, de_version;
9281da177e4SLinus Torvalds 	int ret;
9291da177e4SLinus Torvalds 
9300b728e19SAl Viro 	if (flags & LOOKUP_RCU)
93134286d66SNick Piggin 		return -ECHILD;
93234286d66SNick Piggin 
933c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
9342b0143b5SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
9353b6492dfSDavid Howells 		_enter("{v={%llx:%llu} n=%pd fl=%lx},",
936a455589fSAl Viro 		       vnode->fid.vid, vnode->fid.vnode, dentry,
937260a9803SDavid Howells 		       vnode->flags);
938c435ee34SDavid Howells 	} else {
939a455589fSAl Viro 		_enter("{neg n=%pd}", dentry);
940c435ee34SDavid Howells 	}
9411da177e4SLinus Torvalds 
942260a9803SDavid Howells 	key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
94300d3b7a4SDavid Howells 	if (IS_ERR(key))
94400d3b7a4SDavid Howells 		key = NULL;
94500d3b7a4SDavid Howells 
946c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
947c435ee34SDavid Howells 		inode = d_inode(dentry);
948c435ee34SDavid Howells 		if (inode) {
949c435ee34SDavid Howells 			vnode = AFS_FS_I(inode);
950c435ee34SDavid Howells 			afs_validate(vnode, key);
951c435ee34SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
952c435ee34SDavid Howells 				goto out_bad;
953c435ee34SDavid Howells 		}
954c435ee34SDavid Howells 	}
955c435ee34SDavid Howells 
9561da177e4SLinus Torvalds 	/* lock down the parent dentry so we can peer at it */
95708e0e7c8SDavid Howells 	parent = dget_parent(dentry);
9582b0143b5SDavid Howells 	dir = AFS_FS_I(d_inode(parent));
9591da177e4SLinus Torvalds 
960260a9803SDavid Howells 	/* validate the parent directory */
961260a9803SDavid Howells 	afs_validate(dir, key);
962260a9803SDavid Howells 
963260a9803SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
964a455589fSAl Viro 		_debug("%pd: parent dir deleted", dentry);
965c435ee34SDavid Howells 		goto out_bad_parent;
9661da177e4SLinus Torvalds 	}
9671da177e4SLinus Torvalds 
968a4ff7401SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
969a4ff7401SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
970a4ff7401SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
971a4ff7401SDavid Howells 	 * version.
972a4ff7401SDavid Howells 	 */
973a4ff7401SDavid Howells 	dir_version = (long)dir->status.data_version;
974a4ff7401SDavid Howells 	de_version = (long)dentry->d_fsdata;
975a4ff7401SDavid Howells 	if (de_version == dir_version)
976a4ff7401SDavid Howells 		goto out_valid;
977a4ff7401SDavid Howells 
978a4ff7401SDavid Howells 	dir_version = (long)dir->invalid_before;
979a4ff7401SDavid Howells 	if (de_version - dir_version >= 0)
980a4ff7401SDavid Howells 		goto out_valid;
981260a9803SDavid Howells 
98208e0e7c8SDavid Howells 	_debug("dir modified");
983d55b4da4SDavid Howells 	afs_stat_v(dir, n_reval);
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	/* search the directory for this vnode */
9865cf9dd55SDavid Howells 	ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
987260a9803SDavid Howells 	switch (ret) {
988260a9803SDavid Howells 	case 0:
989260a9803SDavid Howells 		/* the filename maps to something */
9902b0143b5SDavid Howells 		if (d_really_is_negative(dentry))
991c435ee34SDavid Howells 			goto out_bad_parent;
992c435ee34SDavid Howells 		inode = d_inode(dentry);
993c435ee34SDavid Howells 		if (is_bad_inode(inode)) {
994a455589fSAl Viro 			printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
995a455589fSAl Viro 			       dentry);
996c435ee34SDavid Howells 			goto out_bad_parent;
9971da177e4SLinus Torvalds 		}
9981da177e4SLinus Torvalds 
999c435ee34SDavid Howells 		vnode = AFS_FS_I(inode);
1000c435ee34SDavid Howells 
10011da177e4SLinus Torvalds 		/* if the vnode ID has changed, then the dirent points to a
10021da177e4SLinus Torvalds 		 * different file */
100308e0e7c8SDavid Howells 		if (fid.vnode != vnode->fid.vnode) {
10043b6492dfSDavid Howells 			_debug("%pd: dirent changed [%llu != %llu]",
1005a455589fSAl Viro 			       dentry, fid.vnode,
100608e0e7c8SDavid Howells 			       vnode->fid.vnode);
10071da177e4SLinus Torvalds 			goto not_found;
10081da177e4SLinus Torvalds 		}
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 		/* if the vnode ID uniqifier has changed, then the file has
1011260a9803SDavid Howells 		 * been deleted and replaced, and the original vnode ID has
1012260a9803SDavid Howells 		 * been reused */
101308e0e7c8SDavid Howells 		if (fid.unique != vnode->fid.unique) {
1014a455589fSAl Viro 			_debug("%pd: file deleted (uq %u -> %u I:%u)",
1015a455589fSAl Viro 			       dentry, fid.unique,
10167a224228SJean Noel Cordenner 			       vnode->fid.unique,
1017c435ee34SDavid Howells 			       vnode->vfs_inode.i_generation);
1018c435ee34SDavid Howells 			write_seqlock(&vnode->cb_lock);
101908e0e7c8SDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
1020c435ee34SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1021260a9803SDavid Howells 			goto not_found;
1022260a9803SDavid Howells 		}
1023260a9803SDavid Howells 		goto out_valid;
1024260a9803SDavid Howells 
1025260a9803SDavid Howells 	case -ENOENT:
1026260a9803SDavid Howells 		/* the filename is unknown */
1027a455589fSAl Viro 		_debug("%pd: dirent not found", dentry);
10282b0143b5SDavid Howells 		if (d_really_is_positive(dentry))
1029260a9803SDavid Howells 			goto not_found;
1030260a9803SDavid Howells 		goto out_valid;
1031260a9803SDavid Howells 
1032260a9803SDavid Howells 	default:
1033a455589fSAl Viro 		_debug("failed to iterate dir %pd: %d",
1034a455589fSAl Viro 		       parent, ret);
1035c435ee34SDavid Howells 		goto out_bad_parent;
10361da177e4SLinus Torvalds 	}
103708e0e7c8SDavid Howells 
10381da177e4SLinus Torvalds out_valid:
1039a4ff7401SDavid Howells 	dentry->d_fsdata = (void *)dir_version;
10401da177e4SLinus Torvalds 	dput(parent);
104100d3b7a4SDavid Howells 	key_put(key);
10421da177e4SLinus Torvalds 	_leave(" = 1 [valid]");
10431da177e4SLinus Torvalds 	return 1;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	/* the dirent, if it exists, now points to a different vnode */
10461da177e4SLinus Torvalds not_found:
10471da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
10481da177e4SLinus Torvalds 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
10491da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
10501da177e4SLinus Torvalds 
1051c435ee34SDavid Howells out_bad_parent:
1052a455589fSAl Viro 	_debug("dropping dentry %pd2", dentry);
10531da177e4SLinus Torvalds 	dput(parent);
1054c435ee34SDavid Howells out_bad:
105500d3b7a4SDavid Howells 	key_put(key);
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	_leave(" = 0 [bad]");
10581da177e4SLinus Torvalds 	return 0;
1059ec26815aSDavid Howells }
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds /*
10621da177e4SLinus Torvalds  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
10631da177e4SLinus Torvalds  * sleep)
10641da177e4SLinus Torvalds  * - called from dput() when d_count is going to 0.
10651da177e4SLinus Torvalds  * - return 1 to request dentry be unhashed, 0 otherwise
10661da177e4SLinus Torvalds  */
1067fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry)
10681da177e4SLinus Torvalds {
1069a455589fSAl Viro 	_enter("%pd", dentry);
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
10721da177e4SLinus Torvalds 		goto zap;
10731da177e4SLinus Torvalds 
10742b0143b5SDavid Howells 	if (d_really_is_positive(dentry) &&
10752b0143b5SDavid Howells 	    (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
10762b0143b5SDavid Howells 	     test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
10771da177e4SLinus Torvalds 		goto zap;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	_leave(" = 0 [keep]");
10801da177e4SLinus Torvalds 	return 0;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds zap:
10831da177e4SLinus Torvalds 	_leave(" = 1 [zap]");
10841da177e4SLinus Torvalds 	return 1;
1085ec26815aSDavid Howells }
1086260a9803SDavid Howells 
1087260a9803SDavid Howells /*
108879ddbfa5SDavid Howells  * Clean up sillyrename files on dentry removal.
108979ddbfa5SDavid Howells  */
109079ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode)
109179ddbfa5SDavid Howells {
109279ddbfa5SDavid Howells 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
109379ddbfa5SDavid Howells 		afs_silly_iput(dentry, inode);
109479ddbfa5SDavid Howells 	iput(inode);
109579ddbfa5SDavid Howells }
109679ddbfa5SDavid Howells 
109779ddbfa5SDavid Howells /*
1098260a9803SDavid Howells  * handle dentry release
1099260a9803SDavid Howells  */
110066c7e1d3SDavid Howells void afs_d_release(struct dentry *dentry)
1101260a9803SDavid Howells {
1102a455589fSAl Viro 	_enter("%pd", dentry);
1103260a9803SDavid Howells }
1104260a9803SDavid Howells 
1105260a9803SDavid Howells /*
1106d2ddc776SDavid Howells  * Create a new inode for create/mkdir/symlink
1107d2ddc776SDavid Howells  */
1108d2ddc776SDavid Howells static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1109d2ddc776SDavid Howells 				struct dentry *new_dentry,
1110d2ddc776SDavid Howells 				struct afs_fid *newfid,
1111a58823acSDavid Howells 				struct afs_status_cb *new_scb)
1112d2ddc776SDavid Howells {
11135a813276SDavid Howells 	struct afs_vnode *vnode;
1114d2ddc776SDavid Howells 	struct inode *inode;
1115d2ddc776SDavid Howells 
1116d2ddc776SDavid Howells 	if (fc->ac.error < 0)
1117d2ddc776SDavid Howells 		return;
1118d2ddc776SDavid Howells 
1119d2ddc776SDavid Howells 	inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1120a58823acSDavid Howells 			 newfid, new_scb, fc->cbi, fc->vnode);
1121d2ddc776SDavid Howells 	if (IS_ERR(inode)) {
1122d2ddc776SDavid Howells 		/* ENOMEM or EINTR at a really inconvenient time - just abandon
1123d2ddc776SDavid Howells 		 * the new directory on the server.
1124d2ddc776SDavid Howells 		 */
1125d2ddc776SDavid Howells 		fc->ac.error = PTR_ERR(inode);
1126d2ddc776SDavid Howells 		return;
1127d2ddc776SDavid Howells 	}
1128d2ddc776SDavid Howells 
11295a813276SDavid Howells 	vnode = AFS_FS_I(inode);
11305a813276SDavid Howells 	set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1131a58823acSDavid Howells 	if (fc->ac.error == 0)
1132a58823acSDavid Howells 		afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb);
113373116df7SDavid Howells 	d_instantiate(new_dentry, inode);
1134d2ddc776SDavid Howells }
1135d2ddc776SDavid Howells 
1136d2ddc776SDavid Howells /*
1137260a9803SDavid Howells  * create a directory on an AFS filesystem
1138260a9803SDavid Howells  */
113918bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1140260a9803SDavid Howells {
1141a58823acSDavid Howells 	struct afs_status_cb *scb;
1142d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1143d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1144d2ddc776SDavid Howells 	struct afs_fid newfid;
1145260a9803SDavid Howells 	struct key *key;
1146260a9803SDavid Howells 	int ret;
1147260a9803SDavid Howells 
1148d2ddc776SDavid Howells 	mode |= S_IFDIR;
1149260a9803SDavid Howells 
11503b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho",
1151a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1152260a9803SDavid Howells 
1153a58823acSDavid Howells 	ret = -ENOMEM;
1154a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1155a58823acSDavid Howells 	if (!scb)
1156a58823acSDavid Howells 		goto error;
1157a58823acSDavid Howells 
1158260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1159260a9803SDavid Howells 	if (IS_ERR(key)) {
1160260a9803SDavid Howells 		ret = PTR_ERR(key);
1161a58823acSDavid Howells 		goto error_scb;
1162260a9803SDavid Howells 	}
1163260a9803SDavid Howells 
1164d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
116520b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1166a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1167a58823acSDavid Howells 
1168d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
116968251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1170a58823acSDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1171a58823acSDavid Howells 				      &scb[0], &newfid, &scb[1]);
1172d2ddc776SDavid Howells 		}
1173d2ddc776SDavid Howells 
1174a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1175a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1176a58823acSDavid Howells 					&data_version, &scb[0]);
1177a58823acSDavid Howells 		afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
1178d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1179260a9803SDavid Howells 		if (ret < 0)
1180d2ddc776SDavid Howells 			goto error_key;
11814433b691SDavid Howells 	} else {
11824433b691SDavid Howells 		goto error_key;
1183260a9803SDavid Howells 	}
1184260a9803SDavid Howells 
118563a4681fSDavid Howells 	if (ret == 0 &&
118663a4681fSDavid Howells 	    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
118763a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
118863a4681fSDavid Howells 				 afs_edit_dir_for_create);
118963a4681fSDavid Howells 
1190260a9803SDavid Howells 	key_put(key);
1191a58823acSDavid Howells 	kfree(scb);
1192260a9803SDavid Howells 	_leave(" = 0");
1193260a9803SDavid Howells 	return 0;
1194260a9803SDavid Howells 
1195d2ddc776SDavid Howells error_key:
1196260a9803SDavid Howells 	key_put(key);
1197a58823acSDavid Howells error_scb:
1198a58823acSDavid Howells 	kfree(scb);
1199260a9803SDavid Howells error:
1200260a9803SDavid Howells 	d_drop(dentry);
1201260a9803SDavid Howells 	_leave(" = %d", ret);
1202260a9803SDavid Howells 	return ret;
1203260a9803SDavid Howells }
1204260a9803SDavid Howells 
1205260a9803SDavid Howells /*
1206d2ddc776SDavid Howells  * Remove a subdir from a directory.
1207260a9803SDavid Howells  */
1208d2ddc776SDavid Howells static void afs_dir_remove_subdir(struct dentry *dentry)
1209260a9803SDavid Howells {
12102b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1211d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1212d2ddc776SDavid Howells 
1213260a9803SDavid Howells 		clear_nlink(&vnode->vfs_inode);
1214260a9803SDavid Howells 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1215c435ee34SDavid Howells 		clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
121663a4681fSDavid Howells 		clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1217260a9803SDavid Howells 	}
1218260a9803SDavid Howells }
1219260a9803SDavid Howells 
1220260a9803SDavid Howells /*
1221d2ddc776SDavid Howells  * remove a directory from an AFS filesystem
1222260a9803SDavid Howells  */
1223d2ddc776SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1224260a9803SDavid Howells {
1225a58823acSDavid Howells 	struct afs_status_cb *scb;
1226d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1227f58db83fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1228260a9803SDavid Howells 	struct key *key;
1229260a9803SDavid Howells 	int ret;
1230260a9803SDavid Howells 
12313b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1232a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1233260a9803SDavid Howells 
1234a58823acSDavid Howells 	scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
1235a58823acSDavid Howells 	if (!scb)
1236a58823acSDavid Howells 		return -ENOMEM;
1237a58823acSDavid Howells 
1238260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1239260a9803SDavid Howells 	if (IS_ERR(key)) {
1240260a9803SDavid Howells 		ret = PTR_ERR(key);
1241260a9803SDavid Howells 		goto error;
1242260a9803SDavid Howells 	}
1243260a9803SDavid Howells 
1244f58db83fSDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1245f58db83fSDavid Howells 	if (d_really_is_positive(dentry)) {
1246f58db83fSDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1247f58db83fSDavid Howells 		ret = afs_validate(vnode, key);
1248f58db83fSDavid Howells 		if (ret < 0)
1249f58db83fSDavid Howells 			goto error_key;
1250f58db83fSDavid Howells 	}
1251f58db83fSDavid Howells 
125279ddbfa5SDavid Howells 	if (vnode) {
125379ddbfa5SDavid Howells 		ret = down_write_killable(&vnode->rmdir_lock);
125479ddbfa5SDavid Howells 		if (ret < 0)
125579ddbfa5SDavid Howells 			goto error_key;
125679ddbfa5SDavid Howells 	}
125779ddbfa5SDavid Howells 
1258d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
125920b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1260a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1261a58823acSDavid Howells 
1262d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
126368251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1264a58823acSDavid Howells 			afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb);
1265260a9803SDavid Howells 		}
1266260a9803SDavid Howells 
1267a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1268a58823acSDavid Howells 					&data_version, scb);
1269d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
127063a4681fSDavid Howells 		if (ret == 0) {
1271d2ddc776SDavid Howells 			afs_dir_remove_subdir(dentry);
127263a4681fSDavid Howells 			if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
127363a4681fSDavid Howells 				afs_edit_dir_remove(dvnode, &dentry->d_name,
127463a4681fSDavid Howells 						    afs_edit_dir_for_rmdir);
127563a4681fSDavid Howells 		}
1276260a9803SDavid Howells 	}
1277260a9803SDavid Howells 
127879ddbfa5SDavid Howells 	if (vnode)
127979ddbfa5SDavid Howells 		up_write(&vnode->rmdir_lock);
1280f58db83fSDavid Howells error_key:
1281260a9803SDavid Howells 	key_put(key);
1282d2ddc776SDavid Howells error:
1283a58823acSDavid Howells 	kfree(scb);
1284d2ddc776SDavid Howells 	return ret;
1285d2ddc776SDavid Howells }
1286260a9803SDavid Howells 
1287d2ddc776SDavid Howells /*
1288d2ddc776SDavid Howells  * Remove a link to a file or symlink from a directory.
1289d2ddc776SDavid Howells  *
1290d2ddc776SDavid Howells  * If the file was not deleted due to excess hard links, the fileserver will
1291d2ddc776SDavid Howells  * break the callback promise on the file - if it had one - before it returns
1292d2ddc776SDavid Howells  * to us, and if it was deleted, it won't
1293d2ddc776SDavid Howells  *
1294d2ddc776SDavid Howells  * However, if we didn't have a callback promise outstanding, or it was
1295d2ddc776SDavid Howells  * outstanding on a different server, then it won't break it either...
1296d2ddc776SDavid Howells  */
129779ddbfa5SDavid Howells int afs_dir_remove_link(struct dentry *dentry, struct key *key,
1298440fbc3aSDavid Howells 			unsigned long d_version_before,
1299440fbc3aSDavid Howells 			unsigned long d_version_after)
1300d2ddc776SDavid Howells {
1301440fbc3aSDavid Howells 	bool dir_valid;
1302d2ddc776SDavid Howells 	int ret = 0;
1303d2ddc776SDavid Howells 
1304440fbc3aSDavid Howells 	/* There were no intervening changes on the server if the version
1305440fbc3aSDavid Howells 	 * number we got back was incremented by exactly 1.
1306440fbc3aSDavid Howells 	 */
1307440fbc3aSDavid Howells 	dir_valid = (d_version_after == d_version_before + 1);
1308440fbc3aSDavid Howells 
1309d2ddc776SDavid Howells 	if (d_really_is_positive(dentry)) {
1310d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1311d2ddc776SDavid Howells 
131230062bd1SDavid Howells 		if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
131330062bd1SDavid Howells 			/* Already done */
131430062bd1SDavid Howells 		} else if (dir_valid) {
1315440fbc3aSDavid Howells 			drop_nlink(&vnode->vfs_inode);
1316440fbc3aSDavid Howells 			if (vnode->vfs_inode.i_nlink == 0) {
1317440fbc3aSDavid Howells 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
1318440fbc3aSDavid Howells 				clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1319440fbc3aSDavid Howells 			}
1320440fbc3aSDavid Howells 			ret = 0;
1321440fbc3aSDavid Howells 		} else {
1322440fbc3aSDavid Howells 			clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1323440fbc3aSDavid Howells 
1324d2ddc776SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1325d2ddc776SDavid Howells 				kdebug("AFS_VNODE_DELETED");
1326d2ddc776SDavid Howells 
1327d2ddc776SDavid Howells 			ret = afs_validate(vnode, key);
1328d2ddc776SDavid Howells 			if (ret == -ESTALE)
1329d2ddc776SDavid Howells 				ret = 0;
1330440fbc3aSDavid Howells 		}
1331d2ddc776SDavid Howells 		_debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1332d2ddc776SDavid Howells 	}
1333d2ddc776SDavid Howells 
1334d2ddc776SDavid Howells 	return ret;
1335d2ddc776SDavid Howells }
1336d2ddc776SDavid Howells 
1337d2ddc776SDavid Howells /*
1338d2ddc776SDavid Howells  * Remove a file or symlink from an AFS filesystem.
1339d2ddc776SDavid Howells  */
1340d2ddc776SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry)
1341d2ddc776SDavid Howells {
1342d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1343a58823acSDavid Howells 	struct afs_status_cb *scb;
134430062bd1SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1345d2ddc776SDavid Howells 	struct key *key;
1346440fbc3aSDavid Howells 	unsigned long d_version = (unsigned long)dentry->d_fsdata;
134779ddbfa5SDavid Howells 	bool need_rehash = false;
1348d2ddc776SDavid Howells 	int ret;
1349d2ddc776SDavid Howells 
13503b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1351d2ddc776SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1352d2ddc776SDavid Howells 
1353d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1354d2ddc776SDavid Howells 		return -ENAMETOOLONG;
1355d2ddc776SDavid Howells 
1356a58823acSDavid Howells 	ret = -ENOMEM;
1357a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1358a58823acSDavid Howells 	if (!scb)
1359a58823acSDavid Howells 		goto error;
1360a58823acSDavid Howells 
1361d2ddc776SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1362d2ddc776SDavid Howells 	if (IS_ERR(key)) {
1363d2ddc776SDavid Howells 		ret = PTR_ERR(key);
1364a58823acSDavid Howells 		goto error_scb;
1365d2ddc776SDavid Howells 	}
1366d2ddc776SDavid Howells 
1367d2ddc776SDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1368d2ddc776SDavid Howells 	if (d_really_is_positive(dentry)) {
1369d2ddc776SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1370d2ddc776SDavid Howells 		ret = afs_validate(vnode, key);
1371d2ddc776SDavid Howells 		if (ret < 0)
1372d2ddc776SDavid Howells 			goto error_key;
1373d2ddc776SDavid Howells 	}
1374d2ddc776SDavid Howells 
137579ddbfa5SDavid Howells 	spin_lock(&dentry->d_lock);
137679ddbfa5SDavid Howells 	if (vnode && d_count(dentry) > 1) {
137779ddbfa5SDavid Howells 		spin_unlock(&dentry->d_lock);
137879ddbfa5SDavid Howells 		/* Start asynchronous writeout of the inode */
137979ddbfa5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
138079ddbfa5SDavid Howells 		ret = afs_sillyrename(dvnode, vnode, dentry, key);
138179ddbfa5SDavid Howells 		goto error_key;
138279ddbfa5SDavid Howells 	}
138379ddbfa5SDavid Howells 	if (!d_unhashed(dentry)) {
138479ddbfa5SDavid Howells 		/* Prevent a race with RCU lookup. */
138579ddbfa5SDavid Howells 		__d_drop(dentry);
138679ddbfa5SDavid Howells 		need_rehash = true;
138779ddbfa5SDavid Howells 	}
138879ddbfa5SDavid Howells 	spin_unlock(&dentry->d_lock);
138979ddbfa5SDavid Howells 
1390d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
139120b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1392a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1393a58823acSDavid Howells 
1394d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
139568251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
139630062bd1SDavid Howells 
139730062bd1SDavid Howells 			if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
139830062bd1SDavid Howells 			    !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
139930062bd1SDavid Howells 				yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1400a58823acSDavid Howells 						    &scb[0], &scb[1]);
1401a58823acSDavid Howells 				if (fc.ac.error == 0 &&
1402a58823acSDavid Howells 				    scb[1].status.abort_code == VNOVNODE) {
1403a58823acSDavid Howells 					set_bit(AFS_VNODE_DELETED, &vnode->flags);
1404a58823acSDavid Howells 					afs_break_callback(vnode);
1405a58823acSDavid Howells 				}
1406a58823acSDavid Howells 
140730062bd1SDavid Howells 				if (fc.ac.error != -ECONNABORTED ||
140830062bd1SDavid Howells 				    fc.ac.abort_code != RXGEN_OPCODE)
140930062bd1SDavid Howells 					continue;
141030062bd1SDavid Howells 				set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
141130062bd1SDavid Howells 			}
141230062bd1SDavid Howells 
1413a58823acSDavid Howells 			afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
1414d2ddc776SDavid Howells 		}
1415d2ddc776SDavid Howells 
1416a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1417a58823acSDavid Howells 					&data_version, &scb[0]);
1418d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1419d2ddc776SDavid Howells 		if (ret == 0)
1420440fbc3aSDavid Howells 			ret = afs_dir_remove_link(
1421440fbc3aSDavid Howells 				dentry, key, d_version,
1422440fbc3aSDavid Howells 				(unsigned long)dvnode->status.data_version);
142363a4681fSDavid Howells 		if (ret == 0 &&
142463a4681fSDavid Howells 		    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
142563a4681fSDavid Howells 			afs_edit_dir_remove(dvnode, &dentry->d_name,
142663a4681fSDavid Howells 					    afs_edit_dir_for_unlink);
1427d2ddc776SDavid Howells 	}
1428d2ddc776SDavid Howells 
142979ddbfa5SDavid Howells 	if (need_rehash && ret < 0 && ret != -ENOENT)
143079ddbfa5SDavid Howells 		d_rehash(dentry);
143179ddbfa5SDavid Howells 
1432d2ddc776SDavid Howells error_key:
1433260a9803SDavid Howells 	key_put(key);
1434a58823acSDavid Howells error_scb:
1435a58823acSDavid Howells 	kfree(scb);
1436260a9803SDavid Howells error:
1437260a9803SDavid Howells 	_leave(" = %d", ret);
1438260a9803SDavid Howells 	return ret;
1439260a9803SDavid Howells }
1440260a9803SDavid Howells 
1441260a9803SDavid Howells /*
1442260a9803SDavid Howells  * create a regular file on an AFS filesystem
1443260a9803SDavid Howells  */
14444acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1445ebfc3b49SAl Viro 		      bool excl)
1446260a9803SDavid Howells {
1447d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1448a58823acSDavid Howells 	struct afs_status_cb *scb;
144943dd388bSColin Ian King 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1450d2ddc776SDavid Howells 	struct afs_fid newfid;
1451260a9803SDavid Howells 	struct key *key;
1452260a9803SDavid Howells 	int ret;
1453260a9803SDavid Howells 
1454d2ddc776SDavid Howells 	mode |= S_IFREG;
1455260a9803SDavid Howells 
14563b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho,",
1457a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1458260a9803SDavid Howells 
1459d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1460d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1461d2ddc776SDavid Howells 		goto error;
1462d2ddc776SDavid Howells 
1463260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1464260a9803SDavid Howells 	if (IS_ERR(key)) {
1465260a9803SDavid Howells 		ret = PTR_ERR(key);
1466260a9803SDavid Howells 		goto error;
1467260a9803SDavid Howells 	}
1468260a9803SDavid Howells 
1469a58823acSDavid Howells 	ret = -ENOMEM;
1470a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1471a58823acSDavid Howells 	if (!scb)
1472a58823acSDavid Howells 		goto error_scb;
1473a58823acSDavid Howells 
1474d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
147520b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1476a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1477a58823acSDavid Howells 
1478d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
147968251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1480a58823acSDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1481a58823acSDavid Howells 				      &scb[0], &newfid, &scb[1]);
1482d2ddc776SDavid Howells 		}
1483d2ddc776SDavid Howells 
1484a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1485a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1486a58823acSDavid Howells 					&data_version, &scb[0]);
1487a58823acSDavid Howells 		afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
1488d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1489260a9803SDavid Howells 		if (ret < 0)
1490d2ddc776SDavid Howells 			goto error_key;
14914433b691SDavid Howells 	} else {
14924433b691SDavid Howells 		goto error_key;
1493260a9803SDavid Howells 	}
1494260a9803SDavid Howells 
149563a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
149663a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
149763a4681fSDavid Howells 				 afs_edit_dir_for_create);
149863a4681fSDavid Howells 
1499a58823acSDavid Howells 	kfree(scb);
1500260a9803SDavid Howells 	key_put(key);
1501260a9803SDavid Howells 	_leave(" = 0");
1502260a9803SDavid Howells 	return 0;
1503260a9803SDavid Howells 
1504a58823acSDavid Howells error_scb:
1505a58823acSDavid Howells 	kfree(scb);
1506d2ddc776SDavid Howells error_key:
1507260a9803SDavid Howells 	key_put(key);
1508260a9803SDavid Howells error:
1509260a9803SDavid Howells 	d_drop(dentry);
1510260a9803SDavid Howells 	_leave(" = %d", ret);
1511260a9803SDavid Howells 	return ret;
1512260a9803SDavid Howells }
1513260a9803SDavid Howells 
1514260a9803SDavid Howells /*
1515260a9803SDavid Howells  * create a hard link between files in an AFS filesystem
1516260a9803SDavid Howells  */
1517260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
1518260a9803SDavid Howells 		    struct dentry *dentry)
1519260a9803SDavid Howells {
1520d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1521a58823acSDavid Howells 	struct afs_status_cb *scb;
1522a58823acSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1523a58823acSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1524260a9803SDavid Howells 	struct key *key;
1525260a9803SDavid Howells 	int ret;
1526260a9803SDavid Howells 
15273b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%pd}",
1528260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1529260a9803SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode,
1530a455589fSAl Viro 	       dentry);
1531260a9803SDavid Howells 
1532d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1533d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1534d2ddc776SDavid Howells 		goto error;
1535d2ddc776SDavid Howells 
1536a58823acSDavid Howells 	ret = -ENOMEM;
1537a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1538a58823acSDavid Howells 	if (!scb)
1539a58823acSDavid Howells 		goto error;
1540a58823acSDavid Howells 
1541260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1542260a9803SDavid Howells 	if (IS_ERR(key)) {
1543260a9803SDavid Howells 		ret = PTR_ERR(key);
1544a58823acSDavid Howells 		goto error_scb;
1545260a9803SDavid Howells 	}
1546260a9803SDavid Howells 
1547d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
154820b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1549a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1550a58823acSDavid Howells 
1551d2ddc776SDavid Howells 		if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1552d2ddc776SDavid Howells 			afs_end_vnode_operation(&fc);
1553bc1527dcSDavid Howells 			goto error_key;
1554d2ddc776SDavid Howells 		}
1555260a9803SDavid Howells 
1556d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
155768251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
155868251f0aSDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1559a58823acSDavid Howells 			afs_fs_link(&fc, vnode, dentry->d_name.name,
1560a58823acSDavid Howells 				    &scb[0], &scb[1]);
1561d2ddc776SDavid Howells 		}
1562d2ddc776SDavid Howells 
1563a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1564a58823acSDavid Howells 					&data_version, &scb[0]);
1565a58823acSDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1566a58823acSDavid Howells 					NULL, &scb[1]);
15677de9c6eeSAl Viro 		ihold(&vnode->vfs_inode);
1568260a9803SDavid Howells 		d_instantiate(dentry, &vnode->vfs_inode);
1569d2ddc776SDavid Howells 
1570d2ddc776SDavid Howells 		mutex_unlock(&vnode->io_lock);
1571d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1572d2ddc776SDavid Howells 		if (ret < 0)
1573d2ddc776SDavid Howells 			goto error_key;
15744433b691SDavid Howells 	} else {
15754433b691SDavid Howells 		goto error_key;
1576d2ddc776SDavid Howells 	}
1577d2ddc776SDavid Howells 
157863a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
157963a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
158063a4681fSDavid Howells 				 afs_edit_dir_for_link);
158163a4681fSDavid Howells 
1582260a9803SDavid Howells 	key_put(key);
1583a58823acSDavid Howells 	kfree(scb);
1584260a9803SDavid Howells 	_leave(" = 0");
1585260a9803SDavid Howells 	return 0;
1586260a9803SDavid Howells 
1587d2ddc776SDavid Howells error_key:
1588260a9803SDavid Howells 	key_put(key);
1589a58823acSDavid Howells error_scb:
1590a58823acSDavid Howells 	kfree(scb);
1591260a9803SDavid Howells error:
1592260a9803SDavid Howells 	d_drop(dentry);
1593260a9803SDavid Howells 	_leave(" = %d", ret);
1594260a9803SDavid Howells 	return ret;
1595260a9803SDavid Howells }
1596260a9803SDavid Howells 
1597260a9803SDavid Howells /*
1598260a9803SDavid Howells  * create a symlink in an AFS filesystem
1599260a9803SDavid Howells  */
1600260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
1601260a9803SDavid Howells 		       const char *content)
1602260a9803SDavid Howells {
1603d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1604a58823acSDavid Howells 	struct afs_status_cb *scb;
1605d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1606d2ddc776SDavid Howells 	struct afs_fid newfid;
1607260a9803SDavid Howells 	struct key *key;
1608260a9803SDavid Howells 	int ret;
1609260a9803SDavid Howells 
16103b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%s",
1611a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry,
1612260a9803SDavid Howells 	       content);
1613260a9803SDavid Howells 
1614d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1615d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1616d2ddc776SDavid Howells 		goto error;
1617d2ddc776SDavid Howells 
1618260a9803SDavid Howells 	ret = -EINVAL;
161945222b9eSDavid Howells 	if (strlen(content) >= AFSPATHMAX)
1620260a9803SDavid Howells 		goto error;
1621260a9803SDavid Howells 
1622a58823acSDavid Howells 	ret = -ENOMEM;
1623a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1624a58823acSDavid Howells 	if (!scb)
1625a58823acSDavid Howells 		goto error;
1626a58823acSDavid Howells 
1627260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1628260a9803SDavid Howells 	if (IS_ERR(key)) {
1629260a9803SDavid Howells 		ret = PTR_ERR(key);
1630a58823acSDavid Howells 		goto error_scb;
1631260a9803SDavid Howells 	}
1632260a9803SDavid Howells 
1633d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
163420b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1635a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1636a58823acSDavid Howells 
1637d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
163868251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1639a58823acSDavid Howells 			afs_fs_symlink(&fc, dentry->d_name.name, content,
1640a58823acSDavid Howells 				       &scb[0], &newfid, &scb[1]);
1641d2ddc776SDavid Howells 		}
1642d2ddc776SDavid Howells 
1643a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1644a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1645a58823acSDavid Howells 					&data_version, &scb[0]);
1646a58823acSDavid Howells 		afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
1647d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1648260a9803SDavid Howells 		if (ret < 0)
1649d2ddc776SDavid Howells 			goto error_key;
16504433b691SDavid Howells 	} else {
16514433b691SDavid Howells 		goto error_key;
1652260a9803SDavid Howells 	}
1653260a9803SDavid Howells 
165463a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
165563a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
165663a4681fSDavid Howells 				 afs_edit_dir_for_symlink);
165763a4681fSDavid Howells 
1658260a9803SDavid Howells 	key_put(key);
1659a58823acSDavid Howells 	kfree(scb);
1660260a9803SDavid Howells 	_leave(" = 0");
1661260a9803SDavid Howells 	return 0;
1662260a9803SDavid Howells 
1663d2ddc776SDavid Howells error_key:
1664260a9803SDavid Howells 	key_put(key);
1665a58823acSDavid Howells error_scb:
1666a58823acSDavid Howells 	kfree(scb);
1667260a9803SDavid Howells error:
1668260a9803SDavid Howells 	d_drop(dentry);
1669260a9803SDavid Howells 	_leave(" = %d", ret);
1670260a9803SDavid Howells 	return ret;
1671260a9803SDavid Howells }
1672260a9803SDavid Howells 
1673260a9803SDavid Howells /*
1674260a9803SDavid Howells  * rename a file in an AFS filesystem and/or move it between directories
1675260a9803SDavid Howells  */
1676260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
16771cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
16781cd66c93SMiklos Szeredi 		      unsigned int flags)
1679260a9803SDavid Howells {
1680d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1681a58823acSDavid Howells 	struct afs_status_cb *scb;
1682260a9803SDavid Howells 	struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
168379ddbfa5SDavid Howells 	struct dentry *tmp = NULL, *rehash = NULL;
168479ddbfa5SDavid Howells 	struct inode *new_inode;
1685260a9803SDavid Howells 	struct key *key;
168663a4681fSDavid Howells 	bool new_negative = d_is_negative(new_dentry);
1687260a9803SDavid Howells 	int ret;
1688260a9803SDavid Howells 
16891cd66c93SMiklos Szeredi 	if (flags)
16901cd66c93SMiklos Szeredi 		return -EINVAL;
16911cd66c93SMiklos Szeredi 
169279ddbfa5SDavid Howells 	/* Don't allow silly-rename files be moved around. */
169379ddbfa5SDavid Howells 	if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
169479ddbfa5SDavid Howells 		return -EINVAL;
169579ddbfa5SDavid Howells 
16962b0143b5SDavid Howells 	vnode = AFS_FS_I(d_inode(old_dentry));
1697260a9803SDavid Howells 	orig_dvnode = AFS_FS_I(old_dir);
1698260a9803SDavid Howells 	new_dvnode = AFS_FS_I(new_dir);
1699260a9803SDavid Howells 
17003b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1701260a9803SDavid Howells 	       orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1702260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1703260a9803SDavid Howells 	       new_dvnode->fid.vid, new_dvnode->fid.vnode,
1704a455589fSAl Viro 	       new_dentry);
1705260a9803SDavid Howells 
1706a58823acSDavid Howells 	ret = -ENOMEM;
1707a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1708a58823acSDavid Howells 	if (!scb)
1709a58823acSDavid Howells 		goto error;
1710a58823acSDavid Howells 
1711260a9803SDavid Howells 	key = afs_request_key(orig_dvnode->volume->cell);
1712260a9803SDavid Howells 	if (IS_ERR(key)) {
1713260a9803SDavid Howells 		ret = PTR_ERR(key);
1714a58823acSDavid Howells 		goto error_scb;
1715260a9803SDavid Howells 	}
1716260a9803SDavid Howells 
171779ddbfa5SDavid Howells 	/* For non-directories, check whether the target is busy and if so,
171879ddbfa5SDavid Howells 	 * make a copy of the dentry and then do a silly-rename.  If the
171979ddbfa5SDavid Howells 	 * silly-rename succeeds, the copied dentry is hashed and becomes the
172079ddbfa5SDavid Howells 	 * new target.
172179ddbfa5SDavid Howells 	 */
172279ddbfa5SDavid Howells 	if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
172379ddbfa5SDavid Howells 		/* To prevent any new references to the target during the
172479ddbfa5SDavid Howells 		 * rename, we unhash the dentry in advance.
172579ddbfa5SDavid Howells 		 */
172679ddbfa5SDavid Howells 		if (!d_unhashed(new_dentry)) {
172779ddbfa5SDavid Howells 			d_drop(new_dentry);
172879ddbfa5SDavid Howells 			rehash = new_dentry;
172979ddbfa5SDavid Howells 		}
173079ddbfa5SDavid Howells 
173179ddbfa5SDavid Howells 		if (d_count(new_dentry) > 2) {
173279ddbfa5SDavid Howells 			/* copy the target dentry's name */
173379ddbfa5SDavid Howells 			ret = -ENOMEM;
173479ddbfa5SDavid Howells 			tmp = d_alloc(new_dentry->d_parent,
173579ddbfa5SDavid Howells 				      &new_dentry->d_name);
173679ddbfa5SDavid Howells 			if (!tmp)
173779ddbfa5SDavid Howells 				goto error_rehash;
173879ddbfa5SDavid Howells 
173979ddbfa5SDavid Howells 			ret = afs_sillyrename(new_dvnode,
174079ddbfa5SDavid Howells 					      AFS_FS_I(d_inode(new_dentry)),
174179ddbfa5SDavid Howells 					      new_dentry, key);
174279ddbfa5SDavid Howells 			if (ret)
174379ddbfa5SDavid Howells 				goto error_rehash;
174479ddbfa5SDavid Howells 
174579ddbfa5SDavid Howells 			new_dentry = tmp;
174679ddbfa5SDavid Howells 			rehash = NULL;
174779ddbfa5SDavid Howells 			new_negative = true;
174879ddbfa5SDavid Howells 		}
174979ddbfa5SDavid Howells 	}
175079ddbfa5SDavid Howells 
1751d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
175220b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) {
1753a58823acSDavid Howells 		afs_dataversion_t orig_data_version;
1754a58823acSDavid Howells 		afs_dataversion_t new_data_version;
1755a58823acSDavid Howells 		struct afs_status_cb *new_scb = &scb[1];
1756a58823acSDavid Howells 
1757a58823acSDavid Howells 		orig_data_version = orig_dvnode->status.data_version + 1;
1758a58823acSDavid Howells 
1759d2ddc776SDavid Howells 		if (orig_dvnode != new_dvnode) {
1760d2ddc776SDavid Howells 			if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1761d2ddc776SDavid Howells 				afs_end_vnode_operation(&fc);
176279ddbfa5SDavid Howells 				goto error_rehash;
1763d2ddc776SDavid Howells 			}
1764a58823acSDavid Howells 			new_data_version = new_dvnode->status.data_version;
1765a58823acSDavid Howells 		} else {
1766a58823acSDavid Howells 			new_data_version = orig_data_version;
1767a58823acSDavid Howells 			new_scb = &scb[0];
1768d2ddc776SDavid Howells 		}
1769a58823acSDavid Howells 
1770d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
177168251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
177268251f0aSDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
1773d2ddc776SDavid Howells 			afs_fs_rename(&fc, old_dentry->d_name.name,
177463a4681fSDavid Howells 				      new_dvnode, new_dentry->d_name.name,
1775a58823acSDavid Howells 				      &scb[0], new_scb);
1776d2ddc776SDavid Howells 		}
1777d2ddc776SDavid Howells 
1778a58823acSDavid Howells 		afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break,
1779a58823acSDavid Howells 					&orig_data_version, &scb[0]);
1780a58823acSDavid Howells 		if (new_dvnode != orig_dvnode) {
1781a58823acSDavid Howells 			afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2,
1782a58823acSDavid Howells 						&new_data_version, &scb[1]);
1783d2ddc776SDavid Howells 			mutex_unlock(&new_dvnode->io_lock);
1784a58823acSDavid Howells 		}
1785d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1786260a9803SDavid Howells 		if (ret < 0)
178779ddbfa5SDavid Howells 			goto error_rehash;
1788d2ddc776SDavid Howells 	}
1789d2ddc776SDavid Howells 
179063a4681fSDavid Howells 	if (ret == 0) {
179179ddbfa5SDavid Howells 		if (rehash)
179279ddbfa5SDavid Howells 			d_rehash(rehash);
179363a4681fSDavid Howells 		if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
179463a4681fSDavid Howells 		    afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
179579ddbfa5SDavid Howells 					afs_edit_dir_for_rename_0);
179663a4681fSDavid Howells 
179763a4681fSDavid Howells 		if (!new_negative &&
179863a4681fSDavid Howells 		    test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
179963a4681fSDavid Howells 			afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
180079ddbfa5SDavid Howells 					    afs_edit_dir_for_rename_1);
180163a4681fSDavid Howells 
180263a4681fSDavid Howells 		if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
180363a4681fSDavid Howells 			afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
180479ddbfa5SDavid Howells 					 &vnode->fid, afs_edit_dir_for_rename_2);
180579ddbfa5SDavid Howells 
180679ddbfa5SDavid Howells 		new_inode = d_inode(new_dentry);
180779ddbfa5SDavid Howells 		if (new_inode) {
180879ddbfa5SDavid Howells 			spin_lock(&new_inode->i_lock);
180979ddbfa5SDavid Howells 			if (new_inode->i_nlink > 0)
181079ddbfa5SDavid Howells 				drop_nlink(new_inode);
181179ddbfa5SDavid Howells 			spin_unlock(&new_inode->i_lock);
181279ddbfa5SDavid Howells 		}
181379ddbfa5SDavid Howells 		d_move(old_dentry, new_dentry);
181479ddbfa5SDavid Howells 		goto error_tmp;
181563a4681fSDavid Howells 	}
181663a4681fSDavid Howells 
181779ddbfa5SDavid Howells error_rehash:
181879ddbfa5SDavid Howells 	if (rehash)
181979ddbfa5SDavid Howells 		d_rehash(rehash);
182079ddbfa5SDavid Howells error_tmp:
182179ddbfa5SDavid Howells 	if (tmp)
182279ddbfa5SDavid Howells 		dput(tmp);
1823260a9803SDavid Howells 	key_put(key);
1824a58823acSDavid Howells error_scb:
1825a58823acSDavid Howells 	kfree(scb);
1826260a9803SDavid Howells error:
1827260a9803SDavid Howells 	_leave(" = %d", ret);
1828260a9803SDavid Howells 	return ret;
1829260a9803SDavid Howells }
1830f3ddee8dSDavid Howells 
1831f3ddee8dSDavid Howells /*
1832f3ddee8dSDavid Howells  * Release a directory page and clean up its private state if it's not busy
1833f3ddee8dSDavid Howells  * - return true if the page can now be released, false if not
1834f3ddee8dSDavid Howells  */
1835f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1836f3ddee8dSDavid Howells {
1837f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1838f3ddee8dSDavid Howells 
18393b6492dfSDavid Howells 	_enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1840f3ddee8dSDavid Howells 
1841f3ddee8dSDavid Howells 	set_page_private(page, 0);
1842f3ddee8dSDavid Howells 	ClearPagePrivate(page);
1843f3ddee8dSDavid Howells 
1844f3ddee8dSDavid Howells 	/* The directory will need reloading. */
1845f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1846f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_relpg);
1847f3ddee8dSDavid Howells 	return 1;
1848f3ddee8dSDavid Howells }
1849f3ddee8dSDavid Howells 
1850f3ddee8dSDavid Howells /*
1851f3ddee8dSDavid Howells  * invalidate part or all of a page
1852f3ddee8dSDavid Howells  * - release a page and clean up its private data if offset is 0 (indicating
1853f3ddee8dSDavid Howells  *   the entire page)
1854f3ddee8dSDavid Howells  */
1855f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1856f3ddee8dSDavid Howells 				   unsigned int length)
1857f3ddee8dSDavid Howells {
1858f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1859f3ddee8dSDavid Howells 
1860f3ddee8dSDavid Howells 	_enter("{%lu},%u,%u", page->index, offset, length);
1861f3ddee8dSDavid Howells 
1862f3ddee8dSDavid Howells 	BUG_ON(!PageLocked(page));
1863f3ddee8dSDavid Howells 
1864f3ddee8dSDavid Howells 	/* The directory will need reloading. */
1865f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1866f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_inval);
1867f3ddee8dSDavid Howells 
1868f3ddee8dSDavid Howells 	/* we clean up only if the entire page is being invalidated */
1869f3ddee8dSDavid Howells 	if (offset == 0 && length == PAGE_SIZE) {
1870f3ddee8dSDavid Howells 		set_page_private(page, 0);
1871f3ddee8dSDavid Howells 		ClearPagePrivate(page);
1872f3ddee8dSDavid Howells 	}
1873f3ddee8dSDavid Howells }
1874