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
,
85 NFS_INO_REVAL_FORCED
| NFS_INO_INVALID_MODE
);
86 spin_unlock(&inode
->i_lock
);
88 status
= nfs_post_op_update_inode_force_wcc(inode
,
91 if (msg
->rpc_proc
== &nfs4_procedures
[NFSPROC4_CLNT_ALLOCATE
])
92 trace_nfs4_fallocate(inode
, &args
, status
);
94 trace_nfs4_deallocate(inode
, &args
, status
);
95 kfree(res
.falloc_fattr
);
99 static int nfs42_proc_fallocate(struct rpc_message
*msg
, struct file
*filep
,
100 loff_t offset
, loff_t len
)
102 struct inode
*inode
= file_inode(filep
);
103 struct nfs_server
*server
= NFS_SERVER(inode
);
104 struct nfs4_exception exception
= { };
105 struct nfs_lock_context
*lock
;
108 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
110 return PTR_ERR(lock
);
112 exception
.inode
= inode
;
113 exception
.state
= lock
->open_context
->state
;
115 err
= nfs_sync_inode(inode
);
120 err
= _nfs42_proc_fallocate(msg
, filep
, lock
, offset
, len
);
121 if (err
== -ENOTSUPP
) {
125 err
= nfs4_handle_exception(server
, err
, &exception
);
126 } while (exception
.retry
);
128 nfs_put_lock_context(lock
);
132 int nfs42_proc_allocate(struct file
*filep
, loff_t offset
, loff_t len
)
134 struct rpc_message msg
= {
135 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ALLOCATE
],
137 struct inode
*inode
= file_inode(filep
);
140 if (!nfs_server_capable(inode
, NFS_CAP_ALLOCATE
))
145 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
146 if (err
== -EOPNOTSUPP
)
147 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_ALLOCATE
;
153 int nfs42_proc_deallocate(struct file
*filep
, loff_t offset
, loff_t len
)
155 struct rpc_message msg
= {
156 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DEALLOCATE
],
158 struct inode
*inode
= file_inode(filep
);
161 if (!nfs_server_capable(inode
, NFS_CAP_DEALLOCATE
))
166 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
168 truncate_pagecache_range(inode
, offset
, (offset
+ len
) -1);
169 if (err
== -EOPNOTSUPP
)
170 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_DEALLOCATE
;
176 static int handle_async_copy(struct nfs42_copy_res
*res
,
177 struct nfs_server
*dst_server
,
178 struct nfs_server
*src_server
,
181 nfs4_stateid
*src_stateid
,
184 struct nfs4_copy_state
*copy
, *tmp_copy
= NULL
, *iter
;
185 int status
= NFS4_OK
;
186 struct nfs_open_context
*dst_ctx
= nfs_file_open_context(dst
);
187 struct nfs_open_context
*src_ctx
= nfs_file_open_context(src
);
189 copy
= kzalloc(sizeof(struct nfs4_copy_state
), GFP_KERNEL
);
193 spin_lock(&dst_server
->nfs_client
->cl_lock
);
194 list_for_each_entry(iter
,
195 &dst_server
->nfs_client
->pending_cb_stateids
,
197 if (memcmp(&res
->write_res
.stateid
, &iter
->stateid
,
201 list_del(&iter
->copies
);
205 spin_unlock(&dst_server
->nfs_client
->cl_lock
);
211 memcpy(©
->stateid
, &res
->write_res
.stateid
, NFS4_STATEID_SIZE
);
212 init_completion(©
->completion
);
213 copy
->parent_dst_state
= dst_ctx
->state
;
214 copy
->parent_src_state
= src_ctx
->state
;
216 list_add_tail(©
->copies
, &dst_server
->ss_copies
);
217 spin_unlock(&dst_server
->nfs_client
->cl_lock
);
219 if (dst_server
!= src_server
) {
220 spin_lock(&src_server
->nfs_client
->cl_lock
);
221 list_add_tail(©
->src_copies
, &src_server
->ss_src_copies
);
222 spin_unlock(&src_server
->nfs_client
->cl_lock
);
225 status
= wait_for_completion_interruptible(©
->completion
);
226 spin_lock(&dst_server
->nfs_client
->cl_lock
);
227 list_del_init(©
->copies
);
228 spin_unlock(&dst_server
->nfs_client
->cl_lock
);
229 if (dst_server
!= src_server
) {
230 spin_lock(&src_server
->nfs_client
->cl_lock
);
231 list_del_init(©
->src_copies
);
232 spin_unlock(&src_server
->nfs_client
->cl_lock
);
234 if (status
== -ERESTARTSYS
) {
236 } else if (copy
->flags
|| copy
->error
== NFS4ERR_PARTNER_NO_AUTH
) {
242 res
->write_res
.count
= copy
->count
;
243 memcpy(&res
->write_res
.verifier
, ©
->verf
, sizeof(copy
->verf
));
244 status
= -copy
->error
;
250 nfs42_do_offload_cancel_async(dst
, ©
->stateid
);
251 if (!nfs42_files_from_same_server(src
, dst
))
252 nfs42_do_offload_cancel_async(src
, src_stateid
);
256 static int process_copy_commit(struct file
*dst
, loff_t pos_dst
,
257 struct nfs42_copy_res
*res
)
259 struct nfs_commitres cres
;
260 int status
= -ENOMEM
;
262 cres
.verf
= kzalloc(sizeof(struct nfs_writeverf
), GFP_KERNEL
);
266 status
= nfs4_proc_commit(dst
, pos_dst
, res
->write_res
.count
, &cres
);
269 if (nfs_write_verifier_cmp(&res
->write_res
.verifier
.verifier
,
270 &cres
.verf
->verifier
)) {
271 dprintk("commit verf differs from copy verf\n");
281 * nfs42_copy_dest_done - perform inode cache updates after clone/copy offload
282 * @inode: pointer to destination inode
283 * @pos: destination offset
286 * Punch a hole in the inode page cache, so that the NFS client will
287 * know to retrieve new data.
288 * Update the file size if necessary, and then mark the inode as having
289 * invalid cached values for change attribute, ctime, mtime and space used.
291 static void nfs42_copy_dest_done(struct inode
*inode
, loff_t pos
, loff_t len
)
293 loff_t newsize
= pos
+ len
;
294 loff_t end
= newsize
- 1;
296 WARN_ON_ONCE(invalidate_inode_pages2_range(inode
->i_mapping
,
297 pos
>> PAGE_SHIFT
, end
>> PAGE_SHIFT
));
299 spin_lock(&inode
->i_lock
);
300 if (newsize
> i_size_read(inode
))
301 i_size_write(inode
, newsize
);
302 nfs_set_cache_invalid(inode
, NFS_INO_INVALID_CHANGE
|
303 NFS_INO_INVALID_CTIME
|
304 NFS_INO_INVALID_MTIME
|
305 NFS_INO_INVALID_BLOCKS
);
306 spin_unlock(&inode
->i_lock
);
309 static ssize_t
_nfs42_proc_copy(struct file
*src
,
310 struct nfs_lock_context
*src_lock
,
312 struct nfs_lock_context
*dst_lock
,
313 struct nfs42_copy_args
*args
,
314 struct nfs42_copy_res
*res
,
315 struct nl4_server
*nss
,
316 nfs4_stateid
*cnr_stateid
,
319 struct rpc_message msg
= {
320 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COPY
],
324 struct inode
*dst_inode
= file_inode(dst
);
325 struct inode
*src_inode
= file_inode(src
);
326 struct nfs_server
*dst_server
= NFS_SERVER(dst_inode
);
327 struct nfs_server
*src_server
= NFS_SERVER(src_inode
);
328 loff_t pos_src
= args
->src_pos
;
329 loff_t pos_dst
= args
->dst_pos
;
330 size_t count
= args
->count
;
335 nfs4_stateid_copy(&args
->src_stateid
, cnr_stateid
);
337 status
= nfs4_set_rw_stateid(&args
->src_stateid
,
338 src_lock
->open_context
, src_lock
, FMODE_READ
);
340 if (status
== -EAGAIN
)
341 status
= -NFS4ERR_BAD_STATEID
;
345 status
= nfs_filemap_write_and_wait_range(src
->f_mapping
,
346 pos_src
, pos_src
+ (loff_t
)count
- 1);
350 status
= nfs4_set_rw_stateid(&args
->dst_stateid
, dst_lock
->open_context
,
351 dst_lock
, FMODE_WRITE
);
353 if (status
== -EAGAIN
)
354 status
= -NFS4ERR_BAD_STATEID
;
358 status
= nfs_sync_inode(dst_inode
);
362 res
->commit_res
.verf
= NULL
;
364 res
->commit_res
.verf
=
365 kzalloc(sizeof(struct nfs_writeverf
), GFP_KERNEL
);
366 if (!res
->commit_res
.verf
)
369 set_bit(NFS_CLNT_SRC_SSC_COPY_STATE
,
370 &src_lock
->open_context
->state
->flags
);
371 set_bit(NFS_CLNT_DST_SSC_COPY_STATE
,
372 &dst_lock
->open_context
->state
->flags
);
374 status
= nfs4_call_sync(dst_server
->client
, dst_server
, &msg
,
375 &args
->seq_args
, &res
->seq_res
, 0);
376 trace_nfs4_copy(src_inode
, dst_inode
, args
, res
, nss
, status
);
377 if (status
== -ENOTSUPP
)
378 dst_server
->caps
&= ~NFS_CAP_COPY
;
383 nfs_write_verifier_cmp(&res
->write_res
.verifier
.verifier
,
384 &res
->commit_res
.verf
->verifier
)) {
389 if (!res
->synchronous
) {
390 status
= handle_async_copy(res
, dst_server
, src_server
, src
,
391 dst
, &args
->src_stateid
, restart
);
396 if ((!res
->synchronous
|| !args
->sync
) &&
397 res
->write_res
.verifier
.committed
!= NFS_FILE_SYNC
) {
398 status
= process_copy_commit(dst
, pos_dst
, res
);
403 nfs42_copy_dest_done(dst_inode
, pos_dst
, res
->write_res
.count
);
404 nfs_invalidate_atime(src_inode
);
405 status
= res
->write_res
.count
;
408 kfree(res
->commit_res
.verf
);
412 ssize_t
nfs42_proc_copy(struct file
*src
, loff_t pos_src
,
413 struct file
*dst
, loff_t pos_dst
, size_t count
,
414 struct nl4_server
*nss
,
415 nfs4_stateid
*cnr_stateid
, bool sync
)
417 struct nfs_server
*server
= NFS_SERVER(file_inode(dst
));
418 struct nfs_lock_context
*src_lock
;
419 struct nfs_lock_context
*dst_lock
;
420 struct nfs42_copy_args args
= {
421 .src_fh
= NFS_FH(file_inode(src
)),
423 .dst_fh
= NFS_FH(file_inode(dst
)),
428 struct nfs42_copy_res res
;
429 struct nfs4_exception src_exception
= {
430 .inode
= file_inode(src
),
431 .stateid
= &args
.src_stateid
,
433 struct nfs4_exception dst_exception
= {
434 .inode
= file_inode(dst
),
435 .stateid
= &args
.dst_stateid
,
438 bool restart
= false;
440 src_lock
= nfs_get_lock_context(nfs_file_open_context(src
));
441 if (IS_ERR(src_lock
))
442 return PTR_ERR(src_lock
);
444 src_exception
.state
= src_lock
->open_context
->state
;
446 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst
));
447 if (IS_ERR(dst_lock
)) {
448 err
= PTR_ERR(dst_lock
);
449 goto out_put_src_lock
;
452 dst_exception
.state
= dst_lock
->open_context
->state
;
455 inode_lock(file_inode(dst
));
456 err
= _nfs42_proc_copy(src
, src_lock
,
459 nss
, cnr_stateid
, &restart
);
460 inode_unlock(file_inode(dst
));
464 if ((err
== -ENOTSUPP
||
465 err
== -NFS4ERR_OFFLOAD_DENIED
) &&
466 nfs42_files_from_same_server(src
, dst
)) {
469 } else if (err
== -EAGAIN
) {
471 dst_exception
.retry
= 1;
475 } else if (err
== -NFS4ERR_OFFLOAD_NO_REQS
&&
476 args
.sync
!= res
.synchronous
) {
477 args
.sync
= res
.synchronous
;
478 dst_exception
.retry
= 1;
480 } else if ((err
== -ESTALE
||
481 err
== -NFS4ERR_OFFLOAD_DENIED
||
483 !nfs42_files_from_same_server(src
, dst
)) {
484 nfs42_do_offload_cancel_async(src
, &args
.src_stateid
);
489 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
490 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
493 } while (src_exception
.retry
|| dst_exception
.retry
);
495 nfs_put_lock_context(dst_lock
);
497 nfs_put_lock_context(src_lock
);
501 struct nfs42_offloadcancel_data
{
502 struct nfs_server
*seq_server
;
503 struct nfs42_offload_status_args args
;
504 struct nfs42_offload_status_res res
;
507 static void nfs42_offload_cancel_prepare(struct rpc_task
*task
, void *calldata
)
509 struct nfs42_offloadcancel_data
*data
= calldata
;
511 nfs4_setup_sequence(data
->seq_server
->nfs_client
,
512 &data
->args
.osa_seq_args
,
513 &data
->res
.osr_seq_res
, task
);
516 static void nfs42_offload_cancel_done(struct rpc_task
*task
, void *calldata
)
518 struct nfs42_offloadcancel_data
*data
= calldata
;
520 trace_nfs4_offload_cancel(&data
->args
, task
->tk_status
);
521 nfs41_sequence_done(task
, &data
->res
.osr_seq_res
);
522 if (task
->tk_status
&&
523 nfs4_async_handle_error(task
, data
->seq_server
, NULL
,
525 rpc_restart_call_prepare(task
);
528 static void nfs42_free_offloadcancel_data(void *data
)
533 static const struct rpc_call_ops nfs42_offload_cancel_ops
= {
534 .rpc_call_prepare
= nfs42_offload_cancel_prepare
,
535 .rpc_call_done
= nfs42_offload_cancel_done
,
536 .rpc_release
= nfs42_free_offloadcancel_data
,
539 static int nfs42_do_offload_cancel_async(struct file
*dst
,
540 nfs4_stateid
*stateid
)
542 struct nfs_server
*dst_server
= NFS_SERVER(file_inode(dst
));
543 struct nfs42_offloadcancel_data
*data
= NULL
;
544 struct nfs_open_context
*ctx
= nfs_file_open_context(dst
);
545 struct rpc_task
*task
;
546 struct rpc_message msg
= {
547 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OFFLOAD_CANCEL
],
548 .rpc_cred
= ctx
->cred
,
550 struct rpc_task_setup task_setup_data
= {
551 .rpc_client
= dst_server
->client
,
553 .callback_ops
= &nfs42_offload_cancel_ops
,
554 .workqueue
= nfsiod_workqueue
,
555 .flags
= RPC_TASK_ASYNC
,
559 if (!(dst_server
->caps
& NFS_CAP_OFFLOAD_CANCEL
))
562 data
= kzalloc(sizeof(struct nfs42_offloadcancel_data
), GFP_KERNEL
);
566 data
->seq_server
= dst_server
;
567 data
->args
.osa_src_fh
= NFS_FH(file_inode(dst
));
568 memcpy(&data
->args
.osa_stateid
, stateid
,
569 sizeof(data
->args
.osa_stateid
));
570 msg
.rpc_argp
= &data
->args
;
571 msg
.rpc_resp
= &data
->res
;
572 task_setup_data
.callback_data
= data
;
573 nfs4_init_sequence(&data
->args
.osa_seq_args
, &data
->res
.osr_seq_res
,
575 task
= rpc_run_task(&task_setup_data
);
577 return PTR_ERR(task
);
578 status
= rpc_wait_for_completion_task(task
);
579 if (status
== -ENOTSUPP
)
580 dst_server
->caps
&= ~NFS_CAP_OFFLOAD_CANCEL
;
585 static int _nfs42_proc_copy_notify(struct file
*src
, struct file
*dst
,
586 struct nfs42_copy_notify_args
*args
,
587 struct nfs42_copy_notify_res
*res
)
589 struct nfs_server
*src_server
= NFS_SERVER(file_inode(src
));
590 struct rpc_message msg
= {
591 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COPY_NOTIFY
],
596 struct nfs_open_context
*ctx
;
597 struct nfs_lock_context
*l_ctx
;
599 ctx
= get_nfs_open_context(nfs_file_open_context(src
));
600 l_ctx
= nfs_get_lock_context(ctx
);
602 status
= PTR_ERR(l_ctx
);
606 status
= nfs4_set_rw_stateid(&args
->cna_src_stateid
, ctx
, l_ctx
,
608 nfs_put_lock_context(l_ctx
);
610 if (status
== -EAGAIN
)
611 status
= -NFS4ERR_BAD_STATEID
;
615 status
= nfs4_call_sync(src_server
->client
, src_server
, &msg
,
616 &args
->cna_seq_args
, &res
->cnr_seq_res
, 0);
617 trace_nfs4_copy_notify(file_inode(src
), args
, res
, status
);
618 if (status
== -ENOTSUPP
)
619 src_server
->caps
&= ~NFS_CAP_COPY_NOTIFY
;
622 put_nfs_open_context(nfs_file_open_context(src
));
626 int nfs42_proc_copy_notify(struct file
*src
, struct file
*dst
,
627 struct nfs42_copy_notify_res
*res
)
629 struct nfs_server
*src_server
= NFS_SERVER(file_inode(src
));
630 struct nfs42_copy_notify_args
*args
;
631 struct nfs4_exception exception
= {
632 .inode
= file_inode(src
),
636 if (!(src_server
->caps
& NFS_CAP_COPY_NOTIFY
))
639 args
= kzalloc(sizeof(struct nfs42_copy_notify_args
), GFP_KERNEL
);
643 args
->cna_src_fh
= NFS_FH(file_inode(src
)),
644 args
->cna_dst
.nl4_type
= NL4_NETADDR
;
645 nfs42_set_netaddr(dst
, &args
->cna_dst
.u
.nl4_addr
);
646 exception
.stateid
= &args
->cna_src_stateid
;
649 status
= _nfs42_proc_copy_notify(src
, dst
, args
, res
);
650 if (status
== -ENOTSUPP
) {
651 status
= -EOPNOTSUPP
;
654 status
= nfs4_handle_exception(src_server
, status
, &exception
);
655 } while (exception
.retry
);
662 static loff_t
_nfs42_proc_llseek(struct file
*filep
,
663 struct nfs_lock_context
*lock
, loff_t offset
, int whence
)
665 struct inode
*inode
= file_inode(filep
);
666 struct nfs42_seek_args args
= {
667 .sa_fh
= NFS_FH(inode
),
669 .sa_what
= (whence
== SEEK_HOLE
) ?
670 NFS4_CONTENT_HOLE
: NFS4_CONTENT_DATA
,
672 struct nfs42_seek_res res
;
673 struct rpc_message msg
= {
674 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEEK
],
678 struct nfs_server
*server
= NFS_SERVER(inode
);
681 if (!nfs_server_capable(inode
, NFS_CAP_SEEK
))
684 status
= nfs4_set_rw_stateid(&args
.sa_stateid
, lock
->open_context
,
687 if (status
== -EAGAIN
)
688 status
= -NFS4ERR_BAD_STATEID
;
692 status
= nfs_filemap_write_and_wait_range(inode
->i_mapping
,
697 status
= nfs4_call_sync(server
->client
, server
, &msg
,
698 &args
.seq_args
, &res
.seq_res
, 0);
699 trace_nfs4_llseek(inode
, &args
, &res
, status
);
700 if (status
== -ENOTSUPP
)
701 server
->caps
&= ~NFS_CAP_SEEK
;
705 if (whence
== SEEK_DATA
&& res
.sr_eof
)
706 return -NFS4ERR_NXIO
;
708 return vfs_setpos(filep
, res
.sr_offset
, inode
->i_sb
->s_maxbytes
);
711 loff_t
nfs42_proc_llseek(struct file
*filep
, loff_t offset
, int whence
)
713 struct nfs_server
*server
= NFS_SERVER(file_inode(filep
));
714 struct nfs4_exception exception
= { };
715 struct nfs_lock_context
*lock
;
718 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
720 return PTR_ERR(lock
);
722 exception
.inode
= file_inode(filep
);
723 exception
.state
= lock
->open_context
->state
;
726 err
= _nfs42_proc_llseek(filep
, lock
, offset
, whence
);
729 if (err
== -ENOTSUPP
) {
733 err
= nfs4_handle_exception(server
, err
, &exception
);
734 } while (exception
.retry
);
736 nfs_put_lock_context(lock
);
742 nfs42_layoutstat_prepare(struct rpc_task
*task
, void *calldata
)
744 struct nfs42_layoutstat_data
*data
= calldata
;
745 struct inode
*inode
= data
->inode
;
746 struct nfs_server
*server
= NFS_SERVER(inode
);
747 struct pnfs_layout_hdr
*lo
;
749 spin_lock(&inode
->i_lock
);
750 lo
= NFS_I(inode
)->layout
;
751 if (!pnfs_layout_is_valid(lo
)) {
752 spin_unlock(&inode
->i_lock
);
756 nfs4_stateid_copy(&data
->args
.stateid
, &lo
->plh_stateid
);
757 spin_unlock(&inode
->i_lock
);
758 nfs4_setup_sequence(server
->nfs_client
, &data
->args
.seq_args
,
759 &data
->res
.seq_res
, task
);
763 nfs42_layoutstat_done(struct rpc_task
*task
, void *calldata
)
765 struct nfs42_layoutstat_data
*data
= calldata
;
766 struct inode
*inode
= data
->inode
;
767 struct pnfs_layout_hdr
*lo
;
769 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
772 switch (task
->tk_status
) {
775 case -NFS4ERR_BADHANDLE
:
777 pnfs_destroy_layout(NFS_I(inode
));
779 case -NFS4ERR_EXPIRED
:
780 case -NFS4ERR_ADMIN_REVOKED
:
781 case -NFS4ERR_DELEG_REVOKED
:
782 case -NFS4ERR_STALE_STATEID
:
783 case -NFS4ERR_BAD_STATEID
:
784 spin_lock(&inode
->i_lock
);
785 lo
= NFS_I(inode
)->layout
;
786 if (pnfs_layout_is_valid(lo
) &&
787 nfs4_stateid_match(&data
->args
.stateid
,
792 * Mark the bad layout state as invalid, then retry
793 * with the current stateid.
795 pnfs_mark_layout_stateid_invalid(lo
, &head
);
796 spin_unlock(&inode
->i_lock
);
797 pnfs_free_lseg_list(&head
);
798 nfs_commit_inode(inode
, 0);
800 spin_unlock(&inode
->i_lock
);
802 case -NFS4ERR_OLD_STATEID
:
803 spin_lock(&inode
->i_lock
);
804 lo
= NFS_I(inode
)->layout
;
805 if (pnfs_layout_is_valid(lo
) &&
806 nfs4_stateid_match_other(&data
->args
.stateid
,
808 /* Do we need to delay before resending? */
809 if (!nfs4_stateid_is_newer(&lo
->plh_stateid
,
810 &data
->args
.stateid
))
812 rpc_restart_call_prepare(task
);
814 spin_unlock(&inode
->i_lock
);
818 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_LAYOUTSTATS
;
821 trace_nfs4_layoutstats(inode
, &data
->args
.stateid
, task
->tk_status
);
825 nfs42_layoutstat_release(void *calldata
)
827 struct nfs42_layoutstat_data
*data
= calldata
;
828 struct nfs42_layoutstat_devinfo
*devinfo
= data
->args
.devinfo
;
831 for (i
= 0; i
< data
->args
.num_dev
; i
++) {
832 if (devinfo
[i
].ld_private
.ops
&& devinfo
[i
].ld_private
.ops
->free
)
833 devinfo
[i
].ld_private
.ops
->free(&devinfo
[i
].ld_private
);
836 pnfs_put_layout_hdr(NFS_I(data
->args
.inode
)->layout
);
837 smp_mb__before_atomic();
838 clear_bit(NFS_INO_LAYOUTSTATS
, &NFS_I(data
->args
.inode
)->flags
);
839 smp_mb__after_atomic();
840 nfs_iput_and_deactive(data
->inode
);
841 kfree(data
->args
.devinfo
);
845 static const struct rpc_call_ops nfs42_layoutstat_ops
= {
846 .rpc_call_prepare
= nfs42_layoutstat_prepare
,
847 .rpc_call_done
= nfs42_layoutstat_done
,
848 .rpc_release
= nfs42_layoutstat_release
,
851 int nfs42_proc_layoutstats_generic(struct nfs_server
*server
,
852 struct nfs42_layoutstat_data
*data
)
854 struct rpc_message msg
= {
855 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTSTATS
],
856 .rpc_argp
= &data
->args
,
857 .rpc_resp
= &data
->res
,
859 struct rpc_task_setup task_setup
= {
860 .rpc_client
= server
->client
,
862 .callback_ops
= &nfs42_layoutstat_ops
,
863 .callback_data
= data
,
864 .flags
= RPC_TASK_ASYNC
,
866 struct rpc_task
*task
;
868 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
870 nfs42_layoutstat_release(data
);
873 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0, 0);
874 task
= rpc_run_task(&task_setup
);
876 return PTR_ERR(task
);
881 static struct nfs42_layouterror_data
*
882 nfs42_alloc_layouterror_data(struct pnfs_layout_segment
*lseg
, gfp_t gfp_flags
)
884 struct nfs42_layouterror_data
*data
;
885 struct inode
*inode
= lseg
->pls_layout
->plh_inode
;
887 data
= kzalloc(sizeof(*data
), gfp_flags
);
889 data
->args
.inode
= data
->inode
= nfs_igrab_and_active(inode
);
891 data
->lseg
= pnfs_get_lseg(lseg
);
894 nfs_iput_and_deactive(data
->inode
);
902 nfs42_free_layouterror_data(struct nfs42_layouterror_data
*data
)
904 pnfs_put_lseg(data
->lseg
);
905 nfs_iput_and_deactive(data
->inode
);
910 nfs42_layouterror_prepare(struct rpc_task
*task
, void *calldata
)
912 struct nfs42_layouterror_data
*data
= calldata
;
913 struct inode
*inode
= data
->inode
;
914 struct nfs_server
*server
= NFS_SERVER(inode
);
915 struct pnfs_layout_hdr
*lo
= data
->lseg
->pls_layout
;
918 spin_lock(&inode
->i_lock
);
919 if (!pnfs_layout_is_valid(lo
)) {
920 spin_unlock(&inode
->i_lock
);
924 for (i
= 0; i
< data
->args
.num_errors
; i
++)
925 nfs4_stateid_copy(&data
->args
.errors
[i
].stateid
,
927 spin_unlock(&inode
->i_lock
);
928 nfs4_setup_sequence(server
->nfs_client
, &data
->args
.seq_args
,
929 &data
->res
.seq_res
, task
);
933 nfs42_layouterror_done(struct rpc_task
*task
, void *calldata
)
935 struct nfs42_layouterror_data
*data
= calldata
;
936 struct inode
*inode
= data
->inode
;
937 struct pnfs_layout_hdr
*lo
= data
->lseg
->pls_layout
;
939 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
942 switch (task
->tk_status
) {
945 case -NFS4ERR_BADHANDLE
:
947 pnfs_destroy_layout(NFS_I(inode
));
949 case -NFS4ERR_EXPIRED
:
950 case -NFS4ERR_ADMIN_REVOKED
:
951 case -NFS4ERR_DELEG_REVOKED
:
952 case -NFS4ERR_STALE_STATEID
:
953 case -NFS4ERR_BAD_STATEID
:
954 spin_lock(&inode
->i_lock
);
955 if (pnfs_layout_is_valid(lo
) &&
956 nfs4_stateid_match(&data
->args
.errors
[0].stateid
,
961 * Mark the bad layout state as invalid, then retry
962 * with the current stateid.
964 pnfs_mark_layout_stateid_invalid(lo
, &head
);
965 spin_unlock(&inode
->i_lock
);
966 pnfs_free_lseg_list(&head
);
967 nfs_commit_inode(inode
, 0);
969 spin_unlock(&inode
->i_lock
);
971 case -NFS4ERR_OLD_STATEID
:
972 spin_lock(&inode
->i_lock
);
973 if (pnfs_layout_is_valid(lo
) &&
974 nfs4_stateid_match_other(&data
->args
.errors
[0].stateid
,
976 /* Do we need to delay before resending? */
977 if (!nfs4_stateid_is_newer(&lo
->plh_stateid
,
978 &data
->args
.errors
[0].stateid
))
980 rpc_restart_call_prepare(task
);
982 spin_unlock(&inode
->i_lock
);
986 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_LAYOUTERROR
;
989 trace_nfs4_layouterror(inode
, &data
->args
.errors
[0].stateid
,
994 nfs42_layouterror_release(void *calldata
)
996 struct nfs42_layouterror_data
*data
= calldata
;
998 nfs42_free_layouterror_data(data
);
1001 static const struct rpc_call_ops nfs42_layouterror_ops
= {
1002 .rpc_call_prepare
= nfs42_layouterror_prepare
,
1003 .rpc_call_done
= nfs42_layouterror_done
,
1004 .rpc_release
= nfs42_layouterror_release
,
1007 int nfs42_proc_layouterror(struct pnfs_layout_segment
*lseg
,
1008 const struct nfs42_layout_error
*errors
, size_t n
)
1010 struct inode
*inode
= lseg
->pls_layout
->plh_inode
;
1011 struct nfs42_layouterror_data
*data
;
1012 struct rpc_task
*task
;
1013 struct rpc_message msg
= {
1014 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTERROR
],
1016 struct rpc_task_setup task_setup
= {
1017 .rpc_message
= &msg
,
1018 .callback_ops
= &nfs42_layouterror_ops
,
1019 .flags
= RPC_TASK_ASYNC
,
1023 if (!nfs_server_capable(inode
, NFS_CAP_LAYOUTERROR
))
1025 if (n
> NFS42_LAYOUTERROR_MAX
)
1027 data
= nfs42_alloc_layouterror_data(lseg
, nfs_io_gfp_mask());
1030 for (i
= 0; i
< n
; i
++) {
1031 data
->args
.errors
[i
] = errors
[i
];
1032 data
->args
.num_errors
++;
1033 data
->res
.num_errors
++;
1035 msg
.rpc_argp
= &data
->args
;
1036 msg
.rpc_resp
= &data
->res
;
1037 task_setup
.callback_data
= data
;
1038 task_setup
.rpc_client
= NFS_SERVER(inode
)->client
;
1039 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0, 0);
1040 task
= rpc_run_task(&task_setup
);
1042 return PTR_ERR(task
);
1046 EXPORT_SYMBOL_GPL(nfs42_proc_layouterror
);
1048 static int _nfs42_proc_clone(struct rpc_message
*msg
, struct file
*src_f
,
1049 struct file
*dst_f
, struct nfs_lock_context
*src_lock
,
1050 struct nfs_lock_context
*dst_lock
, loff_t src_offset
,
1051 loff_t dst_offset
, loff_t count
)
1053 struct inode
*src_inode
= file_inode(src_f
);
1054 struct inode
*dst_inode
= file_inode(dst_f
);
1055 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
1056 __u32 dst_bitmask
[NFS_BITMASK_SZ
];
1057 struct nfs42_clone_args args
= {
1058 .src_fh
= NFS_FH(src_inode
),
1059 .dst_fh
= NFS_FH(dst_inode
),
1060 .src_offset
= src_offset
,
1061 .dst_offset
= dst_offset
,
1063 .dst_bitmask
= dst_bitmask
,
1065 struct nfs42_clone_res res
= {
1070 msg
->rpc_argp
= &args
;
1071 msg
->rpc_resp
= &res
;
1073 status
= nfs4_set_rw_stateid(&args
.src_stateid
, src_lock
->open_context
,
1074 src_lock
, FMODE_READ
);
1076 if (status
== -EAGAIN
)
1077 status
= -NFS4ERR_BAD_STATEID
;
1080 status
= nfs4_set_rw_stateid(&args
.dst_stateid
, dst_lock
->open_context
,
1081 dst_lock
, FMODE_WRITE
);
1083 if (status
== -EAGAIN
)
1084 status
= -NFS4ERR_BAD_STATEID
;
1088 res
.dst_fattr
= nfs_alloc_fattr();
1092 nfs4_bitmask_set(dst_bitmask
, server
->cache_consistency_bitmask
,
1093 dst_inode
, NFS_INO_INVALID_BLOCKS
);
1095 status
= nfs4_call_sync(server
->client
, server
, msg
,
1096 &args
.seq_args
, &res
.seq_res
, 0);
1097 trace_nfs4_clone(src_inode
, dst_inode
, &args
, status
);
1099 /* a zero-length count means clone to EOF in src */
1100 if (count
== 0 && res
.dst_fattr
->valid
& NFS_ATTR_FATTR_SIZE
)
1101 count
= nfs_size_to_loff_t(res
.dst_fattr
->size
) - dst_offset
;
1102 nfs42_copy_dest_done(dst_inode
, dst_offset
, count
);
1103 status
= nfs_post_op_update_inode(dst_inode
, res
.dst_fattr
);
1106 kfree(res
.dst_fattr
);
1110 int nfs42_proc_clone(struct file
*src_f
, struct file
*dst_f
,
1111 loff_t src_offset
, loff_t dst_offset
, loff_t count
)
1113 struct rpc_message msg
= {
1114 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLONE
],
1116 struct inode
*inode
= file_inode(src_f
);
1117 struct nfs_server
*server
= NFS_SERVER(file_inode(src_f
));
1118 struct nfs_lock_context
*src_lock
;
1119 struct nfs_lock_context
*dst_lock
;
1120 struct nfs4_exception src_exception
= { };
1121 struct nfs4_exception dst_exception
= { };
1124 if (!nfs_server_capable(inode
, NFS_CAP_CLONE
))
1127 src_lock
= nfs_get_lock_context(nfs_file_open_context(src_f
));
1128 if (IS_ERR(src_lock
))
1129 return PTR_ERR(src_lock
);
1131 src_exception
.inode
= file_inode(src_f
);
1132 src_exception
.state
= src_lock
->open_context
->state
;
1134 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst_f
));
1135 if (IS_ERR(dst_lock
)) {
1136 err
= PTR_ERR(dst_lock
);
1137 goto out_put_src_lock
;
1140 dst_exception
.inode
= file_inode(dst_f
);
1141 dst_exception
.state
= dst_lock
->open_context
->state
;
1144 err
= _nfs42_proc_clone(&msg
, src_f
, dst_f
, src_lock
, dst_lock
,
1145 src_offset
, dst_offset
, count
);
1146 if (err
== -ENOTSUPP
|| err
== -EOPNOTSUPP
) {
1147 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_CLONE
;
1152 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
1153 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
1156 } while (src_exception
.retry
|| dst_exception
.retry
);
1158 nfs_put_lock_context(dst_lock
);
1160 nfs_put_lock_context(src_lock
);
1164 #define NFS4XATTR_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
1166 static int _nfs42_proc_removexattr(struct inode
*inode
, const char *name
)
1168 struct nfs_server
*server
= NFS_SERVER(inode
);
1169 struct nfs42_removexattrargs args
= {
1170 .fh
= NFS_FH(inode
),
1173 struct nfs42_removexattrres res
;
1174 struct rpc_message msg
= {
1175 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVEXATTR
],
1180 unsigned long timestamp
= jiffies
;
1182 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
,
1184 trace_nfs4_removexattr(inode
, name
, ret
);
1186 nfs4_update_changeattr(inode
, &res
.cinfo
, timestamp
, 0);
1191 static int _nfs42_proc_setxattr(struct inode
*inode
, const char *name
,
1192 const void *buf
, size_t buflen
, int flags
)
1194 struct nfs_server
*server
= NFS_SERVER(inode
);
1195 __u32 bitmask
[NFS_BITMASK_SZ
];
1196 struct page
*pages
[NFS4XATTR_MAXPAGES
];
1197 struct nfs42_setxattrargs arg
= {
1198 .fh
= NFS_FH(inode
),
1200 .xattr_pages
= pages
,
1201 .xattr_len
= buflen
,
1203 .xattr_flags
= flags
,
1205 struct nfs42_setxattrres res
= {
1208 struct rpc_message msg
= {
1209 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETXATTR
],
1214 unsigned long timestamp
= jiffies
;
1216 if (buflen
> server
->sxasize
)
1219 res
.fattr
= nfs_alloc_fattr();
1224 np
= nfs4_buf_to_pages_noslab(buf
, buflen
, arg
.xattr_pages
);
1232 nfs4_bitmask_set(bitmask
, server
->cache_consistency_bitmask
,
1233 inode
, NFS_INO_INVALID_CHANGE
);
1235 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
,
1237 trace_nfs4_setxattr(inode
, name
, ret
);
1239 for (; np
> 0; np
--)
1240 put_page(pages
[np
- 1]);
1243 nfs4_update_changeattr(inode
, &res
.cinfo
, timestamp
, 0);
1244 ret
= nfs_post_op_update_inode(inode
, res
.fattr
);
1252 static ssize_t
_nfs42_proc_getxattr(struct inode
*inode
, const char *name
,
1253 void *buf
, size_t buflen
, struct page
**pages
,
1256 struct nfs_server
*server
= NFS_SERVER(inode
);
1257 struct nfs42_getxattrargs arg
= {
1258 .fh
= NFS_FH(inode
),
1261 struct nfs42_getxattrres res
;
1262 struct rpc_message msg
= {
1263 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETXATTR
],
1269 arg
.xattr_len
= plen
;
1270 arg
.xattr_pages
= pages
;
1272 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
,
1274 trace_nfs4_getxattr(inode
, name
, ret
);
1279 * Normally, the caching is done one layer up, but for successful
1280 * RPCS, always cache the result here, even if the caller was
1281 * just querying the length, or if the reply was too big for
1282 * the caller. This avoids a second RPC in the case of the
1283 * common query-alloc-retrieve cycle for xattrs.
1285 * Note that xattr_len is always capped to XATTR_SIZE_MAX.
1288 nfs4_xattr_cache_add(inode
, name
, NULL
, pages
, res
.xattr_len
);
1291 if (res
.xattr_len
> buflen
)
1293 _copy_from_pages(buf
, pages
, 0, res
.xattr_len
);
1296 return res
.xattr_len
;
1299 static ssize_t
_nfs42_proc_listxattrs(struct inode
*inode
, void *buf
,
1300 size_t buflen
, u64
*cookiep
, bool *eofp
)
1302 struct nfs_server
*server
= NFS_SERVER(inode
);
1303 struct page
**pages
;
1304 struct nfs42_listxattrsargs arg
= {
1305 .fh
= NFS_FH(inode
),
1308 struct nfs42_listxattrsres res
= {
1311 .xattr_len
= buflen
,
1313 struct rpc_message msg
= {
1314 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LISTXATTRS
],
1323 res
.scratch
= alloc_page(GFP_KERNEL
);
1327 xdrlen
= nfs42_listxattr_xdrsize(buflen
);
1328 if (xdrlen
> server
->lxasize
)
1329 xdrlen
= server
->lxasize
;
1330 np
= xdrlen
/ PAGE_SIZE
+ 1;
1332 pages
= kcalloc(np
, sizeof(struct page
*), GFP_KERNEL
);
1334 goto out_free_scratch
;
1335 for (i
= 0; i
< np
; i
++) {
1336 pages
[i
] = alloc_page(GFP_KERNEL
);
1338 goto out_free_pages
;
1341 arg
.xattr_pages
= pages
;
1344 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
,
1346 trace_nfs4_listxattr(inode
, ret
);
1350 *cookiep
= res
.cookie
;
1357 __free_page(pages
[np
]);
1361 __free_page(res
.scratch
);
1367 ssize_t
nfs42_proc_getxattr(struct inode
*inode
, const char *name
,
1368 void *buf
, size_t buflen
)
1370 struct nfs4_exception exception
= { };
1372 struct page
**pages
;
1374 np
= nfs_page_array_len(0, buflen
?: XATTR_SIZE_MAX
);
1375 pages
= kmalloc_array(np
, sizeof(*pages
), GFP_KERNEL
);
1379 for (i
= 0; i
< np
; i
++) {
1380 pages
[i
] = alloc_page(GFP_KERNEL
);
1388 * The GETXATTR op has no length field in the call, and the
1389 * xattr data is at the end of the reply.
1391 * There is no downside in using the page-aligned length. It will
1392 * allow receiving and caching xattrs that are too large for the
1393 * caller but still fit in the page-rounded value.
1396 err
= _nfs42_proc_getxattr(inode
, name
, buf
, buflen
,
1397 pages
, np
* PAGE_SIZE
);
1400 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
1402 } while (exception
.retry
);
1406 __free_page(pages
[i
]);
1412 int nfs42_proc_setxattr(struct inode
*inode
, const char *name
,
1413 const void *buf
, size_t buflen
, int flags
)
1415 struct nfs4_exception exception
= { };
1419 err
= _nfs42_proc_setxattr(inode
, name
, buf
, buflen
, flags
);
1422 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
1424 } while (exception
.retry
);
1429 ssize_t
nfs42_proc_listxattrs(struct inode
*inode
, void *buf
,
1430 size_t buflen
, u64
*cookiep
, bool *eofp
)
1432 struct nfs4_exception exception
= { };
1436 err
= _nfs42_proc_listxattrs(inode
, buf
, buflen
,
1440 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
1442 } while (exception
.retry
);
1447 int nfs42_proc_removexattr(struct inode
*inode
, const char *name
)
1449 struct nfs4_exception exception
= { };
1453 err
= _nfs42_proc_removexattr(inode
, name
);
1456 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
1458 } while (exception
.retry
);