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> 16fc22945eSChuck Lever #include <linux/rhashtable.h> 1765294c1fSJeff Layton 1865294c1fSJeff Layton #include "vfs.h" 1965294c1fSJeff Layton #include "nfsd.h" 2065294c1fSJeff Layton #include "nfsfh.h" 215e113224STrond Myklebust #include "netns.h" 2265294c1fSJeff Layton #include "filecache.h" 2365294c1fSJeff Layton #include "trace.h" 2465294c1fSJeff Layton 2565294c1fSJeff Layton #define NFSD_LAUNDRETTE_DELAY (2 * HZ) 2665294c1fSJeff Layton 27c7b824c3SChuck Lever #define NFSD_FILE_CACHE_UP (0) 2865294c1fSJeff Layton 2965294c1fSJeff Layton /* We only care about NFSD_MAY_READ/WRITE for this cache */ 3065294c1fSJeff Layton #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE) 3165294c1fSJeff Layton 3265294c1fSJeff Layton static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits); 3329d4bdbbSChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions); 34d6329327SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_releases); 35904940e9SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age); 36df2aff52SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_pages_flushed); 3794660cc1SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions); 3865294c1fSJeff Layton 399542e6a6STrond Myklebust struct nfsd_fcache_disposal { 409542e6a6STrond Myklebust struct work_struct work; 419542e6a6STrond Myklebust spinlock_t lock; 429542e6a6STrond Myklebust struct list_head freeme; 439542e6a6STrond Myklebust }; 449542e6a6STrond Myklebust 4550d0def9SChen Zhou static struct workqueue_struct *nfsd_filecache_wq __read_mostly; 469542e6a6STrond Myklebust 4765294c1fSJeff Layton static struct kmem_cache *nfsd_file_slab; 4865294c1fSJeff Layton static struct kmem_cache *nfsd_file_mark_slab; 4965294c1fSJeff Layton static struct list_lru nfsd_file_lru; 50c7b824c3SChuck Lever static unsigned long nfsd_file_flags; 5165294c1fSJeff Layton static struct fsnotify_group *nfsd_file_fsnotify_group; 5265294c1fSJeff Layton static struct delayed_work nfsd_filecache_laundrette; 53fc22945eSChuck Lever static struct rhashtable nfsd_file_rhash_tbl 54fc22945eSChuck Lever ____cacheline_aligned_in_smp; 55fc22945eSChuck Lever 56fc22945eSChuck Lever enum nfsd_file_lookup_type { 57fc22945eSChuck Lever NFSD_FILE_KEY_INODE, 58fc22945eSChuck Lever NFSD_FILE_KEY_FULL, 59fc22945eSChuck Lever }; 60fc22945eSChuck Lever 61fc22945eSChuck Lever struct nfsd_file_lookup_key { 62fc22945eSChuck Lever struct inode *inode; 63fc22945eSChuck Lever struct net *net; 64fc22945eSChuck Lever const struct cred *cred; 65fc22945eSChuck Lever unsigned char need; 66fc22945eSChuck Lever enum nfsd_file_lookup_type type; 67fc22945eSChuck Lever }; 68fc22945eSChuck Lever 69fc22945eSChuck Lever /* 70fc22945eSChuck Lever * The returned hash value is based solely on the address of an in-code 71fc22945eSChuck Lever * inode, a pointer to a slab-allocated object. The entropy in such a 72fc22945eSChuck Lever * pointer is concentrated in its middle bits. 73fc22945eSChuck Lever */ 74fc22945eSChuck Lever static u32 nfsd_file_inode_hash(const struct inode *inode, u32 seed) 75fc22945eSChuck Lever { 76fc22945eSChuck Lever unsigned long ptr = (unsigned long)inode; 77fc22945eSChuck Lever u32 k; 78fc22945eSChuck Lever 79fc22945eSChuck Lever k = ptr >> L1_CACHE_SHIFT; 80fc22945eSChuck Lever k &= 0x00ffffff; 81fc22945eSChuck Lever return jhash2(&k, 1, seed); 82fc22945eSChuck Lever } 83fc22945eSChuck Lever 84fc22945eSChuck Lever /** 85fc22945eSChuck Lever * nfsd_file_key_hashfn - Compute the hash value of a lookup key 86fc22945eSChuck Lever * @data: key on which to compute the hash value 87fc22945eSChuck Lever * @len: rhash table's key_len parameter (unused) 88fc22945eSChuck Lever * @seed: rhash table's random seed of the day 89fc22945eSChuck Lever * 90fc22945eSChuck Lever * Return value: 91fc22945eSChuck Lever * Computed 32-bit hash value 92fc22945eSChuck Lever */ 93fc22945eSChuck Lever static u32 nfsd_file_key_hashfn(const void *data, u32 len, u32 seed) 94fc22945eSChuck Lever { 95fc22945eSChuck Lever const struct nfsd_file_lookup_key *key = data; 96fc22945eSChuck Lever 97fc22945eSChuck Lever return nfsd_file_inode_hash(key->inode, seed); 98fc22945eSChuck Lever } 99fc22945eSChuck Lever 100fc22945eSChuck Lever /** 101fc22945eSChuck Lever * nfsd_file_obj_hashfn - Compute the hash value of an nfsd_file 102fc22945eSChuck Lever * @data: object on which to compute the hash value 103fc22945eSChuck Lever * @len: rhash table's key_len parameter (unused) 104fc22945eSChuck Lever * @seed: rhash table's random seed of the day 105fc22945eSChuck Lever * 106fc22945eSChuck Lever * Return value: 107fc22945eSChuck Lever * Computed 32-bit hash value 108fc22945eSChuck Lever */ 109fc22945eSChuck Lever static u32 nfsd_file_obj_hashfn(const void *data, u32 len, u32 seed) 110fc22945eSChuck Lever { 111fc22945eSChuck Lever const struct nfsd_file *nf = data; 112fc22945eSChuck Lever 113fc22945eSChuck Lever return nfsd_file_inode_hash(nf->nf_inode, seed); 114fc22945eSChuck Lever } 115fc22945eSChuck Lever 116fc22945eSChuck Lever static bool 117fc22945eSChuck Lever nfsd_match_cred(const struct cred *c1, const struct cred *c2) 118fc22945eSChuck Lever { 119fc22945eSChuck Lever int i; 120fc22945eSChuck Lever 121fc22945eSChuck Lever if (!uid_eq(c1->fsuid, c2->fsuid)) 122fc22945eSChuck Lever return false; 123fc22945eSChuck Lever if (!gid_eq(c1->fsgid, c2->fsgid)) 124fc22945eSChuck Lever return false; 125fc22945eSChuck Lever if (c1->group_info == NULL || c2->group_info == NULL) 126fc22945eSChuck Lever return c1->group_info == c2->group_info; 127fc22945eSChuck Lever if (c1->group_info->ngroups != c2->group_info->ngroups) 128fc22945eSChuck Lever return false; 129fc22945eSChuck Lever for (i = 0; i < c1->group_info->ngroups; i++) { 130fc22945eSChuck Lever if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i])) 131fc22945eSChuck Lever return false; 132fc22945eSChuck Lever } 133fc22945eSChuck Lever return true; 134fc22945eSChuck Lever } 135fc22945eSChuck Lever 136fc22945eSChuck Lever /** 137fc22945eSChuck Lever * nfsd_file_obj_cmpfn - Match a cache item against search criteria 138fc22945eSChuck Lever * @arg: search criteria 139fc22945eSChuck Lever * @ptr: cache item to check 140fc22945eSChuck Lever * 141fc22945eSChuck Lever * Return values: 142fc22945eSChuck Lever * %0 - Item matches search criteria 143fc22945eSChuck Lever * %1 - Item does not match search criteria 144fc22945eSChuck Lever */ 145fc22945eSChuck Lever static int nfsd_file_obj_cmpfn(struct rhashtable_compare_arg *arg, 146fc22945eSChuck Lever const void *ptr) 147fc22945eSChuck Lever { 148fc22945eSChuck Lever const struct nfsd_file_lookup_key *key = arg->key; 149fc22945eSChuck Lever const struct nfsd_file *nf = ptr; 150fc22945eSChuck Lever 151fc22945eSChuck Lever switch (key->type) { 152fc22945eSChuck Lever case NFSD_FILE_KEY_INODE: 153fc22945eSChuck Lever if (nf->nf_inode != key->inode) 154fc22945eSChuck Lever return 1; 155fc22945eSChuck Lever break; 156fc22945eSChuck Lever case NFSD_FILE_KEY_FULL: 157fc22945eSChuck Lever if (nf->nf_inode != key->inode) 158fc22945eSChuck Lever return 1; 159fc22945eSChuck Lever if (nf->nf_may != key->need) 160fc22945eSChuck Lever return 1; 161fc22945eSChuck Lever if (nf->nf_net != key->net) 162fc22945eSChuck Lever return 1; 163fc22945eSChuck Lever if (!nfsd_match_cred(nf->nf_cred, key->cred)) 164fc22945eSChuck Lever return 1; 165fc22945eSChuck Lever if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) 166fc22945eSChuck Lever return 1; 167fc22945eSChuck Lever break; 168fc22945eSChuck Lever } 169fc22945eSChuck Lever return 0; 170fc22945eSChuck Lever } 171fc22945eSChuck Lever 172fc22945eSChuck Lever static const struct rhashtable_params nfsd_file_rhash_params = { 173fc22945eSChuck Lever .key_len = sizeof_field(struct nfsd_file, nf_inode), 174fc22945eSChuck Lever .key_offset = offsetof(struct nfsd_file, nf_inode), 175fc22945eSChuck Lever .head_offset = offsetof(struct nfsd_file, nf_rhash), 176fc22945eSChuck Lever .hashfn = nfsd_file_key_hashfn, 177fc22945eSChuck Lever .obj_hashfn = nfsd_file_obj_hashfn, 178fc22945eSChuck Lever .obj_cmpfn = nfsd_file_obj_cmpfn, 179fc22945eSChuck Lever /* Reduce resizing churn on light workloads */ 180fc22945eSChuck Lever .min_size = 512, /* buckets */ 181fc22945eSChuck Lever .automatic_shrinking = true, 182fc22945eSChuck Lever }; 18365294c1fSJeff Layton 18465294c1fSJeff Layton static void 1859542e6a6STrond Myklebust nfsd_file_schedule_laundrette(void) 18665294c1fSJeff Layton { 187ce502f81SChuck Lever if ((atomic_read(&nfsd_file_rhash_tbl.nelems) == 0) || 188c7b824c3SChuck Lever test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0) 18965294c1fSJeff Layton return; 19065294c1fSJeff Layton 1919542e6a6STrond Myklebust queue_delayed_work(system_wq, &nfsd_filecache_laundrette, 1929542e6a6STrond Myklebust NFSD_LAUNDRETTE_DELAY); 19365294c1fSJeff Layton } 19465294c1fSJeff Layton 19565294c1fSJeff Layton static void 19665294c1fSJeff Layton nfsd_file_slab_free(struct rcu_head *rcu) 19765294c1fSJeff Layton { 19865294c1fSJeff Layton struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu); 19965294c1fSJeff Layton 20065294c1fSJeff Layton put_cred(nf->nf_cred); 20165294c1fSJeff Layton kmem_cache_free(nfsd_file_slab, nf); 20265294c1fSJeff Layton } 20365294c1fSJeff Layton 20465294c1fSJeff Layton static void 20565294c1fSJeff Layton nfsd_file_mark_free(struct fsnotify_mark *mark) 20665294c1fSJeff Layton { 20765294c1fSJeff Layton struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark, 20865294c1fSJeff Layton nfm_mark); 20965294c1fSJeff Layton 21065294c1fSJeff Layton kmem_cache_free(nfsd_file_mark_slab, nfm); 21165294c1fSJeff Layton } 21265294c1fSJeff Layton 21365294c1fSJeff Layton static struct nfsd_file_mark * 21465294c1fSJeff Layton nfsd_file_mark_get(struct nfsd_file_mark *nfm) 21565294c1fSJeff Layton { 216689827cdSTrond Myklebust if (!refcount_inc_not_zero(&nfm->nfm_ref)) 21765294c1fSJeff Layton return NULL; 21865294c1fSJeff Layton return nfm; 21965294c1fSJeff Layton } 22065294c1fSJeff Layton 22165294c1fSJeff Layton static void 22265294c1fSJeff Layton nfsd_file_mark_put(struct nfsd_file_mark *nfm) 22365294c1fSJeff Layton { 224689827cdSTrond Myklebust if (refcount_dec_and_test(&nfm->nfm_ref)) { 22565294c1fSJeff Layton fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group); 22665294c1fSJeff Layton fsnotify_put_mark(&nfm->nfm_mark); 22765294c1fSJeff Layton } 22865294c1fSJeff Layton } 22965294c1fSJeff Layton 23065294c1fSJeff Layton static struct nfsd_file_mark * 23165294c1fSJeff Layton nfsd_file_mark_find_or_create(struct nfsd_file *nf) 23265294c1fSJeff Layton { 23365294c1fSJeff Layton int err; 23465294c1fSJeff Layton struct fsnotify_mark *mark; 23565294c1fSJeff Layton struct nfsd_file_mark *nfm = NULL, *new; 23665294c1fSJeff Layton struct inode *inode = nf->nf_inode; 23765294c1fSJeff Layton 23865294c1fSJeff Layton do { 239b8962a9dSAmir Goldstein fsnotify_group_lock(nfsd_file_fsnotify_group); 24065294c1fSJeff Layton mark = fsnotify_find_mark(&inode->i_fsnotify_marks, 24165294c1fSJeff Layton nfsd_file_fsnotify_group); 24265294c1fSJeff Layton if (mark) { 24365294c1fSJeff Layton nfm = nfsd_file_mark_get(container_of(mark, 24465294c1fSJeff Layton struct nfsd_file_mark, 24565294c1fSJeff Layton nfm_mark)); 246b8962a9dSAmir Goldstein fsnotify_group_unlock(nfsd_file_fsnotify_group); 24790d2f1daSTrond Myklebust if (nfm) { 24865294c1fSJeff Layton fsnotify_put_mark(mark); 24965294c1fSJeff Layton break; 25090d2f1daSTrond Myklebust } 25190d2f1daSTrond Myklebust /* Avoid soft lockup race with nfsd_file_mark_put() */ 25290d2f1daSTrond Myklebust fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group); 25390d2f1daSTrond Myklebust fsnotify_put_mark(mark); 254b8962a9dSAmir Goldstein } else { 255b8962a9dSAmir Goldstein fsnotify_group_unlock(nfsd_file_fsnotify_group); 256b8962a9dSAmir Goldstein } 25765294c1fSJeff Layton 25865294c1fSJeff Layton /* allocate a new nfm */ 25965294c1fSJeff Layton new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL); 26065294c1fSJeff Layton if (!new) 26165294c1fSJeff Layton return NULL; 26265294c1fSJeff Layton fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group); 26365294c1fSJeff Layton new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF; 264689827cdSTrond Myklebust refcount_set(&new->nfm_ref, 1); 26565294c1fSJeff Layton 26665294c1fSJeff Layton err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0); 26765294c1fSJeff Layton 26865294c1fSJeff Layton /* 26965294c1fSJeff Layton * If the add was successful, then return the object. 27065294c1fSJeff Layton * Otherwise, we need to put the reference we hold on the 27165294c1fSJeff Layton * nfm_mark. The fsnotify code will take a reference and put 27265294c1fSJeff Layton * it on failure, so we can't just free it directly. It's also 27365294c1fSJeff Layton * not safe to call fsnotify_destroy_mark on it as the 27465294c1fSJeff Layton * mark->group will be NULL. Thus, we can't let the nfm_ref 27565294c1fSJeff Layton * counter drive the destruction at this point. 27665294c1fSJeff Layton */ 27765294c1fSJeff Layton if (likely(!err)) 27865294c1fSJeff Layton nfm = new; 27965294c1fSJeff Layton else 28065294c1fSJeff Layton fsnotify_put_mark(&new->nfm_mark); 28165294c1fSJeff Layton } while (unlikely(err == -EEXIST)); 28265294c1fSJeff Layton 28365294c1fSJeff Layton return nfm; 28465294c1fSJeff Layton } 28565294c1fSJeff Layton 28665294c1fSJeff Layton static struct nfsd_file * 287ce502f81SChuck Lever nfsd_file_alloc(struct nfsd_file_lookup_key *key, unsigned int may) 28865294c1fSJeff Layton { 28965294c1fSJeff Layton struct nfsd_file *nf; 29065294c1fSJeff Layton 29165294c1fSJeff Layton nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL); 29265294c1fSJeff Layton if (nf) { 29365294c1fSJeff Layton INIT_LIST_HEAD(&nf->nf_lru); 294904940e9SChuck Lever nf->nf_birthtime = ktime_get(); 29565294c1fSJeff Layton nf->nf_file = NULL; 29665294c1fSJeff Layton nf->nf_cred = get_current_cred(); 297ce502f81SChuck Lever nf->nf_net = key->net; 29865294c1fSJeff Layton nf->nf_flags = 0; 299ce502f81SChuck Lever __set_bit(NFSD_FILE_HASHED, &nf->nf_flags); 300ce502f81SChuck Lever __set_bit(NFSD_FILE_PENDING, &nf->nf_flags); 301ce502f81SChuck Lever nf->nf_inode = key->inode; 302ce502f81SChuck Lever /* nf_ref is pre-incremented for hash table */ 303ce502f81SChuck Lever refcount_set(&nf->nf_ref, 2); 304ce502f81SChuck Lever nf->nf_may = key->need; 30565294c1fSJeff Layton nf->nf_mark = NULL; 30665294c1fSJeff Layton trace_nfsd_file_alloc(nf); 30765294c1fSJeff Layton } 30865294c1fSJeff Layton return nf; 30965294c1fSJeff Layton } 31065294c1fSJeff Layton 31165294c1fSJeff Layton static bool 31265294c1fSJeff Layton nfsd_file_free(struct nfsd_file *nf) 31365294c1fSJeff Layton { 314904940e9SChuck Lever s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime)); 31565294c1fSJeff Layton bool flush = false; 31665294c1fSJeff Layton 317d6329327SChuck Lever this_cpu_inc(nfsd_file_releases); 318904940e9SChuck Lever this_cpu_add(nfsd_file_total_age, age); 319d6329327SChuck Lever 32065294c1fSJeff Layton trace_nfsd_file_put_final(nf); 32165294c1fSJeff Layton if (nf->nf_mark) 32265294c1fSJeff Layton nfsd_file_mark_put(nf->nf_mark); 32365294c1fSJeff Layton if (nf->nf_file) { 32465294c1fSJeff Layton get_file(nf->nf_file); 32565294c1fSJeff Layton filp_close(nf->nf_file, NULL); 32665294c1fSJeff Layton fput(nf->nf_file); 32765294c1fSJeff Layton flush = true; 32865294c1fSJeff Layton } 329668ed92eSChuck Lever 330668ed92eSChuck Lever /* 331668ed92eSChuck Lever * If this item is still linked via nf_lru, that's a bug. 332668ed92eSChuck Lever * WARN and leak it to preserve system stability. 333668ed92eSChuck Lever */ 334668ed92eSChuck Lever if (WARN_ON_ONCE(!list_empty(&nf->nf_lru))) 335668ed92eSChuck Lever return flush; 336668ed92eSChuck Lever 33765294c1fSJeff Layton call_rcu(&nf->nf_rcu, nfsd_file_slab_free); 33865294c1fSJeff Layton return flush; 33965294c1fSJeff Layton } 34065294c1fSJeff Layton 341055b24a8STrond Myklebust static bool 342055b24a8STrond Myklebust nfsd_file_check_writeback(struct nfsd_file *nf) 343055b24a8STrond Myklebust { 344055b24a8STrond Myklebust struct file *file = nf->nf_file; 345055b24a8STrond Myklebust struct address_space *mapping; 346055b24a8STrond Myklebust 347055b24a8STrond Myklebust if (!file || !(file->f_mode & FMODE_WRITE)) 348055b24a8STrond Myklebust return false; 349055b24a8STrond Myklebust mapping = file->f_mapping; 350055b24a8STrond Myklebust return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) || 351055b24a8STrond Myklebust mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK); 352055b24a8STrond Myklebust } 353055b24a8STrond Myklebust 354055b24a8STrond Myklebust static int 355055b24a8STrond Myklebust nfsd_file_check_write_error(struct nfsd_file *nf) 356055b24a8STrond Myklebust { 357055b24a8STrond Myklebust struct file *file = nf->nf_file; 358055b24a8STrond Myklebust 359055b24a8STrond Myklebust if (!file || !(file->f_mode & FMODE_WRITE)) 360055b24a8STrond Myklebust return 0; 361055b24a8STrond Myklebust return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err)); 362055b24a8STrond Myklebust } 363055b24a8STrond Myklebust 36465294c1fSJeff Layton static void 3656b8a9433STrond Myklebust nfsd_file_flush(struct nfsd_file *nf) 3666b8a9433STrond Myklebust { 367df2aff52SChuck Lever struct file *file = nf->nf_file; 368df2aff52SChuck Lever 369df2aff52SChuck Lever if (!file || !(file->f_mode & FMODE_WRITE)) 370df2aff52SChuck Lever return; 371df2aff52SChuck Lever this_cpu_add(nfsd_file_pages_flushed, file->f_mapping->nrpages); 372df2aff52SChuck Lever if (vfs_fsync(file, 1) != 0) 3736b8a9433STrond Myklebust nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 3746b8a9433STrond Myklebust } 3756b8a9433STrond Myklebust 376c46203acSChuck Lever static void nfsd_file_lru_add(struct nfsd_file *nf) 377c46203acSChuck Lever { 3784a0e73e6SChuck Lever set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags); 379c46203acSChuck Lever if (list_lru_add(&nfsd_file_lru, &nf->nf_lru)) 380c46203acSChuck Lever trace_nfsd_file_lru_add(nf); 381c46203acSChuck Lever } 382c46203acSChuck Lever 383c46203acSChuck Lever static void nfsd_file_lru_remove(struct nfsd_file *nf) 384c46203acSChuck Lever { 385c46203acSChuck Lever if (list_lru_del(&nfsd_file_lru, &nf->nf_lru)) 386c46203acSChuck Lever trace_nfsd_file_lru_del(nf); 387c46203acSChuck Lever } 388c46203acSChuck Lever 3896b8a9433STrond Myklebust static void 390ce502f81SChuck Lever nfsd_file_hash_remove(struct nfsd_file *nf) 39165294c1fSJeff Layton { 39265294c1fSJeff Layton trace_nfsd_file_unhash(nf); 39365294c1fSJeff Layton 394055b24a8STrond Myklebust if (nfsd_file_check_write_error(nf)) 3953988a578SChuck Lever nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 396ce502f81SChuck Lever rhashtable_remove_fast(&nfsd_file_rhash_tbl, &nf->nf_rhash, 397ce502f81SChuck Lever nfsd_file_rhash_params); 398cb7ec76eSChuck Lever } 399cb7ec76eSChuck Lever 40065294c1fSJeff Layton static bool 40165294c1fSJeff Layton nfsd_file_unhash(struct nfsd_file *nf) 40265294c1fSJeff Layton { 40365294c1fSJeff Layton if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 404ce502f81SChuck Lever nfsd_file_hash_remove(nf); 40565294c1fSJeff Layton return true; 40665294c1fSJeff Layton } 40765294c1fSJeff Layton return false; 40865294c1fSJeff Layton } 40965294c1fSJeff Layton 41065294c1fSJeff Layton /* 41165294c1fSJeff Layton * Return true if the file was unhashed. 41265294c1fSJeff Layton */ 41365294c1fSJeff Layton static bool 414ce502f81SChuck Lever nfsd_file_unhash_and_dispose(struct nfsd_file *nf, struct list_head *dispose) 41565294c1fSJeff Layton { 416ce502f81SChuck Lever trace_nfsd_file_unhash_and_dispose(nf); 41765294c1fSJeff Layton if (!nfsd_file_unhash(nf)) 41865294c1fSJeff Layton return false; 41965294c1fSJeff Layton /* keep final reference for nfsd_file_lru_dispose */ 420689827cdSTrond Myklebust if (refcount_dec_not_one(&nf->nf_ref)) 42165294c1fSJeff Layton return true; 42265294c1fSJeff Layton 4234a0e73e6SChuck Lever nfsd_file_lru_remove(nf); 42465294c1fSJeff Layton list_add(&nf->nf_lru, dispose); 42565294c1fSJeff Layton return true; 42665294c1fSJeff Layton } 42765294c1fSJeff Layton 428b6669305STrond Myklebust static void 42965294c1fSJeff Layton nfsd_file_put_noref(struct nfsd_file *nf) 43065294c1fSJeff Layton { 43165294c1fSJeff Layton trace_nfsd_file_put(nf); 43265294c1fSJeff Layton 433689827cdSTrond Myklebust if (refcount_dec_and_test(&nf->nf_ref)) { 43465294c1fSJeff Layton WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags)); 4354a0e73e6SChuck Lever nfsd_file_lru_remove(nf); 43665294c1fSJeff Layton nfsd_file_free(nf); 43765294c1fSJeff Layton } 43865294c1fSJeff Layton } 43965294c1fSJeff Layton 44065294c1fSJeff Layton void 44165294c1fSJeff Layton nfsd_file_put(struct nfsd_file *nf) 44265294c1fSJeff Layton { 44308af54b3SChuck Lever might_sleep(); 44408af54b3SChuck Lever 4454a0e73e6SChuck Lever nfsd_file_lru_add(nf); 44699939792STrond Myklebust if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) { 4476b8a9433STrond Myklebust nfsd_file_flush(nf); 448b6669305STrond Myklebust nfsd_file_put_noref(nf); 449b6c71c66SChuck Lever } else if (nf->nf_file) { 450b6669305STrond Myklebust nfsd_file_put_noref(nf); 4519542e6a6STrond Myklebust nfsd_file_schedule_laundrette(); 452b6c71c66SChuck Lever } else 453b6c71c66SChuck Lever nfsd_file_put_noref(nf); 45465294c1fSJeff Layton } 45565294c1fSJeff Layton 45665294c1fSJeff Layton struct nfsd_file * 45765294c1fSJeff Layton nfsd_file_get(struct nfsd_file *nf) 45865294c1fSJeff Layton { 459689827cdSTrond Myklebust if (likely(refcount_inc_not_zero(&nf->nf_ref))) 46065294c1fSJeff Layton return nf; 46165294c1fSJeff Layton return NULL; 46265294c1fSJeff Layton } 46365294c1fSJeff Layton 46465294c1fSJeff Layton static void 46565294c1fSJeff Layton nfsd_file_dispose_list(struct list_head *dispose) 46665294c1fSJeff Layton { 46765294c1fSJeff Layton struct nfsd_file *nf; 46865294c1fSJeff Layton 46965294c1fSJeff Layton while(!list_empty(dispose)) { 47065294c1fSJeff Layton nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 471668ed92eSChuck Lever list_del_init(&nf->nf_lru); 4726b8a9433STrond Myklebust nfsd_file_flush(nf); 47365294c1fSJeff Layton nfsd_file_put_noref(nf); 47465294c1fSJeff Layton } 47565294c1fSJeff Layton } 47665294c1fSJeff Layton 47765294c1fSJeff Layton static void 47865294c1fSJeff Layton nfsd_file_dispose_list_sync(struct list_head *dispose) 47965294c1fSJeff Layton { 48065294c1fSJeff Layton bool flush = false; 48165294c1fSJeff Layton struct nfsd_file *nf; 48265294c1fSJeff Layton 48365294c1fSJeff Layton while(!list_empty(dispose)) { 48465294c1fSJeff Layton nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 485668ed92eSChuck Lever list_del_init(&nf->nf_lru); 4866b8a9433STrond Myklebust nfsd_file_flush(nf); 487689827cdSTrond Myklebust if (!refcount_dec_and_test(&nf->nf_ref)) 48865294c1fSJeff Layton continue; 48965294c1fSJeff Layton if (nfsd_file_free(nf)) 49065294c1fSJeff Layton flush = true; 49165294c1fSJeff Layton } 49265294c1fSJeff Layton if (flush) 49365294c1fSJeff Layton flush_delayed_fput(); 49465294c1fSJeff Layton } 49565294c1fSJeff Layton 4969542e6a6STrond Myklebust static void 4979542e6a6STrond Myklebust nfsd_file_list_remove_disposal(struct list_head *dst, 4989542e6a6STrond Myklebust struct nfsd_fcache_disposal *l) 4999542e6a6STrond Myklebust { 5009542e6a6STrond Myklebust spin_lock(&l->lock); 5019542e6a6STrond Myklebust list_splice_init(&l->freeme, dst); 5029542e6a6STrond Myklebust spin_unlock(&l->lock); 5039542e6a6STrond Myklebust } 5049542e6a6STrond Myklebust 5059542e6a6STrond Myklebust static void 5069542e6a6STrond Myklebust nfsd_file_list_add_disposal(struct list_head *files, struct net *net) 5079542e6a6STrond Myklebust { 5081463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 5091463b38eSNeilBrown struct nfsd_fcache_disposal *l = nn->fcache_disposal; 5109542e6a6STrond Myklebust 5119542e6a6STrond Myklebust spin_lock(&l->lock); 5129542e6a6STrond Myklebust list_splice_tail_init(files, &l->freeme); 5139542e6a6STrond Myklebust spin_unlock(&l->lock); 5149542e6a6STrond Myklebust queue_work(nfsd_filecache_wq, &l->work); 5159542e6a6STrond Myklebust } 5169542e6a6STrond Myklebust 5179542e6a6STrond Myklebust static void 5189542e6a6STrond Myklebust nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src, 5199542e6a6STrond Myklebust struct net *net) 5209542e6a6STrond Myklebust { 5219542e6a6STrond Myklebust struct nfsd_file *nf, *tmp; 5229542e6a6STrond Myklebust 5239542e6a6STrond Myklebust list_for_each_entry_safe(nf, tmp, src, nf_lru) { 5249542e6a6STrond Myklebust if (nf->nf_net == net) 5259542e6a6STrond Myklebust list_move_tail(&nf->nf_lru, dst); 5269542e6a6STrond Myklebust } 5279542e6a6STrond Myklebust } 5289542e6a6STrond Myklebust 5299542e6a6STrond Myklebust static void 5309542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(struct list_head *dispose) 5319542e6a6STrond Myklebust { 5329542e6a6STrond Myklebust LIST_HEAD(list); 5339542e6a6STrond Myklebust struct nfsd_file *nf; 5349542e6a6STrond Myklebust 5359542e6a6STrond Myklebust while(!list_empty(dispose)) { 5369542e6a6STrond Myklebust nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 5379542e6a6STrond Myklebust nfsd_file_list_add_pernet(&list, dispose, nf->nf_net); 5389542e6a6STrond Myklebust nfsd_file_list_add_disposal(&list, nf->nf_net); 5399542e6a6STrond Myklebust } 5409542e6a6STrond Myklebust } 5419542e6a6STrond Myklebust 5424a0e73e6SChuck Lever /** 5434a0e73e6SChuck Lever * nfsd_file_lru_cb - Examine an entry on the LRU list 5444a0e73e6SChuck Lever * @item: LRU entry to examine 5454a0e73e6SChuck Lever * @lru: controlling LRU 5464a0e73e6SChuck Lever * @lock: LRU list lock (unused) 5474a0e73e6SChuck Lever * @arg: dispose list 5484a0e73e6SChuck Lever * 54965294c1fSJeff Layton * Note this can deadlock with nfsd_file_cache_purge. 5504a0e73e6SChuck Lever * 5514a0e73e6SChuck Lever * Return values: 5524a0e73e6SChuck Lever * %LRU_REMOVED: @item was removed from the LRU 553edead3a5SChuck Lever * %LRU_ROTATE: @item is to be moved to the LRU tail 5544a0e73e6SChuck Lever * %LRU_SKIP: @item cannot be evicted 55565294c1fSJeff Layton */ 55665294c1fSJeff Layton static enum lru_status 55765294c1fSJeff Layton nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru, 55865294c1fSJeff Layton spinlock_t *lock, void *arg) 55965294c1fSJeff Layton __releases(lock) 56065294c1fSJeff Layton __acquires(lock) 56165294c1fSJeff Layton { 56265294c1fSJeff Layton struct list_head *head = arg; 56365294c1fSJeff Layton struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru); 56465294c1fSJeff Layton 56565294c1fSJeff Layton /* 56665294c1fSJeff Layton * Do a lockless refcount check. The hashtable holds one reference, so 56765294c1fSJeff Layton * we look to see if anything else has a reference, or if any have 56865294c1fSJeff Layton * been put since the shrinker last ran. Those don't get unhashed and 56965294c1fSJeff Layton * released. 57065294c1fSJeff Layton * 57165294c1fSJeff Layton * Note that in the put path, we set the flag and then decrement the 57265294c1fSJeff Layton * counter. Here we check the counter and then test and clear the flag. 57365294c1fSJeff Layton * That order is deliberate to ensure that we can do this locklessly. 57465294c1fSJeff Layton */ 575c46203acSChuck Lever if (refcount_read(&nf->nf_ref) > 1) { 5764a0e73e6SChuck Lever list_lru_isolate(lru, &nf->nf_lru); 577c46203acSChuck Lever trace_nfsd_file_gc_in_use(nf); 5784a0e73e6SChuck Lever return LRU_REMOVED; 579c46203acSChuck Lever } 580055b24a8STrond Myklebust 581055b24a8STrond Myklebust /* 582055b24a8STrond Myklebust * Don't throw out files that are still undergoing I/O or 583055b24a8STrond Myklebust * that have uncleared errors pending. 584055b24a8STrond Myklebust */ 585c46203acSChuck Lever if (nfsd_file_check_writeback(nf)) { 586c46203acSChuck Lever trace_nfsd_file_gc_writeback(nf); 587c46203acSChuck Lever return LRU_SKIP; 588c46203acSChuck Lever } 589055b24a8STrond Myklebust 590c46203acSChuck Lever if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) { 591c46203acSChuck Lever trace_nfsd_file_gc_referenced(nf); 592edead3a5SChuck Lever return LRU_ROTATE; 593c46203acSChuck Lever } 59465294c1fSJeff Layton 595c46203acSChuck Lever if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 596c46203acSChuck Lever trace_nfsd_file_gc_hashed(nf); 597c46203acSChuck Lever return LRU_SKIP; 598c46203acSChuck Lever } 59965294c1fSJeff Layton 60065294c1fSJeff Layton list_lru_isolate_move(lru, &nf->nf_lru, head); 60194660cc1SChuck Lever this_cpu_inc(nfsd_file_evictions); 602c46203acSChuck Lever trace_nfsd_file_gc_disposed(nf); 60365294c1fSJeff Layton return LRU_REMOVED; 60465294c1fSJeff Layton } 60565294c1fSJeff Layton 6060bac5a26SChuck Lever /* 6070bac5a26SChuck Lever * Unhash items on @dispose immediately, then queue them on the 6080bac5a26SChuck Lever * disposal workqueue to finish releasing them in the background. 6090bac5a26SChuck Lever * 6100bac5a26SChuck Lever * cel: Note that between the time list_lru_shrink_walk runs and 6110bac5a26SChuck Lever * now, these items are in the hash table but marked unhashed. 6120bac5a26SChuck Lever * Why release these outside of lru_cb ? There's no lock ordering 6130bac5a26SChuck Lever * problem since lru_cb currently takes no lock. 6140bac5a26SChuck Lever */ 6150bac5a26SChuck Lever static void nfsd_file_gc_dispose_list(struct list_head *dispose) 6160bac5a26SChuck Lever { 6170bac5a26SChuck Lever struct nfsd_file *nf; 6180bac5a26SChuck Lever 619cb7ec76eSChuck Lever list_for_each_entry(nf, dispose, nf_lru) 620cb7ec76eSChuck Lever nfsd_file_hash_remove(nf); 6210bac5a26SChuck Lever nfsd_file_dispose_list_delayed(dispose); 6220bac5a26SChuck Lever } 6230bac5a26SChuck Lever 6249542e6a6STrond Myklebust static void 6259542e6a6STrond Myklebust nfsd_file_gc(void) 6269542e6a6STrond Myklebust { 6273bc6d347SChuck Lever LIST_HEAD(dispose); 62894660cc1SChuck Lever unsigned long ret; 6293bc6d347SChuck Lever 63094660cc1SChuck Lever ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb, 631edead3a5SChuck Lever &dispose, list_lru_count(&nfsd_file_lru)); 63294660cc1SChuck Lever trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru)); 6333bc6d347SChuck Lever nfsd_file_gc_dispose_list(&dispose); 6349542e6a6STrond Myklebust } 6359542e6a6STrond Myklebust 6369542e6a6STrond Myklebust static void 6379542e6a6STrond Myklebust nfsd_file_gc_worker(struct work_struct *work) 6389542e6a6STrond Myklebust { 6399542e6a6STrond Myklebust nfsd_file_gc(); 6409542e6a6STrond Myklebust nfsd_file_schedule_laundrette(); 64165294c1fSJeff Layton } 64265294c1fSJeff Layton 64365294c1fSJeff Layton static unsigned long 64465294c1fSJeff Layton nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc) 64565294c1fSJeff Layton { 64665294c1fSJeff Layton return list_lru_count(&nfsd_file_lru); 64765294c1fSJeff Layton } 64865294c1fSJeff Layton 64965294c1fSJeff Layton static unsigned long 65065294c1fSJeff Layton nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc) 65165294c1fSJeff Layton { 65239f1d1ffSChuck Lever LIST_HEAD(dispose); 65339f1d1ffSChuck Lever unsigned long ret; 65439f1d1ffSChuck Lever 65539f1d1ffSChuck Lever ret = list_lru_shrink_walk(&nfsd_file_lru, sc, 65639f1d1ffSChuck Lever nfsd_file_lru_cb, &dispose); 65794660cc1SChuck Lever trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru)); 65839f1d1ffSChuck Lever nfsd_file_gc_dispose_list(&dispose); 65939f1d1ffSChuck Lever return ret; 66065294c1fSJeff Layton } 66165294c1fSJeff Layton 66265294c1fSJeff Layton static struct shrinker nfsd_file_shrinker = { 66365294c1fSJeff Layton .scan_objects = nfsd_file_lru_scan, 66465294c1fSJeff Layton .count_objects = nfsd_file_lru_count, 66565294c1fSJeff Layton .seeks = 1, 66665294c1fSJeff Layton }; 66765294c1fSJeff Layton 668a8455110SChuck Lever /* 669a8455110SChuck Lever * Find all cache items across all net namespaces that match @inode and 670a8455110SChuck Lever * move them to @dispose. The lookup is atomic wrt nfsd_file_acquire(). 671a8455110SChuck Lever */ 672a8455110SChuck Lever static unsigned int 673a8455110SChuck Lever __nfsd_file_close_inode(struct inode *inode, struct list_head *dispose) 67465294c1fSJeff Layton { 675ce502f81SChuck Lever struct nfsd_file_lookup_key key = { 676ce502f81SChuck Lever .type = NFSD_FILE_KEY_INODE, 677ce502f81SChuck Lever .inode = inode, 678ce502f81SChuck Lever }; 679a8455110SChuck Lever unsigned int count = 0; 68065294c1fSJeff Layton struct nfsd_file *nf; 68165294c1fSJeff Layton 682ce502f81SChuck Lever rcu_read_lock(); 683ce502f81SChuck Lever do { 684ce502f81SChuck Lever nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key, 685ce502f81SChuck Lever nfsd_file_rhash_params); 686ce502f81SChuck Lever if (!nf) 687ce502f81SChuck Lever break; 688ce502f81SChuck Lever nfsd_file_unhash_and_dispose(nf, dispose); 689a8455110SChuck Lever count++; 690ce502f81SChuck Lever } while (1); 691ce502f81SChuck Lever rcu_read_unlock(); 692a8455110SChuck Lever return count; 69365294c1fSJeff Layton } 69465294c1fSJeff Layton 69565294c1fSJeff Layton /** 69665294c1fSJeff Layton * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file 69765294c1fSJeff Layton * @inode: inode of the file to attempt to remove 69865294c1fSJeff Layton * 699a8455110SChuck Lever * Unhash and put, then flush and fput all cache items associated with @inode. 70065294c1fSJeff Layton */ 70165294c1fSJeff Layton void 70265294c1fSJeff Layton nfsd_file_close_inode_sync(struct inode *inode) 70365294c1fSJeff Layton { 70465294c1fSJeff Layton LIST_HEAD(dispose); 705a8455110SChuck Lever unsigned int count; 70665294c1fSJeff Layton 707a8455110SChuck Lever count = __nfsd_file_close_inode(inode, &dispose); 708a8455110SChuck Lever trace_nfsd_file_close_inode_sync(inode, count); 70965294c1fSJeff Layton nfsd_file_dispose_list_sync(&dispose); 71065294c1fSJeff Layton } 71165294c1fSJeff Layton 71265294c1fSJeff Layton /** 71319598141STrond Myklebust * nfsd_file_close_inode - attempt a delayed close of a nfsd_file 71465294c1fSJeff Layton * @inode: inode of the file to attempt to remove 71565294c1fSJeff Layton * 716a8455110SChuck Lever * Unhash and put all cache item associated with @inode. 71765294c1fSJeff Layton */ 71865294c1fSJeff Layton static void 71965294c1fSJeff Layton nfsd_file_close_inode(struct inode *inode) 72065294c1fSJeff Layton { 72165294c1fSJeff Layton LIST_HEAD(dispose); 722a8455110SChuck Lever unsigned int count; 72365294c1fSJeff Layton 724a8455110SChuck Lever count = __nfsd_file_close_inode(inode, &dispose); 725a8455110SChuck Lever trace_nfsd_file_close_inode(inode, count); 7269542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(&dispose); 72765294c1fSJeff Layton } 72865294c1fSJeff Layton 72965294c1fSJeff Layton /** 73065294c1fSJeff Layton * nfsd_file_delayed_close - close unused nfsd_files 73165294c1fSJeff Layton * @work: dummy 73265294c1fSJeff Layton * 73365294c1fSJeff Layton * Walk the LRU list and close any entries that have not been used since 73465294c1fSJeff Layton * the last scan. 73565294c1fSJeff Layton * 73665294c1fSJeff Layton * Note this can deadlock with nfsd_file_cache_purge. 73765294c1fSJeff Layton */ 73865294c1fSJeff Layton static void 73965294c1fSJeff Layton nfsd_file_delayed_close(struct work_struct *work) 74065294c1fSJeff Layton { 74165294c1fSJeff Layton LIST_HEAD(head); 7429542e6a6STrond Myklebust struct nfsd_fcache_disposal *l = container_of(work, 7439542e6a6STrond Myklebust struct nfsd_fcache_disposal, work); 74465294c1fSJeff Layton 7459542e6a6STrond Myklebust nfsd_file_list_remove_disposal(&head, l); 7469542e6a6STrond Myklebust nfsd_file_dispose_list(&head); 74765294c1fSJeff Layton } 74865294c1fSJeff Layton 74965294c1fSJeff Layton static int 75065294c1fSJeff Layton nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg, 75165294c1fSJeff Layton void *data) 75265294c1fSJeff Layton { 75365294c1fSJeff Layton struct file_lock *fl = data; 75465294c1fSJeff Layton 75565294c1fSJeff Layton /* Only close files for F_SETLEASE leases */ 75665294c1fSJeff Layton if (fl->fl_flags & FL_LEASE) 75765294c1fSJeff Layton nfsd_file_close_inode_sync(file_inode(fl->fl_file)); 75865294c1fSJeff Layton return 0; 75965294c1fSJeff Layton } 76065294c1fSJeff Layton 76165294c1fSJeff Layton static struct notifier_block nfsd_file_lease_notifier = { 76265294c1fSJeff Layton .notifier_call = nfsd_file_lease_notifier_call, 76365294c1fSJeff Layton }; 76465294c1fSJeff Layton 76565294c1fSJeff Layton static int 766b9a1b977SAmir Goldstein nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask, 767b9a1b977SAmir Goldstein struct inode *inode, struct inode *dir, 768950cc0d2SAmir Goldstein const struct qstr *name, u32 cookie) 76965294c1fSJeff Layton { 77024dca905SGabriel Krisman Bertazi if (WARN_ON_ONCE(!inode)) 77124dca905SGabriel Krisman Bertazi return 0; 77224dca905SGabriel Krisman Bertazi 77365294c1fSJeff Layton trace_nfsd_file_fsnotify_handle_event(inode, mask); 77465294c1fSJeff Layton 77565294c1fSJeff Layton /* Should be no marks on non-regular files */ 77665294c1fSJeff Layton if (!S_ISREG(inode->i_mode)) { 77765294c1fSJeff Layton WARN_ON_ONCE(1); 77865294c1fSJeff Layton return 0; 77965294c1fSJeff Layton } 78065294c1fSJeff Layton 78165294c1fSJeff Layton /* don't close files if this was not the last link */ 78265294c1fSJeff Layton if (mask & FS_ATTRIB) { 78365294c1fSJeff Layton if (inode->i_nlink) 78465294c1fSJeff Layton return 0; 78565294c1fSJeff Layton } 78665294c1fSJeff Layton 78765294c1fSJeff Layton nfsd_file_close_inode(inode); 78865294c1fSJeff Layton return 0; 78965294c1fSJeff Layton } 79065294c1fSJeff Layton 79165294c1fSJeff Layton 79265294c1fSJeff Layton static const struct fsnotify_ops nfsd_file_fsnotify_ops = { 793b9a1b977SAmir Goldstein .handle_inode_event = nfsd_file_fsnotify_handle_event, 79465294c1fSJeff Layton .free_mark = nfsd_file_mark_free, 79565294c1fSJeff Layton }; 79665294c1fSJeff Layton 79765294c1fSJeff Layton int 79865294c1fSJeff Layton nfsd_file_cache_init(void) 79965294c1fSJeff Layton { 800fc22945eSChuck Lever int ret; 80165294c1fSJeff Layton 802c7b824c3SChuck Lever lockdep_assert_held(&nfsd_mutex); 803c7b824c3SChuck Lever if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) 80465294c1fSJeff Layton return 0; 80565294c1fSJeff Layton 806fc22945eSChuck Lever ret = rhashtable_init(&nfsd_file_rhash_tbl, &nfsd_file_rhash_params); 807fc22945eSChuck Lever if (ret) 808fc22945eSChuck Lever return ret; 809fc22945eSChuck Lever 810fc22945eSChuck Lever ret = -ENOMEM; 8119542e6a6STrond Myklebust nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0); 8129542e6a6STrond Myklebust if (!nfsd_filecache_wq) 8139542e6a6STrond Myklebust goto out; 8149542e6a6STrond Myklebust 81565294c1fSJeff Layton nfsd_file_slab = kmem_cache_create("nfsd_file", 81665294c1fSJeff Layton sizeof(struct nfsd_file), 0, 0, NULL); 81765294c1fSJeff Layton if (!nfsd_file_slab) { 81865294c1fSJeff Layton pr_err("nfsd: unable to create nfsd_file_slab\n"); 81965294c1fSJeff Layton goto out_err; 82065294c1fSJeff Layton } 82165294c1fSJeff Layton 82265294c1fSJeff Layton nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark", 82365294c1fSJeff Layton sizeof(struct nfsd_file_mark), 0, 0, NULL); 82465294c1fSJeff Layton if (!nfsd_file_mark_slab) { 82565294c1fSJeff Layton pr_err("nfsd: unable to create nfsd_file_mark_slab\n"); 82665294c1fSJeff Layton goto out_err; 82765294c1fSJeff Layton } 82865294c1fSJeff Layton 82965294c1fSJeff Layton 83065294c1fSJeff Layton ret = list_lru_init(&nfsd_file_lru); 83165294c1fSJeff Layton if (ret) { 83265294c1fSJeff Layton pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret); 83365294c1fSJeff Layton goto out_err; 83465294c1fSJeff Layton } 83565294c1fSJeff Layton 83665294c1fSJeff Layton ret = register_shrinker(&nfsd_file_shrinker); 83765294c1fSJeff Layton if (ret) { 83865294c1fSJeff Layton pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret); 83965294c1fSJeff Layton goto out_lru; 84065294c1fSJeff Layton } 84165294c1fSJeff Layton 84265294c1fSJeff Layton ret = lease_register_notifier(&nfsd_file_lease_notifier); 84365294c1fSJeff Layton if (ret) { 84465294c1fSJeff Layton pr_err("nfsd: unable to register lease notifier: %d\n", ret); 84565294c1fSJeff Layton goto out_shrinker; 84665294c1fSJeff Layton } 84765294c1fSJeff Layton 848867a448dSAmir Goldstein nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops, 849b8962a9dSAmir Goldstein FSNOTIFY_GROUP_NOFS); 85065294c1fSJeff Layton if (IS_ERR(nfsd_file_fsnotify_group)) { 85165294c1fSJeff Layton pr_err("nfsd: unable to create fsnotify group: %ld\n", 85265294c1fSJeff Layton PTR_ERR(nfsd_file_fsnotify_group)); 853231307dfSHuang Guobin ret = PTR_ERR(nfsd_file_fsnotify_group); 85465294c1fSJeff Layton nfsd_file_fsnotify_group = NULL; 85565294c1fSJeff Layton goto out_notifier; 85665294c1fSJeff Layton } 85765294c1fSJeff Layton 8589542e6a6STrond Myklebust INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker); 85965294c1fSJeff Layton out: 86065294c1fSJeff Layton return ret; 86165294c1fSJeff Layton out_notifier: 86265294c1fSJeff Layton lease_unregister_notifier(&nfsd_file_lease_notifier); 86365294c1fSJeff Layton out_shrinker: 86465294c1fSJeff Layton unregister_shrinker(&nfsd_file_shrinker); 86565294c1fSJeff Layton out_lru: 86665294c1fSJeff Layton list_lru_destroy(&nfsd_file_lru); 86765294c1fSJeff Layton out_err: 86865294c1fSJeff Layton kmem_cache_destroy(nfsd_file_slab); 86965294c1fSJeff Layton nfsd_file_slab = NULL; 87065294c1fSJeff Layton kmem_cache_destroy(nfsd_file_mark_slab); 87165294c1fSJeff Layton nfsd_file_mark_slab = NULL; 8729542e6a6STrond Myklebust destroy_workqueue(nfsd_filecache_wq); 8739542e6a6STrond Myklebust nfsd_filecache_wq = NULL; 874fc22945eSChuck Lever rhashtable_destroy(&nfsd_file_rhash_tbl); 87565294c1fSJeff Layton goto out; 87665294c1fSJeff Layton } 87765294c1fSJeff Layton 87865294c1fSJeff Layton /* 87965294c1fSJeff Layton * Note this can deadlock with nfsd_file_lru_cb. 88065294c1fSJeff Layton */ 881c7b824c3SChuck Lever static void 882c7b824c3SChuck Lever __nfsd_file_cache_purge(struct net *net) 88365294c1fSJeff Layton { 884ce502f81SChuck Lever struct rhashtable_iter iter; 88565294c1fSJeff Layton struct nfsd_file *nf; 88665294c1fSJeff Layton LIST_HEAD(dispose); 88765294c1fSJeff Layton bool del; 88865294c1fSJeff Layton 889ce502f81SChuck Lever rhashtable_walk_enter(&nfsd_file_rhash_tbl, &iter); 890ce502f81SChuck Lever do { 891ce502f81SChuck Lever rhashtable_walk_start(&iter); 8925e113224STrond Myklebust 893ce502f81SChuck Lever nf = rhashtable_walk_next(&iter); 894ce502f81SChuck Lever while (!IS_ERR_OR_NULL(nf)) { 8955e113224STrond Myklebust if (net && nf->nf_net != net) 8965e113224STrond Myklebust continue; 897ce502f81SChuck Lever del = nfsd_file_unhash_and_dispose(nf, &dispose); 89865294c1fSJeff Layton 89965294c1fSJeff Layton /* 90065294c1fSJeff Layton * Deadlock detected! Something marked this entry as 90165294c1fSJeff Layton * unhased, but hasn't removed it from the hash list. 90265294c1fSJeff Layton */ 90365294c1fSJeff Layton WARN_ON_ONCE(!del); 904ce502f81SChuck Lever 905ce502f81SChuck Lever nf = rhashtable_walk_next(&iter); 90665294c1fSJeff Layton } 907ce502f81SChuck Lever 908ce502f81SChuck Lever rhashtable_walk_stop(&iter); 909ce502f81SChuck Lever } while (nf == ERR_PTR(-EAGAIN)); 910ce502f81SChuck Lever rhashtable_walk_exit(&iter); 911ce502f81SChuck Lever 91265294c1fSJeff Layton nfsd_file_dispose_list(&dispose); 91365294c1fSJeff Layton } 91465294c1fSJeff Layton 9159542e6a6STrond Myklebust static struct nfsd_fcache_disposal * 9161463b38eSNeilBrown nfsd_alloc_fcache_disposal(void) 9179542e6a6STrond Myklebust { 9189542e6a6STrond Myklebust struct nfsd_fcache_disposal *l; 9199542e6a6STrond Myklebust 9209542e6a6STrond Myklebust l = kmalloc(sizeof(*l), GFP_KERNEL); 9219542e6a6STrond Myklebust if (!l) 9229542e6a6STrond Myklebust return NULL; 9239542e6a6STrond Myklebust INIT_WORK(&l->work, nfsd_file_delayed_close); 9249542e6a6STrond Myklebust spin_lock_init(&l->lock); 9259542e6a6STrond Myklebust INIT_LIST_HEAD(&l->freeme); 9269542e6a6STrond Myklebust return l; 9279542e6a6STrond Myklebust } 9289542e6a6STrond Myklebust 9299542e6a6STrond Myklebust static void 9309542e6a6STrond Myklebust nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l) 9319542e6a6STrond Myklebust { 9329542e6a6STrond Myklebust cancel_work_sync(&l->work); 9339542e6a6STrond Myklebust nfsd_file_dispose_list(&l->freeme); 9341463b38eSNeilBrown kfree(l); 9359542e6a6STrond Myklebust } 9369542e6a6STrond Myklebust 9379542e6a6STrond Myklebust static void 9389542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(struct net *net) 9399542e6a6STrond Myklebust { 9401463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 9411463b38eSNeilBrown struct nfsd_fcache_disposal *l = nn->fcache_disposal; 9429542e6a6STrond Myklebust 9439542e6a6STrond Myklebust nfsd_free_fcache_disposal(l); 9449542e6a6STrond Myklebust } 9459542e6a6STrond Myklebust 9469542e6a6STrond Myklebust int 9479542e6a6STrond Myklebust nfsd_file_cache_start_net(struct net *net) 9489542e6a6STrond Myklebust { 9491463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 9501463b38eSNeilBrown 9511463b38eSNeilBrown nn->fcache_disposal = nfsd_alloc_fcache_disposal(); 9521463b38eSNeilBrown return nn->fcache_disposal ? 0 : -ENOMEM; 9539542e6a6STrond Myklebust } 9549542e6a6STrond Myklebust 955c7b824c3SChuck Lever /** 956c7b824c3SChuck Lever * nfsd_file_cache_purge - Remove all cache items associated with @net 957c7b824c3SChuck Lever * @net: target net namespace 958c7b824c3SChuck Lever * 959c7b824c3SChuck Lever */ 960c7b824c3SChuck Lever void 961c7b824c3SChuck Lever nfsd_file_cache_purge(struct net *net) 962c7b824c3SChuck Lever { 963c7b824c3SChuck Lever lockdep_assert_held(&nfsd_mutex); 964c7b824c3SChuck Lever if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) 965c7b824c3SChuck Lever __nfsd_file_cache_purge(net); 966c7b824c3SChuck Lever } 967c7b824c3SChuck Lever 9689542e6a6STrond Myklebust void 9699542e6a6STrond Myklebust nfsd_file_cache_shutdown_net(struct net *net) 9709542e6a6STrond Myklebust { 9719542e6a6STrond Myklebust nfsd_file_cache_purge(net); 9729542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(net); 9739542e6a6STrond Myklebust } 9749542e6a6STrond Myklebust 97565294c1fSJeff Layton void 97665294c1fSJeff Layton nfsd_file_cache_shutdown(void) 97765294c1fSJeff Layton { 9788b330f78SChuck Lever int i; 9798b330f78SChuck Lever 980c7b824c3SChuck Lever lockdep_assert_held(&nfsd_mutex); 981c7b824c3SChuck Lever if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0) 982c7b824c3SChuck Lever return; 98365294c1fSJeff Layton 98465294c1fSJeff Layton lease_unregister_notifier(&nfsd_file_lease_notifier); 98565294c1fSJeff Layton unregister_shrinker(&nfsd_file_shrinker); 98665294c1fSJeff Layton /* 98765294c1fSJeff Layton * make sure all callers of nfsd_file_lru_cb are done before 98865294c1fSJeff Layton * calling nfsd_file_cache_purge 98965294c1fSJeff Layton */ 99065294c1fSJeff Layton cancel_delayed_work_sync(&nfsd_filecache_laundrette); 991c7b824c3SChuck Lever __nfsd_file_cache_purge(NULL); 99265294c1fSJeff Layton list_lru_destroy(&nfsd_file_lru); 99365294c1fSJeff Layton rcu_barrier(); 99465294c1fSJeff Layton fsnotify_put_group(nfsd_file_fsnotify_group); 99565294c1fSJeff Layton nfsd_file_fsnotify_group = NULL; 99665294c1fSJeff Layton kmem_cache_destroy(nfsd_file_slab); 99765294c1fSJeff Layton nfsd_file_slab = NULL; 99865294c1fSJeff Layton fsnotify_wait_marks_destroyed(); 99965294c1fSJeff Layton kmem_cache_destroy(nfsd_file_mark_slab); 100065294c1fSJeff Layton nfsd_file_mark_slab = NULL; 10019542e6a6STrond Myklebust destroy_workqueue(nfsd_filecache_wq); 10029542e6a6STrond Myklebust nfsd_filecache_wq = NULL; 1003fc22945eSChuck Lever rhashtable_destroy(&nfsd_file_rhash_tbl); 10048b330f78SChuck Lever 10058b330f78SChuck Lever for_each_possible_cpu(i) { 10068b330f78SChuck Lever per_cpu(nfsd_file_cache_hits, i) = 0; 10078b330f78SChuck Lever per_cpu(nfsd_file_acquisitions, i) = 0; 10088b330f78SChuck Lever per_cpu(nfsd_file_releases, i) = 0; 10098b330f78SChuck Lever per_cpu(nfsd_file_total_age, i) = 0; 10108b330f78SChuck Lever per_cpu(nfsd_file_pages_flushed, i) = 0; 10118b330f78SChuck Lever per_cpu(nfsd_file_evictions, i) = 0; 10128b330f78SChuck Lever } 101365294c1fSJeff Layton } 101465294c1fSJeff Layton 101565294c1fSJeff Layton /** 1016ce502f81SChuck Lever * nfsd_file_is_cached - are there any cached open files for this inode? 1017ce502f81SChuck Lever * @inode: inode to check 101865294c1fSJeff Layton * 1019ce502f81SChuck Lever * The lookup matches inodes in all net namespaces and is atomic wrt 1020ce502f81SChuck Lever * nfsd_file_acquire(). 1021ce502f81SChuck Lever * 1022ce502f81SChuck Lever * Return values: 1023ce502f81SChuck Lever * %true: filecache contains at least one file matching this inode 1024ce502f81SChuck Lever * %false: filecache contains no files matching this inode 102565294c1fSJeff Layton */ 102665294c1fSJeff Layton bool 102765294c1fSJeff Layton nfsd_file_is_cached(struct inode *inode) 102865294c1fSJeff Layton { 1029ce502f81SChuck Lever struct nfsd_file_lookup_key key = { 1030ce502f81SChuck Lever .type = NFSD_FILE_KEY_INODE, 1031ce502f81SChuck Lever .inode = inode, 1032ce502f81SChuck Lever }; 103365294c1fSJeff Layton bool ret = false; 103465294c1fSJeff Layton 1035ce502f81SChuck Lever if (rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key, 1036ce502f81SChuck Lever nfsd_file_rhash_params) != NULL) 103765294c1fSJeff Layton ret = true; 103854f7df70SChuck Lever trace_nfsd_file_is_cached(inode, (int)ret); 103965294c1fSJeff Layton return ret; 104065294c1fSJeff Layton } 104165294c1fSJeff Layton 1042fb70bf12SChuck Lever static __be32 1043*be023006SChuck Lever nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, 1044fb70bf12SChuck Lever unsigned int may_flags, struct nfsd_file **pnf, bool open) 104565294c1fSJeff Layton { 1046ce502f81SChuck Lever struct nfsd_file_lookup_key key = { 1047ce502f81SChuck Lever .type = NFSD_FILE_KEY_FULL, 1048ce502f81SChuck Lever .need = may_flags & NFSD_FILE_MAY_MASK, 1049ce502f81SChuck Lever .net = SVC_NET(rqstp), 1050ce502f81SChuck Lever }; 105165294c1fSJeff Layton struct nfsd_file *nf, *new; 105228c7d86bSTrond Myklebust bool retry = true; 1053ce502f81SChuck Lever __be32 status; 105465294c1fSJeff Layton 105565294c1fSJeff Layton status = fh_verify(rqstp, fhp, S_IFREG, 105665294c1fSJeff Layton may_flags|NFSD_MAY_OWNER_OVERRIDE); 105765294c1fSJeff Layton if (status != nfs_ok) 105865294c1fSJeff Layton return status; 1059ce502f81SChuck Lever key.inode = d_inode(fhp->fh_dentry); 1060ce502f81SChuck Lever key.cred = get_current_cred(); 106165294c1fSJeff Layton 106265294c1fSJeff Layton retry: 1063ce502f81SChuck Lever /* Avoid allocation if the item is already in cache */ 1064ce502f81SChuck Lever nf = rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key, 1065ce502f81SChuck Lever nfsd_file_rhash_params); 1066ce502f81SChuck Lever if (nf) 1067ce502f81SChuck Lever nf = nfsd_file_get(nf); 106865294c1fSJeff Layton if (nf) 106965294c1fSJeff Layton goto wait_for_construction; 107065294c1fSJeff Layton 1071ce502f81SChuck Lever new = nfsd_file_alloc(&key, may_flags); 107265294c1fSJeff Layton if (!new) { 107354f7df70SChuck Lever status = nfserr_jukebox; 107454f7df70SChuck Lever goto out_status; 107565294c1fSJeff Layton } 107665294c1fSJeff Layton 1077ce502f81SChuck Lever nf = rhashtable_lookup_get_insert_key(&nfsd_file_rhash_tbl, 1078ce502f81SChuck Lever &key, &new->nf_rhash, 1079ce502f81SChuck Lever nfsd_file_rhash_params); 1080ce502f81SChuck Lever if (!nf) { 1081ce502f81SChuck Lever nf = new; 108265294c1fSJeff Layton goto open_file; 1083ce502f81SChuck Lever } 1084ce502f81SChuck Lever if (IS_ERR(nf)) 1085ce502f81SChuck Lever goto insert_err; 1086ce502f81SChuck Lever nf = nfsd_file_get(nf); 1087ce502f81SChuck Lever if (nf == NULL) { 1088ce502f81SChuck Lever nf = new; 1089ce502f81SChuck Lever goto open_file; 1090ce502f81SChuck Lever } 109165294c1fSJeff Layton nfsd_file_slab_free(&new->nf_rcu); 109265294c1fSJeff Layton 109365294c1fSJeff Layton wait_for_construction: 109465294c1fSJeff Layton wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE); 109565294c1fSJeff Layton 109665294c1fSJeff Layton /* Did construction of this file fail? */ 109765294c1fSJeff Layton if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 1098ce502f81SChuck Lever trace_nfsd_file_cons_err(rqstp, key.inode, may_flags, nf); 109928c7d86bSTrond Myklebust if (!retry) { 110028c7d86bSTrond Myklebust status = nfserr_jukebox; 110128c7d86bSTrond Myklebust goto out; 110228c7d86bSTrond Myklebust } 110328c7d86bSTrond Myklebust retry = false; 110465294c1fSJeff Layton nfsd_file_put_noref(nf); 110565294c1fSJeff Layton goto retry; 110665294c1fSJeff Layton } 110765294c1fSJeff Layton 11084a0e73e6SChuck Lever nfsd_file_lru_remove(nf); 110965294c1fSJeff Layton this_cpu_inc(nfsd_file_cache_hits); 111065294c1fSJeff Layton 111123ba98deSJeff Layton status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags)); 111265294c1fSJeff Layton out: 111365294c1fSJeff Layton if (status == nfs_ok) { 111429d4bdbbSChuck Lever if (open) 111529d4bdbbSChuck Lever this_cpu_inc(nfsd_file_acquisitions); 111665294c1fSJeff Layton *pnf = nf; 111765294c1fSJeff Layton } else { 111865294c1fSJeff Layton nfsd_file_put(nf); 111965294c1fSJeff Layton nf = NULL; 112065294c1fSJeff Layton } 112165294c1fSJeff Layton 112254f7df70SChuck Lever out_status: 1123ce502f81SChuck Lever put_cred(key.cred); 1124*be023006SChuck Lever if (open) 1125ce502f81SChuck Lever trace_nfsd_file_acquire(rqstp, key.inode, may_flags, nf, status); 112665294c1fSJeff Layton return status; 112754f7df70SChuck Lever 112865294c1fSJeff Layton open_file: 112965294c1fSJeff Layton nf->nf_mark = nfsd_file_mark_find_or_create(nf); 1130fb70bf12SChuck Lever if (nf->nf_mark) { 11310122e882SChuck Lever if (open) { 1132f4d84c52SChuck Lever status = nfsd_open_verified(rqstp, fhp, may_flags, 1133f4d84c52SChuck Lever &nf->nf_file); 11340122e882SChuck Lever trace_nfsd_file_open(nf, status); 11350122e882SChuck Lever } else 1136fb70bf12SChuck Lever status = nfs_ok; 1137fb70bf12SChuck Lever } else 113865294c1fSJeff Layton status = nfserr_jukebox; 113965294c1fSJeff Layton /* 114065294c1fSJeff Layton * If construction failed, or we raced with a call to unlink() 114165294c1fSJeff Layton * then unhash. 114265294c1fSJeff Layton */ 1143ce502f81SChuck Lever if (status != nfs_ok || key.inode->i_nlink == 0) 1144ce502f81SChuck Lever if (nfsd_file_unhash(nf)) 114565294c1fSJeff Layton nfsd_file_put_noref(nf); 114665294c1fSJeff Layton clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags); 114765294c1fSJeff Layton smp_mb__after_atomic(); 114865294c1fSJeff Layton wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING); 114965294c1fSJeff Layton goto out; 1150ce502f81SChuck Lever 1151ce502f81SChuck Lever insert_err: 1152ce502f81SChuck Lever nfsd_file_slab_free(&new->nf_rcu); 1153ce502f81SChuck Lever trace_nfsd_file_insert_err(rqstp, key.inode, may_flags, PTR_ERR(nf)); 1154ce502f81SChuck Lever nf = NULL; 1155ce502f81SChuck Lever status = nfserr_jukebox; 1156ce502f81SChuck Lever goto out_status; 115765294c1fSJeff Layton } 115865294c1fSJeff Layton 1159fb70bf12SChuck Lever /** 1160fb70bf12SChuck Lever * nfsd_file_acquire - Get a struct nfsd_file with an open file 1161fb70bf12SChuck Lever * @rqstp: the RPC transaction being executed 1162fb70bf12SChuck Lever * @fhp: the NFS filehandle of the file to be opened 1163fb70bf12SChuck Lever * @may_flags: NFSD_MAY_ settings for the file 1164fb70bf12SChuck Lever * @pnf: OUT: new or found "struct nfsd_file" object 1165fb70bf12SChuck Lever * 1166fb70bf12SChuck Lever * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in 1167fb70bf12SChuck Lever * network byte order is returned. 1168fb70bf12SChuck Lever */ 1169fb70bf12SChuck Lever __be32 1170fb70bf12SChuck Lever nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, 1171fb70bf12SChuck Lever unsigned int may_flags, struct nfsd_file **pnf) 1172fb70bf12SChuck Lever { 1173*be023006SChuck Lever return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, true); 1174fb70bf12SChuck Lever } 1175fb70bf12SChuck Lever 1176fb70bf12SChuck Lever /** 1177fb70bf12SChuck Lever * nfsd_file_create - Get a struct nfsd_file, do not open 1178fb70bf12SChuck Lever * @rqstp: the RPC transaction being executed 1179fb70bf12SChuck Lever * @fhp: the NFS filehandle of the file just created 1180fb70bf12SChuck Lever * @may_flags: NFSD_MAY_ settings for the file 1181fb70bf12SChuck Lever * @pnf: OUT: new or found "struct nfsd_file" object 1182fb70bf12SChuck Lever * 1183fb70bf12SChuck Lever * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in 1184fb70bf12SChuck Lever * network byte order is returned. 1185fb70bf12SChuck Lever */ 1186fb70bf12SChuck Lever __be32 1187fb70bf12SChuck Lever nfsd_file_create(struct svc_rqst *rqstp, struct svc_fh *fhp, 1188fb70bf12SChuck Lever unsigned int may_flags, struct nfsd_file **pnf) 1189fb70bf12SChuck Lever { 1190*be023006SChuck Lever return nfsd_file_do_acquire(rqstp, fhp, may_flags, pnf, false); 1191fb70bf12SChuck Lever } 1192fb70bf12SChuck Lever 119365294c1fSJeff Layton /* 119465294c1fSJeff Layton * Note that fields may be added, removed or reordered in the future. Programs 119565294c1fSJeff Layton * scraping this file for info should test the labels to ensure they're 119665294c1fSJeff Layton * getting the correct field. 119765294c1fSJeff Layton */ 119865294c1fSJeff Layton static int nfsd_file_cache_stats_show(struct seq_file *m, void *v) 119965294c1fSJeff Layton { 1200df2aff52SChuck Lever unsigned long releases = 0, pages_flushed = 0, evictions = 0; 1201df2aff52SChuck Lever unsigned long hits = 0, acquisitions = 0; 1202ce502f81SChuck Lever unsigned int i, count = 0, buckets = 0; 1203904940e9SChuck Lever unsigned long lru = 0, total_age = 0; 120465294c1fSJeff Layton 1205ce502f81SChuck Lever /* Serialize with server shutdown */ 120665294c1fSJeff Layton mutex_lock(&nfsd_mutex); 1207c7b824c3SChuck Lever if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) { 1208ce502f81SChuck Lever struct bucket_table *tbl; 1209ce502f81SChuck Lever struct rhashtable *ht; 1210ce502f81SChuck Lever 12110fd244c1SChuck Lever lru = list_lru_count(&nfsd_file_lru); 1212ce502f81SChuck Lever 1213ce502f81SChuck Lever rcu_read_lock(); 1214ce502f81SChuck Lever ht = &nfsd_file_rhash_tbl; 1215ce502f81SChuck Lever count = atomic_read(&ht->nelems); 1216ce502f81SChuck Lever tbl = rht_dereference_rcu(ht->tbl, ht); 1217ce502f81SChuck Lever buckets = tbl->size; 1218ce502f81SChuck Lever rcu_read_unlock(); 121965294c1fSJeff Layton } 122065294c1fSJeff Layton mutex_unlock(&nfsd_mutex); 122165294c1fSJeff Layton 122229d4bdbbSChuck Lever for_each_possible_cpu(i) { 122365294c1fSJeff Layton hits += per_cpu(nfsd_file_cache_hits, i); 122429d4bdbbSChuck Lever acquisitions += per_cpu(nfsd_file_acquisitions, i); 1225d6329327SChuck Lever releases += per_cpu(nfsd_file_releases, i); 1226904940e9SChuck Lever total_age += per_cpu(nfsd_file_total_age, i); 122794660cc1SChuck Lever evictions += per_cpu(nfsd_file_evictions, i); 1228df2aff52SChuck Lever pages_flushed += per_cpu(nfsd_file_pages_flushed, i); 122929d4bdbbSChuck Lever } 123065294c1fSJeff Layton 123165294c1fSJeff Layton seq_printf(m, "total entries: %u\n", count); 1232ce502f81SChuck Lever seq_printf(m, "hash buckets: %u\n", buckets); 12330fd244c1SChuck Lever seq_printf(m, "lru entries: %lu\n", lru); 123465294c1fSJeff Layton seq_printf(m, "cache hits: %lu\n", hits); 123529d4bdbbSChuck Lever seq_printf(m, "acquisitions: %lu\n", acquisitions); 1236d6329327SChuck Lever seq_printf(m, "releases: %lu\n", releases); 123794660cc1SChuck Lever seq_printf(m, "evictions: %lu\n", evictions); 1238904940e9SChuck Lever if (releases) 1239904940e9SChuck Lever seq_printf(m, "mean age (ms): %ld\n", total_age / releases); 1240904940e9SChuck Lever else 1241904940e9SChuck Lever seq_printf(m, "mean age (ms): -\n"); 1242df2aff52SChuck Lever seq_printf(m, "pages flushed: %lu\n", pages_flushed); 124365294c1fSJeff Layton return 0; 124465294c1fSJeff Layton } 124565294c1fSJeff Layton 124665294c1fSJeff Layton int nfsd_file_cache_stats_open(struct inode *inode, struct file *file) 124765294c1fSJeff Layton { 124865294c1fSJeff Layton return single_open(file, nfsd_file_cache_stats_show, NULL); 124965294c1fSJeff Layton } 1250