xref: /openbmc/linux/fs/nfs/dir.c (revision a656db998785324a818005bcf71bae6dcbbb3cf5)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  nfs directory handling functions
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
91da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
101da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
111da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
121da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
131da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
141da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
151da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
161da177e4SLinus Torvalds  *              layer (iput() and friends).
171da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/time.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/stat.h>
231da177e4SLinus Torvalds #include <linux/fcntl.h>
241da177e4SLinus Torvalds #include <linux/string.h>
251da177e4SLinus Torvalds #include <linux/kernel.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/mm.h>
281da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
291da177e4SLinus Torvalds #include <linux/nfs_fs.h>
301da177e4SLinus Torvalds #include <linux/nfs_mount.h>
311da177e4SLinus Torvalds #include <linux/pagemap.h>
321da177e4SLinus Torvalds #include <linux/smp_lock.h>
331da177e4SLinus Torvalds #include <linux/namei.h>
341da177e4SLinus Torvalds 
354ce79717STrond Myklebust #include "nfs4_fs.h"
361da177e4SLinus Torvalds #include "delegation.h"
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define NFS_PARANOIA 1
391da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
421da177e4SLinus Torvalds static int nfs_readdir(struct file *, void *, filldir_t);
431da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
441da177e4SLinus Torvalds static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
451da177e4SLinus Torvalds static int nfs_mkdir(struct inode *, struct dentry *, int);
461da177e4SLinus Torvalds static int nfs_rmdir(struct inode *, struct dentry *);
471da177e4SLinus Torvalds static int nfs_unlink(struct inode *, struct dentry *);
481da177e4SLinus Torvalds static int nfs_symlink(struct inode *, struct dentry *, const char *);
491da177e4SLinus Torvalds static int nfs_link(struct dentry *, struct inode *, struct dentry *);
501da177e4SLinus Torvalds static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
511da177e4SLinus Torvalds static int nfs_rename(struct inode *, struct dentry *,
521da177e4SLinus Torvalds 		      struct inode *, struct dentry *);
531da177e4SLinus Torvalds static int nfs_fsync_dir(struct file *, struct dentry *, int);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds struct file_operations nfs_dir_operations = {
561da177e4SLinus Torvalds 	.read		= generic_read_dir,
571da177e4SLinus Torvalds 	.readdir	= nfs_readdir,
581da177e4SLinus Torvalds 	.open		= nfs_opendir,
591da177e4SLinus Torvalds 	.release	= nfs_release,
601da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
611da177e4SLinus Torvalds };
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds struct inode_operations nfs_dir_inode_operations = {
641da177e4SLinus Torvalds 	.create		= nfs_create,
651da177e4SLinus Torvalds 	.lookup		= nfs_lookup,
661da177e4SLinus Torvalds 	.link		= nfs_link,
671da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
681da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
691da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
701da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
711da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
721da177e4SLinus Torvalds 	.rename		= nfs_rename,
731da177e4SLinus Torvalds 	.permission	= nfs_permission,
741da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
751da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
761da177e4SLinus Torvalds };
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
811da177e4SLinus Torvalds struct inode_operations nfs4_dir_inode_operations = {
821da177e4SLinus Torvalds 	.create		= nfs_create,
831da177e4SLinus Torvalds 	.lookup		= nfs_atomic_lookup,
841da177e4SLinus Torvalds 	.link		= nfs_link,
851da177e4SLinus Torvalds 	.unlink		= nfs_unlink,
861da177e4SLinus Torvalds 	.symlink	= nfs_symlink,
871da177e4SLinus Torvalds 	.mkdir		= nfs_mkdir,
881da177e4SLinus Torvalds 	.rmdir		= nfs_rmdir,
891da177e4SLinus Torvalds 	.mknod		= nfs_mknod,
901da177e4SLinus Torvalds 	.rename		= nfs_rename,
911da177e4SLinus Torvalds 	.permission	= nfs_permission,
921da177e4SLinus Torvalds 	.getattr	= nfs_getattr,
931da177e4SLinus Torvalds 	.setattr	= nfs_setattr,
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds #endif /* CONFIG_NFS_V4 */
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /*
991da177e4SLinus Torvalds  * Open file
1001da177e4SLinus Torvalds  */
1011da177e4SLinus Torvalds static int
1021da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	int res = 0;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	lock_kernel();
1071da177e4SLinus Torvalds 	/* Call generic open code in order to cache credentials */
1081da177e4SLinus Torvalds 	if (!res)
1091da177e4SLinus Torvalds 		res = nfs_open(inode, filp);
1101da177e4SLinus Torvalds 	unlock_kernel();
1111da177e4SLinus Torvalds 	return res;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
1151da177e4SLinus Torvalds typedef struct {
1161da177e4SLinus Torvalds 	struct file	*file;
1171da177e4SLinus Torvalds 	struct page	*page;
1181da177e4SLinus Torvalds 	unsigned long	page_index;
1191da177e4SLinus Torvalds 	u32		*ptr;
1201da177e4SLinus Torvalds 	u64		target;
1211da177e4SLinus Torvalds 	struct nfs_entry *entry;
1221da177e4SLinus Torvalds 	decode_dirent_t	decode;
1231da177e4SLinus Torvalds 	int		plus;
1241da177e4SLinus Torvalds 	int		error;
1251da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /* Now we cache directories properly, by stuffing the dirent
1281da177e4SLinus Torvalds  * data directly in the page cache.
1291da177e4SLinus Torvalds  *
1301da177e4SLinus Torvalds  * Inode invalidation due to refresh etc. takes care of
1311da177e4SLinus Torvalds  * _everything_, no sloppy entry flushing logic, no extraneous
1321da177e4SLinus Torvalds  * copying, network direct to page cache, the way it was meant
1331da177e4SLinus Torvalds  * to be.
1341da177e4SLinus Torvalds  *
1351da177e4SLinus Torvalds  * NOTE: Dirent information verification is done always by the
1361da177e4SLinus Torvalds  *	 page-in of the RPC reply, nowhere else, this simplies
1371da177e4SLinus Torvalds  *	 things substantially.
1381da177e4SLinus Torvalds  */
1391da177e4SLinus Torvalds static
1401da177e4SLinus Torvalds int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	struct file	*file = desc->file;
1431da177e4SLinus Torvalds 	struct inode	*inode = file->f_dentry->d_inode;
1441da177e4SLinus Torvalds 	struct rpc_cred	*cred = nfs_file_cred(file);
1451da177e4SLinus Torvalds 	unsigned long	timestamp;
1461da177e4SLinus Torvalds 	int		error;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds  again:
1511da177e4SLinus Torvalds 	timestamp = jiffies;
1521da177e4SLinus Torvalds 	error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->entry->cookie, page,
1531da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
1541da177e4SLinus Torvalds 	if (error < 0) {
1551da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
1561da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
1571da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
1581da177e4SLinus Torvalds 			NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
1591da177e4SLinus Torvalds 			desc->plus = 0;
1601da177e4SLinus Torvalds 			goto again;
1611da177e4SLinus Torvalds 		}
1621da177e4SLinus Torvalds 		goto error;
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds 	SetPageUptodate(page);
1651da177e4SLinus Torvalds 	NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
1661da177e4SLinus Torvalds 	/* Ensure consistent page alignment of the data.
1671da177e4SLinus Torvalds 	 * Note: assumes we have exclusive access to this mapping either
168*a656db99STrond Myklebust 	 *	 through inode->i_sem or some other mechanism.
1691da177e4SLinus Torvalds 	 */
170*a656db99STrond Myklebust 	if (page->index == 0)
171*a656db99STrond Myklebust 		invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1);
1721da177e4SLinus Torvalds 	unlock_page(page);
1731da177e4SLinus Torvalds 	return 0;
1741da177e4SLinus Torvalds  error:
1751da177e4SLinus Torvalds 	SetPageError(page);
1761da177e4SLinus Torvalds 	unlock_page(page);
1771da177e4SLinus Torvalds 	nfs_zap_caches(inode);
1781da177e4SLinus Torvalds 	desc->error = error;
1791da177e4SLinus Torvalds 	return -EIO;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds static inline
1831da177e4SLinus Torvalds int dir_decode(nfs_readdir_descriptor_t *desc)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	u32	*p = desc->ptr;
1861da177e4SLinus Torvalds 	p = desc->decode(p, desc->entry, desc->plus);
1871da177e4SLinus Torvalds 	if (IS_ERR(p))
1881da177e4SLinus Torvalds 		return PTR_ERR(p);
1891da177e4SLinus Torvalds 	desc->ptr = p;
1901da177e4SLinus Torvalds 	return 0;
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds static inline
1941da177e4SLinus Torvalds void dir_page_release(nfs_readdir_descriptor_t *desc)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	kunmap(desc->page);
1971da177e4SLinus Torvalds 	page_cache_release(desc->page);
1981da177e4SLinus Torvalds 	desc->page = NULL;
1991da177e4SLinus Torvalds 	desc->ptr = NULL;
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds /*
2031da177e4SLinus Torvalds  * Given a pointer to a buffer that has already been filled by a call
2041da177e4SLinus Torvalds  * to readdir, find the next entry.
2051da177e4SLinus Torvalds  *
2061da177e4SLinus Torvalds  * If the end of the buffer has been reached, return -EAGAIN, if not,
2071da177e4SLinus Torvalds  * return the offset within the buffer of the next entry to be
2081da177e4SLinus Torvalds  * read.
2091da177e4SLinus Torvalds  */
2101da177e4SLinus Torvalds static inline
2111da177e4SLinus Torvalds int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	struct nfs_entry *entry = desc->entry;
2141da177e4SLinus Torvalds 	int		loop_count = 0,
2151da177e4SLinus Torvalds 			status;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	while((status = dir_decode(desc)) == 0) {
2181da177e4SLinus Torvalds 		dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie);
2191da177e4SLinus Torvalds 		if (entry->prev_cookie == desc->target)
2201da177e4SLinus Torvalds 			break;
2211da177e4SLinus Torvalds 		if (loop_count++ > 200) {
2221da177e4SLinus Torvalds 			loop_count = 0;
2231da177e4SLinus Torvalds 			schedule();
2241da177e4SLinus Torvalds 		}
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: find_dirent() returns %d\n", status);
2271da177e4SLinus Torvalds 	return status;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds /*
2311da177e4SLinus Torvalds  * Find the given page, and call find_dirent() in order to try to
2321da177e4SLinus Torvalds  * return the next entry.
2331da177e4SLinus Torvalds  */
2341da177e4SLinus Torvalds static inline
2351da177e4SLinus Torvalds int find_dirent_page(nfs_readdir_descriptor_t *desc)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	struct inode	*inode = desc->file->f_dentry->d_inode;
2381da177e4SLinus Torvalds 	struct page	*page;
2391da177e4SLinus Torvalds 	int		status;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	page = read_cache_page(inode->i_mapping, desc->page_index,
2441da177e4SLinus Torvalds 			       (filler_t *)nfs_readdir_filler, desc);
2451da177e4SLinus Torvalds 	if (IS_ERR(page)) {
2461da177e4SLinus Torvalds 		status = PTR_ERR(page);
2471da177e4SLinus Torvalds 		goto out;
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 	if (!PageUptodate(page))
2501da177e4SLinus Torvalds 		goto read_error;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	/* NOTE: Someone else may have changed the READDIRPLUS flag */
2531da177e4SLinus Torvalds 	desc->page = page;
2541da177e4SLinus Torvalds 	desc->ptr = kmap(page);		/* matching kunmap in nfs_do_filldir */
2551da177e4SLinus Torvalds 	status = find_dirent(desc, page);
2561da177e4SLinus Torvalds 	if (status < 0)
2571da177e4SLinus Torvalds 		dir_page_release(desc);
2581da177e4SLinus Torvalds  out:
2591da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status);
2601da177e4SLinus Torvalds 	return status;
2611da177e4SLinus Torvalds  read_error:
2621da177e4SLinus Torvalds 	page_cache_release(page);
2631da177e4SLinus Torvalds 	return -EIO;
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds /*
2671da177e4SLinus Torvalds  * Recurse through the page cache pages, and return a
2681da177e4SLinus Torvalds  * filled nfs_entry structure of the next directory entry if possible.
2691da177e4SLinus Torvalds  *
2701da177e4SLinus Torvalds  * The target for the search is 'desc->target'.
2711da177e4SLinus Torvalds  */
2721da177e4SLinus Torvalds static inline
2731da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	int		loop_count = 0;
2761da177e4SLinus Torvalds 	int		res;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target);
2791da177e4SLinus Torvalds 	for (;;) {
2801da177e4SLinus Torvalds 		res = find_dirent_page(desc);
2811da177e4SLinus Torvalds 		if (res != -EAGAIN)
2821da177e4SLinus Torvalds 			break;
2831da177e4SLinus Torvalds 		/* Align to beginning of next page */
2841da177e4SLinus Torvalds 		desc->page_index ++;
2851da177e4SLinus Torvalds 		if (loop_count++ > 200) {
2861da177e4SLinus Torvalds 			loop_count = 0;
2871da177e4SLinus Torvalds 			schedule();
2881da177e4SLinus Torvalds 		}
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res);
2911da177e4SLinus Torvalds 	return res;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static inline unsigned int dt_type(struct inode *inode)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	return (inode->i_mode >> 12) & 15;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds /*
3021da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
3031da177e4SLinus Torvalds  */
3041da177e4SLinus Torvalds static
3051da177e4SLinus Torvalds int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
3061da177e4SLinus Torvalds 		   filldir_t filldir)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	struct file	*file = desc->file;
3091da177e4SLinus Torvalds 	struct nfs_entry *entry = desc->entry;
3101da177e4SLinus Torvalds 	struct dentry	*dentry = NULL;
3111da177e4SLinus Torvalds 	unsigned long	fileid;
3121da177e4SLinus Torvalds 	int		loop_count = 0,
3131da177e4SLinus Torvalds 			res;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	for(;;) {
3181da177e4SLinus Torvalds 		unsigned d_type = DT_UNKNOWN;
3191da177e4SLinus Torvalds 		/* Note: entry->prev_cookie contains the cookie for
3201da177e4SLinus Torvalds 		 *	 retrieving the current dirent on the server */
3211da177e4SLinus Torvalds 		fileid = nfs_fileid_to_ino_t(entry->ino);
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 		/* Get a dentry if we have one */
3241da177e4SLinus Torvalds 		if (dentry != NULL)
3251da177e4SLinus Torvalds 			dput(dentry);
3261da177e4SLinus Torvalds 		dentry = nfs_readdir_lookup(desc);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 		/* Use readdirplus info */
3291da177e4SLinus Torvalds 		if (dentry != NULL && dentry->d_inode != NULL) {
3301da177e4SLinus Torvalds 			d_type = dt_type(dentry->d_inode);
3311da177e4SLinus Torvalds 			fileid = dentry->d_inode->i_ino;
3321da177e4SLinus Torvalds 		}
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 		res = filldir(dirent, entry->name, entry->len,
3351da177e4SLinus Torvalds 			      entry->prev_cookie, fileid, d_type);
3361da177e4SLinus Torvalds 		if (res < 0)
3371da177e4SLinus Torvalds 			break;
3381da177e4SLinus Torvalds 		file->f_pos = desc->target = entry->cookie;
3391da177e4SLinus Torvalds 		if (dir_decode(desc) != 0) {
3401da177e4SLinus Torvalds 			desc->page_index ++;
3411da177e4SLinus Torvalds 			break;
3421da177e4SLinus Torvalds 		}
3431da177e4SLinus Torvalds 		if (loop_count++ > 200) {
3441da177e4SLinus Torvalds 			loop_count = 0;
3451da177e4SLinus Torvalds 			schedule();
3461da177e4SLinus Torvalds 		}
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 	dir_page_release(desc);
3491da177e4SLinus Torvalds 	if (dentry != NULL)
3501da177e4SLinus Torvalds 		dput(dentry);
3511da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res);
3521da177e4SLinus Torvalds 	return res;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds /*
3561da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
3571da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
3581da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
3591da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
3601da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
3611da177e4SLinus Torvalds  *
3621da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
3631da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
3641da177e4SLinus Torvalds  *	 we should already have a complete representation of the
3651da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
3661da177e4SLinus Torvalds  */
3671da177e4SLinus Torvalds static inline
3681da177e4SLinus Torvalds int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
3691da177e4SLinus Torvalds 		     filldir_t filldir)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds 	struct file	*file = desc->file;
3721da177e4SLinus Torvalds 	struct inode	*inode = file->f_dentry->d_inode;
3731da177e4SLinus Torvalds 	struct rpc_cred	*cred = nfs_file_cred(file);
3741da177e4SLinus Torvalds 	struct page	*page = NULL;
3751da177e4SLinus Torvalds 	int		status;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
3801da177e4SLinus Torvalds 	if (!page) {
3811da177e4SLinus Torvalds 		status = -ENOMEM;
3821da177e4SLinus Torvalds 		goto out;
3831da177e4SLinus Torvalds 	}
3841da177e4SLinus Torvalds 	desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->target,
3851da177e4SLinus Torvalds 						page,
3861da177e4SLinus Torvalds 						NFS_SERVER(inode)->dtsize,
3871da177e4SLinus Torvalds 						desc->plus);
3881da177e4SLinus Torvalds 	NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
3891da177e4SLinus Torvalds 	desc->page = page;
3901da177e4SLinus Torvalds 	desc->ptr = kmap(page);		/* matching kunmap in nfs_do_filldir */
3911da177e4SLinus Torvalds 	if (desc->error >= 0) {
3921da177e4SLinus Torvalds 		if ((status = dir_decode(desc)) == 0)
3931da177e4SLinus Torvalds 			desc->entry->prev_cookie = desc->target;
3941da177e4SLinus Torvalds 	} else
3951da177e4SLinus Torvalds 		status = -EIO;
3961da177e4SLinus Torvalds 	if (status < 0)
3971da177e4SLinus Torvalds 		goto out_release;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	status = nfs_do_filldir(desc, dirent, filldir);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/* Reset read descriptor so it searches the page cache from
4021da177e4SLinus Torvalds 	 * the start upon the next call to readdir_search_pagecache() */
4031da177e4SLinus Torvalds 	desc->page_index = 0;
4041da177e4SLinus Torvalds 	desc->entry->cookie = desc->entry->prev_cookie = 0;
4051da177e4SLinus Torvalds 	desc->entry->eof = 0;
4061da177e4SLinus Torvalds  out:
4071da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status);
4081da177e4SLinus Torvalds 	return status;
4091da177e4SLinus Torvalds  out_release:
4101da177e4SLinus Torvalds 	dir_page_release(desc);
4111da177e4SLinus Torvalds 	goto out;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds /* The file offset position is now represented as a true offset into the
4151da177e4SLinus Torvalds  * page cache as is the case in most of the other filesystems.
4161da177e4SLinus Torvalds  */
4171da177e4SLinus Torvalds static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	struct dentry	*dentry = filp->f_dentry;
4201da177e4SLinus Torvalds 	struct inode	*inode = dentry->d_inode;
4211da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
4221da177e4SLinus Torvalds 			*desc = &my_desc;
4231da177e4SLinus Torvalds 	struct nfs_entry my_entry;
4241da177e4SLinus Torvalds 	struct nfs_fh	 fh;
4251da177e4SLinus Torvalds 	struct nfs_fattr fattr;
4261da177e4SLinus Torvalds 	long		res;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	lock_kernel();
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
4311da177e4SLinus Torvalds 	if (res < 0) {
4321da177e4SLinus Torvalds 		unlock_kernel();
4331da177e4SLinus Torvalds 		return res;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	/*
4371da177e4SLinus Torvalds 	 * filp->f_pos points to the file offset in the page cache.
4381da177e4SLinus Torvalds 	 * but if the cache has meanwhile been zapped, we need to
4391da177e4SLinus Torvalds 	 * read from the last dirent to revalidate f_pos
4401da177e4SLinus Torvalds 	 * itself.
4411da177e4SLinus Torvalds 	 */
4421da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	desc->file = filp;
4451da177e4SLinus Torvalds 	desc->target = filp->f_pos;
4461da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
4471da177e4SLinus Torvalds 	desc->plus = NFS_USE_READDIRPLUS(inode);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	my_entry.cookie = my_entry.prev_cookie = 0;
4501da177e4SLinus Torvalds 	my_entry.eof = 0;
4511da177e4SLinus Torvalds 	my_entry.fh = &fh;
4521da177e4SLinus Torvalds 	my_entry.fattr = &fattr;
4531da177e4SLinus Torvalds 	desc->entry = &my_entry;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	while(!desc->entry->eof) {
4561da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
4571da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
4581da177e4SLinus Torvalds 			/* This means either end of directory */
4591da177e4SLinus Torvalds 			if (desc->entry->cookie != desc->target) {
4601da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
4611da177e4SLinus Torvalds 				res = uncached_readdir(desc, dirent, filldir);
4621da177e4SLinus Torvalds 				if (res >= 0)
4631da177e4SLinus Torvalds 					continue;
4641da177e4SLinus Torvalds 			}
4651da177e4SLinus Torvalds 			res = 0;
4661da177e4SLinus Torvalds 			break;
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
4691da177e4SLinus Torvalds 			NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
4701da177e4SLinus Torvalds 			nfs_zap_caches(inode);
4711da177e4SLinus Torvalds 			desc->plus = 0;
4721da177e4SLinus Torvalds 			desc->entry->eof = 0;
4731da177e4SLinus Torvalds 			continue;
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 		if (res < 0)
4761da177e4SLinus Torvalds 			break;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 		res = nfs_do_filldir(desc, dirent, filldir);
4791da177e4SLinus Torvalds 		if (res < 0) {
4801da177e4SLinus Torvalds 			res = 0;
4811da177e4SLinus Torvalds 			break;
4821da177e4SLinus Torvalds 		}
4831da177e4SLinus Torvalds 	}
4841da177e4SLinus Torvalds 	unlock_kernel();
4851da177e4SLinus Torvalds 	if (desc->error < 0)
4861da177e4SLinus Torvalds 		return desc->error;
4871da177e4SLinus Torvalds 	if (res < 0)
4881da177e4SLinus Torvalds 		return res;
4891da177e4SLinus Torvalds 	return 0;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds /*
4931da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
4941da177e4SLinus Torvalds  * is a dummy operation.
4951da177e4SLinus Torvalds  */
4961da177e4SLinus Torvalds int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
4971da177e4SLinus Torvalds {
4981da177e4SLinus Torvalds 	return 0;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /*
5021da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
5031da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
5041da177e4SLinus Torvalds  * and may need to be looked up again.
5051da177e4SLinus Torvalds  */
5061da177e4SLinus Torvalds static inline int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
5091da177e4SLinus Torvalds 		return 1;
5101da177e4SLinus Torvalds 	if ((NFS_FLAGS(dir) & NFS_INO_INVALID_ATTR) != 0
5111da177e4SLinus Torvalds 			|| nfs_attribute_timeout(dir))
5121da177e4SLinus Torvalds 		return 0;
5131da177e4SLinus Torvalds 	return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata);
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	dentry->d_fsdata = (void *)verf;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds /*
5221da177e4SLinus Torvalds  * Whenever an NFS operation succeeds, we know that the dentry
5231da177e4SLinus Torvalds  * is valid, so we update the revalidation timestamp.
5241da177e4SLinus Torvalds  */
5251da177e4SLinus Torvalds static inline void nfs_renew_times(struct dentry * dentry)
5261da177e4SLinus Torvalds {
5271da177e4SLinus Torvalds 	dentry->d_time = jiffies;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301d6757fbSTrond Myklebust /*
5311d6757fbSTrond Myklebust  * Return the intent data that applies to this particular path component
5321d6757fbSTrond Myklebust  *
5331d6757fbSTrond Myklebust  * Note that the current set of intents only apply to the very last
5341d6757fbSTrond Myklebust  * component of the path.
5351d6757fbSTrond Myklebust  * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
5361d6757fbSTrond Myklebust  */
5371d6757fbSTrond Myklebust static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
5381d6757fbSTrond Myklebust {
5391d6757fbSTrond Myklebust 	if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
5401d6757fbSTrond Myklebust 		return 0;
5411d6757fbSTrond Myklebust 	return nd->flags & mask;
5421d6757fbSTrond Myklebust }
5431d6757fbSTrond Myklebust 
5441d6757fbSTrond Myklebust /*
5451d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
5461d6757fbSTrond Myklebust  *
5471d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
5481d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
5491d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
5501d6757fbSTrond Myklebust  *
5511d6757fbSTrond Myklebust  */
5521da177e4SLinus Torvalds static inline
5531da177e4SLinus Torvalds int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	if (nd != NULL) {
5581da177e4SLinus Torvalds 		/* VFS wants an on-the-wire revalidation */
5591d6757fbSTrond Myklebust 		if (nd->flags & LOOKUP_REVAL)
5601da177e4SLinus Torvalds 			goto out_force;
5611da177e4SLinus Torvalds 		/* This is an open(2) */
5621d6757fbSTrond Myklebust 		if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
5631da177e4SLinus Torvalds 				!(server->flags & NFS_MOUNT_NOCTO))
5641da177e4SLinus Torvalds 			goto out_force;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 	return nfs_revalidate_inode(server, inode);
5671da177e4SLinus Torvalds out_force:
5681da177e4SLinus Torvalds 	return __nfs_revalidate_inode(server, inode);
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds /*
5721da177e4SLinus Torvalds  * We judge how long we want to trust negative
5731da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
5741da177e4SLinus Torvalds  *
5751da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
5761da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
5771da177e4SLinus Torvalds  */
5781da177e4SLinus Torvalds static inline
5791da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
5801da177e4SLinus Torvalds 		       struct nameidata *nd)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	/* Don't revalidate a negative dentry if we're creating a new file */
5831d6757fbSTrond Myklebust 	if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
5841da177e4SLinus Torvalds 		return 0;
5851da177e4SLinus Torvalds 	return !nfs_check_verifier(dir, dentry);
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /*
5891da177e4SLinus Torvalds  * This is called every time the dcache has a lookup hit,
5901da177e4SLinus Torvalds  * and we should check whether we can really trust that
5911da177e4SLinus Torvalds  * lookup.
5921da177e4SLinus Torvalds  *
5931da177e4SLinus Torvalds  * NOTE! The hit can be a negative hit too, don't assume
5941da177e4SLinus Torvalds  * we have an inode!
5951da177e4SLinus Torvalds  *
5961da177e4SLinus Torvalds  * If the parent directory is seen to have changed, we throw out the
5971da177e4SLinus Torvalds  * cached dentry and do a new lookup.
5981da177e4SLinus Torvalds  */
5991da177e4SLinus Torvalds static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
6001da177e4SLinus Torvalds {
6011da177e4SLinus Torvalds 	struct inode *dir;
6021da177e4SLinus Torvalds 	struct inode *inode;
6031da177e4SLinus Torvalds 	struct dentry *parent;
6041da177e4SLinus Torvalds 	int error;
6051da177e4SLinus Torvalds 	struct nfs_fh fhandle;
6061da177e4SLinus Torvalds 	struct nfs_fattr fattr;
6071da177e4SLinus Torvalds 	unsigned long verifier;
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	parent = dget_parent(dentry);
6101da177e4SLinus Torvalds 	lock_kernel();
6111da177e4SLinus Torvalds 	dir = parent->d_inode;
6121da177e4SLinus Torvalds 	inode = dentry->d_inode;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	if (!inode) {
6151da177e4SLinus Torvalds 		if (nfs_neg_need_reval(dir, dentry, nd))
6161da177e4SLinus Torvalds 			goto out_bad;
6171da177e4SLinus Torvalds 		goto out_valid;
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	if (is_bad_inode(inode)) {
6211da177e4SLinus Torvalds 		dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n",
6221da177e4SLinus Torvalds 			dentry->d_parent->d_name.name, dentry->d_name.name);
6231da177e4SLinus Torvalds 		goto out_bad;
6241da177e4SLinus Torvalds 	}
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	/* Revalidate parent directory attribute cache */
6271da177e4SLinus Torvalds 	if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
6281da177e4SLinus Torvalds 		goto out_zap_parent;
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	/* Force a full look up iff the parent directory has changed */
6311da177e4SLinus Torvalds 	if (nfs_check_verifier(dir, dentry)) {
6321da177e4SLinus Torvalds 		if (nfs_lookup_verify_inode(inode, nd))
6331da177e4SLinus Torvalds 			goto out_zap_parent;
6341da177e4SLinus Torvalds 		goto out_valid;
6351da177e4SLinus Torvalds 	}
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (NFS_STALE(inode))
6381da177e4SLinus Torvalds 		goto out_bad;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	verifier = nfs_save_change_attribute(dir);
6411da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
6421da177e4SLinus Torvalds 	if (error)
6431da177e4SLinus Torvalds 		goto out_bad;
6441da177e4SLinus Torvalds 	if (nfs_compare_fh(NFS_FH(inode), &fhandle))
6451da177e4SLinus Torvalds 		goto out_bad;
6461da177e4SLinus Torvalds 	if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
6471da177e4SLinus Torvalds 		goto out_bad;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	nfs_renew_times(dentry);
6501da177e4SLinus Torvalds 	nfs_set_verifier(dentry, verifier);
6511da177e4SLinus Torvalds  out_valid:
6521da177e4SLinus Torvalds 	unlock_kernel();
6531da177e4SLinus Torvalds 	dput(parent);
6541da177e4SLinus Torvalds 	return 1;
6551da177e4SLinus Torvalds out_zap_parent:
6561da177e4SLinus Torvalds 	nfs_zap_caches(dir);
6571da177e4SLinus Torvalds  out_bad:
6581da177e4SLinus Torvalds 	NFS_CACHEINV(dir);
6591da177e4SLinus Torvalds 	if (inode && S_ISDIR(inode->i_mode)) {
6601da177e4SLinus Torvalds 		/* Purge readdir caches. */
6611da177e4SLinus Torvalds 		nfs_zap_caches(inode);
6621da177e4SLinus Torvalds 		/* If we have submounts, don't unhash ! */
6631da177e4SLinus Torvalds 		if (have_submounts(dentry))
6641da177e4SLinus Torvalds 			goto out_valid;
6651da177e4SLinus Torvalds 		shrink_dcache_parent(dentry);
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 	d_drop(dentry);
6681da177e4SLinus Torvalds 	unlock_kernel();
6691da177e4SLinus Torvalds 	dput(parent);
6701da177e4SLinus Torvalds 	return 0;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds /*
6741da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
6751da177e4SLinus Torvalds  */
6761da177e4SLinus Torvalds static int nfs_dentry_delete(struct dentry *dentry)
6771da177e4SLinus Torvalds {
6781da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
6791da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
6801da177e4SLinus Torvalds 		dentry->d_flags);
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
6831da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
6841da177e4SLinus Torvalds 		return 1;
6851da177e4SLinus Torvalds 	}
6861da177e4SLinus Torvalds 	if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
6871da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
6881da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
6891da177e4SLinus Torvalds 		return 1;
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 	return 0;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds /*
6961da177e4SLinus Torvalds  * Called when the dentry loses inode.
6971da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
6981da177e4SLinus Torvalds  */
6991da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
7021da177e4SLinus Torvalds 		lock_kernel();
7031da177e4SLinus Torvalds 		inode->i_nlink--;
7041da177e4SLinus Torvalds 		nfs_complete_unlink(dentry);
7051da177e4SLinus Torvalds 		unlock_kernel();
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 	/* When creating a negative dentry, we want to renew d_time */
7081da177e4SLinus Torvalds 	nfs_renew_times(dentry);
7091da177e4SLinus Torvalds 	iput(inode);
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds struct dentry_operations nfs_dentry_operations = {
7131da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
7141da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
7151da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
7161da177e4SLinus Torvalds };
7171da177e4SLinus Torvalds 
7181d6757fbSTrond Myklebust /*
7191d6757fbSTrond Myklebust  * Use intent information to check whether or not we're going to do
7201d6757fbSTrond Myklebust  * an O_EXCL create using this path component.
7211d6757fbSTrond Myklebust  */
7221da177e4SLinus Torvalds static inline
7231da177e4SLinus Torvalds int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
7241da177e4SLinus Torvalds {
7251da177e4SLinus Torvalds 	if (NFS_PROTO(dir)->version == 2)
7261da177e4SLinus Torvalds 		return 0;
7271d6757fbSTrond Myklebust 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
7281da177e4SLinus Torvalds 		return 0;
7291da177e4SLinus Torvalds 	return (nd->intent.open.flags & O_EXCL) != 0;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	struct dentry *res;
7351da177e4SLinus Torvalds 	struct inode *inode = NULL;
7361da177e4SLinus Torvalds 	int error;
7371da177e4SLinus Torvalds 	struct nfs_fh fhandle;
7381da177e4SLinus Torvalds 	struct nfs_fattr fattr;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: lookup(%s/%s)\n",
7411da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	res = ERR_PTR(-ENAMETOOLONG);
7441da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
7451da177e4SLinus Torvalds 		goto out;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	res = ERR_PTR(-ENOMEM);
7481da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	lock_kernel();
7511da177e4SLinus Torvalds 	/* Revalidate parent directory attribute cache */
7521da177e4SLinus Torvalds 	error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
7531da177e4SLinus Torvalds 	if (error < 0) {
7541da177e4SLinus Torvalds 		res = ERR_PTR(error);
7551da177e4SLinus Torvalds 		goto out_unlock;
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	/* If we're doing an exclusive create, optimize away the lookup */
7591da177e4SLinus Torvalds 	if (nfs_is_exclusive_create(dir, nd))
7601da177e4SLinus Torvalds 		goto no_entry;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
7631da177e4SLinus Torvalds 	if (error == -ENOENT)
7641da177e4SLinus Torvalds 		goto no_entry;
7651da177e4SLinus Torvalds 	if (error < 0) {
7661da177e4SLinus Torvalds 		res = ERR_PTR(error);
7671da177e4SLinus Torvalds 		goto out_unlock;
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds 	res = ERR_PTR(-EACCES);
7701da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
7711da177e4SLinus Torvalds 	if (!inode)
7721da177e4SLinus Torvalds 		goto out_unlock;
7731da177e4SLinus Torvalds no_entry:
7741da177e4SLinus Torvalds 	res = d_add_unique(dentry, inode);
7751da177e4SLinus Torvalds 	if (res != NULL)
7761da177e4SLinus Torvalds 		dentry = res;
7771da177e4SLinus Torvalds 	nfs_renew_times(dentry);
7781da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
7791da177e4SLinus Torvalds out_unlock:
7801da177e4SLinus Torvalds 	unlock_kernel();
7811da177e4SLinus Torvalds out:
7821da177e4SLinus Torvalds 	return res;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds #ifdef CONFIG_NFS_V4
7861da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *, struct nameidata *);
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds struct dentry_operations nfs4_dentry_operations = {
7891da177e4SLinus Torvalds 	.d_revalidate	= nfs_open_revalidate,
7901da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
7911da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
7921da177e4SLinus Torvalds };
7931da177e4SLinus Torvalds 
7941d6757fbSTrond Myklebust /*
7951d6757fbSTrond Myklebust  * Use intent information to determine whether we need to substitute
7961d6757fbSTrond Myklebust  * the NFSv4-style stateful OPEN for the LOOKUP call
7971d6757fbSTrond Myklebust  */
7981da177e4SLinus Torvalds static int is_atomic_open(struct inode *dir, struct nameidata *nd)
7991da177e4SLinus Torvalds {
8001d6757fbSTrond Myklebust 	if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
8011da177e4SLinus Torvalds 		return 0;
8021da177e4SLinus Torvalds 	/* NFS does not (yet) have a stateful open for directories */
8031da177e4SLinus Torvalds 	if (nd->flags & LOOKUP_DIRECTORY)
8041da177e4SLinus Torvalds 		return 0;
8051da177e4SLinus Torvalds 	/* Are we trying to write to a read only partition? */
8061da177e4SLinus Torvalds 	if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
8071da177e4SLinus Torvalds 		return 0;
8081da177e4SLinus Torvalds 	return 1;
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	struct dentry *res = NULL;
8141da177e4SLinus Torvalds 	struct inode *inode = NULL;
8151da177e4SLinus Torvalds 	int error;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	/* Check that we are indeed trying to open this file */
8181da177e4SLinus Torvalds 	if (!is_atomic_open(dir, nd))
8191da177e4SLinus Torvalds 		goto no_open;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
8221da177e4SLinus Torvalds 		res = ERR_PTR(-ENAMETOOLONG);
8231da177e4SLinus Torvalds 		goto out;
8241da177e4SLinus Torvalds 	}
8251da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	/* Let vfs_create() deal with O_EXCL */
8281da177e4SLinus Torvalds 	if (nd->intent.open.flags & O_EXCL)
8291da177e4SLinus Torvalds 		goto no_entry;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	/* Open the file on the server */
8321da177e4SLinus Torvalds 	lock_kernel();
8331da177e4SLinus Torvalds 	/* Revalidate parent directory attribute cache */
8341da177e4SLinus Torvalds 	error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
8351da177e4SLinus Torvalds 	if (error < 0) {
8361da177e4SLinus Torvalds 		res = ERR_PTR(error);
8371da177e4SLinus Torvalds 		goto out;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	if (nd->intent.open.flags & O_CREAT) {
8411da177e4SLinus Torvalds 		nfs_begin_data_update(dir);
8421da177e4SLinus Torvalds 		inode = nfs4_atomic_open(dir, dentry, nd);
8431da177e4SLinus Torvalds 		nfs_end_data_update(dir);
8441da177e4SLinus Torvalds 	} else
8451da177e4SLinus Torvalds 		inode = nfs4_atomic_open(dir, dentry, nd);
8461da177e4SLinus Torvalds 	unlock_kernel();
8471da177e4SLinus Torvalds 	if (IS_ERR(inode)) {
8481da177e4SLinus Torvalds 		error = PTR_ERR(inode);
8491da177e4SLinus Torvalds 		switch (error) {
8501da177e4SLinus Torvalds 			/* Make a negative dentry */
8511da177e4SLinus Torvalds 			case -ENOENT:
8521da177e4SLinus Torvalds 				inode = NULL;
8531da177e4SLinus Torvalds 				break;
8541da177e4SLinus Torvalds 			/* This turned out not to be a regular file */
8551da177e4SLinus Torvalds 			case -ELOOP:
8561da177e4SLinus Torvalds 				if (!(nd->intent.open.flags & O_NOFOLLOW))
8571da177e4SLinus Torvalds 					goto no_open;
8581da177e4SLinus Torvalds 			/* case -EISDIR: */
8591da177e4SLinus Torvalds 			/* case -EINVAL: */
8601da177e4SLinus Torvalds 			default:
8611da177e4SLinus Torvalds 				res = ERR_PTR(error);
8621da177e4SLinus Torvalds 				goto out;
8631da177e4SLinus Torvalds 		}
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds no_entry:
8661da177e4SLinus Torvalds 	res = d_add_unique(dentry, inode);
8671da177e4SLinus Torvalds 	if (res != NULL)
8681da177e4SLinus Torvalds 		dentry = res;
8691da177e4SLinus Torvalds 	nfs_renew_times(dentry);
8701da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
8711da177e4SLinus Torvalds out:
8721da177e4SLinus Torvalds 	return res;
8731da177e4SLinus Torvalds no_open:
8741da177e4SLinus Torvalds 	return nfs_lookup(dir, dentry, nd);
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
8781da177e4SLinus Torvalds {
8791da177e4SLinus Torvalds 	struct dentry *parent = NULL;
8801da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
8811da177e4SLinus Torvalds 	struct inode *dir;
8821da177e4SLinus Torvalds 	unsigned long verifier;
8831da177e4SLinus Torvalds 	int openflags, ret = 0;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	parent = dget_parent(dentry);
8861da177e4SLinus Torvalds 	dir = parent->d_inode;
8871da177e4SLinus Torvalds 	if (!is_atomic_open(dir, nd))
8881da177e4SLinus Torvalds 		goto no_open;
8891da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
8901da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
8911da177e4SLinus Torvalds 	 */
8921da177e4SLinus Torvalds 	if (inode == NULL)
8931da177e4SLinus Torvalds 		goto out;
8941da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
8951da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
8961da177e4SLinus Torvalds 		goto no_open;
8971da177e4SLinus Torvalds 	openflags = nd->intent.open.flags;
8981da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
8991da177e4SLinus Torvalds 	if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
9001da177e4SLinus Torvalds 		goto no_open;
9011da177e4SLinus Torvalds 	/* We can't create new files, or truncate existing ones here */
9021da177e4SLinus Torvalds 	openflags &= ~(O_CREAT|O_TRUNC);
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	/*
9051da177e4SLinus Torvalds 	 * Note: we're not holding inode->i_sem and so may be racing with
9061da177e4SLinus Torvalds 	 * operations that change the directory. We therefore save the
9071da177e4SLinus Torvalds 	 * change attribute *before* we do the RPC call.
9081da177e4SLinus Torvalds 	 */
9091da177e4SLinus Torvalds 	lock_kernel();
9101da177e4SLinus Torvalds 	verifier = nfs_save_change_attribute(dir);
9111da177e4SLinus Torvalds 	ret = nfs4_open_revalidate(dir, dentry, openflags);
9121da177e4SLinus Torvalds 	if (!ret)
9131da177e4SLinus Torvalds 		nfs_set_verifier(dentry, verifier);
9141da177e4SLinus Torvalds 	unlock_kernel();
9151da177e4SLinus Torvalds out:
9161da177e4SLinus Torvalds 	dput(parent);
9171da177e4SLinus Torvalds 	if (!ret)
9181da177e4SLinus Torvalds 		d_drop(dentry);
9191da177e4SLinus Torvalds 	return ret;
9201da177e4SLinus Torvalds no_open:
9211da177e4SLinus Torvalds 	dput(parent);
9221da177e4SLinus Torvalds 	if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
9231da177e4SLinus Torvalds 		return 1;
9241da177e4SLinus Torvalds 	return nfs_lookup_revalidate(dentry, nd);
9251da177e4SLinus Torvalds }
9261da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
9291da177e4SLinus Torvalds {
9301da177e4SLinus Torvalds 	struct dentry *parent = desc->file->f_dentry;
9311da177e4SLinus Torvalds 	struct inode *dir = parent->d_inode;
9321da177e4SLinus Torvalds 	struct nfs_entry *entry = desc->entry;
9331da177e4SLinus Torvalds 	struct dentry *dentry, *alias;
9341da177e4SLinus Torvalds 	struct qstr name = {
9351da177e4SLinus Torvalds 		.name = entry->name,
9361da177e4SLinus Torvalds 		.len = entry->len,
9371da177e4SLinus Torvalds 	};
9381da177e4SLinus Torvalds 	struct inode *inode;
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	switch (name.len) {
9411da177e4SLinus Torvalds 		case 2:
9421da177e4SLinus Torvalds 			if (name.name[0] == '.' && name.name[1] == '.')
9431da177e4SLinus Torvalds 				return dget_parent(parent);
9441da177e4SLinus Torvalds 			break;
9451da177e4SLinus Torvalds 		case 1:
9461da177e4SLinus Torvalds 			if (name.name[0] == '.')
9471da177e4SLinus Torvalds 				return dget(parent);
9481da177e4SLinus Torvalds 	}
9491da177e4SLinus Torvalds 	name.hash = full_name_hash(name.name, name.len);
9501da177e4SLinus Torvalds 	dentry = d_lookup(parent, &name);
9511da177e4SLinus Torvalds 	if (dentry != NULL)
9521da177e4SLinus Torvalds 		return dentry;
9531da177e4SLinus Torvalds 	if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
9541da177e4SLinus Torvalds 		return NULL;
9551da177e4SLinus Torvalds 	/* Note: caller is already holding the dir->i_sem! */
9561da177e4SLinus Torvalds 	dentry = d_alloc(parent, &name);
9571da177e4SLinus Torvalds 	if (dentry == NULL)
9581da177e4SLinus Torvalds 		return NULL;
9591da177e4SLinus Torvalds 	dentry->d_op = NFS_PROTO(dir)->dentry_ops;
9601da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
9611da177e4SLinus Torvalds 	if (!inode) {
9621da177e4SLinus Torvalds 		dput(dentry);
9631da177e4SLinus Torvalds 		return NULL;
9641da177e4SLinus Torvalds 	}
9651da177e4SLinus Torvalds 	alias = d_add_unique(dentry, inode);
9661da177e4SLinus Torvalds 	if (alias != NULL) {
9671da177e4SLinus Torvalds 		dput(dentry);
9681da177e4SLinus Torvalds 		dentry = alias;
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds 	nfs_renew_times(dentry);
9711da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
9721da177e4SLinus Torvalds 	return dentry;
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds /*
9761da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
9771da177e4SLinus Torvalds  */
9781da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
9791da177e4SLinus Torvalds 				struct nfs_fattr *fattr)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	struct inode *inode;
9821da177e4SLinus Torvalds 	int error = -EACCES;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/* We may have been initialized further down */
9851da177e4SLinus Torvalds 	if (dentry->d_inode)
9861da177e4SLinus Torvalds 		return 0;
9871da177e4SLinus Torvalds 	if (fhandle->size == 0) {
9881da177e4SLinus Torvalds 		struct inode *dir = dentry->d_parent->d_inode;
9891da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
9901da177e4SLinus Torvalds 		if (error)
9911da177e4SLinus Torvalds 			goto out_err;
9921da177e4SLinus Torvalds 	}
9931da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
9941da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
9951da177e4SLinus Torvalds 		error = server->rpc_ops->getattr(server, fhandle, fattr);
9961da177e4SLinus Torvalds 		if (error < 0)
9971da177e4SLinus Torvalds 			goto out_err;
9981da177e4SLinus Torvalds 	}
9991da177e4SLinus Torvalds 	error = -ENOMEM;
10001da177e4SLinus Torvalds 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
10011da177e4SLinus Torvalds 	if (inode == NULL)
10021da177e4SLinus Torvalds 		goto out_err;
10031da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
10041da177e4SLinus Torvalds 	return 0;
10051da177e4SLinus Torvalds out_err:
10061da177e4SLinus Torvalds 	d_drop(dentry);
10071da177e4SLinus Torvalds 	return error;
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds /*
10111da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
10121da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
10131da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
10141da177e4SLinus Torvalds  * reply path made it appear to have failed.
10151da177e4SLinus Torvalds  */
10161da177e4SLinus Torvalds static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
10171da177e4SLinus Torvalds 		struct nameidata *nd)
10181da177e4SLinus Torvalds {
10191da177e4SLinus Torvalds 	struct iattr attr;
10201da177e4SLinus Torvalds 	int error;
10211da177e4SLinus Torvalds 	int open_flags = 0;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: create(%s/%ld, %s\n", dir->i_sb->s_id,
10241da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	attr.ia_mode = mode;
10271da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	if (nd && (nd->flags & LOOKUP_CREATE))
10301da177e4SLinus Torvalds 		open_flags = nd->intent.open.flags;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	lock_kernel();
10331da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
10341da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
10351da177e4SLinus Torvalds 	nfs_end_data_update(dir);
10361da177e4SLinus Torvalds 	if (error != 0)
10371da177e4SLinus Torvalds 		goto out_err;
10381da177e4SLinus Torvalds 	nfs_renew_times(dentry);
10391da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
10401da177e4SLinus Torvalds 	unlock_kernel();
10411da177e4SLinus Torvalds 	return 0;
10421da177e4SLinus Torvalds out_err:
10431da177e4SLinus Torvalds 	unlock_kernel();
10441da177e4SLinus Torvalds 	d_drop(dentry);
10451da177e4SLinus Torvalds 	return error;
10461da177e4SLinus Torvalds }
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds /*
10491da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
10501da177e4SLinus Torvalds  */
10511da177e4SLinus Torvalds static int
10521da177e4SLinus Torvalds nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
10531da177e4SLinus Torvalds {
10541da177e4SLinus Torvalds 	struct iattr attr;
10551da177e4SLinus Torvalds 	int status;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: mknod(%s/%ld, %s\n", dir->i_sb->s_id,
10581da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	if (!new_valid_dev(rdev))
10611da177e4SLinus Torvalds 		return -EINVAL;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	attr.ia_mode = mode;
10641da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	lock_kernel();
10671da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
10681da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
10691da177e4SLinus Torvalds 	nfs_end_data_update(dir);
10701da177e4SLinus Torvalds 	if (status != 0)
10711da177e4SLinus Torvalds 		goto out_err;
10721da177e4SLinus Torvalds 	nfs_renew_times(dentry);
10731da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
10741da177e4SLinus Torvalds 	unlock_kernel();
10751da177e4SLinus Torvalds 	return 0;
10761da177e4SLinus Torvalds out_err:
10771da177e4SLinus Torvalds 	unlock_kernel();
10781da177e4SLinus Torvalds 	d_drop(dentry);
10791da177e4SLinus Torvalds 	return status;
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds /*
10831da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
10841da177e4SLinus Torvalds  */
10851da177e4SLinus Torvalds static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	struct iattr attr;
10881da177e4SLinus Torvalds 	int error;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: mkdir(%s/%ld, %s\n", dir->i_sb->s_id,
10911da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
10941da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	lock_kernel();
10971da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
10981da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
10991da177e4SLinus Torvalds 	nfs_end_data_update(dir);
11001da177e4SLinus Torvalds 	if (error != 0)
11011da177e4SLinus Torvalds 		goto out_err;
11021da177e4SLinus Torvalds 	nfs_renew_times(dentry);
11031da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11041da177e4SLinus Torvalds 	unlock_kernel();
11051da177e4SLinus Torvalds 	return 0;
11061da177e4SLinus Torvalds out_err:
11071da177e4SLinus Torvalds 	d_drop(dentry);
11081da177e4SLinus Torvalds 	unlock_kernel();
11091da177e4SLinus Torvalds 	return error;
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
11131da177e4SLinus Torvalds {
11141da177e4SLinus Torvalds 	int error;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rmdir(%s/%ld, %s\n", dir->i_sb->s_id,
11171da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	lock_kernel();
11201da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
11211da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
11221da177e4SLinus Torvalds 	/* Ensure the VFS deletes this inode */
11231da177e4SLinus Torvalds 	if (error == 0 && dentry->d_inode != NULL)
11241da177e4SLinus Torvalds 		dentry->d_inode->i_nlink = 0;
11251da177e4SLinus Torvalds 	nfs_end_data_update(dir);
11261da177e4SLinus Torvalds 	unlock_kernel();
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	return error;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds 	static unsigned int sillycounter;
11341da177e4SLinus Torvalds 	const int      i_inosize  = sizeof(dir->i_ino)*2;
11351da177e4SLinus Torvalds 	const int      countersize = sizeof(sillycounter)*2;
11361da177e4SLinus Torvalds 	const int      slen       = sizeof(".nfs") + i_inosize + countersize - 1;
11371da177e4SLinus Torvalds 	char           silly[slen+1];
11381da177e4SLinus Torvalds 	struct qstr    qsilly;
11391da177e4SLinus Torvalds 	struct dentry *sdentry;
11401da177e4SLinus Torvalds 	int            error = -EIO;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
11431da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name,
11441da177e4SLinus Torvalds 		atomic_read(&dentry->d_count));
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds #ifdef NFS_PARANOIA
11471da177e4SLinus Torvalds if (!dentry->d_inode)
11481da177e4SLinus Torvalds printk("NFS: silly-renaming %s/%s, negative dentry??\n",
11491da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name);
11501da177e4SLinus Torvalds #endif
11511da177e4SLinus Torvalds 	/*
11521da177e4SLinus Torvalds 	 * We don't allow a dentry to be silly-renamed twice.
11531da177e4SLinus Torvalds 	 */
11541da177e4SLinus Torvalds 	error = -EBUSY;
11551da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
11561da177e4SLinus Torvalds 		goto out;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	sprintf(silly, ".nfs%*.*lx",
11591da177e4SLinus Torvalds 		i_inosize, i_inosize, dentry->d_inode->i_ino);
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	sdentry = NULL;
11621da177e4SLinus Torvalds 	do {
11631da177e4SLinus Torvalds 		char *suffix = silly + slen - countersize;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 		dput(sdentry);
11661da177e4SLinus Torvalds 		sillycounter++;
11671da177e4SLinus Torvalds 		sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 		dfprintk(VFS, "trying to rename %s to %s\n",
11701da177e4SLinus Torvalds 			 dentry->d_name.name, silly);
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 		sdentry = lookup_one_len(silly, dentry->d_parent, slen);
11731da177e4SLinus Torvalds 		/*
11741da177e4SLinus Torvalds 		 * N.B. Better to return EBUSY here ... it could be
11751da177e4SLinus Torvalds 		 * dangerous to delete the file while it's in use.
11761da177e4SLinus Torvalds 		 */
11771da177e4SLinus Torvalds 		if (IS_ERR(sdentry))
11781da177e4SLinus Torvalds 			goto out;
11791da177e4SLinus Torvalds 	} while(sdentry->d_inode != NULL); /* need negative lookup */
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	qsilly.name = silly;
11821da177e4SLinus Torvalds 	qsilly.len  = strlen(silly);
11831da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
11841da177e4SLinus Torvalds 	if (dentry->d_inode) {
11851da177e4SLinus Torvalds 		nfs_begin_data_update(dentry->d_inode);
11861da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
11871da177e4SLinus Torvalds 				dir, &qsilly);
11881da177e4SLinus Torvalds 		nfs_end_data_update(dentry->d_inode);
11891da177e4SLinus Torvalds 	} else
11901da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
11911da177e4SLinus Torvalds 				dir, &qsilly);
11921da177e4SLinus Torvalds 	nfs_end_data_update(dir);
11931da177e4SLinus Torvalds 	if (!error) {
11941da177e4SLinus Torvalds 		nfs_renew_times(dentry);
11951da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
11961da177e4SLinus Torvalds 		d_move(dentry, sdentry);
11971da177e4SLinus Torvalds 		error = nfs_async_unlink(dentry);
11981da177e4SLinus Torvalds  		/* If we return 0 we don't unlink */
11991da177e4SLinus Torvalds 	}
12001da177e4SLinus Torvalds 	dput(sdentry);
12011da177e4SLinus Torvalds out:
12021da177e4SLinus Torvalds 	return error;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds /*
12061da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
12071da177e4SLinus Torvalds  * and after checking that the file has only one user.
12081da177e4SLinus Torvalds  *
12091da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
12101da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
12111da177e4SLinus Torvalds  */
12121da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
12131da177e4SLinus Torvalds {
12141da177e4SLinus Torvalds 	struct inode *dir = dentry->d_parent->d_inode;
12151da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
12161da177e4SLinus Torvalds 	int error = -EBUSY;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
12191da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
12221da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
12231da177e4SLinus Torvalds 		error = 0;
12241da177e4SLinus Torvalds 		goto out;
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
12281da177e4SLinus Torvalds 	if (inode != NULL) {
12291da177e4SLinus Torvalds 		nfs_begin_data_update(inode);
12301da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
12311da177e4SLinus Torvalds 		/* The VFS may want to delete this inode */
12321da177e4SLinus Torvalds 		if (error == 0)
12331da177e4SLinus Torvalds 			inode->i_nlink--;
12341da177e4SLinus Torvalds 		nfs_end_data_update(inode);
12351da177e4SLinus Torvalds 	} else
12361da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
12371da177e4SLinus Torvalds 	nfs_end_data_update(dir);
12381da177e4SLinus Torvalds out:
12391da177e4SLinus Torvalds 	return error;
12401da177e4SLinus Torvalds }
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
12431da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
12441da177e4SLinus Torvalds  *
12451da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
12461da177e4SLinus Torvalds  */
12471da177e4SLinus Torvalds static int nfs_unlink(struct inode *dir, struct dentry *dentry)
12481da177e4SLinus Torvalds {
12491da177e4SLinus Torvalds 	int error;
12501da177e4SLinus Torvalds 	int need_rehash = 0;
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
12531da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name);
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	lock_kernel();
12561da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
12571da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
12581da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) > 1) {
12591da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
12601da177e4SLinus Torvalds 		spin_unlock(&dcache_lock);
12611da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
12621da177e4SLinus Torvalds 		unlock_kernel();
12631da177e4SLinus Torvalds 		return error;
12641da177e4SLinus Torvalds 	}
12651da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
12661da177e4SLinus Torvalds 		__d_drop(dentry);
12671da177e4SLinus Torvalds 		need_rehash = 1;
12681da177e4SLinus Torvalds 	}
12691da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
12701da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
12711da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
12721da177e4SLinus Torvalds 	if (!error) {
12731da177e4SLinus Torvalds 		nfs_renew_times(dentry);
12741da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
12751da177e4SLinus Torvalds 	} else if (need_rehash)
12761da177e4SLinus Torvalds 		d_rehash(dentry);
12771da177e4SLinus Torvalds 	unlock_kernel();
12781da177e4SLinus Torvalds 	return error;
12791da177e4SLinus Torvalds }
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds static int
12821da177e4SLinus Torvalds nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
12831da177e4SLinus Torvalds {
12841da177e4SLinus Torvalds 	struct iattr attr;
12851da177e4SLinus Torvalds 	struct nfs_fattr sym_attr;
12861da177e4SLinus Torvalds 	struct nfs_fh sym_fh;
12871da177e4SLinus Torvalds 	struct qstr qsymname;
12881da177e4SLinus Torvalds 	int error;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
12911da177e4SLinus Torvalds 		dir->i_ino, dentry->d_name.name, symname);
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds #ifdef NFS_PARANOIA
12941da177e4SLinus Torvalds if (dentry->d_inode)
12951da177e4SLinus Torvalds printk("nfs_proc_symlink: %s/%s not negative!\n",
12961da177e4SLinus Torvalds dentry->d_parent->d_name.name, dentry->d_name.name);
12971da177e4SLinus Torvalds #endif
12981da177e4SLinus Torvalds 	/*
12991da177e4SLinus Torvalds 	 * Fill in the sattr for the call.
13001da177e4SLinus Torvalds  	 * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
13011da177e4SLinus Torvalds 	 */
13021da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
13031da177e4SLinus Torvalds 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	qsymname.name = symname;
13061da177e4SLinus Torvalds 	qsymname.len  = strlen(symname);
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	lock_kernel();
13091da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
13101da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
13111da177e4SLinus Torvalds 					  &attr, &sym_fh, &sym_attr);
13121da177e4SLinus Torvalds 	nfs_end_data_update(dir);
13131da177e4SLinus Torvalds 	if (!error) {
13141da177e4SLinus Torvalds 		error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
13151da177e4SLinus Torvalds 	} else {
13161da177e4SLinus Torvalds 		if (error == -EEXIST)
13171da177e4SLinus Torvalds 			printk("nfs_proc_symlink: %s/%s already exists??\n",
13181da177e4SLinus Torvalds 			       dentry->d_parent->d_name.name, dentry->d_name.name);
13191da177e4SLinus Torvalds 		d_drop(dentry);
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds 	unlock_kernel();
13221da177e4SLinus Torvalds 	return error;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds static int
13261da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
13271da177e4SLinus Torvalds {
13281da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
13291da177e4SLinus Torvalds 	int error;
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
13321da177e4SLinus Torvalds 		old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
13331da177e4SLinus Torvalds 		dentry->d_parent->d_name.name, dentry->d_name.name);
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	/*
13361da177e4SLinus Torvalds 	 * Drop the dentry in advance to force a new lookup.
13371da177e4SLinus Torvalds 	 * Since nfs_proc_link doesn't return a file handle,
13381da177e4SLinus Torvalds 	 * we can't use the existing dentry.
13391da177e4SLinus Torvalds 	 */
13401da177e4SLinus Torvalds 	lock_kernel();
13411da177e4SLinus Torvalds 	d_drop(dentry);
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	nfs_begin_data_update(dir);
13441da177e4SLinus Torvalds 	nfs_begin_data_update(inode);
13451da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
13461da177e4SLinus Torvalds 	nfs_end_data_update(inode);
13471da177e4SLinus Torvalds 	nfs_end_data_update(dir);
13481da177e4SLinus Torvalds 	unlock_kernel();
13491da177e4SLinus Torvalds 	return error;
13501da177e4SLinus Torvalds }
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds /*
13531da177e4SLinus Torvalds  * RENAME
13541da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
13551da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
13561da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
13571da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
13581da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
13591da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
13601da177e4SLinus Torvalds  *
13611da177e4SLinus Torvalds  * FIXED.
13621da177e4SLinus Torvalds  *
13631da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
13641da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
13651da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
13661da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
13671da177e4SLinus Torvalds  * using the inode layer
13681da177e4SLinus Torvalds  *
13691da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
13701da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
13711da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
13721da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
13731da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
13741da177e4SLinus Torvalds  * the rename.
13751da177e4SLinus Torvalds  */
13761da177e4SLinus Torvalds static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
13771da177e4SLinus Torvalds 		      struct inode *new_dir, struct dentry *new_dentry)
13781da177e4SLinus Torvalds {
13791da177e4SLinus Torvalds 	struct inode *old_inode = old_dentry->d_inode;
13801da177e4SLinus Torvalds 	struct inode *new_inode = new_dentry->d_inode;
13811da177e4SLinus Torvalds 	struct dentry *dentry = NULL, *rehash = NULL;
13821da177e4SLinus Torvalds 	int error = -EBUSY;
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	/*
13851da177e4SLinus Torvalds 	 * To prevent any new references to the target during the rename,
13861da177e4SLinus Torvalds 	 * we unhash the dentry and free the inode in advance.
13871da177e4SLinus Torvalds 	 */
13881da177e4SLinus Torvalds 	lock_kernel();
13891da177e4SLinus Torvalds 	if (!d_unhashed(new_dentry)) {
13901da177e4SLinus Torvalds 		d_drop(new_dentry);
13911da177e4SLinus Torvalds 		rehash = new_dentry;
13921da177e4SLinus Torvalds 	}
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
13951da177e4SLinus Torvalds 		 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
13961da177e4SLinus Torvalds 		 new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
13971da177e4SLinus Torvalds 		 atomic_read(&new_dentry->d_count));
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	/*
14001da177e4SLinus Torvalds 	 * First check whether the target is busy ... we can't
14011da177e4SLinus Torvalds 	 * safely do _any_ rename if the target is in use.
14021da177e4SLinus Torvalds 	 *
14031da177e4SLinus Torvalds 	 * For files, make a copy of the dentry and then do a
14041da177e4SLinus Torvalds 	 * silly-rename. If the silly-rename succeeds, the
14051da177e4SLinus Torvalds 	 * copied dentry is hashed and becomes the new target.
14061da177e4SLinus Torvalds 	 */
14071da177e4SLinus Torvalds 	if (!new_inode)
14081da177e4SLinus Torvalds 		goto go_ahead;
14091da177e4SLinus Torvalds 	if (S_ISDIR(new_inode->i_mode))
14101da177e4SLinus Torvalds 		goto out;
14111da177e4SLinus Torvalds 	else if (atomic_read(&new_dentry->d_count) > 2) {
14121da177e4SLinus Torvalds 		int err;
14131da177e4SLinus Torvalds 		/* copy the target dentry's name */
14141da177e4SLinus Torvalds 		dentry = d_alloc(new_dentry->d_parent,
14151da177e4SLinus Torvalds 				 &new_dentry->d_name);
14161da177e4SLinus Torvalds 		if (!dentry)
14171da177e4SLinus Torvalds 			goto out;
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 		/* silly-rename the existing target ... */
14201da177e4SLinus Torvalds 		err = nfs_sillyrename(new_dir, new_dentry);
14211da177e4SLinus Torvalds 		if (!err) {
14221da177e4SLinus Torvalds 			new_dentry = rehash = dentry;
14231da177e4SLinus Torvalds 			new_inode = NULL;
14241da177e4SLinus Torvalds 			/* instantiate the replacement target */
14251da177e4SLinus Torvalds 			d_instantiate(new_dentry, NULL);
14261da177e4SLinus Torvalds 		} else if (atomic_read(&new_dentry->d_count) > 1) {
14271da177e4SLinus Torvalds 		/* dentry still busy? */
14281da177e4SLinus Torvalds #ifdef NFS_PARANOIA
14291da177e4SLinus Torvalds 			printk("nfs_rename: target %s/%s busy, d_count=%d\n",
14301da177e4SLinus Torvalds 			       new_dentry->d_parent->d_name.name,
14311da177e4SLinus Torvalds 			       new_dentry->d_name.name,
14321da177e4SLinus Torvalds 			       atomic_read(&new_dentry->d_count));
14331da177e4SLinus Torvalds #endif
14341da177e4SLinus Torvalds 			goto out;
14351da177e4SLinus Torvalds 		}
14361da177e4SLinus Torvalds 	}
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds go_ahead:
14391da177e4SLinus Torvalds 	/*
14401da177e4SLinus Torvalds 	 * ... prune child dentries and writebacks if needed.
14411da177e4SLinus Torvalds 	 */
14421da177e4SLinus Torvalds 	if (atomic_read(&old_dentry->d_count) > 1) {
14431da177e4SLinus Torvalds 		nfs_wb_all(old_inode);
14441da177e4SLinus Torvalds 		shrink_dcache_parent(old_dentry);
14451da177e4SLinus Torvalds 	}
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	if (new_inode)
14481da177e4SLinus Torvalds 		d_delete(new_dentry);
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	nfs_begin_data_update(old_dir);
14511da177e4SLinus Torvalds 	nfs_begin_data_update(new_dir);
14521da177e4SLinus Torvalds 	nfs_begin_data_update(old_inode);
14531da177e4SLinus Torvalds 	error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
14541da177e4SLinus Torvalds 					   new_dir, &new_dentry->d_name);
14551da177e4SLinus Torvalds 	nfs_end_data_update(old_inode);
14561da177e4SLinus Torvalds 	nfs_end_data_update(new_dir);
14571da177e4SLinus Torvalds 	nfs_end_data_update(old_dir);
14581da177e4SLinus Torvalds out:
14591da177e4SLinus Torvalds 	if (rehash)
14601da177e4SLinus Torvalds 		d_rehash(rehash);
14611da177e4SLinus Torvalds 	if (!error) {
14621da177e4SLinus Torvalds 		if (!S_ISDIR(old_inode->i_mode))
14631da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
14641da177e4SLinus Torvalds 		nfs_renew_times(new_dentry);
14651da177e4SLinus Torvalds 		nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir));
14661da177e4SLinus Torvalds 	}
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	/* new dentry created? */
14691da177e4SLinus Torvalds 	if (dentry)
14701da177e4SLinus Torvalds 		dput(dentry);
14711da177e4SLinus Torvalds 	unlock_kernel();
14721da177e4SLinus Torvalds 	return error;
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
14761da177e4SLinus Torvalds {
14771da177e4SLinus Torvalds 	struct nfs_access_entry *cache = &NFS_I(inode)->cache_access;
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	if (cache->cred != cred
14801da177e4SLinus Torvalds 			|| time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode))
14811da177e4SLinus Torvalds 			|| (NFS_FLAGS(inode) & NFS_INO_INVALID_ACCESS))
14821da177e4SLinus Torvalds 		return -ENOENT;
14831da177e4SLinus Torvalds 	memcpy(res, cache, sizeof(*res));
14841da177e4SLinus Torvalds 	return 0;
14851da177e4SLinus Torvalds }
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
14881da177e4SLinus Torvalds {
14891da177e4SLinus Torvalds 	struct nfs_access_entry *cache = &NFS_I(inode)->cache_access;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	if (cache->cred != set->cred) {
14921da177e4SLinus Torvalds 		if (cache->cred)
14931da177e4SLinus Torvalds 			put_rpccred(cache->cred);
14941da177e4SLinus Torvalds 		cache->cred = get_rpccred(set->cred);
14951da177e4SLinus Torvalds 	}
14961da177e4SLinus Torvalds 	NFS_FLAGS(inode) &= ~NFS_INO_INVALID_ACCESS;
14971da177e4SLinus Torvalds 	cache->jiffies = set->jiffies;
14981da177e4SLinus Torvalds 	cache->mask = set->mask;
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
15021da177e4SLinus Torvalds {
15031da177e4SLinus Torvalds 	struct nfs_access_entry cache;
15041da177e4SLinus Torvalds 	int status;
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 	status = nfs_access_get_cached(inode, cred, &cache);
15071da177e4SLinus Torvalds 	if (status == 0)
15081da177e4SLinus Torvalds 		goto out;
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	/* Be clever: ask server to check for all possible rights */
15111da177e4SLinus Torvalds 	cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
15121da177e4SLinus Torvalds 	cache.cred = cred;
15131da177e4SLinus Torvalds 	cache.jiffies = jiffies;
15141da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
15151da177e4SLinus Torvalds 	if (status != 0)
15161da177e4SLinus Torvalds 		return status;
15171da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
15181da177e4SLinus Torvalds out:
15191da177e4SLinus Torvalds 	if ((cache.mask & mask) == mask)
15201da177e4SLinus Torvalds 		return 0;
15211da177e4SLinus Torvalds 	return -EACCES;
15221da177e4SLinus Torvalds }
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
15251da177e4SLinus Torvalds {
15261da177e4SLinus Torvalds 	struct rpc_cred *cred;
15271da177e4SLinus Torvalds 	int res = 0;
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 	if (mask == 0)
15301da177e4SLinus Torvalds 		goto out;
15311da177e4SLinus Torvalds 	/* Is this sys_access() ? */
15321da177e4SLinus Torvalds 	if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
15331da177e4SLinus Torvalds 		goto force_lookup;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
15361da177e4SLinus Torvalds 		case S_IFLNK:
15371da177e4SLinus Torvalds 			goto out;
15381da177e4SLinus Torvalds 		case S_IFREG:
15391da177e4SLinus Torvalds 			/* NFSv4 has atomic_open... */
15401da177e4SLinus Torvalds 			if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
15411da177e4SLinus Torvalds 					&& nd != NULL
15421da177e4SLinus Torvalds 					&& (nd->flags & LOOKUP_OPEN))
15431da177e4SLinus Torvalds 				goto out;
15441da177e4SLinus Torvalds 			break;
15451da177e4SLinus Torvalds 		case S_IFDIR:
15461da177e4SLinus Torvalds 			/*
15471da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
15481da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
15491da177e4SLinus Torvalds 			 */
15501da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
15511da177e4SLinus Torvalds 				goto out;
15521da177e4SLinus Torvalds 	}
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds force_lookup:
15551da177e4SLinus Torvalds 	lock_kernel();
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
15581da177e4SLinus Torvalds 		goto out_notsup;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
15611da177e4SLinus Torvalds 	if (!IS_ERR(cred)) {
15621da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
15631da177e4SLinus Torvalds 		put_rpccred(cred);
15641da177e4SLinus Torvalds 	} else
15651da177e4SLinus Torvalds 		res = PTR_ERR(cred);
15661da177e4SLinus Torvalds 	unlock_kernel();
15671da177e4SLinus Torvalds out:
15681da177e4SLinus Torvalds 	return res;
15691da177e4SLinus Torvalds out_notsup:
15701da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
15711da177e4SLinus Torvalds 	if (res == 0)
15721da177e4SLinus Torvalds 		res = generic_permission(inode, mask, NULL);
15731da177e4SLinus Torvalds 	unlock_kernel();
15741da177e4SLinus Torvalds 	return res;
15751da177e4SLinus Torvalds }
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds /*
15781da177e4SLinus Torvalds  * Local variables:
15791da177e4SLinus Torvalds  *  version-control: t
15801da177e4SLinus Torvalds  *  kept-new-versions: 5
15811da177e4SLinus Torvalds  * End:
15821da177e4SLinus Torvalds  */
1583