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