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.
7 * The Regents of the University of Michigan
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
32 #include <linux/nfs_fs.h>
33 #include <linux/nfs_page.h>
34 #include <linux/module.h>
36 #include <linux/sunrpc/metrics.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)
51 filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
54 u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
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
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) {
79 return filelayout_get_dense_offset(flseg, offset);
85 static int filelayout_async_handle_error(struct rpc_task *task,
86 struct nfs4_state *state,
87 struct nfs_client *clp,
90 struct nfs_server *mds_server = NFS_SERVER(state->inode);
91 struct nfs_client *mds_client = mds_server->nfs_client;
93 if (task->tk_status >= 0)
97 switch (task->tk_status) {
98 /* MDS state errors */
99 case -NFS4ERR_DELEG_REVOKED:
100 case -NFS4ERR_ADMIN_REVOKED:
101 case -NFS4ERR_BAD_STATEID:
102 nfs_remove_bad_delegation(state->inode);
103 case -NFS4ERR_OPENMODE:
104 nfs4_schedule_stateid_recovery(mds_server, state);
105 goto wait_on_recovery;
106 case -NFS4ERR_EXPIRED:
107 nfs4_schedule_stateid_recovery(mds_server, state);
108 nfs4_schedule_lease_recovery(mds_client);
109 goto wait_on_recovery;
110 /* DS session errors */
111 case -NFS4ERR_BADSESSION:
112 case -NFS4ERR_BADSLOT:
113 case -NFS4ERR_BAD_HIGH_SLOT:
114 case -NFS4ERR_DEADSESSION:
115 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
116 case -NFS4ERR_SEQ_FALSE_RETRY:
117 case -NFS4ERR_SEQ_MISORDERED:
118 dprintk("%s ERROR %d, Reset session. Exchangeid "
119 "flags 0x%x\n", __func__, task->tk_status,
120 clp->cl_exchange_flags);
121 nfs4_schedule_session_recovery(clp->cl_session);
126 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
128 case -NFS4ERR_RETRY_UNCACHED_REP:
131 dprintk("%s DS error. Retry through MDS %d\n", __func__,
140 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
141 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
142 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
146 /* NFS_PROTO call done callback routines */
148 static int filelayout_read_done_cb(struct rpc_task *task,
149 struct nfs_read_data *data)
151 struct nfs_pgio_header *hdr = data->header;
154 dprintk("%s DS read\n", __func__);
156 if (filelayout_async_handle_error(task, data->args.context->state,
157 data->ds_clp, &reset) == -EAGAIN) {
158 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
159 __func__, data->ds_clp, data->ds_clp->cl_session);
161 pnfs_set_lo_fail(hdr->lseg);
162 nfs4_reset_read(task, data);
164 rpc_restart_call_prepare(task);
172 * We reference the rpc_cred of the first WRITE that triggers the need for
173 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
174 * rfc5661 is not clear about which credential should be used.
177 filelayout_set_layoutcommit(struct nfs_write_data *wdata)
179 struct nfs_pgio_header *hdr = wdata->header;
181 if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
182 wdata->res.verf->committed == NFS_FILE_SYNC)
185 pnfs_set_layoutcommit(wdata);
186 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
187 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
191 * Call ops for the async read/write cases
192 * In the case of dense layouts, the offset needs to be reset to its
195 static void filelayout_read_prepare(struct rpc_task *task, void *data)
197 struct nfs_read_data *rdata = data;
199 rdata->read_done_cb = filelayout_read_done_cb;
201 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
202 &rdata->args.seq_args, &rdata->res.seq_res,
206 rpc_call_start(task);
209 static void filelayout_read_call_done(struct rpc_task *task, void *data)
211 struct nfs_read_data *rdata = data;
213 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
215 /* Note this may cause RPC to be resent */
216 rdata->header->mds_ops->rpc_call_done(task, data);
219 static void filelayout_read_count_stats(struct rpc_task *task, void *data)
221 struct nfs_read_data *rdata = data;
223 rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics);
226 static void filelayout_read_release(void *data)
228 struct nfs_read_data *rdata = data;
230 rdata->header->mds_ops->rpc_release(data);
233 static int filelayout_write_done_cb(struct rpc_task *task,
234 struct nfs_write_data *data)
236 struct nfs_pgio_header *hdr = data->header;
239 if (filelayout_async_handle_error(task, data->args.context->state,
240 data->ds_clp, &reset) == -EAGAIN) {
241 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
242 __func__, data->ds_clp, data->ds_clp->cl_session);
244 pnfs_set_lo_fail(hdr->lseg);
245 nfs4_reset_write(task, data);
247 rpc_restart_call_prepare(task);
251 filelayout_set_layoutcommit(data);
255 /* Fake up some data that will cause nfs_commit_release to retry the writes. */
256 static void prepare_to_resend_writes(struct nfs_commit_data *data)
258 struct nfs_page *first = nfs_list_entry(data->pages.next);
260 data->task.tk_status = 0;
261 memcpy(data->verf.verifier, first->wb_verf.verifier,
262 sizeof(first->wb_verf.verifier));
263 data->verf.verifier[0]++; /* ensure verifier mismatch */
266 static int filelayout_commit_done_cb(struct rpc_task *task,
267 struct nfs_commit_data *data)
271 if (filelayout_async_handle_error(task, data->context->state,
272 data->ds_clp, &reset) == -EAGAIN) {
273 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
274 __func__, data->ds_clp, data->ds_clp->cl_session);
276 prepare_to_resend_writes(data);
277 pnfs_set_lo_fail(data->lseg);
279 rpc_restart_call_prepare(task);
286 static void filelayout_write_prepare(struct rpc_task *task, void *data)
288 struct nfs_write_data *wdata = data;
290 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
291 &wdata->args.seq_args, &wdata->res.seq_res,
295 rpc_call_start(task);
298 static void filelayout_write_call_done(struct rpc_task *task, void *data)
300 struct nfs_write_data *wdata = data;
302 /* Note this may cause RPC to be resent */
303 wdata->header->mds_ops->rpc_call_done(task, data);
306 static void filelayout_write_count_stats(struct rpc_task *task, void *data)
308 struct nfs_write_data *wdata = data;
310 rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics);
313 static void filelayout_write_release(void *data)
315 struct nfs_write_data *wdata = data;
317 wdata->header->mds_ops->rpc_release(data);
320 static void filelayout_commit_prepare(struct rpc_task *task, void *data)
322 struct nfs_commit_data *wdata = data;
324 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
325 &wdata->args.seq_args, &wdata->res.seq_res,
329 rpc_call_start(task);
332 static void filelayout_write_commit_done(struct rpc_task *task, void *data)
334 struct nfs_commit_data *wdata = data;
336 /* Note this may cause RPC to be resent */
337 wdata->mds_ops->rpc_call_done(task, data);
340 static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
342 struct nfs_commit_data *cdata = data;
344 rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
347 static void filelayout_commit_release(void *calldata)
349 struct nfs_commit_data *data = calldata;
351 nfs_commit_release_pages(data);
352 if (atomic_dec_and_test(&NFS_I(data->inode)->commits_outstanding))
353 nfs_commit_clear_lock(NFS_I(data->inode));
354 put_lseg(data->lseg);
355 nfs_commitdata_release(data);
358 static const struct rpc_call_ops filelayout_read_call_ops = {
359 .rpc_call_prepare = filelayout_read_prepare,
360 .rpc_call_done = filelayout_read_call_done,
361 .rpc_count_stats = filelayout_read_count_stats,
362 .rpc_release = filelayout_read_release,
365 static const struct rpc_call_ops filelayout_write_call_ops = {
366 .rpc_call_prepare = filelayout_write_prepare,
367 .rpc_call_done = filelayout_write_call_done,
368 .rpc_count_stats = filelayout_write_count_stats,
369 .rpc_release = filelayout_write_release,
372 static const struct rpc_call_ops filelayout_commit_call_ops = {
373 .rpc_call_prepare = filelayout_commit_prepare,
374 .rpc_call_done = filelayout_write_commit_done,
375 .rpc_count_stats = filelayout_commit_count_stats,
376 .rpc_release = filelayout_commit_release,
379 static enum pnfs_try_status
380 filelayout_read_pagelist(struct nfs_read_data *data)
382 struct nfs_pgio_header *hdr = data->header;
383 struct pnfs_layout_segment *lseg = hdr->lseg;
384 struct nfs4_pnfs_ds *ds;
385 loff_t offset = data->args.offset;
390 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
391 __func__, hdr->inode->i_ino,
392 data->args.pgbase, (size_t)data->args.count, offset);
394 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
395 return PNFS_NOT_ATTEMPTED;
397 /* Retrieve the correct rpc_client for the byte range */
398 j = nfs4_fl_calc_j_index(lseg, offset);
399 idx = nfs4_fl_calc_ds_index(lseg, j);
400 ds = nfs4_fl_prepare_ds(lseg, idx);
402 /* Either layout fh index faulty, or ds connect failed */
403 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
404 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
405 return PNFS_NOT_ATTEMPTED;
407 dprintk("%s USE DS: %s\n", __func__, ds->ds_remotestr);
409 /* No multipath support. Use first DS */
410 data->ds_clp = ds->ds_clp;
411 fh = nfs4_fl_select_ds_fh(lseg, j);
415 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
416 data->mds_offset = offset;
418 /* Perform an asynchronous read to ds */
419 status = nfs_initiate_read(ds->ds_clp->cl_rpcclient, data,
420 &filelayout_read_call_ops);
422 return PNFS_ATTEMPTED;
425 /* Perform async writes. */
426 static enum pnfs_try_status
427 filelayout_write_pagelist(struct nfs_write_data *data, int sync)
429 struct nfs_pgio_header *hdr = data->header;
430 struct pnfs_layout_segment *lseg = hdr->lseg;
431 struct nfs4_pnfs_ds *ds;
432 loff_t offset = data->args.offset;
437 if (test_bit(NFS_DEVICEID_INVALID, &FILELAYOUT_DEVID_NODE(lseg)->flags))
438 return PNFS_NOT_ATTEMPTED;
440 /* Retrieve the correct rpc_client for the byte range */
441 j = nfs4_fl_calc_j_index(lseg, offset);
442 idx = nfs4_fl_calc_ds_index(lseg, j);
443 ds = nfs4_fl_prepare_ds(lseg, idx);
445 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
447 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
448 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
449 return PNFS_NOT_ATTEMPTED;
451 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s\n", __func__,
452 hdr->inode->i_ino, sync, (size_t) data->args.count, offset,
455 data->write_done_cb = filelayout_write_done_cb;
456 data->ds_clp = ds->ds_clp;
457 fh = nfs4_fl_select_ds_fh(lseg, j);
461 * Get the file offset on the dserver. Set the write offset to
462 * this offset and save the original offset.
464 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
466 /* Perform an asynchronous write */
467 status = nfs_initiate_write(ds->ds_clp->cl_rpcclient, data,
468 &filelayout_write_call_ops, sync);
470 return PNFS_ATTEMPTED;
474 * filelayout_check_layout()
476 * Make sure layout segment parameters are sane WRT the device.
477 * At this point no generic layer initialization of the lseg has occurred,
478 * and nothing has been added to the layout_hdr cache.
482 filelayout_check_layout(struct pnfs_layout_hdr *lo,
483 struct nfs4_filelayout_segment *fl,
484 struct nfs4_layoutget_res *lgr,
485 struct nfs4_deviceid *id,
488 struct nfs4_deviceid_node *d;
489 struct nfs4_file_layout_dsaddr *dsaddr;
490 int status = -EINVAL;
491 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
493 dprintk("--> %s\n", __func__);
495 /* FIXME: remove this check when layout segment support is added */
496 if (lgr->range.offset != 0 ||
497 lgr->range.length != NFS4_MAX_UINT64) {
498 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
503 if (fl->pattern_offset > lgr->range.offset) {
504 dprintk("%s pattern_offset %lld too large\n",
505 __func__, fl->pattern_offset);
509 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
510 dprintk("%s Invalid stripe unit (%u)\n",
511 __func__, fl->stripe_unit);
515 /* find and reference the deviceid */
516 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
517 NFS_SERVER(lo->plh_inode)->nfs_client, id);
519 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
523 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
524 /* Found deviceid is being reaped */
525 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
530 if (fl->first_stripe_index >= dsaddr->stripe_count) {
531 dprintk("%s Bad first_stripe_index %u\n",
532 __func__, fl->first_stripe_index);
536 if ((fl->stripe_type == STRIPE_SPARSE &&
537 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
538 (fl->stripe_type == STRIPE_DENSE &&
539 fl->num_fh != dsaddr->stripe_count)) {
540 dprintk("%s num_fh %u not valid for given packing\n",
541 __func__, fl->num_fh);
545 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
546 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
547 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
553 dprintk("--> %s returns %d\n", __func__, status);
556 nfs4_fl_put_deviceid(dsaddr);
560 static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
564 for (i = 0; i < fl->num_fh; i++) {
565 if (!fl->fh_array[i])
567 kfree(fl->fh_array[i]);
574 _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
576 filelayout_free_fh_array(fl);
581 filelayout_decode_layout(struct pnfs_layout_hdr *flo,
582 struct nfs4_filelayout_segment *fl,
583 struct nfs4_layoutget_res *lgr,
584 struct nfs4_deviceid *id,
587 struct xdr_stream stream;
589 struct page *scratch;
594 dprintk("%s: set_layout_map Begin\n", __func__);
596 scratch = alloc_page(gfp_flags);
600 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
601 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
603 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
605 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
609 memcpy(id, p, sizeof(*id));
610 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
611 nfs4_print_deviceid(id);
613 nfl_util = be32_to_cpup(p++);
614 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
615 fl->commit_through_mds = 1;
616 if (nfl_util & NFL4_UFLG_DENSE)
617 fl->stripe_type = STRIPE_DENSE;
619 fl->stripe_type = STRIPE_SPARSE;
620 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
622 fl->first_stripe_index = be32_to_cpup(p++);
623 p = xdr_decode_hyper(p, &fl->pattern_offset);
624 fl->num_fh = be32_to_cpup(p++);
626 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
627 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
630 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
631 * Futher checking is done in filelayout_check_layout */
633 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
636 if (fl->num_fh > 0) {
637 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
643 for (i = 0; i < fl->num_fh; i++) {
644 /* Do we want to use a mempool here? */
645 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
646 if (!fl->fh_array[i])
649 p = xdr_inline_decode(&stream, 4);
652 fl->fh_array[i]->size = be32_to_cpup(p++);
653 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
654 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
655 i, fl->fh_array[i]->size);
659 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
662 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
663 dprintk("DEBUG: %s: fh len %d\n", __func__,
664 fl->fh_array[i]->size);
667 __free_page(scratch);
671 filelayout_free_fh_array(fl);
673 __free_page(scratch);
678 filelayout_free_lseg(struct pnfs_layout_segment *lseg)
680 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
682 dprintk("--> %s\n", __func__);
683 nfs4_fl_put_deviceid(fl->dsaddr);
684 /* This assumes a single RW lseg */
685 if (lseg->pls_range.iomode == IOMODE_RW) {
686 struct nfs4_filelayout *flo;
688 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
689 flo->commit_info.nbuckets = 0;
690 kfree(flo->commit_info.buckets);
691 flo->commit_info.buckets = NULL;
693 _filelayout_free_lseg(fl);
697 filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
700 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
701 struct nfs4_filelayout *flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
703 struct nfs4_fl_commit_bucket *buckets;
706 if (fl->commit_through_mds)
708 if (flo->commit_info.nbuckets != 0) {
709 /* This assumes there is only one IOMODE_RW lseg. What
710 * we really want to do is have a layout_hdr level
711 * dictionary of <multipath_list4, fh> keys, each
712 * associated with a struct list_head, populated by calls
713 * to filelayout_write_pagelist().
718 size = (fl->stripe_type == STRIPE_SPARSE) ?
719 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
721 buckets = kcalloc(size, sizeof(struct nfs4_fl_commit_bucket),
728 spin_lock(&lseg->pls_layout->plh_inode->i_lock);
729 if (flo->commit_info.nbuckets != 0)
732 flo->commit_info.buckets = buckets;
733 flo->commit_info.nbuckets = size;
734 for (i = 0; i < size; i++) {
735 INIT_LIST_HEAD(&buckets[i].written);
736 INIT_LIST_HEAD(&buckets[i].committing);
739 spin_unlock(&lseg->pls_layout->plh_inode->i_lock);
744 static struct pnfs_layout_segment *
745 filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
746 struct nfs4_layoutget_res *lgr,
749 struct nfs4_filelayout_segment *fl;
751 struct nfs4_deviceid id;
753 dprintk("--> %s\n", __func__);
754 fl = kzalloc(sizeof(*fl), gfp_flags);
758 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
759 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
760 _filelayout_free_lseg(fl);
763 return &fl->generic_hdr;
767 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
769 * return true : coalesce page
770 * return false : don't coalesce page
773 filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
774 struct nfs_page *req)
776 u64 p_stripe, r_stripe;
779 if (!pnfs_generic_pg_test(pgio, prev, req) ||
780 !nfs_generic_pg_test(pgio, prev, req))
783 p_stripe = (u64)req_offset(prev);
784 r_stripe = (u64)req_offset(req);
785 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
787 do_div(p_stripe, stripe_unit);
788 do_div(r_stripe, stripe_unit);
790 return (p_stripe == r_stripe);
794 filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
795 struct nfs_page *req)
797 BUG_ON(pgio->pg_lseg != NULL);
799 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
805 /* If no lseg, fall back to read through mds */
806 if (pgio->pg_lseg == NULL)
807 nfs_pageio_reset_read_mds(pgio);
811 filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
812 struct nfs_page *req)
816 BUG_ON(pgio->pg_lseg != NULL);
818 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
824 /* If no lseg, fall back to write through mds */
825 if (pgio->pg_lseg == NULL)
827 status = filelayout_alloc_commit_info(pgio->pg_lseg, GFP_NOFS);
829 put_lseg(pgio->pg_lseg);
830 pgio->pg_lseg = NULL;
835 nfs_pageio_reset_write_mds(pgio);
838 static const struct nfs_pageio_ops filelayout_pg_read_ops = {
839 .pg_init = filelayout_pg_init_read,
840 .pg_test = filelayout_pg_test,
841 .pg_doio = pnfs_generic_pg_readpages,
844 static const struct nfs_pageio_ops filelayout_pg_write_ops = {
845 .pg_init = filelayout_pg_init_write,
846 .pg_test = filelayout_pg_test,
847 .pg_doio = pnfs_generic_pg_writepages,
850 static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
852 if (fl->stripe_type == STRIPE_SPARSE)
853 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
858 /* The generic layer is about to remove the req from the commit list.
859 * If this will make the bucket empty, it will need to put the lseg reference.
862 filelayout_clear_request_commit(struct nfs_page *req)
864 struct pnfs_layout_segment *freeme = NULL;
865 struct inode *inode = req->wb_context->dentry->d_inode;
867 spin_lock(&inode->i_lock);
868 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
870 if (list_is_singular(&req->wb_list)) {
871 struct nfs4_fl_commit_bucket *bucket;
873 bucket = list_first_entry(&req->wb_list,
874 struct nfs4_fl_commit_bucket,
876 freeme = bucket->wlseg;
877 bucket->wlseg = NULL;
880 nfs_request_remove_commit_list(req);
881 spin_unlock(&inode->i_lock);
885 static struct list_head *
886 filelayout_choose_commit_list(struct nfs_page *req,
887 struct pnfs_layout_segment *lseg)
889 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
891 struct list_head *list;
892 struct nfs4_fl_commit_bucket *buckets;
894 if (fl->commit_through_mds)
895 return &NFS_I(req->wb_context->dentry->d_inode)->commit_list;
897 /* Note that we are calling nfs4_fl_calc_j_index on each page
898 * that ends up being committed to a data server. An attractive
899 * alternative is to add a field to nfs_write_data and nfs_page
900 * to store the value calculated in filelayout_write_pagelist
901 * and just use that here.
903 j = nfs4_fl_calc_j_index(lseg, req_offset(req));
904 i = select_bucket_index(fl, j);
905 buckets = FILELAYOUT_FROM_HDR(lseg->pls_layout)->commit_info.buckets;
906 list = &buckets[i].written;
907 if (list_empty(list)) {
908 /* Non-empty buckets hold a reference on the lseg. That ref
909 * is normally transferred to the COMMIT call and released
910 * there. It could also be released if the last req is pulled
911 * off due to a rewrite, in which case it will be done in
912 * filelayout_clear_request_commit
914 buckets[i].wlseg = get_lseg(lseg);
916 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
921 filelayout_mark_request_commit(struct nfs_page *req,
922 struct pnfs_layout_segment *lseg)
924 struct list_head *list;
926 list = filelayout_choose_commit_list(req, lseg);
927 nfs_request_add_commit_list(req, list);
930 static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
932 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
934 if (flseg->stripe_type == STRIPE_SPARSE)
937 return nfs4_fl_calc_ds_index(lseg, i);
940 static struct nfs_fh *
941 select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
943 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
945 if (flseg->stripe_type == STRIPE_SPARSE) {
946 if (flseg->num_fh == 1)
948 else if (flseg->num_fh == 0)
949 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
952 return flseg->fh_array[i];
955 static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
957 struct pnfs_layout_segment *lseg = data->lseg;
958 struct nfs4_pnfs_ds *ds;
962 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
963 ds = nfs4_fl_prepare_ds(lseg, idx);
965 printk(KERN_ERR "NFS: %s: prepare_ds failed, use MDS\n",
967 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
968 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
969 prepare_to_resend_writes(data);
970 filelayout_commit_release(data);
973 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
974 data->commit_done_cb = filelayout_commit_done_cb;
975 data->ds_clp = ds->ds_clp;
976 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
979 return nfs_initiate_commit(ds->ds_clp->cl_rpcclient, data,
980 &filelayout_commit_call_ops, how);
984 filelayout_scan_ds_commit_list(struct nfs4_fl_commit_bucket *bucket, int max,
987 struct list_head *src = &bucket->written;
988 struct list_head *dst = &bucket->committing;
989 struct nfs_page *req, *tmp;
992 list_for_each_entry_safe(req, tmp, src, wb_list) {
993 if (!nfs_lock_request(req))
995 if (cond_resched_lock(lock))
996 list_safe_reset_next(req, tmp, wb_list);
997 nfs_request_remove_commit_list(req);
998 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
999 nfs_list_add_request(req, dst);
1005 bucket->clseg = bucket->wlseg;
1006 if (list_empty(src))
1007 bucket->wlseg = NULL;
1009 get_lseg(bucket->clseg);
1014 /* Move reqs from written to committing lists, returning count of number moved.
1015 * Note called with i_lock held.
1017 static int filelayout_scan_commit_lists(struct inode *inode, int max,
1020 struct nfs4_fl_commit_info *fl_cinfo;
1023 fl_cinfo = &FILELAYOUT_FROM_HDR(NFS_I(inode)->layout)->commit_info;
1024 if (fl_cinfo->nbuckets == 0)
1026 for (i = 0; i < fl_cinfo->nbuckets && max != 0; i++) {
1027 cnt = filelayout_scan_ds_commit_list(&fl_cinfo->buckets[i],
1037 alloc_ds_commits(struct inode *inode, struct list_head *list)
1039 struct nfs4_fl_commit_info *fl_cinfo;
1040 struct nfs4_fl_commit_bucket *bucket;
1041 struct nfs_commit_data *data;
1043 unsigned int nreq = 0;
1045 fl_cinfo = &FILELAYOUT_FROM_HDR(NFS_I(inode)->layout)->commit_info;
1046 bucket = fl_cinfo->buckets;
1047 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
1048 if (list_empty(&bucket->committing))
1050 data = nfs_commitdata_alloc();
1053 data->ds_commit_index = i;
1054 data->lseg = bucket->clseg;
1055 bucket->clseg = NULL;
1056 list_add(&data->pages, list);
1060 /* Clean up on error */
1061 for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
1062 if (list_empty(&bucket->committing))
1064 nfs_retry_commit(&bucket->committing, bucket->clseg);
1065 put_lseg(bucket->clseg);
1066 bucket->clseg = NULL;
1068 /* Caller will clean up entries put on list */
1072 /* This follows nfs_commit_list pretty closely */
1074 filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1077 struct nfs_commit_data *data, *tmp;
1079 unsigned int nreq = 0;
1081 if (!list_empty(mds_pages)) {
1082 data = nfs_commitdata_alloc();
1085 list_add(&data->pages, &list);
1088 nfs_retry_commit(mds_pages, NULL);
1091 nreq += alloc_ds_commits(inode, &list);
1094 nfs_commit_clear_lock(NFS_I(inode));
1098 atomic_add(nreq, &NFS_I(inode)->commits_outstanding);
1100 list_for_each_entry_safe(data, tmp, &list, pages) {
1101 list_del_init(&data->pages);
1103 nfs_init_commit(data, mds_pages, NULL);
1104 nfs_initiate_commit(NFS_CLIENT(inode), data,
1105 data->mds_ops, how);
1107 struct nfs4_fl_commit_info *fl_cinfo;
1109 fl_cinfo = &FILELAYOUT_FROM_HDR(data->lseg->pls_layout)->commit_info;
1110 nfs_init_commit(data, &fl_cinfo->buckets[data->ds_commit_index].committing, data->lseg);
1111 filelayout_initiate_commit(data, how);
1115 return PNFS_ATTEMPTED;
1119 filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1121 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1124 static struct pnfs_layout_hdr *
1125 filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1127 struct nfs4_filelayout *flo;
1129 flo = kzalloc(sizeof(*flo), gfp_flags);
1130 return &flo->generic_hdr;
1134 filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1136 kfree(FILELAYOUT_FROM_HDR(lo));
1139 static struct pnfs_layoutdriver_type filelayout_type = {
1140 .id = LAYOUT_NFSV4_1_FILES,
1141 .name = "LAYOUT_NFSV4_1_FILES",
1142 .owner = THIS_MODULE,
1143 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1144 .free_layout_hdr = filelayout_free_layout_hdr,
1145 .alloc_lseg = filelayout_alloc_lseg,
1146 .free_lseg = filelayout_free_lseg,
1147 .pg_read_ops = &filelayout_pg_read_ops,
1148 .pg_write_ops = &filelayout_pg_write_ops,
1149 .mark_request_commit = filelayout_mark_request_commit,
1150 .clear_request_commit = filelayout_clear_request_commit,
1151 .scan_commit_lists = filelayout_scan_commit_lists,
1152 .commit_pagelist = filelayout_commit_pagelist,
1153 .read_pagelist = filelayout_read_pagelist,
1154 .write_pagelist = filelayout_write_pagelist,
1155 .free_deviceid_node = filelayout_free_deveiceid_node,
1158 static int __init nfs4filelayout_init(void)
1160 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1162 return pnfs_register_layoutdriver(&filelayout_type);
1165 static void __exit nfs4filelayout_exit(void)
1167 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1169 pnfs_unregister_layoutdriver(&filelayout_type);
1172 MODULE_ALIAS("nfs-layouttype4-1");
1174 module_init(nfs4filelayout_init);
1175 module_exit(nfs4filelayout_exit);