NFSv4.1 Check invalid deviceid upon slot table waitq wakeup
[linux-2.6.git] / fs / nfs / nfs4filelayout.c
blob1b9bedb89620dcb7331166b3ce726890f1a3e280
1 /*
2 * Module for the pnfs nfs4 file layout driver.
3 * Defines all I/O and Policy interface operations, plus code
4 * to register itself with the pNFS client.
6 * Copyright (c) 2002
7 * The Regents of the University of Michigan
8 * All Rights Reserved
10 * Dean Hildebrand <dhildebz@umich.edu>
12 * Permission is granted to use, copy, create derivative works, and
13 * redistribute this software and such derivative works for any purpose,
14 * so long as the name of the University of Michigan is not used in
15 * any advertising or publicity pertaining to the use or distribution
16 * of this software without specific, written prior authorization. If
17 * the above copyright notice or any other identification of the
18 * University of Michigan is included in any copy of any portion of
19 * this software, then the disclaimer below must also be included.
21 * This software is provided as is, without representation or warranty
22 * of any kind either express or implied, including without limitation
23 * the implied warranties of merchantability, fitness for a particular
24 * purpose, or noninfringement. The Regents of the University of
25 * Michigan shall not be liable for any damages, including special,
26 * indirect, incidental, or consequential damages, with respect to any
27 * claim arising out of or in connection with the use of the software,
28 * even if it has been or is hereafter advised of the possibility of
29 * such damages.
32 #include <linux/nfs_fs.h>
33 #include <linux/nfs_page.h>
34 #include <linux/module.h>
36 #include <linux/sunrpc/metrics.h>
38 #include "internal.h"
39 #include "delegation.h"
40 #include "nfs4filelayout.h"
42 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
46 MODULE_DESCRIPTION("The NFSv4 file layout driver");
48 #define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
50 static loff_t
51 filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
52 loff_t offset)
54 u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
55 u64 stripe_no;
56 u32 rem;
58 offset -= flseg->pattern_offset;
59 stripe_no = div_u64(offset, stripe_width);
60 div_u64_rem(offset, flseg->stripe_unit, &rem);
62 return stripe_no * flseg->stripe_unit + rem;
65 /* This function is used by the layout driver to calculate the
66 * offset of the file on the dserver based on whether the
67 * layout type is STRIPE_DENSE or STRIPE_SPARSE
69 static loff_t
70 filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
72 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
74 switch (flseg->stripe_type) {
75 case STRIPE_SPARSE:
76 return offset;
78 case STRIPE_DENSE:
79 return filelayout_get_dense_offset(flseg, offset);
82 BUG();
85 static void filelayout_reset_write(struct nfs_write_data *data)
87 struct nfs_pgio_header *hdr = data->header;
88 struct inode *inode = hdr->inode;
89 struct rpc_task *task = &data->task;
91 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
92 dprintk("%s Reset task %5u for i/o through MDS "
93 "(req %s/%lld, %u bytes @ offset %llu)\n", __func__,
94 data->task.tk_pid,
95 inode->i_sb->s_id,
96 (long long)NFS_FILEID(inode),
97 data->args.count,
98 (unsigned long long)data->args.offset);
100 task->tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
101 &hdr->pages,
102 hdr->completion_ops);
106 static void filelayout_reset_read(struct nfs_read_data *data)
108 struct nfs_pgio_header *hdr = data->header;
109 struct inode *inode = hdr->inode;
110 struct rpc_task *task = &data->task;
112 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
113 dprintk("%s Reset task %5u for i/o through MDS "
114 "(req %s/%lld, %u bytes @ offset %llu)\n", __func__,
115 data->task.tk_pid,
116 inode->i_sb->s_id,
117 (long long)NFS_FILEID(inode),
118 data->args.count,
119 (unsigned long long)data->args.offset);
121 task->tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
122 &hdr->pages,
123 hdr->completion_ops);
127 static int filelayout_async_handle_error(struct rpc_task *task,
128 struct nfs4_state *state,
129 struct nfs_client *clp,
130 struct pnfs_layout_segment *lseg)
132 struct inode *inode = lseg->pls_layout->plh_inode;
133 struct nfs_server *mds_server = NFS_SERVER(inode);
134 struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
135 struct nfs_client *mds_client = mds_server->nfs_client;
137 if (task->tk_status >= 0)
138 return 0;
140 switch (task->tk_status) {
141 /* MDS state errors */
142 case -NFS4ERR_DELEG_REVOKED:
143 case -NFS4ERR_ADMIN_REVOKED:
144 case -NFS4ERR_BAD_STATEID:
145 if (state == NULL)
146 break;
147 nfs_remove_bad_delegation(state->inode);
148 case -NFS4ERR_OPENMODE:
149 if (state == NULL)
150 break;
151 nfs4_schedule_stateid_recovery(mds_server, state);
152 goto wait_on_recovery;
153 case -NFS4ERR_EXPIRED:
154 if (state != NULL)
155 nfs4_schedule_stateid_recovery(mds_server, state);
156 nfs4_schedule_lease_recovery(mds_client);
157 goto wait_on_recovery;
158 /* DS session errors */
159 case -NFS4ERR_BADSESSION:
160 case -NFS4ERR_BADSLOT:
161 case -NFS4ERR_BAD_HIGH_SLOT:
162 case -NFS4ERR_DEADSESSION:
163 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
164 case -NFS4ERR_SEQ_FALSE_RETRY:
165 case -NFS4ERR_SEQ_MISORDERED:
166 dprintk("%s ERROR %d, Reset session. Exchangeid "
167 "flags 0x%x\n", __func__, task->tk_status,
168 clp->cl_exchange_flags);
169 nfs4_schedule_session_recovery(clp->cl_session);
170 break;
171 case -NFS4ERR_DELAY:
172 case -NFS4ERR_GRACE:
173 case -EKEYEXPIRED:
174 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
175 break;
176 case -NFS4ERR_RETRY_UNCACHED_REP:
177 break;
178 /* RPC connection errors */
179 case -ECONNREFUSED:
180 case -EHOSTDOWN:
181 case -EHOSTUNREACH:
182 case -ENETUNREACH:
183 case -EIO:
184 case -ETIMEDOUT:
185 case -EPIPE:
186 dprintk("%s DS connection error %d\n", __func__,
187 task->tk_status);
188 filelayout_mark_devid_invalid(devid);
189 /* fall through */
190 default:
191 dprintk("%s Retry through MDS. Error %d\n", __func__,
192 task->tk_status);
193 return -NFS4ERR_RESET_TO_MDS;
195 out:
196 task->tk_status = 0;
197 return -EAGAIN;
198 wait_on_recovery:
199 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
200 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
201 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
202 goto out;
205 /* NFS_PROTO call done callback routines */
207 static int filelayout_read_done_cb(struct rpc_task *task,
208 struct nfs_read_data *data)
210 struct nfs_pgio_header *hdr = data->header;
211 int err;
213 err = filelayout_async_handle_error(task, data->args.context->state,
214 data->ds_clp, hdr->lseg);
216 switch (err) {
217 case -NFS4ERR_RESET_TO_MDS:
218 filelayout_reset_read(data);
219 return task->tk_status;
220 case -EAGAIN:
221 rpc_restart_call_prepare(task);
222 return -EAGAIN;
225 return 0;
229 * We reference the rpc_cred of the first WRITE that triggers the need for
230 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
231 * rfc5661 is not clear about which credential should be used.
233 static void
234 filelayout_set_layoutcommit(struct nfs_write_data *wdata)
236 struct nfs_pgio_header *hdr = wdata->header;
238 if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
239 wdata->res.verf->committed == NFS_FILE_SYNC)
240 return;
242 pnfs_set_layoutcommit(wdata);
243 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
244 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
248 * Call ops for the async read/write cases
249 * In the case of dense layouts, the offset needs to be reset to its
250 * original value.
252 static void filelayout_read_prepare(struct rpc_task *task, void *data)
254 struct nfs_read_data *rdata = data;
255 struct pnfs_layout_segment *lseg = rdata->header->lseg;
257 if (filelayout_test_devid_invalid(FILELAYOUT_DEVID_NODE(lseg))) {
258 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
259 filelayout_reset_read(rdata);
260 rpc_exit(task, 0);
261 return;
263 rdata->read_done_cb = filelayout_read_done_cb;
265 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
266 &rdata->args.seq_args, &rdata->res.seq_res,
267 task))
268 return;
270 rpc_call_start(task);
273 static void filelayout_read_call_done(struct rpc_task *task, void *data)
275 struct nfs_read_data *rdata = data;
277 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
279 if (test_bit(NFS_IOHDR_REDO, &rdata->header->flags))
280 return;
282 /* Note this may cause RPC to be resent */
283 rdata->header->mds_ops->rpc_call_done(task, data);
286 static void filelayout_read_count_stats(struct rpc_task *task, void *data)
288 struct nfs_read_data *rdata = data;
290 rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics);
293 static void filelayout_read_release(void *data)
295 struct nfs_read_data *rdata = data;
297 rdata->header->mds_ops->rpc_release(data);
300 static int filelayout_write_done_cb(struct rpc_task *task,
301 struct nfs_write_data *data)
303 struct nfs_pgio_header *hdr = data->header;
304 int err;
306 err = filelayout_async_handle_error(task, data->args.context->state,
307 data->ds_clp, hdr->lseg);
309 switch (err) {
310 case -NFS4ERR_RESET_TO_MDS:
311 filelayout_reset_write(data);
312 return task->tk_status;
313 case -EAGAIN:
314 rpc_restart_call_prepare(task);
315 return -EAGAIN;
318 filelayout_set_layoutcommit(data);
319 return 0;
322 /* Fake up some data that will cause nfs_commit_release to retry the writes. */
323 static void prepare_to_resend_writes(struct nfs_commit_data *data)
325 struct nfs_page *first = nfs_list_entry(data->pages.next);
327 data->task.tk_status = 0;
328 memcpy(data->verf.verifier, first->wb_verf.verifier,
329 sizeof(first->wb_verf.verifier));
330 data->verf.verifier[0]++; /* ensure verifier mismatch */
333 static int filelayout_commit_done_cb(struct rpc_task *task,
334 struct nfs_commit_data *data)
336 int err;
338 err = filelayout_async_handle_error(task, NULL, data->ds_clp,
339 data->lseg);
341 switch (err) {
342 case -NFS4ERR_RESET_TO_MDS:
343 prepare_to_resend_writes(data);
344 return -EAGAIN;
345 case -EAGAIN:
346 rpc_restart_call_prepare(task);
347 return -EAGAIN;
350 return 0;
353 static void filelayout_write_prepare(struct rpc_task *task, void *data)
355 struct nfs_write_data *wdata = data;
356 struct pnfs_layout_segment *lseg = wdata->header->lseg;
358 if (filelayout_test_devid_invalid(FILELAYOUT_DEVID_NODE(lseg))) {
359 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
360 filelayout_reset_write(wdata);
361 rpc_exit(task, 0);
362 return;
364 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
365 &wdata->args.seq_args, &wdata->res.seq_res,
366 task))
367 return;
369 rpc_call_start(task);
372 static void filelayout_write_call_done(struct rpc_task *task, void *data)
374 struct nfs_write_data *wdata = data;
376 if (test_bit(NFS_IOHDR_REDO, &wdata->header->flags))
377 return;
379 /* Note this may cause RPC to be resent */
380 wdata->header->mds_ops->rpc_call_done(task, data);
383 static void filelayout_write_count_stats(struct rpc_task *task, void *data)
385 struct nfs_write_data *wdata = data;
387 rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics);
390 static void filelayout_write_release(void *data)
392 struct nfs_write_data *wdata = data;
394 wdata->header->mds_ops->rpc_release(data);
397 static void filelayout_commit_prepare(struct rpc_task *task, void *data)
399 struct nfs_commit_data *wdata = data;
401 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
402 &wdata->args.seq_args, &wdata->res.seq_res,
403 task))
404 return;
406 rpc_call_start(task);
409 static void filelayout_write_commit_done(struct rpc_task *task, void *data)
411 struct nfs_commit_data *wdata = data;
413 /* Note this may cause RPC to be resent */
414 wdata->mds_ops->rpc_call_done(task, data);
417 static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
419 struct nfs_commit_data *cdata = data;
421 rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
424 static void filelayout_commit_release(void *calldata)
426 struct nfs_commit_data *data = calldata;
428 data->completion_ops->completion(data);
429 put_lseg(data->lseg);
430 nfs_commitdata_release(data);
433 static const struct rpc_call_ops filelayout_read_call_ops = {
434 .rpc_call_prepare = filelayout_read_prepare,
435 .rpc_call_done = filelayout_read_call_done,
436 .rpc_count_stats = filelayout_read_count_stats,
437 .rpc_release = filelayout_read_release,
440 static const struct rpc_call_ops filelayout_write_call_ops = {
441 .rpc_call_prepare = filelayout_write_prepare,
442 .rpc_call_done = filelayout_write_call_done,
443 .rpc_count_stats = filelayout_write_count_stats,
444 .rpc_release = filelayout_write_release,
447 static const struct rpc_call_ops filelayout_commit_call_ops = {
448 .rpc_call_prepare = filelayout_commit_prepare,
449 .rpc_call_done = filelayout_write_commit_done,
450 .rpc_count_stats = filelayout_commit_count_stats,
451 .rpc_release = filelayout_commit_release,
454 static enum pnfs_try_status
455 filelayout_read_pagelist(struct nfs_read_data *data)
457 struct nfs_pgio_header *hdr = data->header;
458 struct pnfs_layout_segment *lseg = hdr->lseg;
459 struct nfs4_pnfs_ds *ds;
460 loff_t offset = data->args.offset;
461 u32 j, idx;
462 struct nfs_fh *fh;
463 int status;
465 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
466 __func__, hdr->inode->i_ino,
467 data->args.pgbase, (size_t)data->args.count, offset);
469 /* Retrieve the correct rpc_client for the byte range */
470 j = nfs4_fl_calc_j_index(lseg, offset);
471 idx = nfs4_fl_calc_ds_index(lseg, j);
472 ds = nfs4_fl_prepare_ds(lseg, idx);
473 if (!ds)
474 return PNFS_NOT_ATTEMPTED;
475 dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
477 /* No multipath support. Use first DS */
478 data->ds_clp = ds->ds_clp;
479 fh = nfs4_fl_select_ds_fh(lseg, j);
480 if (fh)
481 data->args.fh = fh;
483 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
484 data->mds_offset = offset;
486 /* Perform an asynchronous read to ds */
487 status = nfs_initiate_read(ds->ds_clp->cl_rpcclient, data,
488 &filelayout_read_call_ops, RPC_TASK_SOFTCONN);
489 BUG_ON(status != 0);
490 return PNFS_ATTEMPTED;
493 /* Perform async writes. */
494 static enum pnfs_try_status
495 filelayout_write_pagelist(struct nfs_write_data *data, int sync)
497 struct nfs_pgio_header *hdr = data->header;
498 struct pnfs_layout_segment *lseg = hdr->lseg;
499 struct nfs4_pnfs_ds *ds;
500 loff_t offset = data->args.offset;
501 u32 j, idx;
502 struct nfs_fh *fh;
503 int status;
505 /* Retrieve the correct rpc_client for the byte range */
506 j = nfs4_fl_calc_j_index(lseg, offset);
507 idx = nfs4_fl_calc_ds_index(lseg, j);
508 ds = nfs4_fl_prepare_ds(lseg, idx);
509 if (!ds)
510 return PNFS_NOT_ATTEMPTED;
511 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
512 hdr->inode->i_ino, sync, (size_t) data->args.count, offset,
513 ds->ds_remotestr);
515 data->write_done_cb = filelayout_write_done_cb;
516 data->ds_clp = ds->ds_clp;
517 fh = nfs4_fl_select_ds_fh(lseg, j);
518 if (fh)
519 data->args.fh = fh;
521 * Get the file offset on the dserver. Set the write offset to
522 * this offset and save the original offset.
524 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
526 /* Perform an asynchronous write */
527 status = nfs_initiate_write(ds->ds_clp->cl_rpcclient, data,
528 &filelayout_write_call_ops, sync,
529 RPC_TASK_SOFTCONN);
530 BUG_ON(status != 0);
531 return PNFS_ATTEMPTED;
535 * filelayout_check_layout()
537 * Make sure layout segment parameters are sane WRT the device.
538 * At this point no generic layer initialization of the lseg has occurred,
539 * and nothing has been added to the layout_hdr cache.
542 static int
543 filelayout_check_layout(struct pnfs_layout_hdr *lo,
544 struct nfs4_filelayout_segment *fl,
545 struct nfs4_layoutget_res *lgr,
546 struct nfs4_deviceid *id,
547 gfp_t gfp_flags)
549 struct nfs4_deviceid_node *d;
550 struct nfs4_file_layout_dsaddr *dsaddr;
551 int status = -EINVAL;
552 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
554 dprintk("--> %s\n", __func__);
556 /* FIXME: remove this check when layout segment support is added */
557 if (lgr->range.offset != 0 ||
558 lgr->range.length != NFS4_MAX_UINT64) {
559 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
560 __func__);
561 goto out;
564 if (fl->pattern_offset > lgr->range.offset) {
565 dprintk("%s pattern_offset %lld too large\n",
566 __func__, fl->pattern_offset);
567 goto out;
570 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
571 dprintk("%s Invalid stripe unit (%u)\n",
572 __func__, fl->stripe_unit);
573 goto out;
576 /* find and reference the deviceid */
577 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
578 NFS_SERVER(lo->plh_inode)->nfs_client, id);
579 if (d == NULL) {
580 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
581 if (dsaddr == NULL)
582 goto out;
583 } else
584 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
585 /* Found deviceid is being reaped */
586 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
587 goto out_put;
589 fl->dsaddr = dsaddr;
591 if (fl->first_stripe_index >= dsaddr->stripe_count) {
592 dprintk("%s Bad first_stripe_index %u\n",
593 __func__, fl->first_stripe_index);
594 goto out_put;
597 if ((fl->stripe_type == STRIPE_SPARSE &&
598 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
599 (fl->stripe_type == STRIPE_DENSE &&
600 fl->num_fh != dsaddr->stripe_count)) {
601 dprintk("%s num_fh %u not valid for given packing\n",
602 __func__, fl->num_fh);
603 goto out_put;
606 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
607 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
608 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
609 nfss->wsize);
612 status = 0;
613 out:
614 dprintk("--> %s returns %d\n", __func__, status);
615 return status;
616 out_put:
617 nfs4_fl_put_deviceid(dsaddr);
618 goto out;
621 static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
623 int i;
625 for (i = 0; i < fl->num_fh; i++) {
626 if (!fl->fh_array[i])
627 break;
628 kfree(fl->fh_array[i]);
630 kfree(fl->fh_array);
631 fl->fh_array = NULL;
634 static void
635 _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
637 filelayout_free_fh_array(fl);
638 kfree(fl);
641 static int
642 filelayout_decode_layout(struct pnfs_layout_hdr *flo,
643 struct nfs4_filelayout_segment *fl,
644 struct nfs4_layoutget_res *lgr,
645 struct nfs4_deviceid *id,
646 gfp_t gfp_flags)
648 struct xdr_stream stream;
649 struct xdr_buf buf;
650 struct page *scratch;
651 __be32 *p;
652 uint32_t nfl_util;
653 int i;
655 dprintk("%s: set_layout_map Begin\n", __func__);
657 scratch = alloc_page(gfp_flags);
658 if (!scratch)
659 return -ENOMEM;
661 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
662 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
664 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
665 * num_fh (4) */
666 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
667 if (unlikely(!p))
668 goto out_err;
670 memcpy(id, p, sizeof(*id));
671 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
672 nfs4_print_deviceid(id);
674 nfl_util = be32_to_cpup(p++);
675 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
676 fl->commit_through_mds = 1;
677 if (nfl_util & NFL4_UFLG_DENSE)
678 fl->stripe_type = STRIPE_DENSE;
679 else
680 fl->stripe_type = STRIPE_SPARSE;
681 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
683 fl->first_stripe_index = be32_to_cpup(p++);
684 p = xdr_decode_hyper(p, &fl->pattern_offset);
685 fl->num_fh = be32_to_cpup(p++);
687 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
688 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
689 fl->pattern_offset);
691 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
692 * Futher checking is done in filelayout_check_layout */
693 if (fl->num_fh >
694 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
695 goto out_err;
697 if (fl->num_fh > 0) {
698 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
699 gfp_flags);
700 if (!fl->fh_array)
701 goto out_err;
704 for (i = 0; i < fl->num_fh; i++) {
705 /* Do we want to use a mempool here? */
706 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
707 if (!fl->fh_array[i])
708 goto out_err_free;
710 p = xdr_inline_decode(&stream, 4);
711 if (unlikely(!p))
712 goto out_err_free;
713 fl->fh_array[i]->size = be32_to_cpup(p++);
714 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
715 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
716 i, fl->fh_array[i]->size);
717 goto out_err_free;
720 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
721 if (unlikely(!p))
722 goto out_err_free;
723 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
724 dprintk("DEBUG: %s: fh len %d\n", __func__,
725 fl->fh_array[i]->size);
728 __free_page(scratch);
729 return 0;
731 out_err_free:
732 filelayout_free_fh_array(fl);
733 out_err:
734 __free_page(scratch);
735 return -EIO;
738 static void
739 filelayout_free_lseg(struct pnfs_layout_segment *lseg)
741 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
743 dprintk("--> %s\n", __func__);
744 nfs4_fl_put_deviceid(fl->dsaddr);
745 /* This assumes a single RW lseg */
746 if (lseg->pls_range.iomode == IOMODE_RW) {
747 struct nfs4_filelayout *flo;
749 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
750 flo->commit_info.nbuckets = 0;
751 kfree(flo->commit_info.buckets);
752 flo->commit_info.buckets = NULL;
754 _filelayout_free_lseg(fl);
757 static int
758 filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
759 struct nfs_commit_info *cinfo,
760 gfp_t gfp_flags)
762 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
763 struct pnfs_commit_bucket *buckets;
764 int size;
766 if (fl->commit_through_mds)
767 return 0;
768 if (cinfo->ds->nbuckets != 0) {
769 /* This assumes there is only one IOMODE_RW lseg. What
770 * we really want to do is have a layout_hdr level
771 * dictionary of <multipath_list4, fh> keys, each
772 * associated with a struct list_head, populated by calls
773 * to filelayout_write_pagelist().
774 * */
775 return 0;
778 size = (fl->stripe_type == STRIPE_SPARSE) ?
779 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
781 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
782 gfp_flags);
783 if (!buckets)
784 return -ENOMEM;
785 else {
786 int i;
788 spin_lock(cinfo->lock);
789 if (cinfo->ds->nbuckets != 0)
790 kfree(buckets);
791 else {
792 cinfo->ds->buckets = buckets;
793 cinfo->ds->nbuckets = size;
794 for (i = 0; i < size; i++) {
795 INIT_LIST_HEAD(&buckets[i].written);
796 INIT_LIST_HEAD(&buckets[i].committing);
799 spin_unlock(cinfo->lock);
800 return 0;
804 static struct pnfs_layout_segment *
805 filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
806 struct nfs4_layoutget_res *lgr,
807 gfp_t gfp_flags)
809 struct nfs4_filelayout_segment *fl;
810 int rc;
811 struct nfs4_deviceid id;
813 dprintk("--> %s\n", __func__);
814 fl = kzalloc(sizeof(*fl), gfp_flags);
815 if (!fl)
816 return NULL;
818 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
819 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
820 _filelayout_free_lseg(fl);
821 return NULL;
823 return &fl->generic_hdr;
827 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
829 * return true : coalesce page
830 * return false : don't coalesce page
832 static bool
833 filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
834 struct nfs_page *req)
836 u64 p_stripe, r_stripe;
837 u32 stripe_unit;
839 if (!pnfs_generic_pg_test(pgio, prev, req) ||
840 !nfs_generic_pg_test(pgio, prev, req))
841 return false;
843 p_stripe = (u64)req_offset(prev);
844 r_stripe = (u64)req_offset(req);
845 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
847 do_div(p_stripe, stripe_unit);
848 do_div(r_stripe, stripe_unit);
850 return (p_stripe == r_stripe);
853 static void
854 filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
855 struct nfs_page *req)
857 BUG_ON(pgio->pg_lseg != NULL);
859 if (req->wb_offset != req->wb_pgbase) {
861 * Handling unaligned pages is difficult, because have to
862 * somehow split a req in two in certain cases in the
863 * pg.test code. Avoid this by just not using pnfs
864 * in this case.
866 nfs_pageio_reset_read_mds(pgio);
867 return;
869 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
870 req->wb_context,
872 NFS4_MAX_UINT64,
873 IOMODE_READ,
874 GFP_KERNEL);
875 /* If no lseg, fall back to read through mds */
876 if (pgio->pg_lseg == NULL)
877 nfs_pageio_reset_read_mds(pgio);
880 static void
881 filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
882 struct nfs_page *req)
884 struct nfs_commit_info cinfo;
885 int status;
887 BUG_ON(pgio->pg_lseg != NULL);
889 if (req->wb_offset != req->wb_pgbase)
890 goto out_mds;
891 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
892 req->wb_context,
894 NFS4_MAX_UINT64,
895 IOMODE_RW,
896 GFP_NOFS);
897 /* If no lseg, fall back to write through mds */
898 if (pgio->pg_lseg == NULL)
899 goto out_mds;
900 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
901 status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
902 if (status < 0) {
903 put_lseg(pgio->pg_lseg);
904 pgio->pg_lseg = NULL;
905 goto out_mds;
907 return;
908 out_mds:
909 nfs_pageio_reset_write_mds(pgio);
912 static const struct nfs_pageio_ops filelayout_pg_read_ops = {
913 .pg_init = filelayout_pg_init_read,
914 .pg_test = filelayout_pg_test,
915 .pg_doio = pnfs_generic_pg_readpages,
918 static const struct nfs_pageio_ops filelayout_pg_write_ops = {
919 .pg_init = filelayout_pg_init_write,
920 .pg_test = filelayout_pg_test,
921 .pg_doio = pnfs_generic_pg_writepages,
924 static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
926 if (fl->stripe_type == STRIPE_SPARSE)
927 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
928 else
929 return j;
932 /* The generic layer is about to remove the req from the commit list.
933 * If this will make the bucket empty, it will need to put the lseg reference.
935 static void
936 filelayout_clear_request_commit(struct nfs_page *req,
937 struct nfs_commit_info *cinfo)
939 struct pnfs_layout_segment *freeme = NULL;
941 spin_lock(cinfo->lock);
942 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
943 goto out;
944 cinfo->ds->nwritten--;
945 if (list_is_singular(&req->wb_list)) {
946 struct pnfs_commit_bucket *bucket;
948 bucket = list_first_entry(&req->wb_list,
949 struct pnfs_commit_bucket,
950 written);
951 freeme = bucket->wlseg;
952 bucket->wlseg = NULL;
954 out:
955 nfs_request_remove_commit_list(req, cinfo);
956 spin_unlock(cinfo->lock);
957 put_lseg(freeme);
960 static struct list_head *
961 filelayout_choose_commit_list(struct nfs_page *req,
962 struct pnfs_layout_segment *lseg,
963 struct nfs_commit_info *cinfo)
965 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
966 u32 i, j;
967 struct list_head *list;
968 struct pnfs_commit_bucket *buckets;
970 if (fl->commit_through_mds)
971 return &cinfo->mds->list;
973 /* Note that we are calling nfs4_fl_calc_j_index on each page
974 * that ends up being committed to a data server. An attractive
975 * alternative is to add a field to nfs_write_data and nfs_page
976 * to store the value calculated in filelayout_write_pagelist
977 * and just use that here.
979 j = nfs4_fl_calc_j_index(lseg, req_offset(req));
980 i = select_bucket_index(fl, j);
981 buckets = cinfo->ds->buckets;
982 list = &buckets[i].written;
983 if (list_empty(list)) {
984 /* Non-empty buckets hold a reference on the lseg. That ref
985 * is normally transferred to the COMMIT call and released
986 * there. It could also be released if the last req is pulled
987 * off due to a rewrite, in which case it will be done in
988 * filelayout_clear_request_commit
990 buckets[i].wlseg = get_lseg(lseg);
992 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
993 cinfo->ds->nwritten++;
994 return list;
997 static void
998 filelayout_mark_request_commit(struct nfs_page *req,
999 struct pnfs_layout_segment *lseg,
1000 struct nfs_commit_info *cinfo)
1002 struct list_head *list;
1004 list = filelayout_choose_commit_list(req, lseg, cinfo);
1005 nfs_request_add_commit_list(req, list, cinfo);
1008 static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1010 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1012 if (flseg->stripe_type == STRIPE_SPARSE)
1013 return i;
1014 else
1015 return nfs4_fl_calc_ds_index(lseg, i);
1018 static struct nfs_fh *
1019 select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1021 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1023 if (flseg->stripe_type == STRIPE_SPARSE) {
1024 if (flseg->num_fh == 1)
1025 i = 0;
1026 else if (flseg->num_fh == 0)
1027 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
1028 return NULL;
1030 return flseg->fh_array[i];
1033 static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
1035 struct pnfs_layout_segment *lseg = data->lseg;
1036 struct nfs4_pnfs_ds *ds;
1037 u32 idx;
1038 struct nfs_fh *fh;
1040 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1041 ds = nfs4_fl_prepare_ds(lseg, idx);
1042 if (!ds) {
1043 prepare_to_resend_writes(data);
1044 filelayout_commit_release(data);
1045 return -EAGAIN;
1047 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
1048 data->commit_done_cb = filelayout_commit_done_cb;
1049 data->ds_clp = ds->ds_clp;
1050 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1051 if (fh)
1052 data->args.fh = fh;
1053 return nfs_initiate_commit(ds->ds_clp->cl_rpcclient, data,
1054 &filelayout_commit_call_ops, how,
1055 RPC_TASK_SOFTCONN);
1058 static int
1059 transfer_commit_list(struct list_head *src, struct list_head *dst,
1060 struct nfs_commit_info *cinfo, int max)
1062 struct nfs_page *req, *tmp;
1063 int ret = 0;
1065 list_for_each_entry_safe(req, tmp, src, wb_list) {
1066 if (!nfs_lock_request(req))
1067 continue;
1068 if (cond_resched_lock(cinfo->lock))
1069 list_safe_reset_next(req, tmp, wb_list);
1070 nfs_request_remove_commit_list(req, cinfo);
1071 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1072 nfs_list_add_request(req, dst);
1073 ret++;
1074 if ((ret == max) && !cinfo->dreq)
1075 break;
1077 return ret;
1080 static int
1081 filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
1082 struct nfs_commit_info *cinfo,
1083 int max)
1085 struct list_head *src = &bucket->written;
1086 struct list_head *dst = &bucket->committing;
1087 int ret;
1089 ret = transfer_commit_list(src, dst, cinfo, max);
1090 if (ret) {
1091 cinfo->ds->nwritten -= ret;
1092 cinfo->ds->ncommitting += ret;
1093 bucket->clseg = bucket->wlseg;
1094 if (list_empty(src))
1095 bucket->wlseg = NULL;
1096 else
1097 get_lseg(bucket->clseg);
1099 return ret;
1102 /* Move reqs from written to committing lists, returning count of number moved.
1103 * Note called with cinfo->lock held.
1105 static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo,
1106 int max)
1108 int i, rv = 0, cnt;
1110 for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
1111 cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i],
1112 cinfo, max);
1113 max -= cnt;
1114 rv += cnt;
1116 return rv;
1119 /* Pull everything off the committing lists and dump into @dst */
1120 static void filelayout_recover_commit_reqs(struct list_head *dst,
1121 struct nfs_commit_info *cinfo)
1123 struct pnfs_commit_bucket *b;
1124 int i;
1126 /* NOTE cinfo->lock is NOT held, relying on fact that this is
1127 * only called on single thread per dreq.
1128 * Can't take the lock because need to do put_lseg
1130 for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
1131 if (transfer_commit_list(&b->written, dst, cinfo, 0)) {
1132 BUG_ON(!list_empty(&b->written));
1133 put_lseg(b->wlseg);
1134 b->wlseg = NULL;
1137 cinfo->ds->nwritten = 0;
1140 static unsigned int
1141 alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list)
1143 struct pnfs_ds_commit_info *fl_cinfo;
1144 struct pnfs_commit_bucket *bucket;
1145 struct nfs_commit_data *data;
1146 int i, j;
1147 unsigned int nreq = 0;
1149 fl_cinfo = cinfo->ds;
1150 bucket = fl_cinfo->buckets;
1151 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
1152 if (list_empty(&bucket->committing))
1153 continue;
1154 data = nfs_commitdata_alloc();
1155 if (!data)
1156 break;
1157 data->ds_commit_index = i;
1158 data->lseg = bucket->clseg;
1159 bucket->clseg = NULL;
1160 list_add(&data->pages, list);
1161 nreq++;
1164 /* Clean up on error */
1165 for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
1166 if (list_empty(&bucket->committing))
1167 continue;
1168 nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo);
1169 put_lseg(bucket->clseg);
1170 bucket->clseg = NULL;
1172 /* Caller will clean up entries put on list */
1173 return nreq;
1176 /* This follows nfs_commit_list pretty closely */
1177 static int
1178 filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1179 int how, struct nfs_commit_info *cinfo)
1181 struct nfs_commit_data *data, *tmp;
1182 LIST_HEAD(list);
1183 unsigned int nreq = 0;
1185 if (!list_empty(mds_pages)) {
1186 data = nfs_commitdata_alloc();
1187 if (data != NULL) {
1188 data->lseg = NULL;
1189 list_add(&data->pages, &list);
1190 nreq++;
1191 } else
1192 nfs_retry_commit(mds_pages, NULL, cinfo);
1195 nreq += alloc_ds_commits(cinfo, &list);
1197 if (nreq == 0) {
1198 cinfo->completion_ops->error_cleanup(NFS_I(inode));
1199 goto out;
1202 atomic_add(nreq, &cinfo->mds->rpcs_out);
1204 list_for_each_entry_safe(data, tmp, &list, pages) {
1205 list_del_init(&data->pages);
1206 if (!data->lseg) {
1207 nfs_init_commit(data, mds_pages, NULL, cinfo);
1208 nfs_initiate_commit(NFS_CLIENT(inode), data,
1209 data->mds_ops, how, 0);
1210 } else {
1211 struct pnfs_commit_bucket *buckets;
1213 buckets = cinfo->ds->buckets;
1214 nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo);
1215 filelayout_initiate_commit(data, how);
1218 out:
1219 cinfo->ds->ncommitting = 0;
1220 return PNFS_ATTEMPTED;
1223 static void
1224 filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1226 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1229 static struct pnfs_layout_hdr *
1230 filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1232 struct nfs4_filelayout *flo;
1234 flo = kzalloc(sizeof(*flo), gfp_flags);
1235 return &flo->generic_hdr;
1238 static void
1239 filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1241 kfree(FILELAYOUT_FROM_HDR(lo));
1244 static struct pnfs_ds_commit_info *
1245 filelayout_get_ds_info(struct inode *inode)
1247 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1249 if (layout == NULL)
1250 return NULL;
1251 else
1252 return &FILELAYOUT_FROM_HDR(layout)->commit_info;
1255 static struct pnfs_layoutdriver_type filelayout_type = {
1256 .id = LAYOUT_NFSV4_1_FILES,
1257 .name = "LAYOUT_NFSV4_1_FILES",
1258 .owner = THIS_MODULE,
1259 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1260 .free_layout_hdr = filelayout_free_layout_hdr,
1261 .alloc_lseg = filelayout_alloc_lseg,
1262 .free_lseg = filelayout_free_lseg,
1263 .pg_read_ops = &filelayout_pg_read_ops,
1264 .pg_write_ops = &filelayout_pg_write_ops,
1265 .get_ds_info = &filelayout_get_ds_info,
1266 .mark_request_commit = filelayout_mark_request_commit,
1267 .clear_request_commit = filelayout_clear_request_commit,
1268 .scan_commit_lists = filelayout_scan_commit_lists,
1269 .recover_commit_reqs = filelayout_recover_commit_reqs,
1270 .commit_pagelist = filelayout_commit_pagelist,
1271 .read_pagelist = filelayout_read_pagelist,
1272 .write_pagelist = filelayout_write_pagelist,
1273 .free_deviceid_node = filelayout_free_deveiceid_node,
1276 static int __init nfs4filelayout_init(void)
1278 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1279 __func__);
1280 return pnfs_register_layoutdriver(&filelayout_type);
1283 static void __exit nfs4filelayout_exit(void)
1285 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1286 __func__);
1287 pnfs_unregister_layoutdriver(&filelayout_type);
1290 MODULE_ALIAS("nfs-layouttype4-1");
1292 module_init(nfs4filelayout_init);
1293 module_exit(nfs4filelayout_exit);