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
, loff_t pos_src
,
132 struct nfs_lock_context
*src_lock
,
133 struct file
*dst
, loff_t pos_dst
,
134 struct nfs_lock_context
*dst_lock
,
137 struct nfs42_copy_args args
= {
138 .src_fh
= NFS_FH(file_inode(src
)),
140 .dst_fh
= NFS_FH(file_inode(dst
)),
144 struct nfs42_copy_res res
;
145 struct rpc_message msg
= {
146 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COPY
],
150 struct inode
*dst_inode
= file_inode(dst
);
151 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
154 status
= nfs4_set_rw_stateid(&args
.src_stateid
, src_lock
->open_context
,
155 src_lock
, FMODE_READ
);
159 status
= nfs_filemap_write_and_wait_range(file_inode(src
)->i_mapping
,
160 pos_src
, pos_src
+ (loff_t
)count
- 1);
164 status
= nfs4_set_rw_stateid(&args
.dst_stateid
, dst_lock
->open_context
,
165 dst_lock
, FMODE_WRITE
);
169 status
= nfs_sync_inode(dst_inode
);
173 status
= nfs4_call_sync(server
->client
, server
, &msg
,
174 &args
.seq_args
, &res
.seq_res
, 0);
175 if (status
== -ENOTSUPP
)
176 server
->caps
&= ~NFS_CAP_COPY
;
180 if (res
.write_res
.verifier
.committed
!= NFS_FILE_SYNC
) {
181 status
= nfs_commit_file(dst
, &res
.write_res
.verifier
.verifier
);
186 truncate_pagecache_range(dst_inode
, pos_dst
,
187 pos_dst
+ res
.write_res
.count
);
189 return res
.write_res
.count
;
192 ssize_t
nfs42_proc_copy(struct file
*src
, loff_t pos_src
,
193 struct file
*dst
, loff_t pos_dst
,
196 struct nfs_server
*server
= NFS_SERVER(file_inode(dst
));
197 struct nfs_lock_context
*src_lock
;
198 struct nfs_lock_context
*dst_lock
;
199 struct nfs4_exception src_exception
= { };
200 struct nfs4_exception dst_exception
= { };
203 if (!nfs_server_capable(file_inode(dst
), NFS_CAP_COPY
))
206 src_lock
= nfs_get_lock_context(nfs_file_open_context(src
));
207 if (IS_ERR(src_lock
))
208 return PTR_ERR(src_lock
);
210 src_exception
.inode
= file_inode(src
);
211 src_exception
.state
= src_lock
->open_context
->state
;
213 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst
));
214 if (IS_ERR(dst_lock
)) {
215 err
= PTR_ERR(dst_lock
);
216 goto out_put_src_lock
;
219 dst_exception
.inode
= file_inode(dst
);
220 dst_exception
.state
= dst_lock
->open_context
->state
;
223 inode_lock(file_inode(dst
));
224 err
= _nfs42_proc_copy(src
, pos_src
, src_lock
,
225 dst
, pos_dst
, dst_lock
, count
);
226 inode_unlock(file_inode(dst
));
228 if (err
== -ENOTSUPP
) {
233 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
234 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
237 } while (src_exception
.retry
|| dst_exception
.retry
);
239 nfs_put_lock_context(dst_lock
);
241 nfs_put_lock_context(src_lock
);
245 static loff_t
_nfs42_proc_llseek(struct file
*filep
,
246 struct nfs_lock_context
*lock
, loff_t offset
, int whence
)
248 struct inode
*inode
= file_inode(filep
);
249 struct nfs42_seek_args args
= {
250 .sa_fh
= NFS_FH(inode
),
252 .sa_what
= (whence
== SEEK_HOLE
) ?
253 NFS4_CONTENT_HOLE
: NFS4_CONTENT_DATA
,
255 struct nfs42_seek_res res
;
256 struct rpc_message msg
= {
257 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEEK
],
261 struct nfs_server
*server
= NFS_SERVER(inode
);
264 if (!nfs_server_capable(inode
, NFS_CAP_SEEK
))
267 status
= nfs4_set_rw_stateid(&args
.sa_stateid
, lock
->open_context
,
272 status
= nfs_filemap_write_and_wait_range(inode
->i_mapping
,
277 status
= nfs4_call_sync(server
->client
, server
, &msg
,
278 &args
.seq_args
, &res
.seq_res
, 0);
279 if (status
== -ENOTSUPP
)
280 server
->caps
&= ~NFS_CAP_SEEK
;
284 return vfs_setpos(filep
, res
.sr_offset
, inode
->i_sb
->s_maxbytes
);
287 loff_t
nfs42_proc_llseek(struct file
*filep
, loff_t offset
, int whence
)
289 struct nfs_server
*server
= NFS_SERVER(file_inode(filep
));
290 struct nfs4_exception exception
= { };
291 struct nfs_lock_context
*lock
;
294 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
296 return PTR_ERR(lock
);
298 exception
.inode
= file_inode(filep
);
299 exception
.state
= lock
->open_context
->state
;
302 err
= _nfs42_proc_llseek(filep
, lock
, offset
, whence
);
305 if (err
== -ENOTSUPP
) {
309 err
= nfs4_handle_exception(server
, err
, &exception
);
310 } while (exception
.retry
);
312 nfs_put_lock_context(lock
);
318 nfs42_layoutstat_prepare(struct rpc_task
*task
, void *calldata
)
320 struct nfs42_layoutstat_data
*data
= calldata
;
321 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
323 nfs41_setup_sequence(nfs4_get_session(server
), &data
->args
.seq_args
,
324 &data
->res
.seq_res
, task
);
328 nfs42_layoutstat_done(struct rpc_task
*task
, void *calldata
)
330 struct nfs42_layoutstat_data
*data
= calldata
;
331 struct inode
*inode
= data
->inode
;
332 struct pnfs_layout_hdr
*lo
;
334 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
337 switch (task
->tk_status
) {
340 case -NFS4ERR_EXPIRED
:
341 case -NFS4ERR_ADMIN_REVOKED
:
342 case -NFS4ERR_DELEG_REVOKED
:
343 case -NFS4ERR_STALE_STATEID
:
344 case -NFS4ERR_OLD_STATEID
:
345 case -NFS4ERR_BAD_STATEID
:
346 spin_lock(&inode
->i_lock
);
347 lo
= NFS_I(inode
)->layout
;
348 if (lo
&& nfs4_stateid_match(&data
->args
.stateid
,
353 * Mark the bad layout state as invalid, then retry
354 * with the current stateid.
356 pnfs_mark_layout_stateid_invalid(lo
, &head
);
357 spin_unlock(&inode
->i_lock
);
358 pnfs_free_lseg_list(&head
);
360 spin_unlock(&inode
->i_lock
);
364 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_LAYOUTSTATS
;
369 dprintk("%s server returns %d\n", __func__
, task
->tk_status
);
373 nfs42_layoutstat_release(void *calldata
)
375 struct nfs42_layoutstat_data
*data
= calldata
;
376 struct nfs_server
*nfss
= NFS_SERVER(data
->args
.inode
);
378 if (nfss
->pnfs_curr_ld
->cleanup_layoutstats
)
379 nfss
->pnfs_curr_ld
->cleanup_layoutstats(data
);
381 pnfs_put_layout_hdr(NFS_I(data
->args
.inode
)->layout
);
382 smp_mb__before_atomic();
383 clear_bit(NFS_INO_LAYOUTSTATS
, &NFS_I(data
->args
.inode
)->flags
);
384 smp_mb__after_atomic();
385 nfs_iput_and_deactive(data
->inode
);
386 kfree(data
->args
.devinfo
);
390 static const struct rpc_call_ops nfs42_layoutstat_ops
= {
391 .rpc_call_prepare
= nfs42_layoutstat_prepare
,
392 .rpc_call_done
= nfs42_layoutstat_done
,
393 .rpc_release
= nfs42_layoutstat_release
,
396 int nfs42_proc_layoutstats_generic(struct nfs_server
*server
,
397 struct nfs42_layoutstat_data
*data
)
399 struct rpc_message msg
= {
400 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTSTATS
],
401 .rpc_argp
= &data
->args
,
402 .rpc_resp
= &data
->res
,
404 struct rpc_task_setup task_setup
= {
405 .rpc_client
= server
->client
,
407 .callback_ops
= &nfs42_layoutstat_ops
,
408 .callback_data
= data
,
409 .flags
= RPC_TASK_ASYNC
,
411 struct rpc_task
*task
;
413 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
415 nfs42_layoutstat_release(data
);
418 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
419 task
= rpc_run_task(&task_setup
);
421 return PTR_ERR(task
);
425 static int _nfs42_proc_clone(struct rpc_message
*msg
, struct file
*src_f
,
426 struct file
*dst_f
, struct nfs_lock_context
*src_lock
,
427 struct nfs_lock_context
*dst_lock
, loff_t src_offset
,
428 loff_t dst_offset
, loff_t count
)
430 struct inode
*src_inode
= file_inode(src_f
);
431 struct inode
*dst_inode
= file_inode(dst_f
);
432 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
433 struct nfs42_clone_args args
= {
434 .src_fh
= NFS_FH(src_inode
),
435 .dst_fh
= NFS_FH(dst_inode
),
436 .src_offset
= src_offset
,
437 .dst_offset
= dst_offset
,
439 .dst_bitmask
= server
->cache_consistency_bitmask
,
441 struct nfs42_clone_res res
= {
446 msg
->rpc_argp
= &args
;
447 msg
->rpc_resp
= &res
;
449 status
= nfs4_set_rw_stateid(&args
.src_stateid
, src_lock
->open_context
,
450 src_lock
, FMODE_READ
);
454 status
= nfs4_set_rw_stateid(&args
.dst_stateid
, dst_lock
->open_context
,
455 dst_lock
, FMODE_WRITE
);
459 res
.dst_fattr
= nfs_alloc_fattr();
463 status
= nfs4_call_sync(server
->client
, server
, msg
,
464 &args
.seq_args
, &res
.seq_res
, 0);
466 status
= nfs_post_op_update_inode(dst_inode
, res
.dst_fattr
);
468 kfree(res
.dst_fattr
);
472 int nfs42_proc_clone(struct file
*src_f
, struct file
*dst_f
,
473 loff_t src_offset
, loff_t dst_offset
, loff_t count
)
475 struct rpc_message msg
= {
476 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLONE
],
478 struct inode
*inode
= file_inode(src_f
);
479 struct nfs_server
*server
= NFS_SERVER(file_inode(src_f
));
480 struct nfs_lock_context
*src_lock
;
481 struct nfs_lock_context
*dst_lock
;
482 struct nfs4_exception src_exception
= { };
483 struct nfs4_exception dst_exception
= { };
486 if (!nfs_server_capable(inode
, NFS_CAP_CLONE
))
489 src_lock
= nfs_get_lock_context(nfs_file_open_context(src_f
));
490 if (IS_ERR(src_lock
))
491 return PTR_ERR(src_lock
);
493 src_exception
.inode
= file_inode(src_f
);
494 src_exception
.state
= src_lock
->open_context
->state
;
496 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst_f
));
497 if (IS_ERR(dst_lock
)) {
498 err
= PTR_ERR(dst_lock
);
499 goto out_put_src_lock
;
502 dst_exception
.inode
= file_inode(dst_f
);
503 dst_exception
.state
= dst_lock
->open_context
->state
;
506 err
= _nfs42_proc_clone(&msg
, src_f
, dst_f
, src_lock
, dst_lock
,
507 src_offset
, dst_offset
, count
);
508 if (err
== -ENOTSUPP
|| err
== -EOPNOTSUPP
) {
509 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_CLONE
;
514 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
515 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
518 } while (src_exception
.retry
|| dst_exception
.retry
);
520 nfs_put_lock_context(dst_lock
);
522 nfs_put_lock_context(src_lock
);