blob: d0f8db5e46c5ce6ed75fec917476e50a7db0ece2 [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 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) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200124 ore_calc_stripe_info(layout, offset, &ios->si);
125 ios->length = (length <= ios->si.group_length) ? length :
126 ios->si.group_length;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300127 ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
128 }
129
130 return 0;
131}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700132EXPORT_SYMBOL(ore_get_rw_state);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200133
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300134/* Allocate an io_state for all the devices in the comps array
135 *
136 * This version of io_state allocation is used mostly by create/remove
137 * and trunc where we currently need all the devices. The only wastful
138 * bit is the read/write_attributes with no IO. Those sites should
139 * be converted to use ore_get_rw_state() with length=0
140 */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300141int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300142 struct ore_io_state **pios)
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200143{
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300144 return _get_io_state(layout, oc, oc->numdevs, pios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200145}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700146EXPORT_SYMBOL(ore_get_io_state);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200147
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700148void ore_put_io_state(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200149{
150 if (ios) {
151 unsigned i;
152
153 for (i = 0; i < ios->numdevs; i++) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700154 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200155
156 if (per_dev->or)
157 osd_end_request(per_dev->or);
158 if (per_dev->bio)
159 bio_put(per_dev->bio);
160 }
161
162 kfree(ios);
163 }
164}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700165EXPORT_SYMBOL(ore_put_io_state);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200166
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700167static void _sync_done(struct ore_io_state *ios, void *p)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200168{
169 struct completion *waiting = p;
170
171 complete(waiting);
172}
173
174static void _last_io(struct kref *kref)
175{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700176 struct ore_io_state *ios = container_of(
177 kref, struct ore_io_state, kref);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200178
179 ios->done(ios, ios->private);
180}
181
182static void _done_io(struct osd_request *or, void *p)
183{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700184 struct ore_io_state *ios = p;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200185
186 kref_put(&ios->kref, _last_io);
187}
188
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700189static int ore_io_execute(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200190{
191 DECLARE_COMPLETION_ONSTACK(wait);
192 bool sync = (ios->done == NULL);
193 int i, ret;
194
195 if (sync) {
196 ios->done = _sync_done;
197 ios->private = &wait;
198 }
199
200 for (i = 0; i < ios->numdevs; i++) {
201 struct osd_request *or = ios->per_dev[i].or;
202 if (unlikely(!or))
203 continue;
204
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700205 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200206 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700207 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200208 ret);
209 return ret;
210 }
211 }
212
213 kref_init(&ios->kref);
214
215 for (i = 0; i < ios->numdevs; i++) {
216 struct osd_request *or = ios->per_dev[i].or;
217 if (unlikely(!or))
218 continue;
219
220 kref_get(&ios->kref);
221 osd_execute_request_async(or, _done_io, ios);
222 }
223
224 kref_put(&ios->kref, _last_io);
225 ret = 0;
226
227 if (sync) {
228 wait_for_completion(&wait);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700229 ret = ore_check_io(ios, NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200230 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200231 return ret;
232}
233
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200234static void _clear_bio(struct bio *bio)
235{
236 struct bio_vec *bv;
237 unsigned i;
238
239 __bio_for_each_segment(bv, bio, i, 0) {
240 unsigned this_count = bv->bv_len;
241
242 if (likely(PAGE_SIZE == this_count))
243 clear_highpage(bv->bv_page);
244 else
245 zero_user(bv->bv_page, bv->bv_offset, this_count);
246 }
247}
248
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700249int ore_check_io(struct ore_io_state *ios, u64 *resid)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200250{
251 enum osd_err_priority acumulated_osd_err = 0;
252 int acumulated_lin_err = 0;
253 int i;
254
255 for (i = 0; i < ios->numdevs; i++) {
256 struct osd_sense_info osi;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200257 struct osd_request *or = ios->per_dev[i].or;
258 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200259
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200260 if (unlikely(!or))
261 continue;
262
263 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200264 if (likely(!ret))
265 continue;
266
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200267 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
268 /* start read offset passed endof file */
269 _clear_bio(ios->per_dev[i].bio);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700270 ORE_DBGMSG("start read offset passed end of file "
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200271 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200272 _LLU(ios->per_dev[i].offset),
273 _LLU(ios->per_dev[i].length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200274
275 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200276 }
277
278 if (osi.osd_err_pri >= acumulated_osd_err) {
279 acumulated_osd_err = osi.osd_err_pri;
280 acumulated_lin_err = ret;
281 }
282 }
283
284 /* TODO: raid specific residual calculations */
285 if (resid) {
286 if (likely(!acumulated_lin_err))
287 *resid = 0;
288 else
289 *resid = ios->length;
290 }
291
292 return acumulated_lin_err;
293}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700294EXPORT_SYMBOL(ore_check_io);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200295
Boaz Harroshb367e782010-02-07 19:18:58 +0200296/*
297 * L - logical offset into the file
298 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200299 * U - The number of bytes in a stripe within a group
Boaz Harroshb367e782010-02-07 19:18:58 +0200300 *
301 * U = stripe_unit * group_width
302 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200303 * T - The number of bytes striped within a group of component objects
304 * (before advancing to the next group)
Boaz Harroshb367e782010-02-07 19:18:58 +0200305 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200306 * T = stripe_unit * group_width * group_depth
307 *
308 * S - The number of bytes striped across all component objects
309 * before the pattern repeats
310 *
311 * S = stripe_unit * group_width * group_depth * group_count
312 *
313 * M - The "major" (i.e., across all components) stripe number
314 *
315 * M = L / S
316 *
317 * G - Counts the groups from the beginning of the major stripe
318 *
319 * G = (L - (M * S)) / T [or (L % S) / T]
320 *
321 * H - The byte offset within the group
322 *
323 * H = (L - (M * S)) % T [or (L % S) % T]
324 *
325 * N - The "minor" (i.e., across the group) stripe number
326 *
327 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200328 *
329 * C - The component index coresponding to L
330 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200331 * C = (H - (N * U)) / stripe_unit + G * group_width
332 * [or (L % U) / stripe_unit + G * group_width]
Boaz Harroshb367e782010-02-07 19:18:58 +0200333 *
334 * O - The component offset coresponding to L
335 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200336 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harroshb367e782010-02-07 19:18:58 +0200337 */
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700338static void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
339 struct ore_striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200340{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700341 u32 stripe_unit = layout->stripe_unit;
342 u32 group_width = layout->group_width;
343 u64 group_depth = layout->group_depth;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200344
Boaz Harroshb367e782010-02-07 19:18:58 +0200345 u32 U = stripe_unit * group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200346 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700347 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200348 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200349
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200350 /*
351 G = (L - (M * S)) / T
352 H = (L - (M * S)) % T
353 */
354 u64 LmodS = file_offset - M * S;
355 u32 G = div64_u64(LmodS, T);
356 u64 H = LmodS - G * T;
Boaz Harroshb367e782010-02-07 19:18:58 +0200357
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200358 u32 N = div_u64(H, U);
359
360 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
361 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700362 si->dev *= layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200363
364 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
365
366 si->obj_offset = si->unit_off + (N * stripe_unit) +
367 (M * group_depth * stripe_unit);
368
369 si->group_length = T - H;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700370 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200371}
372
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700373static int _add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
374 unsigned pgbase, struct ore_per_dev_state *per_dev,
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200375 int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200376{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200377 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200378 struct request_queue *q =
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700379 osd_request_queue(_ios_od(ios, per_dev->dev));
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200380
381 per_dev->length += cur_len;
382
383 if (per_dev->bio == NULL) {
384 unsigned pages_in_stripe = ios->layout->group_width *
385 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200386 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200387 ios->layout->group_width;
388
389 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
390 if (unlikely(!per_dev->bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700391 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200392 bio_size);
393 return -ENOMEM;
394 }
395 }
396
397 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200398 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
399 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200400
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200401 BUG_ON(ios->nr_pages <= pg);
402 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200403
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200404 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
405 pglen, pgbase);
406 if (unlikely(pglen != added_len))
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200407 return -ENOMEM;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200408 pgbase = 0;
409 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200410 }
411 BUG_ON(cur_len);
412
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200413 *cur_pg = pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200414 return 0;
415}
416
Boaz Harrosh98260752011-10-02 15:32:50 +0200417static int _prepare_for_striping(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200418{
Boaz Harrosh98260752011-10-02 15:32:50 +0200419 struct ore_striping_info *si = &ios->si;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200420 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200421 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200422 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200423 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200424 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200425 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh98260752011-10-02 15:32:50 +0200426 u64 length = ios->length;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200427 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200428
Boaz Harrosh98260752011-10-02 15:32:50 +0200429 if (!ios->pages) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200430 ios->numdevs = ios->layout->mirrors_p1;
431 return 0;
432 }
433
434 BUG_ON(length > si->group_length);
435
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200436 while (length) {
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300437 unsigned comp = dev - first_dev;
438 struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
Boaz Harroshb367e782010-02-07 19:18:58 +0200439 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200440
441 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200442 per_dev->dev = dev;
443 if (dev < si->dev) {
444 per_dev->offset = si->obj_offset + stripe_unit -
445 si->unit_off;
446 cur_len = stripe_unit;
447 } else if (dev == si->dev) {
448 per_dev->offset = si->obj_offset;
449 cur_len = stripe_unit - si->unit_off;
450 page_off = si->unit_off & ~PAGE_MASK;
451 BUG_ON(page_off && (page_off != ios->pgbase));
452 } else { /* dev > si->dev */
453 per_dev->offset = si->obj_offset - si->unit_off;
454 cur_len = stripe_unit;
455 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200456 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200457 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200458 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200459 if (cur_len >= length)
460 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200461
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200462 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
463 cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200464 if (unlikely(ret))
465 goto out;
466
Boaz Harrosh6e316092010-07-29 17:08:13 +0300467 dev += mirrors_p1;
468 dev = (dev % devs_in_group) + first_dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200469
470 length -= cur_len;
471 }
472out:
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300473 ios->numdevs = devs_in_group;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200474 ios->pages_consumed = cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200475 return ret;
476}
477
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700478int ore_create(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200479{
480 int i, ret;
481
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300482 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200483 struct osd_request *or;
484
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700485 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200486 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700487 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200488 ret = -ENOMEM;
489 goto out;
490 }
491 ios->per_dev[i].or = or;
492 ios->numdevs++;
493
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700494 osd_req_create_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200495 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700496 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200497
498out:
499 return ret;
500}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700501EXPORT_SYMBOL(ore_create);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200502
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700503int ore_remove(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200504{
505 int i, ret;
506
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300507 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200508 struct osd_request *or;
509
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700510 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200511 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700512 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200513 ret = -ENOMEM;
514 goto out;
515 }
516 ios->per_dev[i].or = or;
517 ios->numdevs++;
518
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700519 osd_req_remove_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200520 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700521 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200522
523out:
524 return ret;
525}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700526EXPORT_SYMBOL(ore_remove);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200527
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700528static int _write_mirror(struct ore_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200529{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700530 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200531 unsigned dev = ios->per_dev[cur_comp].dev;
532 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
533 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200534
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200535 if (ios->pages && !master_dev->length)
536 return 0; /* Just an empty slot */
537
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200538 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700539 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200540 struct osd_request *or;
541
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700542 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200543 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700544 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200545 ret = -ENOMEM;
546 goto out;
547 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200548 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200549
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200550 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200551 struct bio *bio;
552
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200553 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200554 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200555 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200556 if (unlikely(!bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700557 ORE_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200558 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200559 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200560 ret = -ENOMEM;
561 goto out;
562 }
563
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200564 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200565 bio->bi_bdev = NULL;
566 bio->bi_next = NULL;
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700567 per_dev->offset = master_dev->offset;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200568 per_dev->length = master_dev->length;
569 per_dev->bio = bio;
570 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200571 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200572 bio = master_dev->bio;
573 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200574 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200575 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200576
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700577 osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
578 bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700579 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200580 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700581 _LLU(_ios_obj(ios, dev)->id),
582 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200583 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200584 } else if (ios->kern_buff) {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700585 per_dev->offset = ios->si.obj_offset;
586 per_dev->dev = ios->si.dev + dev;
587
588 /* no cross device without page array */
589 BUG_ON((ios->layout->group_width > 1) &&
590 (ios->si.unit_off + ios->length >
591 ios->layout->stripe_unit));
592
593 ret = osd_req_write_kern(or, _ios_obj(ios, per_dev->dev),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700594 per_dev->offset,
595 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200596 if (unlikely(ret))
597 goto out;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700598 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200599 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700600 _LLU(_ios_obj(ios, dev)->id),
601 _LLU(per_dev->offset),
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700602 _LLU(ios->length), per_dev->dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200603 } else {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700604 osd_req_set_attributes(or, _ios_obj(ios, dev));
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700605 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700606 _LLU(_ios_obj(ios, dev)->id),
607 ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200608 }
609
610 if (ios->out_attr)
611 osd_req_add_set_attr_list(or, ios->out_attr,
612 ios->out_attr_len);
613
614 if (ios->in_attr)
615 osd_req_add_get_attr_list(or, ios->in_attr,
616 ios->in_attr_len);
617 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200618
619out:
620 return ret;
621}
622
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700623int ore_write(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200624{
625 int i;
626 int ret;
627
628 ret = _prepare_for_striping(ios);
629 if (unlikely(ret))
630 return ret;
631
632 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700633 ret = _write_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200634 if (unlikely(ret))
635 return ret;
636 }
637
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700638 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200639 return ret;
640}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700641EXPORT_SYMBOL(ore_write);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200642
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700643static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200644{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200645 struct osd_request *or;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700646 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700647 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
648 unsigned first_dev = (unsigned)obj->id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200649
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200650 if (ios->pages && !per_dev->length)
651 return 0; /* Just an empty slot */
652
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200653 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700654 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200655 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700656 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200657 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200658 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200659 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200660
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200661 if (ios->pages) {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700662 osd_req_read(or, obj, per_dev->offset,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200663 per_dev->bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700664 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700665 " dev=%d\n", _LLU(obj->id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200666 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200667 first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200668 } else {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700669 BUG_ON(ios->kern_buff);
670
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700671 osd_req_get_attributes(or, obj);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700672 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700673 _LLU(obj->id),
674 ios->in_attr_len, first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200675 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200676 if (ios->out_attr)
677 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
678
679 if (ios->in_attr)
680 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
681
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200682 return 0;
683}
684
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700685int ore_read(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200686{
687 int i;
688 int ret;
689
690 ret = _prepare_for_striping(ios);
691 if (unlikely(ret))
692 return ret;
693
694 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700695 ret = _read_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200696 if (unlikely(ret))
697 return ret;
698 }
699
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700700 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200701 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200702}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700703EXPORT_SYMBOL(ore_read);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200704
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700705int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200706{
707 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
708 void *iter = NULL;
709 int nelem;
710
711 do {
712 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200713 osd_req_decode_get_attr_list(ios->per_dev[0].or,
714 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200715 if ((cur_attr.attr_page == attr->attr_page) &&
716 (cur_attr.attr_id == attr->attr_id)) {
717 attr->len = cur_attr.len;
718 attr->val_ptr = cur_attr.val_ptr;
719 return 0;
720 }
721 } while (iter);
722
723 return -EIO;
724}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700725EXPORT_SYMBOL(extract_attr_from_ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200726
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700727static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200728 struct osd_attr *attr)
729{
730 int last_comp = cur_comp + ios->layout->mirrors_p1;
731
732 for (; cur_comp < last_comp; ++cur_comp) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700733 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200734 struct osd_request *or;
735
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700736 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200737 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700738 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200739 return -ENOMEM;
740 }
741 per_dev->or = or;
742
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700743 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200744 osd_req_add_set_attr_list(or, attr, 1);
745 }
746
747 return 0;
748}
749
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700750struct _trunc_info {
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700751 struct ore_striping_info si;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700752 u64 prev_group_obj_off;
753 u64 next_group_obj_off;
754
755 unsigned first_group_dev;
756 unsigned nex_group_dev;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700757};
758
H Hartley Sweeten1958c7c22011-09-23 13:42:43 -0700759static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
760 struct _trunc_info *ti)
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700761{
762 unsigned stripe_unit = layout->stripe_unit;
763
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700764 ore_calc_stripe_info(layout, file_offset, &ti->si);
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700765
766 ti->prev_group_obj_off = ti->si.M * stripe_unit;
767 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
768
769 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
770 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700771}
772
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300773int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700774 u64 size)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200775{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700776 struct ore_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200777 struct exofs_trunc_attr {
778 struct osd_attr attr;
779 __be64 newsize;
780 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700781 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200782 int i, ret;
783
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300784 ret = ore_get_io_state(layout, oc, &ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200785 if (unlikely(ret))
786 return ret;
787
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700788 _calc_trunk_info(ios->layout, size, &ti);
789
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300790 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200791 GFP_KERNEL);
792 if (unlikely(!size_attrs)) {
793 ret = -ENOMEM;
794 goto out;
795 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200796
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300797 ios->numdevs = ios->oc->numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200798
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300799 for (i = 0; i < ios->numdevs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200800 struct exofs_trunc_attr *size_attr = &size_attrs[i];
801 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200802
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700803 if (i < ti.first_group_dev)
804 obj_size = ti.prev_group_obj_off;
805 else if (i >= ti.nex_group_dev)
806 obj_size = ti.next_group_obj_off;
807 else if (i < ti.si.dev) /* dev within this group */
808 obj_size = ti.si.obj_offset +
809 ios->layout->stripe_unit - ti.si.unit_off;
810 else if (i == ti.si.dev)
811 obj_size = ti.si.obj_offset;
812 else /* i > ti.dev */
813 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200814
815 size_attr->newsize = cpu_to_be64(obj_size);
816 size_attr->attr = g_attr_logical_length;
817 size_attr->attr.val_ptr = &size_attr->newsize;
818
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700819 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300820 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200821 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
822 &size_attr->attr);
823 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200824 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200825 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700826 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200827
828out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200829 kfree(size_attrs);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700830 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200831 return ret;
832}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700833EXPORT_SYMBOL(ore_truncate);
Boaz Harrosh85e44df2011-05-16 15:26:47 +0300834
835const struct osd_attr g_attr_logical_length = ATTR_DEF(
836 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700837EXPORT_SYMBOL(g_attr_logical_length);