xref: /openbmc/linux/fs/nfs/nfs42proc.c (revision fa0dadde)
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/addr.h>
7 #include <linux/sunrpc/sched.h>
8 #include <linux/nfs.h>
9 #include <linux/nfs3.h>
10 #include <linux/nfs4.h>
11 #include <linux/nfs_xdr.h>
12 #include <linux/nfs_fs.h>
13 #include "nfs4_fs.h"
14 #include "nfs42.h"
15 #include "iostat.h"
16 #include "pnfs.h"
17 #include "nfs4session.h"
18 #include "internal.h"
19 #include "delegation.h"
20 #include "nfs4trace.h"
21 
22 #define NFSDBG_FACILITY NFSDBG_PROC
23 static int nfs42_do_offload_cancel_async(struct file *dst, nfs4_stateid *std);
24 
25 static void nfs42_set_netaddr(struct file *filep, struct nfs42_netaddr *naddr)
26 {
27 	struct nfs_client *clp = (NFS_SERVER(file_inode(filep)))->nfs_client;
28 	unsigned short port = 2049;
29 
30 	rcu_read_lock();
31 	naddr->netid_len = scnprintf(naddr->netid,
32 					sizeof(naddr->netid), "%s",
33 					rpc_peeraddr2str(clp->cl_rpcclient,
34 					RPC_DISPLAY_NETID));
35 	naddr->addr_len = scnprintf(naddr->addr,
36 					sizeof(naddr->addr),
37 					"%s.%u.%u",
38 					rpc_peeraddr2str(clp->cl_rpcclient,
39 					RPC_DISPLAY_ADDR),
40 					port >> 8, port & 255);
41 	rcu_read_unlock();
42 }
43 
44 static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
45 		struct nfs_lock_context *lock, loff_t offset, loff_t len)
46 {
47 	struct inode *inode = file_inode(filep);
48 	struct nfs_server *server = NFS_SERVER(inode);
49 	u32 bitmask[NFS_BITMASK_SZ];
50 	struct nfs42_falloc_args args = {
51 		.falloc_fh	= NFS_FH(inode),
52 		.falloc_offset	= offset,
53 		.falloc_length	= len,
54 		.falloc_bitmask	= bitmask,
55 	};
56 	struct nfs42_falloc_res res = {
57 		.falloc_server	= server,
58 	};
59 	int status;
60 
61 	msg->rpc_argp = &args;
62 	msg->rpc_resp = &res;
63 
64 	status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
65 			lock, FMODE_WRITE);
66 	if (status) {
67 		if (status == -EAGAIN)
68 			status = -NFS4ERR_BAD_STATEID;
69 		return status;
70 	}
71 
72 	nfs4_bitmask_set(bitmask, server->cache_consistency_bitmask, inode,
73 			 NFS_INO_INVALID_BLOCKS);
74 
75 	res.falloc_fattr = nfs_alloc_fattr();
76 	if (!res.falloc_fattr)
77 		return -ENOMEM;
78 
79 	status = nfs4_call_sync(server->client, server, msg,
80 				&args.seq_args, &res.seq_res, 0);
81 	if (status == 0) {
82 		if (nfs_should_remove_suid(inode)) {
83 			spin_lock(&inode->i_lock);
84 			nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
85 			spin_unlock(&inode->i_lock);
86 		}
87 		status = nfs_post_op_update_inode_force_wcc(inode,
88 							    res.falloc_fattr);
89 	}
90 	if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE])
91 		trace_nfs4_fallocate(inode, &args, status);
92 	else
93 		trace_nfs4_deallocate(inode, &args, status);
94 	kfree(res.falloc_fattr);
95 	return status;
96 }
97 
98 static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
99 				loff_t offset, loff_t len)
100 {
101 	struct inode *inode = file_inode(filep);
102 	struct nfs_server *server = NFS_SERVER(inode);
103 	struct nfs4_exception exception = { };
104 	struct nfs_lock_context *lock;
105 	int err;
106 
107 	lock = nfs_get_lock_context(nfs_file_open_context(filep));
108 	if (IS_ERR(lock))
109 		return PTR_ERR(lock);
110 
111 	exception.inode = inode;
112 	exception.state = lock->open_context->state;
113 
114 	err = nfs_sync_inode(inode);
115 	if (err)
116 		goto out;
117 
118 	do {
119 		err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
120 		if (err == -ENOTSUPP) {
121 			err = -EOPNOTSUPP;
122 			break;
123 		}
124 		err = nfs4_handle_exception(server, err, &exception);
125 	} while (exception.retry);
126 out:
127 	nfs_put_lock_context(lock);
128 	return err;
129 }
130 
131 int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
132 {
133 	struct rpc_message msg = {
134 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
135 	};
136 	struct inode *inode = file_inode(filep);
137 	int err;
138 
139 	if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
140 		return -EOPNOTSUPP;
141 
142 	inode_lock(inode);
143 
144 	err = nfs42_proc_fallocate(&msg, filep, offset, len);
145 	if (err == -EOPNOTSUPP)
146 		NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
147 
148 	inode_unlock(inode);
149 	return err;
150 }
151 
152 int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
153 {
154 	struct rpc_message msg = {
155 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
156 	};
157 	struct inode *inode = file_inode(filep);
158 	int err;
159 
160 	if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
161 		return -EOPNOTSUPP;
162 
163 	inode_lock(inode);
164 
165 	err = nfs42_proc_fallocate(&msg, filep, offset, len);
166 	if (err == 0)
167 		truncate_pagecache_range(inode, offset, (offset + len) -1);
168 	if (err == -EOPNOTSUPP)
169 		NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
170 
171 	inode_unlock(inode);
172 	return err;
173 }
174 
175 static int handle_async_copy(struct nfs42_copy_res *res,
176 			     struct nfs_server *dst_server,
177 			     struct nfs_server *src_server,
178 			     struct file *src,
179 			     struct file *dst,
180 			     nfs4_stateid *src_stateid,
181 			     bool *restart)
182 {
183 	struct nfs4_copy_state *copy, *tmp_copy = NULL, *iter;
184 	int status = NFS4_OK;
185 	struct nfs_open_context *dst_ctx = nfs_file_open_context(dst);
186 	struct nfs_open_context *src_ctx = nfs_file_open_context(src);
187 
188 	copy = kzalloc(sizeof(struct nfs4_copy_state), GFP_KERNEL);
189 	if (!copy)
190 		return -ENOMEM;
191 
192 	spin_lock(&dst_server->nfs_client->cl_lock);
193 	list_for_each_entry(iter,
194 				&dst_server->nfs_client->pending_cb_stateids,
195 				copies) {
196 		if (memcmp(&res->write_res.stateid, &iter->stateid,
197 				NFS4_STATEID_SIZE))
198 			continue;
199 		tmp_copy = iter;
200 		list_del(&iter->copies);
201 		break;
202 	}
203 	if (tmp_copy) {
204 		spin_unlock(&dst_server->nfs_client->cl_lock);
205 		kfree(copy);
206 		copy = tmp_copy;
207 		goto out;
208 	}
209 
210 	memcpy(&copy->stateid, &res->write_res.stateid, NFS4_STATEID_SIZE);
211 	init_completion(&copy->completion);
212 	copy->parent_dst_state = dst_ctx->state;
213 	copy->parent_src_state = src_ctx->state;
214 
215 	list_add_tail(&copy->copies, &dst_server->ss_copies);
216 	spin_unlock(&dst_server->nfs_client->cl_lock);
217 
218 	if (dst_server != src_server) {
219 		spin_lock(&src_server->nfs_client->cl_lock);
220 		list_add_tail(&copy->src_copies, &src_server->ss_copies);
221 		spin_unlock(&src_server->nfs_client->cl_lock);
222 	}
223 
224 	status = wait_for_completion_interruptible(&copy->completion);
225 	spin_lock(&dst_server->nfs_client->cl_lock);
226 	list_del_init(&copy->copies);
227 	spin_unlock(&dst_server->nfs_client->cl_lock);
228 	if (dst_server != src_server) {
229 		spin_lock(&src_server->nfs_client->cl_lock);
230 		list_del_init(&copy->src_copies);
231 		spin_unlock(&src_server->nfs_client->cl_lock);
232 	}
233 	if (status == -ERESTARTSYS) {
234 		goto out_cancel;
235 	} else if (copy->flags || copy->error == NFS4ERR_PARTNER_NO_AUTH) {
236 		status = -EAGAIN;
237 		*restart = true;
238 		goto out_cancel;
239 	}
240 out:
241 	res->write_res.count = copy->count;
242 	memcpy(&res->write_res.verifier, &copy->verf, sizeof(copy->verf));
243 	status = -copy->error;
244 
245 out_free:
246 	kfree(copy);
247 	return status;
248 out_cancel:
249 	nfs42_do_offload_cancel_async(dst, &copy->stateid);
250 	if (!nfs42_files_from_same_server(src, dst))
251 		nfs42_do_offload_cancel_async(src, src_stateid);
252 	goto out_free;
253 }
254 
255 static int process_copy_commit(struct file *dst, loff_t pos_dst,
256 			       struct nfs42_copy_res *res)
257 {
258 	struct nfs_commitres cres;
259 	int status = -ENOMEM;
260 
261 	cres.verf = kzalloc(sizeof(struct nfs_writeverf), GFP_KERNEL);
262 	if (!cres.verf)
263 		goto out;
264 
265 	status = nfs4_proc_commit(dst, pos_dst, res->write_res.count, &cres);
266 	if (status)
267 		goto out_free;
268 	if (nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
269 				    &cres.verf->verifier)) {
270 		dprintk("commit verf differs from copy verf\n");
271 		status = -EAGAIN;
272 	}
273 out_free:
274 	kfree(cres.verf);
275 out:
276 	return status;
277 }
278 
279 /**
280  * nfs42_copy_dest_done - perform inode cache updates after clone/copy offload
281  * @inode: pointer to destination inode
282  * @pos: destination offset
283  * @len: copy length
284  *
285  * Punch a hole in the inode page cache, so that the NFS client will
286  * know to retrieve new data.
287  * Update the file size if necessary, and then mark the inode as having
288  * invalid cached values for change attribute, ctime, mtime and space used.
289  */
290 static void nfs42_copy_dest_done(struct inode *inode, loff_t pos, loff_t len)
291 {
292 	loff_t newsize = pos + len;
293 	loff_t end = newsize - 1;
294 
295 	WARN_ON_ONCE(invalidate_inode_pages2_range(inode->i_mapping,
296 				pos >> PAGE_SHIFT, end >> PAGE_SHIFT));
297 
298 	spin_lock(&inode->i_lock);
299 	if (newsize > i_size_read(inode))
300 		i_size_write(inode, newsize);
301 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
302 					     NFS_INO_INVALID_CTIME |
303 					     NFS_INO_INVALID_MTIME |
304 					     NFS_INO_INVALID_BLOCKS);
305 	spin_unlock(&inode->i_lock);
306 }
307 
308 static ssize_t _nfs42_proc_copy(struct file *src,
309 				struct nfs_lock_context *src_lock,
310 				struct file *dst,
311 				struct nfs_lock_context *dst_lock,
312 				struct nfs42_copy_args *args,
313 				struct nfs42_copy_res *res,
314 				struct nl4_server *nss,
315 				nfs4_stateid *cnr_stateid,
316 				bool *restart)
317 {
318 	struct rpc_message msg = {
319 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
320 		.rpc_argp = args,
321 		.rpc_resp = res,
322 	};
323 	struct inode *dst_inode = file_inode(dst);
324 	struct inode *src_inode = file_inode(src);
325 	struct nfs_server *dst_server = NFS_SERVER(dst_inode);
326 	struct nfs_server *src_server = NFS_SERVER(src_inode);
327 	loff_t pos_src = args->src_pos;
328 	loff_t pos_dst = args->dst_pos;
329 	size_t count = args->count;
330 	ssize_t status;
331 
332 	if (nss) {
333 		args->cp_src = nss;
334 		nfs4_stateid_copy(&args->src_stateid, cnr_stateid);
335 	} else {
336 		status = nfs4_set_rw_stateid(&args->src_stateid,
337 				src_lock->open_context, src_lock, FMODE_READ);
338 		if (status) {
339 			if (status == -EAGAIN)
340 				status = -NFS4ERR_BAD_STATEID;
341 			return status;
342 		}
343 	}
344 	status = nfs_filemap_write_and_wait_range(src->f_mapping,
345 			pos_src, pos_src + (loff_t)count - 1);
346 	if (status)
347 		return status;
348 
349 	status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context,
350 				     dst_lock, FMODE_WRITE);
351 	if (status) {
352 		if (status == -EAGAIN)
353 			status = -NFS4ERR_BAD_STATEID;
354 		return status;
355 	}
356 
357 	status = nfs_sync_inode(dst_inode);
358 	if (status)
359 		return status;
360 
361 	res->commit_res.verf = NULL;
362 	if (args->sync) {
363 		res->commit_res.verf =
364 			kzalloc(sizeof(struct nfs_writeverf), GFP_KERNEL);
365 		if (!res->commit_res.verf)
366 			return -ENOMEM;
367 	}
368 	set_bit(NFS_CLNT_SRC_SSC_COPY_STATE,
369 		&src_lock->open_context->state->flags);
370 	set_bit(NFS_CLNT_DST_SSC_COPY_STATE,
371 		&dst_lock->open_context->state->flags);
372 
373 	status = nfs4_call_sync(dst_server->client, dst_server, &msg,
374 				&args->seq_args, &res->seq_res, 0);
375 	trace_nfs4_copy(src_inode, dst_inode, args, res, nss, status);
376 	if (status == -ENOTSUPP)
377 		dst_server->caps &= ~NFS_CAP_COPY;
378 	if (status)
379 		goto out;
380 
381 	if (args->sync &&
382 		nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
383 				    &res->commit_res.verf->verifier)) {
384 		status = -EAGAIN;
385 		goto out;
386 	}
387 
388 	if (!res->synchronous) {
389 		status = handle_async_copy(res, dst_server, src_server, src,
390 				dst, &args->src_stateid, restart);
391 		if (status)
392 			goto out;
393 	}
394 
395 	if ((!res->synchronous || !args->sync) &&
396 			res->write_res.verifier.committed != NFS_FILE_SYNC) {
397 		status = process_copy_commit(dst, pos_dst, res);
398 		if (status)
399 			goto out;
400 	}
401 
402 	nfs42_copy_dest_done(dst_inode, pos_dst, res->write_res.count);
403 	nfs_invalidate_atime(src_inode);
404 	status = res->write_res.count;
405 out:
406 	if (args->sync)
407 		kfree(res->commit_res.verf);
408 	return status;
409 }
410 
411 ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
412 			struct file *dst, loff_t pos_dst, size_t count,
413 			struct nl4_server *nss,
414 			nfs4_stateid *cnr_stateid, bool sync)
415 {
416 	struct nfs_server *server = NFS_SERVER(file_inode(dst));
417 	struct nfs_lock_context *src_lock;
418 	struct nfs_lock_context *dst_lock;
419 	struct nfs42_copy_args args = {
420 		.src_fh		= NFS_FH(file_inode(src)),
421 		.src_pos	= pos_src,
422 		.dst_fh		= NFS_FH(file_inode(dst)),
423 		.dst_pos	= pos_dst,
424 		.count		= count,
425 		.sync		= sync,
426 	};
427 	struct nfs42_copy_res res;
428 	struct nfs4_exception src_exception = {
429 		.inode		= file_inode(src),
430 		.stateid	= &args.src_stateid,
431 	};
432 	struct nfs4_exception dst_exception = {
433 		.inode		= file_inode(dst),
434 		.stateid	= &args.dst_stateid,
435 	};
436 	ssize_t err, err2;
437 	bool restart = false;
438 
439 	src_lock = nfs_get_lock_context(nfs_file_open_context(src));
440 	if (IS_ERR(src_lock))
441 		return PTR_ERR(src_lock);
442 
443 	src_exception.state = src_lock->open_context->state;
444 
445 	dst_lock = nfs_get_lock_context(nfs_file_open_context(dst));
446 	if (IS_ERR(dst_lock)) {
447 		err = PTR_ERR(dst_lock);
448 		goto out_put_src_lock;
449 	}
450 
451 	dst_exception.state = dst_lock->open_context->state;
452 
453 	do {
454 		inode_lock(file_inode(dst));
455 		err = _nfs42_proc_copy(src, src_lock,
456 				dst, dst_lock,
457 				&args, &res,
458 				nss, cnr_stateid, &restart);
459 		inode_unlock(file_inode(dst));
460 
461 		if (err >= 0)
462 			break;
463 		if ((err == -ENOTSUPP ||
464 				err == -NFS4ERR_OFFLOAD_DENIED) &&
465 				nfs42_files_from_same_server(src, dst)) {
466 			err = -EOPNOTSUPP;
467 			break;
468 		} else if (err == -EAGAIN) {
469 			if (!restart) {
470 				dst_exception.retry = 1;
471 				continue;
472 			}
473 			break;
474 		} else if (err == -NFS4ERR_OFFLOAD_NO_REQS && !args.sync) {
475 			args.sync = true;
476 			dst_exception.retry = 1;
477 			continue;
478 		} else if ((err == -ESTALE ||
479 				err == -NFS4ERR_OFFLOAD_DENIED ||
480 				err == -ENOTSUPP) &&
481 				!nfs42_files_from_same_server(src, dst)) {
482 			nfs42_do_offload_cancel_async(src, &args.src_stateid);
483 			err = -EOPNOTSUPP;
484 			break;
485 		}
486 
487 		err2 = nfs4_handle_exception(server, err, &src_exception);
488 		err  = nfs4_handle_exception(server, err, &dst_exception);
489 		if (!err)
490 			err = err2;
491 	} while (src_exception.retry || dst_exception.retry);
492 
493 	nfs_put_lock_context(dst_lock);
494 out_put_src_lock:
495 	nfs_put_lock_context(src_lock);
496 	return err;
497 }
498 
499 struct nfs42_offloadcancel_data {
500 	struct nfs_server *seq_server;
501 	struct nfs42_offload_status_args args;
502 	struct nfs42_offload_status_res res;
503 };
504 
505 static void nfs42_offload_cancel_prepare(struct rpc_task *task, void *calldata)
506 {
507 	struct nfs42_offloadcancel_data *data = calldata;
508 
509 	nfs4_setup_sequence(data->seq_server->nfs_client,
510 				&data->args.osa_seq_args,
511 				&data->res.osr_seq_res, task);
512 }
513 
514 static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata)
515 {
516 	struct nfs42_offloadcancel_data *data = calldata;
517 
518 	trace_nfs4_offload_cancel(&data->args, task->tk_status);
519 	nfs41_sequence_done(task, &data->res.osr_seq_res);
520 	if (task->tk_status &&
521 		nfs4_async_handle_error(task, data->seq_server, NULL,
522 			NULL) == -EAGAIN)
523 		rpc_restart_call_prepare(task);
524 }
525 
526 static void nfs42_free_offloadcancel_data(void *data)
527 {
528 	kfree(data);
529 }
530 
531 static const struct rpc_call_ops nfs42_offload_cancel_ops = {
532 	.rpc_call_prepare = nfs42_offload_cancel_prepare,
533 	.rpc_call_done = nfs42_offload_cancel_done,
534 	.rpc_release = nfs42_free_offloadcancel_data,
535 };
536 
537 static int nfs42_do_offload_cancel_async(struct file *dst,
538 					 nfs4_stateid *stateid)
539 {
540 	struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
541 	struct nfs42_offloadcancel_data *data = NULL;
542 	struct nfs_open_context *ctx = nfs_file_open_context(dst);
543 	struct rpc_task *task;
544 	struct rpc_message msg = {
545 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_CANCEL],
546 		.rpc_cred = ctx->cred,
547 	};
548 	struct rpc_task_setup task_setup_data = {
549 		.rpc_client = dst_server->client,
550 		.rpc_message = &msg,
551 		.callback_ops = &nfs42_offload_cancel_ops,
552 		.workqueue = nfsiod_workqueue,
553 		.flags = RPC_TASK_ASYNC,
554 	};
555 	int status;
556 
557 	if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL))
558 		return -EOPNOTSUPP;
559 
560 	data = kzalloc(sizeof(struct nfs42_offloadcancel_data), GFP_KERNEL);
561 	if (data == NULL)
562 		return -ENOMEM;
563 
564 	data->seq_server = dst_server;
565 	data->args.osa_src_fh = NFS_FH(file_inode(dst));
566 	memcpy(&data->args.osa_stateid, stateid,
567 		sizeof(data->args.osa_stateid));
568 	msg.rpc_argp = &data->args;
569 	msg.rpc_resp = &data->res;
570 	task_setup_data.callback_data = data;
571 	nfs4_init_sequence(&data->args.osa_seq_args, &data->res.osr_seq_res,
572 			   1, 0);
573 	task = rpc_run_task(&task_setup_data);
574 	if (IS_ERR(task))
575 		return PTR_ERR(task);
576 	status = rpc_wait_for_completion_task(task);
577 	if (status == -ENOTSUPP)
578 		dst_server->caps &= ~NFS_CAP_OFFLOAD_CANCEL;
579 	rpc_put_task(task);
580 	return status;
581 }
582 
583 static int _nfs42_proc_copy_notify(struct file *src, struct file *dst,
584 				   struct nfs42_copy_notify_args *args,
585 				   struct nfs42_copy_notify_res *res)
586 {
587 	struct nfs_server *src_server = NFS_SERVER(file_inode(src));
588 	struct rpc_message msg = {
589 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY_NOTIFY],
590 		.rpc_argp = args,
591 		.rpc_resp = res,
592 	};
593 	int status;
594 	struct nfs_open_context *ctx;
595 	struct nfs_lock_context *l_ctx;
596 
597 	ctx = get_nfs_open_context(nfs_file_open_context(src));
598 	l_ctx = nfs_get_lock_context(ctx);
599 	if (IS_ERR(l_ctx)) {
600 		status = PTR_ERR(l_ctx);
601 		goto out;
602 	}
603 
604 	status = nfs4_set_rw_stateid(&args->cna_src_stateid, ctx, l_ctx,
605 				     FMODE_READ);
606 	nfs_put_lock_context(l_ctx);
607 	if (status) {
608 		if (status == -EAGAIN)
609 			status = -NFS4ERR_BAD_STATEID;
610 		goto out;
611 	}
612 
613 	status = nfs4_call_sync(src_server->client, src_server, &msg,
614 				&args->cna_seq_args, &res->cnr_seq_res, 0);
615 	trace_nfs4_copy_notify(file_inode(src), args, res, status);
616 	if (status == -ENOTSUPP)
617 		src_server->caps &= ~NFS_CAP_COPY_NOTIFY;
618 
619 out:
620 	put_nfs_open_context(nfs_file_open_context(src));
621 	return status;
622 }
623 
624 int nfs42_proc_copy_notify(struct file *src, struct file *dst,
625 				struct nfs42_copy_notify_res *res)
626 {
627 	struct nfs_server *src_server = NFS_SERVER(file_inode(src));
628 	struct nfs42_copy_notify_args *args;
629 	struct nfs4_exception exception = {
630 		.inode = file_inode(src),
631 	};
632 	int status;
633 
634 	if (!(src_server->caps & NFS_CAP_COPY_NOTIFY))
635 		return -EOPNOTSUPP;
636 
637 	args = kzalloc(sizeof(struct nfs42_copy_notify_args), GFP_KERNEL);
638 	if (args == NULL)
639 		return -ENOMEM;
640 
641 	args->cna_src_fh  = NFS_FH(file_inode(src)),
642 	args->cna_dst.nl4_type = NL4_NETADDR;
643 	nfs42_set_netaddr(dst, &args->cna_dst.u.nl4_addr);
644 	exception.stateid = &args->cna_src_stateid;
645 
646 	do {
647 		status = _nfs42_proc_copy_notify(src, dst, args, res);
648 		if (status == -ENOTSUPP) {
649 			status = -EOPNOTSUPP;
650 			goto out;
651 		}
652 		status = nfs4_handle_exception(src_server, status, &exception);
653 	} while (exception.retry);
654 
655 out:
656 	kfree(args);
657 	return status;
658 }
659 
660 static loff_t _nfs42_proc_llseek(struct file *filep,
661 		struct nfs_lock_context *lock, loff_t offset, int whence)
662 {
663 	struct inode *inode = file_inode(filep);
664 	struct nfs42_seek_args args = {
665 		.sa_fh		= NFS_FH(inode),
666 		.sa_offset	= offset,
667 		.sa_what	= (whence == SEEK_HOLE) ?
668 					NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
669 	};
670 	struct nfs42_seek_res res;
671 	struct rpc_message msg = {
672 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
673 		.rpc_argp = &args,
674 		.rpc_resp = &res,
675 	};
676 	struct nfs_server *server = NFS_SERVER(inode);
677 	int status;
678 
679 	if (!nfs_server_capable(inode, NFS_CAP_SEEK))
680 		return -ENOTSUPP;
681 
682 	status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
683 			lock, FMODE_READ);
684 	if (status) {
685 		if (status == -EAGAIN)
686 			status = -NFS4ERR_BAD_STATEID;
687 		return status;
688 	}
689 
690 	status = nfs_filemap_write_and_wait_range(inode->i_mapping,
691 			offset, LLONG_MAX);
692 	if (status)
693 		return status;
694 
695 	status = nfs4_call_sync(server->client, server, &msg,
696 				&args.seq_args, &res.seq_res, 0);
697 	trace_nfs4_llseek(inode, &args, &res, status);
698 	if (status == -ENOTSUPP)
699 		server->caps &= ~NFS_CAP_SEEK;
700 	if (status)
701 		return status;
702 
703 	if (whence == SEEK_DATA && res.sr_eof)
704 		return -NFS4ERR_NXIO;
705 	else
706 		return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
707 }
708 
709 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
710 {
711 	struct nfs_server *server = NFS_SERVER(file_inode(filep));
712 	struct nfs4_exception exception = { };
713 	struct nfs_lock_context *lock;
714 	loff_t err;
715 
716 	lock = nfs_get_lock_context(nfs_file_open_context(filep));
717 	if (IS_ERR(lock))
718 		return PTR_ERR(lock);
719 
720 	exception.inode = file_inode(filep);
721 	exception.state = lock->open_context->state;
722 
723 	do {
724 		err = _nfs42_proc_llseek(filep, lock, offset, whence);
725 		if (err >= 0)
726 			break;
727 		if (err == -ENOTSUPP) {
728 			err = -EOPNOTSUPP;
729 			break;
730 		}
731 		err = nfs4_handle_exception(server, err, &exception);
732 	} while (exception.retry);
733 
734 	nfs_put_lock_context(lock);
735 	return err;
736 }
737 
738 
739 static void
740 nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
741 {
742 	struct nfs42_layoutstat_data *data = calldata;
743 	struct inode *inode = data->inode;
744 	struct nfs_server *server = NFS_SERVER(inode);
745 	struct pnfs_layout_hdr *lo;
746 
747 	spin_lock(&inode->i_lock);
748 	lo = NFS_I(inode)->layout;
749 	if (!pnfs_layout_is_valid(lo)) {
750 		spin_unlock(&inode->i_lock);
751 		rpc_exit(task, 0);
752 		return;
753 	}
754 	nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
755 	spin_unlock(&inode->i_lock);
756 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
757 			    &data->res.seq_res, task);
758 }
759 
760 static void
761 nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
762 {
763 	struct nfs42_layoutstat_data *data = calldata;
764 	struct inode *inode = data->inode;
765 	struct pnfs_layout_hdr *lo;
766 
767 	if (!nfs4_sequence_done(task, &data->res.seq_res))
768 		return;
769 
770 	switch (task->tk_status) {
771 	case 0:
772 		return;
773 	case -NFS4ERR_BADHANDLE:
774 	case -ESTALE:
775 		pnfs_destroy_layout(NFS_I(inode));
776 		break;
777 	case -NFS4ERR_EXPIRED:
778 	case -NFS4ERR_ADMIN_REVOKED:
779 	case -NFS4ERR_DELEG_REVOKED:
780 	case -NFS4ERR_STALE_STATEID:
781 	case -NFS4ERR_BAD_STATEID:
782 		spin_lock(&inode->i_lock);
783 		lo = NFS_I(inode)->layout;
784 		if (pnfs_layout_is_valid(lo) &&
785 		    nfs4_stateid_match(&data->args.stateid,
786 					     &lo->plh_stateid)) {
787 			LIST_HEAD(head);
788 
789 			/*
790 			 * Mark the bad layout state as invalid, then retry
791 			 * with the current stateid.
792 			 */
793 			pnfs_mark_layout_stateid_invalid(lo, &head);
794 			spin_unlock(&inode->i_lock);
795 			pnfs_free_lseg_list(&head);
796 			nfs_commit_inode(inode, 0);
797 		} else
798 			spin_unlock(&inode->i_lock);
799 		break;
800 	case -NFS4ERR_OLD_STATEID:
801 		spin_lock(&inode->i_lock);
802 		lo = NFS_I(inode)->layout;
803 		if (pnfs_layout_is_valid(lo) &&
804 		    nfs4_stateid_match_other(&data->args.stateid,
805 					&lo->plh_stateid)) {
806 			/* Do we need to delay before resending? */
807 			if (!nfs4_stateid_is_newer(&lo->plh_stateid,
808 						&data->args.stateid))
809 				rpc_delay(task, HZ);
810 			rpc_restart_call_prepare(task);
811 		}
812 		spin_unlock(&inode->i_lock);
813 		break;
814 	case -ENOTSUPP:
815 	case -EOPNOTSUPP:
816 		NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
817 	}
818 
819 	trace_nfs4_layoutstats(inode, &data->args.stateid, task->tk_status);
820 }
821 
822 static void
823 nfs42_layoutstat_release(void *calldata)
824 {
825 	struct nfs42_layoutstat_data *data = calldata;
826 	struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo;
827 	int i;
828 
829 	for (i = 0; i < data->args.num_dev; i++) {
830 		if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free)
831 			devinfo[i].ld_private.ops->free(&devinfo[i].ld_private);
832 	}
833 
834 	pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
835 	smp_mb__before_atomic();
836 	clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
837 	smp_mb__after_atomic();
838 	nfs_iput_and_deactive(data->inode);
839 	kfree(data->args.devinfo);
840 	kfree(data);
841 }
842 
843 static const struct rpc_call_ops nfs42_layoutstat_ops = {
844 	.rpc_call_prepare = nfs42_layoutstat_prepare,
845 	.rpc_call_done = nfs42_layoutstat_done,
846 	.rpc_release = nfs42_layoutstat_release,
847 };
848 
849 int nfs42_proc_layoutstats_generic(struct nfs_server *server,
850 				   struct nfs42_layoutstat_data *data)
851 {
852 	struct rpc_message msg = {
853 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
854 		.rpc_argp = &data->args,
855 		.rpc_resp = &data->res,
856 	};
857 	struct rpc_task_setup task_setup = {
858 		.rpc_client = server->client,
859 		.rpc_message = &msg,
860 		.callback_ops = &nfs42_layoutstat_ops,
861 		.callback_data = data,
862 		.flags = RPC_TASK_ASYNC,
863 	};
864 	struct rpc_task *task;
865 
866 	data->inode = nfs_igrab_and_active(data->args.inode);
867 	if (!data->inode) {
868 		nfs42_layoutstat_release(data);
869 		return -EAGAIN;
870 	}
871 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
872 	task = rpc_run_task(&task_setup);
873 	if (IS_ERR(task))
874 		return PTR_ERR(task);
875 	rpc_put_task(task);
876 	return 0;
877 }
878 
879 static struct nfs42_layouterror_data *
880 nfs42_alloc_layouterror_data(struct pnfs_layout_segment *lseg, gfp_t gfp_flags)
881 {
882 	struct nfs42_layouterror_data *data;
883 	struct inode *inode = lseg->pls_layout->plh_inode;
884 
885 	data = kzalloc(sizeof(*data), gfp_flags);
886 	if (data) {
887 		data->args.inode = data->inode = nfs_igrab_and_active(inode);
888 		if (data->inode) {
889 			data->lseg = pnfs_get_lseg(lseg);
890 			if (data->lseg)
891 				return data;
892 			nfs_iput_and_deactive(data->inode);
893 		}
894 		kfree(data);
895 	}
896 	return NULL;
897 }
898 
899 static void
900 nfs42_free_layouterror_data(struct nfs42_layouterror_data *data)
901 {
902 	pnfs_put_lseg(data->lseg);
903 	nfs_iput_and_deactive(data->inode);
904 	kfree(data);
905 }
906 
907 static void
908 nfs42_layouterror_prepare(struct rpc_task *task, void *calldata)
909 {
910 	struct nfs42_layouterror_data *data = calldata;
911 	struct inode *inode = data->inode;
912 	struct nfs_server *server = NFS_SERVER(inode);
913 	struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
914 	unsigned i;
915 
916 	spin_lock(&inode->i_lock);
917 	if (!pnfs_layout_is_valid(lo)) {
918 		spin_unlock(&inode->i_lock);
919 		rpc_exit(task, 0);
920 		return;
921 	}
922 	for (i = 0; i < data->args.num_errors; i++)
923 		nfs4_stateid_copy(&data->args.errors[i].stateid,
924 				&lo->plh_stateid);
925 	spin_unlock(&inode->i_lock);
926 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
927 			    &data->res.seq_res, task);
928 }
929 
930 static void
931 nfs42_layouterror_done(struct rpc_task *task, void *calldata)
932 {
933 	struct nfs42_layouterror_data *data = calldata;
934 	struct inode *inode = data->inode;
935 	struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
936 
937 	if (!nfs4_sequence_done(task, &data->res.seq_res))
938 		return;
939 
940 	switch (task->tk_status) {
941 	case 0:
942 		return;
943 	case -NFS4ERR_BADHANDLE:
944 	case -ESTALE:
945 		pnfs_destroy_layout(NFS_I(inode));
946 		break;
947 	case -NFS4ERR_EXPIRED:
948 	case -NFS4ERR_ADMIN_REVOKED:
949 	case -NFS4ERR_DELEG_REVOKED:
950 	case -NFS4ERR_STALE_STATEID:
951 	case -NFS4ERR_BAD_STATEID:
952 		spin_lock(&inode->i_lock);
953 		if (pnfs_layout_is_valid(lo) &&
954 		    nfs4_stateid_match(&data->args.errors[0].stateid,
955 					     &lo->plh_stateid)) {
956 			LIST_HEAD(head);
957 
958 			/*
959 			 * Mark the bad layout state as invalid, then retry
960 			 * with the current stateid.
961 			 */
962 			pnfs_mark_layout_stateid_invalid(lo, &head);
963 			spin_unlock(&inode->i_lock);
964 			pnfs_free_lseg_list(&head);
965 			nfs_commit_inode(inode, 0);
966 		} else
967 			spin_unlock(&inode->i_lock);
968 		break;
969 	case -NFS4ERR_OLD_STATEID:
970 		spin_lock(&inode->i_lock);
971 		if (pnfs_layout_is_valid(lo) &&
972 		    nfs4_stateid_match_other(&data->args.errors[0].stateid,
973 					&lo->plh_stateid)) {
974 			/* Do we need to delay before resending? */
975 			if (!nfs4_stateid_is_newer(&lo->plh_stateid,
976 						&data->args.errors[0].stateid))
977 				rpc_delay(task, HZ);
978 			rpc_restart_call_prepare(task);
979 		}
980 		spin_unlock(&inode->i_lock);
981 		break;
982 	case -ENOTSUPP:
983 	case -EOPNOTSUPP:
984 		NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTERROR;
985 	}
986 
987 	trace_nfs4_layouterror(inode, &data->args.errors[0].stateid,
988 			       task->tk_status);
989 }
990 
991 static void
992 nfs42_layouterror_release(void *calldata)
993 {
994 	struct nfs42_layouterror_data *data = calldata;
995 
996 	nfs42_free_layouterror_data(data);
997 }
998 
999 static const struct rpc_call_ops nfs42_layouterror_ops = {
1000 	.rpc_call_prepare = nfs42_layouterror_prepare,
1001 	.rpc_call_done = nfs42_layouterror_done,
1002 	.rpc_release = nfs42_layouterror_release,
1003 };
1004 
1005 int nfs42_proc_layouterror(struct pnfs_layout_segment *lseg,
1006 		const struct nfs42_layout_error *errors, size_t n)
1007 {
1008 	struct inode *inode = lseg->pls_layout->plh_inode;
1009 	struct nfs42_layouterror_data *data;
1010 	struct rpc_task *task;
1011 	struct rpc_message msg = {
1012 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTERROR],
1013 	};
1014 	struct rpc_task_setup task_setup = {
1015 		.rpc_message = &msg,
1016 		.callback_ops = &nfs42_layouterror_ops,
1017 		.flags = RPC_TASK_ASYNC,
1018 	};
1019 	unsigned int i;
1020 
1021 	if (!nfs_server_capable(inode, NFS_CAP_LAYOUTERROR))
1022 		return -EOPNOTSUPP;
1023 	if (n > NFS42_LAYOUTERROR_MAX)
1024 		return -EINVAL;
1025 	data = nfs42_alloc_layouterror_data(lseg, nfs_io_gfp_mask());
1026 	if (!data)
1027 		return -ENOMEM;
1028 	for (i = 0; i < n; i++) {
1029 		data->args.errors[i] = errors[i];
1030 		data->args.num_errors++;
1031 		data->res.num_errors++;
1032 	}
1033 	msg.rpc_argp = &data->args;
1034 	msg.rpc_resp = &data->res;
1035 	task_setup.callback_data = data;
1036 	task_setup.rpc_client = NFS_SERVER(inode)->client;
1037 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
1038 	task = rpc_run_task(&task_setup);
1039 	if (IS_ERR(task))
1040 		return PTR_ERR(task);
1041 	rpc_put_task(task);
1042 	return 0;
1043 }
1044 EXPORT_SYMBOL_GPL(nfs42_proc_layouterror);
1045 
1046 static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
1047 		struct file *dst_f, struct nfs_lock_context *src_lock,
1048 		struct nfs_lock_context *dst_lock, loff_t src_offset,
1049 		loff_t dst_offset, loff_t count)
1050 {
1051 	struct inode *src_inode = file_inode(src_f);
1052 	struct inode *dst_inode = file_inode(dst_f);
1053 	struct nfs_server *server = NFS_SERVER(dst_inode);
1054 	__u32 dst_bitmask[NFS_BITMASK_SZ];
1055 	struct nfs42_clone_args args = {
1056 		.src_fh = NFS_FH(src_inode),
1057 		.dst_fh = NFS_FH(dst_inode),
1058 		.src_offset = src_offset,
1059 		.dst_offset = dst_offset,
1060 		.count = count,
1061 		.dst_bitmask = dst_bitmask,
1062 	};
1063 	struct nfs42_clone_res res = {
1064 		.server	= server,
1065 	};
1066 	int status;
1067 
1068 	msg->rpc_argp = &args;
1069 	msg->rpc_resp = &res;
1070 
1071 	status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
1072 			src_lock, FMODE_READ);
1073 	if (status) {
1074 		if (status == -EAGAIN)
1075 			status = -NFS4ERR_BAD_STATEID;
1076 		return status;
1077 	}
1078 	status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
1079 			dst_lock, FMODE_WRITE);
1080 	if (status) {
1081 		if (status == -EAGAIN)
1082 			status = -NFS4ERR_BAD_STATEID;
1083 		return status;
1084 	}
1085 
1086 	res.dst_fattr = nfs_alloc_fattr();
1087 	if (!res.dst_fattr)
1088 		return -ENOMEM;
1089 
1090 	nfs4_bitmask_set(dst_bitmask, server->cache_consistency_bitmask,
1091 			 dst_inode, NFS_INO_INVALID_BLOCKS);
1092 
1093 	status = nfs4_call_sync(server->client, server, msg,
1094 				&args.seq_args, &res.seq_res, 0);
1095 	trace_nfs4_clone(src_inode, dst_inode, &args, status);
1096 	if (status == 0) {
1097 		/* a zero-length count means clone to EOF in src */
1098 		if (count == 0 && res.dst_fattr->valid & NFS_ATTR_FATTR_SIZE)
1099 			count = nfs_size_to_loff_t(res.dst_fattr->size) - dst_offset;
1100 		nfs42_copy_dest_done(dst_inode, dst_offset, count);
1101 		status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
1102 	}
1103 
1104 	kfree(res.dst_fattr);
1105 	return status;
1106 }
1107 
1108 int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
1109 		     loff_t src_offset, loff_t dst_offset, loff_t count)
1110 {
1111 	struct rpc_message msg = {
1112 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
1113 	};
1114 	struct inode *inode = file_inode(src_f);
1115 	struct nfs_server *server = NFS_SERVER(file_inode(src_f));
1116 	struct nfs_lock_context *src_lock;
1117 	struct nfs_lock_context *dst_lock;
1118 	struct nfs4_exception src_exception = { };
1119 	struct nfs4_exception dst_exception = { };
1120 	int err, err2;
1121 
1122 	if (!nfs_server_capable(inode, NFS_CAP_CLONE))
1123 		return -EOPNOTSUPP;
1124 
1125 	src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
1126 	if (IS_ERR(src_lock))
1127 		return PTR_ERR(src_lock);
1128 
1129 	src_exception.inode = file_inode(src_f);
1130 	src_exception.state = src_lock->open_context->state;
1131 
1132 	dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
1133 	if (IS_ERR(dst_lock)) {
1134 		err = PTR_ERR(dst_lock);
1135 		goto out_put_src_lock;
1136 	}
1137 
1138 	dst_exception.inode = file_inode(dst_f);
1139 	dst_exception.state = dst_lock->open_context->state;
1140 
1141 	do {
1142 		err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
1143 					src_offset, dst_offset, count);
1144 		if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
1145 			NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
1146 			err = -EOPNOTSUPP;
1147 			break;
1148 		}
1149 
1150 		err2 = nfs4_handle_exception(server, err, &src_exception);
1151 		err = nfs4_handle_exception(server, err, &dst_exception);
1152 		if (!err)
1153 			err = err2;
1154 	} while (src_exception.retry || dst_exception.retry);
1155 
1156 	nfs_put_lock_context(dst_lock);
1157 out_put_src_lock:
1158 	nfs_put_lock_context(src_lock);
1159 	return err;
1160 }
1161 
1162 #define NFS4XATTR_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
1163 
1164 static int _nfs42_proc_removexattr(struct inode *inode, const char *name)
1165 {
1166 	struct nfs_server *server = NFS_SERVER(inode);
1167 	struct nfs42_removexattrargs args = {
1168 		.fh = NFS_FH(inode),
1169 		.xattr_name = name,
1170 	};
1171 	struct nfs42_removexattrres res;
1172 	struct rpc_message msg = {
1173 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVEXATTR],
1174 		.rpc_argp = &args,
1175 		.rpc_resp = &res,
1176 	};
1177 	int ret;
1178 	unsigned long timestamp = jiffies;
1179 
1180 	ret = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
1181 	    &res.seq_res, 1);
1182 	trace_nfs4_removexattr(inode, name, ret);
1183 	if (!ret)
1184 		nfs4_update_changeattr(inode, &res.cinfo, timestamp, 0);
1185 
1186 	return ret;
1187 }
1188 
1189 static int _nfs42_proc_setxattr(struct inode *inode, const char *name,
1190 				const void *buf, size_t buflen, int flags)
1191 {
1192 	struct nfs_server *server = NFS_SERVER(inode);
1193 	struct page *pages[NFS4XATTR_MAXPAGES];
1194 	struct nfs42_setxattrargs arg = {
1195 		.fh		= NFS_FH(inode),
1196 		.xattr_pages	= pages,
1197 		.xattr_len	= buflen,
1198 		.xattr_name	= name,
1199 		.xattr_flags	= flags,
1200 	};
1201 	struct nfs42_setxattrres res;
1202 	struct rpc_message msg = {
1203 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETXATTR],
1204 		.rpc_argp	= &arg,
1205 		.rpc_resp	= &res,
1206 	};
1207 	int ret, np;
1208 	unsigned long timestamp = jiffies;
1209 
1210 	if (buflen > server->sxasize)
1211 		return -ERANGE;
1212 
1213 	if (buflen > 0) {
1214 		np = nfs4_buf_to_pages_noslab(buf, buflen, arg.xattr_pages);
1215 		if (np < 0)
1216 			return np;
1217 	} else
1218 		np = 0;
1219 
1220 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1221 	    &res.seq_res, 1);
1222 	trace_nfs4_setxattr(inode, name, ret);
1223 
1224 	for (; np > 0; np--)
1225 		put_page(pages[np - 1]);
1226 
1227 	if (!ret)
1228 		nfs4_update_changeattr(inode, &res.cinfo, timestamp, 0);
1229 
1230 	return ret;
1231 }
1232 
1233 static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,
1234 				void *buf, size_t buflen, struct page **pages,
1235 				size_t plen)
1236 {
1237 	struct nfs_server *server = NFS_SERVER(inode);
1238 	struct nfs42_getxattrargs arg = {
1239 		.fh		= NFS_FH(inode),
1240 		.xattr_name	= name,
1241 	};
1242 	struct nfs42_getxattrres res;
1243 	struct rpc_message msg = {
1244 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_GETXATTR],
1245 		.rpc_argp	= &arg,
1246 		.rpc_resp	= &res,
1247 	};
1248 	ssize_t ret;
1249 
1250 	arg.xattr_len = plen;
1251 	arg.xattr_pages = pages;
1252 
1253 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1254 	    &res.seq_res, 0);
1255 	trace_nfs4_getxattr(inode, name, ret);
1256 	if (ret < 0)
1257 		return ret;
1258 
1259 	/*
1260 	 * Normally, the caching is done one layer up, but for successful
1261 	 * RPCS, always cache the result here, even if the caller was
1262 	 * just querying the length, or if the reply was too big for
1263 	 * the caller. This avoids a second RPC in the case of the
1264 	 * common query-alloc-retrieve cycle for xattrs.
1265 	 *
1266 	 * Note that xattr_len is always capped to XATTR_SIZE_MAX.
1267 	 */
1268 
1269 	nfs4_xattr_cache_add(inode, name, NULL, pages, res.xattr_len);
1270 
1271 	if (buflen) {
1272 		if (res.xattr_len > buflen)
1273 			return -ERANGE;
1274 		_copy_from_pages(buf, pages, 0, res.xattr_len);
1275 	}
1276 
1277 	return res.xattr_len;
1278 }
1279 
1280 static ssize_t _nfs42_proc_listxattrs(struct inode *inode, void *buf,
1281 				 size_t buflen, u64 *cookiep, bool *eofp)
1282 {
1283 	struct nfs_server *server = NFS_SERVER(inode);
1284 	struct page **pages;
1285 	struct nfs42_listxattrsargs arg = {
1286 		.fh		= NFS_FH(inode),
1287 		.cookie		= *cookiep,
1288 	};
1289 	struct nfs42_listxattrsres res = {
1290 		.eof = false,
1291 		.xattr_buf = buf,
1292 		.xattr_len = buflen,
1293 	};
1294 	struct rpc_message msg = {
1295 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_LISTXATTRS],
1296 		.rpc_argp	= &arg,
1297 		.rpc_resp	= &res,
1298 	};
1299 	u32 xdrlen;
1300 	int ret, np, i;
1301 
1302 
1303 	ret = -ENOMEM;
1304 	res.scratch = alloc_page(GFP_KERNEL);
1305 	if (!res.scratch)
1306 		goto out;
1307 
1308 	xdrlen = nfs42_listxattr_xdrsize(buflen);
1309 	if (xdrlen > server->lxasize)
1310 		xdrlen = server->lxasize;
1311 	np = xdrlen / PAGE_SIZE + 1;
1312 
1313 	pages = kcalloc(np, sizeof(struct page *), GFP_KERNEL);
1314 	if (!pages)
1315 		goto out_free_scratch;
1316 	for (i = 0; i < np; i++) {
1317 		pages[i] = alloc_page(GFP_KERNEL);
1318 		if (!pages[i])
1319 			goto out_free_pages;
1320 	}
1321 
1322 	arg.xattr_pages = pages;
1323 	arg.count = xdrlen;
1324 
1325 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1326 	    &res.seq_res, 0);
1327 	trace_nfs4_listxattr(inode, ret);
1328 
1329 	if (ret >= 0) {
1330 		ret = res.copied;
1331 		*cookiep = res.cookie;
1332 		*eofp = res.eof;
1333 	}
1334 
1335 out_free_pages:
1336 	while (--np >= 0) {
1337 		if (pages[np])
1338 			__free_page(pages[np]);
1339 	}
1340 	kfree(pages);
1341 out_free_scratch:
1342 	__free_page(res.scratch);
1343 out:
1344 	return ret;
1345 
1346 }
1347 
1348 ssize_t nfs42_proc_getxattr(struct inode *inode, const char *name,
1349 			      void *buf, size_t buflen)
1350 {
1351 	struct nfs4_exception exception = { };
1352 	ssize_t err, np, i;
1353 	struct page **pages;
1354 
1355 	np = nfs_page_array_len(0, buflen ?: XATTR_SIZE_MAX);
1356 	pages = kmalloc_array(np, sizeof(*pages), GFP_KERNEL);
1357 	if (!pages)
1358 		return -ENOMEM;
1359 
1360 	for (i = 0; i < np; i++) {
1361 		pages[i] = alloc_page(GFP_KERNEL);
1362 		if (!pages[i]) {
1363 			np = i + 1;
1364 			err = -ENOMEM;
1365 			goto out;
1366 		}
1367 	}
1368 
1369 	/*
1370 	 * The GETXATTR op has no length field in the call, and the
1371 	 * xattr data is at the end of the reply.
1372 	 *
1373 	 * There is no downside in using the page-aligned length. It will
1374 	 * allow receiving and caching xattrs that are too large for the
1375 	 * caller but still fit in the page-rounded value.
1376 	 */
1377 	do {
1378 		err = _nfs42_proc_getxattr(inode, name, buf, buflen,
1379 			pages, np * PAGE_SIZE);
1380 		if (err >= 0)
1381 			break;
1382 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
1383 				&exception);
1384 	} while (exception.retry);
1385 
1386 out:
1387 	while (--np >= 0)
1388 		__free_page(pages[np]);
1389 	kfree(pages);
1390 
1391 	return err;
1392 }
1393 
1394 int nfs42_proc_setxattr(struct inode *inode, const char *name,
1395 			      const void *buf, size_t buflen, int flags)
1396 {
1397 	struct nfs4_exception exception = { };
1398 	int err;
1399 
1400 	do {
1401 		err = _nfs42_proc_setxattr(inode, name, buf, buflen, flags);
1402 		if (!err)
1403 			break;
1404 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
1405 				&exception);
1406 	} while (exception.retry);
1407 
1408 	return err;
1409 }
1410 
1411 ssize_t nfs42_proc_listxattrs(struct inode *inode, void *buf,
1412 			      size_t buflen, u64 *cookiep, bool *eofp)
1413 {
1414 	struct nfs4_exception exception = { };
1415 	ssize_t err;
1416 
1417 	do {
1418 		err = _nfs42_proc_listxattrs(inode, buf, buflen,
1419 		    cookiep, eofp);
1420 		if (err >= 0)
1421 			break;
1422 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
1423 				&exception);
1424 	} while (exception.retry);
1425 
1426 	return err;
1427 }
1428 
1429 int nfs42_proc_removexattr(struct inode *inode, const char *name)
1430 {
1431 	struct nfs4_exception exception = { };
1432 	int err;
1433 
1434 	do {
1435 		err = _nfs42_proc_removexattr(inode, name);
1436 		if (!err)
1437 			break;
1438 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
1439 				&exception);
1440 	} while (exception.retry);
1441 
1442 	return err;
1443 }
1444