165294c1fSJeff Layton /* 265294c1fSJeff Layton * Open file cache. 365294c1fSJeff Layton * 465294c1fSJeff Layton * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com> 565294c1fSJeff Layton */ 665294c1fSJeff Layton 765294c1fSJeff Layton #include <linux/hash.h> 865294c1fSJeff Layton #include <linux/slab.h> 965294c1fSJeff Layton #include <linux/file.h> 10cbcc268bSMatthew Wilcox (Oracle) #include <linux/pagemap.h> 1165294c1fSJeff Layton #include <linux/sched.h> 1265294c1fSJeff Layton #include <linux/list_lru.h> 1365294c1fSJeff Layton #include <linux/fsnotify_backend.h> 1465294c1fSJeff Layton #include <linux/fsnotify.h> 1565294c1fSJeff Layton #include <linux/seq_file.h> 1665294c1fSJeff Layton 1765294c1fSJeff Layton #include "vfs.h" 1865294c1fSJeff Layton #include "nfsd.h" 1965294c1fSJeff Layton #include "nfsfh.h" 205e113224STrond Myklebust #include "netns.h" 2165294c1fSJeff Layton #include "filecache.h" 2265294c1fSJeff Layton #include "trace.h" 2365294c1fSJeff Layton 2465294c1fSJeff Layton #define NFSDDBG_FACILITY NFSDDBG_FH 2565294c1fSJeff Layton 2665294c1fSJeff Layton /* FIXME: dynamically size this for the machine somehow? */ 2765294c1fSJeff Layton #define NFSD_FILE_HASH_BITS 12 2865294c1fSJeff Layton #define NFSD_FILE_HASH_SIZE (1 << NFSD_FILE_HASH_BITS) 2965294c1fSJeff Layton #define NFSD_LAUNDRETTE_DELAY (2 * HZ) 3065294c1fSJeff Layton 3165294c1fSJeff Layton #define NFSD_FILE_SHUTDOWN (1) 3265294c1fSJeff Layton #define NFSD_FILE_LRU_THRESHOLD (4096UL) 3365294c1fSJeff Layton #define NFSD_FILE_LRU_LIMIT (NFSD_FILE_LRU_THRESHOLD << 2) 3465294c1fSJeff Layton 3565294c1fSJeff Layton /* We only care about NFSD_MAY_READ/WRITE for this cache */ 3665294c1fSJeff Layton #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE) 3765294c1fSJeff Layton 3865294c1fSJeff Layton struct nfsd_fcache_bucket { 3965294c1fSJeff Layton struct hlist_head nfb_head; 4065294c1fSJeff Layton spinlock_t nfb_lock; 4165294c1fSJeff Layton unsigned int nfb_count; 4265294c1fSJeff Layton unsigned int nfb_maxcount; 4365294c1fSJeff Layton }; 4465294c1fSJeff Layton 4565294c1fSJeff Layton static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits); 4629d4bdbbSChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions); 47d6329327SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_releases); 48904940e9SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age); 49df2aff52SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_pages_flushed); 5094660cc1SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions); 5165294c1fSJeff Layton 529542e6a6STrond Myklebust struct nfsd_fcache_disposal { 539542e6a6STrond Myklebust struct work_struct work; 549542e6a6STrond Myklebust spinlock_t lock; 559542e6a6STrond Myklebust struct list_head freeme; 569542e6a6STrond Myklebust }; 579542e6a6STrond Myklebust 5850d0def9SChen Zhou static struct workqueue_struct *nfsd_filecache_wq __read_mostly; 599542e6a6STrond Myklebust 6065294c1fSJeff Layton static struct kmem_cache *nfsd_file_slab; 6165294c1fSJeff Layton static struct kmem_cache *nfsd_file_mark_slab; 6265294c1fSJeff Layton static struct nfsd_fcache_bucket *nfsd_file_hashtbl; 6365294c1fSJeff Layton static struct list_lru nfsd_file_lru; 6465294c1fSJeff Layton static long nfsd_file_lru_flags; 6565294c1fSJeff Layton static struct fsnotify_group *nfsd_file_fsnotify_group; 6665294c1fSJeff Layton static atomic_long_t nfsd_filecache_count; 6765294c1fSJeff Layton static struct delayed_work nfsd_filecache_laundrette; 6865294c1fSJeff Layton 699542e6a6STrond Myklebust static void nfsd_file_gc(void); 7065294c1fSJeff Layton 7165294c1fSJeff Layton static void 729542e6a6STrond Myklebust nfsd_file_schedule_laundrette(void) 7365294c1fSJeff Layton { 7465294c1fSJeff Layton long count = atomic_long_read(&nfsd_filecache_count); 7565294c1fSJeff Layton 7665294c1fSJeff Layton if (count == 0 || test_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags)) 7765294c1fSJeff Layton return; 7865294c1fSJeff Layton 799542e6a6STrond Myklebust queue_delayed_work(system_wq, &nfsd_filecache_laundrette, 809542e6a6STrond Myklebust NFSD_LAUNDRETTE_DELAY); 8165294c1fSJeff Layton } 8265294c1fSJeff Layton 8365294c1fSJeff Layton static void 8465294c1fSJeff Layton nfsd_file_slab_free(struct rcu_head *rcu) 8565294c1fSJeff Layton { 8665294c1fSJeff Layton struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu); 8765294c1fSJeff Layton 8865294c1fSJeff Layton put_cred(nf->nf_cred); 8965294c1fSJeff Layton kmem_cache_free(nfsd_file_slab, nf); 9065294c1fSJeff Layton } 9165294c1fSJeff Layton 9265294c1fSJeff Layton static void 9365294c1fSJeff Layton nfsd_file_mark_free(struct fsnotify_mark *mark) 9465294c1fSJeff Layton { 9565294c1fSJeff Layton struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark, 9665294c1fSJeff Layton nfm_mark); 9765294c1fSJeff Layton 9865294c1fSJeff Layton kmem_cache_free(nfsd_file_mark_slab, nfm); 9965294c1fSJeff Layton } 10065294c1fSJeff Layton 10165294c1fSJeff Layton static struct nfsd_file_mark * 10265294c1fSJeff Layton nfsd_file_mark_get(struct nfsd_file_mark *nfm) 10365294c1fSJeff Layton { 104689827cdSTrond Myklebust if (!refcount_inc_not_zero(&nfm->nfm_ref)) 10565294c1fSJeff Layton return NULL; 10665294c1fSJeff Layton return nfm; 10765294c1fSJeff Layton } 10865294c1fSJeff Layton 10965294c1fSJeff Layton static void 11065294c1fSJeff Layton nfsd_file_mark_put(struct nfsd_file_mark *nfm) 11165294c1fSJeff Layton { 112689827cdSTrond Myklebust if (refcount_dec_and_test(&nfm->nfm_ref)) { 11365294c1fSJeff Layton fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group); 11465294c1fSJeff Layton fsnotify_put_mark(&nfm->nfm_mark); 11565294c1fSJeff Layton } 11665294c1fSJeff Layton } 11765294c1fSJeff Layton 11865294c1fSJeff Layton static struct nfsd_file_mark * 11965294c1fSJeff Layton nfsd_file_mark_find_or_create(struct nfsd_file *nf) 12065294c1fSJeff Layton { 12165294c1fSJeff Layton int err; 12265294c1fSJeff Layton struct fsnotify_mark *mark; 12365294c1fSJeff Layton struct nfsd_file_mark *nfm = NULL, *new; 12465294c1fSJeff Layton struct inode *inode = nf->nf_inode; 12565294c1fSJeff Layton 12665294c1fSJeff Layton do { 127b8962a9dSAmir Goldstein fsnotify_group_lock(nfsd_file_fsnotify_group); 12865294c1fSJeff Layton mark = fsnotify_find_mark(&inode->i_fsnotify_marks, 12965294c1fSJeff Layton nfsd_file_fsnotify_group); 13065294c1fSJeff Layton if (mark) { 13165294c1fSJeff Layton nfm = nfsd_file_mark_get(container_of(mark, 13265294c1fSJeff Layton struct nfsd_file_mark, 13365294c1fSJeff Layton nfm_mark)); 134b8962a9dSAmir Goldstein fsnotify_group_unlock(nfsd_file_fsnotify_group); 13590d2f1daSTrond Myklebust if (nfm) { 13665294c1fSJeff Layton fsnotify_put_mark(mark); 13765294c1fSJeff Layton break; 13890d2f1daSTrond Myklebust } 13990d2f1daSTrond Myklebust /* Avoid soft lockup race with nfsd_file_mark_put() */ 14090d2f1daSTrond Myklebust fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group); 14190d2f1daSTrond Myklebust fsnotify_put_mark(mark); 142b8962a9dSAmir Goldstein } else { 143b8962a9dSAmir Goldstein fsnotify_group_unlock(nfsd_file_fsnotify_group); 144b8962a9dSAmir Goldstein } 14565294c1fSJeff Layton 14665294c1fSJeff Layton /* allocate a new nfm */ 14765294c1fSJeff Layton new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL); 14865294c1fSJeff Layton if (!new) 14965294c1fSJeff Layton return NULL; 15065294c1fSJeff Layton fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group); 15165294c1fSJeff Layton new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF; 152689827cdSTrond Myklebust refcount_set(&new->nfm_ref, 1); 15365294c1fSJeff Layton 15465294c1fSJeff Layton err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0); 15565294c1fSJeff Layton 15665294c1fSJeff Layton /* 15765294c1fSJeff Layton * If the add was successful, then return the object. 15865294c1fSJeff Layton * Otherwise, we need to put the reference we hold on the 15965294c1fSJeff Layton * nfm_mark. The fsnotify code will take a reference and put 16065294c1fSJeff Layton * it on failure, so we can't just free it directly. It's also 16165294c1fSJeff Layton * not safe to call fsnotify_destroy_mark on it as the 16265294c1fSJeff Layton * mark->group will be NULL. Thus, we can't let the nfm_ref 16365294c1fSJeff Layton * counter drive the destruction at this point. 16465294c1fSJeff Layton */ 16565294c1fSJeff Layton if (likely(!err)) 16665294c1fSJeff Layton nfm = new; 16765294c1fSJeff Layton else 16865294c1fSJeff Layton fsnotify_put_mark(&new->nfm_mark); 16965294c1fSJeff Layton } while (unlikely(err == -EEXIST)); 17065294c1fSJeff Layton 17165294c1fSJeff Layton return nfm; 17265294c1fSJeff Layton } 17365294c1fSJeff Layton 17465294c1fSJeff Layton static struct nfsd_file * 1755e113224STrond Myklebust nfsd_file_alloc(struct inode *inode, unsigned int may, unsigned int hashval, 1765e113224STrond Myklebust struct net *net) 17765294c1fSJeff Layton { 17865294c1fSJeff Layton struct nfsd_file *nf; 17965294c1fSJeff Layton 18065294c1fSJeff Layton nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL); 18165294c1fSJeff Layton if (nf) { 18265294c1fSJeff Layton INIT_HLIST_NODE(&nf->nf_node); 18365294c1fSJeff Layton INIT_LIST_HEAD(&nf->nf_lru); 184904940e9SChuck Lever nf->nf_birthtime = ktime_get(); 18565294c1fSJeff Layton nf->nf_file = NULL; 18665294c1fSJeff Layton nf->nf_cred = get_current_cred(); 1875e113224STrond Myklebust nf->nf_net = net; 18865294c1fSJeff Layton nf->nf_flags = 0; 18965294c1fSJeff Layton nf->nf_inode = inode; 19065294c1fSJeff Layton nf->nf_hashval = hashval; 191689827cdSTrond Myklebust refcount_set(&nf->nf_ref, 1); 19265294c1fSJeff Layton nf->nf_may = may & NFSD_FILE_MAY_MASK; 19365294c1fSJeff Layton nf->nf_mark = NULL; 19465294c1fSJeff Layton trace_nfsd_file_alloc(nf); 19565294c1fSJeff Layton } 19665294c1fSJeff Layton return nf; 19765294c1fSJeff Layton } 19865294c1fSJeff Layton 19965294c1fSJeff Layton static bool 20065294c1fSJeff Layton nfsd_file_free(struct nfsd_file *nf) 20165294c1fSJeff Layton { 202904940e9SChuck Lever s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime)); 20365294c1fSJeff Layton bool flush = false; 20465294c1fSJeff Layton 205d6329327SChuck Lever this_cpu_inc(nfsd_file_releases); 206904940e9SChuck Lever this_cpu_add(nfsd_file_total_age, age); 207d6329327SChuck Lever 20865294c1fSJeff Layton trace_nfsd_file_put_final(nf); 20965294c1fSJeff Layton if (nf->nf_mark) 21065294c1fSJeff Layton nfsd_file_mark_put(nf->nf_mark); 21165294c1fSJeff Layton if (nf->nf_file) { 21265294c1fSJeff Layton get_file(nf->nf_file); 21365294c1fSJeff Layton filp_close(nf->nf_file, NULL); 21465294c1fSJeff Layton fput(nf->nf_file); 21565294c1fSJeff Layton flush = true; 21665294c1fSJeff Layton } 217668ed92eSChuck Lever 218668ed92eSChuck Lever /* 219668ed92eSChuck Lever * If this item is still linked via nf_lru, that's a bug. 220668ed92eSChuck Lever * WARN and leak it to preserve system stability. 221668ed92eSChuck Lever */ 222668ed92eSChuck Lever if (WARN_ON_ONCE(!list_empty(&nf->nf_lru))) 223668ed92eSChuck Lever return flush; 224668ed92eSChuck Lever 22565294c1fSJeff Layton call_rcu(&nf->nf_rcu, nfsd_file_slab_free); 22665294c1fSJeff Layton return flush; 22765294c1fSJeff Layton } 22865294c1fSJeff Layton 229055b24a8STrond Myklebust static bool 230055b24a8STrond Myklebust nfsd_file_check_writeback(struct nfsd_file *nf) 231055b24a8STrond Myklebust { 232055b24a8STrond Myklebust struct file *file = nf->nf_file; 233055b24a8STrond Myklebust struct address_space *mapping; 234055b24a8STrond Myklebust 235055b24a8STrond Myklebust if (!file || !(file->f_mode & FMODE_WRITE)) 236055b24a8STrond Myklebust return false; 237055b24a8STrond Myklebust mapping = file->f_mapping; 238055b24a8STrond Myklebust return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) || 239055b24a8STrond Myklebust mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK); 240055b24a8STrond Myklebust } 241055b24a8STrond Myklebust 242055b24a8STrond Myklebust static int 243055b24a8STrond Myklebust nfsd_file_check_write_error(struct nfsd_file *nf) 244055b24a8STrond Myklebust { 245055b24a8STrond Myklebust struct file *file = nf->nf_file; 246055b24a8STrond Myklebust 247055b24a8STrond Myklebust if (!file || !(file->f_mode & FMODE_WRITE)) 248055b24a8STrond Myklebust return 0; 249055b24a8STrond Myklebust return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err)); 250055b24a8STrond Myklebust } 251055b24a8STrond Myklebust 25265294c1fSJeff Layton static void 2536b8a9433STrond Myklebust nfsd_file_flush(struct nfsd_file *nf) 2546b8a9433STrond Myklebust { 255df2aff52SChuck Lever struct file *file = nf->nf_file; 256df2aff52SChuck Lever 257df2aff52SChuck Lever if (!file || !(file->f_mode & FMODE_WRITE)) 258df2aff52SChuck Lever return; 259df2aff52SChuck Lever this_cpu_add(nfsd_file_pages_flushed, file->f_mapping->nrpages); 260df2aff52SChuck Lever if (vfs_fsync(file, 1) != 0) 2616b8a9433STrond Myklebust nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 2626b8a9433STrond Myklebust } 2636b8a9433STrond Myklebust 264*c46203acSChuck Lever static void nfsd_file_lru_add(struct nfsd_file *nf) 265*c46203acSChuck Lever { 266*c46203acSChuck Lever if (list_lru_add(&nfsd_file_lru, &nf->nf_lru)) 267*c46203acSChuck Lever trace_nfsd_file_lru_add(nf); 268*c46203acSChuck Lever } 269*c46203acSChuck Lever 270*c46203acSChuck Lever static void nfsd_file_lru_remove(struct nfsd_file *nf) 271*c46203acSChuck Lever { 272*c46203acSChuck Lever if (list_lru_del(&nfsd_file_lru, &nf->nf_lru)) 273*c46203acSChuck Lever trace_nfsd_file_lru_del(nf); 274*c46203acSChuck Lever } 275*c46203acSChuck Lever 2766b8a9433STrond Myklebust static void 27765294c1fSJeff Layton nfsd_file_do_unhash(struct nfsd_file *nf) 27865294c1fSJeff Layton { 27965294c1fSJeff Layton lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); 28065294c1fSJeff Layton 28165294c1fSJeff Layton trace_nfsd_file_unhash(nf); 28265294c1fSJeff Layton 283055b24a8STrond Myklebust if (nfsd_file_check_write_error(nf)) 2843988a578SChuck Lever nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 28565294c1fSJeff Layton --nfsd_file_hashtbl[nf->nf_hashval].nfb_count; 28665294c1fSJeff Layton hlist_del_rcu(&nf->nf_node); 28765294c1fSJeff Layton atomic_long_dec(&nfsd_filecache_count); 28865294c1fSJeff Layton } 28965294c1fSJeff Layton 29065294c1fSJeff Layton static bool 29165294c1fSJeff Layton nfsd_file_unhash(struct nfsd_file *nf) 29265294c1fSJeff Layton { 29365294c1fSJeff Layton if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 29465294c1fSJeff Layton nfsd_file_do_unhash(nf); 295*c46203acSChuck Lever nfsd_file_lru_remove(nf); 29665294c1fSJeff Layton return true; 29765294c1fSJeff Layton } 29865294c1fSJeff Layton return false; 29965294c1fSJeff Layton } 30065294c1fSJeff Layton 30165294c1fSJeff Layton /* 30265294c1fSJeff Layton * Return true if the file was unhashed. 30365294c1fSJeff Layton */ 30465294c1fSJeff Layton static bool 30565294c1fSJeff Layton nfsd_file_unhash_and_release_locked(struct nfsd_file *nf, struct list_head *dispose) 30665294c1fSJeff Layton { 30765294c1fSJeff Layton lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); 30865294c1fSJeff Layton 30965294c1fSJeff Layton trace_nfsd_file_unhash_and_release_locked(nf); 31065294c1fSJeff Layton if (!nfsd_file_unhash(nf)) 31165294c1fSJeff Layton return false; 31265294c1fSJeff Layton /* keep final reference for nfsd_file_lru_dispose */ 313689827cdSTrond Myklebust if (refcount_dec_not_one(&nf->nf_ref)) 31465294c1fSJeff Layton return true; 31565294c1fSJeff Layton 31665294c1fSJeff Layton list_add(&nf->nf_lru, dispose); 31765294c1fSJeff Layton return true; 31865294c1fSJeff Layton } 31965294c1fSJeff Layton 320b6669305STrond Myklebust static void 32165294c1fSJeff Layton nfsd_file_put_noref(struct nfsd_file *nf) 32265294c1fSJeff Layton { 32365294c1fSJeff Layton trace_nfsd_file_put(nf); 32465294c1fSJeff Layton 325689827cdSTrond Myklebust if (refcount_dec_and_test(&nf->nf_ref)) { 32665294c1fSJeff Layton WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags)); 32765294c1fSJeff Layton nfsd_file_free(nf); 32865294c1fSJeff Layton } 32965294c1fSJeff Layton } 33065294c1fSJeff Layton 33165294c1fSJeff Layton void 33265294c1fSJeff Layton nfsd_file_put(struct nfsd_file *nf) 33365294c1fSJeff Layton { 33408af54b3SChuck Lever might_sleep(); 33508af54b3SChuck Lever 33665294c1fSJeff Layton set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags); 33799939792STrond Myklebust if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) { 3386b8a9433STrond Myklebust nfsd_file_flush(nf); 339b6669305STrond Myklebust nfsd_file_put_noref(nf); 340b6c71c66SChuck Lever } else if (nf->nf_file) { 341b6669305STrond Myklebust nfsd_file_put_noref(nf); 3429542e6a6STrond Myklebust nfsd_file_schedule_laundrette(); 343b6c71c66SChuck Lever } else 344b6c71c66SChuck Lever nfsd_file_put_noref(nf); 345b6c71c66SChuck Lever 3469542e6a6STrond Myklebust if (atomic_long_read(&nfsd_filecache_count) >= NFSD_FILE_LRU_LIMIT) 3479542e6a6STrond Myklebust nfsd_file_gc(); 34865294c1fSJeff Layton } 34965294c1fSJeff Layton 35065294c1fSJeff Layton struct nfsd_file * 35165294c1fSJeff Layton nfsd_file_get(struct nfsd_file *nf) 35265294c1fSJeff Layton { 353689827cdSTrond Myklebust if (likely(refcount_inc_not_zero(&nf->nf_ref))) 35465294c1fSJeff Layton return nf; 35565294c1fSJeff Layton return NULL; 35665294c1fSJeff Layton } 35765294c1fSJeff Layton 35865294c1fSJeff Layton static void 35965294c1fSJeff Layton nfsd_file_dispose_list(struct list_head *dispose) 36065294c1fSJeff Layton { 36165294c1fSJeff Layton struct nfsd_file *nf; 36265294c1fSJeff Layton 36365294c1fSJeff Layton while(!list_empty(dispose)) { 36465294c1fSJeff Layton nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 365668ed92eSChuck Lever list_del_init(&nf->nf_lru); 3666b8a9433STrond Myklebust nfsd_file_flush(nf); 36765294c1fSJeff Layton nfsd_file_put_noref(nf); 36865294c1fSJeff Layton } 36965294c1fSJeff Layton } 37065294c1fSJeff Layton 37165294c1fSJeff Layton static void 37265294c1fSJeff Layton nfsd_file_dispose_list_sync(struct list_head *dispose) 37365294c1fSJeff Layton { 37465294c1fSJeff Layton bool flush = false; 37565294c1fSJeff Layton struct nfsd_file *nf; 37665294c1fSJeff Layton 37765294c1fSJeff Layton while(!list_empty(dispose)) { 37865294c1fSJeff Layton nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 379668ed92eSChuck Lever list_del_init(&nf->nf_lru); 3806b8a9433STrond Myklebust nfsd_file_flush(nf); 381689827cdSTrond Myklebust if (!refcount_dec_and_test(&nf->nf_ref)) 38265294c1fSJeff Layton continue; 38365294c1fSJeff Layton if (nfsd_file_free(nf)) 38465294c1fSJeff Layton flush = true; 38565294c1fSJeff Layton } 38665294c1fSJeff Layton if (flush) 38765294c1fSJeff Layton flush_delayed_fput(); 38865294c1fSJeff Layton } 38965294c1fSJeff Layton 3909542e6a6STrond Myklebust static void 3919542e6a6STrond Myklebust nfsd_file_list_remove_disposal(struct list_head *dst, 3929542e6a6STrond Myklebust struct nfsd_fcache_disposal *l) 3939542e6a6STrond Myklebust { 3949542e6a6STrond Myklebust spin_lock(&l->lock); 3959542e6a6STrond Myklebust list_splice_init(&l->freeme, dst); 3969542e6a6STrond Myklebust spin_unlock(&l->lock); 3979542e6a6STrond Myklebust } 3989542e6a6STrond Myklebust 3999542e6a6STrond Myklebust static void 4009542e6a6STrond Myklebust nfsd_file_list_add_disposal(struct list_head *files, struct net *net) 4019542e6a6STrond Myklebust { 4021463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 4031463b38eSNeilBrown struct nfsd_fcache_disposal *l = nn->fcache_disposal; 4049542e6a6STrond Myklebust 4059542e6a6STrond Myklebust spin_lock(&l->lock); 4069542e6a6STrond Myklebust list_splice_tail_init(files, &l->freeme); 4079542e6a6STrond Myklebust spin_unlock(&l->lock); 4089542e6a6STrond Myklebust queue_work(nfsd_filecache_wq, &l->work); 4099542e6a6STrond Myklebust } 4109542e6a6STrond Myklebust 4119542e6a6STrond Myklebust static void 4129542e6a6STrond Myklebust nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src, 4139542e6a6STrond Myklebust struct net *net) 4149542e6a6STrond Myklebust { 4159542e6a6STrond Myklebust struct nfsd_file *nf, *tmp; 4169542e6a6STrond Myklebust 4179542e6a6STrond Myklebust list_for_each_entry_safe(nf, tmp, src, nf_lru) { 4189542e6a6STrond Myklebust if (nf->nf_net == net) 4199542e6a6STrond Myklebust list_move_tail(&nf->nf_lru, dst); 4209542e6a6STrond Myklebust } 4219542e6a6STrond Myklebust } 4229542e6a6STrond Myklebust 4239542e6a6STrond Myklebust static void 4249542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(struct list_head *dispose) 4259542e6a6STrond Myklebust { 4269542e6a6STrond Myklebust LIST_HEAD(list); 4279542e6a6STrond Myklebust struct nfsd_file *nf; 4289542e6a6STrond Myklebust 4299542e6a6STrond Myklebust while(!list_empty(dispose)) { 4309542e6a6STrond Myklebust nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 4319542e6a6STrond Myklebust nfsd_file_list_add_pernet(&list, dispose, nf->nf_net); 4329542e6a6STrond Myklebust nfsd_file_list_add_disposal(&list, nf->nf_net); 4339542e6a6STrond Myklebust } 4349542e6a6STrond Myklebust } 4359542e6a6STrond Myklebust 43665294c1fSJeff Layton /* 43765294c1fSJeff Layton * Note this can deadlock with nfsd_file_cache_purge. 43865294c1fSJeff Layton */ 43965294c1fSJeff Layton static enum lru_status 44065294c1fSJeff Layton nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru, 44165294c1fSJeff Layton spinlock_t *lock, void *arg) 44265294c1fSJeff Layton __releases(lock) 44365294c1fSJeff Layton __acquires(lock) 44465294c1fSJeff Layton { 44565294c1fSJeff Layton struct list_head *head = arg; 44665294c1fSJeff Layton struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru); 44765294c1fSJeff Layton 44865294c1fSJeff Layton /* 44965294c1fSJeff Layton * Do a lockless refcount check. The hashtable holds one reference, so 45065294c1fSJeff Layton * we look to see if anything else has a reference, or if any have 45165294c1fSJeff Layton * been put since the shrinker last ran. Those don't get unhashed and 45265294c1fSJeff Layton * released. 45365294c1fSJeff Layton * 45465294c1fSJeff Layton * Note that in the put path, we set the flag and then decrement the 45565294c1fSJeff Layton * counter. Here we check the counter and then test and clear the flag. 45665294c1fSJeff Layton * That order is deliberate to ensure that we can do this locklessly. 45765294c1fSJeff Layton */ 458*c46203acSChuck Lever if (refcount_read(&nf->nf_ref) > 1) { 459*c46203acSChuck Lever trace_nfsd_file_gc_in_use(nf); 460*c46203acSChuck Lever return LRU_SKIP; 461*c46203acSChuck Lever } 462055b24a8STrond Myklebust 463055b24a8STrond Myklebust /* 464055b24a8STrond Myklebust * Don't throw out files that are still undergoing I/O or 465055b24a8STrond Myklebust * that have uncleared errors pending. 466055b24a8STrond Myklebust */ 467*c46203acSChuck Lever if (nfsd_file_check_writeback(nf)) { 468*c46203acSChuck Lever trace_nfsd_file_gc_writeback(nf); 469*c46203acSChuck Lever return LRU_SKIP; 470*c46203acSChuck Lever } 471055b24a8STrond Myklebust 472*c46203acSChuck Lever if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) { 473*c46203acSChuck Lever trace_nfsd_file_gc_referenced(nf); 474*c46203acSChuck Lever return LRU_SKIP; 475*c46203acSChuck Lever } 47665294c1fSJeff Layton 477*c46203acSChuck Lever if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 478*c46203acSChuck Lever trace_nfsd_file_gc_hashed(nf); 479*c46203acSChuck Lever return LRU_SKIP; 480*c46203acSChuck Lever } 48165294c1fSJeff Layton 48265294c1fSJeff Layton list_lru_isolate_move(lru, &nf->nf_lru, head); 48394660cc1SChuck Lever this_cpu_inc(nfsd_file_evictions); 484*c46203acSChuck Lever trace_nfsd_file_gc_disposed(nf); 48565294c1fSJeff Layton return LRU_REMOVED; 48665294c1fSJeff Layton } 48765294c1fSJeff Layton 4880bac5a26SChuck Lever /* 4890bac5a26SChuck Lever * Unhash items on @dispose immediately, then queue them on the 4900bac5a26SChuck Lever * disposal workqueue to finish releasing them in the background. 4910bac5a26SChuck Lever * 4920bac5a26SChuck Lever * cel: Note that between the time list_lru_shrink_walk runs and 4930bac5a26SChuck Lever * now, these items are in the hash table but marked unhashed. 4940bac5a26SChuck Lever * Why release these outside of lru_cb ? There's no lock ordering 4950bac5a26SChuck Lever * problem since lru_cb currently takes no lock. 4960bac5a26SChuck Lever */ 4970bac5a26SChuck Lever static void nfsd_file_gc_dispose_list(struct list_head *dispose) 4980bac5a26SChuck Lever { 4990bac5a26SChuck Lever struct nfsd_file *nf; 5000bac5a26SChuck Lever 5010bac5a26SChuck Lever list_for_each_entry(nf, dispose, nf_lru) { 5020bac5a26SChuck Lever spin_lock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); 5030bac5a26SChuck Lever nfsd_file_do_unhash(nf); 5040bac5a26SChuck Lever spin_unlock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); 5050bac5a26SChuck Lever } 5060bac5a26SChuck Lever nfsd_file_dispose_list_delayed(dispose); 5070bac5a26SChuck Lever } 5080bac5a26SChuck Lever 5099542e6a6STrond Myklebust static void 5109542e6a6STrond Myklebust nfsd_file_gc(void) 5119542e6a6STrond Myklebust { 5123bc6d347SChuck Lever LIST_HEAD(dispose); 51394660cc1SChuck Lever unsigned long ret; 5143bc6d347SChuck Lever 51594660cc1SChuck Lever ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb, 5163bc6d347SChuck Lever &dispose, LONG_MAX); 51794660cc1SChuck Lever trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru)); 5183bc6d347SChuck Lever nfsd_file_gc_dispose_list(&dispose); 5199542e6a6STrond Myklebust } 5209542e6a6STrond Myklebust 5219542e6a6STrond Myklebust static void 5229542e6a6STrond Myklebust nfsd_file_gc_worker(struct work_struct *work) 5239542e6a6STrond Myklebust { 5249542e6a6STrond Myklebust nfsd_file_gc(); 5259542e6a6STrond Myklebust nfsd_file_schedule_laundrette(); 52665294c1fSJeff Layton } 52765294c1fSJeff Layton 52865294c1fSJeff Layton static unsigned long 52965294c1fSJeff Layton nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc) 53065294c1fSJeff Layton { 53165294c1fSJeff Layton return list_lru_count(&nfsd_file_lru); 53265294c1fSJeff Layton } 53365294c1fSJeff Layton 53465294c1fSJeff Layton static unsigned long 53565294c1fSJeff Layton nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc) 53665294c1fSJeff Layton { 53739f1d1ffSChuck Lever LIST_HEAD(dispose); 53839f1d1ffSChuck Lever unsigned long ret; 53939f1d1ffSChuck Lever 54039f1d1ffSChuck Lever ret = list_lru_shrink_walk(&nfsd_file_lru, sc, 54139f1d1ffSChuck Lever nfsd_file_lru_cb, &dispose); 54294660cc1SChuck Lever trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru)); 54339f1d1ffSChuck Lever nfsd_file_gc_dispose_list(&dispose); 54439f1d1ffSChuck Lever return ret; 54565294c1fSJeff Layton } 54665294c1fSJeff Layton 54765294c1fSJeff Layton static struct shrinker nfsd_file_shrinker = { 54865294c1fSJeff Layton .scan_objects = nfsd_file_lru_scan, 54965294c1fSJeff Layton .count_objects = nfsd_file_lru_count, 55065294c1fSJeff Layton .seeks = 1, 55165294c1fSJeff Layton }; 55265294c1fSJeff Layton 55365294c1fSJeff Layton static void 55465294c1fSJeff Layton __nfsd_file_close_inode(struct inode *inode, unsigned int hashval, 55565294c1fSJeff Layton struct list_head *dispose) 55665294c1fSJeff Layton { 55765294c1fSJeff Layton struct nfsd_file *nf; 55865294c1fSJeff Layton struct hlist_node *tmp; 55965294c1fSJeff Layton 56065294c1fSJeff Layton spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock); 56165294c1fSJeff Layton hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) { 56265294c1fSJeff Layton if (inode == nf->nf_inode) 56365294c1fSJeff Layton nfsd_file_unhash_and_release_locked(nf, dispose); 56465294c1fSJeff Layton } 56565294c1fSJeff Layton spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); 56665294c1fSJeff Layton } 56765294c1fSJeff Layton 56865294c1fSJeff Layton /** 56965294c1fSJeff Layton * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file 57065294c1fSJeff Layton * @inode: inode of the file to attempt to remove 57165294c1fSJeff Layton * 57265294c1fSJeff Layton * Walk the whole hash bucket, looking for any files that correspond to "inode". 57365294c1fSJeff Layton * If any do, then unhash them and put the hashtable reference to them and 57465294c1fSJeff Layton * destroy any that had their last reference put. Also ensure that any of the 57565294c1fSJeff Layton * fputs also have their final __fput done as well. 57665294c1fSJeff Layton */ 57765294c1fSJeff Layton void 57865294c1fSJeff Layton nfsd_file_close_inode_sync(struct inode *inode) 57965294c1fSJeff Layton { 58065294c1fSJeff Layton unsigned int hashval = (unsigned int)hash_long(inode->i_ino, 58165294c1fSJeff Layton NFSD_FILE_HASH_BITS); 58265294c1fSJeff Layton LIST_HEAD(dispose); 58365294c1fSJeff Layton 58465294c1fSJeff Layton __nfsd_file_close_inode(inode, hashval, &dispose); 58565294c1fSJeff Layton trace_nfsd_file_close_inode_sync(inode, hashval, !list_empty(&dispose)); 58665294c1fSJeff Layton nfsd_file_dispose_list_sync(&dispose); 58765294c1fSJeff Layton } 58865294c1fSJeff Layton 58965294c1fSJeff Layton /** 59019598141STrond Myklebust * nfsd_file_close_inode - attempt a delayed close of a nfsd_file 59165294c1fSJeff Layton * @inode: inode of the file to attempt to remove 59265294c1fSJeff Layton * 59365294c1fSJeff Layton * Walk the whole hash bucket, looking for any files that correspond to "inode". 59465294c1fSJeff Layton * If any do, then unhash them and put the hashtable reference to them and 59565294c1fSJeff Layton * destroy any that had their last reference put. 59665294c1fSJeff Layton */ 59765294c1fSJeff Layton static void 59865294c1fSJeff Layton nfsd_file_close_inode(struct inode *inode) 59965294c1fSJeff Layton { 60065294c1fSJeff Layton unsigned int hashval = (unsigned int)hash_long(inode->i_ino, 60165294c1fSJeff Layton NFSD_FILE_HASH_BITS); 60265294c1fSJeff Layton LIST_HEAD(dispose); 60365294c1fSJeff Layton 60465294c1fSJeff Layton __nfsd_file_close_inode(inode, hashval, &dispose); 60565294c1fSJeff Layton trace_nfsd_file_close_inode(inode, hashval, !list_empty(&dispose)); 6069542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(&dispose); 60765294c1fSJeff Layton } 60865294c1fSJeff Layton 60965294c1fSJeff Layton /** 61065294c1fSJeff Layton * nfsd_file_delayed_close - close unused nfsd_files 61165294c1fSJeff Layton * @work: dummy 61265294c1fSJeff Layton * 61365294c1fSJeff Layton * Walk the LRU list and close any entries that have not been used since 61465294c1fSJeff Layton * the last scan. 61565294c1fSJeff Layton * 61665294c1fSJeff Layton * Note this can deadlock with nfsd_file_cache_purge. 61765294c1fSJeff Layton */ 61865294c1fSJeff Layton static void 61965294c1fSJeff Layton nfsd_file_delayed_close(struct work_struct *work) 62065294c1fSJeff Layton { 62165294c1fSJeff Layton LIST_HEAD(head); 6229542e6a6STrond Myklebust struct nfsd_fcache_disposal *l = container_of(work, 6239542e6a6STrond Myklebust struct nfsd_fcache_disposal, work); 62465294c1fSJeff Layton 6259542e6a6STrond Myklebust nfsd_file_list_remove_disposal(&head, l); 6269542e6a6STrond Myklebust nfsd_file_dispose_list(&head); 62765294c1fSJeff Layton } 62865294c1fSJeff Layton 62965294c1fSJeff Layton static int 63065294c1fSJeff Layton nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg, 63165294c1fSJeff Layton void *data) 63265294c1fSJeff Layton { 63365294c1fSJeff Layton struct file_lock *fl = data; 63465294c1fSJeff Layton 63565294c1fSJeff Layton /* Only close files for F_SETLEASE leases */ 63665294c1fSJeff Layton if (fl->fl_flags & FL_LEASE) 63765294c1fSJeff Layton nfsd_file_close_inode_sync(file_inode(fl->fl_file)); 63865294c1fSJeff Layton return 0; 63965294c1fSJeff Layton } 64065294c1fSJeff Layton 64165294c1fSJeff Layton static struct notifier_block nfsd_file_lease_notifier = { 64265294c1fSJeff Layton .notifier_call = nfsd_file_lease_notifier_call, 64365294c1fSJeff Layton }; 64465294c1fSJeff Layton 64565294c1fSJeff Layton static int 646b9a1b977SAmir Goldstein nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask, 647b9a1b977SAmir Goldstein struct inode *inode, struct inode *dir, 648950cc0d2SAmir Goldstein const struct qstr *name, u32 cookie) 64965294c1fSJeff Layton { 65024dca905SGabriel Krisman Bertazi if (WARN_ON_ONCE(!inode)) 65124dca905SGabriel Krisman Bertazi return 0; 65224dca905SGabriel Krisman Bertazi 65365294c1fSJeff Layton trace_nfsd_file_fsnotify_handle_event(inode, mask); 65465294c1fSJeff Layton 65565294c1fSJeff Layton /* Should be no marks on non-regular files */ 65665294c1fSJeff Layton if (!S_ISREG(inode->i_mode)) { 65765294c1fSJeff Layton WARN_ON_ONCE(1); 65865294c1fSJeff Layton return 0; 65965294c1fSJeff Layton } 66065294c1fSJeff Layton 66165294c1fSJeff Layton /* don't close files if this was not the last link */ 66265294c1fSJeff Layton if (mask & FS_ATTRIB) { 66365294c1fSJeff Layton if (inode->i_nlink) 66465294c1fSJeff Layton return 0; 66565294c1fSJeff Layton } 66665294c1fSJeff Layton 66765294c1fSJeff Layton nfsd_file_close_inode(inode); 66865294c1fSJeff Layton return 0; 66965294c1fSJeff Layton } 67065294c1fSJeff Layton 67165294c1fSJeff Layton 67265294c1fSJeff Layton static const struct fsnotify_ops nfsd_file_fsnotify_ops = { 673b9a1b977SAmir Goldstein .handle_inode_event = nfsd_file_fsnotify_handle_event, 67465294c1fSJeff Layton .free_mark = nfsd_file_mark_free, 67565294c1fSJeff Layton }; 67665294c1fSJeff Layton 67765294c1fSJeff Layton int 67865294c1fSJeff Layton nfsd_file_cache_init(void) 67965294c1fSJeff Layton { 68065294c1fSJeff Layton int ret = -ENOMEM; 68165294c1fSJeff Layton unsigned int i; 68265294c1fSJeff Layton 68365294c1fSJeff Layton clear_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags); 68465294c1fSJeff Layton 68565294c1fSJeff Layton if (nfsd_file_hashtbl) 68665294c1fSJeff Layton return 0; 68765294c1fSJeff Layton 6889542e6a6STrond Myklebust nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0); 6899542e6a6STrond Myklebust if (!nfsd_filecache_wq) 6909542e6a6STrond Myklebust goto out; 6919542e6a6STrond Myklebust 6924d2eeafeSAmir Goldstein nfsd_file_hashtbl = kvcalloc(NFSD_FILE_HASH_SIZE, 69365294c1fSJeff Layton sizeof(*nfsd_file_hashtbl), GFP_KERNEL); 69465294c1fSJeff Layton if (!nfsd_file_hashtbl) { 69565294c1fSJeff Layton pr_err("nfsd: unable to allocate nfsd_file_hashtbl\n"); 69665294c1fSJeff Layton goto out_err; 69765294c1fSJeff Layton } 69865294c1fSJeff Layton 69965294c1fSJeff Layton nfsd_file_slab = kmem_cache_create("nfsd_file", 70065294c1fSJeff Layton sizeof(struct nfsd_file), 0, 0, NULL); 70165294c1fSJeff Layton if (!nfsd_file_slab) { 70265294c1fSJeff Layton pr_err("nfsd: unable to create nfsd_file_slab\n"); 70365294c1fSJeff Layton goto out_err; 70465294c1fSJeff Layton } 70565294c1fSJeff Layton 70665294c1fSJeff Layton nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark", 70765294c1fSJeff Layton sizeof(struct nfsd_file_mark), 0, 0, NULL); 70865294c1fSJeff Layton if (!nfsd_file_mark_slab) { 70965294c1fSJeff Layton pr_err("nfsd: unable to create nfsd_file_mark_slab\n"); 71065294c1fSJeff Layton goto out_err; 71165294c1fSJeff Layton } 71265294c1fSJeff Layton 71365294c1fSJeff Layton 71465294c1fSJeff Layton ret = list_lru_init(&nfsd_file_lru); 71565294c1fSJeff Layton if (ret) { 71665294c1fSJeff Layton pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret); 71765294c1fSJeff Layton goto out_err; 71865294c1fSJeff Layton } 71965294c1fSJeff Layton 72065294c1fSJeff Layton ret = register_shrinker(&nfsd_file_shrinker); 72165294c1fSJeff Layton if (ret) { 72265294c1fSJeff Layton pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret); 72365294c1fSJeff Layton goto out_lru; 72465294c1fSJeff Layton } 72565294c1fSJeff Layton 72665294c1fSJeff Layton ret = lease_register_notifier(&nfsd_file_lease_notifier); 72765294c1fSJeff Layton if (ret) { 72865294c1fSJeff Layton pr_err("nfsd: unable to register lease notifier: %d\n", ret); 72965294c1fSJeff Layton goto out_shrinker; 73065294c1fSJeff Layton } 73165294c1fSJeff Layton 732867a448dSAmir Goldstein nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops, 733b8962a9dSAmir Goldstein FSNOTIFY_GROUP_NOFS); 73465294c1fSJeff Layton if (IS_ERR(nfsd_file_fsnotify_group)) { 73565294c1fSJeff Layton pr_err("nfsd: unable to create fsnotify group: %ld\n", 73665294c1fSJeff Layton PTR_ERR(nfsd_file_fsnotify_group)); 737231307dfSHuang Guobin ret = PTR_ERR(nfsd_file_fsnotify_group); 73865294c1fSJeff Layton nfsd_file_fsnotify_group = NULL; 73965294c1fSJeff Layton goto out_notifier; 74065294c1fSJeff Layton } 74165294c1fSJeff Layton 74265294c1fSJeff Layton for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) { 74365294c1fSJeff Layton INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head); 74465294c1fSJeff Layton spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock); 74565294c1fSJeff Layton } 74665294c1fSJeff Layton 7479542e6a6STrond Myklebust INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker); 74865294c1fSJeff Layton out: 74965294c1fSJeff Layton return ret; 75065294c1fSJeff Layton out_notifier: 75165294c1fSJeff Layton lease_unregister_notifier(&nfsd_file_lease_notifier); 75265294c1fSJeff Layton out_shrinker: 75365294c1fSJeff Layton unregister_shrinker(&nfsd_file_shrinker); 75465294c1fSJeff Layton out_lru: 75565294c1fSJeff Layton list_lru_destroy(&nfsd_file_lru); 75665294c1fSJeff Layton out_err: 75765294c1fSJeff Layton kmem_cache_destroy(nfsd_file_slab); 75865294c1fSJeff Layton nfsd_file_slab = NULL; 75965294c1fSJeff Layton kmem_cache_destroy(nfsd_file_mark_slab); 76065294c1fSJeff Layton nfsd_file_mark_slab = NULL; 7614d2eeafeSAmir Goldstein kvfree(nfsd_file_hashtbl); 76265294c1fSJeff Layton nfsd_file_hashtbl = NULL; 7639542e6a6STrond Myklebust destroy_workqueue(nfsd_filecache_wq); 7649542e6a6STrond Myklebust nfsd_filecache_wq = NULL; 76565294c1fSJeff Layton goto out; 76665294c1fSJeff Layton } 76765294c1fSJeff Layton 76865294c1fSJeff Layton /* 76965294c1fSJeff Layton * Note this can deadlock with nfsd_file_lru_cb. 77065294c1fSJeff Layton */ 77165294c1fSJeff Layton void 7725e113224STrond Myklebust nfsd_file_cache_purge(struct net *net) 77365294c1fSJeff Layton { 77465294c1fSJeff Layton unsigned int i; 77565294c1fSJeff Layton struct nfsd_file *nf; 7765e113224STrond Myklebust struct hlist_node *next; 77765294c1fSJeff Layton LIST_HEAD(dispose); 77865294c1fSJeff Layton bool del; 77965294c1fSJeff Layton 78065294c1fSJeff Layton if (!nfsd_file_hashtbl) 78165294c1fSJeff Layton return; 78265294c1fSJeff Layton 78365294c1fSJeff Layton for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) { 7845e113224STrond Myklebust struct nfsd_fcache_bucket *nfb = &nfsd_file_hashtbl[i]; 7855e113224STrond Myklebust 7865e113224STrond Myklebust spin_lock(&nfb->nfb_lock); 7875e113224STrond Myklebust hlist_for_each_entry_safe(nf, next, &nfb->nfb_head, nf_node) { 7885e113224STrond Myklebust if (net && nf->nf_net != net) 7895e113224STrond Myklebust continue; 79065294c1fSJeff Layton del = nfsd_file_unhash_and_release_locked(nf, &dispose); 79165294c1fSJeff Layton 79265294c1fSJeff Layton /* 79365294c1fSJeff Layton * Deadlock detected! Something marked this entry as 79465294c1fSJeff Layton * unhased, but hasn't removed it from the hash list. 79565294c1fSJeff Layton */ 79665294c1fSJeff Layton WARN_ON_ONCE(!del); 79765294c1fSJeff Layton } 7985e113224STrond Myklebust spin_unlock(&nfb->nfb_lock); 79965294c1fSJeff Layton nfsd_file_dispose_list(&dispose); 80065294c1fSJeff Layton } 80165294c1fSJeff Layton } 80265294c1fSJeff Layton 8039542e6a6STrond Myklebust static struct nfsd_fcache_disposal * 8041463b38eSNeilBrown nfsd_alloc_fcache_disposal(void) 8059542e6a6STrond Myklebust { 8069542e6a6STrond Myklebust struct nfsd_fcache_disposal *l; 8079542e6a6STrond Myklebust 8089542e6a6STrond Myklebust l = kmalloc(sizeof(*l), GFP_KERNEL); 8099542e6a6STrond Myklebust if (!l) 8109542e6a6STrond Myklebust return NULL; 8119542e6a6STrond Myklebust INIT_WORK(&l->work, nfsd_file_delayed_close); 8129542e6a6STrond Myklebust spin_lock_init(&l->lock); 8139542e6a6STrond Myklebust INIT_LIST_HEAD(&l->freeme); 8149542e6a6STrond Myklebust return l; 8159542e6a6STrond Myklebust } 8169542e6a6STrond Myklebust 8179542e6a6STrond Myklebust static void 8189542e6a6STrond Myklebust nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l) 8199542e6a6STrond Myklebust { 8209542e6a6STrond Myklebust cancel_work_sync(&l->work); 8219542e6a6STrond Myklebust nfsd_file_dispose_list(&l->freeme); 8221463b38eSNeilBrown kfree(l); 8239542e6a6STrond Myklebust } 8249542e6a6STrond Myklebust 8259542e6a6STrond Myklebust static void 8269542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(struct net *net) 8279542e6a6STrond Myklebust { 8281463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8291463b38eSNeilBrown struct nfsd_fcache_disposal *l = nn->fcache_disposal; 8309542e6a6STrond Myklebust 8319542e6a6STrond Myklebust nfsd_free_fcache_disposal(l); 8329542e6a6STrond Myklebust } 8339542e6a6STrond Myklebust 8349542e6a6STrond Myklebust int 8359542e6a6STrond Myklebust nfsd_file_cache_start_net(struct net *net) 8369542e6a6STrond Myklebust { 8371463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 8381463b38eSNeilBrown 8391463b38eSNeilBrown nn->fcache_disposal = nfsd_alloc_fcache_disposal(); 8401463b38eSNeilBrown return nn->fcache_disposal ? 0 : -ENOMEM; 8419542e6a6STrond Myklebust } 8429542e6a6STrond Myklebust 8439542e6a6STrond Myklebust void 8449542e6a6STrond Myklebust nfsd_file_cache_shutdown_net(struct net *net) 8459542e6a6STrond Myklebust { 8469542e6a6STrond Myklebust nfsd_file_cache_purge(net); 8479542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(net); 8489542e6a6STrond Myklebust } 8499542e6a6STrond Myklebust 85065294c1fSJeff Layton void 85165294c1fSJeff Layton nfsd_file_cache_shutdown(void) 85265294c1fSJeff Layton { 8538b330f78SChuck Lever int i; 8548b330f78SChuck Lever 85565294c1fSJeff Layton set_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags); 85665294c1fSJeff Layton 85765294c1fSJeff Layton lease_unregister_notifier(&nfsd_file_lease_notifier); 85865294c1fSJeff Layton unregister_shrinker(&nfsd_file_shrinker); 85965294c1fSJeff Layton /* 86065294c1fSJeff Layton * make sure all callers of nfsd_file_lru_cb are done before 86165294c1fSJeff Layton * calling nfsd_file_cache_purge 86265294c1fSJeff Layton */ 86365294c1fSJeff Layton cancel_delayed_work_sync(&nfsd_filecache_laundrette); 8645e113224STrond Myklebust nfsd_file_cache_purge(NULL); 86565294c1fSJeff Layton list_lru_destroy(&nfsd_file_lru); 86665294c1fSJeff Layton rcu_barrier(); 86765294c1fSJeff Layton fsnotify_put_group(nfsd_file_fsnotify_group); 86865294c1fSJeff Layton nfsd_file_fsnotify_group = NULL; 86965294c1fSJeff Layton kmem_cache_destroy(nfsd_file_slab); 87065294c1fSJeff Layton nfsd_file_slab = NULL; 87165294c1fSJeff Layton fsnotify_wait_marks_destroyed(); 87265294c1fSJeff Layton kmem_cache_destroy(nfsd_file_mark_slab); 87365294c1fSJeff Layton nfsd_file_mark_slab = NULL; 8744d2eeafeSAmir Goldstein kvfree(nfsd_file_hashtbl); 87565294c1fSJeff Layton nfsd_file_hashtbl = NULL; 8769542e6a6STrond Myklebust destroy_workqueue(nfsd_filecache_wq); 8779542e6a6STrond Myklebust nfsd_filecache_wq = NULL; 8788b330f78SChuck Lever 8798b330f78SChuck Lever for_each_possible_cpu(i) { 8808b330f78SChuck Lever per_cpu(nfsd_file_cache_hits, i) = 0; 8818b330f78SChuck Lever per_cpu(nfsd_file_acquisitions, i) = 0; 8828b330f78SChuck Lever per_cpu(nfsd_file_releases, i) = 0; 8838b330f78SChuck Lever per_cpu(nfsd_file_total_age, i) = 0; 8848b330f78SChuck Lever per_cpu(nfsd_file_pages_flushed, i) = 0; 8858b330f78SChuck Lever per_cpu(nfsd_file_evictions, i) = 0; 8868b330f78SChuck Lever } 88765294c1fSJeff Layton } 88865294c1fSJeff Layton 88965294c1fSJeff Layton static bool 89065294c1fSJeff Layton nfsd_match_cred(const struct cred *c1, const struct cred *c2) 89165294c1fSJeff Layton { 89265294c1fSJeff Layton int i; 89365294c1fSJeff Layton 89465294c1fSJeff Layton if (!uid_eq(c1->fsuid, c2->fsuid)) 89565294c1fSJeff Layton return false; 89665294c1fSJeff Layton if (!gid_eq(c1->fsgid, c2->fsgid)) 89765294c1fSJeff Layton return false; 89865294c1fSJeff Layton if (c1->group_info == NULL || c2->group_info == NULL) 89965294c1fSJeff Layton return c1->group_info == c2->group_info; 90065294c1fSJeff Layton if (c1->group_info->ngroups != c2->group_info->ngroups) 90165294c1fSJeff Layton return false; 90265294c1fSJeff Layton for (i = 0; i < c1->group_info->ngroups; i++) { 90365294c1fSJeff Layton if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i])) 90465294c1fSJeff Layton return false; 90565294c1fSJeff Layton } 90665294c1fSJeff Layton return true; 90765294c1fSJeff Layton } 90865294c1fSJeff Layton 90965294c1fSJeff Layton static struct nfsd_file * 91065294c1fSJeff Layton nfsd_file_find_locked(struct inode *inode, unsigned int may_flags, 9115e113224STrond Myklebust unsigned int hashval, struct net *net) 91265294c1fSJeff Layton { 91365294c1fSJeff Layton struct nfsd_file *nf; 91465294c1fSJeff Layton unsigned char need = may_flags & NFSD_FILE_MAY_MASK; 91565294c1fSJeff Layton 91665294c1fSJeff Layton hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head, 917057a2274SMadhuparna Bhowmik nf_node, lockdep_is_held(&nfsd_file_hashtbl[hashval].nfb_lock)) { 918ae3c57b5SJ. Bruce Fields if (nf->nf_may != need) 91965294c1fSJeff Layton continue; 92065294c1fSJeff Layton if (nf->nf_inode != inode) 92165294c1fSJeff Layton continue; 9225e113224STrond Myklebust if (nf->nf_net != net) 9235e113224STrond Myklebust continue; 92465294c1fSJeff Layton if (!nfsd_match_cred(nf->nf_cred, current_cred())) 92565294c1fSJeff Layton continue; 926d30881f5STrond Myklebust if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) 927d30881f5STrond Myklebust continue; 92865294c1fSJeff Layton if (nfsd_file_get(nf) != NULL) 92965294c1fSJeff Layton return nf; 93065294c1fSJeff Layton } 93165294c1fSJeff Layton return NULL; 93265294c1fSJeff Layton } 93365294c1fSJeff Layton 93465294c1fSJeff Layton /** 93565294c1fSJeff Layton * nfsd_file_is_cached - are there any cached open files for this fh? 93665294c1fSJeff Layton * @inode: inode of the file to check 93765294c1fSJeff Layton * 93865294c1fSJeff Layton * Scan the hashtable for open files that match this fh. Returns true if there 93965294c1fSJeff Layton * are any, and false if not. 94065294c1fSJeff Layton */ 94165294c1fSJeff Layton bool 94265294c1fSJeff Layton nfsd_file_is_cached(struct inode *inode) 94365294c1fSJeff Layton { 94465294c1fSJeff Layton bool ret = false; 94565294c1fSJeff Layton struct nfsd_file *nf; 94665294c1fSJeff Layton unsigned int hashval; 94765294c1fSJeff Layton 94865294c1fSJeff Layton hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS); 94965294c1fSJeff Layton 95065294c1fSJeff Layton rcu_read_lock(); 95165294c1fSJeff Layton hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head, 95265294c1fSJeff Layton nf_node) { 95365294c1fSJeff Layton if (inode == nf->nf_inode) { 95465294c1fSJeff Layton ret = true; 95565294c1fSJeff Layton break; 95665294c1fSJeff Layton } 95765294c1fSJeff Layton } 95865294c1fSJeff Layton rcu_read_unlock(); 95965294c1fSJeff Layton trace_nfsd_file_is_cached(inode, hashval, (int)ret); 96065294c1fSJeff Layton return ret; 96165294c1fSJeff Layton } 96265294c1fSJeff Layton 963fb70bf12SChuck Lever static __be32 964fb70bf12SChuck Lever nfsd_do_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, 965fb70bf12SChuck Lever unsigned int may_flags, struct nfsd_file **pnf, bool open) 96665294c1fSJeff Layton { 96765294c1fSJeff Layton __be32 status; 9685e113224STrond Myklebust struct net *net = SVC_NET(rqstp); 96965294c1fSJeff Layton struct nfsd_file *nf, *new; 97065294c1fSJeff Layton struct inode *inode; 97165294c1fSJeff Layton unsigned int hashval; 97228c7d86bSTrond Myklebust bool retry = true; 97365294c1fSJeff Layton 97465294c1fSJeff Layton /* FIXME: skip this if fh_dentry is already set? */ 97565294c1fSJeff Layton status = fh_verify(rqstp, fhp, S_IFREG, 97665294c1fSJeff Layton may_flags|NFSD_MAY_OWNER_OVERRIDE); 97765294c1fSJeff Layton if (status != nfs_ok) 97865294c1fSJeff Layton return status; 97965294c1fSJeff Layton 98065294c1fSJeff Layton inode = d_inode(fhp->fh_dentry); 98165294c1fSJeff Layton hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS); 98265294c1fSJeff Layton retry: 98365294c1fSJeff Layton rcu_read_lock(); 9845e113224STrond Myklebust nf = nfsd_file_find_locked(inode, may_flags, hashval, net); 98565294c1fSJeff Layton rcu_read_unlock(); 98665294c1fSJeff Layton if (nf) 98765294c1fSJeff Layton goto wait_for_construction; 98865294c1fSJeff Layton 9895e113224STrond Myklebust new = nfsd_file_alloc(inode, may_flags, hashval, net); 99065294c1fSJeff Layton if (!new) { 99165294c1fSJeff Layton trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, 99265294c1fSJeff Layton NULL, nfserr_jukebox); 99365294c1fSJeff Layton return nfserr_jukebox; 99465294c1fSJeff Layton } 99565294c1fSJeff Layton 99665294c1fSJeff Layton spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock); 9975e113224STrond Myklebust nf = nfsd_file_find_locked(inode, may_flags, hashval, net); 99865294c1fSJeff Layton if (nf == NULL) 99965294c1fSJeff Layton goto open_file; 100065294c1fSJeff Layton spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); 100165294c1fSJeff Layton nfsd_file_slab_free(&new->nf_rcu); 100265294c1fSJeff Layton 100365294c1fSJeff Layton wait_for_construction: 100465294c1fSJeff Layton wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE); 100565294c1fSJeff Layton 100665294c1fSJeff Layton /* Did construction of this file fail? */ 100765294c1fSJeff Layton if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 100828c7d86bSTrond Myklebust if (!retry) { 100928c7d86bSTrond Myklebust status = nfserr_jukebox; 101028c7d86bSTrond Myklebust goto out; 101128c7d86bSTrond Myklebust } 101228c7d86bSTrond Myklebust retry = false; 101365294c1fSJeff Layton nfsd_file_put_noref(nf); 101465294c1fSJeff Layton goto retry; 101565294c1fSJeff Layton } 101665294c1fSJeff Layton 101765294c1fSJeff Layton this_cpu_inc(nfsd_file_cache_hits); 101865294c1fSJeff Layton 101923ba98deSJeff Layton status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags)); 102065294c1fSJeff Layton out: 102165294c1fSJeff Layton if (status == nfs_ok) { 102229d4bdbbSChuck Lever if (open) 102329d4bdbbSChuck Lever this_cpu_inc(nfsd_file_acquisitions); 102465294c1fSJeff Layton *pnf = nf; 102565294c1fSJeff Layton } else { 102665294c1fSJeff Layton nfsd_file_put(nf); 102765294c1fSJeff Layton nf = NULL; 102865294c1fSJeff Layton } 102965294c1fSJeff Layton 103065294c1fSJeff Layton trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, nf, status); 103165294c1fSJeff Layton return status; 103265294c1fSJeff Layton open_file: 103365294c1fSJeff Layton nf = new; 103465294c1fSJeff Layton /* Take reference for the hashtable */ 1035689827cdSTrond Myklebust refcount_inc(&nf->nf_ref); 103665294c1fSJeff Layton __set_bit(NFSD_FILE_HASHED, &nf->nf_flags); 103765294c1fSJeff Layton __set_bit(NFSD_FILE_PENDING, &nf->nf_flags); 1038*c46203acSChuck Lever nfsd_file_lru_add(nf); 103965294c1fSJeff Layton hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head); 104065294c1fSJeff Layton ++nfsd_file_hashtbl[hashval].nfb_count; 104165294c1fSJeff Layton nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount, 104265294c1fSJeff Layton nfsd_file_hashtbl[hashval].nfb_count); 104365294c1fSJeff Layton spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); 10449542e6a6STrond Myklebust if (atomic_long_inc_return(&nfsd_filecache_count) >= NFSD_FILE_LRU_THRESHOLD) 10459542e6a6STrond Myklebust nfsd_file_gc(); 104665294c1fSJeff Layton 104765294c1fSJeff Layton nf->nf_mark = nfsd_file_mark_find_or_create(nf); 1048fb70bf12SChuck Lever if (nf->nf_mark) { 10490122e882SChuck Lever if (open) { 1050f4d84c52SChuck Lever status = nfsd_open_verified(rqstp, fhp, may_flags, 1051f4d84c52SChuck Lever &nf->nf_file); 10520122e882SChuck Lever trace_nfsd_file_open(nf, status); 10530122e882SChuck Lever } else 1054fb70bf12SChuck Lever status = nfs_ok; 1055fb70bf12SChuck Lever } else 105665294c1fSJeff Layton status = nfserr_jukebox; 105765294c1fSJeff Layton /* 105865294c1fSJeff Layton * If construction failed, or we raced with a call to unlink() 105965294c1fSJeff Layton * then unhash. 106065294c1fSJeff Layton */ 106165294c1fSJeff Layton if (status != nfs_ok || inode->i_nlink == 0) { 106265294c1fSJeff Layton bool do_free; 106365294c1fSJeff Layton spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock); 106465294c1fSJeff Layton do_free = nfsd_file_unhash(nf); 106565294c1fSJeff Layton spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); 106665294c1fSJeff Layton if (do_free) 106765294c1fSJeff Layton nfsd_file_put_noref(nf); 106865294c1fSJeff Layton } 106965294c1fSJeff Layton clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags); 107065294c1fSJeff Layton smp_mb__after_atomic(); 107165294c1fSJeff Layton wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING); 107265294c1fSJeff Layton goto out; 107365294c1fSJeff Layton } 107465294c1fSJeff Layton 1075fb70bf12SChuck Lever /** 1076fb70bf12SChuck Lever * nfsd_file_acquire - Get a struct nfsd_file with an open file 1077fb70bf12SChuck Lever * @rqstp: the RPC transaction being executed 1078fb70bf12SChuck Lever * @fhp: the NFS filehandle of the file to be opened 1079fb70bf12SChuck Lever * @may_flags: NFSD_MAY_ settings for the file 1080fb70bf12SChuck Lever * @pnf: OUT: new or found "struct nfsd_file" object 1081fb70bf12SChuck Lever * 1082fb70bf12SChuck Lever * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in 1083fb70bf12SChuck Lever * network byte order is returned. 1084fb70bf12SChuck Lever */ 1085fb70bf12SChuck Lever __be32 1086fb70bf12SChuck Lever nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, 1087fb70bf12SChuck Lever unsigned int may_flags, struct nfsd_file **pnf) 1088fb70bf12SChuck Lever { 1089fb70bf12SChuck Lever return nfsd_do_file_acquire(rqstp, fhp, may_flags, pnf, true); 1090fb70bf12SChuck Lever } 1091fb70bf12SChuck Lever 1092fb70bf12SChuck Lever /** 1093fb70bf12SChuck Lever * nfsd_file_create - Get a struct nfsd_file, do not open 1094fb70bf12SChuck Lever * @rqstp: the RPC transaction being executed 1095fb70bf12SChuck Lever * @fhp: the NFS filehandle of the file just created 1096fb70bf12SChuck Lever * @may_flags: NFSD_MAY_ settings for the file 1097fb70bf12SChuck Lever * @pnf: OUT: new or found "struct nfsd_file" object 1098fb70bf12SChuck Lever * 1099fb70bf12SChuck Lever * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in 1100fb70bf12SChuck Lever * network byte order is returned. 1101fb70bf12SChuck Lever */ 1102fb70bf12SChuck Lever __be32 1103fb70bf12SChuck Lever nfsd_file_create(struct svc_rqst *rqstp, struct svc_fh *fhp, 1104fb70bf12SChuck Lever unsigned int may_flags, struct nfsd_file **pnf) 1105fb70bf12SChuck Lever { 1106fb70bf12SChuck Lever return nfsd_do_file_acquire(rqstp, fhp, may_flags, pnf, false); 1107fb70bf12SChuck Lever } 1108fb70bf12SChuck Lever 110965294c1fSJeff Layton /* 111065294c1fSJeff Layton * Note that fields may be added, removed or reordered in the future. Programs 111165294c1fSJeff Layton * scraping this file for info should test the labels to ensure they're 111265294c1fSJeff Layton * getting the correct field. 111365294c1fSJeff Layton */ 111465294c1fSJeff Layton static int nfsd_file_cache_stats_show(struct seq_file *m, void *v) 111565294c1fSJeff Layton { 1116df2aff52SChuck Lever unsigned long releases = 0, pages_flushed = 0, evictions = 0; 1117df2aff52SChuck Lever unsigned long hits = 0, acquisitions = 0; 111865294c1fSJeff Layton unsigned int i, count = 0, longest = 0; 1119904940e9SChuck Lever unsigned long lru = 0, total_age = 0; 112065294c1fSJeff Layton 112165294c1fSJeff Layton /* 112265294c1fSJeff Layton * No need for spinlocks here since we're not terribly interested in 112365294c1fSJeff Layton * accuracy. We do take the nfsd_mutex simply to ensure that we 112465294c1fSJeff Layton * don't end up racing with server shutdown 112565294c1fSJeff Layton */ 112665294c1fSJeff Layton mutex_lock(&nfsd_mutex); 112765294c1fSJeff Layton if (nfsd_file_hashtbl) { 112865294c1fSJeff Layton for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) { 112965294c1fSJeff Layton count += nfsd_file_hashtbl[i].nfb_count; 113065294c1fSJeff Layton longest = max(longest, nfsd_file_hashtbl[i].nfb_count); 113165294c1fSJeff Layton } 11320fd244c1SChuck Lever lru = list_lru_count(&nfsd_file_lru); 113365294c1fSJeff Layton } 113465294c1fSJeff Layton mutex_unlock(&nfsd_mutex); 113565294c1fSJeff Layton 113629d4bdbbSChuck Lever for_each_possible_cpu(i) { 113765294c1fSJeff Layton hits += per_cpu(nfsd_file_cache_hits, i); 113829d4bdbbSChuck Lever acquisitions += per_cpu(nfsd_file_acquisitions, i); 1139d6329327SChuck Lever releases += per_cpu(nfsd_file_releases, i); 1140904940e9SChuck Lever total_age += per_cpu(nfsd_file_total_age, i); 114194660cc1SChuck Lever evictions += per_cpu(nfsd_file_evictions, i); 1142df2aff52SChuck Lever pages_flushed += per_cpu(nfsd_file_pages_flushed, i); 114329d4bdbbSChuck Lever } 114465294c1fSJeff Layton 114565294c1fSJeff Layton seq_printf(m, "total entries: %u\n", count); 114665294c1fSJeff Layton seq_printf(m, "longest chain: %u\n", longest); 11470fd244c1SChuck Lever seq_printf(m, "lru entries: %lu\n", lru); 114865294c1fSJeff Layton seq_printf(m, "cache hits: %lu\n", hits); 114929d4bdbbSChuck Lever seq_printf(m, "acquisitions: %lu\n", acquisitions); 1150d6329327SChuck Lever seq_printf(m, "releases: %lu\n", releases); 115194660cc1SChuck Lever seq_printf(m, "evictions: %lu\n", evictions); 1152904940e9SChuck Lever if (releases) 1153904940e9SChuck Lever seq_printf(m, "mean age (ms): %ld\n", total_age / releases); 1154904940e9SChuck Lever else 1155904940e9SChuck Lever seq_printf(m, "mean age (ms): -\n"); 1156df2aff52SChuck Lever seq_printf(m, "pages flushed: %lu\n", pages_flushed); 115765294c1fSJeff Layton return 0; 115865294c1fSJeff Layton } 115965294c1fSJeff Layton 116065294c1fSJeff Layton int nfsd_file_cache_stats_open(struct inode *inode, struct file *file) 116165294c1fSJeff Layton { 116265294c1fSJeff Layton return single_open(file, nfsd_file_cache_stats_show, NULL); 116365294c1fSJeff Layton } 1164