xref: /openbmc/linux/fs/nfsd/filecache.c (revision 1342f9dd3fc219089deeb2620f6790f19b4129b1)
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 
40865294c1fSJeff Layton /*
40965294c1fSJeff Layton  * Return true if the file was unhashed.
41065294c1fSJeff Layton  */
41165294c1fSJeff Layton static bool
412ce502f81SChuck Lever nfsd_file_unhash_and_dispose(struct nfsd_file *nf, struct list_head *dispose)
41365294c1fSJeff Layton {
414ce502f81SChuck Lever 	trace_nfsd_file_unhash_and_dispose(nf);
41565294c1fSJeff Layton 	if (!nfsd_file_unhash(nf))
41665294c1fSJeff Layton 		return false;
41765294c1fSJeff Layton 	/* keep final reference for nfsd_file_lru_dispose */
418689827cdSTrond Myklebust 	if (refcount_dec_not_one(&nf->nf_ref))
41965294c1fSJeff Layton 		return true;
42065294c1fSJeff Layton 
4214a0e73e6SChuck Lever 	nfsd_file_lru_remove(nf);
42265294c1fSJeff Layton 	list_add(&nf->nf_lru, dispose);
42365294c1fSJeff Layton 	return true;
42465294c1fSJeff Layton }
42565294c1fSJeff Layton 
426b6669305STrond Myklebust static void
42765294c1fSJeff Layton nfsd_file_put_noref(struct nfsd_file *nf)
42865294c1fSJeff Layton {
42965294c1fSJeff Layton 	trace_nfsd_file_put(nf);
43065294c1fSJeff Layton 
431689827cdSTrond Myklebust 	if (refcount_dec_and_test(&nf->nf_ref)) {
43265294c1fSJeff Layton 		WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
4334a0e73e6SChuck Lever 		nfsd_file_lru_remove(nf);
43465294c1fSJeff Layton 		nfsd_file_free(nf);
43565294c1fSJeff Layton 	}
43665294c1fSJeff Layton }
43765294c1fSJeff Layton 
43865294c1fSJeff Layton void
43965294c1fSJeff Layton nfsd_file_put(struct nfsd_file *nf)
44065294c1fSJeff Layton {
44108af54b3SChuck Lever 	might_sleep();
44208af54b3SChuck Lever 
4434a0e73e6SChuck Lever 	nfsd_file_lru_add(nf);
44499939792STrond Myklebust 	if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) {
4456b8a9433STrond Myklebust 		nfsd_file_flush(nf);
446b6669305STrond Myklebust 		nfsd_file_put_noref(nf);
447b6c71c66SChuck Lever 	} else if (nf->nf_file) {
448b6669305STrond Myklebust 		nfsd_file_put_noref(nf);
4499542e6a6STrond Myklebust 		nfsd_file_schedule_laundrette();
450b6c71c66SChuck Lever 	} else
451b6c71c66SChuck Lever 		nfsd_file_put_noref(nf);
45265294c1fSJeff Layton }
45365294c1fSJeff Layton 
4545e138c4aSChuck Lever /**
4555e138c4aSChuck Lever  * nfsd_file_close - Close an nfsd_file
4565e138c4aSChuck Lever  * @nf: nfsd_file to close
4575e138c4aSChuck Lever  *
4585e138c4aSChuck Lever  * If this is the final reference for @nf, free it immediately.
4595e138c4aSChuck Lever  * This reflects an on-the-wire CLOSE or DELEGRETURN into the
4605e138c4aSChuck Lever  * VFS and exported filesystem.
4615e138c4aSChuck Lever  */
4625e138c4aSChuck Lever void nfsd_file_close(struct nfsd_file *nf)
4635e138c4aSChuck Lever {
4645e138c4aSChuck Lever 	nfsd_file_put(nf);
4655e138c4aSChuck Lever 	if (refcount_dec_if_one(&nf->nf_ref)) {
4665e138c4aSChuck Lever 		nfsd_file_unhash(nf);
4675e138c4aSChuck Lever 		nfsd_file_lru_remove(nf);
4685e138c4aSChuck Lever 		nfsd_file_free(nf);
4695e138c4aSChuck Lever 	}
47065294c1fSJeff Layton }
47165294c1fSJeff Layton 
47265294c1fSJeff Layton struct nfsd_file *
47365294c1fSJeff Layton nfsd_file_get(struct nfsd_file *nf)
47465294c1fSJeff Layton {
475689827cdSTrond Myklebust 	if (likely(refcount_inc_not_zero(&nf->nf_ref)))
47665294c1fSJeff Layton 		return nf;
47765294c1fSJeff Layton 	return NULL;
47865294c1fSJeff Layton }
47965294c1fSJeff Layton 
48065294c1fSJeff Layton static void
48165294c1fSJeff Layton nfsd_file_dispose_list(struct list_head *dispose)
48265294c1fSJeff Layton {
48365294c1fSJeff Layton 	struct nfsd_file *nf;
48465294c1fSJeff Layton 
48565294c1fSJeff Layton 	while(!list_empty(dispose)) {
48665294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
487668ed92eSChuck Lever 		list_del_init(&nf->nf_lru);
4886b8a9433STrond Myklebust 		nfsd_file_flush(nf);
48965294c1fSJeff Layton 		nfsd_file_put_noref(nf);
49065294c1fSJeff Layton 	}
49165294c1fSJeff Layton }
49265294c1fSJeff Layton 
49365294c1fSJeff Layton static void
49465294c1fSJeff Layton nfsd_file_dispose_list_sync(struct list_head *dispose)
49565294c1fSJeff Layton {
49665294c1fSJeff Layton 	bool flush = false;
49765294c1fSJeff Layton 	struct nfsd_file *nf;
49865294c1fSJeff Layton 
49965294c1fSJeff Layton 	while(!list_empty(dispose)) {
50065294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
501668ed92eSChuck Lever 		list_del_init(&nf->nf_lru);
5026b8a9433STrond Myklebust 		nfsd_file_flush(nf);
503689827cdSTrond Myklebust 		if (!refcount_dec_and_test(&nf->nf_ref))
50465294c1fSJeff Layton 			continue;
50565294c1fSJeff Layton 		if (nfsd_file_free(nf))
50665294c1fSJeff Layton 			flush = true;
50765294c1fSJeff Layton 	}
50865294c1fSJeff Layton 	if (flush)
50965294c1fSJeff Layton 		flush_delayed_fput();
51065294c1fSJeff Layton }
51165294c1fSJeff Layton 
5129542e6a6STrond Myklebust static void
5139542e6a6STrond Myklebust nfsd_file_list_remove_disposal(struct list_head *dst,
5149542e6a6STrond Myklebust 		struct nfsd_fcache_disposal *l)
5159542e6a6STrond Myklebust {
5169542e6a6STrond Myklebust 	spin_lock(&l->lock);
5179542e6a6STrond Myklebust 	list_splice_init(&l->freeme, dst);
5189542e6a6STrond Myklebust 	spin_unlock(&l->lock);
5199542e6a6STrond Myklebust }
5209542e6a6STrond Myklebust 
5219542e6a6STrond Myklebust static void
5229542e6a6STrond Myklebust nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
5239542e6a6STrond Myklebust {
5241463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5251463b38eSNeilBrown 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
5269542e6a6STrond Myklebust 
5279542e6a6STrond Myklebust 	spin_lock(&l->lock);
5289542e6a6STrond Myklebust 	list_splice_tail_init(files, &l->freeme);
5299542e6a6STrond Myklebust 	spin_unlock(&l->lock);
5309542e6a6STrond Myklebust 	queue_work(nfsd_filecache_wq, &l->work);
5319542e6a6STrond Myklebust }
5329542e6a6STrond Myklebust 
5339542e6a6STrond Myklebust static void
5349542e6a6STrond Myklebust nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
5359542e6a6STrond Myklebust 		struct net *net)
5369542e6a6STrond Myklebust {
5379542e6a6STrond Myklebust 	struct nfsd_file *nf, *tmp;
5389542e6a6STrond Myklebust 
5399542e6a6STrond Myklebust 	list_for_each_entry_safe(nf, tmp, src, nf_lru) {
5409542e6a6STrond Myklebust 		if (nf->nf_net == net)
5419542e6a6STrond Myklebust 			list_move_tail(&nf->nf_lru, dst);
5429542e6a6STrond Myklebust 	}
5439542e6a6STrond Myklebust }
5449542e6a6STrond Myklebust 
5459542e6a6STrond Myklebust static void
5469542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(struct list_head *dispose)
5479542e6a6STrond Myklebust {
5489542e6a6STrond Myklebust 	LIST_HEAD(list);
5499542e6a6STrond Myklebust 	struct nfsd_file *nf;
5509542e6a6STrond Myklebust 
5519542e6a6STrond Myklebust 	while(!list_empty(dispose)) {
5529542e6a6STrond Myklebust 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
5539542e6a6STrond Myklebust 		nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
5549542e6a6STrond Myklebust 		nfsd_file_list_add_disposal(&list, nf->nf_net);
5559542e6a6STrond Myklebust 	}
5569542e6a6STrond Myklebust }
5579542e6a6STrond Myklebust 
5584a0e73e6SChuck Lever /**
5594a0e73e6SChuck Lever  * nfsd_file_lru_cb - Examine an entry on the LRU list
5604a0e73e6SChuck Lever  * @item: LRU entry to examine
5614a0e73e6SChuck Lever  * @lru: controlling LRU
5624a0e73e6SChuck Lever  * @lock: LRU list lock (unused)
5634a0e73e6SChuck Lever  * @arg: dispose list
5644a0e73e6SChuck Lever  *
56565294c1fSJeff Layton  * Note this can deadlock with nfsd_file_cache_purge.
5664a0e73e6SChuck Lever  *
5674a0e73e6SChuck Lever  * Return values:
5684a0e73e6SChuck Lever  *   %LRU_REMOVED: @item was removed from the LRU
569edead3a5SChuck Lever  *   %LRU_ROTATE: @item is to be moved to the LRU tail
5704a0e73e6SChuck Lever  *   %LRU_SKIP: @item cannot be evicted
57165294c1fSJeff Layton  */
57265294c1fSJeff Layton static enum lru_status
57365294c1fSJeff Layton nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
57465294c1fSJeff Layton 		 spinlock_t *lock, void *arg)
57565294c1fSJeff Layton 	__releases(lock)
57665294c1fSJeff Layton 	__acquires(lock)
57765294c1fSJeff Layton {
57865294c1fSJeff Layton 	struct list_head *head = arg;
57965294c1fSJeff Layton 	struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
58065294c1fSJeff Layton 
58165294c1fSJeff Layton 	/*
58265294c1fSJeff Layton 	 * Do a lockless refcount check. The hashtable holds one reference, so
58365294c1fSJeff Layton 	 * we look to see if anything else has a reference, or if any have
58465294c1fSJeff Layton 	 * been put since the shrinker last ran. Those don't get unhashed and
58565294c1fSJeff Layton 	 * released.
58665294c1fSJeff Layton 	 *
58765294c1fSJeff Layton 	 * Note that in the put path, we set the flag and then decrement the
58865294c1fSJeff Layton 	 * counter. Here we check the counter and then test and clear the flag.
58965294c1fSJeff Layton 	 * That order is deliberate to ensure that we can do this locklessly.
59065294c1fSJeff Layton 	 */
591c46203acSChuck Lever 	if (refcount_read(&nf->nf_ref) > 1) {
5924a0e73e6SChuck Lever 		list_lru_isolate(lru, &nf->nf_lru);
593c46203acSChuck Lever 		trace_nfsd_file_gc_in_use(nf);
5944a0e73e6SChuck Lever 		return LRU_REMOVED;
595c46203acSChuck Lever 	}
596055b24a8STrond Myklebust 
597055b24a8STrond Myklebust 	/*
598055b24a8STrond Myklebust 	 * Don't throw out files that are still undergoing I/O or
599055b24a8STrond Myklebust 	 * that have uncleared errors pending.
600055b24a8STrond Myklebust 	 */
601c46203acSChuck Lever 	if (nfsd_file_check_writeback(nf)) {
602c46203acSChuck Lever 		trace_nfsd_file_gc_writeback(nf);
60365294c1fSJeff Layton 		return LRU_SKIP;
60465294c1fSJeff Layton 	}
60565294c1fSJeff Layton 
606c46203acSChuck Lever 	if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
607c46203acSChuck Lever 		trace_nfsd_file_gc_referenced(nf);
608edead3a5SChuck Lever 		return LRU_ROTATE;
60965294c1fSJeff Layton 	}
61065294c1fSJeff Layton 
611c46203acSChuck Lever 	if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
612c46203acSChuck Lever 		trace_nfsd_file_gc_hashed(nf);
613c46203acSChuck Lever 		return LRU_SKIP;
614c46203acSChuck Lever 	}
61565294c1fSJeff Layton 
61665294c1fSJeff Layton 	list_lru_isolate_move(lru, &nf->nf_lru, head);
61794660cc1SChuck Lever 	this_cpu_inc(nfsd_file_evictions);
618c46203acSChuck Lever 	trace_nfsd_file_gc_disposed(nf);
61965294c1fSJeff Layton 	return LRU_REMOVED;
62065294c1fSJeff Layton }
62165294c1fSJeff Layton 
6220bac5a26SChuck Lever /*
6230bac5a26SChuck Lever  * Unhash items on @dispose immediately, then queue them on the
6240bac5a26SChuck Lever  * disposal workqueue to finish releasing them in the background.
6250bac5a26SChuck Lever  *
6260bac5a26SChuck Lever  * cel: Note that between the time list_lru_shrink_walk runs and
6270bac5a26SChuck Lever  * now, these items are in the hash table but marked unhashed.
6280bac5a26SChuck Lever  * Why release these outside of lru_cb ? There's no lock ordering
6290bac5a26SChuck Lever  * problem since lru_cb currently takes no lock.
6300bac5a26SChuck Lever  */
6310bac5a26SChuck Lever static void nfsd_file_gc_dispose_list(struct list_head *dispose)
6320bac5a26SChuck Lever {
6330bac5a26SChuck Lever 	struct nfsd_file *nf;
6340bac5a26SChuck Lever 
635cb7ec76eSChuck Lever 	list_for_each_entry(nf, dispose, nf_lru)
636cb7ec76eSChuck Lever 		nfsd_file_hash_remove(nf);
6370bac5a26SChuck Lever 	nfsd_file_dispose_list_delayed(dispose);
6389542e6a6STrond Myklebust }
6399542e6a6STrond Myklebust 
6409542e6a6STrond Myklebust static void
6419542e6a6STrond Myklebust nfsd_file_gc(void)
6429542e6a6STrond Myklebust {
6433bc6d347SChuck Lever 	LIST_HEAD(dispose);
64494660cc1SChuck Lever 	unsigned long ret;
6453bc6d347SChuck Lever 
64694660cc1SChuck Lever 	ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
647edead3a5SChuck Lever 			    &dispose, list_lru_count(&nfsd_file_lru));
64894660cc1SChuck Lever 	trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
6493bc6d347SChuck Lever 	nfsd_file_gc_dispose_list(&dispose);
6509542e6a6STrond Myklebust }
6519542e6a6STrond Myklebust 
6529542e6a6STrond Myklebust static void
6539542e6a6STrond Myklebust nfsd_file_gc_worker(struct work_struct *work)
6549542e6a6STrond Myklebust {
6559542e6a6STrond Myklebust 	nfsd_file_gc();
6569542e6a6STrond Myklebust 	nfsd_file_schedule_laundrette();
65765294c1fSJeff Layton }
65865294c1fSJeff Layton 
65965294c1fSJeff Layton static unsigned long
66065294c1fSJeff Layton nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
66165294c1fSJeff Layton {
66265294c1fSJeff Layton 	return list_lru_count(&nfsd_file_lru);
66365294c1fSJeff Layton }
66465294c1fSJeff Layton 
66565294c1fSJeff Layton static unsigned long
66665294c1fSJeff Layton nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
66765294c1fSJeff Layton {
66839f1d1ffSChuck Lever 	LIST_HEAD(dispose);
66939f1d1ffSChuck Lever 	unsigned long ret;
67039f1d1ffSChuck Lever 
67139f1d1ffSChuck Lever 	ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
67239f1d1ffSChuck Lever 				   nfsd_file_lru_cb, &dispose);
67394660cc1SChuck Lever 	trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
67439f1d1ffSChuck Lever 	nfsd_file_gc_dispose_list(&dispose);
67539f1d1ffSChuck Lever 	return ret;
67665294c1fSJeff Layton }
67765294c1fSJeff Layton 
67865294c1fSJeff Layton static struct shrinker	nfsd_file_shrinker = {
67965294c1fSJeff Layton 	.scan_objects = nfsd_file_lru_scan,
68065294c1fSJeff Layton 	.count_objects = nfsd_file_lru_count,
68165294c1fSJeff Layton 	.seeks = 1,
68265294c1fSJeff Layton };
68365294c1fSJeff Layton 
684a8455110SChuck Lever /*
685a8455110SChuck Lever  * Find all cache items across all net namespaces that match @inode and
686a8455110SChuck Lever  * move them to @dispose. The lookup is atomic wrt nfsd_file_acquire().
687a8455110SChuck Lever  */
688a8455110SChuck Lever static unsigned int
689a8455110SChuck Lever __nfsd_file_close_inode(struct inode *inode, struct list_head *dispose)
69065294c1fSJeff Layton {
691ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
692ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_INODE,
693ce502f81SChuck Lever 		.inode	= inode,
694ce502f81SChuck Lever 	};
695a8455110SChuck Lever 	unsigned int count = 0;
69665294c1fSJeff Layton 	struct nfsd_file *nf;
69765294c1fSJeff Layton 
698ce502f81SChuck Lever 	rcu_read_lock();
699ce502f81SChuck Lever 	do {
700ce502f81SChuck Lever 		nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
701ce502f81SChuck Lever 				       nfsd_file_rhash_params);
702ce502f81SChuck Lever 		if (!nf)
703ce502f81SChuck Lever 			break;
704ce502f81SChuck Lever 		nfsd_file_unhash_and_dispose(nf, dispose);
705a8455110SChuck Lever 		count++;
706ce502f81SChuck Lever 	} while (1);
707ce502f81SChuck Lever 	rcu_read_unlock();
708a8455110SChuck Lever 	return count;
70965294c1fSJeff Layton }
71065294c1fSJeff Layton 
71165294c1fSJeff Layton /**
71265294c1fSJeff Layton  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
71365294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
71465294c1fSJeff Layton  *
715a8455110SChuck Lever  * Unhash and put, then flush and fput all cache items associated with @inode.
71665294c1fSJeff Layton  */
71765294c1fSJeff Layton void
71865294c1fSJeff Layton nfsd_file_close_inode_sync(struct inode *inode)
71965294c1fSJeff Layton {
72065294c1fSJeff Layton 	LIST_HEAD(dispose);
721a8455110SChuck Lever 	unsigned int count;
72265294c1fSJeff Layton 
723a8455110SChuck Lever 	count = __nfsd_file_close_inode(inode, &dispose);
724a8455110SChuck Lever 	trace_nfsd_file_close_inode_sync(inode, count);
72565294c1fSJeff Layton 	nfsd_file_dispose_list_sync(&dispose);
72665294c1fSJeff Layton }
72765294c1fSJeff Layton 
72865294c1fSJeff Layton /**
72919598141STrond Myklebust  * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
73065294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
73165294c1fSJeff Layton  *
732a8455110SChuck Lever  * Unhash and put all cache item associated with @inode.
73365294c1fSJeff Layton  */
73465294c1fSJeff Layton static void
73565294c1fSJeff Layton nfsd_file_close_inode(struct inode *inode)
73665294c1fSJeff Layton {
73765294c1fSJeff Layton 	LIST_HEAD(dispose);
738a8455110SChuck Lever 	unsigned int count;
73965294c1fSJeff Layton 
740a8455110SChuck Lever 	count = __nfsd_file_close_inode(inode, &dispose);
741a8455110SChuck Lever 	trace_nfsd_file_close_inode(inode, count);
7429542e6a6STrond Myklebust 	nfsd_file_dispose_list_delayed(&dispose);
74365294c1fSJeff Layton }
74465294c1fSJeff Layton 
74565294c1fSJeff Layton /**
74665294c1fSJeff Layton  * nfsd_file_delayed_close - close unused nfsd_files
74765294c1fSJeff Layton  * @work: dummy
74865294c1fSJeff Layton  *
74965294c1fSJeff Layton  * Walk the LRU list and close any entries that have not been used since
75065294c1fSJeff Layton  * the last scan.
75165294c1fSJeff Layton  *
75265294c1fSJeff Layton  * Note this can deadlock with nfsd_file_cache_purge.
75365294c1fSJeff Layton  */
75465294c1fSJeff Layton static void
75565294c1fSJeff Layton nfsd_file_delayed_close(struct work_struct *work)
75665294c1fSJeff Layton {
75765294c1fSJeff Layton 	LIST_HEAD(head);
7589542e6a6STrond Myklebust 	struct nfsd_fcache_disposal *l = container_of(work,
7599542e6a6STrond Myklebust 			struct nfsd_fcache_disposal, work);
76065294c1fSJeff Layton 
7619542e6a6STrond Myklebust 	nfsd_file_list_remove_disposal(&head, l);
7629542e6a6STrond Myklebust 	nfsd_file_dispose_list(&head);
76365294c1fSJeff Layton }
76465294c1fSJeff Layton 
76565294c1fSJeff Layton static int
76665294c1fSJeff Layton nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
76765294c1fSJeff Layton 			    void *data)
76865294c1fSJeff Layton {
76965294c1fSJeff Layton 	struct file_lock *fl = data;
77065294c1fSJeff Layton 
77165294c1fSJeff Layton 	/* Only close files for F_SETLEASE leases */
77265294c1fSJeff Layton 	if (fl->fl_flags & FL_LEASE)
77365294c1fSJeff Layton 		nfsd_file_close_inode_sync(file_inode(fl->fl_file));
77465294c1fSJeff Layton 	return 0;
77565294c1fSJeff Layton }
77665294c1fSJeff Layton 
77765294c1fSJeff Layton static struct notifier_block nfsd_file_lease_notifier = {
77865294c1fSJeff Layton 	.notifier_call = nfsd_file_lease_notifier_call,
77965294c1fSJeff Layton };
78065294c1fSJeff Layton 
78165294c1fSJeff Layton static int
782b9a1b977SAmir Goldstein nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
783b9a1b977SAmir Goldstein 				struct inode *inode, struct inode *dir,
784950cc0d2SAmir Goldstein 				const struct qstr *name, u32 cookie)
78565294c1fSJeff Layton {
78624dca905SGabriel Krisman Bertazi 	if (WARN_ON_ONCE(!inode))
78724dca905SGabriel Krisman Bertazi 		return 0;
78824dca905SGabriel Krisman Bertazi 
78965294c1fSJeff Layton 	trace_nfsd_file_fsnotify_handle_event(inode, mask);
79065294c1fSJeff Layton 
79165294c1fSJeff Layton 	/* Should be no marks on non-regular files */
79265294c1fSJeff Layton 	if (!S_ISREG(inode->i_mode)) {
79365294c1fSJeff Layton 		WARN_ON_ONCE(1);
79465294c1fSJeff Layton 		return 0;
79565294c1fSJeff Layton 	}
79665294c1fSJeff Layton 
79765294c1fSJeff Layton 	/* don't close files if this was not the last link */
79865294c1fSJeff Layton 	if (mask & FS_ATTRIB) {
79965294c1fSJeff Layton 		if (inode->i_nlink)
80065294c1fSJeff Layton 			return 0;
80165294c1fSJeff Layton 	}
80265294c1fSJeff Layton 
80365294c1fSJeff Layton 	nfsd_file_close_inode(inode);
80465294c1fSJeff Layton 	return 0;
80565294c1fSJeff Layton }
80665294c1fSJeff Layton 
80765294c1fSJeff Layton 
80865294c1fSJeff Layton static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
809b9a1b977SAmir Goldstein 	.handle_inode_event = nfsd_file_fsnotify_handle_event,
81065294c1fSJeff Layton 	.free_mark = nfsd_file_mark_free,
81165294c1fSJeff Layton };
81265294c1fSJeff Layton 
81365294c1fSJeff Layton int
81465294c1fSJeff Layton nfsd_file_cache_init(void)
81565294c1fSJeff Layton {
816fc22945eSChuck Lever 	int ret;
81765294c1fSJeff Layton 
818c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
819c7b824c3SChuck Lever 	if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
82065294c1fSJeff Layton 		return 0;
82165294c1fSJeff Layton 
822fc22945eSChuck Lever 	ret = rhashtable_init(&nfsd_file_rhash_tbl, &nfsd_file_rhash_params);
823fc22945eSChuck Lever 	if (ret)
824fc22945eSChuck Lever 		return ret;
825fc22945eSChuck Lever 
826fc22945eSChuck Lever 	ret = -ENOMEM;
8279542e6a6STrond Myklebust 	nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
8289542e6a6STrond Myklebust 	if (!nfsd_filecache_wq)
8299542e6a6STrond Myklebust 		goto out;
8309542e6a6STrond Myklebust 
83165294c1fSJeff Layton 	nfsd_file_slab = kmem_cache_create("nfsd_file",
83265294c1fSJeff Layton 				sizeof(struct nfsd_file), 0, 0, NULL);
83365294c1fSJeff Layton 	if (!nfsd_file_slab) {
83465294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_slab\n");
83565294c1fSJeff Layton 		goto out_err;
83665294c1fSJeff Layton 	}
83765294c1fSJeff Layton 
83865294c1fSJeff Layton 	nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
83965294c1fSJeff Layton 					sizeof(struct nfsd_file_mark), 0, 0, NULL);
84065294c1fSJeff Layton 	if (!nfsd_file_mark_slab) {
84165294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
84265294c1fSJeff Layton 		goto out_err;
84365294c1fSJeff Layton 	}
84465294c1fSJeff Layton 
84565294c1fSJeff Layton 
84665294c1fSJeff Layton 	ret = list_lru_init(&nfsd_file_lru);
84765294c1fSJeff Layton 	if (ret) {
84865294c1fSJeff Layton 		pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
84965294c1fSJeff Layton 		goto out_err;
85065294c1fSJeff Layton 	}
85165294c1fSJeff Layton 
852e33c267aSRoman Gushchin 	ret = register_shrinker(&nfsd_file_shrinker, "nfsd-filecache");
85365294c1fSJeff Layton 	if (ret) {
85465294c1fSJeff Layton 		pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
85565294c1fSJeff Layton 		goto out_lru;
85665294c1fSJeff Layton 	}
85765294c1fSJeff Layton 
85865294c1fSJeff Layton 	ret = lease_register_notifier(&nfsd_file_lease_notifier);
85965294c1fSJeff Layton 	if (ret) {
86065294c1fSJeff Layton 		pr_err("nfsd: unable to register lease notifier: %d\n", ret);
86165294c1fSJeff Layton 		goto out_shrinker;
86265294c1fSJeff Layton 	}
86365294c1fSJeff Layton 
864867a448dSAmir Goldstein 	nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
865b8962a9dSAmir Goldstein 							FSNOTIFY_GROUP_NOFS);
86665294c1fSJeff Layton 	if (IS_ERR(nfsd_file_fsnotify_group)) {
86765294c1fSJeff Layton 		pr_err("nfsd: unable to create fsnotify group: %ld\n",
86865294c1fSJeff Layton 			PTR_ERR(nfsd_file_fsnotify_group));
869231307dfSHuang Guobin 		ret = PTR_ERR(nfsd_file_fsnotify_group);
87065294c1fSJeff Layton 		nfsd_file_fsnotify_group = NULL;
87165294c1fSJeff Layton 		goto out_notifier;
87265294c1fSJeff Layton 	}
87365294c1fSJeff Layton 
8749542e6a6STrond Myklebust 	INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
87565294c1fSJeff Layton out:
87665294c1fSJeff Layton 	return ret;
87765294c1fSJeff Layton out_notifier:
87865294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
87965294c1fSJeff Layton out_shrinker:
88065294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
88165294c1fSJeff Layton out_lru:
88265294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
88365294c1fSJeff Layton out_err:
88465294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
88565294c1fSJeff Layton 	nfsd_file_slab = NULL;
88665294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
88765294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
8889542e6a6STrond Myklebust 	destroy_workqueue(nfsd_filecache_wq);
8899542e6a6STrond Myklebust 	nfsd_filecache_wq = NULL;
890fc22945eSChuck Lever 	rhashtable_destroy(&nfsd_file_rhash_tbl);
89165294c1fSJeff Layton 	goto out;
89265294c1fSJeff Layton }
89365294c1fSJeff Layton 
89465294c1fSJeff Layton /*
89565294c1fSJeff Layton  * Note this can deadlock with nfsd_file_lru_cb.
89665294c1fSJeff Layton  */
897c7b824c3SChuck Lever static void
898c7b824c3SChuck Lever __nfsd_file_cache_purge(struct net *net)
89965294c1fSJeff Layton {
900ce502f81SChuck Lever 	struct rhashtable_iter iter;
90165294c1fSJeff Layton 	struct nfsd_file *nf;
90265294c1fSJeff Layton 	LIST_HEAD(dispose);
90365294c1fSJeff Layton 	bool del;
90465294c1fSJeff Layton 
905ce502f81SChuck Lever 	rhashtable_walk_enter(&nfsd_file_rhash_tbl, &iter);
906ce502f81SChuck Lever 	do {
907ce502f81SChuck Lever 		rhashtable_walk_start(&iter);
90865294c1fSJeff Layton 
909ce502f81SChuck Lever 		nf = rhashtable_walk_next(&iter);
910ce502f81SChuck Lever 		while (!IS_ERR_OR_NULL(nf)) {
9115e113224STrond Myklebust 			if (net && nf->nf_net != net)
9125e113224STrond Myklebust 				continue;
913ce502f81SChuck Lever 			del = nfsd_file_unhash_and_dispose(nf, &dispose);
91465294c1fSJeff Layton 
91565294c1fSJeff Layton 			/*
91665294c1fSJeff Layton 			 * Deadlock detected! Something marked this entry as
91765294c1fSJeff Layton 			 * unhased, but hasn't removed it from the hash list.
91865294c1fSJeff Layton 			 */
91965294c1fSJeff Layton 			WARN_ON_ONCE(!del);
920ce502f81SChuck Lever 
921ce502f81SChuck Lever 			nf = rhashtable_walk_next(&iter);
92265294c1fSJeff Layton 		}
923ce502f81SChuck Lever 
924ce502f81SChuck Lever 		rhashtable_walk_stop(&iter);
925ce502f81SChuck Lever 	} while (nf == ERR_PTR(-EAGAIN));
926ce502f81SChuck Lever 	rhashtable_walk_exit(&iter);
927ce502f81SChuck Lever 
92865294c1fSJeff Layton 	nfsd_file_dispose_list(&dispose);
92965294c1fSJeff Layton }
93065294c1fSJeff Layton 
9319542e6a6STrond Myklebust static struct nfsd_fcache_disposal *
9321463b38eSNeilBrown nfsd_alloc_fcache_disposal(void)
9339542e6a6STrond Myklebust {
9349542e6a6STrond Myklebust 	struct nfsd_fcache_disposal *l;
9359542e6a6STrond Myklebust 
9369542e6a6STrond Myklebust 	l = kmalloc(sizeof(*l), GFP_KERNEL);
9379542e6a6STrond Myklebust 	if (!l)
9389542e6a6STrond Myklebust 		return NULL;
9399542e6a6STrond Myklebust 	INIT_WORK(&l->work, nfsd_file_delayed_close);
9409542e6a6STrond Myklebust 	spin_lock_init(&l->lock);
9419542e6a6STrond Myklebust 	INIT_LIST_HEAD(&l->freeme);
9429542e6a6STrond Myklebust 	return l;
9439542e6a6STrond Myklebust }
9449542e6a6STrond Myklebust 
9459542e6a6STrond Myklebust static void
9469542e6a6STrond Myklebust nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
9479542e6a6STrond Myklebust {
9489542e6a6STrond Myklebust 	cancel_work_sync(&l->work);
9499542e6a6STrond Myklebust 	nfsd_file_dispose_list(&l->freeme);
9501463b38eSNeilBrown 	kfree(l);
9519542e6a6STrond Myklebust }
9529542e6a6STrond Myklebust 
9539542e6a6STrond Myklebust static void
9549542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(struct net *net)
9559542e6a6STrond Myklebust {
9561463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9571463b38eSNeilBrown 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
9589542e6a6STrond Myklebust 
9599542e6a6STrond Myklebust 	nfsd_free_fcache_disposal(l);
9609542e6a6STrond Myklebust }
9619542e6a6STrond Myklebust 
9629542e6a6STrond Myklebust int
9639542e6a6STrond Myklebust nfsd_file_cache_start_net(struct net *net)
9649542e6a6STrond Myklebust {
9651463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9661463b38eSNeilBrown 
9671463b38eSNeilBrown 	nn->fcache_disposal = nfsd_alloc_fcache_disposal();
9681463b38eSNeilBrown 	return nn->fcache_disposal ? 0 : -ENOMEM;
9699542e6a6STrond Myklebust }
9709542e6a6STrond Myklebust 
971c7b824c3SChuck Lever /**
972c7b824c3SChuck Lever  * nfsd_file_cache_purge - Remove all cache items associated with @net
973c7b824c3SChuck Lever  * @net: target net namespace
974c7b824c3SChuck Lever  *
975c7b824c3SChuck Lever  */
976c7b824c3SChuck Lever void
977c7b824c3SChuck Lever nfsd_file_cache_purge(struct net *net)
978c7b824c3SChuck Lever {
979c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
980c7b824c3SChuck Lever 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
981c7b824c3SChuck Lever 		__nfsd_file_cache_purge(net);
982c7b824c3SChuck Lever }
983c7b824c3SChuck Lever 
9849542e6a6STrond Myklebust void
9859542e6a6STrond Myklebust nfsd_file_cache_shutdown_net(struct net *net)
9869542e6a6STrond Myklebust {
9879542e6a6STrond Myklebust 	nfsd_file_cache_purge(net);
9889542e6a6STrond Myklebust 	nfsd_free_fcache_disposal_net(net);
9899542e6a6STrond Myklebust }
9909542e6a6STrond Myklebust 
99165294c1fSJeff Layton void
99265294c1fSJeff Layton nfsd_file_cache_shutdown(void)
99365294c1fSJeff Layton {
9948b330f78SChuck Lever 	int i;
9958b330f78SChuck Lever 
996c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
997c7b824c3SChuck Lever 	if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
998c7b824c3SChuck Lever 		return;
99965294c1fSJeff Layton 
100065294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
100165294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
100265294c1fSJeff Layton 	/*
100365294c1fSJeff Layton 	 * make sure all callers of nfsd_file_lru_cb are done before
100465294c1fSJeff Layton 	 * calling nfsd_file_cache_purge
100565294c1fSJeff Layton 	 */
100665294c1fSJeff Layton 	cancel_delayed_work_sync(&nfsd_filecache_laundrette);
1007c7b824c3SChuck Lever 	__nfsd_file_cache_purge(NULL);
100865294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
100965294c1fSJeff Layton 	rcu_barrier();
101065294c1fSJeff Layton 	fsnotify_put_group(nfsd_file_fsnotify_group);
101165294c1fSJeff Layton 	nfsd_file_fsnotify_group = NULL;
101265294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
101365294c1fSJeff Layton 	nfsd_file_slab = NULL;
101465294c1fSJeff Layton 	fsnotify_wait_marks_destroyed();
101565294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
101665294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
10179542e6a6STrond Myklebust 	destroy_workqueue(nfsd_filecache_wq);
10189542e6a6STrond Myklebust 	nfsd_filecache_wq = NULL;
1019fc22945eSChuck Lever 	rhashtable_destroy(&nfsd_file_rhash_tbl);
102065294c1fSJeff Layton 
10218b330f78SChuck Lever 	for_each_possible_cpu(i) {
10228b330f78SChuck Lever 		per_cpu(nfsd_file_cache_hits, i) = 0;
10238b330f78SChuck Lever 		per_cpu(nfsd_file_acquisitions, i) = 0;
10248b330f78SChuck Lever 		per_cpu(nfsd_file_releases, i) = 0;
10258b330f78SChuck Lever 		per_cpu(nfsd_file_total_age, i) = 0;
10268b330f78SChuck Lever 		per_cpu(nfsd_file_pages_flushed, i) = 0;
10278b330f78SChuck Lever 		per_cpu(nfsd_file_evictions, i) = 0;
102865294c1fSJeff Layton 	}
102965294c1fSJeff Layton }
103065294c1fSJeff Layton 
103165294c1fSJeff Layton /**
1032ce502f81SChuck Lever  * nfsd_file_is_cached - are there any cached open files for this inode?
1033ce502f81SChuck Lever  * @inode: inode to check
103465294c1fSJeff Layton  *
1035ce502f81SChuck Lever  * The lookup matches inodes in all net namespaces and is atomic wrt
1036ce502f81SChuck Lever  * nfsd_file_acquire().
1037ce502f81SChuck Lever  *
1038ce502f81SChuck Lever  * Return values:
1039ce502f81SChuck Lever  *   %true: filecache contains at least one file matching this inode
1040ce502f81SChuck Lever  *   %false: filecache contains no files matching this inode
104165294c1fSJeff Layton  */
104265294c1fSJeff Layton bool
104365294c1fSJeff Layton nfsd_file_is_cached(struct inode *inode)
104465294c1fSJeff Layton {
1045ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
1046ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_INODE,
1047ce502f81SChuck Lever 		.inode	= inode,
1048ce502f81SChuck Lever 	};
104965294c1fSJeff Layton 	bool ret = false;
105065294c1fSJeff Layton 
1051ce502f81SChuck Lever 	if (rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key,
1052ce502f81SChuck Lever 				   nfsd_file_rhash_params) != NULL)
105365294c1fSJeff Layton 		ret = true;
105454f7df70SChuck Lever 	trace_nfsd_file_is_cached(inode, (int)ret);
105565294c1fSJeff Layton 	return ret;
105665294c1fSJeff Layton }
105765294c1fSJeff Layton 
1058fb70bf12SChuck Lever static __be32
1059be023006SChuck Lever nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1060fb70bf12SChuck Lever 		     unsigned int may_flags, struct nfsd_file **pnf, bool open)
106165294c1fSJeff Layton {
1062ce502f81SChuck Lever 	struct nfsd_file_lookup_key key = {
1063ce502f81SChuck Lever 		.type	= NFSD_FILE_KEY_FULL,
1064ce502f81SChuck Lever 		.need	= may_flags & NFSD_FILE_MAY_MASK,
1065ce502f81SChuck Lever 		.net	= SVC_NET(rqstp),
1066ce502f81SChuck Lever 	};
106765294c1fSJeff Layton 	struct nfsd_file *nf, *new;
106828c7d86bSTrond Myklebust 	bool retry = true;
1069ce502f81SChuck Lever 	__be32 status;
107065294c1fSJeff Layton 
107165294c1fSJeff Layton 	status = fh_verify(rqstp, fhp, S_IFREG,
107265294c1fSJeff Layton 				may_flags|NFSD_MAY_OWNER_OVERRIDE);
107365294c1fSJeff Layton 	if (status != nfs_ok)
107465294c1fSJeff Layton 		return status;
1075ce502f81SChuck Lever 	key.inode = d_inode(fhp->fh_dentry);
1076ce502f81SChuck Lever 	key.cred = get_current_cred();
107765294c1fSJeff Layton 
107865294c1fSJeff Layton retry:
1079ce502f81SChuck Lever 	/* Avoid allocation if the item is already in cache */
1080ce502f81SChuck Lever 	nf = rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key,
1081ce502f81SChuck Lever 				    nfsd_file_rhash_params);
1082ce502f81SChuck Lever 	if (nf)
1083ce502f81SChuck Lever 		nf = nfsd_file_get(nf);
108465294c1fSJeff Layton 	if (nf)
108565294c1fSJeff Layton 		goto wait_for_construction;
108665294c1fSJeff Layton 
1087ce502f81SChuck Lever 	new = nfsd_file_alloc(&key, may_flags);
108865294c1fSJeff Layton 	if (!new) {
108954f7df70SChuck Lever 		status = nfserr_jukebox;
109054f7df70SChuck Lever 		goto out_status;
109165294c1fSJeff Layton 	}
109265294c1fSJeff Layton 
1093ce502f81SChuck Lever 	nf = rhashtable_lookup_get_insert_key(&nfsd_file_rhash_tbl,
1094ce502f81SChuck Lever 					      &key, &new->nf_rhash,
1095ce502f81SChuck Lever 					      nfsd_file_rhash_params);
1096ce502f81SChuck Lever 	if (!nf) {
1097ce502f81SChuck Lever 		nf = new;
109865294c1fSJeff Layton 		goto open_file;
1099ce502f81SChuck Lever 	}
1100ce502f81SChuck Lever 	if (IS_ERR(nf))
1101ce502f81SChuck Lever 		goto insert_err;
1102ce502f81SChuck Lever 	nf = nfsd_file_get(nf);
1103ce502f81SChuck Lever 	if (nf == NULL) {
1104ce502f81SChuck Lever 		nf = new;
1105ce502f81SChuck Lever 		goto open_file;
1106ce502f81SChuck Lever 	}
110765294c1fSJeff Layton 	nfsd_file_slab_free(&new->nf_rcu);
110865294c1fSJeff Layton 
110965294c1fSJeff Layton wait_for_construction:
111065294c1fSJeff Layton 	wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
111165294c1fSJeff Layton 
111265294c1fSJeff Layton 	/* Did construction of this file fail? */
111365294c1fSJeff Layton 	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
1114ce502f81SChuck Lever 		trace_nfsd_file_cons_err(rqstp, key.inode, may_flags, nf);
111528c7d86bSTrond Myklebust 		if (!retry) {
111628c7d86bSTrond Myklebust 			status = nfserr_jukebox;
111728c7d86bSTrond Myklebust 			goto out;
111828c7d86bSTrond Myklebust 		}
111928c7d86bSTrond Myklebust 		retry = false;
112065294c1fSJeff Layton 		nfsd_file_put_noref(nf);
112165294c1fSJeff Layton 		goto retry;
112265294c1fSJeff Layton 	}
112365294c1fSJeff Layton 
11244a0e73e6SChuck Lever 	nfsd_file_lru_remove(nf);
112565294c1fSJeff Layton 	this_cpu_inc(nfsd_file_cache_hits);
112665294c1fSJeff Layton 
112723ba98deSJeff Layton 	status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
112865294c1fSJeff Layton out:
112965294c1fSJeff Layton 	if (status == nfs_ok) {
113029d4bdbbSChuck Lever 		if (open)
113129d4bdbbSChuck Lever 			this_cpu_inc(nfsd_file_acquisitions);
113265294c1fSJeff Layton 		*pnf = nf;
113365294c1fSJeff Layton 	} else {
113465294c1fSJeff Layton 		nfsd_file_put(nf);
113565294c1fSJeff Layton 		nf = NULL;
113665294c1fSJeff Layton 	}
113765294c1fSJeff Layton 
113854f7df70SChuck Lever out_status:
1139ce502f81SChuck Lever 	put_cred(key.cred);
1140be023006SChuck Lever 	if (open)
1141ce502f81SChuck Lever 		trace_nfsd_file_acquire(rqstp, key.inode, may_flags, nf, status);
114265294c1fSJeff Layton 	return status;
114365294c1fSJeff Layton 
114465294c1fSJeff Layton open_file:
1145b40a2839SChuck Lever 	trace_nfsd_file_alloc(nf);
1146427f5f83SChuck Lever 	nf->nf_mark = nfsd_file_mark_find_or_create(nf, key.inode);
1147fb70bf12SChuck Lever 	if (nf->nf_mark) {
11480122e882SChuck Lever 		if (open) {
1149f4d84c52SChuck Lever 			status = nfsd_open_verified(rqstp, fhp, may_flags,
1150f4d84c52SChuck Lever 						    &nf->nf_file);
11510122e882SChuck Lever 			trace_nfsd_file_open(nf, status);
11520122e882SChuck Lever 		} else
1153fb70bf12SChuck Lever 			status = nfs_ok;
1154fb70bf12SChuck Lever 	} else
115565294c1fSJeff Layton 		status = nfserr_jukebox;
115665294c1fSJeff Layton 	/*
115765294c1fSJeff Layton 	 * If construction failed, or we raced with a call to unlink()
115865294c1fSJeff Layton 	 * then unhash.
115965294c1fSJeff Layton 	 */
1160ce502f81SChuck Lever 	if (status != nfs_ok || key.inode->i_nlink == 0)
1161ce502f81SChuck Lever 		if (nfsd_file_unhash(nf))
116265294c1fSJeff Layton 			nfsd_file_put_noref(nf);
116365294c1fSJeff Layton 	clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
116465294c1fSJeff Layton 	smp_mb__after_atomic();
116565294c1fSJeff Layton 	wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
116665294c1fSJeff Layton 	goto out;
1167ce502f81SChuck Lever 
1168ce502f81SChuck Lever insert_err:
1169ce502f81SChuck Lever 	nfsd_file_slab_free(&new->nf_rcu);
1170ce502f81SChuck Lever 	trace_nfsd_file_insert_err(rqstp, key.inode, may_flags, PTR_ERR(nf));
1171ce502f81SChuck Lever 	nf = NULL;
1172ce502f81SChuck Lever 	status = nfserr_jukebox;
1173ce502f81SChuck Lever 	goto out_status;
117465294c1fSJeff Layton }
117565294c1fSJeff Layton 
1176fb70bf12SChuck Lever /**
1177fb70bf12SChuck Lever  * nfsd_file_acquire - Get a struct nfsd_file with an open file
1178fb70bf12SChuck Lever  * @rqstp: the RPC transaction being executed
1179fb70bf12SChuck Lever  * @fhp: the NFS filehandle of the file to be opened
1180fb70bf12SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
1181fb70bf12SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1182fb70bf12SChuck Lever  *
1183fb70bf12SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1184fb70bf12SChuck Lever  * network byte order is returned.
1185fb70bf12SChuck Lever  */
1186fb70bf12SChuck Lever __be32
1187fb70bf12SChuck Lever nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1188fb70bf12SChuck Lever 		  unsigned int may_flags, struct nfsd_file **pnf)
1189fb70bf12SChuck Lever {
1190be023006SChuck Lever 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, true);
1191fb70bf12SChuck Lever }
1192fb70bf12SChuck Lever 
1193fb70bf12SChuck Lever /**
1194fb70bf12SChuck Lever  * nfsd_file_create - Get a struct nfsd_file, do not open
1195fb70bf12SChuck Lever  * @rqstp: the RPC transaction being executed
1196fb70bf12SChuck Lever  * @fhp: the NFS filehandle of the file just created
1197fb70bf12SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
1198fb70bf12SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1199fb70bf12SChuck Lever  *
1200fb70bf12SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1201fb70bf12SChuck Lever  * network byte order is returned.
1202fb70bf12SChuck Lever  */
1203fb70bf12SChuck Lever __be32
1204fb70bf12SChuck Lever nfsd_file_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1205fb70bf12SChuck Lever 		 unsigned int may_flags, struct nfsd_file **pnf)
1206fb70bf12SChuck Lever {
1207be023006SChuck Lever 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, false);
1208fb70bf12SChuck Lever }
1209fb70bf12SChuck Lever 
121065294c1fSJeff Layton /*
121165294c1fSJeff Layton  * Note that fields may be added, removed or reordered in the future. Programs
121265294c1fSJeff Layton  * scraping this file for info should test the labels to ensure they're
121365294c1fSJeff Layton  * getting the correct field.
121465294c1fSJeff Layton  */
1215*1342f9ddSChenXiaoSong int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
121665294c1fSJeff Layton {
1217df2aff52SChuck Lever 	unsigned long releases = 0, pages_flushed = 0, evictions = 0;
1218df2aff52SChuck Lever 	unsigned long hits = 0, acquisitions = 0;
1219ce502f81SChuck Lever 	unsigned int i, count = 0, buckets = 0;
1220904940e9SChuck Lever 	unsigned long lru = 0, total_age = 0;
122165294c1fSJeff Layton 
1222ce502f81SChuck Lever 	/* Serialize with server shutdown */
122365294c1fSJeff Layton 	mutex_lock(&nfsd_mutex);
1224c7b824c3SChuck Lever 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
1225ce502f81SChuck Lever 		struct bucket_table *tbl;
1226ce502f81SChuck Lever 		struct rhashtable *ht;
1227ce502f81SChuck Lever 
12280fd244c1SChuck Lever 		lru = list_lru_count(&nfsd_file_lru);
1229ce502f81SChuck Lever 
1230ce502f81SChuck Lever 		rcu_read_lock();
1231ce502f81SChuck Lever 		ht = &nfsd_file_rhash_tbl;
1232ce502f81SChuck Lever 		count = atomic_read(&ht->nelems);
1233ce502f81SChuck Lever 		tbl = rht_dereference_rcu(ht->tbl, ht);
1234ce502f81SChuck Lever 		buckets = tbl->size;
1235ce502f81SChuck Lever 		rcu_read_unlock();
123665294c1fSJeff Layton 	}
123765294c1fSJeff Layton 	mutex_unlock(&nfsd_mutex);
123865294c1fSJeff Layton 
123929d4bdbbSChuck Lever 	for_each_possible_cpu(i) {
124065294c1fSJeff Layton 		hits += per_cpu(nfsd_file_cache_hits, i);
124129d4bdbbSChuck Lever 		acquisitions += per_cpu(nfsd_file_acquisitions, i);
1242d6329327SChuck Lever 		releases += per_cpu(nfsd_file_releases, i);
1243904940e9SChuck Lever 		total_age += per_cpu(nfsd_file_total_age, i);
124494660cc1SChuck Lever 		evictions += per_cpu(nfsd_file_evictions, i);
1245df2aff52SChuck Lever 		pages_flushed += per_cpu(nfsd_file_pages_flushed, i);
124629d4bdbbSChuck Lever 	}
124765294c1fSJeff Layton 
124865294c1fSJeff Layton 	seq_printf(m, "total entries: %u\n", count);
1249ce502f81SChuck Lever 	seq_printf(m, "hash buckets:  %u\n", buckets);
12500fd244c1SChuck Lever 	seq_printf(m, "lru entries:   %lu\n", lru);
125165294c1fSJeff Layton 	seq_printf(m, "cache hits:    %lu\n", hits);
125229d4bdbbSChuck Lever 	seq_printf(m, "acquisitions:  %lu\n", acquisitions);
1253d6329327SChuck Lever 	seq_printf(m, "releases:      %lu\n", releases);
125494660cc1SChuck Lever 	seq_printf(m, "evictions:     %lu\n", evictions);
1255904940e9SChuck Lever 	if (releases)
1256904940e9SChuck Lever 		seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1257904940e9SChuck Lever 	else
1258904940e9SChuck Lever 		seq_printf(m, "mean age (ms): -\n");
1259df2aff52SChuck Lever 	seq_printf(m, "pages flushed: %lu\n", pages_flushed);
126065294c1fSJeff Layton 	return 0;
126165294c1fSJeff Layton }
1262