1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Common NFS I/O operations for the pnfs file based 4 * layout drivers. 5 * 6 * Copyright (c) 2014, Primary Data, Inc. All rights reserved. 7 * 8 * Tom Haynes <loghyr@primarydata.com> 9 */ 10 11 #include <linux/nfs_fs.h> 12 #include <linux/nfs_page.h> 13 #include <linux/sunrpc/addr.h> 14 #include <linux/module.h> 15 16 #include "nfs4session.h" 17 #include "internal.h" 18 #include "pnfs.h" 19 20 #define NFSDBG_FACILITY NFSDBG_PNFS 21 22 void pnfs_generic_rw_release(void *data) 23 { 24 struct nfs_pgio_header *hdr = data; 25 26 nfs_put_client(hdr->ds_clp); 27 hdr->mds_ops->rpc_release(data); 28 } 29 EXPORT_SYMBOL_GPL(pnfs_generic_rw_release); 30 31 /* Fake up some data that will cause nfs_commit_release to retry the writes. */ 32 void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data) 33 { 34 struct nfs_writeverf *verf = data->res.verf; 35 36 data->task.tk_status = 0; 37 memset(&verf->verifier, 0, sizeof(verf->verifier)); 38 verf->committed = NFS_UNSTABLE; 39 } 40 EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes); 41 42 void pnfs_generic_write_commit_done(struct rpc_task *task, void *data) 43 { 44 struct nfs_commit_data *wdata = data; 45 46 /* Note this may cause RPC to be resent */ 47 wdata->mds_ops->rpc_call_done(task, data); 48 } 49 EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done); 50 51 void pnfs_generic_commit_release(void *calldata) 52 { 53 struct nfs_commit_data *data = calldata; 54 55 data->completion_ops->completion(data); 56 pnfs_put_lseg(data->lseg); 57 nfs_put_client(data->ds_clp); 58 nfs_commitdata_release(data); 59 } 60 EXPORT_SYMBOL_GPL(pnfs_generic_commit_release); 61 62 static struct pnfs_layout_segment * 63 pnfs_free_bucket_lseg(struct pnfs_commit_bucket *bucket) 64 { 65 if (list_empty(&bucket->committing) && list_empty(&bucket->written)) { 66 struct pnfs_layout_segment *freeme = bucket->lseg; 67 bucket->lseg = NULL; 68 return freeme; 69 } 70 return NULL; 71 } 72 73 /* The generic layer is about to remove the req from the commit list. 74 * If this will make the bucket empty, it will need to put the lseg reference. 75 * Note this must be called holding nfsi->commit_mutex 76 */ 77 void 78 pnfs_generic_clear_request_commit(struct nfs_page *req, 79 struct nfs_commit_info *cinfo) 80 { 81 struct pnfs_layout_segment *freeme = NULL; 82 83 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags)) 84 goto out; 85 cinfo->ds->nwritten--; 86 if (list_is_singular(&req->wb_list)) { 87 struct pnfs_commit_bucket *bucket; 88 89 bucket = list_first_entry(&req->wb_list, 90 struct pnfs_commit_bucket, 91 written); 92 freeme = pnfs_free_bucket_lseg(bucket); 93 } 94 out: 95 nfs_request_remove_commit_list(req, cinfo); 96 pnfs_put_lseg(freeme); 97 } 98 EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit); 99 100 struct pnfs_commit_array * 101 pnfs_alloc_commit_array(size_t n, gfp_t gfp_flags) 102 { 103 struct pnfs_commit_array *p; 104 struct pnfs_commit_bucket *b; 105 106 p = kmalloc(struct_size(p, buckets, n), gfp_flags); 107 if (!p) 108 return NULL; 109 p->nbuckets = n; 110 INIT_LIST_HEAD(&p->cinfo_list); 111 INIT_LIST_HEAD(&p->lseg_list); 112 p->lseg = NULL; 113 for (b = &p->buckets[0]; n != 0; b++, n--) { 114 INIT_LIST_HEAD(&b->written); 115 INIT_LIST_HEAD(&b->committing); 116 b->lseg = NULL; 117 b->direct_verf.committed = NFS_INVALID_STABLE_HOW; 118 } 119 return p; 120 } 121 EXPORT_SYMBOL_GPL(pnfs_alloc_commit_array); 122 123 void 124 pnfs_free_commit_array(struct pnfs_commit_array *p) 125 { 126 kfree_rcu(p, rcu); 127 } 128 EXPORT_SYMBOL_GPL(pnfs_free_commit_array); 129 130 static struct pnfs_commit_array * 131 pnfs_find_commit_array_by_lseg(struct pnfs_ds_commit_info *fl_cinfo, 132 struct pnfs_layout_segment *lseg) 133 { 134 struct pnfs_commit_array *array; 135 136 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) { 137 if (array->lseg == lseg) 138 return array; 139 } 140 return NULL; 141 } 142 143 struct pnfs_commit_array * 144 pnfs_add_commit_array(struct pnfs_ds_commit_info *fl_cinfo, 145 struct pnfs_commit_array *new, 146 struct pnfs_layout_segment *lseg) 147 { 148 struct pnfs_commit_array *array; 149 150 array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg); 151 if (array) 152 return array; 153 new->lseg = lseg; 154 refcount_set(&new->refcount, 1); 155 list_add_rcu(&new->cinfo_list, &fl_cinfo->commits); 156 list_add(&new->lseg_list, &lseg->pls_commits); 157 return new; 158 } 159 EXPORT_SYMBOL_GPL(pnfs_add_commit_array); 160 161 static struct pnfs_commit_array * 162 pnfs_lookup_commit_array(struct pnfs_ds_commit_info *fl_cinfo, 163 struct pnfs_layout_segment *lseg) 164 { 165 struct pnfs_commit_array *array; 166 167 rcu_read_lock(); 168 array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg); 169 if (!array) { 170 rcu_read_unlock(); 171 fl_cinfo->ops->setup_ds_info(fl_cinfo, lseg); 172 rcu_read_lock(); 173 array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg); 174 } 175 rcu_read_unlock(); 176 return array; 177 } 178 179 static void 180 pnfs_release_commit_array_locked(struct pnfs_commit_array *array) 181 { 182 list_del_rcu(&array->cinfo_list); 183 list_del(&array->lseg_list); 184 pnfs_free_commit_array(array); 185 } 186 187 static void 188 pnfs_put_commit_array_locked(struct pnfs_commit_array *array) 189 { 190 if (refcount_dec_and_test(&array->refcount)) 191 pnfs_release_commit_array_locked(array); 192 } 193 194 static void 195 pnfs_put_commit_array(struct pnfs_commit_array *array, struct inode *inode) 196 { 197 if (refcount_dec_and_lock(&array->refcount, &inode->i_lock)) { 198 pnfs_release_commit_array_locked(array); 199 spin_unlock(&inode->i_lock); 200 } 201 } 202 203 static struct pnfs_commit_array * 204 pnfs_get_commit_array(struct pnfs_commit_array *array) 205 { 206 if (refcount_inc_not_zero(&array->refcount)) 207 return array; 208 return NULL; 209 } 210 211 static void 212 pnfs_remove_and_free_commit_array(struct pnfs_commit_array *array) 213 { 214 array->lseg = NULL; 215 list_del_init(&array->lseg_list); 216 pnfs_put_commit_array_locked(array); 217 } 218 219 void 220 pnfs_generic_ds_cinfo_release_lseg(struct pnfs_ds_commit_info *fl_cinfo, 221 struct pnfs_layout_segment *lseg) 222 { 223 struct pnfs_commit_array *array, *tmp; 224 225 list_for_each_entry_safe(array, tmp, &lseg->pls_commits, lseg_list) 226 pnfs_remove_and_free_commit_array(array); 227 } 228 EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_release_lseg); 229 230 void 231 pnfs_generic_ds_cinfo_destroy(struct pnfs_ds_commit_info *fl_cinfo) 232 { 233 struct pnfs_commit_array *array, *tmp; 234 235 list_for_each_entry_safe(array, tmp, &fl_cinfo->commits, cinfo_list) 236 pnfs_remove_and_free_commit_array(array); 237 } 238 EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_destroy); 239 240 /* 241 * Locks the nfs_page requests for commit and moves them to 242 * @bucket->committing. 243 */ 244 static int 245 pnfs_bucket_scan_ds_commit_list(struct pnfs_commit_bucket *bucket, 246 struct nfs_commit_info *cinfo, 247 int max) 248 { 249 struct list_head *src = &bucket->written; 250 struct list_head *dst = &bucket->committing; 251 int ret; 252 253 lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex); 254 ret = nfs_scan_commit_list(src, dst, cinfo, max); 255 if (ret) { 256 cinfo->ds->nwritten -= ret; 257 cinfo->ds->ncommitting += ret; 258 } 259 return ret; 260 } 261 262 static int pnfs_bucket_scan_array(struct nfs_commit_info *cinfo, 263 struct pnfs_commit_bucket *buckets, 264 unsigned int nbuckets, 265 int max) 266 { 267 unsigned int i; 268 int rv = 0, cnt; 269 270 for (i = 0; i < nbuckets && max != 0; i++) { 271 cnt = pnfs_bucket_scan_ds_commit_list(&buckets[i], cinfo, max); 272 rv += cnt; 273 max -= cnt; 274 } 275 return rv; 276 } 277 278 /* Move reqs from written to committing lists, returning count 279 * of number moved. 280 */ 281 int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo, int max) 282 { 283 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds; 284 struct pnfs_commit_array *array; 285 int rv = 0, cnt; 286 287 rcu_read_lock(); 288 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) { 289 if (!array->lseg || !pnfs_get_commit_array(array)) 290 continue; 291 rcu_read_unlock(); 292 cnt = pnfs_bucket_scan_array(cinfo, array->buckets, 293 array->nbuckets, max); 294 rcu_read_lock(); 295 pnfs_put_commit_array(array, cinfo->inode); 296 rv += cnt; 297 max -= cnt; 298 if (!max) 299 break; 300 } 301 rcu_read_unlock(); 302 return rv; 303 } 304 EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists); 305 306 static unsigned int 307 pnfs_bucket_recover_commit_reqs(struct list_head *dst, 308 struct pnfs_commit_bucket *buckets, 309 unsigned int nbuckets, 310 struct nfs_commit_info *cinfo) 311 { 312 struct pnfs_commit_bucket *b; 313 struct pnfs_layout_segment *freeme; 314 unsigned int nwritten, ret = 0; 315 unsigned int i; 316 317 restart: 318 for (i = 0, b = buckets; i < nbuckets; i++, b++) { 319 nwritten = nfs_scan_commit_list(&b->written, dst, cinfo, 0); 320 if (!nwritten) 321 continue; 322 ret += nwritten; 323 freeme = pnfs_free_bucket_lseg(b); 324 if (freeme) { 325 pnfs_put_lseg(freeme); 326 goto restart; 327 } 328 } 329 return ret; 330 } 331 332 /* Pull everything off the committing lists and dump into @dst. */ 333 void pnfs_generic_recover_commit_reqs(struct list_head *dst, 334 struct nfs_commit_info *cinfo) 335 { 336 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds; 337 struct pnfs_commit_array *array; 338 unsigned int nwritten; 339 340 lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex); 341 rcu_read_lock(); 342 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) { 343 if (!array->lseg || !pnfs_get_commit_array(array)) 344 continue; 345 rcu_read_unlock(); 346 nwritten = pnfs_bucket_recover_commit_reqs(dst, 347 array->buckets, 348 array->nbuckets, 349 cinfo); 350 rcu_read_lock(); 351 pnfs_put_commit_array(array, cinfo->inode); 352 fl_cinfo->nwritten -= nwritten; 353 } 354 rcu_read_unlock(); 355 } 356 EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs); 357 358 static struct nfs_page * 359 pnfs_bucket_search_commit_reqs(struct pnfs_commit_bucket *buckets, 360 unsigned int nbuckets, struct page *page) 361 { 362 struct nfs_page *req; 363 struct pnfs_commit_bucket *b; 364 unsigned int i; 365 366 /* Linearly search the commit lists for each bucket until a matching 367 * request is found */ 368 for (i = 0, b = buckets; i < nbuckets; i++, b++) { 369 list_for_each_entry(req, &b->written, wb_list) { 370 if (req->wb_page == page) 371 return req->wb_head; 372 } 373 list_for_each_entry(req, &b->committing, wb_list) { 374 if (req->wb_page == page) 375 return req->wb_head; 376 } 377 } 378 return NULL; 379 } 380 381 /* pnfs_generic_search_commit_reqs - Search lists in @cinfo for the head reqest 382 * for @page 383 * @cinfo - commit info for current inode 384 * @page - page to search for matching head request 385 * 386 * Returns a the head request if one is found, otherwise returns NULL. 387 */ 388 struct nfs_page * 389 pnfs_generic_search_commit_reqs(struct nfs_commit_info *cinfo, struct page *page) 390 { 391 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds; 392 struct pnfs_commit_array *array; 393 struct nfs_page *req; 394 395 list_for_each_entry(array, &fl_cinfo->commits, cinfo_list) { 396 req = pnfs_bucket_search_commit_reqs(array->buckets, 397 array->nbuckets, page); 398 if (req) 399 return req; 400 } 401 return NULL; 402 } 403 EXPORT_SYMBOL_GPL(pnfs_generic_search_commit_reqs); 404 405 static struct pnfs_layout_segment * 406 pnfs_bucket_get_committing(struct list_head *head, 407 struct pnfs_commit_bucket *bucket, 408 struct nfs_commit_info *cinfo) 409 { 410 struct list_head *pos; 411 412 list_for_each(pos, &bucket->committing) 413 cinfo->ds->ncommitting--; 414 list_splice_init(&bucket->committing, head); 415 return pnfs_free_bucket_lseg(bucket); 416 } 417 418 static struct nfs_commit_data * 419 pnfs_bucket_fetch_commitdata(struct pnfs_commit_bucket *bucket, 420 struct nfs_commit_info *cinfo) 421 { 422 struct nfs_commit_data *data = nfs_commitdata_alloc(false); 423 424 if (!data) 425 return NULL; 426 data->lseg = pnfs_bucket_get_committing(&data->pages, bucket, cinfo); 427 if (!data->lseg) 428 data->lseg = pnfs_get_lseg(bucket->lseg); 429 return data; 430 } 431 432 static void pnfs_generic_retry_commit(struct pnfs_commit_bucket *buckets, 433 unsigned int nbuckets, 434 struct nfs_commit_info *cinfo, 435 unsigned int idx) 436 { 437 struct pnfs_commit_bucket *bucket; 438 struct pnfs_layout_segment *freeme; 439 LIST_HEAD(pages); 440 441 for (bucket = buckets; idx < nbuckets; bucket++, idx++) { 442 if (list_empty(&bucket->committing)) 443 continue; 444 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 445 freeme = pnfs_bucket_get_committing(&pages, bucket, cinfo); 446 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 447 nfs_retry_commit(&pages, freeme, cinfo, idx); 448 pnfs_put_lseg(freeme); 449 } 450 } 451 452 static unsigned int 453 pnfs_bucket_alloc_ds_commits(struct list_head *list, 454 struct pnfs_commit_bucket *buckets, 455 unsigned int nbuckets, 456 struct nfs_commit_info *cinfo) 457 { 458 struct pnfs_commit_bucket *bucket; 459 struct nfs_commit_data *data; 460 unsigned int i; 461 unsigned int nreq = 0; 462 463 for (i = 0, bucket = buckets; i < nbuckets; i++, bucket++) { 464 if (list_empty(&bucket->committing)) 465 continue; 466 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 467 if (!list_empty(&bucket->committing)) { 468 data = pnfs_bucket_fetch_commitdata(bucket, cinfo); 469 if (!data) 470 goto out_error; 471 data->ds_commit_index = i; 472 list_add_tail(&data->list, list); 473 atomic_inc(&cinfo->mds->rpcs_out); 474 nreq++; 475 } 476 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 477 } 478 return nreq; 479 out_error: 480 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 481 /* Clean up on error */ 482 pnfs_generic_retry_commit(buckets, nbuckets, cinfo, i); 483 return nreq; 484 } 485 486 static unsigned int 487 pnfs_alloc_ds_commits_list(struct list_head *list, 488 struct pnfs_ds_commit_info *fl_cinfo, 489 struct nfs_commit_info *cinfo) 490 { 491 struct pnfs_commit_array *array; 492 unsigned int ret = 0; 493 494 rcu_read_lock(); 495 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) { 496 if (!array->lseg || !pnfs_get_commit_array(array)) 497 continue; 498 rcu_read_unlock(); 499 ret += pnfs_bucket_alloc_ds_commits(list, array->buckets, 500 array->nbuckets, cinfo); 501 rcu_read_lock(); 502 pnfs_put_commit_array(array, cinfo->inode); 503 } 504 rcu_read_unlock(); 505 return ret; 506 } 507 508 /* This follows nfs_commit_list pretty closely */ 509 int 510 pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages, 511 int how, struct nfs_commit_info *cinfo, 512 int (*initiate_commit)(struct nfs_commit_data *data, 513 int how)) 514 { 515 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds; 516 struct nfs_commit_data *data, *tmp; 517 LIST_HEAD(list); 518 unsigned int nreq = 0; 519 520 if (!list_empty(mds_pages)) { 521 data = nfs_commitdata_alloc(true); 522 data->ds_commit_index = -1; 523 list_splice_init(mds_pages, &data->pages); 524 list_add_tail(&data->list, &list); 525 atomic_inc(&cinfo->mds->rpcs_out); 526 nreq++; 527 } 528 529 nreq += pnfs_alloc_ds_commits_list(&list, fl_cinfo, cinfo); 530 if (nreq == 0) 531 goto out; 532 533 list_for_each_entry_safe(data, tmp, &list, list) { 534 list_del(&data->list); 535 if (data->ds_commit_index < 0) { 536 nfs_init_commit(data, NULL, NULL, cinfo); 537 nfs_initiate_commit(NFS_CLIENT(inode), data, 538 NFS_PROTO(data->inode), 539 data->mds_ops, how, 0); 540 } else { 541 nfs_init_commit(data, NULL, data->lseg, cinfo); 542 initiate_commit(data, how); 543 } 544 } 545 out: 546 return PNFS_ATTEMPTED; 547 } 548 EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist); 549 550 /* 551 * Data server cache 552 * 553 * Data servers can be mapped to different device ids. 554 * nfs4_pnfs_ds reference counting 555 * - set to 1 on allocation 556 * - incremented when a device id maps a data server already in the cache. 557 * - decremented when deviceid is removed from the cache. 558 */ 559 static DEFINE_SPINLOCK(nfs4_ds_cache_lock); 560 static LIST_HEAD(nfs4_data_server_cache); 561 562 /* Debug routines */ 563 static void 564 print_ds(struct nfs4_pnfs_ds *ds) 565 { 566 if (ds == NULL) { 567 printk(KERN_WARNING "%s NULL device\n", __func__); 568 return; 569 } 570 printk(KERN_WARNING " ds %s\n" 571 " ref count %d\n" 572 " client %p\n" 573 " cl_exchange_flags %x\n", 574 ds->ds_remotestr, 575 refcount_read(&ds->ds_count), ds->ds_clp, 576 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0); 577 } 578 579 static bool 580 same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2) 581 { 582 struct sockaddr_in *a, *b; 583 struct sockaddr_in6 *a6, *b6; 584 585 if (addr1->sa_family != addr2->sa_family) 586 return false; 587 588 switch (addr1->sa_family) { 589 case AF_INET: 590 a = (struct sockaddr_in *)addr1; 591 b = (struct sockaddr_in *)addr2; 592 593 if (a->sin_addr.s_addr == b->sin_addr.s_addr && 594 a->sin_port == b->sin_port) 595 return true; 596 break; 597 598 case AF_INET6: 599 a6 = (struct sockaddr_in6 *)addr1; 600 b6 = (struct sockaddr_in6 *)addr2; 601 602 /* LINKLOCAL addresses must have matching scope_id */ 603 if (ipv6_addr_src_scope(&a6->sin6_addr) == 604 IPV6_ADDR_SCOPE_LINKLOCAL && 605 a6->sin6_scope_id != b6->sin6_scope_id) 606 return false; 607 608 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) && 609 a6->sin6_port == b6->sin6_port) 610 return true; 611 break; 612 613 default: 614 dprintk("%s: unhandled address family: %u\n", 615 __func__, addr1->sa_family); 616 return false; 617 } 618 619 return false; 620 } 621 622 /* 623 * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does, 624 * declare a match. 625 */ 626 static bool 627 _same_data_server_addrs_locked(const struct list_head *dsaddrs1, 628 const struct list_head *dsaddrs2) 629 { 630 struct nfs4_pnfs_ds_addr *da1, *da2; 631 struct sockaddr *sa1, *sa2; 632 bool match = false; 633 634 list_for_each_entry(da1, dsaddrs1, da_node) { 635 sa1 = (struct sockaddr *)&da1->da_addr; 636 match = false; 637 list_for_each_entry(da2, dsaddrs2, da_node) { 638 sa2 = (struct sockaddr *)&da2->da_addr; 639 match = same_sockaddr(sa1, sa2); 640 if (match) 641 break; 642 } 643 if (!match) 644 break; 645 } 646 return match; 647 } 648 649 /* 650 * Lookup DS by addresses. nfs4_ds_cache_lock is held 651 */ 652 static struct nfs4_pnfs_ds * 653 _data_server_lookup_locked(const struct list_head *dsaddrs) 654 { 655 struct nfs4_pnfs_ds *ds; 656 657 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) 658 if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs)) 659 return ds; 660 return NULL; 661 } 662 663 static void destroy_ds(struct nfs4_pnfs_ds *ds) 664 { 665 struct nfs4_pnfs_ds_addr *da; 666 667 dprintk("--> %s\n", __func__); 668 ifdebug(FACILITY) 669 print_ds(ds); 670 671 nfs_put_client(ds->ds_clp); 672 673 while (!list_empty(&ds->ds_addrs)) { 674 da = list_first_entry(&ds->ds_addrs, 675 struct nfs4_pnfs_ds_addr, 676 da_node); 677 list_del_init(&da->da_node); 678 kfree(da->da_remotestr); 679 kfree(da); 680 } 681 682 kfree(ds->ds_remotestr); 683 kfree(ds); 684 } 685 686 void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds) 687 { 688 if (refcount_dec_and_lock(&ds->ds_count, 689 &nfs4_ds_cache_lock)) { 690 list_del_init(&ds->ds_node); 691 spin_unlock(&nfs4_ds_cache_lock); 692 destroy_ds(ds); 693 } 694 } 695 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put); 696 697 /* 698 * Create a string with a human readable address and port to avoid 699 * complicated setup around many dprinks. 700 */ 701 static char * 702 nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags) 703 { 704 struct nfs4_pnfs_ds_addr *da; 705 char *remotestr; 706 size_t len; 707 char *p; 708 709 len = 3; /* '{', '}' and eol */ 710 list_for_each_entry(da, dsaddrs, da_node) { 711 len += strlen(da->da_remotestr) + 1; /* string plus comma */ 712 } 713 714 remotestr = kzalloc(len, gfp_flags); 715 if (!remotestr) 716 return NULL; 717 718 p = remotestr; 719 *(p++) = '{'; 720 len--; 721 list_for_each_entry(da, dsaddrs, da_node) { 722 size_t ll = strlen(da->da_remotestr); 723 724 if (ll > len) 725 goto out_err; 726 727 memcpy(p, da->da_remotestr, ll); 728 p += ll; 729 len -= ll; 730 731 if (len < 1) 732 goto out_err; 733 (*p++) = ','; 734 len--; 735 } 736 if (len < 2) 737 goto out_err; 738 *(p++) = '}'; 739 *p = '\0'; 740 return remotestr; 741 out_err: 742 kfree(remotestr); 743 return NULL; 744 } 745 746 /* 747 * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if 748 * uncached and return cached struct nfs4_pnfs_ds. 749 */ 750 struct nfs4_pnfs_ds * 751 nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags) 752 { 753 struct nfs4_pnfs_ds *tmp_ds, *ds = NULL; 754 char *remotestr; 755 756 if (list_empty(dsaddrs)) { 757 dprintk("%s: no addresses defined\n", __func__); 758 goto out; 759 } 760 761 ds = kzalloc(sizeof(*ds), gfp_flags); 762 if (!ds) 763 goto out; 764 765 /* this is only used for debugging, so it's ok if its NULL */ 766 remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags); 767 768 spin_lock(&nfs4_ds_cache_lock); 769 tmp_ds = _data_server_lookup_locked(dsaddrs); 770 if (tmp_ds == NULL) { 771 INIT_LIST_HEAD(&ds->ds_addrs); 772 list_splice_init(dsaddrs, &ds->ds_addrs); 773 ds->ds_remotestr = remotestr; 774 refcount_set(&ds->ds_count, 1); 775 INIT_LIST_HEAD(&ds->ds_node); 776 ds->ds_clp = NULL; 777 list_add(&ds->ds_node, &nfs4_data_server_cache); 778 dprintk("%s add new data server %s\n", __func__, 779 ds->ds_remotestr); 780 } else { 781 kfree(remotestr); 782 kfree(ds); 783 refcount_inc(&tmp_ds->ds_count); 784 dprintk("%s data server %s found, inc'ed ds_count to %d\n", 785 __func__, tmp_ds->ds_remotestr, 786 refcount_read(&tmp_ds->ds_count)); 787 ds = tmp_ds; 788 } 789 spin_unlock(&nfs4_ds_cache_lock); 790 out: 791 return ds; 792 } 793 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add); 794 795 static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds) 796 { 797 might_sleep(); 798 wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING, 799 TASK_KILLABLE); 800 } 801 802 static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds) 803 { 804 smp_mb__before_atomic(); 805 clear_bit(NFS4DS_CONNECTING, &ds->ds_state); 806 smp_mb__after_atomic(); 807 wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING); 808 } 809 810 static struct nfs_client *(*get_v3_ds_connect)( 811 struct nfs_server *mds_srv, 812 const struct sockaddr *ds_addr, 813 int ds_addrlen, 814 int ds_proto, 815 unsigned int ds_timeo, 816 unsigned int ds_retrans); 817 818 static bool load_v3_ds_connect(void) 819 { 820 if (!get_v3_ds_connect) { 821 get_v3_ds_connect = symbol_request(nfs3_set_ds_client); 822 WARN_ON_ONCE(!get_v3_ds_connect); 823 } 824 825 return(get_v3_ds_connect != NULL); 826 } 827 828 void nfs4_pnfs_v3_ds_connect_unload(void) 829 { 830 if (get_v3_ds_connect) { 831 symbol_put(nfs3_set_ds_client); 832 get_v3_ds_connect = NULL; 833 } 834 } 835 836 static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv, 837 struct nfs4_pnfs_ds *ds, 838 unsigned int timeo, 839 unsigned int retrans) 840 { 841 struct nfs_client *clp = ERR_PTR(-EIO); 842 struct nfs4_pnfs_ds_addr *da; 843 int status = 0; 844 845 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr); 846 847 if (!load_v3_ds_connect()) 848 goto out; 849 850 list_for_each_entry(da, &ds->ds_addrs, da_node) { 851 dprintk("%s: DS %s: trying address %s\n", 852 __func__, ds->ds_remotestr, da->da_remotestr); 853 854 if (!IS_ERR(clp)) { 855 struct xprt_create xprt_args = { 856 .ident = XPRT_TRANSPORT_TCP, 857 .net = clp->cl_net, 858 .dstaddr = (struct sockaddr *)&da->da_addr, 859 .addrlen = da->da_addrlen, 860 .servername = clp->cl_hostname, 861 }; 862 /* Add this address as an alias */ 863 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args, 864 rpc_clnt_test_and_add_xprt, NULL); 865 continue; 866 } 867 clp = get_v3_ds_connect(mds_srv, 868 (struct sockaddr *)&da->da_addr, 869 da->da_addrlen, IPPROTO_TCP, 870 timeo, retrans); 871 if (IS_ERR(clp)) 872 continue; 873 clp->cl_rpcclient->cl_softerr = 0; 874 clp->cl_rpcclient->cl_softrtry = 0; 875 } 876 877 if (IS_ERR(clp)) { 878 status = PTR_ERR(clp); 879 goto out; 880 } 881 882 smp_wmb(); 883 ds->ds_clp = clp; 884 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); 885 out: 886 return status; 887 } 888 889 static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv, 890 struct nfs4_pnfs_ds *ds, 891 unsigned int timeo, 892 unsigned int retrans, 893 u32 minor_version) 894 { 895 struct nfs_client *clp = ERR_PTR(-EIO); 896 struct nfs4_pnfs_ds_addr *da; 897 int status = 0; 898 899 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr); 900 901 list_for_each_entry(da, &ds->ds_addrs, da_node) { 902 dprintk("%s: DS %s: trying address %s\n", 903 __func__, ds->ds_remotestr, da->da_remotestr); 904 905 if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) { 906 struct xprt_create xprt_args = { 907 .ident = XPRT_TRANSPORT_TCP, 908 .net = clp->cl_net, 909 .dstaddr = (struct sockaddr *)&da->da_addr, 910 .addrlen = da->da_addrlen, 911 .servername = clp->cl_hostname, 912 }; 913 struct nfs4_add_xprt_data xprtdata = { 914 .clp = clp, 915 .cred = nfs4_get_clid_cred(clp), 916 }; 917 struct rpc_add_xprt_test rpcdata = { 918 .add_xprt_test = clp->cl_mvops->session_trunk, 919 .data = &xprtdata, 920 }; 921 922 /** 923 * Test this address for session trunking and 924 * add as an alias 925 */ 926 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args, 927 rpc_clnt_setup_test_and_add_xprt, 928 &rpcdata); 929 if (xprtdata.cred) 930 put_cred(xprtdata.cred); 931 } else { 932 clp = nfs4_set_ds_client(mds_srv, 933 (struct sockaddr *)&da->da_addr, 934 da->da_addrlen, IPPROTO_TCP, 935 timeo, retrans, minor_version); 936 if (IS_ERR(clp)) 937 continue; 938 939 status = nfs4_init_ds_session(clp, 940 mds_srv->nfs_client->cl_lease_time); 941 if (status) { 942 nfs_put_client(clp); 943 clp = ERR_PTR(-EIO); 944 continue; 945 } 946 947 } 948 } 949 950 if (IS_ERR(clp)) { 951 status = PTR_ERR(clp); 952 goto out; 953 } 954 955 smp_wmb(); 956 ds->ds_clp = clp; 957 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); 958 out: 959 return status; 960 } 961 962 /* 963 * Create an rpc connection to the nfs4_pnfs_ds data server. 964 * Currently only supports IPv4 and IPv6 addresses. 965 * If connection fails, make devid unavailable and return a -errno. 966 */ 967 int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds, 968 struct nfs4_deviceid_node *devid, unsigned int timeo, 969 unsigned int retrans, u32 version, u32 minor_version) 970 { 971 int err; 972 973 again: 974 err = 0; 975 if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) { 976 if (version == 3) { 977 err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo, 978 retrans); 979 } else if (version == 4) { 980 err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo, 981 retrans, minor_version); 982 } else { 983 dprintk("%s: unsupported DS version %d\n", __func__, 984 version); 985 err = -EPROTONOSUPPORT; 986 } 987 988 nfs4_clear_ds_conn_bit(ds); 989 } else { 990 nfs4_wait_ds_connect(ds); 991 992 /* what was waited on didn't connect AND didn't mark unavail */ 993 if (!ds->ds_clp && !nfs4_test_deviceid_unavailable(devid)) 994 goto again; 995 } 996 997 /* 998 * At this point the ds->ds_clp should be ready, but it might have 999 * hit an error. 1000 */ 1001 if (!err) { 1002 if (!ds->ds_clp || !nfs_client_init_is_complete(ds->ds_clp)) { 1003 WARN_ON_ONCE(ds->ds_clp || 1004 !nfs4_test_deviceid_unavailable(devid)); 1005 return -EINVAL; 1006 } 1007 err = nfs_client_init_status(ds->ds_clp); 1008 } 1009 1010 return err; 1011 } 1012 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect); 1013 1014 /* 1015 * Currently only supports ipv4, ipv6 and one multi-path address. 1016 */ 1017 struct nfs4_pnfs_ds_addr * 1018 nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags) 1019 { 1020 struct nfs4_pnfs_ds_addr *da = NULL; 1021 char *buf, *portstr; 1022 __be16 port; 1023 int nlen, rlen; 1024 int tmp[2]; 1025 __be32 *p; 1026 char *netid, *match_netid; 1027 size_t len, match_netid_len; 1028 char *startsep = ""; 1029 char *endsep = ""; 1030 1031 1032 /* r_netid */ 1033 p = xdr_inline_decode(xdr, 4); 1034 if (unlikely(!p)) 1035 goto out_err; 1036 nlen = be32_to_cpup(p++); 1037 1038 p = xdr_inline_decode(xdr, nlen); 1039 if (unlikely(!p)) 1040 goto out_err; 1041 1042 netid = kmalloc(nlen+1, gfp_flags); 1043 if (unlikely(!netid)) 1044 goto out_err; 1045 1046 netid[nlen] = '\0'; 1047 memcpy(netid, p, nlen); 1048 1049 /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */ 1050 p = xdr_inline_decode(xdr, 4); 1051 if (unlikely(!p)) 1052 goto out_free_netid; 1053 rlen = be32_to_cpup(p); 1054 1055 p = xdr_inline_decode(xdr, rlen); 1056 if (unlikely(!p)) 1057 goto out_free_netid; 1058 1059 /* port is ".ABC.DEF", 8 chars max */ 1060 if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) { 1061 dprintk("%s: Invalid address, length %d\n", __func__, 1062 rlen); 1063 goto out_free_netid; 1064 } 1065 buf = kmalloc(rlen + 1, gfp_flags); 1066 if (!buf) { 1067 dprintk("%s: Not enough memory\n", __func__); 1068 goto out_free_netid; 1069 } 1070 buf[rlen] = '\0'; 1071 memcpy(buf, p, rlen); 1072 1073 /* replace port '.' with '-' */ 1074 portstr = strrchr(buf, '.'); 1075 if (!portstr) { 1076 dprintk("%s: Failed finding expected dot in port\n", 1077 __func__); 1078 goto out_free_buf; 1079 } 1080 *portstr = '-'; 1081 1082 /* find '.' between address and port */ 1083 portstr = strrchr(buf, '.'); 1084 if (!portstr) { 1085 dprintk("%s: Failed finding expected dot between address and " 1086 "port\n", __func__); 1087 goto out_free_buf; 1088 } 1089 *portstr = '\0'; 1090 1091 da = kzalloc(sizeof(*da), gfp_flags); 1092 if (unlikely(!da)) 1093 goto out_free_buf; 1094 1095 INIT_LIST_HEAD(&da->da_node); 1096 1097 if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr, 1098 sizeof(da->da_addr))) { 1099 dprintk("%s: error parsing address %s\n", __func__, buf); 1100 goto out_free_da; 1101 } 1102 1103 portstr++; 1104 sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]); 1105 port = htons((tmp[0] << 8) | (tmp[1])); 1106 1107 switch (da->da_addr.ss_family) { 1108 case AF_INET: 1109 ((struct sockaddr_in *)&da->da_addr)->sin_port = port; 1110 da->da_addrlen = sizeof(struct sockaddr_in); 1111 match_netid = "tcp"; 1112 match_netid_len = 3; 1113 break; 1114 1115 case AF_INET6: 1116 ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port; 1117 da->da_addrlen = sizeof(struct sockaddr_in6); 1118 match_netid = "tcp6"; 1119 match_netid_len = 4; 1120 startsep = "["; 1121 endsep = "]"; 1122 break; 1123 1124 default: 1125 dprintk("%s: unsupported address family: %u\n", 1126 __func__, da->da_addr.ss_family); 1127 goto out_free_da; 1128 } 1129 1130 if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) { 1131 dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n", 1132 __func__, netid, match_netid); 1133 goto out_free_da; 1134 } 1135 1136 /* save human readable address */ 1137 len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7; 1138 da->da_remotestr = kzalloc(len, gfp_flags); 1139 1140 /* NULL is ok, only used for dprintk */ 1141 if (da->da_remotestr) 1142 snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep, 1143 buf, endsep, ntohs(port)); 1144 1145 dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr); 1146 kfree(buf); 1147 kfree(netid); 1148 return da; 1149 1150 out_free_da: 1151 kfree(da); 1152 out_free_buf: 1153 dprintk("%s: Error parsing DS addr: %s\n", __func__, buf); 1154 kfree(buf); 1155 out_free_netid: 1156 kfree(netid); 1157 out_err: 1158 return NULL; 1159 } 1160 EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr); 1161 1162 void 1163 pnfs_layout_mark_request_commit(struct nfs_page *req, 1164 struct pnfs_layout_segment *lseg, 1165 struct nfs_commit_info *cinfo, 1166 u32 ds_commit_idx) 1167 { 1168 struct list_head *list; 1169 struct pnfs_commit_array *array; 1170 struct pnfs_commit_bucket *bucket; 1171 1172 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 1173 array = pnfs_lookup_commit_array(cinfo->ds, lseg); 1174 if (!array || !pnfs_is_valid_lseg(lseg)) 1175 goto out_resched; 1176 bucket = &array->buckets[ds_commit_idx]; 1177 list = &bucket->written; 1178 /* Non-empty buckets hold a reference on the lseg. That ref 1179 * is normally transferred to the COMMIT call and released 1180 * there. It could also be released if the last req is pulled 1181 * off due to a rewrite, in which case it will be done in 1182 * pnfs_common_clear_request_commit 1183 */ 1184 if (!bucket->lseg) 1185 bucket->lseg = pnfs_get_lseg(lseg); 1186 set_bit(PG_COMMIT_TO_DS, &req->wb_flags); 1187 cinfo->ds->nwritten++; 1188 1189 nfs_request_add_commit_list_locked(req, list, cinfo); 1190 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 1191 nfs_mark_page_unstable(req->wb_page, cinfo); 1192 return; 1193 out_resched: 1194 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 1195 cinfo->completion_ops->resched_write(cinfo, req); 1196 } 1197 EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit); 1198 1199 int 1200 pnfs_nfs_generic_sync(struct inode *inode, bool datasync) 1201 { 1202 int ret; 1203 1204 if (!pnfs_layoutcommit_outstanding(inode)) 1205 return 0; 1206 ret = nfs_commit_inode(inode, FLUSH_SYNC); 1207 if (ret < 0) 1208 return ret; 1209 if (datasync) 1210 return 0; 1211 return pnfs_layoutcommit_inode(inode, true); 1212 } 1213 EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync); 1214 1215