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 err == -NFS4ERR_OFFLOAD_DENIED) &&
465 nfs42_files_from_same_server(src, dst)) {
468 } else if (err == -EAGAIN) {
470 dst_exception.retry = 1;
474 } else if (err == -NFS4ERR_OFFLOAD_NO_REQS &&
475 args.sync != res.synchronous) {
476 args.sync = res.synchronous;
477 dst_exception.retry = 1;
479 } else if ((err == -ESTALE ||
480 err == -NFS4ERR_OFFLOAD_DENIED ||
482 !nfs42_files_from_same_server(src, dst)) {
483 nfs42_do_offload_cancel_async(src, &args.src_stateid);
488 err2 = nfs4_handle_exception(server, err, &src_exception);
489 err = nfs4_handle_exception(server, err, &dst_exception);
492 } while (src_exception.retry || dst_exception.retry);
494 nfs_put_lock_context(dst_lock);
496 nfs_put_lock_context(src_lock);
500 struct nfs42_offloadcancel_data {
501 struct nfs_server *seq_server;
502 struct nfs42_offload_status_args args;
503 struct nfs42_offload_status_res res;
506 static void nfs42_offload_cancel_prepare(struct rpc_task *task, void *calldata)
508 struct nfs42_offloadcancel_data *data = calldata;
510 nfs4_setup_sequence(data->seq_server->nfs_client,
511 &data->args.osa_seq_args,
512 &data->res.osr_seq_res, task);
515 static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata)
517 struct nfs42_offloadcancel_data *data = calldata;
519 trace_nfs4_offload_cancel(&data->args, task->tk_status);
520 nfs41_sequence_done(task, &data->res.osr_seq_res);
521 if (task->tk_status &&
522 nfs4_async_handle_error(task, data->seq_server, NULL,
524 rpc_restart_call_prepare(task);
527 static void nfs42_free_offloadcancel_data(void *data)
532 static const struct rpc_call_ops nfs42_offload_cancel_ops = {
533 .rpc_call_prepare = nfs42_offload_cancel_prepare,
534 .rpc_call_done = nfs42_offload_cancel_done,
535 .rpc_release = nfs42_free_offloadcancel_data,
538 static int nfs42_do_offload_cancel_async(struct file *dst,
539 nfs4_stateid *stateid)
541 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
542 struct nfs42_offloadcancel_data *data = NULL;
543 struct nfs_open_context *ctx = nfs_file_open_context(dst);
544 struct rpc_task *task;
545 struct rpc_message msg = {
546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_CANCEL],
547 .rpc_cred = ctx->cred,
549 struct rpc_task_setup task_setup_data = {
550 .rpc_client = dst_server->client,
552 .callback_ops = &nfs42_offload_cancel_ops,
553 .workqueue = nfsiod_workqueue,
554 .flags = RPC_TASK_ASYNC,
558 if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL))
561 data = kzalloc(sizeof(struct nfs42_offloadcancel_data), GFP_KERNEL);
565 data->seq_server = dst_server;
566 data->args.osa_src_fh = NFS_FH(file_inode(dst));
567 memcpy(&data->args.osa_stateid, stateid,
568 sizeof(data->args.osa_stateid));
569 msg.rpc_argp = &data->args;
570 msg.rpc_resp = &data->res;
571 task_setup_data.callback_data = data;
572 nfs4_init_sequence(&data->args.osa_seq_args, &data->res.osr_seq_res,
574 task = rpc_run_task(&task_setup_data);
576 return PTR_ERR(task);
577 status = rpc_wait_for_completion_task(task);
578 if (status == -ENOTSUPP)
579 dst_server->caps &= ~NFS_CAP_OFFLOAD_CANCEL;
584 static int _nfs42_proc_copy_notify(struct file *src, struct file *dst,
585 struct nfs42_copy_notify_args *args,
586 struct nfs42_copy_notify_res *res)
588 struct nfs_server *src_server = NFS_SERVER(file_inode(src));
589 struct rpc_message msg = {
590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY_NOTIFY],
595 struct nfs_open_context *ctx;
596 struct nfs_lock_context *l_ctx;
598 ctx = get_nfs_open_context(nfs_file_open_context(src));
599 l_ctx = nfs_get_lock_context(ctx);
601 status = PTR_ERR(l_ctx);
605 status = nfs4_set_rw_stateid(&args->cna_src_stateid, ctx, l_ctx,
607 nfs_put_lock_context(l_ctx);
609 if (status == -EAGAIN)
610 status = -NFS4ERR_BAD_STATEID;
614 status = nfs4_call_sync(src_server->client, src_server, &msg,
615 &args->cna_seq_args, &res->cnr_seq_res, 0);
616 trace_nfs4_copy_notify(file_inode(src), args, res, status);
617 if (status == -ENOTSUPP)
618 src_server->caps &= ~NFS_CAP_COPY_NOTIFY;
621 put_nfs_open_context(nfs_file_open_context(src));
625 int nfs42_proc_copy_notify(struct file *src, struct file *dst,
626 struct nfs42_copy_notify_res *res)
628 struct nfs_server *src_server = NFS_SERVER(file_inode(src));
629 struct nfs42_copy_notify_args *args;
630 struct nfs4_exception exception = {
631 .inode = file_inode(src),
635 if (!(src_server->caps & NFS_CAP_COPY_NOTIFY))
638 args = kzalloc(sizeof(struct nfs42_copy_notify_args), GFP_KERNEL);
642 args->cna_src_fh = NFS_FH(file_inode(src)),
643 args->cna_dst.nl4_type = NL4_NETADDR;
644 nfs42_set_netaddr(dst, &args->cna_dst.u.nl4_addr);
645 exception.stateid = &args->cna_src_stateid;
648 status = _nfs42_proc_copy_notify(src, dst, args, res);
649 if (status == -ENOTSUPP) {
650 status = -EOPNOTSUPP;
653 status = nfs4_handle_exception(src_server, status, &exception);
654 } while (exception.retry);
661 static loff_t _nfs42_proc_llseek(struct file *filep,
662 struct nfs_lock_context *lock, loff_t offset, int whence)
664 struct inode *inode = file_inode(filep);
665 struct nfs42_seek_args args = {
666 .sa_fh = NFS_FH(inode),
668 .sa_what = (whence == SEEK_HOLE) ?
669 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
671 struct nfs42_seek_res res;
672 struct rpc_message msg = {
673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
677 struct nfs_server *server = NFS_SERVER(inode);
680 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
683 status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
686 if (status == -EAGAIN)
687 status = -NFS4ERR_BAD_STATEID;
691 status = nfs_filemap_write_and_wait_range(inode->i_mapping,
696 status = nfs4_call_sync(server->client, server, &msg,
697 &args.seq_args, &res.seq_res, 0);
698 trace_nfs4_llseek(inode, &args, &res, status);
699 if (status == -ENOTSUPP)
700 server->caps &= ~NFS_CAP_SEEK;
704 if (whence == SEEK_DATA && res.sr_eof)
705 return -NFS4ERR_NXIO;
707 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
710 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
712 struct nfs_server *server = NFS_SERVER(file_inode(filep));
713 struct nfs4_exception exception = { };
714 struct nfs_lock_context *lock;
717 lock = nfs_get_lock_context(nfs_file_open_context(filep));
719 return PTR_ERR(lock);
721 exception.inode = file_inode(filep);
722 exception.state = lock->open_context->state;
725 err = _nfs42_proc_llseek(filep, lock, offset, whence);
728 if (err == -ENOTSUPP) {
732 err = nfs4_handle_exception(server, err, &exception);
733 } while (exception.retry);
735 nfs_put_lock_context(lock);
741 nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
743 struct nfs42_layoutstat_data *data = calldata;
744 struct inode *inode = data->inode;
745 struct nfs_server *server = NFS_SERVER(inode);
746 struct pnfs_layout_hdr *lo;
748 spin_lock(&inode->i_lock);
749 lo = NFS_I(inode)->layout;
750 if (!pnfs_layout_is_valid(lo)) {
751 spin_unlock(&inode->i_lock);
755 nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
756 spin_unlock(&inode->i_lock);
757 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
758 &data->res.seq_res, task);
762 nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
764 struct nfs42_layoutstat_data *data = calldata;
765 struct inode *inode = data->inode;
766 struct pnfs_layout_hdr *lo;
768 if (!nfs4_sequence_done(task, &data->res.seq_res))
771 switch (task->tk_status) {
774 case -NFS4ERR_BADHANDLE:
776 pnfs_destroy_layout(NFS_I(inode));
778 case -NFS4ERR_EXPIRED:
779 case -NFS4ERR_ADMIN_REVOKED:
780 case -NFS4ERR_DELEG_REVOKED:
781 case -NFS4ERR_STALE_STATEID:
782 case -NFS4ERR_BAD_STATEID:
783 spin_lock(&inode->i_lock);
784 lo = NFS_I(inode)->layout;
785 if (pnfs_layout_is_valid(lo) &&
786 nfs4_stateid_match(&data->args.stateid,
791 * Mark the bad layout state as invalid, then retry
792 * with the current stateid.
794 pnfs_mark_layout_stateid_invalid(lo, &head);
795 spin_unlock(&inode->i_lock);
796 pnfs_free_lseg_list(&head);
797 nfs_commit_inode(inode, 0);
799 spin_unlock(&inode->i_lock);
801 case -NFS4ERR_OLD_STATEID:
802 spin_lock(&inode->i_lock);
803 lo = NFS_I(inode)->layout;
804 if (pnfs_layout_is_valid(lo) &&
805 nfs4_stateid_match_other(&data->args.stateid,
807 /* Do we need to delay before resending? */
808 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
809 &data->args.stateid))
811 rpc_restart_call_prepare(task);
813 spin_unlock(&inode->i_lock);
817 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
820 trace_nfs4_layoutstats(inode, &data->args.stateid, task->tk_status);
824 nfs42_layoutstat_release(void *calldata)
826 struct nfs42_layoutstat_data *data = calldata;
827 struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo;
830 for (i = 0; i < data->args.num_dev; i++) {
831 if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free)
832 devinfo[i].ld_private.ops->free(&devinfo[i].ld_private);
835 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
836 smp_mb__before_atomic();
837 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
838 smp_mb__after_atomic();
839 nfs_iput_and_deactive(data->inode);
840 kfree(data->args.devinfo);
844 static const struct rpc_call_ops nfs42_layoutstat_ops = {
845 .rpc_call_prepare = nfs42_layoutstat_prepare,
846 .rpc_call_done = nfs42_layoutstat_done,
847 .rpc_release = nfs42_layoutstat_release,
850 int nfs42_proc_layoutstats_generic(struct nfs_server *server,
851 struct nfs42_layoutstat_data *data)
853 struct rpc_message msg = {
854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
855 .rpc_argp = &data->args,
856 .rpc_resp = &data->res,
858 struct rpc_task_setup task_setup = {
859 .rpc_client = server->client,
861 .callback_ops = &nfs42_layoutstat_ops,
862 .callback_data = data,
863 .flags = RPC_TASK_ASYNC,
865 struct rpc_task *task;
867 data->inode = nfs_igrab_and_active(data->args.inode);
869 nfs42_layoutstat_release(data);
872 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
873 task = rpc_run_task(&task_setup);
875 return PTR_ERR(task);
880 static struct nfs42_layouterror_data *
881 nfs42_alloc_layouterror_data(struct pnfs_layout_segment *lseg, gfp_t gfp_flags)
883 struct nfs42_layouterror_data *data;
884 struct inode *inode = lseg->pls_layout->plh_inode;
886 data = kzalloc(sizeof(*data), gfp_flags);
888 data->args.inode = data->inode = nfs_igrab_and_active(inode);
890 data->lseg = pnfs_get_lseg(lseg);
893 nfs_iput_and_deactive(data->inode);
901 nfs42_free_layouterror_data(struct nfs42_layouterror_data *data)
903 pnfs_put_lseg(data->lseg);
904 nfs_iput_and_deactive(data->inode);
909 nfs42_layouterror_prepare(struct rpc_task *task, void *calldata)
911 struct nfs42_layouterror_data *data = calldata;
912 struct inode *inode = data->inode;
913 struct nfs_server *server = NFS_SERVER(inode);
914 struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
917 spin_lock(&inode->i_lock);
918 if (!pnfs_layout_is_valid(lo)) {
919 spin_unlock(&inode->i_lock);
923 for (i = 0; i < data->args.num_errors; i++)
924 nfs4_stateid_copy(&data->args.errors[i].stateid,
926 spin_unlock(&inode->i_lock);
927 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
928 &data->res.seq_res, task);
932 nfs42_layouterror_done(struct rpc_task *task, void *calldata)
934 struct nfs42_layouterror_data *data = calldata;
935 struct inode *inode = data->inode;
936 struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
938 if (!nfs4_sequence_done(task, &data->res.seq_res))
941 switch (task->tk_status) {
944 case -NFS4ERR_BADHANDLE:
946 pnfs_destroy_layout(NFS_I(inode));
948 case -NFS4ERR_EXPIRED:
949 case -NFS4ERR_ADMIN_REVOKED:
950 case -NFS4ERR_DELEG_REVOKED:
951 case -NFS4ERR_STALE_STATEID:
952 case -NFS4ERR_BAD_STATEID:
953 spin_lock(&inode->i_lock);
954 if (pnfs_layout_is_valid(lo) &&
955 nfs4_stateid_match(&data->args.errors[0].stateid,
960 * Mark the bad layout state as invalid, then retry
961 * with the current stateid.
963 pnfs_mark_layout_stateid_invalid(lo, &head);
964 spin_unlock(&inode->i_lock);
965 pnfs_free_lseg_list(&head);
966 nfs_commit_inode(inode, 0);
968 spin_unlock(&inode->i_lock);
970 case -NFS4ERR_OLD_STATEID:
971 spin_lock(&inode->i_lock);
972 if (pnfs_layout_is_valid(lo) &&
973 nfs4_stateid_match_other(&data->args.errors[0].stateid,
975 /* Do we need to delay before resending? */
976 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
977 &data->args.errors[0].stateid))
979 rpc_restart_call_prepare(task);
981 spin_unlock(&inode->i_lock);
985 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTERROR;
988 trace_nfs4_layouterror(inode, &data->args.errors[0].stateid,
993 nfs42_layouterror_release(void *calldata)
995 struct nfs42_layouterror_data *data = calldata;
997 nfs42_free_layouterror_data(data);
1000 static const struct rpc_call_ops nfs42_layouterror_ops = {
1001 .rpc_call_prepare = nfs42_layouterror_prepare,
1002 .rpc_call_done = nfs42_layouterror_done,
1003 .rpc_release = nfs42_layouterror_release,
1006 int nfs42_proc_layouterror(struct pnfs_layout_segment *lseg,
1007 const struct nfs42_layout_error *errors, size_t n)
1009 struct inode *inode = lseg->pls_layout->plh_inode;
1010 struct nfs42_layouterror_data *data;
1011 struct rpc_task *task;
1012 struct rpc_message msg = {
1013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTERROR],
1015 struct rpc_task_setup task_setup = {
1016 .rpc_message = &msg,
1017 .callback_ops = &nfs42_layouterror_ops,
1018 .flags = RPC_TASK_ASYNC,
1022 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTERROR))
1024 if (n > NFS42_LAYOUTERROR_MAX)
1026 data = nfs42_alloc_layouterror_data(lseg, nfs_io_gfp_mask());
1029 for (i = 0; i < n; i++) {
1030 data->args.errors[i] = errors[i];
1031 data->args.num_errors++;
1032 data->res.num_errors++;
1034 msg.rpc_argp = &data->args;
1035 msg.rpc_resp = &data->res;
1036 task_setup.callback_data = data;
1037 task_setup.rpc_client = NFS_SERVER(inode)->client;
1038 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
1039 task = rpc_run_task(&task_setup);
1041 return PTR_ERR(task);
1045 EXPORT_SYMBOL_GPL(nfs42_proc_layouterror);
1047 static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
1048 struct file *dst_f, struct nfs_lock_context *src_lock,
1049 struct nfs_lock_context *dst_lock, loff_t src_offset,
1050 loff_t dst_offset, loff_t count)
1052 struct inode *src_inode = file_inode(src_f);
1053 struct inode *dst_inode = file_inode(dst_f);
1054 struct nfs_server *server = NFS_SERVER(dst_inode);
1055 __u32 dst_bitmask[NFS_BITMASK_SZ];
1056 struct nfs42_clone_args args = {
1057 .src_fh = NFS_FH(src_inode),
1058 .dst_fh = NFS_FH(dst_inode),
1059 .src_offset = src_offset,
1060 .dst_offset = dst_offset,
1062 .dst_bitmask = dst_bitmask,
1064 struct nfs42_clone_res res = {
1069 msg->rpc_argp = &args;
1070 msg->rpc_resp = &res;
1072 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
1073 src_lock, FMODE_READ);
1075 if (status == -EAGAIN)
1076 status = -NFS4ERR_BAD_STATEID;
1079 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
1080 dst_lock, FMODE_WRITE);
1082 if (status == -EAGAIN)
1083 status = -NFS4ERR_BAD_STATEID;
1087 res.dst_fattr = nfs_alloc_fattr();
1091 nfs4_bitmask_set(dst_bitmask, server->cache_consistency_bitmask,
1092 dst_inode, NFS_INO_INVALID_BLOCKS);
1094 status = nfs4_call_sync(server->client, server, msg,
1095 &args.seq_args, &res.seq_res, 0);
1096 trace_nfs4_clone(src_inode, dst_inode, &args, status);
1098 /* a zero-length count means clone to EOF in src */
1099 if (count == 0 && res.dst_fattr->valid & NFS_ATTR_FATTR_SIZE)
1100 count = nfs_size_to_loff_t(res.dst_fattr->size) - dst_offset;
1101 nfs42_copy_dest_done(dst_inode, dst_offset, count);
1102 status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
1105 kfree(res.dst_fattr);
1109 int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
1110 loff_t src_offset, loff_t dst_offset, loff_t count)
1112 struct rpc_message msg = {
1113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
1115 struct inode *inode = file_inode(src_f);
1116 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
1117 struct nfs_lock_context *src_lock;
1118 struct nfs_lock_context *dst_lock;
1119 struct nfs4_exception src_exception = { };
1120 struct nfs4_exception dst_exception = { };
1123 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
1126 src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
1127 if (IS_ERR(src_lock))
1128 return PTR_ERR(src_lock);
1130 src_exception.inode = file_inode(src_f);
1131 src_exception.state = src_lock->open_context->state;
1133 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
1134 if (IS_ERR(dst_lock)) {
1135 err = PTR_ERR(dst_lock);
1136 goto out_put_src_lock;
1139 dst_exception.inode = file_inode(dst_f);
1140 dst_exception.state = dst_lock->open_context->state;
1143 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
1144 src_offset, dst_offset, count);
1145 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
1146 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
1151 err2 = nfs4_handle_exception(server, err, &src_exception);
1152 err = nfs4_handle_exception(server, err, &dst_exception);
1155 } while (src_exception.retry || dst_exception.retry);
1157 nfs_put_lock_context(dst_lock);
1159 nfs_put_lock_context(src_lock);
1163 #define NFS4XATTR_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
1165 static int _nfs42_proc_removexattr(struct inode *inode, const char *name)
1167 struct nfs_server *server = NFS_SERVER(inode);
1168 struct nfs42_removexattrargs args = {
1169 .fh = NFS_FH(inode),
1172 struct nfs42_removexattrres res;
1173 struct rpc_message msg = {
1174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVEXATTR],
1179 unsigned long timestamp = jiffies;
1181 ret = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
1183 trace_nfs4_removexattr(inode, name, ret);
1185 nfs4_update_changeattr(inode, &res.cinfo, timestamp, 0);
1190 static int _nfs42_proc_setxattr(struct inode *inode, const char *name,
1191 const void *buf, size_t buflen, int flags)
1193 struct nfs_server *server = NFS_SERVER(inode);
1194 __u32 bitmask[NFS_BITMASK_SZ];
1195 struct page *pages[NFS4XATTR_MAXPAGES];
1196 struct nfs42_setxattrargs arg = {
1197 .fh = NFS_FH(inode),
1199 .xattr_pages = pages,
1200 .xattr_len = buflen,
1202 .xattr_flags = flags,
1204 struct nfs42_setxattrres res = {
1207 struct rpc_message msg = {
1208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETXATTR],
1213 unsigned long timestamp = jiffies;
1215 if (buflen > server->sxasize)
1218 res.fattr = nfs_alloc_fattr();
1223 np = nfs4_buf_to_pages_noslab(buf, buflen, arg.xattr_pages);
1231 nfs4_bitmask_set(bitmask, server->cache_consistency_bitmask,
1232 inode, NFS_INO_INVALID_CHANGE);
1234 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1236 trace_nfs4_setxattr(inode, name, ret);
1238 for (; np > 0; np--)
1239 put_page(pages[np - 1]);
1242 nfs4_update_changeattr(inode, &res.cinfo, timestamp, 0);
1243 ret = nfs_post_op_update_inode(inode, res.fattr);
1251 static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name,
1252 void *buf, size_t buflen, struct page **pages,
1255 struct nfs_server *server = NFS_SERVER(inode);
1256 struct nfs42_getxattrargs arg = {
1257 .fh = NFS_FH(inode),
1260 struct nfs42_getxattrres res;
1261 struct rpc_message msg = {
1262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETXATTR],
1268 arg.xattr_len = plen;
1269 arg.xattr_pages = pages;
1271 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1273 trace_nfs4_getxattr(inode, name, ret);
1278 * Normally, the caching is done one layer up, but for successful
1279 * RPCS, always cache the result here, even if the caller was
1280 * just querying the length, or if the reply was too big for
1281 * the caller. This avoids a second RPC in the case of the
1282 * common query-alloc-retrieve cycle for xattrs.
1284 * Note that xattr_len is always capped to XATTR_SIZE_MAX.
1287 nfs4_xattr_cache_add(inode, name, NULL, pages, res.xattr_len);
1290 if (res.xattr_len > buflen)
1292 _copy_from_pages(buf, pages, 0, res.xattr_len);
1295 return res.xattr_len;
1298 static ssize_t _nfs42_proc_listxattrs(struct inode *inode, void *buf,
1299 size_t buflen, u64 *cookiep, bool *eofp)
1301 struct nfs_server *server = NFS_SERVER(inode);
1302 struct page **pages;
1303 struct nfs42_listxattrsargs arg = {
1304 .fh = NFS_FH(inode),
1307 struct nfs42_listxattrsres res = {
1310 .xattr_len = buflen,
1312 struct rpc_message msg = {
1313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LISTXATTRS],
1322 res.scratch = alloc_page(GFP_KERNEL);
1326 xdrlen = nfs42_listxattr_xdrsize(buflen);
1327 if (xdrlen > server->lxasize)
1328 xdrlen = server->lxasize;
1329 np = xdrlen / PAGE_SIZE + 1;
1331 pages = kcalloc(np, sizeof(struct page *), GFP_KERNEL);
1333 goto out_free_scratch;
1334 for (i = 0; i < np; i++) {
1335 pages[i] = alloc_page(GFP_KERNEL);
1337 goto out_free_pages;
1340 arg.xattr_pages = pages;
1343 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args,
1345 trace_nfs4_listxattr(inode, ret);
1349 *cookiep = res.cookie;
1356 __free_page(pages[np]);
1360 __free_page(res.scratch);
1366 ssize_t nfs42_proc_getxattr(struct inode *inode, const char *name,
1367 void *buf, size_t buflen)
1369 struct nfs4_exception exception = { };
1371 struct page **pages;
1373 np = nfs_page_array_len(0, buflen ?: XATTR_SIZE_MAX);
1374 pages = kmalloc_array(np, sizeof(*pages), GFP_KERNEL);
1378 for (i = 0; i < np; i++) {
1379 pages[i] = alloc_page(GFP_KERNEL);
1387 * The GETXATTR op has no length field in the call, and the
1388 * xattr data is at the end of the reply.
1390 * There is no downside in using the page-aligned length. It will
1391 * allow receiving and caching xattrs that are too large for the
1392 * caller but still fit in the page-rounded value.
1395 err = _nfs42_proc_getxattr(inode, name, buf, buflen,
1396 pages, np * PAGE_SIZE);
1399 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1401 } while (exception.retry);
1405 __free_page(pages[i]);
1411 int nfs42_proc_setxattr(struct inode *inode, const char *name,
1412 const void *buf, size_t buflen, int flags)
1414 struct nfs4_exception exception = { };
1418 err = _nfs42_proc_setxattr(inode, name, buf, buflen, flags);
1421 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1423 } while (exception.retry);
1428 ssize_t nfs42_proc_listxattrs(struct inode *inode, void *buf,
1429 size_t buflen, u64 *cookiep, bool *eofp)
1431 struct nfs4_exception exception = { };
1435 err = _nfs42_proc_listxattrs(inode, buf, buflen,
1439 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1441 } while (exception.retry);
1446 int nfs42_proc_removexattr(struct inode *inode, const char *name)
1448 struct nfs4_exception exception = { };
1452 err = _nfs42_proc_removexattr(inode, name);
1455 err = nfs4_handle_exception(NFS_SERVER(inode), err,
1457 } while (exception.retry);