blob: 4ca59d4927985c9fc2571eefc03f70aa32f58fd5 [file] [log] [blame]
Boaz Harroshb14f8ab2008-10-27 18:27:55 +02001/*
2 * Copyright (C) 2005, 2006
Boaz Harrosh27d2e142009-06-14 17:23:09 +03003 * Avishay Traeger (avishay@gmail.com)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +02004 * 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 Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Boaz Harrosh5d952b82010-02-01 13:35:51 +020026#include <asm/div64.h>
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020027
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070028#include <scsi/osd_ore.h>
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020029
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070030#define ORE_ERR(fmt, a...) printk(KERN_ERR "ore: " fmt, ##a)
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +020031
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070032#ifdef CONFIG_EXOFS_DEBUG
33#define ORE_DBGMSG(fmt, a...) \
34 printk(KERN_NOTICE "ore @%s:%d: " fmt, __func__, __LINE__, ##a)
35#else
36#define ORE_DBGMSG(fmt, a...) \
37 do { if (0) printk(fmt, ##a); } while (0)
38#endif
39
40/* u64 has problems with printk this will cast it to unsigned long long */
41#define _LLU(x) (unsigned long long)(x)
42
43#define ORE_DBGMSG2(M...) do {} while (0)
44/* #define ORE_DBGMSG2 ORE_DBGMSG */
45
Boaz Harroshcf283ad2011-08-06 19:22:06 -070046MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
47MODULE_DESCRIPTION("Objects Raid Engine ore.ko");
48MODULE_LICENSE("GPL");
49
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030050/* ore_verify_layout does a couple of things:
51 * 1. Given a minimum number of needed parameters fixes up the rest of the
52 * members to be operatonals for the ore. The needed parameters are those
53 * that are defined by the pnfs-objects layout STD.
54 * 2. Check to see if the current ore code actually supports these parameters
55 * for example stripe_unit must be a multple of the system PAGE_SIZE,
56 * and etc...
57 * 3. Cache some havily used calculations that will be needed by users.
58 */
59
Boaz Harroshb916c5c2011-09-28 11:55:51 +030060static void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
61 struct ore_striping_info *si);
62
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030063enum { BIO_MAX_PAGES_KMALLOC =
64 (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),};
65
66int ore_verify_layout(unsigned total_comps, struct ore_layout *layout)
67{
68 u64 stripe_length;
69
70/* FIXME: Only raid0 is supported for now. */
71 if (layout->raid_algorithm != PNFS_OSD_RAID_0) {
72 ORE_ERR("Only RAID_0 for now\n");
73 return -EINVAL;
74 }
75 if (0 != (layout->stripe_unit & ~PAGE_MASK)) {
76 ORE_ERR("Stripe Unit(0x%llx)"
77 " must be Multples of PAGE_SIZE(0x%lx)\n",
78 _LLU(layout->stripe_unit), PAGE_SIZE);
79 return -EINVAL;
80 }
81 if (layout->group_width) {
82 if (!layout->group_depth) {
83 ORE_ERR("group_depth == 0 && group_width != 0\n");
84 return -EINVAL;
85 }
86 if (total_comps < (layout->group_width * layout->mirrors_p1)) {
87 ORE_ERR("Data Map wrong, "
88 "numdevs=%d < group_width=%d * mirrors=%d\n",
89 total_comps, layout->group_width,
90 layout->mirrors_p1);
91 return -EINVAL;
92 }
93 layout->group_count = total_comps / layout->mirrors_p1 /
94 layout->group_width;
95 } else {
96 if (layout->group_depth) {
97 printk(KERN_NOTICE "Warning: group_depth ignored "
98 "group_width == 0 && group_depth == %lld\n",
99 _LLU(layout->group_depth));
100 }
101 layout->group_width = total_comps / layout->mirrors_p1;
102 layout->group_depth = -1;
103 layout->group_count = 1;
104 }
105
106 stripe_length = (u64)layout->group_width * layout->stripe_unit;
107 if (stripe_length >= (1ULL << 32)) {
108 ORE_ERR("Stripe_length(0x%llx) >= 32bit is not supported\n",
109 _LLU(stripe_length));
110 return -EINVAL;
111 }
112
113 layout->max_io_length =
114 (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE - layout->stripe_unit) *
115 layout->group_width;
116 return 0;
117}
118EXPORT_SYMBOL(ore_verify_layout);
119
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700120static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700121{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300122 return ios->oc->comps[index & ios->oc->single_comp].cred;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700123}
124
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700125static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700126{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300127 return &ios->oc->comps[index & ios->oc->single_comp].obj;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700128}
129
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700130static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700131{
Boaz Harrosh3bd98562011-09-28 12:04:23 +0300132 ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n",
133 ios->oc->first_dev, ios->oc->numdevs, index,
134 ios->oc->ods);
135
Boaz Harroshd866d872011-09-28 14:43:09 +0300136 return ore_comp_dev(ios->oc, index);
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700137}
138
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300139static int _get_io_state(struct ore_layout *layout,
140 struct ore_components *oc, unsigned numdevs,
141 struct ore_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200142{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700143 struct ore_io_state *ios;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200144
Boaz Harrosh06886a52009-11-08 14:54:08 +0200145 /*TODO: Maybe use kmem_cach per sbi of size
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200146 * exofs_io_state_size(layout->s_numdevs)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200147 */
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300148 ios = kzalloc(ore_io_state_size(numdevs), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200149 if (unlikely(!ios)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700150 ORE_DBGMSG("Failed kzalloc bytes=%d\n",
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300151 ore_io_state_size(numdevs));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200152 *pios = NULL;
153 return -ENOMEM;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200154 }
155
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200156 ios->layout = layout;
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300157 ios->oc = oc;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200158 *pios = ios;
159 return 0;
160}
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300161
162/* Allocate an io_state for only a single group of devices
163 *
164 * If a user needs to call ore_read/write() this version must be used becase it
165 * allocates extra stuff for striping and raid.
166 * The ore might decide to only IO less then @length bytes do to alignmets
167 * and constrains as follows:
168 * - The IO cannot cross group boundary.
169 * - In raid5/6 The end of the IO must align at end of a stripe eg.
170 * (@offset + @length) % strip_size == 0. Or the complete range is within a
171 * single stripe.
172 * - Memory condition only permitted a shorter IO. (A user can use @length=~0
173 * And check the returned ios->length for max_io_size.)
174 *
175 * The caller must check returned ios->length (and/or ios->nr_pages) and
176 * re-issue these pages that fall outside of ios->length
177 */
178int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
179 bool is_reading, u64 offset, u64 length,
180 struct ore_io_state **pios)
181{
182 struct ore_io_state *ios;
183 unsigned numdevs = layout->group_width * layout->mirrors_p1;
184 int ret;
185
186 ret = _get_io_state(layout, oc, numdevs, pios);
187 if (unlikely(ret))
188 return ret;
189
190 ios = *pios;
191 ios->reading = is_reading;
192 ios->offset = offset;
193
194 if (length) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200195 ore_calc_stripe_info(layout, offset, &ios->si);
196 ios->length = (length <= ios->si.group_length) ? length :
197 ios->si.group_length;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300198 ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
199 }
200
201 return 0;
202}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700203EXPORT_SYMBOL(ore_get_rw_state);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200204
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300205/* Allocate an io_state for all the devices in the comps array
206 *
207 * This version of io_state allocation is used mostly by create/remove
208 * and trunc where we currently need all the devices. The only wastful
209 * bit is the read/write_attributes with no IO. Those sites should
210 * be converted to use ore_get_rw_state() with length=0
211 */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300212int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300213 struct ore_io_state **pios)
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200214{
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300215 return _get_io_state(layout, oc, oc->numdevs, pios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200216}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700217EXPORT_SYMBOL(ore_get_io_state);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200218
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700219void ore_put_io_state(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200220{
221 if (ios) {
222 unsigned i;
223
224 for (i = 0; i < ios->numdevs; i++) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700225 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200226
227 if (per_dev->or)
228 osd_end_request(per_dev->or);
229 if (per_dev->bio)
230 bio_put(per_dev->bio);
231 }
232
233 kfree(ios);
234 }
235}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700236EXPORT_SYMBOL(ore_put_io_state);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200237
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700238static void _sync_done(struct ore_io_state *ios, void *p)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200239{
240 struct completion *waiting = p;
241
242 complete(waiting);
243}
244
245static void _last_io(struct kref *kref)
246{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700247 struct ore_io_state *ios = container_of(
248 kref, struct ore_io_state, kref);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200249
250 ios->done(ios, ios->private);
251}
252
253static void _done_io(struct osd_request *or, void *p)
254{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700255 struct ore_io_state *ios = p;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200256
257 kref_put(&ios->kref, _last_io);
258}
259
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700260static int ore_io_execute(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200261{
262 DECLARE_COMPLETION_ONSTACK(wait);
263 bool sync = (ios->done == NULL);
264 int i, ret;
265
266 if (sync) {
267 ios->done = _sync_done;
268 ios->private = &wait;
269 }
270
271 for (i = 0; i < ios->numdevs; i++) {
272 struct osd_request *or = ios->per_dev[i].or;
273 if (unlikely(!or))
274 continue;
275
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700276 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200277 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700278 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200279 ret);
280 return ret;
281 }
282 }
283
284 kref_init(&ios->kref);
285
286 for (i = 0; i < ios->numdevs; i++) {
287 struct osd_request *or = ios->per_dev[i].or;
288 if (unlikely(!or))
289 continue;
290
291 kref_get(&ios->kref);
292 osd_execute_request_async(or, _done_io, ios);
293 }
294
295 kref_put(&ios->kref, _last_io);
296 ret = 0;
297
298 if (sync) {
299 wait_for_completion(&wait);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700300 ret = ore_check_io(ios, NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200301 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200302 return ret;
303}
304
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200305static void _clear_bio(struct bio *bio)
306{
307 struct bio_vec *bv;
308 unsigned i;
309
310 __bio_for_each_segment(bv, bio, i, 0) {
311 unsigned this_count = bv->bv_len;
312
313 if (likely(PAGE_SIZE == this_count))
314 clear_highpage(bv->bv_page);
315 else
316 zero_user(bv->bv_page, bv->bv_offset, this_count);
317 }
318}
319
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700320int ore_check_io(struct ore_io_state *ios, u64 *resid)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200321{
322 enum osd_err_priority acumulated_osd_err = 0;
323 int acumulated_lin_err = 0;
324 int i;
325
326 for (i = 0; i < ios->numdevs; i++) {
327 struct osd_sense_info osi;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200328 struct osd_request *or = ios->per_dev[i].or;
329 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200330
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200331 if (unlikely(!or))
332 continue;
333
334 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200335 if (likely(!ret))
336 continue;
337
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200338 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
339 /* start read offset passed endof file */
340 _clear_bio(ios->per_dev[i].bio);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700341 ORE_DBGMSG("start read offset passed end of file "
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200342 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200343 _LLU(ios->per_dev[i].offset),
344 _LLU(ios->per_dev[i].length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200345
346 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200347 }
348
349 if (osi.osd_err_pri >= acumulated_osd_err) {
350 acumulated_osd_err = osi.osd_err_pri;
351 acumulated_lin_err = ret;
352 }
353 }
354
355 /* TODO: raid specific residual calculations */
356 if (resid) {
357 if (likely(!acumulated_lin_err))
358 *resid = 0;
359 else
360 *resid = ios->length;
361 }
362
363 return acumulated_lin_err;
364}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700365EXPORT_SYMBOL(ore_check_io);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200366
Boaz Harroshb367e782010-02-07 19:18:58 +0200367/*
368 * L - logical offset into the file
369 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200370 * U - The number of bytes in a stripe within a group
Boaz Harroshb367e782010-02-07 19:18:58 +0200371 *
372 * U = stripe_unit * group_width
373 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200374 * T - The number of bytes striped within a group of component objects
375 * (before advancing to the next group)
Boaz Harroshb367e782010-02-07 19:18:58 +0200376 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200377 * T = stripe_unit * group_width * group_depth
378 *
379 * S - The number of bytes striped across all component objects
380 * before the pattern repeats
381 *
382 * S = stripe_unit * group_width * group_depth * group_count
383 *
384 * M - The "major" (i.e., across all components) stripe number
385 *
386 * M = L / S
387 *
388 * G - Counts the groups from the beginning of the major stripe
389 *
390 * G = (L - (M * S)) / T [or (L % S) / T]
391 *
392 * H - The byte offset within the group
393 *
394 * H = (L - (M * S)) % T [or (L % S) % T]
395 *
396 * N - The "minor" (i.e., across the group) stripe number
397 *
398 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200399 *
400 * C - The component index coresponding to L
401 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200402 * C = (H - (N * U)) / stripe_unit + G * group_width
403 * [or (L % U) / stripe_unit + G * group_width]
Boaz Harroshb367e782010-02-07 19:18:58 +0200404 *
405 * O - The component offset coresponding to L
406 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200407 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harroshb367e782010-02-07 19:18:58 +0200408 */
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700409static void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
410 struct ore_striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200411{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700412 u32 stripe_unit = layout->stripe_unit;
413 u32 group_width = layout->group_width;
414 u64 group_depth = layout->group_depth;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200415
Boaz Harroshb367e782010-02-07 19:18:58 +0200416 u32 U = stripe_unit * group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200417 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700418 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200419 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200420
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200421 /*
422 G = (L - (M * S)) / T
423 H = (L - (M * S)) % T
424 */
425 u64 LmodS = file_offset - M * S;
426 u32 G = div64_u64(LmodS, T);
427 u64 H = LmodS - G * T;
Boaz Harroshb367e782010-02-07 19:18:58 +0200428
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200429 u32 N = div_u64(H, U);
430
431 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
432 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700433 si->dev *= layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200434
435 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
436
437 si->obj_offset = si->unit_off + (N * stripe_unit) +
438 (M * group_depth * stripe_unit);
439
440 si->group_length = T - H;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700441 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200442}
443
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700444static int _add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
445 unsigned pgbase, struct ore_per_dev_state *per_dev,
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200446 int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200447{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200448 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200449 struct request_queue *q =
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700450 osd_request_queue(_ios_od(ios, per_dev->dev));
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700451 unsigned len = cur_len;
452 int ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200453
454 if (per_dev->bio == NULL) {
455 unsigned pages_in_stripe = ios->layout->group_width *
456 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200457 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200458 ios->layout->group_width;
459
460 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
461 if (unlikely(!per_dev->bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700462 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200463 bio_size);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700464 ret = -ENOMEM;
465 goto out;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200466 }
467 }
468
469 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200470 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
471 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200472
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200473 BUG_ON(ios->nr_pages <= pg);
474 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200475
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200476 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
477 pglen, pgbase);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700478 if (unlikely(pglen != added_len)) {
479 ret = -ENOMEM;
480 goto out;
481 }
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200482 pgbase = 0;
483 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200484 }
485 BUG_ON(cur_len);
486
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700487 per_dev->length += len;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200488 *cur_pg = pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700489 ret = 0;
490out: /* we fail the complete unit on an error eg don't advance
491 * per_dev->length and cur_pg. This means that we might have a bigger
492 * bio than the CDB requested length (per_dev->length). That's fine
493 * only the oposite is fatal.
494 */
495 return ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200496}
497
Boaz Harrosh98260752011-10-02 15:32:50 +0200498static int _prepare_for_striping(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200499{
Boaz Harrosh98260752011-10-02 15:32:50 +0200500 struct ore_striping_info *si = &ios->si;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200501 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200502 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200503 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200504 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200505 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200506 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh98260752011-10-02 15:32:50 +0200507 u64 length = ios->length;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200508 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200509
Boaz Harrosh98260752011-10-02 15:32:50 +0200510 if (!ios->pages) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200511 ios->numdevs = ios->layout->mirrors_p1;
512 return 0;
513 }
514
515 BUG_ON(length > si->group_length);
516
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200517 while (length) {
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300518 unsigned comp = dev - first_dev;
519 struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
Boaz Harroshb367e782010-02-07 19:18:58 +0200520 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200521
522 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200523 per_dev->dev = dev;
524 if (dev < si->dev) {
525 per_dev->offset = si->obj_offset + stripe_unit -
526 si->unit_off;
527 cur_len = stripe_unit;
528 } else if (dev == si->dev) {
529 per_dev->offset = si->obj_offset;
530 cur_len = stripe_unit - si->unit_off;
531 page_off = si->unit_off & ~PAGE_MASK;
532 BUG_ON(page_off && (page_off != ios->pgbase));
533 } else { /* dev > si->dev */
534 per_dev->offset = si->obj_offset - si->unit_off;
535 cur_len = stripe_unit;
536 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200537 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200538 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200539 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200540 if (cur_len >= length)
541 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200542
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200543 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
544 cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200545 if (unlikely(ret))
546 goto out;
547
Boaz Harrosh6e316092010-07-29 17:08:13 +0300548 dev += mirrors_p1;
549 dev = (dev % devs_in_group) + first_dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200550
551 length -= cur_len;
552 }
553out:
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300554 ios->numdevs = devs_in_group;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200555 ios->pages_consumed = cur_pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700556 if (unlikely(ret)) {
557 if (length == ios->length)
558 return ret;
559 else
560 ios->length -= length;
561 }
562 return 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200563}
564
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700565int ore_create(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200566{
567 int i, ret;
568
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300569 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200570 struct osd_request *or;
571
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700572 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200573 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700574 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200575 ret = -ENOMEM;
576 goto out;
577 }
578 ios->per_dev[i].or = or;
579 ios->numdevs++;
580
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700581 osd_req_create_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200582 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700583 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200584
585out:
586 return ret;
587}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700588EXPORT_SYMBOL(ore_create);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200589
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700590int ore_remove(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200591{
592 int i, ret;
593
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300594 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200595 struct osd_request *or;
596
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700597 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200598 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700599 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200600 ret = -ENOMEM;
601 goto out;
602 }
603 ios->per_dev[i].or = or;
604 ios->numdevs++;
605
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700606 osd_req_remove_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200607 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700608 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200609
610out:
611 return ret;
612}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700613EXPORT_SYMBOL(ore_remove);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200614
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700615static int _write_mirror(struct ore_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200616{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700617 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200618 unsigned dev = ios->per_dev[cur_comp].dev;
619 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
620 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200621
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200622 if (ios->pages && !master_dev->length)
623 return 0; /* Just an empty slot */
624
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200625 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700626 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200627 struct osd_request *or;
628
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700629 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200630 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700631 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200632 ret = -ENOMEM;
633 goto out;
634 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200635 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200636
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200637 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200638 struct bio *bio;
639
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200640 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200641 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200642 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200643 if (unlikely(!bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700644 ORE_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200645 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200646 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200647 ret = -ENOMEM;
648 goto out;
649 }
650
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200651 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200652 bio->bi_bdev = NULL;
653 bio->bi_next = NULL;
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700654 per_dev->offset = master_dev->offset;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200655 per_dev->length = master_dev->length;
656 per_dev->bio = bio;
657 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200658 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200659 bio = master_dev->bio;
660 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200661 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200662 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200663
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700664 osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
665 bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700666 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200667 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700668 _LLU(_ios_obj(ios, dev)->id),
669 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200670 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200671 } else if (ios->kern_buff) {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700672 per_dev->offset = ios->si.obj_offset;
673 per_dev->dev = ios->si.dev + dev;
674
675 /* no cross device without page array */
676 BUG_ON((ios->layout->group_width > 1) &&
677 (ios->si.unit_off + ios->length >
678 ios->layout->stripe_unit));
679
680 ret = osd_req_write_kern(or, _ios_obj(ios, per_dev->dev),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700681 per_dev->offset,
682 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200683 if (unlikely(ret))
684 goto out;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700685 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200686 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700687 _LLU(_ios_obj(ios, dev)->id),
688 _LLU(per_dev->offset),
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700689 _LLU(ios->length), per_dev->dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200690 } else {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700691 osd_req_set_attributes(or, _ios_obj(ios, dev));
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700692 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700693 _LLU(_ios_obj(ios, dev)->id),
694 ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200695 }
696
697 if (ios->out_attr)
698 osd_req_add_set_attr_list(or, ios->out_attr,
699 ios->out_attr_len);
700
701 if (ios->in_attr)
702 osd_req_add_get_attr_list(or, ios->in_attr,
703 ios->in_attr_len);
704 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200705
706out:
707 return ret;
708}
709
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700710int ore_write(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200711{
712 int i;
713 int ret;
714
715 ret = _prepare_for_striping(ios);
716 if (unlikely(ret))
717 return ret;
718
719 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700720 ret = _write_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200721 if (unlikely(ret))
722 return ret;
723 }
724
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700725 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200726 return ret;
727}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700728EXPORT_SYMBOL(ore_write);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200729
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700730static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200731{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200732 struct osd_request *or;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700733 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700734 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
735 unsigned first_dev = (unsigned)obj->id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200736
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200737 if (ios->pages && !per_dev->length)
738 return 0; /* Just an empty slot */
739
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200740 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700741 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200742 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700743 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200744 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200745 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200746 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200747
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200748 if (ios->pages) {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700749 osd_req_read(or, obj, per_dev->offset,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200750 per_dev->bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700751 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700752 " dev=%d\n", _LLU(obj->id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200753 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200754 first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200755 } else {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700756 BUG_ON(ios->kern_buff);
757
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700758 osd_req_get_attributes(or, obj);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700759 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700760 _LLU(obj->id),
761 ios->in_attr_len, first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200762 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200763 if (ios->out_attr)
764 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
765
766 if (ios->in_attr)
767 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
768
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200769 return 0;
770}
771
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700772int ore_read(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200773{
774 int i;
775 int ret;
776
777 ret = _prepare_for_striping(ios);
778 if (unlikely(ret))
779 return ret;
780
781 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700782 ret = _read_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200783 if (unlikely(ret))
784 return ret;
785 }
786
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700787 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200788 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200789}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700790EXPORT_SYMBOL(ore_read);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200791
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700792int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200793{
794 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
795 void *iter = NULL;
796 int nelem;
797
798 do {
799 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200800 osd_req_decode_get_attr_list(ios->per_dev[0].or,
801 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200802 if ((cur_attr.attr_page == attr->attr_page) &&
803 (cur_attr.attr_id == attr->attr_id)) {
804 attr->len = cur_attr.len;
805 attr->val_ptr = cur_attr.val_ptr;
806 return 0;
807 }
808 } while (iter);
809
810 return -EIO;
811}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700812EXPORT_SYMBOL(extract_attr_from_ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200813
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700814static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200815 struct osd_attr *attr)
816{
817 int last_comp = cur_comp + ios->layout->mirrors_p1;
818
819 for (; cur_comp < last_comp; ++cur_comp) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700820 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200821 struct osd_request *or;
822
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700823 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200824 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700825 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200826 return -ENOMEM;
827 }
828 per_dev->or = or;
829
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700830 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200831 osd_req_add_set_attr_list(or, attr, 1);
832 }
833
834 return 0;
835}
836
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700837struct _trunc_info {
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700838 struct ore_striping_info si;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700839 u64 prev_group_obj_off;
840 u64 next_group_obj_off;
841
842 unsigned first_group_dev;
843 unsigned nex_group_dev;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700844};
845
H Hartley Sweeten1958c7c22011-09-23 13:42:43 -0700846static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
847 struct _trunc_info *ti)
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700848{
849 unsigned stripe_unit = layout->stripe_unit;
850
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700851 ore_calc_stripe_info(layout, file_offset, &ti->si);
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700852
853 ti->prev_group_obj_off = ti->si.M * stripe_unit;
854 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
855
856 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
857 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700858}
859
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300860int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700861 u64 size)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200862{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700863 struct ore_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200864 struct exofs_trunc_attr {
865 struct osd_attr attr;
866 __be64 newsize;
867 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700868 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200869 int i, ret;
870
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300871 ret = ore_get_io_state(layout, oc, &ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200872 if (unlikely(ret))
873 return ret;
874
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700875 _calc_trunk_info(ios->layout, size, &ti);
876
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300877 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200878 GFP_KERNEL);
879 if (unlikely(!size_attrs)) {
880 ret = -ENOMEM;
881 goto out;
882 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200883
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300884 ios->numdevs = ios->oc->numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200885
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300886 for (i = 0; i < ios->numdevs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200887 struct exofs_trunc_attr *size_attr = &size_attrs[i];
888 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200889
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700890 if (i < ti.first_group_dev)
891 obj_size = ti.prev_group_obj_off;
892 else if (i >= ti.nex_group_dev)
893 obj_size = ti.next_group_obj_off;
894 else if (i < ti.si.dev) /* dev within this group */
895 obj_size = ti.si.obj_offset +
896 ios->layout->stripe_unit - ti.si.unit_off;
897 else if (i == ti.si.dev)
898 obj_size = ti.si.obj_offset;
899 else /* i > ti.dev */
900 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200901
902 size_attr->newsize = cpu_to_be64(obj_size);
903 size_attr->attr = g_attr_logical_length;
904 size_attr->attr.val_ptr = &size_attr->newsize;
905
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700906 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300907 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200908 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
909 &size_attr->attr);
910 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200911 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200912 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700913 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200914
915out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200916 kfree(size_attrs);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700917 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200918 return ret;
919}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700920EXPORT_SYMBOL(ore_truncate);
Boaz Harrosh85e44df2011-05-16 15:26:47 +0300921
922const struct osd_attr g_attr_logical_length = ATTR_DEF(
923 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700924EXPORT_SYMBOL(g_attr_logical_length);