xref: /openbmc/linux/fs/nfsd/filecache.c (revision d3aefd2b29ff5ffdeb5c06a7d3191a027a18cdb8)
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;
66fc22945eSChuck Lever 	enum nfsd_file_lookup_type	type;
67fc22945eSChuck Lever };
68fc22945eSChuck Lever 
69fc22945eSChuck Lever /*
70fc22945eSChuck Lever  * The returned hash value is based solely on the address of an in-code
71fc22945eSChuck Lever  * inode, a pointer to a slab-allocated object. The entropy in such a
72fc22945eSChuck Lever  * pointer is concentrated in its middle bits.
73fc22945eSChuck Lever  */
74fc22945eSChuck Lever static u32 nfsd_file_inode_hash(const struct inode *inode, u32 seed)
75fc22945eSChuck Lever {
76fc22945eSChuck Lever 	unsigned long ptr = (unsigned long)inode;
77fc22945eSChuck Lever 	u32 k;
78fc22945eSChuck Lever 
79fc22945eSChuck Lever 	k = ptr >> L1_CACHE_SHIFT;
80fc22945eSChuck Lever 	k &= 0x00ffffff;
81fc22945eSChuck Lever 	return jhash2(&k, 1, seed);
82fc22945eSChuck Lever }
83fc22945eSChuck Lever 
84fc22945eSChuck Lever /**
85fc22945eSChuck Lever  * nfsd_file_key_hashfn - Compute the hash value of a lookup key
86fc22945eSChuck Lever  * @data: key on which to compute the hash value
87fc22945eSChuck Lever  * @len: rhash table's key_len parameter (unused)
88fc22945eSChuck Lever  * @seed: rhash table's random seed of the day
89fc22945eSChuck Lever  *
90fc22945eSChuck Lever  * Return value:
91fc22945eSChuck Lever  *   Computed 32-bit hash value
92fc22945eSChuck Lever  */
93fc22945eSChuck Lever static u32 nfsd_file_key_hashfn(const void *data, u32 len, u32 seed)
94fc22945eSChuck Lever {
95fc22945eSChuck Lever 	const struct nfsd_file_lookup_key *key = data;
96fc22945eSChuck Lever 
97fc22945eSChuck Lever 	return nfsd_file_inode_hash(key->inode, seed);
98fc22945eSChuck Lever }
99fc22945eSChuck Lever 
100fc22945eSChuck Lever /**
101fc22945eSChuck Lever  * nfsd_file_obj_hashfn - Compute the hash value of an nfsd_file
102fc22945eSChuck Lever  * @data: object on which to compute the hash value
103fc22945eSChuck Lever  * @len: rhash table's key_len parameter (unused)
104fc22945eSChuck Lever  * @seed: rhash table's random seed of the day
105fc22945eSChuck Lever  *
106fc22945eSChuck Lever  * Return value:
107fc22945eSChuck Lever  *   Computed 32-bit hash value
108fc22945eSChuck Lever  */
109fc22945eSChuck Lever static u32 nfsd_file_obj_hashfn(const void *data, u32 len, u32 seed)
110fc22945eSChuck Lever {
111fc22945eSChuck Lever 	const struct nfsd_file *nf = data;
112fc22945eSChuck Lever 
113fc22945eSChuck Lever 	return nfsd_file_inode_hash(nf->nf_inode, seed);
114fc22945eSChuck Lever }
115fc22945eSChuck Lever 
116fc22945eSChuck Lever static bool
117fc22945eSChuck Lever nfsd_match_cred(const struct cred *c1, const struct cred *c2)
118fc22945eSChuck Lever {
119fc22945eSChuck Lever 	int i;
120fc22945eSChuck Lever 
121fc22945eSChuck Lever 	if (!uid_eq(c1->fsuid, c2->fsuid))
122fc22945eSChuck Lever 		return false;
123fc22945eSChuck Lever 	if (!gid_eq(c1->fsgid, c2->fsgid))
124fc22945eSChuck Lever 		return false;
125fc22945eSChuck Lever 	if (c1->group_info == NULL || c2->group_info == NULL)
126fc22945eSChuck Lever 		return c1->group_info == c2->group_info;
127fc22945eSChuck Lever 	if (c1->group_info->ngroups != c2->group_info->ngroups)
128fc22945eSChuck Lever 		return false;
129fc22945eSChuck Lever 	for (i = 0; i < c1->group_info->ngroups; i++) {
130fc22945eSChuck Lever 		if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
131fc22945eSChuck Lever 			return false;
132fc22945eSChuck Lever 	}
133fc22945eSChuck Lever 	return true;
134fc22945eSChuck Lever }
135fc22945eSChuck Lever 
136fc22945eSChuck Lever /**
137fc22945eSChuck Lever  * nfsd_file_obj_cmpfn - Match a cache item against search criteria
138fc22945eSChuck Lever  * @arg: search criteria
139fc22945eSChuck Lever  * @ptr: cache item to check
140fc22945eSChuck Lever  *
141fc22945eSChuck Lever  * Return values:
142fc22945eSChuck Lever  *   %0 - Item matches search criteria
143fc22945eSChuck Lever  *   %1 - Item does not match search criteria
144fc22945eSChuck Lever  */
145fc22945eSChuck Lever static int nfsd_file_obj_cmpfn(struct rhashtable_compare_arg *arg,
146fc22945eSChuck Lever 			       const void *ptr)
147fc22945eSChuck Lever {
148fc22945eSChuck Lever 	const struct nfsd_file_lookup_key *key = arg->key;
149fc22945eSChuck Lever 	const struct nfsd_file *nf = ptr;
150fc22945eSChuck Lever 
151fc22945eSChuck Lever 	switch (key->type) {
152fc22945eSChuck Lever 	case NFSD_FILE_KEY_INODE:
153fc22945eSChuck Lever 		if (nf->nf_inode != key->inode)
154fc22945eSChuck Lever 			return 1;
155fc22945eSChuck Lever 		break;
156fc22945eSChuck Lever 	case NFSD_FILE_KEY_FULL:
157fc22945eSChuck Lever 		if (nf->nf_inode != key->inode)
158fc22945eSChuck Lever 			return 1;
159fc22945eSChuck Lever 		if (nf->nf_may != key->need)
160fc22945eSChuck Lever 			return 1;
161fc22945eSChuck Lever 		if (nf->nf_net != key->net)
162fc22945eSChuck Lever 			return 1;
163fc22945eSChuck Lever 		if (!nfsd_match_cred(nf->nf_cred, key->cred))
164fc22945eSChuck Lever 			return 1;
165fc22945eSChuck Lever 		if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0)
166fc22945eSChuck Lever 			return 1;
167fc22945eSChuck Lever 		break;
168fc22945eSChuck Lever 	}
169fc22945eSChuck Lever 	return 0;
170fc22945eSChuck Lever }
171fc22945eSChuck Lever 
172fc22945eSChuck Lever static const struct rhashtable_params nfsd_file_rhash_params = {
173fc22945eSChuck Lever 	.key_len		= sizeof_field(struct nfsd_file, nf_inode),
174fc22945eSChuck Lever 	.key_offset		= offsetof(struct nfsd_file, nf_inode),
175fc22945eSChuck Lever 	.head_offset		= offsetof(struct nfsd_file, nf_rhash),
176fc22945eSChuck Lever 	.hashfn			= nfsd_file_key_hashfn,
177fc22945eSChuck Lever 	.obj_hashfn		= nfsd_file_obj_hashfn,
178fc22945eSChuck Lever 	.obj_cmpfn		= nfsd_file_obj_cmpfn,
179fc22945eSChuck Lever 	/* Reduce resizing churn on light workloads */
180fc22945eSChuck Lever 	.min_size		= 512,		/* buckets */
181fc22945eSChuck Lever 	.automatic_shrinking	= true,
182fc22945eSChuck Lever };
18365294c1fSJeff Layton 
18465294c1fSJeff Layton static void
1859542e6a6STrond Myklebust nfsd_file_schedule_laundrette(void)
18665294c1fSJeff Layton {
187ce502f81SChuck Lever 	if ((atomic_read(&nfsd_file_rhash_tbl.nelems) == 0) ||
188c7b824c3SChuck Lever 	    test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
18965294c1fSJeff Layton 		return;
19065294c1fSJeff Layton 
1919542e6a6STrond Myklebust 	queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
1929542e6a6STrond Myklebust 			NFSD_LAUNDRETTE_DELAY);
19365294c1fSJeff Layton }
19465294c1fSJeff Layton 
19565294c1fSJeff Layton static void
19665294c1fSJeff Layton nfsd_file_slab_free(struct rcu_head *rcu)
19765294c1fSJeff Layton {
19865294c1fSJeff Layton 	struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
19965294c1fSJeff Layton 
20065294c1fSJeff Layton 	put_cred(nf->nf_cred);
20165294c1fSJeff Layton 	kmem_cache_free(nfsd_file_slab, nf);
20265294c1fSJeff Layton }
20365294c1fSJeff Layton 
20465294c1fSJeff Layton static void
20565294c1fSJeff Layton nfsd_file_mark_free(struct fsnotify_mark *mark)
20665294c1fSJeff Layton {
20765294c1fSJeff Layton 	struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
20865294c1fSJeff Layton 						  nfm_mark);
20965294c1fSJeff Layton 
21065294c1fSJeff Layton 	kmem_cache_free(nfsd_file_mark_slab, nfm);
21165294c1fSJeff Layton }
21265294c1fSJeff Layton 
21365294c1fSJeff Layton static struct nfsd_file_mark *
21465294c1fSJeff Layton nfsd_file_mark_get(struct nfsd_file_mark *nfm)
21565294c1fSJeff Layton {
216689827cdSTrond Myklebust 	if (!refcount_inc_not_zero(&nfm->nfm_ref))
21765294c1fSJeff Layton 		return NULL;
21865294c1fSJeff Layton 	return nfm;
21965294c1fSJeff Layton }
22065294c1fSJeff Layton 
22165294c1fSJeff Layton static void
22265294c1fSJeff Layton nfsd_file_mark_put(struct nfsd_file_mark *nfm)
22365294c1fSJeff Layton {
224689827cdSTrond Myklebust 	if (refcount_dec_and_test(&nfm->nfm_ref)) {
22565294c1fSJeff Layton 		fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
22665294c1fSJeff Layton 		fsnotify_put_mark(&nfm->nfm_mark);
22765294c1fSJeff Layton 	}
22865294c1fSJeff Layton }
22965294c1fSJeff Layton 
23065294c1fSJeff Layton static struct nfsd_file_mark *
231427f5f83SChuck Lever nfsd_file_mark_find_or_create(struct nfsd_file *nf, struct inode *inode)
23265294c1fSJeff Layton {
23365294c1fSJeff Layton 	int			err;
23465294c1fSJeff Layton 	struct fsnotify_mark	*mark;
23565294c1fSJeff Layton 	struct nfsd_file_mark	*nfm = NULL, *new;
23665294c1fSJeff Layton 
23765294c1fSJeff Layton 	do {
238b8962a9dSAmir Goldstein 		fsnotify_group_lock(nfsd_file_fsnotify_group);
23965294c1fSJeff Layton 		mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
24065294c1fSJeff Layton 					  nfsd_file_fsnotify_group);
24165294c1fSJeff Layton 		if (mark) {
24265294c1fSJeff Layton 			nfm = nfsd_file_mark_get(container_of(mark,
24365294c1fSJeff Layton 						 struct nfsd_file_mark,
24465294c1fSJeff Layton 						 nfm_mark));
245b8962a9dSAmir Goldstein 			fsnotify_group_unlock(nfsd_file_fsnotify_group);
24690d2f1daSTrond Myklebust 			if (nfm) {
24765294c1fSJeff Layton 				fsnotify_put_mark(mark);
24865294c1fSJeff Layton 				break;
24990d2f1daSTrond Myklebust 			}
25090d2f1daSTrond Myklebust 			/* Avoid soft lockup race with nfsd_file_mark_put() */
25190d2f1daSTrond Myklebust 			fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
25290d2f1daSTrond Myklebust 			fsnotify_put_mark(mark);
253b8962a9dSAmir Goldstein 		} else {
254b8962a9dSAmir Goldstein 			fsnotify_group_unlock(nfsd_file_fsnotify_group);
255b8962a9dSAmir Goldstein 		}
25665294c1fSJeff Layton 
25765294c1fSJeff Layton 		/* allocate a new nfm */
25865294c1fSJeff Layton 		new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
25965294c1fSJeff Layton 		if (!new)
26065294c1fSJeff Layton 			return NULL;
26165294c1fSJeff Layton 		fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
26265294c1fSJeff Layton 		new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
263689827cdSTrond Myklebust 		refcount_set(&new->nfm_ref, 1);
26465294c1fSJeff Layton 
26565294c1fSJeff Layton 		err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
26665294c1fSJeff Layton 
26765294c1fSJeff Layton 		/*
26865294c1fSJeff Layton 		 * If the add was successful, then return the object.
26965294c1fSJeff Layton 		 * Otherwise, we need to put the reference we hold on the
27065294c1fSJeff Layton 		 * nfm_mark. The fsnotify code will take a reference and put
27165294c1fSJeff Layton 		 * it on failure, so we can't just free it directly. It's also
27265294c1fSJeff Layton 		 * not safe to call fsnotify_destroy_mark on it as the
27365294c1fSJeff Layton 		 * mark->group will be NULL. Thus, we can't let the nfm_ref
27465294c1fSJeff Layton 		 * counter drive the destruction at this point.
27565294c1fSJeff Layton 		 */
27665294c1fSJeff Layton 		if (likely(!err))
27765294c1fSJeff Layton 			nfm = new;
27865294c1fSJeff Layton 		else
27965294c1fSJeff Layton 			fsnotify_put_mark(&new->nfm_mark);
28065294c1fSJeff Layton 	} while (unlikely(err == -EEXIST));
28165294c1fSJeff Layton 
28265294c1fSJeff Layton 	return nfm;
28365294c1fSJeff Layton }
28465294c1fSJeff Layton 
28565294c1fSJeff Layton static struct nfsd_file *
286ce502f81SChuck Lever nfsd_file_alloc(struct nfsd_file_lookup_key *key, unsigned int may)
28765294c1fSJeff Layton {
28865294c1fSJeff Layton 	struct nfsd_file *nf;
28965294c1fSJeff Layton 
29065294c1fSJeff Layton 	nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
29165294c1fSJeff Layton 	if (nf) {
29265294c1fSJeff Layton 		INIT_LIST_HEAD(&nf->nf_lru);
293904940e9SChuck Lever 		nf->nf_birthtime = ktime_get();
29465294c1fSJeff Layton 		nf->nf_file = NULL;
29565294c1fSJeff Layton 		nf->nf_cred = get_current_cred();
296ce502f81SChuck Lever 		nf->nf_net = key->net;
29765294c1fSJeff Layton 		nf->nf_flags = 0;
298ce502f81SChuck Lever 		__set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
299ce502f81SChuck Lever 		__set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
300ce502f81SChuck Lever 		nf->nf_inode = key->inode;
301ce502f81SChuck Lever 		/* nf_ref is pre-incremented for hash table */
302ce502f81SChuck Lever 		refcount_set(&nf->nf_ref, 2);
303ce502f81SChuck Lever 		nf->nf_may = key->need;
30465294c1fSJeff Layton 		nf->nf_mark = NULL;
30565294c1fSJeff Layton 	}
30665294c1fSJeff Layton 	return nf;
30765294c1fSJeff Layton }
30865294c1fSJeff Layton 
30965294c1fSJeff Layton static bool
31065294c1fSJeff Layton nfsd_file_free(struct nfsd_file *nf)
31165294c1fSJeff Layton {
312904940e9SChuck Lever 	s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime));
31365294c1fSJeff Layton 	bool flush = false;
31465294c1fSJeff Layton 
315d6329327SChuck Lever 	this_cpu_inc(nfsd_file_releases);
316904940e9SChuck Lever 	this_cpu_add(nfsd_file_total_age, age);
317d6329327SChuck Lever 
31865294c1fSJeff Layton 	trace_nfsd_file_put_final(nf);
31965294c1fSJeff Layton 	if (nf->nf_mark)
32065294c1fSJeff Layton 		nfsd_file_mark_put(nf->nf_mark);
32165294c1fSJeff Layton 	if (nf->nf_file) {
32265294c1fSJeff Layton 		get_file(nf->nf_file);
32365294c1fSJeff Layton 		filp_close(nf->nf_file, NULL);
32465294c1fSJeff Layton 		fput(nf->nf_file);
32565294c1fSJeff Layton 		flush = true;
32665294c1fSJeff Layton 	}
327668ed92eSChuck Lever 
328668ed92eSChuck Lever 	/*
329668ed92eSChuck Lever 	 * If this item is still linked via nf_lru, that's a bug.
330668ed92eSChuck Lever 	 * WARN and leak it to preserve system stability.
331668ed92eSChuck Lever 	 */
332668ed92eSChuck Lever 	if (WARN_ON_ONCE(!list_empty(&nf->nf_lru)))
333668ed92eSChuck Lever 		return flush;
334668ed92eSChuck Lever 
33565294c1fSJeff Layton 	call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
33665294c1fSJeff Layton 	return flush;
33765294c1fSJeff Layton }
33865294c1fSJeff Layton 
339055b24a8STrond Myklebust static bool
340055b24a8STrond Myklebust nfsd_file_check_writeback(struct nfsd_file *nf)
341055b24a8STrond Myklebust {
342055b24a8STrond Myklebust 	struct file *file = nf->nf_file;
343055b24a8STrond Myklebust 	struct address_space *mapping;
344055b24a8STrond Myklebust 
345055b24a8STrond Myklebust 	if (!file || !(file->f_mode & FMODE_WRITE))
346055b24a8STrond Myklebust 		return false;
347055b24a8STrond Myklebust 	mapping = file->f_mapping;
348055b24a8STrond Myklebust 	return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
349055b24a8STrond Myklebust 		mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
350055b24a8STrond Myklebust }
351055b24a8STrond Myklebust 
352055b24a8STrond Myklebust static int
353055b24a8STrond Myklebust nfsd_file_check_write_error(struct nfsd_file *nf)
354055b24a8STrond Myklebust {
355055b24a8STrond Myklebust 	struct file *file = nf->nf_file;
356055b24a8STrond Myklebust 
357055b24a8STrond Myklebust 	if (!file || !(file->f_mode & FMODE_WRITE))
358055b24a8STrond Myklebust 		return 0;
359055b24a8STrond Myklebust 	return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
360055b24a8STrond Myklebust }
361055b24a8STrond Myklebust 
36265294c1fSJeff Layton static void
3636b8a9433STrond Myklebust nfsd_file_flush(struct nfsd_file *nf)
3646b8a9433STrond Myklebust {
365df2aff52SChuck Lever 	struct file *file = nf->nf_file;
366df2aff52SChuck Lever 
367df2aff52SChuck Lever 	if (!file || !(file->f_mode & FMODE_WRITE))
368df2aff52SChuck Lever 		return;
369df2aff52SChuck Lever 	this_cpu_add(nfsd_file_pages_flushed, file->f_mapping->nrpages);
370df2aff52SChuck Lever 	if (vfs_fsync(file, 1) != 0)
3716b8a9433STrond Myklebust 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
3726b8a9433STrond Myklebust }
3736b8a9433STrond Myklebust 
374c46203acSChuck Lever static void nfsd_file_lru_add(struct nfsd_file *nf)
37565294c1fSJeff Layton {
3764a0e73e6SChuck Lever 	set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
377c46203acSChuck Lever 	if (list_lru_add(&nfsd_file_lru, &nf->nf_lru))
378c46203acSChuck Lever 		trace_nfsd_file_lru_add(nf);
379c46203acSChuck Lever }
38065294c1fSJeff Layton 
381c46203acSChuck Lever static void nfsd_file_lru_remove(struct nfsd_file *nf)
382c46203acSChuck Lever {
383c46203acSChuck Lever 	if (list_lru_del(&nfsd_file_lru, &nf->nf_lru))
384c46203acSChuck Lever 		trace_nfsd_file_lru_del(nf);
385c46203acSChuck Lever }
386c46203acSChuck Lever 
38765294c1fSJeff Layton static void
388ce502f81SChuck Lever nfsd_file_hash_remove(struct nfsd_file *nf)
38965294c1fSJeff Layton {
39065294c1fSJeff Layton 	trace_nfsd_file_unhash(nf);
39165294c1fSJeff Layton 
392055b24a8STrond Myklebust 	if (nfsd_file_check_write_error(nf))
3933988a578SChuck Lever 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
394ce502f81SChuck Lever 	rhashtable_remove_fast(&nfsd_file_rhash_tbl, &nf->nf_rhash,
395ce502f81SChuck Lever 			       nfsd_file_rhash_params);
39665294c1fSJeff Layton }
39765294c1fSJeff Layton 
39865294c1fSJeff Layton static bool
39965294c1fSJeff Layton nfsd_file_unhash(struct nfsd_file *nf)
40065294c1fSJeff Layton {
40165294c1fSJeff Layton 	if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
402ce502f81SChuck Lever 		nfsd_file_hash_remove(nf);
40365294c1fSJeff Layton 		return true;
40465294c1fSJeff Layton 	}
40565294c1fSJeff Layton 	return false;
40665294c1fSJeff Layton }
40765294c1fSJeff Layton 
4088d0d254bSJeff Layton static void
409ce502f81SChuck Lever nfsd_file_unhash_and_dispose(struct nfsd_file *nf, struct list_head *dispose)
41065294c1fSJeff Layton {
411ce502f81SChuck Lever 	trace_nfsd_file_unhash_and_dispose(nf);
4128d0d254bSJeff Layton 	if (nfsd_file_unhash(nf)) {
4138d0d254bSJeff Layton 		/* caller must call nfsd_file_dispose_list() later */
4144a0e73e6SChuck Lever 		nfsd_file_lru_remove(nf);
41565294c1fSJeff Layton 		list_add(&nf->nf_lru, dispose);
4168d0d254bSJeff Layton 	}
41765294c1fSJeff Layton }
41865294c1fSJeff Layton 
419b6669305STrond Myklebust static void
42065294c1fSJeff Layton nfsd_file_put_noref(struct nfsd_file *nf)
42165294c1fSJeff Layton {
42265294c1fSJeff Layton 	trace_nfsd_file_put(nf);
42365294c1fSJeff Layton 
424689827cdSTrond Myklebust 	if (refcount_dec_and_test(&nf->nf_ref)) {
42565294c1fSJeff Layton 		WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
4264a0e73e6SChuck Lever 		nfsd_file_lru_remove(nf);
42765294c1fSJeff Layton 		nfsd_file_free(nf);
42865294c1fSJeff Layton 	}
42965294c1fSJeff Layton }
43065294c1fSJeff Layton 
43165294c1fSJeff Layton void
43265294c1fSJeff Layton nfsd_file_put(struct nfsd_file *nf)
43365294c1fSJeff Layton {
43408af54b3SChuck Lever 	might_sleep();
43508af54b3SChuck Lever 
4364a0e73e6SChuck Lever 	nfsd_file_lru_add(nf);
43799939792STrond Myklebust 	if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) {
4386b8a9433STrond Myklebust 		nfsd_file_flush(nf);
439b6669305STrond Myklebust 		nfsd_file_put_noref(nf);
440b6c71c66SChuck Lever 	} else if (nf->nf_file) {
441b6669305STrond Myklebust 		nfsd_file_put_noref(nf);
4429542e6a6STrond Myklebust 		nfsd_file_schedule_laundrette();
443b6c71c66SChuck Lever 	} else
444b6c71c66SChuck Lever 		nfsd_file_put_noref(nf);
44565294c1fSJeff Layton }
44665294c1fSJeff Layton 
4475e138c4aSChuck Lever /**
4485e138c4aSChuck Lever  * nfsd_file_close - Close an nfsd_file
4495e138c4aSChuck Lever  * @nf: nfsd_file to close
4505e138c4aSChuck Lever  *
4515e138c4aSChuck Lever  * If this is the final reference for @nf, free it immediately.
4525e138c4aSChuck Lever  * This reflects an on-the-wire CLOSE or DELEGRETURN into the
4535e138c4aSChuck Lever  * VFS and exported filesystem.
4545e138c4aSChuck Lever  */
4555e138c4aSChuck Lever void nfsd_file_close(struct nfsd_file *nf)
4565e138c4aSChuck Lever {
4575e138c4aSChuck Lever 	nfsd_file_put(nf);
4585e138c4aSChuck Lever 	if (refcount_dec_if_one(&nf->nf_ref)) {
4595e138c4aSChuck Lever 		nfsd_file_unhash(nf);
4605e138c4aSChuck Lever 		nfsd_file_lru_remove(nf);
4615e138c4aSChuck Lever 		nfsd_file_free(nf);
4625e138c4aSChuck Lever 	}
46365294c1fSJeff Layton }
46465294c1fSJeff Layton 
46565294c1fSJeff Layton struct nfsd_file *
46665294c1fSJeff Layton nfsd_file_get(struct nfsd_file *nf)
46765294c1fSJeff Layton {
468689827cdSTrond Myklebust 	if (likely(refcount_inc_not_zero(&nf->nf_ref)))
46965294c1fSJeff Layton 		return nf;
47065294c1fSJeff Layton 	return NULL;
47165294c1fSJeff Layton }
47265294c1fSJeff Layton 
47365294c1fSJeff Layton static void
47465294c1fSJeff Layton nfsd_file_dispose_list(struct list_head *dispose)
47565294c1fSJeff Layton {
47665294c1fSJeff Layton 	struct nfsd_file *nf;
47765294c1fSJeff Layton 
47865294c1fSJeff Layton 	while(!list_empty(dispose)) {
47965294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
480668ed92eSChuck Lever 		list_del_init(&nf->nf_lru);
4816b8a9433STrond Myklebust 		nfsd_file_flush(nf);
48265294c1fSJeff Layton 		nfsd_file_put_noref(nf);
48365294c1fSJeff Layton 	}
48465294c1fSJeff Layton }
48565294c1fSJeff Layton 
48665294c1fSJeff Layton static void
48765294c1fSJeff Layton nfsd_file_dispose_list_sync(struct list_head *dispose)
48865294c1fSJeff Layton {
48965294c1fSJeff Layton 	bool flush = false;
49065294c1fSJeff Layton 	struct nfsd_file *nf;
49165294c1fSJeff Layton 
49265294c1fSJeff Layton 	while(!list_empty(dispose)) {
49365294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
494668ed92eSChuck Lever 		list_del_init(&nf->nf_lru);
4956b8a9433STrond Myklebust 		nfsd_file_flush(nf);
496689827cdSTrond Myklebust 		if (!refcount_dec_and_test(&nf->nf_ref))
49765294c1fSJeff Layton 			continue;
49865294c1fSJeff Layton 		if (nfsd_file_free(nf))
49965294c1fSJeff Layton 			flush = true;
50065294c1fSJeff Layton 	}
50165294c1fSJeff Layton 	if (flush)
50265294c1fSJeff Layton 		flush_delayed_fput();
50365294c1fSJeff Layton }
50465294c1fSJeff Layton 
5059542e6a6STrond Myklebust static void
5069542e6a6STrond Myklebust nfsd_file_list_remove_disposal(struct list_head *dst,
5079542e6a6STrond Myklebust 		struct nfsd_fcache_disposal *l)
5089542e6a6STrond Myklebust {
5099542e6a6STrond Myklebust 	spin_lock(&l->lock);
5109542e6a6STrond Myklebust 	list_splice_init(&l->freeme, dst);
5119542e6a6STrond Myklebust 	spin_unlock(&l->lock);
5129542e6a6STrond Myklebust }
5139542e6a6STrond Myklebust 
5149542e6a6STrond Myklebust static void
5159542e6a6STrond Myklebust nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
5169542e6a6STrond Myklebust {
5171463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5181463b38eSNeilBrown 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
5199542e6a6STrond Myklebust 
5209542e6a6STrond Myklebust 	spin_lock(&l->lock);
5219542e6a6STrond Myklebust 	list_splice_tail_init(files, &l->freeme);
5229542e6a6STrond Myklebust 	spin_unlock(&l->lock);
5239542e6a6STrond Myklebust 	queue_work(nfsd_filecache_wq, &l->work);
5249542e6a6STrond Myklebust }
5259542e6a6STrond Myklebust 
5269542e6a6STrond Myklebust static void
5279542e6a6STrond Myklebust nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
5289542e6a6STrond Myklebust 		struct net *net)
5299542e6a6STrond Myklebust {
5309542e6a6STrond Myklebust 	struct nfsd_file *nf, *tmp;
5319542e6a6STrond Myklebust 
5329542e6a6STrond Myklebust 	list_for_each_entry_safe(nf, tmp, src, nf_lru) {
5339542e6a6STrond Myklebust 		if (nf->nf_net == net)
5349542e6a6STrond Myklebust 			list_move_tail(&nf->nf_lru, dst);
5359542e6a6STrond Myklebust 	}
5369542e6a6STrond Myklebust }
5379542e6a6STrond Myklebust 
5389542e6a6STrond Myklebust static void
5399542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(struct list_head *dispose)
5409542e6a6STrond Myklebust {
5419542e6a6STrond Myklebust 	LIST_HEAD(list);
5429542e6a6STrond Myklebust 	struct nfsd_file *nf;
5439542e6a6STrond Myklebust 
5449542e6a6STrond Myklebust 	while(!list_empty(dispose)) {
5459542e6a6STrond Myklebust 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
5469542e6a6STrond Myklebust 		nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
5479542e6a6STrond Myklebust 		nfsd_file_list_add_disposal(&list, nf->nf_net);
5489542e6a6STrond Myklebust 	}
5499542e6a6STrond Myklebust }
5509542e6a6STrond Myklebust 
5514a0e73e6SChuck Lever /**
5524a0e73e6SChuck Lever  * nfsd_file_lru_cb - Examine an entry on the LRU list
5534a0e73e6SChuck Lever  * @item: LRU entry to examine
5544a0e73e6SChuck Lever  * @lru: controlling LRU
5554a0e73e6SChuck Lever  * @lock: LRU list lock (unused)
5564a0e73e6SChuck Lever  * @arg: dispose list
5574a0e73e6SChuck Lever  *
5584a0e73e6SChuck Lever  * Return values:
5594a0e73e6SChuck Lever  *   %LRU_REMOVED: @item was removed from the LRU
560edead3a5SChuck Lever  *   %LRU_ROTATE: @item is to be moved to the LRU tail
5614a0e73e6SChuck Lever  *   %LRU_SKIP: @item cannot be evicted
56265294c1fSJeff Layton  */
56365294c1fSJeff Layton static enum lru_status
56465294c1fSJeff Layton nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
56565294c1fSJeff Layton 		 spinlock_t *lock, void *arg)
56665294c1fSJeff Layton 	__releases(lock)
56765294c1fSJeff Layton 	__acquires(lock)
56865294c1fSJeff Layton {
56965294c1fSJeff Layton 	struct list_head *head = arg;
57065294c1fSJeff Layton 	struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
57165294c1fSJeff Layton 
57265294c1fSJeff Layton 	/*
57365294c1fSJeff Layton 	 * Do a lockless refcount check. The hashtable holds one reference, so
57465294c1fSJeff Layton 	 * we look to see if anything else has a reference, or if any have
57565294c1fSJeff Layton 	 * been put since the shrinker last ran. Those don't get unhashed and
57665294c1fSJeff Layton 	 * released.
57765294c1fSJeff Layton 	 *
57865294c1fSJeff Layton 	 * Note that in the put path, we set the flag and then decrement the
57965294c1fSJeff Layton 	 * counter. Here we check the counter and then test and clear the flag.
58065294c1fSJeff Layton 	 * That order is deliberate to ensure that we can do this locklessly.
58165294c1fSJeff Layton 	 */
582c46203acSChuck Lever 	if (refcount_read(&nf->nf_ref) > 1) {
5834a0e73e6SChuck Lever 		list_lru_isolate(lru, &nf->nf_lru);
584c46203acSChuck Lever 		trace_nfsd_file_gc_in_use(nf);
5854a0e73e6SChuck Lever 		return LRU_REMOVED;
586c46203acSChuck Lever 	}
587055b24a8STrond Myklebust 
588055b24a8STrond Myklebust 	/*
589055b24a8STrond Myklebust 	 * Don't throw out files that are still undergoing I/O or
590055b24a8STrond Myklebust 	 * that have uncleared errors pending.
591055b24a8STrond Myklebust 	 */
592c46203acSChuck Lever 	if (nfsd_file_check_writeback(nf)) {
593c46203acSChuck Lever 		trace_nfsd_file_gc_writeback(nf);
59465294c1fSJeff Layton 		return LRU_SKIP;
59565294c1fSJeff Layton 	}
59665294c1fSJeff Layton 
597c46203acSChuck Lever 	if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
598c46203acSChuck Lever 		trace_nfsd_file_gc_referenced(nf);
599edead3a5SChuck Lever 		return LRU_ROTATE;
60065294c1fSJeff Layton 	}
60165294c1fSJeff Layton 
602c46203acSChuck Lever 	if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
603c46203acSChuck Lever 		trace_nfsd_file_gc_hashed(nf);
604c46203acSChuck Lever 		return LRU_SKIP;
605c46203acSChuck Lever 	}
60665294c1fSJeff Layton 
60765294c1fSJeff Layton 	list_lru_isolate_move(lru, &nf->nf_lru, head);
60894660cc1SChuck Lever 	this_cpu_inc(nfsd_file_evictions);
609c46203acSChuck Lever 	trace_nfsd_file_gc_disposed(nf);
61065294c1fSJeff Layton 	return LRU_REMOVED;
61165294c1fSJeff Layton }
61265294c1fSJeff Layton 
6130bac5a26SChuck Lever /*
6140bac5a26SChuck Lever  * Unhash items on @dispose immediately, then queue them on the
6150bac5a26SChuck Lever  * disposal workqueue to finish releasing them in the background.
6160bac5a26SChuck Lever  *
6170bac5a26SChuck Lever  * cel: Note that between the time list_lru_shrink_walk runs and
6180bac5a26SChuck Lever  * now, these items are in the hash table but marked unhashed.
6190bac5a26SChuck Lever  * Why release these outside of lru_cb ? There's no lock ordering
6200bac5a26SChuck Lever  * problem since lru_cb currently takes no lock.
6210bac5a26SChuck Lever  */
6220bac5a26SChuck Lever static void nfsd_file_gc_dispose_list(struct list_head *dispose)
6230bac5a26SChuck Lever {
6240bac5a26SChuck Lever 	struct nfsd_file *nf;
6250bac5a26SChuck Lever 
626cb7ec76eSChuck Lever 	list_for_each_entry(nf, dispose, nf_lru)
627cb7ec76eSChuck Lever 		nfsd_file_hash_remove(nf);
6280bac5a26SChuck Lever 	nfsd_file_dispose_list_delayed(dispose);
6299542e6a6STrond Myklebust }
6309542e6a6STrond Myklebust 
6319542e6a6STrond Myklebust static void
6329542e6a6STrond Myklebust nfsd_file_gc(void)
6339542e6a6STrond Myklebust {
6343bc6d347SChuck Lever 	LIST_HEAD(dispose);
63594660cc1SChuck Lever 	unsigned long ret;
6363bc6d347SChuck Lever 
63794660cc1SChuck Lever 	ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
638edead3a5SChuck Lever 			    &dispose, list_lru_count(&nfsd_file_lru));
63994660cc1SChuck Lever 	trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
6403bc6d347SChuck Lever 	nfsd_file_gc_dispose_list(&dispose);
6419542e6a6STrond Myklebust }
6429542e6a6STrond Myklebust 
6439542e6a6STrond Myklebust static void
6449542e6a6STrond Myklebust nfsd_file_gc_worker(struct work_struct *work)
6459542e6a6STrond Myklebust {
6469542e6a6STrond Myklebust 	nfsd_file_gc();
6479542e6a6STrond Myklebust 	nfsd_file_schedule_laundrette();
64865294c1fSJeff Layton }
64965294c1fSJeff Layton 
65065294c1fSJeff Layton static unsigned long
65165294c1fSJeff Layton nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
65265294c1fSJeff Layton {
65365294c1fSJeff Layton 	return list_lru_count(&nfsd_file_lru);
65465294c1fSJeff Layton }
65565294c1fSJeff Layton 
65665294c1fSJeff Layton static unsigned long
65765294c1fSJeff Layton nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
65865294c1fSJeff Layton {
65939f1d1ffSChuck Lever 	LIST_HEAD(dispose);
66039f1d1ffSChuck Lever 	unsigned long ret;
66139f1d1ffSChuck Lever 
66239f1d1ffSChuck Lever 	ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
66339f1d1ffSChuck Lever 				   nfsd_file_lru_cb, &dispose);
66494660cc1SChuck Lever 	trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
66539f1d1ffSChuck Lever 	nfsd_file_gc_dispose_list(&dispose);
66639f1d1ffSChuck Lever 	return ret;
66765294c1fSJeff Layton }
66865294c1fSJeff Layton 
66965294c1fSJeff Layton static struct shrinker	nfsd_file_shrinker = {
67065294c1fSJeff Layton 	.scan_objects = nfsd_file_lru_scan,
67165294c1fSJeff Layton 	.count_objects = nfsd_file_lru_count,
67265294c1fSJeff Layton 	.seeks = 1,
67365294c1fSJeff Layton };
67465294c1fSJeff Layton 
675a8455110SChuck Lever /*
676a8455110SChuck Lever  * Find all cache items across all net namespaces that match @inode and
677a8455110SChuck Lever  * move them to @dispose. The lookup is atomic wrt nfsd_file_acquire().
678a8455110SChuck Lever  */
679a8455110SChuck Lever static unsigned int
680a8455110SChuck Lever __nfsd_file_close_inode(struct inode *inode, struct list_head *dispose)
68165294c1fSJeff Layton {
682ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
683ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_INODE,
684ce502f81SChuck Lever 		.inode	= inode,
685ce502f81SChuck Lever 	};
686a8455110SChuck Lever 	unsigned int count = 0;
68765294c1fSJeff Layton 	struct nfsd_file *nf;
68865294c1fSJeff Layton 
689ce502f81SChuck Lever 	rcu_read_lock();
690ce502f81SChuck Lever 	do {
691ce502f81SChuck Lever 		nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
692ce502f81SChuck Lever 				       nfsd_file_rhash_params);
693ce502f81SChuck Lever 		if (!nf)
694ce502f81SChuck Lever 			break;
695ce502f81SChuck Lever 		nfsd_file_unhash_and_dispose(nf, dispose);
696a8455110SChuck Lever 		count++;
697ce502f81SChuck Lever 	} while (1);
698ce502f81SChuck Lever 	rcu_read_unlock();
699a8455110SChuck Lever 	return count;
70065294c1fSJeff Layton }
70165294c1fSJeff Layton 
70265294c1fSJeff Layton /**
70365294c1fSJeff Layton  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
70465294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
70565294c1fSJeff Layton  *
706a8455110SChuck Lever  * Unhash and put, then flush and fput all cache items associated with @inode.
70765294c1fSJeff Layton  */
70865294c1fSJeff Layton void
70965294c1fSJeff Layton nfsd_file_close_inode_sync(struct inode *inode)
71065294c1fSJeff Layton {
71165294c1fSJeff Layton 	LIST_HEAD(dispose);
712a8455110SChuck Lever 	unsigned int count;
71365294c1fSJeff Layton 
714a8455110SChuck Lever 	count = __nfsd_file_close_inode(inode, &dispose);
715a8455110SChuck Lever 	trace_nfsd_file_close_inode_sync(inode, count);
71665294c1fSJeff Layton 	nfsd_file_dispose_list_sync(&dispose);
71765294c1fSJeff Layton }
71865294c1fSJeff Layton 
71965294c1fSJeff Layton /**
72019598141STrond Myklebust  * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
72165294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
72265294c1fSJeff Layton  *
723a8455110SChuck Lever  * Unhash and put all cache item associated with @inode.
72465294c1fSJeff Layton  */
72565294c1fSJeff Layton static void
72665294c1fSJeff Layton nfsd_file_close_inode(struct inode *inode)
72765294c1fSJeff Layton {
72865294c1fSJeff Layton 	LIST_HEAD(dispose);
729a8455110SChuck Lever 	unsigned int count;
73065294c1fSJeff Layton 
731a8455110SChuck Lever 	count = __nfsd_file_close_inode(inode, &dispose);
732a8455110SChuck Lever 	trace_nfsd_file_close_inode(inode, count);
7339542e6a6STrond Myklebust 	nfsd_file_dispose_list_delayed(&dispose);
73465294c1fSJeff Layton }
73565294c1fSJeff Layton 
73665294c1fSJeff Layton /**
73765294c1fSJeff Layton  * nfsd_file_delayed_close - close unused nfsd_files
73865294c1fSJeff Layton  * @work: dummy
73965294c1fSJeff Layton  *
74065294c1fSJeff Layton  * Walk the LRU list and close any entries that have not been used since
74165294c1fSJeff Layton  * the last scan.
74265294c1fSJeff Layton  */
74365294c1fSJeff Layton static void
74465294c1fSJeff Layton nfsd_file_delayed_close(struct work_struct *work)
74565294c1fSJeff Layton {
74665294c1fSJeff Layton 	LIST_HEAD(head);
7479542e6a6STrond Myklebust 	struct nfsd_fcache_disposal *l = container_of(work,
7489542e6a6STrond Myklebust 			struct nfsd_fcache_disposal, work);
74965294c1fSJeff Layton 
7509542e6a6STrond Myklebust 	nfsd_file_list_remove_disposal(&head, l);
7519542e6a6STrond Myklebust 	nfsd_file_dispose_list(&head);
75265294c1fSJeff Layton }
75365294c1fSJeff Layton 
75465294c1fSJeff Layton static int
75565294c1fSJeff Layton nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
75665294c1fSJeff Layton 			    void *data)
75765294c1fSJeff Layton {
75865294c1fSJeff Layton 	struct file_lock *fl = data;
75965294c1fSJeff Layton 
76065294c1fSJeff Layton 	/* Only close files for F_SETLEASE leases */
76165294c1fSJeff Layton 	if (fl->fl_flags & FL_LEASE)
76265294c1fSJeff Layton 		nfsd_file_close_inode_sync(file_inode(fl->fl_file));
76365294c1fSJeff Layton 	return 0;
76465294c1fSJeff Layton }
76565294c1fSJeff Layton 
76665294c1fSJeff Layton static struct notifier_block nfsd_file_lease_notifier = {
76765294c1fSJeff Layton 	.notifier_call = nfsd_file_lease_notifier_call,
76865294c1fSJeff Layton };
76965294c1fSJeff Layton 
77065294c1fSJeff Layton static int
771b9a1b977SAmir Goldstein nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
772b9a1b977SAmir Goldstein 				struct inode *inode, struct inode *dir,
773950cc0d2SAmir Goldstein 				const struct qstr *name, u32 cookie)
77465294c1fSJeff Layton {
77524dca905SGabriel Krisman Bertazi 	if (WARN_ON_ONCE(!inode))
77624dca905SGabriel Krisman Bertazi 		return 0;
77724dca905SGabriel Krisman Bertazi 
77865294c1fSJeff Layton 	trace_nfsd_file_fsnotify_handle_event(inode, mask);
77965294c1fSJeff Layton 
78065294c1fSJeff Layton 	/* Should be no marks on non-regular files */
78165294c1fSJeff Layton 	if (!S_ISREG(inode->i_mode)) {
78265294c1fSJeff Layton 		WARN_ON_ONCE(1);
78365294c1fSJeff Layton 		return 0;
78465294c1fSJeff Layton 	}
78565294c1fSJeff Layton 
78665294c1fSJeff Layton 	/* don't close files if this was not the last link */
78765294c1fSJeff Layton 	if (mask & FS_ATTRIB) {
78865294c1fSJeff Layton 		if (inode->i_nlink)
78965294c1fSJeff Layton 			return 0;
79065294c1fSJeff Layton 	}
79165294c1fSJeff Layton 
79265294c1fSJeff Layton 	nfsd_file_close_inode(inode);
79365294c1fSJeff Layton 	return 0;
79465294c1fSJeff Layton }
79565294c1fSJeff Layton 
79665294c1fSJeff Layton 
79765294c1fSJeff Layton static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
798b9a1b977SAmir Goldstein 	.handle_inode_event = nfsd_file_fsnotify_handle_event,
79965294c1fSJeff Layton 	.free_mark = nfsd_file_mark_free,
80065294c1fSJeff Layton };
80165294c1fSJeff Layton 
80265294c1fSJeff Layton int
80365294c1fSJeff Layton nfsd_file_cache_init(void)
80465294c1fSJeff Layton {
805fc22945eSChuck Lever 	int ret;
80665294c1fSJeff Layton 
807c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
808c7b824c3SChuck Lever 	if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
80965294c1fSJeff Layton 		return 0;
81065294c1fSJeff Layton 
811fc22945eSChuck Lever 	ret = rhashtable_init(&nfsd_file_rhash_tbl, &nfsd_file_rhash_params);
812fc22945eSChuck Lever 	if (ret)
813fc22945eSChuck Lever 		return ret;
814fc22945eSChuck Lever 
815fc22945eSChuck Lever 	ret = -ENOMEM;
8169542e6a6STrond Myklebust 	nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
8179542e6a6STrond Myklebust 	if (!nfsd_filecache_wq)
8189542e6a6STrond Myklebust 		goto out;
8199542e6a6STrond Myklebust 
82065294c1fSJeff Layton 	nfsd_file_slab = kmem_cache_create("nfsd_file",
82165294c1fSJeff Layton 				sizeof(struct nfsd_file), 0, 0, NULL);
82265294c1fSJeff Layton 	if (!nfsd_file_slab) {
82365294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_slab\n");
82465294c1fSJeff Layton 		goto out_err;
82565294c1fSJeff Layton 	}
82665294c1fSJeff Layton 
82765294c1fSJeff Layton 	nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
82865294c1fSJeff Layton 					sizeof(struct nfsd_file_mark), 0, 0, NULL);
82965294c1fSJeff Layton 	if (!nfsd_file_mark_slab) {
83065294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
83165294c1fSJeff Layton 		goto out_err;
83265294c1fSJeff Layton 	}
83365294c1fSJeff Layton 
83465294c1fSJeff Layton 
83565294c1fSJeff Layton 	ret = list_lru_init(&nfsd_file_lru);
83665294c1fSJeff Layton 	if (ret) {
83765294c1fSJeff Layton 		pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
83865294c1fSJeff Layton 		goto out_err;
83965294c1fSJeff Layton 	}
84065294c1fSJeff Layton 
841e33c267aSRoman Gushchin 	ret = register_shrinker(&nfsd_file_shrinker, "nfsd-filecache");
84265294c1fSJeff Layton 	if (ret) {
84365294c1fSJeff Layton 		pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
84465294c1fSJeff Layton 		goto out_lru;
84565294c1fSJeff Layton 	}
84665294c1fSJeff Layton 
84765294c1fSJeff Layton 	ret = lease_register_notifier(&nfsd_file_lease_notifier);
84865294c1fSJeff Layton 	if (ret) {
84965294c1fSJeff Layton 		pr_err("nfsd: unable to register lease notifier: %d\n", ret);
85065294c1fSJeff Layton 		goto out_shrinker;
85165294c1fSJeff Layton 	}
85265294c1fSJeff Layton 
853867a448dSAmir Goldstein 	nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
854b8962a9dSAmir Goldstein 							FSNOTIFY_GROUP_NOFS);
85565294c1fSJeff Layton 	if (IS_ERR(nfsd_file_fsnotify_group)) {
85665294c1fSJeff Layton 		pr_err("nfsd: unable to create fsnotify group: %ld\n",
85765294c1fSJeff Layton 			PTR_ERR(nfsd_file_fsnotify_group));
858231307dfSHuang Guobin 		ret = PTR_ERR(nfsd_file_fsnotify_group);
85965294c1fSJeff Layton 		nfsd_file_fsnotify_group = NULL;
86065294c1fSJeff Layton 		goto out_notifier;
86165294c1fSJeff Layton 	}
86265294c1fSJeff Layton 
8639542e6a6STrond Myklebust 	INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
86465294c1fSJeff Layton out:
86565294c1fSJeff Layton 	return ret;
86665294c1fSJeff Layton out_notifier:
86765294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
86865294c1fSJeff Layton out_shrinker:
86965294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
87065294c1fSJeff Layton out_lru:
87165294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
87265294c1fSJeff Layton out_err:
87365294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
87465294c1fSJeff Layton 	nfsd_file_slab = NULL;
87565294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
87665294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
8779542e6a6STrond Myklebust 	destroy_workqueue(nfsd_filecache_wq);
8789542e6a6STrond Myklebust 	nfsd_filecache_wq = NULL;
879fc22945eSChuck Lever 	rhashtable_destroy(&nfsd_file_rhash_tbl);
88065294c1fSJeff Layton 	goto out;
88165294c1fSJeff Layton }
88265294c1fSJeff Layton 
883c7b824c3SChuck Lever static void
884c7b824c3SChuck Lever __nfsd_file_cache_purge(struct net *net)
88565294c1fSJeff Layton {
886ce502f81SChuck Lever 	struct rhashtable_iter iter;
88765294c1fSJeff Layton 	struct nfsd_file *nf;
88865294c1fSJeff Layton 	LIST_HEAD(dispose);
88965294c1fSJeff Layton 
890ce502f81SChuck Lever 	rhashtable_walk_enter(&nfsd_file_rhash_tbl, &iter);
891ce502f81SChuck Lever 	do {
892ce502f81SChuck Lever 		rhashtable_walk_start(&iter);
89365294c1fSJeff Layton 
894ce502f81SChuck Lever 		nf = rhashtable_walk_next(&iter);
895ce502f81SChuck Lever 		while (!IS_ERR_OR_NULL(nf)) {
896*d3aefd2bSJeff Layton 			if (!net || nf->nf_net == net)
8978d0d254bSJeff Layton 				nfsd_file_unhash_and_dispose(nf, &dispose);
898ce502f81SChuck Lever 			nf = rhashtable_walk_next(&iter);
89965294c1fSJeff Layton 		}
900ce502f81SChuck Lever 
901ce502f81SChuck Lever 		rhashtable_walk_stop(&iter);
902ce502f81SChuck Lever 	} while (nf == ERR_PTR(-EAGAIN));
903ce502f81SChuck Lever 	rhashtable_walk_exit(&iter);
904ce502f81SChuck Lever 
90565294c1fSJeff Layton 	nfsd_file_dispose_list(&dispose);
90665294c1fSJeff Layton }
90765294c1fSJeff Layton 
9089542e6a6STrond Myklebust static struct nfsd_fcache_disposal *
9091463b38eSNeilBrown nfsd_alloc_fcache_disposal(void)
9109542e6a6STrond Myklebust {
9119542e6a6STrond Myklebust 	struct nfsd_fcache_disposal *l;
9129542e6a6STrond Myklebust 
9139542e6a6STrond Myklebust 	l = kmalloc(sizeof(*l), GFP_KERNEL);
9149542e6a6STrond Myklebust 	if (!l)
9159542e6a6STrond Myklebust 		return NULL;
9169542e6a6STrond Myklebust 	INIT_WORK(&l->work, nfsd_file_delayed_close);
9179542e6a6STrond Myklebust 	spin_lock_init(&l->lock);
9189542e6a6STrond Myklebust 	INIT_LIST_HEAD(&l->freeme);
9199542e6a6STrond Myklebust 	return l;
9209542e6a6STrond Myklebust }
9219542e6a6STrond Myklebust 
9229542e6a6STrond Myklebust static void
9239542e6a6STrond Myklebust nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
9249542e6a6STrond Myklebust {
9259542e6a6STrond Myklebust 	cancel_work_sync(&l->work);
9269542e6a6STrond Myklebust 	nfsd_file_dispose_list(&l->freeme);
9271463b38eSNeilBrown 	kfree(l);
9289542e6a6STrond Myklebust }
9299542e6a6STrond Myklebust 
9309542e6a6STrond Myklebust static void
9319542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(struct net *net)
9329542e6a6STrond Myklebust {
9331463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9341463b38eSNeilBrown 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
9359542e6a6STrond Myklebust 
9369542e6a6STrond Myklebust 	nfsd_free_fcache_disposal(l);
9379542e6a6STrond Myklebust }
9389542e6a6STrond Myklebust 
9399542e6a6STrond Myklebust int
9409542e6a6STrond Myklebust nfsd_file_cache_start_net(struct net *net)
9419542e6a6STrond Myklebust {
9421463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9431463b38eSNeilBrown 
9441463b38eSNeilBrown 	nn->fcache_disposal = nfsd_alloc_fcache_disposal();
9451463b38eSNeilBrown 	return nn->fcache_disposal ? 0 : -ENOMEM;
9469542e6a6STrond Myklebust }
9479542e6a6STrond Myklebust 
948c7b824c3SChuck Lever /**
949c7b824c3SChuck Lever  * nfsd_file_cache_purge - Remove all cache items associated with @net
950c7b824c3SChuck Lever  * @net: target net namespace
951c7b824c3SChuck Lever  *
952c7b824c3SChuck Lever  */
953c7b824c3SChuck Lever void
954c7b824c3SChuck Lever nfsd_file_cache_purge(struct net *net)
955c7b824c3SChuck Lever {
956c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
957c7b824c3SChuck Lever 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
958c7b824c3SChuck Lever 		__nfsd_file_cache_purge(net);
959c7b824c3SChuck Lever }
960c7b824c3SChuck Lever 
9619542e6a6STrond Myklebust void
9629542e6a6STrond Myklebust nfsd_file_cache_shutdown_net(struct net *net)
9639542e6a6STrond Myklebust {
9649542e6a6STrond Myklebust 	nfsd_file_cache_purge(net);
9659542e6a6STrond Myklebust 	nfsd_free_fcache_disposal_net(net);
9669542e6a6STrond Myklebust }
9679542e6a6STrond Myklebust 
96865294c1fSJeff Layton void
96965294c1fSJeff Layton nfsd_file_cache_shutdown(void)
97065294c1fSJeff Layton {
9718b330f78SChuck Lever 	int i;
9728b330f78SChuck Lever 
973c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
974c7b824c3SChuck Lever 	if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
975c7b824c3SChuck Lever 		return;
97665294c1fSJeff Layton 
97765294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
97865294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
97965294c1fSJeff Layton 	/*
98065294c1fSJeff Layton 	 * make sure all callers of nfsd_file_lru_cb are done before
98165294c1fSJeff Layton 	 * calling nfsd_file_cache_purge
98265294c1fSJeff Layton 	 */
98365294c1fSJeff Layton 	cancel_delayed_work_sync(&nfsd_filecache_laundrette);
984c7b824c3SChuck Lever 	__nfsd_file_cache_purge(NULL);
98565294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
98665294c1fSJeff Layton 	rcu_barrier();
98765294c1fSJeff Layton 	fsnotify_put_group(nfsd_file_fsnotify_group);
98865294c1fSJeff Layton 	nfsd_file_fsnotify_group = NULL;
98965294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
99065294c1fSJeff Layton 	nfsd_file_slab = NULL;
99165294c1fSJeff Layton 	fsnotify_wait_marks_destroyed();
99265294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
99365294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
9949542e6a6STrond Myklebust 	destroy_workqueue(nfsd_filecache_wq);
9959542e6a6STrond Myklebust 	nfsd_filecache_wq = NULL;
996fc22945eSChuck Lever 	rhashtable_destroy(&nfsd_file_rhash_tbl);
99765294c1fSJeff Layton 
9988b330f78SChuck Lever 	for_each_possible_cpu(i) {
9998b330f78SChuck Lever 		per_cpu(nfsd_file_cache_hits, i) = 0;
10008b330f78SChuck Lever 		per_cpu(nfsd_file_acquisitions, i) = 0;
10018b330f78SChuck Lever 		per_cpu(nfsd_file_releases, i) = 0;
10028b330f78SChuck Lever 		per_cpu(nfsd_file_total_age, i) = 0;
10038b330f78SChuck Lever 		per_cpu(nfsd_file_pages_flushed, i) = 0;
10048b330f78SChuck Lever 		per_cpu(nfsd_file_evictions, i) = 0;
100565294c1fSJeff Layton 	}
100665294c1fSJeff Layton }
100765294c1fSJeff Layton 
100865294c1fSJeff Layton /**
1009ce502f81SChuck Lever  * nfsd_file_is_cached - are there any cached open files for this inode?
1010ce502f81SChuck Lever  * @inode: inode to check
101165294c1fSJeff Layton  *
1012ce502f81SChuck Lever  * The lookup matches inodes in all net namespaces and is atomic wrt
1013ce502f81SChuck Lever  * nfsd_file_acquire().
1014ce502f81SChuck Lever  *
1015ce502f81SChuck Lever  * Return values:
1016ce502f81SChuck Lever  *   %true: filecache contains at least one file matching this inode
1017ce502f81SChuck Lever  *   %false: filecache contains no files matching this inode
101865294c1fSJeff Layton  */
101965294c1fSJeff Layton bool
102065294c1fSJeff Layton nfsd_file_is_cached(struct inode *inode)
102165294c1fSJeff Layton {
1022ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
1023ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_INODE,
1024ce502f81SChuck Lever 		.inode	= inode,
1025ce502f81SChuck Lever 	};
102665294c1fSJeff Layton 	bool ret = false;
102765294c1fSJeff Layton 
1028ce502f81SChuck Lever 	if (rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key,
1029ce502f81SChuck Lever 				   nfsd_file_rhash_params) != NULL)
103065294c1fSJeff Layton 		ret = true;
103154f7df70SChuck Lever 	trace_nfsd_file_is_cached(inode, (int)ret);
103265294c1fSJeff Layton 	return ret;
103365294c1fSJeff Layton }
103465294c1fSJeff Layton 
1035fb70bf12SChuck Lever static __be32
1036be023006SChuck Lever nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1037fb70bf12SChuck Lever 		     unsigned int may_flags, struct nfsd_file **pnf, bool open)
103865294c1fSJeff Layton {
1039ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
1040ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_FULL,
1041ce502f81SChuck Lever 		.need	= may_flags & NFSD_FILE_MAY_MASK,
1042ce502f81SChuck Lever 		.net	= SVC_NET(rqstp),
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);
1079243a5263SJeff Layton 	if (ret == -EEXIST)
1080243a5263SJeff Layton 		goto retry;
1081243a5263SJeff Layton 	trace_nfsd_file_insert_err(rqstp, key.inode, may_flags, ret);
1082243a5263SJeff Layton 	status = nfserr_jukebox;
1083243a5263SJeff Layton 	goto out_status;
108465294c1fSJeff Layton 
108565294c1fSJeff Layton wait_for_construction:
108665294c1fSJeff Layton 	wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
108765294c1fSJeff Layton 
108865294c1fSJeff Layton 	/* Did construction of this file fail? */
108965294c1fSJeff Layton 	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
1090ce502f81SChuck Lever 		trace_nfsd_file_cons_err(rqstp, key.inode, may_flags, nf);
1091243a5263SJeff Layton 		if (!open_retry) {
109228c7d86bSTrond Myklebust 			status = nfserr_jukebox;
109328c7d86bSTrond Myklebust 			goto out;
109428c7d86bSTrond Myklebust 		}
1095243a5263SJeff Layton 		open_retry = false;
109665294c1fSJeff Layton 		nfsd_file_put_noref(nf);
109765294c1fSJeff Layton 		goto retry;
109865294c1fSJeff Layton 	}
109965294c1fSJeff Layton 
11004a0e73e6SChuck Lever 	nfsd_file_lru_remove(nf);
110165294c1fSJeff Layton 	this_cpu_inc(nfsd_file_cache_hits);
110265294c1fSJeff Layton 
110323ba98deSJeff Layton 	status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
110465294c1fSJeff Layton out:
110565294c1fSJeff Layton 	if (status == nfs_ok) {
110629d4bdbbSChuck Lever 		if (open)
110729d4bdbbSChuck Lever 			this_cpu_inc(nfsd_file_acquisitions);
110865294c1fSJeff Layton 		*pnf = nf;
110965294c1fSJeff Layton 	} else {
111065294c1fSJeff Layton 		nfsd_file_put(nf);
111165294c1fSJeff Layton 		nf = NULL;
111265294c1fSJeff Layton 	}
111365294c1fSJeff Layton 
111454f7df70SChuck Lever out_status:
1115ce502f81SChuck Lever 	put_cred(key.cred);
1116be023006SChuck Lever 	if (open)
1117ce502f81SChuck Lever 		trace_nfsd_file_acquire(rqstp, key.inode, may_flags, nf, status);
111865294c1fSJeff Layton 	return status;
111965294c1fSJeff Layton 
112065294c1fSJeff Layton open_file:
1121b40a2839SChuck Lever 	trace_nfsd_file_alloc(nf);
1122427f5f83SChuck Lever 	nf->nf_mark = nfsd_file_mark_find_or_create(nf, key.inode);
1123fb70bf12SChuck Lever 	if (nf->nf_mark) {
11240122e882SChuck Lever 		if (open) {
1125f4d84c52SChuck Lever 			status = nfsd_open_verified(rqstp, fhp, may_flags,
1126f4d84c52SChuck Lever 						    &nf->nf_file);
11270122e882SChuck Lever 			trace_nfsd_file_open(nf, status);
11280122e882SChuck Lever 		} else
1129fb70bf12SChuck Lever 			status = nfs_ok;
1130fb70bf12SChuck Lever 	} else
113165294c1fSJeff Layton 		status = nfserr_jukebox;
113265294c1fSJeff Layton 	/*
113365294c1fSJeff Layton 	 * If construction failed, or we raced with a call to unlink()
113465294c1fSJeff Layton 	 * then unhash.
113565294c1fSJeff Layton 	 */
1136ce502f81SChuck Lever 	if (status != nfs_ok || key.inode->i_nlink == 0)
1137ce502f81SChuck Lever 		if (nfsd_file_unhash(nf))
113865294c1fSJeff Layton 			nfsd_file_put_noref(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 /**
1146fb70bf12SChuck Lever  * nfsd_file_acquire - Get a struct nfsd_file with an open file
1147fb70bf12SChuck Lever  * @rqstp: the RPC transaction being executed
1148fb70bf12SChuck Lever  * @fhp: the NFS filehandle of the file to be opened
1149fb70bf12SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
1150fb70bf12SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1151fb70bf12SChuck Lever  *
1152fb70bf12SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1153fb70bf12SChuck Lever  * network byte order is returned.
1154fb70bf12SChuck Lever  */
1155fb70bf12SChuck Lever __be32
1156fb70bf12SChuck Lever nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1157fb70bf12SChuck Lever 		  unsigned int may_flags, struct nfsd_file **pnf)
1158fb70bf12SChuck Lever {
1159be023006SChuck Lever 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, true);
1160fb70bf12SChuck Lever }
1161fb70bf12SChuck Lever 
1162fb70bf12SChuck Lever /**
1163fb70bf12SChuck Lever  * nfsd_file_create - Get a struct nfsd_file, do not open
1164fb70bf12SChuck Lever  * @rqstp: the RPC transaction being executed
1165fb70bf12SChuck Lever  * @fhp: the NFS filehandle of the file just created
1166fb70bf12SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
1167fb70bf12SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1168fb70bf12SChuck Lever  *
1169fb70bf12SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1170fb70bf12SChuck Lever  * network byte order is returned.
1171fb70bf12SChuck Lever  */
1172fb70bf12SChuck Lever __be32
1173fb70bf12SChuck Lever nfsd_file_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1174fb70bf12SChuck Lever 		 unsigned int may_flags, struct nfsd_file **pnf)
1175fb70bf12SChuck Lever {
1176be023006SChuck Lever 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, false);
1177fb70bf12SChuck Lever }
1178fb70bf12SChuck Lever 
117965294c1fSJeff Layton /*
118065294c1fSJeff Layton  * Note that fields may be added, removed or reordered in the future. Programs
118165294c1fSJeff Layton  * scraping this file for info should test the labels to ensure they're
118265294c1fSJeff Layton  * getting the correct field.
118365294c1fSJeff Layton  */
11841342f9ddSChenXiaoSong int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
118565294c1fSJeff Layton {
1186df2aff52SChuck Lever 	unsigned long releases = 0, pages_flushed = 0, evictions = 0;
1187df2aff52SChuck Lever 	unsigned long hits = 0, acquisitions = 0;
1188ce502f81SChuck Lever 	unsigned int i, count = 0, buckets = 0;
1189904940e9SChuck Lever 	unsigned long lru = 0, total_age = 0;
119065294c1fSJeff Layton 
1191ce502f81SChuck Lever 	/* Serialize with server shutdown */
119265294c1fSJeff Layton 	mutex_lock(&nfsd_mutex);
1193c7b824c3SChuck Lever 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
1194ce502f81SChuck Lever 		struct bucket_table *tbl;
1195ce502f81SChuck Lever 		struct rhashtable *ht;
1196ce502f81SChuck Lever 
11970fd244c1SChuck Lever 		lru = list_lru_count(&nfsd_file_lru);
1198ce502f81SChuck Lever 
1199ce502f81SChuck Lever 		rcu_read_lock();
1200ce502f81SChuck Lever 		ht = &nfsd_file_rhash_tbl;
1201ce502f81SChuck Lever 		count = atomic_read(&ht->nelems);
1202ce502f81SChuck Lever 		tbl = rht_dereference_rcu(ht->tbl, ht);
1203ce502f81SChuck Lever 		buckets = tbl->size;
1204ce502f81SChuck Lever 		rcu_read_unlock();
120565294c1fSJeff Layton 	}
120665294c1fSJeff Layton 	mutex_unlock(&nfsd_mutex);
120765294c1fSJeff Layton 
120829d4bdbbSChuck Lever 	for_each_possible_cpu(i) {
120965294c1fSJeff Layton 		hits += per_cpu(nfsd_file_cache_hits, i);
121029d4bdbbSChuck Lever 		acquisitions += per_cpu(nfsd_file_acquisitions, i);
1211d6329327SChuck Lever 		releases += per_cpu(nfsd_file_releases, i);
1212904940e9SChuck Lever 		total_age += per_cpu(nfsd_file_total_age, i);
121394660cc1SChuck Lever 		evictions += per_cpu(nfsd_file_evictions, i);
1214df2aff52SChuck Lever 		pages_flushed += per_cpu(nfsd_file_pages_flushed, i);
121529d4bdbbSChuck Lever 	}
121665294c1fSJeff Layton 
121765294c1fSJeff Layton 	seq_printf(m, "total entries: %u\n", count);
1218ce502f81SChuck Lever 	seq_printf(m, "hash buckets:  %u\n", buckets);
12190fd244c1SChuck Lever 	seq_printf(m, "lru entries:   %lu\n", lru);
122065294c1fSJeff Layton 	seq_printf(m, "cache hits:    %lu\n", hits);
122129d4bdbbSChuck Lever 	seq_printf(m, "acquisitions:  %lu\n", acquisitions);
1222d6329327SChuck Lever 	seq_printf(m, "releases:      %lu\n", releases);
122394660cc1SChuck Lever 	seq_printf(m, "evictions:     %lu\n", evictions);
1224904940e9SChuck Lever 	if (releases)
1225904940e9SChuck Lever 		seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1226904940e9SChuck Lever 	else
1227904940e9SChuck Lever 		seq_printf(m, "mean age (ms): -\n");
1228df2aff52SChuck Lever 	seq_printf(m, "pages flushed: %lu\n", pages_flushed);
122965294c1fSJeff Layton 	return 0;
123065294c1fSJeff Layton }
1231