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"
21 #define NFSDBG_FACILITY NFSDBG_PROC
22 static int nfs42_do_offload_cancel_async(struct file
*dst
, nfs4_stateid
*std
);
24 static void nfs42_set_netaddr(struct file
*filep
, struct nfs42_netaddr
*naddr
)
26 struct nfs_client
*clp
= (NFS_SERVER(file_inode(filep
)))->nfs_client
;
27 unsigned short port
= 2049;
30 naddr
->netid_len
= scnprintf(naddr
->netid
,
31 sizeof(naddr
->netid
), "%s",
32 rpc_peeraddr2str(clp
->cl_rpcclient
,
34 naddr
->addr_len
= scnprintf(naddr
->addr
,
37 rpc_peeraddr2str(clp
->cl_rpcclient
,
39 port
>> 8, port
& 255);
43 static int _nfs42_proc_fallocate(struct rpc_message
*msg
, struct file
*filep
,
44 struct nfs_lock_context
*lock
, loff_t offset
, loff_t len
)
46 struct inode
*inode
= file_inode(filep
);
47 struct nfs_server
*server
= NFS_SERVER(inode
);
48 struct nfs42_falloc_args args
= {
49 .falloc_fh
= NFS_FH(inode
),
50 .falloc_offset
= offset
,
52 .falloc_bitmask
= nfs4_fattr_bitmap
,
54 struct nfs42_falloc_res res
= {
55 .falloc_server
= server
,
59 msg
->rpc_argp
= &args
;
62 status
= nfs4_set_rw_stateid(&args
.falloc_stateid
, lock
->open_context
,
65 if (status
== -EAGAIN
)
66 status
= -NFS4ERR_BAD_STATEID
;
70 res
.falloc_fattr
= nfs_alloc_fattr();
71 if (!res
.falloc_fattr
)
74 status
= nfs4_call_sync(server
->client
, server
, msg
,
75 &args
.seq_args
, &res
.seq_res
, 0);
77 status
= nfs_post_op_update_inode(inode
, res
.falloc_fattr
);
79 kfree(res
.falloc_fattr
);
83 static int nfs42_proc_fallocate(struct rpc_message
*msg
, struct file
*filep
,
84 loff_t offset
, loff_t len
)
86 struct nfs_server
*server
= NFS_SERVER(file_inode(filep
));
87 struct nfs4_exception exception
= { };
88 struct nfs_lock_context
*lock
;
91 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
95 exception
.inode
= file_inode(filep
);
96 exception
.state
= lock
->open_context
->state
;
99 err
= _nfs42_proc_fallocate(msg
, filep
, lock
, offset
, len
);
100 if (err
== -ENOTSUPP
) {
104 err
= nfs4_handle_exception(server
, err
, &exception
);
105 } while (exception
.retry
);
107 nfs_put_lock_context(lock
);
111 int nfs42_proc_allocate(struct file
*filep
, loff_t offset
, loff_t len
)
113 struct rpc_message msg
= {
114 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ALLOCATE
],
116 struct inode
*inode
= file_inode(filep
);
119 if (!nfs_server_capable(inode
, NFS_CAP_ALLOCATE
))
124 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
125 if (err
== -EOPNOTSUPP
)
126 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_ALLOCATE
;
132 int nfs42_proc_deallocate(struct file
*filep
, loff_t offset
, loff_t len
)
134 struct rpc_message msg
= {
135 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DEALLOCATE
],
137 struct inode
*inode
= file_inode(filep
);
140 if (!nfs_server_capable(inode
, NFS_CAP_DEALLOCATE
))
144 err
= nfs_sync_inode(inode
);
148 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
150 truncate_pagecache_range(inode
, offset
, (offset
+ len
) -1);
151 if (err
== -EOPNOTSUPP
)
152 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_DEALLOCATE
;
158 static int handle_async_copy(struct nfs42_copy_res
*res
,
159 struct nfs_server
*dst_server
,
160 struct nfs_server
*src_server
,
163 nfs4_stateid
*src_stateid
,
166 struct nfs4_copy_state
*copy
, *tmp_copy
;
167 int status
= NFS4_OK
;
168 bool found_pending
= false;
169 struct nfs_open_context
*dst_ctx
= nfs_file_open_context(dst
);
170 struct nfs_open_context
*src_ctx
= nfs_file_open_context(src
);
172 copy
= kzalloc(sizeof(struct nfs4_copy_state
), GFP_NOFS
);
176 spin_lock(&dst_server
->nfs_client
->cl_lock
);
177 list_for_each_entry(tmp_copy
,
178 &dst_server
->nfs_client
->pending_cb_stateids
,
180 if (memcmp(&res
->write_res
.stateid
, &tmp_copy
->stateid
,
183 found_pending
= true;
184 list_del(&tmp_copy
->copies
);
188 spin_unlock(&dst_server
->nfs_client
->cl_lock
);
194 memcpy(©
->stateid
, &res
->write_res
.stateid
, NFS4_STATEID_SIZE
);
195 init_completion(©
->completion
);
196 copy
->parent_dst_state
= dst_ctx
->state
;
197 copy
->parent_src_state
= src_ctx
->state
;
199 list_add_tail(©
->copies
, &dst_server
->ss_copies
);
200 spin_unlock(&dst_server
->nfs_client
->cl_lock
);
202 if (dst_server
!= src_server
) {
203 spin_lock(&src_server
->nfs_client
->cl_lock
);
204 list_add_tail(©
->src_copies
, &src_server
->ss_copies
);
205 spin_unlock(&src_server
->nfs_client
->cl_lock
);
208 status
= wait_for_completion_interruptible(©
->completion
);
209 spin_lock(&dst_server
->nfs_client
->cl_lock
);
210 list_del_init(©
->copies
);
211 spin_unlock(&dst_server
->nfs_client
->cl_lock
);
212 if (dst_server
!= src_server
) {
213 spin_lock(&src_server
->nfs_client
->cl_lock
);
214 list_del_init(©
->src_copies
);
215 spin_unlock(&src_server
->nfs_client
->cl_lock
);
217 if (status
== -ERESTARTSYS
) {
219 } else if (copy
->flags
|| copy
->error
== NFS4ERR_PARTNER_NO_AUTH
) {
225 res
->write_res
.count
= copy
->count
;
226 memcpy(&res
->write_res
.verifier
, ©
->verf
, sizeof(copy
->verf
));
227 status
= -copy
->error
;
233 nfs42_do_offload_cancel_async(dst
, ©
->stateid
);
234 if (!nfs42_files_from_same_server(src
, dst
))
235 nfs42_do_offload_cancel_async(src
, src_stateid
);
239 static int process_copy_commit(struct file
*dst
, loff_t pos_dst
,
240 struct nfs42_copy_res
*res
)
242 struct nfs_commitres cres
;
243 int status
= -ENOMEM
;
245 cres
.verf
= kzalloc(sizeof(struct nfs_writeverf
), GFP_NOFS
);
249 status
= nfs4_proc_commit(dst
, pos_dst
, res
->write_res
.count
, &cres
);
252 if (nfs_write_verifier_cmp(&res
->write_res
.verifier
.verifier
,
253 &cres
.verf
->verifier
)) {
254 dprintk("commit verf differs from copy verf\n");
263 static ssize_t
_nfs42_proc_copy(struct file
*src
,
264 struct nfs_lock_context
*src_lock
,
266 struct nfs_lock_context
*dst_lock
,
267 struct nfs42_copy_args
*args
,
268 struct nfs42_copy_res
*res
,
269 struct nl4_server
*nss
,
270 nfs4_stateid
*cnr_stateid
,
273 struct rpc_message msg
= {
274 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COPY
],
278 struct inode
*dst_inode
= file_inode(dst
);
279 struct inode
*src_inode
= file_inode(src
);
280 struct nfs_server
*dst_server
= NFS_SERVER(dst_inode
);
281 struct nfs_server
*src_server
= NFS_SERVER(src_inode
);
282 loff_t pos_src
= args
->src_pos
;
283 loff_t pos_dst
= args
->dst_pos
;
284 size_t count
= args
->count
;
289 nfs4_stateid_copy(&args
->src_stateid
, cnr_stateid
);
291 status
= nfs4_set_rw_stateid(&args
->src_stateid
,
292 src_lock
->open_context
, src_lock
, FMODE_READ
);
294 if (status
== -EAGAIN
)
295 status
= -NFS4ERR_BAD_STATEID
;
299 status
= nfs_filemap_write_and_wait_range(file_inode(src
)->i_mapping
,
300 pos_src
, pos_src
+ (loff_t
)count
- 1);
304 status
= nfs4_set_rw_stateid(&args
->dst_stateid
, dst_lock
->open_context
,
305 dst_lock
, FMODE_WRITE
);
307 if (status
== -EAGAIN
)
308 status
= -NFS4ERR_BAD_STATEID
;
312 status
= nfs_sync_inode(dst_inode
);
316 res
->commit_res
.verf
= NULL
;
318 res
->commit_res
.verf
=
319 kzalloc(sizeof(struct nfs_writeverf
), GFP_NOFS
);
320 if (!res
->commit_res
.verf
)
323 set_bit(NFS_CLNT_SRC_SSC_COPY_STATE
,
324 &src_lock
->open_context
->state
->flags
);
325 set_bit(NFS_CLNT_DST_SSC_COPY_STATE
,
326 &dst_lock
->open_context
->state
->flags
);
328 status
= nfs4_call_sync(dst_server
->client
, dst_server
, &msg
,
329 &args
->seq_args
, &res
->seq_res
, 0);
330 if (status
== -ENOTSUPP
)
331 dst_server
->caps
&= ~NFS_CAP_COPY
;
336 nfs_write_verifier_cmp(&res
->write_res
.verifier
.verifier
,
337 &res
->commit_res
.verf
->verifier
)) {
342 if (!res
->synchronous
) {
343 status
= handle_async_copy(res
, dst_server
, src_server
, src
,
344 dst
, &args
->src_stateid
, restart
);
349 if ((!res
->synchronous
|| !args
->sync
) &&
350 res
->write_res
.verifier
.committed
!= NFS_FILE_SYNC
) {
351 status
= process_copy_commit(dst
, pos_dst
, res
);
356 truncate_pagecache_range(dst_inode
, pos_dst
,
357 pos_dst
+ res
->write_res
.count
);
359 status
= res
->write_res
.count
;
362 kfree(res
->commit_res
.verf
);
366 ssize_t
nfs42_proc_copy(struct file
*src
, loff_t pos_src
,
367 struct file
*dst
, loff_t pos_dst
, size_t count
,
368 struct nl4_server
*nss
,
369 nfs4_stateid
*cnr_stateid
, bool sync
)
371 struct nfs_server
*server
= NFS_SERVER(file_inode(dst
));
372 struct nfs_lock_context
*src_lock
;
373 struct nfs_lock_context
*dst_lock
;
374 struct nfs42_copy_args args
= {
375 .src_fh
= NFS_FH(file_inode(src
)),
377 .dst_fh
= NFS_FH(file_inode(dst
)),
382 struct nfs42_copy_res res
;
383 struct nfs4_exception src_exception
= {
384 .inode
= file_inode(src
),
385 .stateid
= &args
.src_stateid
,
387 struct nfs4_exception dst_exception
= {
388 .inode
= file_inode(dst
),
389 .stateid
= &args
.dst_stateid
,
392 bool restart
= false;
394 src_lock
= nfs_get_lock_context(nfs_file_open_context(src
));
395 if (IS_ERR(src_lock
))
396 return PTR_ERR(src_lock
);
398 src_exception
.state
= src_lock
->open_context
->state
;
400 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst
));
401 if (IS_ERR(dst_lock
)) {
402 err
= PTR_ERR(dst_lock
);
403 goto out_put_src_lock
;
406 dst_exception
.state
= dst_lock
->open_context
->state
;
409 inode_lock(file_inode(dst
));
410 err
= _nfs42_proc_copy(src
, src_lock
,
413 nss
, cnr_stateid
, &restart
);
414 inode_unlock(file_inode(dst
));
418 if (err
== -ENOTSUPP
&&
419 nfs42_files_from_same_server(src
, dst
)) {
422 } else if (err
== -EAGAIN
) {
424 dst_exception
.retry
= 1;
428 } else if (err
== -NFS4ERR_OFFLOAD_NO_REQS
&& !args
.sync
) {
430 dst_exception
.retry
= 1;
432 } else if ((err
== -ESTALE
||
433 err
== -NFS4ERR_OFFLOAD_DENIED
||
435 !nfs42_files_from_same_server(src
, dst
)) {
436 nfs42_do_offload_cancel_async(src
, &args
.src_stateid
);
441 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
442 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
445 } while (src_exception
.retry
|| dst_exception
.retry
);
447 nfs_put_lock_context(dst_lock
);
449 nfs_put_lock_context(src_lock
);
453 struct nfs42_offloadcancel_data
{
454 struct nfs_server
*seq_server
;
455 struct nfs42_offload_status_args args
;
456 struct nfs42_offload_status_res res
;
459 static void nfs42_offload_cancel_prepare(struct rpc_task
*task
, void *calldata
)
461 struct nfs42_offloadcancel_data
*data
= calldata
;
463 nfs4_setup_sequence(data
->seq_server
->nfs_client
,
464 &data
->args
.osa_seq_args
,
465 &data
->res
.osr_seq_res
, task
);
468 static void nfs42_offload_cancel_done(struct rpc_task
*task
, void *calldata
)
470 struct nfs42_offloadcancel_data
*data
= calldata
;
472 nfs41_sequence_done(task
, &data
->res
.osr_seq_res
);
473 if (task
->tk_status
&&
474 nfs4_async_handle_error(task
, data
->seq_server
, NULL
,
476 rpc_restart_call_prepare(task
);
479 static void nfs42_free_offloadcancel_data(void *data
)
484 static const struct rpc_call_ops nfs42_offload_cancel_ops
= {
485 .rpc_call_prepare
= nfs42_offload_cancel_prepare
,
486 .rpc_call_done
= nfs42_offload_cancel_done
,
487 .rpc_release
= nfs42_free_offloadcancel_data
,
490 static int nfs42_do_offload_cancel_async(struct file
*dst
,
491 nfs4_stateid
*stateid
)
493 struct nfs_server
*dst_server
= NFS_SERVER(file_inode(dst
));
494 struct nfs42_offloadcancel_data
*data
= NULL
;
495 struct nfs_open_context
*ctx
= nfs_file_open_context(dst
);
496 struct rpc_task
*task
;
497 struct rpc_message msg
= {
498 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OFFLOAD_CANCEL
],
499 .rpc_cred
= ctx
->cred
,
501 struct rpc_task_setup task_setup_data
= {
502 .rpc_client
= dst_server
->client
,
504 .callback_ops
= &nfs42_offload_cancel_ops
,
505 .workqueue
= nfsiod_workqueue
,
506 .flags
= RPC_TASK_ASYNC
,
510 if (!(dst_server
->caps
& NFS_CAP_OFFLOAD_CANCEL
))
513 data
= kzalloc(sizeof(struct nfs42_offloadcancel_data
), GFP_NOFS
);
517 data
->seq_server
= dst_server
;
518 data
->args
.osa_src_fh
= NFS_FH(file_inode(dst
));
519 memcpy(&data
->args
.osa_stateid
, stateid
,
520 sizeof(data
->args
.osa_stateid
));
521 msg
.rpc_argp
= &data
->args
;
522 msg
.rpc_resp
= &data
->res
;
523 task_setup_data
.callback_data
= data
;
524 nfs4_init_sequence(&data
->args
.osa_seq_args
, &data
->res
.osr_seq_res
,
526 task
= rpc_run_task(&task_setup_data
);
528 return PTR_ERR(task
);
529 status
= rpc_wait_for_completion_task(task
);
530 if (status
== -ENOTSUPP
)
531 dst_server
->caps
&= ~NFS_CAP_OFFLOAD_CANCEL
;
536 static int _nfs42_proc_copy_notify(struct file
*src
, struct file
*dst
,
537 struct nfs42_copy_notify_args
*args
,
538 struct nfs42_copy_notify_res
*res
)
540 struct nfs_server
*src_server
= NFS_SERVER(file_inode(src
));
541 struct rpc_message msg
= {
542 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COPY_NOTIFY
],
547 struct nfs_open_context
*ctx
;
548 struct nfs_lock_context
*l_ctx
;
550 ctx
= get_nfs_open_context(nfs_file_open_context(src
));
551 l_ctx
= nfs_get_lock_context(ctx
);
553 return PTR_ERR(l_ctx
);
555 status
= nfs4_set_rw_stateid(&args
->cna_src_stateid
, ctx
, l_ctx
,
557 nfs_put_lock_context(l_ctx
);
559 if (status
== -EAGAIN
)
560 status
= -NFS4ERR_BAD_STATEID
;
564 status
= nfs4_call_sync(src_server
->client
, src_server
, &msg
,
565 &args
->cna_seq_args
, &res
->cnr_seq_res
, 0);
566 if (status
== -ENOTSUPP
)
567 src_server
->caps
&= ~NFS_CAP_COPY_NOTIFY
;
569 put_nfs_open_context(nfs_file_open_context(src
));
573 int nfs42_proc_copy_notify(struct file
*src
, struct file
*dst
,
574 struct nfs42_copy_notify_res
*res
)
576 struct nfs_server
*src_server
= NFS_SERVER(file_inode(src
));
577 struct nfs42_copy_notify_args
*args
;
578 struct nfs4_exception exception
= {
579 .inode
= file_inode(src
),
583 if (!(src_server
->caps
& NFS_CAP_COPY_NOTIFY
))
586 args
= kzalloc(sizeof(struct nfs42_copy_notify_args
), GFP_NOFS
);
590 args
->cna_src_fh
= NFS_FH(file_inode(src
)),
591 args
->cna_dst
.nl4_type
= NL4_NETADDR
;
592 nfs42_set_netaddr(dst
, &args
->cna_dst
.u
.nl4_addr
);
593 exception
.stateid
= &args
->cna_src_stateid
;
596 status
= _nfs42_proc_copy_notify(src
, dst
, args
, res
);
597 if (status
== -ENOTSUPP
) {
598 status
= -EOPNOTSUPP
;
601 status
= nfs4_handle_exception(src_server
, status
, &exception
);
602 } while (exception
.retry
);
609 static loff_t
_nfs42_proc_llseek(struct file
*filep
,
610 struct nfs_lock_context
*lock
, loff_t offset
, int whence
)
612 struct inode
*inode
= file_inode(filep
);
613 struct nfs42_seek_args args
= {
614 .sa_fh
= NFS_FH(inode
),
616 .sa_what
= (whence
== SEEK_HOLE
) ?
617 NFS4_CONTENT_HOLE
: NFS4_CONTENT_DATA
,
619 struct nfs42_seek_res res
;
620 struct rpc_message msg
= {
621 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEEK
],
625 struct nfs_server
*server
= NFS_SERVER(inode
);
628 if (!nfs_server_capable(inode
, NFS_CAP_SEEK
))
631 status
= nfs4_set_rw_stateid(&args
.sa_stateid
, lock
->open_context
,
634 if (status
== -EAGAIN
)
635 status
= -NFS4ERR_BAD_STATEID
;
639 status
= nfs_filemap_write_and_wait_range(inode
->i_mapping
,
644 status
= nfs4_call_sync(server
->client
, server
, &msg
,
645 &args
.seq_args
, &res
.seq_res
, 0);
646 if (status
== -ENOTSUPP
)
647 server
->caps
&= ~NFS_CAP_SEEK
;
651 return vfs_setpos(filep
, res
.sr_offset
, inode
->i_sb
->s_maxbytes
);
654 loff_t
nfs42_proc_llseek(struct file
*filep
, loff_t offset
, int whence
)
656 struct nfs_server
*server
= NFS_SERVER(file_inode(filep
));
657 struct nfs4_exception exception
= { };
658 struct nfs_lock_context
*lock
;
661 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
663 return PTR_ERR(lock
);
665 exception
.inode
= file_inode(filep
);
666 exception
.state
= lock
->open_context
->state
;
669 err
= _nfs42_proc_llseek(filep
, lock
, offset
, whence
);
672 if (err
== -ENOTSUPP
) {
676 err
= nfs4_handle_exception(server
, err
, &exception
);
677 } while (exception
.retry
);
679 nfs_put_lock_context(lock
);
685 nfs42_layoutstat_prepare(struct rpc_task
*task
, void *calldata
)
687 struct nfs42_layoutstat_data
*data
= calldata
;
688 struct inode
*inode
= data
->inode
;
689 struct nfs_server
*server
= NFS_SERVER(inode
);
690 struct pnfs_layout_hdr
*lo
;
692 spin_lock(&inode
->i_lock
);
693 lo
= NFS_I(inode
)->layout
;
694 if (!pnfs_layout_is_valid(lo
)) {
695 spin_unlock(&inode
->i_lock
);
699 nfs4_stateid_copy(&data
->args
.stateid
, &lo
->plh_stateid
);
700 spin_unlock(&inode
->i_lock
);
701 nfs4_setup_sequence(server
->nfs_client
, &data
->args
.seq_args
,
702 &data
->res
.seq_res
, task
);
706 nfs42_layoutstat_done(struct rpc_task
*task
, void *calldata
)
708 struct nfs42_layoutstat_data
*data
= calldata
;
709 struct inode
*inode
= data
->inode
;
710 struct pnfs_layout_hdr
*lo
;
712 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
715 switch (task
->tk_status
) {
718 case -NFS4ERR_BADHANDLE
:
720 pnfs_destroy_layout(NFS_I(inode
));
722 case -NFS4ERR_EXPIRED
:
723 case -NFS4ERR_ADMIN_REVOKED
:
724 case -NFS4ERR_DELEG_REVOKED
:
725 case -NFS4ERR_STALE_STATEID
:
726 case -NFS4ERR_BAD_STATEID
:
727 spin_lock(&inode
->i_lock
);
728 lo
= NFS_I(inode
)->layout
;
729 if (pnfs_layout_is_valid(lo
) &&
730 nfs4_stateid_match(&data
->args
.stateid
,
735 * Mark the bad layout state as invalid, then retry
736 * with the current stateid.
738 pnfs_mark_layout_stateid_invalid(lo
, &head
);
739 spin_unlock(&inode
->i_lock
);
740 pnfs_free_lseg_list(&head
);
741 nfs_commit_inode(inode
, 0);
743 spin_unlock(&inode
->i_lock
);
745 case -NFS4ERR_OLD_STATEID
:
746 spin_lock(&inode
->i_lock
);
747 lo
= NFS_I(inode
)->layout
;
748 if (pnfs_layout_is_valid(lo
) &&
749 nfs4_stateid_match_other(&data
->args
.stateid
,
751 /* Do we need to delay before resending? */
752 if (!nfs4_stateid_is_newer(&lo
->plh_stateid
,
753 &data
->args
.stateid
))
755 rpc_restart_call_prepare(task
);
757 spin_unlock(&inode
->i_lock
);
761 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_LAYOUTSTATS
;
766 nfs42_layoutstat_release(void *calldata
)
768 struct nfs42_layoutstat_data
*data
= calldata
;
769 struct nfs42_layoutstat_devinfo
*devinfo
= data
->args
.devinfo
;
772 for (i
= 0; i
< data
->args
.num_dev
; i
++) {
773 if (devinfo
[i
].ld_private
.ops
&& devinfo
[i
].ld_private
.ops
->free
)
774 devinfo
[i
].ld_private
.ops
->free(&devinfo
[i
].ld_private
);
777 pnfs_put_layout_hdr(NFS_I(data
->args
.inode
)->layout
);
778 smp_mb__before_atomic();
779 clear_bit(NFS_INO_LAYOUTSTATS
, &NFS_I(data
->args
.inode
)->flags
);
780 smp_mb__after_atomic();
781 nfs_iput_and_deactive(data
->inode
);
782 kfree(data
->args
.devinfo
);
786 static const struct rpc_call_ops nfs42_layoutstat_ops
= {
787 .rpc_call_prepare
= nfs42_layoutstat_prepare
,
788 .rpc_call_done
= nfs42_layoutstat_done
,
789 .rpc_release
= nfs42_layoutstat_release
,
792 int nfs42_proc_layoutstats_generic(struct nfs_server
*server
,
793 struct nfs42_layoutstat_data
*data
)
795 struct rpc_message msg
= {
796 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTSTATS
],
797 .rpc_argp
= &data
->args
,
798 .rpc_resp
= &data
->res
,
800 struct rpc_task_setup task_setup
= {
801 .rpc_client
= server
->client
,
803 .callback_ops
= &nfs42_layoutstat_ops
,
804 .callback_data
= data
,
805 .flags
= RPC_TASK_ASYNC
,
807 struct rpc_task
*task
;
809 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
811 nfs42_layoutstat_release(data
);
814 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0, 0);
815 task
= rpc_run_task(&task_setup
);
817 return PTR_ERR(task
);
822 static struct nfs42_layouterror_data
*
823 nfs42_alloc_layouterror_data(struct pnfs_layout_segment
*lseg
, gfp_t gfp_flags
)
825 struct nfs42_layouterror_data
*data
;
826 struct inode
*inode
= lseg
->pls_layout
->plh_inode
;
828 data
= kzalloc(sizeof(*data
), gfp_flags
);
830 data
->args
.inode
= data
->inode
= nfs_igrab_and_active(inode
);
832 data
->lseg
= pnfs_get_lseg(lseg
);
835 nfs_iput_and_deactive(data
->inode
);
843 nfs42_free_layouterror_data(struct nfs42_layouterror_data
*data
)
845 pnfs_put_lseg(data
->lseg
);
846 nfs_iput_and_deactive(data
->inode
);
851 nfs42_layouterror_prepare(struct rpc_task
*task
, void *calldata
)
853 struct nfs42_layouterror_data
*data
= calldata
;
854 struct inode
*inode
= data
->inode
;
855 struct nfs_server
*server
= NFS_SERVER(inode
);
856 struct pnfs_layout_hdr
*lo
= data
->lseg
->pls_layout
;
859 spin_lock(&inode
->i_lock
);
860 if (!pnfs_layout_is_valid(lo
)) {
861 spin_unlock(&inode
->i_lock
);
865 for (i
= 0; i
< data
->args
.num_errors
; i
++)
866 nfs4_stateid_copy(&data
->args
.errors
[i
].stateid
,
868 spin_unlock(&inode
->i_lock
);
869 nfs4_setup_sequence(server
->nfs_client
, &data
->args
.seq_args
,
870 &data
->res
.seq_res
, task
);
874 nfs42_layouterror_done(struct rpc_task
*task
, void *calldata
)
876 struct nfs42_layouterror_data
*data
= calldata
;
877 struct inode
*inode
= data
->inode
;
878 struct pnfs_layout_hdr
*lo
= data
->lseg
->pls_layout
;
880 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
883 switch (task
->tk_status
) {
886 case -NFS4ERR_BADHANDLE
:
888 pnfs_destroy_layout(NFS_I(inode
));
890 case -NFS4ERR_EXPIRED
:
891 case -NFS4ERR_ADMIN_REVOKED
:
892 case -NFS4ERR_DELEG_REVOKED
:
893 case -NFS4ERR_STALE_STATEID
:
894 case -NFS4ERR_BAD_STATEID
:
895 spin_lock(&inode
->i_lock
);
896 if (pnfs_layout_is_valid(lo
) &&
897 nfs4_stateid_match(&data
->args
.errors
[0].stateid
,
902 * Mark the bad layout state as invalid, then retry
903 * with the current stateid.
905 pnfs_mark_layout_stateid_invalid(lo
, &head
);
906 spin_unlock(&inode
->i_lock
);
907 pnfs_free_lseg_list(&head
);
908 nfs_commit_inode(inode
, 0);
910 spin_unlock(&inode
->i_lock
);
912 case -NFS4ERR_OLD_STATEID
:
913 spin_lock(&inode
->i_lock
);
914 if (pnfs_layout_is_valid(lo
) &&
915 nfs4_stateid_match_other(&data
->args
.errors
[0].stateid
,
917 /* Do we need to delay before resending? */
918 if (!nfs4_stateid_is_newer(&lo
->plh_stateid
,
919 &data
->args
.errors
[0].stateid
))
921 rpc_restart_call_prepare(task
);
923 spin_unlock(&inode
->i_lock
);
927 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_LAYOUTERROR
;
932 nfs42_layouterror_release(void *calldata
)
934 struct nfs42_layouterror_data
*data
= calldata
;
936 nfs42_free_layouterror_data(data
);
939 static const struct rpc_call_ops nfs42_layouterror_ops
= {
940 .rpc_call_prepare
= nfs42_layouterror_prepare
,
941 .rpc_call_done
= nfs42_layouterror_done
,
942 .rpc_release
= nfs42_layouterror_release
,
945 int nfs42_proc_layouterror(struct pnfs_layout_segment
*lseg
,
946 const struct nfs42_layout_error
*errors
, size_t n
)
948 struct inode
*inode
= lseg
->pls_layout
->plh_inode
;
949 struct nfs42_layouterror_data
*data
;
950 struct rpc_task
*task
;
951 struct rpc_message msg
= {
952 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTERROR
],
954 struct rpc_task_setup task_setup
= {
956 .callback_ops
= &nfs42_layouterror_ops
,
957 .flags
= RPC_TASK_ASYNC
,
961 if (!nfs_server_capable(inode
, NFS_CAP_LAYOUTERROR
))
963 if (n
> NFS42_LAYOUTERROR_MAX
)
965 data
= nfs42_alloc_layouterror_data(lseg
, GFP_NOFS
);
968 for (i
= 0; i
< n
; i
++) {
969 data
->args
.errors
[i
] = errors
[i
];
970 data
->args
.num_errors
++;
971 data
->res
.num_errors
++;
973 msg
.rpc_argp
= &data
->args
;
974 msg
.rpc_resp
= &data
->res
;
975 task_setup
.callback_data
= data
;
976 task_setup
.rpc_client
= NFS_SERVER(inode
)->client
;
977 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0, 0);
978 task
= rpc_run_task(&task_setup
);
980 return PTR_ERR(task
);
984 EXPORT_SYMBOL_GPL(nfs42_proc_layouterror
);
986 static int _nfs42_proc_clone(struct rpc_message
*msg
, struct file
*src_f
,
987 struct file
*dst_f
, struct nfs_lock_context
*src_lock
,
988 struct nfs_lock_context
*dst_lock
, loff_t src_offset
,
989 loff_t dst_offset
, loff_t count
)
991 struct inode
*src_inode
= file_inode(src_f
);
992 struct inode
*dst_inode
= file_inode(dst_f
);
993 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
994 struct nfs42_clone_args args
= {
995 .src_fh
= NFS_FH(src_inode
),
996 .dst_fh
= NFS_FH(dst_inode
),
997 .src_offset
= src_offset
,
998 .dst_offset
= dst_offset
,
1000 .dst_bitmask
= server
->cache_consistency_bitmask
,
1002 struct nfs42_clone_res res
= {
1007 msg
->rpc_argp
= &args
;
1008 msg
->rpc_resp
= &res
;
1010 status
= nfs4_set_rw_stateid(&args
.src_stateid
, src_lock
->open_context
,
1011 src_lock
, FMODE_READ
);
1013 if (status
== -EAGAIN
)
1014 status
= -NFS4ERR_BAD_STATEID
;
1017 status
= nfs4_set_rw_stateid(&args
.dst_stateid
, dst_lock
->open_context
,
1018 dst_lock
, FMODE_WRITE
);
1020 if (status
== -EAGAIN
)
1021 status
= -NFS4ERR_BAD_STATEID
;
1025 res
.dst_fattr
= nfs_alloc_fattr();
1029 status
= nfs4_call_sync(server
->client
, server
, msg
,
1030 &args
.seq_args
, &res
.seq_res
, 0);
1032 status
= nfs_post_op_update_inode(dst_inode
, res
.dst_fattr
);
1034 kfree(res
.dst_fattr
);
1038 int nfs42_proc_clone(struct file
*src_f
, struct file
*dst_f
,
1039 loff_t src_offset
, loff_t dst_offset
, loff_t count
)
1041 struct rpc_message msg
= {
1042 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLONE
],
1044 struct inode
*inode
= file_inode(src_f
);
1045 struct nfs_server
*server
= NFS_SERVER(file_inode(src_f
));
1046 struct nfs_lock_context
*src_lock
;
1047 struct nfs_lock_context
*dst_lock
;
1048 struct nfs4_exception src_exception
= { };
1049 struct nfs4_exception dst_exception
= { };
1052 if (!nfs_server_capable(inode
, NFS_CAP_CLONE
))
1055 src_lock
= nfs_get_lock_context(nfs_file_open_context(src_f
));
1056 if (IS_ERR(src_lock
))
1057 return PTR_ERR(src_lock
);
1059 src_exception
.inode
= file_inode(src_f
);
1060 src_exception
.state
= src_lock
->open_context
->state
;
1062 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst_f
));
1063 if (IS_ERR(dst_lock
)) {
1064 err
= PTR_ERR(dst_lock
);
1065 goto out_put_src_lock
;
1068 dst_exception
.inode
= file_inode(dst_f
);
1069 dst_exception
.state
= dst_lock
->open_context
->state
;
1072 err
= _nfs42_proc_clone(&msg
, src_f
, dst_f
, src_lock
, dst_lock
,
1073 src_offset
, dst_offset
, count
);
1074 if (err
== -ENOTSUPP
|| err
== -EOPNOTSUPP
) {
1075 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_CLONE
;
1080 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
1081 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
1084 } while (src_exception
.retry
|| dst_exception
.retry
);
1086 nfs_put_lock_context(dst_lock
);
1088 nfs_put_lock_context(src_lock
);