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 struct nfs_local_kiocb { 34 struct kiocb kiocb; 35 struct bio_vec *bvec; 36 struct nfs_pgio_header *hdr; 37 struct work_struct work; 38 void (*aio_complete_work)(struct work_struct *); 39 struct nfsd_file *localio; 40 }; 41 42 struct nfs_local_fsync_ctx { 43 struct nfsd_file *localio; 44 struct nfs_commit_data *data; 45 struct work_struct work; 46 struct completion *done; 47 }; 48 49 static bool localio_enabled __read_mostly = true; 50 module_param(localio_enabled, bool, 0644); 51 52 static bool localio_O_DIRECT_semantics __read_mostly = false; 53 module_param(localio_O_DIRECT_semantics, bool, 0644); 54 MODULE_PARM_DESC(localio_O_DIRECT_semantics, 55 "LOCALIO will use O_DIRECT semantics to filesystem."); 56 57 static inline bool nfs_client_is_local(const struct nfs_client *clp) 58 { 59 return !!rcu_access_pointer(clp->cl_uuid.net); 60 } 61 62 bool nfs_server_is_local(const struct nfs_client *clp) 63 { 64 return nfs_client_is_local(clp) && localio_enabled; 65 } 66 EXPORT_SYMBOL_GPL(nfs_server_is_local); 67 68 /* 69 * UUID_IS_LOCAL XDR functions 70 */ 71 72 static void localio_xdr_enc_uuidargs(struct rpc_rqst *req, 73 struct xdr_stream *xdr, 74 const void *data) 75 { 76 const u8 *uuid = data; 77 78 encode_opaque_fixed(xdr, uuid, UUID_SIZE); 79 } 80 81 static int localio_xdr_dec_uuidres(struct rpc_rqst *req, 82 struct xdr_stream *xdr, 83 void *result) 84 { 85 /* void return */ 86 return 0; 87 } 88 89 static const struct rpc_procinfo nfs_localio_procedures[] = { 90 [LOCALIOPROC_UUID_IS_LOCAL] = { 91 .p_proc = LOCALIOPROC_UUID_IS_LOCAL, 92 .p_encode = localio_xdr_enc_uuidargs, 93 .p_decode = localio_xdr_dec_uuidres, 94 .p_arglen = XDR_QUADLEN(UUID_SIZE), 95 .p_replen = 0, 96 .p_statidx = LOCALIOPROC_UUID_IS_LOCAL, 97 .p_name = "UUID_IS_LOCAL", 98 }, 99 }; 100 101 static unsigned int nfs_localio_counts[ARRAY_SIZE(nfs_localio_procedures)]; 102 static const struct rpc_version nfslocalio_version1 = { 103 .number = 1, 104 .nrprocs = ARRAY_SIZE(nfs_localio_procedures), 105 .procs = nfs_localio_procedures, 106 .counts = nfs_localio_counts, 107 }; 108 109 static const struct rpc_version *nfslocalio_version[] = { 110 [1] = &nfslocalio_version1, 111 }; 112 113 extern const struct rpc_program nfslocalio_program; 114 static struct rpc_stat nfslocalio_rpcstat = { &nfslocalio_program }; 115 116 const struct rpc_program nfslocalio_program = { 117 .name = "nfslocalio", 118 .number = NFS_LOCALIO_PROGRAM, 119 .nrvers = ARRAY_SIZE(nfslocalio_version), 120 .version = nfslocalio_version, 121 .stats = &nfslocalio_rpcstat, 122 }; 123 124 /* 125 * nfs_init_localioclient - Initialise an NFS localio client connection 126 */ 127 static struct rpc_clnt *nfs_init_localioclient(struct nfs_client *clp) 128 { 129 struct rpc_clnt *rpcclient_localio; 130 131 rpcclient_localio = rpc_bind_new_program(clp->cl_rpcclient, 132 &nfslocalio_program, 1); 133 134 dprintk_rcu("%s: server (%s) %s NFS LOCALIO.\n", 135 __func__, rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR), 136 (IS_ERR(rpcclient_localio) ? "does not support" : "supports")); 137 138 return rpcclient_localio; 139 } 140 141 static bool nfs_server_uuid_is_local(struct nfs_client *clp) 142 { 143 u8 uuid[UUID_SIZE]; 144 struct rpc_message msg = { 145 .rpc_argp = &uuid, 146 }; 147 struct rpc_clnt *rpcclient_localio; 148 int status; 149 150 rpcclient_localio = nfs_init_localioclient(clp); 151 if (IS_ERR(rpcclient_localio)) 152 return false; 153 154 export_uuid(uuid, &clp->cl_uuid.uuid); 155 156 msg.rpc_proc = &nfs_localio_procedures[LOCALIOPROC_UUID_IS_LOCAL]; 157 status = rpc_call_sync(rpcclient_localio, &msg, 0); 158 dprintk("%s: NFS reply UUID_IS_LOCAL: status=%d\n", 159 __func__, status); 160 rpc_shutdown_client(rpcclient_localio); 161 162 /* Server is only local if it initialized required struct members */ 163 if (status || !rcu_access_pointer(clp->cl_uuid.net) || !clp->cl_uuid.dom) 164 return false; 165 166 return true; 167 } 168 169 /* 170 * nfs_local_probe - probe local i/o support for an nfs_server and nfs_client 171 * - called after alloc_client and init_client (so cl_rpcclient exists) 172 * - this function is idempotent, it can be called for old or new clients 173 */ 174 static void nfs_local_probe(struct nfs_client *clp) 175 { 176 /* Disallow localio if disabled via sysfs or AUTH_SYS isn't used */ 177 if (!localio_enabled || 178 clp->cl_rpcclient->cl_auth->au_flavor != RPC_AUTH_UNIX) { 179 nfs_localio_disable_client(clp); 180 return; 181 } 182 183 if (nfs_client_is_local(clp)) { 184 /* If already enabled, disable and re-enable */ 185 nfs_localio_disable_client(clp); 186 } 187 188 if (!nfs_uuid_begin(&clp->cl_uuid)) 189 return; 190 if (nfs_server_uuid_is_local(clp)) 191 nfs_localio_enable_client(clp); 192 nfs_uuid_end(&clp->cl_uuid); 193 } 194 195 void nfs_local_probe_async_work(struct work_struct *work) 196 { 197 struct nfs_client *clp = 198 container_of(work, struct nfs_client, cl_local_probe_work); 199 200 if (!refcount_inc_not_zero(&clp->cl_count)) 201 return; 202 nfs_local_probe(clp); 203 nfs_put_client(clp); 204 } 205 206 void nfs_local_probe_async(struct nfs_client *clp) 207 { 208 queue_work(nfsiod_workqueue, &clp->cl_local_probe_work); 209 } 210 EXPORT_SYMBOL_GPL(nfs_local_probe_async); 211 212 static inline void nfs_local_file_put(struct nfsd_file *localio) 213 { 214 /* nfs_to_nfsd_file_put_local() expects an __rcu pointer 215 * but we have a __kernel pointer. It is always safe 216 * to cast a __kernel pointer to an __rcu pointer 217 * because the cast only weakens what is known about the pointer. 218 */ 219 struct nfsd_file __rcu *nf = (struct nfsd_file __rcu*) localio; 220 221 nfs_to_nfsd_file_put_local(&nf); 222 } 223 224 /* 225 * __nfs_local_open_fh - open a local filehandle in terms of nfsd_file. 226 * 227 * Returns a pointer to a struct nfsd_file or ERR_PTR. 228 * Caller must release returned nfsd_file with nfs_to_nfsd_file_put_local(). 229 */ 230 static struct nfsd_file * 231 __nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred, 232 struct nfs_fh *fh, struct nfs_file_localio *nfl, 233 struct nfsd_file __rcu **pnf, 234 const fmode_t mode) 235 { 236 struct nfsd_file *localio; 237 238 localio = nfs_open_local_fh(&clp->cl_uuid, clp->cl_rpcclient, 239 cred, fh, nfl, pnf, mode); 240 if (IS_ERR(localio)) { 241 int status = PTR_ERR(localio); 242 trace_nfs_local_open_fh(fh, mode, status); 243 switch (status) { 244 case -ENOMEM: 245 case -ENXIO: 246 case -ENOENT: 247 /* Revalidate localio, will disable if unsupported */ 248 nfs_local_probe(clp); 249 } 250 } 251 return localio; 252 } 253 254 /* 255 * nfs_local_open_fh - open a local filehandle in terms of nfsd_file. 256 * First checking if the open nfsd_file is already cached, otherwise 257 * must __nfs_local_open_fh and insert the nfsd_file in nfs_file_localio. 258 * 259 * Returns a pointer to a struct nfsd_file or NULL. 260 */ 261 struct nfsd_file * 262 nfs_local_open_fh(struct nfs_client *clp, const struct cred *cred, 263 struct nfs_fh *fh, struct nfs_file_localio *nfl, 264 const fmode_t mode) 265 { 266 struct nfsd_file *nf, __rcu **pnf; 267 268 if (!nfs_server_is_local(clp)) 269 return NULL; 270 if (mode & ~(FMODE_READ | FMODE_WRITE)) 271 return NULL; 272 273 if (mode & FMODE_WRITE) 274 pnf = &nfl->rw_file; 275 else 276 pnf = &nfl->ro_file; 277 278 nf = __nfs_local_open_fh(clp, cred, fh, nfl, pnf, mode); 279 if (IS_ERR(nf)) 280 return NULL; 281 return nf; 282 } 283 EXPORT_SYMBOL_GPL(nfs_local_open_fh); 284 285 static struct bio_vec * 286 nfs_bvec_alloc_and_import_pagevec(struct page **pagevec, 287 unsigned int npages, gfp_t flags) 288 { 289 struct bio_vec *bvec, *p; 290 291 bvec = kmalloc_array(npages, sizeof(*bvec), flags); 292 if (bvec != NULL) { 293 for (p = bvec; npages > 0; p++, pagevec++, npages--) { 294 p->bv_page = *pagevec; 295 p->bv_len = PAGE_SIZE; 296 p->bv_offset = 0; 297 } 298 } 299 return bvec; 300 } 301 302 static void 303 nfs_local_iocb_free(struct nfs_local_kiocb *iocb) 304 { 305 kfree(iocb->bvec); 306 kfree(iocb); 307 } 308 309 static struct nfs_local_kiocb * 310 nfs_local_iocb_alloc(struct nfs_pgio_header *hdr, 311 struct file *file, gfp_t flags) 312 { 313 struct nfs_local_kiocb *iocb; 314 315 iocb = kmalloc(sizeof(*iocb), flags); 316 if (iocb == NULL) 317 return NULL; 318 iocb->bvec = nfs_bvec_alloc_and_import_pagevec(hdr->page_array.pagevec, 319 hdr->page_array.npages, flags); 320 if (iocb->bvec == NULL) { 321 kfree(iocb); 322 return NULL; 323 } 324 325 if (localio_O_DIRECT_semantics && 326 test_bit(NFS_IOHDR_ODIRECT, &hdr->flags)) { 327 iocb->kiocb.ki_filp = file; 328 iocb->kiocb.ki_flags = IOCB_DIRECT; 329 } else 330 init_sync_kiocb(&iocb->kiocb, file); 331 332 iocb->kiocb.ki_pos = hdr->args.offset; 333 iocb->hdr = hdr; 334 iocb->kiocb.ki_flags &= ~IOCB_APPEND; 335 iocb->aio_complete_work = NULL; 336 337 return iocb; 338 } 339 340 static void 341 nfs_local_iter_init(struct iov_iter *i, struct nfs_local_kiocb *iocb, int dir) 342 { 343 struct nfs_pgio_header *hdr = iocb->hdr; 344 345 iov_iter_bvec(i, dir, iocb->bvec, hdr->page_array.npages, 346 hdr->args.count + hdr->args.pgbase); 347 if (hdr->args.pgbase != 0) 348 iov_iter_advance(i, hdr->args.pgbase); 349 } 350 351 static void 352 nfs_local_hdr_release(struct nfs_pgio_header *hdr, 353 const struct rpc_call_ops *call_ops) 354 { 355 call_ops->rpc_call_done(&hdr->task, hdr); 356 call_ops->rpc_release(hdr); 357 } 358 359 static void 360 nfs_local_pgio_init(struct nfs_pgio_header *hdr, 361 const struct rpc_call_ops *call_ops) 362 { 363 hdr->task.tk_ops = call_ops; 364 if (!hdr->task.tk_start) 365 hdr->task.tk_start = ktime_get(); 366 } 367 368 static void 369 nfs_local_pgio_done(struct nfs_pgio_header *hdr, long status) 370 { 371 if (status >= 0) { 372 hdr->res.count = status; 373 hdr->res.op_status = NFS4_OK; 374 hdr->task.tk_status = 0; 375 } else { 376 hdr->res.op_status = nfs_localio_errno_to_nfs4_stat(status); 377 hdr->task.tk_status = status; 378 } 379 } 380 381 static void 382 nfs_local_pgio_release(struct nfs_local_kiocb *iocb) 383 { 384 struct nfs_pgio_header *hdr = iocb->hdr; 385 386 nfs_local_file_put(iocb->localio); 387 nfs_local_iocb_free(iocb); 388 nfs_local_hdr_release(hdr, hdr->task.tk_ops); 389 } 390 391 /* 392 * Complete the I/O from iocb->kiocb.ki_complete() 393 * 394 * Note that this function can be called from a bottom half context, 395 * hence we need to queue the rpc_call_done() etc to a workqueue 396 */ 397 static inline void nfs_local_pgio_aio_complete(struct nfs_local_kiocb *iocb) 398 { 399 INIT_WORK(&iocb->work, iocb->aio_complete_work); 400 queue_work(nfsiod_workqueue, &iocb->work); 401 } 402 403 static void 404 nfs_local_read_done(struct nfs_local_kiocb *iocb, long status) 405 { 406 struct nfs_pgio_header *hdr = iocb->hdr; 407 struct file *filp = iocb->kiocb.ki_filp; 408 409 nfs_local_pgio_done(hdr, status); 410 411 /* 412 * Must clear replen otherwise NFSv3 data corruption will occur 413 * if/when switching from LOCALIO back to using normal RPC. 414 */ 415 hdr->res.replen = 0; 416 417 if (hdr->res.count != hdr->args.count || 418 hdr->args.offset + hdr->res.count >= i_size_read(file_inode(filp))) 419 hdr->res.eof = true; 420 421 dprintk("%s: read %ld bytes eof %d.\n", __func__, 422 status > 0 ? status : 0, hdr->res.eof); 423 } 424 425 static void nfs_local_read_aio_complete_work(struct work_struct *work) 426 { 427 struct nfs_local_kiocb *iocb = 428 container_of(work, struct nfs_local_kiocb, work); 429 430 nfs_local_pgio_release(iocb); 431 } 432 433 static void nfs_local_read_aio_complete(struct kiocb *kiocb, long ret) 434 { 435 struct nfs_local_kiocb *iocb = 436 container_of(kiocb, struct nfs_local_kiocb, kiocb); 437 438 nfs_local_read_done(iocb, ret); 439 nfs_local_pgio_aio_complete(iocb); /* Calls nfs_local_read_aio_complete_work */ 440 } 441 442 static void nfs_local_call_read(struct work_struct *work) 443 { 444 struct nfs_local_kiocb *iocb = 445 container_of(work, struct nfs_local_kiocb, work); 446 struct file *filp = iocb->kiocb.ki_filp; 447 const struct cred *save_cred; 448 struct iov_iter iter; 449 ssize_t status; 450 451 save_cred = override_creds(filp->f_cred); 452 453 nfs_local_iter_init(&iter, iocb, READ); 454 455 status = filp->f_op->read_iter(&iocb->kiocb, &iter); 456 if (status != -EIOCBQUEUED) { 457 nfs_local_read_done(iocb, status); 458 nfs_local_pgio_release(iocb); 459 } 460 461 revert_creds(save_cred); 462 } 463 464 static int 465 nfs_do_local_read(struct nfs_pgio_header *hdr, 466 struct nfsd_file *localio, 467 const struct rpc_call_ops *call_ops) 468 { 469 struct nfs_local_kiocb *iocb; 470 struct file *file = nfs_to->nfsd_file_file(localio); 471 472 /* Don't support filesystems without read_iter */ 473 if (!file->f_op->read_iter) 474 return -EAGAIN; 475 476 dprintk("%s: vfs_read count=%u pos=%llu\n", 477 __func__, hdr->args.count, hdr->args.offset); 478 479 iocb = nfs_local_iocb_alloc(hdr, file, GFP_KERNEL); 480 if (iocb == NULL) 481 return -ENOMEM; 482 iocb->localio = localio; 483 484 nfs_local_pgio_init(hdr, call_ops); 485 hdr->res.eof = false; 486 487 if (iocb->kiocb.ki_flags & IOCB_DIRECT) { 488 iocb->kiocb.ki_complete = nfs_local_read_aio_complete; 489 iocb->aio_complete_work = nfs_local_read_aio_complete_work; 490 } 491 492 INIT_WORK(&iocb->work, nfs_local_call_read); 493 queue_work(nfslocaliod_workqueue, &iocb->work); 494 495 return 0; 496 } 497 498 static void 499 nfs_copy_boot_verifier(struct nfs_write_verifier *verifier, struct inode *inode) 500 { 501 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 502 u32 *verf = (u32 *)verifier->data; 503 int seq = 0; 504 505 do { 506 read_seqbegin_or_lock(&clp->cl_boot_lock, &seq); 507 verf[0] = (u32)clp->cl_nfssvc_boot.tv_sec; 508 verf[1] = (u32)clp->cl_nfssvc_boot.tv_nsec; 509 } while (need_seqretry(&clp->cl_boot_lock, seq)); 510 done_seqretry(&clp->cl_boot_lock, seq); 511 } 512 513 static void 514 nfs_reset_boot_verifier(struct inode *inode) 515 { 516 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 517 518 write_seqlock(&clp->cl_boot_lock); 519 ktime_get_real_ts64(&clp->cl_nfssvc_boot); 520 write_sequnlock(&clp->cl_boot_lock); 521 } 522 523 static void 524 nfs_set_local_verifier(struct inode *inode, 525 struct nfs_writeverf *verf, 526 enum nfs3_stable_how how) 527 { 528 nfs_copy_boot_verifier(&verf->verifier, inode); 529 verf->committed = how; 530 } 531 532 /* Factored out from fs/nfsd/vfs.h:fh_getattr() */ 533 static int __vfs_getattr(struct path *p, struct kstat *stat, int version) 534 { 535 u32 request_mask = STATX_BASIC_STATS; 536 537 if (version == 4) 538 request_mask |= (STATX_BTIME | STATX_CHANGE_COOKIE); 539 return vfs_getattr(p, stat, request_mask, AT_STATX_SYNC_AS_STAT); 540 } 541 542 /* Copied from fs/nfsd/nfsfh.c:nfsd4_change_attribute() */ 543 static u64 __nfsd4_change_attribute(const struct kstat *stat, 544 const struct inode *inode) 545 { 546 u64 chattr; 547 548 if (stat->result_mask & STATX_CHANGE_COOKIE) { 549 chattr = stat->change_cookie; 550 if (S_ISREG(inode->i_mode) && 551 !(stat->attributes & STATX_ATTR_CHANGE_MONOTONIC)) { 552 chattr += (u64)stat->ctime.tv_sec << 30; 553 chattr += stat->ctime.tv_nsec; 554 } 555 } else { 556 chattr = time_to_chattr(&stat->ctime); 557 } 558 return chattr; 559 } 560 561 static void nfs_local_vfs_getattr(struct nfs_local_kiocb *iocb) 562 { 563 struct kstat stat; 564 struct file *filp = iocb->kiocb.ki_filp; 565 struct nfs_pgio_header *hdr = iocb->hdr; 566 struct nfs_fattr *fattr = hdr->res.fattr; 567 int version = NFS_PROTO(hdr->inode)->version; 568 569 if (unlikely(!fattr) || __vfs_getattr(&filp->f_path, &stat, version)) 570 return; 571 572 fattr->valid = (NFS_ATTR_FATTR_FILEID | 573 NFS_ATTR_FATTR_CHANGE | 574 NFS_ATTR_FATTR_SIZE | 575 NFS_ATTR_FATTR_ATIME | 576 NFS_ATTR_FATTR_MTIME | 577 NFS_ATTR_FATTR_CTIME | 578 NFS_ATTR_FATTR_SPACE_USED); 579 580 fattr->fileid = stat.ino; 581 fattr->size = stat.size; 582 fattr->atime = stat.atime; 583 fattr->mtime = stat.mtime; 584 fattr->ctime = stat.ctime; 585 if (version == 4) { 586 fattr->change_attr = 587 __nfsd4_change_attribute(&stat, file_inode(filp)); 588 } else 589 fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime); 590 fattr->du.nfs3.used = stat.blocks << 9; 591 } 592 593 static void 594 nfs_local_write_done(struct nfs_local_kiocb *iocb, long status) 595 { 596 struct nfs_pgio_header *hdr = iocb->hdr; 597 struct inode *inode = hdr->inode; 598 599 dprintk("%s: wrote %ld bytes.\n", __func__, status > 0 ? status : 0); 600 601 /* Handle short writes as if they are ENOSPC */ 602 if (status > 0 && status < hdr->args.count) { 603 hdr->mds_offset += status; 604 hdr->args.offset += status; 605 hdr->args.pgbase += status; 606 hdr->args.count -= status; 607 nfs_set_pgio_error(hdr, -ENOSPC, hdr->args.offset); 608 status = -ENOSPC; 609 } 610 if (status < 0) 611 nfs_reset_boot_verifier(inode); 612 613 nfs_local_pgio_done(hdr, status); 614 } 615 616 static void nfs_local_write_aio_complete_work(struct work_struct *work) 617 { 618 struct nfs_local_kiocb *iocb = 619 container_of(work, struct nfs_local_kiocb, work); 620 621 nfs_local_vfs_getattr(iocb); 622 nfs_local_pgio_release(iocb); 623 } 624 625 static void nfs_local_write_aio_complete(struct kiocb *kiocb, long ret) 626 { 627 struct nfs_local_kiocb *iocb = 628 container_of(kiocb, struct nfs_local_kiocb, kiocb); 629 630 nfs_local_write_done(iocb, ret); 631 nfs_local_pgio_aio_complete(iocb); /* Calls nfs_local_write_aio_complete_work */ 632 } 633 634 static void nfs_local_call_write(struct work_struct *work) 635 { 636 struct nfs_local_kiocb *iocb = 637 container_of(work, struct nfs_local_kiocb, work); 638 struct file *filp = iocb->kiocb.ki_filp; 639 unsigned long old_flags = current->flags; 640 const struct cred *save_cred; 641 struct iov_iter iter; 642 ssize_t status; 643 644 current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO; 645 save_cred = override_creds(filp->f_cred); 646 647 nfs_local_iter_init(&iter, iocb, WRITE); 648 649 file_start_write(filp); 650 status = filp->f_op->write_iter(&iocb->kiocb, &iter); 651 file_end_write(filp); 652 if (status != -EIOCBQUEUED) { 653 nfs_local_write_done(iocb, status); 654 nfs_local_vfs_getattr(iocb); 655 nfs_local_pgio_release(iocb); 656 } 657 658 revert_creds(save_cred); 659 current->flags = old_flags; 660 } 661 662 static int 663 nfs_do_local_write(struct nfs_pgio_header *hdr, 664 struct nfsd_file *localio, 665 const struct rpc_call_ops *call_ops) 666 { 667 struct nfs_local_kiocb *iocb; 668 struct file *file = nfs_to->nfsd_file_file(localio); 669 670 /* Don't support filesystems without write_iter */ 671 if (!file->f_op->write_iter) 672 return -EAGAIN; 673 674 dprintk("%s: vfs_write count=%u pos=%llu %s\n", 675 __func__, hdr->args.count, hdr->args.offset, 676 (hdr->args.stable == NFS_UNSTABLE) ? "unstable" : "stable"); 677 678 iocb = nfs_local_iocb_alloc(hdr, file, GFP_NOIO); 679 if (iocb == NULL) 680 return -ENOMEM; 681 iocb->localio = localio; 682 683 switch (hdr->args.stable) { 684 default: 685 break; 686 case NFS_DATA_SYNC: 687 iocb->kiocb.ki_flags |= IOCB_DSYNC; 688 break; 689 case NFS_FILE_SYNC: 690 iocb->kiocb.ki_flags |= IOCB_DSYNC|IOCB_SYNC; 691 } 692 693 nfs_local_pgio_init(hdr, call_ops); 694 695 nfs_set_local_verifier(hdr->inode, hdr->res.verf, hdr->args.stable); 696 697 if (iocb->kiocb.ki_flags & IOCB_DIRECT) { 698 iocb->kiocb.ki_complete = nfs_local_write_aio_complete; 699 iocb->aio_complete_work = nfs_local_write_aio_complete_work; 700 } 701 702 INIT_WORK(&iocb->work, nfs_local_call_write); 703 queue_work(nfslocaliod_workqueue, &iocb->work); 704 705 return 0; 706 } 707 708 int nfs_local_doio(struct nfs_client *clp, struct nfsd_file *localio, 709 struct nfs_pgio_header *hdr, 710 const struct rpc_call_ops *call_ops) 711 { 712 int status = 0; 713 714 if (!hdr->args.count) 715 return 0; 716 717 switch (hdr->rw_mode) { 718 case FMODE_READ: 719 status = nfs_do_local_read(hdr, localio, call_ops); 720 break; 721 case FMODE_WRITE: 722 status = nfs_do_local_write(hdr, localio, call_ops); 723 break; 724 default: 725 dprintk("%s: invalid mode: %d\n", __func__, 726 hdr->rw_mode); 727 status = -EINVAL; 728 } 729 730 if (status != 0) { 731 if (status == -EAGAIN) 732 nfs_localio_disable_client(clp); 733 nfs_local_file_put(localio); 734 hdr->task.tk_status = status; 735 nfs_local_hdr_release(hdr, call_ops); 736 } 737 return status; 738 } 739 740 static void 741 nfs_local_init_commit(struct nfs_commit_data *data, 742 const struct rpc_call_ops *call_ops) 743 { 744 data->task.tk_ops = call_ops; 745 } 746 747 static int 748 nfs_local_run_commit(struct file *filp, struct nfs_commit_data *data) 749 { 750 loff_t start = data->args.offset; 751 loff_t end = LLONG_MAX; 752 753 if (data->args.count > 0) { 754 end = start + data->args.count - 1; 755 if (end < start) 756 end = LLONG_MAX; 757 } 758 759 dprintk("%s: commit %llu - %llu\n", __func__, start, end); 760 return vfs_fsync_range(filp, start, end, 0); 761 } 762 763 static void 764 nfs_local_commit_done(struct nfs_commit_data *data, int status) 765 { 766 if (status >= 0) { 767 nfs_set_local_verifier(data->inode, 768 data->res.verf, 769 NFS_FILE_SYNC); 770 data->res.op_status = NFS4_OK; 771 data->task.tk_status = 0; 772 } else { 773 nfs_reset_boot_verifier(data->inode); 774 data->res.op_status = nfs_localio_errno_to_nfs4_stat(status); 775 data->task.tk_status = status; 776 } 777 } 778 779 static void 780 nfs_local_release_commit_data(struct nfsd_file *localio, 781 struct nfs_commit_data *data, 782 const struct rpc_call_ops *call_ops) 783 { 784 nfs_local_file_put(localio); 785 call_ops->rpc_call_done(&data->task, data); 786 call_ops->rpc_release(data); 787 } 788 789 static void 790 nfs_local_fsync_ctx_free(struct nfs_local_fsync_ctx *ctx) 791 { 792 nfs_local_release_commit_data(ctx->localio, ctx->data, 793 ctx->data->task.tk_ops); 794 kfree(ctx); 795 } 796 797 static void 798 nfs_local_fsync_work(struct work_struct *work) 799 { 800 struct nfs_local_fsync_ctx *ctx; 801 int status; 802 803 ctx = container_of(work, struct nfs_local_fsync_ctx, work); 804 805 status = nfs_local_run_commit(nfs_to->nfsd_file_file(ctx->localio), 806 ctx->data); 807 nfs_local_commit_done(ctx->data, status); 808 if (ctx->done != NULL) 809 complete(ctx->done); 810 nfs_local_fsync_ctx_free(ctx); 811 } 812 813 static struct nfs_local_fsync_ctx * 814 nfs_local_fsync_ctx_alloc(struct nfs_commit_data *data, 815 struct nfsd_file *localio, gfp_t flags) 816 { 817 struct nfs_local_fsync_ctx *ctx = kmalloc(sizeof(*ctx), flags); 818 819 if (ctx != NULL) { 820 ctx->localio = localio; 821 ctx->data = data; 822 INIT_WORK(&ctx->work, nfs_local_fsync_work); 823 ctx->done = NULL; 824 } 825 return ctx; 826 } 827 828 int nfs_local_commit(struct nfsd_file *localio, 829 struct nfs_commit_data *data, 830 const struct rpc_call_ops *call_ops, int how) 831 { 832 struct nfs_local_fsync_ctx *ctx; 833 834 ctx = nfs_local_fsync_ctx_alloc(data, localio, GFP_KERNEL); 835 if (!ctx) { 836 nfs_local_commit_done(data, -ENOMEM); 837 nfs_local_release_commit_data(localio, data, call_ops); 838 return -ENOMEM; 839 } 840 841 nfs_local_init_commit(data, call_ops); 842 843 if (how & FLUSH_SYNC) { 844 DECLARE_COMPLETION_ONSTACK(done); 845 ctx->done = &done; 846 queue_work(nfsiod_workqueue, &ctx->work); 847 wait_for_completion(&done); 848 } else 849 queue_work(nfsiod_workqueue, &ctx->work); 850 851 return 0; 852 } 853