]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1c6dcbe5 AS |
2 | /* |
3 | * Copyright (c) 2014 Anna Schumaker <[email protected]> | |
4 | */ | |
5 | #include <linux/fs.h> | |
6 | #include <linux/sunrpc/sched.h> | |
7 | #include <linux/nfs.h> | |
8 | #include <linux/nfs3.h> | |
9 | #include <linux/nfs4.h> | |
10 | #include <linux/nfs_xdr.h> | |
11 | #include <linux/nfs_fs.h> | |
12 | #include "nfs4_fs.h" | |
13 | #include "nfs42.h" | |
1b4a4bd8 PT |
14 | #include "iostat.h" |
15 | #include "pnfs.h" | |
efc6f4aa | 16 | #include "nfs4session.h" |
1b4a4bd8 PT |
17 | #include "internal.h" |
18 | ||
291e1b94 | 19 | #define NFSDBG_FACILITY NFSDBG_PROC |
c975c209 | 20 | static int nfs42_do_offload_cancel_async(struct file *dst, nfs4_stateid *std); |
1c6dcbe5 | 21 | |
f4ac1674 | 22 | static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, |
4bdf87eb | 23 | struct nfs_lock_context *lock, loff_t offset, loff_t len) |
f4ac1674 AS |
24 | { |
25 | struct inode *inode = file_inode(filep); | |
9a51940b | 26 | struct nfs_server *server = NFS_SERVER(inode); |
f4ac1674 AS |
27 | struct nfs42_falloc_args args = { |
28 | .falloc_fh = NFS_FH(inode), | |
29 | .falloc_offset = offset, | |
30 | .falloc_length = len, | |
9a51940b AS |
31 | .falloc_bitmask = server->cache_consistency_bitmask, |
32 | }; | |
33 | struct nfs42_falloc_res res = { | |
34 | .falloc_server = server, | |
f4ac1674 | 35 | }; |
f4ac1674 AS |
36 | int status; |
37 | ||
38 | msg->rpc_argp = &args; | |
39 | msg->rpc_resp = &res; | |
40 | ||
4bdf87eb CH |
41 | status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context, |
42 | lock, FMODE_WRITE); | |
f4ac1674 AS |
43 | if (status) |
44 | return status; | |
45 | ||
9a51940b AS |
46 | res.falloc_fattr = nfs_alloc_fattr(); |
47 | if (!res.falloc_fattr) | |
48 | return -ENOMEM; | |
49 | ||
50 | status = nfs4_call_sync(server->client, server, msg, | |
51 | &args.seq_args, &res.seq_res, 0); | |
52 | if (status == 0) | |
53 | status = nfs_post_op_update_inode(inode, res.falloc_fattr); | |
54 | ||
55 | kfree(res.falloc_fattr); | |
56 | return status; | |
f4ac1674 AS |
57 | } |
58 | ||
59 | static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, | |
60 | loff_t offset, loff_t len) | |
61 | { | |
62 | struct nfs_server *server = NFS_SERVER(file_inode(filep)); | |
63 | struct nfs4_exception exception = { }; | |
4bdf87eb | 64 | struct nfs_lock_context *lock; |
f4ac1674 AS |
65 | int err; |
66 | ||
4bdf87eb CH |
67 | lock = nfs_get_lock_context(nfs_file_open_context(filep)); |
68 | if (IS_ERR(lock)) | |
69 | return PTR_ERR(lock); | |
70 | ||
71 | exception.inode = file_inode(filep); | |
72 | exception.state = lock->open_context->state; | |
73 | ||
f4ac1674 | 74 | do { |
4bdf87eb CH |
75 | err = _nfs42_proc_fallocate(msg, filep, lock, offset, len); |
76 | if (err == -ENOTSUPP) { | |
77 | err = -EOPNOTSUPP; | |
78 | break; | |
79 | } | |
f4ac1674 AS |
80 | err = nfs4_handle_exception(server, err, &exception); |
81 | } while (exception.retry); | |
82 | ||
4bdf87eb | 83 | nfs_put_lock_context(lock); |
f4ac1674 AS |
84 | return err; |
85 | } | |
86 | ||
87 | int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len) | |
88 | { | |
89 | struct rpc_message msg = { | |
90 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE], | |
91 | }; | |
92 | struct inode *inode = file_inode(filep); | |
93 | int err; | |
94 | ||
95 | if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE)) | |
96 | return -EOPNOTSUPP; | |
97 | ||
5955102c | 98 | inode_lock(inode); |
f830f7dd | 99 | |
f4ac1674 AS |
100 | err = nfs42_proc_fallocate(&msg, filep, offset, len); |
101 | if (err == -EOPNOTSUPP) | |
102 | NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE; | |
f830f7dd | 103 | |
5955102c | 104 | inode_unlock(inode); |
f4ac1674 AS |
105 | return err; |
106 | } | |
107 | ||
624bd5b7 AS |
108 | int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len) |
109 | { | |
110 | struct rpc_message msg = { | |
111 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE], | |
112 | }; | |
113 | struct inode *inode = file_inode(filep); | |
114 | int err; | |
115 | ||
116 | if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE)) | |
117 | return -EOPNOTSUPP; | |
118 | ||
5955102c | 119 | inode_lock(inode); |
1e564d3d TM |
120 | err = nfs_sync_inode(inode); |
121 | if (err) | |
122 | goto out_unlock; | |
f830f7dd | 123 | |
624bd5b7 | 124 | err = nfs42_proc_fallocate(&msg, filep, offset, len); |
9a51940b AS |
125 | if (err == 0) |
126 | truncate_pagecache_range(inode, offset, (offset + len) -1); | |
624bd5b7 AS |
127 | if (err == -EOPNOTSUPP) |
128 | NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE; | |
1e564d3d | 129 | out_unlock: |
5955102c | 130 | inode_unlock(inode); |
624bd5b7 AS |
131 | return err; |
132 | } | |
133 | ||
62164f31 OK |
134 | static int handle_async_copy(struct nfs42_copy_res *res, |
135 | struct nfs_server *server, | |
136 | struct file *src, | |
137 | struct file *dst, | |
138 | nfs4_stateid *src_stateid) | |
139 | { | |
99f2c555 | 140 | struct nfs4_copy_state *copy, *tmp_copy; |
62164f31 | 141 | int status = NFS4_OK; |
bc0c9079 | 142 | bool found_pending = false; |
e4648aa4 | 143 | struct nfs_open_context *ctx = nfs_file_open_context(dst); |
bc0c9079 | 144 | |
99f2c555 OK |
145 | copy = kzalloc(sizeof(struct nfs4_copy_state), GFP_NOFS); |
146 | if (!copy) | |
147 | return -ENOMEM; | |
148 | ||
bc0c9079 | 149 | spin_lock(&server->nfs_client->cl_lock); |
99f2c555 | 150 | list_for_each_entry(tmp_copy, &server->nfs_client->pending_cb_stateids, |
bc0c9079 | 151 | copies) { |
99f2c555 | 152 | if (memcmp(&res->write_res.stateid, &tmp_copy->stateid, |
bc0c9079 OK |
153 | NFS4_STATEID_SIZE)) |
154 | continue; | |
155 | found_pending = true; | |
99f2c555 | 156 | list_del(&tmp_copy->copies); |
bc0c9079 OK |
157 | break; |
158 | } | |
159 | if (found_pending) { | |
160 | spin_unlock(&server->nfs_client->cl_lock); | |
99f2c555 OK |
161 | kfree(copy); |
162 | copy = tmp_copy; | |
bc0c9079 OK |
163 | goto out; |
164 | } | |
62164f31 | 165 | |
62164f31 OK |
166 | memcpy(©->stateid, &res->write_res.stateid, NFS4_STATEID_SIZE); |
167 | init_completion(©->completion); | |
e4648aa4 | 168 | copy->parent_state = ctx->state; |
62164f31 | 169 | |
62164f31 OK |
170 | list_add_tail(©->copies, &server->ss_copies); |
171 | spin_unlock(&server->nfs_client->cl_lock); | |
172 | ||
c975c209 | 173 | status = wait_for_completion_interruptible(©->completion); |
62164f31 OK |
174 | spin_lock(&server->nfs_client->cl_lock); |
175 | list_del_init(©->copies); | |
176 | spin_unlock(&server->nfs_client->cl_lock); | |
c975c209 | 177 | if (status == -ERESTARTSYS) { |
e4648aa4 OK |
178 | goto out_cancel; |
179 | } else if (copy->flags) { | |
180 | status = -EAGAIN; | |
181 | goto out_cancel; | |
c975c209 | 182 | } |
bc0c9079 | 183 | out: |
62164f31 OK |
184 | res->write_res.count = copy->count; |
185 | memcpy(&res->write_res.verifier, ©->verf, sizeof(copy->verf)); | |
186 | status = -copy->error; | |
187 | ||
e4648aa4 OK |
188 | kfree(copy); |
189 | return status; | |
190 | out_cancel: | |
191 | nfs42_do_offload_cancel_async(dst, ©->stateid); | |
62164f31 OK |
192 | kfree(copy); |
193 | return status; | |
194 | } | |
195 | ||
6b8d84e2 OK |
196 | static int process_copy_commit(struct file *dst, loff_t pos_dst, |
197 | struct nfs42_copy_res *res) | |
198 | { | |
199 | struct nfs_commitres cres; | |
200 | int status = -ENOMEM; | |
201 | ||
202 | cres.verf = kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS); | |
203 | if (!cres.verf) | |
204 | goto out; | |
205 | ||
206 | status = nfs4_proc_commit(dst, pos_dst, res->write_res.count, &cres); | |
207 | if (status) | |
208 | goto out_free; | |
209 | if (nfs_write_verifier_cmp(&res->write_res.verifier.verifier, | |
210 | &cres.verf->verifier)) { | |
211 | dprintk("commit verf differs from copy verf\n"); | |
212 | status = -EAGAIN; | |
213 | } | |
214 | out_free: | |
215 | kfree(cres.verf); | |
216 | out: | |
217 | return status; | |
218 | } | |
219 | ||
9d8cacbf | 220 | static ssize_t _nfs42_proc_copy(struct file *src, |
2e72448b | 221 | struct nfs_lock_context *src_lock, |
9d8cacbf | 222 | struct file *dst, |
2e72448b | 223 | struct nfs_lock_context *dst_lock, |
9d8cacbf TM |
224 | struct nfs42_copy_args *args, |
225 | struct nfs42_copy_res *res) | |
2e72448b | 226 | { |
2e72448b AS |
227 | struct rpc_message msg = { |
228 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY], | |
9d8cacbf TM |
229 | .rpc_argp = args, |
230 | .rpc_resp = res, | |
2e72448b AS |
231 | }; |
232 | struct inode *dst_inode = file_inode(dst); | |
233 | struct nfs_server *server = NFS_SERVER(dst_inode); | |
9d8cacbf TM |
234 | loff_t pos_src = args->src_pos; |
235 | loff_t pos_dst = args->dst_pos; | |
236 | size_t count = args->count; | |
1ee48bdd | 237 | ssize_t status; |
2e72448b | 238 | |
9d8cacbf | 239 | status = nfs4_set_rw_stateid(&args->src_stateid, src_lock->open_context, |
2e72448b AS |
240 | src_lock, FMODE_READ); |
241 | if (status) | |
242 | return status; | |
243 | ||
837bb1d7 TM |
244 | status = nfs_filemap_write_and_wait_range(file_inode(src)->i_mapping, |
245 | pos_src, pos_src + (loff_t)count - 1); | |
246 | if (status) | |
247 | return status; | |
248 | ||
9d8cacbf | 249 | status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context, |
2e72448b AS |
250 | dst_lock, FMODE_WRITE); |
251 | if (status) | |
252 | return status; | |
837bb1d7 TM |
253 | |
254 | status = nfs_sync_inode(dst_inode); | |
255 | if (status) | |
256 | return status; | |
2e72448b | 257 | |
62164f31 OK |
258 | res->commit_res.verf = NULL; |
259 | if (args->sync) { | |
260 | res->commit_res.verf = | |
261 | kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS); | |
262 | if (!res->commit_res.verf) | |
263 | return -ENOMEM; | |
264 | } | |
e4648aa4 OK |
265 | set_bit(NFS_CLNT_DST_SSC_COPY_STATE, |
266 | &dst_lock->open_context->state->flags); | |
267 | ||
2e72448b | 268 | status = nfs4_call_sync(server->client, server, &msg, |
9d8cacbf | 269 | &args->seq_args, &res->seq_res, 0); |
2e72448b AS |
270 | if (status == -ENOTSUPP) |
271 | server->caps &= ~NFS_CAP_COPY; | |
272 | if (status) | |
e0926934 | 273 | goto out; |
2e72448b | 274 | |
62164f31 OK |
275 | if (args->sync && |
276 | nfs_write_verifier_cmp(&res->write_res.verifier.verifier, | |
e0926934 OK |
277 | &res->commit_res.verf->verifier)) { |
278 | status = -EAGAIN; | |
279 | goto out; | |
2e72448b AS |
280 | } |
281 | ||
62164f31 OK |
282 | if (!res->synchronous) { |
283 | status = handle_async_copy(res, server, src, dst, | |
284 | &args->src_stateid); | |
285 | if (status) | |
286 | return status; | |
287 | } | |
288 | ||
6b8d84e2 OK |
289 | if ((!res->synchronous || !args->sync) && |
290 | res->write_res.verifier.committed != NFS_FILE_SYNC) { | |
291 | status = process_copy_commit(dst, pos_dst, res); | |
292 | if (status) | |
293 | return status; | |
294 | } | |
295 | ||
2e72448b | 296 | truncate_pagecache_range(dst_inode, pos_dst, |
9d8cacbf | 297 | pos_dst + res->write_res.count); |
2e72448b | 298 | |
e0926934 OK |
299 | status = res->write_res.count; |
300 | out: | |
62164f31 OK |
301 | if (args->sync) |
302 | kfree(res->commit_res.verf); | |
e0926934 | 303 | return status; |
2e72448b AS |
304 | } |
305 | ||
306 | ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src, | |
307 | struct file *dst, loff_t pos_dst, | |
308 | size_t count) | |
309 | { | |
310 | struct nfs_server *server = NFS_SERVER(file_inode(dst)); | |
311 | struct nfs_lock_context *src_lock; | |
312 | struct nfs_lock_context *dst_lock; | |
9d8cacbf TM |
313 | struct nfs42_copy_args args = { |
314 | .src_fh = NFS_FH(file_inode(src)), | |
315 | .src_pos = pos_src, | |
316 | .dst_fh = NFS_FH(file_inode(dst)), | |
317 | .dst_pos = pos_dst, | |
318 | .count = count, | |
62164f31 | 319 | .sync = false, |
9d8cacbf TM |
320 | }; |
321 | struct nfs42_copy_res res; | |
322 | struct nfs4_exception src_exception = { | |
323 | .inode = file_inode(src), | |
324 | .stateid = &args.src_stateid, | |
325 | }; | |
326 | struct nfs4_exception dst_exception = { | |
327 | .inode = file_inode(dst), | |
328 | .stateid = &args.dst_stateid, | |
329 | }; | |
2e72448b AS |
330 | ssize_t err, err2; |
331 | ||
332 | if (!nfs_server_capable(file_inode(dst), NFS_CAP_COPY)) | |
333 | return -EOPNOTSUPP; | |
334 | ||
335 | src_lock = nfs_get_lock_context(nfs_file_open_context(src)); | |
336 | if (IS_ERR(src_lock)) | |
337 | return PTR_ERR(src_lock); | |
338 | ||
2e72448b AS |
339 | src_exception.state = src_lock->open_context->state; |
340 | ||
341 | dst_lock = nfs_get_lock_context(nfs_file_open_context(dst)); | |
342 | if (IS_ERR(dst_lock)) { | |
343 | err = PTR_ERR(dst_lock); | |
344 | goto out_put_src_lock; | |
345 | } | |
346 | ||
2e72448b AS |
347 | dst_exception.state = dst_lock->open_context->state; |
348 | ||
349 | do { | |
ea8ea737 | 350 | inode_lock(file_inode(dst)); |
9d8cacbf TM |
351 | err = _nfs42_proc_copy(src, src_lock, |
352 | dst, dst_lock, | |
353 | &args, &res); | |
ea8ea737 | 354 | inode_unlock(file_inode(dst)); |
2e72448b | 355 | |
9d8cacbf TM |
356 | if (err >= 0) |
357 | break; | |
2e72448b AS |
358 | if (err == -ENOTSUPP) { |
359 | err = -EOPNOTSUPP; | |
360 | break; | |
539f57b3 OK |
361 | } else if (err == -EAGAIN) { |
362 | dst_exception.retry = 1; | |
363 | continue; | |
364 | } else if (err == -NFS4ERR_OFFLOAD_NO_REQS && !args.sync) { | |
365 | args.sync = true; | |
e0926934 OK |
366 | dst_exception.retry = 1; |
367 | continue; | |
2e72448b AS |
368 | } |
369 | ||
370 | err2 = nfs4_handle_exception(server, err, &src_exception); | |
371 | err = nfs4_handle_exception(server, err, &dst_exception); | |
372 | if (!err) | |
373 | err = err2; | |
374 | } while (src_exception.retry || dst_exception.retry); | |
375 | ||
376 | nfs_put_lock_context(dst_lock); | |
377 | out_put_src_lock: | |
378 | nfs_put_lock_context(src_lock); | |
379 | return err; | |
380 | } | |
381 | ||
c975c209 OK |
382 | struct nfs42_offloadcancel_data { |
383 | struct nfs_server *seq_server; | |
384 | struct nfs42_offload_status_args args; | |
385 | struct nfs42_offload_status_res res; | |
386 | }; | |
387 | ||
388 | static void nfs42_offload_cancel_prepare(struct rpc_task *task, void *calldata) | |
389 | { | |
390 | struct nfs42_offloadcancel_data *data = calldata; | |
391 | ||
392 | nfs4_setup_sequence(data->seq_server->nfs_client, | |
393 | &data->args.osa_seq_args, | |
394 | &data->res.osr_seq_res, task); | |
395 | } | |
396 | ||
397 | static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata) | |
398 | { | |
399 | struct nfs42_offloadcancel_data *data = calldata; | |
400 | ||
401 | nfs41_sequence_done(task, &data->res.osr_seq_res); | |
402 | if (task->tk_status && | |
403 | nfs4_async_handle_error(task, data->seq_server, NULL, | |
404 | NULL) == -EAGAIN) | |
405 | rpc_restart_call_prepare(task); | |
406 | } | |
407 | ||
408 | static void nfs42_free_offloadcancel_data(void *data) | |
409 | { | |
410 | kfree(data); | |
411 | } | |
412 | ||
413 | static const struct rpc_call_ops nfs42_offload_cancel_ops = { | |
414 | .rpc_call_prepare = nfs42_offload_cancel_prepare, | |
415 | .rpc_call_done = nfs42_offload_cancel_done, | |
416 | .rpc_release = nfs42_free_offloadcancel_data, | |
417 | }; | |
418 | ||
419 | static int nfs42_do_offload_cancel_async(struct file *dst, | |
420 | nfs4_stateid *stateid) | |
421 | { | |
422 | struct nfs_server *dst_server = NFS_SERVER(file_inode(dst)); | |
423 | struct nfs42_offloadcancel_data *data = NULL; | |
424 | struct nfs_open_context *ctx = nfs_file_open_context(dst); | |
425 | struct rpc_task *task; | |
426 | struct rpc_message msg = { | |
427 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_CANCEL], | |
428 | .rpc_cred = ctx->cred, | |
429 | }; | |
430 | struct rpc_task_setup task_setup_data = { | |
431 | .rpc_client = dst_server->client, | |
432 | .rpc_message = &msg, | |
433 | .callback_ops = &nfs42_offload_cancel_ops, | |
434 | .workqueue = nfsiod_workqueue, | |
435 | .flags = RPC_TASK_ASYNC, | |
436 | }; | |
437 | int status; | |
438 | ||
439 | if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL)) | |
440 | return -EOPNOTSUPP; | |
441 | ||
442 | data = kzalloc(sizeof(struct nfs42_offloadcancel_data), GFP_NOFS); | |
443 | if (data == NULL) | |
444 | return -ENOMEM; | |
445 | ||
446 | data->seq_server = dst_server; | |
447 | data->args.osa_src_fh = NFS_FH(file_inode(dst)); | |
448 | memcpy(&data->args.osa_stateid, stateid, | |
449 | sizeof(data->args.osa_stateid)); | |
450 | msg.rpc_argp = &data->args; | |
451 | msg.rpc_resp = &data->res; | |
452 | task_setup_data.callback_data = data; | |
453 | nfs4_init_sequence(&data->args.osa_seq_args, &data->res.osr_seq_res, | |
454 | 1, 0); | |
455 | task = rpc_run_task(&task_setup_data); | |
456 | if (IS_ERR(task)) | |
457 | return PTR_ERR(task); | |
458 | status = rpc_wait_for_completion_task(task); | |
459 | if (status == -ENOTSUPP) | |
460 | dst_server->caps &= ~NFS_CAP_OFFLOAD_CANCEL; | |
461 | rpc_put_task(task); | |
462 | return status; | |
463 | } | |
464 | ||
4bdf87eb CH |
465 | static loff_t _nfs42_proc_llseek(struct file *filep, |
466 | struct nfs_lock_context *lock, loff_t offset, int whence) | |
1c6dcbe5 AS |
467 | { |
468 | struct inode *inode = file_inode(filep); | |
469 | struct nfs42_seek_args args = { | |
470 | .sa_fh = NFS_FH(inode), | |
471 | .sa_offset = offset, | |
472 | .sa_what = (whence == SEEK_HOLE) ? | |
473 | NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA, | |
474 | }; | |
475 | struct nfs42_seek_res res; | |
476 | struct rpc_message msg = { | |
477 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK], | |
478 | .rpc_argp = &args, | |
479 | .rpc_resp = &res, | |
480 | }; | |
481 | struct nfs_server *server = NFS_SERVER(inode); | |
482 | int status; | |
483 | ||
878ffa9f | 484 | if (!nfs_server_capable(inode, NFS_CAP_SEEK)) |
1c6dcbe5 AS |
485 | return -ENOTSUPP; |
486 | ||
4bdf87eb CH |
487 | status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context, |
488 | lock, FMODE_READ); | |
1c6dcbe5 AS |
489 | if (status) |
490 | return status; | |
491 | ||
e95fc4a0 TM |
492 | status = nfs_filemap_write_and_wait_range(inode->i_mapping, |
493 | offset, LLONG_MAX); | |
494 | if (status) | |
495 | return status; | |
496 | ||
1c6dcbe5 AS |
497 | status = nfs4_call_sync(server->client, server, &msg, |
498 | &args.seq_args, &res.seq_res, 0); | |
499 | if (status == -ENOTSUPP) | |
500 | server->caps &= ~NFS_CAP_SEEK; | |
501 | if (status) | |
502 | return status; | |
503 | ||
504 | return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes); | |
505 | } | |
be3a5d23 | 506 | |
bdcc2cd1 BF |
507 | loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence) |
508 | { | |
509 | struct nfs_server *server = NFS_SERVER(file_inode(filep)); | |
510 | struct nfs4_exception exception = { }; | |
4bdf87eb | 511 | struct nfs_lock_context *lock; |
306a5549 | 512 | loff_t err; |
bdcc2cd1 | 513 | |
4bdf87eb CH |
514 | lock = nfs_get_lock_context(nfs_file_open_context(filep)); |
515 | if (IS_ERR(lock)) | |
516 | return PTR_ERR(lock); | |
517 | ||
518 | exception.inode = file_inode(filep); | |
519 | exception.state = lock->open_context->state; | |
520 | ||
bdcc2cd1 | 521 | do { |
4bdf87eb | 522 | err = _nfs42_proc_llseek(filep, lock, offset, whence); |
306a5549 BF |
523 | if (err >= 0) |
524 | break; | |
4bdf87eb CH |
525 | if (err == -ENOTSUPP) { |
526 | err = -EOPNOTSUPP; | |
527 | break; | |
528 | } | |
bdcc2cd1 BF |
529 | err = nfs4_handle_exception(server, err, &exception); |
530 | } while (exception.retry); | |
531 | ||
4bdf87eb | 532 | nfs_put_lock_context(lock); |
bdcc2cd1 BF |
533 | return err; |
534 | } | |
535 | ||
536 | ||
1b4a4bd8 PT |
537 | static void |
538 | nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata) | |
539 | { | |
540 | struct nfs42_layoutstat_data *data = calldata; | |
9a0fe867 TM |
541 | struct inode *inode = data->inode; |
542 | struct nfs_server *server = NFS_SERVER(inode); | |
543 | struct pnfs_layout_hdr *lo; | |
1b4a4bd8 | 544 | |
9a0fe867 TM |
545 | spin_lock(&inode->i_lock); |
546 | lo = NFS_I(inode)->layout; | |
547 | if (!pnfs_layout_is_valid(lo)) { | |
548 | spin_unlock(&inode->i_lock); | |
549 | rpc_exit(task, 0); | |
550 | return; | |
551 | } | |
552 | nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid); | |
553 | spin_unlock(&inode->i_lock); | |
6de7e12f AS |
554 | nfs4_setup_sequence(server->nfs_client, &data->args.seq_args, |
555 | &data->res.seq_res, task); | |
1b4a4bd8 PT |
556 | } |
557 | ||
558 | static void | |
559 | nfs42_layoutstat_done(struct rpc_task *task, void *calldata) | |
560 | { | |
561 | struct nfs42_layoutstat_data *data = calldata; | |
68d264cf PT |
562 | struct inode *inode = data->inode; |
563 | struct pnfs_layout_hdr *lo; | |
1b4a4bd8 PT |
564 | |
565 | if (!nfs4_sequence_done(task, &data->res.seq_res)) | |
566 | return; | |
567 | ||
6c5a0d89 TM |
568 | switch (task->tk_status) { |
569 | case 0: | |
570 | break; | |
cf61eb26 TM |
571 | case -NFS4ERR_BADHANDLE: |
572 | case -ESTALE: | |
573 | pnfs_destroy_layout(NFS_I(inode)); | |
574 | break; | |
68d264cf | 575 | case -NFS4ERR_EXPIRED: |
206b3bb5 TM |
576 | case -NFS4ERR_ADMIN_REVOKED: |
577 | case -NFS4ERR_DELEG_REVOKED: | |
68d264cf | 578 | case -NFS4ERR_STALE_STATEID: |
68d264cf PT |
579 | case -NFS4ERR_BAD_STATEID: |
580 | spin_lock(&inode->i_lock); | |
581 | lo = NFS_I(inode)->layout; | |
9a0fe867 TM |
582 | if (pnfs_layout_is_valid(lo) && |
583 | nfs4_stateid_match(&data->args.stateid, | |
68d264cf PT |
584 | &lo->plh_stateid)) { |
585 | LIST_HEAD(head); | |
586 | ||
587 | /* | |
588 | * Mark the bad layout state as invalid, then retry | |
589 | * with the current stateid. | |
590 | */ | |
5f46be04 | 591 | pnfs_mark_layout_stateid_invalid(lo, &head); |
68d264cf PT |
592 | spin_unlock(&inode->i_lock); |
593 | pnfs_free_lseg_list(&head); | |
1f18b82c | 594 | nfs_commit_inode(inode, 0); |
68d264cf PT |
595 | } else |
596 | spin_unlock(&inode->i_lock); | |
597 | break; | |
9a0fe867 TM |
598 | case -NFS4ERR_OLD_STATEID: |
599 | spin_lock(&inode->i_lock); | |
600 | lo = NFS_I(inode)->layout; | |
601 | if (pnfs_layout_is_valid(lo) && | |
602 | nfs4_stateid_match_other(&data->args.stateid, | |
603 | &lo->plh_stateid)) { | |
604 | /* Do we need to delay before resending? */ | |
605 | if (!nfs4_stateid_is_newer(&lo->plh_stateid, | |
606 | &data->args.stateid)) | |
607 | rpc_delay(task, HZ); | |
608 | rpc_restart_call_prepare(task); | |
609 | } | |
610 | spin_unlock(&inode->i_lock); | |
611 | break; | |
6c5a0d89 TM |
612 | case -ENOTSUPP: |
613 | case -EOPNOTSUPP: | |
68d264cf | 614 | NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS; |
6c5a0d89 | 615 | } |
1b4a4bd8 PT |
616 | } |
617 | ||
618 | static void | |
619 | nfs42_layoutstat_release(void *calldata) | |
620 | { | |
621 | struct nfs42_layoutstat_data *data = calldata; | |
422c93c8 TM |
622 | struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo; |
623 | int i; | |
8733408d | 624 | |
422c93c8 TM |
625 | for (i = 0; i < data->args.num_dev; i++) { |
626 | if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free) | |
627 | devinfo[i].ld_private.ops->free(&devinfo[i].ld_private); | |
628 | } | |
1b4a4bd8 PT |
629 | |
630 | pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout); | |
1bfe3b25 PT |
631 | smp_mb__before_atomic(); |
632 | clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags); | |
633 | smp_mb__after_atomic(); | |
1b4a4bd8 PT |
634 | nfs_iput_and_deactive(data->inode); |
635 | kfree(data->args.devinfo); | |
636 | kfree(data); | |
637 | } | |
638 | ||
be3a5d23 | 639 | static const struct rpc_call_ops nfs42_layoutstat_ops = { |
1b4a4bd8 PT |
640 | .rpc_call_prepare = nfs42_layoutstat_prepare, |
641 | .rpc_call_done = nfs42_layoutstat_done, | |
642 | .rpc_release = nfs42_layoutstat_release, | |
be3a5d23 TM |
643 | }; |
644 | ||
645 | int nfs42_proc_layoutstats_generic(struct nfs_server *server, | |
646 | struct nfs42_layoutstat_data *data) | |
647 | { | |
648 | struct rpc_message msg = { | |
649 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS], | |
650 | .rpc_argp = &data->args, | |
651 | .rpc_resp = &data->res, | |
652 | }; | |
653 | struct rpc_task_setup task_setup = { | |
654 | .rpc_client = server->client, | |
655 | .rpc_message = &msg, | |
656 | .callback_ops = &nfs42_layoutstat_ops, | |
657 | .callback_data = data, | |
658 | .flags = RPC_TASK_ASYNC, | |
659 | }; | |
660 | struct rpc_task *task; | |
661 | ||
1b4a4bd8 PT |
662 | data->inode = nfs_igrab_and_active(data->args.inode); |
663 | if (!data->inode) { | |
664 | nfs42_layoutstat_release(data); | |
665 | return -EAGAIN; | |
666 | } | |
fba83f34 | 667 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0); |
be3a5d23 TM |
668 | task = rpc_run_task(&task_setup); |
669 | if (IS_ERR(task)) | |
670 | return PTR_ERR(task); | |
3f807e5a | 671 | rpc_put_task(task); |
be3a5d23 TM |
672 | return 0; |
673 | } | |
e5341f3a PT |
674 | |
675 | static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f, | |
4bdf87eb CH |
676 | struct file *dst_f, struct nfs_lock_context *src_lock, |
677 | struct nfs_lock_context *dst_lock, loff_t src_offset, | |
678 | loff_t dst_offset, loff_t count) | |
e5341f3a PT |
679 | { |
680 | struct inode *src_inode = file_inode(src_f); | |
681 | struct inode *dst_inode = file_inode(dst_f); | |
682 | struct nfs_server *server = NFS_SERVER(dst_inode); | |
683 | struct nfs42_clone_args args = { | |
684 | .src_fh = NFS_FH(src_inode), | |
685 | .dst_fh = NFS_FH(dst_inode), | |
686 | .src_offset = src_offset, | |
687 | .dst_offset = dst_offset, | |
9494b2ce | 688 | .count = count, |
e5341f3a PT |
689 | .dst_bitmask = server->cache_consistency_bitmask, |
690 | }; | |
691 | struct nfs42_clone_res res = { | |
692 | .server = server, | |
693 | }; | |
694 | int status; | |
695 | ||
696 | msg->rpc_argp = &args; | |
697 | msg->rpc_resp = &res; | |
698 | ||
4bdf87eb CH |
699 | status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context, |
700 | src_lock, FMODE_READ); | |
e5341f3a PT |
701 | if (status) |
702 | return status; | |
703 | ||
4bdf87eb CH |
704 | status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context, |
705 | dst_lock, FMODE_WRITE); | |
e5341f3a PT |
706 | if (status) |
707 | return status; | |
708 | ||
709 | res.dst_fattr = nfs_alloc_fattr(); | |
710 | if (!res.dst_fattr) | |
711 | return -ENOMEM; | |
712 | ||
713 | status = nfs4_call_sync(server->client, server, msg, | |
714 | &args.seq_args, &res.seq_res, 0); | |
715 | if (status == 0) | |
716 | status = nfs_post_op_update_inode(dst_inode, res.dst_fattr); | |
717 | ||
718 | kfree(res.dst_fattr); | |
719 | return status; | |
720 | } | |
721 | ||
722 | int nfs42_proc_clone(struct file *src_f, struct file *dst_f, | |
723 | loff_t src_offset, loff_t dst_offset, loff_t count) | |
724 | { | |
725 | struct rpc_message msg = { | |
726 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE], | |
727 | }; | |
728 | struct inode *inode = file_inode(src_f); | |
729 | struct nfs_server *server = NFS_SERVER(file_inode(src_f)); | |
4bdf87eb CH |
730 | struct nfs_lock_context *src_lock; |
731 | struct nfs_lock_context *dst_lock; | |
732 | struct nfs4_exception src_exception = { }; | |
733 | struct nfs4_exception dst_exception = { }; | |
734 | int err, err2; | |
e5341f3a PT |
735 | |
736 | if (!nfs_server_capable(inode, NFS_CAP_CLONE)) | |
737 | return -EOPNOTSUPP; | |
738 | ||
4bdf87eb CH |
739 | src_lock = nfs_get_lock_context(nfs_file_open_context(src_f)); |
740 | if (IS_ERR(src_lock)) | |
741 | return PTR_ERR(src_lock); | |
742 | ||
743 | src_exception.inode = file_inode(src_f); | |
744 | src_exception.state = src_lock->open_context->state; | |
745 | ||
746 | dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f)); | |
747 | if (IS_ERR(dst_lock)) { | |
748 | err = PTR_ERR(dst_lock); | |
749 | goto out_put_src_lock; | |
750 | } | |
751 | ||
752 | dst_exception.inode = file_inode(dst_f); | |
753 | dst_exception.state = dst_lock->open_context->state; | |
754 | ||
e5341f3a | 755 | do { |
4bdf87eb CH |
756 | err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock, |
757 | src_offset, dst_offset, count); | |
e5341f3a PT |
758 | if (err == -ENOTSUPP || err == -EOPNOTSUPP) { |
759 | NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE; | |
4bdf87eb CH |
760 | err = -EOPNOTSUPP; |
761 | break; | |
e5341f3a | 762 | } |
e5341f3a | 763 | |
4bdf87eb CH |
764 | err2 = nfs4_handle_exception(server, err, &src_exception); |
765 | err = nfs4_handle_exception(server, err, &dst_exception); | |
766 | if (!err) | |
767 | err = err2; | |
768 | } while (src_exception.retry || dst_exception.retry); | |
e5341f3a | 769 | |
4bdf87eb CH |
770 | nfs_put_lock_context(dst_lock); |
771 | out_put_src_lock: | |
772 | nfs_put_lock_context(src_lock); | |
773 | return err; | |
e5341f3a | 774 | } |