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