xref: /openbmc/linux/fs/nfsd/filecache.c (revision 08af54b3e5729bc1d56ad3190af811301bdc37a1)
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 
17 #include "vfs.h"
18 #include "nfsd.h"
19 #include "nfsfh.h"
20 #include "netns.h"
21 #include "filecache.h"
22 #include "trace.h"
23 
24 #define NFSDDBG_FACILITY	NFSDDBG_FH
25 
26 /* FIXME: dynamically size this for the machine somehow? */
27 #define NFSD_FILE_HASH_BITS                   12
28 #define NFSD_FILE_HASH_SIZE                  (1 << NFSD_FILE_HASH_BITS)
29 #define NFSD_LAUNDRETTE_DELAY		     (2 * HZ)
30 
31 #define NFSD_FILE_SHUTDOWN		     (1)
32 #define NFSD_FILE_LRU_THRESHOLD		     (4096UL)
33 #define NFSD_FILE_LRU_LIMIT		     (NFSD_FILE_LRU_THRESHOLD << 2)
34 
35 /* We only care about NFSD_MAY_READ/WRITE for this cache */
36 #define NFSD_FILE_MAY_MASK	(NFSD_MAY_READ|NFSD_MAY_WRITE)
37 
38 struct nfsd_fcache_bucket {
39 	struct hlist_head	nfb_head;
40 	spinlock_t		nfb_lock;
41 	unsigned int		nfb_count;
42 	unsigned int		nfb_maxcount;
43 };
44 
45 static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
46 
47 struct nfsd_fcache_disposal {
48 	struct work_struct work;
49 	spinlock_t lock;
50 	struct list_head freeme;
51 };
52 
53 static struct workqueue_struct *nfsd_filecache_wq __read_mostly;
54 
55 static struct kmem_cache		*nfsd_file_slab;
56 static struct kmem_cache		*nfsd_file_mark_slab;
57 static struct nfsd_fcache_bucket	*nfsd_file_hashtbl;
58 static struct list_lru			nfsd_file_lru;
59 static long				nfsd_file_lru_flags;
60 static struct fsnotify_group		*nfsd_file_fsnotify_group;
61 static atomic_long_t			nfsd_filecache_count;
62 static struct delayed_work		nfsd_filecache_laundrette;
63 
64 static void nfsd_file_gc(void);
65 
66 static void
67 nfsd_file_schedule_laundrette(void)
68 {
69 	long count = atomic_long_read(&nfsd_filecache_count);
70 
71 	if (count == 0 || test_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags))
72 		return;
73 
74 	queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
75 			NFSD_LAUNDRETTE_DELAY);
76 }
77 
78 static void
79 nfsd_file_slab_free(struct rcu_head *rcu)
80 {
81 	struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
82 
83 	put_cred(nf->nf_cred);
84 	kmem_cache_free(nfsd_file_slab, nf);
85 }
86 
87 static void
88 nfsd_file_mark_free(struct fsnotify_mark *mark)
89 {
90 	struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
91 						  nfm_mark);
92 
93 	kmem_cache_free(nfsd_file_mark_slab, nfm);
94 }
95 
96 static struct nfsd_file_mark *
97 nfsd_file_mark_get(struct nfsd_file_mark *nfm)
98 {
99 	if (!refcount_inc_not_zero(&nfm->nfm_ref))
100 		return NULL;
101 	return nfm;
102 }
103 
104 static void
105 nfsd_file_mark_put(struct nfsd_file_mark *nfm)
106 {
107 	if (refcount_dec_and_test(&nfm->nfm_ref)) {
108 		fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
109 		fsnotify_put_mark(&nfm->nfm_mark);
110 	}
111 }
112 
113 static struct nfsd_file_mark *
114 nfsd_file_mark_find_or_create(struct nfsd_file *nf)
115 {
116 	int			err;
117 	struct fsnotify_mark	*mark;
118 	struct nfsd_file_mark	*nfm = NULL, *new;
119 	struct inode *inode = nf->nf_inode;
120 
121 	do {
122 		mutex_lock(&nfsd_file_fsnotify_group->mark_mutex);
123 		mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
124 				nfsd_file_fsnotify_group);
125 		if (mark) {
126 			nfm = nfsd_file_mark_get(container_of(mark,
127 						 struct nfsd_file_mark,
128 						 nfm_mark));
129 			mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
130 			if (nfm) {
131 				fsnotify_put_mark(mark);
132 				break;
133 			}
134 			/* Avoid soft lockup race with nfsd_file_mark_put() */
135 			fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
136 			fsnotify_put_mark(mark);
137 		} else
138 			mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
139 
140 		/* allocate a new nfm */
141 		new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
142 		if (!new)
143 			return NULL;
144 		fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
145 		new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
146 		refcount_set(&new->nfm_ref, 1);
147 
148 		err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
149 
150 		/*
151 		 * If the add was successful, then return the object.
152 		 * Otherwise, we need to put the reference we hold on the
153 		 * nfm_mark. The fsnotify code will take a reference and put
154 		 * it on failure, so we can't just free it directly. It's also
155 		 * not safe to call fsnotify_destroy_mark on it as the
156 		 * mark->group will be NULL. Thus, we can't let the nfm_ref
157 		 * counter drive the destruction at this point.
158 		 */
159 		if (likely(!err))
160 			nfm = new;
161 		else
162 			fsnotify_put_mark(&new->nfm_mark);
163 	} while (unlikely(err == -EEXIST));
164 
165 	return nfm;
166 }
167 
168 static struct nfsd_file *
169 nfsd_file_alloc(struct inode *inode, unsigned int may, unsigned int hashval,
170 		struct net *net)
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_net = net;
181 		nf->nf_flags = 0;
182 		nf->nf_inode = inode;
183 		nf->nf_hashval = hashval;
184 		refcount_set(&nf->nf_ref, 1);
185 		nf->nf_may = may & NFSD_FILE_MAY_MASK;
186 		if (may & NFSD_MAY_NOT_BREAK_LEASE) {
187 			if (may & NFSD_MAY_WRITE)
188 				__set_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags);
189 			if (may & NFSD_MAY_READ)
190 				__set_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
191 		}
192 		nf->nf_mark = NULL;
193 		trace_nfsd_file_alloc(nf);
194 	}
195 	return nf;
196 }
197 
198 static bool
199 nfsd_file_free(struct nfsd_file *nf)
200 {
201 	bool flush = false;
202 
203 	trace_nfsd_file_put_final(nf);
204 	if (nf->nf_mark)
205 		nfsd_file_mark_put(nf->nf_mark);
206 	if (nf->nf_file) {
207 		get_file(nf->nf_file);
208 		filp_close(nf->nf_file, NULL);
209 		fput(nf->nf_file);
210 		flush = true;
211 	}
212 	call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
213 	return flush;
214 }
215 
216 static bool
217 nfsd_file_check_writeback(struct nfsd_file *nf)
218 {
219 	struct file *file = nf->nf_file;
220 	struct address_space *mapping;
221 
222 	if (!file || !(file->f_mode & FMODE_WRITE))
223 		return false;
224 	mapping = file->f_mapping;
225 	return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
226 		mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
227 }
228 
229 static int
230 nfsd_file_check_write_error(struct nfsd_file *nf)
231 {
232 	struct file *file = nf->nf_file;
233 
234 	if (!file || !(file->f_mode & FMODE_WRITE))
235 		return 0;
236 	return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
237 }
238 
239 static void
240 nfsd_file_flush(struct nfsd_file *nf)
241 {
242 	if (nf->nf_file && vfs_fsync(nf->nf_file, 1) != 0)
243 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
244 }
245 
246 static void
247 nfsd_file_do_unhash(struct nfsd_file *nf)
248 {
249 	lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
250 
251 	trace_nfsd_file_unhash(nf);
252 
253 	if (nfsd_file_check_write_error(nf))
254 		nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
255 	--nfsd_file_hashtbl[nf->nf_hashval].nfb_count;
256 	hlist_del_rcu(&nf->nf_node);
257 	atomic_long_dec(&nfsd_filecache_count);
258 }
259 
260 static bool
261 nfsd_file_unhash(struct nfsd_file *nf)
262 {
263 	if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
264 		nfsd_file_do_unhash(nf);
265 		if (!list_empty(&nf->nf_lru))
266 			list_lru_del(&nfsd_file_lru, &nf->nf_lru);
267 		return true;
268 	}
269 	return false;
270 }
271 
272 /*
273  * Return true if the file was unhashed.
274  */
275 static bool
276 nfsd_file_unhash_and_release_locked(struct nfsd_file *nf, struct list_head *dispose)
277 {
278 	lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
279 
280 	trace_nfsd_file_unhash_and_release_locked(nf);
281 	if (!nfsd_file_unhash(nf))
282 		return false;
283 	/* keep final reference for nfsd_file_lru_dispose */
284 	if (refcount_dec_not_one(&nf->nf_ref))
285 		return true;
286 
287 	list_add(&nf->nf_lru, dispose);
288 	return true;
289 }
290 
291 static void
292 nfsd_file_put_noref(struct nfsd_file *nf)
293 {
294 	trace_nfsd_file_put(nf);
295 
296 	if (refcount_dec_and_test(&nf->nf_ref)) {
297 		WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
298 		nfsd_file_free(nf);
299 	}
300 }
301 
302 void
303 nfsd_file_put(struct nfsd_file *nf)
304 {
305 	might_sleep();
306 
307 	set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
308 	if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) {
309 		nfsd_file_flush(nf);
310 		nfsd_file_put_noref(nf);
311 	} else {
312 		nfsd_file_put_noref(nf);
313 		if (nf->nf_file)
314 			nfsd_file_schedule_laundrette();
315 	}
316 	if (atomic_long_read(&nfsd_filecache_count) >= NFSD_FILE_LRU_LIMIT)
317 		nfsd_file_gc();
318 }
319 
320 struct nfsd_file *
321 nfsd_file_get(struct nfsd_file *nf)
322 {
323 	if (likely(refcount_inc_not_zero(&nf->nf_ref)))
324 		return nf;
325 	return NULL;
326 }
327 
328 static void
329 nfsd_file_dispose_list(struct list_head *dispose)
330 {
331 	struct nfsd_file *nf;
332 
333 	while(!list_empty(dispose)) {
334 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
335 		list_del(&nf->nf_lru);
336 		nfsd_file_flush(nf);
337 		nfsd_file_put_noref(nf);
338 	}
339 }
340 
341 static void
342 nfsd_file_dispose_list_sync(struct list_head *dispose)
343 {
344 	bool flush = false;
345 	struct nfsd_file *nf;
346 
347 	while(!list_empty(dispose)) {
348 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
349 		list_del(&nf->nf_lru);
350 		nfsd_file_flush(nf);
351 		if (!refcount_dec_and_test(&nf->nf_ref))
352 			continue;
353 		if (nfsd_file_free(nf))
354 			flush = true;
355 	}
356 	if (flush)
357 		flush_delayed_fput();
358 }
359 
360 static void
361 nfsd_file_list_remove_disposal(struct list_head *dst,
362 		struct nfsd_fcache_disposal *l)
363 {
364 	spin_lock(&l->lock);
365 	list_splice_init(&l->freeme, dst);
366 	spin_unlock(&l->lock);
367 }
368 
369 static void
370 nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
371 {
372 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
373 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
374 
375 	spin_lock(&l->lock);
376 	list_splice_tail_init(files, &l->freeme);
377 	spin_unlock(&l->lock);
378 	queue_work(nfsd_filecache_wq, &l->work);
379 }
380 
381 static void
382 nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
383 		struct net *net)
384 {
385 	struct nfsd_file *nf, *tmp;
386 
387 	list_for_each_entry_safe(nf, tmp, src, nf_lru) {
388 		if (nf->nf_net == net)
389 			list_move_tail(&nf->nf_lru, dst);
390 	}
391 }
392 
393 static void
394 nfsd_file_dispose_list_delayed(struct list_head *dispose)
395 {
396 	LIST_HEAD(list);
397 	struct nfsd_file *nf;
398 
399 	while(!list_empty(dispose)) {
400 		nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
401 		nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
402 		nfsd_file_list_add_disposal(&list, nf->nf_net);
403 	}
404 }
405 
406 /*
407  * Note this can deadlock with nfsd_file_cache_purge.
408  */
409 static enum lru_status
410 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
411 		 spinlock_t *lock, void *arg)
412 	__releases(lock)
413 	__acquires(lock)
414 {
415 	struct list_head *head = arg;
416 	struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
417 
418 	/*
419 	 * Do a lockless refcount check. The hashtable holds one reference, so
420 	 * we look to see if anything else has a reference, or if any have
421 	 * been put since the shrinker last ran. Those don't get unhashed and
422 	 * released.
423 	 *
424 	 * Note that in the put path, we set the flag and then decrement the
425 	 * counter. Here we check the counter and then test and clear the flag.
426 	 * That order is deliberate to ensure that we can do this locklessly.
427 	 */
428 	if (refcount_read(&nf->nf_ref) > 1)
429 		goto out_skip;
430 
431 	/*
432 	 * Don't throw out files that are still undergoing I/O or
433 	 * that have uncleared errors pending.
434 	 */
435 	if (nfsd_file_check_writeback(nf))
436 		goto out_skip;
437 
438 	if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags))
439 		goto out_skip;
440 
441 	if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags))
442 		goto out_skip;
443 
444 	list_lru_isolate_move(lru, &nf->nf_lru, head);
445 	return LRU_REMOVED;
446 out_skip:
447 	return LRU_SKIP;
448 }
449 
450 static unsigned long
451 nfsd_file_lru_walk_list(struct shrink_control *sc)
452 {
453 	LIST_HEAD(head);
454 	struct nfsd_file *nf;
455 	unsigned long ret;
456 
457 	if (sc)
458 		ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
459 				nfsd_file_lru_cb, &head);
460 	else
461 		ret = list_lru_walk(&nfsd_file_lru,
462 				nfsd_file_lru_cb,
463 				&head, LONG_MAX);
464 	list_for_each_entry(nf, &head, nf_lru) {
465 		spin_lock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
466 		nfsd_file_do_unhash(nf);
467 		spin_unlock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
468 	}
469 	nfsd_file_dispose_list_delayed(&head);
470 	return ret;
471 }
472 
473 static void
474 nfsd_file_gc(void)
475 {
476 	nfsd_file_lru_walk_list(NULL);
477 }
478 
479 static void
480 nfsd_file_gc_worker(struct work_struct *work)
481 {
482 	nfsd_file_gc();
483 	nfsd_file_schedule_laundrette();
484 }
485 
486 static unsigned long
487 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
488 {
489 	return list_lru_count(&nfsd_file_lru);
490 }
491 
492 static unsigned long
493 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
494 {
495 	return nfsd_file_lru_walk_list(sc);
496 }
497 
498 static struct shrinker	nfsd_file_shrinker = {
499 	.scan_objects = nfsd_file_lru_scan,
500 	.count_objects = nfsd_file_lru_count,
501 	.seeks = 1,
502 };
503 
504 static void
505 __nfsd_file_close_inode(struct inode *inode, unsigned int hashval,
506 			struct list_head *dispose)
507 {
508 	struct nfsd_file	*nf;
509 	struct hlist_node	*tmp;
510 
511 	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
512 	hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) {
513 		if (inode == nf->nf_inode)
514 			nfsd_file_unhash_and_release_locked(nf, dispose);
515 	}
516 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
517 }
518 
519 /**
520  * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
521  * @inode: inode of the file to attempt to remove
522  *
523  * Walk the whole hash bucket, looking for any files that correspond to "inode".
524  * If any do, then unhash them and put the hashtable reference to them and
525  * destroy any that had their last reference put. Also ensure that any of the
526  * fputs also have their final __fput done as well.
527  */
528 void
529 nfsd_file_close_inode_sync(struct inode *inode)
530 {
531 	unsigned int		hashval = (unsigned int)hash_long(inode->i_ino,
532 						NFSD_FILE_HASH_BITS);
533 	LIST_HEAD(dispose);
534 
535 	__nfsd_file_close_inode(inode, hashval, &dispose);
536 	trace_nfsd_file_close_inode_sync(inode, hashval, !list_empty(&dispose));
537 	nfsd_file_dispose_list_sync(&dispose);
538 }
539 
540 /**
541  * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
542  * @inode: inode of the file to attempt to remove
543  *
544  * Walk the whole hash bucket, looking for any files that correspond to "inode".
545  * If any do, then unhash them and put the hashtable reference to them and
546  * destroy any that had their last reference put.
547  */
548 static void
549 nfsd_file_close_inode(struct inode *inode)
550 {
551 	unsigned int		hashval = (unsigned int)hash_long(inode->i_ino,
552 						NFSD_FILE_HASH_BITS);
553 	LIST_HEAD(dispose);
554 
555 	__nfsd_file_close_inode(inode, hashval, &dispose);
556 	trace_nfsd_file_close_inode(inode, hashval, !list_empty(&dispose));
557 	nfsd_file_dispose_list_delayed(&dispose);
558 }
559 
560 /**
561  * nfsd_file_delayed_close - close unused nfsd_files
562  * @work: dummy
563  *
564  * Walk the LRU list and close any entries that have not been used since
565  * the last scan.
566  *
567  * Note this can deadlock with nfsd_file_cache_purge.
568  */
569 static void
570 nfsd_file_delayed_close(struct work_struct *work)
571 {
572 	LIST_HEAD(head);
573 	struct nfsd_fcache_disposal *l = container_of(work,
574 			struct nfsd_fcache_disposal, work);
575 
576 	nfsd_file_list_remove_disposal(&head, l);
577 	nfsd_file_dispose_list(&head);
578 }
579 
580 static int
581 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
582 			    void *data)
583 {
584 	struct file_lock *fl = data;
585 
586 	/* Only close files for F_SETLEASE leases */
587 	if (fl->fl_flags & FL_LEASE)
588 		nfsd_file_close_inode_sync(file_inode(fl->fl_file));
589 	return 0;
590 }
591 
592 static struct notifier_block nfsd_file_lease_notifier = {
593 	.notifier_call = nfsd_file_lease_notifier_call,
594 };
595 
596 static int
597 nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
598 				struct inode *inode, struct inode *dir,
599 				const struct qstr *name, u32 cookie)
600 {
601 	if (WARN_ON_ONCE(!inode))
602 		return 0;
603 
604 	trace_nfsd_file_fsnotify_handle_event(inode, mask);
605 
606 	/* Should be no marks on non-regular files */
607 	if (!S_ISREG(inode->i_mode)) {
608 		WARN_ON_ONCE(1);
609 		return 0;
610 	}
611 
612 	/* don't close files if this was not the last link */
613 	if (mask & FS_ATTRIB) {
614 		if (inode->i_nlink)
615 			return 0;
616 	}
617 
618 	nfsd_file_close_inode(inode);
619 	return 0;
620 }
621 
622 
623 static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
624 	.handle_inode_event = nfsd_file_fsnotify_handle_event,
625 	.free_mark = nfsd_file_mark_free,
626 };
627 
628 int
629 nfsd_file_cache_init(void)
630 {
631 	int		ret = -ENOMEM;
632 	unsigned int	i;
633 
634 	clear_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
635 
636 	if (nfsd_file_hashtbl)
637 		return 0;
638 
639 	nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
640 	if (!nfsd_filecache_wq)
641 		goto out;
642 
643 	nfsd_file_hashtbl = kvcalloc(NFSD_FILE_HASH_SIZE,
644 				sizeof(*nfsd_file_hashtbl), GFP_KERNEL);
645 	if (!nfsd_file_hashtbl) {
646 		pr_err("nfsd: unable to allocate nfsd_file_hashtbl\n");
647 		goto out_err;
648 	}
649 
650 	nfsd_file_slab = kmem_cache_create("nfsd_file",
651 				sizeof(struct nfsd_file), 0, 0, NULL);
652 	if (!nfsd_file_slab) {
653 		pr_err("nfsd: unable to create nfsd_file_slab\n");
654 		goto out_err;
655 	}
656 
657 	nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
658 					sizeof(struct nfsd_file_mark), 0, 0, NULL);
659 	if (!nfsd_file_mark_slab) {
660 		pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
661 		goto out_err;
662 	}
663 
664 
665 	ret = list_lru_init(&nfsd_file_lru);
666 	if (ret) {
667 		pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
668 		goto out_err;
669 	}
670 
671 	ret = register_shrinker(&nfsd_file_shrinker);
672 	if (ret) {
673 		pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
674 		goto out_lru;
675 	}
676 
677 	ret = lease_register_notifier(&nfsd_file_lease_notifier);
678 	if (ret) {
679 		pr_err("nfsd: unable to register lease notifier: %d\n", ret);
680 		goto out_shrinker;
681 	}
682 
683 	nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops);
684 	if (IS_ERR(nfsd_file_fsnotify_group)) {
685 		pr_err("nfsd: unable to create fsnotify group: %ld\n",
686 			PTR_ERR(nfsd_file_fsnotify_group));
687 		ret = PTR_ERR(nfsd_file_fsnotify_group);
688 		nfsd_file_fsnotify_group = NULL;
689 		goto out_notifier;
690 	}
691 
692 	for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
693 		INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head);
694 		spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock);
695 	}
696 
697 	INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
698 out:
699 	return ret;
700 out_notifier:
701 	lease_unregister_notifier(&nfsd_file_lease_notifier);
702 out_shrinker:
703 	unregister_shrinker(&nfsd_file_shrinker);
704 out_lru:
705 	list_lru_destroy(&nfsd_file_lru);
706 out_err:
707 	kmem_cache_destroy(nfsd_file_slab);
708 	nfsd_file_slab = NULL;
709 	kmem_cache_destroy(nfsd_file_mark_slab);
710 	nfsd_file_mark_slab = NULL;
711 	kvfree(nfsd_file_hashtbl);
712 	nfsd_file_hashtbl = NULL;
713 	destroy_workqueue(nfsd_filecache_wq);
714 	nfsd_filecache_wq = NULL;
715 	goto out;
716 }
717 
718 /*
719  * Note this can deadlock with nfsd_file_lru_cb.
720  */
721 void
722 nfsd_file_cache_purge(struct net *net)
723 {
724 	unsigned int		i;
725 	struct nfsd_file	*nf;
726 	struct hlist_node	*next;
727 	LIST_HEAD(dispose);
728 	bool del;
729 
730 	if (!nfsd_file_hashtbl)
731 		return;
732 
733 	for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
734 		struct nfsd_fcache_bucket *nfb = &nfsd_file_hashtbl[i];
735 
736 		spin_lock(&nfb->nfb_lock);
737 		hlist_for_each_entry_safe(nf, next, &nfb->nfb_head, nf_node) {
738 			if (net && nf->nf_net != net)
739 				continue;
740 			del = nfsd_file_unhash_and_release_locked(nf, &dispose);
741 
742 			/*
743 			 * Deadlock detected! Something marked this entry as
744 			 * unhased, but hasn't removed it from the hash list.
745 			 */
746 			WARN_ON_ONCE(!del);
747 		}
748 		spin_unlock(&nfb->nfb_lock);
749 		nfsd_file_dispose_list(&dispose);
750 	}
751 }
752 
753 static struct nfsd_fcache_disposal *
754 nfsd_alloc_fcache_disposal(void)
755 {
756 	struct nfsd_fcache_disposal *l;
757 
758 	l = kmalloc(sizeof(*l), GFP_KERNEL);
759 	if (!l)
760 		return NULL;
761 	INIT_WORK(&l->work, nfsd_file_delayed_close);
762 	spin_lock_init(&l->lock);
763 	INIT_LIST_HEAD(&l->freeme);
764 	return l;
765 }
766 
767 static void
768 nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
769 {
770 	cancel_work_sync(&l->work);
771 	nfsd_file_dispose_list(&l->freeme);
772 	kfree(l);
773 }
774 
775 static void
776 nfsd_free_fcache_disposal_net(struct net *net)
777 {
778 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
779 	struct nfsd_fcache_disposal *l = nn->fcache_disposal;
780 
781 	nfsd_free_fcache_disposal(l);
782 }
783 
784 int
785 nfsd_file_cache_start_net(struct net *net)
786 {
787 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
788 
789 	nn->fcache_disposal = nfsd_alloc_fcache_disposal();
790 	return nn->fcache_disposal ? 0 : -ENOMEM;
791 }
792 
793 void
794 nfsd_file_cache_shutdown_net(struct net *net)
795 {
796 	nfsd_file_cache_purge(net);
797 	nfsd_free_fcache_disposal_net(net);
798 }
799 
800 void
801 nfsd_file_cache_shutdown(void)
802 {
803 	set_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
804 
805 	lease_unregister_notifier(&nfsd_file_lease_notifier);
806 	unregister_shrinker(&nfsd_file_shrinker);
807 	/*
808 	 * make sure all callers of nfsd_file_lru_cb are done before
809 	 * calling nfsd_file_cache_purge
810 	 */
811 	cancel_delayed_work_sync(&nfsd_filecache_laundrette);
812 	nfsd_file_cache_purge(NULL);
813 	list_lru_destroy(&nfsd_file_lru);
814 	rcu_barrier();
815 	fsnotify_put_group(nfsd_file_fsnotify_group);
816 	nfsd_file_fsnotify_group = NULL;
817 	kmem_cache_destroy(nfsd_file_slab);
818 	nfsd_file_slab = NULL;
819 	fsnotify_wait_marks_destroyed();
820 	kmem_cache_destroy(nfsd_file_mark_slab);
821 	nfsd_file_mark_slab = NULL;
822 	kvfree(nfsd_file_hashtbl);
823 	nfsd_file_hashtbl = NULL;
824 	destroy_workqueue(nfsd_filecache_wq);
825 	nfsd_filecache_wq = NULL;
826 }
827 
828 static bool
829 nfsd_match_cred(const struct cred *c1, const struct cred *c2)
830 {
831 	int i;
832 
833 	if (!uid_eq(c1->fsuid, c2->fsuid))
834 		return false;
835 	if (!gid_eq(c1->fsgid, c2->fsgid))
836 		return false;
837 	if (c1->group_info == NULL || c2->group_info == NULL)
838 		return c1->group_info == c2->group_info;
839 	if (c1->group_info->ngroups != c2->group_info->ngroups)
840 		return false;
841 	for (i = 0; i < c1->group_info->ngroups; i++) {
842 		if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
843 			return false;
844 	}
845 	return true;
846 }
847 
848 static struct nfsd_file *
849 nfsd_file_find_locked(struct inode *inode, unsigned int may_flags,
850 			unsigned int hashval, struct net *net)
851 {
852 	struct nfsd_file *nf;
853 	unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
854 
855 	hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
856 				 nf_node, lockdep_is_held(&nfsd_file_hashtbl[hashval].nfb_lock)) {
857 		if (nf->nf_may != need)
858 			continue;
859 		if (nf->nf_inode != inode)
860 			continue;
861 		if (nf->nf_net != net)
862 			continue;
863 		if (!nfsd_match_cred(nf->nf_cred, current_cred()))
864 			continue;
865 		if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags))
866 			continue;
867 		if (nfsd_file_get(nf) != NULL)
868 			return nf;
869 	}
870 	return NULL;
871 }
872 
873 /**
874  * nfsd_file_is_cached - are there any cached open files for this fh?
875  * @inode: inode of the file to check
876  *
877  * Scan the hashtable for open files that match this fh. Returns true if there
878  * are any, and false if not.
879  */
880 bool
881 nfsd_file_is_cached(struct inode *inode)
882 {
883 	bool			ret = false;
884 	struct nfsd_file	*nf;
885 	unsigned int		hashval;
886 
887         hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
888 
889 	rcu_read_lock();
890 	hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
891 				 nf_node) {
892 		if (inode == nf->nf_inode) {
893 			ret = true;
894 			break;
895 		}
896 	}
897 	rcu_read_unlock();
898 	trace_nfsd_file_is_cached(inode, hashval, (int)ret);
899 	return ret;
900 }
901 
902 static __be32
903 nfsd_do_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
904 		     unsigned int may_flags, struct nfsd_file **pnf, bool open)
905 {
906 	__be32	status;
907 	struct net *net = SVC_NET(rqstp);
908 	struct nfsd_file *nf, *new;
909 	struct inode *inode;
910 	unsigned int hashval;
911 	bool retry = true;
912 
913 	/* FIXME: skip this if fh_dentry is already set? */
914 	status = fh_verify(rqstp, fhp, S_IFREG,
915 				may_flags|NFSD_MAY_OWNER_OVERRIDE);
916 	if (status != nfs_ok)
917 		return status;
918 
919 	inode = d_inode(fhp->fh_dentry);
920 	hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
921 retry:
922 	rcu_read_lock();
923 	nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
924 	rcu_read_unlock();
925 	if (nf)
926 		goto wait_for_construction;
927 
928 	new = nfsd_file_alloc(inode, may_flags, hashval, net);
929 	if (!new) {
930 		trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags,
931 					NULL, nfserr_jukebox);
932 		return nfserr_jukebox;
933 	}
934 
935 	spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
936 	nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
937 	if (nf == NULL)
938 		goto open_file;
939 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
940 	nfsd_file_slab_free(&new->nf_rcu);
941 
942 wait_for_construction:
943 	wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
944 
945 	/* Did construction of this file fail? */
946 	if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
947 		if (!retry) {
948 			status = nfserr_jukebox;
949 			goto out;
950 		}
951 		retry = false;
952 		nfsd_file_put_noref(nf);
953 		goto retry;
954 	}
955 
956 	this_cpu_inc(nfsd_file_cache_hits);
957 
958 	if (!(may_flags & NFSD_MAY_NOT_BREAK_LEASE)) {
959 		bool write = (may_flags & NFSD_MAY_WRITE);
960 
961 		if (test_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags) ||
962 		    (test_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags) && write)) {
963 			status = nfserrno(nfsd_open_break_lease(
964 					file_inode(nf->nf_file), may_flags));
965 			if (status == nfs_ok) {
966 				clear_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
967 				if (write)
968 					clear_bit(NFSD_FILE_BREAK_WRITE,
969 						  &nf->nf_flags);
970 			}
971 		}
972 	}
973 out:
974 	if (status == nfs_ok) {
975 		*pnf = nf;
976 	} else {
977 		nfsd_file_put(nf);
978 		nf = NULL;
979 	}
980 
981 	trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, nf, status);
982 	return status;
983 open_file:
984 	nf = new;
985 	/* Take reference for the hashtable */
986 	refcount_inc(&nf->nf_ref);
987 	__set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
988 	__set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
989 	list_lru_add(&nfsd_file_lru, &nf->nf_lru);
990 	hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head);
991 	++nfsd_file_hashtbl[hashval].nfb_count;
992 	nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount,
993 			nfsd_file_hashtbl[hashval].nfb_count);
994 	spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
995 	if (atomic_long_inc_return(&nfsd_filecache_count) >= NFSD_FILE_LRU_THRESHOLD)
996 		nfsd_file_gc();
997 
998 	nf->nf_mark = nfsd_file_mark_find_or_create(nf);
999 	if (nf->nf_mark) {
1000 		if (open) {
1001 			status = nfsd_open_verified(rqstp, fhp, may_flags,
1002 						    &nf->nf_file);
1003 			trace_nfsd_file_open(nf, status);
1004 		} else
1005 			status = nfs_ok;
1006 	} else
1007 		status = nfserr_jukebox;
1008 	/*
1009 	 * If construction failed, or we raced with a call to unlink()
1010 	 * then unhash.
1011 	 */
1012 	if (status != nfs_ok || inode->i_nlink == 0) {
1013 		bool do_free;
1014 		spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
1015 		do_free = nfsd_file_unhash(nf);
1016 		spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
1017 		if (do_free)
1018 			nfsd_file_put_noref(nf);
1019 	}
1020 	clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
1021 	smp_mb__after_atomic();
1022 	wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
1023 	goto out;
1024 }
1025 
1026 /**
1027  * nfsd_file_acquire - Get a struct nfsd_file with an open file
1028  * @rqstp: the RPC transaction being executed
1029  * @fhp: the NFS filehandle of the file to be opened
1030  * @may_flags: NFSD_MAY_ settings for the file
1031  * @pnf: OUT: new or found "struct nfsd_file" object
1032  *
1033  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1034  * network byte order is returned.
1035  */
1036 __be32
1037 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1038 		  unsigned int may_flags, struct nfsd_file **pnf)
1039 {
1040 	return nfsd_do_file_acquire(rqstp, fhp, may_flags, pnf, true);
1041 }
1042 
1043 /**
1044  * nfsd_file_create - Get a struct nfsd_file, do not open
1045  * @rqstp: the RPC transaction being executed
1046  * @fhp: the NFS filehandle of the file just created
1047  * @may_flags: NFSD_MAY_ settings for the file
1048  * @pnf: OUT: new or found "struct nfsd_file" object
1049  *
1050  * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1051  * network byte order is returned.
1052  */
1053 __be32
1054 nfsd_file_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1055 		 unsigned int may_flags, struct nfsd_file **pnf)
1056 {
1057 	return nfsd_do_file_acquire(rqstp, fhp, may_flags, pnf, false);
1058 }
1059 
1060 /*
1061  * Note that fields may be added, removed or reordered in the future. Programs
1062  * scraping this file for info should test the labels to ensure they're
1063  * getting the correct field.
1064  */
1065 static int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
1066 {
1067 	unsigned int i, count = 0, longest = 0;
1068 	unsigned long hits = 0;
1069 
1070 	/*
1071 	 * No need for spinlocks here since we're not terribly interested in
1072 	 * accuracy. We do take the nfsd_mutex simply to ensure that we
1073 	 * don't end up racing with server shutdown
1074 	 */
1075 	mutex_lock(&nfsd_mutex);
1076 	if (nfsd_file_hashtbl) {
1077 		for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
1078 			count += nfsd_file_hashtbl[i].nfb_count;
1079 			longest = max(longest, nfsd_file_hashtbl[i].nfb_count);
1080 		}
1081 	}
1082 	mutex_unlock(&nfsd_mutex);
1083 
1084 	for_each_possible_cpu(i)
1085 		hits += per_cpu(nfsd_file_cache_hits, i);
1086 
1087 	seq_printf(m, "total entries: %u\n", count);
1088 	seq_printf(m, "longest chain: %u\n", longest);
1089 	seq_printf(m, "cache hits:    %lu\n", hits);
1090 	return 0;
1091 }
1092 
1093 int nfsd_file_cache_stats_open(struct inode *inode, struct file *file)
1094 {
1095 	return single_open(file, nfsd_file_cache_stats_show, NULL);
1096 }
1097