blob: 9c7feacb03581a0c11c4ccfa40421ff4ca078c9e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -04002/*
3 * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
4 */
5#include <linux/fs.h>
Olga Kornievskaia04915672019-06-04 16:14:30 -04006#include <linux/sunrpc/addr.h>
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -04007#include <linux/sunrpc/sched.h>
8#include <linux/nfs.h>
9#include <linux/nfs3.h>
10#include <linux/nfs4.h>
11#include <linux/nfs_xdr.h>
12#include <linux/nfs_fs.h>
13#include "nfs4_fs.h"
14#include "nfs42.h"
Peng Tao1b4a4bd2015-06-23 19:51:56 +080015#include "iostat.h"
16#include "pnfs.h"
Anna Schumakerefc6f4a2017-01-09 15:14:33 -050017#include "nfs4session.h"
Peng Tao1b4a4bd2015-06-23 19:51:56 +080018#include "internal.h"
Olga Kornievskaia04915672019-06-04 16:14:30 -040019#include "delegation.h"
Peng Tao1b4a4bd2015-06-23 19:51:56 +080020
Anna Schumaker291e1b92015-11-16 14:51:07 -050021#define NFSDBG_FACILITY NFSDBG_PROC
Olga Kornievskaiac975c202018-07-09 15:13:34 -040022static int nfs42_do_offload_cancel_async(struct file *dst, nfs4_stateid *std);
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -040023
Olga Kornievskaia04915672019-06-04 16:14:30 -040024static void nfs42_set_netaddr(struct file *filep, struct nfs42_netaddr *naddr)
25{
26 struct nfs_client *clp = (NFS_SERVER(file_inode(filep)))->nfs_client;
27 unsigned short port = 2049;
28
29 rcu_read_lock();
30 naddr->netid_len = scnprintf(naddr->netid,
31 sizeof(naddr->netid), "%s",
32 rpc_peeraddr2str(clp->cl_rpcclient,
33 RPC_DISPLAY_NETID));
34 naddr->addr_len = scnprintf(naddr->addr,
35 sizeof(naddr->addr),
36 "%s.%u.%u",
37 rpc_peeraddr2str(clp->cl_rpcclient,
38 RPC_DISPLAY_ADDR),
39 port >> 8, port & 255);
40 rcu_read_unlock();
41}
42
Anna Schumakerf4ac1672014-11-25 13:18:15 -050043static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010044 struct nfs_lock_context *lock, loff_t offset, loff_t len)
Anna Schumakerf4ac1672014-11-25 13:18:15 -050045{
46 struct inode *inode = file_inode(filep);
Anna Schumaker9a519402015-03-16 14:06:23 -040047 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerf4ac1672014-11-25 13:18:15 -050048 struct nfs42_falloc_args args = {
49 .falloc_fh = NFS_FH(inode),
50 .falloc_offset = offset,
51 .falloc_length = len,
Anna Schumaker9a519402015-03-16 14:06:23 -040052 .falloc_bitmask = server->cache_consistency_bitmask,
Anna Schumakerf4ac1672014-11-25 13:18:15 -050053 };
Anna Schumaker9a519402015-03-16 14:06:23 -040054 struct nfs42_falloc_res res = {
55 .falloc_server = server,
56 };
Anna Schumakerf4ac1672014-11-25 13:18:15 -050057 int status;
58
59 msg->rpc_argp = &args;
60 msg->rpc_resp = &res;
61
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010062 status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
63 lock, FMODE_WRITE);
Anna Schumakerf4ac1672014-11-25 13:18:15 -050064 if (status)
65 return status;
66
Anna Schumaker9a519402015-03-16 14:06:23 -040067 res.falloc_fattr = nfs_alloc_fattr();
68 if (!res.falloc_fattr)
69 return -ENOMEM;
70
71 status = nfs4_call_sync(server->client, server, msg,
72 &args.seq_args, &res.seq_res, 0);
73 if (status == 0)
74 status = nfs_post_op_update_inode(inode, res.falloc_fattr);
75
76 kfree(res.falloc_fattr);
77 return status;
Anna Schumakerf4ac1672014-11-25 13:18:15 -050078}
79
80static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
81 loff_t offset, loff_t len)
82{
83 struct nfs_server *server = NFS_SERVER(file_inode(filep));
84 struct nfs4_exception exception = { };
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010085 struct nfs_lock_context *lock;
Anna Schumakerf4ac1672014-11-25 13:18:15 -050086 int err;
87
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010088 lock = nfs_get_lock_context(nfs_file_open_context(filep));
89 if (IS_ERR(lock))
90 return PTR_ERR(lock);
91
92 exception.inode = file_inode(filep);
93 exception.state = lock->open_context->state;
94
Anna Schumakerf4ac1672014-11-25 13:18:15 -050095 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010096 err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
97 if (err == -ENOTSUPP) {
98 err = -EOPNOTSUPP;
99 break;
100 }
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500101 err = nfs4_handle_exception(server, err, &exception);
102 } while (exception.retry);
103
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100104 nfs_put_lock_context(lock);
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500105 return err;
106}
107
108int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
109{
110 struct rpc_message msg = {
111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
112 };
113 struct inode *inode = file_inode(filep);
114 int err;
115
116 if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
117 return -EOPNOTSUPP;
118
Al Viro59551022016-01-22 15:40:57 -0500119 inode_lock(inode);
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400120
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500121 err = nfs42_proc_fallocate(&msg, filep, offset, len);
122 if (err == -EOPNOTSUPP)
123 NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400124
Al Viro59551022016-01-22 15:40:57 -0500125 inode_unlock(inode);
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500126 return err;
127}
128
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500129int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
130{
131 struct rpc_message msg = {
132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
133 };
134 struct inode *inode = file_inode(filep);
135 int err;
136
137 if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
138 return -EOPNOTSUPP;
139
Al Viro59551022016-01-22 15:40:57 -0500140 inode_lock(inode);
Trond Myklebust1e564d32016-06-25 17:50:53 -0400141 err = nfs_sync_inode(inode);
142 if (err)
143 goto out_unlock;
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400144
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500145 err = nfs42_proc_fallocate(&msg, filep, offset, len);
Anna Schumaker9a519402015-03-16 14:06:23 -0400146 if (err == 0)
147 truncate_pagecache_range(inode, offset, (offset + len) -1);
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500148 if (err == -EOPNOTSUPP)
149 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
Trond Myklebust1e564d32016-06-25 17:50:53 -0400150out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500151 inode_unlock(inode);
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500152 return err;
153}
154
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400155static int handle_async_copy(struct nfs42_copy_res *res,
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400156 struct nfs_server *dst_server,
157 struct nfs_server *src_server,
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400158 struct file *src,
159 struct file *dst,
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400160 nfs4_stateid *src_stateid,
161 bool *restart)
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400162{
Olga Kornievskaia99f2c552018-11-21 11:24:22 -0500163 struct nfs4_copy_state *copy, *tmp_copy;
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400164 int status = NFS4_OK;
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400165 bool found_pending = false;
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400166 struct nfs_open_context *dst_ctx = nfs_file_open_context(dst);
167 struct nfs_open_context *src_ctx = nfs_file_open_context(src);
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400168
Olga Kornievskaia99f2c552018-11-21 11:24:22 -0500169 copy = kzalloc(sizeof(struct nfs4_copy_state), GFP_NOFS);
170 if (!copy)
171 return -ENOMEM;
172
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400173 spin_lock(&dst_server->nfs_client->cl_lock);
174 list_for_each_entry(tmp_copy,
175 &dst_server->nfs_client->pending_cb_stateids,
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400176 copies) {
Olga Kornievskaia99f2c552018-11-21 11:24:22 -0500177 if (memcmp(&res->write_res.stateid, &tmp_copy->stateid,
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400178 NFS4_STATEID_SIZE))
179 continue;
180 found_pending = true;
Olga Kornievskaia99f2c552018-11-21 11:24:22 -0500181 list_del(&tmp_copy->copies);
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400182 break;
183 }
184 if (found_pending) {
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400185 spin_unlock(&dst_server->nfs_client->cl_lock);
Olga Kornievskaia99f2c552018-11-21 11:24:22 -0500186 kfree(copy);
187 copy = tmp_copy;
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400188 goto out;
189 }
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400190
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400191 memcpy(&copy->stateid, &res->write_res.stateid, NFS4_STATEID_SIZE);
192 init_completion(&copy->completion);
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400193 copy->parent_dst_state = dst_ctx->state;
194 copy->parent_src_state = src_ctx->state;
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400195
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400196 list_add_tail(&copy->copies, &dst_server->ss_copies);
197 spin_unlock(&dst_server->nfs_client->cl_lock);
198
199 if (dst_server != src_server) {
200 spin_lock(&src_server->nfs_client->cl_lock);
201 list_add_tail(&copy->src_copies, &src_server->ss_copies);
202 spin_unlock(&src_server->nfs_client->cl_lock);
203 }
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400204
Olga Kornievskaiac975c202018-07-09 15:13:34 -0400205 status = wait_for_completion_interruptible(&copy->completion);
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400206 spin_lock(&dst_server->nfs_client->cl_lock);
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400207 list_del_init(&copy->copies);
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400208 spin_unlock(&dst_server->nfs_client->cl_lock);
209 if (dst_server != src_server) {
210 spin_lock(&src_server->nfs_client->cl_lock);
211 list_del_init(&copy->src_copies);
212 spin_unlock(&src_server->nfs_client->cl_lock);
213 }
Olga Kornievskaiac975c202018-07-09 15:13:34 -0400214 if (status == -ERESTARTSYS) {
Olga Kornievskaiae4648aa2018-08-13 15:33:01 -0400215 goto out_cancel;
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400216 } else if (copy->flags || copy->error == NFS4ERR_PARTNER_NO_AUTH) {
Olga Kornievskaiae4648aa2018-08-13 15:33:01 -0400217 status = -EAGAIN;
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400218 *restart = true;
Olga Kornievskaiae4648aa2018-08-13 15:33:01 -0400219 goto out_cancel;
Olga Kornievskaiac975c202018-07-09 15:13:34 -0400220 }
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400221out:
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400222 res->write_res.count = copy->count;
223 memcpy(&res->write_res.verifier, &copy->verf, sizeof(copy->verf));
224 status = -copy->error;
225
Olga Kornievskaia12406022019-07-02 14:57:25 -0400226out_free:
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400227 kfree(copy);
228 return status;
Olga Kornievskaiae4648aa2018-08-13 15:33:01 -0400229out_cancel:
230 nfs42_do_offload_cancel_async(dst, &copy->stateid);
Olga Kornievskaia12406022019-07-02 14:57:25 -0400231 if (!nfs42_files_from_same_server(src, dst))
232 nfs42_do_offload_cancel_async(src, src_stateid);
233 goto out_free;
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400234}
235
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -0400236static int process_copy_commit(struct file *dst, loff_t pos_dst,
237 struct nfs42_copy_res *res)
238{
239 struct nfs_commitres cres;
240 int status = -ENOMEM;
241
242 cres.verf = kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS);
243 if (!cres.verf)
244 goto out;
245
246 status = nfs4_proc_commit(dst, pos_dst, res->write_res.count, &cres);
247 if (status)
248 goto out_free;
249 if (nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
250 &cres.verf->verifier)) {
251 dprintk("commit verf differs from copy verf\n");
252 status = -EAGAIN;
253 }
254out_free:
255 kfree(cres.verf);
256out:
257 return status;
258}
259
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500260static ssize_t _nfs42_proc_copy(struct file *src,
Anna Schumaker2e724482013-05-21 16:53:03 -0400261 struct nfs_lock_context *src_lock,
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500262 struct file *dst,
Anna Schumaker2e724482013-05-21 16:53:03 -0400263 struct nfs_lock_context *dst_lock,
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500264 struct nfs42_copy_args *args,
Olga Kornievskaia1d38f3f2019-06-04 11:54:18 -0400265 struct nfs42_copy_res *res,
266 struct nl4_server *nss,
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400267 nfs4_stateid *cnr_stateid,
268 bool *restart)
Anna Schumaker2e724482013-05-21 16:53:03 -0400269{
Anna Schumaker2e724482013-05-21 16:53:03 -0400270 struct rpc_message msg = {
271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500272 .rpc_argp = args,
273 .rpc_resp = res,
Anna Schumaker2e724482013-05-21 16:53:03 -0400274 };
275 struct inode *dst_inode = file_inode(dst);
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400276 struct inode *src_inode = file_inode(src);
277 struct nfs_server *dst_server = NFS_SERVER(dst_inode);
278 struct nfs_server *src_server = NFS_SERVER(src_inode);
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500279 loff_t pos_src = args->src_pos;
280 loff_t pos_dst = args->dst_pos;
281 size_t count = args->count;
Olga Kornievskaia1ee48bd2017-07-06 09:43:02 -0400282 ssize_t status;
Anna Schumaker2e724482013-05-21 16:53:03 -0400283
Olga Kornievskaia1d38f3f2019-06-04 11:54:18 -0400284 if (nss) {
285 args->cp_src = nss;
286 nfs4_stateid_copy(&args->src_stateid, cnr_stateid);
287 } else {
288 status = nfs4_set_rw_stateid(&args->src_stateid,
289 src_lock->open_context, src_lock, FMODE_READ);
290 if (status)
291 return status;
292 }
Trond Myklebust837bb1d2016-06-25 18:12:03 -0400293 status = nfs_filemap_write_and_wait_range(file_inode(src)->i_mapping,
294 pos_src, pos_src + (loff_t)count - 1);
295 if (status)
296 return status;
297
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500298 status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context,
Anna Schumaker2e724482013-05-21 16:53:03 -0400299 dst_lock, FMODE_WRITE);
300 if (status)
301 return status;
302
Trond Myklebust837bb1d2016-06-25 18:12:03 -0400303 status = nfs_sync_inode(dst_inode);
304 if (status)
305 return status;
306
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400307 res->commit_res.verf = NULL;
308 if (args->sync) {
309 res->commit_res.verf =
310 kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS);
311 if (!res->commit_res.verf)
312 return -ENOMEM;
313 }
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400314 set_bit(NFS_CLNT_SRC_SSC_COPY_STATE,
315 &src_lock->open_context->state->flags);
Olga Kornievskaiae4648aa2018-08-13 15:33:01 -0400316 set_bit(NFS_CLNT_DST_SSC_COPY_STATE,
317 &dst_lock->open_context->state->flags);
318
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400319 status = nfs4_call_sync(dst_server->client, dst_server, &msg,
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500320 &args->seq_args, &res->seq_res, 0);
Anna Schumaker2e724482013-05-21 16:53:03 -0400321 if (status == -ENOTSUPP)
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400322 dst_server->caps &= ~NFS_CAP_COPY;
Anna Schumaker2e724482013-05-21 16:53:03 -0400323 if (status)
Olga Kornievskaiae0926932017-05-08 18:02:24 -0400324 goto out;
Anna Schumaker2e724482013-05-21 16:53:03 -0400325
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400326 if (args->sync &&
327 nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
Olga Kornievskaiae0926932017-05-08 18:02:24 -0400328 &res->commit_res.verf->verifier)) {
329 status = -EAGAIN;
330 goto out;
Anna Schumaker2e724482013-05-21 16:53:03 -0400331 }
332
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400333 if (!res->synchronous) {
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400334 status = handle_async_copy(res, dst_server, src_server, src,
335 dst, &args->src_stateid, restart);
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400336 if (status)
337 return status;
338 }
339
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -0400340 if ((!res->synchronous || !args->sync) &&
341 res->write_res.verifier.committed != NFS_FILE_SYNC) {
342 status = process_copy_commit(dst, pos_dst, res);
343 if (status)
344 return status;
345 }
346
Anna Schumaker2e724482013-05-21 16:53:03 -0400347 truncate_pagecache_range(dst_inode, pos_dst,
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500348 pos_dst + res->write_res.count);
Anna Schumaker2e724482013-05-21 16:53:03 -0400349
Olga Kornievskaiae0926932017-05-08 18:02:24 -0400350 status = res->write_res.count;
351out:
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400352 if (args->sync)
353 kfree(res->commit_res.verf);
Olga Kornievskaiae0926932017-05-08 18:02:24 -0400354 return status;
Anna Schumaker2e724482013-05-21 16:53:03 -0400355}
356
357ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
Olga Kornievskaia1d38f3f2019-06-04 11:54:18 -0400358 struct file *dst, loff_t pos_dst, size_t count,
359 struct nl4_server *nss,
360 nfs4_stateid *cnr_stateid)
Anna Schumaker2e724482013-05-21 16:53:03 -0400361{
362 struct nfs_server *server = NFS_SERVER(file_inode(dst));
363 struct nfs_lock_context *src_lock;
364 struct nfs_lock_context *dst_lock;
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500365 struct nfs42_copy_args args = {
366 .src_fh = NFS_FH(file_inode(src)),
367 .src_pos = pos_src,
368 .dst_fh = NFS_FH(file_inode(dst)),
369 .dst_pos = pos_dst,
370 .count = count,
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400371 .sync = false,
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500372 };
373 struct nfs42_copy_res res;
374 struct nfs4_exception src_exception = {
375 .inode = file_inode(src),
376 .stateid = &args.src_stateid,
377 };
378 struct nfs4_exception dst_exception = {
379 .inode = file_inode(dst),
380 .stateid = &args.dst_stateid,
381 };
Anna Schumaker2e724482013-05-21 16:53:03 -0400382 ssize_t err, err2;
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400383 bool restart = false;
Anna Schumaker2e724482013-05-21 16:53:03 -0400384
Anna Schumaker2e724482013-05-21 16:53:03 -0400385 src_lock = nfs_get_lock_context(nfs_file_open_context(src));
386 if (IS_ERR(src_lock))
387 return PTR_ERR(src_lock);
388
Anna Schumaker2e724482013-05-21 16:53:03 -0400389 src_exception.state = src_lock->open_context->state;
390
391 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst));
392 if (IS_ERR(dst_lock)) {
393 err = PTR_ERR(dst_lock);
394 goto out_put_src_lock;
395 }
396
Anna Schumaker2e724482013-05-21 16:53:03 -0400397 dst_exception.state = dst_lock->open_context->state;
398
399 do {
Linus Torvaldsea8ea732016-05-26 10:33:33 -0700400 inode_lock(file_inode(dst));
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500401 err = _nfs42_proc_copy(src, src_lock,
402 dst, dst_lock,
Olga Kornievskaia1d38f3f2019-06-04 11:54:18 -0400403 &args, &res,
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400404 nss, cnr_stateid, &restart);
Linus Torvaldsea8ea732016-05-26 10:33:33 -0700405 inode_unlock(file_inode(dst));
Anna Schumaker2e724482013-05-21 16:53:03 -0400406
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500407 if (err >= 0)
408 break;
Olga Kornievskaia12406022019-07-02 14:57:25 -0400409 if (err == -ENOTSUPP &&
410 nfs42_files_from_same_server(src, dst)) {
Anna Schumaker2e724482013-05-21 16:53:03 -0400411 err = -EOPNOTSUPP;
412 break;
Olga Kornievskaia539f57b2018-07-09 15:13:35 -0400413 } else if (err == -EAGAIN) {
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400414 if (!restart) {
415 dst_exception.retry = 1;
416 continue;
417 }
418 break;
Olga Kornievskaia539f57b2018-07-09 15:13:35 -0400419 } else if (err == -NFS4ERR_OFFLOAD_NO_REQS && !args.sync) {
420 args.sync = true;
Olga Kornievskaiae0926932017-05-08 18:02:24 -0400421 dst_exception.retry = 1;
422 continue;
Olga Kornievskaia6b61c962017-09-26 13:51:39 -0400423 } else if ((err == -ESTALE ||
Olga Kornievskaia12406022019-07-02 14:57:25 -0400424 err == -NFS4ERR_OFFLOAD_DENIED ||
425 err == -ENOTSUPP) &&
Olga Kornievskaia7e350192017-09-25 15:59:44 -0400426 !nfs42_files_from_same_server(src, dst)) {
427 nfs42_do_offload_cancel_async(src, &args.src_stateid);
428 err = -EOPNOTSUPP;
429 break;
Anna Schumaker2e724482013-05-21 16:53:03 -0400430 }
431
432 err2 = nfs4_handle_exception(server, err, &src_exception);
433 err = nfs4_handle_exception(server, err, &dst_exception);
434 if (!err)
435 err = err2;
436 } while (src_exception.retry || dst_exception.retry);
437
438 nfs_put_lock_context(dst_lock);
439out_put_src_lock:
440 nfs_put_lock_context(src_lock);
441 return err;
442}
443
Olga Kornievskaiac975c202018-07-09 15:13:34 -0400444struct nfs42_offloadcancel_data {
445 struct nfs_server *seq_server;
446 struct nfs42_offload_status_args args;
447 struct nfs42_offload_status_res res;
448};
449
450static void nfs42_offload_cancel_prepare(struct rpc_task *task, void *calldata)
451{
452 struct nfs42_offloadcancel_data *data = calldata;
453
454 nfs4_setup_sequence(data->seq_server->nfs_client,
455 &data->args.osa_seq_args,
456 &data->res.osr_seq_res, task);
457}
458
459static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata)
460{
461 struct nfs42_offloadcancel_data *data = calldata;
462
463 nfs41_sequence_done(task, &data->res.osr_seq_res);
464 if (task->tk_status &&
465 nfs4_async_handle_error(task, data->seq_server, NULL,
466 NULL) == -EAGAIN)
467 rpc_restart_call_prepare(task);
468}
469
470static void nfs42_free_offloadcancel_data(void *data)
471{
472 kfree(data);
473}
474
475static const struct rpc_call_ops nfs42_offload_cancel_ops = {
476 .rpc_call_prepare = nfs42_offload_cancel_prepare,
477 .rpc_call_done = nfs42_offload_cancel_done,
478 .rpc_release = nfs42_free_offloadcancel_data,
479};
480
481static int nfs42_do_offload_cancel_async(struct file *dst,
482 nfs4_stateid *stateid)
483{
484 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
485 struct nfs42_offloadcancel_data *data = NULL;
486 struct nfs_open_context *ctx = nfs_file_open_context(dst);
487 struct rpc_task *task;
488 struct rpc_message msg = {
489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_CANCEL],
490 .rpc_cred = ctx->cred,
491 };
492 struct rpc_task_setup task_setup_data = {
493 .rpc_client = dst_server->client,
494 .rpc_message = &msg,
495 .callback_ops = &nfs42_offload_cancel_ops,
496 .workqueue = nfsiod_workqueue,
497 .flags = RPC_TASK_ASYNC,
498 };
499 int status;
500
501 if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL))
502 return -EOPNOTSUPP;
503
504 data = kzalloc(sizeof(struct nfs42_offloadcancel_data), GFP_NOFS);
505 if (data == NULL)
506 return -ENOMEM;
507
508 data->seq_server = dst_server;
509 data->args.osa_src_fh = NFS_FH(file_inode(dst));
510 memcpy(&data->args.osa_stateid, stateid,
511 sizeof(data->args.osa_stateid));
512 msg.rpc_argp = &data->args;
513 msg.rpc_resp = &data->res;
514 task_setup_data.callback_data = data;
515 nfs4_init_sequence(&data->args.osa_seq_args, &data->res.osr_seq_res,
516 1, 0);
517 task = rpc_run_task(&task_setup_data);
518 if (IS_ERR(task))
519 return PTR_ERR(task);
520 status = rpc_wait_for_completion_task(task);
521 if (status == -ENOTSUPP)
522 dst_server->caps &= ~NFS_CAP_OFFLOAD_CANCEL;
523 rpc_put_task(task);
524 return status;
525}
526
Olga Kornievskaia04915672019-06-04 16:14:30 -0400527int _nfs42_proc_copy_notify(struct file *src, struct file *dst,
528 struct nfs42_copy_notify_args *args,
529 struct nfs42_copy_notify_res *res)
530{
531 struct nfs_server *src_server = NFS_SERVER(file_inode(src));
532 struct rpc_message msg = {
533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY_NOTIFY],
534 .rpc_argp = args,
535 .rpc_resp = res,
536 };
537 int status;
538 struct nfs_open_context *ctx;
539 struct nfs_lock_context *l_ctx;
540
541 ctx = get_nfs_open_context(nfs_file_open_context(src));
542 l_ctx = nfs_get_lock_context(ctx);
543 if (IS_ERR(l_ctx))
544 return PTR_ERR(l_ctx);
545
546 status = nfs4_set_rw_stateid(&args->cna_src_stateid, ctx, l_ctx,
547 FMODE_READ);
548 nfs_put_lock_context(l_ctx);
549 if (status)
550 return status;
551
552 status = nfs4_call_sync(src_server->client, src_server, &msg,
553 &args->cna_seq_args, &res->cnr_seq_res, 0);
554 if (status == -ENOTSUPP)
555 src_server->caps &= ~NFS_CAP_COPY_NOTIFY;
556
557 put_nfs_open_context(nfs_file_open_context(src));
558 return status;
559}
560
561int nfs42_proc_copy_notify(struct file *src, struct file *dst,
562 struct nfs42_copy_notify_res *res)
563{
564 struct nfs_server *src_server = NFS_SERVER(file_inode(src));
565 struct nfs42_copy_notify_args *args;
566 struct nfs4_exception exception = {
567 .inode = file_inode(src),
568 };
569 int status;
570
571 if (!(src_server->caps & NFS_CAP_COPY_NOTIFY))
572 return -EOPNOTSUPP;
573
574 args = kzalloc(sizeof(struct nfs42_copy_notify_args), GFP_NOFS);
575 if (args == NULL)
576 return -ENOMEM;
577
578 args->cna_src_fh = NFS_FH(file_inode(src)),
579 args->cna_dst.nl4_type = NL4_NETADDR;
580 nfs42_set_netaddr(dst, &args->cna_dst.u.nl4_addr);
581 exception.stateid = &args->cna_src_stateid;
582
583 do {
584 status = _nfs42_proc_copy_notify(src, dst, args, res);
585 if (status == -ENOTSUPP) {
586 status = -EOPNOTSUPP;
587 goto out;
588 }
589 status = nfs4_handle_exception(src_server, status, &exception);
590 } while (exception.retry);
591
592out:
593 kfree(args);
594 return status;
595}
596
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100597static loff_t _nfs42_proc_llseek(struct file *filep,
598 struct nfs_lock_context *lock, loff_t offset, int whence)
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400599{
600 struct inode *inode = file_inode(filep);
601 struct nfs42_seek_args args = {
602 .sa_fh = NFS_FH(inode),
603 .sa_offset = offset,
604 .sa_what = (whence == SEEK_HOLE) ?
605 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
606 };
607 struct nfs42_seek_res res;
608 struct rpc_message msg = {
609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
610 .rpc_argp = &args,
611 .rpc_resp = &res,
612 };
613 struct nfs_server *server = NFS_SERVER(inode);
614 int status;
615
Anna Schumaker878ffa92014-10-23 14:00:54 -0400616 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400617 return -ENOTSUPP;
618
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100619 status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
620 lock, FMODE_READ);
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400621 if (status)
622 return status;
623
Trond Myklebuste95fc4a2016-06-25 17:57:39 -0400624 status = nfs_filemap_write_and_wait_range(inode->i_mapping,
625 offset, LLONG_MAX);
626 if (status)
627 return status;
628
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400629 status = nfs4_call_sync(server->client, server, &msg,
630 &args.seq_args, &res.seq_res, 0);
631 if (status == -ENOTSUPP)
632 server->caps &= ~NFS_CAP_SEEK;
633 if (status)
634 return status;
635
636 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
637}
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800638
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400639loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
640{
641 struct nfs_server *server = NFS_SERVER(file_inode(filep));
642 struct nfs4_exception exception = { };
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100643 struct nfs_lock_context *lock;
J. Bruce Fields306a5542015-09-16 17:21:27 -0400644 loff_t err;
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400645
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100646 lock = nfs_get_lock_context(nfs_file_open_context(filep));
647 if (IS_ERR(lock))
648 return PTR_ERR(lock);
649
650 exception.inode = file_inode(filep);
651 exception.state = lock->open_context->state;
652
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400653 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100654 err = _nfs42_proc_llseek(filep, lock, offset, whence);
J. Bruce Fields306a5542015-09-16 17:21:27 -0400655 if (err >= 0)
656 break;
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100657 if (err == -ENOTSUPP) {
658 err = -EOPNOTSUPP;
659 break;
660 }
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400661 err = nfs4_handle_exception(server, err, &exception);
662 } while (exception.retry);
663
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100664 nfs_put_lock_context(lock);
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400665 return err;
666}
667
668
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800669static void
670nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
671{
672 struct nfs42_layoutstat_data *data = calldata;
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400673 struct inode *inode = data->inode;
674 struct nfs_server *server = NFS_SERVER(inode);
675 struct pnfs_layout_hdr *lo;
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800676
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400677 spin_lock(&inode->i_lock);
678 lo = NFS_I(inode)->layout;
679 if (!pnfs_layout_is_valid(lo)) {
680 spin_unlock(&inode->i_lock);
681 rpc_exit(task, 0);
682 return;
683 }
684 nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
685 spin_unlock(&inode->i_lock);
Anna Schumaker6de7e122017-01-09 16:51:52 -0500686 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
687 &data->res.seq_res, task);
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800688}
689
690static void
691nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
692{
693 struct nfs42_layoutstat_data *data = calldata;
Peng Tao68d264c2015-12-06 20:55:09 -0500694 struct inode *inode = data->inode;
695 struct pnfs_layout_hdr *lo;
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800696
697 if (!nfs4_sequence_done(task, &data->res.seq_res))
698 return;
699
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400700 switch (task->tk_status) {
701 case 0:
702 break;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400703 case -NFS4ERR_BADHANDLE:
704 case -ESTALE:
705 pnfs_destroy_layout(NFS_I(inode));
706 break;
Peng Tao68d264c2015-12-06 20:55:09 -0500707 case -NFS4ERR_EXPIRED:
Trond Myklebust206b3bb2016-08-05 12:16:19 -0400708 case -NFS4ERR_ADMIN_REVOKED:
709 case -NFS4ERR_DELEG_REVOKED:
Peng Tao68d264c2015-12-06 20:55:09 -0500710 case -NFS4ERR_STALE_STATEID:
Peng Tao68d264c2015-12-06 20:55:09 -0500711 case -NFS4ERR_BAD_STATEID:
712 spin_lock(&inode->i_lock);
713 lo = NFS_I(inode)->layout;
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400714 if (pnfs_layout_is_valid(lo) &&
715 nfs4_stateid_match(&data->args.stateid,
Peng Tao68d264c2015-12-06 20:55:09 -0500716 &lo->plh_stateid)) {
717 LIST_HEAD(head);
718
719 /*
720 * Mark the bad layout state as invalid, then retry
721 * with the current stateid.
722 */
Trond Myklebust5f46be02016-07-22 11:25:27 -0400723 pnfs_mark_layout_stateid_invalid(lo, &head);
Peng Tao68d264c2015-12-06 20:55:09 -0500724 spin_unlock(&inode->i_lock);
725 pnfs_free_lseg_list(&head);
Trond Myklebust1f18b822017-04-29 10:10:17 -0400726 nfs_commit_inode(inode, 0);
Peng Tao68d264c2015-12-06 20:55:09 -0500727 } else
728 spin_unlock(&inode->i_lock);
729 break;
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400730 case -NFS4ERR_OLD_STATEID:
731 spin_lock(&inode->i_lock);
732 lo = NFS_I(inode)->layout;
733 if (pnfs_layout_is_valid(lo) &&
734 nfs4_stateid_match_other(&data->args.stateid,
735 &lo->plh_stateid)) {
736 /* Do we need to delay before resending? */
737 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
738 &data->args.stateid))
739 rpc_delay(task, HZ);
740 rpc_restart_call_prepare(task);
741 }
742 spin_unlock(&inode->i_lock);
743 break;
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400744 case -ENOTSUPP:
745 case -EOPNOTSUPP:
Peng Tao68d264c2015-12-06 20:55:09 -0500746 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400747 }
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800748}
749
750static void
751nfs42_layoutstat_release(void *calldata)
752{
753 struct nfs42_layoutstat_data *data = calldata;
Trond Myklebust422c93c2016-10-06 17:53:20 -0400754 struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo;
755 int i;
Peng Tao87334082015-06-23 19:51:57 +0800756
Trond Myklebust422c93c2016-10-06 17:53:20 -0400757 for (i = 0; i < data->args.num_dev; i++) {
758 if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free)
759 devinfo[i].ld_private.ops->free(&devinfo[i].ld_private);
760 }
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800761
762 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
Peng Tao1bfe3b22015-06-23 19:52:03 +0800763 smp_mb__before_atomic();
764 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
765 smp_mb__after_atomic();
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800766 nfs_iput_and_deactive(data->inode);
767 kfree(data->args.devinfo);
768 kfree(data);
769}
770
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800771static const struct rpc_call_ops nfs42_layoutstat_ops = {
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800772 .rpc_call_prepare = nfs42_layoutstat_prepare,
773 .rpc_call_done = nfs42_layoutstat_done,
774 .rpc_release = nfs42_layoutstat_release,
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800775};
776
777int nfs42_proc_layoutstats_generic(struct nfs_server *server,
778 struct nfs42_layoutstat_data *data)
779{
780 struct rpc_message msg = {
781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
782 .rpc_argp = &data->args,
783 .rpc_resp = &data->res,
784 };
785 struct rpc_task_setup task_setup = {
786 .rpc_client = server->client,
787 .rpc_message = &msg,
788 .callback_ops = &nfs42_layoutstat_ops,
789 .callback_data = data,
790 .flags = RPC_TASK_ASYNC,
791 };
792 struct rpc_task *task;
793
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800794 data->inode = nfs_igrab_and_active(data->args.inode);
795 if (!data->inode) {
796 nfs42_layoutstat_release(data);
797 return -EAGAIN;
798 }
Anna Schumakerfba83f32018-05-04 16:22:50 -0400799 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800800 task = rpc_run_task(&task_setup);
801 if (IS_ERR(task))
802 return PTR_ERR(task);
Jeff Layton3f807e52016-10-04 00:07:43 -0400803 rpc_put_task(task);
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800804 return 0;
805}
Peng Taoe5341f32015-09-26 02:24:35 +0800806
Trond Myklebust3eb86092019-02-08 10:31:05 -0500807static struct nfs42_layouterror_data *
808nfs42_alloc_layouterror_data(struct pnfs_layout_segment *lseg, gfp_t gfp_flags)
809{
810 struct nfs42_layouterror_data *data;
811 struct inode *inode = lseg->pls_layout->plh_inode;
812
813 data = kzalloc(sizeof(*data), gfp_flags);
814 if (data) {
815 data->args.inode = data->inode = nfs_igrab_and_active(inode);
816 if (data->inode) {
817 data->lseg = pnfs_get_lseg(lseg);
818 if (data->lseg)
819 return data;
820 nfs_iput_and_deactive(data->inode);
821 }
822 kfree(data);
823 }
824 return NULL;
825}
826
827static void
828nfs42_free_layouterror_data(struct nfs42_layouterror_data *data)
829{
830 pnfs_put_lseg(data->lseg);
831 nfs_iput_and_deactive(data->inode);
832 kfree(data);
833}
834
835static void
836nfs42_layouterror_prepare(struct rpc_task *task, void *calldata)
837{
838 struct nfs42_layouterror_data *data = calldata;
839 struct inode *inode = data->inode;
840 struct nfs_server *server = NFS_SERVER(inode);
841 struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
842 unsigned i;
843
844 spin_lock(&inode->i_lock);
845 if (!pnfs_layout_is_valid(lo)) {
846 spin_unlock(&inode->i_lock);
847 rpc_exit(task, 0);
848 return;
849 }
850 for (i = 0; i < data->args.num_errors; i++)
851 nfs4_stateid_copy(&data->args.errors[i].stateid,
852 &lo->plh_stateid);
853 spin_unlock(&inode->i_lock);
854 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
855 &data->res.seq_res, task);
856}
857
858static void
859nfs42_layouterror_done(struct rpc_task *task, void *calldata)
860{
861 struct nfs42_layouterror_data *data = calldata;
862 struct inode *inode = data->inode;
863 struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
864
865 if (!nfs4_sequence_done(task, &data->res.seq_res))
866 return;
867
868 switch (task->tk_status) {
869 case 0:
870 break;
871 case -NFS4ERR_BADHANDLE:
872 case -ESTALE:
873 pnfs_destroy_layout(NFS_I(inode));
874 break;
875 case -NFS4ERR_EXPIRED:
876 case -NFS4ERR_ADMIN_REVOKED:
877 case -NFS4ERR_DELEG_REVOKED:
878 case -NFS4ERR_STALE_STATEID:
879 case -NFS4ERR_BAD_STATEID:
880 spin_lock(&inode->i_lock);
881 if (pnfs_layout_is_valid(lo) &&
882 nfs4_stateid_match(&data->args.errors[0].stateid,
883 &lo->plh_stateid)) {
884 LIST_HEAD(head);
885
886 /*
887 * Mark the bad layout state as invalid, then retry
888 * with the current stateid.
889 */
890 pnfs_mark_layout_stateid_invalid(lo, &head);
891 spin_unlock(&inode->i_lock);
892 pnfs_free_lseg_list(&head);
893 nfs_commit_inode(inode, 0);
894 } else
895 spin_unlock(&inode->i_lock);
896 break;
897 case -NFS4ERR_OLD_STATEID:
898 spin_lock(&inode->i_lock);
899 if (pnfs_layout_is_valid(lo) &&
900 nfs4_stateid_match_other(&data->args.errors[0].stateid,
901 &lo->plh_stateid)) {
902 /* Do we need to delay before resending? */
903 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
904 &data->args.errors[0].stateid))
905 rpc_delay(task, HZ);
906 rpc_restart_call_prepare(task);
907 }
908 spin_unlock(&inode->i_lock);
909 break;
910 case -ENOTSUPP:
911 case -EOPNOTSUPP:
912 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTERROR;
913 }
914}
915
916static void
917nfs42_layouterror_release(void *calldata)
918{
919 struct nfs42_layouterror_data *data = calldata;
920
921 nfs42_free_layouterror_data(data);
922}
923
924static const struct rpc_call_ops nfs42_layouterror_ops = {
925 .rpc_call_prepare = nfs42_layouterror_prepare,
926 .rpc_call_done = nfs42_layouterror_done,
927 .rpc_release = nfs42_layouterror_release,
928};
929
930int nfs42_proc_layouterror(struct pnfs_layout_segment *lseg,
931 const struct nfs42_layout_error *errors, size_t n)
932{
933 struct inode *inode = lseg->pls_layout->plh_inode;
934 struct nfs42_layouterror_data *data;
935 struct rpc_task *task;
936 struct rpc_message msg = {
937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTERROR],
938 };
939 struct rpc_task_setup task_setup = {
940 .rpc_message = &msg,
941 .callback_ops = &nfs42_layouterror_ops,
942 .flags = RPC_TASK_ASYNC,
943 };
944 unsigned int i;
945
946 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTERROR))
947 return -EOPNOTSUPP;
948 if (n > NFS42_LAYOUTERROR_MAX)
949 return -EINVAL;
950 data = nfs42_alloc_layouterror_data(lseg, GFP_NOFS);
951 if (!data)
952 return -ENOMEM;
953 for (i = 0; i < n; i++) {
954 data->args.errors[i] = errors[i];
955 data->args.num_errors++;
956 data->res.num_errors++;
957 }
958 msg.rpc_argp = &data->args;
959 msg.rpc_resp = &data->res;
960 task_setup.callback_data = data;
961 task_setup.rpc_client = NFS_SERVER(inode)->client;
962 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
963 task = rpc_run_task(&task_setup);
964 if (IS_ERR(task))
965 return PTR_ERR(task);
966 rpc_put_task(task);
967 return 0;
968}
969EXPORT_SYMBOL_GPL(nfs42_proc_layouterror);
970
Peng Taoe5341f32015-09-26 02:24:35 +0800971static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100972 struct file *dst_f, struct nfs_lock_context *src_lock,
973 struct nfs_lock_context *dst_lock, loff_t src_offset,
974 loff_t dst_offset, loff_t count)
Peng Taoe5341f32015-09-26 02:24:35 +0800975{
976 struct inode *src_inode = file_inode(src_f);
977 struct inode *dst_inode = file_inode(dst_f);
978 struct nfs_server *server = NFS_SERVER(dst_inode);
979 struct nfs42_clone_args args = {
980 .src_fh = NFS_FH(src_inode),
981 .dst_fh = NFS_FH(dst_inode),
982 .src_offset = src_offset,
983 .dst_offset = dst_offset,
Christoph Hellwig9494b2c2015-11-13 09:38:45 +0100984 .count = count,
Peng Taoe5341f32015-09-26 02:24:35 +0800985 .dst_bitmask = server->cache_consistency_bitmask,
986 };
987 struct nfs42_clone_res res = {
988 .server = server,
989 };
990 int status;
991
992 msg->rpc_argp = &args;
993 msg->rpc_resp = &res;
994
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100995 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
996 src_lock, FMODE_READ);
Peng Taoe5341f32015-09-26 02:24:35 +0800997 if (status)
998 return status;
999
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001000 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
1001 dst_lock, FMODE_WRITE);
Peng Taoe5341f32015-09-26 02:24:35 +08001002 if (status)
1003 return status;
1004
1005 res.dst_fattr = nfs_alloc_fattr();
1006 if (!res.dst_fattr)
1007 return -ENOMEM;
1008
1009 status = nfs4_call_sync(server->client, server, msg,
1010 &args.seq_args, &res.seq_res, 0);
1011 if (status == 0)
1012 status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
1013
1014 kfree(res.dst_fattr);
1015 return status;
1016}
1017
1018int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
1019 loff_t src_offset, loff_t dst_offset, loff_t count)
1020{
1021 struct rpc_message msg = {
1022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
1023 };
1024 struct inode *inode = file_inode(src_f);
1025 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001026 struct nfs_lock_context *src_lock;
1027 struct nfs_lock_context *dst_lock;
1028 struct nfs4_exception src_exception = { };
1029 struct nfs4_exception dst_exception = { };
1030 int err, err2;
Peng Taoe5341f32015-09-26 02:24:35 +08001031
1032 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
1033 return -EOPNOTSUPP;
1034
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001035 src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
1036 if (IS_ERR(src_lock))
1037 return PTR_ERR(src_lock);
1038
1039 src_exception.inode = file_inode(src_f);
1040 src_exception.state = src_lock->open_context->state;
1041
1042 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
1043 if (IS_ERR(dst_lock)) {
1044 err = PTR_ERR(dst_lock);
1045 goto out_put_src_lock;
1046 }
1047
1048 dst_exception.inode = file_inode(dst_f);
1049 dst_exception.state = dst_lock->open_context->state;
1050
Peng Taoe5341f32015-09-26 02:24:35 +08001051 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001052 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
1053 src_offset, dst_offset, count);
Peng Taoe5341f32015-09-26 02:24:35 +08001054 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
1055 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001056 err = -EOPNOTSUPP;
1057 break;
Peng Taoe5341f32015-09-26 02:24:35 +08001058 }
Peng Taoe5341f32015-09-26 02:24:35 +08001059
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001060 err2 = nfs4_handle_exception(server, err, &src_exception);
1061 err = nfs4_handle_exception(server, err, &dst_exception);
1062 if (!err)
1063 err = err2;
1064 } while (src_exception.retry || dst_exception.retry);
1065
1066 nfs_put_lock_context(dst_lock);
1067out_put_src_lock:
1068 nfs_put_lock_context(src_lock);
Peng Taoe5341f32015-09-26 02:24:35 +08001069 return err;
Peng Taoe5341f32015-09-26 02:24:35 +08001070}