NFSv4.1: Fix a bad reference count issue in the pNFS commit code
[linux-2.6.git] / fs / nfs / nfs4filelayout.c
blob33849d3bc885976840abd484dc1e27eadbc21ca6
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);
104 /* balance nfs_get_client in filelayout_write_pagelist */
105 nfs_put_client(data->ds_clp);
106 data->ds_clp = NULL;
109 static void filelayout_reset_read(struct nfs_read_data *data)
111 struct nfs_pgio_header *hdr = data->header;
112 struct inode *inode = hdr->inode;
113 struct rpc_task *task = &data->task;
115 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
116 dprintk("%s Reset task %5u for i/o through MDS "
117 "(req %s/%lld, %u bytes @ offset %llu)\n", __func__,
118 data->task.tk_pid,
119 inode->i_sb->s_id,
120 (long long)NFS_FILEID(inode),
121 data->args.count,
122 (unsigned long long)data->args.offset);
124 task->tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
125 &hdr->pages,
126 hdr->completion_ops);
128 /* balance nfs_get_client in filelayout_read_pagelist */
129 nfs_put_client(data->ds_clp);
130 data->ds_clp = NULL;
133 static int filelayout_async_handle_error(struct rpc_task *task,
134 struct nfs4_state *state,
135 struct nfs_client *clp,
136 struct pnfs_layout_segment *lseg)
138 struct inode *inode = lseg->pls_layout->plh_inode;
139 struct nfs_server *mds_server = NFS_SERVER(inode);
140 struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
141 struct nfs_client *mds_client = mds_server->nfs_client;
142 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
144 if (task->tk_status >= 0)
145 return 0;
147 switch (task->tk_status) {
148 /* MDS state errors */
149 case -NFS4ERR_DELEG_REVOKED:
150 case -NFS4ERR_ADMIN_REVOKED:
151 case -NFS4ERR_BAD_STATEID:
152 if (state == NULL)
153 break;
154 nfs_remove_bad_delegation(state->inode);
155 case -NFS4ERR_OPENMODE:
156 if (state == NULL)
157 break;
158 nfs4_schedule_stateid_recovery(mds_server, state);
159 goto wait_on_recovery;
160 case -NFS4ERR_EXPIRED:
161 if (state != NULL)
162 nfs4_schedule_stateid_recovery(mds_server, state);
163 nfs4_schedule_lease_recovery(mds_client);
164 goto wait_on_recovery;
165 /* DS session errors */
166 case -NFS4ERR_BADSESSION:
167 case -NFS4ERR_BADSLOT:
168 case -NFS4ERR_BAD_HIGH_SLOT:
169 case -NFS4ERR_DEADSESSION:
170 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
171 case -NFS4ERR_SEQ_FALSE_RETRY:
172 case -NFS4ERR_SEQ_MISORDERED:
173 dprintk("%s ERROR %d, Reset session. Exchangeid "
174 "flags 0x%x\n", __func__, task->tk_status,
175 clp->cl_exchange_flags);
176 nfs4_schedule_session_recovery(clp->cl_session);
177 break;
178 case -NFS4ERR_DELAY:
179 case -NFS4ERR_GRACE:
180 case -EKEYEXPIRED:
181 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
182 break;
183 case -NFS4ERR_RETRY_UNCACHED_REP:
184 break;
185 /* Invalidate Layout errors */
186 case -NFS4ERR_PNFS_NO_LAYOUT:
187 case -ESTALE: /* mapped NFS4ERR_STALE */
188 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
189 case -EISDIR: /* mapped NFS4ERR_ISDIR */
190 case -NFS4ERR_FHEXPIRED:
191 case -NFS4ERR_WRONG_TYPE:
192 dprintk("%s Invalid layout error %d\n", __func__,
193 task->tk_status);
195 * Destroy layout so new i/o will get a new layout.
196 * Layout will not be destroyed until all current lseg
197 * references are put. Mark layout as invalid to resend failed
198 * i/o and all i/o waiting on the slot table to the MDS until
199 * layout is destroyed and a new valid layout is obtained.
201 set_bit(NFS_LAYOUT_INVALID,
202 &NFS_I(state->inode)->layout->plh_flags);
203 pnfs_destroy_layout(NFS_I(state->inode));
204 rpc_wake_up(&tbl->slot_tbl_waitq);
205 goto reset;
206 /* RPC connection errors */
207 case -ECONNREFUSED:
208 case -EHOSTDOWN:
209 case -EHOSTUNREACH:
210 case -ENETUNREACH:
211 case -EIO:
212 case -ETIMEDOUT:
213 case -EPIPE:
214 dprintk("%s DS connection error %d\n", __func__,
215 task->tk_status);
216 if (!filelayout_test_devid_invalid(devid))
217 _pnfs_return_layout(state->inode);
218 filelayout_mark_devid_invalid(devid);
219 rpc_wake_up(&tbl->slot_tbl_waitq);
220 nfs4_ds_disconnect(clp);
221 /* fall through */
222 default:
223 reset:
224 dprintk("%s Retry through MDS. Error %d\n", __func__,
225 task->tk_status);
226 return -NFS4ERR_RESET_TO_MDS;
228 out:
229 task->tk_status = 0;
230 return -EAGAIN;
231 wait_on_recovery:
232 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
233 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
234 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
235 goto out;
238 /* NFS_PROTO call done callback routines */
240 static int filelayout_read_done_cb(struct rpc_task *task,
241 struct nfs_read_data *data)
243 struct nfs_pgio_header *hdr = data->header;
244 int err;
246 err = filelayout_async_handle_error(task, data->args.context->state,
247 data->ds_clp, hdr->lseg);
249 switch (err) {
250 case -NFS4ERR_RESET_TO_MDS:
251 filelayout_reset_read(data);
252 return task->tk_status;
253 case -EAGAIN:
254 rpc_restart_call_prepare(task);
255 return -EAGAIN;
258 return 0;
262 * We reference the rpc_cred of the first WRITE that triggers the need for
263 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
264 * rfc5661 is not clear about which credential should be used.
266 static void
267 filelayout_set_layoutcommit(struct nfs_write_data *wdata)
269 struct nfs_pgio_header *hdr = wdata->header;
271 if (FILELAYOUT_LSEG(hdr->lseg)->commit_through_mds ||
272 wdata->res.verf->committed == NFS_FILE_SYNC)
273 return;
275 pnfs_set_layoutcommit(wdata);
276 dprintk("%s ionde %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
277 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
281 * Call ops for the async read/write cases
282 * In the case of dense layouts, the offset needs to be reset to its
283 * original value.
285 static void filelayout_read_prepare(struct rpc_task *task, void *data)
287 struct nfs_read_data *rdata = data;
289 if (filelayout_reset_to_mds(rdata->header->lseg)) {
290 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
291 filelayout_reset_read(rdata);
292 rpc_exit(task, 0);
293 return;
295 rdata->read_done_cb = filelayout_read_done_cb;
297 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
298 &rdata->args.seq_args, &rdata->res.seq_res,
299 task))
300 return;
302 rpc_call_start(task);
305 static void filelayout_read_call_done(struct rpc_task *task, void *data)
307 struct nfs_read_data *rdata = data;
309 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
311 if (test_bit(NFS_IOHDR_REDO, &rdata->header->flags))
312 return;
314 /* Note this may cause RPC to be resent */
315 rdata->header->mds_ops->rpc_call_done(task, data);
318 static void filelayout_read_count_stats(struct rpc_task *task, void *data)
320 struct nfs_read_data *rdata = data;
322 rpc_count_iostats(task, NFS_SERVER(rdata->header->inode)->client->cl_metrics);
325 static void filelayout_read_release(void *data)
327 struct nfs_read_data *rdata = data;
329 if (!test_bit(NFS_IOHDR_REDO, &rdata->header->flags))
330 nfs_put_client(rdata->ds_clp);
331 rdata->header->mds_ops->rpc_release(data);
334 static int filelayout_write_done_cb(struct rpc_task *task,
335 struct nfs_write_data *data)
337 struct nfs_pgio_header *hdr = data->header;
338 int err;
340 err = filelayout_async_handle_error(task, data->args.context->state,
341 data->ds_clp, hdr->lseg);
343 switch (err) {
344 case -NFS4ERR_RESET_TO_MDS:
345 filelayout_reset_write(data);
346 return task->tk_status;
347 case -EAGAIN:
348 rpc_restart_call_prepare(task);
349 return -EAGAIN;
352 filelayout_set_layoutcommit(data);
353 return 0;
356 /* Fake up some data that will cause nfs_commit_release to retry the writes. */
357 static void prepare_to_resend_writes(struct nfs_commit_data *data)
359 struct nfs_page *first = nfs_list_entry(data->pages.next);
361 data->task.tk_status = 0;
362 memcpy(data->verf.verifier, first->wb_verf.verifier,
363 sizeof(first->wb_verf.verifier));
364 data->verf.verifier[0]++; /* ensure verifier mismatch */
367 static int filelayout_commit_done_cb(struct rpc_task *task,
368 struct nfs_commit_data *data)
370 int err;
372 err = filelayout_async_handle_error(task, NULL, data->ds_clp,
373 data->lseg);
375 switch (err) {
376 case -NFS4ERR_RESET_TO_MDS:
377 prepare_to_resend_writes(data);
378 return -EAGAIN;
379 case -EAGAIN:
380 rpc_restart_call_prepare(task);
381 return -EAGAIN;
384 return 0;
387 static void filelayout_write_prepare(struct rpc_task *task, void *data)
389 struct nfs_write_data *wdata = data;
391 if (filelayout_reset_to_mds(wdata->header->lseg)) {
392 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
393 filelayout_reset_write(wdata);
394 rpc_exit(task, 0);
395 return;
397 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
398 &wdata->args.seq_args, &wdata->res.seq_res,
399 task))
400 return;
402 rpc_call_start(task);
405 static void filelayout_write_call_done(struct rpc_task *task, void *data)
407 struct nfs_write_data *wdata = data;
409 if (test_bit(NFS_IOHDR_REDO, &wdata->header->flags))
410 return;
412 /* Note this may cause RPC to be resent */
413 wdata->header->mds_ops->rpc_call_done(task, data);
416 static void filelayout_write_count_stats(struct rpc_task *task, void *data)
418 struct nfs_write_data *wdata = data;
420 rpc_count_iostats(task, NFS_SERVER(wdata->header->inode)->client->cl_metrics);
423 static void filelayout_write_release(void *data)
425 struct nfs_write_data *wdata = data;
427 if (!test_bit(NFS_IOHDR_REDO, &wdata->header->flags))
428 nfs_put_client(wdata->ds_clp);
429 wdata->header->mds_ops->rpc_release(data);
432 static void filelayout_commit_prepare(struct rpc_task *task, void *data)
434 struct nfs_commit_data *wdata = data;
436 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
437 &wdata->args.seq_args, &wdata->res.seq_res,
438 task))
439 return;
441 rpc_call_start(task);
444 static void filelayout_write_commit_done(struct rpc_task *task, void *data)
446 struct nfs_commit_data *wdata = data;
448 /* Note this may cause RPC to be resent */
449 wdata->mds_ops->rpc_call_done(task, data);
452 static void filelayout_commit_count_stats(struct rpc_task *task, void *data)
454 struct nfs_commit_data *cdata = data;
456 rpc_count_iostats(task, NFS_SERVER(cdata->inode)->client->cl_metrics);
459 static void filelayout_commit_release(void *calldata)
461 struct nfs_commit_data *data = calldata;
463 data->completion_ops->completion(data);
464 put_lseg(data->lseg);
465 nfs_put_client(data->ds_clp);
466 nfs_commitdata_release(data);
469 static const struct rpc_call_ops filelayout_read_call_ops = {
470 .rpc_call_prepare = filelayout_read_prepare,
471 .rpc_call_done = filelayout_read_call_done,
472 .rpc_count_stats = filelayout_read_count_stats,
473 .rpc_release = filelayout_read_release,
476 static const struct rpc_call_ops filelayout_write_call_ops = {
477 .rpc_call_prepare = filelayout_write_prepare,
478 .rpc_call_done = filelayout_write_call_done,
479 .rpc_count_stats = filelayout_write_count_stats,
480 .rpc_release = filelayout_write_release,
483 static const struct rpc_call_ops filelayout_commit_call_ops = {
484 .rpc_call_prepare = filelayout_commit_prepare,
485 .rpc_call_done = filelayout_write_commit_done,
486 .rpc_count_stats = filelayout_commit_count_stats,
487 .rpc_release = filelayout_commit_release,
490 static enum pnfs_try_status
491 filelayout_read_pagelist(struct nfs_read_data *data)
493 struct nfs_pgio_header *hdr = data->header;
494 struct pnfs_layout_segment *lseg = hdr->lseg;
495 struct nfs4_pnfs_ds *ds;
496 loff_t offset = data->args.offset;
497 u32 j, idx;
498 struct nfs_fh *fh;
499 int status;
501 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
502 __func__, hdr->inode->i_ino,
503 data->args.pgbase, (size_t)data->args.count, offset);
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 USE DS: %s cl_count %d\n", __func__,
512 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
514 /* No multipath support. Use first DS */
515 atomic_inc(&ds->ds_clp->cl_count);
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 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
522 data->mds_offset = offset;
524 /* Perform an asynchronous read to ds */
525 status = nfs_initiate_read(ds->ds_clp->cl_rpcclient, data,
526 &filelayout_read_call_ops, RPC_TASK_SOFTCONN);
527 BUG_ON(status != 0);
528 return PNFS_ATTEMPTED;
531 /* Perform async writes. */
532 static enum pnfs_try_status
533 filelayout_write_pagelist(struct nfs_write_data *data, int sync)
535 struct nfs_pgio_header *hdr = data->header;
536 struct pnfs_layout_segment *lseg = hdr->lseg;
537 struct nfs4_pnfs_ds *ds;
538 loff_t offset = data->args.offset;
539 u32 j, idx;
540 struct nfs_fh *fh;
541 int status;
543 /* Retrieve the correct rpc_client for the byte range */
544 j = nfs4_fl_calc_j_index(lseg, offset);
545 idx = nfs4_fl_calc_ds_index(lseg, j);
546 ds = nfs4_fl_prepare_ds(lseg, idx);
547 if (!ds)
548 return PNFS_NOT_ATTEMPTED;
549 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d\n",
550 __func__, hdr->inode->i_ino, sync, (size_t) data->args.count,
551 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count));
553 data->write_done_cb = filelayout_write_done_cb;
554 atomic_inc(&ds->ds_clp->cl_count);
555 data->ds_clp = ds->ds_clp;
556 fh = nfs4_fl_select_ds_fh(lseg, j);
557 if (fh)
558 data->args.fh = fh;
560 * Get the file offset on the dserver. Set the write offset to
561 * this offset and save the original offset.
563 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
565 /* Perform an asynchronous write */
566 status = nfs_initiate_write(ds->ds_clp->cl_rpcclient, data,
567 &filelayout_write_call_ops, sync,
568 RPC_TASK_SOFTCONN);
569 BUG_ON(status != 0);
570 return PNFS_ATTEMPTED;
574 * filelayout_check_layout()
576 * Make sure layout segment parameters are sane WRT the device.
577 * At this point no generic layer initialization of the lseg has occurred,
578 * and nothing has been added to the layout_hdr cache.
581 static int
582 filelayout_check_layout(struct pnfs_layout_hdr *lo,
583 struct nfs4_filelayout_segment *fl,
584 struct nfs4_layoutget_res *lgr,
585 struct nfs4_deviceid *id,
586 gfp_t gfp_flags)
588 struct nfs4_deviceid_node *d;
589 struct nfs4_file_layout_dsaddr *dsaddr;
590 int status = -EINVAL;
591 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
593 dprintk("--> %s\n", __func__);
595 /* FIXME: remove this check when layout segment support is added */
596 if (lgr->range.offset != 0 ||
597 lgr->range.length != NFS4_MAX_UINT64) {
598 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
599 __func__);
600 goto out;
603 if (fl->pattern_offset > lgr->range.offset) {
604 dprintk("%s pattern_offset %lld too large\n",
605 __func__, fl->pattern_offset);
606 goto out;
609 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
610 dprintk("%s Invalid stripe unit (%u)\n",
611 __func__, fl->stripe_unit);
612 goto out;
615 /* find and reference the deviceid */
616 d = nfs4_find_get_deviceid(NFS_SERVER(lo->plh_inode)->pnfs_curr_ld,
617 NFS_SERVER(lo->plh_inode)->nfs_client, id);
618 if (d == NULL) {
619 dsaddr = get_device_info(lo->plh_inode, id, gfp_flags);
620 if (dsaddr == NULL)
621 goto out;
622 } else
623 dsaddr = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
624 /* Found deviceid is being reaped */
625 if (test_bit(NFS_DEVICEID_INVALID, &dsaddr->id_node.flags))
626 goto out_put;
628 fl->dsaddr = dsaddr;
630 if (fl->first_stripe_index >= dsaddr->stripe_count) {
631 dprintk("%s Bad first_stripe_index %u\n",
632 __func__, fl->first_stripe_index);
633 goto out_put;
636 if ((fl->stripe_type == STRIPE_SPARSE &&
637 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
638 (fl->stripe_type == STRIPE_DENSE &&
639 fl->num_fh != dsaddr->stripe_count)) {
640 dprintk("%s num_fh %u not valid for given packing\n",
641 __func__, fl->num_fh);
642 goto out_put;
645 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
646 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
647 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
648 nfss->wsize);
651 status = 0;
652 out:
653 dprintk("--> %s returns %d\n", __func__, status);
654 return status;
655 out_put:
656 nfs4_fl_put_deviceid(dsaddr);
657 goto out;
660 static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
662 int i;
664 for (i = 0; i < fl->num_fh; i++) {
665 if (!fl->fh_array[i])
666 break;
667 kfree(fl->fh_array[i]);
669 kfree(fl->fh_array);
670 fl->fh_array = NULL;
673 static void
674 _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
676 filelayout_free_fh_array(fl);
677 kfree(fl);
680 static int
681 filelayout_decode_layout(struct pnfs_layout_hdr *flo,
682 struct nfs4_filelayout_segment *fl,
683 struct nfs4_layoutget_res *lgr,
684 struct nfs4_deviceid *id,
685 gfp_t gfp_flags)
687 struct xdr_stream stream;
688 struct xdr_buf buf;
689 struct page *scratch;
690 __be32 *p;
691 uint32_t nfl_util;
692 int i;
694 dprintk("%s: set_layout_map Begin\n", __func__);
696 scratch = alloc_page(gfp_flags);
697 if (!scratch)
698 return -ENOMEM;
700 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, lgr->layoutp->len);
701 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
703 /* 20 = ufl_util (4), first_stripe_index (4), pattern_offset (8),
704 * num_fh (4) */
705 p = xdr_inline_decode(&stream, NFS4_DEVICEID4_SIZE + 20);
706 if (unlikely(!p))
707 goto out_err;
709 memcpy(id, p, sizeof(*id));
710 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
711 nfs4_print_deviceid(id);
713 nfl_util = be32_to_cpup(p++);
714 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
715 fl->commit_through_mds = 1;
716 if (nfl_util & NFL4_UFLG_DENSE)
717 fl->stripe_type = STRIPE_DENSE;
718 else
719 fl->stripe_type = STRIPE_SPARSE;
720 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
722 fl->first_stripe_index = be32_to_cpup(p++);
723 p = xdr_decode_hyper(p, &fl->pattern_offset);
724 fl->num_fh = be32_to_cpup(p++);
726 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
727 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
728 fl->pattern_offset);
730 /* Note that a zero value for num_fh is legal for STRIPE_SPARSE.
731 * Futher checking is done in filelayout_check_layout */
732 if (fl->num_fh >
733 max(NFS4_PNFS_MAX_STRIPE_CNT, NFS4_PNFS_MAX_MULTI_CNT))
734 goto out_err;
736 if (fl->num_fh > 0) {
737 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
738 gfp_flags);
739 if (!fl->fh_array)
740 goto out_err;
743 for (i = 0; i < fl->num_fh; i++) {
744 /* Do we want to use a mempool here? */
745 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), gfp_flags);
746 if (!fl->fh_array[i])
747 goto out_err_free;
749 p = xdr_inline_decode(&stream, 4);
750 if (unlikely(!p))
751 goto out_err_free;
752 fl->fh_array[i]->size = be32_to_cpup(p++);
753 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
754 printk(KERN_ERR "NFS: Too big fh %d received %d\n",
755 i, fl->fh_array[i]->size);
756 goto out_err_free;
759 p = xdr_inline_decode(&stream, fl->fh_array[i]->size);
760 if (unlikely(!p))
761 goto out_err_free;
762 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
763 dprintk("DEBUG: %s: fh len %d\n", __func__,
764 fl->fh_array[i]->size);
767 __free_page(scratch);
768 return 0;
770 out_err_free:
771 filelayout_free_fh_array(fl);
772 out_err:
773 __free_page(scratch);
774 return -EIO;
777 static void
778 filelayout_free_lseg(struct pnfs_layout_segment *lseg)
780 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
782 dprintk("--> %s\n", __func__);
783 nfs4_fl_put_deviceid(fl->dsaddr);
784 /* This assumes a single RW lseg */
785 if (lseg->pls_range.iomode == IOMODE_RW) {
786 struct nfs4_filelayout *flo;
788 flo = FILELAYOUT_FROM_HDR(lseg->pls_layout);
789 flo->commit_info.nbuckets = 0;
790 kfree(flo->commit_info.buckets);
791 flo->commit_info.buckets = NULL;
793 _filelayout_free_lseg(fl);
796 static int
797 filelayout_alloc_commit_info(struct pnfs_layout_segment *lseg,
798 struct nfs_commit_info *cinfo,
799 gfp_t gfp_flags)
801 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
802 struct pnfs_commit_bucket *buckets;
803 int size;
805 if (fl->commit_through_mds)
806 return 0;
807 if (cinfo->ds->nbuckets != 0) {
808 /* This assumes there is only one IOMODE_RW lseg. What
809 * we really want to do is have a layout_hdr level
810 * dictionary of <multipath_list4, fh> keys, each
811 * associated with a struct list_head, populated by calls
812 * to filelayout_write_pagelist().
813 * */
814 return 0;
817 size = (fl->stripe_type == STRIPE_SPARSE) ?
818 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
820 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
821 gfp_flags);
822 if (!buckets)
823 return -ENOMEM;
824 else {
825 int i;
827 spin_lock(cinfo->lock);
828 if (cinfo->ds->nbuckets != 0)
829 kfree(buckets);
830 else {
831 cinfo->ds->buckets = buckets;
832 cinfo->ds->nbuckets = size;
833 for (i = 0; i < size; i++) {
834 INIT_LIST_HEAD(&buckets[i].written);
835 INIT_LIST_HEAD(&buckets[i].committing);
838 spin_unlock(cinfo->lock);
839 return 0;
843 static struct pnfs_layout_segment *
844 filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
845 struct nfs4_layoutget_res *lgr,
846 gfp_t gfp_flags)
848 struct nfs4_filelayout_segment *fl;
849 int rc;
850 struct nfs4_deviceid id;
852 dprintk("--> %s\n", __func__);
853 fl = kzalloc(sizeof(*fl), gfp_flags);
854 if (!fl)
855 return NULL;
857 rc = filelayout_decode_layout(layoutid, fl, lgr, &id, gfp_flags);
858 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id, gfp_flags)) {
859 _filelayout_free_lseg(fl);
860 return NULL;
862 return &fl->generic_hdr;
866 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
868 * return true : coalesce page
869 * return false : don't coalesce page
871 static bool
872 filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
873 struct nfs_page *req)
875 u64 p_stripe, r_stripe;
876 u32 stripe_unit;
878 if (!pnfs_generic_pg_test(pgio, prev, req) ||
879 !nfs_generic_pg_test(pgio, prev, req))
880 return false;
882 p_stripe = (u64)req_offset(prev);
883 r_stripe = (u64)req_offset(req);
884 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
886 do_div(p_stripe, stripe_unit);
887 do_div(r_stripe, stripe_unit);
889 return (p_stripe == r_stripe);
892 static void
893 filelayout_pg_init_read(struct nfs_pageio_descriptor *pgio,
894 struct nfs_page *req)
896 BUG_ON(pgio->pg_lseg != NULL);
898 if (req->wb_offset != req->wb_pgbase) {
900 * Handling unaligned pages is difficult, because have to
901 * somehow split a req in two in certain cases in the
902 * pg.test code. Avoid this by just not using pnfs
903 * in this case.
905 nfs_pageio_reset_read_mds(pgio);
906 return;
908 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
909 req->wb_context,
911 NFS4_MAX_UINT64,
912 IOMODE_READ,
913 GFP_KERNEL);
914 /* If no lseg, fall back to read through mds */
915 if (pgio->pg_lseg == NULL)
916 nfs_pageio_reset_read_mds(pgio);
919 static void
920 filelayout_pg_init_write(struct nfs_pageio_descriptor *pgio,
921 struct nfs_page *req)
923 struct nfs_commit_info cinfo;
924 int status;
926 BUG_ON(pgio->pg_lseg != NULL);
928 if (req->wb_offset != req->wb_pgbase)
929 goto out_mds;
930 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
931 req->wb_context,
933 NFS4_MAX_UINT64,
934 IOMODE_RW,
935 GFP_NOFS);
936 /* If no lseg, fall back to write through mds */
937 if (pgio->pg_lseg == NULL)
938 goto out_mds;
939 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
940 status = filelayout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
941 if (status < 0) {
942 put_lseg(pgio->pg_lseg);
943 pgio->pg_lseg = NULL;
944 goto out_mds;
946 return;
947 out_mds:
948 nfs_pageio_reset_write_mds(pgio);
951 static const struct nfs_pageio_ops filelayout_pg_read_ops = {
952 .pg_init = filelayout_pg_init_read,
953 .pg_test = filelayout_pg_test,
954 .pg_doio = pnfs_generic_pg_readpages,
957 static const struct nfs_pageio_ops filelayout_pg_write_ops = {
958 .pg_init = filelayout_pg_init_write,
959 .pg_test = filelayout_pg_test,
960 .pg_doio = pnfs_generic_pg_writepages,
963 static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
965 if (fl->stripe_type == STRIPE_SPARSE)
966 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
967 else
968 return j;
971 /* The generic layer is about to remove the req from the commit list.
972 * If this will make the bucket empty, it will need to put the lseg reference.
974 static void
975 filelayout_clear_request_commit(struct nfs_page *req,
976 struct nfs_commit_info *cinfo)
978 struct pnfs_layout_segment *freeme = NULL;
980 spin_lock(cinfo->lock);
981 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
982 goto out;
983 cinfo->ds->nwritten--;
984 if (list_is_singular(&req->wb_list)) {
985 struct pnfs_commit_bucket *bucket;
987 bucket = list_first_entry(&req->wb_list,
988 struct pnfs_commit_bucket,
989 written);
990 freeme = bucket->wlseg;
991 bucket->wlseg = NULL;
993 out:
994 nfs_request_remove_commit_list(req, cinfo);
995 spin_unlock(cinfo->lock);
996 put_lseg(freeme);
999 static struct list_head *
1000 filelayout_choose_commit_list(struct nfs_page *req,
1001 struct pnfs_layout_segment *lseg,
1002 struct nfs_commit_info *cinfo)
1004 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
1005 u32 i, j;
1006 struct list_head *list;
1007 struct pnfs_commit_bucket *buckets;
1009 if (fl->commit_through_mds)
1010 return &cinfo->mds->list;
1012 /* Note that we are calling nfs4_fl_calc_j_index on each page
1013 * that ends up being committed to a data server. An attractive
1014 * alternative is to add a field to nfs_write_data and nfs_page
1015 * to store the value calculated in filelayout_write_pagelist
1016 * and just use that here.
1018 j = nfs4_fl_calc_j_index(lseg, req_offset(req));
1019 i = select_bucket_index(fl, j);
1020 buckets = cinfo->ds->buckets;
1021 list = &buckets[i].written;
1022 if (list_empty(list)) {
1023 /* Non-empty buckets hold a reference on the lseg. That ref
1024 * is normally transferred to the COMMIT call and released
1025 * there. It could also be released if the last req is pulled
1026 * off due to a rewrite, in which case it will be done in
1027 * filelayout_clear_request_commit
1029 buckets[i].wlseg = get_lseg(lseg);
1031 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1032 cinfo->ds->nwritten++;
1033 return list;
1036 static void
1037 filelayout_mark_request_commit(struct nfs_page *req,
1038 struct pnfs_layout_segment *lseg,
1039 struct nfs_commit_info *cinfo)
1041 struct list_head *list;
1043 list = filelayout_choose_commit_list(req, lseg, cinfo);
1044 nfs_request_add_commit_list(req, list, cinfo);
1047 static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1049 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1051 if (flseg->stripe_type == STRIPE_SPARSE)
1052 return i;
1053 else
1054 return nfs4_fl_calc_ds_index(lseg, i);
1057 static struct nfs_fh *
1058 select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1060 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
1062 if (flseg->stripe_type == STRIPE_SPARSE) {
1063 if (flseg->num_fh == 1)
1064 i = 0;
1065 else if (flseg->num_fh == 0)
1066 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
1067 return NULL;
1069 return flseg->fh_array[i];
1072 static int filelayout_initiate_commit(struct nfs_commit_data *data, int how)
1074 struct pnfs_layout_segment *lseg = data->lseg;
1075 struct nfs4_pnfs_ds *ds;
1076 u32 idx;
1077 struct nfs_fh *fh;
1079 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1080 ds = nfs4_fl_prepare_ds(lseg, idx);
1081 if (!ds) {
1082 prepare_to_resend_writes(data);
1083 filelayout_commit_release(data);
1084 return -EAGAIN;
1086 dprintk("%s ino %lu, how %d cl_count %d\n", __func__,
1087 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count));
1088 data->commit_done_cb = filelayout_commit_done_cb;
1089 atomic_inc(&ds->ds_clp->cl_count);
1090 data->ds_clp = ds->ds_clp;
1091 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1092 if (fh)
1093 data->args.fh = fh;
1094 return nfs_initiate_commit(ds->ds_clp->cl_rpcclient, data,
1095 &filelayout_commit_call_ops, how,
1096 RPC_TASK_SOFTCONN);
1099 static int
1100 transfer_commit_list(struct list_head *src, struct list_head *dst,
1101 struct nfs_commit_info *cinfo, int max)
1103 struct nfs_page *req, *tmp;
1104 int ret = 0;
1106 list_for_each_entry_safe(req, tmp, src, wb_list) {
1107 if (!nfs_lock_request(req))
1108 continue;
1109 kref_get(&req->wb_kref);
1110 if (cond_resched_lock(cinfo->lock))
1111 list_safe_reset_next(req, tmp, wb_list);
1112 nfs_request_remove_commit_list(req, cinfo);
1113 clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1114 nfs_list_add_request(req, dst);
1115 ret++;
1116 if ((ret == max) && !cinfo->dreq)
1117 break;
1119 return ret;
1122 static int
1123 filelayout_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
1124 struct nfs_commit_info *cinfo,
1125 int max)
1127 struct list_head *src = &bucket->written;
1128 struct list_head *dst = &bucket->committing;
1129 int ret;
1131 ret = transfer_commit_list(src, dst, cinfo, max);
1132 if (ret) {
1133 cinfo->ds->nwritten -= ret;
1134 cinfo->ds->ncommitting += ret;
1135 bucket->clseg = bucket->wlseg;
1136 if (list_empty(src))
1137 bucket->wlseg = NULL;
1138 else
1139 get_lseg(bucket->clseg);
1141 return ret;
1144 /* Move reqs from written to committing lists, returning count of number moved.
1145 * Note called with cinfo->lock held.
1147 static int filelayout_scan_commit_lists(struct nfs_commit_info *cinfo,
1148 int max)
1150 int i, rv = 0, cnt;
1152 for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
1153 cnt = filelayout_scan_ds_commit_list(&cinfo->ds->buckets[i],
1154 cinfo, max);
1155 max -= cnt;
1156 rv += cnt;
1158 return rv;
1161 /* Pull everything off the committing lists and dump into @dst */
1162 static void filelayout_recover_commit_reqs(struct list_head *dst,
1163 struct nfs_commit_info *cinfo)
1165 struct pnfs_commit_bucket *b;
1166 int i;
1168 /* NOTE cinfo->lock is NOT held, relying on fact that this is
1169 * only called on single thread per dreq.
1170 * Can't take the lock because need to do put_lseg
1172 for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
1173 if (transfer_commit_list(&b->written, dst, cinfo, 0)) {
1174 BUG_ON(!list_empty(&b->written));
1175 put_lseg(b->wlseg);
1176 b->wlseg = NULL;
1179 cinfo->ds->nwritten = 0;
1182 static unsigned int
1183 alloc_ds_commits(struct nfs_commit_info *cinfo, struct list_head *list)
1185 struct pnfs_ds_commit_info *fl_cinfo;
1186 struct pnfs_commit_bucket *bucket;
1187 struct nfs_commit_data *data;
1188 int i, j;
1189 unsigned int nreq = 0;
1191 fl_cinfo = cinfo->ds;
1192 bucket = fl_cinfo->buckets;
1193 for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
1194 if (list_empty(&bucket->committing))
1195 continue;
1196 data = nfs_commitdata_alloc();
1197 if (!data)
1198 break;
1199 data->ds_commit_index = i;
1200 data->lseg = bucket->clseg;
1201 bucket->clseg = NULL;
1202 list_add(&data->pages, list);
1203 nreq++;
1206 /* Clean up on error */
1207 for (j = i; j < fl_cinfo->nbuckets; j++, bucket++) {
1208 if (list_empty(&bucket->committing))
1209 continue;
1210 nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo);
1211 put_lseg(bucket->clseg);
1212 bucket->clseg = NULL;
1214 /* Caller will clean up entries put on list */
1215 return nreq;
1218 /* This follows nfs_commit_list pretty closely */
1219 static int
1220 filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1221 int how, struct nfs_commit_info *cinfo)
1223 struct nfs_commit_data *data, *tmp;
1224 LIST_HEAD(list);
1225 unsigned int nreq = 0;
1227 if (!list_empty(mds_pages)) {
1228 data = nfs_commitdata_alloc();
1229 if (data != NULL) {
1230 data->lseg = NULL;
1231 list_add(&data->pages, &list);
1232 nreq++;
1233 } else
1234 nfs_retry_commit(mds_pages, NULL, cinfo);
1237 nreq += alloc_ds_commits(cinfo, &list);
1239 if (nreq == 0) {
1240 cinfo->completion_ops->error_cleanup(NFS_I(inode));
1241 goto out;
1244 atomic_add(nreq, &cinfo->mds->rpcs_out);
1246 list_for_each_entry_safe(data, tmp, &list, pages) {
1247 list_del_init(&data->pages);
1248 if (!data->lseg) {
1249 nfs_init_commit(data, mds_pages, NULL, cinfo);
1250 nfs_initiate_commit(NFS_CLIENT(inode), data,
1251 data->mds_ops, how, 0);
1252 } else {
1253 struct pnfs_commit_bucket *buckets;
1255 buckets = cinfo->ds->buckets;
1256 nfs_init_commit(data, &buckets[data->ds_commit_index].committing, data->lseg, cinfo);
1257 filelayout_initiate_commit(data, how);
1260 out:
1261 cinfo->ds->ncommitting = 0;
1262 return PNFS_ATTEMPTED;
1265 static void
1266 filelayout_free_deveiceid_node(struct nfs4_deviceid_node *d)
1268 nfs4_fl_free_deviceid(container_of(d, struct nfs4_file_layout_dsaddr, id_node));
1271 static struct pnfs_layout_hdr *
1272 filelayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
1274 struct nfs4_filelayout *flo;
1276 flo = kzalloc(sizeof(*flo), gfp_flags);
1277 return &flo->generic_hdr;
1280 static void
1281 filelayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
1283 kfree(FILELAYOUT_FROM_HDR(lo));
1286 static struct pnfs_ds_commit_info *
1287 filelayout_get_ds_info(struct inode *inode)
1289 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1291 if (layout == NULL)
1292 return NULL;
1293 else
1294 return &FILELAYOUT_FROM_HDR(layout)->commit_info;
1297 static struct pnfs_layoutdriver_type filelayout_type = {
1298 .id = LAYOUT_NFSV4_1_FILES,
1299 .name = "LAYOUT_NFSV4_1_FILES",
1300 .owner = THIS_MODULE,
1301 .alloc_layout_hdr = filelayout_alloc_layout_hdr,
1302 .free_layout_hdr = filelayout_free_layout_hdr,
1303 .alloc_lseg = filelayout_alloc_lseg,
1304 .free_lseg = filelayout_free_lseg,
1305 .pg_read_ops = &filelayout_pg_read_ops,
1306 .pg_write_ops = &filelayout_pg_write_ops,
1307 .get_ds_info = &filelayout_get_ds_info,
1308 .mark_request_commit = filelayout_mark_request_commit,
1309 .clear_request_commit = filelayout_clear_request_commit,
1310 .scan_commit_lists = filelayout_scan_commit_lists,
1311 .recover_commit_reqs = filelayout_recover_commit_reqs,
1312 .commit_pagelist = filelayout_commit_pagelist,
1313 .read_pagelist = filelayout_read_pagelist,
1314 .write_pagelist = filelayout_write_pagelist,
1315 .free_deviceid_node = filelayout_free_deveiceid_node,
1318 static int __init nfs4filelayout_init(void)
1320 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
1321 __func__);
1322 return pnfs_register_layoutdriver(&filelayout_type);
1325 static void __exit nfs4filelayout_exit(void)
1327 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
1328 __func__);
1329 pnfs_unregister_layoutdriver(&filelayout_type);
1332 MODULE_ALIAS("nfs-layouttype4-1");
1334 module_init(nfs4filelayout_init);
1335 module_exit(nfs4filelayout_exit);