xref: /openbmc/linux/fs/nfsd/filecache.c (revision bb13f35b96f4c83dc4015d71fa2b543c665fbdae)
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/sched.h>
11 #include <linux/list_lru.h>
12 #include <linux/fsnotify_backend.h>
13 #include <linux/fsnotify.h>
14 #include <linux/seq_file.h>
15 
16 #include "vfs.h"
17 #include "nfsd.h"
18 #include "nfsfh.h"
19 #include "filecache.h"
20 #include "trace.h"
21 
22 #define NFSDDBG_FACILITY	NFSDDBG_FH
23 
24 /* FIXME: dynamically size this for the machine somehow? */
25 #define NFSD_FILE_HASH_BITS                   12
26 #define NFSD_FILE_HASH_SIZE                  (1 << NFSD_FILE_HASH_BITS)
27 #define NFSD_LAUNDRETTE_DELAY		     (2 * HZ)
28 
29 #define NFSD_FILE_LRU_RESCAN		     (0)
30 #define NFSD_FILE_SHUTDOWN		     (1)
31 #define NFSD_FILE_LRU_THRESHOLD		     (4096UL)
32 #define NFSD_FILE_LRU_LIMIT		     (NFSD_FILE_LRU_THRESHOLD << 2)
33 
34 /* We only care about NFSD_MAY_READ/WRITE for this cache */
35 #define NFSD_FILE_MAY_MASK	(NFSD_MAY_READ|NFSD_MAY_WRITE)
36 
37 struct nfsd_fcache_bucket {
38 	struct hlist_head	nfb_head;
39 	spinlock_t		nfb_lock;
40 	unsigned int		nfb_count;
41 	unsigned int		nfb_maxcount;
42 };
43 
44 static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
45 
46 static struct kmem_cache		*nfsd_file_slab;
47 static struct kmem_cache		*nfsd_file_mark_slab;
48 static struct nfsd_fcache_bucket	*nfsd_file_hashtbl;
49 static struct list_lru			nfsd_file_lru;
50 static long				nfsd_file_lru_flags;
51 static struct fsnotify_group		*nfsd_file_fsnotify_group;
52 static atomic_long_t			nfsd_filecache_count;
53 static struct delayed_work		nfsd_filecache_laundrette;
54 
55 enum nfsd_file_laundrette_ctl {
56 	NFSD_FILE_LAUNDRETTE_NOFLUSH = 0,
57 	NFSD_FILE_LAUNDRETTE_MAY_FLUSH
58 };
59 
60 static void
61 nfsd_file_schedule_laundrette(enum nfsd_file_laundrette_ctl ctl)
62 {
63 	long count = atomic_long_read(&nfsd_filecache_count);
64 
65 	if (count == 0 || test_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags))
66 		return;
67 
68 	/* Be more aggressive about scanning if over the threshold */
69 	if (count > NFSD_FILE_LRU_THRESHOLD)
70 		mod_delayed_work(system_wq, &nfsd_filecache_laundrette, 0);
71 	else
72 		schedule_delayed_work(&nfsd_filecache_laundrette, NFSD_LAUNDRETTE_DELAY);
73 
74 	if (ctl == NFSD_FILE_LAUNDRETTE_NOFLUSH)
75 		return;
76 
77 	/* ...and don't delay flushing if we're out of control */
78 	if (count >= NFSD_FILE_LRU_LIMIT)
79 		flush_delayed_work(&nfsd_filecache_laundrette);
80 }
81 
82 static void
83 nfsd_file_slab_free(struct rcu_head *rcu)
84 {
85 	struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
86 
87 	put_cred(nf->nf_cred);
88 	kmem_cache_free(nfsd_file_slab, nf);
89 }
90 
91 static void
92 nfsd_file_mark_free(struct fsnotify_mark *mark)
93 {
94 	struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
95 						  nfm_mark);
96 
97 	kmem_cache_free(nfsd_file_mark_slab, nfm);
98 }
99 
100 static struct nfsd_file_mark *
101 nfsd_file_mark_get(struct nfsd_file_mark *nfm)
102 {
103 	if (!atomic_inc_not_zero(&nfm->nfm_ref))
104 		return NULL;
105 	return nfm;
106 }
107 
108 static void
109 nfsd_file_mark_put(struct nfsd_file_mark *nfm)
110 {
111 	if (atomic_dec_and_test(&nfm->nfm_ref)) {
112 
113 		fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
114 		fsnotify_put_mark(&nfm->nfm_mark);
115 	}
116 }
117 
118 static struct nfsd_file_mark *
119 nfsd_file_mark_find_or_create(struct nfsd_file *nf)
120 {
121 	int			err;
122 	struct fsnotify_mark	*mark;
123 	struct nfsd_file_mark	*nfm = NULL, *new;
124 	struct inode *inode = nf->nf_inode;
125 
126 	do {
127 		mutex_lock(&nfsd_file_fsnotify_group->mark_mutex);
128 		mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
129 				nfsd_file_fsnotify_group);
130 		if (mark) {
131 			nfm = nfsd_file_mark_get(container_of(mark,
132 						 struct nfsd_file_mark,
133 						 nfm_mark));
134 			mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
135 			fsnotify_put_mark(mark);
136 			if (likely(nfm))
137 				break;
138 		} else
139 			mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
140 
141 		/* allocate a new nfm */
142 		new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
143 		if (!new)
144 			return NULL;
145 		fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
146 		new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
147 		atomic_set(&new->nfm_ref, 1);
148 
149 		err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
150 
151 		/*
152 		 * If the add was successful, then return the object.
153 		 * Otherwise, we need to put the reference we hold on the
154 		 * nfm_mark. The fsnotify code will take a reference and put
155 		 * it on failure, so we can't just free it directly. It's also
156 		 * not safe to call fsnotify_destroy_mark on it as the
157 		 * mark->group will be NULL. Thus, we can't let the nfm_ref
158 		 * counter drive the destruction at this point.
159 		 */
160 		if (likely(!err))
161 			nfm = new;
162 		else
163 			fsnotify_put_mark(&new->nfm_mark);
164 	} while (unlikely(err == -EEXIST));
165 
166 	return nfm;
167 }
168 
169 static struct nfsd_file *
170 nfsd_file_alloc(struct inode *inode, unsigned int may, unsigned int hashval)
171 {
172 	struct nfsd_file *nf;
173 
174 	nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
175 	if (nf) {
176 		INIT_HLIST_NODE(&nf->nf_node);
177 		INIT_LIST_HEAD(&nf->nf_lru);
178 		nf->nf_file = NULL;
179 		nf->nf_cred = get_current_cred();
180 		nf->nf_flags = 0;
181 		nf->nf_inode = inode;
182 		nf->nf_hashval = hashval;
183 		atomic_set(&nf->nf_ref, 1);
184 		nf->nf_may = may & NFSD_FILE_MAY_MASK;
185 		if (may & NFSD_MAY_NOT_BREAK_LEASE) {
186 			if (may & NFSD_MAY_WRITE)
187 				__set_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags);
188 			if (may & NFSD_MAY_READ)
189 				__set_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
190 		}
191 		nf->nf_mark = NULL;
192 		trace_nfsd_file_alloc(nf);
193 	}
194 	return nf;
195 }
196 
197 static bool
198 nfsd_file_free(struct nfsd_file *nf)
199 {
200 	bool flush = false;
201 
202 	trace_nfsd_file_put_final(nf);
203 	if (nf->nf_mark)
204 		nfsd_file_mark_put(nf->nf_mark);
205 	if (nf->nf_file) {
206 		get_file(nf->nf_file);
207 		filp_close(nf->nf_file, NULL);
208 		fput(nf->nf_file);
209 		flush = true;
210 	}
211 	call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
212 	return flush;
213 }
214 
215 static void
216 nfsd_file_do_unhash(struct nfsd_file *nf)
217 {
218 	lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
219 
220 	trace_nfsd_file_unhash(nf);
221 
222 	--nfsd_file_hashtbl[nf->nf_hashval].nfb_count;
223 	hlist_del_rcu(&nf->nf_node);
224 	if (!list_empty(&nf->nf_lru))
225 		list_lru_del(&nfsd_file_lru, &nf->nf_lru);
226 	atomic_long_dec(&nfsd_filecache_count);
227 }
228 
229 static bool
230 nfsd_file_unhash(struct nfsd_file *nf)
231 {
232 	if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
233 		nfsd_file_do_unhash(nf);
234 		return true;
235 	}
236 	return false;
237 }
238 
239 /*
240  * Return true if the file was unhashed.
241  */
242 static bool
243 nfsd_file_unhash_and_release_locked(struct nfsd_file *nf, struct list_head *dispose)
244 {
245 	lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
246 
247 	trace_nfsd_file_unhash_and_release_locked(nf);
248 	if (!nfsd_file_unhash(nf))
249 		return false;
250 	/* keep final reference for nfsd_file_lru_dispose */
251 	if (atomic_add_unless(&nf->nf_ref, -1, 1))
252 		return true;
253 
254 	list_add(&nf->nf_lru, dispose);
255 	return true;
256 }
257 
258 static int
259 nfsd_file_put_noref(struct nfsd_file *nf)
260 {
261 	int count;
262 	trace_nfsd_file_put(nf);
263 
264 	count = atomic_dec_return(&nf->nf_ref);
265 	if (!count) {
266 		WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
267 		nfsd_file_free(nf);
268 	}
269 	return count;
270 }
271 
272 void
273 nfsd_file_put(struct nfsd_file *nf)
274 {
275 	bool is_hashed = test_bit(NFSD_FILE_HASHED, &nf->nf_flags) != 0;
276 
277 	set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
278 	if (nfsd_file_put_noref(nf) == 1 && is_hashed)
279 		nfsd_file_schedule_laundrette(NFSD_FILE_LAUNDRETTE_MAY_FLUSH);
280 }
281 
282 struct nfsd_file *
283 nfsd_file_get(struct nfsd_file *nf)
284 {
285 	if (likely(atomic_inc_not_zero(&nf->nf_ref)))
286 		return nf;
287 	return NULL;
288 }
289 
290 static void
291 nfsd_file_dispose_list(struct list_head *dispose)
292 {
293 	struct nfsd_file *nf;
294 
295 	while(!list_empty(dispose)) {
296 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
297 		list_del(&nf->nf_lru);
298 		nfsd_file_put_noref(nf);
299 	}
300 }
301 
302 static void
303 nfsd_file_dispose_list_sync(struct list_head *dispose)
304 {
305 	bool flush = false;
306 	struct nfsd_file *nf;
307 
308 	while(!list_empty(dispose)) {
309 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
310 		list_del(&nf->nf_lru);
311 		if (!atomic_dec_and_test(&nf->nf_ref))
312 			continue;
313 		if (nfsd_file_free(nf))
314 			flush = true;
315 	}
316 	if (flush)
317 		flush_delayed_fput();
318 }
319 
320 /*
321  * Note this can deadlock with nfsd_file_cache_purge.
322  */
323 static enum lru_status
324 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
325 		 spinlock_t *lock, void *arg)
326 	__releases(lock)
327 	__acquires(lock)
328 {
329 	struct list_head *head = arg;
330 	struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
331 
332 	/*
333 	 * Do a lockless refcount check. The hashtable holds one reference, so
334 	 * we look to see if anything else has a reference, or if any have
335 	 * been put since the shrinker last ran. Those don't get unhashed and
336 	 * released.
337 	 *
338 	 * Note that in the put path, we set the flag and then decrement the
339 	 * counter. Here we check the counter and then test and clear the flag.
340 	 * That order is deliberate to ensure that we can do this locklessly.
341 	 */
342 	if (atomic_read(&nf->nf_ref) > 1)
343 		goto out_skip;
344 	if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags))
345 		goto out_rescan;
346 
347 	if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags))
348 		goto out_skip;
349 
350 	list_lru_isolate_move(lru, &nf->nf_lru, head);
351 	return LRU_REMOVED;
352 out_rescan:
353 	set_bit(NFSD_FILE_LRU_RESCAN, &nfsd_file_lru_flags);
354 out_skip:
355 	return LRU_SKIP;
356 }
357 
358 static void
359 nfsd_file_lru_dispose(struct list_head *head)
360 {
361 	while(!list_empty(head)) {
362 		struct nfsd_file *nf = list_first_entry(head,
363 				struct nfsd_file, nf_lru);
364 		list_del_init(&nf->nf_lru);
365 		spin_lock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
366 		nfsd_file_do_unhash(nf);
367 		spin_unlock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
368 		nfsd_file_put_noref(nf);
369 	}
370 }
371 
372 static unsigned long
373 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
374 {
375 	return list_lru_count(&nfsd_file_lru);
376 }
377 
378 static unsigned long
379 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
380 {
381 	LIST_HEAD(head);
382 	unsigned long ret;
383 
384 	ret = list_lru_shrink_walk(&nfsd_file_lru, sc, nfsd_file_lru_cb, &head);
385 	nfsd_file_lru_dispose(&head);
386 	return ret;
387 }
388 
389 static struct shrinker	nfsd_file_shrinker = {
390 	.scan_objects = nfsd_file_lru_scan,
391 	.count_objects = nfsd_file_lru_count,
392 	.seeks = 1,
393 };
394 
395 static void
396 __nfsd_file_close_inode(struct inode *inode, unsigned int hashval,
397 			struct list_head *dispose)
398 {
399 	struct nfsd_file	*nf;
400 	struct hlist_node	*tmp;
401 
402 	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
403 	hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) {
404 		if (inode == nf->nf_inode)
405 			nfsd_file_unhash_and_release_locked(nf, dispose);
406 	}
407 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
408 }
409 
410 /**
411  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
412  * @inode: inode of the file to attempt to remove
413  *
414  * Walk the whole hash bucket, looking for any files that correspond to "inode".
415  * If any do, then unhash them and put the hashtable reference to them and
416  * destroy any that had their last reference put. Also ensure that any of the
417  * fputs also have their final __fput done as well.
418  */
419 void
420 nfsd_file_close_inode_sync(struct inode *inode)
421 {
422 	unsigned int		hashval = (unsigned int)hash_long(inode->i_ino,
423 						NFSD_FILE_HASH_BITS);
424 	LIST_HEAD(dispose);
425 
426 	__nfsd_file_close_inode(inode, hashval, &dispose);
427 	trace_nfsd_file_close_inode_sync(inode, hashval, !list_empty(&dispose));
428 	nfsd_file_dispose_list_sync(&dispose);
429 }
430 
431 /**
432  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
433  * @inode: inode of the file to attempt to remove
434  *
435  * Walk the whole hash bucket, looking for any files that correspond to "inode".
436  * If any do, then unhash them and put the hashtable reference to them and
437  * destroy any that had their last reference put.
438  */
439 static void
440 nfsd_file_close_inode(struct inode *inode)
441 {
442 	unsigned int		hashval = (unsigned int)hash_long(inode->i_ino,
443 						NFSD_FILE_HASH_BITS);
444 	LIST_HEAD(dispose);
445 
446 	__nfsd_file_close_inode(inode, hashval, &dispose);
447 	trace_nfsd_file_close_inode(inode, hashval, !list_empty(&dispose));
448 	nfsd_file_dispose_list(&dispose);
449 }
450 
451 /**
452  * nfsd_file_delayed_close - close unused nfsd_files
453  * @work: dummy
454  *
455  * Walk the LRU list and close any entries that have not been used since
456  * the last scan.
457  *
458  * Note this can deadlock with nfsd_file_cache_purge.
459  */
460 static void
461 nfsd_file_delayed_close(struct work_struct *work)
462 {
463 	LIST_HEAD(head);
464 
465 	list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb, &head, LONG_MAX);
466 
467 	if (test_and_clear_bit(NFSD_FILE_LRU_RESCAN, &nfsd_file_lru_flags))
468 		nfsd_file_schedule_laundrette(NFSD_FILE_LAUNDRETTE_NOFLUSH);
469 
470 	if (!list_empty(&head)) {
471 		nfsd_file_lru_dispose(&head);
472 		flush_delayed_fput();
473 	}
474 }
475 
476 static int
477 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
478 			    void *data)
479 {
480 	struct file_lock *fl = data;
481 
482 	/* Only close files for F_SETLEASE leases */
483 	if (fl->fl_flags & FL_LEASE)
484 		nfsd_file_close_inode_sync(file_inode(fl->fl_file));
485 	return 0;
486 }
487 
488 static struct notifier_block nfsd_file_lease_notifier = {
489 	.notifier_call = nfsd_file_lease_notifier_call,
490 };
491 
492 static int
493 nfsd_file_fsnotify_handle_event(struct fsnotify_group *group,
494 				struct inode *inode,
495 				u32 mask, const void *data, int data_type,
496 				const struct qstr *file_name, u32 cookie,
497 				struct fsnotify_iter_info *iter_info)
498 {
499 	trace_nfsd_file_fsnotify_handle_event(inode, mask);
500 
501 	/* Should be no marks on non-regular files */
502 	if (!S_ISREG(inode->i_mode)) {
503 		WARN_ON_ONCE(1);
504 		return 0;
505 	}
506 
507 	/* don't close files if this was not the last link */
508 	if (mask & FS_ATTRIB) {
509 		if (inode->i_nlink)
510 			return 0;
511 	}
512 
513 	nfsd_file_close_inode(inode);
514 	return 0;
515 }
516 
517 
518 static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
519 	.handle_event = nfsd_file_fsnotify_handle_event,
520 	.free_mark = nfsd_file_mark_free,
521 };
522 
523 int
524 nfsd_file_cache_init(void)
525 {
526 	int		ret = -ENOMEM;
527 	unsigned int	i;
528 
529 	clear_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
530 
531 	if (nfsd_file_hashtbl)
532 		return 0;
533 
534 	nfsd_file_hashtbl = kcalloc(NFSD_FILE_HASH_SIZE,
535 				sizeof(*nfsd_file_hashtbl), GFP_KERNEL);
536 	if (!nfsd_file_hashtbl) {
537 		pr_err("nfsd: unable to allocate nfsd_file_hashtbl\n");
538 		goto out_err;
539 	}
540 
541 	nfsd_file_slab = kmem_cache_create("nfsd_file",
542 				sizeof(struct nfsd_file), 0, 0, NULL);
543 	if (!nfsd_file_slab) {
544 		pr_err("nfsd: unable to create nfsd_file_slab\n");
545 		goto out_err;
546 	}
547 
548 	nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
549 					sizeof(struct nfsd_file_mark), 0, 0, NULL);
550 	if (!nfsd_file_mark_slab) {
551 		pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
552 		goto out_err;
553 	}
554 
555 
556 	ret = list_lru_init(&nfsd_file_lru);
557 	if (ret) {
558 		pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
559 		goto out_err;
560 	}
561 
562 	ret = register_shrinker(&nfsd_file_shrinker);
563 	if (ret) {
564 		pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
565 		goto out_lru;
566 	}
567 
568 	ret = lease_register_notifier(&nfsd_file_lease_notifier);
569 	if (ret) {
570 		pr_err("nfsd: unable to register lease notifier: %d\n", ret);
571 		goto out_shrinker;
572 	}
573 
574 	nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops);
575 	if (IS_ERR(nfsd_file_fsnotify_group)) {
576 		pr_err("nfsd: unable to create fsnotify group: %ld\n",
577 			PTR_ERR(nfsd_file_fsnotify_group));
578 		nfsd_file_fsnotify_group = NULL;
579 		goto out_notifier;
580 	}
581 
582 	for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
583 		INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head);
584 		spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock);
585 	}
586 
587 	INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_delayed_close);
588 out:
589 	return ret;
590 out_notifier:
591 	lease_unregister_notifier(&nfsd_file_lease_notifier);
592 out_shrinker:
593 	unregister_shrinker(&nfsd_file_shrinker);
594 out_lru:
595 	list_lru_destroy(&nfsd_file_lru);
596 out_err:
597 	kmem_cache_destroy(nfsd_file_slab);
598 	nfsd_file_slab = NULL;
599 	kmem_cache_destroy(nfsd_file_mark_slab);
600 	nfsd_file_mark_slab = NULL;
601 	kfree(nfsd_file_hashtbl);
602 	nfsd_file_hashtbl = NULL;
603 	goto out;
604 }
605 
606 /*
607  * Note this can deadlock with nfsd_file_lru_cb.
608  */
609 void
610 nfsd_file_cache_purge(void)
611 {
612 	unsigned int		i;
613 	struct nfsd_file	*nf;
614 	LIST_HEAD(dispose);
615 	bool del;
616 
617 	if (!nfsd_file_hashtbl)
618 		return;
619 
620 	for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
621 		spin_lock(&nfsd_file_hashtbl[i].nfb_lock);
622 		while(!hlist_empty(&nfsd_file_hashtbl[i].nfb_head)) {
623 			nf = hlist_entry(nfsd_file_hashtbl[i].nfb_head.first,
624 					 struct nfsd_file, nf_node);
625 			del = nfsd_file_unhash_and_release_locked(nf, &dispose);
626 
627 			/*
628 			 * Deadlock detected! Something marked this entry as
629 			 * unhased, but hasn't removed it from the hash list.
630 			 */
631 			WARN_ON_ONCE(!del);
632 		}
633 		spin_unlock(&nfsd_file_hashtbl[i].nfb_lock);
634 		nfsd_file_dispose_list(&dispose);
635 	}
636 }
637 
638 void
639 nfsd_file_cache_shutdown(void)
640 {
641 	LIST_HEAD(dispose);
642 
643 	set_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
644 
645 	lease_unregister_notifier(&nfsd_file_lease_notifier);
646 	unregister_shrinker(&nfsd_file_shrinker);
647 	/*
648 	 * make sure all callers of nfsd_file_lru_cb are done before
649 	 * calling nfsd_file_cache_purge
650 	 */
651 	cancel_delayed_work_sync(&nfsd_filecache_laundrette);
652 	nfsd_file_cache_purge();
653 	list_lru_destroy(&nfsd_file_lru);
654 	rcu_barrier();
655 	fsnotify_put_group(nfsd_file_fsnotify_group);
656 	nfsd_file_fsnotify_group = NULL;
657 	kmem_cache_destroy(nfsd_file_slab);
658 	nfsd_file_slab = NULL;
659 	fsnotify_wait_marks_destroyed();
660 	kmem_cache_destroy(nfsd_file_mark_slab);
661 	nfsd_file_mark_slab = NULL;
662 	kfree(nfsd_file_hashtbl);
663 	nfsd_file_hashtbl = NULL;
664 }
665 
666 static bool
667 nfsd_match_cred(const struct cred *c1, const struct cred *c2)
668 {
669 	int i;
670 
671 	if (!uid_eq(c1->fsuid, c2->fsuid))
672 		return false;
673 	if (!gid_eq(c1->fsgid, c2->fsgid))
674 		return false;
675 	if (c1->group_info == NULL || c2->group_info == NULL)
676 		return c1->group_info == c2->group_info;
677 	if (c1->group_info->ngroups != c2->group_info->ngroups)
678 		return false;
679 	for (i = 0; i < c1->group_info->ngroups; i++) {
680 		if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
681 			return false;
682 	}
683 	return true;
684 }
685 
686 static struct nfsd_file *
687 nfsd_file_find_locked(struct inode *inode, unsigned int may_flags,
688 			unsigned int hashval)
689 {
690 	struct nfsd_file *nf;
691 	unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
692 
693 	hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
694 				 nf_node) {
695 		if ((need & nf->nf_may) != need)
696 			continue;
697 		if (nf->nf_inode != inode)
698 			continue;
699 		if (!nfsd_match_cred(nf->nf_cred, current_cred()))
700 			continue;
701 		if (nfsd_file_get(nf) != NULL)
702 			return nf;
703 	}
704 	return NULL;
705 }
706 
707 /**
708  * nfsd_file_is_cached - are there any cached open files for this fh?
709  * @inode: inode of the file to check
710  *
711  * Scan the hashtable for open files that match this fh. Returns true if there
712  * are any, and false if not.
713  */
714 bool
715 nfsd_file_is_cached(struct inode *inode)
716 {
717 	bool			ret = false;
718 	struct nfsd_file	*nf;
719 	unsigned int		hashval;
720 
721         hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
722 
723 	rcu_read_lock();
724 	hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
725 				 nf_node) {
726 		if (inode == nf->nf_inode) {
727 			ret = true;
728 			break;
729 		}
730 	}
731 	rcu_read_unlock();
732 	trace_nfsd_file_is_cached(inode, hashval, (int)ret);
733 	return ret;
734 }
735 
736 __be32
737 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
738 		  unsigned int may_flags, struct nfsd_file **pnf)
739 {
740 	__be32	status;
741 	struct nfsd_file *nf, *new;
742 	struct inode *inode;
743 	unsigned int hashval;
744 
745 	/* FIXME: skip this if fh_dentry is already set? */
746 	status = fh_verify(rqstp, fhp, S_IFREG,
747 				may_flags|NFSD_MAY_OWNER_OVERRIDE);
748 	if (status != nfs_ok)
749 		return status;
750 
751 	inode = d_inode(fhp->fh_dentry);
752 	hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
753 retry:
754 	rcu_read_lock();
755 	nf = nfsd_file_find_locked(inode, may_flags, hashval);
756 	rcu_read_unlock();
757 	if (nf)
758 		goto wait_for_construction;
759 
760 	new = nfsd_file_alloc(inode, may_flags, hashval);
761 	if (!new) {
762 		trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags,
763 					NULL, nfserr_jukebox);
764 		return nfserr_jukebox;
765 	}
766 
767 	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
768 	nf = nfsd_file_find_locked(inode, may_flags, hashval);
769 	if (nf == NULL)
770 		goto open_file;
771 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
772 	nfsd_file_slab_free(&new->nf_rcu);
773 
774 wait_for_construction:
775 	wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
776 
777 	/* Did construction of this file fail? */
778 	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
779 		nfsd_file_put_noref(nf);
780 		goto retry;
781 	}
782 
783 	this_cpu_inc(nfsd_file_cache_hits);
784 
785 	if (!(may_flags & NFSD_MAY_NOT_BREAK_LEASE)) {
786 		bool write = (may_flags & NFSD_MAY_WRITE);
787 
788 		if (test_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags) ||
789 		    (test_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags) && write)) {
790 			status = nfserrno(nfsd_open_break_lease(
791 					file_inode(nf->nf_file), may_flags));
792 			if (status == nfs_ok) {
793 				clear_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
794 				if (write)
795 					clear_bit(NFSD_FILE_BREAK_WRITE,
796 						  &nf->nf_flags);
797 			}
798 		}
799 	}
800 out:
801 	if (status == nfs_ok) {
802 		*pnf = nf;
803 	} else {
804 		nfsd_file_put(nf);
805 		nf = NULL;
806 	}
807 
808 	trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, nf, status);
809 	return status;
810 open_file:
811 	nf = new;
812 	/* Take reference for the hashtable */
813 	atomic_inc(&nf->nf_ref);
814 	__set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
815 	__set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
816 	list_lru_add(&nfsd_file_lru, &nf->nf_lru);
817 	hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head);
818 	++nfsd_file_hashtbl[hashval].nfb_count;
819 	nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount,
820 			nfsd_file_hashtbl[hashval].nfb_count);
821 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
822 	atomic_long_inc(&nfsd_filecache_count);
823 
824 	nf->nf_mark = nfsd_file_mark_find_or_create(nf);
825 	if (nf->nf_mark)
826 		status = nfsd_open_verified(rqstp, fhp, S_IFREG,
827 				may_flags, &nf->nf_file);
828 	else
829 		status = nfserr_jukebox;
830 	/*
831 	 * If construction failed, or we raced with a call to unlink()
832 	 * then unhash.
833 	 */
834 	if (status != nfs_ok || inode->i_nlink == 0) {
835 		bool do_free;
836 		spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
837 		do_free = nfsd_file_unhash(nf);
838 		spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
839 		if (do_free)
840 			nfsd_file_put_noref(nf);
841 	}
842 	clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
843 	smp_mb__after_atomic();
844 	wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
845 	goto out;
846 }
847 
848 /*
849  * Note that fields may be added, removed or reordered in the future. Programs
850  * scraping this file for info should test the labels to ensure they're
851  * getting the correct field.
852  */
853 static int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
854 {
855 	unsigned int i, count = 0, longest = 0;
856 	unsigned long hits = 0;
857 
858 	/*
859 	 * No need for spinlocks here since we're not terribly interested in
860 	 * accuracy. We do take the nfsd_mutex simply to ensure that we
861 	 * don't end up racing with server shutdown
862 	 */
863 	mutex_lock(&nfsd_mutex);
864 	if (nfsd_file_hashtbl) {
865 		for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
866 			count += nfsd_file_hashtbl[i].nfb_count;
867 			longest = max(longest, nfsd_file_hashtbl[i].nfb_count);
868 		}
869 	}
870 	mutex_unlock(&nfsd_mutex);
871 
872 	for_each_possible_cpu(i)
873 		hits += per_cpu(nfsd_file_cache_hits, i);
874 
875 	seq_printf(m, "total entries: %u\n", count);
876 	seq_printf(m, "longest chain: %u\n", longest);
877 	seq_printf(m, "cache hits:    %lu\n", hits);
878 	return 0;
879 }
880 
881 int nfsd_file_cache_stats_open(struct inode *inode, struct file *file)
882 {
883 	return single_open(file, nfsd_file_cache_stats_show, NULL);
884 }
885