blob: c1c2cc607adf592f888e67320e1cba58c1474a77 [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 Harroshb916c5c2011-09-28 11:55:51 +030050static void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
51 struct ore_striping_info *si);
52
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070053static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -070054{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +030055 return ios->oc->comps[index & ios->oc->single_comp].cred;
Boaz Harrosh9e9db452011-08-05 15:06:04 -070056}
57
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070058static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -070059{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +030060 return &ios->oc->comps[index & ios->oc->single_comp].obj;
Boaz Harrosh9e9db452011-08-05 15:06:04 -070061}
62
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070063static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -070064{
Boaz Harroshd866d872011-09-28 14:43:09 +030065 return ore_comp_dev(ios->oc, index);
Boaz Harrosh9e9db452011-08-05 15:06:04 -070066}
67
Boaz Harroshb916c5c2011-09-28 11:55:51 +030068static int _get_io_state(struct ore_layout *layout,
69 struct ore_components *oc, unsigned numdevs,
70 struct ore_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020071{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070072 struct ore_io_state *ios;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020073
Boaz Harrosh06886a52009-11-08 14:54:08 +020074 /*TODO: Maybe use kmem_cach per sbi of size
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020075 * exofs_io_state_size(layout->s_numdevs)
Boaz Harrosh06886a52009-11-08 14:54:08 +020076 */
Boaz Harroshb916c5c2011-09-28 11:55:51 +030077 ios = kzalloc(ore_io_state_size(numdevs), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +020078 if (unlikely(!ios)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070079 ORE_DBGMSG("Failed kzalloc bytes=%d\n",
Boaz Harroshb916c5c2011-09-28 11:55:51 +030080 ore_io_state_size(numdevs));
Boaz Harrosh06886a52009-11-08 14:54:08 +020081 *pios = NULL;
82 return -ENOMEM;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020083 }
84
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020085 ios->layout = layout;
Boaz Harrosh5bf696d2011-09-28 11:39:59 +030086 ios->oc = oc;
Boaz Harrosh06886a52009-11-08 14:54:08 +020087 *pios = ios;
88 return 0;
89}
Boaz Harroshb916c5c2011-09-28 11:55:51 +030090
91/* Allocate an io_state for only a single group of devices
92 *
93 * If a user needs to call ore_read/write() this version must be used becase it
94 * allocates extra stuff for striping and raid.
95 * The ore might decide to only IO less then @length bytes do to alignmets
96 * and constrains as follows:
97 * - The IO cannot cross group boundary.
98 * - In raid5/6 The end of the IO must align at end of a stripe eg.
99 * (@offset + @length) % strip_size == 0. Or the complete range is within a
100 * single stripe.
101 * - Memory condition only permitted a shorter IO. (A user can use @length=~0
102 * And check the returned ios->length for max_io_size.)
103 *
104 * The caller must check returned ios->length (and/or ios->nr_pages) and
105 * re-issue these pages that fall outside of ios->length
106 */
107int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
108 bool is_reading, u64 offset, u64 length,
109 struct ore_io_state **pios)
110{
111 struct ore_io_state *ios;
112 unsigned numdevs = layout->group_width * layout->mirrors_p1;
113 int ret;
114
115 ret = _get_io_state(layout, oc, numdevs, pios);
116 if (unlikely(ret))
117 return ret;
118
119 ios = *pios;
120 ios->reading = is_reading;
121 ios->offset = offset;
122
123 if (length) {
124 struct ore_striping_info si;
125
126 ore_calc_stripe_info(layout, offset, &si);
127 ios->length = (length <= si.group_length) ? length :
128 si.group_length;
129 ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
130 }
131
132 return 0;
133}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700134EXPORT_SYMBOL(ore_get_rw_state);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200135
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300136/* Allocate an io_state for all the devices in the comps array
137 *
138 * This version of io_state allocation is used mostly by create/remove
139 * and trunc where we currently need all the devices. The only wastful
140 * bit is the read/write_attributes with no IO. Those sites should
141 * be converted to use ore_get_rw_state() with length=0
142 */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300143int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300144 struct ore_io_state **pios)
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200145{
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300146 return _get_io_state(layout, oc, oc->numdevs, pios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200147}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700148EXPORT_SYMBOL(ore_get_io_state);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200149
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700150void ore_put_io_state(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200151{
152 if (ios) {
153 unsigned i;
154
155 for (i = 0; i < ios->numdevs; i++) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700156 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200157
158 if (per_dev->or)
159 osd_end_request(per_dev->or);
160 if (per_dev->bio)
161 bio_put(per_dev->bio);
162 }
163
164 kfree(ios);
165 }
166}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700167EXPORT_SYMBOL(ore_put_io_state);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200168
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700169static void _sync_done(struct ore_io_state *ios, void *p)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200170{
171 struct completion *waiting = p;
172
173 complete(waiting);
174}
175
176static void _last_io(struct kref *kref)
177{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700178 struct ore_io_state *ios = container_of(
179 kref, struct ore_io_state, kref);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200180
181 ios->done(ios, ios->private);
182}
183
184static void _done_io(struct osd_request *or, void *p)
185{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700186 struct ore_io_state *ios = p;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200187
188 kref_put(&ios->kref, _last_io);
189}
190
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700191static int ore_io_execute(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200192{
193 DECLARE_COMPLETION_ONSTACK(wait);
194 bool sync = (ios->done == NULL);
195 int i, ret;
196
197 if (sync) {
198 ios->done = _sync_done;
199 ios->private = &wait;
200 }
201
202 for (i = 0; i < ios->numdevs; i++) {
203 struct osd_request *or = ios->per_dev[i].or;
204 if (unlikely(!or))
205 continue;
206
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700207 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200208 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700209 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200210 ret);
211 return ret;
212 }
213 }
214
215 kref_init(&ios->kref);
216
217 for (i = 0; i < ios->numdevs; i++) {
218 struct osd_request *or = ios->per_dev[i].or;
219 if (unlikely(!or))
220 continue;
221
222 kref_get(&ios->kref);
223 osd_execute_request_async(or, _done_io, ios);
224 }
225
226 kref_put(&ios->kref, _last_io);
227 ret = 0;
228
229 if (sync) {
230 wait_for_completion(&wait);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700231 ret = ore_check_io(ios, NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200232 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200233 return ret;
234}
235
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200236static void _clear_bio(struct bio *bio)
237{
238 struct bio_vec *bv;
239 unsigned i;
240
241 __bio_for_each_segment(bv, bio, i, 0) {
242 unsigned this_count = bv->bv_len;
243
244 if (likely(PAGE_SIZE == this_count))
245 clear_highpage(bv->bv_page);
246 else
247 zero_user(bv->bv_page, bv->bv_offset, this_count);
248 }
249}
250
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700251int ore_check_io(struct ore_io_state *ios, u64 *resid)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200252{
253 enum osd_err_priority acumulated_osd_err = 0;
254 int acumulated_lin_err = 0;
255 int i;
256
257 for (i = 0; i < ios->numdevs; i++) {
258 struct osd_sense_info osi;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200259 struct osd_request *or = ios->per_dev[i].or;
260 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200261
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200262 if (unlikely(!or))
263 continue;
264
265 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200266 if (likely(!ret))
267 continue;
268
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200269 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
270 /* start read offset passed endof file */
271 _clear_bio(ios->per_dev[i].bio);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700272 ORE_DBGMSG("start read offset passed end of file "
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200273 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200274 _LLU(ios->per_dev[i].offset),
275 _LLU(ios->per_dev[i].length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200276
277 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200278 }
279
280 if (osi.osd_err_pri >= acumulated_osd_err) {
281 acumulated_osd_err = osi.osd_err_pri;
282 acumulated_lin_err = ret;
283 }
284 }
285
286 /* TODO: raid specific residual calculations */
287 if (resid) {
288 if (likely(!acumulated_lin_err))
289 *resid = 0;
290 else
291 *resid = ios->length;
292 }
293
294 return acumulated_lin_err;
295}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700296EXPORT_SYMBOL(ore_check_io);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200297
Boaz Harroshb367e782010-02-07 19:18:58 +0200298/*
299 * L - logical offset into the file
300 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200301 * U - The number of bytes in a stripe within a group
Boaz Harroshb367e782010-02-07 19:18:58 +0200302 *
303 * U = stripe_unit * group_width
304 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200305 * T - The number of bytes striped within a group of component objects
306 * (before advancing to the next group)
Boaz Harroshb367e782010-02-07 19:18:58 +0200307 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200308 * T = stripe_unit * group_width * group_depth
309 *
310 * S - The number of bytes striped across all component objects
311 * before the pattern repeats
312 *
313 * S = stripe_unit * group_width * group_depth * group_count
314 *
315 * M - The "major" (i.e., across all components) stripe number
316 *
317 * M = L / S
318 *
319 * G - Counts the groups from the beginning of the major stripe
320 *
321 * G = (L - (M * S)) / T [or (L % S) / T]
322 *
323 * H - The byte offset within the group
324 *
325 * H = (L - (M * S)) % T [or (L % S) % T]
326 *
327 * N - The "minor" (i.e., across the group) stripe number
328 *
329 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200330 *
331 * C - The component index coresponding to L
332 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200333 * C = (H - (N * U)) / stripe_unit + G * group_width
334 * [or (L % U) / stripe_unit + G * group_width]
Boaz Harroshb367e782010-02-07 19:18:58 +0200335 *
336 * O - The component offset coresponding to L
337 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200338 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harroshb367e782010-02-07 19:18:58 +0200339 */
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700340static void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
341 struct ore_striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200342{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700343 u32 stripe_unit = layout->stripe_unit;
344 u32 group_width = layout->group_width;
345 u64 group_depth = layout->group_depth;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200346
Boaz Harroshb367e782010-02-07 19:18:58 +0200347 u32 U = stripe_unit * group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200348 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700349 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200350 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200351
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200352 /*
353 G = (L - (M * S)) / T
354 H = (L - (M * S)) % T
355 */
356 u64 LmodS = file_offset - M * S;
357 u32 G = div64_u64(LmodS, T);
358 u64 H = LmodS - G * T;
Boaz Harroshb367e782010-02-07 19:18:58 +0200359
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200360 u32 N = div_u64(H, U);
361
362 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
363 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700364 si->dev *= layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200365
366 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
367
368 si->obj_offset = si->unit_off + (N * stripe_unit) +
369 (M * group_depth * stripe_unit);
370
371 si->group_length = T - H;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700372 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200373}
374
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700375static int _add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
376 unsigned pgbase, struct ore_per_dev_state *per_dev,
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200377 int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200378{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200379 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200380 struct request_queue *q =
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700381 osd_request_queue(_ios_od(ios, per_dev->dev));
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200382
383 per_dev->length += cur_len;
384
385 if (per_dev->bio == NULL) {
386 unsigned pages_in_stripe = ios->layout->group_width *
387 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200388 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200389 ios->layout->group_width;
390
391 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
392 if (unlikely(!per_dev->bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700393 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200394 bio_size);
395 return -ENOMEM;
396 }
397 }
398
399 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200400 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
401 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200402
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200403 BUG_ON(ios->nr_pages <= pg);
404 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200405
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200406 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
407 pglen, pgbase);
408 if (unlikely(pglen != added_len))
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200409 return -ENOMEM;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200410 pgbase = 0;
411 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200412 }
413 BUG_ON(cur_len);
414
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200415 *cur_pg = pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200416 return 0;
417}
418
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700419static int _prepare_one_group(struct ore_io_state *ios, u64 length,
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700420 struct ore_striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200421{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200422 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200423 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200424 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200425 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200426 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200427 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200428 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200429
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200430 while (length) {
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300431 unsigned comp = dev - first_dev;
432 struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
Boaz Harroshb367e782010-02-07 19:18:58 +0200433 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200434
435 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200436 per_dev->dev = dev;
437 if (dev < si->dev) {
438 per_dev->offset = si->obj_offset + stripe_unit -
439 si->unit_off;
440 cur_len = stripe_unit;
441 } else if (dev == si->dev) {
442 per_dev->offset = si->obj_offset;
443 cur_len = stripe_unit - si->unit_off;
444 page_off = si->unit_off & ~PAGE_MASK;
445 BUG_ON(page_off && (page_off != ios->pgbase));
446 } else { /* dev > si->dev */
447 per_dev->offset = si->obj_offset - si->unit_off;
448 cur_len = stripe_unit;
449 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200450 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200451 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200452 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200453 if (cur_len >= length)
454 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200455
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200456 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
457 cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200458 if (unlikely(ret))
459 goto out;
460
Boaz Harrosh6e316092010-07-29 17:08:13 +0300461 dev += mirrors_p1;
462 dev = (dev % devs_in_group) + first_dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200463
464 length -= cur_len;
465 }
466out:
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300467 ios->numdevs = devs_in_group;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200468 ios->pages_consumed = cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200469 return ret;
470}
471
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700472static int _prepare_for_striping(struct ore_io_state *ios)
Boaz Harroshb367e782010-02-07 19:18:58 +0200473{
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700474 struct ore_striping_info si;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300475 int ret;
Boaz Harroshb367e782010-02-07 19:18:58 +0200476
Boaz Harroshb367e782010-02-07 19:18:58 +0200477 if (!ios->pages) {
478 if (ios->kern_buff) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700479 struct ore_per_dev_state *per_dev = &ios->per_dev[0];
Boaz Harroshb367e782010-02-07 19:18:58 +0200480
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700481 ore_calc_stripe_info(ios->layout, ios->offset, &si);
Boaz Harroshb367e782010-02-07 19:18:58 +0200482 per_dev->offset = si.obj_offset;
483 per_dev->dev = si.dev;
484
485 /* no cross device without page array */
486 BUG_ON((ios->layout->group_width > 1) &&
487 (si.unit_off + ios->length >
488 ios->layout->stripe_unit));
489 }
490 ios->numdevs = ios->layout->mirrors_p1;
491 return 0;
492 }
493
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300494 ore_calc_stripe_info(ios->layout, ios->offset, &si);
Boaz Harrosh5002dd12010-08-02 20:06:46 +0300495
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300496 BUG_ON(ios->length > si.group_length);
497 ret = _prepare_one_group(ios, ios->length, &si);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200498
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200499 return ret;
Boaz Harroshb367e782010-02-07 19:18:58 +0200500}
501
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700502int ore_create(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200503{
504 int i, ret;
505
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300506 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200507 struct osd_request *or;
508
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700509 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200510 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700511 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200512 ret = -ENOMEM;
513 goto out;
514 }
515 ios->per_dev[i].or = or;
516 ios->numdevs++;
517
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700518 osd_req_create_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200519 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700520 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200521
522out:
523 return ret;
524}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700525EXPORT_SYMBOL(ore_create);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200526
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700527int ore_remove(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200528{
529 int i, ret;
530
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300531 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200532 struct osd_request *or;
533
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700534 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200535 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700536 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200537 ret = -ENOMEM;
538 goto out;
539 }
540 ios->per_dev[i].or = or;
541 ios->numdevs++;
542
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700543 osd_req_remove_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200544 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700545 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200546
547out:
548 return ret;
549}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700550EXPORT_SYMBOL(ore_remove);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200551
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700552static int _write_mirror(struct ore_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200553{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700554 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200555 unsigned dev = ios->per_dev[cur_comp].dev;
556 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
557 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200558
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200559 if (ios->pages && !master_dev->length)
560 return 0; /* Just an empty slot */
561
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200562 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700563 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200564 struct osd_request *or;
565
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700566 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200567 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700568 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200569 ret = -ENOMEM;
570 goto out;
571 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200572 per_dev->or = or;
573 per_dev->offset = master_dev->offset;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200574
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200575 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200576 struct bio *bio;
577
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200578 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200579 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200580 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200581 if (unlikely(!bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700582 ORE_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200583 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200584 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200585 ret = -ENOMEM;
586 goto out;
587 }
588
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200589 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200590 bio->bi_bdev = NULL;
591 bio->bi_next = NULL;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200592 per_dev->length = master_dev->length;
593 per_dev->bio = bio;
594 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200595 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200596 bio = master_dev->bio;
597 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200598 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200599 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200600
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700601 osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
602 bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700603 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200604 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700605 _LLU(_ios_obj(ios, dev)->id),
606 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200607 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200608 } else if (ios->kern_buff) {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700609 ret = osd_req_write_kern(or, _ios_obj(ios, dev),
610 per_dev->offset,
611 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200612 if (unlikely(ret))
613 goto out;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700614 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200615 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700616 _LLU(_ios_obj(ios, dev)->id),
617 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200618 _LLU(ios->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200619 } else {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700620 osd_req_set_attributes(or, _ios_obj(ios, dev));
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700621 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700622 _LLU(_ios_obj(ios, dev)->id),
623 ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200624 }
625
626 if (ios->out_attr)
627 osd_req_add_set_attr_list(or, ios->out_attr,
628 ios->out_attr_len);
629
630 if (ios->in_attr)
631 osd_req_add_get_attr_list(or, ios->in_attr,
632 ios->in_attr_len);
633 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200634
635out:
636 return ret;
637}
638
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700639int ore_write(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200640{
641 int i;
642 int ret;
643
644 ret = _prepare_for_striping(ios);
645 if (unlikely(ret))
646 return ret;
647
648 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700649 ret = _write_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200650 if (unlikely(ret))
651 return ret;
652 }
653
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700654 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200655 return ret;
656}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700657EXPORT_SYMBOL(ore_write);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200658
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700659static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200660{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200661 struct osd_request *or;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700662 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700663 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
664 unsigned first_dev = (unsigned)obj->id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200665
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200666 if (ios->pages && !per_dev->length)
667 return 0; /* Just an empty slot */
668
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200669 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700670 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200671 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700672 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200673 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200674 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200675 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200676
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200677 if (ios->pages) {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700678 osd_req_read(or, obj, per_dev->offset,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200679 per_dev->bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700680 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700681 " dev=%d\n", _LLU(obj->id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200682 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200683 first_dev);
684 } else if (ios->kern_buff) {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700685 int ret = osd_req_read_kern(or, obj, per_dev->offset,
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200686 ios->kern_buff, ios->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700687 ORE_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200688 "length=0x%llx dev=%d ret=>%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700689 _LLU(obj->id), _LLU(per_dev->offset),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200690 _LLU(ios->length), first_dev, ret);
691 if (unlikely(ret))
692 return ret;
693 } else {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700694 osd_req_get_attributes(or, obj);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700695 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700696 _LLU(obj->id),
697 ios->in_attr_len, first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200698 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200699 if (ios->out_attr)
700 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
701
702 if (ios->in_attr)
703 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
704
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200705 return 0;
706}
707
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700708int ore_read(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200709{
710 int i;
711 int ret;
712
713 ret = _prepare_for_striping(ios);
714 if (unlikely(ret))
715 return ret;
716
717 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700718 ret = _read_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200719 if (unlikely(ret))
720 return ret;
721 }
722
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700723 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200724 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200725}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700726EXPORT_SYMBOL(ore_read);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200727
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700728int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200729{
730 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
731 void *iter = NULL;
732 int nelem;
733
734 do {
735 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200736 osd_req_decode_get_attr_list(ios->per_dev[0].or,
737 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200738 if ((cur_attr.attr_page == attr->attr_page) &&
739 (cur_attr.attr_id == attr->attr_id)) {
740 attr->len = cur_attr.len;
741 attr->val_ptr = cur_attr.val_ptr;
742 return 0;
743 }
744 } while (iter);
745
746 return -EIO;
747}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700748EXPORT_SYMBOL(extract_attr_from_ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200749
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700750static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200751 struct osd_attr *attr)
752{
753 int last_comp = cur_comp + ios->layout->mirrors_p1;
754
755 for (; cur_comp < last_comp; ++cur_comp) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700756 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200757 struct osd_request *or;
758
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700759 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200760 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700761 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200762 return -ENOMEM;
763 }
764 per_dev->or = or;
765
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700766 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200767 osd_req_add_set_attr_list(or, attr, 1);
768 }
769
770 return 0;
771}
772
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700773struct _trunc_info {
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700774 struct ore_striping_info si;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700775 u64 prev_group_obj_off;
776 u64 next_group_obj_off;
777
778 unsigned first_group_dev;
779 unsigned nex_group_dev;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700780};
781
H Hartley Sweeten1958c7c22011-09-23 13:42:43 -0700782static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
783 struct _trunc_info *ti)
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700784{
785 unsigned stripe_unit = layout->stripe_unit;
786
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700787 ore_calc_stripe_info(layout, file_offset, &ti->si);
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700788
789 ti->prev_group_obj_off = ti->si.M * stripe_unit;
790 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
791
792 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
793 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700794}
795
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300796int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700797 u64 size)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200798{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700799 struct ore_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200800 struct exofs_trunc_attr {
801 struct osd_attr attr;
802 __be64 newsize;
803 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700804 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200805 int i, ret;
806
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300807 ret = ore_get_io_state(layout, oc, &ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200808 if (unlikely(ret))
809 return ret;
810
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700811 _calc_trunk_info(ios->layout, size, &ti);
812
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300813 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200814 GFP_KERNEL);
815 if (unlikely(!size_attrs)) {
816 ret = -ENOMEM;
817 goto out;
818 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200819
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300820 ios->numdevs = ios->oc->numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200821
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300822 for (i = 0; i < ios->numdevs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200823 struct exofs_trunc_attr *size_attr = &size_attrs[i];
824 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200825
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700826 if (i < ti.first_group_dev)
827 obj_size = ti.prev_group_obj_off;
828 else if (i >= ti.nex_group_dev)
829 obj_size = ti.next_group_obj_off;
830 else if (i < ti.si.dev) /* dev within this group */
831 obj_size = ti.si.obj_offset +
832 ios->layout->stripe_unit - ti.si.unit_off;
833 else if (i == ti.si.dev)
834 obj_size = ti.si.obj_offset;
835 else /* i > ti.dev */
836 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200837
838 size_attr->newsize = cpu_to_be64(obj_size);
839 size_attr->attr = g_attr_logical_length;
840 size_attr->attr.val_ptr = &size_attr->newsize;
841
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700842 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300843 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200844 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
845 &size_attr->attr);
846 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200847 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200848 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700849 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200850
851out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200852 kfree(size_attrs);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700853 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200854 return ret;
855}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700856EXPORT_SYMBOL(ore_truncate);
Boaz Harrosh85e44df2011-05-16 15:26:47 +0300857
858const struct osd_attr g_attr_logical_length = ATTR_DEF(
859 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700860EXPORT_SYMBOL(g_attr_logical_length);