Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com> |
| 4 | */ |
| 5 | #include <linux/fs.h> |
Olga Kornievskaia | 0491567 | 2019-06-04 16:14:30 -0400 | [diff] [blame] | 6 | #include <linux/sunrpc/addr.h> |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 7 | #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 Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 15 | #include "iostat.h" |
| 16 | #include "pnfs.h" |
Anna Schumaker | efc6f4a | 2017-01-09 15:14:33 -0500 | [diff] [blame] | 17 | #include "nfs4session.h" |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 18 | #include "internal.h" |
Olga Kornievskaia | 0491567 | 2019-06-04 16:14:30 -0400 | [diff] [blame] | 19 | #include "delegation.h" |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 20 | |
Anna Schumaker | 291e1b9 | 2015-11-16 14:51:07 -0500 | [diff] [blame] | 21 | #define NFSDBG_FACILITY NFSDBG_PROC |
Olga Kornievskaia | c975c20 | 2018-07-09 15:13:34 -0400 | [diff] [blame] | 22 | static int nfs42_do_offload_cancel_async(struct file *dst, nfs4_stateid *std); |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 23 | |
Olga Kornievskaia | 0491567 | 2019-06-04 16:14:30 -0400 | [diff] [blame] | 24 | static 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 Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 43 | static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 44 | struct nfs_lock_context *lock, loff_t offset, loff_t len) |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 45 | { |
| 46 | struct inode *inode = file_inode(filep); |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 47 | struct nfs_server *server = NFS_SERVER(inode); |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 48 | struct nfs42_falloc_args args = { |
| 49 | .falloc_fh = NFS_FH(inode), |
| 50 | .falloc_offset = offset, |
| 51 | .falloc_length = len, |
Anna Schumaker | 913eca1 | 2019-11-12 16:37:24 -0500 | [diff] [blame] | 52 | .falloc_bitmask = nfs4_fattr_bitmap, |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 53 | }; |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 54 | struct nfs42_falloc_res res = { |
| 55 | .falloc_server = server, |
| 56 | }; |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 57 | int status; |
| 58 | |
| 59 | msg->rpc_argp = &args; |
| 60 | msg->rpc_resp = &res; |
| 61 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 62 | status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context, |
| 63 | lock, FMODE_WRITE); |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 64 | if (status) { |
| 65 | if (status == -EAGAIN) |
| 66 | status = -NFS4ERR_BAD_STATEID; |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 67 | return status; |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 68 | } |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 69 | |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 70 | 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 Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static 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 Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 88 | struct nfs_lock_context *lock; |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 89 | int err; |
| 90 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 91 | 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 Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 98 | do { |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 99 | err = _nfs42_proc_fallocate(msg, filep, lock, offset, len); |
| 100 | if (err == -ENOTSUPP) { |
| 101 | err = -EOPNOTSUPP; |
| 102 | break; |
| 103 | } |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 104 | err = nfs4_handle_exception(server, err, &exception); |
| 105 | } while (exception.retry); |
| 106 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 107 | nfs_put_lock_context(lock); |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 108 | return err; |
| 109 | } |
| 110 | |
| 111 | int 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 Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 122 | inode_lock(inode); |
Anna Schumaker | f830f7d | 2015-03-16 14:06:24 -0400 | [diff] [blame] | 123 | |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 124 | err = nfs42_proc_fallocate(&msg, filep, offset, len); |
| 125 | if (err == -EOPNOTSUPP) |
| 126 | NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE; |
Anna Schumaker | f830f7d | 2015-03-16 14:06:24 -0400 | [diff] [blame] | 127 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 128 | inode_unlock(inode); |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 129 | return err; |
| 130 | } |
| 131 | |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 132 | int 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 Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 143 | inode_lock(inode); |
Trond Myklebust | 1e564d3 | 2016-06-25 17:50:53 -0400 | [diff] [blame] | 144 | err = nfs_sync_inode(inode); |
| 145 | if (err) |
| 146 | goto out_unlock; |
Anna Schumaker | f830f7d | 2015-03-16 14:06:24 -0400 | [diff] [blame] | 147 | |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 148 | err = nfs42_proc_fallocate(&msg, filep, offset, len); |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 149 | if (err == 0) |
| 150 | truncate_pagecache_range(inode, offset, (offset + len) -1); |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 151 | if (err == -EOPNOTSUPP) |
| 152 | NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE; |
Trond Myklebust | 1e564d3 | 2016-06-25 17:50:53 -0400 | [diff] [blame] | 153 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 154 | inode_unlock(inode); |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 155 | return err; |
| 156 | } |
| 157 | |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 158 | static int handle_async_copy(struct nfs42_copy_res *res, |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 159 | struct nfs_server *dst_server, |
| 160 | struct nfs_server *src_server, |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 161 | struct file *src, |
| 162 | struct file *dst, |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 163 | nfs4_stateid *src_stateid, |
| 164 | bool *restart) |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 165 | { |
Olga Kornievskaia | 99f2c55 | 2018-11-21 11:24:22 -0500 | [diff] [blame] | 166 | struct nfs4_copy_state *copy, *tmp_copy; |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 167 | int status = NFS4_OK; |
Olga Kornievskaia | bc0c907 | 2018-07-09 15:13:32 -0400 | [diff] [blame] | 168 | bool found_pending = false; |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 169 | struct nfs_open_context *dst_ctx = nfs_file_open_context(dst); |
| 170 | struct nfs_open_context *src_ctx = nfs_file_open_context(src); |
Olga Kornievskaia | bc0c907 | 2018-07-09 15:13:32 -0400 | [diff] [blame] | 171 | |
Olga Kornievskaia | 99f2c55 | 2018-11-21 11:24:22 -0500 | [diff] [blame] | 172 | copy = kzalloc(sizeof(struct nfs4_copy_state), GFP_NOFS); |
| 173 | if (!copy) |
| 174 | return -ENOMEM; |
| 175 | |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 176 | spin_lock(&dst_server->nfs_client->cl_lock); |
| 177 | list_for_each_entry(tmp_copy, |
| 178 | &dst_server->nfs_client->pending_cb_stateids, |
Olga Kornievskaia | bc0c907 | 2018-07-09 15:13:32 -0400 | [diff] [blame] | 179 | copies) { |
Olga Kornievskaia | 99f2c55 | 2018-11-21 11:24:22 -0500 | [diff] [blame] | 180 | if (memcmp(&res->write_res.stateid, &tmp_copy->stateid, |
Olga Kornievskaia | bc0c907 | 2018-07-09 15:13:32 -0400 | [diff] [blame] | 181 | NFS4_STATEID_SIZE)) |
| 182 | continue; |
| 183 | found_pending = true; |
Olga Kornievskaia | 99f2c55 | 2018-11-21 11:24:22 -0500 | [diff] [blame] | 184 | list_del(&tmp_copy->copies); |
Olga Kornievskaia | bc0c907 | 2018-07-09 15:13:32 -0400 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | if (found_pending) { |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 188 | spin_unlock(&dst_server->nfs_client->cl_lock); |
Olga Kornievskaia | 99f2c55 | 2018-11-21 11:24:22 -0500 | [diff] [blame] | 189 | kfree(copy); |
| 190 | copy = tmp_copy; |
Olga Kornievskaia | bc0c907 | 2018-07-09 15:13:32 -0400 | [diff] [blame] | 191 | goto out; |
| 192 | } |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 193 | |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 194 | memcpy(©->stateid, &res->write_res.stateid, NFS4_STATEID_SIZE); |
| 195 | init_completion(©->completion); |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 196 | copy->parent_dst_state = dst_ctx->state; |
| 197 | copy->parent_src_state = src_ctx->state; |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 198 | |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 199 | list_add_tail(©->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(©->src_copies, &src_server->ss_copies); |
| 205 | spin_unlock(&src_server->nfs_client->cl_lock); |
| 206 | } |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 207 | |
Olga Kornievskaia | c975c20 | 2018-07-09 15:13:34 -0400 | [diff] [blame] | 208 | status = wait_for_completion_interruptible(©->completion); |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 209 | spin_lock(&dst_server->nfs_client->cl_lock); |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 210 | list_del_init(©->copies); |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 211 | 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(©->src_copies); |
| 215 | spin_unlock(&src_server->nfs_client->cl_lock); |
| 216 | } |
Olga Kornievskaia | c975c20 | 2018-07-09 15:13:34 -0400 | [diff] [blame] | 217 | if (status == -ERESTARTSYS) { |
Olga Kornievskaia | e4648aa | 2018-08-13 15:33:01 -0400 | [diff] [blame] | 218 | goto out_cancel; |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 219 | } else if (copy->flags || copy->error == NFS4ERR_PARTNER_NO_AUTH) { |
Olga Kornievskaia | e4648aa | 2018-08-13 15:33:01 -0400 | [diff] [blame] | 220 | status = -EAGAIN; |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 221 | *restart = true; |
Olga Kornievskaia | e4648aa | 2018-08-13 15:33:01 -0400 | [diff] [blame] | 222 | goto out_cancel; |
Olga Kornievskaia | c975c20 | 2018-07-09 15:13:34 -0400 | [diff] [blame] | 223 | } |
Olga Kornievskaia | bc0c907 | 2018-07-09 15:13:32 -0400 | [diff] [blame] | 224 | out: |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 225 | res->write_res.count = copy->count; |
| 226 | memcpy(&res->write_res.verifier, ©->verf, sizeof(copy->verf)); |
| 227 | status = -copy->error; |
| 228 | |
Olga Kornievskaia | 1240602 | 2019-07-02 14:57:25 -0400 | [diff] [blame] | 229 | out_free: |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 230 | kfree(copy); |
| 231 | return status; |
Olga Kornievskaia | e4648aa | 2018-08-13 15:33:01 -0400 | [diff] [blame] | 232 | out_cancel: |
| 233 | nfs42_do_offload_cancel_async(dst, ©->stateid); |
Olga Kornievskaia | 1240602 | 2019-07-02 14:57:25 -0400 | [diff] [blame] | 234 | if (!nfs42_files_from_same_server(src, dst)) |
| 235 | nfs42_do_offload_cancel_async(src, src_stateid); |
| 236 | goto out_free; |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Olga Kornievskaia | 6b8d84e | 2018-07-09 15:13:36 -0400 | [diff] [blame] | 239 | static 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 | } |
| 257 | out_free: |
| 258 | kfree(cres.verf); |
| 259 | out: |
| 260 | return status; |
| 261 | } |
| 262 | |
Trond Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 263 | static ssize_t _nfs42_proc_copy(struct file *src, |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 264 | struct nfs_lock_context *src_lock, |
Trond Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 265 | struct file *dst, |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 266 | struct nfs_lock_context *dst_lock, |
Trond Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 267 | struct nfs42_copy_args *args, |
Olga Kornievskaia | 1d38f3f | 2019-06-04 11:54:18 -0400 | [diff] [blame] | 268 | struct nfs42_copy_res *res, |
| 269 | struct nl4_server *nss, |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 270 | nfs4_stateid *cnr_stateid, |
| 271 | bool *restart) |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 272 | { |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 273 | struct rpc_message msg = { |
| 274 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY], |
Trond Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 275 | .rpc_argp = args, |
| 276 | .rpc_resp = res, |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 277 | }; |
| 278 | struct inode *dst_inode = file_inode(dst); |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 279 | 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 Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 282 | loff_t pos_src = args->src_pos; |
| 283 | loff_t pos_dst = args->dst_pos; |
| 284 | size_t count = args->count; |
Olga Kornievskaia | 1ee48bd | 2017-07-06 09:43:02 -0400 | [diff] [blame] | 285 | ssize_t status; |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 286 | |
Olga Kornievskaia | 1d38f3f | 2019-06-04 11:54:18 -0400 | [diff] [blame] | 287 | 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 Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 293 | if (status) { |
| 294 | if (status == -EAGAIN) |
| 295 | status = -NFS4ERR_BAD_STATEID; |
Olga Kornievskaia | 1d38f3f | 2019-06-04 11:54:18 -0400 | [diff] [blame] | 296 | return status; |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 297 | } |
Olga Kornievskaia | 1d38f3f | 2019-06-04 11:54:18 -0400 | [diff] [blame] | 298 | } |
Trond Myklebust | 837bb1d | 2016-06-25 18:12:03 -0400 | [diff] [blame] | 299 | 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 Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 304 | status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context, |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 305 | dst_lock, FMODE_WRITE); |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 306 | if (status) { |
| 307 | if (status == -EAGAIN) |
| 308 | status = -NFS4ERR_BAD_STATEID; |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 309 | return status; |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 310 | } |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 311 | |
Trond Myklebust | 837bb1d | 2016-06-25 18:12:03 -0400 | [diff] [blame] | 312 | status = nfs_sync_inode(dst_inode); |
| 313 | if (status) |
| 314 | return status; |
| 315 | |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 316 | 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 Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 323 | set_bit(NFS_CLNT_SRC_SSC_COPY_STATE, |
| 324 | &src_lock->open_context->state->flags); |
Olga Kornievskaia | e4648aa | 2018-08-13 15:33:01 -0400 | [diff] [blame] | 325 | set_bit(NFS_CLNT_DST_SSC_COPY_STATE, |
| 326 | &dst_lock->open_context->state->flags); |
| 327 | |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 328 | status = nfs4_call_sync(dst_server->client, dst_server, &msg, |
Trond Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 329 | &args->seq_args, &res->seq_res, 0); |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 330 | if (status == -ENOTSUPP) |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 331 | dst_server->caps &= ~NFS_CAP_COPY; |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 332 | if (status) |
Olga Kornievskaia | e092693 | 2017-05-08 18:02:24 -0400 | [diff] [blame] | 333 | goto out; |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 334 | |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 335 | if (args->sync && |
| 336 | nfs_write_verifier_cmp(&res->write_res.verifier.verifier, |
Olga Kornievskaia | e092693 | 2017-05-08 18:02:24 -0400 | [diff] [blame] | 337 | &res->commit_res.verf->verifier)) { |
| 338 | status = -EAGAIN; |
| 339 | goto out; |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 342 | if (!res->synchronous) { |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 343 | status = handle_async_copy(res, dst_server, src_server, src, |
| 344 | dst, &args->src_stateid, restart); |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 345 | if (status) |
Wenwen Wang | 123c23c | 2020-02-03 03:47:53 +0000 | [diff] [blame] | 346 | goto out; |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Olga Kornievskaia | 6b8d84e | 2018-07-09 15:13:36 -0400 | [diff] [blame] | 349 | 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 Wang | 123c23c | 2020-02-03 03:47:53 +0000 | [diff] [blame] | 353 | goto out; |
Olga Kornievskaia | 6b8d84e | 2018-07-09 15:13:36 -0400 | [diff] [blame] | 354 | } |
| 355 | |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 356 | truncate_pagecache_range(dst_inode, pos_dst, |
Trond Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 357 | pos_dst + res->write_res.count); |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 358 | |
Olga Kornievskaia | e092693 | 2017-05-08 18:02:24 -0400 | [diff] [blame] | 359 | status = res->write_res.count; |
| 360 | out: |
Olga Kornievskaia | 62164f3 | 2018-07-09 15:13:31 -0400 | [diff] [blame] | 361 | if (args->sync) |
| 362 | kfree(res->commit_res.verf); |
Olga Kornievskaia | e092693 | 2017-05-08 18:02:24 -0400 | [diff] [blame] | 363 | return status; |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src, |
Olga Kornievskaia | 1d38f3f | 2019-06-04 11:54:18 -0400 | [diff] [blame] | 367 | struct file *dst, loff_t pos_dst, size_t count, |
| 368 | struct nl4_server *nss, |
Olga Kornievskaia | 1275101 | 2019-07-03 10:38:02 -0400 | [diff] [blame] | 369 | nfs4_stateid *cnr_stateid, bool sync) |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 370 | { |
| 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 Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 374 | 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 Kornievskaia | 1275101 | 2019-07-03 10:38:02 -0400 | [diff] [blame] | 380 | .sync = sync, |
Trond Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 381 | }; |
| 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 Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 391 | ssize_t err, err2; |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 392 | bool restart = false; |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 393 | |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 394 | 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 Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 398 | 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 Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 406 | dst_exception.state = dst_lock->open_context->state; |
| 407 | |
| 408 | do { |
Linus Torvalds | ea8ea73 | 2016-05-26 10:33:33 -0700 | [diff] [blame] | 409 | inode_lock(file_inode(dst)); |
Trond Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 410 | err = _nfs42_proc_copy(src, src_lock, |
| 411 | dst, dst_lock, |
Olga Kornievskaia | 1d38f3f | 2019-06-04 11:54:18 -0400 | [diff] [blame] | 412 | &args, &res, |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 413 | nss, cnr_stateid, &restart); |
Linus Torvalds | ea8ea73 | 2016-05-26 10:33:33 -0700 | [diff] [blame] | 414 | inode_unlock(file_inode(dst)); |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 415 | |
Trond Myklebust | 9d8cacb | 2017-02-17 18:42:32 -0500 | [diff] [blame] | 416 | if (err >= 0) |
| 417 | break; |
Olga Kornievskaia | 1240602 | 2019-07-02 14:57:25 -0400 | [diff] [blame] | 418 | if (err == -ENOTSUPP && |
| 419 | nfs42_files_from_same_server(src, dst)) { |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 420 | err = -EOPNOTSUPP; |
| 421 | break; |
Olga Kornievskaia | 539f57b | 2018-07-09 15:13:35 -0400 | [diff] [blame] | 422 | } else if (err == -EAGAIN) { |
Olga Kornievskaia | 0e65a32 | 2019-06-14 14:38:40 -0400 | [diff] [blame] | 423 | if (!restart) { |
| 424 | dst_exception.retry = 1; |
| 425 | continue; |
| 426 | } |
| 427 | break; |
Olga Kornievskaia | 539f57b | 2018-07-09 15:13:35 -0400 | [diff] [blame] | 428 | } else if (err == -NFS4ERR_OFFLOAD_NO_REQS && !args.sync) { |
| 429 | args.sync = true; |
Olga Kornievskaia | e092693 | 2017-05-08 18:02:24 -0400 | [diff] [blame] | 430 | dst_exception.retry = 1; |
| 431 | continue; |
Olga Kornievskaia | 6b61c96 | 2017-09-26 13:51:39 -0400 | [diff] [blame] | 432 | } else if ((err == -ESTALE || |
Olga Kornievskaia | 1240602 | 2019-07-02 14:57:25 -0400 | [diff] [blame] | 433 | err == -NFS4ERR_OFFLOAD_DENIED || |
| 434 | err == -ENOTSUPP) && |
Olga Kornievskaia | 7e35019 | 2017-09-25 15:59:44 -0400 | [diff] [blame] | 435 | !nfs42_files_from_same_server(src, dst)) { |
| 436 | nfs42_do_offload_cancel_async(src, &args.src_stateid); |
| 437 | err = -EOPNOTSUPP; |
| 438 | break; |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 439 | } |
| 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); |
| 448 | out_put_src_lock: |
| 449 | nfs_put_lock_context(src_lock); |
| 450 | return err; |
| 451 | } |
| 452 | |
Olga Kornievskaia | c975c20 | 2018-07-09 15:13:34 -0400 | [diff] [blame] | 453 | struct 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 | |
| 459 | static 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 | |
| 468 | static 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 | |
| 479 | static void nfs42_free_offloadcancel_data(void *data) |
| 480 | { |
| 481 | kfree(data); |
| 482 | } |
| 483 | |
| 484 | static 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 | |
| 490 | static 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 | |
YueHaibing | 0003010 | 2019-11-14 22:01:41 +0800 | [diff] [blame] | 536 | static 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 Kornievskaia | 0491567 | 2019-06-04 16:14:30 -0400 | [diff] [blame] | 539 | { |
| 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 Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 558 | if (status) { |
| 559 | if (status == -EAGAIN) |
| 560 | status = -NFS4ERR_BAD_STATEID; |
Olga Kornievskaia | 0491567 | 2019-06-04 16:14:30 -0400 | [diff] [blame] | 561 | return status; |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 562 | } |
Olga Kornievskaia | 0491567 | 2019-06-04 16:14:30 -0400 | [diff] [blame] | 563 | |
| 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 | |
| 573 | int 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 | |
| 604 | out: |
| 605 | kfree(args); |
| 606 | return status; |
| 607 | } |
| 608 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 609 | static loff_t _nfs42_proc_llseek(struct file *filep, |
| 610 | struct nfs_lock_context *lock, loff_t offset, int whence) |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 611 | { |
| 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 Schumaker | 878ffa9 | 2014-10-23 14:00:54 -0400 | [diff] [blame] | 628 | if (!nfs_server_capable(inode, NFS_CAP_SEEK)) |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 629 | return -ENOTSUPP; |
| 630 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 631 | status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context, |
| 632 | lock, FMODE_READ); |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 633 | if (status) { |
| 634 | if (status == -EAGAIN) |
| 635 | status = -NFS4ERR_BAD_STATEID; |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 636 | return status; |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 637 | } |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 638 | |
Trond Myklebust | e95fc4a | 2016-06-25 17:57:39 -0400 | [diff] [blame] | 639 | status = nfs_filemap_write_and_wait_range(inode->i_mapping, |
| 640 | offset, LLONG_MAX); |
| 641 | if (status) |
| 642 | return status; |
| 643 | |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 644 | 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 Myklebust | be3a5d2 | 2015-06-23 19:51:55 +0800 | [diff] [blame] | 653 | |
J. Bruce Fields | bdcc2cd | 2015-07-23 11:08:43 -0400 | [diff] [blame] | 654 | loff_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 Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 658 | struct nfs_lock_context *lock; |
J. Bruce Fields | 306a554 | 2015-09-16 17:21:27 -0400 | [diff] [blame] | 659 | loff_t err; |
J. Bruce Fields | bdcc2cd | 2015-07-23 11:08:43 -0400 | [diff] [blame] | 660 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 661 | 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 Fields | bdcc2cd | 2015-07-23 11:08:43 -0400 | [diff] [blame] | 668 | do { |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 669 | err = _nfs42_proc_llseek(filep, lock, offset, whence); |
J. Bruce Fields | 306a554 | 2015-09-16 17:21:27 -0400 | [diff] [blame] | 670 | if (err >= 0) |
| 671 | break; |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 672 | if (err == -ENOTSUPP) { |
| 673 | err = -EOPNOTSUPP; |
| 674 | break; |
| 675 | } |
J. Bruce Fields | bdcc2cd | 2015-07-23 11:08:43 -0400 | [diff] [blame] | 676 | err = nfs4_handle_exception(server, err, &exception); |
| 677 | } while (exception.retry); |
| 678 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 679 | nfs_put_lock_context(lock); |
J. Bruce Fields | bdcc2cd | 2015-07-23 11:08:43 -0400 | [diff] [blame] | 680 | return err; |
| 681 | } |
| 682 | |
| 683 | |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 684 | static void |
| 685 | nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata) |
| 686 | { |
| 687 | struct nfs42_layoutstat_data *data = calldata; |
Trond Myklebust | 9a0fe86 | 2016-08-19 15:33:12 -0400 | [diff] [blame] | 688 | struct inode *inode = data->inode; |
| 689 | struct nfs_server *server = NFS_SERVER(inode); |
| 690 | struct pnfs_layout_hdr *lo; |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 691 | |
Trond Myklebust | 9a0fe86 | 2016-08-19 15:33:12 -0400 | [diff] [blame] | 692 | 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 Schumaker | 6de7e12 | 2017-01-09 16:51:52 -0500 | [diff] [blame] | 701 | nfs4_setup_sequence(server->nfs_client, &data->args.seq_args, |
| 702 | &data->res.seq_res, task); |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | static void |
| 706 | nfs42_layoutstat_done(struct rpc_task *task, void *calldata) |
| 707 | { |
| 708 | struct nfs42_layoutstat_data *data = calldata; |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 709 | struct inode *inode = data->inode; |
| 710 | struct pnfs_layout_hdr *lo; |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 711 | |
| 712 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
| 713 | return; |
| 714 | |
Trond Myklebust | 6c5a0d8 | 2015-06-27 11:45:46 -0400 | [diff] [blame] | 715 | switch (task->tk_status) { |
| 716 | case 0: |
| 717 | break; |
Trond Myklebust | cf61eb2 | 2018-05-29 22:06:08 -0400 | [diff] [blame] | 718 | case -NFS4ERR_BADHANDLE: |
| 719 | case -ESTALE: |
| 720 | pnfs_destroy_layout(NFS_I(inode)); |
| 721 | break; |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 722 | case -NFS4ERR_EXPIRED: |
Trond Myklebust | 206b3bb | 2016-08-05 12:16:19 -0400 | [diff] [blame] | 723 | case -NFS4ERR_ADMIN_REVOKED: |
| 724 | case -NFS4ERR_DELEG_REVOKED: |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 725 | case -NFS4ERR_STALE_STATEID: |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 726 | case -NFS4ERR_BAD_STATEID: |
| 727 | spin_lock(&inode->i_lock); |
| 728 | lo = NFS_I(inode)->layout; |
Trond Myklebust | 9a0fe86 | 2016-08-19 15:33:12 -0400 | [diff] [blame] | 729 | if (pnfs_layout_is_valid(lo) && |
| 730 | nfs4_stateid_match(&data->args.stateid, |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 731 | &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 Myklebust | 5f46be0 | 2016-07-22 11:25:27 -0400 | [diff] [blame] | 738 | pnfs_mark_layout_stateid_invalid(lo, &head); |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 739 | spin_unlock(&inode->i_lock); |
| 740 | pnfs_free_lseg_list(&head); |
Trond Myklebust | 1f18b82 | 2017-04-29 10:10:17 -0400 | [diff] [blame] | 741 | nfs_commit_inode(inode, 0); |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 742 | } else |
| 743 | spin_unlock(&inode->i_lock); |
| 744 | break; |
Trond Myklebust | 9a0fe86 | 2016-08-19 15:33:12 -0400 | [diff] [blame] | 745 | 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 Myklebust | 6c5a0d8 | 2015-06-27 11:45:46 -0400 | [diff] [blame] | 759 | case -ENOTSUPP: |
| 760 | case -EOPNOTSUPP: |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 761 | NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS; |
Trond Myklebust | 6c5a0d8 | 2015-06-27 11:45:46 -0400 | [diff] [blame] | 762 | } |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static void |
| 766 | nfs42_layoutstat_release(void *calldata) |
| 767 | { |
| 768 | struct nfs42_layoutstat_data *data = calldata; |
Trond Myklebust | 422c93c | 2016-10-06 17:53:20 -0400 | [diff] [blame] | 769 | struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo; |
| 770 | int i; |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 771 | |
Trond Myklebust | 422c93c | 2016-10-06 17:53:20 -0400 | [diff] [blame] | 772 | 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 Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 776 | |
| 777 | pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout); |
Peng Tao | 1bfe3b2 | 2015-06-23 19:52:03 +0800 | [diff] [blame] | 778 | smp_mb__before_atomic(); |
| 779 | clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags); |
| 780 | smp_mb__after_atomic(); |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 781 | nfs_iput_and_deactive(data->inode); |
| 782 | kfree(data->args.devinfo); |
| 783 | kfree(data); |
| 784 | } |
| 785 | |
Trond Myklebust | be3a5d2 | 2015-06-23 19:51:55 +0800 | [diff] [blame] | 786 | static const struct rpc_call_ops nfs42_layoutstat_ops = { |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 787 | .rpc_call_prepare = nfs42_layoutstat_prepare, |
| 788 | .rpc_call_done = nfs42_layoutstat_done, |
| 789 | .rpc_release = nfs42_layoutstat_release, |
Trond Myklebust | be3a5d2 | 2015-06-23 19:51:55 +0800 | [diff] [blame] | 790 | }; |
| 791 | |
| 792 | int 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 Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 809 | data->inode = nfs_igrab_and_active(data->args.inode); |
| 810 | if (!data->inode) { |
| 811 | nfs42_layoutstat_release(data); |
| 812 | return -EAGAIN; |
| 813 | } |
Anna Schumaker | fba83f3 | 2018-05-04 16:22:50 -0400 | [diff] [blame] | 814 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0); |
Trond Myklebust | be3a5d2 | 2015-06-23 19:51:55 +0800 | [diff] [blame] | 815 | task = rpc_run_task(&task_setup); |
| 816 | if (IS_ERR(task)) |
| 817 | return PTR_ERR(task); |
Jeff Layton | 3f807e5 | 2016-10-04 00:07:43 -0400 | [diff] [blame] | 818 | rpc_put_task(task); |
Trond Myklebust | be3a5d2 | 2015-06-23 19:51:55 +0800 | [diff] [blame] | 819 | return 0; |
| 820 | } |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 821 | |
Trond Myklebust | 3eb8609 | 2019-02-08 10:31:05 -0500 | [diff] [blame] | 822 | static struct nfs42_layouterror_data * |
| 823 | nfs42_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 | |
| 842 | static void |
| 843 | nfs42_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 | |
| 850 | static void |
| 851 | nfs42_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 | |
| 873 | static void |
| 874 | nfs42_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 | |
| 931 | static void |
| 932 | nfs42_layouterror_release(void *calldata) |
| 933 | { |
| 934 | struct nfs42_layouterror_data *data = calldata; |
| 935 | |
| 936 | nfs42_free_layouterror_data(data); |
| 937 | } |
| 938 | |
| 939 | static 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 | |
| 945 | int 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 | } |
| 984 | EXPORT_SYMBOL_GPL(nfs42_proc_layouterror); |
| 985 | |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 986 | static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f, |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 987 | 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 Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 990 | { |
| 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 Hellwig | 9494b2c | 2015-11-13 09:38:45 +0100 | [diff] [blame] | 999 | .count = count, |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1000 | .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 Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 1010 | status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context, |
| 1011 | src_lock, FMODE_READ); |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 1012 | if (status) { |
| 1013 | if (status == -EAGAIN) |
| 1014 | status = -NFS4ERR_BAD_STATEID; |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1015 | return status; |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 1016 | } |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 1017 | status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context, |
| 1018 | dst_lock, FMODE_WRITE); |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 1019 | if (status) { |
| 1020 | if (status == -EAGAIN) |
| 1021 | status = -NFS4ERR_BAD_STATEID; |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1022 | return status; |
Olga Kornievskaia | d826e5b | 2019-12-18 16:50:42 -0500 | [diff] [blame] | 1023 | } |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1024 | |
| 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 | |
| 1038 | int 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 Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 1046 | 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 Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1051 | |
| 1052 | if (!nfs_server_capable(inode, NFS_CAP_CLONE)) |
| 1053 | return -EOPNOTSUPP; |
| 1054 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 1055 | 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 Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1071 | do { |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 1072 | err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock, |
| 1073 | src_offset, dst_offset, count); |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1074 | if (err == -ENOTSUPP || err == -EOPNOTSUPP) { |
| 1075 | NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE; |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 1076 | err = -EOPNOTSUPP; |
| 1077 | break; |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1078 | } |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1079 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 1080 | 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); |
| 1087 | out_put_src_lock: |
| 1088 | nfs_put_lock_context(src_lock); |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1089 | return err; |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 1090 | } |