1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * The NFSD open file cache. 4 * 5 * (c) 2015 - Jeff Layton <jeff.layton@primarydata.com> 6 * 7 * An nfsd_file object is a per-file collection of open state that binds 8 * together: 9 * - a struct file * 10 * - a user credential 11 * - a network namespace 12 * - a read-ahead context 13 * - monitoring for writeback errors 14 * 15 * nfsd_file objects are reference-counted. Consumers acquire a new 16 * object via the nfsd_file_acquire API. They manage their interest in 17 * the acquired object, and hence the object's reference count, via 18 * nfsd_file_get and nfsd_file_put. There are two varieties of nfsd_file 19 * object: 20 * 21 * * non-garbage-collected: When a consumer wants to precisely control 22 * the lifetime of a file's open state, it acquires a non-garbage- 23 * collected nfsd_file. The final nfsd_file_put releases the open 24 * state immediately. 25 * 26 * * garbage-collected: When a consumer does not control the lifetime 27 * of open state, it acquires a garbage-collected nfsd_file. The 28 * final nfsd_file_put allows the open state to linger for a period 29 * during which it may be re-used. 30 */ 31 32 #include <linux/hash.h> 33 #include <linux/slab.h> 34 #include <linux/file.h> 35 #include <linux/pagemap.h> 36 #include <linux/sched.h> 37 #include <linux/list_lru.h> 38 #include <linux/fsnotify_backend.h> 39 #include <linux/fsnotify.h> 40 #include <linux/seq_file.h> 41 #include <linux/rhashtable.h> 42 #include <linux/nfslocalio.h> 43 44 #include "vfs.h" 45 #include "nfsd.h" 46 #include "nfsfh.h" 47 #include "netns.h" 48 #include "filecache.h" 49 #include "trace.h" 50 51 #define NFSD_LAUNDRETTE_DELAY (2 * HZ) 52 53 #define NFSD_FILE_CACHE_UP (0) 54 55 /* We only care about NFSD_MAY_READ/WRITE for this cache */ 56 #define NFSD_FILE_MAY_MASK (NFSD_MAY_READ|NFSD_MAY_WRITE|NFSD_MAY_LOCALIO) 57 58 /* If the shrinker runs between calls to list_lru_walk_node() in 59 * nfsd_file_gc(), the "remaining" count will be wrong. This could 60 * result in premature freeing of some files. This may not matter much 61 * but is easy to fix with this spinlock which temporarily disables 62 * the shrinker. 63 * 64 * It also serializes callers of nfsd_file_dispose_list_delayed() 65 * against per-net shutdown. 66 */ 67 static DEFINE_SPINLOCK(nfsd_gc_lock); 68 69 static DEFINE_PER_CPU(unsigned long, nfsd_file_cache_hits); 70 static DEFINE_PER_CPU(unsigned long, nfsd_file_acquisitions); 71 static DEFINE_PER_CPU(unsigned long, nfsd_file_allocations); 72 static DEFINE_PER_CPU(unsigned long, nfsd_file_releases); 73 static DEFINE_PER_CPU(unsigned long, nfsd_file_total_age); 74 static DEFINE_PER_CPU(unsigned long, nfsd_file_evictions); 75 76 static struct kmem_cache *nfsd_file_slab; 77 static struct kmem_cache *nfsd_file_mark_slab; 78 static struct list_lru nfsd_file_lru; 79 static unsigned long nfsd_file_flags; 80 static struct fsnotify_group *nfsd_file_fsnotify_group; 81 static struct delayed_work nfsd_filecache_laundrette; 82 static struct rhltable nfsd_file_rhltable 83 ____cacheline_aligned_in_smp; 84 85 static bool 86 nfsd_match_cred(const struct cred *c1, const struct cred *c2) 87 { 88 int i; 89 90 if (!uid_eq(c1->fsuid, c2->fsuid)) 91 return false; 92 if (!gid_eq(c1->fsgid, c2->fsgid)) 93 return false; 94 if (c1->group_info == NULL || c2->group_info == NULL) 95 return c1->group_info == c2->group_info; 96 if (c1->group_info->ngroups != c2->group_info->ngroups) 97 return false; 98 for (i = 0; i < c1->group_info->ngroups; i++) { 99 if (!gid_eq(c1->group_info->gid[i], c2->group_info->gid[i])) 100 return false; 101 } 102 return true; 103 } 104 105 static const struct rhashtable_params nfsd_file_rhash_params = { 106 .key_len = sizeof_field(struct nfsd_file, nf_inode), 107 .key_offset = offsetof(struct nfsd_file, nf_inode), 108 .head_offset = offsetof(struct nfsd_file, nf_rlist), 109 110 /* 111 * Start with a single page hash table to reduce resizing churn 112 * on light workloads. 113 */ 114 .min_size = 256, 115 .automatic_shrinking = true, 116 }; 117 118 static void 119 nfsd_file_schedule_laundrette(void) 120 { 121 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags)) 122 queue_delayed_work(system_dfl_wq, &nfsd_filecache_laundrette, 123 NFSD_LAUNDRETTE_DELAY); 124 } 125 126 static void 127 nfsd_file_slab_free(struct rcu_head *rcu) 128 { 129 struct nfsd_file *nf = container_of(rcu, struct nfsd_file, nf_rcu); 130 131 put_cred(nf->nf_cred); 132 kmem_cache_free(nfsd_file_slab, nf); 133 } 134 135 static void 136 nfsd_file_mark_free(struct fsnotify_mark *mark) 137 { 138 struct nfsd_file_mark *nfm = container_of(mark, struct nfsd_file_mark, 139 nfm_mark); 140 141 kmem_cache_free(nfsd_file_mark_slab, nfm); 142 } 143 144 static struct nfsd_file_mark * 145 nfsd_file_mark_get(struct nfsd_file_mark *nfm) 146 { 147 if (!refcount_inc_not_zero(&nfm->nfm_ref)) 148 return NULL; 149 return nfm; 150 } 151 152 static void 153 nfsd_file_mark_put(struct nfsd_file_mark *nfm) 154 { 155 if (refcount_dec_and_test(&nfm->nfm_ref)) { 156 fsnotify_destroy_mark(&nfm->nfm_mark, nfsd_file_fsnotify_group); 157 fsnotify_put_mark(&nfm->nfm_mark); 158 } 159 } 160 161 static struct nfsd_file_mark * 162 nfsd_file_mark_find_or_create(struct inode *inode) 163 { 164 int err; 165 struct fsnotify_mark *mark; 166 struct nfsd_file_mark *nfm = NULL, *new; 167 168 do { 169 fsnotify_group_lock(nfsd_file_fsnotify_group); 170 mark = fsnotify_find_inode_mark(inode, 171 nfsd_file_fsnotify_group); 172 if (mark) { 173 nfm = nfsd_file_mark_get(container_of(mark, 174 struct nfsd_file_mark, 175 nfm_mark)); 176 fsnotify_group_unlock(nfsd_file_fsnotify_group); 177 if (nfm) { 178 fsnotify_put_mark(mark); 179 break; 180 } 181 /* Avoid soft lockup race with nfsd_file_mark_put() */ 182 fsnotify_destroy_mark(mark, nfsd_file_fsnotify_group); 183 fsnotify_put_mark(mark); 184 } else { 185 fsnotify_group_unlock(nfsd_file_fsnotify_group); 186 } 187 188 /* allocate a new nfm */ 189 new = kmem_cache_alloc(nfsd_file_mark_slab, GFP_KERNEL); 190 if (!new) 191 return NULL; 192 fsnotify_init_mark(&new->nfm_mark, nfsd_file_fsnotify_group); 193 new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF; 194 refcount_set(&new->nfm_ref, 1); 195 mutex_init(&new->nfm_recalc_mutex); 196 197 err = fsnotify_add_inode_mark(&new->nfm_mark, inode, 0); 198 199 /* 200 * If the add was successful, then return the object. 201 * Otherwise, we need to put the reference we hold on the 202 * nfm_mark. The fsnotify code will take a reference and put 203 * it on failure, so we can't just free it directly. It's also 204 * not safe to call fsnotify_destroy_mark on it as the 205 * mark->group will be NULL. Thus, we can't let the nfm_ref 206 * counter drive the destruction at this point. 207 */ 208 if (likely(!err)) 209 nfm = new; 210 else 211 fsnotify_put_mark(&new->nfm_mark); 212 } while (unlikely(err == -EEXIST)); 213 214 return nfm; 215 } 216 217 static struct nfsd_file * 218 nfsd_file_alloc(struct net *net, struct inode *inode, unsigned char need, 219 bool want_gc) 220 { 221 struct nfsd_file *nf; 222 223 nf = kmem_cache_alloc(nfsd_file_slab, GFP_KERNEL); 224 if (unlikely(!nf)) 225 return NULL; 226 227 this_cpu_inc(nfsd_file_allocations); 228 INIT_LIST_HEAD(&nf->nf_lru); 229 INIT_LIST_HEAD(&nf->nf_gc); 230 nf->nf_birthtime = ktime_get(); 231 nf->nf_file = NULL; 232 nf->nf_cred = get_current_cred(); 233 nf->nf_net = net; 234 nf->nf_flags = want_gc ? 235 BIT(NFSD_FILE_HASHED) | BIT(NFSD_FILE_PENDING) | BIT(NFSD_FILE_GC) : 236 BIT(NFSD_FILE_HASHED) | BIT(NFSD_FILE_PENDING); 237 nf->nf_inode = inode; 238 refcount_set(&nf->nf_ref, 1); 239 nf->nf_may = need; 240 nf->nf_mark = NULL; 241 nf->nf_dio_mem_align = 0; 242 nf->nf_dio_offset_align = 0; 243 nf->nf_dio_read_offset_align = 0; 244 return nf; 245 } 246 247 /** 248 * nfsd_file_check_write_error - check for writeback errors on a file 249 * @nf: nfsd_file to check for writeback errors 250 * 251 * Check whether a nfsd_file has an unseen error. Reset the write 252 * verifier if so. 253 */ 254 static void 255 nfsd_file_check_write_error(struct nfsd_file *nf) 256 { 257 struct file *file = nf->nf_file; 258 259 if ((file->f_mode & FMODE_WRITE) && 260 filemap_check_wb_err(file->f_mapping, READ_ONCE(file->f_wb_err))) 261 nfsd_reset_write_verifier(net_generic(nf->nf_net, nfsd_net_id)); 262 } 263 264 static void 265 nfsd_file_hash_remove(struct nfsd_file *nf) 266 { 267 trace_nfsd_file_unhash(nf); 268 rhltable_remove(&nfsd_file_rhltable, &nf->nf_rlist, 269 nfsd_file_rhash_params); 270 } 271 272 static bool 273 nfsd_file_unhash(struct nfsd_file *nf) 274 { 275 if (test_and_clear_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 276 nfsd_file_hash_remove(nf); 277 return true; 278 } 279 return false; 280 } 281 282 static void 283 nfsd_file_free(struct nfsd_file *nf) 284 { 285 s64 age = ktime_to_ms(ktime_sub(ktime_get(), nf->nf_birthtime)); 286 287 trace_nfsd_file_free(nf); 288 289 this_cpu_inc(nfsd_file_releases); 290 this_cpu_add(nfsd_file_total_age, age); 291 292 nfsd_file_unhash(nf); 293 if (nf->nf_mark) 294 nfsd_file_mark_put(nf->nf_mark); 295 if (nf->nf_file) { 296 nfsd_file_check_write_error(nf); 297 nfsd_filp_close(nf->nf_file); 298 } 299 300 /* 301 * If this item is still linked via nf_lru, that's a bug. 302 * WARN and leak it to preserve system stability. 303 */ 304 if (WARN_ON_ONCE(!list_empty(&nf->nf_lru))) 305 return; 306 307 call_rcu(&nf->nf_rcu, nfsd_file_slab_free); 308 } 309 310 static bool 311 nfsd_file_check_writeback(struct nfsd_file *nf) 312 { 313 struct file *file = nf->nf_file; 314 struct address_space *mapping; 315 316 /* File not open for write? */ 317 if (!(file->f_mode & FMODE_WRITE)) 318 return false; 319 320 /* 321 * Some filesystems (e.g. NFS) flush all dirty data on close. 322 * On others, there is no need to wait for writeback. 323 */ 324 if (!(file_inode(file)->i_sb->s_export_op->flags & EXPORT_OP_FLUSH_ON_CLOSE)) 325 return false; 326 327 mapping = file->f_mapping; 328 return mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) || 329 mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK); 330 } 331 332 static void nfsd_file_lru_add(struct nfsd_file *nf) 333 { 334 refcount_inc(&nf->nf_ref); 335 if (list_lru_add_obj(&nfsd_file_lru, &nf->nf_lru)) 336 trace_nfsd_file_lru_add(nf); 337 else { 338 refcount_dec(&nf->nf_ref); 339 WARN_ON_ONCE(1); 340 return; 341 } 342 nfsd_file_schedule_laundrette(); 343 } 344 345 static bool nfsd_file_lru_remove(struct nfsd_file *nf) 346 { 347 if (list_lru_del_obj(&nfsd_file_lru, &nf->nf_lru)) { 348 trace_nfsd_file_lru_del(nf); 349 return true; 350 } 351 return false; 352 } 353 354 struct nfsd_file * 355 nfsd_file_get(struct nfsd_file *nf) 356 { 357 if (nf && refcount_inc_not_zero(&nf->nf_ref)) 358 return nf; 359 return NULL; 360 } 361 362 /** 363 * nfsd_file_put - put the reference to a nfsd_file 364 * @nf: nfsd_file of which to put the reference 365 * 366 * Put a reference to a nfsd_file. In the non-GC case, we just put the 367 * reference immediately. In the GC case, if the reference would be 368 * the last one, the put it on the LRU instead to be cleaned up later. 369 */ 370 void 371 nfsd_file_put(struct nfsd_file *nf) 372 { 373 might_sleep(); 374 trace_nfsd_file_put(nf); 375 376 if (test_bit(NFSD_FILE_GC, &nf->nf_flags) && 377 test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 378 set_bit(NFSD_FILE_REFERENCED, &nf->nf_flags); 379 set_bit(NFSD_FILE_RECENT, &nf->nf_flags); 380 } 381 382 if (refcount_dec_and_test(&nf->nf_ref)) 383 nfsd_file_free(nf); 384 } 385 386 /** 387 * nfsd_file_put_local - put nfsd_file reference and arm nfsd_net_put in caller 388 * @pnf: nfsd_file of which to put the reference 389 * 390 * First save the associated net to return to caller, then put 391 * the reference of the nfsd_file. 392 */ 393 struct net * 394 nfsd_file_put_local(struct nfsd_file __rcu **pnf) 395 { 396 struct nfsd_file *nf; 397 struct net *net = NULL; 398 399 nf = unrcu_pointer(xchg(pnf, NULL)); 400 if (nf) { 401 net = nf->nf_net; 402 nfsd_file_put(nf); 403 } 404 return net; 405 } 406 407 /** 408 * nfsd_file_file - get the backing file of an nfsd_file 409 * @nf: nfsd_file of which to access the backing file. 410 * 411 * Return backing file for @nf. 412 */ 413 struct file * 414 nfsd_file_file(struct nfsd_file *nf) 415 { 416 return nf->nf_file; 417 } 418 419 static void 420 nfsd_file_dispose_list(struct list_head *dispose) 421 { 422 struct nfsd_file *nf; 423 424 while (!list_empty(dispose)) { 425 nf = list_first_entry(dispose, struct nfsd_file, nf_gc); 426 list_del_init(&nf->nf_gc); 427 nfsd_file_free(nf); 428 } 429 } 430 431 /** 432 * nfsd_file_dispose_list_delayed - queue dead files for nfsd thread disposal 433 * @dispose: list of nfsd_files to be disposed 434 * 435 * Transfers each file to the dispose list in its nfsd_net and wakes an nfsd 436 * thread to do the actual close. This keeps the cost of fput() in the nfsd 437 * threads rather than in the shrinker or GC worker. 438 * 439 * All callers must hold nfsd_gc_lock, so that nfsd_file_cache_shutdown_net() 440 * can synchronize against them before draining the per-net dispose list. 441 * This guarantees nf_net is still live when we call net_generic(). 442 */ 443 static void 444 nfsd_file_dispose_list_delayed(struct list_head *dispose) 445 { 446 lockdep_assert_held(&nfsd_gc_lock); 447 448 while (!list_empty(dispose)) { 449 struct nfsd_file *nf = list_first_entry(dispose, 450 struct nfsd_file, nf_gc); 451 struct nfsd_net *nn = net_generic(nf->nf_net, nfsd_net_id); 452 struct svc_serv *serv; 453 454 spin_lock(&nn->fcache_dispose_lock); 455 list_move_tail(&nf->nf_gc, &nn->fcache_dispose_list); 456 spin_unlock(&nn->fcache_dispose_lock); 457 458 /* 459 * The filecache laundrette is shut down after the 460 * nn->nfsd_serv pointer is cleared, but before the 461 * svc_serv is freed. 462 */ 463 serv = nn->nfsd_serv; 464 if (serv) 465 svc_wake_up(serv); 466 } 467 } 468 469 /** 470 * nfsd_file_net_dispose - deal with nfsd_files waiting to be disposed. 471 * @nn: nfsd_net in which to find files to be disposed. 472 * 473 * When files held open for nfsv3 are removed from the filecache, whether 474 * due to memory pressure or garbage collection, they are queued to 475 * a per-net-ns queue. This function completes the disposal, either 476 * directly or by waking another nfsd thread to help with the work. 477 */ 478 void nfsd_file_net_dispose(struct nfsd_net *nn) 479 { 480 if (!list_empty(&nn->fcache_dispose_list)) { 481 LIST_HEAD(dispose); 482 int i; 483 484 spin_lock(&nn->fcache_dispose_lock); 485 for (i = 0; i < 8 && !list_empty(&nn->fcache_dispose_list); i++) 486 list_move(nn->fcache_dispose_list.next, &dispose); 487 spin_unlock(&nn->fcache_dispose_lock); 488 if (!list_empty(&nn->fcache_dispose_list)) { 489 /* 490 * Wake up another thread to share the work 491 * *before* doing any actual disposing. 492 * 493 * The filecache laundrette is shut down after 494 * the nn->nfsd_serv pointer is cleared, but 495 * before the svc_serv is freed. 496 */ 497 struct svc_serv *serv = nn->nfsd_serv; 498 499 if (serv) 500 svc_wake_up(serv); 501 } 502 nfsd_file_dispose_list(&dispose); 503 } 504 } 505 506 /** 507 * nfsd_file_lru_cb - Examine an entry on the LRU list 508 * @item: LRU entry to examine 509 * @lru: controlling LRU 510 * @arg: dispose list 511 * 512 * Return values: 513 * %LRU_REMOVED: @item was removed from the LRU 514 * %LRU_ROTATE: @item is to be moved to the LRU tail 515 * %LRU_SKIP: @item cannot be evicted 516 */ 517 static enum lru_status 518 nfsd_file_lru_cb(struct list_head *item, struct list_lru_one *lru, 519 void *arg) 520 { 521 struct list_head *head = arg; 522 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru); 523 524 /* We should only be dealing with GC entries here */ 525 WARN_ON_ONCE(!test_bit(NFSD_FILE_GC, &nf->nf_flags)); 526 527 /* 528 * Don't throw out files that are still undergoing I/O or 529 * that have uncleared errors pending. 530 */ 531 if (nfsd_file_check_writeback(nf)) { 532 trace_nfsd_file_gc_writeback(nf); 533 return LRU_SKIP; 534 } 535 536 /* If it was recently added to the list, skip it */ 537 if (test_and_clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags)) { 538 trace_nfsd_file_gc_referenced(nf); 539 return LRU_ROTATE; 540 } 541 542 /* 543 * Put the reference held on behalf of the LRU if it is the last 544 * reference, else rotate. 545 */ 546 if (!refcount_dec_if_one(&nf->nf_ref)) { 547 trace_nfsd_file_gc_in_use(nf); 548 return LRU_ROTATE; 549 } 550 551 /* Refcount went to zero. Unhash it and queue it to the dispose list */ 552 nfsd_file_unhash(nf); 553 list_lru_isolate(lru, &nf->nf_lru); 554 list_add(&nf->nf_gc, head); 555 this_cpu_inc(nfsd_file_evictions); 556 trace_nfsd_file_gc_disposed(nf); 557 return LRU_REMOVED; 558 } 559 560 static enum lru_status 561 nfsd_file_gc_cb(struct list_head *item, struct list_lru_one *lru, 562 void *arg) 563 { 564 struct nfsd_file *nf = list_entry(item, struct nfsd_file, nf_lru); 565 566 if (test_and_clear_bit(NFSD_FILE_RECENT, &nf->nf_flags)) { 567 /* 568 * "REFERENCED" really means "should be at the end of the 569 * LRU. As we are putting it there we can clear the flag. 570 */ 571 clear_bit(NFSD_FILE_REFERENCED, &nf->nf_flags); 572 trace_nfsd_file_gc_aged(nf); 573 return LRU_ROTATE; 574 } 575 return nfsd_file_lru_cb(item, lru, arg); 576 } 577 578 static void 579 nfsd_file_gc(void) 580 { 581 unsigned long ret = 0; 582 LIST_HEAD(dispose); 583 int nid; 584 585 spin_lock(&nfsd_gc_lock); 586 for_each_node_state(nid, N_NORMAL_MEMORY) { 587 unsigned long remaining = list_lru_count_node(&nfsd_file_lru, nid); 588 589 while (remaining > 0) { 590 unsigned long nr = min(remaining, NFSD_FILE_GC_BATCH); 591 592 remaining -= nr; 593 ret += list_lru_walk_node(&nfsd_file_lru, nid, nfsd_file_gc_cb, 594 &dispose, &nr); 595 if (nr) 596 /* walk aborted early */ 597 remaining = 0; 598 } 599 } 600 nfsd_file_dispose_list_delayed(&dispose); 601 spin_unlock(&nfsd_gc_lock); 602 trace_nfsd_file_gc_removed(ret, list_lru_count(&nfsd_file_lru)); 603 } 604 605 static void 606 nfsd_file_gc_worker(struct work_struct *work) 607 { 608 if (list_lru_count(&nfsd_file_lru)) 609 nfsd_file_gc(); 610 nfsd_file_schedule_laundrette(); 611 } 612 613 static unsigned long 614 nfsd_file_lru_count(struct shrinker *s, struct shrink_control *sc) 615 { 616 return list_lru_count(&nfsd_file_lru); 617 } 618 619 static unsigned long 620 nfsd_file_lru_scan(struct shrinker *s, struct shrink_control *sc) 621 { 622 LIST_HEAD(dispose); 623 unsigned long ret; 624 625 if (!spin_trylock(&nfsd_gc_lock)) 626 return SHRINK_STOP; 627 628 ret = list_lru_shrink_walk(&nfsd_file_lru, sc, 629 nfsd_file_lru_cb, &dispose); 630 nfsd_file_dispose_list_delayed(&dispose); 631 spin_unlock(&nfsd_gc_lock); 632 trace_nfsd_file_shrinker_removed(ret, list_lru_count(&nfsd_file_lru)); 633 return ret; 634 } 635 636 static struct shrinker *nfsd_file_shrinker; 637 638 /** 639 * nfsd_file_cond_queue - conditionally unhash and queue a nfsd_file 640 * @nf: nfsd_file to attempt to queue 641 * @dispose: private list to queue successfully-put objects 642 * 643 * Unhash an nfsd_file, try to get a reference to it, and then put that 644 * reference. If it's the last reference, queue it to the dispose list. 645 */ 646 static void 647 nfsd_file_cond_queue(struct nfsd_file *nf, struct list_head *dispose) 648 __must_hold(RCU) 649 { 650 int decrement = 1; 651 652 /* If we raced with someone else unhashing, ignore it */ 653 if (!nfsd_file_unhash(nf)) 654 return; 655 656 /* If we can't get a reference, ignore it */ 657 if (!nfsd_file_get(nf)) 658 return; 659 660 /* Extra decrement if we remove from the LRU */ 661 if (nfsd_file_lru_remove(nf)) 662 ++decrement; 663 664 /* If refcount goes to 0, then put on the dispose list */ 665 if (refcount_sub_and_test(decrement, &nf->nf_ref)) { 666 list_add(&nf->nf_gc, dispose); 667 trace_nfsd_file_closing(nf); 668 } 669 } 670 671 /** 672 * nfsd_file_queue_for_close: try to close out any open nfsd_files for an inode 673 * @inode: inode on which to close out nfsd_files 674 * @dispose: list on which to gather nfsd_files to close out 675 * 676 * An nfsd_file represents a struct file being held open on behalf of nfsd. 677 * An open file however can block other activity (such as leases), or cause 678 * undesirable behavior (e.g. spurious silly-renames when reexporting NFS). 679 * 680 * This function is intended to find open nfsd_files when this sort of 681 * conflicting access occurs and then attempt to close those files out. 682 * 683 * Populates the dispose list with entries that have already had their 684 * refcounts go to zero. The actual free of an nfsd_file can be expensive, 685 * so we leave it up to the caller whether it wants to wait or not. 686 */ 687 static void 688 nfsd_file_queue_for_close(struct inode *inode, struct list_head *dispose) 689 { 690 struct rhlist_head *tmp, *list; 691 struct nfsd_file *nf; 692 693 rcu_read_lock(); 694 list = rhltable_lookup(&nfsd_file_rhltable, &inode, 695 nfsd_file_rhash_params); 696 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) { 697 if (!test_bit(NFSD_FILE_GC, &nf->nf_flags)) 698 continue; 699 nfsd_file_cond_queue(nf, dispose); 700 } 701 rcu_read_unlock(); 702 } 703 704 /** 705 * nfsd_file_close_inode - attempt a deferred close of a nfsd_file 706 * @inode: inode of the file to attempt to remove 707 * 708 * Close out any open nfsd_files that can be reaped for @inode. The 709 * actual freeing is deferred to the nfsd service threads. 710 * 711 * This is used by the fsnotify callbacks and setlease notifier. 712 */ 713 static void 714 nfsd_file_close_inode(struct inode *inode) 715 { 716 LIST_HEAD(dispose); 717 718 spin_lock(&nfsd_gc_lock); 719 nfsd_file_queue_for_close(inode, &dispose); 720 nfsd_file_dispose_list_delayed(&dispose); 721 spin_unlock(&nfsd_gc_lock); 722 } 723 724 /** 725 * nfsd_file_close_inode_sync - attempt to forcibly close a nfsd_file 726 * @inode: inode of the file to attempt to remove 727 * 728 * Close out any open nfsd_files that can be reaped for @inode. The 729 * nfsd_files are closed out synchronously. 730 * 731 * This is called from nfsd_rename and nfsd_unlink to avoid silly-renames 732 * when reexporting NFS. 733 */ 734 void 735 nfsd_file_close_inode_sync(struct inode *inode) 736 { 737 LIST_HEAD(dispose); 738 739 trace_nfsd_file_close(inode); 740 741 nfsd_file_queue_for_close(inode, &dispose); 742 nfsd_file_dispose_list(&dispose); 743 } 744 745 /** 746 * nfsd_file_close_export - close cached file handles for an export 747 * @net: net namespace in which to operate 748 * @path: export path whose cached files should be closed 749 * 750 * Close out GC-managed nfsd_file entries whose underlying file is on 751 * the same filesystem as, and a descendant of, @path. nfsd_file 752 * entries do not carry an export reference, so the check uses the 753 * file's dentry ancestry. False positives (closing a cached handle 754 * that did not originate from the target export) are harmless -- the 755 * handle is simply reopened on the next access. 756 * 757 * Called from the NFSD_CMD_UNLOCK_EXPORT handler before revoking 758 * NFSv4 state, to ensure that cached file handles do not hold the 759 * filesystem busy. 760 */ 761 void nfsd_file_close_export(struct net *net, const struct path *path) 762 { 763 struct rhashtable_iter iter; 764 struct nfsd_file *nf; 765 LIST_HEAD(dispose); 766 767 rhltable_walk_enter(&nfsd_file_rhltable, &iter); 768 do { 769 rhashtable_walk_start(&iter); 770 771 nf = rhashtable_walk_next(&iter); 772 while (!IS_ERR_OR_NULL(nf)) { 773 if (nf->nf_net == net && 774 test_bit(NFSD_FILE_GC, &nf->nf_flags) && 775 nf->nf_file && 776 file_inode(nf->nf_file)->i_sb == 777 path->dentry->d_sb && 778 is_subdir(nf->nf_file->f_path.dentry, 779 path->dentry)) 780 nfsd_file_cond_queue(nf, &dispose); 781 nf = rhashtable_walk_next(&iter); 782 } 783 784 rhashtable_walk_stop(&iter); 785 } while (nf == ERR_PTR(-EAGAIN)); 786 rhashtable_walk_exit(&iter); 787 788 nfsd_file_dispose_list(&dispose); 789 } 790 791 static int 792 nfsd_file_lease_notifier_call(struct notifier_block *nb, unsigned long arg, 793 void *data) 794 { 795 struct file_lease *fl = data; 796 797 /* Only close files for F_SETLEASE leases */ 798 if (fl->c.flc_flags & FL_LEASE) 799 nfsd_file_close_inode(file_inode(fl->c.flc_file)); 800 return 0; 801 } 802 803 static struct notifier_block nfsd_file_lease_notifier = { 804 .notifier_call = nfsd_file_lease_notifier_call, 805 }; 806 807 static int 808 nfsd_file_fsnotify_handle_event(struct fsnotify_mark *mark, u32 mask, 809 struct inode *inode, struct inode *dir, 810 const struct qstr *name, u32 cookie) 811 { 812 if (WARN_ON_ONCE(!inode)) 813 return 0; 814 815 trace_nfsd_file_fsnotify_handle_event(inode, mask); 816 817 /* Should be no marks on non-regular files */ 818 if (!S_ISREG(inode->i_mode)) { 819 WARN_ON_ONCE(1); 820 return 0; 821 } 822 823 /* don't close files if this was not the last link */ 824 if (mask & FS_ATTRIB) { 825 if (inode->i_nlink) 826 return 0; 827 } 828 829 nfsd_file_close_inode(inode); 830 return 0; 831 } 832 833 834 static const struct fsnotify_ops nfsd_file_fsnotify_ops = { 835 .handle_inode_event = nfsd_file_fsnotify_handle_event, 836 .free_mark = nfsd_file_mark_free, 837 }; 838 839 int 840 nfsd_file_cache_init(void) 841 { 842 int ret; 843 844 lockdep_assert_held(&nfsd_mutex); 845 if (test_and_set_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) 846 return 0; 847 848 ret = rhltable_init(&nfsd_file_rhltable, &nfsd_file_rhash_params); 849 if (ret) 850 goto out; 851 852 ret = -ENOMEM; 853 nfsd_file_slab = KMEM_CACHE(nfsd_file, 0); 854 if (!nfsd_file_slab) { 855 pr_err("nfsd: unable to create nfsd_file_slab\n"); 856 goto out_err; 857 } 858 859 nfsd_file_mark_slab = KMEM_CACHE(nfsd_file_mark, 0); 860 if (!nfsd_file_mark_slab) { 861 pr_err("nfsd: unable to create nfsd_file_mark_slab\n"); 862 goto out_err; 863 } 864 865 ret = list_lru_init(&nfsd_file_lru); 866 if (ret) { 867 pr_err("nfsd: failed to init nfsd_file_lru: %d\n", ret); 868 goto out_err; 869 } 870 871 nfsd_file_shrinker = shrinker_alloc(0, "nfsd-filecache"); 872 if (!nfsd_file_shrinker) { 873 ret = -ENOMEM; 874 pr_err("nfsd: failed to allocate nfsd_file_shrinker\n"); 875 goto out_lru; 876 } 877 878 nfsd_file_shrinker->count_objects = nfsd_file_lru_count; 879 nfsd_file_shrinker->scan_objects = nfsd_file_lru_scan; 880 nfsd_file_shrinker->seeks = 1; 881 882 shrinker_register(nfsd_file_shrinker); 883 884 ret = lease_register_notifier(&nfsd_file_lease_notifier); 885 if (ret) { 886 pr_err("nfsd: unable to register lease notifier: %d\n", ret); 887 goto out_shrinker; 888 } 889 890 nfsd_file_fsnotify_group = fsnotify_alloc_group(&nfsd_file_fsnotify_ops, 891 0); 892 if (IS_ERR(nfsd_file_fsnotify_group)) { 893 pr_err("nfsd: unable to create fsnotify group: %ld\n", 894 PTR_ERR(nfsd_file_fsnotify_group)); 895 ret = PTR_ERR(nfsd_file_fsnotify_group); 896 nfsd_file_fsnotify_group = NULL; 897 goto out_notifier; 898 } 899 900 INIT_DELAYED_WORK(&nfsd_filecache_laundrette, nfsd_file_gc_worker); 901 out: 902 if (ret) 903 clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags); 904 return ret; 905 out_notifier: 906 lease_unregister_notifier(&nfsd_file_lease_notifier); 907 out_shrinker: 908 shrinker_free(nfsd_file_shrinker); 909 out_lru: 910 list_lru_destroy(&nfsd_file_lru); 911 out_err: 912 kmem_cache_destroy(nfsd_file_slab); 913 nfsd_file_slab = NULL; 914 kmem_cache_destroy(nfsd_file_mark_slab); 915 nfsd_file_mark_slab = NULL; 916 rhltable_destroy(&nfsd_file_rhltable); 917 goto out; 918 } 919 920 /** 921 * __nfsd_file_cache_purge: clean out the cache for shutdown 922 * @net: net-namespace to shut down the cache (may be NULL) 923 * 924 * Walk the nfsd_file cache and close out any that match @net. If @net is NULL, 925 * then close out everything. Called when an nfsd instance is being shut down, 926 * and when the exports table is flushed. 927 */ 928 static void 929 __nfsd_file_cache_purge(struct net *net) 930 { 931 struct rhashtable_iter iter; 932 struct nfsd_file *nf; 933 LIST_HEAD(dispose); 934 935 #if IS_ENABLED(CONFIG_NFS_LOCALIO) 936 if (net) { 937 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 938 nfs_localio_invalidate_clients(&nn->local_clients, 939 &nn->local_clients_lock); 940 } 941 #endif 942 943 rhltable_walk_enter(&nfsd_file_rhltable, &iter); 944 do { 945 rhashtable_walk_start(&iter); 946 947 nf = rhashtable_walk_next(&iter); 948 while (!IS_ERR_OR_NULL(nf)) { 949 if (!net || nf->nf_net == net) 950 nfsd_file_cond_queue(nf, &dispose); 951 nf = rhashtable_walk_next(&iter); 952 } 953 954 rhashtable_walk_stop(&iter); 955 } while (nf == ERR_PTR(-EAGAIN)); 956 rhashtable_walk_exit(&iter); 957 958 nfsd_file_dispose_list(&dispose); 959 } 960 961 int 962 nfsd_file_cache_start_net(struct net *net) 963 { 964 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 965 966 spin_lock_init(&nn->fcache_dispose_lock); 967 INIT_LIST_HEAD(&nn->fcache_dispose_list); 968 return 0; 969 } 970 971 /** 972 * nfsd_file_cache_purge - Remove all cache items associated with @net 973 * @net: target net namespace 974 * 975 */ 976 void 977 nfsd_file_cache_purge(struct net *net) 978 { 979 lockdep_assert_held(&nfsd_mutex); 980 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) 981 __nfsd_file_cache_purge(net); 982 } 983 984 void 985 nfsd_file_cache_shutdown_net(struct net *net) 986 { 987 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 988 989 nfsd_file_cache_purge(net); 990 /* 991 * Ensure any in-progress shrinker, GC, or fsnotify/lease callback 992 * (all of which hold nfsd_gc_lock while calling 993 * nfsd_file_dispose_list_delayed()) has fully completed before 994 * draining the per-net dispose list. 995 */ 996 spin_lock(&nfsd_gc_lock); 997 spin_unlock(&nfsd_gc_lock); 998 nfsd_file_dispose_list(&nn->fcache_dispose_list); 999 } 1000 1001 void 1002 nfsd_file_cache_shutdown(void) 1003 { 1004 int i; 1005 1006 lockdep_assert_held(&nfsd_mutex); 1007 if (test_and_clear_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 0) 1008 return; 1009 1010 lease_unregister_notifier(&nfsd_file_lease_notifier); 1011 shrinker_free(nfsd_file_shrinker); 1012 /* 1013 * make sure all callers of nfsd_file_lru_cb are done before 1014 * calling nfsd_file_cache_purge 1015 */ 1016 cancel_delayed_work_sync(&nfsd_filecache_laundrette); 1017 __nfsd_file_cache_purge(NULL); 1018 list_lru_destroy(&nfsd_file_lru); 1019 rcu_barrier(); 1020 fsnotify_put_group(nfsd_file_fsnotify_group); 1021 nfsd_file_fsnotify_group = NULL; 1022 kmem_cache_destroy(nfsd_file_slab); 1023 nfsd_file_slab = NULL; 1024 fsnotify_wait_marks_destroyed(); 1025 kmem_cache_destroy(nfsd_file_mark_slab); 1026 nfsd_file_mark_slab = NULL; 1027 rhltable_destroy(&nfsd_file_rhltable); 1028 1029 for_each_possible_cpu(i) { 1030 per_cpu(nfsd_file_cache_hits, i) = 0; 1031 per_cpu(nfsd_file_acquisitions, i) = 0; 1032 per_cpu(nfsd_file_allocations, i) = 0; 1033 per_cpu(nfsd_file_releases, i) = 0; 1034 per_cpu(nfsd_file_total_age, i) = 0; 1035 per_cpu(nfsd_file_evictions, i) = 0; 1036 } 1037 } 1038 1039 static struct nfsd_file * 1040 nfsd_file_lookup_locked(const struct net *net, const struct cred *cred, 1041 struct inode *inode, unsigned char need, 1042 bool want_gc) 1043 { 1044 struct rhlist_head *tmp, *list; 1045 struct nfsd_file *nf; 1046 1047 list = rhltable_lookup(&nfsd_file_rhltable, &inode, 1048 nfsd_file_rhash_params); 1049 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) { 1050 if (nf->nf_may != need) 1051 continue; 1052 if (nf->nf_net != net) 1053 continue; 1054 if (!nfsd_match_cred(nf->nf_cred, cred)) 1055 continue; 1056 if (test_bit(NFSD_FILE_GC, &nf->nf_flags) != want_gc) 1057 continue; 1058 if (test_bit(NFSD_FILE_HASHED, &nf->nf_flags) == 0) 1059 continue; 1060 1061 if (!nfsd_file_get(nf)) 1062 continue; 1063 return nf; 1064 } 1065 return NULL; 1066 } 1067 1068 /** 1069 * nfsd_file_is_cached - are there any cached open files for this inode? 1070 * @inode: inode to check 1071 * 1072 * The lookup matches inodes in all net namespaces and is atomic wrt 1073 * nfsd_file_acquire(). 1074 * 1075 * Return values: 1076 * %true: filecache contains at least one file matching this inode 1077 * %false: filecache contains no files matching this inode 1078 */ 1079 bool 1080 nfsd_file_is_cached(struct inode *inode) 1081 { 1082 struct rhlist_head *tmp, *list; 1083 struct nfsd_file *nf; 1084 bool ret = false; 1085 1086 rcu_read_lock(); 1087 list = rhltable_lookup(&nfsd_file_rhltable, &inode, 1088 nfsd_file_rhash_params); 1089 rhl_for_each_entry_rcu(nf, tmp, list, nf_rlist) 1090 if (test_bit(NFSD_FILE_GC, &nf->nf_flags)) { 1091 ret = true; 1092 break; 1093 } 1094 rcu_read_unlock(); 1095 1096 trace_nfsd_file_is_cached(inode, (int)ret); 1097 return ret; 1098 } 1099 1100 static __be32 1101 nfsd_file_get_dio_attrs(const struct svc_fh *fhp, struct nfsd_file *nf) 1102 { 1103 struct inode *inode = file_inode(nf->nf_file); 1104 struct kstat stat; 1105 __be32 status; 1106 1107 /* Currently only need to get DIO alignment info for regular files */ 1108 if (!S_ISREG(inode->i_mode)) 1109 return nfs_ok; 1110 1111 status = fh_getattr(fhp, &stat); 1112 if (status != nfs_ok) 1113 return status; 1114 1115 trace_nfsd_file_get_dio_attrs(inode, &stat); 1116 1117 if (stat.result_mask & STATX_DIOALIGN) { 1118 nf->nf_dio_mem_align = stat.dio_mem_align; 1119 nf->nf_dio_offset_align = stat.dio_offset_align; 1120 } 1121 if (stat.result_mask & STATX_DIO_READ_ALIGN) 1122 nf->nf_dio_read_offset_align = stat.dio_read_offset_align; 1123 else 1124 nf->nf_dio_read_offset_align = nf->nf_dio_offset_align; 1125 1126 return nfs_ok; 1127 } 1128 1129 static __be32 1130 nfsd_file_do_acquire(struct svc_rqst *rqstp, struct net *net, 1131 struct svc_cred *cred, 1132 struct auth_domain *client, 1133 struct svc_fh *fhp, 1134 unsigned int may_flags, struct file *file, 1135 umode_t type, bool want_gc, struct nfsd_file **pnf) 1136 { 1137 unsigned char need = may_flags & NFSD_FILE_MAY_MASK; 1138 struct nfsd_file *new, *nf; 1139 bool stale_retry = true; 1140 bool open_retry = true; 1141 struct inode *inode; 1142 __be32 status; 1143 int ret; 1144 1145 retry: 1146 if (rqstp) 1147 status = fh_verify(rqstp, fhp, type, 1148 may_flags|NFSD_MAY_OWNER_OVERRIDE); 1149 else 1150 status = fh_verify_local(net, cred, client, fhp, type, 1151 may_flags|NFSD_MAY_OWNER_OVERRIDE); 1152 1153 if (status != nfs_ok) 1154 return status; 1155 inode = d_inode(fhp->fh_dentry); 1156 1157 rcu_read_lock(); 1158 nf = nfsd_file_lookup_locked(net, current_cred(), inode, need, want_gc); 1159 rcu_read_unlock(); 1160 1161 if (nf) 1162 goto wait_for_construction; 1163 1164 new = nfsd_file_alloc(net, inode, need, want_gc); 1165 if (!new) { 1166 status = nfserr_jukebox; 1167 goto out; 1168 } 1169 1170 rcu_read_lock(); 1171 spin_lock(&inode->i_lock); 1172 nf = nfsd_file_lookup_locked(net, current_cred(), inode, need, want_gc); 1173 if (unlikely(nf)) { 1174 spin_unlock(&inode->i_lock); 1175 rcu_read_unlock(); 1176 nfsd_file_free(new); 1177 goto wait_for_construction; 1178 } 1179 nf = new; 1180 ret = rhltable_insert(&nfsd_file_rhltable, &nf->nf_rlist, 1181 nfsd_file_rhash_params); 1182 spin_unlock(&inode->i_lock); 1183 rcu_read_unlock(); 1184 if (likely(ret == 0)) 1185 goto open_file; 1186 1187 trace_nfsd_file_insert_err(rqstp, inode, may_flags, ret); 1188 status = nfserr_jukebox; 1189 goto construction_err; 1190 1191 wait_for_construction: 1192 wait_on_bit(&nf->nf_flags, NFSD_FILE_PENDING, TASK_UNINTERRUPTIBLE); 1193 1194 /* Did construction of this file fail? */ 1195 if (!test_bit(NFSD_FILE_HASHED, &nf->nf_flags)) { 1196 trace_nfsd_file_cons_err(rqstp, inode, may_flags, nf); 1197 if (!open_retry) { 1198 status = nfserr_jukebox; 1199 goto construction_err; 1200 } 1201 nfsd_file_put(nf); 1202 open_retry = false; 1203 fh_put(fhp); 1204 goto retry; 1205 } 1206 this_cpu_inc(nfsd_file_cache_hits); 1207 1208 status = nfserrno(nfsd_open_break_lease(file_inode(nf->nf_file), may_flags)); 1209 if (status != nfs_ok) { 1210 nfsd_file_put(nf); 1211 nf = NULL; 1212 } 1213 1214 out: 1215 if (status == nfs_ok) { 1216 this_cpu_inc(nfsd_file_acquisitions); 1217 nfsd_file_check_write_error(nf); 1218 *pnf = nf; 1219 } 1220 trace_nfsd_file_acquire(rqstp, inode, may_flags, nf, status); 1221 return status; 1222 1223 open_file: 1224 trace_nfsd_file_alloc(nf); 1225 1226 if (type == S_IFREG) 1227 nf->nf_mark = nfsd_file_mark_find_or_create(inode); 1228 1229 if (type != S_IFREG || nf->nf_mark) { 1230 if (file && (file->f_mode & FMODE_OPENED)) { 1231 get_file(file); 1232 nf->nf_file = file; 1233 status = nfs_ok; 1234 trace_nfsd_file_opened(nf, status); 1235 } else { 1236 ret = nfsd_open_verified(fhp, type, may_flags, &nf->nf_file); 1237 if (ret == -EOPENSTALE && stale_retry) { 1238 stale_retry = false; 1239 nfsd_file_unhash(nf); 1240 clear_and_wake_up_bit(NFSD_FILE_PENDING, 1241 &nf->nf_flags); 1242 if (refcount_dec_and_test(&nf->nf_ref)) 1243 nfsd_file_free(nf); 1244 nf = NULL; 1245 fh_put(fhp); 1246 goto retry; 1247 } 1248 status = nfserrno(ret); 1249 trace_nfsd_file_open(nf, status); 1250 if (status == nfs_ok) 1251 status = nfsd_file_get_dio_attrs(fhp, nf); 1252 } 1253 } else 1254 status = nfserr_jukebox; 1255 /* 1256 * If construction failed, or we raced with a call to unlink() 1257 * then unhash. 1258 */ 1259 if (status != nfs_ok || inode->i_nlink == 0) 1260 nfsd_file_unhash(nf); 1261 else if (want_gc) 1262 nfsd_file_lru_add(nf); 1263 1264 clear_and_wake_up_bit(NFSD_FILE_PENDING, &nf->nf_flags); 1265 if (status == nfs_ok) 1266 goto out; 1267 1268 construction_err: 1269 if (refcount_dec_and_test(&nf->nf_ref)) 1270 nfsd_file_free(nf); 1271 nf = NULL; 1272 goto out; 1273 } 1274 1275 /** 1276 * nfsd_file_acquire_gc - Get a struct nfsd_file with an open file 1277 * @rqstp: the RPC transaction being executed 1278 * @fhp: the NFS filehandle of the file to be opened 1279 * @may_flags: NFSD_MAY_ settings for the file 1280 * @pnf: OUT: new or found "struct nfsd_file" object 1281 * 1282 * The nfsd_file object returned by this API is reference-counted 1283 * and garbage-collected. The object is retained for a few 1284 * seconds after the final nfsd_file_put() in case the caller 1285 * wants to re-use it. 1286 * 1287 * Return values: 1288 * %nfs_ok - @pnf points to an nfsd_file with its reference 1289 * count boosted. 1290 * 1291 * On error, an nfsstat value in network byte order is returned. 1292 */ 1293 __be32 1294 nfsd_file_acquire_gc(struct svc_rqst *rqstp, struct svc_fh *fhp, 1295 unsigned int may_flags, struct nfsd_file **pnf) 1296 { 1297 return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL, 1298 fhp, may_flags, NULL, S_IFREG, true, pnf); 1299 } 1300 1301 /** 1302 * nfsd_file_acquire - Get a struct nfsd_file with an open file 1303 * @rqstp: the RPC transaction being executed 1304 * @fhp: the NFS filehandle of the file to be opened 1305 * @may_flags: NFSD_MAY_ settings for the file 1306 * @pnf: OUT: new or found "struct nfsd_file" object 1307 * 1308 * The nfsd_file_object returned by this API is reference-counted 1309 * but not garbage-collected. The object is unhashed after the 1310 * final nfsd_file_put(). 1311 * 1312 * Return values: 1313 * %nfs_ok - @pnf points to an nfsd_file with its reference 1314 * count boosted. 1315 * 1316 * On error, an nfsstat value in network byte order is returned. 1317 */ 1318 __be32 1319 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, 1320 unsigned int may_flags, struct nfsd_file **pnf) 1321 { 1322 return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL, 1323 fhp, may_flags, NULL, S_IFREG, false, pnf); 1324 } 1325 1326 /** 1327 * nfsd_file_acquire_local - Get a struct nfsd_file with an open file for localio 1328 * @net: The network namespace in which to perform a lookup 1329 * @cred: the user credential with which to validate access 1330 * @client: the auth_domain for LOCALIO lookup 1331 * @fhp: the NFS filehandle of the file to be opened 1332 * @may_flags: NFSD_MAY_ settings for the file 1333 * @pnf: OUT: new or found "struct nfsd_file" object 1334 * 1335 * This file lookup interface provide access to a file given the 1336 * filehandle and credential. No connection-based authorisation 1337 * is performed and in that way it is quite different to other 1338 * file access mediated by nfsd. It allows a kernel module such as the NFS 1339 * client to reach across network and filesystem namespaces to access 1340 * a file. The security implications of this should be carefully 1341 * considered before use. 1342 * 1343 * The nfsd_file_object returned by this API is reference-counted 1344 * but not garbage-collected. The object is unhashed after the 1345 * final nfsd_file_put(). 1346 * 1347 * Return values: 1348 * %nfs_ok - @pnf points to an nfsd_file with its reference 1349 * count boosted. 1350 * 1351 * On error, an nfsstat value in network byte order is returned. 1352 */ 1353 __be32 1354 nfsd_file_acquire_local(struct net *net, struct svc_cred *cred, 1355 struct auth_domain *client, struct svc_fh *fhp, 1356 unsigned int may_flags, struct nfsd_file **pnf) 1357 { 1358 /* 1359 * Save creds before calling nfsd_file_do_acquire() (which calls 1360 * nfsd_setuser). Important because caller (LOCALIO) is from 1361 * client context. 1362 */ 1363 const struct cred *save_cred = get_current_cred(); 1364 __be32 beres; 1365 1366 beres = nfsd_file_do_acquire(NULL, net, cred, client, fhp, may_flags, 1367 NULL, S_IFREG, false, pnf); 1368 put_cred(revert_creds(save_cred)); 1369 return beres; 1370 } 1371 1372 /** 1373 * nfsd_file_acquire_opened - Get a struct nfsd_file using existing open file 1374 * @rqstp: the RPC transaction being executed 1375 * @fhp: the NFS filehandle of the file just created 1376 * @may_flags: NFSD_MAY_ settings for the file 1377 * @file: cached, already-open file (may be NULL or not yet opened) 1378 * @pnf: OUT: new or found "struct nfsd_file" object 1379 * 1380 * Acquire a nfsd_file object that is not GC'ed. If one doesn't already exist, 1381 * and @file has FMODE_OPENED set, use it to instantiate a new nfsd_file 1382 * instead of opening a new one. 1383 * 1384 * Return values: 1385 * %nfs_ok - @pnf points to an nfsd_file with its reference 1386 * count boosted. 1387 * 1388 * On error, an nfsstat value in network byte order is returned. 1389 */ 1390 __be32 1391 nfsd_file_acquire_opened(struct svc_rqst *rqstp, struct svc_fh *fhp, 1392 unsigned int may_flags, struct file *file, 1393 struct nfsd_file **pnf) 1394 { 1395 return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL, 1396 fhp, may_flags, file, S_IFREG, false, pnf); 1397 } 1398 1399 /** 1400 * nfsd_file_acquire_dir - Get a struct nfsd_file with an open directory 1401 * @rqstp: the RPC transaction being executed 1402 * @fhp: the NFS filehandle of the file to be opened 1403 * @pnf: OUT: new or found "struct nfsd_file" object 1404 * 1405 * The nfsd_file_object returned by this API is reference-counted 1406 * but not garbage-collected. The object is unhashed after the 1407 * final nfsd_file_put(). This opens directories only, and only 1408 * in O_RDONLY mode. 1409 * 1410 * Return values: 1411 * %nfs_ok - @pnf points to an nfsd_file with its reference 1412 * count boosted. 1413 * 1414 * On error, an nfsstat value in network byte order is returned. 1415 */ 1416 __be32 1417 nfsd_file_acquire_dir(struct svc_rqst *rqstp, struct svc_fh *fhp, 1418 struct nfsd_file **pnf) 1419 { 1420 return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL, fhp, 1421 NFSD_MAY_READ|NFSD_MAY_64BIT_COOKIE, 1422 NULL, S_IFDIR, false, pnf); 1423 } 1424 1425 /* 1426 * Note that fields may be added, removed or reordered in the future. Programs 1427 * scraping this file for info should test the labels to ensure they're 1428 * getting the correct field. 1429 */ 1430 int nfsd_file_cache_stats_show(struct seq_file *m, void *v) 1431 { 1432 unsigned long allocations = 0, releases = 0, evictions = 0; 1433 unsigned long hits = 0, acquisitions = 0; 1434 unsigned int i, count = 0, buckets = 0; 1435 unsigned long lru = 0, total_age = 0; 1436 1437 /* Serialize with server shutdown */ 1438 mutex_lock(&nfsd_mutex); 1439 if (test_bit(NFSD_FILE_CACHE_UP, &nfsd_file_flags) == 1) { 1440 struct bucket_table *tbl; 1441 struct rhashtable *ht; 1442 1443 lru = list_lru_count(&nfsd_file_lru); 1444 1445 rcu_read_lock(); 1446 ht = &nfsd_file_rhltable.ht; 1447 count = atomic_read(&ht->nelems); 1448 tbl = rht_dereference_rcu(ht->tbl, ht); 1449 buckets = tbl->size; 1450 rcu_read_unlock(); 1451 } 1452 mutex_unlock(&nfsd_mutex); 1453 1454 for_each_possible_cpu(i) { 1455 hits += per_cpu(nfsd_file_cache_hits, i); 1456 acquisitions += per_cpu(nfsd_file_acquisitions, i); 1457 allocations += per_cpu(nfsd_file_allocations, i); 1458 releases += per_cpu(nfsd_file_releases, i); 1459 total_age += per_cpu(nfsd_file_total_age, i); 1460 evictions += per_cpu(nfsd_file_evictions, i); 1461 } 1462 1463 seq_printf(m, "total inodes: %u\n", count); 1464 seq_printf(m, "hash buckets: %u\n", buckets); 1465 seq_printf(m, "lru entries: %lu\n", lru); 1466 seq_printf(m, "cache hits: %lu\n", hits); 1467 seq_printf(m, "acquisitions: %lu\n", acquisitions); 1468 seq_printf(m, "allocations: %lu\n", allocations); 1469 seq_printf(m, "releases: %lu\n", releases); 1470 seq_printf(m, "evictions: %lu\n", evictions); 1471 if (releases) 1472 seq_printf(m, "mean age (ms): %ld\n", total_age / releases); 1473 else 1474 seq_printf(m, "mean age (ms): -\n"); 1475 return 0; 1476 } 1477 1478 /** 1479 * nfsd_fsnotify_recalc_mask - recalculate the fsnotify mask for a nfsd_file 1480 * @nf: nfsd_file to recalculate the mask on 1481 * 1482 * When a directory nfsd_file has a delegation added or removed, that may 1483 * change the events that nfsd requires from the VFS layer. This function 1484 * recalculates the fsnotify mask based on the leases present. 1485 */ 1486 void nfsd_fsnotify_recalc_mask(struct nfsd_file *nf) 1487 { 1488 struct inode *inode = file_inode(nf->nf_file); 1489 u32 lease_mask, set = 0, clear = 0; 1490 struct fsnotify_mark *mark; 1491 1492 /* This is only needed when adding or removing dir delegs */ 1493 if (!S_ISDIR(inode->i_mode) || !nf->nf_mark) 1494 return; 1495 1496 mark = &nf->nf_mark->nfm_mark; 1497 1498 /* 1499 * The mark is shared by every nfsd_file on this inode, so concurrent 1500 * delegation add/remove on the same directory can recalc it in 1501 * parallel. Serialize the read of the lease state and the update of 1502 * the mark so that a recalc working from a stale snapshot of the 1503 * lease list can't clobber a concurrent recalc's update. 1504 */ 1505 mutex_lock(&nf->nf_mark->nfm_recalc_mutex); 1506 1507 /* Set up notifications for any ignored delegation events */ 1508 lease_mask = inode_lease_ignore_mask(inode); 1509 1510 if (lease_mask & FL_IGN_DIR_CREATE) 1511 set |= FS_CREATE | FS_MOVED_TO; 1512 else 1513 clear |= FS_CREATE | FS_MOVED_TO; 1514 1515 if (lease_mask & FL_IGN_DIR_DELETE) 1516 set |= FS_DELETE | FS_MOVED_FROM; 1517 else 1518 clear |= FS_DELETE | FS_MOVED_FROM; 1519 1520 if (lease_mask & FL_IGN_DIR_RENAME) 1521 set |= FS_RENAME; 1522 else 1523 clear |= FS_RENAME; 1524 1525 fsnotify_modify_mark_mask(mark, set, clear); 1526 mutex_unlock(&nf->nf_mark->nfm_recalc_mutex); 1527 } 1528