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 nfs_server
*server
= NFS_SERVER(file_inode(filep
));
63 struct nfs4_exception exception
= { };
64 struct nfs_lock_context
*lock
;
67 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
71 exception
.inode
= file_inode(filep
);
72 exception
.state
= lock
->open_context
->state
;
75 err
= _nfs42_proc_fallocate(msg
, filep
, lock
, offset
, len
);
76 if (err
== -ENOTSUPP
) {
80 err
= nfs4_handle_exception(server
, err
, &exception
);
81 } while (exception
.retry
);
83 nfs_put_lock_context(lock
);
87 int nfs42_proc_allocate(struct file
*filep
, loff_t offset
, loff_t len
)
89 struct rpc_message msg
= {
90 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ALLOCATE
],
92 struct inode
*inode
= file_inode(filep
);
95 if (!nfs_server_capable(inode
, NFS_CAP_ALLOCATE
))
100 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
101 if (err
== -EOPNOTSUPP
)
102 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_ALLOCATE
;
108 int nfs42_proc_deallocate(struct file
*filep
, loff_t offset
, loff_t len
)
110 struct rpc_message msg
= {
111 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DEALLOCATE
],
113 struct inode
*inode
= file_inode(filep
);
116 if (!nfs_server_capable(inode
, NFS_CAP_DEALLOCATE
))
120 err
= nfs_sync_inode(inode
);
124 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
126 truncate_pagecache_range(inode
, offset
, (offset
+ len
) -1);
127 if (err
== -EOPNOTSUPP
)
128 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_DEALLOCATE
;
134 static int handle_async_copy(struct nfs42_copy_res
*res
,
135 struct nfs_server
*server
,
138 nfs4_stateid
*src_stateid
)
140 struct nfs4_copy_state
*copy
, *tmp_copy
;
141 int status
= NFS4_OK
;
142 bool found_pending
= false;
143 struct nfs_open_context
*ctx
= nfs_file_open_context(dst
);
145 copy
= kzalloc(sizeof(struct nfs4_copy_state
), GFP_NOFS
);
149 spin_lock(&server
->nfs_client
->cl_lock
);
150 list_for_each_entry(tmp_copy
, &server
->nfs_client
->pending_cb_stateids
,
152 if (memcmp(&res
->write_res
.stateid
, &tmp_copy
->stateid
,
155 found_pending
= true;
156 list_del(&tmp_copy
->copies
);
160 spin_unlock(&server
->nfs_client
->cl_lock
);
166 memcpy(©
->stateid
, &res
->write_res
.stateid
, NFS4_STATEID_SIZE
);
167 init_completion(©
->completion
);
168 copy
->parent_state
= ctx
->state
;
170 list_add_tail(©
->copies
, &server
->ss_copies
);
171 spin_unlock(&server
->nfs_client
->cl_lock
);
173 status
= wait_for_completion_interruptible(©
->completion
);
174 spin_lock(&server
->nfs_client
->cl_lock
);
175 list_del_init(©
->copies
);
176 spin_unlock(&server
->nfs_client
->cl_lock
);
177 if (status
== -ERESTARTSYS
) {
179 } else if (copy
->flags
) {
184 res
->write_res
.count
= copy
->count
;
185 memcpy(&res
->write_res
.verifier
, ©
->verf
, sizeof(copy
->verf
));
186 status
= -copy
->error
;
191 nfs42_do_offload_cancel_async(dst
, ©
->stateid
);
196 static int process_copy_commit(struct file
*dst
, loff_t pos_dst
,
197 struct nfs42_copy_res
*res
)
199 struct nfs_commitres cres
;
200 int status
= -ENOMEM
;
202 cres
.verf
= kzalloc(sizeof(struct nfs_writeverf
), GFP_NOFS
);
206 status
= nfs4_proc_commit(dst
, pos_dst
, res
->write_res
.count
, &cres
);
209 if (nfs_write_verifier_cmp(&res
->write_res
.verifier
.verifier
,
210 &cres
.verf
->verifier
)) {
211 dprintk("commit verf differs from copy verf\n");
220 static ssize_t
_nfs42_proc_copy(struct file
*src
,
221 struct nfs_lock_context
*src_lock
,
223 struct nfs_lock_context
*dst_lock
,
224 struct nfs42_copy_args
*args
,
225 struct nfs42_copy_res
*res
)
227 struct rpc_message msg
= {
228 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COPY
],
232 struct inode
*dst_inode
= file_inode(dst
);
233 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
234 loff_t pos_src
= args
->src_pos
;
235 loff_t pos_dst
= args
->dst_pos
;
236 size_t count
= args
->count
;
239 status
= nfs4_set_rw_stateid(&args
->src_stateid
, src_lock
->open_context
,
240 src_lock
, FMODE_READ
);
244 status
= nfs_filemap_write_and_wait_range(file_inode(src
)->i_mapping
,
245 pos_src
, pos_src
+ (loff_t
)count
- 1);
249 status
= nfs4_set_rw_stateid(&args
->dst_stateid
, dst_lock
->open_context
,
250 dst_lock
, FMODE_WRITE
);
254 status
= nfs_sync_inode(dst_inode
);
258 res
->commit_res
.verf
= NULL
;
260 res
->commit_res
.verf
=
261 kzalloc(sizeof(struct nfs_writeverf
), GFP_NOFS
);
262 if (!res
->commit_res
.verf
)
265 set_bit(NFS_CLNT_DST_SSC_COPY_STATE
,
266 &dst_lock
->open_context
->state
->flags
);
268 status
= nfs4_call_sync(server
->client
, server
, &msg
,
269 &args
->seq_args
, &res
->seq_res
, 0);
270 if (status
== -ENOTSUPP
)
271 server
->caps
&= ~NFS_CAP_COPY
;
276 nfs_write_verifier_cmp(&res
->write_res
.verifier
.verifier
,
277 &res
->commit_res
.verf
->verifier
)) {
282 if (!res
->synchronous
) {
283 status
= handle_async_copy(res
, server
, src
, dst
,
289 if ((!res
->synchronous
|| !args
->sync
) &&
290 res
->write_res
.verifier
.committed
!= NFS_FILE_SYNC
) {
291 status
= process_copy_commit(dst
, pos_dst
, res
);
296 truncate_pagecache_range(dst_inode
, pos_dst
,
297 pos_dst
+ res
->write_res
.count
);
299 status
= res
->write_res
.count
;
302 kfree(res
->commit_res
.verf
);
306 ssize_t
nfs42_proc_copy(struct file
*src
, loff_t pos_src
,
307 struct file
*dst
, loff_t pos_dst
,
310 struct nfs_server
*server
= NFS_SERVER(file_inode(dst
));
311 struct nfs_lock_context
*src_lock
;
312 struct nfs_lock_context
*dst_lock
;
313 struct nfs42_copy_args args
= {
314 .src_fh
= NFS_FH(file_inode(src
)),
316 .dst_fh
= NFS_FH(file_inode(dst
)),
321 struct nfs42_copy_res res
;
322 struct nfs4_exception src_exception
= {
323 .inode
= file_inode(src
),
324 .stateid
= &args
.src_stateid
,
326 struct nfs4_exception dst_exception
= {
327 .inode
= file_inode(dst
),
328 .stateid
= &args
.dst_stateid
,
332 src_lock
= nfs_get_lock_context(nfs_file_open_context(src
));
333 if (IS_ERR(src_lock
))
334 return PTR_ERR(src_lock
);
336 src_exception
.state
= src_lock
->open_context
->state
;
338 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst
));
339 if (IS_ERR(dst_lock
)) {
340 err
= PTR_ERR(dst_lock
);
341 goto out_put_src_lock
;
344 dst_exception
.state
= dst_lock
->open_context
->state
;
347 inode_lock(file_inode(dst
));
348 err
= _nfs42_proc_copy(src
, src_lock
,
351 inode_unlock(file_inode(dst
));
355 if (err
== -ENOTSUPP
) {
358 } else if (err
== -EAGAIN
) {
359 dst_exception
.retry
= 1;
361 } else if (err
== -NFS4ERR_OFFLOAD_NO_REQS
&& !args
.sync
) {
363 dst_exception
.retry
= 1;
367 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
368 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
371 } while (src_exception
.retry
|| dst_exception
.retry
);
373 nfs_put_lock_context(dst_lock
);
375 nfs_put_lock_context(src_lock
);
379 struct nfs42_offloadcancel_data
{
380 struct nfs_server
*seq_server
;
381 struct nfs42_offload_status_args args
;
382 struct nfs42_offload_status_res res
;
385 static void nfs42_offload_cancel_prepare(struct rpc_task
*task
, void *calldata
)
387 struct nfs42_offloadcancel_data
*data
= calldata
;
389 nfs4_setup_sequence(data
->seq_server
->nfs_client
,
390 &data
->args
.osa_seq_args
,
391 &data
->res
.osr_seq_res
, task
);
394 static void nfs42_offload_cancel_done(struct rpc_task
*task
, void *calldata
)
396 struct nfs42_offloadcancel_data
*data
= calldata
;
398 nfs41_sequence_done(task
, &data
->res
.osr_seq_res
);
399 if (task
->tk_status
&&
400 nfs4_async_handle_error(task
, data
->seq_server
, NULL
,
402 rpc_restart_call_prepare(task
);
405 static void nfs42_free_offloadcancel_data(void *data
)
410 static const struct rpc_call_ops nfs42_offload_cancel_ops
= {
411 .rpc_call_prepare
= nfs42_offload_cancel_prepare
,
412 .rpc_call_done
= nfs42_offload_cancel_done
,
413 .rpc_release
= nfs42_free_offloadcancel_data
,
416 static int nfs42_do_offload_cancel_async(struct file
*dst
,
417 nfs4_stateid
*stateid
)
419 struct nfs_server
*dst_server
= NFS_SERVER(file_inode(dst
));
420 struct nfs42_offloadcancel_data
*data
= NULL
;
421 struct nfs_open_context
*ctx
= nfs_file_open_context(dst
);
422 struct rpc_task
*task
;
423 struct rpc_message msg
= {
424 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OFFLOAD_CANCEL
],
425 .rpc_cred
= ctx
->cred
,
427 struct rpc_task_setup task_setup_data
= {
428 .rpc_client
= dst_server
->client
,
430 .callback_ops
= &nfs42_offload_cancel_ops
,
431 .workqueue
= nfsiod_workqueue
,
432 .flags
= RPC_TASK_ASYNC
,
436 if (!(dst_server
->caps
& NFS_CAP_OFFLOAD_CANCEL
))
439 data
= kzalloc(sizeof(struct nfs42_offloadcancel_data
), GFP_NOFS
);
443 data
->seq_server
= dst_server
;
444 data
->args
.osa_src_fh
= NFS_FH(file_inode(dst
));
445 memcpy(&data
->args
.osa_stateid
, stateid
,
446 sizeof(data
->args
.osa_stateid
));
447 msg
.rpc_argp
= &data
->args
;
448 msg
.rpc_resp
= &data
->res
;
449 task_setup_data
.callback_data
= data
;
450 nfs4_init_sequence(&data
->args
.osa_seq_args
, &data
->res
.osr_seq_res
,
452 task
= rpc_run_task(&task_setup_data
);
454 return PTR_ERR(task
);
455 status
= rpc_wait_for_completion_task(task
);
456 if (status
== -ENOTSUPP
)
457 dst_server
->caps
&= ~NFS_CAP_OFFLOAD_CANCEL
;
462 static loff_t
_nfs42_proc_llseek(struct file
*filep
,
463 struct nfs_lock_context
*lock
, loff_t offset
, int whence
)
465 struct inode
*inode
= file_inode(filep
);
466 struct nfs42_seek_args args
= {
467 .sa_fh
= NFS_FH(inode
),
469 .sa_what
= (whence
== SEEK_HOLE
) ?
470 NFS4_CONTENT_HOLE
: NFS4_CONTENT_DATA
,
472 struct nfs42_seek_res res
;
473 struct rpc_message msg
= {
474 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEEK
],
478 struct nfs_server
*server
= NFS_SERVER(inode
);
481 if (!nfs_server_capable(inode
, NFS_CAP_SEEK
))
484 status
= nfs4_set_rw_stateid(&args
.sa_stateid
, lock
->open_context
,
489 status
= nfs_filemap_write_and_wait_range(inode
->i_mapping
,
494 status
= nfs4_call_sync(server
->client
, server
, &msg
,
495 &args
.seq_args
, &res
.seq_res
, 0);
496 if (status
== -ENOTSUPP
)
497 server
->caps
&= ~NFS_CAP_SEEK
;
501 return vfs_setpos(filep
, res
.sr_offset
, inode
->i_sb
->s_maxbytes
);
504 loff_t
nfs42_proc_llseek(struct file
*filep
, loff_t offset
, int whence
)
506 struct nfs_server
*server
= NFS_SERVER(file_inode(filep
));
507 struct nfs4_exception exception
= { };
508 struct nfs_lock_context
*lock
;
511 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
513 return PTR_ERR(lock
);
515 exception
.inode
= file_inode(filep
);
516 exception
.state
= lock
->open_context
->state
;
519 err
= _nfs42_proc_llseek(filep
, lock
, offset
, whence
);
522 if (err
== -ENOTSUPP
) {
526 err
= nfs4_handle_exception(server
, err
, &exception
);
527 } while (exception
.retry
);
529 nfs_put_lock_context(lock
);
535 nfs42_layoutstat_prepare(struct rpc_task
*task
, void *calldata
)
537 struct nfs42_layoutstat_data
*data
= calldata
;
538 struct inode
*inode
= data
->inode
;
539 struct nfs_server
*server
= NFS_SERVER(inode
);
540 struct pnfs_layout_hdr
*lo
;
542 spin_lock(&inode
->i_lock
);
543 lo
= NFS_I(inode
)->layout
;
544 if (!pnfs_layout_is_valid(lo
)) {
545 spin_unlock(&inode
->i_lock
);
549 nfs4_stateid_copy(&data
->args
.stateid
, &lo
->plh_stateid
);
550 spin_unlock(&inode
->i_lock
);
551 nfs4_setup_sequence(server
->nfs_client
, &data
->args
.seq_args
,
552 &data
->res
.seq_res
, task
);
556 nfs42_layoutstat_done(struct rpc_task
*task
, void *calldata
)
558 struct nfs42_layoutstat_data
*data
= calldata
;
559 struct inode
*inode
= data
->inode
;
560 struct pnfs_layout_hdr
*lo
;
562 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
565 switch (task
->tk_status
) {
568 case -NFS4ERR_BADHANDLE
:
570 pnfs_destroy_layout(NFS_I(inode
));
572 case -NFS4ERR_EXPIRED
:
573 case -NFS4ERR_ADMIN_REVOKED
:
574 case -NFS4ERR_DELEG_REVOKED
:
575 case -NFS4ERR_STALE_STATEID
:
576 case -NFS4ERR_BAD_STATEID
:
577 spin_lock(&inode
->i_lock
);
578 lo
= NFS_I(inode
)->layout
;
579 if (pnfs_layout_is_valid(lo
) &&
580 nfs4_stateid_match(&data
->args
.stateid
,
585 * Mark the bad layout state as invalid, then retry
586 * with the current stateid.
588 pnfs_mark_layout_stateid_invalid(lo
, &head
);
589 spin_unlock(&inode
->i_lock
);
590 pnfs_free_lseg_list(&head
);
591 nfs_commit_inode(inode
, 0);
593 spin_unlock(&inode
->i_lock
);
595 case -NFS4ERR_OLD_STATEID
:
596 spin_lock(&inode
->i_lock
);
597 lo
= NFS_I(inode
)->layout
;
598 if (pnfs_layout_is_valid(lo
) &&
599 nfs4_stateid_match_other(&data
->args
.stateid
,
601 /* Do we need to delay before resending? */
602 if (!nfs4_stateid_is_newer(&lo
->plh_stateid
,
603 &data
->args
.stateid
))
605 rpc_restart_call_prepare(task
);
607 spin_unlock(&inode
->i_lock
);
611 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_LAYOUTSTATS
;
616 nfs42_layoutstat_release(void *calldata
)
618 struct nfs42_layoutstat_data
*data
= calldata
;
619 struct nfs42_layoutstat_devinfo
*devinfo
= data
->args
.devinfo
;
622 for (i
= 0; i
< data
->args
.num_dev
; i
++) {
623 if (devinfo
[i
].ld_private
.ops
&& devinfo
[i
].ld_private
.ops
->free
)
624 devinfo
[i
].ld_private
.ops
->free(&devinfo
[i
].ld_private
);
627 pnfs_put_layout_hdr(NFS_I(data
->args
.inode
)->layout
);
628 smp_mb__before_atomic();
629 clear_bit(NFS_INO_LAYOUTSTATS
, &NFS_I(data
->args
.inode
)->flags
);
630 smp_mb__after_atomic();
631 nfs_iput_and_deactive(data
->inode
);
632 kfree(data
->args
.devinfo
);
636 static const struct rpc_call_ops nfs42_layoutstat_ops
= {
637 .rpc_call_prepare
= nfs42_layoutstat_prepare
,
638 .rpc_call_done
= nfs42_layoutstat_done
,
639 .rpc_release
= nfs42_layoutstat_release
,
642 int nfs42_proc_layoutstats_generic(struct nfs_server
*server
,
643 struct nfs42_layoutstat_data
*data
)
645 struct rpc_message msg
= {
646 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTSTATS
],
647 .rpc_argp
= &data
->args
,
648 .rpc_resp
= &data
->res
,
650 struct rpc_task_setup task_setup
= {
651 .rpc_client
= server
->client
,
653 .callback_ops
= &nfs42_layoutstat_ops
,
654 .callback_data
= data
,
655 .flags
= RPC_TASK_ASYNC
,
657 struct rpc_task
*task
;
659 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
661 nfs42_layoutstat_release(data
);
664 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0, 0);
665 task
= rpc_run_task(&task_setup
);
667 return PTR_ERR(task
);
672 static int _nfs42_proc_clone(struct rpc_message
*msg
, struct file
*src_f
,
673 struct file
*dst_f
, struct nfs_lock_context
*src_lock
,
674 struct nfs_lock_context
*dst_lock
, loff_t src_offset
,
675 loff_t dst_offset
, loff_t count
)
677 struct inode
*src_inode
= file_inode(src_f
);
678 struct inode
*dst_inode
= file_inode(dst_f
);
679 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
680 struct nfs42_clone_args args
= {
681 .src_fh
= NFS_FH(src_inode
),
682 .dst_fh
= NFS_FH(dst_inode
),
683 .src_offset
= src_offset
,
684 .dst_offset
= dst_offset
,
686 .dst_bitmask
= server
->cache_consistency_bitmask
,
688 struct nfs42_clone_res res
= {
693 msg
->rpc_argp
= &args
;
694 msg
->rpc_resp
= &res
;
696 status
= nfs4_set_rw_stateid(&args
.src_stateid
, src_lock
->open_context
,
697 src_lock
, FMODE_READ
);
701 status
= nfs4_set_rw_stateid(&args
.dst_stateid
, dst_lock
->open_context
,
702 dst_lock
, FMODE_WRITE
);
706 res
.dst_fattr
= nfs_alloc_fattr();
710 status
= nfs4_call_sync(server
->client
, server
, msg
,
711 &args
.seq_args
, &res
.seq_res
, 0);
713 status
= nfs_post_op_update_inode(dst_inode
, res
.dst_fattr
);
715 kfree(res
.dst_fattr
);
719 int nfs42_proc_clone(struct file
*src_f
, struct file
*dst_f
,
720 loff_t src_offset
, loff_t dst_offset
, loff_t count
)
722 struct rpc_message msg
= {
723 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLONE
],
725 struct inode
*inode
= file_inode(src_f
);
726 struct nfs_server
*server
= NFS_SERVER(file_inode(src_f
));
727 struct nfs_lock_context
*src_lock
;
728 struct nfs_lock_context
*dst_lock
;
729 struct nfs4_exception src_exception
= { };
730 struct nfs4_exception dst_exception
= { };
733 if (!nfs_server_capable(inode
, NFS_CAP_CLONE
))
736 src_lock
= nfs_get_lock_context(nfs_file_open_context(src_f
));
737 if (IS_ERR(src_lock
))
738 return PTR_ERR(src_lock
);
740 src_exception
.inode
= file_inode(src_f
);
741 src_exception
.state
= src_lock
->open_context
->state
;
743 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst_f
));
744 if (IS_ERR(dst_lock
)) {
745 err
= PTR_ERR(dst_lock
);
746 goto out_put_src_lock
;
749 dst_exception
.inode
= file_inode(dst_f
);
750 dst_exception
.state
= dst_lock
->open_context
->state
;
753 err
= _nfs42_proc_clone(&msg
, src_f
, dst_f
, src_lock
, dst_lock
,
754 src_offset
, dst_offset
, count
);
755 if (err
== -ENOTSUPP
|| err
== -EOPNOTSUPP
) {
756 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_CLONE
;
761 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
762 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
765 } while (src_exception
.retry
|| dst_exception
.retry
);
767 nfs_put_lock_context(dst_lock
);
769 nfs_put_lock_context(src_lock
);