Dean Hildebrand | 7ab672c | 2010-10-20 00:18:00 -0400 | [diff] [blame] | 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> |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 33 | |
| 34 | #include "internal.h" |
| 35 | #include "nfs4filelayout.h" |
Dean Hildebrand | 7ab672c | 2010-10-20 00:18:00 -0400 | [diff] [blame] | 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 | |
Trond Myklebust | 1c78709 | 2010-10-21 16:56:48 -0400 | [diff] [blame] | 43 | static int |
| 44 | filelayout_set_layoutdriver(struct nfs_server *nfss) |
Dean Hildebrand | 7ab672c | 2010-10-20 00:18:00 -0400 | [diff] [blame] | 45 | { |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 46 | int status = pnfs_alloc_init_deviceid_cache(nfss->nfs_client, |
| 47 | nfs4_fl_free_deviceid_callback); |
| 48 | if (status) { |
| 49 | printk(KERN_WARNING "%s: deviceid cache could not be " |
| 50 | "initialized\n", __func__); |
| 51 | return status; |
| 52 | } |
| 53 | dprintk("%s: deviceid cache has been initialized successfully\n", |
| 54 | __func__); |
Dean Hildebrand | 7ab672c | 2010-10-20 00:18:00 -0400 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
Trond Myklebust | 1c78709 | 2010-10-21 16:56:48 -0400 | [diff] [blame] | 58 | /* Clear out the layout by destroying its device list */ |
| 59 | static int |
| 60 | filelayout_clear_layoutdriver(struct nfs_server *nfss) |
Dean Hildebrand | 7ab672c | 2010-10-20 00:18:00 -0400 | [diff] [blame] | 61 | { |
| 62 | dprintk("--> %s\n", __func__); |
| 63 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 64 | if (nfss->nfs_client->cl_devid_cache) |
| 65 | pnfs_put_deviceid_cache(nfss->nfs_client); |
Dean Hildebrand | 7ab672c | 2010-10-20 00:18:00 -0400 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Fred Isaman | cfe7f41 | 2011-03-01 01:34:18 +0000 | [diff] [blame] | 69 | static loff_t |
| 70 | filelayout_get_dense_offset(struct nfs4_filelayout_segment *flseg, |
| 71 | loff_t offset) |
| 72 | { |
| 73 | u32 stripe_width = flseg->stripe_unit * flseg->dsaddr->stripe_count; |
| 74 | u64 tmp; |
| 75 | |
| 76 | offset -= flseg->pattern_offset; |
| 77 | tmp = offset; |
| 78 | do_div(tmp, stripe_width); |
| 79 | |
| 80 | return tmp * flseg->stripe_unit + do_div(offset, flseg->stripe_unit); |
| 81 | } |
| 82 | |
| 83 | /* This function is used by the layout driver to calculate the |
| 84 | * offset of the file on the dserver based on whether the |
| 85 | * layout type is STRIPE_DENSE or STRIPE_SPARSE |
| 86 | */ |
| 87 | static loff_t |
| 88 | filelayout_get_dserver_offset(struct pnfs_layout_segment *lseg, loff_t offset) |
| 89 | { |
| 90 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); |
| 91 | |
| 92 | switch (flseg->stripe_type) { |
| 93 | case STRIPE_SPARSE: |
| 94 | return offset; |
| 95 | |
| 96 | case STRIPE_DENSE: |
| 97 | return filelayout_get_dense_offset(flseg, offset); |
| 98 | } |
| 99 | |
| 100 | BUG(); |
| 101 | } |
| 102 | |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 103 | /* |
Andy Adamson | dc70d7b | 2011-03-01 01:34:19 +0000 | [diff] [blame^] | 104 | * Call ops for the async read/write cases |
| 105 | * In the case of dense layouts, the offset needs to be reset to its |
| 106 | * original value. |
| 107 | */ |
| 108 | static void filelayout_read_prepare(struct rpc_task *task, void *data) |
| 109 | { |
| 110 | struct nfs_read_data *rdata = (struct nfs_read_data *)data; |
| 111 | |
| 112 | if (nfs41_setup_sequence(rdata->ds_clp->cl_session, |
| 113 | &rdata->args.seq_args, &rdata->res.seq_res, |
| 114 | 0, task)) |
| 115 | return; |
| 116 | |
| 117 | rpc_call_start(task); |
| 118 | } |
| 119 | |
| 120 | static void filelayout_read_call_done(struct rpc_task *task, void *data) |
| 121 | { |
| 122 | struct nfs_read_data *rdata = (struct nfs_read_data *)data; |
| 123 | |
| 124 | dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status); |
| 125 | |
| 126 | /* Note this may cause RPC to be resent */ |
| 127 | rdata->mds_ops->rpc_call_done(task, data); |
| 128 | } |
| 129 | |
| 130 | static void filelayout_read_release(void *data) |
| 131 | { |
| 132 | struct nfs_read_data *rdata = (struct nfs_read_data *)data; |
| 133 | |
| 134 | rdata->mds_ops->rpc_release(data); |
| 135 | } |
| 136 | |
| 137 | struct rpc_call_ops filelayout_read_call_ops = { |
| 138 | .rpc_call_prepare = filelayout_read_prepare, |
| 139 | .rpc_call_done = filelayout_read_call_done, |
| 140 | .rpc_release = filelayout_read_release, |
| 141 | }; |
| 142 | |
| 143 | static enum pnfs_try_status |
| 144 | filelayout_read_pagelist(struct nfs_read_data *data) |
| 145 | { |
| 146 | struct pnfs_layout_segment *lseg = data->lseg; |
| 147 | struct nfs4_pnfs_ds *ds; |
| 148 | loff_t offset = data->args.offset; |
| 149 | u32 j, idx; |
| 150 | struct nfs_fh *fh; |
| 151 | int status; |
| 152 | |
| 153 | dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n", |
| 154 | __func__, data->inode->i_ino, |
| 155 | data->args.pgbase, (size_t)data->args.count, offset); |
| 156 | |
| 157 | /* Retrieve the correct rpc_client for the byte range */ |
| 158 | j = nfs4_fl_calc_j_index(lseg, offset); |
| 159 | idx = nfs4_fl_calc_ds_index(lseg, j); |
| 160 | ds = nfs4_fl_prepare_ds(lseg, idx); |
| 161 | if (!ds) { |
| 162 | printk(KERN_ERR "%s: prepare_ds failed, use MDS\n", __func__); |
| 163 | return PNFS_NOT_ATTEMPTED; |
| 164 | } |
| 165 | dprintk("%s USE DS:ip %x %hu\n", __func__, |
| 166 | ntohl(ds->ds_ip_addr), ntohs(ds->ds_port)); |
| 167 | |
| 168 | /* No multipath support. Use first DS */ |
| 169 | data->ds_clp = ds->ds_clp; |
| 170 | fh = nfs4_fl_select_ds_fh(lseg, j); |
| 171 | if (fh) |
| 172 | data->args.fh = fh; |
| 173 | |
| 174 | data->args.offset = filelayout_get_dserver_offset(lseg, offset); |
| 175 | data->mds_offset = offset; |
| 176 | |
| 177 | /* Perform an asynchronous read to ds */ |
| 178 | status = nfs_initiate_read(data, ds->ds_clp->cl_rpcclient, |
| 179 | &filelayout_read_call_ops); |
| 180 | BUG_ON(status != 0); |
| 181 | return PNFS_ATTEMPTED; |
| 182 | } |
| 183 | |
| 184 | /* |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 185 | * filelayout_check_layout() |
| 186 | * |
| 187 | * Make sure layout segment parameters are sane WRT the device. |
| 188 | * At this point no generic layer initialization of the lseg has occurred, |
| 189 | * and nothing has been added to the layout_hdr cache. |
| 190 | * |
| 191 | */ |
| 192 | static int |
| 193 | filelayout_check_layout(struct pnfs_layout_hdr *lo, |
| 194 | struct nfs4_filelayout_segment *fl, |
| 195 | struct nfs4_layoutget_res *lgr, |
| 196 | struct nfs4_deviceid *id) |
| 197 | { |
| 198 | struct nfs4_file_layout_dsaddr *dsaddr; |
| 199 | int status = -EINVAL; |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 200 | struct nfs_server *nfss = NFS_SERVER(lo->plh_inode); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 201 | |
| 202 | dprintk("--> %s\n", __func__); |
| 203 | |
| 204 | if (fl->pattern_offset > lgr->range.offset) { |
| 205 | dprintk("%s pattern_offset %lld to large\n", |
| 206 | __func__, fl->pattern_offset); |
| 207 | goto out; |
| 208 | } |
| 209 | |
| 210 | if (fl->stripe_unit % PAGE_SIZE) { |
| 211 | dprintk("%s Stripe unit (%u) not page aligned\n", |
| 212 | __func__, fl->stripe_unit); |
| 213 | goto out; |
| 214 | } |
| 215 | |
| 216 | /* find and reference the deviceid */ |
| 217 | dsaddr = nfs4_fl_find_get_deviceid(nfss->nfs_client, id); |
| 218 | if (dsaddr == NULL) { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 219 | dsaddr = get_device_info(lo->plh_inode, id); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 220 | if (dsaddr == NULL) |
| 221 | goto out; |
| 222 | } |
| 223 | fl->dsaddr = dsaddr; |
| 224 | |
| 225 | if (fl->first_stripe_index < 0 || |
| 226 | fl->first_stripe_index >= dsaddr->stripe_count) { |
| 227 | dprintk("%s Bad first_stripe_index %d\n", |
| 228 | __func__, fl->first_stripe_index); |
| 229 | goto out_put; |
| 230 | } |
| 231 | |
| 232 | if ((fl->stripe_type == STRIPE_SPARSE && |
| 233 | fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) || |
| 234 | (fl->stripe_type == STRIPE_DENSE && |
| 235 | fl->num_fh != dsaddr->stripe_count)) { |
| 236 | dprintk("%s num_fh %u not valid for given packing\n", |
| 237 | __func__, fl->num_fh); |
| 238 | goto out_put; |
| 239 | } |
| 240 | |
| 241 | if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) { |
| 242 | dprintk("%s Stripe unit (%u) not aligned with rsize %u " |
| 243 | "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize, |
| 244 | nfss->wsize); |
| 245 | } |
| 246 | |
| 247 | status = 0; |
| 248 | out: |
| 249 | dprintk("--> %s returns %d\n", __func__, status); |
| 250 | return status; |
| 251 | out_put: |
| 252 | pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache, &dsaddr->deviceid); |
| 253 | goto out; |
| 254 | } |
| 255 | |
| 256 | static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl) |
| 257 | { |
| 258 | int i; |
| 259 | |
| 260 | for (i = 0; i < fl->num_fh; i++) { |
| 261 | if (!fl->fh_array[i]) |
| 262 | break; |
| 263 | kfree(fl->fh_array[i]); |
| 264 | } |
| 265 | kfree(fl->fh_array); |
| 266 | fl->fh_array = NULL; |
| 267 | } |
| 268 | |
| 269 | static void |
| 270 | _filelayout_free_lseg(struct nfs4_filelayout_segment *fl) |
| 271 | { |
| 272 | filelayout_free_fh_array(fl); |
| 273 | kfree(fl); |
| 274 | } |
| 275 | |
| 276 | static int |
| 277 | filelayout_decode_layout(struct pnfs_layout_hdr *flo, |
| 278 | struct nfs4_filelayout_segment *fl, |
| 279 | struct nfs4_layoutget_res *lgr, |
| 280 | struct nfs4_deviceid *id) |
| 281 | { |
| 282 | uint32_t *p = (uint32_t *)lgr->layout.buf; |
| 283 | uint32_t nfl_util; |
| 284 | int i; |
| 285 | |
| 286 | dprintk("%s: set_layout_map Begin\n", __func__); |
| 287 | |
| 288 | memcpy(id, p, sizeof(*id)); |
| 289 | p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE); |
| 290 | print_deviceid(id); |
| 291 | |
| 292 | nfl_util = be32_to_cpup(p++); |
| 293 | if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS) |
| 294 | fl->commit_through_mds = 1; |
| 295 | if (nfl_util & NFL4_UFLG_DENSE) |
| 296 | fl->stripe_type = STRIPE_DENSE; |
| 297 | else |
| 298 | fl->stripe_type = STRIPE_SPARSE; |
| 299 | fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK; |
| 300 | |
| 301 | fl->first_stripe_index = be32_to_cpup(p++); |
| 302 | p = xdr_decode_hyper(p, &fl->pattern_offset); |
| 303 | fl->num_fh = be32_to_cpup(p++); |
| 304 | |
| 305 | dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n", |
| 306 | __func__, nfl_util, fl->num_fh, fl->first_stripe_index, |
| 307 | fl->pattern_offset); |
| 308 | |
| 309 | fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *), |
| 310 | GFP_KERNEL); |
| 311 | if (!fl->fh_array) |
| 312 | return -ENOMEM; |
| 313 | |
| 314 | for (i = 0; i < fl->num_fh; i++) { |
| 315 | /* Do we want to use a mempool here? */ |
| 316 | fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL); |
| 317 | if (!fl->fh_array[i]) { |
| 318 | filelayout_free_fh_array(fl); |
| 319 | return -ENOMEM; |
| 320 | } |
| 321 | fl->fh_array[i]->size = be32_to_cpup(p++); |
| 322 | if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) { |
| 323 | printk(KERN_ERR "Too big fh %d received %d\n", |
| 324 | i, fl->fh_array[i]->size); |
| 325 | filelayout_free_fh_array(fl); |
| 326 | return -EIO; |
| 327 | } |
| 328 | memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size); |
| 329 | p += XDR_QUADLEN(fl->fh_array[i]->size); |
| 330 | dprintk("DEBUG: %s: fh len %d\n", __func__, |
| 331 | fl->fh_array[i]->size); |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static struct pnfs_layout_segment * |
| 338 | filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid, |
| 339 | struct nfs4_layoutget_res *lgr) |
| 340 | { |
| 341 | struct nfs4_filelayout_segment *fl; |
| 342 | int rc; |
| 343 | struct nfs4_deviceid id; |
| 344 | |
| 345 | dprintk("--> %s\n", __func__); |
| 346 | fl = kzalloc(sizeof(*fl), GFP_KERNEL); |
| 347 | if (!fl) |
| 348 | return NULL; |
| 349 | |
| 350 | rc = filelayout_decode_layout(layoutid, fl, lgr, &id); |
| 351 | if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) { |
| 352 | _filelayout_free_lseg(fl); |
| 353 | return NULL; |
| 354 | } |
| 355 | return &fl->generic_hdr; |
| 356 | } |
| 357 | |
| 358 | static void |
| 359 | filelayout_free_lseg(struct pnfs_layout_segment *lseg) |
| 360 | { |
Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 361 | struct nfs_server *nfss = NFS_SERVER(lseg->pls_layout->plh_inode); |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 362 | struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg); |
| 363 | |
| 364 | dprintk("--> %s\n", __func__); |
| 365 | pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache, |
| 366 | &fl->dsaddr->deviceid); |
| 367 | _filelayout_free_lseg(fl); |
| 368 | } |
| 369 | |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 370 | /* |
| 371 | * filelayout_pg_test(). Called by nfs_can_coalesce_requests() |
| 372 | * |
| 373 | * return 1 : coalesce page |
| 374 | * return 0 : don't coalesce page |
| 375 | */ |
| 376 | int |
| 377 | filelayout_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev, |
| 378 | struct nfs_page *req) |
| 379 | { |
| 380 | u64 p_stripe, r_stripe; |
| 381 | u32 stripe_unit; |
| 382 | |
| 383 | if (!pgio->pg_lseg) |
| 384 | return 1; |
| 385 | p_stripe = (u64)prev->wb_index << PAGE_CACHE_SHIFT; |
| 386 | r_stripe = (u64)req->wb_index << PAGE_CACHE_SHIFT; |
| 387 | stripe_unit = FILELAYOUT_LSEG(pgio->pg_lseg)->stripe_unit; |
| 388 | |
| 389 | do_div(p_stripe, stripe_unit); |
| 390 | do_div(r_stripe, stripe_unit); |
| 391 | |
| 392 | return (p_stripe == r_stripe); |
| 393 | } |
| 394 | |
Dean Hildebrand | 7ab672c | 2010-10-20 00:18:00 -0400 | [diff] [blame] | 395 | static struct pnfs_layoutdriver_type filelayout_type = { |
| 396 | .id = LAYOUT_NFSV4_1_FILES, |
| 397 | .name = "LAYOUT_NFSV4_1_FILES", |
| 398 | .owner = THIS_MODULE, |
Trond Myklebust | 1c78709 | 2010-10-21 16:56:48 -0400 | [diff] [blame] | 399 | .set_layoutdriver = filelayout_set_layoutdriver, |
| 400 | .clear_layoutdriver = filelayout_clear_layoutdriver, |
Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 401 | .alloc_lseg = filelayout_alloc_lseg, |
| 402 | .free_lseg = filelayout_free_lseg, |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 403 | .pg_test = filelayout_pg_test, |
Andy Adamson | dc70d7b | 2011-03-01 01:34:19 +0000 | [diff] [blame^] | 404 | .read_pagelist = filelayout_read_pagelist, |
Dean Hildebrand | 7ab672c | 2010-10-20 00:18:00 -0400 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | static int __init nfs4filelayout_init(void) |
| 408 | { |
| 409 | printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n", |
| 410 | __func__); |
| 411 | return pnfs_register_layoutdriver(&filelayout_type); |
| 412 | } |
| 413 | |
| 414 | static void __exit nfs4filelayout_exit(void) |
| 415 | { |
| 416 | printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n", |
| 417 | __func__); |
| 418 | pnfs_unregister_layoutdriver(&filelayout_type); |
| 419 | } |
| 420 | |
| 421 | module_init(nfs4filelayout_init); |
| 422 | module_exit(nfs4filelayout_exit); |