blob: 98cf58341066fec824a147372a08333ba2817f6c [file] [log] [blame]
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -04001/*
2 * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
3 */
4#include <linux/fs.h>
5#include <linux/sunrpc/sched.h>
6#include <linux/nfs.h>
7#include <linux/nfs3.h>
8#include <linux/nfs4.h>
9#include <linux/nfs_xdr.h>
10#include <linux/nfs_fs.h>
11#include "nfs4_fs.h"
12#include "nfs42.h"
Peng Tao1b4a4bd2015-06-23 19:51:56 +080013#include "iostat.h"
14#include "pnfs.h"
Anna Schumakerefc6f4a2017-01-09 15:14:33 -050015#include "nfs4session.h"
Peng Tao1b4a4bd2015-06-23 19:51:56 +080016#include "internal.h"
17
Anna Schumaker291e1b92015-11-16 14:51:07 -050018#define NFSDBG_FACILITY NFSDBG_PROC
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -040019
Anna Schumakerf4ac1672014-11-25 13:18:15 -050020static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010021 struct nfs_lock_context *lock, loff_t offset, loff_t len)
Anna Schumakerf4ac1672014-11-25 13:18:15 -050022{
23 struct inode *inode = file_inode(filep);
Anna Schumaker9a519402015-03-16 14:06:23 -040024 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerf4ac1672014-11-25 13:18:15 -050025 struct nfs42_falloc_args args = {
26 .falloc_fh = NFS_FH(inode),
27 .falloc_offset = offset,
28 .falloc_length = len,
Anna Schumaker9a519402015-03-16 14:06:23 -040029 .falloc_bitmask = server->cache_consistency_bitmask,
Anna Schumakerf4ac1672014-11-25 13:18:15 -050030 };
Anna Schumaker9a519402015-03-16 14:06:23 -040031 struct nfs42_falloc_res res = {
32 .falloc_server = server,
33 };
Anna Schumakerf4ac1672014-11-25 13:18:15 -050034 int status;
35
36 msg->rpc_argp = &args;
37 msg->rpc_resp = &res;
38
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010039 status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
40 lock, FMODE_WRITE);
Anna Schumakerf4ac1672014-11-25 13:18:15 -050041 if (status)
42 return status;
43
Anna Schumaker9a519402015-03-16 14:06:23 -040044 res.falloc_fattr = nfs_alloc_fattr();
45 if (!res.falloc_fattr)
46 return -ENOMEM;
47
48 status = nfs4_call_sync(server->client, server, msg,
49 &args.seq_args, &res.seq_res, 0);
50 if (status == 0)
51 status = nfs_post_op_update_inode(inode, res.falloc_fattr);
52
53 kfree(res.falloc_fattr);
54 return status;
Anna Schumakerf4ac1672014-11-25 13:18:15 -050055}
56
57static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
58 loff_t offset, loff_t len)
59{
60 struct nfs_server *server = NFS_SERVER(file_inode(filep));
61 struct nfs4_exception exception = { };
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010062 struct nfs_lock_context *lock;
Anna Schumakerf4ac1672014-11-25 13:18:15 -050063 int err;
64
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010065 lock = nfs_get_lock_context(nfs_file_open_context(filep));
66 if (IS_ERR(lock))
67 return PTR_ERR(lock);
68
69 exception.inode = file_inode(filep);
70 exception.state = lock->open_context->state;
71
Anna Schumakerf4ac1672014-11-25 13:18:15 -050072 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010073 err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
74 if (err == -ENOTSUPP) {
75 err = -EOPNOTSUPP;
76 break;
77 }
Anna Schumakerf4ac1672014-11-25 13:18:15 -050078 err = nfs4_handle_exception(server, err, &exception);
79 } while (exception.retry);
80
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010081 nfs_put_lock_context(lock);
Anna Schumakerf4ac1672014-11-25 13:18:15 -050082 return err;
83}
84
85int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
86{
87 struct rpc_message msg = {
88 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
89 };
90 struct inode *inode = file_inode(filep);
91 int err;
92
93 if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
94 return -EOPNOTSUPP;
95
Al Viro59551022016-01-22 15:40:57 -050096 inode_lock(inode);
Anna Schumakerf830f7d2015-03-16 14:06:24 -040097
Anna Schumakerf4ac1672014-11-25 13:18:15 -050098 err = nfs42_proc_fallocate(&msg, filep, offset, len);
99 if (err == -EOPNOTSUPP)
100 NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400101
Al Viro59551022016-01-22 15:40:57 -0500102 inode_unlock(inode);
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500103 return err;
104}
105
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500106int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
107{
108 struct rpc_message msg = {
109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
110 };
111 struct inode *inode = file_inode(filep);
112 int err;
113
114 if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
115 return -EOPNOTSUPP;
116
Al Viro59551022016-01-22 15:40:57 -0500117 inode_lock(inode);
Trond Myklebust1e564d32016-06-25 17:50:53 -0400118 err = nfs_sync_inode(inode);
119 if (err)
120 goto out_unlock;
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400121
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500122 err = nfs42_proc_fallocate(&msg, filep, offset, len);
Anna Schumaker9a519402015-03-16 14:06:23 -0400123 if (err == 0)
124 truncate_pagecache_range(inode, offset, (offset + len) -1);
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500125 if (err == -EOPNOTSUPP)
126 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
Trond Myklebust1e564d32016-06-25 17:50:53 -0400127out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500128 inode_unlock(inode);
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500129 return err;
130}
131
Anna Schumaker2e724482013-05-21 16:53:03 -0400132static ssize_t _nfs42_proc_copy(struct file *src, loff_t pos_src,
133 struct nfs_lock_context *src_lock,
134 struct file *dst, loff_t pos_dst,
135 struct nfs_lock_context *dst_lock,
136 size_t count)
137{
138 struct nfs42_copy_args args = {
139 .src_fh = NFS_FH(file_inode(src)),
140 .src_pos = pos_src,
141 .dst_fh = NFS_FH(file_inode(dst)),
142 .dst_pos = pos_dst,
143 .count = count,
144 };
145 struct nfs42_copy_res res;
146 struct rpc_message msg = {
147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
148 .rpc_argp = &args,
149 .rpc_resp = &res,
150 };
151 struct inode *dst_inode = file_inode(dst);
152 struct nfs_server *server = NFS_SERVER(dst_inode);
153 int status;
154
155 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
156 src_lock, FMODE_READ);
157 if (status)
158 return status;
159
Trond Myklebust837bb1d2016-06-25 18:12:03 -0400160 status = nfs_filemap_write_and_wait_range(file_inode(src)->i_mapping,
161 pos_src, pos_src + (loff_t)count - 1);
162 if (status)
163 return status;
164
Anna Schumaker2e724482013-05-21 16:53:03 -0400165 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
166 dst_lock, FMODE_WRITE);
167 if (status)
168 return status;
169
Trond Myklebust837bb1d2016-06-25 18:12:03 -0400170 status = nfs_sync_inode(dst_inode);
171 if (status)
172 return status;
173
Anna Schumaker2e724482013-05-21 16:53:03 -0400174 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;
178 if (status)
179 return status;
180
181 if (res.write_res.verifier.committed != NFS_FILE_SYNC) {
182 status = nfs_commit_file(dst, &res.write_res.verifier.verifier);
183 if (status)
184 return status;
185 }
186
187 truncate_pagecache_range(dst_inode, pos_dst,
188 pos_dst + res.write_res.count);
189
190 return res.write_res.count;
191}
192
193ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
194 struct file *dst, loff_t pos_dst,
195 size_t count)
196{
197 struct nfs_server *server = NFS_SERVER(file_inode(dst));
198 struct nfs_lock_context *src_lock;
199 struct nfs_lock_context *dst_lock;
200 struct nfs4_exception src_exception = { };
201 struct nfs4_exception dst_exception = { };
202 ssize_t err, err2;
203
204 if (!nfs_server_capable(file_inode(dst), NFS_CAP_COPY))
205 return -EOPNOTSUPP;
206
207 src_lock = nfs_get_lock_context(nfs_file_open_context(src));
208 if (IS_ERR(src_lock))
209 return PTR_ERR(src_lock);
210
211 src_exception.inode = file_inode(src);
212 src_exception.state = src_lock->open_context->state;
213
214 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst));
215 if (IS_ERR(dst_lock)) {
216 err = PTR_ERR(dst_lock);
217 goto out_put_src_lock;
218 }
219
220 dst_exception.inode = file_inode(dst);
221 dst_exception.state = dst_lock->open_context->state;
222
223 do {
Linus Torvaldsea8ea732016-05-26 10:33:33 -0700224 inode_lock(file_inode(dst));
Anna Schumaker2e724482013-05-21 16:53:03 -0400225 err = _nfs42_proc_copy(src, pos_src, src_lock,
226 dst, pos_dst, dst_lock, count);
Linus Torvaldsea8ea732016-05-26 10:33:33 -0700227 inode_unlock(file_inode(dst));
Anna Schumaker2e724482013-05-21 16:53:03 -0400228
229 if (err == -ENOTSUPP) {
230 err = -EOPNOTSUPP;
231 break;
232 }
233
234 err2 = nfs4_handle_exception(server, err, &src_exception);
235 err = nfs4_handle_exception(server, err, &dst_exception);
236 if (!err)
237 err = err2;
238 } while (src_exception.retry || dst_exception.retry);
239
240 nfs_put_lock_context(dst_lock);
241out_put_src_lock:
242 nfs_put_lock_context(src_lock);
243 return err;
244}
245
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100246static loff_t _nfs42_proc_llseek(struct file *filep,
247 struct nfs_lock_context *lock, loff_t offset, int whence)
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400248{
249 struct inode *inode = file_inode(filep);
250 struct nfs42_seek_args args = {
251 .sa_fh = NFS_FH(inode),
252 .sa_offset = offset,
253 .sa_what = (whence == SEEK_HOLE) ?
254 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
255 };
256 struct nfs42_seek_res res;
257 struct rpc_message msg = {
258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
259 .rpc_argp = &args,
260 .rpc_resp = &res,
261 };
262 struct nfs_server *server = NFS_SERVER(inode);
263 int status;
264
Anna Schumaker878ffa92014-10-23 14:00:54 -0400265 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400266 return -ENOTSUPP;
267
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100268 status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
269 lock, FMODE_READ);
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400270 if (status)
271 return status;
272
Trond Myklebuste95fc4a2016-06-25 17:57:39 -0400273 status = nfs_filemap_write_and_wait_range(inode->i_mapping,
274 offset, LLONG_MAX);
275 if (status)
276 return status;
277
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400278 status = nfs4_call_sync(server->client, server, &msg,
279 &args.seq_args, &res.seq_res, 0);
280 if (status == -ENOTSUPP)
281 server->caps &= ~NFS_CAP_SEEK;
282 if (status)
283 return status;
284
285 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
286}
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800287
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400288loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
289{
290 struct nfs_server *server = NFS_SERVER(file_inode(filep));
291 struct nfs4_exception exception = { };
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100292 struct nfs_lock_context *lock;
J. Bruce Fields306a5542015-09-16 17:21:27 -0400293 loff_t err;
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400294
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100295 lock = nfs_get_lock_context(nfs_file_open_context(filep));
296 if (IS_ERR(lock))
297 return PTR_ERR(lock);
298
299 exception.inode = file_inode(filep);
300 exception.state = lock->open_context->state;
301
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400302 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100303 err = _nfs42_proc_llseek(filep, lock, offset, whence);
J. Bruce Fields306a5542015-09-16 17:21:27 -0400304 if (err >= 0)
305 break;
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100306 if (err == -ENOTSUPP) {
307 err = -EOPNOTSUPP;
308 break;
309 }
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400310 err = nfs4_handle_exception(server, err, &exception);
311 } while (exception.retry);
312
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100313 nfs_put_lock_context(lock);
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400314 return err;
315}
316
317
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800318static void
319nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
320{
321 struct nfs42_layoutstat_data *data = calldata;
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400322 struct inode *inode = data->inode;
323 struct nfs_server *server = NFS_SERVER(inode);
324 struct pnfs_layout_hdr *lo;
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800325
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400326 spin_lock(&inode->i_lock);
327 lo = NFS_I(inode)->layout;
328 if (!pnfs_layout_is_valid(lo)) {
329 spin_unlock(&inode->i_lock);
330 rpc_exit(task, 0);
331 return;
332 }
333 nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
334 spin_unlock(&inode->i_lock);
Anna Schumaker6de7e122017-01-09 16:51:52 -0500335 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
336 &data->res.seq_res, task);
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800337}
338
339static void
340nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
341{
342 struct nfs42_layoutstat_data *data = calldata;
Peng Tao68d264c2015-12-06 20:55:09 -0500343 struct inode *inode = data->inode;
344 struct pnfs_layout_hdr *lo;
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800345
346 if (!nfs4_sequence_done(task, &data->res.seq_res))
347 return;
348
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400349 switch (task->tk_status) {
350 case 0:
351 break;
Peng Tao68d264c2015-12-06 20:55:09 -0500352 case -NFS4ERR_EXPIRED:
Trond Myklebust206b3bb2016-08-05 12:16:19 -0400353 case -NFS4ERR_ADMIN_REVOKED:
354 case -NFS4ERR_DELEG_REVOKED:
Peng Tao68d264c2015-12-06 20:55:09 -0500355 case -NFS4ERR_STALE_STATEID:
Peng Tao68d264c2015-12-06 20:55:09 -0500356 case -NFS4ERR_BAD_STATEID:
357 spin_lock(&inode->i_lock);
358 lo = NFS_I(inode)->layout;
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400359 if (pnfs_layout_is_valid(lo) &&
360 nfs4_stateid_match(&data->args.stateid,
Peng Tao68d264c2015-12-06 20:55:09 -0500361 &lo->plh_stateid)) {
362 LIST_HEAD(head);
363
364 /*
365 * Mark the bad layout state as invalid, then retry
366 * with the current stateid.
367 */
Trond Myklebust5f46be02016-07-22 11:25:27 -0400368 pnfs_mark_layout_stateid_invalid(lo, &head);
Peng Tao68d264c2015-12-06 20:55:09 -0500369 spin_unlock(&inode->i_lock);
370 pnfs_free_lseg_list(&head);
371 } else
372 spin_unlock(&inode->i_lock);
373 break;
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400374 case -NFS4ERR_OLD_STATEID:
375 spin_lock(&inode->i_lock);
376 lo = NFS_I(inode)->layout;
377 if (pnfs_layout_is_valid(lo) &&
378 nfs4_stateid_match_other(&data->args.stateid,
379 &lo->plh_stateid)) {
380 /* Do we need to delay before resending? */
381 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
382 &data->args.stateid))
383 rpc_delay(task, HZ);
384 rpc_restart_call_prepare(task);
385 }
386 spin_unlock(&inode->i_lock);
387 break;
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400388 case -ENOTSUPP:
389 case -EOPNOTSUPP:
Peng Tao68d264c2015-12-06 20:55:09 -0500390 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400391 }
Peng Tao68d264c2015-12-06 20:55:09 -0500392
393 dprintk("%s server returns %d\n", __func__, task->tk_status);
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800394}
395
396static void
397nfs42_layoutstat_release(void *calldata)
398{
399 struct nfs42_layoutstat_data *data = calldata;
Trond Myklebust422c93c2016-10-06 17:53:20 -0400400 struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo;
401 int i;
Peng Tao87334082015-06-23 19:51:57 +0800402
Trond Myklebust422c93c2016-10-06 17:53:20 -0400403 for (i = 0; i < data->args.num_dev; i++) {
404 if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free)
405 devinfo[i].ld_private.ops->free(&devinfo[i].ld_private);
406 }
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800407
408 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
Peng Tao1bfe3b22015-06-23 19:52:03 +0800409 smp_mb__before_atomic();
410 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
411 smp_mb__after_atomic();
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800412 nfs_iput_and_deactive(data->inode);
413 kfree(data->args.devinfo);
414 kfree(data);
415}
416
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800417static const struct rpc_call_ops nfs42_layoutstat_ops = {
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800418 .rpc_call_prepare = nfs42_layoutstat_prepare,
419 .rpc_call_done = nfs42_layoutstat_done,
420 .rpc_release = nfs42_layoutstat_release,
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800421};
422
423int nfs42_proc_layoutstats_generic(struct nfs_server *server,
424 struct nfs42_layoutstat_data *data)
425{
426 struct rpc_message msg = {
427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
428 .rpc_argp = &data->args,
429 .rpc_resp = &data->res,
430 };
431 struct rpc_task_setup task_setup = {
432 .rpc_client = server->client,
433 .rpc_message = &msg,
434 .callback_ops = &nfs42_layoutstat_ops,
435 .callback_data = data,
436 .flags = RPC_TASK_ASYNC,
437 };
438 struct rpc_task *task;
439
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800440 data->inode = nfs_igrab_and_active(data->args.inode);
441 if (!data->inode) {
442 nfs42_layoutstat_release(data);
443 return -EAGAIN;
444 }
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800445 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
446 task = rpc_run_task(&task_setup);
447 if (IS_ERR(task))
448 return PTR_ERR(task);
Jeff Layton3f807e52016-10-04 00:07:43 -0400449 rpc_put_task(task);
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800450 return 0;
451}
Peng Taoe5341f32015-09-26 02:24:35 +0800452
453static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100454 struct file *dst_f, struct nfs_lock_context *src_lock,
455 struct nfs_lock_context *dst_lock, loff_t src_offset,
456 loff_t dst_offset, loff_t count)
Peng Taoe5341f32015-09-26 02:24:35 +0800457{
458 struct inode *src_inode = file_inode(src_f);
459 struct inode *dst_inode = file_inode(dst_f);
460 struct nfs_server *server = NFS_SERVER(dst_inode);
461 struct nfs42_clone_args args = {
462 .src_fh = NFS_FH(src_inode),
463 .dst_fh = NFS_FH(dst_inode),
464 .src_offset = src_offset,
465 .dst_offset = dst_offset,
Christoph Hellwig9494b2c2015-11-13 09:38:45 +0100466 .count = count,
Peng Taoe5341f32015-09-26 02:24:35 +0800467 .dst_bitmask = server->cache_consistency_bitmask,
468 };
469 struct nfs42_clone_res res = {
470 .server = server,
471 };
472 int status;
473
474 msg->rpc_argp = &args;
475 msg->rpc_resp = &res;
476
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100477 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
478 src_lock, FMODE_READ);
Peng Taoe5341f32015-09-26 02:24:35 +0800479 if (status)
480 return status;
481
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100482 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
483 dst_lock, FMODE_WRITE);
Peng Taoe5341f32015-09-26 02:24:35 +0800484 if (status)
485 return status;
486
487 res.dst_fattr = nfs_alloc_fattr();
488 if (!res.dst_fattr)
489 return -ENOMEM;
490
491 status = nfs4_call_sync(server->client, server, msg,
492 &args.seq_args, &res.seq_res, 0);
493 if (status == 0)
494 status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
495
496 kfree(res.dst_fattr);
497 return status;
498}
499
500int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
501 loff_t src_offset, loff_t dst_offset, loff_t count)
502{
503 struct rpc_message msg = {
504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
505 };
506 struct inode *inode = file_inode(src_f);
507 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100508 struct nfs_lock_context *src_lock;
509 struct nfs_lock_context *dst_lock;
510 struct nfs4_exception src_exception = { };
511 struct nfs4_exception dst_exception = { };
512 int err, err2;
Peng Taoe5341f32015-09-26 02:24:35 +0800513
514 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
515 return -EOPNOTSUPP;
516
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100517 src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
518 if (IS_ERR(src_lock))
519 return PTR_ERR(src_lock);
520
521 src_exception.inode = file_inode(src_f);
522 src_exception.state = src_lock->open_context->state;
523
524 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
525 if (IS_ERR(dst_lock)) {
526 err = PTR_ERR(dst_lock);
527 goto out_put_src_lock;
528 }
529
530 dst_exception.inode = file_inode(dst_f);
531 dst_exception.state = dst_lock->open_context->state;
532
Peng Taoe5341f32015-09-26 02:24:35 +0800533 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100534 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
535 src_offset, dst_offset, count);
Peng Taoe5341f32015-09-26 02:24:35 +0800536 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
537 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100538 err = -EOPNOTSUPP;
539 break;
Peng Taoe5341f32015-09-26 02:24:35 +0800540 }
Peng Taoe5341f32015-09-26 02:24:35 +0800541
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100542 err2 = nfs4_handle_exception(server, err, &src_exception);
543 err = nfs4_handle_exception(server, err, &dst_exception);
544 if (!err)
545 err = err2;
546 } while (src_exception.retry || dst_exception.retry);
547
548 nfs_put_lock_context(dst_lock);
549out_put_src_lock:
550 nfs_put_lock_context(src_lock);
Peng Taoe5341f32015-09-26 02:24:35 +0800551 return err;
Peng Taoe5341f32015-09-26 02:24:35 +0800552}