1 // SPDX-License-Identifier: GPL-2.0
3 * The NFSD open file cache.
5 * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com>
7 * An nfsd_file object is a per-file collection of open state that binds
11 * - a network namespace
12 * - a read-ahead context
13 * - monitoring for writeback errors
15 * nfsd_file objects are reference-counted. Consumers acquire a new
16 * object via the nfsd_file_acquire API. They manage their interest in
17 * the acquired object, and hence the object's reference count, via
18 * nfsd_file_get and nfsd_file_put. There are two varieties of nfsd_file
21 * * non-garbage-collected: When a consumer wants to precisely control
22 * the lifetime of a file's open state, it acquires a non-garbage-
23 * collected nfsd_file. The final nfsd_file_put releases the open
26 * * garbage-collected: When a consumer does not control the lifetime
27 * of open state, it acquires a garbage-collected nfsd_file. The
28 * final nfsd_file_put allows the open state to linger for a period
29 * during which it may be re-used.
32 #include <linux/hash.h>
33 #include <linux/slab.h>
34 #include <linux/file.h>
35 #include <linux/pagemap.h>
36 #include <linux/sched.h>
37 #include <linux/list_lru.h>
38 #include <linux/fsnotify_backend.h>
39 #include <linux/fsnotify.h>
40 #include <linux/seq_file.h>
41 #include <linux/rhashtable.h>
47 #include "filecache.h"
50 #define NFSD_LAUNDRETTE_DELAY (2 * HZ)
52 #define NFSD_FILE_CACHE_UP (0)
54 /* We only care about NFSD_MAY_READ/WRITE for this cache */
55 #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE)
57 static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits);
58 static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions);
59 static DEFINE_PER_CPU(unsigned long, nfsd_file_releases);
60 static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age);
61 static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions);
63 struct nfsd_fcache_disposal {
65 struct list_head freeme;
68 static struct kmem_cache *nfsd_file_slab;
69 static struct kmem_cache *nfsd_file_mark_slab;
70 static struct list_lru nfsd_file_lru;
71 static unsigned long nfsd_file_flags;
72 static struct fsnotify_group *nfsd_file_fsnotify_group;
73 static struct delayed_work nfsd_filecache_laundrette;
74 static struct rhltable nfsd_file_rhltable
75 ____cacheline_aligned_in_smp;
78 nfsd_match_cred(const struct cred *c1, const struct cred *c2)
82 if (!uid_eq(c1->fsuid, c2->fsuid))
84 if (!gid_eq(c1->fsgid, c2->fsgid))
86 if (c1->group_info == NULL || c2->group_info == NULL)
87 return c1->group_info == c2->group_info;
88 if (c1->group_info->ngroups != c2->group_info->ngroups)
90 for (i = 0; i < c1->group_info->ngroups; i++) {
91 if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i]))
97 static const struct rhashtable_params nfsd_file_rhash_params = {
98 .key_len = sizeof_field(struct nfsd_file, nf_inode),
99 .key_offset = offsetof(struct nfsd_file, nf_inode),
100 .head_offset = offsetof(struct nfsd_file, nf_rlist),
103 * Start with a single page hash table to reduce resizing churn
104 * on light workloads.
107 .automatic_shrinking = true,
111 nfsd_file_schedule_laundrette(void)
113 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags))
114 queue_delayed_work(system_wq, &nfsd_filecache_laundrette,
115 NFSD_LAUNDRETTE_DELAY);
119 nfsd_file_slab_free(struct rcu_head *rcu)
121 struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu);
123 put_cred(nf->nf_cred);
124 kmem_cache_free(nfsd_file_slab, nf);
128 nfsd_file_mark_free(struct fsnotify_mark *mark)
130 struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark,
133 kmem_cache_free(nfsd_file_mark_slab, nfm);
136 static struct nfsd_file_mark *
137 nfsd_file_mark_get(struct nfsd_file_mark *nfm)
139 if (!refcount_inc_not_zero(&nfm->nfm_ref))
145 nfsd_file_mark_put(struct nfsd_file_mark *nfm)
147 if (refcount_dec_and_test(&nfm->nfm_ref)) {
148 fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group);
149 fsnotify_put_mark(&nfm->nfm_mark);
153 static struct nfsd_file_mark *
154 nfsd_file_mark_find_or_create(struct nfsd_file *nf, struct inode *inode)
157 struct fsnotify_mark *mark;
158 struct nfsd_file_mark *nfm = NULL, *new;
161 fsnotify_group_lock(nfsd_file_fsnotify_group);
162 mark = fsnotify_find_mark(&inode->i_fsnotify_marks,
163 nfsd_file_fsnotify_group);
165 nfm = nfsd_file_mark_get(container_of(mark,
166 struct nfsd_file_mark,
168 fsnotify_group_unlock(nfsd_file_fsnotify_group);
170 fsnotify_put_mark(mark);
173 /* Avoid soft lockup race with nfsd_file_mark_put() */
174 fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group);
175 fsnotify_put_mark(mark);
177 fsnotify_group_unlock(nfsd_file_fsnotify_group);
180 /* allocate a new nfm */
181 new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL);
184 fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group);
185 new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF;
186 refcount_set(&new->nfm_ref, 1);
188 err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0);
191 * If the add was successful, then return the object.
192 * Otherwise, we need to put the reference we hold on the
193 * nfm_mark. The fsnotify code will take a reference and put
194 * it on failure, so we can't just free it directly. It's also
195 * not safe to call fsnotify_destroy_mark on it as the
196 * mark->group will be NULL. Thus, we can't let the nfm_ref
197 * counter drive the destruction at this point.
202 fsnotify_put_mark(&new->nfm_mark);
203 } while (unlikely(err == -EEXIST));
208 static struct nfsd_file *
209 nfsd_file_alloc(struct net *net, struct inode *inode, unsigned char need,
212 struct nfsd_file *nf;
214 nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL);
218 INIT_LIST_HEAD(&nf->nf_lru);
219 nf->nf_birthtime = ktime_get();
221 nf->nf_cred = get_current_cred();
223 nf->nf_flags = want_gc ?
224 BIT(NFSD_FILE_HASHED) | BIT(NFSD_FILE_PENDING) | BIT(NFSD_FILE_GC) :
225 BIT(NFSD_FILE_HASHED) | BIT(NFSD_FILE_PENDING);
226 nf->nf_inode = inode;
227 refcount_set(&nf->nf_ref, 1);
234 * nfsd_file_check_write_error - check for writeback errors on a file
235 * @nf: nfsd_file to check for writeback errors
237 * Check whether a nfsd_file has an unseen error. Reset the write
241 nfsd_file_check_write_error(struct nfsd_file *nf)
243 struct file *file = nf->nf_file;
245 if ((file->f_mode & FMODE_WRITE) &&
246 filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err)))
247 nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id));
251 nfsd_file_hash_remove(struct nfsd_file *nf)
253 trace_nfsd_file_unhash(nf);
254 rhltable_remove(&nfsd_file_rhltable, &nf->nf_rlist,
255 nfsd_file_rhash_params);
259 nfsd_file_unhash(struct nfsd_file *nf)
261 if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
262 nfsd_file_hash_remove(nf);
269 nfsd_file_free(struct nfsd_file *nf)
271 s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime));
273 trace_nfsd_file_free(nf);
275 this_cpu_inc(nfsd_file_releases);
276 this_cpu_add(nfsd_file_total_age, age);
278 nfsd_file_unhash(nf);
280 nfsd_file_mark_put(nf->nf_mark);
282 nfsd_file_check_write_error(nf);
283 nfsd_filp_close(nf->nf_file);
287 * If this item is still linked via nf_lru, that's a bug.
288 * WARN and leak it to preserve system stability.
290 if (WARN_ON_ONCE(!list_empty(&nf->nf_lru)))
293 call_rcu(&nf->nf_rcu, nfsd_file_slab_free);
297 nfsd_file_check_writeback(struct nfsd_file *nf)
299 struct file *file = nf->nf_file;
300 struct address_space *mapping;
302 /* File not open for write? */
303 if (!(file->f_mode & FMODE_WRITE))
307 * Some filesystems (e.g. NFS) flush all dirty data on close.
308 * On others, there is no need to wait for writeback.
310 if (!(file_inode(file)->i_sb->s_export_op->flags & EXPORT_OP_FLUSH_ON_CLOSE))
313 mapping = file->f_mapping;
314 return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) ||
315 mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK);
319 static bool nfsd_file_lru_add(struct nfsd_file *nf)
321 set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags);
322 if (list_lru_add_obj(&nfsd_file_lru, &nf->nf_lru)) {
323 trace_nfsd_file_lru_add(nf);
329 static bool nfsd_file_lru_remove(struct nfsd_file *nf)
331 if (list_lru_del_obj(&nfsd_file_lru, &nf->nf_lru)) {
332 trace_nfsd_file_lru_del(nf);
339 nfsd_file_get(struct nfsd_file *nf)
341 if (nf && refcount_inc_not_zero(&nf->nf_ref))
347 * nfsd_file_put - put the reference to a nfsd_file
348 * @nf: nfsd_file of which to put the reference
350 * Put a reference to a nfsd_file. In the non-GC case, we just put the
351 * reference immediately. In the GC case, if the reference would be
352 * the last one, the put it on the LRU instead to be cleaned up later.
355 nfsd_file_put(struct nfsd_file *nf)
358 trace_nfsd_file_put(nf);
360 if (test_bit(NFSD_FILE_GC, &nf->nf_flags) &&
361 test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
363 * If this is the last reference (nf_ref == 1), then try to
364 * transfer it to the LRU.
366 if (refcount_dec_not_one(&nf->nf_ref))
369 /* Try to add it to the LRU. If that fails, decrement. */
370 if (nfsd_file_lru_add(nf)) {
371 /* If it's still hashed, we're done */
372 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
373 nfsd_file_schedule_laundrette();
378 * We're racing with unhashing, so try to remove it from
379 * the LRU. If removal fails, then someone else already
382 if (!nfsd_file_lru_remove(nf))
386 if (refcount_dec_and_test(&nf->nf_ref))
391 nfsd_file_dispose_list(struct list_head *dispose)
393 struct nfsd_file *nf;
395 while (!list_empty(dispose)) {
396 nf = list_first_entry(dispose, struct nfsd_file, nf_lru);
397 list_del_init(&nf->nf_lru);
403 * nfsd_file_dispose_list_delayed - move list of dead files to net's freeme list
404 * @dispose: list of nfsd_files to be disposed
406 * Transfers each file to the "freeme" list for its nfsd_net, to eventually
407 * be disposed of by the per-net garbage collector.
410 nfsd_file_dispose_list_delayed(struct list_head *dispose)
412 while(!list_empty(dispose)) {
413 struct nfsd_file *nf = list_first_entry(dispose,
414 struct nfsd_file, nf_lru);
415 struct nfsd_net *nn = net_generic(nf->nf_net, nfsd_net_id);
416 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
419 list_move_tail(&nf->nf_lru, &l->freeme);
420 spin_unlock(&l->lock);
421 svc_wake_up(nn->nfsd_serv);
426 * nfsd_file_net_dispose - deal with nfsd_files waiting to be disposed.
427 * @nn: nfsd_net in which to find files to be disposed.
429 * When files held open for nfsv3 are removed from the filecache, whether
430 * due to memory pressure or garbage collection, they are queued to
431 * a per-net-ns queue. This function completes the disposal, either
432 * directly or by waking another nfsd thread to help with the work.
434 void nfsd_file_net_dispose(struct nfsd_net *nn)
436 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
438 if (!list_empty(&l->freeme)) {
443 for (i = 0; i < 8 && !list_empty(&l->freeme); i++)
444 list_move(l->freeme.next, &dispose);
445 spin_unlock(&l->lock);
446 if (!list_empty(&l->freeme))
447 /* Wake up another thread to share the work
448 * *before* doing any actual disposing.
450 svc_wake_up(nn->nfsd_serv);
451 nfsd_file_dispose_list(&dispose);
456 * nfsd_file_lru_cb - Examine an entry on the LRU list
457 * @item: LRU entry to examine
458 * @lru: controlling LRU
459 * @lock: LRU list lock (unused)
463 * %LRU_REMOVED: @item was removed from the LRU
464 * %LRU_ROTATE: @item is to be moved to the LRU tail
465 * %LRU_SKIP: @item cannot be evicted
467 static enum lru_status
468 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru,
469 spinlock_t *lock, void *arg)
473 struct list_head *head = arg;
474 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru);
476 /* We should only be dealing with GC entries here */
477 WARN_ON_ONCE(!test_bit(NFSD_FILE_GC, &nf->nf_flags));
480 * Don't throw out files that are still undergoing I/O or
481 * that have uncleared errors pending.
483 if (nfsd_file_check_writeback(nf)) {
484 trace_nfsd_file_gc_writeback(nf);
488 /* If it was recently added to the list, skip it */
489 if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) {
490 trace_nfsd_file_gc_referenced(nf);
495 * Put the reference held on behalf of the LRU. If it wasn't the last
496 * one, then just remove it from the LRU and ignore it.
498 if (!refcount_dec_and_test(&nf->nf_ref)) {
499 trace_nfsd_file_gc_in_use(nf);
500 list_lru_isolate(lru, &nf->nf_lru);
504 /* Refcount went to zero. Unhash it and queue it to the dispose list */
505 nfsd_file_unhash(nf);
506 list_lru_isolate_move(lru, &nf->nf_lru, head);
507 this_cpu_inc(nfsd_file_evictions);
508 trace_nfsd_file_gc_disposed(nf);
518 ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb,
519 &dispose, list_lru_count(&nfsd_file_lru));
520 trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru));
521 nfsd_file_dispose_list_delayed(&dispose);
525 nfsd_file_gc_worker(struct work_struct *work)
528 if (list_lru_count(&nfsd_file_lru))
529 nfsd_file_schedule_laundrette();
533 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc)
535 return list_lru_count(&nfsd_file_lru);
539 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc)
544 ret = list_lru_shrink_walk(&nfsd_file_lru, sc,
545 nfsd_file_lru_cb, &dispose);
546 trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru));
547 nfsd_file_dispose_list_delayed(&dispose);
551 static struct shrinker *nfsd_file_shrinker;
554 * nfsd_file_cond_queue - conditionally unhash and queue a nfsd_file
555 * @nf: nfsd_file to attempt to queue
556 * @dispose: private list to queue successfully-put objects
558 * Unhash an nfsd_file, try to get a reference to it, and then put that
559 * reference. If it's the last reference, queue it to the dispose list.
562 nfsd_file_cond_queue(struct nfsd_file *nf, struct list_head *dispose)
567 /* If we raced with someone else unhashing, ignore it */
568 if (!nfsd_file_unhash(nf))
571 /* If we can't get a reference, ignore it */
572 if (!nfsd_file_get(nf))
575 /* Extra decrement if we remove from the LRU */
576 if (nfsd_file_lru_remove(nf))
579 /* If refcount goes to 0, then put on the dispose list */
580 if (refcount_sub_and_test(decrement, &nf->nf_ref)) {
581 list_add(&nf->nf_lru, dispose);
582 trace_nfsd_file_closing(nf);
587 * nfsd_file_queue_for_close: try to close out any open nfsd_files for an inode
588 * @inode: inode on which to close out nfsd_files
589 * @dispose: list on which to gather nfsd_files to close out
591 * An nfsd_file represents a struct file being held open on behalf of nfsd.
592 * An open file however can block other activity (such as leases), or cause
593 * undesirable behavior (e.g. spurious silly-renames when reexporting NFS).
595 * This function is intended to find open nfsd_files when this sort of
596 * conflicting access occurs and then attempt to close those files out.
598 * Populates the dispose list with entries that have already had their
599 * refcounts go to zero. The actual free of an nfsd_file can be expensive,
600 * so we leave it up to the caller whether it wants to wait or not.
603 nfsd_file_queue_for_close(struct inode *inode, struct list_head *dispose)
605 struct rhlist_head *tmp, *list;
606 struct nfsd_file *nf;
609 list = rhltable_lookup(&nfsd_file_rhltable, &inode,
610 nfsd_file_rhash_params);
611 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) {
612 if (!test_bit(NFSD_FILE_GC, &nf->nf_flags))
614 nfsd_file_cond_queue(nf, dispose);
620 * nfsd_file_close_inode - attempt a delayed close of a nfsd_file
621 * @inode: inode of the file to attempt to remove
623 * Close out any open nfsd_files that can be reaped for @inode. The
624 * actual freeing is deferred to the dispose_list_delayed infrastructure.
626 * This is used by the fsnotify callbacks and setlease notifier.
629 nfsd_file_close_inode(struct inode *inode)
633 nfsd_file_queue_for_close(inode, &dispose);
634 nfsd_file_dispose_list_delayed(&dispose);
638 * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file
639 * @inode: inode of the file to attempt to remove
641 * Close out any open nfsd_files that can be reaped for @inode. The
642 * nfsd_files are closed out synchronously.
644 * This is called from nfsd_rename and nfsd_unlink to avoid silly-renames
645 * when reexporting NFS.
648 nfsd_file_close_inode_sync(struct inode *inode)
650 struct nfsd_file *nf;
653 trace_nfsd_file_close(inode);
655 nfsd_file_queue_for_close(inode, &dispose);
656 while (!list_empty(&dispose)) {
657 nf = list_first_entry(&dispose, struct nfsd_file, nf_lru);
658 list_del_init(&nf->nf_lru);
664 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg,
667 struct file_lock *fl = data;
669 /* Only close files for F_SETLEASE leases */
670 if (fl->c.flc_flags & FL_LEASE)
671 nfsd_file_close_inode(file_inode(fl->c.flc_file));
675 static struct notifier_block nfsd_file_lease_notifier = {
676 .notifier_call = nfsd_file_lease_notifier_call,
680 nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask,
681 struct inode *inode, struct inode *dir,
682 const struct qstr *name, u32 cookie)
684 if (WARN_ON_ONCE(!inode))
687 trace_nfsd_file_fsnotify_handle_event(inode, mask);
689 /* Should be no marks on non-regular files */
690 if (!S_ISREG(inode->i_mode)) {
695 /* don't close files if this was not the last link */
696 if (mask & FS_ATTRIB) {
701 nfsd_file_close_inode(inode);
706 static const struct fsnotify_ops nfsd_file_fsnotify_ops = {
707 .handle_inode_event = nfsd_file_fsnotify_handle_event,
708 .free_mark = nfsd_file_mark_free,
712 nfsd_file_cache_init(void)
716 lockdep_assert_held(&nfsd_mutex);
717 if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
720 ret = rhltable_init(&nfsd_file_rhltable, &nfsd_file_rhash_params);
725 nfsd_file_slab = KMEM_CACHE(nfsd_file, 0);
726 if (!nfsd_file_slab) {
727 pr_err("nfsd: unable to create nfsd_file_slab\n");
731 nfsd_file_mark_slab = KMEM_CACHE(nfsd_file_mark, 0);
732 if (!nfsd_file_mark_slab) {
733 pr_err("nfsd: unable to create nfsd_file_mark_slab\n");
737 ret = list_lru_init(&nfsd_file_lru);
739 pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret);
743 nfsd_file_shrinker = shrinker_alloc(0, "nfsd-filecache");
744 if (!nfsd_file_shrinker) {
746 pr_err("nfsd: failed to allocate nfsd_file_shrinker\n");
750 nfsd_file_shrinker->count_objects = nfsd_file_lru_count;
751 nfsd_file_shrinker->scan_objects = nfsd_file_lru_scan;
752 nfsd_file_shrinker->seeks = 1;
754 shrinker_register(nfsd_file_shrinker);
756 ret = lease_register_notifier(&nfsd_file_lease_notifier);
758 pr_err("nfsd: unable to register lease notifier: %d\n", ret);
762 nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops,
763 FSNOTIFY_GROUP_NOFS);
764 if (IS_ERR(nfsd_file_fsnotify_group)) {
765 pr_err("nfsd: unable to create fsnotify group: %ld\n",
766 PTR_ERR(nfsd_file_fsnotify_group));
767 ret = PTR_ERR(nfsd_file_fsnotify_group);
768 nfsd_file_fsnotify_group = NULL;
772 INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker);
776 lease_unregister_notifier(&nfsd_file_lease_notifier);
778 shrinker_free(nfsd_file_shrinker);
780 list_lru_destroy(&nfsd_file_lru);
782 kmem_cache_destroy(nfsd_file_slab);
783 nfsd_file_slab = NULL;
784 kmem_cache_destroy(nfsd_file_mark_slab);
785 nfsd_file_mark_slab = NULL;
786 rhltable_destroy(&nfsd_file_rhltable);
791 * __nfsd_file_cache_purge: clean out the cache for shutdown
792 * @net: net-namespace to shut down the cache (may be NULL)
794 * Walk the nfsd_file cache and close out any that match @net. If @net is NULL,
795 * then close out everything. Called when an nfsd instance is being shut down,
796 * and when the exports table is flushed.
799 __nfsd_file_cache_purge(struct net *net)
801 struct rhashtable_iter iter;
802 struct nfsd_file *nf;
805 rhltable_walk_enter(&nfsd_file_rhltable, &iter);
807 rhashtable_walk_start(&iter);
809 nf = rhashtable_walk_next(&iter);
810 while (!IS_ERR_OR_NULL(nf)) {
811 if (!net || nf->nf_net == net)
812 nfsd_file_cond_queue(nf, &dispose);
813 nf = rhashtable_walk_next(&iter);
816 rhashtable_walk_stop(&iter);
817 } while (nf == ERR_PTR(-EAGAIN));
818 rhashtable_walk_exit(&iter);
820 nfsd_file_dispose_list(&dispose);
823 static struct nfsd_fcache_disposal *
824 nfsd_alloc_fcache_disposal(void)
826 struct nfsd_fcache_disposal *l;
828 l = kmalloc(sizeof(*l), GFP_KERNEL);
831 spin_lock_init(&l->lock);
832 INIT_LIST_HEAD(&l->freeme);
837 nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l)
839 nfsd_file_dispose_list(&l->freeme);
844 nfsd_free_fcache_disposal_net(struct net *net)
846 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
847 struct nfsd_fcache_disposal *l = nn->fcache_disposal;
849 nfsd_free_fcache_disposal(l);
853 nfsd_file_cache_start_net(struct net *net)
855 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
857 nn->fcache_disposal = nfsd_alloc_fcache_disposal();
858 return nn->fcache_disposal ? 0 : -ENOMEM;
862 * nfsd_file_cache_purge - Remove all cache items associated with @net
863 * @net: target net namespace
867 nfsd_file_cache_purge(struct net *net)
869 lockdep_assert_held(&nfsd_mutex);
870 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1)
871 __nfsd_file_cache_purge(net);
875 nfsd_file_cache_shutdown_net(struct net *net)
877 nfsd_file_cache_purge(net);
878 nfsd_free_fcache_disposal_net(net);
882 nfsd_file_cache_shutdown(void)
886 lockdep_assert_held(&nfsd_mutex);
887 if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0)
890 lease_unregister_notifier(&nfsd_file_lease_notifier);
891 shrinker_free(nfsd_file_shrinker);
893 * make sure all callers of nfsd_file_lru_cb are done before
894 * calling nfsd_file_cache_purge
896 cancel_delayed_work_sync(&nfsd_filecache_laundrette);
897 __nfsd_file_cache_purge(NULL);
898 list_lru_destroy(&nfsd_file_lru);
900 fsnotify_put_group(nfsd_file_fsnotify_group);
901 nfsd_file_fsnotify_group = NULL;
902 kmem_cache_destroy(nfsd_file_slab);
903 nfsd_file_slab = NULL;
904 fsnotify_wait_marks_destroyed();
905 kmem_cache_destroy(nfsd_file_mark_slab);
906 nfsd_file_mark_slab = NULL;
907 rhltable_destroy(&nfsd_file_rhltable);
909 for_each_possible_cpu(i) {
910 per_cpu(nfsd_file_cache_hits, i) = 0;
911 per_cpu(nfsd_file_acquisitions, i) = 0;
912 per_cpu(nfsd_file_releases, i) = 0;
913 per_cpu(nfsd_file_total_age, i) = 0;
914 per_cpu(nfsd_file_evictions, i) = 0;
918 static struct nfsd_file *
919 nfsd_file_lookup_locked(const struct net *net, const struct cred *cred,
920 struct inode *inode, unsigned char need,
923 struct rhlist_head *tmp, *list;
924 struct nfsd_file *nf;
926 list = rhltable_lookup(&nfsd_file_rhltable, &inode,
927 nfsd_file_rhash_params);
928 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) {
929 if (nf->nf_may != need)
931 if (nf->nf_net != net)
933 if (!nfsd_match_cred(nf->nf_cred, cred))
935 if (test_bit(NFSD_FILE_GC, &nf->nf_flags) != want_gc)
937 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0)
940 if (!nfsd_file_get(nf))
948 * nfsd_file_is_cached - are there any cached open files for this inode?
949 * @inode: inode to check
951 * The lookup matches inodes in all net namespaces and is atomic wrt
952 * nfsd_file_acquire().
955 * %true: filecache contains at least one file matching this inode
956 * %false: filecache contains no files matching this inode
959 nfsd_file_is_cached(struct inode *inode)
961 struct rhlist_head *tmp, *list;
962 struct nfsd_file *nf;
966 list = rhltable_lookup(&nfsd_file_rhltable, &inode,
967 nfsd_file_rhash_params);
968 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist)
969 if (test_bit(NFSD_FILE_GC, &nf->nf_flags)) {
975 trace_nfsd_file_is_cached(inode, (int)ret);
980 nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
981 unsigned int may_flags, struct file *file,
982 struct nfsd_file **pnf, bool want_gc)
984 unsigned char need = may_flags & NFSD_FILE_MAY_MASK;
985 struct net *net = SVC_NET(rqstp);
986 struct nfsd_file *new, *nf;
987 bool stale_retry = true;
988 bool open_retry = true;
994 status = fh_verify(rqstp, fhp, S_IFREG,
995 may_flags|NFSD_MAY_OWNER_OVERRIDE);
996 if (status != nfs_ok)
998 inode = d_inode(fhp->fh_dentry);
1001 nf = nfsd_file_lookup_locked(net, current_cred(), inode, need, want_gc);
1006 * If the nf is on the LRU then it holds an extra reference
1007 * that must be put if it's removed. It had better not be
1008 * the last one however, since we should hold another.
1010 if (nfsd_file_lru_remove(nf))
1011 WARN_ON_ONCE(refcount_dec_and_test(&nf->nf_ref));
1012 goto wait_for_construction;
1015 new = nfsd_file_alloc(net, inode, need, want_gc);
1017 status = nfserr_jukebox;
1022 spin_lock(&inode->i_lock);
1023 nf = nfsd_file_lookup_locked(net, current_cred(), inode, need, want_gc);
1025 spin_unlock(&inode->i_lock);
1027 nfsd_file_slab_free(&new->nf_rcu);
1028 goto wait_for_construction;
1031 ret = rhltable_insert(&nfsd_file_rhltable, &nf->nf_rlist,
1032 nfsd_file_rhash_params);
1033 spin_unlock(&inode->i_lock);
1035 if (likely(ret == 0))
1040 trace_nfsd_file_insert_err(rqstp, inode, may_flags, ret);
1041 status = nfserr_jukebox;
1042 goto construction_err;
1044 wait_for_construction:
1045 wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE);
1047 /* Did construction of this file fail? */
1048 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) {
1049 trace_nfsd_file_cons_err(rqstp, inode, may_flags, nf);
1051 status = nfserr_jukebox;
1052 goto construction_err;
1058 this_cpu_inc(nfsd_file_cache_hits);
1060 status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags));
1061 if (status != nfs_ok) {
1067 if (status == nfs_ok) {
1068 this_cpu_inc(nfsd_file_acquisitions);
1069 nfsd_file_check_write_error(nf);
1072 trace_nfsd_file_acquire(rqstp, inode, may_flags, nf, status);
1076 trace_nfsd_file_alloc(nf);
1077 nf->nf_mark = nfsd_file_mark_find_or_create(nf, inode);
1083 trace_nfsd_file_opened(nf, status);
1085 ret = nfsd_open_verified(rqstp, fhp, may_flags,
1087 if (ret == -EOPENSTALE && stale_retry) {
1088 stale_retry = false;
1089 nfsd_file_unhash(nf);
1090 clear_and_wake_up_bit(NFSD_FILE_PENDING,
1092 if (refcount_dec_and_test(&nf->nf_ref))
1098 status = nfserrno(ret);
1099 trace_nfsd_file_open(nf, status);
1102 status = nfserr_jukebox;
1104 * If construction failed, or we raced with a call to unlink()
1107 if (status != nfs_ok || inode->i_nlink == 0)
1108 nfsd_file_unhash(nf);
1109 clear_and_wake_up_bit(NFSD_FILE_PENDING, &nf->nf_flags);
1110 if (status == nfs_ok)
1114 if (refcount_dec_and_test(&nf->nf_ref))
1121 * nfsd_file_acquire_gc - Get a struct nfsd_file with an open file
1122 * @rqstp: the RPC transaction being executed
1123 * @fhp: the NFS filehandle of the file to be opened
1124 * @may_flags: NFSD_MAY_ settings for the file
1125 * @pnf: OUT: new or found "struct nfsd_file" object
1127 * The nfsd_file object returned by this API is reference-counted
1128 * and garbage-collected. The object is retained for a few
1129 * seconds after the final nfsd_file_put() in case the caller
1130 * wants to re-use it.
1133 * %nfs_ok - @pnf points to an nfsd_file with its reference
1136 * On error, an nfsstat value in network byte order is returned.
1139 nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp,
1140 unsigned int may_flags, struct nfsd_file **pnf)
1142 return nfsd_file_do_acquire(rqstp, fhp, may_flags, NULL, pnf, true);
1146 * nfsd_file_acquire - Get a struct nfsd_file with an open file
1147 * @rqstp: the RPC transaction being executed
1148 * @fhp: the NFS filehandle of the file to be opened
1149 * @may_flags: NFSD_MAY_ settings for the file
1150 * @pnf: OUT: new or found "struct nfsd_file" object
1152 * The nfsd_file_object returned by this API is reference-counted
1153 * but not garbage-collected. The object is unhashed after the
1154 * final nfsd_file_put().
1157 * %nfs_ok - @pnf points to an nfsd_file with its reference
1160 * On error, an nfsstat value in network byte order is returned.
1163 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp,
1164 unsigned int may_flags, struct nfsd_file **pnf)
1166 return nfsd_file_do_acquire(rqstp, fhp, may_flags, NULL, pnf, false);
1170 * nfsd_file_acquire_opened - Get a struct nfsd_file using existing open file
1171 * @rqstp: the RPC transaction being executed
1172 * @fhp: the NFS filehandle of the file just created
1173 * @may_flags: NFSD_MAY_ settings for the file
1174 * @file: cached, already-open file (may be NULL)
1175 * @pnf: OUT: new or found "struct nfsd_file" object
1177 * Acquire a nfsd_file object that is not GC'ed. If one doesn't already exist,
1178 * and @file is non-NULL, use it to instantiate a new nfsd_file instead of
1179 * opening a new one.
1182 * %nfs_ok - @pnf points to an nfsd_file with its reference
1185 * On error, an nfsstat value in network byte order is returned.
1188 nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp,
1189 unsigned int may_flags, struct file *file,
1190 struct nfsd_file **pnf)
1192 return nfsd_file_do_acquire(rqstp, fhp, may_flags, file, pnf, false);
1196 * Note that fields may be added, removed or reordered in the future. Programs
1197 * scraping this file for info should test the labels to ensure they're
1198 * getting the correct field.
1200 int nfsd_file_cache_stats_show(struct seq_file *m, void *v)
1202 unsigned long releases = 0, evictions = 0;
1203 unsigned long hits = 0, acquisitions = 0;
1204 unsigned int i, count = 0, buckets = 0;
1205 unsigned long lru = 0, total_age = 0;
1207 /* Serialize with server shutdown */
1208 mutex_lock(&nfsd_mutex);
1209 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) {
1210 struct bucket_table *tbl;
1211 struct rhashtable *ht;
1213 lru = list_lru_count(&nfsd_file_lru);
1216 ht = &nfsd_file_rhltable.ht;
1217 count = atomic_read(&ht->nelems);
1218 tbl = rht_dereference_rcu(ht->tbl, ht);
1219 buckets = tbl->size;
1222 mutex_unlock(&nfsd_mutex);
1224 for_each_possible_cpu(i) {
1225 hits += per_cpu(nfsd_file_cache_hits, i);
1226 acquisitions += per_cpu(nfsd_file_acquisitions, i);
1227 releases += per_cpu(nfsd_file_releases, i);
1228 total_age += per_cpu(nfsd_file_total_age, i);
1229 evictions += per_cpu(nfsd_file_evictions, i);
1232 seq_printf(m, "total inodes: %u\n", count);
1233 seq_printf(m, "hash buckets: %u\n", buckets);
1234 seq_printf(m, "lru entries: %lu\n", lru);
1235 seq_printf(m, "cache hits: %lu\n", hits);
1236 seq_printf(m, "acquisitions: %lu\n", acquisitions);
1237 seq_printf(m, "releases: %lu\n", releases);
1238 seq_printf(m, "evictions: %lu\n", evictions);
1240 seq_printf(m, "mean age (ms): %ld\n", total_age / releases);
1242 seq_printf(m, "mean age (ms): -\n");