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 | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 26 | #include <scsi/scsi_device.h> |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 27 | #include <asm/div64.h> |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 28 | |
| 29 | #include "exofs.h" |
| 30 | |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 31 | #define EXOFS_DBGMSG2(M...) do {} while (0) |
| 32 | /* #define EXOFS_DBGMSG2 EXOFS_DBGMSG */ |
| 33 | |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 34 | void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj) |
| 35 | { |
| 36 | osd_sec_init_nosec_doall_caps(cred_a, obj, false, true); |
| 37 | } |
| 38 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 39 | int exofs_read_kern(struct osd_dev *od, u8 *cred, struct osd_obj_id *obj, |
| 40 | u64 offset, void *p, unsigned length) |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 41 | { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 42 | struct osd_request *or = osd_start_request(od, GFP_KERNEL); |
| 43 | /* struct osd_sense_info osi = {.key = 0};*/ |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 44 | int ret; |
| 45 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 46 | if (unlikely(!or)) { |
| 47 | EXOFS_DBGMSG("%s: osd_start_request failed.\n", __func__); |
| 48 | return -ENOMEM; |
| 49 | } |
| 50 | ret = osd_req_read_kern(or, obj, offset, p, length); |
| 51 | if (unlikely(ret)) { |
| 52 | EXOFS_DBGMSG("%s: osd_req_read_kern failed.\n", __func__); |
| 53 | goto out; |
| 54 | } |
| 55 | |
| 56 | ret = osd_finalize_request(or, 0, cred, NULL); |
| 57 | if (unlikely(ret)) { |
Paul Bolle | 426d310 | 2010-08-07 12:30:03 +0200 | [diff] [blame] | 58 | EXOFS_DBGMSG("Failed to osd_finalize_request() => %d\n", ret); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 59 | goto out; |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ret = osd_execute_request(or); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 63 | if (unlikely(ret)) |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 64 | EXOFS_DBGMSG("osd_execute_request() => %d\n", ret); |
| 65 | /* osd_req_decode_sense(or, ret); */ |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 66 | |
| 67 | out: |
| 68 | osd_end_request(or); |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | |
Boaz Harrosh | e1042ba | 2010-11-16 20:09:58 +0200 | [diff] [blame^] | 72 | int exofs_get_rw_state(struct exofs_layout *layout, bool is_reading, |
| 73 | u64 offset, u64 length, struct exofs_io_state **pios) |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 74 | { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 75 | struct exofs_io_state *ios; |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 76 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 77 | /*TODO: Maybe use kmem_cach per sbi of size |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame] | 78 | * exofs_io_state_size(layout->s_numdevs) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 79 | */ |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame] | 80 | ios = kzalloc(exofs_io_state_size(layout->s_numdevs), GFP_KERNEL); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 81 | if (unlikely(!ios)) { |
Paul Bolle | 426d310 | 2010-08-07 12:30:03 +0200 | [diff] [blame] | 82 | EXOFS_DBGMSG("Failed kzalloc bytes=%d\n", |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame] | 83 | exofs_io_state_size(layout->s_numdevs)); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 84 | *pios = NULL; |
| 85 | return -ENOMEM; |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame] | 88 | ios->layout = layout; |
| 89 | ios->obj.partition = layout->s_pid; |
Boaz Harrosh | e1042ba | 2010-11-16 20:09:58 +0200 | [diff] [blame^] | 90 | ios->offset = offset; |
| 91 | ios->length = length; |
| 92 | ios->reading = is_reading; |
| 93 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 94 | *pios = ios; |
| 95 | return 0; |
| 96 | } |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 97 | |
Boaz Harrosh | e1042ba | 2010-11-16 20:09:58 +0200 | [diff] [blame^] | 98 | int exofs_get_io_state(struct exofs_layout *layout, |
| 99 | struct exofs_io_state **ios) |
| 100 | { |
| 101 | return exofs_get_rw_state(layout, true, 0, 0, ios); |
| 102 | } |
| 103 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 104 | void exofs_put_io_state(struct exofs_io_state *ios) |
| 105 | { |
| 106 | if (ios) { |
| 107 | unsigned i; |
| 108 | |
| 109 | for (i = 0; i < ios->numdevs; i++) { |
| 110 | struct exofs_per_dev_state *per_dev = &ios->per_dev[i]; |
| 111 | |
| 112 | if (per_dev->or) |
| 113 | osd_end_request(per_dev->or); |
| 114 | if (per_dev->bio) |
| 115 | bio_put(per_dev->bio); |
| 116 | } |
| 117 | |
| 118 | kfree(ios); |
| 119 | } |
| 120 | } |
| 121 | |
Boaz Harrosh | d9c740d | 2010-01-28 11:58:08 +0200 | [diff] [blame] | 122 | unsigned exofs_layout_od_id(struct exofs_layout *layout, |
| 123 | osd_id obj_no, unsigned layout_index) |
| 124 | { |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 125 | /* switch (layout->lay_func) { |
| 126 | case LAYOUT_MOVING_WINDOW: |
| 127 | {*/ |
| 128 | unsigned dev_mod = obj_no; |
| 129 | |
| 130 | return (layout_index + dev_mod * layout->mirrors_p1) % |
| 131 | layout->s_numdevs; |
| 132 | /* } |
| 133 | case LAYOUT_FUNC_IMPLICT: |
| 134 | return layout->devs[layout_index]; |
| 135 | }*/ |
Boaz Harrosh | d9c740d | 2010-01-28 11:58:08 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static inline struct osd_dev *exofs_ios_od(struct exofs_io_state *ios, |
| 139 | unsigned layout_index) |
| 140 | { |
| 141 | return ios->layout->s_ods[ |
| 142 | exofs_layout_od_id(ios->layout, ios->obj.id, layout_index)]; |
| 143 | } |
| 144 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 145 | static void _sync_done(struct exofs_io_state *ios, void *p) |
| 146 | { |
| 147 | struct completion *waiting = p; |
| 148 | |
| 149 | complete(waiting); |
| 150 | } |
| 151 | |
| 152 | static void _last_io(struct kref *kref) |
| 153 | { |
| 154 | struct exofs_io_state *ios = container_of( |
| 155 | kref, struct exofs_io_state, kref); |
| 156 | |
| 157 | ios->done(ios, ios->private); |
| 158 | } |
| 159 | |
| 160 | static void _done_io(struct osd_request *or, void *p) |
| 161 | { |
| 162 | struct exofs_io_state *ios = p; |
| 163 | |
| 164 | kref_put(&ios->kref, _last_io); |
| 165 | } |
| 166 | |
| 167 | static int exofs_io_execute(struct exofs_io_state *ios) |
| 168 | { |
| 169 | DECLARE_COMPLETION_ONSTACK(wait); |
| 170 | bool sync = (ios->done == NULL); |
| 171 | int i, ret; |
| 172 | |
| 173 | if (sync) { |
| 174 | ios->done = _sync_done; |
| 175 | ios->private = &wait; |
| 176 | } |
| 177 | |
| 178 | for (i = 0; i < ios->numdevs; i++) { |
| 179 | struct osd_request *or = ios->per_dev[i].or; |
| 180 | if (unlikely(!or)) |
| 181 | continue; |
| 182 | |
| 183 | ret = osd_finalize_request(or, 0, ios->cred, NULL); |
| 184 | if (unlikely(ret)) { |
Paul Bolle | 426d310 | 2010-08-07 12:30:03 +0200 | [diff] [blame] | 185 | EXOFS_DBGMSG("Failed to osd_finalize_request() => %d\n", |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 186 | ret); |
| 187 | return ret; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | kref_init(&ios->kref); |
| 192 | |
| 193 | for (i = 0; i < ios->numdevs; i++) { |
| 194 | struct osd_request *or = ios->per_dev[i].or; |
| 195 | if (unlikely(!or)) |
| 196 | continue; |
| 197 | |
| 198 | kref_get(&ios->kref); |
| 199 | osd_execute_request_async(or, _done_io, ios); |
| 200 | } |
| 201 | |
| 202 | kref_put(&ios->kref, _last_io); |
| 203 | ret = 0; |
| 204 | |
| 205 | if (sync) { |
| 206 | wait_for_completion(&wait); |
| 207 | ret = exofs_check_io(ios, NULL); |
| 208 | } |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 209 | return ret; |
| 210 | } |
| 211 | |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 212 | static void _clear_bio(struct bio *bio) |
| 213 | { |
| 214 | struct bio_vec *bv; |
| 215 | unsigned i; |
| 216 | |
| 217 | __bio_for_each_segment(bv, bio, i, 0) { |
| 218 | unsigned this_count = bv->bv_len; |
| 219 | |
| 220 | if (likely(PAGE_SIZE == this_count)) |
| 221 | clear_highpage(bv->bv_page); |
| 222 | else |
| 223 | zero_user(bv->bv_page, bv->bv_offset, this_count); |
| 224 | } |
| 225 | } |
| 226 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 227 | int exofs_check_io(struct exofs_io_state *ios, u64 *resid) |
| 228 | { |
| 229 | enum osd_err_priority acumulated_osd_err = 0; |
| 230 | int acumulated_lin_err = 0; |
| 231 | int i; |
| 232 | |
| 233 | for (i = 0; i < ios->numdevs; i++) { |
| 234 | struct osd_sense_info osi; |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 235 | struct osd_request *or = ios->per_dev[i].or; |
| 236 | int ret; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 237 | |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 238 | if (unlikely(!or)) |
| 239 | continue; |
| 240 | |
| 241 | ret = osd_req_decode_sense(or, &osi); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 242 | if (likely(!ret)) |
| 243 | continue; |
| 244 | |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 245 | if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) { |
| 246 | /* start read offset passed endof file */ |
| 247 | _clear_bio(ios->per_dev[i].bio); |
| 248 | EXOFS_DBGMSG("start read offset passed end of file " |
| 249 | "offset=0x%llx, length=0x%llx\n", |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 250 | _LLU(ios->per_dev[i].offset), |
| 251 | _LLU(ios->per_dev[i].length)); |
Boaz Harrosh | 22ddc55 | 2010-01-19 19:24:45 +0200 | [diff] [blame] | 252 | |
| 253 | continue; /* we recovered */ |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if (osi.osd_err_pri >= acumulated_osd_err) { |
| 257 | acumulated_osd_err = osi.osd_err_pri; |
| 258 | acumulated_lin_err = ret; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* TODO: raid specific residual calculations */ |
| 263 | if (resid) { |
| 264 | if (likely(!acumulated_lin_err)) |
| 265 | *resid = 0; |
| 266 | else |
| 267 | *resid = ios->length; |
| 268 | } |
| 269 | |
| 270 | return acumulated_lin_err; |
| 271 | } |
| 272 | |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 273 | /* |
| 274 | * L - logical offset into the file |
| 275 | * |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 276 | * U - The number of bytes in a stripe within a group |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 277 | * |
| 278 | * U = stripe_unit * group_width |
| 279 | * |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 280 | * T - The number of bytes striped within a group of component objects |
| 281 | * (before advancing to the next group) |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 282 | * |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 283 | * T = stripe_unit * group_width * group_depth |
| 284 | * |
| 285 | * S - The number of bytes striped across all component objects |
| 286 | * before the pattern repeats |
| 287 | * |
| 288 | * S = stripe_unit * group_width * group_depth * group_count |
| 289 | * |
| 290 | * M - The "major" (i.e., across all components) stripe number |
| 291 | * |
| 292 | * M = L / S |
| 293 | * |
| 294 | * G - Counts the groups from the beginning of the major stripe |
| 295 | * |
| 296 | * G = (L - (M * S)) / T [or (L % S) / T] |
| 297 | * |
| 298 | * H - The byte offset within the group |
| 299 | * |
| 300 | * H = (L - (M * S)) % T [or (L % S) % T] |
| 301 | * |
| 302 | * N - The "minor" (i.e., across the group) stripe number |
| 303 | * |
| 304 | * N = H / U |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 305 | * |
| 306 | * C - The component index coresponding to L |
| 307 | * |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 308 | * C = (H - (N * U)) / stripe_unit + G * group_width |
| 309 | * [or (L % U) / stripe_unit + G * group_width] |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 310 | * |
| 311 | * O - The component offset coresponding to L |
| 312 | * |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 313 | * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 314 | */ |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 315 | struct _striping_info { |
| 316 | u64 obj_offset; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 317 | u64 group_length; |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 318 | u64 M; /* for truncate */ |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 319 | unsigned dev; |
| 320 | unsigned unit_off; |
| 321 | }; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 322 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 323 | static void _calc_stripe_info(struct exofs_layout *layout, u64 file_offset, |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 324 | struct _striping_info *si) |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 325 | { |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 326 | u32 stripe_unit = layout->stripe_unit; |
| 327 | u32 group_width = layout->group_width; |
| 328 | u64 group_depth = layout->group_depth; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 329 | |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 330 | u32 U = stripe_unit * group_width; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 331 | u64 T = U * group_depth; |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 332 | u64 S = T * layout->group_count; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 333 | u64 M = div64_u64(file_offset, S); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 334 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 335 | /* |
| 336 | G = (L - (M * S)) / T |
| 337 | H = (L - (M * S)) % T |
| 338 | */ |
| 339 | u64 LmodS = file_offset - M * S; |
| 340 | u32 G = div64_u64(LmodS, T); |
| 341 | u64 H = LmodS - G * T; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 342 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 343 | u32 N = div_u64(H, U); |
| 344 | |
| 345 | /* "H - (N * U)" is just "H % U" so it's bound to u32 */ |
| 346 | si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width; |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 347 | si->dev *= layout->mirrors_p1; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 348 | |
| 349 | div_u64_rem(file_offset, stripe_unit, &si->unit_off); |
| 350 | |
| 351 | si->obj_offset = si->unit_off + (N * stripe_unit) + |
| 352 | (M * group_depth * stripe_unit); |
| 353 | |
| 354 | si->group_length = T - H; |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 355 | si->M = M; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 358 | static int _add_stripe_unit(struct exofs_io_state *ios, unsigned *cur_pg, |
| 359 | unsigned pgbase, struct exofs_per_dev_state *per_dev, |
| 360 | int cur_len) |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 361 | { |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 362 | unsigned pg = *cur_pg; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 363 | struct request_queue *q = |
| 364 | osd_request_queue(exofs_ios_od(ios, per_dev->dev)); |
| 365 | |
| 366 | per_dev->length += cur_len; |
| 367 | |
| 368 | if (per_dev->bio == NULL) { |
| 369 | unsigned pages_in_stripe = ios->layout->group_width * |
| 370 | (ios->layout->stripe_unit / PAGE_SIZE); |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 371 | unsigned bio_size = (ios->nr_pages + pages_in_stripe) / |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 372 | ios->layout->group_width; |
| 373 | |
| 374 | per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size); |
| 375 | if (unlikely(!per_dev->bio)) { |
Paul Bolle | 426d310 | 2010-08-07 12:30:03 +0200 | [diff] [blame] | 376 | EXOFS_DBGMSG("Failed to allocate BIO size=%u\n", |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 377 | bio_size); |
| 378 | return -ENOMEM; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | while (cur_len > 0) { |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 383 | unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len); |
| 384 | unsigned added_len; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 385 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 386 | BUG_ON(ios->nr_pages <= pg); |
| 387 | cur_len -= pglen; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 388 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 389 | added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg], |
| 390 | pglen, pgbase); |
| 391 | if (unlikely(pglen != added_len)) |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 392 | return -ENOMEM; |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 393 | pgbase = 0; |
| 394 | ++pg; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 395 | } |
| 396 | BUG_ON(cur_len); |
| 397 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 398 | *cur_pg = pg; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 402 | static int _prepare_one_group(struct exofs_io_state *ios, u64 length, |
Boaz Harrosh | 6e31609 | 2010-07-29 17:08:13 +0300 | [diff] [blame] | 403 | struct _striping_info *si) |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 404 | { |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 405 | unsigned stripe_unit = ios->layout->stripe_unit; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 406 | unsigned mirrors_p1 = ios->layout->mirrors_p1; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 407 | unsigned devs_in_group = ios->layout->group_width * mirrors_p1; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 408 | unsigned dev = si->dev; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 409 | unsigned first_dev = dev - (dev % devs_in_group); |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 410 | unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0; |
| 411 | unsigned cur_pg = ios->pages_consumed; |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 412 | int ret = 0; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 413 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 414 | while (length) { |
Boaz Harrosh | 6e31609 | 2010-07-29 17:08:13 +0300 | [diff] [blame] | 415 | struct exofs_per_dev_state *per_dev = &ios->per_dev[dev]; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 416 | unsigned cur_len, page_off = 0; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 417 | |
| 418 | if (!per_dev->length) { |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 419 | per_dev->dev = dev; |
| 420 | if (dev < si->dev) { |
| 421 | per_dev->offset = si->obj_offset + stripe_unit - |
| 422 | si->unit_off; |
| 423 | cur_len = stripe_unit; |
| 424 | } else if (dev == si->dev) { |
| 425 | per_dev->offset = si->obj_offset; |
| 426 | cur_len = stripe_unit - si->unit_off; |
| 427 | page_off = si->unit_off & ~PAGE_MASK; |
| 428 | BUG_ON(page_off && (page_off != ios->pgbase)); |
| 429 | } else { /* dev > si->dev */ |
| 430 | per_dev->offset = si->obj_offset - si->unit_off; |
| 431 | cur_len = stripe_unit; |
| 432 | } |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 433 | |
Boaz Harrosh | 6e31609 | 2010-07-29 17:08:13 +0300 | [diff] [blame] | 434 | if (max_comp < dev) |
| 435 | max_comp = dev; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 436 | } else { |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 437 | cur_len = stripe_unit; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 438 | } |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 439 | if (cur_len >= length) |
| 440 | cur_len = length; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 441 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 442 | ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev, |
| 443 | cur_len); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 444 | if (unlikely(ret)) |
| 445 | goto out; |
| 446 | |
Boaz Harrosh | 6e31609 | 2010-07-29 17:08:13 +0300 | [diff] [blame] | 447 | dev += mirrors_p1; |
| 448 | dev = (dev % devs_in_group) + first_dev; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 449 | |
| 450 | length -= cur_len; |
| 451 | } |
| 452 | out: |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 453 | ios->numdevs = max_comp + mirrors_p1; |
| 454 | ios->pages_consumed = cur_pg; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 455 | return ret; |
| 456 | } |
| 457 | |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 458 | static int _prepare_for_striping(struct exofs_io_state *ios) |
| 459 | { |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 460 | u64 length = ios->length; |
Boaz Harrosh | 5002dd1 | 2010-08-02 20:06:46 +0300 | [diff] [blame] | 461 | u64 offset = ios->offset; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 462 | struct _striping_info si; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 463 | int ret = 0; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 464 | |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 465 | if (!ios->pages) { |
| 466 | if (ios->kern_buff) { |
| 467 | struct exofs_per_dev_state *per_dev = &ios->per_dev[0]; |
| 468 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 469 | _calc_stripe_info(ios->layout, ios->offset, &si); |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 470 | per_dev->offset = si.obj_offset; |
| 471 | per_dev->dev = si.dev; |
| 472 | |
| 473 | /* no cross device without page array */ |
| 474 | BUG_ON((ios->layout->group_width > 1) && |
| 475 | (si.unit_off + ios->length > |
| 476 | ios->layout->stripe_unit)); |
| 477 | } |
| 478 | ios->numdevs = ios->layout->mirrors_p1; |
| 479 | return 0; |
| 480 | } |
| 481 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 482 | while (length) { |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 483 | _calc_stripe_info(ios->layout, offset, &si); |
Boaz Harrosh | 5002dd1 | 2010-08-02 20:06:46 +0300 | [diff] [blame] | 484 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 485 | if (length < si.group_length) |
| 486 | si.group_length = length; |
| 487 | |
Boaz Harrosh | 6e31609 | 2010-07-29 17:08:13 +0300 | [diff] [blame] | 488 | ret = _prepare_one_group(ios, si.group_length, &si); |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 489 | if (unlikely(ret)) |
| 490 | goto out; |
| 491 | |
Boaz Harrosh | 5002dd1 | 2010-08-02 20:06:46 +0300 | [diff] [blame] | 492 | offset += si.group_length; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 493 | length -= si.group_length; |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | out: |
| 497 | return ret; |
Boaz Harrosh | b367e78 | 2010-02-07 19:18:58 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 500 | int exofs_sbi_create(struct exofs_io_state *ios) |
| 501 | { |
| 502 | int i, ret; |
| 503 | |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame] | 504 | for (i = 0; i < ios->layout->s_numdevs; i++) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 505 | struct osd_request *or; |
| 506 | |
Boaz Harrosh | d9c740d | 2010-01-28 11:58:08 +0200 | [diff] [blame] | 507 | or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 508 | if (unlikely(!or)) { |
| 509 | EXOFS_ERR("%s: osd_start_request failed\n", __func__); |
| 510 | ret = -ENOMEM; |
| 511 | goto out; |
| 512 | } |
| 513 | ios->per_dev[i].or = or; |
| 514 | ios->numdevs++; |
| 515 | |
| 516 | osd_req_create_object(or, &ios->obj); |
| 517 | } |
| 518 | ret = exofs_io_execute(ios); |
| 519 | |
| 520 | out: |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | int exofs_sbi_remove(struct exofs_io_state *ios) |
| 525 | { |
| 526 | int i, ret; |
| 527 | |
Boaz Harrosh | 45d3abc | 2010-01-28 11:46:16 +0200 | [diff] [blame] | 528 | for (i = 0; i < ios->layout->s_numdevs; i++) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 529 | struct osd_request *or; |
| 530 | |
Boaz Harrosh | d9c740d | 2010-01-28 11:58:08 +0200 | [diff] [blame] | 531 | or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 532 | if (unlikely(!or)) { |
| 533 | EXOFS_ERR("%s: osd_start_request failed\n", __func__); |
| 534 | ret = -ENOMEM; |
| 535 | goto out; |
| 536 | } |
| 537 | ios->per_dev[i].or = or; |
| 538 | ios->numdevs++; |
| 539 | |
| 540 | osd_req_remove_object(or, &ios->obj); |
| 541 | } |
| 542 | ret = exofs_io_execute(ios); |
| 543 | |
| 544 | out: |
| 545 | return ret; |
| 546 | } |
| 547 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 548 | static int _sbi_write_mirror(struct exofs_io_state *ios, int cur_comp) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 549 | { |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 550 | struct exofs_per_dev_state *master_dev = &ios->per_dev[cur_comp]; |
| 551 | unsigned dev = ios->per_dev[cur_comp].dev; |
| 552 | unsigned last_comp = cur_comp + ios->layout->mirrors_p1; |
| 553 | int ret = 0; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 554 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 555 | if (ios->pages && !master_dev->length) |
| 556 | return 0; /* Just an empty slot */ |
| 557 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 558 | for (; cur_comp < last_comp; ++cur_comp, ++dev) { |
| 559 | struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp]; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 560 | struct osd_request *or; |
| 561 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 562 | or = osd_start_request(exofs_ios_od(ios, dev), GFP_KERNEL); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 563 | if (unlikely(!or)) { |
| 564 | EXOFS_ERR("%s: osd_start_request failed\n", __func__); |
| 565 | ret = -ENOMEM; |
| 566 | goto out; |
| 567 | } |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 568 | per_dev->or = or; |
| 569 | per_dev->offset = master_dev->offset; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 570 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 571 | if (ios->pages) { |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 572 | struct bio *bio; |
| 573 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 574 | if (per_dev != master_dev) { |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 575 | bio = bio_kmalloc(GFP_KERNEL, |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 576 | master_dev->bio->bi_max_vecs); |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 577 | if (unlikely(!bio)) { |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 578 | EXOFS_DBGMSG( |
Paul Bolle | 426d310 | 2010-08-07 12:30:03 +0200 | [diff] [blame] | 579 | "Failed to allocate BIO size=%u\n", |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 580 | master_dev->bio->bi_max_vecs); |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 581 | ret = -ENOMEM; |
| 582 | goto out; |
| 583 | } |
| 584 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 585 | __bio_clone(bio, master_dev->bio); |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 586 | bio->bi_bdev = NULL; |
| 587 | bio->bi_next = NULL; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 588 | per_dev->length = master_dev->length; |
| 589 | per_dev->bio = bio; |
| 590 | per_dev->dev = dev; |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 591 | } else { |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 592 | bio = master_dev->bio; |
| 593 | /* FIXME: bio_set_dir() */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 594 | bio->bi_rw |= REQ_WRITE; |
Boaz Harrosh | 04dc1e8 | 2009-11-16 16:03:05 +0200 | [diff] [blame] | 595 | } |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 596 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 597 | osd_req_write(or, &ios->obj, per_dev->offset, bio, |
| 598 | per_dev->length); |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 599 | EXOFS_DBGMSG("write(0x%llx) offset=0x%llx " |
| 600 | "length=0x%llx dev=%d\n", |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 601 | _LLU(ios->obj.id), _LLU(per_dev->offset), |
| 602 | _LLU(per_dev->length), dev); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 603 | } else if (ios->kern_buff) { |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 604 | ret = osd_req_write_kern(or, &ios->obj, per_dev->offset, |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 605 | ios->kern_buff, ios->length); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 606 | if (unlikely(ret)) |
| 607 | goto out; |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 608 | EXOFS_DBGMSG2("write_kern(0x%llx) offset=0x%llx " |
| 609 | "length=0x%llx dev=%d\n", |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 610 | _LLU(ios->obj.id), _LLU(per_dev->offset), |
| 611 | _LLU(ios->length), dev); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 612 | } else { |
| 613 | osd_req_set_attributes(or, &ios->obj); |
Boaz Harrosh | 34ce4e7 | 2009-12-15 19:34:17 +0200 | [diff] [blame] | 614 | EXOFS_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n", |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 615 | _LLU(ios->obj.id), ios->out_attr_len, dev); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | if (ios->out_attr) |
| 619 | osd_req_add_set_attr_list(or, ios->out_attr, |
| 620 | ios->out_attr_len); |
| 621 | |
| 622 | if (ios->in_attr) |
| 623 | osd_req_add_get_attr_list(or, ios->in_attr, |
| 624 | ios->in_attr_len); |
| 625 | } |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 626 | |
| 627 | out: |
| 628 | return ret; |
| 629 | } |
| 630 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 631 | int exofs_sbi_write(struct exofs_io_state *ios) |
| 632 | { |
| 633 | int i; |
| 634 | int ret; |
| 635 | |
| 636 | ret = _prepare_for_striping(ios); |
| 637 | if (unlikely(ret)) |
| 638 | return ret; |
| 639 | |
| 640 | for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) { |
| 641 | ret = _sbi_write_mirror(ios, i); |
| 642 | if (unlikely(ret)) |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | ret = exofs_io_execute(ios); |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | static int _sbi_read_mirror(struct exofs_io_state *ios, unsigned cur_comp) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 651 | { |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 652 | struct osd_request *or; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 653 | struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp]; |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 654 | unsigned first_dev = (unsigned)ios->obj.id; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 655 | |
Boaz Harrosh | 50a76fd | 2010-02-11 13:01:39 +0200 | [diff] [blame] | 656 | if (ios->pages && !per_dev->length) |
| 657 | return 0; /* Just an empty slot */ |
| 658 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 659 | first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1; |
Boaz Harrosh | d9c740d | 2010-01-28 11:58:08 +0200 | [diff] [blame] | 660 | or = osd_start_request(exofs_ios_od(ios, first_dev), GFP_KERNEL); |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 661 | if (unlikely(!or)) { |
| 662 | EXOFS_ERR("%s: osd_start_request failed\n", __func__); |
| 663 | return -ENOMEM; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 664 | } |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 665 | per_dev->or = or; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 666 | |
Boaz Harrosh | 86093aa | 2010-01-28 18:24:06 +0200 | [diff] [blame] | 667 | if (ios->pages) { |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 668 | osd_req_read(or, &ios->obj, per_dev->offset, |
| 669 | per_dev->bio, per_dev->length); |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 670 | EXOFS_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx" |
| 671 | " dev=%d\n", _LLU(ios->obj.id), |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 672 | _LLU(per_dev->offset), _LLU(per_dev->length), |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 673 | first_dev); |
| 674 | } else if (ios->kern_buff) { |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 675 | int ret = osd_req_read_kern(or, &ios->obj, per_dev->offset, |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 676 | ios->kern_buff, ios->length); |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 677 | EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx " |
| 678 | "length=0x%llx dev=%d ret=>%d\n", |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 679 | _LLU(ios->obj.id), _LLU(per_dev->offset), |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 680 | _LLU(ios->length), first_dev, ret); |
| 681 | if (unlikely(ret)) |
| 682 | return ret; |
| 683 | } else { |
| 684 | osd_req_get_attributes(or, &ios->obj); |
| 685 | EXOFS_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n", |
| 686 | _LLU(ios->obj.id), ios->in_attr_len, first_dev); |
| 687 | } |
Boaz Harrosh | 46f4d97 | 2010-02-01 11:37:30 +0200 | [diff] [blame] | 688 | if (ios->out_attr) |
| 689 | osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len); |
| 690 | |
| 691 | if (ios->in_attr) |
| 692 | osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len); |
| 693 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | int exofs_sbi_read(struct exofs_io_state *ios) |
| 698 | { |
| 699 | int i; |
| 700 | int ret; |
| 701 | |
| 702 | ret = _prepare_for_striping(ios); |
| 703 | if (unlikely(ret)) |
| 704 | return ret; |
| 705 | |
| 706 | for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) { |
| 707 | ret = _sbi_read_mirror(ios, i); |
| 708 | if (unlikely(ret)) |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | ret = exofs_io_execute(ios); |
| 713 | return ret; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | int extract_attr_from_ios(struct exofs_io_state *ios, struct osd_attr *attr) |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 717 | { |
| 718 | struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */ |
| 719 | void *iter = NULL; |
| 720 | int nelem; |
| 721 | |
| 722 | do { |
| 723 | nelem = 1; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 724 | osd_req_decode_get_attr_list(ios->per_dev[0].or, |
| 725 | &cur_attr, &nelem, &iter); |
Boaz Harrosh | b14f8ab | 2008-10-27 18:27:55 +0200 | [diff] [blame] | 726 | if ((cur_attr.attr_page == attr->attr_page) && |
| 727 | (cur_attr.attr_id == attr->attr_id)) { |
| 728 | attr->len = cur_attr.len; |
| 729 | attr->val_ptr = cur_attr.val_ptr; |
| 730 | return 0; |
| 731 | } |
| 732 | } while (iter); |
| 733 | |
| 734 | return -EIO; |
| 735 | } |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 736 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 737 | static int _truncate_mirrors(struct exofs_io_state *ios, unsigned cur_comp, |
| 738 | struct osd_attr *attr) |
| 739 | { |
| 740 | int last_comp = cur_comp + ios->layout->mirrors_p1; |
| 741 | |
| 742 | for (; cur_comp < last_comp; ++cur_comp) { |
| 743 | struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp]; |
| 744 | struct osd_request *or; |
| 745 | |
| 746 | or = osd_start_request(exofs_ios_od(ios, cur_comp), GFP_KERNEL); |
| 747 | if (unlikely(!or)) { |
| 748 | EXOFS_ERR("%s: osd_start_request failed\n", __func__); |
| 749 | return -ENOMEM; |
| 750 | } |
| 751 | per_dev->or = or; |
| 752 | |
| 753 | osd_req_set_attributes(or, &ios->obj); |
| 754 | osd_req_add_set_attr_list(or, attr, 1); |
| 755 | } |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 760 | struct _trunc_info { |
| 761 | struct _striping_info si; |
| 762 | u64 prev_group_obj_off; |
| 763 | u64 next_group_obj_off; |
| 764 | |
| 765 | unsigned first_group_dev; |
| 766 | unsigned nex_group_dev; |
| 767 | unsigned max_devs; |
| 768 | }; |
| 769 | |
| 770 | void _calc_trunk_info(struct exofs_layout *layout, u64 file_offset, |
| 771 | struct _trunc_info *ti) |
| 772 | { |
| 773 | unsigned stripe_unit = layout->stripe_unit; |
| 774 | |
| 775 | _calc_stripe_info(layout, file_offset, &ti->si); |
| 776 | |
| 777 | ti->prev_group_obj_off = ti->si.M * stripe_unit; |
| 778 | ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0; |
| 779 | |
| 780 | ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width); |
| 781 | ti->nex_group_dev = ti->first_group_dev + layout->group_width; |
| 782 | ti->max_devs = layout->group_width * layout->group_count; |
| 783 | } |
| 784 | |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 785 | int exofs_oi_truncate(struct exofs_i_info *oi, u64 size) |
| 786 | { |
| 787 | struct exofs_sb_info *sbi = oi->vfs_inode.i_sb->s_fs_info; |
| 788 | struct exofs_io_state *ios; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 789 | struct exofs_trunc_attr { |
| 790 | struct osd_attr attr; |
| 791 | __be64 newsize; |
| 792 | } *size_attrs; |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 793 | struct _trunc_info ti; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 794 | int i, ret; |
| 795 | |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 796 | ret = exofs_get_io_state(&sbi->layout, &ios); |
| 797 | if (unlikely(ret)) |
| 798 | return ret; |
| 799 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 800 | _calc_trunk_info(ios->layout, size, &ti); |
| 801 | |
| 802 | size_attrs = kcalloc(ti.max_devs, sizeof(*size_attrs), |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 803 | GFP_KERNEL); |
| 804 | if (unlikely(!size_attrs)) { |
| 805 | ret = -ENOMEM; |
| 806 | goto out; |
| 807 | } |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 808 | |
| 809 | ios->obj.id = exofs_oi_objno(oi); |
| 810 | ios->cred = oi->i_cred; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 811 | ios->numdevs = ios->layout->s_numdevs; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 812 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 813 | for (i = 0; i < ti.max_devs; ++i) { |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 814 | struct exofs_trunc_attr *size_attr = &size_attrs[i]; |
| 815 | u64 obj_size; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 816 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 817 | if (i < ti.first_group_dev) |
| 818 | obj_size = ti.prev_group_obj_off; |
| 819 | else if (i >= ti.nex_group_dev) |
| 820 | obj_size = ti.next_group_obj_off; |
| 821 | else if (i < ti.si.dev) /* dev within this group */ |
| 822 | obj_size = ti.si.obj_offset + |
| 823 | ios->layout->stripe_unit - ti.si.unit_off; |
| 824 | else if (i == ti.si.dev) |
| 825 | obj_size = ti.si.obj_offset; |
| 826 | else /* i > ti.dev */ |
| 827 | obj_size = ti.si.obj_offset - ti.si.unit_off; |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 828 | |
| 829 | size_attr->newsize = cpu_to_be64(obj_size); |
| 830 | size_attr->attr = g_attr_logical_length; |
| 831 | size_attr->attr.val_ptr = &size_attr->newsize; |
| 832 | |
Boaz Harrosh | 16f75bb | 2011-08-03 20:44:16 -0700 | [diff] [blame] | 833 | EXOFS_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n", |
| 834 | _LLU(ios->obj.id), _LLU(obj_size), i); |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 835 | ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1, |
| 836 | &size_attr->attr); |
| 837 | if (unlikely(ret)) |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 838 | goto out; |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 839 | } |
| 840 | ret = exofs_io_execute(ios); |
| 841 | |
| 842 | out: |
Boaz Harrosh | 5d952b8 | 2010-02-01 13:35:51 +0200 | [diff] [blame] | 843 | kfree(size_attrs); |
Boaz Harrosh | 06886a5 | 2009-11-08 14:54:08 +0200 | [diff] [blame] | 844 | exofs_put_io_state(ios); |
| 845 | return ret; |
| 846 | } |