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_get_dense_offset(struct nfs4_filelayout_segment
*flseg
,
49 u32 stripe_width
= flseg
->stripe_unit
* flseg
->dsaddr
->stripe_count
;
52 offset
-= flseg
->pattern_offset
;
54 do_div(tmp
, stripe_width
);
56 return tmp
* flseg
->stripe_unit
+ do_div(offset
, flseg
->stripe_unit
);
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
64 filelayout_get_dserver_offset(struct pnfs_layout_segment
*lseg
, loff_t offset
)
66 struct nfs4_filelayout_segment
*flseg
= FILELAYOUT_LSEG(lseg
);
68 switch (flseg
->stripe_type
) {
73 return filelayout_get_dense_offset(flseg
, offset
);
79 /* For data server errors we don't recover from */
81 filelayout_set_lo_fail(struct pnfs_layout_segment
*lseg
)
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
);
87 dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__
);
88 set_bit(lo_fail_bit(IOMODE_READ
), &lseg
->pls_layout
->plh_flags
);
92 static int filelayout_async_handle_error(struct rpc_task
*task
,
93 struct nfs4_state
*state
,
94 struct nfs_client
*clp
,
97 if (task
->tk_status
>= 0)
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
);
118 rpc_delay(task
, FILELAYOUT_POLL_RETRY_MAX
);
121 dprintk("%s DS error. Retry through MDS %d\n", __func__
,
130 /* NFS_PROTO call done callback routines */
132 static int filelayout_read_done_cb(struct rpc_task
*task
,
133 struct nfs_read_data
*data
)
135 struct nfs_client
*clp
= data
->ds_clp
;
138 dprintk("%s DS read\n", __func__
);
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
);
145 filelayout_set_lo_fail(data
->lseg
);
146 nfs4_reset_read(task
, data
);
147 clp
= NFS_SERVER(data
->inode
)->nfs_client
;
149 nfs_restart_rpc(task
, clp
);
157 * Call ops for the async read/write cases
158 * In the case of dense layouts, the offset needs to be reset to its
161 static void filelayout_read_prepare(struct rpc_task
*task
, void *data
)
163 struct nfs_read_data
*rdata
= (struct nfs_read_data
*)data
;
165 rdata
->read_done_cb
= filelayout_read_done_cb
;
167 if (nfs41_setup_sequence(rdata
->ds_clp
->cl_session
,
168 &rdata
->args
.seq_args
, &rdata
->res
.seq_res
,
172 rpc_call_start(task
);
175 static void filelayout_read_call_done(struct rpc_task
*task
, void *data
)
177 struct nfs_read_data
*rdata
= (struct nfs_read_data
*)data
;
179 dprintk("--> %s task->tk_status %d\n", __func__
, task
->tk_status
);
181 /* Note this may cause RPC to be resent */
182 rdata
->mds_ops
->rpc_call_done(task
, data
);
185 static void filelayout_read_release(void *data
)
187 struct nfs_read_data
*rdata
= (struct nfs_read_data
*)data
;
189 rdata
->mds_ops
->rpc_release(data
);
192 struct rpc_call_ops filelayout_read_call_ops
= {
193 .rpc_call_prepare
= filelayout_read_prepare
,
194 .rpc_call_done
= filelayout_read_call_done
,
195 .rpc_release
= filelayout_read_release
,
198 static enum pnfs_try_status
199 filelayout_read_pagelist(struct nfs_read_data
*data
)
201 struct pnfs_layout_segment
*lseg
= data
->lseg
;
202 struct nfs4_pnfs_ds
*ds
;
203 loff_t offset
= data
->args
.offset
;
208 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
209 __func__
, data
->inode
->i_ino
,
210 data
->args
.pgbase
, (size_t)data
->args
.count
, offset
);
212 /* Retrieve the correct rpc_client for the byte range */
213 j
= nfs4_fl_calc_j_index(lseg
, offset
);
214 idx
= nfs4_fl_calc_ds_index(lseg
, j
);
215 ds
= nfs4_fl_prepare_ds(lseg
, idx
);
217 /* Either layout fh index faulty, or ds connect failed */
218 set_bit(lo_fail_bit(IOMODE_RW
), &lseg
->pls_layout
->plh_flags
);
219 set_bit(lo_fail_bit(IOMODE_READ
), &lseg
->pls_layout
->plh_flags
);
220 return PNFS_NOT_ATTEMPTED
;
222 dprintk("%s USE DS:ip %x %hu\n", __func__
,
223 ntohl(ds
->ds_ip_addr
), ntohs(ds
->ds_port
));
225 /* No multipath support. Use first DS */
226 data
->ds_clp
= ds
->ds_clp
;
227 fh
= nfs4_fl_select_ds_fh(lseg
, j
);
231 data
->args
.offset
= filelayout_get_dserver_offset(lseg
, offset
);
232 data
->mds_offset
= offset
;
234 /* Perform an asynchronous read to ds */
235 status
= nfs_initiate_read(data
, ds
->ds_clp
->cl_rpcclient
,
236 &filelayout_read_call_ops
);
238 return PNFS_ATTEMPTED
;
242 * filelayout_check_layout()
244 * Make sure layout segment parameters are sane WRT the device.
245 * At this point no generic layer initialization of the lseg has occurred,
246 * and nothing has been added to the layout_hdr cache.
250 filelayout_check_layout(struct pnfs_layout_hdr
*lo
,
251 struct nfs4_filelayout_segment
*fl
,
252 struct nfs4_layoutget_res
*lgr
,
253 struct nfs4_deviceid
*id
)
255 struct nfs4_file_layout_dsaddr
*dsaddr
;
256 int status
= -EINVAL
;
257 struct nfs_server
*nfss
= NFS_SERVER(lo
->plh_inode
);
259 dprintk("--> %s\n", __func__
);
261 if (fl
->pattern_offset
> lgr
->range
.offset
) {
262 dprintk("%s pattern_offset %lld to large\n",
263 __func__
, fl
->pattern_offset
);
267 if (fl
->stripe_unit
% PAGE_SIZE
) {
268 dprintk("%s Stripe unit (%u) not page aligned\n",
269 __func__
, fl
->stripe_unit
);
273 /* find and reference the deviceid */
274 dsaddr
= nfs4_fl_find_get_deviceid(id
);
275 if (dsaddr
== NULL
) {
276 dsaddr
= get_device_info(lo
->plh_inode
, id
);
282 if (fl
->first_stripe_index
< 0 ||
283 fl
->first_stripe_index
>= dsaddr
->stripe_count
) {
284 dprintk("%s Bad first_stripe_index %d\n",
285 __func__
, fl
->first_stripe_index
);
289 if ((fl
->stripe_type
== STRIPE_SPARSE
&&
290 fl
->num_fh
> 1 && fl
->num_fh
!= dsaddr
->ds_num
) ||
291 (fl
->stripe_type
== STRIPE_DENSE
&&
292 fl
->num_fh
!= dsaddr
->stripe_count
)) {
293 dprintk("%s num_fh %u not valid for given packing\n",
294 __func__
, fl
->num_fh
);
298 if (fl
->stripe_unit
% nfss
->rsize
|| fl
->stripe_unit
% nfss
->wsize
) {
299 dprintk("%s Stripe unit (%u) not aligned with rsize %u "
300 "wsize %u\n", __func__
, fl
->stripe_unit
, nfss
->rsize
,
306 dprintk("--> %s returns %d\n", __func__
, status
);
309 nfs4_fl_put_deviceid(dsaddr
);
313 static void filelayout_free_fh_array(struct nfs4_filelayout_segment
*fl
)
317 for (i
= 0; i
< fl
->num_fh
; i
++) {
318 if (!fl
->fh_array
[i
])
320 kfree(fl
->fh_array
[i
]);
327 _filelayout_free_lseg(struct nfs4_filelayout_segment
*fl
)
329 filelayout_free_fh_array(fl
);
334 filelayout_decode_layout(struct pnfs_layout_hdr
*flo
,
335 struct nfs4_filelayout_segment
*fl
,
336 struct nfs4_layoutget_res
*lgr
,
337 struct nfs4_deviceid
*id
)
339 uint32_t *p
= (uint32_t *)lgr
->layout
.buf
;
343 dprintk("%s: set_layout_map Begin\n", __func__
);
345 memcpy(id
, p
, sizeof(*id
));
346 p
+= XDR_QUADLEN(NFS4_DEVICEID4_SIZE
);
349 nfl_util
= be32_to_cpup(p
++);
350 if (nfl_util
& NFL4_UFLG_COMMIT_THRU_MDS
)
351 fl
->commit_through_mds
= 1;
352 if (nfl_util
& NFL4_UFLG_DENSE
)
353 fl
->stripe_type
= STRIPE_DENSE
;
355 fl
->stripe_type
= STRIPE_SPARSE
;
356 fl
->stripe_unit
= nfl_util
& ~NFL4_UFLG_MASK
;
358 fl
->first_stripe_index
= be32_to_cpup(p
++);
359 p
= xdr_decode_hyper(p
, &fl
->pattern_offset
);
360 fl
->num_fh
= be32_to_cpup(p
++);
362 dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
363 __func__
, nfl_util
, fl
->num_fh
, fl
->first_stripe_index
,
366 fl
->fh_array
= kzalloc(fl
->num_fh
* sizeof(struct nfs_fh
*),
371 for (i
= 0; i
< fl
->num_fh
; i
++) {
372 /* Do we want to use a mempool here? */
373 fl
->fh_array
[i
] = kmalloc(sizeof(struct nfs_fh
), GFP_KERNEL
);
374 if (!fl
->fh_array
[i
]) {
375 filelayout_free_fh_array(fl
);
378 fl
->fh_array
[i
]->size
= be32_to_cpup(p
++);
379 if (sizeof(struct nfs_fh
) < fl
->fh_array
[i
]->size
) {
380 printk(KERN_ERR
"Too big fh %d received %d\n",
381 i
, fl
->fh_array
[i
]->size
);
382 filelayout_free_fh_array(fl
);
385 memcpy(fl
->fh_array
[i
]->data
, p
, fl
->fh_array
[i
]->size
);
386 p
+= XDR_QUADLEN(fl
->fh_array
[i
]->size
);
387 dprintk("DEBUG: %s: fh len %d\n", __func__
,
388 fl
->fh_array
[i
]->size
);
394 static struct pnfs_layout_segment
*
395 filelayout_alloc_lseg(struct pnfs_layout_hdr
*layoutid
,
396 struct nfs4_layoutget_res
*lgr
)
398 struct nfs4_filelayout_segment
*fl
;
400 struct nfs4_deviceid id
;
402 dprintk("--> %s\n", __func__
);
403 fl
= kzalloc(sizeof(*fl
), GFP_KERNEL
);
407 rc
= filelayout_decode_layout(layoutid
, fl
, lgr
, &id
);
408 if (rc
!= 0 || filelayout_check_layout(layoutid
, fl
, lgr
, &id
)) {
409 _filelayout_free_lseg(fl
);
412 return &fl
->generic_hdr
;
416 filelayout_free_lseg(struct pnfs_layout_segment
*lseg
)
418 struct nfs4_filelayout_segment
*fl
= FILELAYOUT_LSEG(lseg
);
420 dprintk("--> %s\n", __func__
);
421 nfs4_fl_put_deviceid(fl
->dsaddr
);
422 _filelayout_free_lseg(fl
);
426 * filelayout_pg_test(). Called by nfs_can_coalesce_requests()
428 * return 1 : coalesce page
429 * return 0 : don't coalesce page
432 filelayout_pg_test(struct nfs_pageio_descriptor
*pgio
, struct nfs_page
*prev
,
433 struct nfs_page
*req
)
435 u64 p_stripe
, r_stripe
;
440 p_stripe
= (u64
)prev
->wb_index
<< PAGE_CACHE_SHIFT
;
441 r_stripe
= (u64
)req
->wb_index
<< PAGE_CACHE_SHIFT
;
442 stripe_unit
= FILELAYOUT_LSEG(pgio
->pg_lseg
)->stripe_unit
;
444 do_div(p_stripe
, stripe_unit
);
445 do_div(r_stripe
, stripe_unit
);
447 return (p_stripe
== r_stripe
);
450 static struct pnfs_layoutdriver_type filelayout_type
= {
451 .id
= LAYOUT_NFSV4_1_FILES
,
452 .name
= "LAYOUT_NFSV4_1_FILES",
453 .owner
= THIS_MODULE
,
454 .alloc_lseg
= filelayout_alloc_lseg
,
455 .free_lseg
= filelayout_free_lseg
,
456 .pg_test
= filelayout_pg_test
,
457 .read_pagelist
= filelayout_read_pagelist
,
460 static int __init
nfs4filelayout_init(void)
462 printk(KERN_INFO
"%s: NFSv4 File Layout Driver Registering...\n",
464 return pnfs_register_layoutdriver(&filelayout_type
);
467 static void __exit
nfs4filelayout_exit(void)
469 printk(KERN_INFO
"%s: NFSv4 File Layout Driver Unregistering...\n",
471 pnfs_unregister_layoutdriver(&filelayout_type
);
474 module_init(nfs4filelayout_init
);
475 module_exit(nfs4filelayout_exit
);