xref: /openbmc/linux/fs/afs/dir.c (revision a38a7558)
11da177e4SLinus Torvalds /* dir.c: AFS filesystem directory handling
21da177e4SLinus Torvalds  *
3f3ddee8dSDavid Howells  * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
41da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
1434286d66SNick Piggin #include <linux/namei.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
16f3ddee8dSDavid Howells #include <linux/swap.h>
1700d3b7a4SDavid Howells #include <linux/ctype.h>
18e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
19f3ddee8dSDavid Howells #include <linux/task_io_accounting_ops.h>
201da177e4SLinus Torvalds #include "internal.h"
21a58823acSDavid Howells #include "afs_fs.h"
224ea219a8SDavid Howells #include "xdr_fs.h"
231da177e4SLinus Torvalds 
24260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
2500cd8dd3SAl Viro 				 unsigned int flags);
261da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file);
271bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx);
280b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
29fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry);
3079ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode);
315cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
325cf9dd55SDavid Howells 				  loff_t fpos, u64 ino, unsigned dtype);
33ac7576f4SMiklos Szeredi static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
34afefdbb2SDavid Howells 			      loff_t fpos, u64 ino, unsigned dtype);
354acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
36ebfc3b49SAl Viro 		      bool excl);
3718bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
38260a9803SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry);
39260a9803SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry);
40260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
41260a9803SDavid Howells 		    struct dentry *dentry);
42260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
43260a9803SDavid Howells 		       const char *content);
44260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
451cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
461cd66c93SMiklos Szeredi 		      unsigned int flags);
47f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
48f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
49f3ddee8dSDavid Howells 				   unsigned int length);
50f3ddee8dSDavid Howells 
51f3ddee8dSDavid Howells static int afs_dir_set_page_dirty(struct page *page)
52f3ddee8dSDavid Howells {
53f3ddee8dSDavid Howells 	BUG(); /* This should never happen. */
54f3ddee8dSDavid Howells }
551da177e4SLinus Torvalds 
564b6f5d20SArjan van de Ven const struct file_operations afs_dir_file_operations = {
571da177e4SLinus Torvalds 	.open		= afs_dir_open,
5800d3b7a4SDavid Howells 	.release	= afs_release,
5929884effSAl Viro 	.iterate_shared	= afs_readdir,
60e8d6c554SDavid Howells 	.lock		= afs_lock,
613222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
64754661f1SArjan van de Ven const struct inode_operations afs_dir_inode_operations = {
65260a9803SDavid Howells 	.create		= afs_create,
66260a9803SDavid Howells 	.lookup		= afs_lookup,
67260a9803SDavid Howells 	.link		= afs_link,
68260a9803SDavid Howells 	.unlink		= afs_unlink,
69260a9803SDavid Howells 	.symlink	= afs_symlink,
70260a9803SDavid Howells 	.mkdir		= afs_mkdir,
71260a9803SDavid Howells 	.rmdir		= afs_rmdir,
722773bf00SMiklos Szeredi 	.rename		= afs_rename,
7300d3b7a4SDavid Howells 	.permission	= afs_permission,
74416351f2SDavid Howells 	.getattr	= afs_getattr,
7531143d5dSDavid Howells 	.setattr	= afs_setattr,
76d3e3b7eaSDavid Howells 	.listxattr	= afs_listxattr,
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
79f3ddee8dSDavid Howells const struct address_space_operations afs_dir_aops = {
80f3ddee8dSDavid Howells 	.set_page_dirty	= afs_dir_set_page_dirty,
81f3ddee8dSDavid Howells 	.releasepage	= afs_dir_releasepage,
82f3ddee8dSDavid Howells 	.invalidatepage	= afs_dir_invalidatepage,
83f3ddee8dSDavid Howells };
84f3ddee8dSDavid Howells 
85d61dcce2SAl Viro const struct dentry_operations afs_fs_dentry_operations = {
861da177e4SLinus Torvalds 	.d_revalidate	= afs_d_revalidate,
871da177e4SLinus Torvalds 	.d_delete	= afs_d_delete,
88260a9803SDavid Howells 	.d_release	= afs_d_release,
89d18610b0SDavid Howells 	.d_automount	= afs_d_automount,
9079ddbfa5SDavid Howells 	.d_iput		= afs_d_iput,
911da177e4SLinus Torvalds };
921da177e4SLinus Torvalds 
935cf9dd55SDavid Howells struct afs_lookup_one_cookie {
945cf9dd55SDavid Howells 	struct dir_context	ctx;
955cf9dd55SDavid Howells 	struct qstr		name;
965cf9dd55SDavid Howells 	bool			found;
975cf9dd55SDavid Howells 	struct afs_fid		fid;
985cf9dd55SDavid Howells };
995cf9dd55SDavid Howells 
100260a9803SDavid Howells struct afs_lookup_cookie {
1011bbae9f8SAl Viro 	struct dir_context	ctx;
1021bbae9f8SAl Viro 	struct qstr		name;
1035cf9dd55SDavid Howells 	bool			found;
1045cf9dd55SDavid Howells 	bool			one_only;
1055cf9dd55SDavid Howells 	unsigned short		nr_fids;
10687182759SDavid Howells 	struct afs_status_cb	*statuses;
1075cf9dd55SDavid Howells 	struct afs_fid		fids[50];
1081da177e4SLinus Torvalds };
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * check that a directory page is valid
1121da177e4SLinus Torvalds  */
113f3ddee8dSDavid Howells static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
114f3ddee8dSDavid Howells 			       loff_t i_size)
1151da177e4SLinus Torvalds {
11600317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
117f3ddee8dSDavid Howells 	loff_t latter, off;
1181da177e4SLinus Torvalds 	int tmp, qty;
1191da177e4SLinus Torvalds 
120dab17c1aSDavid Howells 	/* Determine how many magic numbers there should be in this page, but
121dab17c1aSDavid Howells 	 * we must take care because the directory may change size under us.
122dab17c1aSDavid Howells 	 */
123dab17c1aSDavid Howells 	off = page_offset(page);
124dab17c1aSDavid Howells 	if (i_size <= off)
125dab17c1aSDavid Howells 		goto checked;
126dab17c1aSDavid Howells 
127dab17c1aSDavid Howells 	latter = i_size - off;
1281da177e4SLinus Torvalds 	if (latter >= PAGE_SIZE)
1291da177e4SLinus Torvalds 		qty = PAGE_SIZE;
1301da177e4SLinus Torvalds 	else
1311da177e4SLinus Torvalds 		qty = latter;
13200317636SDavid Howells 	qty /= sizeof(union afs_xdr_dir_block);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	/* check them */
13563a4681fSDavid Howells 	dbuf = kmap(page);
1361da177e4SLinus Torvalds 	for (tmp = 0; tmp < qty; tmp++) {
13700317636SDavid Howells 		if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
138dab17c1aSDavid Howells 			printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
139f3ddee8dSDavid Howells 			       __func__, dvnode->vfs_inode.i_ino, tmp, qty,
14000317636SDavid Howells 			       ntohs(dbuf->blocks[tmp].hdr.magic));
141f3ddee8dSDavid Howells 			trace_afs_dir_check_failed(dvnode, off, i_size);
14263a4681fSDavid Howells 			kunmap(page);
143f51375cdSDavid Howells 			trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
1441da177e4SLinus Torvalds 			goto error;
1451da177e4SLinus Torvalds 		}
14663a4681fSDavid Howells 
14763a4681fSDavid Howells 		/* Make sure each block is NUL terminated so we can reasonably
14863a4681fSDavid Howells 		 * use string functions on it.  The filenames in the page
14963a4681fSDavid Howells 		 * *should* be NUL-terminated anyway.
15063a4681fSDavid Howells 		 */
15163a4681fSDavid Howells 		((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 
15463a4681fSDavid Howells 	kunmap(page);
15563a4681fSDavid Howells 
156dab17c1aSDavid Howells checked:
157f3ddee8dSDavid Howells 	afs_stat_v(dvnode, n_read_dir);
158be5b82dbSAl Viro 	return true;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds error:
161be5b82dbSAl Viro 	return false;
162ec26815aSDavid Howells }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /*
165445b1028SDavid Howells  * Check the contents of a directory that we've just read.
166445b1028SDavid Howells  */
167445b1028SDavid Howells static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
168445b1028SDavid Howells {
169445b1028SDavid Howells 	struct afs_xdr_dir_page *dbuf;
170445b1028SDavid Howells 	unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
171445b1028SDavid Howells 
172445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++)
173445b1028SDavid Howells 		if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
174445b1028SDavid Howells 			goto bad;
175445b1028SDavid Howells 	return true;
176445b1028SDavid Howells 
177445b1028SDavid Howells bad:
178445b1028SDavid Howells 	pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
179445b1028SDavid Howells 		dvnode->fid.vid, dvnode->fid.vnode,
180445b1028SDavid Howells 		req->file_size, req->len, req->actual_len, req->remain);
181445b1028SDavid Howells 	pr_warn("DIR %llx %x %x %x\n",
182445b1028SDavid Howells 		req->pos, req->index, req->nr_pages, req->offset);
183445b1028SDavid Howells 
184445b1028SDavid Howells 	for (i = 0; i < req->nr_pages; i++) {
185445b1028SDavid Howells 		dbuf = kmap(req->pages[i]);
186445b1028SDavid Howells 		for (j = 0; j < qty; j++) {
187445b1028SDavid Howells 			union afs_xdr_dir_block *block = &dbuf->blocks[j];
188445b1028SDavid Howells 
189445b1028SDavid Howells 			pr_warn("[%02x] %32phN\n", i * qty + j, block);
190445b1028SDavid Howells 		}
191445b1028SDavid Howells 		kunmap(req->pages[i]);
192445b1028SDavid Howells 	}
193445b1028SDavid Howells 	return false;
194445b1028SDavid Howells }
195445b1028SDavid Howells 
196445b1028SDavid Howells /*
1971da177e4SLinus Torvalds  * open an AFS directory file
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	_enter("{%lu}", inode->i_ino);
2021da177e4SLinus Torvalds 
20300317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
20400317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
2051da177e4SLinus Torvalds 
20608e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
2071da177e4SLinus Torvalds 		return -ENOENT;
2081da177e4SLinus Torvalds 
20900d3b7a4SDavid Howells 	return afs_open(inode, file);
210ec26815aSDavid Howells }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /*
213f3ddee8dSDavid Howells  * Read the directory into the pagecache in one go, scrubbing the previous
214f3ddee8dSDavid Howells  * contents.  The list of pages is returned, pinning them so that they don't
215f3ddee8dSDavid Howells  * get reclaimed during the iteration.
216f3ddee8dSDavid Howells  */
217f3ddee8dSDavid Howells static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
218b61f7dcfSDavid Howells 	__acquires(&dvnode->validate_lock)
219f3ddee8dSDavid Howells {
220f3ddee8dSDavid Howells 	struct afs_read *req;
221f3ddee8dSDavid Howells 	loff_t i_size;
222f3ddee8dSDavid Howells 	int nr_pages, nr_inline, i, n;
223f3ddee8dSDavid Howells 	int ret = -ENOMEM;
224f3ddee8dSDavid Howells 
225f3ddee8dSDavid Howells retry:
226f3ddee8dSDavid Howells 	i_size = i_size_read(&dvnode->vfs_inode);
227f3ddee8dSDavid Howells 	if (i_size < 2048)
228f51375cdSDavid Howells 		return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
229f51375cdSDavid Howells 	if (i_size > 2048 * 1024) {
230f51375cdSDavid Howells 		trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
231f3ddee8dSDavid Howells 		return ERR_PTR(-EFBIG);
232f51375cdSDavid Howells 	}
233f3ddee8dSDavid Howells 
234f3ddee8dSDavid Howells 	_enter("%llu", i_size);
235f3ddee8dSDavid Howells 
236f3ddee8dSDavid Howells 	/* Get a request record to hold the page list.  We want to hold it
237f3ddee8dSDavid Howells 	 * inline if we can, but we don't want to make an order 1 allocation.
238f3ddee8dSDavid Howells 	 */
239f3ddee8dSDavid Howells 	nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
240f3ddee8dSDavid Howells 	nr_inline = nr_pages;
241f3ddee8dSDavid Howells 	if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
242f3ddee8dSDavid Howells 		nr_inline = 0;
243f3ddee8dSDavid Howells 
244f3ddee8dSDavid Howells 	req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
245f3ddee8dSDavid Howells 		      GFP_KERNEL);
246f3ddee8dSDavid Howells 	if (!req)
247f3ddee8dSDavid Howells 		return ERR_PTR(-ENOMEM);
248f3ddee8dSDavid Howells 
249f3ddee8dSDavid Howells 	refcount_set(&req->usage, 1);
250f3ddee8dSDavid Howells 	req->nr_pages = nr_pages;
251f3ddee8dSDavid Howells 	req->actual_len = i_size; /* May change */
252f3ddee8dSDavid Howells 	req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
253f3ddee8dSDavid Howells 	req->data_version = dvnode->status.data_version; /* May change */
254f3ddee8dSDavid Howells 	if (nr_inline > 0) {
255f3ddee8dSDavid Howells 		req->pages = req->array;
256f3ddee8dSDavid Howells 	} else {
257f3ddee8dSDavid Howells 		req->pages = kcalloc(nr_pages, sizeof(struct page *),
258f3ddee8dSDavid Howells 				     GFP_KERNEL);
259f3ddee8dSDavid Howells 		if (!req->pages)
260f3ddee8dSDavid Howells 			goto error;
261f3ddee8dSDavid Howells 	}
262f3ddee8dSDavid Howells 
263f3ddee8dSDavid Howells 	/* Get a list of all the pages that hold or will hold the directory
264f3ddee8dSDavid Howells 	 * content.  We need to fill in any gaps that we might find where the
265f3ddee8dSDavid Howells 	 * memory reclaimer has been at work.  If there are any gaps, we will
266f3ddee8dSDavid Howells 	 * need to reread the entire directory contents.
267f3ddee8dSDavid Howells 	 */
268f3ddee8dSDavid Howells 	i = 0;
269f3ddee8dSDavid Howells 	do {
270f3ddee8dSDavid Howells 		n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
271f3ddee8dSDavid Howells 					  req->nr_pages - i,
272f3ddee8dSDavid Howells 					  req->pages + i);
273f3ddee8dSDavid Howells 		_debug("find %u at %u/%u", n, i, req->nr_pages);
274f3ddee8dSDavid Howells 		if (n == 0) {
275f3ddee8dSDavid Howells 			gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
276f3ddee8dSDavid Howells 
277f3ddee8dSDavid Howells 			if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
278f3ddee8dSDavid Howells 				afs_stat_v(dvnode, n_inval);
279f3ddee8dSDavid Howells 
280f3ddee8dSDavid Howells 			ret = -ENOMEM;
281f3ddee8dSDavid Howells 			req->pages[i] = __page_cache_alloc(gfp);
282f3ddee8dSDavid Howells 			if (!req->pages[i])
283f3ddee8dSDavid Howells 				goto error;
284f3ddee8dSDavid Howells 			ret = add_to_page_cache_lru(req->pages[i],
285f3ddee8dSDavid Howells 						    dvnode->vfs_inode.i_mapping,
286f3ddee8dSDavid Howells 						    i, gfp);
287f3ddee8dSDavid Howells 			if (ret < 0)
288f3ddee8dSDavid Howells 				goto error;
289f3ddee8dSDavid Howells 
290f3ddee8dSDavid Howells 			set_page_private(req->pages[i], 1);
291f3ddee8dSDavid Howells 			SetPagePrivate(req->pages[i]);
292f3ddee8dSDavid Howells 			unlock_page(req->pages[i]);
293f3ddee8dSDavid Howells 			i++;
294f3ddee8dSDavid Howells 		} else {
295f3ddee8dSDavid Howells 			i += n;
296f3ddee8dSDavid Howells 		}
297f3ddee8dSDavid Howells 	} while (i < req->nr_pages);
298f3ddee8dSDavid Howells 
299f3ddee8dSDavid Howells 	/* If we're going to reload, we need to lock all the pages to prevent
300f3ddee8dSDavid Howells 	 * races.
301f3ddee8dSDavid Howells 	 */
302f3ddee8dSDavid Howells 	ret = -ERESTARTSYS;
303b61f7dcfSDavid Howells 	if (down_read_killable(&dvnode->validate_lock) < 0)
304b61f7dcfSDavid Howells 		goto error;
305f3ddee8dSDavid Howells 
306f3ddee8dSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
307f3ddee8dSDavid Howells 		goto success;
308f3ddee8dSDavid Howells 
309b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
310b61f7dcfSDavid Howells 	if (down_write_killable(&dvnode->validate_lock) < 0)
311b61f7dcfSDavid Howells 		goto error;
312b61f7dcfSDavid Howells 
313b61f7dcfSDavid Howells 	if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
31499987c56SDavid Howells 		trace_afs_reload_dir(dvnode);
315f3ddee8dSDavid Howells 		ret = afs_fetch_data(dvnode, key, req);
316f3ddee8dSDavid Howells 		if (ret < 0)
317b61f7dcfSDavid Howells 			goto error_unlock;
318f3ddee8dSDavid Howells 
319f3ddee8dSDavid Howells 		task_io_account_read(PAGE_SIZE * req->nr_pages);
320f3ddee8dSDavid Howells 
321f3ddee8dSDavid Howells 		if (req->len < req->file_size)
322f3ddee8dSDavid Howells 			goto content_has_grown;
323f3ddee8dSDavid Howells 
324f3ddee8dSDavid Howells 		/* Validate the data we just read. */
325f3ddee8dSDavid Howells 		ret = -EIO;
326445b1028SDavid Howells 		if (!afs_dir_check_pages(dvnode, req))
327b61f7dcfSDavid Howells 			goto error_unlock;
328f3ddee8dSDavid Howells 
329f3ddee8dSDavid Howells 		// TODO: Trim excess pages
330f3ddee8dSDavid Howells 
331f3ddee8dSDavid Howells 		set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
332f3ddee8dSDavid Howells 	}
333f3ddee8dSDavid Howells 
334b61f7dcfSDavid Howells 	downgrade_write(&dvnode->validate_lock);
335f3ddee8dSDavid Howells success:
336f3ddee8dSDavid Howells 	return req;
337f3ddee8dSDavid Howells 
338f3ddee8dSDavid Howells error_unlock:
339b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
340f3ddee8dSDavid Howells error:
341f3ddee8dSDavid Howells 	afs_put_read(req);
342f3ddee8dSDavid Howells 	_leave(" = %d", ret);
343f3ddee8dSDavid Howells 	return ERR_PTR(ret);
344f3ddee8dSDavid Howells 
345f3ddee8dSDavid Howells content_has_grown:
346b61f7dcfSDavid Howells 	up_write(&dvnode->validate_lock);
347f3ddee8dSDavid Howells 	afs_put_read(req);
348f3ddee8dSDavid Howells 	goto retry;
349f3ddee8dSDavid Howells }
350f3ddee8dSDavid Howells 
351f3ddee8dSDavid Howells /*
3521da177e4SLinus Torvalds  * deal with one block in an AFS directory
3531da177e4SLinus Torvalds  */
354f51375cdSDavid Howells static int afs_dir_iterate_block(struct afs_vnode *dvnode,
355f51375cdSDavid Howells 				 struct dir_context *ctx,
35600317636SDavid Howells 				 union afs_xdr_dir_block *block,
3571bbae9f8SAl Viro 				 unsigned blkoff)
3581da177e4SLinus Torvalds {
35900317636SDavid Howells 	union afs_xdr_dirent *dire;
3601da177e4SLinus Torvalds 	unsigned offset, next, curr;
3611da177e4SLinus Torvalds 	size_t nlen;
3621bbae9f8SAl Viro 	int tmp;
3631da177e4SLinus Torvalds 
3641bbae9f8SAl Viro 	_enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
3651da177e4SLinus Torvalds 
36600317636SDavid Howells 	curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	/* walk through the block, an entry at a time */
3694ea219a8SDavid Howells 	for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
3704ea219a8SDavid Howells 	     offset < AFS_DIR_SLOTS_PER_BLOCK;
3711da177e4SLinus Torvalds 	     offset = next
3721da177e4SLinus Torvalds 	     ) {
3731da177e4SLinus Torvalds 		next = offset + 1;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 		/* skip entries marked unused in the bitmap */
37600317636SDavid Howells 		if (!(block->hdr.bitmap[offset / 8] &
3771da177e4SLinus Torvalds 		      (1 << (offset % 8)))) {
3785b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: unused",
37900317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block), offset);
3801da177e4SLinus Torvalds 			if (offset >= curr)
3811bbae9f8SAl Viro 				ctx->pos = blkoff +
38200317636SDavid Howells 					next * sizeof(union afs_xdr_dirent);
3831da177e4SLinus Torvalds 			continue;
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		/* got a valid entry */
3871da177e4SLinus Torvalds 		dire = &block->dirents[offset];
3881da177e4SLinus Torvalds 		nlen = strnlen(dire->u.name,
3891da177e4SLinus Torvalds 			       sizeof(*block) -
39000317636SDavid Howells 			       offset * sizeof(union afs_xdr_dirent));
3911da177e4SLinus Torvalds 
3925b5e0928SAlexey Dobriyan 		_debug("ENT[%zu.%u]: %s %zu \"%s\"",
39300317636SDavid Howells 		       blkoff / sizeof(union afs_xdr_dir_block), offset,
3941da177e4SLinus Torvalds 		       (offset < curr ? "skip" : "fill"),
3951da177e4SLinus Torvalds 		       nlen, dire->u.name);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 		/* work out where the next possible entry is */
39800317636SDavid Howells 		for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
3994ea219a8SDavid Howells 			if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
4005b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
4011da177e4SLinus Torvalds 				       " %u travelled beyond end dir block"
4025b5e0928SAlexey Dobriyan 				       " (len %u/%zu)",
40300317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4041da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
405f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_over_end);
4061da177e4SLinus Torvalds 			}
40700317636SDavid Howells 			if (!(block->hdr.bitmap[next / 8] &
4081da177e4SLinus Torvalds 			      (1 << (next % 8)))) {
4095b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
4105b5e0928SAlexey Dobriyan 				       " %u unmarked extension (len %u/%zu)",
41100317636SDavid Howells 				       blkoff / sizeof(union afs_xdr_dir_block),
4121da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
413f51375cdSDavid Howells 				return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
4141da177e4SLinus Torvalds 			}
4151da177e4SLinus Torvalds 
4165b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: ext %u/%zu",
41700317636SDavid Howells 			       blkoff / sizeof(union afs_xdr_dir_block),
4181da177e4SLinus Torvalds 			       next, tmp, nlen);
4191da177e4SLinus Torvalds 			next++;
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 		/* skip if starts before the current position */
4231da177e4SLinus Torvalds 		if (offset < curr)
4241da177e4SLinus Torvalds 			continue;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 		/* found the next entry */
4271bbae9f8SAl Viro 		if (!dir_emit(ctx, dire->u.name, nlen,
4281da177e4SLinus Torvalds 			      ntohl(dire->u.vnode),
4295cf9dd55SDavid Howells 			      (ctx->actor == afs_lookup_filldir ||
4305cf9dd55SDavid Howells 			       ctx->actor == afs_lookup_one_filldir)?
4311bbae9f8SAl Viro 			      ntohl(dire->u.unique) : DT_UNKNOWN)) {
4321da177e4SLinus Torvalds 			_leave(" = 0 [full]");
4331da177e4SLinus Torvalds 			return 0;
4341da177e4SLinus Torvalds 		}
4351da177e4SLinus Torvalds 
43600317636SDavid Howells 		ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	_leave(" = 1 [more]");
4401da177e4SLinus Torvalds 	return 1;
441ec26815aSDavid Howells }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds /*
44408e0e7c8SDavid Howells  * iterate through the data blob that lists the contents of an AFS directory
4451da177e4SLinus Torvalds  */
4461bbae9f8SAl Viro static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
4471bbae9f8SAl Viro 			   struct key *key)
4481da177e4SLinus Torvalds {
449f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
45000317636SDavid Howells 	struct afs_xdr_dir_page *dbuf;
45100317636SDavid Howells 	union afs_xdr_dir_block *dblock;
452f3ddee8dSDavid Howells 	struct afs_read *req;
4531da177e4SLinus Torvalds 	struct page *page;
4541da177e4SLinus Torvalds 	unsigned blkoff, limit;
4551da177e4SLinus Torvalds 	int ret;
4561da177e4SLinus Torvalds 
4571bbae9f8SAl Viro 	_enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
4581da177e4SLinus Torvalds 
45908e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
4601da177e4SLinus Torvalds 		_leave(" = -ESTALE");
4611da177e4SLinus Torvalds 		return -ESTALE;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
464f3ddee8dSDavid Howells 	req = afs_read_dir(dvnode, key);
465f3ddee8dSDavid Howells 	if (IS_ERR(req))
466f3ddee8dSDavid Howells 		return PTR_ERR(req);
467f3ddee8dSDavid Howells 
4681da177e4SLinus Torvalds 	/* round the file position up to the next entry boundary */
46900317636SDavid Howells 	ctx->pos += sizeof(union afs_xdr_dirent) - 1;
47000317636SDavid Howells 	ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	/* walk through the blocks in sequence */
4731da177e4SLinus Torvalds 	ret = 0;
474f3ddee8dSDavid Howells 	while (ctx->pos < req->actual_len) {
47500317636SDavid Howells 		blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
4761da177e4SLinus Torvalds 
477f3ddee8dSDavid Howells 		/* Fetch the appropriate page from the directory and re-add it
478f3ddee8dSDavid Howells 		 * to the LRU.
479f3ddee8dSDavid Howells 		 */
480f3ddee8dSDavid Howells 		page = req->pages[blkoff / PAGE_SIZE];
481f3ddee8dSDavid Howells 		if (!page) {
482f51375cdSDavid Howells 			ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
4831da177e4SLinus Torvalds 			break;
4841da177e4SLinus Torvalds 		}
485f3ddee8dSDavid Howells 		mark_page_accessed(page);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 		limit = blkoff & ~(PAGE_SIZE - 1);
4881da177e4SLinus Torvalds 
489f3ddee8dSDavid Howells 		dbuf = kmap(page);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 		/* deal with the individual blocks stashed on this page */
4921da177e4SLinus Torvalds 		do {
4931da177e4SLinus Torvalds 			dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
49400317636SDavid Howells 					       sizeof(union afs_xdr_dir_block)];
495f51375cdSDavid Howells 			ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
4961da177e4SLinus Torvalds 			if (ret != 1) {
497f3ddee8dSDavid Howells 				kunmap(page);
4981da177e4SLinus Torvalds 				goto out;
4991da177e4SLinus Torvalds 			}
5001da177e4SLinus Torvalds 
50100317636SDavid Howells 			blkoff += sizeof(union afs_xdr_dir_block);
5021da177e4SLinus Torvalds 
5031bbae9f8SAl Viro 		} while (ctx->pos < dir->i_size && blkoff < limit);
5041da177e4SLinus Torvalds 
505f3ddee8dSDavid Howells 		kunmap(page);
5061da177e4SLinus Torvalds 		ret = 0;
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds out:
510b61f7dcfSDavid Howells 	up_read(&dvnode->validate_lock);
511f3ddee8dSDavid Howells 	afs_put_read(req);
5121da177e4SLinus Torvalds 	_leave(" = %d", ret);
5131da177e4SLinus Torvalds 	return ret;
514ec26815aSDavid Howells }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds /*
5171da177e4SLinus Torvalds  * read an AFS directory
5181da177e4SLinus Torvalds  */
5191bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx)
5201da177e4SLinus Torvalds {
521215804a9SDavid Howells 	return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
522ec26815aSDavid Howells }
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds /*
5255cf9dd55SDavid Howells  * Search the directory for a single name
5261da177e4SLinus Torvalds  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5271da177e4SLinus Torvalds  *   uniquifier through dtype
5281da177e4SLinus Torvalds  */
5295cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
530ac7576f4SMiklos Szeredi 				  int nlen, loff_t fpos, u64 ino, unsigned dtype)
5311da177e4SLinus Torvalds {
5325cf9dd55SDavid Howells 	struct afs_lookup_one_cookie *cookie =
5335cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_one_cookie, ctx);
5341da177e4SLinus Torvalds 
5351bbae9f8SAl Viro 	_enter("{%s,%u},%s,%u,,%llu,%u",
5361bbae9f8SAl Viro 	       cookie->name.name, cookie->name.len, name, nlen,
537ba3e0e1aSDavid S. Miller 	       (unsigned long long) ino, dtype);
5381da177e4SLinus Torvalds 
53908e0e7c8SDavid Howells 	/* insanity checks first */
54000317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
54100317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
54208e0e7c8SDavid Howells 
5431bbae9f8SAl Viro 	if (cookie->name.len != nlen ||
5441bbae9f8SAl Viro 	    memcmp(cookie->name.name, name, nlen) != 0) {
5451da177e4SLinus Torvalds 		_leave(" = 0 [no]");
5461da177e4SLinus Torvalds 		return 0;
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	cookie->fid.vnode = ino;
5501da177e4SLinus Torvalds 	cookie->fid.unique = dtype;
5511da177e4SLinus Torvalds 	cookie->found = 1;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	_leave(" = -1 [found]");
5541da177e4SLinus Torvalds 	return -1;
555ec26815aSDavid Howells }
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /*
5585cf9dd55SDavid Howells  * Do a lookup of a single name in a directory
559260a9803SDavid Howells  * - just returns the FID the dentry name maps to if found
5601da177e4SLinus Torvalds  */
5615cf9dd55SDavid Howells static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
56200d3b7a4SDavid Howells 			     struct afs_fid *fid, struct key *key)
5631da177e4SLinus Torvalds {
5641bbae9f8SAl Viro 	struct afs_super_info *as = dir->i_sb->s_fs_info;
5655cf9dd55SDavid Howells 	struct afs_lookup_one_cookie cookie = {
5665cf9dd55SDavid Howells 		.ctx.actor = afs_lookup_one_filldir,
5671bbae9f8SAl Viro 		.name = dentry->d_name,
5681bbae9f8SAl Viro 		.fid.vid = as->volume->vid
5691bbae9f8SAl Viro 	};
5701da177e4SLinus Torvalds 	int ret;
5711da177e4SLinus Torvalds 
572a455589fSAl Viro 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	/* search the directory */
5751bbae9f8SAl Viro 	ret = afs_dir_iterate(dir, &cookie.ctx, key);
5761da177e4SLinus Torvalds 	if (ret < 0) {
57708e0e7c8SDavid Howells 		_leave(" = %d [iter]", ret);
57808e0e7c8SDavid Howells 		return ret;
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	ret = -ENOENT;
5821da177e4SLinus Torvalds 	if (!cookie.found) {
58308e0e7c8SDavid Howells 		_leave(" = -ENOENT [not found]");
58408e0e7c8SDavid Howells 		return -ENOENT;
58508e0e7c8SDavid Howells 	}
58608e0e7c8SDavid Howells 
58708e0e7c8SDavid Howells 	*fid = cookie.fid;
5883b6492dfSDavid Howells 	_leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
58908e0e7c8SDavid Howells 	return 0;
59008e0e7c8SDavid Howells }
59108e0e7c8SDavid Howells 
59208e0e7c8SDavid Howells /*
5935cf9dd55SDavid Howells  * search the directory for a name
5945cf9dd55SDavid Howells  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5955cf9dd55SDavid Howells  *   uniquifier through dtype
5965cf9dd55SDavid Howells  */
5975cf9dd55SDavid Howells static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
5985cf9dd55SDavid Howells 			      int nlen, loff_t fpos, u64 ino, unsigned dtype)
5995cf9dd55SDavid Howells {
6005cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie =
6015cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_cookie, ctx);
6025cf9dd55SDavid Howells 	int ret;
6035cf9dd55SDavid Howells 
6045cf9dd55SDavid Howells 	_enter("{%s,%u},%s,%u,,%llu,%u",
6055cf9dd55SDavid Howells 	       cookie->name.name, cookie->name.len, name, nlen,
6065cf9dd55SDavid Howells 	       (unsigned long long) ino, dtype);
6075cf9dd55SDavid Howells 
6085cf9dd55SDavid Howells 	/* insanity checks first */
60900317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
61000317636SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
6115cf9dd55SDavid Howells 
6125cf9dd55SDavid Howells 	if (cookie->found) {
6135cf9dd55SDavid Howells 		if (cookie->nr_fids < 50) {
6145cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].vnode	= ino;
6155cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].unique	= dtype;
6165cf9dd55SDavid Howells 			cookie->nr_fids++;
6175cf9dd55SDavid Howells 		}
6185cf9dd55SDavid Howells 	} else if (cookie->name.len == nlen &&
6195cf9dd55SDavid Howells 		   memcmp(cookie->name.name, name, nlen) == 0) {
6205cf9dd55SDavid Howells 		cookie->fids[0].vnode	= ino;
6215cf9dd55SDavid Howells 		cookie->fids[0].unique	= dtype;
6225cf9dd55SDavid Howells 		cookie->found = 1;
6235cf9dd55SDavid Howells 		if (cookie->one_only)
6245cf9dd55SDavid Howells 			return -1;
6255cf9dd55SDavid Howells 	}
6265cf9dd55SDavid Howells 
6275cf9dd55SDavid Howells 	ret = cookie->nr_fids >= 50 ? -1 : 0;
6285cf9dd55SDavid Howells 	_leave(" = %d", ret);
6295cf9dd55SDavid Howells 	return ret;
6305cf9dd55SDavid Howells }
6315cf9dd55SDavid Howells 
6325cf9dd55SDavid Howells /*
6335cf9dd55SDavid Howells  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
6345cf9dd55SDavid Howells  * files in one go and create inodes for them.  The inode of the file we were
6355cf9dd55SDavid Howells  * asked for is returned.
6365cf9dd55SDavid Howells  */
6375cf9dd55SDavid Howells static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
6385cf9dd55SDavid Howells 				   struct key *key)
6395cf9dd55SDavid Howells {
6405cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie;
641f642404aSDavid Howells 	struct afs_cb_interest *dcbi, *cbi = NULL;
6425cf9dd55SDavid Howells 	struct afs_super_info *as = dir->i_sb->s_fs_info;
64387182759SDavid Howells 	struct afs_status_cb *scb;
6445cf9dd55SDavid Howells 	struct afs_iget_data data;
6455cf9dd55SDavid Howells 	struct afs_fs_cursor fc;
646f642404aSDavid Howells 	struct afs_server *server;
6475cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
6485cf9dd55SDavid Howells 	struct inode *inode = NULL;
6495cf9dd55SDavid Howells 	int ret, i;
6505cf9dd55SDavid Howells 
6515cf9dd55SDavid Howells 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
6525cf9dd55SDavid Howells 
6535cf9dd55SDavid Howells 	cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
6545cf9dd55SDavid Howells 	if (!cookie)
6555cf9dd55SDavid Howells 		return ERR_PTR(-ENOMEM);
6565cf9dd55SDavid Howells 
6575cf9dd55SDavid Howells 	cookie->ctx.actor = afs_lookup_filldir;
6585cf9dd55SDavid Howells 	cookie->name = dentry->d_name;
6595cf9dd55SDavid Howells 	cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
6605cf9dd55SDavid Howells 
6615cf9dd55SDavid Howells 	read_seqlock_excl(&dvnode->cb_lock);
662f642404aSDavid Howells 	dcbi = rcu_dereference_protected(dvnode->cb_interest,
663f642404aSDavid Howells 					 lockdep_is_held(&dvnode->cb_lock.lock));
664f642404aSDavid Howells 	if (dcbi) {
665f642404aSDavid Howells 		server = dcbi->server;
666f642404aSDavid Howells 		if (server &&
667f642404aSDavid Howells 		    test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
6685cf9dd55SDavid Howells 			cookie->one_only = true;
669f642404aSDavid Howells 	}
6705cf9dd55SDavid Howells 	read_sequnlock_excl(&dvnode->cb_lock);
6715cf9dd55SDavid Howells 
6725cf9dd55SDavid Howells 	for (i = 0; i < 50; i++)
6735cf9dd55SDavid Howells 		cookie->fids[i].vid = as->volume->vid;
6745cf9dd55SDavid Howells 
6755cf9dd55SDavid Howells 	/* search the directory */
6765cf9dd55SDavid Howells 	ret = afs_dir_iterate(dir, &cookie->ctx, key);
6775cf9dd55SDavid Howells 	if (ret < 0) {
6785cf9dd55SDavid Howells 		inode = ERR_PTR(ret);
6795cf9dd55SDavid Howells 		goto out;
6805cf9dd55SDavid Howells 	}
6815cf9dd55SDavid Howells 
6825cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOENT);
6835cf9dd55SDavid Howells 	if (!cookie->found)
6845cf9dd55SDavid Howells 		goto out;
6855cf9dd55SDavid Howells 
6865cf9dd55SDavid Howells 	/* Check to see if we already have an inode for the primary fid. */
6875cf9dd55SDavid Howells 	data.volume = dvnode->volume;
6885cf9dd55SDavid Howells 	data.fid = cookie->fids[0];
6895cf9dd55SDavid Howells 	inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
6905cf9dd55SDavid Howells 	if (inode)
6915cf9dd55SDavid Howells 		goto out;
6925cf9dd55SDavid Howells 
6935cf9dd55SDavid Howells 	/* Need space for examining all the selected files */
6945cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOMEM);
69587182759SDavid Howells 	cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb),
6965cf9dd55SDavid Howells 				    GFP_KERNEL);
6975cf9dd55SDavid Howells 	if (!cookie->statuses)
6985cf9dd55SDavid Howells 		goto out;
6995cf9dd55SDavid Howells 
7005cf9dd55SDavid Howells 	/* Try FS.InlineBulkStatus first.  Abort codes for the individual
7015cf9dd55SDavid Howells 	 * lookups contained therein are stored in the reply without aborting
7025cf9dd55SDavid Howells 	 * the whole operation.
7035cf9dd55SDavid Howells 	 */
7045cf9dd55SDavid Howells 	if (cookie->one_only)
7055cf9dd55SDavid Howells 		goto no_inline_bulk_status;
7065cf9dd55SDavid Howells 
7075cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
70820b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
7095cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
7105cf9dd55SDavid Howells 			if (test_bit(AFS_SERVER_FL_NO_IBULK,
7115cf9dd55SDavid Howells 				      &fc.cbi->server->flags)) {
7125cf9dd55SDavid Howells 				fc.ac.abort_code = RX_INVALID_OPERATION;
7135cf9dd55SDavid Howells 				fc.ac.error = -ECONNABORTED;
7145cf9dd55SDavid Howells 				break;
7155cf9dd55SDavid Howells 			}
7165cf9dd55SDavid Howells 			afs_fs_inline_bulk_status(&fc,
7175cf9dd55SDavid Howells 						  afs_v2net(dvnode),
7185cf9dd55SDavid Howells 						  cookie->fids,
7195cf9dd55SDavid Howells 						  cookie->statuses,
7205cf9dd55SDavid Howells 						  cookie->nr_fids, NULL);
7215cf9dd55SDavid Howells 		}
7225cf9dd55SDavid Howells 
7235cf9dd55SDavid Howells 		if (fc.ac.error == 0)
7245cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
7255cf9dd55SDavid Howells 		if (fc.ac.abort_code == RX_INVALID_OPERATION)
7265cf9dd55SDavid Howells 			set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
7275cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
7285cf9dd55SDavid Howells 	}
7295cf9dd55SDavid Howells 
7305cf9dd55SDavid Howells 	if (!IS_ERR(inode))
7315cf9dd55SDavid Howells 		goto success;
7325cf9dd55SDavid Howells 	if (fc.ac.abort_code != RX_INVALID_OPERATION)
7335cf9dd55SDavid Howells 		goto out_c;
7345cf9dd55SDavid Howells 
7355cf9dd55SDavid Howells no_inline_bulk_status:
7365cf9dd55SDavid Howells 	/* We could try FS.BulkStatus next, but this aborts the entire op if
7375cf9dd55SDavid Howells 	 * any of the lookups fails - so, for the moment, revert to
7385cf9dd55SDavid Howells 	 * FS.FetchStatus for just the primary fid.
7395cf9dd55SDavid Howells 	 */
7405cf9dd55SDavid Howells 	cookie->nr_fids = 1;
7415cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
74220b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
7435cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
74487182759SDavid Howells 			scb = &cookie->statuses[0];
7455cf9dd55SDavid Howells 			afs_fs_fetch_status(&fc,
7465cf9dd55SDavid Howells 					    afs_v2net(dvnode),
7475cf9dd55SDavid Howells 					    cookie->fids,
748a58823acSDavid Howells 					    scb,
7495cf9dd55SDavid Howells 					    NULL);
7505cf9dd55SDavid Howells 		}
7515cf9dd55SDavid Howells 
7525cf9dd55SDavid Howells 		if (fc.ac.error == 0)
7535cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
7545cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
7555cf9dd55SDavid Howells 	}
7565cf9dd55SDavid Howells 
7575cf9dd55SDavid Howells 	if (IS_ERR(inode))
7585cf9dd55SDavid Howells 		goto out_c;
7595cf9dd55SDavid Howells 
7605cf9dd55SDavid Howells 	for (i = 0; i < cookie->nr_fids; i++)
76187182759SDavid Howells 		cookie->statuses[i].status.abort_code = 0;
7625cf9dd55SDavid Howells 
7635cf9dd55SDavid Howells success:
7645cf9dd55SDavid Howells 	/* Turn all the files into inodes and save the first one - which is the
7655cf9dd55SDavid Howells 	 * one we actually want.
7665cf9dd55SDavid Howells 	 */
76787182759SDavid Howells 	scb = &cookie->statuses[0];
76887182759SDavid Howells 	if (scb->status.abort_code != 0)
76987182759SDavid Howells 		inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code));
7705cf9dd55SDavid Howells 
7715cf9dd55SDavid Howells 	for (i = 0; i < cookie->nr_fids; i++) {
77287182759SDavid Howells 		struct afs_status_cb *scb = &cookie->statuses[i];
7735cf9dd55SDavid Howells 		struct inode *ti;
7745cf9dd55SDavid Howells 
77587182759SDavid Howells 		if (scb->status.abort_code != 0)
7765cf9dd55SDavid Howells 			continue;
7775cf9dd55SDavid Howells 
7785cf9dd55SDavid Howells 		ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
779a58823acSDavid Howells 			      scb, cbi, dvnode);
7805cf9dd55SDavid Howells 		if (i == 0) {
7815cf9dd55SDavid Howells 			inode = ti;
7825cf9dd55SDavid Howells 		} else {
7835cf9dd55SDavid Howells 			if (!IS_ERR(ti))
7845cf9dd55SDavid Howells 				iput(ti);
7855cf9dd55SDavid Howells 		}
7865cf9dd55SDavid Howells 	}
7875cf9dd55SDavid Howells 
7885cf9dd55SDavid Howells out_c:
7895cf9dd55SDavid Howells 	afs_put_cb_interest(afs_v2net(dvnode), cbi);
79087182759SDavid Howells 	kvfree(cookie->statuses);
7915cf9dd55SDavid Howells out:
7925cf9dd55SDavid Howells 	kfree(cookie);
7935cf9dd55SDavid Howells 	return inode;
7945cf9dd55SDavid Howells }
7955cf9dd55SDavid Howells 
7965cf9dd55SDavid Howells /*
7976f8880d8SDavid Howells  * Look up an entry in a directory with @sys substitution.
7986f8880d8SDavid Howells  */
7996f8880d8SDavid Howells static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
8006f8880d8SDavid Howells 				       struct key *key)
8016f8880d8SDavid Howells {
8026f8880d8SDavid Howells 	struct afs_sysnames *subs;
8036f8880d8SDavid Howells 	struct afs_net *net = afs_i2net(dir);
8046f8880d8SDavid Howells 	struct dentry *ret;
8056f8880d8SDavid Howells 	char *buf, *p, *name;
8066f8880d8SDavid Howells 	int len, i;
8076f8880d8SDavid Howells 
8086f8880d8SDavid Howells 	_enter("");
8096f8880d8SDavid Howells 
8106f8880d8SDavid Howells 	ret = ERR_PTR(-ENOMEM);
8116f8880d8SDavid Howells 	p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
8126f8880d8SDavid Howells 	if (!buf)
8136f8880d8SDavid Howells 		goto out_p;
8146f8880d8SDavid Howells 	if (dentry->d_name.len > 4) {
8156f8880d8SDavid Howells 		memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
8166f8880d8SDavid Howells 		p += dentry->d_name.len - 4;
8176f8880d8SDavid Howells 	}
8186f8880d8SDavid Howells 
8196f8880d8SDavid Howells 	/* There is an ordered list of substitutes that we have to try. */
8206f8880d8SDavid Howells 	read_lock(&net->sysnames_lock);
8216f8880d8SDavid Howells 	subs = net->sysnames;
8226f8880d8SDavid Howells 	refcount_inc(&subs->usage);
8236f8880d8SDavid Howells 	read_unlock(&net->sysnames_lock);
8246f8880d8SDavid Howells 
8256f8880d8SDavid Howells 	for (i = 0; i < subs->nr; i++) {
8266f8880d8SDavid Howells 		name = subs->subs[i];
8276f8880d8SDavid Howells 		len = dentry->d_name.len - 4 + strlen(name);
8286f8880d8SDavid Howells 		if (len >= AFSNAMEMAX) {
8296f8880d8SDavid Howells 			ret = ERR_PTR(-ENAMETOOLONG);
8306f8880d8SDavid Howells 			goto out_s;
8316f8880d8SDavid Howells 		}
8326f8880d8SDavid Howells 
8336f8880d8SDavid Howells 		strcpy(p, name);
8346f8880d8SDavid Howells 		ret = lookup_one_len(buf, dentry->d_parent, len);
8356f8880d8SDavid Howells 		if (IS_ERR(ret) || d_is_positive(ret))
8366f8880d8SDavid Howells 			goto out_s;
8376f8880d8SDavid Howells 		dput(ret);
8386f8880d8SDavid Howells 	}
8396f8880d8SDavid Howells 
8406f8880d8SDavid Howells 	/* We don't want to d_add() the @sys dentry here as we don't want to
8416f8880d8SDavid Howells 	 * the cached dentry to hide changes to the sysnames list.
8426f8880d8SDavid Howells 	 */
8436f8880d8SDavid Howells 	ret = NULL;
8446f8880d8SDavid Howells out_s:
8456f8880d8SDavid Howells 	afs_put_sysnames(subs);
8466f8880d8SDavid Howells 	kfree(buf);
8476f8880d8SDavid Howells out_p:
8486f8880d8SDavid Howells 	key_put(key);
8496f8880d8SDavid Howells 	return ret;
8506f8880d8SDavid Howells }
8516f8880d8SDavid Howells 
8526f8880d8SDavid Howells /*
85308e0e7c8SDavid Howells  * look up an entry in a directory
85408e0e7c8SDavid Howells  */
855260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
85600cd8dd3SAl Viro 				 unsigned int flags)
85708e0e7c8SDavid Howells {
8585cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
85908e0e7c8SDavid Howells 	struct inode *inode;
86034b2a88fSAl Viro 	struct dentry *d;
86100d3b7a4SDavid Howells 	struct key *key;
86208e0e7c8SDavid Howells 	int ret;
86308e0e7c8SDavid Howells 
8643b6492dfSDavid Howells 	_enter("{%llx:%llu},%p{%pd},",
8655cf9dd55SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
866260a9803SDavid Howells 
8672b0143b5SDavid Howells 	ASSERTCMP(d_inode(dentry), ==, NULL);
86808e0e7c8SDavid Howells 
86945222b9eSDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX) {
87008e0e7c8SDavid Howells 		_leave(" = -ENAMETOOLONG");
87108e0e7c8SDavid Howells 		return ERR_PTR(-ENAMETOOLONG);
87208e0e7c8SDavid Howells 	}
87308e0e7c8SDavid Howells 
8745cf9dd55SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
87508e0e7c8SDavid Howells 		_leave(" = -ESTALE");
87608e0e7c8SDavid Howells 		return ERR_PTR(-ESTALE);
87708e0e7c8SDavid Howells 	}
87808e0e7c8SDavid Howells 
8795cf9dd55SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
88000d3b7a4SDavid Howells 	if (IS_ERR(key)) {
88100d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
882e231c2eeSDavid Howells 		return ERR_CAST(key);
88300d3b7a4SDavid Howells 	}
88400d3b7a4SDavid Howells 
8855cf9dd55SDavid Howells 	ret = afs_validate(dvnode, key);
88608e0e7c8SDavid Howells 	if (ret < 0) {
88700d3b7a4SDavid Howells 		key_put(key);
888260a9803SDavid Howells 		_leave(" = %d [val]", ret);
8891da177e4SLinus Torvalds 		return ERR_PTR(ret);
8901da177e4SLinus Torvalds 	}
8911da177e4SLinus Torvalds 
8926f8880d8SDavid Howells 	if (dentry->d_name.len >= 4 &&
8936f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
8946f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
8956f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
8966f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 1] == 's')
8976f8880d8SDavid Howells 		return afs_lookup_atsys(dir, dentry, key);
8986f8880d8SDavid Howells 
899d55b4da4SDavid Howells 	afs_stat_v(dvnode, n_lookup);
9005cf9dd55SDavid Howells 	inode = afs_do_lookup(dir, dentry, key);
90134b2a88fSAl Viro 	key_put(key);
90234b2a88fSAl Viro 	if (inode == ERR_PTR(-ENOENT)) {
9035cf9dd55SDavid Howells 		inode = afs_try_auto_mntpt(dentry, dir);
90434b2a88fSAl Viro 	} else {
90534b2a88fSAl Viro 		dentry->d_fsdata =
90634b2a88fSAl Viro 			(void *)(unsigned long)dvnode->status.data_version;
907bec5eb61Swanglei 	}
90834b2a88fSAl Viro 	d = d_splice_alias(inode, dentry);
90980548b03SDavid Howells 	if (!IS_ERR_OR_NULL(d)) {
91034b2a88fSAl Viro 		d->d_fsdata = dentry->d_fsdata;
91180548b03SDavid Howells 		trace_afs_lookup(dvnode, &d->d_name,
91280548b03SDavid Howells 				 inode ? AFS_FS_I(inode) : NULL);
91380548b03SDavid Howells 	} else {
91480548b03SDavid Howells 		trace_afs_lookup(dvnode, &dentry->d_name,
91580548b03SDavid Howells 				 inode ? AFS_FS_I(inode) : NULL);
91680548b03SDavid Howells 	}
91734b2a88fSAl Viro 	return d;
918ec26815aSDavid Howells }
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds /*
9211da177e4SLinus Torvalds  * check that a dentry lookup hit has found a valid entry
9221da177e4SLinus Torvalds  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
9231da177e4SLinus Torvalds  *   inode
9241da177e4SLinus Torvalds  */
9250b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
9261da177e4SLinus Torvalds {
927260a9803SDavid Howells 	struct afs_vnode *vnode, *dir;
928dd0d9a46SArtem Bityutskiy 	struct afs_fid uninitialized_var(fid);
9291da177e4SLinus Torvalds 	struct dentry *parent;
930c435ee34SDavid Howells 	struct inode *inode;
93100d3b7a4SDavid Howells 	struct key *key;
932a4ff7401SDavid Howells 	long dir_version, de_version;
9331da177e4SLinus Torvalds 	int ret;
9341da177e4SLinus Torvalds 
9350b728e19SAl Viro 	if (flags & LOOKUP_RCU)
93634286d66SNick Piggin 		return -ECHILD;
93734286d66SNick Piggin 
938c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
9392b0143b5SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
9403b6492dfSDavid Howells 		_enter("{v={%llx:%llu} n=%pd fl=%lx},",
941a455589fSAl Viro 		       vnode->fid.vid, vnode->fid.vnode, dentry,
942260a9803SDavid Howells 		       vnode->flags);
943c435ee34SDavid Howells 	} else {
944a455589fSAl Viro 		_enter("{neg n=%pd}", dentry);
945c435ee34SDavid Howells 	}
9461da177e4SLinus Torvalds 
947260a9803SDavid Howells 	key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
94800d3b7a4SDavid Howells 	if (IS_ERR(key))
94900d3b7a4SDavid Howells 		key = NULL;
95000d3b7a4SDavid Howells 
951c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
952c435ee34SDavid Howells 		inode = d_inode(dentry);
953c435ee34SDavid Howells 		if (inode) {
954c435ee34SDavid Howells 			vnode = AFS_FS_I(inode);
955c435ee34SDavid Howells 			afs_validate(vnode, key);
956c435ee34SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
957c435ee34SDavid Howells 				goto out_bad;
958c435ee34SDavid Howells 		}
959c435ee34SDavid Howells 	}
960c435ee34SDavid Howells 
9611da177e4SLinus Torvalds 	/* lock down the parent dentry so we can peer at it */
96208e0e7c8SDavid Howells 	parent = dget_parent(dentry);
9632b0143b5SDavid Howells 	dir = AFS_FS_I(d_inode(parent));
9641da177e4SLinus Torvalds 
965260a9803SDavid Howells 	/* validate the parent directory */
966260a9803SDavid Howells 	afs_validate(dir, key);
967260a9803SDavid Howells 
968260a9803SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
969a455589fSAl Viro 		_debug("%pd: parent dir deleted", dentry);
970c435ee34SDavid Howells 		goto out_bad_parent;
9711da177e4SLinus Torvalds 	}
9721da177e4SLinus Torvalds 
973a4ff7401SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
974a4ff7401SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
975a4ff7401SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
976a4ff7401SDavid Howells 	 * version.
977a4ff7401SDavid Howells 	 */
978a4ff7401SDavid Howells 	dir_version = (long)dir->status.data_version;
979a4ff7401SDavid Howells 	de_version = (long)dentry->d_fsdata;
980a4ff7401SDavid Howells 	if (de_version == dir_version)
981a4ff7401SDavid Howells 		goto out_valid;
982a4ff7401SDavid Howells 
983a4ff7401SDavid Howells 	dir_version = (long)dir->invalid_before;
984a4ff7401SDavid Howells 	if (de_version - dir_version >= 0)
985a4ff7401SDavid Howells 		goto out_valid;
986260a9803SDavid Howells 
98708e0e7c8SDavid Howells 	_debug("dir modified");
988d55b4da4SDavid Howells 	afs_stat_v(dir, n_reval);
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	/* search the directory for this vnode */
9915cf9dd55SDavid Howells 	ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
992260a9803SDavid Howells 	switch (ret) {
993260a9803SDavid Howells 	case 0:
994260a9803SDavid Howells 		/* the filename maps to something */
9952b0143b5SDavid Howells 		if (d_really_is_negative(dentry))
996c435ee34SDavid Howells 			goto out_bad_parent;
997c435ee34SDavid Howells 		inode = d_inode(dentry);
998c435ee34SDavid Howells 		if (is_bad_inode(inode)) {
999a455589fSAl Viro 			printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1000a455589fSAl Viro 			       dentry);
1001c435ee34SDavid Howells 			goto out_bad_parent;
10021da177e4SLinus Torvalds 		}
10031da177e4SLinus Torvalds 
1004c435ee34SDavid Howells 		vnode = AFS_FS_I(inode);
1005c435ee34SDavid Howells 
10061da177e4SLinus Torvalds 		/* if the vnode ID has changed, then the dirent points to a
10071da177e4SLinus Torvalds 		 * different file */
100808e0e7c8SDavid Howells 		if (fid.vnode != vnode->fid.vnode) {
10093b6492dfSDavid Howells 			_debug("%pd: dirent changed [%llu != %llu]",
1010a455589fSAl Viro 			       dentry, fid.vnode,
101108e0e7c8SDavid Howells 			       vnode->fid.vnode);
10121da177e4SLinus Torvalds 			goto not_found;
10131da177e4SLinus Torvalds 		}
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 		/* if the vnode ID uniqifier has changed, then the file has
1016260a9803SDavid Howells 		 * been deleted and replaced, and the original vnode ID has
1017260a9803SDavid Howells 		 * been reused */
101808e0e7c8SDavid Howells 		if (fid.unique != vnode->fid.unique) {
1019a455589fSAl Viro 			_debug("%pd: file deleted (uq %u -> %u I:%u)",
1020a455589fSAl Viro 			       dentry, fid.unique,
10217a224228SJean Noel Cordenner 			       vnode->fid.unique,
1022c435ee34SDavid Howells 			       vnode->vfs_inode.i_generation);
1023c435ee34SDavid Howells 			write_seqlock(&vnode->cb_lock);
102408e0e7c8SDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
1025c435ee34SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1026260a9803SDavid Howells 			goto not_found;
1027260a9803SDavid Howells 		}
1028260a9803SDavid Howells 		goto out_valid;
1029260a9803SDavid Howells 
1030260a9803SDavid Howells 	case -ENOENT:
1031260a9803SDavid Howells 		/* the filename is unknown */
1032a455589fSAl Viro 		_debug("%pd: dirent not found", dentry);
10332b0143b5SDavid Howells 		if (d_really_is_positive(dentry))
1034260a9803SDavid Howells 			goto not_found;
1035260a9803SDavid Howells 		goto out_valid;
1036260a9803SDavid Howells 
1037260a9803SDavid Howells 	default:
1038a455589fSAl Viro 		_debug("failed to iterate dir %pd: %d",
1039a455589fSAl Viro 		       parent, ret);
1040c435ee34SDavid Howells 		goto out_bad_parent;
10411da177e4SLinus Torvalds 	}
104208e0e7c8SDavid Howells 
10431da177e4SLinus Torvalds out_valid:
1044a4ff7401SDavid Howells 	dentry->d_fsdata = (void *)dir_version;
10451da177e4SLinus Torvalds 	dput(parent);
104600d3b7a4SDavid Howells 	key_put(key);
10471da177e4SLinus Torvalds 	_leave(" = 1 [valid]");
10481da177e4SLinus Torvalds 	return 1;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	/* the dirent, if it exists, now points to a different vnode */
10511da177e4SLinus Torvalds not_found:
10521da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
10531da177e4SLinus Torvalds 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
10541da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
10551da177e4SLinus Torvalds 
1056c435ee34SDavid Howells out_bad_parent:
1057a455589fSAl Viro 	_debug("dropping dentry %pd2", dentry);
10581da177e4SLinus Torvalds 	dput(parent);
1059c435ee34SDavid Howells out_bad:
106000d3b7a4SDavid Howells 	key_put(key);
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	_leave(" = 0 [bad]");
10631da177e4SLinus Torvalds 	return 0;
1064ec26815aSDavid Howells }
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds /*
10671da177e4SLinus Torvalds  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
10681da177e4SLinus Torvalds  * sleep)
10691da177e4SLinus Torvalds  * - called from dput() when d_count is going to 0.
10701da177e4SLinus Torvalds  * - return 1 to request dentry be unhashed, 0 otherwise
10711da177e4SLinus Torvalds  */
1072fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry)
10731da177e4SLinus Torvalds {
1074a455589fSAl Viro 	_enter("%pd", dentry);
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
10771da177e4SLinus Torvalds 		goto zap;
10781da177e4SLinus Torvalds 
10792b0143b5SDavid Howells 	if (d_really_is_positive(dentry) &&
10802b0143b5SDavid Howells 	    (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
10812b0143b5SDavid Howells 	     test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
10821da177e4SLinus Torvalds 		goto zap;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	_leave(" = 0 [keep]");
10851da177e4SLinus Torvalds 	return 0;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds zap:
10881da177e4SLinus Torvalds 	_leave(" = 1 [zap]");
10891da177e4SLinus Torvalds 	return 1;
1090ec26815aSDavid Howells }
1091260a9803SDavid Howells 
1092260a9803SDavid Howells /*
109379ddbfa5SDavid Howells  * Clean up sillyrename files on dentry removal.
109479ddbfa5SDavid Howells  */
109579ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode)
109679ddbfa5SDavid Howells {
109779ddbfa5SDavid Howells 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
109879ddbfa5SDavid Howells 		afs_silly_iput(dentry, inode);
109979ddbfa5SDavid Howells 	iput(inode);
110079ddbfa5SDavid Howells }
110179ddbfa5SDavid Howells 
110279ddbfa5SDavid Howells /*
1103260a9803SDavid Howells  * handle dentry release
1104260a9803SDavid Howells  */
110566c7e1d3SDavid Howells void afs_d_release(struct dentry *dentry)
1106260a9803SDavid Howells {
1107a455589fSAl Viro 	_enter("%pd", dentry);
1108260a9803SDavid Howells }
1109260a9803SDavid Howells 
1110260a9803SDavid Howells /*
1111d2ddc776SDavid Howells  * Create a new inode for create/mkdir/symlink
1112d2ddc776SDavid Howells  */
1113d2ddc776SDavid Howells static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1114d2ddc776SDavid Howells 				struct dentry *new_dentry,
1115d2ddc776SDavid Howells 				struct afs_fid *newfid,
1116a58823acSDavid Howells 				struct afs_status_cb *new_scb)
1117d2ddc776SDavid Howells {
11185a813276SDavid Howells 	struct afs_vnode *vnode;
1119d2ddc776SDavid Howells 	struct inode *inode;
1120d2ddc776SDavid Howells 
1121d2ddc776SDavid Howells 	if (fc->ac.error < 0)
1122d2ddc776SDavid Howells 		return;
1123d2ddc776SDavid Howells 
1124d2ddc776SDavid Howells 	inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1125a58823acSDavid Howells 			 newfid, new_scb, fc->cbi, fc->vnode);
1126d2ddc776SDavid Howells 	if (IS_ERR(inode)) {
1127d2ddc776SDavid Howells 		/* ENOMEM or EINTR at a really inconvenient time - just abandon
1128d2ddc776SDavid Howells 		 * the new directory on the server.
1129d2ddc776SDavid Howells 		 */
1130d2ddc776SDavid Howells 		fc->ac.error = PTR_ERR(inode);
1131d2ddc776SDavid Howells 		return;
1132d2ddc776SDavid Howells 	}
1133d2ddc776SDavid Howells 
11345a813276SDavid Howells 	vnode = AFS_FS_I(inode);
11355a813276SDavid Howells 	set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1136a58823acSDavid Howells 	if (fc->ac.error == 0)
1137a58823acSDavid Howells 		afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb);
113873116df7SDavid Howells 	d_instantiate(new_dentry, inode);
1139d2ddc776SDavid Howells }
1140d2ddc776SDavid Howells 
1141d2ddc776SDavid Howells /*
1142260a9803SDavid Howells  * create a directory on an AFS filesystem
1143260a9803SDavid Howells  */
114418bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1145260a9803SDavid Howells {
1146a58823acSDavid Howells 	struct afs_status_cb *scb;
1147d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1148d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1149d2ddc776SDavid Howells 	struct afs_fid newfid;
1150260a9803SDavid Howells 	struct key *key;
1151260a9803SDavid Howells 	int ret;
1152260a9803SDavid Howells 
1153d2ddc776SDavid Howells 	mode |= S_IFDIR;
1154260a9803SDavid Howells 
11553b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho",
1156a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1157260a9803SDavid Howells 
1158a58823acSDavid Howells 	ret = -ENOMEM;
1159a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1160a58823acSDavid Howells 	if (!scb)
1161a58823acSDavid Howells 		goto error;
1162a58823acSDavid Howells 
1163260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1164260a9803SDavid Howells 	if (IS_ERR(key)) {
1165260a9803SDavid Howells 		ret = PTR_ERR(key);
1166a58823acSDavid Howells 		goto error_scb;
1167260a9803SDavid Howells 	}
1168260a9803SDavid Howells 
1169d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
117020b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1171a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1172a58823acSDavid Howells 
1173d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
117468251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1175a58823acSDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1176a58823acSDavid Howells 				      &scb[0], &newfid, &scb[1]);
1177d2ddc776SDavid Howells 		}
1178d2ddc776SDavid Howells 
1179a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1180a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1181a58823acSDavid Howells 					&data_version, &scb[0]);
1182a58823acSDavid Howells 		afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
1183d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1184260a9803SDavid Howells 		if (ret < 0)
1185d2ddc776SDavid Howells 			goto error_key;
11864433b691SDavid Howells 	} else {
11874433b691SDavid Howells 		goto error_key;
1188260a9803SDavid Howells 	}
1189260a9803SDavid Howells 
119063a4681fSDavid Howells 	if (ret == 0 &&
119163a4681fSDavid Howells 	    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
119263a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
119363a4681fSDavid Howells 				 afs_edit_dir_for_create);
119463a4681fSDavid Howells 
1195260a9803SDavid Howells 	key_put(key);
1196a58823acSDavid Howells 	kfree(scb);
1197260a9803SDavid Howells 	_leave(" = 0");
1198260a9803SDavid Howells 	return 0;
1199260a9803SDavid Howells 
1200d2ddc776SDavid Howells error_key:
1201260a9803SDavid Howells 	key_put(key);
1202a58823acSDavid Howells error_scb:
1203a58823acSDavid Howells 	kfree(scb);
1204260a9803SDavid Howells error:
1205260a9803SDavid Howells 	d_drop(dentry);
1206260a9803SDavid Howells 	_leave(" = %d", ret);
1207260a9803SDavid Howells 	return ret;
1208260a9803SDavid Howells }
1209260a9803SDavid Howells 
1210260a9803SDavid Howells /*
1211d2ddc776SDavid Howells  * Remove a subdir from a directory.
1212260a9803SDavid Howells  */
1213d2ddc776SDavid Howells static void afs_dir_remove_subdir(struct dentry *dentry)
1214260a9803SDavid Howells {
12152b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1216d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1217d2ddc776SDavid Howells 
1218260a9803SDavid Howells 		clear_nlink(&vnode->vfs_inode);
1219260a9803SDavid Howells 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1220c435ee34SDavid Howells 		clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
122163a4681fSDavid Howells 		clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1222260a9803SDavid Howells 	}
1223260a9803SDavid Howells }
1224260a9803SDavid Howells 
1225260a9803SDavid Howells /*
1226d2ddc776SDavid Howells  * remove a directory from an AFS filesystem
1227260a9803SDavid Howells  */
1228d2ddc776SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1229260a9803SDavid Howells {
1230a58823acSDavid Howells 	struct afs_status_cb *scb;
1231d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1232f58db83fSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1233260a9803SDavid Howells 	struct key *key;
1234260a9803SDavid Howells 	int ret;
1235260a9803SDavid Howells 
12363b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1237a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1238260a9803SDavid Howells 
1239a58823acSDavid Howells 	scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
1240a58823acSDavid Howells 	if (!scb)
1241a58823acSDavid Howells 		return -ENOMEM;
1242a58823acSDavid Howells 
1243260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1244260a9803SDavid Howells 	if (IS_ERR(key)) {
1245260a9803SDavid Howells 		ret = PTR_ERR(key);
1246260a9803SDavid Howells 		goto error;
1247260a9803SDavid Howells 	}
1248260a9803SDavid Howells 
1249f58db83fSDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1250f58db83fSDavid Howells 	if (d_really_is_positive(dentry)) {
1251f58db83fSDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1252f58db83fSDavid Howells 		ret = afs_validate(vnode, key);
1253f58db83fSDavid Howells 		if (ret < 0)
1254f58db83fSDavid Howells 			goto error_key;
1255f58db83fSDavid Howells 	}
1256f58db83fSDavid Howells 
125779ddbfa5SDavid Howells 	if (vnode) {
125879ddbfa5SDavid Howells 		ret = down_write_killable(&vnode->rmdir_lock);
125979ddbfa5SDavid Howells 		if (ret < 0)
126079ddbfa5SDavid Howells 			goto error_key;
126179ddbfa5SDavid Howells 	}
126279ddbfa5SDavid Howells 
1263d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
126420b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1265a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1266a58823acSDavid Howells 
1267d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
126868251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1269a58823acSDavid Howells 			afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb);
1270260a9803SDavid Howells 		}
1271260a9803SDavid Howells 
1272a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1273a58823acSDavid Howells 					&data_version, scb);
1274d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
127563a4681fSDavid Howells 		if (ret == 0) {
1276d2ddc776SDavid Howells 			afs_dir_remove_subdir(dentry);
127763a4681fSDavid Howells 			if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
127863a4681fSDavid Howells 				afs_edit_dir_remove(dvnode, &dentry->d_name,
127963a4681fSDavid Howells 						    afs_edit_dir_for_rmdir);
128063a4681fSDavid Howells 		}
1281260a9803SDavid Howells 	}
1282260a9803SDavid Howells 
128379ddbfa5SDavid Howells 	if (vnode)
128479ddbfa5SDavid Howells 		up_write(&vnode->rmdir_lock);
1285f58db83fSDavid Howells error_key:
1286260a9803SDavid Howells 	key_put(key);
1287d2ddc776SDavid Howells error:
1288a58823acSDavid Howells 	kfree(scb);
1289d2ddc776SDavid Howells 	return ret;
1290d2ddc776SDavid Howells }
1291260a9803SDavid Howells 
1292d2ddc776SDavid Howells /*
1293d2ddc776SDavid Howells  * Remove a link to a file or symlink from a directory.
1294d2ddc776SDavid Howells  *
1295d2ddc776SDavid Howells  * If the file was not deleted due to excess hard links, the fileserver will
1296d2ddc776SDavid Howells  * break the callback promise on the file - if it had one - before it returns
1297d2ddc776SDavid Howells  * to us, and if it was deleted, it won't
1298d2ddc776SDavid Howells  *
1299d2ddc776SDavid Howells  * However, if we didn't have a callback promise outstanding, or it was
1300d2ddc776SDavid Howells  * outstanding on a different server, then it won't break it either...
1301d2ddc776SDavid Howells  */
1302a38a7558SDavid Howells static int afs_dir_remove_link(struct afs_vnode *dvnode, struct dentry *dentry,
1303a38a7558SDavid Howells 			       struct key *key)
1304d2ddc776SDavid Howells {
1305d2ddc776SDavid Howells 	int ret = 0;
1306d2ddc776SDavid Howells 
1307d2ddc776SDavid Howells 	if (d_really_is_positive(dentry)) {
1308d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1309d2ddc776SDavid Howells 
131030062bd1SDavid Howells 		if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
131130062bd1SDavid Howells 			/* Already done */
1312a38a7558SDavid Howells 		} else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1313a38a7558SDavid Howells 			write_seqlock(&vnode->cb_lock);
1314440fbc3aSDavid Howells 			drop_nlink(&vnode->vfs_inode);
1315440fbc3aSDavid Howells 			if (vnode->vfs_inode.i_nlink == 0) {
1316440fbc3aSDavid Howells 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
1317a38a7558SDavid Howells 				__afs_break_callback(vnode);
1318440fbc3aSDavid Howells 			}
1319a38a7558SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1320440fbc3aSDavid Howells 			ret = 0;
1321440fbc3aSDavid Howells 		} else {
1322a38a7558SDavid Howells 			afs_break_callback(vnode);
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;
134679ddbfa5SDavid Howells 	bool need_rehash = false;
1347d2ddc776SDavid Howells 	int ret;
1348d2ddc776SDavid Howells 
13493b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd}",
1350d2ddc776SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1351d2ddc776SDavid Howells 
1352d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1353d2ddc776SDavid Howells 		return -ENAMETOOLONG;
1354d2ddc776SDavid Howells 
1355a58823acSDavid Howells 	ret = -ENOMEM;
1356a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1357a58823acSDavid Howells 	if (!scb)
1358a58823acSDavid Howells 		goto error;
1359a58823acSDavid Howells 
1360d2ddc776SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1361d2ddc776SDavid Howells 	if (IS_ERR(key)) {
1362d2ddc776SDavid Howells 		ret = PTR_ERR(key);
1363a58823acSDavid Howells 		goto error_scb;
1364d2ddc776SDavid Howells 	}
1365d2ddc776SDavid Howells 
1366d2ddc776SDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1367d2ddc776SDavid Howells 	if (d_really_is_positive(dentry)) {
1368d2ddc776SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1369d2ddc776SDavid Howells 		ret = afs_validate(vnode, key);
1370d2ddc776SDavid Howells 		if (ret < 0)
1371d2ddc776SDavid Howells 			goto error_key;
1372d2ddc776SDavid Howells 	}
1373d2ddc776SDavid Howells 
137479ddbfa5SDavid Howells 	spin_lock(&dentry->d_lock);
137579ddbfa5SDavid Howells 	if (vnode && d_count(dentry) > 1) {
137679ddbfa5SDavid Howells 		spin_unlock(&dentry->d_lock);
137779ddbfa5SDavid Howells 		/* Start asynchronous writeout of the inode */
137879ddbfa5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
137979ddbfa5SDavid Howells 		ret = afs_sillyrename(dvnode, vnode, dentry, key);
138079ddbfa5SDavid Howells 		goto error_key;
138179ddbfa5SDavid Howells 	}
138279ddbfa5SDavid Howells 	if (!d_unhashed(dentry)) {
138379ddbfa5SDavid Howells 		/* Prevent a race with RCU lookup. */
138479ddbfa5SDavid Howells 		__d_drop(dentry);
138579ddbfa5SDavid Howells 		need_rehash = true;
138679ddbfa5SDavid Howells 	}
138779ddbfa5SDavid Howells 	spin_unlock(&dentry->d_lock);
138879ddbfa5SDavid Howells 
1389d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
139020b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1391a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1392a38a7558SDavid Howells 		afs_dataversion_t data_version_2 = vnode->status.data_version;
1393a58823acSDavid Howells 
1394d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
139568251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1396a38a7558SDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
139730062bd1SDavid Howells 
139830062bd1SDavid Howells 			if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
139930062bd1SDavid Howells 			    !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
140030062bd1SDavid Howells 				yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1401a58823acSDavid Howells 						    &scb[0], &scb[1]);
140230062bd1SDavid Howells 				if (fc.ac.error != -ECONNABORTED ||
140330062bd1SDavid Howells 				    fc.ac.abort_code != RXGEN_OPCODE)
140430062bd1SDavid Howells 					continue;
140530062bd1SDavid Howells 				set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
140630062bd1SDavid Howells 			}
140730062bd1SDavid Howells 
1408a58823acSDavid Howells 			afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
1409d2ddc776SDavid Howells 		}
1410d2ddc776SDavid Howells 
1411a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1412a58823acSDavid Howells 					&data_version, &scb[0]);
1413a38a7558SDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1414a38a7558SDavid Howells 					&data_version_2, &scb[1]);
1415d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1416a38a7558SDavid Howells 		if (ret == 0 && !(scb[1].have_status || scb[1].have_error))
1417a38a7558SDavid Howells 			ret = afs_dir_remove_link(dvnode, dentry, key);
141863a4681fSDavid Howells 		if (ret == 0 &&
141963a4681fSDavid Howells 		    test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
142063a4681fSDavid Howells 			afs_edit_dir_remove(dvnode, &dentry->d_name,
142163a4681fSDavid Howells 					    afs_edit_dir_for_unlink);
1422d2ddc776SDavid Howells 	}
1423d2ddc776SDavid Howells 
142479ddbfa5SDavid Howells 	if (need_rehash && ret < 0 && ret != -ENOENT)
142579ddbfa5SDavid Howells 		d_rehash(dentry);
142679ddbfa5SDavid Howells 
1427d2ddc776SDavid Howells error_key:
1428260a9803SDavid Howells 	key_put(key);
1429a58823acSDavid Howells error_scb:
1430a58823acSDavid Howells 	kfree(scb);
1431260a9803SDavid Howells error:
1432260a9803SDavid Howells 	_leave(" = %d", ret);
1433260a9803SDavid Howells 	return ret;
1434260a9803SDavid Howells }
1435260a9803SDavid Howells 
1436260a9803SDavid Howells /*
1437260a9803SDavid Howells  * create a regular file on an AFS filesystem
1438260a9803SDavid Howells  */
14394acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1440ebfc3b49SAl Viro 		      bool excl)
1441260a9803SDavid Howells {
1442d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1443a58823acSDavid Howells 	struct afs_status_cb *scb;
144443dd388bSColin Ian King 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1445d2ddc776SDavid Howells 	struct afs_fid newfid;
1446260a9803SDavid Howells 	struct key *key;
1447260a9803SDavid Howells 	int ret;
1448260a9803SDavid Howells 
1449d2ddc776SDavid Howells 	mode |= S_IFREG;
1450260a9803SDavid Howells 
14513b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%ho,",
1452a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1453260a9803SDavid Howells 
1454d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1455d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1456d2ddc776SDavid Howells 		goto error;
1457d2ddc776SDavid Howells 
1458260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1459260a9803SDavid Howells 	if (IS_ERR(key)) {
1460260a9803SDavid Howells 		ret = PTR_ERR(key);
1461260a9803SDavid Howells 		goto error;
1462260a9803SDavid Howells 	}
1463260a9803SDavid Howells 
1464a58823acSDavid Howells 	ret = -ENOMEM;
1465a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1466a58823acSDavid Howells 	if (!scb)
1467a58823acSDavid Howells 		goto error_scb;
1468a58823acSDavid Howells 
1469d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
147020b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1471a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1472a58823acSDavid Howells 
1473d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
147468251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1475a58823acSDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1476a58823acSDavid Howells 				      &scb[0], &newfid, &scb[1]);
1477d2ddc776SDavid Howells 		}
1478d2ddc776SDavid Howells 
1479a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1480a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1481a58823acSDavid Howells 					&data_version, &scb[0]);
1482a58823acSDavid Howells 		afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
1483d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1484260a9803SDavid Howells 		if (ret < 0)
1485d2ddc776SDavid Howells 			goto error_key;
14864433b691SDavid Howells 	} else {
14874433b691SDavid Howells 		goto error_key;
1488260a9803SDavid Howells 	}
1489260a9803SDavid Howells 
149063a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
149163a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
149263a4681fSDavid Howells 				 afs_edit_dir_for_create);
149363a4681fSDavid Howells 
1494a58823acSDavid Howells 	kfree(scb);
1495260a9803SDavid Howells 	key_put(key);
1496260a9803SDavid Howells 	_leave(" = 0");
1497260a9803SDavid Howells 	return 0;
1498260a9803SDavid Howells 
1499a58823acSDavid Howells error_scb:
1500a58823acSDavid Howells 	kfree(scb);
1501d2ddc776SDavid Howells error_key:
1502260a9803SDavid Howells 	key_put(key);
1503260a9803SDavid Howells error:
1504260a9803SDavid Howells 	d_drop(dentry);
1505260a9803SDavid Howells 	_leave(" = %d", ret);
1506260a9803SDavid Howells 	return ret;
1507260a9803SDavid Howells }
1508260a9803SDavid Howells 
1509260a9803SDavid Howells /*
1510260a9803SDavid Howells  * create a hard link between files in an AFS filesystem
1511260a9803SDavid Howells  */
1512260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
1513260a9803SDavid Howells 		    struct dentry *dentry)
1514260a9803SDavid Howells {
1515d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1516a58823acSDavid Howells 	struct afs_status_cb *scb;
1517a58823acSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1518a58823acSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1519260a9803SDavid Howells 	struct key *key;
1520260a9803SDavid Howells 	int ret;
1521260a9803SDavid Howells 
15223b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%pd}",
1523260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1524260a9803SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode,
1525a455589fSAl Viro 	       dentry);
1526260a9803SDavid Howells 
1527d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1528d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1529d2ddc776SDavid Howells 		goto error;
1530d2ddc776SDavid Howells 
1531a58823acSDavid Howells 	ret = -ENOMEM;
1532a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1533a58823acSDavid Howells 	if (!scb)
1534a58823acSDavid Howells 		goto error;
1535a58823acSDavid Howells 
1536260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1537260a9803SDavid Howells 	if (IS_ERR(key)) {
1538260a9803SDavid Howells 		ret = PTR_ERR(key);
1539a58823acSDavid Howells 		goto error_scb;
1540260a9803SDavid Howells 	}
1541260a9803SDavid Howells 
1542d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
154320b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1544a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1545a58823acSDavid Howells 
1546d2ddc776SDavid Howells 		if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1547d2ddc776SDavid Howells 			afs_end_vnode_operation(&fc);
1548bc1527dcSDavid Howells 			goto error_key;
1549d2ddc776SDavid Howells 		}
1550260a9803SDavid Howells 
1551d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
155268251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
155368251f0aSDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1554a58823acSDavid Howells 			afs_fs_link(&fc, vnode, dentry->d_name.name,
1555a58823acSDavid Howells 				    &scb[0], &scb[1]);
1556d2ddc776SDavid Howells 		}
1557d2ddc776SDavid Howells 
1558a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1559a58823acSDavid Howells 					&data_version, &scb[0]);
1560a58823acSDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1561a58823acSDavid Howells 					NULL, &scb[1]);
15627de9c6eeSAl Viro 		ihold(&vnode->vfs_inode);
1563260a9803SDavid Howells 		d_instantiate(dentry, &vnode->vfs_inode);
1564d2ddc776SDavid Howells 
1565d2ddc776SDavid Howells 		mutex_unlock(&vnode->io_lock);
1566d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1567d2ddc776SDavid Howells 		if (ret < 0)
1568d2ddc776SDavid Howells 			goto error_key;
15694433b691SDavid Howells 	} else {
15704433b691SDavid Howells 		goto error_key;
1571d2ddc776SDavid Howells 	}
1572d2ddc776SDavid Howells 
157363a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
157463a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
157563a4681fSDavid Howells 				 afs_edit_dir_for_link);
157663a4681fSDavid Howells 
1577260a9803SDavid Howells 	key_put(key);
1578a58823acSDavid Howells 	kfree(scb);
1579260a9803SDavid Howells 	_leave(" = 0");
1580260a9803SDavid Howells 	return 0;
1581260a9803SDavid Howells 
1582d2ddc776SDavid Howells error_key:
1583260a9803SDavid Howells 	key_put(key);
1584a58823acSDavid Howells error_scb:
1585a58823acSDavid Howells 	kfree(scb);
1586260a9803SDavid Howells error:
1587260a9803SDavid Howells 	d_drop(dentry);
1588260a9803SDavid Howells 	_leave(" = %d", ret);
1589260a9803SDavid Howells 	return ret;
1590260a9803SDavid Howells }
1591260a9803SDavid Howells 
1592260a9803SDavid Howells /*
1593260a9803SDavid Howells  * create a symlink in an AFS filesystem
1594260a9803SDavid Howells  */
1595260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
1596260a9803SDavid Howells 		       const char *content)
1597260a9803SDavid Howells {
1598d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1599a58823acSDavid Howells 	struct afs_status_cb *scb;
1600d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1601d2ddc776SDavid Howells 	struct afs_fid newfid;
1602260a9803SDavid Howells 	struct key *key;
1603260a9803SDavid Howells 	int ret;
1604260a9803SDavid Howells 
16053b6492dfSDavid Howells 	_enter("{%llx:%llu},{%pd},%s",
1606a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry,
1607260a9803SDavid Howells 	       content);
1608260a9803SDavid Howells 
1609d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1610d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1611d2ddc776SDavid Howells 		goto error;
1612d2ddc776SDavid Howells 
1613260a9803SDavid Howells 	ret = -EINVAL;
161445222b9eSDavid Howells 	if (strlen(content) >= AFSPATHMAX)
1615260a9803SDavid Howells 		goto error;
1616260a9803SDavid Howells 
1617a58823acSDavid Howells 	ret = -ENOMEM;
1618a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1619a58823acSDavid Howells 	if (!scb)
1620a58823acSDavid Howells 		goto error;
1621a58823acSDavid Howells 
1622260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1623260a9803SDavid Howells 	if (IS_ERR(key)) {
1624260a9803SDavid Howells 		ret = PTR_ERR(key);
1625a58823acSDavid Howells 		goto error_scb;
1626260a9803SDavid Howells 	}
1627260a9803SDavid Howells 
1628d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
162920b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1630a58823acSDavid Howells 		afs_dataversion_t data_version = dvnode->status.data_version + 1;
1631a58823acSDavid Howells 
1632d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
163368251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1634a58823acSDavid Howells 			afs_fs_symlink(&fc, dentry->d_name.name, content,
1635a58823acSDavid Howells 				       &scb[0], &newfid, &scb[1]);
1636d2ddc776SDavid Howells 		}
1637d2ddc776SDavid Howells 
1638a58823acSDavid Howells 		afs_check_for_remote_deletion(&fc, dvnode);
1639a58823acSDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1640a58823acSDavid Howells 					&data_version, &scb[0]);
1641a58823acSDavid Howells 		afs_vnode_new_inode(&fc, dentry, &newfid, &scb[1]);
1642d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1643260a9803SDavid Howells 		if (ret < 0)
1644d2ddc776SDavid Howells 			goto error_key;
16454433b691SDavid Howells 	} else {
16464433b691SDavid Howells 		goto error_key;
1647260a9803SDavid Howells 	}
1648260a9803SDavid Howells 
164963a4681fSDavid Howells 	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
165063a4681fSDavid Howells 		afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
165163a4681fSDavid Howells 				 afs_edit_dir_for_symlink);
165263a4681fSDavid Howells 
1653260a9803SDavid Howells 	key_put(key);
1654a58823acSDavid Howells 	kfree(scb);
1655260a9803SDavid Howells 	_leave(" = 0");
1656260a9803SDavid Howells 	return 0;
1657260a9803SDavid Howells 
1658d2ddc776SDavid Howells error_key:
1659260a9803SDavid Howells 	key_put(key);
1660a58823acSDavid Howells error_scb:
1661a58823acSDavid Howells 	kfree(scb);
1662260a9803SDavid Howells error:
1663260a9803SDavid Howells 	d_drop(dentry);
1664260a9803SDavid Howells 	_leave(" = %d", ret);
1665260a9803SDavid Howells 	return ret;
1666260a9803SDavid Howells }
1667260a9803SDavid Howells 
1668260a9803SDavid Howells /*
1669260a9803SDavid Howells  * rename a file in an AFS filesystem and/or move it between directories
1670260a9803SDavid Howells  */
1671260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
16721cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
16731cd66c93SMiklos Szeredi 		      unsigned int flags)
1674260a9803SDavid Howells {
1675d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1676a58823acSDavid Howells 	struct afs_status_cb *scb;
1677260a9803SDavid Howells 	struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
167879ddbfa5SDavid Howells 	struct dentry *tmp = NULL, *rehash = NULL;
167979ddbfa5SDavid Howells 	struct inode *new_inode;
1680260a9803SDavid Howells 	struct key *key;
168163a4681fSDavid Howells 	bool new_negative = d_is_negative(new_dentry);
1682260a9803SDavid Howells 	int ret;
1683260a9803SDavid Howells 
16841cd66c93SMiklos Szeredi 	if (flags)
16851cd66c93SMiklos Szeredi 		return -EINVAL;
16861cd66c93SMiklos Szeredi 
168779ddbfa5SDavid Howells 	/* Don't allow silly-rename files be moved around. */
168879ddbfa5SDavid Howells 	if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
168979ddbfa5SDavid Howells 		return -EINVAL;
169079ddbfa5SDavid Howells 
16912b0143b5SDavid Howells 	vnode = AFS_FS_I(d_inode(old_dentry));
1692260a9803SDavid Howells 	orig_dvnode = AFS_FS_I(old_dir);
1693260a9803SDavid Howells 	new_dvnode = AFS_FS_I(new_dir);
1694260a9803SDavid Howells 
16953b6492dfSDavid Howells 	_enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1696260a9803SDavid Howells 	       orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1697260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1698260a9803SDavid Howells 	       new_dvnode->fid.vid, new_dvnode->fid.vnode,
1699a455589fSAl Viro 	       new_dentry);
1700260a9803SDavid Howells 
1701a58823acSDavid Howells 	ret = -ENOMEM;
1702a58823acSDavid Howells 	scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1703a58823acSDavid Howells 	if (!scb)
1704a58823acSDavid Howells 		goto error;
1705a58823acSDavid Howells 
1706260a9803SDavid Howells 	key = afs_request_key(orig_dvnode->volume->cell);
1707260a9803SDavid Howells 	if (IS_ERR(key)) {
1708260a9803SDavid Howells 		ret = PTR_ERR(key);
1709a58823acSDavid Howells 		goto error_scb;
1710260a9803SDavid Howells 	}
1711260a9803SDavid Howells 
171279ddbfa5SDavid Howells 	/* For non-directories, check whether the target is busy and if so,
171379ddbfa5SDavid Howells 	 * make a copy of the dentry and then do a silly-rename.  If the
171479ddbfa5SDavid Howells 	 * silly-rename succeeds, the copied dentry is hashed and becomes the
171579ddbfa5SDavid Howells 	 * new target.
171679ddbfa5SDavid Howells 	 */
171779ddbfa5SDavid Howells 	if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
171879ddbfa5SDavid Howells 		/* To prevent any new references to the target during the
171979ddbfa5SDavid Howells 		 * rename, we unhash the dentry in advance.
172079ddbfa5SDavid Howells 		 */
172179ddbfa5SDavid Howells 		if (!d_unhashed(new_dentry)) {
172279ddbfa5SDavid Howells 			d_drop(new_dentry);
172379ddbfa5SDavid Howells 			rehash = new_dentry;
172479ddbfa5SDavid Howells 		}
172579ddbfa5SDavid Howells 
172679ddbfa5SDavid Howells 		if (d_count(new_dentry) > 2) {
172779ddbfa5SDavid Howells 			/* copy the target dentry's name */
172879ddbfa5SDavid Howells 			ret = -ENOMEM;
172979ddbfa5SDavid Howells 			tmp = d_alloc(new_dentry->d_parent,
173079ddbfa5SDavid Howells 				      &new_dentry->d_name);
173179ddbfa5SDavid Howells 			if (!tmp)
173279ddbfa5SDavid Howells 				goto error_rehash;
173379ddbfa5SDavid Howells 
173479ddbfa5SDavid Howells 			ret = afs_sillyrename(new_dvnode,
173579ddbfa5SDavid Howells 					      AFS_FS_I(d_inode(new_dentry)),
173679ddbfa5SDavid Howells 					      new_dentry, key);
173779ddbfa5SDavid Howells 			if (ret)
173879ddbfa5SDavid Howells 				goto error_rehash;
173979ddbfa5SDavid Howells 
174079ddbfa5SDavid Howells 			new_dentry = tmp;
174179ddbfa5SDavid Howells 			rehash = NULL;
174279ddbfa5SDavid Howells 			new_negative = true;
174379ddbfa5SDavid Howells 		}
174479ddbfa5SDavid Howells 	}
174579ddbfa5SDavid Howells 
1746d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
174720b8391fSDavid Howells 	if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) {
1748a58823acSDavid Howells 		afs_dataversion_t orig_data_version;
1749a58823acSDavid Howells 		afs_dataversion_t new_data_version;
1750a58823acSDavid Howells 		struct afs_status_cb *new_scb = &scb[1];
1751a58823acSDavid Howells 
1752a58823acSDavid Howells 		orig_data_version = orig_dvnode->status.data_version + 1;
1753a58823acSDavid Howells 
1754d2ddc776SDavid Howells 		if (orig_dvnode != new_dvnode) {
1755d2ddc776SDavid Howells 			if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1756d2ddc776SDavid Howells 				afs_end_vnode_operation(&fc);
175779ddbfa5SDavid Howells 				goto error_rehash;
1758d2ddc776SDavid Howells 			}
1759a58823acSDavid Howells 			new_data_version = new_dvnode->status.data_version;
1760a58823acSDavid Howells 		} else {
1761a58823acSDavid Howells 			new_data_version = orig_data_version;
1762a58823acSDavid Howells 			new_scb = &scb[0];
1763d2ddc776SDavid Howells 		}
1764a58823acSDavid Howells 
1765d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
176668251f0aSDavid Howells 			fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
176768251f0aSDavid Howells 			fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
1768d2ddc776SDavid Howells 			afs_fs_rename(&fc, old_dentry->d_name.name,
176963a4681fSDavid Howells 				      new_dvnode, new_dentry->d_name.name,
1770a58823acSDavid Howells 				      &scb[0], new_scb);
1771d2ddc776SDavid Howells 		}
1772d2ddc776SDavid Howells 
1773a58823acSDavid Howells 		afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break,
1774a58823acSDavid Howells 					&orig_data_version, &scb[0]);
1775a58823acSDavid Howells 		if (new_dvnode != orig_dvnode) {
1776a58823acSDavid Howells 			afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2,
1777a58823acSDavid Howells 						&new_data_version, &scb[1]);
1778d2ddc776SDavid Howells 			mutex_unlock(&new_dvnode->io_lock);
1779a58823acSDavid Howells 		}
1780d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1781260a9803SDavid Howells 		if (ret < 0)
178279ddbfa5SDavid Howells 			goto error_rehash;
1783d2ddc776SDavid Howells 	}
1784d2ddc776SDavid Howells 
178563a4681fSDavid Howells 	if (ret == 0) {
178679ddbfa5SDavid Howells 		if (rehash)
178779ddbfa5SDavid Howells 			d_rehash(rehash);
178863a4681fSDavid Howells 		if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
178963a4681fSDavid Howells 		    afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
179079ddbfa5SDavid Howells 					afs_edit_dir_for_rename_0);
179163a4681fSDavid Howells 
179263a4681fSDavid Howells 		if (!new_negative &&
179363a4681fSDavid Howells 		    test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
179463a4681fSDavid Howells 			afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
179579ddbfa5SDavid Howells 					    afs_edit_dir_for_rename_1);
179663a4681fSDavid Howells 
179763a4681fSDavid Howells 		if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
179863a4681fSDavid Howells 			afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
179979ddbfa5SDavid Howells 					 &vnode->fid, afs_edit_dir_for_rename_2);
180079ddbfa5SDavid Howells 
180179ddbfa5SDavid Howells 		new_inode = d_inode(new_dentry);
180279ddbfa5SDavid Howells 		if (new_inode) {
180379ddbfa5SDavid Howells 			spin_lock(&new_inode->i_lock);
180479ddbfa5SDavid Howells 			if (new_inode->i_nlink > 0)
180579ddbfa5SDavid Howells 				drop_nlink(new_inode);
180679ddbfa5SDavid Howells 			spin_unlock(&new_inode->i_lock);
180779ddbfa5SDavid Howells 		}
180879ddbfa5SDavid Howells 		d_move(old_dentry, new_dentry);
180979ddbfa5SDavid Howells 		goto error_tmp;
181063a4681fSDavid Howells 	}
181163a4681fSDavid Howells 
181279ddbfa5SDavid Howells error_rehash:
181379ddbfa5SDavid Howells 	if (rehash)
181479ddbfa5SDavid Howells 		d_rehash(rehash);
181579ddbfa5SDavid Howells error_tmp:
181679ddbfa5SDavid Howells 	if (tmp)
181779ddbfa5SDavid Howells 		dput(tmp);
1818260a9803SDavid Howells 	key_put(key);
1819a58823acSDavid Howells error_scb:
1820a58823acSDavid Howells 	kfree(scb);
1821260a9803SDavid Howells error:
1822260a9803SDavid Howells 	_leave(" = %d", ret);
1823260a9803SDavid Howells 	return ret;
1824260a9803SDavid Howells }
1825f3ddee8dSDavid Howells 
1826f3ddee8dSDavid Howells /*
1827f3ddee8dSDavid Howells  * Release a directory page and clean up its private state if it's not busy
1828f3ddee8dSDavid Howells  * - return true if the page can now be released, false if not
1829f3ddee8dSDavid Howells  */
1830f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1831f3ddee8dSDavid Howells {
1832f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1833f3ddee8dSDavid Howells 
18343b6492dfSDavid Howells 	_enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1835f3ddee8dSDavid Howells 
1836f3ddee8dSDavid Howells 	set_page_private(page, 0);
1837f3ddee8dSDavid Howells 	ClearPagePrivate(page);
1838f3ddee8dSDavid Howells 
1839f3ddee8dSDavid Howells 	/* The directory will need reloading. */
1840f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1841f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_relpg);
1842f3ddee8dSDavid Howells 	return 1;
1843f3ddee8dSDavid Howells }
1844f3ddee8dSDavid Howells 
1845f3ddee8dSDavid Howells /*
1846f3ddee8dSDavid Howells  * invalidate part or all of a page
1847f3ddee8dSDavid Howells  * - release a page and clean up its private data if offset is 0 (indicating
1848f3ddee8dSDavid Howells  *   the entire page)
1849f3ddee8dSDavid Howells  */
1850f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1851f3ddee8dSDavid Howells 				   unsigned int length)
1852f3ddee8dSDavid Howells {
1853f3ddee8dSDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1854f3ddee8dSDavid Howells 
1855f3ddee8dSDavid Howells 	_enter("{%lu},%u,%u", page->index, offset, length);
1856f3ddee8dSDavid Howells 
1857f3ddee8dSDavid Howells 	BUG_ON(!PageLocked(page));
1858f3ddee8dSDavid Howells 
1859f3ddee8dSDavid Howells 	/* The directory will need reloading. */
1860f3ddee8dSDavid Howells 	if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1861f3ddee8dSDavid Howells 		afs_stat_v(dvnode, n_inval);
1862f3ddee8dSDavid Howells 
1863f3ddee8dSDavid Howells 	/* we clean up only if the entire page is being invalidated */
1864f3ddee8dSDavid Howells 	if (offset == 0 && length == PAGE_SIZE) {
1865f3ddee8dSDavid Howells 		set_page_private(page, 0);
1866f3ddee8dSDavid Howells 		ClearPagePrivate(page);
1867f3ddee8dSDavid Howells 	}
1868f3ddee8dSDavid Howells }
1869