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