1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * NFS client support for local clients to bypass network stack 4 * 5 * Copyright (C) 2014 Weston Andros Adamson <dros@primarydata.com> 6 * Copyright (C) 2019 Trond Myklebust <trond.myklebust@hammerspace.com> 7 * Copyright (C) 2024 Mike Snitzer <snitzer@hammerspace.com> 8 * Copyright (C) 2024 NeilBrown <neilb@suse.de> 9 */ 10 11 #include <linux/module.h> 12 #include <linux/errno.h> 13 #include <linux/vfs.h> 14 #include <linux/file.h> 15 #include <linux/inet.h> 16 #include <linux/sunrpc/addr.h> 17 #include <linux/inetdevice.h> 18 #include <net/addrconf.h> 19 #include <linux/nfs_common.h> 20 #include <linux/nfslocalio.h> 21 #include <linux/bvec.h> 22 23 #include <linux/nfs.h> 24 #include <linux/nfs_fs.h> 25 #include <linux/nfs_xdr.h> 26 27 #include "internal.h" 28 #include "pnfs.h" 29 #include "nfstrace.h" 30 31 #define NFSDBG_FACILITY NFSDBG_VFS 32 33 #define NFSLOCAL_MAX_IOS 3 34 35 struct nfs_local_kiocb { 36 struct kiocb kiocb; 37 struct bio_vec *bvec; 38 struct nfs_pgio_header *hdr; 39 struct work_struct work; 40 void (*aio_complete_work)(struct work_struct *); 41 struct nfsd_file *localio; 42 /* Begin mostly DIO-specific members */ 43 size_t end_len; 44 short int end_iter_index; 45 atomic_t n_iters; 46 struct iov_iter iters[NFSLOCAL_MAX_IOS]; 47 bool iter_is_dio_aligned[NFSLOCAL_MAX_IOS]; 48 /* End mostly DIO-specific members */ 49 }; 50 51 struct nfs_local_fsync_ctx { 52 struct nfsd_file *localio; 53 struct nfs_commit_data *data; 54 struct work_struct work; 55 struct completion *done; 56 }; 57 58 static bool localio_enabled __read_mostly = true; 59 module_param(localio_enabled, bool, 0644); 60 61 static inline bool nfs_client_is_local(const struct nfs_client *clp) 62 { 63 return !!rcu_access_pointer(clp->cl_uuid.net); 64 } 65 66 bool nfs_server_is_local(const struct nfs_client *clp) 67 { 68 return nfs_client_is_local(clp) && localio_enabled; 69 } 70 EXPORT_SYMBOL_GPL(nfs_server_is_local); 71 72 /* 73 * UUID_IS_LOCAL XDR functions 74 */ 75 76 static void localio_xdr_enc_uuidargs(struct rpc_rqst *req, 77 struct xdr_stream *xdr, 78 const void *data) 79 { 80 const u8 *uuid = data; 81 82 encode_opaque_fixed(xdr, uuid, UUID_SIZE); 83 } 84 85 static int localio_xdr_dec_uuidres(struct rpc_rqst *req, 86 struct xdr_stream *xdr, 87 void *result) 88 { 89 /* void return */ 90 return 0; 91 } 92 93 static const struct rpc_procinfo nfs_localio_procedures[] = { 94 [LOCALIOPROC_UUID_IS_LOCAL] = { 95 .p_proc = LOCALIOPROC_UUID_IS_LOCAL, 96 .p_encode = localio_xdr_enc_uuidargs, 97 .p_decode = localio_xdr_dec_uuidres, 98 .p_arglen = XDR_QUADLEN(UUID_SIZE), 99 .p_replen = 0, 100 .p_statidx = LOCALIOPROC_UUID_IS_LOCAL, 101 .p_name = "UUID_IS_LOCAL", 102 }, 103 }; 104 105 static unsigned int nfs_localio_counts[ARRAY_SIZE(nfs_localio_procedures)]; 106 static const struct rpc_version nfslocalio_version1 = { 107 .number = 1, 108 .nrprocs = ARRAY_SIZE(nfs_localio_procedures), 109 .procs = nfs_localio_procedures, 110 .counts = nfs_localio_counts, 111 }; 112 113 static const struct rpc_version *nfslocalio_version[] = { 114 [1] = &nfslocalio_version1, 115 }; 116 117 extern const struct rpc_program nfslocalio_program; 118 static struct rpc_stat nfslocalio_rpcstat = { &nfslocalio_program }; 119 120 const struct rpc_program nfslocalio_program = { 121 .name = "nfslocalio", 122 .number = NFS_LOCALIO_PROGRAM, 123 .nrvers = ARRAY_SIZE(nfslocalio_version), 124 .version = nfslocalio_version, 125 .stats = &nfslocalio_rpcstat, 126 }; 127 128 /* 129 * nfs_init_localioclient - Initialise an NFS localio client connection 130 */ 131 static struct rpc_clnt *nfs_init_localioclient(struct nfs_client *clp) 132 { 133 struct rpc_clnt *rpcclient_localio; 134 135 rpcclient_localio = rpc_bind_new_program(clp->cl_rpcclient, 136 &nfslocalio_program, 1); 137 138 dprintk_rcu("%s: server (%s) %s NFS LOCALIO.\n", 139 __func__, rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR), 140 (IS_ERR(rpcclient_localio) ? "does not support" : "supports")); 141 142 return rpcclient_localio; 143 } 144 145 static bool nfs_server_uuid_is_local(struct nfs_client *clp) 146 { 147 u8 uuid[UUID_SIZE]; 148 struct rpc_message msg = { 149 .rpc_argp = &uuid, 150 }; 151 struct rpc_clnt *rpcclient_localio; 152 int status; 153 154 rpcclient_localio = nfs_init_localioclient(clp); 155 if (IS_ERR(rpcclient_localio)) 156 return false; 157 158 export_uuid(uuid, &clp->cl_uuid.uuid); 159 160 msg.rpc_proc = &nfs_localio_procedures[LOCALIOPROC_UUID_IS_LOCAL]; 161 status = rpc_call_sync(rpcclient_localio, &msg, 0); 162 dprintk("%s: NFS reply UUID_IS_LOCAL: status=%d\n", 163 __func__, status); 164 rpc_shutdown_client(rpcclient_localio); 165 166 /* Server is only local if it initialized required struct members */ 167 if (status || !rcu_access_pointer(clp->cl_uuid.net) || !clp->cl_uuid.dom) 168 return false; 169 170 return true; 171 } 172 173 /* 174 * nfs_local_probe - probe local i/o support for an nfs_server and nfs_client 175 * - called after alloc_client and init_client (so cl_rpcclient exists) 176 * - this function is idempotent, it can be called for old or new clients 177 */ 178 static void nfs_local_probe(struct nfs_client *clp) 179 { 180 /* Disallow localio if disabled via sysfs or AUTH_SYS isn't used */ 181 if (!localio_enabled || 182 clp->cl_rpcclient->cl_auth->au_flavor != RPC_AUTH_UNIX) { 183 nfs_localio_disable_client(clp); 184 return; 185 } 186 187 if (nfs_client_is_local(clp)) 188 return; 189 190 if (!nfs_uuid_begin(&clp->cl_uuid)) 191 return; 192 if (nfs_server_uuid_is_local(clp)) 193 nfs_localio_enable_client(clp); 194 nfs_uuid_end(&clp->cl_uuid); 195 } 196 197 void nfs_local_probe_async_work(struct work_struct *work) 198 { 199 struct nfs_client *clp = 200 container_of(work, struct nfs_client, cl_local_probe_work); 201 202 if (!refcount_inc_not_zero(&clp->cl_count)) 203 return; 204 nfs_local_probe(clp); 205 nfs_put_client(clp); 206 } 207 208 void nfs_local_probe_async(struct nfs_client *clp) 209 { 210 queue_work(nfsiod_workqueue, &clp->cl_local_probe_work); 211 } 212 EXPORT_SYMBOL_GPL(nfs_local_probe_async); 213 214 static inline void nfs_local_file_put(struct nfsd_file *localio) 215 { 216 /* nfs_to_nfsd_file_put_local() expects an __rcu pointer 217 * but we have a __kernel pointer. It is always safe 218 * to cast a __kernel pointer to an __rcu pointer 219 * because the cast only weakens what is known about the pointer. 220 */ 221 struct nfsd_file __rcu *nf = (struct nfsd_file __rcu*) localio; 222 223 nfs_to_nfsd_file_put_local(&nf); 224 } 225 226 /* 227 * __nfs_local_open_fh - open a local filehandle in terms of nfsd_file. 228 * 229 * Returns a pointer to a struct nfsd_file or ERR_PTR. 230 * Caller must release returned nfsd_file with nfs_to_nfsd_file_put_local(). 231 */ 232 static struct nfsd_file * 233 __nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred, 234 struct nfs_fh *fh, struct nfs_file_localio *nfl, 235 struct nfsd_file __rcu **pnf, 236 const fmode_t mode) 237 { 238 int status = 0; 239 struct nfsd_file *localio; 240 241 localio = nfs_open_local_fh(&clp->cl_uuid, clp->cl_rpcclient, 242 cred, fh, nfl, pnf, mode); 243 if (IS_ERR(localio)) { 244 status = PTR_ERR(localio); 245 switch (status) { 246 case -ENOMEM: 247 case -ENXIO: 248 case -ENOENT: 249 /* Revalidate localio */ 250 nfs_localio_disable_client(clp); 251 nfs_local_probe(clp); 252 } 253 } 254 trace_nfs_local_open_fh(fh, mode, status); 255 return localio; 256 } 257 258 /* 259 * nfs_local_open_fh - open a local filehandle in terms of nfsd_file. 260 * First checking if the open nfsd_file is already cached, otherwise 261 * must __nfs_local_open_fh and insert the nfsd_file in nfs_file_localio. 262 * 263 * Returns a pointer to a struct nfsd_file or NULL. 264 */ 265 struct nfsd_file * 266 nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred, 267 struct nfs_fh *fh, struct nfs_file_localio *nfl, 268 const fmode_t mode) 269 { 270 struct nfsd_file *nf, __rcu **pnf; 271 272 if (!nfs_server_is_local(clp)) 273 return NULL; 274 if (mode & ~(FMODE_READ | FMODE_WRITE)) 275 return NULL; 276 277 if (mode & FMODE_WRITE) 278 pnf = &nfl->rw_file; 279 else 280 pnf = &nfl->ro_file; 281 282 nf = __nfs_local_open_fh(clp, cred, fh, nfl, pnf, mode); 283 if (IS_ERR(nf)) 284 return NULL; 285 return nf; 286 } 287 EXPORT_SYMBOL_GPL(nfs_local_open_fh); 288 289 static void 290 nfs_local_iocb_free(struct nfs_local_kiocb *iocb) 291 { 292 kfree(iocb->bvec); 293 kfree(iocb); 294 } 295 296 static struct nfs_local_kiocb * 297 nfs_local_iocb_alloc(struct nfs_pgio_header *hdr, 298 struct file *file, gfp_t flags) 299 { 300 struct nfs_local_kiocb *iocb; 301 302 iocb = kzalloc(sizeof(*iocb), flags); 303 if (iocb == NULL) 304 return NULL; 305 306 iocb->bvec = kmalloc_array(hdr->page_array.npages, 307 sizeof(struct bio_vec), flags); 308 if (iocb->bvec == NULL) { 309 kfree(iocb); 310 return NULL; 311 } 312 313 init_sync_kiocb(&iocb->kiocb, file); 314 315 iocb->hdr = hdr; 316 iocb->kiocb.ki_pos = hdr->args.offset; 317 iocb->kiocb.ki_flags &= ~IOCB_APPEND; 318 iocb->kiocb.ki_complete = NULL; 319 iocb->aio_complete_work = NULL; 320 321 iocb->end_iter_index = -1; 322 323 return iocb; 324 } 325 326 static bool 327 nfs_is_local_dio_possible(struct nfs_local_kiocb *iocb, int rw, 328 size_t len, struct nfs_local_dio *local_dio) 329 { 330 struct nfs_pgio_header *hdr = iocb->hdr; 331 loff_t offset = hdr->args.offset; 332 u32 nf_dio_mem_align, nf_dio_offset_align, nf_dio_read_offset_align; 333 loff_t start_end, orig_end, middle_end; 334 335 nfs_to->nfsd_file_dio_alignment(iocb->localio, &nf_dio_mem_align, 336 &nf_dio_offset_align, &nf_dio_read_offset_align); 337 if (rw == ITER_DEST) 338 nf_dio_offset_align = nf_dio_read_offset_align; 339 340 if (unlikely(!nf_dio_mem_align || !nf_dio_offset_align)) 341 return false; 342 if (unlikely(len < nf_dio_offset_align)) 343 return false; 344 345 local_dio->mem_align = nf_dio_mem_align; 346 local_dio->offset_align = nf_dio_offset_align; 347 348 start_end = round_up(offset, nf_dio_offset_align); 349 orig_end = offset + len; 350 middle_end = round_down(orig_end, nf_dio_offset_align); 351 352 local_dio->middle_offset = start_end; 353 local_dio->end_offset = middle_end; 354 355 local_dio->start_len = start_end - offset; 356 local_dio->middle_len = middle_end - start_end; 357 local_dio->end_len = orig_end - middle_end; 358 359 if (rw == ITER_DEST) 360 trace_nfs_local_dio_read(hdr->inode, offset, len, local_dio); 361 else 362 trace_nfs_local_dio_write(hdr->inode, offset, len, local_dio); 363 return true; 364 } 365 366 static bool nfs_iov_iter_aligned_bvec(const struct iov_iter *i, 367 unsigned int addr_mask, unsigned int len_mask) 368 { 369 const struct bio_vec *bvec = i->bvec; 370 size_t skip = i->iov_offset; 371 size_t size = i->count; 372 373 if (size & len_mask) 374 return false; 375 do { 376 size_t len = bvec->bv_len; 377 378 if (len > size) 379 len = size; 380 if ((unsigned long)(bvec->bv_offset + skip) & addr_mask) 381 return false; 382 bvec++; 383 size -= len; 384 skip = 0; 385 } while (size); 386 387 return true; 388 } 389 390 static void 391 nfs_local_iter_setup(struct iov_iter *iter, int rw, struct bio_vec *bvec, 392 unsigned int nvecs, unsigned long total, 393 size_t start, size_t len) 394 { 395 iov_iter_bvec(iter, rw, bvec, nvecs, total); 396 if (start) 397 iov_iter_advance(iter, start); 398 iov_iter_truncate(iter, len); 399 } 400 401 /* 402 * Setup as many as 3 iov_iter based on extents described by @local_dio. 403 * Returns the number of iov_iter that were setup. 404 */ 405 static int 406 nfs_local_iters_setup_dio(struct nfs_local_kiocb *iocb, int rw, 407 unsigned int nvecs, unsigned long total, 408 struct nfs_local_dio *local_dio) 409 { 410 int n_iters = 0; 411 struct iov_iter *iters = iocb->iters; 412 413 /* Setup misaligned start? */ 414 if (local_dio->start_len) { 415 nfs_local_iter_setup(&iters[n_iters], rw, iocb->bvec, 416 nvecs, total, 0, local_dio->start_len); 417 ++n_iters; 418 } 419 420 /* 421 * Setup DIO-aligned middle, if there is no misaligned end (below) 422 * then AIO completion is used, see nfs_local_call_{read,write} 423 */ 424 nfs_local_iter_setup(&iters[n_iters], rw, iocb->bvec, nvecs, 425 total, local_dio->start_len, local_dio->middle_len); 426 427 iocb->iter_is_dio_aligned[n_iters] = 428 nfs_iov_iter_aligned_bvec(&iters[n_iters], 429 local_dio->mem_align-1, local_dio->offset_align-1); 430 431 if (unlikely(!iocb->iter_is_dio_aligned[n_iters])) { 432 trace_nfs_local_dio_misaligned(iocb->hdr->inode, 433 local_dio->start_len, local_dio->middle_len, local_dio); 434 return 0; /* no DIO-aligned IO possible */ 435 } 436 iocb->end_iter_index = n_iters; 437 ++n_iters; 438 439 /* Setup misaligned end? */ 440 if (local_dio->end_len) { 441 nfs_local_iter_setup(&iters[n_iters], rw, iocb->bvec, 442 nvecs, total, local_dio->start_len + 443 local_dio->middle_len, local_dio->end_len); 444 iocb->end_iter_index = n_iters; 445 ++n_iters; 446 } 447 448 atomic_set(&iocb->n_iters, n_iters); 449 return n_iters; 450 } 451 452 static noinline_for_stack void 453 nfs_local_iters_init(struct nfs_local_kiocb *iocb, int rw) 454 { 455 struct nfs_pgio_header *hdr = iocb->hdr; 456 struct page **pagevec = hdr->page_array.pagevec; 457 unsigned long v, total; 458 unsigned int base; 459 size_t len; 460 461 v = 0; 462 total = hdr->args.count; 463 base = hdr->args.pgbase; 464 while (total && v < hdr->page_array.npages) { 465 len = min_t(size_t, total, PAGE_SIZE - base); 466 bvec_set_page(&iocb->bvec[v], *pagevec, len, base); 467 total -= len; 468 ++pagevec; 469 ++v; 470 base = 0; 471 } 472 len = hdr->args.count - total; 473 474 /* 475 * For each iocb, iocb->n_iters is always at least 1 and we always 476 * end io after first nfs_local_pgio_done call unless misaligned DIO. 477 */ 478 atomic_set(&iocb->n_iters, 1); 479 480 if (test_bit(NFS_IOHDR_ODIRECT, &hdr->flags)) { 481 struct nfs_local_dio local_dio; 482 483 if (nfs_is_local_dio_possible(iocb, rw, len, &local_dio) && 484 nfs_local_iters_setup_dio(iocb, rw, v, len, &local_dio) != 0) { 485 /* Ensure DIO WRITE's IO on stable storage upon completion */ 486 if (rw == ITER_SOURCE) 487 iocb->kiocb.ki_flags |= IOCB_DSYNC|IOCB_SYNC; 488 return; /* is DIO-aligned */ 489 } 490 } 491 492 /* Use buffered IO */ 493 iov_iter_bvec(&iocb->iters[0], rw, iocb->bvec, v, len); 494 } 495 496 static void 497 nfs_local_hdr_release(struct nfs_pgio_header *hdr, 498 const struct rpc_call_ops *call_ops) 499 { 500 call_ops->rpc_call_done(&hdr->task, hdr); 501 call_ops->rpc_release(hdr); 502 } 503 504 static void 505 nfs_local_pgio_init(struct nfs_pgio_header *hdr, 506 const struct rpc_call_ops *call_ops) 507 { 508 hdr->task.tk_ops = call_ops; 509 if (!hdr->task.tk_start) 510 hdr->task.tk_start = ktime_get(); 511 } 512 513 static bool 514 nfs_local_pgio_done(struct nfs_local_kiocb *iocb, long status, bool force) 515 { 516 struct nfs_pgio_header *hdr = iocb->hdr; 517 518 /* Must handle partial completions */ 519 if (status >= 0) { 520 hdr->res.count += status; 521 /* @hdr was initialized to 0 (zeroed during allocation) */ 522 if (hdr->task.tk_status == 0) 523 hdr->res.op_status = NFS4_OK; 524 } else { 525 hdr->res.op_status = nfs_localio_errno_to_nfs4_stat(status); 526 hdr->task.tk_status = status; 527 } 528 529 if (force) 530 return true; 531 532 BUG_ON(atomic_read(&iocb->n_iters) <= 0); 533 return atomic_dec_and_test(&iocb->n_iters); 534 } 535 536 static void 537 nfs_local_iocb_release(struct nfs_local_kiocb *iocb) 538 { 539 nfs_local_file_put(iocb->localio); 540 nfs_local_iocb_free(iocb); 541 } 542 543 static void 544 nfs_local_pgio_release(struct nfs_local_kiocb *iocb) 545 { 546 struct nfs_pgio_header *hdr = iocb->hdr; 547 548 nfs_local_iocb_release(iocb); 549 nfs_local_hdr_release(hdr, hdr->task.tk_ops); 550 } 551 552 /* 553 * Complete the I/O from iocb->kiocb.ki_complete() 554 * 555 * Note that this function can be called from a bottom half context, 556 * hence we need to queue the rpc_call_done() etc to a workqueue 557 */ 558 static inline void nfs_local_pgio_aio_complete(struct nfs_local_kiocb *iocb) 559 { 560 INIT_WORK(&iocb->work, iocb->aio_complete_work); 561 queue_work(nfsiod_workqueue, &iocb->work); 562 } 563 564 static void nfs_local_read_done(struct nfs_local_kiocb *iocb) 565 { 566 struct nfs_pgio_header *hdr = iocb->hdr; 567 struct file *filp = iocb->kiocb.ki_filp; 568 long status = hdr->task.tk_status; 569 570 if ((iocb->kiocb.ki_flags & IOCB_DIRECT) && status == -EINVAL) { 571 /* Underlying FS will return -EINVAL if misaligned DIO is attempted. */ 572 pr_info_ratelimited("nfs: Unexpected direct I/O read alignment failure\n"); 573 } 574 575 /* 576 * Must clear replen otherwise NFSv3 data corruption will occur 577 * if/when switching from LOCALIO back to using normal RPC. 578 */ 579 hdr->res.replen = 0; 580 581 /* nfs_readpage_result() handles short read */ 582 583 if (hdr->args.offset + hdr->res.count >= i_size_read(file_inode(filp))) 584 hdr->res.eof = true; 585 586 dprintk("%s: read %ld bytes eof %d.\n", __func__, 587 status > 0 ? status : 0, hdr->res.eof); 588 } 589 590 static inline void nfs_local_read_iocb_done(struct nfs_local_kiocb *iocb) 591 { 592 nfs_local_read_done(iocb); 593 nfs_local_pgio_release(iocb); 594 } 595 596 static void nfs_local_read_aio_complete_work(struct work_struct *work) 597 { 598 struct nfs_local_kiocb *iocb = 599 container_of(work, struct nfs_local_kiocb, work); 600 601 nfs_local_read_iocb_done(iocb); 602 } 603 604 static void nfs_local_read_aio_complete(struct kiocb *kiocb, long ret) 605 { 606 struct nfs_local_kiocb *iocb = 607 container_of(kiocb, struct nfs_local_kiocb, kiocb); 608 609 /* AIO completion of DIO read should always be last to complete */ 610 if (unlikely(!nfs_local_pgio_done(iocb, ret, false))) 611 return; 612 613 nfs_local_pgio_aio_complete(iocb); /* Calls nfs_local_read_aio_complete_work */ 614 } 615 616 static void nfs_local_call_read(struct work_struct *work) 617 { 618 struct nfs_local_kiocb *iocb = 619 container_of(work, struct nfs_local_kiocb, work); 620 struct file *filp = iocb->kiocb.ki_filp; 621 bool force_done = false; 622 ssize_t status; 623 int n_iters; 624 625 n_iters = atomic_read(&iocb->n_iters); 626 for (int i = 0; i < n_iters ; i++) { 627 if (iocb->iter_is_dio_aligned[i]) { 628 iocb->kiocb.ki_flags |= IOCB_DIRECT; 629 /* Only use AIO completion if DIO-aligned segment is last */ 630 if (i == iocb->end_iter_index) { 631 iocb->kiocb.ki_complete = nfs_local_read_aio_complete; 632 iocb->aio_complete_work = nfs_local_read_aio_complete_work; 633 } 634 } else 635 iocb->kiocb.ki_flags &= ~IOCB_DIRECT; 636 637 scoped_with_creds(filp->f_cred) 638 status = filp->f_op->read_iter(&iocb->kiocb, &iocb->iters[i]); 639 640 if (status != -EIOCBQUEUED) { 641 if (unlikely(status >= 0 && status < iocb->iters[i].count)) 642 force_done = true; /* Partial read */ 643 if (nfs_local_pgio_done(iocb, status, force_done)) { 644 nfs_local_read_iocb_done(iocb); 645 break; 646 } 647 } 648 } 649 } 650 651 static int 652 nfs_local_do_read(struct nfs_local_kiocb *iocb, 653 const struct rpc_call_ops *call_ops) 654 { 655 struct nfs_pgio_header *hdr = iocb->hdr; 656 657 dprintk("%s: vfs_read count=%u pos=%llu\n", 658 __func__, hdr->args.count, hdr->args.offset); 659 660 nfs_local_pgio_init(hdr, call_ops); 661 hdr->res.eof = false; 662 663 INIT_WORK(&iocb->work, nfs_local_call_read); 664 queue_work(nfslocaliod_workqueue, &iocb->work); 665 666 return 0; 667 } 668 669 static void 670 nfs_copy_boot_verifier(struct nfs_write_verifier *verifier, struct inode *inode) 671 { 672 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 673 u32 *verf = (u32 *)verifier->data; 674 unsigned int seq; 675 676 do { 677 seq = read_seqbegin(&clp->cl_boot_lock); 678 verf[0] = (u32)clp->cl_nfssvc_boot.tv_sec; 679 verf[1] = (u32)clp->cl_nfssvc_boot.tv_nsec; 680 } while (read_seqretry(&clp->cl_boot_lock, seq)); 681 } 682 683 static void 684 nfs_reset_boot_verifier(struct inode *inode) 685 { 686 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 687 688 write_seqlock(&clp->cl_boot_lock); 689 ktime_get_real_ts64(&clp->cl_nfssvc_boot); 690 write_sequnlock(&clp->cl_boot_lock); 691 } 692 693 static void 694 nfs_set_local_verifier(struct inode *inode, 695 struct nfs_writeverf *verf, 696 enum nfs3_stable_how how) 697 { 698 nfs_copy_boot_verifier(&verf->verifier, inode); 699 verf->committed = how; 700 } 701 702 /* Factored out from fs/nfsd/vfs.h:fh_getattr() */ 703 static int __vfs_getattr(const struct path *p, struct kstat *stat, int version) 704 { 705 u32 request_mask = STATX_BASIC_STATS; 706 707 if (version == 4) 708 request_mask |= (STATX_BTIME | STATX_CHANGE_COOKIE); 709 return vfs_getattr(p, stat, request_mask, AT_STATX_SYNC_AS_STAT); 710 } 711 712 /* Copied from fs/nfsd/nfsfh.c:nfsd4_change_attribute() */ 713 static u64 __nfsd4_change_attribute(const struct kstat *stat, 714 const struct inode *inode) 715 { 716 u64 chattr; 717 718 if (stat->result_mask & STATX_CHANGE_COOKIE) { 719 chattr = stat->change_cookie; 720 if (S_ISREG(inode->i_mode) && 721 !(stat->attributes & STATX_ATTR_CHANGE_MONOTONIC)) { 722 chattr += (u64)stat->ctime.tv_sec << 30; 723 chattr += stat->ctime.tv_nsec; 724 } 725 } else { 726 chattr = time_to_chattr(&stat->ctime); 727 } 728 return chattr; 729 } 730 731 static void nfs_local_vfs_getattr(struct nfs_local_kiocb *iocb) 732 { 733 struct kstat stat; 734 struct file *filp = iocb->kiocb.ki_filp; 735 struct nfs_pgio_header *hdr = iocb->hdr; 736 struct nfs_fattr *fattr = hdr->res.fattr; 737 int version = NFS_PROTO(hdr->inode)->version; 738 739 if (unlikely(!fattr) || __vfs_getattr(&filp->f_path, &stat, version)) 740 return; 741 742 fattr->valid = (NFS_ATTR_FATTR_FILEID | 743 NFS_ATTR_FATTR_CHANGE | 744 NFS_ATTR_FATTR_SIZE | 745 NFS_ATTR_FATTR_ATIME | 746 NFS_ATTR_FATTR_MTIME | 747 NFS_ATTR_FATTR_CTIME | 748 NFS_ATTR_FATTR_SPACE_USED); 749 750 fattr->fileid = stat.ino; 751 fattr->size = stat.size; 752 fattr->atime = stat.atime; 753 fattr->mtime = stat.mtime; 754 fattr->ctime = stat.ctime; 755 if (version == 4) { 756 fattr->change_attr = 757 __nfsd4_change_attribute(&stat, file_inode(filp)); 758 } else 759 fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime); 760 fattr->du.nfs3.used = stat.blocks << 9; 761 } 762 763 static void nfs_local_write_done(struct nfs_local_kiocb *iocb) 764 { 765 struct nfs_pgio_header *hdr = iocb->hdr; 766 long status = hdr->task.tk_status; 767 768 dprintk("%s: wrote %ld bytes.\n", __func__, status > 0 ? status : 0); 769 770 if ((iocb->kiocb.ki_flags & IOCB_DIRECT) && status == -EINVAL) { 771 /* Underlying FS will return -EINVAL if misaligned DIO is attempted. */ 772 pr_info_ratelimited("nfs: Unexpected direct I/O write alignment failure\n"); 773 } 774 775 /* Handle short writes as if they are ENOSPC */ 776 status = hdr->res.count; 777 if (status > 0 && status < hdr->args.count) { 778 hdr->mds_offset += status; 779 hdr->args.offset += status; 780 hdr->args.pgbase += status; 781 hdr->args.count -= status; 782 nfs_set_pgio_error(hdr, -ENOSPC, hdr->args.offset); 783 status = -ENOSPC; 784 /* record -ENOSPC in terms of nfs_local_pgio_done */ 785 (void) nfs_local_pgio_done(iocb, status, true); 786 } 787 if (hdr->task.tk_status < 0) 788 nfs_reset_boot_verifier(hdr->inode); 789 } 790 791 static inline void nfs_local_write_iocb_done(struct nfs_local_kiocb *iocb) 792 { 793 nfs_local_write_done(iocb); 794 nfs_local_vfs_getattr(iocb); 795 nfs_local_pgio_release(iocb); 796 } 797 798 static void nfs_local_write_aio_complete_work(struct work_struct *work) 799 { 800 struct nfs_local_kiocb *iocb = 801 container_of(work, struct nfs_local_kiocb, work); 802 803 nfs_local_write_iocb_done(iocb); 804 } 805 806 static void nfs_local_write_aio_complete(struct kiocb *kiocb, long ret) 807 { 808 struct nfs_local_kiocb *iocb = 809 container_of(kiocb, struct nfs_local_kiocb, kiocb); 810 811 /* AIO completion of DIO write should always be last to complete */ 812 if (unlikely(!nfs_local_pgio_done(iocb, ret, false))) 813 return; 814 815 nfs_local_pgio_aio_complete(iocb); /* Calls nfs_local_write_aio_complete_work */ 816 } 817 818 static void nfs_local_call_write(struct work_struct *work) 819 { 820 struct nfs_local_kiocb *iocb = 821 container_of(work, struct nfs_local_kiocb, work); 822 struct file *filp = iocb->kiocb.ki_filp; 823 unsigned long old_flags = current->flags; 824 bool force_done = false; 825 ssize_t status; 826 int n_iters; 827 828 current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO; 829 830 file_start_write(filp); 831 n_iters = atomic_read(&iocb->n_iters); 832 for (int i = 0; i < n_iters ; i++) { 833 if (iocb->iter_is_dio_aligned[i]) { 834 iocb->kiocb.ki_flags |= IOCB_DIRECT; 835 /* Only use AIO completion if DIO-aligned segment is last */ 836 if (i == iocb->end_iter_index) { 837 iocb->kiocb.ki_complete = nfs_local_write_aio_complete; 838 iocb->aio_complete_work = nfs_local_write_aio_complete_work; 839 } 840 } else 841 iocb->kiocb.ki_flags &= ~IOCB_DIRECT; 842 843 scoped_with_creds(filp->f_cred) 844 status = filp->f_op->write_iter(&iocb->kiocb, &iocb->iters[i]); 845 846 if (status != -EIOCBQUEUED) { 847 if (unlikely(status >= 0 && status < iocb->iters[i].count)) 848 force_done = true; /* Partial write */ 849 if (nfs_local_pgio_done(iocb, status, force_done)) { 850 nfs_local_write_iocb_done(iocb); 851 break; 852 } 853 } 854 } 855 file_end_write(filp); 856 857 current->flags = old_flags; 858 } 859 860 static int 861 nfs_local_do_write(struct nfs_local_kiocb *iocb, 862 const struct rpc_call_ops *call_ops) 863 { 864 struct nfs_pgio_header *hdr = iocb->hdr; 865 866 dprintk("%s: vfs_write count=%u pos=%llu %s\n", 867 __func__, hdr->args.count, hdr->args.offset, 868 (hdr->args.stable == NFS_UNSTABLE) ? "unstable" : "stable"); 869 870 switch (hdr->args.stable) { 871 default: 872 break; 873 case NFS_DATA_SYNC: 874 iocb->kiocb.ki_flags |= IOCB_DSYNC; 875 break; 876 case NFS_FILE_SYNC: 877 iocb->kiocb.ki_flags |= IOCB_DSYNC|IOCB_SYNC; 878 } 879 880 nfs_local_pgio_init(hdr, call_ops); 881 882 nfs_set_local_verifier(hdr->inode, hdr->res.verf, hdr->args.stable); 883 884 INIT_WORK(&iocb->work, nfs_local_call_write); 885 queue_work(nfslocaliod_workqueue, &iocb->work); 886 887 return 0; 888 } 889 890 static struct nfs_local_kiocb * 891 nfs_local_iocb_init(struct nfs_pgio_header *hdr, struct nfsd_file *localio) 892 { 893 struct file *file = nfs_to->nfsd_file_file(localio); 894 struct nfs_local_kiocb *iocb; 895 gfp_t gfp_mask; 896 int rw; 897 898 if (hdr->rw_mode & FMODE_READ) { 899 if (!file->f_op->read_iter) 900 return ERR_PTR(-EOPNOTSUPP); 901 gfp_mask = GFP_KERNEL; 902 rw = ITER_DEST; 903 } else { 904 if (!file->f_op->write_iter) 905 return ERR_PTR(-EOPNOTSUPP); 906 gfp_mask = GFP_NOIO; 907 rw = ITER_SOURCE; 908 } 909 910 iocb = nfs_local_iocb_alloc(hdr, file, gfp_mask); 911 if (iocb == NULL) 912 return ERR_PTR(-ENOMEM); 913 iocb->hdr = hdr; 914 iocb->localio = localio; 915 916 nfs_local_iters_init(iocb, rw); 917 918 return iocb; 919 } 920 921 int nfs_local_doio(struct nfs_client *clp, struct nfsd_file *localio, 922 struct nfs_pgio_header *hdr, 923 const struct rpc_call_ops *call_ops) 924 { 925 struct nfs_local_kiocb *iocb; 926 int status = 0; 927 928 if (!hdr->args.count) 929 return 0; 930 931 iocb = nfs_local_iocb_init(hdr, localio); 932 if (IS_ERR(iocb)) 933 return PTR_ERR(iocb); 934 935 switch (hdr->rw_mode) { 936 case FMODE_READ: 937 status = nfs_local_do_read(iocb, call_ops); 938 break; 939 case FMODE_WRITE: 940 status = nfs_local_do_write(iocb, call_ops); 941 break; 942 default: 943 dprintk("%s: invalid mode: %d\n", __func__, 944 hdr->rw_mode); 945 status = -EOPNOTSUPP; 946 } 947 948 if (status != 0) { 949 if (status == -EAGAIN) 950 nfs_localio_disable_client(clp); 951 nfs_local_iocb_release(iocb); 952 hdr->task.tk_status = status; 953 nfs_local_hdr_release(hdr, call_ops); 954 } 955 return status; 956 } 957 958 static void 959 nfs_local_init_commit(struct nfs_commit_data *data, 960 const struct rpc_call_ops *call_ops) 961 { 962 data->task.tk_ops = call_ops; 963 } 964 965 static int 966 nfs_local_run_commit(struct file *filp, struct nfs_commit_data *data) 967 { 968 loff_t start = data->args.offset; 969 loff_t end = LLONG_MAX; 970 971 if (data->args.count > 0) { 972 end = start + data->args.count - 1; 973 if (end < start) 974 end = LLONG_MAX; 975 } 976 977 dprintk("%s: commit %llu - %llu\n", __func__, start, end); 978 return vfs_fsync_range(filp, start, end, 0); 979 } 980 981 static void 982 nfs_local_commit_done(struct nfs_commit_data *data, int status) 983 { 984 if (status >= 0) { 985 nfs_set_local_verifier(data->inode, 986 data->res.verf, 987 NFS_FILE_SYNC); 988 data->res.op_status = NFS4_OK; 989 data->task.tk_status = 0; 990 } else { 991 nfs_reset_boot_verifier(data->inode); 992 data->res.op_status = nfs_localio_errno_to_nfs4_stat(status); 993 data->task.tk_status = status; 994 } 995 } 996 997 static void 998 nfs_local_release_commit_data(struct nfsd_file *localio, 999 struct nfs_commit_data *data, 1000 const struct rpc_call_ops *call_ops) 1001 { 1002 nfs_local_file_put(localio); 1003 call_ops->rpc_call_done(&data->task, data); 1004 call_ops->rpc_release(data); 1005 } 1006 1007 static void 1008 nfs_local_fsync_ctx_free(struct nfs_local_fsync_ctx *ctx) 1009 { 1010 nfs_local_release_commit_data(ctx->localio, ctx->data, 1011 ctx->data->task.tk_ops); 1012 kfree(ctx); 1013 } 1014 1015 static void 1016 nfs_local_fsync_work(struct work_struct *work) 1017 { 1018 struct nfs_local_fsync_ctx *ctx; 1019 int status; 1020 1021 ctx = container_of(work, struct nfs_local_fsync_ctx, work); 1022 1023 status = nfs_local_run_commit(nfs_to->nfsd_file_file(ctx->localio), 1024 ctx->data); 1025 nfs_local_commit_done(ctx->data, status); 1026 if (ctx->done != NULL) 1027 complete(ctx->done); 1028 nfs_local_fsync_ctx_free(ctx); 1029 } 1030 1031 static struct nfs_local_fsync_ctx * 1032 nfs_local_fsync_ctx_alloc(struct nfs_commit_data *data, 1033 struct nfsd_file *localio, gfp_t flags) 1034 { 1035 struct nfs_local_fsync_ctx *ctx = kmalloc(sizeof(*ctx), flags); 1036 1037 if (ctx != NULL) { 1038 ctx->localio = localio; 1039 ctx->data = data; 1040 INIT_WORK(&ctx->work, nfs_local_fsync_work); 1041 ctx->done = NULL; 1042 } 1043 return ctx; 1044 } 1045 1046 int nfs_local_commit(struct nfsd_file *localio, 1047 struct nfs_commit_data *data, 1048 const struct rpc_call_ops *call_ops, int how) 1049 { 1050 struct nfs_local_fsync_ctx *ctx; 1051 1052 ctx = nfs_local_fsync_ctx_alloc(data, localio, GFP_KERNEL); 1053 if (!ctx) { 1054 nfs_local_commit_done(data, -ENOMEM); 1055 nfs_local_release_commit_data(localio, data, call_ops); 1056 return -ENOMEM; 1057 } 1058 1059 nfs_local_init_commit(data, call_ops); 1060 1061 if (how & FLUSH_SYNC) { 1062 DECLARE_COMPLETION_ONSTACK(done); 1063 ctx->done = &done; 1064 queue_work(nfsiod_workqueue, &ctx->work); 1065 wait_for_completion(&done); 1066 } else 1067 queue_work(nfsiod_workqueue, &ctx->work); 1068 1069 return 0; 1070 } 1071