4 * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com>
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>
22 #include "filecache.h"
25 #define NFSD_LAUNDRETTE_DELAY (2 * HZ)
27 #define NFSD_FILE_CACHE_UP (0)
29 /* We only care about NFSD_MAY_READ/WRITE for this cache */
30 #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE)
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_pages_flushed);
37 static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions);
39 struct nfsd_fcache_disposal {
40 struct work_struct work;
42 struct list_head freeme;
45 static struct workqueue_struct *nfsd_filecache_wq __read_mostly;
47 static struct kmem_cache *nfsd_file_slab;
48 static struct kmem_cache *nfsd_file_mark_slab;
49 static struct list_lru nfsd_file_lru;
50 static unsigned long nfsd_file_flags;
51 static struct fsnotify_group *nfsd_file_fsnotify_group;
52 static struct delayed_work nfsd_filecache_laundrette;
53 static struct rhashtable nfsd_file_rhash_tbl
54 ____cacheline_aligned_in_smp;
56 enum nfsd_file_lookup_type {
61 struct nfsd_file_lookup_key {
64 const struct cred *cred;
66 enum nfsd_file_lookup_type type;
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.
74 static u32 nfsd_file_inode_hash(const struct inode *inode, u32 seed)
76 unsigned long ptr = (unsigned long)inode;
79 k = ptr >> L1_CACHE_SHIFT;
81 return jhash2(&k, 1, seed);
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
91 * Computed 32-bit hash value
93 static u32 nfsd_file_key_hashfn(const void *data, u32 len, u32 seed)
95 const struct nfsd_file_lookup_key *key = data;
97 return nfsd_file_inode_hash(key->inode, seed);
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
107 * Computed 32-bit hash value
109 static u32 nfsd_file_obj_hashfn(const void *data, u32 len, u32 seed)
111 const struct nfsd_file *nf = data;
113 return nfsd_file_inode_hash(nf->nf_inode, seed);
117 nfsd_match_cred(const struct cred *c1, const struct cred *c2)
121 if (!uid_eq(c1->fsuid, c2->fsuid))
123 if (!gid_eq(c1->fsgid, c2->fsgid))
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)
129 for (i = 0; i < c1->group_info->ngroups; i++) {
130 if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
137 * nfsd_file_obj_cmpfn - Match a cache item against search criteria
138 * @arg: search criteria
139 * @ptr: cache item to check
142 * %0 - Item matches search criteria
143 * %1 - Item does not match search criteria
145 static int nfsd_file_obj_cmpfn(struct rhashtable_compare_arg *arg,
148 const struct nfsd_file_lookup_key *key = arg->key;
149 const struct nfsd_file *nf = ptr;
152 case NFSD_FILE_KEY_INODE:
153 if (nf->nf_inode != key->inode)
156 case NFSD_FILE_KEY_FULL:
157 if (nf->nf_inode != key->inode)
159 if (nf->nf_may != key->need)
161 if (nf->nf_net != key->net)
163 if (!nfsd_match_cred(nf->nf_cred, key->cred))
165 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0)
172 static const struct rhashtable_params nfsd_file_rhash_params = {
173 .key_len = sizeof_field(struct nfsd_file, nf_inode),
174 .key_offset = offsetof(struct nfsd_file, nf_inode),
175 .head_offset = offsetof(struct nfsd_file, nf_rhash),
176 .hashfn = nfsd_file_key_hashfn,
177 .obj_hashfn = nfsd_file_obj_hashfn,
178 .obj_cmpfn = nfsd_file_obj_cmpfn,
179 /* Reduce resizing churn on light workloads */
180 .min_size = 512, /* buckets */
181 .automatic_shrinking = true,
185 nfsd_file_schedule_laundrette(void)
187 if ((atomic_read(&nfsd_file_rhash_tbl.nelems) == 0) ||
188 test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
191 queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
192 NFSD_LAUNDRETTE_DELAY);
196 nfsd_file_slab_free(struct rcu_head *rcu)
198 struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
200 put_cred(nf->nf_cred);
201 kmem_cache_free(nfsd_file_slab, nf);
205 nfsd_file_mark_free(struct fsnotify_mark *mark)
207 struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
210 kmem_cache_free(nfsd_file_mark_slab, nfm);
213 static struct nfsd_file_mark *
214 nfsd_file_mark_get(struct nfsd_file_mark *nfm)
216 if (!refcount_inc_not_zero(&nfm->nfm_ref))
222 nfsd_file_mark_put(struct nfsd_file_mark *nfm)
224 if (refcount_dec_and_test(&nfm->nfm_ref)) {
225 fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
226 fsnotify_put_mark(&nfm->nfm_mark);
230 static struct nfsd_file_mark *
231 nfsd_file_mark_find_or_create(struct nfsd_file *nf, struct inode *inode)
234 struct fsnotify_mark *mark;
235 struct nfsd_file_mark *nfm = NULL, *new;
238 fsnotify_group_lock(nfsd_file_fsnotify_group);
239 mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
240 nfsd_file_fsnotify_group);
242 nfm = nfsd_file_mark_get(container_of(mark,
243 struct nfsd_file_mark,
245 fsnotify_group_unlock(nfsd_file_fsnotify_group);
247 fsnotify_put_mark(mark);
250 /* Avoid soft lockup race with nfsd_file_mark_put() */
251 fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
252 fsnotify_put_mark(mark);
254 fsnotify_group_unlock(nfsd_file_fsnotify_group);
257 /* allocate a new nfm */
258 new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
261 fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
262 new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
263 refcount_set(&new->nfm_ref, 1);
265 err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
268 * If the add was successful, then return the object.
269 * Otherwise, we need to put the reference we hold on the
270 * nfm_mark. The fsnotify code will take a reference and put
271 * it on failure, so we can't just free it directly. It's also
272 * not safe to call fsnotify_destroy_mark on it as the
273 * mark->group will be NULL. Thus, we can't let the nfm_ref
274 * counter drive the destruction at this point.
279 fsnotify_put_mark(&new->nfm_mark);
280 } while (unlikely(err == -EEXIST));
285 static struct nfsd_file *
286 nfsd_file_alloc(struct nfsd_file_lookup_key *key, unsigned int may)
288 struct nfsd_file *nf;
290 nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
292 INIT_LIST_HEAD(&nf->nf_lru);
293 nf->nf_birthtime = ktime_get();
295 nf->nf_cred = get_current_cred();
296 nf->nf_net = key->net;
298 __set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
299 __set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
300 nf->nf_inode = key->inode;
301 /* nf_ref is pre-incremented for hash table */
302 refcount_set(&nf->nf_ref, 2);
303 nf->nf_may = key->need;
310 nfsd_file_free(struct nfsd_file *nf)
312 s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime));
315 this_cpu_inc(nfsd_file_releases);
316 this_cpu_add(nfsd_file_total_age, age);
318 trace_nfsd_file_put_final(nf);
320 nfsd_file_mark_put(nf->nf_mark);
322 get_file(nf->nf_file);
323 filp_close(nf->nf_file, NULL);
329 * If this item is still linked via nf_lru, that's a bug.
330 * WARN and leak it to preserve system stability.
332 if (WARN_ON_ONCE(!list_empty(&nf->nf_lru)))
335 call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
340 nfsd_file_check_writeback(struct nfsd_file *nf)
342 struct file *file = nf->nf_file;
343 struct address_space *mapping;
345 if (!file || !(file->f_mode & FMODE_WRITE))
347 mapping = file->f_mapping;
348 return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
349 mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
353 nfsd_file_check_write_error(struct nfsd_file *nf)
355 struct file *file = nf->nf_file;
357 if (!file || !(file->f_mode & FMODE_WRITE))
359 return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
363 nfsd_file_flush(struct nfsd_file *nf)
365 struct file *file = nf->nf_file;
367 if (!file || !(file->f_mode & FMODE_WRITE))
369 this_cpu_add(nfsd_file_pages_flushed, file->f_mapping->nrpages);
370 if (vfs_fsync(file, 1) != 0)
371 nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
374 static void nfsd_file_lru_add(struct nfsd_file *nf)
376 set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
377 if (list_lru_add(&nfsd_file_lru, &nf->nf_lru))
378 trace_nfsd_file_lru_add(nf);
381 static void nfsd_file_lru_remove(struct nfsd_file *nf)
383 if (list_lru_del(&nfsd_file_lru, &nf->nf_lru))
384 trace_nfsd_file_lru_del(nf);
388 nfsd_file_hash_remove(struct nfsd_file *nf)
390 trace_nfsd_file_unhash(nf);
392 if (nfsd_file_check_write_error(nf))
393 nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
394 rhashtable_remove_fast(&nfsd_file_rhash_tbl, &nf->nf_rhash,
395 nfsd_file_rhash_params);
399 nfsd_file_unhash(struct nfsd_file *nf)
401 if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
402 nfsd_file_hash_remove(nf);
409 nfsd_file_unhash_and_dispose(struct nfsd_file *nf, struct list_head *dispose)
411 trace_nfsd_file_unhash_and_dispose(nf);
412 if (nfsd_file_unhash(nf)) {
413 /* caller must call nfsd_file_dispose_list() later */
414 nfsd_file_lru_remove(nf);
415 list_add(&nf->nf_lru, dispose);
420 nfsd_file_put_noref(struct nfsd_file *nf)
422 trace_nfsd_file_put(nf);
424 if (refcount_dec_and_test(&nf->nf_ref)) {
425 WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
426 nfsd_file_lru_remove(nf);
432 nfsd_file_put(struct nfsd_file *nf)
436 nfsd_file_lru_add(nf);
437 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) {
439 nfsd_file_put_noref(nf);
440 } else if (nf->nf_file) {
441 nfsd_file_put_noref(nf);
442 nfsd_file_schedule_laundrette();
444 nfsd_file_put_noref(nf);
448 * nfsd_file_close - Close an nfsd_file
449 * @nf: nfsd_file to close
451 * If this is the final reference for @nf, free it immediately.
452 * This reflects an on-the-wire CLOSE or DELEGRETURN into the
453 * VFS and exported filesystem.
455 void nfsd_file_close(struct nfsd_file *nf)
458 if (refcount_dec_if_one(&nf->nf_ref)) {
459 nfsd_file_unhash(nf);
460 nfsd_file_lru_remove(nf);
466 nfsd_file_get(struct nfsd_file *nf)
468 if (likely(refcount_inc_not_zero(&nf->nf_ref)))
474 nfsd_file_dispose_list(struct list_head *dispose)
476 struct nfsd_file *nf;
478 while(!list_empty(dispose)) {
479 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
480 list_del_init(&nf->nf_lru);
482 nfsd_file_put_noref(nf);
487 nfsd_file_dispose_list_sync(struct list_head *dispose)
490 struct nfsd_file *nf;
492 while(!list_empty(dispose)) {
493 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
494 list_del_init(&nf->nf_lru);
496 if (!refcount_dec_and_test(&nf->nf_ref))
498 if (nfsd_file_free(nf))
502 flush_delayed_fput();
506 nfsd_file_list_remove_disposal(struct list_head *dst,
507 struct nfsd_fcache_disposal *l)
510 list_splice_init(&l->freeme, dst);
511 spin_unlock(&l->lock);
515 nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
517 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
518 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
521 list_splice_tail_init(files, &l->freeme);
522 spin_unlock(&l->lock);
523 queue_work(nfsd_filecache_wq, &l->work);
527 nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
530 struct nfsd_file *nf, *tmp;
532 list_for_each_entry_safe(nf, tmp, src, nf_lru) {
533 if (nf->nf_net == net)
534 list_move_tail(&nf->nf_lru, dst);
539 nfsd_file_dispose_list_delayed(struct list_head *dispose)
542 struct nfsd_file *nf;
544 while(!list_empty(dispose)) {
545 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
546 nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
547 nfsd_file_list_add_disposal(&list, nf->nf_net);
552 * nfsd_file_lru_cb - Examine an entry on the LRU list
553 * @item: LRU entry to examine
554 * @lru: controlling LRU
555 * @lock: LRU list lock (unused)
559 * %LRU_REMOVED: @item was removed from the LRU
560 * %LRU_ROTATE: @item is to be moved to the LRU tail
561 * %LRU_SKIP: @item cannot be evicted
563 static enum lru_status
564 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
565 spinlock_t *lock, void *arg)
569 struct list_head *head = arg;
570 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
573 * Do a lockless refcount check. The hashtable holds one reference, so
574 * we look to see if anything else has a reference, or if any have
575 * been put since the shrinker last ran. Those don't get unhashed and
578 * Note that in the put path, we set the flag and then decrement the
579 * counter. Here we check the counter and then test and clear the flag.
580 * That order is deliberate to ensure that we can do this locklessly.
582 if (refcount_read(&nf->nf_ref) > 1) {
583 list_lru_isolate(lru, &nf->nf_lru);
584 trace_nfsd_file_gc_in_use(nf);
589 * Don't throw out files that are still undergoing I/O or
590 * that have uncleared errors pending.
592 if (nfsd_file_check_writeback(nf)) {
593 trace_nfsd_file_gc_writeback(nf);
597 if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
598 trace_nfsd_file_gc_referenced(nf);
602 if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
603 trace_nfsd_file_gc_hashed(nf);
607 list_lru_isolate_move(lru, &nf->nf_lru, head);
608 this_cpu_inc(nfsd_file_evictions);
609 trace_nfsd_file_gc_disposed(nf);
614 * Unhash items on @dispose immediately, then queue them on the
615 * disposal workqueue to finish releasing them in the background.
617 * cel: Note that between the time list_lru_shrink_walk runs and
618 * now, these items are in the hash table but marked unhashed.
619 * Why release these outside of lru_cb ? There's no lock ordering
620 * problem since lru_cb currently takes no lock.
622 static void nfsd_file_gc_dispose_list(struct list_head *dispose)
624 struct nfsd_file *nf;
626 list_for_each_entry(nf, dispose, nf_lru)
627 nfsd_file_hash_remove(nf);
628 nfsd_file_dispose_list_delayed(dispose);
637 ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
638 &dispose, list_lru_count(&nfsd_file_lru));
639 trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
640 nfsd_file_gc_dispose_list(&dispose);
644 nfsd_file_gc_worker(struct work_struct *work)
647 nfsd_file_schedule_laundrette();
651 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
653 return list_lru_count(&nfsd_file_lru);
657 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
662 ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
663 nfsd_file_lru_cb, &dispose);
664 trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
665 nfsd_file_gc_dispose_list(&dispose);
669 static struct shrinker nfsd_file_shrinker = {
670 .scan_objects = nfsd_file_lru_scan,
671 .count_objects = nfsd_file_lru_count,
676 * Find all cache items across all net namespaces that match @inode and
677 * move them to @dispose. The lookup is atomic wrt nfsd_file_acquire().
680 __nfsd_file_close_inode(struct inode *inode, struct list_head *dispose)
682 struct nfsd_file_lookup_key key = {
683 .type = NFSD_FILE_KEY_INODE,
686 unsigned int count = 0;
687 struct nfsd_file *nf;
691 nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
692 nfsd_file_rhash_params);
695 nfsd_file_unhash_and_dispose(nf, dispose);
703 * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
704 * @inode: inode of the file to attempt to remove
706 * Unhash and put, then flush and fput all cache items associated with @inode.
709 nfsd_file_close_inode_sync(struct inode *inode)
714 count = __nfsd_file_close_inode(inode, &dispose);
715 trace_nfsd_file_close_inode_sync(inode, count);
716 nfsd_file_dispose_list_sync(&dispose);
720 * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
721 * @inode: inode of the file to attempt to remove
723 * Unhash and put all cache item associated with @inode.
726 nfsd_file_close_inode(struct inode *inode)
731 count = __nfsd_file_close_inode(inode, &dispose);
732 trace_nfsd_file_close_inode(inode, count);
733 nfsd_file_dispose_list_delayed(&dispose);
737 * nfsd_file_delayed_close - close unused nfsd_files
740 * Walk the LRU list and close any entries that have not been used since
744 nfsd_file_delayed_close(struct work_struct *work)
747 struct nfsd_fcache_disposal *l = container_of(work,
748 struct nfsd_fcache_disposal, work);
750 nfsd_file_list_remove_disposal(&head, l);
751 nfsd_file_dispose_list(&head);
755 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
758 struct file_lock *fl = data;
760 /* Only close files for F_SETLEASE leases */
761 if (fl->fl_flags & FL_LEASE)
762 nfsd_file_close_inode_sync(file_inode(fl->fl_file));
766 static struct notifier_block nfsd_file_lease_notifier = {
767 .notifier_call = nfsd_file_lease_notifier_call,
771 nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
772 struct inode *inode, struct inode *dir,
773 const struct qstr *name, u32 cookie)
775 if (WARN_ON_ONCE(!inode))
778 trace_nfsd_file_fsnotify_handle_event(inode, mask);
780 /* Should be no marks on non-regular files */
781 if (!S_ISREG(inode->i_mode)) {
786 /* don't close files if this was not the last link */
787 if (mask & FS_ATTRIB) {
792 nfsd_file_close_inode(inode);
797 static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
798 .handle_inode_event = nfsd_file_fsnotify_handle_event,
799 .free_mark = nfsd_file_mark_free,
803 nfsd_file_cache_init(void)
807 lockdep_assert_held(&nfsd_mutex);
808 if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
811 ret = rhashtable_init(&nfsd_file_rhash_tbl, &nfsd_file_rhash_params);
816 nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
817 if (!nfsd_filecache_wq)
820 nfsd_file_slab = kmem_cache_create("nfsd_file",
821 sizeof(struct nfsd_file), 0, 0, NULL);
822 if (!nfsd_file_slab) {
823 pr_err("nfsd: unable to create nfsd_file_slab\n");
827 nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
828 sizeof(struct nfsd_file_mark), 0, 0, NULL);
829 if (!nfsd_file_mark_slab) {
830 pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
835 ret = list_lru_init(&nfsd_file_lru);
837 pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
841 ret = register_shrinker(&nfsd_file_shrinker, "nfsd-filecache");
843 pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
847 ret = lease_register_notifier(&nfsd_file_lease_notifier);
849 pr_err("nfsd: unable to register lease notifier: %d\n", ret);
853 nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
854 FSNOTIFY_GROUP_NOFS);
855 if (IS_ERR(nfsd_file_fsnotify_group)) {
856 pr_err("nfsd: unable to create fsnotify group: %ld\n",
857 PTR_ERR(nfsd_file_fsnotify_group));
858 ret = PTR_ERR(nfsd_file_fsnotify_group);
859 nfsd_file_fsnotify_group = NULL;
863 INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
867 lease_unregister_notifier(&nfsd_file_lease_notifier);
869 unregister_shrinker(&nfsd_file_shrinker);
871 list_lru_destroy(&nfsd_file_lru);
873 kmem_cache_destroy(nfsd_file_slab);
874 nfsd_file_slab = NULL;
875 kmem_cache_destroy(nfsd_file_mark_slab);
876 nfsd_file_mark_slab = NULL;
877 destroy_workqueue(nfsd_filecache_wq);
878 nfsd_filecache_wq = NULL;
879 rhashtable_destroy(&nfsd_file_rhash_tbl);
884 __nfsd_file_cache_purge(struct net *net)
886 struct rhashtable_iter iter;
887 struct nfsd_file *nf;
890 rhashtable_walk_enter(&nfsd_file_rhash_tbl, &iter);
892 rhashtable_walk_start(&iter);
894 nf = rhashtable_walk_next(&iter);
895 while (!IS_ERR_OR_NULL(nf)) {
896 if (!net || nf->nf_net == net)
897 nfsd_file_unhash_and_dispose(nf, &dispose);
898 nf = rhashtable_walk_next(&iter);
901 rhashtable_walk_stop(&iter);
902 } while (nf == ERR_PTR(-EAGAIN));
903 rhashtable_walk_exit(&iter);
905 nfsd_file_dispose_list(&dispose);
908 static struct nfsd_fcache_disposal *
909 nfsd_alloc_fcache_disposal(void)
911 struct nfsd_fcache_disposal *l;
913 l = kmalloc(sizeof(*l), GFP_KERNEL);
916 INIT_WORK(&l->work, nfsd_file_delayed_close);
917 spin_lock_init(&l->lock);
918 INIT_LIST_HEAD(&l->freeme);
923 nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
925 cancel_work_sync(&l->work);
926 nfsd_file_dispose_list(&l->freeme);
931 nfsd_free_fcache_disposal_net(struct net *net)
933 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
934 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
936 nfsd_free_fcache_disposal(l);
940 nfsd_file_cache_start_net(struct net *net)
942 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
944 nn->fcache_disposal = nfsd_alloc_fcache_disposal();
945 return nn->fcache_disposal ? 0 : -ENOMEM;
949 * nfsd_file_cache_purge - Remove all cache items associated with @net
950 * @net: target net namespace
954 nfsd_file_cache_purge(struct net *net)
956 lockdep_assert_held(&nfsd_mutex);
957 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
958 __nfsd_file_cache_purge(net);
962 nfsd_file_cache_shutdown_net(struct net *net)
964 nfsd_file_cache_purge(net);
965 nfsd_free_fcache_disposal_net(net);
969 nfsd_file_cache_shutdown(void)
973 lockdep_assert_held(&nfsd_mutex);
974 if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
977 lease_unregister_notifier(&nfsd_file_lease_notifier);
978 unregister_shrinker(&nfsd_file_shrinker);
980 * make sure all callers of nfsd_file_lru_cb are done before
981 * calling nfsd_file_cache_purge
983 cancel_delayed_work_sync(&nfsd_filecache_laundrette);
984 __nfsd_file_cache_purge(NULL);
985 list_lru_destroy(&nfsd_file_lru);
987 fsnotify_put_group(nfsd_file_fsnotify_group);
988 nfsd_file_fsnotify_group = NULL;
989 kmem_cache_destroy(nfsd_file_slab);
990 nfsd_file_slab = NULL;
991 fsnotify_wait_marks_destroyed();
992 kmem_cache_destroy(nfsd_file_mark_slab);
993 nfsd_file_mark_slab = NULL;
994 destroy_workqueue(nfsd_filecache_wq);
995 nfsd_filecache_wq = NULL;
996 rhashtable_destroy(&nfsd_file_rhash_tbl);
998 for_each_possible_cpu(i) {
999 per_cpu(nfsd_file_cache_hits, i) = 0;
1000 per_cpu(nfsd_file_acquisitions, i) = 0;
1001 per_cpu(nfsd_file_releases, i) = 0;
1002 per_cpu(nfsd_file_total_age, i) = 0;
1003 per_cpu(nfsd_file_pages_flushed, i) = 0;
1004 per_cpu(nfsd_file_evictions, i) = 0;
1009 * nfsd_file_is_cached - are there any cached open files for this inode?
1010 * @inode: inode to check
1012 * The lookup matches inodes in all net namespaces and is atomic wrt
1013 * nfsd_file_acquire().
1016 * %true: filecache contains at least one file matching this inode
1017 * %false: filecache contains no files matching this inode
1020 nfsd_file_is_cached(struct inode *inode)
1022 struct nfsd_file_lookup_key key = {
1023 .type = NFSD_FILE_KEY_INODE,
1028 if (rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key,
1029 nfsd_file_rhash_params) != NULL)
1031 trace_nfsd_file_is_cached(inode, (int)ret);
1036 nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1037 unsigned int may_flags, struct nfsd_file **pnf, bool open)
1039 struct nfsd_file_lookup_key key = {
1040 .type = NFSD_FILE_KEY_FULL,
1041 .need = may_flags & NFSD_FILE_MAY_MASK,
1042 .net = SVC_NET(rqstp),
1044 bool open_retry = true;
1045 struct nfsd_file *nf;
1049 status = fh_verify(rqstp, fhp, S_IFREG,
1050 may_flags|NFSD_MAY_OWNER_OVERRIDE);
1051 if (status != nfs_ok)
1053 key.inode = d_inode(fhp->fh_dentry);
1054 key.cred = get_current_cred();
1058 nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key,
1059 nfsd_file_rhash_params);
1061 nf = nfsd_file_get(nf);
1064 goto wait_for_construction;
1066 nf = nfsd_file_alloc(&key, may_flags);
1068 status = nfserr_jukebox;
1072 ret = rhashtable_lookup_insert_key(&nfsd_file_rhash_tbl,
1073 &key, &nf->nf_rhash,
1074 nfsd_file_rhash_params);
1075 if (likely(ret == 0))
1078 nfsd_file_slab_free(&nf->nf_rcu);
1082 trace_nfsd_file_insert_err(rqstp, key.inode, may_flags, ret);
1083 status = nfserr_jukebox;
1086 wait_for_construction:
1087 wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
1089 /* Did construction of this file fail? */
1090 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
1091 trace_nfsd_file_cons_err(rqstp, key.inode, may_flags, nf);
1093 status = nfserr_jukebox;
1097 nfsd_file_put_noref(nf);
1101 nfsd_file_lru_remove(nf);
1102 this_cpu_inc(nfsd_file_cache_hits);
1104 status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
1106 if (status == nfs_ok) {
1108 this_cpu_inc(nfsd_file_acquisitions);
1118 trace_nfsd_file_acquire(rqstp, key.inode, may_flags, nf, status);
1122 trace_nfsd_file_alloc(nf);
1123 nf->nf_mark = nfsd_file_mark_find_or_create(nf, key.inode);
1126 status = nfsd_open_verified(rqstp, fhp, may_flags,
1128 trace_nfsd_file_open(nf, status);
1132 status = nfserr_jukebox;
1134 * If construction failed, or we raced with a call to unlink()
1137 if (status != nfs_ok || key.inode->i_nlink == 0)
1138 if (nfsd_file_unhash(nf))
1139 nfsd_file_put_noref(nf);
1140 clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
1141 smp_mb__after_atomic();
1142 wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
1147 * nfsd_file_acquire - Get a struct nfsd_file with an open file
1148 * @rqstp: the RPC transaction being executed
1149 * @fhp: the NFS filehandle of the file to be opened
1150 * @may_flags: NFSD_MAY_ settings for the file
1151 * @pnf: OUT: new or found "struct nfsd_file" object
1153 * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1154 * network byte order is returned.
1157 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1158 unsigned int may_flags, struct nfsd_file **pnf)
1160 return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, true);
1164 * nfsd_file_create - Get a struct nfsd_file, do not open
1165 * @rqstp: the RPC transaction being executed
1166 * @fhp: the NFS filehandle of the file just created
1167 * @may_flags: NFSD_MAY_ settings for the file
1168 * @pnf: OUT: new or found "struct nfsd_file" object
1170 * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1171 * network byte order is returned.
1174 nfsd_file_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1175 unsigned int may_flags, struct nfsd_file **pnf)
1177 return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, false);
1181 * Note that fields may be added, removed or reordered in the future. Programs
1182 * scraping this file for info should test the labels to ensure they're
1183 * getting the correct field.
1185 int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
1187 unsigned long releases = 0, pages_flushed = 0, evictions = 0;
1188 unsigned long hits = 0, acquisitions = 0;
1189 unsigned int i, count = 0, buckets = 0;
1190 unsigned long lru = 0, total_age = 0;
1192 /* Serialize with server shutdown */
1193 mutex_lock(&nfsd_mutex);
1194 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
1195 struct bucket_table *tbl;
1196 struct rhashtable *ht;
1198 lru = list_lru_count(&nfsd_file_lru);
1201 ht = &nfsd_file_rhash_tbl;
1202 count = atomic_read(&ht->nelems);
1203 tbl = rht_dereference_rcu(ht->tbl, ht);
1204 buckets = tbl->size;
1207 mutex_unlock(&nfsd_mutex);
1209 for_each_possible_cpu(i) {
1210 hits += per_cpu(nfsd_file_cache_hits, i);
1211 acquisitions += per_cpu(nfsd_file_acquisitions, i);
1212 releases += per_cpu(nfsd_file_releases, i);
1213 total_age += per_cpu(nfsd_file_total_age, i);
1214 evictions += per_cpu(nfsd_file_evictions, i);
1215 pages_flushed += per_cpu(nfsd_file_pages_flushed, i);
1218 seq_printf(m, "total entries: %u\n", count);
1219 seq_printf(m, "hash buckets: %u\n", buckets);
1220 seq_printf(m, "lru entries: %lu\n", lru);
1221 seq_printf(m, "cache hits: %lu\n", hits);
1222 seq_printf(m, "acquisitions: %lu\n", acquisitions);
1223 seq_printf(m, "releases: %lu\n", releases);
1224 seq_printf(m, "evictions: %lu\n", evictions);
1226 seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1228 seq_printf(m, "mean age (ms): -\n");
1229 seq_printf(m, "pages flushed: %lu\n", pages_flushed);