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