| 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 |  | 
| Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 69 | /* | 
|  | 70 | * filelayout_check_layout() | 
|  | 71 | * | 
|  | 72 | * Make sure layout segment parameters are sane WRT the device. | 
|  | 73 | * At this point no generic layer initialization of the lseg has occurred, | 
|  | 74 | * and nothing has been added to the layout_hdr cache. | 
|  | 75 | * | 
|  | 76 | */ | 
|  | 77 | static int | 
|  | 78 | filelayout_check_layout(struct pnfs_layout_hdr *lo, | 
|  | 79 | struct nfs4_filelayout_segment *fl, | 
|  | 80 | struct nfs4_layoutget_res *lgr, | 
|  | 81 | struct nfs4_deviceid *id) | 
|  | 82 | { | 
|  | 83 | struct nfs4_file_layout_dsaddr *dsaddr; | 
|  | 84 | int status = -EINVAL; | 
| Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 85 | struct nfs_server *nfss = NFS_SERVER(lo->plh_inode); | 
| Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 86 |  | 
|  | 87 | dprintk("--> %s\n", __func__); | 
|  | 88 |  | 
|  | 89 | if (fl->pattern_offset > lgr->range.offset) { | 
|  | 90 | dprintk("%s pattern_offset %lld to large\n", | 
|  | 91 | __func__, fl->pattern_offset); | 
|  | 92 | goto out; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | if (fl->stripe_unit % PAGE_SIZE) { | 
|  | 96 | dprintk("%s Stripe unit (%u) not page aligned\n", | 
|  | 97 | __func__, fl->stripe_unit); | 
|  | 98 | goto out; | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | /* find and reference the deviceid */ | 
|  | 102 | dsaddr = nfs4_fl_find_get_deviceid(nfss->nfs_client, id); | 
|  | 103 | if (dsaddr == NULL) { | 
| Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 104 | dsaddr = get_device_info(lo->plh_inode, id); | 
| Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 105 | if (dsaddr == NULL) | 
|  | 106 | goto out; | 
|  | 107 | } | 
|  | 108 | fl->dsaddr = dsaddr; | 
|  | 109 |  | 
|  | 110 | if (fl->first_stripe_index < 0 || | 
|  | 111 | fl->first_stripe_index >= dsaddr->stripe_count) { | 
|  | 112 | dprintk("%s Bad first_stripe_index %d\n", | 
|  | 113 | __func__, fl->first_stripe_index); | 
|  | 114 | goto out_put; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | if ((fl->stripe_type == STRIPE_SPARSE && | 
|  | 118 | fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) || | 
|  | 119 | (fl->stripe_type == STRIPE_DENSE && | 
|  | 120 | fl->num_fh != dsaddr->stripe_count)) { | 
|  | 121 | dprintk("%s num_fh %u not valid for given packing\n", | 
|  | 122 | __func__, fl->num_fh); | 
|  | 123 | goto out_put; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) { | 
|  | 127 | dprintk("%s Stripe unit (%u) not aligned with rsize %u " | 
|  | 128 | "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize, | 
|  | 129 | nfss->wsize); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | status = 0; | 
|  | 133 | out: | 
|  | 134 | dprintk("--> %s returns %d\n", __func__, status); | 
|  | 135 | return status; | 
|  | 136 | out_put: | 
|  | 137 | pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache, &dsaddr->deviceid); | 
|  | 138 | goto out; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl) | 
|  | 142 | { | 
|  | 143 | int i; | 
|  | 144 |  | 
|  | 145 | for (i = 0; i < fl->num_fh; i++) { | 
|  | 146 | if (!fl->fh_array[i]) | 
|  | 147 | break; | 
|  | 148 | kfree(fl->fh_array[i]); | 
|  | 149 | } | 
|  | 150 | kfree(fl->fh_array); | 
|  | 151 | fl->fh_array = NULL; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | static void | 
|  | 155 | _filelayout_free_lseg(struct nfs4_filelayout_segment *fl) | 
|  | 156 | { | 
|  | 157 | filelayout_free_fh_array(fl); | 
|  | 158 | kfree(fl); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | static int | 
|  | 162 | filelayout_decode_layout(struct pnfs_layout_hdr *flo, | 
|  | 163 | struct nfs4_filelayout_segment *fl, | 
|  | 164 | struct nfs4_layoutget_res *lgr, | 
|  | 165 | struct nfs4_deviceid *id) | 
|  | 166 | { | 
|  | 167 | uint32_t *p = (uint32_t *)lgr->layout.buf; | 
|  | 168 | uint32_t nfl_util; | 
|  | 169 | int i; | 
|  | 170 |  | 
|  | 171 | dprintk("%s: set_layout_map Begin\n", __func__); | 
|  | 172 |  | 
|  | 173 | memcpy(id, p, sizeof(*id)); | 
|  | 174 | p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE); | 
|  | 175 | print_deviceid(id); | 
|  | 176 |  | 
|  | 177 | nfl_util = be32_to_cpup(p++); | 
|  | 178 | if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS) | 
|  | 179 | fl->commit_through_mds = 1; | 
|  | 180 | if (nfl_util & NFL4_UFLG_DENSE) | 
|  | 181 | fl->stripe_type = STRIPE_DENSE; | 
|  | 182 | else | 
|  | 183 | fl->stripe_type = STRIPE_SPARSE; | 
|  | 184 | fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK; | 
|  | 185 |  | 
|  | 186 | fl->first_stripe_index = be32_to_cpup(p++); | 
|  | 187 | p = xdr_decode_hyper(p, &fl->pattern_offset); | 
|  | 188 | fl->num_fh = be32_to_cpup(p++); | 
|  | 189 |  | 
|  | 190 | dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n", | 
|  | 191 | __func__, nfl_util, fl->num_fh, fl->first_stripe_index, | 
|  | 192 | fl->pattern_offset); | 
|  | 193 |  | 
|  | 194 | fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *), | 
|  | 195 | GFP_KERNEL); | 
|  | 196 | if (!fl->fh_array) | 
|  | 197 | return -ENOMEM; | 
|  | 198 |  | 
|  | 199 | for (i = 0; i < fl->num_fh; i++) { | 
|  | 200 | /* Do we want to use a mempool here? */ | 
|  | 201 | fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL); | 
|  | 202 | if (!fl->fh_array[i]) { | 
|  | 203 | filelayout_free_fh_array(fl); | 
|  | 204 | return -ENOMEM; | 
|  | 205 | } | 
|  | 206 | fl->fh_array[i]->size = be32_to_cpup(p++); | 
|  | 207 | if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) { | 
|  | 208 | printk(KERN_ERR "Too big fh %d received %d\n", | 
|  | 209 | i, fl->fh_array[i]->size); | 
|  | 210 | filelayout_free_fh_array(fl); | 
|  | 211 | return -EIO; | 
|  | 212 | } | 
|  | 213 | memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size); | 
|  | 214 | p += XDR_QUADLEN(fl->fh_array[i]->size); | 
|  | 215 | dprintk("DEBUG: %s: fh len %d\n", __func__, | 
|  | 216 | fl->fh_array[i]->size); | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | return 0; | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | static struct pnfs_layout_segment * | 
|  | 223 | filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid, | 
|  | 224 | struct nfs4_layoutget_res *lgr) | 
|  | 225 | { | 
|  | 226 | struct nfs4_filelayout_segment *fl; | 
|  | 227 | int rc; | 
|  | 228 | struct nfs4_deviceid id; | 
|  | 229 |  | 
|  | 230 | dprintk("--> %s\n", __func__); | 
|  | 231 | fl = kzalloc(sizeof(*fl), GFP_KERNEL); | 
|  | 232 | if (!fl) | 
|  | 233 | return NULL; | 
|  | 234 |  | 
|  | 235 | rc = filelayout_decode_layout(layoutid, fl, lgr, &id); | 
|  | 236 | if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) { | 
|  | 237 | _filelayout_free_lseg(fl); | 
|  | 238 | return NULL; | 
|  | 239 | } | 
|  | 240 | return &fl->generic_hdr; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | static void | 
|  | 244 | filelayout_free_lseg(struct pnfs_layout_segment *lseg) | 
|  | 245 | { | 
| Fred Isaman | b7edfaa | 2011-01-06 11:36:21 +0000 | [diff] [blame] | 246 | struct nfs_server *nfss = NFS_SERVER(lseg->pls_layout->plh_inode); | 
| Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 247 | struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg); | 
|  | 248 |  | 
|  | 249 | dprintk("--> %s\n", __func__); | 
|  | 250 | pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache, | 
|  | 251 | &fl->dsaddr->deviceid); | 
|  | 252 | _filelayout_free_lseg(fl); | 
|  | 253 | } | 
|  | 254 |  | 
| Dean Hildebrand | 7ab672c | 2010-10-20 00:18:00 -0400 | [diff] [blame] | 255 | static struct pnfs_layoutdriver_type filelayout_type = { | 
|  | 256 | .id = LAYOUT_NFSV4_1_FILES, | 
|  | 257 | .name = "LAYOUT_NFSV4_1_FILES", | 
|  | 258 | .owner = THIS_MODULE, | 
| Trond Myklebust | 1c78709 | 2010-10-21 16:56:48 -0400 | [diff] [blame] | 259 | .set_layoutdriver = filelayout_set_layoutdriver, | 
|  | 260 | .clear_layoutdriver = filelayout_clear_layoutdriver, | 
| Andy Adamson | 16b374c | 2010-10-20 00:18:04 -0400 | [diff] [blame] | 261 | .alloc_lseg              = filelayout_alloc_lseg, | 
|  | 262 | .free_lseg               = filelayout_free_lseg, | 
| Dean Hildebrand | 7ab672c | 2010-10-20 00:18:00 -0400 | [diff] [blame] | 263 | }; | 
|  | 264 |  | 
|  | 265 | static int __init nfs4filelayout_init(void) | 
|  | 266 | { | 
|  | 267 | printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n", | 
|  | 268 | __func__); | 
|  | 269 | return pnfs_register_layoutdriver(&filelayout_type); | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | static void __exit nfs4filelayout_exit(void) | 
|  | 273 | { | 
|  | 274 | printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n", | 
|  | 275 | __func__); | 
|  | 276 | pnfs_unregister_layoutdriver(&filelayout_type); | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | module_init(nfs4filelayout_init); | 
|  | 280 | module_exit(nfs4filelayout_exit); |