]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/nfs/nfs4filelayout.c
NFSv4.1: filelayout async error handler
[mv-sheeva.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 int
46 filelayout_set_layoutdriver(struct nfs_server *nfss)
47 {
48         int status = pnfs_alloc_init_deviceid_cache(nfss->nfs_client,
49                                                 nfs4_fl_free_deviceid_callback);
50         if (status) {
51                 printk(KERN_WARNING "%s: deviceid cache could not be "
52                         "initialized\n", __func__);
53                 return status;
54         }
55         dprintk("%s: deviceid cache has been initialized successfully\n",
56                 __func__);
57         return 0;
58 }
59
60 /* Clear out the layout by destroying its device list */
61 static int
62 filelayout_clear_layoutdriver(struct nfs_server *nfss)
63 {
64         dprintk("--> %s\n", __func__);
65
66         if (nfss->nfs_client->cl_devid_cache)
67                 pnfs_put_deviceid_cache(nfss->nfs_client);
68         return 0;
69 }
70
71 static loff_t
72 filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
73                             loff_t offset)
74 {
75         u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
76         u64 tmp;
77
78         offset -= flseg->pattern_offset;
79         tmp = offset;
80         do_div(tmp, stripe_width);
81
82         return tmp * flseg->stripe_unit + do_div(offset, flseg->stripe_unit);
83 }
84
85 /* This function is used by the layout driver to calculate the
86  * offset of the file on the dserver based on whether the
87  * layout type is STRIPE_DENSE or STRIPE_SPARSE
88  */
89 static loff_t
90 filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
91 {
92         struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
93
94         switch (flseg->stripe_type) {
95         case STRIPE_SPARSE:
96                 return offset;
97
98         case STRIPE_DENSE:
99                 return filelayout_get_dense_offset(flseg, offset);
100         }
101
102         BUG();
103 }
104
105 /* For data server errors we don't recover from */
106 static void
107 filelayout_set_lo_fail(struct pnfs_layout_segment *lseg)
108 {
109         if (lseg->pls_range.iomode == IOMODE_RW) {
110                 dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
111                 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
112         } else {
113                 dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
114                 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
115         }
116 }
117
118 static int filelayout_async_handle_error(struct rpc_task *task,
119                                          struct nfs4_state *state,
120                                          struct nfs_client *clp,
121                                          int *reset)
122 {
123         if (task->tk_status >= 0)
124                 return 0;
125
126         *reset = 0;
127
128         switch (task->tk_status) {
129         case -NFS4ERR_BADSESSION:
130         case -NFS4ERR_BADSLOT:
131         case -NFS4ERR_BAD_HIGH_SLOT:
132         case -NFS4ERR_DEADSESSION:
133         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
134         case -NFS4ERR_SEQ_FALSE_RETRY:
135         case -NFS4ERR_SEQ_MISORDERED:
136                 dprintk("%s ERROR %d, Reset session. Exchangeid "
137                         "flags 0x%x\n", __func__, task->tk_status,
138                         clp->cl_exchange_flags);
139                 nfs4_schedule_session_recovery(clp->cl_session);
140                 break;
141         case -NFS4ERR_DELAY:
142         case -NFS4ERR_GRACE:
143         case -EKEYEXPIRED:
144                 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
145                 break;
146         default:
147                 dprintk("%s DS error. Retry through MDS %d\n", __func__,
148                         task->tk_status);
149                 *reset = 1;
150                 break;
151         }
152         task->tk_status = 0;
153         return -EAGAIN;
154 }
155
156 /* NFS_PROTO call done callback routines */
157
158 static int filelayout_read_done_cb(struct rpc_task *task,
159                                 struct nfs_read_data *data)
160 {
161         struct nfs_client *clp = data->ds_clp;
162         int reset = 0;
163
164         dprintk("%s DS read\n", __func__);
165
166         if (filelayout_async_handle_error(task, data->args.context->state,
167                                           data->ds_clp, &reset) == -EAGAIN) {
168                 dprintk("%s calling restart ds_clp %p ds_clp->cl_session %p\n",
169                         __func__, data->ds_clp, data->ds_clp->cl_session);
170                 if (reset) {
171                         filelayout_set_lo_fail(data->lseg);
172                         nfs4_reset_read(task, data);
173                         clp = NFS_SERVER(data->inode)->nfs_client;
174                 }
175                 nfs_restart_rpc(task, clp);
176                 return -EAGAIN;
177         }
178
179         return 0;
180 }
181
182 /*
183  * Call ops for the async read/write cases
184  * In the case of dense layouts, the offset needs to be reset to its
185  * original value.
186  */
187 static void filelayout_read_prepare(struct rpc_task *task, void *data)
188 {
189         struct nfs_read_data *rdata = (struct nfs_read_data *)data;
190
191         rdata->read_done_cb = filelayout_read_done_cb;
192
193         if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
194                                 &rdata->args.seq_args, &rdata->res.seq_res,
195                                 0, task))
196                 return;
197
198         rpc_call_start(task);
199 }
200
201 static void filelayout_read_call_done(struct rpc_task *task, void *data)
202 {
203         struct nfs_read_data *rdata = (struct nfs_read_data *)data;
204
205         dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
206
207         /* Note this may cause RPC to be resent */
208         rdata->mds_ops->rpc_call_done(task, data);
209 }
210
211 static void filelayout_read_release(void *data)
212 {
213         struct nfs_read_data *rdata = (struct nfs_read_data *)data;
214
215         rdata->mds_ops->rpc_release(data);
216 }
217
218 struct rpc_call_ops filelayout_read_call_ops = {
219         .rpc_call_prepare = filelayout_read_prepare,
220         .rpc_call_done = filelayout_read_call_done,
221         .rpc_release = filelayout_read_release,
222 };
223
224 static enum pnfs_try_status
225 filelayout_read_pagelist(struct nfs_read_data *data)
226 {
227         struct pnfs_layout_segment *lseg = data->lseg;
228         struct nfs4_pnfs_ds *ds;
229         loff_t offset = data->args.offset;
230         u32 j, idx;
231         struct nfs_fh *fh;
232         int status;
233
234         dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
235                 __func__, data->inode->i_ino,
236                 data->args.pgbase, (size_t)data->args.count, offset);
237
238         /* Retrieve the correct rpc_client for the byte range */
239         j = nfs4_fl_calc_j_index(lseg, offset);
240         idx = nfs4_fl_calc_ds_index(lseg, j);
241         ds = nfs4_fl_prepare_ds(lseg, idx);
242         if (!ds) {
243                 printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
244                 return PNFS_NOT_ATTEMPTED;
245         }
246         dprintk("%s USE DS:ip %x %hu\n", __func__,
247                 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
248
249         /* No multipath support. Use first DS */
250         data->ds_clp = ds->ds_clp;
251         fh = nfs4_fl_select_ds_fh(lseg, j);
252         if (fh)
253                 data->args.fh = fh;
254
255         data->args.offset = filelayout_get_dserver_offset(lseg, offset);
256         data->mds_offset = offset;
257
258         /* Perform an asynchronous read to ds */
259         status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
260                                    &filelayout_read_call_ops);
261         BUG_ON(status != 0);
262         return PNFS_ATTEMPTED;
263 }
264
265 /*
266  * filelayout_check_layout()
267  *
268  * Make sure layout segment parameters are sane WRT the device.
269  * At this point no generic layer initialization of the lseg has occurred,
270  * and nothing has been added to the layout_hdr cache.
271  *
272  */
273 static int
274 filelayout_check_layout(struct pnfs_layout_hdr *lo,
275                         struct nfs4_filelayout_segment *fl,
276                         struct nfs4_layoutget_res *lgr,
277                         struct nfs4_deviceid *id)
278 {
279         struct nfs4_file_layout_dsaddr *dsaddr;
280         int status = -EINVAL;
281         struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
282
283         dprintk("--> %s\n", __func__);
284
285         if (fl->pattern_offset > lgr->range.offset) {
286                 dprintk("%s pattern_offset %lld to large\n",
287                                 __func__, fl->pattern_offset);
288                 goto out;
289         }
290
291         if (fl->stripe_unit % PAGE_SIZE) {
292                 dprintk("%s Stripe unit (%u) not page aligned\n",
293                         __func__, fl->stripe_unit);
294                 goto out;
295         }
296
297         /* find and reference the deviceid */
298         dsaddr = nfs4_fl_find_get_deviceid(nfss->nfs_client, id);
299         if (dsaddr == NULL) {
300                 dsaddr = get_device_info(lo->plh_inode, id);
301                 if (dsaddr == NULL)
302                         goto out;
303         }
304         fl->dsaddr = dsaddr;
305
306         if (fl->first_stripe_index < 0 ||
307             fl->first_stripe_index >= dsaddr->stripe_count) {
308                 dprintk("%s Bad first_stripe_index %d\n",
309                                 __func__, fl->first_stripe_index);
310                 goto out_put;
311         }
312
313         if ((fl->stripe_type == STRIPE_SPARSE &&
314             fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
315             (fl->stripe_type == STRIPE_DENSE &&
316             fl->num_fh != dsaddr->stripe_count)) {
317                 dprintk("%s num_fh %u not valid for given packing\n",
318                         __func__, fl->num_fh);
319                 goto out_put;
320         }
321
322         if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
323                 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
324                         "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
325                         nfss->wsize);
326         }
327
328         status = 0;
329 out:
330         dprintk("--> %s returns %d\n", __func__, status);
331         return status;
332 out_put:
333         pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache, &dsaddr->deviceid);
334         goto out;
335 }
336
337 static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
338 {
339         int i;
340
341         for (i = 0; i < fl->num_fh; i++) {
342                 if (!fl->fh_array[i])
343                         break;
344                 kfree(fl->fh_array[i]);
345         }
346         kfree(fl->fh_array);
347         fl->fh_array = NULL;
348 }
349
350 static void
351 _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
352 {
353         filelayout_free_fh_array(fl);
354         kfree(fl);
355 }
356
357 static int
358 filelayout_decode_layout(struct pnfs_layout_hdr *flo,
359                          struct nfs4_filelayout_segment *fl,
360                          struct nfs4_layoutget_res *lgr,
361                          struct nfs4_deviceid *id)
362 {
363         uint32_t *p = (uint32_t *)lgr->layout.buf;
364         uint32_t nfl_util;
365         int i;
366
367         dprintk("%s: set_layout_map Begin\n", __func__);
368
369         memcpy(id, p, sizeof(*id));
370         p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
371         print_deviceid(id);
372
373         nfl_util = be32_to_cpup(p++);
374         if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
375                 fl->commit_through_mds = 1;
376         if (nfl_util & NFL4_UFLG_DENSE)
377                 fl->stripe_type = STRIPE_DENSE;
378         else
379                 fl->stripe_type = STRIPE_SPARSE;
380         fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
381
382         fl->first_stripe_index = be32_to_cpup(p++);
383         p = xdr_decode_hyper(p, &fl->pattern_offset);
384         fl->num_fh = be32_to_cpup(p++);
385
386         dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
387                 __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
388                 fl->pattern_offset);
389
390         fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
391                                GFP_KERNEL);
392         if (!fl->fh_array)
393                 return -ENOMEM;
394
395         for (i = 0; i < fl->num_fh; i++) {
396                 /* Do we want to use a mempool here? */
397                 fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
398                 if (!fl->fh_array[i]) {
399                         filelayout_free_fh_array(fl);
400                         return -ENOMEM;
401                 }
402                 fl->fh_array[i]->size = be32_to_cpup(p++);
403                 if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
404                         printk(KERN_ERR "Too big fh %d received %d\n",
405                                i, fl->fh_array[i]->size);
406                         filelayout_free_fh_array(fl);
407                         return -EIO;
408                 }
409                 memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
410                 p += XDR_QUADLEN(fl->fh_array[i]->size);
411                 dprintk("DEBUG: %s: fh len %d\n", __func__,
412                         fl->fh_array[i]->size);
413         }
414
415         return 0;
416 }
417
418 static struct pnfs_layout_segment *
419 filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
420                       struct nfs4_layoutget_res *lgr)
421 {
422         struct nfs4_filelayout_segment *fl;
423         int rc;
424         struct nfs4_deviceid id;
425
426         dprintk("--> %s\n", __func__);
427         fl = kzalloc(sizeof(*fl), GFP_KERNEL);
428         if (!fl)
429                 return NULL;
430
431         rc = filelayout_decode_layout(layoutid, fl, lgr, &id);
432         if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) {
433                 _filelayout_free_lseg(fl);
434                 return NULL;
435         }
436         return &fl->generic_hdr;
437 }
438
439 static void
440 filelayout_free_lseg(struct pnfs_layout_segment *lseg)
441 {
442         struct nfs_server *nfss = NFS_SERVER(lseg->pls_layout->plh_inode);
443         struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
444
445         dprintk("--> %s\n", __func__);
446         pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache,
447                           &fl->dsaddr->deviceid);
448         _filelayout_free_lseg(fl);
449 }
450
451 /*
452  * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
453  *
454  * return 1 :  coalesce page
455  * return 0 :  don't coalesce page
456  */
457 int
458 filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
459                    struct nfs_page *req)
460 {
461         u64 p_stripe, r_stripe;
462         u32 stripe_unit;
463
464         if (!pgio->pg_lseg)
465                 return 1;
466         p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT;
467         r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT;
468         stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit;
469
470         do_div(p_stripe, stripe_unit);
471         do_div(r_stripe, stripe_unit);
472
473         return (p_stripe == r_stripe);
474 }
475
476 static struct pnfs_layoutdriver_type filelayout_type = {
477         .id = LAYOUT_NFSV4_1_FILES,
478         .name = "LAYOUT_NFSV4_1_FILES",
479         .owner = THIS_MODULE,
480         .set_layoutdriver = filelayout_set_layoutdriver,
481         .clear_layoutdriver = filelayout_clear_layoutdriver,
482         .alloc_lseg              = filelayout_alloc_lseg,
483         .free_lseg               = filelayout_free_lseg,
484         .pg_test                = filelayout_pg_test,
485         .read_pagelist          = filelayout_read_pagelist,
486 };
487
488 static int __init nfs4filelayout_init(void)
489 {
490         printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
491                __func__);
492         return pnfs_register_layoutdriver(&filelayout_type);
493 }
494
495 static void __exit nfs4filelayout_exit(void)
496 {
497         printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
498                __func__);
499         pnfs_unregister_layoutdriver(&filelayout_type);
500 }
501
502 module_init(nfs4filelayout_init);
503 module_exit(nfs4filelayout_exit);