blob: e2ae54b35dfe1ff5da1bd1d656327b7b245e2cda [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 Schumaker913eca12019-11-12 16:37:24 -050052 .falloc_bitmask = nfs4_fattr_bitmap,
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);
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -050064 if (status) {
65 if (status == -EAGAIN)
66 status = -NFS4ERR_BAD_STATEID;
Anna Schumakerf4ac1672014-11-25 13:18:15 -050067 return status;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -050068 }
Anna Schumakerf4ac1672014-11-25 13:18:15 -050069
Anna Schumaker9a519402015-03-16 14:06:23 -040070 res.falloc_fattr = nfs_alloc_fattr();
71 if (!res.falloc_fattr)
72 return -ENOMEM;
73
74 status = nfs4_call_sync(server->client, server, msg,
75 &args.seq_args, &res.seq_res, 0);
76 if (status == 0)
77 status = nfs_post_op_update_inode(inode, res.falloc_fattr);
78
79 kfree(res.falloc_fattr);
80 return status;
Anna Schumakerf4ac1672014-11-25 13:18:15 -050081}
82
83static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
84 loff_t offset, loff_t len)
85{
86 struct nfs_server *server = NFS_SERVER(file_inode(filep));
87 struct nfs4_exception exception = { };
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010088 struct nfs_lock_context *lock;
Anna Schumakerf4ac1672014-11-25 13:18:15 -050089 int err;
90
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010091 lock = nfs_get_lock_context(nfs_file_open_context(filep));
92 if (IS_ERR(lock))
93 return PTR_ERR(lock);
94
95 exception.inode = file_inode(filep);
96 exception.state = lock->open_context->state;
97
Anna Schumakerf4ac1672014-11-25 13:18:15 -050098 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +010099 err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
100 if (err == -ENOTSUPP) {
101 err = -EOPNOTSUPP;
102 break;
103 }
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500104 err = nfs4_handle_exception(server, err, &exception);
105 } while (exception.retry);
106
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100107 nfs_put_lock_context(lock);
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500108 return err;
109}
110
111int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
112{
113 struct rpc_message msg = {
114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
115 };
116 struct inode *inode = file_inode(filep);
117 int err;
118
119 if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
120 return -EOPNOTSUPP;
121
Al Viro59551022016-01-22 15:40:57 -0500122 inode_lock(inode);
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400123
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500124 err = nfs42_proc_fallocate(&msg, filep, offset, len);
125 if (err == -EOPNOTSUPP)
126 NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400127
Al Viro59551022016-01-22 15:40:57 -0500128 inode_unlock(inode);
Anna Schumakerf4ac1672014-11-25 13:18:15 -0500129 return err;
130}
131
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500132int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
133{
134 struct rpc_message msg = {
135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
136 };
137 struct inode *inode = file_inode(filep);
138 int err;
139
140 if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
141 return -EOPNOTSUPP;
142
Al Viro59551022016-01-22 15:40:57 -0500143 inode_lock(inode);
Trond Myklebust1e564d32016-06-25 17:50:53 -0400144 err = nfs_sync_inode(inode);
145 if (err)
146 goto out_unlock;
Anna Schumakerf830f7d2015-03-16 14:06:24 -0400147
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500148 err = nfs42_proc_fallocate(&msg, filep, offset, len);
Anna Schumaker9a519402015-03-16 14:06:23 -0400149 if (err == 0)
150 truncate_pagecache_range(inode, offset, (offset + len) -1);
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500151 if (err == -EOPNOTSUPP)
152 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
Trond Myklebust1e564d32016-06-25 17:50:53 -0400153out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500154 inode_unlock(inode);
Anna Schumaker624bd5b2014-11-25 13:18:16 -0500155 return err;
156}
157
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400158static int handle_async_copy(struct nfs42_copy_res *res,
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400159 struct nfs_server *dst_server,
160 struct nfs_server *src_server,
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400161 struct file *src,
162 struct file *dst,
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400163 nfs4_stateid *src_stateid,
164 bool *restart)
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400165{
Olga Kornievskaia99f2c552018-11-21 11:24:22 -0500166 struct nfs4_copy_state *copy, *tmp_copy;
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400167 int status = NFS4_OK;
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400168 bool found_pending = false;
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400169 struct nfs_open_context *dst_ctx = nfs_file_open_context(dst);
170 struct nfs_open_context *src_ctx = nfs_file_open_context(src);
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400171
Olga Kornievskaia99f2c552018-11-21 11:24:22 -0500172 copy = kzalloc(sizeof(struct nfs4_copy_state), GFP_NOFS);
173 if (!copy)
174 return -ENOMEM;
175
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400176 spin_lock(&dst_server->nfs_client->cl_lock);
177 list_for_each_entry(tmp_copy,
178 &dst_server->nfs_client->pending_cb_stateids,
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400179 copies) {
Olga Kornievskaia99f2c552018-11-21 11:24:22 -0500180 if (memcmp(&res->write_res.stateid, &tmp_copy->stateid,
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400181 NFS4_STATEID_SIZE))
182 continue;
183 found_pending = true;
Olga Kornievskaia99f2c552018-11-21 11:24:22 -0500184 list_del(&tmp_copy->copies);
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400185 break;
186 }
187 if (found_pending) {
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400188 spin_unlock(&dst_server->nfs_client->cl_lock);
Olga Kornievskaia99f2c552018-11-21 11:24:22 -0500189 kfree(copy);
190 copy = tmp_copy;
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400191 goto out;
192 }
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400193
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400194 memcpy(&copy->stateid, &res->write_res.stateid, NFS4_STATEID_SIZE);
195 init_completion(&copy->completion);
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400196 copy->parent_dst_state = dst_ctx->state;
197 copy->parent_src_state = src_ctx->state;
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400198
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400199 list_add_tail(&copy->copies, &dst_server->ss_copies);
200 spin_unlock(&dst_server->nfs_client->cl_lock);
201
202 if (dst_server != src_server) {
203 spin_lock(&src_server->nfs_client->cl_lock);
204 list_add_tail(&copy->src_copies, &src_server->ss_copies);
205 spin_unlock(&src_server->nfs_client->cl_lock);
206 }
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400207
Olga Kornievskaiac975c202018-07-09 15:13:34 -0400208 status = wait_for_completion_interruptible(&copy->completion);
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400209 spin_lock(&dst_server->nfs_client->cl_lock);
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400210 list_del_init(&copy->copies);
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400211 spin_unlock(&dst_server->nfs_client->cl_lock);
212 if (dst_server != src_server) {
213 spin_lock(&src_server->nfs_client->cl_lock);
214 list_del_init(&copy->src_copies);
215 spin_unlock(&src_server->nfs_client->cl_lock);
216 }
Olga Kornievskaiac975c202018-07-09 15:13:34 -0400217 if (status == -ERESTARTSYS) {
Olga Kornievskaiae4648aa2018-08-13 15:33:01 -0400218 goto out_cancel;
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400219 } else if (copy->flags || copy->error == NFS4ERR_PARTNER_NO_AUTH) {
Olga Kornievskaiae4648aa2018-08-13 15:33:01 -0400220 status = -EAGAIN;
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400221 *restart = true;
Olga Kornievskaiae4648aa2018-08-13 15:33:01 -0400222 goto out_cancel;
Olga Kornievskaiac975c202018-07-09 15:13:34 -0400223 }
Olga Kornievskaiabc0c9072018-07-09 15:13:32 -0400224out:
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400225 res->write_res.count = copy->count;
226 memcpy(&res->write_res.verifier, &copy->verf, sizeof(copy->verf));
227 status = -copy->error;
228
Olga Kornievskaia12406022019-07-02 14:57:25 -0400229out_free:
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400230 kfree(copy);
231 return status;
Olga Kornievskaiae4648aa2018-08-13 15:33:01 -0400232out_cancel:
233 nfs42_do_offload_cancel_async(dst, &copy->stateid);
Olga Kornievskaia12406022019-07-02 14:57:25 -0400234 if (!nfs42_files_from_same_server(src, dst))
235 nfs42_do_offload_cancel_async(src, src_stateid);
236 goto out_free;
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400237}
238
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -0400239static int process_copy_commit(struct file *dst, loff_t pos_dst,
240 struct nfs42_copy_res *res)
241{
242 struct nfs_commitres cres;
243 int status = -ENOMEM;
244
245 cres.verf = kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS);
246 if (!cres.verf)
247 goto out;
248
249 status = nfs4_proc_commit(dst, pos_dst, res->write_res.count, &cres);
250 if (status)
251 goto out_free;
252 if (nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
253 &cres.verf->verifier)) {
254 dprintk("commit verf differs from copy verf\n");
255 status = -EAGAIN;
256 }
257out_free:
258 kfree(cres.verf);
259out:
260 return status;
261}
262
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500263static ssize_t _nfs42_proc_copy(struct file *src,
Anna Schumaker2e724482013-05-21 16:53:03 -0400264 struct nfs_lock_context *src_lock,
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500265 struct file *dst,
Anna Schumaker2e724482013-05-21 16:53:03 -0400266 struct nfs_lock_context *dst_lock,
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500267 struct nfs42_copy_args *args,
Olga Kornievskaia1d38f3f2019-06-04 11:54:18 -0400268 struct nfs42_copy_res *res,
269 struct nl4_server *nss,
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400270 nfs4_stateid *cnr_stateid,
271 bool *restart)
Anna Schumaker2e724482013-05-21 16:53:03 -0400272{
Anna Schumaker2e724482013-05-21 16:53:03 -0400273 struct rpc_message msg = {
274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500275 .rpc_argp = args,
276 .rpc_resp = res,
Anna Schumaker2e724482013-05-21 16:53:03 -0400277 };
278 struct inode *dst_inode = file_inode(dst);
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400279 struct inode *src_inode = file_inode(src);
280 struct nfs_server *dst_server = NFS_SERVER(dst_inode);
281 struct nfs_server *src_server = NFS_SERVER(src_inode);
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500282 loff_t pos_src = args->src_pos;
283 loff_t pos_dst = args->dst_pos;
284 size_t count = args->count;
Olga Kornievskaia1ee48bd2017-07-06 09:43:02 -0400285 ssize_t status;
Anna Schumaker2e724482013-05-21 16:53:03 -0400286
Olga Kornievskaia1d38f3f2019-06-04 11:54:18 -0400287 if (nss) {
288 args->cp_src = nss;
289 nfs4_stateid_copy(&args->src_stateid, cnr_stateid);
290 } else {
291 status = nfs4_set_rw_stateid(&args->src_stateid,
292 src_lock->open_context, src_lock, FMODE_READ);
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -0500293 if (status) {
294 if (status == -EAGAIN)
295 status = -NFS4ERR_BAD_STATEID;
Olga Kornievskaia1d38f3f2019-06-04 11:54:18 -0400296 return status;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -0500297 }
Olga Kornievskaia1d38f3f2019-06-04 11:54:18 -0400298 }
Trond Myklebust837bb1d2016-06-25 18:12:03 -0400299 status = nfs_filemap_write_and_wait_range(file_inode(src)->i_mapping,
300 pos_src, pos_src + (loff_t)count - 1);
301 if (status)
302 return status;
303
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500304 status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context,
Anna Schumaker2e724482013-05-21 16:53:03 -0400305 dst_lock, FMODE_WRITE);
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -0500306 if (status) {
307 if (status == -EAGAIN)
308 status = -NFS4ERR_BAD_STATEID;
Anna Schumaker2e724482013-05-21 16:53:03 -0400309 return status;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -0500310 }
Anna Schumaker2e724482013-05-21 16:53:03 -0400311
Trond Myklebust837bb1d2016-06-25 18:12:03 -0400312 status = nfs_sync_inode(dst_inode);
313 if (status)
314 return status;
315
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400316 res->commit_res.verf = NULL;
317 if (args->sync) {
318 res->commit_res.verf =
319 kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS);
320 if (!res->commit_res.verf)
321 return -ENOMEM;
322 }
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400323 set_bit(NFS_CLNT_SRC_SSC_COPY_STATE,
324 &src_lock->open_context->state->flags);
Olga Kornievskaiae4648aa2018-08-13 15:33:01 -0400325 set_bit(NFS_CLNT_DST_SSC_COPY_STATE,
326 &dst_lock->open_context->state->flags);
327
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400328 status = nfs4_call_sync(dst_server->client, dst_server, &msg,
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500329 &args->seq_args, &res->seq_res, 0);
Anna Schumaker2e724482013-05-21 16:53:03 -0400330 if (status == -ENOTSUPP)
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400331 dst_server->caps &= ~NFS_CAP_COPY;
Anna Schumaker2e724482013-05-21 16:53:03 -0400332 if (status)
Olga Kornievskaiae0926932017-05-08 18:02:24 -0400333 goto out;
Anna Schumaker2e724482013-05-21 16:53:03 -0400334
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400335 if (args->sync &&
336 nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
Olga Kornievskaiae0926932017-05-08 18:02:24 -0400337 &res->commit_res.verf->verifier)) {
338 status = -EAGAIN;
339 goto out;
Anna Schumaker2e724482013-05-21 16:53:03 -0400340 }
341
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400342 if (!res->synchronous) {
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400343 status = handle_async_copy(res, dst_server, src_server, src,
344 dst, &args->src_stateid, restart);
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400345 if (status)
Wenwen Wang123c23c2020-02-03 03:47:53 +0000346 goto out;
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400347 }
348
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -0400349 if ((!res->synchronous || !args->sync) &&
350 res->write_res.verifier.committed != NFS_FILE_SYNC) {
351 status = process_copy_commit(dst, pos_dst, res);
352 if (status)
Wenwen Wang123c23c2020-02-03 03:47:53 +0000353 goto out;
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -0400354 }
355
Anna Schumaker2e724482013-05-21 16:53:03 -0400356 truncate_pagecache_range(dst_inode, pos_dst,
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500357 pos_dst + res->write_res.count);
Anna Schumaker2e724482013-05-21 16:53:03 -0400358
Olga Kornievskaiae0926932017-05-08 18:02:24 -0400359 status = res->write_res.count;
360out:
Olga Kornievskaia62164f32018-07-09 15:13:31 -0400361 if (args->sync)
362 kfree(res->commit_res.verf);
Olga Kornievskaiae0926932017-05-08 18:02:24 -0400363 return status;
Anna Schumaker2e724482013-05-21 16:53:03 -0400364}
365
366ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
Olga Kornievskaia1d38f3f2019-06-04 11:54:18 -0400367 struct file *dst, loff_t pos_dst, size_t count,
368 struct nl4_server *nss,
Olga Kornievskaia12751012019-07-03 10:38:02 -0400369 nfs4_stateid *cnr_stateid, bool sync)
Anna Schumaker2e724482013-05-21 16:53:03 -0400370{
371 struct nfs_server *server = NFS_SERVER(file_inode(dst));
372 struct nfs_lock_context *src_lock;
373 struct nfs_lock_context *dst_lock;
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500374 struct nfs42_copy_args args = {
375 .src_fh = NFS_FH(file_inode(src)),
376 .src_pos = pos_src,
377 .dst_fh = NFS_FH(file_inode(dst)),
378 .dst_pos = pos_dst,
379 .count = count,
Olga Kornievskaia12751012019-07-03 10:38:02 -0400380 .sync = sync,
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500381 };
382 struct nfs42_copy_res res;
383 struct nfs4_exception src_exception = {
384 .inode = file_inode(src),
385 .stateid = &args.src_stateid,
386 };
387 struct nfs4_exception dst_exception = {
388 .inode = file_inode(dst),
389 .stateid = &args.dst_stateid,
390 };
Anna Schumaker2e724482013-05-21 16:53:03 -0400391 ssize_t err, err2;
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400392 bool restart = false;
Anna Schumaker2e724482013-05-21 16:53:03 -0400393
Anna Schumaker2e724482013-05-21 16:53:03 -0400394 src_lock = nfs_get_lock_context(nfs_file_open_context(src));
395 if (IS_ERR(src_lock))
396 return PTR_ERR(src_lock);
397
Anna Schumaker2e724482013-05-21 16:53:03 -0400398 src_exception.state = src_lock->open_context->state;
399
400 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst));
401 if (IS_ERR(dst_lock)) {
402 err = PTR_ERR(dst_lock);
403 goto out_put_src_lock;
404 }
405
Anna Schumaker2e724482013-05-21 16:53:03 -0400406 dst_exception.state = dst_lock->open_context->state;
407
408 do {
Linus Torvaldsea8ea732016-05-26 10:33:33 -0700409 inode_lock(file_inode(dst));
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500410 err = _nfs42_proc_copy(src, src_lock,
411 dst, dst_lock,
Olga Kornievskaia1d38f3f2019-06-04 11:54:18 -0400412 &args, &res,
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400413 nss, cnr_stateid, &restart);
Linus Torvaldsea8ea732016-05-26 10:33:33 -0700414 inode_unlock(file_inode(dst));
Anna Schumaker2e724482013-05-21 16:53:03 -0400415
Trond Myklebust9d8cacb2017-02-17 18:42:32 -0500416 if (err >= 0)
417 break;
Olga Kornievskaia12406022019-07-02 14:57:25 -0400418 if (err == -ENOTSUPP &&
419 nfs42_files_from_same_server(src, dst)) {
Anna Schumaker2e724482013-05-21 16:53:03 -0400420 err = -EOPNOTSUPP;
421 break;
Olga Kornievskaia539f57b2018-07-09 15:13:35 -0400422 } else if (err == -EAGAIN) {
Olga Kornievskaia0e65a322019-06-14 14:38:40 -0400423 if (!restart) {
424 dst_exception.retry = 1;
425 continue;
426 }
427 break;
Olga Kornievskaia539f57b2018-07-09 15:13:35 -0400428 } else if (err == -NFS4ERR_OFFLOAD_NO_REQS && !args.sync) {
429 args.sync = true;
Olga Kornievskaiae0926932017-05-08 18:02:24 -0400430 dst_exception.retry = 1;
431 continue;
Olga Kornievskaia6b61c962017-09-26 13:51:39 -0400432 } else if ((err == -ESTALE ||
Olga Kornievskaia12406022019-07-02 14:57:25 -0400433 err == -NFS4ERR_OFFLOAD_DENIED ||
434 err == -ENOTSUPP) &&
Olga Kornievskaia7e350192017-09-25 15:59:44 -0400435 !nfs42_files_from_same_server(src, dst)) {
436 nfs42_do_offload_cancel_async(src, &args.src_stateid);
437 err = -EOPNOTSUPP;
438 break;
Anna Schumaker2e724482013-05-21 16:53:03 -0400439 }
440
441 err2 = nfs4_handle_exception(server, err, &src_exception);
442 err = nfs4_handle_exception(server, err, &dst_exception);
443 if (!err)
444 err = err2;
445 } while (src_exception.retry || dst_exception.retry);
446
447 nfs_put_lock_context(dst_lock);
448out_put_src_lock:
449 nfs_put_lock_context(src_lock);
450 return err;
451}
452
Olga Kornievskaiac975c202018-07-09 15:13:34 -0400453struct nfs42_offloadcancel_data {
454 struct nfs_server *seq_server;
455 struct nfs42_offload_status_args args;
456 struct nfs42_offload_status_res res;
457};
458
459static void nfs42_offload_cancel_prepare(struct rpc_task *task, void *calldata)
460{
461 struct nfs42_offloadcancel_data *data = calldata;
462
463 nfs4_setup_sequence(data->seq_server->nfs_client,
464 &data->args.osa_seq_args,
465 &data->res.osr_seq_res, task);
466}
467
468static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata)
469{
470 struct nfs42_offloadcancel_data *data = calldata;
471
472 nfs41_sequence_done(task, &data->res.osr_seq_res);
473 if (task->tk_status &&
474 nfs4_async_handle_error(task, data->seq_server, NULL,
475 NULL) == -EAGAIN)
476 rpc_restart_call_prepare(task);
477}
478
479static void nfs42_free_offloadcancel_data(void *data)
480{
481 kfree(data);
482}
483
484static const struct rpc_call_ops nfs42_offload_cancel_ops = {
485 .rpc_call_prepare = nfs42_offload_cancel_prepare,
486 .rpc_call_done = nfs42_offload_cancel_done,
487 .rpc_release = nfs42_free_offloadcancel_data,
488};
489
490static int nfs42_do_offload_cancel_async(struct file *dst,
491 nfs4_stateid *stateid)
492{
493 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
494 struct nfs42_offloadcancel_data *data = NULL;
495 struct nfs_open_context *ctx = nfs_file_open_context(dst);
496 struct rpc_task *task;
497 struct rpc_message msg = {
498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_CANCEL],
499 .rpc_cred = ctx->cred,
500 };
501 struct rpc_task_setup task_setup_data = {
502 .rpc_client = dst_server->client,
503 .rpc_message = &msg,
504 .callback_ops = &nfs42_offload_cancel_ops,
505 .workqueue = nfsiod_workqueue,
506 .flags = RPC_TASK_ASYNC,
507 };
508 int status;
509
510 if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL))
511 return -EOPNOTSUPP;
512
513 data = kzalloc(sizeof(struct nfs42_offloadcancel_data), GFP_NOFS);
514 if (data == NULL)
515 return -ENOMEM;
516
517 data->seq_server = dst_server;
518 data->args.osa_src_fh = NFS_FH(file_inode(dst));
519 memcpy(&data->args.osa_stateid, stateid,
520 sizeof(data->args.osa_stateid));
521 msg.rpc_argp = &data->args;
522 msg.rpc_resp = &data->res;
523 task_setup_data.callback_data = data;
524 nfs4_init_sequence(&data->args.osa_seq_args, &data->res.osr_seq_res,
525 1, 0);
526 task = rpc_run_task(&task_setup_data);
527 if (IS_ERR(task))
528 return PTR_ERR(task);
529 status = rpc_wait_for_completion_task(task);
530 if (status == -ENOTSUPP)
531 dst_server->caps &= ~NFS_CAP_OFFLOAD_CANCEL;
532 rpc_put_task(task);
533 return status;
534}
535
YueHaibing00030102019-11-14 22:01:41 +0800536static int _nfs42_proc_copy_notify(struct file *src, struct file *dst,
537 struct nfs42_copy_notify_args *args,
538 struct nfs42_copy_notify_res *res)
Olga Kornievskaia04915672019-06-04 16:14:30 -0400539{
540 struct nfs_server *src_server = NFS_SERVER(file_inode(src));
541 struct rpc_message msg = {
542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY_NOTIFY],
543 .rpc_argp = args,
544 .rpc_resp = res,
545 };
546 int status;
547 struct nfs_open_context *ctx;
548 struct nfs_lock_context *l_ctx;
549
550 ctx = get_nfs_open_context(nfs_file_open_context(src));
551 l_ctx = nfs_get_lock_context(ctx);
552 if (IS_ERR(l_ctx))
553 return PTR_ERR(l_ctx);
554
555 status = nfs4_set_rw_stateid(&args->cna_src_stateid, ctx, l_ctx,
556 FMODE_READ);
557 nfs_put_lock_context(l_ctx);
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -0500558 if (status) {
559 if (status == -EAGAIN)
560 status = -NFS4ERR_BAD_STATEID;
Olga Kornievskaia04915672019-06-04 16:14:30 -0400561 return status;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -0500562 }
Olga Kornievskaia04915672019-06-04 16:14:30 -0400563
564 status = nfs4_call_sync(src_server->client, src_server, &msg,
565 &args->cna_seq_args, &res->cnr_seq_res, 0);
566 if (status == -ENOTSUPP)
567 src_server->caps &= ~NFS_CAP_COPY_NOTIFY;
568
569 put_nfs_open_context(nfs_file_open_context(src));
570 return status;
571}
572
573int nfs42_proc_copy_notify(struct file *src, struct file *dst,
574 struct nfs42_copy_notify_res *res)
575{
576 struct nfs_server *src_server = NFS_SERVER(file_inode(src));
577 struct nfs42_copy_notify_args *args;
578 struct nfs4_exception exception = {
579 .inode = file_inode(src),
580 };
581 int status;
582
583 if (!(src_server->caps & NFS_CAP_COPY_NOTIFY))
584 return -EOPNOTSUPP;
585
586 args = kzalloc(sizeof(struct nfs42_copy_notify_args), GFP_NOFS);
587 if (args == NULL)
588 return -ENOMEM;
589
590 args->cna_src_fh = NFS_FH(file_inode(src)),
591 args->cna_dst.nl4_type = NL4_NETADDR;
592 nfs42_set_netaddr(dst, &args->cna_dst.u.nl4_addr);
593 exception.stateid = &args->cna_src_stateid;
594
595 do {
596 status = _nfs42_proc_copy_notify(src, dst, args, res);
597 if (status == -ENOTSUPP) {
598 status = -EOPNOTSUPP;
599 goto out;
600 }
601 status = nfs4_handle_exception(src_server, status, &exception);
602 } while (exception.retry);
603
604out:
605 kfree(args);
606 return status;
607}
608
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100609static loff_t _nfs42_proc_llseek(struct file *filep,
610 struct nfs_lock_context *lock, loff_t offset, int whence)
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400611{
612 struct inode *inode = file_inode(filep);
613 struct nfs42_seek_args args = {
614 .sa_fh = NFS_FH(inode),
615 .sa_offset = offset,
616 .sa_what = (whence == SEEK_HOLE) ?
617 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
618 };
619 struct nfs42_seek_res res;
620 struct rpc_message msg = {
621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
622 .rpc_argp = &args,
623 .rpc_resp = &res,
624 };
625 struct nfs_server *server = NFS_SERVER(inode);
626 int status;
627
Anna Schumaker878ffa92014-10-23 14:00:54 -0400628 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400629 return -ENOTSUPP;
630
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100631 status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
632 lock, FMODE_READ);
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -0500633 if (status) {
634 if (status == -EAGAIN)
635 status = -NFS4ERR_BAD_STATEID;
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400636 return status;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -0500637 }
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400638
Trond Myklebuste95fc4a2016-06-25 17:57:39 -0400639 status = nfs_filemap_write_and_wait_range(inode->i_mapping,
640 offset, LLONG_MAX);
641 if (status)
642 return status;
643
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400644 status = nfs4_call_sync(server->client, server, &msg,
645 &args.seq_args, &res.seq_res, 0);
646 if (status == -ENOTSUPP)
647 server->caps &= ~NFS_CAP_SEEK;
648 if (status)
649 return status;
650
651 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
652}
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800653
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400654loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
655{
656 struct nfs_server *server = NFS_SERVER(file_inode(filep));
657 struct nfs4_exception exception = { };
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100658 struct nfs_lock_context *lock;
J. Bruce Fields306a5542015-09-16 17:21:27 -0400659 loff_t err;
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400660
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100661 lock = nfs_get_lock_context(nfs_file_open_context(filep));
662 if (IS_ERR(lock))
663 return PTR_ERR(lock);
664
665 exception.inode = file_inode(filep);
666 exception.state = lock->open_context->state;
667
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400668 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100669 err = _nfs42_proc_llseek(filep, lock, offset, whence);
J. Bruce Fields306a5542015-09-16 17:21:27 -0400670 if (err >= 0)
671 break;
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100672 if (err == -ENOTSUPP) {
673 err = -EOPNOTSUPP;
674 break;
675 }
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400676 err = nfs4_handle_exception(server, err, &exception);
677 } while (exception.retry);
678
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100679 nfs_put_lock_context(lock);
J. Bruce Fieldsbdcc2cd2015-07-23 11:08:43 -0400680 return err;
681}
682
683
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800684static void
685nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
686{
687 struct nfs42_layoutstat_data *data = calldata;
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400688 struct inode *inode = data->inode;
689 struct nfs_server *server = NFS_SERVER(inode);
690 struct pnfs_layout_hdr *lo;
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800691
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400692 spin_lock(&inode->i_lock);
693 lo = NFS_I(inode)->layout;
694 if (!pnfs_layout_is_valid(lo)) {
695 spin_unlock(&inode->i_lock);
696 rpc_exit(task, 0);
697 return;
698 }
699 nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
700 spin_unlock(&inode->i_lock);
Anna Schumaker6de7e122017-01-09 16:51:52 -0500701 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
702 &data->res.seq_res, task);
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800703}
704
705static void
706nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
707{
708 struct nfs42_layoutstat_data *data = calldata;
Peng Tao68d264c2015-12-06 20:55:09 -0500709 struct inode *inode = data->inode;
710 struct pnfs_layout_hdr *lo;
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800711
712 if (!nfs4_sequence_done(task, &data->res.seq_res))
713 return;
714
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400715 switch (task->tk_status) {
716 case 0:
717 break;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400718 case -NFS4ERR_BADHANDLE:
719 case -ESTALE:
720 pnfs_destroy_layout(NFS_I(inode));
721 break;
Peng Tao68d264c2015-12-06 20:55:09 -0500722 case -NFS4ERR_EXPIRED:
Trond Myklebust206b3bb2016-08-05 12:16:19 -0400723 case -NFS4ERR_ADMIN_REVOKED:
724 case -NFS4ERR_DELEG_REVOKED:
Peng Tao68d264c2015-12-06 20:55:09 -0500725 case -NFS4ERR_STALE_STATEID:
Peng Tao68d264c2015-12-06 20:55:09 -0500726 case -NFS4ERR_BAD_STATEID:
727 spin_lock(&inode->i_lock);
728 lo = NFS_I(inode)->layout;
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400729 if (pnfs_layout_is_valid(lo) &&
730 nfs4_stateid_match(&data->args.stateid,
Peng Tao68d264c2015-12-06 20:55:09 -0500731 &lo->plh_stateid)) {
732 LIST_HEAD(head);
733
734 /*
735 * Mark the bad layout state as invalid, then retry
736 * with the current stateid.
737 */
Trond Myklebust5f46be02016-07-22 11:25:27 -0400738 pnfs_mark_layout_stateid_invalid(lo, &head);
Peng Tao68d264c2015-12-06 20:55:09 -0500739 spin_unlock(&inode->i_lock);
740 pnfs_free_lseg_list(&head);
Trond Myklebust1f18b822017-04-29 10:10:17 -0400741 nfs_commit_inode(inode, 0);
Peng Tao68d264c2015-12-06 20:55:09 -0500742 } else
743 spin_unlock(&inode->i_lock);
744 break;
Trond Myklebust9a0fe862016-08-19 15:33:12 -0400745 case -NFS4ERR_OLD_STATEID:
746 spin_lock(&inode->i_lock);
747 lo = NFS_I(inode)->layout;
748 if (pnfs_layout_is_valid(lo) &&
749 nfs4_stateid_match_other(&data->args.stateid,
750 &lo->plh_stateid)) {
751 /* Do we need to delay before resending? */
752 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
753 &data->args.stateid))
754 rpc_delay(task, HZ);
755 rpc_restart_call_prepare(task);
756 }
757 spin_unlock(&inode->i_lock);
758 break;
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400759 case -ENOTSUPP:
760 case -EOPNOTSUPP:
Peng Tao68d264c2015-12-06 20:55:09 -0500761 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
Trond Myklebust6c5a0d82015-06-27 11:45:46 -0400762 }
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800763}
764
765static void
766nfs42_layoutstat_release(void *calldata)
767{
768 struct nfs42_layoutstat_data *data = calldata;
Trond Myklebust422c93c2016-10-06 17:53:20 -0400769 struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo;
770 int i;
Peng Tao87334082015-06-23 19:51:57 +0800771
Trond Myklebust422c93c2016-10-06 17:53:20 -0400772 for (i = 0; i < data->args.num_dev; i++) {
773 if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free)
774 devinfo[i].ld_private.ops->free(&devinfo[i].ld_private);
775 }
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800776
777 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
Peng Tao1bfe3b22015-06-23 19:52:03 +0800778 smp_mb__before_atomic();
779 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
780 smp_mb__after_atomic();
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800781 nfs_iput_and_deactive(data->inode);
782 kfree(data->args.devinfo);
783 kfree(data);
784}
785
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800786static const struct rpc_call_ops nfs42_layoutstat_ops = {
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800787 .rpc_call_prepare = nfs42_layoutstat_prepare,
788 .rpc_call_done = nfs42_layoutstat_done,
789 .rpc_release = nfs42_layoutstat_release,
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800790};
791
792int nfs42_proc_layoutstats_generic(struct nfs_server *server,
793 struct nfs42_layoutstat_data *data)
794{
795 struct rpc_message msg = {
796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
797 .rpc_argp = &data->args,
798 .rpc_resp = &data->res,
799 };
800 struct rpc_task_setup task_setup = {
801 .rpc_client = server->client,
802 .rpc_message = &msg,
803 .callback_ops = &nfs42_layoutstat_ops,
804 .callback_data = data,
805 .flags = RPC_TASK_ASYNC,
806 };
807 struct rpc_task *task;
808
Peng Tao1b4a4bd2015-06-23 19:51:56 +0800809 data->inode = nfs_igrab_and_active(data->args.inode);
810 if (!data->inode) {
811 nfs42_layoutstat_release(data);
812 return -EAGAIN;
813 }
Anna Schumakerfba83f32018-05-04 16:22:50 -0400814 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800815 task = rpc_run_task(&task_setup);
816 if (IS_ERR(task))
817 return PTR_ERR(task);
Jeff Layton3f807e52016-10-04 00:07:43 -0400818 rpc_put_task(task);
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800819 return 0;
820}
Peng Taoe5341f32015-09-26 02:24:35 +0800821
Trond Myklebust3eb86092019-02-08 10:31:05 -0500822static struct nfs42_layouterror_data *
823nfs42_alloc_layouterror_data(struct pnfs_layout_segment *lseg, gfp_t gfp_flags)
824{
825 struct nfs42_layouterror_data *data;
826 struct inode *inode = lseg->pls_layout->plh_inode;
827
828 data = kzalloc(sizeof(*data), gfp_flags);
829 if (data) {
830 data->args.inode = data->inode = nfs_igrab_and_active(inode);
831 if (data->inode) {
832 data->lseg = pnfs_get_lseg(lseg);
833 if (data->lseg)
834 return data;
835 nfs_iput_and_deactive(data->inode);
836 }
837 kfree(data);
838 }
839 return NULL;
840}
841
842static void
843nfs42_free_layouterror_data(struct nfs42_layouterror_data *data)
844{
845 pnfs_put_lseg(data->lseg);
846 nfs_iput_and_deactive(data->inode);
847 kfree(data);
848}
849
850static void
851nfs42_layouterror_prepare(struct rpc_task *task, void *calldata)
852{
853 struct nfs42_layouterror_data *data = calldata;
854 struct inode *inode = data->inode;
855 struct nfs_server *server = NFS_SERVER(inode);
856 struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
857 unsigned i;
858
859 spin_lock(&inode->i_lock);
860 if (!pnfs_layout_is_valid(lo)) {
861 spin_unlock(&inode->i_lock);
862 rpc_exit(task, 0);
863 return;
864 }
865 for (i = 0; i < data->args.num_errors; i++)
866 nfs4_stateid_copy(&data->args.errors[i].stateid,
867 &lo->plh_stateid);
868 spin_unlock(&inode->i_lock);
869 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
870 &data->res.seq_res, task);
871}
872
873static void
874nfs42_layouterror_done(struct rpc_task *task, void *calldata)
875{
876 struct nfs42_layouterror_data *data = calldata;
877 struct inode *inode = data->inode;
878 struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
879
880 if (!nfs4_sequence_done(task, &data->res.seq_res))
881 return;
882
883 switch (task->tk_status) {
884 case 0:
885 break;
886 case -NFS4ERR_BADHANDLE:
887 case -ESTALE:
888 pnfs_destroy_layout(NFS_I(inode));
889 break;
890 case -NFS4ERR_EXPIRED:
891 case -NFS4ERR_ADMIN_REVOKED:
892 case -NFS4ERR_DELEG_REVOKED:
893 case -NFS4ERR_STALE_STATEID:
894 case -NFS4ERR_BAD_STATEID:
895 spin_lock(&inode->i_lock);
896 if (pnfs_layout_is_valid(lo) &&
897 nfs4_stateid_match(&data->args.errors[0].stateid,
898 &lo->plh_stateid)) {
899 LIST_HEAD(head);
900
901 /*
902 * Mark the bad layout state as invalid, then retry
903 * with the current stateid.
904 */
905 pnfs_mark_layout_stateid_invalid(lo, &head);
906 spin_unlock(&inode->i_lock);
907 pnfs_free_lseg_list(&head);
908 nfs_commit_inode(inode, 0);
909 } else
910 spin_unlock(&inode->i_lock);
911 break;
912 case -NFS4ERR_OLD_STATEID:
913 spin_lock(&inode->i_lock);
914 if (pnfs_layout_is_valid(lo) &&
915 nfs4_stateid_match_other(&data->args.errors[0].stateid,
916 &lo->plh_stateid)) {
917 /* Do we need to delay before resending? */
918 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
919 &data->args.errors[0].stateid))
920 rpc_delay(task, HZ);
921 rpc_restart_call_prepare(task);
922 }
923 spin_unlock(&inode->i_lock);
924 break;
925 case -ENOTSUPP:
926 case -EOPNOTSUPP:
927 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTERROR;
928 }
929}
930
931static void
932nfs42_layouterror_release(void *calldata)
933{
934 struct nfs42_layouterror_data *data = calldata;
935
936 nfs42_free_layouterror_data(data);
937}
938
939static const struct rpc_call_ops nfs42_layouterror_ops = {
940 .rpc_call_prepare = nfs42_layouterror_prepare,
941 .rpc_call_done = nfs42_layouterror_done,
942 .rpc_release = nfs42_layouterror_release,
943};
944
945int nfs42_proc_layouterror(struct pnfs_layout_segment *lseg,
946 const struct nfs42_layout_error *errors, size_t n)
947{
948 struct inode *inode = lseg->pls_layout->plh_inode;
949 struct nfs42_layouterror_data *data;
950 struct rpc_task *task;
951 struct rpc_message msg = {
952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTERROR],
953 };
954 struct rpc_task_setup task_setup = {
955 .rpc_message = &msg,
956 .callback_ops = &nfs42_layouterror_ops,
957 .flags = RPC_TASK_ASYNC,
958 };
959 unsigned int i;
960
961 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTERROR))
962 return -EOPNOTSUPP;
963 if (n > NFS42_LAYOUTERROR_MAX)
964 return -EINVAL;
965 data = nfs42_alloc_layouterror_data(lseg, GFP_NOFS);
966 if (!data)
967 return -ENOMEM;
968 for (i = 0; i < n; i++) {
969 data->args.errors[i] = errors[i];
970 data->args.num_errors++;
971 data->res.num_errors++;
972 }
973 msg.rpc_argp = &data->args;
974 msg.rpc_resp = &data->res;
975 task_setup.callback_data = data;
976 task_setup.rpc_client = NFS_SERVER(inode)->client;
977 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
978 task = rpc_run_task(&task_setup);
979 if (IS_ERR(task))
980 return PTR_ERR(task);
981 rpc_put_task(task);
982 return 0;
983}
984EXPORT_SYMBOL_GPL(nfs42_proc_layouterror);
985
Peng Taoe5341f32015-09-26 02:24:35 +0800986static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +0100987 struct file *dst_f, struct nfs_lock_context *src_lock,
988 struct nfs_lock_context *dst_lock, loff_t src_offset,
989 loff_t dst_offset, loff_t count)
Peng Taoe5341f32015-09-26 02:24:35 +0800990{
991 struct inode *src_inode = file_inode(src_f);
992 struct inode *dst_inode = file_inode(dst_f);
993 struct nfs_server *server = NFS_SERVER(dst_inode);
994 struct nfs42_clone_args args = {
995 .src_fh = NFS_FH(src_inode),
996 .dst_fh = NFS_FH(dst_inode),
997 .src_offset = src_offset,
998 .dst_offset = dst_offset,
Christoph Hellwig9494b2c2015-11-13 09:38:45 +0100999 .count = count,
Peng Taoe5341f32015-09-26 02:24:35 +08001000 .dst_bitmask = server->cache_consistency_bitmask,
1001 };
1002 struct nfs42_clone_res res = {
1003 .server = server,
1004 };
1005 int status;
1006
1007 msg->rpc_argp = &args;
1008 msg->rpc_resp = &res;
1009
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001010 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
1011 src_lock, FMODE_READ);
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05001012 if (status) {
1013 if (status == -EAGAIN)
1014 status = -NFS4ERR_BAD_STATEID;
Peng Taoe5341f32015-09-26 02:24:35 +08001015 return status;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05001016 }
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001017 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
1018 dst_lock, FMODE_WRITE);
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05001019 if (status) {
1020 if (status == -EAGAIN)
1021 status = -NFS4ERR_BAD_STATEID;
Peng Taoe5341f32015-09-26 02:24:35 +08001022 return status;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05001023 }
Peng Taoe5341f32015-09-26 02:24:35 +08001024
1025 res.dst_fattr = nfs_alloc_fattr();
1026 if (!res.dst_fattr)
1027 return -ENOMEM;
1028
1029 status = nfs4_call_sync(server->client, server, msg,
1030 &args.seq_args, &res.seq_res, 0);
1031 if (status == 0)
1032 status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
1033
1034 kfree(res.dst_fattr);
1035 return status;
1036}
1037
1038int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
1039 loff_t src_offset, loff_t dst_offset, loff_t count)
1040{
1041 struct rpc_message msg = {
1042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
1043 };
1044 struct inode *inode = file_inode(src_f);
1045 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001046 struct nfs_lock_context *src_lock;
1047 struct nfs_lock_context *dst_lock;
1048 struct nfs4_exception src_exception = { };
1049 struct nfs4_exception dst_exception = { };
1050 int err, err2;
Peng Taoe5341f32015-09-26 02:24:35 +08001051
1052 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
1053 return -EOPNOTSUPP;
1054
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001055 src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
1056 if (IS_ERR(src_lock))
1057 return PTR_ERR(src_lock);
1058
1059 src_exception.inode = file_inode(src_f);
1060 src_exception.state = src_lock->open_context->state;
1061
1062 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
1063 if (IS_ERR(dst_lock)) {
1064 err = PTR_ERR(dst_lock);
1065 goto out_put_src_lock;
1066 }
1067
1068 dst_exception.inode = file_inode(dst_f);
1069 dst_exception.state = dst_lock->open_context->state;
1070
Peng Taoe5341f32015-09-26 02:24:35 +08001071 do {
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001072 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
1073 src_offset, dst_offset, count);
Peng Taoe5341f32015-09-26 02:24:35 +08001074 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
1075 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001076 err = -EOPNOTSUPP;
1077 break;
Peng Taoe5341f32015-09-26 02:24:35 +08001078 }
Peng Taoe5341f32015-09-26 02:24:35 +08001079
Christoph Hellwig4bdf87e2016-02-17 15:48:28 +01001080 err2 = nfs4_handle_exception(server, err, &src_exception);
1081 err = nfs4_handle_exception(server, err, &dst_exception);
1082 if (!err)
1083 err = err2;
1084 } while (src_exception.retry || dst_exception.retry);
1085
1086 nfs_put_lock_context(dst_lock);
1087out_put_src_lock:
1088 nfs_put_lock_context(src_lock);
Peng Taoe5341f32015-09-26 02:24:35 +08001089 return err;
Peng Taoe5341f32015-09-26 02:24:35 +08001090}