xref: /openbmc/linux/fs/nfs/dir.c (revision e762a639816015a70bb1af8ea4baf54f4facb591)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/nfs/dir.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1992  Rick Sladkey
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  nfs directory handling functions
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * 10 Apr 1996	Added silly rename for unlink	--okir
101da177e4SLinus Torvalds  * 28 Sep 1996	Improved directory cache --okir
111da177e4SLinus Torvalds  * 23 Aug 1997  Claus Heine claus@momo.math.rwth-aachen.de
121da177e4SLinus Torvalds  *              Re-implemented silly rename for unlink, newly implemented
131da177e4SLinus Torvalds  *              silly rename for nfs_rename() following the suggestions
141da177e4SLinus Torvalds  *              of Olaf Kirch (okir) found in this file.
151da177e4SLinus Torvalds  *              Following Linus comments on my original hack, this version
161da177e4SLinus Torvalds  *              depends only on the dcache stuff and doesn't touch the inode
171da177e4SLinus Torvalds  *              layer (iput() and friends).
181da177e4SLinus Torvalds  *  6 Jun 1999	Cache readdir lookups in the page cache. -DaveM
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
21ddda8e0aSBryan Schumaker #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/time.h>
231da177e4SLinus Torvalds #include <linux/errno.h>
241da177e4SLinus Torvalds #include <linux/stat.h>
251da177e4SLinus Torvalds #include <linux/fcntl.h>
261da177e4SLinus Torvalds #include <linux/string.h>
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/slab.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
311da177e4SLinus Torvalds #include <linux/nfs_fs.h>
321da177e4SLinus Torvalds #include <linux/nfs_mount.h>
331da177e4SLinus Torvalds #include <linux/pagemap.h>
34873101b3SChuck Lever #include <linux/pagevec.h>
351da177e4SLinus Torvalds #include <linux/namei.h>
3654ceac45SDavid Howells #include <linux/mount.h>
37a0b8cab3SMel Gorman #include <linux/swap.h>
38e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
3904e4bd1cSCatalin Marinas #include <linux/kmemleak.h>
4064c2ce8bSAneesh Kumar K.V #include <linux/xattr.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include "delegation.h"
4391d5b470SChuck Lever #include "iostat.h"
444c30d56eSAdrian Bunk #include "internal.h"
45cd9a1c0eSTrond Myklebust #include "fscache.h"
461da177e4SLinus Torvalds 
47f4ce1299STrond Myklebust #include "nfstrace.h"
48f4ce1299STrond Myklebust 
491da177e4SLinus Torvalds /* #define NFS_DEBUG_VERBOSE 1 */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static int nfs_opendir(struct inode *, struct file *);
52480c2006SBryan Schumaker static int nfs_closedir(struct inode *, struct file *);
5323db8620SAl Viro static int nfs_readdir(struct file *, struct dir_context *);
5402c24a82SJosef Bacik static int nfs_fsync_dir(struct file *, loff_t, loff_t, int);
55f0dd2136STrond Myklebust static loff_t nfs_llseek_dir(struct file *, loff_t, int);
5611de3b11STrond Myklebust static void nfs_readdir_clear_array(struct page*);
571da177e4SLinus Torvalds 
584b6f5d20SArjan van de Ven const struct file_operations nfs_dir_operations = {
59f0dd2136STrond Myklebust 	.llseek		= nfs_llseek_dir,
601da177e4SLinus Torvalds 	.read		= generic_read_dir,
6193a6ab7bSTrond Myklebust 	.iterate_shared	= nfs_readdir,
621da177e4SLinus Torvalds 	.open		= nfs_opendir,
63480c2006SBryan Schumaker 	.release	= nfs_closedir,
641da177e4SLinus Torvalds 	.fsync		= nfs_fsync_dir,
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
6711de3b11STrond Myklebust const struct address_space_operations nfs_dir_aops = {
6811de3b11STrond Myklebust 	.freepage = nfs_readdir_clear_array,
69d1bacf9eSBryan Schumaker };
70d1bacf9eSBryan Schumaker 
71684f39b4SNeilBrown static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir, const struct cred *cred)
72480c2006SBryan Schumaker {
73311324adSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
74480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
75480c2006SBryan Schumaker 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
76480c2006SBryan Schumaker 	if (ctx != NULL) {
778ef2ce3eSBryan Schumaker 		ctx->duped = 0;
78311324adSTrond Myklebust 		ctx->attr_gencount = nfsi->attr_gencount;
79480c2006SBryan Schumaker 		ctx->dir_cookie = 0;
808ef2ce3eSBryan Schumaker 		ctx->dup_cookie = 0;
81684f39b4SNeilBrown 		ctx->cred = get_cred(cred);
82311324adSTrond Myklebust 		spin_lock(&dir->i_lock);
831c341b77STrond Myklebust 		if (list_empty(&nfsi->open_files) &&
841c341b77STrond Myklebust 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
851c341b77STrond Myklebust 			nfsi->cache_validity |= NFS_INO_INVALID_DATA |
861c341b77STrond Myklebust 				NFS_INO_REVAL_FORCED;
87311324adSTrond Myklebust 		list_add(&ctx->list, &nfsi->open_files);
88311324adSTrond Myklebust 		spin_unlock(&dir->i_lock);
89480c2006SBryan Schumaker 		return ctx;
90480c2006SBryan Schumaker 	}
910c030806STrond Myklebust 	return  ERR_PTR(-ENOMEM);
920c030806STrond Myklebust }
93480c2006SBryan Schumaker 
94311324adSTrond Myklebust static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx)
95480c2006SBryan Schumaker {
96311324adSTrond Myklebust 	spin_lock(&dir->i_lock);
97311324adSTrond Myklebust 	list_del(&ctx->list);
98311324adSTrond Myklebust 	spin_unlock(&dir->i_lock);
99684f39b4SNeilBrown 	put_cred(ctx->cred);
100480c2006SBryan Schumaker 	kfree(ctx);
101480c2006SBryan Schumaker }
102480c2006SBryan Schumaker 
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds  * Open file
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds static int
1071da177e4SLinus Torvalds nfs_opendir(struct inode *inode, struct file *filp)
1081da177e4SLinus Torvalds {
109480c2006SBryan Schumaker 	int res = 0;
110480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx;
1111da177e4SLinus Torvalds 
1126de1472fSAl Viro 	dfprintk(FILE, "NFS: open dir(%pD2)\n", filp);
113cc0dd2d1SChuck Lever 
114cc0dd2d1SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSOPEN);
1151e7cb3dcSChuck Lever 
116684f39b4SNeilBrown 	ctx = alloc_nfs_open_dir_context(inode, current_cred());
117480c2006SBryan Schumaker 	if (IS_ERR(ctx)) {
118480c2006SBryan Schumaker 		res = PTR_ERR(ctx);
119480c2006SBryan Schumaker 		goto out;
120480c2006SBryan Schumaker 	}
121480c2006SBryan Schumaker 	filp->private_data = ctx;
122480c2006SBryan Schumaker out:
1231da177e4SLinus Torvalds 	return res;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
126480c2006SBryan Schumaker static int
127480c2006SBryan Schumaker nfs_closedir(struct inode *inode, struct file *filp)
128480c2006SBryan Schumaker {
129a455589fSAl Viro 	put_nfs_open_dir_context(file_inode(filp), filp->private_data);
130480c2006SBryan Schumaker 	return 0;
131480c2006SBryan Schumaker }
132480c2006SBryan Schumaker 
133d1bacf9eSBryan Schumaker struct nfs_cache_array_entry {
134d1bacf9eSBryan Schumaker 	u64 cookie;
135d1bacf9eSBryan Schumaker 	u64 ino;
136d1bacf9eSBryan Schumaker 	struct qstr string;
1370b26a0bfSTrond Myklebust 	unsigned char d_type;
138d1bacf9eSBryan Schumaker };
139d1bacf9eSBryan Schumaker 
140d1bacf9eSBryan Schumaker struct nfs_cache_array {
141d1bacf9eSBryan Schumaker 	u64 last_cookie;
142b1e21c97STrond Myklebust 	unsigned int size;
143b1e21c97STrond Myklebust 	unsigned char page_full : 1,
144b1e21c97STrond Myklebust 		      page_is_eof : 1;
1455601cda8SGustavo A. R. Silva 	struct nfs_cache_array_entry array[];
146d1bacf9eSBryan Schumaker };
147d1bacf9eSBryan Schumaker 
1482e7a4641STrond Myklebust typedef struct nfs_readdir_descriptor {
1491da177e4SLinus Torvalds 	struct file	*file;
1501da177e4SLinus Torvalds 	struct page	*page;
15123db8620SAl Viro 	struct dir_context *ctx;
1521f1d4aa4STrond Myklebust 	pgoff_t		page_index;
1532e7a4641STrond Myklebust 	u64		dir_cookie;
1540aded708STrond Myklebust 	u64		last_cookie;
1552e7a4641STrond Myklebust 	u64		dup_cookie;
156f0dd2136STrond Myklebust 	loff_t		current_index;
15759e356a9STrond Myklebust 	loff_t		prev_index;
158d1bacf9eSBryan Schumaker 
159a1147b82STrond Myklebust 	unsigned long	dir_verifier;
1601f4eab7eSNeil Brown 	unsigned long	timestamp;
1614704f0e2STrond Myklebust 	unsigned long	gencount;
1622e7a4641STrond Myklebust 	unsigned long	attr_gencount;
163d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
1642e7a4641STrond Myklebust 	signed char duped;
165a7a3b1e9SBenjamin Coddington 	bool plus;
166a7a3b1e9SBenjamin Coddington 	bool eof;
1671da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1681da177e4SLinus Torvalds 
1691f1d4aa4STrond Myklebust static void nfs_readdir_array_init(struct nfs_cache_array *array)
1701f1d4aa4STrond Myklebust {
1711f1d4aa4STrond Myklebust 	memset(array, 0, sizeof(struct nfs_cache_array));
1721f1d4aa4STrond Myklebust }
1731f1d4aa4STrond Myklebust 
1741f1d4aa4STrond Myklebust static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie)
1754b310319STrond Myklebust {
1764b310319STrond Myklebust 	struct nfs_cache_array *array;
1774b310319STrond Myklebust 
1784b310319STrond Myklebust 	array = kmap_atomic(page);
1791f1d4aa4STrond Myklebust 	nfs_readdir_array_init(array);
1801f1d4aa4STrond Myklebust 	array->last_cookie = last_cookie;
1814b310319STrond Myklebust 	kunmap_atomic(array);
1824b310319STrond Myklebust }
1834b310319STrond Myklebust 
184d1bacf9eSBryan Schumaker /*
185d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
186d1bacf9eSBryan Schumaker  */
187d1bacf9eSBryan Schumaker static
18811de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
189d1bacf9eSBryan Schumaker {
19011de3b11STrond Myklebust 	struct nfs_cache_array *array;
191d1bacf9eSBryan Schumaker 	int i;
1928cd51a0cSTrond Myklebust 
1932b86ce2dSCong Wang 	array = kmap_atomic(page);
194d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
195d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1961f1d4aa4STrond Myklebust 	nfs_readdir_array_init(array);
1972b86ce2dSCong Wang 	kunmap_atomic(array);
198d1bacf9eSBryan Schumaker }
199d1bacf9eSBryan Schumaker 
200b1e21c97STrond Myklebust static void nfs_readdir_array_set_eof(struct nfs_cache_array *array)
201b1e21c97STrond Myklebust {
202b1e21c97STrond Myklebust 	array->page_is_eof = 1;
203b1e21c97STrond Myklebust 	array->page_full = 1;
204b1e21c97STrond Myklebust }
205b1e21c97STrond Myklebust 
206b1e21c97STrond Myklebust static bool nfs_readdir_array_is_full(struct nfs_cache_array *array)
207b1e21c97STrond Myklebust {
208b1e21c97STrond Myklebust 	return array->page_full;
209b1e21c97STrond Myklebust }
210b1e21c97STrond Myklebust 
211d1bacf9eSBryan Schumaker /*
212d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
213d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
214d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
215d1bacf9eSBryan Schumaker  */
216d1bacf9eSBryan Schumaker static
2174a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
218d1bacf9eSBryan Schumaker {
219d1bacf9eSBryan Schumaker 	string->len = len;
2203803d672STrond Myklebust 	string->name = kmemdup_nul(name, len, GFP_KERNEL);
2214a201d6eSTrond Myklebust 	if (string->name == NULL)
2224a201d6eSTrond Myklebust 		return -ENOMEM;
22304e4bd1cSCatalin Marinas 	/*
22404e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
22504e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
22604e4bd1cSCatalin Marinas 	 */
22704e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2288387ff25SLinus Torvalds 	string->hash = full_name_hash(NULL, name, len);
2294a201d6eSTrond Myklebust 	return 0;
230d1bacf9eSBryan Schumaker }
231d1bacf9eSBryan Schumaker 
232b1e21c97STrond Myklebust /*
233b1e21c97STrond Myklebust  * Check that the next array entry lies entirely within the page bounds
234b1e21c97STrond Myklebust  */
235b1e21c97STrond Myklebust static int nfs_readdir_array_can_expand(struct nfs_cache_array *array)
236b1e21c97STrond Myklebust {
237b1e21c97STrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
238b1e21c97STrond Myklebust 
239b1e21c97STrond Myklebust 	if (array->page_full)
240b1e21c97STrond Myklebust 		return -ENOSPC;
241b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size + 1];
242b1e21c97STrond Myklebust 	if ((char *)cache_entry - (char *)array > PAGE_SIZE) {
243b1e21c97STrond Myklebust 		array->page_full = 1;
244b1e21c97STrond Myklebust 		return -ENOSPC;
245b1e21c97STrond Myklebust 	}
246b1e21c97STrond Myklebust 	return 0;
247b1e21c97STrond Myklebust }
248b1e21c97STrond Myklebust 
249d1bacf9eSBryan Schumaker static
250d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
251d1bacf9eSBryan Schumaker {
2520795bf83SFabian Frederick 	struct nfs_cache_array *array = kmap(page);
2534a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2544a201d6eSTrond Myklebust 	int ret;
2554a201d6eSTrond Myklebust 
256b1e21c97STrond Myklebust 	ret = nfs_readdir_array_can_expand(array);
257b1e21c97STrond Myklebust 	if (ret)
2583020093fSTrond Myklebust 		goto out;
2593020093fSTrond Myklebust 
260b1e21c97STrond Myklebust 	cache_entry = &array->array[array->size];
2614a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2624a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2630b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2644a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2654a201d6eSTrond Myklebust 	if (ret)
2664a201d6eSTrond Myklebust 		goto out;
267d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2688cd51a0cSTrond Myklebust 	array->size++;
26947c716cbSTrond Myklebust 	if (entry->eof != 0)
270b1e21c97STrond Myklebust 		nfs_readdir_array_set_eof(array);
2714a201d6eSTrond Myklebust out:
2720795bf83SFabian Frederick 	kunmap(page);
2734a201d6eSTrond Myklebust 	return ret;
274d1bacf9eSBryan Schumaker }
275d1bacf9eSBryan Schumaker 
2761f1d4aa4STrond Myklebust static struct page *nfs_readdir_page_get_locked(struct address_space *mapping,
2771f1d4aa4STrond Myklebust 						pgoff_t index, u64 last_cookie)
2781f1d4aa4STrond Myklebust {
2791f1d4aa4STrond Myklebust 	struct page *page;
2801f1d4aa4STrond Myklebust 
2811f1d4aa4STrond Myklebust 	page = grab_cache_page(mapping, index);
2821f1d4aa4STrond Myklebust 	if (page && !PageUptodate(page)) {
2831f1d4aa4STrond Myklebust 		nfs_readdir_page_init_array(page, last_cookie);
2841f1d4aa4STrond Myklebust 		if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0)
2851f1d4aa4STrond Myklebust 			nfs_zap_mapping(mapping->host, mapping);
2861f1d4aa4STrond Myklebust 		SetPageUptodate(page);
2871f1d4aa4STrond Myklebust 	}
2881f1d4aa4STrond Myklebust 
2891f1d4aa4STrond Myklebust 	return page;
2901f1d4aa4STrond Myklebust }
2911f1d4aa4STrond Myklebust 
2921f1d4aa4STrond Myklebust static u64 nfs_readdir_page_last_cookie(struct page *page)
2931f1d4aa4STrond Myklebust {
2941f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
2951f1d4aa4STrond Myklebust 	u64 ret;
2961f1d4aa4STrond Myklebust 
2971f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
2981f1d4aa4STrond Myklebust 	ret = array->last_cookie;
2991f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3001f1d4aa4STrond Myklebust 	return ret;
3011f1d4aa4STrond Myklebust }
3021f1d4aa4STrond Myklebust 
3031f1d4aa4STrond Myklebust static bool nfs_readdir_page_needs_filling(struct page *page)
3041f1d4aa4STrond Myklebust {
3051f1d4aa4STrond Myklebust 	struct nfs_cache_array *array;
3061f1d4aa4STrond Myklebust 	bool ret;
3071f1d4aa4STrond Myklebust 
3081f1d4aa4STrond Myklebust 	array = kmap_atomic(page);
3091f1d4aa4STrond Myklebust 	ret = !nfs_readdir_array_is_full(array);
3101f1d4aa4STrond Myklebust 	kunmap_atomic(array);
3111f1d4aa4STrond Myklebust 	return ret;
3121f1d4aa4STrond Myklebust }
3131f1d4aa4STrond Myklebust 
314b1e21c97STrond Myklebust static void nfs_readdir_page_set_eof(struct page *page)
315b1e21c97STrond Myklebust {
316b1e21c97STrond Myklebust 	struct nfs_cache_array *array;
317b1e21c97STrond Myklebust 
318b1e21c97STrond Myklebust 	array = kmap_atomic(page);
319b1e21c97STrond Myklebust 	nfs_readdir_array_set_eof(array);
320b1e21c97STrond Myklebust 	kunmap_atomic(array);
321b1e21c97STrond Myklebust }
322b1e21c97STrond Myklebust 
3233b2a09f1STrond Myklebust static void nfs_readdir_page_unlock_and_put(struct page *page)
3243b2a09f1STrond Myklebust {
3253b2a09f1STrond Myklebust 	unlock_page(page);
3263b2a09f1STrond Myklebust 	put_page(page);
3273b2a09f1STrond Myklebust }
3283b2a09f1STrond Myklebust 
3293b2a09f1STrond Myklebust static struct page *nfs_readdir_page_get_next(struct address_space *mapping,
3303b2a09f1STrond Myklebust 					      pgoff_t index, u64 cookie)
3313b2a09f1STrond Myklebust {
3323b2a09f1STrond Myklebust 	struct page *page;
3333b2a09f1STrond Myklebust 
3343b2a09f1STrond Myklebust 	page = nfs_readdir_page_get_locked(mapping, index, cookie);
3353b2a09f1STrond Myklebust 	if (page) {
3363b2a09f1STrond Myklebust 		if (nfs_readdir_page_last_cookie(page) == cookie)
3373b2a09f1STrond Myklebust 			return page;
3383b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
3393b2a09f1STrond Myklebust 	}
3403b2a09f1STrond Myklebust 	return NULL;
3413b2a09f1STrond Myklebust }
3423b2a09f1STrond Myklebust 
34359e356a9STrond Myklebust static inline
34459e356a9STrond Myklebust int is_32bit_api(void)
34559e356a9STrond Myklebust {
34659e356a9STrond Myklebust #ifdef CONFIG_COMPAT
34759e356a9STrond Myklebust 	return in_compat_syscall();
34859e356a9STrond Myklebust #else
34959e356a9STrond Myklebust 	return (BITS_PER_LONG == 32);
35059e356a9STrond Myklebust #endif
35159e356a9STrond Myklebust }
35259e356a9STrond Myklebust 
35359e356a9STrond Myklebust static
35459e356a9STrond Myklebust bool nfs_readdir_use_cookie(const struct file *filp)
35559e356a9STrond Myklebust {
35659e356a9STrond Myklebust 	if ((filp->f_mode & FMODE_32BITHASH) ||
35759e356a9STrond Myklebust 	    (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api()))
35859e356a9STrond Myklebust 		return false;
35959e356a9STrond Myklebust 	return true;
36059e356a9STrond Myklebust }
36159e356a9STrond Myklebust 
362d1bacf9eSBryan Schumaker static
363d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
364d1bacf9eSBryan Schumaker {
36523db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
366d1bacf9eSBryan Schumaker 	unsigned int index;
367d1bacf9eSBryan Schumaker 
368d1bacf9eSBryan Schumaker 	if (diff < 0)
369d1bacf9eSBryan Schumaker 		goto out_eof;
370d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
371b1e21c97STrond Myklebust 		if (array->page_is_eof)
372d1bacf9eSBryan Schumaker 			goto out_eof;
373d1bacf9eSBryan Schumaker 		return -EAGAIN;
374d1bacf9eSBryan Schumaker 	}
375d1bacf9eSBryan Schumaker 
376d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
3772e7a4641STrond Myklebust 	desc->dir_cookie = array->array[index].cookie;
378d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
379d1bacf9eSBryan Schumaker 	return 0;
380d1bacf9eSBryan Schumaker out_eof:
3816089dd0dSThomas Meyer 	desc->eof = true;
382d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
383d1bacf9eSBryan Schumaker }
384d1bacf9eSBryan Schumaker 
3854db72b40SJeff Layton static bool
3864db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
3874db72b40SJeff Layton {
3884db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
3894db72b40SJeff Layton 		return false;
3904db72b40SJeff Layton 	smp_rmb();
3914db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
3924db72b40SJeff Layton }
3934db72b40SJeff Layton 
394d1bacf9eSBryan Schumaker static
395d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
396d1bacf9eSBryan Schumaker {
397d1bacf9eSBryan Schumaker 	int i;
3988ef2ce3eSBryan Schumaker 	loff_t new_pos;
399d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
400d1bacf9eSBryan Schumaker 
401d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
4022e7a4641STrond Myklebust 		if (array->array[i].cookie == desc->dir_cookie) {
403496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
4040c030806STrond Myklebust 
4058ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
4062e7a4641STrond Myklebust 			if (desc->attr_gencount != nfsi->attr_gencount ||
4074db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
4082e7a4641STrond Myklebust 				desc->duped = 0;
4092e7a4641STrond Myklebust 				desc->attr_gencount = nfsi->attr_gencount;
41059e356a9STrond Myklebust 			} else if (new_pos < desc->prev_index) {
4112e7a4641STrond Myklebust 				if (desc->duped > 0
4122e7a4641STrond Myklebust 				    && desc->dup_cookie == desc->dir_cookie) {
4130c030806STrond Myklebust 					if (printk_ratelimit()) {
4146de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
4150c030806STrond Myklebust 								"Please contact your server vendor.  "
4169581a4aeSJeff Layton 								"The file: %.*s has duplicate cookie %llu\n",
4179581a4aeSJeff Layton 								desc->file, array->array[i].string.len,
4182e7a4641STrond Myklebust 								array->array[i].string.name, desc->dir_cookie);
4190c030806STrond Myklebust 					}
4200c030806STrond Myklebust 					status = -ELOOP;
4210c030806STrond Myklebust 					goto out;
4220c030806STrond Myklebust 				}
4232e7a4641STrond Myklebust 				desc->dup_cookie = desc->dir_cookie;
4242e7a4641STrond Myklebust 				desc->duped = -1;
4258ef2ce3eSBryan Schumaker 			}
42659e356a9STrond Myklebust 			if (nfs_readdir_use_cookie(desc->file))
4272e7a4641STrond Myklebust 				desc->ctx->pos = desc->dir_cookie;
42859e356a9STrond Myklebust 			else
42923db8620SAl Viro 				desc->ctx->pos = new_pos;
43059e356a9STrond Myklebust 			desc->prev_index = new_pos;
4318cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
43247c716cbSTrond Myklebust 			return 0;
4338cd51a0cSTrond Myklebust 		}
4348cd51a0cSTrond Myklebust 	}
435b1e21c97STrond Myklebust 	if (array->page_is_eof) {
436d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
4372e7a4641STrond Myklebust 		if (desc->dir_cookie == array->last_cookie)
4386089dd0dSThomas Meyer 			desc->eof = true;
439d1bacf9eSBryan Schumaker 	}
4400c030806STrond Myklebust out:
441d1bacf9eSBryan Schumaker 	return status;
442d1bacf9eSBryan Schumaker }
443d1bacf9eSBryan Schumaker 
444d1bacf9eSBryan Schumaker static
445d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
446d1bacf9eSBryan Schumaker {
447d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
44847c716cbSTrond Myklebust 	int status;
449d1bacf9eSBryan Schumaker 
4500795bf83SFabian Frederick 	array = kmap(desc->page);
451d1bacf9eSBryan Schumaker 
4522e7a4641STrond Myklebust 	if (desc->dir_cookie == 0)
453d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
454d1bacf9eSBryan Schumaker 	else
455d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
456d1bacf9eSBryan Schumaker 
45747c716cbSTrond Myklebust 	if (status == -EAGAIN) {
4580aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
459e47c085aSTrond Myklebust 		desc->current_index += array->size;
46047c716cbSTrond Myklebust 		desc->page_index++;
46147c716cbSTrond Myklebust 	}
4620795bf83SFabian Frederick 	kunmap(desc->page);
463d1bacf9eSBryan Schumaker 	return status;
464d1bacf9eSBryan Schumaker }
465d1bacf9eSBryan Schumaker 
466d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
467d1bacf9eSBryan Schumaker static
46856e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
469d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
470d1bacf9eSBryan Schumaker {
471480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
472684f39b4SNeilBrown 	const struct cred *cred = ctx->cred;
4734704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
4741da177e4SLinus Torvalds 	int		error;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds  again:
4771da177e4SLinus Torvalds 	timestamp = jiffies;
4784704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
479a1147b82STrond Myklebust 	desc->dir_verifier = nfs_save_change_attribute(inode);
480be62a1a8SMiklos Szeredi 	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
4811da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
4821da177e4SLinus Torvalds 	if (error < 0) {
4831da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
4841da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
4851da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
4863a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
487a7a3b1e9SBenjamin Coddington 			desc->plus = false;
4881da177e4SLinus Torvalds 			goto again;
4891da177e4SLinus Torvalds 		}
4901da177e4SLinus Torvalds 		goto error;
4911da177e4SLinus Torvalds 	}
4921f4eab7eSNeil Brown 	desc->timestamp = timestamp;
4934704f0e2STrond Myklebust 	desc->gencount = gencount;
494d1bacf9eSBryan Schumaker error:
495d1bacf9eSBryan Schumaker 	return error;
496d1bacf9eSBryan Schumaker }
497d1bacf9eSBryan Schumaker 
498573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
499573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
500d1bacf9eSBryan Schumaker {
50159e356a9STrond Myklebust 	struct inode *inode = file_inode(desc->file);
502573c4e1eSChuck Lever 	int error;
503d1bacf9eSBryan Schumaker 
50459e356a9STrond Myklebust 	error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus);
505573c4e1eSChuck Lever 	if (error)
506573c4e1eSChuck Lever 		return error;
507d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
508d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
509d1bacf9eSBryan Schumaker 	return 0;
510d1bacf9eSBryan Schumaker }
511d1bacf9eSBryan Schumaker 
512fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
513fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
514fa923369STrond Myklebust  */
515d39ab9deSBryan Schumaker static
516d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
517d39ab9deSBryan Schumaker {
518d8fdb47fSTrond Myklebust 	struct inode *inode;
519fa923369STrond Myklebust 	struct nfs_inode *nfsi;
520fa923369STrond Myklebust 
5212b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
5222b0143b5SDavid Howells 		return 0;
523fa923369STrond Myklebust 
524d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
525d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
526d8fdb47fSTrond Myklebust 		return 0;
527d8fdb47fSTrond Myklebust 
528d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
5297dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
530d39ab9deSBryan Schumaker 		return 0;
5317dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
5327dc72d5fSTrond Myklebust 		return 0;
5337dc72d5fSTrond Myklebust 	return 1;
534d39ab9deSBryan Schumaker }
535d39ab9deSBryan Schumaker 
536d39ab9deSBryan Schumaker static
53723db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
538d69ee9b8STrond Myklebust {
539d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
540d69ee9b8STrond Myklebust 		return false;
541d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
542d69ee9b8STrond Myklebust 		return true;
54323db8620SAl Viro 	if (ctx->pos == 0)
544d69ee9b8STrond Myklebust 		return true;
545d69ee9b8STrond Myklebust 	return false;
546d69ee9b8STrond Myklebust }
547d69ee9b8STrond Myklebust 
548d69ee9b8STrond Myklebust /*
54963519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
55063519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
551d69ee9b8STrond Myklebust  * directory.
552d69ee9b8STrond Myklebust  */
553d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
554d69ee9b8STrond Myklebust {
55563519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
55663519fbcSTrond Myklebust 
55763519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
55863519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
55963519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
560d69ee9b8STrond Myklebust }
561d69ee9b8STrond Myklebust 
562311324adSTrond Myklebust /*
563311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
564311324adSTrond Myklebust  *
565311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
566311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
567311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
568311324adSTrond Myklebust  * cache flush.
569311324adSTrond Myklebust  */
570311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
571311324adSTrond Myklebust {
57263519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
57363519fbcSTrond Myklebust 
57463519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
57563519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
57663519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
577227823d2SDai Ngo 		invalidate_mapping_pages(dir->i_mapping,
578227823d2SDai Ngo 			nfsi->page_index + 1, -1);
579311324adSTrond Myklebust 	}
580311324adSTrond Myklebust }
581311324adSTrond Myklebust 
582d69ee9b8STrond Myklebust static
583a1147b82STrond Myklebust void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry,
584a1147b82STrond Myklebust 		unsigned long dir_verifier)
585d39ab9deSBryan Schumaker {
58626fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
5879ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
5884a201d6eSTrond Myklebust 	struct dentry *dentry;
5894a201d6eSTrond Myklebust 	struct dentry *alias;
590d39ab9deSBryan Schumaker 	struct inode *inode;
591aa9c2669SDavid Quigley 	int status;
592d39ab9deSBryan Schumaker 
593fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
594fa923369STrond Myklebust 		return;
5956c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
5966c441c25STrond Myklebust 		return;
59778d04af4STrond Myklebust 	if (filename.len == 0)
59878d04af4STrond Myklebust 		return;
59978d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
60078d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
60178d04af4STrond Myklebust 		return;
60278d04af4STrond Myklebust 	/* ...or '/' */
60378d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
60478d04af4STrond Myklebust 		return;
6054a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
6064a201d6eSTrond Myklebust 		if (filename.len == 1)
6074a201d6eSTrond Myklebust 			return;
6084a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
6094a201d6eSTrond Myklebust 			return;
6104a201d6eSTrond Myklebust 	}
6118387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
612d39ab9deSBryan Schumaker 
6134a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
6149ac3d3e8SAl Viro again:
6159ac3d3e8SAl Viro 	if (!dentry) {
6169ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
6179ac3d3e8SAl Viro 		if (IS_ERR(dentry))
6189ac3d3e8SAl Viro 			return;
6199ac3d3e8SAl Viro 	}
6209ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
6216c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
6226c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
6236c441c25STrond Myklebust 					&entry->fattr->fsid))
6246c441c25STrond Myklebust 			goto out;
625d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
6267dc72d5fSTrond Myklebust 			if (!entry->fh->size)
6277dc72d5fSTrond Myklebust 				goto out;
628a1147b82STrond Myklebust 			nfs_set_verifier(dentry, dir_verifier);
6292b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
630aa9c2669SDavid Quigley 			if (!status)
6312b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
632d39ab9deSBryan Schumaker 			goto out;
633d39ab9deSBryan Schumaker 		} else {
6345542aa2fSEric W. Biederman 			d_invalidate(dentry);
635d39ab9deSBryan Schumaker 			dput(dentry);
6369ac3d3e8SAl Viro 			dentry = NULL;
6379ac3d3e8SAl Viro 			goto again;
638d39ab9deSBryan Schumaker 		}
639d39ab9deSBryan Schumaker 	}
6407dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
6417dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
6427dc72d5fSTrond Myklebust 		goto out;
6437dc72d5fSTrond Myklebust 	}
644d39ab9deSBryan Schumaker 
6451775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
64641d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
6479ac3d3e8SAl Viro 	d_lookup_done(dentry);
6489ac3d3e8SAl Viro 	if (alias) {
649d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
650d39ab9deSBryan Schumaker 			goto out;
6519ac3d3e8SAl Viro 		dput(dentry);
6529ac3d3e8SAl Viro 		dentry = alias;
6539ac3d3e8SAl Viro 	}
654a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
655d39ab9deSBryan Schumaker out:
656d39ab9deSBryan Schumaker 	dput(dentry);
657d39ab9deSBryan Schumaker }
658d39ab9deSBryan Schumaker 
659d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
6603b2a09f1STrond Myklebust static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc,
6613b2a09f1STrond Myklebust 				   struct nfs_entry *entry,
6623b2a09f1STrond Myklebust 				   struct page **xdr_pages,
6633b2a09f1STrond Myklebust 				   struct page *fillme, unsigned int buflen)
664d1bacf9eSBryan Schumaker {
6653b2a09f1STrond Myklebust 	struct address_space *mapping = desc->file->f_mapping;
666babddc72SBryan Schumaker 	struct xdr_stream stream;
667f7da7a12SBenny Halevy 	struct xdr_buf buf;
6683b2a09f1STrond Myklebust 	struct page *scratch, *new, *page = fillme;
6695c346854STrond Myklebust 	int status;
670babddc72SBryan Schumaker 
6716650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
6726650239aSTrond Myklebust 	if (scratch == NULL)
6736650239aSTrond Myklebust 		return -ENOMEM;
674babddc72SBryan Schumaker 
675f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
6766650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
67799424380SBryan Schumaker 
67899424380SBryan Schumaker 	do {
679d33030e2SJeffrey Mitchell 		if (entry->label)
680d33030e2SJeffrey Mitchell 			entry->label->len = NFS4_MAXLABELLEN;
681d33030e2SJeffrey Mitchell 
68299424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
683972bcdf2STrond Myklebust 		if (status != 0)
68499424380SBryan Schumaker 			break;
6855c346854STrond Myklebust 
686a7a3b1e9SBenjamin Coddington 		if (desc->plus)
687a1147b82STrond Myklebust 			nfs_prime_dcache(file_dentry(desc->file), entry,
688a1147b82STrond Myklebust 					desc->dir_verifier);
6898cd51a0cSTrond Myklebust 
690db531db9SMax Kellermann 		status = nfs_readdir_add_to_array(entry, page);
6913b2a09f1STrond Myklebust 		if (status != -ENOSPC)
6923b2a09f1STrond Myklebust 			continue;
6933b2a09f1STrond Myklebust 
6943b2a09f1STrond Myklebust 		if (page->mapping != mapping)
6953b2a09f1STrond Myklebust 			break;
6963b2a09f1STrond Myklebust 		new = nfs_readdir_page_get_next(mapping, page->index + 1,
6973b2a09f1STrond Myklebust 						entry->prev_cookie);
6983b2a09f1STrond Myklebust 		if (!new)
6993b2a09f1STrond Myklebust 			break;
7003b2a09f1STrond Myklebust 		if (page != fillme)
7013b2a09f1STrond Myklebust 			nfs_readdir_page_unlock_and_put(page);
7023b2a09f1STrond Myklebust 		page = new;
7033b2a09f1STrond Myklebust 		status = nfs_readdir_add_to_array(entry, page);
704972bcdf2STrond Myklebust 	} while (!status && !entry->eof);
70599424380SBryan Schumaker 
706972bcdf2STrond Myklebust 	switch (status) {
707972bcdf2STrond Myklebust 	case -EBADCOOKIE:
708972bcdf2STrond Myklebust 		if (entry->eof) {
709b1e21c97STrond Myklebust 			nfs_readdir_page_set_eof(page);
71099424380SBryan Schumaker 			status = 0;
71156e4ebf8SBryan Schumaker 		}
712972bcdf2STrond Myklebust 		break;
713972bcdf2STrond Myklebust 	case -ENOSPC:
714972bcdf2STrond Myklebust 	case -EAGAIN:
715972bcdf2STrond Myklebust 		status = 0;
716972bcdf2STrond Myklebust 		break;
717972bcdf2STrond Myklebust 	}
7186650239aSTrond Myklebust 
7193b2a09f1STrond Myklebust 	if (page != fillme)
7203b2a09f1STrond Myklebust 		nfs_readdir_page_unlock_and_put(page);
7213b2a09f1STrond Myklebust 
7226650239aSTrond Myklebust 	put_page(scratch);
7238cd51a0cSTrond Myklebust 	return status;
7248cd51a0cSTrond Myklebust }
72556e4ebf8SBryan Schumaker 
72656e4ebf8SBryan Schumaker static
727c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
72856e4ebf8SBryan Schumaker {
72956e4ebf8SBryan Schumaker 	unsigned int i;
73056e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
73156e4ebf8SBryan Schumaker 		put_page(pages[i]);
73256e4ebf8SBryan Schumaker }
73356e4ebf8SBryan Schumaker 
73456e4ebf8SBryan Schumaker /*
735bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
736bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
73756e4ebf8SBryan Schumaker  */
73856e4ebf8SBryan Schumaker static
739c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
74056e4ebf8SBryan Schumaker {
74156e4ebf8SBryan Schumaker 	unsigned int i;
74256e4ebf8SBryan Schumaker 
74356e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
74456e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
74556e4ebf8SBryan Schumaker 		if (page == NULL)
74656e4ebf8SBryan Schumaker 			goto out_freepages;
74756e4ebf8SBryan Schumaker 		pages[i] = page;
74856e4ebf8SBryan Schumaker 	}
7496650239aSTrond Myklebust 	return 0;
75056e4ebf8SBryan Schumaker 
75156e4ebf8SBryan Schumaker out_freepages:
752c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
7536650239aSTrond Myklebust 	return -ENOMEM;
754d1bacf9eSBryan Schumaker }
755d1bacf9eSBryan Schumaker 
756d1bacf9eSBryan Schumaker static
757d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
758d1bacf9eSBryan Schumaker {
75956e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
760d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
761d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
7628cd51a0cSTrond Myklebust 	int status = -ENOMEM;
76356e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
764d1bacf9eSBryan Schumaker 
765d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
7661f1d4aa4STrond Myklebust 	entry.cookie = nfs_readdir_page_last_cookie(page);
767d1bacf9eSBryan Schumaker 	entry.eof = 0;
768d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
769d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
770573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
771d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
772d1bacf9eSBryan Schumaker 		goto out;
773d1bacf9eSBryan Schumaker 
77414c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
77514c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
77614c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
77714c43f76SDavid Quigley 		goto out;
77814c43f76SDavid Quigley 	}
77914c43f76SDavid Quigley 
780c7e9668eSAnna Schumaker 	status = nfs_readdir_alloc_pages(pages, array_size);
7816650239aSTrond Myklebust 	if (status < 0)
782*e762a639STrond Myklebust 		goto out_release_label;
783d1bacf9eSBryan Schumaker 	do {
784ac396128STrond Myklebust 		unsigned int pglen;
78556e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
786babddc72SBryan Schumaker 
787d1bacf9eSBryan Schumaker 		if (status < 0)
788d1bacf9eSBryan Schumaker 			break;
789972bcdf2STrond Myklebust 
790ac396128STrond Myklebust 		pglen = status;
791972bcdf2STrond Myklebust 		if (pglen == 0) {
792972bcdf2STrond Myklebust 			nfs_readdir_page_set_eof(page);
7938cd51a0cSTrond Myklebust 			break;
7948cd51a0cSTrond Myklebust 		}
795972bcdf2STrond Myklebust 
796972bcdf2STrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
797*e762a639STrond Myklebust 	} while (!status && nfs_readdir_page_needs_filling(page));
798d1bacf9eSBryan Schumaker 
799c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
800*e762a639STrond Myklebust out_release_label:
80114c43f76SDavid Quigley 	nfs4_label_free(entry.label);
802d1bacf9eSBryan Schumaker out:
803d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
804d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
805d1bacf9eSBryan Schumaker 	return status;
806d1bacf9eSBryan Schumaker }
807d1bacf9eSBryan Schumaker 
8081f1d4aa4STrond Myklebust static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc)
8091da177e4SLinus Torvalds {
81009cbfeafSKirill A. Shutemov 	put_page(desc->page);
8111da177e4SLinus Torvalds 	desc->page = NULL;
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8141f1d4aa4STrond Myklebust static void
8151f1d4aa4STrond Myklebust nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc)
8161da177e4SLinus Torvalds {
8171f1d4aa4STrond Myklebust 	unlock_page(desc->page);
8181f1d4aa4STrond Myklebust 	nfs_readdir_page_put(desc);
8191f1d4aa4STrond Myklebust }
8201f1d4aa4STrond Myklebust 
8211f1d4aa4STrond Myklebust static struct page *
8221f1d4aa4STrond Myklebust nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc)
8231f1d4aa4STrond Myklebust {
8241f1d4aa4STrond Myklebust 	return nfs_readdir_page_get_locked(desc->file->f_mapping,
8251f1d4aa4STrond Myklebust 					   desc->page_index,
8261f1d4aa4STrond Myklebust 					   desc->last_cookie);
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds /*
830d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
831114de382STrond Myklebust  * and locks the page to prevent removal from the page cache.
8321da177e4SLinus Torvalds  */
833d1bacf9eSBryan Schumaker static
834114de382STrond Myklebust int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc)
835d1bacf9eSBryan Schumaker {
836227823d2SDai Ngo 	struct inode *inode = file_inode(desc->file);
837227823d2SDai Ngo 	struct nfs_inode *nfsi = NFS_I(inode);
838d1bacf9eSBryan Schumaker 	int res;
839d1bacf9eSBryan Schumaker 
8401f1d4aa4STrond Myklebust 	desc->page = nfs_readdir_page_get_cached(desc);
8411f1d4aa4STrond Myklebust 	if (!desc->page)
8421f1d4aa4STrond Myklebust 		return -ENOMEM;
8431f1d4aa4STrond Myklebust 	if (nfs_readdir_page_needs_filling(desc->page)) {
8441f1d4aa4STrond Myklebust 		res = nfs_readdir_xdr_to_array(desc, desc->page, inode);
8451f1d4aa4STrond Myklebust 		if (res < 0)
846114de382STrond Myklebust 			goto error;
8471f1d4aa4STrond Myklebust 	}
848114de382STrond Myklebust 	res = nfs_readdir_search_array(desc);
849227823d2SDai Ngo 	if (res == 0) {
850227823d2SDai Ngo 		nfsi->page_index = desc->page_index;
851114de382STrond Myklebust 		return 0;
852114de382STrond Myklebust 	}
853114de382STrond Myklebust error:
8541f1d4aa4STrond Myklebust 	nfs_readdir_page_unlock_and_put_cached(desc);
855d1bacf9eSBryan Schumaker 	return res;
856d1bacf9eSBryan Schumaker }
857d1bacf9eSBryan Schumaker 
858d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
8591da177e4SLinus Torvalds static inline
8601da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
8611da177e4SLinus Torvalds {
8628cd51a0cSTrond Myklebust 	int res;
863d1bacf9eSBryan Schumaker 
8640aded708STrond Myklebust 	if (desc->page_index == 0) {
8658cd51a0cSTrond Myklebust 		desc->current_index = 0;
86659e356a9STrond Myklebust 		desc->prev_index = 0;
8670aded708STrond Myklebust 		desc->last_cookie = 0;
8680aded708STrond Myklebust 	}
86947c716cbSTrond Myklebust 	do {
870114de382STrond Myklebust 		res = find_and_lock_cache_page(desc);
87147c716cbSTrond Myklebust 	} while (res == -EAGAIN);
8721da177e4SLinus Torvalds 	return res;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds /*
8761da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
8771da177e4SLinus Torvalds  */
8781da177e4SLinus Torvalds static
87923db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	struct file	*file = desc->file;
882d1bacf9eSBryan Schumaker 	int i = 0;
883d1bacf9eSBryan Schumaker 	int res = 0;
884d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
8858ef2ce3eSBryan Schumaker 
8860795bf83SFabian Frederick 	array = kmap(desc->page);
887d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
888ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
8891da177e4SLinus Torvalds 
890ece0b423STrond Myklebust 		ent = &array->array[i];
89123db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
89223db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
8936089dd0dSThomas Meyer 			desc->eof = true;
8941da177e4SLinus Torvalds 			break;
895ece0b423STrond Myklebust 		}
896d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
8972e7a4641STrond Myklebust 			desc->dir_cookie = array->array[i+1].cookie;
898d1bacf9eSBryan Schumaker 		else
8992e7a4641STrond Myklebust 			desc->dir_cookie = array->last_cookie;
90059e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(file))
9012e7a4641STrond Myklebust 			desc->ctx->pos = desc->dir_cookie;
90259e356a9STrond Myklebust 		else
90359e356a9STrond Myklebust 			desc->ctx->pos++;
9042e7a4641STrond Myklebust 		if (desc->duped != 0)
9052e7a4641STrond Myklebust 			desc->duped = 1;
9068cd51a0cSTrond Myklebust 	}
907b1e21c97STrond Myklebust 	if (array->page_is_eof)
9086089dd0dSThomas Meyer 		desc->eof = true;
909d1bacf9eSBryan Schumaker 
9100795bf83SFabian Frederick 	kunmap(desc->page);
9111e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
9122e7a4641STrond Myklebust 			(unsigned long long)desc->dir_cookie, res);
9131da177e4SLinus Torvalds 	return res;
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds /*
9171da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
9181da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
9191da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
9201da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
9211da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
9221da177e4SLinus Torvalds  *
9231da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
9241da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
9251da177e4SLinus Torvalds  *	 we should already have a complete representation of the
9261da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
9271da177e4SLinus Torvalds  */
9281da177e4SLinus Torvalds static inline
92923db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
9301da177e4SLinus Torvalds {
9311da177e4SLinus Torvalds 	struct page	*page = NULL;
9321da177e4SLinus Torvalds 	int		status;
933496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
9341da177e4SLinus Torvalds 
9351e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
9362e7a4641STrond Myklebust 			(unsigned long long)desc->dir_cookie);
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
9391da177e4SLinus Torvalds 	if (!page) {
9401da177e4SLinus Torvalds 		status = -ENOMEM;
9411da177e4SLinus Torvalds 		goto out;
9421da177e4SLinus Torvalds 	}
9431da177e4SLinus Torvalds 
9447a8e1dc3STrond Myklebust 	desc->page_index = 0;
9452e7a4641STrond Myklebust 	desc->last_cookie = desc->dir_cookie;
9467a8e1dc3STrond Myklebust 	desc->page = page;
9472e7a4641STrond Myklebust 	desc->duped = 0;
9487a8e1dc3STrond Myklebust 
9491f1d4aa4STrond Myklebust 	nfs_readdir_page_init_array(page, desc->dir_cookie);
95085f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
95185f8607eSTrond Myklebust 	if (status < 0)
952d1bacf9eSBryan Schumaker 		goto out_release;
953d1bacf9eSBryan Schumaker 
95423db8620SAl Viro 	status = nfs_do_filldir(desc);
9551da177e4SLinus Torvalds 
956114de382STrond Myklebust  out_release:
957114de382STrond Myklebust 	nfs_readdir_clear_array(desc->page);
9581f1d4aa4STrond Myklebust 	nfs_readdir_page_put(desc);
9591da177e4SLinus Torvalds  out:
9601e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
9613110ff80SHarvey Harrison 			__func__, status);
9621da177e4SLinus Torvalds 	return status;
9631da177e4SLinus Torvalds }
9641da177e4SLinus Torvalds 
96500a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
96600a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
96700a92642SOlivier Galibert    whole directory.
9681da177e4SLinus Torvalds  */
96923db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
9701da177e4SLinus Torvalds {
971be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
9722b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
97323db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
97459e356a9STrond Myklebust 	nfs_readdir_descriptor_t my_desc = {
97559e356a9STrond Myklebust 		.file = file,
97659e356a9STrond Myklebust 		.ctx = ctx,
97759e356a9STrond Myklebust 		.plus = nfs_use_readdirplus(inode, ctx),
97859e356a9STrond Myklebust 	},
97959e356a9STrond Myklebust 			*desc = &my_desc;
98007b5ce8eSScott Mayhew 	int res = 0;
9811da177e4SLinus Torvalds 
9826de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
9836de1472fSAl Viro 			file, (long long)ctx->pos);
98491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
98591d5b470SChuck Lever 
9861da177e4SLinus Torvalds 	/*
98723db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
988f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
98900a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
99000a92642SOlivier Galibert 	 * revalidate the cookie.
9911da177e4SLinus Torvalds 	 */
99279f687a3STrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
99323db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
994fccca7fcSTrond Myklebust 	if (res < 0)
995fccca7fcSTrond Myklebust 		goto out;
996fccca7fcSTrond Myklebust 
9972e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
9982e7a4641STrond Myklebust 	desc->dir_cookie = dir_ctx->dir_cookie;
9992e7a4641STrond Myklebust 	desc->dup_cookie = dir_ctx->dup_cookie;
10002e7a4641STrond Myklebust 	desc->duped = dir_ctx->duped;
10012e7a4641STrond Myklebust 	desc->attr_gencount = dir_ctx->attr_gencount;
10022e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
10032e7a4641STrond Myklebust 
100447c716cbSTrond Myklebust 	do {
10051da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
100600a92642SOlivier Galibert 
10071da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
1008ece0b423STrond Myklebust 			res = 0;
10091da177e4SLinus Torvalds 			/* This means either end of directory */
10102e7a4641STrond Myklebust 			if (desc->dir_cookie && !desc->eof) {
10111da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
101223db8620SAl Viro 				res = uncached_readdir(desc);
1013ece0b423STrond Myklebust 				if (res == 0)
10141da177e4SLinus Torvalds 					continue;
10151da177e4SLinus Torvalds 			}
10161da177e4SLinus Torvalds 			break;
10171da177e4SLinus Torvalds 		}
10181da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
10193a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
10201da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1021baf57a09STrond Myklebust 			desc->page_index = 0;
1022a7a3b1e9SBenjamin Coddington 			desc->plus = false;
1023a7a3b1e9SBenjamin Coddington 			desc->eof = false;
10241da177e4SLinus Torvalds 			continue;
10251da177e4SLinus Torvalds 		}
10261da177e4SLinus Torvalds 		if (res < 0)
10271da177e4SLinus Torvalds 			break;
10281da177e4SLinus Torvalds 
102923db8620SAl Viro 		res = nfs_do_filldir(desc);
10301f1d4aa4STrond Myklebust 		nfs_readdir_page_unlock_and_put_cached(desc);
1031ece0b423STrond Myklebust 		if (res < 0)
10321da177e4SLinus Torvalds 			break;
103347c716cbSTrond Myklebust 	} while (!desc->eof);
10342e7a4641STrond Myklebust 
10352e7a4641STrond Myklebust 	spin_lock(&file->f_lock);
10362e7a4641STrond Myklebust 	dir_ctx->dir_cookie = desc->dir_cookie;
10372e7a4641STrond Myklebust 	dir_ctx->dup_cookie = desc->dup_cookie;
10382e7a4641STrond Myklebust 	dir_ctx->duped = desc->duped;
10392e7a4641STrond Myklebust 	dir_ctx->attr_gencount = desc->attr_gencount;
10402e7a4641STrond Myklebust 	spin_unlock(&file->f_lock);
10412e7a4641STrond Myklebust 
1042fccca7fcSTrond Myklebust out:
10431e7cb3dcSChuck Lever 	if (res > 0)
10441e7cb3dcSChuck Lever 		res = 0;
10456de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
10461da177e4SLinus Torvalds 	return res;
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
1049965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
1050f0dd2136STrond Myklebust {
1051480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
1052b84e06c5SChuck Lever 
10536de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
10546de1472fSAl Viro 			filp, offset, whence);
1055b84e06c5SChuck Lever 
1056965c8e59SAndrew Morton 	switch (whence) {
1057f0dd2136STrond Myklebust 	default:
1058b2b1ff3dSTrond Myklebust 		return -EINVAL;
1059b2b1ff3dSTrond Myklebust 	case SEEK_SET:
1060b2b1ff3dSTrond Myklebust 		if (offset < 0)
1061b2b1ff3dSTrond Myklebust 			return -EINVAL;
106283f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1063b2b1ff3dSTrond Myklebust 		break;
1064b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
1065b2b1ff3dSTrond Myklebust 		if (offset == 0)
1066b2b1ff3dSTrond Myklebust 			return filp->f_pos;
106783f2c45eSTrond Myklebust 		spin_lock(&filp->f_lock);
1068b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
1069b2b1ff3dSTrond Myklebust 		if (offset < 0) {
107083f2c45eSTrond Myklebust 			spin_unlock(&filp->f_lock);
1071b2b1ff3dSTrond Myklebust 			return -EINVAL;
1072b2b1ff3dSTrond Myklebust 		}
1073f0dd2136STrond Myklebust 	}
1074f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1075f0dd2136STrond Myklebust 		filp->f_pos = offset;
107659e356a9STrond Myklebust 		if (nfs_readdir_use_cookie(filp))
107759e356a9STrond Myklebust 			dir_ctx->dir_cookie = offset;
107859e356a9STrond Myklebust 		else
1079480c2006SBryan Schumaker 			dir_ctx->dir_cookie = 0;
10808ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
1081f0dd2136STrond Myklebust 	}
108283f2c45eSTrond Myklebust 	spin_unlock(&filp->f_lock);
1083f0dd2136STrond Myklebust 	return offset;
1084f0dd2136STrond Myklebust }
1085f0dd2136STrond Myklebust 
10861da177e4SLinus Torvalds /*
10871da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
10881da177e4SLinus Torvalds  * is a dummy operation.
10891da177e4SLinus Torvalds  */
109002c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
109102c24a82SJosef Bacik 			 int datasync)
10921da177e4SLinus Torvalds {
10936de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
10941e7cb3dcSChuck Lever 
109511decaf8STrond Myklebust 	nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC);
10961da177e4SLinus Torvalds 	return 0;
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
1099bfc69a45STrond Myklebust /**
1100bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1101302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1102bfc69a45STrond Myklebust  *
1103bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1104bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1105bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1106bfc69a45STrond Myklebust  *
1107efeda80dSTrond Myklebust  * Note that we reserve bit '0' as a tag to let us know when a dentry
1108efeda80dSTrond Myklebust  * was revalidated while holding a delegation on its inode.
1109efeda80dSTrond Myklebust  *
1110bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1111bfc69a45STrond Myklebust  */
1112bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1113bfc69a45STrond Myklebust {
1114efeda80dSTrond Myklebust 	NFS_I(dir)->cache_change_attribute += 2;
1115bfc69a45STrond Myklebust }
111689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1117bfc69a45STrond Myklebust 
1118efeda80dSTrond Myklebust /**
1119efeda80dSTrond Myklebust  * nfs_verify_change_attribute - Detects NFS remote directory changes
1120efeda80dSTrond Myklebust  * @dir: pointer to parent directory inode
1121efeda80dSTrond Myklebust  * @verf: previously saved change attribute
1122efeda80dSTrond Myklebust  *
1123efeda80dSTrond Myklebust  * Return "false" if the verifiers doesn't match the change attribute.
1124efeda80dSTrond Myklebust  * This would usually indicate that the directory contents have changed on
1125efeda80dSTrond Myklebust  * the server, and that any dentries need revalidating.
1126efeda80dSTrond Myklebust  */
1127efeda80dSTrond Myklebust static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf)
1128efeda80dSTrond Myklebust {
1129efeda80dSTrond Myklebust 	return (verf & ~1UL) == nfs_save_change_attribute(dir);
1130efeda80dSTrond Myklebust }
1131efeda80dSTrond Myklebust 
1132efeda80dSTrond Myklebust static void nfs_set_verifier_delegated(unsigned long *verf)
1133efeda80dSTrond Myklebust {
1134efeda80dSTrond Myklebust 	*verf |= 1UL;
1135efeda80dSTrond Myklebust }
1136efeda80dSTrond Myklebust 
1137efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1138efeda80dSTrond Myklebust static void nfs_unset_verifier_delegated(unsigned long *verf)
1139efeda80dSTrond Myklebust {
1140efeda80dSTrond Myklebust 	*verf &= ~1UL;
1141efeda80dSTrond Myklebust }
1142efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1143efeda80dSTrond Myklebust 
1144efeda80dSTrond Myklebust static bool nfs_test_verifier_delegated(unsigned long verf)
1145efeda80dSTrond Myklebust {
1146efeda80dSTrond Myklebust 	return verf & 1;
1147efeda80dSTrond Myklebust }
1148efeda80dSTrond Myklebust 
1149efeda80dSTrond Myklebust static bool nfs_verifier_is_delegated(struct dentry *dentry)
1150efeda80dSTrond Myklebust {
1151efeda80dSTrond Myklebust 	return nfs_test_verifier_delegated(dentry->d_time);
1152efeda80dSTrond Myklebust }
1153efeda80dSTrond Myklebust 
1154efeda80dSTrond Myklebust static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf)
1155efeda80dSTrond Myklebust {
1156efeda80dSTrond Myklebust 	struct inode *inode = d_inode(dentry);
1157efeda80dSTrond Myklebust 
1158efeda80dSTrond Myklebust 	if (!nfs_verifier_is_delegated(dentry) &&
1159efeda80dSTrond Myklebust 	    !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf))
1160efeda80dSTrond Myklebust 		goto out;
1161efeda80dSTrond Myklebust 	if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1162efeda80dSTrond Myklebust 		nfs_set_verifier_delegated(&verf);
1163efeda80dSTrond Myklebust out:
1164efeda80dSTrond Myklebust 	dentry->d_time = verf;
1165efeda80dSTrond Myklebust }
1166efeda80dSTrond Myklebust 
1167efeda80dSTrond Myklebust /**
1168efeda80dSTrond Myklebust  * nfs_set_verifier - save a parent directory verifier in the dentry
1169efeda80dSTrond Myklebust  * @dentry: pointer to dentry
1170efeda80dSTrond Myklebust  * @verf: verifier to save
1171efeda80dSTrond Myklebust  *
1172efeda80dSTrond Myklebust  * Saves the parent directory verifier in @dentry. If the inode has
1173efeda80dSTrond Myklebust  * a delegation, we also tag the dentry as having been revalidated
1174efeda80dSTrond Myklebust  * while holding a delegation so that we know we don't have to
1175efeda80dSTrond Myklebust  * look it up again after a directory change.
1176efeda80dSTrond Myklebust  */
1177efeda80dSTrond Myklebust void nfs_set_verifier(struct dentry *dentry, unsigned long verf)
1178efeda80dSTrond Myklebust {
1179efeda80dSTrond Myklebust 
1180efeda80dSTrond Myklebust 	spin_lock(&dentry->d_lock);
1181efeda80dSTrond Myklebust 	nfs_set_verifier_locked(dentry, verf);
1182efeda80dSTrond Myklebust 	spin_unlock(&dentry->d_lock);
1183efeda80dSTrond Myklebust }
1184efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_set_verifier);
1185efeda80dSTrond Myklebust 
1186efeda80dSTrond Myklebust #if IS_ENABLED(CONFIG_NFS_V4)
1187efeda80dSTrond Myklebust /**
1188efeda80dSTrond Myklebust  * nfs_clear_verifier_delegated - clear the dir verifier delegation tag
1189efeda80dSTrond Myklebust  * @inode: pointer to inode
1190efeda80dSTrond Myklebust  *
1191efeda80dSTrond Myklebust  * Iterates through the dentries in the inode alias list and clears
1192efeda80dSTrond Myklebust  * the tag used to indicate that the dentry has been revalidated
1193efeda80dSTrond Myklebust  * while holding a delegation.
1194efeda80dSTrond Myklebust  * This function is intended for use when the delegation is being
1195efeda80dSTrond Myklebust  * returned or revoked.
1196efeda80dSTrond Myklebust  */
1197efeda80dSTrond Myklebust void nfs_clear_verifier_delegated(struct inode *inode)
1198efeda80dSTrond Myklebust {
1199efeda80dSTrond Myklebust 	struct dentry *alias;
1200efeda80dSTrond Myklebust 
1201efeda80dSTrond Myklebust 	if (!inode)
1202efeda80dSTrond Myklebust 		return;
1203efeda80dSTrond Myklebust 	spin_lock(&inode->i_lock);
1204efeda80dSTrond Myklebust 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1205efeda80dSTrond Myklebust 		spin_lock(&alias->d_lock);
1206efeda80dSTrond Myklebust 		nfs_unset_verifier_delegated(&alias->d_time);
1207efeda80dSTrond Myklebust 		spin_unlock(&alias->d_lock);
1208efeda80dSTrond Myklebust 	}
1209efeda80dSTrond Myklebust 	spin_unlock(&inode->i_lock);
1210efeda80dSTrond Myklebust }
1211efeda80dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated);
1212efeda80dSTrond Myklebust #endif /* IS_ENABLED(CONFIG_NFS_V4) */
1213efeda80dSTrond Myklebust 
12141da177e4SLinus Torvalds /*
12151da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
12161da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
12171da177e4SLinus Torvalds  * and may need to be looked up again.
1218912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
12191da177e4SLinus Torvalds  */
1220912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1221912a108dSNeilBrown 			      int rcu_walk)
12221da177e4SLinus Torvalds {
12231da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
12241da177e4SLinus Torvalds 		return 1;
12254eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
12264eec952eSTrond Myklebust 		return 0;
1227f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
12286ecc5e8fSTrond Myklebust 		return 0;
1229f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
12301cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1231912a108dSNeilBrown 		if (rcu_walk)
1232f2c77f4eSTrond Myklebust 			return 0;
12331cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
12341cd9cb05STrond Myklebust 			return 0;
12351cd9cb05STrond Myklebust 	}
1236f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1237f2c77f4eSTrond Myklebust 		return 0;
1238f2c77f4eSTrond Myklebust 	return 1;
12391da177e4SLinus Torvalds }
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds /*
1242a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1243a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1244a12802caSTrond Myklebust  */
1245fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1246a12802caSTrond Myklebust {
1247a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1248a12802caSTrond Myklebust 		return 0;
1249fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1250a12802caSTrond Myklebust }
1251a12802caSTrond Myklebust 
1252a12802caSTrond Myklebust /*
12531d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
12541d6757fbSTrond Myklebust  *
12551d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
12561d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
12571d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
12581d6757fbSTrond Myklebust  *
12591d6757fbSTrond Myklebust  */
126065a0c149STrond Myklebust static
1261fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
12621da177e4SLinus Torvalds {
12631da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
126465a0c149STrond Myklebust 	int ret;
12651da177e4SLinus Torvalds 
126636d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
12674e99a1ffSTrond Myklebust 		return 0;
126847921921STrond Myklebust 
126947921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
127047921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
127147921921STrond Myklebust 		case S_IFREG:
127247921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
127347921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
127447921921STrond Myklebust 				goto out;
1275df561f66SGustavo A. R. Silva 			fallthrough;
127647921921STrond Myklebust 		case S_IFDIR:
127747921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
127847921921STrond Myklebust 				break;
127947921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
128047921921STrond Myklebust 			goto out_force;
128147921921STrond Myklebust 		}
128247921921STrond Myklebust 	}
128347921921STrond Myklebust 
12841da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1285fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
12861da177e4SLinus Torvalds 		goto out_force;
128765a0c149STrond Myklebust out:
1288a61246c9SLance Shelton 	return (inode->i_nlink == 0) ? -ESTALE : 0;
12891da177e4SLinus Torvalds out_force:
12901fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
12911fa1e384SNeilBrown 		return -ECHILD;
129265a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
129365a0c149STrond Myklebust 	if (ret != 0)
129465a0c149STrond Myklebust 		return ret;
129565a0c149STrond Myklebust 	goto out;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds /*
12991da177e4SLinus Torvalds  * We judge how long we want to trust negative
13001da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
13011da177e4SLinus Torvalds  *
13021da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
13031da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1304912a108dSNeilBrown  *
1305912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1306912a108dSNeilBrown  * suggesting a reval is needed.
13079f6d44d4STrond Myklebust  *
13089f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
13099f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
13101da177e4SLinus Torvalds  */
13111da177e4SLinus Torvalds static inline
13121da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1313fa3c56bbSAl Viro 		       unsigned int flags)
13141da177e4SLinus Torvalds {
13159f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
13161da177e4SLinus Torvalds 		return 0;
13174eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
13184eec952eSTrond Myklebust 		return 1;
1319912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds 
13225ceb9d7fSTrond Myklebust static int
13235ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
13245ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
13251da177e4SLinus Torvalds {
13265ceb9d7fSTrond Myklebust 	switch (error) {
13275ceb9d7fSTrond Myklebust 	case 1:
13286de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
13296de1472fSAl Viro 			__func__, dentry);
13301da177e4SLinus Torvalds 		return 1;
13315ceb9d7fSTrond Myklebust 	case 0:
1332a1643a92STrond Myklebust 		nfs_mark_for_revalidate(dir);
13331da177e4SLinus Torvalds 		if (inode && S_ISDIR(inode->i_mode)) {
13341da177e4SLinus Torvalds 			/* Purge readdir caches. */
13351da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1336a3f432bfSJ. Bruce Fields 			/*
1337a3f432bfSJ. Bruce Fields 			 * We can't d_drop the root of a disconnected tree:
1338a3f432bfSJ. Bruce Fields 			 * its d_hash is on the s_anon list and d_drop() would hide
1339a3f432bfSJ. Bruce Fields 			 * it from shrink_dcache_for_unmount(), leading to busy
1340a3f432bfSJ. Bruce Fields 			 * inodes on unmount and further oopses.
1341a3f432bfSJ. Bruce Fields 			 */
1342a3f432bfSJ. Bruce Fields 			if (IS_ROOT(dentry))
13435ceb9d7fSTrond Myklebust 				return 1;
13441da177e4SLinus Torvalds 		}
13456de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
13466de1472fSAl Viro 				__func__, dentry);
13471da177e4SLinus Torvalds 		return 0;
13485ceb9d7fSTrond Myklebust 	}
13496de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
13506de1472fSAl Viro 				__func__, dentry, error);
1351e1fb4d05STrond Myklebust 	return error;
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
13545ceb9d7fSTrond Myklebust static int
13555ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
13565ceb9d7fSTrond Myklebust 			       unsigned int flags)
13575ceb9d7fSTrond Myklebust {
13585ceb9d7fSTrond Myklebust 	int ret = 1;
13595ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
13605ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
13615ceb9d7fSTrond Myklebust 			return -ECHILD;
13625ceb9d7fSTrond Myklebust 		ret = 0;
13635ceb9d7fSTrond Myklebust 	}
13645ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
13655ceb9d7fSTrond Myklebust }
13665ceb9d7fSTrond Myklebust 
13675ceb9d7fSTrond Myklebust static int
13685ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
13695ceb9d7fSTrond Myklebust 				struct inode *inode)
13705ceb9d7fSTrond Myklebust {
13715ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
13725ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
13735ceb9d7fSTrond Myklebust }
13745ceb9d7fSTrond Myklebust 
13755ceb9d7fSTrond Myklebust static int
13765ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
13775ceb9d7fSTrond Myklebust 			     struct inode *inode)
13785ceb9d7fSTrond Myklebust {
13795ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
13805ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
13815ceb9d7fSTrond Myklebust 	struct nfs4_label *label;
1382a1147b82STrond Myklebust 	unsigned long dir_verifier;
13835ceb9d7fSTrond Myklebust 	int ret;
13845ceb9d7fSTrond Myklebust 
13855ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
13865ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
13875ceb9d7fSTrond Myklebust 	fattr = nfs_alloc_fattr();
13885ceb9d7fSTrond Myklebust 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
13895ceb9d7fSTrond Myklebust 	if (fhandle == NULL || fattr == NULL || IS_ERR(label))
13905ceb9d7fSTrond Myklebust 		goto out;
13915ceb9d7fSTrond Myklebust 
1392a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
1393f7b37b8bSTrond Myklebust 	ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
13945ceb9d7fSTrond Myklebust 	if (ret < 0) {
1395f7b37b8bSTrond Myklebust 		switch (ret) {
1396f7b37b8bSTrond Myklebust 		case -ESTALE:
1397f7b37b8bSTrond Myklebust 		case -ENOENT:
13985ceb9d7fSTrond Myklebust 			ret = 0;
1399f7b37b8bSTrond Myklebust 			break;
1400f7b37b8bSTrond Myklebust 		case -ETIMEDOUT:
1401f7b37b8bSTrond Myklebust 			if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
1402f7b37b8bSTrond Myklebust 				ret = 1;
1403f7b37b8bSTrond Myklebust 		}
14045ceb9d7fSTrond Myklebust 		goto out;
14055ceb9d7fSTrond Myklebust 	}
14065ceb9d7fSTrond Myklebust 	ret = 0;
14075ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
14085ceb9d7fSTrond Myklebust 		goto out;
14095ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
14105ceb9d7fSTrond Myklebust 		goto out;
14115ceb9d7fSTrond Myklebust 
14125ceb9d7fSTrond Myklebust 	nfs_setsecurity(inode, fattr, label);
1413a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
14145ceb9d7fSTrond Myklebust 
14155ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
14165ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
14175ceb9d7fSTrond Myklebust 	ret = 1;
14185ceb9d7fSTrond Myklebust out:
14195ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
14205ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
14215ceb9d7fSTrond Myklebust 	nfs4_label_free(label);
14225ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
14235ceb9d7fSTrond Myklebust }
14245ceb9d7fSTrond Myklebust 
14255ceb9d7fSTrond Myklebust /*
14265ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
14275ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
14285ceb9d7fSTrond Myklebust  * lookup.
14295ceb9d7fSTrond Myklebust  *
14305ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
14315ceb9d7fSTrond Myklebust  * we have an inode!
14325ceb9d7fSTrond Myklebust  *
14335ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
14345ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
14355ceb9d7fSTrond Myklebust  */
14365ceb9d7fSTrond Myklebust static int
14375ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
14385ceb9d7fSTrond Myklebust 			 unsigned int flags)
14395ceb9d7fSTrond Myklebust {
14405ceb9d7fSTrond Myklebust 	struct inode *inode;
14415ceb9d7fSTrond Myklebust 	int error;
14425ceb9d7fSTrond Myklebust 
14435ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
14445ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
14455ceb9d7fSTrond Myklebust 
14465ceb9d7fSTrond Myklebust 	if (!inode)
14475ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
14485ceb9d7fSTrond Myklebust 
14495ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
14505ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
14515ceb9d7fSTrond Myklebust 				__func__, dentry);
14525ceb9d7fSTrond Myklebust 		goto out_bad;
14535ceb9d7fSTrond Myklebust 	}
14545ceb9d7fSTrond Myklebust 
1455efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
14565ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
14575ceb9d7fSTrond Myklebust 
14585ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
14595ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
14605ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
14615ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
14625ceb9d7fSTrond Myklebust 		if (error) {
14635ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
14645ceb9d7fSTrond Myklebust 				nfs_zap_caches(dir);
14655ceb9d7fSTrond Myklebust 			goto out_bad;
14665ceb9d7fSTrond Myklebust 		}
14675ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
14685ceb9d7fSTrond Myklebust 		goto out_valid;
14695ceb9d7fSTrond Myklebust 	}
14705ceb9d7fSTrond Myklebust 
14715ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
14725ceb9d7fSTrond Myklebust 		return -ECHILD;
14735ceb9d7fSTrond Myklebust 
14745ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
14755ceb9d7fSTrond Myklebust 		goto out_bad;
14765ceb9d7fSTrond Myklebust 
14775ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
14785ceb9d7fSTrond Myklebust 	error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
14795ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
14805ceb9d7fSTrond Myklebust 	return error;
14815ceb9d7fSTrond Myklebust out_valid:
14825ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
14835ceb9d7fSTrond Myklebust out_bad:
14845ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
14855ceb9d7fSTrond Myklebust 		return -ECHILD;
14865ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
14875ceb9d7fSTrond Myklebust }
14885ceb9d7fSTrond Myklebust 
14895ceb9d7fSTrond Myklebust static int
1490c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1491c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
14925ceb9d7fSTrond Myklebust {
14935ceb9d7fSTrond Myklebust 	struct dentry *parent;
14945ceb9d7fSTrond Myklebust 	struct inode *dir;
14955ceb9d7fSTrond Myklebust 	int ret;
14965ceb9d7fSTrond Myklebust 
14975ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
14985ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
14995ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
15005ceb9d7fSTrond Myklebust 		if (!dir)
15015ceb9d7fSTrond Myklebust 			return -ECHILD;
1502c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
15035ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
15045ceb9d7fSTrond Myklebust 			return -ECHILD;
15055ceb9d7fSTrond Myklebust 	} else {
15065ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1507c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
15085ceb9d7fSTrond Myklebust 		dput(parent);
15095ceb9d7fSTrond Myklebust 	}
15105ceb9d7fSTrond Myklebust 	return ret;
15115ceb9d7fSTrond Myklebust }
15125ceb9d7fSTrond Myklebust 
1513c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1514c7944ebbSTrond Myklebust {
1515c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1516c7944ebbSTrond Myklebust }
1517c7944ebbSTrond Myklebust 
15181da177e4SLinus Torvalds /*
15192b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1520ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1521ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1522ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1523ecf3d1f1SJeff Layton  *
1524ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1525ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1526ecf3d1f1SJeff Layton  */
1527ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1528ecf3d1f1SJeff Layton {
15292b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
15309cdd1d3fSTrond Myklebust 	int error = 0;
1531ecf3d1f1SJeff Layton 
1532ecf3d1f1SJeff Layton 	/*
1533ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1534ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1535ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1536ecf3d1f1SJeff Layton 	 */
1537ecf3d1f1SJeff Layton 	if (!inode) {
15386de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
15396de1472fSAl Viro 				__func__, dentry);
1540ecf3d1f1SJeff Layton 		return 1;
1541ecf3d1f1SJeff Layton 	}
1542ecf3d1f1SJeff Layton 
1543ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
15446de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
15456de1472fSAl Viro 				__func__, dentry);
1546ecf3d1f1SJeff Layton 		return 0;
1547ecf3d1f1SJeff Layton 	}
1548ecf3d1f1SJeff Layton 
1549b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1550ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1551ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1552ecf3d1f1SJeff Layton 	return !error;
1553ecf3d1f1SJeff Layton }
1554ecf3d1f1SJeff Layton 
1555ecf3d1f1SJeff Layton /*
15561da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
15571da177e4SLinus Torvalds  */
1558fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
15591da177e4SLinus Torvalds {
15606de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
15616de1472fSAl Viro 		dentry, dentry->d_flags);
15621da177e4SLinus Torvalds 
156377f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
15642b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
156577f11192STrond Myklebust 		return 1;
156677f11192STrond Myklebust 
15671da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
15681da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
15691da177e4SLinus Torvalds 		return 1;
15701da177e4SLinus Torvalds 	}
15711751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
15721da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
15731da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
15741da177e4SLinus Torvalds 		return 1;
15751da177e4SLinus Torvalds 	}
15761da177e4SLinus Torvalds 	return 0;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds 
15801f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
15811b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
15821b83d707STrond Myklebust {
15831b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
15841f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
158559a707b0STrond Myklebust 	if (inode->i_nlink > 0)
158659a707b0STrond Myklebust 		drop_nlink(inode);
158759a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
158816e14375STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
158916e14375STrond Myklebust 		| NFS_INO_INVALID_CTIME
159059a707b0STrond Myklebust 		| NFS_INO_INVALID_OTHER
159159a707b0STrond Myklebust 		| NFS_INO_REVAL_FORCED;
15921b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
15931b83d707STrond Myklebust }
15941b83d707STrond Myklebust 
15951da177e4SLinus Torvalds /*
15961da177e4SLinus Torvalds  * Called when the dentry loses inode.
15971da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
15981da177e4SLinus Torvalds  */
15991da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
16001da177e4SLinus Torvalds {
160183672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
160283672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
160383672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
160483672d39SNeil Brown 
16051da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1606e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
16071f018458STrond Myklebust 		nfs_drop_nlink(inode);
16081da177e4SLinus Torvalds 	}
16091da177e4SLinus Torvalds 	iput(inode);
16101da177e4SLinus Torvalds }
16111da177e4SLinus Torvalds 
1612b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1613b1942c5fSAl Viro {
1614b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1615b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1616b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1617b1942c5fSAl Viro 			WARN_ON(1);
1618b1942c5fSAl Viro 		else
1619b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1620b1942c5fSAl Viro 	}
1621b1942c5fSAl Viro }
1622b1942c5fSAl Viro 
1623f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
16241da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1625ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
16261da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
16271da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
162836d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1629b1942c5fSAl Viro 	.d_release	= nfs_d_release,
16301da177e4SLinus Torvalds };
1631ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
16321da177e4SLinus Torvalds 
1633597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
16341da177e4SLinus Torvalds {
16351da177e4SLinus Torvalds 	struct dentry *res;
16361da177e4SLinus Torvalds 	struct inode *inode = NULL;
1637e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1638e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
16391775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
1640a1147b82STrond Myklebust 	unsigned long dir_verifier;
16411da177e4SLinus Torvalds 	int error;
16421da177e4SLinus Torvalds 
16436de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
164491d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
16451da177e4SLinus Torvalds 
1646130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1647130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
16481da177e4SLinus Torvalds 
1649fd684071STrond Myklebust 	/*
1650fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1651fd684071STrond Myklebust 	 * but don't hash the dentry.
1652fd684071STrond Myklebust 	 */
16539f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1654130f9ab7SAl Viro 		return NULL;
16551da177e4SLinus Torvalds 
1656e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1657e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1658e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1659e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1660e1fb4d05STrond Myklebust 		goto out;
1661e1fb4d05STrond Myklebust 
166214c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
166314c43f76SDavid Quigley 	if (IS_ERR(label))
166414c43f76SDavid Quigley 		goto out;
166514c43f76SDavid Quigley 
1666a1147b82STrond Myklebust 	dir_verifier = nfs_save_change_attribute(dir);
16676e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
1668f7b37b8bSTrond Myklebust 	error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label);
16691da177e4SLinus Torvalds 	if (error == -ENOENT)
16701da177e4SLinus Torvalds 		goto no_entry;
16711da177e4SLinus Torvalds 	if (error < 0) {
16721da177e4SLinus Torvalds 		res = ERR_PTR(error);
1673bf130914SAl Viro 		goto out_label;
16741da177e4SLinus Torvalds 	}
16751775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1676bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
167703f28e3aSTrond Myklebust 	if (IS_ERR(res))
1678bf130914SAl Viro 		goto out_label;
167954ceac45SDavid Howells 
168063519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
168163519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1682d69ee9b8STrond Myklebust 
16831da177e4SLinus Torvalds no_entry:
168441d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
16859eaef27bSTrond Myklebust 	if (res != NULL) {
16869eaef27bSTrond Myklebust 		if (IS_ERR(res))
1687bf130914SAl Viro 			goto out_label;
16881da177e4SLinus Torvalds 		dentry = res;
16899eaef27bSTrond Myklebust 	}
1690a1147b82STrond Myklebust 	nfs_set_verifier(dentry, dir_verifier);
1691bf130914SAl Viro out_label:
16926e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
169314c43f76SDavid Quigley 	nfs4_label_free(label);
16941da177e4SLinus Torvalds out:
1695e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1696e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
16971da177e4SLinus Torvalds 	return res;
16981da177e4SLinus Torvalds }
1699ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
17001da177e4SLinus Torvalds 
170189d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
17020b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
17031da177e4SLinus Torvalds 
1704f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
17050ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1706b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
17071da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
17081da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
170936d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1710b1942c5fSAl Viro 	.d_release	= nfs_d_release,
17111da177e4SLinus Torvalds };
171289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
17131da177e4SLinus Torvalds 
17148a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
17158a5e929dSAl Viro {
17168a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
17178a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
17188a5e929dSAl Viro 		res |= FMODE_READ;
17198a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
17208a5e929dSAl Viro 		res |= FMODE_WRITE;
17218a5e929dSAl Viro 	return res;
17228a5e929dSAl Viro }
17238a5e929dSAl Viro 
1724532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1725cd9a1c0eSTrond Myklebust {
1726532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1727cd9a1c0eSTrond Myklebust }
1728cd9a1c0eSTrond Myklebust 
1729cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1730cd9a1c0eSTrond Myklebust {
1731f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1732cd9a1c0eSTrond Myklebust 	return 0;
1733cd9a1c0eSTrond Myklebust }
1734cd9a1c0eSTrond Myklebust 
1735d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
17360dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1737b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1738cd9a1c0eSTrond Myklebust {
17390dd2b474SMiklos Szeredi 	int err;
17400dd2b474SMiklos Szeredi 
1741be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
174230d90494SAl Viro 	if (err)
1743d9585277SAl Viro 		goto out;
1744eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
174530d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1746eaa2b82cSNeilBrown 	else
17479821421aSTrond Myklebust 		err = -EOPENSTALE;
1748cd9a1c0eSTrond Myklebust out:
1749d9585277SAl Viro 	return err;
1750cd9a1c0eSTrond Myklebust }
1751cd9a1c0eSTrond Myklebust 
175273a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
175330d90494SAl Viro 		    struct file *file, unsigned open_flags,
175444907d79SAl Viro 		    umode_t mode)
17551da177e4SLinus Torvalds {
1756c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1757cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
17580dd2b474SMiklos Szeredi 	struct dentry *res;
17590dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1760f46e0bd3STrond Myklebust 	struct inode *inode;
17611472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1762c94c0953SAl Viro 	bool switched = false;
176373a09dd9SAl Viro 	int created = 0;
1764898f635cSTrond Myklebust 	int err;
17651da177e4SLinus Torvalds 
17660dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
17672b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
17680dd2b474SMiklos Szeredi 
17691e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
17706de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
17711e7cb3dcSChuck Lever 
17729597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
17739597c13bSJeff Layton 	if (err)
17749597c13bSJeff Layton 		return err;
17759597c13bSJeff Layton 
17760dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
17770dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
177800699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
17790dd2b474SMiklos Szeredi 			/*
17800dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
17810dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
17820dd2b474SMiklos Szeredi 			 * again
17830dd2b474SMiklos Szeredi 			 */
1784d9585277SAl Viro 			return -ENOENT;
17850dd2b474SMiklos Szeredi 		}
17861472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
17871da177e4SLinus Torvalds 		goto no_open;
17881da177e4SLinus Torvalds 	}
17891da177e4SLinus Torvalds 
17900dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1791d9585277SAl Viro 		return -ENAMETOOLONG;
17921da177e4SLinus Torvalds 
17930dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1794dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1795dff25ddbSAndreas Gruenbacher 
1796dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1797dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1798dff25ddbSAndreas Gruenbacher 
1799536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1800dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
18010dd2b474SMiklos Szeredi 	}
1802536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1803536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1804536e43d1STrond Myklebust 		attr.ia_size = 0;
1805cd9a1c0eSTrond Myklebust 	}
1806cd9a1c0eSTrond Myklebust 
1807c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1808c94c0953SAl Viro 		d_drop(dentry);
1809c94c0953SAl Viro 		switched = true;
1810c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1811c94c0953SAl Viro 					  &dentry->d_name, &wq);
1812c94c0953SAl Viro 		if (IS_ERR(dentry))
1813c94c0953SAl Viro 			return PTR_ERR(dentry);
1814c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1815c94c0953SAl Viro 			return finish_no_open(file, dentry);
1816c94c0953SAl Viro 	}
1817c94c0953SAl Viro 
1818532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
18190dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
18200dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1821d9585277SAl Viro 		goto out;
18220dd2b474SMiklos Szeredi 
18236e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
182473a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
182573a09dd9SAl Viro 	if (created)
182673a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1827275bb307STrond Myklebust 	if (IS_ERR(inode)) {
18280dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
18296e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
18302d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1831d20cb71dSAl Viro 		d_drop(dentry);
18320dd2b474SMiklos Szeredi 		switch (err) {
18331da177e4SLinus Torvalds 		case -ENOENT:
1834774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
1835809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
18360dd2b474SMiklos Szeredi 			break;
18371788ea6eSJeff Layton 		case -EISDIR:
18386f926b5bSTrond Myklebust 		case -ENOTDIR:
18396f926b5bSTrond Myklebust 			goto no_open;
18401da177e4SLinus Torvalds 		case -ELOOP:
18410dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
18421da177e4SLinus Torvalds 				goto no_open;
18430dd2b474SMiklos Szeredi 			break;
18441da177e4SLinus Torvalds 			/* case -EINVAL: */
18451da177e4SLinus Torvalds 		default:
18460dd2b474SMiklos Szeredi 			break;
18471da177e4SLinus Torvalds 		}
18481da177e4SLinus Torvalds 		goto out;
18491da177e4SLinus Torvalds 	}
18500dd2b474SMiklos Szeredi 
1851b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
18526e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
18532d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1854d9585277SAl Viro out:
1855c94c0953SAl Viro 	if (unlikely(switched)) {
1856c94c0953SAl Viro 		d_lookup_done(dentry);
1857c94c0953SAl Viro 		dput(dentry);
1858c94c0953SAl Viro 	}
1859d9585277SAl Viro 	return err;
18600dd2b474SMiklos Szeredi 
18611da177e4SLinus Torvalds no_open:
18621472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1863c94c0953SAl Viro 	if (switched) {
1864c94c0953SAl Viro 		d_lookup_done(dentry);
1865c94c0953SAl Viro 		if (!res)
1866c94c0953SAl Viro 			res = dentry;
1867c94c0953SAl Viro 		else
1868c94c0953SAl Viro 			dput(dentry);
1869c94c0953SAl Viro 	}
18700dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1871c94c0953SAl Viro 		return PTR_ERR(res);
1872e45198a6SAl Viro 	return finish_no_open(file, res);
18731da177e4SLinus Torvalds }
187489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
18751da177e4SLinus Torvalds 
1876c7944ebbSTrond Myklebust static int
1877c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1878c7944ebbSTrond Myklebust 			  unsigned int flags)
18791da177e4SLinus Torvalds {
1880657e94b6SNick Piggin 	struct inode *inode;
18811da177e4SLinus Torvalds 
1882fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1883c7944ebbSTrond Myklebust 		goto full_reval;
1884eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
1885c7944ebbSTrond Myklebust 		goto full_reval;
18862b484297STrond Myklebust 
18872b0143b5SDavid Howells 	inode = d_inode(dentry);
18882b484297STrond Myklebust 
18891da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
18901da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
18911da177e4SLinus Torvalds 	 */
1892c7944ebbSTrond Myklebust 	if (inode == NULL)
1893c7944ebbSTrond Myklebust 		goto full_reval;
189449317a7fSNeilBrown 
1895efeda80dSTrond Myklebust 	if (nfs_verifier_is_delegated(dentry))
1896c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1897216d5d06STrond Myklebust 
18981da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
18991da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
1900c7944ebbSTrond Myklebust 		goto full_reval;
1901c7944ebbSTrond Myklebust 
19021da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1903c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1904c7944ebbSTrond Myklebust 		goto reval_dentry;
1905c7944ebbSTrond Myklebust 
1906c7944ebbSTrond Myklebust 	/* Check if the directory changed */
1907c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1908c7944ebbSTrond Myklebust 		goto reval_dentry;
19091da177e4SLinus Torvalds 
19100ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1911c7944ebbSTrond Myklebust 	return 1;
1912c7944ebbSTrond Myklebust reval_dentry:
1913c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
1914c7944ebbSTrond Myklebust 		return -ECHILD;
191542f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
19160ef97dcfSMiklos Szeredi 
1917c7944ebbSTrond Myklebust full_reval:
1918c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
1919c7944ebbSTrond Myklebust }
1920535918f1STrond Myklebust 
1921c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1922c7944ebbSTrond Myklebust {
1923c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
1924c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
1925c0204fd2STrond Myklebust }
1926c0204fd2STrond Myklebust 
19271da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
19281da177e4SLinus Torvalds 
1929406cd915SBenjamin Coddington struct dentry *
1930406cd915SBenjamin Coddington nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle,
19311775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
19321775fd3eSDavid Quigley 				struct nfs4_label *label)
19331da177e4SLinus Torvalds {
1934fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
19352b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
19361da177e4SLinus Torvalds 	struct inode *inode;
1937b0c6108eSAl Viro 	struct dentry *d;
1938406cd915SBenjamin Coddington 	int error;
19391da177e4SLinus Torvalds 
1940fab728e1STrond Myklebust 	d_drop(dentry);
1941fab728e1STrond Myklebust 
19421da177e4SLinus Torvalds 	if (fhandle->size == 0) {
1943f7b37b8bSTrond Myklebust 		error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL);
19441da177e4SLinus Torvalds 		if (error)
1945fab728e1STrond Myklebust 			goto out_error;
19461da177e4SLinus Torvalds 	}
19475724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
19481da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
19491da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
1950a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
1951a841b54dSTrond Myklebust 				fattr, NULL, NULL);
19521da177e4SLinus Torvalds 		if (error < 0)
1953fab728e1STrond Myklebust 			goto out_error;
19541da177e4SLinus Torvalds 	}
19551775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1956b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
1957fab728e1STrond Myklebust out:
1958fab728e1STrond Myklebust 	dput(parent);
1959406cd915SBenjamin Coddington 	return d;
1960fab728e1STrond Myklebust out_error:
1961fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1962406cd915SBenjamin Coddington 	d = ERR_PTR(error);
1963406cd915SBenjamin Coddington 	goto out;
1964406cd915SBenjamin Coddington }
1965406cd915SBenjamin Coddington EXPORT_SYMBOL_GPL(nfs_add_or_obtain);
1966406cd915SBenjamin Coddington 
1967406cd915SBenjamin Coddington /*
1968406cd915SBenjamin Coddington  * Code common to create, mkdir, and mknod.
1969406cd915SBenjamin Coddington  */
1970406cd915SBenjamin Coddington int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
1971406cd915SBenjamin Coddington 				struct nfs_fattr *fattr,
1972406cd915SBenjamin Coddington 				struct nfs4_label *label)
1973406cd915SBenjamin Coddington {
1974406cd915SBenjamin Coddington 	struct dentry *d;
1975406cd915SBenjamin Coddington 
1976406cd915SBenjamin Coddington 	d = nfs_add_or_obtain(dentry, fhandle, fattr, label);
1977406cd915SBenjamin Coddington 	if (IS_ERR(d))
1978406cd915SBenjamin Coddington 		return PTR_ERR(d);
1979406cd915SBenjamin Coddington 
1980406cd915SBenjamin Coddington 	/* Callers don't care */
1981406cd915SBenjamin Coddington 	dput(d);
1982406cd915SBenjamin Coddington 	return 0;
19831da177e4SLinus Torvalds }
1984ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds /*
19871da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
19881da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
19891da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
19901da177e4SLinus Torvalds  * reply path made it appear to have failed.
19911da177e4SLinus Torvalds  */
1992597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1993ebfc3b49SAl Viro 		umode_t mode, bool excl)
19941da177e4SLinus Torvalds {
19951da177e4SLinus Torvalds 	struct iattr attr;
1996ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
19971da177e4SLinus Torvalds 	int error;
19981da177e4SLinus Torvalds 
19991e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
20006de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	attr.ia_mode = mode;
20031da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
20041da177e4SLinus Torvalds 
20058b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
20068867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
20078b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
20081da177e4SLinus Torvalds 	if (error != 0)
20091da177e4SLinus Torvalds 		goto out_err;
20101da177e4SLinus Torvalds 	return 0;
20111da177e4SLinus Torvalds out_err:
20121da177e4SLinus Torvalds 	d_drop(dentry);
20131da177e4SLinus Torvalds 	return error;
20141da177e4SLinus Torvalds }
2015ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds /*
20181da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
20191da177e4SLinus Torvalds  */
2020597d9289SBryan Schumaker int
20211a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
20221da177e4SLinus Torvalds {
20231da177e4SLinus Torvalds 	struct iattr attr;
20241da177e4SLinus Torvalds 	int status;
20251da177e4SLinus Torvalds 
20261e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
20276de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds 	attr.ia_mode = mode;
20301da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
20311da177e4SLinus Torvalds 
20321ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
20331da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
20341ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
20351da177e4SLinus Torvalds 	if (status != 0)
20361da177e4SLinus Torvalds 		goto out_err;
20371da177e4SLinus Torvalds 	return 0;
20381da177e4SLinus Torvalds out_err:
20391da177e4SLinus Torvalds 	d_drop(dentry);
20401da177e4SLinus Torvalds 	return status;
20411da177e4SLinus Torvalds }
2042ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
20431da177e4SLinus Torvalds 
20441da177e4SLinus Torvalds /*
20451da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
20461da177e4SLinus Torvalds  */
2047597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20481da177e4SLinus Torvalds {
20491da177e4SLinus Torvalds 	struct iattr attr;
20501da177e4SLinus Torvalds 	int error;
20511da177e4SLinus Torvalds 
20521e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
20536de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
20561da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
20571da177e4SLinus Torvalds 
20581ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
20591da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
20601ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
20611da177e4SLinus Torvalds 	if (error != 0)
20621da177e4SLinus Torvalds 		goto out_err;
20631da177e4SLinus Torvalds 	return 0;
20641da177e4SLinus Torvalds out_err:
20651da177e4SLinus Torvalds 	d_drop(dentry);
20661da177e4SLinus Torvalds 	return error;
20671da177e4SLinus Torvalds }
2068ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
20691da177e4SLinus Torvalds 
2070d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
2071d45b9d8bSTrond Myklebust {
2072dc3f4198SAl Viro 	if (simple_positive(dentry))
2073d45b9d8bSTrond Myklebust 		d_delete(dentry);
2074d45b9d8bSTrond Myklebust }
2075d45b9d8bSTrond Myklebust 
2076597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
20771da177e4SLinus Torvalds {
20781da177e4SLinus Torvalds 	int error;
20791da177e4SLinus Torvalds 
20801e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
20816de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
20821da177e4SLinus Torvalds 
20831ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
20842b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
2085884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
20861da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
20871da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
2088ba6c0592STrond Myklebust 		switch (error) {
2089ba6c0592STrond Myklebust 		case 0:
20902b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
2091ba6c0592STrond Myklebust 			break;
2092ba6c0592STrond Myklebust 		case -ENOENT:
2093d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
2094ba6c0592STrond Myklebust 		}
2095884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
2096ba6c0592STrond Myklebust 	} else
2097ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
20981ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 	return error;
21011da177e4SLinus Torvalds }
2102ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
21031da177e4SLinus Torvalds 
21041da177e4SLinus Torvalds /*
21051da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
21061da177e4SLinus Torvalds  * and after checking that the file has only one user.
21071da177e4SLinus Torvalds  *
21081da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
21091da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
21101da177e4SLinus Torvalds  */
21111da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
21121da177e4SLinus Torvalds {
21132b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
21142b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
21151da177e4SLinus Torvalds 	int error = -EBUSY;
21161da177e4SLinus Torvalds 
21176de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
21201da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
21211da177e4SLinus Torvalds 		error = 0;
21221da177e4SLinus Torvalds 		goto out;
21231da177e4SLinus Torvalds 	}
21241da177e4SLinus Torvalds 
21251ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
21261da177e4SLinus Torvalds 	if (inode != NULL) {
2127912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
21281da177e4SLinus Torvalds 		if (error == 0)
21291b83d707STrond Myklebust 			nfs_drop_nlink(inode);
21301da177e4SLinus Torvalds 	} else
2131912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
2132d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
2133d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
21341ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
21351da177e4SLinus Torvalds out:
21361da177e4SLinus Torvalds 	return error;
21371da177e4SLinus Torvalds }
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
21401da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
21411da177e4SLinus Torvalds  *
21421da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
21431da177e4SLinus Torvalds  */
2144597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
21451da177e4SLinus Torvalds {
21461da177e4SLinus Torvalds 	int error;
21471da177e4SLinus Torvalds 	int need_rehash = 0;
21481da177e4SLinus Torvalds 
21491e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
21506de1472fSAl Viro 		dir->i_ino, dentry);
21511da177e4SLinus Torvalds 
21521ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
21531da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
215484d08fa8SAl Viro 	if (d_count(dentry) > 1) {
21551da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
2156ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
21572b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
21581da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
21591ca42382STrond Myklebust 		goto out;
21601da177e4SLinus Torvalds 	}
21611da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
21621da177e4SLinus Torvalds 		__d_drop(dentry);
21631da177e4SLinus Torvalds 		need_rehash = 1;
21641da177e4SLinus Torvalds 	}
21651da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
21661da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
2167d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
21681da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
21691da177e4SLinus Torvalds 	} else if (need_rehash)
21701da177e4SLinus Torvalds 		d_rehash(dentry);
21711ca42382STrond Myklebust out:
21721ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
21731da177e4SLinus Torvalds 	return error;
21741da177e4SLinus Torvalds }
2175ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
21761da177e4SLinus Torvalds 
2177873101b3SChuck Lever /*
2178873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
2179873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
2180873101b3SChuck Lever  * using prepare_write/commit_write.
2181873101b3SChuck Lever  *
2182873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
2183873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
2184873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
2185873101b3SChuck Lever  * symlink request has completed on the server.
2186873101b3SChuck Lever  *
2187873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
2188873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2189873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
2190873101b3SChuck Lever  * and move the raw page into its mapping.
2191873101b3SChuck Lever  */
2192597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
21931da177e4SLinus Torvalds {
2194873101b3SChuck Lever 	struct page *page;
2195873101b3SChuck Lever 	char *kaddr;
21961da177e4SLinus Torvalds 	struct iattr attr;
2197873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
21981da177e4SLinus Torvalds 	int error;
21991da177e4SLinus Torvalds 
22001e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
22016de1472fSAl Viro 		dir->i_ino, dentry, symname);
22021da177e4SLinus Torvalds 
2203873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2204873101b3SChuck Lever 		return -ENAMETOOLONG;
22051da177e4SLinus Torvalds 
2206873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2207873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
22081da177e4SLinus Torvalds 
2209e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
221076566991STrond Myklebust 	if (!page)
2211873101b3SChuck Lever 		return -ENOMEM;
2212873101b3SChuck Lever 
2213e8ecde25SAl Viro 	kaddr = page_address(page);
2214873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2215873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2216873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2217873101b3SChuck Lever 
22181ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
221994a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
22201ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2221873101b3SChuck Lever 	if (error != 0) {
22221e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2223873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
22246de1472fSAl Viro 			dentry, symname, error);
22251da177e4SLinus Torvalds 		d_drop(dentry);
2226873101b3SChuck Lever 		__free_page(page);
22271da177e4SLinus Torvalds 		return error;
22281da177e4SLinus Torvalds 	}
22291da177e4SLinus Torvalds 
2230873101b3SChuck Lever 	/*
2231873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2232873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2233873101b3SChuck Lever 	 */
22342b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2235873101b3SChuck Lever 							GFP_KERNEL)) {
2236873101b3SChuck Lever 		SetPageUptodate(page);
2237873101b3SChuck Lever 		unlock_page(page);
2238a0b54addSRafael Aquini 		/*
2239a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2240a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2241a0b54addSRafael Aquini 		 */
224209cbfeafSKirill A. Shutemov 		put_page(page);
2243873101b3SChuck Lever 	} else
2244873101b3SChuck Lever 		__free_page(page);
2245873101b3SChuck Lever 
2246873101b3SChuck Lever 	return 0;
2247873101b3SChuck Lever }
2248ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2249873101b3SChuck Lever 
2250597d9289SBryan Schumaker int
22511da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
22521da177e4SLinus Torvalds {
22532b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
22541da177e4SLinus Torvalds 	int error;
22551da177e4SLinus Torvalds 
22566de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
22576de1472fSAl Viro 		old_dentry, dentry);
22581da177e4SLinus Torvalds 
22591fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
22609697d234STrond Myklebust 	d_drop(dentry);
22611da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2262cf809556STrond Myklebust 	if (error == 0) {
22637de9c6eeSAl Viro 		ihold(inode);
22649697d234STrond Myklebust 		d_add(dentry, inode);
2265cf809556STrond Myklebust 	}
22661fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
22671da177e4SLinus Torvalds 	return error;
22681da177e4SLinus Torvalds }
2269ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds /*
22721da177e4SLinus Torvalds  * RENAME
22731da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
22741da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
22751da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
22761da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
22771da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
22781da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
22791da177e4SLinus Torvalds  *
22801da177e4SLinus Torvalds  * FIXED.
22811da177e4SLinus Torvalds  *
22821da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
22831da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
22841da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
22851da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
22861da177e4SLinus Torvalds  * using the inode layer
22871da177e4SLinus Torvalds  *
22881da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
22891da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
22901da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
22911da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
22921da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
22931da177e4SLinus Torvalds  * the rename.
22941da177e4SLinus Torvalds  */
2295597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
22961cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
22971cd66c93SMiklos Szeredi 	       unsigned int flags)
22981da177e4SLinus Torvalds {
22992b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
23002b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2301d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
230280a491fdSJeff Layton 	struct rpc_task *task;
23031da177e4SLinus Torvalds 	int error = -EBUSY;
23041da177e4SLinus Torvalds 
23051cd66c93SMiklos Szeredi 	if (flags)
23061cd66c93SMiklos Szeredi 		return -EINVAL;
23071cd66c93SMiklos Szeredi 
23086de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
23096de1472fSAl Viro 		 old_dentry, new_dentry,
231084d08fa8SAl Viro 		 d_count(new_dentry));
23111da177e4SLinus Torvalds 
231270ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
23131da177e4SLinus Torvalds 	/*
231428f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
231528f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
231628f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
231728f79a1aSMiklos Szeredi 	 * the new target.
23181da177e4SLinus Torvalds 	 */
231927226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
232027226104SMiklos Szeredi 		/*
232127226104SMiklos Szeredi 		 * To prevent any new references to the target during the
232227226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
232327226104SMiklos Szeredi 		 */
2324d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
232527226104SMiklos Szeredi 			d_drop(new_dentry);
2326d9f29500SBenjamin Coddington 			rehash = new_dentry;
2327d9f29500SBenjamin Coddington 		}
232827226104SMiklos Szeredi 
232984d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
23301da177e4SLinus Torvalds 			int err;
233127226104SMiklos Szeredi 
23321da177e4SLinus Torvalds 			/* copy the target dentry's name */
23331da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
23341da177e4SLinus Torvalds 					 &new_dentry->d_name);
23351da177e4SLinus Torvalds 			if (!dentry)
23361da177e4SLinus Torvalds 				goto out;
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
23391da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
234024e93025SMiklos Szeredi 			if (err)
23411da177e4SLinus Torvalds 				goto out;
234224e93025SMiklos Szeredi 
234324e93025SMiklos Szeredi 			new_dentry = dentry;
2344d9f29500SBenjamin Coddington 			rehash = NULL;
234524e93025SMiklos Szeredi 			new_inode = NULL;
2346b1e4adf4STrond Myklebust 		}
234727226104SMiklos Szeredi 	}
23481da177e4SLinus Torvalds 
2349d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
235080a491fdSJeff Layton 	if (IS_ERR(task)) {
235180a491fdSJeff Layton 		error = PTR_ERR(task);
235280a491fdSJeff Layton 		goto out;
235380a491fdSJeff Layton 	}
235480a491fdSJeff Layton 
235580a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2356818a8dbeSBenjamin Coddington 	if (error != 0) {
2357818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2358818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2359818a8dbeSBenjamin Coddington 		smp_wmb();
2360818a8dbeSBenjamin Coddington 	} else
236180a491fdSJeff Layton 		error = task->tk_status;
236280a491fdSJeff Layton 	rpc_put_task(task);
236359a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
236459a707b0STrond Myklebust 	if (error == 0) {
236559a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
236659a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
236759a707b0STrond Myklebust 		NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
236859a707b0STrond Myklebust 			| NFS_INO_INVALID_CTIME
236959a707b0STrond Myklebust 			| NFS_INO_REVAL_FORCED;
237059a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
237159a707b0STrond Myklebust 	}
23721da177e4SLinus Torvalds out:
2373d9f29500SBenjamin Coddington 	if (rehash)
2374d9f29500SBenjamin Coddington 		d_rehash(rehash);
237570ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
237670ded201STrond Myklebust 			new_dir, new_dentry, error);
2377d9f29500SBenjamin Coddington 	if (!error) {
2378d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2379d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2380d9f29500SBenjamin Coddington 		/*
2381d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2382d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2383d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2384d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2385d9f29500SBenjamin Coddington 		 */
2386d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2387d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2388d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2389d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2390d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2391d9f29500SBenjamin Coddington 
23921da177e4SLinus Torvalds 	/* new dentry created? */
23931da177e4SLinus Torvalds 	if (dentry)
23941da177e4SLinus Torvalds 		dput(dentry);
23951da177e4SLinus Torvalds 	return error;
23961da177e4SLinus Torvalds }
2397ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
23981da177e4SLinus Torvalds 
2399cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2400cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2401cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2402cfcea3e8STrond Myklebust 
2403a8b373eeSTrond Myklebust static unsigned long nfs_access_max_cachesize = 4*1024*1024;
24043a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
24053a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
24063a505845STrond Myklebust 
24071c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
24081c3c07e9STrond Myklebust {
2409b68572e0SNeilBrown 	put_cred(entry->cred);
2410f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
24114e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2412cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
24134e857c58SPeter Zijlstra 	smp_mb__after_atomic();
24141c3c07e9STrond Myklebust }
24151c3c07e9STrond Myklebust 
24161a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
24171a81bb8aSTrond Myklebust {
24181a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
24191a81bb8aSTrond Myklebust 
24201a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
24211a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
24221a81bb8aSTrond Myklebust 		list_del(&cache->lru);
24231a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
24241a81bb8aSTrond Myklebust 	}
24251a81bb8aSTrond Myklebust }
24261a81bb8aSTrond Myklebust 
24273a505845STrond Myklebust static unsigned long
24283a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2429979df72eSTrond Myklebust {
2430979df72eSTrond Myklebust 	LIST_HEAD(head);
2431aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2432979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
24331ab6c499SDave Chinner 	long freed = 0;
2434979df72eSTrond Myklebust 
2435a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2436aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2437979df72eSTrond Myklebust 		struct inode *inode;
2438979df72eSTrond Myklebust 
2439979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2440979df72eSTrond Myklebust 			break;
24419c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2442979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2443979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2444979df72eSTrond Myklebust 			goto remove_lru_entry;
2445979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2446979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2447979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2448979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
24491ab6c499SDave Chinner 		freed++;
2450979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2451979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2452979df72eSTrond Myklebust 					&nfs_access_lru_list);
2453979df72eSTrond Myklebust 		else {
2454979df72eSTrond Myklebust remove_lru_entry:
2455979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
24564e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2457979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
24584e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2459979df72eSTrond Myklebust 		}
246059844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2461979df72eSTrond Myklebust 	}
2462979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
24631a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
24641ab6c499SDave Chinner 	return freed;
24651ab6c499SDave Chinner }
24661ab6c499SDave Chinner 
24671ab6c499SDave Chinner unsigned long
24683a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
24693a505845STrond Myklebust {
24703a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
24713a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
24723a505845STrond Myklebust 
24733a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
24743a505845STrond Myklebust 		return SHRINK_STOP;
24753a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
24763a505845STrond Myklebust }
24773a505845STrond Myklebust 
24783a505845STrond Myklebust 
24793a505845STrond Myklebust unsigned long
24801ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
24811ab6c499SDave Chinner {
248255f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2483979df72eSTrond Myklebust }
2484979df72eSTrond Myklebust 
24853a505845STrond Myklebust static void
24863a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
24873a505845STrond Myklebust {
24883a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
24893a505845STrond Myklebust 	unsigned long diff;
24903a505845STrond Myklebust 	unsigned int nr_to_scan;
24913a505845STrond Myklebust 
24923a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
24933a505845STrond Myklebust 		return;
24943a505845STrond Myklebust 	nr_to_scan = 100;
24953a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
24963a505845STrond Myklebust 	if (diff < nr_to_scan)
24973a505845STrond Myklebust 		nr_to_scan = diff;
24983a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
24993a505845STrond Myklebust }
25003a505845STrond Myklebust 
25011a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
25021c3c07e9STrond Myklebust {
25031c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
25041a81bb8aSTrond Myklebust 	struct rb_node *n;
25051c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
25061c3c07e9STrond Myklebust 
25071c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
25081c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
25091c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
25101c3c07e9STrond Myklebust 		rb_erase(n, root_node);
25111a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
25121c3c07e9STrond Myklebust 	}
25131c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
25141c3c07e9STrond Myklebust }
25151c3c07e9STrond Myklebust 
25161c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
25171c3c07e9STrond Myklebust {
25181a81bb8aSTrond Myklebust 	LIST_HEAD(head);
25191a81bb8aSTrond Myklebust 
25201a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
25211a81bb8aSTrond Myklebust 		return;
2522cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2523cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
25241a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2525cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2526cfcea3e8STrond Myklebust 
25271c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
25281a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
25291a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
25301a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
25311a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
25321c3c07e9STrond Myklebust }
25331c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
25341c3c07e9STrond Myklebust 
2535b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
25361c3c07e9STrond Myklebust {
25371c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
25381c3c07e9STrond Myklebust 
25391c3c07e9STrond Myklebust 	while (n != NULL) {
2540b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2541b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
2542b68572e0SNeilBrown 		int cmp = cred_fscmp(cred, entry->cred);
25431c3c07e9STrond Myklebust 
2544b68572e0SNeilBrown 		if (cmp < 0)
25451c3c07e9STrond Myklebust 			n = n->rb_left;
2546b68572e0SNeilBrown 		else if (cmp > 0)
25471c3c07e9STrond Myklebust 			n = n->rb_right;
25481c3c07e9STrond Myklebust 		else
25491c3c07e9STrond Myklebust 			return entry;
25501c3c07e9STrond Myklebust 	}
25511c3c07e9STrond Myklebust 	return NULL;
25521c3c07e9STrond Myklebust }
25531c3c07e9STrond Myklebust 
2554d2ae4f8bSFrank van der Linden static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
25551da177e4SLinus Torvalds {
255655296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
25571c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
255857b69181STrond Myklebust 	bool retry = true;
255957b69181STrond Myklebust 	int err;
25601da177e4SLinus Torvalds 
25611c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
256257b69181STrond Myklebust 	for(;;) {
25631c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
25641c3c07e9STrond Myklebust 			goto out_zap;
25651c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
256657b69181STrond Myklebust 		err = -ENOENT;
25671c3c07e9STrond Myklebust 		if (cache == NULL)
25681c3c07e9STrond Myklebust 			goto out;
256957b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
257021c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
257157b69181STrond Myklebust 			break;
25725c965db8STrond Myklebust 		if (!retry)
25735c965db8STrond Myklebust 			break;
257457b69181STrond Myklebust 		err = -ECHILD;
257557b69181STrond Myklebust 		if (!may_block)
257657b69181STrond Myklebust 			goto out;
257757b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
257857b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
257957b69181STrond Myklebust 		if (err)
258057b69181STrond Myklebust 			return err;
258157b69181STrond Myklebust 		spin_lock(&inode->i_lock);
258257b69181STrond Myklebust 		retry = false;
258357b69181STrond Myklebust 	}
25841c3c07e9STrond Myklebust 	res->cred = cache->cred;
25851c3c07e9STrond Myklebust 	res->mask = cache->mask;
2586cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
25871c3c07e9STrond Myklebust 	err = 0;
25881c3c07e9STrond Myklebust out:
25891c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
25901c3c07e9STrond Myklebust 	return err;
25911c3c07e9STrond Myklebust out_zap:
25921a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
25931a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
25941c3c07e9STrond Myklebust 	return -ENOENT;
25951c3c07e9STrond Myklebust }
25961c3c07e9STrond Myklebust 
2597b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
2598f682a398SNeilBrown {
2599f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2600f682a398SNeilBrown 	 * but do it without locking.
2601f682a398SNeilBrown 	 */
2602f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2603f682a398SNeilBrown 	struct nfs_access_entry *cache;
2604f682a398SNeilBrown 	int err = -ECHILD;
2605f682a398SNeilBrown 	struct list_head *lh;
2606f682a398SNeilBrown 
2607f682a398SNeilBrown 	rcu_read_lock();
2608f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2609f682a398SNeilBrown 		goto out;
26109f01eb5dSMadhuparna Bhowmik 	lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru));
2611f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2612f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
26139a206de2STrond Myklebust 	    cred_fscmp(cred, cache->cred) != 0)
2614f682a398SNeilBrown 		cache = NULL;
2615f682a398SNeilBrown 	if (cache == NULL)
2616f682a398SNeilBrown 		goto out;
261721c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2618f682a398SNeilBrown 		goto out;
2619f682a398SNeilBrown 	res->cred = cache->cred;
2620f682a398SNeilBrown 	res->mask = cache->mask;
262121c3ba7eSTrond Myklebust 	err = 0;
2622f682a398SNeilBrown out:
2623f682a398SNeilBrown 	rcu_read_unlock();
2624f682a398SNeilBrown 	return err;
2625f682a398SNeilBrown }
2626f682a398SNeilBrown 
2627d2ae4f8bSFrank van der Linden int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct
2628d2ae4f8bSFrank van der Linden nfs_access_entry *res, bool may_block)
2629d2ae4f8bSFrank van der Linden {
2630d2ae4f8bSFrank van der Linden 	int status;
2631d2ae4f8bSFrank van der Linden 
2632d2ae4f8bSFrank van der Linden 	status = nfs_access_get_cached_rcu(inode, cred, res);
2633d2ae4f8bSFrank van der Linden 	if (status != 0)
2634d2ae4f8bSFrank van der Linden 		status = nfs_access_get_cached_locked(inode, cred, res,
2635d2ae4f8bSFrank van der Linden 		    may_block);
2636d2ae4f8bSFrank van der Linden 
2637d2ae4f8bSFrank van der Linden 	return status;
2638d2ae4f8bSFrank van der Linden }
2639d2ae4f8bSFrank van der Linden EXPORT_SYMBOL_GPL(nfs_access_get_cached);
2640d2ae4f8bSFrank van der Linden 
26411c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
26421c3c07e9STrond Myklebust {
2643cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2644cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
26451c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
26461c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
26471c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2648b68572e0SNeilBrown 	int cmp;
26491c3c07e9STrond Myklebust 
26501c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
26511c3c07e9STrond Myklebust 	while (*p != NULL) {
26521c3c07e9STrond Myklebust 		parent = *p;
26531c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2654b68572e0SNeilBrown 		cmp = cred_fscmp(set->cred, entry->cred);
26551c3c07e9STrond Myklebust 
2656b68572e0SNeilBrown 		if (cmp < 0)
26571c3c07e9STrond Myklebust 			p = &parent->rb_left;
2658b68572e0SNeilBrown 		else if (cmp > 0)
26591c3c07e9STrond Myklebust 			p = &parent->rb_right;
26601c3c07e9STrond Myklebust 		else
26611c3c07e9STrond Myklebust 			goto found;
26621c3c07e9STrond Myklebust 	}
26631c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
26641c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2665cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
26661c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
26671c3c07e9STrond Myklebust 	return;
26681c3c07e9STrond Myklebust found:
26691c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2670cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2671cfcea3e8STrond Myklebust 	list_del(&entry->lru);
26721c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
26731c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
26741da177e4SLinus Torvalds }
26751da177e4SLinus Torvalds 
26766168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
26771da177e4SLinus Torvalds {
26781c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
26791c3c07e9STrond Myklebust 	if (cache == NULL)
26801c3c07e9STrond Myklebust 		return;
26811c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
2682b68572e0SNeilBrown 	cache->cred = get_cred(set->cred);
26831da177e4SLinus Torvalds 	cache->mask = set->mask;
26841c3c07e9STrond Myklebust 
2685f682a398SNeilBrown 	/* The above field assignments must be visible
2686f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2687f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2688f682a398SNeilBrown 	 */
2689f682a398SNeilBrown 	smp_wmb();
26901c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2691cfcea3e8STrond Myklebust 
2692cfcea3e8STrond Myklebust 	/* Update accounting */
26934e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2694cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
26954e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2696cfcea3e8STrond Myklebust 
2697cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
26981a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2699cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
27001a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
27011a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
27021a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2703cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2704cfcea3e8STrond Myklebust 	}
27053a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
27061da177e4SLinus Torvalds }
27076168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
27086168f62cSWeston Andros Adamson 
27093c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
27103c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
27113c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
27123c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
27133c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
27143c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2715ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
27163c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
27173c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
271815d4b73aSTrond Myklebust static int
2719ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
272015d4b73aSTrond Myklebust {
272115d4b73aSTrond Myklebust 	int mask = 0;
272215d4b73aSTrond Myklebust 
272315d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
272415d4b73aSTrond Myklebust 		mask |= MAY_READ;
2725ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2726ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
272715d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2728ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
272915d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2730ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2731ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2732ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2733ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
273415d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2735ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2736ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
273715d4b73aSTrond Myklebust 	return mask;
273815d4b73aSTrond Myklebust }
273915d4b73aSTrond Myklebust 
27406168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
27416168f62cSWeston Andros Adamson {
2742bd8b2441STrond Myklebust 	entry->mask = access_result;
27436168f62cSWeston Andros Adamson }
27446168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
27451da177e4SLinus Torvalds 
2746b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
27471da177e4SLinus Torvalds {
27481da177e4SLinus Torvalds 	struct nfs_access_entry cache;
274957b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2750e8194b7dSTrond Myklebust 	int cache_mask = -1;
27511da177e4SLinus Torvalds 	int status;
27521da177e4SLinus Torvalds 
2753f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2754f4ce1299STrond Myklebust 
275557b69181STrond Myklebust 	status = nfs_access_get_cached(inode, cred, &cache, may_block);
27561da177e4SLinus Torvalds 	if (status == 0)
2757f4ce1299STrond Myklebust 		goto out_cached;
27581da177e4SLinus Torvalds 
2759f3324a2aSNeilBrown 	status = -ECHILD;
276057b69181STrond Myklebust 	if (!may_block)
2761f3324a2aSNeilBrown 		goto out;
2762f3324a2aSNeilBrown 
27631750d929SAnna Schumaker 	/*
27641750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
27651750d929SAnna Schumaker 	 */
27661750d929SAnna Schumaker 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
276772832a24SFrank van der Linden 	if (nfs_server_capable(inode, NFS_CAP_XATTR)) {
276872832a24SFrank van der Linden 		cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE |
276972832a24SFrank van der Linden 		    NFS_ACCESS_XALIST;
277072832a24SFrank van der Linden 	}
27711750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
27721750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
27731750d929SAnna Schumaker 	else
27741750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
27751da177e4SLinus Torvalds 	cache.cred = cred;
27761da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2777a71ee337SSuresh Jayaraman 	if (status != 0) {
2778a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2779a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
278093ce4af7STrond Myklebust 				nfs_set_inode_stale(inode);
278193ce4af7STrond Myklebust 			else
278293ce4af7STrond Myklebust 				nfs_zap_caches(inode);
2783a71ee337SSuresh Jayaraman 		}
2784f4ce1299STrond Myklebust 		goto out;
2785a71ee337SSuresh Jayaraman 	}
27861da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2787f4ce1299STrond Myklebust out_cached:
2788ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
2789bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2790f4ce1299STrond Myklebust 		status = -EACCES;
27911da177e4SLinus Torvalds out:
2792e8194b7dSTrond Myklebust 	trace_nfs_access_exit(inode, mask, cache_mask, status);
2793f4ce1299STrond Myklebust 	return status;
27941da177e4SLinus Torvalds }
27951da177e4SLinus Torvalds 
2796af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2797af22f94aSTrond Myklebust {
2798af22f94aSTrond Myklebust 	int mask = 0;
2799af22f94aSTrond Myklebust 
2800f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2801f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2802f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2803f8d9a897SWeston Andros Adamson 	} else {
28048a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2805af22f94aSTrond Myklebust 			mask |= MAY_READ;
28068a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2807af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2808f8d9a897SWeston Andros Adamson 	}
2809f8d9a897SWeston Andros Adamson 
2810af22f94aSTrond Myklebust 	return mask;
2811af22f94aSTrond Myklebust }
2812af22f94aSTrond Myklebust 
2813b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
2814af22f94aSTrond Myklebust {
2815af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2816af22f94aSTrond Myklebust }
281789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2818af22f94aSTrond Myklebust 
28195c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
28205c5fc09aSTrond Myklebust {
28215c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
282221c3ba7eSTrond Myklebust 	int ret = 0;
28235c5fc09aSTrond Myklebust 
28243825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
28253825827eSTrond Myklebust 		return 0;
2826cf834027STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
28275c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
282821c3ba7eSTrond Myklebust 			return -ECHILD;
282921c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
283021c3ba7eSTrond Myklebust 	}
28315c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
28325c5fc09aSTrond Myklebust 		ret = -EACCES;
28335c5fc09aSTrond Myklebust 	return ret;
28345c5fc09aSTrond Myklebust }
28355c5fc09aSTrond Myklebust 
283610556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
28371da177e4SLinus Torvalds {
2838b68572e0SNeilBrown 	const struct cred *cred = current_cred();
28391da177e4SLinus Torvalds 	int res = 0;
28401da177e4SLinus Torvalds 
284191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
284291d5b470SChuck Lever 
2843e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
28441da177e4SLinus Torvalds 		goto out;
28451da177e4SLinus Torvalds 	/* Is this sys_access() ? */
28469cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
28471da177e4SLinus Torvalds 		goto force_lookup;
28481da177e4SLinus Torvalds 
28491da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
28501da177e4SLinus Torvalds 		case S_IFLNK:
28511da177e4SLinus Torvalds 			goto out;
28521da177e4SLinus Torvalds 		case S_IFREG:
2853762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2854762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2855762674f8STrond Myklebust 				return 0;
28561da177e4SLinus Torvalds 			break;
28571da177e4SLinus Torvalds 		case S_IFDIR:
28581da177e4SLinus Torvalds 			/*
28591da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
28601da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
28611da177e4SLinus Torvalds 			 */
28621da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
28631da177e4SLinus Torvalds 				goto out;
28641da177e4SLinus Torvalds 	}
28651da177e4SLinus Torvalds 
28661da177e4SLinus Torvalds force_lookup:
28671da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
28681da177e4SLinus Torvalds 		goto out_notsup;
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds 	res = nfs_do_access(inode, cred, mask);
28711da177e4SLinus Torvalds out:
28725c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
28735c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2874f696a365SMiklos Szeredi 
28751e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
28761e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
28771da177e4SLinus Torvalds 	return res;
28781da177e4SLinus Torvalds out_notsup:
2879d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2880d51ac1a8SNeilBrown 		return -ECHILD;
2881d51ac1a8SNeilBrown 
28821da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
28831da177e4SLinus Torvalds 	if (res == 0)
28842830ba7fSAl Viro 		res = generic_permission(inode, mask);
28851e7cb3dcSChuck Lever 	goto out;
28861da177e4SLinus Torvalds }
2887ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds /*
28901da177e4SLinus Torvalds  * Local variables:
28911da177e4SLinus Torvalds  *  version-control: t
28921da177e4SLinus Torvalds  *  kept-new-versions: 5
28931da177e4SLinus Torvalds  * End:
28941da177e4SLinus Torvalds  */
2895