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