blob: 97e75a22af72660e08dd1e754f4ecc91fa863cad [file] [log] [blame]
Dean Hildebrand7ab672c2010-10-20 00:18:00 -04001/*
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.
5 *
6 * Copyright (c) 2002
7 * The Regents of the University of Michigan
8 * All Rights Reserved
9 *
10 * Dean Hildebrand <dhildebz@umich.edu>
11 *
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.
20 *
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.
30 */
31
32#include <linux/nfs_fs.h>
Andy Adamson16b374c2010-10-20 00:18:04 -040033
34#include "internal.h"
35#include "nfs4filelayout.h"
Dean Hildebrand7ab672c2010-10-20 00:18:00 -040036
37#define NFSDBG_FACILITY NFSDBG_PNFS_LD
38
39MODULE_LICENSE("GPL");
40MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
41MODULE_DESCRIPTION("The NFSv4 file layout driver");
42
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +000043#define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
44
Fred Isamancfe7f412011-03-01 01:34:18 +000045static loff_t
46filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
47 loff_t offset)
48{
49 u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
50 u64 tmp;
51
52 offset -= flseg->pattern_offset;
53 tmp = offset;
54 do_div(tmp, stripe_width);
55
56 return tmp * flseg->stripe_unit + do_div(offset, flseg->stripe_unit);
57}
58
59/* This function is used by the layout driver to calculate the
60 * offset of the file on the dserver based on whether the
61 * layout type is STRIPE_DENSE or STRIPE_SPARSE
62 */
63static loff_t
64filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
65{
66 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
67
68 switch (flseg->stripe_type) {
69 case STRIPE_SPARSE:
70 return offset;
71
72 case STRIPE_DENSE:
73 return filelayout_get_dense_offset(flseg, offset);
74 }
75
76 BUG();
77}
78
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +000079/* For data server errors we don't recover from */
80static void
81filelayout_set_lo_fail(struct pnfs_layout_segment *lseg)
82{
83 if (lseg->pls_range.iomode == IOMODE_RW) {
84 dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
85 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
86 } else {
87 dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
88 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
89 }
90}
91
92static int filelayout_async_handle_error(struct rpc_task *task,
93 struct nfs4_state *state,
94 struct nfs_client *clp,
95 int *reset)
96{
97 if (task->tk_status >= 0)
98 return 0;
99
100 *reset = 0;
101
102 switch (task->tk_status) {
103 case -NFS4ERR_BADSESSION:
104 case -NFS4ERR_BADSLOT:
105 case -NFS4ERR_BAD_HIGH_SLOT:
106 case -NFS4ERR_DEADSESSION:
107 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
108 case -NFS4ERR_SEQ_FALSE_RETRY:
109 case -NFS4ERR_SEQ_MISORDERED:
110 dprintk("%s ERROR %d, Reset session. Exchangeid "
111 "flags 0x%x\n", __func__, task->tk_status,
112 clp->cl_exchange_flags);
113 nfs4_schedule_session_recovery(clp->cl_session);
114 break;
115 case -NFS4ERR_DELAY:
116 case -NFS4ERR_GRACE:
117 case -EKEYEXPIRED:
118 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
119 break;
120 default:
121 dprintk("%s DS error. Retry through MDS %d\n", __func__,
122 task->tk_status);
123 *reset = 1;
124 break;
125 }
126 task->tk_status = 0;
127 return -EAGAIN;
128}
129
130/* NFS_PROTO call done callback routines */
131
132static int filelayout_read_done_cb(struct rpc_task *task,
133 struct nfs_read_data *data)
134{
135 struct nfs_client *clp = data->ds_clp;
136 int reset = 0;
137
138 dprintk("%s DS read\n", __func__);
139
140 if (filelayout_async_handle_error(task, data->args.context->state,
141 data->ds_clp, &reset) == -EAGAIN) {
142 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
143 __func__, data->ds_clp, data->ds_clp->cl_session);
144 if (reset) {
145 filelayout_set_lo_fail(data->lseg);
146 nfs4_reset_read(task, data);
147 clp = NFS_SERVER(data->inode)->nfs_client;
148 }
149 nfs_restart_rpc(task, clp);
150 return -EAGAIN;
151 }
152
153 return 0;
154}
155
Andy Adamson16b374c2010-10-20 00:18:04 -0400156/*
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000157 * Call ops for the async read/write cases
158 * In the case of dense layouts, the offset needs to be reset to its
159 * original value.
160 */
161static void filelayout_read_prepare(struct rpc_task *task, void *data)
162{
163 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
164
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000165 rdata->read_done_cb = filelayout_read_done_cb;
166
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000167 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
168 &rdata->args.seq_args, &rdata->res.seq_res,
169 0, task))
170 return;
171
172 rpc_call_start(task);
173}
174
175static void filelayout_read_call_done(struct rpc_task *task, void *data)
176{
177 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
178
179 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
180
181 /* Note this may cause RPC to be resent */
182 rdata->mds_ops->rpc_call_done(task, data);
183}
184
185static void filelayout_read_release(void *data)
186{
187 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
188
189 rdata->mds_ops->rpc_release(data);
190}
191
Fred Isamana69aef12011-03-03 15:13:47 +0000192static int filelayout_write_done_cb(struct rpc_task *task,
193 struct nfs_write_data *data)
194{
195 int reset = 0;
196
197 if (filelayout_async_handle_error(task, data->args.context->state,
198 data->ds_clp, &reset) == -EAGAIN) {
199 struct nfs_client *clp;
200
201 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
202 __func__, data->ds_clp, data->ds_clp->cl_session);
203 if (reset) {
204 filelayout_set_lo_fail(data->lseg);
205 nfs4_reset_write(task, data);
206 clp = NFS_SERVER(data->inode)->nfs_client;
207 } else
208 clp = data->ds_clp;
209 nfs_restart_rpc(task, clp);
210 return -EAGAIN;
211 }
212
213 return 0;
214}
215
Fred Isamane0c2b382011-03-23 13:27:53 +0000216/* Fake up some data that will cause nfs_commit_release to retry the writes. */
217static void prepare_to_resend_writes(struct nfs_write_data *data)
218{
219 struct nfs_page *first = nfs_list_entry(data->pages.next);
220
221 data->task.tk_status = 0;
222 memcpy(data->verf.verifier, first->wb_verf.verifier,
223 sizeof(first->wb_verf.verifier));
224 data->verf.verifier[0]++; /* ensure verifier mismatch */
225}
226
227static int filelayout_commit_done_cb(struct rpc_task *task,
228 struct nfs_write_data *data)
229{
230 int reset = 0;
231
232 if (filelayout_async_handle_error(task, data->args.context->state,
233 data->ds_clp, &reset) == -EAGAIN) {
234 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
235 __func__, data->ds_clp, data->ds_clp->cl_session);
236 if (reset) {
237 prepare_to_resend_writes(data);
238 filelayout_set_lo_fail(data->lseg);
239 } else
240 nfs_restart_rpc(task, data->ds_clp);
241 return -EAGAIN;
242 }
243
244 return 0;
245}
246
Fred Isamana69aef12011-03-03 15:13:47 +0000247static void filelayout_write_prepare(struct rpc_task *task, void *data)
248{
249 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
250
251 if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
252 &wdata->args.seq_args, &wdata->res.seq_res,
253 0, task))
254 return;
255
256 rpc_call_start(task);
257}
258
259static void filelayout_write_call_done(struct rpc_task *task, void *data)
260{
261 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
262
263 /* Note this may cause RPC to be resent */
264 wdata->mds_ops->rpc_call_done(task, data);
265}
266
267static void filelayout_write_release(void *data)
268{
269 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
270
271 wdata->mds_ops->rpc_release(data);
272}
273
Fred Isamane0c2b382011-03-23 13:27:53 +0000274static void filelayout_commit_release(void *data)
275{
276 struct nfs_write_data *wdata = (struct nfs_write_data *)data;
277
278 nfs_commit_release_pages(wdata);
279 if (atomic_dec_and_test(&NFS_I(wdata->inode)->commits_outstanding))
280 nfs_commit_clear_lock(NFS_I(wdata->inode));
281 nfs_commitdata_release(wdata);
282}
283
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000284struct rpc_call_ops filelayout_read_call_ops = {
285 .rpc_call_prepare = filelayout_read_prepare,
286 .rpc_call_done = filelayout_read_call_done,
287 .rpc_release = filelayout_read_release,
288};
289
Fred Isamana69aef12011-03-03 15:13:47 +0000290struct rpc_call_ops filelayout_write_call_ops = {
291 .rpc_call_prepare = filelayout_write_prepare,
292 .rpc_call_done = filelayout_write_call_done,
293 .rpc_release = filelayout_write_release,
294};
295
Fred Isamane0c2b382011-03-23 13:27:53 +0000296struct rpc_call_ops filelayout_commit_call_ops = {
297 .rpc_call_prepare = filelayout_write_prepare,
298 .rpc_call_done = filelayout_write_call_done,
299 .rpc_release = filelayout_commit_release,
300};
301
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000302static enum pnfs_try_status
303filelayout_read_pagelist(struct nfs_read_data *data)
304{
305 struct pnfs_layout_segment *lseg = data->lseg;
306 struct nfs4_pnfs_ds *ds;
307 loff_t offset = data->args.offset;
308 u32 j, idx;
309 struct nfs_fh *fh;
310 int status;
311
312 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
313 __func__, data->inode->i_ino,
314 data->args.pgbase, (size_t)data->args.count, offset);
315
316 /* Retrieve the correct rpc_client for the byte range */
317 j = nfs4_fl_calc_j_index(lseg, offset);
318 idx = nfs4_fl_calc_ds_index(lseg, j);
319 ds = nfs4_fl_prepare_ds(lseg, idx);
320 if (!ds) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000321 /* Either layout fh index faulty, or ds connect failed */
322 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
323 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000324 return PNFS_NOT_ATTEMPTED;
325 }
326 dprintk("%s USE DS:ip %x %hu\n", __func__,
327 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
328
329 /* No multipath support. Use first DS */
330 data->ds_clp = ds->ds_clp;
331 fh = nfs4_fl_select_ds_fh(lseg, j);
332 if (fh)
333 data->args.fh = fh;
334
335 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
336 data->mds_offset = offset;
337
338 /* Perform an asynchronous read to ds */
339 status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
340 &filelayout_read_call_ops);
341 BUG_ON(status != 0);
342 return PNFS_ATTEMPTED;
343}
344
Fred Isamana69aef12011-03-03 15:13:47 +0000345/* Perform async writes. */
Andy Adamson0382b742011-03-03 15:13:45 +0000346static enum pnfs_try_status
347filelayout_write_pagelist(struct nfs_write_data *data, int sync)
348{
Fred Isamana69aef12011-03-03 15:13:47 +0000349 struct pnfs_layout_segment *lseg = data->lseg;
350 struct nfs4_pnfs_ds *ds;
351 loff_t offset = data->args.offset;
352 u32 j, idx;
353 struct nfs_fh *fh;
354 int status;
355
356 /* Retrieve the correct rpc_client for the byte range */
357 j = nfs4_fl_calc_j_index(lseg, offset);
358 idx = nfs4_fl_calc_ds_index(lseg, j);
359 ds = nfs4_fl_prepare_ds(lseg, idx);
360 if (!ds) {
361 printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
362 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
363 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
364 return PNFS_NOT_ATTEMPTED;
365 }
366 dprintk("%s ino %lu sync %d req %Zu@%llu DS:%x:%hu\n", __func__,
367 data->inode->i_ino, sync, (size_t) data->args.count, offset,
368 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
369
370 /* We can't handle commit to ds yet */
371 if (!FILELAYOUT_LSEG(lseg)->commit_through_mds)
372 data->args.stable = NFS_FILE_SYNC;
373
374 data->write_done_cb = filelayout_write_done_cb;
375 data->ds_clp = ds->ds_clp;
376 fh = nfs4_fl_select_ds_fh(lseg, j);
377 if (fh)
378 data->args.fh = fh;
379 /*
380 * Get the file offset on the dserver. Set the write offset to
381 * this offset and save the original offset.
382 */
383 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
384 data->mds_offset = offset;
385
386 /* Perform an asynchronous write */
387 status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
388 &filelayout_write_call_ops, sync);
389 BUG_ON(status != 0);
390 return PNFS_ATTEMPTED;
Andy Adamson0382b742011-03-03 15:13:45 +0000391}
392
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000393/*
Andy Adamson16b374c2010-10-20 00:18:04 -0400394 * filelayout_check_layout()
395 *
396 * Make sure layout segment parameters are sane WRT the device.
397 * At this point no generic layer initialization of the lseg has occurred,
398 * and nothing has been added to the layout_hdr cache.
399 *
400 */
401static int
402filelayout_check_layout(struct pnfs_layout_hdr *lo,
403 struct nfs4_filelayout_segment *fl,
404 struct nfs4_layoutget_res *lgr,
405 struct nfs4_deviceid *id)
406{
407 struct nfs4_file_layout_dsaddr *dsaddr;
408 int status = -EINVAL;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000409 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
Andy Adamson16b374c2010-10-20 00:18:04 -0400410
411 dprintk("--> %s\n", __func__);
412
413 if (fl->pattern_offset > lgr->range.offset) {
414 dprintk("%s pattern_offset %lld to large\n",
415 __func__, fl->pattern_offset);
416 goto out;
417 }
418
Benny Halevy75247af2011-02-22 15:56:01 -0800419 if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
420 dprintk("%s Invalid stripe unit (%u)\n",
Andy Adamson16b374c2010-10-20 00:18:04 -0400421 __func__, fl->stripe_unit);
422 goto out;
423 }
424
425 /* find and reference the deviceid */
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000426 dsaddr = nfs4_fl_find_get_deviceid(id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400427 if (dsaddr == NULL) {
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000428 dsaddr = get_device_info(lo->plh_inode, id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400429 if (dsaddr == NULL)
430 goto out;
431 }
432 fl->dsaddr = dsaddr;
433
434 if (fl->first_stripe_index < 0 ||
435 fl->first_stripe_index >= dsaddr->stripe_count) {
436 dprintk("%s Bad first_stripe_index %d\n",
437 __func__, fl->first_stripe_index);
438 goto out_put;
439 }
440
441 if ((fl->stripe_type == STRIPE_SPARSE &&
442 fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
443 (fl->stripe_type == STRIPE_DENSE &&
444 fl->num_fh != dsaddr->stripe_count)) {
445 dprintk("%s num_fh %u not valid for given packing\n",
446 __func__, fl->num_fh);
447 goto out_put;
448 }
449
450 if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
451 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
452 "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
453 nfss->wsize);
454 }
455
456 status = 0;
457out:
458 dprintk("--> %s returns %d\n", __func__, status);
459 return status;
460out_put:
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000461 nfs4_fl_put_deviceid(dsaddr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400462 goto out;
463}
464
465static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
466{
467 int i;
468
469 for (i = 0; i < fl->num_fh; i++) {
470 if (!fl->fh_array[i])
471 break;
472 kfree(fl->fh_array[i]);
473 }
474 kfree(fl->fh_array);
475 fl->fh_array = NULL;
476}
477
478static void
479_filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
480{
481 filelayout_free_fh_array(fl);
482 kfree(fl);
483}
484
485static int
486filelayout_decode_layout(struct pnfs_layout_hdr *flo,
487 struct nfs4_filelayout_segment *fl,
488 struct nfs4_layoutget_res *lgr,
489 struct nfs4_deviceid *id)
490{
491 uint32_t *p = (uint32_t *)lgr->layout.buf;
492 uint32_t nfl_util;
493 int i;
494
495 dprintk("%s: set_layout_map Begin\n", __func__);
496
497 memcpy(id, p, sizeof(*id));
498 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
499 print_deviceid(id);
500
501 nfl_util = be32_to_cpup(p++);
502 if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
503 fl->commit_through_mds = 1;
504 if (nfl_util & NFL4_UFLG_DENSE)
505 fl->stripe_type = STRIPE_DENSE;
506 else
507 fl->stripe_type = STRIPE_SPARSE;
508 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
509
510 fl->first_stripe_index = be32_to_cpup(p++);
511 p = xdr_decode_hyper(p, &fl->pattern_offset);
512 fl->num_fh = be32_to_cpup(p++);
513
514 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
515 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
516 fl->pattern_offset);
517
518 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
519 GFP_KERNEL);
520 if (!fl->fh_array)
521 return -ENOMEM;
522
523 for (i = 0; i < fl->num_fh; i++) {
524 /* Do we want to use a mempool here? */
525 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
526 if (!fl->fh_array[i]) {
527 filelayout_free_fh_array(fl);
528 return -ENOMEM;
529 }
530 fl->fh_array[i]->size = be32_to_cpup(p++);
531 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
532 printk(KERN_ERR "Too big fh %d received %d\n",
533 i, fl->fh_array[i]->size);
534 filelayout_free_fh_array(fl);
535 return -EIO;
536 }
537 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
538 p += XDR_QUADLEN(fl->fh_array[i]->size);
539 dprintk("DEBUG: %s: fh len %d\n", __func__,
540 fl->fh_array[i]->size);
541 }
542
543 return 0;
544}
545
Fred Isamanc8795132011-03-23 13:27:49 +0000546static void
547filelayout_free_lseg(struct pnfs_layout_segment *lseg)
548{
549 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
550
551 dprintk("--> %s\n", __func__);
552 nfs4_fl_put_deviceid(fl->dsaddr);
Fred Isaman425eb732011-03-23 13:27:50 +0000553 kfree(fl->commit_buckets);
Fred Isamanc8795132011-03-23 13:27:49 +0000554 _filelayout_free_lseg(fl);
555}
556
Andy Adamson16b374c2010-10-20 00:18:04 -0400557static struct pnfs_layout_segment *
558filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
559 struct nfs4_layoutget_res *lgr)
560{
561 struct nfs4_filelayout_segment *fl;
562 int rc;
563 struct nfs4_deviceid id;
564
565 dprintk("--> %s\n", __func__);
566 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
567 if (!fl)
568 return NULL;
569
570 rc = filelayout_decode_layout(layoutid, fl, lgr, &id);
571 if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) {
572 _filelayout_free_lseg(fl);
573 return NULL;
574 }
Fred Isaman425eb732011-03-23 13:27:50 +0000575
576 /* This assumes there is only one IOMODE_RW lseg. What
577 * we really want to do is have a layout_hdr level
578 * dictionary of <multipath_list4, fh> keys, each
579 * associated with a struct list_head, populated by calls
580 * to filelayout_write_pagelist().
581 * */
582 if ((!fl->commit_through_mds) && (lgr->range.iomode == IOMODE_RW)) {
583 int i;
584 int size = (fl->stripe_type == STRIPE_SPARSE) ?
585 fl->dsaddr->ds_num : fl->dsaddr->stripe_count;
586
587 fl->commit_buckets = kcalloc(size, sizeof(struct list_head), GFP_KERNEL);
588 if (!fl->commit_buckets) {
589 filelayout_free_lseg(&fl->generic_hdr);
590 return NULL;
591 }
592 fl->number_of_buckets = size;
593 for (i = 0; i < size; i++)
594 INIT_LIST_HEAD(&fl->commit_buckets[i]);
595 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400596 return &fl->generic_hdr;
597}
598
Fred Isaman94ad1c82011-03-01 01:34:14 +0000599/*
600 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
601 *
602 * return 1 : coalesce page
603 * return 0 : don't coalesce page
604 */
605int
606filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
607 struct nfs_page *req)
608{
609 u64 p_stripe, r_stripe;
610 u32 stripe_unit;
611
612 if (!pgio->pg_lseg)
613 return 1;
614 p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
615 r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
616 stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
617
618 do_div(p_stripe, stripe_unit);
619 do_div(r_stripe, stripe_unit);
620
621 return (p_stripe == r_stripe);
622}
623
Fred Isamane0c2b382011-03-23 13:27:53 +0000624static bool filelayout_mark_pnfs_commit(struct pnfs_layout_segment *lseg)
625{
626 return !FILELAYOUT_LSEG(lseg)->commit_through_mds;
627}
628
629static u32 select_bucket_index(struct nfs4_filelayout_segment *fl, u32 j)
630{
631 if (fl->stripe_type == STRIPE_SPARSE)
632 return nfs4_fl_calc_ds_index(&fl->generic_hdr, j);
633 else
634 return j;
635}
636
637struct list_head *filelayout_choose_commit_list(struct nfs_page *req)
638{
639 struct pnfs_layout_segment *lseg = req->wb_commit_lseg;
640 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
641 u32 i, j;
642 struct list_head *list;
643
644 /* Note that we are calling nfs4_fl_calc_j_index on each page
645 * that ends up being committed to a data server. An attractive
646 * alternative is to add a field to nfs_write_data and nfs_page
647 * to store the value calculated in filelayout_write_pagelist
648 * and just use that here.
649 */
650 j = nfs4_fl_calc_j_index(lseg,
651 (loff_t)req->wb_index << PAGE_CACHE_SHIFT);
652 i = select_bucket_index(fl, j);
653 list = &fl->commit_buckets[i];
654 if (list_empty(list)) {
655 /* Non-empty buckets hold a reference on the lseg */
656 get_lseg(lseg);
657 }
658 return list;
659}
660
661static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
662{
663 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
664
665 if (flseg->stripe_type == STRIPE_SPARSE)
666 return i;
667 else
668 return nfs4_fl_calc_ds_index(lseg, i);
669}
670
671static struct nfs_fh *
672select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
673{
674 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
675
676 if (flseg->stripe_type == STRIPE_SPARSE) {
677 if (flseg->num_fh == 1)
678 i = 0;
679 else if (flseg->num_fh == 0)
680 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
681 return NULL;
682 }
683 return flseg->fh_array[i];
684}
685
686static int filelayout_initiate_commit(struct nfs_write_data *data, int how)
687{
688 struct pnfs_layout_segment *lseg = data->lseg;
689 struct nfs4_pnfs_ds *ds;
690 u32 idx;
691 struct nfs_fh *fh;
692
693 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
694 ds = nfs4_fl_prepare_ds(lseg, idx);
695 if (!ds) {
696 printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
697 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
698 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
699 prepare_to_resend_writes(data);
700 data->mds_ops->rpc_release(data);
701 return -EAGAIN;
702 }
703 dprintk("%s ino %lu, how %d\n", __func__, data->inode->i_ino, how);
704 data->write_done_cb = filelayout_commit_done_cb;
705 data->ds_clp = ds->ds_clp;
706 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
707 if (fh)
708 data->args.fh = fh;
709 return nfs_initiate_commit(data, ds->ds_clp->cl_rpcclient,
710 &filelayout_commit_call_ops, how);
711}
712
713/*
714 * This is only useful while we are using whole file layouts.
715 */
716static struct pnfs_layout_segment *find_only_write_lseg(struct inode *inode)
717{
718 struct pnfs_layout_segment *lseg, *rv = NULL;
719
720 spin_lock(&inode->i_lock);
721 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
722 if (lseg->pls_range.iomode == IOMODE_RW)
723 rv = get_lseg(lseg);
724 spin_unlock(&inode->i_lock);
725 return rv;
726}
727
728static int alloc_ds_commits(struct inode *inode, struct list_head *list)
729{
730 struct pnfs_layout_segment *lseg;
731 struct nfs4_filelayout_segment *fl;
732 struct nfs_write_data *data;
733 int i, j;
734
735 /* Won't need this when non-whole file layout segments are supported
736 * instead we will use a pnfs_layout_hdr structure */
737 lseg = find_only_write_lseg(inode);
738 if (!lseg)
739 return 0;
740 fl = FILELAYOUT_LSEG(lseg);
741 for (i = 0; i < fl->number_of_buckets; i++) {
742 if (list_empty(&fl->commit_buckets[i]))
743 continue;
744 data = nfs_commitdata_alloc();
745 if (!data)
746 goto out_bad;
747 data->ds_commit_index = i;
748 data->lseg = lseg;
749 list_add(&data->pages, list);
750 }
751 put_lseg(lseg);
752 return 0;
753
754out_bad:
755 for (j = i; j < fl->number_of_buckets; j++) {
756 if (list_empty(&fl->commit_buckets[i]))
757 continue;
758 nfs_retry_commit(&fl->commit_buckets[i], lseg);
759 put_lseg(lseg); /* associated with emptying bucket */
760 }
761 put_lseg(lseg);
762 /* Caller will clean up entries put on list */
763 return -ENOMEM;
764}
765
766/* This follows nfs_commit_list pretty closely */
767static int
768filelayout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
769 int how)
770{
771 struct nfs_write_data *data, *tmp;
772 LIST_HEAD(list);
773
774 if (!list_empty(mds_pages)) {
775 data = nfs_commitdata_alloc();
776 if (!data)
777 goto out_bad;
778 data->lseg = NULL;
779 list_add(&data->pages, &list);
780 }
781
782 if (alloc_ds_commits(inode, &list))
783 goto out_bad;
784
785 list_for_each_entry_safe(data, tmp, &list, pages) {
786 list_del_init(&data->pages);
787 atomic_inc(&NFS_I(inode)->commits_outstanding);
788 if (!data->lseg) {
789 nfs_init_commit(data, mds_pages, NULL);
790 nfs_initiate_commit(data, NFS_CLIENT(inode),
791 data->mds_ops, how);
792 } else {
793 nfs_init_commit(data, &FILELAYOUT_LSEG(data->lseg)->commit_buckets[data->ds_commit_index], data->lseg);
794 filelayout_initiate_commit(data, how);
795 }
796 }
797 return 0;
798 out_bad:
799 list_for_each_entry_safe(data, tmp, &list, pages) {
800 nfs_retry_commit(&data->pages, data->lseg);
801 list_del_init(&data->pages);
802 nfs_commit_free(data);
803 }
804 nfs_retry_commit(mds_pages, NULL);
805 nfs_commit_clear_lock(NFS_I(inode));
806 return -ENOMEM;
807}
808
Dean Hildebrand7ab672c2010-10-20 00:18:00 -0400809static struct pnfs_layoutdriver_type filelayout_type = {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000810 .id = LAYOUT_NFSV4_1_FILES,
811 .name = "LAYOUT_NFSV4_1_FILES",
812 .owner = THIS_MODULE,
813 .alloc_lseg = filelayout_alloc_lseg,
814 .free_lseg = filelayout_free_lseg,
Fred Isaman94ad1c82011-03-01 01:34:14 +0000815 .pg_test = filelayout_pg_test,
Fred Isamane0c2b382011-03-23 13:27:53 +0000816 .mark_pnfs_commit = filelayout_mark_pnfs_commit,
817 .choose_commit_list = filelayout_choose_commit_list,
818 .commit_pagelist = filelayout_commit_pagelist,
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000819 .read_pagelist = filelayout_read_pagelist,
Andy Adamson0382b742011-03-03 15:13:45 +0000820 .write_pagelist = filelayout_write_pagelist,
Dean Hildebrand7ab672c2010-10-20 00:18:00 -0400821};
822
823static int __init nfs4filelayout_init(void)
824{
825 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
826 __func__);
827 return pnfs_register_layoutdriver(&filelayout_type);
828}
829
830static void __exit nfs4filelayout_exit(void)
831{
832 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
833 __func__);
834 pnfs_unregister_layoutdriver(&filelayout_type);
835}
836
837module_init(nfs4filelayout_init);
838module_exit(nfs4filelayout_exit);