xref: /openbmc/linux/fs/nfsd/filecache.c (revision 36ebbdb96b694dd9c6b25ad98f2bbd263d022b63)
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>
1065294c1fSJeff Layton #include <linux/sched.h>
1165294c1fSJeff Layton #include <linux/list_lru.h>
1265294c1fSJeff Layton #include <linux/fsnotify_backend.h>
1365294c1fSJeff Layton #include <linux/fsnotify.h>
1465294c1fSJeff Layton #include <linux/seq_file.h>
1565294c1fSJeff Layton 
1665294c1fSJeff Layton #include "vfs.h"
1765294c1fSJeff Layton #include "nfsd.h"
1865294c1fSJeff Layton #include "nfsfh.h"
195e113224STrond Myklebust #include "netns.h"
2065294c1fSJeff Layton #include "filecache.h"
2165294c1fSJeff Layton #include "trace.h"
2265294c1fSJeff Layton 
2365294c1fSJeff Layton #define NFSDDBG_FACILITY	NFSDDBG_FH
2465294c1fSJeff Layton 
2565294c1fSJeff Layton /* FIXME: dynamically size this for the machine somehow? */
2665294c1fSJeff Layton #define NFSD_FILE_HASH_BITS                   12
2765294c1fSJeff Layton #define NFSD_FILE_HASH_SIZE                  (1 << NFSD_FILE_HASH_BITS)
2865294c1fSJeff Layton #define NFSD_LAUNDRETTE_DELAY		     (2 * HZ)
2965294c1fSJeff Layton 
3065294c1fSJeff Layton #define NFSD_FILE_LRU_RESCAN		     (0)
3165294c1fSJeff Layton #define NFSD_FILE_SHUTDOWN		     (1)
3265294c1fSJeff Layton #define NFSD_FILE_LRU_THRESHOLD		     (4096UL)
3365294c1fSJeff Layton #define NFSD_FILE_LRU_LIMIT		     (NFSD_FILE_LRU_THRESHOLD << 2)
3465294c1fSJeff Layton 
3565294c1fSJeff Layton /* We only care about NFSD_MAY_READ/WRITE for this cache */
3665294c1fSJeff Layton #define NFSD_FILE_MAY_MASK	(NFSD_MAY_READ|NFSD_MAY_WRITE)
3765294c1fSJeff Layton 
3865294c1fSJeff Layton struct nfsd_fcache_bucket {
3965294c1fSJeff Layton 	struct hlist_head	nfb_head;
4065294c1fSJeff Layton 	spinlock_t		nfb_lock;
4165294c1fSJeff Layton 	unsigned int		nfb_count;
4265294c1fSJeff Layton 	unsigned int		nfb_maxcount;
4365294c1fSJeff Layton };
4465294c1fSJeff Layton 
4565294c1fSJeff Layton static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
4665294c1fSJeff Layton 
4765294c1fSJeff Layton static struct kmem_cache		*nfsd_file_slab;
4865294c1fSJeff Layton static struct kmem_cache		*nfsd_file_mark_slab;
4965294c1fSJeff Layton static struct nfsd_fcache_bucket	*nfsd_file_hashtbl;
5065294c1fSJeff Layton static struct list_lru			nfsd_file_lru;
5165294c1fSJeff Layton static long				nfsd_file_lru_flags;
5265294c1fSJeff Layton static struct fsnotify_group		*nfsd_file_fsnotify_group;
5365294c1fSJeff Layton static atomic_long_t			nfsd_filecache_count;
5465294c1fSJeff Layton static struct delayed_work		nfsd_filecache_laundrette;
5565294c1fSJeff Layton 
5665294c1fSJeff Layton enum nfsd_file_laundrette_ctl {
5765294c1fSJeff Layton 	NFSD_FILE_LAUNDRETTE_NOFLUSH = 0,
5865294c1fSJeff Layton 	NFSD_FILE_LAUNDRETTE_MAY_FLUSH
5965294c1fSJeff Layton };
6065294c1fSJeff Layton 
6165294c1fSJeff Layton static void
6265294c1fSJeff Layton nfsd_file_schedule_laundrette(enum nfsd_file_laundrette_ctl ctl)
6365294c1fSJeff Layton {
6465294c1fSJeff Layton 	long count = atomic_long_read(&nfsd_filecache_count);
6565294c1fSJeff Layton 
6665294c1fSJeff Layton 	if (count == 0 || test_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags))
6765294c1fSJeff Layton 		return;
6865294c1fSJeff Layton 
6965294c1fSJeff Layton 	/* Be more aggressive about scanning if over the threshold */
7065294c1fSJeff Layton 	if (count > NFSD_FILE_LRU_THRESHOLD)
7165294c1fSJeff Layton 		mod_delayed_work(system_wq, &nfsd_filecache_laundrette, 0);
7265294c1fSJeff Layton 	else
7365294c1fSJeff Layton 		schedule_delayed_work(&nfsd_filecache_laundrette, NFSD_LAUNDRETTE_DELAY);
7465294c1fSJeff Layton 
7565294c1fSJeff Layton 	if (ctl == NFSD_FILE_LAUNDRETTE_NOFLUSH)
7665294c1fSJeff Layton 		return;
7765294c1fSJeff Layton 
7865294c1fSJeff Layton 	/* ...and don't delay flushing if we're out of control */
7965294c1fSJeff Layton 	if (count >= NFSD_FILE_LRU_LIMIT)
8065294c1fSJeff Layton 		flush_delayed_work(&nfsd_filecache_laundrette);
8165294c1fSJeff Layton }
8265294c1fSJeff Layton 
8365294c1fSJeff Layton static void
8465294c1fSJeff Layton nfsd_file_slab_free(struct rcu_head *rcu)
8565294c1fSJeff Layton {
8665294c1fSJeff Layton 	struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
8765294c1fSJeff Layton 
8865294c1fSJeff Layton 	put_cred(nf->nf_cred);
8965294c1fSJeff Layton 	kmem_cache_free(nfsd_file_slab, nf);
9065294c1fSJeff Layton }
9165294c1fSJeff Layton 
9265294c1fSJeff Layton static void
9365294c1fSJeff Layton nfsd_file_mark_free(struct fsnotify_mark *mark)
9465294c1fSJeff Layton {
9565294c1fSJeff Layton 	struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
9665294c1fSJeff Layton 						  nfm_mark);
9765294c1fSJeff Layton 
9865294c1fSJeff Layton 	kmem_cache_free(nfsd_file_mark_slab, nfm);
9965294c1fSJeff Layton }
10065294c1fSJeff Layton 
10165294c1fSJeff Layton static struct nfsd_file_mark *
10265294c1fSJeff Layton nfsd_file_mark_get(struct nfsd_file_mark *nfm)
10365294c1fSJeff Layton {
10465294c1fSJeff Layton 	if (!atomic_inc_not_zero(&nfm->nfm_ref))
10565294c1fSJeff Layton 		return NULL;
10665294c1fSJeff Layton 	return nfm;
10765294c1fSJeff Layton }
10865294c1fSJeff Layton 
10965294c1fSJeff Layton static void
11065294c1fSJeff Layton nfsd_file_mark_put(struct nfsd_file_mark *nfm)
11165294c1fSJeff Layton {
11265294c1fSJeff Layton 	if (atomic_dec_and_test(&nfm->nfm_ref)) {
11365294c1fSJeff Layton 
11465294c1fSJeff Layton 		fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
11565294c1fSJeff Layton 		fsnotify_put_mark(&nfm->nfm_mark);
11665294c1fSJeff Layton 	}
11765294c1fSJeff Layton }
11865294c1fSJeff Layton 
11965294c1fSJeff Layton static struct nfsd_file_mark *
12065294c1fSJeff Layton nfsd_file_mark_find_or_create(struct nfsd_file *nf)
12165294c1fSJeff Layton {
12265294c1fSJeff Layton 	int			err;
12365294c1fSJeff Layton 	struct fsnotify_mark	*mark;
12465294c1fSJeff Layton 	struct nfsd_file_mark	*nfm = NULL, *new;
12565294c1fSJeff Layton 	struct inode *inode = nf->nf_inode;
12665294c1fSJeff Layton 
12765294c1fSJeff Layton 	do {
12865294c1fSJeff Layton 		mutex_lock(&nfsd_file_fsnotify_group->mark_mutex);
12965294c1fSJeff Layton 		mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
13065294c1fSJeff Layton 				nfsd_file_fsnotify_group);
13165294c1fSJeff Layton 		if (mark) {
13265294c1fSJeff Layton 			nfm = nfsd_file_mark_get(container_of(mark,
13365294c1fSJeff Layton 						 struct nfsd_file_mark,
13465294c1fSJeff Layton 						 nfm_mark));
13565294c1fSJeff Layton 			mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
13665294c1fSJeff Layton 			fsnotify_put_mark(mark);
13765294c1fSJeff Layton 			if (likely(nfm))
13865294c1fSJeff Layton 				break;
13965294c1fSJeff Layton 		} else
14065294c1fSJeff Layton 			mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
14165294c1fSJeff Layton 
14265294c1fSJeff Layton 		/* allocate a new nfm */
14365294c1fSJeff Layton 		new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
14465294c1fSJeff Layton 		if (!new)
14565294c1fSJeff Layton 			return NULL;
14665294c1fSJeff Layton 		fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
14765294c1fSJeff Layton 		new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
14865294c1fSJeff Layton 		atomic_set(&new->nfm_ref, 1);
14965294c1fSJeff Layton 
15065294c1fSJeff Layton 		err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
15165294c1fSJeff Layton 
15265294c1fSJeff Layton 		/*
15365294c1fSJeff Layton 		 * If the add was successful, then return the object.
15465294c1fSJeff Layton 		 * Otherwise, we need to put the reference we hold on the
15565294c1fSJeff Layton 		 * nfm_mark. The fsnotify code will take a reference and put
15665294c1fSJeff Layton 		 * it on failure, so we can't just free it directly. It's also
15765294c1fSJeff Layton 		 * not safe to call fsnotify_destroy_mark on it as the
15865294c1fSJeff Layton 		 * mark->group will be NULL. Thus, we can't let the nfm_ref
15965294c1fSJeff Layton 		 * counter drive the destruction at this point.
16065294c1fSJeff Layton 		 */
16165294c1fSJeff Layton 		if (likely(!err))
16265294c1fSJeff Layton 			nfm = new;
16365294c1fSJeff Layton 		else
16465294c1fSJeff Layton 			fsnotify_put_mark(&new->nfm_mark);
16565294c1fSJeff Layton 	} while (unlikely(err == -EEXIST));
16665294c1fSJeff Layton 
16765294c1fSJeff Layton 	return nfm;
16865294c1fSJeff Layton }
16965294c1fSJeff Layton 
17065294c1fSJeff Layton static struct nfsd_file *
1715e113224STrond Myklebust nfsd_file_alloc(struct inode *inode, unsigned int may, unsigned int hashval,
1725e113224STrond Myklebust 		struct net *net)
17365294c1fSJeff Layton {
17465294c1fSJeff Layton 	struct nfsd_file *nf;
17565294c1fSJeff Layton 
17665294c1fSJeff Layton 	nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
17765294c1fSJeff Layton 	if (nf) {
17865294c1fSJeff Layton 		INIT_HLIST_NODE(&nf->nf_node);
17965294c1fSJeff Layton 		INIT_LIST_HEAD(&nf->nf_lru);
18065294c1fSJeff Layton 		nf->nf_file = NULL;
18165294c1fSJeff Layton 		nf->nf_cred = get_current_cred();
1825e113224STrond Myklebust 		nf->nf_net = net;
18365294c1fSJeff Layton 		nf->nf_flags = 0;
18465294c1fSJeff Layton 		nf->nf_inode = inode;
18565294c1fSJeff Layton 		nf->nf_hashval = hashval;
18665294c1fSJeff Layton 		atomic_set(&nf->nf_ref, 1);
18765294c1fSJeff Layton 		nf->nf_may = may & NFSD_FILE_MAY_MASK;
18865294c1fSJeff Layton 		if (may & NFSD_MAY_NOT_BREAK_LEASE) {
18965294c1fSJeff Layton 			if (may & NFSD_MAY_WRITE)
19065294c1fSJeff Layton 				__set_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags);
19165294c1fSJeff Layton 			if (may & NFSD_MAY_READ)
19265294c1fSJeff Layton 				__set_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
19365294c1fSJeff Layton 		}
19465294c1fSJeff Layton 		nf->nf_mark = NULL;
19565294c1fSJeff Layton 		trace_nfsd_file_alloc(nf);
19665294c1fSJeff Layton 	}
19765294c1fSJeff Layton 	return nf;
19865294c1fSJeff Layton }
19965294c1fSJeff Layton 
20065294c1fSJeff Layton static bool
20165294c1fSJeff Layton nfsd_file_free(struct nfsd_file *nf)
20265294c1fSJeff Layton {
20365294c1fSJeff Layton 	bool flush = false;
20465294c1fSJeff Layton 
20565294c1fSJeff Layton 	trace_nfsd_file_put_final(nf);
20665294c1fSJeff Layton 	if (nf->nf_mark)
20765294c1fSJeff Layton 		nfsd_file_mark_put(nf->nf_mark);
20865294c1fSJeff Layton 	if (nf->nf_file) {
20965294c1fSJeff Layton 		get_file(nf->nf_file);
21065294c1fSJeff Layton 		filp_close(nf->nf_file, NULL);
21165294c1fSJeff Layton 		fput(nf->nf_file);
21265294c1fSJeff Layton 		flush = true;
21365294c1fSJeff Layton 	}
21465294c1fSJeff Layton 	call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
21565294c1fSJeff Layton 	return flush;
21665294c1fSJeff Layton }
21765294c1fSJeff Layton 
218055b24a8STrond Myklebust static bool
219055b24a8STrond Myklebust nfsd_file_check_writeback(struct nfsd_file *nf)
220055b24a8STrond Myklebust {
221055b24a8STrond Myklebust 	struct file *file = nf->nf_file;
222055b24a8STrond Myklebust 	struct address_space *mapping;
223055b24a8STrond Myklebust 
224055b24a8STrond Myklebust 	if (!file || !(file->f_mode & FMODE_WRITE))
225055b24a8STrond Myklebust 		return false;
226055b24a8STrond Myklebust 	mapping = file->f_mapping;
227055b24a8STrond Myklebust 	return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
228055b24a8STrond Myklebust 		mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
229055b24a8STrond Myklebust }
230055b24a8STrond Myklebust 
231055b24a8STrond Myklebust static int
232055b24a8STrond Myklebust nfsd_file_check_write_error(struct nfsd_file *nf)
233055b24a8STrond Myklebust {
234055b24a8STrond Myklebust 	struct file *file = nf->nf_file;
235055b24a8STrond Myklebust 
236055b24a8STrond Myklebust 	if (!file || !(file->f_mode & FMODE_WRITE))
237055b24a8STrond Myklebust 		return 0;
238055b24a8STrond Myklebust 	return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
239055b24a8STrond Myklebust }
240055b24a8STrond Myklebust 
241055b24a8STrond Myklebust static bool
242055b24a8STrond Myklebust nfsd_file_in_use(struct nfsd_file *nf)
243055b24a8STrond Myklebust {
244055b24a8STrond Myklebust 	return nfsd_file_check_writeback(nf) ||
245055b24a8STrond Myklebust 			nfsd_file_check_write_error(nf);
246055b24a8STrond Myklebust }
247055b24a8STrond Myklebust 
24865294c1fSJeff Layton static void
24965294c1fSJeff Layton nfsd_file_do_unhash(struct nfsd_file *nf)
25065294c1fSJeff Layton {
25165294c1fSJeff Layton 	lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
25265294c1fSJeff Layton 
25365294c1fSJeff Layton 	trace_nfsd_file_unhash(nf);
25465294c1fSJeff Layton 
255055b24a8STrond Myklebust 	if (nfsd_file_check_write_error(nf))
256055b24a8STrond Myklebust 		nfsd_reset_boot_verifier(net_generic(nf->nf_net, nfsd_net_id));
25765294c1fSJeff Layton 	--nfsd_file_hashtbl[nf->nf_hashval].nfb_count;
25865294c1fSJeff Layton 	hlist_del_rcu(&nf->nf_node);
25965294c1fSJeff Layton 	atomic_long_dec(&nfsd_filecache_count);
26065294c1fSJeff Layton }
26165294c1fSJeff Layton 
26265294c1fSJeff Layton static bool
26365294c1fSJeff Layton nfsd_file_unhash(struct nfsd_file *nf)
26465294c1fSJeff Layton {
26565294c1fSJeff Layton 	if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
26665294c1fSJeff Layton 		nfsd_file_do_unhash(nf);
267*36ebbdb9STrond Myklebust 		if (!list_empty(&nf->nf_lru))
268*36ebbdb9STrond Myklebust 			list_lru_del(&nfsd_file_lru, &nf->nf_lru);
26965294c1fSJeff Layton 		return true;
27065294c1fSJeff Layton 	}
27165294c1fSJeff Layton 	return false;
27265294c1fSJeff Layton }
27365294c1fSJeff Layton 
27465294c1fSJeff Layton /*
27565294c1fSJeff Layton  * Return true if the file was unhashed.
27665294c1fSJeff Layton  */
27765294c1fSJeff Layton static bool
27865294c1fSJeff Layton nfsd_file_unhash_and_release_locked(struct nfsd_file *nf, struct list_head *dispose)
27965294c1fSJeff Layton {
28065294c1fSJeff Layton 	lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
28165294c1fSJeff Layton 
28265294c1fSJeff Layton 	trace_nfsd_file_unhash_and_release_locked(nf);
28365294c1fSJeff Layton 	if (!nfsd_file_unhash(nf))
28465294c1fSJeff Layton 		return false;
28565294c1fSJeff Layton 	/* keep final reference for nfsd_file_lru_dispose */
28665294c1fSJeff Layton 	if (atomic_add_unless(&nf->nf_ref, -1, 1))
28765294c1fSJeff Layton 		return true;
28865294c1fSJeff Layton 
28965294c1fSJeff Layton 	list_add(&nf->nf_lru, dispose);
29065294c1fSJeff Layton 	return true;
29165294c1fSJeff Layton }
29265294c1fSJeff Layton 
29365294c1fSJeff Layton static int
29465294c1fSJeff Layton nfsd_file_put_noref(struct nfsd_file *nf)
29565294c1fSJeff Layton {
29665294c1fSJeff Layton 	int count;
29765294c1fSJeff Layton 	trace_nfsd_file_put(nf);
29865294c1fSJeff Layton 
29965294c1fSJeff Layton 	count = atomic_dec_return(&nf->nf_ref);
30065294c1fSJeff Layton 	if (!count) {
30165294c1fSJeff Layton 		WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
30265294c1fSJeff Layton 		nfsd_file_free(nf);
30365294c1fSJeff Layton 	}
30465294c1fSJeff Layton 	return count;
30565294c1fSJeff Layton }
30665294c1fSJeff Layton 
30765294c1fSJeff Layton void
30865294c1fSJeff Layton nfsd_file_put(struct nfsd_file *nf)
30965294c1fSJeff Layton {
31065294c1fSJeff Layton 	bool is_hashed = test_bit(NFSD_FILE_HASHED, &nf->nf_flags) != 0;
311055b24a8STrond Myklebust 	bool unused = !nfsd_file_in_use(nf);
31265294c1fSJeff Layton 
31365294c1fSJeff Layton 	set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
314055b24a8STrond Myklebust 	if (nfsd_file_put_noref(nf) == 1 && is_hashed && unused)
31565294c1fSJeff Layton 		nfsd_file_schedule_laundrette(NFSD_FILE_LAUNDRETTE_MAY_FLUSH);
31665294c1fSJeff Layton }
31765294c1fSJeff Layton 
31865294c1fSJeff Layton struct nfsd_file *
31965294c1fSJeff Layton nfsd_file_get(struct nfsd_file *nf)
32065294c1fSJeff Layton {
32165294c1fSJeff Layton 	if (likely(atomic_inc_not_zero(&nf->nf_ref)))
32265294c1fSJeff Layton 		return nf;
32365294c1fSJeff Layton 	return NULL;
32465294c1fSJeff Layton }
32565294c1fSJeff Layton 
32665294c1fSJeff Layton static void
32765294c1fSJeff Layton nfsd_file_dispose_list(struct list_head *dispose)
32865294c1fSJeff Layton {
32965294c1fSJeff Layton 	struct nfsd_file *nf;
33065294c1fSJeff Layton 
33165294c1fSJeff Layton 	while(!list_empty(dispose)) {
33265294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
33365294c1fSJeff Layton 		list_del(&nf->nf_lru);
33465294c1fSJeff Layton 		nfsd_file_put_noref(nf);
33565294c1fSJeff Layton 	}
33665294c1fSJeff Layton }
33765294c1fSJeff Layton 
33865294c1fSJeff Layton static void
33965294c1fSJeff Layton nfsd_file_dispose_list_sync(struct list_head *dispose)
34065294c1fSJeff Layton {
34165294c1fSJeff Layton 	bool flush = false;
34265294c1fSJeff Layton 	struct nfsd_file *nf;
34365294c1fSJeff Layton 
34465294c1fSJeff Layton 	while(!list_empty(dispose)) {
34565294c1fSJeff Layton 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
34665294c1fSJeff Layton 		list_del(&nf->nf_lru);
34765294c1fSJeff Layton 		if (!atomic_dec_and_test(&nf->nf_ref))
34865294c1fSJeff Layton 			continue;
34965294c1fSJeff Layton 		if (nfsd_file_free(nf))
35065294c1fSJeff Layton 			flush = true;
35165294c1fSJeff Layton 	}
35265294c1fSJeff Layton 	if (flush)
35365294c1fSJeff Layton 		flush_delayed_fput();
35465294c1fSJeff Layton }
35565294c1fSJeff Layton 
35665294c1fSJeff Layton /*
35765294c1fSJeff Layton  * Note this can deadlock with nfsd_file_cache_purge.
35865294c1fSJeff Layton  */
35965294c1fSJeff Layton static enum lru_status
36065294c1fSJeff Layton nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
36165294c1fSJeff Layton 		 spinlock_t *lock, void *arg)
36265294c1fSJeff Layton 	__releases(lock)
36365294c1fSJeff Layton 	__acquires(lock)
36465294c1fSJeff Layton {
36565294c1fSJeff Layton 	struct list_head *head = arg;
36665294c1fSJeff Layton 	struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
36765294c1fSJeff Layton 
36865294c1fSJeff Layton 	/*
36965294c1fSJeff Layton 	 * Do a lockless refcount check. The hashtable holds one reference, so
37065294c1fSJeff Layton 	 * we look to see if anything else has a reference, or if any have
37165294c1fSJeff Layton 	 * been put since the shrinker last ran. Those don't get unhashed and
37265294c1fSJeff Layton 	 * released.
37365294c1fSJeff Layton 	 *
37465294c1fSJeff Layton 	 * Note that in the put path, we set the flag and then decrement the
37565294c1fSJeff Layton 	 * counter. Here we check the counter and then test and clear the flag.
37665294c1fSJeff Layton 	 * That order is deliberate to ensure that we can do this locklessly.
37765294c1fSJeff Layton 	 */
37865294c1fSJeff Layton 	if (atomic_read(&nf->nf_ref) > 1)
37965294c1fSJeff Layton 		goto out_skip;
380055b24a8STrond Myklebust 
381055b24a8STrond Myklebust 	/*
382055b24a8STrond Myklebust 	 * Don't throw out files that are still undergoing I/O or
383055b24a8STrond Myklebust 	 * that have uncleared errors pending.
384055b24a8STrond Myklebust 	 */
385055b24a8STrond Myklebust 	if (nfsd_file_check_writeback(nf))
386055b24a8STrond Myklebust 		goto out_skip;
387055b24a8STrond Myklebust 
38865294c1fSJeff Layton 	if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags))
38965294c1fSJeff Layton 		goto out_rescan;
39065294c1fSJeff Layton 
39165294c1fSJeff Layton 	if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags))
39265294c1fSJeff Layton 		goto out_skip;
39365294c1fSJeff Layton 
39465294c1fSJeff Layton 	list_lru_isolate_move(lru, &nf->nf_lru, head);
39565294c1fSJeff Layton 	return LRU_REMOVED;
39665294c1fSJeff Layton out_rescan:
39765294c1fSJeff Layton 	set_bit(NFSD_FILE_LRU_RESCAN, &nfsd_file_lru_flags);
39865294c1fSJeff Layton out_skip:
39965294c1fSJeff Layton 	return LRU_SKIP;
40065294c1fSJeff Layton }
40165294c1fSJeff Layton 
40265294c1fSJeff Layton static void
40365294c1fSJeff Layton nfsd_file_lru_dispose(struct list_head *head)
40465294c1fSJeff Layton {
405*36ebbdb9STrond Myklebust 	struct nfsd_file *nf;
406*36ebbdb9STrond Myklebust 
407*36ebbdb9STrond Myklebust 	list_for_each_entry(nf, head, nf_lru) {
40865294c1fSJeff Layton 		spin_lock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
40965294c1fSJeff Layton 		nfsd_file_do_unhash(nf);
41065294c1fSJeff Layton 		spin_unlock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
41165294c1fSJeff Layton 	}
412*36ebbdb9STrond Myklebust 	nfsd_file_dispose_list(head);
41365294c1fSJeff Layton }
41465294c1fSJeff Layton 
41565294c1fSJeff Layton static unsigned long
41665294c1fSJeff Layton nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
41765294c1fSJeff Layton {
41865294c1fSJeff Layton 	return list_lru_count(&nfsd_file_lru);
41965294c1fSJeff Layton }
42065294c1fSJeff Layton 
42165294c1fSJeff Layton static unsigned long
42265294c1fSJeff Layton nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
42365294c1fSJeff Layton {
42465294c1fSJeff Layton 	LIST_HEAD(head);
42565294c1fSJeff Layton 	unsigned long ret;
42665294c1fSJeff Layton 
42765294c1fSJeff Layton 	ret = list_lru_shrink_walk(&nfsd_file_lru, sc, nfsd_file_lru_cb, &head);
42865294c1fSJeff Layton 	nfsd_file_lru_dispose(&head);
42965294c1fSJeff Layton 	return ret;
43065294c1fSJeff Layton }
43165294c1fSJeff Layton 
43265294c1fSJeff Layton static struct shrinker	nfsd_file_shrinker = {
43365294c1fSJeff Layton 	.scan_objects = nfsd_file_lru_scan,
43465294c1fSJeff Layton 	.count_objects = nfsd_file_lru_count,
43565294c1fSJeff Layton 	.seeks = 1,
43665294c1fSJeff Layton };
43765294c1fSJeff Layton 
43865294c1fSJeff Layton static void
43965294c1fSJeff Layton __nfsd_file_close_inode(struct inode *inode, unsigned int hashval,
44065294c1fSJeff Layton 			struct list_head *dispose)
44165294c1fSJeff Layton {
44265294c1fSJeff Layton 	struct nfsd_file	*nf;
44365294c1fSJeff Layton 	struct hlist_node	*tmp;
44465294c1fSJeff Layton 
44565294c1fSJeff Layton 	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
44665294c1fSJeff Layton 	hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) {
44765294c1fSJeff Layton 		if (inode == nf->nf_inode)
44865294c1fSJeff Layton 			nfsd_file_unhash_and_release_locked(nf, dispose);
44965294c1fSJeff Layton 	}
45065294c1fSJeff Layton 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
45165294c1fSJeff Layton }
45265294c1fSJeff Layton 
45365294c1fSJeff Layton /**
45465294c1fSJeff Layton  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
45565294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
45665294c1fSJeff Layton  *
45765294c1fSJeff Layton  * Walk the whole hash bucket, looking for any files that correspond to "inode".
45865294c1fSJeff Layton  * If any do, then unhash them and put the hashtable reference to them and
45965294c1fSJeff Layton  * destroy any that had their last reference put. Also ensure that any of the
46065294c1fSJeff Layton  * fputs also have their final __fput done as well.
46165294c1fSJeff Layton  */
46265294c1fSJeff Layton void
46365294c1fSJeff Layton nfsd_file_close_inode_sync(struct inode *inode)
46465294c1fSJeff Layton {
46565294c1fSJeff Layton 	unsigned int		hashval = (unsigned int)hash_long(inode->i_ino,
46665294c1fSJeff Layton 						NFSD_FILE_HASH_BITS);
46765294c1fSJeff Layton 	LIST_HEAD(dispose);
46865294c1fSJeff Layton 
46965294c1fSJeff Layton 	__nfsd_file_close_inode(inode, hashval, &dispose);
47065294c1fSJeff Layton 	trace_nfsd_file_close_inode_sync(inode, hashval, !list_empty(&dispose));
47165294c1fSJeff Layton 	nfsd_file_dispose_list_sync(&dispose);
47265294c1fSJeff Layton }
47365294c1fSJeff Layton 
47465294c1fSJeff Layton /**
47565294c1fSJeff Layton  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
47665294c1fSJeff Layton  * @inode: inode of the file to attempt to remove
47765294c1fSJeff Layton  *
47865294c1fSJeff Layton  * Walk the whole hash bucket, looking for any files that correspond to "inode".
47965294c1fSJeff Layton  * If any do, then unhash them and put the hashtable reference to them and
48065294c1fSJeff Layton  * destroy any that had their last reference put.
48165294c1fSJeff Layton  */
48265294c1fSJeff Layton static void
48365294c1fSJeff Layton nfsd_file_close_inode(struct inode *inode)
48465294c1fSJeff Layton {
48565294c1fSJeff Layton 	unsigned int		hashval = (unsigned int)hash_long(inode->i_ino,
48665294c1fSJeff Layton 						NFSD_FILE_HASH_BITS);
48765294c1fSJeff Layton 	LIST_HEAD(dispose);
48865294c1fSJeff Layton 
48965294c1fSJeff Layton 	__nfsd_file_close_inode(inode, hashval, &dispose);
49065294c1fSJeff Layton 	trace_nfsd_file_close_inode(inode, hashval, !list_empty(&dispose));
49165294c1fSJeff Layton 	nfsd_file_dispose_list(&dispose);
49265294c1fSJeff Layton }
49365294c1fSJeff Layton 
49465294c1fSJeff Layton /**
49565294c1fSJeff Layton  * nfsd_file_delayed_close - close unused nfsd_files
49665294c1fSJeff Layton  * @work: dummy
49765294c1fSJeff Layton  *
49865294c1fSJeff Layton  * Walk the LRU list and close any entries that have not been used since
49965294c1fSJeff Layton  * the last scan.
50065294c1fSJeff Layton  *
50165294c1fSJeff Layton  * Note this can deadlock with nfsd_file_cache_purge.
50265294c1fSJeff Layton  */
50365294c1fSJeff Layton static void
50465294c1fSJeff Layton nfsd_file_delayed_close(struct work_struct *work)
50565294c1fSJeff Layton {
50665294c1fSJeff Layton 	LIST_HEAD(head);
50765294c1fSJeff Layton 
50865294c1fSJeff Layton 	list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb, &head, LONG_MAX);
50965294c1fSJeff Layton 
51065294c1fSJeff Layton 	if (test_and_clear_bit(NFSD_FILE_LRU_RESCAN, &nfsd_file_lru_flags))
51165294c1fSJeff Layton 		nfsd_file_schedule_laundrette(NFSD_FILE_LAUNDRETTE_NOFLUSH);
51265294c1fSJeff Layton 
51365294c1fSJeff Layton 	if (!list_empty(&head)) {
51465294c1fSJeff Layton 		nfsd_file_lru_dispose(&head);
51565294c1fSJeff Layton 		flush_delayed_fput();
51665294c1fSJeff Layton 	}
51765294c1fSJeff Layton }
51865294c1fSJeff Layton 
51965294c1fSJeff Layton static int
52065294c1fSJeff Layton nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
52165294c1fSJeff Layton 			    void *data)
52265294c1fSJeff Layton {
52365294c1fSJeff Layton 	struct file_lock *fl = data;
52465294c1fSJeff Layton 
52565294c1fSJeff Layton 	/* Only close files for F_SETLEASE leases */
52665294c1fSJeff Layton 	if (fl->fl_flags & FL_LEASE)
52765294c1fSJeff Layton 		nfsd_file_close_inode_sync(file_inode(fl->fl_file));
52865294c1fSJeff Layton 	return 0;
52965294c1fSJeff Layton }
53065294c1fSJeff Layton 
53165294c1fSJeff Layton static struct notifier_block nfsd_file_lease_notifier = {
53265294c1fSJeff Layton 	.notifier_call = nfsd_file_lease_notifier_call,
53365294c1fSJeff Layton };
53465294c1fSJeff Layton 
53565294c1fSJeff Layton static int
53665294c1fSJeff Layton nfsd_file_fsnotify_handle_event(struct fsnotify_group *group,
53765294c1fSJeff Layton 				struct inode *inode,
53865294c1fSJeff Layton 				u32 mask, const void *data, int data_type,
53965294c1fSJeff Layton 				const struct qstr *file_name, u32 cookie,
54065294c1fSJeff Layton 				struct fsnotify_iter_info *iter_info)
54165294c1fSJeff Layton {
54265294c1fSJeff Layton 	trace_nfsd_file_fsnotify_handle_event(inode, mask);
54365294c1fSJeff Layton 
54465294c1fSJeff Layton 	/* Should be no marks on non-regular files */
54565294c1fSJeff Layton 	if (!S_ISREG(inode->i_mode)) {
54665294c1fSJeff Layton 		WARN_ON_ONCE(1);
54765294c1fSJeff Layton 		return 0;
54865294c1fSJeff Layton 	}
54965294c1fSJeff Layton 
55065294c1fSJeff Layton 	/* don't close files if this was not the last link */
55165294c1fSJeff Layton 	if (mask & FS_ATTRIB) {
55265294c1fSJeff Layton 		if (inode->i_nlink)
55365294c1fSJeff Layton 			return 0;
55465294c1fSJeff Layton 	}
55565294c1fSJeff Layton 
55665294c1fSJeff Layton 	nfsd_file_close_inode(inode);
55765294c1fSJeff Layton 	return 0;
55865294c1fSJeff Layton }
55965294c1fSJeff Layton 
56065294c1fSJeff Layton 
56165294c1fSJeff Layton static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
56265294c1fSJeff Layton 	.handle_event = nfsd_file_fsnotify_handle_event,
56365294c1fSJeff Layton 	.free_mark = nfsd_file_mark_free,
56465294c1fSJeff Layton };
56565294c1fSJeff Layton 
56665294c1fSJeff Layton int
56765294c1fSJeff Layton nfsd_file_cache_init(void)
56865294c1fSJeff Layton {
56965294c1fSJeff Layton 	int		ret = -ENOMEM;
57065294c1fSJeff Layton 	unsigned int	i;
57165294c1fSJeff Layton 
57265294c1fSJeff Layton 	clear_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
57365294c1fSJeff Layton 
57465294c1fSJeff Layton 	if (nfsd_file_hashtbl)
57565294c1fSJeff Layton 		return 0;
57665294c1fSJeff Layton 
57765294c1fSJeff Layton 	nfsd_file_hashtbl = kcalloc(NFSD_FILE_HASH_SIZE,
57865294c1fSJeff Layton 				sizeof(*nfsd_file_hashtbl), GFP_KERNEL);
57965294c1fSJeff Layton 	if (!nfsd_file_hashtbl) {
58065294c1fSJeff Layton 		pr_err("nfsd: unable to allocate nfsd_file_hashtbl\n");
58165294c1fSJeff Layton 		goto out_err;
58265294c1fSJeff Layton 	}
58365294c1fSJeff Layton 
58465294c1fSJeff Layton 	nfsd_file_slab = kmem_cache_create("nfsd_file",
58565294c1fSJeff Layton 				sizeof(struct nfsd_file), 0, 0, NULL);
58665294c1fSJeff Layton 	if (!nfsd_file_slab) {
58765294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_slab\n");
58865294c1fSJeff Layton 		goto out_err;
58965294c1fSJeff Layton 	}
59065294c1fSJeff Layton 
59165294c1fSJeff Layton 	nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
59265294c1fSJeff Layton 					sizeof(struct nfsd_file_mark), 0, 0, NULL);
59365294c1fSJeff Layton 	if (!nfsd_file_mark_slab) {
59465294c1fSJeff Layton 		pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
59565294c1fSJeff Layton 		goto out_err;
59665294c1fSJeff Layton 	}
59765294c1fSJeff Layton 
59865294c1fSJeff Layton 
59965294c1fSJeff Layton 	ret = list_lru_init(&nfsd_file_lru);
60065294c1fSJeff Layton 	if (ret) {
60165294c1fSJeff Layton 		pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
60265294c1fSJeff Layton 		goto out_err;
60365294c1fSJeff Layton 	}
60465294c1fSJeff Layton 
60565294c1fSJeff Layton 	ret = register_shrinker(&nfsd_file_shrinker);
60665294c1fSJeff Layton 	if (ret) {
60765294c1fSJeff Layton 		pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
60865294c1fSJeff Layton 		goto out_lru;
60965294c1fSJeff Layton 	}
61065294c1fSJeff Layton 
61165294c1fSJeff Layton 	ret = lease_register_notifier(&nfsd_file_lease_notifier);
61265294c1fSJeff Layton 	if (ret) {
61365294c1fSJeff Layton 		pr_err("nfsd: unable to register lease notifier: %d\n", ret);
61465294c1fSJeff Layton 		goto out_shrinker;
61565294c1fSJeff Layton 	}
61665294c1fSJeff Layton 
61765294c1fSJeff Layton 	nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops);
61865294c1fSJeff Layton 	if (IS_ERR(nfsd_file_fsnotify_group)) {
61965294c1fSJeff Layton 		pr_err("nfsd: unable to create fsnotify group: %ld\n",
62065294c1fSJeff Layton 			PTR_ERR(nfsd_file_fsnotify_group));
62165294c1fSJeff Layton 		nfsd_file_fsnotify_group = NULL;
62265294c1fSJeff Layton 		goto out_notifier;
62365294c1fSJeff Layton 	}
62465294c1fSJeff Layton 
62565294c1fSJeff Layton 	for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
62665294c1fSJeff Layton 		INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head);
62765294c1fSJeff Layton 		spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock);
62865294c1fSJeff Layton 	}
62965294c1fSJeff Layton 
63065294c1fSJeff Layton 	INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_delayed_close);
63165294c1fSJeff Layton out:
63265294c1fSJeff Layton 	return ret;
63365294c1fSJeff Layton out_notifier:
63465294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
63565294c1fSJeff Layton out_shrinker:
63665294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
63765294c1fSJeff Layton out_lru:
63865294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
63965294c1fSJeff Layton out_err:
64065294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
64165294c1fSJeff Layton 	nfsd_file_slab = NULL;
64265294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
64365294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
64465294c1fSJeff Layton 	kfree(nfsd_file_hashtbl);
64565294c1fSJeff Layton 	nfsd_file_hashtbl = NULL;
64665294c1fSJeff Layton 	goto out;
64765294c1fSJeff Layton }
64865294c1fSJeff Layton 
64965294c1fSJeff Layton /*
65065294c1fSJeff Layton  * Note this can deadlock with nfsd_file_lru_cb.
65165294c1fSJeff Layton  */
65265294c1fSJeff Layton void
6535e113224STrond Myklebust nfsd_file_cache_purge(struct net *net)
65465294c1fSJeff Layton {
65565294c1fSJeff Layton 	unsigned int		i;
65665294c1fSJeff Layton 	struct nfsd_file	*nf;
6575e113224STrond Myklebust 	struct hlist_node	*next;
65865294c1fSJeff Layton 	LIST_HEAD(dispose);
65965294c1fSJeff Layton 	bool del;
66065294c1fSJeff Layton 
66165294c1fSJeff Layton 	if (!nfsd_file_hashtbl)
66265294c1fSJeff Layton 		return;
66365294c1fSJeff Layton 
66465294c1fSJeff Layton 	for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
6655e113224STrond Myklebust 		struct nfsd_fcache_bucket *nfb = &nfsd_file_hashtbl[i];
6665e113224STrond Myklebust 
6675e113224STrond Myklebust 		spin_lock(&nfb->nfb_lock);
6685e113224STrond Myklebust 		hlist_for_each_entry_safe(nf, next, &nfb->nfb_head, nf_node) {
6695e113224STrond Myklebust 			if (net && nf->nf_net != net)
6705e113224STrond Myklebust 				continue;
67165294c1fSJeff Layton 			del = nfsd_file_unhash_and_release_locked(nf, &dispose);
67265294c1fSJeff Layton 
67365294c1fSJeff Layton 			/*
67465294c1fSJeff Layton 			 * Deadlock detected! Something marked this entry as
67565294c1fSJeff Layton 			 * unhased, but hasn't removed it from the hash list.
67665294c1fSJeff Layton 			 */
67765294c1fSJeff Layton 			WARN_ON_ONCE(!del);
67865294c1fSJeff Layton 		}
6795e113224STrond Myklebust 		spin_unlock(&nfb->nfb_lock);
68065294c1fSJeff Layton 		nfsd_file_dispose_list(&dispose);
68165294c1fSJeff Layton 	}
68265294c1fSJeff Layton }
68365294c1fSJeff Layton 
68465294c1fSJeff Layton void
68565294c1fSJeff Layton nfsd_file_cache_shutdown(void)
68665294c1fSJeff Layton {
68765294c1fSJeff Layton 	set_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
68865294c1fSJeff Layton 
68965294c1fSJeff Layton 	lease_unregister_notifier(&nfsd_file_lease_notifier);
69065294c1fSJeff Layton 	unregister_shrinker(&nfsd_file_shrinker);
69165294c1fSJeff Layton 	/*
69265294c1fSJeff Layton 	 * make sure all callers of nfsd_file_lru_cb are done before
69365294c1fSJeff Layton 	 * calling nfsd_file_cache_purge
69465294c1fSJeff Layton 	 */
69565294c1fSJeff Layton 	cancel_delayed_work_sync(&nfsd_filecache_laundrette);
6965e113224STrond Myklebust 	nfsd_file_cache_purge(NULL);
69765294c1fSJeff Layton 	list_lru_destroy(&nfsd_file_lru);
69865294c1fSJeff Layton 	rcu_barrier();
69965294c1fSJeff Layton 	fsnotify_put_group(nfsd_file_fsnotify_group);
70065294c1fSJeff Layton 	nfsd_file_fsnotify_group = NULL;
70165294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_slab);
70265294c1fSJeff Layton 	nfsd_file_slab = NULL;
70365294c1fSJeff Layton 	fsnotify_wait_marks_destroyed();
70465294c1fSJeff Layton 	kmem_cache_destroy(nfsd_file_mark_slab);
70565294c1fSJeff Layton 	nfsd_file_mark_slab = NULL;
70665294c1fSJeff Layton 	kfree(nfsd_file_hashtbl);
70765294c1fSJeff Layton 	nfsd_file_hashtbl = NULL;
70865294c1fSJeff Layton }
70965294c1fSJeff Layton 
71065294c1fSJeff Layton static bool
71165294c1fSJeff Layton nfsd_match_cred(const struct cred *c1, const struct cred *c2)
71265294c1fSJeff Layton {
71365294c1fSJeff Layton 	int i;
71465294c1fSJeff Layton 
71565294c1fSJeff Layton 	if (!uid_eq(c1->fsuid, c2->fsuid))
71665294c1fSJeff Layton 		return false;
71765294c1fSJeff Layton 	if (!gid_eq(c1->fsgid, c2->fsgid))
71865294c1fSJeff Layton 		return false;
71965294c1fSJeff Layton 	if (c1->group_info == NULL || c2->group_info == NULL)
72065294c1fSJeff Layton 		return c1->group_info == c2->group_info;
72165294c1fSJeff Layton 	if (c1->group_info->ngroups != c2->group_info->ngroups)
72265294c1fSJeff Layton 		return false;
72365294c1fSJeff Layton 	for (i = 0; i < c1->group_info->ngroups; i++) {
72465294c1fSJeff Layton 		if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
72565294c1fSJeff Layton 			return false;
72665294c1fSJeff Layton 	}
72765294c1fSJeff Layton 	return true;
72865294c1fSJeff Layton }
72965294c1fSJeff Layton 
73065294c1fSJeff Layton static struct nfsd_file *
73165294c1fSJeff Layton nfsd_file_find_locked(struct inode *inode, unsigned int may_flags,
7325e113224STrond Myklebust 			unsigned int hashval, struct net *net)
73365294c1fSJeff Layton {
73465294c1fSJeff Layton 	struct nfsd_file *nf;
73565294c1fSJeff Layton 	unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
73665294c1fSJeff Layton 
73765294c1fSJeff Layton 	hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
73865294c1fSJeff Layton 				 nf_node) {
73965294c1fSJeff Layton 		if ((need & nf->nf_may) != need)
74065294c1fSJeff Layton 			continue;
74165294c1fSJeff Layton 		if (nf->nf_inode != inode)
74265294c1fSJeff Layton 			continue;
7435e113224STrond Myklebust 		if (nf->nf_net != net)
7445e113224STrond Myklebust 			continue;
74565294c1fSJeff Layton 		if (!nfsd_match_cred(nf->nf_cred, current_cred()))
74665294c1fSJeff Layton 			continue;
74765294c1fSJeff Layton 		if (nfsd_file_get(nf) != NULL)
74865294c1fSJeff Layton 			return nf;
74965294c1fSJeff Layton 	}
75065294c1fSJeff Layton 	return NULL;
75165294c1fSJeff Layton }
75265294c1fSJeff Layton 
75365294c1fSJeff Layton /**
75465294c1fSJeff Layton  * nfsd_file_is_cached - are there any cached open files for this fh?
75565294c1fSJeff Layton  * @inode: inode of the file to check
75665294c1fSJeff Layton  *
75765294c1fSJeff Layton  * Scan the hashtable for open files that match this fh. Returns true if there
75865294c1fSJeff Layton  * are any, and false if not.
75965294c1fSJeff Layton  */
76065294c1fSJeff Layton bool
76165294c1fSJeff Layton nfsd_file_is_cached(struct inode *inode)
76265294c1fSJeff Layton {
76365294c1fSJeff Layton 	bool			ret = false;
76465294c1fSJeff Layton 	struct nfsd_file	*nf;
76565294c1fSJeff Layton 	unsigned int		hashval;
76665294c1fSJeff Layton 
76765294c1fSJeff Layton         hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
76865294c1fSJeff Layton 
76965294c1fSJeff Layton 	rcu_read_lock();
77065294c1fSJeff Layton 	hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
77165294c1fSJeff Layton 				 nf_node) {
77265294c1fSJeff Layton 		if (inode == nf->nf_inode) {
77365294c1fSJeff Layton 			ret = true;
77465294c1fSJeff Layton 			break;
77565294c1fSJeff Layton 		}
77665294c1fSJeff Layton 	}
77765294c1fSJeff Layton 	rcu_read_unlock();
77865294c1fSJeff Layton 	trace_nfsd_file_is_cached(inode, hashval, (int)ret);
77965294c1fSJeff Layton 	return ret;
78065294c1fSJeff Layton }
78165294c1fSJeff Layton 
78265294c1fSJeff Layton __be32
78365294c1fSJeff Layton nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
78465294c1fSJeff Layton 		  unsigned int may_flags, struct nfsd_file **pnf)
78565294c1fSJeff Layton {
78665294c1fSJeff Layton 	__be32	status;
7875e113224STrond Myklebust 	struct net *net = SVC_NET(rqstp);
78865294c1fSJeff Layton 	struct nfsd_file *nf, *new;
78965294c1fSJeff Layton 	struct inode *inode;
79065294c1fSJeff Layton 	unsigned int hashval;
79128c7d86bSTrond Myklebust 	bool retry = true;
79265294c1fSJeff Layton 
79365294c1fSJeff Layton 	/* FIXME: skip this if fh_dentry is already set? */
79465294c1fSJeff Layton 	status = fh_verify(rqstp, fhp, S_IFREG,
79565294c1fSJeff Layton 				may_flags|NFSD_MAY_OWNER_OVERRIDE);
79665294c1fSJeff Layton 	if (status != nfs_ok)
79765294c1fSJeff Layton 		return status;
79865294c1fSJeff Layton 
79965294c1fSJeff Layton 	inode = d_inode(fhp->fh_dentry);
80065294c1fSJeff Layton 	hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
80165294c1fSJeff Layton retry:
80265294c1fSJeff Layton 	rcu_read_lock();
8035e113224STrond Myklebust 	nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
80465294c1fSJeff Layton 	rcu_read_unlock();
80565294c1fSJeff Layton 	if (nf)
80665294c1fSJeff Layton 		goto wait_for_construction;
80765294c1fSJeff Layton 
8085e113224STrond Myklebust 	new = nfsd_file_alloc(inode, may_flags, hashval, net);
80965294c1fSJeff Layton 	if (!new) {
81065294c1fSJeff Layton 		trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags,
81165294c1fSJeff Layton 					NULL, nfserr_jukebox);
81265294c1fSJeff Layton 		return nfserr_jukebox;
81365294c1fSJeff Layton 	}
81465294c1fSJeff Layton 
81565294c1fSJeff Layton 	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
8165e113224STrond Myklebust 	nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
81765294c1fSJeff Layton 	if (nf == NULL)
81865294c1fSJeff Layton 		goto open_file;
81965294c1fSJeff Layton 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
82065294c1fSJeff Layton 	nfsd_file_slab_free(&new->nf_rcu);
82165294c1fSJeff Layton 
82265294c1fSJeff Layton wait_for_construction:
82365294c1fSJeff Layton 	wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
82465294c1fSJeff Layton 
82565294c1fSJeff Layton 	/* Did construction of this file fail? */
82665294c1fSJeff Layton 	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
82728c7d86bSTrond Myklebust 		if (!retry) {
82828c7d86bSTrond Myklebust 			status = nfserr_jukebox;
82928c7d86bSTrond Myklebust 			goto out;
83028c7d86bSTrond Myklebust 		}
83128c7d86bSTrond Myklebust 		retry = false;
83265294c1fSJeff Layton 		nfsd_file_put_noref(nf);
83365294c1fSJeff Layton 		goto retry;
83465294c1fSJeff Layton 	}
83565294c1fSJeff Layton 
83665294c1fSJeff Layton 	this_cpu_inc(nfsd_file_cache_hits);
83765294c1fSJeff Layton 
83865294c1fSJeff Layton 	if (!(may_flags & NFSD_MAY_NOT_BREAK_LEASE)) {
83965294c1fSJeff Layton 		bool write = (may_flags & NFSD_MAY_WRITE);
84065294c1fSJeff Layton 
84165294c1fSJeff Layton 		if (test_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags) ||
84265294c1fSJeff Layton 		    (test_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags) && write)) {
84365294c1fSJeff Layton 			status = nfserrno(nfsd_open_break_lease(
84465294c1fSJeff Layton 					file_inode(nf->nf_file), may_flags));
84565294c1fSJeff Layton 			if (status == nfs_ok) {
84665294c1fSJeff Layton 				clear_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
84765294c1fSJeff Layton 				if (write)
84865294c1fSJeff Layton 					clear_bit(NFSD_FILE_BREAK_WRITE,
84965294c1fSJeff Layton 						  &nf->nf_flags);
85065294c1fSJeff Layton 			}
85165294c1fSJeff Layton 		}
85265294c1fSJeff Layton 	}
85365294c1fSJeff Layton out:
85465294c1fSJeff Layton 	if (status == nfs_ok) {
85565294c1fSJeff Layton 		*pnf = nf;
85665294c1fSJeff Layton 	} else {
85765294c1fSJeff Layton 		nfsd_file_put(nf);
85865294c1fSJeff Layton 		nf = NULL;
85965294c1fSJeff Layton 	}
86065294c1fSJeff Layton 
86165294c1fSJeff Layton 	trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, nf, status);
86265294c1fSJeff Layton 	return status;
86365294c1fSJeff Layton open_file:
86465294c1fSJeff Layton 	nf = new;
86565294c1fSJeff Layton 	/* Take reference for the hashtable */
86665294c1fSJeff Layton 	atomic_inc(&nf->nf_ref);
86765294c1fSJeff Layton 	__set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
86865294c1fSJeff Layton 	__set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
86965294c1fSJeff Layton 	list_lru_add(&nfsd_file_lru, &nf->nf_lru);
87065294c1fSJeff Layton 	hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head);
87165294c1fSJeff Layton 	++nfsd_file_hashtbl[hashval].nfb_count;
87265294c1fSJeff Layton 	nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount,
87365294c1fSJeff Layton 			nfsd_file_hashtbl[hashval].nfb_count);
87465294c1fSJeff Layton 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
87565294c1fSJeff Layton 	atomic_long_inc(&nfsd_filecache_count);
87665294c1fSJeff Layton 
87765294c1fSJeff Layton 	nf->nf_mark = nfsd_file_mark_find_or_create(nf);
87865294c1fSJeff Layton 	if (nf->nf_mark)
87965294c1fSJeff Layton 		status = nfsd_open_verified(rqstp, fhp, S_IFREG,
88065294c1fSJeff Layton 				may_flags, &nf->nf_file);
88165294c1fSJeff Layton 	else
88265294c1fSJeff Layton 		status = nfserr_jukebox;
88365294c1fSJeff Layton 	/*
88465294c1fSJeff Layton 	 * If construction failed, or we raced with a call to unlink()
88565294c1fSJeff Layton 	 * then unhash.
88665294c1fSJeff Layton 	 */
88765294c1fSJeff Layton 	if (status != nfs_ok || inode->i_nlink == 0) {
88865294c1fSJeff Layton 		bool do_free;
88965294c1fSJeff Layton 		spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
89065294c1fSJeff Layton 		do_free = nfsd_file_unhash(nf);
89165294c1fSJeff Layton 		spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
89265294c1fSJeff Layton 		if (do_free)
89365294c1fSJeff Layton 			nfsd_file_put_noref(nf);
89465294c1fSJeff Layton 	}
89565294c1fSJeff Layton 	clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
89665294c1fSJeff Layton 	smp_mb__after_atomic();
89765294c1fSJeff Layton 	wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
89865294c1fSJeff Layton 	goto out;
89965294c1fSJeff Layton }
90065294c1fSJeff Layton 
90165294c1fSJeff Layton /*
90265294c1fSJeff Layton  * Note that fields may be added, removed or reordered in the future. Programs
90365294c1fSJeff Layton  * scraping this file for info should test the labels to ensure they're
90465294c1fSJeff Layton  * getting the correct field.
90565294c1fSJeff Layton  */
90665294c1fSJeff Layton static int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
90765294c1fSJeff Layton {
90865294c1fSJeff Layton 	unsigned int i, count = 0, longest = 0;
90965294c1fSJeff Layton 	unsigned long hits = 0;
91065294c1fSJeff Layton 
91165294c1fSJeff Layton 	/*
91265294c1fSJeff Layton 	 * No need for spinlocks here since we're not terribly interested in
91365294c1fSJeff Layton 	 * accuracy. We do take the nfsd_mutex simply to ensure that we
91465294c1fSJeff Layton 	 * don't end up racing with server shutdown
91565294c1fSJeff Layton 	 */
91665294c1fSJeff Layton 	mutex_lock(&nfsd_mutex);
91765294c1fSJeff Layton 	if (nfsd_file_hashtbl) {
91865294c1fSJeff Layton 		for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
91965294c1fSJeff Layton 			count += nfsd_file_hashtbl[i].nfb_count;
92065294c1fSJeff Layton 			longest = max(longest, nfsd_file_hashtbl[i].nfb_count);
92165294c1fSJeff Layton 		}
92265294c1fSJeff Layton 	}
92365294c1fSJeff Layton 	mutex_unlock(&nfsd_mutex);
92465294c1fSJeff Layton 
92565294c1fSJeff Layton 	for_each_possible_cpu(i)
92665294c1fSJeff Layton 		hits += per_cpu(nfsd_file_cache_hits, i);
92765294c1fSJeff Layton 
92865294c1fSJeff Layton 	seq_printf(m, "total entries: %u\n", count);
92965294c1fSJeff Layton 	seq_printf(m, "longest chain: %u\n", longest);
93065294c1fSJeff Layton 	seq_printf(m, "cache hits:    %lu\n", hits);
93165294c1fSJeff Layton 	return 0;
93265294c1fSJeff Layton }
93365294c1fSJeff Layton 
93465294c1fSJeff Layton int nfsd_file_cache_stats_open(struct inode *inode, struct file *file)
93565294c1fSJeff Layton {
93665294c1fSJeff Layton 	return single_open(file, nfsd_file_cache_stats_show, NULL);
93765294c1fSJeff Layton }
938