Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.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
34 #include "internal.h"
35 #include "nfs4filelayout.h"
36
37 #define NFSDBG_FACILITY         NFSDBG_PNFS_LD
38
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
41 MODULE_DESCRIPTION("The NFSv4 file layout driver");
42
43 #define FILELAYOUT_POLL_RETRY_MAX     (15*HZ)
44
45 static loff_t
46 filelayout_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  */
63 static loff_t
64 filelayout_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
79 /* For data server errors we don't recover from */
80 static void
81 filelayout_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
92 static 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
132 static 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
156 /*
157  * 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  */
161 static void filelayout_read_prepare(struct rpc_task *task, void *data)
162 {
163         struct nfs_read_data *rdata = (struct nfs_read_data *)data;
164
165         rdata->read_done_cb = filelayout_read_done_cb;
166
167         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
175 static 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
185 static 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
192 static 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
216 static void filelayout_write_prepare(struct rpc_task *task, void *data)
217 {
218         struct nfs_write_data *wdata = (struct nfs_write_data *)data;
219
220         if (nfs41_setup_sequence(wdata->ds_clp->cl_session,
221                                 &wdata->args.seq_args, &wdata->res.seq_res,
222                                 0, task))
223                 return;
224
225         rpc_call_start(task);
226 }
227
228 static void filelayout_write_call_done(struct rpc_task *task, void *data)
229 {
230         struct nfs_write_data *wdata = (struct nfs_write_data *)data;
231
232         /* Note this may cause RPC to be resent */
233         wdata->mds_ops->rpc_call_done(task, data);
234 }
235
236 static void filelayout_write_release(void *data)
237 {
238         struct nfs_write_data *wdata = (struct nfs_write_data *)data;
239
240         wdata->mds_ops->rpc_release(data);
241 }
242
243 struct rpc_call_ops filelayout_read_call_ops = {
244         .rpc_call_prepare = filelayout_read_prepare,
245         .rpc_call_done = filelayout_read_call_done,
246         .rpc_release = filelayout_read_release,
247 };
248
249 struct rpc_call_ops filelayout_write_call_ops = {
250         .rpc_call_prepare = filelayout_write_prepare,
251         .rpc_call_done = filelayout_write_call_done,
252         .rpc_release = filelayout_write_release,
253 };
254
255 static enum pnfs_try_status
256 filelayout_read_pagelist(struct nfs_read_data *data)
257 {
258         struct pnfs_layout_segment *lseg = data->lseg;
259         struct nfs4_pnfs_ds *ds;
260         loff_t offset = data->args.offset;
261         u32 j, idx;
262         struct nfs_fh *fh;
263         int status;
264
265         dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
266                 __func__, data->inode->i_ino,
267                 data->args.pgbase, (size_t)data->args.count, offset);
268
269         /* Retrieve the correct rpc_client for the byte range */
270         j = nfs4_fl_calc_j_index(lseg, offset);
271         idx = nfs4_fl_calc_ds_index(lseg, j);
272         ds = nfs4_fl_prepare_ds(lseg, idx);
273         if (!ds) {
274                 /* Either layout fh index faulty, or ds connect failed */
275                 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
276                 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
277                 return PNFS_NOT_ATTEMPTED;
278         }
279         dprintk("%s USE DS:ip %x %hu\n", __func__,
280                 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
281
282         /* No multipath support. Use first DS */
283         data->ds_clp = ds->ds_clp;
284         fh = nfs4_fl_select_ds_fh(lseg, j);
285         if (fh)
286                 data->args.fh = fh;
287
288         data->args.offset = filelayout_get_dserver_offset(lseg, offset);
289         data->mds_offset = offset;
290
291         /* Perform an asynchronous read to ds */
292         status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
293                                    &filelayout_read_call_ops);
294         BUG_ON(status != 0);
295         return PNFS_ATTEMPTED;
296 }
297
298 /* Perform async writes. */
299 static enum pnfs_try_status
300 filelayout_write_pagelist(struct nfs_write_data *data, int sync)
301 {
302         struct pnfs_layout_segment *lseg = data->lseg;
303         struct nfs4_pnfs_ds *ds;
304         loff_t offset = data->args.offset;
305         u32 j, idx;
306         struct nfs_fh *fh;
307         int status;
308
309         /* Retrieve the correct rpc_client for the byte range */
310         j = nfs4_fl_calc_j_index(lseg, offset);
311         idx = nfs4_fl_calc_ds_index(lseg, j);
312         ds = nfs4_fl_prepare_ds(lseg, idx);
313         if (!ds) {
314                 printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
315                 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
316                 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
317                 return PNFS_NOT_ATTEMPTED;
318         }
319         dprintk("%s ino %lu sync %d req %Zu@%llu DS:%x:%hu\n", __func__,
320                 data->inode->i_ino, sync, (size_t) data->args.count, offset,
321                 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
322
323         /* We can't handle commit to ds yet */
324         if (!FILELAYOUT_LSEG(lseg)->commit_through_mds)
325                 data->args.stable = NFS_FILE_SYNC;
326
327         data->write_done_cb = filelayout_write_done_cb;
328         data->ds_clp = ds->ds_clp;
329         fh = nfs4_fl_select_ds_fh(lseg, j);
330         if (fh)
331                 data->args.fh = fh;
332         /*
333          * Get the file offset on the dserver. Set the write offset to
334          * this offset and save the original offset.
335          */
336         data->args.offset = filelayout_get_dserver_offset(lseg, offset);
337         data->mds_offset = offset;
338
339         /* Perform an asynchronous write */
340         status = nfs_initiate_write(data, ds->ds_clp->cl_rpcclient,
341                                     &filelayout_write_call_ops, sync);
342         BUG_ON(status != 0);
343         return PNFS_ATTEMPTED;
344 }
345
346 /*
347  * filelayout_check_layout()
348  *
349  * Make sure layout segment parameters are sane WRT the device.
350  * At this point no generic layer initialization of the lseg has occurred,
351  * and nothing has been added to the layout_hdr cache.
352  *
353  */
354 static int
355 filelayout_check_layout(struct pnfs_layout_hdr *lo,
356                         struct nfs4_filelayout_segment *fl,
357                         struct nfs4_layoutget_res *lgr,
358                         struct nfs4_deviceid *id)
359 {
360         struct nfs4_file_layout_dsaddr *dsaddr;
361         int status = -EINVAL;
362         struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
363
364         dprintk("--> %s\n", __func__);
365
366         if (fl->pattern_offset > lgr->range.offset) {
367                 dprintk("%s pattern_offset %lld to large\n",
368                                 __func__, fl->pattern_offset);
369                 goto out;
370         }
371
372         if (!fl->stripe_unit || fl->stripe_unit % PAGE_SIZE) {
373                 dprintk("%s Invalid stripe unit (%u)\n",
374                         __func__, fl->stripe_unit);
375                 goto out;
376         }
377
378         /* find and reference the deviceid */
379         dsaddr = nfs4_fl_find_get_deviceid(id);
380         if (dsaddr == NULL) {
381                 dsaddr = get_device_info(lo->plh_inode, id);
382                 if (dsaddr == NULL)
383                         goto out;
384         }
385         fl->dsaddr = dsaddr;
386
387         if (fl->first_stripe_index < 0 ||
388             fl->first_stripe_index >= dsaddr->stripe_count) {
389                 dprintk("%s Bad first_stripe_index %d\n",
390                                 __func__, fl->first_stripe_index);
391                 goto out_put;
392         }
393
394         if ((fl->stripe_type == STRIPE_SPARSE &&
395             fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
396             (fl->stripe_type == STRIPE_DENSE &&
397             fl->num_fh != dsaddr->stripe_count)) {
398                 dprintk("%s num_fh %u not valid for given packing\n",
399                         __func__, fl->num_fh);
400                 goto out_put;
401         }
402
403         if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
404                 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
405                         "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
406                         nfss->wsize);
407         }
408
409         status = 0;
410 out:
411         dprintk("--> %s returns %d\n", __func__, status);
412         return status;
413 out_put:
414         nfs4_fl_put_deviceid(dsaddr);
415         goto out;
416 }
417
418 static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
419 {
420         int i;
421
422         for (i = 0; i < fl->num_fh; i++) {
423                 if (!fl->fh_array[i])
424                         break;
425                 kfree(fl->fh_array[i]);
426         }
427         kfree(fl->fh_array);
428         fl->fh_array = NULL;
429 }
430
431 static void
432 _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
433 {
434         filelayout_free_fh_array(fl);
435         kfree(fl);
436 }
437
438 static int
439 filelayout_decode_layout(struct pnfs_layout_hdr *flo,
440                          struct nfs4_filelayout_segment *fl,
441                          struct nfs4_layoutget_res *lgr,
442                          struct nfs4_deviceid *id)
443 {
444         uint32_t *p = (uint32_t *)lgr->layout.buf;
445         uint32_t nfl_util;
446         int i;
447
448         dprintk("%s: set_layout_map Begin\n", __func__);
449
450         memcpy(id, p, sizeof(*id));
451         p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
452         print_deviceid(id);
453
454         nfl_util = be32_to_cpup(p++);
455         if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
456                 fl->commit_through_mds = 1;
457         if (nfl_util & NFL4_UFLG_DENSE)
458                 fl->stripe_type = STRIPE_DENSE;
459         else
460                 fl->stripe_type = STRIPE_SPARSE;
461         fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
462
463         fl->first_stripe_index = be32_to_cpup(p++);
464         p = xdr_decode_hyper(p, &fl->pattern_offset);
465         fl->num_fh = be32_to_cpup(p++);
466
467         dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
468                 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
469                 fl->pattern_offset);
470
471         fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
472                                GFP_KERNEL);
473         if (!fl->fh_array)
474                 return -ENOMEM;
475
476         for (i = 0; i < fl->num_fh; i++) {
477                 /* Do we want to use a mempool here? */
478                 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
479                 if (!fl->fh_array[i]) {
480                         filelayout_free_fh_array(fl);
481                         return -ENOMEM;
482                 }
483                 fl->fh_array[i]->size = be32_to_cpup(p++);
484                 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
485                         printk(KERN_ERR "Too big fh %d received %d\n",
486                                i, fl->fh_array[i]->size);
487                         filelayout_free_fh_array(fl);
488                         return -EIO;
489                 }
490                 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
491                 p += XDR_QUADLEN(fl->fh_array[i]->size);
492                 dprintk("DEBUG: %s: fh len %d\n", __func__,
493                         fl->fh_array[i]->size);
494         }
495
496         return 0;
497 }
498
499 static struct pnfs_layout_segment *
500 filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
501                       struct nfs4_layoutget_res *lgr)
502 {
503         struct nfs4_filelayout_segment *fl;
504         int rc;
505         struct nfs4_deviceid id;
506
507         dprintk("--> %s\n", __func__);
508         fl = kzalloc(sizeof(*fl), GFP_KERNEL);
509         if (!fl)
510                 return NULL;
511
512         rc = filelayout_decode_layout(layoutid, fl, lgr, &id);
513         if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) {
514                 _filelayout_free_lseg(fl);
515                 return NULL;
516         }
517         return &fl->generic_hdr;
518 }
519
520 static void
521 filelayout_free_lseg(struct pnfs_layout_segment *lseg)
522 {
523         struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
524
525         dprintk("--> %s\n", __func__);
526         nfs4_fl_put_deviceid(fl->dsaddr);
527         _filelayout_free_lseg(fl);
528 }
529
530 /*
531  * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
532  *
533  * return 1 :  coalesce page
534  * return 0 :  don't coalesce page
535  */
536 int
537 filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
538                    struct nfs_page *req)
539 {
540         u64 p_stripe, r_stripe;
541         u32 stripe_unit;
542
543         if (!pgio->pg_lseg)
544                 return 1;
545         p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
546         r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
547         stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
548
549         do_div(p_stripe, stripe_unit);
550         do_div(r_stripe, stripe_unit);
551
552         return (p_stripe == r_stripe);
553 }
554
555 static struct pnfs_layoutdriver_type filelayout_type = {
556         .id                     = LAYOUT_NFSV4_1_FILES,
557         .name                   = "LAYOUT_NFSV4_1_FILES",
558         .owner                  = THIS_MODULE,
559         .alloc_lseg             = filelayout_alloc_lseg,
560         .free_lseg              = filelayout_free_lseg,
561         .pg_test                = filelayout_pg_test,
562         .read_pagelist          = filelayout_read_pagelist,
563         .write_pagelist         = filelayout_write_pagelist,
564 };
565
566 static int __init nfs4filelayout_init(void)
567 {
568         printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
569                __func__);
570         return pnfs_register_layoutdriver(&filelayout_type);
571 }
572
573 static void __exit nfs4filelayout_exit(void)
574 {
575         printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
576                __func__);
577         pnfs_unregister_layoutdriver(&filelayout_type);
578 }
579
580 module_init(nfs4filelayout_init);
581 module_exit(nfs4filelayout_exit);