xref: /openbmc/linux/fs/nfs/dir.c (revision 1c341b777501613aad83f9c233a3fe5701cff083)
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,
61b044f645SBenjamin Coddington 	.iterate	= 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);
83*1c341b77STrond Myklebust 		if (list_empty(&nfsi->open_files) &&
84*1c341b77STrond Myklebust 		    (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER))
85*1c341b77STrond Myklebust 			nfsi->cache_validity |= NFS_INO_INVALID_DATA |
86*1c341b77STrond 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 {
14188b8e133SChuck Lever 	int size;
142d1bacf9eSBryan Schumaker 	int eof_index;
143d1bacf9eSBryan Schumaker 	u64 last_cookie;
144d1bacf9eSBryan Schumaker 	struct nfs_cache_array_entry array[0];
145d1bacf9eSBryan Schumaker };
146d1bacf9eSBryan Schumaker 
147be4c2d47Sluanshi struct readdirvec {
148be4c2d47Sluanshi 	unsigned long nr;
149be4c2d47Sluanshi 	unsigned long index;
150be4c2d47Sluanshi 	struct page *pages[NFS_MAX_READDIR_RAPAGES];
151be4c2d47Sluanshi };
152be4c2d47Sluanshi 
153a7a3b1e9SBenjamin Coddington typedef int (*decode_dirent_t)(struct xdr_stream *, struct nfs_entry *, bool);
1541da177e4SLinus Torvalds typedef struct {
1551da177e4SLinus Torvalds 	struct file	*file;
1561da177e4SLinus Torvalds 	struct page	*page;
15723db8620SAl Viro 	struct dir_context *ctx;
1581da177e4SLinus Torvalds 	unsigned long	page_index;
159be4c2d47Sluanshi 	struct readdirvec pvec;
160f0dd2136STrond Myklebust 	u64		*dir_cookie;
1610aded708STrond Myklebust 	u64		last_cookie;
162f0dd2136STrond Myklebust 	loff_t		current_index;
1631da177e4SLinus Torvalds 	decode_dirent_t	decode;
164d1bacf9eSBryan Schumaker 
1651f4eab7eSNeil Brown 	unsigned long	timestamp;
1664704f0e2STrond Myklebust 	unsigned long	gencount;
167d1bacf9eSBryan Schumaker 	unsigned int	cache_entry_index;
168a7a3b1e9SBenjamin Coddington 	bool plus;
169a7a3b1e9SBenjamin Coddington 	bool eof;
1701da177e4SLinus Torvalds } nfs_readdir_descriptor_t;
1711da177e4SLinus Torvalds 
172d1bacf9eSBryan Schumaker /*
173d1bacf9eSBryan Schumaker  * we are freeing strings created by nfs_add_to_readdir_array()
174d1bacf9eSBryan Schumaker  */
175d1bacf9eSBryan Schumaker static
17611de3b11STrond Myklebust void nfs_readdir_clear_array(struct page *page)
177d1bacf9eSBryan Schumaker {
17811de3b11STrond Myklebust 	struct nfs_cache_array *array;
179d1bacf9eSBryan Schumaker 	int i;
1808cd51a0cSTrond Myklebust 
1812b86ce2dSCong Wang 	array = kmap_atomic(page);
182d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++)
183d1bacf9eSBryan Schumaker 		kfree(array->array[i].string.name);
1842b86ce2dSCong Wang 	kunmap_atomic(array);
185d1bacf9eSBryan Schumaker }
186d1bacf9eSBryan Schumaker 
187d1bacf9eSBryan Schumaker /*
188d1bacf9eSBryan Schumaker  * the caller is responsible for freeing qstr.name
189d1bacf9eSBryan Schumaker  * when called by nfs_readdir_add_to_array, the strings will be freed in
190d1bacf9eSBryan Schumaker  * nfs_clear_readdir_array()
191d1bacf9eSBryan Schumaker  */
192d1bacf9eSBryan Schumaker static
1934a201d6eSTrond Myklebust int nfs_readdir_make_qstr(struct qstr *string, const char *name, unsigned int len)
194d1bacf9eSBryan Schumaker {
195d1bacf9eSBryan Schumaker 	string->len = len;
196d1bacf9eSBryan Schumaker 	string->name = kmemdup(name, len, GFP_KERNEL);
1974a201d6eSTrond Myklebust 	if (string->name == NULL)
1984a201d6eSTrond Myklebust 		return -ENOMEM;
19904e4bd1cSCatalin Marinas 	/*
20004e4bd1cSCatalin Marinas 	 * Avoid a kmemleak false positive. The pointer to the name is stored
20104e4bd1cSCatalin Marinas 	 * in a page cache page which kmemleak does not scan.
20204e4bd1cSCatalin Marinas 	 */
20304e4bd1cSCatalin Marinas 	kmemleak_not_leak(string->name);
2048387ff25SLinus Torvalds 	string->hash = full_name_hash(NULL, name, len);
2054a201d6eSTrond Myklebust 	return 0;
206d1bacf9eSBryan Schumaker }
207d1bacf9eSBryan Schumaker 
208d1bacf9eSBryan Schumaker static
209d1bacf9eSBryan Schumaker int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page)
210d1bacf9eSBryan Schumaker {
2110795bf83SFabian Frederick 	struct nfs_cache_array *array = kmap(page);
2124a201d6eSTrond Myklebust 	struct nfs_cache_array_entry *cache_entry;
2134a201d6eSTrond Myklebust 	int ret;
2144a201d6eSTrond Myklebust 
2154a201d6eSTrond Myklebust 	cache_entry = &array->array[array->size];
2163020093fSTrond Myklebust 
2173020093fSTrond Myklebust 	/* Check that this entry lies within the page bounds */
2183020093fSTrond Myklebust 	ret = -ENOSPC;
2193020093fSTrond Myklebust 	if ((char *)&cache_entry[1] - (char *)page_address(page) > PAGE_SIZE)
2203020093fSTrond Myklebust 		goto out;
2213020093fSTrond Myklebust 
2224a201d6eSTrond Myklebust 	cache_entry->cookie = entry->prev_cookie;
2234a201d6eSTrond Myklebust 	cache_entry->ino = entry->ino;
2240b26a0bfSTrond Myklebust 	cache_entry->d_type = entry->d_type;
2254a201d6eSTrond Myklebust 	ret = nfs_readdir_make_qstr(&cache_entry->string, entry->name, entry->len);
2264a201d6eSTrond Myklebust 	if (ret)
2274a201d6eSTrond Myklebust 		goto out;
228d1bacf9eSBryan Schumaker 	array->last_cookie = entry->cookie;
2298cd51a0cSTrond Myklebust 	array->size++;
23047c716cbSTrond Myklebust 	if (entry->eof != 0)
231d1bacf9eSBryan Schumaker 		array->eof_index = array->size;
2324a201d6eSTrond Myklebust out:
2330795bf83SFabian Frederick 	kunmap(page);
2344a201d6eSTrond Myklebust 	return ret;
235d1bacf9eSBryan Schumaker }
236d1bacf9eSBryan Schumaker 
237d1bacf9eSBryan Schumaker static
238d1bacf9eSBryan Schumaker int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
239d1bacf9eSBryan Schumaker {
24023db8620SAl Viro 	loff_t diff = desc->ctx->pos - desc->current_index;
241d1bacf9eSBryan Schumaker 	unsigned int index;
242d1bacf9eSBryan Schumaker 
243d1bacf9eSBryan Schumaker 	if (diff < 0)
244d1bacf9eSBryan Schumaker 		goto out_eof;
245d1bacf9eSBryan Schumaker 	if (diff >= array->size) {
2468cd51a0cSTrond Myklebust 		if (array->eof_index >= 0)
247d1bacf9eSBryan Schumaker 			goto out_eof;
248d1bacf9eSBryan Schumaker 		return -EAGAIN;
249d1bacf9eSBryan Schumaker 	}
250d1bacf9eSBryan Schumaker 
251d1bacf9eSBryan Schumaker 	index = (unsigned int)diff;
252d1bacf9eSBryan Schumaker 	*desc->dir_cookie = array->array[index].cookie;
253d1bacf9eSBryan Schumaker 	desc->cache_entry_index = index;
254d1bacf9eSBryan Schumaker 	return 0;
255d1bacf9eSBryan Schumaker out_eof:
2566089dd0dSThomas Meyer 	desc->eof = true;
257d1bacf9eSBryan Schumaker 	return -EBADCOOKIE;
258d1bacf9eSBryan Schumaker }
259d1bacf9eSBryan Schumaker 
2604db72b40SJeff Layton static bool
2614db72b40SJeff Layton nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi)
2624db72b40SJeff Layton {
2634db72b40SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA))
2644db72b40SJeff Layton 		return false;
2654db72b40SJeff Layton 	smp_rmb();
2664db72b40SJeff Layton 	return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags);
2674db72b40SJeff Layton }
2684db72b40SJeff Layton 
269d1bacf9eSBryan Schumaker static
270d1bacf9eSBryan Schumaker int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_descriptor_t *desc)
271d1bacf9eSBryan Schumaker {
272d1bacf9eSBryan Schumaker 	int i;
2738ef2ce3eSBryan Schumaker 	loff_t new_pos;
274d1bacf9eSBryan Schumaker 	int status = -EAGAIN;
275d1bacf9eSBryan Schumaker 
276d1bacf9eSBryan Schumaker 	for (i = 0; i < array->size; i++) {
2778cd51a0cSTrond Myklebust 		if (array->array[i].cookie == *desc->dir_cookie) {
278496ad9aaSAl Viro 			struct nfs_inode *nfsi = NFS_I(file_inode(desc->file));
2790c030806STrond Myklebust 			struct nfs_open_dir_context *ctx = desc->file->private_data;
2800c030806STrond Myklebust 
2818ef2ce3eSBryan Schumaker 			new_pos = desc->current_index + i;
2824db72b40SJeff Layton 			if (ctx->attr_gencount != nfsi->attr_gencount ||
2834db72b40SJeff Layton 			    !nfs_readdir_inode_mapping_valid(nfsi)) {
2840c030806STrond Myklebust 				ctx->duped = 0;
2850c030806STrond Myklebust 				ctx->attr_gencount = nfsi->attr_gencount;
28623db8620SAl Viro 			} else if (new_pos < desc->ctx->pos) {
2870c030806STrond Myklebust 				if (ctx->duped > 0
2880c030806STrond Myklebust 				    && ctx->dup_cookie == *desc->dir_cookie) {
2890c030806STrond Myklebust 					if (printk_ratelimit()) {
2906de1472fSAl Viro 						pr_notice("NFS: directory %pD2 contains a readdir loop."
2910c030806STrond Myklebust 								"Please contact your server vendor.  "
2929581a4aeSJeff Layton 								"The file: %.*s has duplicate cookie %llu\n",
2939581a4aeSJeff Layton 								desc->file, array->array[i].string.len,
2949581a4aeSJeff Layton 								array->array[i].string.name, *desc->dir_cookie);
2950c030806STrond Myklebust 					}
2960c030806STrond Myklebust 					status = -ELOOP;
2970c030806STrond Myklebust 					goto out;
2980c030806STrond Myklebust 				}
2998ef2ce3eSBryan Schumaker 				ctx->dup_cookie = *desc->dir_cookie;
3000c030806STrond Myklebust 				ctx->duped = -1;
3018ef2ce3eSBryan Schumaker 			}
30223db8620SAl Viro 			desc->ctx->pos = new_pos;
3038cd51a0cSTrond Myklebust 			desc->cache_entry_index = i;
30447c716cbSTrond Myklebust 			return 0;
3058cd51a0cSTrond Myklebust 		}
3068cd51a0cSTrond Myklebust 	}
30747c716cbSTrond Myklebust 	if (array->eof_index >= 0) {
308d1bacf9eSBryan Schumaker 		status = -EBADCOOKIE;
30918fb5fe4STrond Myklebust 		if (*desc->dir_cookie == array->last_cookie)
3106089dd0dSThomas Meyer 			desc->eof = true;
311d1bacf9eSBryan Schumaker 	}
3120c030806STrond Myklebust out:
313d1bacf9eSBryan Schumaker 	return status;
314d1bacf9eSBryan Schumaker }
315d1bacf9eSBryan Schumaker 
316d1bacf9eSBryan Schumaker static
317d1bacf9eSBryan Schumaker int nfs_readdir_search_array(nfs_readdir_descriptor_t *desc)
318d1bacf9eSBryan Schumaker {
319d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
32047c716cbSTrond Myklebust 	int status;
321d1bacf9eSBryan Schumaker 
3220795bf83SFabian Frederick 	array = kmap(desc->page);
323d1bacf9eSBryan Schumaker 
324d1bacf9eSBryan Schumaker 	if (*desc->dir_cookie == 0)
325d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_pos(array, desc);
326d1bacf9eSBryan Schumaker 	else
327d1bacf9eSBryan Schumaker 		status = nfs_readdir_search_for_cookie(array, desc);
328d1bacf9eSBryan Schumaker 
32947c716cbSTrond Myklebust 	if (status == -EAGAIN) {
3300aded708STrond Myklebust 		desc->last_cookie = array->last_cookie;
331e47c085aSTrond Myklebust 		desc->current_index += array->size;
33247c716cbSTrond Myklebust 		desc->page_index++;
33347c716cbSTrond Myklebust 	}
3340795bf83SFabian Frederick 	kunmap(desc->page);
335d1bacf9eSBryan Schumaker 	return status;
336d1bacf9eSBryan Schumaker }
337d1bacf9eSBryan Schumaker 
338d1bacf9eSBryan Schumaker /* Fill a page with xdr information before transferring to the cache page */
339d1bacf9eSBryan Schumaker static
34056e4ebf8SBryan Schumaker int nfs_readdir_xdr_filler(struct page **pages, nfs_readdir_descriptor_t *desc,
341d1bacf9eSBryan Schumaker 			struct nfs_entry *entry, struct file *file, struct inode *inode)
342d1bacf9eSBryan Schumaker {
343480c2006SBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
344684f39b4SNeilBrown 	const struct cred *cred = ctx->cred;
3454704f0e2STrond Myklebust 	unsigned long	timestamp, gencount;
3461da177e4SLinus Torvalds 	int		error;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds  again:
3491da177e4SLinus Torvalds 	timestamp = jiffies;
3504704f0e2STrond Myklebust 	gencount = nfs_inc_attr_generation_counter();
351be62a1a8SMiklos Szeredi 	error = NFS_PROTO(inode)->readdir(file_dentry(file), cred, entry->cookie, pages,
3521da177e4SLinus Torvalds 					  NFS_SERVER(inode)->dtsize, desc->plus);
3531da177e4SLinus Torvalds 	if (error < 0) {
3541da177e4SLinus Torvalds 		/* We requested READDIRPLUS, but the server doesn't grok it */
3551da177e4SLinus Torvalds 		if (error == -ENOTSUPP && desc->plus) {
3561da177e4SLinus Torvalds 			NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
3573a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
358a7a3b1e9SBenjamin Coddington 			desc->plus = false;
3591da177e4SLinus Torvalds 			goto again;
3601da177e4SLinus Torvalds 		}
3611da177e4SLinus Torvalds 		goto error;
3621da177e4SLinus Torvalds 	}
3631f4eab7eSNeil Brown 	desc->timestamp = timestamp;
3644704f0e2STrond Myklebust 	desc->gencount = gencount;
365d1bacf9eSBryan Schumaker error:
366d1bacf9eSBryan Schumaker 	return error;
367d1bacf9eSBryan Schumaker }
368d1bacf9eSBryan Schumaker 
369573c4e1eSChuck Lever static int xdr_decode(nfs_readdir_descriptor_t *desc,
370573c4e1eSChuck Lever 		      struct nfs_entry *entry, struct xdr_stream *xdr)
371d1bacf9eSBryan Schumaker {
372573c4e1eSChuck Lever 	int error;
373d1bacf9eSBryan Schumaker 
374573c4e1eSChuck Lever 	error = desc->decode(xdr, entry, desc->plus);
375573c4e1eSChuck Lever 	if (error)
376573c4e1eSChuck Lever 		return error;
377d1bacf9eSBryan Schumaker 	entry->fattr->time_start = desc->timestamp;
378d1bacf9eSBryan Schumaker 	entry->fattr->gencount = desc->gencount;
379d1bacf9eSBryan Schumaker 	return 0;
380d1bacf9eSBryan Schumaker }
381d1bacf9eSBryan Schumaker 
382fa923369STrond Myklebust /* Match file and dirent using either filehandle or fileid
383fa923369STrond Myklebust  * Note: caller is responsible for checking the fsid
384fa923369STrond Myklebust  */
385d39ab9deSBryan Schumaker static
386d39ab9deSBryan Schumaker int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry)
387d39ab9deSBryan Schumaker {
388d8fdb47fSTrond Myklebust 	struct inode *inode;
389fa923369STrond Myklebust 	struct nfs_inode *nfsi;
390fa923369STrond Myklebust 
3912b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
3922b0143b5SDavid Howells 		return 0;
393fa923369STrond Myklebust 
394d8fdb47fSTrond Myklebust 	inode = d_inode(dentry);
395d8fdb47fSTrond Myklebust 	if (is_bad_inode(inode) || NFS_STALE(inode))
396d8fdb47fSTrond Myklebust 		return 0;
397d8fdb47fSTrond Myklebust 
398d8fdb47fSTrond Myklebust 	nfsi = NFS_I(inode);
3997dc72d5fSTrond Myklebust 	if (entry->fattr->fileid != nfsi->fileid)
400d39ab9deSBryan Schumaker 		return 0;
4017dc72d5fSTrond Myklebust 	if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0)
4027dc72d5fSTrond Myklebust 		return 0;
4037dc72d5fSTrond Myklebust 	return 1;
404d39ab9deSBryan Schumaker }
405d39ab9deSBryan Schumaker 
406d39ab9deSBryan Schumaker static
40723db8620SAl Viro bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx)
408d69ee9b8STrond Myklebust {
409d69ee9b8STrond Myklebust 	if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS))
410d69ee9b8STrond Myklebust 		return false;
411d69ee9b8STrond Myklebust 	if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags))
412d69ee9b8STrond Myklebust 		return true;
41323db8620SAl Viro 	if (ctx->pos == 0)
414d69ee9b8STrond Myklebust 		return true;
415d69ee9b8STrond Myklebust 	return false;
416d69ee9b8STrond Myklebust }
417d69ee9b8STrond Myklebust 
418d69ee9b8STrond Myklebust /*
41963519fbcSTrond Myklebust  * This function is called by the lookup and getattr code to request the
42063519fbcSTrond Myklebust  * use of readdirplus to accelerate any future lookups in the same
421d69ee9b8STrond Myklebust  * directory.
422d69ee9b8STrond Myklebust  */
423d69ee9b8STrond Myklebust void nfs_advise_use_readdirplus(struct inode *dir)
424d69ee9b8STrond Myklebust {
42563519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
42663519fbcSTrond Myklebust 
42763519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
42863519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files))
42963519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
430d69ee9b8STrond Myklebust }
431d69ee9b8STrond Myklebust 
432311324adSTrond Myklebust /*
433311324adSTrond Myklebust  * This function is mainly for use by nfs_getattr().
434311324adSTrond Myklebust  *
435311324adSTrond Myklebust  * If this is an 'ls -l', we want to force use of readdirplus.
436311324adSTrond Myklebust  * Do this by checking if there is an active file descriptor
437311324adSTrond Myklebust  * and calling nfs_advise_use_readdirplus, then forcing a
438311324adSTrond Myklebust  * cache flush.
439311324adSTrond Myklebust  */
440311324adSTrond Myklebust void nfs_force_use_readdirplus(struct inode *dir)
441311324adSTrond Myklebust {
44263519fbcSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(dir);
44363519fbcSTrond Myklebust 
44463519fbcSTrond Myklebust 	if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) &&
44563519fbcSTrond Myklebust 	    !list_empty(&nfsi->open_files)) {
44663519fbcSTrond Myklebust 		set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags);
44779f687a3STrond Myklebust 		invalidate_mapping_pages(dir->i_mapping, 0, -1);
448311324adSTrond Myklebust 	}
449311324adSTrond Myklebust }
450311324adSTrond Myklebust 
451d69ee9b8STrond Myklebust static
452d39ab9deSBryan Schumaker void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry)
453d39ab9deSBryan Schumaker {
45426fe5750SLinus Torvalds 	struct qstr filename = QSTR_INIT(entry->name, entry->len);
4559ac3d3e8SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
4564a201d6eSTrond Myklebust 	struct dentry *dentry;
4574a201d6eSTrond Myklebust 	struct dentry *alias;
4582b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
459d39ab9deSBryan Schumaker 	struct inode *inode;
460aa9c2669SDavid Quigley 	int status;
461d39ab9deSBryan Schumaker 
462fa923369STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID))
463fa923369STrond Myklebust 		return;
4646c441c25STrond Myklebust 	if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID))
4656c441c25STrond Myklebust 		return;
46678d04af4STrond Myklebust 	if (filename.len == 0)
46778d04af4STrond Myklebust 		return;
46878d04af4STrond Myklebust 	/* Validate that the name doesn't contain any illegal '\0' */
46978d04af4STrond Myklebust 	if (strnlen(filename.name, filename.len) != filename.len)
47078d04af4STrond Myklebust 		return;
47178d04af4STrond Myklebust 	/* ...or '/' */
47278d04af4STrond Myklebust 	if (strnchr(filename.name, filename.len, '/'))
47378d04af4STrond Myklebust 		return;
4744a201d6eSTrond Myklebust 	if (filename.name[0] == '.') {
4754a201d6eSTrond Myklebust 		if (filename.len == 1)
4764a201d6eSTrond Myklebust 			return;
4774a201d6eSTrond Myklebust 		if (filename.len == 2 && filename.name[1] == '.')
4784a201d6eSTrond Myklebust 			return;
4794a201d6eSTrond Myklebust 	}
4808387ff25SLinus Torvalds 	filename.hash = full_name_hash(parent, filename.name, filename.len);
481d39ab9deSBryan Schumaker 
4824a201d6eSTrond Myklebust 	dentry = d_lookup(parent, &filename);
4839ac3d3e8SAl Viro again:
4849ac3d3e8SAl Viro 	if (!dentry) {
4859ac3d3e8SAl Viro 		dentry = d_alloc_parallel(parent, &filename, &wq);
4869ac3d3e8SAl Viro 		if (IS_ERR(dentry))
4879ac3d3e8SAl Viro 			return;
4889ac3d3e8SAl Viro 	}
4899ac3d3e8SAl Viro 	if (!d_in_lookup(dentry)) {
4906c441c25STrond Myklebust 		/* Is there a mountpoint here? If so, just exit */
4916c441c25STrond Myklebust 		if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid,
4926c441c25STrond Myklebust 					&entry->fattr->fsid))
4936c441c25STrond Myklebust 			goto out;
494d39ab9deSBryan Schumaker 		if (nfs_same_file(dentry, entry)) {
4957dc72d5fSTrond Myklebust 			if (!entry->fh->size)
4967dc72d5fSTrond Myklebust 				goto out;
497cda57a1eSJeff Layton 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
4982b0143b5SDavid Howells 			status = nfs_refresh_inode(d_inode(dentry), entry->fattr);
499aa9c2669SDavid Quigley 			if (!status)
5002b0143b5SDavid Howells 				nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label);
501d39ab9deSBryan Schumaker 			goto out;
502d39ab9deSBryan Schumaker 		} else {
5035542aa2fSEric W. Biederman 			d_invalidate(dentry);
504d39ab9deSBryan Schumaker 			dput(dentry);
5059ac3d3e8SAl Viro 			dentry = NULL;
5069ac3d3e8SAl Viro 			goto again;
507d39ab9deSBryan Schumaker 		}
508d39ab9deSBryan Schumaker 	}
5097dc72d5fSTrond Myklebust 	if (!entry->fh->size) {
5107dc72d5fSTrond Myklebust 		d_lookup_done(dentry);
5117dc72d5fSTrond Myklebust 		goto out;
5127dc72d5fSTrond Myklebust 	}
513d39ab9deSBryan Schumaker 
5141775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label);
51541d28bcaSAl Viro 	alias = d_splice_alias(inode, dentry);
5169ac3d3e8SAl Viro 	d_lookup_done(dentry);
5179ac3d3e8SAl Viro 	if (alias) {
518d39ab9deSBryan Schumaker 		if (IS_ERR(alias))
519d39ab9deSBryan Schumaker 			goto out;
5209ac3d3e8SAl Viro 		dput(dentry);
5219ac3d3e8SAl Viro 		dentry = alias;
5229ac3d3e8SAl Viro 	}
523d39ab9deSBryan Schumaker 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
524d39ab9deSBryan Schumaker out:
525d39ab9deSBryan Schumaker 	dput(dentry);
526d39ab9deSBryan Schumaker }
527d39ab9deSBryan Schumaker 
528d1bacf9eSBryan Schumaker /* Perform conversion from xdr to cache array */
529d1bacf9eSBryan Schumaker static
5308cd51a0cSTrond Myklebust int nfs_readdir_page_filler(nfs_readdir_descriptor_t *desc, struct nfs_entry *entry,
5316650239aSTrond Myklebust 				struct page **xdr_pages, struct page *page, unsigned int buflen)
532d1bacf9eSBryan Schumaker {
533babddc72SBryan Schumaker 	struct xdr_stream stream;
534f7da7a12SBenny Halevy 	struct xdr_buf buf;
5356650239aSTrond Myklebust 	struct page *scratch;
53699424380SBryan Schumaker 	struct nfs_cache_array *array;
5375c346854STrond Myklebust 	unsigned int count = 0;
5385c346854STrond Myklebust 	int status;
539be4c2d47Sluanshi 	int max_rapages = NFS_MAX_READDIR_RAPAGES;
540be4c2d47Sluanshi 
541be4c2d47Sluanshi 	desc->pvec.index = desc->page_index;
542be4c2d47Sluanshi 	desc->pvec.nr = 0;
543babddc72SBryan Schumaker 
5446650239aSTrond Myklebust 	scratch = alloc_page(GFP_KERNEL);
5456650239aSTrond Myklebust 	if (scratch == NULL)
5466650239aSTrond Myklebust 		return -ENOMEM;
547babddc72SBryan Schumaker 
548ce85cfbeSBenjamin Coddington 	if (buflen == 0)
549ce85cfbeSBenjamin Coddington 		goto out_nopages;
550ce85cfbeSBenjamin Coddington 
551f7da7a12SBenny Halevy 	xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen);
5526650239aSTrond Myklebust 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
55399424380SBryan Schumaker 
55499424380SBryan Schumaker 	do {
55599424380SBryan Schumaker 		status = xdr_decode(desc, entry, &stream);
5568cd51a0cSTrond Myklebust 		if (status != 0) {
5578cd51a0cSTrond Myklebust 			if (status == -EAGAIN)
5588cd51a0cSTrond Myklebust 				status = 0;
55999424380SBryan Schumaker 			break;
5608cd51a0cSTrond Myklebust 		}
56199424380SBryan Schumaker 
5625c346854STrond Myklebust 		count++;
5635c346854STrond Myklebust 
564a7a3b1e9SBenjamin Coddington 		if (desc->plus)
565be62a1a8SMiklos Szeredi 			nfs_prime_dcache(file_dentry(desc->file), entry);
5668cd51a0cSTrond Myklebust 
567be4c2d47Sluanshi 		status = nfs_readdir_add_to_array(entry, desc->pvec.pages[desc->pvec.nr]);
568be4c2d47Sluanshi 		if (status == -ENOSPC) {
569be4c2d47Sluanshi 			desc->pvec.nr++;
570be4c2d47Sluanshi 			if (desc->pvec.nr == max_rapages)
571be4c2d47Sluanshi 				break;
572be4c2d47Sluanshi 			status = nfs_readdir_add_to_array(entry, desc->pvec.pages[desc->pvec.nr]);
573be4c2d47Sluanshi 		}
5748cd51a0cSTrond Myklebust 		if (status != 0)
5758cd51a0cSTrond Myklebust 			break;
57699424380SBryan Schumaker 	} while (!entry->eof);
57799424380SBryan Schumaker 
578be4c2d47Sluanshi 	/*
579be4c2d47Sluanshi 	 * page and desc->pvec.pages[0] are valid, don't need to check
580be4c2d47Sluanshi 	 * whether or not to be NULL.
581be4c2d47Sluanshi 	 */
582be4c2d47Sluanshi 	copy_highpage(page, desc->pvec.pages[0]);
583be4c2d47Sluanshi 
584ce85cfbeSBenjamin Coddington out_nopages:
58547c716cbSTrond Myklebust 	if (count == 0 || (status == -EBADCOOKIE && entry->eof != 0)) {
586be4c2d47Sluanshi 		array = kmap_atomic(desc->pvec.pages[desc->pvec.nr]);
5878cd51a0cSTrond Myklebust 		array->eof_index = array->size;
58899424380SBryan Schumaker 		status = 0;
589be4c2d47Sluanshi 		kunmap_atomic(array);
59056e4ebf8SBryan Schumaker 	}
5916650239aSTrond Myklebust 
5926650239aSTrond Myklebust 	put_page(scratch);
593be4c2d47Sluanshi 
594be4c2d47Sluanshi 	/*
595be4c2d47Sluanshi 	 * desc->pvec.nr > 0 means at least one page was completely filled,
596be4c2d47Sluanshi 	 * we should return -ENOSPC. Otherwise function
597be4c2d47Sluanshi 	 * nfs_readdir_xdr_to_array will enter infinite loop.
598be4c2d47Sluanshi 	 */
599be4c2d47Sluanshi 	if (desc->pvec.nr > 0)
600be4c2d47Sluanshi 		return -ENOSPC;
6018cd51a0cSTrond Myklebust 	return status;
6028cd51a0cSTrond Myklebust }
60356e4ebf8SBryan Schumaker 
60456e4ebf8SBryan Schumaker static
605c7e9668eSAnna Schumaker void nfs_readdir_free_pages(struct page **pages, unsigned int npages)
60656e4ebf8SBryan Schumaker {
60756e4ebf8SBryan Schumaker 	unsigned int i;
60856e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++)
60956e4ebf8SBryan Schumaker 		put_page(pages[i]);
61056e4ebf8SBryan Schumaker }
61156e4ebf8SBryan Schumaker 
61256e4ebf8SBryan Schumaker /*
613bf211ca1Szhangliguang  * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call
614bf211ca1Szhangliguang  * to nfs_readdir_free_pages()
61556e4ebf8SBryan Schumaker  */
61656e4ebf8SBryan Schumaker static
617c7e9668eSAnna Schumaker int nfs_readdir_alloc_pages(struct page **pages, unsigned int npages)
61856e4ebf8SBryan Schumaker {
61956e4ebf8SBryan Schumaker 	unsigned int i;
62056e4ebf8SBryan Schumaker 
62156e4ebf8SBryan Schumaker 	for (i = 0; i < npages; i++) {
62256e4ebf8SBryan Schumaker 		struct page *page = alloc_page(GFP_KERNEL);
62356e4ebf8SBryan Schumaker 		if (page == NULL)
62456e4ebf8SBryan Schumaker 			goto out_freepages;
62556e4ebf8SBryan Schumaker 		pages[i] = page;
62656e4ebf8SBryan Schumaker 	}
6276650239aSTrond Myklebust 	return 0;
62856e4ebf8SBryan Schumaker 
62956e4ebf8SBryan Schumaker out_freepages:
630c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, i);
6316650239aSTrond Myklebust 	return -ENOMEM;
632d1bacf9eSBryan Schumaker }
633d1bacf9eSBryan Schumaker 
634be4c2d47Sluanshi /*
635be4c2d47Sluanshi  * nfs_readdir_rapages_init initialize rapages by nfs_cache_array structure.
636be4c2d47Sluanshi  */
637be4c2d47Sluanshi static
638be4c2d47Sluanshi void nfs_readdir_rapages_init(nfs_readdir_descriptor_t *desc)
639be4c2d47Sluanshi {
640be4c2d47Sluanshi 	struct nfs_cache_array *array;
641be4c2d47Sluanshi 	int max_rapages = NFS_MAX_READDIR_RAPAGES;
642be4c2d47Sluanshi 	int index;
643be4c2d47Sluanshi 
644be4c2d47Sluanshi 	for (index = 0; index < max_rapages; index++) {
645be4c2d47Sluanshi 		array = kmap_atomic(desc->pvec.pages[index]);
646be4c2d47Sluanshi 		memset(array, 0, sizeof(struct nfs_cache_array));
647be4c2d47Sluanshi 		array->eof_index = -1;
648be4c2d47Sluanshi 		kunmap_atomic(array);
649be4c2d47Sluanshi 	}
650be4c2d47Sluanshi }
651be4c2d47Sluanshi 
652d1bacf9eSBryan Schumaker static
653d1bacf9eSBryan Schumaker int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page, struct inode *inode)
654d1bacf9eSBryan Schumaker {
65556e4ebf8SBryan Schumaker 	struct page *pages[NFS_MAX_READDIR_PAGES];
656d1bacf9eSBryan Schumaker 	struct nfs_entry entry;
657d1bacf9eSBryan Schumaker 	struct file	*file = desc->file;
658d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array;
6598cd51a0cSTrond Myklebust 	int status = -ENOMEM;
66056e4ebf8SBryan Schumaker 	unsigned int array_size = ARRAY_SIZE(pages);
661d1bacf9eSBryan Schumaker 
662be4c2d47Sluanshi 	/*
663be4c2d47Sluanshi 	 * This means we hit readdir rdpages miss, the preallocated rdpages
664be4c2d47Sluanshi 	 * are useless, the preallocate rdpages should be reinitialized.
665be4c2d47Sluanshi 	 */
666be4c2d47Sluanshi 	nfs_readdir_rapages_init(desc);
667be4c2d47Sluanshi 
668d1bacf9eSBryan Schumaker 	entry.prev_cookie = 0;
6690aded708STrond Myklebust 	entry.cookie = desc->last_cookie;
670d1bacf9eSBryan Schumaker 	entry.eof = 0;
671d1bacf9eSBryan Schumaker 	entry.fh = nfs_alloc_fhandle();
672d1bacf9eSBryan Schumaker 	entry.fattr = nfs_alloc_fattr();
673573c4e1eSChuck Lever 	entry.server = NFS_SERVER(inode);
674d1bacf9eSBryan Schumaker 	if (entry.fh == NULL || entry.fattr == NULL)
675d1bacf9eSBryan Schumaker 		goto out;
676d1bacf9eSBryan Schumaker 
67714c43f76SDavid Quigley 	entry.label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT);
67814c43f76SDavid Quigley 	if (IS_ERR(entry.label)) {
67914c43f76SDavid Quigley 		status = PTR_ERR(entry.label);
68014c43f76SDavid Quigley 		goto out;
68114c43f76SDavid Quigley 	}
68214c43f76SDavid Quigley 
6830795bf83SFabian Frederick 	array = kmap(page);
684d1bacf9eSBryan Schumaker 	memset(array, 0, sizeof(struct nfs_cache_array));
685d1bacf9eSBryan Schumaker 	array->eof_index = -1;
686d1bacf9eSBryan Schumaker 
687c7e9668eSAnna Schumaker 	status = nfs_readdir_alloc_pages(pages, array_size);
6886650239aSTrond Myklebust 	if (status < 0)
689d1bacf9eSBryan Schumaker 		goto out_release_array;
690d1bacf9eSBryan Schumaker 	do {
691ac396128STrond Myklebust 		unsigned int pglen;
69256e4ebf8SBryan Schumaker 		status = nfs_readdir_xdr_filler(pages, desc, &entry, file, inode);
693babddc72SBryan Schumaker 
694d1bacf9eSBryan Schumaker 		if (status < 0)
695d1bacf9eSBryan Schumaker 			break;
696ac396128STrond Myklebust 		pglen = status;
6976650239aSTrond Myklebust 		status = nfs_readdir_page_filler(desc, &entry, pages, page, pglen);
6988cd51a0cSTrond Myklebust 		if (status < 0) {
6998cd51a0cSTrond Myklebust 			if (status == -ENOSPC)
7008cd51a0cSTrond Myklebust 				status = 0;
7018cd51a0cSTrond Myklebust 			break;
7028cd51a0cSTrond Myklebust 		}
7038cd51a0cSTrond Myklebust 	} while (array->eof_index < 0);
704d1bacf9eSBryan Schumaker 
705c7e9668eSAnna Schumaker 	nfs_readdir_free_pages(pages, array_size);
706d1bacf9eSBryan Schumaker out_release_array:
7070795bf83SFabian Frederick 	kunmap(page);
70814c43f76SDavid Quigley 	nfs4_label_free(entry.label);
709d1bacf9eSBryan Schumaker out:
710d1bacf9eSBryan Schumaker 	nfs_free_fattr(entry.fattr);
711d1bacf9eSBryan Schumaker 	nfs_free_fhandle(entry.fh);
712d1bacf9eSBryan Schumaker 	return status;
713d1bacf9eSBryan Schumaker }
714d1bacf9eSBryan Schumaker 
715d1bacf9eSBryan Schumaker /*
716d1bacf9eSBryan Schumaker  * Now we cache directories properly, by converting xdr information
717d1bacf9eSBryan Schumaker  * to an array that can be used for lookups later.  This results in
718d1bacf9eSBryan Schumaker  * fewer cache pages, since we can store more information on each page.
719d1bacf9eSBryan Schumaker  * We only need to convert from xdr once so future lookups are much simpler
7201da177e4SLinus Torvalds  */
721d1bacf9eSBryan Schumaker static
722a46126ccSChristoph Hellwig int nfs_readdir_filler(void *data, struct page* page)
723d1bacf9eSBryan Schumaker {
724a46126ccSChristoph Hellwig 	nfs_readdir_descriptor_t *desc = data;
725496ad9aaSAl Viro 	struct inode	*inode = file_inode(desc->file);
7268cd51a0cSTrond Myklebust 	int ret;
727d1bacf9eSBryan Schumaker 
728be4c2d47Sluanshi 	/*
729be4c2d47Sluanshi 	 * If desc->page_index in range desc->pvec.index and
730be4c2d47Sluanshi 	 * desc->pvec.index + desc->pvec.nr, we get readdir cache hit.
731be4c2d47Sluanshi 	 */
732be4c2d47Sluanshi 	if (desc->page_index >= desc->pvec.index &&
733be4c2d47Sluanshi 		desc->page_index < (desc->pvec.index + desc->pvec.nr)) {
734be4c2d47Sluanshi 		/*
735be4c2d47Sluanshi 		 * page and desc->pvec.pages[x] are valid, don't need to check
736be4c2d47Sluanshi 		 * whether or not to be NULL.
737be4c2d47Sluanshi 		 */
738be4c2d47Sluanshi 		copy_highpage(page, desc->pvec.pages[desc->page_index - desc->pvec.index]);
739be4c2d47Sluanshi 		ret = 0;
740be4c2d47Sluanshi 	} else {
7418cd51a0cSTrond Myklebust 		ret = nfs_readdir_xdr_to_array(desc, page, inode);
7428cd51a0cSTrond Myklebust 		if (ret < 0)
743d1bacf9eSBryan Schumaker 			goto error;
744be4c2d47Sluanshi 	}
745be4c2d47Sluanshi 
746d1bacf9eSBryan Schumaker 	SetPageUptodate(page);
747d1bacf9eSBryan Schumaker 
7482aac05a9STrond Myklebust 	if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
749cd9ae2b6STrond Myklebust 		/* Should never happen */
750cd9ae2b6STrond Myklebust 		nfs_zap_mapping(inode, inode->i_mapping);
751cd9ae2b6STrond Myklebust 	}
7521da177e4SLinus Torvalds 	unlock_page(page);
7531da177e4SLinus Torvalds 	return 0;
7541da177e4SLinus Torvalds  error:
7551da177e4SLinus Torvalds 	unlock_page(page);
7568cd51a0cSTrond Myklebust 	return ret;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
759d1bacf9eSBryan Schumaker static
760d1bacf9eSBryan Schumaker void cache_page_release(nfs_readdir_descriptor_t *desc)
7611da177e4SLinus Torvalds {
762b044f645SBenjamin Coddington 	if (!desc->page->mapping)
76311de3b11STrond Myklebust 		nfs_readdir_clear_array(desc->page);
76409cbfeafSKirill A. Shutemov 	put_page(desc->page);
7651da177e4SLinus Torvalds 	desc->page = NULL;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
768d1bacf9eSBryan Schumaker static
769d1bacf9eSBryan Schumaker struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
7701da177e4SLinus Torvalds {
771a46126ccSChristoph Hellwig 	return read_cache_page(desc->file->f_mapping, desc->page_index,
772a46126ccSChristoph Hellwig 			nfs_readdir_filler, desc);
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds /*
776d1bacf9eSBryan Schumaker  * Returns 0 if desc->dir_cookie was found on page desc->page_index
7771da177e4SLinus Torvalds  */
778d1bacf9eSBryan Schumaker static
779d1bacf9eSBryan Schumaker int find_cache_page(nfs_readdir_descriptor_t *desc)
780d1bacf9eSBryan Schumaker {
781d1bacf9eSBryan Schumaker 	int res;
782d1bacf9eSBryan Schumaker 
783d1bacf9eSBryan Schumaker 	desc->page = get_cache_page(desc);
784d1bacf9eSBryan Schumaker 	if (IS_ERR(desc->page))
785d1bacf9eSBryan Schumaker 		return PTR_ERR(desc->page);
786d1bacf9eSBryan Schumaker 
787d1bacf9eSBryan Schumaker 	res = nfs_readdir_search_array(desc);
78847c716cbSTrond Myklebust 	if (res != 0)
789d1bacf9eSBryan Schumaker 		cache_page_release(desc);
790d1bacf9eSBryan Schumaker 	return res;
791d1bacf9eSBryan Schumaker }
792d1bacf9eSBryan Schumaker 
793d1bacf9eSBryan Schumaker /* Search for desc->dir_cookie from the beginning of the page cache */
7941da177e4SLinus Torvalds static inline
7951da177e4SLinus Torvalds int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
7961da177e4SLinus Torvalds {
7978cd51a0cSTrond Myklebust 	int res;
798d1bacf9eSBryan Schumaker 
7990aded708STrond Myklebust 	if (desc->page_index == 0) {
8008cd51a0cSTrond Myklebust 		desc->current_index = 0;
8010aded708STrond Myklebust 		desc->last_cookie = 0;
8020aded708STrond Myklebust 	}
80347c716cbSTrond Myklebust 	do {
804d1bacf9eSBryan Schumaker 		res = find_cache_page(desc);
80547c716cbSTrond Myklebust 	} while (res == -EAGAIN);
8061da177e4SLinus Torvalds 	return res;
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds /*
8101da177e4SLinus Torvalds  * Once we've found the start of the dirent within a page: fill 'er up...
8111da177e4SLinus Torvalds  */
8121da177e4SLinus Torvalds static
81323db8620SAl Viro int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
8141da177e4SLinus Torvalds {
8151da177e4SLinus Torvalds 	struct file	*file = desc->file;
816d1bacf9eSBryan Schumaker 	int i = 0;
817d1bacf9eSBryan Schumaker 	int res = 0;
818d1bacf9eSBryan Schumaker 	struct nfs_cache_array *array = NULL;
8198ef2ce3eSBryan Schumaker 	struct nfs_open_dir_context *ctx = file->private_data;
8208ef2ce3eSBryan Schumaker 
8210795bf83SFabian Frederick 	array = kmap(desc->page);
822d1bacf9eSBryan Schumaker 	for (i = desc->cache_entry_index; i < array->size; i++) {
823ece0b423STrond Myklebust 		struct nfs_cache_array_entry *ent;
8241da177e4SLinus Torvalds 
825ece0b423STrond Myklebust 		ent = &array->array[i];
82623db8620SAl Viro 		if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
82723db8620SAl Viro 		    nfs_compat_user_ino64(ent->ino), ent->d_type)) {
8286089dd0dSThomas Meyer 			desc->eof = true;
8291da177e4SLinus Torvalds 			break;
830ece0b423STrond Myklebust 		}
83123db8620SAl Viro 		desc->ctx->pos++;
832d1bacf9eSBryan Schumaker 		if (i < (array->size-1))
833d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->array[i+1].cookie;
834d1bacf9eSBryan Schumaker 		else
835d1bacf9eSBryan Schumaker 			*desc->dir_cookie = array->last_cookie;
8360c030806STrond Myklebust 		if (ctx->duped != 0)
8370c030806STrond Myklebust 			ctx->duped = 1;
8388cd51a0cSTrond Myklebust 	}
83947c716cbSTrond Myklebust 	if (array->eof_index >= 0)
8406089dd0dSThomas Meyer 		desc->eof = true;
841d1bacf9eSBryan Schumaker 
8420795bf83SFabian Frederick 	kunmap(desc->page);
843d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8441e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
8451e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie, res);
8461da177e4SLinus Torvalds 	return res;
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds /*
8501da177e4SLinus Torvalds  * If we cannot find a cookie in our cache, we suspect that this is
8511da177e4SLinus Torvalds  * because it points to a deleted file, so we ask the server to return
8521da177e4SLinus Torvalds  * whatever it thinks is the next entry. We then feed this to filldir.
8531da177e4SLinus Torvalds  * If all goes well, we should then be able to find our way round the
8541da177e4SLinus Torvalds  * cache on the next call to readdir_search_pagecache();
8551da177e4SLinus Torvalds  *
8561da177e4SLinus Torvalds  * NOTE: we cannot add the anonymous page to the pagecache because
8571da177e4SLinus Torvalds  *	 the data it contains might not be page aligned. Besides,
8581da177e4SLinus Torvalds  *	 we should already have a complete representation of the
8591da177e4SLinus Torvalds  *	 directory in the page cache by the time we get here.
8601da177e4SLinus Torvalds  */
8611da177e4SLinus Torvalds static inline
86223db8620SAl Viro int uncached_readdir(nfs_readdir_descriptor_t *desc)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	struct page	*page = NULL;
8651da177e4SLinus Torvalds 	int		status;
866496ad9aaSAl Viro 	struct inode *inode = file_inode(desc->file);
8670c030806STrond Myklebust 	struct nfs_open_dir_context *ctx = desc->file->private_data;
8681da177e4SLinus Torvalds 
8691e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
8701e7cb3dcSChuck Lever 			(unsigned long long)*desc->dir_cookie);
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	page = alloc_page(GFP_HIGHUSER);
8731da177e4SLinus Torvalds 	if (!page) {
8741da177e4SLinus Torvalds 		status = -ENOMEM;
8751da177e4SLinus Torvalds 		goto out;
8761da177e4SLinus Torvalds 	}
8771da177e4SLinus Torvalds 
8787a8e1dc3STrond Myklebust 	desc->page_index = 0;
8790aded708STrond Myklebust 	desc->last_cookie = *desc->dir_cookie;
8807a8e1dc3STrond Myklebust 	desc->page = page;
8810c030806STrond Myklebust 	ctx->duped = 0;
8827a8e1dc3STrond Myklebust 
88385f8607eSTrond Myklebust 	status = nfs_readdir_xdr_to_array(desc, page, inode);
88485f8607eSTrond Myklebust 	if (status < 0)
885d1bacf9eSBryan Schumaker 		goto out_release;
886d1bacf9eSBryan Schumaker 
88723db8620SAl Viro 	status = nfs_do_filldir(desc);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds  out:
8901e7cb3dcSChuck Lever 	dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
8913110ff80SHarvey Harrison 			__func__, status);
8921da177e4SLinus Torvalds 	return status;
8931da177e4SLinus Torvalds  out_release:
894d1bacf9eSBryan Schumaker 	cache_page_release(desc);
8951da177e4SLinus Torvalds 	goto out;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
89800a92642SOlivier Galibert /* The file offset position represents the dirent entry number.  A
89900a92642SOlivier Galibert    last cookie cache takes care of the common case of reading the
90000a92642SOlivier Galibert    whole directory.
9011da177e4SLinus Torvalds  */
90223db8620SAl Viro static int nfs_readdir(struct file *file, struct dir_context *ctx)
9031da177e4SLinus Torvalds {
904be62a1a8SMiklos Szeredi 	struct dentry	*dentry = file_dentry(file);
9052b0143b5SDavid Howells 	struct inode	*inode = d_inode(dentry);
9061da177e4SLinus Torvalds 	nfs_readdir_descriptor_t my_desc,
9071da177e4SLinus Torvalds 			*desc = &my_desc;
90823db8620SAl Viro 	struct nfs_open_dir_context *dir_ctx = file->private_data;
90907b5ce8eSScott Mayhew 	int res = 0;
910be4c2d47Sluanshi 	int max_rapages = NFS_MAX_READDIR_RAPAGES;
9111da177e4SLinus Torvalds 
9126de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n",
9136de1472fSAl Viro 			file, (long long)ctx->pos);
91491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
91591d5b470SChuck Lever 
9161da177e4SLinus Torvalds 	/*
91723db8620SAl Viro 	 * ctx->pos points to the dirent entry number.
918f0dd2136STrond Myklebust 	 * *desc->dir_cookie has the cookie for the next entry. We have
91900a92642SOlivier Galibert 	 * to either find the entry with the appropriate number or
92000a92642SOlivier Galibert 	 * revalidate the cookie.
9211da177e4SLinus Torvalds 	 */
9221da177e4SLinus Torvalds 	memset(desc, 0, sizeof(*desc));
9231da177e4SLinus Torvalds 
92423db8620SAl Viro 	desc->file = file;
92523db8620SAl Viro 	desc->ctx = ctx;
926480c2006SBryan Schumaker 	desc->dir_cookie = &dir_ctx->dir_cookie;
9271da177e4SLinus Torvalds 	desc->decode = NFS_PROTO(inode)->decode_dirent;
928a7a3b1e9SBenjamin Coddington 	desc->plus = nfs_use_readdirplus(inode, ctx);
9291da177e4SLinus Torvalds 
930be4c2d47Sluanshi 	res = nfs_readdir_alloc_pages(desc->pvec.pages, max_rapages);
931be4c2d47Sluanshi 	if (res < 0)
932be4c2d47Sluanshi 		return -ENOMEM;
933be4c2d47Sluanshi 
934be4c2d47Sluanshi 	nfs_readdir_rapages_init(desc);
935be4c2d47Sluanshi 
93679f687a3STrond Myklebust 	if (ctx->pos == 0 || nfs_attribute_cache_expired(inode))
93723db8620SAl Viro 		res = nfs_revalidate_mapping(inode, file->f_mapping);
938fccca7fcSTrond Myklebust 	if (res < 0)
939fccca7fcSTrond Myklebust 		goto out;
940fccca7fcSTrond Myklebust 
94147c716cbSTrond Myklebust 	do {
9421da177e4SLinus Torvalds 		res = readdir_search_pagecache(desc);
94300a92642SOlivier Galibert 
9441da177e4SLinus Torvalds 		if (res == -EBADCOOKIE) {
945ece0b423STrond Myklebust 			res = 0;
9461da177e4SLinus Torvalds 			/* This means either end of directory */
9476089dd0dSThomas Meyer 			if (*desc->dir_cookie && !desc->eof) {
9481da177e4SLinus Torvalds 				/* Or that the server has 'lost' a cookie */
94923db8620SAl Viro 				res = uncached_readdir(desc);
950ece0b423STrond Myklebust 				if (res == 0)
9511da177e4SLinus Torvalds 					continue;
9521da177e4SLinus Torvalds 			}
9531da177e4SLinus Torvalds 			break;
9541da177e4SLinus Torvalds 		}
9551da177e4SLinus Torvalds 		if (res == -ETOOSMALL && desc->plus) {
9563a10c30aSBenny Halevy 			clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
9571da177e4SLinus Torvalds 			nfs_zap_caches(inode);
958baf57a09STrond Myklebust 			desc->page_index = 0;
959a7a3b1e9SBenjamin Coddington 			desc->plus = false;
960a7a3b1e9SBenjamin Coddington 			desc->eof = false;
9611da177e4SLinus Torvalds 			continue;
9621da177e4SLinus Torvalds 		}
9631da177e4SLinus Torvalds 		if (res < 0)
9641da177e4SLinus Torvalds 			break;
9651da177e4SLinus Torvalds 
96623db8620SAl Viro 		res = nfs_do_filldir(desc);
967ece0b423STrond Myklebust 		if (res < 0)
9681da177e4SLinus Torvalds 			break;
96947c716cbSTrond Myklebust 	} while (!desc->eof);
970fccca7fcSTrond Myklebust out:
971be4c2d47Sluanshi 	nfs_readdir_free_pages(desc->pvec.pages, max_rapages);
9721e7cb3dcSChuck Lever 	if (res > 0)
9731e7cb3dcSChuck Lever 		res = 0;
9746de1472fSAl Viro 	dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res);
9751da177e4SLinus Torvalds 	return res;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
978965c8e59SAndrew Morton static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
979f0dd2136STrond Myklebust {
980b044f645SBenjamin Coddington 	struct inode *inode = file_inode(filp);
981480c2006SBryan Schumaker 	struct nfs_open_dir_context *dir_ctx = filp->private_data;
982b84e06c5SChuck Lever 
9836de1472fSAl Viro 	dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
9846de1472fSAl Viro 			filp, offset, whence);
985b84e06c5SChuck Lever 
986965c8e59SAndrew Morton 	switch (whence) {
987f0dd2136STrond Myklebust 	default:
988b2b1ff3dSTrond Myklebust 		return -EINVAL;
989b2b1ff3dSTrond Myklebust 	case SEEK_SET:
990b2b1ff3dSTrond Myklebust 		if (offset < 0)
991b2b1ff3dSTrond Myklebust 			return -EINVAL;
992b2b1ff3dSTrond Myklebust 		inode_lock(inode);
993b2b1ff3dSTrond Myklebust 		break;
994b2b1ff3dSTrond Myklebust 	case SEEK_CUR:
995b2b1ff3dSTrond Myklebust 		if (offset == 0)
996b2b1ff3dSTrond Myklebust 			return filp->f_pos;
997b2b1ff3dSTrond Myklebust 		inode_lock(inode);
998b2b1ff3dSTrond Myklebust 		offset += filp->f_pos;
999b2b1ff3dSTrond Myklebust 		if (offset < 0) {
1000b2b1ff3dSTrond Myklebust 			inode_unlock(inode);
1001b2b1ff3dSTrond Myklebust 			return -EINVAL;
1002b2b1ff3dSTrond Myklebust 		}
1003f0dd2136STrond Myklebust 	}
1004f0dd2136STrond Myklebust 	if (offset != filp->f_pos) {
1005f0dd2136STrond Myklebust 		filp->f_pos = offset;
1006480c2006SBryan Schumaker 		dir_ctx->dir_cookie = 0;
10078ef2ce3eSBryan Schumaker 		dir_ctx->duped = 0;
1008f0dd2136STrond Myklebust 	}
1009b044f645SBenjamin Coddington 	inode_unlock(inode);
1010f0dd2136STrond Myklebust 	return offset;
1011f0dd2136STrond Myklebust }
1012f0dd2136STrond Myklebust 
10131da177e4SLinus Torvalds /*
10141da177e4SLinus Torvalds  * All directory operations under NFS are synchronous, so fsync()
10151da177e4SLinus Torvalds  * is a dummy operation.
10161da177e4SLinus Torvalds  */
101702c24a82SJosef Bacik static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end,
101802c24a82SJosef Bacik 			 int datasync)
10191da177e4SLinus Torvalds {
10206de1472fSAl Viro 	struct inode *inode = file_inode(filp);
10217ea80859SChristoph Hellwig 
10226de1472fSAl Viro 	dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync);
10231e7cb3dcSChuck Lever 
10245955102cSAl Viro 	inode_lock(inode);
10256de1472fSAl Viro 	nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
10265955102cSAl Viro 	inode_unlock(inode);
10271da177e4SLinus Torvalds 	return 0;
10281da177e4SLinus Torvalds }
10291da177e4SLinus Torvalds 
1030bfc69a45STrond Myklebust /**
1031bfc69a45STrond Myklebust  * nfs_force_lookup_revalidate - Mark the directory as having changed
1032302fad7bSTrond Myklebust  * @dir: pointer to directory inode
1033bfc69a45STrond Myklebust  *
1034bfc69a45STrond Myklebust  * This forces the revalidation code in nfs_lookup_revalidate() to do a
1035bfc69a45STrond Myklebust  * full lookup on all child dentries of 'dir' whenever a change occurs
1036bfc69a45STrond Myklebust  * on the server that might have invalidated our dcache.
1037bfc69a45STrond Myklebust  *
1038bfc69a45STrond Myklebust  * The caller should be holding dir->i_lock
1039bfc69a45STrond Myklebust  */
1040bfc69a45STrond Myklebust void nfs_force_lookup_revalidate(struct inode *dir)
1041bfc69a45STrond Myklebust {
1042011935a0STrond Myklebust 	NFS_I(dir)->cache_change_attribute++;
1043bfc69a45STrond Myklebust }
104489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate);
1045bfc69a45STrond Myklebust 
10461da177e4SLinus Torvalds /*
10471da177e4SLinus Torvalds  * A check for whether or not the parent directory has changed.
10481da177e4SLinus Torvalds  * In the case it has, we assume that the dentries are untrustworthy
10491da177e4SLinus Torvalds  * and may need to be looked up again.
1050912a108dSNeilBrown  * If rcu_walk prevents us from performing a full check, return 0.
10511da177e4SLinus Torvalds  */
1052912a108dSNeilBrown static int nfs_check_verifier(struct inode *dir, struct dentry *dentry,
1053912a108dSNeilBrown 			      int rcu_walk)
10541da177e4SLinus Torvalds {
10551da177e4SLinus Torvalds 	if (IS_ROOT(dentry))
10561da177e4SLinus Torvalds 		return 1;
10574eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
10584eec952eSTrond Myklebust 		return 0;
1059f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
10606ecc5e8fSTrond Myklebust 		return 0;
1061f2c77f4eSTrond Myklebust 	/* Revalidate nfsi->cache_change_attribute before we declare a match */
10621cd9cb05STrond Myklebust 	if (nfs_mapping_need_revalidate_inode(dir)) {
1063912a108dSNeilBrown 		if (rcu_walk)
1064f2c77f4eSTrond Myklebust 			return 0;
10651cd9cb05STrond Myklebust 		if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
10661cd9cb05STrond Myklebust 			return 0;
10671cd9cb05STrond Myklebust 	}
1068f2c77f4eSTrond Myklebust 	if (!nfs_verify_change_attribute(dir, dentry->d_time))
1069f2c77f4eSTrond Myklebust 		return 0;
1070f2c77f4eSTrond Myklebust 	return 1;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds /*
1074a12802caSTrond Myklebust  * Use intent information to check whether or not we're going to do
1075a12802caSTrond Myklebust  * an O_EXCL create using this path component.
1076a12802caSTrond Myklebust  */
1077fa3c56bbSAl Viro static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags)
1078a12802caSTrond Myklebust {
1079a12802caSTrond Myklebust 	if (NFS_PROTO(dir)->version == 2)
1080a12802caSTrond Myklebust 		return 0;
1081fa3c56bbSAl Viro 	return flags & LOOKUP_EXCL;
1082a12802caSTrond Myklebust }
1083a12802caSTrond Myklebust 
1084a12802caSTrond Myklebust /*
10851d6757fbSTrond Myklebust  * Inode and filehandle revalidation for lookups.
10861d6757fbSTrond Myklebust  *
10871d6757fbSTrond Myklebust  * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
10881d6757fbSTrond Myklebust  * or if the intent information indicates that we're about to open this
10891d6757fbSTrond Myklebust  * particular file and the "nocto" mount flag is not set.
10901d6757fbSTrond Myklebust  *
10911d6757fbSTrond Myklebust  */
109265a0c149STrond Myklebust static
1093fa3c56bbSAl Viro int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags)
10941da177e4SLinus Torvalds {
10951da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
109665a0c149STrond Myklebust 	int ret;
10971da177e4SLinus Torvalds 
109836d43a43SDavid Howells 	if (IS_AUTOMOUNT(inode))
10994e99a1ffSTrond Myklebust 		return 0;
110047921921STrond Myklebust 
110147921921STrond Myklebust 	if (flags & LOOKUP_OPEN) {
110247921921STrond Myklebust 		switch (inode->i_mode & S_IFMT) {
110347921921STrond Myklebust 		case S_IFREG:
110447921921STrond Myklebust 			/* A NFSv4 OPEN will revalidate later */
110547921921STrond Myklebust 			if (server->caps & NFS_CAP_ATOMIC_OPEN)
110647921921STrond Myklebust 				goto out;
110747921921STrond Myklebust 			/* Fallthrough */
110847921921STrond Myklebust 		case S_IFDIR:
110947921921STrond Myklebust 			if (server->flags & NFS_MOUNT_NOCTO)
111047921921STrond Myklebust 				break;
111147921921STrond Myklebust 			/* NFS close-to-open cache consistency validation */
111247921921STrond Myklebust 			goto out_force;
111347921921STrond Myklebust 		}
111447921921STrond Myklebust 	}
111547921921STrond Myklebust 
11161da177e4SLinus Torvalds 	/* VFS wants an on-the-wire revalidation */
1117fa3c56bbSAl Viro 	if (flags & LOOKUP_REVAL)
11181da177e4SLinus Torvalds 		goto out_force;
111965a0c149STrond Myklebust out:
1120a61246c9SLance Shelton 	return (inode->i_nlink == 0) ? -ESTALE : 0;
11211da177e4SLinus Torvalds out_force:
11221fa1e384SNeilBrown 	if (flags & LOOKUP_RCU)
11231fa1e384SNeilBrown 		return -ECHILD;
112465a0c149STrond Myklebust 	ret = __nfs_revalidate_inode(server, inode);
112565a0c149STrond Myklebust 	if (ret != 0)
112665a0c149STrond Myklebust 		return ret;
112765a0c149STrond Myklebust 	goto out;
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds /*
11311da177e4SLinus Torvalds  * We judge how long we want to trust negative
11321da177e4SLinus Torvalds  * dentries by looking at the parent inode mtime.
11331da177e4SLinus Torvalds  *
11341da177e4SLinus Torvalds  * If parent mtime has changed, we revalidate, else we wait for a
11351da177e4SLinus Torvalds  * period corresponding to the parent's attribute cache timeout value.
1136912a108dSNeilBrown  *
1137912a108dSNeilBrown  * If LOOKUP_RCU prevents us from performing a full check, return 1
1138912a108dSNeilBrown  * suggesting a reval is needed.
11399f6d44d4STrond Myklebust  *
11409f6d44d4STrond Myklebust  * Note that when creating a new file, or looking up a rename target,
11419f6d44d4STrond Myklebust  * then it shouldn't be necessary to revalidate a negative dentry.
11421da177e4SLinus Torvalds  */
11431da177e4SLinus Torvalds static inline
11441da177e4SLinus Torvalds int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
1145fa3c56bbSAl Viro 		       unsigned int flags)
11461da177e4SLinus Torvalds {
11479f6d44d4STrond Myklebust 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
11481da177e4SLinus Torvalds 		return 0;
11494eec952eSTrond Myklebust 	if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
11504eec952eSTrond Myklebust 		return 1;
1151912a108dSNeilBrown 	return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU);
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
11545ceb9d7fSTrond Myklebust static int
11555ceb9d7fSTrond Myklebust nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry,
11565ceb9d7fSTrond Myklebust 			   struct inode *inode, int error)
11571da177e4SLinus Torvalds {
11585ceb9d7fSTrond Myklebust 	switch (error) {
11595ceb9d7fSTrond Myklebust 	case 1:
11606de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n",
11616de1472fSAl Viro 			__func__, dentry);
11621da177e4SLinus Torvalds 		return 1;
11635ceb9d7fSTrond Myklebust 	case 0:
1164a1643a92STrond Myklebust 		nfs_mark_for_revalidate(dir);
11651da177e4SLinus Torvalds 		if (inode && S_ISDIR(inode->i_mode)) {
11661da177e4SLinus Torvalds 			/* Purge readdir caches. */
11671da177e4SLinus Torvalds 			nfs_zap_caches(inode);
1168a3f432bfSJ. Bruce Fields 			/*
1169a3f432bfSJ. Bruce Fields 			 * We can't d_drop the root of a disconnected tree:
1170a3f432bfSJ. Bruce Fields 			 * its d_hash is on the s_anon list and d_drop() would hide
1171a3f432bfSJ. Bruce Fields 			 * it from shrink_dcache_for_unmount(), leading to busy
1172a3f432bfSJ. Bruce Fields 			 * inodes on unmount and further oopses.
1173a3f432bfSJ. Bruce Fields 			 */
1174a3f432bfSJ. Bruce Fields 			if (IS_ROOT(dentry))
11755ceb9d7fSTrond Myklebust 				return 1;
11761da177e4SLinus Torvalds 		}
11776de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n",
11786de1472fSAl Viro 				__func__, dentry);
11791da177e4SLinus Torvalds 		return 0;
11805ceb9d7fSTrond Myklebust 	}
11816de1472fSAl Viro 	dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n",
11826de1472fSAl Viro 				__func__, dentry, error);
1183e1fb4d05STrond Myklebust 	return error;
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
11865ceb9d7fSTrond Myklebust static int
11875ceb9d7fSTrond Myklebust nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry,
11885ceb9d7fSTrond Myklebust 			       unsigned int flags)
11895ceb9d7fSTrond Myklebust {
11905ceb9d7fSTrond Myklebust 	int ret = 1;
11915ceb9d7fSTrond Myklebust 	if (nfs_neg_need_reval(dir, dentry, flags)) {
11925ceb9d7fSTrond Myklebust 		if (flags & LOOKUP_RCU)
11935ceb9d7fSTrond Myklebust 			return -ECHILD;
11945ceb9d7fSTrond Myklebust 		ret = 0;
11955ceb9d7fSTrond Myklebust 	}
11965ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, NULL, ret);
11975ceb9d7fSTrond Myklebust }
11985ceb9d7fSTrond Myklebust 
11995ceb9d7fSTrond Myklebust static int
12005ceb9d7fSTrond Myklebust nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry,
12015ceb9d7fSTrond Myklebust 				struct inode *inode)
12025ceb9d7fSTrond Myklebust {
12035ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
12045ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
12055ceb9d7fSTrond Myklebust }
12065ceb9d7fSTrond Myklebust 
12075ceb9d7fSTrond Myklebust static int
12085ceb9d7fSTrond Myklebust nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry,
12095ceb9d7fSTrond Myklebust 			     struct inode *inode)
12105ceb9d7fSTrond Myklebust {
12115ceb9d7fSTrond Myklebust 	struct nfs_fh *fhandle;
12125ceb9d7fSTrond Myklebust 	struct nfs_fattr *fattr;
12135ceb9d7fSTrond Myklebust 	struct nfs4_label *label;
12145ceb9d7fSTrond Myklebust 	int ret;
12155ceb9d7fSTrond Myklebust 
12165ceb9d7fSTrond Myklebust 	ret = -ENOMEM;
12175ceb9d7fSTrond Myklebust 	fhandle = nfs_alloc_fhandle();
12185ceb9d7fSTrond Myklebust 	fattr = nfs_alloc_fattr();
12195ceb9d7fSTrond Myklebust 	label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
12205ceb9d7fSTrond Myklebust 	if (fhandle == NULL || fattr == NULL || IS_ERR(label))
12215ceb9d7fSTrond Myklebust 		goto out;
12225ceb9d7fSTrond Myklebust 
12235ceb9d7fSTrond Myklebust 	ret = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
12245ceb9d7fSTrond Myklebust 	if (ret < 0) {
12255ceb9d7fSTrond Myklebust 		if (ret == -ESTALE || ret == -ENOENT)
12265ceb9d7fSTrond Myklebust 			ret = 0;
12275ceb9d7fSTrond Myklebust 		goto out;
12285ceb9d7fSTrond Myklebust 	}
12295ceb9d7fSTrond Myklebust 	ret = 0;
12305ceb9d7fSTrond Myklebust 	if (nfs_compare_fh(NFS_FH(inode), fhandle))
12315ceb9d7fSTrond Myklebust 		goto out;
12325ceb9d7fSTrond Myklebust 	if (nfs_refresh_inode(inode, fattr) < 0)
12335ceb9d7fSTrond Myklebust 		goto out;
12345ceb9d7fSTrond Myklebust 
12355ceb9d7fSTrond Myklebust 	nfs_setsecurity(inode, fattr, label);
12365ceb9d7fSTrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
12375ceb9d7fSTrond Myklebust 
12385ceb9d7fSTrond Myklebust 	/* set a readdirplus hint that we had a cache miss */
12395ceb9d7fSTrond Myklebust 	nfs_force_use_readdirplus(dir);
12405ceb9d7fSTrond Myklebust 	ret = 1;
12415ceb9d7fSTrond Myklebust out:
12425ceb9d7fSTrond Myklebust 	nfs_free_fattr(fattr);
12435ceb9d7fSTrond Myklebust 	nfs_free_fhandle(fhandle);
12445ceb9d7fSTrond Myklebust 	nfs4_label_free(label);
12455ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, ret);
12465ceb9d7fSTrond Myklebust }
12475ceb9d7fSTrond Myklebust 
12485ceb9d7fSTrond Myklebust /*
12495ceb9d7fSTrond Myklebust  * This is called every time the dcache has a lookup hit,
12505ceb9d7fSTrond Myklebust  * and we should check whether we can really trust that
12515ceb9d7fSTrond Myklebust  * lookup.
12525ceb9d7fSTrond Myklebust  *
12535ceb9d7fSTrond Myklebust  * NOTE! The hit can be a negative hit too, don't assume
12545ceb9d7fSTrond Myklebust  * we have an inode!
12555ceb9d7fSTrond Myklebust  *
12565ceb9d7fSTrond Myklebust  * If the parent directory is seen to have changed, we throw out the
12575ceb9d7fSTrond Myklebust  * cached dentry and do a new lookup.
12585ceb9d7fSTrond Myklebust  */
12595ceb9d7fSTrond Myklebust static int
12605ceb9d7fSTrond Myklebust nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
12615ceb9d7fSTrond Myklebust 			 unsigned int flags)
12625ceb9d7fSTrond Myklebust {
12635ceb9d7fSTrond Myklebust 	struct inode *inode;
12645ceb9d7fSTrond Myklebust 	int error;
12655ceb9d7fSTrond Myklebust 
12665ceb9d7fSTrond Myklebust 	nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
12675ceb9d7fSTrond Myklebust 	inode = d_inode(dentry);
12685ceb9d7fSTrond Myklebust 
12695ceb9d7fSTrond Myklebust 	if (!inode)
12705ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_negative(dir, dentry, flags);
12715ceb9d7fSTrond Myklebust 
12725ceb9d7fSTrond Myklebust 	if (is_bad_inode(inode)) {
12735ceb9d7fSTrond Myklebust 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
12745ceb9d7fSTrond Myklebust 				__func__, dentry);
12755ceb9d7fSTrond Myklebust 		goto out_bad;
12765ceb9d7fSTrond Myklebust 	}
12775ceb9d7fSTrond Myklebust 
12785ceb9d7fSTrond Myklebust 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
12795ceb9d7fSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
12805ceb9d7fSTrond Myklebust 
12815ceb9d7fSTrond Myklebust 	/* Force a full look up iff the parent directory has changed */
12825ceb9d7fSTrond Myklebust 	if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) &&
12835ceb9d7fSTrond Myklebust 	    nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) {
12845ceb9d7fSTrond Myklebust 		error = nfs_lookup_verify_inode(inode, flags);
12855ceb9d7fSTrond Myklebust 		if (error) {
12865ceb9d7fSTrond Myklebust 			if (error == -ESTALE)
12875ceb9d7fSTrond Myklebust 				nfs_zap_caches(dir);
12885ceb9d7fSTrond Myklebust 			goto out_bad;
12895ceb9d7fSTrond Myklebust 		}
12905ceb9d7fSTrond Myklebust 		nfs_advise_use_readdirplus(dir);
12915ceb9d7fSTrond Myklebust 		goto out_valid;
12925ceb9d7fSTrond Myklebust 	}
12935ceb9d7fSTrond Myklebust 
12945ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
12955ceb9d7fSTrond Myklebust 		return -ECHILD;
12965ceb9d7fSTrond Myklebust 
12975ceb9d7fSTrond Myklebust 	if (NFS_STALE(inode))
12985ceb9d7fSTrond Myklebust 		goto out_bad;
12995ceb9d7fSTrond Myklebust 
13005ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_enter(dir, dentry, flags);
13015ceb9d7fSTrond Myklebust 	error = nfs_lookup_revalidate_dentry(dir, dentry, inode);
13025ceb9d7fSTrond Myklebust 	trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error);
13035ceb9d7fSTrond Myklebust 	return error;
13045ceb9d7fSTrond Myklebust out_valid:
13055ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 1);
13065ceb9d7fSTrond Myklebust out_bad:
13075ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU)
13085ceb9d7fSTrond Myklebust 		return -ECHILD;
13095ceb9d7fSTrond Myklebust 	return nfs_lookup_revalidate_done(dir, dentry, inode, 0);
13105ceb9d7fSTrond Myklebust }
13115ceb9d7fSTrond Myklebust 
13125ceb9d7fSTrond Myklebust static int
1313c7944ebbSTrond Myklebust __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags,
1314c7944ebbSTrond Myklebust 			int (*reval)(struct inode *, struct dentry *, unsigned int))
13155ceb9d7fSTrond Myklebust {
13165ceb9d7fSTrond Myklebust 	struct dentry *parent;
13175ceb9d7fSTrond Myklebust 	struct inode *dir;
13185ceb9d7fSTrond Myklebust 	int ret;
13195ceb9d7fSTrond Myklebust 
13205ceb9d7fSTrond Myklebust 	if (flags & LOOKUP_RCU) {
13215ceb9d7fSTrond Myklebust 		parent = READ_ONCE(dentry->d_parent);
13225ceb9d7fSTrond Myklebust 		dir = d_inode_rcu(parent);
13235ceb9d7fSTrond Myklebust 		if (!dir)
13245ceb9d7fSTrond Myklebust 			return -ECHILD;
1325c7944ebbSTrond Myklebust 		ret = reval(dir, dentry, flags);
13265ceb9d7fSTrond Myklebust 		if (parent != READ_ONCE(dentry->d_parent))
13275ceb9d7fSTrond Myklebust 			return -ECHILD;
13285ceb9d7fSTrond Myklebust 	} else {
13295ceb9d7fSTrond Myklebust 		parent = dget_parent(dentry);
1330c7944ebbSTrond Myklebust 		ret = reval(d_inode(parent), dentry, flags);
13315ceb9d7fSTrond Myklebust 		dput(parent);
13325ceb9d7fSTrond Myklebust 	}
13335ceb9d7fSTrond Myklebust 	return ret;
13345ceb9d7fSTrond Myklebust }
13355ceb9d7fSTrond Myklebust 
1336c7944ebbSTrond Myklebust static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1337c7944ebbSTrond Myklebust {
1338c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate);
1339c7944ebbSTrond Myklebust }
1340c7944ebbSTrond Myklebust 
13411da177e4SLinus Torvalds /*
13422b0143b5SDavid Howells  * A weaker form of d_revalidate for revalidating just the d_inode(dentry)
1343ecf3d1f1SJeff Layton  * when we don't really care about the dentry name. This is called when a
1344ecf3d1f1SJeff Layton  * pathwalk ends on a dentry that was not found via a normal lookup in the
1345ecf3d1f1SJeff Layton  * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals).
1346ecf3d1f1SJeff Layton  *
1347ecf3d1f1SJeff Layton  * In this situation, we just want to verify that the inode itself is OK
1348ecf3d1f1SJeff Layton  * since the dentry might have changed on the server.
1349ecf3d1f1SJeff Layton  */
1350ecf3d1f1SJeff Layton static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags)
1351ecf3d1f1SJeff Layton {
13522b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
13539cdd1d3fSTrond Myklebust 	int error = 0;
1354ecf3d1f1SJeff Layton 
1355ecf3d1f1SJeff Layton 	/*
1356ecf3d1f1SJeff Layton 	 * I believe we can only get a negative dentry here in the case of a
1357ecf3d1f1SJeff Layton 	 * procfs-style symlink. Just assume it's correct for now, but we may
1358ecf3d1f1SJeff Layton 	 * eventually need to do something more here.
1359ecf3d1f1SJeff Layton 	 */
1360ecf3d1f1SJeff Layton 	if (!inode) {
13616de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n",
13626de1472fSAl Viro 				__func__, dentry);
1363ecf3d1f1SJeff Layton 		return 1;
1364ecf3d1f1SJeff Layton 	}
1365ecf3d1f1SJeff Layton 
1366ecf3d1f1SJeff Layton 	if (is_bad_inode(inode)) {
13676de1472fSAl Viro 		dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n",
13686de1472fSAl Viro 				__func__, dentry);
1369ecf3d1f1SJeff Layton 		return 0;
1370ecf3d1f1SJeff Layton 	}
1371ecf3d1f1SJeff Layton 
1372b688741cSNeilBrown 	error = nfs_lookup_verify_inode(inode, flags);
1373ecf3d1f1SJeff Layton 	dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n",
1374ecf3d1f1SJeff Layton 			__func__, inode->i_ino, error ? "invalid" : "valid");
1375ecf3d1f1SJeff Layton 	return !error;
1376ecf3d1f1SJeff Layton }
1377ecf3d1f1SJeff Layton 
1378ecf3d1f1SJeff Layton /*
13791da177e4SLinus Torvalds  * This is called from dput() when d_count is going to 0.
13801da177e4SLinus Torvalds  */
1381fe15ce44SNick Piggin static int nfs_dentry_delete(const struct dentry *dentry)
13821da177e4SLinus Torvalds {
13836de1472fSAl Viro 	dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n",
13846de1472fSAl Viro 		dentry, dentry->d_flags);
13851da177e4SLinus Torvalds 
138677f11192STrond Myklebust 	/* Unhash any dentry with a stale inode */
13872b0143b5SDavid Howells 	if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry)))
138877f11192STrond Myklebust 		return 1;
138977f11192STrond Myklebust 
13901da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
13911da177e4SLinus Torvalds 		/* Unhash it, so that ->d_iput() would be called */
13921da177e4SLinus Torvalds 		return 1;
13931da177e4SLinus Torvalds 	}
13941751e8a6SLinus Torvalds 	if (!(dentry->d_sb->s_flags & SB_ACTIVE)) {
13951da177e4SLinus Torvalds 		/* Unhash it, so that ancestors of killed async unlink
13961da177e4SLinus Torvalds 		 * files will be cleaned up during umount */
13971da177e4SLinus Torvalds 		return 1;
13981da177e4SLinus Torvalds 	}
13991da177e4SLinus Torvalds 	return 0;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds }
14021da177e4SLinus Torvalds 
14031f018458STrond Myklebust /* Ensure that we revalidate inode->i_nlink */
14041b83d707STrond Myklebust static void nfs_drop_nlink(struct inode *inode)
14051b83d707STrond Myklebust {
14061b83d707STrond Myklebust 	spin_lock(&inode->i_lock);
14071f018458STrond Myklebust 	/* drop the inode if we're reasonably sure this is the last link */
140859a707b0STrond Myklebust 	if (inode->i_nlink > 0)
140959a707b0STrond Myklebust 		drop_nlink(inode);
141059a707b0STrond Myklebust 	NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter();
141116e14375STrond Myklebust 	NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
141216e14375STrond Myklebust 		| NFS_INO_INVALID_CTIME
141359a707b0STrond Myklebust 		| NFS_INO_INVALID_OTHER
141459a707b0STrond Myklebust 		| NFS_INO_REVAL_FORCED;
14151b83d707STrond Myklebust 	spin_unlock(&inode->i_lock);
14161b83d707STrond Myklebust }
14171b83d707STrond Myklebust 
14181da177e4SLinus Torvalds /*
14191da177e4SLinus Torvalds  * Called when the dentry loses inode.
14201da177e4SLinus Torvalds  * We use it to clean up silly-renamed files.
14211da177e4SLinus Torvalds  */
14221da177e4SLinus Torvalds static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
14231da177e4SLinus Torvalds {
142483672d39SNeil Brown 	if (S_ISDIR(inode->i_mode))
142583672d39SNeil Brown 		/* drop any readdir cache as it could easily be old */
142683672d39SNeil Brown 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
142783672d39SNeil Brown 
14281da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
1429e4eff1a6STrond Myklebust 		nfs_complete_unlink(dentry, inode);
14301f018458STrond Myklebust 		nfs_drop_nlink(inode);
14311da177e4SLinus Torvalds 	}
14321da177e4SLinus Torvalds 	iput(inode);
14331da177e4SLinus Torvalds }
14341da177e4SLinus Torvalds 
1435b1942c5fSAl Viro static void nfs_d_release(struct dentry *dentry)
1436b1942c5fSAl Viro {
1437b1942c5fSAl Viro 	/* free cached devname value, if it survived that far */
1438b1942c5fSAl Viro 	if (unlikely(dentry->d_fsdata)) {
1439b1942c5fSAl Viro 		if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1440b1942c5fSAl Viro 			WARN_ON(1);
1441b1942c5fSAl Viro 		else
1442b1942c5fSAl Viro 			kfree(dentry->d_fsdata);
1443b1942c5fSAl Viro 	}
1444b1942c5fSAl Viro }
1445b1942c5fSAl Viro 
1446f786aa90SAl Viro const struct dentry_operations nfs_dentry_operations = {
14471da177e4SLinus Torvalds 	.d_revalidate	= nfs_lookup_revalidate,
1448ecf3d1f1SJeff Layton 	.d_weak_revalidate	= nfs_weak_revalidate,
14491da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
14501da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
145136d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1452b1942c5fSAl Viro 	.d_release	= nfs_d_release,
14531da177e4SLinus Torvalds };
1454ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_dentry_operations);
14551da177e4SLinus Torvalds 
1456597d9289SBryan Schumaker struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
14571da177e4SLinus Torvalds {
14581da177e4SLinus Torvalds 	struct dentry *res;
14591da177e4SLinus Torvalds 	struct inode *inode = NULL;
1460e1fb4d05STrond Myklebust 	struct nfs_fh *fhandle = NULL;
1461e1fb4d05STrond Myklebust 	struct nfs_fattr *fattr = NULL;
14621775fd3eSDavid Quigley 	struct nfs4_label *label = NULL;
14631da177e4SLinus Torvalds 	int error;
14641da177e4SLinus Torvalds 
14656de1472fSAl Viro 	dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry);
146691d5b470SChuck Lever 	nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
14671da177e4SLinus Torvalds 
1468130f9ab7SAl Viro 	if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen))
1469130f9ab7SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
14701da177e4SLinus Torvalds 
1471fd684071STrond Myklebust 	/*
1472fd684071STrond Myklebust 	 * If we're doing an exclusive create, optimize away the lookup
1473fd684071STrond Myklebust 	 * but don't hash the dentry.
1474fd684071STrond Myklebust 	 */
14759f6d44d4STrond Myklebust 	if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET)
1476130f9ab7SAl Viro 		return NULL;
14771da177e4SLinus Torvalds 
1478e1fb4d05STrond Myklebust 	res = ERR_PTR(-ENOMEM);
1479e1fb4d05STrond Myklebust 	fhandle = nfs_alloc_fhandle();
1480e1fb4d05STrond Myklebust 	fattr = nfs_alloc_fattr();
1481e1fb4d05STrond Myklebust 	if (fhandle == NULL || fattr == NULL)
1482e1fb4d05STrond Myklebust 		goto out;
1483e1fb4d05STrond Myklebust 
148414c43f76SDavid Quigley 	label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT);
148514c43f76SDavid Quigley 	if (IS_ERR(label))
148614c43f76SDavid Quigley 		goto out;
148714c43f76SDavid Quigley 
14886e0d0be7STrond Myklebust 	trace_nfs_lookup_enter(dir, dentry, flags);
14891775fd3eSDavid Quigley 	error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, label);
14901da177e4SLinus Torvalds 	if (error == -ENOENT)
14911da177e4SLinus Torvalds 		goto no_entry;
14921da177e4SLinus Torvalds 	if (error < 0) {
14931da177e4SLinus Torvalds 		res = ERR_PTR(error);
1494bf130914SAl Viro 		goto out_label;
14951da177e4SLinus Torvalds 	}
14961775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1497bf0c84f1SNamhyung Kim 	res = ERR_CAST(inode);
149803f28e3aSTrond Myklebust 	if (IS_ERR(res))
1499bf130914SAl Viro 		goto out_label;
150054ceac45SDavid Howells 
150163519fbcSTrond Myklebust 	/* Notify readdir to use READDIRPLUS */
150263519fbcSTrond Myklebust 	nfs_force_use_readdirplus(dir);
1503d69ee9b8STrond Myklebust 
15041da177e4SLinus Torvalds no_entry:
150541d28bcaSAl Viro 	res = d_splice_alias(inode, dentry);
15069eaef27bSTrond Myklebust 	if (res != NULL) {
15079eaef27bSTrond Myklebust 		if (IS_ERR(res))
1508bf130914SAl Viro 			goto out_label;
15091da177e4SLinus Torvalds 		dentry = res;
15109eaef27bSTrond Myklebust 	}
15111da177e4SLinus Torvalds 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1512bf130914SAl Viro out_label:
15136e0d0be7STrond Myklebust 	trace_nfs_lookup_exit(dir, dentry, flags, error);
151414c43f76SDavid Quigley 	nfs4_label_free(label);
15151da177e4SLinus Torvalds out:
1516e1fb4d05STrond Myklebust 	nfs_free_fattr(fattr);
1517e1fb4d05STrond Myklebust 	nfs_free_fhandle(fhandle);
15181da177e4SLinus Torvalds 	return res;
15191da177e4SLinus Torvalds }
1520ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_lookup);
15211da177e4SLinus Torvalds 
152289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
15230b728e19SAl Viro static int nfs4_lookup_revalidate(struct dentry *, unsigned int);
15241da177e4SLinus Torvalds 
1525f786aa90SAl Viro const struct dentry_operations nfs4_dentry_operations = {
15260ef97dcfSMiklos Szeredi 	.d_revalidate	= nfs4_lookup_revalidate,
1527b688741cSNeilBrown 	.d_weak_revalidate	= nfs_weak_revalidate,
15281da177e4SLinus Torvalds 	.d_delete	= nfs_dentry_delete,
15291da177e4SLinus Torvalds 	.d_iput		= nfs_dentry_iput,
153036d43a43SDavid Howells 	.d_automount	= nfs_d_automount,
1531b1942c5fSAl Viro 	.d_release	= nfs_d_release,
15321da177e4SLinus Torvalds };
153389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs4_dentry_operations);
15341da177e4SLinus Torvalds 
15358a5e929dSAl Viro static fmode_t flags_to_mode(int flags)
15368a5e929dSAl Viro {
15378a5e929dSAl Viro 	fmode_t res = (__force fmode_t)flags & FMODE_EXEC;
15388a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_WRONLY)
15398a5e929dSAl Viro 		res |= FMODE_READ;
15408a5e929dSAl Viro 	if ((flags & O_ACCMODE) != O_RDONLY)
15418a5e929dSAl Viro 		res |= FMODE_WRITE;
15428a5e929dSAl Viro 	return res;
15438a5e929dSAl Viro }
15448a5e929dSAl Viro 
1545532d4defSNeilBrown static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp)
1546cd9a1c0eSTrond Myklebust {
1547532d4defSNeilBrown 	return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp);
1548cd9a1c0eSTrond Myklebust }
1549cd9a1c0eSTrond Myklebust 
1550cd9a1c0eSTrond Myklebust static int do_open(struct inode *inode, struct file *filp)
1551cd9a1c0eSTrond Myklebust {
1552f1fe29b4SDavid Howells 	nfs_fscache_open_file(inode, filp);
1553cd9a1c0eSTrond Myklebust 	return 0;
1554cd9a1c0eSTrond Myklebust }
1555cd9a1c0eSTrond Myklebust 
1556d9585277SAl Viro static int nfs_finish_open(struct nfs_open_context *ctx,
15570dd2b474SMiklos Szeredi 			   struct dentry *dentry,
1558b452a458SAl Viro 			   struct file *file, unsigned open_flags)
1559cd9a1c0eSTrond Myklebust {
15600dd2b474SMiklos Szeredi 	int err;
15610dd2b474SMiklos Szeredi 
1562be12af3eSAl Viro 	err = finish_open(file, dentry, do_open);
156330d90494SAl Viro 	if (err)
1564d9585277SAl Viro 		goto out;
1565eaa2b82cSNeilBrown 	if (S_ISREG(file->f_path.dentry->d_inode->i_mode))
156630d90494SAl Viro 		nfs_file_set_open_context(file, ctx);
1567eaa2b82cSNeilBrown 	else
1568eaa2b82cSNeilBrown 		err = -ESTALE;
1569cd9a1c0eSTrond Myklebust out:
1570d9585277SAl Viro 	return err;
1571cd9a1c0eSTrond Myklebust }
1572cd9a1c0eSTrond Myklebust 
157373a79706SBryan Schumaker int nfs_atomic_open(struct inode *dir, struct dentry *dentry,
157430d90494SAl Viro 		    struct file *file, unsigned open_flags,
157544907d79SAl Viro 		    umode_t mode)
15761da177e4SLinus Torvalds {
1577c94c0953SAl Viro 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1578cd9a1c0eSTrond Myklebust 	struct nfs_open_context *ctx;
15790dd2b474SMiklos Szeredi 	struct dentry *res;
15800dd2b474SMiklos Szeredi 	struct iattr attr = { .ia_valid = ATTR_OPEN };
1581f46e0bd3STrond Myklebust 	struct inode *inode;
15821472b83eSTrond Myklebust 	unsigned int lookup_flags = 0;
1583c94c0953SAl Viro 	bool switched = false;
158473a09dd9SAl Viro 	int created = 0;
1585898f635cSTrond Myklebust 	int err;
15861da177e4SLinus Torvalds 
15870dd2b474SMiklos Szeredi 	/* Expect a negative dentry */
15882b0143b5SDavid Howells 	BUG_ON(d_inode(dentry));
15890dd2b474SMiklos Szeredi 
15901e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n",
15916de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
15921e7cb3dcSChuck Lever 
15939597c13bSJeff Layton 	err = nfs_check_flags(open_flags);
15949597c13bSJeff Layton 	if (err)
15959597c13bSJeff Layton 		return err;
15969597c13bSJeff Layton 
15970dd2b474SMiklos Szeredi 	/* NFS only supports OPEN on regular files */
15980dd2b474SMiklos Szeredi 	if ((open_flags & O_DIRECTORY)) {
159900699ad8SAl Viro 		if (!d_in_lookup(dentry)) {
16000dd2b474SMiklos Szeredi 			/*
16010dd2b474SMiklos Szeredi 			 * Hashed negative dentry with O_DIRECTORY: dentry was
16020dd2b474SMiklos Szeredi 			 * revalidated and is fine, no need to perform lookup
16030dd2b474SMiklos Szeredi 			 * again
16040dd2b474SMiklos Szeredi 			 */
1605d9585277SAl Viro 			return -ENOENT;
16060dd2b474SMiklos Szeredi 		}
16071472b83eSTrond Myklebust 		lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY;
16081da177e4SLinus Torvalds 		goto no_open;
16091da177e4SLinus Torvalds 	}
16101da177e4SLinus Torvalds 
16110dd2b474SMiklos Szeredi 	if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
1612d9585277SAl Viro 		return -ENAMETOOLONG;
16131da177e4SLinus Torvalds 
16140dd2b474SMiklos Szeredi 	if (open_flags & O_CREAT) {
1615dff25ddbSAndreas Gruenbacher 		struct nfs_server *server = NFS_SERVER(dir);
1616dff25ddbSAndreas Gruenbacher 
1617dff25ddbSAndreas Gruenbacher 		if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
1618dff25ddbSAndreas Gruenbacher 			mode &= ~current_umask();
1619dff25ddbSAndreas Gruenbacher 
1620536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_MODE;
1621dff25ddbSAndreas Gruenbacher 		attr.ia_mode = mode;
16220dd2b474SMiklos Szeredi 	}
1623536e43d1STrond Myklebust 	if (open_flags & O_TRUNC) {
1624536e43d1STrond Myklebust 		attr.ia_valid |= ATTR_SIZE;
1625536e43d1STrond Myklebust 		attr.ia_size = 0;
1626cd9a1c0eSTrond Myklebust 	}
1627cd9a1c0eSTrond Myklebust 
1628c94c0953SAl Viro 	if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) {
1629c94c0953SAl Viro 		d_drop(dentry);
1630c94c0953SAl Viro 		switched = true;
1631c94c0953SAl Viro 		dentry = d_alloc_parallel(dentry->d_parent,
1632c94c0953SAl Viro 					  &dentry->d_name, &wq);
1633c94c0953SAl Viro 		if (IS_ERR(dentry))
1634c94c0953SAl Viro 			return PTR_ERR(dentry);
1635c94c0953SAl Viro 		if (unlikely(!d_in_lookup(dentry)))
1636c94c0953SAl Viro 			return finish_no_open(file, dentry);
1637c94c0953SAl Viro 	}
1638c94c0953SAl Viro 
1639532d4defSNeilBrown 	ctx = create_nfs_open_context(dentry, open_flags, file);
16400dd2b474SMiklos Szeredi 	err = PTR_ERR(ctx);
16410dd2b474SMiklos Szeredi 	if (IS_ERR(ctx))
1642d9585277SAl Viro 		goto out;
16430dd2b474SMiklos Szeredi 
16446e0d0be7STrond Myklebust 	trace_nfs_atomic_open_enter(dir, ctx, open_flags);
164573a09dd9SAl Viro 	inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created);
164673a09dd9SAl Viro 	if (created)
164773a09dd9SAl Viro 		file->f_mode |= FMODE_CREATED;
1648275bb307STrond Myklebust 	if (IS_ERR(inode)) {
16490dd2b474SMiklos Szeredi 		err = PTR_ERR(inode);
16506e0d0be7STrond Myklebust 		trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
16512d9db750STrond Myklebust 		put_nfs_open_context(ctx);
1652d20cb71dSAl Viro 		d_drop(dentry);
16530dd2b474SMiklos Szeredi 		switch (err) {
16541da177e4SLinus Torvalds 		case -ENOENT:
1655774d9513SPeng Tao 			d_splice_alias(NULL, dentry);
1656809fd143STrond Myklebust 			nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
16570dd2b474SMiklos Szeredi 			break;
16581788ea6eSJeff Layton 		case -EISDIR:
16596f926b5bSTrond Myklebust 		case -ENOTDIR:
16606f926b5bSTrond Myklebust 			goto no_open;
16611da177e4SLinus Torvalds 		case -ELOOP:
16620dd2b474SMiklos Szeredi 			if (!(open_flags & O_NOFOLLOW))
16631da177e4SLinus Torvalds 				goto no_open;
16640dd2b474SMiklos Szeredi 			break;
16651da177e4SLinus Torvalds 			/* case -EINVAL: */
16661da177e4SLinus Torvalds 		default:
16670dd2b474SMiklos Szeredi 			break;
16681da177e4SLinus Torvalds 		}
16691da177e4SLinus Torvalds 		goto out;
16701da177e4SLinus Torvalds 	}
16710dd2b474SMiklos Szeredi 
1672b452a458SAl Viro 	err = nfs_finish_open(ctx, ctx->dentry, file, open_flags);
16736e0d0be7STrond Myklebust 	trace_nfs_atomic_open_exit(dir, ctx, open_flags, err);
16742d9db750STrond Myklebust 	put_nfs_open_context(ctx);
1675d9585277SAl Viro out:
1676c94c0953SAl Viro 	if (unlikely(switched)) {
1677c94c0953SAl Viro 		d_lookup_done(dentry);
1678c94c0953SAl Viro 		dput(dentry);
1679c94c0953SAl Viro 	}
1680d9585277SAl Viro 	return err;
16810dd2b474SMiklos Szeredi 
16821da177e4SLinus Torvalds no_open:
16831472b83eSTrond Myklebust 	res = nfs_lookup(dir, dentry, lookup_flags);
1684c94c0953SAl Viro 	if (switched) {
1685c94c0953SAl Viro 		d_lookup_done(dentry);
1686c94c0953SAl Viro 		if (!res)
1687c94c0953SAl Viro 			res = dentry;
1688c94c0953SAl Viro 		else
1689c94c0953SAl Viro 			dput(dentry);
1690c94c0953SAl Viro 	}
16910dd2b474SMiklos Szeredi 	if (IS_ERR(res))
1692c94c0953SAl Viro 		return PTR_ERR(res);
1693e45198a6SAl Viro 	return finish_no_open(file, res);
16941da177e4SLinus Torvalds }
169589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_atomic_open);
16961da177e4SLinus Torvalds 
1697c7944ebbSTrond Myklebust static int
1698c7944ebbSTrond Myklebust nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry,
1699c7944ebbSTrond Myklebust 			  unsigned int flags)
17001da177e4SLinus Torvalds {
1701657e94b6SNick Piggin 	struct inode *inode;
17021da177e4SLinus Torvalds 
1703fa3c56bbSAl Viro 	if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY))
1704c7944ebbSTrond Myklebust 		goto full_reval;
1705eda72afbSMiklos Szeredi 	if (d_mountpoint(dentry))
1706c7944ebbSTrond Myklebust 		goto full_reval;
17072b484297STrond Myklebust 
17082b0143b5SDavid Howells 	inode = d_inode(dentry);
17092b484297STrond Myklebust 
17101da177e4SLinus Torvalds 	/* We can't create new files in nfs_open_revalidate(), so we
17111da177e4SLinus Torvalds 	 * optimize away revalidation of negative dentries.
17121da177e4SLinus Torvalds 	 */
1713c7944ebbSTrond Myklebust 	if (inode == NULL)
1714c7944ebbSTrond Myklebust 		goto full_reval;
171549317a7fSNeilBrown 
1716c7944ebbSTrond Myklebust 	if (NFS_PROTO(dir)->have_delegation(inode, FMODE_READ))
1717c7944ebbSTrond Myklebust 		return nfs_lookup_revalidate_delegated(dir, dentry, inode);
1718216d5d06STrond Myklebust 
17191da177e4SLinus Torvalds 	/* NFS only supports OPEN on regular files */
17201da177e4SLinus Torvalds 	if (!S_ISREG(inode->i_mode))
1721c7944ebbSTrond Myklebust 		goto full_reval;
1722c7944ebbSTrond Myklebust 
17231da177e4SLinus Torvalds 	/* We cannot do exclusive creation on a positive dentry */
1724c7944ebbSTrond Myklebust 	if (flags & (LOOKUP_EXCL | LOOKUP_REVAL))
1725c7944ebbSTrond Myklebust 		goto reval_dentry;
1726c7944ebbSTrond Myklebust 
1727c7944ebbSTrond Myklebust 	/* Check if the directory changed */
1728c7944ebbSTrond Myklebust 	if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU))
1729c7944ebbSTrond Myklebust 		goto reval_dentry;
17301da177e4SLinus Torvalds 
17310ef97dcfSMiklos Szeredi 	/* Let f_op->open() actually open (and revalidate) the file */
1732c7944ebbSTrond Myklebust 	return 1;
1733c7944ebbSTrond Myklebust reval_dentry:
1734c7944ebbSTrond Myklebust 	if (flags & LOOKUP_RCU)
1735c7944ebbSTrond Myklebust 		return -ECHILD;
173642f72cf3Szhangliguang 	return nfs_lookup_revalidate_dentry(dir, dentry, inode);
17370ef97dcfSMiklos Szeredi 
1738c7944ebbSTrond Myklebust full_reval:
1739c7944ebbSTrond Myklebust 	return nfs_do_lookup_revalidate(dir, dentry, flags);
1740c7944ebbSTrond Myklebust }
1741535918f1STrond Myklebust 
1742c7944ebbSTrond Myklebust static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags)
1743c7944ebbSTrond Myklebust {
1744c7944ebbSTrond Myklebust 	return __nfs_lookup_revalidate(dentry, flags,
1745c7944ebbSTrond Myklebust 			nfs4_do_lookup_revalidate);
1746c0204fd2STrond Myklebust }
1747c0204fd2STrond Myklebust 
17481da177e4SLinus Torvalds #endif /* CONFIG_NFSV4 */
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds /*
17511da177e4SLinus Torvalds  * Code common to create, mkdir, and mknod.
17521da177e4SLinus Torvalds  */
17531da177e4SLinus Torvalds int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
17541775fd3eSDavid Quigley 				struct nfs_fattr *fattr,
17551775fd3eSDavid Quigley 				struct nfs4_label *label)
17561da177e4SLinus Torvalds {
1757fab728e1STrond Myklebust 	struct dentry *parent = dget_parent(dentry);
17582b0143b5SDavid Howells 	struct inode *dir = d_inode(parent);
17591da177e4SLinus Torvalds 	struct inode *inode;
1760b0c6108eSAl Viro 	struct dentry *d;
17611da177e4SLinus Torvalds 	int error = -EACCES;
17621da177e4SLinus Torvalds 
1763fab728e1STrond Myklebust 	d_drop(dentry);
1764fab728e1STrond Myklebust 
17651da177e4SLinus Torvalds 	/* We may have been initialized further down */
17662b0143b5SDavid Howells 	if (d_really_is_positive(dentry))
1767fab728e1STrond Myklebust 		goto out;
17681da177e4SLinus Torvalds 	if (fhandle->size == 0) {
17691775fd3eSDavid Quigley 		error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr, NULL);
17701da177e4SLinus Torvalds 		if (error)
1771fab728e1STrond Myklebust 			goto out_error;
17721da177e4SLinus Torvalds 	}
17735724ab37STrond Myklebust 	nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
17741da177e4SLinus Torvalds 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
17751da177e4SLinus Torvalds 		struct nfs_server *server = NFS_SB(dentry->d_sb);
1776a841b54dSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, fhandle,
1777a841b54dSTrond Myklebust 				fattr, NULL, NULL);
17781da177e4SLinus Torvalds 		if (error < 0)
1779fab728e1STrond Myklebust 			goto out_error;
17801da177e4SLinus Torvalds 	}
17811775fd3eSDavid Quigley 	inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label);
1782b0c6108eSAl Viro 	d = d_splice_alias(inode, dentry);
1783b0c6108eSAl Viro 	if (IS_ERR(d)) {
1784b0c6108eSAl Viro 		error = PTR_ERR(d);
1785fab728e1STrond Myklebust 		goto out_error;
1786b0c6108eSAl Viro 	}
1787b0c6108eSAl Viro 	dput(d);
1788fab728e1STrond Myklebust out:
1789fab728e1STrond Myklebust 	dput(parent);
17901da177e4SLinus Torvalds 	return 0;
1791fab728e1STrond Myklebust out_error:
1792fab728e1STrond Myklebust 	nfs_mark_for_revalidate(dir);
1793fab728e1STrond Myklebust 	dput(parent);
1794fab728e1STrond Myklebust 	return error;
17951da177e4SLinus Torvalds }
1796ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_instantiate);
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds /*
17991da177e4SLinus Torvalds  * Following a failed create operation, we drop the dentry rather
18001da177e4SLinus Torvalds  * than retain a negative dentry. This avoids a problem in the event
18011da177e4SLinus Torvalds  * that the operation succeeded on the server, but an error in the
18021da177e4SLinus Torvalds  * reply path made it appear to have failed.
18031da177e4SLinus Torvalds  */
1804597d9289SBryan Schumaker int nfs_create(struct inode *dir, struct dentry *dentry,
1805ebfc3b49SAl Viro 		umode_t mode, bool excl)
18061da177e4SLinus Torvalds {
18071da177e4SLinus Torvalds 	struct iattr attr;
1808ebfc3b49SAl Viro 	int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT;
18091da177e4SLinus Torvalds 	int error;
18101da177e4SLinus Torvalds 
18111e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: create(%s/%lu), %pd\n",
18126de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 	attr.ia_mode = mode;
18151da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
18161da177e4SLinus Torvalds 
18178b0ad3d4STrond Myklebust 	trace_nfs_create_enter(dir, dentry, open_flags);
18188867fe58SMiklos Szeredi 	error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
18198b0ad3d4STrond Myklebust 	trace_nfs_create_exit(dir, dentry, open_flags, error);
18201da177e4SLinus Torvalds 	if (error != 0)
18211da177e4SLinus Torvalds 		goto out_err;
18221da177e4SLinus Torvalds 	return 0;
18231da177e4SLinus Torvalds out_err:
18241da177e4SLinus Torvalds 	d_drop(dentry);
18251da177e4SLinus Torvalds 	return error;
18261da177e4SLinus Torvalds }
1827ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create);
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds /*
18301da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
18311da177e4SLinus Torvalds  */
1832597d9289SBryan Schumaker int
18331a67aafbSAl Viro nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
18341da177e4SLinus Torvalds {
18351da177e4SLinus Torvalds 	struct iattr attr;
18361da177e4SLinus Torvalds 	int status;
18371da177e4SLinus Torvalds 
18381e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n",
18396de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	attr.ia_mode = mode;
18421da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
18431da177e4SLinus Torvalds 
18441ca42382STrond Myklebust 	trace_nfs_mknod_enter(dir, dentry);
18451da177e4SLinus Torvalds 	status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
18461ca42382STrond Myklebust 	trace_nfs_mknod_exit(dir, dentry, status);
18471da177e4SLinus Torvalds 	if (status != 0)
18481da177e4SLinus Torvalds 		goto out_err;
18491da177e4SLinus Torvalds 	return 0;
18501da177e4SLinus Torvalds out_err:
18511da177e4SLinus Torvalds 	d_drop(dentry);
18521da177e4SLinus Torvalds 	return status;
18531da177e4SLinus Torvalds }
1854ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mknod);
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds /*
18571da177e4SLinus Torvalds  * See comments for nfs_proc_create regarding failed operations.
18581da177e4SLinus Torvalds  */
1859597d9289SBryan Schumaker int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
18601da177e4SLinus Torvalds {
18611da177e4SLinus Torvalds 	struct iattr attr;
18621da177e4SLinus Torvalds 	int error;
18631da177e4SLinus Torvalds 
18641e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n",
18656de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	attr.ia_valid = ATTR_MODE;
18681da177e4SLinus Torvalds 	attr.ia_mode = mode | S_IFDIR;
18691da177e4SLinus Torvalds 
18701ca42382STrond Myklebust 	trace_nfs_mkdir_enter(dir, dentry);
18711da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
18721ca42382STrond Myklebust 	trace_nfs_mkdir_exit(dir, dentry, error);
18731da177e4SLinus Torvalds 	if (error != 0)
18741da177e4SLinus Torvalds 		goto out_err;
18751da177e4SLinus Torvalds 	return 0;
18761da177e4SLinus Torvalds out_err:
18771da177e4SLinus Torvalds 	d_drop(dentry);
18781da177e4SLinus Torvalds 	return error;
18791da177e4SLinus Torvalds }
1880ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mkdir);
18811da177e4SLinus Torvalds 
1882d45b9d8bSTrond Myklebust static void nfs_dentry_handle_enoent(struct dentry *dentry)
1883d45b9d8bSTrond Myklebust {
1884dc3f4198SAl Viro 	if (simple_positive(dentry))
1885d45b9d8bSTrond Myklebust 		d_delete(dentry);
1886d45b9d8bSTrond Myklebust }
1887d45b9d8bSTrond Myklebust 
1888597d9289SBryan Schumaker int nfs_rmdir(struct inode *dir, struct dentry *dentry)
18891da177e4SLinus Torvalds {
18901da177e4SLinus Torvalds 	int error;
18911da177e4SLinus Torvalds 
18921e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n",
18936de1472fSAl Viro 			dir->i_sb->s_id, dir->i_ino, dentry);
18941da177e4SLinus Torvalds 
18951ca42382STrond Myklebust 	trace_nfs_rmdir_enter(dir, dentry);
18962b0143b5SDavid Howells 	if (d_really_is_positive(dentry)) {
1897884be175SAl Viro 		down_write(&NFS_I(d_inode(dentry))->rmdir_sem);
18981da177e4SLinus Torvalds 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
18991da177e4SLinus Torvalds 		/* Ensure the VFS deletes this inode */
1900ba6c0592STrond Myklebust 		switch (error) {
1901ba6c0592STrond Myklebust 		case 0:
19022b0143b5SDavid Howells 			clear_nlink(d_inode(dentry));
1903ba6c0592STrond Myklebust 			break;
1904ba6c0592STrond Myklebust 		case -ENOENT:
1905d45b9d8bSTrond Myklebust 			nfs_dentry_handle_enoent(dentry);
1906ba6c0592STrond Myklebust 		}
1907884be175SAl Viro 		up_write(&NFS_I(d_inode(dentry))->rmdir_sem);
1908ba6c0592STrond Myklebust 	} else
1909ba6c0592STrond Myklebust 		error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
19101ca42382STrond Myklebust 	trace_nfs_rmdir_exit(dir, dentry, error);
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 	return error;
19131da177e4SLinus Torvalds }
1914ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rmdir);
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds /*
19171da177e4SLinus Torvalds  * Remove a file after making sure there are no pending writes,
19181da177e4SLinus Torvalds  * and after checking that the file has only one user.
19191da177e4SLinus Torvalds  *
19201da177e4SLinus Torvalds  * We invalidate the attribute cache and free the inode prior to the operation
19211da177e4SLinus Torvalds  * to avoid possible races if the server reuses the inode.
19221da177e4SLinus Torvalds  */
19231da177e4SLinus Torvalds static int nfs_safe_remove(struct dentry *dentry)
19241da177e4SLinus Torvalds {
19252b0143b5SDavid Howells 	struct inode *dir = d_inode(dentry->d_parent);
19262b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
19271da177e4SLinus Torvalds 	int error = -EBUSY;
19281da177e4SLinus Torvalds 
19296de1472fSAl Viro 	dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry);
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	/* If the dentry was sillyrenamed, we simply call d_delete() */
19321da177e4SLinus Torvalds 	if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
19331da177e4SLinus Torvalds 		error = 0;
19341da177e4SLinus Torvalds 		goto out;
19351da177e4SLinus Torvalds 	}
19361da177e4SLinus Torvalds 
19371ca42382STrond Myklebust 	trace_nfs_remove_enter(dir, dentry);
19381da177e4SLinus Torvalds 	if (inode != NULL) {
1939912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
19401da177e4SLinus Torvalds 		if (error == 0)
19411b83d707STrond Myklebust 			nfs_drop_nlink(inode);
19421da177e4SLinus Torvalds 	} else
1943912678dbSTrond Myklebust 		error = NFS_PROTO(dir)->remove(dir, dentry);
1944d45b9d8bSTrond Myklebust 	if (error == -ENOENT)
1945d45b9d8bSTrond Myklebust 		nfs_dentry_handle_enoent(dentry);
19461ca42382STrond Myklebust 	trace_nfs_remove_exit(dir, dentry, error);
19471da177e4SLinus Torvalds out:
19481da177e4SLinus Torvalds 	return error;
19491da177e4SLinus Torvalds }
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds /*  We do silly rename. In case sillyrename() returns -EBUSY, the inode
19521da177e4SLinus Torvalds  *  belongs to an active ".nfs..." file and we return -EBUSY.
19531da177e4SLinus Torvalds  *
19541da177e4SLinus Torvalds  *  If sillyrename() returns 0, we do nothing, otherwise we unlink.
19551da177e4SLinus Torvalds  */
1956597d9289SBryan Schumaker int nfs_unlink(struct inode *dir, struct dentry *dentry)
19571da177e4SLinus Torvalds {
19581da177e4SLinus Torvalds 	int error;
19591da177e4SLinus Torvalds 	int need_rehash = 0;
19601da177e4SLinus Torvalds 
19611e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id,
19626de1472fSAl Viro 		dir->i_ino, dentry);
19631da177e4SLinus Torvalds 
19641ca42382STrond Myklebust 	trace_nfs_unlink_enter(dir, dentry);
19651da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
196684d08fa8SAl Viro 	if (d_count(dentry) > 1) {
19671da177e4SLinus Torvalds 		spin_unlock(&dentry->d_lock);
1968ccfeb506STrond Myklebust 		/* Start asynchronous writeout of the inode */
19692b0143b5SDavid Howells 		write_inode_now(d_inode(dentry), 0);
19701da177e4SLinus Torvalds 		error = nfs_sillyrename(dir, dentry);
19711ca42382STrond Myklebust 		goto out;
19721da177e4SLinus Torvalds 	}
19731da177e4SLinus Torvalds 	if (!d_unhashed(dentry)) {
19741da177e4SLinus Torvalds 		__d_drop(dentry);
19751da177e4SLinus Torvalds 		need_rehash = 1;
19761da177e4SLinus Torvalds 	}
19771da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
19781da177e4SLinus Torvalds 	error = nfs_safe_remove(dentry);
1979d45b9d8bSTrond Myklebust 	if (!error || error == -ENOENT) {
19801da177e4SLinus Torvalds 		nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
19811da177e4SLinus Torvalds 	} else if (need_rehash)
19821da177e4SLinus Torvalds 		d_rehash(dentry);
19831ca42382STrond Myklebust out:
19841ca42382STrond Myklebust 	trace_nfs_unlink_exit(dir, dentry, error);
19851da177e4SLinus Torvalds 	return error;
19861da177e4SLinus Torvalds }
1987ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_unlink);
19881da177e4SLinus Torvalds 
1989873101b3SChuck Lever /*
1990873101b3SChuck Lever  * To create a symbolic link, most file systems instantiate a new inode,
1991873101b3SChuck Lever  * add a page to it containing the path, then write it out to the disk
1992873101b3SChuck Lever  * using prepare_write/commit_write.
1993873101b3SChuck Lever  *
1994873101b3SChuck Lever  * Unfortunately the NFS client can't create the in-core inode first
1995873101b3SChuck Lever  * because it needs a file handle to create an in-core inode (see
1996873101b3SChuck Lever  * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
1997873101b3SChuck Lever  * symlink request has completed on the server.
1998873101b3SChuck Lever  *
1999873101b3SChuck Lever  * So instead we allocate a raw page, copy the symname into it, then do
2000873101b3SChuck Lever  * the SYMLINK request with the page as the buffer.  If it succeeds, we
2001873101b3SChuck Lever  * now have a new file handle and can instantiate an in-core NFS inode
2002873101b3SChuck Lever  * and move the raw page into its mapping.
2003873101b3SChuck Lever  */
2004597d9289SBryan Schumaker int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20051da177e4SLinus Torvalds {
2006873101b3SChuck Lever 	struct page *page;
2007873101b3SChuck Lever 	char *kaddr;
20081da177e4SLinus Torvalds 	struct iattr attr;
2009873101b3SChuck Lever 	unsigned int pathlen = strlen(symname);
20101da177e4SLinus Torvalds 	int error;
20111da177e4SLinus Torvalds 
20121e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id,
20136de1472fSAl Viro 		dir->i_ino, dentry, symname);
20141da177e4SLinus Torvalds 
2015873101b3SChuck Lever 	if (pathlen > PAGE_SIZE)
2016873101b3SChuck Lever 		return -ENAMETOOLONG;
20171da177e4SLinus Torvalds 
2018873101b3SChuck Lever 	attr.ia_mode = S_IFLNK | S_IRWXUGO;
2019873101b3SChuck Lever 	attr.ia_valid = ATTR_MODE;
20201da177e4SLinus Torvalds 
2021e8ecde25SAl Viro 	page = alloc_page(GFP_USER);
202276566991STrond Myklebust 	if (!page)
2023873101b3SChuck Lever 		return -ENOMEM;
2024873101b3SChuck Lever 
2025e8ecde25SAl Viro 	kaddr = page_address(page);
2026873101b3SChuck Lever 	memcpy(kaddr, symname, pathlen);
2027873101b3SChuck Lever 	if (pathlen < PAGE_SIZE)
2028873101b3SChuck Lever 		memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
2029873101b3SChuck Lever 
20301ca42382STrond Myklebust 	trace_nfs_symlink_enter(dir, dentry);
203194a6d753SChuck Lever 	error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
20321ca42382STrond Myklebust 	trace_nfs_symlink_exit(dir, dentry, error);
2033873101b3SChuck Lever 	if (error != 0) {
20341e8968c5SNiels de Vos 		dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n",
2035873101b3SChuck Lever 			dir->i_sb->s_id, dir->i_ino,
20366de1472fSAl Viro 			dentry, symname, error);
20371da177e4SLinus Torvalds 		d_drop(dentry);
2038873101b3SChuck Lever 		__free_page(page);
20391da177e4SLinus Torvalds 		return error;
20401da177e4SLinus Torvalds 	}
20411da177e4SLinus Torvalds 
2042873101b3SChuck Lever 	/*
2043873101b3SChuck Lever 	 * No big deal if we can't add this page to the page cache here.
2044873101b3SChuck Lever 	 * READLINK will get the missing page from the server if needed.
2045873101b3SChuck Lever 	 */
20462b0143b5SDavid Howells 	if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0,
2047873101b3SChuck Lever 							GFP_KERNEL)) {
2048873101b3SChuck Lever 		SetPageUptodate(page);
2049873101b3SChuck Lever 		unlock_page(page);
2050a0b54addSRafael Aquini 		/*
2051a0b54addSRafael Aquini 		 * add_to_page_cache_lru() grabs an extra page refcount.
2052a0b54addSRafael Aquini 		 * Drop it here to avoid leaking this page later.
2053a0b54addSRafael Aquini 		 */
205409cbfeafSKirill A. Shutemov 		put_page(page);
2055873101b3SChuck Lever 	} else
2056873101b3SChuck Lever 		__free_page(page);
2057873101b3SChuck Lever 
2058873101b3SChuck Lever 	return 0;
2059873101b3SChuck Lever }
2060ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_symlink);
2061873101b3SChuck Lever 
2062597d9289SBryan Schumaker int
20631da177e4SLinus Torvalds nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
20641da177e4SLinus Torvalds {
20652b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
20661da177e4SLinus Torvalds 	int error;
20671da177e4SLinus Torvalds 
20686de1472fSAl Viro 	dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n",
20696de1472fSAl Viro 		old_dentry, dentry);
20701da177e4SLinus Torvalds 
20711fd1085bSTrond Myklebust 	trace_nfs_link_enter(inode, dir, dentry);
20729697d234STrond Myklebust 	d_drop(dentry);
20731da177e4SLinus Torvalds 	error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
2074cf809556STrond Myklebust 	if (error == 0) {
20757de9c6eeSAl Viro 		ihold(inode);
20769697d234STrond Myklebust 		d_add(dentry, inode);
2077cf809556STrond Myklebust 	}
20781fd1085bSTrond Myklebust 	trace_nfs_link_exit(inode, dir, dentry, error);
20791da177e4SLinus Torvalds 	return error;
20801da177e4SLinus Torvalds }
2081ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_link);
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds /*
20841da177e4SLinus Torvalds  * RENAME
20851da177e4SLinus Torvalds  * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
20861da177e4SLinus Torvalds  * different file handle for the same inode after a rename (e.g. when
20871da177e4SLinus Torvalds  * moving to a different directory). A fail-safe method to do so would
20881da177e4SLinus Torvalds  * be to look up old_dir/old_name, create a link to new_dir/new_name and
20891da177e4SLinus Torvalds  * rename the old file using the sillyrename stuff. This way, the original
20901da177e4SLinus Torvalds  * file in old_dir will go away when the last process iput()s the inode.
20911da177e4SLinus Torvalds  *
20921da177e4SLinus Torvalds  * FIXED.
20931da177e4SLinus Torvalds  *
20941da177e4SLinus Torvalds  * It actually works quite well. One needs to have the possibility for
20951da177e4SLinus Torvalds  * at least one ".nfs..." file in each directory the file ever gets
20961da177e4SLinus Torvalds  * moved or linked to which happens automagically with the new
20971da177e4SLinus Torvalds  * implementation that only depends on the dcache stuff instead of
20981da177e4SLinus Torvalds  * using the inode layer
20991da177e4SLinus Torvalds  *
21001da177e4SLinus Torvalds  * Unfortunately, things are a little more complicated than indicated
21011da177e4SLinus Torvalds  * above. For a cross-directory move, we want to make sure we can get
21021da177e4SLinus Torvalds  * rid of the old inode after the operation.  This means there must be
21031da177e4SLinus Torvalds  * no pending writes (if it's a file), and the use count must be 1.
21041da177e4SLinus Torvalds  * If these conditions are met, we can drop the dentries before doing
21051da177e4SLinus Torvalds  * the rename.
21061da177e4SLinus Torvalds  */
2107597d9289SBryan Schumaker int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
21081cd66c93SMiklos Szeredi 	       struct inode *new_dir, struct dentry *new_dentry,
21091cd66c93SMiklos Szeredi 	       unsigned int flags)
21101da177e4SLinus Torvalds {
21112b0143b5SDavid Howells 	struct inode *old_inode = d_inode(old_dentry);
21122b0143b5SDavid Howells 	struct inode *new_inode = d_inode(new_dentry);
2113d9f29500SBenjamin Coddington 	struct dentry *dentry = NULL, *rehash = NULL;
211480a491fdSJeff Layton 	struct rpc_task *task;
21151da177e4SLinus Torvalds 	int error = -EBUSY;
21161da177e4SLinus Torvalds 
21171cd66c93SMiklos Szeredi 	if (flags)
21181cd66c93SMiklos Szeredi 		return -EINVAL;
21191cd66c93SMiklos Szeredi 
21206de1472fSAl Viro 	dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n",
21216de1472fSAl Viro 		 old_dentry, new_dentry,
212284d08fa8SAl Viro 		 d_count(new_dentry));
21231da177e4SLinus Torvalds 
212470ded201STrond Myklebust 	trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry);
21251da177e4SLinus Torvalds 	/*
212628f79a1aSMiklos Szeredi 	 * For non-directories, check whether the target is busy and if so,
212728f79a1aSMiklos Szeredi 	 * make a copy of the dentry and then do a silly-rename. If the
212828f79a1aSMiklos Szeredi 	 * silly-rename succeeds, the copied dentry is hashed and becomes
212928f79a1aSMiklos Szeredi 	 * the new target.
21301da177e4SLinus Torvalds 	 */
213127226104SMiklos Szeredi 	if (new_inode && !S_ISDIR(new_inode->i_mode)) {
213227226104SMiklos Szeredi 		/*
213327226104SMiklos Szeredi 		 * To prevent any new references to the target during the
213427226104SMiklos Szeredi 		 * rename, we unhash the dentry in advance.
213527226104SMiklos Szeredi 		 */
2136d9f29500SBenjamin Coddington 		if (!d_unhashed(new_dentry)) {
213727226104SMiklos Szeredi 			d_drop(new_dentry);
2138d9f29500SBenjamin Coddington 			rehash = new_dentry;
2139d9f29500SBenjamin Coddington 		}
214027226104SMiklos Szeredi 
214184d08fa8SAl Viro 		if (d_count(new_dentry) > 2) {
21421da177e4SLinus Torvalds 			int err;
214327226104SMiklos Szeredi 
21441da177e4SLinus Torvalds 			/* copy the target dentry's name */
21451da177e4SLinus Torvalds 			dentry = d_alloc(new_dentry->d_parent,
21461da177e4SLinus Torvalds 					 &new_dentry->d_name);
21471da177e4SLinus Torvalds 			if (!dentry)
21481da177e4SLinus Torvalds 				goto out;
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 			/* silly-rename the existing target ... */
21511da177e4SLinus Torvalds 			err = nfs_sillyrename(new_dir, new_dentry);
215224e93025SMiklos Szeredi 			if (err)
21531da177e4SLinus Torvalds 				goto out;
215424e93025SMiklos Szeredi 
215524e93025SMiklos Szeredi 			new_dentry = dentry;
2156d9f29500SBenjamin Coddington 			rehash = NULL;
215724e93025SMiklos Szeredi 			new_inode = NULL;
2158b1e4adf4STrond Myklebust 		}
215927226104SMiklos Szeredi 	}
21601da177e4SLinus Torvalds 
2161d9f29500SBenjamin Coddington 	task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL);
216280a491fdSJeff Layton 	if (IS_ERR(task)) {
216380a491fdSJeff Layton 		error = PTR_ERR(task);
216480a491fdSJeff Layton 		goto out;
216580a491fdSJeff Layton 	}
216680a491fdSJeff Layton 
216780a491fdSJeff Layton 	error = rpc_wait_for_completion_task(task);
2168818a8dbeSBenjamin Coddington 	if (error != 0) {
2169818a8dbeSBenjamin Coddington 		((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1;
2170818a8dbeSBenjamin Coddington 		/* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */
2171818a8dbeSBenjamin Coddington 		smp_wmb();
2172818a8dbeSBenjamin Coddington 	} else
217380a491fdSJeff Layton 		error = task->tk_status;
217480a491fdSJeff Layton 	rpc_put_task(task);
217559a707b0STrond Myklebust 	/* Ensure the inode attributes are revalidated */
217659a707b0STrond Myklebust 	if (error == 0) {
217759a707b0STrond Myklebust 		spin_lock(&old_inode->i_lock);
217859a707b0STrond Myklebust 		NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter();
217959a707b0STrond Myklebust 		NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE
218059a707b0STrond Myklebust 			| NFS_INO_INVALID_CTIME
218159a707b0STrond Myklebust 			| NFS_INO_REVAL_FORCED;
218259a707b0STrond Myklebust 		spin_unlock(&old_inode->i_lock);
218359a707b0STrond Myklebust 	}
21841da177e4SLinus Torvalds out:
2185d9f29500SBenjamin Coddington 	if (rehash)
2186d9f29500SBenjamin Coddington 		d_rehash(rehash);
218770ded201STrond Myklebust 	trace_nfs_rename_exit(old_dir, old_dentry,
218870ded201STrond Myklebust 			new_dir, new_dentry, error);
2189d9f29500SBenjamin Coddington 	if (!error) {
2190d9f29500SBenjamin Coddington 		if (new_inode != NULL)
2191d9f29500SBenjamin Coddington 			nfs_drop_nlink(new_inode);
2192d9f29500SBenjamin Coddington 		/*
2193d9f29500SBenjamin Coddington 		 * The d_move() should be here instead of in an async RPC completion
2194d9f29500SBenjamin Coddington 		 * handler because we need the proper locks to move the dentry.  If
2195d9f29500SBenjamin Coddington 		 * we're interrupted by a signal, the async RPC completion handler
2196d9f29500SBenjamin Coddington 		 * should mark the directories for revalidation.
2197d9f29500SBenjamin Coddington 		 */
2198d9f29500SBenjamin Coddington 		d_move(old_dentry, new_dentry);
2199d803224cSTrond Myklebust 		nfs_set_verifier(old_dentry,
2200d9f29500SBenjamin Coddington 					nfs_save_change_attribute(new_dir));
2201d9f29500SBenjamin Coddington 	} else if (error == -ENOENT)
2202d9f29500SBenjamin Coddington 		nfs_dentry_handle_enoent(old_dentry);
2203d9f29500SBenjamin Coddington 
22041da177e4SLinus Torvalds 	/* new dentry created? */
22051da177e4SLinus Torvalds 	if (dentry)
22061da177e4SLinus Torvalds 		dput(dentry);
22071da177e4SLinus Torvalds 	return error;
22081da177e4SLinus Torvalds }
2209ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_rename);
22101da177e4SLinus Torvalds 
2211cfcea3e8STrond Myklebust static DEFINE_SPINLOCK(nfs_access_lru_lock);
2212cfcea3e8STrond Myklebust static LIST_HEAD(nfs_access_lru_list);
2213cfcea3e8STrond Myklebust static atomic_long_t nfs_access_nr_entries;
2214cfcea3e8STrond Myklebust 
22153a505845STrond Myklebust static unsigned long nfs_access_max_cachesize = ULONG_MAX;
22163a505845STrond Myklebust module_param(nfs_access_max_cachesize, ulong, 0644);
22173a505845STrond Myklebust MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length");
22183a505845STrond Myklebust 
22191c3c07e9STrond Myklebust static void nfs_access_free_entry(struct nfs_access_entry *entry)
22201c3c07e9STrond Myklebust {
2221b68572e0SNeilBrown 	put_cred(entry->cred);
2222f682a398SNeilBrown 	kfree_rcu(entry, rcu_head);
22234e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2224cfcea3e8STrond Myklebust 	atomic_long_dec(&nfs_access_nr_entries);
22254e857c58SPeter Zijlstra 	smp_mb__after_atomic();
22261c3c07e9STrond Myklebust }
22271c3c07e9STrond Myklebust 
22281a81bb8aSTrond Myklebust static void nfs_access_free_list(struct list_head *head)
22291a81bb8aSTrond Myklebust {
22301a81bb8aSTrond Myklebust 	struct nfs_access_entry *cache;
22311a81bb8aSTrond Myklebust 
22321a81bb8aSTrond Myklebust 	while (!list_empty(head)) {
22331a81bb8aSTrond Myklebust 		cache = list_entry(head->next, struct nfs_access_entry, lru);
22341a81bb8aSTrond Myklebust 		list_del(&cache->lru);
22351a81bb8aSTrond Myklebust 		nfs_access_free_entry(cache);
22361a81bb8aSTrond Myklebust 	}
22371a81bb8aSTrond Myklebust }
22381a81bb8aSTrond Myklebust 
22393a505845STrond Myklebust static unsigned long
22403a505845STrond Myklebust nfs_do_access_cache_scan(unsigned int nr_to_scan)
2241979df72eSTrond Myklebust {
2242979df72eSTrond Myklebust 	LIST_HEAD(head);
2243aa510da5STrond Myklebust 	struct nfs_inode *nfsi, *next;
2244979df72eSTrond Myklebust 	struct nfs_access_entry *cache;
22451ab6c499SDave Chinner 	long freed = 0;
2246979df72eSTrond Myklebust 
2247a50f7951STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
2248aa510da5STrond Myklebust 	list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) {
2249979df72eSTrond Myklebust 		struct inode *inode;
2250979df72eSTrond Myklebust 
2251979df72eSTrond Myklebust 		if (nr_to_scan-- == 0)
2252979df72eSTrond Myklebust 			break;
22539c7e7e23STrond Myklebust 		inode = &nfsi->vfs_inode;
2254979df72eSTrond Myklebust 		spin_lock(&inode->i_lock);
2255979df72eSTrond Myklebust 		if (list_empty(&nfsi->access_cache_entry_lru))
2256979df72eSTrond Myklebust 			goto remove_lru_entry;
2257979df72eSTrond Myklebust 		cache = list_entry(nfsi->access_cache_entry_lru.next,
2258979df72eSTrond Myklebust 				struct nfs_access_entry, lru);
2259979df72eSTrond Myklebust 		list_move(&cache->lru, &head);
2260979df72eSTrond Myklebust 		rb_erase(&cache->rb_node, &nfsi->access_cache);
22611ab6c499SDave Chinner 		freed++;
2262979df72eSTrond Myklebust 		if (!list_empty(&nfsi->access_cache_entry_lru))
2263979df72eSTrond Myklebust 			list_move_tail(&nfsi->access_cache_inode_lru,
2264979df72eSTrond Myklebust 					&nfs_access_lru_list);
2265979df72eSTrond Myklebust 		else {
2266979df72eSTrond Myklebust remove_lru_entry:
2267979df72eSTrond Myklebust 			list_del_init(&nfsi->access_cache_inode_lru);
22684e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2269979df72eSTrond Myklebust 			clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
22704e857c58SPeter Zijlstra 			smp_mb__after_atomic();
2271979df72eSTrond Myklebust 		}
227259844a9bSTrond Myklebust 		spin_unlock(&inode->i_lock);
2273979df72eSTrond Myklebust 	}
2274979df72eSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
22751a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
22761ab6c499SDave Chinner 	return freed;
22771ab6c499SDave Chinner }
22781ab6c499SDave Chinner 
22791ab6c499SDave Chinner unsigned long
22803a505845STrond Myklebust nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc)
22813a505845STrond Myklebust {
22823a505845STrond Myklebust 	int nr_to_scan = sc->nr_to_scan;
22833a505845STrond Myklebust 	gfp_t gfp_mask = sc->gfp_mask;
22843a505845STrond Myklebust 
22853a505845STrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
22863a505845STrond Myklebust 		return SHRINK_STOP;
22873a505845STrond Myklebust 	return nfs_do_access_cache_scan(nr_to_scan);
22883a505845STrond Myklebust }
22893a505845STrond Myklebust 
22903a505845STrond Myklebust 
22913a505845STrond Myklebust unsigned long
22921ab6c499SDave Chinner nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc)
22931ab6c499SDave Chinner {
229455f841ceSGlauber Costa 	return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries));
2295979df72eSTrond Myklebust }
2296979df72eSTrond Myklebust 
22973a505845STrond Myklebust static void
22983a505845STrond Myklebust nfs_access_cache_enforce_limit(void)
22993a505845STrond Myklebust {
23003a505845STrond Myklebust 	long nr_entries = atomic_long_read(&nfs_access_nr_entries);
23013a505845STrond Myklebust 	unsigned long diff;
23023a505845STrond Myklebust 	unsigned int nr_to_scan;
23033a505845STrond Myklebust 
23043a505845STrond Myklebust 	if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize)
23053a505845STrond Myklebust 		return;
23063a505845STrond Myklebust 	nr_to_scan = 100;
23073a505845STrond Myklebust 	diff = nr_entries - nfs_access_max_cachesize;
23083a505845STrond Myklebust 	if (diff < nr_to_scan)
23093a505845STrond Myklebust 		nr_to_scan = diff;
23103a505845STrond Myklebust 	nfs_do_access_cache_scan(nr_to_scan);
23113a505845STrond Myklebust }
23123a505845STrond Myklebust 
23131a81bb8aSTrond Myklebust static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
23141c3c07e9STrond Myklebust {
23151c3c07e9STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
23161a81bb8aSTrond Myklebust 	struct rb_node *n;
23171c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
23181c3c07e9STrond Myklebust 
23191c3c07e9STrond Myklebust 	/* Unhook entries from the cache */
23201c3c07e9STrond Myklebust 	while ((n = rb_first(root_node)) != NULL) {
23211c3c07e9STrond Myklebust 		entry = rb_entry(n, struct nfs_access_entry, rb_node);
23221c3c07e9STrond Myklebust 		rb_erase(n, root_node);
23231a81bb8aSTrond Myklebust 		list_move(&entry->lru, head);
23241c3c07e9STrond Myklebust 	}
23251c3c07e9STrond Myklebust 	nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
23261c3c07e9STrond Myklebust }
23271c3c07e9STrond Myklebust 
23281c3c07e9STrond Myklebust void nfs_access_zap_cache(struct inode *inode)
23291c3c07e9STrond Myklebust {
23301a81bb8aSTrond Myklebust 	LIST_HEAD(head);
23311a81bb8aSTrond Myklebust 
23321a81bb8aSTrond Myklebust 	if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
23331a81bb8aSTrond Myklebust 		return;
2334cfcea3e8STrond Myklebust 	/* Remove from global LRU init */
2335cfcea3e8STrond Myklebust 	spin_lock(&nfs_access_lru_lock);
23361a81bb8aSTrond Myklebust 	if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
2337cfcea3e8STrond Myklebust 		list_del_init(&NFS_I(inode)->access_cache_inode_lru);
2338cfcea3e8STrond Myklebust 
23391c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
23401a81bb8aSTrond Myklebust 	__nfs_access_zap_cache(NFS_I(inode), &head);
23411a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
23421a81bb8aSTrond Myklebust 	spin_unlock(&nfs_access_lru_lock);
23431a81bb8aSTrond Myklebust 	nfs_access_free_list(&head);
23441c3c07e9STrond Myklebust }
23451c606fb7SBryan Schumaker EXPORT_SYMBOL_GPL(nfs_access_zap_cache);
23461c3c07e9STrond Myklebust 
2347b68572e0SNeilBrown static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred)
23481c3c07e9STrond Myklebust {
23491c3c07e9STrond Myklebust 	struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
23501c3c07e9STrond Myklebust 
23511c3c07e9STrond Myklebust 	while (n != NULL) {
2352b68572e0SNeilBrown 		struct nfs_access_entry *entry =
2353b68572e0SNeilBrown 			rb_entry(n, struct nfs_access_entry, rb_node);
2354b68572e0SNeilBrown 		int cmp = cred_fscmp(cred, entry->cred);
23551c3c07e9STrond Myklebust 
2356b68572e0SNeilBrown 		if (cmp < 0)
23571c3c07e9STrond Myklebust 			n = n->rb_left;
2358b68572e0SNeilBrown 		else if (cmp > 0)
23591c3c07e9STrond Myklebust 			n = n->rb_right;
23601c3c07e9STrond Myklebust 		else
23611c3c07e9STrond Myklebust 			return entry;
23621c3c07e9STrond Myklebust 	}
23631c3c07e9STrond Myklebust 	return NULL;
23641c3c07e9STrond Myklebust }
23651c3c07e9STrond Myklebust 
2366b68572e0SNeilBrown static int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block)
23671da177e4SLinus Torvalds {
236855296809SChuck Lever 	struct nfs_inode *nfsi = NFS_I(inode);
23691c3c07e9STrond Myklebust 	struct nfs_access_entry *cache;
237057b69181STrond Myklebust 	bool retry = true;
237157b69181STrond Myklebust 	int err;
23721da177e4SLinus Torvalds 
23731c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
237457b69181STrond Myklebust 	for(;;) {
23751c3c07e9STrond Myklebust 		if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
23761c3c07e9STrond Myklebust 			goto out_zap;
23771c3c07e9STrond Myklebust 		cache = nfs_access_search_rbtree(inode, cred);
237857b69181STrond Myklebust 		err = -ENOENT;
23791c3c07e9STrond Myklebust 		if (cache == NULL)
23801c3c07e9STrond Myklebust 			goto out;
238157b69181STrond Myklebust 		/* Found an entry, is our attribute cache valid? */
238221c3ba7eSTrond Myklebust 		if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
238357b69181STrond Myklebust 			break;
238457b69181STrond Myklebust 		err = -ECHILD;
238557b69181STrond Myklebust 		if (!may_block)
238657b69181STrond Myklebust 			goto out;
238757b69181STrond Myklebust 		if (!retry)
238857b69181STrond Myklebust 			goto out_zap;
238957b69181STrond Myklebust 		spin_unlock(&inode->i_lock);
239057b69181STrond Myklebust 		err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
239157b69181STrond Myklebust 		if (err)
239257b69181STrond Myklebust 			return err;
239357b69181STrond Myklebust 		spin_lock(&inode->i_lock);
239457b69181STrond Myklebust 		retry = false;
239557b69181STrond Myklebust 	}
23961c3c07e9STrond Myklebust 	res->cred = cache->cred;
23971c3c07e9STrond Myklebust 	res->mask = cache->mask;
2398cfcea3e8STrond Myklebust 	list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
23991c3c07e9STrond Myklebust 	err = 0;
24001c3c07e9STrond Myklebust out:
24011c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
24021c3c07e9STrond Myklebust 	return err;
24031c3c07e9STrond Myklebust out_zap:
24041a81bb8aSTrond Myklebust 	spin_unlock(&inode->i_lock);
24051a81bb8aSTrond Myklebust 	nfs_access_zap_cache(inode);
24061c3c07e9STrond Myklebust 	return -ENOENT;
24071c3c07e9STrond Myklebust }
24081c3c07e9STrond Myklebust 
2409b68572e0SNeilBrown static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res)
2410f682a398SNeilBrown {
2411f682a398SNeilBrown 	/* Only check the most recently returned cache entry,
2412f682a398SNeilBrown 	 * but do it without locking.
2413f682a398SNeilBrown 	 */
2414f682a398SNeilBrown 	struct nfs_inode *nfsi = NFS_I(inode);
2415f682a398SNeilBrown 	struct nfs_access_entry *cache;
2416f682a398SNeilBrown 	int err = -ECHILD;
2417f682a398SNeilBrown 	struct list_head *lh;
2418f682a398SNeilBrown 
2419f682a398SNeilBrown 	rcu_read_lock();
2420f682a398SNeilBrown 	if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
2421f682a398SNeilBrown 		goto out;
2422f682a398SNeilBrown 	lh = rcu_dereference(nfsi->access_cache_entry_lru.prev);
2423f682a398SNeilBrown 	cache = list_entry(lh, struct nfs_access_entry, lru);
2424f682a398SNeilBrown 	if (lh == &nfsi->access_cache_entry_lru ||
2425f682a398SNeilBrown 	    cred != cache->cred)
2426f682a398SNeilBrown 		cache = NULL;
2427f682a398SNeilBrown 	if (cache == NULL)
2428f682a398SNeilBrown 		goto out;
242921c3ba7eSTrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS))
2430f682a398SNeilBrown 		goto out;
2431f682a398SNeilBrown 	res->cred = cache->cred;
2432f682a398SNeilBrown 	res->mask = cache->mask;
243321c3ba7eSTrond Myklebust 	err = 0;
2434f682a398SNeilBrown out:
2435f682a398SNeilBrown 	rcu_read_unlock();
2436f682a398SNeilBrown 	return err;
2437f682a398SNeilBrown }
2438f682a398SNeilBrown 
24391c3c07e9STrond Myklebust static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
24401c3c07e9STrond Myklebust {
2441cfcea3e8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
2442cfcea3e8STrond Myklebust 	struct rb_root *root_node = &nfsi->access_cache;
24431c3c07e9STrond Myklebust 	struct rb_node **p = &root_node->rb_node;
24441c3c07e9STrond Myklebust 	struct rb_node *parent = NULL;
24451c3c07e9STrond Myklebust 	struct nfs_access_entry *entry;
2446b68572e0SNeilBrown 	int cmp;
24471c3c07e9STrond Myklebust 
24481c3c07e9STrond Myklebust 	spin_lock(&inode->i_lock);
24491c3c07e9STrond Myklebust 	while (*p != NULL) {
24501c3c07e9STrond Myklebust 		parent = *p;
24511c3c07e9STrond Myklebust 		entry = rb_entry(parent, struct nfs_access_entry, rb_node);
2452b68572e0SNeilBrown 		cmp = cred_fscmp(set->cred, entry->cred);
24531c3c07e9STrond Myklebust 
2454b68572e0SNeilBrown 		if (cmp < 0)
24551c3c07e9STrond Myklebust 			p = &parent->rb_left;
2456b68572e0SNeilBrown 		else if (cmp > 0)
24571c3c07e9STrond Myklebust 			p = &parent->rb_right;
24581c3c07e9STrond Myklebust 		else
24591c3c07e9STrond Myklebust 			goto found;
24601c3c07e9STrond Myklebust 	}
24611c3c07e9STrond Myklebust 	rb_link_node(&set->rb_node, parent, p);
24621c3c07e9STrond Myklebust 	rb_insert_color(&set->rb_node, root_node);
2463cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
24641c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
24651c3c07e9STrond Myklebust 	return;
24661c3c07e9STrond Myklebust found:
24671c3c07e9STrond Myklebust 	rb_replace_node(parent, &set->rb_node, root_node);
2468cfcea3e8STrond Myklebust 	list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
2469cfcea3e8STrond Myklebust 	list_del(&entry->lru);
24701c3c07e9STrond Myklebust 	spin_unlock(&inode->i_lock);
24711c3c07e9STrond Myklebust 	nfs_access_free_entry(entry);
24721da177e4SLinus Torvalds }
24731da177e4SLinus Torvalds 
24746168f62cSWeston Andros Adamson void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
24751da177e4SLinus Torvalds {
24761c3c07e9STrond Myklebust 	struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
24771c3c07e9STrond Myklebust 	if (cache == NULL)
24781c3c07e9STrond Myklebust 		return;
24791c3c07e9STrond Myklebust 	RB_CLEAR_NODE(&cache->rb_node);
2480b68572e0SNeilBrown 	cache->cred = get_cred(set->cred);
24811da177e4SLinus Torvalds 	cache->mask = set->mask;
24821c3c07e9STrond Myklebust 
2483f682a398SNeilBrown 	/* The above field assignments must be visible
2484f682a398SNeilBrown 	 * before this item appears on the lru.  We cannot easily
2485f682a398SNeilBrown 	 * use rcu_assign_pointer, so just force the memory barrier.
2486f682a398SNeilBrown 	 */
2487f682a398SNeilBrown 	smp_wmb();
24881c3c07e9STrond Myklebust 	nfs_access_add_rbtree(inode, cache);
2489cfcea3e8STrond Myklebust 
2490cfcea3e8STrond Myklebust 	/* Update accounting */
24914e857c58SPeter Zijlstra 	smp_mb__before_atomic();
2492cfcea3e8STrond Myklebust 	atomic_long_inc(&nfs_access_nr_entries);
24934e857c58SPeter Zijlstra 	smp_mb__after_atomic();
2494cfcea3e8STrond Myklebust 
2495cfcea3e8STrond Myklebust 	/* Add inode to global LRU list */
24961a81bb8aSTrond Myklebust 	if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
2497cfcea3e8STrond Myklebust 		spin_lock(&nfs_access_lru_lock);
24981a81bb8aSTrond Myklebust 		if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
24991a81bb8aSTrond Myklebust 			list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
25001a81bb8aSTrond Myklebust 					&nfs_access_lru_list);
2501cfcea3e8STrond Myklebust 		spin_unlock(&nfs_access_lru_lock);
2502cfcea3e8STrond Myklebust 	}
25033a505845STrond Myklebust 	nfs_access_cache_enforce_limit();
25041da177e4SLinus Torvalds }
25056168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_add_cache);
25066168f62cSWeston Andros Adamson 
25073c181827SAnna Schumaker #define NFS_MAY_READ (NFS_ACCESS_READ)
25083c181827SAnna Schumaker #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \
25093c181827SAnna Schumaker 		NFS_ACCESS_EXTEND | \
25103c181827SAnna Schumaker 		NFS_ACCESS_DELETE)
25113c181827SAnna Schumaker #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \
25123c181827SAnna Schumaker 		NFS_ACCESS_EXTEND)
2513ecbb903cSTrond Myklebust #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE
25143c181827SAnna Schumaker #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP)
25153c181827SAnna Schumaker #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE)
251615d4b73aSTrond Myklebust static int
2517ecbb903cSTrond Myklebust nfs_access_calc_mask(u32 access_result, umode_t umode)
251815d4b73aSTrond Myklebust {
251915d4b73aSTrond Myklebust 	int mask = 0;
252015d4b73aSTrond Myklebust 
252115d4b73aSTrond Myklebust 	if (access_result & NFS_MAY_READ)
252215d4b73aSTrond Myklebust 		mask |= MAY_READ;
2523ecbb903cSTrond Myklebust 	if (S_ISDIR(umode)) {
2524ecbb903cSTrond Myklebust 		if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE)
252515d4b73aSTrond Myklebust 			mask |= MAY_WRITE;
2526ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP)
252715d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2528ecbb903cSTrond Myklebust 	} else if (S_ISREG(umode)) {
2529ecbb903cSTrond Myklebust 		if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE)
2530ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
2531ecbb903cSTrond Myklebust 		if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE)
253215d4b73aSTrond Myklebust 			mask |= MAY_EXEC;
2533ecbb903cSTrond Myklebust 	} else if (access_result & NFS_MAY_WRITE)
2534ecbb903cSTrond Myklebust 			mask |= MAY_WRITE;
253515d4b73aSTrond Myklebust 	return mask;
253615d4b73aSTrond Myklebust }
253715d4b73aSTrond Myklebust 
25386168f62cSWeston Andros Adamson void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result)
25396168f62cSWeston Andros Adamson {
2540bd8b2441STrond Myklebust 	entry->mask = access_result;
25416168f62cSWeston Andros Adamson }
25426168f62cSWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_access_set_mask);
25431da177e4SLinus Torvalds 
2544b68572e0SNeilBrown static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask)
25451da177e4SLinus Torvalds {
25461da177e4SLinus Torvalds 	struct nfs_access_entry cache;
254757b69181STrond Myklebust 	bool may_block = (mask & MAY_NOT_BLOCK) == 0;
2548bd8b2441STrond Myklebust 	int cache_mask;
25491da177e4SLinus Torvalds 	int status;
25501da177e4SLinus Torvalds 
2551f4ce1299STrond Myklebust 	trace_nfs_access_enter(inode);
2552f4ce1299STrond Myklebust 
2553f682a398SNeilBrown 	status = nfs_access_get_cached_rcu(inode, cred, &cache);
2554f682a398SNeilBrown 	if (status != 0)
255557b69181STrond Myklebust 		status = nfs_access_get_cached(inode, cred, &cache, may_block);
25561da177e4SLinus Torvalds 	if (status == 0)
2557f4ce1299STrond Myklebust 		goto out_cached;
25581da177e4SLinus Torvalds 
2559f3324a2aSNeilBrown 	status = -ECHILD;
256057b69181STrond Myklebust 	if (!may_block)
2561f3324a2aSNeilBrown 		goto out;
2562f3324a2aSNeilBrown 
25631750d929SAnna Schumaker 	/*
25641750d929SAnna Schumaker 	 * Determine which access bits we want to ask for...
25651750d929SAnna Schumaker 	 */
25661750d929SAnna Schumaker 	cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
25671750d929SAnna Schumaker 	if (S_ISDIR(inode->i_mode))
25681750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
25691750d929SAnna Schumaker 	else
25701750d929SAnna Schumaker 		cache.mask |= NFS_ACCESS_EXECUTE;
25711da177e4SLinus Torvalds 	cache.cred = cred;
25721da177e4SLinus Torvalds 	status = NFS_PROTO(inode)->access(inode, &cache);
2573a71ee337SSuresh Jayaraman 	if (status != 0) {
2574a71ee337SSuresh Jayaraman 		if (status == -ESTALE) {
2575a71ee337SSuresh Jayaraman 			nfs_zap_caches(inode);
2576a71ee337SSuresh Jayaraman 			if (!S_ISDIR(inode->i_mode))
2577a71ee337SSuresh Jayaraman 				set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
2578a71ee337SSuresh Jayaraman 		}
2579f4ce1299STrond Myklebust 		goto out;
2580a71ee337SSuresh Jayaraman 	}
25811da177e4SLinus Torvalds 	nfs_access_add_cache(inode, &cache);
2582f4ce1299STrond Myklebust out_cached:
2583ecbb903cSTrond Myklebust 	cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode);
2584bd8b2441STrond Myklebust 	if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0)
2585f4ce1299STrond Myklebust 		status = -EACCES;
25861da177e4SLinus Torvalds out:
2587f4ce1299STrond Myklebust 	trace_nfs_access_exit(inode, status);
2588f4ce1299STrond Myklebust 	return status;
25891da177e4SLinus Torvalds }
25901da177e4SLinus Torvalds 
2591af22f94aSTrond Myklebust static int nfs_open_permission_mask(int openflags)
2592af22f94aSTrond Myklebust {
2593af22f94aSTrond Myklebust 	int mask = 0;
2594af22f94aSTrond Myklebust 
2595f8d9a897SWeston Andros Adamson 	if (openflags & __FMODE_EXEC) {
2596f8d9a897SWeston Andros Adamson 		/* ONLY check exec rights */
2597f8d9a897SWeston Andros Adamson 		mask = MAY_EXEC;
2598f8d9a897SWeston Andros Adamson 	} else {
25998a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_WRONLY)
2600af22f94aSTrond Myklebust 			mask |= MAY_READ;
26018a5e929dSAl Viro 		if ((openflags & O_ACCMODE) != O_RDONLY)
2602af22f94aSTrond Myklebust 			mask |= MAY_WRITE;
2603f8d9a897SWeston Andros Adamson 	}
2604f8d9a897SWeston Andros Adamson 
2605af22f94aSTrond Myklebust 	return mask;
2606af22f94aSTrond Myklebust }
2607af22f94aSTrond Myklebust 
2608b68572e0SNeilBrown int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags)
2609af22f94aSTrond Myklebust {
2610af22f94aSTrond Myklebust 	return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
2611af22f94aSTrond Myklebust }
261289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_may_open);
2613af22f94aSTrond Myklebust 
26145c5fc09aSTrond Myklebust static int nfs_execute_ok(struct inode *inode, int mask)
26155c5fc09aSTrond Myklebust {
26165c5fc09aSTrond Myklebust 	struct nfs_server *server = NFS_SERVER(inode);
261721c3ba7eSTrond Myklebust 	int ret = 0;
26185c5fc09aSTrond Myklebust 
26193825827eSTrond Myklebust 	if (S_ISDIR(inode->i_mode))
26203825827eSTrond Myklebust 		return 0;
2621cf834027STrond Myklebust 	if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) {
26225c5fc09aSTrond Myklebust 		if (mask & MAY_NOT_BLOCK)
262321c3ba7eSTrond Myklebust 			return -ECHILD;
262421c3ba7eSTrond Myklebust 		ret = __nfs_revalidate_inode(server, inode);
262521c3ba7eSTrond Myklebust 	}
26265c5fc09aSTrond Myklebust 	if (ret == 0 && !execute_ok(inode))
26275c5fc09aSTrond Myklebust 		ret = -EACCES;
26285c5fc09aSTrond Myklebust 	return ret;
26295c5fc09aSTrond Myklebust }
26305c5fc09aSTrond Myklebust 
263110556cb2SAl Viro int nfs_permission(struct inode *inode, int mask)
26321da177e4SLinus Torvalds {
2633b68572e0SNeilBrown 	const struct cred *cred = current_cred();
26341da177e4SLinus Torvalds 	int res = 0;
26351da177e4SLinus Torvalds 
263691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSACCESS);
263791d5b470SChuck Lever 
2638e6305c43SAl Viro 	if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
26391da177e4SLinus Torvalds 		goto out;
26401da177e4SLinus Torvalds 	/* Is this sys_access() ? */
26419cfcac81SEric Paris 	if (mask & (MAY_ACCESS | MAY_CHDIR))
26421da177e4SLinus Torvalds 		goto force_lookup;
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds 	switch (inode->i_mode & S_IFMT) {
26451da177e4SLinus Torvalds 		case S_IFLNK:
26461da177e4SLinus Torvalds 			goto out;
26471da177e4SLinus Torvalds 		case S_IFREG:
2648762674f8STrond Myklebust 			if ((mask & MAY_OPEN) &&
2649762674f8STrond Myklebust 			   nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN))
2650762674f8STrond Myklebust 				return 0;
26511da177e4SLinus Torvalds 			break;
26521da177e4SLinus Torvalds 		case S_IFDIR:
26531da177e4SLinus Torvalds 			/*
26541da177e4SLinus Torvalds 			 * Optimize away all write operations, since the server
26551da177e4SLinus Torvalds 			 * will check permissions when we perform the op.
26561da177e4SLinus Torvalds 			 */
26571da177e4SLinus Torvalds 			if ((mask & MAY_WRITE) && !(mask & MAY_READ))
26581da177e4SLinus Torvalds 				goto out;
26591da177e4SLinus Torvalds 	}
26601da177e4SLinus Torvalds 
26611da177e4SLinus Torvalds force_lookup:
26621da177e4SLinus Torvalds 	if (!NFS_PROTO(inode)->access)
26631da177e4SLinus Torvalds 		goto out_notsup;
26641da177e4SLinus Torvalds 
2665f3324a2aSNeilBrown 	/* Always try fast lookups first */
2666f3324a2aSNeilBrown 	rcu_read_lock();
2667f3324a2aSNeilBrown 	res = nfs_do_access(inode, cred, mask|MAY_NOT_BLOCK);
2668f3324a2aSNeilBrown 	rcu_read_unlock();
2669f3324a2aSNeilBrown 	if (res == -ECHILD && !(mask & MAY_NOT_BLOCK)) {
2670f3324a2aSNeilBrown 		/* Fast lookup failed, try the slow way */
26711da177e4SLinus Torvalds 		res = nfs_do_access(inode, cred, mask);
2672f3324a2aSNeilBrown 	}
26731da177e4SLinus Torvalds out:
26745c5fc09aSTrond Myklebust 	if (!res && (mask & MAY_EXEC))
26755c5fc09aSTrond Myklebust 		res = nfs_execute_ok(inode, mask);
2676f696a365SMiklos Szeredi 
26771e8968c5SNiels de Vos 	dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n",
26781e7cb3dcSChuck Lever 		inode->i_sb->s_id, inode->i_ino, mask, res);
26791da177e4SLinus Torvalds 	return res;
26801da177e4SLinus Torvalds out_notsup:
2681d51ac1a8SNeilBrown 	if (mask & MAY_NOT_BLOCK)
2682d51ac1a8SNeilBrown 		return -ECHILD;
2683d51ac1a8SNeilBrown 
26841da177e4SLinus Torvalds 	res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
26851da177e4SLinus Torvalds 	if (res == 0)
26862830ba7fSAl Viro 		res = generic_permission(inode, mask);
26871e7cb3dcSChuck Lever 	goto out;
26881da177e4SLinus Torvalds }
2689ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_permission);
26901da177e4SLinus Torvalds 
26911da177e4SLinus Torvalds /*
26921da177e4SLinus Torvalds  * Local variables:
26931da177e4SLinus Torvalds  *  version-control: t
26941da177e4SLinus Torvalds  *  kept-new-versions: 5
26951da177e4SLinus Torvalds  * End:
26961da177e4SLinus Torvalds  */
2697