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