Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Open file cache. |
| 3 | * |
| 4 | * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/hash.h> |
| 8 | #include <linux/slab.h> |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 9 | #include <linux/file.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/list_lru.h> |
| 12 | #include <linux/fsnotify_backend.h> |
| 13 | #include <linux/fsnotify.h> |
| 14 | #include <linux/seq_file.h> |
| 15 | |
| 16 | #include "vfs.h" |
| 17 | #include "nfsd.h" |
| 18 | #include "nfsfh.h" |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 19 | #include "netns.h" |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 20 | #include "filecache.h" |
| 21 | #include "trace.h" |
| 22 | |
| 23 | #define NFSDDBG_FACILITY NFSDDBG_FH |
| 24 | |
| 25 | /* FIXME: dynamically size this for the machine somehow? */ |
| 26 | #define NFSD_FILE_HASH_BITS 12 |
| 27 | #define NFSD_FILE_HASH_SIZE (1 << NFSD_FILE_HASH_BITS) |
| 28 | #define NFSD_LAUNDRETTE_DELAY (2 * HZ) |
| 29 | |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 30 | #define NFSD_FILE_SHUTDOWN (1) |
| 31 | #define NFSD_FILE_LRU_THRESHOLD (4096UL) |
| 32 | #define NFSD_FILE_LRU_LIMIT (NFSD_FILE_LRU_THRESHOLD << 2) |
| 33 | |
| 34 | /* We only care about NFSD_MAY_READ/WRITE for this cache */ |
| 35 | #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE) |
| 36 | |
| 37 | struct nfsd_fcache_bucket { |
| 38 | struct hlist_head nfb_head; |
| 39 | spinlock_t nfb_lock; |
| 40 | unsigned int nfb_count; |
| 41 | unsigned int nfb_maxcount; |
| 42 | }; |
| 43 | |
| 44 | static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits); |
| 45 | |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 46 | struct nfsd_fcache_disposal { |
| 47 | struct list_head list; |
| 48 | struct work_struct work; |
| 49 | struct net *net; |
| 50 | spinlock_t lock; |
| 51 | struct list_head freeme; |
| 52 | struct rcu_head rcu; |
| 53 | }; |
| 54 | |
| 55 | struct workqueue_struct *nfsd_filecache_wq __read_mostly; |
| 56 | |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 57 | static struct kmem_cache *nfsd_file_slab; |
| 58 | static struct kmem_cache *nfsd_file_mark_slab; |
| 59 | static struct nfsd_fcache_bucket *nfsd_file_hashtbl; |
| 60 | static struct list_lru nfsd_file_lru; |
| 61 | static long nfsd_file_lru_flags; |
| 62 | static struct fsnotify_group *nfsd_file_fsnotify_group; |
| 63 | static atomic_long_t nfsd_filecache_count; |
| 64 | static struct delayed_work nfsd_filecache_laundrette; |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(laundrette_lock); |
| 66 | static LIST_HEAD(laundrettes); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 67 | |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 68 | static void nfsd_file_gc(void); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 69 | |
| 70 | static void |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 71 | nfsd_file_schedule_laundrette(void) |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 72 | { |
| 73 | long count = atomic_long_read(&nfsd_filecache_count); |
| 74 | |
| 75 | if (count == 0 || test_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags)) |
| 76 | return; |
| 77 | |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 78 | queue_delayed_work(system_wq, &nfsd_filecache_laundrette, |
| 79 | NFSD_LAUNDRETTE_DELAY); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void |
| 83 | nfsd_file_slab_free(struct rcu_head *rcu) |
| 84 | { |
| 85 | struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu); |
| 86 | |
| 87 | put_cred(nf->nf_cred); |
| 88 | kmem_cache_free(nfsd_file_slab, nf); |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | nfsd_file_mark_free(struct fsnotify_mark *mark) |
| 93 | { |
| 94 | struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark, |
| 95 | nfm_mark); |
| 96 | |
| 97 | kmem_cache_free(nfsd_file_mark_slab, nfm); |
| 98 | } |
| 99 | |
| 100 | static struct nfsd_file_mark * |
| 101 | nfsd_file_mark_get(struct nfsd_file_mark *nfm) |
| 102 | { |
| 103 | if (!atomic_inc_not_zero(&nfm->nfm_ref)) |
| 104 | return NULL; |
| 105 | return nfm; |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | nfsd_file_mark_put(struct nfsd_file_mark *nfm) |
| 110 | { |
| 111 | if (atomic_dec_and_test(&nfm->nfm_ref)) { |
| 112 | |
| 113 | fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group); |
| 114 | fsnotify_put_mark(&nfm->nfm_mark); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static struct nfsd_file_mark * |
| 119 | nfsd_file_mark_find_or_create(struct nfsd_file *nf) |
| 120 | { |
| 121 | int err; |
| 122 | struct fsnotify_mark *mark; |
| 123 | struct nfsd_file_mark *nfm = NULL, *new; |
| 124 | struct inode *inode = nf->nf_inode; |
| 125 | |
| 126 | do { |
| 127 | mutex_lock(&nfsd_file_fsnotify_group->mark_mutex); |
| 128 | mark = fsnotify_find_mark(&inode->i_fsnotify_marks, |
| 129 | nfsd_file_fsnotify_group); |
| 130 | if (mark) { |
| 131 | nfm = nfsd_file_mark_get(container_of(mark, |
| 132 | struct nfsd_file_mark, |
| 133 | nfm_mark)); |
| 134 | mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex); |
| 135 | fsnotify_put_mark(mark); |
| 136 | if (likely(nfm)) |
| 137 | break; |
| 138 | } else |
| 139 | mutex_unlock(&nfsd_file_fsnotify_group->mark_mutex); |
| 140 | |
| 141 | /* allocate a new nfm */ |
| 142 | new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL); |
| 143 | if (!new) |
| 144 | return NULL; |
| 145 | fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group); |
| 146 | new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF; |
| 147 | atomic_set(&new->nfm_ref, 1); |
| 148 | |
| 149 | err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0); |
| 150 | |
| 151 | /* |
| 152 | * If the add was successful, then return the object. |
| 153 | * Otherwise, we need to put the reference we hold on the |
| 154 | * nfm_mark. The fsnotify code will take a reference and put |
| 155 | * it on failure, so we can't just free it directly. It's also |
| 156 | * not safe to call fsnotify_destroy_mark on it as the |
| 157 | * mark->group will be NULL. Thus, we can't let the nfm_ref |
| 158 | * counter drive the destruction at this point. |
| 159 | */ |
| 160 | if (likely(!err)) |
| 161 | nfm = new; |
| 162 | else |
| 163 | fsnotify_put_mark(&new->nfm_mark); |
| 164 | } while (unlikely(err == -EEXIST)); |
| 165 | |
| 166 | return nfm; |
| 167 | } |
| 168 | |
| 169 | static struct nfsd_file * |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 170 | nfsd_file_alloc(struct inode *inode, unsigned int may, unsigned int hashval, |
| 171 | struct net *net) |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 172 | { |
| 173 | struct nfsd_file *nf; |
| 174 | |
| 175 | nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL); |
| 176 | if (nf) { |
| 177 | INIT_HLIST_NODE(&nf->nf_node); |
| 178 | INIT_LIST_HEAD(&nf->nf_lru); |
| 179 | nf->nf_file = NULL; |
| 180 | nf->nf_cred = get_current_cred(); |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 181 | nf->nf_net = net; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 182 | nf->nf_flags = 0; |
| 183 | nf->nf_inode = inode; |
| 184 | nf->nf_hashval = hashval; |
| 185 | atomic_set(&nf->nf_ref, 1); |
| 186 | nf->nf_may = may & NFSD_FILE_MAY_MASK; |
| 187 | if (may & NFSD_MAY_NOT_BREAK_LEASE) { |
| 188 | if (may & NFSD_MAY_WRITE) |
| 189 | __set_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags); |
| 190 | if (may & NFSD_MAY_READ) |
| 191 | __set_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags); |
| 192 | } |
| 193 | nf->nf_mark = NULL; |
| 194 | trace_nfsd_file_alloc(nf); |
| 195 | } |
| 196 | return nf; |
| 197 | } |
| 198 | |
| 199 | static bool |
| 200 | nfsd_file_free(struct nfsd_file *nf) |
| 201 | { |
| 202 | bool flush = false; |
| 203 | |
| 204 | trace_nfsd_file_put_final(nf); |
| 205 | if (nf->nf_mark) |
| 206 | nfsd_file_mark_put(nf->nf_mark); |
| 207 | if (nf->nf_file) { |
| 208 | get_file(nf->nf_file); |
| 209 | filp_close(nf->nf_file, NULL); |
| 210 | fput(nf->nf_file); |
| 211 | flush = true; |
| 212 | } |
| 213 | call_rcu(&nf->nf_rcu, nfsd_file_slab_free); |
| 214 | return flush; |
| 215 | } |
| 216 | |
Trond Myklebust | 055b24a | 2019-09-02 13:02:57 -0400 | [diff] [blame] | 217 | static bool |
| 218 | nfsd_file_check_writeback(struct nfsd_file *nf) |
| 219 | { |
| 220 | struct file *file = nf->nf_file; |
| 221 | struct address_space *mapping; |
| 222 | |
| 223 | if (!file || !(file->f_mode & FMODE_WRITE)) |
| 224 | return false; |
| 225 | mapping = file->f_mapping; |
| 226 | return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) || |
| 227 | mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK); |
| 228 | } |
| 229 | |
| 230 | static int |
| 231 | nfsd_file_check_write_error(struct nfsd_file *nf) |
| 232 | { |
| 233 | struct file *file = nf->nf_file; |
| 234 | |
| 235 | if (!file || !(file->f_mode & FMODE_WRITE)) |
| 236 | return 0; |
| 237 | return filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err)); |
| 238 | } |
| 239 | |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 240 | static void |
| 241 | nfsd_file_do_unhash(struct nfsd_file *nf) |
| 242 | { |
| 243 | lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); |
| 244 | |
| 245 | trace_nfsd_file_unhash(nf); |
| 246 | |
Trond Myklebust | 055b24a | 2019-09-02 13:02:57 -0400 | [diff] [blame] | 247 | if (nfsd_file_check_write_error(nf)) |
| 248 | nfsd_reset_boot_verifier(net_generic(nf->nf_net, nfsd_net_id)); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 249 | --nfsd_file_hashtbl[nf->nf_hashval].nfb_count; |
| 250 | hlist_del_rcu(&nf->nf_node); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 251 | atomic_long_dec(&nfsd_filecache_count); |
| 252 | } |
| 253 | |
| 254 | static bool |
| 255 | nfsd_file_unhash(struct nfsd_file *nf) |
| 256 | { |
| 257 | if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { |
| 258 | nfsd_file_do_unhash(nf); |
Trond Myklebust | 36ebbdb | 2020-01-06 13:18:04 -0500 | [diff] [blame] | 259 | if (!list_empty(&nf->nf_lru)) |
| 260 | list_lru_del(&nfsd_file_lru, &nf->nf_lru); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 261 | return true; |
| 262 | } |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Return true if the file was unhashed. |
| 268 | */ |
| 269 | static bool |
| 270 | nfsd_file_unhash_and_release_locked(struct nfsd_file *nf, struct list_head *dispose) |
| 271 | { |
| 272 | lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); |
| 273 | |
| 274 | trace_nfsd_file_unhash_and_release_locked(nf); |
| 275 | if (!nfsd_file_unhash(nf)) |
| 276 | return false; |
| 277 | /* keep final reference for nfsd_file_lru_dispose */ |
| 278 | if (atomic_add_unless(&nf->nf_ref, -1, 1)) |
| 279 | return true; |
| 280 | |
| 281 | list_add(&nf->nf_lru, dispose); |
| 282 | return true; |
| 283 | } |
| 284 | |
Trond Myklebust | b666930 | 2020-01-06 13:18:08 -0500 | [diff] [blame^] | 285 | static void |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 286 | nfsd_file_put_noref(struct nfsd_file *nf) |
| 287 | { |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 288 | trace_nfsd_file_put(nf); |
| 289 | |
Trond Myklebust | b666930 | 2020-01-06 13:18:08 -0500 | [diff] [blame^] | 290 | if (atomic_dec_and_test(&nf->nf_ref)) { |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 291 | WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags)); |
| 292 | nfsd_file_free(nf); |
| 293 | } |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void |
| 297 | nfsd_file_put(struct nfsd_file *nf) |
| 298 | { |
Trond Myklebust | b666930 | 2020-01-06 13:18:08 -0500 | [diff] [blame^] | 299 | bool is_hashed; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 300 | |
| 301 | set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags); |
Trond Myklebust | b666930 | 2020-01-06 13:18:08 -0500 | [diff] [blame^] | 302 | if (atomic_read(&nf->nf_ref) > 2 || !nf->nf_file) { |
| 303 | nfsd_file_put_noref(nf); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | filemap_flush(nf->nf_file->f_mapping); |
| 308 | is_hashed = test_bit(NFSD_FILE_HASHED, &nf->nf_flags) != 0; |
| 309 | nfsd_file_put_noref(nf); |
| 310 | if (is_hashed) |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 311 | nfsd_file_schedule_laundrette(); |
| 312 | if (atomic_long_read(&nfsd_filecache_count) >= NFSD_FILE_LRU_LIMIT) |
| 313 | nfsd_file_gc(); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | struct nfsd_file * |
| 317 | nfsd_file_get(struct nfsd_file *nf) |
| 318 | { |
| 319 | if (likely(atomic_inc_not_zero(&nf->nf_ref))) |
| 320 | return nf; |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | static void |
| 325 | nfsd_file_dispose_list(struct list_head *dispose) |
| 326 | { |
| 327 | struct nfsd_file *nf; |
| 328 | |
| 329 | while(!list_empty(dispose)) { |
| 330 | nf = list_first_entry(dispose, struct nfsd_file, nf_lru); |
| 331 | list_del(&nf->nf_lru); |
| 332 | nfsd_file_put_noref(nf); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | nfsd_file_dispose_list_sync(struct list_head *dispose) |
| 338 | { |
| 339 | bool flush = false; |
| 340 | struct nfsd_file *nf; |
| 341 | |
| 342 | while(!list_empty(dispose)) { |
| 343 | nf = list_first_entry(dispose, struct nfsd_file, nf_lru); |
| 344 | list_del(&nf->nf_lru); |
| 345 | if (!atomic_dec_and_test(&nf->nf_ref)) |
| 346 | continue; |
| 347 | if (nfsd_file_free(nf)) |
| 348 | flush = true; |
| 349 | } |
| 350 | if (flush) |
| 351 | flush_delayed_fput(); |
| 352 | } |
| 353 | |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 354 | static void |
| 355 | nfsd_file_list_remove_disposal(struct list_head *dst, |
| 356 | struct nfsd_fcache_disposal *l) |
| 357 | { |
| 358 | spin_lock(&l->lock); |
| 359 | list_splice_init(&l->freeme, dst); |
| 360 | spin_unlock(&l->lock); |
| 361 | } |
| 362 | |
| 363 | static void |
| 364 | nfsd_file_list_add_disposal(struct list_head *files, struct net *net) |
| 365 | { |
| 366 | struct nfsd_fcache_disposal *l; |
| 367 | |
| 368 | rcu_read_lock(); |
| 369 | list_for_each_entry_rcu(l, &laundrettes, list) { |
| 370 | if (l->net == net) { |
| 371 | spin_lock(&l->lock); |
| 372 | list_splice_tail_init(files, &l->freeme); |
| 373 | spin_unlock(&l->lock); |
| 374 | queue_work(nfsd_filecache_wq, &l->work); |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | rcu_read_unlock(); |
| 379 | } |
| 380 | |
| 381 | static void |
| 382 | nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src, |
| 383 | struct net *net) |
| 384 | { |
| 385 | struct nfsd_file *nf, *tmp; |
| 386 | |
| 387 | list_for_each_entry_safe(nf, tmp, src, nf_lru) { |
| 388 | if (nf->nf_net == net) |
| 389 | list_move_tail(&nf->nf_lru, dst); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | static void |
| 394 | nfsd_file_dispose_list_delayed(struct list_head *dispose) |
| 395 | { |
| 396 | LIST_HEAD(list); |
| 397 | struct nfsd_file *nf; |
| 398 | |
| 399 | while(!list_empty(dispose)) { |
| 400 | nf = list_first_entry(dispose, struct nfsd_file, nf_lru); |
| 401 | nfsd_file_list_add_pernet(&list, dispose, nf->nf_net); |
| 402 | nfsd_file_list_add_disposal(&list, nf->nf_net); |
| 403 | } |
| 404 | } |
| 405 | |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 406 | /* |
| 407 | * Note this can deadlock with nfsd_file_cache_purge. |
| 408 | */ |
| 409 | static enum lru_status |
| 410 | nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru, |
| 411 | spinlock_t *lock, void *arg) |
| 412 | __releases(lock) |
| 413 | __acquires(lock) |
| 414 | { |
| 415 | struct list_head *head = arg; |
| 416 | struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru); |
| 417 | |
| 418 | /* |
| 419 | * Do a lockless refcount check. The hashtable holds one reference, so |
| 420 | * we look to see if anything else has a reference, or if any have |
| 421 | * been put since the shrinker last ran. Those don't get unhashed and |
| 422 | * released. |
| 423 | * |
| 424 | * Note that in the put path, we set the flag and then decrement the |
| 425 | * counter. Here we check the counter and then test and clear the flag. |
| 426 | * That order is deliberate to ensure that we can do this locklessly. |
| 427 | */ |
| 428 | if (atomic_read(&nf->nf_ref) > 1) |
| 429 | goto out_skip; |
Trond Myklebust | 055b24a | 2019-09-02 13:02:57 -0400 | [diff] [blame] | 430 | |
| 431 | /* |
| 432 | * Don't throw out files that are still undergoing I/O or |
| 433 | * that have uncleared errors pending. |
| 434 | */ |
| 435 | if (nfsd_file_check_writeback(nf)) |
| 436 | goto out_skip; |
| 437 | |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 438 | if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) |
Trond Myklebust | bd6e1ce | 2020-01-06 13:18:06 -0500 | [diff] [blame] | 439 | goto out_skip; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 440 | |
| 441 | if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) |
| 442 | goto out_skip; |
| 443 | |
| 444 | list_lru_isolate_move(lru, &nf->nf_lru, head); |
| 445 | return LRU_REMOVED; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 446 | out_skip: |
| 447 | return LRU_SKIP; |
| 448 | } |
| 449 | |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 450 | static unsigned long |
| 451 | nfsd_file_lru_walk_list(struct shrink_control *sc) |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 452 | { |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 453 | LIST_HEAD(head); |
Trond Myklebust | 36ebbdb | 2020-01-06 13:18:04 -0500 | [diff] [blame] | 454 | struct nfsd_file *nf; |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 455 | unsigned long ret; |
Trond Myklebust | 36ebbdb | 2020-01-06 13:18:04 -0500 | [diff] [blame] | 456 | |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 457 | if (sc) |
| 458 | ret = list_lru_shrink_walk(&nfsd_file_lru, sc, |
| 459 | nfsd_file_lru_cb, &head); |
| 460 | else |
| 461 | ret = list_lru_walk(&nfsd_file_lru, |
| 462 | nfsd_file_lru_cb, |
| 463 | &head, LONG_MAX); |
| 464 | list_for_each_entry(nf, &head, nf_lru) { |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 465 | spin_lock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); |
| 466 | nfsd_file_do_unhash(nf); |
| 467 | spin_unlock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 468 | } |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 469 | nfsd_file_dispose_list_delayed(&head); |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | static void |
| 474 | nfsd_file_gc(void) |
| 475 | { |
| 476 | nfsd_file_lru_walk_list(NULL); |
| 477 | } |
| 478 | |
| 479 | static void |
| 480 | nfsd_file_gc_worker(struct work_struct *work) |
| 481 | { |
| 482 | nfsd_file_gc(); |
| 483 | nfsd_file_schedule_laundrette(); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static unsigned long |
| 487 | nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc) |
| 488 | { |
| 489 | return list_lru_count(&nfsd_file_lru); |
| 490 | } |
| 491 | |
| 492 | static unsigned long |
| 493 | nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc) |
| 494 | { |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 495 | return nfsd_file_lru_walk_list(sc); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static struct shrinker nfsd_file_shrinker = { |
| 499 | .scan_objects = nfsd_file_lru_scan, |
| 500 | .count_objects = nfsd_file_lru_count, |
| 501 | .seeks = 1, |
| 502 | }; |
| 503 | |
| 504 | static void |
| 505 | __nfsd_file_close_inode(struct inode *inode, unsigned int hashval, |
| 506 | struct list_head *dispose) |
| 507 | { |
| 508 | struct nfsd_file *nf; |
| 509 | struct hlist_node *tmp; |
| 510 | |
| 511 | spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock); |
| 512 | hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) { |
| 513 | if (inode == nf->nf_inode) |
| 514 | nfsd_file_unhash_and_release_locked(nf, dispose); |
| 515 | } |
| 516 | spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file |
| 521 | * @inode: inode of the file to attempt to remove |
| 522 | * |
| 523 | * Walk the whole hash bucket, looking for any files that correspond to "inode". |
| 524 | * If any do, then unhash them and put the hashtable reference to them and |
| 525 | * destroy any that had their last reference put. Also ensure that any of the |
| 526 | * fputs also have their final __fput done as well. |
| 527 | */ |
| 528 | void |
| 529 | nfsd_file_close_inode_sync(struct inode *inode) |
| 530 | { |
| 531 | unsigned int hashval = (unsigned int)hash_long(inode->i_ino, |
| 532 | NFSD_FILE_HASH_BITS); |
| 533 | LIST_HEAD(dispose); |
| 534 | |
| 535 | __nfsd_file_close_inode(inode, hashval, &dispose); |
| 536 | trace_nfsd_file_close_inode_sync(inode, hashval, !list_empty(&dispose)); |
| 537 | nfsd_file_dispose_list_sync(&dispose); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file |
| 542 | * @inode: inode of the file to attempt to remove |
| 543 | * |
| 544 | * Walk the whole hash bucket, looking for any files that correspond to "inode". |
| 545 | * If any do, then unhash them and put the hashtable reference to them and |
| 546 | * destroy any that had their last reference put. |
| 547 | */ |
| 548 | static void |
| 549 | nfsd_file_close_inode(struct inode *inode) |
| 550 | { |
| 551 | unsigned int hashval = (unsigned int)hash_long(inode->i_ino, |
| 552 | NFSD_FILE_HASH_BITS); |
| 553 | LIST_HEAD(dispose); |
| 554 | |
| 555 | __nfsd_file_close_inode(inode, hashval, &dispose); |
| 556 | trace_nfsd_file_close_inode(inode, hashval, !list_empty(&dispose)); |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 557 | nfsd_file_dispose_list_delayed(&dispose); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** |
| 561 | * nfsd_file_delayed_close - close unused nfsd_files |
| 562 | * @work: dummy |
| 563 | * |
| 564 | * Walk the LRU list and close any entries that have not been used since |
| 565 | * the last scan. |
| 566 | * |
| 567 | * Note this can deadlock with nfsd_file_cache_purge. |
| 568 | */ |
| 569 | static void |
| 570 | nfsd_file_delayed_close(struct work_struct *work) |
| 571 | { |
| 572 | LIST_HEAD(head); |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 573 | struct nfsd_fcache_disposal *l = container_of(work, |
| 574 | struct nfsd_fcache_disposal, work); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 575 | |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 576 | nfsd_file_list_remove_disposal(&head, l); |
| 577 | nfsd_file_dispose_list(&head); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static int |
| 581 | nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg, |
| 582 | void *data) |
| 583 | { |
| 584 | struct file_lock *fl = data; |
| 585 | |
| 586 | /* Only close files for F_SETLEASE leases */ |
| 587 | if (fl->fl_flags & FL_LEASE) |
| 588 | nfsd_file_close_inode_sync(file_inode(fl->fl_file)); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static struct notifier_block nfsd_file_lease_notifier = { |
| 593 | .notifier_call = nfsd_file_lease_notifier_call, |
| 594 | }; |
| 595 | |
| 596 | static int |
| 597 | nfsd_file_fsnotify_handle_event(struct fsnotify_group *group, |
| 598 | struct inode *inode, |
| 599 | u32 mask, const void *data, int data_type, |
| 600 | const struct qstr *file_name, u32 cookie, |
| 601 | struct fsnotify_iter_info *iter_info) |
| 602 | { |
| 603 | trace_nfsd_file_fsnotify_handle_event(inode, mask); |
| 604 | |
| 605 | /* Should be no marks on non-regular files */ |
| 606 | if (!S_ISREG(inode->i_mode)) { |
| 607 | WARN_ON_ONCE(1); |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | /* don't close files if this was not the last link */ |
| 612 | if (mask & FS_ATTRIB) { |
| 613 | if (inode->i_nlink) |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | nfsd_file_close_inode(inode); |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | static const struct fsnotify_ops nfsd_file_fsnotify_ops = { |
| 623 | .handle_event = nfsd_file_fsnotify_handle_event, |
| 624 | .free_mark = nfsd_file_mark_free, |
| 625 | }; |
| 626 | |
| 627 | int |
| 628 | nfsd_file_cache_init(void) |
| 629 | { |
| 630 | int ret = -ENOMEM; |
| 631 | unsigned int i; |
| 632 | |
| 633 | clear_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags); |
| 634 | |
| 635 | if (nfsd_file_hashtbl) |
| 636 | return 0; |
| 637 | |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 638 | nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0); |
| 639 | if (!nfsd_filecache_wq) |
| 640 | goto out; |
| 641 | |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 642 | nfsd_file_hashtbl = kcalloc(NFSD_FILE_HASH_SIZE, |
| 643 | sizeof(*nfsd_file_hashtbl), GFP_KERNEL); |
| 644 | if (!nfsd_file_hashtbl) { |
| 645 | pr_err("nfsd: unable to allocate nfsd_file_hashtbl\n"); |
| 646 | goto out_err; |
| 647 | } |
| 648 | |
| 649 | nfsd_file_slab = kmem_cache_create("nfsd_file", |
| 650 | sizeof(struct nfsd_file), 0, 0, NULL); |
| 651 | if (!nfsd_file_slab) { |
| 652 | pr_err("nfsd: unable to create nfsd_file_slab\n"); |
| 653 | goto out_err; |
| 654 | } |
| 655 | |
| 656 | nfsd_file_mark_slab = kmem_cache_create("nfsd_file_mark", |
| 657 | sizeof(struct nfsd_file_mark), 0, 0, NULL); |
| 658 | if (!nfsd_file_mark_slab) { |
| 659 | pr_err("nfsd: unable to create nfsd_file_mark_slab\n"); |
| 660 | goto out_err; |
| 661 | } |
| 662 | |
| 663 | |
| 664 | ret = list_lru_init(&nfsd_file_lru); |
| 665 | if (ret) { |
| 666 | pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret); |
| 667 | goto out_err; |
| 668 | } |
| 669 | |
| 670 | ret = register_shrinker(&nfsd_file_shrinker); |
| 671 | if (ret) { |
| 672 | pr_err("nfsd: failed to register nfsd_file_shrinker: %d\n", ret); |
| 673 | goto out_lru; |
| 674 | } |
| 675 | |
| 676 | ret = lease_register_notifier(&nfsd_file_lease_notifier); |
| 677 | if (ret) { |
| 678 | pr_err("nfsd: unable to register lease notifier: %d\n", ret); |
| 679 | goto out_shrinker; |
| 680 | } |
| 681 | |
| 682 | nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops); |
| 683 | if (IS_ERR(nfsd_file_fsnotify_group)) { |
| 684 | pr_err("nfsd: unable to create fsnotify group: %ld\n", |
| 685 | PTR_ERR(nfsd_file_fsnotify_group)); |
| 686 | nfsd_file_fsnotify_group = NULL; |
| 687 | goto out_notifier; |
| 688 | } |
| 689 | |
| 690 | for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) { |
| 691 | INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head); |
| 692 | spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock); |
| 693 | } |
| 694 | |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 695 | INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 696 | out: |
| 697 | return ret; |
| 698 | out_notifier: |
| 699 | lease_unregister_notifier(&nfsd_file_lease_notifier); |
| 700 | out_shrinker: |
| 701 | unregister_shrinker(&nfsd_file_shrinker); |
| 702 | out_lru: |
| 703 | list_lru_destroy(&nfsd_file_lru); |
| 704 | out_err: |
| 705 | kmem_cache_destroy(nfsd_file_slab); |
| 706 | nfsd_file_slab = NULL; |
| 707 | kmem_cache_destroy(nfsd_file_mark_slab); |
| 708 | nfsd_file_mark_slab = NULL; |
| 709 | kfree(nfsd_file_hashtbl); |
| 710 | nfsd_file_hashtbl = NULL; |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 711 | destroy_workqueue(nfsd_filecache_wq); |
| 712 | nfsd_filecache_wq = NULL; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 713 | goto out; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Note this can deadlock with nfsd_file_lru_cb. |
| 718 | */ |
| 719 | void |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 720 | nfsd_file_cache_purge(struct net *net) |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 721 | { |
| 722 | unsigned int i; |
| 723 | struct nfsd_file *nf; |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 724 | struct hlist_node *next; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 725 | LIST_HEAD(dispose); |
| 726 | bool del; |
| 727 | |
| 728 | if (!nfsd_file_hashtbl) |
| 729 | return; |
| 730 | |
| 731 | for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) { |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 732 | struct nfsd_fcache_bucket *nfb = &nfsd_file_hashtbl[i]; |
| 733 | |
| 734 | spin_lock(&nfb->nfb_lock); |
| 735 | hlist_for_each_entry_safe(nf, next, &nfb->nfb_head, nf_node) { |
| 736 | if (net && nf->nf_net != net) |
| 737 | continue; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 738 | del = nfsd_file_unhash_and_release_locked(nf, &dispose); |
| 739 | |
| 740 | /* |
| 741 | * Deadlock detected! Something marked this entry as |
| 742 | * unhased, but hasn't removed it from the hash list. |
| 743 | */ |
| 744 | WARN_ON_ONCE(!del); |
| 745 | } |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 746 | spin_unlock(&nfb->nfb_lock); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 747 | nfsd_file_dispose_list(&dispose); |
| 748 | } |
| 749 | } |
| 750 | |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 751 | static struct nfsd_fcache_disposal * |
| 752 | nfsd_alloc_fcache_disposal(struct net *net) |
| 753 | { |
| 754 | struct nfsd_fcache_disposal *l; |
| 755 | |
| 756 | l = kmalloc(sizeof(*l), GFP_KERNEL); |
| 757 | if (!l) |
| 758 | return NULL; |
| 759 | INIT_WORK(&l->work, nfsd_file_delayed_close); |
| 760 | l->net = net; |
| 761 | spin_lock_init(&l->lock); |
| 762 | INIT_LIST_HEAD(&l->freeme); |
| 763 | return l; |
| 764 | } |
| 765 | |
| 766 | static void |
| 767 | nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l) |
| 768 | { |
| 769 | rcu_assign_pointer(l->net, NULL); |
| 770 | cancel_work_sync(&l->work); |
| 771 | nfsd_file_dispose_list(&l->freeme); |
| 772 | kfree_rcu(l, rcu); |
| 773 | } |
| 774 | |
| 775 | static void |
| 776 | nfsd_add_fcache_disposal(struct nfsd_fcache_disposal *l) |
| 777 | { |
| 778 | spin_lock(&laundrette_lock); |
| 779 | list_add_tail_rcu(&l->list, &laundrettes); |
| 780 | spin_unlock(&laundrette_lock); |
| 781 | } |
| 782 | |
| 783 | static void |
| 784 | nfsd_del_fcache_disposal(struct nfsd_fcache_disposal *l) |
| 785 | { |
| 786 | spin_lock(&laundrette_lock); |
| 787 | list_del_rcu(&l->list); |
| 788 | spin_unlock(&laundrette_lock); |
| 789 | } |
| 790 | |
| 791 | static int |
| 792 | nfsd_alloc_fcache_disposal_net(struct net *net) |
| 793 | { |
| 794 | struct nfsd_fcache_disposal *l; |
| 795 | |
| 796 | l = nfsd_alloc_fcache_disposal(net); |
| 797 | if (!l) |
| 798 | return -ENOMEM; |
| 799 | nfsd_add_fcache_disposal(l); |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | static void |
| 804 | nfsd_free_fcache_disposal_net(struct net *net) |
| 805 | { |
| 806 | struct nfsd_fcache_disposal *l; |
| 807 | |
| 808 | rcu_read_lock(); |
| 809 | list_for_each_entry_rcu(l, &laundrettes, list) { |
| 810 | if (l->net != net) |
| 811 | continue; |
| 812 | nfsd_del_fcache_disposal(l); |
| 813 | rcu_read_unlock(); |
| 814 | nfsd_free_fcache_disposal(l); |
| 815 | return; |
| 816 | } |
| 817 | rcu_read_unlock(); |
| 818 | } |
| 819 | |
| 820 | int |
| 821 | nfsd_file_cache_start_net(struct net *net) |
| 822 | { |
| 823 | return nfsd_alloc_fcache_disposal_net(net); |
| 824 | } |
| 825 | |
| 826 | void |
| 827 | nfsd_file_cache_shutdown_net(struct net *net) |
| 828 | { |
| 829 | nfsd_file_cache_purge(net); |
| 830 | nfsd_free_fcache_disposal_net(net); |
| 831 | } |
| 832 | |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 833 | void |
| 834 | nfsd_file_cache_shutdown(void) |
| 835 | { |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 836 | set_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags); |
| 837 | |
| 838 | lease_unregister_notifier(&nfsd_file_lease_notifier); |
| 839 | unregister_shrinker(&nfsd_file_shrinker); |
| 840 | /* |
| 841 | * make sure all callers of nfsd_file_lru_cb are done before |
| 842 | * calling nfsd_file_cache_purge |
| 843 | */ |
| 844 | cancel_delayed_work_sync(&nfsd_filecache_laundrette); |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 845 | nfsd_file_cache_purge(NULL); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 846 | list_lru_destroy(&nfsd_file_lru); |
| 847 | rcu_barrier(); |
| 848 | fsnotify_put_group(nfsd_file_fsnotify_group); |
| 849 | nfsd_file_fsnotify_group = NULL; |
| 850 | kmem_cache_destroy(nfsd_file_slab); |
| 851 | nfsd_file_slab = NULL; |
| 852 | fsnotify_wait_marks_destroyed(); |
| 853 | kmem_cache_destroy(nfsd_file_mark_slab); |
| 854 | nfsd_file_mark_slab = NULL; |
| 855 | kfree(nfsd_file_hashtbl); |
| 856 | nfsd_file_hashtbl = NULL; |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 857 | destroy_workqueue(nfsd_filecache_wq); |
| 858 | nfsd_filecache_wq = NULL; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | static bool |
| 862 | nfsd_match_cred(const struct cred *c1, const struct cred *c2) |
| 863 | { |
| 864 | int i; |
| 865 | |
| 866 | if (!uid_eq(c1->fsuid, c2->fsuid)) |
| 867 | return false; |
| 868 | if (!gid_eq(c1->fsgid, c2->fsgid)) |
| 869 | return false; |
| 870 | if (c1->group_info == NULL || c2->group_info == NULL) |
| 871 | return c1->group_info == c2->group_info; |
| 872 | if (c1->group_info->ngroups != c2->group_info->ngroups) |
| 873 | return false; |
| 874 | for (i = 0; i < c1->group_info->ngroups; i++) { |
| 875 | if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i])) |
| 876 | return false; |
| 877 | } |
| 878 | return true; |
| 879 | } |
| 880 | |
| 881 | static struct nfsd_file * |
| 882 | nfsd_file_find_locked(struct inode *inode, unsigned int may_flags, |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 883 | unsigned int hashval, struct net *net) |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 884 | { |
| 885 | struct nfsd_file *nf; |
| 886 | unsigned char need = may_flags & NFSD_FILE_MAY_MASK; |
| 887 | |
| 888 | hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head, |
| 889 | nf_node) { |
| 890 | if ((need & nf->nf_may) != need) |
| 891 | continue; |
| 892 | if (nf->nf_inode != inode) |
| 893 | continue; |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 894 | if (nf->nf_net != net) |
| 895 | continue; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 896 | if (!nfsd_match_cred(nf->nf_cred, current_cred())) |
| 897 | continue; |
| 898 | if (nfsd_file_get(nf) != NULL) |
| 899 | return nf; |
| 900 | } |
| 901 | return NULL; |
| 902 | } |
| 903 | |
| 904 | /** |
| 905 | * nfsd_file_is_cached - are there any cached open files for this fh? |
| 906 | * @inode: inode of the file to check |
| 907 | * |
| 908 | * Scan the hashtable for open files that match this fh. Returns true if there |
| 909 | * are any, and false if not. |
| 910 | */ |
| 911 | bool |
| 912 | nfsd_file_is_cached(struct inode *inode) |
| 913 | { |
| 914 | bool ret = false; |
| 915 | struct nfsd_file *nf; |
| 916 | unsigned int hashval; |
| 917 | |
| 918 | hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS); |
| 919 | |
| 920 | rcu_read_lock(); |
| 921 | hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head, |
| 922 | nf_node) { |
| 923 | if (inode == nf->nf_inode) { |
| 924 | ret = true; |
| 925 | break; |
| 926 | } |
| 927 | } |
| 928 | rcu_read_unlock(); |
| 929 | trace_nfsd_file_is_cached(inode, hashval, (int)ret); |
| 930 | return ret; |
| 931 | } |
| 932 | |
| 933 | __be32 |
| 934 | nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 935 | unsigned int may_flags, struct nfsd_file **pnf) |
| 936 | { |
| 937 | __be32 status; |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 938 | struct net *net = SVC_NET(rqstp); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 939 | struct nfsd_file *nf, *new; |
| 940 | struct inode *inode; |
| 941 | unsigned int hashval; |
Trond Myklebust | 28c7d86 | 2020-01-06 13:18:03 -0500 | [diff] [blame] | 942 | bool retry = true; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 943 | |
| 944 | /* FIXME: skip this if fh_dentry is already set? */ |
| 945 | status = fh_verify(rqstp, fhp, S_IFREG, |
| 946 | may_flags|NFSD_MAY_OWNER_OVERRIDE); |
| 947 | if (status != nfs_ok) |
| 948 | return status; |
| 949 | |
| 950 | inode = d_inode(fhp->fh_dentry); |
| 951 | hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS); |
| 952 | retry: |
| 953 | rcu_read_lock(); |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 954 | nf = nfsd_file_find_locked(inode, may_flags, hashval, net); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 955 | rcu_read_unlock(); |
| 956 | if (nf) |
| 957 | goto wait_for_construction; |
| 958 | |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 959 | new = nfsd_file_alloc(inode, may_flags, hashval, net); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 960 | if (!new) { |
| 961 | trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, |
| 962 | NULL, nfserr_jukebox); |
| 963 | return nfserr_jukebox; |
| 964 | } |
| 965 | |
| 966 | spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock); |
Trond Myklebust | 5e11322 | 2019-09-02 13:02:55 -0400 | [diff] [blame] | 967 | nf = nfsd_file_find_locked(inode, may_flags, hashval, net); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 968 | if (nf == NULL) |
| 969 | goto open_file; |
| 970 | spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); |
| 971 | nfsd_file_slab_free(&new->nf_rcu); |
| 972 | |
| 973 | wait_for_construction: |
| 974 | wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE); |
| 975 | |
| 976 | /* Did construction of this file fail? */ |
| 977 | if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { |
Trond Myklebust | 28c7d86 | 2020-01-06 13:18:03 -0500 | [diff] [blame] | 978 | if (!retry) { |
| 979 | status = nfserr_jukebox; |
| 980 | goto out; |
| 981 | } |
| 982 | retry = false; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 983 | nfsd_file_put_noref(nf); |
| 984 | goto retry; |
| 985 | } |
| 986 | |
| 987 | this_cpu_inc(nfsd_file_cache_hits); |
| 988 | |
| 989 | if (!(may_flags & NFSD_MAY_NOT_BREAK_LEASE)) { |
| 990 | bool write = (may_flags & NFSD_MAY_WRITE); |
| 991 | |
| 992 | if (test_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags) || |
| 993 | (test_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags) && write)) { |
| 994 | status = nfserrno(nfsd_open_break_lease( |
| 995 | file_inode(nf->nf_file), may_flags)); |
| 996 | if (status == nfs_ok) { |
| 997 | clear_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags); |
| 998 | if (write) |
| 999 | clear_bit(NFSD_FILE_BREAK_WRITE, |
| 1000 | &nf->nf_flags); |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | out: |
| 1005 | if (status == nfs_ok) { |
| 1006 | *pnf = nf; |
| 1007 | } else { |
| 1008 | nfsd_file_put(nf); |
| 1009 | nf = NULL; |
| 1010 | } |
| 1011 | |
| 1012 | trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, nf, status); |
| 1013 | return status; |
| 1014 | open_file: |
| 1015 | nf = new; |
| 1016 | /* Take reference for the hashtable */ |
| 1017 | atomic_inc(&nf->nf_ref); |
| 1018 | __set_bit(NFSD_FILE_HASHED, &nf->nf_flags); |
| 1019 | __set_bit(NFSD_FILE_PENDING, &nf->nf_flags); |
| 1020 | list_lru_add(&nfsd_file_lru, &nf->nf_lru); |
| 1021 | hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head); |
| 1022 | ++nfsd_file_hashtbl[hashval].nfb_count; |
| 1023 | nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount, |
| 1024 | nfsd_file_hashtbl[hashval].nfb_count); |
| 1025 | spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); |
Trond Myklebust | 9542e6a | 2020-01-06 13:18:05 -0500 | [diff] [blame] | 1026 | if (atomic_long_inc_return(&nfsd_filecache_count) >= NFSD_FILE_LRU_THRESHOLD) |
| 1027 | nfsd_file_gc(); |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 1028 | |
| 1029 | nf->nf_mark = nfsd_file_mark_find_or_create(nf); |
| 1030 | if (nf->nf_mark) |
| 1031 | status = nfsd_open_verified(rqstp, fhp, S_IFREG, |
| 1032 | may_flags, &nf->nf_file); |
| 1033 | else |
| 1034 | status = nfserr_jukebox; |
| 1035 | /* |
| 1036 | * If construction failed, or we raced with a call to unlink() |
| 1037 | * then unhash. |
| 1038 | */ |
| 1039 | if (status != nfs_ok || inode->i_nlink == 0) { |
| 1040 | bool do_free; |
| 1041 | spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock); |
| 1042 | do_free = nfsd_file_unhash(nf); |
| 1043 | spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); |
| 1044 | if (do_free) |
| 1045 | nfsd_file_put_noref(nf); |
| 1046 | } |
| 1047 | clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags); |
| 1048 | smp_mb__after_atomic(); |
| 1049 | wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING); |
| 1050 | goto out; |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * Note that fields may be added, removed or reordered in the future. Programs |
| 1055 | * scraping this file for info should test the labels to ensure they're |
| 1056 | * getting the correct field. |
| 1057 | */ |
| 1058 | static int nfsd_file_cache_stats_show(struct seq_file *m, void *v) |
| 1059 | { |
| 1060 | unsigned int i, count = 0, longest = 0; |
| 1061 | unsigned long hits = 0; |
| 1062 | |
| 1063 | /* |
| 1064 | * No need for spinlocks here since we're not terribly interested in |
| 1065 | * accuracy. We do take the nfsd_mutex simply to ensure that we |
| 1066 | * don't end up racing with server shutdown |
| 1067 | */ |
| 1068 | mutex_lock(&nfsd_mutex); |
| 1069 | if (nfsd_file_hashtbl) { |
| 1070 | for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) { |
| 1071 | count += nfsd_file_hashtbl[i].nfb_count; |
| 1072 | longest = max(longest, nfsd_file_hashtbl[i].nfb_count); |
| 1073 | } |
| 1074 | } |
| 1075 | mutex_unlock(&nfsd_mutex); |
| 1076 | |
| 1077 | for_each_possible_cpu(i) |
| 1078 | hits += per_cpu(nfsd_file_cache_hits, i); |
| 1079 | |
| 1080 | seq_printf(m, "total entries: %u\n", count); |
| 1081 | seq_printf(m, "longest chain: %u\n", longest); |
| 1082 | seq_printf(m, "cache hits: %lu\n", hits); |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | int nfsd_file_cache_stats_open(struct inode *inode, struct file *file) |
| 1087 | { |
| 1088 | return single_open(file, nfsd_file_cache_stats_show, NULL); |
| 1089 | } |