blob: 21d6130b462eef6db2297b9e4229564b75402ea6 [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 Harrosh34ce4e7c2009-12-15 19:34:17 +020031#define EXOFS_DBGMSG2(M...) do {} while (0)
32/* #define EXOFS_DBGMSG2 EXOFS_DBGMSG */
33
Boaz Harroshe1042ba2010-11-16 20:09:58 +020034int exofs_get_rw_state(struct exofs_layout *layout, bool is_reading,
35 u64 offset, u64 length, struct exofs_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020036{
Boaz Harrosh06886a52009-11-08 14:54:08 +020037 struct exofs_io_state *ios;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020038
Boaz Harrosh06886a52009-11-08 14:54:08 +020039 /*TODO: Maybe use kmem_cach per sbi of size
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020040 * exofs_io_state_size(layout->s_numdevs)
Boaz Harrosh06886a52009-11-08 14:54:08 +020041 */
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020042 ios = kzalloc(exofs_io_state_size(layout->s_numdevs), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +020043 if (unlikely(!ios)) {
Paul Bolle426d3102010-08-07 12:30:03 +020044 EXOFS_DBGMSG("Failed kzalloc bytes=%d\n",
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020045 exofs_io_state_size(layout->s_numdevs));
Boaz Harrosh06886a52009-11-08 14:54:08 +020046 *pios = NULL;
47 return -ENOMEM;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020048 }
49
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020050 ios->layout = layout;
51 ios->obj.partition = layout->s_pid;
Boaz Harroshe1042ba2010-11-16 20:09:58 +020052 ios->offset = offset;
53 ios->length = length;
54 ios->reading = is_reading;
55
Boaz Harrosh06886a52009-11-08 14:54:08 +020056 *pios = ios;
57 return 0;
58}
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020059
Boaz Harroshe1042ba2010-11-16 20:09:58 +020060int exofs_get_io_state(struct exofs_layout *layout,
61 struct exofs_io_state **ios)
62{
63 return exofs_get_rw_state(layout, true, 0, 0, ios);
64}
65
Boaz Harrosh06886a52009-11-08 14:54:08 +020066void exofs_put_io_state(struct exofs_io_state *ios)
67{
68 if (ios) {
69 unsigned i;
70
71 for (i = 0; i < ios->numdevs; i++) {
72 struct exofs_per_dev_state *per_dev = &ios->per_dev[i];
73
74 if (per_dev->or)
75 osd_end_request(per_dev->or);
76 if (per_dev->bio)
77 bio_put(per_dev->bio);
78 }
79
80 kfree(ios);
81 }
82}
83
84static void _sync_done(struct exofs_io_state *ios, void *p)
85{
86 struct completion *waiting = p;
87
88 complete(waiting);
89}
90
91static void _last_io(struct kref *kref)
92{
93 struct exofs_io_state *ios = container_of(
94 kref, struct exofs_io_state, kref);
95
96 ios->done(ios, ios->private);
97}
98
99static void _done_io(struct osd_request *or, void *p)
100{
101 struct exofs_io_state *ios = p;
102
103 kref_put(&ios->kref, _last_io);
104}
105
106static int exofs_io_execute(struct exofs_io_state *ios)
107{
108 DECLARE_COMPLETION_ONSTACK(wait);
109 bool sync = (ios->done == NULL);
110 int i, ret;
111
112 if (sync) {
113 ios->done = _sync_done;
114 ios->private = &wait;
115 }
116
117 for (i = 0; i < ios->numdevs; i++) {
118 struct osd_request *or = ios->per_dev[i].or;
119 if (unlikely(!or))
120 continue;
121
122 ret = osd_finalize_request(or, 0, ios->cred, NULL);
123 if (unlikely(ret)) {
Paul Bolle426d3102010-08-07 12:30:03 +0200124 EXOFS_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200125 ret);
126 return ret;
127 }
128 }
129
130 kref_init(&ios->kref);
131
132 for (i = 0; i < ios->numdevs; i++) {
133 struct osd_request *or = ios->per_dev[i].or;
134 if (unlikely(!or))
135 continue;
136
137 kref_get(&ios->kref);
138 osd_execute_request_async(or, _done_io, ios);
139 }
140
141 kref_put(&ios->kref, _last_io);
142 ret = 0;
143
144 if (sync) {
145 wait_for_completion(&wait);
146 ret = exofs_check_io(ios, NULL);
147 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200148 return ret;
149}
150
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200151static void _clear_bio(struct bio *bio)
152{
153 struct bio_vec *bv;
154 unsigned i;
155
156 __bio_for_each_segment(bv, bio, i, 0) {
157 unsigned this_count = bv->bv_len;
158
159 if (likely(PAGE_SIZE == this_count))
160 clear_highpage(bv->bv_page);
161 else
162 zero_user(bv->bv_page, bv->bv_offset, this_count);
163 }
164}
165
Boaz Harrosh06886a52009-11-08 14:54:08 +0200166int exofs_check_io(struct exofs_io_state *ios, u64 *resid)
167{
168 enum osd_err_priority acumulated_osd_err = 0;
169 int acumulated_lin_err = 0;
170 int i;
171
172 for (i = 0; i < ios->numdevs; i++) {
173 struct osd_sense_info osi;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200174 struct osd_request *or = ios->per_dev[i].or;
175 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200176
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200177 if (unlikely(!or))
178 continue;
179
180 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200181 if (likely(!ret))
182 continue;
183
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200184 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
185 /* start read offset passed endof file */
186 _clear_bio(ios->per_dev[i].bio);
187 EXOFS_DBGMSG("start read offset passed end of file "
188 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200189 _LLU(ios->per_dev[i].offset),
190 _LLU(ios->per_dev[i].length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200191
192 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200193 }
194
195 if (osi.osd_err_pri >= acumulated_osd_err) {
196 acumulated_osd_err = osi.osd_err_pri;
197 acumulated_lin_err = ret;
198 }
199 }
200
201 /* TODO: raid specific residual calculations */
202 if (resid) {
203 if (likely(!acumulated_lin_err))
204 *resid = 0;
205 else
206 *resid = ios->length;
207 }
208
209 return acumulated_lin_err;
210}
211
Boaz Harroshb367e782010-02-07 19:18:58 +0200212/*
213 * L - logical offset into the file
214 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200215 * U - The number of bytes in a stripe within a group
Boaz Harroshb367e782010-02-07 19:18:58 +0200216 *
217 * U = stripe_unit * group_width
218 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200219 * T - The number of bytes striped within a group of component objects
220 * (before advancing to the next group)
Boaz Harroshb367e782010-02-07 19:18:58 +0200221 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200222 * T = stripe_unit * group_width * group_depth
223 *
224 * S - The number of bytes striped across all component objects
225 * before the pattern repeats
226 *
227 * S = stripe_unit * group_width * group_depth * group_count
228 *
229 * M - The "major" (i.e., across all components) stripe number
230 *
231 * M = L / S
232 *
233 * G - Counts the groups from the beginning of the major stripe
234 *
235 * G = (L - (M * S)) / T [or (L % S) / T]
236 *
237 * H - The byte offset within the group
238 *
239 * H = (L - (M * S)) % T [or (L % S) % T]
240 *
241 * N - The "minor" (i.e., across the group) stripe number
242 *
243 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200244 *
245 * C - The component index coresponding to L
246 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200247 * C = (H - (N * U)) / stripe_unit + G * group_width
248 * [or (L % U) / stripe_unit + G * group_width]
Boaz Harroshb367e782010-02-07 19:18:58 +0200249 *
250 * O - The component offset coresponding to L
251 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200252 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harroshb367e782010-02-07 19:18:58 +0200253 */
Boaz Harroshb367e782010-02-07 19:18:58 +0200254struct _striping_info {
255 u64 obj_offset;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200256 u64 group_length;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700257 u64 M; /* for truncate */
Boaz Harroshb367e782010-02-07 19:18:58 +0200258 unsigned dev;
259 unsigned unit_off;
260};
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200261
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700262static void _calc_stripe_info(struct exofs_layout *layout, u64 file_offset,
Boaz Harroshb367e782010-02-07 19:18:58 +0200263 struct _striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200264{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700265 u32 stripe_unit = layout->stripe_unit;
266 u32 group_width = layout->group_width;
267 u64 group_depth = layout->group_depth;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200268
Boaz Harroshb367e782010-02-07 19:18:58 +0200269 u32 U = stripe_unit * group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200270 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700271 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200272 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200273
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200274 /*
275 G = (L - (M * S)) / T
276 H = (L - (M * S)) % T
277 */
278 u64 LmodS = file_offset - M * S;
279 u32 G = div64_u64(LmodS, T);
280 u64 H = LmodS - G * T;
Boaz Harroshb367e782010-02-07 19:18:58 +0200281
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200282 u32 N = div_u64(H, U);
283
284 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
285 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700286 si->dev *= layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200287
288 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
289
290 si->obj_offset = si->unit_off + (N * stripe_unit) +
291 (M * group_depth * stripe_unit);
292
293 si->group_length = T - H;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700294 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200295}
296
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200297static int _add_stripe_unit(struct exofs_io_state *ios, unsigned *cur_pg,
298 unsigned pgbase, struct exofs_per_dev_state *per_dev,
299 int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200300{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200301 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200302 struct request_queue *q =
303 osd_request_queue(exofs_ios_od(ios, per_dev->dev));
304
305 per_dev->length += cur_len;
306
307 if (per_dev->bio == NULL) {
308 unsigned pages_in_stripe = ios->layout->group_width *
309 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200310 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200311 ios->layout->group_width;
312
313 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
314 if (unlikely(!per_dev->bio)) {
Paul Bolle426d3102010-08-07 12:30:03 +0200315 EXOFS_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200316 bio_size);
317 return -ENOMEM;
318 }
319 }
320
321 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200322 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
323 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200324
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200325 BUG_ON(ios->nr_pages <= pg);
326 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200327
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200328 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
329 pglen, pgbase);
330 if (unlikely(pglen != added_len))
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200331 return -ENOMEM;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200332 pgbase = 0;
333 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200334 }
335 BUG_ON(cur_len);
336
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200337 *cur_pg = pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200338 return 0;
339}
340
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200341static int _prepare_one_group(struct exofs_io_state *ios, u64 length,
Boaz Harrosh6e316092010-07-29 17:08:13 +0300342 struct _striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200343{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200344 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200345 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200346 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200347 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200348 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200349 unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
350 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200351 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200352
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200353 while (length) {
Boaz Harrosh6e316092010-07-29 17:08:13 +0300354 struct exofs_per_dev_state *per_dev = &ios->per_dev[dev];
Boaz Harroshb367e782010-02-07 19:18:58 +0200355 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200356
357 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200358 per_dev->dev = dev;
359 if (dev < si->dev) {
360 per_dev->offset = si->obj_offset + stripe_unit -
361 si->unit_off;
362 cur_len = stripe_unit;
363 } else if (dev == si->dev) {
364 per_dev->offset = si->obj_offset;
365 cur_len = stripe_unit - si->unit_off;
366 page_off = si->unit_off & ~PAGE_MASK;
367 BUG_ON(page_off && (page_off != ios->pgbase));
368 } else { /* dev > si->dev */
369 per_dev->offset = si->obj_offset - si->unit_off;
370 cur_len = stripe_unit;
371 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200372
Boaz Harrosh6e316092010-07-29 17:08:13 +0300373 if (max_comp < dev)
374 max_comp = dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200375 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200376 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200377 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200378 if (cur_len >= length)
379 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200380
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200381 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
382 cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200383 if (unlikely(ret))
384 goto out;
385
Boaz Harrosh6e316092010-07-29 17:08:13 +0300386 dev += mirrors_p1;
387 dev = (dev % devs_in_group) + first_dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200388
389 length -= cur_len;
390 }
391out:
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200392 ios->numdevs = max_comp + mirrors_p1;
393 ios->pages_consumed = cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200394 return ret;
395}
396
Boaz Harroshb367e782010-02-07 19:18:58 +0200397static int _prepare_for_striping(struct exofs_io_state *ios)
398{
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200399 u64 length = ios->length;
Boaz Harrosh5002dd12010-08-02 20:06:46 +0300400 u64 offset = ios->offset;
Boaz Harroshb367e782010-02-07 19:18:58 +0200401 struct _striping_info si;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200402 int ret = 0;
Boaz Harroshb367e782010-02-07 19:18:58 +0200403
Boaz Harroshb367e782010-02-07 19:18:58 +0200404 if (!ios->pages) {
405 if (ios->kern_buff) {
406 struct exofs_per_dev_state *per_dev = &ios->per_dev[0];
407
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700408 _calc_stripe_info(ios->layout, ios->offset, &si);
Boaz Harroshb367e782010-02-07 19:18:58 +0200409 per_dev->offset = si.obj_offset;
410 per_dev->dev = si.dev;
411
412 /* no cross device without page array */
413 BUG_ON((ios->layout->group_width > 1) &&
414 (si.unit_off + ios->length >
415 ios->layout->stripe_unit));
416 }
417 ios->numdevs = ios->layout->mirrors_p1;
418 return 0;
419 }
420
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200421 while (length) {
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700422 _calc_stripe_info(ios->layout, offset, &si);
Boaz Harrosh5002dd12010-08-02 20:06:46 +0300423
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200424 if (length < si.group_length)
425 si.group_length = length;
426
Boaz Harrosh6e316092010-07-29 17:08:13 +0300427 ret = _prepare_one_group(ios, si.group_length, &si);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200428 if (unlikely(ret))
429 goto out;
430
Boaz Harrosh5002dd12010-08-02 20:06:46 +0300431 offset += si.group_length;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200432 length -= si.group_length;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200433 }
434
435out:
436 return ret;
Boaz Harroshb367e782010-02-07 19:18:58 +0200437}
438
Boaz Harrosh06886a52009-11-08 14:54:08 +0200439int exofs_sbi_create(struct exofs_io_state *ios)
440{
441 int i, ret;
442
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200443 for (i = 0; i < ios->layout->s_numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200444 struct osd_request *or;
445
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200446 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200447 if (unlikely(!or)) {
448 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
449 ret = -ENOMEM;
450 goto out;
451 }
452 ios->per_dev[i].or = or;
453 ios->numdevs++;
454
455 osd_req_create_object(or, &ios->obj);
456 }
457 ret = exofs_io_execute(ios);
458
459out:
460 return ret;
461}
462
463int exofs_sbi_remove(struct exofs_io_state *ios)
464{
465 int i, ret;
466
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200467 for (i = 0; i < ios->layout->s_numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200468 struct osd_request *or;
469
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200470 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200471 if (unlikely(!or)) {
472 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
473 ret = -ENOMEM;
474 goto out;
475 }
476 ios->per_dev[i].or = or;
477 ios->numdevs++;
478
479 osd_req_remove_object(or, &ios->obj);
480 }
481 ret = exofs_io_execute(ios);
482
483out:
484 return ret;
485}
486
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200487static int _sbi_write_mirror(struct exofs_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200488{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200489 struct exofs_per_dev_state *master_dev = &ios->per_dev[cur_comp];
490 unsigned dev = ios->per_dev[cur_comp].dev;
491 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
492 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200493
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200494 if (ios->pages && !master_dev->length)
495 return 0; /* Just an empty slot */
496
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200497 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
498 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200499 struct osd_request *or;
500
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200501 or = osd_start_request(exofs_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200502 if (unlikely(!or)) {
503 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
504 ret = -ENOMEM;
505 goto out;
506 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200507 per_dev->or = or;
508 per_dev->offset = master_dev->offset;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200509
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200510 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200511 struct bio *bio;
512
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200513 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200514 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200515 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200516 if (unlikely(!bio)) {
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200517 EXOFS_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200518 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200519 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200520 ret = -ENOMEM;
521 goto out;
522 }
523
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200524 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200525 bio->bi_bdev = NULL;
526 bio->bi_next = NULL;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200527 per_dev->length = master_dev->length;
528 per_dev->bio = bio;
529 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200530 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200531 bio = master_dev->bio;
532 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200533 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200534 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200535
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200536 osd_req_write(or, &ios->obj, per_dev->offset, bio,
537 per_dev->length);
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200538 EXOFS_DBGMSG("write(0x%llx) offset=0x%llx "
539 "length=0x%llx dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200540 _LLU(ios->obj.id), _LLU(per_dev->offset),
541 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200542 } else if (ios->kern_buff) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200543 ret = osd_req_write_kern(or, &ios->obj, per_dev->offset,
Boaz Harrosh06886a52009-11-08 14:54:08 +0200544 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200545 if (unlikely(ret))
546 goto out;
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200547 EXOFS_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
548 "length=0x%llx dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200549 _LLU(ios->obj.id), _LLU(per_dev->offset),
550 _LLU(ios->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200551 } else {
552 osd_req_set_attributes(or, &ios->obj);
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200553 EXOFS_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200554 _LLU(ios->obj.id), ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200555 }
556
557 if (ios->out_attr)
558 osd_req_add_set_attr_list(or, ios->out_attr,
559 ios->out_attr_len);
560
561 if (ios->in_attr)
562 osd_req_add_get_attr_list(or, ios->in_attr,
563 ios->in_attr_len);
564 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200565
566out:
567 return ret;
568}
569
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200570int exofs_sbi_write(struct exofs_io_state *ios)
571{
572 int i;
573 int ret;
574
575 ret = _prepare_for_striping(ios);
576 if (unlikely(ret))
577 return ret;
578
579 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
580 ret = _sbi_write_mirror(ios, i);
581 if (unlikely(ret))
582 return ret;
583 }
584
585 ret = exofs_io_execute(ios);
586 return ret;
587}
588
589static int _sbi_read_mirror(struct exofs_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200590{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200591 struct osd_request *or;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200592 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200593 unsigned first_dev = (unsigned)ios->obj.id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200594
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200595 if (ios->pages && !per_dev->length)
596 return 0; /* Just an empty slot */
597
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200598 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200599 or = osd_start_request(exofs_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200600 if (unlikely(!or)) {
601 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
602 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200603 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200604 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200605
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200606 if (ios->pages) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200607 osd_req_read(or, &ios->obj, per_dev->offset,
608 per_dev->bio, per_dev->length);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200609 EXOFS_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
610 " dev=%d\n", _LLU(ios->obj.id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200611 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200612 first_dev);
613 } else if (ios->kern_buff) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200614 int ret = osd_req_read_kern(or, &ios->obj, per_dev->offset,
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200615 ios->kern_buff, ios->length);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200616 EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
617 "length=0x%llx dev=%d ret=>%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200618 _LLU(ios->obj.id), _LLU(per_dev->offset),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200619 _LLU(ios->length), first_dev, ret);
620 if (unlikely(ret))
621 return ret;
622 } else {
623 osd_req_get_attributes(or, &ios->obj);
624 EXOFS_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
625 _LLU(ios->obj.id), ios->in_attr_len, first_dev);
626 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200627 if (ios->out_attr)
628 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
629
630 if (ios->in_attr)
631 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
632
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200633 return 0;
634}
635
636int exofs_sbi_read(struct exofs_io_state *ios)
637{
638 int i;
639 int ret;
640
641 ret = _prepare_for_striping(ios);
642 if (unlikely(ret))
643 return ret;
644
645 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
646 ret = _sbi_read_mirror(ios, i);
647 if (unlikely(ret))
648 return ret;
649 }
650
651 ret = exofs_io_execute(ios);
652 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200653}
654
655int extract_attr_from_ios(struct exofs_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200656{
657 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
658 void *iter = NULL;
659 int nelem;
660
661 do {
662 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200663 osd_req_decode_get_attr_list(ios->per_dev[0].or,
664 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200665 if ((cur_attr.attr_page == attr->attr_page) &&
666 (cur_attr.attr_id == attr->attr_id)) {
667 attr->len = cur_attr.len;
668 attr->val_ptr = cur_attr.val_ptr;
669 return 0;
670 }
671 } while (iter);
672
673 return -EIO;
674}
Boaz Harrosh06886a52009-11-08 14:54:08 +0200675
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200676static int _truncate_mirrors(struct exofs_io_state *ios, unsigned cur_comp,
677 struct osd_attr *attr)
678{
679 int last_comp = cur_comp + ios->layout->mirrors_p1;
680
681 for (; cur_comp < last_comp; ++cur_comp) {
682 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
683 struct osd_request *or;
684
685 or = osd_start_request(exofs_ios_od(ios, cur_comp), GFP_KERNEL);
686 if (unlikely(!or)) {
687 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
688 return -ENOMEM;
689 }
690 per_dev->or = or;
691
692 osd_req_set_attributes(or, &ios->obj);
693 osd_req_add_set_attr_list(or, attr, 1);
694 }
695
696 return 0;
697}
698
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700699struct _trunc_info {
700 struct _striping_info si;
701 u64 prev_group_obj_off;
702 u64 next_group_obj_off;
703
704 unsigned first_group_dev;
705 unsigned nex_group_dev;
706 unsigned max_devs;
707};
708
709void _calc_trunk_info(struct exofs_layout *layout, u64 file_offset,
710 struct _trunc_info *ti)
711{
712 unsigned stripe_unit = layout->stripe_unit;
713
714 _calc_stripe_info(layout, file_offset, &ti->si);
715
716 ti->prev_group_obj_off = ti->si.M * stripe_unit;
717 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
718
719 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
720 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
721 ti->max_devs = layout->group_width * layout->group_count;
722}
723
Boaz Harrosh06886a52009-11-08 14:54:08 +0200724int exofs_oi_truncate(struct exofs_i_info *oi, u64 size)
725{
726 struct exofs_sb_info *sbi = oi->vfs_inode.i_sb->s_fs_info;
727 struct exofs_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200728 struct exofs_trunc_attr {
729 struct osd_attr attr;
730 __be64 newsize;
731 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700732 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200733 int i, ret;
734
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200735 ret = exofs_get_io_state(&sbi->layout, &ios);
736 if (unlikely(ret))
737 return ret;
738
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700739 _calc_trunk_info(ios->layout, size, &ti);
740
741 size_attrs = kcalloc(ti.max_devs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200742 GFP_KERNEL);
743 if (unlikely(!size_attrs)) {
744 ret = -ENOMEM;
745 goto out;
746 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200747
748 ios->obj.id = exofs_oi_objno(oi);
749 ios->cred = oi->i_cred;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200750 ios->numdevs = ios->layout->s_numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200751
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700752 for (i = 0; i < ti.max_devs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200753 struct exofs_trunc_attr *size_attr = &size_attrs[i];
754 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200755
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700756 if (i < ti.first_group_dev)
757 obj_size = ti.prev_group_obj_off;
758 else if (i >= ti.nex_group_dev)
759 obj_size = ti.next_group_obj_off;
760 else if (i < ti.si.dev) /* dev within this group */
761 obj_size = ti.si.obj_offset +
762 ios->layout->stripe_unit - ti.si.unit_off;
763 else if (i == ti.si.dev)
764 obj_size = ti.si.obj_offset;
765 else /* i > ti.dev */
766 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200767
768 size_attr->newsize = cpu_to_be64(obj_size);
769 size_attr->attr = g_attr_logical_length;
770 size_attr->attr.val_ptr = &size_attr->newsize;
771
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700772 EXOFS_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
773 _LLU(ios->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200774 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
775 &size_attr->attr);
776 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200777 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200778 }
779 ret = exofs_io_execute(ios);
780
781out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200782 kfree(size_attrs);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200783 exofs_put_io_state(ios);
784 return ret;
785}
Boaz Harrosh85e44df2011-05-16 15:26:47 +0300786
787const struct osd_attr g_attr_logical_length = ATTR_DEF(
788 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);