xref: /openbmc/linux/fs/nfsd/filecache.c (revision c7b824c3d06c85e054caf86e227255112c5e3c38)
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>
1665294c1fSJeff Layton 
1765294c1fSJeff Layton #include "vfs.h"
1865294c1fSJeff Layton #include "nfsd.h"
1965294c1fSJeff Layton #include "nfsfh.h"
205e113224STrond Myklebust #include "netns.h"
2165294c1fSJeff Layton #include "filecache.h"
2265294c1fSJeff Layton #include "trace.h"
2365294c1fSJeff Layton 
2465294c1fSJeff Layton #define NFSDDBG_FACILITY	NFSDDBG_FH
2565294c1fSJeff Layton 
2665294c1fSJeff Layton /* FIXME: dynamically size this for the machine somehow? */
2765294c1fSJeff Layton #define NFSD_FILE_HASH_BITS                   12
2865294c1fSJeff Layton #define NFSD_FILE_HASH_SIZE                  (1 << NFSD_FILE_HASH_BITS)
2965294c1fSJeff Layton #define NFSD_LAUNDRETTE_DELAY		     (2 * HZ)
3065294c1fSJeff Layton 
31*c7b824c3SChuck Lever #define NFSD_FILE_CACHE_UP		     (0)
3265294c1fSJeff Layton 
3365294c1fSJeff Layton /* We only care about NFSD_MAY_READ/WRITE for this cache */
3465294c1fSJeff Layton #define NFSD_FILE_MAY_MASK	(NFSD_MAY_READ|NFSD_MAY_WRITE)
3565294c1fSJeff Layton 
3665294c1fSJeff Layton struct nfsd_fcache_bucket {
3765294c1fSJeff Layton 	struct hlist_head	nfb_head;
3865294c1fSJeff Layton 	spinlock_t		nfb_lock;
3965294c1fSJeff Layton 	unsigned int		nfb_count;
4065294c1fSJeff Layton 	unsigned int		nfb_maxcount;
4165294c1fSJeff Layton };
4265294c1fSJeff Layton 
4365294c1fSJeff Layton static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
4429d4bdbbSChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions);
45d6329327SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_releases);
46904940e9SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age);
47df2aff52SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_pages_flushed);
4894660cc1SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions);
4965294c1fSJeff Layton 
509542e6a6STrond Myklebust struct nfsd_fcache_disposal {
519542e6a6STrond Myklebust 	struct work_struct work;
529542e6a6STrond Myklebust 	spinlock_t lock;
539542e6a6STrond Myklebust 	struct list_head freeme;
549542e6a6STrond Myklebust };
559542e6a6STrond Myklebust 
5650d0def9SChen Zhou static struct workqueue_struct *nfsd_filecache_wq __read_mostly;
579542e6a6STrond Myklebust 
5865294c1fSJeff Layton static struct kmem_cache		*nfsd_file_slab;
5965294c1fSJeff Layton static struct kmem_cache		*nfsd_file_mark_slab;
6065294c1fSJeff Layton static struct nfsd_fcache_bucket	*nfsd_file_hashtbl;
6165294c1fSJeff Layton static struct list_lru			nfsd_file_lru;
62*c7b824c3SChuck Lever static unsigned long			nfsd_file_flags;
6365294c1fSJeff Layton static struct fsnotify_group		*nfsd_file_fsnotify_group;
6465294c1fSJeff Layton static atomic_long_t			nfsd_filecache_count;
6565294c1fSJeff Layton static struct delayed_work		nfsd_filecache_laundrette;
6665294c1fSJeff Layton 
6765294c1fSJeff Layton static void
689542e6a6STrond Myklebust nfsd_file_schedule_laundrette(void)
6965294c1fSJeff Layton {
70*c7b824c3SChuck Lever 	if ((atomic_long_read(&nfsd_filecache_count) == 0) ||
71*c7b824c3SChuck Lever 	    test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
7265294c1fSJeff Layton 		return;
7365294c1fSJeff Layton 
749542e6a6STrond Myklebust 	queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
759542e6a6STrond Myklebust 			NFSD_LAUNDRETTE_DELAY);
7665294c1fSJeff Layton }
7765294c1fSJeff Layton 
7865294c1fSJeff Layton static void
7965294c1fSJeff Layton nfsd_file_slab_free(struct rcu_head *rcu)
8065294c1fSJeff Layton {
8165294c1fSJeff Layton 	struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
8265294c1fSJeff Layton 
8365294c1fSJeff Layton 	put_cred(nf->nf_cred);
8465294c1fSJeff Layton 	kmem_cache_free(nfsd_file_slab, nf);
8565294c1fSJeff Layton }
8665294c1fSJeff Layton 
8765294c1fSJeff Layton static void
8865294c1fSJeff Layton nfsd_file_mark_free(struct fsnotify_mark *mark)
8965294c1fSJeff Layton {
9065294c1fSJeff Layton 	struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
9165294c1fSJeff Layton 						  nfm_mark);
9265294c1fSJeff Layton 
9365294c1fSJeff Layton 	kmem_cache_free(nfsd_file_mark_slab, nfm);
9465294c1fSJeff Layton }
9565294c1fSJeff Layton 
9665294c1fSJeff Layton static struct nfsd_file_mark *
9765294c1fSJeff Layton nfsd_file_mark_get(struct nfsd_file_mark *nfm)
9865294c1fSJeff Layton {
99689827cdSTrond Myklebust 	if (!refcount_inc_not_zero(&nfm->nfm_ref))
10065294c1fSJeff Layton 		return NULL;
10165294c1fSJeff Layton 	return nfm;
10265294c1fSJeff Layton }
10365294c1fSJeff Layton 
10465294c1fSJeff Layton static void
10565294c1fSJeff Layton nfsd_file_mark_put(struct nfsd_file_mark *nfm)
10665294c1fSJeff Layton {
107689827cdSTrond Myklebust 	if (refcount_dec_and_test(&nfm->nfm_ref)) {
10865294c1fSJeff Layton 		fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
10965294c1fSJeff Layton 		fsnotify_put_mark(&nfm->nfm_mark);
11065294c1fSJeff Layton 	}
11165294c1fSJeff Layton }
11265294c1fSJeff Layton 
11365294c1fSJeff Layton static struct nfsd_file_mark *
11465294c1fSJeff Layton nfsd_file_mark_find_or_create(struct nfsd_file *nf)
11565294c1fSJeff Layton {
11665294c1fSJeff Layton 	int			err;
11765294c1fSJeff Layton 	struct fsnotify_mark	*mark;
11865294c1fSJeff Layton 	struct nfsd_file_mark	*nfm = NULL, *new;
11965294c1fSJeff Layton 	struct inode *inode = nf->nf_inode;
12065294c1fSJeff Layton 
12165294c1fSJeff Layton 	do {
122b8962a9dSAmir Goldstein 		fsnotify_group_lock(nfsd_file_fsnotify_group);
12365294c1fSJeff Layton 		mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
12465294c1fSJeff Layton 					  nfsd_file_fsnotify_group);
12565294c1fSJeff Layton 		if (mark) {
12665294c1fSJeff Layton 			nfm = nfsd_file_mark_get(container_of(mark,
12765294c1fSJeff Layton 						 struct nfsd_file_mark,
12865294c1fSJeff Layton 						 nfm_mark));
129b8962a9dSAmir Goldstein 			fsnotify_group_unlock(nfsd_file_fsnotify_group);
13090d2f1daSTrond Myklebust 			if (nfm) {
13165294c1fSJeff Layton 				fsnotify_put_mark(mark);
13265294c1fSJeff Layton 				break;
13390d2f1daSTrond Myklebust 			}
13490d2f1daSTrond Myklebust 			/* Avoid soft lockup race with nfsd_file_mark_put() */
13590d2f1daSTrond Myklebust 			fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
13690d2f1daSTrond Myklebust 			fsnotify_put_mark(mark);
137b8962a9dSAmir Goldstein 		} else {
138b8962a9dSAmir Goldstein 			fsnotify_group_unlock(nfsd_file_fsnotify_group);
139b8962a9dSAmir Goldstein 		}
14065294c1fSJeff Layton 
14165294c1fSJeff Layton 		/* allocate a new nfm */
14265294c1fSJeff Layton 		new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
14365294c1fSJeff Layton 		if (!new)
14465294c1fSJeff Layton 			return NULL;
14565294c1fSJeff Layton 		fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
14665294c1fSJeff Layton 		new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
147689827cdSTrond Myklebust 		refcount_set(&new->nfm_ref, 1);
14865294c1fSJeff Layton 
14965294c1fSJeff Layton 		err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
15065294c1fSJeff Layton 
15165294c1fSJeff Layton 		/*
15265294c1fSJeff Layton 		 * If the add was successful, then return the object.
15365294c1fSJeff Layton 		 * Otherwise, we need to put the reference we hold on the
15465294c1fSJeff Layton 		 * nfm_mark. The fsnotify code will take a reference and put
15565294c1fSJeff Layton 		 * it on failure, so we can't just free it directly. It's also
15665294c1fSJeff Layton 		 * not safe to call fsnotify_destroy_mark on it as the
15765294c1fSJeff Layton 		 * mark->group will be NULL. Thus, we can't let the nfm_ref
15865294c1fSJeff Layton 		 * counter drive the destruction at this point.
15965294c1fSJeff Layton 		 */
16065294c1fSJeff Layton 		if (likely(!err))
16165294c1fSJeff Layton 			nfm = new;
16265294c1fSJeff Layton 		else
16365294c1fSJeff Layton 			fsnotify_put_mark(&new->nfm_mark);
16465294c1fSJeff Layton 	} while (unlikely(err == -EEXIST));
16565294c1fSJeff Layton 
16665294c1fSJeff Layton 	return nfm;
16765294c1fSJeff Layton }
16865294c1fSJeff Layton 
16965294c1fSJeff Layton static struct nfsd_file *
170f0743c2bSChuck Lever nfsd_file_alloc(struct inode *inode, unsigned int may, struct net *net)
17165294c1fSJeff Layton {
17265294c1fSJeff Layton 	struct nfsd_file *nf;
17365294c1fSJeff Layton 
17465294c1fSJeff Layton 	nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
17565294c1fSJeff Layton 	if (nf) {
17665294c1fSJeff Layton 		INIT_HLIST_NODE(&nf->nf_node);
17765294c1fSJeff Layton 		INIT_LIST_HEAD(&nf->nf_lru);
178904940e9SChuck Lever 		nf->nf_birthtime = ktime_get();
17965294c1fSJeff Layton 		nf->nf_file = NULL;
18065294c1fSJeff Layton 		nf->nf_cred = get_current_cred();
1815e113224STrond Myklebust 		nf->nf_net = net;
18265294c1fSJeff Layton 		nf->nf_flags = 0;
18365294c1fSJeff Layton 		nf->nf_inode = inode;
184689827cdSTrond Myklebust 		refcount_set(&nf->nf_ref, 1);
18565294c1fSJeff Layton 		nf->nf_may = may & NFSD_FILE_MAY_MASK;
18665294c1fSJeff Layton 		nf->nf_mark = NULL;
18765294c1fSJeff Layton 		trace_nfsd_file_alloc(nf);
18865294c1fSJeff Layton 	}
18965294c1fSJeff Layton 	return nf;
19065294c1fSJeff Layton }
19165294c1fSJeff Layton 
19265294c1fSJeff Layton static bool
19365294c1fSJeff Layton nfsd_file_free(struct nfsd_file *nf)
19465294c1fSJeff Layton {
195904940e9SChuck Lever 	s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime));
19665294c1fSJeff Layton 	bool flush = false;
19765294c1fSJeff Layton 
198d6329327SChuck Lever 	this_cpu_inc(nfsd_file_releases);
199904940e9SChuck Lever 	this_cpu_add(nfsd_file_total_age, age);
200d6329327SChuck Lever 
20165294c1fSJeff Layton 	trace_nfsd_file_put_final(nf);
20265294c1fSJeff Layton 	if (nf->nf_mark)
20365294c1fSJeff Layton 		nfsd_file_mark_put(nf->nf_mark);
20465294c1fSJeff Layton 	if (nf->nf_file) {
20565294c1fSJeff Layton 		get_file(nf->nf_file);
20665294c1fSJeff Layton 		filp_close(nf->nf_file, NULL);
20765294c1fSJeff Layton 		fput(nf->nf_file);
20865294c1fSJeff Layton 		flush = true;
20965294c1fSJeff Layton 	}
210668ed92eSChuck Lever 
211668ed92eSChuck Lever 	/*
212668ed92eSChuck Lever 	 * If this item is still linked via nf_lru, that's a bug.
213668ed92eSChuck Lever 	 * WARN and leak it to preserve system stability.
214668ed92eSChuck Lever 	 */
215668ed92eSChuck Lever 	if (WARN_ON_ONCE(!list_empty(&nf->nf_lru)))
216668ed92eSChuck Lever 		return flush;
217668ed92eSChuck Lever 
21865294c1fSJeff Layton 	call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
21965294c1fSJeff Layton 	return flush;
22065294c1fSJeff Layton }
22165294c1fSJeff Layton 
222055b24a8STrond Myklebust static bool
223055b24a8STrond Myklebust nfsd_file_check_writeback(struct nfsd_file *nf)
224055b24a8STrond Myklebust {
225055b24a8STrond Myklebust 	struct file *file = nf->nf_file;
226055b24a8STrond Myklebust 	struct address_space *mapping;
227055b24a8STrond Myklebust 
228055b24a8STrond Myklebust 	if (!file || !(file->f_mode & FMODE_WRITE))
229055b24a8STrond Myklebust 		return false;
230055b24a8STrond Myklebust 	mapping = file->f_mapping;
231055b24a8STrond Myklebust 	return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
232055b24a8STrond Myklebust 		mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
233055b24a8STrond Myklebust }
234055b24a8STrond Myklebust 
235055b24a8STrond Myklebust static int
236055b24a8STrond Myklebust nfsd_file_check_write_error(struct nfsd_file *nf)
237055b24a8STrond Myklebust {
238055b24a8STrond Myklebust 	struct file *file = nf->nf_file;
239055b24a8STrond Myklebust 
240055b24a8STrond Myklebust 	if (!file || !(file->f_mode & FMODE_WRITE))
241055b24a8STrond Myklebust 		return 0;
242055b24a8STrond Myklebust 	return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
243055b24a8STrond Myklebust }
244055b24a8STrond Myklebust 
24565294c1fSJeff Layton static void
2466b8a9433STrond Myklebust nfsd_file_flush(struct nfsd_file *nf)
2476b8a9433STrond Myklebust {
248df2aff52SChuck Lever 	struct file *file = nf->nf_file;
249df2aff52SChuck Lever 
250df2aff52SChuck Lever 	if (!file || !(file->f_mode & FMODE_WRITE))
251df2aff52SChuck Lever 		return;
252df2aff52SChuck Lever 	this_cpu_add(nfsd_file_pages_flushed, file->f_mapping->nrpages);
253df2aff52SChuck Lever 	if (vfs_fsync(file, 1) != 0)
2546b8a9433STrond Myklebust 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
2556b8a9433STrond Myklebust }
2566b8a9433STrond Myklebust 
257c46203acSChuck Lever static void nfsd_file_lru_add(struct nfsd_file *nf)
258c46203acSChuck Lever {
2594a0e73e6SChuck Lever 	set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
260c46203acSChuck Lever 	if (list_lru_add(&nfsd_file_lru, &nf->nf_lru))
261c46203acSChuck Lever 		trace_nfsd_file_lru_add(nf);
262c46203acSChuck Lever }
263c46203acSChuck Lever 
264c46203acSChuck Lever static void nfsd_file_lru_remove(struct nfsd_file *nf)
265c46203acSChuck Lever {
266c46203acSChuck Lever 	if (list_lru_del(&nfsd_file_lru, &nf->nf_lru))
267c46203acSChuck Lever 		trace_nfsd_file_lru_del(nf);
268c46203acSChuck Lever }
269c46203acSChuck Lever 
2706b8a9433STrond Myklebust static void
27165294c1fSJeff Layton nfsd_file_do_unhash(struct nfsd_file *nf)
27265294c1fSJeff Layton {
27387553263SChuck Lever 	struct inode *inode = nf->nf_inode;
27487553263SChuck Lever 	unsigned int hashval = (unsigned int)hash_long(inode->i_ino,
27587553263SChuck Lever 				NFSD_FILE_HASH_BITS);
27687553263SChuck Lever 
27787553263SChuck Lever 	lockdep_assert_held(&nfsd_file_hashtbl[hashval].nfb_lock);
27865294c1fSJeff Layton 
27965294c1fSJeff Layton 	trace_nfsd_file_unhash(nf);
28065294c1fSJeff Layton 
281055b24a8STrond Myklebust 	if (nfsd_file_check_write_error(nf))
2823988a578SChuck Lever 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
28387553263SChuck Lever 	--nfsd_file_hashtbl[hashval].nfb_count;
28465294c1fSJeff Layton 	hlist_del_rcu(&nf->nf_node);
28565294c1fSJeff Layton 	atomic_long_dec(&nfsd_filecache_count);
28665294c1fSJeff Layton }
28765294c1fSJeff Layton 
288cb7ec76eSChuck Lever static void
289cb7ec76eSChuck Lever nfsd_file_hash_remove(struct nfsd_file *nf)
290cb7ec76eSChuck Lever {
291cb7ec76eSChuck Lever 	struct inode *inode = nf->nf_inode;
292cb7ec76eSChuck Lever 	unsigned int hashval = (unsigned int)hash_long(inode->i_ino,
293cb7ec76eSChuck Lever 				NFSD_FILE_HASH_BITS);
294cb7ec76eSChuck Lever 
295cb7ec76eSChuck Lever 	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
296cb7ec76eSChuck Lever 	nfsd_file_do_unhash(nf);
297cb7ec76eSChuck Lever 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
298cb7ec76eSChuck Lever }
299cb7ec76eSChuck Lever 
30065294c1fSJeff Layton static bool
30165294c1fSJeff Layton nfsd_file_unhash(struct nfsd_file *nf)
30265294c1fSJeff Layton {
30365294c1fSJeff Layton 	if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
30465294c1fSJeff Layton 		nfsd_file_do_unhash(nf);
30565294c1fSJeff Layton 		return true;
30665294c1fSJeff Layton 	}
30765294c1fSJeff Layton 	return false;
30865294c1fSJeff Layton }
30965294c1fSJeff Layton 
31065294c1fSJeff Layton /*
31165294c1fSJeff Layton  * Return true if the file was unhashed.
31265294c1fSJeff Layton  */
31365294c1fSJeff Layton static bool
31465294c1fSJeff Layton nfsd_file_unhash_and_release_locked(struct nfsd_file *nf, struct list_head *dispose)
31565294c1fSJeff Layton {
31665294c1fSJeff Layton 	trace_nfsd_file_unhash_and_release_locked(nf);
31765294c1fSJeff Layton 	if (!nfsd_file_unhash(nf))
31865294c1fSJeff Layton 		return false;
31965294c1fSJeff Layton 	/* keep final reference for nfsd_file_lru_dispose */
320689827cdSTrond Myklebust 	if (refcount_dec_not_one(&nf->nf_ref))
32165294c1fSJeff Layton 		return true;
32265294c1fSJeff Layton 
3234a0e73e6SChuck Lever 	nfsd_file_lru_remove(nf);
32465294c1fSJeff Layton 	list_add(&nf->nf_lru, dispose);
32565294c1fSJeff Layton 	return true;
32665294c1fSJeff Layton }
32765294c1fSJeff Layton 
328b6669305STrond Myklebust static void
32965294c1fSJeff Layton nfsd_file_put_noref(struct nfsd_file *nf)
33065294c1fSJeff Layton {
33165294c1fSJeff Layton 	trace_nfsd_file_put(nf);
33265294c1fSJeff Layton 
333689827cdSTrond Myklebust 	if (refcount_dec_and_test(&nf->nf_ref)) {
33465294c1fSJeff Layton 		WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
3354a0e73e6SChuck Lever 		nfsd_file_lru_remove(nf);
33665294c1fSJeff Layton 		nfsd_file_free(nf);
33765294c1fSJeff Layton 	}
33865294c1fSJeff Layton }
33965294c1fSJeff Layton 
34065294c1fSJeff Layton void
34165294c1fSJeff Layton nfsd_file_put(struct nfsd_file *nf)
34265294c1fSJeff Layton {
34308af54b3SChuck Lever 	might_sleep();
34408af54b3SChuck Lever 
3454a0e73e6SChuck Lever 	nfsd_file_lru_add(nf);
34699939792STrond Myklebust 	if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) {
3476b8a9433STrond Myklebust 		nfsd_file_flush(nf);
348b6669305STrond Myklebust 		nfsd_file_put_noref(nf);
349b6c71c66SChuck Lever 	} else if (nf->nf_file) {
350b6669305STrond Myklebust 		nfsd_file_put_noref(nf);
3519542e6a6STrond Myklebust 		nfsd_file_schedule_laundrette();
352b6c71c66SChuck Lever 	} else
353b6c71c66SChuck Lever 		nfsd_file_put_noref(nf);
35465294c1fSJeff Layton }
35565294c1fSJeff Layton 
35665294c1fSJeff Layton struct nfsd_file *
35765294c1fSJeff Layton nfsd_file_get(struct nfsd_file *nf)
35865294c1fSJeff Layton {
359689827cdSTrond Myklebust 	if (likely(refcount_inc_not_zero(&nf->nf_ref)))
36065294c1fSJeff Layton 		return nf;
36165294c1fSJeff Layton 	return NULL;
36265294c1fSJeff Layton }
36365294c1fSJeff Layton 
36465294c1fSJeff Layton static void
36565294c1fSJeff Layton nfsd_file_dispose_list(struct list_head *dispose)
36665294c1fSJeff Layton {
36765294c1fSJeff Layton 	struct nfsd_file *nf;
36865294c1fSJeff Layton 
36965294c1fSJeff Layton 	while(!list_empty(dispose)) {
37065294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
371668ed92eSChuck Lever 		list_del_init(&nf->nf_lru);
3726b8a9433STrond Myklebust 		nfsd_file_flush(nf);
37365294c1fSJeff Layton 		nfsd_file_put_noref(nf);
37465294c1fSJeff Layton 	}
37565294c1fSJeff Layton }
37665294c1fSJeff Layton 
37765294c1fSJeff Layton static void
37865294c1fSJeff Layton nfsd_file_dispose_list_sync(struct list_head *dispose)
37965294c1fSJeff Layton {
38065294c1fSJeff Layton 	bool flush = false;
38165294c1fSJeff Layton 	struct nfsd_file *nf;
38265294c1fSJeff Layton 
38365294c1fSJeff Layton 	while(!list_empty(dispose)) {
38465294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
385668ed92eSChuck Lever 		list_del_init(&nf->nf_lru);
3866b8a9433STrond Myklebust 		nfsd_file_flush(nf);
387689827cdSTrond Myklebust 		if (!refcount_dec_and_test(&nf->nf_ref))
38865294c1fSJeff Layton 			continue;
38965294c1fSJeff Layton 		if (nfsd_file_free(nf))
39065294c1fSJeff Layton 			flush = true;
39165294c1fSJeff Layton 	}
39265294c1fSJeff Layton 	if (flush)
39365294c1fSJeff Layton 		flush_delayed_fput();
39465294c1fSJeff Layton }
39565294c1fSJeff Layton 
3969542e6a6STrond Myklebust static void
3979542e6a6STrond Myklebust nfsd_file_list_remove_disposal(struct list_head *dst,
3989542e6a6STrond Myklebust 		struct nfsd_fcache_disposal *l)
3999542e6a6STrond Myklebust {
4009542e6a6STrond Myklebust 	spin_lock(&l->lock);
4019542e6a6STrond Myklebust 	list_splice_init(&l->freeme, dst);
4029542e6a6STrond Myklebust 	spin_unlock(&l->lock);
4039542e6a6STrond Myklebust }
4049542e6a6STrond Myklebust 
4059542e6a6STrond Myklebust static void
4069542e6a6STrond Myklebust nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
4079542e6a6STrond Myklebust {
4081463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4091463b38eSNeilBrown 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
4109542e6a6STrond Myklebust 
4119542e6a6STrond Myklebust 	spin_lock(&l->lock);
4129542e6a6STrond Myklebust 	list_splice_tail_init(files, &l->freeme);
4139542e6a6STrond Myklebust 	spin_unlock(&l->lock);
4149542e6a6STrond Myklebust 	queue_work(nfsd_filecache_wq, &l->work);
4159542e6a6STrond Myklebust }
4169542e6a6STrond Myklebust 
4179542e6a6STrond Myklebust static void
4189542e6a6STrond Myklebust nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
4199542e6a6STrond Myklebust 		struct net *net)
4209542e6a6STrond Myklebust {
4219542e6a6STrond Myklebust 	struct nfsd_file *nf, *tmp;
4229542e6a6STrond Myklebust 
4239542e6a6STrond Myklebust 	list_for_each_entry_safe(nf, tmp, src, nf_lru) {
4249542e6a6STrond Myklebust 		if (nf->nf_net == net)
4259542e6a6STrond Myklebust 			list_move_tail(&nf->nf_lru, dst);
4269542e6a6STrond Myklebust 	}
4279542e6a6STrond Myklebust }
4289542e6a6STrond Myklebust 
4299542e6a6STrond Myklebust static void
4309542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(struct list_head *dispose)
4319542e6a6STrond Myklebust {
4329542e6a6STrond Myklebust 	LIST_HEAD(list);
4339542e6a6STrond Myklebust 	struct nfsd_file *nf;
4349542e6a6STrond Myklebust 
4359542e6a6STrond Myklebust 	while(!list_empty(dispose)) {
4369542e6a6STrond Myklebust 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
4379542e6a6STrond Myklebust 		nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
4389542e6a6STrond Myklebust 		nfsd_file_list_add_disposal(&list, nf->nf_net);
4399542e6a6STrond Myklebust 	}
4409542e6a6STrond Myklebust }
4419542e6a6STrond Myklebust 
4424a0e73e6SChuck Lever /**
4434a0e73e6SChuck Lever  * nfsd_file_lru_cb - Examine an entry on the LRU list
4444a0e73e6SChuck Lever  * @item: LRU entry to examine
4454a0e73e6SChuck Lever  * @lru: controlling LRU
4464a0e73e6SChuck Lever  * @lock: LRU list lock (unused)
4474a0e73e6SChuck Lever  * @arg: dispose list
4484a0e73e6SChuck Lever  *
44965294c1fSJeff Layton  * Note this can deadlock with nfsd_file_cache_purge.
4504a0e73e6SChuck Lever  *
4514a0e73e6SChuck Lever  * Return values:
4524a0e73e6SChuck Lever  *   %LRU_REMOVED: @item was removed from the LRU
453edead3a5SChuck Lever  *   %LRU_ROTATE: @item is to be moved to the LRU tail
4544a0e73e6SChuck Lever  *   %LRU_SKIP: @item cannot be evicted
45565294c1fSJeff Layton  */
45665294c1fSJeff Layton static enum lru_status
45765294c1fSJeff Layton nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
45865294c1fSJeff Layton 		 spinlock_t *lock, void *arg)
45965294c1fSJeff Layton 	__releases(lock)
46065294c1fSJeff Layton 	__acquires(lock)
46165294c1fSJeff Layton {
46265294c1fSJeff Layton 	struct list_head *head = arg;
46365294c1fSJeff Layton 	struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
46465294c1fSJeff Layton 
46565294c1fSJeff Layton 	/*
46665294c1fSJeff Layton 	 * Do a lockless refcount check. The hashtable holds one reference, so
46765294c1fSJeff Layton 	 * we look to see if anything else has a reference, or if any have
46865294c1fSJeff Layton 	 * been put since the shrinker last ran. Those don't get unhashed and
46965294c1fSJeff Layton 	 * released.
47065294c1fSJeff Layton 	 *
47165294c1fSJeff Layton 	 * Note that in the put path, we set the flag and then decrement the
47265294c1fSJeff Layton 	 * counter. Here we check the counter and then test and clear the flag.
47365294c1fSJeff Layton 	 * That order is deliberate to ensure that we can do this locklessly.
47465294c1fSJeff Layton 	 */
475c46203acSChuck Lever 	if (refcount_read(&nf->nf_ref) > 1) {
4764a0e73e6SChuck Lever 		list_lru_isolate(lru, &nf->nf_lru);
477c46203acSChuck Lever 		trace_nfsd_file_gc_in_use(nf);
4784a0e73e6SChuck Lever 		return LRU_REMOVED;
479c46203acSChuck Lever 	}
480055b24a8STrond Myklebust 
481055b24a8STrond Myklebust 	/*
482055b24a8STrond Myklebust 	 * Don't throw out files that are still undergoing I/O or
483055b24a8STrond Myklebust 	 * that have uncleared errors pending.
484055b24a8STrond Myklebust 	 */
485c46203acSChuck Lever 	if (nfsd_file_check_writeback(nf)) {
486c46203acSChuck Lever 		trace_nfsd_file_gc_writeback(nf);
487c46203acSChuck Lever 		return LRU_SKIP;
488c46203acSChuck Lever 	}
489055b24a8STrond Myklebust 
490c46203acSChuck Lever 	if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
491c46203acSChuck Lever 		trace_nfsd_file_gc_referenced(nf);
492edead3a5SChuck Lever 		return LRU_ROTATE;
493c46203acSChuck Lever 	}
49465294c1fSJeff Layton 
495c46203acSChuck Lever 	if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
496c46203acSChuck Lever 		trace_nfsd_file_gc_hashed(nf);
497c46203acSChuck Lever 		return LRU_SKIP;
498c46203acSChuck Lever 	}
49965294c1fSJeff Layton 
50065294c1fSJeff Layton 	list_lru_isolate_move(lru, &nf->nf_lru, head);
50194660cc1SChuck Lever 	this_cpu_inc(nfsd_file_evictions);
502c46203acSChuck Lever 	trace_nfsd_file_gc_disposed(nf);
50365294c1fSJeff Layton 	return LRU_REMOVED;
50465294c1fSJeff Layton }
50565294c1fSJeff Layton 
5060bac5a26SChuck Lever /*
5070bac5a26SChuck Lever  * Unhash items on @dispose immediately, then queue them on the
5080bac5a26SChuck Lever  * disposal workqueue to finish releasing them in the background.
5090bac5a26SChuck Lever  *
5100bac5a26SChuck Lever  * cel: Note that between the time list_lru_shrink_walk runs and
5110bac5a26SChuck Lever  * now, these items are in the hash table but marked unhashed.
5120bac5a26SChuck Lever  * Why release these outside of lru_cb ? There's no lock ordering
5130bac5a26SChuck Lever  * problem since lru_cb currently takes no lock.
5140bac5a26SChuck Lever  */
5150bac5a26SChuck Lever static void nfsd_file_gc_dispose_list(struct list_head *dispose)
5160bac5a26SChuck Lever {
5170bac5a26SChuck Lever 	struct nfsd_file *nf;
5180bac5a26SChuck Lever 
519cb7ec76eSChuck Lever 	list_for_each_entry(nf, dispose, nf_lru)
520cb7ec76eSChuck Lever 		nfsd_file_hash_remove(nf);
5210bac5a26SChuck Lever 	nfsd_file_dispose_list_delayed(dispose);
5220bac5a26SChuck Lever }
5230bac5a26SChuck Lever 
5249542e6a6STrond Myklebust static void
5259542e6a6STrond Myklebust nfsd_file_gc(void)
5269542e6a6STrond Myklebust {
5273bc6d347SChuck Lever 	LIST_HEAD(dispose);
52894660cc1SChuck Lever 	unsigned long ret;
5293bc6d347SChuck Lever 
53094660cc1SChuck Lever 	ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
531edead3a5SChuck Lever 			    &dispose, list_lru_count(&nfsd_file_lru));
53294660cc1SChuck Lever 	trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
5333bc6d347SChuck Lever 	nfsd_file_gc_dispose_list(&dispose);
5349542e6a6STrond Myklebust }
5359542e6a6STrond Myklebust 
5369542e6a6STrond Myklebust static void
5379542e6a6STrond Myklebust nfsd_file_gc_worker(struct work_struct *work)
5389542e6a6STrond Myklebust {
5399542e6a6STrond Myklebust 	nfsd_file_gc();
5409542e6a6STrond Myklebust 	nfsd_file_schedule_laundrette();
54165294c1fSJeff Layton }
54265294c1fSJeff Layton 
54365294c1fSJeff Layton static unsigned long
54465294c1fSJeff Layton nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
54565294c1fSJeff Layton {
54665294c1fSJeff Layton 	return list_lru_count(&nfsd_file_lru);
54765294c1fSJeff Layton }
54865294c1fSJeff Layton 
54965294c1fSJeff Layton static unsigned long
55065294c1fSJeff Layton nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
55165294c1fSJeff Layton {
55239f1d1ffSChuck Lever 	LIST_HEAD(dispose);
55339f1d1ffSChuck Lever 	unsigned long ret;
55439f1d1ffSChuck Lever 
55539f1d1ffSChuck Lever 	ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
55639f1d1ffSChuck Lever 				   nfsd_file_lru_cb, &dispose);
55794660cc1SChuck Lever 	trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
55839f1d1ffSChuck Lever 	nfsd_file_gc_dispose_list(&dispose);
55939f1d1ffSChuck Lever 	return ret;
56065294c1fSJeff Layton }
56165294c1fSJeff Layton 
56265294c1fSJeff Layton static struct shrinker	nfsd_file_shrinker = {
56365294c1fSJeff Layton 	.scan_objects = nfsd_file_lru_scan,
56465294c1fSJeff Layton 	.count_objects = nfsd_file_lru_count,
56565294c1fSJeff Layton 	.seeks = 1,
56665294c1fSJeff Layton };
56765294c1fSJeff Layton 
568a8455110SChuck Lever /*
569a8455110SChuck Lever  * Find all cache items across all net namespaces that match @inode and
570a8455110SChuck Lever  * move them to @dispose. The lookup is atomic wrt nfsd_file_acquire().
571a8455110SChuck Lever  */
572a8455110SChuck Lever static unsigned int
573a8455110SChuck Lever __nfsd_file_close_inode(struct inode *inode, struct list_head *dispose)
57465294c1fSJeff Layton {
575a8455110SChuck Lever 	unsigned int		hashval = (unsigned int)hash_long(inode->i_ino,
576a8455110SChuck Lever 						NFSD_FILE_HASH_BITS);
577a8455110SChuck Lever 	unsigned int		count = 0;
57865294c1fSJeff Layton 	struct nfsd_file	*nf;
57965294c1fSJeff Layton 	struct hlist_node	*tmp;
58065294c1fSJeff Layton 
58165294c1fSJeff Layton 	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
58265294c1fSJeff Layton 	hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) {
583a8455110SChuck Lever 		if (inode == nf->nf_inode) {
58465294c1fSJeff Layton 			nfsd_file_unhash_and_release_locked(nf, dispose);
585a8455110SChuck Lever 			count++;
586a8455110SChuck Lever 		}
58765294c1fSJeff Layton 	}
58865294c1fSJeff Layton 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
589a8455110SChuck Lever 	return count;
59065294c1fSJeff Layton }
59165294c1fSJeff Layton 
59265294c1fSJeff Layton /**
59365294c1fSJeff Layton  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
59465294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
59565294c1fSJeff Layton  *
596a8455110SChuck Lever  * Unhash and put, then flush and fput all cache items associated with @inode.
59765294c1fSJeff Layton  */
59865294c1fSJeff Layton void
59965294c1fSJeff Layton nfsd_file_close_inode_sync(struct inode *inode)
60065294c1fSJeff Layton {
60165294c1fSJeff Layton 	LIST_HEAD(dispose);
602a8455110SChuck Lever 	unsigned int count;
60365294c1fSJeff Layton 
604a8455110SChuck Lever 	count = __nfsd_file_close_inode(inode, &dispose);
605a8455110SChuck Lever 	trace_nfsd_file_close_inode_sync(inode, count);
60665294c1fSJeff Layton 	nfsd_file_dispose_list_sync(&dispose);
60765294c1fSJeff Layton }
60865294c1fSJeff Layton 
60965294c1fSJeff Layton /**
61019598141STrond Myklebust  * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
61165294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
61265294c1fSJeff Layton  *
613a8455110SChuck Lever  * Unhash and put all cache item associated with @inode.
61465294c1fSJeff Layton  */
61565294c1fSJeff Layton static void
61665294c1fSJeff Layton nfsd_file_close_inode(struct inode *inode)
61765294c1fSJeff Layton {
61865294c1fSJeff Layton 	LIST_HEAD(dispose);
619a8455110SChuck Lever 	unsigned int count;
62065294c1fSJeff Layton 
621a8455110SChuck Lever 	count = __nfsd_file_close_inode(inode, &dispose);
622a8455110SChuck Lever 	trace_nfsd_file_close_inode(inode, count);
6239542e6a6STrond Myklebust 	nfsd_file_dispose_list_delayed(&dispose);
62465294c1fSJeff Layton }
62565294c1fSJeff Layton 
62665294c1fSJeff Layton /**
62765294c1fSJeff Layton  * nfsd_file_delayed_close - close unused nfsd_files
62865294c1fSJeff Layton  * @work: dummy
62965294c1fSJeff Layton  *
63065294c1fSJeff Layton  * Walk the LRU list and close any entries that have not been used since
63165294c1fSJeff Layton  * the last scan.
63265294c1fSJeff Layton  *
63365294c1fSJeff Layton  * Note this can deadlock with nfsd_file_cache_purge.
63465294c1fSJeff Layton  */
63565294c1fSJeff Layton static void
63665294c1fSJeff Layton nfsd_file_delayed_close(struct work_struct *work)
63765294c1fSJeff Layton {
63865294c1fSJeff Layton 	LIST_HEAD(head);
6399542e6a6STrond Myklebust 	struct nfsd_fcache_disposal *l = container_of(work,
6409542e6a6STrond Myklebust 			struct nfsd_fcache_disposal, work);
64165294c1fSJeff Layton 
6429542e6a6STrond Myklebust 	nfsd_file_list_remove_disposal(&head, l);
6439542e6a6STrond Myklebust 	nfsd_file_dispose_list(&head);
64465294c1fSJeff Layton }
64565294c1fSJeff Layton 
64665294c1fSJeff Layton static int
64765294c1fSJeff Layton nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
64865294c1fSJeff Layton 			    void *data)
64965294c1fSJeff Layton {
65065294c1fSJeff Layton 	struct file_lock *fl = data;
65165294c1fSJeff Layton 
65265294c1fSJeff Layton 	/* Only close files for F_SETLEASE leases */
65365294c1fSJeff Layton 	if (fl->fl_flags & FL_LEASE)
65465294c1fSJeff Layton 		nfsd_file_close_inode_sync(file_inode(fl->fl_file));
65565294c1fSJeff Layton 	return 0;
65665294c1fSJeff Layton }
65765294c1fSJeff Layton 
65865294c1fSJeff Layton static struct notifier_block nfsd_file_lease_notifier = {
65965294c1fSJeff Layton 	.notifier_call = nfsd_file_lease_notifier_call,
66065294c1fSJeff Layton };
66165294c1fSJeff Layton 
66265294c1fSJeff Layton static int
663b9a1b977SAmir Goldstein nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
664b9a1b977SAmir Goldstein 				struct inode *inode, struct inode *dir,
665950cc0d2SAmir Goldstein 				const struct qstr *name, u32 cookie)
66665294c1fSJeff Layton {
66724dca905SGabriel Krisman Bertazi 	if (WARN_ON_ONCE(!inode))
66824dca905SGabriel Krisman Bertazi 		return 0;
66924dca905SGabriel Krisman Bertazi 
67065294c1fSJeff Layton 	trace_nfsd_file_fsnotify_handle_event(inode, mask);
67165294c1fSJeff Layton 
67265294c1fSJeff Layton 	/* Should be no marks on non-regular files */
67365294c1fSJeff Layton 	if (!S_ISREG(inode->i_mode)) {
67465294c1fSJeff Layton 		WARN_ON_ONCE(1);
67565294c1fSJeff Layton 		return 0;
67665294c1fSJeff Layton 	}
67765294c1fSJeff Layton 
67865294c1fSJeff Layton 	/* don't close files if this was not the last link */
67965294c1fSJeff Layton 	if (mask & FS_ATTRIB) {
68065294c1fSJeff Layton 		if (inode->i_nlink)
68165294c1fSJeff Layton 			return 0;
68265294c1fSJeff Layton 	}
68365294c1fSJeff Layton 
68465294c1fSJeff Layton 	nfsd_file_close_inode(inode);
68565294c1fSJeff Layton 	return 0;
68665294c1fSJeff Layton }
68765294c1fSJeff Layton 
68865294c1fSJeff Layton 
68965294c1fSJeff Layton static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
690b9a1b977SAmir Goldstein 	.handle_inode_event = nfsd_file_fsnotify_handle_event,
69165294c1fSJeff Layton 	.free_mark = nfsd_file_mark_free,
69265294c1fSJeff Layton };
69365294c1fSJeff Layton 
69465294c1fSJeff Layton int
69565294c1fSJeff Layton nfsd_file_cache_init(void)
69665294c1fSJeff Layton {
69765294c1fSJeff Layton 	int		ret = -ENOMEM;
69865294c1fSJeff Layton 	unsigned int	i;
69965294c1fSJeff Layton 
700*c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
701*c7b824c3SChuck Lever 	if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
70265294c1fSJeff Layton 		return 0;
70365294c1fSJeff Layton 
7049542e6a6STrond Myklebust 	nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
7059542e6a6STrond Myklebust 	if (!nfsd_filecache_wq)
7069542e6a6STrond Myklebust 		goto out;
7079542e6a6STrond Myklebust 
7084d2eeafeSAmir Goldstein 	nfsd_file_hashtbl = kvcalloc(NFSD_FILE_HASH_SIZE,
70965294c1fSJeff Layton 				sizeof(*nfsd_file_hashtbl), GFP_KERNEL);
71065294c1fSJeff Layton 	if (!nfsd_file_hashtbl) {
71165294c1fSJeff Layton 		pr_err("nfsd: unable to allocate nfsd_file_hashtbl\n");
71265294c1fSJeff Layton 		goto out_err;
71365294c1fSJeff Layton 	}
71465294c1fSJeff Layton 
71565294c1fSJeff Layton 	nfsd_file_slab = kmem_cache_create("nfsd_file",
71665294c1fSJeff Layton 				sizeof(struct nfsd_file), 0, 0, NULL);
71765294c1fSJeff Layton 	if (!nfsd_file_slab) {
71865294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_slab\n");
71965294c1fSJeff Layton 		goto out_err;
72065294c1fSJeff Layton 	}
72165294c1fSJeff Layton 
72265294c1fSJeff Layton 	nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
72365294c1fSJeff Layton 					sizeof(struct nfsd_file_mark), 0, 0, NULL);
72465294c1fSJeff Layton 	if (!nfsd_file_mark_slab) {
72565294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
72665294c1fSJeff Layton 		goto out_err;
72765294c1fSJeff Layton 	}
72865294c1fSJeff Layton 
72965294c1fSJeff Layton 
73065294c1fSJeff Layton 	ret = list_lru_init(&nfsd_file_lru);
73165294c1fSJeff Layton 	if (ret) {
73265294c1fSJeff Layton 		pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
73365294c1fSJeff Layton 		goto out_err;
73465294c1fSJeff Layton 	}
73565294c1fSJeff Layton 
73665294c1fSJeff Layton 	ret = register_shrinker(&nfsd_file_shrinker);
73765294c1fSJeff Layton 	if (ret) {
73865294c1fSJeff Layton 		pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
73965294c1fSJeff Layton 		goto out_lru;
74065294c1fSJeff Layton 	}
74165294c1fSJeff Layton 
74265294c1fSJeff Layton 	ret = lease_register_notifier(&nfsd_file_lease_notifier);
74365294c1fSJeff Layton 	if (ret) {
74465294c1fSJeff Layton 		pr_err("nfsd: unable to register lease notifier: %d\n", ret);
74565294c1fSJeff Layton 		goto out_shrinker;
74665294c1fSJeff Layton 	}
74765294c1fSJeff Layton 
748867a448dSAmir Goldstein 	nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
749b8962a9dSAmir Goldstein 							FSNOTIFY_GROUP_NOFS);
75065294c1fSJeff Layton 	if (IS_ERR(nfsd_file_fsnotify_group)) {
75165294c1fSJeff Layton 		pr_err("nfsd: unable to create fsnotify group: %ld\n",
75265294c1fSJeff Layton 			PTR_ERR(nfsd_file_fsnotify_group));
753231307dfSHuang Guobin 		ret = PTR_ERR(nfsd_file_fsnotify_group);
75465294c1fSJeff Layton 		nfsd_file_fsnotify_group = NULL;
75565294c1fSJeff Layton 		goto out_notifier;
75665294c1fSJeff Layton 	}
75765294c1fSJeff Layton 
75865294c1fSJeff Layton 	for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
75965294c1fSJeff Layton 		INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head);
76065294c1fSJeff Layton 		spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock);
76165294c1fSJeff Layton 	}
76265294c1fSJeff Layton 
7639542e6a6STrond Myklebust 	INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
76465294c1fSJeff Layton out:
76565294c1fSJeff Layton 	return ret;
76665294c1fSJeff Layton out_notifier:
76765294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
76865294c1fSJeff Layton out_shrinker:
76965294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
77065294c1fSJeff Layton out_lru:
77165294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
77265294c1fSJeff Layton out_err:
77365294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
77465294c1fSJeff Layton 	nfsd_file_slab = NULL;
77565294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
77665294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
7774d2eeafeSAmir Goldstein 	kvfree(nfsd_file_hashtbl);
77865294c1fSJeff Layton 	nfsd_file_hashtbl = NULL;
7799542e6a6STrond Myklebust 	destroy_workqueue(nfsd_filecache_wq);
7809542e6a6STrond Myklebust 	nfsd_filecache_wq = NULL;
78165294c1fSJeff Layton 	goto out;
78265294c1fSJeff Layton }
78365294c1fSJeff Layton 
78465294c1fSJeff Layton /*
78565294c1fSJeff Layton  * Note this can deadlock with nfsd_file_lru_cb.
78665294c1fSJeff Layton  */
787*c7b824c3SChuck Lever static void
788*c7b824c3SChuck Lever __nfsd_file_cache_purge(struct net *net)
78965294c1fSJeff Layton {
79065294c1fSJeff Layton 	unsigned int		i;
79165294c1fSJeff Layton 	struct nfsd_file	*nf;
7925e113224STrond Myklebust 	struct hlist_node	*next;
79365294c1fSJeff Layton 	LIST_HEAD(dispose);
79465294c1fSJeff Layton 	bool del;
79565294c1fSJeff Layton 
79665294c1fSJeff Layton 	for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
7975e113224STrond Myklebust 		struct nfsd_fcache_bucket *nfb = &nfsd_file_hashtbl[i];
7985e113224STrond Myklebust 
7995e113224STrond Myklebust 		spin_lock(&nfb->nfb_lock);
8005e113224STrond Myklebust 		hlist_for_each_entry_safe(nf, next, &nfb->nfb_head, nf_node) {
8015e113224STrond Myklebust 			if (net && nf->nf_net != net)
8025e113224STrond Myklebust 				continue;
80365294c1fSJeff Layton 			del = nfsd_file_unhash_and_release_locked(nf, &dispose);
80465294c1fSJeff Layton 
80565294c1fSJeff Layton 			/*
80665294c1fSJeff Layton 			 * Deadlock detected! Something marked this entry as
80765294c1fSJeff Layton 			 * unhased, but hasn't removed it from the hash list.
80865294c1fSJeff Layton 			 */
80965294c1fSJeff Layton 			WARN_ON_ONCE(!del);
81065294c1fSJeff Layton 		}
8115e113224STrond Myklebust 		spin_unlock(&nfb->nfb_lock);
81265294c1fSJeff Layton 		nfsd_file_dispose_list(&dispose);
81365294c1fSJeff Layton 	}
81465294c1fSJeff Layton }
81565294c1fSJeff Layton 
8169542e6a6STrond Myklebust static struct nfsd_fcache_disposal *
8171463b38eSNeilBrown nfsd_alloc_fcache_disposal(void)
8189542e6a6STrond Myklebust {
8199542e6a6STrond Myklebust 	struct nfsd_fcache_disposal *l;
8209542e6a6STrond Myklebust 
8219542e6a6STrond Myklebust 	l = kmalloc(sizeof(*l), GFP_KERNEL);
8229542e6a6STrond Myklebust 	if (!l)
8239542e6a6STrond Myklebust 		return NULL;
8249542e6a6STrond Myklebust 	INIT_WORK(&l->work, nfsd_file_delayed_close);
8259542e6a6STrond Myklebust 	spin_lock_init(&l->lock);
8269542e6a6STrond Myklebust 	INIT_LIST_HEAD(&l->freeme);
8279542e6a6STrond Myklebust 	return l;
8289542e6a6STrond Myklebust }
8299542e6a6STrond Myklebust 
8309542e6a6STrond Myklebust static void
8319542e6a6STrond Myklebust nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
8329542e6a6STrond Myklebust {
8339542e6a6STrond Myklebust 	cancel_work_sync(&l->work);
8349542e6a6STrond Myklebust 	nfsd_file_dispose_list(&l->freeme);
8351463b38eSNeilBrown 	kfree(l);
8369542e6a6STrond Myklebust }
8379542e6a6STrond Myklebust 
8389542e6a6STrond Myklebust static void
8399542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(struct net *net)
8409542e6a6STrond Myklebust {
8411463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8421463b38eSNeilBrown 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
8439542e6a6STrond Myklebust 
8449542e6a6STrond Myklebust 	nfsd_free_fcache_disposal(l);
8459542e6a6STrond Myklebust }
8469542e6a6STrond Myklebust 
8479542e6a6STrond Myklebust int
8489542e6a6STrond Myklebust nfsd_file_cache_start_net(struct net *net)
8499542e6a6STrond Myklebust {
8501463b38eSNeilBrown 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8511463b38eSNeilBrown 
8521463b38eSNeilBrown 	nn->fcache_disposal = nfsd_alloc_fcache_disposal();
8531463b38eSNeilBrown 	return nn->fcache_disposal ? 0 : -ENOMEM;
8549542e6a6STrond Myklebust }
8559542e6a6STrond Myklebust 
856*c7b824c3SChuck Lever /**
857*c7b824c3SChuck Lever  * nfsd_file_cache_purge - Remove all cache items associated with @net
858*c7b824c3SChuck Lever  * @net: target net namespace
859*c7b824c3SChuck Lever  *
860*c7b824c3SChuck Lever  */
861*c7b824c3SChuck Lever void
862*c7b824c3SChuck Lever nfsd_file_cache_purge(struct net *net)
863*c7b824c3SChuck Lever {
864*c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
865*c7b824c3SChuck Lever 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
866*c7b824c3SChuck Lever 		__nfsd_file_cache_purge(net);
867*c7b824c3SChuck Lever }
868*c7b824c3SChuck Lever 
8699542e6a6STrond Myklebust void
8709542e6a6STrond Myklebust nfsd_file_cache_shutdown_net(struct net *net)
8719542e6a6STrond Myklebust {
8729542e6a6STrond Myklebust 	nfsd_file_cache_purge(net);
8739542e6a6STrond Myklebust 	nfsd_free_fcache_disposal_net(net);
8749542e6a6STrond Myklebust }
8759542e6a6STrond Myklebust 
87665294c1fSJeff Layton void
87765294c1fSJeff Layton nfsd_file_cache_shutdown(void)
87865294c1fSJeff Layton {
8798b330f78SChuck Lever 	int i;
8808b330f78SChuck Lever 
881*c7b824c3SChuck Lever 	lockdep_assert_held(&nfsd_mutex);
882*c7b824c3SChuck Lever 	if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
883*c7b824c3SChuck Lever 		return;
88465294c1fSJeff Layton 
88565294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
88665294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
88765294c1fSJeff Layton 	/*
88865294c1fSJeff Layton 	 * make sure all callers of nfsd_file_lru_cb are done before
88965294c1fSJeff Layton 	 * calling nfsd_file_cache_purge
89065294c1fSJeff Layton 	 */
89165294c1fSJeff Layton 	cancel_delayed_work_sync(&nfsd_filecache_laundrette);
892*c7b824c3SChuck Lever 	__nfsd_file_cache_purge(NULL);
89365294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
89465294c1fSJeff Layton 	rcu_barrier();
89565294c1fSJeff Layton 	fsnotify_put_group(nfsd_file_fsnotify_group);
89665294c1fSJeff Layton 	nfsd_file_fsnotify_group = NULL;
89765294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
89865294c1fSJeff Layton 	nfsd_file_slab = NULL;
89965294c1fSJeff Layton 	fsnotify_wait_marks_destroyed();
90065294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
90165294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
9024d2eeafeSAmir Goldstein 	kvfree(nfsd_file_hashtbl);
90365294c1fSJeff Layton 	nfsd_file_hashtbl = NULL;
9049542e6a6STrond Myklebust 	destroy_workqueue(nfsd_filecache_wq);
9059542e6a6STrond Myklebust 	nfsd_filecache_wq = NULL;
9068b330f78SChuck Lever 
9078b330f78SChuck Lever 	for_each_possible_cpu(i) {
9088b330f78SChuck Lever 		per_cpu(nfsd_file_cache_hits, i) = 0;
9098b330f78SChuck Lever 		per_cpu(nfsd_file_acquisitions, i) = 0;
9108b330f78SChuck Lever 		per_cpu(nfsd_file_releases, i) = 0;
9118b330f78SChuck Lever 		per_cpu(nfsd_file_total_age, i) = 0;
9128b330f78SChuck Lever 		per_cpu(nfsd_file_pages_flushed, i) = 0;
9138b330f78SChuck Lever 		per_cpu(nfsd_file_evictions, i) = 0;
9148b330f78SChuck Lever 	}
91565294c1fSJeff Layton }
91665294c1fSJeff Layton 
91765294c1fSJeff Layton static bool
91865294c1fSJeff Layton nfsd_match_cred(const struct cred *c1, const struct cred *c2)
91965294c1fSJeff Layton {
92065294c1fSJeff Layton 	int i;
92165294c1fSJeff Layton 
92265294c1fSJeff Layton 	if (!uid_eq(c1->fsuid, c2->fsuid))
92365294c1fSJeff Layton 		return false;
92465294c1fSJeff Layton 	if (!gid_eq(c1->fsgid, c2->fsgid))
92565294c1fSJeff Layton 		return false;
92665294c1fSJeff Layton 	if (c1->group_info == NULL || c2->group_info == NULL)
92765294c1fSJeff Layton 		return c1->group_info == c2->group_info;
92865294c1fSJeff Layton 	if (c1->group_info->ngroups != c2->group_info->ngroups)
92965294c1fSJeff Layton 		return false;
93065294c1fSJeff Layton 	for (i = 0; i < c1->group_info->ngroups; i++) {
93165294c1fSJeff Layton 		if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
93265294c1fSJeff Layton 			return false;
93365294c1fSJeff Layton 	}
93465294c1fSJeff Layton 	return true;
93565294c1fSJeff Layton }
93665294c1fSJeff Layton 
93765294c1fSJeff Layton static struct nfsd_file *
93865294c1fSJeff Layton nfsd_file_find_locked(struct inode *inode, unsigned int may_flags,
9395e113224STrond Myklebust 			unsigned int hashval, struct net *net)
94065294c1fSJeff Layton {
94165294c1fSJeff Layton 	struct nfsd_file *nf;
94265294c1fSJeff Layton 	unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
94365294c1fSJeff Layton 
94465294c1fSJeff Layton 	hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
945057a2274SMadhuparna Bhowmik 				 nf_node, lockdep_is_held(&nfsd_file_hashtbl[hashval].nfb_lock)) {
946ae3c57b5SJ. Bruce Fields 		if (nf->nf_may != need)
94765294c1fSJeff Layton 			continue;
94865294c1fSJeff Layton 		if (nf->nf_inode != inode)
94965294c1fSJeff Layton 			continue;
9505e113224STrond Myklebust 		if (nf->nf_net != net)
9515e113224STrond Myklebust 			continue;
95265294c1fSJeff Layton 		if (!nfsd_match_cred(nf->nf_cred, current_cred()))
95365294c1fSJeff Layton 			continue;
954d30881f5STrond Myklebust 		if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags))
955d30881f5STrond Myklebust 			continue;
95665294c1fSJeff Layton 		if (nfsd_file_get(nf) != NULL)
95765294c1fSJeff Layton 			return nf;
95865294c1fSJeff Layton 	}
95965294c1fSJeff Layton 	return NULL;
96065294c1fSJeff Layton }
96165294c1fSJeff Layton 
96265294c1fSJeff Layton /**
96365294c1fSJeff Layton  * nfsd_file_is_cached - are there any cached open files for this fh?
96465294c1fSJeff Layton  * @inode: inode of the file to check
96565294c1fSJeff Layton  *
96665294c1fSJeff Layton  * Scan the hashtable for open files that match this fh. Returns true if there
96765294c1fSJeff Layton  * are any, and false if not.
96865294c1fSJeff Layton  */
96965294c1fSJeff Layton bool
97065294c1fSJeff Layton nfsd_file_is_cached(struct inode *inode)
97165294c1fSJeff Layton {
97265294c1fSJeff Layton 	bool			ret = false;
97365294c1fSJeff Layton 	struct nfsd_file	*nf;
97465294c1fSJeff Layton 	unsigned int		hashval;
97565294c1fSJeff Layton 
97665294c1fSJeff Layton         hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
97765294c1fSJeff Layton 
97865294c1fSJeff Layton 	rcu_read_lock();
97965294c1fSJeff Layton 	hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
98065294c1fSJeff Layton 				 nf_node) {
98165294c1fSJeff Layton 		if (inode == nf->nf_inode) {
98265294c1fSJeff Layton 			ret = true;
98365294c1fSJeff Layton 			break;
98465294c1fSJeff Layton 		}
98565294c1fSJeff Layton 	}
98665294c1fSJeff Layton 	rcu_read_unlock();
98754f7df70SChuck Lever 	trace_nfsd_file_is_cached(inode, (int)ret);
98865294c1fSJeff Layton 	return ret;
98965294c1fSJeff Layton }
99065294c1fSJeff Layton 
991fb70bf12SChuck Lever static __be32
992fb70bf12SChuck Lever nfsd_do_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
993fb70bf12SChuck Lever 		     unsigned int may_flags, struct nfsd_file **pnf, bool open)
99465294c1fSJeff Layton {
99565294c1fSJeff Layton 	__be32	status;
9965e113224STrond Myklebust 	struct net *net = SVC_NET(rqstp);
99765294c1fSJeff Layton 	struct nfsd_file *nf, *new;
99865294c1fSJeff Layton 	struct inode *inode;
99965294c1fSJeff Layton 	unsigned int hashval;
100028c7d86bSTrond Myklebust 	bool retry = true;
100165294c1fSJeff Layton 
100265294c1fSJeff Layton 	/* FIXME: skip this if fh_dentry is already set? */
100365294c1fSJeff Layton 	status = fh_verify(rqstp, fhp, S_IFREG,
100465294c1fSJeff Layton 				may_flags|NFSD_MAY_OWNER_OVERRIDE);
100565294c1fSJeff Layton 	if (status != nfs_ok)
100665294c1fSJeff Layton 		return status;
100765294c1fSJeff Layton 
100865294c1fSJeff Layton 	inode = d_inode(fhp->fh_dentry);
100965294c1fSJeff Layton 	hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
101065294c1fSJeff Layton retry:
101165294c1fSJeff Layton 	rcu_read_lock();
10125e113224STrond Myklebust 	nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
101365294c1fSJeff Layton 	rcu_read_unlock();
101465294c1fSJeff Layton 	if (nf)
101565294c1fSJeff Layton 		goto wait_for_construction;
101665294c1fSJeff Layton 
1017f0743c2bSChuck Lever 	new = nfsd_file_alloc(inode, may_flags, net);
101865294c1fSJeff Layton 	if (!new) {
101954f7df70SChuck Lever 		status = nfserr_jukebox;
102054f7df70SChuck Lever 		goto out_status;
102165294c1fSJeff Layton 	}
102265294c1fSJeff Layton 
102365294c1fSJeff Layton 	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
10245e113224STrond Myklebust 	nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
102565294c1fSJeff Layton 	if (nf == NULL)
102665294c1fSJeff Layton 		goto open_file;
102765294c1fSJeff Layton 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
102865294c1fSJeff Layton 	nfsd_file_slab_free(&new->nf_rcu);
102965294c1fSJeff Layton 
103065294c1fSJeff Layton wait_for_construction:
103165294c1fSJeff Layton 	wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
103265294c1fSJeff Layton 
103365294c1fSJeff Layton 	/* Did construction of this file fail? */
103465294c1fSJeff Layton 	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
103528c7d86bSTrond Myklebust 		if (!retry) {
103628c7d86bSTrond Myklebust 			status = nfserr_jukebox;
103728c7d86bSTrond Myklebust 			goto out;
103828c7d86bSTrond Myklebust 		}
103928c7d86bSTrond Myklebust 		retry = false;
104065294c1fSJeff Layton 		nfsd_file_put_noref(nf);
104165294c1fSJeff Layton 		goto retry;
104265294c1fSJeff Layton 	}
104365294c1fSJeff Layton 
10444a0e73e6SChuck Lever 	nfsd_file_lru_remove(nf);
104565294c1fSJeff Layton 	this_cpu_inc(nfsd_file_cache_hits);
104665294c1fSJeff Layton 
104723ba98deSJeff Layton 	status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
104865294c1fSJeff Layton out:
104965294c1fSJeff Layton 	if (status == nfs_ok) {
105029d4bdbbSChuck Lever 		if (open)
105129d4bdbbSChuck Lever 			this_cpu_inc(nfsd_file_acquisitions);
105265294c1fSJeff Layton 		*pnf = nf;
105365294c1fSJeff Layton 	} else {
105465294c1fSJeff Layton 		nfsd_file_put(nf);
105565294c1fSJeff Layton 		nf = NULL;
105665294c1fSJeff Layton 	}
105765294c1fSJeff Layton 
105854f7df70SChuck Lever out_status:
105954f7df70SChuck Lever 	trace_nfsd_file_acquire(rqstp, inode, may_flags, nf, status);
106065294c1fSJeff Layton 	return status;
106154f7df70SChuck Lever 
106265294c1fSJeff Layton open_file:
106365294c1fSJeff Layton 	nf = new;
106465294c1fSJeff Layton 	/* Take reference for the hashtable */
1065689827cdSTrond Myklebust 	refcount_inc(&nf->nf_ref);
106665294c1fSJeff Layton 	__set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
106765294c1fSJeff Layton 	__set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
106865294c1fSJeff Layton 	hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head);
106965294c1fSJeff Layton 	++nfsd_file_hashtbl[hashval].nfb_count;
107065294c1fSJeff Layton 	nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount,
107165294c1fSJeff Layton 			nfsd_file_hashtbl[hashval].nfb_count);
107265294c1fSJeff Layton 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
10736df19411SChuck Lever 	atomic_long_inc(&nfsd_filecache_count);
107465294c1fSJeff Layton 
107565294c1fSJeff Layton 	nf->nf_mark = nfsd_file_mark_find_or_create(nf);
1076fb70bf12SChuck Lever 	if (nf->nf_mark) {
10770122e882SChuck Lever 		if (open) {
1078f4d84c52SChuck Lever 			status = nfsd_open_verified(rqstp, fhp, may_flags,
1079f4d84c52SChuck Lever 						    &nf->nf_file);
10800122e882SChuck Lever 			trace_nfsd_file_open(nf, status);
10810122e882SChuck Lever 		} else
1082fb70bf12SChuck Lever 			status = nfs_ok;
1083fb70bf12SChuck Lever 	} else
108465294c1fSJeff Layton 		status = nfserr_jukebox;
108565294c1fSJeff Layton 	/*
108665294c1fSJeff Layton 	 * If construction failed, or we raced with a call to unlink()
108765294c1fSJeff Layton 	 * then unhash.
108865294c1fSJeff Layton 	 */
108965294c1fSJeff Layton 	if (status != nfs_ok || inode->i_nlink == 0) {
109065294c1fSJeff Layton 		bool do_free;
10914a0e73e6SChuck Lever 		nfsd_file_lru_remove(nf);
109265294c1fSJeff Layton 		spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
109365294c1fSJeff Layton 		do_free = nfsd_file_unhash(nf);
109465294c1fSJeff Layton 		spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
109565294c1fSJeff Layton 		if (do_free)
109665294c1fSJeff Layton 			nfsd_file_put_noref(nf);
109765294c1fSJeff Layton 	}
109865294c1fSJeff Layton 	clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
109965294c1fSJeff Layton 	smp_mb__after_atomic();
110065294c1fSJeff Layton 	wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
110165294c1fSJeff Layton 	goto out;
110265294c1fSJeff Layton }
110365294c1fSJeff Layton 
1104fb70bf12SChuck Lever /**
1105fb70bf12SChuck Lever  * nfsd_file_acquire - Get a struct nfsd_file with an open file
1106fb70bf12SChuck Lever  * @rqstp: the RPC transaction being executed
1107fb70bf12SChuck Lever  * @fhp: the NFS filehandle of the file to be opened
1108fb70bf12SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
1109fb70bf12SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1110fb70bf12SChuck Lever  *
1111fb70bf12SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1112fb70bf12SChuck Lever  * network byte order is returned.
1113fb70bf12SChuck Lever  */
1114fb70bf12SChuck Lever __be32
1115fb70bf12SChuck Lever nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1116fb70bf12SChuck Lever 		  unsigned int may_flags, struct nfsd_file **pnf)
1117fb70bf12SChuck Lever {
1118fb70bf12SChuck Lever 	return nfsd_do_file_acquire(rqstp, fhp, may_flags, pnf, true);
1119fb70bf12SChuck Lever }
1120fb70bf12SChuck Lever 
1121fb70bf12SChuck Lever /**
1122fb70bf12SChuck Lever  * nfsd_file_create - Get a struct nfsd_file, do not open
1123fb70bf12SChuck Lever  * @rqstp: the RPC transaction being executed
1124fb70bf12SChuck Lever  * @fhp: the NFS filehandle of the file just created
1125fb70bf12SChuck Lever  * @may_flags: NFSD_MAY_ settings for the file
1126fb70bf12SChuck Lever  * @pnf: OUT: new or found "struct nfsd_file" object
1127fb70bf12SChuck Lever  *
1128fb70bf12SChuck Lever  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1129fb70bf12SChuck Lever  * network byte order is returned.
1130fb70bf12SChuck Lever  */
1131fb70bf12SChuck Lever __be32
1132fb70bf12SChuck Lever nfsd_file_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1133fb70bf12SChuck Lever 		 unsigned int may_flags, struct nfsd_file **pnf)
1134fb70bf12SChuck Lever {
1135fb70bf12SChuck Lever 	return nfsd_do_file_acquire(rqstp, fhp, may_flags, pnf, false);
1136fb70bf12SChuck Lever }
1137fb70bf12SChuck Lever 
113865294c1fSJeff Layton /*
113965294c1fSJeff Layton  * Note that fields may be added, removed or reordered in the future. Programs
114065294c1fSJeff Layton  * scraping this file for info should test the labels to ensure they're
114165294c1fSJeff Layton  * getting the correct field.
114265294c1fSJeff Layton  */
114365294c1fSJeff Layton static int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
114465294c1fSJeff Layton {
1145df2aff52SChuck Lever 	unsigned long releases = 0, pages_flushed = 0, evictions = 0;
1146df2aff52SChuck Lever 	unsigned long hits = 0, acquisitions = 0;
114765294c1fSJeff Layton 	unsigned int i, count = 0, longest = 0;
1148904940e9SChuck Lever 	unsigned long lru = 0, total_age = 0;
114965294c1fSJeff Layton 
115065294c1fSJeff Layton 	/*
115165294c1fSJeff Layton 	 * No need for spinlocks here since we're not terribly interested in
115265294c1fSJeff Layton 	 * accuracy. We do take the nfsd_mutex simply to ensure that we
115365294c1fSJeff Layton 	 * don't end up racing with server shutdown
115465294c1fSJeff Layton 	 */
115565294c1fSJeff Layton 	mutex_lock(&nfsd_mutex);
1156*c7b824c3SChuck Lever 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
115765294c1fSJeff Layton 		for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
115865294c1fSJeff Layton 			count += nfsd_file_hashtbl[i].nfb_count;
115965294c1fSJeff Layton 			longest = max(longest, nfsd_file_hashtbl[i].nfb_count);
116065294c1fSJeff Layton 		}
11610fd244c1SChuck Lever 		lru = list_lru_count(&nfsd_file_lru);
116265294c1fSJeff Layton 	}
116365294c1fSJeff Layton 	mutex_unlock(&nfsd_mutex);
116465294c1fSJeff Layton 
116529d4bdbbSChuck Lever 	for_each_possible_cpu(i) {
116665294c1fSJeff Layton 		hits += per_cpu(nfsd_file_cache_hits, i);
116729d4bdbbSChuck Lever 		acquisitions += per_cpu(nfsd_file_acquisitions, i);
1168d6329327SChuck Lever 		releases += per_cpu(nfsd_file_releases, i);
1169904940e9SChuck Lever 		total_age += per_cpu(nfsd_file_total_age, i);
117094660cc1SChuck Lever 		evictions += per_cpu(nfsd_file_evictions, i);
1171df2aff52SChuck Lever 		pages_flushed += per_cpu(nfsd_file_pages_flushed, i);
117229d4bdbbSChuck Lever 	}
117365294c1fSJeff Layton 
117465294c1fSJeff Layton 	seq_printf(m, "total entries: %u\n", count);
117565294c1fSJeff Layton 	seq_printf(m, "longest chain: %u\n", longest);
11760fd244c1SChuck Lever 	seq_printf(m, "lru entries:   %lu\n", lru);
117765294c1fSJeff Layton 	seq_printf(m, "cache hits:    %lu\n", hits);
117829d4bdbbSChuck Lever 	seq_printf(m, "acquisitions:  %lu\n", acquisitions);
1179d6329327SChuck Lever 	seq_printf(m, "releases:      %lu\n", releases);
118094660cc1SChuck Lever 	seq_printf(m, "evictions:     %lu\n", evictions);
1181904940e9SChuck Lever 	if (releases)
1182904940e9SChuck Lever 		seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1183904940e9SChuck Lever 	else
1184904940e9SChuck Lever 		seq_printf(m, "mean age (ms): -\n");
1185df2aff52SChuck Lever 	seq_printf(m, "pages flushed: %lu\n", pages_flushed);
118665294c1fSJeff Layton 	return 0;
118765294c1fSJeff Layton }
118865294c1fSJeff Layton 
118965294c1fSJeff Layton int nfsd_file_cache_stats_open(struct inode *inode, struct file *file)
119065294c1fSJeff Layton {
119165294c1fSJeff Layton 	return single_open(file, nfsd_file_cache_stats_show, NULL);
119265294c1fSJeff Layton }
1193