spi: efm32: Convert to use GPIO descriptors
[linux/fpc-iii.git] / fs / nfs / nfs42proc.c
blobe2ae54b35dfe1ff5da1bd1d656327b7b245e2cda
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
4 */
5 #include <linux/fs.h>
6 #include <linux/sunrpc/addr.h>
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"
15 #include "iostat.h"
16 #include "pnfs.h"
17 #include "nfs4session.h"
18 #include "internal.h"
19 #include "delegation.h"
21 #define NFSDBG_FACILITY NFSDBG_PROC
22 static int nfs42_do_offload_cancel_async(struct file *dst, nfs4_stateid *std);
24 static void nfs42_set_netaddr(struct file *filep, struct nfs42_netaddr *naddr)
26 struct nfs_client *clp = (NFS_SERVER(file_inode(filep)))->nfs_client;
27 unsigned short port = 2049;
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();
43 static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
44 struct nfs_lock_context *lock, loff_t offset, loff_t len)
46 struct inode *inode = file_inode(filep);
47 struct nfs_server *server = NFS_SERVER(inode);
48 struct nfs42_falloc_args args = {
49 .falloc_fh = NFS_FH(inode),
50 .falloc_offset = offset,
51 .falloc_length = len,
52 .falloc_bitmask = nfs4_fattr_bitmap,
54 struct nfs42_falloc_res res = {
55 .falloc_server = server,
57 int status;
59 msg->rpc_argp = &args;
60 msg->rpc_resp = &res;
62 status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
63 lock, FMODE_WRITE);
64 if (status) {
65 if (status == -EAGAIN)
66 status = -NFS4ERR_BAD_STATEID;
67 return status;
70 res.falloc_fattr = nfs_alloc_fattr();
71 if (!res.falloc_fattr)
72 return -ENOMEM;
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);
79 kfree(res.falloc_fattr);
80 return status;
83 static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
84 loff_t offset, loff_t len)
86 struct nfs_server *server = NFS_SERVER(file_inode(filep));
87 struct nfs4_exception exception = { };
88 struct nfs_lock_context *lock;
89 int err;
91 lock = nfs_get_lock_context(nfs_file_open_context(filep));
92 if (IS_ERR(lock))
93 return PTR_ERR(lock);
95 exception.inode = file_inode(filep);
96 exception.state = lock->open_context->state;
98 do {
99 err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
100 if (err == -ENOTSUPP) {
101 err = -EOPNOTSUPP;
102 break;
104 err = nfs4_handle_exception(server, err, &exception);
105 } while (exception.retry);
107 nfs_put_lock_context(lock);
108 return err;
111 int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
113 struct rpc_message msg = {
114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
116 struct inode *inode = file_inode(filep);
117 int err;
119 if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
120 return -EOPNOTSUPP;
122 inode_lock(inode);
124 err = nfs42_proc_fallocate(&msg, filep, offset, len);
125 if (err == -EOPNOTSUPP)
126 NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
128 inode_unlock(inode);
129 return err;
132 int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
134 struct rpc_message msg = {
135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
137 struct inode *inode = file_inode(filep);
138 int err;
140 if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
141 return -EOPNOTSUPP;
143 inode_lock(inode);
144 err = nfs_sync_inode(inode);
145 if (err)
146 goto out_unlock;
148 err = nfs42_proc_fallocate(&msg, filep, offset, len);
149 if (err == 0)
150 truncate_pagecache_range(inode, offset, (offset + len) -1);
151 if (err == -EOPNOTSUPP)
152 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
153 out_unlock:
154 inode_unlock(inode);
155 return err;
158 static int handle_async_copy(struct nfs42_copy_res *res,
159 struct nfs_server *dst_server,
160 struct nfs_server *src_server,
161 struct file *src,
162 struct file *dst,
163 nfs4_stateid *src_stateid,
164 bool *restart)
166 struct nfs4_copy_state *copy, *tmp_copy;
167 int status = NFS4_OK;
168 bool found_pending = false;
169 struct nfs_open_context *dst_ctx = nfs_file_open_context(dst);
170 struct nfs_open_context *src_ctx = nfs_file_open_context(src);
172 copy = kzalloc(sizeof(struct nfs4_copy_state), GFP_NOFS);
173 if (!copy)
174 return -ENOMEM;
176 spin_lock(&dst_server->nfs_client->cl_lock);
177 list_for_each_entry(tmp_copy,
178 &dst_server->nfs_client->pending_cb_stateids,
179 copies) {
180 if (memcmp(&res->write_res.stateid, &tmp_copy->stateid,
181 NFS4_STATEID_SIZE))
182 continue;
183 found_pending = true;
184 list_del(&tmp_copy->copies);
185 break;
187 if (found_pending) {
188 spin_unlock(&dst_server->nfs_client->cl_lock);
189 kfree(copy);
190 copy = tmp_copy;
191 goto out;
194 memcpy(&copy->stateid, &res->write_res.stateid, NFS4_STATEID_SIZE);
195 init_completion(&copy->completion);
196 copy->parent_dst_state = dst_ctx->state;
197 copy->parent_src_state = src_ctx->state;
199 list_add_tail(&copy->copies, &dst_server->ss_copies);
200 spin_unlock(&dst_server->nfs_client->cl_lock);
202 if (dst_server != src_server) {
203 spin_lock(&src_server->nfs_client->cl_lock);
204 list_add_tail(&copy->src_copies, &src_server->ss_copies);
205 spin_unlock(&src_server->nfs_client->cl_lock);
208 status = wait_for_completion_interruptible(&copy->completion);
209 spin_lock(&dst_server->nfs_client->cl_lock);
210 list_del_init(&copy->copies);
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(&copy->src_copies);
215 spin_unlock(&src_server->nfs_client->cl_lock);
217 if (status == -ERESTARTSYS) {
218 goto out_cancel;
219 } else if (copy->flags || copy->error == NFS4ERR_PARTNER_NO_AUTH) {
220 status = -EAGAIN;
221 *restart = true;
222 goto out_cancel;
224 out:
225 res->write_res.count = copy->count;
226 memcpy(&res->write_res.verifier, &copy->verf, sizeof(copy->verf));
227 status = -copy->error;
229 out_free:
230 kfree(copy);
231 return status;
232 out_cancel:
233 nfs42_do_offload_cancel_async(dst, &copy->stateid);
234 if (!nfs42_files_from_same_server(src, dst))
235 nfs42_do_offload_cancel_async(src, src_stateid);
236 goto out_free;
239 static int process_copy_commit(struct file *dst, loff_t pos_dst,
240 struct nfs42_copy_res *res)
242 struct nfs_commitres cres;
243 int status = -ENOMEM;
245 cres.verf = kzalloc(sizeof(struct nfs_writeverf), GFP_NOFS);
246 if (!cres.verf)
247 goto out;
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;
257 out_free:
258 kfree(cres.verf);
259 out:
260 return status;
263 static ssize_t _nfs42_proc_copy(struct file *src,
264 struct nfs_lock_context *src_lock,
265 struct file *dst,
266 struct nfs_lock_context *dst_lock,
267 struct nfs42_copy_args *args,
268 struct nfs42_copy_res *res,
269 struct nl4_server *nss,
270 nfs4_stateid *cnr_stateid,
271 bool *restart)
273 struct rpc_message msg = {
274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
275 .rpc_argp = args,
276 .rpc_resp = res,
278 struct inode *dst_inode = file_inode(dst);
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);
282 loff_t pos_src = args->src_pos;
283 loff_t pos_dst = args->dst_pos;
284 size_t count = args->count;
285 ssize_t status;
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);
293 if (status) {
294 if (status == -EAGAIN)
295 status = -NFS4ERR_BAD_STATEID;
296 return status;
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;
304 status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context,
305 dst_lock, FMODE_WRITE);
306 if (status) {
307 if (status == -EAGAIN)
308 status = -NFS4ERR_BAD_STATEID;
309 return status;
312 status = nfs_sync_inode(dst_inode);
313 if (status)
314 return status;
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;
323 set_bit(NFS_CLNT_SRC_SSC_COPY_STATE,
324 &src_lock->open_context->state->flags);
325 set_bit(NFS_CLNT_DST_SSC_COPY_STATE,
326 &dst_lock->open_context->state->flags);
328 status = nfs4_call_sync(dst_server->client, dst_server, &msg,
329 &args->seq_args, &res->seq_res, 0);
330 if (status == -ENOTSUPP)
331 dst_server->caps &= ~NFS_CAP_COPY;
332 if (status)
333 goto out;
335 if (args->sync &&
336 nfs_write_verifier_cmp(&res->write_res.verifier.verifier,
337 &res->commit_res.verf->verifier)) {
338 status = -EAGAIN;
339 goto out;
342 if (!res->synchronous) {
343 status = handle_async_copy(res, dst_server, src_server, src,
344 dst, &args->src_stateid, restart);
345 if (status)
346 goto out;
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)
353 goto out;
356 truncate_pagecache_range(dst_inode, pos_dst,
357 pos_dst + res->write_res.count);
359 status = res->write_res.count;
360 out:
361 if (args->sync)
362 kfree(res->commit_res.verf);
363 return status;
366 ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
367 struct file *dst, loff_t pos_dst, size_t count,
368 struct nl4_server *nss,
369 nfs4_stateid *cnr_stateid, bool sync)
371 struct nfs_server *server = NFS_SERVER(file_inode(dst));
372 struct nfs_lock_context *src_lock;
373 struct nfs_lock_context *dst_lock;
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,
380 .sync = sync,
382 struct nfs42_copy_res res;
383 struct nfs4_exception src_exception = {
384 .inode = file_inode(src),
385 .stateid = &args.src_stateid,
387 struct nfs4_exception dst_exception = {
388 .inode = file_inode(dst),
389 .stateid = &args.dst_stateid,
391 ssize_t err, err2;
392 bool restart = false;
394 src_lock = nfs_get_lock_context(nfs_file_open_context(src));
395 if (IS_ERR(src_lock))
396 return PTR_ERR(src_lock);
398 src_exception.state = src_lock->open_context->state;
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;
406 dst_exception.state = dst_lock->open_context->state;
408 do {
409 inode_lock(file_inode(dst));
410 err = _nfs42_proc_copy(src, src_lock,
411 dst, dst_lock,
412 &args, &res,
413 nss, cnr_stateid, &restart);
414 inode_unlock(file_inode(dst));
416 if (err >= 0)
417 break;
418 if (err == -ENOTSUPP &&
419 nfs42_files_from_same_server(src, dst)) {
420 err = -EOPNOTSUPP;
421 break;
422 } else if (err == -EAGAIN) {
423 if (!restart) {
424 dst_exception.retry = 1;
425 continue;
427 break;
428 } else if (err == -NFS4ERR_OFFLOAD_NO_REQS && !args.sync) {
429 args.sync = true;
430 dst_exception.retry = 1;
431 continue;
432 } else if ((err == -ESTALE ||
433 err == -NFS4ERR_OFFLOAD_DENIED ||
434 err == -ENOTSUPP) &&
435 !nfs42_files_from_same_server(src, dst)) {
436 nfs42_do_offload_cancel_async(src, &args.src_stateid);
437 err = -EOPNOTSUPP;
438 break;
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);
447 nfs_put_lock_context(dst_lock);
448 out_put_src_lock:
449 nfs_put_lock_context(src_lock);
450 return err;
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;
459 static void nfs42_offload_cancel_prepare(struct rpc_task *task, void *calldata)
461 struct nfs42_offloadcancel_data *data = calldata;
463 nfs4_setup_sequence(data->seq_server->nfs_client,
464 &data->args.osa_seq_args,
465 &data->res.osr_seq_res, task);
468 static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata)
470 struct nfs42_offloadcancel_data *data = calldata;
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);
479 static void nfs42_free_offloadcancel_data(void *data)
481 kfree(data);
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,
490 static int nfs42_do_offload_cancel_async(struct file *dst,
491 nfs4_stateid *stateid)
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,
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,
508 int status;
510 if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL))
511 return -EOPNOTSUPP;
513 data = kzalloc(sizeof(struct nfs42_offloadcancel_data), GFP_NOFS);
514 if (data == NULL)
515 return -ENOMEM;
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;
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)
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,
546 int status;
547 struct nfs_open_context *ctx;
548 struct nfs_lock_context *l_ctx;
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);
555 status = nfs4_set_rw_stateid(&args->cna_src_stateid, ctx, l_ctx,
556 FMODE_READ);
557 nfs_put_lock_context(l_ctx);
558 if (status) {
559 if (status == -EAGAIN)
560 status = -NFS4ERR_BAD_STATEID;
561 return status;
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;
569 put_nfs_open_context(nfs_file_open_context(src));
570 return status;
573 int nfs42_proc_copy_notify(struct file *src, struct file *dst,
574 struct nfs42_copy_notify_res *res)
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),
581 int status;
583 if (!(src_server->caps & NFS_CAP_COPY_NOTIFY))
584 return -EOPNOTSUPP;
586 args = kzalloc(sizeof(struct nfs42_copy_notify_args), GFP_NOFS);
587 if (args == NULL)
588 return -ENOMEM;
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;
595 do {
596 status = _nfs42_proc_copy_notify(src, dst, args, res);
597 if (status == -ENOTSUPP) {
598 status = -EOPNOTSUPP;
599 goto out;
601 status = nfs4_handle_exception(src_server, status, &exception);
602 } while (exception.retry);
604 out:
605 kfree(args);
606 return status;
609 static loff_t _nfs42_proc_llseek(struct file *filep,
610 struct nfs_lock_context *lock, loff_t offset, int whence)
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,
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,
625 struct nfs_server *server = NFS_SERVER(inode);
626 int status;
628 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
629 return -ENOTSUPP;
631 status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
632 lock, FMODE_READ);
633 if (status) {
634 if (status == -EAGAIN)
635 status = -NFS4ERR_BAD_STATEID;
636 return status;
639 status = nfs_filemap_write_and_wait_range(inode->i_mapping,
640 offset, LLONG_MAX);
641 if (status)
642 return status;
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;
651 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
654 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
656 struct nfs_server *server = NFS_SERVER(file_inode(filep));
657 struct nfs4_exception exception = { };
658 struct nfs_lock_context *lock;
659 loff_t err;
661 lock = nfs_get_lock_context(nfs_file_open_context(filep));
662 if (IS_ERR(lock))
663 return PTR_ERR(lock);
665 exception.inode = file_inode(filep);
666 exception.state = lock->open_context->state;
668 do {
669 err = _nfs42_proc_llseek(filep, lock, offset, whence);
670 if (err >= 0)
671 break;
672 if (err == -ENOTSUPP) {
673 err = -EOPNOTSUPP;
674 break;
676 err = nfs4_handle_exception(server, err, &exception);
677 } while (exception.retry);
679 nfs_put_lock_context(lock);
680 return err;
684 static void
685 nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
687 struct nfs42_layoutstat_data *data = calldata;
688 struct inode *inode = data->inode;
689 struct nfs_server *server = NFS_SERVER(inode);
690 struct pnfs_layout_hdr *lo;
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;
699 nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
700 spin_unlock(&inode->i_lock);
701 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
702 &data->res.seq_res, task);
705 static void
706 nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
708 struct nfs42_layoutstat_data *data = calldata;
709 struct inode *inode = data->inode;
710 struct pnfs_layout_hdr *lo;
712 if (!nfs4_sequence_done(task, &data->res.seq_res))
713 return;
715 switch (task->tk_status) {
716 case 0:
717 break;
718 case -NFS4ERR_BADHANDLE:
719 case -ESTALE:
720 pnfs_destroy_layout(NFS_I(inode));
721 break;
722 case -NFS4ERR_EXPIRED:
723 case -NFS4ERR_ADMIN_REVOKED:
724 case -NFS4ERR_DELEG_REVOKED:
725 case -NFS4ERR_STALE_STATEID:
726 case -NFS4ERR_BAD_STATEID:
727 spin_lock(&inode->i_lock);
728 lo = NFS_I(inode)->layout;
729 if (pnfs_layout_is_valid(lo) &&
730 nfs4_stateid_match(&data->args.stateid,
731 &lo->plh_stateid)) {
732 LIST_HEAD(head);
735 * Mark the bad layout state as invalid, then retry
736 * with the current stateid.
738 pnfs_mark_layout_stateid_invalid(lo, &head);
739 spin_unlock(&inode->i_lock);
740 pnfs_free_lseg_list(&head);
741 nfs_commit_inode(inode, 0);
742 } else
743 spin_unlock(&inode->i_lock);
744 break;
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);
757 spin_unlock(&inode->i_lock);
758 break;
759 case -ENOTSUPP:
760 case -EOPNOTSUPP:
761 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
765 static void
766 nfs42_layoutstat_release(void *calldata)
768 struct nfs42_layoutstat_data *data = calldata;
769 struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo;
770 int i;
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);
777 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
778 smp_mb__before_atomic();
779 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
780 smp_mb__after_atomic();
781 nfs_iput_and_deactive(data->inode);
782 kfree(data->args.devinfo);
783 kfree(data);
786 static const struct rpc_call_ops nfs42_layoutstat_ops = {
787 .rpc_call_prepare = nfs42_layoutstat_prepare,
788 .rpc_call_done = nfs42_layoutstat_done,
789 .rpc_release = nfs42_layoutstat_release,
792 int nfs42_proc_layoutstats_generic(struct nfs_server *server,
793 struct nfs42_layoutstat_data *data)
795 struct rpc_message msg = {
796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
797 .rpc_argp = &data->args,
798 .rpc_resp = &data->res,
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,
807 struct rpc_task *task;
809 data->inode = nfs_igrab_and_active(data->args.inode);
810 if (!data->inode) {
811 nfs42_layoutstat_release(data);
812 return -EAGAIN;
814 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
815 task = rpc_run_task(&task_setup);
816 if (IS_ERR(task))
817 return PTR_ERR(task);
818 rpc_put_task(task);
819 return 0;
822 static struct nfs42_layouterror_data *
823 nfs42_alloc_layouterror_data(struct pnfs_layout_segment *lseg, gfp_t gfp_flags)
825 struct nfs42_layouterror_data *data;
826 struct inode *inode = lseg->pls_layout->plh_inode;
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);
837 kfree(data);
839 return NULL;
842 static void
843 nfs42_free_layouterror_data(struct nfs42_layouterror_data *data)
845 pnfs_put_lseg(data->lseg);
846 nfs_iput_and_deactive(data->inode);
847 kfree(data);
850 static void
851 nfs42_layouterror_prepare(struct rpc_task *task, void *calldata)
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;
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;
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);
873 static void
874 nfs42_layouterror_done(struct rpc_task *task, void *calldata)
876 struct nfs42_layouterror_data *data = calldata;
877 struct inode *inode = data->inode;
878 struct pnfs_layout_hdr *lo = data->lseg->pls_layout;
880 if (!nfs4_sequence_done(task, &data->res.seq_res))
881 return;
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);
902 * Mark the bad layout state as invalid, then retry
903 * with the current stateid.
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);
923 spin_unlock(&inode->i_lock);
924 break;
925 case -ENOTSUPP:
926 case -EOPNOTSUPP:
927 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTERROR;
931 static void
932 nfs42_layouterror_release(void *calldata)
934 struct nfs42_layouterror_data *data = calldata;
936 nfs42_free_layouterror_data(data);
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,
945 int nfs42_proc_layouterror(struct pnfs_layout_segment *lseg,
946 const struct nfs42_layout_error *errors, size_t n)
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],
954 struct rpc_task_setup task_setup = {
955 .rpc_message = &msg,
956 .callback_ops = &nfs42_layouterror_ops,
957 .flags = RPC_TASK_ASYNC,
959 unsigned int i;
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++;
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;
984 EXPORT_SYMBOL_GPL(nfs42_proc_layouterror);
986 static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
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)
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,
999 .count = count,
1000 .dst_bitmask = server->cache_consistency_bitmask,
1002 struct nfs42_clone_res res = {
1003 .server = server,
1005 int status;
1007 msg->rpc_argp = &args;
1008 msg->rpc_resp = &res;
1010 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
1011 src_lock, FMODE_READ);
1012 if (status) {
1013 if (status == -EAGAIN)
1014 status = -NFS4ERR_BAD_STATEID;
1015 return status;
1017 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
1018 dst_lock, FMODE_WRITE);
1019 if (status) {
1020 if (status == -EAGAIN)
1021 status = -NFS4ERR_BAD_STATEID;
1022 return status;
1025 res.dst_fattr = nfs_alloc_fattr();
1026 if (!res.dst_fattr)
1027 return -ENOMEM;
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);
1034 kfree(res.dst_fattr);
1035 return status;
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)
1041 struct rpc_message msg = {
1042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
1044 struct inode *inode = file_inode(src_f);
1045 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
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;
1052 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
1053 return -EOPNOTSUPP;
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);
1059 src_exception.inode = file_inode(src_f);
1060 src_exception.state = src_lock->open_context->state;
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;
1068 dst_exception.inode = file_inode(dst_f);
1069 dst_exception.state = dst_lock->open_context->state;
1071 do {
1072 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
1073 src_offset, dst_offset, count);
1074 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
1075 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
1076 err = -EOPNOTSUPP;
1077 break;
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);
1086 nfs_put_lock_context(dst_lock);
1087 out_put_src_lock:
1088 nfs_put_lock_context(src_lock);
1089 return err;