1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
6 #include <linux/sunrpc/addr.h>
7 #include <linux/sunrpc/sched.h>
9 #include <linux/nfs3.h>
10 #include <linux/nfs4.h>
11 #include <linux/nfs_xdr.h>
12 #include <linux/nfs_fs.h>
17 #include "nfs4session.h"
19 #include "delegation.h"
20 #include "nfs4trace.h"
22 #define NFSDBG_FACILITY NFSDBG_PROC
23 static int nfs42_do_offload_cancel_async(struct file *dst, nfs4_stateid *std);
25 static void nfs42_set_netaddr(struct file *filep, struct nfs42_netaddr *naddr)
27 struct nfs_client *clp = (NFS_SERVER(file_inode(filep)))->nfs_client;
28 unsigned short port = 2049;
31 naddr->netid_len = scnprintf(naddr->netid,
32 sizeof(naddr->netid), "%s",
33 rpc_peeraddr2str(clp->cl_rpcclient,
35 naddr->addr_len = scnprintf(naddr->addr,
38 rpc_peeraddr2str(clp->cl_rpcclient,
40 port >> 8, port & 255);
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)
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,
54 .falloc_bitmask = bitmask,
56 struct nfs42_falloc_res res = {
57 .falloc_server = server,
61 msg->rpc_argp = &args;
64 status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
67 if (status == -EAGAIN)
68 status = -NFS4ERR_BAD_STATEID;
72 nfs4_bitmask_set(bitmask, server->cache_consistency_bitmask, inode,
73 NFS_INO_INVALID_BLOCKS);
75 res.falloc_fattr = nfs_alloc_fattr();
76 if (!res.falloc_fattr)
79 status = nfs4_call_sync(server->client, server, msg,
80 &args.seq_args, &res.seq_res, 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);
87 status = nfs_post_op_update_inode_force_wcc(inode,
90 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE])
91 trace_nfs4_fallocate(inode, &args, status);
93 trace_nfs4_deallocate(inode, &args, status);
94 kfree(res.falloc_fattr);
98 static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
99 loff_t offset, loff_t len)
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;
107 lock = nfs_get_lock_context(nfs_file_open_context(filep));
109 return PTR_ERR(lock);
111 exception.inode = inode;
112 exception.state = lock->open_context->state;
114 err = nfs_sync_inode(inode);
119 err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
120 if (err == -ENOTSUPP) {
124 err = nfs4_handle_exception(server, err, &exception);
125 } while (exception.retry);
127 nfs_put_lock_context(lock);
131 int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
133 struct rpc_message msg = {
134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
136 struct inode *inode = file_inode(filep);
139 if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
144 err = nfs42_proc_fallocate(&msg, filep, offset, len);
145 if (err == -EOPNOTSUPP)
146 NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
152 int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
154 struct rpc_message msg = {
155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
157 struct inode *inode = file_inode(filep);
160 if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
165 err = nfs42_proc_fallocate(&msg, filep, offset, len);
167 truncate_pagecache_range(inode, offset, (offset + len) -1);
168 if (err == -EOPNOTSUPP)
169 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
175 static int handle_async_copy(struct nfs42_copy_res *res,
176 struct nfs_server *dst_server,
177 struct nfs_server *src_server,
180 nfs4_stateid *src_stateid,
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);
188 copy = kzalloc(sizeof(struct nfs4_copy_state), GFP_KERNEL);
192 spin_lock(&dst_server->nfs_client->cl_lock);
193 list_for_each_entry(iter,
194 &dst_server->nfs_client->pending_cb_stateids,
196 if (memcmp(&res->write_res.stateid, &iter->stateid,
200 list_del(&iter->copies);
204 spin_unlock(&dst_server->nfs_client->cl_lock);
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;
215 list_add_tail(©->copies, &dst_server->ss_copies);
216 spin_unlock(&dst_server->nfs_client->cl_lock);
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);
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);
233 if (status == -ERESTARTSYS) {
235 } else if (copy->flags || copy->error == NFS4ERR_PARTNER_NO_AUTH) {
241 res->write_res.count = copy->count;
242 memcpy(&res->write_res.verifier, ©->verf, sizeof(copy->verf));
243 status = -copy->error;
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);
255 static int process_copy_commit(struct file *dst, loff_t pos_dst,
256 struct nfs42_copy_res *res)
258 struct nfs_commitres cres;
259 int status = -ENOMEM;
261 cres.verf = kzalloc(sizeof(struct nfs_writeverf), GFP_KERNEL);
265 status = nfs4_proc_commit(dst, pos_dst, res->write_res.count, &cres);
268 if (nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
269 &cres.verf->verifier)) {
270 dprintk("commit verf differs from copy verf\n");
280 * nfs42_copy_dest_done - perform inode cache updates after clone/copy offload
281 * @inode: pointer to destination inode
282 * @pos: destination offset
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.
290 static void nfs42_copy_dest_done(struct inode *inode, loff_t pos, loff_t len)
292 loff_t newsize = pos + len;
293 loff_t end = newsize - 1;
295 WARN_ON_ONCE(invalidate_inode_pages2_range(inode->i_mapping,
296 pos >> PAGE_SHIFT, end >> PAGE_SHIFT));
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);
308 static ssize_t _nfs42_proc_copy(struct file *src,
309 struct nfs_lock_context *src_lock,
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,
318 struct rpc_message msg = {
319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
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;
334 nfs4_stateid_copy(&args->src_stateid, cnr_stateid);
336 status = nfs4_set_rw_stateid(&args->src_stateid,
337 src_lock->open_context, src_lock, FMODE_READ);
339 if (status == -EAGAIN)
340 status = -NFS4ERR_BAD_STATEID;
344 status = nfs_filemap_write_and_wait_range(src->f_mapping,
345 pos_src, pos_src + (loff_t)count - 1);
349 status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context,
350 dst_lock, FMODE_WRITE);
352 if (status == -EAGAIN)
353 status = -NFS4ERR_BAD_STATEID;
357 status = nfs_sync_inode(dst_inode);
361 res->commit_res.verf = NULL;
363 res->commit_res.verf =
364 kzalloc(sizeof(struct nfs_writeverf), GFP_KERNEL);
365 if (!res->commit_res.verf)
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);
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;
382 nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
383 &res->commit_res.verf->verifier)) {
388 if (!res->synchronous) {
389 status = handle_async_copy(res, dst_server, src_server, src,
390 dst, &args->src_stateid, restart);
395 if ((!res->synchronous || !args->sync) &&
396 res->write_res.verifier.committed != NFS_FILE_SYNC) {
397 status = process_copy_commit(dst, pos_dst, res);
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;
407 kfree(res->commit_res.verf);
411 ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
412 struct file *dst, loff_t pos_dst, size_t count,
413 struct nl4_server *nss,
414 nfs4_stateid *cnr_stateid, bool sync)
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)),
422 .dst_fh = NFS_FH(file_inode(dst)),
427 struct nfs42_copy_res res;
428 struct nfs4_exception src_exception = {
429 .inode = file_inode(src),
430 .stateid = &args.src_stateid,
432 struct nfs4_exception dst_exception = {
433 .inode = file_inode(dst),
434 .stateid = &args.dst_stateid,
437 bool restart = false;
439 src_lock = nfs_get_lock_context(nfs_file_open_context(src));
440 if (IS_ERR(src_lock))
441 return PTR_ERR(src_lock);
443 src_exception.state = src_lock->open_context->state;
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;
451 dst_exception.state = dst_lock->open_context->state;
454 inode_lock(file_inode(dst));
455 err = _nfs42_proc_copy(src, src_lock,
458 nss, cnr_stateid, &restart);
459 inode_unlock(file_inode(dst));
463 if (err == -ENOTSUPP &&
464 nfs42_files_from_same_server(src, dst)) {
467 } else if (err == -EAGAIN) {
469 dst_exception.retry = 1;
473 } else if (err == -NFS4ERR_OFFLOAD_NO_REQS &&
474 args.sync != res.synchronous) {
475 args.sync = res.synchronous;
476 dst_exception.retry = 1;
478 } else if ((err == -ESTALE ||
479 err == -NFS4ERR_OFFLOAD_DENIED ||
481 !nfs42_files_from_same_server(src, dst)) {
482 nfs42_do_offload_cancel_async(src, &args.src_stateid);
487 err2 = nfs4_handle_exception(server, err, &src_exception);
488 err = nfs4_handle_exception(server, err, &dst_exception);
491 } while (src_exception.retry || dst_exception.retry);
493 nfs_put_lock_context(dst_lock);
495 nfs_put_lock_context(src_lock);
499 struct nfs42_offloadcancel_data {
500 struct nfs_server *seq_server;
501 struct nfs42_offload_status_args args;
502 struct nfs42_offload_status_res res;
505 static void nfs42_offload_cancel_prepare(struct rpc_task *task, void *calldata)
507 struct nfs42_offloadcancel_data *data = calldata;
509 nfs4_setup_sequence(data->seq_server->nfs_client,
510 &data->args.osa_seq_args,
511 &data->res.osr_seq_res, task);
514 static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata)
516 struct nfs42_offloadcancel_data *data = calldata;
518 trace_nfs4_offload_cancel(&data->args, task->tk_status);
519 nfs41_sequence_done(task, &data->res.osr_seq_res);
520 if (task->tk_status &&
521 nfs4_async_handle_error(task, data->seq_server, NULL,
523 rpc_restart_call_prepare(task);
526 static void nfs42_free_offloadcancel_data(void *data)
531 static const struct rpc_call_ops nfs42_offload_cancel_ops = {
532 .rpc_call_prepare = nfs42_offload_cancel_prepare,
533 .rpc_call_done = nfs42_offload_cancel_done,
534 .rpc_release = nfs42_free_offloadcancel_data,
537 static int nfs42_do_offload_cancel_async(struct file *dst,
538 nfs4_stateid *stateid)
540 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
541 struct nfs42_offloadcancel_data *data = NULL;
542 struct nfs_open_context *ctx = nfs_file_open_context(dst);
543 struct rpc_task *task;
544 struct rpc_message msg = {
545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_CANCEL],
546 .rpc_cred = ctx->cred,
548 struct rpc_task_setup task_setup_data = {
549 .rpc_client = dst_server->client,
551 .callback_ops = &nfs42_offload_cancel_ops,
552 .workqueue = nfsiod_workqueue,
553 .flags = RPC_TASK_ASYNC,
557 if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL))
560 data = kzalloc(sizeof(struct nfs42_offloadcancel_data), GFP_KERNEL);
564 data->seq_server = dst_server;
565 data->args.osa_src_fh = NFS_FH(file_inode(dst));
566 memcpy(&data->args.osa_stateid, stateid,
567 sizeof(data->args.osa_stateid));
568 msg.rpc_argp = &data->args;
569 msg.rpc_resp = &data->res;
570 task_setup_data.callback_data = data;
571 nfs4_init_sequence(&data->args.osa_seq_args, &data->res.osr_seq_res,
573 task = rpc_run_task(&task_setup_data);
575 return PTR_ERR(task);
576 status = rpc_wait_for_completion_task(task);
577 if (status == -ENOTSUPP)
578 dst_server->caps &= ~NFS_CAP_OFFLOAD_CANCEL;
583 static int _nfs42_proc_copy_notify(struct file *src, struct file *dst,
584 struct nfs42_copy_notify_args *args,
585 struct nfs42_copy_notify_res *res)
587 struct nfs_server *src_server = NFS_SERVER(file_inode(src));
588 struct rpc_message msg = {
589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY_NOTIFY],
594 struct nfs_open_context *ctx;
595 struct nfs_lock_context *l_ctx;
597 ctx = get_nfs_open_context(nfs_file_open_context(src));
598 l_ctx = nfs_get_lock_context(ctx);
600 status = PTR_ERR(l_ctx);
604 status = nfs4_set_rw_stateid(&args->cna_src_stateid, ctx, l_ctx,
606 nfs_put_lock_context(l_ctx);
608 if (status == -EAGAIN)
609 status = -NFS4ERR_BAD_STATEID;
613 status = nfs4_call_sync(src_server->client, src_server, &msg,
614 &args->cna_seq_args, &res->cnr_seq_res, 0);
615 trace_nfs4_copy_notify(file_inode(src), args, res, status);
616 if (status == -ENOTSUPP)
617 src_server->caps &= ~NFS_CAP_COPY_NOTIFY;
620 put_nfs_open_context(nfs_file_open_context(src));
624 int nfs42_proc_copy_notify(struct file *src, struct file *dst,
625 struct nfs42_copy_notify_res *res)
627 struct nfs_server *src_server = NFS_SERVER(file_inode(src));
628 struct nfs42_copy_notify_args *args;
629 struct nfs4_exception exception = {
630 .inode = file_inode(src),
634 if (!(src_server->caps & NFS_CAP_COPY_NOTIFY))
637 args = kzalloc(sizeof(struct nfs42_copy_notify_args), GFP_KERNEL);
641 args->cna_src_fh = NFS_FH(file_inode(src)),
642 args->cna_dst.nl4_type = NL4_NETADDR;
643 nfs42_set_netaddr(dst, &args->cna_dst.u.nl4_addr);
644 exception.stateid = &args->cna_src_stateid;
647 status = _nfs42_proc_copy_notify(src, dst, args, res);
648 if (status == -ENOTSUPP) {
649 status = -EOPNOTSUPP;
652 status = nfs4_handle_exception(src_server, status, &exception);
653 } while (exception.retry);
660 static loff_t _nfs42_proc_llseek(struct file *filep,
661 struct nfs_lock_context *lock, loff_t offset, int whence)
663 struct inode *inode = file_inode(filep);
664 struct nfs42_seek_args args = {
665 .sa_fh = NFS_FH(inode),
667 .sa_what = (whence == SEEK_HOLE) ?
668 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
670 struct nfs42_seek_res res;
671 struct rpc_message msg = {
672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
676 struct nfs_server *server = NFS_SERVER(inode);
679 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
682 status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
685 if (status == -EAGAIN)
686 status = -NFS4ERR_BAD_STATEID;
690 status = nfs_filemap_write_and_wait_range(inode->i_mapping,
695 status = nfs4_call_sync(server->client, server, &msg,
696 &args.seq_args, &res.seq_res, 0);
697 trace_nfs4_llseek(inode, &args, &res, status);
698 if (status == -ENOTSUPP)
699 server->caps &= ~NFS_CAP_SEEK;
703 if (whence == SEEK_DATA && res.sr_eof)
704 return -NFS4ERR_NXIO;
706 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
709 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
711 struct nfs_server *server = NFS_SERVER(file_inode(filep));
712 struct nfs4_exception exception = { };
713 struct nfs_lock_context *lock;
716 lock = nfs_get_lock_context(nfs_file_open_context(filep));
718 return PTR_ERR(lock);
720 exception.inode = file_inode(filep);
721 exception.state = lock->open_context->state;
724 err = _nfs42_proc_llseek(filep, lock, offset, whence);
727 if (err == -ENOTSUPP) {
731 err = nfs4_handle_exception(server, err, &exception);
732 } while (exception.retry);
734 nfs_put_lock_context(lock);
740 nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
742 struct nfs42_layoutstat_data *data = calldata;
743 struct inode *inode = data->inode;
744 struct nfs_server *server = NFS_SERVER(inode);
745 struct pnfs_layout_hdr *lo;
747 spin_lock(&inode->i_lock);
748 lo = NFS_I(inode)->layout;
749 if (!pnfs_layout_is_valid(lo)) {
750 spin_unlock(&inode->i_lock);
754 nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
755 spin_unlock(&inode->i_lock);
756 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
757 &data->res.seq_res, task);
761 nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
763 struct nfs42_layoutstat_data *data = calldata;
764 struct inode *inode = data->inode;
765 struct pnfs_layout_hdr *lo;
767 if (!nfs4_sequence_done(task, &data->res.seq_res))
770 switch (task->tk_status) {
773 case -NFS4ERR_BADHANDLE:
775 pnfs_destroy_layout(NFS_I(inode));
777 case -NFS4ERR_EXPIRED:
778 case -NFS4ERR_ADMIN_REVOKED:
779 case -NFS4ERR_DELEG_REVOKED:
780 case -NFS4ERR_STALE_STATEID:
781 case -NFS4ERR_BAD_STATEID:
782 spin_lock(&inode->i_lock);
783 lo = NFS_I(inode)->layout;
784 if (pnfs_layout_is_valid(lo) &&
785 nfs4_stateid_match(&data->args.stateid,
790 * Mark the bad layout state as invalid, then retry
791 * with the current stateid.
793 pnfs_mark_layout_stateid_invalid(lo, &head);
794 spin_unlock(&inode->i_lock);
795 pnfs_free_lseg_list(&head);
796 nfs_commit_inode(inode, 0);
798 spin_unlock(&inode->i_lock);
800 case -NFS4ERR_OLD_STATEID:
801 spin_lock(&inode->i_lock);
802 lo = NFS_I(inode)->layout;
803 if (pnfs_layout_is_valid(lo) &&
804 nfs4_stateid_match_other(&data->args.stateid,
806 /* Do we need to delay before resending? */
807 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
808 &data->args.stateid))
810 rpc_restart_call_prepare(task);
812 spin_unlock(&inode->i_lock);
816 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
819 trace_nfs4_layoutstats(inode, &data->args.stateid, task->tk_status);
823 nfs42_layoutstat_release(void *calldata)
825 struct nfs42_layoutstat_data *data = calldata;
826 struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo;
829 for (i = 0; i < data->args.num_dev; i++) {
830 if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free)
831 devinfo[i].ld_private.ops->free(&devinfo[i].ld_private);
834 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
835 smp_mb__before_atomic();
836 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
837 smp_mb__after_atomic();
838 nfs_iput_and_deactive(data->inode);
839 kfree(data->args.devinfo);
843 static const struct rpc_call_ops nfs42_layoutstat_ops = {
844 .rpc_call_prepare = nfs42_layoutstat_prepare,
845 .rpc_call_done = nfs42_layoutstat_done,
846 .rpc_release = nfs42_layoutstat_release,
849 int nfs42_proc_layoutstats_generic(struct nfs_server *server,
850 struct nfs42_layoutstat_data *data)
852 struct rpc_message msg = {
853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
854 .rpc_argp = &data->args,
855 .rpc_resp = &data->res,
857 struct rpc_task_setup task_setup = {
858 .rpc_client = server->client,
860 .callback_ops = &nfs42_layoutstat_ops,
861 .callback_data = data,
862 .flags = RPC_TASK_ASYNC,
864 struct rpc_task *task;
866 data->inode = nfs_igrab_and_active(data->args.inode);
868 nfs42_layoutstat_release(data);
871 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
872 task = rpc_run_task(&task_setup);
874 return PTR_ERR(task);
879 static struct nfs42_layouterror_data *
880 nfs42_alloc_layouterror_data(struct pnfs_layout_segment *lseg, gfp_t gfp_flags)
882 struct nfs42_layouterror_data *data;
883 struct inode *inode = lseg->pls_layout->plh_inode;
885 data = kzalloc(sizeof(*data), gfp_flags);
887 data->args.inode = data->inode = nfs_igrab_and_active(inode);
889 data->lseg = pnfs_get_lseg(lseg);
892 nfs_iput_and_deactive(data->inode);
900 nfs42_free_layouterror_data(struct nfs42_layouterror_data *data)
902 pnfs_put_lseg(data->lseg);
903 nfs_iput_and_deactive(data->inode);
908 nfs42_layouterror_prepare(struct rpc_task *task, void *calldata)
910 struct nfs42_layouterror_data *data = calldata;
911 struct inode *inode = data->inode;
912 struct nfs_server *server = NFS_SERVER(inode);
913 struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
916 spin_lock(&inode->i_lock);
917 if (!pnfs_layout_is_valid(lo)) {
918 spin_unlock(&inode->i_lock);
922 for (i = 0; i < data->args.num_errors; i++)
923 nfs4_stateid_copy(&data->args.errors[i].stateid,
925 spin_unlock(&inode->i_lock);
926 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
927 &data->res.seq_res, task);
931 nfs42_layouterror_done(struct rpc_task *task, void *calldata)
933 struct nfs42_layouterror_data *data = calldata;
934 struct inode *inode = data->inode;
935 struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
937 if (!nfs4_sequence_done(task, &data->res.seq_res))
940 switch (task->tk_status) {
943 case -NFS4ERR_BADHANDLE:
945 pnfs_destroy_layout(NFS_I(inode));
947 case -NFS4ERR_EXPIRED:
948 case -NFS4ERR_ADMIN_REVOKED:
949 case -NFS4ERR_DELEG_REVOKED:
950 case -NFS4ERR_STALE_STATEID:
951 case -NFS4ERR_BAD_STATEID:
952 spin_lock(&inode->i_lock);
953 if (pnfs_layout_is_valid(lo) &&
954 nfs4_stateid_match(&data->args.errors[0].stateid,
959 * Mark the bad layout state as invalid, then retry
960 * with the current stateid.
962 pnfs_mark_layout_stateid_invalid(lo, &head);
963 spin_unlock(&inode->i_lock);
964 pnfs_free_lseg_list(&head);
965 nfs_commit_inode(inode, 0);
967 spin_unlock(&inode->i_lock);
969 case -NFS4ERR_OLD_STATEID:
970 spin_lock(&inode->i_lock);
971 if (pnfs_layout_is_valid(lo) &&
972 nfs4_stateid_match_other(&data->args.errors[0].stateid,
974 /* Do we need to delay before resending? */
975 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
976 &data->args.errors[0].stateid))
978 rpc_restart_call_prepare(task);
980 spin_unlock(&inode->i_lock);
984 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTERROR;
987 trace_nfs4_layouterror(inode, &data->args.errors[0].stateid,
992 nfs42_layouterror_release(void *calldata)
994 struct nfs42_layouterror_data *data = calldata;
996 nfs42_free_layouterror_data(data);
999 static const struct rpc_call_ops nfs42_layouterror_ops = {
1000 .rpc_call_prepare = nfs42_layouterror_prepare,
1001 .rpc_call_done = nfs42_layouterror_done,
1002 .rpc_release = nfs42_layouterror_release,
1005 int nfs42_proc_layouterror(struct pnfs_layout_segment *lseg,
1006 const struct nfs42_layout_error *errors, size_t n)
1008 struct inode *inode = lseg->pls_layout->plh_inode;
1009 struct nfs42_layouterror_data *data;
1010 struct rpc_task *task;
1011 struct rpc_message msg = {
1012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTERROR],
1014 struct rpc_task_setup task_setup = {
1015 .rpc_message = &msg,
1016 .callback_ops = &nfs42_layouterror_ops,
1017 .flags = RPC_TASK_ASYNC,
1021 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTERROR))
1023 if (n > NFS42_LAYOUTERROR_MAX)
1025 data = nfs42_alloc_layouterror_data(lseg, nfs_io_gfp_mask());
1028 for (i = 0; i < n; i++) {
1029 data->args.errors[i] = errors[i];
1030 data->args.num_errors++;
1031 data->res.num_errors++;
1033 msg.rpc_argp = &data->args;
1034 msg.rpc_resp = &data->res;
1035 task_setup.callback_data = data;
1036 task_setup.rpc_client = NFS_SERVER(inode)->client;
1037 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
1038 task = rpc_run_task(&task_setup);
1040 return PTR_ERR(task);
1044 EXPORT_SYMBOL_GPL(nfs42_proc_layouterror);
1046 static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
1047 struct file *dst_f, struct nfs_lock_context *src_lock,
1048 struct nfs_lock_context *dst_lock, loff_t src_offset,
1049 loff_t dst_offset, loff_t count)
1051 struct inode *src_inode = file_inode(src_f);
1052 struct inode *dst_inode = file_inode(dst_f);
1053 struct nfs_server *server = NFS_SERVER(dst_inode);
1054 __u32 dst_bitmask[NFS_BITMASK_SZ];
1055 struct nfs42_clone_args args = {
1056 .src_fh = NFS_FH(src_inode),
1057 .dst_fh = NFS_FH(dst_inode),
1058 .src_offset = src_offset,
1059 .dst_offset = dst_offset,
1061 .dst_bitmask = dst_bitmask,
1063 struct nfs42_clone_res res = {
1068 msg->rpc_argp = &args;
1069 msg->rpc_resp = &res;
1071 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
1072 src_lock, FMODE_READ);
1074 if (status == -EAGAIN)
1075 status = -NFS4ERR_BAD_STATEID;
1078 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
1079 dst_lock, FMODE_WRITE);
1081 if (status == -EAGAIN)
1082 status = -NFS4ERR_BAD_STATEID;
1086 res.dst_fattr = nfs_alloc_fattr();
1090 nfs4_bitmask_set(dst_bitmask, server->cache_consistency_bitmask,
1091 dst_inode, NFS_INO_INVALID_BLOCKS);
1093 status = nfs4_call_sync(server->client, server, msg,
1094 &args.seq_args, &res.seq_res, 0);
1095 trace_nfs4_clone(src_inode, dst_inode, &args, status);
1097 /* a zero-length count means clone to EOF in src */
1098 if (count == 0 && res.dst_fattr->valid & NFS_ATTR_FATTR_SIZE)
1099 count = nfs_size_to_loff_t(res.dst_fattr->size) - dst_offset;
1100 nfs42_copy_dest_done(dst_inode, dst_offset, count);
1101 status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
1104 kfree(res.dst_fattr);
1108 int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
1109 loff_t src_offset, loff_t dst_offset, loff_t count)
1111 struct rpc_message msg = {
1112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
1114 struct inode *inode = file_inode(src_f);
1115 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
1116 struct nfs_lock_context *src_lock;
1117 struct nfs_lock_context *dst_lock;
1118 struct nfs4_exception src_exception = { };
1119 struct nfs4_exception dst_exception = { };
1122 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
1125 src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
1126 if (IS_ERR(src_lock))
1127 return PTR_ERR(src_lock);
1129 src_exception.inode = file_inode(src_f);
1130 src_exception.state = src_lock->open_context->state;
1132 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
1133 if (IS_ERR(dst_lock)) {
1134 err = PTR_ERR(dst_lock);
1135 goto out_put_src_lock;
1138 dst_exception.inode = file_inode(dst_f);
1139 dst_exception.state = dst_lock->open_context->state;
1142 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
1143 src_offset, dst_offset, count);
1144 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
1145 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
1150 err2 = nfs4_handle_exception(server, err, &src_exception);
1151 err = nfs4_handle_exception(server, err, &dst_exception);
1154 } while (src_exception.retry || dst_exception.retry);
1156 nfs_put_lock_context(dst_lock);
1158 nfs_put_lock_context(src_lock);
1162 #define NFS4XATTR_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
1164 static int _nfs42_proc_removexattr(struct inode *inode, const char *name)
1166 struct nfs_server *server = NFS_SERVER(inode);
1167 struct nfs42_removexattrargs args = {
1168 .fh = NFS_FH(inode),
1171 struct nfs42_removexattrres res;
1172 struct rpc_message msg = {
1173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVEXATTR],
1178 unsigned long timestamp = jiffies;
1180 ret = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
1182 trace_nfs4_removexattr(inode, name, ret);
1184 nfs4_update_changeattr(inode, &res.cinfo, timestamp, 0);
1189 static int _nfs42_proc_setxattr(struct inode *inode, const char *name,
1190 const void *buf, size_t buflen, int flags)
1192 struct nfs_server *server = NFS_SERVER(inode);
1193 struct page *pages[NFS4XATTR_MAXPAGES];
1194 struct nfs42_setxattrargs arg = {
1195 .fh = NFS_FH(inode),
1196 .xattr_pages = pages,
1197 .xattr_len = buflen,
1199 .xattr_flags = flags,
1201 struct nfs42_setxattrres res;
1202 struct rpc_message msg = {
1203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETXATTR],
1208 unsigned long timestamp = jiffies;
1210 if (buflen > server->sxasize)
1214 np = nfs4_buf_to_pages_noslab(buf, buflen, arg.xattr_pages);
1220 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1222 trace_nfs4_setxattr(inode, name, ret);
1224 for (; np > 0; np--)
1225 put_page(pages[np - 1]);
1228 nfs4_update_changeattr(inode, &res.cinfo, timestamp, 0);
1233 static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,
1234 void *buf, size_t buflen, struct page **pages,
1237 struct nfs_server *server = NFS_SERVER(inode);
1238 struct nfs42_getxattrargs arg = {
1239 .fh = NFS_FH(inode),
1242 struct nfs42_getxattrres res;
1243 struct rpc_message msg = {
1244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETXATTR],
1250 arg.xattr_len = plen;
1251 arg.xattr_pages = pages;
1253 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1255 trace_nfs4_getxattr(inode, name, ret);
1260 * Normally, the caching is done one layer up, but for successful
1261 * RPCS, always cache the result here, even if the caller was
1262 * just querying the length, or if the reply was too big for
1263 * the caller. This avoids a second RPC in the case of the
1264 * common query-alloc-retrieve cycle for xattrs.
1266 * Note that xattr_len is always capped to XATTR_SIZE_MAX.
1269 nfs4_xattr_cache_add(inode, name, NULL, pages, res.xattr_len);
1272 if (res.xattr_len > buflen)
1274 _copy_from_pages(buf, pages, 0, res.xattr_len);
1277 return res.xattr_len;
1280 static ssize_t _nfs42_proc_listxattrs(struct inode *inode, void *buf,
1281 size_t buflen, u64 *cookiep, bool *eofp)
1283 struct nfs_server *server = NFS_SERVER(inode);
1284 struct page **pages;
1285 struct nfs42_listxattrsargs arg = {
1286 .fh = NFS_FH(inode),
1289 struct nfs42_listxattrsres res = {
1292 .xattr_len = buflen,
1294 struct rpc_message msg = {
1295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LISTXATTRS],
1304 res.scratch = alloc_page(GFP_KERNEL);
1308 xdrlen = nfs42_listxattr_xdrsize(buflen);
1309 if (xdrlen > server->lxasize)
1310 xdrlen = server->lxasize;
1311 np = xdrlen / PAGE_SIZE + 1;
1313 pages = kcalloc(np, sizeof(struct page *), GFP_KERNEL);
1315 goto out_free_scratch;
1316 for (i = 0; i < np; i++) {
1317 pages[i] = alloc_page(GFP_KERNEL);
1319 goto out_free_pages;
1322 arg.xattr_pages = pages;
1325 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1327 trace_nfs4_listxattr(inode, ret);
1331 *cookiep = res.cookie;
1338 __free_page(pages[np]);
1342 __free_page(res.scratch);
1348 ssize_t nfs42_proc_getxattr(struct inode *inode, const char *name,
1349 void *buf, size_t buflen)
1351 struct nfs4_exception exception = { };
1353 struct page **pages;
1355 np = nfs_page_array_len(0, buflen ?: XATTR_SIZE_MAX);
1356 pages = kmalloc_array(np, sizeof(*pages), GFP_KERNEL);
1360 for (i = 0; i < np; i++) {
1361 pages[i] = alloc_page(GFP_KERNEL);
1369 * The GETXATTR op has no length field in the call, and the
1370 * xattr data is at the end of the reply.
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.
1377 err = _nfs42_proc_getxattr(inode, name, buf, buflen,
1378 pages, np * PAGE_SIZE);
1381 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1383 } while (exception.retry);
1387 __free_page(pages[i]);
1393 int nfs42_proc_setxattr(struct inode *inode, const char *name,
1394 const void *buf, size_t buflen, int flags)
1396 struct nfs4_exception exception = { };
1400 err = _nfs42_proc_setxattr(inode, name, buf, buflen, flags);
1403 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1405 } while (exception.retry);
1410 ssize_t nfs42_proc_listxattrs(struct inode *inode, void *buf,
1411 size_t buflen, u64 *cookiep, bool *eofp)
1413 struct nfs4_exception exception = { };
1417 err = _nfs42_proc_listxattrs(inode, buf, buflen,
1421 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1423 } while (exception.retry);
1428 int nfs42_proc_removexattr(struct inode *inode, const char *name)
1430 struct nfs4_exception exception = { };
1434 err = _nfs42_proc_removexattr(inode, name);
1437 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1439 } while (exception.retry);