Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/blocklayout/blocklayout.c |
| 3 | * |
| 4 | * Module for the NFSv4.1 pNFS block layout driver. |
| 5 | * |
| 6 | * Copyright (c) 2006 The Regents of the University of Michigan. |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Andy Adamson <andros@citi.umich.edu> |
| 10 | * Fred Isaman <iisaman@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 from the |
| 22 | * university of michigan as to its fitness for any purpose, and without |
| 23 | * warranty by the university of michigan of any kind, either express |
| 24 | * or implied, including without limitation the implied warranties of |
| 25 | * merchantability and fitness for a particular purpose. the regents |
| 26 | * of the university of michigan shall not be liable for any damages, |
| 27 | * including special, indirect, incidental, or consequential damages, |
| 28 | * with respect to any claim arising out or in connection with the use |
| 29 | * of the software, even if it has been or is hereafter advised of the |
| 30 | * possibility of such damages. |
| 31 | */ |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/init.h> |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 34 | #include <linux/mount.h> |
| 35 | #include <linux/namei.h> |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 36 | |
| 37 | #include "blocklayout.h" |
| 38 | |
| 39 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD |
| 40 | |
| 41 | MODULE_LICENSE("GPL"); |
| 42 | MODULE_AUTHOR("Andy Adamson <andros@citi.umich.edu>"); |
| 43 | MODULE_DESCRIPTION("The NFSv4.1 pNFS Block layout driver"); |
| 44 | |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 45 | struct dentry *bl_device_pipe; |
| 46 | wait_queue_head_t bl_wq; |
| 47 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 48 | static enum pnfs_try_status |
| 49 | bl_read_pagelist(struct nfs_read_data *rdata) |
| 50 | { |
| 51 | return PNFS_NOT_ATTEMPTED; |
| 52 | } |
| 53 | |
| 54 | static enum pnfs_try_status |
| 55 | bl_write_pagelist(struct nfs_write_data *wdata, |
| 56 | int sync) |
| 57 | { |
| 58 | return PNFS_NOT_ATTEMPTED; |
| 59 | } |
| 60 | |
Fred Isaman | 9e69296 | 2011-07-30 20:52:41 -0400 | [diff] [blame] | 61 | /* FIXME - range ignored */ |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 62 | static void |
Fred Isaman | 9e69296 | 2011-07-30 20:52:41 -0400 | [diff] [blame] | 63 | release_extents(struct pnfs_block_layout *bl, struct pnfs_layout_range *range) |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 64 | { |
Fred Isaman | 9e69296 | 2011-07-30 20:52:41 -0400 | [diff] [blame] | 65 | int i; |
| 66 | struct pnfs_block_extent *be; |
| 67 | |
| 68 | spin_lock(&bl->bl_ext_lock); |
| 69 | for (i = 0; i < EXTENT_LISTS; i++) { |
| 70 | while (!list_empty(&bl->bl_extents[i])) { |
| 71 | be = list_first_entry(&bl->bl_extents[i], |
| 72 | struct pnfs_block_extent, |
| 73 | be_node); |
| 74 | list_del(&be->be_node); |
| 75 | bl_put_extent(be); |
| 76 | } |
| 77 | } |
| 78 | spin_unlock(&bl->bl_ext_lock); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 81 | static void |
| 82 | release_inval_marks(struct pnfs_inval_markings *marks) |
| 83 | { |
Fred Isaman | c1c2a4c | 2011-07-30 20:52:49 -0400 | [diff] [blame] | 84 | struct pnfs_inval_tracking *pos, *temp; |
| 85 | |
| 86 | list_for_each_entry_safe(pos, temp, &marks->im_tree.mtt_stub, it_link) { |
| 87 | list_del(&pos->it_link); |
| 88 | kfree(pos); |
| 89 | } |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
| 93 | static void bl_free_layout_hdr(struct pnfs_layout_hdr *lo) |
| 94 | { |
| 95 | struct pnfs_block_layout *bl = BLK_LO2EXT(lo); |
| 96 | |
| 97 | dprintk("%s enter\n", __func__); |
| 98 | release_extents(bl, NULL); |
| 99 | release_inval_marks(&bl->bl_inval); |
| 100 | kfree(bl); |
| 101 | } |
| 102 | |
| 103 | static struct pnfs_layout_hdr *bl_alloc_layout_hdr(struct inode *inode, |
| 104 | gfp_t gfp_flags) |
| 105 | { |
| 106 | struct pnfs_block_layout *bl; |
| 107 | |
| 108 | dprintk("%s enter\n", __func__); |
| 109 | bl = kzalloc(sizeof(*bl), gfp_flags); |
| 110 | if (!bl) |
| 111 | return NULL; |
| 112 | spin_lock_init(&bl->bl_ext_lock); |
| 113 | INIT_LIST_HEAD(&bl->bl_extents[0]); |
| 114 | INIT_LIST_HEAD(&bl->bl_extents[1]); |
| 115 | INIT_LIST_HEAD(&bl->bl_commit); |
| 116 | INIT_LIST_HEAD(&bl->bl_committing); |
| 117 | bl->bl_count = 0; |
| 118 | bl->bl_blocksize = NFS_SERVER(inode)->pnfs_blksize >> SECTOR_SHIFT; |
| 119 | BL_INIT_INVAL_MARKS(&bl->bl_inval, bl->bl_blocksize); |
| 120 | return &bl->bl_layout; |
| 121 | } |
| 122 | |
Fred Isaman | a60d2eb | 2011-07-30 20:52:44 -0400 | [diff] [blame] | 123 | static void bl_free_lseg(struct pnfs_layout_segment *lseg) |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 124 | { |
Fred Isaman | a60d2eb | 2011-07-30 20:52:44 -0400 | [diff] [blame] | 125 | dprintk("%s enter\n", __func__); |
| 126 | kfree(lseg); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Fred Isaman | a60d2eb | 2011-07-30 20:52:44 -0400 | [diff] [blame] | 129 | /* We pretty much ignore lseg, and store all data layout wide, so we |
| 130 | * can correctly merge. |
| 131 | */ |
| 132 | static struct pnfs_layout_segment *bl_alloc_lseg(struct pnfs_layout_hdr *lo, |
| 133 | struct nfs4_layoutget_res *lgr, |
| 134 | gfp_t gfp_flags) |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 135 | { |
Fred Isaman | a60d2eb | 2011-07-30 20:52:44 -0400 | [diff] [blame] | 136 | struct pnfs_layout_segment *lseg; |
| 137 | int status; |
| 138 | |
| 139 | dprintk("%s enter\n", __func__); |
| 140 | lseg = kzalloc(sizeof(*lseg), gfp_flags); |
| 141 | if (!lseg) |
| 142 | return ERR_PTR(-ENOMEM); |
| 143 | status = nfs4_blk_process_layoutget(lo, lgr, gfp_flags); |
| 144 | if (status) { |
| 145 | /* We don't want to call the full-blown bl_free_lseg, |
| 146 | * since on error extents were not touched. |
| 147 | */ |
| 148 | kfree(lseg); |
| 149 | return ERR_PTR(status); |
| 150 | } |
| 151 | return lseg; |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void |
| 155 | bl_encode_layoutcommit(struct pnfs_layout_hdr *lo, struct xdr_stream *xdr, |
| 156 | const struct nfs4_layoutcommit_args *arg) |
| 157 | { |
Fred Isaman | 90ace12 | 2011-07-30 20:52:51 -0400 | [diff] [blame] | 158 | dprintk("%s enter\n", __func__); |
| 159 | encode_pnfs_block_layoutupdate(BLK_LO2EXT(lo), xdr, arg); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void |
| 163 | bl_cleanup_layoutcommit(struct nfs4_layoutcommit_data *lcdata) |
| 164 | { |
Fred Isaman | b2be781 | 2011-07-30 20:52:52 -0400 | [diff] [blame^] | 165 | struct pnfs_layout_hdr *lo = NFS_I(lcdata->args.inode)->layout; |
| 166 | |
| 167 | dprintk("%s enter\n", __func__); |
| 168 | clean_pnfs_block_layoutupdate(BLK_LO2EXT(lo), &lcdata->args, lcdata->res.status); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 171 | static void free_blk_mountid(struct block_mount_id *mid) |
| 172 | { |
| 173 | if (mid) { |
| 174 | struct pnfs_block_dev *dev; |
| 175 | spin_lock(&mid->bm_lock); |
| 176 | while (!list_empty(&mid->bm_devlist)) { |
| 177 | dev = list_first_entry(&mid->bm_devlist, |
| 178 | struct pnfs_block_dev, |
| 179 | bm_node); |
| 180 | list_del(&dev->bm_node); |
| 181 | bl_free_block_dev(dev); |
| 182 | } |
| 183 | spin_unlock(&mid->bm_lock); |
| 184 | kfree(mid); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* This is mostly copied from the filelayout's get_device_info function. |
| 189 | * It seems much of this should be at the generic pnfs level. |
| 190 | */ |
| 191 | static struct pnfs_block_dev * |
| 192 | nfs4_blk_get_deviceinfo(struct nfs_server *server, const struct nfs_fh *fh, |
| 193 | struct nfs4_deviceid *d_id) |
| 194 | { |
| 195 | struct pnfs_device *dev; |
| 196 | struct pnfs_block_dev *rv = NULL; |
| 197 | u32 max_resp_sz; |
| 198 | int max_pages; |
| 199 | struct page **pages = NULL; |
| 200 | int i, rc; |
| 201 | |
| 202 | /* |
| 203 | * Use the session max response size as the basis for setting |
| 204 | * GETDEVICEINFO's maxcount |
| 205 | */ |
| 206 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; |
| 207 | max_pages = max_resp_sz >> PAGE_SHIFT; |
| 208 | dprintk("%s max_resp_sz %u max_pages %d\n", |
| 209 | __func__, max_resp_sz, max_pages); |
| 210 | |
| 211 | dev = kmalloc(sizeof(*dev), GFP_NOFS); |
| 212 | if (!dev) { |
| 213 | dprintk("%s kmalloc failed\n", __func__); |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | pages = kzalloc(max_pages * sizeof(struct page *), GFP_NOFS); |
| 218 | if (pages == NULL) { |
| 219 | kfree(dev); |
| 220 | return NULL; |
| 221 | } |
| 222 | for (i = 0; i < max_pages; i++) { |
| 223 | pages[i] = alloc_page(GFP_NOFS); |
| 224 | if (!pages[i]) |
| 225 | goto out_free; |
| 226 | } |
| 227 | |
| 228 | memcpy(&dev->dev_id, d_id, sizeof(*d_id)); |
| 229 | dev->layout_type = LAYOUT_BLOCK_VOLUME; |
| 230 | dev->pages = pages; |
| 231 | dev->pgbase = 0; |
| 232 | dev->pglen = PAGE_SIZE * max_pages; |
| 233 | dev->mincount = 0; |
| 234 | |
| 235 | dprintk("%s: dev_id: %s\n", __func__, dev->dev_id.data); |
| 236 | rc = nfs4_proc_getdeviceinfo(server, dev); |
| 237 | dprintk("%s getdevice info returns %d\n", __func__, rc); |
| 238 | if (rc) |
| 239 | goto out_free; |
| 240 | |
| 241 | rv = nfs4_blk_decode_device(server, dev); |
| 242 | out_free: |
| 243 | for (i = 0; i < max_pages; i++) |
| 244 | __free_page(pages[i]); |
| 245 | kfree(pages); |
| 246 | kfree(dev); |
| 247 | return rv; |
| 248 | } |
| 249 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 250 | static int |
| 251 | bl_set_layoutdriver(struct nfs_server *server, const struct nfs_fh *fh) |
| 252 | { |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 253 | struct block_mount_id *b_mt_id = NULL; |
| 254 | struct pnfs_devicelist *dlist = NULL; |
| 255 | struct pnfs_block_dev *bdev; |
| 256 | LIST_HEAD(block_disklist); |
| 257 | int status = 0, i; |
| 258 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 259 | dprintk("%s enter\n", __func__); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 260 | |
| 261 | if (server->pnfs_blksize == 0) { |
| 262 | dprintk("%s Server did not return blksize\n", __func__); |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | b_mt_id = kzalloc(sizeof(struct block_mount_id), GFP_NOFS); |
| 266 | if (!b_mt_id) { |
| 267 | status = -ENOMEM; |
| 268 | goto out_error; |
| 269 | } |
| 270 | /* Initialize nfs4 block layout mount id */ |
| 271 | spin_lock_init(&b_mt_id->bm_lock); |
| 272 | INIT_LIST_HEAD(&b_mt_id->bm_devlist); |
| 273 | |
| 274 | dlist = kmalloc(sizeof(struct pnfs_devicelist), GFP_NOFS); |
| 275 | if (!dlist) { |
| 276 | status = -ENOMEM; |
| 277 | goto out_error; |
| 278 | } |
| 279 | dlist->eof = 0; |
| 280 | while (!dlist->eof) { |
| 281 | status = nfs4_proc_getdevicelist(server, fh, dlist); |
| 282 | if (status) |
| 283 | goto out_error; |
| 284 | dprintk("%s GETDEVICELIST numdevs=%i, eof=%i\n", |
| 285 | __func__, dlist->num_devs, dlist->eof); |
| 286 | for (i = 0; i < dlist->num_devs; i++) { |
| 287 | bdev = nfs4_blk_get_deviceinfo(server, fh, |
| 288 | &dlist->dev_id[i]); |
| 289 | if (!bdev) { |
| 290 | status = -ENODEV; |
| 291 | goto out_error; |
| 292 | } |
| 293 | spin_lock(&b_mt_id->bm_lock); |
| 294 | list_add(&bdev->bm_node, &b_mt_id->bm_devlist); |
| 295 | spin_unlock(&b_mt_id->bm_lock); |
| 296 | } |
| 297 | } |
| 298 | dprintk("%s SUCCESS\n", __func__); |
| 299 | server->pnfs_ld_data = b_mt_id; |
| 300 | |
| 301 | out_return: |
| 302 | kfree(dlist); |
| 303 | return status; |
| 304 | |
| 305 | out_error: |
| 306 | free_blk_mountid(b_mt_id); |
| 307 | goto out_return; |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static int |
| 311 | bl_clear_layoutdriver(struct nfs_server *server) |
| 312 | { |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 313 | struct block_mount_id *b_mt_id = server->pnfs_ld_data; |
| 314 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 315 | dprintk("%s enter\n", __func__); |
Fred Isaman | 2f9fd18 | 2011-07-30 20:52:46 -0400 | [diff] [blame] | 316 | free_blk_mountid(b_mt_id); |
| 317 | dprintk("%s RETURNS\n", __func__); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
Benny Halevy | e9643fe | 2011-07-30 20:52:40 -0400 | [diff] [blame] | 321 | static const struct nfs_pageio_ops bl_pg_read_ops = { |
| 322 | .pg_init = pnfs_generic_pg_init_read, |
| 323 | .pg_test = pnfs_generic_pg_test, |
| 324 | .pg_doio = pnfs_generic_pg_readpages, |
| 325 | }; |
| 326 | |
| 327 | static const struct nfs_pageio_ops bl_pg_write_ops = { |
| 328 | .pg_init = pnfs_generic_pg_init_write, |
| 329 | .pg_test = pnfs_generic_pg_test, |
| 330 | .pg_doio = pnfs_generic_pg_writepages, |
| 331 | }; |
| 332 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 333 | static struct pnfs_layoutdriver_type blocklayout_type = { |
| 334 | .id = LAYOUT_BLOCK_VOLUME, |
| 335 | .name = "LAYOUT_BLOCK_VOLUME", |
| 336 | .read_pagelist = bl_read_pagelist, |
| 337 | .write_pagelist = bl_write_pagelist, |
| 338 | .alloc_layout_hdr = bl_alloc_layout_hdr, |
| 339 | .free_layout_hdr = bl_free_layout_hdr, |
| 340 | .alloc_lseg = bl_alloc_lseg, |
| 341 | .free_lseg = bl_free_lseg, |
| 342 | .encode_layoutcommit = bl_encode_layoutcommit, |
| 343 | .cleanup_layoutcommit = bl_cleanup_layoutcommit, |
| 344 | .set_layoutdriver = bl_set_layoutdriver, |
| 345 | .clear_layoutdriver = bl_clear_layoutdriver, |
Benny Halevy | e9643fe | 2011-07-30 20:52:40 -0400 | [diff] [blame] | 346 | .pg_read_ops = &bl_pg_read_ops, |
| 347 | .pg_write_ops = &bl_pg_write_ops, |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 348 | }; |
| 349 | |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 350 | static const struct rpc_pipe_ops bl_upcall_ops = { |
| 351 | .upcall = bl_pipe_upcall, |
| 352 | .downcall = bl_pipe_downcall, |
| 353 | .destroy_msg = bl_pipe_destroy_msg, |
| 354 | }; |
| 355 | |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 356 | static int __init nfs4blocklayout_init(void) |
| 357 | { |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 358 | struct vfsmount *mnt; |
| 359 | struct path path; |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 360 | int ret; |
| 361 | |
| 362 | dprintk("%s: NFSv4 Block Layout Driver Registering...\n", __func__); |
| 363 | |
| 364 | ret = pnfs_register_layoutdriver(&blocklayout_type); |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 365 | if (ret) |
| 366 | goto out; |
| 367 | |
| 368 | init_waitqueue_head(&bl_wq); |
| 369 | |
| 370 | mnt = rpc_get_mount(); |
| 371 | if (IS_ERR(mnt)) { |
| 372 | ret = PTR_ERR(mnt); |
| 373 | goto out_remove; |
| 374 | } |
| 375 | |
| 376 | ret = vfs_path_lookup(mnt->mnt_root, |
| 377 | mnt, |
| 378 | NFS_PIPE_DIRNAME, 0, &path); |
| 379 | if (ret) |
| 380 | goto out_remove; |
| 381 | |
| 382 | bl_device_pipe = rpc_mkpipe(path.dentry, "blocklayout", NULL, |
| 383 | &bl_upcall_ops, 0); |
| 384 | if (IS_ERR(bl_device_pipe)) { |
| 385 | ret = PTR_ERR(bl_device_pipe); |
| 386 | goto out_remove; |
| 387 | } |
| 388 | out: |
| 389 | return ret; |
| 390 | |
| 391 | out_remove: |
| 392 | pnfs_unregister_layoutdriver(&blocklayout_type); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | static void __exit nfs4blocklayout_exit(void) |
| 397 | { |
| 398 | dprintk("%s: NFSv4 Block Layout Driver Unregistering...\n", |
| 399 | __func__); |
| 400 | |
| 401 | pnfs_unregister_layoutdriver(&blocklayout_type); |
Jim Rees | fe0a9b7 | 2011-07-30 20:52:42 -0400 | [diff] [blame] | 402 | rpc_unlink(bl_device_pipe); |
Fred Isaman | 155e752 | 2011-07-30 20:52:39 -0400 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | MODULE_ALIAS("nfs-layouttype4-3"); |
| 406 | |
| 407 | module_init(nfs4blocklayout_init); |
| 408 | module_exit(nfs4blocklayout_exit); |