xref: /openbmc/linux/fs/nfs/nfs42proc.c (revision 8957261c)
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 &&
475 				args.sync != res.synchronous) {
476 			args.sync = res.synchronous;
477 			dst_exception.retry = 1;
478 			continue;
479 		} else if ((err == -ESTALE ||
480 				err == -NFS4ERR_OFFLOAD_DENIED ||
481 				err == -ENOTSUPP) &&
482 				!nfs42_files_from_same_server(src, dst)) {
483 			nfs42_do_offload_cancel_async(src, &args.src_stateid);
484 			err = -EOPNOTSUPP;
485 			break;
486 		}
487 
488 		err2 = nfs4_handle_exception(server, err, &src_exception);
489 		err  = nfs4_handle_exception(server, err, &dst_exception);
490 		if (!err)
491 			err = err2;
492 	} while (src_exception.retry || dst_exception.retry);
493 
494 	nfs_put_lock_context(dst_lock);
495 out_put_src_lock:
496 	nfs_put_lock_context(src_lock);
497 	return err;
498 }
499 
500 struct nfs42_offloadcancel_data {
501 	struct nfs_server *seq_server;
502 	struct nfs42_offload_status_args args;
503 	struct nfs42_offload_status_res res;
504 };
505 
506 static void nfs42_offload_cancel_prepare(struct rpc_task *task, void *calldata)
507 {
508 	struct nfs42_offloadcancel_data *data = calldata;
509 
510 	nfs4_setup_sequence(data->seq_server->nfs_client,
511 				&data->args.osa_seq_args,
512 				&data->res.osr_seq_res, task);
513 }
514 
515 static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata)
516 {
517 	struct nfs42_offloadcancel_data *data = calldata;
518 
519 	trace_nfs4_offload_cancel(&data->args, task->tk_status);
520 	nfs41_sequence_done(task, &data->res.osr_seq_res);
521 	if (task->tk_status &&
522 		nfs4_async_handle_error(task, data->seq_server, NULL,
523 			NULL) == -EAGAIN)
524 		rpc_restart_call_prepare(task);
525 }
526 
527 static void nfs42_free_offloadcancel_data(void *data)
528 {
529 	kfree(data);
530 }
531 
532 static const struct rpc_call_ops nfs42_offload_cancel_ops = {
533 	.rpc_call_prepare = nfs42_offload_cancel_prepare,
534 	.rpc_call_done = nfs42_offload_cancel_done,
535 	.rpc_release = nfs42_free_offloadcancel_data,
536 };
537 
538 static int nfs42_do_offload_cancel_async(struct file *dst,
539 					 nfs4_stateid *stateid)
540 {
541 	struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
542 	struct nfs42_offloadcancel_data *data = NULL;
543 	struct nfs_open_context *ctx = nfs_file_open_context(dst);
544 	struct rpc_task *task;
545 	struct rpc_message msg = {
546 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_CANCEL],
547 		.rpc_cred = ctx->cred,
548 	};
549 	struct rpc_task_setup task_setup_data = {
550 		.rpc_client = dst_server->client,
551 		.rpc_message = &msg,
552 		.callback_ops = &nfs42_offload_cancel_ops,
553 		.workqueue = nfsiod_workqueue,
554 		.flags = RPC_TASK_ASYNC,
555 	};
556 	int status;
557 
558 	if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL))
559 		return -EOPNOTSUPP;
560 
561 	data = kzalloc(sizeof(struct nfs42_offloadcancel_data), GFP_KERNEL);
562 	if (data == NULL)
563 		return -ENOMEM;
564 
565 	data->seq_server = dst_server;
566 	data->args.osa_src_fh = NFS_FH(file_inode(dst));
567 	memcpy(&data->args.osa_stateid, stateid,
568 		sizeof(data->args.osa_stateid));
569 	msg.rpc_argp = &data->args;
570 	msg.rpc_resp = &data->res;
571 	task_setup_data.callback_data = data;
572 	nfs4_init_sequence(&data->args.osa_seq_args, &data->res.osr_seq_res,
573 			   1, 0);
574 	task = rpc_run_task(&task_setup_data);
575 	if (IS_ERR(task))
576 		return PTR_ERR(task);
577 	status = rpc_wait_for_completion_task(task);
578 	if (status == -ENOTSUPP)
579 		dst_server->caps &= ~NFS_CAP_OFFLOAD_CANCEL;
580 	rpc_put_task(task);
581 	return status;
582 }
583 
584 static int _nfs42_proc_copy_notify(struct file *src, struct file *dst,
585 				   struct nfs42_copy_notify_args *args,
586 				   struct nfs42_copy_notify_res *res)
587 {
588 	struct nfs_server *src_server = NFS_SERVER(file_inode(src));
589 	struct rpc_message msg = {
590 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY_NOTIFY],
591 		.rpc_argp = args,
592 		.rpc_resp = res,
593 	};
594 	int status;
595 	struct nfs_open_context *ctx;
596 	struct nfs_lock_context *l_ctx;
597 
598 	ctx = get_nfs_open_context(nfs_file_open_context(src));
599 	l_ctx = nfs_get_lock_context(ctx);
600 	if (IS_ERR(l_ctx)) {
601 		status = PTR_ERR(l_ctx);
602 		goto out;
603 	}
604 
605 	status = nfs4_set_rw_stateid(&args->cna_src_stateid, ctx, l_ctx,
606 				     FMODE_READ);
607 	nfs_put_lock_context(l_ctx);
608 	if (status) {
609 		if (status == -EAGAIN)
610 			status = -NFS4ERR_BAD_STATEID;
611 		goto out;
612 	}
613 
614 	status = nfs4_call_sync(src_server->client, src_server, &msg,
615 				&args->cna_seq_args, &res->cnr_seq_res, 0);
616 	trace_nfs4_copy_notify(file_inode(src), args, res, status);
617 	if (status == -ENOTSUPP)
618 		src_server->caps &= ~NFS_CAP_COPY_NOTIFY;
619 
620 out:
621 	put_nfs_open_context(nfs_file_open_context(src));
622 	return status;
623 }
624 
625 int nfs42_proc_copy_notify(struct file *src, struct file *dst,
626 				struct nfs42_copy_notify_res *res)
627 {
628 	struct nfs_server *src_server = NFS_SERVER(file_inode(src));
629 	struct nfs42_copy_notify_args *args;
630 	struct nfs4_exception exception = {
631 		.inode = file_inode(src),
632 	};
633 	int status;
634 
635 	if (!(src_server->caps & NFS_CAP_COPY_NOTIFY))
636 		return -EOPNOTSUPP;
637 
638 	args = kzalloc(sizeof(struct nfs42_copy_notify_args), GFP_KERNEL);
639 	if (args == NULL)
640 		return -ENOMEM;
641 
642 	args->cna_src_fh  = NFS_FH(file_inode(src)),
643 	args->cna_dst.nl4_type = NL4_NETADDR;
644 	nfs42_set_netaddr(dst, &args->cna_dst.u.nl4_addr);
645 	exception.stateid = &args->cna_src_stateid;
646 
647 	do {
648 		status = _nfs42_proc_copy_notify(src, dst, args, res);
649 		if (status == -ENOTSUPP) {
650 			status = -EOPNOTSUPP;
651 			goto out;
652 		}
653 		status = nfs4_handle_exception(src_server, status, &exception);
654 	} while (exception.retry);
655 
656 out:
657 	kfree(args);
658 	return status;
659 }
660 
661 static loff_t _nfs42_proc_llseek(struct file *filep,
662 		struct nfs_lock_context *lock, loff_t offset, int whence)
663 {
664 	struct inode *inode = file_inode(filep);
665 	struct nfs42_seek_args args = {
666 		.sa_fh		= NFS_FH(inode),
667 		.sa_offset	= offset,
668 		.sa_what	= (whence == SEEK_HOLE) ?
669 					NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
670 	};
671 	struct nfs42_seek_res res;
672 	struct rpc_message msg = {
673 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
674 		.rpc_argp = &args,
675 		.rpc_resp = &res,
676 	};
677 	struct nfs_server *server = NFS_SERVER(inode);
678 	int status;
679 
680 	if (!nfs_server_capable(inode, NFS_CAP_SEEK))
681 		return -ENOTSUPP;
682 
683 	status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
684 			lock, FMODE_READ);
685 	if (status) {
686 		if (status == -EAGAIN)
687 			status = -NFS4ERR_BAD_STATEID;
688 		return status;
689 	}
690 
691 	status = nfs_filemap_write_and_wait_range(inode->i_mapping,
692 			offset, LLONG_MAX);
693 	if (status)
694 		return status;
695 
696 	status = nfs4_call_sync(server->client, server, &msg,
697 				&args.seq_args, &res.seq_res, 0);
698 	trace_nfs4_llseek(inode, &args, &res, status);
699 	if (status == -ENOTSUPP)
700 		server->caps &= ~NFS_CAP_SEEK;
701 	if (status)
702 		return status;
703 
704 	if (whence == SEEK_DATA && res.sr_eof)
705 		return -NFS4ERR_NXIO;
706 	else
707 		return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
708 }
709 
710 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
711 {
712 	struct nfs_server *server = NFS_SERVER(file_inode(filep));
713 	struct nfs4_exception exception = { };
714 	struct nfs_lock_context *lock;
715 	loff_t err;
716 
717 	lock = nfs_get_lock_context(nfs_file_open_context(filep));
718 	if (IS_ERR(lock))
719 		return PTR_ERR(lock);
720 
721 	exception.inode = file_inode(filep);
722 	exception.state = lock->open_context->state;
723 
724 	do {
725 		err = _nfs42_proc_llseek(filep, lock, offset, whence);
726 		if (err >= 0)
727 			break;
728 		if (err == -ENOTSUPP) {
729 			err = -EOPNOTSUPP;
730 			break;
731 		}
732 		err = nfs4_handle_exception(server, err, &exception);
733 	} while (exception.retry);
734 
735 	nfs_put_lock_context(lock);
736 	return err;
737 }
738 
739 
740 static void
741 nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
742 {
743 	struct nfs42_layoutstat_data *data = calldata;
744 	struct inode *inode = data->inode;
745 	struct nfs_server *server = NFS_SERVER(inode);
746 	struct pnfs_layout_hdr *lo;
747 
748 	spin_lock(&inode->i_lock);
749 	lo = NFS_I(inode)->layout;
750 	if (!pnfs_layout_is_valid(lo)) {
751 		spin_unlock(&inode->i_lock);
752 		rpc_exit(task, 0);
753 		return;
754 	}
755 	nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
756 	spin_unlock(&inode->i_lock);
757 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
758 			    &data->res.seq_res, task);
759 }
760 
761 static void
762 nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
763 {
764 	struct nfs42_layoutstat_data *data = calldata;
765 	struct inode *inode = data->inode;
766 	struct pnfs_layout_hdr *lo;
767 
768 	if (!nfs4_sequence_done(task, &data->res.seq_res))
769 		return;
770 
771 	switch (task->tk_status) {
772 	case 0:
773 		return;
774 	case -NFS4ERR_BADHANDLE:
775 	case -ESTALE:
776 		pnfs_destroy_layout(NFS_I(inode));
777 		break;
778 	case -NFS4ERR_EXPIRED:
779 	case -NFS4ERR_ADMIN_REVOKED:
780 	case -NFS4ERR_DELEG_REVOKED:
781 	case -NFS4ERR_STALE_STATEID:
782 	case -NFS4ERR_BAD_STATEID:
783 		spin_lock(&inode->i_lock);
784 		lo = NFS_I(inode)->layout;
785 		if (pnfs_layout_is_valid(lo) &&
786 		    nfs4_stateid_match(&data->args.stateid,
787 					     &lo->plh_stateid)) {
788 			LIST_HEAD(head);
789 
790 			/*
791 			 * Mark the bad layout state as invalid, then retry
792 			 * with the current stateid.
793 			 */
794 			pnfs_mark_layout_stateid_invalid(lo, &head);
795 			spin_unlock(&inode->i_lock);
796 			pnfs_free_lseg_list(&head);
797 			nfs_commit_inode(inode, 0);
798 		} else
799 			spin_unlock(&inode->i_lock);
800 		break;
801 	case -NFS4ERR_OLD_STATEID:
802 		spin_lock(&inode->i_lock);
803 		lo = NFS_I(inode)->layout;
804 		if (pnfs_layout_is_valid(lo) &&
805 		    nfs4_stateid_match_other(&data->args.stateid,
806 					&lo->plh_stateid)) {
807 			/* Do we need to delay before resending? */
808 			if (!nfs4_stateid_is_newer(&lo->plh_stateid,
809 						&data->args.stateid))
810 				rpc_delay(task, HZ);
811 			rpc_restart_call_prepare(task);
812 		}
813 		spin_unlock(&inode->i_lock);
814 		break;
815 	case -ENOTSUPP:
816 	case -EOPNOTSUPP:
817 		NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
818 	}
819 
820 	trace_nfs4_layoutstats(inode, &data->args.stateid, task->tk_status);
821 }
822 
823 static void
824 nfs42_layoutstat_release(void *calldata)
825 {
826 	struct nfs42_layoutstat_data *data = calldata;
827 	struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo;
828 	int i;
829 
830 	for (i = 0; i < data->args.num_dev; i++) {
831 		if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free)
832 			devinfo[i].ld_private.ops->free(&devinfo[i].ld_private);
833 	}
834 
835 	pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
836 	smp_mb__before_atomic();
837 	clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
838 	smp_mb__after_atomic();
839 	nfs_iput_and_deactive(data->inode);
840 	kfree(data->args.devinfo);
841 	kfree(data);
842 }
843 
844 static const struct rpc_call_ops nfs42_layoutstat_ops = {
845 	.rpc_call_prepare = nfs42_layoutstat_prepare,
846 	.rpc_call_done = nfs42_layoutstat_done,
847 	.rpc_release = nfs42_layoutstat_release,
848 };
849 
850 int nfs42_proc_layoutstats_generic(struct nfs_server *server,
851 				   struct nfs42_layoutstat_data *data)
852 {
853 	struct rpc_message msg = {
854 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
855 		.rpc_argp = &data->args,
856 		.rpc_resp = &data->res,
857 	};
858 	struct rpc_task_setup task_setup = {
859 		.rpc_client = server->client,
860 		.rpc_message = &msg,
861 		.callback_ops = &nfs42_layoutstat_ops,
862 		.callback_data = data,
863 		.flags = RPC_TASK_ASYNC,
864 	};
865 	struct rpc_task *task;
866 
867 	data->inode = nfs_igrab_and_active(data->args.inode);
868 	if (!data->inode) {
869 		nfs42_layoutstat_release(data);
870 		return -EAGAIN;
871 	}
872 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
873 	task = rpc_run_task(&task_setup);
874 	if (IS_ERR(task))
875 		return PTR_ERR(task);
876 	rpc_put_task(task);
877 	return 0;
878 }
879 
880 static struct nfs42_layouterror_data *
881 nfs42_alloc_layouterror_data(struct pnfs_layout_segment *lseg, gfp_t gfp_flags)
882 {
883 	struct nfs42_layouterror_data *data;
884 	struct inode *inode = lseg->pls_layout->plh_inode;
885 
886 	data = kzalloc(sizeof(*data), gfp_flags);
887 	if (data) {
888 		data->args.inode = data->inode = nfs_igrab_and_active(inode);
889 		if (data->inode) {
890 			data->lseg = pnfs_get_lseg(lseg);
891 			if (data->lseg)
892 				return data;
893 			nfs_iput_and_deactive(data->inode);
894 		}
895 		kfree(data);
896 	}
897 	return NULL;
898 }
899 
900 static void
901 nfs42_free_layouterror_data(struct nfs42_layouterror_data *data)
902 {
903 	pnfs_put_lseg(data->lseg);
904 	nfs_iput_and_deactive(data->inode);
905 	kfree(data);
906 }
907 
908 static void
909 nfs42_layouterror_prepare(struct rpc_task *task, void *calldata)
910 {
911 	struct nfs42_layouterror_data *data = calldata;
912 	struct inode *inode = data->inode;
913 	struct nfs_server *server = NFS_SERVER(inode);
914 	struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
915 	unsigned i;
916 
917 	spin_lock(&inode->i_lock);
918 	if (!pnfs_layout_is_valid(lo)) {
919 		spin_unlock(&inode->i_lock);
920 		rpc_exit(task, 0);
921 		return;
922 	}
923 	for (i = 0; i < data->args.num_errors; i++)
924 		nfs4_stateid_copy(&data->args.errors[i].stateid,
925 				&lo->plh_stateid);
926 	spin_unlock(&inode->i_lock);
927 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
928 			    &data->res.seq_res, task);
929 }
930 
931 static void
932 nfs42_layouterror_done(struct rpc_task *task, void *calldata)
933 {
934 	struct nfs42_layouterror_data *data = calldata;
935 	struct inode *inode = data->inode;
936 	struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
937 
938 	if (!nfs4_sequence_done(task, &data->res.seq_res))
939 		return;
940 
941 	switch (task->tk_status) {
942 	case 0:
943 		return;
944 	case -NFS4ERR_BADHANDLE:
945 	case -ESTALE:
946 		pnfs_destroy_layout(NFS_I(inode));
947 		break;
948 	case -NFS4ERR_EXPIRED:
949 	case -NFS4ERR_ADMIN_REVOKED:
950 	case -NFS4ERR_DELEG_REVOKED:
951 	case -NFS4ERR_STALE_STATEID:
952 	case -NFS4ERR_BAD_STATEID:
953 		spin_lock(&inode->i_lock);
954 		if (pnfs_layout_is_valid(lo) &&
955 		    nfs4_stateid_match(&data->args.errors[0].stateid,
956 					     &lo->plh_stateid)) {
957 			LIST_HEAD(head);
958 
959 			/*
960 			 * Mark the bad layout state as invalid, then retry
961 			 * with the current stateid.
962 			 */
963 			pnfs_mark_layout_stateid_invalid(lo, &head);
964 			spin_unlock(&inode->i_lock);
965 			pnfs_free_lseg_list(&head);
966 			nfs_commit_inode(inode, 0);
967 		} else
968 			spin_unlock(&inode->i_lock);
969 		break;
970 	case -NFS4ERR_OLD_STATEID:
971 		spin_lock(&inode->i_lock);
972 		if (pnfs_layout_is_valid(lo) &&
973 		    nfs4_stateid_match_other(&data->args.errors[0].stateid,
974 					&lo->plh_stateid)) {
975 			/* Do we need to delay before resending? */
976 			if (!nfs4_stateid_is_newer(&lo->plh_stateid,
977 						&data->args.errors[0].stateid))
978 				rpc_delay(task, HZ);
979 			rpc_restart_call_prepare(task);
980 		}
981 		spin_unlock(&inode->i_lock);
982 		break;
983 	case -ENOTSUPP:
984 	case -EOPNOTSUPP:
985 		NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTERROR;
986 	}
987 
988 	trace_nfs4_layouterror(inode, &data->args.errors[0].stateid,
989 			       task->tk_status);
990 }
991 
992 static void
993 nfs42_layouterror_release(void *calldata)
994 {
995 	struct nfs42_layouterror_data *data = calldata;
996 
997 	nfs42_free_layouterror_data(data);
998 }
999 
1000 static const struct rpc_call_ops nfs42_layouterror_ops = {
1001 	.rpc_call_prepare = nfs42_layouterror_prepare,
1002 	.rpc_call_done = nfs42_layouterror_done,
1003 	.rpc_release = nfs42_layouterror_release,
1004 };
1005 
1006 int nfs42_proc_layouterror(struct pnfs_layout_segment *lseg,
1007 		const struct nfs42_layout_error *errors, size_t n)
1008 {
1009 	struct inode *inode = lseg->pls_layout->plh_inode;
1010 	struct nfs42_layouterror_data *data;
1011 	struct rpc_task *task;
1012 	struct rpc_message msg = {
1013 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTERROR],
1014 	};
1015 	struct rpc_task_setup task_setup = {
1016 		.rpc_message = &msg,
1017 		.callback_ops = &nfs42_layouterror_ops,
1018 		.flags = RPC_TASK_ASYNC,
1019 	};
1020 	unsigned int i;
1021 
1022 	if (!nfs_server_capable(inode, NFS_CAP_LAYOUTERROR))
1023 		return -EOPNOTSUPP;
1024 	if (n > NFS42_LAYOUTERROR_MAX)
1025 		return -EINVAL;
1026 	data = nfs42_alloc_layouterror_data(lseg, nfs_io_gfp_mask());
1027 	if (!data)
1028 		return -ENOMEM;
1029 	for (i = 0; i < n; i++) {
1030 		data->args.errors[i] = errors[i];
1031 		data->args.num_errors++;
1032 		data->res.num_errors++;
1033 	}
1034 	msg.rpc_argp = &data->args;
1035 	msg.rpc_resp = &data->res;
1036 	task_setup.callback_data = data;
1037 	task_setup.rpc_client = NFS_SERVER(inode)->client;
1038 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
1039 	task = rpc_run_task(&task_setup);
1040 	if (IS_ERR(task))
1041 		return PTR_ERR(task);
1042 	rpc_put_task(task);
1043 	return 0;
1044 }
1045 EXPORT_SYMBOL_GPL(nfs42_proc_layouterror);
1046 
1047 static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
1048 		struct file *dst_f, struct nfs_lock_context *src_lock,
1049 		struct nfs_lock_context *dst_lock, loff_t src_offset,
1050 		loff_t dst_offset, loff_t count)
1051 {
1052 	struct inode *src_inode = file_inode(src_f);
1053 	struct inode *dst_inode = file_inode(dst_f);
1054 	struct nfs_server *server = NFS_SERVER(dst_inode);
1055 	__u32 dst_bitmask[NFS_BITMASK_SZ];
1056 	struct nfs42_clone_args args = {
1057 		.src_fh = NFS_FH(src_inode),
1058 		.dst_fh = NFS_FH(dst_inode),
1059 		.src_offset = src_offset,
1060 		.dst_offset = dst_offset,
1061 		.count = count,
1062 		.dst_bitmask = dst_bitmask,
1063 	};
1064 	struct nfs42_clone_res res = {
1065 		.server	= server,
1066 	};
1067 	int status;
1068 
1069 	msg->rpc_argp = &args;
1070 	msg->rpc_resp = &res;
1071 
1072 	status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
1073 			src_lock, FMODE_READ);
1074 	if (status) {
1075 		if (status == -EAGAIN)
1076 			status = -NFS4ERR_BAD_STATEID;
1077 		return status;
1078 	}
1079 	status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
1080 			dst_lock, FMODE_WRITE);
1081 	if (status) {
1082 		if (status == -EAGAIN)
1083 			status = -NFS4ERR_BAD_STATEID;
1084 		return status;
1085 	}
1086 
1087 	res.dst_fattr = nfs_alloc_fattr();
1088 	if (!res.dst_fattr)
1089 		return -ENOMEM;
1090 
1091 	nfs4_bitmask_set(dst_bitmask, server->cache_consistency_bitmask,
1092 			 dst_inode, NFS_INO_INVALID_BLOCKS);
1093 
1094 	status = nfs4_call_sync(server->client, server, msg,
1095 				&args.seq_args, &res.seq_res, 0);
1096 	trace_nfs4_clone(src_inode, dst_inode, &args, status);
1097 	if (status == 0) {
1098 		/* a zero-length count means clone to EOF in src */
1099 		if (count == 0 && res.dst_fattr->valid & NFS_ATTR_FATTR_SIZE)
1100 			count = nfs_size_to_loff_t(res.dst_fattr->size) - dst_offset;
1101 		nfs42_copy_dest_done(dst_inode, dst_offset, count);
1102 		status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
1103 	}
1104 
1105 	kfree(res.dst_fattr);
1106 	return status;
1107 }
1108 
1109 int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
1110 		     loff_t src_offset, loff_t dst_offset, loff_t count)
1111 {
1112 	struct rpc_message msg = {
1113 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
1114 	};
1115 	struct inode *inode = file_inode(src_f);
1116 	struct nfs_server *server = NFS_SERVER(file_inode(src_f));
1117 	struct nfs_lock_context *src_lock;
1118 	struct nfs_lock_context *dst_lock;
1119 	struct nfs4_exception src_exception = { };
1120 	struct nfs4_exception dst_exception = { };
1121 	int err, err2;
1122 
1123 	if (!nfs_server_capable(inode, NFS_CAP_CLONE))
1124 		return -EOPNOTSUPP;
1125 
1126 	src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
1127 	if (IS_ERR(src_lock))
1128 		return PTR_ERR(src_lock);
1129 
1130 	src_exception.inode = file_inode(src_f);
1131 	src_exception.state = src_lock->open_context->state;
1132 
1133 	dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
1134 	if (IS_ERR(dst_lock)) {
1135 		err = PTR_ERR(dst_lock);
1136 		goto out_put_src_lock;
1137 	}
1138 
1139 	dst_exception.inode = file_inode(dst_f);
1140 	dst_exception.state = dst_lock->open_context->state;
1141 
1142 	do {
1143 		err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
1144 					src_offset, dst_offset, count);
1145 		if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
1146 			NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
1147 			err = -EOPNOTSUPP;
1148 			break;
1149 		}
1150 
1151 		err2 = nfs4_handle_exception(server, err, &src_exception);
1152 		err = nfs4_handle_exception(server, err, &dst_exception);
1153 		if (!err)
1154 			err = err2;
1155 	} while (src_exception.retry || dst_exception.retry);
1156 
1157 	nfs_put_lock_context(dst_lock);
1158 out_put_src_lock:
1159 	nfs_put_lock_context(src_lock);
1160 	return err;
1161 }
1162 
1163 #define NFS4XATTR_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
1164 
1165 static int _nfs42_proc_removexattr(struct inode *inode, const char *name)
1166 {
1167 	struct nfs_server *server = NFS_SERVER(inode);
1168 	struct nfs42_removexattrargs args = {
1169 		.fh = NFS_FH(inode),
1170 		.xattr_name = name,
1171 	};
1172 	struct nfs42_removexattrres res;
1173 	struct rpc_message msg = {
1174 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVEXATTR],
1175 		.rpc_argp = &args,
1176 		.rpc_resp = &res,
1177 	};
1178 	int ret;
1179 	unsigned long timestamp = jiffies;
1180 
1181 	ret = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
1182 	    &res.seq_res, 1);
1183 	trace_nfs4_removexattr(inode, name, ret);
1184 	if (!ret)
1185 		nfs4_update_changeattr(inode, &res.cinfo, timestamp, 0);
1186 
1187 	return ret;
1188 }
1189 
1190 static int _nfs42_proc_setxattr(struct inode *inode, const char *name,
1191 				const void *buf, size_t buflen, int flags)
1192 {
1193 	struct nfs_server *server = NFS_SERVER(inode);
1194 	__u32 bitmask[NFS_BITMASK_SZ];
1195 	struct page *pages[NFS4XATTR_MAXPAGES];
1196 	struct nfs42_setxattrargs arg = {
1197 		.fh		= NFS_FH(inode),
1198 		.bitmask	= bitmask,
1199 		.xattr_pages	= pages,
1200 		.xattr_len	= buflen,
1201 		.xattr_name	= name,
1202 		.xattr_flags	= flags,
1203 	};
1204 	struct nfs42_setxattrres res = {
1205 		.server		= server,
1206 	};
1207 	struct rpc_message msg = {
1208 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETXATTR],
1209 		.rpc_argp	= &arg,
1210 		.rpc_resp	= &res,
1211 	};
1212 	int ret, np;
1213 	unsigned long timestamp = jiffies;
1214 
1215 	if (buflen > server->sxasize)
1216 		return -ERANGE;
1217 
1218 	res.fattr = nfs_alloc_fattr();
1219 	if (!res.fattr)
1220 		return -ENOMEM;
1221 
1222 	if (buflen > 0) {
1223 		np = nfs4_buf_to_pages_noslab(buf, buflen, arg.xattr_pages);
1224 		if (np < 0) {
1225 			ret = np;
1226 			goto out;
1227 		}
1228 	} else
1229 		np = 0;
1230 
1231 	nfs4_bitmask_set(bitmask, server->cache_consistency_bitmask,
1232 			 inode, NFS_INO_INVALID_CHANGE);
1233 
1234 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1235 	    &res.seq_res, 1);
1236 	trace_nfs4_setxattr(inode, name, ret);
1237 
1238 	for (; np > 0; np--)
1239 		put_page(pages[np - 1]);
1240 
1241 	if (!ret) {
1242 		nfs4_update_changeattr(inode, &res.cinfo, timestamp, 0);
1243 		ret = nfs_post_op_update_inode(inode, res.fattr);
1244 	}
1245 
1246 out:
1247 	kfree(res.fattr);
1248 	return ret;
1249 }
1250 
1251 static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,
1252 				void *buf, size_t buflen, struct page **pages,
1253 				size_t plen)
1254 {
1255 	struct nfs_server *server = NFS_SERVER(inode);
1256 	struct nfs42_getxattrargs arg = {
1257 		.fh		= NFS_FH(inode),
1258 		.xattr_name	= name,
1259 	};
1260 	struct nfs42_getxattrres res;
1261 	struct rpc_message msg = {
1262 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_GETXATTR],
1263 		.rpc_argp	= &arg,
1264 		.rpc_resp	= &res,
1265 	};
1266 	ssize_t ret;
1267 
1268 	arg.xattr_len = plen;
1269 	arg.xattr_pages = pages;
1270 
1271 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1272 	    &res.seq_res, 0);
1273 	trace_nfs4_getxattr(inode, name, ret);
1274 	if (ret < 0)
1275 		return ret;
1276 
1277 	/*
1278 	 * Normally, the caching is done one layer up, but for successful
1279 	 * RPCS, always cache the result here, even if the caller was
1280 	 * just querying the length, or if the reply was too big for
1281 	 * the caller. This avoids a second RPC in the case of the
1282 	 * common query-alloc-retrieve cycle for xattrs.
1283 	 *
1284 	 * Note that xattr_len is always capped to XATTR_SIZE_MAX.
1285 	 */
1286 
1287 	nfs4_xattr_cache_add(inode, name, NULL, pages, res.xattr_len);
1288 
1289 	if (buflen) {
1290 		if (res.xattr_len > buflen)
1291 			return -ERANGE;
1292 		_copy_from_pages(buf, pages, 0, res.xattr_len);
1293 	}
1294 
1295 	return res.xattr_len;
1296 }
1297 
1298 static ssize_t _nfs42_proc_listxattrs(struct inode *inode, void *buf,
1299 				 size_t buflen, u64 *cookiep, bool *eofp)
1300 {
1301 	struct nfs_server *server = NFS_SERVER(inode);
1302 	struct page **pages;
1303 	struct nfs42_listxattrsargs arg = {
1304 		.fh		= NFS_FH(inode),
1305 		.cookie		= *cookiep,
1306 	};
1307 	struct nfs42_listxattrsres res = {
1308 		.eof = false,
1309 		.xattr_buf = buf,
1310 		.xattr_len = buflen,
1311 	};
1312 	struct rpc_message msg = {
1313 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_LISTXATTRS],
1314 		.rpc_argp	= &arg,
1315 		.rpc_resp	= &res,
1316 	};
1317 	u32 xdrlen;
1318 	int ret, np, i;
1319 
1320 
1321 	ret = -ENOMEM;
1322 	res.scratch = alloc_page(GFP_KERNEL);
1323 	if (!res.scratch)
1324 		goto out;
1325 
1326 	xdrlen = nfs42_listxattr_xdrsize(buflen);
1327 	if (xdrlen > server->lxasize)
1328 		xdrlen = server->lxasize;
1329 	np = xdrlen / PAGE_SIZE + 1;
1330 
1331 	pages = kcalloc(np, sizeof(struct page *), GFP_KERNEL);
1332 	if (!pages)
1333 		goto out_free_scratch;
1334 	for (i = 0; i < np; i++) {
1335 		pages[i] = alloc_page(GFP_KERNEL);
1336 		if (!pages[i])
1337 			goto out_free_pages;
1338 	}
1339 
1340 	arg.xattr_pages = pages;
1341 	arg.count = xdrlen;
1342 
1343 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1344 	    &res.seq_res, 0);
1345 	trace_nfs4_listxattr(inode, ret);
1346 
1347 	if (ret >= 0) {
1348 		ret = res.copied;
1349 		*cookiep = res.cookie;
1350 		*eofp = res.eof;
1351 	}
1352 
1353 out_free_pages:
1354 	while (--np >= 0) {
1355 		if (pages[np])
1356 			__free_page(pages[np]);
1357 	}
1358 	kfree(pages);
1359 out_free_scratch:
1360 	__free_page(res.scratch);
1361 out:
1362 	return ret;
1363 
1364 }
1365 
1366 ssize_t nfs42_proc_getxattr(struct inode *inode, const char *name,
1367 			      void *buf, size_t buflen)
1368 {
1369 	struct nfs4_exception exception = { };
1370 	ssize_t err, np, i;
1371 	struct page **pages;
1372 
1373 	np = nfs_page_array_len(0, buflen ?: XATTR_SIZE_MAX);
1374 	pages = kmalloc_array(np, sizeof(*pages), GFP_KERNEL);
1375 	if (!pages)
1376 		return -ENOMEM;
1377 
1378 	for (i = 0; i < np; i++) {
1379 		pages[i] = alloc_page(GFP_KERNEL);
1380 		if (!pages[i]) {
1381 			err = -ENOMEM;
1382 			goto out;
1383 		}
1384 	}
1385 
1386 	/*
1387 	 * The GETXATTR op has no length field in the call, and the
1388 	 * xattr data is at the end of the reply.
1389 	 *
1390 	 * There is no downside in using the page-aligned length. It will
1391 	 * allow receiving and caching xattrs that are too large for the
1392 	 * caller but still fit in the page-rounded value.
1393 	 */
1394 	do {
1395 		err = _nfs42_proc_getxattr(inode, name, buf, buflen,
1396 			pages, np * PAGE_SIZE);
1397 		if (err >= 0)
1398 			break;
1399 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
1400 				&exception);
1401 	} while (exception.retry);
1402 
1403 out:
1404 	while (--i >= 0)
1405 		__free_page(pages[i]);
1406 	kfree(pages);
1407 
1408 	return err;
1409 }
1410 
1411 int nfs42_proc_setxattr(struct inode *inode, const char *name,
1412 			      const void *buf, size_t buflen, int flags)
1413 {
1414 	struct nfs4_exception exception = { };
1415 	int err;
1416 
1417 	do {
1418 		err = _nfs42_proc_setxattr(inode, name, buf, buflen, flags);
1419 		if (!err)
1420 			break;
1421 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
1422 				&exception);
1423 	} while (exception.retry);
1424 
1425 	return err;
1426 }
1427 
1428 ssize_t nfs42_proc_listxattrs(struct inode *inode, void *buf,
1429 			      size_t buflen, u64 *cookiep, bool *eofp)
1430 {
1431 	struct nfs4_exception exception = { };
1432 	ssize_t err;
1433 
1434 	do {
1435 		err = _nfs42_proc_listxattrs(inode, buf, buflen,
1436 		    cookiep, eofp);
1437 		if (err >= 0)
1438 			break;
1439 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
1440 				&exception);
1441 	} while (exception.retry);
1442 
1443 	return err;
1444 }
1445 
1446 int nfs42_proc_removexattr(struct inode *inode, const char *name)
1447 {
1448 	struct nfs4_exception exception = { };
1449 	int err;
1450 
1451 	do {
1452 		err = _nfs42_proc_removexattr(inode, name);
1453 		if (!err)
1454 			break;
1455 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
1456 				&exception);
1457 	} while (exception.retry);
1458 
1459 	return err;
1460 }
1461