xref: /openbmc/linux/fs/nfsd/filecache.c (revision 4d1ea8455716ca070e3cd85767e6f6a562a58b1b)
165294c1fSJeff Layton /*
265294c1fSJeff Layton  * Open file cache.
365294c1fSJeff Layton  *
465294c1fSJeff Layton  * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com>
565294c1fSJeff Layton  */
665294c1fSJeff Layton 
765294c1fSJeff Layton #include <linux/hash.h>
865294c1fSJeff Layton #include <linux/slab.h>
965294c1fSJeff Layton #include <linux/file.h>
10cbcc268bSMatthew Wilcox (Oracle) #include <linux/pagemap.h>
1165294c1fSJeff Layton #include <linux/sched.h>
1265294c1fSJeff Layton #include <linux/list_lru.h>
1365294c1fSJeff Layton #include <linux/fsnotify_backend.h>
1465294c1fSJeff Layton #include <linux/fsnotify.h>
1565294c1fSJeff Layton #include <linux/seq_file.h>
16fc22945eSChuck Lever #include <linux/rhashtable.h>
1765294c1fSJeff Layton 
1865294c1fSJeff Layton #include "vfs.h"
1965294c1fSJeff Layton #include "nfsd.h"
2065294c1fSJeff Layton #include "nfsfh.h"
215e113224STrond Myklebust #include "netns.h"
2265294c1fSJeff Layton #include "filecache.h"
2365294c1fSJeff Layton #include "trace.h"
2465294c1fSJeff Layton 
2565294c1fSJeff Layton #define NFSD_LAUNDRETTE_DELAY		     (2 * HZ)
2665294c1fSJeff Layton 
27c7b824c3SChuck Lever #define NFSD_FILE_CACHE_UP		     (0)
2865294c1fSJeff Layton 
2965294c1fSJeff Layton /* We only care about NFSD_MAY_READ/WRITE for this cache */
3065294c1fSJeff Layton #define NFSD_FILE_MAY_MASK	(NFSD_MAY_READ|NFSD_MAY_WRITE)
3165294c1fSJeff Layton 
3265294c1fSJeff Layton static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
3329d4bdbbSChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions);
34d6329327SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_releases);
35904940e9SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age);
36df2aff52SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_pages_flushed);
3794660cc1SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions);
3865294c1fSJeff Layton 
399542e6a6STrond Myklebust struct nfsd_fcache_disposal {
409542e6a6STrond Myklebust 	struct work_struct work;
419542e6a6STrond Myklebust 	spinlock_t lock;
429542e6a6STrond Myklebust 	struct list_head freeme;
439542e6a6STrond Myklebust };
449542e6a6STrond Myklebust 
4550d0def9SChen Zhou static struct workqueue_struct *nfsd_filecache_wq __read_mostly;
469542e6a6STrond Myklebust 
4765294c1fSJeff Layton static struct kmem_cache		*nfsd_file_slab;
4865294c1fSJeff Layton static struct kmem_cache		*nfsd_file_mark_slab;
4965294c1fSJeff Layton static struct list_lru			nfsd_file_lru;
50c7b824c3SChuck Lever static unsigned long			nfsd_file_flags;
5165294c1fSJeff Layton static struct fsnotify_group		*nfsd_file_fsnotify_group;
5265294c1fSJeff Layton static struct delayed_work		nfsd_filecache_laundrette;
53fc22945eSChuck Lever static struct rhashtable		nfsd_file_rhash_tbl
54fc22945eSChuck Lever 						____cacheline_aligned_in_smp;
5565294c1fSJeff Layton 
56fc22945eSChuck Lever enum nfsd_file_lookup_type {
57fc22945eSChuck Lever 	NFSD_FILE_KEY_INODE,
58fc22945eSChuck Lever 	NFSD_FILE_KEY_FULL,
59fc22945eSChuck Lever };
60fc22945eSChuck Lever 
61fc22945eSChuck Lever struct nfsd_file_lookup_key {
62fc22945eSChuck Lever 	struct inode			*inode;
63fc22945eSChuck Lever 	struct net			*net;
64fc22945eSChuck Lever 	const struct cred		*cred;
65fc22945eSChuck Lever 	unsigned char			need;
66*4d1ea845SChuck Lever 	bool				gc;
67fc22945eSChuck Lever 	enum nfsd_file_lookup_type	type;
68fc22945eSChuck Lever };
69fc22945eSChuck Lever 
70fc22945eSChuck Lever /*
71fc22945eSChuck Lever  * The returned hash value is based solely on the address of an in-code
72fc22945eSChuck Lever  * inode, a pointer to a slab-allocated object. The entropy in such a
73fc22945eSChuck Lever  * pointer is concentrated in its middle bits.
74fc22945eSChuck Lever  */
75fc22945eSChuck Lever static u32 nfsd_file_inode_hash(const struct inode *inode, u32 seed)
76fc22945eSChuck Lever {
77fc22945eSChuck Lever 	unsigned long ptr = (unsigned long)inode;
78fc22945eSChuck Lever 	u32 k;
79fc22945eSChuck Lever 
80fc22945eSChuck Lever 	k = ptr >> L1_CACHE_SHIFT;
81fc22945eSChuck Lever 	k &= 0x00ffffff;
82fc22945eSChuck Lever 	return jhash2(&k, 1, seed);
83fc22945eSChuck Lever }
84fc22945eSChuck Lever 
85fc22945eSChuck Lever /**
86fc22945eSChuck Lever  * nfsd_file_key_hashfn - Compute the hash value of a lookup key
87fc22945eSChuck Lever  * @data: key on which to compute the hash value
88fc22945eSChuck Lever  * @len: rhash table's key_len parameter (unused)
89fc22945eSChuck Lever  * @seed: rhash table's random seed of the day
90fc22945eSChuck Lever  *
91fc22945eSChuck Lever  * Return value:
92fc22945eSChuck Lever  *   Computed 32-bit hash value
93fc22945eSChuck Lever  */
94fc22945eSChuck Lever static u32 nfsd_file_key_hashfn(const void *data, u32 len, u32 seed)
95fc22945eSChuck Lever {
96fc22945eSChuck Lever 	const struct nfsd_file_lookup_key *key = data;
97fc22945eSChuck Lever 
98fc22945eSChuck Lever 	return nfsd_file_inode_hash(key->inode, seed);
99fc22945eSChuck Lever }
100fc22945eSChuck Lever 
101fc22945eSChuck Lever /**
102fc22945eSChuck Lever  * nfsd_file_obj_hashfn - Compute the hash value of an nfsd_file
103fc22945eSChuck Lever  * @data: object on which to compute the hash value
104fc22945eSChuck Lever  * @len: rhash table's key_len parameter (unused)
105fc22945eSChuck Lever  * @seed: rhash table's random seed of the day
106fc22945eSChuck Lever  *
107fc22945eSChuck Lever  * Return value:
108fc22945eSChuck Lever  *   Computed 32-bit hash value
109fc22945eSChuck Lever  */
110fc22945eSChuck Lever static u32 nfsd_file_obj_hashfn(const void *data, u32 len, u32 seed)
111fc22945eSChuck Lever {
112fc22945eSChuck Lever 	const struct nfsd_file *nf = data;
113fc22945eSChuck Lever 
114fc22945eSChuck Lever 	return nfsd_file_inode_hash(nf->nf_inode, seed);
115fc22945eSChuck Lever }
116fc22945eSChuck Lever 
117fc22945eSChuck Lever static bool
118fc22945eSChuck Lever nfsd_match_cred(const struct cred *c1, const struct cred *c2)
119fc22945eSChuck Lever {
120fc22945eSChuck Lever 	int i;
121fc22945eSChuck Lever 
122fc22945eSChuck Lever 	if (!uid_eq(c1->fsuid, c2->fsuid))
123fc22945eSChuck Lever 		return false;
124fc22945eSChuck Lever 	if (!gid_eq(c1->fsgid, c2->fsgid))
125fc22945eSChuck Lever 		return false;
126fc22945eSChuck Lever 	if (c1->group_info == NULL || c2->group_info == NULL)
127fc22945eSChuck Lever 		return c1->group_info == c2->group_info;
128fc22945eSChuck Lever 	if (c1->group_info->ngroups != c2->group_info->ngroups)
129fc22945eSChuck Lever 		return false;
130fc22945eSChuck Lever 	for (i = 0; i < c1->group_info->ngroups; i++) {
131fc22945eSChuck Lever 		if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
132fc22945eSChuck Lever 			return false;
133fc22945eSChuck Lever 	}
134fc22945eSChuck Lever 	return true;
135fc22945eSChuck Lever }
136fc22945eSChuck Lever 
137fc22945eSChuck Lever /**
138fc22945eSChuck Lever  * nfsd_file_obj_cmpfn - Match a cache item against search criteria
139fc22945eSChuck Lever  * @arg: search criteria
140fc22945eSChuck Lever  * @ptr: cache item to check
141fc22945eSChuck Lever  *
142fc22945eSChuck Lever  * Return values:
143fc22945eSChuck Lever  *   %0 - Item matches search criteria
144fc22945eSChuck Lever  *   %1 - Item does not match search criteria
145fc22945eSChuck Lever  */
146fc22945eSChuck Lever static int nfsd_file_obj_cmpfn(struct rhashtable_compare_arg *arg,
147fc22945eSChuck Lever 			       const void *ptr)
148fc22945eSChuck Lever {
149fc22945eSChuck Lever 	const struct nfsd_file_lookup_key *key = arg->key;
150fc22945eSChuck Lever 	const struct nfsd_file *nf = ptr;
151fc22945eSChuck Lever 
152fc22945eSChuck Lever 	switch (key->type) {
153fc22945eSChuck Lever 	case NFSD_FILE_KEY_INODE:
154fc22945eSChuck Lever 		if (nf->nf_inode != key->inode)
155fc22945eSChuck Lever 			return 1;
156fc22945eSChuck Lever 		break;
157fc22945eSChuck Lever 	case NFSD_FILE_KEY_FULL:
158fc22945eSChuck Lever 		if (nf->nf_inode != key->inode)
159fc22945eSChuck Lever 			return 1;
160fc22945eSChuck Lever 		if (nf->nf_may != key->need)
161fc22945eSChuck Lever 			return 1;
162fc22945eSChuck Lever 		if (nf->nf_net != key->net)
163fc22945eSChuck Lever 			return 1;
164fc22945eSChuck Lever 		if (!nfsd_match_cred(nf->nf_cred, key->cred))
165fc22945eSChuck Lever 			return 1;
166*4d1ea845SChuck Lever 		if (!!test_bit(NFSD_FILE_GC, &nf->nf_flags) != key->gc)
167*4d1ea845SChuck Lever 			return 1;
168fc22945eSChuck Lever 		if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0)
169fc22945eSChuck Lever 			return 1;
170fc22945eSChuck Lever 		break;
171fc22945eSChuck Lever 	}
172fc22945eSChuck Lever 	return 0;
173fc22945eSChuck Lever }
174fc22945eSChuck Lever 
175fc22945eSChuck Lever static const struct rhashtable_params nfsd_file_rhash_params = {
176fc22945eSChuck Lever 	.key_len		= sizeof_field(struct nfsd_file, nf_inode),
177fc22945eSChuck Lever 	.key_offset		= offsetof(struct nfsd_file, nf_inode),
178fc22945eSChuck Lever 	.head_offset		= offsetof(struct nfsd_file, nf_rhash),
179fc22945eSChuck Lever 	.hashfn			= nfsd_file_key_hashfn,
180fc22945eSChuck Lever 	.obj_hashfn		= nfsd_file_obj_hashfn,
181fc22945eSChuck Lever 	.obj_cmpfn		= nfsd_file_obj_cmpfn,
182fc22945eSChuck Lever 	/* Reduce resizing churn on light workloads */
183fc22945eSChuck Lever 	.min_size		= 512,		/* buckets */
184fc22945eSChuck Lever 	.automatic_shrinking	= true,
185fc22945eSChuck Lever };
18665294c1fSJeff Layton 
18765294c1fSJeff Layton static void
1889542e6a6STrond Myklebust nfsd_file_schedule_laundrette(void)
18965294c1fSJeff Layton {
190ce502f81SChuck Lever 	if ((atomic_read(&nfsd_file_rhash_tbl.nelems) == 0) ||
191c7b824c3SChuck Lever 	    test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
19265294c1fSJeff Layton 		return;
19365294c1fSJeff Layton 
1949542e6a6STrond Myklebust 	queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
1959542e6a6STrond Myklebust 			NFSD_LAUNDRETTE_DELAY);
19665294c1fSJeff Layton }
19765294c1fSJeff Layton 
19865294c1fSJeff Layton static void
19965294c1fSJeff Layton nfsd_file_slab_free(struct rcu_head *rcu)
20065294c1fSJeff Layton {
20165294c1fSJeff Layton 	struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
20265294c1fSJeff Layton 
20365294c1fSJeff Layton 	put_cred(nf->nf_cred);
20465294c1fSJeff Layton 	kmem_cache_free(nfsd_file_slab, nf);
20565294c1fSJeff Layton }
20665294c1fSJeff Layton 
20765294c1fSJeff Layton static void
20865294c1fSJeff Layton nfsd_file_mark_free(struct fsnotify_mark *mark)
20965294c1fSJeff Layton {
21065294c1fSJeff Layton 	struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
21165294c1fSJeff Layton 						  nfm_mark);
21265294c1fSJeff Layton 
21365294c1fSJeff Layton 	kmem_cache_free(nfsd_file_mark_slab, nfm);
21465294c1fSJeff Layton }
21565294c1fSJeff Layton 
21665294c1fSJeff Layton static struct nfsd_file_mark *
21765294c1fSJeff Layton nfsd_file_mark_get(struct nfsd_file_mark *nfm)
21865294c1fSJeff Layton {
219689827cdSTrond Myklebust 	if (!refcount_inc_not_zero(&nfm->nfm_ref))
22065294c1fSJeff Layton 		return NULL;
22165294c1fSJeff Layton 	return nfm;
22265294c1fSJeff Layton }
22365294c1fSJeff Layton 
22465294c1fSJeff Layton static void
22565294c1fSJeff Layton nfsd_file_mark_put(struct nfsd_file_mark *nfm)
22665294c1fSJeff Layton {
227689827cdSTrond Myklebust 	if (refcount_dec_and_test(&nfm->nfm_ref)) {
22865294c1fSJeff Layton 		fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
22965294c1fSJeff Layton 		fsnotify_put_mark(&nfm->nfm_mark);
23065294c1fSJeff Layton 	}
23165294c1fSJeff Layton }
23265294c1fSJeff Layton 
23365294c1fSJeff Layton static struct nfsd_file_mark *
234427f5f83SChuck Lever nfsd_file_mark_find_or_create(struct nfsd_file *nf, struct inode *inode)
23565294c1fSJeff Layton {
23665294c1fSJeff Layton 	int			err;
23765294c1fSJeff Layton 	struct fsnotify_mark	*mark;
23865294c1fSJeff Layton 	struct nfsd_file_mark	*nfm = NULL, *new;
23965294c1fSJeff Layton 
24065294c1fSJeff Layton 	do {
241b8962a9dSAmir Goldstein 		fsnotify_group_lock(nfsd_file_fsnotify_group);
24265294c1fSJeff Layton 		mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
24365294c1fSJeff Layton 					  nfsd_file_fsnotify_group);
24465294c1fSJeff Layton 		if (mark) {
24565294c1fSJeff Layton 			nfm = nfsd_file_mark_get(container_of(mark,
24665294c1fSJeff Layton 						 struct nfsd_file_mark,
24765294c1fSJeff Layton 						 nfm_mark));
248b8962a9dSAmir Goldstein 			fsnotify_group_unlock(nfsd_file_fsnotify_group);
24990d2f1daSTrond Myklebust 			if (nfm) {
25065294c1fSJeff Layton 				fsnotify_put_mark(mark);
25165294c1fSJeff Layton 				break;
25290d2f1daSTrond Myklebust 			}
25390d2f1daSTrond Myklebust 			/* Avoid soft lockup race with nfsd_file_mark_put() */
25490d2f1daSTrond Myklebust 			fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
25590d2f1daSTrond Myklebust 			fsnotify_put_mark(mark);
256b8962a9dSAmir Goldstein 		} else {
257b8962a9dSAmir Goldstein 			fsnotify_group_unlock(nfsd_file_fsnotify_group);
258b8962a9dSAmir Goldstein 		}
25965294c1fSJeff Layton 
26065294c1fSJeff Layton 		/* allocate a new nfm */
26165294c1fSJeff Layton 		new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
26265294c1fSJeff Layton 		if (!new)
26365294c1fSJeff Layton 			return NULL;
26465294c1fSJeff Layton 		fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
26565294c1fSJeff Layton 		new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
266689827cdSTrond Myklebust 		refcount_set(&new->nfm_ref, 1);
26765294c1fSJeff Layton 
26865294c1fSJeff Layton 		err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
26965294c1fSJeff Layton 
27065294c1fSJeff Layton 		/*
27165294c1fSJeff Layton 		 * If the add was successful, then return the object.
27265294c1fSJeff Layton 		 * Otherwise, we need to put the reference we hold on the
27365294c1fSJeff Layton 		 * nfm_mark. The fsnotify code will take a reference and put
27465294c1fSJeff Layton 		 * it on failure, so we can't just free it directly. It's also
27565294c1fSJeff Layton 		 * not safe to call fsnotify_destroy_mark on it as the
27665294c1fSJeff Layton 		 * mark->group will be NULL. Thus, we can't let the nfm_ref
27765294c1fSJeff Layton 		 * counter drive the destruction at this point.
27865294c1fSJeff Layton 		 */
27965294c1fSJeff Layton 		if (likely(!err))
28065294c1fSJeff Layton 			nfm = new;
28165294c1fSJeff Layton 		else
28265294c1fSJeff Layton 			fsnotify_put_mark(&new->nfm_mark);
28365294c1fSJeff Layton 	} while (unlikely(err == -EEXIST));
28465294c1fSJeff Layton 
28565294c1fSJeff Layton 	return nfm;
28665294c1fSJeff Layton }
28765294c1fSJeff Layton 
28865294c1fSJeff Layton static struct nfsd_file *
289ce502f81SChuck Lever nfsd_file_alloc(struct nfsd_file_lookup_key *key, unsigned int may)
29065294c1fSJeff Layton {
29165294c1fSJeff Layton 	struct nfsd_file *nf;
29265294c1fSJeff Layton 
29365294c1fSJeff Layton 	nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
29465294c1fSJeff Layton 	if (nf) {
29565294c1fSJeff Layton 		INIT_LIST_HEAD(&nf->nf_lru);
296904940e9SChuck Lever 		nf->nf_birthtime = ktime_get();
29765294c1fSJeff Layton 		nf->nf_file = NULL;
29865294c1fSJeff Layton 		nf->nf_cred = get_current_cred();
299ce502f81SChuck Lever 		nf->nf_net = key->net;
30065294c1fSJeff Layton 		nf->nf_flags = 0;
301ce502f81SChuck Lever 		__set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
302ce502f81SChuck Lever 		__set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
303*4d1ea845SChuck Lever 		if (key->gc)
304*4d1ea845SChuck Lever 			__set_bit(NFSD_FILE_GC, &nf->nf_flags);
305ce502f81SChuck Lever 		nf->nf_inode = key->inode;
306ce502f81SChuck Lever 		/* nf_ref is pre-incremented for hash table */
307ce502f81SChuck Lever 		refcount_set(&nf->nf_ref, 2);
308ce502f81SChuck Lever 		nf->nf_may = key->need;
30965294c1fSJeff Layton 		nf->nf_mark = NULL;
31065294c1fSJeff Layton 	}
31165294c1fSJeff Layton 	return nf;
31265294c1fSJeff Layton }
31365294c1fSJeff Layton 
31465294c1fSJeff Layton static bool
31565294c1fSJeff Layton nfsd_file_free(struct nfsd_file *nf)
31665294c1fSJeff Layton {
317904940e9SChuck Lever 	s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime));
31865294c1fSJeff Layton 	bool flush = false;
31965294c1fSJeff Layton 
320d6329327SChuck Lever 	this_cpu_inc(nfsd_file_releases);
321904940e9SChuck Lever 	this_cpu_add(nfsd_file_total_age, age);
322d6329327SChuck Lever 
32365294c1fSJeff Layton 	trace_nfsd_file_put_final(nf);
32465294c1fSJeff Layton 	if (nf->nf_mark)
32565294c1fSJeff Layton 		nfsd_file_mark_put(nf->nf_mark);
32665294c1fSJeff Layton 	if (nf->nf_file) {
32765294c1fSJeff Layton 		get_file(nf->nf_file);
32865294c1fSJeff Layton 		filp_close(nf->nf_file, NULL);
32965294c1fSJeff Layton 		fput(nf->nf_file);
33065294c1fSJeff Layton 		flush = true;
33165294c1fSJeff Layton 	}
332668ed92eSChuck Lever 
333668ed92eSChuck Lever 	/*
334668ed92eSChuck Lever 	 * If this item is still linked via nf_lru, that's a bug.
335668ed92eSChuck Lever 	 * WARN and leak it to preserve system stability.
336668ed92eSChuck Lever 	 */
337668ed92eSChuck Lever 	if (WARN_ON_ONCE(!list_empty(&nf->nf_lru)))
338668ed92eSChuck Lever 		return flush;
339668ed92eSChuck Lever 
34065294c1fSJeff Layton 	call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
34165294c1fSJeff Layton 	return flush;
34265294c1fSJeff Layton }
34365294c1fSJeff Layton 
344055b24a8STrond Myklebust static bool
345055b24a8STrond Myklebust nfsd_file_check_writeback(struct nfsd_file *nf)
346055b24a8STrond Myklebust {
347055b24a8STrond Myklebust 	struct file *file = nf->nf_file;
348055b24a8STrond Myklebust 	struct address_space *mapping;
349055b24a8STrond Myklebust 
350055b24a8STrond Myklebust 	if (!file || !(file->f_mode & FMODE_WRITE))
351055b24a8STrond Myklebust 		return false;
352055b24a8STrond Myklebust 	mapping = file->f_mapping;
353055b24a8STrond Myklebust 	return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
354055b24a8STrond Myklebust 		mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
355055b24a8STrond Myklebust }
356055b24a8STrond Myklebust 
357055b24a8STrond Myklebust static int
358055b24a8STrond Myklebust nfsd_file_check_write_error(struct nfsd_file *nf)
359055b24a8STrond Myklebust {
360055b24a8STrond Myklebust 	struct file *file = nf->nf_file;
361055b24a8STrond Myklebust 
362055b24a8STrond Myklebust 	if (!file || !(file->f_mode & FMODE_WRITE))
363055b24a8STrond Myklebust 		return 0;
364055b24a8STrond Myklebust 	return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
365055b24a8STrond Myklebust }
366055b24a8STrond Myklebust 
36765294c1fSJeff Layton static void
3686b8a9433STrond Myklebust nfsd_file_flush(struct nfsd_file *nf)
3696b8a9433STrond Myklebust {
370df2aff52SChuck Lever 	struct file *file = nf->nf_file;
371df2aff52SChuck Lever 
372df2aff52SChuck Lever 	if (!file || !(file->f_mode & FMODE_WRITE))
373df2aff52SChuck Lever 		return;
374df2aff52SChuck Lever 	this_cpu_add(nfsd_file_pages_flushed, file->f_mapping->nrpages);
375df2aff52SChuck Lever 	if (vfs_fsync(file, 1) != 0)
3766b8a9433STrond Myklebust 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
3776b8a9433STrond Myklebust }
3786b8a9433STrond Myklebust 
379c46203acSChuck Lever static void nfsd_file_lru_add(struct nfsd_file *nf)
38065294c1fSJeff Layton {
3814a0e73e6SChuck Lever 	set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
382c46203acSChuck Lever 	if (list_lru_add(&nfsd_file_lru, &nf->nf_lru))
383c46203acSChuck Lever 		trace_nfsd_file_lru_add(nf);
384c46203acSChuck Lever }
38565294c1fSJeff Layton 
386c46203acSChuck Lever static void nfsd_file_lru_remove(struct nfsd_file *nf)
387c46203acSChuck Lever {
388c46203acSChuck Lever 	if (list_lru_del(&nfsd_file_lru, &nf->nf_lru))
389c46203acSChuck Lever 		trace_nfsd_file_lru_del(nf);
390c46203acSChuck Lever }
391c46203acSChuck Lever 
39265294c1fSJeff Layton static void
393ce502f81SChuck Lever nfsd_file_hash_remove(struct nfsd_file *nf)
39465294c1fSJeff Layton {
39565294c1fSJeff Layton 	trace_nfsd_file_unhash(nf);
39665294c1fSJeff Layton 
397055b24a8STrond Myklebust 	if (nfsd_file_check_write_error(nf))
3983988a578SChuck Lever 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
399ce502f81SChuck Lever 	rhashtable_remove_fast(&nfsd_file_rhash_tbl, &nf->nf_rhash,
400ce502f81SChuck Lever 			       nfsd_file_rhash_params);
40165294c1fSJeff Layton }
40265294c1fSJeff Layton 
40365294c1fSJeff Layton static bool
40465294c1fSJeff Layton nfsd_file_unhash(struct nfsd_file *nf)
40565294c1fSJeff Layton {
40665294c1fSJeff Layton 	if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
407ce502f81SChuck Lever 		nfsd_file_hash_remove(nf);
40865294c1fSJeff Layton 		return true;
40965294c1fSJeff Layton 	}
41065294c1fSJeff Layton 	return false;
41165294c1fSJeff Layton }
41265294c1fSJeff Layton 
4138d0d254bSJeff Layton static void
414ce502f81SChuck Lever nfsd_file_unhash_and_dispose(struct nfsd_file *nf, struct list_head *dispose)
41565294c1fSJeff Layton {
416ce502f81SChuck Lever 	trace_nfsd_file_unhash_and_dispose(nf);
4178d0d254bSJeff Layton 	if (nfsd_file_unhash(nf)) {
4188d0d254bSJeff Layton 		/* caller must call nfsd_file_dispose_list() later */
4194a0e73e6SChuck Lever 		nfsd_file_lru_remove(nf);
42065294c1fSJeff Layton 		list_add(&nf->nf_lru, dispose);
4218d0d254bSJeff Layton 	}
42265294c1fSJeff Layton }
42365294c1fSJeff Layton 
424b6669305STrond Myklebust static void
42565294c1fSJeff Layton nfsd_file_put_noref(struct nfsd_file *nf)
42665294c1fSJeff Layton {
42765294c1fSJeff Layton 	trace_nfsd_file_put(nf);
42865294c1fSJeff Layton 
429689827cdSTrond Myklebust 	if (refcount_dec_and_test(&nf->nf_ref)) {
43065294c1fSJeff Layton 		WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
4314a0e73e6SChuck Lever 		nfsd_file_lru_remove(nf);
43265294c1fSJeff Layton 		nfsd_file_free(nf);
43365294c1fSJeff Layton 	}
43465294c1fSJeff Layton }
43565294c1fSJeff Layton 
436*4d1ea845SChuck Lever static void
437*4d1ea845SChuck Lever nfsd_file_unhash_and_put(struct nfsd_file *nf)
438*4d1ea845SChuck Lever {
439*4d1ea845SChuck Lever 	if (nfsd_file_unhash(nf))
440*4d1ea845SChuck Lever 		nfsd_file_put_noref(nf);
441*4d1ea845SChuck Lever }
442*4d1ea845SChuck Lever 
44365294c1fSJeff Layton void
44465294c1fSJeff Layton nfsd_file_put(struct nfsd_file *nf)
44565294c1fSJeff Layton {
44608af54b3SChuck Lever 	might_sleep();
44708af54b3SChuck Lever 
448*4d1ea845SChuck Lever 	if (test_bit(NFSD_FILE_GC, &nf->nf_flags))
4494a0e73e6SChuck Lever 		nfsd_file_lru_add(nf);
450*4d1ea845SChuck Lever 	else if (refcount_read(&nf->nf_ref) == 2)
451*4d1ea845SChuck Lever 		nfsd_file_unhash_and_put(nf);
452*4d1ea845SChuck Lever 
453*4d1ea845SChuck Lever 	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
4546b8a9433STrond Myklebust 		nfsd_file_flush(nf);
455b6669305STrond Myklebust 		nfsd_file_put_noref(nf);
456*4d1ea845SChuck Lever 	} else if (nf->nf_file && test_bit(NFSD_FILE_GC, &nf->nf_flags)) {
457b6669305STrond Myklebust 		nfsd_file_put_noref(nf);
4589542e6a6STrond Myklebust 		nfsd_file_schedule_laundrette();
459b6c71c66SChuck Lever 	} else
460b6c71c66SChuck Lever 		nfsd_file_put_noref(nf);
46165294c1fSJeff Layton }
46265294c1fSJeff Layton 
46365294c1fSJeff Layton struct nfsd_file *
46465294c1fSJeff Layton nfsd_file_get(struct nfsd_file *nf)
46565294c1fSJeff Layton {
466689827cdSTrond Myklebust 	if (likely(refcount_inc_not_zero(&nf->nf_ref)))
46765294c1fSJeff Layton 		return nf;
46865294c1fSJeff Layton 	return NULL;
46965294c1fSJeff Layton }
47065294c1fSJeff Layton 
47165294c1fSJeff Layton static void
47265294c1fSJeff Layton nfsd_file_dispose_list(struct list_head *dispose)
47365294c1fSJeff Layton {
47465294c1fSJeff Layton 	struct nfsd_file *nf;
47565294c1fSJeff Layton 
47665294c1fSJeff Layton 	while(!list_empty(dispose)) {
47765294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
478668ed92eSChuck Lever 		list_del_init(&nf->nf_lru);
4796b8a9433STrond Myklebust 		nfsd_file_flush(nf);
48065294c1fSJeff Layton 		nfsd_file_put_noref(nf);
48165294c1fSJeff Layton 	}
48265294c1fSJeff Layton }
48365294c1fSJeff Layton 
48465294c1fSJeff Layton static void
48565294c1fSJeff Layton nfsd_file_dispose_list_sync(struct list_head *dispose)
48665294c1fSJeff Layton {
48765294c1fSJeff Layton 	bool flush = false;
48865294c1fSJeff Layton 	struct nfsd_file *nf;
48965294c1fSJeff Layton 
49065294c1fSJeff Layton 	while(!list_empty(dispose)) {
49165294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
492668ed92eSChuck Lever 		list_del_init(&nf->nf_lru);
4936b8a9433STrond Myklebust 		nfsd_file_flush(nf);
494689827cdSTrond Myklebust 		if (!refcount_dec_and_test(&nf->nf_ref))
49565294c1fSJeff Layton 			continue;
49665294c1fSJeff Layton 		if (nfsd_file_free(nf))
49765294c1fSJeff Layton 			flush = true;
49865294c1fSJeff Layton 	}
49965294c1fSJeff Layton 	if (flush)
50065294c1fSJeff Layton 		flush_delayed_fput();
50165294c1fSJeff Layton }
50265294c1fSJeff Layton 
5039542e6a6STrond Myklebust static void
5049542e6a6STrond Myklebust nfsd_file_list_remove_disposal(struct list_head *dst,
5059542e6a6STrond Myklebust 		struct nfsd_fcache_disposal *l)
5069542e6a6STrond Myklebust {
5079542e6a6STrond Myklebust 	spin_lock(&l->lock);
5089542e6a6STrond Myklebust 	list_splice_init(&l->freeme, dst);
5099542e6a6STrond Myklebust 	spin_unlock(&l->lock);
5109542e6a6STrond Myklebust }
5119542e6a6STrond Myklebust 
5129542e6a6STrond Myklebust static void
5139542e6a6STrond Myklebust nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
5149542e6a6STrond Myklebust {
5151463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5161463b38eSNeilBrown 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
5179542e6a6STrond Myklebust 
5189542e6a6STrond Myklebust 	spin_lock(&l->lock);
5199542e6a6STrond Myklebust 	list_splice_tail_init(files, &l->freeme);
5209542e6a6STrond Myklebust 	spin_unlock(&l->lock);
5219542e6a6STrond Myklebust 	queue_work(nfsd_filecache_wq, &l->work);
5229542e6a6STrond Myklebust }
5239542e6a6STrond Myklebust 
5249542e6a6STrond Myklebust static void
5259542e6a6STrond Myklebust nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
5269542e6a6STrond Myklebust 		struct net *net)
5279542e6a6STrond Myklebust {
5289542e6a6STrond Myklebust 	struct nfsd_file *nf, *tmp;
5299542e6a6STrond Myklebust 
5309542e6a6STrond Myklebust 	list_for_each_entry_safe(nf, tmp, src, nf_lru) {
5319542e6a6STrond Myklebust 		if (nf->nf_net == net)
5329542e6a6STrond Myklebust 			list_move_tail(&nf->nf_lru, dst);
5339542e6a6STrond Myklebust 	}
5349542e6a6STrond Myklebust }
5359542e6a6STrond Myklebust 
5369542e6a6STrond Myklebust static void
5379542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(struct list_head *dispose)
5389542e6a6STrond Myklebust {
5399542e6a6STrond Myklebust 	LIST_HEAD(list);
5409542e6a6STrond Myklebust 	struct nfsd_file *nf;
5419542e6a6STrond Myklebust 
5429542e6a6STrond Myklebust 	while(!list_empty(dispose)) {
5439542e6a6STrond Myklebust 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
5449542e6a6STrond Myklebust 		nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
5459542e6a6STrond Myklebust 		nfsd_file_list_add_disposal(&list, nf->nf_net);
5469542e6a6STrond Myklebust 	}
5479542e6a6STrond Myklebust }
5489542e6a6STrond Myklebust 
5494a0e73e6SChuck Lever /**
5504a0e73e6SChuck Lever  * nfsd_file_lru_cb - Examine an entry on the LRU list
5514a0e73e6SChuck Lever  * @item: LRU entry to examine
5524a0e73e6SChuck Lever  * @lru: controlling LRU
5534a0e73e6SChuck Lever  * @lock: LRU list lock (unused)
5544a0e73e6SChuck Lever  * @arg: dispose list
5554a0e73e6SChuck Lever  *
5564a0e73e6SChuck Lever  * Return values:
5574a0e73e6SChuck Lever  *   %LRU_REMOVED: @item was removed from the LRU
558edead3a5SChuck Lever  *   %LRU_ROTATE: @item is to be moved to the LRU tail
5594a0e73e6SChuck Lever  *   %LRU_SKIP: @item cannot be evicted
56065294c1fSJeff Layton  */
56165294c1fSJeff Layton static enum lru_status
56265294c1fSJeff Layton nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
56365294c1fSJeff Layton 		 spinlock_t *lock, void *arg)
56465294c1fSJeff Layton 	__releases(lock)
56565294c1fSJeff Layton 	__acquires(lock)
56665294c1fSJeff Layton {
56765294c1fSJeff Layton 	struct list_head *head = arg;
56865294c1fSJeff Layton 	struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
56965294c1fSJeff Layton 
57065294c1fSJeff Layton 	/*
57165294c1fSJeff Layton 	 * Do a lockless refcount check. The hashtable holds one reference, so
57265294c1fSJeff Layton 	 * we look to see if anything else has a reference, or if any have
57365294c1fSJeff Layton 	 * been put since the shrinker last ran. Those don't get unhashed and
57465294c1fSJeff Layton 	 * released.
57565294c1fSJeff Layton 	 *
57665294c1fSJeff Layton 	 * Note that in the put path, we set the flag and then decrement the
57765294c1fSJeff Layton 	 * counter. Here we check the counter and then test and clear the flag.
57865294c1fSJeff Layton 	 * That order is deliberate to ensure that we can do this locklessly.
57965294c1fSJeff Layton 	 */
580c46203acSChuck Lever 	if (refcount_read(&nf->nf_ref) > 1) {
5814a0e73e6SChuck Lever 		list_lru_isolate(lru, &nf->nf_lru);
582c46203acSChuck Lever 		trace_nfsd_file_gc_in_use(nf);
5834a0e73e6SChuck Lever 		return LRU_REMOVED;
584c46203acSChuck Lever 	}
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 
595c46203acSChuck Lever 	if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
596c46203acSChuck Lever 		trace_nfsd_file_gc_referenced(nf);
597edead3a5SChuck Lever 		return LRU_ROTATE;
59865294c1fSJeff Layton 	}
59965294c1fSJeff Layton 
600c46203acSChuck Lever 	if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
601c46203acSChuck Lever 		trace_nfsd_file_gc_hashed(nf);
602c46203acSChuck Lever 		return LRU_SKIP;
603c46203acSChuck Lever 	}
60465294c1fSJeff Layton 
60565294c1fSJeff Layton 	list_lru_isolate_move(lru, &nf->nf_lru, head);
60694660cc1SChuck Lever 	this_cpu_inc(nfsd_file_evictions);
607c46203acSChuck Lever 	trace_nfsd_file_gc_disposed(nf);
60865294c1fSJeff Layton 	return LRU_REMOVED;
60965294c1fSJeff Layton }
61065294c1fSJeff Layton 
6110bac5a26SChuck Lever /*
6120bac5a26SChuck Lever  * Unhash items on @dispose immediately, then queue them on the
6130bac5a26SChuck Lever  * disposal workqueue to finish releasing them in the background.
6140bac5a26SChuck Lever  *
6150bac5a26SChuck Lever  * cel: Note that between the time list_lru_shrink_walk runs and
6160bac5a26SChuck Lever  * now, these items are in the hash table but marked unhashed.
6170bac5a26SChuck Lever  * Why release these outside of lru_cb ? There's no lock ordering
6180bac5a26SChuck Lever  * problem since lru_cb currently takes no lock.
6190bac5a26SChuck Lever  */
6200bac5a26SChuck Lever static void nfsd_file_gc_dispose_list(struct list_head *dispose)
6210bac5a26SChuck Lever {
6220bac5a26SChuck Lever 	struct nfsd_file *nf;
6230bac5a26SChuck Lever 
624cb7ec76eSChuck Lever 	list_for_each_entry(nf, dispose, nf_lru)
625cb7ec76eSChuck Lever 		nfsd_file_hash_remove(nf);
6260bac5a26SChuck Lever 	nfsd_file_dispose_list_delayed(dispose);
6279542e6a6STrond Myklebust }
6289542e6a6STrond Myklebust 
6299542e6a6STrond Myklebust static void
6309542e6a6STrond Myklebust nfsd_file_gc(void)
6319542e6a6STrond Myklebust {
6323bc6d347SChuck Lever 	LIST_HEAD(dispose);
63394660cc1SChuck Lever 	unsigned long ret;
6343bc6d347SChuck Lever 
63594660cc1SChuck Lever 	ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
636edead3a5SChuck Lever 			    &dispose, list_lru_count(&nfsd_file_lru));
63794660cc1SChuck Lever 	trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
6383bc6d347SChuck Lever 	nfsd_file_gc_dispose_list(&dispose);
6399542e6a6STrond Myklebust }
6409542e6a6STrond Myklebust 
6419542e6a6STrond Myklebust static void
6429542e6a6STrond Myklebust nfsd_file_gc_worker(struct work_struct *work)
6439542e6a6STrond Myklebust {
6449542e6a6STrond Myklebust 	nfsd_file_gc();
6459542e6a6STrond Myklebust 	nfsd_file_schedule_laundrette();
64665294c1fSJeff Layton }
64765294c1fSJeff Layton 
64865294c1fSJeff Layton static unsigned long
64965294c1fSJeff Layton nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
65065294c1fSJeff Layton {
65165294c1fSJeff Layton 	return list_lru_count(&nfsd_file_lru);
65265294c1fSJeff Layton }
65365294c1fSJeff Layton 
65465294c1fSJeff Layton static unsigned long
65565294c1fSJeff Layton nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
65665294c1fSJeff Layton {
65739f1d1ffSChuck Lever 	LIST_HEAD(dispose);
65839f1d1ffSChuck Lever 	unsigned long ret;
65939f1d1ffSChuck Lever 
66039f1d1ffSChuck Lever 	ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
66139f1d1ffSChuck Lever 				   nfsd_file_lru_cb, &dispose);
66294660cc1SChuck Lever 	trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
66339f1d1ffSChuck Lever 	nfsd_file_gc_dispose_list(&dispose);
66439f1d1ffSChuck Lever 	return ret;
66565294c1fSJeff Layton }
66665294c1fSJeff Layton 
66765294c1fSJeff Layton static struct shrinker	nfsd_file_shrinker = {
66865294c1fSJeff Layton 	.scan_objects = nfsd_file_lru_scan,
66965294c1fSJeff Layton 	.count_objects = nfsd_file_lru_count,
67065294c1fSJeff Layton 	.seeks = 1,
67165294c1fSJeff Layton };
67265294c1fSJeff Layton 
673a8455110SChuck Lever /*
674a8455110SChuck Lever  * Find all cache items across all net namespaces that match @inode and
675a8455110SChuck Lever  * move them to @dispose. The lookup is atomic wrt nfsd_file_acquire().
676a8455110SChuck Lever  */
677a8455110SChuck Lever static unsigned int
678a8455110SChuck Lever __nfsd_file_close_inode(struct inode *inode, struct list_head *dispose)
67965294c1fSJeff Layton {
680ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
681ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_INODE,
682ce502f81SChuck Lever 		.inode	= inode,
683ce502f81SChuck Lever 	};
684a8455110SChuck Lever 	unsigned int count = 0;
68565294c1fSJeff Layton 	struct nfsd_file *nf;
68665294c1fSJeff Layton 
687ce502f81SChuck Lever 	rcu_read_lock();
688ce502f81SChuck Lever 	do {
689ce502f81SChuck Lever 		nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
690ce502f81SChuck Lever 				       nfsd_file_rhash_params);
691ce502f81SChuck Lever 		if (!nf)
692ce502f81SChuck Lever 			break;
693ce502f81SChuck Lever 		nfsd_file_unhash_and_dispose(nf, dispose);
694a8455110SChuck Lever 		count++;
695ce502f81SChuck Lever 	} while (1);
696ce502f81SChuck Lever 	rcu_read_unlock();
697a8455110SChuck Lever 	return count;
69865294c1fSJeff Layton }
69965294c1fSJeff Layton 
70065294c1fSJeff Layton /**
70165294c1fSJeff Layton  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
70265294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
70365294c1fSJeff Layton  *
704a8455110SChuck Lever  * Unhash and put, then flush and fput all cache items associated with @inode.
70565294c1fSJeff Layton  */
70665294c1fSJeff Layton void
70765294c1fSJeff Layton nfsd_file_close_inode_sync(struct inode *inode)
70865294c1fSJeff Layton {
70965294c1fSJeff Layton 	LIST_HEAD(dispose);
710a8455110SChuck Lever 	unsigned int count;
71165294c1fSJeff Layton 
712a8455110SChuck Lever 	count = __nfsd_file_close_inode(inode, &dispose);
713a8455110SChuck Lever 	trace_nfsd_file_close_inode_sync(inode, count);
71465294c1fSJeff Layton 	nfsd_file_dispose_list_sync(&dispose);
71565294c1fSJeff Layton }
71665294c1fSJeff Layton 
71765294c1fSJeff Layton /**
71819598141STrond Myklebust  * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
71965294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
72065294c1fSJeff Layton  *
721a8455110SChuck Lever  * Unhash and put all cache item associated with @inode.
72265294c1fSJeff Layton  */
72365294c1fSJeff Layton static void
72465294c1fSJeff Layton nfsd_file_close_inode(struct inode *inode)
72565294c1fSJeff Layton {
72665294c1fSJeff Layton 	LIST_HEAD(dispose);
727a8455110SChuck Lever 	unsigned int count;
72865294c1fSJeff Layton 
729a8455110SChuck Lever 	count = __nfsd_file_close_inode(inode, &dispose);
730a8455110SChuck Lever 	trace_nfsd_file_close_inode(inode, count);
7319542e6a6STrond Myklebust 	nfsd_file_dispose_list_delayed(&dispose);
73265294c1fSJeff Layton }
73365294c1fSJeff Layton 
73465294c1fSJeff Layton /**
73565294c1fSJeff Layton  * nfsd_file_delayed_close - close unused nfsd_files
73665294c1fSJeff Layton  * @work: dummy
73765294c1fSJeff Layton  *
73865294c1fSJeff Layton  * Walk the LRU list and close any entries that have not been used since
73965294c1fSJeff Layton  * the last scan.
74065294c1fSJeff Layton  */
74165294c1fSJeff Layton static void
74265294c1fSJeff Layton nfsd_file_delayed_close(struct work_struct *work)
74365294c1fSJeff Layton {
74465294c1fSJeff Layton 	LIST_HEAD(head);
7459542e6a6STrond Myklebust 	struct nfsd_fcache_disposal *l = container_of(work,
7469542e6a6STrond Myklebust 			struct nfsd_fcache_disposal, work);
74765294c1fSJeff Layton 
7489542e6a6STrond Myklebust 	nfsd_file_list_remove_disposal(&head, l);
7499542e6a6STrond Myklebust 	nfsd_file_dispose_list(&head);
75065294c1fSJeff Layton }
75165294c1fSJeff Layton 
75265294c1fSJeff Layton static int
75365294c1fSJeff Layton nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
75465294c1fSJeff Layton 			    void *data)
75565294c1fSJeff Layton {
75665294c1fSJeff Layton 	struct file_lock *fl = data;
75765294c1fSJeff Layton 
75865294c1fSJeff Layton 	/* Only close files for F_SETLEASE leases */
75965294c1fSJeff Layton 	if (fl->fl_flags & FL_LEASE)
76065294c1fSJeff Layton 		nfsd_file_close_inode_sync(file_inode(fl->fl_file));
76165294c1fSJeff Layton 	return 0;
76265294c1fSJeff Layton }
76365294c1fSJeff Layton 
76465294c1fSJeff Layton static struct notifier_block nfsd_file_lease_notifier = {
76565294c1fSJeff Layton 	.notifier_call = nfsd_file_lease_notifier_call,
76665294c1fSJeff Layton };
76765294c1fSJeff Layton 
76865294c1fSJeff Layton static int
769b9a1b977SAmir Goldstein nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
770b9a1b977SAmir Goldstein 				struct inode *inode, struct inode *dir,
771950cc0d2SAmir Goldstein 				const struct qstr *name, u32 cookie)
77265294c1fSJeff Layton {
77324dca905SGabriel Krisman Bertazi 	if (WARN_ON_ONCE(!inode))
77424dca905SGabriel Krisman Bertazi 		return 0;
77524dca905SGabriel Krisman Bertazi 
77665294c1fSJeff Layton 	trace_nfsd_file_fsnotify_handle_event(inode, mask);
77765294c1fSJeff Layton 
77865294c1fSJeff Layton 	/* Should be no marks on non-regular files */
77965294c1fSJeff Layton 	if (!S_ISREG(inode->i_mode)) {
78065294c1fSJeff Layton 		WARN_ON_ONCE(1);
78165294c1fSJeff Layton 		return 0;
78265294c1fSJeff Layton 	}
78365294c1fSJeff Layton 
78465294c1fSJeff Layton 	/* don't close files if this was not the last link */
78565294c1fSJeff Layton 	if (mask & FS_ATTRIB) {
78665294c1fSJeff Layton 		if (inode->i_nlink)
78765294c1fSJeff Layton 			return 0;
78865294c1fSJeff Layton 	}
78965294c1fSJeff Layton 
79065294c1fSJeff Layton 	nfsd_file_close_inode(inode);
79165294c1fSJeff Layton 	return 0;
79265294c1fSJeff Layton }
79365294c1fSJeff Layton 
79465294c1fSJeff Layton 
79565294c1fSJeff Layton static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
796b9a1b977SAmir Goldstein 	.handle_inode_event = nfsd_file_fsnotify_handle_event,
79765294c1fSJeff Layton 	.free_mark = nfsd_file_mark_free,
79865294c1fSJeff Layton };
79965294c1fSJeff Layton 
80065294c1fSJeff Layton int
80165294c1fSJeff Layton nfsd_file_cache_init(void)
80265294c1fSJeff Layton {
803fc22945eSChuck Lever 	int ret;
80465294c1fSJeff Layton 
805c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
806c7b824c3SChuck Lever 	if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
80765294c1fSJeff Layton 		return 0;
80865294c1fSJeff Layton 
809fc22945eSChuck Lever 	ret = rhashtable_init(&nfsd_file_rhash_tbl, &nfsd_file_rhash_params);
810fc22945eSChuck Lever 	if (ret)
811fc22945eSChuck Lever 		return ret;
812fc22945eSChuck Lever 
813fc22945eSChuck Lever 	ret = -ENOMEM;
8149542e6a6STrond Myklebust 	nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
8159542e6a6STrond Myklebust 	if (!nfsd_filecache_wq)
8169542e6a6STrond Myklebust 		goto out;
8179542e6a6STrond Myklebust 
81865294c1fSJeff Layton 	nfsd_file_slab = kmem_cache_create("nfsd_file",
81965294c1fSJeff Layton 				sizeof(struct nfsd_file), 0, 0, NULL);
82065294c1fSJeff Layton 	if (!nfsd_file_slab) {
82165294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_slab\n");
82265294c1fSJeff Layton 		goto out_err;
82365294c1fSJeff Layton 	}
82465294c1fSJeff Layton 
82565294c1fSJeff Layton 	nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
82665294c1fSJeff Layton 					sizeof(struct nfsd_file_mark), 0, 0, NULL);
82765294c1fSJeff Layton 	if (!nfsd_file_mark_slab) {
82865294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
82965294c1fSJeff Layton 		goto out_err;
83065294c1fSJeff Layton 	}
83165294c1fSJeff Layton 
83265294c1fSJeff Layton 
83365294c1fSJeff Layton 	ret = list_lru_init(&nfsd_file_lru);
83465294c1fSJeff Layton 	if (ret) {
83565294c1fSJeff Layton 		pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
83665294c1fSJeff Layton 		goto out_err;
83765294c1fSJeff Layton 	}
83865294c1fSJeff Layton 
839e33c267aSRoman Gushchin 	ret = register_shrinker(&nfsd_file_shrinker, "nfsd-filecache");
84065294c1fSJeff Layton 	if (ret) {
84165294c1fSJeff Layton 		pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
84265294c1fSJeff Layton 		goto out_lru;
84365294c1fSJeff Layton 	}
84465294c1fSJeff Layton 
84565294c1fSJeff Layton 	ret = lease_register_notifier(&nfsd_file_lease_notifier);
84665294c1fSJeff Layton 	if (ret) {
84765294c1fSJeff Layton 		pr_err("nfsd: unable to register lease notifier: %d\n", ret);
84865294c1fSJeff Layton 		goto out_shrinker;
84965294c1fSJeff Layton 	}
85065294c1fSJeff Layton 
851867a448dSAmir Goldstein 	nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
852b8962a9dSAmir Goldstein 							FSNOTIFY_GROUP_NOFS);
85365294c1fSJeff Layton 	if (IS_ERR(nfsd_file_fsnotify_group)) {
85465294c1fSJeff Layton 		pr_err("nfsd: unable to create fsnotify group: %ld\n",
85565294c1fSJeff Layton 			PTR_ERR(nfsd_file_fsnotify_group));
856231307dfSHuang Guobin 		ret = PTR_ERR(nfsd_file_fsnotify_group);
85765294c1fSJeff Layton 		nfsd_file_fsnotify_group = NULL;
85865294c1fSJeff Layton 		goto out_notifier;
85965294c1fSJeff Layton 	}
86065294c1fSJeff Layton 
8619542e6a6STrond Myklebust 	INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
86265294c1fSJeff Layton out:
86365294c1fSJeff Layton 	return ret;
86465294c1fSJeff Layton out_notifier:
86565294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
86665294c1fSJeff Layton out_shrinker:
86765294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
86865294c1fSJeff Layton out_lru:
86965294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
87065294c1fSJeff Layton out_err:
87165294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
87265294c1fSJeff Layton 	nfsd_file_slab = NULL;
87365294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
87465294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
8759542e6a6STrond Myklebust 	destroy_workqueue(nfsd_filecache_wq);
8769542e6a6STrond Myklebust 	nfsd_filecache_wq = NULL;
877fc22945eSChuck Lever 	rhashtable_destroy(&nfsd_file_rhash_tbl);
87865294c1fSJeff Layton 	goto out;
87965294c1fSJeff Layton }
88065294c1fSJeff Layton 
881c7b824c3SChuck Lever static void
882c7b824c3SChuck Lever __nfsd_file_cache_purge(struct net *net)
88365294c1fSJeff Layton {
884ce502f81SChuck Lever 	struct rhashtable_iter iter;
88565294c1fSJeff Layton 	struct nfsd_file *nf;
88665294c1fSJeff Layton 	LIST_HEAD(dispose);
88765294c1fSJeff Layton 
888ce502f81SChuck Lever 	rhashtable_walk_enter(&nfsd_file_rhash_tbl, &iter);
889ce502f81SChuck Lever 	do {
890ce502f81SChuck Lever 		rhashtable_walk_start(&iter);
89165294c1fSJeff Layton 
892ce502f81SChuck Lever 		nf = rhashtable_walk_next(&iter);
893ce502f81SChuck Lever 		while (!IS_ERR_OR_NULL(nf)) {
894d3aefd2bSJeff Layton 			if (!net || nf->nf_net == net)
8958d0d254bSJeff Layton 				nfsd_file_unhash_and_dispose(nf, &dispose);
896ce502f81SChuck Lever 			nf = rhashtable_walk_next(&iter);
89765294c1fSJeff Layton 		}
898ce502f81SChuck Lever 
899ce502f81SChuck Lever 		rhashtable_walk_stop(&iter);
900ce502f81SChuck Lever 	} while (nf == ERR_PTR(-EAGAIN));
901ce502f81SChuck Lever 	rhashtable_walk_exit(&iter);
902ce502f81SChuck Lever 
90365294c1fSJeff Layton 	nfsd_file_dispose_list(&dispose);
90465294c1fSJeff Layton }
90565294c1fSJeff Layton 
9069542e6a6STrond Myklebust static struct nfsd_fcache_disposal *
9071463b38eSNeilBrown nfsd_alloc_fcache_disposal(void)
9089542e6a6STrond Myklebust {
9099542e6a6STrond Myklebust 	struct nfsd_fcache_disposal *l;
9109542e6a6STrond Myklebust 
9119542e6a6STrond Myklebust 	l = kmalloc(sizeof(*l), GFP_KERNEL);
9129542e6a6STrond Myklebust 	if (!l)
9139542e6a6STrond Myklebust 		return NULL;
9149542e6a6STrond Myklebust 	INIT_WORK(&l->work, nfsd_file_delayed_close);
9159542e6a6STrond Myklebust 	spin_lock_init(&l->lock);
9169542e6a6STrond Myklebust 	INIT_LIST_HEAD(&l->freeme);
9179542e6a6STrond Myklebust 	return l;
9189542e6a6STrond Myklebust }
9199542e6a6STrond Myklebust 
9209542e6a6STrond Myklebust static void
9219542e6a6STrond Myklebust nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
9229542e6a6STrond Myklebust {
9239542e6a6STrond Myklebust 	cancel_work_sync(&l->work);
9249542e6a6STrond Myklebust 	nfsd_file_dispose_list(&l->freeme);
9251463b38eSNeilBrown 	kfree(l);
9269542e6a6STrond Myklebust }
9279542e6a6STrond Myklebust 
9289542e6a6STrond Myklebust static void
9299542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(struct net *net)
9309542e6a6STrond Myklebust {
9311463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9321463b38eSNeilBrown 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
9339542e6a6STrond Myklebust 
9349542e6a6STrond Myklebust 	nfsd_free_fcache_disposal(l);
9359542e6a6STrond Myklebust }
9369542e6a6STrond Myklebust 
9379542e6a6STrond Myklebust int
9389542e6a6STrond Myklebust nfsd_file_cache_start_net(struct net *net)
9399542e6a6STrond Myklebust {
9401463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9411463b38eSNeilBrown 
9421463b38eSNeilBrown 	nn->fcache_disposal = nfsd_alloc_fcache_disposal();
9431463b38eSNeilBrown 	return nn->fcache_disposal ? 0 : -ENOMEM;
9449542e6a6STrond Myklebust }
9459542e6a6STrond Myklebust 
946c7b824c3SChuck Lever /**
947c7b824c3SChuck Lever  * nfsd_file_cache_purge - Remove all cache items associated with @net
948c7b824c3SChuck Lever  * @net: target net namespace
949c7b824c3SChuck Lever  *
950c7b824c3SChuck Lever  */
951c7b824c3SChuck Lever void
952c7b824c3SChuck Lever nfsd_file_cache_purge(struct net *net)
953c7b824c3SChuck Lever {
954c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
955c7b824c3SChuck Lever 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
956c7b824c3SChuck Lever 		__nfsd_file_cache_purge(net);
957c7b824c3SChuck Lever }
958c7b824c3SChuck Lever 
9599542e6a6STrond Myklebust void
9609542e6a6STrond Myklebust nfsd_file_cache_shutdown_net(struct net *net)
9619542e6a6STrond Myklebust {
9629542e6a6STrond Myklebust 	nfsd_file_cache_purge(net);
9639542e6a6STrond Myklebust 	nfsd_free_fcache_disposal_net(net);
9649542e6a6STrond Myklebust }
9659542e6a6STrond Myklebust 
96665294c1fSJeff Layton void
96765294c1fSJeff Layton nfsd_file_cache_shutdown(void)
96865294c1fSJeff Layton {
9698b330f78SChuck Lever 	int i;
9708b330f78SChuck Lever 
971c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
972c7b824c3SChuck Lever 	if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
973c7b824c3SChuck Lever 		return;
97465294c1fSJeff Layton 
97565294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
97665294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
97765294c1fSJeff Layton 	/*
97865294c1fSJeff Layton 	 * make sure all callers of nfsd_file_lru_cb are done before
97965294c1fSJeff Layton 	 * calling nfsd_file_cache_purge
98065294c1fSJeff Layton 	 */
98165294c1fSJeff Layton 	cancel_delayed_work_sync(&nfsd_filecache_laundrette);
982c7b824c3SChuck Lever 	__nfsd_file_cache_purge(NULL);
98365294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
98465294c1fSJeff Layton 	rcu_barrier();
98565294c1fSJeff Layton 	fsnotify_put_group(nfsd_file_fsnotify_group);
98665294c1fSJeff Layton 	nfsd_file_fsnotify_group = NULL;
98765294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
98865294c1fSJeff Layton 	nfsd_file_slab = NULL;
98965294c1fSJeff Layton 	fsnotify_wait_marks_destroyed();
99065294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
99165294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
9929542e6a6STrond Myklebust 	destroy_workqueue(nfsd_filecache_wq);
9939542e6a6STrond Myklebust 	nfsd_filecache_wq = NULL;
994fc22945eSChuck Lever 	rhashtable_destroy(&nfsd_file_rhash_tbl);
99565294c1fSJeff Layton 
9968b330f78SChuck Lever 	for_each_possible_cpu(i) {
9978b330f78SChuck Lever 		per_cpu(nfsd_file_cache_hits, i) = 0;
9988b330f78SChuck Lever 		per_cpu(nfsd_file_acquisitions, i) = 0;
9998b330f78SChuck Lever 		per_cpu(nfsd_file_releases, i) = 0;
10008b330f78SChuck Lever 		per_cpu(nfsd_file_total_age, i) = 0;
10018b330f78SChuck Lever 		per_cpu(nfsd_file_pages_flushed, i) = 0;
10028b330f78SChuck Lever 		per_cpu(nfsd_file_evictions, i) = 0;
100365294c1fSJeff Layton 	}
100465294c1fSJeff Layton }
100565294c1fSJeff Layton 
100665294c1fSJeff Layton /**
1007ce502f81SChuck Lever  * nfsd_file_is_cached - are there any cached open files for this inode?
1008ce502f81SChuck Lever  * @inode: inode to check
100965294c1fSJeff Layton  *
1010ce502f81SChuck Lever  * The lookup matches inodes in all net namespaces and is atomic wrt
1011ce502f81SChuck Lever  * nfsd_file_acquire().
1012ce502f81SChuck Lever  *
1013ce502f81SChuck Lever  * Return values:
1014ce502f81SChuck Lever  *   %true: filecache contains at least one file matching this inode
1015ce502f81SChuck Lever  *   %false: filecache contains no files matching this inode
101665294c1fSJeff Layton  */
101765294c1fSJeff Layton bool
101865294c1fSJeff Layton nfsd_file_is_cached(struct inode *inode)
101965294c1fSJeff Layton {
1020ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
1021ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_INODE,
1022ce502f81SChuck Lever 		.inode	= inode,
1023ce502f81SChuck Lever 	};
102465294c1fSJeff Layton 	bool ret = false;
102565294c1fSJeff Layton 
1026ce502f81SChuck Lever 	if (rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key,
1027ce502f81SChuck Lever 				   nfsd_file_rhash_params) != NULL)
102865294c1fSJeff Layton 		ret = true;
102954f7df70SChuck Lever 	trace_nfsd_file_is_cached(inode, (int)ret);
103065294c1fSJeff Layton 	return ret;
103165294c1fSJeff Layton }
103265294c1fSJeff Layton 
1033fb70bf12SChuck Lever static __be32
1034be023006SChuck Lever nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1035*4d1ea845SChuck Lever 		     unsigned int may_flags, struct nfsd_file **pnf,
1036*4d1ea845SChuck Lever 		     bool open, bool want_gc)
103765294c1fSJeff Layton {
1038ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
1039ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_FULL,
1040ce502f81SChuck Lever 		.need	= may_flags & NFSD_FILE_MAY_MASK,
1041ce502f81SChuck Lever 		.net	= SVC_NET(rqstp),
1042*4d1ea845SChuck Lever 		.gc	= want_gc,
1043ce502f81SChuck Lever 	};
1044243a5263SJeff Layton 	bool open_retry = true;
1045243a5263SJeff Layton 	struct nfsd_file *nf;
1046ce502f81SChuck Lever 	__be32 status;
1047243a5263SJeff Layton 	int ret;
104865294c1fSJeff Layton 
104965294c1fSJeff Layton 	status = fh_verify(rqstp, fhp, S_IFREG,
105065294c1fSJeff Layton 				may_flags|NFSD_MAY_OWNER_OVERRIDE);
105165294c1fSJeff Layton 	if (status != nfs_ok)
105265294c1fSJeff Layton 		return status;
1053ce502f81SChuck Lever 	key.inode = d_inode(fhp->fh_dentry);
1054ce502f81SChuck Lever 	key.cred = get_current_cred();
105565294c1fSJeff Layton 
105665294c1fSJeff Layton retry:
1057243a5263SJeff Layton 	rcu_read_lock();
1058243a5263SJeff Layton 	nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
1059ce502f81SChuck Lever 			       nfsd_file_rhash_params);
1060ce502f81SChuck Lever 	if (nf)
1061ce502f81SChuck Lever 		nf = nfsd_file_get(nf);
1062243a5263SJeff Layton 	rcu_read_unlock();
106365294c1fSJeff Layton 	if (nf)
106465294c1fSJeff Layton 		goto wait_for_construction;
106565294c1fSJeff Layton 
1066243a5263SJeff Layton 	nf = nfsd_file_alloc(&key, may_flags);
1067243a5263SJeff Layton 	if (!nf) {
106854f7df70SChuck Lever 		status = nfserr_jukebox;
106954f7df70SChuck Lever 		goto out_status;
107065294c1fSJeff Layton 	}
107165294c1fSJeff Layton 
1072243a5263SJeff Layton 	ret = rhashtable_lookup_insert_key(&nfsd_file_rhash_tbl,
1073243a5263SJeff Layton 					   &key, &nf->nf_rhash,
1074ce502f81SChuck Lever 					   nfsd_file_rhash_params);
1075243a5263SJeff Layton 	if (likely(ret == 0))
107665294c1fSJeff Layton 		goto open_file;
1077243a5263SJeff Layton 
1078243a5263SJeff Layton 	nfsd_file_slab_free(&nf->nf_rcu);
1079bdd6b562SJeff Layton 	nf = NULL;
1080243a5263SJeff Layton 	if (ret == -EEXIST)
1081243a5263SJeff Layton 		goto retry;
1082243a5263SJeff Layton 	trace_nfsd_file_insert_err(rqstp, key.inode, may_flags, ret);
1083243a5263SJeff Layton 	status = nfserr_jukebox;
1084243a5263SJeff Layton 	goto out_status;
108565294c1fSJeff Layton 
108665294c1fSJeff Layton wait_for_construction:
108765294c1fSJeff Layton 	wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
108865294c1fSJeff Layton 
108965294c1fSJeff Layton 	/* Did construction of this file fail? */
109065294c1fSJeff Layton 	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
1091ce502f81SChuck Lever 		trace_nfsd_file_cons_err(rqstp, key.inode, may_flags, nf);
1092243a5263SJeff Layton 		if (!open_retry) {
109328c7d86bSTrond Myklebust 			status = nfserr_jukebox;
109428c7d86bSTrond Myklebust 			goto out;
109528c7d86bSTrond Myklebust 		}
1096243a5263SJeff Layton 		open_retry = false;
109765294c1fSJeff Layton 		nfsd_file_put_noref(nf);
109865294c1fSJeff Layton 		goto retry;
109965294c1fSJeff Layton 	}
110065294c1fSJeff Layton 
11014a0e73e6SChuck Lever 	nfsd_file_lru_remove(nf);
110265294c1fSJeff Layton 	this_cpu_inc(nfsd_file_cache_hits);
110365294c1fSJeff Layton 
110423ba98deSJeff Layton 	status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
110565294c1fSJeff Layton out:
110665294c1fSJeff Layton 	if (status == nfs_ok) {
110729d4bdbbSChuck Lever 		if (open)
110829d4bdbbSChuck Lever 			this_cpu_inc(nfsd_file_acquisitions);
110965294c1fSJeff Layton 		*pnf = nf;
111065294c1fSJeff Layton 	} else {
111165294c1fSJeff Layton 		nfsd_file_put(nf);
111265294c1fSJeff Layton 		nf = NULL;
111365294c1fSJeff Layton 	}
111465294c1fSJeff Layton 
111554f7df70SChuck Lever out_status:
1116ce502f81SChuck Lever 	put_cred(key.cred);
1117be023006SChuck Lever 	if (open)
1118ce502f81SChuck Lever 		trace_nfsd_file_acquire(rqstp, key.inode, may_flags, nf, status);
111965294c1fSJeff Layton 	return status;
112065294c1fSJeff Layton 
112165294c1fSJeff Layton open_file:
1122b40a2839SChuck Lever 	trace_nfsd_file_alloc(nf);
1123427f5f83SChuck Lever 	nf->nf_mark = nfsd_file_mark_find_or_create(nf, key.inode);
1124fb70bf12SChuck Lever 	if (nf->nf_mark) {
11250122e882SChuck Lever 		if (open) {
1126f4d84c52SChuck Lever 			status = nfsd_open_verified(rqstp, fhp, may_flags,
1127f4d84c52SChuck Lever 						    &nf->nf_file);
11280122e882SChuck Lever 			trace_nfsd_file_open(nf, status);
11290122e882SChuck Lever 		} else
1130fb70bf12SChuck Lever 			status = nfs_ok;
1131fb70bf12SChuck Lever 	} else
113265294c1fSJeff Layton 		status = nfserr_jukebox;
113365294c1fSJeff Layton 	/*
113465294c1fSJeff Layton 	 * If construction failed, or we raced with a call to unlink()
113565294c1fSJeff Layton 	 * then unhash.
113665294c1fSJeff Layton 	 */
1137ce502f81SChuck Lever 	if (status != nfs_ok || key.inode->i_nlink == 0)
1138*4d1ea845SChuck Lever 		nfsd_file_unhash_and_put(nf);
113965294c1fSJeff Layton 	clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
114065294c1fSJeff Layton 	smp_mb__after_atomic();
114165294c1fSJeff Layton 	wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
114265294c1fSJeff Layton 	goto out;
114365294c1fSJeff Layton }
114465294c1fSJeff Layton 
1145fb70bf12SChuck Lever /**
1146*4d1ea845SChuck Lever  * nfsd_file_acquire_gc - Get a struct nfsd_file with an open file
1147*4d1ea845SChuck Lever  * @rqstp: the RPC transaction being executed
1148*4d1ea845SChuck Lever  * @fhp: the NFS filehandle of the file to be opened
1149*4d1ea845SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
1150*4d1ea845SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1151*4d1ea845SChuck Lever  *
1152*4d1ea845SChuck Lever  * The nfsd_file object returned by this API is reference-counted
1153*4d1ea845SChuck Lever  * and garbage-collected. The object is retained for a few
1154*4d1ea845SChuck Lever  * seconds after the final nfsd_file_put() in case the caller
1155*4d1ea845SChuck Lever  * wants to re-use it.
1156*4d1ea845SChuck Lever  *
1157*4d1ea845SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1158*4d1ea845SChuck Lever  * network byte order is returned.
1159*4d1ea845SChuck Lever  */
1160*4d1ea845SChuck Lever __be32
1161*4d1ea845SChuck Lever nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp,
1162*4d1ea845SChuck Lever 		     unsigned int may_flags, struct nfsd_file **pnf)
1163*4d1ea845SChuck Lever {
1164*4d1ea845SChuck Lever 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, true, true);
1165*4d1ea845SChuck Lever }
1166*4d1ea845SChuck Lever 
1167*4d1ea845SChuck Lever /**
1168fb70bf12SChuck Lever  * nfsd_file_acquire - Get a struct nfsd_file with an open file
1169fb70bf12SChuck Lever  * @rqstp: the RPC transaction being executed
1170fb70bf12SChuck Lever  * @fhp: the NFS filehandle of the file to be opened
1171fb70bf12SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
1172fb70bf12SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1173fb70bf12SChuck Lever  *
1174*4d1ea845SChuck Lever  * The nfsd_file_object returned by this API is reference-counted
1175*4d1ea845SChuck Lever  * but not garbage-collected. The object is unhashed after the
1176*4d1ea845SChuck Lever  * final nfsd_file_put().
1177*4d1ea845SChuck Lever  *
1178fb70bf12SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1179fb70bf12SChuck Lever  * network byte order is returned.
1180fb70bf12SChuck Lever  */
1181fb70bf12SChuck Lever __be32
1182fb70bf12SChuck Lever nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1183fb70bf12SChuck Lever 		  unsigned int may_flags, struct nfsd_file **pnf)
1184fb70bf12SChuck Lever {
1185*4d1ea845SChuck Lever 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, true, false);
1186fb70bf12SChuck Lever }
1187fb70bf12SChuck Lever 
1188fb70bf12SChuck Lever /**
1189fb70bf12SChuck Lever  * nfsd_file_create - Get a struct nfsd_file, do not open
1190fb70bf12SChuck Lever  * @rqstp: the RPC transaction being executed
1191fb70bf12SChuck Lever  * @fhp: the NFS filehandle of the file just created
1192fb70bf12SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
1193fb70bf12SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1194fb70bf12SChuck Lever  *
1195*4d1ea845SChuck Lever  * The nfsd_file_object returned by this API is reference-counted
1196*4d1ea845SChuck Lever  * but not garbage-collected. The object is released immediately
1197*4d1ea845SChuck Lever  * one RCU grace period after the final nfsd_file_put().
1198*4d1ea845SChuck Lever  *
1199fb70bf12SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1200fb70bf12SChuck Lever  * network byte order is returned.
1201fb70bf12SChuck Lever  */
1202fb70bf12SChuck Lever __be32
1203fb70bf12SChuck Lever nfsd_file_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1204fb70bf12SChuck Lever 		 unsigned int may_flags, struct nfsd_file **pnf)
1205fb70bf12SChuck Lever {
1206*4d1ea845SChuck Lever 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, false, false);
1207fb70bf12SChuck Lever }
1208fb70bf12SChuck Lever 
120965294c1fSJeff Layton /*
121065294c1fSJeff Layton  * Note that fields may be added, removed or reordered in the future. Programs
121165294c1fSJeff Layton  * scraping this file for info should test the labels to ensure they're
121265294c1fSJeff Layton  * getting the correct field.
121365294c1fSJeff Layton  */
12141342f9ddSChenXiaoSong int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
121565294c1fSJeff Layton {
1216df2aff52SChuck Lever 	unsigned long releases = 0, pages_flushed = 0, evictions = 0;
1217df2aff52SChuck Lever 	unsigned long hits = 0, acquisitions = 0;
1218ce502f81SChuck Lever 	unsigned int i, count = 0, buckets = 0;
1219904940e9SChuck Lever 	unsigned long lru = 0, total_age = 0;
122065294c1fSJeff Layton 
1221ce502f81SChuck Lever 	/* Serialize with server shutdown */
122265294c1fSJeff Layton 	mutex_lock(&nfsd_mutex);
1223c7b824c3SChuck Lever 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
1224ce502f81SChuck Lever 		struct bucket_table *tbl;
1225ce502f81SChuck Lever 		struct rhashtable *ht;
1226ce502f81SChuck Lever 
12270fd244c1SChuck Lever 		lru = list_lru_count(&nfsd_file_lru);
1228ce502f81SChuck Lever 
1229ce502f81SChuck Lever 		rcu_read_lock();
1230ce502f81SChuck Lever 		ht = &nfsd_file_rhash_tbl;
1231ce502f81SChuck Lever 		count = atomic_read(&ht->nelems);
1232ce502f81SChuck Lever 		tbl = rht_dereference_rcu(ht->tbl, ht);
1233ce502f81SChuck Lever 		buckets = tbl->size;
1234ce502f81SChuck Lever 		rcu_read_unlock();
123565294c1fSJeff Layton 	}
123665294c1fSJeff Layton 	mutex_unlock(&nfsd_mutex);
123765294c1fSJeff Layton 
123829d4bdbbSChuck Lever 	for_each_possible_cpu(i) {
123965294c1fSJeff Layton 		hits += per_cpu(nfsd_file_cache_hits, i);
124029d4bdbbSChuck Lever 		acquisitions += per_cpu(nfsd_file_acquisitions, i);
1241d6329327SChuck Lever 		releases += per_cpu(nfsd_file_releases, i);
1242904940e9SChuck Lever 		total_age += per_cpu(nfsd_file_total_age, i);
124394660cc1SChuck Lever 		evictions += per_cpu(nfsd_file_evictions, i);
1244df2aff52SChuck Lever 		pages_flushed += per_cpu(nfsd_file_pages_flushed, i);
124529d4bdbbSChuck Lever 	}
124665294c1fSJeff Layton 
124765294c1fSJeff Layton 	seq_printf(m, "total entries: %u\n", count);
1248ce502f81SChuck Lever 	seq_printf(m, "hash buckets:  %u\n", buckets);
12490fd244c1SChuck Lever 	seq_printf(m, "lru entries:   %lu\n", lru);
125065294c1fSJeff Layton 	seq_printf(m, "cache hits:    %lu\n", hits);
125129d4bdbbSChuck Lever 	seq_printf(m, "acquisitions:  %lu\n", acquisitions);
1252d6329327SChuck Lever 	seq_printf(m, "releases:      %lu\n", releases);
125394660cc1SChuck Lever 	seq_printf(m, "evictions:     %lu\n", evictions);
1254904940e9SChuck Lever 	if (releases)
1255904940e9SChuck Lever 		seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1256904940e9SChuck Lever 	else
1257904940e9SChuck Lever 		seq_printf(m, "mean age (ms): -\n");
1258df2aff52SChuck Lever 	seq_printf(m, "pages flushed: %lu\n", pages_flushed);
125965294c1fSJeff Layton 	return 0;
126065294c1fSJeff Layton }
1261