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
nfs_client_is_local(const struct nfs_client * clp)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
nfs_server_is_local(const struct nfs_client * clp)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
localio_xdr_enc_uuidargs(struct rpc_rqst * req,struct xdr_stream * xdr,const void * data)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
localio_xdr_dec_uuidres(struct rpc_rqst * req,struct xdr_stream * xdr,void * result)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 */
nfs_init_localioclient(struct nfs_client * clp)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
nfs_server_uuid_is_local(struct nfs_client * clp)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 */
nfs_local_probe(struct nfs_client * clp)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
nfs_local_probe_async_work(struct work_struct * work)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
nfs_local_probe_async(struct nfs_client * clp)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
nfs_local_file_put(struct nfsd_file * localio)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 *
__nfs_local_open_fh(struct nfs_client * clp,const struct cred * cred,struct nfs_fh * fh,struct nfs_file_localio * nfl,struct nfsd_file __rcu ** pnf,const fmode_t mode)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 *
nfs_local_open_fh(struct nfs_client * clp,const struct cred * cred,struct nfs_fh * fh,struct nfs_file_localio * nfl,const fmode_t mode)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 *
nfs_bvec_alloc_and_import_pagevec(struct page ** pagevec,unsigned int npages,gfp_t flags)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
nfs_local_iocb_free(struct nfs_local_kiocb * iocb)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 *
nfs_local_iocb_alloc(struct nfs_pgio_header * hdr,struct file * file,gfp_t flags)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
nfs_local_iter_init(struct iov_iter * i,struct nfs_local_kiocb * iocb,int dir)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
nfs_local_hdr_release(struct nfs_pgio_header * hdr,const struct rpc_call_ops * call_ops)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
nfs_local_pgio_init(struct nfs_pgio_header * hdr,const struct rpc_call_ops * call_ops)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
nfs_local_pgio_done(struct nfs_pgio_header * hdr,long status)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
nfs_local_pgio_release(struct nfs_local_kiocb * iocb)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 */
nfs_local_pgio_aio_complete(struct nfs_local_kiocb * iocb)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
nfs_local_read_done(struct nfs_local_kiocb * iocb,long status)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
nfs_local_read_aio_complete_work(struct work_struct * work)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
nfs_local_read_aio_complete(struct kiocb * kiocb,long ret)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
nfs_local_call_read(struct work_struct * work)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
nfs_do_local_read(struct nfs_pgio_header * hdr,struct nfsd_file * localio,const struct rpc_call_ops * call_ops)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
nfs_copy_boot_verifier(struct nfs_write_verifier * verifier,struct inode * inode)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 unsigned int seq;
504
505 do {
506 seq = read_seqbegin(&clp->cl_boot_lock);
507 verf[0] = (u32)clp->cl_nfssvc_boot.tv_sec;
508 verf[1] = (u32)clp->cl_nfssvc_boot.tv_nsec;
509 } while (read_seqretry(&clp->cl_boot_lock, seq));
510 }
511
512 static void
nfs_reset_boot_verifier(struct inode * inode)513 nfs_reset_boot_verifier(struct inode *inode)
514 {
515 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
516
517 write_seqlock(&clp->cl_boot_lock);
518 ktime_get_real_ts64(&clp->cl_nfssvc_boot);
519 write_sequnlock(&clp->cl_boot_lock);
520 }
521
522 static void
nfs_set_local_verifier(struct inode * inode,struct nfs_writeverf * verf,enum nfs3_stable_how how)523 nfs_set_local_verifier(struct inode *inode,
524 struct nfs_writeverf *verf,
525 enum nfs3_stable_how how)
526 {
527 nfs_copy_boot_verifier(&verf->verifier, inode);
528 verf->committed = how;
529 }
530
531 /* Factored out from fs/nfsd/vfs.h:fh_getattr() */
__vfs_getattr(struct path * p,struct kstat * stat,int version)532 static int __vfs_getattr(struct path *p, struct kstat *stat, int version)
533 {
534 u32 request_mask = STATX_BASIC_STATS;
535
536 if (version == 4)
537 request_mask |= (STATX_BTIME | STATX_CHANGE_COOKIE);
538 return vfs_getattr(p, stat, request_mask, AT_STATX_SYNC_AS_STAT);
539 }
540
541 /* Copied from fs/nfsd/nfsfh.c:nfsd4_change_attribute() */
__nfsd4_change_attribute(const struct kstat * stat,const struct inode * inode)542 static u64 __nfsd4_change_attribute(const struct kstat *stat,
543 const struct inode *inode)
544 {
545 u64 chattr;
546
547 if (stat->result_mask & STATX_CHANGE_COOKIE) {
548 chattr = stat->change_cookie;
549 if (S_ISREG(inode->i_mode) &&
550 !(stat->attributes & STATX_ATTR_CHANGE_MONOTONIC)) {
551 chattr += (u64)stat->ctime.tv_sec << 30;
552 chattr += stat->ctime.tv_nsec;
553 }
554 } else {
555 chattr = time_to_chattr(&stat->ctime);
556 }
557 return chattr;
558 }
559
nfs_local_vfs_getattr(struct nfs_local_kiocb * iocb)560 static void nfs_local_vfs_getattr(struct nfs_local_kiocb *iocb)
561 {
562 struct kstat stat;
563 struct file *filp = iocb->kiocb.ki_filp;
564 struct nfs_pgio_header *hdr = iocb->hdr;
565 struct nfs_fattr *fattr = hdr->res.fattr;
566 int version = NFS_PROTO(hdr->inode)->version;
567
568 if (unlikely(!fattr) || __vfs_getattr(&filp->f_path, &stat, version))
569 return;
570
571 fattr->valid = (NFS_ATTR_FATTR_FILEID |
572 NFS_ATTR_FATTR_CHANGE |
573 NFS_ATTR_FATTR_SIZE |
574 NFS_ATTR_FATTR_ATIME |
575 NFS_ATTR_FATTR_MTIME |
576 NFS_ATTR_FATTR_CTIME |
577 NFS_ATTR_FATTR_SPACE_USED);
578
579 fattr->fileid = stat.ino;
580 fattr->size = stat.size;
581 fattr->atime = stat.atime;
582 fattr->mtime = stat.mtime;
583 fattr->ctime = stat.ctime;
584 if (version == 4) {
585 fattr->change_attr =
586 __nfsd4_change_attribute(&stat, file_inode(filp));
587 } else
588 fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime);
589 fattr->du.nfs3.used = stat.blocks << 9;
590 }
591
592 static void
nfs_local_write_done(struct nfs_local_kiocb * iocb,long status)593 nfs_local_write_done(struct nfs_local_kiocb *iocb, long status)
594 {
595 struct nfs_pgio_header *hdr = iocb->hdr;
596 struct inode *inode = hdr->inode;
597
598 dprintk("%s: wrote %ld bytes.\n", __func__, status > 0 ? status : 0);
599
600 /* Handle short writes as if they are ENOSPC */
601 if (status > 0 && status < hdr->args.count) {
602 hdr->mds_offset += status;
603 hdr->args.offset += status;
604 hdr->args.pgbase += status;
605 hdr->args.count -= status;
606 nfs_set_pgio_error(hdr, -ENOSPC, hdr->args.offset);
607 status = -ENOSPC;
608 }
609 if (status < 0)
610 nfs_reset_boot_verifier(inode);
611
612 nfs_local_pgio_done(hdr, status);
613 }
614
nfs_local_write_aio_complete_work(struct work_struct * work)615 static void nfs_local_write_aio_complete_work(struct work_struct *work)
616 {
617 struct nfs_local_kiocb *iocb =
618 container_of(work, struct nfs_local_kiocb, work);
619
620 nfs_local_vfs_getattr(iocb);
621 nfs_local_pgio_release(iocb);
622 }
623
nfs_local_write_aio_complete(struct kiocb * kiocb,long ret)624 static void nfs_local_write_aio_complete(struct kiocb *kiocb, long ret)
625 {
626 struct nfs_local_kiocb *iocb =
627 container_of(kiocb, struct nfs_local_kiocb, kiocb);
628
629 nfs_local_write_done(iocb, ret);
630 nfs_local_pgio_aio_complete(iocb); /* Calls nfs_local_write_aio_complete_work */
631 }
632
nfs_local_call_write(struct work_struct * work)633 static void nfs_local_call_write(struct work_struct *work)
634 {
635 struct nfs_local_kiocb *iocb =
636 container_of(work, struct nfs_local_kiocb, work);
637 struct file *filp = iocb->kiocb.ki_filp;
638 unsigned long old_flags = current->flags;
639 const struct cred *save_cred;
640 struct iov_iter iter;
641 ssize_t status;
642
643 current->flags |= PF_LOCAL_THROTTLE | PF_MEMALLOC_NOIO;
644 save_cred = override_creds(filp->f_cred);
645
646 nfs_local_iter_init(&iter, iocb, WRITE);
647
648 file_start_write(filp);
649 status = filp->f_op->write_iter(&iocb->kiocb, &iter);
650 file_end_write(filp);
651 if (status != -EIOCBQUEUED) {
652 nfs_local_write_done(iocb, status);
653 nfs_local_vfs_getattr(iocb);
654 nfs_local_pgio_release(iocb);
655 }
656
657 revert_creds(save_cred);
658 current->flags = old_flags;
659 }
660
661 static int
nfs_do_local_write(struct nfs_pgio_header * hdr,struct nfsd_file * localio,const struct rpc_call_ops * call_ops)662 nfs_do_local_write(struct nfs_pgio_header *hdr,
663 struct nfsd_file *localio,
664 const struct rpc_call_ops *call_ops)
665 {
666 struct nfs_local_kiocb *iocb;
667 struct file *file = nfs_to->nfsd_file_file(localio);
668
669 /* Don't support filesystems without write_iter */
670 if (!file->f_op->write_iter)
671 return -EAGAIN;
672
673 dprintk("%s: vfs_write count=%u pos=%llu %s\n",
674 __func__, hdr->args.count, hdr->args.offset,
675 (hdr->args.stable == NFS_UNSTABLE) ? "unstable" : "stable");
676
677 iocb = nfs_local_iocb_alloc(hdr, file, GFP_NOIO);
678 if (iocb == NULL)
679 return -ENOMEM;
680 iocb->localio = localio;
681
682 switch (hdr->args.stable) {
683 default:
684 break;
685 case NFS_DATA_SYNC:
686 iocb->kiocb.ki_flags |= IOCB_DSYNC;
687 break;
688 case NFS_FILE_SYNC:
689 iocb->kiocb.ki_flags |= IOCB_DSYNC|IOCB_SYNC;
690 }
691
692 nfs_local_pgio_init(hdr, call_ops);
693
694 nfs_set_local_verifier(hdr->inode, hdr->res.verf, hdr->args.stable);
695
696 if (iocb->kiocb.ki_flags & IOCB_DIRECT) {
697 iocb->kiocb.ki_complete = nfs_local_write_aio_complete;
698 iocb->aio_complete_work = nfs_local_write_aio_complete_work;
699 }
700
701 INIT_WORK(&iocb->work, nfs_local_call_write);
702 queue_work(nfslocaliod_workqueue, &iocb->work);
703
704 return 0;
705 }
706
nfs_local_doio(struct nfs_client * clp,struct nfsd_file * localio,struct nfs_pgio_header * hdr,const struct rpc_call_ops * call_ops)707 int nfs_local_doio(struct nfs_client *clp, struct nfsd_file *localio,
708 struct nfs_pgio_header *hdr,
709 const struct rpc_call_ops *call_ops)
710 {
711 int status = 0;
712
713 if (!hdr->args.count)
714 return 0;
715
716 switch (hdr->rw_mode) {
717 case FMODE_READ:
718 status = nfs_do_local_read(hdr, localio, call_ops);
719 break;
720 case FMODE_WRITE:
721 status = nfs_do_local_write(hdr, localio, call_ops);
722 break;
723 default:
724 dprintk("%s: invalid mode: %d\n", __func__,
725 hdr->rw_mode);
726 status = -EINVAL;
727 }
728
729 if (status != 0) {
730 if (status == -EAGAIN)
731 nfs_localio_disable_client(clp);
732 nfs_local_file_put(localio);
733 hdr->task.tk_status = status;
734 nfs_local_hdr_release(hdr, call_ops);
735 }
736 return status;
737 }
738
739 static void
nfs_local_init_commit(struct nfs_commit_data * data,const struct rpc_call_ops * call_ops)740 nfs_local_init_commit(struct nfs_commit_data *data,
741 const struct rpc_call_ops *call_ops)
742 {
743 data->task.tk_ops = call_ops;
744 }
745
746 static int
nfs_local_run_commit(struct file * filp,struct nfs_commit_data * data)747 nfs_local_run_commit(struct file *filp, struct nfs_commit_data *data)
748 {
749 loff_t start = data->args.offset;
750 loff_t end = LLONG_MAX;
751
752 if (data->args.count > 0) {
753 end = start + data->args.count - 1;
754 if (end < start)
755 end = LLONG_MAX;
756 }
757
758 dprintk("%s: commit %llu - %llu\n", __func__, start, end);
759 return vfs_fsync_range(filp, start, end, 0);
760 }
761
762 static void
nfs_local_commit_done(struct nfs_commit_data * data,int status)763 nfs_local_commit_done(struct nfs_commit_data *data, int status)
764 {
765 if (status >= 0) {
766 nfs_set_local_verifier(data->inode,
767 data->res.verf,
768 NFS_FILE_SYNC);
769 data->res.op_status = NFS4_OK;
770 data->task.tk_status = 0;
771 } else {
772 nfs_reset_boot_verifier(data->inode);
773 data->res.op_status = nfs_localio_errno_to_nfs4_stat(status);
774 data->task.tk_status = status;
775 }
776 }
777
778 static void
nfs_local_release_commit_data(struct nfsd_file * localio,struct nfs_commit_data * data,const struct rpc_call_ops * call_ops)779 nfs_local_release_commit_data(struct nfsd_file *localio,
780 struct nfs_commit_data *data,
781 const struct rpc_call_ops *call_ops)
782 {
783 nfs_local_file_put(localio);
784 call_ops->rpc_call_done(&data->task, data);
785 call_ops->rpc_release(data);
786 }
787
788 static void
nfs_local_fsync_ctx_free(struct nfs_local_fsync_ctx * ctx)789 nfs_local_fsync_ctx_free(struct nfs_local_fsync_ctx *ctx)
790 {
791 nfs_local_release_commit_data(ctx->localio, ctx->data,
792 ctx->data->task.tk_ops);
793 kfree(ctx);
794 }
795
796 static void
nfs_local_fsync_work(struct work_struct * work)797 nfs_local_fsync_work(struct work_struct *work)
798 {
799 struct nfs_local_fsync_ctx *ctx;
800 int status;
801
802 ctx = container_of(work, struct nfs_local_fsync_ctx, work);
803
804 status = nfs_local_run_commit(nfs_to->nfsd_file_file(ctx->localio),
805 ctx->data);
806 nfs_local_commit_done(ctx->data, status);
807 if (ctx->done != NULL)
808 complete(ctx->done);
809 nfs_local_fsync_ctx_free(ctx);
810 }
811
812 static struct nfs_local_fsync_ctx *
nfs_local_fsync_ctx_alloc(struct nfs_commit_data * data,struct nfsd_file * localio,gfp_t flags)813 nfs_local_fsync_ctx_alloc(struct nfs_commit_data *data,
814 struct nfsd_file *localio, gfp_t flags)
815 {
816 struct nfs_local_fsync_ctx *ctx = kmalloc(sizeof(*ctx), flags);
817
818 if (ctx != NULL) {
819 ctx->localio = localio;
820 ctx->data = data;
821 INIT_WORK(&ctx->work, nfs_local_fsync_work);
822 ctx->done = NULL;
823 }
824 return ctx;
825 }
826
nfs_local_commit(struct nfsd_file * localio,struct nfs_commit_data * data,const struct rpc_call_ops * call_ops,int how)827 int nfs_local_commit(struct nfsd_file *localio,
828 struct nfs_commit_data *data,
829 const struct rpc_call_ops *call_ops, int how)
830 {
831 struct nfs_local_fsync_ctx *ctx;
832
833 ctx = nfs_local_fsync_ctx_alloc(data, localio, GFP_KERNEL);
834 if (!ctx) {
835 nfs_local_commit_done(data, -ENOMEM);
836 nfs_local_release_commit_data(localio, data, call_ops);
837 return -ENOMEM;
838 }
839
840 nfs_local_init_commit(data, call_ops);
841
842 if (how & FLUSH_SYNC) {
843 DECLARE_COMPLETION_ONSTACK(done);
844 ctx->done = &done;
845 queue_work(nfsiod_workqueue, &ctx->work);
846 wait_for_completion(&done);
847 } else
848 queue_work(nfsiod_workqueue, &ctx->work);
849
850 return 0;
851 }
852