1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
6 #include <linux/sunrpc/sched.h>
8 #include <linux/nfs3.h>
9 #include <linux/nfs4.h>
10 #include <linux/nfs_xdr.h>
11 #include <linux/nfs_fs.h>
16 #include "nfs4session.h"
19 #define NFSDBG_FACILITY NFSDBG_PROC
20 static int nfs42_do_offload_cancel_async(struct file *dst, nfs4_stateid *std);
22 static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
23 struct nfs_lock_context *lock, loff_t offset, loff_t len)
25 struct inode *inode = file_inode(filep);
26 struct nfs_server *server = NFS_SERVER(inode);
27 struct nfs42_falloc_args args = {
28 .falloc_fh = NFS_FH(inode),
29 .falloc_offset = offset,
31 .falloc_bitmask = server->cache_consistency_bitmask,
33 struct nfs42_falloc_res res = {
34 .falloc_server = server,
38 msg->rpc_argp = &args;
41 status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
46 res.falloc_fattr = nfs_alloc_fattr();
47 if (!res.falloc_fattr)
50 status = nfs4_call_sync(server->client, server, msg,
51 &args.seq_args, &res.seq_res, 0);
53 status = nfs_post_op_update_inode(inode, res.falloc_fattr);
55 kfree(res.falloc_fattr);
59 static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
60 loff_t offset, loff_t len)
62 struct inode *inode = file_inode(filep);
63 struct nfs_server *server = NFS_SERVER(inode);
64 struct nfs4_exception exception = { };
65 struct nfs_lock_context *lock;
68 lock = nfs_get_lock_context(nfs_file_open_context(filep));
72 exception.inode = inode;
73 exception.state = lock->open_context->state;
75 err = nfs_sync_inode(inode);
80 err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
81 if (err == -ENOTSUPP) {
85 err = nfs4_handle_exception(server, err, &exception);
86 } while (exception.retry);
88 nfs_put_lock_context(lock);
92 int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
94 struct rpc_message msg = {
95 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
97 struct inode *inode = file_inode(filep);
100 if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
105 err = nfs42_proc_fallocate(&msg, filep, offset, len);
106 if (err == -EOPNOTSUPP)
107 NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
113 int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
115 struct rpc_message msg = {
116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
118 struct inode *inode = file_inode(filep);
121 if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
126 err = nfs42_proc_fallocate(&msg, filep, offset, len);
128 truncate_pagecache_range(inode, offset, (offset + len) -1);
129 if (err == -EOPNOTSUPP)
130 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
136 static int handle_async_copy(struct nfs42_copy_res *res,
137 struct nfs_server *server,
140 nfs4_stateid *src_stateid)
142 struct nfs4_copy_state *copy, *tmp_copy;
143 int status = NFS4_OK;
144 bool found_pending = false;
145 struct nfs_open_context *ctx = nfs_file_open_context(dst);
147 copy = kzalloc(sizeof(struct nfs4_copy_state), GFP_NOFS);
151 spin_lock(&server->nfs_client->cl_lock);
152 list_for_each_entry(tmp_copy, &server->nfs_client->pending_cb_stateids,
154 if (memcmp(&res->write_res.stateid, &tmp_copy->stateid,
157 found_pending = true;
158 list_del(&tmp_copy->copies);
162 spin_unlock(&server->nfs_client->cl_lock);
168 memcpy(©->stateid, &res->write_res.stateid, NFS4_STATEID_SIZE);
169 init_completion(©->completion);
170 copy->parent_state = ctx->state;
172 list_add_tail(©->copies, &server->ss_copies);
173 spin_unlock(&server->nfs_client->cl_lock);
175 status = wait_for_completion_interruptible(©->completion);
176 spin_lock(&server->nfs_client->cl_lock);
177 list_del_init(©->copies);
178 spin_unlock(&server->nfs_client->cl_lock);
179 if (status == -ERESTARTSYS) {
181 } else if (copy->flags) {
186 res->write_res.count = copy->count;
187 memcpy(&res->write_res.verifier, ©->verf, sizeof(copy->verf));
188 status = -copy->error;
193 nfs42_do_offload_cancel_async(dst, ©->stateid);
198 static int process_copy_commit(struct file *dst, loff_t pos_dst,
199 struct nfs42_copy_res *res)
201 struct nfs_commitres cres;
202 int status = -ENOMEM;
204 cres.verf = kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS);
208 status = nfs4_proc_commit(dst, pos_dst, res->write_res.count, &cres);
211 if (nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
212 &cres.verf->verifier)) {
213 dprintk("commit verf differs from copy verf\n");
222 static ssize_t _nfs42_proc_copy(struct file *src,
223 struct nfs_lock_context *src_lock,
225 struct nfs_lock_context *dst_lock,
226 struct nfs42_copy_args *args,
227 struct nfs42_copy_res *res)
229 struct rpc_message msg = {
230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
234 struct inode *dst_inode = file_inode(dst);
235 struct nfs_server *server = NFS_SERVER(dst_inode);
236 loff_t pos_src = args->src_pos;
237 loff_t pos_dst = args->dst_pos;
238 size_t count = args->count;
241 status = nfs4_set_rw_stateid(&args->src_stateid, src_lock->open_context,
242 src_lock, FMODE_READ);
246 status = nfs_filemap_write_and_wait_range(file_inode(src)->i_mapping,
247 pos_src, pos_src + (loff_t)count - 1);
251 status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context,
252 dst_lock, FMODE_WRITE);
256 status = nfs_sync_inode(dst_inode);
260 res->commit_res.verf = NULL;
262 res->commit_res.verf =
263 kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS);
264 if (!res->commit_res.verf)
267 set_bit(NFS_CLNT_DST_SSC_COPY_STATE,
268 &dst_lock->open_context->state->flags);
270 status = nfs4_call_sync(server->client, server, &msg,
271 &args->seq_args, &res->seq_res, 0);
272 if (status == -ENOTSUPP)
273 server->caps &= ~NFS_CAP_COPY;
278 nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
279 &res->commit_res.verf->verifier)) {
284 if (!res->synchronous) {
285 status = handle_async_copy(res, server, src, dst,
291 if ((!res->synchronous || !args->sync) &&
292 res->write_res.verifier.committed != NFS_FILE_SYNC) {
293 status = process_copy_commit(dst, pos_dst, res);
298 truncate_pagecache_range(dst_inode, pos_dst,
299 pos_dst + res->write_res.count);
301 status = res->write_res.count;
304 kfree(res->commit_res.verf);
308 ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
309 struct file *dst, loff_t pos_dst,
312 struct nfs_server *server = NFS_SERVER(file_inode(dst));
313 struct nfs_lock_context *src_lock;
314 struct nfs_lock_context *dst_lock;
315 struct nfs42_copy_args args = {
316 .src_fh = NFS_FH(file_inode(src)),
318 .dst_fh = NFS_FH(file_inode(dst)),
323 struct nfs42_copy_res res;
324 struct nfs4_exception src_exception = {
325 .inode = file_inode(src),
326 .stateid = &args.src_stateid,
328 struct nfs4_exception dst_exception = {
329 .inode = file_inode(dst),
330 .stateid = &args.dst_stateid,
334 src_lock = nfs_get_lock_context(nfs_file_open_context(src));
335 if (IS_ERR(src_lock))
336 return PTR_ERR(src_lock);
338 src_exception.state = src_lock->open_context->state;
340 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst));
341 if (IS_ERR(dst_lock)) {
342 err = PTR_ERR(dst_lock);
343 goto out_put_src_lock;
346 dst_exception.state = dst_lock->open_context->state;
349 inode_lock(file_inode(dst));
350 err = _nfs42_proc_copy(src, src_lock,
353 inode_unlock(file_inode(dst));
357 if (err == -ENOTSUPP) {
360 } else if (err == -EAGAIN) {
361 dst_exception.retry = 1;
363 } else if (err == -NFS4ERR_OFFLOAD_NO_REQS && !args.sync) {
365 dst_exception.retry = 1;
369 err2 = nfs4_handle_exception(server, err, &src_exception);
370 err = nfs4_handle_exception(server, err, &dst_exception);
373 } while (src_exception.retry || dst_exception.retry);
375 nfs_put_lock_context(dst_lock);
377 nfs_put_lock_context(src_lock);
381 struct nfs42_offloadcancel_data {
382 struct nfs_server *seq_server;
383 struct nfs42_offload_status_args args;
384 struct nfs42_offload_status_res res;
387 static void nfs42_offload_cancel_prepare(struct rpc_task *task, void *calldata)
389 struct nfs42_offloadcancel_data *data = calldata;
391 nfs4_setup_sequence(data->seq_server->nfs_client,
392 &data->args.osa_seq_args,
393 &data->res.osr_seq_res, task);
396 static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata)
398 struct nfs42_offloadcancel_data *data = calldata;
400 nfs41_sequence_done(task, &data->res.osr_seq_res);
401 if (task->tk_status &&
402 nfs4_async_handle_error(task, data->seq_server, NULL,
404 rpc_restart_call_prepare(task);
407 static void nfs42_free_offloadcancel_data(void *data)
412 static const struct rpc_call_ops nfs42_offload_cancel_ops = {
413 .rpc_call_prepare = nfs42_offload_cancel_prepare,
414 .rpc_call_done = nfs42_offload_cancel_done,
415 .rpc_release = nfs42_free_offloadcancel_data,
418 static int nfs42_do_offload_cancel_async(struct file *dst,
419 nfs4_stateid *stateid)
421 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
422 struct nfs42_offloadcancel_data *data = NULL;
423 struct nfs_open_context *ctx = nfs_file_open_context(dst);
424 struct rpc_task *task;
425 struct rpc_message msg = {
426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_CANCEL],
427 .rpc_cred = ctx->cred,
429 struct rpc_task_setup task_setup_data = {
430 .rpc_client = dst_server->client,
432 .callback_ops = &nfs42_offload_cancel_ops,
433 .workqueue = nfsiod_workqueue,
434 .flags = RPC_TASK_ASYNC,
438 if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL))
441 data = kzalloc(sizeof(struct nfs42_offloadcancel_data), GFP_NOFS);
445 data->seq_server = dst_server;
446 data->args.osa_src_fh = NFS_FH(file_inode(dst));
447 memcpy(&data->args.osa_stateid, stateid,
448 sizeof(data->args.osa_stateid));
449 msg.rpc_argp = &data->args;
450 msg.rpc_resp = &data->res;
451 task_setup_data.callback_data = data;
452 nfs4_init_sequence(&data->args.osa_seq_args, &data->res.osr_seq_res,
454 task = rpc_run_task(&task_setup_data);
456 return PTR_ERR(task);
457 status = rpc_wait_for_completion_task(task);
458 if (status == -ENOTSUPP)
459 dst_server->caps &= ~NFS_CAP_OFFLOAD_CANCEL;
464 static loff_t _nfs42_proc_llseek(struct file *filep,
465 struct nfs_lock_context *lock, loff_t offset, int whence)
467 struct inode *inode = file_inode(filep);
468 struct nfs42_seek_args args = {
469 .sa_fh = NFS_FH(inode),
471 .sa_what = (whence == SEEK_HOLE) ?
472 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
474 struct nfs42_seek_res res;
475 struct rpc_message msg = {
476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
480 struct nfs_server *server = NFS_SERVER(inode);
483 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
486 status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
491 status = nfs_filemap_write_and_wait_range(inode->i_mapping,
496 status = nfs4_call_sync(server->client, server, &msg,
497 &args.seq_args, &res.seq_res, 0);
498 if (status == -ENOTSUPP)
499 server->caps &= ~NFS_CAP_SEEK;
503 if (whence == SEEK_DATA && res.sr_eof)
504 return -NFS4ERR_NXIO;
506 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
509 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
511 struct nfs_server *server = NFS_SERVER(file_inode(filep));
512 struct nfs4_exception exception = { };
513 struct nfs_lock_context *lock;
516 lock = nfs_get_lock_context(nfs_file_open_context(filep));
518 return PTR_ERR(lock);
520 exception.inode = file_inode(filep);
521 exception.state = lock->open_context->state;
524 err = _nfs42_proc_llseek(filep, lock, offset, whence);
527 if (err == -ENOTSUPP) {
531 err = nfs4_handle_exception(server, err, &exception);
532 } while (exception.retry);
534 nfs_put_lock_context(lock);
540 nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
542 struct nfs42_layoutstat_data *data = calldata;
543 struct inode *inode = data->inode;
544 struct nfs_server *server = NFS_SERVER(inode);
545 struct pnfs_layout_hdr *lo;
547 spin_lock(&inode->i_lock);
548 lo = NFS_I(inode)->layout;
549 if (!pnfs_layout_is_valid(lo)) {
550 spin_unlock(&inode->i_lock);
554 nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
555 spin_unlock(&inode->i_lock);
556 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
557 &data->res.seq_res, task);
561 nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
563 struct nfs42_layoutstat_data *data = calldata;
564 struct inode *inode = data->inode;
565 struct pnfs_layout_hdr *lo;
567 if (!nfs4_sequence_done(task, &data->res.seq_res))
570 switch (task->tk_status) {
573 case -NFS4ERR_BADHANDLE:
575 pnfs_destroy_layout(NFS_I(inode));
577 case -NFS4ERR_EXPIRED:
578 case -NFS4ERR_ADMIN_REVOKED:
579 case -NFS4ERR_DELEG_REVOKED:
580 case -NFS4ERR_STALE_STATEID:
581 case -NFS4ERR_BAD_STATEID:
582 spin_lock(&inode->i_lock);
583 lo = NFS_I(inode)->layout;
584 if (pnfs_layout_is_valid(lo) &&
585 nfs4_stateid_match(&data->args.stateid,
590 * Mark the bad layout state as invalid, then retry
591 * with the current stateid.
593 pnfs_mark_layout_stateid_invalid(lo, &head);
594 spin_unlock(&inode->i_lock);
595 pnfs_free_lseg_list(&head);
596 nfs_commit_inode(inode, 0);
598 spin_unlock(&inode->i_lock);
600 case -NFS4ERR_OLD_STATEID:
601 spin_lock(&inode->i_lock);
602 lo = NFS_I(inode)->layout;
603 if (pnfs_layout_is_valid(lo) &&
604 nfs4_stateid_match_other(&data->args.stateid,
606 /* Do we need to delay before resending? */
607 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
608 &data->args.stateid))
610 rpc_restart_call_prepare(task);
612 spin_unlock(&inode->i_lock);
616 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
621 nfs42_layoutstat_release(void *calldata)
623 struct nfs42_layoutstat_data *data = calldata;
624 struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo;
627 for (i = 0; i < data->args.num_dev; i++) {
628 if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free)
629 devinfo[i].ld_private.ops->free(&devinfo[i].ld_private);
632 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
633 smp_mb__before_atomic();
634 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
635 smp_mb__after_atomic();
636 nfs_iput_and_deactive(data->inode);
637 kfree(data->args.devinfo);
641 static const struct rpc_call_ops nfs42_layoutstat_ops = {
642 .rpc_call_prepare = nfs42_layoutstat_prepare,
643 .rpc_call_done = nfs42_layoutstat_done,
644 .rpc_release = nfs42_layoutstat_release,
647 int nfs42_proc_layoutstats_generic(struct nfs_server *server,
648 struct nfs42_layoutstat_data *data)
650 struct rpc_message msg = {
651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
652 .rpc_argp = &data->args,
653 .rpc_resp = &data->res,
655 struct rpc_task_setup task_setup = {
656 .rpc_client = server->client,
658 .callback_ops = &nfs42_layoutstat_ops,
659 .callback_data = data,
660 .flags = RPC_TASK_ASYNC,
662 struct rpc_task *task;
664 data->inode = nfs_igrab_and_active(data->args.inode);
666 nfs42_layoutstat_release(data);
669 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
670 task = rpc_run_task(&task_setup);
672 return PTR_ERR(task);
677 static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
678 struct file *dst_f, struct nfs_lock_context *src_lock,
679 struct nfs_lock_context *dst_lock, loff_t src_offset,
680 loff_t dst_offset, loff_t count)
682 struct inode *src_inode = file_inode(src_f);
683 struct inode *dst_inode = file_inode(dst_f);
684 struct nfs_server *server = NFS_SERVER(dst_inode);
685 struct nfs42_clone_args args = {
686 .src_fh = NFS_FH(src_inode),
687 .dst_fh = NFS_FH(dst_inode),
688 .src_offset = src_offset,
689 .dst_offset = dst_offset,
691 .dst_bitmask = server->cache_consistency_bitmask,
693 struct nfs42_clone_res res = {
698 msg->rpc_argp = &args;
699 msg->rpc_resp = &res;
701 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
702 src_lock, FMODE_READ);
706 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
707 dst_lock, FMODE_WRITE);
711 res.dst_fattr = nfs_alloc_fattr();
715 status = nfs4_call_sync(server->client, server, msg,
716 &args.seq_args, &res.seq_res, 0);
718 status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
720 kfree(res.dst_fattr);
724 int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
725 loff_t src_offset, loff_t dst_offset, loff_t count)
727 struct rpc_message msg = {
728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
730 struct inode *inode = file_inode(src_f);
731 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
732 struct nfs_lock_context *src_lock;
733 struct nfs_lock_context *dst_lock;
734 struct nfs4_exception src_exception = { };
735 struct nfs4_exception dst_exception = { };
738 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
741 src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
742 if (IS_ERR(src_lock))
743 return PTR_ERR(src_lock);
745 src_exception.inode = file_inode(src_f);
746 src_exception.state = src_lock->open_context->state;
748 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
749 if (IS_ERR(dst_lock)) {
750 err = PTR_ERR(dst_lock);
751 goto out_put_src_lock;
754 dst_exception.inode = file_inode(dst_f);
755 dst_exception.state = dst_lock->open_context->state;
758 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
759 src_offset, dst_offset, count);
760 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
761 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
766 err2 = nfs4_handle_exception(server, err, &src_exception);
767 err = nfs4_handle_exception(server, err, &dst_exception);
770 } while (src_exception.retry || dst_exception.retry);
772 nfs_put_lock_context(dst_lock);
774 nfs_put_lock_context(src_lock);