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
21 static int _nfs42_proc_fallocate(struct rpc_message
*msg
, struct file
*filep
,
22 struct nfs_lock_context
*lock
, loff_t offset
, loff_t len
)
24 struct inode
*inode
= file_inode(filep
);
25 struct nfs_server
*server
= NFS_SERVER(inode
);
26 struct nfs42_falloc_args args
= {
27 .falloc_fh
= NFS_FH(inode
),
28 .falloc_offset
= offset
,
30 .falloc_bitmask
= server
->cache_consistency_bitmask
,
32 struct nfs42_falloc_res res
= {
33 .falloc_server
= server
,
37 msg
->rpc_argp
= &args
;
40 status
= nfs4_set_rw_stateid(&args
.falloc_stateid
, lock
->open_context
,
45 res
.falloc_fattr
= nfs_alloc_fattr();
46 if (!res
.falloc_fattr
)
49 status
= nfs4_call_sync(server
->client
, server
, msg
,
50 &args
.seq_args
, &res
.seq_res
, 0);
52 status
= nfs_post_op_update_inode(inode
, res
.falloc_fattr
);
54 kfree(res
.falloc_fattr
);
58 static int nfs42_proc_fallocate(struct rpc_message
*msg
, struct file
*filep
,
59 loff_t offset
, loff_t len
)
61 struct nfs_server
*server
= NFS_SERVER(file_inode(filep
));
62 struct nfs4_exception exception
= { };
63 struct nfs_lock_context
*lock
;
66 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
70 exception
.inode
= file_inode(filep
);
71 exception
.state
= lock
->open_context
->state
;
74 err
= _nfs42_proc_fallocate(msg
, filep
, lock
, offset
, len
);
75 if (err
== -ENOTSUPP
) {
79 err
= nfs4_handle_exception(server
, err
, &exception
);
80 } while (exception
.retry
);
82 nfs_put_lock_context(lock
);
86 int nfs42_proc_allocate(struct file
*filep
, loff_t offset
, loff_t len
)
88 struct rpc_message msg
= {
89 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ALLOCATE
],
91 struct inode
*inode
= file_inode(filep
);
94 if (!nfs_server_capable(inode
, NFS_CAP_ALLOCATE
))
99 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
100 if (err
== -EOPNOTSUPP
)
101 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_ALLOCATE
;
107 int nfs42_proc_deallocate(struct file
*filep
, loff_t offset
, loff_t len
)
109 struct rpc_message msg
= {
110 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DEALLOCATE
],
112 struct inode
*inode
= file_inode(filep
);
115 if (!nfs_server_capable(inode
, NFS_CAP_DEALLOCATE
))
119 err
= nfs_sync_inode(inode
);
123 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
125 truncate_pagecache_range(inode
, offset
, (offset
+ len
) -1);
126 if (err
== -EOPNOTSUPP
)
127 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_DEALLOCATE
;
133 static ssize_t
_nfs42_proc_copy(struct file
*src
,
134 struct nfs_lock_context
*src_lock
,
136 struct nfs_lock_context
*dst_lock
,
137 struct nfs42_copy_args
*args
,
138 struct nfs42_copy_res
*res
)
140 struct rpc_message msg
= {
141 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COPY
],
145 struct inode
*dst_inode
= file_inode(dst
);
146 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
147 loff_t pos_src
= args
->src_pos
;
148 loff_t pos_dst
= args
->dst_pos
;
149 size_t count
= args
->count
;
152 status
= nfs4_set_rw_stateid(&args
->src_stateid
, src_lock
->open_context
,
153 src_lock
, FMODE_READ
);
157 status
= nfs_filemap_write_and_wait_range(file_inode(src
)->i_mapping
,
158 pos_src
, pos_src
+ (loff_t
)count
- 1);
162 status
= nfs4_set_rw_stateid(&args
->dst_stateid
, dst_lock
->open_context
,
163 dst_lock
, FMODE_WRITE
);
167 status
= nfs_sync_inode(dst_inode
);
171 res
->commit_res
.verf
= kzalloc(sizeof(struct nfs_writeverf
), GFP_NOFS
);
172 if (!res
->commit_res
.verf
)
174 status
= nfs4_call_sync(server
->client
, server
, &msg
,
175 &args
->seq_args
, &res
->seq_res
, 0);
176 if (status
== -ENOTSUPP
)
177 server
->caps
&= ~NFS_CAP_COPY
;
181 if (nfs_write_verifier_cmp(&res
->write_res
.verifier
.verifier
,
182 &res
->commit_res
.verf
->verifier
)) {
187 truncate_pagecache_range(dst_inode
, pos_dst
,
188 pos_dst
+ res
->write_res
.count
);
190 status
= res
->write_res
.count
;
192 kfree(res
->commit_res
.verf
);
196 ssize_t
nfs42_proc_copy(struct file
*src
, loff_t pos_src
,
197 struct file
*dst
, loff_t pos_dst
,
200 struct nfs_server
*server
= NFS_SERVER(file_inode(dst
));
201 struct nfs_lock_context
*src_lock
;
202 struct nfs_lock_context
*dst_lock
;
203 struct nfs42_copy_args args
= {
204 .src_fh
= NFS_FH(file_inode(src
)),
206 .dst_fh
= NFS_FH(file_inode(dst
)),
210 struct nfs42_copy_res res
;
211 struct nfs4_exception src_exception
= {
212 .inode
= file_inode(src
),
213 .stateid
= &args
.src_stateid
,
215 struct nfs4_exception dst_exception
= {
216 .inode
= file_inode(dst
),
217 .stateid
= &args
.dst_stateid
,
221 if (!nfs_server_capable(file_inode(dst
), NFS_CAP_COPY
))
224 src_lock
= nfs_get_lock_context(nfs_file_open_context(src
));
225 if (IS_ERR(src_lock
))
226 return PTR_ERR(src_lock
);
228 src_exception
.state
= src_lock
->open_context
->state
;
230 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst
));
231 if (IS_ERR(dst_lock
)) {
232 err
= PTR_ERR(dst_lock
);
233 goto out_put_src_lock
;
236 dst_exception
.state
= dst_lock
->open_context
->state
;
239 inode_lock(file_inode(dst
));
240 err
= _nfs42_proc_copy(src
, src_lock
,
243 inode_unlock(file_inode(dst
));
247 if (err
== -ENOTSUPP
) {
250 } if (err
== -EAGAIN
) {
251 dst_exception
.retry
= 1;
255 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
256 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
259 } while (src_exception
.retry
|| dst_exception
.retry
);
261 nfs_put_lock_context(dst_lock
);
263 nfs_put_lock_context(src_lock
);
267 static loff_t
_nfs42_proc_llseek(struct file
*filep
,
268 struct nfs_lock_context
*lock
, loff_t offset
, int whence
)
270 struct inode
*inode
= file_inode(filep
);
271 struct nfs42_seek_args args
= {
272 .sa_fh
= NFS_FH(inode
),
274 .sa_what
= (whence
== SEEK_HOLE
) ?
275 NFS4_CONTENT_HOLE
: NFS4_CONTENT_DATA
,
277 struct nfs42_seek_res res
;
278 struct rpc_message msg
= {
279 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEEK
],
283 struct nfs_server
*server
= NFS_SERVER(inode
);
286 if (!nfs_server_capable(inode
, NFS_CAP_SEEK
))
289 status
= nfs4_set_rw_stateid(&args
.sa_stateid
, lock
->open_context
,
294 status
= nfs_filemap_write_and_wait_range(inode
->i_mapping
,
299 status
= nfs4_call_sync(server
->client
, server
, &msg
,
300 &args
.seq_args
, &res
.seq_res
, 0);
301 if (status
== -ENOTSUPP
)
302 server
->caps
&= ~NFS_CAP_SEEK
;
306 return vfs_setpos(filep
, res
.sr_offset
, inode
->i_sb
->s_maxbytes
);
309 loff_t
nfs42_proc_llseek(struct file
*filep
, loff_t offset
, int whence
)
311 struct nfs_server
*server
= NFS_SERVER(file_inode(filep
));
312 struct nfs4_exception exception
= { };
313 struct nfs_lock_context
*lock
;
316 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
318 return PTR_ERR(lock
);
320 exception
.inode
= file_inode(filep
);
321 exception
.state
= lock
->open_context
->state
;
324 err
= _nfs42_proc_llseek(filep
, lock
, offset
, whence
);
327 if (err
== -ENOTSUPP
) {
331 err
= nfs4_handle_exception(server
, err
, &exception
);
332 } while (exception
.retry
);
334 nfs_put_lock_context(lock
);
340 nfs42_layoutstat_prepare(struct rpc_task
*task
, void *calldata
)
342 struct nfs42_layoutstat_data
*data
= calldata
;
343 struct inode
*inode
= data
->inode
;
344 struct nfs_server
*server
= NFS_SERVER(inode
);
345 struct pnfs_layout_hdr
*lo
;
347 spin_lock(&inode
->i_lock
);
348 lo
= NFS_I(inode
)->layout
;
349 if (!pnfs_layout_is_valid(lo
)) {
350 spin_unlock(&inode
->i_lock
);
354 nfs4_stateid_copy(&data
->args
.stateid
, &lo
->plh_stateid
);
355 spin_unlock(&inode
->i_lock
);
356 nfs4_setup_sequence(server
->nfs_client
, &data
->args
.seq_args
,
357 &data
->res
.seq_res
, task
);
361 nfs42_layoutstat_done(struct rpc_task
*task
, void *calldata
)
363 struct nfs42_layoutstat_data
*data
= calldata
;
364 struct inode
*inode
= data
->inode
;
365 struct pnfs_layout_hdr
*lo
;
367 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
370 switch (task
->tk_status
) {
373 case -NFS4ERR_BADHANDLE
:
375 pnfs_destroy_layout(NFS_I(inode
));
377 case -NFS4ERR_EXPIRED
:
378 case -NFS4ERR_ADMIN_REVOKED
:
379 case -NFS4ERR_DELEG_REVOKED
:
380 case -NFS4ERR_STALE_STATEID
:
381 case -NFS4ERR_BAD_STATEID
:
382 spin_lock(&inode
->i_lock
);
383 lo
= NFS_I(inode
)->layout
;
384 if (pnfs_layout_is_valid(lo
) &&
385 nfs4_stateid_match(&data
->args
.stateid
,
390 * Mark the bad layout state as invalid, then retry
391 * with the current stateid.
393 pnfs_mark_layout_stateid_invalid(lo
, &head
);
394 spin_unlock(&inode
->i_lock
);
395 pnfs_free_lseg_list(&head
);
396 nfs_commit_inode(inode
, 0);
398 spin_unlock(&inode
->i_lock
);
400 case -NFS4ERR_OLD_STATEID
:
401 spin_lock(&inode
->i_lock
);
402 lo
= NFS_I(inode
)->layout
;
403 if (pnfs_layout_is_valid(lo
) &&
404 nfs4_stateid_match_other(&data
->args
.stateid
,
406 /* Do we need to delay before resending? */
407 if (!nfs4_stateid_is_newer(&lo
->plh_stateid
,
408 &data
->args
.stateid
))
410 rpc_restart_call_prepare(task
);
412 spin_unlock(&inode
->i_lock
);
416 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_LAYOUTSTATS
;
421 nfs42_layoutstat_release(void *calldata
)
423 struct nfs42_layoutstat_data
*data
= calldata
;
424 struct nfs42_layoutstat_devinfo
*devinfo
= data
->args
.devinfo
;
427 for (i
= 0; i
< data
->args
.num_dev
; i
++) {
428 if (devinfo
[i
].ld_private
.ops
&& devinfo
[i
].ld_private
.ops
->free
)
429 devinfo
[i
].ld_private
.ops
->free(&devinfo
[i
].ld_private
);
432 pnfs_put_layout_hdr(NFS_I(data
->args
.inode
)->layout
);
433 smp_mb__before_atomic();
434 clear_bit(NFS_INO_LAYOUTSTATS
, &NFS_I(data
->args
.inode
)->flags
);
435 smp_mb__after_atomic();
436 nfs_iput_and_deactive(data
->inode
);
437 kfree(data
->args
.devinfo
);
441 static const struct rpc_call_ops nfs42_layoutstat_ops
= {
442 .rpc_call_prepare
= nfs42_layoutstat_prepare
,
443 .rpc_call_done
= nfs42_layoutstat_done
,
444 .rpc_release
= nfs42_layoutstat_release
,
447 int nfs42_proc_layoutstats_generic(struct nfs_server
*server
,
448 struct nfs42_layoutstat_data
*data
)
450 struct rpc_message msg
= {
451 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTSTATS
],
452 .rpc_argp
= &data
->args
,
453 .rpc_resp
= &data
->res
,
455 struct rpc_task_setup task_setup
= {
456 .rpc_client
= server
->client
,
458 .callback_ops
= &nfs42_layoutstat_ops
,
459 .callback_data
= data
,
460 .flags
= RPC_TASK_ASYNC
,
462 struct rpc_task
*task
;
464 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
466 nfs42_layoutstat_release(data
);
469 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0, 0);
470 task
= rpc_run_task(&task_setup
);
472 return PTR_ERR(task
);
477 static int _nfs42_proc_clone(struct rpc_message
*msg
, struct file
*src_f
,
478 struct file
*dst_f
, struct nfs_lock_context
*src_lock
,
479 struct nfs_lock_context
*dst_lock
, loff_t src_offset
,
480 loff_t dst_offset
, loff_t count
)
482 struct inode
*src_inode
= file_inode(src_f
);
483 struct inode
*dst_inode
= file_inode(dst_f
);
484 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
485 struct nfs42_clone_args args
= {
486 .src_fh
= NFS_FH(src_inode
),
487 .dst_fh
= NFS_FH(dst_inode
),
488 .src_offset
= src_offset
,
489 .dst_offset
= dst_offset
,
491 .dst_bitmask
= server
->cache_consistency_bitmask
,
493 struct nfs42_clone_res res
= {
498 msg
->rpc_argp
= &args
;
499 msg
->rpc_resp
= &res
;
501 status
= nfs4_set_rw_stateid(&args
.src_stateid
, src_lock
->open_context
,
502 src_lock
, FMODE_READ
);
506 status
= nfs4_set_rw_stateid(&args
.dst_stateid
, dst_lock
->open_context
,
507 dst_lock
, FMODE_WRITE
);
511 res
.dst_fattr
= nfs_alloc_fattr();
515 status
= nfs4_call_sync(server
->client
, server
, msg
,
516 &args
.seq_args
, &res
.seq_res
, 0);
518 status
= nfs_post_op_update_inode(dst_inode
, res
.dst_fattr
);
520 kfree(res
.dst_fattr
);
524 int nfs42_proc_clone(struct file
*src_f
, struct file
*dst_f
,
525 loff_t src_offset
, loff_t dst_offset
, loff_t count
)
527 struct rpc_message msg
= {
528 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLONE
],
530 struct inode
*inode
= file_inode(src_f
);
531 struct nfs_server
*server
= NFS_SERVER(file_inode(src_f
));
532 struct nfs_lock_context
*src_lock
;
533 struct nfs_lock_context
*dst_lock
;
534 struct nfs4_exception src_exception
= { };
535 struct nfs4_exception dst_exception
= { };
538 if (!nfs_server_capable(inode
, NFS_CAP_CLONE
))
541 src_lock
= nfs_get_lock_context(nfs_file_open_context(src_f
));
542 if (IS_ERR(src_lock
))
543 return PTR_ERR(src_lock
);
545 src_exception
.inode
= file_inode(src_f
);
546 src_exception
.state
= src_lock
->open_context
->state
;
548 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst_f
));
549 if (IS_ERR(dst_lock
)) {
550 err
= PTR_ERR(dst_lock
);
551 goto out_put_src_lock
;
554 dst_exception
.inode
= file_inode(dst_f
);
555 dst_exception
.state
= dst_lock
->open_context
->state
;
558 err
= _nfs42_proc_clone(&msg
, src_f
, dst_f
, src_lock
, dst_lock
,
559 src_offset
, dst_offset
, count
);
560 if (err
== -ENOTSUPP
|| err
== -EOPNOTSUPP
) {
561 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_CLONE
;
566 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
567 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
570 } while (src_exception
.retry
|| dst_exception
.retry
);
572 nfs_put_lock_context(dst_lock
);
574 nfs_put_lock_context(src_lock
);