2 * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
5 #include <linux/sunrpc/sched.h>
7 #include <linux/nfs3.h>
8 #include <linux/nfs4.h>
9 #include <linux/nfs_xdr.h>
10 #include <linux/nfs_fs.h>
17 #define NFSDBG_FACILITY NFSDBG_PROC
19 static int _nfs42_proc_fallocate(struct rpc_message
*msg
, struct file
*filep
,
20 struct nfs_lock_context
*lock
, loff_t offset
, loff_t len
)
22 struct inode
*inode
= file_inode(filep
);
23 struct nfs_server
*server
= NFS_SERVER(inode
);
24 struct nfs42_falloc_args args
= {
25 .falloc_fh
= NFS_FH(inode
),
26 .falloc_offset
= offset
,
28 .falloc_bitmask
= server
->cache_consistency_bitmask
,
30 struct nfs42_falloc_res res
= {
31 .falloc_server
= server
,
35 msg
->rpc_argp
= &args
;
38 status
= nfs4_set_rw_stateid(&args
.falloc_stateid
, lock
->open_context
,
43 res
.falloc_fattr
= nfs_alloc_fattr();
44 if (!res
.falloc_fattr
)
47 status
= nfs4_call_sync(server
->client
, server
, msg
,
48 &args
.seq_args
, &res
.seq_res
, 0);
50 status
= nfs_post_op_update_inode(inode
, res
.falloc_fattr
);
52 kfree(res
.falloc_fattr
);
56 static int nfs42_proc_fallocate(struct rpc_message
*msg
, struct file
*filep
,
57 loff_t offset
, loff_t len
)
59 struct nfs_server
*server
= NFS_SERVER(file_inode(filep
));
60 struct nfs4_exception exception
= { };
61 struct nfs_lock_context
*lock
;
64 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
68 exception
.inode
= file_inode(filep
);
69 exception
.state
= lock
->open_context
->state
;
72 err
= _nfs42_proc_fallocate(msg
, filep
, lock
, offset
, len
);
73 if (err
== -ENOTSUPP
) {
77 err
= nfs4_handle_exception(server
, err
, &exception
);
78 } while (exception
.retry
);
80 nfs_put_lock_context(lock
);
84 int nfs42_proc_allocate(struct file
*filep
, loff_t offset
, loff_t len
)
86 struct rpc_message msg
= {
87 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ALLOCATE
],
89 struct inode
*inode
= file_inode(filep
);
92 if (!nfs_server_capable(inode
, NFS_CAP_ALLOCATE
))
97 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
98 if (err
== -EOPNOTSUPP
)
99 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_ALLOCATE
;
105 int nfs42_proc_deallocate(struct file
*filep
, loff_t offset
, loff_t len
)
107 struct rpc_message msg
= {
108 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DEALLOCATE
],
110 struct inode
*inode
= file_inode(filep
);
113 if (!nfs_server_capable(inode
, NFS_CAP_DEALLOCATE
))
117 err
= nfs_sync_inode(inode
);
121 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
123 truncate_pagecache_range(inode
, offset
, (offset
+ len
) -1);
124 if (err
== -EOPNOTSUPP
)
125 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_DEALLOCATE
;
131 static ssize_t
_nfs42_proc_copy(struct file
*src
,
132 struct nfs_lock_context
*src_lock
,
134 struct nfs_lock_context
*dst_lock
,
135 struct nfs42_copy_args
*args
,
136 struct nfs42_copy_res
*res
)
138 struct rpc_message msg
= {
139 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COPY
],
143 struct inode
*dst_inode
= file_inode(dst
);
144 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
145 loff_t pos_src
= args
->src_pos
;
146 loff_t pos_dst
= args
->dst_pos
;
147 size_t count
= args
->count
;
150 status
= nfs4_set_rw_stateid(&args
->src_stateid
, src_lock
->open_context
,
151 src_lock
, FMODE_READ
);
155 status
= nfs_filemap_write_and_wait_range(file_inode(src
)->i_mapping
,
156 pos_src
, pos_src
+ (loff_t
)count
- 1);
160 status
= nfs4_set_rw_stateid(&args
->dst_stateid
, dst_lock
->open_context
,
161 dst_lock
, FMODE_WRITE
);
165 status
= nfs_sync_inode(dst_inode
);
169 status
= nfs4_call_sync(server
->client
, server
, &msg
,
170 &args
->seq_args
, &res
->seq_res
, 0);
171 if (status
== -ENOTSUPP
)
172 server
->caps
&= ~NFS_CAP_COPY
;
176 if (res
->write_res
.verifier
.committed
!= NFS_FILE_SYNC
) {
177 status
= nfs_commit_file(dst
, &res
->write_res
.verifier
.verifier
);
182 truncate_pagecache_range(dst_inode
, pos_dst
,
183 pos_dst
+ res
->write_res
.count
);
185 return res
->write_res
.count
;
188 ssize_t
nfs42_proc_copy(struct file
*src
, loff_t pos_src
,
189 struct file
*dst
, loff_t pos_dst
,
192 struct nfs_server
*server
= NFS_SERVER(file_inode(dst
));
193 struct nfs_lock_context
*src_lock
;
194 struct nfs_lock_context
*dst_lock
;
195 struct nfs42_copy_args args
= {
196 .src_fh
= NFS_FH(file_inode(src
)),
198 .dst_fh
= NFS_FH(file_inode(dst
)),
202 struct nfs42_copy_res res
;
203 struct nfs4_exception src_exception
= {
204 .inode
= file_inode(src
),
205 .stateid
= &args
.src_stateid
,
207 struct nfs4_exception dst_exception
= {
208 .inode
= file_inode(dst
),
209 .stateid
= &args
.dst_stateid
,
213 if (!nfs_server_capable(file_inode(dst
), NFS_CAP_COPY
))
216 src_lock
= nfs_get_lock_context(nfs_file_open_context(src
));
217 if (IS_ERR(src_lock
))
218 return PTR_ERR(src_lock
);
220 src_exception
.state
= src_lock
->open_context
->state
;
222 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst
));
223 if (IS_ERR(dst_lock
)) {
224 err
= PTR_ERR(dst_lock
);
225 goto out_put_src_lock
;
228 dst_exception
.state
= dst_lock
->open_context
->state
;
231 inode_lock(file_inode(dst
));
232 err
= _nfs42_proc_copy(src
, src_lock
,
235 inode_unlock(file_inode(dst
));
239 if (err
== -ENOTSUPP
) {
244 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
245 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
248 } while (src_exception
.retry
|| dst_exception
.retry
);
250 nfs_put_lock_context(dst_lock
);
252 nfs_put_lock_context(src_lock
);
256 static loff_t
_nfs42_proc_llseek(struct file
*filep
,
257 struct nfs_lock_context
*lock
, loff_t offset
, int whence
)
259 struct inode
*inode
= file_inode(filep
);
260 struct nfs42_seek_args args
= {
261 .sa_fh
= NFS_FH(inode
),
263 .sa_what
= (whence
== SEEK_HOLE
) ?
264 NFS4_CONTENT_HOLE
: NFS4_CONTENT_DATA
,
266 struct nfs42_seek_res res
;
267 struct rpc_message msg
= {
268 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEEK
],
272 struct nfs_server
*server
= NFS_SERVER(inode
);
275 if (!nfs_server_capable(inode
, NFS_CAP_SEEK
))
278 status
= nfs4_set_rw_stateid(&args
.sa_stateid
, lock
->open_context
,
283 status
= nfs_filemap_write_and_wait_range(inode
->i_mapping
,
288 status
= nfs4_call_sync(server
->client
, server
, &msg
,
289 &args
.seq_args
, &res
.seq_res
, 0);
290 if (status
== -ENOTSUPP
)
291 server
->caps
&= ~NFS_CAP_SEEK
;
295 return vfs_setpos(filep
, res
.sr_offset
, inode
->i_sb
->s_maxbytes
);
298 loff_t
nfs42_proc_llseek(struct file
*filep
, loff_t offset
, int whence
)
300 struct nfs_server
*server
= NFS_SERVER(file_inode(filep
));
301 struct nfs4_exception exception
= { };
302 struct nfs_lock_context
*lock
;
305 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
307 return PTR_ERR(lock
);
309 exception
.inode
= file_inode(filep
);
310 exception
.state
= lock
->open_context
->state
;
313 err
= _nfs42_proc_llseek(filep
, lock
, offset
, whence
);
316 if (err
== -ENOTSUPP
) {
320 err
= nfs4_handle_exception(server
, err
, &exception
);
321 } while (exception
.retry
);
323 nfs_put_lock_context(lock
);
329 nfs42_layoutstat_prepare(struct rpc_task
*task
, void *calldata
)
331 struct nfs42_layoutstat_data
*data
= calldata
;
332 struct inode
*inode
= data
->inode
;
333 struct nfs_server
*server
= NFS_SERVER(inode
);
334 struct pnfs_layout_hdr
*lo
;
336 spin_lock(&inode
->i_lock
);
337 lo
= NFS_I(inode
)->layout
;
338 if (!pnfs_layout_is_valid(lo
)) {
339 spin_unlock(&inode
->i_lock
);
343 nfs4_stateid_copy(&data
->args
.stateid
, &lo
->plh_stateid
);
344 spin_unlock(&inode
->i_lock
);
345 nfs41_setup_sequence(nfs4_get_session(server
), &data
->args
.seq_args
,
346 &data
->res
.seq_res
, task
);
351 nfs42_layoutstat_done(struct rpc_task
*task
, void *calldata
)
353 struct nfs42_layoutstat_data
*data
= calldata
;
354 struct inode
*inode
= data
->inode
;
355 struct pnfs_layout_hdr
*lo
;
357 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
360 switch (task
->tk_status
) {
363 case -NFS4ERR_EXPIRED
:
364 case -NFS4ERR_ADMIN_REVOKED
:
365 case -NFS4ERR_DELEG_REVOKED
:
366 case -NFS4ERR_STALE_STATEID
:
367 case -NFS4ERR_BAD_STATEID
:
368 spin_lock(&inode
->i_lock
);
369 lo
= NFS_I(inode
)->layout
;
370 if (pnfs_layout_is_valid(lo
) &&
371 nfs4_stateid_match(&data
->args
.stateid
,
376 * Mark the bad layout state as invalid, then retry
377 * with the current stateid.
379 pnfs_mark_layout_stateid_invalid(lo
, &head
);
380 spin_unlock(&inode
->i_lock
);
381 pnfs_free_lseg_list(&head
);
383 spin_unlock(&inode
->i_lock
);
385 case -NFS4ERR_OLD_STATEID
:
386 spin_lock(&inode
->i_lock
);
387 lo
= NFS_I(inode
)->layout
;
388 if (pnfs_layout_is_valid(lo
) &&
389 nfs4_stateid_match_other(&data
->args
.stateid
,
391 /* Do we need to delay before resending? */
392 if (!nfs4_stateid_is_newer(&lo
->plh_stateid
,
393 &data
->args
.stateid
))
395 rpc_restart_call_prepare(task
);
397 spin_unlock(&inode
->i_lock
);
401 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_LAYOUTSTATS
;
404 dprintk("%s server returns %d\n", __func__
, task
->tk_status
);
408 nfs42_layoutstat_release(void *calldata
)
410 struct nfs42_layoutstat_data
*data
= calldata
;
411 struct nfs_server
*nfss
= NFS_SERVER(data
->args
.inode
);
413 if (nfss
->pnfs_curr_ld
->cleanup_layoutstats
)
414 nfss
->pnfs_curr_ld
->cleanup_layoutstats(data
);
416 pnfs_put_layout_hdr(NFS_I(data
->args
.inode
)->layout
);
417 smp_mb__before_atomic();
418 clear_bit(NFS_INO_LAYOUTSTATS
, &NFS_I(data
->args
.inode
)->flags
);
419 smp_mb__after_atomic();
420 nfs_iput_and_deactive(data
->inode
);
421 kfree(data
->args
.devinfo
);
425 static const struct rpc_call_ops nfs42_layoutstat_ops
= {
426 .rpc_call_prepare
= nfs42_layoutstat_prepare
,
427 .rpc_call_done
= nfs42_layoutstat_done
,
428 .rpc_release
= nfs42_layoutstat_release
,
431 int nfs42_proc_layoutstats_generic(struct nfs_server
*server
,
432 struct nfs42_layoutstat_data
*data
)
434 struct rpc_message msg
= {
435 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTSTATS
],
436 .rpc_argp
= &data
->args
,
437 .rpc_resp
= &data
->res
,
439 struct rpc_task_setup task_setup
= {
440 .rpc_client
= server
->client
,
442 .callback_ops
= &nfs42_layoutstat_ops
,
443 .callback_data
= data
,
444 .flags
= RPC_TASK_ASYNC
,
446 struct rpc_task
*task
;
448 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
450 nfs42_layoutstat_release(data
);
453 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
454 task
= rpc_run_task(&task_setup
);
456 return PTR_ERR(task
);
461 static int _nfs42_proc_clone(struct rpc_message
*msg
, struct file
*src_f
,
462 struct file
*dst_f
, struct nfs_lock_context
*src_lock
,
463 struct nfs_lock_context
*dst_lock
, loff_t src_offset
,
464 loff_t dst_offset
, loff_t count
)
466 struct inode
*src_inode
= file_inode(src_f
);
467 struct inode
*dst_inode
= file_inode(dst_f
);
468 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
469 struct nfs42_clone_args args
= {
470 .src_fh
= NFS_FH(src_inode
),
471 .dst_fh
= NFS_FH(dst_inode
),
472 .src_offset
= src_offset
,
473 .dst_offset
= dst_offset
,
475 .dst_bitmask
= server
->cache_consistency_bitmask
,
477 struct nfs42_clone_res res
= {
482 msg
->rpc_argp
= &args
;
483 msg
->rpc_resp
= &res
;
485 status
= nfs4_set_rw_stateid(&args
.src_stateid
, src_lock
->open_context
,
486 src_lock
, FMODE_READ
);
490 status
= nfs4_set_rw_stateid(&args
.dst_stateid
, dst_lock
->open_context
,
491 dst_lock
, FMODE_WRITE
);
495 res
.dst_fattr
= nfs_alloc_fattr();
499 status
= nfs4_call_sync(server
->client
, server
, msg
,
500 &args
.seq_args
, &res
.seq_res
, 0);
502 status
= nfs_post_op_update_inode(dst_inode
, res
.dst_fattr
);
504 kfree(res
.dst_fattr
);
508 int nfs42_proc_clone(struct file
*src_f
, struct file
*dst_f
,
509 loff_t src_offset
, loff_t dst_offset
, loff_t count
)
511 struct rpc_message msg
= {
512 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLONE
],
514 struct inode
*inode
= file_inode(src_f
);
515 struct nfs_server
*server
= NFS_SERVER(file_inode(src_f
));
516 struct nfs_lock_context
*src_lock
;
517 struct nfs_lock_context
*dst_lock
;
518 struct nfs4_exception src_exception
= { };
519 struct nfs4_exception dst_exception
= { };
522 if (!nfs_server_capable(inode
, NFS_CAP_CLONE
))
525 src_lock
= nfs_get_lock_context(nfs_file_open_context(src_f
));
526 if (IS_ERR(src_lock
))
527 return PTR_ERR(src_lock
);
529 src_exception
.inode
= file_inode(src_f
);
530 src_exception
.state
= src_lock
->open_context
->state
;
532 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst_f
));
533 if (IS_ERR(dst_lock
)) {
534 err
= PTR_ERR(dst_lock
);
535 goto out_put_src_lock
;
538 dst_exception
.inode
= file_inode(dst_f
);
539 dst_exception
.state
= dst_lock
->open_context
->state
;
542 err
= _nfs42_proc_clone(&msg
, src_f
, dst_f
, src_lock
, dst_lock
,
543 src_offset
, dst_offset
, count
);
544 if (err
== -ENOTSUPP
|| err
== -EOPNOTSUPP
) {
545 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_CLONE
;
550 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
551 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
554 } while (src_exception
.retry
|| dst_exception
.retry
);
556 nfs_put_lock_context(dst_lock
);
558 nfs_put_lock_context(src_lock
);