xref: /linux/fs/nfs/nfs42proc.c (revision cc1e6315e83db0e517dd9279050b88adc83a7eba)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
4  */
5 #include <linux/fs.h>
6 #include <linux/sunrpc/sched.h>
7 #include <linux/nfs.h>
8 #include <linux/nfs3.h>
9 #include <linux/nfs4.h>
10 #include <linux/nfs_xdr.h>
11 #include <linux/nfs_fs.h>
12 #include "nfs4_fs.h"
13 #include "nfs42.h"
14 #include "iostat.h"
15 #include "pnfs.h"
16 #include "nfs4session.h"
17 #include "internal.h"
18 
19 #define NFSDBG_FACILITY NFSDBG_PROC
20 static int nfs42_do_offload_cancel_async(struct file *dst, nfs4_stateid *std);
21 
22 static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
23 		struct nfs_lock_context *lock, loff_t offset, loff_t len)
24 {
25 	struct inode *inode = file_inode(filep);
26 	struct nfs_server *server = NFS_SERVER(inode);
27 	struct nfs42_falloc_args args = {
28 		.falloc_fh	= NFS_FH(inode),
29 		.falloc_offset	= offset,
30 		.falloc_length	= len,
31 		.falloc_bitmask	= server->cache_consistency_bitmask,
32 	};
33 	struct nfs42_falloc_res res = {
34 		.falloc_server	= server,
35 	};
36 	int status;
37 
38 	msg->rpc_argp = &args;
39 	msg->rpc_resp = &res;
40 
41 	status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
42 			lock, FMODE_WRITE);
43 	if (status)
44 		return status;
45 
46 	res.falloc_fattr = nfs_alloc_fattr();
47 	if (!res.falloc_fattr)
48 		return -ENOMEM;
49 
50 	status = nfs4_call_sync(server->client, server, msg,
51 				&args.seq_args, &res.seq_res, 0);
52 	if (status == 0)
53 		status = nfs_post_op_update_inode(inode, res.falloc_fattr);
54 
55 	kfree(res.falloc_fattr);
56 	return status;
57 }
58 
59 static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
60 				loff_t offset, loff_t len)
61 {
62 	struct nfs_server *server = NFS_SERVER(file_inode(filep));
63 	struct nfs4_exception exception = { };
64 	struct nfs_lock_context *lock;
65 	int err;
66 
67 	lock = nfs_get_lock_context(nfs_file_open_context(filep));
68 	if (IS_ERR(lock))
69 		return PTR_ERR(lock);
70 
71 	exception.inode = file_inode(filep);
72 	exception.state = lock->open_context->state;
73 
74 	do {
75 		err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
76 		if (err == -ENOTSUPP) {
77 			err = -EOPNOTSUPP;
78 			break;
79 		}
80 		err = nfs4_handle_exception(server, err, &exception);
81 	} while (exception.retry);
82 
83 	nfs_put_lock_context(lock);
84 	return err;
85 }
86 
87 int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
88 {
89 	struct rpc_message msg = {
90 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
91 	};
92 	struct inode *inode = file_inode(filep);
93 	int err;
94 
95 	if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
96 		return -EOPNOTSUPP;
97 
98 	inode_lock(inode);
99 
100 	err = nfs42_proc_fallocate(&msg, filep, offset, len);
101 	if (err == -EOPNOTSUPP)
102 		NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
103 
104 	inode_unlock(inode);
105 	return err;
106 }
107 
108 int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
109 {
110 	struct rpc_message msg = {
111 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
112 	};
113 	struct inode *inode = file_inode(filep);
114 	int err;
115 
116 	if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
117 		return -EOPNOTSUPP;
118 
119 	inode_lock(inode);
120 	err = nfs_sync_inode(inode);
121 	if (err)
122 		goto out_unlock;
123 
124 	err = nfs42_proc_fallocate(&msg, filep, offset, len);
125 	if (err == 0)
126 		truncate_pagecache_range(inode, offset, (offset + len) -1);
127 	if (err == -EOPNOTSUPP)
128 		NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
129 out_unlock:
130 	inode_unlock(inode);
131 	return err;
132 }
133 
134 static int handle_async_copy(struct nfs42_copy_res *res,
135 			     struct nfs_server *server,
136 			     struct file *src,
137 			     struct file *dst,
138 			     nfs4_stateid *src_stateid)
139 {
140 	struct nfs4_copy_state *copy;
141 	int status = NFS4_OK;
142 	bool found_pending = false;
143 	struct nfs_open_context *ctx = nfs_file_open_context(dst);
144 
145 	spin_lock(&server->nfs_client->cl_lock);
146 	list_for_each_entry(copy, &server->nfs_client->pending_cb_stateids,
147 				copies) {
148 		if (memcmp(&res->write_res.stateid, &copy->stateid,
149 				NFS4_STATEID_SIZE))
150 			continue;
151 		found_pending = true;
152 		list_del(&copy->copies);
153 		break;
154 	}
155 	if (found_pending) {
156 		spin_unlock(&server->nfs_client->cl_lock);
157 		goto out;
158 	}
159 
160 	copy = kzalloc(sizeof(struct nfs4_copy_state), GFP_NOFS);
161 	if (!copy) {
162 		spin_unlock(&server->nfs_client->cl_lock);
163 		return -ENOMEM;
164 	}
165 	memcpy(&copy->stateid, &res->write_res.stateid, NFS4_STATEID_SIZE);
166 	init_completion(&copy->completion);
167 	copy->parent_state = ctx->state;
168 
169 	list_add_tail(&copy->copies, &server->ss_copies);
170 	spin_unlock(&server->nfs_client->cl_lock);
171 
172 	status = wait_for_completion_interruptible(&copy->completion);
173 	spin_lock(&server->nfs_client->cl_lock);
174 	list_del_init(&copy->copies);
175 	spin_unlock(&server->nfs_client->cl_lock);
176 	if (status == -ERESTARTSYS) {
177 		goto out_cancel;
178 	} else if (copy->flags) {
179 		status = -EAGAIN;
180 		goto out_cancel;
181 	}
182 out:
183 	res->write_res.count = copy->count;
184 	memcpy(&res->write_res.verifier, &copy->verf, sizeof(copy->verf));
185 	status = -copy->error;
186 
187 	kfree(copy);
188 	return status;
189 out_cancel:
190 	nfs42_do_offload_cancel_async(dst, &copy->stateid);
191 	kfree(copy);
192 	return status;
193 }
194 
195 static int process_copy_commit(struct file *dst, loff_t pos_dst,
196 			       struct nfs42_copy_res *res)
197 {
198 	struct nfs_commitres cres;
199 	int status = -ENOMEM;
200 
201 	cres.verf = kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS);
202 	if (!cres.verf)
203 		goto out;
204 
205 	status = nfs4_proc_commit(dst, pos_dst, res->write_res.count, &cres);
206 	if (status)
207 		goto out_free;
208 	if (nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
209 				    &cres.verf->verifier)) {
210 		dprintk("commit verf differs from copy verf\n");
211 		status = -EAGAIN;
212 	}
213 out_free:
214 	kfree(cres.verf);
215 out:
216 	return status;
217 }
218 
219 static ssize_t _nfs42_proc_copy(struct file *src,
220 				struct nfs_lock_context *src_lock,
221 				struct file *dst,
222 				struct nfs_lock_context *dst_lock,
223 				struct nfs42_copy_args *args,
224 				struct nfs42_copy_res *res)
225 {
226 	struct rpc_message msg = {
227 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
228 		.rpc_argp = args,
229 		.rpc_resp = res,
230 	};
231 	struct inode *dst_inode = file_inode(dst);
232 	struct nfs_server *server = NFS_SERVER(dst_inode);
233 	loff_t pos_src = args->src_pos;
234 	loff_t pos_dst = args->dst_pos;
235 	size_t count = args->count;
236 	ssize_t status;
237 
238 	status = nfs4_set_rw_stateid(&args->src_stateid, src_lock->open_context,
239 				     src_lock, FMODE_READ);
240 	if (status)
241 		return status;
242 
243 	status = nfs_filemap_write_and_wait_range(file_inode(src)->i_mapping,
244 			pos_src, pos_src + (loff_t)count - 1);
245 	if (status)
246 		return status;
247 
248 	status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context,
249 				     dst_lock, FMODE_WRITE);
250 	if (status)
251 		return status;
252 
253 	status = nfs_sync_inode(dst_inode);
254 	if (status)
255 		return status;
256 
257 	res->commit_res.verf = NULL;
258 	if (args->sync) {
259 		res->commit_res.verf =
260 			kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS);
261 		if (!res->commit_res.verf)
262 			return -ENOMEM;
263 	}
264 	set_bit(NFS_CLNT_DST_SSC_COPY_STATE,
265 		&dst_lock->open_context->state->flags);
266 
267 	status = nfs4_call_sync(server->client, server, &msg,
268 				&args->seq_args, &res->seq_res, 0);
269 	if (status == -ENOTSUPP)
270 		server->caps &= ~NFS_CAP_COPY;
271 	if (status)
272 		goto out;
273 
274 	if (args->sync &&
275 		nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
276 				    &res->commit_res.verf->verifier)) {
277 		status = -EAGAIN;
278 		goto out;
279 	}
280 
281 	if (!res->synchronous) {
282 		status = handle_async_copy(res, server, src, dst,
283 				&args->src_stateid);
284 		if (status)
285 			return status;
286 	}
287 
288 	if ((!res->synchronous || !args->sync) &&
289 			res->write_res.verifier.committed != NFS_FILE_SYNC) {
290 		status = process_copy_commit(dst, pos_dst, res);
291 		if (status)
292 			return status;
293 	}
294 
295 	truncate_pagecache_range(dst_inode, pos_dst,
296 				 pos_dst + res->write_res.count);
297 
298 	status = res->write_res.count;
299 out:
300 	if (args->sync)
301 		kfree(res->commit_res.verf);
302 	return status;
303 }
304 
305 ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
306 			struct file *dst, loff_t pos_dst,
307 			size_t count)
308 {
309 	struct nfs_server *server = NFS_SERVER(file_inode(dst));
310 	struct nfs_lock_context *src_lock;
311 	struct nfs_lock_context *dst_lock;
312 	struct nfs42_copy_args args = {
313 		.src_fh		= NFS_FH(file_inode(src)),
314 		.src_pos	= pos_src,
315 		.dst_fh		= NFS_FH(file_inode(dst)),
316 		.dst_pos	= pos_dst,
317 		.count		= count,
318 		.sync		= false,
319 	};
320 	struct nfs42_copy_res res;
321 	struct nfs4_exception src_exception = {
322 		.inode		= file_inode(src),
323 		.stateid	= &args.src_stateid,
324 	};
325 	struct nfs4_exception dst_exception = {
326 		.inode		= file_inode(dst),
327 		.stateid	= &args.dst_stateid,
328 	};
329 	ssize_t err, err2;
330 
331 	if (!nfs_server_capable(file_inode(dst), NFS_CAP_COPY))
332 		return -EOPNOTSUPP;
333 
334 	src_lock = nfs_get_lock_context(nfs_file_open_context(src));
335 	if (IS_ERR(src_lock))
336 		return PTR_ERR(src_lock);
337 
338 	src_exception.state = src_lock->open_context->state;
339 
340 	dst_lock = nfs_get_lock_context(nfs_file_open_context(dst));
341 	if (IS_ERR(dst_lock)) {
342 		err = PTR_ERR(dst_lock);
343 		goto out_put_src_lock;
344 	}
345 
346 	dst_exception.state = dst_lock->open_context->state;
347 
348 	do {
349 		inode_lock(file_inode(dst));
350 		err = _nfs42_proc_copy(src, src_lock,
351 				dst, dst_lock,
352 				&args, &res);
353 		inode_unlock(file_inode(dst));
354 
355 		if (err >= 0)
356 			break;
357 		if (err == -ENOTSUPP) {
358 			err = -EOPNOTSUPP;
359 			break;
360 		} else if (err == -EAGAIN) {
361 			dst_exception.retry = 1;
362 			continue;
363 		} else if (err == -NFS4ERR_OFFLOAD_NO_REQS && !args.sync) {
364 			args.sync = true;
365 			dst_exception.retry = 1;
366 			continue;
367 		}
368 
369 		err2 = nfs4_handle_exception(server, err, &src_exception);
370 		err  = nfs4_handle_exception(server, err, &dst_exception);
371 		if (!err)
372 			err = err2;
373 	} while (src_exception.retry || dst_exception.retry);
374 
375 	nfs_put_lock_context(dst_lock);
376 out_put_src_lock:
377 	nfs_put_lock_context(src_lock);
378 	return err;
379 }
380 
381 struct nfs42_offloadcancel_data {
382 	struct nfs_server *seq_server;
383 	struct nfs42_offload_status_args args;
384 	struct nfs42_offload_status_res res;
385 };
386 
387 static void nfs42_offload_cancel_prepare(struct rpc_task *task, void *calldata)
388 {
389 	struct nfs42_offloadcancel_data *data = calldata;
390 
391 	nfs4_setup_sequence(data->seq_server->nfs_client,
392 				&data->args.osa_seq_args,
393 				&data->res.osr_seq_res, task);
394 }
395 
396 static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata)
397 {
398 	struct nfs42_offloadcancel_data *data = calldata;
399 
400 	nfs41_sequence_done(task, &data->res.osr_seq_res);
401 	if (task->tk_status &&
402 		nfs4_async_handle_error(task, data->seq_server, NULL,
403 			NULL) == -EAGAIN)
404 		rpc_restart_call_prepare(task);
405 }
406 
407 static void nfs42_free_offloadcancel_data(void *data)
408 {
409 	kfree(data);
410 }
411 
412 static const struct rpc_call_ops nfs42_offload_cancel_ops = {
413 	.rpc_call_prepare = nfs42_offload_cancel_prepare,
414 	.rpc_call_done = nfs42_offload_cancel_done,
415 	.rpc_release = nfs42_free_offloadcancel_data,
416 };
417 
418 static int nfs42_do_offload_cancel_async(struct file *dst,
419 					 nfs4_stateid *stateid)
420 {
421 	struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
422 	struct nfs42_offloadcancel_data *data = NULL;
423 	struct nfs_open_context *ctx = nfs_file_open_context(dst);
424 	struct rpc_task *task;
425 	struct rpc_message msg = {
426 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_CANCEL],
427 		.rpc_cred = ctx->cred,
428 	};
429 	struct rpc_task_setup task_setup_data = {
430 		.rpc_client = dst_server->client,
431 		.rpc_message = &msg,
432 		.callback_ops = &nfs42_offload_cancel_ops,
433 		.workqueue = nfsiod_workqueue,
434 		.flags = RPC_TASK_ASYNC,
435 	};
436 	int status;
437 
438 	if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL))
439 		return -EOPNOTSUPP;
440 
441 	data = kzalloc(sizeof(struct nfs42_offloadcancel_data), GFP_NOFS);
442 	if (data == NULL)
443 		return -ENOMEM;
444 
445 	data->seq_server = dst_server;
446 	data->args.osa_src_fh = NFS_FH(file_inode(dst));
447 	memcpy(&data->args.osa_stateid, stateid,
448 		sizeof(data->args.osa_stateid));
449 	msg.rpc_argp = &data->args;
450 	msg.rpc_resp = &data->res;
451 	task_setup_data.callback_data = data;
452 	nfs4_init_sequence(&data->args.osa_seq_args, &data->res.osr_seq_res,
453 			   1, 0);
454 	task = rpc_run_task(&task_setup_data);
455 	if (IS_ERR(task))
456 		return PTR_ERR(task);
457 	status = rpc_wait_for_completion_task(task);
458 	if (status == -ENOTSUPP)
459 		dst_server->caps &= ~NFS_CAP_OFFLOAD_CANCEL;
460 	rpc_put_task(task);
461 	return status;
462 }
463 
464 static loff_t _nfs42_proc_llseek(struct file *filep,
465 		struct nfs_lock_context *lock, loff_t offset, int whence)
466 {
467 	struct inode *inode = file_inode(filep);
468 	struct nfs42_seek_args args = {
469 		.sa_fh		= NFS_FH(inode),
470 		.sa_offset	= offset,
471 		.sa_what	= (whence == SEEK_HOLE) ?
472 					NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
473 	};
474 	struct nfs42_seek_res res;
475 	struct rpc_message msg = {
476 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
477 		.rpc_argp = &args,
478 		.rpc_resp = &res,
479 	};
480 	struct nfs_server *server = NFS_SERVER(inode);
481 	int status;
482 
483 	if (!nfs_server_capable(inode, NFS_CAP_SEEK))
484 		return -ENOTSUPP;
485 
486 	status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
487 			lock, FMODE_READ);
488 	if (status)
489 		return status;
490 
491 	status = nfs_filemap_write_and_wait_range(inode->i_mapping,
492 			offset, LLONG_MAX);
493 	if (status)
494 		return status;
495 
496 	status = nfs4_call_sync(server->client, server, &msg,
497 				&args.seq_args, &res.seq_res, 0);
498 	if (status == -ENOTSUPP)
499 		server->caps &= ~NFS_CAP_SEEK;
500 	if (status)
501 		return status;
502 
503 	return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
504 }
505 
506 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
507 {
508 	struct nfs_server *server = NFS_SERVER(file_inode(filep));
509 	struct nfs4_exception exception = { };
510 	struct nfs_lock_context *lock;
511 	loff_t err;
512 
513 	lock = nfs_get_lock_context(nfs_file_open_context(filep));
514 	if (IS_ERR(lock))
515 		return PTR_ERR(lock);
516 
517 	exception.inode = file_inode(filep);
518 	exception.state = lock->open_context->state;
519 
520 	do {
521 		err = _nfs42_proc_llseek(filep, lock, offset, whence);
522 		if (err >= 0)
523 			break;
524 		if (err == -ENOTSUPP) {
525 			err = -EOPNOTSUPP;
526 			break;
527 		}
528 		err = nfs4_handle_exception(server, err, &exception);
529 	} while (exception.retry);
530 
531 	nfs_put_lock_context(lock);
532 	return err;
533 }
534 
535 
536 static void
537 nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
538 {
539 	struct nfs42_layoutstat_data *data = calldata;
540 	struct inode *inode = data->inode;
541 	struct nfs_server *server = NFS_SERVER(inode);
542 	struct pnfs_layout_hdr *lo;
543 
544 	spin_lock(&inode->i_lock);
545 	lo = NFS_I(inode)->layout;
546 	if (!pnfs_layout_is_valid(lo)) {
547 		spin_unlock(&inode->i_lock);
548 		rpc_exit(task, 0);
549 		return;
550 	}
551 	nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
552 	spin_unlock(&inode->i_lock);
553 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
554 			    &data->res.seq_res, task);
555 }
556 
557 static void
558 nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
559 {
560 	struct nfs42_layoutstat_data *data = calldata;
561 	struct inode *inode = data->inode;
562 	struct pnfs_layout_hdr *lo;
563 
564 	if (!nfs4_sequence_done(task, &data->res.seq_res))
565 		return;
566 
567 	switch (task->tk_status) {
568 	case 0:
569 		break;
570 	case -NFS4ERR_BADHANDLE:
571 	case -ESTALE:
572 		pnfs_destroy_layout(NFS_I(inode));
573 		break;
574 	case -NFS4ERR_EXPIRED:
575 	case -NFS4ERR_ADMIN_REVOKED:
576 	case -NFS4ERR_DELEG_REVOKED:
577 	case -NFS4ERR_STALE_STATEID:
578 	case -NFS4ERR_BAD_STATEID:
579 		spin_lock(&inode->i_lock);
580 		lo = NFS_I(inode)->layout;
581 		if (pnfs_layout_is_valid(lo) &&
582 		    nfs4_stateid_match(&data->args.stateid,
583 					     &lo->plh_stateid)) {
584 			LIST_HEAD(head);
585 
586 			/*
587 			 * Mark the bad layout state as invalid, then retry
588 			 * with the current stateid.
589 			 */
590 			pnfs_mark_layout_stateid_invalid(lo, &head);
591 			spin_unlock(&inode->i_lock);
592 			pnfs_free_lseg_list(&head);
593 			nfs_commit_inode(inode, 0);
594 		} else
595 			spin_unlock(&inode->i_lock);
596 		break;
597 	case -NFS4ERR_OLD_STATEID:
598 		spin_lock(&inode->i_lock);
599 		lo = NFS_I(inode)->layout;
600 		if (pnfs_layout_is_valid(lo) &&
601 		    nfs4_stateid_match_other(&data->args.stateid,
602 					&lo->plh_stateid)) {
603 			/* Do we need to delay before resending? */
604 			if (!nfs4_stateid_is_newer(&lo->plh_stateid,
605 						&data->args.stateid))
606 				rpc_delay(task, HZ);
607 			rpc_restart_call_prepare(task);
608 		}
609 		spin_unlock(&inode->i_lock);
610 		break;
611 	case -ENOTSUPP:
612 	case -EOPNOTSUPP:
613 		NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
614 	}
615 }
616 
617 static void
618 nfs42_layoutstat_release(void *calldata)
619 {
620 	struct nfs42_layoutstat_data *data = calldata;
621 	struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo;
622 	int i;
623 
624 	for (i = 0; i < data->args.num_dev; i++) {
625 		if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free)
626 			devinfo[i].ld_private.ops->free(&devinfo[i].ld_private);
627 	}
628 
629 	pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
630 	smp_mb__before_atomic();
631 	clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
632 	smp_mb__after_atomic();
633 	nfs_iput_and_deactive(data->inode);
634 	kfree(data->args.devinfo);
635 	kfree(data);
636 }
637 
638 static const struct rpc_call_ops nfs42_layoutstat_ops = {
639 	.rpc_call_prepare = nfs42_layoutstat_prepare,
640 	.rpc_call_done = nfs42_layoutstat_done,
641 	.rpc_release = nfs42_layoutstat_release,
642 };
643 
644 int nfs42_proc_layoutstats_generic(struct nfs_server *server,
645 				   struct nfs42_layoutstat_data *data)
646 {
647 	struct rpc_message msg = {
648 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
649 		.rpc_argp = &data->args,
650 		.rpc_resp = &data->res,
651 	};
652 	struct rpc_task_setup task_setup = {
653 		.rpc_client = server->client,
654 		.rpc_message = &msg,
655 		.callback_ops = &nfs42_layoutstat_ops,
656 		.callback_data = data,
657 		.flags = RPC_TASK_ASYNC,
658 	};
659 	struct rpc_task *task;
660 
661 	data->inode = nfs_igrab_and_active(data->args.inode);
662 	if (!data->inode) {
663 		nfs42_layoutstat_release(data);
664 		return -EAGAIN;
665 	}
666 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
667 	task = rpc_run_task(&task_setup);
668 	if (IS_ERR(task))
669 		return PTR_ERR(task);
670 	rpc_put_task(task);
671 	return 0;
672 }
673 
674 static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
675 		struct file *dst_f, struct nfs_lock_context *src_lock,
676 		struct nfs_lock_context *dst_lock, loff_t src_offset,
677 		loff_t dst_offset, loff_t count)
678 {
679 	struct inode *src_inode = file_inode(src_f);
680 	struct inode *dst_inode = file_inode(dst_f);
681 	struct nfs_server *server = NFS_SERVER(dst_inode);
682 	struct nfs42_clone_args args = {
683 		.src_fh = NFS_FH(src_inode),
684 		.dst_fh = NFS_FH(dst_inode),
685 		.src_offset = src_offset,
686 		.dst_offset = dst_offset,
687 		.count = count,
688 		.dst_bitmask = server->cache_consistency_bitmask,
689 	};
690 	struct nfs42_clone_res res = {
691 		.server	= server,
692 	};
693 	int status;
694 
695 	msg->rpc_argp = &args;
696 	msg->rpc_resp = &res;
697 
698 	status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
699 			src_lock, FMODE_READ);
700 	if (status)
701 		return status;
702 
703 	status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
704 			dst_lock, FMODE_WRITE);
705 	if (status)
706 		return status;
707 
708 	res.dst_fattr = nfs_alloc_fattr();
709 	if (!res.dst_fattr)
710 		return -ENOMEM;
711 
712 	status = nfs4_call_sync(server->client, server, msg,
713 				&args.seq_args, &res.seq_res, 0);
714 	if (status == 0)
715 		status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
716 
717 	kfree(res.dst_fattr);
718 	return status;
719 }
720 
721 int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
722 		     loff_t src_offset, loff_t dst_offset, loff_t count)
723 {
724 	struct rpc_message msg = {
725 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
726 	};
727 	struct inode *inode = file_inode(src_f);
728 	struct nfs_server *server = NFS_SERVER(file_inode(src_f));
729 	struct nfs_lock_context *src_lock;
730 	struct nfs_lock_context *dst_lock;
731 	struct nfs4_exception src_exception = { };
732 	struct nfs4_exception dst_exception = { };
733 	int err, err2;
734 
735 	if (!nfs_server_capable(inode, NFS_CAP_CLONE))
736 		return -EOPNOTSUPP;
737 
738 	src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
739 	if (IS_ERR(src_lock))
740 		return PTR_ERR(src_lock);
741 
742 	src_exception.inode = file_inode(src_f);
743 	src_exception.state = src_lock->open_context->state;
744 
745 	dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
746 	if (IS_ERR(dst_lock)) {
747 		err = PTR_ERR(dst_lock);
748 		goto out_put_src_lock;
749 	}
750 
751 	dst_exception.inode = file_inode(dst_f);
752 	dst_exception.state = dst_lock->open_context->state;
753 
754 	do {
755 		err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
756 					src_offset, dst_offset, count);
757 		if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
758 			NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
759 			err = -EOPNOTSUPP;
760 			break;
761 		}
762 
763 		err2 = nfs4_handle_exception(server, err, &src_exception);
764 		err = nfs4_handle_exception(server, err, &dst_exception);
765 		if (!err)
766 			err = err2;
767 	} while (src_exception.retry || dst_exception.retry);
768 
769 	nfs_put_lock_context(dst_lock);
770 out_put_src_lock:
771 	nfs_put_lock_context(src_lock);
772 	return err;
773 }
774