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
))
119 err
= nfs42_proc_fallocate(&msg
, filep
, offset
, len
);
121 truncate_pagecache_range(inode
, offset
, (offset
+ len
) -1);
122 if (err
== -EOPNOTSUPP
)
123 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_DEALLOCATE
;
129 static loff_t
_nfs42_proc_llseek(struct file
*filep
,
130 struct nfs_lock_context
*lock
, loff_t offset
, int whence
)
132 struct inode
*inode
= file_inode(filep
);
133 struct nfs42_seek_args args
= {
134 .sa_fh
= NFS_FH(inode
),
136 .sa_what
= (whence
== SEEK_HOLE
) ?
137 NFS4_CONTENT_HOLE
: NFS4_CONTENT_DATA
,
139 struct nfs42_seek_res res
;
140 struct rpc_message msg
= {
141 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEEK
],
145 struct nfs_server
*server
= NFS_SERVER(inode
);
148 if (!nfs_server_capable(inode
, NFS_CAP_SEEK
))
151 status
= nfs4_set_rw_stateid(&args
.sa_stateid
, lock
->open_context
,
157 status
= nfs4_call_sync(server
->client
, server
, &msg
,
158 &args
.seq_args
, &res
.seq_res
, 0);
159 if (status
== -ENOTSUPP
)
160 server
->caps
&= ~NFS_CAP_SEEK
;
164 return vfs_setpos(filep
, res
.sr_offset
, inode
->i_sb
->s_maxbytes
);
167 loff_t
nfs42_proc_llseek(struct file
*filep
, loff_t offset
, int whence
)
169 struct nfs_server
*server
= NFS_SERVER(file_inode(filep
));
170 struct nfs4_exception exception
= { };
171 struct nfs_lock_context
*lock
;
174 lock
= nfs_get_lock_context(nfs_file_open_context(filep
));
176 return PTR_ERR(lock
);
178 exception
.inode
= file_inode(filep
);
179 exception
.state
= lock
->open_context
->state
;
182 err
= _nfs42_proc_llseek(filep
, lock
, offset
, whence
);
185 if (err
== -ENOTSUPP
) {
189 err
= nfs4_handle_exception(server
, err
, &exception
);
190 } while (exception
.retry
);
192 nfs_put_lock_context(lock
);
198 nfs42_layoutstat_prepare(struct rpc_task
*task
, void *calldata
)
200 struct nfs42_layoutstat_data
*data
= calldata
;
201 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
203 nfs41_setup_sequence(nfs4_get_session(server
), &data
->args
.seq_args
,
204 &data
->res
.seq_res
, task
);
208 nfs42_layoutstat_done(struct rpc_task
*task
, void *calldata
)
210 struct nfs42_layoutstat_data
*data
= calldata
;
211 struct inode
*inode
= data
->inode
;
212 struct pnfs_layout_hdr
*lo
;
214 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
217 switch (task
->tk_status
) {
220 case -NFS4ERR_EXPIRED
:
221 case -NFS4ERR_STALE_STATEID
:
222 case -NFS4ERR_OLD_STATEID
:
223 case -NFS4ERR_BAD_STATEID
:
224 spin_lock(&inode
->i_lock
);
225 lo
= NFS_I(inode
)->layout
;
226 if (lo
&& nfs4_stateid_match(&data
->args
.stateid
,
231 * Mark the bad layout state as invalid, then retry
232 * with the current stateid.
234 set_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
);
235 pnfs_mark_matching_lsegs_invalid(lo
, &head
, NULL
);
236 spin_unlock(&inode
->i_lock
);
237 pnfs_free_lseg_list(&head
);
239 spin_unlock(&inode
->i_lock
);
243 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_LAYOUTSTATS
;
248 dprintk("%s server returns %d\n", __func__
, task
->tk_status
);
252 nfs42_layoutstat_release(void *calldata
)
254 struct nfs42_layoutstat_data
*data
= calldata
;
255 struct nfs_server
*nfss
= NFS_SERVER(data
->args
.inode
);
257 if (nfss
->pnfs_curr_ld
->cleanup_layoutstats
)
258 nfss
->pnfs_curr_ld
->cleanup_layoutstats(data
);
260 pnfs_put_layout_hdr(NFS_I(data
->args
.inode
)->layout
);
261 smp_mb__before_atomic();
262 clear_bit(NFS_INO_LAYOUTSTATS
, &NFS_I(data
->args
.inode
)->flags
);
263 smp_mb__after_atomic();
264 nfs_iput_and_deactive(data
->inode
);
265 kfree(data
->args
.devinfo
);
269 static const struct rpc_call_ops nfs42_layoutstat_ops
= {
270 .rpc_call_prepare
= nfs42_layoutstat_prepare
,
271 .rpc_call_done
= nfs42_layoutstat_done
,
272 .rpc_release
= nfs42_layoutstat_release
,
275 int nfs42_proc_layoutstats_generic(struct nfs_server
*server
,
276 struct nfs42_layoutstat_data
*data
)
278 struct rpc_message msg
= {
279 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTSTATS
],
280 .rpc_argp
= &data
->args
,
281 .rpc_resp
= &data
->res
,
283 struct rpc_task_setup task_setup
= {
284 .rpc_client
= server
->client
,
286 .callback_ops
= &nfs42_layoutstat_ops
,
287 .callback_data
= data
,
288 .flags
= RPC_TASK_ASYNC
,
290 struct rpc_task
*task
;
292 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
294 nfs42_layoutstat_release(data
);
297 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
298 task
= rpc_run_task(&task_setup
);
300 return PTR_ERR(task
);
304 static int _nfs42_proc_clone(struct rpc_message
*msg
, struct file
*src_f
,
305 struct file
*dst_f
, struct nfs_lock_context
*src_lock
,
306 struct nfs_lock_context
*dst_lock
, loff_t src_offset
,
307 loff_t dst_offset
, loff_t count
)
309 struct inode
*src_inode
= file_inode(src_f
);
310 struct inode
*dst_inode
= file_inode(dst_f
);
311 struct nfs_server
*server
= NFS_SERVER(dst_inode
);
312 struct nfs42_clone_args args
= {
313 .src_fh
= NFS_FH(src_inode
),
314 .dst_fh
= NFS_FH(dst_inode
),
315 .src_offset
= src_offset
,
316 .dst_offset
= dst_offset
,
318 .dst_bitmask
= server
->cache_consistency_bitmask
,
320 struct nfs42_clone_res res
= {
325 msg
->rpc_argp
= &args
;
326 msg
->rpc_resp
= &res
;
328 status
= nfs4_set_rw_stateid(&args
.src_stateid
, src_lock
->open_context
,
329 src_lock
, FMODE_READ
);
333 status
= nfs4_set_rw_stateid(&args
.dst_stateid
, dst_lock
->open_context
,
334 dst_lock
, FMODE_WRITE
);
338 res
.dst_fattr
= nfs_alloc_fattr();
342 status
= nfs4_call_sync(server
->client
, server
, msg
,
343 &args
.seq_args
, &res
.seq_res
, 0);
345 status
= nfs_post_op_update_inode(dst_inode
, res
.dst_fattr
);
347 kfree(res
.dst_fattr
);
351 int nfs42_proc_clone(struct file
*src_f
, struct file
*dst_f
,
352 loff_t src_offset
, loff_t dst_offset
, loff_t count
)
354 struct rpc_message msg
= {
355 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLONE
],
357 struct inode
*inode
= file_inode(src_f
);
358 struct nfs_server
*server
= NFS_SERVER(file_inode(src_f
));
359 struct nfs_lock_context
*src_lock
;
360 struct nfs_lock_context
*dst_lock
;
361 struct nfs4_exception src_exception
= { };
362 struct nfs4_exception dst_exception
= { };
365 if (!nfs_server_capable(inode
, NFS_CAP_CLONE
))
368 src_lock
= nfs_get_lock_context(nfs_file_open_context(src_f
));
369 if (IS_ERR(src_lock
))
370 return PTR_ERR(src_lock
);
372 src_exception
.inode
= file_inode(src_f
);
373 src_exception
.state
= src_lock
->open_context
->state
;
375 dst_lock
= nfs_get_lock_context(nfs_file_open_context(dst_f
));
376 if (IS_ERR(dst_lock
)) {
377 err
= PTR_ERR(dst_lock
);
378 goto out_put_src_lock
;
381 dst_exception
.inode
= file_inode(dst_f
);
382 dst_exception
.state
= dst_lock
->open_context
->state
;
385 err
= _nfs42_proc_clone(&msg
, src_f
, dst_f
, src_lock
, dst_lock
,
386 src_offset
, dst_offset
, count
);
387 if (err
== -ENOTSUPP
|| err
== -EOPNOTSUPP
) {
388 NFS_SERVER(inode
)->caps
&= ~NFS_CAP_CLONE
;
393 err2
= nfs4_handle_exception(server
, err
, &src_exception
);
394 err
= nfs4_handle_exception(server
, err
, &dst_exception
);
397 } while (src_exception
.retry
|| dst_exception
.retry
);
399 nfs_put_lock_context(dst_lock
);
401 nfs_put_lock_context(src_lock
);