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 } 30765294c1fSJeff Layton return nf; 30865294c1fSJeff Layton } 30965294c1fSJeff Layton 31065294c1fSJeff Layton static bool 31165294c1fSJeff Layton nfsd_file_free(struct nfsd_file *nf) 31265294c1fSJeff Layton { 313904940e9SChuck Lever s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime)); 31465294c1fSJeff Layton bool flush = false; 31565294c1fSJeff Layton 316d6329327SChuck Lever this_cpu_inc(nfsd_file_releases); 317904940e9SChuck Lever this_cpu_add(nfsd_file_total_age, age); 318d6329327SChuck Lever 31965294c1fSJeff Layton trace_nfsd_file_put_final(nf); 32065294c1fSJeff Layton if (nf->nf_mark) 32165294c1fSJeff Layton nfsd_file_mark_put(nf->nf_mark); 32265294c1fSJeff Layton if (nf->nf_file) { 32365294c1fSJeff Layton get_file(nf->nf_file); 32465294c1fSJeff Layton filp_close(nf->nf_file, NULL); 32565294c1fSJeff Layton fput(nf->nf_file); 32665294c1fSJeff Layton flush = true; 32765294c1fSJeff Layton } 328668ed92eSChuck Lever 329668ed92eSChuck Lever /* 330668ed92eSChuck Lever * If this item is still linked via nf_lru, that's a bug. 331668ed92eSChuck Lever * WARN and leak it to preserve system stability. 332668ed92eSChuck Lever */ 333668ed92eSChuck Lever if (WARN_ON_ONCE(!list_empty(&nf->nf_lru))) 334668ed92eSChuck Lever return flush; 335668ed92eSChuck Lever 33665294c1fSJeff Layton call_rcu(&nf->nf_rcu, nfsd_file_slab_free); 33765294c1fSJeff Layton return flush; 33865294c1fSJeff Layton } 33965294c1fSJeff Layton 340055b24a8STrond Myklebust static bool 341055b24a8STrond Myklebust nfsd_file_check_writeback(struct nfsd_file *nf) 342055b24a8STrond Myklebust { 343055b24a8STrond Myklebust struct file *file = nf->nf_file; 344055b24a8STrond Myklebust struct address_space *mapping; 345055b24a8STrond Myklebust 346055b24a8STrond Myklebust if (!file || !(file->f_mode & FMODE_WRITE)) 347055b24a8STrond Myklebust return false; 348055b24a8STrond Myklebust mapping = file->f_mapping; 349055b24a8STrond Myklebust return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) || 350055b24a8STrond Myklebust mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK); 351055b24a8STrond Myklebust } 352055b24a8STrond Myklebust 353055b24a8STrond Myklebust static int 354055b24a8STrond Myklebust nfsd_file_check_write_error(struct nfsd_file *nf) 355055b24a8STrond Myklebust { 356055b24a8STrond Myklebust struct file *file = nf->nf_file; 357055b24a8STrond Myklebust 358055b24a8STrond Myklebust if (!file || !(file->f_mode & FMODE_WRITE)) 359055b24a8STrond Myklebust return 0; 360055b24a8STrond Myklebust return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err)); 361055b24a8STrond Myklebust } 362055b24a8STrond Myklebust 36365294c1fSJeff Layton static void 3646b8a9433STrond Myklebust nfsd_file_flush(struct nfsd_file *nf) 3656b8a9433STrond Myklebust { 366df2aff52SChuck Lever struct file *file = nf->nf_file; 367df2aff52SChuck Lever 368df2aff52SChuck Lever if (!file || !(file->f_mode & FMODE_WRITE)) 369df2aff52SChuck Lever return; 370df2aff52SChuck Lever this_cpu_add(nfsd_file_pages_flushed, file->f_mapping->nrpages); 371df2aff52SChuck Lever if (vfs_fsync(file, 1) != 0) 3726b8a9433STrond Myklebust nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 3736b8a9433STrond Myklebust } 3746b8a9433STrond Myklebust 375c46203acSChuck Lever static void nfsd_file_lru_add(struct nfsd_file *nf) 376c46203acSChuck Lever { 3774a0e73e6SChuck Lever set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags); 378c46203acSChuck Lever if (list_lru_add(&nfsd_file_lru, &nf->nf_lru)) 379c46203acSChuck Lever trace_nfsd_file_lru_add(nf); 380c46203acSChuck Lever } 381c46203acSChuck Lever 382c46203acSChuck Lever static void nfsd_file_lru_remove(struct nfsd_file *nf) 383c46203acSChuck Lever { 384c46203acSChuck Lever if (list_lru_del(&nfsd_file_lru, &nf->nf_lru)) 385c46203acSChuck Lever trace_nfsd_file_lru_del(nf); 386c46203acSChuck Lever } 387c46203acSChuck Lever 3886b8a9433STrond Myklebust static void 389ce502f81SChuck Lever nfsd_file_hash_remove(struct nfsd_file *nf) 39065294c1fSJeff Layton { 39165294c1fSJeff Layton trace_nfsd_file_unhash(nf); 39265294c1fSJeff Layton 393055b24a8STrond Myklebust if (nfsd_file_check_write_error(nf)) 3943988a578SChuck Lever nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 395ce502f81SChuck Lever rhashtable_remove_fast(&nfsd_file_rhash_tbl, &nf->nf_rhash, 396ce502f81SChuck Lever nfsd_file_rhash_params); 397cb7ec76eSChuck Lever } 398cb7ec76eSChuck Lever 39965294c1fSJeff Layton static bool 40065294c1fSJeff Layton nfsd_file_unhash(struct nfsd_file *nf) 40165294c1fSJeff Layton { 40265294c1fSJeff Layton if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 403ce502f81SChuck Lever nfsd_file_hash_remove(nf); 40465294c1fSJeff Layton return true; 40565294c1fSJeff Layton } 40665294c1fSJeff Layton return false; 40765294c1fSJeff Layton } 40865294c1fSJeff Layton 40965294c1fSJeff Layton /* 41065294c1fSJeff Layton * Return true if the file was unhashed. 41165294c1fSJeff Layton */ 41265294c1fSJeff Layton static bool 413ce502f81SChuck Lever nfsd_file_unhash_and_dispose(struct nfsd_file *nf, struct list_head *dispose) 41465294c1fSJeff Layton { 415ce502f81SChuck Lever trace_nfsd_file_unhash_and_dispose(nf); 41665294c1fSJeff Layton if (!nfsd_file_unhash(nf)) 41765294c1fSJeff Layton return false; 41865294c1fSJeff Layton /* keep final reference for nfsd_file_lru_dispose */ 419689827cdSTrond Myklebust if (refcount_dec_not_one(&nf->nf_ref)) 42065294c1fSJeff Layton return true; 42165294c1fSJeff Layton 4224a0e73e6SChuck Lever nfsd_file_lru_remove(nf); 42365294c1fSJeff Layton list_add(&nf->nf_lru, dispose); 42465294c1fSJeff Layton return true; 42565294c1fSJeff Layton } 42665294c1fSJeff Layton 427b6669305STrond Myklebust static void 42865294c1fSJeff Layton nfsd_file_put_noref(struct nfsd_file *nf) 42965294c1fSJeff Layton { 43065294c1fSJeff Layton trace_nfsd_file_put(nf); 43165294c1fSJeff Layton 432689827cdSTrond Myklebust if (refcount_dec_and_test(&nf->nf_ref)) { 43365294c1fSJeff Layton WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags)); 4344a0e73e6SChuck Lever nfsd_file_lru_remove(nf); 43565294c1fSJeff Layton nfsd_file_free(nf); 43665294c1fSJeff Layton } 43765294c1fSJeff Layton } 43865294c1fSJeff Layton 43965294c1fSJeff Layton void 44065294c1fSJeff Layton nfsd_file_put(struct nfsd_file *nf) 44165294c1fSJeff Layton { 44208af54b3SChuck Lever might_sleep(); 44308af54b3SChuck Lever 4444a0e73e6SChuck Lever nfsd_file_lru_add(nf); 44599939792STrond Myklebust if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) { 4466b8a9433STrond Myklebust nfsd_file_flush(nf); 447b6669305STrond Myklebust nfsd_file_put_noref(nf); 448b6c71c66SChuck Lever } else if (nf->nf_file) { 449b6669305STrond Myklebust nfsd_file_put_noref(nf); 4509542e6a6STrond Myklebust nfsd_file_schedule_laundrette(); 451b6c71c66SChuck Lever } else 452b6c71c66SChuck Lever nfsd_file_put_noref(nf); 45365294c1fSJeff Layton } 45465294c1fSJeff Layton 45565294c1fSJeff Layton struct nfsd_file * 45665294c1fSJeff Layton nfsd_file_get(struct nfsd_file *nf) 45765294c1fSJeff Layton { 458689827cdSTrond Myklebust if (likely(refcount_inc_not_zero(&nf->nf_ref))) 45965294c1fSJeff Layton return nf; 46065294c1fSJeff Layton return NULL; 46165294c1fSJeff Layton } 46265294c1fSJeff Layton 46365294c1fSJeff Layton static void 46465294c1fSJeff Layton nfsd_file_dispose_list(struct list_head *dispose) 46565294c1fSJeff Layton { 46665294c1fSJeff Layton struct nfsd_file *nf; 46765294c1fSJeff Layton 46865294c1fSJeff Layton while(!list_empty(dispose)) { 46965294c1fSJeff Layton nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 470668ed92eSChuck Lever list_del_init(&nf->nf_lru); 4716b8a9433STrond Myklebust nfsd_file_flush(nf); 47265294c1fSJeff Layton nfsd_file_put_noref(nf); 47365294c1fSJeff Layton } 47465294c1fSJeff Layton } 47565294c1fSJeff Layton 47665294c1fSJeff Layton static void 47765294c1fSJeff Layton nfsd_file_dispose_list_sync(struct list_head *dispose) 47865294c1fSJeff Layton { 47965294c1fSJeff Layton bool flush = false; 48065294c1fSJeff Layton struct nfsd_file *nf; 48165294c1fSJeff Layton 48265294c1fSJeff Layton while(!list_empty(dispose)) { 48365294c1fSJeff Layton nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 484668ed92eSChuck Lever list_del_init(&nf->nf_lru); 4856b8a9433STrond Myklebust nfsd_file_flush(nf); 486689827cdSTrond Myklebust if (!refcount_dec_and_test(&nf->nf_ref)) 48765294c1fSJeff Layton continue; 48865294c1fSJeff Layton if (nfsd_file_free(nf)) 48965294c1fSJeff Layton flush = true; 49065294c1fSJeff Layton } 49165294c1fSJeff Layton if (flush) 49265294c1fSJeff Layton flush_delayed_fput(); 49365294c1fSJeff Layton } 49465294c1fSJeff Layton 4959542e6a6STrond Myklebust static void 4969542e6a6STrond Myklebust nfsd_file_list_remove_disposal(struct list_head *dst, 4979542e6a6STrond Myklebust struct nfsd_fcache_disposal *l) 4989542e6a6STrond Myklebust { 4999542e6a6STrond Myklebust spin_lock(&l->lock); 5009542e6a6STrond Myklebust list_splice_init(&l->freeme, dst); 5019542e6a6STrond Myklebust spin_unlock(&l->lock); 5029542e6a6STrond Myklebust } 5039542e6a6STrond Myklebust 5049542e6a6STrond Myklebust static void 5059542e6a6STrond Myklebust nfsd_file_list_add_disposal(struct list_head *files, struct net *net) 5069542e6a6STrond Myklebust { 5071463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 5081463b38eSNeilBrown struct nfsd_fcache_disposal *l = nn->fcache_disposal; 5099542e6a6STrond Myklebust 5109542e6a6STrond Myklebust spin_lock(&l->lock); 5119542e6a6STrond Myklebust list_splice_tail_init(files, &l->freeme); 5129542e6a6STrond Myklebust spin_unlock(&l->lock); 5139542e6a6STrond Myklebust queue_work(nfsd_filecache_wq, &l->work); 5149542e6a6STrond Myklebust } 5159542e6a6STrond Myklebust 5169542e6a6STrond Myklebust static void 5179542e6a6STrond Myklebust nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src, 5189542e6a6STrond Myklebust struct net *net) 5199542e6a6STrond Myklebust { 5209542e6a6STrond Myklebust struct nfsd_file *nf, *tmp; 5219542e6a6STrond Myklebust 5229542e6a6STrond Myklebust list_for_each_entry_safe(nf, tmp, src, nf_lru) { 5239542e6a6STrond Myklebust if (nf->nf_net == net) 5249542e6a6STrond Myklebust list_move_tail(&nf->nf_lru, dst); 5259542e6a6STrond Myklebust } 5269542e6a6STrond Myklebust } 5279542e6a6STrond Myklebust 5289542e6a6STrond Myklebust static void 5299542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(struct list_head *dispose) 5309542e6a6STrond Myklebust { 5319542e6a6STrond Myklebust LIST_HEAD(list); 5329542e6a6STrond Myklebust struct nfsd_file *nf; 5339542e6a6STrond Myklebust 5349542e6a6STrond Myklebust while(!list_empty(dispose)) { 5359542e6a6STrond Myklebust nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 5369542e6a6STrond Myklebust nfsd_file_list_add_pernet(&list, dispose, nf->nf_net); 5379542e6a6STrond Myklebust nfsd_file_list_add_disposal(&list, nf->nf_net); 5389542e6a6STrond Myklebust } 5399542e6a6STrond Myklebust } 5409542e6a6STrond Myklebust 5414a0e73e6SChuck Lever /** 5424a0e73e6SChuck Lever * nfsd_file_lru_cb - Examine an entry on the LRU list 5434a0e73e6SChuck Lever * @item: LRU entry to examine 5444a0e73e6SChuck Lever * @lru: controlling LRU 5454a0e73e6SChuck Lever * @lock: LRU list lock (unused) 5464a0e73e6SChuck Lever * @arg: dispose list 5474a0e73e6SChuck Lever * 54865294c1fSJeff Layton * Note this can deadlock with nfsd_file_cache_purge. 5494a0e73e6SChuck Lever * 5504a0e73e6SChuck Lever * Return values: 5514a0e73e6SChuck Lever * %LRU_REMOVED: @item was removed from the LRU 552edead3a5SChuck Lever * %LRU_ROTATE: @item is to be moved to the LRU tail 5534a0e73e6SChuck Lever * %LRU_SKIP: @item cannot be evicted 55465294c1fSJeff Layton */ 55565294c1fSJeff Layton static enum lru_status 55665294c1fSJeff Layton nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru, 55765294c1fSJeff Layton spinlock_t *lock, void *arg) 55865294c1fSJeff Layton __releases(lock) 55965294c1fSJeff Layton __acquires(lock) 56065294c1fSJeff Layton { 56165294c1fSJeff Layton struct list_head *head = arg; 56265294c1fSJeff Layton struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru); 56365294c1fSJeff Layton 56465294c1fSJeff Layton /* 56565294c1fSJeff Layton * Do a lockless refcount check. The hashtable holds one reference, so 56665294c1fSJeff Layton * we look to see if anything else has a reference, or if any have 56765294c1fSJeff Layton * been put since the shrinker last ran. Those don't get unhashed and 56865294c1fSJeff Layton * released. 56965294c1fSJeff Layton * 57065294c1fSJeff Layton * Note that in the put path, we set the flag and then decrement the 57165294c1fSJeff Layton * counter. Here we check the counter and then test and clear the flag. 57265294c1fSJeff Layton * That order is deliberate to ensure that we can do this locklessly. 57365294c1fSJeff Layton */ 574c46203acSChuck Lever if (refcount_read(&nf->nf_ref) > 1) { 5754a0e73e6SChuck Lever list_lru_isolate(lru, &nf->nf_lru); 576c46203acSChuck Lever trace_nfsd_file_gc_in_use(nf); 5774a0e73e6SChuck Lever return LRU_REMOVED; 578c46203acSChuck Lever } 579055b24a8STrond Myklebust 580055b24a8STrond Myklebust /* 581055b24a8STrond Myklebust * Don't throw out files that are still undergoing I/O or 582055b24a8STrond Myklebust * that have uncleared errors pending. 583055b24a8STrond Myklebust */ 584c46203acSChuck Lever if (nfsd_file_check_writeback(nf)) { 585c46203acSChuck Lever trace_nfsd_file_gc_writeback(nf); 586c46203acSChuck Lever return LRU_SKIP; 587c46203acSChuck Lever } 588055b24a8STrond Myklebust 589c46203acSChuck Lever if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) { 590c46203acSChuck Lever trace_nfsd_file_gc_referenced(nf); 591edead3a5SChuck Lever return LRU_ROTATE; 592c46203acSChuck Lever } 59365294c1fSJeff Layton 594c46203acSChuck Lever if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 595c46203acSChuck Lever trace_nfsd_file_gc_hashed(nf); 596c46203acSChuck Lever return LRU_SKIP; 597c46203acSChuck Lever } 59865294c1fSJeff Layton 59965294c1fSJeff Layton list_lru_isolate_move(lru, &nf->nf_lru, head); 60094660cc1SChuck Lever this_cpu_inc(nfsd_file_evictions); 601c46203acSChuck Lever trace_nfsd_file_gc_disposed(nf); 60265294c1fSJeff Layton return LRU_REMOVED; 60365294c1fSJeff Layton } 60465294c1fSJeff Layton 6050bac5a26SChuck Lever /* 6060bac5a26SChuck Lever * Unhash items on @dispose immediately, then queue them on the 6070bac5a26SChuck Lever * disposal workqueue to finish releasing them in the background. 6080bac5a26SChuck Lever * 6090bac5a26SChuck Lever * cel: Note that between the time list_lru_shrink_walk runs and 6100bac5a26SChuck Lever * now, these items are in the hash table but marked unhashed. 6110bac5a26SChuck Lever * Why release these outside of lru_cb ? There's no lock ordering 6120bac5a26SChuck Lever * problem since lru_cb currently takes no lock. 6130bac5a26SChuck Lever */ 6140bac5a26SChuck Lever static void nfsd_file_gc_dispose_list(struct list_head *dispose) 6150bac5a26SChuck Lever { 6160bac5a26SChuck Lever struct nfsd_file *nf; 6170bac5a26SChuck Lever 618cb7ec76eSChuck Lever list_for_each_entry(nf, dispose, nf_lru) 619cb7ec76eSChuck Lever nfsd_file_hash_remove(nf); 6200bac5a26SChuck Lever nfsd_file_dispose_list_delayed(dispose); 6210bac5a26SChuck Lever } 6220bac5a26SChuck Lever 6239542e6a6STrond Myklebust static void 6249542e6a6STrond Myklebust nfsd_file_gc(void) 6259542e6a6STrond Myklebust { 6263bc6d347SChuck Lever LIST_HEAD(dispose); 62794660cc1SChuck Lever unsigned long ret; 6283bc6d347SChuck Lever 62994660cc1SChuck Lever ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb, 630edead3a5SChuck Lever &dispose, list_lru_count(&nfsd_file_lru)); 63194660cc1SChuck Lever trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru)); 6323bc6d347SChuck Lever nfsd_file_gc_dispose_list(&dispose); 6339542e6a6STrond Myklebust } 6349542e6a6STrond Myklebust 6359542e6a6STrond Myklebust static void 6369542e6a6STrond Myklebust nfsd_file_gc_worker(struct work_struct *work) 6379542e6a6STrond Myklebust { 6389542e6a6STrond Myklebust nfsd_file_gc(); 6399542e6a6STrond Myklebust nfsd_file_schedule_laundrette(); 64065294c1fSJeff Layton } 64165294c1fSJeff Layton 64265294c1fSJeff Layton static unsigned long 64365294c1fSJeff Layton nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc) 64465294c1fSJeff Layton { 64565294c1fSJeff Layton return list_lru_count(&nfsd_file_lru); 64665294c1fSJeff Layton } 64765294c1fSJeff Layton 64865294c1fSJeff Layton static unsigned long 64965294c1fSJeff Layton nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc) 65065294c1fSJeff Layton { 65139f1d1ffSChuck Lever LIST_HEAD(dispose); 65239f1d1ffSChuck Lever unsigned long ret; 65339f1d1ffSChuck Lever 65439f1d1ffSChuck Lever ret = list_lru_shrink_walk(&nfsd_file_lru, sc, 65539f1d1ffSChuck Lever nfsd_file_lru_cb, &dispose); 65694660cc1SChuck Lever trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru)); 65739f1d1ffSChuck Lever nfsd_file_gc_dispose_list(&dispose); 65839f1d1ffSChuck Lever return ret; 65965294c1fSJeff Layton } 66065294c1fSJeff Layton 66165294c1fSJeff Layton static struct shrinker nfsd_file_shrinker = { 66265294c1fSJeff Layton .scan_objects = nfsd_file_lru_scan, 66365294c1fSJeff Layton .count_objects = nfsd_file_lru_count, 66465294c1fSJeff Layton .seeks = 1, 66565294c1fSJeff Layton }; 66665294c1fSJeff Layton 667a8455110SChuck Lever /* 668a8455110SChuck Lever * Find all cache items across all net namespaces that match @inode and 669a8455110SChuck Lever * move them to @dispose. The lookup is atomic wrt nfsd_file_acquire(). 670a8455110SChuck Lever */ 671a8455110SChuck Lever static unsigned int 672a8455110SChuck Lever __nfsd_file_close_inode(struct inode *inode, struct list_head *dispose) 67365294c1fSJeff Layton { 674ce502f81SChuck Lever struct nfsd_file_lookup_key key = { 675ce502f81SChuck Lever .type = NFSD_FILE_KEY_INODE, 676ce502f81SChuck Lever .inode = inode, 677ce502f81SChuck Lever }; 678a8455110SChuck Lever unsigned int count = 0; 67965294c1fSJeff Layton struct nfsd_file *nf; 68065294c1fSJeff Layton 681ce502f81SChuck Lever rcu_read_lock(); 682ce502f81SChuck Lever do { 683ce502f81SChuck Lever nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key, 684ce502f81SChuck Lever nfsd_file_rhash_params); 685ce502f81SChuck Lever if (!nf) 686ce502f81SChuck Lever break; 687ce502f81SChuck Lever nfsd_file_unhash_and_dispose(nf, dispose); 688a8455110SChuck Lever count++; 689ce502f81SChuck Lever } while (1); 690ce502f81SChuck Lever rcu_read_unlock(); 691a8455110SChuck Lever return count; 69265294c1fSJeff Layton } 69365294c1fSJeff Layton 69465294c1fSJeff Layton /** 69565294c1fSJeff Layton * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file 69665294c1fSJeff Layton * @inode: inode of the file to attempt to remove 69765294c1fSJeff Layton * 698a8455110SChuck Lever * Unhash and put, then flush and fput all cache items associated with @inode. 69965294c1fSJeff Layton */ 70065294c1fSJeff Layton void 70165294c1fSJeff Layton nfsd_file_close_inode_sync(struct inode *inode) 70265294c1fSJeff Layton { 70365294c1fSJeff Layton LIST_HEAD(dispose); 704a8455110SChuck Lever unsigned int count; 70565294c1fSJeff Layton 706a8455110SChuck Lever count = __nfsd_file_close_inode(inode, &dispose); 707a8455110SChuck Lever trace_nfsd_file_close_inode_sync(inode, count); 70865294c1fSJeff Layton nfsd_file_dispose_list_sync(&dispose); 70965294c1fSJeff Layton } 71065294c1fSJeff Layton 71165294c1fSJeff Layton /** 71219598141STrond Myklebust * nfsd_file_close_inode - attempt a delayed close of a nfsd_file 71365294c1fSJeff Layton * @inode: inode of the file to attempt to remove 71465294c1fSJeff Layton * 715a8455110SChuck Lever * Unhash and put all cache item associated with @inode. 71665294c1fSJeff Layton */ 71765294c1fSJeff Layton static void 71865294c1fSJeff Layton nfsd_file_close_inode(struct inode *inode) 71965294c1fSJeff Layton { 72065294c1fSJeff Layton LIST_HEAD(dispose); 721a8455110SChuck Lever unsigned int count; 72265294c1fSJeff Layton 723a8455110SChuck Lever count = __nfsd_file_close_inode(inode, &dispose); 724a8455110SChuck Lever trace_nfsd_file_close_inode(inode, count); 7259542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(&dispose); 72665294c1fSJeff Layton } 72765294c1fSJeff Layton 72865294c1fSJeff Layton /** 72965294c1fSJeff Layton * nfsd_file_delayed_close - close unused nfsd_files 73065294c1fSJeff Layton * @work: dummy 73165294c1fSJeff Layton * 73265294c1fSJeff Layton * Walk the LRU list and close any entries that have not been used since 73365294c1fSJeff Layton * the last scan. 73465294c1fSJeff Layton * 73565294c1fSJeff Layton * Note this can deadlock with nfsd_file_cache_purge. 73665294c1fSJeff Layton */ 73765294c1fSJeff Layton static void 73865294c1fSJeff Layton nfsd_file_delayed_close(struct work_struct *work) 73965294c1fSJeff Layton { 74065294c1fSJeff Layton LIST_HEAD(head); 7419542e6a6STrond Myklebust struct nfsd_fcache_disposal *l = container_of(work, 7429542e6a6STrond Myklebust struct nfsd_fcache_disposal, work); 74365294c1fSJeff Layton 7449542e6a6STrond Myklebust nfsd_file_list_remove_disposal(&head, l); 7459542e6a6STrond Myklebust nfsd_file_dispose_list(&head); 74665294c1fSJeff Layton } 74765294c1fSJeff Layton 74865294c1fSJeff Layton static int 74965294c1fSJeff Layton nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg, 75065294c1fSJeff Layton void *data) 75165294c1fSJeff Layton { 75265294c1fSJeff Layton struct file_lock *fl = data; 75365294c1fSJeff Layton 75465294c1fSJeff Layton /* Only close files for F_SETLEASE leases */ 75565294c1fSJeff Layton if (fl->fl_flags & FL_LEASE) 75665294c1fSJeff Layton nfsd_file_close_inode_sync(file_inode(fl->fl_file)); 75765294c1fSJeff Layton return 0; 75865294c1fSJeff Layton } 75965294c1fSJeff Layton 76065294c1fSJeff Layton static struct notifier_block nfsd_file_lease_notifier = { 76165294c1fSJeff Layton .notifier_call = nfsd_file_lease_notifier_call, 76265294c1fSJeff Layton }; 76365294c1fSJeff Layton 76465294c1fSJeff Layton static int 765b9a1b977SAmir Goldstein nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask, 766b9a1b977SAmir Goldstein struct inode *inode, struct inode *dir, 767950cc0d2SAmir Goldstein const struct qstr *name, u32 cookie) 76865294c1fSJeff Layton { 76924dca905SGabriel Krisman Bertazi if (WARN_ON_ONCE(!inode)) 77024dca905SGabriel Krisman Bertazi return 0; 77124dca905SGabriel Krisman Bertazi 77265294c1fSJeff Layton trace_nfsd_file_fsnotify_handle_event(inode, mask); 77365294c1fSJeff Layton 77465294c1fSJeff Layton /* Should be no marks on non-regular files */ 77565294c1fSJeff Layton if (!S_ISREG(inode->i_mode)) { 77665294c1fSJeff Layton WARN_ON_ONCE(1); 77765294c1fSJeff Layton return 0; 77865294c1fSJeff Layton } 77965294c1fSJeff Layton 78065294c1fSJeff Layton /* don't close files if this was not the last link */ 78165294c1fSJeff Layton if (mask & FS_ATTRIB) { 78265294c1fSJeff Layton if (inode->i_nlink) 78365294c1fSJeff Layton return 0; 78465294c1fSJeff Layton } 78565294c1fSJeff Layton 78665294c1fSJeff Layton nfsd_file_close_inode(inode); 78765294c1fSJeff Layton return 0; 78865294c1fSJeff Layton } 78965294c1fSJeff Layton 79065294c1fSJeff Layton 79165294c1fSJeff Layton static const struct fsnotify_ops nfsd_file_fsnotify_ops = { 792b9a1b977SAmir Goldstein .handle_inode_event = nfsd_file_fsnotify_handle_event, 79365294c1fSJeff Layton .free_mark = nfsd_file_mark_free, 79465294c1fSJeff Layton }; 79565294c1fSJeff Layton 79665294c1fSJeff Layton int 79765294c1fSJeff Layton nfsd_file_cache_init(void) 79865294c1fSJeff Layton { 799fc22945eSChuck Lever int ret; 80065294c1fSJeff Layton 801c7b824c3SChuck Lever lockdep_assert_held(&nfsd_mutex); 802c7b824c3SChuck Lever if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) 80365294c1fSJeff Layton return 0; 80465294c1fSJeff Layton 805fc22945eSChuck Lever ret = rhashtable_init(&nfsd_file_rhash_tbl, &nfsd_file_rhash_params); 806fc22945eSChuck Lever if (ret) 807fc22945eSChuck Lever return ret; 808fc22945eSChuck Lever 809fc22945eSChuck Lever ret = -ENOMEM; 8109542e6a6STrond Myklebust nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0); 8119542e6a6STrond Myklebust if (!nfsd_filecache_wq) 8129542e6a6STrond Myklebust goto out; 8139542e6a6STrond Myklebust 81465294c1fSJeff Layton nfsd_file_slab = kmem_cache_create("nfsd_file", 81565294c1fSJeff Layton sizeof(struct nfsd_file), 0, 0, NULL); 81665294c1fSJeff Layton if (!nfsd_file_slab) { 81765294c1fSJeff Layton pr_err("nfsd: unable to create nfsd_file_slab\n"); 81865294c1fSJeff Layton goto out_err; 81965294c1fSJeff Layton } 82065294c1fSJeff Layton 82165294c1fSJeff Layton nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark", 82265294c1fSJeff Layton sizeof(struct nfsd_file_mark), 0, 0, NULL); 82365294c1fSJeff Layton if (!nfsd_file_mark_slab) { 82465294c1fSJeff Layton pr_err("nfsd: unable to create nfsd_file_mark_slab\n"); 82565294c1fSJeff Layton goto out_err; 82665294c1fSJeff Layton } 82765294c1fSJeff Layton 82865294c1fSJeff Layton 82965294c1fSJeff Layton ret = list_lru_init(&nfsd_file_lru); 83065294c1fSJeff Layton if (ret) { 83165294c1fSJeff Layton pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret); 83265294c1fSJeff Layton goto out_err; 83365294c1fSJeff Layton } 83465294c1fSJeff Layton 83565294c1fSJeff Layton ret = register_shrinker(&nfsd_file_shrinker); 83665294c1fSJeff Layton if (ret) { 83765294c1fSJeff Layton pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret); 83865294c1fSJeff Layton goto out_lru; 83965294c1fSJeff Layton } 84065294c1fSJeff Layton 84165294c1fSJeff Layton ret = lease_register_notifier(&nfsd_file_lease_notifier); 84265294c1fSJeff Layton if (ret) { 84365294c1fSJeff Layton pr_err("nfsd: unable to register lease notifier: %d\n", ret); 84465294c1fSJeff Layton goto out_shrinker; 84565294c1fSJeff Layton } 84665294c1fSJeff Layton 847867a448dSAmir Goldstein nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops, 848b8962a9dSAmir Goldstein FSNOTIFY_GROUP_NOFS); 84965294c1fSJeff Layton if (IS_ERR(nfsd_file_fsnotify_group)) { 85065294c1fSJeff Layton pr_err("nfsd: unable to create fsnotify group: %ld\n", 85165294c1fSJeff Layton PTR_ERR(nfsd_file_fsnotify_group)); 852231307dfSHuang Guobin ret = PTR_ERR(nfsd_file_fsnotify_group); 85365294c1fSJeff Layton nfsd_file_fsnotify_group = NULL; 85465294c1fSJeff Layton goto out_notifier; 85565294c1fSJeff Layton } 85665294c1fSJeff Layton 8579542e6a6STrond Myklebust INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker); 85865294c1fSJeff Layton out: 85965294c1fSJeff Layton return ret; 86065294c1fSJeff Layton out_notifier: 86165294c1fSJeff Layton lease_unregister_notifier(&nfsd_file_lease_notifier); 86265294c1fSJeff Layton out_shrinker: 86365294c1fSJeff Layton unregister_shrinker(&nfsd_file_shrinker); 86465294c1fSJeff Layton out_lru: 86565294c1fSJeff Layton list_lru_destroy(&nfsd_file_lru); 86665294c1fSJeff Layton out_err: 86765294c1fSJeff Layton kmem_cache_destroy(nfsd_file_slab); 86865294c1fSJeff Layton nfsd_file_slab = NULL; 86965294c1fSJeff Layton kmem_cache_destroy(nfsd_file_mark_slab); 87065294c1fSJeff Layton nfsd_file_mark_slab = NULL; 8719542e6a6STrond Myklebust destroy_workqueue(nfsd_filecache_wq); 8729542e6a6STrond Myklebust nfsd_filecache_wq = NULL; 873fc22945eSChuck Lever rhashtable_destroy(&nfsd_file_rhash_tbl); 87465294c1fSJeff Layton goto out; 87565294c1fSJeff Layton } 87665294c1fSJeff Layton 87765294c1fSJeff Layton /* 87865294c1fSJeff Layton * Note this can deadlock with nfsd_file_lru_cb. 87965294c1fSJeff Layton */ 880c7b824c3SChuck Lever static void 881c7b824c3SChuck Lever __nfsd_file_cache_purge(struct net *net) 88265294c1fSJeff Layton { 883ce502f81SChuck Lever struct rhashtable_iter iter; 88465294c1fSJeff Layton struct nfsd_file *nf; 88565294c1fSJeff Layton LIST_HEAD(dispose); 88665294c1fSJeff Layton bool del; 88765294c1fSJeff Layton 888ce502f81SChuck Lever rhashtable_walk_enter(&nfsd_file_rhash_tbl, &iter); 889ce502f81SChuck Lever do { 890ce502f81SChuck Lever rhashtable_walk_start(&iter); 8915e113224STrond Myklebust 892ce502f81SChuck Lever nf = rhashtable_walk_next(&iter); 893ce502f81SChuck Lever while (!IS_ERR_OR_NULL(nf)) { 8945e113224STrond Myklebust if (net && nf->nf_net != net) 8955e113224STrond Myklebust continue; 896ce502f81SChuck Lever del = nfsd_file_unhash_and_dispose(nf, &dispose); 89765294c1fSJeff Layton 89865294c1fSJeff Layton /* 89965294c1fSJeff Layton * Deadlock detected! Something marked this entry as 90065294c1fSJeff Layton * unhased, but hasn't removed it from the hash list. 90165294c1fSJeff Layton */ 90265294c1fSJeff Layton WARN_ON_ONCE(!del); 903ce502f81SChuck Lever 904ce502f81SChuck Lever nf = rhashtable_walk_next(&iter); 90565294c1fSJeff Layton } 906ce502f81SChuck Lever 907ce502f81SChuck Lever rhashtable_walk_stop(&iter); 908ce502f81SChuck Lever } while (nf == ERR_PTR(-EAGAIN)); 909ce502f81SChuck Lever rhashtable_walk_exit(&iter); 910ce502f81SChuck Lever 91165294c1fSJeff Layton nfsd_file_dispose_list(&dispose); 91265294c1fSJeff Layton } 91365294c1fSJeff Layton 9149542e6a6STrond Myklebust static struct nfsd_fcache_disposal * 9151463b38eSNeilBrown nfsd_alloc_fcache_disposal(void) 9169542e6a6STrond Myklebust { 9179542e6a6STrond Myklebust struct nfsd_fcache_disposal *l; 9189542e6a6STrond Myklebust 9199542e6a6STrond Myklebust l = kmalloc(sizeof(*l), GFP_KERNEL); 9209542e6a6STrond Myklebust if (!l) 9219542e6a6STrond Myklebust return NULL; 9229542e6a6STrond Myklebust INIT_WORK(&l->work, nfsd_file_delayed_close); 9239542e6a6STrond Myklebust spin_lock_init(&l->lock); 9249542e6a6STrond Myklebust INIT_LIST_HEAD(&l->freeme); 9259542e6a6STrond Myklebust return l; 9269542e6a6STrond Myklebust } 9279542e6a6STrond Myklebust 9289542e6a6STrond Myklebust static void 9299542e6a6STrond Myklebust nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l) 9309542e6a6STrond Myklebust { 9319542e6a6STrond Myklebust cancel_work_sync(&l->work); 9329542e6a6STrond Myklebust nfsd_file_dispose_list(&l->freeme); 9331463b38eSNeilBrown kfree(l); 9349542e6a6STrond Myklebust } 9359542e6a6STrond Myklebust 9369542e6a6STrond Myklebust static void 9379542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(struct net *net) 9389542e6a6STrond Myklebust { 9391463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 9401463b38eSNeilBrown struct nfsd_fcache_disposal *l = nn->fcache_disposal; 9419542e6a6STrond Myklebust 9429542e6a6STrond Myklebust nfsd_free_fcache_disposal(l); 9439542e6a6STrond Myklebust } 9449542e6a6STrond Myklebust 9459542e6a6STrond Myklebust int 9469542e6a6STrond Myklebust nfsd_file_cache_start_net(struct net *net) 9479542e6a6STrond Myklebust { 9481463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 9491463b38eSNeilBrown 9501463b38eSNeilBrown nn->fcache_disposal = nfsd_alloc_fcache_disposal(); 9511463b38eSNeilBrown return nn->fcache_disposal ? 0 : -ENOMEM; 9529542e6a6STrond Myklebust } 9539542e6a6STrond Myklebust 954c7b824c3SChuck Lever /** 955c7b824c3SChuck Lever * nfsd_file_cache_purge - Remove all cache items associated with @net 956c7b824c3SChuck Lever * @net: target net namespace 957c7b824c3SChuck Lever * 958c7b824c3SChuck Lever */ 959c7b824c3SChuck Lever void 960c7b824c3SChuck Lever nfsd_file_cache_purge(struct net *net) 961c7b824c3SChuck Lever { 962c7b824c3SChuck Lever lockdep_assert_held(&nfsd_mutex); 963c7b824c3SChuck Lever if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) 964c7b824c3SChuck Lever __nfsd_file_cache_purge(net); 965c7b824c3SChuck Lever } 966c7b824c3SChuck Lever 9679542e6a6STrond Myklebust void 9689542e6a6STrond Myklebust nfsd_file_cache_shutdown_net(struct net *net) 9699542e6a6STrond Myklebust { 9709542e6a6STrond Myklebust nfsd_file_cache_purge(net); 9719542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(net); 9729542e6a6STrond Myklebust } 9739542e6a6STrond Myklebust 97465294c1fSJeff Layton void 97565294c1fSJeff Layton nfsd_file_cache_shutdown(void) 97665294c1fSJeff Layton { 9778b330f78SChuck Lever int i; 9788b330f78SChuck Lever 979c7b824c3SChuck Lever lockdep_assert_held(&nfsd_mutex); 980c7b824c3SChuck Lever if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0) 981c7b824c3SChuck Lever return; 98265294c1fSJeff Layton 98365294c1fSJeff Layton lease_unregister_notifier(&nfsd_file_lease_notifier); 98465294c1fSJeff Layton unregister_shrinker(&nfsd_file_shrinker); 98565294c1fSJeff Layton /* 98665294c1fSJeff Layton * make sure all callers of nfsd_file_lru_cb are done before 98765294c1fSJeff Layton * calling nfsd_file_cache_purge 98865294c1fSJeff Layton */ 98965294c1fSJeff Layton cancel_delayed_work_sync(&nfsd_filecache_laundrette); 990c7b824c3SChuck Lever __nfsd_file_cache_purge(NULL); 99165294c1fSJeff Layton list_lru_destroy(&nfsd_file_lru); 99265294c1fSJeff Layton rcu_barrier(); 99365294c1fSJeff Layton fsnotify_put_group(nfsd_file_fsnotify_group); 99465294c1fSJeff Layton nfsd_file_fsnotify_group = NULL; 99565294c1fSJeff Layton kmem_cache_destroy(nfsd_file_slab); 99665294c1fSJeff Layton nfsd_file_slab = NULL; 99765294c1fSJeff Layton fsnotify_wait_marks_destroyed(); 99865294c1fSJeff Layton kmem_cache_destroy(nfsd_file_mark_slab); 99965294c1fSJeff Layton nfsd_file_mark_slab = NULL; 10009542e6a6STrond Myklebust destroy_workqueue(nfsd_filecache_wq); 10019542e6a6STrond Myklebust nfsd_filecache_wq = NULL; 1002fc22945eSChuck Lever rhashtable_destroy(&nfsd_file_rhash_tbl); 10038b330f78SChuck Lever 10048b330f78SChuck Lever for_each_possible_cpu(i) { 10058b330f78SChuck Lever per_cpu(nfsd_file_cache_hits, i) = 0; 10068b330f78SChuck Lever per_cpu(nfsd_file_acquisitions, i) = 0; 10078b330f78SChuck Lever per_cpu(nfsd_file_releases, i) = 0; 10088b330f78SChuck Lever per_cpu(nfsd_file_total_age, i) = 0; 10098b330f78SChuck Lever per_cpu(nfsd_file_pages_flushed, i) = 0; 10108b330f78SChuck Lever per_cpu(nfsd_file_evictions, i) = 0; 10118b330f78SChuck Lever } 101265294c1fSJeff Layton } 101365294c1fSJeff Layton 101465294c1fSJeff Layton /** 1015ce502f81SChuck Lever * nfsd_file_is_cached - are there any cached open files for this inode? 1016ce502f81SChuck Lever * @inode: inode to check 101765294c1fSJeff Layton * 1018ce502f81SChuck Lever * The lookup matches inodes in all net namespaces and is atomic wrt 1019ce502f81SChuck Lever * nfsd_file_acquire(). 1020ce502f81SChuck Lever * 1021ce502f81SChuck Lever * Return values: 1022ce502f81SChuck Lever * %true: filecache contains at least one file matching this inode 1023ce502f81SChuck Lever * %false: filecache contains no files matching this inode 102465294c1fSJeff Layton */ 102565294c1fSJeff Layton bool 102665294c1fSJeff Layton nfsd_file_is_cached(struct inode *inode) 102765294c1fSJeff Layton { 1028ce502f81SChuck Lever struct nfsd_file_lookup_key key = { 1029ce502f81SChuck Lever .type = NFSD_FILE_KEY_INODE, 1030ce502f81SChuck Lever .inode = inode, 1031ce502f81SChuck Lever }; 103265294c1fSJeff Layton bool ret = false; 103365294c1fSJeff Layton 1034ce502f81SChuck Lever if (rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key, 1035ce502f81SChuck Lever nfsd_file_rhash_params) != NULL) 103665294c1fSJeff Layton ret = true; 103754f7df70SChuck Lever trace_nfsd_file_is_cached(inode, (int)ret); 103865294c1fSJeff Layton return ret; 103965294c1fSJeff Layton } 104065294c1fSJeff Layton 1041fb70bf12SChuck Lever static __be32 1042be023006SChuck Lever nfsd_file_do_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, 1043fb70bf12SChuck Lever unsigned int may_flags, struct nfsd_file **pnf, bool open) 104465294c1fSJeff Layton { 1045ce502f81SChuck Lever struct nfsd_file_lookup_key key = { 1046ce502f81SChuck Lever .type = NFSD_FILE_KEY_FULL, 1047ce502f81SChuck Lever .need = may_flags & NFSD_FILE_MAY_MASK, 1048ce502f81SChuck Lever .net = SVC_NET(rqstp), 1049ce502f81SChuck Lever }; 105065294c1fSJeff Layton struct nfsd_file *nf, *new; 105128c7d86bSTrond Myklebust bool retry = true; 1052ce502f81SChuck Lever __be32 status; 105365294c1fSJeff Layton 105465294c1fSJeff Layton status = fh_verify(rqstp, fhp, S_IFREG, 105565294c1fSJeff Layton may_flags|NFSD_MAY_OWNER_OVERRIDE); 105665294c1fSJeff Layton if (status != nfs_ok) 105765294c1fSJeff Layton return status; 1058ce502f81SChuck Lever key.inode = d_inode(fhp->fh_dentry); 1059ce502f81SChuck Lever key.cred = get_current_cred(); 106065294c1fSJeff Layton 106165294c1fSJeff Layton retry: 1062ce502f81SChuck Lever /* Avoid allocation if the item is already in cache */ 1063ce502f81SChuck Lever nf = rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key, 1064ce502f81SChuck Lever nfsd_file_rhash_params); 1065ce502f81SChuck Lever if (nf) 1066ce502f81SChuck Lever nf = nfsd_file_get(nf); 106765294c1fSJeff Layton if (nf) 106865294c1fSJeff Layton goto wait_for_construction; 106965294c1fSJeff Layton 1070ce502f81SChuck Lever new = nfsd_file_alloc(&key, may_flags); 107165294c1fSJeff Layton if (!new) { 107254f7df70SChuck Lever status = nfserr_jukebox; 107354f7df70SChuck Lever goto out_status; 107465294c1fSJeff Layton } 107565294c1fSJeff Layton 1076ce502f81SChuck Lever nf = rhashtable_lookup_get_insert_key(&nfsd_file_rhash_tbl, 1077ce502f81SChuck Lever &key, &new->nf_rhash, 1078ce502f81SChuck Lever nfsd_file_rhash_params); 1079ce502f81SChuck Lever if (!nf) { 1080ce502f81SChuck Lever nf = new; 108165294c1fSJeff Layton goto open_file; 1082ce502f81SChuck Lever } 1083ce502f81SChuck Lever if (IS_ERR(nf)) 1084ce502f81SChuck Lever goto insert_err; 1085ce502f81SChuck Lever nf = nfsd_file_get(nf); 1086ce502f81SChuck Lever if (nf == NULL) { 1087ce502f81SChuck Lever nf = new; 1088ce502f81SChuck Lever goto open_file; 1089ce502f81SChuck Lever } 109065294c1fSJeff Layton nfsd_file_slab_free(&new->nf_rcu); 109165294c1fSJeff Layton 109265294c1fSJeff Layton wait_for_construction: 109365294c1fSJeff Layton wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE); 109465294c1fSJeff Layton 109565294c1fSJeff Layton /* Did construction of this file fail? */ 109665294c1fSJeff Layton if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 1097ce502f81SChuck Lever trace_nfsd_file_cons_err(rqstp, key.inode, may_flags, nf); 109828c7d86bSTrond Myklebust if (!retry) { 109928c7d86bSTrond Myklebust status = nfserr_jukebox; 110028c7d86bSTrond Myklebust goto out; 110128c7d86bSTrond Myklebust } 110228c7d86bSTrond Myklebust retry = false; 110365294c1fSJeff Layton nfsd_file_put_noref(nf); 110465294c1fSJeff Layton goto retry; 110565294c1fSJeff Layton } 110665294c1fSJeff Layton 11074a0e73e6SChuck Lever nfsd_file_lru_remove(nf); 110865294c1fSJeff Layton this_cpu_inc(nfsd_file_cache_hits); 110965294c1fSJeff Layton 111023ba98deSJeff Layton status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags)); 111165294c1fSJeff Layton out: 111265294c1fSJeff Layton if (status == nfs_ok) { 111329d4bdbbSChuck Lever if (open) 111429d4bdbbSChuck Lever this_cpu_inc(nfsd_file_acquisitions); 111565294c1fSJeff Layton *pnf = nf; 111665294c1fSJeff Layton } else { 111765294c1fSJeff Layton nfsd_file_put(nf); 111865294c1fSJeff Layton nf = NULL; 111965294c1fSJeff Layton } 112065294c1fSJeff Layton 112154f7df70SChuck Lever out_status: 1122ce502f81SChuck Lever put_cred(key.cred); 1123be023006SChuck Lever if (open) 1124ce502f81SChuck Lever trace_nfsd_file_acquire(rqstp, key.inode, may_flags, nf, status); 112565294c1fSJeff Layton return status; 112654f7df70SChuck Lever 112765294c1fSJeff Layton open_file: 1128*b40a2839SChuck Lever trace_nfsd_file_alloc(nf); 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 { 1173be023006SChuck 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 { 1190be023006SChuck 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