xref: /openbmc/linux/fs/nfsd/filecache.c (revision 70f62231cdfd52357836733dd31db787e0412ab2)
13f054211SChuck Lever // SPDX-License-Identifier: GPL-2.0
265294c1fSJeff Layton /*
33f054211SChuck Lever  * The NFSD open file cache.
465294c1fSJeff Layton  *
565294c1fSJeff Layton  * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com>
6b3276c1fSChuck Lever  *
7b3276c1fSChuck Lever  * An nfsd_file object is a per-file collection of open state that binds
8b3276c1fSChuck Lever  * together:
9b3276c1fSChuck Lever  *   - a struct file *
10b3276c1fSChuck Lever  *   - a user credential
11b3276c1fSChuck Lever  *   - a network namespace
12b3276c1fSChuck Lever  *   - a read-ahead context
13b3276c1fSChuck Lever  *   - monitoring for writeback errors
14b3276c1fSChuck Lever  *
15b3276c1fSChuck Lever  * nfsd_file objects are reference-counted. Consumers acquire a new
16b3276c1fSChuck Lever  * object via the nfsd_file_acquire API. They manage their interest in
17b3276c1fSChuck Lever  * the acquired object, and hence the object's reference count, via
18b3276c1fSChuck Lever  * nfsd_file_get and nfsd_file_put. There are two varieties of nfsd_file
19b3276c1fSChuck Lever  * object:
20b3276c1fSChuck Lever  *
21b3276c1fSChuck Lever  *  * non-garbage-collected: When a consumer wants to precisely control
22b3276c1fSChuck Lever  *    the lifetime of a file's open state, it acquires a non-garbage-
23b3276c1fSChuck Lever  *    collected nfsd_file. The final nfsd_file_put releases the open
24b3276c1fSChuck Lever  *    state immediately.
25b3276c1fSChuck Lever  *
26b3276c1fSChuck Lever  *  * garbage-collected: When a consumer does not control the lifetime
27b3276c1fSChuck Lever  *    of open state, it acquires a garbage-collected nfsd_file. The
28b3276c1fSChuck Lever  *    final nfsd_file_put allows the open state to linger for a period
29b3276c1fSChuck Lever  *    during which it may be re-used.
3065294c1fSJeff Layton  */
3165294c1fSJeff Layton 
3265294c1fSJeff Layton #include <linux/hash.h>
3365294c1fSJeff Layton #include <linux/slab.h>
3465294c1fSJeff Layton #include <linux/file.h>
35cbcc268bSMatthew Wilcox (Oracle) #include <linux/pagemap.h>
3665294c1fSJeff Layton #include <linux/sched.h>
3765294c1fSJeff Layton #include <linux/list_lru.h>
3865294c1fSJeff Layton #include <linux/fsnotify_backend.h>
3965294c1fSJeff Layton #include <linux/fsnotify.h>
4065294c1fSJeff Layton #include <linux/seq_file.h>
41fc22945eSChuck Lever #include <linux/rhashtable.h>
4265294c1fSJeff Layton 
4365294c1fSJeff Layton #include "vfs.h"
4465294c1fSJeff Layton #include "nfsd.h"
4565294c1fSJeff Layton #include "nfsfh.h"
465e113224STrond Myklebust #include "netns.h"
4765294c1fSJeff Layton #include "filecache.h"
4865294c1fSJeff Layton #include "trace.h"
4965294c1fSJeff Layton 
5065294c1fSJeff Layton #define NFSD_LAUNDRETTE_DELAY		     (2 * HZ)
5165294c1fSJeff Layton 
52c7b824c3SChuck Lever #define NFSD_FILE_CACHE_UP		     (0)
5365294c1fSJeff Layton 
5465294c1fSJeff Layton /* We only care about NFSD_MAY_READ/WRITE for this cache */
5565294c1fSJeff Layton #define NFSD_FILE_MAY_MASK	(NFSD_MAY_READ|NFSD_MAY_WRITE)
5665294c1fSJeff Layton 
5765294c1fSJeff Layton static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
5829d4bdbbSChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions);
59d6329327SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_releases);
60904940e9SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age);
6194660cc1SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions);
6265294c1fSJeff Layton 
639542e6a6STrond Myklebust struct nfsd_fcache_disposal {
649542e6a6STrond Myklebust 	struct work_struct work;
659542e6a6STrond Myklebust 	spinlock_t lock;
669542e6a6STrond Myklebust 	struct list_head freeme;
679542e6a6STrond Myklebust };
689542e6a6STrond Myklebust 
6950d0def9SChen Zhou static struct workqueue_struct *nfsd_filecache_wq __read_mostly;
709542e6a6STrond Myklebust 
7165294c1fSJeff Layton static struct kmem_cache		*nfsd_file_slab;
7265294c1fSJeff Layton static struct kmem_cache		*nfsd_file_mark_slab;
7365294c1fSJeff Layton static struct list_lru			nfsd_file_lru;
74c7b824c3SChuck Lever static unsigned long			nfsd_file_flags;
7565294c1fSJeff Layton static struct fsnotify_group		*nfsd_file_fsnotify_group;
7665294c1fSJeff Layton static struct delayed_work		nfsd_filecache_laundrette;
77fc22945eSChuck Lever static struct rhashtable		nfsd_file_rhash_tbl
78fc22945eSChuck Lever 						____cacheline_aligned_in_smp;
7965294c1fSJeff Layton 
80fc22945eSChuck Lever enum nfsd_file_lookup_type {
81fc22945eSChuck Lever 	NFSD_FILE_KEY_INODE,
82fc22945eSChuck Lever 	NFSD_FILE_KEY_FULL,
83fc22945eSChuck Lever };
84fc22945eSChuck Lever 
85fc22945eSChuck Lever struct nfsd_file_lookup_key {
86fc22945eSChuck Lever 	struct inode			*inode;
87fc22945eSChuck Lever 	struct net			*net;
88fc22945eSChuck Lever 	const struct cred		*cred;
89fc22945eSChuck Lever 	unsigned char			need;
904d1ea845SChuck Lever 	bool				gc;
91fc22945eSChuck Lever 	enum nfsd_file_lookup_type	type;
92fc22945eSChuck Lever };
93fc22945eSChuck Lever 
94fc22945eSChuck Lever /*
95fc22945eSChuck Lever  * The returned hash value is based solely on the address of an in-code
96fc22945eSChuck Lever  * inode, a pointer to a slab-allocated object. The entropy in such a
97fc22945eSChuck Lever  * pointer is concentrated in its middle bits.
98fc22945eSChuck Lever  */
99fc22945eSChuck Lever static u32 nfsd_file_inode_hash(const struct inode *inode, u32 seed)
100fc22945eSChuck Lever {
101fc22945eSChuck Lever 	unsigned long ptr = (unsigned long)inode;
102fc22945eSChuck Lever 	u32 k;
103fc22945eSChuck Lever 
104fc22945eSChuck Lever 	k = ptr >> L1_CACHE_SHIFT;
105fc22945eSChuck Lever 	k &= 0x00ffffff;
106fc22945eSChuck Lever 	return jhash2(&k, 1, seed);
107fc22945eSChuck Lever }
108fc22945eSChuck Lever 
109fc22945eSChuck Lever /**
110fc22945eSChuck Lever  * nfsd_file_key_hashfn - Compute the hash value of a lookup key
111fc22945eSChuck Lever  * @data: key on which to compute the hash value
112fc22945eSChuck Lever  * @len: rhash table's key_len parameter (unused)
113fc22945eSChuck Lever  * @seed: rhash table's random seed of the day
114fc22945eSChuck Lever  *
115fc22945eSChuck Lever  * Return value:
116fc22945eSChuck Lever  *   Computed 32-bit hash value
117fc22945eSChuck Lever  */
118fc22945eSChuck Lever static u32 nfsd_file_key_hashfn(const void *data, u32 len, u32 seed)
119fc22945eSChuck Lever {
120fc22945eSChuck Lever 	const struct nfsd_file_lookup_key *key = data;
121fc22945eSChuck Lever 
122fc22945eSChuck Lever 	return nfsd_file_inode_hash(key->inode, seed);
123fc22945eSChuck Lever }
124fc22945eSChuck Lever 
125fc22945eSChuck Lever /**
126fc22945eSChuck Lever  * nfsd_file_obj_hashfn - Compute the hash value of an nfsd_file
127fc22945eSChuck Lever  * @data: object on which to compute the hash value
128fc22945eSChuck Lever  * @len: rhash table's key_len parameter (unused)
129fc22945eSChuck Lever  * @seed: rhash table's random seed of the day
130fc22945eSChuck Lever  *
131fc22945eSChuck Lever  * Return value:
132fc22945eSChuck Lever  *   Computed 32-bit hash value
133fc22945eSChuck Lever  */
134fc22945eSChuck Lever static u32 nfsd_file_obj_hashfn(const void *data, u32 len, u32 seed)
135fc22945eSChuck Lever {
136fc22945eSChuck Lever 	const struct nfsd_file *nf = data;
137fc22945eSChuck Lever 
138fc22945eSChuck Lever 	return nfsd_file_inode_hash(nf->nf_inode, seed);
139fc22945eSChuck Lever }
140fc22945eSChuck Lever 
141fc22945eSChuck Lever static bool
142fc22945eSChuck Lever nfsd_match_cred(const struct cred *c1, const struct cred *c2)
143fc22945eSChuck Lever {
144fc22945eSChuck Lever 	int i;
145fc22945eSChuck Lever 
146fc22945eSChuck Lever 	if (!uid_eq(c1->fsuid, c2->fsuid))
147fc22945eSChuck Lever 		return false;
148fc22945eSChuck Lever 	if (!gid_eq(c1->fsgid, c2->fsgid))
149fc22945eSChuck Lever 		return false;
150fc22945eSChuck Lever 	if (c1->group_info == NULL || c2->group_info == NULL)
151fc22945eSChuck Lever 		return c1->group_info == c2->group_info;
152fc22945eSChuck Lever 	if (c1->group_info->ngroups != c2->group_info->ngroups)
153fc22945eSChuck Lever 		return false;
154fc22945eSChuck Lever 	for (i = 0; i < c1->group_info->ngroups; i++) {
155fc22945eSChuck Lever 		if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
156fc22945eSChuck Lever 			return false;
157fc22945eSChuck Lever 	}
158fc22945eSChuck Lever 	return true;
159fc22945eSChuck Lever }
160fc22945eSChuck Lever 
161fc22945eSChuck Lever /**
162fc22945eSChuck Lever  * nfsd_file_obj_cmpfn - Match a cache item against search criteria
163fc22945eSChuck Lever  * @arg: search criteria
164fc22945eSChuck Lever  * @ptr: cache item to check
165fc22945eSChuck Lever  *
166fc22945eSChuck Lever  * Return values:
167fc22945eSChuck Lever  *   %0 - Item matches search criteria
168fc22945eSChuck Lever  *   %1 - Item does not match search criteria
169fc22945eSChuck Lever  */
170fc22945eSChuck Lever static int nfsd_file_obj_cmpfn(struct rhashtable_compare_arg *arg,
171fc22945eSChuck Lever 			       const void *ptr)
172fc22945eSChuck Lever {
173fc22945eSChuck Lever 	const struct nfsd_file_lookup_key *key = arg->key;
174fc22945eSChuck Lever 	const struct nfsd_file *nf = ptr;
175fc22945eSChuck Lever 
176fc22945eSChuck Lever 	switch (key->type) {
177fc22945eSChuck Lever 	case NFSD_FILE_KEY_INODE:
178fc22945eSChuck Lever 		if (nf->nf_inode != key->inode)
179fc22945eSChuck Lever 			return 1;
180fc22945eSChuck Lever 		break;
181fc22945eSChuck Lever 	case NFSD_FILE_KEY_FULL:
182fc22945eSChuck Lever 		if (nf->nf_inode != key->inode)
183fc22945eSChuck Lever 			return 1;
184fc22945eSChuck Lever 		if (nf->nf_may != key->need)
185fc22945eSChuck Lever 			return 1;
186fc22945eSChuck Lever 		if (nf->nf_net != key->net)
187fc22945eSChuck Lever 			return 1;
188fc22945eSChuck Lever 		if (!nfsd_match_cred(nf->nf_cred, key->cred))
189fc22945eSChuck Lever 			return 1;
1904d1ea845SChuck Lever 		if (!!test_bit(NFSD_FILE_GC, &nf->nf_flags) != key->gc)
1914d1ea845SChuck Lever 			return 1;
192fc22945eSChuck Lever 		if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0)
193fc22945eSChuck Lever 			return 1;
194fc22945eSChuck Lever 		break;
195fc22945eSChuck Lever 	}
196fc22945eSChuck Lever 	return 0;
197fc22945eSChuck Lever }
198fc22945eSChuck Lever 
199fc22945eSChuck Lever static const struct rhashtable_params nfsd_file_rhash_params = {
200fc22945eSChuck Lever 	.key_len		= sizeof_field(struct nfsd_file, nf_inode),
201fc22945eSChuck Lever 	.key_offset		= offsetof(struct nfsd_file, nf_inode),
202fc22945eSChuck Lever 	.head_offset		= offsetof(struct nfsd_file, nf_rhash),
203fc22945eSChuck Lever 	.hashfn			= nfsd_file_key_hashfn,
204fc22945eSChuck Lever 	.obj_hashfn		= nfsd_file_obj_hashfn,
205fc22945eSChuck Lever 	.obj_cmpfn		= nfsd_file_obj_cmpfn,
206fc22945eSChuck Lever 	/* Reduce resizing churn on light workloads */
207fc22945eSChuck Lever 	.min_size		= 512,		/* buckets */
208fc22945eSChuck Lever 	.automatic_shrinking	= true,
209fc22945eSChuck Lever };
21065294c1fSJeff Layton 
21165294c1fSJeff Layton static void
2129542e6a6STrond Myklebust nfsd_file_schedule_laundrette(void)
21365294c1fSJeff Layton {
21422ae4c11SJeff Layton 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags))
2159542e6a6STrond Myklebust 		queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
2169542e6a6STrond Myklebust 				   NFSD_LAUNDRETTE_DELAY);
21765294c1fSJeff Layton }
21865294c1fSJeff Layton 
21965294c1fSJeff Layton static void
22065294c1fSJeff Layton nfsd_file_slab_free(struct rcu_head *rcu)
22165294c1fSJeff Layton {
22265294c1fSJeff Layton 	struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
22365294c1fSJeff Layton 
22465294c1fSJeff Layton 	put_cred(nf->nf_cred);
22565294c1fSJeff Layton 	kmem_cache_free(nfsd_file_slab, nf);
22665294c1fSJeff Layton }
22765294c1fSJeff Layton 
22865294c1fSJeff Layton static void
22965294c1fSJeff Layton nfsd_file_mark_free(struct fsnotify_mark *mark)
23065294c1fSJeff Layton {
23165294c1fSJeff Layton 	struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
23265294c1fSJeff Layton 						  nfm_mark);
23365294c1fSJeff Layton 
23465294c1fSJeff Layton 	kmem_cache_free(nfsd_file_mark_slab, nfm);
23565294c1fSJeff Layton }
23665294c1fSJeff Layton 
23765294c1fSJeff Layton static struct nfsd_file_mark *
23865294c1fSJeff Layton nfsd_file_mark_get(struct nfsd_file_mark *nfm)
23965294c1fSJeff Layton {
240689827cdSTrond Myklebust 	if (!refcount_inc_not_zero(&nfm->nfm_ref))
24165294c1fSJeff Layton 		return NULL;
24265294c1fSJeff Layton 	return nfm;
24365294c1fSJeff Layton }
24465294c1fSJeff Layton 
24565294c1fSJeff Layton static void
24665294c1fSJeff Layton nfsd_file_mark_put(struct nfsd_file_mark *nfm)
24765294c1fSJeff Layton {
248689827cdSTrond Myklebust 	if (refcount_dec_and_test(&nfm->nfm_ref)) {
24965294c1fSJeff Layton 		fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
25065294c1fSJeff Layton 		fsnotify_put_mark(&nfm->nfm_mark);
25165294c1fSJeff Layton 	}
25265294c1fSJeff Layton }
25365294c1fSJeff Layton 
25465294c1fSJeff Layton static struct nfsd_file_mark *
255427f5f83SChuck Lever nfsd_file_mark_find_or_create(struct nfsd_file *nf, struct inode *inode)
25665294c1fSJeff Layton {
25765294c1fSJeff Layton 	int			err;
25865294c1fSJeff Layton 	struct fsnotify_mark	*mark;
25965294c1fSJeff Layton 	struct nfsd_file_mark	*nfm = NULL, *new;
26065294c1fSJeff Layton 
26165294c1fSJeff Layton 	do {
262b8962a9dSAmir Goldstein 		fsnotify_group_lock(nfsd_file_fsnotify_group);
26365294c1fSJeff Layton 		mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
26465294c1fSJeff Layton 					  nfsd_file_fsnotify_group);
26565294c1fSJeff Layton 		if (mark) {
26665294c1fSJeff Layton 			nfm = nfsd_file_mark_get(container_of(mark,
26765294c1fSJeff Layton 						 struct nfsd_file_mark,
26865294c1fSJeff Layton 						 nfm_mark));
269b8962a9dSAmir Goldstein 			fsnotify_group_unlock(nfsd_file_fsnotify_group);
27090d2f1daSTrond Myklebust 			if (nfm) {
27165294c1fSJeff Layton 				fsnotify_put_mark(mark);
27265294c1fSJeff Layton 				break;
27390d2f1daSTrond Myklebust 			}
27490d2f1daSTrond Myklebust 			/* Avoid soft lockup race with nfsd_file_mark_put() */
27590d2f1daSTrond Myklebust 			fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
27690d2f1daSTrond Myklebust 			fsnotify_put_mark(mark);
277b8962a9dSAmir Goldstein 		} else {
278b8962a9dSAmir Goldstein 			fsnotify_group_unlock(nfsd_file_fsnotify_group);
279b8962a9dSAmir Goldstein 		}
28065294c1fSJeff Layton 
28165294c1fSJeff Layton 		/* allocate a new nfm */
28265294c1fSJeff Layton 		new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
28365294c1fSJeff Layton 		if (!new)
28465294c1fSJeff Layton 			return NULL;
28565294c1fSJeff Layton 		fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
28665294c1fSJeff Layton 		new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
287689827cdSTrond Myklebust 		refcount_set(&new->nfm_ref, 1);
28865294c1fSJeff Layton 
28965294c1fSJeff Layton 		err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
29065294c1fSJeff Layton 
29165294c1fSJeff Layton 		/*
29265294c1fSJeff Layton 		 * If the add was successful, then return the object.
29365294c1fSJeff Layton 		 * Otherwise, we need to put the reference we hold on the
29465294c1fSJeff Layton 		 * nfm_mark. The fsnotify code will take a reference and put
29565294c1fSJeff Layton 		 * it on failure, so we can't just free it directly. It's also
29665294c1fSJeff Layton 		 * not safe to call fsnotify_destroy_mark on it as the
29765294c1fSJeff Layton 		 * mark->group will be NULL. Thus, we can't let the nfm_ref
29865294c1fSJeff Layton 		 * counter drive the destruction at this point.
29965294c1fSJeff Layton 		 */
30065294c1fSJeff Layton 		if (likely(!err))
30165294c1fSJeff Layton 			nfm = new;
30265294c1fSJeff Layton 		else
30365294c1fSJeff Layton 			fsnotify_put_mark(&new->nfm_mark);
30465294c1fSJeff Layton 	} while (unlikely(err == -EEXIST));
30565294c1fSJeff Layton 
30665294c1fSJeff Layton 	return nfm;
30765294c1fSJeff Layton }
30865294c1fSJeff Layton 
30965294c1fSJeff Layton static struct nfsd_file *
310ce502f81SChuck Lever nfsd_file_alloc(struct nfsd_file_lookup_key *key, unsigned int may)
31165294c1fSJeff Layton {
31265294c1fSJeff Layton 	struct nfsd_file *nf;
31365294c1fSJeff Layton 
31465294c1fSJeff Layton 	nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
31565294c1fSJeff Layton 	if (nf) {
31665294c1fSJeff Layton 		INIT_LIST_HEAD(&nf->nf_lru);
317904940e9SChuck Lever 		nf->nf_birthtime = ktime_get();
31865294c1fSJeff Layton 		nf->nf_file = NULL;
31965294c1fSJeff Layton 		nf->nf_cred = get_current_cred();
320ce502f81SChuck Lever 		nf->nf_net = key->net;
32165294c1fSJeff Layton 		nf->nf_flags = 0;
322ce502f81SChuck Lever 		__set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
323ce502f81SChuck Lever 		__set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
3244d1ea845SChuck Lever 		if (key->gc)
3254d1ea845SChuck Lever 			__set_bit(NFSD_FILE_GC, &nf->nf_flags);
326ce502f81SChuck Lever 		nf->nf_inode = key->inode;
327ac3a2585SJeff Layton 		refcount_set(&nf->nf_ref, 1);
328ce502f81SChuck Lever 		nf->nf_may = key->need;
32965294c1fSJeff Layton 		nf->nf_mark = NULL;
33065294c1fSJeff Layton 	}
33165294c1fSJeff Layton 	return nf;
33265294c1fSJeff Layton }
33365294c1fSJeff Layton 
33482141185SJeff Layton static void
33582141185SJeff Layton nfsd_file_fsync(struct nfsd_file *nf)
33682141185SJeff Layton {
33782141185SJeff Layton 	struct file *file = nf->nf_file;
338d7064eafSChuck Lever 	int ret;
33982141185SJeff Layton 
34082141185SJeff Layton 	if (!file || !(file->f_mode & FMODE_WRITE))
34182141185SJeff Layton 		return;
342d7064eafSChuck Lever 	ret = vfs_fsync(file, 1);
343d7064eafSChuck Lever 	trace_nfsd_file_fsync(nf, ret);
344d7064eafSChuck Lever 	if (ret)
34582141185SJeff Layton 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
34682141185SJeff Layton }
34782141185SJeff Layton 
34882141185SJeff Layton static int
34982141185SJeff Layton nfsd_file_check_write_error(struct nfsd_file *nf)
35082141185SJeff Layton {
35182141185SJeff Layton 	struct file *file = nf->nf_file;
35282141185SJeff Layton 
35382141185SJeff Layton 	if (!file || !(file->f_mode & FMODE_WRITE))
35482141185SJeff Layton 		return 0;
35582141185SJeff Layton 	return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
35682141185SJeff Layton }
35782141185SJeff Layton 
35882141185SJeff Layton static void
35982141185SJeff Layton nfsd_file_hash_remove(struct nfsd_file *nf)
36082141185SJeff Layton {
36182141185SJeff Layton 	trace_nfsd_file_unhash(nf);
36282141185SJeff Layton 
36382141185SJeff Layton 	if (nfsd_file_check_write_error(nf))
36482141185SJeff Layton 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
36582141185SJeff Layton 	rhashtable_remove_fast(&nfsd_file_rhash_tbl, &nf->nf_rhash,
36682141185SJeff Layton 			       nfsd_file_rhash_params);
36782141185SJeff Layton }
36882141185SJeff Layton 
36982141185SJeff Layton static bool
37082141185SJeff Layton nfsd_file_unhash(struct nfsd_file *nf)
37182141185SJeff Layton {
37282141185SJeff Layton 	if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
37382141185SJeff Layton 		nfsd_file_hash_remove(nf);
37482141185SJeff Layton 		return true;
37582141185SJeff Layton 	}
37682141185SJeff Layton 	return false;
37782141185SJeff Layton }
37882141185SJeff Layton 
379ac3a2585SJeff Layton static void
38065294c1fSJeff Layton nfsd_file_free(struct nfsd_file *nf)
38165294c1fSJeff Layton {
382904940e9SChuck Lever 	s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime));
38365294c1fSJeff Layton 
38482141185SJeff Layton 	trace_nfsd_file_free(nf);
38582141185SJeff Layton 
386d6329327SChuck Lever 	this_cpu_inc(nfsd_file_releases);
387904940e9SChuck Lever 	this_cpu_add(nfsd_file_total_age, age);
388d6329327SChuck Lever 
389ac3a2585SJeff Layton 	nfsd_file_unhash(nf);
390ac3a2585SJeff Layton 
391ac3a2585SJeff Layton 	/*
392ac3a2585SJeff Layton 	 * We call fsync here in order to catch writeback errors. It's not
393ac3a2585SJeff Layton 	 * strictly required by the protocol, but an nfsd_file could get
394ac3a2585SJeff Layton 	 * evicted from the cache before a COMMIT comes in. If another
395ac3a2585SJeff Layton 	 * task were to open that file in the interim and scrape the error,
396ac3a2585SJeff Layton 	 * then the client may never see it. By calling fsync here, we ensure
397ac3a2585SJeff Layton 	 * that writeback happens before the entry is freed, and that any
398ac3a2585SJeff Layton 	 * errors reported result in the write verifier changing.
399ac3a2585SJeff Layton 	 */
400ac3a2585SJeff Layton 	nfsd_file_fsync(nf);
401ac3a2585SJeff Layton 
40265294c1fSJeff Layton 	if (nf->nf_mark)
40365294c1fSJeff Layton 		nfsd_file_mark_put(nf->nf_mark);
40465294c1fSJeff Layton 	if (nf->nf_file) {
40565294c1fSJeff Layton 		get_file(nf->nf_file);
40665294c1fSJeff Layton 		filp_close(nf->nf_file, NULL);
40765294c1fSJeff Layton 		fput(nf->nf_file);
40865294c1fSJeff Layton 	}
409668ed92eSChuck Lever 
410668ed92eSChuck Lever 	/*
411668ed92eSChuck Lever 	 * If this item is still linked via nf_lru, that's a bug.
412668ed92eSChuck Lever 	 * WARN and leak it to preserve system stability.
413668ed92eSChuck Lever 	 */
414668ed92eSChuck Lever 	if (WARN_ON_ONCE(!list_empty(&nf->nf_lru)))
415ac3a2585SJeff Layton 		return;
416668ed92eSChuck Lever 
41765294c1fSJeff Layton 	call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
41865294c1fSJeff Layton }
41965294c1fSJeff Layton 
420055b24a8STrond Myklebust static bool
421055b24a8STrond Myklebust nfsd_file_check_writeback(struct nfsd_file *nf)
422055b24a8STrond Myklebust {
423055b24a8STrond Myklebust 	struct file *file = nf->nf_file;
424055b24a8STrond Myklebust 	struct address_space *mapping;
425055b24a8STrond Myklebust 
426055b24a8STrond Myklebust 	if (!file || !(file->f_mode & FMODE_WRITE))
427055b24a8STrond Myklebust 		return false;
428055b24a8STrond Myklebust 	mapping = file->f_mapping;
429055b24a8STrond Myklebust 	return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
430055b24a8STrond Myklebust 		mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
431055b24a8STrond Myklebust }
432055b24a8STrond Myklebust 
433ac3a2585SJeff Layton static bool nfsd_file_lru_add(struct nfsd_file *nf)
43465294c1fSJeff Layton {
4354a0e73e6SChuck Lever 	set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
436ac3a2585SJeff Layton 	if (list_lru_add(&nfsd_file_lru, &nf->nf_lru)) {
437c46203acSChuck Lever 		trace_nfsd_file_lru_add(nf);
438ac3a2585SJeff Layton 		return true;
439ac3a2585SJeff Layton 	}
440ac3a2585SJeff Layton 	return false;
441c46203acSChuck Lever }
44265294c1fSJeff Layton 
443ac3a2585SJeff Layton static bool nfsd_file_lru_remove(struct nfsd_file *nf)
444c46203acSChuck Lever {
445ac3a2585SJeff Layton 	if (list_lru_del(&nfsd_file_lru, &nf->nf_lru)) {
446c46203acSChuck Lever 		trace_nfsd_file_lru_del(nf);
447ac3a2585SJeff Layton 		return true;
448ac3a2585SJeff Layton 	}
449ac3a2585SJeff Layton 	return false;
450c46203acSChuck Lever }
451c46203acSChuck Lever 
45282141185SJeff Layton struct nfsd_file *
45382141185SJeff Layton nfsd_file_get(struct nfsd_file *nf)
45465294c1fSJeff Layton {
455*70f62231SJeff Layton 	if (nf && refcount_inc_not_zero(&nf->nf_ref))
45682141185SJeff Layton 		return nf;
45782141185SJeff Layton 	return NULL;
45865294c1fSJeff Layton }
45965294c1fSJeff Layton 
460ac3a2585SJeff Layton /**
461ac3a2585SJeff Layton  * nfsd_file_put - put the reference to a nfsd_file
462ac3a2585SJeff Layton  * @nf: nfsd_file of which to put the reference
463ac3a2585SJeff Layton  *
464ac3a2585SJeff Layton  * Put a reference to a nfsd_file. In the non-GC case, we just put the
465ac3a2585SJeff Layton  * reference immediately. In the GC case, if the reference would be
466ac3a2585SJeff Layton  * the last one, the put it on the LRU instead to be cleaned up later.
467ac3a2585SJeff Layton  */
46865294c1fSJeff Layton void
46965294c1fSJeff Layton nfsd_file_put(struct nfsd_file *nf)
47065294c1fSJeff Layton {
47108af54b3SChuck Lever 	might_sleep();
472ac3a2585SJeff Layton 	trace_nfsd_file_put(nf);
47308af54b3SChuck Lever 
474ac3a2585SJeff Layton 	if (test_bit(NFSD_FILE_GC, &nf->nf_flags) &&
475ac3a2585SJeff Layton 	    test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
476ac3a2585SJeff Layton 		/*
477ac3a2585SJeff Layton 		 * If this is the last reference (nf_ref == 1), then try to
478ac3a2585SJeff Layton 		 * transfer it to the LRU.
479ac3a2585SJeff Layton 		 */
480ac3a2585SJeff Layton 		if (refcount_dec_not_one(&nf->nf_ref))
481ac3a2585SJeff Layton 			return;
4824d1ea845SChuck Lever 
483ac3a2585SJeff Layton 		/* Try to add it to the LRU.  If that fails, decrement. */
484ac3a2585SJeff Layton 		if (nfsd_file_lru_add(nf)) {
485ac3a2585SJeff Layton 			/* If it's still hashed, we're done */
486ac3a2585SJeff Layton 			if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
4879542e6a6STrond Myklebust 				nfsd_file_schedule_laundrette();
488ac3a2585SJeff Layton 				return;
489ac3a2585SJeff Layton 			}
490ac3a2585SJeff Layton 
491ac3a2585SJeff Layton 			/*
492ac3a2585SJeff Layton 			 * We're racing with unhashing, so try to remove it from
493ac3a2585SJeff Layton 			 * the LRU. If removal fails, then someone else already
494ac3a2585SJeff Layton 			 * has our reference.
495ac3a2585SJeff Layton 			 */
496ac3a2585SJeff Layton 			if (!nfsd_file_lru_remove(nf))
497ac3a2585SJeff Layton 				return;
498ac3a2585SJeff Layton 		}
499ac3a2585SJeff Layton 	}
500ac3a2585SJeff Layton 	if (refcount_dec_and_test(&nf->nf_ref))
501ac3a2585SJeff Layton 		nfsd_file_free(nf);
50265294c1fSJeff Layton }
50365294c1fSJeff Layton 
50465294c1fSJeff Layton static void
50565294c1fSJeff Layton nfsd_file_dispose_list(struct list_head *dispose)
50665294c1fSJeff Layton {
50765294c1fSJeff Layton 	struct nfsd_file *nf;
50865294c1fSJeff Layton 
50965294c1fSJeff Layton 	while (!list_empty(dispose)) {
51065294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
511668ed92eSChuck Lever 		list_del_init(&nf->nf_lru);
512ac3a2585SJeff Layton 		nfsd_file_free(nf);
51365294c1fSJeff Layton 	}
51465294c1fSJeff Layton }
51565294c1fSJeff Layton 
51665294c1fSJeff Layton static void
5179542e6a6STrond Myklebust nfsd_file_list_remove_disposal(struct list_head *dst,
5189542e6a6STrond Myklebust 		struct nfsd_fcache_disposal *l)
5199542e6a6STrond Myklebust {
5209542e6a6STrond Myklebust 	spin_lock(&l->lock);
5219542e6a6STrond Myklebust 	list_splice_init(&l->freeme, dst);
5229542e6a6STrond Myklebust 	spin_unlock(&l->lock);
5239542e6a6STrond Myklebust }
5249542e6a6STrond Myklebust 
5259542e6a6STrond Myklebust static void
5269542e6a6STrond Myklebust nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
5279542e6a6STrond Myklebust {
5281463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5291463b38eSNeilBrown 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
5309542e6a6STrond Myklebust 
5319542e6a6STrond Myklebust 	spin_lock(&l->lock);
5329542e6a6STrond Myklebust 	list_splice_tail_init(files, &l->freeme);
5339542e6a6STrond Myklebust 	spin_unlock(&l->lock);
5349542e6a6STrond Myklebust 	queue_work(nfsd_filecache_wq, &l->work);
5359542e6a6STrond Myklebust }
5369542e6a6STrond Myklebust 
5379542e6a6STrond Myklebust static void
5389542e6a6STrond Myklebust nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
5399542e6a6STrond Myklebust 		struct net *net)
5409542e6a6STrond Myklebust {
5419542e6a6STrond Myklebust 	struct nfsd_file *nf, *tmp;
5429542e6a6STrond Myklebust 
5439542e6a6STrond Myklebust 	list_for_each_entry_safe(nf, tmp, src, nf_lru) {
5449542e6a6STrond Myklebust 		if (nf->nf_net == net)
5459542e6a6STrond Myklebust 			list_move_tail(&nf->nf_lru, dst);
5469542e6a6STrond Myklebust 	}
5479542e6a6STrond Myklebust }
5489542e6a6STrond Myklebust 
5499542e6a6STrond Myklebust static void
5509542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(struct list_head *dispose)
5519542e6a6STrond Myklebust {
5529542e6a6STrond Myklebust 	LIST_HEAD(list);
5539542e6a6STrond Myklebust 	struct nfsd_file *nf;
5549542e6a6STrond Myklebust 
5559542e6a6STrond Myklebust 	while(!list_empty(dispose)) {
5569542e6a6STrond Myklebust 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
5579542e6a6STrond Myklebust 		nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
5589542e6a6STrond Myklebust 		nfsd_file_list_add_disposal(&list, nf->nf_net);
5599542e6a6STrond Myklebust 	}
5609542e6a6STrond Myklebust }
5619542e6a6STrond Myklebust 
5624a0e73e6SChuck Lever /**
5634a0e73e6SChuck Lever  * nfsd_file_lru_cb - Examine an entry on the LRU list
5644a0e73e6SChuck Lever  * @item: LRU entry to examine
5654a0e73e6SChuck Lever  * @lru: controlling LRU
5664a0e73e6SChuck Lever  * @lock: LRU list lock (unused)
5674a0e73e6SChuck Lever  * @arg: dispose list
5684a0e73e6SChuck Lever  *
5694a0e73e6SChuck Lever  * Return values:
5704a0e73e6SChuck Lever  *   %LRU_REMOVED: @item was removed from the LRU
571edead3a5SChuck Lever  *   %LRU_ROTATE: @item is to be moved to the LRU tail
5724a0e73e6SChuck Lever  *   %LRU_SKIP: @item cannot be evicted
57365294c1fSJeff Layton  */
57465294c1fSJeff Layton static enum lru_status
57565294c1fSJeff Layton nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
57665294c1fSJeff Layton 		 spinlock_t *lock, void *arg)
57765294c1fSJeff Layton 	__releases(lock)
57865294c1fSJeff Layton 	__acquires(lock)
57965294c1fSJeff Layton {
58065294c1fSJeff Layton 	struct list_head *head = arg;
58165294c1fSJeff Layton 	struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
58265294c1fSJeff Layton 
583ac3a2585SJeff Layton 	/* We should only be dealing with GC entries here */
584ac3a2585SJeff Layton 	WARN_ON_ONCE(!test_bit(NFSD_FILE_GC, &nf->nf_flags));
585055b24a8STrond Myklebust 
586055b24a8STrond Myklebust 	/*
587055b24a8STrond Myklebust 	 * Don't throw out files that are still undergoing I/O or
588055b24a8STrond Myklebust 	 * that have uncleared errors pending.
589055b24a8STrond Myklebust 	 */
590c46203acSChuck Lever 	if (nfsd_file_check_writeback(nf)) {
591c46203acSChuck Lever 		trace_nfsd_file_gc_writeback(nf);
59265294c1fSJeff Layton 		return LRU_SKIP;
59365294c1fSJeff Layton 	}
59465294c1fSJeff Layton 
595ac3a2585SJeff Layton 	/* If it was recently added to the list, skip it */
596c46203acSChuck Lever 	if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
597c46203acSChuck Lever 		trace_nfsd_file_gc_referenced(nf);
598edead3a5SChuck Lever 		return LRU_ROTATE;
59965294c1fSJeff Layton 	}
60065294c1fSJeff Layton 
601ac3a2585SJeff Layton 	/*
602ac3a2585SJeff Layton 	 * Put the reference held on behalf of the LRU. If it wasn't the last
603ac3a2585SJeff Layton 	 * one, then just remove it from the LRU and ignore it.
604ac3a2585SJeff Layton 	 */
605ac3a2585SJeff Layton 	if (!refcount_dec_and_test(&nf->nf_ref)) {
606ac3a2585SJeff Layton 		trace_nfsd_file_gc_in_use(nf);
607ac3a2585SJeff Layton 		list_lru_isolate(lru, &nf->nf_lru);
608ac3a2585SJeff Layton 		return LRU_REMOVED;
609c46203acSChuck Lever 	}
61065294c1fSJeff Layton 
611ac3a2585SJeff Layton 	/* Refcount went to zero. Unhash it and queue it to the dispose list */
612ac3a2585SJeff Layton 	nfsd_file_unhash(nf);
61365294c1fSJeff Layton 	list_lru_isolate_move(lru, &nf->nf_lru, head);
61494660cc1SChuck Lever 	this_cpu_inc(nfsd_file_evictions);
615c46203acSChuck Lever 	trace_nfsd_file_gc_disposed(nf);
61665294c1fSJeff Layton 	return LRU_REMOVED;
61765294c1fSJeff Layton }
61865294c1fSJeff Layton 
6199542e6a6STrond Myklebust static void
6209542e6a6STrond Myklebust nfsd_file_gc(void)
6219542e6a6STrond Myklebust {
6223bc6d347SChuck Lever 	LIST_HEAD(dispose);
62394660cc1SChuck Lever 	unsigned long ret;
6243bc6d347SChuck Lever 
62594660cc1SChuck Lever 	ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
626edead3a5SChuck Lever 			    &dispose, list_lru_count(&nfsd_file_lru));
62794660cc1SChuck Lever 	trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
628ac3a2585SJeff Layton 	nfsd_file_dispose_list_delayed(&dispose);
6299542e6a6STrond Myklebust }
6309542e6a6STrond Myklebust 
6319542e6a6STrond Myklebust static void
6329542e6a6STrond Myklebust nfsd_file_gc_worker(struct work_struct *work)
6339542e6a6STrond Myklebust {
6349542e6a6STrond Myklebust 	nfsd_file_gc();
63522ae4c11SJeff Layton 	if (list_lru_count(&nfsd_file_lru))
6369542e6a6STrond Myklebust 		nfsd_file_schedule_laundrette();
63765294c1fSJeff Layton }
63865294c1fSJeff Layton 
63965294c1fSJeff Layton static unsigned long
64065294c1fSJeff Layton nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
64165294c1fSJeff Layton {
64265294c1fSJeff Layton 	return list_lru_count(&nfsd_file_lru);
64365294c1fSJeff Layton }
64465294c1fSJeff Layton 
64565294c1fSJeff Layton static unsigned long
64665294c1fSJeff Layton nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
64765294c1fSJeff Layton {
64839f1d1ffSChuck Lever 	LIST_HEAD(dispose);
64939f1d1ffSChuck Lever 	unsigned long ret;
65039f1d1ffSChuck Lever 
65139f1d1ffSChuck Lever 	ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
65239f1d1ffSChuck Lever 				   nfsd_file_lru_cb, &dispose);
65394660cc1SChuck Lever 	trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
654ac3a2585SJeff Layton 	nfsd_file_dispose_list_delayed(&dispose);
65539f1d1ffSChuck Lever 	return ret;
65665294c1fSJeff Layton }
65765294c1fSJeff Layton 
65865294c1fSJeff Layton static struct shrinker	nfsd_file_shrinker = {
65965294c1fSJeff Layton 	.scan_objects = nfsd_file_lru_scan,
66065294c1fSJeff Layton 	.count_objects = nfsd_file_lru_count,
66165294c1fSJeff Layton 	.seeks = 1,
66265294c1fSJeff Layton };
66365294c1fSJeff Layton 
664ac3a2585SJeff Layton /**
6654bdbba54SJeff Layton  * nfsd_file_cond_queue - conditionally unhash and queue a nfsd_file
6664bdbba54SJeff Layton  * @nf: nfsd_file to attempt to queue
6674bdbba54SJeff Layton  * @dispose: private list to queue successfully-put objects
6684bdbba54SJeff Layton  *
6694bdbba54SJeff Layton  * Unhash an nfsd_file, try to get a reference to it, and then put that
6704bdbba54SJeff Layton  * reference. If it's the last reference, queue it to the dispose list.
6714bdbba54SJeff Layton  */
6724bdbba54SJeff Layton static void
6734bdbba54SJeff Layton nfsd_file_cond_queue(struct nfsd_file *nf, struct list_head *dispose)
6744bdbba54SJeff Layton 	__must_hold(RCU)
6754bdbba54SJeff Layton {
6764bdbba54SJeff Layton 	int decrement = 1;
6774bdbba54SJeff Layton 
6784bdbba54SJeff Layton 	/* If we raced with someone else unhashing, ignore it */
6794bdbba54SJeff Layton 	if (!nfsd_file_unhash(nf))
6804bdbba54SJeff Layton 		return;
6814bdbba54SJeff Layton 
6824bdbba54SJeff Layton 	/* If we can't get a reference, ignore it */
6834bdbba54SJeff Layton 	if (!nfsd_file_get(nf))
6844bdbba54SJeff Layton 		return;
6854bdbba54SJeff Layton 
6864bdbba54SJeff Layton 	/* Extra decrement if we remove from the LRU */
6874bdbba54SJeff Layton 	if (nfsd_file_lru_remove(nf))
6884bdbba54SJeff Layton 		++decrement;
6894bdbba54SJeff Layton 
6904bdbba54SJeff Layton 	/* If refcount goes to 0, then put on the dispose list */
6914bdbba54SJeff Layton 	if (refcount_sub_and_test(decrement, &nf->nf_ref)) {
6924bdbba54SJeff Layton 		list_add(&nf->nf_lru, dispose);
6934bdbba54SJeff Layton 		trace_nfsd_file_closing(nf);
6944bdbba54SJeff Layton 	}
6954bdbba54SJeff Layton }
6964bdbba54SJeff Layton 
6974bdbba54SJeff Layton /**
698ac3a2585SJeff Layton  * nfsd_file_queue_for_close: try to close out any open nfsd_files for an inode
699ac3a2585SJeff Layton  * @inode:   inode on which to close out nfsd_files
700ac3a2585SJeff Layton  * @dispose: list on which to gather nfsd_files to close out
701ac3a2585SJeff Layton  *
702ac3a2585SJeff Layton  * An nfsd_file represents a struct file being held open on behalf of nfsd. An
703ac3a2585SJeff Layton  * open file however can block other activity (such as leases), or cause
704ac3a2585SJeff Layton  * undesirable behavior (e.g. spurious silly-renames when reexporting NFS).
705ac3a2585SJeff Layton  *
706ac3a2585SJeff Layton  * This function is intended to find open nfsd_files when this sort of
707ac3a2585SJeff Layton  * conflicting access occurs and then attempt to close those files out.
708ac3a2585SJeff Layton  *
709ac3a2585SJeff Layton  * Populates the dispose list with entries that have already had their
710ac3a2585SJeff Layton  * refcounts go to zero. The actual free of an nfsd_file can be expensive,
711ac3a2585SJeff Layton  * so we leave it up to the caller whether it wants to wait or not.
712a8455110SChuck Lever  */
713ac3a2585SJeff Layton static void
714ac3a2585SJeff Layton nfsd_file_queue_for_close(struct inode *inode, struct list_head *dispose)
71565294c1fSJeff Layton {
716ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
717ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_INODE,
718ce502f81SChuck Lever 		.inode	= inode,
719ce502f81SChuck Lever 	};
72065294c1fSJeff Layton 	struct nfsd_file *nf;
72165294c1fSJeff Layton 
722ce502f81SChuck Lever 	rcu_read_lock();
723ce502f81SChuck Lever 	do {
724ce502f81SChuck Lever 		nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
725ce502f81SChuck Lever 				       nfsd_file_rhash_params);
726ce502f81SChuck Lever 		if (!nf)
727ce502f81SChuck Lever 			break;
7284bdbba54SJeff Layton 		nfsd_file_cond_queue(nf, dispose);
729ce502f81SChuck Lever 	} while (1);
730ce502f81SChuck Lever 	rcu_read_unlock();
73165294c1fSJeff Layton }
73265294c1fSJeff Layton 
73365294c1fSJeff Layton /**
73419598141STrond Myklebust  * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
73565294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
73665294c1fSJeff Layton  *
737ac3a2585SJeff Layton  * Close out any open nfsd_files that can be reaped for @inode. The
738ac3a2585SJeff Layton  * actual freeing is deferred to the dispose_list_delayed infrastructure.
739ac3a2585SJeff Layton  *
740ac3a2585SJeff Layton  * This is used by the fsnotify callbacks and setlease notifier.
74165294c1fSJeff Layton  */
74265294c1fSJeff Layton static void
74365294c1fSJeff Layton nfsd_file_close_inode(struct inode *inode)
74465294c1fSJeff Layton {
74565294c1fSJeff Layton 	LIST_HEAD(dispose);
74665294c1fSJeff Layton 
747ac3a2585SJeff Layton 	nfsd_file_queue_for_close(inode, &dispose);
7489542e6a6STrond Myklebust 	nfsd_file_dispose_list_delayed(&dispose);
74965294c1fSJeff Layton }
75065294c1fSJeff Layton 
75165294c1fSJeff Layton /**
752ac3a2585SJeff Layton  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
753ac3a2585SJeff Layton  * @inode: inode of the file to attempt to remove
754ac3a2585SJeff Layton  *
755ac3a2585SJeff Layton  * Close out any open nfsd_files that can be reaped for @inode. The
756ac3a2585SJeff Layton  * nfsd_files are closed out synchronously.
757ac3a2585SJeff Layton  *
758ac3a2585SJeff Layton  * This is called from nfsd_rename and nfsd_unlink to avoid silly-renames
759ac3a2585SJeff Layton  * when reexporting NFS.
760ac3a2585SJeff Layton  */
761ac3a2585SJeff Layton void
762ac3a2585SJeff Layton nfsd_file_close_inode_sync(struct inode *inode)
763ac3a2585SJeff Layton {
764ac3a2585SJeff Layton 	struct nfsd_file *nf;
765ac3a2585SJeff Layton 	LIST_HEAD(dispose);
766ac3a2585SJeff Layton 
767ac3a2585SJeff Layton 	trace_nfsd_file_close(inode);
768ac3a2585SJeff Layton 
769ac3a2585SJeff Layton 	nfsd_file_queue_for_close(inode, &dispose);
770ac3a2585SJeff Layton 	while (!list_empty(&dispose)) {
771ac3a2585SJeff Layton 		nf = list_first_entry(&dispose, struct nfsd_file, nf_lru);
772ac3a2585SJeff Layton 		list_del_init(&nf->nf_lru);
773ac3a2585SJeff Layton 		nfsd_file_free(nf);
774ac3a2585SJeff Layton 	}
775ac3a2585SJeff Layton 	flush_delayed_fput();
776ac3a2585SJeff Layton }
777ac3a2585SJeff Layton 
778ac3a2585SJeff Layton /**
77965294c1fSJeff Layton  * nfsd_file_delayed_close - close unused nfsd_files
78065294c1fSJeff Layton  * @work: dummy
78165294c1fSJeff Layton  *
782ac3a2585SJeff Layton  * Walk the LRU list and destroy any entries that have not been used since
78365294c1fSJeff Layton  * the last scan.
78465294c1fSJeff Layton  */
78565294c1fSJeff Layton static void
78665294c1fSJeff Layton nfsd_file_delayed_close(struct work_struct *work)
78765294c1fSJeff Layton {
78865294c1fSJeff Layton 	LIST_HEAD(head);
7899542e6a6STrond Myklebust 	struct nfsd_fcache_disposal *l = container_of(work,
7909542e6a6STrond Myklebust 			struct nfsd_fcache_disposal, work);
79165294c1fSJeff Layton 
7929542e6a6STrond Myklebust 	nfsd_file_list_remove_disposal(&head, l);
7939542e6a6STrond Myklebust 	nfsd_file_dispose_list(&head);
79465294c1fSJeff Layton }
79565294c1fSJeff Layton 
79665294c1fSJeff Layton static int
79765294c1fSJeff Layton nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
79865294c1fSJeff Layton 			    void *data)
79965294c1fSJeff Layton {
80065294c1fSJeff Layton 	struct file_lock *fl = data;
80165294c1fSJeff Layton 
80265294c1fSJeff Layton 	/* Only close files for F_SETLEASE leases */
80365294c1fSJeff Layton 	if (fl->fl_flags & FL_LEASE)
804ac3a2585SJeff Layton 		nfsd_file_close_inode(file_inode(fl->fl_file));
80565294c1fSJeff Layton 	return 0;
80665294c1fSJeff Layton }
80765294c1fSJeff Layton 
80865294c1fSJeff Layton static struct notifier_block nfsd_file_lease_notifier = {
80965294c1fSJeff Layton 	.notifier_call = nfsd_file_lease_notifier_call,
81065294c1fSJeff Layton };
81165294c1fSJeff Layton 
81265294c1fSJeff Layton static int
813b9a1b977SAmir Goldstein nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
814b9a1b977SAmir Goldstein 				struct inode *inode, struct inode *dir,
815950cc0d2SAmir Goldstein 				const struct qstr *name, u32 cookie)
81665294c1fSJeff Layton {
81724dca905SGabriel Krisman Bertazi 	if (WARN_ON_ONCE(!inode))
81824dca905SGabriel Krisman Bertazi 		return 0;
81924dca905SGabriel Krisman Bertazi 
82065294c1fSJeff Layton 	trace_nfsd_file_fsnotify_handle_event(inode, mask);
82165294c1fSJeff Layton 
82265294c1fSJeff Layton 	/* Should be no marks on non-regular files */
82365294c1fSJeff Layton 	if (!S_ISREG(inode->i_mode)) {
82465294c1fSJeff Layton 		WARN_ON_ONCE(1);
82565294c1fSJeff Layton 		return 0;
82665294c1fSJeff Layton 	}
82765294c1fSJeff Layton 
82865294c1fSJeff Layton 	/* don't close files if this was not the last link */
82965294c1fSJeff Layton 	if (mask & FS_ATTRIB) {
83065294c1fSJeff Layton 		if (inode->i_nlink)
83165294c1fSJeff Layton 			return 0;
83265294c1fSJeff Layton 	}
83365294c1fSJeff Layton 
83465294c1fSJeff Layton 	nfsd_file_close_inode(inode);
83565294c1fSJeff Layton 	return 0;
83665294c1fSJeff Layton }
83765294c1fSJeff Layton 
83865294c1fSJeff Layton 
83965294c1fSJeff Layton static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
840b9a1b977SAmir Goldstein 	.handle_inode_event = nfsd_file_fsnotify_handle_event,
84165294c1fSJeff Layton 	.free_mark = nfsd_file_mark_free,
84265294c1fSJeff Layton };
84365294c1fSJeff Layton 
84465294c1fSJeff Layton int
84565294c1fSJeff Layton nfsd_file_cache_init(void)
84665294c1fSJeff Layton {
847fc22945eSChuck Lever 	int ret;
84865294c1fSJeff Layton 
849c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
850c7b824c3SChuck Lever 	if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
85165294c1fSJeff Layton 		return 0;
85265294c1fSJeff Layton 
853fc22945eSChuck Lever 	ret = rhashtable_init(&nfsd_file_rhash_tbl, &nfsd_file_rhash_params);
854fc22945eSChuck Lever 	if (ret)
855fc22945eSChuck Lever 		return ret;
856fc22945eSChuck Lever 
857fc22945eSChuck Lever 	ret = -ENOMEM;
8589542e6a6STrond Myklebust 	nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
8599542e6a6STrond Myklebust 	if (!nfsd_filecache_wq)
8609542e6a6STrond Myklebust 		goto out;
8619542e6a6STrond Myklebust 
86265294c1fSJeff Layton 	nfsd_file_slab = kmem_cache_create("nfsd_file",
86365294c1fSJeff Layton 				sizeof(struct nfsd_file), 0, 0, NULL);
86465294c1fSJeff Layton 	if (!nfsd_file_slab) {
86565294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_slab\n");
86665294c1fSJeff Layton 		goto out_err;
86765294c1fSJeff Layton 	}
86865294c1fSJeff Layton 
86965294c1fSJeff Layton 	nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
87065294c1fSJeff Layton 					sizeof(struct nfsd_file_mark), 0, 0, NULL);
87165294c1fSJeff Layton 	if (!nfsd_file_mark_slab) {
87265294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
87365294c1fSJeff Layton 		goto out_err;
87465294c1fSJeff Layton 	}
87565294c1fSJeff Layton 
87665294c1fSJeff Layton 
87765294c1fSJeff Layton 	ret = list_lru_init(&nfsd_file_lru);
87865294c1fSJeff Layton 	if (ret) {
87965294c1fSJeff Layton 		pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
88065294c1fSJeff Layton 		goto out_err;
88165294c1fSJeff Layton 	}
88265294c1fSJeff Layton 
883e33c267aSRoman Gushchin 	ret = register_shrinker(&nfsd_file_shrinker, "nfsd-filecache");
88465294c1fSJeff Layton 	if (ret) {
88565294c1fSJeff Layton 		pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
88665294c1fSJeff Layton 		goto out_lru;
88765294c1fSJeff Layton 	}
88865294c1fSJeff Layton 
88965294c1fSJeff Layton 	ret = lease_register_notifier(&nfsd_file_lease_notifier);
89065294c1fSJeff Layton 	if (ret) {
89165294c1fSJeff Layton 		pr_err("nfsd: unable to register lease notifier: %d\n", ret);
89265294c1fSJeff Layton 		goto out_shrinker;
89365294c1fSJeff Layton 	}
89465294c1fSJeff Layton 
895867a448dSAmir Goldstein 	nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
896b8962a9dSAmir Goldstein 							FSNOTIFY_GROUP_NOFS);
89765294c1fSJeff Layton 	if (IS_ERR(nfsd_file_fsnotify_group)) {
89865294c1fSJeff Layton 		pr_err("nfsd: unable to create fsnotify group: %ld\n",
89965294c1fSJeff Layton 			PTR_ERR(nfsd_file_fsnotify_group));
900231307dfSHuang Guobin 		ret = PTR_ERR(nfsd_file_fsnotify_group);
90165294c1fSJeff Layton 		nfsd_file_fsnotify_group = NULL;
90265294c1fSJeff Layton 		goto out_notifier;
90365294c1fSJeff Layton 	}
90465294c1fSJeff Layton 
9059542e6a6STrond Myklebust 	INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
90665294c1fSJeff Layton out:
90765294c1fSJeff Layton 	return ret;
90865294c1fSJeff Layton out_notifier:
90965294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
91065294c1fSJeff Layton out_shrinker:
91165294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
91265294c1fSJeff Layton out_lru:
91365294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
91465294c1fSJeff Layton out_err:
91565294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
91665294c1fSJeff Layton 	nfsd_file_slab = NULL;
91765294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
91865294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
9199542e6a6STrond Myklebust 	destroy_workqueue(nfsd_filecache_wq);
9209542e6a6STrond Myklebust 	nfsd_filecache_wq = NULL;
921fc22945eSChuck Lever 	rhashtable_destroy(&nfsd_file_rhash_tbl);
92265294c1fSJeff Layton 	goto out;
92365294c1fSJeff Layton }
92465294c1fSJeff Layton 
925ac3a2585SJeff Layton /**
926ac3a2585SJeff Layton  * __nfsd_file_cache_purge: clean out the cache for shutdown
927ac3a2585SJeff Layton  * @net: net-namespace to shut down the cache (may be NULL)
928ac3a2585SJeff Layton  *
929ac3a2585SJeff Layton  * Walk the nfsd_file cache and close out any that match @net. If @net is NULL,
930ac3a2585SJeff Layton  * then close out everything. Called when an nfsd instance is being shut down.
931ac3a2585SJeff Layton  */
932c7b824c3SChuck Lever static void
933c7b824c3SChuck Lever __nfsd_file_cache_purge(struct net *net)
93465294c1fSJeff Layton {
935ce502f81SChuck Lever 	struct rhashtable_iter iter;
93665294c1fSJeff Layton 	struct nfsd_file *nf;
93765294c1fSJeff Layton 	LIST_HEAD(dispose);
93865294c1fSJeff Layton 
939ce502f81SChuck Lever 	rhashtable_walk_enter(&nfsd_file_rhash_tbl, &iter);
940ce502f81SChuck Lever 	do {
941ce502f81SChuck Lever 		rhashtable_walk_start(&iter);
94265294c1fSJeff Layton 
943ce502f81SChuck Lever 		nf = rhashtable_walk_next(&iter);
944ce502f81SChuck Lever 		while (!IS_ERR_OR_NULL(nf)) {
9454bdbba54SJeff Layton 			if (!net || nf->nf_net == net)
9464bdbba54SJeff Layton 				nfsd_file_cond_queue(nf, &dispose);
947ce502f81SChuck Lever 			nf = rhashtable_walk_next(&iter);
94865294c1fSJeff Layton 		}
949ce502f81SChuck Lever 
950ce502f81SChuck Lever 		rhashtable_walk_stop(&iter);
951ce502f81SChuck Lever 	} while (nf == ERR_PTR(-EAGAIN));
952ce502f81SChuck Lever 	rhashtable_walk_exit(&iter);
953ce502f81SChuck Lever 
95465294c1fSJeff Layton 	nfsd_file_dispose_list(&dispose);
95565294c1fSJeff Layton }
95665294c1fSJeff Layton 
9579542e6a6STrond Myklebust static struct nfsd_fcache_disposal *
9581463b38eSNeilBrown nfsd_alloc_fcache_disposal(void)
9599542e6a6STrond Myklebust {
9609542e6a6STrond Myklebust 	struct nfsd_fcache_disposal *l;
9619542e6a6STrond Myklebust 
9629542e6a6STrond Myklebust 	l = kmalloc(sizeof(*l), GFP_KERNEL);
9639542e6a6STrond Myklebust 	if (!l)
9649542e6a6STrond Myklebust 		return NULL;
9659542e6a6STrond Myklebust 	INIT_WORK(&l->work, nfsd_file_delayed_close);
9669542e6a6STrond Myklebust 	spin_lock_init(&l->lock);
9679542e6a6STrond Myklebust 	INIT_LIST_HEAD(&l->freeme);
9689542e6a6STrond Myklebust 	return l;
9699542e6a6STrond Myklebust }
9709542e6a6STrond Myklebust 
9719542e6a6STrond Myklebust static void
9729542e6a6STrond Myklebust nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
9739542e6a6STrond Myklebust {
9749542e6a6STrond Myklebust 	cancel_work_sync(&l->work);
9759542e6a6STrond Myklebust 	nfsd_file_dispose_list(&l->freeme);
9761463b38eSNeilBrown 	kfree(l);
9779542e6a6STrond Myklebust }
9789542e6a6STrond Myklebust 
9799542e6a6STrond Myklebust static void
9809542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(struct net *net)
9819542e6a6STrond Myklebust {
9821463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9831463b38eSNeilBrown 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
9849542e6a6STrond Myklebust 
9859542e6a6STrond Myklebust 	nfsd_free_fcache_disposal(l);
9869542e6a6STrond Myklebust }
9879542e6a6STrond Myklebust 
9889542e6a6STrond Myklebust int
9899542e6a6STrond Myklebust nfsd_file_cache_start_net(struct net *net)
9909542e6a6STrond Myklebust {
9911463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9921463b38eSNeilBrown 
9931463b38eSNeilBrown 	nn->fcache_disposal = nfsd_alloc_fcache_disposal();
9941463b38eSNeilBrown 	return nn->fcache_disposal ? 0 : -ENOMEM;
9959542e6a6STrond Myklebust }
9969542e6a6STrond Myklebust 
997c7b824c3SChuck Lever /**
998c7b824c3SChuck Lever  * nfsd_file_cache_purge - Remove all cache items associated with @net
999c7b824c3SChuck Lever  * @net: target net namespace
1000c7b824c3SChuck Lever  *
1001c7b824c3SChuck Lever  */
1002c7b824c3SChuck Lever void
1003c7b824c3SChuck Lever nfsd_file_cache_purge(struct net *net)
1004c7b824c3SChuck Lever {
1005c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
1006c7b824c3SChuck Lever 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
1007c7b824c3SChuck Lever 		__nfsd_file_cache_purge(net);
1008c7b824c3SChuck Lever }
1009c7b824c3SChuck Lever 
10109542e6a6STrond Myklebust void
10119542e6a6STrond Myklebust nfsd_file_cache_shutdown_net(struct net *net)
10129542e6a6STrond Myklebust {
10139542e6a6STrond Myklebust 	nfsd_file_cache_purge(net);
10149542e6a6STrond Myklebust 	nfsd_free_fcache_disposal_net(net);
10159542e6a6STrond Myklebust }
10169542e6a6STrond Myklebust 
101765294c1fSJeff Layton void
101865294c1fSJeff Layton nfsd_file_cache_shutdown(void)
101965294c1fSJeff Layton {
10208b330f78SChuck Lever 	int i;
10218b330f78SChuck Lever 
1022c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
1023c7b824c3SChuck Lever 	if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
1024c7b824c3SChuck Lever 		return;
102565294c1fSJeff Layton 
102665294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
102765294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
102865294c1fSJeff Layton 	/*
102965294c1fSJeff Layton 	 * make sure all callers of nfsd_file_lru_cb are done before
103065294c1fSJeff Layton 	 * calling nfsd_file_cache_purge
103165294c1fSJeff Layton 	 */
103265294c1fSJeff Layton 	cancel_delayed_work_sync(&nfsd_filecache_laundrette);
1033c7b824c3SChuck Lever 	__nfsd_file_cache_purge(NULL);
103465294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
103565294c1fSJeff Layton 	rcu_barrier();
103665294c1fSJeff Layton 	fsnotify_put_group(nfsd_file_fsnotify_group);
103765294c1fSJeff Layton 	nfsd_file_fsnotify_group = NULL;
103865294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
103965294c1fSJeff Layton 	nfsd_file_slab = NULL;
104065294c1fSJeff Layton 	fsnotify_wait_marks_destroyed();
104165294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
104265294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
10439542e6a6STrond Myklebust 	destroy_workqueue(nfsd_filecache_wq);
10449542e6a6STrond Myklebust 	nfsd_filecache_wq = NULL;
1045fc22945eSChuck Lever 	rhashtable_destroy(&nfsd_file_rhash_tbl);
104665294c1fSJeff Layton 
10478b330f78SChuck Lever 	for_each_possible_cpu(i) {
10488b330f78SChuck Lever 		per_cpu(nfsd_file_cache_hits, i) = 0;
10498b330f78SChuck Lever 		per_cpu(nfsd_file_acquisitions, i) = 0;
10508b330f78SChuck Lever 		per_cpu(nfsd_file_releases, i) = 0;
10518b330f78SChuck Lever 		per_cpu(nfsd_file_total_age, i) = 0;
10528b330f78SChuck Lever 		per_cpu(nfsd_file_evictions, i) = 0;
105365294c1fSJeff Layton 	}
105465294c1fSJeff Layton }
105565294c1fSJeff Layton 
105665294c1fSJeff Layton /**
1057ce502f81SChuck Lever  * nfsd_file_is_cached - are there any cached open files for this inode?
1058ce502f81SChuck Lever  * @inode: inode to check
105965294c1fSJeff Layton  *
1060ce502f81SChuck Lever  * The lookup matches inodes in all net namespaces and is atomic wrt
1061ce502f81SChuck Lever  * nfsd_file_acquire().
1062ce502f81SChuck Lever  *
1063ce502f81SChuck Lever  * Return values:
1064ce502f81SChuck Lever  *   %true: filecache contains at least one file matching this inode
1065ce502f81SChuck Lever  *   %false: filecache contains no files matching this inode
106665294c1fSJeff Layton  */
106765294c1fSJeff Layton bool
106865294c1fSJeff Layton nfsd_file_is_cached(struct inode *inode)
106965294c1fSJeff Layton {
1070ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
1071ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_INODE,
1072ce502f81SChuck Lever 		.inode	= inode,
1073ce502f81SChuck Lever 	};
107465294c1fSJeff Layton 	bool ret = false;
107565294c1fSJeff Layton 
1076ce502f81SChuck Lever 	if (rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key,
1077ce502f81SChuck Lever 				   nfsd_file_rhash_params) != NULL)
107865294c1fSJeff Layton 		ret = true;
107954f7df70SChuck Lever 	trace_nfsd_file_is_cached(inode, (int)ret);
108065294c1fSJeff Layton 	return ret;
108165294c1fSJeff Layton }
108265294c1fSJeff Layton 
1083fb70bf12SChuck Lever static __be32
1084be023006SChuck Lever nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
10850b3a551fSJeff Layton 		     unsigned int may_flags, struct file *file,
10860b3a551fSJeff Layton 		     struct nfsd_file **pnf, bool want_gc)
108765294c1fSJeff Layton {
1088ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
1089ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_FULL,
1090ce502f81SChuck Lever 		.need	= may_flags & NFSD_FILE_MAY_MASK,
1091ce502f81SChuck Lever 		.net	= SVC_NET(rqstp),
10924d1ea845SChuck Lever 		.gc	= want_gc,
1093ce502f81SChuck Lever 	};
1094243a5263SJeff Layton 	bool open_retry = true;
1095243a5263SJeff Layton 	struct nfsd_file *nf;
1096ce502f81SChuck Lever 	__be32 status;
1097243a5263SJeff Layton 	int ret;
109865294c1fSJeff Layton 
109965294c1fSJeff Layton 	status = fh_verify(rqstp, fhp, S_IFREG,
110065294c1fSJeff Layton 				may_flags|NFSD_MAY_OWNER_OVERRIDE);
110165294c1fSJeff Layton 	if (status != nfs_ok)
110265294c1fSJeff Layton 		return status;
1103ce502f81SChuck Lever 	key.inode = d_inode(fhp->fh_dentry);
1104ce502f81SChuck Lever 	key.cred = get_current_cred();
110565294c1fSJeff Layton 
110665294c1fSJeff Layton retry:
1107243a5263SJeff Layton 	rcu_read_lock();
1108243a5263SJeff Layton 	nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
1109ce502f81SChuck Lever 			       nfsd_file_rhash_params);
1110ce502f81SChuck Lever 	nf = nfsd_file_get(nf);
1111243a5263SJeff Layton 	rcu_read_unlock();
1112ac3a2585SJeff Layton 
1113ac3a2585SJeff Layton 	if (nf) {
1114ac3a2585SJeff Layton 		if (nfsd_file_lru_remove(nf))
1115ac3a2585SJeff Layton 			WARN_ON_ONCE(refcount_dec_and_test(&nf->nf_ref));
111665294c1fSJeff Layton 		goto wait_for_construction;
1117ac3a2585SJeff Layton 	}
111865294c1fSJeff Layton 
1119243a5263SJeff Layton 	nf = nfsd_file_alloc(&key, may_flags);
1120243a5263SJeff Layton 	if (!nf) {
112154f7df70SChuck Lever 		status = nfserr_jukebox;
112254f7df70SChuck Lever 		goto out_status;
112365294c1fSJeff Layton 	}
112465294c1fSJeff Layton 
1125243a5263SJeff Layton 	ret = rhashtable_lookup_insert_key(&nfsd_file_rhash_tbl,
1126243a5263SJeff Layton 					   &key, &nf->nf_rhash,
1127ce502f81SChuck Lever 					   nfsd_file_rhash_params);
1128243a5263SJeff Layton 	if (likely(ret == 0))
112965294c1fSJeff Layton 		goto open_file;
1130243a5263SJeff Layton 
1131243a5263SJeff Layton 	nfsd_file_slab_free(&nf->nf_rcu);
1132bdd6b562SJeff Layton 	nf = NULL;
1133243a5263SJeff Layton 	if (ret == -EEXIST)
1134243a5263SJeff Layton 		goto retry;
1135243a5263SJeff Layton 	trace_nfsd_file_insert_err(rqstp, key.inode, may_flags, ret);
1136243a5263SJeff Layton 	status = nfserr_jukebox;
1137243a5263SJeff Layton 	goto out_status;
113865294c1fSJeff Layton 
113965294c1fSJeff Layton wait_for_construction:
114065294c1fSJeff Layton 	wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
114165294c1fSJeff Layton 
114265294c1fSJeff Layton 	/* Did construction of this file fail? */
114365294c1fSJeff Layton 	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
1144ce502f81SChuck Lever 		trace_nfsd_file_cons_err(rqstp, key.inode, may_flags, nf);
1145243a5263SJeff Layton 		if (!open_retry) {
114628c7d86bSTrond Myklebust 			status = nfserr_jukebox;
114728c7d86bSTrond Myklebust 			goto out;
114828c7d86bSTrond Myklebust 		}
1149243a5263SJeff Layton 		open_retry = false;
1150ac3a2585SJeff Layton 		if (refcount_dec_and_test(&nf->nf_ref))
1151ac3a2585SJeff Layton 			nfsd_file_free(nf);
115265294c1fSJeff Layton 		goto retry;
115365294c1fSJeff Layton 	}
115465294c1fSJeff Layton 
115565294c1fSJeff Layton 	this_cpu_inc(nfsd_file_cache_hits);
115665294c1fSJeff Layton 
115723ba98deSJeff Layton 	status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
115865294c1fSJeff Layton out:
115965294c1fSJeff Layton 	if (status == nfs_ok) {
116029d4bdbbSChuck Lever 		this_cpu_inc(nfsd_file_acquisitions);
116165294c1fSJeff Layton 		*pnf = nf;
116265294c1fSJeff Layton 	} else {
1163ac3a2585SJeff Layton 		if (refcount_dec_and_test(&nf->nf_ref))
1164ac3a2585SJeff Layton 			nfsd_file_free(nf);
116565294c1fSJeff Layton 		nf = NULL;
116665294c1fSJeff Layton 	}
116765294c1fSJeff Layton 
116854f7df70SChuck Lever out_status:
1169ce502f81SChuck Lever 	put_cred(key.cred);
1170ce502f81SChuck Lever 	trace_nfsd_file_acquire(rqstp, key.inode, may_flags, nf, status);
117165294c1fSJeff Layton 	return status;
117265294c1fSJeff Layton 
117365294c1fSJeff Layton open_file:
1174b40a2839SChuck Lever 	trace_nfsd_file_alloc(nf);
1175427f5f83SChuck Lever 	nf->nf_mark = nfsd_file_mark_find_or_create(nf, key.inode);
1176fb70bf12SChuck Lever 	if (nf->nf_mark) {
11770b3a551fSJeff Layton 		if (file) {
11780b3a551fSJeff Layton 			get_file(file);
11790b3a551fSJeff Layton 			nf->nf_file = file;
11800b3a551fSJeff Layton 			status = nfs_ok;
11810b3a551fSJeff Layton 			trace_nfsd_file_opened(nf, status);
11820b3a551fSJeff Layton 		} else {
1183f4d84c52SChuck Lever 			status = nfsd_open_verified(rqstp, fhp, may_flags,
1184f4d84c52SChuck Lever 						    &nf->nf_file);
11850122e882SChuck Lever 			trace_nfsd_file_open(nf, status);
11860b3a551fSJeff Layton 		}
1187fb70bf12SChuck Lever 	} else
118865294c1fSJeff Layton 		status = nfserr_jukebox;
118965294c1fSJeff Layton 	/*
119065294c1fSJeff Layton 	 * If construction failed, or we raced with a call to unlink()
119165294c1fSJeff Layton 	 * then unhash.
119265294c1fSJeff Layton 	 */
1193ac3a2585SJeff Layton 	if (status == nfs_ok && key.inode->i_nlink == 0)
1194ac3a2585SJeff Layton 		status = nfserr_jukebox;
1195ac3a2585SJeff Layton 	if (status != nfs_ok)
1196ac3a2585SJeff Layton 		nfsd_file_unhash(nf);
119765294c1fSJeff Layton 	clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
119865294c1fSJeff Layton 	smp_mb__after_atomic();
119965294c1fSJeff Layton 	wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
120065294c1fSJeff Layton 	goto out;
120165294c1fSJeff Layton }
120265294c1fSJeff Layton 
1203fb70bf12SChuck Lever /**
12044d1ea845SChuck Lever  * nfsd_file_acquire_gc - Get a struct nfsd_file with an open file
12054d1ea845SChuck Lever  * @rqstp: the RPC transaction being executed
12064d1ea845SChuck Lever  * @fhp: the NFS filehandle of the file to be opened
12074d1ea845SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
12084d1ea845SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
12094d1ea845SChuck Lever  *
12104d1ea845SChuck Lever  * The nfsd_file object returned by this API is reference-counted
12114d1ea845SChuck Lever  * and garbage-collected. The object is retained for a few
12124d1ea845SChuck Lever  * seconds after the final nfsd_file_put() in case the caller
12134d1ea845SChuck Lever  * wants to re-use it.
12144d1ea845SChuck Lever  *
12154d1ea845SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
12164d1ea845SChuck Lever  * network byte order is returned.
12174d1ea845SChuck Lever  */
12184d1ea845SChuck Lever __be32
12194d1ea845SChuck Lever nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp,
12204d1ea845SChuck Lever 		     unsigned int may_flags, struct nfsd_file **pnf)
12214d1ea845SChuck Lever {
12220b3a551fSJeff Layton 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, NULL, pnf, true);
12234d1ea845SChuck Lever }
12244d1ea845SChuck Lever 
12254d1ea845SChuck Lever /**
1226fb70bf12SChuck Lever  * nfsd_file_acquire - Get a struct nfsd_file with an open file
1227fb70bf12SChuck Lever  * @rqstp: the RPC transaction being executed
1228fb70bf12SChuck Lever  * @fhp: the NFS filehandle of the file to be opened
1229fb70bf12SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
1230fb70bf12SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1231fb70bf12SChuck Lever  *
12324d1ea845SChuck Lever  * The nfsd_file_object returned by this API is reference-counted
12334d1ea845SChuck Lever  * but not garbage-collected. The object is unhashed after the
12344d1ea845SChuck Lever  * final nfsd_file_put().
12354d1ea845SChuck Lever  *
1236fb70bf12SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1237fb70bf12SChuck Lever  * network byte order is returned.
1238fb70bf12SChuck Lever  */
1239fb70bf12SChuck Lever __be32
1240fb70bf12SChuck Lever nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1241fb70bf12SChuck Lever 		  unsigned int may_flags, struct nfsd_file **pnf)
1242fb70bf12SChuck Lever {
12430b3a551fSJeff Layton 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, NULL, pnf, false);
1244fb70bf12SChuck Lever }
1245fb70bf12SChuck Lever 
1246fb70bf12SChuck Lever /**
12470b3a551fSJeff Layton  * nfsd_file_acquire_opened - Get a struct nfsd_file using existing open file
1248fb70bf12SChuck Lever  * @rqstp: the RPC transaction being executed
1249fb70bf12SChuck Lever  * @fhp: the NFS filehandle of the file just created
1250fb70bf12SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
12510b3a551fSJeff Layton  * @file: cached, already-open file (may be NULL)
1252fb70bf12SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1253fb70bf12SChuck Lever  *
12540b3a551fSJeff Layton  * Acquire a nfsd_file object that is not GC'ed. If one doesn't already exist,
12550b3a551fSJeff Layton  * and @file is non-NULL, use it to instantiate a new nfsd_file instead of
12560b3a551fSJeff Layton  * opening a new one.
12574d1ea845SChuck Lever  *
1258fb70bf12SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1259fb70bf12SChuck Lever  * network byte order is returned.
1260fb70bf12SChuck Lever  */
1261fb70bf12SChuck Lever __be32
12620b3a551fSJeff Layton nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,
12630b3a551fSJeff Layton 			 unsigned int may_flags, struct file *file,
12640b3a551fSJeff Layton 			 struct nfsd_file **pnf)
1265fb70bf12SChuck Lever {
12660b3a551fSJeff Layton 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, file, pnf, false);
1267fb70bf12SChuck Lever }
1268fb70bf12SChuck Lever 
126965294c1fSJeff Layton /*
127065294c1fSJeff Layton  * Note that fields may be added, removed or reordered in the future. Programs
127165294c1fSJeff Layton  * scraping this file for info should test the labels to ensure they're
127265294c1fSJeff Layton  * getting the correct field.
127365294c1fSJeff Layton  */
12741342f9ddSChenXiaoSong int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
127565294c1fSJeff Layton {
12761f696e23SJeff Layton 	unsigned long releases = 0, evictions = 0;
1277df2aff52SChuck Lever 	unsigned long hits = 0, acquisitions = 0;
1278ce502f81SChuck Lever 	unsigned int i, count = 0, buckets = 0;
1279904940e9SChuck Lever 	unsigned long lru = 0, total_age = 0;
128065294c1fSJeff Layton 
1281ce502f81SChuck Lever 	/* Serialize with server shutdown */
128265294c1fSJeff Layton 	mutex_lock(&nfsd_mutex);
1283c7b824c3SChuck Lever 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
1284ce502f81SChuck Lever 		struct bucket_table *tbl;
1285ce502f81SChuck Lever 		struct rhashtable *ht;
1286ce502f81SChuck Lever 
12870fd244c1SChuck Lever 		lru = list_lru_count(&nfsd_file_lru);
1288ce502f81SChuck Lever 
1289ce502f81SChuck Lever 		rcu_read_lock();
1290ce502f81SChuck Lever 		ht = &nfsd_file_rhash_tbl;
1291ce502f81SChuck Lever 		count = atomic_read(&ht->nelems);
1292ce502f81SChuck Lever 		tbl = rht_dereference_rcu(ht->tbl, ht);
1293ce502f81SChuck Lever 		buckets = tbl->size;
1294ce502f81SChuck Lever 		rcu_read_unlock();
129565294c1fSJeff Layton 	}
129665294c1fSJeff Layton 	mutex_unlock(&nfsd_mutex);
129765294c1fSJeff Layton 
129829d4bdbbSChuck Lever 	for_each_possible_cpu(i) {
129965294c1fSJeff Layton 		hits += per_cpu(nfsd_file_cache_hits, i);
130029d4bdbbSChuck Lever 		acquisitions += per_cpu(nfsd_file_acquisitions, i);
1301d6329327SChuck Lever 		releases += per_cpu(nfsd_file_releases, i);
1302904940e9SChuck Lever 		total_age += per_cpu(nfsd_file_total_age, i);
130394660cc1SChuck Lever 		evictions += per_cpu(nfsd_file_evictions, i);
130429d4bdbbSChuck Lever 	}
130565294c1fSJeff Layton 
130665294c1fSJeff Layton 	seq_printf(m, "total entries: %u\n", count);
1307ce502f81SChuck Lever 	seq_printf(m, "hash buckets:  %u\n", buckets);
13080fd244c1SChuck Lever 	seq_printf(m, "lru entries:   %lu\n", lru);
130965294c1fSJeff Layton 	seq_printf(m, "cache hits:    %lu\n", hits);
131029d4bdbbSChuck Lever 	seq_printf(m, "acquisitions:  %lu\n", acquisitions);
1311d6329327SChuck Lever 	seq_printf(m, "releases:      %lu\n", releases);
131294660cc1SChuck Lever 	seq_printf(m, "evictions:     %lu\n", evictions);
1313904940e9SChuck Lever 	if (releases)
1314904940e9SChuck Lever 		seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1315904940e9SChuck Lever 	else
1316904940e9SChuck Lever 		seq_printf(m, "mean age (ms): -\n");
131765294c1fSJeff Layton 	return 0;
131865294c1fSJeff Layton }
1319