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 NFSDDBG_FACILITY NFSDDBG_FH 2665294c1fSJeff Layton 2765294c1fSJeff Layton /* FIXME: dynamically size this for the machine somehow? */ 2865294c1fSJeff Layton #define NFSD_FILE_HASH_BITS 12 2965294c1fSJeff Layton #define NFSD_FILE_HASH_SIZE (1 << NFSD_FILE_HASH_BITS) 3065294c1fSJeff Layton #define NFSD_LAUNDRETTE_DELAY (2 * HZ) 3165294c1fSJeff Layton 32c7b824c3SChuck Lever #define NFSD_FILE_CACHE_UP (0) 3365294c1fSJeff Layton 3465294c1fSJeff Layton /* We only care about NFSD_MAY_READ/WRITE for this cache */ 3565294c1fSJeff Layton #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE) 3665294c1fSJeff Layton 3765294c1fSJeff Layton struct nfsd_fcache_bucket { 3865294c1fSJeff Layton struct hlist_head nfb_head; 3965294c1fSJeff Layton spinlock_t nfb_lock; 4065294c1fSJeff Layton unsigned int nfb_count; 4165294c1fSJeff Layton unsigned int nfb_maxcount; 4265294c1fSJeff Layton }; 4365294c1fSJeff Layton 4465294c1fSJeff Layton static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits); 4529d4bdbbSChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions); 46d6329327SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_releases); 47904940e9SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age); 48df2aff52SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_pages_flushed); 4994660cc1SChuck Lever static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions); 5065294c1fSJeff Layton 519542e6a6STrond Myklebust struct nfsd_fcache_disposal { 529542e6a6STrond Myklebust struct work_struct work; 539542e6a6STrond Myklebust spinlock_t lock; 549542e6a6STrond Myklebust struct list_head freeme; 559542e6a6STrond Myklebust }; 569542e6a6STrond Myklebust 5750d0def9SChen Zhou static struct workqueue_struct *nfsd_filecache_wq __read_mostly; 589542e6a6STrond Myklebust 5965294c1fSJeff Layton static struct kmem_cache *nfsd_file_slab; 6065294c1fSJeff Layton static struct kmem_cache *nfsd_file_mark_slab; 6165294c1fSJeff Layton static struct nfsd_fcache_bucket *nfsd_file_hashtbl; 6265294c1fSJeff Layton static struct list_lru nfsd_file_lru; 63c7b824c3SChuck Lever static unsigned long nfsd_file_flags; 6465294c1fSJeff Layton static struct fsnotify_group *nfsd_file_fsnotify_group; 6565294c1fSJeff Layton static struct delayed_work nfsd_filecache_laundrette; 66fc22945eSChuck Lever static struct rhashtable nfsd_file_rhash_tbl 67fc22945eSChuck Lever ____cacheline_aligned_in_smp; 68fc22945eSChuck Lever 69fc22945eSChuck Lever enum nfsd_file_lookup_type { 70fc22945eSChuck Lever NFSD_FILE_KEY_INODE, 71fc22945eSChuck Lever NFSD_FILE_KEY_FULL, 72fc22945eSChuck Lever }; 73fc22945eSChuck Lever 74fc22945eSChuck Lever struct nfsd_file_lookup_key { 75fc22945eSChuck Lever struct inode *inode; 76fc22945eSChuck Lever struct net *net; 77fc22945eSChuck Lever const struct cred *cred; 78fc22945eSChuck Lever unsigned char need; 79fc22945eSChuck Lever enum nfsd_file_lookup_type type; 80fc22945eSChuck Lever }; 81fc22945eSChuck Lever 82fc22945eSChuck Lever /* 83fc22945eSChuck Lever * The returned hash value is based solely on the address of an in-code 84fc22945eSChuck Lever * inode, a pointer to a slab-allocated object. The entropy in such a 85fc22945eSChuck Lever * pointer is concentrated in its middle bits. 86fc22945eSChuck Lever */ 87fc22945eSChuck Lever static u32 nfsd_file_inode_hash(const struct inode *inode, u32 seed) 88fc22945eSChuck Lever { 89fc22945eSChuck Lever unsigned long ptr = (unsigned long)inode; 90fc22945eSChuck Lever u32 k; 91fc22945eSChuck Lever 92fc22945eSChuck Lever k = ptr >> L1_CACHE_SHIFT; 93fc22945eSChuck Lever k &= 0x00ffffff; 94fc22945eSChuck Lever return jhash2(&k, 1, seed); 95fc22945eSChuck Lever } 96fc22945eSChuck Lever 97fc22945eSChuck Lever /** 98fc22945eSChuck Lever * nfsd_file_key_hashfn - Compute the hash value of a lookup key 99fc22945eSChuck Lever * @data: key on which to compute the hash value 100fc22945eSChuck Lever * @len: rhash table's key_len parameter (unused) 101fc22945eSChuck Lever * @seed: rhash table's random seed of the day 102fc22945eSChuck Lever * 103fc22945eSChuck Lever * Return value: 104fc22945eSChuck Lever * Computed 32-bit hash value 105fc22945eSChuck Lever */ 106fc22945eSChuck Lever static u32 nfsd_file_key_hashfn(const void *data, u32 len, u32 seed) 107fc22945eSChuck Lever { 108fc22945eSChuck Lever const struct nfsd_file_lookup_key *key = data; 109fc22945eSChuck Lever 110fc22945eSChuck Lever return nfsd_file_inode_hash(key->inode, seed); 111fc22945eSChuck Lever } 112fc22945eSChuck Lever 113fc22945eSChuck Lever /** 114fc22945eSChuck Lever * nfsd_file_obj_hashfn - Compute the hash value of an nfsd_file 115fc22945eSChuck Lever * @data: object on which to compute the hash value 116fc22945eSChuck Lever * @len: rhash table's key_len parameter (unused) 117fc22945eSChuck Lever * @seed: rhash table's random seed of the day 118fc22945eSChuck Lever * 119fc22945eSChuck Lever * Return value: 120fc22945eSChuck Lever * Computed 32-bit hash value 121fc22945eSChuck Lever */ 122fc22945eSChuck Lever static u32 nfsd_file_obj_hashfn(const void *data, u32 len, u32 seed) 123fc22945eSChuck Lever { 124fc22945eSChuck Lever const struct nfsd_file *nf = data; 125fc22945eSChuck Lever 126fc22945eSChuck Lever return nfsd_file_inode_hash(nf->nf_inode, seed); 127fc22945eSChuck Lever } 128fc22945eSChuck Lever 129fc22945eSChuck Lever static bool 130fc22945eSChuck Lever nfsd_match_cred(const struct cred *c1, const struct cred *c2) 131fc22945eSChuck Lever { 132fc22945eSChuck Lever int i; 133fc22945eSChuck Lever 134fc22945eSChuck Lever if (!uid_eq(c1->fsuid, c2->fsuid)) 135fc22945eSChuck Lever return false; 136fc22945eSChuck Lever if (!gid_eq(c1->fsgid, c2->fsgid)) 137fc22945eSChuck Lever return false; 138fc22945eSChuck Lever if (c1->group_info == NULL || c2->group_info == NULL) 139fc22945eSChuck Lever return c1->group_info == c2->group_info; 140fc22945eSChuck Lever if (c1->group_info->ngroups != c2->group_info->ngroups) 141fc22945eSChuck Lever return false; 142fc22945eSChuck Lever for (i = 0; i < c1->group_info->ngroups; i++) { 143fc22945eSChuck Lever if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i])) 144fc22945eSChuck Lever return false; 145fc22945eSChuck Lever } 146fc22945eSChuck Lever return true; 147fc22945eSChuck Lever } 148fc22945eSChuck Lever 149fc22945eSChuck Lever /** 150fc22945eSChuck Lever * nfsd_file_obj_cmpfn - Match a cache item against search criteria 151fc22945eSChuck Lever * @arg: search criteria 152fc22945eSChuck Lever * @ptr: cache item to check 153fc22945eSChuck Lever * 154fc22945eSChuck Lever * Return values: 155fc22945eSChuck Lever * %0 - Item matches search criteria 156fc22945eSChuck Lever * %1 - Item does not match search criteria 157fc22945eSChuck Lever */ 158fc22945eSChuck Lever static int nfsd_file_obj_cmpfn(struct rhashtable_compare_arg *arg, 159fc22945eSChuck Lever const void *ptr) 160fc22945eSChuck Lever { 161fc22945eSChuck Lever const struct nfsd_file_lookup_key *key = arg->key; 162fc22945eSChuck Lever const struct nfsd_file *nf = ptr; 163fc22945eSChuck Lever 164fc22945eSChuck Lever switch (key->type) { 165fc22945eSChuck Lever case NFSD_FILE_KEY_INODE: 166fc22945eSChuck Lever if (nf->nf_inode != key->inode) 167fc22945eSChuck Lever return 1; 168fc22945eSChuck Lever break; 169fc22945eSChuck Lever case NFSD_FILE_KEY_FULL: 170fc22945eSChuck Lever if (nf->nf_inode != key->inode) 171fc22945eSChuck Lever return 1; 172fc22945eSChuck Lever if (nf->nf_may != key->need) 173fc22945eSChuck Lever return 1; 174fc22945eSChuck Lever if (nf->nf_net != key->net) 175fc22945eSChuck Lever return 1; 176fc22945eSChuck Lever if (!nfsd_match_cred(nf->nf_cred, key->cred)) 177fc22945eSChuck Lever return 1; 178fc22945eSChuck Lever if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) 179fc22945eSChuck Lever return 1; 180fc22945eSChuck Lever break; 181fc22945eSChuck Lever } 182fc22945eSChuck Lever return 0; 183fc22945eSChuck Lever } 184fc22945eSChuck Lever 185fc22945eSChuck Lever static const struct rhashtable_params nfsd_file_rhash_params = { 186fc22945eSChuck Lever .key_len = sizeof_field(struct nfsd_file, nf_inode), 187fc22945eSChuck Lever .key_offset = offsetof(struct nfsd_file, nf_inode), 188fc22945eSChuck Lever .head_offset = offsetof(struct nfsd_file, nf_rhash), 189fc22945eSChuck Lever .hashfn = nfsd_file_key_hashfn, 190fc22945eSChuck Lever .obj_hashfn = nfsd_file_obj_hashfn, 191fc22945eSChuck Lever .obj_cmpfn = nfsd_file_obj_cmpfn, 192fc22945eSChuck Lever /* Reduce resizing churn on light workloads */ 193fc22945eSChuck Lever .min_size = 512, /* buckets */ 194fc22945eSChuck Lever .automatic_shrinking = true, 195fc22945eSChuck Lever }; 19665294c1fSJeff Layton 19765294c1fSJeff Layton static void 1989542e6a6STrond Myklebust nfsd_file_schedule_laundrette(void) 19965294c1fSJeff Layton { 200*ce502f81SChuck Lever if ((atomic_read(&nfsd_file_rhash_tbl.nelems) == 0) || 201c7b824c3SChuck Lever test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0) 20265294c1fSJeff Layton return; 20365294c1fSJeff Layton 2049542e6a6STrond Myklebust queue_delayed_work(system_wq, &nfsd_filecache_laundrette, 2059542e6a6STrond Myklebust NFSD_LAUNDRETTE_DELAY); 20665294c1fSJeff Layton } 20765294c1fSJeff Layton 20865294c1fSJeff Layton static void 20965294c1fSJeff Layton nfsd_file_slab_free(struct rcu_head *rcu) 21065294c1fSJeff Layton { 21165294c1fSJeff Layton struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu); 21265294c1fSJeff Layton 21365294c1fSJeff Layton put_cred(nf->nf_cred); 21465294c1fSJeff Layton kmem_cache_free(nfsd_file_slab, nf); 21565294c1fSJeff Layton } 21665294c1fSJeff Layton 21765294c1fSJeff Layton static void 21865294c1fSJeff Layton nfsd_file_mark_free(struct fsnotify_mark *mark) 21965294c1fSJeff Layton { 22065294c1fSJeff Layton struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark, 22165294c1fSJeff Layton nfm_mark); 22265294c1fSJeff Layton 22365294c1fSJeff Layton kmem_cache_free(nfsd_file_mark_slab, nfm); 22465294c1fSJeff Layton } 22565294c1fSJeff Layton 22665294c1fSJeff Layton static struct nfsd_file_mark * 22765294c1fSJeff Layton nfsd_file_mark_get(struct nfsd_file_mark *nfm) 22865294c1fSJeff Layton { 229689827cdSTrond Myklebust if (!refcount_inc_not_zero(&nfm->nfm_ref)) 23065294c1fSJeff Layton return NULL; 23165294c1fSJeff Layton return nfm; 23265294c1fSJeff Layton } 23365294c1fSJeff Layton 23465294c1fSJeff Layton static void 23565294c1fSJeff Layton nfsd_file_mark_put(struct nfsd_file_mark *nfm) 23665294c1fSJeff Layton { 237689827cdSTrond Myklebust if (refcount_dec_and_test(&nfm->nfm_ref)) { 23865294c1fSJeff Layton fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group); 23965294c1fSJeff Layton fsnotify_put_mark(&nfm->nfm_mark); 24065294c1fSJeff Layton } 24165294c1fSJeff Layton } 24265294c1fSJeff Layton 24365294c1fSJeff Layton static struct nfsd_file_mark * 24465294c1fSJeff Layton nfsd_file_mark_find_or_create(struct nfsd_file *nf) 24565294c1fSJeff Layton { 24665294c1fSJeff Layton int err; 24765294c1fSJeff Layton struct fsnotify_mark *mark; 24865294c1fSJeff Layton struct nfsd_file_mark *nfm = NULL, *new; 24965294c1fSJeff Layton struct inode *inode = nf->nf_inode; 25065294c1fSJeff Layton 25165294c1fSJeff Layton do { 252b8962a9dSAmir Goldstein fsnotify_group_lock(nfsd_file_fsnotify_group); 25365294c1fSJeff Layton mark = fsnotify_find_mark(&inode->i_fsnotify_marks, 25465294c1fSJeff Layton nfsd_file_fsnotify_group); 25565294c1fSJeff Layton if (mark) { 25665294c1fSJeff Layton nfm = nfsd_file_mark_get(container_of(mark, 25765294c1fSJeff Layton struct nfsd_file_mark, 25865294c1fSJeff Layton nfm_mark)); 259b8962a9dSAmir Goldstein fsnotify_group_unlock(nfsd_file_fsnotify_group); 26090d2f1daSTrond Myklebust if (nfm) { 26165294c1fSJeff Layton fsnotify_put_mark(mark); 26265294c1fSJeff Layton break; 26390d2f1daSTrond Myklebust } 26490d2f1daSTrond Myklebust /* Avoid soft lockup race with nfsd_file_mark_put() */ 26590d2f1daSTrond Myklebust fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group); 26690d2f1daSTrond Myklebust fsnotify_put_mark(mark); 267b8962a9dSAmir Goldstein } else { 268b8962a9dSAmir Goldstein fsnotify_group_unlock(nfsd_file_fsnotify_group); 269b8962a9dSAmir Goldstein } 27065294c1fSJeff Layton 27165294c1fSJeff Layton /* allocate a new nfm */ 27265294c1fSJeff Layton new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL); 27365294c1fSJeff Layton if (!new) 27465294c1fSJeff Layton return NULL; 27565294c1fSJeff Layton fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group); 27665294c1fSJeff Layton new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF; 277689827cdSTrond Myklebust refcount_set(&new->nfm_ref, 1); 27865294c1fSJeff Layton 27965294c1fSJeff Layton err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0); 28065294c1fSJeff Layton 28165294c1fSJeff Layton /* 28265294c1fSJeff Layton * If the add was successful, then return the object. 28365294c1fSJeff Layton * Otherwise, we need to put the reference we hold on the 28465294c1fSJeff Layton * nfm_mark. The fsnotify code will take a reference and put 28565294c1fSJeff Layton * it on failure, so we can't just free it directly. It's also 28665294c1fSJeff Layton * not safe to call fsnotify_destroy_mark on it as the 28765294c1fSJeff Layton * mark->group will be NULL. Thus, we can't let the nfm_ref 28865294c1fSJeff Layton * counter drive the destruction at this point. 28965294c1fSJeff Layton */ 29065294c1fSJeff Layton if (likely(!err)) 29165294c1fSJeff Layton nfm = new; 29265294c1fSJeff Layton else 29365294c1fSJeff Layton fsnotify_put_mark(&new->nfm_mark); 29465294c1fSJeff Layton } while (unlikely(err == -EEXIST)); 29565294c1fSJeff Layton 29665294c1fSJeff Layton return nfm; 29765294c1fSJeff Layton } 29865294c1fSJeff Layton 29965294c1fSJeff Layton static struct nfsd_file * 300*ce502f81SChuck Lever nfsd_file_alloc(struct nfsd_file_lookup_key *key, unsigned int may) 30165294c1fSJeff Layton { 30265294c1fSJeff Layton struct nfsd_file *nf; 30365294c1fSJeff Layton 30465294c1fSJeff Layton nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL); 30565294c1fSJeff Layton if (nf) { 30665294c1fSJeff Layton INIT_HLIST_NODE(&nf->nf_node); 30765294c1fSJeff Layton INIT_LIST_HEAD(&nf->nf_lru); 308904940e9SChuck Lever nf->nf_birthtime = ktime_get(); 30965294c1fSJeff Layton nf->nf_file = NULL; 31065294c1fSJeff Layton nf->nf_cred = get_current_cred(); 311*ce502f81SChuck Lever nf->nf_net = key->net; 31265294c1fSJeff Layton nf->nf_flags = 0; 313*ce502f81SChuck Lever __set_bit(NFSD_FILE_HASHED, &nf->nf_flags); 314*ce502f81SChuck Lever __set_bit(NFSD_FILE_PENDING, &nf->nf_flags); 315*ce502f81SChuck Lever nf->nf_inode = key->inode; 316*ce502f81SChuck Lever /* nf_ref is pre-incremented for hash table */ 317*ce502f81SChuck Lever refcount_set(&nf->nf_ref, 2); 318*ce502f81SChuck Lever nf->nf_may = key->need; 31965294c1fSJeff Layton nf->nf_mark = NULL; 32065294c1fSJeff Layton trace_nfsd_file_alloc(nf); 32165294c1fSJeff Layton } 32265294c1fSJeff Layton return nf; 32365294c1fSJeff Layton } 32465294c1fSJeff Layton 32565294c1fSJeff Layton static bool 32665294c1fSJeff Layton nfsd_file_free(struct nfsd_file *nf) 32765294c1fSJeff Layton { 328904940e9SChuck Lever s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime)); 32965294c1fSJeff Layton bool flush = false; 33065294c1fSJeff Layton 331d6329327SChuck Lever this_cpu_inc(nfsd_file_releases); 332904940e9SChuck Lever this_cpu_add(nfsd_file_total_age, age); 333d6329327SChuck Lever 33465294c1fSJeff Layton trace_nfsd_file_put_final(nf); 33565294c1fSJeff Layton if (nf->nf_mark) 33665294c1fSJeff Layton nfsd_file_mark_put(nf->nf_mark); 33765294c1fSJeff Layton if (nf->nf_file) { 33865294c1fSJeff Layton get_file(nf->nf_file); 33965294c1fSJeff Layton filp_close(nf->nf_file, NULL); 34065294c1fSJeff Layton fput(nf->nf_file); 34165294c1fSJeff Layton flush = true; 34265294c1fSJeff Layton } 343668ed92eSChuck Lever 344668ed92eSChuck Lever /* 345668ed92eSChuck Lever * If this item is still linked via nf_lru, that's a bug. 346668ed92eSChuck Lever * WARN and leak it to preserve system stability. 347668ed92eSChuck Lever */ 348668ed92eSChuck Lever if (WARN_ON_ONCE(!list_empty(&nf->nf_lru))) 349668ed92eSChuck Lever return flush; 350668ed92eSChuck Lever 35165294c1fSJeff Layton call_rcu(&nf->nf_rcu, nfsd_file_slab_free); 35265294c1fSJeff Layton return flush; 35365294c1fSJeff Layton } 35465294c1fSJeff Layton 355055b24a8STrond Myklebust static bool 356055b24a8STrond Myklebust nfsd_file_check_writeback(struct nfsd_file *nf) 357055b24a8STrond Myklebust { 358055b24a8STrond Myklebust struct file *file = nf->nf_file; 359055b24a8STrond Myklebust struct address_space *mapping; 360055b24a8STrond Myklebust 361055b24a8STrond Myklebust if (!file || !(file->f_mode & FMODE_WRITE)) 362055b24a8STrond Myklebust return false; 363055b24a8STrond Myklebust mapping = file->f_mapping; 364055b24a8STrond Myklebust return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) || 365055b24a8STrond Myklebust mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK); 366055b24a8STrond Myklebust } 367055b24a8STrond Myklebust 368055b24a8STrond Myklebust static int 369055b24a8STrond Myklebust nfsd_file_check_write_error(struct nfsd_file *nf) 370055b24a8STrond Myklebust { 371055b24a8STrond Myklebust struct file *file = nf->nf_file; 372055b24a8STrond Myklebust 373055b24a8STrond Myklebust if (!file || !(file->f_mode & FMODE_WRITE)) 374055b24a8STrond Myklebust return 0; 375055b24a8STrond Myklebust return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err)); 376055b24a8STrond Myklebust } 377055b24a8STrond Myklebust 37865294c1fSJeff Layton static void 3796b8a9433STrond Myklebust nfsd_file_flush(struct nfsd_file *nf) 3806b8a9433STrond Myklebust { 381df2aff52SChuck Lever struct file *file = nf->nf_file; 382df2aff52SChuck Lever 383df2aff52SChuck Lever if (!file || !(file->f_mode & FMODE_WRITE)) 384df2aff52SChuck Lever return; 385df2aff52SChuck Lever this_cpu_add(nfsd_file_pages_flushed, file->f_mapping->nrpages); 386df2aff52SChuck Lever if (vfs_fsync(file, 1) != 0) 3876b8a9433STrond Myklebust nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 3886b8a9433STrond Myklebust } 3896b8a9433STrond Myklebust 390c46203acSChuck Lever static void nfsd_file_lru_add(struct nfsd_file *nf) 391c46203acSChuck Lever { 3924a0e73e6SChuck Lever set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags); 393c46203acSChuck Lever if (list_lru_add(&nfsd_file_lru, &nf->nf_lru)) 394c46203acSChuck Lever trace_nfsd_file_lru_add(nf); 395c46203acSChuck Lever } 396c46203acSChuck Lever 397c46203acSChuck Lever static void nfsd_file_lru_remove(struct nfsd_file *nf) 398c46203acSChuck Lever { 399c46203acSChuck Lever if (list_lru_del(&nfsd_file_lru, &nf->nf_lru)) 400c46203acSChuck Lever trace_nfsd_file_lru_del(nf); 401c46203acSChuck Lever } 402c46203acSChuck Lever 4036b8a9433STrond Myklebust static void 404*ce502f81SChuck Lever nfsd_file_hash_remove(struct nfsd_file *nf) 40565294c1fSJeff Layton { 40665294c1fSJeff Layton trace_nfsd_file_unhash(nf); 40765294c1fSJeff Layton 408055b24a8STrond Myklebust if (nfsd_file_check_write_error(nf)) 4093988a578SChuck Lever nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 410*ce502f81SChuck Lever rhashtable_remove_fast(&nfsd_file_rhash_tbl, &nf->nf_rhash, 411*ce502f81SChuck Lever nfsd_file_rhash_params); 412cb7ec76eSChuck Lever } 413cb7ec76eSChuck Lever 41465294c1fSJeff Layton static bool 41565294c1fSJeff Layton nfsd_file_unhash(struct nfsd_file *nf) 41665294c1fSJeff Layton { 41765294c1fSJeff Layton if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 418*ce502f81SChuck Lever nfsd_file_hash_remove(nf); 41965294c1fSJeff Layton return true; 42065294c1fSJeff Layton } 42165294c1fSJeff Layton return false; 42265294c1fSJeff Layton } 42365294c1fSJeff Layton 42465294c1fSJeff Layton /* 42565294c1fSJeff Layton * Return true if the file was unhashed. 42665294c1fSJeff Layton */ 42765294c1fSJeff Layton static bool 428*ce502f81SChuck Lever nfsd_file_unhash_and_dispose(struct nfsd_file *nf, struct list_head *dispose) 42965294c1fSJeff Layton { 430*ce502f81SChuck Lever trace_nfsd_file_unhash_and_dispose(nf); 43165294c1fSJeff Layton if (!nfsd_file_unhash(nf)) 43265294c1fSJeff Layton return false; 43365294c1fSJeff Layton /* keep final reference for nfsd_file_lru_dispose */ 434689827cdSTrond Myklebust if (refcount_dec_not_one(&nf->nf_ref)) 43565294c1fSJeff Layton return true; 43665294c1fSJeff Layton 4374a0e73e6SChuck Lever nfsd_file_lru_remove(nf); 43865294c1fSJeff Layton list_add(&nf->nf_lru, dispose); 43965294c1fSJeff Layton return true; 44065294c1fSJeff Layton } 44165294c1fSJeff Layton 442b6669305STrond Myklebust static void 44365294c1fSJeff Layton nfsd_file_put_noref(struct nfsd_file *nf) 44465294c1fSJeff Layton { 44565294c1fSJeff Layton trace_nfsd_file_put(nf); 44665294c1fSJeff Layton 447689827cdSTrond Myklebust if (refcount_dec_and_test(&nf->nf_ref)) { 44865294c1fSJeff Layton WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags)); 4494a0e73e6SChuck Lever nfsd_file_lru_remove(nf); 45065294c1fSJeff Layton nfsd_file_free(nf); 45165294c1fSJeff Layton } 45265294c1fSJeff Layton } 45365294c1fSJeff Layton 45465294c1fSJeff Layton void 45565294c1fSJeff Layton nfsd_file_put(struct nfsd_file *nf) 45665294c1fSJeff Layton { 45708af54b3SChuck Lever might_sleep(); 45808af54b3SChuck Lever 4594a0e73e6SChuck Lever nfsd_file_lru_add(nf); 46099939792STrond Myklebust if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) { 4616b8a9433STrond Myklebust nfsd_file_flush(nf); 462b6669305STrond Myklebust nfsd_file_put_noref(nf); 463b6c71c66SChuck Lever } else if (nf->nf_file) { 464b6669305STrond Myklebust nfsd_file_put_noref(nf); 4659542e6a6STrond Myklebust nfsd_file_schedule_laundrette(); 466b6c71c66SChuck Lever } else 467b6c71c66SChuck Lever nfsd_file_put_noref(nf); 46865294c1fSJeff Layton } 46965294c1fSJeff Layton 47065294c1fSJeff Layton struct nfsd_file * 47165294c1fSJeff Layton nfsd_file_get(struct nfsd_file *nf) 47265294c1fSJeff Layton { 473689827cdSTrond Myklebust if (likely(refcount_inc_not_zero(&nf->nf_ref))) 47465294c1fSJeff Layton return nf; 47565294c1fSJeff Layton return NULL; 47665294c1fSJeff Layton } 47765294c1fSJeff Layton 47865294c1fSJeff Layton static void 47965294c1fSJeff Layton nfsd_file_dispose_list(struct list_head *dispose) 48065294c1fSJeff Layton { 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); 48765294c1fSJeff Layton nfsd_file_put_noref(nf); 48865294c1fSJeff Layton } 48965294c1fSJeff Layton } 49065294c1fSJeff Layton 49165294c1fSJeff Layton static void 49265294c1fSJeff Layton nfsd_file_dispose_list_sync(struct list_head *dispose) 49365294c1fSJeff Layton { 49465294c1fSJeff Layton bool flush = false; 49565294c1fSJeff Layton struct nfsd_file *nf; 49665294c1fSJeff Layton 49765294c1fSJeff Layton while(!list_empty(dispose)) { 49865294c1fSJeff Layton nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 499668ed92eSChuck Lever list_del_init(&nf->nf_lru); 5006b8a9433STrond Myklebust nfsd_file_flush(nf); 501689827cdSTrond Myklebust if (!refcount_dec_and_test(&nf->nf_ref)) 50265294c1fSJeff Layton continue; 50365294c1fSJeff Layton if (nfsd_file_free(nf)) 50465294c1fSJeff Layton flush = true; 50565294c1fSJeff Layton } 50665294c1fSJeff Layton if (flush) 50765294c1fSJeff Layton flush_delayed_fput(); 50865294c1fSJeff Layton } 50965294c1fSJeff Layton 5109542e6a6STrond Myklebust static void 5119542e6a6STrond Myklebust nfsd_file_list_remove_disposal(struct list_head *dst, 5129542e6a6STrond Myklebust struct nfsd_fcache_disposal *l) 5139542e6a6STrond Myklebust { 5149542e6a6STrond Myklebust spin_lock(&l->lock); 5159542e6a6STrond Myklebust list_splice_init(&l->freeme, dst); 5169542e6a6STrond Myklebust spin_unlock(&l->lock); 5179542e6a6STrond Myklebust } 5189542e6a6STrond Myklebust 5199542e6a6STrond Myklebust static void 5209542e6a6STrond Myklebust nfsd_file_list_add_disposal(struct list_head *files, struct net *net) 5219542e6a6STrond Myklebust { 5221463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 5231463b38eSNeilBrown struct nfsd_fcache_disposal *l = nn->fcache_disposal; 5249542e6a6STrond Myklebust 5259542e6a6STrond Myklebust spin_lock(&l->lock); 5269542e6a6STrond Myklebust list_splice_tail_init(files, &l->freeme); 5279542e6a6STrond Myklebust spin_unlock(&l->lock); 5289542e6a6STrond Myklebust queue_work(nfsd_filecache_wq, &l->work); 5299542e6a6STrond Myklebust } 5309542e6a6STrond Myklebust 5319542e6a6STrond Myklebust static void 5329542e6a6STrond Myklebust nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src, 5339542e6a6STrond Myklebust struct net *net) 5349542e6a6STrond Myklebust { 5359542e6a6STrond Myklebust struct nfsd_file *nf, *tmp; 5369542e6a6STrond Myklebust 5379542e6a6STrond Myklebust list_for_each_entry_safe(nf, tmp, src, nf_lru) { 5389542e6a6STrond Myklebust if (nf->nf_net == net) 5399542e6a6STrond Myklebust list_move_tail(&nf->nf_lru, dst); 5409542e6a6STrond Myklebust } 5419542e6a6STrond Myklebust } 5429542e6a6STrond Myklebust 5439542e6a6STrond Myklebust static void 5449542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(struct list_head *dispose) 5459542e6a6STrond Myklebust { 5469542e6a6STrond Myklebust LIST_HEAD(list); 5479542e6a6STrond Myklebust struct nfsd_file *nf; 5489542e6a6STrond Myklebust 5499542e6a6STrond Myklebust while(!list_empty(dispose)) { 5509542e6a6STrond Myklebust nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 5519542e6a6STrond Myklebust nfsd_file_list_add_pernet(&list, dispose, nf->nf_net); 5529542e6a6STrond Myklebust nfsd_file_list_add_disposal(&list, nf->nf_net); 5539542e6a6STrond Myklebust } 5549542e6a6STrond Myklebust } 5559542e6a6STrond Myklebust 5564a0e73e6SChuck Lever /** 5574a0e73e6SChuck Lever * nfsd_file_lru_cb - Examine an entry on the LRU list 5584a0e73e6SChuck Lever * @item: LRU entry to examine 5594a0e73e6SChuck Lever * @lru: controlling LRU 5604a0e73e6SChuck Lever * @lock: LRU list lock (unused) 5614a0e73e6SChuck Lever * @arg: dispose list 5624a0e73e6SChuck Lever * 56365294c1fSJeff Layton * Note this can deadlock with nfsd_file_cache_purge. 5644a0e73e6SChuck Lever * 5654a0e73e6SChuck Lever * Return values: 5664a0e73e6SChuck Lever * %LRU_REMOVED: @item was removed from the LRU 567edead3a5SChuck Lever * %LRU_ROTATE: @item is to be moved to the LRU tail 5684a0e73e6SChuck Lever * %LRU_SKIP: @item cannot be evicted 56965294c1fSJeff Layton */ 57065294c1fSJeff Layton static enum lru_status 57165294c1fSJeff Layton nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru, 57265294c1fSJeff Layton spinlock_t *lock, void *arg) 57365294c1fSJeff Layton __releases(lock) 57465294c1fSJeff Layton __acquires(lock) 57565294c1fSJeff Layton { 57665294c1fSJeff Layton struct list_head *head = arg; 57765294c1fSJeff Layton struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru); 57865294c1fSJeff Layton 57965294c1fSJeff Layton /* 58065294c1fSJeff Layton * Do a lockless refcount check. The hashtable holds one reference, so 58165294c1fSJeff Layton * we look to see if anything else has a reference, or if any have 58265294c1fSJeff Layton * been put since the shrinker last ran. Those don't get unhashed and 58365294c1fSJeff Layton * released. 58465294c1fSJeff Layton * 58565294c1fSJeff Layton * Note that in the put path, we set the flag and then decrement the 58665294c1fSJeff Layton * counter. Here we check the counter and then test and clear the flag. 58765294c1fSJeff Layton * That order is deliberate to ensure that we can do this locklessly. 58865294c1fSJeff Layton */ 589c46203acSChuck Lever if (refcount_read(&nf->nf_ref) > 1) { 5904a0e73e6SChuck Lever list_lru_isolate(lru, &nf->nf_lru); 591c46203acSChuck Lever trace_nfsd_file_gc_in_use(nf); 5924a0e73e6SChuck Lever return LRU_REMOVED; 593c46203acSChuck Lever } 594055b24a8STrond Myklebust 595055b24a8STrond Myklebust /* 596055b24a8STrond Myklebust * Don't throw out files that are still undergoing I/O or 597055b24a8STrond Myklebust * that have uncleared errors pending. 598055b24a8STrond Myklebust */ 599c46203acSChuck Lever if (nfsd_file_check_writeback(nf)) { 600c46203acSChuck Lever trace_nfsd_file_gc_writeback(nf); 601c46203acSChuck Lever return LRU_SKIP; 602c46203acSChuck Lever } 603055b24a8STrond Myklebust 604c46203acSChuck Lever if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) { 605c46203acSChuck Lever trace_nfsd_file_gc_referenced(nf); 606edead3a5SChuck Lever return LRU_ROTATE; 607c46203acSChuck Lever } 60865294c1fSJeff Layton 609c46203acSChuck Lever if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 610c46203acSChuck Lever trace_nfsd_file_gc_hashed(nf); 611c46203acSChuck Lever return LRU_SKIP; 612c46203acSChuck Lever } 61365294c1fSJeff Layton 61465294c1fSJeff Layton list_lru_isolate_move(lru, &nf->nf_lru, head); 61594660cc1SChuck Lever this_cpu_inc(nfsd_file_evictions); 616c46203acSChuck Lever trace_nfsd_file_gc_disposed(nf); 61765294c1fSJeff Layton return LRU_REMOVED; 61865294c1fSJeff Layton } 61965294c1fSJeff Layton 6200bac5a26SChuck Lever /* 6210bac5a26SChuck Lever * Unhash items on @dispose immediately, then queue them on the 6220bac5a26SChuck Lever * disposal workqueue to finish releasing them in the background. 6230bac5a26SChuck Lever * 6240bac5a26SChuck Lever * cel: Note that between the time list_lru_shrink_walk runs and 6250bac5a26SChuck Lever * now, these items are in the hash table but marked unhashed. 6260bac5a26SChuck Lever * Why release these outside of lru_cb ? There's no lock ordering 6270bac5a26SChuck Lever * problem since lru_cb currently takes no lock. 6280bac5a26SChuck Lever */ 6290bac5a26SChuck Lever static void nfsd_file_gc_dispose_list(struct list_head *dispose) 6300bac5a26SChuck Lever { 6310bac5a26SChuck Lever struct nfsd_file *nf; 6320bac5a26SChuck Lever 633cb7ec76eSChuck Lever list_for_each_entry(nf, dispose, nf_lru) 634cb7ec76eSChuck Lever nfsd_file_hash_remove(nf); 6350bac5a26SChuck Lever nfsd_file_dispose_list_delayed(dispose); 6360bac5a26SChuck Lever } 6370bac5a26SChuck Lever 6389542e6a6STrond Myklebust static void 6399542e6a6STrond Myklebust nfsd_file_gc(void) 6409542e6a6STrond Myklebust { 6413bc6d347SChuck Lever LIST_HEAD(dispose); 64294660cc1SChuck Lever unsigned long ret; 6433bc6d347SChuck Lever 64494660cc1SChuck Lever ret = list_lru_walk(&nfsd_file_lru, nfsd_file_lru_cb, 645edead3a5SChuck Lever &dispose, list_lru_count(&nfsd_file_lru)); 64694660cc1SChuck Lever trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru)); 6473bc6d347SChuck Lever nfsd_file_gc_dispose_list(&dispose); 6489542e6a6STrond Myklebust } 6499542e6a6STrond Myklebust 6509542e6a6STrond Myklebust static void 6519542e6a6STrond Myklebust nfsd_file_gc_worker(struct work_struct *work) 6529542e6a6STrond Myklebust { 6539542e6a6STrond Myklebust nfsd_file_gc(); 6549542e6a6STrond Myklebust nfsd_file_schedule_laundrette(); 65565294c1fSJeff Layton } 65665294c1fSJeff Layton 65765294c1fSJeff Layton static unsigned long 65865294c1fSJeff Layton nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc) 65965294c1fSJeff Layton { 66065294c1fSJeff Layton return list_lru_count(&nfsd_file_lru); 66165294c1fSJeff Layton } 66265294c1fSJeff Layton 66365294c1fSJeff Layton static unsigned long 66465294c1fSJeff Layton nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc) 66565294c1fSJeff Layton { 66639f1d1ffSChuck Lever LIST_HEAD(dispose); 66739f1d1ffSChuck Lever unsigned long ret; 66839f1d1ffSChuck Lever 66939f1d1ffSChuck Lever ret = list_lru_shrink_walk(&nfsd_file_lru, sc, 67039f1d1ffSChuck Lever nfsd_file_lru_cb, &dispose); 67194660cc1SChuck Lever trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru)); 67239f1d1ffSChuck Lever nfsd_file_gc_dispose_list(&dispose); 67339f1d1ffSChuck Lever return ret; 67465294c1fSJeff Layton } 67565294c1fSJeff Layton 67665294c1fSJeff Layton static struct shrinker nfsd_file_shrinker = { 67765294c1fSJeff Layton .scan_objects = nfsd_file_lru_scan, 67865294c1fSJeff Layton .count_objects = nfsd_file_lru_count, 67965294c1fSJeff Layton .seeks = 1, 68065294c1fSJeff Layton }; 68165294c1fSJeff Layton 682a8455110SChuck Lever /* 683a8455110SChuck Lever * Find all cache items across all net namespaces that match @inode and 684a8455110SChuck Lever * move them to @dispose. The lookup is atomic wrt nfsd_file_acquire(). 685a8455110SChuck Lever */ 686a8455110SChuck Lever static unsigned int 687a8455110SChuck Lever __nfsd_file_close_inode(struct inode *inode, struct list_head *dispose) 68865294c1fSJeff Layton { 689*ce502f81SChuck Lever struct nfsd_file_lookup_key key = { 690*ce502f81SChuck Lever .type = NFSD_FILE_KEY_INODE, 691*ce502f81SChuck Lever .inode = inode, 692*ce502f81SChuck Lever }; 693a8455110SChuck Lever unsigned int count = 0; 69465294c1fSJeff Layton struct nfsd_file *nf; 69565294c1fSJeff Layton 696*ce502f81SChuck Lever rcu_read_lock(); 697*ce502f81SChuck Lever do { 698*ce502f81SChuck Lever nf = rhashtable_lookup(&nfsd_file_rhash_tbl, &key, 699*ce502f81SChuck Lever nfsd_file_rhash_params); 700*ce502f81SChuck Lever if (!nf) 701*ce502f81SChuck Lever break; 702*ce502f81SChuck Lever nfsd_file_unhash_and_dispose(nf, dispose); 703a8455110SChuck Lever count++; 704*ce502f81SChuck Lever } while (1); 705*ce502f81SChuck Lever rcu_read_unlock(); 706a8455110SChuck Lever return count; 70765294c1fSJeff Layton } 70865294c1fSJeff Layton 70965294c1fSJeff Layton /** 71065294c1fSJeff Layton * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file 71165294c1fSJeff Layton * @inode: inode of the file to attempt to remove 71265294c1fSJeff Layton * 713a8455110SChuck Lever * Unhash and put, then flush and fput all cache items associated with @inode. 71465294c1fSJeff Layton */ 71565294c1fSJeff Layton void 71665294c1fSJeff Layton nfsd_file_close_inode_sync(struct inode *inode) 71765294c1fSJeff Layton { 71865294c1fSJeff Layton LIST_HEAD(dispose); 719a8455110SChuck Lever unsigned int count; 72065294c1fSJeff Layton 721a8455110SChuck Lever count = __nfsd_file_close_inode(inode, &dispose); 722a8455110SChuck Lever trace_nfsd_file_close_inode_sync(inode, count); 72365294c1fSJeff Layton nfsd_file_dispose_list_sync(&dispose); 72465294c1fSJeff Layton } 72565294c1fSJeff Layton 72665294c1fSJeff Layton /** 72719598141STrond Myklebust * nfsd_file_close_inode - attempt a delayed close of a nfsd_file 72865294c1fSJeff Layton * @inode: inode of the file to attempt to remove 72965294c1fSJeff Layton * 730a8455110SChuck Lever * Unhash and put all cache item associated with @inode. 73165294c1fSJeff Layton */ 73265294c1fSJeff Layton static void 73365294c1fSJeff Layton nfsd_file_close_inode(struct inode *inode) 73465294c1fSJeff Layton { 73565294c1fSJeff Layton LIST_HEAD(dispose); 736a8455110SChuck Lever unsigned int count; 73765294c1fSJeff Layton 738a8455110SChuck Lever count = __nfsd_file_close_inode(inode, &dispose); 739a8455110SChuck Lever trace_nfsd_file_close_inode(inode, count); 7409542e6a6STrond Myklebust nfsd_file_dispose_list_delayed(&dispose); 74165294c1fSJeff Layton } 74265294c1fSJeff Layton 74365294c1fSJeff Layton /** 74465294c1fSJeff Layton * nfsd_file_delayed_close - close unused nfsd_files 74565294c1fSJeff Layton * @work: dummy 74665294c1fSJeff Layton * 74765294c1fSJeff Layton * Walk the LRU list and close any entries that have not been used since 74865294c1fSJeff Layton * the last scan. 74965294c1fSJeff Layton * 75065294c1fSJeff Layton * Note this can deadlock with nfsd_file_cache_purge. 75165294c1fSJeff Layton */ 75265294c1fSJeff Layton static void 75365294c1fSJeff Layton nfsd_file_delayed_close(struct work_struct *work) 75465294c1fSJeff Layton { 75565294c1fSJeff Layton LIST_HEAD(head); 7569542e6a6STrond Myklebust struct nfsd_fcache_disposal *l = container_of(work, 7579542e6a6STrond Myklebust struct nfsd_fcache_disposal, work); 75865294c1fSJeff Layton 7599542e6a6STrond Myklebust nfsd_file_list_remove_disposal(&head, l); 7609542e6a6STrond Myklebust nfsd_file_dispose_list(&head); 76165294c1fSJeff Layton } 76265294c1fSJeff Layton 76365294c1fSJeff Layton static int 76465294c1fSJeff Layton nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg, 76565294c1fSJeff Layton void *data) 76665294c1fSJeff Layton { 76765294c1fSJeff Layton struct file_lock *fl = data; 76865294c1fSJeff Layton 76965294c1fSJeff Layton /* Only close files for F_SETLEASE leases */ 77065294c1fSJeff Layton if (fl->fl_flags & FL_LEASE) 77165294c1fSJeff Layton nfsd_file_close_inode_sync(file_inode(fl->fl_file)); 77265294c1fSJeff Layton return 0; 77365294c1fSJeff Layton } 77465294c1fSJeff Layton 77565294c1fSJeff Layton static struct notifier_block nfsd_file_lease_notifier = { 77665294c1fSJeff Layton .notifier_call = nfsd_file_lease_notifier_call, 77765294c1fSJeff Layton }; 77865294c1fSJeff Layton 77965294c1fSJeff Layton static int 780b9a1b977SAmir Goldstein nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask, 781b9a1b977SAmir Goldstein struct inode *inode, struct inode *dir, 782950cc0d2SAmir Goldstein const struct qstr *name, u32 cookie) 78365294c1fSJeff Layton { 78424dca905SGabriel Krisman Bertazi if (WARN_ON_ONCE(!inode)) 78524dca905SGabriel Krisman Bertazi return 0; 78624dca905SGabriel Krisman Bertazi 78765294c1fSJeff Layton trace_nfsd_file_fsnotify_handle_event(inode, mask); 78865294c1fSJeff Layton 78965294c1fSJeff Layton /* Should be no marks on non-regular files */ 79065294c1fSJeff Layton if (!S_ISREG(inode->i_mode)) { 79165294c1fSJeff Layton WARN_ON_ONCE(1); 79265294c1fSJeff Layton return 0; 79365294c1fSJeff Layton } 79465294c1fSJeff Layton 79565294c1fSJeff Layton /* don't close files if this was not the last link */ 79665294c1fSJeff Layton if (mask & FS_ATTRIB) { 79765294c1fSJeff Layton if (inode->i_nlink) 79865294c1fSJeff Layton return 0; 79965294c1fSJeff Layton } 80065294c1fSJeff Layton 80165294c1fSJeff Layton nfsd_file_close_inode(inode); 80265294c1fSJeff Layton return 0; 80365294c1fSJeff Layton } 80465294c1fSJeff Layton 80565294c1fSJeff Layton 80665294c1fSJeff Layton static const struct fsnotify_ops nfsd_file_fsnotify_ops = { 807b9a1b977SAmir Goldstein .handle_inode_event = nfsd_file_fsnotify_handle_event, 80865294c1fSJeff Layton .free_mark = nfsd_file_mark_free, 80965294c1fSJeff Layton }; 81065294c1fSJeff Layton 81165294c1fSJeff Layton int 81265294c1fSJeff Layton nfsd_file_cache_init(void) 81365294c1fSJeff Layton { 814fc22945eSChuck Lever int ret; 81565294c1fSJeff Layton unsigned int i; 81665294c1fSJeff Layton 817c7b824c3SChuck Lever lockdep_assert_held(&nfsd_mutex); 818c7b824c3SChuck Lever if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) 81965294c1fSJeff Layton return 0; 82065294c1fSJeff Layton 821fc22945eSChuck Lever ret = rhashtable_init(&nfsd_file_rhash_tbl, &nfsd_file_rhash_params); 822fc22945eSChuck Lever if (ret) 823fc22945eSChuck Lever return ret; 824fc22945eSChuck Lever 825fc22945eSChuck Lever ret = -ENOMEM; 8269542e6a6STrond Myklebust nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0); 8279542e6a6STrond Myklebust if (!nfsd_filecache_wq) 8289542e6a6STrond Myklebust goto out; 8299542e6a6STrond Myklebust 8304d2eeafeSAmir Goldstein nfsd_file_hashtbl = kvcalloc(NFSD_FILE_HASH_SIZE, 83165294c1fSJeff Layton sizeof(*nfsd_file_hashtbl), GFP_KERNEL); 83265294c1fSJeff Layton if (!nfsd_file_hashtbl) { 83365294c1fSJeff Layton pr_err("nfsd: unable to allocate nfsd_file_hashtbl\n"); 83465294c1fSJeff Layton goto out_err; 83565294c1fSJeff Layton } 83665294c1fSJeff Layton 83765294c1fSJeff Layton nfsd_file_slab = kmem_cache_create("nfsd_file", 83865294c1fSJeff Layton sizeof(struct nfsd_file), 0, 0, NULL); 83965294c1fSJeff Layton if (!nfsd_file_slab) { 84065294c1fSJeff Layton pr_err("nfsd: unable to create nfsd_file_slab\n"); 84165294c1fSJeff Layton goto out_err; 84265294c1fSJeff Layton } 84365294c1fSJeff Layton 84465294c1fSJeff Layton nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark", 84565294c1fSJeff Layton sizeof(struct nfsd_file_mark), 0, 0, NULL); 84665294c1fSJeff Layton if (!nfsd_file_mark_slab) { 84765294c1fSJeff Layton pr_err("nfsd: unable to create nfsd_file_mark_slab\n"); 84865294c1fSJeff Layton goto out_err; 84965294c1fSJeff Layton } 85065294c1fSJeff Layton 85165294c1fSJeff Layton 85265294c1fSJeff Layton ret = list_lru_init(&nfsd_file_lru); 85365294c1fSJeff Layton if (ret) { 85465294c1fSJeff Layton pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret); 85565294c1fSJeff Layton goto out_err; 85665294c1fSJeff Layton } 85765294c1fSJeff Layton 85865294c1fSJeff Layton ret = register_shrinker(&nfsd_file_shrinker); 85965294c1fSJeff Layton if (ret) { 86065294c1fSJeff Layton pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret); 86165294c1fSJeff Layton goto out_lru; 86265294c1fSJeff Layton } 86365294c1fSJeff Layton 86465294c1fSJeff Layton ret = lease_register_notifier(&nfsd_file_lease_notifier); 86565294c1fSJeff Layton if (ret) { 86665294c1fSJeff Layton pr_err("nfsd: unable to register lease notifier: %d\n", ret); 86765294c1fSJeff Layton goto out_shrinker; 86865294c1fSJeff Layton } 86965294c1fSJeff Layton 870867a448dSAmir Goldstein nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops, 871b8962a9dSAmir Goldstein FSNOTIFY_GROUP_NOFS); 87265294c1fSJeff Layton if (IS_ERR(nfsd_file_fsnotify_group)) { 87365294c1fSJeff Layton pr_err("nfsd: unable to create fsnotify group: %ld\n", 87465294c1fSJeff Layton PTR_ERR(nfsd_file_fsnotify_group)); 875231307dfSHuang Guobin ret = PTR_ERR(nfsd_file_fsnotify_group); 87665294c1fSJeff Layton nfsd_file_fsnotify_group = NULL; 87765294c1fSJeff Layton goto out_notifier; 87865294c1fSJeff Layton } 87965294c1fSJeff Layton 88065294c1fSJeff Layton for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) { 88165294c1fSJeff Layton INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head); 88265294c1fSJeff Layton spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock); 88365294c1fSJeff Layton } 88465294c1fSJeff Layton 8859542e6a6STrond Myklebust INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker); 88665294c1fSJeff Layton out: 88765294c1fSJeff Layton return ret; 88865294c1fSJeff Layton out_notifier: 88965294c1fSJeff Layton lease_unregister_notifier(&nfsd_file_lease_notifier); 89065294c1fSJeff Layton out_shrinker: 89165294c1fSJeff Layton unregister_shrinker(&nfsd_file_shrinker); 89265294c1fSJeff Layton out_lru: 89365294c1fSJeff Layton list_lru_destroy(&nfsd_file_lru); 89465294c1fSJeff Layton out_err: 89565294c1fSJeff Layton kmem_cache_destroy(nfsd_file_slab); 89665294c1fSJeff Layton nfsd_file_slab = NULL; 89765294c1fSJeff Layton kmem_cache_destroy(nfsd_file_mark_slab); 89865294c1fSJeff Layton nfsd_file_mark_slab = NULL; 8994d2eeafeSAmir Goldstein kvfree(nfsd_file_hashtbl); 90065294c1fSJeff Layton nfsd_file_hashtbl = NULL; 9019542e6a6STrond Myklebust destroy_workqueue(nfsd_filecache_wq); 9029542e6a6STrond Myklebust nfsd_filecache_wq = NULL; 903fc22945eSChuck Lever rhashtable_destroy(&nfsd_file_rhash_tbl); 90465294c1fSJeff Layton goto out; 90565294c1fSJeff Layton } 90665294c1fSJeff Layton 90765294c1fSJeff Layton /* 90865294c1fSJeff Layton * Note this can deadlock with nfsd_file_lru_cb. 90965294c1fSJeff Layton */ 910c7b824c3SChuck Lever static void 911c7b824c3SChuck Lever __nfsd_file_cache_purge(struct net *net) 91265294c1fSJeff Layton { 913*ce502f81SChuck Lever struct rhashtable_iter iter; 91465294c1fSJeff Layton struct nfsd_file *nf; 91565294c1fSJeff Layton LIST_HEAD(dispose); 91665294c1fSJeff Layton bool del; 91765294c1fSJeff Layton 918*ce502f81SChuck Lever rhashtable_walk_enter(&nfsd_file_rhash_tbl, &iter); 919*ce502f81SChuck Lever do { 920*ce502f81SChuck Lever rhashtable_walk_start(&iter); 9215e113224STrond Myklebust 922*ce502f81SChuck Lever nf = rhashtable_walk_next(&iter); 923*ce502f81SChuck Lever while (!IS_ERR_OR_NULL(nf)) { 9245e113224STrond Myklebust if (net && nf->nf_net != net) 9255e113224STrond Myklebust continue; 926*ce502f81SChuck Lever del = nfsd_file_unhash_and_dispose(nf, &dispose); 92765294c1fSJeff Layton 92865294c1fSJeff Layton /* 92965294c1fSJeff Layton * Deadlock detected! Something marked this entry as 93065294c1fSJeff Layton * unhased, but hasn't removed it from the hash list. 93165294c1fSJeff Layton */ 93265294c1fSJeff Layton WARN_ON_ONCE(!del); 933*ce502f81SChuck Lever 934*ce502f81SChuck Lever nf = rhashtable_walk_next(&iter); 93565294c1fSJeff Layton } 936*ce502f81SChuck Lever 937*ce502f81SChuck Lever rhashtable_walk_stop(&iter); 938*ce502f81SChuck Lever } while (nf == ERR_PTR(-EAGAIN)); 939*ce502f81SChuck Lever rhashtable_walk_exit(&iter); 940*ce502f81SChuck Lever 94165294c1fSJeff Layton nfsd_file_dispose_list(&dispose); 94265294c1fSJeff Layton } 94365294c1fSJeff Layton 9449542e6a6STrond Myklebust static struct nfsd_fcache_disposal * 9451463b38eSNeilBrown nfsd_alloc_fcache_disposal(void) 9469542e6a6STrond Myklebust { 9479542e6a6STrond Myklebust struct nfsd_fcache_disposal *l; 9489542e6a6STrond Myklebust 9499542e6a6STrond Myklebust l = kmalloc(sizeof(*l), GFP_KERNEL); 9509542e6a6STrond Myklebust if (!l) 9519542e6a6STrond Myklebust return NULL; 9529542e6a6STrond Myklebust INIT_WORK(&l->work, nfsd_file_delayed_close); 9539542e6a6STrond Myklebust spin_lock_init(&l->lock); 9549542e6a6STrond Myklebust INIT_LIST_HEAD(&l->freeme); 9559542e6a6STrond Myklebust return l; 9569542e6a6STrond Myklebust } 9579542e6a6STrond Myklebust 9589542e6a6STrond Myklebust static void 9599542e6a6STrond Myklebust nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l) 9609542e6a6STrond Myklebust { 9619542e6a6STrond Myklebust cancel_work_sync(&l->work); 9629542e6a6STrond Myklebust nfsd_file_dispose_list(&l->freeme); 9631463b38eSNeilBrown kfree(l); 9649542e6a6STrond Myklebust } 9659542e6a6STrond Myklebust 9669542e6a6STrond Myklebust static void 9679542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(struct net *net) 9689542e6a6STrond Myklebust { 9691463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 9701463b38eSNeilBrown struct nfsd_fcache_disposal *l = nn->fcache_disposal; 9719542e6a6STrond Myklebust 9729542e6a6STrond Myklebust nfsd_free_fcache_disposal(l); 9739542e6a6STrond Myklebust } 9749542e6a6STrond Myklebust 9759542e6a6STrond Myklebust int 9769542e6a6STrond Myklebust nfsd_file_cache_start_net(struct net *net) 9779542e6a6STrond Myklebust { 9781463b38eSNeilBrown struct nfsd_net *nn = net_generic(net, nfsd_net_id); 9791463b38eSNeilBrown 9801463b38eSNeilBrown nn->fcache_disposal = nfsd_alloc_fcache_disposal(); 9811463b38eSNeilBrown return nn->fcache_disposal ? 0 : -ENOMEM; 9829542e6a6STrond Myklebust } 9839542e6a6STrond Myklebust 984c7b824c3SChuck Lever /** 985c7b824c3SChuck Lever * nfsd_file_cache_purge - Remove all cache items associated with @net 986c7b824c3SChuck Lever * @net: target net namespace 987c7b824c3SChuck Lever * 988c7b824c3SChuck Lever */ 989c7b824c3SChuck Lever void 990c7b824c3SChuck Lever nfsd_file_cache_purge(struct net *net) 991c7b824c3SChuck Lever { 992c7b824c3SChuck Lever lockdep_assert_held(&nfsd_mutex); 993c7b824c3SChuck Lever if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) 994c7b824c3SChuck Lever __nfsd_file_cache_purge(net); 995c7b824c3SChuck Lever } 996c7b824c3SChuck Lever 9979542e6a6STrond Myklebust void 9989542e6a6STrond Myklebust nfsd_file_cache_shutdown_net(struct net *net) 9999542e6a6STrond Myklebust { 10009542e6a6STrond Myklebust nfsd_file_cache_purge(net); 10019542e6a6STrond Myklebust nfsd_free_fcache_disposal_net(net); 10029542e6a6STrond Myklebust } 10039542e6a6STrond Myklebust 100465294c1fSJeff Layton void 100565294c1fSJeff Layton nfsd_file_cache_shutdown(void) 100665294c1fSJeff Layton { 10078b330f78SChuck Lever int i; 10088b330f78SChuck Lever 1009c7b824c3SChuck Lever lockdep_assert_held(&nfsd_mutex); 1010c7b824c3SChuck Lever if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0) 1011c7b824c3SChuck Lever return; 101265294c1fSJeff Layton 101365294c1fSJeff Layton lease_unregister_notifier(&nfsd_file_lease_notifier); 101465294c1fSJeff Layton unregister_shrinker(&nfsd_file_shrinker); 101565294c1fSJeff Layton /* 101665294c1fSJeff Layton * make sure all callers of nfsd_file_lru_cb are done before 101765294c1fSJeff Layton * calling nfsd_file_cache_purge 101865294c1fSJeff Layton */ 101965294c1fSJeff Layton cancel_delayed_work_sync(&nfsd_filecache_laundrette); 1020c7b824c3SChuck Lever __nfsd_file_cache_purge(NULL); 102165294c1fSJeff Layton list_lru_destroy(&nfsd_file_lru); 102265294c1fSJeff Layton rcu_barrier(); 102365294c1fSJeff Layton fsnotify_put_group(nfsd_file_fsnotify_group); 102465294c1fSJeff Layton nfsd_file_fsnotify_group = NULL; 102565294c1fSJeff Layton kmem_cache_destroy(nfsd_file_slab); 102665294c1fSJeff Layton nfsd_file_slab = NULL; 102765294c1fSJeff Layton fsnotify_wait_marks_destroyed(); 102865294c1fSJeff Layton kmem_cache_destroy(nfsd_file_mark_slab); 102965294c1fSJeff Layton nfsd_file_mark_slab = NULL; 10304d2eeafeSAmir Goldstein kvfree(nfsd_file_hashtbl); 103165294c1fSJeff Layton nfsd_file_hashtbl = NULL; 10329542e6a6STrond Myklebust destroy_workqueue(nfsd_filecache_wq); 10339542e6a6STrond Myklebust nfsd_filecache_wq = NULL; 1034fc22945eSChuck Lever rhashtable_destroy(&nfsd_file_rhash_tbl); 10358b330f78SChuck Lever 10368b330f78SChuck Lever for_each_possible_cpu(i) { 10378b330f78SChuck Lever per_cpu(nfsd_file_cache_hits, i) = 0; 10388b330f78SChuck Lever per_cpu(nfsd_file_acquisitions, i) = 0; 10398b330f78SChuck Lever per_cpu(nfsd_file_releases, i) = 0; 10408b330f78SChuck Lever per_cpu(nfsd_file_total_age, i) = 0; 10418b330f78SChuck Lever per_cpu(nfsd_file_pages_flushed, i) = 0; 10428b330f78SChuck Lever per_cpu(nfsd_file_evictions, i) = 0; 10438b330f78SChuck Lever } 104465294c1fSJeff Layton } 104565294c1fSJeff Layton 104665294c1fSJeff Layton /** 1047*ce502f81SChuck Lever * nfsd_file_is_cached - are there any cached open files for this inode? 1048*ce502f81SChuck Lever * @inode: inode to check 104965294c1fSJeff Layton * 1050*ce502f81SChuck Lever * The lookup matches inodes in all net namespaces and is atomic wrt 1051*ce502f81SChuck Lever * nfsd_file_acquire(). 1052*ce502f81SChuck Lever * 1053*ce502f81SChuck Lever * Return values: 1054*ce502f81SChuck Lever * %true: filecache contains at least one file matching this inode 1055*ce502f81SChuck Lever * %false: filecache contains no files matching this inode 105665294c1fSJeff Layton */ 105765294c1fSJeff Layton bool 105865294c1fSJeff Layton nfsd_file_is_cached(struct inode *inode) 105965294c1fSJeff Layton { 1060*ce502f81SChuck Lever struct nfsd_file_lookup_key key = { 1061*ce502f81SChuck Lever .type = NFSD_FILE_KEY_INODE, 1062*ce502f81SChuck Lever .inode = inode, 1063*ce502f81SChuck Lever }; 106465294c1fSJeff Layton bool ret = false; 106565294c1fSJeff Layton 1066*ce502f81SChuck Lever if (rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key, 1067*ce502f81SChuck Lever nfsd_file_rhash_params) != NULL) 106865294c1fSJeff Layton ret = true; 106954f7df70SChuck Lever trace_nfsd_file_is_cached(inode, (int)ret); 107065294c1fSJeff Layton return ret; 107165294c1fSJeff Layton } 107265294c1fSJeff Layton 1073fb70bf12SChuck Lever static __be32 1074fb70bf12SChuck Lever nfsd_do_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, 1075fb70bf12SChuck Lever unsigned int may_flags, struct nfsd_file **pnf, bool open) 107665294c1fSJeff Layton { 1077*ce502f81SChuck Lever struct nfsd_file_lookup_key key = { 1078*ce502f81SChuck Lever .type = NFSD_FILE_KEY_FULL, 1079*ce502f81SChuck Lever .need = may_flags & NFSD_FILE_MAY_MASK, 1080*ce502f81SChuck Lever .net = SVC_NET(rqstp), 1081*ce502f81SChuck Lever }; 108265294c1fSJeff Layton struct nfsd_file *nf, *new; 108328c7d86bSTrond Myklebust bool retry = true; 1084*ce502f81SChuck Lever __be32 status; 108565294c1fSJeff Layton 108665294c1fSJeff Layton status = fh_verify(rqstp, fhp, S_IFREG, 108765294c1fSJeff Layton may_flags|NFSD_MAY_OWNER_OVERRIDE); 108865294c1fSJeff Layton if (status != nfs_ok) 108965294c1fSJeff Layton return status; 1090*ce502f81SChuck Lever key.inode = d_inode(fhp->fh_dentry); 1091*ce502f81SChuck Lever key.cred = get_current_cred(); 109265294c1fSJeff Layton 109365294c1fSJeff Layton retry: 1094*ce502f81SChuck Lever /* Avoid allocation if the item is already in cache */ 1095*ce502f81SChuck Lever nf = rhashtable_lookup_fast(&nfsd_file_rhash_tbl, &key, 1096*ce502f81SChuck Lever nfsd_file_rhash_params); 1097*ce502f81SChuck Lever if (nf) 1098*ce502f81SChuck Lever nf = nfsd_file_get(nf); 109965294c1fSJeff Layton if (nf) 110065294c1fSJeff Layton goto wait_for_construction; 110165294c1fSJeff Layton 1102*ce502f81SChuck Lever new = nfsd_file_alloc(&key, may_flags); 110365294c1fSJeff Layton if (!new) { 110454f7df70SChuck Lever status = nfserr_jukebox; 110554f7df70SChuck Lever goto out_status; 110665294c1fSJeff Layton } 110765294c1fSJeff Layton 1108*ce502f81SChuck Lever nf = rhashtable_lookup_get_insert_key(&nfsd_file_rhash_tbl, 1109*ce502f81SChuck Lever &key, &new->nf_rhash, 1110*ce502f81SChuck Lever nfsd_file_rhash_params); 1111*ce502f81SChuck Lever if (!nf) { 1112*ce502f81SChuck Lever nf = new; 111365294c1fSJeff Layton goto open_file; 1114*ce502f81SChuck Lever } 1115*ce502f81SChuck Lever if (IS_ERR(nf)) 1116*ce502f81SChuck Lever goto insert_err; 1117*ce502f81SChuck Lever nf = nfsd_file_get(nf); 1118*ce502f81SChuck Lever if (nf == NULL) { 1119*ce502f81SChuck Lever nf = new; 1120*ce502f81SChuck Lever goto open_file; 1121*ce502f81SChuck Lever } 112265294c1fSJeff Layton nfsd_file_slab_free(&new->nf_rcu); 112365294c1fSJeff Layton 112465294c1fSJeff Layton wait_for_construction: 112565294c1fSJeff Layton wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE); 112665294c1fSJeff Layton 112765294c1fSJeff Layton /* Did construction of this file fail? */ 112865294c1fSJeff Layton if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 1129*ce502f81SChuck Lever trace_nfsd_file_cons_err(rqstp, key.inode, may_flags, nf); 113028c7d86bSTrond Myklebust if (!retry) { 113128c7d86bSTrond Myklebust status = nfserr_jukebox; 113228c7d86bSTrond Myklebust goto out; 113328c7d86bSTrond Myklebust } 113428c7d86bSTrond Myklebust retry = false; 113565294c1fSJeff Layton nfsd_file_put_noref(nf); 113665294c1fSJeff Layton goto retry; 113765294c1fSJeff Layton } 113865294c1fSJeff Layton 11394a0e73e6SChuck Lever nfsd_file_lru_remove(nf); 114065294c1fSJeff Layton this_cpu_inc(nfsd_file_cache_hits); 114165294c1fSJeff Layton 114223ba98deSJeff Layton status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags)); 114365294c1fSJeff Layton out: 114465294c1fSJeff Layton if (status == nfs_ok) { 114529d4bdbbSChuck Lever if (open) 114629d4bdbbSChuck Lever this_cpu_inc(nfsd_file_acquisitions); 114765294c1fSJeff Layton *pnf = nf; 114865294c1fSJeff Layton } else { 114965294c1fSJeff Layton nfsd_file_put(nf); 115065294c1fSJeff Layton nf = NULL; 115165294c1fSJeff Layton } 115265294c1fSJeff Layton 115354f7df70SChuck Lever out_status: 1154*ce502f81SChuck Lever put_cred(key.cred); 1155*ce502f81SChuck Lever trace_nfsd_file_acquire(rqstp, key.inode, may_flags, nf, status); 115665294c1fSJeff Layton return status; 115754f7df70SChuck Lever 115865294c1fSJeff Layton open_file: 115965294c1fSJeff Layton nf->nf_mark = nfsd_file_mark_find_or_create(nf); 1160fb70bf12SChuck Lever if (nf->nf_mark) { 11610122e882SChuck Lever if (open) { 1162f4d84c52SChuck Lever status = nfsd_open_verified(rqstp, fhp, may_flags, 1163f4d84c52SChuck Lever &nf->nf_file); 11640122e882SChuck Lever trace_nfsd_file_open(nf, status); 11650122e882SChuck Lever } else 1166fb70bf12SChuck Lever status = nfs_ok; 1167fb70bf12SChuck Lever } else 116865294c1fSJeff Layton status = nfserr_jukebox; 116965294c1fSJeff Layton /* 117065294c1fSJeff Layton * If construction failed, or we raced with a call to unlink() 117165294c1fSJeff Layton * then unhash. 117265294c1fSJeff Layton */ 1173*ce502f81SChuck Lever if (status != nfs_ok || key.inode->i_nlink == 0) 1174*ce502f81SChuck Lever if (nfsd_file_unhash(nf)) 117565294c1fSJeff Layton nfsd_file_put_noref(nf); 117665294c1fSJeff Layton clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags); 117765294c1fSJeff Layton smp_mb__after_atomic(); 117865294c1fSJeff Layton wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING); 117965294c1fSJeff Layton goto out; 1180*ce502f81SChuck Lever 1181*ce502f81SChuck Lever insert_err: 1182*ce502f81SChuck Lever nfsd_file_slab_free(&new->nf_rcu); 1183*ce502f81SChuck Lever trace_nfsd_file_insert_err(rqstp, key.inode, may_flags, PTR_ERR(nf)); 1184*ce502f81SChuck Lever nf = NULL; 1185*ce502f81SChuck Lever status = nfserr_jukebox; 1186*ce502f81SChuck Lever goto out_status; 118765294c1fSJeff Layton } 118865294c1fSJeff Layton 1189fb70bf12SChuck Lever /** 1190fb70bf12SChuck Lever * nfsd_file_acquire - Get a struct nfsd_file with an open file 1191fb70bf12SChuck Lever * @rqstp: the RPC transaction being executed 1192fb70bf12SChuck Lever * @fhp: the NFS filehandle of the file to be opened 1193fb70bf12SChuck Lever * @may_flags: NFSD_MAY_ settings for the file 1194fb70bf12SChuck Lever * @pnf: OUT: new or found "struct nfsd_file" object 1195fb70bf12SChuck Lever * 1196fb70bf12SChuck Lever * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in 1197fb70bf12SChuck Lever * network byte order is returned. 1198fb70bf12SChuck Lever */ 1199fb70bf12SChuck Lever __be32 1200fb70bf12SChuck Lever nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, 1201fb70bf12SChuck Lever unsigned int may_flags, struct nfsd_file **pnf) 1202fb70bf12SChuck Lever { 1203fb70bf12SChuck Lever return nfsd_do_file_acquire(rqstp, fhp, may_flags, pnf, true); 1204fb70bf12SChuck Lever } 1205fb70bf12SChuck Lever 1206fb70bf12SChuck Lever /** 1207fb70bf12SChuck Lever * nfsd_file_create - Get a struct nfsd_file, do not open 1208fb70bf12SChuck Lever * @rqstp: the RPC transaction being executed 1209fb70bf12SChuck Lever * @fhp: the NFS filehandle of the file just created 1210fb70bf12SChuck Lever * @may_flags: NFSD_MAY_ settings for the file 1211fb70bf12SChuck Lever * @pnf: OUT: new or found "struct nfsd_file" object 1212fb70bf12SChuck Lever * 1213fb70bf12SChuck Lever * Returns nfs_ok and sets @pnf on success; otherwise an nfsstat in 1214fb70bf12SChuck Lever * network byte order is returned. 1215fb70bf12SChuck Lever */ 1216fb70bf12SChuck Lever __be32 1217fb70bf12SChuck Lever nfsd_file_create(struct svc_rqst *rqstp, struct svc_fh *fhp, 1218fb70bf12SChuck Lever unsigned int may_flags, struct nfsd_file **pnf) 1219fb70bf12SChuck Lever { 1220fb70bf12SChuck Lever return nfsd_do_file_acquire(rqstp, fhp, may_flags, pnf, false); 1221fb70bf12SChuck Lever } 1222fb70bf12SChuck Lever 122365294c1fSJeff Layton /* 122465294c1fSJeff Layton * Note that fields may be added, removed or reordered in the future. Programs 122565294c1fSJeff Layton * scraping this file for info should test the labels to ensure they're 122665294c1fSJeff Layton * getting the correct field. 122765294c1fSJeff Layton */ 122865294c1fSJeff Layton static int nfsd_file_cache_stats_show(struct seq_file *m, void *v) 122965294c1fSJeff Layton { 1230df2aff52SChuck Lever unsigned long releases = 0, pages_flushed = 0, evictions = 0; 1231df2aff52SChuck Lever unsigned long hits = 0, acquisitions = 0; 1232*ce502f81SChuck Lever unsigned int i, count = 0, buckets = 0; 1233904940e9SChuck Lever unsigned long lru = 0, total_age = 0; 123465294c1fSJeff Layton 1235*ce502f81SChuck Lever /* Serialize with server shutdown */ 123665294c1fSJeff Layton mutex_lock(&nfsd_mutex); 1237c7b824c3SChuck Lever if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) { 1238*ce502f81SChuck Lever struct bucket_table *tbl; 1239*ce502f81SChuck Lever struct rhashtable *ht; 1240*ce502f81SChuck Lever 12410fd244c1SChuck Lever lru = list_lru_count(&nfsd_file_lru); 1242*ce502f81SChuck Lever 1243*ce502f81SChuck Lever rcu_read_lock(); 1244*ce502f81SChuck Lever ht = &nfsd_file_rhash_tbl; 1245*ce502f81SChuck Lever count = atomic_read(&ht->nelems); 1246*ce502f81SChuck Lever tbl = rht_dereference_rcu(ht->tbl, ht); 1247*ce502f81SChuck Lever buckets = tbl->size; 1248*ce502f81SChuck Lever rcu_read_unlock(); 124965294c1fSJeff Layton } 125065294c1fSJeff Layton mutex_unlock(&nfsd_mutex); 125165294c1fSJeff Layton 125229d4bdbbSChuck Lever for_each_possible_cpu(i) { 125365294c1fSJeff Layton hits += per_cpu(nfsd_file_cache_hits, i); 125429d4bdbbSChuck Lever acquisitions += per_cpu(nfsd_file_acquisitions, i); 1255d6329327SChuck Lever releases += per_cpu(nfsd_file_releases, i); 1256904940e9SChuck Lever total_age += per_cpu(nfsd_file_total_age, i); 125794660cc1SChuck Lever evictions += per_cpu(nfsd_file_evictions, i); 1258df2aff52SChuck Lever pages_flushed += per_cpu(nfsd_file_pages_flushed, i); 125929d4bdbbSChuck Lever } 126065294c1fSJeff Layton 126165294c1fSJeff Layton seq_printf(m, "total entries: %u\n", count); 1262*ce502f81SChuck Lever seq_printf(m, "hash buckets: %u\n", buckets); 12630fd244c1SChuck Lever seq_printf(m, "lru entries: %lu\n", lru); 126465294c1fSJeff Layton seq_printf(m, "cache hits: %lu\n", hits); 126529d4bdbbSChuck Lever seq_printf(m, "acquisitions: %lu\n", acquisitions); 1266d6329327SChuck Lever seq_printf(m, "releases: %lu\n", releases); 126794660cc1SChuck Lever seq_printf(m, "evictions: %lu\n", evictions); 1268904940e9SChuck Lever if (releases) 1269904940e9SChuck Lever seq_printf(m, "mean age (ms): %ld\n", total_age / releases); 1270904940e9SChuck Lever else 1271904940e9SChuck Lever seq_printf(m, "mean age (ms): -\n"); 1272df2aff52SChuck Lever seq_printf(m, "pages flushed: %lu\n", pages_flushed); 127365294c1fSJeff Layton return 0; 127465294c1fSJeff Layton } 127565294c1fSJeff Layton 127665294c1fSJeff Layton int nfsd_file_cache_stats_open(struct inode *inode, struct file *file) 127765294c1fSJeff Layton { 127865294c1fSJeff Layton return single_open(file, nfsd_file_cache_stats_show, NULL); 127965294c1fSJeff Layton } 1280