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