blob: fbb47ba2cd71fbece4d9d0b26653c4b159e4062e [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 Harroshb14f8ab2008-10-27 18:27:55 +020026#include <scsi/scsi_device.h>
Boaz Harrosh5d952b82010-02-01 13:35:51 +020027#include <asm/div64.h>
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020028
29#include "exofs.h"
30
Boaz Harrosh34ce4e72009-12-15 19:34:17 +020031#define EXOFS_DBGMSG2(M...) do {} while (0)
32/* #define EXOFS_DBGMSG2 EXOFS_DBGMSG */
33
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020034void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj)
35{
36 osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
37}
38
Boaz Harrosh06886a52009-11-08 14:54:08 +020039int exofs_read_kern(struct osd_dev *od, u8 *cred, struct osd_obj_id *obj,
40 u64 offset, void *p, unsigned length)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020041{
Boaz Harrosh06886a52009-11-08 14:54:08 +020042 struct osd_request *or = osd_start_request(od, GFP_KERNEL);
43/* struct osd_sense_info osi = {.key = 0};*/
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020044 int ret;
45
Boaz Harrosh06886a52009-11-08 14:54:08 +020046 if (unlikely(!or)) {
47 EXOFS_DBGMSG("%s: osd_start_request failed.\n", __func__);
48 return -ENOMEM;
49 }
50 ret = osd_req_read_kern(or, obj, offset, p, length);
51 if (unlikely(ret)) {
52 EXOFS_DBGMSG("%s: osd_req_read_kern failed.\n", __func__);
53 goto out;
54 }
55
56 ret = osd_finalize_request(or, 0, cred, NULL);
57 if (unlikely(ret)) {
Paul Bolle426d3102010-08-07 12:30:03 +020058 EXOFS_DBGMSG("Failed to osd_finalize_request() => %d\n", ret);
Boaz Harrosh06886a52009-11-08 14:54:08 +020059 goto out;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020060 }
61
62 ret = osd_execute_request(or);
Boaz Harrosh06886a52009-11-08 14:54:08 +020063 if (unlikely(ret))
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020064 EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
65 /* osd_req_decode_sense(or, ret); */
Boaz Harrosh06886a52009-11-08 14:54:08 +020066
67out:
68 osd_end_request(or);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020069 return ret;
70}
71
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020072int exofs_get_io_state(struct exofs_layout *layout,
73 struct exofs_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020074{
Boaz Harrosh06886a52009-11-08 14:54:08 +020075 struct exofs_io_state *ios;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020076
Boaz Harrosh06886a52009-11-08 14:54:08 +020077 /*TODO: Maybe use kmem_cach per sbi of size
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020078 * exofs_io_state_size(layout->s_numdevs)
Boaz Harrosh06886a52009-11-08 14:54:08 +020079 */
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020080 ios = kzalloc(exofs_io_state_size(layout->s_numdevs), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +020081 if (unlikely(!ios)) {
Paul Bolle426d3102010-08-07 12:30:03 +020082 EXOFS_DBGMSG("Failed kzalloc bytes=%d\n",
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020083 exofs_io_state_size(layout->s_numdevs));
Boaz Harrosh06886a52009-11-08 14:54:08 +020084 *pios = NULL;
85 return -ENOMEM;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020086 }
87
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020088 ios->layout = layout;
89 ios->obj.partition = layout->s_pid;
Boaz Harrosh06886a52009-11-08 14:54:08 +020090 *pios = ios;
91 return 0;
92}
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020093
Boaz Harrosh06886a52009-11-08 14:54:08 +020094void exofs_put_io_state(struct exofs_io_state *ios)
95{
96 if (ios) {
97 unsigned i;
98
99 for (i = 0; i < ios->numdevs; i++) {
100 struct exofs_per_dev_state *per_dev = &ios->per_dev[i];
101
102 if (per_dev->or)
103 osd_end_request(per_dev->or);
104 if (per_dev->bio)
105 bio_put(per_dev->bio);
106 }
107
108 kfree(ios);
109 }
110}
111
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200112unsigned exofs_layout_od_id(struct exofs_layout *layout,
113 osd_id obj_no, unsigned layout_index)
114{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200115/* switch (layout->lay_func) {
116 case LAYOUT_MOVING_WINDOW:
117 {*/
118 unsigned dev_mod = obj_no;
119
120 return (layout_index + dev_mod * layout->mirrors_p1) %
121 layout->s_numdevs;
122/* }
123 case LAYOUT_FUNC_IMPLICT:
124 return layout->devs[layout_index];
125 }*/
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200126}
127
128static inline struct osd_dev *exofs_ios_od(struct exofs_io_state *ios,
129 unsigned layout_index)
130{
131 return ios->layout->s_ods[
132 exofs_layout_od_id(ios->layout, ios->obj.id, layout_index)];
133}
134
Boaz Harrosh06886a52009-11-08 14:54:08 +0200135static void _sync_done(struct exofs_io_state *ios, void *p)
136{
137 struct completion *waiting = p;
138
139 complete(waiting);
140}
141
142static void _last_io(struct kref *kref)
143{
144 struct exofs_io_state *ios = container_of(
145 kref, struct exofs_io_state, kref);
146
147 ios->done(ios, ios->private);
148}
149
150static void _done_io(struct osd_request *or, void *p)
151{
152 struct exofs_io_state *ios = p;
153
154 kref_put(&ios->kref, _last_io);
155}
156
157static int exofs_io_execute(struct exofs_io_state *ios)
158{
159 DECLARE_COMPLETION_ONSTACK(wait);
160 bool sync = (ios->done == NULL);
161 int i, ret;
162
163 if (sync) {
164 ios->done = _sync_done;
165 ios->private = &wait;
166 }
167
168 for (i = 0; i < ios->numdevs; i++) {
169 struct osd_request *or = ios->per_dev[i].or;
170 if (unlikely(!or))
171 continue;
172
173 ret = osd_finalize_request(or, 0, ios->cred, NULL);
174 if (unlikely(ret)) {
Paul Bolle426d3102010-08-07 12:30:03 +0200175 EXOFS_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200176 ret);
177 return ret;
178 }
179 }
180
181 kref_init(&ios->kref);
182
183 for (i = 0; i < ios->numdevs; i++) {
184 struct osd_request *or = ios->per_dev[i].or;
185 if (unlikely(!or))
186 continue;
187
188 kref_get(&ios->kref);
189 osd_execute_request_async(or, _done_io, ios);
190 }
191
192 kref_put(&ios->kref, _last_io);
193 ret = 0;
194
195 if (sync) {
196 wait_for_completion(&wait);
197 ret = exofs_check_io(ios, NULL);
198 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200199 return ret;
200}
201
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200202static void _clear_bio(struct bio *bio)
203{
204 struct bio_vec *bv;
205 unsigned i;
206
207 __bio_for_each_segment(bv, bio, i, 0) {
208 unsigned this_count = bv->bv_len;
209
210 if (likely(PAGE_SIZE == this_count))
211 clear_highpage(bv->bv_page);
212 else
213 zero_user(bv->bv_page, bv->bv_offset, this_count);
214 }
215}
216
Boaz Harrosh06886a52009-11-08 14:54:08 +0200217int exofs_check_io(struct exofs_io_state *ios, u64 *resid)
218{
219 enum osd_err_priority acumulated_osd_err = 0;
220 int acumulated_lin_err = 0;
221 int i;
222
223 for (i = 0; i < ios->numdevs; i++) {
224 struct osd_sense_info osi;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200225 struct osd_request *or = ios->per_dev[i].or;
226 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200227
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200228 if (unlikely(!or))
229 continue;
230
231 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200232 if (likely(!ret))
233 continue;
234
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200235 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
236 /* start read offset passed endof file */
237 _clear_bio(ios->per_dev[i].bio);
238 EXOFS_DBGMSG("start read offset passed end of file "
239 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200240 _LLU(ios->per_dev[i].offset),
241 _LLU(ios->per_dev[i].length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200242
243 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200244 }
245
246 if (osi.osd_err_pri >= acumulated_osd_err) {
247 acumulated_osd_err = osi.osd_err_pri;
248 acumulated_lin_err = ret;
249 }
250 }
251
252 /* TODO: raid specific residual calculations */
253 if (resid) {
254 if (likely(!acumulated_lin_err))
255 *resid = 0;
256 else
257 *resid = ios->length;
258 }
259
260 return acumulated_lin_err;
261}
262
Boaz Harroshb367e782010-02-07 19:18:58 +0200263/*
264 * L - logical offset into the file
265 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200266 * U - The number of bytes in a stripe within a group
Boaz Harroshb367e782010-02-07 19:18:58 +0200267 *
268 * U = stripe_unit * group_width
269 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200270 * T - The number of bytes striped within a group of component objects
271 * (before advancing to the next group)
Boaz Harroshb367e782010-02-07 19:18:58 +0200272 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200273 * T = stripe_unit * group_width * group_depth
274 *
275 * S - The number of bytes striped across all component objects
276 * before the pattern repeats
277 *
278 * S = stripe_unit * group_width * group_depth * group_count
279 *
280 * M - The "major" (i.e., across all components) stripe number
281 *
282 * M = L / S
283 *
284 * G - Counts the groups from the beginning of the major stripe
285 *
286 * G = (L - (M * S)) / T [or (L % S) / T]
287 *
288 * H - The byte offset within the group
289 *
290 * H = (L - (M * S)) % T [or (L % S) % T]
291 *
292 * N - The "minor" (i.e., across the group) stripe number
293 *
294 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200295 *
296 * C - The component index coresponding to L
297 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200298 * C = (H - (N * U)) / stripe_unit + G * group_width
299 * [or (L % U) / stripe_unit + G * group_width]
Boaz Harroshb367e782010-02-07 19:18:58 +0200300 *
301 * O - The component offset coresponding to L
302 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200303 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harroshb367e782010-02-07 19:18:58 +0200304 */
Boaz Harroshb367e782010-02-07 19:18:58 +0200305struct _striping_info {
306 u64 obj_offset;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200307 u64 group_length;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700308 u64 M; /* for truncate */
Boaz Harroshb367e782010-02-07 19:18:58 +0200309 unsigned dev;
310 unsigned unit_off;
311};
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200312
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700313static void _calc_stripe_info(struct exofs_layout *layout, u64 file_offset,
Boaz Harroshb367e782010-02-07 19:18:58 +0200314 struct _striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200315{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700316 u32 stripe_unit = layout->stripe_unit;
317 u32 group_width = layout->group_width;
318 u64 group_depth = layout->group_depth;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200319
Boaz Harroshb367e782010-02-07 19:18:58 +0200320 u32 U = stripe_unit * group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200321 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700322 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200323 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200324
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200325 /*
326 G = (L - (M * S)) / T
327 H = (L - (M * S)) % T
328 */
329 u64 LmodS = file_offset - M * S;
330 u32 G = div64_u64(LmodS, T);
331 u64 H = LmodS - G * T;
Boaz Harroshb367e782010-02-07 19:18:58 +0200332
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200333 u32 N = div_u64(H, U);
334
335 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
336 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700337 si->dev *= layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200338
339 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
340
341 si->obj_offset = si->unit_off + (N * stripe_unit) +
342 (M * group_depth * stripe_unit);
343
344 si->group_length = T - H;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700345 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200346}
347
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200348static int _add_stripe_unit(struct exofs_io_state *ios, unsigned *cur_pg,
349 unsigned pgbase, struct exofs_per_dev_state *per_dev,
350 int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200351{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200352 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200353 struct request_queue *q =
354 osd_request_queue(exofs_ios_od(ios, per_dev->dev));
355
356 per_dev->length += cur_len;
357
358 if (per_dev->bio == NULL) {
359 unsigned pages_in_stripe = ios->layout->group_width *
360 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200361 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200362 ios->layout->group_width;
363
364 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
365 if (unlikely(!per_dev->bio)) {
Paul Bolle426d3102010-08-07 12:30:03 +0200366 EXOFS_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200367 bio_size);
368 return -ENOMEM;
369 }
370 }
371
372 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200373 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
374 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200375
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200376 BUG_ON(ios->nr_pages <= pg);
377 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200378
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200379 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
380 pglen, pgbase);
381 if (unlikely(pglen != added_len))
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200382 return -ENOMEM;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200383 pgbase = 0;
384 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200385 }
386 BUG_ON(cur_len);
387
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200388 *cur_pg = pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200389 return 0;
390}
391
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200392static int _prepare_one_group(struct exofs_io_state *ios, u64 length,
Boaz Harrosh6e316092010-07-29 17:08:13 +0300393 struct _striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200394{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200395 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200396 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200397 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200398 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200399 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200400 unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
401 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200402 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200403
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200404 while (length) {
Boaz Harrosh6e316092010-07-29 17:08:13 +0300405 struct exofs_per_dev_state *per_dev = &ios->per_dev[dev];
Boaz Harroshb367e782010-02-07 19:18:58 +0200406 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200407
408 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200409 per_dev->dev = dev;
410 if (dev < si->dev) {
411 per_dev->offset = si->obj_offset + stripe_unit -
412 si->unit_off;
413 cur_len = stripe_unit;
414 } else if (dev == si->dev) {
415 per_dev->offset = si->obj_offset;
416 cur_len = stripe_unit - si->unit_off;
417 page_off = si->unit_off & ~PAGE_MASK;
418 BUG_ON(page_off && (page_off != ios->pgbase));
419 } else { /* dev > si->dev */
420 per_dev->offset = si->obj_offset - si->unit_off;
421 cur_len = stripe_unit;
422 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200423
Boaz Harrosh6e316092010-07-29 17:08:13 +0300424 if (max_comp < dev)
425 max_comp = dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200426 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200427 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200428 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200429 if (cur_len >= length)
430 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200431
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200432 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
433 cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200434 if (unlikely(ret))
435 goto out;
436
Boaz Harrosh6e316092010-07-29 17:08:13 +0300437 dev += mirrors_p1;
438 dev = (dev % devs_in_group) + first_dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200439
440 length -= cur_len;
441 }
442out:
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200443 ios->numdevs = max_comp + mirrors_p1;
444 ios->pages_consumed = cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200445 return ret;
446}
447
Boaz Harroshb367e782010-02-07 19:18:58 +0200448static int _prepare_for_striping(struct exofs_io_state *ios)
449{
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200450 u64 length = ios->length;
Boaz Harrosh5002dd12010-08-02 20:06:46 +0300451 u64 offset = ios->offset;
Boaz Harroshb367e782010-02-07 19:18:58 +0200452 struct _striping_info si;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200453 int ret = 0;
Boaz Harroshb367e782010-02-07 19:18:58 +0200454
Boaz Harroshb367e782010-02-07 19:18:58 +0200455 if (!ios->pages) {
456 if (ios->kern_buff) {
457 struct exofs_per_dev_state *per_dev = &ios->per_dev[0];
458
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700459 _calc_stripe_info(ios->layout, ios->offset, &si);
Boaz Harroshb367e782010-02-07 19:18:58 +0200460 per_dev->offset = si.obj_offset;
461 per_dev->dev = si.dev;
462
463 /* no cross device without page array */
464 BUG_ON((ios->layout->group_width > 1) &&
465 (si.unit_off + ios->length >
466 ios->layout->stripe_unit));
467 }
468 ios->numdevs = ios->layout->mirrors_p1;
469 return 0;
470 }
471
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200472 while (length) {
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700473 _calc_stripe_info(ios->layout, offset, &si);
Boaz Harrosh5002dd12010-08-02 20:06:46 +0300474
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200475 if (length < si.group_length)
476 si.group_length = length;
477
Boaz Harrosh6e316092010-07-29 17:08:13 +0300478 ret = _prepare_one_group(ios, si.group_length, &si);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200479 if (unlikely(ret))
480 goto out;
481
Boaz Harrosh5002dd12010-08-02 20:06:46 +0300482 offset += si.group_length;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200483 length -= si.group_length;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200484 }
485
486out:
487 return ret;
Boaz Harroshb367e782010-02-07 19:18:58 +0200488}
489
Boaz Harrosh06886a52009-11-08 14:54:08 +0200490int exofs_sbi_create(struct exofs_io_state *ios)
491{
492 int i, ret;
493
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200494 for (i = 0; i < ios->layout->s_numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200495 struct osd_request *or;
496
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200497 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200498 if (unlikely(!or)) {
499 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
500 ret = -ENOMEM;
501 goto out;
502 }
503 ios->per_dev[i].or = or;
504 ios->numdevs++;
505
506 osd_req_create_object(or, &ios->obj);
507 }
508 ret = exofs_io_execute(ios);
509
510out:
511 return ret;
512}
513
514int exofs_sbi_remove(struct exofs_io_state *ios)
515{
516 int i, ret;
517
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200518 for (i = 0; i < ios->layout->s_numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200519 struct osd_request *or;
520
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200521 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200522 if (unlikely(!or)) {
523 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
524 ret = -ENOMEM;
525 goto out;
526 }
527 ios->per_dev[i].or = or;
528 ios->numdevs++;
529
530 osd_req_remove_object(or, &ios->obj);
531 }
532 ret = exofs_io_execute(ios);
533
534out:
535 return ret;
536}
537
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200538static int _sbi_write_mirror(struct exofs_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200539{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200540 struct exofs_per_dev_state *master_dev = &ios->per_dev[cur_comp];
541 unsigned dev = ios->per_dev[cur_comp].dev;
542 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
543 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200544
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200545 if (ios->pages && !master_dev->length)
546 return 0; /* Just an empty slot */
547
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200548 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
549 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200550 struct osd_request *or;
551
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200552 or = osd_start_request(exofs_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200553 if (unlikely(!or)) {
554 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
555 ret = -ENOMEM;
556 goto out;
557 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200558 per_dev->or = or;
559 per_dev->offset = master_dev->offset;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200560
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200561 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200562 struct bio *bio;
563
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200564 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200565 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200566 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200567 if (unlikely(!bio)) {
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200568 EXOFS_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200569 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200570 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200571 ret = -ENOMEM;
572 goto out;
573 }
574
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200575 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200576 bio->bi_bdev = NULL;
577 bio->bi_next = NULL;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200578 per_dev->length = master_dev->length;
579 per_dev->bio = bio;
580 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200581 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200582 bio = master_dev->bio;
583 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200584 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200585 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200586
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200587 osd_req_write(or, &ios->obj, per_dev->offset, bio,
588 per_dev->length);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200589 EXOFS_DBGMSG("write(0x%llx) offset=0x%llx "
590 "length=0x%llx dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200591 _LLU(ios->obj.id), _LLU(per_dev->offset),
592 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200593 } else if (ios->kern_buff) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200594 ret = osd_req_write_kern(or, &ios->obj, per_dev->offset,
Boaz Harrosh06886a52009-11-08 14:54:08 +0200595 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200596 if (unlikely(ret))
597 goto out;
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200598 EXOFS_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
599 "length=0x%llx dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200600 _LLU(ios->obj.id), _LLU(per_dev->offset),
601 _LLU(ios->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200602 } else {
603 osd_req_set_attributes(or, &ios->obj);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200604 EXOFS_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200605 _LLU(ios->obj.id), ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200606 }
607
608 if (ios->out_attr)
609 osd_req_add_set_attr_list(or, ios->out_attr,
610 ios->out_attr_len);
611
612 if (ios->in_attr)
613 osd_req_add_get_attr_list(or, ios->in_attr,
614 ios->in_attr_len);
615 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200616
617out:
618 return ret;
619}
620
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200621int exofs_sbi_write(struct exofs_io_state *ios)
622{
623 int i;
624 int ret;
625
626 ret = _prepare_for_striping(ios);
627 if (unlikely(ret))
628 return ret;
629
630 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
631 ret = _sbi_write_mirror(ios, i);
632 if (unlikely(ret))
633 return ret;
634 }
635
636 ret = exofs_io_execute(ios);
637 return ret;
638}
639
640static int _sbi_read_mirror(struct exofs_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200641{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200642 struct osd_request *or;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200643 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200644 unsigned first_dev = (unsigned)ios->obj.id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200645
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200646 if (ios->pages && !per_dev->length)
647 return 0; /* Just an empty slot */
648
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200649 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200650 or = osd_start_request(exofs_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200651 if (unlikely(!or)) {
652 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
653 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200654 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200655 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200656
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200657 if (ios->pages) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200658 osd_req_read(or, &ios->obj, per_dev->offset,
659 per_dev->bio, per_dev->length);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200660 EXOFS_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
661 " dev=%d\n", _LLU(ios->obj.id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200662 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200663 first_dev);
664 } else if (ios->kern_buff) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200665 int ret = osd_req_read_kern(or, &ios->obj, per_dev->offset,
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200666 ios->kern_buff, ios->length);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200667 EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
668 "length=0x%llx dev=%d ret=>%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200669 _LLU(ios->obj.id), _LLU(per_dev->offset),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200670 _LLU(ios->length), first_dev, ret);
671 if (unlikely(ret))
672 return ret;
673 } else {
674 osd_req_get_attributes(or, &ios->obj);
675 EXOFS_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
676 _LLU(ios->obj.id), ios->in_attr_len, first_dev);
677 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200678 if (ios->out_attr)
679 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
680
681 if (ios->in_attr)
682 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
683
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200684 return 0;
685}
686
687int exofs_sbi_read(struct exofs_io_state *ios)
688{
689 int i;
690 int ret;
691
692 ret = _prepare_for_striping(ios);
693 if (unlikely(ret))
694 return ret;
695
696 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
697 ret = _sbi_read_mirror(ios, i);
698 if (unlikely(ret))
699 return ret;
700 }
701
702 ret = exofs_io_execute(ios);
703 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200704}
705
706int extract_attr_from_ios(struct exofs_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200707{
708 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
709 void *iter = NULL;
710 int nelem;
711
712 do {
713 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200714 osd_req_decode_get_attr_list(ios->per_dev[0].or,
715 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200716 if ((cur_attr.attr_page == attr->attr_page) &&
717 (cur_attr.attr_id == attr->attr_id)) {
718 attr->len = cur_attr.len;
719 attr->val_ptr = cur_attr.val_ptr;
720 return 0;
721 }
722 } while (iter);
723
724 return -EIO;
725}
Boaz Harrosh06886a52009-11-08 14:54:08 +0200726
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200727static int _truncate_mirrors(struct exofs_io_state *ios, unsigned cur_comp,
728 struct osd_attr *attr)
729{
730 int last_comp = cur_comp + ios->layout->mirrors_p1;
731
732 for (; cur_comp < last_comp; ++cur_comp) {
733 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
734 struct osd_request *or;
735
736 or = osd_start_request(exofs_ios_od(ios, cur_comp), GFP_KERNEL);
737 if (unlikely(!or)) {
738 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
739 return -ENOMEM;
740 }
741 per_dev->or = or;
742
743 osd_req_set_attributes(or, &ios->obj);
744 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 {
751 struct _striping_info si;
752 u64 prev_group_obj_off;
753 u64 next_group_obj_off;
754
755 unsigned first_group_dev;
756 unsigned nex_group_dev;
757 unsigned max_devs;
758};
759
760void _calc_trunk_info(struct exofs_layout *layout, u64 file_offset,
761 struct _trunc_info *ti)
762{
763 unsigned stripe_unit = layout->stripe_unit;
764
765 _calc_stripe_info(layout, file_offset, &ti->si);
766
767 ti->prev_group_obj_off = ti->si.M * stripe_unit;
768 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
769
770 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
771 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
772 ti->max_devs = layout->group_width * layout->group_count;
773}
774
Boaz Harrosh06886a52009-11-08 14:54:08 +0200775int exofs_oi_truncate(struct exofs_i_info *oi, u64 size)
776{
777 struct exofs_sb_info *sbi = oi->vfs_inode.i_sb->s_fs_info;
778 struct exofs_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200779 struct exofs_trunc_attr {
780 struct osd_attr attr;
781 __be64 newsize;
782 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700783 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200784 int i, ret;
785
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200786 ret = exofs_get_io_state(&sbi->layout, &ios);
787 if (unlikely(ret))
788 return ret;
789
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700790 _calc_trunk_info(ios->layout, size, &ti);
791
792 size_attrs = kcalloc(ti.max_devs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200793 GFP_KERNEL);
794 if (unlikely(!size_attrs)) {
795 ret = -ENOMEM;
796 goto out;
797 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200798
799 ios->obj.id = exofs_oi_objno(oi);
800 ios->cred = oi->i_cred;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200801 ios->numdevs = ios->layout->s_numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200802
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700803 for (i = 0; i < ti.max_devs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200804 struct exofs_trunc_attr *size_attr = &size_attrs[i];
805 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200806
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700807 if (i < ti.first_group_dev)
808 obj_size = ti.prev_group_obj_off;
809 else if (i >= ti.nex_group_dev)
810 obj_size = ti.next_group_obj_off;
811 else if (i < ti.si.dev) /* dev within this group */
812 obj_size = ti.si.obj_offset +
813 ios->layout->stripe_unit - ti.si.unit_off;
814 else if (i == ti.si.dev)
815 obj_size = ti.si.obj_offset;
816 else /* i > ti.dev */
817 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200818
819 size_attr->newsize = cpu_to_be64(obj_size);
820 size_attr->attr = g_attr_logical_length;
821 size_attr->attr.val_ptr = &size_attr->newsize;
822
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700823 EXOFS_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
824 _LLU(ios->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200825 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
826 &size_attr->attr);
827 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200828 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200829 }
830 ret = exofs_io_execute(ios);
831
832out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200833 kfree(size_attrs);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200834 exofs_put_io_state(ios);
835 return ret;
836}