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> 9 #include <linux/file.h> 10 #include <linux/pagemap.h> 11 #include <linux/sched.h> 12 #include <linux/list_lru.h> 13 #include <linux/fsnotify_backend.h> 14 #include <linux/fsnotify.h> 15 #include <linux/seq_file.h> 16 17 #include "vfs.h" 18 #include "nfsd.h" 19 #include "nfsfh.h" 20 #include "netns.h" 21 #include "filecache.h" 22 #include "trace.h" 23 24 #define NFSDDBG_FACILITY NFSDDBG_FH 25 26 /* FIXME: dynamically size this for the machine somehow? */ 27 #define NFSD_FILE_HASH_BITS 12 28 #define NFSD_FILE_HASH_SIZE (1 << NFSD_FILE_HASH_BITS) 29 #define NFSD_LAUNDRETTE_DELAY (2 * HZ) 30 31 #define NFSD_FILE_SHUTDOWN (1) 32 #define NFSD_FILE_LRU_THRESHOLD (4096UL) 33 #define NFSD_FILE_LRU_LIMIT (NFSD_FILE_LRU_THRESHOLD << 2) 34 35 /* We only care about NFSD_MAY_READ/WRITE for this cache */ 36 #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE) 37 38 struct nfsd_fcache_bucket { 39 struct hlist_head nfb_head; 40 spinlock_t nfb_lock; 41 unsigned int nfb_count; 42 unsigned int nfb_maxcount; 43 }; 44 45 static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits); 46 47 struct nfsd_fcache_disposal { 48 struct work_struct work; 49 spinlock_t lock; 50 struct list_head freeme; 51 }; 52 53 static struct workqueue_struct *nfsd_filecache_wq __read_mostly; 54 55 static struct kmem_cache *nfsd_file_slab; 56 static struct kmem_cache *nfsd_file_mark_slab; 57 static struct nfsd_fcache_bucket *nfsd_file_hashtbl; 58 static struct list_lru nfsd_file_lru; 59 static long nfsd_file_lru_flags; 60 static struct fsnotify_group *nfsd_file_fsnotify_group; 61 static atomic_long_t nfsd_filecache_count; 62 static struct delayed_work nfsd_filecache_laundrette; 63 64 static void nfsd_file_gc(void); 65 66 static void 67 nfsd_file_schedule_laundrette(void) 68 { 69 long count = atomic_long_read(&nfsd_filecache_count); 70 71 if (count == 0 || test_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags)) 72 return; 73 74 queue_delayed_work(system_wq, &nfsd_filecache_laundrette, 75 NFSD_LAUNDRETTE_DELAY); 76 } 77 78 static void 79 nfsd_file_slab_free(struct rcu_head *rcu) 80 { 81 struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu); 82 83 put_cred(nf->nf_cred); 84 kmem_cache_free(nfsd_file_slab, nf); 85 } 86 87 static void 88 nfsd_file_mark_free(struct fsnotify_mark *mark) 89 { 90 struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark, 91 nfm_mark); 92 93 kmem_cache_free(nfsd_file_mark_slab, nfm); 94 } 95 96 static struct nfsd_file_mark * 97 nfsd_file_mark_get(struct nfsd_file_mark *nfm) 98 { 99 if (!refcount_inc_not_zero(&nfm->nfm_ref)) 100 return NULL; 101 return nfm; 102 } 103 104 static void 105 nfsd_file_mark_put(struct nfsd_file_mark *nfm) 106 { 107 if (refcount_dec_and_test(&nfm->nfm_ref)) { 108 fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group); 109 fsnotify_put_mark(&nfm->nfm_mark); 110 } 111 } 112 113 static struct nfsd_file_mark * 114 nfsd_file_mark_find_or_create(struct nfsd_file *nf) 115 { 116 int err; 117 struct fsnotify_mark *mark; 118 struct nfsd_file_mark *nfm = NULL, *new; 119 struct inode *inode = nf->nf_inode; 120 121 do { 122 fsnotify_group_lock(nfsd_file_fsnotify_group); 123 mark = fsnotify_find_mark(&inode->i_fsnotify_marks, 124 nfsd_file_fsnotify_group); 125 if (mark) { 126 nfm = nfsd_file_mark_get(container_of(mark, 127 struct nfsd_file_mark, 128 nfm_mark)); 129 fsnotify_group_unlock(nfsd_file_fsnotify_group); 130 if (nfm) { 131 fsnotify_put_mark(mark); 132 break; 133 } 134 /* Avoid soft lockup race with nfsd_file_mark_put() */ 135 fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group); 136 fsnotify_put_mark(mark); 137 } else { 138 fsnotify_group_unlock(nfsd_file_fsnotify_group); 139 } 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 refcount_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 * 170 nfsd_file_alloc(struct inode *inode, unsigned int may, unsigned int hashval, 171 struct net *net) 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(); 181 nf->nf_net = net; 182 nf->nf_flags = 0; 183 nf->nf_inode = inode; 184 nf->nf_hashval = hashval; 185 refcount_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 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 240 static void 241 nfsd_file_flush(struct nfsd_file *nf) 242 { 243 if (nf->nf_file && vfs_fsync(nf->nf_file, 1) != 0) 244 nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 245 } 246 247 static void 248 nfsd_file_do_unhash(struct nfsd_file *nf) 249 { 250 lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); 251 252 trace_nfsd_file_unhash(nf); 253 254 if (nfsd_file_check_write_error(nf)) 255 nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 256 --nfsd_file_hashtbl[nf->nf_hashval].nfb_count; 257 hlist_del_rcu(&nf->nf_node); 258 atomic_long_dec(&nfsd_filecache_count); 259 } 260 261 static bool 262 nfsd_file_unhash(struct nfsd_file *nf) 263 { 264 if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 265 nfsd_file_do_unhash(nf); 266 if (!list_empty(&nf->nf_lru)) 267 list_lru_del(&nfsd_file_lru, &nf->nf_lru); 268 return true; 269 } 270 return false; 271 } 272 273 /* 274 * Return true if the file was unhashed. 275 */ 276 static bool 277 nfsd_file_unhash_and_release_locked(struct nfsd_file *nf, struct list_head *dispose) 278 { 279 lockdep_assert_held(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); 280 281 trace_nfsd_file_unhash_and_release_locked(nf); 282 if (!nfsd_file_unhash(nf)) 283 return false; 284 /* keep final reference for nfsd_file_lru_dispose */ 285 if (refcount_dec_not_one(&nf->nf_ref)) 286 return true; 287 288 list_add(&nf->nf_lru, dispose); 289 return true; 290 } 291 292 static void 293 nfsd_file_put_noref(struct nfsd_file *nf) 294 { 295 trace_nfsd_file_put(nf); 296 297 if (refcount_dec_and_test(&nf->nf_ref)) { 298 WARN_ON(test_bit(NFSD_FILE_HASHED, &nf->nf_flags)); 299 nfsd_file_free(nf); 300 } 301 } 302 303 void 304 nfsd_file_put(struct nfsd_file *nf) 305 { 306 set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags); 307 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) { 308 nfsd_file_flush(nf); 309 nfsd_file_put_noref(nf); 310 } else { 311 nfsd_file_put_noref(nf); 312 if (nf->nf_file) 313 nfsd_file_schedule_laundrette(); 314 } 315 if (atomic_long_read(&nfsd_filecache_count) >= NFSD_FILE_LRU_LIMIT) 316 nfsd_file_gc(); 317 } 318 319 struct nfsd_file * 320 nfsd_file_get(struct nfsd_file *nf) 321 { 322 if (likely(refcount_inc_not_zero(&nf->nf_ref))) 323 return nf; 324 return NULL; 325 } 326 327 static void 328 nfsd_file_dispose_list(struct list_head *dispose) 329 { 330 struct nfsd_file *nf; 331 332 while(!list_empty(dispose)) { 333 nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 334 list_del(&nf->nf_lru); 335 nfsd_file_flush(nf); 336 nfsd_file_put_noref(nf); 337 } 338 } 339 340 static void 341 nfsd_file_dispose_list_sync(struct list_head *dispose) 342 { 343 bool flush = false; 344 struct nfsd_file *nf; 345 346 while(!list_empty(dispose)) { 347 nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 348 list_del(&nf->nf_lru); 349 nfsd_file_flush(nf); 350 if (!refcount_dec_and_test(&nf->nf_ref)) 351 continue; 352 if (nfsd_file_free(nf)) 353 flush = true; 354 } 355 if (flush) 356 flush_delayed_fput(); 357 } 358 359 static void 360 nfsd_file_list_remove_disposal(struct list_head *dst, 361 struct nfsd_fcache_disposal *l) 362 { 363 spin_lock(&l->lock); 364 list_splice_init(&l->freeme, dst); 365 spin_unlock(&l->lock); 366 } 367 368 static void 369 nfsd_file_list_add_disposal(struct list_head *files, struct net *net) 370 { 371 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 372 struct nfsd_fcache_disposal *l = nn->fcache_disposal; 373 374 spin_lock(&l->lock); 375 list_splice_tail_init(files, &l->freeme); 376 spin_unlock(&l->lock); 377 queue_work(nfsd_filecache_wq, &l->work); 378 } 379 380 static void 381 nfsd_file_list_add_pernet(struct list_head *dst, struct list_head *src, 382 struct net *net) 383 { 384 struct nfsd_file *nf, *tmp; 385 386 list_for_each_entry_safe(nf, tmp, src, nf_lru) { 387 if (nf->nf_net == net) 388 list_move_tail(&nf->nf_lru, dst); 389 } 390 } 391 392 static void 393 nfsd_file_dispose_list_delayed(struct list_head *dispose) 394 { 395 LIST_HEAD(list); 396 struct nfsd_file *nf; 397 398 while(!list_empty(dispose)) { 399 nf = list_first_entry(dispose, struct nfsd_file, nf_lru); 400 nfsd_file_list_add_pernet(&list, dispose, nf->nf_net); 401 nfsd_file_list_add_disposal(&list, nf->nf_net); 402 } 403 } 404 405 /* 406 * Note this can deadlock with nfsd_file_cache_purge. 407 */ 408 static enum lru_status 409 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru, 410 spinlock_t *lock, void *arg) 411 __releases(lock) 412 __acquires(lock) 413 { 414 struct list_head *head = arg; 415 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru); 416 417 /* 418 * Do a lockless refcount check. The hashtable holds one reference, so 419 * we look to see if anything else has a reference, or if any have 420 * been put since the shrinker last ran. Those don't get unhashed and 421 * released. 422 * 423 * Note that in the put path, we set the flag and then decrement the 424 * counter. Here we check the counter and then test and clear the flag. 425 * That order is deliberate to ensure that we can do this locklessly. 426 */ 427 if (refcount_read(&nf->nf_ref) > 1) 428 goto out_skip; 429 430 /* 431 * Don't throw out files that are still undergoing I/O or 432 * that have uncleared errors pending. 433 */ 434 if (nfsd_file_check_writeback(nf)) 435 goto out_skip; 436 437 if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) 438 goto out_skip; 439 440 if (!test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) 441 goto out_skip; 442 443 list_lru_isolate_move(lru, &nf->nf_lru, head); 444 return LRU_REMOVED; 445 out_skip: 446 return LRU_SKIP; 447 } 448 449 static unsigned long 450 nfsd_file_lru_walk_list(struct shrink_control *sc) 451 { 452 LIST_HEAD(head); 453 struct nfsd_file *nf; 454 unsigned long ret; 455 456 if (sc) 457 ret = list_lru_shrink_walk(&nfsd_file_lru, sc, 458 nfsd_file_lru_cb, &head); 459 else 460 ret = list_lru_walk(&nfsd_file_lru, 461 nfsd_file_lru_cb, 462 &head, LONG_MAX); 463 list_for_each_entry(nf, &head, nf_lru) { 464 spin_lock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); 465 nfsd_file_do_unhash(nf); 466 spin_unlock(&nfsd_file_hashtbl[nf->nf_hashval].nfb_lock); 467 } 468 nfsd_file_dispose_list_delayed(&head); 469 return ret; 470 } 471 472 static void 473 nfsd_file_gc(void) 474 { 475 nfsd_file_lru_walk_list(NULL); 476 } 477 478 static void 479 nfsd_file_gc_worker(struct work_struct *work) 480 { 481 nfsd_file_gc(); 482 nfsd_file_schedule_laundrette(); 483 } 484 485 static unsigned long 486 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc) 487 { 488 return list_lru_count(&nfsd_file_lru); 489 } 490 491 static unsigned long 492 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc) 493 { 494 return nfsd_file_lru_walk_list(sc); 495 } 496 497 static struct shrinker nfsd_file_shrinker = { 498 .scan_objects = nfsd_file_lru_scan, 499 .count_objects = nfsd_file_lru_count, 500 .seeks = 1, 501 }; 502 503 static void 504 __nfsd_file_close_inode(struct inode *inode, unsigned int hashval, 505 struct list_head *dispose) 506 { 507 struct nfsd_file *nf; 508 struct hlist_node *tmp; 509 510 spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock); 511 hlist_for_each_entry_safe(nf, tmp, &nfsd_file_hashtbl[hashval].nfb_head, nf_node) { 512 if (inode == nf->nf_inode) 513 nfsd_file_unhash_and_release_locked(nf, dispose); 514 } 515 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); 516 } 517 518 /** 519 * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file 520 * @inode: inode of the file to attempt to remove 521 * 522 * Walk the whole hash bucket, looking for any files that correspond to "inode". 523 * If any do, then unhash them and put the hashtable reference to them and 524 * destroy any that had their last reference put. Also ensure that any of the 525 * fputs also have their final __fput done as well. 526 */ 527 void 528 nfsd_file_close_inode_sync(struct inode *inode) 529 { 530 unsigned int hashval = (unsigned int)hash_long(inode->i_ino, 531 NFSD_FILE_HASH_BITS); 532 LIST_HEAD(dispose); 533 534 __nfsd_file_close_inode(inode, hashval, &dispose); 535 trace_nfsd_file_close_inode_sync(inode, hashval, !list_empty(&dispose)); 536 nfsd_file_dispose_list_sync(&dispose); 537 } 538 539 /** 540 * nfsd_file_close_inode - attempt a delayed close of a nfsd_file 541 * @inode: inode of the file to attempt to remove 542 * 543 * Walk the whole hash bucket, looking for any files that correspond to "inode". 544 * If any do, then unhash them and put the hashtable reference to them and 545 * destroy any that had their last reference put. 546 */ 547 static void 548 nfsd_file_close_inode(struct inode *inode) 549 { 550 unsigned int hashval = (unsigned int)hash_long(inode->i_ino, 551 NFSD_FILE_HASH_BITS); 552 LIST_HEAD(dispose); 553 554 __nfsd_file_close_inode(inode, hashval, &dispose); 555 trace_nfsd_file_close_inode(inode, hashval, !list_empty(&dispose)); 556 nfsd_file_dispose_list_delayed(&dispose); 557 } 558 559 /** 560 * nfsd_file_delayed_close - close unused nfsd_files 561 * @work: dummy 562 * 563 * Walk the LRU list and close any entries that have not been used since 564 * the last scan. 565 * 566 * Note this can deadlock with nfsd_file_cache_purge. 567 */ 568 static void 569 nfsd_file_delayed_close(struct work_struct *work) 570 { 571 LIST_HEAD(head); 572 struct nfsd_fcache_disposal *l = container_of(work, 573 struct nfsd_fcache_disposal, work); 574 575 nfsd_file_list_remove_disposal(&head, l); 576 nfsd_file_dispose_list(&head); 577 } 578 579 static int 580 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg, 581 void *data) 582 { 583 struct file_lock *fl = data; 584 585 /* Only close files for F_SETLEASE leases */ 586 if (fl->fl_flags & FL_LEASE) 587 nfsd_file_close_inode_sync(file_inode(fl->fl_file)); 588 return 0; 589 } 590 591 static struct notifier_block nfsd_file_lease_notifier = { 592 .notifier_call = nfsd_file_lease_notifier_call, 593 }; 594 595 static int 596 nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask, 597 struct inode *inode, struct inode *dir, 598 const struct qstr *name, u32 cookie) 599 { 600 if (WARN_ON_ONCE(!inode)) 601 return 0; 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_inode_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 638 nfsd_filecache_wq = alloc_workqueue("nfsd_filecache", 0, 0); 639 if (!nfsd_filecache_wq) 640 goto out; 641 642 nfsd_file_hashtbl = kvcalloc(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 FSNOTIFY_GROUP_NOFS); 684 if (IS_ERR(nfsd_file_fsnotify_group)) { 685 pr_err("nfsd: unable to create fsnotify group: %ld\n", 686 PTR_ERR(nfsd_file_fsnotify_group)); 687 ret = PTR_ERR(nfsd_file_fsnotify_group); 688 nfsd_file_fsnotify_group = NULL; 689 goto out_notifier; 690 } 691 692 for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) { 693 INIT_HLIST_HEAD(&nfsd_file_hashtbl[i].nfb_head); 694 spin_lock_init(&nfsd_file_hashtbl[i].nfb_lock); 695 } 696 697 INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker); 698 out: 699 return ret; 700 out_notifier: 701 lease_unregister_notifier(&nfsd_file_lease_notifier); 702 out_shrinker: 703 unregister_shrinker(&nfsd_file_shrinker); 704 out_lru: 705 list_lru_destroy(&nfsd_file_lru); 706 out_err: 707 kmem_cache_destroy(nfsd_file_slab); 708 nfsd_file_slab = NULL; 709 kmem_cache_destroy(nfsd_file_mark_slab); 710 nfsd_file_mark_slab = NULL; 711 kvfree(nfsd_file_hashtbl); 712 nfsd_file_hashtbl = NULL; 713 destroy_workqueue(nfsd_filecache_wq); 714 nfsd_filecache_wq = NULL; 715 goto out; 716 } 717 718 /* 719 * Note this can deadlock with nfsd_file_lru_cb. 720 */ 721 void 722 nfsd_file_cache_purge(struct net *net) 723 { 724 unsigned int i; 725 struct nfsd_file *nf; 726 struct hlist_node *next; 727 LIST_HEAD(dispose); 728 bool del; 729 730 if (!nfsd_file_hashtbl) 731 return; 732 733 for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) { 734 struct nfsd_fcache_bucket *nfb = &nfsd_file_hashtbl[i]; 735 736 spin_lock(&nfb->nfb_lock); 737 hlist_for_each_entry_safe(nf, next, &nfb->nfb_head, nf_node) { 738 if (net && nf->nf_net != net) 739 continue; 740 del = nfsd_file_unhash_and_release_locked(nf, &dispose); 741 742 /* 743 * Deadlock detected! Something marked this entry as 744 * unhased, but hasn't removed it from the hash list. 745 */ 746 WARN_ON_ONCE(!del); 747 } 748 spin_unlock(&nfb->nfb_lock); 749 nfsd_file_dispose_list(&dispose); 750 } 751 } 752 753 static struct nfsd_fcache_disposal * 754 nfsd_alloc_fcache_disposal(void) 755 { 756 struct nfsd_fcache_disposal *l; 757 758 l = kmalloc(sizeof(*l), GFP_KERNEL); 759 if (!l) 760 return NULL; 761 INIT_WORK(&l->work, nfsd_file_delayed_close); 762 spin_lock_init(&l->lock); 763 INIT_LIST_HEAD(&l->freeme); 764 return l; 765 } 766 767 static void 768 nfsd_free_fcache_disposal(struct nfsd_fcache_disposal *l) 769 { 770 cancel_work_sync(&l->work); 771 nfsd_file_dispose_list(&l->freeme); 772 kfree(l); 773 } 774 775 static void 776 nfsd_free_fcache_disposal_net(struct net *net) 777 { 778 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 779 struct nfsd_fcache_disposal *l = nn->fcache_disposal; 780 781 nfsd_free_fcache_disposal(l); 782 } 783 784 int 785 nfsd_file_cache_start_net(struct net *net) 786 { 787 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 788 789 nn->fcache_disposal = nfsd_alloc_fcache_disposal(); 790 return nn->fcache_disposal ? 0 : -ENOMEM; 791 } 792 793 void 794 nfsd_file_cache_shutdown_net(struct net *net) 795 { 796 nfsd_file_cache_purge(net); 797 nfsd_free_fcache_disposal_net(net); 798 } 799 800 void 801 nfsd_file_cache_shutdown(void) 802 { 803 set_bit(NFSD_FILE_SHUTDOWN, &nfsd_file_lru_flags); 804 805 lease_unregister_notifier(&nfsd_file_lease_notifier); 806 unregister_shrinker(&nfsd_file_shrinker); 807 /* 808 * make sure all callers of nfsd_file_lru_cb are done before 809 * calling nfsd_file_cache_purge 810 */ 811 cancel_delayed_work_sync(&nfsd_filecache_laundrette); 812 nfsd_file_cache_purge(NULL); 813 list_lru_destroy(&nfsd_file_lru); 814 rcu_barrier(); 815 fsnotify_put_group(nfsd_file_fsnotify_group); 816 nfsd_file_fsnotify_group = NULL; 817 kmem_cache_destroy(nfsd_file_slab); 818 nfsd_file_slab = NULL; 819 fsnotify_wait_marks_destroyed(); 820 kmem_cache_destroy(nfsd_file_mark_slab); 821 nfsd_file_mark_slab = NULL; 822 kvfree(nfsd_file_hashtbl); 823 nfsd_file_hashtbl = NULL; 824 destroy_workqueue(nfsd_filecache_wq); 825 nfsd_filecache_wq = NULL; 826 } 827 828 static bool 829 nfsd_match_cred(const struct cred *c1, const struct cred *c2) 830 { 831 int i; 832 833 if (!uid_eq(c1->fsuid, c2->fsuid)) 834 return false; 835 if (!gid_eq(c1->fsgid, c2->fsgid)) 836 return false; 837 if (c1->group_info == NULL || c2->group_info == NULL) 838 return c1->group_info == c2->group_info; 839 if (c1->group_info->ngroups != c2->group_info->ngroups) 840 return false; 841 for (i = 0; i < c1->group_info->ngroups; i++) { 842 if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i])) 843 return false; 844 } 845 return true; 846 } 847 848 static struct nfsd_file * 849 nfsd_file_find_locked(struct inode *inode, unsigned int may_flags, 850 unsigned int hashval, struct net *net) 851 { 852 struct nfsd_file *nf; 853 unsigned char need = may_flags & NFSD_FILE_MAY_MASK; 854 855 hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head, 856 nf_node, lockdep_is_held(&nfsd_file_hashtbl[hashval].nfb_lock)) { 857 if (nf->nf_may != need) 858 continue; 859 if (nf->nf_inode != inode) 860 continue; 861 if (nf->nf_net != net) 862 continue; 863 if (!nfsd_match_cred(nf->nf_cred, current_cred())) 864 continue; 865 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) 866 continue; 867 if (nfsd_file_get(nf) != NULL) 868 return nf; 869 } 870 return NULL; 871 } 872 873 /** 874 * nfsd_file_is_cached - are there any cached open files for this fh? 875 * @inode: inode of the file to check 876 * 877 * Scan the hashtable for open files that match this fh. Returns true if there 878 * are any, and false if not. 879 */ 880 bool 881 nfsd_file_is_cached(struct inode *inode) 882 { 883 bool ret = false; 884 struct nfsd_file *nf; 885 unsigned int hashval; 886 887 hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS); 888 889 rcu_read_lock(); 890 hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head, 891 nf_node) { 892 if (inode == nf->nf_inode) { 893 ret = true; 894 break; 895 } 896 } 897 rcu_read_unlock(); 898 trace_nfsd_file_is_cached(inode, hashval, (int)ret); 899 return ret; 900 } 901 902 __be32 903 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, 904 unsigned int may_flags, struct nfsd_file **pnf) 905 { 906 __be32 status; 907 struct net *net = SVC_NET(rqstp); 908 struct nfsd_file *nf, *new; 909 struct inode *inode; 910 unsigned int hashval; 911 bool retry = true; 912 913 /* FIXME: skip this if fh_dentry is already set? */ 914 status = fh_verify(rqstp, fhp, S_IFREG, 915 may_flags|NFSD_MAY_OWNER_OVERRIDE); 916 if (status != nfs_ok) 917 return status; 918 919 inode = d_inode(fhp->fh_dentry); 920 hashval = (unsigned int)hash_long(inode->i_ino, NFSD_FILE_HASH_BITS); 921 retry: 922 rcu_read_lock(); 923 nf = nfsd_file_find_locked(inode, may_flags, hashval, net); 924 rcu_read_unlock(); 925 if (nf) 926 goto wait_for_construction; 927 928 new = nfsd_file_alloc(inode, may_flags, hashval, net); 929 if (!new) { 930 trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, 931 NULL, nfserr_jukebox); 932 return nfserr_jukebox; 933 } 934 935 spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock); 936 nf = nfsd_file_find_locked(inode, may_flags, hashval, net); 937 if (nf == NULL) 938 goto open_file; 939 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); 940 nfsd_file_slab_free(&new->nf_rcu); 941 942 wait_for_construction: 943 wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE); 944 945 /* Did construction of this file fail? */ 946 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 947 if (!retry) { 948 status = nfserr_jukebox; 949 goto out; 950 } 951 retry = false; 952 nfsd_file_put_noref(nf); 953 goto retry; 954 } 955 956 this_cpu_inc(nfsd_file_cache_hits); 957 958 if (!(may_flags & NFSD_MAY_NOT_BREAK_LEASE)) { 959 bool write = (may_flags & NFSD_MAY_WRITE); 960 961 if (test_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags) || 962 (test_bit(NFSD_FILE_BREAK_WRITE, &nf->nf_flags) && write)) { 963 status = nfserrno(nfsd_open_break_lease( 964 file_inode(nf->nf_file), may_flags)); 965 if (status == nfs_ok) { 966 clear_bit(NFSD_FILE_BREAK_READ, &nf->nf_flags); 967 if (write) 968 clear_bit(NFSD_FILE_BREAK_WRITE, 969 &nf->nf_flags); 970 } 971 } 972 } 973 out: 974 if (status == nfs_ok) { 975 *pnf = nf; 976 } else { 977 nfsd_file_put(nf); 978 nf = NULL; 979 } 980 981 trace_nfsd_file_acquire(rqstp, hashval, inode, may_flags, nf, status); 982 return status; 983 open_file: 984 nf = new; 985 /* Take reference for the hashtable */ 986 refcount_inc(&nf->nf_ref); 987 __set_bit(NFSD_FILE_HASHED, &nf->nf_flags); 988 __set_bit(NFSD_FILE_PENDING, &nf->nf_flags); 989 list_lru_add(&nfsd_file_lru, &nf->nf_lru); 990 hlist_add_head_rcu(&nf->nf_node, &nfsd_file_hashtbl[hashval].nfb_head); 991 ++nfsd_file_hashtbl[hashval].nfb_count; 992 nfsd_file_hashtbl[hashval].nfb_maxcount = max(nfsd_file_hashtbl[hashval].nfb_maxcount, 993 nfsd_file_hashtbl[hashval].nfb_count); 994 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); 995 if (atomic_long_inc_return(&nfsd_filecache_count) >= NFSD_FILE_LRU_THRESHOLD) 996 nfsd_file_gc(); 997 998 nf->nf_mark = nfsd_file_mark_find_or_create(nf); 999 if (nf->nf_mark) 1000 status = nfsd_open_verified(rqstp, fhp, S_IFREG, 1001 may_flags, &nf->nf_file); 1002 else 1003 status = nfserr_jukebox; 1004 /* 1005 * If construction failed, or we raced with a call to unlink() 1006 * then unhash. 1007 */ 1008 if (status != nfs_ok || inode->i_nlink == 0) { 1009 bool do_free; 1010 spin_lock(&nfsd_file_hashtbl[hashval].nfb_lock); 1011 do_free = nfsd_file_unhash(nf); 1012 spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); 1013 if (do_free) 1014 nfsd_file_put_noref(nf); 1015 } 1016 clear_bit_unlock(NFSD_FILE_PENDING, &nf->nf_flags); 1017 smp_mb__after_atomic(); 1018 wake_up_bit(&nf->nf_flags, NFSD_FILE_PENDING); 1019 goto out; 1020 } 1021 1022 /* 1023 * Note that fields may be added, removed or reordered in the future. Programs 1024 * scraping this file for info should test the labels to ensure they're 1025 * getting the correct field. 1026 */ 1027 static int nfsd_file_cache_stats_show(struct seq_file *m, void *v) 1028 { 1029 unsigned int i, count = 0, longest = 0; 1030 unsigned long hits = 0; 1031 1032 /* 1033 * No need for spinlocks here since we're not terribly interested in 1034 * accuracy. We do take the nfsd_mutex simply to ensure that we 1035 * don't end up racing with server shutdown 1036 */ 1037 mutex_lock(&nfsd_mutex); 1038 if (nfsd_file_hashtbl) { 1039 for (i = 0; i < NFSD_FILE_HASH_SIZE; i++) { 1040 count += nfsd_file_hashtbl[i].nfb_count; 1041 longest = max(longest, nfsd_file_hashtbl[i].nfb_count); 1042 } 1043 } 1044 mutex_unlock(&nfsd_mutex); 1045 1046 for_each_possible_cpu(i) 1047 hits += per_cpu(nfsd_file_cache_hits, i); 1048 1049 seq_printf(m, "total entries: %u\n", count); 1050 seq_printf(m, "longest chain: %u\n", longest); 1051 seq_printf(m, "cache hits: %lu\n", hits); 1052 return 0; 1053 } 1054 1055 int nfsd_file_cache_stats_open(struct inode *inode, struct file *file) 1056 { 1057 return single_open(file, nfsd_file_cache_stats_show, NULL); 1058 } 1059