blob: 6f47527348042dc83f5b97f48870eed6d1afcf41 [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"
15#include "internal.h"
16
Anna Schumaker291e1b92015-11-16 14:51:07 -050017#define NFSDBG_FACILITY NFSDBG_PROC
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -040018
Anna Schumakerf4ac1672014-11-25 13:18:15 -050019static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010020 struct nfs_lock_context *lock, loff_t offset, loff_t len)
Anna Schumakerf4ac1672014-11-25 13:18:15 -050021{
22 struct inode *inode = file_inode(filep);
Anna Schumaker9a519402015-03-16 14:06:23 -040023 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerf4ac1672014-11-25 13:18:15 -050024 struct nfs42_falloc_args args = {
25 .falloc_fh = NFS_FH(inode),
26 .falloc_offset = offset,
27 .falloc_length = len,
Anna Schumaker9a519402015-03-16 14:06:23 -040028 .falloc_bitmask = server->cache_consistency_bitmask,
Anna Schumakerf4ac1672014-11-25 13:18:15 -050029 };
Anna Schumaker9a519402015-03-16 14:06:23 -040030 struct nfs42_falloc_res res = {
31 .falloc_server = server,
32 };
Anna Schumakerf4ac1672014-11-25 13:18:15 -050033 int status;
34
35 msg->rpc_argp = &args;
36 msg->rpc_resp = &res;
37
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010038 status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
39 lock, FMODE_WRITE);
Anna Schumakerf4ac1672014-11-25 13:18:15 -050040 if (status)
41 return status;
42
Anna Schumaker9a519402015-03-16 14:06:23 -040043 res.falloc_fattr = nfs_alloc_fattr();
44 if (!res.falloc_fattr)
45 return -ENOMEM;
46
47 status = nfs4_call_sync(server->client, server, msg,
48 &args.seq_args, &res.seq_res, 0);
49 if (status == 0)
50 status = nfs_post_op_update_inode(inode, res.falloc_fattr);
51
52 kfree(res.falloc_fattr);
53 return status;
Anna Schumakerf4ac1672014-11-25 13:18:15 -050054}
55
56static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
57 loff_t offset, loff_t len)
58{
59 struct nfs_server *server = NFS_SERVER(file_inode(filep));
60 struct nfs4_exception exception = { };
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010061 struct nfs_lock_context *lock;
Anna Schumakerf4ac1672014-11-25 13:18:15 -050062 int err;
63
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010064 lock = nfs_get_lock_context(nfs_file_open_context(filep));
65 if (IS_ERR(lock))
66 return PTR_ERR(lock);
67
68 exception.inode = file_inode(filep);
69 exception.state = lock->open_context->state;
70
Anna Schumakerf4ac1672014-11-25 13:18:15 -050071 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010072 err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
73 if (err == -ENOTSUPP) {
74 err = -EOPNOTSUPP;
75 break;
76 }
Anna Schumakerf4ac1672014-11-25 13:18:15 -050077 err = nfs4_handle_exception(server, err, &exception);
78 } while (exception.retry);
79
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010080 nfs_put_lock_context(lock);
Anna Schumakerf4ac1672014-11-25 13:18:15 -050081 return err;
82}
83
84int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
85{
86 struct rpc_message msg = {
87 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
88 };
89 struct inode *inode = file_inode(filep);
90 int err;
91
92 if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
93 return -EOPNOTSUPP;
94
Al Viro59551022016-01-22 15:40:57 -050095 inode_lock(inode);
Anna Schumakerf830f7d2015-03-16 14:06:24 -040096
Anna Schumakerf4ac1672014-11-25 13:18:15 -050097 err = nfs42_proc_fallocate(&msg, filep, offset, len);
98 if (err == -EOPNOTSUPP)
99 NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400100
Al Viro59551022016-01-22 15:40:57 -0500101 inode_unlock(inode);
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500102 return err;
103}
104
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500105int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
106{
107 struct rpc_message msg = {
108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
109 };
110 struct inode *inode = file_inode(filep);
111 int err;
112
113 if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
114 return -EOPNOTSUPP;
115
Al Viro59551022016-01-22 15:40:57 -0500116 inode_lock(inode);
Trond Myklebust1e564d32016-06-25 17:50:53 -0400117 err = nfs_sync_inode(inode);
118 if (err)
119 goto out_unlock;
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400120
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500121 err = nfs42_proc_fallocate(&msg, filep, offset, len);
Anna Schumaker9a519402015-03-16 14:06:23 -0400122 if (err == 0)
123 truncate_pagecache_range(inode, offset, (offset + len) -1);
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500124 if (err == -EOPNOTSUPP)
125 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
Trond Myklebust1e564d32016-06-25 17:50:53 -0400126out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500127 inode_unlock(inode);
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500128 return err;
129}
130
Anna Schumaker2e724482013-05-21 16:53:03 -0400131static 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,
135 size_t count)
136{
137 struct nfs42_copy_args args = {
138 .src_fh = NFS_FH(file_inode(src)),
139 .src_pos = pos_src,
140 .dst_fh = NFS_FH(file_inode(dst)),
141 .dst_pos = pos_dst,
142 .count = count,
143 };
144 struct nfs42_copy_res res;
145 struct rpc_message msg = {
146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
147 .rpc_argp = &args,
148 .rpc_resp = &res,
149 };
150 struct inode *dst_inode = file_inode(dst);
151 struct nfs_server *server = NFS_SERVER(dst_inode);
152 int status;
153
154 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
155 src_lock, FMODE_READ);
156 if (status)
157 return status;
158
Trond Myklebust837bb1d2016-06-25 18:12:03 -0400159 status = nfs_filemap_write_and_wait_range(file_inode(src)->i_mapping,
160 pos_src, pos_src + (loff_t)count - 1);
161 if (status)
162 return status;
163
Anna Schumaker2e724482013-05-21 16:53:03 -0400164 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
165 dst_lock, FMODE_WRITE);
166 if (status)
167 return status;
168
Trond Myklebust837bb1d2016-06-25 18:12:03 -0400169 status = nfs_sync_inode(dst_inode);
170 if (status)
171 return status;
172
Anna Schumaker2e724482013-05-21 16:53:03 -0400173 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;
177 if (status)
178 return status;
179
180 if (res.write_res.verifier.committed != NFS_FILE_SYNC) {
181 status = nfs_commit_file(dst, &res.write_res.verifier.verifier);
182 if (status)
183 return status;
184 }
185
186 truncate_pagecache_range(dst_inode, pos_dst,
187 pos_dst + res.write_res.count);
188
189 return res.write_res.count;
190}
191
192ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
193 struct file *dst, loff_t pos_dst,
194 size_t count)
195{
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 = { };
201 ssize_t err, err2;
202
203 if (!nfs_server_capable(file_inode(dst), NFS_CAP_COPY))
204 return -EOPNOTSUPP;
205
206 src_lock = nfs_get_lock_context(nfs_file_open_context(src));
207 if (IS_ERR(src_lock))
208 return PTR_ERR(src_lock);
209
210 src_exception.inode = file_inode(src);
211 src_exception.state = src_lock->open_context->state;
212
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;
217 }
218
219 dst_exception.inode = file_inode(dst);
220 dst_exception.state = dst_lock->open_context->state;
221
222 do {
Linus Torvaldsea8ea732016-05-26 10:33:33 -0700223 inode_lock(file_inode(dst));
Anna Schumaker2e724482013-05-21 16:53:03 -0400224 err = _nfs42_proc_copy(src, pos_src, src_lock,
225 dst, pos_dst, dst_lock, count);
Linus Torvaldsea8ea732016-05-26 10:33:33 -0700226 inode_unlock(file_inode(dst));
Anna Schumaker2e724482013-05-21 16:53:03 -0400227
228 if (err == -ENOTSUPP) {
229 err = -EOPNOTSUPP;
230 break;
231 }
232
233 err2 = nfs4_handle_exception(server, err, &src_exception);
234 err = nfs4_handle_exception(server, err, &dst_exception);
235 if (!err)
236 err = err2;
237 } while (src_exception.retry || dst_exception.retry);
238
239 nfs_put_lock_context(dst_lock);
240out_put_src_lock:
241 nfs_put_lock_context(src_lock);
242 return err;
243}
244
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100245static loff_t _nfs42_proc_llseek(struct file *filep,
246 struct nfs_lock_context *lock, loff_t offset, int whence)
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400247{
248 struct inode *inode = file_inode(filep);
249 struct nfs42_seek_args args = {
250 .sa_fh = NFS_FH(inode),
251 .sa_offset = offset,
252 .sa_what = (whence == SEEK_HOLE) ?
253 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
254 };
255 struct nfs42_seek_res res;
256 struct rpc_message msg = {
257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
258 .rpc_argp = &args,
259 .rpc_resp = &res,
260 };
261 struct nfs_server *server = NFS_SERVER(inode);
262 int status;
263
Anna Schumaker878ffa92014-10-23 14:00:54 -0400264 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400265 return -ENOTSUPP;
266
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100267 status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
268 lock, FMODE_READ);
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400269 if (status)
270 return status;
271
Trond Myklebuste95fc4a2016-06-25 17:57:39 -0400272 status = nfs_filemap_write_and_wait_range(inode->i_mapping,
273 offset, LLONG_MAX);
274 if (status)
275 return status;
276
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400277 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;
281 if (status)
282 return status;
283
284 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
285}
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800286
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400287loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
288{
289 struct nfs_server *server = NFS_SERVER(file_inode(filep));
290 struct nfs4_exception exception = { };
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100291 struct nfs_lock_context *lock;
J. Bruce Fields306a5542015-09-16 17:21:27 -0400292 loff_t err;
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400293
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100294 lock = nfs_get_lock_context(nfs_file_open_context(filep));
295 if (IS_ERR(lock))
296 return PTR_ERR(lock);
297
298 exception.inode = file_inode(filep);
299 exception.state = lock->open_context->state;
300
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400301 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100302 err = _nfs42_proc_llseek(filep, lock, offset, whence);
J. Bruce Fields306a5542015-09-16 17:21:27 -0400303 if (err >= 0)
304 break;
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100305 if (err == -ENOTSUPP) {
306 err = -EOPNOTSUPP;
307 break;
308 }
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400309 err = nfs4_handle_exception(server, err, &exception);
310 } while (exception.retry);
311
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100312 nfs_put_lock_context(lock);
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400313 return err;
314}
315
316
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800317static void
318nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
319{
320 struct nfs42_layoutstat_data *data = calldata;
321 struct nfs_server *server = NFS_SERVER(data->args.inode);
322
323 nfs41_setup_sequence(nfs4_get_session(server), &data->args.seq_args,
324 &data->res.seq_res, task);
325}
326
327static void
328nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
329{
330 struct nfs42_layoutstat_data *data = calldata;
Peng Tao68d264c2015-12-06 20:55:09 -0500331 struct inode *inode = data->inode;
332 struct pnfs_layout_hdr *lo;
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800333
334 if (!nfs4_sequence_done(task, &data->res.seq_res))
335 return;
336
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400337 switch (task->tk_status) {
338 case 0:
339 break;
Peng Tao68d264c2015-12-06 20:55:09 -0500340 case -NFS4ERR_EXPIRED:
Trond Myklebust206b3bb2016-08-05 12:16:19 -0400341 case -NFS4ERR_ADMIN_REVOKED:
342 case -NFS4ERR_DELEG_REVOKED:
Peng Tao68d264c2015-12-06 20:55:09 -0500343 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,
349 &lo->plh_stateid)) {
350 LIST_HEAD(head);
351
352 /*
353 * Mark the bad layout state as invalid, then retry
354 * with the current stateid.
355 */
Trond Myklebust5f46be02016-07-22 11:25:27 -0400356 pnfs_mark_layout_stateid_invalid(lo, &head);
Peng Tao68d264c2015-12-06 20:55:09 -0500357 spin_unlock(&inode->i_lock);
358 pnfs_free_lseg_list(&head);
359 } else
360 spin_unlock(&inode->i_lock);
361 break;
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400362 case -ENOTSUPP:
363 case -EOPNOTSUPP:
Peng Tao68d264c2015-12-06 20:55:09 -0500364 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400365 default:
Peng Tao68d264c2015-12-06 20:55:09 -0500366 break;
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400367 }
Peng Tao68d264c2015-12-06 20:55:09 -0500368
369 dprintk("%s server returns %d\n", __func__, task->tk_status);
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800370}
371
372static void
373nfs42_layoutstat_release(void *calldata)
374{
375 struct nfs42_layoutstat_data *data = calldata;
Peng Tao87334082015-06-23 19:51:57 +0800376 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
377
378 if (nfss->pnfs_curr_ld->cleanup_layoutstats)
379 nfss->pnfs_curr_ld->cleanup_layoutstats(data);
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800380
381 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
Peng Tao1bfe3b22015-06-23 19:52:03 +0800382 smp_mb__before_atomic();
383 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
384 smp_mb__after_atomic();
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800385 nfs_iput_and_deactive(data->inode);
386 kfree(data->args.devinfo);
387 kfree(data);
388}
389
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800390static const struct rpc_call_ops nfs42_layoutstat_ops = {
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800391 .rpc_call_prepare = nfs42_layoutstat_prepare,
392 .rpc_call_done = nfs42_layoutstat_done,
393 .rpc_release = nfs42_layoutstat_release,
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800394};
395
396int nfs42_proc_layoutstats_generic(struct nfs_server *server,
397 struct nfs42_layoutstat_data *data)
398{
399 struct rpc_message msg = {
400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
401 .rpc_argp = &data->args,
402 .rpc_resp = &data->res,
403 };
404 struct rpc_task_setup task_setup = {
405 .rpc_client = server->client,
406 .rpc_message = &msg,
407 .callback_ops = &nfs42_layoutstat_ops,
408 .callback_data = data,
409 .flags = RPC_TASK_ASYNC,
410 };
411 struct rpc_task *task;
412
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800413 data->inode = nfs_igrab_and_active(data->args.inode);
414 if (!data->inode) {
415 nfs42_layoutstat_release(data);
416 return -EAGAIN;
417 }
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800418 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
419 task = rpc_run_task(&task_setup);
420 if (IS_ERR(task))
421 return PTR_ERR(task);
422 return 0;
423}
Peng Taoe5341f32015-09-26 02:24:35 +0800424
425static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100426 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)
Peng Taoe5341f32015-09-26 02:24:35 +0800429{
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,
Christoph Hellwig9494b2c2015-11-13 09:38:45 +0100438 .count = count,
Peng Taoe5341f32015-09-26 02:24:35 +0800439 .dst_bitmask = server->cache_consistency_bitmask,
440 };
441 struct nfs42_clone_res res = {
442 .server = server,
443 };
444 int status;
445
446 msg->rpc_argp = &args;
447 msg->rpc_resp = &res;
448
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100449 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
450 src_lock, FMODE_READ);
Peng Taoe5341f32015-09-26 02:24:35 +0800451 if (status)
452 return status;
453
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100454 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
455 dst_lock, FMODE_WRITE);
Peng Taoe5341f32015-09-26 02:24:35 +0800456 if (status)
457 return status;
458
459 res.dst_fattr = nfs_alloc_fattr();
460 if (!res.dst_fattr)
461 return -ENOMEM;
462
463 status = nfs4_call_sync(server->client, server, msg,
464 &args.seq_args, &res.seq_res, 0);
465 if (status == 0)
466 status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
467
468 kfree(res.dst_fattr);
469 return status;
470}
471
472int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
473 loff_t src_offset, loff_t dst_offset, loff_t count)
474{
475 struct rpc_message msg = {
476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
477 };
478 struct inode *inode = file_inode(src_f);
479 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100480 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 = { };
484 int err, err2;
Peng Taoe5341f32015-09-26 02:24:35 +0800485
486 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
487 return -EOPNOTSUPP;
488
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100489 src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
490 if (IS_ERR(src_lock))
491 return PTR_ERR(src_lock);
492
493 src_exception.inode = file_inode(src_f);
494 src_exception.state = src_lock->open_context->state;
495
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;
500 }
501
502 dst_exception.inode = file_inode(dst_f);
503 dst_exception.state = dst_lock->open_context->state;
504
Peng Taoe5341f32015-09-26 02:24:35 +0800505 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100506 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
507 src_offset, dst_offset, count);
Peng Taoe5341f32015-09-26 02:24:35 +0800508 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
509 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100510 err = -EOPNOTSUPP;
511 break;
Peng Taoe5341f32015-09-26 02:24:35 +0800512 }
Peng Taoe5341f32015-09-26 02:24:35 +0800513
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100514 err2 = nfs4_handle_exception(server, err, &src_exception);
515 err = nfs4_handle_exception(server, err, &dst_exception);
516 if (!err)
517 err = err2;
518 } while (src_exception.retry || dst_exception.retry);
519
520 nfs_put_lock_context(dst_lock);
521out_put_src_lock:
522 nfs_put_lock_context(src_lock);
Peng Taoe5341f32015-09-26 02:24:35 +0800523 return err;
Peng Taoe5341f32015-09-26 02:24:35 +0800524}