Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005, 2006 |
Boaz Harrosh | 27d2e14 | 2009-06-14 17:23:09 +0300 | [diff] [blame] | 3 | * Avishay Traeger (avishay@gmail.com) |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 4 | * Copyright (C) 2008, 2009 |
| 5 | * Boaz Harrosh <bharrosh@panasas.com> |
| 6 | * |
| 7 | * This file is part of exofs. |
| 8 | * |
| 9 | * exofs is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation. Since it is based on ext2, and the only |
| 12 | * valid version of GPL for the Linux kernel is version 2, the only valid |
| 13 | * version of GPL for exofs is version 2. |
| 14 | * |
| 15 | * exofs is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with exofs; if not, write to the Free Software |
| 22 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | */ |
| 24 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Paul Gortmaker | 143cb49 | 2011-07-01 14:23:34 -0400 | [diff] [blame] | 26 | #include <linux/module.h> |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 27 | #include <asm/div64.h> |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 28 | #include <linux/lcm.h> |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 29 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 30 | #include "ore_raid.h" |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 31 | |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 32 | MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>"); |
| 33 | MODULE_DESCRIPTION("Objects Raid Engine ore.ko"); |
| 34 | MODULE_LICENSE("GPL"); |
| 35 | |
Boaz Harrosh | 5a51c0c | 2011-09-28 13:18:45 +0300 | [diff] [blame] | 36 | /* ore_verify_layout does a couple of things: |
| 37 | * 1. Given a minimum number of needed parameters fixes up the rest of the |
| 38 | * members to be operatonals for the ore. The needed parameters are those |
| 39 | * that are defined by the pnfs-objects layout STD. |
| 40 | * 2. Check to see if the current ore code actually supports these parameters |
| 41 | * for example stripe_unit must be a multple of the system PAGE_SIZE, |
| 42 | * and etc... |
| 43 | * 3. Cache some havily used calculations that will be needed by users. |
| 44 | */ |
| 45 | |
Boaz Harrosh | 5a51c0c | 2011-09-28 13:18:45 +0300 | [diff] [blame] | 46 | enum { BIO_MAX_PAGES_KMALLOC = |
| 47 | (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),}; |
| 48 | |
| 49 | int ore_verify_layout(unsigned total_comps, struct ore_layout *layout) |
| 50 | { |
| 51 | u64 stripe_length; |
| 52 | |
Boaz Harrosh | 44231e6 | 2010-11-21 20:03:24 +0200 | [diff] [blame] | 53 | switch (layout->raid_algorithm) { |
| 54 | case PNFS_OSD_RAID_0: |
| 55 | layout->parity = 0; |
| 56 | break; |
| 57 | case PNFS_OSD_RAID_5: |
| 58 | layout->parity = 1; |
| 59 | break; |
| 60 | case PNFS_OSD_RAID_PQ: |
| 61 | case PNFS_OSD_RAID_4: |
| 62 | default: |
| 63 | ORE_ERR("Only RAID_0/5 for now\n"); |
Boaz Harrosh | 5a51c0c | 2011-09-28 13:18:45 +0300 | [diff] [blame] | 64 | return -EINVAL; |
| 65 | } |
| 66 | if (0 != (layout->stripe_unit & ~PAGE_MASK)) { |
| 67 | ORE_ERR("Stripe Unit(0x%llx)" |
| 68 | " must be Multples of PAGE_SIZE(0x%lx)\n", |
| 69 | _LLU(layout->stripe_unit), PAGE_SIZE); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | if (layout->group_width) { |
| 73 | if (!layout->group_depth) { |
| 74 | ORE_ERR("group_depth == 0 && group_width != 0\n"); |
| 75 | return -EINVAL; |
| 76 | } |
| 77 | if (total_comps < (layout->group_width * layout->mirrors_p1)) { |
| 78 | ORE_ERR("Data Map wrong, " |
| 79 | "numdevs=%d < group_width=%d * mirrors=%d\n", |
| 80 | total_comps, layout->group_width, |
| 81 | layout->mirrors_p1); |
| 82 | return -EINVAL; |
| 83 | } |
| 84 | layout->group_count = total_comps / layout->mirrors_p1 / |
| 85 | layout->group_width; |
| 86 | } else { |
| 87 | if (layout->group_depth) { |
| 88 | printk(KERN_NOTICE "Warning: group_depth ignored " |
| 89 | "group_width == 0 && group_depth == %lld\n", |
| 90 | _LLU(layout->group_depth)); |
| 91 | } |
| 92 | layout->group_width = total_comps / layout->mirrors_p1; |
| 93 | layout->group_depth = -1; |
| 94 | layout->group_count = 1; |
| 95 | } |
| 96 | |
| 97 | stripe_length = (u64)layout->group_width * layout->stripe_unit; |
| 98 | if (stripe_length >= (1ULL << 32)) { |
| 99 | ORE_ERR("Stripe_length(0x%llx) >= 32bit is not supported\n", |
| 100 | _LLU(stripe_length)); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | layout->max_io_length = |
| 105 | (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE - layout->stripe_unit) * |
Boaz Harrosh | aad560b | 2013-11-21 17:58:08 +0200 | [diff] [blame] | 106 | (layout->group_width - layout->parity); |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 107 | if (layout->parity) { |
| 108 | unsigned stripe_length = |
| 109 | (layout->group_width - layout->parity) * |
| 110 | layout->stripe_unit; |
| 111 | |
| 112 | layout->max_io_length /= stripe_length; |
| 113 | layout->max_io_length *= stripe_length; |
| 114 | } |
Boaz Harrosh | 5a51c0c | 2011-09-28 13:18:45 +0300 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | EXPORT_SYMBOL(ore_verify_layout); |
| 118 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 119 | static u8 *_ios_cred(struct ore_io_state *ios, unsigned index) |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 120 | { |
Boaz Harrosh | 5bf696d | 2011-09-28 11:39:59 +0300 | [diff] [blame] | 121 | return ios->oc->comps[index & ios->oc->single_comp].cred; |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 124 | static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index) |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 125 | { |
Boaz Harrosh | 5bf696d | 2011-09-28 11:39:59 +0300 | [diff] [blame] | 126 | return &ios->oc->comps[index & ios->oc->single_comp].obj; |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 129 | static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index) |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 130 | { |
Boaz Harrosh | 3bd9856 | 2011-09-28 12:04:23 +0300 | [diff] [blame] | 131 | ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n", |
| 132 | ios->oc->first_dev, ios->oc->numdevs, index, |
| 133 | ios->oc->ods); |
| 134 | |
Boaz Harrosh | d866d87 | 2011-09-28 14:43:09 +0300 | [diff] [blame] | 135 | return ore_comp_dev(ios->oc, index); |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 138 | int _ore_get_io_state(struct ore_layout *layout, |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 139 | struct ore_components *oc, unsigned numdevs, |
| 140 | unsigned sgs_per_dev, unsigned num_par_pages, |
| 141 | struct ore_io_state **pios) |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 142 | { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 143 | struct ore_io_state *ios; |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 144 | struct page **pages; |
| 145 | struct osd_sg_entry *sgilist; |
| 146 | struct __alloc_all_io_state { |
| 147 | struct ore_io_state ios; |
| 148 | struct ore_per_dev_state per_dev[numdevs]; |
| 149 | union { |
| 150 | struct osd_sg_entry sglist[sgs_per_dev * numdevs]; |
| 151 | struct page *pages[num_par_pages]; |
| 152 | }; |
| 153 | } *_aios; |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 154 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 155 | if (likely(sizeof(*_aios) <= PAGE_SIZE)) { |
| 156 | _aios = kzalloc(sizeof(*_aios), GFP_KERNEL); |
| 157 | if (unlikely(!_aios)) { |
| 158 | ORE_DBGMSG("Failed kzalloc bytes=%zd\n", |
| 159 | sizeof(*_aios)); |
| 160 | *pios = NULL; |
| 161 | return -ENOMEM; |
| 162 | } |
| 163 | pages = num_par_pages ? _aios->pages : NULL; |
| 164 | sgilist = sgs_per_dev ? _aios->sglist : NULL; |
| 165 | ios = &_aios->ios; |
| 166 | } else { |
| 167 | struct __alloc_small_io_state { |
| 168 | struct ore_io_state ios; |
| 169 | struct ore_per_dev_state per_dev[numdevs]; |
| 170 | } *_aio_small; |
| 171 | union __extra_part { |
| 172 | struct osd_sg_entry sglist[sgs_per_dev * numdevs]; |
| 173 | struct page *pages[num_par_pages]; |
| 174 | } *extra_part; |
| 175 | |
| 176 | _aio_small = kzalloc(sizeof(*_aio_small), GFP_KERNEL); |
| 177 | if (unlikely(!_aio_small)) { |
| 178 | ORE_DBGMSG("Failed alloc first part bytes=%zd\n", |
| 179 | sizeof(*_aio_small)); |
| 180 | *pios = NULL; |
| 181 | return -ENOMEM; |
| 182 | } |
| 183 | extra_part = kzalloc(sizeof(*extra_part), GFP_KERNEL); |
| 184 | if (unlikely(!extra_part)) { |
| 185 | ORE_DBGMSG("Failed alloc second part bytes=%zd\n", |
| 186 | sizeof(*extra_part)); |
| 187 | kfree(_aio_small); |
| 188 | *pios = NULL; |
| 189 | return -ENOMEM; |
| 190 | } |
| 191 | |
| 192 | pages = num_par_pages ? extra_part->pages : NULL; |
| 193 | sgilist = sgs_per_dev ? extra_part->sglist : NULL; |
| 194 | /* In this case the per_dev[0].sgilist holds the pointer to |
| 195 | * be freed |
| 196 | */ |
| 197 | ios = &_aio_small->ios; |
| 198 | ios->extra_part_alloc = true; |
| 199 | } |
| 200 | |
| 201 | if (pages) { |
| 202 | ios->parity_pages = pages; |
| 203 | ios->max_par_pages = num_par_pages; |
| 204 | } |
| 205 | if (sgilist) { |
| 206 | unsigned d; |
| 207 | |
| 208 | for (d = 0; d < numdevs; ++d) { |
| 209 | ios->per_dev[d].sglist = sgilist; |
| 210 | sgilist += sgs_per_dev; |
| 211 | } |
| 212 | ios->sgs_per_dev = sgs_per_dev; |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame] | 215 | ios->layout = layout; |
Boaz Harrosh | 5bf696d | 2011-09-28 11:39:59 +0300 | [diff] [blame] | 216 | ios->oc = oc; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 217 | *pios = ios; |
| 218 | return 0; |
| 219 | } |
Boaz Harrosh | b916c5c | 2011-09-28 11:55:51 +0300 | [diff] [blame] | 220 | |
| 221 | /* Allocate an io_state for only a single group of devices |
| 222 | * |
| 223 | * If a user needs to call ore_read/write() this version must be used becase it |
| 224 | * allocates extra stuff for striping and raid. |
| 225 | * The ore might decide to only IO less then @length bytes do to alignmets |
| 226 | * and constrains as follows: |
| 227 | * - The IO cannot cross group boundary. |
| 228 | * - In raid5/6 The end of the IO must align at end of a stripe eg. |
| 229 | * (@offset + @length) % strip_size == 0. Or the complete range is within a |
| 230 | * single stripe. |
| 231 | * - Memory condition only permitted a shorter IO. (A user can use @length=~0 |
| 232 | * And check the returned ios->length for max_io_size.) |
| 233 | * |
| 234 | * The caller must check returned ios->length (and/or ios->nr_pages) and |
| 235 | * re-issue these pages that fall outside of ios->length |
| 236 | */ |
| 237 | int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc, |
| 238 | bool is_reading, u64 offset, u64 length, |
| 239 | struct ore_io_state **pios) |
| 240 | { |
| 241 | struct ore_io_state *ios; |
| 242 | unsigned numdevs = layout->group_width * layout->mirrors_p1; |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 243 | unsigned sgs_per_dev = 0, max_par_pages = 0; |
Boaz Harrosh | b916c5c | 2011-09-28 11:55:51 +0300 | [diff] [blame] | 244 | int ret; |
| 245 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 246 | if (layout->parity && length) { |
| 247 | unsigned data_devs = layout->group_width - layout->parity; |
| 248 | unsigned stripe_size = layout->stripe_unit * data_devs; |
| 249 | unsigned pages_in_unit = layout->stripe_unit / PAGE_SIZE; |
| 250 | u32 remainder; |
| 251 | u64 num_stripes; |
| 252 | u64 num_raid_units; |
| 253 | |
| 254 | num_stripes = div_u64_rem(length, stripe_size, &remainder); |
| 255 | if (remainder) |
| 256 | ++num_stripes; |
| 257 | |
| 258 | num_raid_units = num_stripes * layout->parity; |
| 259 | |
| 260 | if (is_reading) { |
| 261 | /* For reads add per_dev sglist array */ |
| 262 | /* TODO: Raid 6 we need twice more. Actually: |
| 263 | * num_stripes / LCMdP(W,P); |
| 264 | * if (W%P != 0) num_stripes *= parity; |
| 265 | */ |
| 266 | |
| 267 | /* first/last seg is split */ |
| 268 | num_raid_units += layout->group_width; |
Boaz Harrosh | 361aba5 | 2011-12-28 19:14:23 +0200 | [diff] [blame] | 269 | sgs_per_dev = div_u64(num_raid_units, data_devs) + 2; |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 270 | } else { |
| 271 | /* For Writes add parity pages array. */ |
| 272 | max_par_pages = num_raid_units * pages_in_unit * |
| 273 | sizeof(struct page *); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | ret = _ore_get_io_state(layout, oc, numdevs, sgs_per_dev, max_par_pages, |
| 278 | pios); |
Boaz Harrosh | b916c5c | 2011-09-28 11:55:51 +0300 | [diff] [blame] | 279 | if (unlikely(ret)) |
| 280 | return ret; |
| 281 | |
| 282 | ios = *pios; |
| 283 | ios->reading = is_reading; |
| 284 | ios->offset = offset; |
| 285 | |
| 286 | if (length) { |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 287 | ore_calc_stripe_info(layout, offset, length, &ios->si); |
| 288 | ios->length = ios->si.length; |
Boaz Harrosh | aad560b | 2013-11-21 17:58:08 +0200 | [diff] [blame] | 289 | ios->nr_pages = ((ios->offset & (PAGE_SIZE - 1)) + |
| 290 | ios->length + PAGE_SIZE - 1) / PAGE_SIZE; |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 291 | if (layout->parity) |
| 292 | _ore_post_alloc_raid_stuff(ios); |
Boaz Harrosh | b916c5c | 2011-09-28 11:55:51 +0300 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 297 | EXPORT_SYMBOL(ore_get_rw_state); |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 298 | |
Boaz Harrosh | b916c5c | 2011-09-28 11:55:51 +0300 | [diff] [blame] | 299 | /* Allocate an io_state for all the devices in the comps array |
| 300 | * |
| 301 | * This version of io_state allocation is used mostly by create/remove |
| 302 | * and trunc where we currently need all the devices. The only wastful |
| 303 | * bit is the read/write_attributes with no IO. Those sites should |
| 304 | * be converted to use ore_get_rw_state() with length=0 |
| 305 | */ |
Boaz Harrosh | 5bf696d | 2011-09-28 11:39:59 +0300 | [diff] [blame] | 306 | int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc, |
Boaz Harrosh | b916c5c | 2011-09-28 11:55:51 +0300 | [diff] [blame] | 307 | struct ore_io_state **pios) |
Boaz Harrosh | e1042ba | 2010-11-16 20:09:58 +0200 | [diff] [blame] | 308 | { |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 309 | return _ore_get_io_state(layout, oc, oc->numdevs, 0, 0, pios); |
Boaz Harrosh | e1042ba | 2010-11-16 20:09:58 +0200 | [diff] [blame] | 310 | } |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 311 | EXPORT_SYMBOL(ore_get_io_state); |
Boaz Harrosh | e1042ba | 2010-11-16 20:09:58 +0200 | [diff] [blame] | 312 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 313 | void ore_put_io_state(struct ore_io_state *ios) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 314 | { |
| 315 | if (ios) { |
| 316 | unsigned i; |
| 317 | |
| 318 | for (i = 0; i < ios->numdevs; i++) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 319 | struct ore_per_dev_state *per_dev = &ios->per_dev[i]; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 320 | |
| 321 | if (per_dev->or) |
| 322 | osd_end_request(per_dev->or); |
| 323 | if (per_dev->bio) |
| 324 | bio_put(per_dev->bio); |
| 325 | } |
| 326 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 327 | _ore_free_raid_stuff(ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 328 | kfree(ios); |
| 329 | } |
| 330 | } |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 331 | EXPORT_SYMBOL(ore_put_io_state); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 332 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 333 | static void _sync_done(struct ore_io_state *ios, void *p) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 334 | { |
| 335 | struct completion *waiting = p; |
| 336 | |
| 337 | complete(waiting); |
| 338 | } |
| 339 | |
| 340 | static void _last_io(struct kref *kref) |
| 341 | { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 342 | struct ore_io_state *ios = container_of( |
| 343 | kref, struct ore_io_state, kref); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 344 | |
| 345 | ios->done(ios, ios->private); |
| 346 | } |
| 347 | |
| 348 | static void _done_io(struct osd_request *or, void *p) |
| 349 | { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 350 | struct ore_io_state *ios = p; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 351 | |
| 352 | kref_put(&ios->kref, _last_io); |
| 353 | } |
| 354 | |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 355 | int ore_io_execute(struct ore_io_state *ios) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 356 | { |
| 357 | DECLARE_COMPLETION_ONSTACK(wait); |
| 358 | bool sync = (ios->done == NULL); |
| 359 | int i, ret; |
| 360 | |
| 361 | if (sync) { |
| 362 | ios->done = _sync_done; |
| 363 | ios->private = &wait; |
| 364 | } |
| 365 | |
| 366 | for (i = 0; i < ios->numdevs; i++) { |
| 367 | struct osd_request *or = ios->per_dev[i].or; |
| 368 | if (unlikely(!or)) |
| 369 | continue; |
| 370 | |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 371 | ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 372 | if (unlikely(ret)) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 373 | ORE_DBGMSG("Failed to osd_finalize_request() => %d\n", |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 374 | ret); |
| 375 | return ret; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | kref_init(&ios->kref); |
| 380 | |
| 381 | for (i = 0; i < ios->numdevs; i++) { |
| 382 | struct osd_request *or = ios->per_dev[i].or; |
| 383 | if (unlikely(!or)) |
| 384 | continue; |
| 385 | |
| 386 | kref_get(&ios->kref); |
| 387 | osd_execute_request_async(or, _done_io, ios); |
| 388 | } |
| 389 | |
| 390 | kref_put(&ios->kref, _last_io); |
| 391 | ret = 0; |
| 392 | |
| 393 | if (sync) { |
| 394 | wait_for_completion(&wait); |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 395 | ret = ore_check_io(ios, NULL); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 396 | } |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 397 | return ret; |
| 398 | } |
| 399 | |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 400 | static void _clear_bio(struct bio *bio) |
| 401 | { |
| 402 | struct bio_vec *bv; |
| 403 | unsigned i; |
| 404 | |
Kent Overstreet | d74c6d5 | 2013-02-06 12:23:11 -0800 | [diff] [blame] | 405 | bio_for_each_segment_all(bv, bio, i) { |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 406 | unsigned this_count = bv->bv_len; |
| 407 | |
| 408 | if (likely(PAGE_SIZE == this_count)) |
| 409 | clear_highpage(bv->bv_page); |
| 410 | else |
| 411 | zero_user(bv->bv_page, bv->bv_offset, this_count); |
| 412 | } |
| 413 | } |
| 414 | |
Boaz Harrosh | 4b46c9f | 2011-09-28 13:25:50 +0300 | [diff] [blame] | 415 | int ore_check_io(struct ore_io_state *ios, ore_on_dev_error on_dev_error) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 416 | { |
| 417 | enum osd_err_priority acumulated_osd_err = 0; |
| 418 | int acumulated_lin_err = 0; |
| 419 | int i; |
| 420 | |
| 421 | for (i = 0; i < ios->numdevs; i++) { |
| 422 | struct osd_sense_info osi; |
Boaz Harrosh | 4b46c9f | 2011-09-28 13:25:50 +0300 | [diff] [blame] | 423 | struct ore_per_dev_state *per_dev = &ios->per_dev[i]; |
| 424 | struct osd_request *or = per_dev->or; |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 425 | int ret; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 426 | |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 427 | if (unlikely(!or)) |
| 428 | continue; |
| 429 | |
| 430 | ret = osd_req_decode_sense(or, &osi); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 431 | if (likely(!ret)) |
| 432 | continue; |
| 433 | |
Boaz Harrosh | 2deb76d | 2014-01-13 19:10:40 +0200 | [diff] [blame] | 434 | if ((OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) && |
| 435 | per_dev->bio) { |
| 436 | /* start read offset passed endof file. |
| 437 | * Note: if we do not have bio it means read-attributes |
| 438 | * In this case we should return error to caller. |
| 439 | */ |
Boaz Harrosh | 4b46c9f | 2011-09-28 13:25:50 +0300 | [diff] [blame] | 440 | _clear_bio(per_dev->bio); |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 441 | ORE_DBGMSG("start read offset passed end of file " |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 442 | "offset=0x%llx, length=0x%llx\n", |
Boaz Harrosh | 4b46c9f | 2011-09-28 13:25:50 +0300 | [diff] [blame] | 443 | _LLU(per_dev->offset), |
| 444 | _LLU(per_dev->length)); |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 445 | |
| 446 | continue; /* we recovered */ |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 447 | } |
| 448 | |
Boaz Harrosh | 4b46c9f | 2011-09-28 13:25:50 +0300 | [diff] [blame] | 449 | if (on_dev_error) { |
| 450 | u64 residual = ios->reading ? |
| 451 | or->in.residual : or->out.residual; |
| 452 | u64 offset = (ios->offset + ios->length) - residual; |
Boaz Harrosh | ffefb8ea | 2011-12-27 19:23:36 +0200 | [diff] [blame] | 453 | unsigned dev = per_dev->dev - ios->oc->first_dev; |
| 454 | struct ore_dev *od = ios->oc->ods[dev]; |
Boaz Harrosh | 4b46c9f | 2011-09-28 13:25:50 +0300 | [diff] [blame] | 455 | |
Boaz Harrosh | ffefb8ea | 2011-12-27 19:23:36 +0200 | [diff] [blame] | 456 | on_dev_error(ios, od, dev, osi.osd_err_pri, |
Boaz Harrosh | 4b46c9f | 2011-09-28 13:25:50 +0300 | [diff] [blame] | 457 | offset, residual); |
| 458 | } |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 459 | if (osi.osd_err_pri >= acumulated_osd_err) { |
| 460 | acumulated_osd_err = osi.osd_err_pri; |
| 461 | acumulated_lin_err = ret; |
| 462 | } |
| 463 | } |
| 464 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 465 | return acumulated_lin_err; |
| 466 | } |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 467 | EXPORT_SYMBOL(ore_check_io); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 468 | |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 469 | /* |
| 470 | * L - logical offset into the file |
| 471 | * |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 472 | * D - number of Data devices |
| 473 | * D = group_width - parity |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 474 | * |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 475 | * U - The number of bytes in a stripe within a group |
| 476 | * U = stripe_unit * D |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 477 | * |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 478 | * T - The number of bytes striped within a group of component objects |
| 479 | * (before advancing to the next group) |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 480 | * T = U * group_depth |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 481 | * |
| 482 | * S - The number of bytes striped across all component objects |
| 483 | * before the pattern repeats |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 484 | * S = T * group_count |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 485 | * |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 486 | * M - The "major" (i.e., across all components) cycle number |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 487 | * M = L / S |
| 488 | * |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 489 | * G - Counts the groups from the beginning of the major cycle |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 490 | * G = (L - (M * S)) / T [or (L % S) / T] |
| 491 | * |
| 492 | * H - The byte offset within the group |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 493 | * H = (L - (M * S)) % T [or (L % S) % T] |
| 494 | * |
| 495 | * N - The "minor" (i.e., across the group) stripe number |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 496 | * N = H / U |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 497 | * |
| 498 | * C - The component index coresponding to L |
| 499 | * |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 500 | * C = (H - (N * U)) / stripe_unit + G * D |
| 501 | * [or (L % U) / stripe_unit + G * D] |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 502 | * |
| 503 | * O - The component offset coresponding to L |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 504 | * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 505 | * |
| 506 | * LCMdP – Parity cycle: Lowest Common Multiple of group_width, parity |
| 507 | * divide by parity |
| 508 | * LCMdP = lcm(group_width, parity) / parity |
| 509 | * |
| 510 | * R - The parity Rotation stripe |
| 511 | * (Note parity cycle always starts at a group's boundary) |
| 512 | * R = N % LCMdP |
| 513 | * |
| 514 | * I = the first parity device index |
| 515 | * I = (group_width + group_width - R*parity - parity) % group_width |
| 516 | * |
| 517 | * Craid - The component index Rotated |
| 518 | * Craid = (group_width + C - R*parity) % group_width |
| 519 | * (We add the group_width to avoid negative numbers modulo math) |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 520 | */ |
Boaz Harrosh | 611d7a5 | 2011-10-04 14:20:17 +0200 | [diff] [blame] | 521 | void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset, |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 522 | u64 length, struct ore_striping_info *si) |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 523 | { |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 524 | u32 stripe_unit = layout->stripe_unit; |
| 525 | u32 group_width = layout->group_width; |
| 526 | u64 group_depth = layout->group_depth; |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 527 | u32 parity = layout->parity; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 528 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 529 | u32 D = group_width - parity; |
| 530 | u32 U = D * stripe_unit; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 531 | u64 T = U * group_depth; |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 532 | u64 S = T * layout->group_count; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 533 | u64 M = div64_u64(file_offset, S); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 534 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 535 | /* |
| 536 | G = (L - (M * S)) / T |
| 537 | H = (L - (M * S)) % T |
| 538 | */ |
| 539 | u64 LmodS = file_offset - M * S; |
| 540 | u32 G = div64_u64(LmodS, T); |
| 541 | u64 H = LmodS - G * T; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 542 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 543 | u32 N = div_u64(H, U); |
Boaz Harrosh | aad560b | 2013-11-21 17:58:08 +0200 | [diff] [blame] | 544 | u32 Nlast; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 545 | |
| 546 | /* "H - (N * U)" is just "H % U" so it's bound to u32 */ |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 547 | u32 C = (u32)(H - (N * U)) / stripe_unit + G * group_width; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 548 | |
| 549 | div_u64_rem(file_offset, stripe_unit, &si->unit_off); |
| 550 | |
| 551 | si->obj_offset = si->unit_off + (N * stripe_unit) + |
| 552 | (M * group_depth * stripe_unit); |
| 553 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 554 | if (parity) { |
| 555 | u32 LCMdP = lcm(group_width, parity) / parity; |
| 556 | /* R = N % LCMdP; */ |
| 557 | u32 RxP = (N % LCMdP) * parity; |
| 558 | u32 first_dev = C - C % group_width; |
| 559 | |
| 560 | si->par_dev = (group_width + group_width - parity - RxP) % |
| 561 | group_width + first_dev; |
| 562 | si->dev = (group_width + C - RxP) % group_width + first_dev; |
| 563 | si->bytes_in_stripe = U; |
| 564 | si->first_stripe_start = M * S + G * T + N * U; |
| 565 | } else { |
| 566 | /* Make the math correct see _prepare_one_group */ |
| 567 | si->par_dev = group_width; |
| 568 | si->dev = C; |
| 569 | } |
| 570 | |
| 571 | si->dev *= layout->mirrors_p1; |
| 572 | si->par_dev *= layout->mirrors_p1; |
| 573 | si->offset = file_offset; |
| 574 | si->length = T - H; |
| 575 | if (si->length > length) |
| 576 | si->length = length; |
Boaz Harrosh | aad560b | 2013-11-21 17:58:08 +0200 | [diff] [blame] | 577 | |
| 578 | Nlast = div_u64(H + si->length + U - 1, U); |
| 579 | si->maxdevUnits = Nlast - N; |
| 580 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 581 | si->M = M; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 582 | } |
Boaz Harrosh | 611d7a5 | 2011-10-04 14:20:17 +0200 | [diff] [blame] | 583 | EXPORT_SYMBOL(ore_calc_stripe_info); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 584 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 585 | int _ore_add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg, |
| 586 | unsigned pgbase, struct page **pages, |
| 587 | struct ore_per_dev_state *per_dev, int cur_len) |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 588 | { |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 589 | unsigned pg = *cur_pg; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 590 | struct request_queue *q = |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 591 | osd_request_queue(_ios_od(ios, per_dev->dev)); |
Boaz Harrosh | bbf9a31 | 2011-08-26 21:04:52 -0700 | [diff] [blame] | 592 | unsigned len = cur_len; |
| 593 | int ret; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 594 | |
| 595 | if (per_dev->bio == NULL) { |
Boaz Harrosh | aad560b | 2013-11-21 17:58:08 +0200 | [diff] [blame] | 596 | unsigned bio_size; |
| 597 | |
| 598 | if (!ios->reading) { |
| 599 | bio_size = ios->si.maxdevUnits; |
| 600 | } else { |
| 601 | bio_size = (ios->si.maxdevUnits + 1) * |
| 602 | (ios->layout->group_width - ios->layout->parity) / |
| 603 | ios->layout->group_width; |
| 604 | } |
| 605 | bio_size *= (ios->layout->stripe_unit / PAGE_SIZE); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 606 | |
| 607 | per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size); |
| 608 | if (unlikely(!per_dev->bio)) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 609 | ORE_DBGMSG("Failed to allocate BIO size=%u\n", |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 610 | bio_size); |
Boaz Harrosh | bbf9a31 | 2011-08-26 21:04:52 -0700 | [diff] [blame] | 611 | ret = -ENOMEM; |
| 612 | goto out; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
| 616 | while (cur_len > 0) { |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 617 | unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len); |
| 618 | unsigned added_len; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 619 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 620 | cur_len -= pglen; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 621 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 622 | added_len = bio_add_pc_page(q, per_dev->bio, pages[pg], |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 623 | pglen, pgbase); |
Boaz Harrosh | bbf9a31 | 2011-08-26 21:04:52 -0700 | [diff] [blame] | 624 | if (unlikely(pglen != added_len)) { |
Boaz Harrosh | aad560b | 2013-11-21 17:58:08 +0200 | [diff] [blame] | 625 | /* If bi_vcnt == bi_max then this is a SW BUG */ |
| 626 | ORE_DBGMSG("Failed bio_add_pc_page bi_vcnt=0x%x " |
| 627 | "bi_max=0x%x BIO_MAX=0x%x cur_len=0x%x\n", |
| 628 | per_dev->bio->bi_vcnt, |
| 629 | per_dev->bio->bi_max_vecs, |
| 630 | BIO_MAX_PAGES_KMALLOC, cur_len); |
Boaz Harrosh | bbf9a31 | 2011-08-26 21:04:52 -0700 | [diff] [blame] | 631 | ret = -ENOMEM; |
| 632 | goto out; |
| 633 | } |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 634 | _add_stripe_page(ios->sp2d, &ios->si, pages[pg]); |
| 635 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 636 | pgbase = 0; |
| 637 | ++pg; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 638 | } |
| 639 | BUG_ON(cur_len); |
| 640 | |
Boaz Harrosh | bbf9a31 | 2011-08-26 21:04:52 -0700 | [diff] [blame] | 641 | per_dev->length += len; |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 642 | *cur_pg = pg; |
Boaz Harrosh | bbf9a31 | 2011-08-26 21:04:52 -0700 | [diff] [blame] | 643 | ret = 0; |
| 644 | out: /* we fail the complete unit on an error eg don't advance |
| 645 | * per_dev->length and cur_pg. This means that we might have a bigger |
| 646 | * bio than the CDB requested length (per_dev->length). That's fine |
| 647 | * only the oposite is fatal. |
| 648 | */ |
| 649 | return ret; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Boaz Harrosh | 9826075 | 2011-10-02 15:32:50 +0200 | [diff] [blame] | 652 | static int _prepare_for_striping(struct ore_io_state *ios) |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 653 | { |
Boaz Harrosh | 9826075 | 2011-10-02 15:32:50 +0200 | [diff] [blame] | 654 | struct ore_striping_info *si = &ios->si; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 655 | unsigned stripe_unit = ios->layout->stripe_unit; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 656 | unsigned mirrors_p1 = ios->layout->mirrors_p1; |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 657 | unsigned group_width = ios->layout->group_width; |
| 658 | unsigned devs_in_group = group_width * mirrors_p1; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 659 | unsigned dev = si->dev; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 660 | unsigned first_dev = dev - (dev % devs_in_group); |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 661 | unsigned dev_order; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 662 | unsigned cur_pg = ios->pages_consumed; |
Boaz Harrosh | 9826075 | 2011-10-02 15:32:50 +0200 | [diff] [blame] | 663 | u64 length = ios->length; |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 664 | int ret = 0; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 665 | |
Boaz Harrosh | 9826075 | 2011-10-02 15:32:50 +0200 | [diff] [blame] | 666 | if (!ios->pages) { |
Boaz Harrosh | 9826075 | 2011-10-02 15:32:50 +0200 | [diff] [blame] | 667 | ios->numdevs = ios->layout->mirrors_p1; |
| 668 | return 0; |
| 669 | } |
| 670 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 671 | BUG_ON(length > si->length); |
| 672 | |
| 673 | dev_order = _dev_order(devs_in_group, mirrors_p1, si->par_dev, dev); |
| 674 | si->cur_comp = dev_order; |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 675 | si->cur_pg = si->unit_off / PAGE_SIZE; |
Boaz Harrosh | 9826075 | 2011-10-02 15:32:50 +0200 | [diff] [blame] | 676 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 677 | while (length) { |
Boaz Harrosh | 101a642 | 2014-04-03 17:53:31 +0300 | [diff] [blame^] | 678 | struct ore_per_dev_state *per_dev = |
| 679 | &ios->per_dev[dev - first_dev]; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 680 | unsigned cur_len, page_off = 0; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 681 | |
| 682 | if (!per_dev->length) { |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 683 | per_dev->dev = dev; |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 684 | if (dev == si->dev) { |
| 685 | WARN_ON(dev == si->par_dev); |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 686 | per_dev->offset = si->obj_offset; |
| 687 | cur_len = stripe_unit - si->unit_off; |
| 688 | page_off = si->unit_off & ~PAGE_MASK; |
| 689 | BUG_ON(page_off && (page_off != ios->pgbase)); |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 690 | } else { |
| 691 | if (si->cur_comp > dev_order) |
| 692 | per_dev->offset = |
| 693 | si->obj_offset - si->unit_off; |
| 694 | else /* si->cur_comp < dev_order */ |
| 695 | per_dev->offset = |
| 696 | si->obj_offset + stripe_unit - |
| 697 | si->unit_off; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 698 | cur_len = stripe_unit; |
| 699 | } |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 700 | } else { |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 701 | cur_len = stripe_unit; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 702 | } |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 703 | if (cur_len >= length) |
| 704 | cur_len = length; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 705 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 706 | ret = _ore_add_stripe_unit(ios, &cur_pg, page_off, ios->pages, |
| 707 | per_dev, cur_len); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 708 | if (unlikely(ret)) |
| 709 | goto out; |
| 710 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 711 | length -= cur_len; |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 712 | |
Boaz Harrosh | 101a642 | 2014-04-03 17:53:31 +0300 | [diff] [blame^] | 713 | dev = ((dev + mirrors_p1) % devs_in_group) + first_dev; |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 714 | si->cur_comp = (si->cur_comp + 1) % group_width; |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 715 | if (unlikely((dev == si->par_dev) || (!length && ios->sp2d))) { |
| 716 | if (!length && ios->sp2d) { |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 717 | /* If we are writing and this is the very last |
| 718 | * stripe. then operate on parity dev. |
| 719 | */ |
| 720 | dev = si->par_dev; |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 721 | } |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 722 | per_dev = &ios->per_dev[dev - first_dev]; |
| 723 | if (!per_dev->length) { |
| 724 | /* Only/always the parity unit of the first |
| 725 | * stripe will be empty. So this is a chance to |
| 726 | * initialize the per_dev info. |
| 727 | */ |
| 728 | per_dev->dev = dev; |
| 729 | per_dev->offset = si->obj_offset - si->unit_off; |
| 730 | } |
| 731 | |
Boaz Harrosh | 101a642 | 2014-04-03 17:53:31 +0300 | [diff] [blame^] | 732 | /* In writes cur_len just means if it's the |
| 733 | * last one. See _ore_add_parity_unit. |
| 734 | */ |
| 735 | ret = _ore_add_parity_unit(ios, si, per_dev, |
| 736 | ios->sp2d ? length : cur_len); |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 737 | if (unlikely(ret)) |
| 738 | goto out; |
| 739 | |
| 740 | /* Rotate next par_dev backwards with wraping */ |
| 741 | si->par_dev = (devs_in_group + si->par_dev - |
| 742 | ios->layout->parity * mirrors_p1) % |
| 743 | devs_in_group + first_dev; |
| 744 | /* Next stripe, start fresh */ |
| 745 | si->cur_comp = 0; |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 746 | si->cur_pg = 0; |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 747 | } |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 748 | } |
| 749 | out: |
Boaz Harrosh | b916c5c | 2011-09-28 11:55:51 +0300 | [diff] [blame] | 750 | ios->numdevs = devs_in_group; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 751 | ios->pages_consumed = cur_pg; |
Boaz Harrosh | 62b62ad | 2012-06-08 04:30:40 +0300 | [diff] [blame] | 752 | return ret; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 755 | int ore_create(struct ore_io_state *ios) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 756 | { |
| 757 | int i, ret; |
| 758 | |
Boaz Harrosh | 5bf696d | 2011-09-28 11:39:59 +0300 | [diff] [blame] | 759 | for (i = 0; i < ios->oc->numdevs; i++) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 760 | struct osd_request *or; |
| 761 | |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 762 | or = osd_start_request(_ios_od(ios, i), GFP_KERNEL); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 763 | if (unlikely(!or)) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 764 | ORE_ERR("%s: osd_start_request failed\n", __func__); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 765 | ret = -ENOMEM; |
| 766 | goto out; |
| 767 | } |
| 768 | ios->per_dev[i].or = or; |
| 769 | ios->numdevs++; |
| 770 | |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 771 | osd_req_create_object(or, _ios_obj(ios, i)); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 772 | } |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 773 | ret = ore_io_execute(ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 774 | |
| 775 | out: |
| 776 | return ret; |
| 777 | } |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 778 | EXPORT_SYMBOL(ore_create); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 779 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 780 | int ore_remove(struct ore_io_state *ios) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 781 | { |
| 782 | int i, ret; |
| 783 | |
Boaz Harrosh | 5bf696d | 2011-09-28 11:39:59 +0300 | [diff] [blame] | 784 | for (i = 0; i < ios->oc->numdevs; i++) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 785 | struct osd_request *or; |
| 786 | |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 787 | or = osd_start_request(_ios_od(ios, i), GFP_KERNEL); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 788 | if (unlikely(!or)) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 789 | ORE_ERR("%s: osd_start_request failed\n", __func__); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 790 | ret = -ENOMEM; |
| 791 | goto out; |
| 792 | } |
| 793 | ios->per_dev[i].or = or; |
| 794 | ios->numdevs++; |
| 795 | |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 796 | osd_req_remove_object(or, _ios_obj(ios, i)); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 797 | } |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 798 | ret = ore_io_execute(ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 799 | |
| 800 | out: |
| 801 | return ret; |
| 802 | } |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 803 | EXPORT_SYMBOL(ore_remove); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 804 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 805 | static int _write_mirror(struct ore_io_state *ios, int cur_comp) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 806 | { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 807 | struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp]; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 808 | unsigned dev = ios->per_dev[cur_comp].dev; |
| 809 | unsigned last_comp = cur_comp + ios->layout->mirrors_p1; |
| 810 | int ret = 0; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 811 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 812 | if (ios->pages && !master_dev->length) |
| 813 | return 0; /* Just an empty slot */ |
| 814 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 815 | for (; cur_comp < last_comp; ++cur_comp, ++dev) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 816 | struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp]; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 817 | struct osd_request *or; |
| 818 | |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 819 | or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 820 | if (unlikely(!or)) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 821 | ORE_ERR("%s: osd_start_request failed\n", __func__); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 822 | ret = -ENOMEM; |
| 823 | goto out; |
| 824 | } |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 825 | per_dev->or = or; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 826 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 827 | if (ios->pages) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 828 | struct bio *bio; |
| 829 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 830 | if (per_dev != master_dev) { |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 831 | bio = bio_clone_kmalloc(master_dev->bio, |
| 832 | GFP_KERNEL); |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 833 | if (unlikely(!bio)) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 834 | ORE_DBGMSG( |
Paul Bolle | 426d310 | 2010-08-07 12:30:03 +0200 | [diff] [blame] | 835 | "Failed to allocate BIO size=%u\n", |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 836 | master_dev->bio->bi_max_vecs); |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 837 | ret = -ENOMEM; |
| 838 | goto out; |
| 839 | } |
| 840 | |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 841 | bio->bi_bdev = NULL; |
| 842 | bio->bi_next = NULL; |
Boaz Harrosh | 6851a5e | 2011-08-24 18:10:49 -0700 | [diff] [blame] | 843 | per_dev->offset = master_dev->offset; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 844 | per_dev->length = master_dev->length; |
| 845 | per_dev->bio = bio; |
| 846 | per_dev->dev = dev; |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 847 | } else { |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 848 | bio = master_dev->bio; |
| 849 | /* FIXME: bio_set_dir() */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 850 | bio->bi_rw |= REQ_WRITE; |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 851 | } |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 852 | |
Boaz Harrosh | 9e62bb4 | 2012-08-01 17:48:36 +0300 | [diff] [blame] | 853 | osd_req_write(or, _ios_obj(ios, cur_comp), |
| 854 | per_dev->offset, bio, per_dev->length); |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 855 | ORE_DBGMSG("write(0x%llx) offset=0x%llx " |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 856 | "length=0x%llx dev=%d\n", |
Boaz Harrosh | 9e62bb4 | 2012-08-01 17:48:36 +0300 | [diff] [blame] | 857 | _LLU(_ios_obj(ios, cur_comp)->id), |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 858 | _LLU(per_dev->offset), |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 859 | _LLU(per_dev->length), dev); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 860 | } else if (ios->kern_buff) { |
Boaz Harrosh | 6851a5e | 2011-08-24 18:10:49 -0700 | [diff] [blame] | 861 | per_dev->offset = ios->si.obj_offset; |
| 862 | per_dev->dev = ios->si.dev + dev; |
| 863 | |
| 864 | /* no cross device without page array */ |
| 865 | BUG_ON((ios->layout->group_width > 1) && |
| 866 | (ios->si.unit_off + ios->length > |
| 867 | ios->layout->stripe_unit)); |
| 868 | |
Boaz Harrosh | 9e62bb4 | 2012-08-01 17:48:36 +0300 | [diff] [blame] | 869 | ret = osd_req_write_kern(or, _ios_obj(ios, cur_comp), |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 870 | per_dev->offset, |
| 871 | ios->kern_buff, ios->length); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 872 | if (unlikely(ret)) |
| 873 | goto out; |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 874 | ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx " |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 875 | "length=0x%llx dev=%d\n", |
Boaz Harrosh | 9e62bb4 | 2012-08-01 17:48:36 +0300 | [diff] [blame] | 876 | _LLU(_ios_obj(ios, cur_comp)->id), |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 877 | _LLU(per_dev->offset), |
Boaz Harrosh | 6851a5e | 2011-08-24 18:10:49 -0700 | [diff] [blame] | 878 | _LLU(ios->length), per_dev->dev); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 879 | } else { |
Boaz Harrosh | 9e62bb4 | 2012-08-01 17:48:36 +0300 | [diff] [blame] | 880 | osd_req_set_attributes(or, _ios_obj(ios, cur_comp)); |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 881 | ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n", |
Boaz Harrosh | 9e62bb4 | 2012-08-01 17:48:36 +0300 | [diff] [blame] | 882 | _LLU(_ios_obj(ios, cur_comp)->id), |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 883 | ios->out_attr_len, dev); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | if (ios->out_attr) |
| 887 | osd_req_add_set_attr_list(or, ios->out_attr, |
| 888 | ios->out_attr_len); |
| 889 | |
| 890 | if (ios->in_attr) |
| 891 | osd_req_add_get_attr_list(or, ios->in_attr, |
| 892 | ios->in_attr_len); |
| 893 | } |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 894 | |
| 895 | out: |
| 896 | return ret; |
| 897 | } |
| 898 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 899 | int ore_write(struct ore_io_state *ios) |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 900 | { |
| 901 | int i; |
| 902 | int ret; |
| 903 | |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 904 | if (unlikely(ios->sp2d && !ios->r4w)) { |
| 905 | /* A library is attempting a RAID-write without providing |
| 906 | * a pages lock interface. |
| 907 | */ |
| 908 | WARN_ON_ONCE(1); |
| 909 | return -ENOTSUPP; |
| 910 | } |
| 911 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 912 | ret = _prepare_for_striping(ios); |
| 913 | if (unlikely(ret)) |
| 914 | return ret; |
| 915 | |
| 916 | for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 917 | ret = _write_mirror(ios, i); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 918 | if (unlikely(ret)) |
| 919 | return ret; |
| 920 | } |
| 921 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 922 | ret = ore_io_execute(ios); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 923 | return ret; |
| 924 | } |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 925 | EXPORT_SYMBOL(ore_write); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 926 | |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 927 | int _ore_read_mirror(struct ore_io_state *ios, unsigned cur_comp) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 928 | { |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 929 | struct osd_request *or; |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 930 | struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp]; |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 931 | struct osd_obj_id *obj = _ios_obj(ios, cur_comp); |
| 932 | unsigned first_dev = (unsigned)obj->id; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 933 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 934 | if (ios->pages && !per_dev->length) |
| 935 | return 0; /* Just an empty slot */ |
| 936 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 937 | first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1; |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 938 | or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL); |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 939 | if (unlikely(!or)) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 940 | ORE_ERR("%s: osd_start_request failed\n", __func__); |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 941 | return -ENOMEM; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 942 | } |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 943 | per_dev->or = or; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 944 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 945 | if (ios->pages) { |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 946 | if (per_dev->cur_sg) { |
| 947 | /* finalize the last sg_entry */ |
| 948 | _ore_add_sg_seg(per_dev, 0, false); |
| 949 | if (unlikely(!per_dev->cur_sg)) |
| 950 | return 0; /* Skip parity only device */ |
| 951 | |
| 952 | osd_req_read_sg(or, obj, per_dev->bio, |
| 953 | per_dev->sglist, per_dev->cur_sg); |
| 954 | } else { |
| 955 | /* The no raid case */ |
| 956 | osd_req_read(or, obj, per_dev->offset, |
| 957 | per_dev->bio, per_dev->length); |
| 958 | } |
| 959 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 960 | ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx" |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 961 | " dev=%d sg_len=%d\n", _LLU(obj->id), |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 962 | _LLU(per_dev->offset), _LLU(per_dev->length), |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 963 | first_dev, per_dev->cur_sg); |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 964 | } else { |
Boaz Harrosh | 6851a5e | 2011-08-24 18:10:49 -0700 | [diff] [blame] | 965 | BUG_ON(ios->kern_buff); |
| 966 | |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 967 | osd_req_get_attributes(or, obj); |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 968 | ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n", |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 969 | _LLU(obj->id), |
| 970 | ios->in_attr_len, first_dev); |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 971 | } |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 972 | if (ios->out_attr) |
| 973 | osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len); |
| 974 | |
| 975 | if (ios->in_attr) |
| 976 | osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len); |
| 977 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 978 | return 0; |
| 979 | } |
| 980 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 981 | int ore_read(struct ore_io_state *ios) |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 982 | { |
| 983 | int i; |
| 984 | int ret; |
| 985 | |
| 986 | ret = _prepare_for_striping(ios); |
| 987 | if (unlikely(ret)) |
| 988 | return ret; |
| 989 | |
| 990 | for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) { |
Boaz Harrosh | 769ba8d | 2011-10-14 15:33:51 +0200 | [diff] [blame] | 991 | ret = _ore_read_mirror(ios, i); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 992 | if (unlikely(ret)) |
| 993 | return ret; |
| 994 | } |
| 995 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 996 | ret = ore_io_execute(ios); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 997 | return ret; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 998 | } |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 999 | EXPORT_SYMBOL(ore_read); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1000 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 1001 | int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr) |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 1002 | { |
| 1003 | struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */ |
| 1004 | void *iter = NULL; |
| 1005 | int nelem; |
| 1006 | |
| 1007 | do { |
| 1008 | nelem = 1; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1009 | osd_req_decode_get_attr_list(ios->per_dev[0].or, |
| 1010 | &cur_attr, &nelem, &iter); |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 1011 | if ((cur_attr.attr_page == attr->attr_page) && |
| 1012 | (cur_attr.attr_id == attr->attr_id)) { |
| 1013 | attr->len = cur_attr.len; |
| 1014 | attr->val_ptr = cur_attr.val_ptr; |
| 1015 | return 0; |
| 1016 | } |
| 1017 | } while (iter); |
| 1018 | |
| 1019 | return -EIO; |
| 1020 | } |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 1021 | EXPORT_SYMBOL(extract_attr_from_ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1022 | |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 1023 | static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp, |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1024 | struct osd_attr *attr) |
| 1025 | { |
| 1026 | int last_comp = cur_comp + ios->layout->mirrors_p1; |
| 1027 | |
| 1028 | for (; cur_comp < last_comp; ++cur_comp) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 1029 | struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp]; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1030 | struct osd_request *or; |
| 1031 | |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 1032 | or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1033 | if (unlikely(!or)) { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 1034 | ORE_ERR("%s: osd_start_request failed\n", __func__); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1035 | return -ENOMEM; |
| 1036 | } |
| 1037 | per_dev->or = or; |
| 1038 | |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 1039 | osd_req_set_attributes(or, _ios_obj(ios, cur_comp)); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1040 | osd_req_add_set_attr_list(or, attr, 1); |
| 1041 | } |
| 1042 | |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 1046 | struct _trunc_info { |
Boaz Harrosh | eb507bc | 2011-08-10 14:17:28 -0700 | [diff] [blame] | 1047 | struct ore_striping_info si; |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 1048 | u64 prev_group_obj_off; |
| 1049 | u64 next_group_obj_off; |
| 1050 | |
| 1051 | unsigned first_group_dev; |
| 1052 | unsigned nex_group_dev; |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 1053 | }; |
| 1054 | |
H Hartley Sweeten | 1958c7c2 | 2011-09-23 13:42:43 -0700 | [diff] [blame] | 1055 | static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset, |
| 1056 | struct _trunc_info *ti) |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 1057 | { |
| 1058 | unsigned stripe_unit = layout->stripe_unit; |
| 1059 | |
Boaz Harrosh | a1fec1d | 2011-10-12 18:42:22 +0200 | [diff] [blame] | 1060 | ore_calc_stripe_info(layout, file_offset, 0, &ti->si); |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 1061 | |
| 1062 | ti->prev_group_obj_off = ti->si.M * stripe_unit; |
| 1063 | ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0; |
| 1064 | |
| 1065 | ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width); |
| 1066 | ti->nex_group_dev = ti->first_group_dev + layout->group_width; |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Boaz Harrosh | 5bf696d | 2011-09-28 11:39:59 +0300 | [diff] [blame] | 1069 | int ore_truncate(struct ore_layout *layout, struct ore_components *oc, |
Boaz Harrosh | 9e9db45 | 2011-08-05 15:06:04 -0700 | [diff] [blame] | 1070 | u64 size) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1071 | { |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 1072 | struct ore_io_state *ios; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1073 | struct exofs_trunc_attr { |
| 1074 | struct osd_attr attr; |
| 1075 | __be64 newsize; |
| 1076 | } *size_attrs; |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 1077 | struct _trunc_info ti; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1078 | int i, ret; |
| 1079 | |
Boaz Harrosh | 5bf696d | 2011-09-28 11:39:59 +0300 | [diff] [blame] | 1080 | ret = ore_get_io_state(layout, oc, &ios); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1081 | if (unlikely(ret)) |
| 1082 | return ret; |
| 1083 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 1084 | _calc_trunk_info(ios->layout, size, &ti); |
| 1085 | |
Boaz Harrosh | b916c5c | 2011-09-28 11:55:51 +0300 | [diff] [blame] | 1086 | size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs), |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1087 | GFP_KERNEL); |
| 1088 | if (unlikely(!size_attrs)) { |
| 1089 | ret = -ENOMEM; |
| 1090 | goto out; |
| 1091 | } |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1092 | |
Boaz Harrosh | 5bf696d | 2011-09-28 11:39:59 +0300 | [diff] [blame] | 1093 | ios->numdevs = ios->oc->numdevs; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1094 | |
Boaz Harrosh | b916c5c | 2011-09-28 11:55:51 +0300 | [diff] [blame] | 1095 | for (i = 0; i < ios->numdevs; ++i) { |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1096 | struct exofs_trunc_attr *size_attr = &size_attrs[i]; |
| 1097 | u64 obj_size; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1098 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 1099 | if (i < ti.first_group_dev) |
| 1100 | obj_size = ti.prev_group_obj_off; |
| 1101 | else if (i >= ti.nex_group_dev) |
| 1102 | obj_size = ti.next_group_obj_off; |
| 1103 | else if (i < ti.si.dev) /* dev within this group */ |
| 1104 | obj_size = ti.si.obj_offset + |
| 1105 | ios->layout->stripe_unit - ti.si.unit_off; |
| 1106 | else if (i == ti.si.dev) |
| 1107 | obj_size = ti.si.obj_offset; |
| 1108 | else /* i > ti.dev */ |
| 1109 | obj_size = ti.si.obj_offset - ti.si.unit_off; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1110 | |
| 1111 | size_attr->newsize = cpu_to_be64(obj_size); |
| 1112 | size_attr->attr = g_attr_logical_length; |
| 1113 | size_attr->attr.val_ptr = &size_attr->newsize; |
| 1114 | |
Boaz Harrosh | aad560b | 2013-11-21 17:58:08 +0200 | [diff] [blame] | 1115 | ORE_DBGMSG2("trunc(0x%llx) obj_offset=0x%llx dev=%d\n", |
Boaz Harrosh | 5bf696d | 2011-09-28 11:39:59 +0300 | [diff] [blame] | 1116 | _LLU(oc->comps->obj.id), _LLU(obj_size), i); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1117 | ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1, |
| 1118 | &size_attr->attr); |
| 1119 | if (unlikely(ret)) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1120 | goto out; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1121 | } |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 1122 | ret = ore_io_execute(ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1123 | |
| 1124 | out: |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 1125 | kfree(size_attrs); |
Boaz Harrosh | 8ff660a | 2011-08-06 19:26:31 -0700 | [diff] [blame] | 1126 | ore_put_io_state(ios); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 1127 | return ret; |
| 1128 | } |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 1129 | EXPORT_SYMBOL(ore_truncate); |
Boaz Harrosh | 85e44df | 2011-05-16 15:26:47 +0300 | [diff] [blame] | 1130 | |
| 1131 | const struct osd_attr g_attr_logical_length = ATTR_DEF( |
| 1132 | OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8); |
Boaz Harrosh | cf283ad | 2011-08-06 19:22:06 -0700 | [diff] [blame] | 1133 | EXPORT_SYMBOL(g_attr_logical_length); |