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.
7 * The Regents of the University of Michigan
10 * Dean Hildebrand <dhildebz@umich.edu>
12 * Permission is granted to use, copy, create derivative works, and
13 * redistribute this software and such derivative works for any purpose,
14 * so long as the name of the University of Michigan is not used in
15 * any advertising or publicity pertaining to the use or distribution
16 * of this software without specific, written prior authorization. If
17 * the above copyright notice or any other identification of the
18 * University of Michigan is included in any copy of any portion of
19 * this software, then the disclaimer below must also be included.
21 * This software is provided as is, without representation or warranty
22 * of any kind either express or implied, including without limitation
23 * the implied warranties of merchantability, fitness for a particular
24 * purpose, or noninfringement. The Regents of the University of
25 * Michigan shall not be liable for any damages, including special,
26 * indirect, incidental, or consequential damages, with respect to any
27 * claim arising out of or in connection with the use of the software,
28 * even if it has been or is hereafter advised of the possibility of
32 #include <linux/nfs_fs.h>
35 #include "nfs4filelayout.h"
37 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
41 MODULE_DESCRIPTION("The NFSv4 file layout driver");
43 #define FILELAYOUT_POLL_RETRY_MAX (15*HZ)
46 filelayout_set_layoutdriver(struct nfs_server *nfss)
48 int status = pnfs_alloc_init_deviceid_cache(nfss->nfs_client,
49 nfs4_fl_free_deviceid_callback);
51 printk(KERN_WARNING "%s: deviceid cache could not be "
52 "initialized\n", __func__);
55 dprintk("%s: deviceid cache has been initialized successfully\n",
60 /* Clear out the layout by destroying its device list */
62 filelayout_clear_layoutdriver(struct nfs_server *nfss)
64 dprintk("--> %s\n", __func__);
66 if (nfss->nfs_client->cl_devid_cache)
67 pnfs_put_deviceid_cache(nfss->nfs_client);
72 filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg,
75 u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count;
78 offset -= flseg->pattern_offset;
80 do_div(tmp, stripe_width);
82 return tmp * flseg->stripe_unit + do_div(offset, flseg->stripe_unit);
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
90 filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset)
92 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
94 switch (flseg->stripe_type) {
99 return filelayout_get_dense_offset(flseg, offset);
105 /* For data server errors we don't recover from */
107 filelayout_set_lo_fail(struct pnfs_layout_segment *lseg)
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);
113 dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
114 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
118 static int filelayout_async_handle_error(struct rpc_task *task,
119 struct nfs4_state *state,
120 struct nfs_client *clp,
123 if (task->tk_status >= 0)
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);
144 rpc_delay(task, FILELAYOUT_POLL_RETRY_MAX);
147 dprintk("%s DS error. Retry through MDS %d\n", __func__,
156 /* NFS_PROTO call done callback routines */
158 static int filelayout_read_done_cb(struct rpc_task *task,
159 struct nfs_read_data *data)
161 struct nfs_client *clp = data->ds_clp;
164 dprintk("%s DS read\n", __func__);
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);
171 filelayout_set_lo_fail(data->lseg);
172 nfs4_reset_read(task, data);
173 clp = NFS_SERVER(data->inode)->nfs_client;
175 nfs_restart_rpc(task, clp);
183 * Call ops for the async read/write cases
184 * In the case of dense layouts, the offset needs to be reset to its
187 static void filelayout_read_prepare(struct rpc_task *task, void *data)
189 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
191 rdata->read_done_cb = filelayout_read_done_cb;
193 if (nfs41_setup_sequence(rdata->ds_clp->cl_session,
194 &rdata->args.seq_args, &rdata->res.seq_res,
198 rpc_call_start(task);
201 static void filelayout_read_call_done(struct rpc_task *task, void *data)
203 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
205 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
207 /* Note this may cause RPC to be resent */
208 rdata->mds_ops->rpc_call_done(task, data);
211 static void filelayout_read_release(void *data)
213 struct nfs_read_data *rdata = (struct nfs_read_data *)data;
215 rdata->mds_ops->rpc_release(data);
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,
224 static enum pnfs_try_status
225 filelayout_read_pagelist(struct nfs_read_data *data)
227 struct pnfs_layout_segment *lseg = data->lseg;
228 struct nfs4_pnfs_ds *ds;
229 loff_t offset = data->args.offset;
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);
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);
243 printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__);
244 return PNFS_NOT_ATTEMPTED;
246 dprintk("%s USE DS:ip %x %hu\n", __func__,
247 ntohl(ds->ds_ip_addr), ntohs(ds->ds_port));
249 /* No multipath support. Use first DS */
250 data->ds_clp = ds->ds_clp;
251 fh = nfs4_fl_select_ds_fh(lseg, j);
255 data->args.offset = filelayout_get_dserver_offset(lseg, offset);
256 data->mds_offset = offset;
258 /* Perform an asynchronous read to ds */
259 status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient,
260 &filelayout_read_call_ops);
262 return PNFS_ATTEMPTED;
266 * filelayout_check_layout()
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.
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)
279 struct nfs4_file_layout_dsaddr *dsaddr;
280 int status = -EINVAL;
281 struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
283 dprintk("--> %s\n", __func__);
285 if (fl->pattern_offset > lgr->range.offset) {
286 dprintk("%s pattern_offset %lld to large\n",
287 __func__, fl->pattern_offset);
291 if (fl->stripe_unit % PAGE_SIZE) {
292 dprintk("%s Stripe unit (%u) not page aligned\n",
293 __func__, fl->stripe_unit);
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);
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);
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);
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,
330 dprintk("--> %s returns %d\n", __func__, status);
333 pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache, &dsaddr->deviceid);
337 static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
341 for (i = 0; i < fl->num_fh; i++) {
342 if (!fl->fh_array[i])
344 kfree(fl->fh_array[i]);
351 _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
353 filelayout_free_fh_array(fl);
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)
363 uint32_t *p = (uint32_t *)lgr->layout.buf;
367 dprintk("%s: set_layout_map Begin\n", __func__);
369 memcpy(id, p, sizeof(*id));
370 p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
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;
379 fl->stripe_type = STRIPE_SPARSE;
380 fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
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++);
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,
390 fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
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);
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);
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);
418 static struct pnfs_layout_segment *
419 filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
420 struct nfs4_layoutget_res *lgr)
422 struct nfs4_filelayout_segment *fl;
424 struct nfs4_deviceid id;
426 dprintk("--> %s\n", __func__);
427 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
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);
436 return &fl->generic_hdr;
440 filelayout_free_lseg(struct pnfs_layout_segment *lseg)
442 struct nfs_server *nfss = NFS_SERVER(lseg->pls_layout->plh_inode);
443 struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
445 dprintk("--> %s\n", __func__);
446 pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache,
447 &fl->dsaddr->deviceid);
448 _filelayout_free_lseg(fl);
452 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
454 * return 1 : coalesce page
455 * return 0 : don't coalesce page
458 filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
459 struct nfs_page *req)
461 u64 p_stripe, r_stripe;
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;
470 do_div(p_stripe, stripe_unit);
471 do_div(r_stripe, stripe_unit);
473 return (p_stripe == r_stripe);
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,
488 static int __init nfs4filelayout_init(void)
490 printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
492 return pnfs_register_layoutdriver(&filelayout_type);
495 static void __exit nfs4filelayout_exit(void)
497 printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
499 pnfs_unregister_layoutdriver(&filelayout_type);
502 module_init(nfs4filelayout_init);
503 module_exit(nfs4filelayout_exit);