xref: /openbmc/linux/fs/afs/dir.c (revision a4ff7401)
11da177e4SLinus Torvalds /* dir.c: AFS filesystem directory handling
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  * Copyright (C) 2002 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/module.h>
141da177e4SLinus Torvalds #include <linux/init.h>
151da177e4SLinus Torvalds #include <linux/fs.h>
1634286d66SNick Piggin #include <linux/namei.h>
171da177e4SLinus Torvalds #include <linux/pagemap.h>
1800d3b7a4SDavid Howells #include <linux/ctype.h>
19e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
204d673da1SDavid Howells #include <linux/dns_resolver.h>
211da177e4SLinus Torvalds #include "internal.h"
221da177e4SLinus Torvalds 
23260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
2400cd8dd3SAl Viro 				 unsigned int flags);
254d673da1SDavid Howells static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
264d673da1SDavid Howells 					 unsigned int flags);
271da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file);
281bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx);
290b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
30fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry);
31260a9803SDavid Howells static void afs_d_release(struct dentry *dentry);
325cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
335cf9dd55SDavid Howells 				  loff_t fpos, u64 ino, unsigned dtype);
34ac7576f4SMiklos Szeredi static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
35afefdbb2SDavid Howells 			      loff_t fpos, u64 ino, unsigned dtype);
364acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
37ebfc3b49SAl Viro 		      bool excl);
3818bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
39260a9803SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry);
40260a9803SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry);
41260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
42260a9803SDavid Howells 		    struct dentry *dentry);
43260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
44260a9803SDavid Howells 		       const char *content);
45260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
461cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
471cd66c93SMiklos Szeredi 		      unsigned int flags);
481da177e4SLinus Torvalds 
494b6f5d20SArjan van de Ven const struct file_operations afs_dir_file_operations = {
501da177e4SLinus Torvalds 	.open		= afs_dir_open,
5100d3b7a4SDavid Howells 	.release	= afs_release,
5229884effSAl Viro 	.iterate_shared	= afs_readdir,
53e8d6c554SDavid Howells 	.lock		= afs_lock,
543222a3e5SChristoph Hellwig 	.llseek		= generic_file_llseek,
551da177e4SLinus Torvalds };
561da177e4SLinus Torvalds 
57754661f1SArjan van de Ven const struct inode_operations afs_dir_inode_operations = {
58260a9803SDavid Howells 	.create		= afs_create,
59260a9803SDavid Howells 	.lookup		= afs_lookup,
60260a9803SDavid Howells 	.link		= afs_link,
61260a9803SDavid Howells 	.unlink		= afs_unlink,
62260a9803SDavid Howells 	.symlink	= afs_symlink,
63260a9803SDavid Howells 	.mkdir		= afs_mkdir,
64260a9803SDavid Howells 	.rmdir		= afs_rmdir,
652773bf00SMiklos Szeredi 	.rename		= afs_rename,
6600d3b7a4SDavid Howells 	.permission	= afs_permission,
67416351f2SDavid Howells 	.getattr	= afs_getattr,
6831143d5dSDavid Howells 	.setattr	= afs_setattr,
69d3e3b7eaSDavid Howells 	.listxattr	= afs_listxattr,
701da177e4SLinus Torvalds };
711da177e4SLinus Torvalds 
724d673da1SDavid Howells const struct file_operations afs_dynroot_file_operations = {
734d673da1SDavid Howells 	.open		= dcache_dir_open,
744d673da1SDavid Howells 	.release	= dcache_dir_close,
754d673da1SDavid Howells 	.iterate_shared	= dcache_readdir,
764d673da1SDavid Howells 	.llseek		= dcache_dir_lseek,
774d673da1SDavid Howells };
784d673da1SDavid Howells 
794d673da1SDavid Howells const struct inode_operations afs_dynroot_inode_operations = {
804d673da1SDavid Howells 	.lookup		= afs_dynroot_lookup,
814d673da1SDavid Howells };
824d673da1SDavid Howells 
83d61dcce2SAl Viro const struct dentry_operations afs_fs_dentry_operations = {
841da177e4SLinus Torvalds 	.d_revalidate	= afs_d_revalidate,
851da177e4SLinus Torvalds 	.d_delete	= afs_d_delete,
86260a9803SDavid Howells 	.d_release	= afs_d_release,
87d18610b0SDavid Howells 	.d_automount	= afs_d_automount,
881da177e4SLinus Torvalds };
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #define AFS_DIR_HASHTBL_SIZE	128
911da177e4SLinus Torvalds #define AFS_DIR_DIRENT_SIZE	32
921da177e4SLinus Torvalds #define AFS_DIRENT_PER_BLOCK	64
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds union afs_dirent {
951da177e4SLinus Torvalds 	struct {
961da177e4SLinus Torvalds 		uint8_t		valid;
971da177e4SLinus Torvalds 		uint8_t		unused[1];
981da177e4SLinus Torvalds 		__be16		hash_next;
991da177e4SLinus Torvalds 		__be32		vnode;
1001da177e4SLinus Torvalds 		__be32		unique;
1011da177e4SLinus Torvalds 		uint8_t		name[16];
1021da177e4SLinus Torvalds 		uint8_t		overflow[4];	/* if any char of the name (inc
1031da177e4SLinus Torvalds 						 * NUL) reaches here, consume
1041da177e4SLinus Torvalds 						 * the next dirent too */
1051da177e4SLinus Torvalds 	} u;
1061da177e4SLinus Torvalds 	uint8_t	extended_name[32];
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
1101da177e4SLinus Torvalds struct afs_dir_pagehdr {
1111da177e4SLinus Torvalds 	__be16		npages;
1121da177e4SLinus Torvalds 	__be16		magic;
1131da177e4SLinus Torvalds #define AFS_DIR_MAGIC htons(1234)
1141da177e4SLinus Torvalds 	uint8_t		nentries;
1151da177e4SLinus Torvalds 	uint8_t		bitmap[8];
1161da177e4SLinus Torvalds 	uint8_t		pad[19];
1171da177e4SLinus Torvalds };
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /* directory block layout */
1201da177e4SLinus Torvalds union afs_dir_block {
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	struct afs_dir_pagehdr pagehdr;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	struct {
1251da177e4SLinus Torvalds 		struct afs_dir_pagehdr	pagehdr;
1261da177e4SLinus Torvalds 		uint8_t			alloc_ctrs[128];
1271da177e4SLinus Torvalds 		/* dir hash table */
1281da177e4SLinus Torvalds 		uint16_t		hashtable[AFS_DIR_HASHTBL_SIZE];
1291da177e4SLinus Torvalds 	} hdr;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
1321da177e4SLinus Torvalds };
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /* layout on a linux VM page */
1351da177e4SLinus Torvalds struct afs_dir_page {
1361da177e4SLinus Torvalds 	union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
1371da177e4SLinus Torvalds };
1381da177e4SLinus Torvalds 
1395cf9dd55SDavid Howells struct afs_lookup_one_cookie {
1405cf9dd55SDavid Howells 	struct dir_context	ctx;
1415cf9dd55SDavid Howells 	struct qstr		name;
1425cf9dd55SDavid Howells 	bool			found;
1435cf9dd55SDavid Howells 	struct afs_fid		fid;
1445cf9dd55SDavid Howells };
1455cf9dd55SDavid Howells 
146260a9803SDavid Howells struct afs_lookup_cookie {
1471bbae9f8SAl Viro 	struct dir_context	ctx;
1481bbae9f8SAl Viro 	struct qstr		name;
1495cf9dd55SDavid Howells 	bool			found;
1505cf9dd55SDavid Howells 	bool			one_only;
1515cf9dd55SDavid Howells 	unsigned short		nr_fids;
1525cf9dd55SDavid Howells 	struct afs_file_status	*statuses;
1535cf9dd55SDavid Howells 	struct afs_callback	*callbacks;
1545cf9dd55SDavid Howells 	struct afs_fid		fids[50];
1551da177e4SLinus Torvalds };
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*
1581da177e4SLinus Torvalds  * check that a directory page is valid
1591da177e4SLinus Torvalds  */
160dab17c1aSDavid Howells bool afs_dir_check_page(struct inode *dir, struct page *page)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	struct afs_dir_page *dbuf;
163dab17c1aSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(dir);
164dab17c1aSDavid Howells 	loff_t latter, i_size, off;
1651da177e4SLinus Torvalds 	int tmp, qty;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds #if 0
1681da177e4SLinus Torvalds 	/* check the page count */
1691da177e4SLinus Torvalds 	qty = desc.size / sizeof(dbuf->blocks[0]);
1701da177e4SLinus Torvalds 	if (qty == 0)
1711da177e4SLinus Torvalds 		goto error;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
1741da177e4SLinus Torvalds 		printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
175530b6412SHarvey Harrison 		       __func__, dir->i_ino, qty,
17608e0e7c8SDavid Howells 		       ntohs(dbuf->blocks[0].pagehdr.npages));
1771da177e4SLinus Torvalds 		goto error;
1781da177e4SLinus Torvalds 	}
1791da177e4SLinus Torvalds #endif
1801da177e4SLinus Torvalds 
181dab17c1aSDavid Howells 	/* Determine how many magic numbers there should be in this page, but
182dab17c1aSDavid Howells 	 * we must take care because the directory may change size under us.
183dab17c1aSDavid Howells 	 */
184dab17c1aSDavid Howells 	off = page_offset(page);
185dab17c1aSDavid Howells 	i_size = i_size_read(dir);
186dab17c1aSDavid Howells 	if (i_size <= off)
187dab17c1aSDavid Howells 		goto checked;
188dab17c1aSDavid Howells 
189dab17c1aSDavid Howells 	latter = i_size - off;
1901da177e4SLinus Torvalds 	if (latter >= PAGE_SIZE)
1911da177e4SLinus Torvalds 		qty = PAGE_SIZE;
1921da177e4SLinus Torvalds 	else
1931da177e4SLinus Torvalds 		qty = latter;
1941da177e4SLinus Torvalds 	qty /= sizeof(union afs_dir_block);
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	/* check them */
1971da177e4SLinus Torvalds 	dbuf = page_address(page);
1981da177e4SLinus Torvalds 	for (tmp = 0; tmp < qty; tmp++) {
1991da177e4SLinus Torvalds 		if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
200dab17c1aSDavid Howells 			printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
201530b6412SHarvey Harrison 			       __func__, dir->i_ino, tmp, qty,
2021da177e4SLinus Torvalds 			       ntohs(dbuf->blocks[tmp].pagehdr.magic));
203dab17c1aSDavid Howells 			trace_afs_dir_check_failed(vnode, off, i_size);
2041da177e4SLinus Torvalds 			goto error;
2051da177e4SLinus Torvalds 		}
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 
208dab17c1aSDavid Howells checked:
209d55b4da4SDavid Howells 	afs_stat_v(vnode, n_read_dir);
2101da177e4SLinus Torvalds 	SetPageChecked(page);
211be5b82dbSAl Viro 	return true;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds error:
2141da177e4SLinus Torvalds 	SetPageError(page);
215be5b82dbSAl Viro 	return false;
216ec26815aSDavid Howells }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds /*
2191da177e4SLinus Torvalds  * discard a page cached in the pagecache
2201da177e4SLinus Torvalds  */
2211da177e4SLinus Torvalds static inline void afs_dir_put_page(struct page *page)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	kunmap(page);
224dab17c1aSDavid Howells 	unlock_page(page);
22509cbfeafSKirill A. Shutemov 	put_page(page);
226ec26815aSDavid Howells }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds /*
2291da177e4SLinus Torvalds  * get a page into the pagecache
2301da177e4SLinus Torvalds  */
23100d3b7a4SDavid Howells static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
23200d3b7a4SDavid Howells 				     struct key *key)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	struct page *page;
2351da177e4SLinus Torvalds 	_enter("{%lu},%lu", dir->i_ino, index);
2361da177e4SLinus Torvalds 
237f6d335c0SAl Viro 	page = read_cache_page(dir->i_mapping, index, afs_page_filler, key);
2381da177e4SLinus Torvalds 	if (!IS_ERR(page)) {
239dab17c1aSDavid Howells 		lock_page(page);
2401da177e4SLinus Torvalds 		kmap(page);
241be5b82dbSAl Viro 		if (unlikely(!PageChecked(page))) {
242dab17c1aSDavid Howells 			if (PageError(page))
2431da177e4SLinus Torvalds 				goto fail;
2441da177e4SLinus Torvalds 		}
245be5b82dbSAl Viro 	}
2461da177e4SLinus Torvalds 	return page;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds fail:
2491da177e4SLinus Torvalds 	afs_dir_put_page(page);
25008e0e7c8SDavid Howells 	_leave(" = -EIO");
2511da177e4SLinus Torvalds 	return ERR_PTR(-EIO);
252ec26815aSDavid Howells }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  * open an AFS directory file
2561da177e4SLinus Torvalds  */
2571da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	_enter("{%lu}", inode->i_ino);
2601da177e4SLinus Torvalds 
2612ecd05aeSAlexey Dobriyan 	BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
2622ecd05aeSAlexey Dobriyan 	BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
2631da177e4SLinus Torvalds 
26408e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
2651da177e4SLinus Torvalds 		return -ENOENT;
2661da177e4SLinus Torvalds 
26700d3b7a4SDavid Howells 	return afs_open(inode, file);
268ec26815aSDavid Howells }
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds /*
2711da177e4SLinus Torvalds  * deal with one block in an AFS directory
2721da177e4SLinus Torvalds  */
2731bbae9f8SAl Viro static int afs_dir_iterate_block(struct dir_context *ctx,
2741da177e4SLinus Torvalds 				 union afs_dir_block *block,
2751bbae9f8SAl Viro 				 unsigned blkoff)
2761da177e4SLinus Torvalds {
2771da177e4SLinus Torvalds 	union afs_dirent *dire;
2781da177e4SLinus Torvalds 	unsigned offset, next, curr;
2791da177e4SLinus Torvalds 	size_t nlen;
2801bbae9f8SAl Viro 	int tmp;
2811da177e4SLinus Torvalds 
2821bbae9f8SAl Viro 	_enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
2831da177e4SLinus Torvalds 
2841bbae9f8SAl Viro 	curr = (ctx->pos - blkoff) / sizeof(union afs_dirent);
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	/* walk through the block, an entry at a time */
2871da177e4SLinus Torvalds 	for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
2881da177e4SLinus Torvalds 	     offset < AFS_DIRENT_PER_BLOCK;
2891da177e4SLinus Torvalds 	     offset = next
2901da177e4SLinus Torvalds 	     ) {
2911da177e4SLinus Torvalds 		next = offset + 1;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 		/* skip entries marked unused in the bitmap */
2941da177e4SLinus Torvalds 		if (!(block->pagehdr.bitmap[offset / 8] &
2951da177e4SLinus Torvalds 		      (1 << (offset % 8)))) {
2965b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: unused",
2971da177e4SLinus Torvalds 			       blkoff / sizeof(union afs_dir_block), offset);
2981da177e4SLinus Torvalds 			if (offset >= curr)
2991bbae9f8SAl Viro 				ctx->pos = blkoff +
3001da177e4SLinus Torvalds 					next * sizeof(union afs_dirent);
3011da177e4SLinus Torvalds 			continue;
3021da177e4SLinus Torvalds 		}
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 		/* got a valid entry */
3051da177e4SLinus Torvalds 		dire = &block->dirents[offset];
3061da177e4SLinus Torvalds 		nlen = strnlen(dire->u.name,
3071da177e4SLinus Torvalds 			       sizeof(*block) -
3081da177e4SLinus Torvalds 			       offset * sizeof(union afs_dirent));
3091da177e4SLinus Torvalds 
3105b5e0928SAlexey Dobriyan 		_debug("ENT[%zu.%u]: %s %zu \"%s\"",
3111da177e4SLinus Torvalds 		       blkoff / sizeof(union afs_dir_block), offset,
3121da177e4SLinus Torvalds 		       (offset < curr ? "skip" : "fill"),
3131da177e4SLinus Torvalds 		       nlen, dire->u.name);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 		/* work out where the next possible entry is */
3161da177e4SLinus Torvalds 		for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
3171da177e4SLinus Torvalds 			if (next >= AFS_DIRENT_PER_BLOCK) {
3185b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
3191da177e4SLinus Torvalds 				       " %u travelled beyond end dir block"
3205b5e0928SAlexey Dobriyan 				       " (len %u/%zu)",
3211da177e4SLinus Torvalds 				       blkoff / sizeof(union afs_dir_block),
3221da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
3231da177e4SLinus Torvalds 				return -EIO;
3241da177e4SLinus Torvalds 			}
3251da177e4SLinus Torvalds 			if (!(block->pagehdr.bitmap[next / 8] &
3261da177e4SLinus Torvalds 			      (1 << (next % 8)))) {
3275b5e0928SAlexey Dobriyan 				_debug("ENT[%zu.%u]:"
3285b5e0928SAlexey Dobriyan 				       " %u unmarked extension (len %u/%zu)",
3291da177e4SLinus Torvalds 				       blkoff / sizeof(union afs_dir_block),
3301da177e4SLinus Torvalds 				       offset, next, tmp, nlen);
3311da177e4SLinus Torvalds 				return -EIO;
3321da177e4SLinus Torvalds 			}
3331da177e4SLinus Torvalds 
3345b5e0928SAlexey Dobriyan 			_debug("ENT[%zu.%u]: ext %u/%zu",
3351da177e4SLinus Torvalds 			       blkoff / sizeof(union afs_dir_block),
3361da177e4SLinus Torvalds 			       next, tmp, nlen);
3371da177e4SLinus Torvalds 			next++;
3381da177e4SLinus Torvalds 		}
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 		/* skip if starts before the current position */
3411da177e4SLinus Torvalds 		if (offset < curr)
3421da177e4SLinus Torvalds 			continue;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 		/* found the next entry */
3451bbae9f8SAl Viro 		if (!dir_emit(ctx, dire->u.name, nlen,
3461da177e4SLinus Torvalds 			      ntohl(dire->u.vnode),
3475cf9dd55SDavid Howells 			      (ctx->actor == afs_lookup_filldir ||
3485cf9dd55SDavid Howells 			       ctx->actor == afs_lookup_one_filldir)?
3491bbae9f8SAl Viro 			      ntohl(dire->u.unique) : DT_UNKNOWN)) {
3501da177e4SLinus Torvalds 			_leave(" = 0 [full]");
3511da177e4SLinus Torvalds 			return 0;
3521da177e4SLinus Torvalds 		}
3531da177e4SLinus Torvalds 
3541bbae9f8SAl Viro 		ctx->pos = blkoff + next * sizeof(union afs_dirent);
3551da177e4SLinus Torvalds 	}
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	_leave(" = 1 [more]");
3581da177e4SLinus Torvalds 	return 1;
359ec26815aSDavid Howells }
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds /*
36208e0e7c8SDavid Howells  * iterate through the data blob that lists the contents of an AFS directory
3631da177e4SLinus Torvalds  */
3641bbae9f8SAl Viro static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
3651bbae9f8SAl Viro 			   struct key *key)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	union afs_dir_block *dblock;
3681da177e4SLinus Torvalds 	struct afs_dir_page *dbuf;
3691da177e4SLinus Torvalds 	struct page *page;
3701da177e4SLinus Torvalds 	unsigned blkoff, limit;
3711da177e4SLinus Torvalds 	int ret;
3721da177e4SLinus Torvalds 
3731bbae9f8SAl Viro 	_enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
3741da177e4SLinus Torvalds 
37508e0e7c8SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
3761da177e4SLinus Torvalds 		_leave(" = -ESTALE");
3771da177e4SLinus Torvalds 		return -ESTALE;
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	/* round the file position up to the next entry boundary */
3811bbae9f8SAl Viro 	ctx->pos += sizeof(union afs_dirent) - 1;
3821bbae9f8SAl Viro 	ctx->pos &= ~(sizeof(union afs_dirent) - 1);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* walk through the blocks in sequence */
3851da177e4SLinus Torvalds 	ret = 0;
3861bbae9f8SAl Viro 	while (ctx->pos < dir->i_size) {
3871bbae9f8SAl Viro 		blkoff = ctx->pos & ~(sizeof(union afs_dir_block) - 1);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 		/* fetch the appropriate page from the directory */
39000d3b7a4SDavid Howells 		page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
3911da177e4SLinus Torvalds 		if (IS_ERR(page)) {
3921da177e4SLinus Torvalds 			ret = PTR_ERR(page);
3931da177e4SLinus Torvalds 			break;
3941da177e4SLinus Torvalds 		}
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 		limit = blkoff & ~(PAGE_SIZE - 1);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 		dbuf = page_address(page);
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 		/* deal with the individual blocks stashed on this page */
4011da177e4SLinus Torvalds 		do {
4021da177e4SLinus Torvalds 			dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
4031da177e4SLinus Torvalds 					       sizeof(union afs_dir_block)];
4041bbae9f8SAl Viro 			ret = afs_dir_iterate_block(ctx, dblock, blkoff);
4051da177e4SLinus Torvalds 			if (ret != 1) {
4061da177e4SLinus Torvalds 				afs_dir_put_page(page);
4071da177e4SLinus Torvalds 				goto out;
4081da177e4SLinus Torvalds 			}
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 			blkoff += sizeof(union afs_dir_block);
4111da177e4SLinus Torvalds 
4121bbae9f8SAl Viro 		} while (ctx->pos < dir->i_size && blkoff < limit);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 		afs_dir_put_page(page);
4151da177e4SLinus Torvalds 		ret = 0;
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds out:
4191da177e4SLinus Torvalds 	_leave(" = %d", ret);
4201da177e4SLinus Torvalds 	return ret;
421ec26815aSDavid Howells }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds /*
4241da177e4SLinus Torvalds  * read an AFS directory
4251da177e4SLinus Torvalds  */
4261bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx)
4271da177e4SLinus Torvalds {
428215804a9SDavid Howells 	return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
429ec26815aSDavid Howells }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds /*
4325cf9dd55SDavid Howells  * Search the directory for a single name
4331da177e4SLinus Torvalds  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
4341da177e4SLinus Torvalds  *   uniquifier through dtype
4351da177e4SLinus Torvalds  */
4365cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
437ac7576f4SMiklos Szeredi 				  int nlen, loff_t fpos, u64 ino, unsigned dtype)
4381da177e4SLinus Torvalds {
4395cf9dd55SDavid Howells 	struct afs_lookup_one_cookie *cookie =
4405cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_one_cookie, ctx);
4411da177e4SLinus Torvalds 
4421bbae9f8SAl Viro 	_enter("{%s,%u},%s,%u,,%llu,%u",
4431bbae9f8SAl Viro 	       cookie->name.name, cookie->name.len, name, nlen,
444ba3e0e1aSDavid S. Miller 	       (unsigned long long) ino, dtype);
4451da177e4SLinus Torvalds 
44608e0e7c8SDavid Howells 	/* insanity checks first */
44708e0e7c8SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
44808e0e7c8SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
44908e0e7c8SDavid Howells 
4501bbae9f8SAl Viro 	if (cookie->name.len != nlen ||
4511bbae9f8SAl Viro 	    memcmp(cookie->name.name, name, nlen) != 0) {
4521da177e4SLinus Torvalds 		_leave(" = 0 [no]");
4531da177e4SLinus Torvalds 		return 0;
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	cookie->fid.vnode = ino;
4571da177e4SLinus Torvalds 	cookie->fid.unique = dtype;
4581da177e4SLinus Torvalds 	cookie->found = 1;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	_leave(" = -1 [found]");
4611da177e4SLinus Torvalds 	return -1;
462ec26815aSDavid Howells }
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds /*
4655cf9dd55SDavid Howells  * Do a lookup of a single name in a directory
466260a9803SDavid Howells  * - just returns the FID the dentry name maps to if found
4671da177e4SLinus Torvalds  */
4685cf9dd55SDavid Howells static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
46900d3b7a4SDavid Howells 			     struct afs_fid *fid, struct key *key)
4701da177e4SLinus Torvalds {
4711bbae9f8SAl Viro 	struct afs_super_info *as = dir->i_sb->s_fs_info;
4725cf9dd55SDavid Howells 	struct afs_lookup_one_cookie cookie = {
4735cf9dd55SDavid Howells 		.ctx.actor = afs_lookup_one_filldir,
4741bbae9f8SAl Viro 		.name = dentry->d_name,
4751bbae9f8SAl Viro 		.fid.vid = as->volume->vid
4761bbae9f8SAl Viro 	};
4771da177e4SLinus Torvalds 	int ret;
4781da177e4SLinus Torvalds 
479a455589fSAl Viro 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	/* search the directory */
4821bbae9f8SAl Viro 	ret = afs_dir_iterate(dir, &cookie.ctx, key);
4831da177e4SLinus Torvalds 	if (ret < 0) {
48408e0e7c8SDavid Howells 		_leave(" = %d [iter]", ret);
48508e0e7c8SDavid Howells 		return ret;
4861da177e4SLinus Torvalds 	}
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	ret = -ENOENT;
4891da177e4SLinus Torvalds 	if (!cookie.found) {
49008e0e7c8SDavid Howells 		_leave(" = -ENOENT [not found]");
49108e0e7c8SDavid Howells 		return -ENOENT;
49208e0e7c8SDavid Howells 	}
49308e0e7c8SDavid Howells 
49408e0e7c8SDavid Howells 	*fid = cookie.fid;
49508e0e7c8SDavid Howells 	_leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
49608e0e7c8SDavid Howells 	return 0;
49708e0e7c8SDavid Howells }
49808e0e7c8SDavid Howells 
49908e0e7c8SDavid Howells /*
5005cf9dd55SDavid Howells  * search the directory for a name
5015cf9dd55SDavid Howells  * - if afs_dir_iterate_block() spots this function, it'll pass the FID
5025cf9dd55SDavid Howells  *   uniquifier through dtype
5035cf9dd55SDavid Howells  */
5045cf9dd55SDavid Howells static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
5055cf9dd55SDavid Howells 			      int nlen, loff_t fpos, u64 ino, unsigned dtype)
5065cf9dd55SDavid Howells {
5075cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie =
5085cf9dd55SDavid Howells 		container_of(ctx, struct afs_lookup_cookie, ctx);
5095cf9dd55SDavid Howells 	int ret;
5105cf9dd55SDavid Howells 
5115cf9dd55SDavid Howells 	_enter("{%s,%u},%s,%u,,%llu,%u",
5125cf9dd55SDavid Howells 	       cookie->name.name, cookie->name.len, name, nlen,
5135cf9dd55SDavid Howells 	       (unsigned long long) ino, dtype);
5145cf9dd55SDavid Howells 
5155cf9dd55SDavid Howells 	/* insanity checks first */
5165cf9dd55SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
5175cf9dd55SDavid Howells 	BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
5185cf9dd55SDavid Howells 
5195cf9dd55SDavid Howells 	if (cookie->found) {
5205cf9dd55SDavid Howells 		if (cookie->nr_fids < 50) {
5215cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].vnode	= ino;
5225cf9dd55SDavid Howells 			cookie->fids[cookie->nr_fids].unique	= dtype;
5235cf9dd55SDavid Howells 			cookie->nr_fids++;
5245cf9dd55SDavid Howells 		}
5255cf9dd55SDavid Howells 	} else if (cookie->name.len == nlen &&
5265cf9dd55SDavid Howells 		   memcmp(cookie->name.name, name, nlen) == 0) {
5275cf9dd55SDavid Howells 		cookie->fids[0].vnode	= ino;
5285cf9dd55SDavid Howells 		cookie->fids[0].unique	= dtype;
5295cf9dd55SDavid Howells 		cookie->found = 1;
5305cf9dd55SDavid Howells 		if (cookie->one_only)
5315cf9dd55SDavid Howells 			return -1;
5325cf9dd55SDavid Howells 	}
5335cf9dd55SDavid Howells 
5345cf9dd55SDavid Howells 	ret = cookie->nr_fids >= 50 ? -1 : 0;
5355cf9dd55SDavid Howells 	_leave(" = %d", ret);
5365cf9dd55SDavid Howells 	return ret;
5375cf9dd55SDavid Howells }
5385cf9dd55SDavid Howells 
5395cf9dd55SDavid Howells /*
5405cf9dd55SDavid Howells  * Do a lookup in a directory.  We make use of bulk lookup to query a slew of
5415cf9dd55SDavid Howells  * files in one go and create inodes for them.  The inode of the file we were
5425cf9dd55SDavid Howells  * asked for is returned.
5435cf9dd55SDavid Howells  */
5445cf9dd55SDavid Howells static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
5455cf9dd55SDavid Howells 				   struct key *key)
5465cf9dd55SDavid Howells {
5475cf9dd55SDavid Howells 	struct afs_lookup_cookie *cookie;
5485cf9dd55SDavid Howells 	struct afs_cb_interest *cbi = NULL;
5495cf9dd55SDavid Howells 	struct afs_super_info *as = dir->i_sb->s_fs_info;
5505cf9dd55SDavid Howells 	struct afs_iget_data data;
5515cf9dd55SDavid Howells 	struct afs_fs_cursor fc;
5525cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
5535cf9dd55SDavid Howells 	struct inode *inode = NULL;
5545cf9dd55SDavid Howells 	int ret, i;
5555cf9dd55SDavid Howells 
5565cf9dd55SDavid Howells 	_enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
5575cf9dd55SDavid Howells 
5585cf9dd55SDavid Howells 	cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
5595cf9dd55SDavid Howells 	if (!cookie)
5605cf9dd55SDavid Howells 		return ERR_PTR(-ENOMEM);
5615cf9dd55SDavid Howells 
5625cf9dd55SDavid Howells 	cookie->ctx.actor = afs_lookup_filldir;
5635cf9dd55SDavid Howells 	cookie->name = dentry->d_name;
5645cf9dd55SDavid Howells 	cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
5655cf9dd55SDavid Howells 
5665cf9dd55SDavid Howells 	read_seqlock_excl(&dvnode->cb_lock);
5675cf9dd55SDavid Howells 	if (dvnode->cb_interest &&
5685cf9dd55SDavid Howells 	    dvnode->cb_interest->server &&
5695cf9dd55SDavid Howells 	    test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
5705cf9dd55SDavid Howells 		cookie->one_only = true;
5715cf9dd55SDavid Howells 	read_sequnlock_excl(&dvnode->cb_lock);
5725cf9dd55SDavid Howells 
5735cf9dd55SDavid Howells 	for (i = 0; i < 50; i++)
5745cf9dd55SDavid Howells 		cookie->fids[i].vid = as->volume->vid;
5755cf9dd55SDavid Howells 
5765cf9dd55SDavid Howells 	/* search the directory */
5775cf9dd55SDavid Howells 	ret = afs_dir_iterate(dir, &cookie->ctx, key);
5785cf9dd55SDavid Howells 	if (ret < 0) {
5795cf9dd55SDavid Howells 		inode = ERR_PTR(ret);
5805cf9dd55SDavid Howells 		goto out;
5815cf9dd55SDavid Howells 	}
5825cf9dd55SDavid Howells 
5835cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOENT);
5845cf9dd55SDavid Howells 	if (!cookie->found)
5855cf9dd55SDavid Howells 		goto out;
5865cf9dd55SDavid Howells 
5875cf9dd55SDavid Howells 	/* Check to see if we already have an inode for the primary fid. */
5885cf9dd55SDavid Howells 	data.volume = dvnode->volume;
5895cf9dd55SDavid Howells 	data.fid = cookie->fids[0];
5905cf9dd55SDavid Howells 	inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
5915cf9dd55SDavid Howells 	if (inode)
5925cf9dd55SDavid Howells 		goto out;
5935cf9dd55SDavid Howells 
5945cf9dd55SDavid Howells 	/* Need space for examining all the selected files */
5955cf9dd55SDavid Howells 	inode = ERR_PTR(-ENOMEM);
5965cf9dd55SDavid Howells 	cookie->statuses = kcalloc(cookie->nr_fids, sizeof(struct afs_file_status),
5975cf9dd55SDavid Howells 				   GFP_KERNEL);
5985cf9dd55SDavid Howells 	if (!cookie->statuses)
5995cf9dd55SDavid Howells 		goto out;
6005cf9dd55SDavid Howells 
6015cf9dd55SDavid Howells 	cookie->callbacks = kcalloc(cookie->nr_fids, sizeof(struct afs_callback),
6025cf9dd55SDavid Howells 				    GFP_KERNEL);
6035cf9dd55SDavid Howells 	if (!cookie->callbacks)
6045cf9dd55SDavid Howells 		goto out_s;
6055cf9dd55SDavid Howells 
6065cf9dd55SDavid Howells 	/* Try FS.InlineBulkStatus first.  Abort codes for the individual
6075cf9dd55SDavid Howells 	 * lookups contained therein are stored in the reply without aborting
6085cf9dd55SDavid Howells 	 * the whole operation.
6095cf9dd55SDavid Howells 	 */
6105cf9dd55SDavid Howells 	if (cookie->one_only)
6115cf9dd55SDavid Howells 		goto no_inline_bulk_status;
6125cf9dd55SDavid Howells 
6135cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
6145cf9dd55SDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
6155cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
6165cf9dd55SDavid Howells 			if (test_bit(AFS_SERVER_FL_NO_IBULK,
6175cf9dd55SDavid Howells 				      &fc.cbi->server->flags)) {
6185cf9dd55SDavid Howells 				fc.ac.abort_code = RX_INVALID_OPERATION;
6195cf9dd55SDavid Howells 				fc.ac.error = -ECONNABORTED;
6205cf9dd55SDavid Howells 				break;
6215cf9dd55SDavid Howells 			}
6225cf9dd55SDavid Howells 			afs_fs_inline_bulk_status(&fc,
6235cf9dd55SDavid Howells 						  afs_v2net(dvnode),
6245cf9dd55SDavid Howells 						  cookie->fids,
6255cf9dd55SDavid Howells 						  cookie->statuses,
6265cf9dd55SDavid Howells 						  cookie->callbacks,
6275cf9dd55SDavid Howells 						  cookie->nr_fids, NULL);
6285cf9dd55SDavid Howells 		}
6295cf9dd55SDavid Howells 
6305cf9dd55SDavid Howells 		if (fc.ac.error == 0)
6315cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
6325cf9dd55SDavid Howells 		if (fc.ac.abort_code == RX_INVALID_OPERATION)
6335cf9dd55SDavid Howells 			set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
6345cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
6355cf9dd55SDavid Howells 	}
6365cf9dd55SDavid Howells 
6375cf9dd55SDavid Howells 	if (!IS_ERR(inode))
6385cf9dd55SDavid Howells 		goto success;
6395cf9dd55SDavid Howells 	if (fc.ac.abort_code != RX_INVALID_OPERATION)
6405cf9dd55SDavid Howells 		goto out_c;
6415cf9dd55SDavid Howells 
6425cf9dd55SDavid Howells no_inline_bulk_status:
6435cf9dd55SDavid Howells 	/* We could try FS.BulkStatus next, but this aborts the entire op if
6445cf9dd55SDavid Howells 	 * any of the lookups fails - so, for the moment, revert to
6455cf9dd55SDavid Howells 	 * FS.FetchStatus for just the primary fid.
6465cf9dd55SDavid Howells 	 */
6475cf9dd55SDavid Howells 	cookie->nr_fids = 1;
6485cf9dd55SDavid Howells 	inode = ERR_PTR(-ERESTARTSYS);
6495cf9dd55SDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
6505cf9dd55SDavid Howells 		while (afs_select_fileserver(&fc)) {
6515cf9dd55SDavid Howells 			afs_fs_fetch_status(&fc,
6525cf9dd55SDavid Howells 					    afs_v2net(dvnode),
6535cf9dd55SDavid Howells 					    cookie->fids,
6545cf9dd55SDavid Howells 					    cookie->statuses,
6555cf9dd55SDavid Howells 					    cookie->callbacks,
6565cf9dd55SDavid Howells 					    NULL);
6575cf9dd55SDavid Howells 		}
6585cf9dd55SDavid Howells 
6595cf9dd55SDavid Howells 		if (fc.ac.error == 0)
6605cf9dd55SDavid Howells 			cbi = afs_get_cb_interest(fc.cbi);
6615cf9dd55SDavid Howells 		inode = ERR_PTR(afs_end_vnode_operation(&fc));
6625cf9dd55SDavid Howells 	}
6635cf9dd55SDavid Howells 
6645cf9dd55SDavid Howells 	if (IS_ERR(inode))
6655cf9dd55SDavid Howells 		goto out_c;
6665cf9dd55SDavid Howells 
6675cf9dd55SDavid Howells 	for (i = 0; i < cookie->nr_fids; i++)
6685cf9dd55SDavid Howells 		cookie->statuses[i].abort_code = 0;
6695cf9dd55SDavid Howells 
6705cf9dd55SDavid Howells success:
6715cf9dd55SDavid Howells 	/* Turn all the files into inodes and save the first one - which is the
6725cf9dd55SDavid Howells 	 * one we actually want.
6735cf9dd55SDavid Howells 	 */
6745cf9dd55SDavid Howells 	if (cookie->statuses[0].abort_code != 0)
6755cf9dd55SDavid Howells 		inode = ERR_PTR(afs_abort_to_error(cookie->statuses[0].abort_code));
6765cf9dd55SDavid Howells 
6775cf9dd55SDavid Howells 	for (i = 0; i < cookie->nr_fids; i++) {
6785cf9dd55SDavid Howells 		struct inode *ti;
6795cf9dd55SDavid Howells 
6805cf9dd55SDavid Howells 		if (cookie->statuses[i].abort_code != 0)
6815cf9dd55SDavid Howells 			continue;
6825cf9dd55SDavid Howells 
6835cf9dd55SDavid Howells 		ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
6845cf9dd55SDavid Howells 			      &cookie->statuses[i],
6855cf9dd55SDavid Howells 			      &cookie->callbacks[i],
6865cf9dd55SDavid Howells 			      cbi);
6875cf9dd55SDavid Howells 		if (i == 0) {
6885cf9dd55SDavid Howells 			inode = ti;
6895cf9dd55SDavid Howells 		} else {
6905cf9dd55SDavid Howells 			if (!IS_ERR(ti))
6915cf9dd55SDavid Howells 				iput(ti);
6925cf9dd55SDavid Howells 		}
6935cf9dd55SDavid Howells 	}
6945cf9dd55SDavid Howells 
6955cf9dd55SDavid Howells out_c:
6965cf9dd55SDavid Howells 	afs_put_cb_interest(afs_v2net(dvnode), cbi);
6975cf9dd55SDavid Howells 	kfree(cookie->callbacks);
6985cf9dd55SDavid Howells out_s:
6995cf9dd55SDavid Howells 	kfree(cookie->statuses);
7005cf9dd55SDavid Howells out:
7015cf9dd55SDavid Howells 	kfree(cookie);
7025cf9dd55SDavid Howells 	return inode;
7035cf9dd55SDavid Howells }
7045cf9dd55SDavid Howells 
7055cf9dd55SDavid Howells /*
7064d673da1SDavid Howells  * Probe to see if a cell may exist.  This prevents positive dentries from
7074d673da1SDavid Howells  * being created unnecessarily.
7084d673da1SDavid Howells  */
7094d673da1SDavid Howells static int afs_probe_cell_name(struct dentry *dentry)
7104d673da1SDavid Howells {
7114d673da1SDavid Howells 	struct afs_cell *cell;
7124d673da1SDavid Howells 	const char *name = dentry->d_name.name;
7134d673da1SDavid Howells 	size_t len = dentry->d_name.len;
7144d673da1SDavid Howells 	int ret;
7154d673da1SDavid Howells 
7164d673da1SDavid Howells 	/* Names prefixed with a dot are R/W mounts. */
7174d673da1SDavid Howells 	if (name[0] == '.') {
7184d673da1SDavid Howells 		if (len == 1)
7194d673da1SDavid Howells 			return -EINVAL;
7204d673da1SDavid Howells 		name++;
7214d673da1SDavid Howells 		len--;
7224d673da1SDavid Howells 	}
7234d673da1SDavid Howells 
7244d673da1SDavid Howells 	cell = afs_lookup_cell_rcu(afs_d2net(dentry), name, len);
7254d673da1SDavid Howells 	if (!IS_ERR(cell)) {
7264d673da1SDavid Howells 		afs_put_cell(afs_d2net(dentry), cell);
7274d673da1SDavid Howells 		return 0;
7284d673da1SDavid Howells 	}
7294d673da1SDavid Howells 
7304d673da1SDavid Howells 	ret = dns_query("afsdb", name, len, "ipv4", NULL, NULL);
7314d673da1SDavid Howells 	if (ret == -ENODATA)
7324d673da1SDavid Howells 		ret = -EDESTADDRREQ;
7334d673da1SDavid Howells 	return ret;
7344d673da1SDavid Howells }
7354d673da1SDavid Howells 
7364d673da1SDavid Howells /*
737bec5eb61Swanglei  * Try to auto mount the mountpoint with pseudo directory, if the autocell
738bec5eb61Swanglei  * operation is setted.
739bec5eb61Swanglei  */
7405cf9dd55SDavid Howells static struct inode *afs_try_auto_mntpt(struct dentry *dentry, struct inode *dir)
741bec5eb61Swanglei {
742bec5eb61Swanglei 	struct afs_vnode *vnode = AFS_FS_I(dir);
743bec5eb61Swanglei 	struct inode *inode;
7444d673da1SDavid Howells 	int ret = -ENOENT;
745bec5eb61Swanglei 
7464d673da1SDavid Howells 	_enter("%p{%pd}, {%x:%u}",
7474d673da1SDavid Howells 	       dentry, dentry, vnode->fid.vid, vnode->fid.vnode);
748bec5eb61Swanglei 
7494d673da1SDavid Howells 	if (!test_bit(AFS_VNODE_AUTOCELL, &vnode->flags))
750bec5eb61Swanglei 		goto out;
751bec5eb61Swanglei 
7524d673da1SDavid Howells 	ret = afs_probe_cell_name(dentry);
7534d673da1SDavid Howells 	if (ret < 0)
7544d673da1SDavid Howells 		goto out;
7554d673da1SDavid Howells 
7564d673da1SDavid Howells 	inode = afs_iget_pseudo_dir(dir->i_sb, false);
757bec5eb61Swanglei 	if (IS_ERR(inode)) {
758bec5eb61Swanglei 		ret = PTR_ERR(inode);
759bec5eb61Swanglei 		goto out;
760bec5eb61Swanglei 	}
761bec5eb61Swanglei 
762bec5eb61Swanglei 	_leave("= %p", inode);
763bec5eb61Swanglei 	return inode;
764bec5eb61Swanglei 
765bec5eb61Swanglei out:
766bec5eb61Swanglei 	_leave("= %d", ret);
767bec5eb61Swanglei 	return ERR_PTR(ret);
768bec5eb61Swanglei }
769bec5eb61Swanglei 
770bec5eb61Swanglei /*
7716f8880d8SDavid Howells  * Look up an entry in a directory with @sys substitution.
7726f8880d8SDavid Howells  */
7736f8880d8SDavid Howells static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
7746f8880d8SDavid Howells 				       struct key *key)
7756f8880d8SDavid Howells {
7766f8880d8SDavid Howells 	struct afs_sysnames *subs;
7776f8880d8SDavid Howells 	struct afs_net *net = afs_i2net(dir);
7786f8880d8SDavid Howells 	struct dentry *ret;
7796f8880d8SDavid Howells 	char *buf, *p, *name;
7806f8880d8SDavid Howells 	int len, i;
7816f8880d8SDavid Howells 
7826f8880d8SDavid Howells 	_enter("");
7836f8880d8SDavid Howells 
7846f8880d8SDavid Howells 	ret = ERR_PTR(-ENOMEM);
7856f8880d8SDavid Howells 	p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
7866f8880d8SDavid Howells 	if (!buf)
7876f8880d8SDavid Howells 		goto out_p;
7886f8880d8SDavid Howells 	if (dentry->d_name.len > 4) {
7896f8880d8SDavid Howells 		memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
7906f8880d8SDavid Howells 		p += dentry->d_name.len - 4;
7916f8880d8SDavid Howells 	}
7926f8880d8SDavid Howells 
7936f8880d8SDavid Howells 	/* There is an ordered list of substitutes that we have to try. */
7946f8880d8SDavid Howells 	read_lock(&net->sysnames_lock);
7956f8880d8SDavid Howells 	subs = net->sysnames;
7966f8880d8SDavid Howells 	refcount_inc(&subs->usage);
7976f8880d8SDavid Howells 	read_unlock(&net->sysnames_lock);
7986f8880d8SDavid Howells 
7996f8880d8SDavid Howells 	for (i = 0; i < subs->nr; i++) {
8006f8880d8SDavid Howells 		name = subs->subs[i];
8016f8880d8SDavid Howells 		len = dentry->d_name.len - 4 + strlen(name);
8026f8880d8SDavid Howells 		if (len >= AFSNAMEMAX) {
8036f8880d8SDavid Howells 			ret = ERR_PTR(-ENAMETOOLONG);
8046f8880d8SDavid Howells 			goto out_s;
8056f8880d8SDavid Howells 		}
8066f8880d8SDavid Howells 
8076f8880d8SDavid Howells 		strcpy(p, name);
8086f8880d8SDavid Howells 		ret = lookup_one_len(buf, dentry->d_parent, len);
8096f8880d8SDavid Howells 		if (IS_ERR(ret) || d_is_positive(ret))
8106f8880d8SDavid Howells 			goto out_s;
8116f8880d8SDavid Howells 		dput(ret);
8126f8880d8SDavid Howells 	}
8136f8880d8SDavid Howells 
8146f8880d8SDavid Howells 	/* We don't want to d_add() the @sys dentry here as we don't want to
8156f8880d8SDavid Howells 	 * the cached dentry to hide changes to the sysnames list.
8166f8880d8SDavid Howells 	 */
8176f8880d8SDavid Howells 	ret = NULL;
8186f8880d8SDavid Howells out_s:
8196f8880d8SDavid Howells 	afs_put_sysnames(subs);
8206f8880d8SDavid Howells 	kfree(buf);
8216f8880d8SDavid Howells out_p:
8226f8880d8SDavid Howells 	key_put(key);
8236f8880d8SDavid Howells 	return ret;
8246f8880d8SDavid Howells }
8256f8880d8SDavid Howells 
8266f8880d8SDavid Howells /*
82708e0e7c8SDavid Howells  * look up an entry in a directory
82808e0e7c8SDavid Howells  */
829260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
83000cd8dd3SAl Viro 				 unsigned int flags)
83108e0e7c8SDavid Howells {
8325cf9dd55SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
83308e0e7c8SDavid Howells 	struct inode *inode;
83400d3b7a4SDavid Howells 	struct key *key;
83508e0e7c8SDavid Howells 	int ret;
83608e0e7c8SDavid Howells 
837a455589fSAl Viro 	_enter("{%x:%u},%p{%pd},",
8385cf9dd55SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
839260a9803SDavid Howells 
8402b0143b5SDavid Howells 	ASSERTCMP(d_inode(dentry), ==, NULL);
84108e0e7c8SDavid Howells 
84245222b9eSDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX) {
84308e0e7c8SDavid Howells 		_leave(" = -ENAMETOOLONG");
84408e0e7c8SDavid Howells 		return ERR_PTR(-ENAMETOOLONG);
84508e0e7c8SDavid Howells 	}
84608e0e7c8SDavid Howells 
8475cf9dd55SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
84808e0e7c8SDavid Howells 		_leave(" = -ESTALE");
84908e0e7c8SDavid Howells 		return ERR_PTR(-ESTALE);
85008e0e7c8SDavid Howells 	}
85108e0e7c8SDavid Howells 
8525cf9dd55SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
85300d3b7a4SDavid Howells 	if (IS_ERR(key)) {
85400d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
855e231c2eeSDavid Howells 		return ERR_CAST(key);
85600d3b7a4SDavid Howells 	}
85700d3b7a4SDavid Howells 
8585cf9dd55SDavid Howells 	ret = afs_validate(dvnode, key);
85908e0e7c8SDavid Howells 	if (ret < 0) {
86000d3b7a4SDavid Howells 		key_put(key);
861260a9803SDavid Howells 		_leave(" = %d [val]", ret);
8621da177e4SLinus Torvalds 		return ERR_PTR(ret);
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 
8656f8880d8SDavid Howells 	if (dentry->d_name.len >= 4 &&
8666f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
8676f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
8686f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
8696f8880d8SDavid Howells 	    dentry->d_name.name[dentry->d_name.len - 1] == 's')
8706f8880d8SDavid Howells 		return afs_lookup_atsys(dir, dentry, key);
8716f8880d8SDavid Howells 
872d55b4da4SDavid Howells 	afs_stat_v(dvnode, n_lookup);
8735cf9dd55SDavid Howells 	inode = afs_do_lookup(dir, dentry, key);
8745cf9dd55SDavid Howells 	if (IS_ERR(inode)) {
8755cf9dd55SDavid Howells 		ret = PTR_ERR(inode);
8764d673da1SDavid Howells 		if (ret == -ENOENT) {
8775cf9dd55SDavid Howells 			inode = afs_try_auto_mntpt(dentry, dir);
878bec5eb61Swanglei 			if (!IS_ERR(inode)) {
879bec5eb61Swanglei 				key_put(key);
880bec5eb61Swanglei 				goto success;
881bec5eb61Swanglei 			}
882bec5eb61Swanglei 
883bec5eb61Swanglei 			ret = PTR_ERR(inode);
8844d673da1SDavid Howells 		}
8854d673da1SDavid Howells 
886260a9803SDavid Howells 		key_put(key);
887260a9803SDavid Howells 		if (ret == -ENOENT) {
888260a9803SDavid Howells 			d_add(dentry, NULL);
889260a9803SDavid Howells 			_leave(" = NULL [negative]");
890260a9803SDavid Howells 			return NULL;
891260a9803SDavid Howells 		}
892260a9803SDavid Howells 		_leave(" = %d [do]", ret);
893260a9803SDavid Howells 		return ERR_PTR(ret);
894260a9803SDavid Howells 	}
8955cf9dd55SDavid Howells 	dentry->d_fsdata = (void *)(unsigned long)dvnode->status.data_version;
896260a9803SDavid Howells 
8971da177e4SLinus Torvalds 	/* instantiate the dentry */
89800d3b7a4SDavid Howells 	key_put(key);
89908e0e7c8SDavid Howells 	if (IS_ERR(inode)) {
90008e0e7c8SDavid Howells 		_leave(" = %ld", PTR_ERR(inode));
901e231c2eeSDavid Howells 		return ERR_CAST(inode);
9021da177e4SLinus Torvalds 	}
9031da177e4SLinus Torvalds 
904bec5eb61Swanglei success:
9051da177e4SLinus Torvalds 	d_add(dentry, inode);
9065cf9dd55SDavid Howells 	_leave(" = 0 { ino=%lu v=%u }",
9072b0143b5SDavid Howells 	       d_inode(dentry)->i_ino,
9082b0143b5SDavid Howells 	       d_inode(dentry)->i_generation);
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	return NULL;
911ec26815aSDavid Howells }
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds /*
91437ab6368SDavid Howells  * Look up @cell in a dynroot directory.  This is a substitution for the
91537ab6368SDavid Howells  * local cell name for the net namespace.
91637ab6368SDavid Howells  */
91737ab6368SDavid Howells static struct dentry *afs_lookup_atcell(struct dentry *dentry)
91837ab6368SDavid Howells {
91937ab6368SDavid Howells 	struct afs_cell *cell;
92037ab6368SDavid Howells 	struct afs_net *net = afs_d2net(dentry);
92137ab6368SDavid Howells 	struct dentry *ret;
92237ab6368SDavid Howells 	unsigned int seq = 0;
92337ab6368SDavid Howells 	char *name;
92437ab6368SDavid Howells 	int len;
92537ab6368SDavid Howells 
92637ab6368SDavid Howells 	if (!net->ws_cell)
92737ab6368SDavid Howells 		return ERR_PTR(-ENOENT);
92837ab6368SDavid Howells 
92937ab6368SDavid Howells 	ret = ERR_PTR(-ENOMEM);
93037ab6368SDavid Howells 	name = kmalloc(AFS_MAXCELLNAME + 1, GFP_KERNEL);
93137ab6368SDavid Howells 	if (!name)
93237ab6368SDavid Howells 		goto out_p;
93337ab6368SDavid Howells 
93437ab6368SDavid Howells 	rcu_read_lock();
93537ab6368SDavid Howells 	do {
93637ab6368SDavid Howells 		read_seqbegin_or_lock(&net->cells_lock, &seq);
93737ab6368SDavid Howells 		cell = rcu_dereference_raw(net->ws_cell);
93837ab6368SDavid Howells 		if (cell) {
93937ab6368SDavid Howells 			len = cell->name_len;
94037ab6368SDavid Howells 			memcpy(name, cell->name, len + 1);
94137ab6368SDavid Howells 		}
94237ab6368SDavid Howells 	} while (need_seqretry(&net->cells_lock, seq));
94337ab6368SDavid Howells 	done_seqretry(&net->cells_lock, seq);
94437ab6368SDavid Howells 	rcu_read_unlock();
94537ab6368SDavid Howells 
94637ab6368SDavid Howells 	ret = ERR_PTR(-ENOENT);
94737ab6368SDavid Howells 	if (!cell)
94837ab6368SDavid Howells 		goto out_n;
94937ab6368SDavid Howells 
95037ab6368SDavid Howells 	ret = lookup_one_len(name, dentry->d_parent, len);
95137ab6368SDavid Howells 
95237ab6368SDavid Howells 	/* We don't want to d_add() the @cell dentry here as we don't want to
95337ab6368SDavid Howells 	 * the cached dentry to hide changes to the local cell name.
95437ab6368SDavid Howells 	 */
95537ab6368SDavid Howells 
95637ab6368SDavid Howells out_n:
95737ab6368SDavid Howells 	kfree(name);
95837ab6368SDavid Howells out_p:
95937ab6368SDavid Howells 	return ret;
96037ab6368SDavid Howells }
96137ab6368SDavid Howells 
96237ab6368SDavid Howells /*
9634d673da1SDavid Howells  * Look up an entry in a dynroot directory.
9644d673da1SDavid Howells  */
9654d673da1SDavid Howells static struct dentry *afs_dynroot_lookup(struct inode *dir, struct dentry *dentry,
9664d673da1SDavid Howells 					 unsigned int flags)
9674d673da1SDavid Howells {
9684d673da1SDavid Howells 	struct afs_vnode *vnode;
9694d673da1SDavid Howells 	struct inode *inode;
9704d673da1SDavid Howells 	int ret;
9714d673da1SDavid Howells 
9724d673da1SDavid Howells 	vnode = AFS_FS_I(dir);
9734d673da1SDavid Howells 
9744d673da1SDavid Howells 	_enter("%pd", dentry);
9754d673da1SDavid Howells 
9764d673da1SDavid Howells 	ASSERTCMP(d_inode(dentry), ==, NULL);
9774d673da1SDavid Howells 
9784d673da1SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX) {
9794d673da1SDavid Howells 		_leave(" = -ENAMETOOLONG");
9804d673da1SDavid Howells 		return ERR_PTR(-ENAMETOOLONG);
9814d673da1SDavid Howells 	}
9824d673da1SDavid Howells 
98337ab6368SDavid Howells 	if (dentry->d_name.len == 5 &&
98437ab6368SDavid Howells 	    memcmp(dentry->d_name.name, "@cell", 5) == 0)
98537ab6368SDavid Howells 		return afs_lookup_atcell(dentry);
98637ab6368SDavid Howells 
9875cf9dd55SDavid Howells 	inode = afs_try_auto_mntpt(dentry, dir);
9884d673da1SDavid Howells 	if (IS_ERR(inode)) {
9894d673da1SDavid Howells 		ret = PTR_ERR(inode);
9904d673da1SDavid Howells 		if (ret == -ENOENT) {
9914d673da1SDavid Howells 			d_add(dentry, NULL);
9924d673da1SDavid Howells 			_leave(" = NULL [negative]");
9934d673da1SDavid Howells 			return NULL;
9944d673da1SDavid Howells 		}
9954d673da1SDavid Howells 		_leave(" = %d [do]", ret);
9964d673da1SDavid Howells 		return ERR_PTR(ret);
9974d673da1SDavid Howells 	}
9984d673da1SDavid Howells 
9994d673da1SDavid Howells 	d_add(dentry, inode);
10004d673da1SDavid Howells 	_leave(" = 0 { ino=%lu v=%u }",
10014d673da1SDavid Howells 	       d_inode(dentry)->i_ino, d_inode(dentry)->i_generation);
10024d673da1SDavid Howells 	return NULL;
10034d673da1SDavid Howells }
10044d673da1SDavid Howells 
10054d673da1SDavid Howells /*
10061da177e4SLinus Torvalds  * check that a dentry lookup hit has found a valid entry
10071da177e4SLinus Torvalds  * - NOTE! the hit can be a negative hit too, so we can't assume we have an
10081da177e4SLinus Torvalds  *   inode
10091da177e4SLinus Torvalds  */
10100b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
10111da177e4SLinus Torvalds {
10124d673da1SDavid Howells 	struct afs_super_info *as = dentry->d_sb->s_fs_info;
1013260a9803SDavid Howells 	struct afs_vnode *vnode, *dir;
1014dd0d9a46SArtem Bityutskiy 	struct afs_fid uninitialized_var(fid);
10151da177e4SLinus Torvalds 	struct dentry *parent;
1016c435ee34SDavid Howells 	struct inode *inode;
101700d3b7a4SDavid Howells 	struct key *key;
1018a4ff7401SDavid Howells 	long dir_version, de_version;
10191da177e4SLinus Torvalds 	int ret;
10201da177e4SLinus Torvalds 
10210b728e19SAl Viro 	if (flags & LOOKUP_RCU)
102234286d66SNick Piggin 		return -ECHILD;
102334286d66SNick Piggin 
10244d673da1SDavid Howells 	if (as->dyn_root)
10254d673da1SDavid Howells 		return 1;
10264d673da1SDavid Howells 
1027c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
10282b0143b5SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1029a455589fSAl Viro 		_enter("{v={%x:%u} n=%pd fl=%lx},",
1030a455589fSAl Viro 		       vnode->fid.vid, vnode->fid.vnode, dentry,
1031260a9803SDavid Howells 		       vnode->flags);
1032c435ee34SDavid Howells 	} else {
1033a455589fSAl Viro 		_enter("{neg n=%pd}", dentry);
1034c435ee34SDavid Howells 	}
10351da177e4SLinus Torvalds 
1036260a9803SDavid Howells 	key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
103700d3b7a4SDavid Howells 	if (IS_ERR(key))
103800d3b7a4SDavid Howells 		key = NULL;
103900d3b7a4SDavid Howells 
1040c435ee34SDavid Howells 	if (d_really_is_positive(dentry)) {
1041c435ee34SDavid Howells 		inode = d_inode(dentry);
1042c435ee34SDavid Howells 		if (inode) {
1043c435ee34SDavid Howells 			vnode = AFS_FS_I(inode);
1044c435ee34SDavid Howells 			afs_validate(vnode, key);
1045c435ee34SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1046c435ee34SDavid Howells 				goto out_bad;
1047c435ee34SDavid Howells 		}
1048c435ee34SDavid Howells 	}
1049c435ee34SDavid Howells 
10501da177e4SLinus Torvalds 	/* lock down the parent dentry so we can peer at it */
105108e0e7c8SDavid Howells 	parent = dget_parent(dentry);
10522b0143b5SDavid Howells 	dir = AFS_FS_I(d_inode(parent));
10531da177e4SLinus Torvalds 
1054260a9803SDavid Howells 	/* validate the parent directory */
1055260a9803SDavid Howells 	afs_validate(dir, key);
1056260a9803SDavid Howells 
1057260a9803SDavid Howells 	if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1058a455589fSAl Viro 		_debug("%pd: parent dir deleted", dentry);
1059c435ee34SDavid Howells 		goto out_bad_parent;
10601da177e4SLinus Torvalds 	}
10611da177e4SLinus Torvalds 
1062a4ff7401SDavid Howells 	/* We only need to invalidate a dentry if the server's copy changed
1063a4ff7401SDavid Howells 	 * behind our back.  If we made the change, it's no problem.  Note that
1064a4ff7401SDavid Howells 	 * on a 32-bit system, we only have 32 bits in the dentry to store the
1065a4ff7401SDavid Howells 	 * version.
1066a4ff7401SDavid Howells 	 */
1067a4ff7401SDavid Howells 	dir_version = (long)dir->status.data_version;
1068a4ff7401SDavid Howells 	de_version = (long)dentry->d_fsdata;
1069a4ff7401SDavid Howells 	if (de_version == dir_version)
1070a4ff7401SDavid Howells 		goto out_valid;
1071a4ff7401SDavid Howells 
1072a4ff7401SDavid Howells 	dir_version = (long)dir->invalid_before;
1073a4ff7401SDavid Howells 	if (de_version - dir_version >= 0)
1074a4ff7401SDavid Howells 		goto out_valid;
1075260a9803SDavid Howells 
107608e0e7c8SDavid Howells 	_debug("dir modified");
1077d55b4da4SDavid Howells 	afs_stat_v(dir, n_reval);
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	/* search the directory for this vnode */
10805cf9dd55SDavid Howells 	ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
1081260a9803SDavid Howells 	switch (ret) {
1082260a9803SDavid Howells 	case 0:
1083260a9803SDavid Howells 		/* the filename maps to something */
10842b0143b5SDavid Howells 		if (d_really_is_negative(dentry))
1085c435ee34SDavid Howells 			goto out_bad_parent;
1086c435ee34SDavid Howells 		inode = d_inode(dentry);
1087c435ee34SDavid Howells 		if (is_bad_inode(inode)) {
1088a455589fSAl Viro 			printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1089a455589fSAl Viro 			       dentry);
1090c435ee34SDavid Howells 			goto out_bad_parent;
10911da177e4SLinus Torvalds 		}
10921da177e4SLinus Torvalds 
1093c435ee34SDavid Howells 		vnode = AFS_FS_I(inode);
1094c435ee34SDavid Howells 
10951da177e4SLinus Torvalds 		/* if the vnode ID has changed, then the dirent points to a
10961da177e4SLinus Torvalds 		 * different file */
109708e0e7c8SDavid Howells 		if (fid.vnode != vnode->fid.vnode) {
1098a455589fSAl Viro 			_debug("%pd: dirent changed [%u != %u]",
1099a455589fSAl Viro 			       dentry, fid.vnode,
110008e0e7c8SDavid Howells 			       vnode->fid.vnode);
11011da177e4SLinus Torvalds 			goto not_found;
11021da177e4SLinus Torvalds 		}
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 		/* if the vnode ID uniqifier has changed, then the file has
1105260a9803SDavid Howells 		 * been deleted and replaced, and the original vnode ID has
1106260a9803SDavid Howells 		 * been reused */
110708e0e7c8SDavid Howells 		if (fid.unique != vnode->fid.unique) {
1108a455589fSAl Viro 			_debug("%pd: file deleted (uq %u -> %u I:%u)",
1109a455589fSAl Viro 			       dentry, fid.unique,
11107a224228SJean Noel Cordenner 			       vnode->fid.unique,
1111c435ee34SDavid Howells 			       vnode->vfs_inode.i_generation);
1112c435ee34SDavid Howells 			write_seqlock(&vnode->cb_lock);
111308e0e7c8SDavid Howells 			set_bit(AFS_VNODE_DELETED, &vnode->flags);
1114c435ee34SDavid Howells 			write_sequnlock(&vnode->cb_lock);
1115260a9803SDavid Howells 			goto not_found;
1116260a9803SDavid Howells 		}
1117260a9803SDavid Howells 		goto out_valid;
1118260a9803SDavid Howells 
1119260a9803SDavid Howells 	case -ENOENT:
1120260a9803SDavid Howells 		/* the filename is unknown */
1121a455589fSAl Viro 		_debug("%pd: dirent not found", dentry);
11222b0143b5SDavid Howells 		if (d_really_is_positive(dentry))
1123260a9803SDavid Howells 			goto not_found;
1124260a9803SDavid Howells 		goto out_valid;
1125260a9803SDavid Howells 
1126260a9803SDavid Howells 	default:
1127a455589fSAl Viro 		_debug("failed to iterate dir %pd: %d",
1128a455589fSAl Viro 		       parent, ret);
1129c435ee34SDavid Howells 		goto out_bad_parent;
11301da177e4SLinus Torvalds 	}
113108e0e7c8SDavid Howells 
11321da177e4SLinus Torvalds out_valid:
1133a4ff7401SDavid Howells 	dentry->d_fsdata = (void *)dir_version;
11341da177e4SLinus Torvalds 	dput(parent);
113500d3b7a4SDavid Howells 	key_put(key);
11361da177e4SLinus Torvalds 	_leave(" = 1 [valid]");
11371da177e4SLinus Torvalds 	return 1;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	/* the dirent, if it exists, now points to a different vnode */
11401da177e4SLinus Torvalds not_found:
11411da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
11421da177e4SLinus Torvalds 	dentry->d_flags |= DCACHE_NFSFS_RENAMED;
11431da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
11441da177e4SLinus Torvalds 
1145c435ee34SDavid Howells out_bad_parent:
1146a455589fSAl Viro 	_debug("dropping dentry %pd2", dentry);
11471da177e4SLinus Torvalds 	dput(parent);
1148c435ee34SDavid Howells out_bad:
114900d3b7a4SDavid Howells 	key_put(key);
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	_leave(" = 0 [bad]");
11521da177e4SLinus Torvalds 	return 0;
1153ec26815aSDavid Howells }
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds /*
11561da177e4SLinus Torvalds  * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
11571da177e4SLinus Torvalds  * sleep)
11581da177e4SLinus Torvalds  * - called from dput() when d_count is going to 0.
11591da177e4SLinus Torvalds  * - return 1 to request dentry be unhashed, 0 otherwise
11601da177e4SLinus Torvalds  */
1161fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry)
11621da177e4SLinus Torvalds {
1163a455589fSAl Viro 	_enter("%pd", dentry);
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
11661da177e4SLinus Torvalds 		goto zap;
11671da177e4SLinus Torvalds 
11682b0143b5SDavid Howells 	if (d_really_is_positive(dentry) &&
11692b0143b5SDavid Howells 	    (test_bit(AFS_VNODE_DELETED,   &AFS_FS_I(d_inode(dentry))->flags) ||
11702b0143b5SDavid Howells 	     test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
11711da177e4SLinus Torvalds 		goto zap;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	_leave(" = 0 [keep]");
11741da177e4SLinus Torvalds 	return 0;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds zap:
11771da177e4SLinus Torvalds 	_leave(" = 1 [zap]");
11781da177e4SLinus Torvalds 	return 1;
1179ec26815aSDavid Howells }
1180260a9803SDavid Howells 
1181260a9803SDavid Howells /*
1182260a9803SDavid Howells  * handle dentry release
1183260a9803SDavid Howells  */
1184260a9803SDavid Howells static void afs_d_release(struct dentry *dentry)
1185260a9803SDavid Howells {
1186a455589fSAl Viro 	_enter("%pd", dentry);
1187260a9803SDavid Howells }
1188260a9803SDavid Howells 
1189260a9803SDavid Howells /*
1190d2ddc776SDavid Howells  * Create a new inode for create/mkdir/symlink
1191d2ddc776SDavid Howells  */
1192d2ddc776SDavid Howells static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1193d2ddc776SDavid Howells 				struct dentry *new_dentry,
1194d2ddc776SDavid Howells 				struct afs_fid *newfid,
1195d2ddc776SDavid Howells 				struct afs_file_status *newstatus,
1196d2ddc776SDavid Howells 				struct afs_callback *newcb)
1197d2ddc776SDavid Howells {
1198d2ddc776SDavid Howells 	struct inode *inode;
1199d2ddc776SDavid Howells 
1200d2ddc776SDavid Howells 	if (fc->ac.error < 0)
1201d2ddc776SDavid Howells 		return;
1202d2ddc776SDavid Howells 
1203bc1527dcSDavid Howells 	d_drop(new_dentry);
1204bc1527dcSDavid Howells 
1205d2ddc776SDavid Howells 	inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1206d2ddc776SDavid Howells 			 newfid, newstatus, newcb, fc->cbi);
1207d2ddc776SDavid Howells 	if (IS_ERR(inode)) {
1208d2ddc776SDavid Howells 		/* ENOMEM or EINTR at a really inconvenient time - just abandon
1209d2ddc776SDavid Howells 		 * the new directory on the server.
1210d2ddc776SDavid Howells 		 */
1211d2ddc776SDavid Howells 		fc->ac.error = PTR_ERR(inode);
1212d2ddc776SDavid Howells 		return;
1213d2ddc776SDavid Howells 	}
1214d2ddc776SDavid Howells 
1215bc1527dcSDavid Howells 	d_add(new_dentry, inode);
1216d2ddc776SDavid Howells }
1217d2ddc776SDavid Howells 
1218d2ddc776SDavid Howells /*
1219260a9803SDavid Howells  * create a directory on an AFS filesystem
1220260a9803SDavid Howells  */
122118bb1db3SAl Viro static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1222260a9803SDavid Howells {
1223d2ddc776SDavid Howells 	struct afs_file_status newstatus;
1224d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1225d2ddc776SDavid Howells 	struct afs_callback newcb;
1226d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1227d2ddc776SDavid Howells 	struct afs_fid newfid;
1228260a9803SDavid Howells 	struct key *key;
1229260a9803SDavid Howells 	int ret;
1230260a9803SDavid Howells 
1231d2ddc776SDavid Howells 	mode |= S_IFDIR;
1232260a9803SDavid Howells 
1233a455589fSAl Viro 	_enter("{%x:%u},{%pd},%ho",
1234a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1235260a9803SDavid Howells 
1236260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1237260a9803SDavid Howells 	if (IS_ERR(key)) {
1238260a9803SDavid Howells 		ret = PTR_ERR(key);
1239260a9803SDavid Howells 		goto error;
1240260a9803SDavid Howells 	}
1241260a9803SDavid Howells 
1242d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
1243d2ddc776SDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1244d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
1245d2ddc776SDavid Howells 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1246d2ddc776SDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1247d2ddc776SDavid Howells 				      &newfid, &newstatus, &newcb);
1248d2ddc776SDavid Howells 		}
1249d2ddc776SDavid Howells 
1250d2ddc776SDavid Howells 		afs_check_for_remote_deletion(&fc, fc.vnode);
1251d2ddc776SDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1252d2ddc776SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1253d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1254260a9803SDavid Howells 		if (ret < 0)
1255d2ddc776SDavid Howells 			goto error_key;
12564433b691SDavid Howells 	} else {
12574433b691SDavid Howells 		goto error_key;
1258260a9803SDavid Howells 	}
1259260a9803SDavid Howells 
1260260a9803SDavid Howells 	key_put(key);
1261260a9803SDavid Howells 	_leave(" = 0");
1262260a9803SDavid Howells 	return 0;
1263260a9803SDavid Howells 
1264d2ddc776SDavid Howells error_key:
1265260a9803SDavid Howells 	key_put(key);
1266260a9803SDavid Howells error:
1267260a9803SDavid Howells 	d_drop(dentry);
1268260a9803SDavid Howells 	_leave(" = %d", ret);
1269260a9803SDavid Howells 	return ret;
1270260a9803SDavid Howells }
1271260a9803SDavid Howells 
1272260a9803SDavid Howells /*
1273d2ddc776SDavid Howells  * Remove a subdir from a directory.
1274260a9803SDavid Howells  */
1275d2ddc776SDavid Howells static void afs_dir_remove_subdir(struct dentry *dentry)
1276260a9803SDavid Howells {
12772b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1278d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1279d2ddc776SDavid Howells 
1280260a9803SDavid Howells 		clear_nlink(&vnode->vfs_inode);
1281260a9803SDavid Howells 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1282c435ee34SDavid Howells 		clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1283260a9803SDavid Howells 	}
1284260a9803SDavid Howells }
1285260a9803SDavid Howells 
1286260a9803SDavid Howells /*
1287d2ddc776SDavid Howells  * remove a directory from an AFS filesystem
1288260a9803SDavid Howells  */
1289d2ddc776SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1290260a9803SDavid Howells {
1291d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1292d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1293260a9803SDavid Howells 	struct key *key;
1294260a9803SDavid Howells 	int ret;
1295260a9803SDavid Howells 
1296a455589fSAl Viro 	_enter("{%x:%u},{%pd}",
1297a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1298260a9803SDavid Howells 
1299260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1300260a9803SDavid Howells 	if (IS_ERR(key)) {
1301260a9803SDavid Howells 		ret = PTR_ERR(key);
1302260a9803SDavid Howells 		goto error;
1303260a9803SDavid Howells 	}
1304260a9803SDavid Howells 
1305d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
1306d2ddc776SDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1307d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
1308d2ddc776SDavid Howells 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1309d2ddc776SDavid Howells 			afs_fs_remove(&fc, dentry->d_name.name, true);
1310260a9803SDavid Howells 		}
1311260a9803SDavid Howells 
1312d2ddc776SDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1313d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1314d2ddc776SDavid Howells 		if (ret == 0)
1315d2ddc776SDavid Howells 			afs_dir_remove_subdir(dentry);
1316260a9803SDavid Howells 	}
1317260a9803SDavid Howells 
1318260a9803SDavid Howells 	key_put(key);
1319d2ddc776SDavid Howells error:
1320d2ddc776SDavid Howells 	return ret;
1321d2ddc776SDavid Howells }
1322260a9803SDavid Howells 
1323d2ddc776SDavid Howells /*
1324d2ddc776SDavid Howells  * Remove a link to a file or symlink from a directory.
1325d2ddc776SDavid Howells  *
1326d2ddc776SDavid Howells  * If the file was not deleted due to excess hard links, the fileserver will
1327d2ddc776SDavid Howells  * break the callback promise on the file - if it had one - before it returns
1328d2ddc776SDavid Howells  * to us, and if it was deleted, it won't
1329d2ddc776SDavid Howells  *
1330d2ddc776SDavid Howells  * However, if we didn't have a callback promise outstanding, or it was
1331d2ddc776SDavid Howells  * outstanding on a different server, then it won't break it either...
1332d2ddc776SDavid Howells  */
1333440fbc3aSDavid Howells static int afs_dir_remove_link(struct dentry *dentry, struct key *key,
1334440fbc3aSDavid Howells 			       unsigned long d_version_before,
1335440fbc3aSDavid Howells 			       unsigned long d_version_after)
1336d2ddc776SDavid Howells {
1337440fbc3aSDavid Howells 	bool dir_valid;
1338d2ddc776SDavid Howells 	int ret = 0;
1339d2ddc776SDavid Howells 
1340440fbc3aSDavid Howells 	/* There were no intervening changes on the server if the version
1341440fbc3aSDavid Howells 	 * number we got back was incremented by exactly 1.
1342440fbc3aSDavid Howells 	 */
1343440fbc3aSDavid Howells 	dir_valid = (d_version_after == d_version_before + 1);
1344440fbc3aSDavid Howells 
1345d2ddc776SDavid Howells 	if (d_really_is_positive(dentry)) {
1346d2ddc776SDavid Howells 		struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1347d2ddc776SDavid Howells 
1348440fbc3aSDavid Howells 		if (dir_valid) {
1349440fbc3aSDavid Howells 			drop_nlink(&vnode->vfs_inode);
1350440fbc3aSDavid Howells 			if (vnode->vfs_inode.i_nlink == 0) {
1351440fbc3aSDavid Howells 				set_bit(AFS_VNODE_DELETED, &vnode->flags);
1352440fbc3aSDavid Howells 				clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1353440fbc3aSDavid Howells 			}
1354440fbc3aSDavid Howells 			ret = 0;
1355440fbc3aSDavid Howells 		} else {
1356440fbc3aSDavid Howells 			clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1357440fbc3aSDavid Howells 
1358d2ddc776SDavid Howells 			if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1359d2ddc776SDavid Howells 				kdebug("AFS_VNODE_DELETED");
1360d2ddc776SDavid Howells 
1361d2ddc776SDavid Howells 			ret = afs_validate(vnode, key);
1362d2ddc776SDavid Howells 			if (ret == -ESTALE)
1363d2ddc776SDavid Howells 				ret = 0;
1364440fbc3aSDavid Howells 		}
1365d2ddc776SDavid Howells 		_debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1366d2ddc776SDavid Howells 	}
1367d2ddc776SDavid Howells 
1368d2ddc776SDavid Howells 	return ret;
1369d2ddc776SDavid Howells }
1370d2ddc776SDavid Howells 
1371d2ddc776SDavid Howells /*
1372d2ddc776SDavid Howells  * Remove a file or symlink from an AFS filesystem.
1373d2ddc776SDavid Howells  */
1374d2ddc776SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry)
1375d2ddc776SDavid Howells {
1376d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1377d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
1378d2ddc776SDavid Howells 	struct key *key;
1379440fbc3aSDavid Howells 	unsigned long d_version = (unsigned long)dentry->d_fsdata;
1380d2ddc776SDavid Howells 	int ret;
1381d2ddc776SDavid Howells 
1382d2ddc776SDavid Howells 	_enter("{%x:%u},{%pd}",
1383d2ddc776SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode, dentry);
1384d2ddc776SDavid Howells 
1385d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1386d2ddc776SDavid Howells 		return -ENAMETOOLONG;
1387d2ddc776SDavid Howells 
1388d2ddc776SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1389d2ddc776SDavid Howells 	if (IS_ERR(key)) {
1390d2ddc776SDavid Howells 		ret = PTR_ERR(key);
1391d2ddc776SDavid Howells 		goto error;
1392d2ddc776SDavid Howells 	}
1393d2ddc776SDavid Howells 
1394d2ddc776SDavid Howells 	/* Try to make sure we have a callback promise on the victim. */
1395d2ddc776SDavid Howells 	if (d_really_is_positive(dentry)) {
1396d2ddc776SDavid Howells 		vnode = AFS_FS_I(d_inode(dentry));
1397d2ddc776SDavid Howells 		ret = afs_validate(vnode, key);
1398d2ddc776SDavid Howells 		if (ret < 0)
1399d2ddc776SDavid Howells 			goto error_key;
1400d2ddc776SDavid Howells 	}
1401d2ddc776SDavid Howells 
1402d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
1403d2ddc776SDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1404d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
1405d2ddc776SDavid Howells 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1406d2ddc776SDavid Howells 			afs_fs_remove(&fc, dentry->d_name.name, false);
1407d2ddc776SDavid Howells 		}
1408d2ddc776SDavid Howells 
1409d2ddc776SDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1410d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1411d2ddc776SDavid Howells 		if (ret == 0)
1412440fbc3aSDavid Howells 			ret = afs_dir_remove_link(
1413440fbc3aSDavid Howells 				dentry, key, d_version,
1414440fbc3aSDavid Howells 				(unsigned long)dvnode->status.data_version);
1415d2ddc776SDavid Howells 	}
1416d2ddc776SDavid Howells 
1417d2ddc776SDavid Howells error_key:
1418260a9803SDavid Howells 	key_put(key);
1419260a9803SDavid Howells error:
1420260a9803SDavid Howells 	_leave(" = %d", ret);
1421260a9803SDavid Howells 	return ret;
1422260a9803SDavid Howells }
1423260a9803SDavid Howells 
1424260a9803SDavid Howells /*
1425260a9803SDavid Howells  * create a regular file on an AFS filesystem
1426260a9803SDavid Howells  */
14274acdaf27SAl Viro static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1428ebfc3b49SAl Viro 		      bool excl)
1429260a9803SDavid Howells {
1430d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1431d2ddc776SDavid Howells 	struct afs_file_status newstatus;
1432d2ddc776SDavid Howells 	struct afs_callback newcb;
143343dd388bSColin Ian King 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1434d2ddc776SDavid Howells 	struct afs_fid newfid;
1435260a9803SDavid Howells 	struct key *key;
1436260a9803SDavid Howells 	int ret;
1437260a9803SDavid Howells 
1438d2ddc776SDavid Howells 	mode |= S_IFREG;
1439260a9803SDavid Howells 
1440a455589fSAl Viro 	_enter("{%x:%u},{%pd},%ho,",
1441a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1442260a9803SDavid Howells 
1443d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1444d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1445d2ddc776SDavid Howells 		goto error;
1446d2ddc776SDavid Howells 
1447260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1448260a9803SDavid Howells 	if (IS_ERR(key)) {
1449260a9803SDavid Howells 		ret = PTR_ERR(key);
1450260a9803SDavid Howells 		goto error;
1451260a9803SDavid Howells 	}
1452260a9803SDavid Howells 
1453d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
1454d2ddc776SDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1455d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
1456d2ddc776SDavid Howells 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1457d2ddc776SDavid Howells 			afs_fs_create(&fc, dentry->d_name.name, mode,
1458d2ddc776SDavid Howells 				      &newfid, &newstatus, &newcb);
1459d2ddc776SDavid Howells 		}
1460d2ddc776SDavid Howells 
1461d2ddc776SDavid Howells 		afs_check_for_remote_deletion(&fc, fc.vnode);
1462d2ddc776SDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1463d2ddc776SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1464d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1465260a9803SDavid Howells 		if (ret < 0)
1466d2ddc776SDavid Howells 			goto error_key;
14674433b691SDavid Howells 	} else {
14684433b691SDavid Howells 		goto error_key;
1469260a9803SDavid Howells 	}
1470260a9803SDavid Howells 
1471260a9803SDavid Howells 	key_put(key);
1472260a9803SDavid Howells 	_leave(" = 0");
1473260a9803SDavid Howells 	return 0;
1474260a9803SDavid Howells 
1475d2ddc776SDavid Howells error_key:
1476260a9803SDavid Howells 	key_put(key);
1477260a9803SDavid Howells error:
1478260a9803SDavid Howells 	d_drop(dentry);
1479260a9803SDavid Howells 	_leave(" = %d", ret);
1480260a9803SDavid Howells 	return ret;
1481260a9803SDavid Howells }
1482260a9803SDavid Howells 
1483260a9803SDavid Howells /*
1484260a9803SDavid Howells  * create a hard link between files in an AFS filesystem
1485260a9803SDavid Howells  */
1486260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir,
1487260a9803SDavid Howells 		    struct dentry *dentry)
1488260a9803SDavid Howells {
1489d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1490260a9803SDavid Howells 	struct afs_vnode *dvnode, *vnode;
1491260a9803SDavid Howells 	struct key *key;
1492260a9803SDavid Howells 	int ret;
1493260a9803SDavid Howells 
14942b0143b5SDavid Howells 	vnode = AFS_FS_I(d_inode(from));
1495260a9803SDavid Howells 	dvnode = AFS_FS_I(dir);
1496260a9803SDavid Howells 
1497a455589fSAl Viro 	_enter("{%x:%u},{%x:%u},{%pd}",
1498260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1499260a9803SDavid Howells 	       dvnode->fid.vid, dvnode->fid.vnode,
1500a455589fSAl Viro 	       dentry);
1501260a9803SDavid Howells 
1502d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1503d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1504d2ddc776SDavid Howells 		goto error;
1505d2ddc776SDavid Howells 
1506260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1507260a9803SDavid Howells 	if (IS_ERR(key)) {
1508260a9803SDavid Howells 		ret = PTR_ERR(key);
1509260a9803SDavid Howells 		goto error;
1510260a9803SDavid Howells 	}
1511260a9803SDavid Howells 
1512d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
1513d2ddc776SDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1514d2ddc776SDavid Howells 		if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1515d2ddc776SDavid Howells 			afs_end_vnode_operation(&fc);
1516bc1527dcSDavid Howells 			goto error_key;
1517d2ddc776SDavid Howells 		}
1518260a9803SDavid Howells 
1519d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
1520d2ddc776SDavid Howells 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1521d2ddc776SDavid Howells 			fc.cb_break_2 = vnode->cb_break + vnode->cb_s_break;
1522d2ddc776SDavid Howells 			afs_fs_link(&fc, vnode, dentry->d_name.name);
1523d2ddc776SDavid Howells 		}
1524d2ddc776SDavid Howells 
1525d2ddc776SDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1526d2ddc776SDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
15277de9c6eeSAl Viro 		ihold(&vnode->vfs_inode);
1528260a9803SDavid Howells 		d_instantiate(dentry, &vnode->vfs_inode);
1529d2ddc776SDavid Howells 
1530d2ddc776SDavid Howells 		mutex_unlock(&vnode->io_lock);
1531d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1532d2ddc776SDavid Howells 		if (ret < 0)
1533d2ddc776SDavid Howells 			goto error_key;
15344433b691SDavid Howells 	} else {
15354433b691SDavid Howells 		goto error_key;
1536d2ddc776SDavid Howells 	}
1537d2ddc776SDavid Howells 
1538260a9803SDavid Howells 	key_put(key);
1539260a9803SDavid Howells 	_leave(" = 0");
1540260a9803SDavid Howells 	return 0;
1541260a9803SDavid Howells 
1542d2ddc776SDavid Howells error_key:
1543260a9803SDavid Howells 	key_put(key);
1544260a9803SDavid Howells error:
1545260a9803SDavid Howells 	d_drop(dentry);
1546260a9803SDavid Howells 	_leave(" = %d", ret);
1547260a9803SDavid Howells 	return ret;
1548260a9803SDavid Howells }
1549260a9803SDavid Howells 
1550260a9803SDavid Howells /*
1551260a9803SDavid Howells  * create a symlink in an AFS filesystem
1552260a9803SDavid Howells  */
1553260a9803SDavid Howells static int afs_symlink(struct inode *dir, struct dentry *dentry,
1554260a9803SDavid Howells 		       const char *content)
1555260a9803SDavid Howells {
1556d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1557d2ddc776SDavid Howells 	struct afs_file_status newstatus;
1558d2ddc776SDavid Howells 	struct afs_vnode *dvnode = AFS_FS_I(dir);
1559d2ddc776SDavid Howells 	struct afs_fid newfid;
1560260a9803SDavid Howells 	struct key *key;
1561260a9803SDavid Howells 	int ret;
1562260a9803SDavid Howells 
1563a455589fSAl Viro 	_enter("{%x:%u},{%pd},%s",
1564a455589fSAl Viro 	       dvnode->fid.vid, dvnode->fid.vnode, dentry,
1565260a9803SDavid Howells 	       content);
1566260a9803SDavid Howells 
1567d2ddc776SDavid Howells 	ret = -ENAMETOOLONG;
1568d2ddc776SDavid Howells 	if (dentry->d_name.len >= AFSNAMEMAX)
1569d2ddc776SDavid Howells 		goto error;
1570d2ddc776SDavid Howells 
1571260a9803SDavid Howells 	ret = -EINVAL;
157245222b9eSDavid Howells 	if (strlen(content) >= AFSPATHMAX)
1573260a9803SDavid Howells 		goto error;
1574260a9803SDavid Howells 
1575260a9803SDavid Howells 	key = afs_request_key(dvnode->volume->cell);
1576260a9803SDavid Howells 	if (IS_ERR(key)) {
1577260a9803SDavid Howells 		ret = PTR_ERR(key);
1578260a9803SDavid Howells 		goto error;
1579260a9803SDavid Howells 	}
1580260a9803SDavid Howells 
1581d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
1582d2ddc776SDavid Howells 	if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1583d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
1584d2ddc776SDavid Howells 			fc.cb_break = dvnode->cb_break + dvnode->cb_s_break;
1585d2ddc776SDavid Howells 			afs_fs_symlink(&fc, dentry->d_name.name, content,
1586d2ddc776SDavid Howells 				       &newfid, &newstatus);
1587d2ddc776SDavid Howells 		}
1588d2ddc776SDavid Howells 
1589d2ddc776SDavid Howells 		afs_check_for_remote_deletion(&fc, fc.vnode);
1590d2ddc776SDavid Howells 		afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1591d2ddc776SDavid Howells 		afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
1592d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1593260a9803SDavid Howells 		if (ret < 0)
1594d2ddc776SDavid Howells 			goto error_key;
15954433b691SDavid Howells 	} else {
15964433b691SDavid Howells 		goto error_key;
1597260a9803SDavid Howells 	}
1598260a9803SDavid Howells 
1599260a9803SDavid Howells 	key_put(key);
1600260a9803SDavid Howells 	_leave(" = 0");
1601260a9803SDavid Howells 	return 0;
1602260a9803SDavid Howells 
1603d2ddc776SDavid Howells error_key:
1604260a9803SDavid Howells 	key_put(key);
1605260a9803SDavid Howells error:
1606260a9803SDavid Howells 	d_drop(dentry);
1607260a9803SDavid Howells 	_leave(" = %d", ret);
1608260a9803SDavid Howells 	return ret;
1609260a9803SDavid Howells }
1610260a9803SDavid Howells 
1611260a9803SDavid Howells /*
1612260a9803SDavid Howells  * rename a file in an AFS filesystem and/or move it between directories
1613260a9803SDavid Howells  */
1614260a9803SDavid Howells static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
16151cd66c93SMiklos Szeredi 		      struct inode *new_dir, struct dentry *new_dentry,
16161cd66c93SMiklos Szeredi 		      unsigned int flags)
1617260a9803SDavid Howells {
1618d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
1619260a9803SDavid Howells 	struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1620260a9803SDavid Howells 	struct key *key;
1621260a9803SDavid Howells 	int ret;
1622260a9803SDavid Howells 
16231cd66c93SMiklos Szeredi 	if (flags)
16241cd66c93SMiklos Szeredi 		return -EINVAL;
16251cd66c93SMiklos Szeredi 
16262b0143b5SDavid Howells 	vnode = AFS_FS_I(d_inode(old_dentry));
1627260a9803SDavid Howells 	orig_dvnode = AFS_FS_I(old_dir);
1628260a9803SDavid Howells 	new_dvnode = AFS_FS_I(new_dir);
1629260a9803SDavid Howells 
1630a455589fSAl Viro 	_enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
1631260a9803SDavid Howells 	       orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1632260a9803SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode,
1633260a9803SDavid Howells 	       new_dvnode->fid.vid, new_dvnode->fid.vnode,
1634a455589fSAl Viro 	       new_dentry);
1635260a9803SDavid Howells 
1636260a9803SDavid Howells 	key = afs_request_key(orig_dvnode->volume->cell);
1637260a9803SDavid Howells 	if (IS_ERR(key)) {
1638260a9803SDavid Howells 		ret = PTR_ERR(key);
1639260a9803SDavid Howells 		goto error;
1640260a9803SDavid Howells 	}
1641260a9803SDavid Howells 
1642d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
1643d2ddc776SDavid Howells 	if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
1644d2ddc776SDavid Howells 		if (orig_dvnode != new_dvnode) {
1645d2ddc776SDavid Howells 			if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1646d2ddc776SDavid Howells 				afs_end_vnode_operation(&fc);
1647bc1527dcSDavid Howells 				goto error_key;
1648d2ddc776SDavid Howells 			}
1649d2ddc776SDavid Howells 		}
1650d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
1651d2ddc776SDavid Howells 			fc.cb_break = orig_dvnode->cb_break + orig_dvnode->cb_s_break;
1652d2ddc776SDavid Howells 			fc.cb_break_2 = new_dvnode->cb_break + new_dvnode->cb_s_break;
1653d2ddc776SDavid Howells 			afs_fs_rename(&fc, old_dentry->d_name.name,
1654d2ddc776SDavid Howells 				      new_dvnode, new_dentry->d_name.name);
1655d2ddc776SDavid Howells 		}
1656d2ddc776SDavid Howells 
1657d2ddc776SDavid Howells 		afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
1658d2ddc776SDavid Howells 		afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
1659d2ddc776SDavid Howells 		if (orig_dvnode != new_dvnode)
1660d2ddc776SDavid Howells 			mutex_unlock(&new_dvnode->io_lock);
1661d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
1662260a9803SDavid Howells 		if (ret < 0)
1663d2ddc776SDavid Howells 			goto error_key;
1664d2ddc776SDavid Howells 	}
1665d2ddc776SDavid Howells 
1666d2ddc776SDavid Howells error_key:
1667260a9803SDavid Howells 	key_put(key);
1668260a9803SDavid Howells error:
1669260a9803SDavid Howells 	_leave(" = %d", ret);
1670260a9803SDavid Howells 	return ret;
1671260a9803SDavid Howells }
1672