blob: f1b718028a1f181f32c49589d1ab136f2894e626 [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 Harrosh3bd98562011-09-28 12:04:23 +030065 ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n",
66 ios->oc->first_dev, ios->oc->numdevs, index,
67 ios->oc->ods);
68
Boaz Harroshd866d872011-09-28 14:43:09 +030069 return ore_comp_dev(ios->oc, index);
Boaz Harrosh9e9db452011-08-05 15:06:04 -070070}
71
Boaz Harroshb916c5c2011-09-28 11:55:51 +030072static int _get_io_state(struct ore_layout *layout,
73 struct ore_components *oc, unsigned numdevs,
74 struct ore_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020075{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070076 struct ore_io_state *ios;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020077
Boaz Harrosh06886a52009-11-08 14:54:08 +020078 /*TODO: Maybe use kmem_cach per sbi of size
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020079 * exofs_io_state_size(layout->s_numdevs)
Boaz Harrosh06886a52009-11-08 14:54:08 +020080 */
Boaz Harroshb916c5c2011-09-28 11:55:51 +030081 ios = kzalloc(ore_io_state_size(numdevs), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +020082 if (unlikely(!ios)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070083 ORE_DBGMSG("Failed kzalloc bytes=%d\n",
Boaz Harroshb916c5c2011-09-28 11:55:51 +030084 ore_io_state_size(numdevs));
Boaz Harrosh06886a52009-11-08 14:54:08 +020085 *pios = NULL;
86 return -ENOMEM;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020087 }
88
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020089 ios->layout = layout;
Boaz Harrosh5bf696d2011-09-28 11:39:59 +030090 ios->oc = oc;
Boaz Harrosh06886a52009-11-08 14:54:08 +020091 *pios = ios;
92 return 0;
93}
Boaz Harroshb916c5c2011-09-28 11:55:51 +030094
95/* Allocate an io_state for only a single group of devices
96 *
97 * If a user needs to call ore_read/write() this version must be used becase it
98 * allocates extra stuff for striping and raid.
99 * The ore might decide to only IO less then @length bytes do to alignmets
100 * and constrains as follows:
101 * - The IO cannot cross group boundary.
102 * - In raid5/6 The end of the IO must align at end of a stripe eg.
103 * (@offset + @length) % strip_size == 0. Or the complete range is within a
104 * single stripe.
105 * - Memory condition only permitted a shorter IO. (A user can use @length=~0
106 * And check the returned ios->length for max_io_size.)
107 *
108 * The caller must check returned ios->length (and/or ios->nr_pages) and
109 * re-issue these pages that fall outside of ios->length
110 */
111int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
112 bool is_reading, u64 offset, u64 length,
113 struct ore_io_state **pios)
114{
115 struct ore_io_state *ios;
116 unsigned numdevs = layout->group_width * layout->mirrors_p1;
117 int ret;
118
119 ret = _get_io_state(layout, oc, numdevs, pios);
120 if (unlikely(ret))
121 return ret;
122
123 ios = *pios;
124 ios->reading = is_reading;
125 ios->offset = offset;
126
127 if (length) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200128 ore_calc_stripe_info(layout, offset, &ios->si);
129 ios->length = (length <= ios->si.group_length) ? length :
130 ios->si.group_length;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300131 ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
132 }
133
134 return 0;
135}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700136EXPORT_SYMBOL(ore_get_rw_state);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200137
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300138/* Allocate an io_state for all the devices in the comps array
139 *
140 * This version of io_state allocation is used mostly by create/remove
141 * and trunc where we currently need all the devices. The only wastful
142 * bit is the read/write_attributes with no IO. Those sites should
143 * be converted to use ore_get_rw_state() with length=0
144 */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300145int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300146 struct ore_io_state **pios)
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200147{
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300148 return _get_io_state(layout, oc, oc->numdevs, pios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200149}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700150EXPORT_SYMBOL(ore_get_io_state);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200151
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700152void ore_put_io_state(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200153{
154 if (ios) {
155 unsigned i;
156
157 for (i = 0; i < ios->numdevs; i++) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700158 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200159
160 if (per_dev->or)
161 osd_end_request(per_dev->or);
162 if (per_dev->bio)
163 bio_put(per_dev->bio);
164 }
165
166 kfree(ios);
167 }
168}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700169EXPORT_SYMBOL(ore_put_io_state);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200170
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700171static void _sync_done(struct ore_io_state *ios, void *p)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200172{
173 struct completion *waiting = p;
174
175 complete(waiting);
176}
177
178static void _last_io(struct kref *kref)
179{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700180 struct ore_io_state *ios = container_of(
181 kref, struct ore_io_state, kref);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200182
183 ios->done(ios, ios->private);
184}
185
186static void _done_io(struct osd_request *or, void *p)
187{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700188 struct ore_io_state *ios = p;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200189
190 kref_put(&ios->kref, _last_io);
191}
192
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700193static int ore_io_execute(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200194{
195 DECLARE_COMPLETION_ONSTACK(wait);
196 bool sync = (ios->done == NULL);
197 int i, ret;
198
199 if (sync) {
200 ios->done = _sync_done;
201 ios->private = &wait;
202 }
203
204 for (i = 0; i < ios->numdevs; i++) {
205 struct osd_request *or = ios->per_dev[i].or;
206 if (unlikely(!or))
207 continue;
208
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700209 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200210 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700211 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200212 ret);
213 return ret;
214 }
215 }
216
217 kref_init(&ios->kref);
218
219 for (i = 0; i < ios->numdevs; i++) {
220 struct osd_request *or = ios->per_dev[i].or;
221 if (unlikely(!or))
222 continue;
223
224 kref_get(&ios->kref);
225 osd_execute_request_async(or, _done_io, ios);
226 }
227
228 kref_put(&ios->kref, _last_io);
229 ret = 0;
230
231 if (sync) {
232 wait_for_completion(&wait);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700233 ret = ore_check_io(ios, NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200234 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200235 return ret;
236}
237
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200238static void _clear_bio(struct bio *bio)
239{
240 struct bio_vec *bv;
241 unsigned i;
242
243 __bio_for_each_segment(bv, bio, i, 0) {
244 unsigned this_count = bv->bv_len;
245
246 if (likely(PAGE_SIZE == this_count))
247 clear_highpage(bv->bv_page);
248 else
249 zero_user(bv->bv_page, bv->bv_offset, this_count);
250 }
251}
252
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700253int ore_check_io(struct ore_io_state *ios, u64 *resid)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200254{
255 enum osd_err_priority acumulated_osd_err = 0;
256 int acumulated_lin_err = 0;
257 int i;
258
259 for (i = 0; i < ios->numdevs; i++) {
260 struct osd_sense_info osi;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200261 struct osd_request *or = ios->per_dev[i].or;
262 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200263
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200264 if (unlikely(!or))
265 continue;
266
267 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200268 if (likely(!ret))
269 continue;
270
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200271 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
272 /* start read offset passed endof file */
273 _clear_bio(ios->per_dev[i].bio);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700274 ORE_DBGMSG("start read offset passed end of file "
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200275 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200276 _LLU(ios->per_dev[i].offset),
277 _LLU(ios->per_dev[i].length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200278
279 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200280 }
281
282 if (osi.osd_err_pri >= acumulated_osd_err) {
283 acumulated_osd_err = osi.osd_err_pri;
284 acumulated_lin_err = ret;
285 }
286 }
287
288 /* TODO: raid specific residual calculations */
289 if (resid) {
290 if (likely(!acumulated_lin_err))
291 *resid = 0;
292 else
293 *resid = ios->length;
294 }
295
296 return acumulated_lin_err;
297}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700298EXPORT_SYMBOL(ore_check_io);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200299
Boaz Harroshb367e782010-02-07 19:18:58 +0200300/*
301 * L - logical offset into the file
302 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200303 * U - The number of bytes in a stripe within a group
Boaz Harroshb367e782010-02-07 19:18:58 +0200304 *
305 * U = stripe_unit * group_width
306 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200307 * T - The number of bytes striped within a group of component objects
308 * (before advancing to the next group)
Boaz Harroshb367e782010-02-07 19:18:58 +0200309 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200310 * T = stripe_unit * group_width * group_depth
311 *
312 * S - The number of bytes striped across all component objects
313 * before the pattern repeats
314 *
315 * S = stripe_unit * group_width * group_depth * group_count
316 *
317 * M - The "major" (i.e., across all components) stripe number
318 *
319 * M = L / S
320 *
321 * G - Counts the groups from the beginning of the major stripe
322 *
323 * G = (L - (M * S)) / T [or (L % S) / T]
324 *
325 * H - The byte offset within the group
326 *
327 * H = (L - (M * S)) % T [or (L % S) % T]
328 *
329 * N - The "minor" (i.e., across the group) stripe number
330 *
331 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200332 *
333 * C - The component index coresponding to L
334 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200335 * C = (H - (N * U)) / stripe_unit + G * group_width
336 * [or (L % U) / stripe_unit + G * group_width]
Boaz Harroshb367e782010-02-07 19:18:58 +0200337 *
338 * O - The component offset coresponding to L
339 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200340 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harroshb367e782010-02-07 19:18:58 +0200341 */
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700342static void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
343 struct ore_striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200344{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700345 u32 stripe_unit = layout->stripe_unit;
346 u32 group_width = layout->group_width;
347 u64 group_depth = layout->group_depth;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200348
Boaz Harroshb367e782010-02-07 19:18:58 +0200349 u32 U = stripe_unit * group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200350 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700351 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200352 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200353
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200354 /*
355 G = (L - (M * S)) / T
356 H = (L - (M * S)) % T
357 */
358 u64 LmodS = file_offset - M * S;
359 u32 G = div64_u64(LmodS, T);
360 u64 H = LmodS - G * T;
Boaz Harroshb367e782010-02-07 19:18:58 +0200361
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200362 u32 N = div_u64(H, U);
363
364 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
365 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700366 si->dev *= layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200367
368 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
369
370 si->obj_offset = si->unit_off + (N * stripe_unit) +
371 (M * group_depth * stripe_unit);
372
373 si->group_length = T - H;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700374 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200375}
376
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700377static int _add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
378 unsigned pgbase, struct ore_per_dev_state *per_dev,
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200379 int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200380{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200381 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200382 struct request_queue *q =
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700383 osd_request_queue(_ios_od(ios, per_dev->dev));
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700384 unsigned len = cur_len;
385 int ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200386
387 if (per_dev->bio == NULL) {
388 unsigned pages_in_stripe = ios->layout->group_width *
389 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200390 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200391 ios->layout->group_width;
392
393 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
394 if (unlikely(!per_dev->bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700395 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200396 bio_size);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700397 ret = -ENOMEM;
398 goto out;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200399 }
400 }
401
402 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200403 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
404 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200405
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200406 BUG_ON(ios->nr_pages <= pg);
407 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200408
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200409 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
410 pglen, pgbase);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700411 if (unlikely(pglen != added_len)) {
412 ret = -ENOMEM;
413 goto out;
414 }
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200415 pgbase = 0;
416 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200417 }
418 BUG_ON(cur_len);
419
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700420 per_dev->length += len;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200421 *cur_pg = pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700422 ret = 0;
423out: /* we fail the complete unit on an error eg don't advance
424 * per_dev->length and cur_pg. This means that we might have a bigger
425 * bio than the CDB requested length (per_dev->length). That's fine
426 * only the oposite is fatal.
427 */
428 return ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200429}
430
Boaz Harrosh98260752011-10-02 15:32:50 +0200431static int _prepare_for_striping(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200432{
Boaz Harrosh98260752011-10-02 15:32:50 +0200433 struct ore_striping_info *si = &ios->si;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200434 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200435 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200436 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200437 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200438 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200439 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh98260752011-10-02 15:32:50 +0200440 u64 length = ios->length;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200441 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200442
Boaz Harrosh98260752011-10-02 15:32:50 +0200443 if (!ios->pages) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200444 ios->numdevs = ios->layout->mirrors_p1;
445 return 0;
446 }
447
448 BUG_ON(length > si->group_length);
449
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200450 while (length) {
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300451 unsigned comp = dev - first_dev;
452 struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
Boaz Harroshb367e782010-02-07 19:18:58 +0200453 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200454
455 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200456 per_dev->dev = dev;
457 if (dev < si->dev) {
458 per_dev->offset = si->obj_offset + stripe_unit -
459 si->unit_off;
460 cur_len = stripe_unit;
461 } else if (dev == si->dev) {
462 per_dev->offset = si->obj_offset;
463 cur_len = stripe_unit - si->unit_off;
464 page_off = si->unit_off & ~PAGE_MASK;
465 BUG_ON(page_off && (page_off != ios->pgbase));
466 } else { /* dev > si->dev */
467 per_dev->offset = si->obj_offset - si->unit_off;
468 cur_len = stripe_unit;
469 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200470 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200471 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200472 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200473 if (cur_len >= length)
474 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200475
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200476 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
477 cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200478 if (unlikely(ret))
479 goto out;
480
Boaz Harrosh6e316092010-07-29 17:08:13 +0300481 dev += mirrors_p1;
482 dev = (dev % devs_in_group) + first_dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200483
484 length -= cur_len;
485 }
486out:
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300487 ios->numdevs = devs_in_group;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200488 ios->pages_consumed = cur_pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700489 if (unlikely(ret)) {
490 if (length == ios->length)
491 return ret;
492 else
493 ios->length -= length;
494 }
495 return 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200496}
497
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700498int ore_create(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200499{
500 int i, ret;
501
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300502 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200503 struct osd_request *or;
504
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700505 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200506 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700507 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200508 ret = -ENOMEM;
509 goto out;
510 }
511 ios->per_dev[i].or = or;
512 ios->numdevs++;
513
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700514 osd_req_create_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200515 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700516 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200517
518out:
519 return ret;
520}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700521EXPORT_SYMBOL(ore_create);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200522
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700523int ore_remove(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200524{
525 int i, ret;
526
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300527 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200528 struct osd_request *or;
529
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700530 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200531 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700532 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200533 ret = -ENOMEM;
534 goto out;
535 }
536 ios->per_dev[i].or = or;
537 ios->numdevs++;
538
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700539 osd_req_remove_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200540 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700541 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200542
543out:
544 return ret;
545}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700546EXPORT_SYMBOL(ore_remove);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200547
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700548static int _write_mirror(struct ore_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200549{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700550 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200551 unsigned dev = ios->per_dev[cur_comp].dev;
552 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
553 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200554
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200555 if (ios->pages && !master_dev->length)
556 return 0; /* Just an empty slot */
557
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200558 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700559 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200560 struct osd_request *or;
561
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700562 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200563 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700564 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200565 ret = -ENOMEM;
566 goto out;
567 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200568 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200569
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200570 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200571 struct bio *bio;
572
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200573 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200574 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200575 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200576 if (unlikely(!bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700577 ORE_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200578 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200579 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200580 ret = -ENOMEM;
581 goto out;
582 }
583
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200584 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200585 bio->bi_bdev = NULL;
586 bio->bi_next = NULL;
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700587 per_dev->offset = master_dev->offset;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200588 per_dev->length = master_dev->length;
589 per_dev->bio = bio;
590 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200591 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200592 bio = master_dev->bio;
593 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200594 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200595 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200596
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700597 osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
598 bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700599 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200600 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700601 _LLU(_ios_obj(ios, dev)->id),
602 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200603 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200604 } else if (ios->kern_buff) {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700605 per_dev->offset = ios->si.obj_offset;
606 per_dev->dev = ios->si.dev + dev;
607
608 /* no cross device without page array */
609 BUG_ON((ios->layout->group_width > 1) &&
610 (ios->si.unit_off + ios->length >
611 ios->layout->stripe_unit));
612
613 ret = osd_req_write_kern(or, _ios_obj(ios, per_dev->dev),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700614 per_dev->offset,
615 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200616 if (unlikely(ret))
617 goto out;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700618 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200619 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700620 _LLU(_ios_obj(ios, dev)->id),
621 _LLU(per_dev->offset),
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700622 _LLU(ios->length), per_dev->dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200623 } else {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700624 osd_req_set_attributes(or, _ios_obj(ios, dev));
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700625 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700626 _LLU(_ios_obj(ios, dev)->id),
627 ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200628 }
629
630 if (ios->out_attr)
631 osd_req_add_set_attr_list(or, ios->out_attr,
632 ios->out_attr_len);
633
634 if (ios->in_attr)
635 osd_req_add_get_attr_list(or, ios->in_attr,
636 ios->in_attr_len);
637 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200638
639out:
640 return ret;
641}
642
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700643int ore_write(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200644{
645 int i;
646 int ret;
647
648 ret = _prepare_for_striping(ios);
649 if (unlikely(ret))
650 return ret;
651
652 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700653 ret = _write_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200654 if (unlikely(ret))
655 return ret;
656 }
657
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700658 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200659 return ret;
660}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700661EXPORT_SYMBOL(ore_write);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200662
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700663static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200664{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200665 struct osd_request *or;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700666 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700667 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
668 unsigned first_dev = (unsigned)obj->id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200669
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200670 if (ios->pages && !per_dev->length)
671 return 0; /* Just an empty slot */
672
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200673 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700674 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200675 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700676 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200677 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200678 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200679 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200680
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200681 if (ios->pages) {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700682 osd_req_read(or, obj, per_dev->offset,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200683 per_dev->bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700684 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700685 " dev=%d\n", _LLU(obj->id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200686 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200687 first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200688 } else {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700689 BUG_ON(ios->kern_buff);
690
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700691 osd_req_get_attributes(or, obj);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700692 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700693 _LLU(obj->id),
694 ios->in_attr_len, first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200695 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200696 if (ios->out_attr)
697 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
698
699 if (ios->in_attr)
700 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
701
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200702 return 0;
703}
704
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700705int ore_read(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200706{
707 int i;
708 int ret;
709
710 ret = _prepare_for_striping(ios);
711 if (unlikely(ret))
712 return ret;
713
714 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700715 ret = _read_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200716 if (unlikely(ret))
717 return ret;
718 }
719
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700720 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200721 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200722}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700723EXPORT_SYMBOL(ore_read);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200724
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700725int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200726{
727 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
728 void *iter = NULL;
729 int nelem;
730
731 do {
732 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200733 osd_req_decode_get_attr_list(ios->per_dev[0].or,
734 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200735 if ((cur_attr.attr_page == attr->attr_page) &&
736 (cur_attr.attr_id == attr->attr_id)) {
737 attr->len = cur_attr.len;
738 attr->val_ptr = cur_attr.val_ptr;
739 return 0;
740 }
741 } while (iter);
742
743 return -EIO;
744}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700745EXPORT_SYMBOL(extract_attr_from_ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200746
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700747static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200748 struct osd_attr *attr)
749{
750 int last_comp = cur_comp + ios->layout->mirrors_p1;
751
752 for (; cur_comp < last_comp; ++cur_comp) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700753 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200754 struct osd_request *or;
755
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700756 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200757 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700758 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200759 return -ENOMEM;
760 }
761 per_dev->or = or;
762
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700763 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200764 osd_req_add_set_attr_list(or, attr, 1);
765 }
766
767 return 0;
768}
769
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700770struct _trunc_info {
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700771 struct ore_striping_info si;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700772 u64 prev_group_obj_off;
773 u64 next_group_obj_off;
774
775 unsigned first_group_dev;
776 unsigned nex_group_dev;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700777};
778
H Hartley Sweeten1958c7c22011-09-23 13:42:43 -0700779static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
780 struct _trunc_info *ti)
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700781{
782 unsigned stripe_unit = layout->stripe_unit;
783
Boaz Harrosheb507bc2011-08-10 14:17:28 -0700784 ore_calc_stripe_info(layout, file_offset, &ti->si);
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700785
786 ti->prev_group_obj_off = ti->si.M * stripe_unit;
787 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
788
789 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
790 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700791}
792
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300793int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700794 u64 size)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200795{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700796 struct ore_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200797 struct exofs_trunc_attr {
798 struct osd_attr attr;
799 __be64 newsize;
800 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700801 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200802 int i, ret;
803
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300804 ret = ore_get_io_state(layout, oc, &ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200805 if (unlikely(ret))
806 return ret;
807
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700808 _calc_trunk_info(ios->layout, size, &ti);
809
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300810 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200811 GFP_KERNEL);
812 if (unlikely(!size_attrs)) {
813 ret = -ENOMEM;
814 goto out;
815 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200816
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300817 ios->numdevs = ios->oc->numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200818
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300819 for (i = 0; i < ios->numdevs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200820 struct exofs_trunc_attr *size_attr = &size_attrs[i];
821 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200822
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700823 if (i < ti.first_group_dev)
824 obj_size = ti.prev_group_obj_off;
825 else if (i >= ti.nex_group_dev)
826 obj_size = ti.next_group_obj_off;
827 else if (i < ti.si.dev) /* dev within this group */
828 obj_size = ti.si.obj_offset +
829 ios->layout->stripe_unit - ti.si.unit_off;
830 else if (i == ti.si.dev)
831 obj_size = ti.si.obj_offset;
832 else /* i > ti.dev */
833 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200834
835 size_attr->newsize = cpu_to_be64(obj_size);
836 size_attr->attr = g_attr_logical_length;
837 size_attr->attr.val_ptr = &size_attr->newsize;
838
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700839 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300840 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200841 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
842 &size_attr->attr);
843 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200844 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200845 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700846 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200847
848out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200849 kfree(size_attrs);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700850 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200851 return ret;
852}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700853EXPORT_SYMBOL(ore_truncate);
Boaz Harrosh85e44df2011-05-16 15:26:47 +0300854
855const struct osd_attr g_attr_logical_length = ATTR_DEF(
856 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700857EXPORT_SYMBOL(g_attr_logical_length);