blob: 25305af881983a0bd78daecdcec0c63355de0f67 [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 Harrosh8ff660a2011-08-06 19:26:31 -070050static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -070051{
52 return ios->comps->comps[index & ios->comps->single_comp].cred;
53}
54
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070055static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -070056{
57 return &ios->comps->comps[index & ios->comps->single_comp].obj;
58}
59
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070060static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -070061{
62 return ios->comps->ods[index];
63}
64
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070065int ore_get_rw_state(struct ore_layout *layout, struct ore_components *comps,
66 bool is_reading, u64 offset, u64 length,
67 struct ore_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020068{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070069 struct ore_io_state *ios;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020070
Boaz Harrosh06886a52009-11-08 14:54:08 +020071 /*TODO: Maybe use kmem_cach per sbi of size
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020072 * exofs_io_state_size(layout->s_numdevs)
Boaz Harrosh06886a52009-11-08 14:54:08 +020073 */
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070074 ios = kzalloc(ore_io_state_size(comps->numdevs), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +020075 if (unlikely(!ios)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070076 ORE_DBGMSG("Failed kzalloc bytes=%d\n",
77 ore_io_state_size(comps->numdevs));
Boaz Harrosh06886a52009-11-08 14:54:08 +020078 *pios = NULL;
79 return -ENOMEM;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020080 }
81
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020082 ios->layout = layout;
Boaz Harrosh9e9db452011-08-05 15:06:04 -070083 ios->comps = comps;
Boaz Harroshe1042ba2010-11-16 20:09:58 +020084 ios->offset = offset;
85 ios->length = length;
86 ios->reading = is_reading;
87
Boaz Harrosh06886a52009-11-08 14:54:08 +020088 *pios = ios;
89 return 0;
90}
Boaz Harroshcf283ad2011-08-06 19:22:06 -070091EXPORT_SYMBOL(ore_get_rw_state);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020092
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070093int ore_get_io_state(struct ore_layout *layout, struct ore_components *comps,
94 struct ore_io_state **ios)
Boaz Harroshe1042ba2010-11-16 20:09:58 +020095{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070096 return ore_get_rw_state(layout, comps, true, 0, 0, ios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +020097}
Boaz Harroshcf283ad2011-08-06 19:22:06 -070098EXPORT_SYMBOL(ore_get_io_state);
Boaz Harroshe1042ba2010-11-16 20:09:58 +020099
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700100void ore_put_io_state(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200101{
102 if (ios) {
103 unsigned i;
104
105 for (i = 0; i < ios->numdevs; i++) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700106 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200107
108 if (per_dev->or)
109 osd_end_request(per_dev->or);
110 if (per_dev->bio)
111 bio_put(per_dev->bio);
112 }
113
114 kfree(ios);
115 }
116}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700117EXPORT_SYMBOL(ore_put_io_state);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200118
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700119static void _sync_done(struct ore_io_state *ios, void *p)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200120{
121 struct completion *waiting = p;
122
123 complete(waiting);
124}
125
126static void _last_io(struct kref *kref)
127{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700128 struct ore_io_state *ios = container_of(
129 kref, struct ore_io_state, kref);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200130
131 ios->done(ios, ios->private);
132}
133
134static void _done_io(struct osd_request *or, void *p)
135{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700136 struct ore_io_state *ios = p;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200137
138 kref_put(&ios->kref, _last_io);
139}
140
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700141static int ore_io_execute(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200142{
143 DECLARE_COMPLETION_ONSTACK(wait);
144 bool sync = (ios->done == NULL);
145 int i, ret;
146
147 if (sync) {
148 ios->done = _sync_done;
149 ios->private = &wait;
150 }
151
152 for (i = 0; i < ios->numdevs; i++) {
153 struct osd_request *or = ios->per_dev[i].or;
154 if (unlikely(!or))
155 continue;
156
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700157 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200158 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700159 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200160 ret);
161 return ret;
162 }
163 }
164
165 kref_init(&ios->kref);
166
167 for (i = 0; i < ios->numdevs; i++) {
168 struct osd_request *or = ios->per_dev[i].or;
169 if (unlikely(!or))
170 continue;
171
172 kref_get(&ios->kref);
173 osd_execute_request_async(or, _done_io, ios);
174 }
175
176 kref_put(&ios->kref, _last_io);
177 ret = 0;
178
179 if (sync) {
180 wait_for_completion(&wait);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700181 ret = ore_check_io(ios, NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200182 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200183 return ret;
184}
185
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200186static void _clear_bio(struct bio *bio)
187{
188 struct bio_vec *bv;
189 unsigned i;
190
191 __bio_for_each_segment(bv, bio, i, 0) {
192 unsigned this_count = bv->bv_len;
193
194 if (likely(PAGE_SIZE == this_count))
195 clear_highpage(bv->bv_page);
196 else
197 zero_user(bv->bv_page, bv->bv_offset, this_count);
198 }
199}
200
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700201int ore_check_io(struct ore_io_state *ios, u64 *resid)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200202{
203 enum osd_err_priority acumulated_osd_err = 0;
204 int acumulated_lin_err = 0;
205 int i;
206
207 for (i = 0; i < ios->numdevs; i++) {
208 struct osd_sense_info osi;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200209 struct osd_request *or = ios->per_dev[i].or;
210 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200211
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200212 if (unlikely(!or))
213 continue;
214
215 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200216 if (likely(!ret))
217 continue;
218
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200219 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
220 /* start read offset passed endof file */
221 _clear_bio(ios->per_dev[i].bio);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700222 ORE_DBGMSG("start read offset passed end of file "
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200223 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200224 _LLU(ios->per_dev[i].offset),
225 _LLU(ios->per_dev[i].length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200226
227 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200228 }
229
230 if (osi.osd_err_pri >= acumulated_osd_err) {
231 acumulated_osd_err = osi.osd_err_pri;
232 acumulated_lin_err = ret;
233 }
234 }
235
236 /* TODO: raid specific residual calculations */
237 if (resid) {
238 if (likely(!acumulated_lin_err))
239 *resid = 0;
240 else
241 *resid = ios->length;
242 }
243
244 return acumulated_lin_err;
245}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700246EXPORT_SYMBOL(ore_check_io);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200247
Boaz Harroshb367e782010-02-07 19:18:58 +0200248/*
249 * L - logical offset into the file
250 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200251 * U - The number of bytes in a stripe within a group
Boaz Harroshb367e782010-02-07 19:18:58 +0200252 *
253 * U = stripe_unit * group_width
254 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200255 * T - The number of bytes striped within a group of component objects
256 * (before advancing to the next group)
Boaz Harroshb367e782010-02-07 19:18:58 +0200257 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200258 * T = stripe_unit * group_width * group_depth
259 *
260 * S - The number of bytes striped across all component objects
261 * before the pattern repeats
262 *
263 * S = stripe_unit * group_width * group_depth * group_count
264 *
265 * M - The "major" (i.e., across all components) stripe number
266 *
267 * M = L / S
268 *
269 * G - Counts the groups from the beginning of the major stripe
270 *
271 * G = (L - (M * S)) / T [or (L % S) / T]
272 *
273 * H - The byte offset within the group
274 *
275 * H = (L - (M * S)) % T [or (L % S) % T]
276 *
277 * N - The "minor" (i.e., across the group) stripe number
278 *
279 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200280 *
281 * C - The component index coresponding to L
282 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200283 * C = (H - (N * U)) / stripe_unit + G * group_width
284 * [or (L % U) / stripe_unit + G * group_width]
Boaz Harroshb367e782010-02-07 19:18:58 +0200285 *
286 * O - The component offset coresponding to L
287 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200288 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harroshb367e782010-02-07 19:18:58 +0200289 */
Boaz Harroshb367e782010-02-07 19:18:58 +0200290struct _striping_info {
291 u64 obj_offset;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200292 u64 group_length;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700293 u64 M; /* for truncate */
Boaz Harroshb367e782010-02-07 19:18:58 +0200294 unsigned dev;
295 unsigned unit_off;
296};
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200297
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700298static void _calc_stripe_info(struct ore_layout *layout, u64 file_offset,
Boaz Harroshb367e782010-02-07 19:18:58 +0200299 struct _striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200300{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700301 u32 stripe_unit = layout->stripe_unit;
302 u32 group_width = layout->group_width;
303 u64 group_depth = layout->group_depth;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200304
Boaz Harroshb367e782010-02-07 19:18:58 +0200305 u32 U = stripe_unit * group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200306 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700307 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200308 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200309
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200310 /*
311 G = (L - (M * S)) / T
312 H = (L - (M * S)) % T
313 */
314 u64 LmodS = file_offset - M * S;
315 u32 G = div64_u64(LmodS, T);
316 u64 H = LmodS - G * T;
Boaz Harroshb367e782010-02-07 19:18:58 +0200317
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200318 u32 N = div_u64(H, U);
319
320 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
321 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700322 si->dev *= layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200323
324 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
325
326 si->obj_offset = si->unit_off + (N * stripe_unit) +
327 (M * group_depth * stripe_unit);
328
329 si->group_length = T - H;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700330 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200331}
332
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700333static int _add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
334 unsigned pgbase, struct ore_per_dev_state *per_dev,
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200335 int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200336{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200337 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200338 struct request_queue *q =
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700339 osd_request_queue(_ios_od(ios, per_dev->dev));
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200340
341 per_dev->length += cur_len;
342
343 if (per_dev->bio == NULL) {
344 unsigned pages_in_stripe = ios->layout->group_width *
345 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200346 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200347 ios->layout->group_width;
348
349 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
350 if (unlikely(!per_dev->bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700351 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200352 bio_size);
353 return -ENOMEM;
354 }
355 }
356
357 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200358 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
359 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200360
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200361 BUG_ON(ios->nr_pages <= pg);
362 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200363
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200364 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
365 pglen, pgbase);
366 if (unlikely(pglen != added_len))
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200367 return -ENOMEM;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200368 pgbase = 0;
369 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200370 }
371 BUG_ON(cur_len);
372
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200373 *cur_pg = pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200374 return 0;
375}
376
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700377static int _prepare_one_group(struct ore_io_state *ios, u64 length,
Boaz Harrosh6e316092010-07-29 17:08:13 +0300378 struct _striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200379{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200380 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200381 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200382 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200383 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200384 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200385 unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
386 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200387 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200388
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200389 while (length) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700390 struct ore_per_dev_state *per_dev = &ios->per_dev[dev];
Boaz Harroshb367e782010-02-07 19:18:58 +0200391 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200392
393 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200394 per_dev->dev = dev;
395 if (dev < si->dev) {
396 per_dev->offset = si->obj_offset + stripe_unit -
397 si->unit_off;
398 cur_len = stripe_unit;
399 } else if (dev == si->dev) {
400 per_dev->offset = si->obj_offset;
401 cur_len = stripe_unit - si->unit_off;
402 page_off = si->unit_off & ~PAGE_MASK;
403 BUG_ON(page_off && (page_off != ios->pgbase));
404 } else { /* dev > si->dev */
405 per_dev->offset = si->obj_offset - si->unit_off;
406 cur_len = stripe_unit;
407 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200408
Boaz Harrosh6e316092010-07-29 17:08:13 +0300409 if (max_comp < dev)
410 max_comp = dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200411 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200412 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200413 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200414 if (cur_len >= length)
415 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200416
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200417 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
418 cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200419 if (unlikely(ret))
420 goto out;
421
Boaz Harrosh6e316092010-07-29 17:08:13 +0300422 dev += mirrors_p1;
423 dev = (dev % devs_in_group) + first_dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200424
425 length -= cur_len;
426 }
427out:
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200428 ios->numdevs = max_comp + mirrors_p1;
429 ios->pages_consumed = cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200430 return ret;
431}
432
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700433static int _prepare_for_striping(struct ore_io_state *ios)
Boaz Harroshb367e782010-02-07 19:18:58 +0200434{
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200435 u64 length = ios->length;
Boaz Harrosh5002dd12010-08-02 20:06:46 +0300436 u64 offset = ios->offset;
Boaz Harroshb367e782010-02-07 19:18:58 +0200437 struct _striping_info si;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200438 int ret = 0;
Boaz Harroshb367e782010-02-07 19:18:58 +0200439
Boaz Harroshb367e782010-02-07 19:18:58 +0200440 if (!ios->pages) {
441 if (ios->kern_buff) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700442 struct ore_per_dev_state *per_dev = &ios->per_dev[0];
Boaz Harroshb367e782010-02-07 19:18:58 +0200443
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700444 _calc_stripe_info(ios->layout, ios->offset, &si);
Boaz Harroshb367e782010-02-07 19:18:58 +0200445 per_dev->offset = si.obj_offset;
446 per_dev->dev = si.dev;
447
448 /* no cross device without page array */
449 BUG_ON((ios->layout->group_width > 1) &&
450 (si.unit_off + ios->length >
451 ios->layout->stripe_unit));
452 }
453 ios->numdevs = ios->layout->mirrors_p1;
454 return 0;
455 }
456
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200457 while (length) {
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700458 _calc_stripe_info(ios->layout, offset, &si);
Boaz Harrosh5002dd12010-08-02 20:06:46 +0300459
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200460 if (length < si.group_length)
461 si.group_length = length;
462
Boaz Harrosh6e316092010-07-29 17:08:13 +0300463 ret = _prepare_one_group(ios, si.group_length, &si);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200464 if (unlikely(ret))
465 goto out;
466
Boaz Harrosh5002dd12010-08-02 20:06:46 +0300467 offset += si.group_length;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200468 length -= si.group_length;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200469 }
470
471out:
472 return ret;
Boaz Harroshb367e782010-02-07 19:18:58 +0200473}
474
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700475int ore_create(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200476{
477 int i, ret;
478
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700479 for (i = 0; i < ios->comps->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200480 struct osd_request *or;
481
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700482 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200483 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700484 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200485 ret = -ENOMEM;
486 goto out;
487 }
488 ios->per_dev[i].or = or;
489 ios->numdevs++;
490
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700491 osd_req_create_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200492 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700493 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200494
495out:
496 return ret;
497}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700498EXPORT_SYMBOL(ore_create);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200499
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700500int ore_remove(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200501{
502 int i, ret;
503
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700504 for (i = 0; i < ios->comps->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200505 struct osd_request *or;
506
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700507 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200508 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700509 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200510 ret = -ENOMEM;
511 goto out;
512 }
513 ios->per_dev[i].or = or;
514 ios->numdevs++;
515
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700516 osd_req_remove_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200517 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700518 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200519
520out:
521 return ret;
522}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700523EXPORT_SYMBOL(ore_remove);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200524
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700525static int _write_mirror(struct ore_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200526{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700527 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200528 unsigned dev = ios->per_dev[cur_comp].dev;
529 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
530 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200531
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200532 if (ios->pages && !master_dev->length)
533 return 0; /* Just an empty slot */
534
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200535 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700536 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200537 struct osd_request *or;
538
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700539 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200540 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700541 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200542 ret = -ENOMEM;
543 goto out;
544 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200545 per_dev->or = or;
546 per_dev->offset = master_dev->offset;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200547
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200548 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200549 struct bio *bio;
550
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200551 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200552 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200553 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200554 if (unlikely(!bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700555 ORE_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200556 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200557 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200558 ret = -ENOMEM;
559 goto out;
560 }
561
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200562 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200563 bio->bi_bdev = NULL;
564 bio->bi_next = NULL;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200565 per_dev->length = master_dev->length;
566 per_dev->bio = bio;
567 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200568 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200569 bio = master_dev->bio;
570 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200571 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200572 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200573
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700574 osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
575 bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700576 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200577 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700578 _LLU(_ios_obj(ios, dev)->id),
579 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200580 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200581 } else if (ios->kern_buff) {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700582 ret = osd_req_write_kern(or, _ios_obj(ios, dev),
583 per_dev->offset,
584 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200585 if (unlikely(ret))
586 goto out;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700587 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200588 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700589 _LLU(_ios_obj(ios, dev)->id),
590 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200591 _LLU(ios->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200592 } else {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700593 osd_req_set_attributes(or, _ios_obj(ios, dev));
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700594 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700595 _LLU(_ios_obj(ios, dev)->id),
596 ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200597 }
598
599 if (ios->out_attr)
600 osd_req_add_set_attr_list(or, ios->out_attr,
601 ios->out_attr_len);
602
603 if (ios->in_attr)
604 osd_req_add_get_attr_list(or, ios->in_attr,
605 ios->in_attr_len);
606 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200607
608out:
609 return ret;
610}
611
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700612int ore_write(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200613{
614 int i;
615 int ret;
616
617 ret = _prepare_for_striping(ios);
618 if (unlikely(ret))
619 return ret;
620
621 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700622 ret = _write_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200623 if (unlikely(ret))
624 return ret;
625 }
626
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700627 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200628 return ret;
629}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700630EXPORT_SYMBOL(ore_write);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200631
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700632static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200633{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200634 struct osd_request *or;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700635 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700636 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
637 unsigned first_dev = (unsigned)obj->id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200638
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200639 if (ios->pages && !per_dev->length)
640 return 0; /* Just an empty slot */
641
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200642 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700643 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200644 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700645 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200646 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200647 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200648 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200649
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200650 if (ios->pages) {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700651 osd_req_read(or, obj, per_dev->offset,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200652 per_dev->bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700653 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700654 " dev=%d\n", _LLU(obj->id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200655 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200656 first_dev);
657 } else if (ios->kern_buff) {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700658 int ret = osd_req_read_kern(or, obj, per_dev->offset,
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200659 ios->kern_buff, ios->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700660 ORE_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200661 "length=0x%llx dev=%d ret=>%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700662 _LLU(obj->id), _LLU(per_dev->offset),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200663 _LLU(ios->length), first_dev, ret);
664 if (unlikely(ret))
665 return ret;
666 } else {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700667 osd_req_get_attributes(or, obj);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700668 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700669 _LLU(obj->id),
670 ios->in_attr_len, first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200671 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200672 if (ios->out_attr)
673 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
674
675 if (ios->in_attr)
676 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
677
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200678 return 0;
679}
680
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700681int ore_read(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200682{
683 int i;
684 int ret;
685
686 ret = _prepare_for_striping(ios);
687 if (unlikely(ret))
688 return ret;
689
690 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700691 ret = _read_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200692 if (unlikely(ret))
693 return ret;
694 }
695
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700696 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200697 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200698}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700699EXPORT_SYMBOL(ore_read);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200700
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700701int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200702{
703 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
704 void *iter = NULL;
705 int nelem;
706
707 do {
708 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200709 osd_req_decode_get_attr_list(ios->per_dev[0].or,
710 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200711 if ((cur_attr.attr_page == attr->attr_page) &&
712 (cur_attr.attr_id == attr->attr_id)) {
713 attr->len = cur_attr.len;
714 attr->val_ptr = cur_attr.val_ptr;
715 return 0;
716 }
717 } while (iter);
718
719 return -EIO;
720}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700721EXPORT_SYMBOL(extract_attr_from_ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200722
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700723static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200724 struct osd_attr *attr)
725{
726 int last_comp = cur_comp + ios->layout->mirrors_p1;
727
728 for (; cur_comp < last_comp; ++cur_comp) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700729 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200730 struct osd_request *or;
731
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700732 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200733 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700734 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200735 return -ENOMEM;
736 }
737 per_dev->or = or;
738
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700739 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200740 osd_req_add_set_attr_list(or, attr, 1);
741 }
742
743 return 0;
744}
745
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700746struct _trunc_info {
747 struct _striping_info si;
748 u64 prev_group_obj_off;
749 u64 next_group_obj_off;
750
751 unsigned first_group_dev;
752 unsigned nex_group_dev;
753 unsigned max_devs;
754};
755
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700756void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700757 struct _trunc_info *ti)
758{
759 unsigned stripe_unit = layout->stripe_unit;
760
761 _calc_stripe_info(layout, file_offset, &ti->si);
762
763 ti->prev_group_obj_off = ti->si.M * stripe_unit;
764 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
765
766 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
767 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
768 ti->max_devs = layout->group_width * layout->group_count;
769}
770
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700771int ore_truncate(struct ore_layout *layout, struct ore_components *comps,
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700772 u64 size)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200773{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700774 struct ore_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200775 struct exofs_trunc_attr {
776 struct osd_attr attr;
777 __be64 newsize;
778 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700779 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200780 int i, ret;
781
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700782 ret = ore_get_io_state(layout, comps, &ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200783 if (unlikely(ret))
784 return ret;
785
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700786 _calc_trunk_info(ios->layout, size, &ti);
787
788 size_attrs = kcalloc(ti.max_devs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200789 GFP_KERNEL);
790 if (unlikely(!size_attrs)) {
791 ret = -ENOMEM;
792 goto out;
793 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200794
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700795 ios->numdevs = ios->comps->numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200796
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700797 for (i = 0; i < ti.max_devs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200798 struct exofs_trunc_attr *size_attr = &size_attrs[i];
799 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200800
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700801 if (i < ti.first_group_dev)
802 obj_size = ti.prev_group_obj_off;
803 else if (i >= ti.nex_group_dev)
804 obj_size = ti.next_group_obj_off;
805 else if (i < ti.si.dev) /* dev within this group */
806 obj_size = ti.si.obj_offset +
807 ios->layout->stripe_unit - ti.si.unit_off;
808 else if (i == ti.si.dev)
809 obj_size = ti.si.obj_offset;
810 else /* i > ti.dev */
811 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200812
813 size_attr->newsize = cpu_to_be64(obj_size);
814 size_attr->attr = g_attr_logical_length;
815 size_attr->attr.val_ptr = &size_attr->newsize;
816
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700817 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700818 _LLU(comps->comps->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200819 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
820 &size_attr->attr);
821 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200822 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200823 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700824 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200825
826out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200827 kfree(size_attrs);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700828 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200829 return ret;
830}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700831EXPORT_SYMBOL(ore_truncate);
Boaz Harrosh85e44df2011-05-16 15:26:47 +0300832
833const struct osd_attr g_attr_logical_length = ATTR_DEF(
834 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700835EXPORT_SYMBOL(g_attr_logical_length);