1 /*
2  * Open file cache.
3  *
4  * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com>
5  */
6 
7 #include <linux/hash.h>
8 #include <linux/slab.h>
9 #include <linux/file.h>
10 #include <linux/pagemap.h>
11 #include <linux/sched.h>
12 #include <linux/list_lru.h>
13 #include <linux/fsnotify_backend.h>
14 #include <linux/fsnotify.h>
15 #include <linux/seq_file.h>
16 #include <linux/rhashtable.h>
17 
18 #include "vfs.h"
19 #include "nfsd.h"
20 #include "nfsfh.h"
21 #include "netns.h"
22 #include "filecache.h"
23 #include "trace.h"
24 
25 #define NFSD_LAUNDRETTE_DELAY		     (2 * HZ)
26 
27 #define NFSD_FILE_CACHE_UP		     (0)
28 
29 /* We only care about NFSD_MAY_READ/WRITE for this cache */
30 #define NFSD_FILE_MAY_MASK	(NFSD_MAY_READ|NFSD_MAY_WRITE)
31 
32 static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
33 static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions);
34 static DEFINE_PER_CPU(unsigned long, nfsd_file_releases);
35 static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age);
36 static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions);
37 
38 struct nfsd_fcache_disposal {
39 	struct work_struct work;
40 	spinlock_t lock;
41 	struct list_head freeme;
42 };
43 
44 static struct workqueue_struct *nfsd_filecache_wq __read_mostly;
45 
46 static struct kmem_cache		*nfsd_file_slab;
47 static struct kmem_cache		*nfsd_file_mark_slab;
48 static struct list_lru			nfsd_file_lru;
49 static unsigned long			nfsd_file_flags;
50 static struct fsnotify_group		*nfsd_file_fsnotify_group;
51 static struct delayed_work		nfsd_filecache_laundrette;
52 static struct rhashtable		nfsd_file_rhash_tbl
53 						____cacheline_aligned_in_smp;
54 
55 enum nfsd_file_lookup_type {
56 	NFSD_FILE_KEY_INODE,
57 	NFSD_FILE_KEY_FULL,
58 };
59 
60 struct nfsd_file_lookup_key {
61 	struct inode			*inode;
62 	struct net			*net;
63 	const struct cred		*cred;
64 	unsigned char			need;
65 	bool				gc;
66 	enum nfsd_file_lookup_type	type;
67 };
68 
69 /*
70  * The returned hash value is based solely on the address of an in-code
71  * inode, a pointer to a slab-allocated object. The entropy in such a
72  * pointer is concentrated in its middle bits.
73  */
nfsd_file_inode_hash(const struct inode * inode,u32 seed)74 static u32 nfsd_file_inode_hash(const struct inode *inode, u32 seed)
75 {
76 	unsigned long ptr = (unsigned long)inode;
77 	u32 k;
78 
79 	k = ptr >> L1_CACHE_SHIFT;
80 	k &= 0x00ffffff;
81 	return jhash2(&k, 1, seed);
82 }
83 
84 /**
85  * nfsd_file_key_hashfn - Compute the hash value of a lookup key
86  * @data: key on which to compute the hash value
87  * @len: rhash table's key_len parameter (unused)
88  * @seed: rhash table's random seed of the day
89  *
90  * Return value:
91  *   Computed 32-bit hash value
92  */
nfsd_file_key_hashfn(const void * data,u32 len,u32 seed)93 static u32 nfsd_file_key_hashfn(const void *data, u32 len, u32 seed)
94 {
95 	const struct nfsd_file_lookup_key *key = data;
96 
97 	return nfsd_file_inode_hash(key->inode, seed);
98 }
99 
100 /**
101  * nfsd_file_obj_hashfn - Compute the hash value of an nfsd_file
102  * @data: object on which to compute the hash value
103  * @len: rhash table's key_len parameter (unused)
104  * @seed: rhash table's random seed of the day
105  *
106  * Return value:
107  *   Computed 32-bit hash value
108  */
nfsd_file_obj_hashfn(const void * data,u32 len,u32 seed)109 static u32 nfsd_file_obj_hashfn(const void *data, u32 len, u32 seed)
110 {
111 	const struct nfsd_file *nf = data;
112 
113 	return nfsd_file_inode_hash(nf->nf_inode, seed);
114 }
115 
116 static bool
nfsd_match_cred(const struct cred * c1,const struct cred * c2)117 nfsd_match_cred(const struct cred *c1, const struct cred *c2)
118 {
119 	int i;
120 
121 	if (!uid_eq(c1->fsuid, c2->fsuid))
122 		return false;
123 	if (!gid_eq(c1->fsgid, c2->fsgid))
124 		return false;
125 	if (c1->group_info == NULL || c2->group_info == NULL)
126 		return c1->group_info == c2->group_info;
127 	if (c1->group_info->ngroups != c2->group_info->ngroups)
128 		return false;
129 	for (i = 0; i < c1->group_info->ngroups; i++) {
130 		if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
131 			return false;
132 	}
133 	return true;
134 }
135 
136 /**
137  * nfsd_file_obj_cmpfn - Match a cache item against search criteria
138  * @arg: search criteria
139  * @ptr: cache item to check
140  *
141  * Return values:
142  *   %0 - Item matches search criteria
143  *   %1 - Item does not match search criteria
144  */
nfsd_file_obj_cmpfn(struct rhashtable_compare_arg * arg,const void * ptr)145 static int nfsd_file_obj_cmpfn(struct rhashtable_compare_arg *arg,
146 			       const void *ptr)
147 {
148 	const struct nfsd_file_lookup_key *key = arg->key;
149 	const struct nfsd_file *nf = ptr;
150 
151 	switch (key->type) {
152 	case NFSD_FILE_KEY_INODE:
153 		if (nf->nf_inode != key->inode)
154 			return 1;
155 		break;
156 	case NFSD_FILE_KEY_FULL:
157 		if (nf->nf_inode != key->inode)
158 			return 1;
159 		if (nf->nf_may != key->need)
160 			return 1;
161 		if (nf->nf_net != key->net)
162 			return 1;
163 		if (!nfsd_match_cred(nf->nf_cred, key->cred))
164 			return 1;
165 		if (!!test_bit(NFSD_FILE_GC, &nf->nf_flags) != key->gc)
166 			return 1;
167 		if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0)
168 			return 1;
169 		break;
170 	}
171 	return 0;
172 }
173 
174 static const struct rhashtable_params nfsd_file_rhash_params = {
175 	.key_len		= sizeof_field(struct nfsd_file, nf_inode),
176 	.key_offset		= offsetof(struct nfsd_file, nf_inode),
177 	.head_offset		= offsetof(struct nfsd_file, nf_rhash),
178 	.hashfn			= nfsd_file_key_hashfn,
179 	.obj_hashfn		= nfsd_file_obj_hashfn,
180 	.obj_cmpfn		= nfsd_file_obj_cmpfn,
181 	/* Reduce resizing churn on light workloads */
182 	.min_size		= 512,		/* buckets */
183 	.automatic_shrinking	= true,
184 };
185 
186 static void
nfsd_file_schedule_laundrette(void)187 nfsd_file_schedule_laundrette(void)
188 {
189 	if ((atomic_read(&nfsd_file_rhash_tbl.nelems) == 0) ||
190 	    test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
191 		return;
192 
193 	queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
194 			NFSD_LAUNDRETTE_DELAY);
195 }
196 
197 static void
nfsd_file_slab_free(struct rcu_head * rcu)198 nfsd_file_slab_free(struct rcu_head *rcu)
199 {
200 	struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
201 
202 	put_cred(nf->nf_cred);
203 	kmem_cache_free(nfsd_file_slab, nf);
204 }
205 
206 static void
nfsd_file_mark_free(struct fsnotify_mark * mark)207 nfsd_file_mark_free(struct fsnotify_mark *mark)
208 {
209 	struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
210 						  nfm_mark);
211 
212 	kmem_cache_free(nfsd_file_mark_slab, nfm);
213 }
214 
215 static struct nfsd_file_mark *
nfsd_file_mark_get(struct nfsd_file_mark * nfm)216 nfsd_file_mark_get(struct nfsd_file_mark *nfm)
217 {
218 	if (!refcount_inc_not_zero(&nfm->nfm_ref))
219 		return NULL;
220 	return nfm;
221 }
222 
223 static void
nfsd_file_mark_put(struct nfsd_file_mark * nfm)224 nfsd_file_mark_put(struct nfsd_file_mark *nfm)
225 {
226 	if (refcount_dec_and_test(&nfm->nfm_ref)) {
227 		fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
228 		fsnotify_put_mark(&nfm->nfm_mark);
229 	}
230 }
231 
232 static struct nfsd_file_mark *
nfsd_file_mark_find_or_create(struct nfsd_file * nf,struct inode * inode)233 nfsd_file_mark_find_or_create(struct nfsd_file *nf, struct inode *inode)
234 {
235 	int			err;
236 	struct fsnotify_mark	*mark;
237 	struct nfsd_file_mark	*nfm = NULL, *new;
238 
239 	do {
240 		fsnotify_group_lock(nfsd_file_fsnotify_group);
241 		mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
242 					  nfsd_file_fsnotify_group);
243 		if (mark) {
244 			nfm = nfsd_file_mark_get(container_of(mark,
245 						 struct nfsd_file_mark,
246 						 nfm_mark));
247 			fsnotify_group_unlock(nfsd_file_fsnotify_group);
248 			if (nfm) {
249 				fsnotify_put_mark(mark);
250 				break;
251 			}
252 			/* Avoid soft lockup race with nfsd_file_mark_put() */
253 			fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
254 			fsnotify_put_mark(mark);
255 		} else {
256 			fsnotify_group_unlock(nfsd_file_fsnotify_group);
257 		}
258 
259 		/* allocate a new nfm */
260 		new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
261 		if (!new)
262 			return NULL;
263 		fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
264 		new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
265 		refcount_set(&new->nfm_ref, 1);
266 
267 		err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
268 
269 		/*
270 		 * If the add was successful, then return the object.
271 		 * Otherwise, we need to put the reference we hold on the
272 		 * nfm_mark. The fsnotify code will take a reference and put
273 		 * it on failure, so we can't just free it directly. It's also
274 		 * not safe to call fsnotify_destroy_mark on it as the
275 		 * mark->group will be NULL. Thus, we can't let the nfm_ref
276 		 * counter drive the destruction at this point.
277 		 */
278 		if (likely(!err))
279 			nfm = new;
280 		else
281 			fsnotify_put_mark(&new->nfm_mark);
282 	} while (unlikely(err == -EEXIST));
283 
284 	return nfm;
285 }
286 
287 static struct nfsd_file *
nfsd_file_alloc(struct nfsd_file_lookup_key * key,unsigned int may)288 nfsd_file_alloc(struct nfsd_file_lookup_key *key, unsigned int may)
289 {
290 	struct nfsd_file *nf;
291 
292 	nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
293 	if (nf) {
294 		INIT_LIST_HEAD(&nf->nf_lru);
295 		nf->nf_birthtime = ktime_get();
296 		nf->nf_file = NULL;
297 		nf->nf_cred = get_current_cred();
298 		nf->nf_net = key->net;
299 		nf->nf_flags = 0;
300 		__set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
301 		__set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
302 		if (key->gc)
303 			__set_bit(NFSD_FILE_GC, &nf->nf_flags);
304 		nf->nf_inode = key->inode;
305 		refcount_set(&nf->nf_ref, 1);
306 		nf->nf_may = key->need;
307 		nf->nf_mark = NULL;
308 	}
309 	return nf;
310 }
311 
312 static void
nfsd_file_fsync(struct nfsd_file * nf)313 nfsd_file_fsync(struct nfsd_file *nf)
314 {
315 	struct file *file = nf->nf_file;
316 	int ret;
317 
318 	if (!file || !(file->f_mode & FMODE_WRITE))
319 		return;
320 	ret = vfs_fsync(file, 1);
321 	trace_nfsd_file_fsync(nf, ret);
322 	if (ret)
323 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
324 }
325 
326 static int
nfsd_file_check_write_error(struct nfsd_file * nf)327 nfsd_file_check_write_error(struct nfsd_file *nf)
328 {
329 	struct file *file = nf->nf_file;
330 
331 	if (!file || !(file->f_mode & FMODE_WRITE))
332 		return 0;
333 	return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
334 }
335 
336 static void
nfsd_file_hash_remove(struct nfsd_file * nf)337 nfsd_file_hash_remove(struct nfsd_file *nf)
338 {
339 	trace_nfsd_file_unhash(nf);
340 
341 	if (nfsd_file_check_write_error(nf))
342 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
343 	rhashtable_remove_fast(&nfsd_file_rhash_tbl, &nf->nf_rhash,
344 			       nfsd_file_rhash_params);
345 }
346 
347 static bool
nfsd_file_unhash(struct nfsd_file * nf)348 nfsd_file_unhash(struct nfsd_file *nf)
349 {
350 	if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
351 		nfsd_file_hash_remove(nf);
352 		return true;
353 	}
354 	return false;
355 }
356 
357 static void
nfsd_file_free(struct nfsd_file * nf)358 nfsd_file_free(struct nfsd_file *nf)
359 {
360 	s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime));
361 
362 	trace_nfsd_file_free(nf);
363 
364 	this_cpu_inc(nfsd_file_releases);
365 	this_cpu_add(nfsd_file_total_age, age);
366 
367 	nfsd_file_unhash(nf);
368 
369 	/*
370 	 * We call fsync here in order to catch writeback errors. It's not
371 	 * strictly required by the protocol, but an nfsd_file could get
372 	 * evicted from the cache before a COMMIT comes in. If another
373 	 * task were to open that file in the interim and scrape the error,
374 	 * then the client may never see it. By calling fsync here, we ensure
375 	 * that writeback happens before the entry is freed, and that any
376 	 * errors reported result in the write verifier changing.
377 	 */
378 	nfsd_file_fsync(nf);
379 
380 	if (nf->nf_mark)
381 		nfsd_file_mark_put(nf->nf_mark);
382 	if (nf->nf_file) {
383 		get_file(nf->nf_file);
384 		filp_close(nf->nf_file, NULL);
385 		fput(nf->nf_file);
386 	}
387 
388 	/*
389 	 * If this item is still linked via nf_lru, that's a bug.
390 	 * WARN and leak it to preserve system stability.
391 	 */
392 	if (WARN_ON_ONCE(!list_empty(&nf->nf_lru)))
393 		return;
394 
395 	call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
396 }
397 
398 static bool
nfsd_file_check_writeback(struct nfsd_file * nf)399 nfsd_file_check_writeback(struct nfsd_file *nf)
400 {
401 	struct file *file = nf->nf_file;
402 	struct address_space *mapping;
403 
404 	if (!file || !(file->f_mode & FMODE_WRITE))
405 		return false;
406 	mapping = file->f_mapping;
407 	return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
408 		mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
409 }
410 
nfsd_file_lru_add(struct nfsd_file * nf)411 static bool nfsd_file_lru_add(struct nfsd_file *nf)
412 {
413 	set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
414 	if (list_lru_add(&nfsd_file_lru, &nf->nf_lru)) {
415 		trace_nfsd_file_lru_add(nf);
416 		return true;
417 	}
418 	return false;
419 }
420 
nfsd_file_lru_remove(struct nfsd_file * nf)421 static bool nfsd_file_lru_remove(struct nfsd_file *nf)
422 {
423 	if (list_lru_del(&nfsd_file_lru, &nf->nf_lru)) {
424 		trace_nfsd_file_lru_del(nf);
425 		return true;
426 	}
427 	return false;
428 }
429 
430 struct nfsd_file *
nfsd_file_get(struct nfsd_file * nf)431 nfsd_file_get(struct nfsd_file *nf)
432 {
433 	if (likely(refcount_inc_not_zero(&nf->nf_ref)))
434 		return nf;
435 	return NULL;
436 }
437 
438 /**
439  * nfsd_file_put - put the reference to a nfsd_file
440  * @nf: nfsd_file of which to put the reference
441  *
442  * Put a reference to a nfsd_file. In the non-GC case, we just put the
443  * reference immediately. In the GC case, if the reference would be
444  * the last one, the put it on the LRU instead to be cleaned up later.
445  */
446 void
nfsd_file_put(struct nfsd_file * nf)447 nfsd_file_put(struct nfsd_file *nf)
448 {
449 	might_sleep();
450 	trace_nfsd_file_put(nf);
451 
452 	if (test_bit(NFSD_FILE_GC, &nf->nf_flags) &&
453 	    test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
454 		/*
455 		 * If this is the last reference (nf_ref == 1), then try to
456 		 * transfer it to the LRU.
457 		 */
458 		if (refcount_dec_not_one(&nf->nf_ref))
459 			return;
460 
461 		/* Try to add it to the LRU.  If that fails, decrement. */
462 		if (nfsd_file_lru_add(nf)) {
463 			/* If it's still hashed, we're done */
464 			if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
465 				nfsd_file_schedule_laundrette();
466 				return;
467 			}
468 
469 			/*
470 			 * We're racing with unhashing, so try to remove it from
471 			 * the LRU. If removal fails, then someone else already
472 			 * has our reference.
473 			 */
474 			if (!nfsd_file_lru_remove(nf))
475 				return;
476 		}
477 	}
478 	if (refcount_dec_and_test(&nf->nf_ref))
479 		nfsd_file_free(nf);
480 }
481 
482 static void
nfsd_file_dispose_list(struct list_head * dispose)483 nfsd_file_dispose_list(struct list_head *dispose)
484 {
485 	struct nfsd_file *nf;
486 
487 	while (!list_empty(dispose)) {
488 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
489 		list_del_init(&nf->nf_lru);
490 		nfsd_file_free(nf);
491 	}
492 }
493 
494 static void
nfsd_file_list_remove_disposal(struct list_head * dst,struct nfsd_fcache_disposal * l)495 nfsd_file_list_remove_disposal(struct list_head *dst,
496 		struct nfsd_fcache_disposal *l)
497 {
498 	spin_lock(&l->lock);
499 	list_splice_init(&l->freeme, dst);
500 	spin_unlock(&l->lock);
501 }
502 
503 static void
nfsd_file_list_add_disposal(struct list_head * files,struct net * net)504 nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
505 {
506 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
507 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
508 
509 	spin_lock(&l->lock);
510 	list_splice_tail_init(files, &l->freeme);
511 	spin_unlock(&l->lock);
512 	queue_work(nfsd_filecache_wq, &l->work);
513 }
514 
515 static void
nfsd_file_list_add_pernet(struct list_head * dst,struct list_head * src,struct net * net)516 nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
517 		struct net *net)
518 {
519 	struct nfsd_file *nf, *tmp;
520 
521 	list_for_each_entry_safe(nf, tmp, src, nf_lru) {
522 		if (nf->nf_net == net)
523 			list_move_tail(&nf->nf_lru, dst);
524 	}
525 }
526 
527 static void
nfsd_file_dispose_list_delayed(struct list_head * dispose)528 nfsd_file_dispose_list_delayed(struct list_head *dispose)
529 {
530 	LIST_HEAD(list);
531 	struct nfsd_file *nf;
532 
533 	while(!list_empty(dispose)) {
534 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
535 		nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
536 		nfsd_file_list_add_disposal(&list, nf->nf_net);
537 	}
538 }
539 
540 /**
541  * nfsd_file_lru_cb - Examine an entry on the LRU list
542  * @item: LRU entry to examine
543  * @lru: controlling LRU
544  * @lock: LRU list lock (unused)
545  * @arg: dispose list
546  *
547  * Return values:
548  *   %LRU_REMOVED: @item was removed from the LRU
549  *   %LRU_ROTATE: @item is to be moved to the LRU tail
550  *   %LRU_SKIP: @item cannot be evicted
551  */
552 static enum lru_status
nfsd_file_lru_cb(struct list_head * item,struct list_lru_one * lru,spinlock_t * lock,void * arg)553 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
554 		 spinlock_t *lock, void *arg)
555 	__releases(lock)
556 	__acquires(lock)
557 {
558 	struct list_head *head = arg;
559 	struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
560 
561 	/* We should only be dealing with GC entries here */
562 	WARN_ON_ONCE(!test_bit(NFSD_FILE_GC, &nf->nf_flags));
563 
564 	/*
565 	 * Don't throw out files that are still undergoing I/O or
566 	 * that have uncleared errors pending.
567 	 */
568 	if (nfsd_file_check_writeback(nf)) {
569 		trace_nfsd_file_gc_writeback(nf);
570 		return LRU_SKIP;
571 	}
572 
573 	/* If it was recently added to the list, skip it */
574 	if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
575 		trace_nfsd_file_gc_referenced(nf);
576 		return LRU_ROTATE;
577 	}
578 
579 	/*
580 	 * Put the reference held on behalf of the LRU. If it wasn't the last
581 	 * one, then just remove it from the LRU and ignore it.
582 	 */
583 	if (!refcount_dec_and_test(&nf->nf_ref)) {
584 		trace_nfsd_file_gc_in_use(nf);
585 		list_lru_isolate(lru, &nf->nf_lru);
586 		return LRU_REMOVED;
587 	}
588 
589 	/* Refcount went to zero. Unhash it and queue it to the dispose list */
590 	nfsd_file_unhash(nf);
591 	list_lru_isolate_move(lru, &nf->nf_lru, head);
592 	this_cpu_inc(nfsd_file_evictions);
593 	trace_nfsd_file_gc_disposed(nf);
594 	return LRU_REMOVED;
595 }
596 
597 static void
nfsd_file_gc(void)598 nfsd_file_gc(void)
599 {
600 	LIST_HEAD(dispose);
601 	unsigned long ret;
602 
603 	ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
604 			    &dispose, list_lru_count(&nfsd_file_lru));
605 	trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
606 	nfsd_file_dispose_list_delayed(&dispose);
607 }
608 
609 static void
nfsd_file_gc_worker(struct work_struct * work)610 nfsd_file_gc_worker(struct work_struct *work)
611 {
612 	nfsd_file_gc();
613 	nfsd_file_schedule_laundrette();
614 }
615 
616 static unsigned long
nfsd_file_lru_count(struct shrinker * s,struct shrink_control * sc)617 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
618 {
619 	return list_lru_count(&nfsd_file_lru);
620 }
621 
622 static unsigned long
nfsd_file_lru_scan(struct shrinker * s,struct shrink_control * sc)623 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
624 {
625 	LIST_HEAD(dispose);
626 	unsigned long ret;
627 
628 	ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
629 				   nfsd_file_lru_cb, &dispose);
630 	trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
631 	nfsd_file_dispose_list_delayed(&dispose);
632 	return ret;
633 }
634 
635 static struct shrinker	nfsd_file_shrinker = {
636 	.scan_objects = nfsd_file_lru_scan,
637 	.count_objects = nfsd_file_lru_count,
638 	.seeks = 1,
639 };
640 
641 /**
642  * nfsd_file_cond_queue - conditionally unhash and queue a nfsd_file
643  * @nf: nfsd_file to attempt to queue
644  * @dispose: private list to queue successfully-put objects
645  *
646  * Unhash an nfsd_file, try to get a reference to it, and then put that
647  * reference. If it's the last reference, queue it to the dispose list.
648  */
649 static void
nfsd_file_cond_queue(struct nfsd_file * nf,struct list_head * dispose)650 nfsd_file_cond_queue(struct nfsd_file *nf, struct list_head *dispose)
651 	__must_hold(RCU)
652 {
653 	int decrement = 1;
654 
655 	/* If we raced with someone else unhashing, ignore it */
656 	if (!nfsd_file_unhash(nf))
657 		return;
658 
659 	/* If we can't get a reference, ignore it */
660 	if (!nfsd_file_get(nf))
661 		return;
662 
663 	/* Extra decrement if we remove from the LRU */
664 	if (nfsd_file_lru_remove(nf))
665 		++decrement;
666 
667 	/* If refcount goes to 0, then put on the dispose list */
668 	if (refcount_sub_and_test(decrement, &nf->nf_ref)) {
669 		list_add(&nf->nf_lru, dispose);
670 		trace_nfsd_file_closing(nf);
671 	}
672 }
673 
674 /**
675  * nfsd_file_queue_for_close: try to close out any open nfsd_files for an inode
676  * @inode:   inode on which to close out nfsd_files
677  * @dispose: list on which to gather nfsd_files to close out
678  *
679  * An nfsd_file represents a struct file being held open on behalf of nfsd. An
680  * open file however can block other activity (such as leases), or cause
681  * undesirable behavior (e.g. spurious silly-renames when reexporting NFS).
682  *
683  * This function is intended to find open nfsd_files when this sort of
684  * conflicting access occurs and then attempt to close those files out.
685  *
686  * Populates the dispose list with entries that have already had their
687  * refcounts go to zero. The actual free of an nfsd_file can be expensive,
688  * so we leave it up to the caller whether it wants to wait or not.
689  */
690 static void
nfsd_file_queue_for_close(struct inode * inode,struct list_head * dispose)691 nfsd_file_queue_for_close(struct inode *inode, struct list_head *dispose)
692 {
693 	struct nfsd_file_lookup_key key = {
694 		.type	= NFSD_FILE_KEY_INODE,
695 		.inode	= inode,
696 	};
697 	struct nfsd_file *nf;
698 
699 	rcu_read_lock();
700 	do {
701 		nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
702 				       nfsd_file_rhash_params);
703 		if (!nf)
704 			break;
705 		nfsd_file_cond_queue(nf, dispose);
706 	} while (1);
707 	rcu_read_unlock();
708 }
709 
710 /**
711  * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
712  * @inode: inode of the file to attempt to remove
713  *
714  * Close out any open nfsd_files that can be reaped for @inode. The
715  * actual freeing is deferred to the dispose_list_delayed infrastructure.
716  *
717  * This is used by the fsnotify callbacks and setlease notifier.
718  */
719 static void
nfsd_file_close_inode(struct inode * inode)720 nfsd_file_close_inode(struct inode *inode)
721 {
722 	LIST_HEAD(dispose);
723 
724 	nfsd_file_queue_for_close(inode, &dispose);
725 	nfsd_file_dispose_list_delayed(&dispose);
726 }
727 
728 /**
729  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
730  * @inode: inode of the file to attempt to remove
731  *
732  * Close out any open nfsd_files that can be reaped for @inode. The
733  * nfsd_files are closed out synchronously.
734  *
735  * This is called from nfsd_rename and nfsd_unlink to avoid silly-renames
736  * when reexporting NFS.
737  */
738 void
nfsd_file_close_inode_sync(struct inode * inode)739 nfsd_file_close_inode_sync(struct inode *inode)
740 {
741 	struct nfsd_file *nf;
742 	LIST_HEAD(dispose);
743 
744 	trace_nfsd_file_close(inode);
745 
746 	nfsd_file_queue_for_close(inode, &dispose);
747 	while (!list_empty(&dispose)) {
748 		nf = list_first_entry(&dispose, struct nfsd_file, nf_lru);
749 		list_del_init(&nf->nf_lru);
750 		nfsd_file_free(nf);
751 	}
752 	flush_delayed_fput();
753 }
754 
755 /**
756  * nfsd_file_delayed_close - close unused nfsd_files
757  * @work: dummy
758  *
759  * Walk the LRU list and destroy any entries that have not been used since
760  * the last scan.
761  */
762 static void
nfsd_file_delayed_close(struct work_struct * work)763 nfsd_file_delayed_close(struct work_struct *work)
764 {
765 	LIST_HEAD(head);
766 	struct nfsd_fcache_disposal *l = container_of(work,
767 			struct nfsd_fcache_disposal, work);
768 
769 	nfsd_file_list_remove_disposal(&head, l);
770 	nfsd_file_dispose_list(&head);
771 }
772 
773 static int
nfsd_file_lease_notifier_call(struct notifier_block * nb,unsigned long arg,void * data)774 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
775 			    void *data)
776 {
777 	struct file_lock *fl = data;
778 
779 	/* Only close files for F_SETLEASE leases */
780 	if (fl->fl_flags & FL_LEASE)
781 		nfsd_file_close_inode(file_inode(fl->fl_file));
782 	return 0;
783 }
784 
785 static struct notifier_block nfsd_file_lease_notifier = {
786 	.notifier_call = nfsd_file_lease_notifier_call,
787 };
788 
789 static int
nfsd_file_fsnotify_handle_event(struct fsnotify_mark * mark,u32 mask,struct inode * inode,struct inode * dir,const struct qstr * name,u32 cookie)790 nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
791 				struct inode *inode, struct inode *dir,
792 				const struct qstr *name, u32 cookie)
793 {
794 	if (WARN_ON_ONCE(!inode))
795 		return 0;
796 
797 	trace_nfsd_file_fsnotify_handle_event(inode, mask);
798 
799 	/* Should be no marks on non-regular files */
800 	if (!S_ISREG(inode->i_mode)) {
801 		WARN_ON_ONCE(1);
802 		return 0;
803 	}
804 
805 	/* don't close files if this was not the last link */
806 	if (mask & FS_ATTRIB) {
807 		if (inode->i_nlink)
808 			return 0;
809 	}
810 
811 	nfsd_file_close_inode(inode);
812 	return 0;
813 }
814 
815 
816 static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
817 	.handle_inode_event = nfsd_file_fsnotify_handle_event,
818 	.free_mark = nfsd_file_mark_free,
819 };
820 
821 int
nfsd_file_cache_init(void)822 nfsd_file_cache_init(void)
823 {
824 	int ret;
825 
826 	lockdep_assert_held(&nfsd_mutex);
827 	if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
828 		return 0;
829 
830 	ret = rhashtable_init(&nfsd_file_rhash_tbl, &nfsd_file_rhash_params);
831 	if (ret)
832 		return ret;
833 
834 	ret = -ENOMEM;
835 	nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
836 	if (!nfsd_filecache_wq)
837 		goto out;
838 
839 	nfsd_file_slab = kmem_cache_create("nfsd_file",
840 				sizeof(struct nfsd_file), 0, 0, NULL);
841 	if (!nfsd_file_slab) {
842 		pr_err("nfsd: unable to create nfsd_file_slab\n");
843 		goto out_err;
844 	}
845 
846 	nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
847 					sizeof(struct nfsd_file_mark), 0, 0, NULL);
848 	if (!nfsd_file_mark_slab) {
849 		pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
850 		goto out_err;
851 	}
852 
853 
854 	ret = list_lru_init(&nfsd_file_lru);
855 	if (ret) {
856 		pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
857 		goto out_err;
858 	}
859 
860 	ret = register_shrinker(&nfsd_file_shrinker, "nfsd-filecache");
861 	if (ret) {
862 		pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
863 		goto out_lru;
864 	}
865 
866 	ret = lease_register_notifier(&nfsd_file_lease_notifier);
867 	if (ret) {
868 		pr_err("nfsd: unable to register lease notifier: %d\n", ret);
869 		goto out_shrinker;
870 	}
871 
872 	nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
873 							FSNOTIFY_GROUP_NOFS);
874 	if (IS_ERR(nfsd_file_fsnotify_group)) {
875 		pr_err("nfsd: unable to create fsnotify group: %ld\n",
876 			PTR_ERR(nfsd_file_fsnotify_group));
877 		ret = PTR_ERR(nfsd_file_fsnotify_group);
878 		nfsd_file_fsnotify_group = NULL;
879 		goto out_notifier;
880 	}
881 
882 	INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
883 out:
884 	return ret;
885 out_notifier:
886 	lease_unregister_notifier(&nfsd_file_lease_notifier);
887 out_shrinker:
888 	unregister_shrinker(&nfsd_file_shrinker);
889 out_lru:
890 	list_lru_destroy(&nfsd_file_lru);
891 out_err:
892 	kmem_cache_destroy(nfsd_file_slab);
893 	nfsd_file_slab = NULL;
894 	kmem_cache_destroy(nfsd_file_mark_slab);
895 	nfsd_file_mark_slab = NULL;
896 	destroy_workqueue(nfsd_filecache_wq);
897 	nfsd_filecache_wq = NULL;
898 	rhashtable_destroy(&nfsd_file_rhash_tbl);
899 	goto out;
900 }
901 
902 /**
903  * __nfsd_file_cache_purge: clean out the cache for shutdown
904  * @net: net-namespace to shut down the cache (may be NULL)
905  *
906  * Walk the nfsd_file cache and close out any that match @net. If @net is NULL,
907  * then close out everything. Called when an nfsd instance is being shut down.
908  */
909 static void
__nfsd_file_cache_purge(struct net * net)910 __nfsd_file_cache_purge(struct net *net)
911 {
912 	struct rhashtable_iter iter;
913 	struct nfsd_file *nf;
914 	LIST_HEAD(dispose);
915 
916 	rhashtable_walk_enter(&nfsd_file_rhash_tbl, &iter);
917 	do {
918 		rhashtable_walk_start(&iter);
919 
920 		nf = rhashtable_walk_next(&iter);
921 		while (!IS_ERR_OR_NULL(nf)) {
922 			if (!net || nf->nf_net == net)
923 				nfsd_file_cond_queue(nf, &dispose);
924 			nf = rhashtable_walk_next(&iter);
925 		}
926 
927 		rhashtable_walk_stop(&iter);
928 	} while (nf == ERR_PTR(-EAGAIN));
929 	rhashtable_walk_exit(&iter);
930 
931 	nfsd_file_dispose_list(&dispose);
932 }
933 
934 static struct nfsd_fcache_disposal *
nfsd_alloc_fcache_disposal(void)935 nfsd_alloc_fcache_disposal(void)
936 {
937 	struct nfsd_fcache_disposal *l;
938 
939 	l = kmalloc(sizeof(*l), GFP_KERNEL);
940 	if (!l)
941 		return NULL;
942 	INIT_WORK(&l->work, nfsd_file_delayed_close);
943 	spin_lock_init(&l->lock);
944 	INIT_LIST_HEAD(&l->freeme);
945 	return l;
946 }
947 
948 static void
nfsd_free_fcache_disposal(struct nfsd_fcache_disposal * l)949 nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
950 {
951 	cancel_work_sync(&l->work);
952 	nfsd_file_dispose_list(&l->freeme);
953 	kfree(l);
954 }
955 
956 static void
nfsd_free_fcache_disposal_net(struct net * net)957 nfsd_free_fcache_disposal_net(struct net *net)
958 {
959 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
960 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
961 
962 	nfsd_free_fcache_disposal(l);
963 }
964 
965 int
nfsd_file_cache_start_net(struct net * net)966 nfsd_file_cache_start_net(struct net *net)
967 {
968 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
969 
970 	nn->fcache_disposal = nfsd_alloc_fcache_disposal();
971 	return nn->fcache_disposal ? 0 : -ENOMEM;
972 }
973 
974 /**
975  * nfsd_file_cache_purge - Remove all cache items associated with @net
976  * @net: target net namespace
977  *
978  */
979 void
nfsd_file_cache_purge(struct net * net)980 nfsd_file_cache_purge(struct net *net)
981 {
982 	lockdep_assert_held(&nfsd_mutex);
983 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
984 		__nfsd_file_cache_purge(net);
985 }
986 
987 void
nfsd_file_cache_shutdown_net(struct net * net)988 nfsd_file_cache_shutdown_net(struct net *net)
989 {
990 	nfsd_file_cache_purge(net);
991 	nfsd_free_fcache_disposal_net(net);
992 }
993 
994 void
nfsd_file_cache_shutdown(void)995 nfsd_file_cache_shutdown(void)
996 {
997 	int i;
998 
999 	lockdep_assert_held(&nfsd_mutex);
1000 	if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
1001 		return;
1002 
1003 	lease_unregister_notifier(&nfsd_file_lease_notifier);
1004 	unregister_shrinker(&nfsd_file_shrinker);
1005 	/*
1006 	 * make sure all callers of nfsd_file_lru_cb are done before
1007 	 * calling nfsd_file_cache_purge
1008 	 */
1009 	cancel_delayed_work_sync(&nfsd_filecache_laundrette);
1010 	__nfsd_file_cache_purge(NULL);
1011 	list_lru_destroy(&nfsd_file_lru);
1012 	rcu_barrier();
1013 	fsnotify_put_group(nfsd_file_fsnotify_group);
1014 	nfsd_file_fsnotify_group = NULL;
1015 	kmem_cache_destroy(nfsd_file_slab);
1016 	nfsd_file_slab = NULL;
1017 	fsnotify_wait_marks_destroyed();
1018 	kmem_cache_destroy(nfsd_file_mark_slab);
1019 	nfsd_file_mark_slab = NULL;
1020 	destroy_workqueue(nfsd_filecache_wq);
1021 	nfsd_filecache_wq = NULL;
1022 	rhashtable_destroy(&nfsd_file_rhash_tbl);
1023 
1024 	for_each_possible_cpu(i) {
1025 		per_cpu(nfsd_file_cache_hits, i) = 0;
1026 		per_cpu(nfsd_file_acquisitions, i) = 0;
1027 		per_cpu(nfsd_file_releases, i) = 0;
1028 		per_cpu(nfsd_file_total_age, i) = 0;
1029 		per_cpu(nfsd_file_evictions, i) = 0;
1030 	}
1031 }
1032 
1033 /**
1034  * nfsd_file_is_cached - are there any cached open files for this inode?
1035  * @inode: inode to check
1036  *
1037  * The lookup matches inodes in all net namespaces and is atomic wrt
1038  * nfsd_file_acquire().
1039  *
1040  * Return values:
1041  *   %true: filecache contains at least one file matching this inode
1042  *   %false: filecache contains no files matching this inode
1043  */
1044 bool
nfsd_file_is_cached(struct inode * inode)1045 nfsd_file_is_cached(struct inode *inode)
1046 {
1047 	struct nfsd_file_lookup_key key = {
1048 		.type	= NFSD_FILE_KEY_INODE,
1049 		.inode	= inode,
1050 	};
1051 	bool ret = false;
1052 
1053 	if (rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key,
1054 				   nfsd_file_rhash_params) != NULL)
1055 		ret = true;
1056 	trace_nfsd_file_is_cached(inode, (int)ret);
1057 	return ret;
1058 }
1059 
1060 static __be32
nfsd_file_do_acquire(struct svc_rqst * rqstp,struct svc_fh * fhp,unsigned int may_flags,struct file * file,struct nfsd_file ** pnf,bool want_gc)1061 nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1062 		     unsigned int may_flags, struct file *file,
1063 		     struct nfsd_file **pnf, bool want_gc)
1064 {
1065 	struct nfsd_file_lookup_key key = {
1066 		.type	= NFSD_FILE_KEY_FULL,
1067 		.need	= may_flags & NFSD_FILE_MAY_MASK,
1068 		.net	= SVC_NET(rqstp),
1069 		.gc	= want_gc,
1070 	};
1071 	bool open_retry = true;
1072 	struct nfsd_file *nf;
1073 	__be32 status;
1074 	int ret;
1075 
1076 	status = fh_verify(rqstp, fhp, S_IFREG,
1077 				may_flags|NFSD_MAY_OWNER_OVERRIDE);
1078 	if (status != nfs_ok)
1079 		return status;
1080 	key.inode = d_inode(fhp->fh_dentry);
1081 	key.cred = get_current_cred();
1082 
1083 retry:
1084 	rcu_read_lock();
1085 	nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
1086 			       nfsd_file_rhash_params);
1087 	if (nf)
1088 		nf = nfsd_file_get(nf);
1089 	rcu_read_unlock();
1090 
1091 	if (nf) {
1092 		if (nfsd_file_lru_remove(nf))
1093 			WARN_ON_ONCE(refcount_dec_and_test(&nf->nf_ref));
1094 		goto wait_for_construction;
1095 	}
1096 
1097 	nf = nfsd_file_alloc(&key, may_flags);
1098 	if (!nf) {
1099 		status = nfserr_jukebox;
1100 		goto out_status;
1101 	}
1102 
1103 	ret = rhashtable_lookup_insert_key(&nfsd_file_rhash_tbl,
1104 					   &key, &nf->nf_rhash,
1105 					   nfsd_file_rhash_params);
1106 	if (likely(ret == 0))
1107 		goto open_file;
1108 
1109 	nfsd_file_slab_free(&nf->nf_rcu);
1110 	nf = NULL;
1111 	if (ret == -EEXIST)
1112 		goto retry;
1113 	trace_nfsd_file_insert_err(rqstp, key.inode, may_flags, ret);
1114 	status = nfserr_jukebox;
1115 	goto out_status;
1116 
1117 wait_for_construction:
1118 	wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
1119 
1120 	/* Did construction of this file fail? */
1121 	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
1122 		trace_nfsd_file_cons_err(rqstp, key.inode, may_flags, nf);
1123 		if (!open_retry) {
1124 			status = nfserr_jukebox;
1125 			goto out;
1126 		}
1127 		open_retry = false;
1128 		if (refcount_dec_and_test(&nf->nf_ref))
1129 			nfsd_file_free(nf);
1130 		goto retry;
1131 	}
1132 
1133 	this_cpu_inc(nfsd_file_cache_hits);
1134 
1135 	status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
1136 out:
1137 	if (status == nfs_ok) {
1138 		this_cpu_inc(nfsd_file_acquisitions);
1139 		*pnf = nf;
1140 	} else {
1141 		if (refcount_dec_and_test(&nf->nf_ref))
1142 			nfsd_file_free(nf);
1143 		nf = NULL;
1144 	}
1145 
1146 out_status:
1147 	put_cred(key.cred);
1148 	trace_nfsd_file_acquire(rqstp, key.inode, may_flags, nf, status);
1149 	return status;
1150 
1151 open_file:
1152 	trace_nfsd_file_alloc(nf);
1153 	nf->nf_mark = nfsd_file_mark_find_or_create(nf, key.inode);
1154 	if (nf->nf_mark) {
1155 		if (file) {
1156 			get_file(file);
1157 			nf->nf_file = file;
1158 			status = nfs_ok;
1159 			trace_nfsd_file_opened(nf, status);
1160 		} else {
1161 			status = nfsd_open_verified(rqstp, fhp, may_flags,
1162 						    &nf->nf_file);
1163 			trace_nfsd_file_open(nf, status);
1164 		}
1165 	} else
1166 		status = nfserr_jukebox;
1167 	/*
1168 	 * If construction failed, or we raced with a call to unlink()
1169 	 * then unhash.
1170 	 */
1171 	if (status == nfs_ok && key.inode->i_nlink == 0)
1172 		status = nfserr_jukebox;
1173 	if (status != nfs_ok)
1174 		nfsd_file_unhash(nf);
1175 	clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
1176 	smp_mb__after_atomic();
1177 	wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
1178 	goto out;
1179 }
1180 
1181 /**
1182  * nfsd_file_acquire_gc - Get a struct nfsd_file with an open file
1183  * @rqstp: the RPC transaction being executed
1184  * @fhp: the NFS filehandle of the file to be opened
1185  * @may_flags: NFSD_MAY_ settings for the file
1186  * @pnf: OUT: new or found "struct nfsd_file" object
1187  *
1188  * The nfsd_file object returned by this API is reference-counted
1189  * and garbage-collected. The object is retained for a few
1190  * seconds after the final nfsd_file_put() in case the caller
1191  * wants to re-use it.
1192  *
1193  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1194  * network byte order is returned.
1195  */
1196 __be32
nfsd_file_acquire_gc(struct svc_rqst * rqstp,struct svc_fh * fhp,unsigned int may_flags,struct nfsd_file ** pnf)1197 nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp,
1198 		     unsigned int may_flags, struct nfsd_file **pnf)
1199 {
1200 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, NULL, pnf, true);
1201 }
1202 
1203 /**
1204  * nfsd_file_acquire - Get a struct nfsd_file with an open file
1205  * @rqstp: the RPC transaction being executed
1206  * @fhp: the NFS filehandle of the file to be opened
1207  * @may_flags: NFSD_MAY_ settings for the file
1208  * @pnf: OUT: new or found "struct nfsd_file" object
1209  *
1210  * The nfsd_file_object returned by this API is reference-counted
1211  * but not garbage-collected. The object is unhashed after the
1212  * final nfsd_file_put().
1213  *
1214  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1215  * network byte order is returned.
1216  */
1217 __be32
nfsd_file_acquire(struct svc_rqst * rqstp,struct svc_fh * fhp,unsigned int may_flags,struct nfsd_file ** pnf)1218 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1219 		  unsigned int may_flags, struct nfsd_file **pnf)
1220 {
1221 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, NULL, pnf, false);
1222 }
1223 
1224 /**
1225  * nfsd_file_acquire_opened - Get a struct nfsd_file using existing open file
1226  * @rqstp: the RPC transaction being executed
1227  * @fhp: the NFS filehandle of the file just created
1228  * @may_flags: NFSD_MAY_ settings for the file
1229  * @file: cached, already-open file (may be NULL)
1230  * @pnf: OUT: new or found "struct nfsd_file" object
1231  *
1232  * Acquire a nfsd_file object that is not GC'ed. If one doesn't already exist,
1233  * and @file is non-NULL, use it to instantiate a new nfsd_file instead of
1234  * opening a new one.
1235  *
1236  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1237  * network byte order is returned.
1238  */
1239 __be32
nfsd_file_acquire_opened(struct svc_rqst * rqstp,struct svc_fh * fhp,unsigned int may_flags,struct file * file,struct nfsd_file ** pnf)1240 nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,
1241 			 unsigned int may_flags, struct file *file,
1242 			 struct nfsd_file **pnf)
1243 {
1244 	return nfsd_file_do_acquire(rqstp, fhp, may_flags, file, pnf, false);
1245 }
1246 
1247 /*
1248  * Note that fields may be added, removed or reordered in the future. Programs
1249  * scraping this file for info should test the labels to ensure they're
1250  * getting the correct field.
1251  */
nfsd_file_cache_stats_show(struct seq_file * m,void * v)1252 int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
1253 {
1254 	unsigned long releases = 0, evictions = 0;
1255 	unsigned long hits = 0, acquisitions = 0;
1256 	unsigned int i, count = 0, buckets = 0;
1257 	unsigned long lru = 0, total_age = 0;
1258 
1259 	/* Serialize with server shutdown */
1260 	mutex_lock(&nfsd_mutex);
1261 	if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
1262 		struct bucket_table *tbl;
1263 		struct rhashtable *ht;
1264 
1265 		lru = list_lru_count(&nfsd_file_lru);
1266 
1267 		rcu_read_lock();
1268 		ht = &nfsd_file_rhash_tbl;
1269 		count = atomic_read(&ht->nelems);
1270 		tbl = rht_dereference_rcu(ht->tbl, ht);
1271 		buckets = tbl->size;
1272 		rcu_read_unlock();
1273 	}
1274 	mutex_unlock(&nfsd_mutex);
1275 
1276 	for_each_possible_cpu(i) {
1277 		hits += per_cpu(nfsd_file_cache_hits, i);
1278 		acquisitions += per_cpu(nfsd_file_acquisitions, i);
1279 		releases += per_cpu(nfsd_file_releases, i);
1280 		total_age += per_cpu(nfsd_file_total_age, i);
1281 		evictions += per_cpu(nfsd_file_evictions, i);
1282 	}
1283 
1284 	seq_printf(m, "total entries: %u\n", count);
1285 	seq_printf(m, "hash buckets:  %u\n", buckets);
1286 	seq_printf(m, "lru entries:   %lu\n", lru);
1287 	seq_printf(m, "cache hits:    %lu\n", hits);
1288 	seq_printf(m, "acquisitions:  %lu\n", acquisitions);
1289 	seq_printf(m, "releases:      %lu\n", releases);
1290 	seq_printf(m, "evictions:     %lu\n", evictions);
1291 	if (releases)
1292 		seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1293 	else
1294 		seq_printf(m, "mean age (ms): -\n");
1295 	return 0;
1296 }
1297