1 /* 2 * pNFS functions to call and manage layout drivers. 3 * 4 * Copyright (c) 2002 [year of first publication] 5 * The Regents of the University of Michigan 6 * All Rights Reserved 7 * 8 * Dean Hildebrand <dhildebz@umich.edu> 9 * 10 * Permission is granted to use, copy, create derivative works, and 11 * redistribute this software and such derivative works for any purpose, 12 * so long as the name of the University of Michigan is not used in 13 * any advertising or publicity pertaining to the use or distribution 14 * of this software without specific, written prior authorization. If 15 * the above copyright notice or any other identification of the 16 * University of Michigan is included in any copy of any portion of 17 * this software, then the disclaimer below must also be included. 18 * 19 * This software is provided as is, without representation or warranty 20 * of any kind either express or implied, including without limitation 21 * the implied warranties of merchantability, fitness for a particular 22 * purpose, or noninfringement. The Regents of the University of 23 * Michigan shall not be liable for any damages, including special, 24 * indirect, incidental, or consequential damages, with respect to any 25 * claim arising out of or in connection with the use of the software, 26 * even if it has been or is hereafter advised of the possibility of 27 * such damages. 28 */ 29 30 #include <linux/nfs_fs.h> 31 #include <linux/nfs_page.h> 32 #include <linux/module.h> 33 #include <linux/sort.h> 34 #include "internal.h" 35 #include "pnfs.h" 36 #include "iostat.h" 37 #include "nfs4trace.h" 38 #include "delegation.h" 39 #include "nfs42.h" 40 #include "nfs4_fs.h" 41 42 #define NFSDBG_FACILITY NFSDBG_PNFS 43 #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ) 44 45 /* Locking: 46 * 47 * pnfs_spinlock: 48 * protects pnfs_modules_tbl. 49 */ 50 static DEFINE_SPINLOCK(pnfs_spinlock); 51 52 /* 53 * pnfs_modules_tbl holds all pnfs modules 54 */ 55 static LIST_HEAD(pnfs_modules_tbl); 56 57 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo); 58 static void pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo, 59 struct list_head *free_me, 60 const struct pnfs_layout_range *range, 61 u32 seq); 62 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg, 63 struct list_head *tmp_list); 64 static int pnfs_layout_return_on_reboot(struct pnfs_layout_hdr *lo); 65 66 /* Return the registered pnfs layout driver module matching given id */ 67 static struct pnfs_layoutdriver_type * 68 find_pnfs_driver_locked(u32 id) 69 { 70 struct pnfs_layoutdriver_type *local; 71 72 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid) 73 if (local->id == id) 74 goto out; 75 local = NULL; 76 out: 77 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local); 78 return local; 79 } 80 81 static struct pnfs_layoutdriver_type * 82 find_pnfs_driver(u32 id) 83 { 84 struct pnfs_layoutdriver_type *local; 85 86 spin_lock(&pnfs_spinlock); 87 local = find_pnfs_driver_locked(id); 88 if (local != NULL && !try_module_get(local->owner)) { 89 dprintk("%s: Could not grab reference on module\n", __func__); 90 local = NULL; 91 } 92 spin_unlock(&pnfs_spinlock); 93 return local; 94 } 95 96 const struct pnfs_layoutdriver_type *pnfs_find_layoutdriver(u32 id) 97 { 98 return find_pnfs_driver(id); 99 } 100 101 void pnfs_put_layoutdriver(const struct pnfs_layoutdriver_type *ld) 102 { 103 if (ld) 104 module_put(ld->owner); 105 } 106 107 void 108 unset_pnfs_layoutdriver(struct nfs_server *nfss) 109 { 110 if (nfss->pnfs_curr_ld) { 111 if (nfss->pnfs_curr_ld->clear_layoutdriver) 112 nfss->pnfs_curr_ld->clear_layoutdriver(nfss); 113 /* Decrement the MDS count. Purge the deviceid cache if zero */ 114 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count)) 115 nfs4_deviceid_purge_client(nfss->nfs_client); 116 module_put(nfss->pnfs_curr_ld->owner); 117 } 118 nfss->pnfs_curr_ld = NULL; 119 } 120 121 /* 122 * When the server sends a list of layout types, we choose one in the order 123 * given in the list below. 124 * 125 * FIXME: should this list be configurable in some fashion? module param? 126 * mount option? something else? 127 */ 128 static const u32 ld_prefs[] = { 129 LAYOUT_SCSI, 130 LAYOUT_BLOCK_VOLUME, 131 LAYOUT_OSD2_OBJECTS, 132 LAYOUT_FLEX_FILES, 133 LAYOUT_NFSV4_1_FILES, 134 0 135 }; 136 137 static int 138 ld_cmp(const void *e1, const void *e2) 139 { 140 u32 ld1 = *((u32 *)e1); 141 u32 ld2 = *((u32 *)e2); 142 int i; 143 144 for (i = 0; ld_prefs[i] != 0; i++) { 145 if (ld1 == ld_prefs[i]) 146 return -1; 147 148 if (ld2 == ld_prefs[i]) 149 return 1; 150 } 151 return 0; 152 } 153 154 /* 155 * Try to set the server's pnfs module to the pnfs layout type specified by id. 156 * Currently only one pNFS layout driver per filesystem is supported. 157 * 158 * @ids array of layout types supported by MDS. 159 */ 160 void 161 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh, 162 struct nfs_fsinfo *fsinfo) 163 { 164 struct pnfs_layoutdriver_type *ld_type = NULL; 165 u32 id; 166 int i; 167 168 if (fsinfo->nlayouttypes == 0) 169 goto out_no_driver; 170 if (!(server->nfs_client->cl_exchange_flags & 171 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) { 172 printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n", 173 __func__, server->nfs_client->cl_exchange_flags); 174 goto out_no_driver; 175 } 176 177 sort(fsinfo->layouttype, fsinfo->nlayouttypes, 178 sizeof(*fsinfo->layouttype), ld_cmp, NULL); 179 180 for (i = 0; i < fsinfo->nlayouttypes; i++) { 181 id = fsinfo->layouttype[i]; 182 ld_type = find_pnfs_driver(id); 183 if (!ld_type) { 184 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, 185 id); 186 ld_type = find_pnfs_driver(id); 187 } 188 if (ld_type) 189 break; 190 } 191 192 if (!ld_type) { 193 dprintk("%s: No pNFS module found!\n", __func__); 194 goto out_no_driver; 195 } 196 197 server->pnfs_curr_ld = ld_type; 198 if (ld_type->set_layoutdriver 199 && ld_type->set_layoutdriver(server, mntfh)) { 200 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout " 201 "driver %u.\n", __func__, id); 202 module_put(ld_type->owner); 203 goto out_no_driver; 204 } 205 /* Bump the MDS count */ 206 atomic_inc(&server->nfs_client->cl_mds_count); 207 208 dprintk("%s: pNFS module for %u set\n", __func__, id); 209 return; 210 211 out_no_driver: 212 dprintk("%s: Using NFSv4 I/O\n", __func__); 213 server->pnfs_curr_ld = NULL; 214 } 215 216 int 217 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type) 218 { 219 int status = -EINVAL; 220 struct pnfs_layoutdriver_type *tmp; 221 222 if (ld_type->id == 0) { 223 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__); 224 return status; 225 } 226 if (!ld_type->alloc_lseg || !ld_type->free_lseg) { 227 printk(KERN_ERR "NFS: %s Layout driver must provide " 228 "alloc_lseg and free_lseg.\n", __func__); 229 return status; 230 } 231 232 spin_lock(&pnfs_spinlock); 233 tmp = find_pnfs_driver_locked(ld_type->id); 234 if (!tmp) { 235 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl); 236 status = 0; 237 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id, 238 ld_type->name); 239 } else { 240 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n", 241 __func__, ld_type->id); 242 } 243 spin_unlock(&pnfs_spinlock); 244 245 return status; 246 } 247 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver); 248 249 void 250 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type) 251 { 252 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id); 253 spin_lock(&pnfs_spinlock); 254 list_del(&ld_type->pnfs_tblid); 255 spin_unlock(&pnfs_spinlock); 256 } 257 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver); 258 259 /* 260 * pNFS client layout cache 261 */ 262 263 /* Need to hold i_lock if caller does not already hold reference */ 264 void 265 pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo) 266 { 267 refcount_inc(&lo->plh_refcount); 268 } 269 270 static struct pnfs_layout_hdr * 271 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags) 272 { 273 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; 274 return ld->alloc_layout_hdr(ino, gfp_flags); 275 } 276 277 static void 278 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo) 279 { 280 struct nfs_server *server = NFS_SERVER(lo->plh_inode); 281 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld; 282 283 if (test_and_clear_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) { 284 struct nfs_client *clp = server->nfs_client; 285 286 spin_lock(&clp->cl_lock); 287 list_del_rcu(&lo->plh_layouts); 288 spin_unlock(&clp->cl_lock); 289 } 290 put_cred(lo->plh_lc_cred); 291 return ld->free_layout_hdr(lo); 292 } 293 294 static void 295 pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo) 296 { 297 struct nfs_inode *nfsi = NFS_I(lo->plh_inode); 298 dprintk("%s: freeing layout cache %p\n", __func__, lo); 299 nfsi->layout = NULL; 300 /* Reset MDS Threshold I/O counters */ 301 nfsi->write_io = 0; 302 nfsi->read_io = 0; 303 } 304 305 void 306 pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo) 307 { 308 struct inode *inode; 309 unsigned long i_state; 310 311 if (!lo) 312 return; 313 inode = lo->plh_inode; 314 pnfs_layoutreturn_before_put_layout_hdr(lo); 315 316 if (refcount_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) { 317 if (!list_empty(&lo->plh_segs)) 318 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n"); 319 pnfs_detach_layout_hdr(lo); 320 i_state = inode->i_state; 321 spin_unlock(&inode->i_lock); 322 pnfs_free_layout_hdr(lo); 323 /* Notify pnfs_destroy_layout_final() that we're done */ 324 if (i_state & (I_FREEING | I_CLEAR)) 325 wake_up_var(lo); 326 } 327 } 328 329 static struct inode * 330 pnfs_grab_inode_layout_hdr(struct pnfs_layout_hdr *lo) 331 { 332 struct inode *inode = igrab(lo->plh_inode); 333 if (inode) 334 return inode; 335 set_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags); 336 return NULL; 337 } 338 339 /* 340 * Compare 2 layout stateid sequence ids, to see which is newer, 341 * taking into account wraparound issues. 342 */ 343 static bool pnfs_seqid_is_newer(u32 s1, u32 s2) 344 { 345 return (s32)(s1 - s2) > 0; 346 } 347 348 static void pnfs_barrier_update(struct pnfs_layout_hdr *lo, u32 newseq) 349 { 350 if (pnfs_seqid_is_newer(newseq, lo->plh_barrier) || !lo->plh_barrier) 351 lo->plh_barrier = newseq; 352 } 353 354 static void 355 pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode, 356 u32 seq) 357 { 358 if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode) 359 iomode = IOMODE_ANY; 360 lo->plh_return_iomode = iomode; 361 set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags); 362 /* 363 * We must set lo->plh_return_seq to avoid livelocks with 364 * pnfs_layout_need_return() 365 */ 366 if (seq == 0) 367 seq = be32_to_cpu(lo->plh_stateid.seqid); 368 if (!lo->plh_return_seq || pnfs_seqid_is_newer(seq, lo->plh_return_seq)) 369 lo->plh_return_seq = seq; 370 pnfs_barrier_update(lo, seq); 371 } 372 373 static void 374 pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo) 375 { 376 struct pnfs_layout_segment *lseg; 377 lo->plh_return_iomode = 0; 378 lo->plh_return_seq = 0; 379 clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags); 380 list_for_each_entry(lseg, &lo->plh_segs, pls_list) { 381 if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags)) 382 continue; 383 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0); 384 } 385 } 386 387 static void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo) 388 { 389 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags); 390 clear_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags); 391 smp_mb__after_atomic(); 392 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN); 393 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq); 394 } 395 396 static void 397 pnfs_clear_lseg_state(struct pnfs_layout_segment *lseg, 398 struct list_head *free_me) 399 { 400 clear_bit(NFS_LSEG_ROC, &lseg->pls_flags); 401 clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags); 402 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) 403 pnfs_lseg_dec_and_remove_zero(lseg, free_me); 404 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) 405 pnfs_lseg_dec_and_remove_zero(lseg, free_me); 406 } 407 408 /* 409 * Update the seqid of a layout stateid after receiving 410 * NFS4ERR_OLD_STATEID 411 */ 412 bool nfs4_layout_refresh_old_stateid(nfs4_stateid *dst, 413 struct pnfs_layout_range *dst_range, 414 struct inode *inode) 415 { 416 struct pnfs_layout_hdr *lo; 417 struct pnfs_layout_range range = { 418 .iomode = IOMODE_ANY, 419 .offset = 0, 420 .length = NFS4_MAX_UINT64, 421 }; 422 bool ret = false; 423 LIST_HEAD(head); 424 int err; 425 426 spin_lock(&inode->i_lock); 427 lo = NFS_I(inode)->layout; 428 if (lo && pnfs_layout_is_valid(lo) && 429 nfs4_stateid_match_other(dst, &lo->plh_stateid)) { 430 /* Is our call using the most recent seqid? If so, bump it */ 431 if (!nfs4_stateid_is_newer(&lo->plh_stateid, dst)) { 432 nfs4_stateid_seqid_inc(dst); 433 ret = true; 434 goto out; 435 } 436 /* Try to update the seqid to the most recent */ 437 err = pnfs_mark_matching_lsegs_return(lo, &head, &range, 0); 438 if (err != -EBUSY) { 439 dst->seqid = lo->plh_stateid.seqid; 440 *dst_range = range; 441 ret = true; 442 } 443 } 444 out: 445 spin_unlock(&inode->i_lock); 446 pnfs_free_lseg_list(&head); 447 return ret; 448 } 449 450 /* 451 * Mark a pnfs_layout_hdr and all associated layout segments as invalid 452 * 453 * In order to continue using the pnfs_layout_hdr, a full recovery 454 * is required. 455 * Note that caller must hold inode->i_lock. 456 */ 457 int 458 pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo, 459 struct list_head *lseg_list) 460 { 461 struct pnfs_layout_range range = { 462 .iomode = IOMODE_ANY, 463 .offset = 0, 464 .length = NFS4_MAX_UINT64, 465 }; 466 struct pnfs_layout_segment *lseg, *next; 467 468 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); 469 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) 470 pnfs_clear_lseg_state(lseg, lseg_list); 471 pnfs_clear_layoutreturn_info(lo); 472 pnfs_free_returned_lsegs(lo, lseg_list, &range, 0); 473 set_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags); 474 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) && 475 !test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) 476 pnfs_clear_layoutreturn_waitbit(lo); 477 return !list_empty(&lo->plh_segs); 478 } 479 480 static int pnfs_mark_layout_stateid_return(struct pnfs_layout_hdr *lo, 481 struct list_head *lseg_list, 482 enum pnfs_iomode iomode, u32 seq) 483 { 484 struct pnfs_layout_range range = { 485 .iomode = iomode, 486 .length = NFS4_MAX_UINT64, 487 }; 488 489 return pnfs_mark_matching_lsegs_return(lo, lseg_list, &range, seq); 490 } 491 492 static int 493 pnfs_iomode_to_fail_bit(u32 iomode) 494 { 495 return iomode == IOMODE_RW ? 496 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED; 497 } 498 499 static void 500 pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit) 501 { 502 lo->plh_retry_timestamp = jiffies; 503 if (!test_and_set_bit(fail_bit, &lo->plh_flags)) 504 refcount_inc(&lo->plh_refcount); 505 } 506 507 static void 508 pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit) 509 { 510 if (test_and_clear_bit(fail_bit, &lo->plh_flags)) 511 refcount_dec(&lo->plh_refcount); 512 } 513 514 static void 515 pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode) 516 { 517 struct inode *inode = lo->plh_inode; 518 struct pnfs_layout_range range = { 519 .iomode = iomode, 520 .offset = 0, 521 .length = NFS4_MAX_UINT64, 522 }; 523 LIST_HEAD(head); 524 525 spin_lock(&inode->i_lock); 526 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); 527 pnfs_mark_matching_lsegs_return(lo, &head, &range, 0); 528 spin_unlock(&inode->i_lock); 529 pnfs_free_lseg_list(&head); 530 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__, 531 iomode == IOMODE_RW ? "RW" : "READ"); 532 } 533 534 static bool 535 pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode) 536 { 537 unsigned long start, end; 538 int fail_bit = pnfs_iomode_to_fail_bit(iomode); 539 540 if (test_bit(fail_bit, &lo->plh_flags) == 0) 541 return false; 542 end = jiffies; 543 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT; 544 if (!time_in_range(lo->plh_retry_timestamp, start, end)) { 545 /* It is time to retry the failed layoutgets */ 546 pnfs_layout_clear_fail_bit(lo, fail_bit); 547 return false; 548 } 549 return true; 550 } 551 552 static void 553 pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg, 554 const struct pnfs_layout_range *range, 555 const nfs4_stateid *stateid) 556 { 557 INIT_LIST_HEAD(&lseg->pls_list); 558 INIT_LIST_HEAD(&lseg->pls_lc_list); 559 INIT_LIST_HEAD(&lseg->pls_commits); 560 refcount_set(&lseg->pls_refcount, 1); 561 set_bit(NFS_LSEG_VALID, &lseg->pls_flags); 562 lseg->pls_layout = lo; 563 lseg->pls_range = *range; 564 lseg->pls_seq = be32_to_cpu(stateid->seqid); 565 } 566 567 static void pnfs_free_lseg(struct pnfs_layout_segment *lseg) 568 { 569 if (lseg != NULL) { 570 struct inode *inode = lseg->pls_layout->plh_inode; 571 NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg); 572 } 573 } 574 575 static void 576 pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo, 577 struct pnfs_layout_segment *lseg) 578 { 579 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); 580 list_del_init(&lseg->pls_list); 581 /* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */ 582 refcount_dec(&lo->plh_refcount); 583 if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags)) 584 return; 585 if (list_empty(&lo->plh_segs) && 586 !test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) && 587 !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) { 588 if (atomic_read(&lo->plh_outstanding) == 0) 589 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); 590 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); 591 } 592 } 593 594 static bool 595 pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr *lo, 596 struct pnfs_layout_segment *lseg) 597 { 598 if (test_and_clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) && 599 pnfs_layout_is_valid(lo)) { 600 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0); 601 list_move_tail(&lseg->pls_list, &lo->plh_return_segs); 602 return true; 603 } 604 return false; 605 } 606 607 void 608 pnfs_put_lseg(struct pnfs_layout_segment *lseg) 609 { 610 struct pnfs_layout_hdr *lo; 611 struct inode *inode; 612 613 if (!lseg) 614 return; 615 616 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg, 617 refcount_read(&lseg->pls_refcount), 618 test_bit(NFS_LSEG_VALID, &lseg->pls_flags)); 619 620 lo = lseg->pls_layout; 621 inode = lo->plh_inode; 622 623 if (refcount_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) { 624 pnfs_get_layout_hdr(lo); 625 pnfs_layout_remove_lseg(lo, lseg); 626 if (pnfs_cache_lseg_for_layoutreturn(lo, lseg)) 627 lseg = NULL; 628 spin_unlock(&inode->i_lock); 629 pnfs_free_lseg(lseg); 630 pnfs_put_layout_hdr(lo); 631 } 632 } 633 EXPORT_SYMBOL_GPL(pnfs_put_lseg); 634 635 /* 636 * is l2 fully contained in l1? 637 * start1 end1 638 * [----------------------------------) 639 * start2 end2 640 * [----------------) 641 */ 642 static bool 643 pnfs_lseg_range_contained(const struct pnfs_layout_range *l1, 644 const struct pnfs_layout_range *l2) 645 { 646 u64 start1 = l1->offset; 647 u64 end1 = pnfs_end_offset(start1, l1->length); 648 u64 start2 = l2->offset; 649 u64 end2 = pnfs_end_offset(start2, l2->length); 650 651 return (start1 <= start2) && (end1 >= end2); 652 } 653 654 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg, 655 struct list_head *tmp_list) 656 { 657 if (!refcount_dec_and_test(&lseg->pls_refcount)) 658 return false; 659 pnfs_layout_remove_lseg(lseg->pls_layout, lseg); 660 list_add(&lseg->pls_list, tmp_list); 661 return true; 662 } 663 664 /* Returns 1 if lseg is removed from list, 0 otherwise */ 665 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg, 666 struct list_head *tmp_list) 667 { 668 int rv = 0; 669 670 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) { 671 /* Remove the reference keeping the lseg in the 672 * list. It will now be removed when all 673 * outstanding io is finished. 674 */ 675 dprintk("%s: lseg %p ref %d\n", __func__, lseg, 676 refcount_read(&lseg->pls_refcount)); 677 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list)) 678 rv = 1; 679 } 680 return rv; 681 } 682 683 static bool 684 pnfs_should_free_range(const struct pnfs_layout_range *lseg_range, 685 const struct pnfs_layout_range *recall_range) 686 { 687 return (recall_range->iomode == IOMODE_ANY || 688 lseg_range->iomode == recall_range->iomode) && 689 pnfs_lseg_range_intersecting(lseg_range, recall_range); 690 } 691 692 static bool 693 pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg, 694 const struct pnfs_layout_range *recall_range, 695 u32 seq) 696 { 697 if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq)) 698 return false; 699 if (recall_range == NULL) 700 return true; 701 return pnfs_should_free_range(&lseg->pls_range, recall_range); 702 } 703 704 /** 705 * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later 706 * @lo: layout header containing the lsegs 707 * @tmp_list: list head where doomed lsegs should go 708 * @recall_range: optional recall range argument to match (may be NULL) 709 * @seq: only invalidate lsegs obtained prior to this sequence (may be 0) 710 * 711 * Walk the list of lsegs in the layout header, and tear down any that should 712 * be destroyed. If "recall_range" is specified then the segment must match 713 * that range. If "seq" is non-zero, then only match segments that were handed 714 * out at or before that sequence. 715 * 716 * Returns number of matching invalid lsegs remaining in list after scanning 717 * it and purging them. 718 */ 719 int 720 pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo, 721 struct list_head *tmp_list, 722 const struct pnfs_layout_range *recall_range, 723 u32 seq) 724 { 725 struct pnfs_layout_segment *lseg, *next; 726 struct nfs_server *server = NFS_SERVER(lo->plh_inode); 727 int remaining = 0; 728 729 dprintk("%s:Begin lo %p\n", __func__, lo); 730 731 if (list_empty(&lo->plh_segs)) 732 return 0; 733 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) 734 if (pnfs_match_lseg_recall(lseg, recall_range, seq)) { 735 dprintk("%s: freeing lseg %p iomode %d seq %u " 736 "offset %llu length %llu\n", __func__, 737 lseg, lseg->pls_range.iomode, lseg->pls_seq, 738 lseg->pls_range.offset, lseg->pls_range.length); 739 if (mark_lseg_invalid(lseg, tmp_list)) 740 continue; 741 remaining++; 742 pnfs_lseg_cancel_io(server, lseg); 743 } 744 dprintk("%s:Return %i\n", __func__, remaining); 745 return remaining; 746 } 747 748 static void 749 pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo, 750 struct list_head *free_me, 751 const struct pnfs_layout_range *range, 752 u32 seq) 753 { 754 struct pnfs_layout_segment *lseg, *next; 755 756 list_for_each_entry_safe(lseg, next, &lo->plh_return_segs, pls_list) { 757 if (pnfs_match_lseg_recall(lseg, range, seq)) 758 list_move_tail(&lseg->pls_list, free_me); 759 } 760 } 761 762 /* note free_me must contain lsegs from a single layout_hdr */ 763 void 764 pnfs_free_lseg_list(struct list_head *free_me) 765 { 766 struct pnfs_layout_segment *lseg, *tmp; 767 768 if (list_empty(free_me)) 769 return; 770 771 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) { 772 list_del(&lseg->pls_list); 773 pnfs_free_lseg(lseg); 774 } 775 } 776 777 static struct pnfs_layout_hdr *__pnfs_destroy_layout(struct nfs_inode *nfsi) 778 { 779 struct pnfs_layout_hdr *lo; 780 LIST_HEAD(tmp_list); 781 782 spin_lock(&nfsi->vfs_inode.i_lock); 783 lo = nfsi->layout; 784 if (lo) { 785 pnfs_get_layout_hdr(lo); 786 pnfs_mark_layout_stateid_invalid(lo, &tmp_list); 787 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED); 788 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED); 789 spin_unlock(&nfsi->vfs_inode.i_lock); 790 pnfs_free_lseg_list(&tmp_list); 791 nfs_commit_inode(&nfsi->vfs_inode, 0); 792 pnfs_put_layout_hdr(lo); 793 } else 794 spin_unlock(&nfsi->vfs_inode.i_lock); 795 return lo; 796 } 797 798 void pnfs_destroy_layout(struct nfs_inode *nfsi) 799 { 800 __pnfs_destroy_layout(nfsi); 801 } 802 EXPORT_SYMBOL_GPL(pnfs_destroy_layout); 803 804 static bool pnfs_layout_removed(struct nfs_inode *nfsi, 805 struct pnfs_layout_hdr *lo) 806 { 807 bool ret; 808 809 spin_lock(&nfsi->vfs_inode.i_lock); 810 ret = nfsi->layout != lo; 811 spin_unlock(&nfsi->vfs_inode.i_lock); 812 return ret; 813 } 814 815 void pnfs_destroy_layout_final(struct nfs_inode *nfsi) 816 { 817 struct pnfs_layout_hdr *lo = __pnfs_destroy_layout(nfsi); 818 819 if (lo) 820 wait_var_event(lo, pnfs_layout_removed(nfsi, lo)); 821 } 822 823 static bool 824 pnfs_layout_add_bulk_destroy_list(struct inode *inode, 825 struct list_head *layout_list) 826 { 827 struct pnfs_layout_hdr *lo; 828 bool ret = false; 829 830 spin_lock(&inode->i_lock); 831 lo = NFS_I(inode)->layout; 832 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) { 833 pnfs_get_layout_hdr(lo); 834 list_add(&lo->plh_bulk_destroy, layout_list); 835 ret = true; 836 } 837 spin_unlock(&inode->i_lock); 838 return ret; 839 } 840 841 /* Caller must hold rcu_read_lock and clp->cl_lock */ 842 static int 843 pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp, 844 struct nfs_server *server, 845 struct list_head *layout_list) 846 __must_hold(&clp->cl_lock) 847 __must_hold(RCU) 848 { 849 struct pnfs_layout_hdr *lo, *next; 850 struct inode *inode; 851 852 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) { 853 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) || 854 test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) || 855 !list_empty(&lo->plh_bulk_destroy)) 856 continue; 857 /* If the sb is being destroyed, just bail */ 858 if (!nfs_sb_active(server->super)) 859 break; 860 inode = pnfs_grab_inode_layout_hdr(lo); 861 if (inode != NULL) { 862 if (pnfs_layout_add_bulk_destroy_list(inode, 863 layout_list)) 864 continue; 865 rcu_read_unlock(); 866 spin_unlock(&clp->cl_lock); 867 iput(inode); 868 } else { 869 rcu_read_unlock(); 870 spin_unlock(&clp->cl_lock); 871 } 872 nfs_sb_deactive(server->super); 873 spin_lock(&clp->cl_lock); 874 rcu_read_lock(); 875 return -EAGAIN; 876 } 877 return 0; 878 } 879 880 static int 881 pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list, 882 enum pnfs_layout_destroy_mode mode) 883 { 884 struct pnfs_layout_hdr *lo; 885 struct inode *inode; 886 LIST_HEAD(lseg_list); 887 int ret = 0; 888 889 while (!list_empty(layout_list)) { 890 lo = list_entry(layout_list->next, struct pnfs_layout_hdr, 891 plh_bulk_destroy); 892 dprintk("%s freeing layout for inode %lu\n", __func__, 893 lo->plh_inode->i_ino); 894 inode = lo->plh_inode; 895 896 pnfs_layoutcommit_inode(inode, false); 897 898 spin_lock(&inode->i_lock); 899 list_del_init(&lo->plh_bulk_destroy); 900 if (mode == PNFS_LAYOUT_FILE_BULK_RETURN) { 901 pnfs_mark_layout_stateid_return(lo, &lseg_list, 902 IOMODE_ANY, 0); 903 } else if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) { 904 if (mode == PNFS_LAYOUT_BULK_RETURN) 905 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); 906 ret = -EAGAIN; 907 } 908 spin_unlock(&inode->i_lock); 909 pnfs_free_lseg_list(&lseg_list); 910 /* Free all lsegs that are attached to commit buckets */ 911 nfs_commit_inode(inode, 0); 912 pnfs_put_layout_hdr(lo); 913 nfs_iput_and_deactive(inode); 914 } 915 return ret; 916 } 917 918 int pnfs_layout_destroy_byfsid(struct nfs_client *clp, struct nfs_fsid *fsid, 919 enum pnfs_layout_destroy_mode mode) 920 { 921 struct nfs_server *server; 922 LIST_HEAD(layout_list); 923 924 spin_lock(&clp->cl_lock); 925 rcu_read_lock(); 926 restart: 927 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 928 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0) 929 continue; 930 if (pnfs_layout_bulk_destroy_byserver_locked(clp, 931 server, 932 &layout_list) != 0) 933 goto restart; 934 } 935 rcu_read_unlock(); 936 spin_unlock(&clp->cl_lock); 937 938 return pnfs_layout_free_bulk_destroy_list(&layout_list, mode); 939 } 940 941 static void pnfs_layout_build_destroy_list_byclient(struct nfs_client *clp, 942 struct list_head *list) 943 { 944 struct nfs_server *server; 945 946 spin_lock(&clp->cl_lock); 947 rcu_read_lock(); 948 restart: 949 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 950 if (pnfs_layout_bulk_destroy_byserver_locked(clp, server, 951 list) != 0) 952 goto restart; 953 } 954 rcu_read_unlock(); 955 spin_unlock(&clp->cl_lock); 956 } 957 958 static int pnfs_layout_do_destroy_byclid(struct nfs_client *clp, 959 struct list_head *list, 960 enum pnfs_layout_destroy_mode mode) 961 { 962 pnfs_layout_build_destroy_list_byclient(clp, list); 963 return pnfs_layout_free_bulk_destroy_list(list, mode); 964 } 965 966 int pnfs_layout_destroy_byclid(struct nfs_client *clp, 967 enum pnfs_layout_destroy_mode mode) 968 { 969 LIST_HEAD(layout_list); 970 971 return pnfs_layout_do_destroy_byclid(clp, &layout_list, mode); 972 } 973 974 /* 975 * Called by the state manager to remove all layouts established under an 976 * expired lease. 977 */ 978 void 979 pnfs_destroy_all_layouts(struct nfs_client *clp) 980 { 981 nfs4_deviceid_mark_client_invalid(clp); 982 nfs4_deviceid_purge_client(clp); 983 984 pnfs_layout_destroy_byclid(clp, PNFS_LAYOUT_INVALIDATE); 985 } 986 987 static void pnfs_layout_build_recover_list_byclient(struct nfs_client *clp, 988 struct list_head *list) 989 { 990 struct nfs_server *server; 991 992 spin_lock(&clp->cl_lock); 993 rcu_read_lock(); 994 restart: 995 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 996 if (!(server->caps & NFS_CAP_REBOOT_LAYOUTRETURN)) 997 continue; 998 if (pnfs_layout_bulk_destroy_byserver_locked(clp, server, 999 list) != 0) 1000 goto restart; 1001 } 1002 rcu_read_unlock(); 1003 spin_unlock(&clp->cl_lock); 1004 } 1005 1006 static int pnfs_layout_bulk_list_reboot(struct list_head *list) 1007 { 1008 struct pnfs_layout_hdr *lo; 1009 struct nfs_server *server; 1010 int ret; 1011 1012 list_for_each_entry(lo, list, plh_bulk_destroy) { 1013 server = NFS_SERVER(lo->plh_inode); 1014 ret = pnfs_layout_return_on_reboot(lo); 1015 switch (ret) { 1016 case 0: 1017 continue; 1018 case -NFS4ERR_BAD_STATEID: 1019 server->caps &= ~NFS_CAP_REBOOT_LAYOUTRETURN; 1020 break; 1021 case -NFS4ERR_NO_GRACE: 1022 break; 1023 default: 1024 goto err; 1025 } 1026 break; 1027 } 1028 return 0; 1029 err: 1030 return ret; 1031 } 1032 1033 int pnfs_layout_handle_reboot(struct nfs_client *clp) 1034 { 1035 LIST_HEAD(list); 1036 int ret = 0, ret2; 1037 1038 pnfs_layout_build_recover_list_byclient(clp, &list); 1039 if (!list_empty(&list)) 1040 ret = pnfs_layout_bulk_list_reboot(&list); 1041 ret2 = pnfs_layout_do_destroy_byclid(clp, &list, 1042 PNFS_LAYOUT_INVALIDATE); 1043 if (!ret) 1044 ret = ret2; 1045 return (ret == 0) ? 0 : -EAGAIN; 1046 } 1047 1048 static void 1049 pnfs_set_layout_cred(struct pnfs_layout_hdr *lo, const struct cred *cred) 1050 { 1051 const struct cred *old; 1052 1053 if (cred && cred_fscmp(lo->plh_lc_cred, cred) != 0) { 1054 old = xchg(&lo->plh_lc_cred, get_cred(cred)); 1055 put_cred(old); 1056 } 1057 } 1058 1059 /* update lo->plh_stateid with new if is more recent */ 1060 void 1061 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new, 1062 const struct cred *cred, bool update_barrier) 1063 { 1064 u32 oldseq = be32_to_cpu(lo->plh_stateid.seqid); 1065 u32 newseq = be32_to_cpu(new->seqid); 1066 1067 if (!pnfs_layout_is_valid(lo)) { 1068 pnfs_set_layout_cred(lo, cred); 1069 nfs4_stateid_copy(&lo->plh_stateid, new); 1070 lo->plh_barrier = newseq; 1071 pnfs_clear_layoutreturn_info(lo); 1072 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); 1073 return; 1074 } 1075 1076 if (pnfs_seqid_is_newer(newseq, oldseq)) 1077 nfs4_stateid_copy(&lo->plh_stateid, new); 1078 1079 if (update_barrier) { 1080 pnfs_barrier_update(lo, newseq); 1081 return; 1082 } 1083 /* 1084 * Because of wraparound, we want to keep the barrier 1085 * "close" to the current seqids. We really only want to 1086 * get here from a layoutget call. 1087 */ 1088 if (atomic_read(&lo->plh_outstanding) == 1) 1089 pnfs_barrier_update(lo, be32_to_cpu(lo->plh_stateid.seqid)); 1090 } 1091 1092 static bool 1093 pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo, 1094 const nfs4_stateid *stateid) 1095 { 1096 u32 seqid = be32_to_cpu(stateid->seqid); 1097 1098 return lo->plh_barrier && pnfs_seqid_is_newer(lo->plh_barrier, seqid); 1099 } 1100 1101 /* lget is set to 1 if called from inside send_layoutget call chain */ 1102 static bool 1103 pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo) 1104 { 1105 return lo->plh_block_lgets || 1106 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags); 1107 } 1108 1109 static struct nfs_server * 1110 pnfs_find_server(struct inode *inode, struct nfs_open_context *ctx) 1111 { 1112 struct nfs_server *server; 1113 1114 if (inode) { 1115 server = NFS_SERVER(inode); 1116 } else { 1117 struct dentry *parent_dir = dget_parent(ctx->dentry); 1118 server = NFS_SERVER(parent_dir->d_inode); 1119 dput(parent_dir); 1120 } 1121 return server; 1122 } 1123 1124 static void nfs4_free_pages(struct page **pages, size_t size) 1125 { 1126 int i; 1127 1128 if (!pages) 1129 return; 1130 1131 for (i = 0; i < size; i++) { 1132 if (!pages[i]) 1133 break; 1134 __free_page(pages[i]); 1135 } 1136 kfree(pages); 1137 } 1138 1139 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags) 1140 { 1141 struct page **pages; 1142 int i; 1143 1144 pages = kmalloc_array(size, sizeof(struct page *), gfp_flags); 1145 if (!pages) { 1146 dprintk("%s: can't alloc array of %zu pages\n", __func__, size); 1147 return NULL; 1148 } 1149 1150 for (i = 0; i < size; i++) { 1151 pages[i] = alloc_page(gfp_flags); 1152 if (!pages[i]) { 1153 dprintk("%s: failed to allocate page\n", __func__); 1154 nfs4_free_pages(pages, i); 1155 return NULL; 1156 } 1157 } 1158 1159 return pages; 1160 } 1161 1162 static struct nfs4_layoutget * 1163 pnfs_alloc_init_layoutget_args(struct inode *ino, 1164 struct nfs_open_context *ctx, 1165 const nfs4_stateid *stateid, 1166 const struct pnfs_layout_range *range, 1167 gfp_t gfp_flags) 1168 { 1169 struct nfs_server *server = pnfs_find_server(ino, ctx); 1170 size_t max_reply_sz = server->pnfs_curr_ld->max_layoutget_response; 1171 size_t max_pages = max_response_pages(server); 1172 struct nfs4_layoutget *lgp; 1173 1174 dprintk("--> %s\n", __func__); 1175 1176 lgp = kzalloc(sizeof(*lgp), gfp_flags); 1177 if (lgp == NULL) 1178 return NULL; 1179 1180 if (max_reply_sz) { 1181 size_t npages = (max_reply_sz + PAGE_SIZE - 1) >> PAGE_SHIFT; 1182 if (npages < max_pages) 1183 max_pages = npages; 1184 } 1185 1186 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags); 1187 if (!lgp->args.layout.pages) { 1188 kfree(lgp); 1189 return NULL; 1190 } 1191 lgp->args.layout.pglen = max_pages * PAGE_SIZE; 1192 lgp->res.layoutp = &lgp->args.layout; 1193 1194 /* Don't confuse uninitialised result and success */ 1195 lgp->res.status = -NFS4ERR_DELAY; 1196 1197 lgp->args.minlength = PAGE_SIZE; 1198 if (lgp->args.minlength > range->length) 1199 lgp->args.minlength = range->length; 1200 if (ino) { 1201 loff_t i_size = i_size_read(ino); 1202 1203 if (range->iomode == IOMODE_READ) { 1204 if (range->offset >= i_size) 1205 lgp->args.minlength = 0; 1206 else if (i_size - range->offset < lgp->args.minlength) 1207 lgp->args.minlength = i_size - range->offset; 1208 } 1209 } 1210 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE; 1211 pnfs_copy_range(&lgp->args.range, range); 1212 lgp->args.type = server->pnfs_curr_ld->id; 1213 lgp->args.inode = ino; 1214 lgp->args.ctx = get_nfs_open_context(ctx); 1215 nfs4_stateid_copy(&lgp->args.stateid, stateid); 1216 lgp->gfp_flags = gfp_flags; 1217 lgp->cred = ctx->cred; 1218 return lgp; 1219 } 1220 1221 void pnfs_layoutget_free(struct nfs4_layoutget *lgp) 1222 { 1223 size_t max_pages = lgp->args.layout.pglen / PAGE_SIZE; 1224 1225 nfs4_free_pages(lgp->args.layout.pages, max_pages); 1226 pnfs_put_layout_hdr(lgp->lo); 1227 put_nfs_open_context(lgp->args.ctx); 1228 kfree(lgp); 1229 } 1230 1231 static void pnfs_clear_layoutcommit(struct inode *inode, 1232 struct list_head *head) 1233 { 1234 struct nfs_inode *nfsi = NFS_I(inode); 1235 struct pnfs_layout_segment *lseg, *tmp; 1236 1237 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) 1238 return; 1239 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) { 1240 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) 1241 continue; 1242 pnfs_lseg_dec_and_remove_zero(lseg, head); 1243 } 1244 } 1245 1246 static void 1247 pnfs_layoutreturn_retry_later_locked(struct pnfs_layout_hdr *lo, 1248 const nfs4_stateid *arg_stateid, 1249 const struct pnfs_layout_range *range) 1250 { 1251 const struct pnfs_layout_segment *lseg; 1252 u32 seq = be32_to_cpu(arg_stateid->seqid); 1253 1254 if (pnfs_layout_is_valid(lo) && 1255 nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid)) { 1256 list_for_each_entry(lseg, &lo->plh_return_segs, pls_list) { 1257 if (pnfs_seqid_is_newer(lseg->pls_seq, seq) || 1258 !pnfs_should_free_range(&lseg->pls_range, range)) 1259 continue; 1260 pnfs_set_plh_return_info(lo, range->iomode, seq); 1261 break; 1262 } 1263 } 1264 } 1265 1266 void pnfs_layoutreturn_retry_later(struct pnfs_layout_hdr *lo, 1267 const nfs4_stateid *arg_stateid, 1268 const struct pnfs_layout_range *range) 1269 { 1270 struct inode *inode = lo->plh_inode; 1271 1272 spin_lock(&inode->i_lock); 1273 pnfs_layoutreturn_retry_later_locked(lo, arg_stateid, range); 1274 pnfs_clear_layoutreturn_waitbit(lo); 1275 spin_unlock(&inode->i_lock); 1276 } 1277 1278 void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo, 1279 const nfs4_stateid *arg_stateid, 1280 const struct pnfs_layout_range *range, 1281 const nfs4_stateid *stateid) 1282 { 1283 struct inode *inode = lo->plh_inode; 1284 LIST_HEAD(freeme); 1285 1286 spin_lock(&inode->i_lock); 1287 if (!pnfs_layout_is_valid(lo) || 1288 !nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid)) 1289 goto out_unlock; 1290 if (stateid) { 1291 u32 seq = be32_to_cpu(arg_stateid->seqid); 1292 1293 pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq); 1294 pnfs_free_returned_lsegs(lo, &freeme, range, seq); 1295 pnfs_set_layout_stateid(lo, stateid, NULL, true); 1296 } else 1297 pnfs_mark_layout_stateid_invalid(lo, &freeme); 1298 out_unlock: 1299 pnfs_clear_layoutreturn_waitbit(lo); 1300 spin_unlock(&inode->i_lock); 1301 pnfs_free_lseg_list(&freeme); 1302 1303 } 1304 1305 static bool 1306 pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo, 1307 nfs4_stateid *stateid, 1308 const struct cred **cred, 1309 enum pnfs_iomode *iomode) 1310 { 1311 /* Serialise LAYOUTGET/LAYOUTRETURN */ 1312 if (atomic_read(&lo->plh_outstanding) != 0) 1313 return false; 1314 if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) 1315 return false; 1316 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags); 1317 pnfs_get_layout_hdr(lo); 1318 nfs4_stateid_copy(stateid, &lo->plh_stateid); 1319 *cred = get_cred(lo->plh_lc_cred); 1320 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) { 1321 if (lo->plh_return_seq != 0) 1322 stateid->seqid = cpu_to_be32(lo->plh_return_seq); 1323 if (iomode != NULL) 1324 *iomode = lo->plh_return_iomode; 1325 pnfs_clear_layoutreturn_info(lo); 1326 } else if (iomode != NULL) 1327 *iomode = IOMODE_ANY; 1328 pnfs_barrier_update(lo, be32_to_cpu(stateid->seqid)); 1329 return true; 1330 } 1331 1332 static void 1333 pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args, 1334 struct pnfs_layout_hdr *lo, 1335 const nfs4_stateid *stateid, 1336 enum pnfs_iomode iomode) 1337 { 1338 struct inode *inode = lo->plh_inode; 1339 1340 args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id; 1341 args->inode = inode; 1342 args->range.iomode = iomode; 1343 args->range.offset = 0; 1344 args->range.length = NFS4_MAX_UINT64; 1345 args->layout = lo; 1346 nfs4_stateid_copy(&args->stateid, stateid); 1347 } 1348 1349 static int 1350 pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, 1351 const nfs4_stateid *stateid, 1352 const struct cred **pcred, 1353 enum pnfs_iomode iomode, 1354 unsigned int flags) 1355 { 1356 struct inode *ino = lo->plh_inode; 1357 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; 1358 struct nfs4_layoutreturn *lrp; 1359 const struct cred *cred = *pcred; 1360 int status = 0; 1361 1362 *pcred = NULL; 1363 lrp = kzalloc(sizeof(*lrp), nfs_io_gfp_mask()); 1364 if (unlikely(lrp == NULL)) { 1365 status = -ENOMEM; 1366 spin_lock(&ino->i_lock); 1367 pnfs_clear_layoutreturn_waitbit(lo); 1368 spin_unlock(&ino->i_lock); 1369 put_cred(cred); 1370 pnfs_put_layout_hdr(lo); 1371 goto out; 1372 } 1373 1374 pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode); 1375 lrp->args.ld_private = &lrp->ld_private; 1376 lrp->clp = NFS_SERVER(ino)->nfs_client; 1377 lrp->cred = cred; 1378 if (ld->prepare_layoutreturn) 1379 ld->prepare_layoutreturn(&lrp->args); 1380 1381 status = nfs4_proc_layoutreturn(lrp, flags); 1382 out: 1383 dprintk("<-- %s status: %d\n", __func__, status); 1384 return status; 1385 } 1386 1387 /* Return true if layoutreturn is needed */ 1388 static bool 1389 pnfs_layout_need_return(struct pnfs_layout_hdr *lo) 1390 { 1391 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) 1392 return false; 1393 return pnfs_mark_layout_stateid_return(lo, &lo->plh_return_segs, 1394 lo->plh_return_iomode, 1395 lo->plh_return_seq) != EBUSY; 1396 } 1397 1398 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo) 1399 { 1400 struct inode *inode= lo->plh_inode; 1401 1402 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) 1403 return; 1404 spin_lock(&inode->i_lock); 1405 if (pnfs_layout_need_return(lo)) { 1406 const struct cred *cred; 1407 nfs4_stateid stateid; 1408 enum pnfs_iomode iomode; 1409 bool send; 1410 1411 send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode); 1412 spin_unlock(&inode->i_lock); 1413 if (send) { 1414 /* Send an async layoutreturn so we dont deadlock */ 1415 pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, 1416 PNFS_FL_LAYOUTRETURN_ASYNC); 1417 } 1418 } else 1419 spin_unlock(&inode->i_lock); 1420 } 1421 1422 /* 1423 * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr 1424 * when the layout segment list is empty. 1425 * 1426 * Note that a pnfs_layout_hdr can exist with an empty layout segment 1427 * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the 1428 * deviceid is marked invalid. 1429 */ 1430 int 1431 _pnfs_return_layout(struct inode *ino) 1432 { 1433 struct pnfs_layout_hdr *lo = NULL; 1434 struct nfs_inode *nfsi = NFS_I(ino); 1435 struct pnfs_layout_range range = { 1436 .iomode = IOMODE_ANY, 1437 .offset = 0, 1438 .length = NFS4_MAX_UINT64, 1439 }; 1440 LIST_HEAD(tmp_list); 1441 const struct cred *cred; 1442 nfs4_stateid stateid; 1443 int status = 0; 1444 bool send, valid_layout; 1445 1446 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino); 1447 1448 spin_lock(&ino->i_lock); 1449 lo = nfsi->layout; 1450 if (!lo) { 1451 spin_unlock(&ino->i_lock); 1452 dprintk("NFS: %s no layout to return\n", __func__); 1453 goto out; 1454 } 1455 /* Reference matched in nfs4_layoutreturn_release */ 1456 pnfs_get_layout_hdr(lo); 1457 /* Is there an outstanding layoutreturn ? */ 1458 if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) { 1459 spin_unlock(&ino->i_lock); 1460 if (wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN, 1461 TASK_UNINTERRUPTIBLE)) 1462 goto out_put_layout_hdr; 1463 spin_lock(&ino->i_lock); 1464 } 1465 valid_layout = pnfs_layout_is_valid(lo); 1466 pnfs_clear_layoutcommit(ino, &tmp_list); 1467 pnfs_mark_matching_lsegs_return(lo, &tmp_list, &range, 0); 1468 1469 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) 1470 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range); 1471 1472 /* Don't send a LAYOUTRETURN if list was initially empty */ 1473 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) || 1474 !valid_layout) { 1475 spin_unlock(&ino->i_lock); 1476 dprintk("NFS: %s no layout segments to return\n", __func__); 1477 goto out_wait_layoutreturn; 1478 } 1479 1480 send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, NULL); 1481 spin_unlock(&ino->i_lock); 1482 if (send) 1483 status = pnfs_send_layoutreturn(lo, &stateid, &cred, IOMODE_ANY, 1484 0); 1485 out_wait_layoutreturn: 1486 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN, TASK_UNINTERRUPTIBLE); 1487 out_put_layout_hdr: 1488 pnfs_free_lseg_list(&tmp_list); 1489 pnfs_put_layout_hdr(lo); 1490 out: 1491 dprintk("<-- %s status: %d\n", __func__, status); 1492 return status; 1493 } 1494 1495 int 1496 pnfs_commit_and_return_layout(struct inode *inode) 1497 { 1498 struct pnfs_layout_hdr *lo; 1499 int ret; 1500 1501 spin_lock(&inode->i_lock); 1502 lo = NFS_I(inode)->layout; 1503 if (lo == NULL) { 1504 spin_unlock(&inode->i_lock); 1505 return 0; 1506 } 1507 pnfs_get_layout_hdr(lo); 1508 /* Block new layoutgets and read/write to ds */ 1509 lo->plh_block_lgets++; 1510 spin_unlock(&inode->i_lock); 1511 filemap_fdatawait(inode->i_mapping); 1512 ret = pnfs_layoutcommit_inode(inode, true); 1513 if (ret == 0) 1514 ret = _pnfs_return_layout(inode); 1515 spin_lock(&inode->i_lock); 1516 lo->plh_block_lgets--; 1517 spin_unlock(&inode->i_lock); 1518 pnfs_put_layout_hdr(lo); 1519 return ret; 1520 } 1521 1522 static int pnfs_layout_return_on_reboot(struct pnfs_layout_hdr *lo) 1523 { 1524 struct inode *inode = lo->plh_inode; 1525 const struct cred *cred; 1526 1527 spin_lock(&inode->i_lock); 1528 if (!pnfs_layout_is_valid(lo)) { 1529 spin_unlock(&inode->i_lock); 1530 return 0; 1531 } 1532 cred = get_cred(lo->plh_lc_cred); 1533 pnfs_get_layout_hdr(lo); 1534 spin_unlock(&inode->i_lock); 1535 1536 return pnfs_send_layoutreturn(lo, &zero_stateid, &cred, IOMODE_ANY, 1537 PNFS_FL_LAYOUTRETURN_PRIVILEGED); 1538 } 1539 1540 bool pnfs_roc(struct inode *ino, 1541 struct nfs4_layoutreturn_args *args, 1542 struct nfs4_layoutreturn_res *res, 1543 const struct cred *cred) 1544 { 1545 struct nfs_inode *nfsi = NFS_I(ino); 1546 struct nfs_open_context *ctx; 1547 struct nfs4_state *state; 1548 struct pnfs_layout_hdr *lo; 1549 struct pnfs_layout_segment *lseg, *next; 1550 const struct cred *lc_cred; 1551 nfs4_stateid stateid; 1552 enum pnfs_iomode iomode = 0; 1553 bool layoutreturn = false, roc = false; 1554 bool skip_read = false; 1555 1556 if (!nfs_have_layout(ino)) 1557 return false; 1558 retry: 1559 rcu_read_lock(); 1560 spin_lock(&ino->i_lock); 1561 lo = nfsi->layout; 1562 if (!lo || !pnfs_layout_is_valid(lo) || 1563 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { 1564 lo = NULL; 1565 goto out_noroc; 1566 } 1567 pnfs_get_layout_hdr(lo); 1568 if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) { 1569 spin_unlock(&ino->i_lock); 1570 rcu_read_unlock(); 1571 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN, 1572 TASK_UNINTERRUPTIBLE); 1573 pnfs_put_layout_hdr(lo); 1574 goto retry; 1575 } 1576 1577 /* no roc if we hold a delegation */ 1578 if (nfs4_check_delegation(ino, FMODE_READ)) { 1579 if (nfs4_check_delegation(ino, FMODE_WRITE)) 1580 goto out_noroc; 1581 skip_read = true; 1582 } 1583 1584 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) { 1585 state = ctx->state; 1586 if (state == NULL) 1587 continue; 1588 /* Don't return layout if there is open file state */ 1589 if (state->state & FMODE_WRITE) 1590 goto out_noroc; 1591 if (state->state & FMODE_READ) 1592 skip_read = true; 1593 } 1594 1595 1596 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) { 1597 if (skip_read && lseg->pls_range.iomode == IOMODE_READ) 1598 continue; 1599 /* If we are sending layoutreturn, invalidate all valid lsegs */ 1600 if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags)) 1601 continue; 1602 /* 1603 * Note: mark lseg for return so pnfs_layout_remove_lseg 1604 * doesn't invalidate the layout for us. 1605 */ 1606 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags); 1607 if (!mark_lseg_invalid(lseg, &lo->plh_return_segs)) 1608 continue; 1609 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0); 1610 } 1611 1612 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) 1613 goto out_noroc; 1614 1615 /* ROC in two conditions: 1616 * 1. there are ROC lsegs 1617 * 2. we don't send layoutreturn 1618 */ 1619 /* lo ref dropped in pnfs_roc_release() */ 1620 layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &lc_cred, &iomode); 1621 /* If the creds don't match, we can't compound the layoutreturn */ 1622 if (!layoutreturn || cred_fscmp(cred, lc_cred) != 0) 1623 goto out_noroc; 1624 1625 roc = layoutreturn; 1626 pnfs_init_layoutreturn_args(args, lo, &stateid, iomode); 1627 res->lrs_present = 0; 1628 layoutreturn = false; 1629 put_cred(lc_cred); 1630 1631 out_noroc: 1632 spin_unlock(&ino->i_lock); 1633 rcu_read_unlock(); 1634 pnfs_layoutcommit_inode(ino, true); 1635 if (roc) { 1636 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld; 1637 if (ld->prepare_layoutreturn) 1638 ld->prepare_layoutreturn(args); 1639 pnfs_put_layout_hdr(lo); 1640 return true; 1641 } 1642 if (layoutreturn) 1643 pnfs_send_layoutreturn(lo, &stateid, &lc_cred, iomode, 0); 1644 pnfs_put_layout_hdr(lo); 1645 return false; 1646 } 1647 1648 int pnfs_roc_done(struct rpc_task *task, struct nfs4_layoutreturn_args **argpp, 1649 struct nfs4_layoutreturn_res **respp, int *ret) 1650 { 1651 struct nfs4_layoutreturn_args *arg = *argpp; 1652 int retval = -EAGAIN; 1653 1654 if (!arg) 1655 return 0; 1656 /* Handle Layoutreturn errors */ 1657 switch (*ret) { 1658 case 0: 1659 retval = 0; 1660 break; 1661 case -NFS4ERR_NOMATCHING_LAYOUT: 1662 /* Was there an RPC level error? If not, retry */ 1663 if (task->tk_rpc_status == 0) 1664 break; 1665 /* If the call was not sent, let caller handle it */ 1666 if (!RPC_WAS_SENT(task)) 1667 return 0; 1668 /* 1669 * Otherwise, assume the call succeeded and 1670 * that we need to release the layout 1671 */ 1672 *ret = 0; 1673 (*respp)->lrs_present = 0; 1674 retval = 0; 1675 break; 1676 case -NFS4ERR_DELAY: 1677 /* Let the caller handle the retry */ 1678 *ret = -NFS4ERR_NOMATCHING_LAYOUT; 1679 return 0; 1680 case -NFS4ERR_OLD_STATEID: 1681 if (!nfs4_layout_refresh_old_stateid(&arg->stateid, 1682 &arg->range, arg->inode)) 1683 break; 1684 *ret = -NFS4ERR_NOMATCHING_LAYOUT; 1685 return -EAGAIN; 1686 } 1687 *argpp = NULL; 1688 *respp = NULL; 1689 return retval; 1690 } 1691 1692 void pnfs_roc_release(struct nfs4_layoutreturn_args *args, 1693 struct nfs4_layoutreturn_res *res, int ret) 1694 { 1695 struct pnfs_layout_hdr *lo = args->layout; 1696 struct inode *inode = args->inode; 1697 const nfs4_stateid *res_stateid = NULL; 1698 struct nfs4_xdr_opaque_data *ld_private = args->ld_private; 1699 1700 switch (ret) { 1701 case -NFS4ERR_BADSESSION: 1702 case -NFS4ERR_DEADSESSION: 1703 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: 1704 case -NFS4ERR_NOMATCHING_LAYOUT: 1705 spin_lock(&inode->i_lock); 1706 pnfs_layoutreturn_retry_later_locked(lo, &args->stateid, 1707 &args->range); 1708 pnfs_clear_layoutreturn_waitbit(lo); 1709 spin_unlock(&inode->i_lock); 1710 break; 1711 case 0: 1712 if (res->lrs_present) 1713 res_stateid = &res->stateid; 1714 fallthrough; 1715 default: 1716 pnfs_layoutreturn_free_lsegs(lo, &args->stateid, &args->range, 1717 res_stateid); 1718 } 1719 trace_nfs4_layoutreturn_on_close(args->inode, &args->stateid, ret); 1720 if (ld_private && ld_private->ops && ld_private->ops->free) 1721 ld_private->ops->free(ld_private); 1722 pnfs_put_layout_hdr(lo); 1723 } 1724 1725 bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task) 1726 { 1727 struct nfs_inode *nfsi = NFS_I(ino); 1728 struct pnfs_layout_hdr *lo; 1729 bool sleep = false; 1730 1731 /* we might not have grabbed lo reference. so need to check under 1732 * i_lock */ 1733 spin_lock(&ino->i_lock); 1734 lo = nfsi->layout; 1735 if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) { 1736 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL); 1737 sleep = true; 1738 } 1739 spin_unlock(&ino->i_lock); 1740 return sleep; 1741 } 1742 1743 /* 1744 * Compare two layout segments for sorting into layout cache. 1745 * We want to preferentially return RW over RO layouts, so ensure those 1746 * are seen first. 1747 */ 1748 static s64 1749 pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1, 1750 const struct pnfs_layout_range *l2) 1751 { 1752 s64 d; 1753 1754 /* high offset > low offset */ 1755 d = l1->offset - l2->offset; 1756 if (d) 1757 return d; 1758 1759 /* short length > long length */ 1760 d = l2->length - l1->length; 1761 if (d) 1762 return d; 1763 1764 /* read > read/write */ 1765 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ); 1766 } 1767 1768 static bool 1769 pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1, 1770 const struct pnfs_layout_range *l2) 1771 { 1772 return pnfs_lseg_range_cmp(l1, l2) > 0; 1773 } 1774 1775 static bool 1776 pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg, 1777 struct pnfs_layout_segment *old) 1778 { 1779 return false; 1780 } 1781 1782 void 1783 pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo, 1784 struct pnfs_layout_segment *lseg, 1785 bool (*is_after)(const struct pnfs_layout_range *, 1786 const struct pnfs_layout_range *), 1787 bool (*do_merge)(struct pnfs_layout_segment *, 1788 struct pnfs_layout_segment *), 1789 struct list_head *free_me) 1790 { 1791 struct pnfs_layout_segment *lp, *tmp; 1792 1793 dprintk("%s:Begin\n", __func__); 1794 1795 list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) { 1796 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0) 1797 continue; 1798 if (do_merge(lseg, lp)) { 1799 mark_lseg_invalid(lp, free_me); 1800 continue; 1801 } 1802 if (is_after(&lseg->pls_range, &lp->pls_range)) 1803 continue; 1804 list_add_tail(&lseg->pls_list, &lp->pls_list); 1805 dprintk("%s: inserted lseg %p " 1806 "iomode %d offset %llu length %llu before " 1807 "lp %p iomode %d offset %llu length %llu\n", 1808 __func__, lseg, lseg->pls_range.iomode, 1809 lseg->pls_range.offset, lseg->pls_range.length, 1810 lp, lp->pls_range.iomode, lp->pls_range.offset, 1811 lp->pls_range.length); 1812 goto out; 1813 } 1814 list_add_tail(&lseg->pls_list, &lo->plh_segs); 1815 dprintk("%s: inserted lseg %p " 1816 "iomode %d offset %llu length %llu at tail\n", 1817 __func__, lseg, lseg->pls_range.iomode, 1818 lseg->pls_range.offset, lseg->pls_range.length); 1819 out: 1820 pnfs_get_layout_hdr(lo); 1821 1822 dprintk("%s:Return\n", __func__); 1823 } 1824 EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg); 1825 1826 static void 1827 pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo, 1828 struct pnfs_layout_segment *lseg, 1829 struct list_head *free_me) 1830 { 1831 struct inode *inode = lo->plh_inode; 1832 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; 1833 1834 if (ld->add_lseg != NULL) 1835 ld->add_lseg(lo, lseg, free_me); 1836 else 1837 pnfs_generic_layout_insert_lseg(lo, lseg, 1838 pnfs_lseg_range_is_after, 1839 pnfs_lseg_no_merge, 1840 free_me); 1841 } 1842 1843 static struct pnfs_layout_hdr * 1844 alloc_init_layout_hdr(struct inode *ino, 1845 struct nfs_open_context *ctx, 1846 gfp_t gfp_flags) 1847 { 1848 struct pnfs_layout_hdr *lo; 1849 1850 lo = pnfs_alloc_layout_hdr(ino, gfp_flags); 1851 if (!lo) 1852 return NULL; 1853 refcount_set(&lo->plh_refcount, 1); 1854 INIT_LIST_HEAD(&lo->plh_layouts); 1855 INIT_LIST_HEAD(&lo->plh_segs); 1856 INIT_LIST_HEAD(&lo->plh_return_segs); 1857 INIT_LIST_HEAD(&lo->plh_bulk_destroy); 1858 lo->plh_inode = ino; 1859 lo->plh_lc_cred = get_cred(ctx->cred); 1860 lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID; 1861 return lo; 1862 } 1863 1864 static struct pnfs_layout_hdr * 1865 pnfs_find_alloc_layout(struct inode *ino, 1866 struct nfs_open_context *ctx, 1867 gfp_t gfp_flags) 1868 __releases(&ino->i_lock) 1869 __acquires(&ino->i_lock) 1870 { 1871 struct nfs_inode *nfsi = NFS_I(ino); 1872 struct pnfs_layout_hdr *new = NULL; 1873 1874 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout); 1875 1876 if (nfsi->layout != NULL) 1877 goto out_existing; 1878 spin_unlock(&ino->i_lock); 1879 new = alloc_init_layout_hdr(ino, ctx, gfp_flags); 1880 spin_lock(&ino->i_lock); 1881 1882 if (likely(nfsi->layout == NULL)) { /* Won the race? */ 1883 nfsi->layout = new; 1884 return new; 1885 } else if (new != NULL) 1886 pnfs_free_layout_hdr(new); 1887 out_existing: 1888 pnfs_get_layout_hdr(nfsi->layout); 1889 return nfsi->layout; 1890 } 1891 1892 /* 1893 * iomode matching rules: 1894 * iomode lseg strict match 1895 * iomode 1896 * ----- ----- ------ ----- 1897 * ANY READ N/A true 1898 * ANY RW N/A true 1899 * RW READ N/A false 1900 * RW RW N/A true 1901 * READ READ N/A true 1902 * READ RW true false 1903 * READ RW false true 1904 */ 1905 static bool 1906 pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range, 1907 const struct pnfs_layout_range *range, 1908 bool strict_iomode) 1909 { 1910 struct pnfs_layout_range range1; 1911 1912 if ((range->iomode == IOMODE_RW && 1913 ls_range->iomode != IOMODE_RW) || 1914 (range->iomode != ls_range->iomode && 1915 strict_iomode) || 1916 !pnfs_lseg_range_intersecting(ls_range, range)) 1917 return false; 1918 1919 /* range1 covers only the first byte in the range */ 1920 range1 = *range; 1921 range1.length = 1; 1922 return pnfs_lseg_range_contained(ls_range, &range1); 1923 } 1924 1925 /* 1926 * lookup range in layout 1927 */ 1928 static struct pnfs_layout_segment * 1929 pnfs_find_lseg(struct pnfs_layout_hdr *lo, 1930 struct pnfs_layout_range *range, 1931 bool strict_iomode) 1932 { 1933 struct pnfs_layout_segment *lseg, *ret = NULL; 1934 1935 dprintk("%s:Begin\n", __func__); 1936 1937 list_for_each_entry(lseg, &lo->plh_segs, pls_list) { 1938 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) && 1939 pnfs_lseg_range_match(&lseg->pls_range, range, 1940 strict_iomode)) { 1941 ret = pnfs_get_lseg(lseg); 1942 break; 1943 } 1944 } 1945 1946 dprintk("%s:Return lseg %p ref %d\n", 1947 __func__, ret, ret ? refcount_read(&ret->pls_refcount) : 0); 1948 return ret; 1949 } 1950 1951 /* 1952 * Use mdsthreshold hints set at each OPEN to determine if I/O should go 1953 * to the MDS or over pNFS 1954 * 1955 * The nfs_inode read_io and write_io fields are cumulative counters reset 1956 * when there are no layout segments. Note that in pnfs_update_layout iomode 1957 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a 1958 * WRITE request. 1959 * 1960 * A return of true means use MDS I/O. 1961 * 1962 * From rfc 5661: 1963 * If a file's size is smaller than the file size threshold, data accesses 1964 * SHOULD be sent to the metadata server. If an I/O request has a length that 1965 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata 1966 * server. If both file size and I/O size are provided, the client SHOULD 1967 * reach or exceed both thresholds before sending its read or write 1968 * requests to the data server. 1969 */ 1970 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx, 1971 struct inode *ino, int iomode) 1972 { 1973 struct nfs4_threshold *t = ctx->mdsthreshold; 1974 struct nfs_inode *nfsi = NFS_I(ino); 1975 loff_t fsize = i_size_read(ino); 1976 bool size = false, size_set = false, io = false, io_set = false, ret = false; 1977 1978 if (t == NULL) 1979 return ret; 1980 1981 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n", 1982 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz); 1983 1984 switch (iomode) { 1985 case IOMODE_READ: 1986 if (t->bm & THRESHOLD_RD) { 1987 dprintk("%s fsize %llu\n", __func__, fsize); 1988 size_set = true; 1989 if (fsize < t->rd_sz) 1990 size = true; 1991 } 1992 if (t->bm & THRESHOLD_RD_IO) { 1993 dprintk("%s nfsi->read_io %llu\n", __func__, 1994 nfsi->read_io); 1995 io_set = true; 1996 if (nfsi->read_io < t->rd_io_sz) 1997 io = true; 1998 } 1999 break; 2000 case IOMODE_RW: 2001 if (t->bm & THRESHOLD_WR) { 2002 dprintk("%s fsize %llu\n", __func__, fsize); 2003 size_set = true; 2004 if (fsize < t->wr_sz) 2005 size = true; 2006 } 2007 if (t->bm & THRESHOLD_WR_IO) { 2008 dprintk("%s nfsi->write_io %llu\n", __func__, 2009 nfsi->write_io); 2010 io_set = true; 2011 if (nfsi->write_io < t->wr_io_sz) 2012 io = true; 2013 } 2014 break; 2015 } 2016 if (size_set && io_set) { 2017 if (size && io) 2018 ret = true; 2019 } else if (size || io) 2020 ret = true; 2021 2022 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret); 2023 return ret; 2024 } 2025 2026 static int pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo) 2027 { 2028 /* 2029 * send layoutcommit as it can hold up layoutreturn due to lseg 2030 * reference 2031 */ 2032 pnfs_layoutcommit_inode(lo->plh_inode, false); 2033 return wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN, 2034 nfs_wait_bit_killable, 2035 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE); 2036 } 2037 2038 static void nfs_layoutget_begin(struct pnfs_layout_hdr *lo) 2039 { 2040 atomic_inc(&lo->plh_outstanding); 2041 } 2042 2043 static void nfs_layoutget_end(struct pnfs_layout_hdr *lo) 2044 { 2045 if (atomic_dec_and_test(&lo->plh_outstanding) && 2046 test_and_clear_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags)) 2047 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN); 2048 } 2049 2050 static bool pnfs_is_first_layoutget(struct pnfs_layout_hdr *lo) 2051 { 2052 return test_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags); 2053 } 2054 2055 static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo) 2056 { 2057 unsigned long *bitlock = &lo->plh_flags; 2058 2059 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock); 2060 smp_mb__after_atomic(); 2061 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET); 2062 } 2063 2064 static void _add_to_server_list(struct pnfs_layout_hdr *lo, 2065 struct nfs_server *server) 2066 { 2067 if (!test_and_set_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) { 2068 struct nfs_client *clp = server->nfs_client; 2069 2070 /* The lo must be on the clp list if there is any 2071 * chance of a CB_LAYOUTRECALL(FILE) coming in. 2072 */ 2073 spin_lock(&clp->cl_lock); 2074 list_add_tail_rcu(&lo->plh_layouts, &server->layouts); 2075 spin_unlock(&clp->cl_lock); 2076 } 2077 } 2078 2079 /* 2080 * Layout segment is retreived from the server if not cached. 2081 * The appropriate layout segment is referenced and returned to the caller. 2082 */ 2083 struct pnfs_layout_segment * 2084 pnfs_update_layout(struct inode *ino, 2085 struct nfs_open_context *ctx, 2086 loff_t pos, 2087 u64 count, 2088 enum pnfs_iomode iomode, 2089 bool strict_iomode, 2090 gfp_t gfp_flags) 2091 { 2092 struct pnfs_layout_range arg = { 2093 .iomode = iomode, 2094 .offset = pos, 2095 .length = count, 2096 }; 2097 unsigned pg_offset; 2098 struct nfs_server *server = NFS_SERVER(ino); 2099 struct nfs_client *clp = server->nfs_client; 2100 struct pnfs_layout_hdr *lo = NULL; 2101 struct pnfs_layout_segment *lseg = NULL; 2102 struct nfs4_layoutget *lgp; 2103 nfs4_stateid stateid; 2104 struct nfs4_exception exception = { 2105 .inode = ino, 2106 }; 2107 unsigned long giveup = jiffies + (clp->cl_lease_time << 1); 2108 bool first; 2109 2110 if (!pnfs_enabled_sb(NFS_SERVER(ino))) { 2111 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, 2112 PNFS_UPDATE_LAYOUT_NO_PNFS); 2113 goto out; 2114 } 2115 2116 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) { 2117 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, 2118 PNFS_UPDATE_LAYOUT_MDSTHRESH); 2119 goto out; 2120 } 2121 2122 lookup_again: 2123 if (!nfs4_valid_open_stateid(ctx->state)) { 2124 trace_pnfs_update_layout(ino, pos, count, 2125 iomode, lo, lseg, 2126 PNFS_UPDATE_LAYOUT_INVALID_OPEN); 2127 lseg = ERR_PTR(-EIO); 2128 goto out; 2129 } 2130 2131 lseg = ERR_PTR(nfs4_client_recover_expired_lease(clp)); 2132 if (IS_ERR(lseg)) 2133 goto out; 2134 first = false; 2135 spin_lock(&ino->i_lock); 2136 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags); 2137 if (lo == NULL) { 2138 spin_unlock(&ino->i_lock); 2139 lseg = ERR_PTR(-ENOMEM); 2140 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, 2141 PNFS_UPDATE_LAYOUT_NOMEM); 2142 goto out; 2143 } 2144 2145 /* Do we even need to bother with this? */ 2146 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { 2147 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, 2148 PNFS_UPDATE_LAYOUT_BULK_RECALL); 2149 dprintk("%s matches recall, use MDS\n", __func__); 2150 goto out_unlock; 2151 } 2152 2153 /* if LAYOUTGET already failed once we don't try again */ 2154 if (pnfs_layout_io_test_failed(lo, iomode)) { 2155 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, 2156 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL); 2157 goto out_unlock; 2158 } 2159 2160 /* 2161 * If the layout segment list is empty, but there are outstanding 2162 * layoutget calls, then they might be subject to a layoutrecall. 2163 */ 2164 if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) && 2165 atomic_read(&lo->plh_outstanding) != 0) { 2166 spin_unlock(&ino->i_lock); 2167 lseg = ERR_PTR(wait_on_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN, 2168 TASK_KILLABLE)); 2169 if (IS_ERR(lseg)) 2170 goto out_put_layout_hdr; 2171 pnfs_put_layout_hdr(lo); 2172 goto lookup_again; 2173 } 2174 2175 /* 2176 * Because we free lsegs when sending LAYOUTRETURN, we need to wait 2177 * for LAYOUTRETURN. 2178 */ 2179 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) { 2180 spin_unlock(&ino->i_lock); 2181 dprintk("%s wait for layoutreturn\n", __func__); 2182 lseg = ERR_PTR(pnfs_prepare_to_retry_layoutget(lo)); 2183 if (!IS_ERR(lseg)) { 2184 pnfs_put_layout_hdr(lo); 2185 dprintk("%s retrying\n", __func__); 2186 trace_pnfs_update_layout(ino, pos, count, iomode, lo, 2187 lseg, 2188 PNFS_UPDATE_LAYOUT_RETRY); 2189 goto lookup_again; 2190 } 2191 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, 2192 PNFS_UPDATE_LAYOUT_RETURN); 2193 goto out_put_layout_hdr; 2194 } 2195 2196 lseg = pnfs_find_lseg(lo, &arg, strict_iomode); 2197 if (lseg) { 2198 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, 2199 PNFS_UPDATE_LAYOUT_FOUND_CACHED); 2200 goto out_unlock; 2201 } 2202 2203 /* 2204 * Choose a stateid for the LAYOUTGET. If we don't have a layout 2205 * stateid, or it has been invalidated, then we must use the open 2206 * stateid. 2207 */ 2208 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) { 2209 int status; 2210 2211 /* 2212 * The first layoutget for the file. Need to serialize per 2213 * RFC 5661 Errata 3208. 2214 */ 2215 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, 2216 &lo->plh_flags)) { 2217 spin_unlock(&ino->i_lock); 2218 lseg = ERR_PTR(wait_on_bit(&lo->plh_flags, 2219 NFS_LAYOUT_FIRST_LAYOUTGET, 2220 TASK_KILLABLE)); 2221 if (IS_ERR(lseg)) 2222 goto out_put_layout_hdr; 2223 pnfs_put_layout_hdr(lo); 2224 dprintk("%s retrying\n", __func__); 2225 goto lookup_again; 2226 } 2227 2228 spin_unlock(&ino->i_lock); 2229 first = true; 2230 status = nfs4_select_rw_stateid(ctx->state, 2231 iomode == IOMODE_RW ? FMODE_WRITE : FMODE_READ, 2232 NULL, &stateid, NULL); 2233 if (status != 0) { 2234 lseg = ERR_PTR(status); 2235 trace_pnfs_update_layout(ino, pos, count, 2236 iomode, lo, lseg, 2237 PNFS_UPDATE_LAYOUT_INVALID_OPEN); 2238 nfs4_schedule_stateid_recovery(server, ctx->state); 2239 pnfs_clear_first_layoutget(lo); 2240 pnfs_put_layout_hdr(lo); 2241 goto lookup_again; 2242 } 2243 spin_lock(&ino->i_lock); 2244 } else { 2245 nfs4_stateid_copy(&stateid, &lo->plh_stateid); 2246 } 2247 2248 if (pnfs_layoutgets_blocked(lo)) { 2249 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, 2250 PNFS_UPDATE_LAYOUT_BLOCKED); 2251 goto out_unlock; 2252 } 2253 nfs_layoutget_begin(lo); 2254 spin_unlock(&ino->i_lock); 2255 2256 _add_to_server_list(lo, server); 2257 2258 pg_offset = arg.offset & ~PAGE_MASK; 2259 if (pg_offset) { 2260 arg.offset -= pg_offset; 2261 arg.length += pg_offset; 2262 } 2263 if (arg.length != NFS4_MAX_UINT64) 2264 arg.length = PAGE_ALIGN(arg.length); 2265 2266 lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &stateid, &arg, gfp_flags); 2267 if (!lgp) { 2268 lseg = ERR_PTR(-ENOMEM); 2269 trace_pnfs_update_layout(ino, pos, count, iomode, lo, NULL, 2270 PNFS_UPDATE_LAYOUT_NOMEM); 2271 nfs_layoutget_end(lo); 2272 goto out_put_layout_hdr; 2273 } 2274 2275 lgp->lo = lo; 2276 pnfs_get_layout_hdr(lo); 2277 2278 lseg = nfs4_proc_layoutget(lgp, &exception); 2279 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, 2280 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET); 2281 nfs_layoutget_end(lo); 2282 if (IS_ERR(lseg)) { 2283 switch(PTR_ERR(lseg)) { 2284 case -EBUSY: 2285 if (time_after(jiffies, giveup)) 2286 lseg = NULL; 2287 break; 2288 case -ERECALLCONFLICT: 2289 case -EAGAIN: 2290 break; 2291 case -ENODATA: 2292 /* The server returned NFS4ERR_LAYOUTUNAVAILABLE */ 2293 pnfs_layout_set_fail_bit( 2294 lo, pnfs_iomode_to_fail_bit(iomode)); 2295 lseg = NULL; 2296 goto out_put_layout_hdr; 2297 default: 2298 if (!nfs_error_is_fatal(PTR_ERR(lseg))) { 2299 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); 2300 lseg = NULL; 2301 } 2302 goto out_put_layout_hdr; 2303 } 2304 if (lseg) { 2305 if (!exception.retry) 2306 goto out_put_layout_hdr; 2307 if (first) 2308 pnfs_clear_first_layoutget(lo); 2309 trace_pnfs_update_layout(ino, pos, count, 2310 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY); 2311 pnfs_put_layout_hdr(lo); 2312 goto lookup_again; 2313 } 2314 } else { 2315 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); 2316 } 2317 2318 out_put_layout_hdr: 2319 if (first) 2320 pnfs_clear_first_layoutget(lo); 2321 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg, 2322 PNFS_UPDATE_LAYOUT_EXIT); 2323 pnfs_put_layout_hdr(lo); 2324 out: 2325 dprintk("%s: inode %s/%llu pNFS layout segment %s for " 2326 "(%s, offset: %llu, length: %llu)\n", 2327 __func__, ino->i_sb->s_id, 2328 (unsigned long long)NFS_FILEID(ino), 2329 IS_ERR_OR_NULL(lseg) ? "not found" : "found", 2330 iomode==IOMODE_RW ? "read/write" : "read-only", 2331 (unsigned long long)pos, 2332 (unsigned long long)count); 2333 return lseg; 2334 out_unlock: 2335 spin_unlock(&ino->i_lock); 2336 goto out_put_layout_hdr; 2337 } 2338 EXPORT_SYMBOL_GPL(pnfs_update_layout); 2339 2340 static bool 2341 pnfs_sanity_check_layout_range(struct pnfs_layout_range *range) 2342 { 2343 switch (range->iomode) { 2344 case IOMODE_READ: 2345 case IOMODE_RW: 2346 break; 2347 default: 2348 return false; 2349 } 2350 if (range->offset == NFS4_MAX_UINT64) 2351 return false; 2352 if (range->length == 0) 2353 return false; 2354 if (range->length != NFS4_MAX_UINT64 && 2355 range->length > NFS4_MAX_UINT64 - range->offset) 2356 return false; 2357 return true; 2358 } 2359 2360 static struct pnfs_layout_hdr * 2361 _pnfs_grab_empty_layout(struct inode *ino, struct nfs_open_context *ctx) 2362 { 2363 struct pnfs_layout_hdr *lo; 2364 2365 spin_lock(&ino->i_lock); 2366 lo = pnfs_find_alloc_layout(ino, ctx, nfs_io_gfp_mask()); 2367 if (!lo) 2368 goto out_unlock; 2369 if (!test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) 2370 goto out_unlock; 2371 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) 2372 goto out_unlock; 2373 if (pnfs_layoutgets_blocked(lo)) 2374 goto out_unlock; 2375 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags)) 2376 goto out_unlock; 2377 nfs_layoutget_begin(lo); 2378 spin_unlock(&ino->i_lock); 2379 _add_to_server_list(lo, NFS_SERVER(ino)); 2380 return lo; 2381 2382 out_unlock: 2383 spin_unlock(&ino->i_lock); 2384 pnfs_put_layout_hdr(lo); 2385 return NULL; 2386 } 2387 2388 static void _lgopen_prepare_attached(struct nfs4_opendata *data, 2389 struct nfs_open_context *ctx) 2390 { 2391 struct inode *ino = data->dentry->d_inode; 2392 struct pnfs_layout_range rng = { 2393 .iomode = (data->o_arg.fmode & FMODE_WRITE) ? 2394 IOMODE_RW: IOMODE_READ, 2395 .offset = 0, 2396 .length = NFS4_MAX_UINT64, 2397 }; 2398 struct nfs4_layoutget *lgp; 2399 struct pnfs_layout_hdr *lo; 2400 2401 /* Heuristic: don't send layoutget if we have cached data */ 2402 if (rng.iomode == IOMODE_READ && 2403 (i_size_read(ino) == 0 || ino->i_mapping->nrpages != 0)) 2404 return; 2405 2406 lo = _pnfs_grab_empty_layout(ino, ctx); 2407 if (!lo) 2408 return; 2409 lgp = pnfs_alloc_init_layoutget_args(ino, ctx, ¤t_stateid, &rng, 2410 nfs_io_gfp_mask()); 2411 if (!lgp) { 2412 pnfs_clear_first_layoutget(lo); 2413 nfs_layoutget_end(lo); 2414 pnfs_put_layout_hdr(lo); 2415 return; 2416 } 2417 lgp->lo = lo; 2418 data->lgp = lgp; 2419 data->o_arg.lg_args = &lgp->args; 2420 data->o_res.lg_res = &lgp->res; 2421 } 2422 2423 static void _lgopen_prepare_floating(struct nfs4_opendata *data, 2424 struct nfs_open_context *ctx) 2425 { 2426 struct inode *ino = data->dentry->d_inode; 2427 struct pnfs_layout_range rng = { 2428 .iomode = (data->o_arg.fmode & FMODE_WRITE) ? 2429 IOMODE_RW: IOMODE_READ, 2430 .offset = 0, 2431 .length = NFS4_MAX_UINT64, 2432 }; 2433 struct nfs4_layoutget *lgp; 2434 2435 lgp = pnfs_alloc_init_layoutget_args(ino, ctx, ¤t_stateid, &rng, 2436 nfs_io_gfp_mask()); 2437 if (!lgp) 2438 return; 2439 data->lgp = lgp; 2440 data->o_arg.lg_args = &lgp->args; 2441 data->o_res.lg_res = &lgp->res; 2442 } 2443 2444 void pnfs_lgopen_prepare(struct nfs4_opendata *data, 2445 struct nfs_open_context *ctx) 2446 { 2447 struct nfs_server *server = NFS_SERVER(data->dir->d_inode); 2448 2449 if (!(pnfs_enabled_sb(server) && 2450 server->pnfs_curr_ld->flags & PNFS_LAYOUTGET_ON_OPEN)) 2451 return; 2452 /* Could check on max_ops, but currently hardcoded high enough */ 2453 if (!nfs_server_capable(data->dir->d_inode, NFS_CAP_LGOPEN)) 2454 return; 2455 if (data->lgp) 2456 return; 2457 if (data->state) 2458 _lgopen_prepare_attached(data, ctx); 2459 else 2460 _lgopen_prepare_floating(data, ctx); 2461 } 2462 2463 void pnfs_parse_lgopen(struct inode *ino, struct nfs4_layoutget *lgp, 2464 struct nfs_open_context *ctx) 2465 { 2466 struct pnfs_layout_hdr *lo; 2467 struct pnfs_layout_segment *lseg; 2468 struct nfs_server *srv = NFS_SERVER(ino); 2469 u32 iomode; 2470 2471 if (!lgp) 2472 return; 2473 dprintk("%s: entered with status %i\n", __func__, lgp->res.status); 2474 if (lgp->res.status) { 2475 switch (lgp->res.status) { 2476 default: 2477 break; 2478 /* 2479 * Halt lgopen attempts if the server doesn't recognise 2480 * the "current stateid" value, the layout type, or the 2481 * layoutget operation as being valid. 2482 * Also if it complains about too many ops in the compound 2483 * or of the request/reply being too big. 2484 */ 2485 case -NFS4ERR_BAD_STATEID: 2486 case -NFS4ERR_NOTSUPP: 2487 case -NFS4ERR_REP_TOO_BIG: 2488 case -NFS4ERR_REP_TOO_BIG_TO_CACHE: 2489 case -NFS4ERR_REQ_TOO_BIG: 2490 case -NFS4ERR_TOO_MANY_OPS: 2491 case -NFS4ERR_UNKNOWN_LAYOUTTYPE: 2492 srv->caps &= ~NFS_CAP_LGOPEN; 2493 } 2494 return; 2495 } 2496 if (!lgp->lo) { 2497 lo = _pnfs_grab_empty_layout(ino, ctx); 2498 if (!lo) 2499 return; 2500 lgp->lo = lo; 2501 } else 2502 lo = lgp->lo; 2503 2504 lseg = pnfs_layout_process(lgp); 2505 if (!IS_ERR(lseg)) { 2506 iomode = lgp->args.range.iomode; 2507 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode)); 2508 pnfs_put_lseg(lseg); 2509 } 2510 } 2511 2512 void nfs4_lgopen_release(struct nfs4_layoutget *lgp) 2513 { 2514 if (lgp != NULL) { 2515 if (lgp->lo) { 2516 pnfs_clear_first_layoutget(lgp->lo); 2517 nfs_layoutget_end(lgp->lo); 2518 } 2519 pnfs_layoutget_free(lgp); 2520 } 2521 } 2522 2523 struct pnfs_layout_segment * 2524 pnfs_layout_process(struct nfs4_layoutget *lgp) 2525 { 2526 struct pnfs_layout_hdr *lo = lgp->lo; 2527 struct nfs4_layoutget_res *res = &lgp->res; 2528 struct pnfs_layout_segment *lseg; 2529 struct inode *ino = lo->plh_inode; 2530 LIST_HEAD(free_me); 2531 2532 if (!pnfs_sanity_check_layout_range(&res->range)) 2533 return ERR_PTR(-EINVAL); 2534 2535 /* Inject layout blob into I/O device driver */ 2536 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags); 2537 if (IS_ERR_OR_NULL(lseg)) { 2538 if (!lseg) 2539 lseg = ERR_PTR(-ENOMEM); 2540 2541 dprintk("%s: Could not allocate layout: error %ld\n", 2542 __func__, PTR_ERR(lseg)); 2543 return lseg; 2544 } 2545 2546 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid); 2547 2548 spin_lock(&ino->i_lock); 2549 if (pnfs_layoutgets_blocked(lo)) { 2550 dprintk("%s forget reply due to state\n", __func__); 2551 goto out_forget; 2552 } 2553 2554 if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) && 2555 !pnfs_is_first_layoutget(lo)) 2556 goto out_forget; 2557 2558 if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) { 2559 /* existing state ID, make sure the sequence number matches. */ 2560 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) { 2561 if (!pnfs_layout_is_valid(lo)) 2562 lo->plh_barrier = 0; 2563 dprintk("%s forget reply due to sequence\n", __func__); 2564 goto out_forget; 2565 } 2566 pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, false); 2567 } else if (pnfs_layout_is_valid(lo)) { 2568 /* 2569 * We got an entirely new state ID. Mark all segments for the 2570 * inode invalid, and retry the layoutget 2571 */ 2572 struct pnfs_layout_range range = { 2573 .iomode = IOMODE_ANY, 2574 .length = NFS4_MAX_UINT64, 2575 }; 2576 pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0); 2577 goto out_forget; 2578 } else { 2579 /* We have a completely new layout */ 2580 pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, true); 2581 } 2582 2583 pnfs_get_lseg(lseg); 2584 pnfs_layout_insert_lseg(lo, lseg, &free_me); 2585 2586 2587 if (res->return_on_close) 2588 set_bit(NFS_LSEG_ROC, &lseg->pls_flags); 2589 2590 spin_unlock(&ino->i_lock); 2591 pnfs_free_lseg_list(&free_me); 2592 return lseg; 2593 2594 out_forget: 2595 spin_unlock(&ino->i_lock); 2596 lseg->pls_layout = lo; 2597 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg); 2598 return ERR_PTR(-EAGAIN); 2599 } 2600 2601 /** 2602 * pnfs_mark_matching_lsegs_return - Free or return matching layout segments 2603 * @lo: pointer to layout header 2604 * @tmp_list: list header to be used with pnfs_free_lseg_list() 2605 * @return_range: describe layout segment ranges to be returned 2606 * @seq: stateid seqid to match 2607 * 2608 * This function is mainly intended for use by layoutrecall. It attempts 2609 * to free the layout segment immediately, or else to mark it for return 2610 * as soon as its reference count drops to zero. 2611 * 2612 * Returns 2613 * - 0: a layoutreturn needs to be scheduled. 2614 * - EBUSY: there are layout segment that are still in use. 2615 * - ENOENT: there are no layout segments that need to be returned. 2616 */ 2617 int 2618 pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo, 2619 struct list_head *tmp_list, 2620 const struct pnfs_layout_range *return_range, 2621 u32 seq) 2622 { 2623 struct pnfs_layout_segment *lseg, *next; 2624 struct nfs_server *server = NFS_SERVER(lo->plh_inode); 2625 int remaining = 0; 2626 2627 dprintk("%s:Begin lo %p\n", __func__, lo); 2628 2629 assert_spin_locked(&lo->plh_inode->i_lock); 2630 2631 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) 2632 tmp_list = &lo->plh_return_segs; 2633 2634 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) 2635 if (pnfs_match_lseg_recall(lseg, return_range, seq)) { 2636 dprintk("%s: marking lseg %p iomode %d " 2637 "offset %llu length %llu\n", __func__, 2638 lseg, lseg->pls_range.iomode, 2639 lseg->pls_range.offset, 2640 lseg->pls_range.length); 2641 if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags)) 2642 tmp_list = &lo->plh_return_segs; 2643 if (mark_lseg_invalid(lseg, tmp_list)) 2644 continue; 2645 remaining++; 2646 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags); 2647 pnfs_lseg_cancel_io(server, lseg); 2648 } 2649 2650 if (remaining) { 2651 pnfs_set_plh_return_info(lo, return_range->iomode, seq); 2652 return -EBUSY; 2653 } 2654 2655 if (!list_empty(&lo->plh_return_segs)) { 2656 pnfs_set_plh_return_info(lo, return_range->iomode, seq); 2657 return 0; 2658 } 2659 2660 return -ENOENT; 2661 } 2662 2663 static void 2664 pnfs_mark_layout_for_return(struct inode *inode, 2665 const struct pnfs_layout_range *range) 2666 { 2667 struct pnfs_layout_hdr *lo; 2668 bool return_now = false; 2669 2670 spin_lock(&inode->i_lock); 2671 lo = NFS_I(inode)->layout; 2672 if (!pnfs_layout_is_valid(lo)) { 2673 spin_unlock(&inode->i_lock); 2674 return; 2675 } 2676 pnfs_set_plh_return_info(lo, range->iomode, 0); 2677 /* 2678 * mark all matching lsegs so that we are sure to have no live 2679 * segments at hand when sending layoutreturn. See pnfs_put_lseg() 2680 * for how it works. 2681 */ 2682 if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, range, 0) != -EBUSY) { 2683 const struct cred *cred; 2684 nfs4_stateid stateid; 2685 enum pnfs_iomode iomode; 2686 2687 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode); 2688 spin_unlock(&inode->i_lock); 2689 if (return_now) 2690 pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, 2691 PNFS_FL_LAYOUTRETURN_ASYNC); 2692 } else { 2693 spin_unlock(&inode->i_lock); 2694 nfs_commit_inode(inode, 0); 2695 } 2696 } 2697 2698 void pnfs_error_mark_layout_for_return(struct inode *inode, 2699 struct pnfs_layout_segment *lseg) 2700 { 2701 struct pnfs_layout_range range = { 2702 .iomode = lseg->pls_range.iomode, 2703 .offset = 0, 2704 .length = NFS4_MAX_UINT64, 2705 }; 2706 2707 pnfs_mark_layout_for_return(inode, &range); 2708 } 2709 EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return); 2710 2711 static bool 2712 pnfs_layout_can_be_returned(struct pnfs_layout_hdr *lo) 2713 { 2714 return pnfs_layout_is_valid(lo) && 2715 !test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) && 2716 !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags); 2717 } 2718 2719 static struct pnfs_layout_segment * 2720 pnfs_find_first_lseg(struct pnfs_layout_hdr *lo, 2721 const struct pnfs_layout_range *range, 2722 enum pnfs_iomode iomode) 2723 { 2724 struct pnfs_layout_segment *lseg; 2725 2726 list_for_each_entry(lseg, &lo->plh_segs, pls_list) { 2727 if (!test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) 2728 continue; 2729 if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags)) 2730 continue; 2731 if (lseg->pls_range.iomode != iomode && iomode != IOMODE_ANY) 2732 continue; 2733 if (pnfs_lseg_range_intersecting(&lseg->pls_range, range)) 2734 return lseg; 2735 } 2736 return NULL; 2737 } 2738 2739 /* Find open file states whose mode matches that of the range */ 2740 static bool 2741 pnfs_should_return_unused_layout(struct pnfs_layout_hdr *lo, 2742 const struct pnfs_layout_range *range) 2743 { 2744 struct list_head *head; 2745 struct nfs_open_context *ctx; 2746 fmode_t mode = 0; 2747 2748 if (!pnfs_layout_can_be_returned(lo) || 2749 !pnfs_find_first_lseg(lo, range, range->iomode)) 2750 return false; 2751 2752 head = &NFS_I(lo->plh_inode)->open_files; 2753 list_for_each_entry_rcu(ctx, head, list) { 2754 if (ctx->state) 2755 mode |= ctx->state->state & (FMODE_READ|FMODE_WRITE); 2756 } 2757 2758 switch (range->iomode) { 2759 default: 2760 break; 2761 case IOMODE_READ: 2762 mode &= ~FMODE_WRITE; 2763 break; 2764 case IOMODE_RW: 2765 if (pnfs_find_first_lseg(lo, range, IOMODE_READ)) 2766 mode &= ~FMODE_READ; 2767 } 2768 return mode == 0; 2769 } 2770 2771 static int pnfs_layout_return_unused_byserver(struct nfs_server *server, 2772 void *data) 2773 { 2774 const struct pnfs_layout_range *range = data; 2775 const struct cred *cred; 2776 struct pnfs_layout_hdr *lo; 2777 struct inode *inode; 2778 nfs4_stateid stateid; 2779 enum pnfs_iomode iomode; 2780 2781 restart: 2782 rcu_read_lock(); 2783 list_for_each_entry_rcu(lo, &server->layouts, plh_layouts) { 2784 inode = lo->plh_inode; 2785 if (!inode || !pnfs_layout_can_be_returned(lo) || 2786 test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) 2787 continue; 2788 spin_lock(&inode->i_lock); 2789 if (!lo->plh_inode || 2790 !pnfs_should_return_unused_layout(lo, range)) { 2791 spin_unlock(&inode->i_lock); 2792 continue; 2793 } 2794 pnfs_get_layout_hdr(lo); 2795 pnfs_set_plh_return_info(lo, range->iomode, 0); 2796 if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, 2797 range, 0) != 0 || 2798 !pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode)) { 2799 spin_unlock(&inode->i_lock); 2800 rcu_read_unlock(); 2801 pnfs_put_layout_hdr(lo); 2802 cond_resched(); 2803 goto restart; 2804 } 2805 spin_unlock(&inode->i_lock); 2806 rcu_read_unlock(); 2807 pnfs_send_layoutreturn(lo, &stateid, &cred, iomode, 2808 PNFS_FL_LAYOUTRETURN_ASYNC); 2809 pnfs_put_layout_hdr(lo); 2810 cond_resched(); 2811 goto restart; 2812 } 2813 rcu_read_unlock(); 2814 return 0; 2815 } 2816 2817 void 2818 pnfs_layout_return_unused_byclid(struct nfs_client *clp, 2819 enum pnfs_iomode iomode) 2820 { 2821 struct pnfs_layout_range range = { 2822 .iomode = iomode, 2823 .offset = 0, 2824 .length = NFS4_MAX_UINT64, 2825 }; 2826 2827 nfs_client_for_each_server(clp, pnfs_layout_return_unused_byserver, 2828 &range); 2829 } 2830 2831 /* Check if we have we have a valid layout but if there isn't an intersection 2832 * between the request and the pgio->pg_lseg, put this pgio->pg_lseg away. 2833 */ 2834 void 2835 pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio, 2836 struct nfs_page *req) 2837 { 2838 if (pgio->pg_lseg == NULL || 2839 (test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags) && 2840 pnfs_lseg_request_intersecting(pgio->pg_lseg, req))) 2841 return; 2842 pnfs_put_lseg(pgio->pg_lseg); 2843 pgio->pg_lseg = NULL; 2844 } 2845 EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout); 2846 2847 void 2848 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req) 2849 { 2850 u64 rd_size; 2851 2852 pnfs_generic_pg_check_layout(pgio, req); 2853 if (pgio->pg_lseg == NULL) { 2854 if (pgio->pg_dreq == NULL) 2855 rd_size = i_size_read(pgio->pg_inode) - req_offset(req); 2856 else 2857 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq, 2858 req_offset(req)); 2859 2860 pgio->pg_lseg = 2861 pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req), 2862 req_offset(req), rd_size, 2863 IOMODE_READ, false, 2864 nfs_io_gfp_mask()); 2865 if (IS_ERR(pgio->pg_lseg)) { 2866 pgio->pg_error = PTR_ERR(pgio->pg_lseg); 2867 pgio->pg_lseg = NULL; 2868 return; 2869 } 2870 } 2871 /* If no lseg, fall back to read through mds */ 2872 if (pgio->pg_lseg == NULL) 2873 nfs_pageio_reset_read_mds(pgio); 2874 2875 } 2876 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read); 2877 2878 void 2879 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, 2880 struct nfs_page *req, u64 wb_size) 2881 { 2882 pnfs_generic_pg_check_layout(pgio, req); 2883 if (pgio->pg_lseg == NULL) { 2884 pgio->pg_lseg = 2885 pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req), 2886 req_offset(req), wb_size, IOMODE_RW, 2887 false, nfs_io_gfp_mask()); 2888 if (IS_ERR(pgio->pg_lseg)) { 2889 pgio->pg_error = PTR_ERR(pgio->pg_lseg); 2890 pgio->pg_lseg = NULL; 2891 return; 2892 } 2893 } 2894 /* If no lseg, fall back to write through mds */ 2895 if (pgio->pg_lseg == NULL) 2896 nfs_pageio_reset_write_mds(pgio); 2897 } 2898 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write); 2899 2900 void 2901 pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc) 2902 { 2903 if (desc->pg_lseg) { 2904 pnfs_put_lseg(desc->pg_lseg); 2905 desc->pg_lseg = NULL; 2906 } 2907 } 2908 EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup); 2909 2910 /* 2911 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number 2912 * of bytes (maximum @req->wb_bytes) that can be coalesced. 2913 */ 2914 size_t 2915 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, 2916 struct nfs_page *prev, struct nfs_page *req) 2917 { 2918 unsigned int size; 2919 u64 seg_end, req_start, seg_left; 2920 2921 size = nfs_generic_pg_test(pgio, prev, req); 2922 if (!size) 2923 return 0; 2924 2925 /* 2926 * 'size' contains the number of bytes left in the current page (up 2927 * to the original size asked for in @req->wb_bytes). 2928 * 2929 * Calculate how many bytes are left in the layout segment 2930 * and if there are less bytes than 'size', return that instead. 2931 * 2932 * Please also note that 'end_offset' is actually the offset of the 2933 * first byte that lies outside the pnfs_layout_range. FIXME? 2934 * 2935 */ 2936 if (pgio->pg_lseg) { 2937 seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset, 2938 pgio->pg_lseg->pls_range.length); 2939 req_start = req_offset(req); 2940 2941 /* start of request is past the last byte of this segment */ 2942 if (req_start >= seg_end) 2943 return 0; 2944 2945 /* adjust 'size' iff there are fewer bytes left in the 2946 * segment than what nfs_generic_pg_test returned */ 2947 seg_left = seg_end - req_start; 2948 if (seg_left < size) 2949 size = (unsigned int)seg_left; 2950 } 2951 2952 return size; 2953 } 2954 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test); 2955 2956 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr) 2957 { 2958 struct nfs_pageio_descriptor pgio; 2959 2960 /* Resend all requests through the MDS */ 2961 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true, 2962 hdr->completion_ops); 2963 return nfs_pageio_resend(&pgio, hdr); 2964 } 2965 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds); 2966 2967 static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr) 2968 { 2969 2970 dprintk("pnfs write error = %d\n", hdr->pnfs_error); 2971 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & 2972 PNFS_LAYOUTRET_ON_ERROR) { 2973 pnfs_return_layout(hdr->inode); 2974 } 2975 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) 2976 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr); 2977 } 2978 2979 /* 2980 * Called by non rpc-based layout drivers 2981 */ 2982 void pnfs_ld_write_done(struct nfs_pgio_header *hdr) 2983 { 2984 if (likely(!hdr->pnfs_error)) { 2985 pnfs_set_layoutcommit(hdr->inode, hdr->lseg, 2986 hdr->mds_offset + hdr->res.count); 2987 hdr->mds_ops->rpc_call_done(&hdr->task, hdr); 2988 } 2989 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error); 2990 if (unlikely(hdr->pnfs_error)) 2991 pnfs_ld_handle_write_error(hdr); 2992 hdr->mds_ops->rpc_release(hdr); 2993 } 2994 EXPORT_SYMBOL_GPL(pnfs_ld_write_done); 2995 2996 static void 2997 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc, 2998 struct nfs_pgio_header *hdr) 2999 { 3000 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 3001 3002 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { 3003 list_splice_tail_init(&hdr->pages, &mirror->pg_list); 3004 nfs_pageio_reset_write_mds(desc); 3005 mirror->pg_recoalesce = 1; 3006 } 3007 hdr->completion_ops->completion(hdr); 3008 } 3009 3010 static enum pnfs_try_status 3011 pnfs_try_to_write_data(struct nfs_pgio_header *hdr, 3012 const struct rpc_call_ops *call_ops, 3013 struct pnfs_layout_segment *lseg, 3014 int how) 3015 { 3016 struct inode *inode = hdr->inode; 3017 enum pnfs_try_status trypnfs; 3018 struct nfs_server *nfss = NFS_SERVER(inode); 3019 3020 hdr->mds_ops = call_ops; 3021 3022 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__, 3023 inode->i_ino, hdr->args.count, hdr->args.offset, how); 3024 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how); 3025 if (trypnfs != PNFS_NOT_ATTEMPTED) 3026 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE); 3027 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); 3028 return trypnfs; 3029 } 3030 3031 static void 3032 pnfs_do_write(struct nfs_pageio_descriptor *desc, 3033 struct nfs_pgio_header *hdr, int how) 3034 { 3035 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; 3036 struct pnfs_layout_segment *lseg = desc->pg_lseg; 3037 enum pnfs_try_status trypnfs; 3038 3039 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how); 3040 switch (trypnfs) { 3041 case PNFS_NOT_ATTEMPTED: 3042 pnfs_write_through_mds(desc, hdr); 3043 break; 3044 case PNFS_ATTEMPTED: 3045 break; 3046 case PNFS_TRY_AGAIN: 3047 /* cleanup hdr and prepare to redo pnfs */ 3048 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { 3049 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 3050 list_splice_init(&hdr->pages, &mirror->pg_list); 3051 mirror->pg_recoalesce = 1; 3052 } 3053 hdr->mds_ops->rpc_release(hdr); 3054 } 3055 } 3056 3057 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr) 3058 { 3059 pnfs_put_lseg(hdr->lseg); 3060 nfs_pgio_header_free(hdr); 3061 } 3062 3063 int 3064 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) 3065 { 3066 struct nfs_pgio_header *hdr; 3067 int ret; 3068 3069 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); 3070 if (!hdr) { 3071 desc->pg_error = -ENOMEM; 3072 return desc->pg_error; 3073 } 3074 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free); 3075 3076 hdr->lseg = pnfs_get_lseg(desc->pg_lseg); 3077 ret = nfs_generic_pgio(desc, hdr); 3078 if (!ret) 3079 pnfs_do_write(desc, hdr, desc->pg_ioflags); 3080 3081 return ret; 3082 } 3083 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages); 3084 3085 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr) 3086 { 3087 struct nfs_pageio_descriptor pgio; 3088 3089 /* Resend all requests through the MDS */ 3090 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops); 3091 return nfs_pageio_resend(&pgio, hdr); 3092 } 3093 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds); 3094 3095 static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr) 3096 { 3097 dprintk("pnfs read error = %d\n", hdr->pnfs_error); 3098 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags & 3099 PNFS_LAYOUTRET_ON_ERROR) { 3100 pnfs_return_layout(hdr->inode); 3101 } 3102 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) 3103 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr); 3104 } 3105 3106 /* 3107 * Called by non rpc-based layout drivers 3108 */ 3109 void pnfs_ld_read_done(struct nfs_pgio_header *hdr) 3110 { 3111 if (likely(!hdr->pnfs_error)) 3112 hdr->mds_ops->rpc_call_done(&hdr->task, hdr); 3113 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error); 3114 if (unlikely(hdr->pnfs_error)) 3115 pnfs_ld_handle_read_error(hdr); 3116 hdr->mds_ops->rpc_release(hdr); 3117 } 3118 EXPORT_SYMBOL_GPL(pnfs_ld_read_done); 3119 3120 static void 3121 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc, 3122 struct nfs_pgio_header *hdr) 3123 { 3124 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 3125 3126 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { 3127 list_splice_tail_init(&hdr->pages, &mirror->pg_list); 3128 nfs_pageio_reset_read_mds(desc); 3129 mirror->pg_recoalesce = 1; 3130 } 3131 hdr->completion_ops->completion(hdr); 3132 } 3133 3134 /* 3135 * Call the appropriate parallel I/O subsystem read function. 3136 */ 3137 static enum pnfs_try_status 3138 pnfs_try_to_read_data(struct nfs_pgio_header *hdr, 3139 const struct rpc_call_ops *call_ops, 3140 struct pnfs_layout_segment *lseg) 3141 { 3142 struct inode *inode = hdr->inode; 3143 struct nfs_server *nfss = NFS_SERVER(inode); 3144 enum pnfs_try_status trypnfs; 3145 3146 hdr->mds_ops = call_ops; 3147 3148 dprintk("%s: Reading ino:%lu %u@%llu\n", 3149 __func__, inode->i_ino, hdr->args.count, hdr->args.offset); 3150 3151 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr); 3152 if (trypnfs != PNFS_NOT_ATTEMPTED) 3153 nfs_inc_stats(inode, NFSIOS_PNFS_READ); 3154 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs); 3155 return trypnfs; 3156 } 3157 3158 /* Resend all requests through pnfs. */ 3159 void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr, 3160 unsigned int mirror_idx) 3161 { 3162 struct nfs_pageio_descriptor pgio; 3163 3164 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { 3165 /* Prevent deadlocks with layoutreturn! */ 3166 pnfs_put_lseg(hdr->lseg); 3167 hdr->lseg = NULL; 3168 3169 nfs_pageio_init_read(&pgio, hdr->inode, false, 3170 hdr->completion_ops); 3171 pgio.pg_mirror_idx = mirror_idx; 3172 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr); 3173 } 3174 } 3175 EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs); 3176 3177 static void 3178 pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr) 3179 { 3180 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops; 3181 struct pnfs_layout_segment *lseg = desc->pg_lseg; 3182 enum pnfs_try_status trypnfs; 3183 3184 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg); 3185 switch (trypnfs) { 3186 case PNFS_NOT_ATTEMPTED: 3187 pnfs_read_through_mds(desc, hdr); 3188 break; 3189 case PNFS_ATTEMPTED: 3190 break; 3191 case PNFS_TRY_AGAIN: 3192 /* cleanup hdr and prepare to redo pnfs */ 3193 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { 3194 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc); 3195 list_splice_init(&hdr->pages, &mirror->pg_list); 3196 mirror->pg_recoalesce = 1; 3197 } 3198 hdr->mds_ops->rpc_release(hdr); 3199 } 3200 } 3201 3202 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr) 3203 { 3204 pnfs_put_lseg(hdr->lseg); 3205 nfs_pgio_header_free(hdr); 3206 } 3207 3208 int 3209 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc) 3210 { 3211 struct nfs_pgio_header *hdr; 3212 int ret; 3213 3214 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops); 3215 if (!hdr) { 3216 desc->pg_error = -ENOMEM; 3217 return desc->pg_error; 3218 } 3219 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free); 3220 hdr->lseg = pnfs_get_lseg(desc->pg_lseg); 3221 ret = nfs_generic_pgio(desc, hdr); 3222 if (!ret) 3223 pnfs_do_read(desc, hdr); 3224 return ret; 3225 } 3226 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages); 3227 3228 static void pnfs_clear_layoutcommitting(struct inode *inode) 3229 { 3230 unsigned long *bitlock = &NFS_I(inode)->flags; 3231 3232 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock); 3233 smp_mb__after_atomic(); 3234 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING); 3235 } 3236 3237 /* 3238 * There can be multiple RW segments. 3239 */ 3240 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp) 3241 { 3242 struct pnfs_layout_segment *lseg; 3243 3244 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) { 3245 if (lseg->pls_range.iomode == IOMODE_RW && 3246 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) 3247 list_add(&lseg->pls_lc_list, listp); 3248 } 3249 } 3250 3251 static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp) 3252 { 3253 struct pnfs_layout_segment *lseg, *tmp; 3254 3255 /* Matched by references in pnfs_set_layoutcommit */ 3256 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) { 3257 list_del_init(&lseg->pls_lc_list); 3258 pnfs_put_lseg(lseg); 3259 } 3260 3261 pnfs_clear_layoutcommitting(inode); 3262 } 3263 3264 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg) 3265 { 3266 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode); 3267 } 3268 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail); 3269 3270 void 3271 pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg, 3272 loff_t end_pos) 3273 { 3274 struct nfs_inode *nfsi = NFS_I(inode); 3275 bool mark_as_dirty = false; 3276 3277 spin_lock(&inode->i_lock); 3278 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) { 3279 nfsi->layout->plh_lwb = end_pos; 3280 mark_as_dirty = true; 3281 dprintk("%s: Set layoutcommit for inode %lu ", 3282 __func__, inode->i_ino); 3283 } else if (end_pos > nfsi->layout->plh_lwb) 3284 nfsi->layout->plh_lwb = end_pos; 3285 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) { 3286 /* references matched in nfs4_layoutcommit_release */ 3287 pnfs_get_lseg(lseg); 3288 } 3289 spin_unlock(&inode->i_lock); 3290 dprintk("%s: lseg %p end_pos %llu\n", 3291 __func__, lseg, nfsi->layout->plh_lwb); 3292 3293 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one 3294 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */ 3295 if (mark_as_dirty) 3296 mark_inode_dirty_sync(inode); 3297 } 3298 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit); 3299 3300 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data) 3301 { 3302 struct nfs_server *nfss = NFS_SERVER(data->args.inode); 3303 3304 if (nfss->pnfs_curr_ld->cleanup_layoutcommit) 3305 nfss->pnfs_curr_ld->cleanup_layoutcommit(data); 3306 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list); 3307 } 3308 3309 /* 3310 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and 3311 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough 3312 * data to disk to allow the server to recover the data if it crashes. 3313 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag 3314 * is off, and a COMMIT is sent to a data server, or 3315 * if WRITEs to a data server return NFS_DATA_SYNC. 3316 */ 3317 int 3318 pnfs_layoutcommit_inode(struct inode *inode, bool sync) 3319 { 3320 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; 3321 struct nfs4_layoutcommit_data *data; 3322 struct nfs_inode *nfsi = NFS_I(inode); 3323 loff_t end_pos; 3324 int status; 3325 3326 if (!pnfs_layoutcommit_outstanding(inode)) 3327 return 0; 3328 3329 dprintk("--> %s inode %lu\n", __func__, inode->i_ino); 3330 3331 status = -EAGAIN; 3332 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) { 3333 if (!sync) 3334 goto out; 3335 status = wait_on_bit_lock_action(&nfsi->flags, 3336 NFS_INO_LAYOUTCOMMITTING, 3337 nfs_wait_bit_killable, 3338 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE); 3339 if (status) 3340 goto out; 3341 } 3342 3343 status = -ENOMEM; 3344 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */ 3345 data = kzalloc(sizeof(*data), nfs_io_gfp_mask()); 3346 if (!data) 3347 goto clear_layoutcommitting; 3348 3349 status = 0; 3350 spin_lock(&inode->i_lock); 3351 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) 3352 goto out_unlock; 3353 3354 INIT_LIST_HEAD(&data->lseg_list); 3355 pnfs_list_write_lseg(inode, &data->lseg_list); 3356 3357 end_pos = nfsi->layout->plh_lwb; 3358 3359 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid); 3360 data->cred = get_cred(nfsi->layout->plh_lc_cred); 3361 spin_unlock(&inode->i_lock); 3362 3363 data->args.inode = inode; 3364 nfs_fattr_init(&data->fattr); 3365 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask; 3366 data->res.fattr = &data->fattr; 3367 if (end_pos != 0) 3368 data->args.lastbytewritten = end_pos - 1; 3369 else 3370 data->args.lastbytewritten = U64_MAX; 3371 data->res.server = NFS_SERVER(inode); 3372 3373 if (ld->prepare_layoutcommit) { 3374 status = ld->prepare_layoutcommit(&data->args); 3375 if (status) { 3376 put_cred(data->cred); 3377 spin_lock(&inode->i_lock); 3378 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags); 3379 if (end_pos > nfsi->layout->plh_lwb) 3380 nfsi->layout->plh_lwb = end_pos; 3381 goto out_unlock; 3382 } 3383 } 3384 3385 3386 status = nfs4_proc_layoutcommit(data, sync); 3387 out: 3388 if (status) 3389 mark_inode_dirty_sync(inode); 3390 dprintk("<-- %s status %d\n", __func__, status); 3391 return status; 3392 out_unlock: 3393 spin_unlock(&inode->i_lock); 3394 kfree(data); 3395 clear_layoutcommitting: 3396 pnfs_clear_layoutcommitting(inode); 3397 goto out; 3398 } 3399 EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode); 3400 3401 int 3402 pnfs_generic_sync(struct inode *inode, bool datasync) 3403 { 3404 return pnfs_layoutcommit_inode(inode, true); 3405 } 3406 EXPORT_SYMBOL_GPL(pnfs_generic_sync); 3407 3408 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void) 3409 { 3410 struct nfs4_threshold *thp; 3411 3412 thp = kzalloc(sizeof(*thp), nfs_io_gfp_mask()); 3413 if (!thp) { 3414 dprintk("%s mdsthreshold allocation failed\n", __func__); 3415 return NULL; 3416 } 3417 return thp; 3418 } 3419 3420 #if IS_ENABLED(CONFIG_NFS_V4_2) 3421 int 3422 pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags) 3423 { 3424 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld; 3425 struct nfs_server *server = NFS_SERVER(inode); 3426 struct nfs_inode *nfsi = NFS_I(inode); 3427 struct nfs42_layoutstat_data *data; 3428 struct pnfs_layout_hdr *hdr; 3429 int status = 0; 3430 3431 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats) 3432 goto out; 3433 3434 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS)) 3435 goto out; 3436 3437 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags)) 3438 goto out; 3439 3440 spin_lock(&inode->i_lock); 3441 if (!NFS_I(inode)->layout) { 3442 spin_unlock(&inode->i_lock); 3443 goto out_clear_layoutstats; 3444 } 3445 hdr = NFS_I(inode)->layout; 3446 pnfs_get_layout_hdr(hdr); 3447 spin_unlock(&inode->i_lock); 3448 3449 data = kzalloc(sizeof(*data), gfp_flags); 3450 if (!data) { 3451 status = -ENOMEM; 3452 goto out_put; 3453 } 3454 3455 data->args.fh = NFS_FH(inode); 3456 data->args.inode = inode; 3457 status = ld->prepare_layoutstats(&data->args); 3458 if (status) 3459 goto out_free; 3460 3461 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data); 3462 3463 out: 3464 dprintk("%s returns %d\n", __func__, status); 3465 return status; 3466 3467 out_free: 3468 kfree(data); 3469 out_put: 3470 pnfs_put_layout_hdr(hdr); 3471 out_clear_layoutstats: 3472 smp_mb__before_atomic(); 3473 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags); 3474 smp_mb__after_atomic(); 3475 goto out; 3476 } 3477 EXPORT_SYMBOL_GPL(pnfs_report_layoutstat); 3478 #endif 3479 3480 unsigned int layoutstats_timer; 3481 module_param(layoutstats_timer, uint, 0644); 3482 EXPORT_SYMBOL_GPL(layoutstats_timer); 3483