blob: d92998d5c2d6291bf09fd5c3bfa42fb9bea6e5de [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 Harrosh34ce4e72009-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 Harrosh5a51c0c2011-09-28 13:18:45 +030060enum { BIO_MAX_PAGES_KMALLOC =
61 (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),};
62
63int ore_verify_layout(unsigned total_comps, struct ore_layout *layout)
64{
65 u64 stripe_length;
66
67/* FIXME: Only raid0 is supported for now. */
68 if (layout->raid_algorithm != PNFS_OSD_RAID_0) {
69 ORE_ERR("Only RAID_0 for now\n");
70 return -EINVAL;
71 }
72 if (0 != (layout->stripe_unit & ~PAGE_MASK)) {
73 ORE_ERR("Stripe Unit(0x%llx)"
74 " must be Multples of PAGE_SIZE(0x%lx)\n",
75 _LLU(layout->stripe_unit), PAGE_SIZE);
76 return -EINVAL;
77 }
78 if (layout->group_width) {
79 if (!layout->group_depth) {
80 ORE_ERR("group_depth == 0 && group_width != 0\n");
81 return -EINVAL;
82 }
83 if (total_comps < (layout->group_width * layout->mirrors_p1)) {
84 ORE_ERR("Data Map wrong, "
85 "numdevs=%d < group_width=%d * mirrors=%d\n",
86 total_comps, layout->group_width,
87 layout->mirrors_p1);
88 return -EINVAL;
89 }
90 layout->group_count = total_comps / layout->mirrors_p1 /
91 layout->group_width;
92 } else {
93 if (layout->group_depth) {
94 printk(KERN_NOTICE "Warning: group_depth ignored "
95 "group_width == 0 && group_depth == %lld\n",
96 _LLU(layout->group_depth));
97 }
98 layout->group_width = total_comps / layout->mirrors_p1;
99 layout->group_depth = -1;
100 layout->group_count = 1;
101 }
102
103 stripe_length = (u64)layout->group_width * layout->stripe_unit;
104 if (stripe_length >= (1ULL << 32)) {
105 ORE_ERR("Stripe_length(0x%llx) >= 32bit is not supported\n",
106 _LLU(stripe_length));
107 return -EINVAL;
108 }
109
110 layout->max_io_length =
111 (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE - layout->stripe_unit) *
112 layout->group_width;
113 return 0;
114}
115EXPORT_SYMBOL(ore_verify_layout);
116
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700117static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700118{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300119 return ios->oc->comps[index & ios->oc->single_comp].cred;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700120}
121
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700122static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700123{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300124 return &ios->oc->comps[index & ios->oc->single_comp].obj;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700125}
126
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700127static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700128{
Boaz Harrosh3bd98562011-09-28 12:04:23 +0300129 ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n",
130 ios->oc->first_dev, ios->oc->numdevs, index,
131 ios->oc->ods);
132
Boaz Harroshd866d872011-09-28 14:43:09 +0300133 return ore_comp_dev(ios->oc, index);
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700134}
135
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300136static int _get_io_state(struct ore_layout *layout,
137 struct ore_components *oc, unsigned numdevs,
138 struct ore_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200139{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700140 struct ore_io_state *ios;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200141
Boaz Harrosh06886a52009-11-08 14:54:08 +0200142 /*TODO: Maybe use kmem_cach per sbi of size
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200143 * exofs_io_state_size(layout->s_numdevs)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200144 */
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300145 ios = kzalloc(ore_io_state_size(numdevs), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200146 if (unlikely(!ios)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700147 ORE_DBGMSG("Failed kzalloc bytes=%d\n",
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300148 ore_io_state_size(numdevs));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200149 *pios = NULL;
150 return -ENOMEM;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200151 }
152
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200153 ios->layout = layout;
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300154 ios->oc = oc;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200155 *pios = ios;
156 return 0;
157}
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300158
159/* Allocate an io_state for only a single group of devices
160 *
161 * If a user needs to call ore_read/write() this version must be used becase it
162 * allocates extra stuff for striping and raid.
163 * The ore might decide to only IO less then @length bytes do to alignmets
164 * and constrains as follows:
165 * - The IO cannot cross group boundary.
166 * - In raid5/6 The end of the IO must align at end of a stripe eg.
167 * (@offset + @length) % strip_size == 0. Or the complete range is within a
168 * single stripe.
169 * - Memory condition only permitted a shorter IO. (A user can use @length=~0
170 * And check the returned ios->length for max_io_size.)
171 *
172 * The caller must check returned ios->length (and/or ios->nr_pages) and
173 * re-issue these pages that fall outside of ios->length
174 */
175int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
176 bool is_reading, u64 offset, u64 length,
177 struct ore_io_state **pios)
178{
179 struct ore_io_state *ios;
180 unsigned numdevs = layout->group_width * layout->mirrors_p1;
181 int ret;
182
183 ret = _get_io_state(layout, oc, numdevs, pios);
184 if (unlikely(ret))
185 return ret;
186
187 ios = *pios;
188 ios->reading = is_reading;
189 ios->offset = offset;
190
191 if (length) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200192 ore_calc_stripe_info(layout, offset, &ios->si);
193 ios->length = (length <= ios->si.group_length) ? length :
194 ios->si.group_length;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300195 ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
196 }
197
198 return 0;
199}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700200EXPORT_SYMBOL(ore_get_rw_state);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200201
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300202/* Allocate an io_state for all the devices in the comps array
203 *
204 * This version of io_state allocation is used mostly by create/remove
205 * and trunc where we currently need all the devices. The only wastful
206 * bit is the read/write_attributes with no IO. Those sites should
207 * be converted to use ore_get_rw_state() with length=0
208 */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300209int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300210 struct ore_io_state **pios)
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200211{
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300212 return _get_io_state(layout, oc, oc->numdevs, pios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200213}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700214EXPORT_SYMBOL(ore_get_io_state);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200215
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700216void ore_put_io_state(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200217{
218 if (ios) {
219 unsigned i;
220
221 for (i = 0; i < ios->numdevs; i++) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700222 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200223
224 if (per_dev->or)
225 osd_end_request(per_dev->or);
226 if (per_dev->bio)
227 bio_put(per_dev->bio);
228 }
229
230 kfree(ios);
231 }
232}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700233EXPORT_SYMBOL(ore_put_io_state);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200234
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700235static void _sync_done(struct ore_io_state *ios, void *p)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200236{
237 struct completion *waiting = p;
238
239 complete(waiting);
240}
241
242static void _last_io(struct kref *kref)
243{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700244 struct ore_io_state *ios = container_of(
245 kref, struct ore_io_state, kref);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200246
247 ios->done(ios, ios->private);
248}
249
250static void _done_io(struct osd_request *or, void *p)
251{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700252 struct ore_io_state *ios = p;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200253
254 kref_put(&ios->kref, _last_io);
255}
256
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700257static int ore_io_execute(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200258{
259 DECLARE_COMPLETION_ONSTACK(wait);
260 bool sync = (ios->done == NULL);
261 int i, ret;
262
263 if (sync) {
264 ios->done = _sync_done;
265 ios->private = &wait;
266 }
267
268 for (i = 0; i < ios->numdevs; i++) {
269 struct osd_request *or = ios->per_dev[i].or;
270 if (unlikely(!or))
271 continue;
272
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700273 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200274 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700275 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200276 ret);
277 return ret;
278 }
279 }
280
281 kref_init(&ios->kref);
282
283 for (i = 0; i < ios->numdevs; i++) {
284 struct osd_request *or = ios->per_dev[i].or;
285 if (unlikely(!or))
286 continue;
287
288 kref_get(&ios->kref);
289 osd_execute_request_async(or, _done_io, ios);
290 }
291
292 kref_put(&ios->kref, _last_io);
293 ret = 0;
294
295 if (sync) {
296 wait_for_completion(&wait);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700297 ret = ore_check_io(ios, NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200298 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200299 return ret;
300}
301
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200302static void _clear_bio(struct bio *bio)
303{
304 struct bio_vec *bv;
305 unsigned i;
306
307 __bio_for_each_segment(bv, bio, i, 0) {
308 unsigned this_count = bv->bv_len;
309
310 if (likely(PAGE_SIZE == this_count))
311 clear_highpage(bv->bv_page);
312 else
313 zero_user(bv->bv_page, bv->bv_offset, this_count);
314 }
315}
316
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300317int ore_check_io(struct ore_io_state *ios, ore_on_dev_error on_dev_error)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200318{
319 enum osd_err_priority acumulated_osd_err = 0;
320 int acumulated_lin_err = 0;
321 int i;
322
323 for (i = 0; i < ios->numdevs; i++) {
324 struct osd_sense_info osi;
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300325 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
326 struct osd_request *or = per_dev->or;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200327 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200328
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200329 if (unlikely(!or))
330 continue;
331
332 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200333 if (likely(!ret))
334 continue;
335
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200336 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
337 /* start read offset passed endof file */
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300338 _clear_bio(per_dev->bio);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700339 ORE_DBGMSG("start read offset passed end of file "
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200340 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300341 _LLU(per_dev->offset),
342 _LLU(per_dev->length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200343
344 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200345 }
346
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300347 if (on_dev_error) {
348 u64 residual = ios->reading ?
349 or->in.residual : or->out.residual;
350 u64 offset = (ios->offset + ios->length) - residual;
351 struct ore_dev *od = ios->oc->ods[
352 per_dev->dev - ios->oc->first_dev];
353
354 on_dev_error(ios, od, per_dev->dev, osi.osd_err_pri,
355 offset, residual);
356 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200357 if (osi.osd_err_pri >= acumulated_osd_err) {
358 acumulated_osd_err = osi.osd_err_pri;
359 acumulated_lin_err = ret;
360 }
361 }
362
Boaz Harrosh06886a52009-11-08 14:54:08 +0200363 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 Harrosh611d7a52011-10-04 14:20:17 +0200409void 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}
Boaz Harrosh611d7a52011-10-04 14:20:17 +0200443EXPORT_SYMBOL(ore_calc_stripe_info);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200444
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700445static int _add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
446 unsigned pgbase, struct ore_per_dev_state *per_dev,
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200447 int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200448{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200449 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200450 struct request_queue *q =
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700451 osd_request_queue(_ios_od(ios, per_dev->dev));
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700452 unsigned len = cur_len;
453 int ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200454
455 if (per_dev->bio == NULL) {
456 unsigned pages_in_stripe = ios->layout->group_width *
457 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200458 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200459 ios->layout->group_width;
460
461 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
462 if (unlikely(!per_dev->bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700463 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200464 bio_size);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700465 ret = -ENOMEM;
466 goto out;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200467 }
468 }
469
470 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200471 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
472 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200473
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200474 BUG_ON(ios->nr_pages <= pg);
475 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200476
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200477 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
478 pglen, pgbase);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700479 if (unlikely(pglen != added_len)) {
480 ret = -ENOMEM;
481 goto out;
482 }
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200483 pgbase = 0;
484 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200485 }
486 BUG_ON(cur_len);
487
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700488 per_dev->length += len;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200489 *cur_pg = pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700490 ret = 0;
491out: /* we fail the complete unit on an error eg don't advance
492 * per_dev->length and cur_pg. This means that we might have a bigger
493 * bio than the CDB requested length (per_dev->length). That's fine
494 * only the oposite is fatal.
495 */
496 return ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200497}
498
Boaz Harrosh98260752011-10-02 15:32:50 +0200499static int _prepare_for_striping(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200500{
Boaz Harrosh98260752011-10-02 15:32:50 +0200501 struct ore_striping_info *si = &ios->si;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200502 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200503 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200504 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200505 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200506 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200507 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh98260752011-10-02 15:32:50 +0200508 u64 length = ios->length;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200509 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200510
Boaz Harrosh98260752011-10-02 15:32:50 +0200511 if (!ios->pages) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200512 ios->numdevs = ios->layout->mirrors_p1;
513 return 0;
514 }
515
516 BUG_ON(length > si->group_length);
517
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200518 while (length) {
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300519 unsigned comp = dev - first_dev;
520 struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
Boaz Harroshb367e782010-02-07 19:18:58 +0200521 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200522
523 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200524 per_dev->dev = dev;
525 if (dev < si->dev) {
526 per_dev->offset = si->obj_offset + stripe_unit -
527 si->unit_off;
528 cur_len = stripe_unit;
529 } else if (dev == si->dev) {
530 per_dev->offset = si->obj_offset;
531 cur_len = stripe_unit - si->unit_off;
532 page_off = si->unit_off & ~PAGE_MASK;
533 BUG_ON(page_off && (page_off != ios->pgbase));
534 } else { /* dev > si->dev */
535 per_dev->offset = si->obj_offset - si->unit_off;
536 cur_len = stripe_unit;
537 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200538 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200539 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200540 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200541 if (cur_len >= length)
542 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200543
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200544 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
545 cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200546 if (unlikely(ret))
547 goto out;
548
Boaz Harrosh6e316092010-07-29 17:08:13 +0300549 dev += mirrors_p1;
550 dev = (dev % devs_in_group) + first_dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200551
552 length -= cur_len;
553 }
554out:
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300555 ios->numdevs = devs_in_group;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200556 ios->pages_consumed = cur_pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700557 if (unlikely(ret)) {
558 if (length == ios->length)
559 return ret;
560 else
561 ios->length -= length;
562 }
563 return 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200564}
565
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700566int ore_create(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200567{
568 int i, ret;
569
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300570 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200571 struct osd_request *or;
572
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700573 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200574 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700575 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200576 ret = -ENOMEM;
577 goto out;
578 }
579 ios->per_dev[i].or = or;
580 ios->numdevs++;
581
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700582 osd_req_create_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200583 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700584 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200585
586out:
587 return ret;
588}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700589EXPORT_SYMBOL(ore_create);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200590
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700591int ore_remove(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200592{
593 int i, ret;
594
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300595 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200596 struct osd_request *or;
597
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700598 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200599 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700600 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200601 ret = -ENOMEM;
602 goto out;
603 }
604 ios->per_dev[i].or = or;
605 ios->numdevs++;
606
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700607 osd_req_remove_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200608 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700609 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200610
611out:
612 return ret;
613}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700614EXPORT_SYMBOL(ore_remove);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200615
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700616static int _write_mirror(struct ore_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200617{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700618 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200619 unsigned dev = ios->per_dev[cur_comp].dev;
620 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
621 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200622
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200623 if (ios->pages && !master_dev->length)
624 return 0; /* Just an empty slot */
625
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200626 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700627 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200628 struct osd_request *or;
629
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700630 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200631 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700632 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200633 ret = -ENOMEM;
634 goto out;
635 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200636 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200637
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200638 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200639 struct bio *bio;
640
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200641 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200642 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200643 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200644 if (unlikely(!bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700645 ORE_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200646 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200647 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200648 ret = -ENOMEM;
649 goto out;
650 }
651
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200652 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200653 bio->bi_bdev = NULL;
654 bio->bi_next = NULL;
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700655 per_dev->offset = master_dev->offset;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200656 per_dev->length = master_dev->length;
657 per_dev->bio = bio;
658 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200659 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200660 bio = master_dev->bio;
661 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200662 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200663 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200664
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700665 osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
666 bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700667 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200668 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700669 _LLU(_ios_obj(ios, dev)->id),
670 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200671 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200672 } else if (ios->kern_buff) {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700673 per_dev->offset = ios->si.obj_offset;
674 per_dev->dev = ios->si.dev + dev;
675
676 /* no cross device without page array */
677 BUG_ON((ios->layout->group_width > 1) &&
678 (ios->si.unit_off + ios->length >
679 ios->layout->stripe_unit));
680
681 ret = osd_req_write_kern(or, _ios_obj(ios, per_dev->dev),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700682 per_dev->offset,
683 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200684 if (unlikely(ret))
685 goto out;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700686 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200687 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700688 _LLU(_ios_obj(ios, dev)->id),
689 _LLU(per_dev->offset),
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700690 _LLU(ios->length), per_dev->dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200691 } else {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700692 osd_req_set_attributes(or, _ios_obj(ios, dev));
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700693 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700694 _LLU(_ios_obj(ios, dev)->id),
695 ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200696 }
697
698 if (ios->out_attr)
699 osd_req_add_set_attr_list(or, ios->out_attr,
700 ios->out_attr_len);
701
702 if (ios->in_attr)
703 osd_req_add_get_attr_list(or, ios->in_attr,
704 ios->in_attr_len);
705 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200706
707out:
708 return ret;
709}
710
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700711int ore_write(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200712{
713 int i;
714 int ret;
715
716 ret = _prepare_for_striping(ios);
717 if (unlikely(ret))
718 return ret;
719
720 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700721 ret = _write_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200722 if (unlikely(ret))
723 return ret;
724 }
725
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700726 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200727 return ret;
728}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700729EXPORT_SYMBOL(ore_write);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200730
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700731static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200732{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200733 struct osd_request *or;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700734 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700735 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
736 unsigned first_dev = (unsigned)obj->id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200737
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200738 if (ios->pages && !per_dev->length)
739 return 0; /* Just an empty slot */
740
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200741 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700742 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200743 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700744 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200745 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200746 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200747 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200748
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200749 if (ios->pages) {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700750 osd_req_read(or, obj, per_dev->offset,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200751 per_dev->bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700752 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700753 " dev=%d\n", _LLU(obj->id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200754 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200755 first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200756 } else {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700757 BUG_ON(ios->kern_buff);
758
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700759 osd_req_get_attributes(or, obj);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700760 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700761 _LLU(obj->id),
762 ios->in_attr_len, first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200763 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200764 if (ios->out_attr)
765 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
766
767 if (ios->in_attr)
768 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
769
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200770 return 0;
771}
772
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700773int ore_read(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200774{
775 int i;
776 int ret;
777
778 ret = _prepare_for_striping(ios);
779 if (unlikely(ret))
780 return ret;
781
782 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700783 ret = _read_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200784 if (unlikely(ret))
785 return ret;
786 }
787
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700788 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200789 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200790}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700791EXPORT_SYMBOL(ore_read);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200792
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700793int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200794{
795 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
796 void *iter = NULL;
797 int nelem;
798
799 do {
800 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200801 osd_req_decode_get_attr_list(ios->per_dev[0].or,
802 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200803 if ((cur_attr.attr_page == attr->attr_page) &&
804 (cur_attr.attr_id == attr->attr_id)) {
805 attr->len = cur_attr.len;
806 attr->val_ptr = cur_attr.val_ptr;
807 return 0;
808 }
809 } while (iter);
810
811 return -EIO;
812}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700813EXPORT_SYMBOL(extract_attr_from_ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200814
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700815static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200816 struct osd_attr *attr)
817{
818 int last_comp = cur_comp + ios->layout->mirrors_p1;
819
820 for (; cur_comp < last_comp; ++cur_comp) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700821 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200822 struct osd_request *or;
823
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700824 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200825 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700826 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200827 return -ENOMEM;
828 }
829 per_dev->or = or;
830
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700831 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200832 osd_req_add_set_attr_list(or, attr, 1);
833 }
834
835 return 0;
836}
837
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700838struct _trunc_info {
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700839 struct ore_striping_info si;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700840 u64 prev_group_obj_off;
841 u64 next_group_obj_off;
842
843 unsigned first_group_dev;
844 unsigned nex_group_dev;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700845};
846
H Hartley Sweeten1958c7c22011-09-23 13:42:43 -0700847static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
848 struct _trunc_info *ti)
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700849{
850 unsigned stripe_unit = layout->stripe_unit;
851
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700852 ore_calc_stripe_info(layout, file_offset, &ti->si);
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700853
854 ti->prev_group_obj_off = ti->si.M * stripe_unit;
855 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
856
857 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
858 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700859}
860
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300861int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700862 u64 size)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200863{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700864 struct ore_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200865 struct exofs_trunc_attr {
866 struct osd_attr attr;
867 __be64 newsize;
868 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700869 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200870 int i, ret;
871
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300872 ret = ore_get_io_state(layout, oc, &ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200873 if (unlikely(ret))
874 return ret;
875
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700876 _calc_trunk_info(ios->layout, size, &ti);
877
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300878 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200879 GFP_KERNEL);
880 if (unlikely(!size_attrs)) {
881 ret = -ENOMEM;
882 goto out;
883 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200884
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300885 ios->numdevs = ios->oc->numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200886
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300887 for (i = 0; i < ios->numdevs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200888 struct exofs_trunc_attr *size_attr = &size_attrs[i];
889 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200890
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700891 if (i < ti.first_group_dev)
892 obj_size = ti.prev_group_obj_off;
893 else if (i >= ti.nex_group_dev)
894 obj_size = ti.next_group_obj_off;
895 else if (i < ti.si.dev) /* dev within this group */
896 obj_size = ti.si.obj_offset +
897 ios->layout->stripe_unit - ti.si.unit_off;
898 else if (i == ti.si.dev)
899 obj_size = ti.si.obj_offset;
900 else /* i > ti.dev */
901 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200902
903 size_attr->newsize = cpu_to_be64(obj_size);
904 size_attr->attr = g_attr_logical_length;
905 size_attr->attr.val_ptr = &size_attr->newsize;
906
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700907 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300908 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200909 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
910 &size_attr->attr);
911 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200912 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200913 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700914 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200915
916out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200917 kfree(size_attrs);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700918 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200919 return ret;
920}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700921EXPORT_SYMBOL(ore_truncate);
Boaz Harrosh85e44df2011-05-16 15:26:47 +0300922
923const struct osd_attr g_attr_logical_length = ATTR_DEF(
924 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700925EXPORT_SYMBOL(g_attr_logical_length);