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/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>
20 #include "filecache.h"
23 #define NFSDDBG_FACILITY NFSDDBG_FH
25 /* FIXME: dynamically size this for the machine somehow? */
26 #define NFSD_FILE_HASH_BITS 12
27 #define NFSD_FILE_HASH_SIZE (1 << NFSD_FILE_HASH_BITS)
28 #define NFSD_LAUNDRETTE_DELAY (2 * HZ)
30 #define NFSD_FILE_SHUTDOWN (1)
31 #define NFSD_FILE_LRU_THRESHOLD (4096UL)
32 #define NFSD_FILE_LRU_LIMIT (NFSD_FILE_LRU_THRESHOLD << 2)
34 /* We only care about NFSD_MAY_READ/WRITE for this cache */
35 #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE)
37 struct nfsd_fcache_bucket {
38 struct hlist_head nfb_head;
40 unsigned int nfb_count;
41 unsigned int nfb_maxcount;
44 static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
46 struct nfsd_fcache_disposal {
47 struct list_head list;
48 struct work_struct work;
51 struct list_head freeme;
55 struct workqueue_struct *nfsd_filecache_wq __read_mostly;
57 static struct kmem_cache *nfsd_file_slab;
58 static struct kmem_cache *nfsd_file_mark_slab;
59 static struct nfsd_fcache_bucket *nfsd_file_hashtbl;
60 static struct list_lru nfsd_file_lru;
61 static long nfsd_file_lru_flags;
62 static struct fsnotify_group *nfsd_file_fsnotify_group;
63 static atomic_long_t nfsd_filecache_count;
64 static struct delayed_work nfsd_filecache_laundrette;
65 static DEFINE_SPINLOCK(laundrette_lock);
66 static LIST_HEAD(laundrettes);
68 static void nfsd_file_gc(void);
71 nfsd_file_schedule_laundrette(void)
73 long count = atomic_long_read(&nfsd_filecache_count);
75 if (count == 0 || test_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags))
78 queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
79 NFSD_LAUNDRETTE_DELAY);
83 nfsd_file_slab_free(struct rcu_head *rcu)
85 struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
87 put_cred(nf->nf_cred);
88 kmem_cache_free(nfsd_file_slab, nf);
92 nfsd_file_mark_free(struct fsnotify_mark *mark)
94 struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
97 kmem_cache_free(nfsd_file_mark_slab, nfm);
100 static struct nfsd_file_mark *
101 nfsd_file_mark_get(struct nfsd_file_mark *nfm)
103 if (!atomic_inc_not_zero(&nfm->nfm_ref))
109 nfsd_file_mark_put(struct nfsd_file_mark *nfm)
111 if (atomic_dec_and_test(&nfm->nfm_ref)) {
113 fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
114 fsnotify_put_mark(&nfm->nfm_mark);
118 static struct nfsd_file_mark *
119 nfsd_file_mark_find_or_create(struct nfsd_file *nf)
122 struct fsnotify_mark *mark;
123 struct nfsd_file_mark *nfm = NULL, *new;
124 struct inode *inode = nf->nf_inode;
127 mutex_lock(&nfsd_file_fsnotify_group->mark_mutex);
128 mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
129 nfsd_file_fsnotify_group);
131 nfm = nfsd_file_mark_get(container_of(mark,
132 struct nfsd_file_mark,
134 mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
135 fsnotify_put_mark(mark);
139 mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex);
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 atomic_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 atomic_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_in_use(struct nfsd_file *nf)
243 return nfsd_file_check_writeback(nf) ||
244 nfsd_file_check_write_error(nf);
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_boot_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 (atomic_add_unless(&nf->nf_ref, -1, 1))
288 list_add(&nf->nf_lru, dispose);
293 nfsd_file_put_noref(struct nfsd_file *nf)
296 trace_nfsd_file_put(nf);
298 count = atomic_dec_return(&nf->nf_ref);
300 WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags));
307 nfsd_file_put(struct nfsd_file *nf)
309 bool is_hashed = test_bit(NFSD_FILE_HASHED, &nf->nf_flags) != 0;
310 bool unused = !nfsd_file_in_use(nf);
312 set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
313 if (nfsd_file_put_noref(nf) == 1 && is_hashed && unused)
314 nfsd_file_schedule_laundrette();
315 if (atomic_long_read(&nfsd_filecache_count) >= NFSD_FILE_LRU_LIMIT)
320 nfsd_file_get(struct nfsd_file *nf)
322 if (likely(atomic_inc_not_zero(&nf->nf_ref)))
328 nfsd_file_dispose_list(struct list_head *dispose)
330 struct nfsd_file *nf;
332 while(!list_empty(dispose)) {
333 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
334 list_del(&nf->nf_lru);
335 nfsd_file_put_noref(nf);
340 nfsd_file_dispose_list_sync(struct list_head *dispose)
343 struct nfsd_file *nf;
345 while(!list_empty(dispose)) {
346 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
347 list_del(&nf->nf_lru);
348 if (!atomic_dec_and_test(&nf->nf_ref))
350 if (nfsd_file_free(nf))
354 flush_delayed_fput();
358 nfsd_file_list_remove_disposal(struct list_head *dst,
359 struct nfsd_fcache_disposal *l)
362 list_splice_init(&l->freeme, dst);
363 spin_unlock(&l->lock);
367 nfsd_file_list_add_disposal(struct list_head *files, struct net *net)
369 struct nfsd_fcache_disposal *l;
372 list_for_each_entry_rcu(l, &laundrettes, list) {
375 list_splice_tail_init(files, &l->freeme);
376 spin_unlock(&l->lock);
377 queue_work(nfsd_filecache_wq, &l->work);
385 nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src,
388 struct nfsd_file *nf, *tmp;
390 list_for_each_entry_safe(nf, tmp, src, nf_lru) {
391 if (nf->nf_net == net)
392 list_move_tail(&nf->nf_lru, dst);
397 nfsd_file_dispose_list_delayed(struct list_head *dispose)
400 struct nfsd_file *nf;
402 while(!list_empty(dispose)) {
403 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
404 nfsd_file_list_add_pernet(&list, dispose, nf->nf_net);
405 nfsd_file_list_add_disposal(&list, nf->nf_net);
410 * Note this can deadlock with nfsd_file_cache_purge.
412 static enum lru_status
413 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
414 spinlock_t *lock, void *arg)
418 struct list_head *head = arg;
419 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
422 * Do a lockless refcount check. The hashtable holds one reference, so
423 * we look to see if anything else has a reference, or if any have
424 * been put since the shrinker last ran. Those don't get unhashed and
427 * Note that in the put path, we set the flag and then decrement the
428 * counter. Here we check the counter and then test and clear the flag.
429 * That order is deliberate to ensure that we can do this locklessly.
431 if (atomic_read(&nf->nf_ref) > 1)
435 * Don't throw out files that are still undergoing I/O or
436 * that have uncleared errors pending.
438 if (nfsd_file_check_writeback(nf))
441 if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags))
444 if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags))
447 list_lru_isolate_move(lru, &nf->nf_lru, head);
454 nfsd_file_lru_walk_list(struct shrink_control *sc)
457 struct nfsd_file *nf;
461 ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
462 nfsd_file_lru_cb, &head);
464 ret = list_lru_walk(&nfsd_file_lru,
467 list_for_each_entry(nf, &head, nf_lru) {
468 spin_lock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
469 nfsd_file_do_unhash(nf);
470 spin_unlock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock);
472 nfsd_file_dispose_list_delayed(&head);
479 nfsd_file_lru_walk_list(NULL);
483 nfsd_file_gc_worker(struct work_struct *work)
486 nfsd_file_schedule_laundrette();
490 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
492 return list_lru_count(&nfsd_file_lru);
496 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
498 return nfsd_file_lru_walk_list(sc);
501 static struct shrinker nfsd_file_shrinker = {
502 .scan_objects = nfsd_file_lru_scan,
503 .count_objects = nfsd_file_lru_count,
508 __nfsd_file_close_inode(struct inode *inode, unsigned int hashval,
509 struct list_head *dispose)
511 struct nfsd_file *nf;
512 struct hlist_node *tmp;
514 spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
515 hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) {
516 if (inode == nf->nf_inode)
517 nfsd_file_unhash_and_release_locked(nf, dispose);
519 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
523 * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
524 * @inode: inode of the file to attempt to remove
526 * Walk the whole hash bucket, looking for any files that correspond to "inode".
527 * If any do, then unhash them and put the hashtable reference to them and
528 * destroy any that had their last reference put. Also ensure that any of the
529 * fputs also have their final __fput done as well.
532 nfsd_file_close_inode_sync(struct inode *inode)
534 unsigned int hashval = (unsigned int)hash_long(inode->i_ino,
535 NFSD_FILE_HASH_BITS);
538 __nfsd_file_close_inode(inode, hashval, &dispose);
539 trace_nfsd_file_close_inode_sync(inode, hashval, !list_empty(&dispose));
540 nfsd_file_dispose_list_sync(&dispose);
544 * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
545 * @inode: inode of the file to attempt to remove
547 * Walk the whole hash bucket, looking for any files that correspond to "inode".
548 * If any do, then unhash them and put the hashtable reference to them and
549 * destroy any that had their last reference put.
552 nfsd_file_close_inode(struct inode *inode)
554 unsigned int hashval = (unsigned int)hash_long(inode->i_ino,
555 NFSD_FILE_HASH_BITS);
558 __nfsd_file_close_inode(inode, hashval, &dispose);
559 trace_nfsd_file_close_inode(inode, hashval, !list_empty(&dispose));
560 nfsd_file_dispose_list_delayed(&dispose);
564 * nfsd_file_delayed_close - close unused nfsd_files
567 * Walk the LRU list and close any entries that have not been used since
570 * Note this can deadlock with nfsd_file_cache_purge.
573 nfsd_file_delayed_close(struct work_struct *work)
576 struct nfsd_fcache_disposal *l = container_of(work,
577 struct nfsd_fcache_disposal, work);
579 nfsd_file_list_remove_disposal(&head, l);
580 nfsd_file_dispose_list(&head);
584 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
587 struct file_lock *fl = data;
589 /* Only close files for F_SETLEASE leases */
590 if (fl->fl_flags & FL_LEASE)
591 nfsd_file_close_inode_sync(file_inode(fl->fl_file));
595 static struct notifier_block nfsd_file_lease_notifier = {
596 .notifier_call = nfsd_file_lease_notifier_call,
600 nfsd_file_fsnotify_handle_event(struct fsnotify_group *group,
602 u32 mask, const void *data, int data_type,
603 const struct qstr *file_name, u32 cookie,
604 struct fsnotify_iter_info *iter_info)
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_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 = kcalloc(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 if (IS_ERR(nfsd_file_fsnotify_group)) {
687 pr_err("nfsd: unable to create fsnotify group: %ld\n",
688 PTR_ERR(nfsd_file_fsnotify_group));
689 nfsd_file_fsnotify_group = NULL;
693 for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
694 INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head);
695 spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock);
698 INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
702 lease_unregister_notifier(&nfsd_file_lease_notifier);
704 unregister_shrinker(&nfsd_file_shrinker);
706 list_lru_destroy(&nfsd_file_lru);
708 kmem_cache_destroy(nfsd_file_slab);
709 nfsd_file_slab = NULL;
710 kmem_cache_destroy(nfsd_file_mark_slab);
711 nfsd_file_mark_slab = NULL;
712 kfree(nfsd_file_hashtbl);
713 nfsd_file_hashtbl = NULL;
714 destroy_workqueue(nfsd_filecache_wq);
715 nfsd_filecache_wq = NULL;
720 * Note this can deadlock with nfsd_file_lru_cb.
723 nfsd_file_cache_purge(struct net *net)
726 struct nfsd_file *nf;
727 struct hlist_node *next;
731 if (!nfsd_file_hashtbl)
734 for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
735 struct nfsd_fcache_bucket *nfb = &nfsd_file_hashtbl[i];
737 spin_lock(&nfb->nfb_lock);
738 hlist_for_each_entry_safe(nf, next, &nfb->nfb_head, nf_node) {
739 if (net && nf->nf_net != net)
741 del = nfsd_file_unhash_and_release_locked(nf, &dispose);
744 * Deadlock detected! Something marked this entry as
745 * unhased, but hasn't removed it from the hash list.
749 spin_unlock(&nfb->nfb_lock);
750 nfsd_file_dispose_list(&dispose);
754 static struct nfsd_fcache_disposal *
755 nfsd_alloc_fcache_disposal(struct net *net)
757 struct nfsd_fcache_disposal *l;
759 l = kmalloc(sizeof(*l), GFP_KERNEL);
762 INIT_WORK(&l->work, nfsd_file_delayed_close);
764 spin_lock_init(&l->lock);
765 INIT_LIST_HEAD(&l->freeme);
770 nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
772 rcu_assign_pointer(l->net, NULL);
773 cancel_work_sync(&l->work);
774 nfsd_file_dispose_list(&l->freeme);
779 nfsd_add_fcache_disposal(struct nfsd_fcache_disposal *l)
781 spin_lock(&laundrette_lock);
782 list_add_tail_rcu(&l->list, &laundrettes);
783 spin_unlock(&laundrette_lock);
787 nfsd_del_fcache_disposal(struct nfsd_fcache_disposal *l)
789 spin_lock(&laundrette_lock);
790 list_del_rcu(&l->list);
791 spin_unlock(&laundrette_lock);
795 nfsd_alloc_fcache_disposal_net(struct net *net)
797 struct nfsd_fcache_disposal *l;
799 l = nfsd_alloc_fcache_disposal(net);
802 nfsd_add_fcache_disposal(l);
807 nfsd_free_fcache_disposal_net(struct net *net)
809 struct nfsd_fcache_disposal *l;
812 list_for_each_entry_rcu(l, &laundrettes, list) {
815 nfsd_del_fcache_disposal(l);
817 nfsd_free_fcache_disposal(l);
824 nfsd_file_cache_start_net(struct net *net)
826 return nfsd_alloc_fcache_disposal_net(net);
830 nfsd_file_cache_shutdown_net(struct net *net)
832 nfsd_file_cache_purge(net);
833 nfsd_free_fcache_disposal_net(net);
837 nfsd_file_cache_shutdown(void)
839 set_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags);
841 lease_unregister_notifier(&nfsd_file_lease_notifier);
842 unregister_shrinker(&nfsd_file_shrinker);
844 * make sure all callers of nfsd_file_lru_cb are done before
845 * calling nfsd_file_cache_purge
847 cancel_delayed_work_sync(&nfsd_filecache_laundrette);
848 nfsd_file_cache_purge(NULL);
849 list_lru_destroy(&nfsd_file_lru);
851 fsnotify_put_group(nfsd_file_fsnotify_group);
852 nfsd_file_fsnotify_group = NULL;
853 kmem_cache_destroy(nfsd_file_slab);
854 nfsd_file_slab = NULL;
855 fsnotify_wait_marks_destroyed();
856 kmem_cache_destroy(nfsd_file_mark_slab);
857 nfsd_file_mark_slab = NULL;
858 kfree(nfsd_file_hashtbl);
859 nfsd_file_hashtbl = NULL;
860 destroy_workqueue(nfsd_filecache_wq);
861 nfsd_filecache_wq = NULL;
865 nfsd_match_cred(const struct cred *c1, const struct cred *c2)
869 if (!uid_eq(c1->fsuid, c2->fsuid))
871 if (!gid_eq(c1->fsgid, c2->fsgid))
873 if (c1->group_info == NULL || c2->group_info == NULL)
874 return c1->group_info == c2->group_info;
875 if (c1->group_info->ngroups != c2->group_info->ngroups)
877 for (i = 0; i < c1->group_info->ngroups; i++) {
878 if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
884 static struct nfsd_file *
885 nfsd_file_find_locked(struct inode *inode, unsigned int may_flags,
886 unsigned int hashval, struct net *net)
888 struct nfsd_file *nf;
889 unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
891 hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
893 if ((need & nf->nf_may) != need)
895 if (nf->nf_inode != inode)
897 if (nf->nf_net != net)
899 if (!nfsd_match_cred(nf->nf_cred, current_cred()))
901 if (nfsd_file_get(nf) != NULL)
908 * nfsd_file_is_cached - are there any cached open files for this fh?
909 * @inode: inode of the file to check
911 * Scan the hashtable for open files that match this fh. Returns true if there
912 * are any, and false if not.
915 nfsd_file_is_cached(struct inode *inode)
918 struct nfsd_file *nf;
919 unsigned int hashval;
921 hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
924 hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head,
926 if (inode == nf->nf_inode) {
932 trace_nfsd_file_is_cached(inode, hashval, (int)ret);
937 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
938 unsigned int may_flags, struct nfsd_file **pnf)
941 struct net *net = SVC_NET(rqstp);
942 struct nfsd_file *nf, *new;
944 unsigned int hashval;
947 /* FIXME: skip this if fh_dentry is already set? */
948 status = fh_verify(rqstp, fhp, S_IFREG,
949 may_flags|NFSD_MAY_OWNER_OVERRIDE);
950 if (status != nfs_ok)
953 inode = d_inode(fhp->fh_dentry);
954 hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS);
957 nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
960 goto wait_for_construction;
962 new = nfsd_file_alloc(inode, may_flags, hashval, net);
964 trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags,
965 NULL, nfserr_jukebox);
966 return nfserr_jukebox;
969 spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
970 nf = nfsd_file_find_locked(inode, may_flags, hashval, net);
973 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
974 nfsd_file_slab_free(&new->nf_rcu);
976 wait_for_construction:
977 wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
979 /* Did construction of this file fail? */
980 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
982 status = nfserr_jukebox;
986 nfsd_file_put_noref(nf);
990 this_cpu_inc(nfsd_file_cache_hits);
992 if (!(may_flags & NFSD_MAY_NOT_BREAK_LEASE)) {
993 bool write = (may_flags & NFSD_MAY_WRITE);
995 if (test_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags) ||
996 (test_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags) && write)) {
997 status = nfserrno(nfsd_open_break_lease(
998 file_inode(nf->nf_file), may_flags));
999 if (status == nfs_ok) {
1000 clear_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags);
1002 clear_bit(NFSD_FILE_BREAK_WRITE,
1008 if (status == nfs_ok) {
1015 trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, nf, status);
1019 /* Take reference for the hashtable */
1020 atomic_inc(&nf->nf_ref);
1021 __set_bit(NFSD_FILE_HASHED, &nf->nf_flags);
1022 __set_bit(NFSD_FILE_PENDING, &nf->nf_flags);
1023 list_lru_add(&nfsd_file_lru, &nf->nf_lru);
1024 hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head);
1025 ++nfsd_file_hashtbl[hashval].nfb_count;
1026 nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount,
1027 nfsd_file_hashtbl[hashval].nfb_count);
1028 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
1029 if (atomic_long_inc_return(&nfsd_filecache_count) >= NFSD_FILE_LRU_THRESHOLD)
1032 nf->nf_mark = nfsd_file_mark_find_or_create(nf);
1034 status = nfsd_open_verified(rqstp, fhp, S_IFREG,
1035 may_flags, &nf->nf_file);
1037 status = nfserr_jukebox;
1039 * If construction failed, or we raced with a call to unlink()
1042 if (status != nfs_ok || inode->i_nlink == 0) {
1044 spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock);
1045 do_free = nfsd_file_unhash(nf);
1046 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock);
1048 nfsd_file_put_noref(nf);
1050 clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags);
1051 smp_mb__after_atomic();
1052 wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING);
1057 * Note that fields may be added, removed or reordered in the future. Programs
1058 * scraping this file for info should test the labels to ensure they're
1059 * getting the correct field.
1061 static int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
1063 unsigned int i, count = 0, longest = 0;
1064 unsigned long hits = 0;
1067 * No need for spinlocks here since we're not terribly interested in
1068 * accuracy. We do take the nfsd_mutex simply to ensure that we
1069 * don't end up racing with server shutdown
1071 mutex_lock(&nfsd_mutex);
1072 if (nfsd_file_hashtbl) {
1073 for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) {
1074 count += nfsd_file_hashtbl[i].nfb_count;
1075 longest = max(longest, nfsd_file_hashtbl[i].nfb_count);
1078 mutex_unlock(&nfsd_mutex);
1080 for_each_possible_cpu(i)
1081 hits += per_cpu(nfsd_file_cache_hits, i);
1083 seq_printf(m, "total entries: %u\n", count);
1084 seq_printf(m, "longest chain: %u\n", longest);
1085 seq_printf(m, "cache hits: %lu\n", hits);
1089 int nfsd_file_cache_stats_open(struct inode *inode, struct file *file)
1091 return single_open(file, nfsd_file_cache_stats_show, NULL);