Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com> |
| 3 | */ |
| 4 | #include <linux/fs.h> |
| 5 | #include <linux/sunrpc/sched.h> |
| 6 | #include <linux/nfs.h> |
| 7 | #include <linux/nfs3.h> |
| 8 | #include <linux/nfs4.h> |
| 9 | #include <linux/nfs_xdr.h> |
| 10 | #include <linux/nfs_fs.h> |
| 11 | #include "nfs4_fs.h" |
| 12 | #include "nfs42.h" |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 13 | #include "iostat.h" |
| 14 | #include "pnfs.h" |
| 15 | #include "internal.h" |
| 16 | |
Anna Schumaker | 291e1b9 | 2015-11-16 14:51:07 -0500 | [diff] [blame] | 17 | #define NFSDBG_FACILITY NFSDBG_PROC |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 18 | |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 19 | static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 20 | struct nfs_lock_context *lock, loff_t offset, loff_t len) |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 21 | { |
| 22 | struct inode *inode = file_inode(filep); |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 23 | struct nfs_server *server = NFS_SERVER(inode); |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 24 | struct nfs42_falloc_args args = { |
| 25 | .falloc_fh = NFS_FH(inode), |
| 26 | .falloc_offset = offset, |
| 27 | .falloc_length = len, |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 28 | .falloc_bitmask = server->cache_consistency_bitmask, |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 29 | }; |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 30 | struct nfs42_falloc_res res = { |
| 31 | .falloc_server = server, |
| 32 | }; |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 33 | int status; |
| 34 | |
| 35 | msg->rpc_argp = &args; |
| 36 | msg->rpc_resp = &res; |
| 37 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 38 | status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context, |
| 39 | lock, FMODE_WRITE); |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 40 | if (status) |
| 41 | return status; |
| 42 | |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 43 | res.falloc_fattr = nfs_alloc_fattr(); |
| 44 | if (!res.falloc_fattr) |
| 45 | return -ENOMEM; |
| 46 | |
| 47 | status = nfs4_call_sync(server->client, server, msg, |
| 48 | &args.seq_args, &res.seq_res, 0); |
| 49 | if (status == 0) |
| 50 | status = nfs_post_op_update_inode(inode, res.falloc_fattr); |
| 51 | |
| 52 | kfree(res.falloc_fattr); |
| 53 | return status; |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, |
| 57 | loff_t offset, loff_t len) |
| 58 | { |
| 59 | struct nfs_server *server = NFS_SERVER(file_inode(filep)); |
| 60 | struct nfs4_exception exception = { }; |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 61 | struct nfs_lock_context *lock; |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 62 | int err; |
| 63 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 64 | lock = nfs_get_lock_context(nfs_file_open_context(filep)); |
| 65 | if (IS_ERR(lock)) |
| 66 | return PTR_ERR(lock); |
| 67 | |
| 68 | exception.inode = file_inode(filep); |
| 69 | exception.state = lock->open_context->state; |
| 70 | |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 71 | do { |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 72 | err = _nfs42_proc_fallocate(msg, filep, lock, offset, len); |
| 73 | if (err == -ENOTSUPP) { |
| 74 | err = -EOPNOTSUPP; |
| 75 | break; |
| 76 | } |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 77 | err = nfs4_handle_exception(server, err, &exception); |
| 78 | } while (exception.retry); |
| 79 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 80 | nfs_put_lock_context(lock); |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 81 | return err; |
| 82 | } |
| 83 | |
| 84 | int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len) |
| 85 | { |
| 86 | struct rpc_message msg = { |
| 87 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE], |
| 88 | }; |
| 89 | struct inode *inode = file_inode(filep); |
| 90 | int err; |
| 91 | |
| 92 | if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE)) |
| 93 | return -EOPNOTSUPP; |
| 94 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 95 | inode_lock(inode); |
Anna Schumaker | f830f7d | 2015-03-16 14:06:24 -0400 | [diff] [blame] | 96 | |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 97 | err = nfs42_proc_fallocate(&msg, filep, offset, len); |
| 98 | if (err == -EOPNOTSUPP) |
| 99 | NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE; |
Anna Schumaker | f830f7d | 2015-03-16 14:06:24 -0400 | [diff] [blame] | 100 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 101 | inode_unlock(inode); |
Anna Schumaker | f4ac167 | 2014-11-25 13:18:15 -0500 | [diff] [blame] | 102 | return err; |
| 103 | } |
| 104 | |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 105 | int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len) |
| 106 | { |
| 107 | struct rpc_message msg = { |
| 108 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE], |
| 109 | }; |
| 110 | struct inode *inode = file_inode(filep); |
| 111 | int err; |
| 112 | |
| 113 | if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE)) |
| 114 | return -EOPNOTSUPP; |
| 115 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 116 | inode_lock(inode); |
Trond Myklebust | 1e564d3 | 2016-06-25 17:50:53 -0400 | [diff] [blame^] | 117 | err = nfs_sync_inode(inode); |
| 118 | if (err) |
| 119 | goto out_unlock; |
Anna Schumaker | f830f7d | 2015-03-16 14:06:24 -0400 | [diff] [blame] | 120 | |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 121 | err = nfs42_proc_fallocate(&msg, filep, offset, len); |
Anna Schumaker | 9a51940 | 2015-03-16 14:06:23 -0400 | [diff] [blame] | 122 | if (err == 0) |
| 123 | truncate_pagecache_range(inode, offset, (offset + len) -1); |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 124 | if (err == -EOPNOTSUPP) |
| 125 | NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE; |
Trond Myklebust | 1e564d3 | 2016-06-25 17:50:53 -0400 | [diff] [blame^] | 126 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 127 | inode_unlock(inode); |
Anna Schumaker | 624bd5b | 2014-11-25 13:18:16 -0500 | [diff] [blame] | 128 | return err; |
| 129 | } |
| 130 | |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 131 | static ssize_t _nfs42_proc_copy(struct file *src, loff_t pos_src, |
| 132 | struct nfs_lock_context *src_lock, |
| 133 | struct file *dst, loff_t pos_dst, |
| 134 | struct nfs_lock_context *dst_lock, |
| 135 | size_t count) |
| 136 | { |
| 137 | struct nfs42_copy_args args = { |
| 138 | .src_fh = NFS_FH(file_inode(src)), |
| 139 | .src_pos = pos_src, |
| 140 | .dst_fh = NFS_FH(file_inode(dst)), |
| 141 | .dst_pos = pos_dst, |
| 142 | .count = count, |
| 143 | }; |
| 144 | struct nfs42_copy_res res; |
| 145 | struct rpc_message msg = { |
| 146 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY], |
| 147 | .rpc_argp = &args, |
| 148 | .rpc_resp = &res, |
| 149 | }; |
| 150 | struct inode *dst_inode = file_inode(dst); |
| 151 | struct nfs_server *server = NFS_SERVER(dst_inode); |
| 152 | int status; |
| 153 | |
| 154 | status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context, |
| 155 | src_lock, FMODE_READ); |
| 156 | if (status) |
| 157 | return status; |
| 158 | |
| 159 | status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context, |
| 160 | dst_lock, FMODE_WRITE); |
| 161 | if (status) |
| 162 | return status; |
| 163 | |
| 164 | status = nfs4_call_sync(server->client, server, &msg, |
| 165 | &args.seq_args, &res.seq_res, 0); |
| 166 | if (status == -ENOTSUPP) |
| 167 | server->caps &= ~NFS_CAP_COPY; |
| 168 | if (status) |
| 169 | return status; |
| 170 | |
| 171 | if (res.write_res.verifier.committed != NFS_FILE_SYNC) { |
| 172 | status = nfs_commit_file(dst, &res.write_res.verifier.verifier); |
| 173 | if (status) |
| 174 | return status; |
| 175 | } |
| 176 | |
| 177 | truncate_pagecache_range(dst_inode, pos_dst, |
| 178 | pos_dst + res.write_res.count); |
| 179 | |
| 180 | return res.write_res.count; |
| 181 | } |
| 182 | |
| 183 | ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src, |
| 184 | struct file *dst, loff_t pos_dst, |
| 185 | size_t count) |
| 186 | { |
| 187 | struct nfs_server *server = NFS_SERVER(file_inode(dst)); |
| 188 | struct nfs_lock_context *src_lock; |
| 189 | struct nfs_lock_context *dst_lock; |
| 190 | struct nfs4_exception src_exception = { }; |
| 191 | struct nfs4_exception dst_exception = { }; |
| 192 | ssize_t err, err2; |
| 193 | |
| 194 | if (!nfs_server_capable(file_inode(dst), NFS_CAP_COPY)) |
| 195 | return -EOPNOTSUPP; |
| 196 | |
| 197 | src_lock = nfs_get_lock_context(nfs_file_open_context(src)); |
| 198 | if (IS_ERR(src_lock)) |
| 199 | return PTR_ERR(src_lock); |
| 200 | |
| 201 | src_exception.inode = file_inode(src); |
| 202 | src_exception.state = src_lock->open_context->state; |
| 203 | |
| 204 | dst_lock = nfs_get_lock_context(nfs_file_open_context(dst)); |
| 205 | if (IS_ERR(dst_lock)) { |
| 206 | err = PTR_ERR(dst_lock); |
| 207 | goto out_put_src_lock; |
| 208 | } |
| 209 | |
| 210 | dst_exception.inode = file_inode(dst); |
| 211 | dst_exception.state = dst_lock->open_context->state; |
| 212 | |
| 213 | do { |
Linus Torvalds | ea8ea73 | 2016-05-26 10:33:33 -0700 | [diff] [blame] | 214 | inode_lock(file_inode(dst)); |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 215 | err = _nfs42_proc_copy(src, pos_src, src_lock, |
| 216 | dst, pos_dst, dst_lock, count); |
Linus Torvalds | ea8ea73 | 2016-05-26 10:33:33 -0700 | [diff] [blame] | 217 | inode_unlock(file_inode(dst)); |
Anna Schumaker | 2e72448 | 2013-05-21 16:53:03 -0400 | [diff] [blame] | 218 | |
| 219 | if (err == -ENOTSUPP) { |
| 220 | err = -EOPNOTSUPP; |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | err2 = nfs4_handle_exception(server, err, &src_exception); |
| 225 | err = nfs4_handle_exception(server, err, &dst_exception); |
| 226 | if (!err) |
| 227 | err = err2; |
| 228 | } while (src_exception.retry || dst_exception.retry); |
| 229 | |
| 230 | nfs_put_lock_context(dst_lock); |
| 231 | out_put_src_lock: |
| 232 | nfs_put_lock_context(src_lock); |
| 233 | return err; |
| 234 | } |
| 235 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 236 | static loff_t _nfs42_proc_llseek(struct file *filep, |
| 237 | struct nfs_lock_context *lock, loff_t offset, int whence) |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 238 | { |
| 239 | struct inode *inode = file_inode(filep); |
| 240 | struct nfs42_seek_args args = { |
| 241 | .sa_fh = NFS_FH(inode), |
| 242 | .sa_offset = offset, |
| 243 | .sa_what = (whence == SEEK_HOLE) ? |
| 244 | NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA, |
| 245 | }; |
| 246 | struct nfs42_seek_res res; |
| 247 | struct rpc_message msg = { |
| 248 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK], |
| 249 | .rpc_argp = &args, |
| 250 | .rpc_resp = &res, |
| 251 | }; |
| 252 | struct nfs_server *server = NFS_SERVER(inode); |
| 253 | int status; |
| 254 | |
Anna Schumaker | 878ffa9 | 2014-10-23 14:00:54 -0400 | [diff] [blame] | 255 | if (!nfs_server_capable(inode, NFS_CAP_SEEK)) |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 256 | return -ENOTSUPP; |
| 257 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 258 | status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context, |
| 259 | lock, FMODE_READ); |
Anna Schumaker | 1c6dcbe | 2014-09-26 13:58:48 -0400 | [diff] [blame] | 260 | if (status) |
| 261 | return status; |
| 262 | |
| 263 | nfs_wb_all(inode); |
| 264 | status = nfs4_call_sync(server->client, server, &msg, |
| 265 | &args.seq_args, &res.seq_res, 0); |
| 266 | if (status == -ENOTSUPP) |
| 267 | server->caps &= ~NFS_CAP_SEEK; |
| 268 | if (status) |
| 269 | return status; |
| 270 | |
| 271 | return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes); |
| 272 | } |
Trond Myklebust | be3a5d2 | 2015-06-23 19:51:55 +0800 | [diff] [blame] | 273 | |
J. Bruce Fields | bdcc2cd | 2015-07-23 11:08:43 -0400 | [diff] [blame] | 274 | loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence) |
| 275 | { |
| 276 | struct nfs_server *server = NFS_SERVER(file_inode(filep)); |
| 277 | struct nfs4_exception exception = { }; |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 278 | struct nfs_lock_context *lock; |
J. Bruce Fields | 306a554 | 2015-09-16 17:21:27 -0400 | [diff] [blame] | 279 | loff_t err; |
J. Bruce Fields | bdcc2cd | 2015-07-23 11:08:43 -0400 | [diff] [blame] | 280 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 281 | lock = nfs_get_lock_context(nfs_file_open_context(filep)); |
| 282 | if (IS_ERR(lock)) |
| 283 | return PTR_ERR(lock); |
| 284 | |
| 285 | exception.inode = file_inode(filep); |
| 286 | exception.state = lock->open_context->state; |
| 287 | |
J. Bruce Fields | bdcc2cd | 2015-07-23 11:08:43 -0400 | [diff] [blame] | 288 | do { |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 289 | err = _nfs42_proc_llseek(filep, lock, offset, whence); |
J. Bruce Fields | 306a554 | 2015-09-16 17:21:27 -0400 | [diff] [blame] | 290 | if (err >= 0) |
| 291 | break; |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 292 | if (err == -ENOTSUPP) { |
| 293 | err = -EOPNOTSUPP; |
| 294 | break; |
| 295 | } |
J. Bruce Fields | bdcc2cd | 2015-07-23 11:08:43 -0400 | [diff] [blame] | 296 | err = nfs4_handle_exception(server, err, &exception); |
| 297 | } while (exception.retry); |
| 298 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 299 | nfs_put_lock_context(lock); |
J. Bruce Fields | bdcc2cd | 2015-07-23 11:08:43 -0400 | [diff] [blame] | 300 | return err; |
| 301 | } |
| 302 | |
| 303 | |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 304 | static void |
| 305 | nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata) |
| 306 | { |
| 307 | struct nfs42_layoutstat_data *data = calldata; |
| 308 | struct nfs_server *server = NFS_SERVER(data->args.inode); |
| 309 | |
| 310 | nfs41_setup_sequence(nfs4_get_session(server), &data->args.seq_args, |
| 311 | &data->res.seq_res, task); |
| 312 | } |
| 313 | |
| 314 | static void |
| 315 | nfs42_layoutstat_done(struct rpc_task *task, void *calldata) |
| 316 | { |
| 317 | struct nfs42_layoutstat_data *data = calldata; |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 318 | struct inode *inode = data->inode; |
| 319 | struct pnfs_layout_hdr *lo; |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 320 | |
| 321 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
| 322 | return; |
| 323 | |
Trond Myklebust | 6c5a0d8 | 2015-06-27 11:45:46 -0400 | [diff] [blame] | 324 | switch (task->tk_status) { |
| 325 | case 0: |
| 326 | break; |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 327 | case -NFS4ERR_EXPIRED: |
| 328 | case -NFS4ERR_STALE_STATEID: |
| 329 | case -NFS4ERR_OLD_STATEID: |
| 330 | case -NFS4ERR_BAD_STATEID: |
| 331 | spin_lock(&inode->i_lock); |
| 332 | lo = NFS_I(inode)->layout; |
| 333 | if (lo && nfs4_stateid_match(&data->args.stateid, |
| 334 | &lo->plh_stateid)) { |
| 335 | LIST_HEAD(head); |
| 336 | |
| 337 | /* |
| 338 | * Mark the bad layout state as invalid, then retry |
| 339 | * with the current stateid. |
| 340 | */ |
| 341 | set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags); |
Jeff Layton | 6d597e1 | 2016-05-17 12:28:42 -0400 | [diff] [blame] | 342 | pnfs_mark_matching_lsegs_invalid(lo, &head, NULL, 0); |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 343 | spin_unlock(&inode->i_lock); |
| 344 | pnfs_free_lseg_list(&head); |
| 345 | } else |
| 346 | spin_unlock(&inode->i_lock); |
| 347 | break; |
Trond Myklebust | 6c5a0d8 | 2015-06-27 11:45:46 -0400 | [diff] [blame] | 348 | case -ENOTSUPP: |
| 349 | case -EOPNOTSUPP: |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 350 | NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS; |
Trond Myklebust | 6c5a0d8 | 2015-06-27 11:45:46 -0400 | [diff] [blame] | 351 | default: |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 352 | break; |
Trond Myklebust | 6c5a0d8 | 2015-06-27 11:45:46 -0400 | [diff] [blame] | 353 | } |
Peng Tao | 68d264c | 2015-12-06 20:55:09 -0500 | [diff] [blame] | 354 | |
| 355 | dprintk("%s server returns %d\n", __func__, task->tk_status); |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static void |
| 359 | nfs42_layoutstat_release(void *calldata) |
| 360 | { |
| 361 | struct nfs42_layoutstat_data *data = calldata; |
Peng Tao | 8733408 | 2015-06-23 19:51:57 +0800 | [diff] [blame] | 362 | struct nfs_server *nfss = NFS_SERVER(data->args.inode); |
| 363 | |
| 364 | if (nfss->pnfs_curr_ld->cleanup_layoutstats) |
| 365 | nfss->pnfs_curr_ld->cleanup_layoutstats(data); |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 366 | |
| 367 | pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout); |
Peng Tao | 1bfe3b2 | 2015-06-23 19:52:03 +0800 | [diff] [blame] | 368 | smp_mb__before_atomic(); |
| 369 | clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags); |
| 370 | smp_mb__after_atomic(); |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 371 | nfs_iput_and_deactive(data->inode); |
| 372 | kfree(data->args.devinfo); |
| 373 | kfree(data); |
| 374 | } |
| 375 | |
Trond Myklebust | be3a5d2 | 2015-06-23 19:51:55 +0800 | [diff] [blame] | 376 | static const struct rpc_call_ops nfs42_layoutstat_ops = { |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 377 | .rpc_call_prepare = nfs42_layoutstat_prepare, |
| 378 | .rpc_call_done = nfs42_layoutstat_done, |
| 379 | .rpc_release = nfs42_layoutstat_release, |
Trond Myklebust | be3a5d2 | 2015-06-23 19:51:55 +0800 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | int nfs42_proc_layoutstats_generic(struct nfs_server *server, |
| 383 | struct nfs42_layoutstat_data *data) |
| 384 | { |
| 385 | struct rpc_message msg = { |
| 386 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS], |
| 387 | .rpc_argp = &data->args, |
| 388 | .rpc_resp = &data->res, |
| 389 | }; |
| 390 | struct rpc_task_setup task_setup = { |
| 391 | .rpc_client = server->client, |
| 392 | .rpc_message = &msg, |
| 393 | .callback_ops = &nfs42_layoutstat_ops, |
| 394 | .callback_data = data, |
| 395 | .flags = RPC_TASK_ASYNC, |
| 396 | }; |
| 397 | struct rpc_task *task; |
| 398 | |
Peng Tao | 1b4a4bd | 2015-06-23 19:51:56 +0800 | [diff] [blame] | 399 | data->inode = nfs_igrab_and_active(data->args.inode); |
| 400 | if (!data->inode) { |
| 401 | nfs42_layoutstat_release(data); |
| 402 | return -EAGAIN; |
| 403 | } |
Trond Myklebust | be3a5d2 | 2015-06-23 19:51:55 +0800 | [diff] [blame] | 404 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0); |
| 405 | task = rpc_run_task(&task_setup); |
| 406 | if (IS_ERR(task)) |
| 407 | return PTR_ERR(task); |
| 408 | return 0; |
| 409 | } |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 410 | |
| 411 | 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] | 412 | struct file *dst_f, struct nfs_lock_context *src_lock, |
| 413 | struct nfs_lock_context *dst_lock, loff_t src_offset, |
| 414 | loff_t dst_offset, loff_t count) |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 415 | { |
| 416 | struct inode *src_inode = file_inode(src_f); |
| 417 | struct inode *dst_inode = file_inode(dst_f); |
| 418 | struct nfs_server *server = NFS_SERVER(dst_inode); |
| 419 | struct nfs42_clone_args args = { |
| 420 | .src_fh = NFS_FH(src_inode), |
| 421 | .dst_fh = NFS_FH(dst_inode), |
| 422 | .src_offset = src_offset, |
| 423 | .dst_offset = dst_offset, |
Christoph Hellwig | 9494b2c | 2015-11-13 09:38:45 +0100 | [diff] [blame] | 424 | .count = count, |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 425 | .dst_bitmask = server->cache_consistency_bitmask, |
| 426 | }; |
| 427 | struct nfs42_clone_res res = { |
| 428 | .server = server, |
| 429 | }; |
| 430 | int status; |
| 431 | |
| 432 | msg->rpc_argp = &args; |
| 433 | msg->rpc_resp = &res; |
| 434 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 435 | status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context, |
| 436 | src_lock, FMODE_READ); |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 437 | if (status) |
| 438 | return status; |
| 439 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 440 | status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context, |
| 441 | dst_lock, FMODE_WRITE); |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 442 | if (status) |
| 443 | return status; |
| 444 | |
| 445 | res.dst_fattr = nfs_alloc_fattr(); |
| 446 | if (!res.dst_fattr) |
| 447 | return -ENOMEM; |
| 448 | |
| 449 | status = nfs4_call_sync(server->client, server, msg, |
| 450 | &args.seq_args, &res.seq_res, 0); |
| 451 | if (status == 0) |
| 452 | status = nfs_post_op_update_inode(dst_inode, res.dst_fattr); |
| 453 | |
| 454 | kfree(res.dst_fattr); |
| 455 | return status; |
| 456 | } |
| 457 | |
| 458 | int nfs42_proc_clone(struct file *src_f, struct file *dst_f, |
| 459 | loff_t src_offset, loff_t dst_offset, loff_t count) |
| 460 | { |
| 461 | struct rpc_message msg = { |
| 462 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE], |
| 463 | }; |
| 464 | struct inode *inode = file_inode(src_f); |
| 465 | struct nfs_server *server = NFS_SERVER(file_inode(src_f)); |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 466 | struct nfs_lock_context *src_lock; |
| 467 | struct nfs_lock_context *dst_lock; |
| 468 | struct nfs4_exception src_exception = { }; |
| 469 | struct nfs4_exception dst_exception = { }; |
| 470 | int err, err2; |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 471 | |
| 472 | if (!nfs_server_capable(inode, NFS_CAP_CLONE)) |
| 473 | return -EOPNOTSUPP; |
| 474 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 475 | src_lock = nfs_get_lock_context(nfs_file_open_context(src_f)); |
| 476 | if (IS_ERR(src_lock)) |
| 477 | return PTR_ERR(src_lock); |
| 478 | |
| 479 | src_exception.inode = file_inode(src_f); |
| 480 | src_exception.state = src_lock->open_context->state; |
| 481 | |
| 482 | dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f)); |
| 483 | if (IS_ERR(dst_lock)) { |
| 484 | err = PTR_ERR(dst_lock); |
| 485 | goto out_put_src_lock; |
| 486 | } |
| 487 | |
| 488 | dst_exception.inode = file_inode(dst_f); |
| 489 | dst_exception.state = dst_lock->open_context->state; |
| 490 | |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 491 | do { |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 492 | err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock, |
| 493 | src_offset, dst_offset, count); |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 494 | if (err == -ENOTSUPP || err == -EOPNOTSUPP) { |
| 495 | NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE; |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 496 | err = -EOPNOTSUPP; |
| 497 | break; |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 498 | } |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 499 | |
Christoph Hellwig | 4bdf87e | 2016-02-17 15:48:28 +0100 | [diff] [blame] | 500 | err2 = nfs4_handle_exception(server, err, &src_exception); |
| 501 | err = nfs4_handle_exception(server, err, &dst_exception); |
| 502 | if (!err) |
| 503 | err = err2; |
| 504 | } while (src_exception.retry || dst_exception.retry); |
| 505 | |
| 506 | nfs_put_lock_context(dst_lock); |
| 507 | out_put_src_lock: |
| 508 | nfs_put_lock_context(src_lock); |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 509 | return err; |
Peng Tao | e5341f3 | 2015-09-26 02:24:35 +0800 | [diff] [blame] | 510 | } |