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_set_rw_stateid(nfs4_stateid *dst, struct file *file,
22 struct nfs_open_context *open;
23 struct nfs_lock_context *lock;
26 open = get_nfs_open_context(nfs_file_open_context(file));
27 lock = nfs_get_lock_context(open);
29 put_nfs_open_context(open);
33 ret = nfs4_set_rw_stateid(dst, open, lock, fmode);
35 nfs_put_lock_context(lock);
36 put_nfs_open_context(open);
40 static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
41 loff_t offset, loff_t len)
43 struct inode *inode = file_inode(filep);
44 struct nfs_server *server = NFS_SERVER(inode);
45 struct nfs42_falloc_args args = {
46 .falloc_fh = NFS_FH(inode),
47 .falloc_offset = offset,
49 .falloc_bitmask = server->cache_consistency_bitmask,
51 struct nfs42_falloc_res res = {
52 .falloc_server = server,
56 msg->rpc_argp = &args;
59 status = nfs42_set_rw_stateid(&args.falloc_stateid, filep, FMODE_WRITE);
63 res.falloc_fattr = nfs_alloc_fattr();
64 if (!res.falloc_fattr)
67 status = nfs4_call_sync(server->client, server, msg,
68 &args.seq_args, &res.seq_res, 0);
70 status = nfs_post_op_update_inode(inode, res.falloc_fattr);
72 kfree(res.falloc_fattr);
76 static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
77 loff_t offset, loff_t len)
79 struct nfs_server *server = NFS_SERVER(file_inode(filep));
80 struct nfs4_exception exception = { };
84 err = _nfs42_proc_fallocate(msg, filep, offset, len);
87 err = nfs4_handle_exception(server, err, &exception);
88 } while (exception.retry);
93 int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
95 struct rpc_message msg = {
96 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
98 struct inode *inode = file_inode(filep);
101 if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
104 mutex_lock(&inode->i_mutex);
106 err = nfs42_proc_fallocate(&msg, filep, offset, len);
107 if (err == -EOPNOTSUPP)
108 NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
110 mutex_unlock(&inode->i_mutex);
114 int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
116 struct rpc_message msg = {
117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
119 struct inode *inode = file_inode(filep);
122 if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
126 mutex_lock(&inode->i_mutex);
128 err = nfs42_proc_fallocate(&msg, filep, offset, len);
130 truncate_pagecache_range(inode, offset, (offset + len) -1);
131 if (err == -EOPNOTSUPP)
132 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
134 mutex_unlock(&inode->i_mutex);
138 static loff_t _nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
140 struct inode *inode = file_inode(filep);
141 struct nfs42_seek_args args = {
142 .sa_fh = NFS_FH(inode),
144 .sa_what = (whence == SEEK_HOLE) ?
145 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
147 struct nfs42_seek_res res;
148 struct rpc_message msg = {
149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
153 struct nfs_server *server = NFS_SERVER(inode);
156 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
159 status = nfs42_set_rw_stateid(&args.sa_stateid, filep, FMODE_READ);
164 status = nfs4_call_sync(server->client, server, &msg,
165 &args.seq_args, &res.seq_res, 0);
166 if (status == -ENOTSUPP)
167 server->caps &= ~NFS_CAP_SEEK;
171 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
174 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
176 struct nfs_server *server = NFS_SERVER(file_inode(filep));
177 struct nfs4_exception exception = { };
181 err = _nfs42_proc_llseek(filep, offset, whence);
184 if (err == -ENOTSUPP)
186 err = nfs4_handle_exception(server, err, &exception);
187 } while (exception.retry);
194 nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
196 struct nfs42_layoutstat_data *data = calldata;
197 struct nfs_server *server = NFS_SERVER(data->args.inode);
199 nfs41_setup_sequence(nfs4_get_session(server), &data->args.seq_args,
200 &data->res.seq_res, task);
204 nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
206 struct nfs42_layoutstat_data *data = calldata;
208 if (!nfs4_sequence_done(task, &data->res.seq_res))
211 switch (task->tk_status) {
216 NFS_SERVER(data->inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
218 dprintk("%s server returns %d\n", __func__, task->tk_status);
223 nfs42_layoutstat_release(void *calldata)
225 struct nfs42_layoutstat_data *data = calldata;
226 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
228 if (nfss->pnfs_curr_ld->cleanup_layoutstats)
229 nfss->pnfs_curr_ld->cleanup_layoutstats(data);
231 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
232 smp_mb__before_atomic();
233 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
234 smp_mb__after_atomic();
235 nfs_iput_and_deactive(data->inode);
236 kfree(data->args.devinfo);
240 static const struct rpc_call_ops nfs42_layoutstat_ops = {
241 .rpc_call_prepare = nfs42_layoutstat_prepare,
242 .rpc_call_done = nfs42_layoutstat_done,
243 .rpc_release = nfs42_layoutstat_release,
246 int nfs42_proc_layoutstats_generic(struct nfs_server *server,
247 struct nfs42_layoutstat_data *data)
249 struct rpc_message msg = {
250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
251 .rpc_argp = &data->args,
252 .rpc_resp = &data->res,
254 struct rpc_task_setup task_setup = {
255 .rpc_client = server->client,
257 .callback_ops = &nfs42_layoutstat_ops,
258 .callback_data = data,
259 .flags = RPC_TASK_ASYNC,
261 struct rpc_task *task;
263 data->inode = nfs_igrab_and_active(data->args.inode);
265 nfs42_layoutstat_release(data);
268 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
269 task = rpc_run_task(&task_setup);
271 return PTR_ERR(task);
275 static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
276 struct file *dst_f, loff_t src_offset,
277 loff_t dst_offset, loff_t count)
279 struct inode *src_inode = file_inode(src_f);
280 struct inode *dst_inode = file_inode(dst_f);
281 struct nfs_server *server = NFS_SERVER(dst_inode);
282 struct nfs42_clone_args args = {
283 .src_fh = NFS_FH(src_inode),
284 .dst_fh = NFS_FH(dst_inode),
285 .src_offset = src_offset,
286 .dst_offset = dst_offset,
288 .dst_bitmask = server->cache_consistency_bitmask,
290 struct nfs42_clone_res res = {
295 msg->rpc_argp = &args;
296 msg->rpc_resp = &res;
298 status = nfs42_set_rw_stateid(&args.src_stateid, src_f, FMODE_READ);
302 status = nfs42_set_rw_stateid(&args.dst_stateid, dst_f, FMODE_WRITE);
306 res.dst_fattr = nfs_alloc_fattr();
310 status = nfs4_call_sync(server->client, server, msg,
311 &args.seq_args, &res.seq_res, 0);
313 status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
315 kfree(res.dst_fattr);
319 int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
320 loff_t src_offset, loff_t dst_offset, loff_t count)
322 struct rpc_message msg = {
323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
325 struct inode *inode = file_inode(src_f);
326 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
327 struct nfs4_exception exception = { };
330 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
334 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_offset,
336 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
337 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
340 err = nfs4_handle_exception(server, err, &exception);
341 } while (exception.retry);