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>
21 #include "filecache.h"
24 #define NFSDDBG_FACILITY NFSDDBG_FH
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)
31 #define NFSD_FILE_SHUTDOWN (1)
32 #define NFSD_FILE_LRU_THRESHOLD (4096UL)
33 #define NFSD_FILE_LRU_LIMIT (NFSD_FILE_LRU_THRESHOLD << 2)
35 /* We only care about NFSD_MAY_READ/WRITE for this cache */
36 #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE)
38 struct nfsd_fcache_bucket {
39 struct hlist_head nfb_head;
41 unsigned int nfb_count;
42 unsigned int nfb_maxcount;
45 static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
47 struct nfsd_fcache_disposal {
48 struct work_struct work;
50 struct list_head freeme;
53 static struct workqueue_struct *nfsd_filecache_wq __read_mostly;
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;
64 static void nfsd_file_gc(void);
67 nfsd_file_schedule_laundrette(void)
69 long count = atomic_long_read(&nfsd_filecache_count);
71 if (count == 0 || test_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags))
74 queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
75 NFSD_LAUNDRETTE_DELAY);
79 nfsd_file_slab_free(struct rcu_head *rcu)
81 struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
83 put_cred(nf->nf_cred);
84 kmem_cache_free(nfsd_file_slab, nf);
88 nfsd_file_mark_free(struct fsnotify_mark *mark)
90 struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
93 kmem_cache_free(nfsd_file_mark_slab, nfm);
96 static struct nfsd_file_mark *
97 nfsd_file_mark_get(struct nfsd_file_mark *nfm)
99 if (!refcount_inc_not_zero(&nfm->nfm_ref))
105 nfsd_file_mark_put(struct nfsd_file_mark *nfm)
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);
113 static struct nfsd_file_mark *
114 nfsd_file_mark_find_or_create(struct nfsd_file *nf)
117 struct fsnotify_mark *mark;
118 struct nfsd_file_mark *nfm = NULL, *new;
119 struct inode *inode = nf->nf_inode;
122 fsnotify_group_lock(nfsd_file_fsnotify_group);
123 mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
124 nfsd_file_fsnotify_group);
126 nfm = nfsd_file_mark_get(container_of(mark,
127 struct nfsd_file_mark,
129 fsnotify_group_unlock(nfsd_file_fsnotify_group);
131 fsnotify_put_mark(mark);
134 /* Avoid soft lockup race with nfsd_file_mark_put() */
135 fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
136 fsnotify_put_mark(mark);
138 fsnotify_group_unlock(nfsd_file_fsnotify_group);
141 /* allocate a new nfm */
142 new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
145 fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
146 new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
147 refcount_set(&new->nfm_ref, 1);
149 err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
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.
163 fsnotify_put_mark(&new->nfm_mark);
164 } while (unlikely(err == -EEXIST));
169 static struct nfsd_file *
170 nfsd_file_alloc(struct inode *inode, unsigned int may, unsigned int hashval,
173 struct nfsd_file *nf;
175 nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
177 INIT_HLIST_NODE(&nf->nf_node);
178 INIT_LIST_HEAD(&nf->nf_lru);
180 nf->nf_cred = get_current_cred();
183 nf->nf_inode = inode;
184 nf->nf_hashval = hashval;
185 refcount_set(&nf->nf_ref, 1);
186 nf->nf_may = may & NFSD_FILE_MAY_MASK;
187 if (may & NFSD_MAY_NOT_BREAK_LEASE) {
188 if (may & NFSD_MAY_WRITE)
189 __set_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags);
190 if (may & NFSD_MAY_READ)
191 __set_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
194 trace_nfsd_file_alloc(nf);
200 nfsd_file_free(struct nfsd_file *nf)
204 trace_nfsd_file_put_final(nf);
206 nfsd_file_mark_put(nf->nf_mark);
208 get_file(nf->nf_file);
209 filp_close(nf->nf_file, NULL);
213 call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
218 nfsd_file_check_writeback(struct nfsd_file *nf)
220 struct file *file = nf->nf_file;
221 struct address_space *mapping;
223 if (!file || !(file->f_mode & FMODE_WRITE))
225 mapping = file->f_mapping;
226 return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
227 mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
231 nfsd_file_check_write_error(struct nfsd_file *nf)
233 struct file *file = nf->nf_file;
235 if (!file || !(file->f_mode & FMODE_WRITE))
237 return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err));
241 nfsd_file_flush(struct nfsd_file *nf)
243 if (nf->nf_file && vfs_fsync(nf->nf_file, 1) != 0)
244 nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
248 nfsd_file_do_unhash(struct nfsd_file *nf)
250 lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
252 trace_nfsd_file_unhash(nf);
254 if (nfsd_file_check_write_error(nf))
255 nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
256 --nfsd_file_hashtbl[nf->nf_hashval].nfb_count;
257 hlist_del_rcu(&nf->nf_node);
258 atomic_long_dec(&nfsd_filecache_count);
262 nfsd_file_unhash(struct nfsd_file *nf)
264 if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
265 nfsd_file_do_unhash(nf);
266 if (!list_empty(&nf->nf_lru))
267 list_lru_del(&nfsd_file_lru, &nf->nf_lru);
274 * Return true if the file was unhashed.
277 nfsd_file_unhash_and_release_locked(struct nfsd_file *nf, struct list_head *dispose)
279 lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
281 trace_nfsd_file_unhash_and_release_locked(nf);
282 if (!nfsd_file_unhash(nf))
284 /* keep final reference for nfsd_file_lru_dispose */
285 if (refcount_dec_not_one(&nf->nf_ref))
288 list_add(&nf->nf_lru, dispose);
293 nfsd_file_put_noref(struct nfsd_file *nf)
295 trace_nfsd_file_put(nf);
297 if (refcount_dec_and_test(&nf->nf_ref)) {
298 WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
304 nfsd_file_put(struct nfsd_file *nf)
308 set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
309 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) {
311 nfsd_file_put_noref(nf);
312 } else if (nf->nf_file) {
313 nfsd_file_put_noref(nf);
314 nfsd_file_schedule_laundrette();
316 nfsd_file_put_noref(nf);
318 if (atomic_long_read(&nfsd_filecache_count) >= NFSD_FILE_LRU_LIMIT)
323 nfsd_file_get(struct nfsd_file *nf)
325 if (likely(refcount_inc_not_zero(&nf->nf_ref)))
331 nfsd_file_dispose_list(struct list_head *dispose)
333 struct nfsd_file *nf;
335 while(!list_empty(dispose)) {
336 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
337 list_del(&nf->nf_lru);
339 nfsd_file_put_noref(nf);
344 nfsd_file_dispose_list_sync(struct list_head *dispose)
347 struct nfsd_file *nf;
349 while(!list_empty(dispose)) {
350 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
351 list_del(&nf->nf_lru);
353 if (!refcount_dec_and_test(&nf->nf_ref))
355 if (nfsd_file_free(nf))
359 flush_delayed_fput();
363 nfsd_file_list_remove_disposal(struct list_head *dst,
364 struct nfsd_fcache_disposal *l)
367 list_splice_init(&l->freeme, dst);
368 spin_unlock(&l->lock);
372 nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
374 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
375 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
378 list_splice_tail_init(files, &l->freeme);
379 spin_unlock(&l->lock);
380 queue_work(nfsd_filecache_wq, &l->work);
384 nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
387 struct nfsd_file *nf, *tmp;
389 list_for_each_entry_safe(nf, tmp, src, nf_lru) {
390 if (nf->nf_net == net)
391 list_move_tail(&nf->nf_lru, dst);
396 nfsd_file_dispose_list_delayed(struct list_head *dispose)
399 struct nfsd_file *nf;
401 while(!list_empty(dispose)) {
402 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
403 nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
404 nfsd_file_list_add_disposal(&list, nf->nf_net);
409 * Note this can deadlock with nfsd_file_cache_purge.
411 static enum lru_status
412 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
413 spinlock_t *lock, void *arg)
417 struct list_head *head = arg;
418 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
421 * Do a lockless refcount check. The hashtable holds one reference, so
422 * we look to see if anything else has a reference, or if any have
423 * been put since the shrinker last ran. Those don't get unhashed and
426 * Note that in the put path, we set the flag and then decrement the
427 * counter. Here we check the counter and then test and clear the flag.
428 * That order is deliberate to ensure that we can do this locklessly.
430 if (refcount_read(&nf->nf_ref) > 1)
434 * Don't throw out files that are still undergoing I/O or
435 * that have uncleared errors pending.
437 if (nfsd_file_check_writeback(nf))
440 if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags))
443 if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags))
446 list_lru_isolate_move(lru, &nf->nf_lru, head);
453 nfsd_file_lru_walk_list(struct shrink_control *sc)
456 struct nfsd_file *nf;
460 ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
461 nfsd_file_lru_cb, &head);
463 ret = list_lru_walk(&nfsd_file_lru,
466 list_for_each_entry(nf, &head, nf_lru) {
467 spin_lock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
468 nfsd_file_do_unhash(nf);
469 spin_unlock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
471 nfsd_file_dispose_list_delayed(&head);
478 nfsd_file_lru_walk_list(NULL);
482 nfsd_file_gc_worker(struct work_struct *work)
485 nfsd_file_schedule_laundrette();
489 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
491 return list_lru_count(&nfsd_file_lru);
495 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
497 return nfsd_file_lru_walk_list(sc);
500 static struct shrinker nfsd_file_shrinker = {
501 .scan_objects = nfsd_file_lru_scan,
502 .count_objects = nfsd_file_lru_count,
507 __nfsd_file_close_inode(struct inode *inode, unsigned int hashval,
508 struct list_head *dispose)
510 struct nfsd_file *nf;
511 struct hlist_node *tmp;
513 spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
514 hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) {
515 if (inode == nf->nf_inode)
516 nfsd_file_unhash_and_release_locked(nf, dispose);
518 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
522 * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
523 * @inode: inode of the file to attempt to remove
525 * Walk the whole hash bucket, looking for any files that correspond to "inode".
526 * If any do, then unhash them and put the hashtable reference to them and
527 * destroy any that had their last reference put. Also ensure that any of the
528 * fputs also have their final __fput done as well.
531 nfsd_file_close_inode_sync(struct inode *inode)
533 unsigned int hashval = (unsigned int)hash_long(inode->i_ino,
534 NFSD_FILE_HASH_BITS);
537 __nfsd_file_close_inode(inode, hashval, &dispose);
538 trace_nfsd_file_close_inode_sync(inode, hashval, !list_empty(&dispose));
539 nfsd_file_dispose_list_sync(&dispose);
543 * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
544 * @inode: inode of the file to attempt to remove
546 * Walk the whole hash bucket, looking for any files that correspond to "inode".
547 * If any do, then unhash them and put the hashtable reference to them and
548 * destroy any that had their last reference put.
551 nfsd_file_close_inode(struct inode *inode)
553 unsigned int hashval = (unsigned int)hash_long(inode->i_ino,
554 NFSD_FILE_HASH_BITS);
557 __nfsd_file_close_inode(inode, hashval, &dispose);
558 trace_nfsd_file_close_inode(inode, hashval, !list_empty(&dispose));
559 nfsd_file_dispose_list_delayed(&dispose);
563 * nfsd_file_delayed_close - close unused nfsd_files
566 * Walk the LRU list and close any entries that have not been used since
569 * Note this can deadlock with nfsd_file_cache_purge.
572 nfsd_file_delayed_close(struct work_struct *work)
575 struct nfsd_fcache_disposal *l = container_of(work,
576 struct nfsd_fcache_disposal, work);
578 nfsd_file_list_remove_disposal(&head, l);
579 nfsd_file_dispose_list(&head);
583 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
586 struct file_lock *fl = data;
588 /* Only close files for F_SETLEASE leases */
589 if (fl->fl_flags & FL_LEASE)
590 nfsd_file_close_inode_sync(file_inode(fl->fl_file));
594 static struct notifier_block nfsd_file_lease_notifier = {
595 .notifier_call = nfsd_file_lease_notifier_call,
599 nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
600 struct inode *inode, struct inode *dir,
601 const struct qstr *name, u32 cookie)
603 if (WARN_ON_ONCE(!inode))
606 trace_nfsd_file_fsnotify_handle_event(inode, mask);
608 /* Should be no marks on non-regular files */
609 if (!S_ISREG(inode->i_mode)) {
614 /* don't close files if this was not the last link */
615 if (mask & FS_ATTRIB) {
620 nfsd_file_close_inode(inode);
625 static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
626 .handle_inode_event = nfsd_file_fsnotify_handle_event,
627 .free_mark = nfsd_file_mark_free,
631 nfsd_file_cache_init(void)
636 clear_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
638 if (nfsd_file_hashtbl)
641 nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0);
642 if (!nfsd_filecache_wq)
645 nfsd_file_hashtbl = kvcalloc(NFSD_FILE_HASH_SIZE,
646 sizeof(*nfsd_file_hashtbl), GFP_KERNEL);
647 if (!nfsd_file_hashtbl) {
648 pr_err("nfsd: unable to allocate nfsd_file_hashtbl\n");
652 nfsd_file_slab = kmem_cache_create("nfsd_file",
653 sizeof(struct nfsd_file), 0, 0, NULL);
654 if (!nfsd_file_slab) {
655 pr_err("nfsd: unable to create nfsd_file_slab\n");
659 nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark",
660 sizeof(struct nfsd_file_mark), 0, 0, NULL);
661 if (!nfsd_file_mark_slab) {
662 pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
667 ret = list_lru_init(&nfsd_file_lru);
669 pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
673 ret = register_shrinker(&nfsd_file_shrinker);
675 pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret);
679 ret = lease_register_notifier(&nfsd_file_lease_notifier);
681 pr_err("nfsd: unable to register lease notifier: %d\n", ret);
685 nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
686 FSNOTIFY_GROUP_NOFS);
687 if (IS_ERR(nfsd_file_fsnotify_group)) {
688 pr_err("nfsd: unable to create fsnotify group: %ld\n",
689 PTR_ERR(nfsd_file_fsnotify_group));
690 ret = PTR_ERR(nfsd_file_fsnotify_group);
691 nfsd_file_fsnotify_group = NULL;
695 for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
696 INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head);
697 spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock);
700 INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
704 lease_unregister_notifier(&nfsd_file_lease_notifier);
706 unregister_shrinker(&nfsd_file_shrinker);
708 list_lru_destroy(&nfsd_file_lru);
710 kmem_cache_destroy(nfsd_file_slab);
711 nfsd_file_slab = NULL;
712 kmem_cache_destroy(nfsd_file_mark_slab);
713 nfsd_file_mark_slab = NULL;
714 kvfree(nfsd_file_hashtbl);
715 nfsd_file_hashtbl = NULL;
716 destroy_workqueue(nfsd_filecache_wq);
717 nfsd_filecache_wq = NULL;
722 * Note this can deadlock with nfsd_file_lru_cb.
725 nfsd_file_cache_purge(struct net *net)
728 struct nfsd_file *nf;
729 struct hlist_node *next;
733 if (!nfsd_file_hashtbl)
736 for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
737 struct nfsd_fcache_bucket *nfb = &nfsd_file_hashtbl[i];
739 spin_lock(&nfb->nfb_lock);
740 hlist_for_each_entry_safe(nf, next, &nfb->nfb_head, nf_node) {
741 if (net && nf->nf_net != net)
743 del = nfsd_file_unhash_and_release_locked(nf, &dispose);
746 * Deadlock detected! Something marked this entry as
747 * unhased, but hasn't removed it from the hash list.
751 spin_unlock(&nfb->nfb_lock);
752 nfsd_file_dispose_list(&dispose);
756 static struct nfsd_fcache_disposal *
757 nfsd_alloc_fcache_disposal(void)
759 struct nfsd_fcache_disposal *l;
761 l = kmalloc(sizeof(*l), GFP_KERNEL);
764 INIT_WORK(&l->work, nfsd_file_delayed_close);
765 spin_lock_init(&l->lock);
766 INIT_LIST_HEAD(&l->freeme);
771 nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
773 cancel_work_sync(&l->work);
774 nfsd_file_dispose_list(&l->freeme);
779 nfsd_free_fcache_disposal_net(struct net *net)
781 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
782 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
784 nfsd_free_fcache_disposal(l);
788 nfsd_file_cache_start_net(struct net *net)
790 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
792 nn->fcache_disposal = nfsd_alloc_fcache_disposal();
793 return nn->fcache_disposal ? 0 : -ENOMEM;
797 nfsd_file_cache_shutdown_net(struct net *net)
799 nfsd_file_cache_purge(net);
800 nfsd_free_fcache_disposal_net(net);
804 nfsd_file_cache_shutdown(void)
806 set_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
808 lease_unregister_notifier(&nfsd_file_lease_notifier);
809 unregister_shrinker(&nfsd_file_shrinker);
811 * make sure all callers of nfsd_file_lru_cb are done before
812 * calling nfsd_file_cache_purge
814 cancel_delayed_work_sync(&nfsd_filecache_laundrette);
815 nfsd_file_cache_purge(NULL);
816 list_lru_destroy(&nfsd_file_lru);
818 fsnotify_put_group(nfsd_file_fsnotify_group);
819 nfsd_file_fsnotify_group = NULL;
820 kmem_cache_destroy(nfsd_file_slab);
821 nfsd_file_slab = NULL;
822 fsnotify_wait_marks_destroyed();
823 kmem_cache_destroy(nfsd_file_mark_slab);
824 nfsd_file_mark_slab = NULL;
825 kvfree(nfsd_file_hashtbl);
826 nfsd_file_hashtbl = NULL;
827 destroy_workqueue(nfsd_filecache_wq);
828 nfsd_filecache_wq = NULL;
832 nfsd_match_cred(const struct cred *c1, const struct cred *c2)
836 if (!uid_eq(c1->fsuid, c2->fsuid))
838 if (!gid_eq(c1->fsgid, c2->fsgid))
840 if (c1->group_info == NULL || c2->group_info == NULL)
841 return c1->group_info == c2->group_info;
842 if (c1->group_info->ngroups != c2->group_info->ngroups)
844 for (i = 0; i < c1->group_info->ngroups; i++) {
845 if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
851 static struct nfsd_file *
852 nfsd_file_find_locked(struct inode *inode, unsigned int may_flags,
853 unsigned int hashval, struct net *net)
855 struct nfsd_file *nf;
856 unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
858 hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
859 nf_node, lockdep_is_held(&nfsd_file_hashtbl[hashval].nfb_lock)) {
860 if (nf->nf_may != need)
862 if (nf->nf_inode != inode)
864 if (nf->nf_net != net)
866 if (!nfsd_match_cred(nf->nf_cred, current_cred()))
868 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags))
870 if (nfsd_file_get(nf) != NULL)
877 * nfsd_file_is_cached - are there any cached open files for this fh?
878 * @inode: inode of the file to check
880 * Scan the hashtable for open files that match this fh. Returns true if there
881 * are any, and false if not.
884 nfsd_file_is_cached(struct inode *inode)
887 struct nfsd_file *nf;
888 unsigned int hashval;
890 hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
893 hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
895 if (inode == nf->nf_inode) {
901 trace_nfsd_file_is_cached(inode, hashval, (int)ret);
906 nfsd_do_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
907 unsigned int may_flags, struct nfsd_file **pnf, bool open)
910 struct net *net = SVC_NET(rqstp);
911 struct nfsd_file *nf, *new;
913 unsigned int hashval;
916 /* FIXME: skip this if fh_dentry is already set? */
917 status = fh_verify(rqstp, fhp, S_IFREG,
918 may_flags|NFSD_MAY_OWNER_OVERRIDE);
919 if (status != nfs_ok)
922 inode = d_inode(fhp->fh_dentry);
923 hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
926 nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
929 goto wait_for_construction;
931 new = nfsd_file_alloc(inode, may_flags, hashval, net);
933 trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags,
934 NULL, nfserr_jukebox);
935 return nfserr_jukebox;
938 spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
939 nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
942 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
943 nfsd_file_slab_free(&new->nf_rcu);
945 wait_for_construction:
946 wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
948 /* Did construction of this file fail? */
949 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
951 status = nfserr_jukebox;
955 nfsd_file_put_noref(nf);
959 this_cpu_inc(nfsd_file_cache_hits);
961 if (!(may_flags & NFSD_MAY_NOT_BREAK_LEASE)) {
962 bool write = (may_flags & NFSD_MAY_WRITE);
964 if (test_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags) ||
965 (test_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags) && write)) {
966 status = nfserrno(nfsd_open_break_lease(
967 file_inode(nf->nf_file), may_flags));
968 if (status == nfs_ok) {
969 clear_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
971 clear_bit(NFSD_FILE_BREAK_WRITE,
977 if (status == nfs_ok) {
984 trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, nf, status);
988 /* Take reference for the hashtable */
989 refcount_inc(&nf->nf_ref);
990 __set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
991 __set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
992 list_lru_add(&nfsd_file_lru, &nf->nf_lru);
993 hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head);
994 ++nfsd_file_hashtbl[hashval].nfb_count;
995 nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount,
996 nfsd_file_hashtbl[hashval].nfb_count);
997 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
998 if (atomic_long_inc_return(&nfsd_filecache_count) >= NFSD_FILE_LRU_THRESHOLD)
1001 nf->nf_mark = nfsd_file_mark_find_or_create(nf);
1004 status = nfsd_open_verified(rqstp, fhp, may_flags,
1006 trace_nfsd_file_open(nf, status);
1010 status = nfserr_jukebox;
1012 * If construction failed, or we raced with a call to unlink()
1015 if (status != nfs_ok || inode->i_nlink == 0) {
1017 spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
1018 do_free = nfsd_file_unhash(nf);
1019 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
1021 nfsd_file_put_noref(nf);
1023 clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
1024 smp_mb__after_atomic();
1025 wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
1030 * nfsd_file_acquire - Get a struct nfsd_file with an open file
1031 * @rqstp: the RPC transaction being executed
1032 * @fhp: the NFS filehandle of the file to be opened
1033 * @may_flags: NFSD_MAY_ settings for the file
1034 * @pnf: OUT: new or found "struct nfsd_file" object
1036 * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1037 * network byte order is returned.
1040 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1041 unsigned int may_flags, struct nfsd_file **pnf)
1043 return nfsd_do_file_acquire(rqstp, fhp, may_flags, pnf, true);
1047 * nfsd_file_create - Get a struct nfsd_file, do not open
1048 * @rqstp: the RPC transaction being executed
1049 * @fhp: the NFS filehandle of the file just created
1050 * @may_flags: NFSD_MAY_ settings for the file
1051 * @pnf: OUT: new or found "struct nfsd_file" object
1053 * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in
1054 * network byte order is returned.
1057 nfsd_file_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
1058 unsigned int may_flags, struct nfsd_file **pnf)
1060 return nfsd_do_file_acquire(rqstp, fhp, may_flags, pnf, false);
1064 * Note that fields may be added, removed or reordered in the future. Programs
1065 * scraping this file for info should test the labels to ensure they're
1066 * getting the correct field.
1068 static int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
1070 unsigned int i, count = 0, longest = 0;
1071 unsigned long hits = 0;
1074 * No need for spinlocks here since we're not terribly interested in
1075 * accuracy. We do take the nfsd_mutex simply to ensure that we
1076 * don't end up racing with server shutdown
1078 mutex_lock(&nfsd_mutex);
1079 if (nfsd_file_hashtbl) {
1080 for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
1081 count += nfsd_file_hashtbl[i].nfb_count;
1082 longest = max(longest, nfsd_file_hashtbl[i].nfb_count);
1085 mutex_unlock(&nfsd_mutex);
1087 for_each_possible_cpu(i)
1088 hits += per_cpu(nfsd_file_cache_hits, i);
1090 seq_printf(m, "total entries: %u\n", count);
1091 seq_printf(m, "longest chain: %u\n", longest);
1092 seq_printf(m, "cache hits: %lu\n", hits);
1096 int nfsd_file_cache_stats_open(struct inode *inode, struct file *file)
1098 return single_open(file, nfsd_file_cache_stats_show, NULL);