]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - fs/exofs/ore.c
3a000084cd2176cc763c66b01a03a6b8129097e4
[~shefty/rdma-dev.git] / fs / exofs / ore.c
1 /*
2  * Copyright (C) 2005, 2006
3  * Avishay Traeger (avishay@gmail.com)
4  * 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
25 #include <linux/slab.h>
26 #include <asm/div64.h>
27
28 #include <scsi/osd_ore.h>
29
30 #define ORE_ERR(fmt, a...) printk(KERN_ERR "ore: " fmt, ##a)
31
32 #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
46 static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
47 {
48         return ios->comps->comps[index & ios->comps->single_comp].cred;
49 }
50
51 static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
52 {
53         return &ios->comps->comps[index & ios->comps->single_comp].obj;
54 }
55
56 static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
57 {
58         return ios->comps->ods[index];
59 }
60
61 int  ore_get_rw_state(struct ore_layout *layout, struct ore_components *comps,
62                       bool is_reading, u64 offset, u64 length,
63                       struct ore_io_state **pios)
64 {
65         struct ore_io_state *ios;
66
67         /*TODO: Maybe use kmem_cach per sbi of size
68          * exofs_io_state_size(layout->s_numdevs)
69          */
70         ios = kzalloc(ore_io_state_size(comps->numdevs), GFP_KERNEL);
71         if (unlikely(!ios)) {
72                 ORE_DBGMSG("Failed kzalloc bytes=%d\n",
73                              ore_io_state_size(comps->numdevs));
74                 *pios = NULL;
75                 return -ENOMEM;
76         }
77
78         ios->layout = layout;
79         ios->comps = comps;
80         ios->offset = offset;
81         ios->length = length;
82         ios->reading = is_reading;
83
84         *pios = ios;
85         return 0;
86 }
87
88 int  ore_get_io_state(struct ore_layout *layout, struct ore_components *comps,
89                       struct ore_io_state **ios)
90 {
91         return ore_get_rw_state(layout, comps, true, 0, 0, ios);
92 }
93
94 void ore_put_io_state(struct ore_io_state *ios)
95 {
96         if (ios) {
97                 unsigned i;
98
99                 for (i = 0; i < ios->numdevs; i++) {
100                         struct ore_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
112 static void _sync_done(struct ore_io_state *ios, void *p)
113 {
114         struct completion *waiting = p;
115
116         complete(waiting);
117 }
118
119 static void _last_io(struct kref *kref)
120 {
121         struct ore_io_state *ios = container_of(
122                                         kref, struct ore_io_state, kref);
123
124         ios->done(ios, ios->private);
125 }
126
127 static void _done_io(struct osd_request *or, void *p)
128 {
129         struct ore_io_state *ios = p;
130
131         kref_put(&ios->kref, _last_io);
132 }
133
134 static int ore_io_execute(struct ore_io_state *ios)
135 {
136         DECLARE_COMPLETION_ONSTACK(wait);
137         bool sync = (ios->done == NULL);
138         int i, ret;
139
140         if (sync) {
141                 ios->done = _sync_done;
142                 ios->private = &wait;
143         }
144
145         for (i = 0; i < ios->numdevs; i++) {
146                 struct osd_request *or = ios->per_dev[i].or;
147                 if (unlikely(!or))
148                         continue;
149
150                 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
151                 if (unlikely(ret)) {
152                         ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
153                                      ret);
154                         return ret;
155                 }
156         }
157
158         kref_init(&ios->kref);
159
160         for (i = 0; i < ios->numdevs; i++) {
161                 struct osd_request *or = ios->per_dev[i].or;
162                 if (unlikely(!or))
163                         continue;
164
165                 kref_get(&ios->kref);
166                 osd_execute_request_async(or, _done_io, ios);
167         }
168
169         kref_put(&ios->kref, _last_io);
170         ret = 0;
171
172         if (sync) {
173                 wait_for_completion(&wait);
174                 ret = ore_check_io(ios, NULL);
175         }
176         return ret;
177 }
178
179 static void _clear_bio(struct bio *bio)
180 {
181         struct bio_vec *bv;
182         unsigned i;
183
184         __bio_for_each_segment(bv, bio, i, 0) {
185                 unsigned this_count = bv->bv_len;
186
187                 if (likely(PAGE_SIZE == this_count))
188                         clear_highpage(bv->bv_page);
189                 else
190                         zero_user(bv->bv_page, bv->bv_offset, this_count);
191         }
192 }
193
194 int ore_check_io(struct ore_io_state *ios, u64 *resid)
195 {
196         enum osd_err_priority acumulated_osd_err = 0;
197         int acumulated_lin_err = 0;
198         int i;
199
200         for (i = 0; i < ios->numdevs; i++) {
201                 struct osd_sense_info osi;
202                 struct osd_request *or = ios->per_dev[i].or;
203                 int ret;
204
205                 if (unlikely(!or))
206                         continue;
207
208                 ret = osd_req_decode_sense(or, &osi);
209                 if (likely(!ret))
210                         continue;
211
212                 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
213                         /* start read offset passed endof file */
214                         _clear_bio(ios->per_dev[i].bio);
215                         ORE_DBGMSG("start read offset passed end of file "
216                                 "offset=0x%llx, length=0x%llx\n",
217                                 _LLU(ios->per_dev[i].offset),
218                                 _LLU(ios->per_dev[i].length));
219
220                         continue; /* we recovered */
221                 }
222
223                 if (osi.osd_err_pri >= acumulated_osd_err) {
224                         acumulated_osd_err = osi.osd_err_pri;
225                         acumulated_lin_err = ret;
226                 }
227         }
228
229         /* TODO: raid specific residual calculations */
230         if (resid) {
231                 if (likely(!acumulated_lin_err))
232                         *resid = 0;
233                 else
234                         *resid = ios->length;
235         }
236
237         return acumulated_lin_err;
238 }
239
240 /*
241  * L - logical offset into the file
242  *
243  * U - The number of bytes in a stripe within a group
244  *
245  *      U = stripe_unit * group_width
246  *
247  * T - The number of bytes striped within a group of component objects
248  *     (before advancing to the next group)
249  *
250  *      T = stripe_unit * group_width * group_depth
251  *
252  * S - The number of bytes striped across all component objects
253  *     before the pattern repeats
254  *
255  *      S = stripe_unit * group_width * group_depth * group_count
256  *
257  * M - The "major" (i.e., across all components) stripe number
258  *
259  *      M = L / S
260  *
261  * G - Counts the groups from the beginning of the major stripe
262  *
263  *      G = (L - (M * S)) / T   [or (L % S) / T]
264  *
265  * H - The byte offset within the group
266  *
267  *      H = (L - (M * S)) % T   [or (L % S) % T]
268  *
269  * N - The "minor" (i.e., across the group) stripe number
270  *
271  *      N = H / U
272  *
273  * C - The component index coresponding to L
274  *
275  *      C = (H - (N * U)) / stripe_unit + G * group_width
276  *      [or (L % U) / stripe_unit + G * group_width]
277  *
278  * O - The component offset coresponding to L
279  *
280  *      O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
281  */
282 struct _striping_info {
283         u64 obj_offset;
284         u64 group_length;
285         u64 M; /* for truncate */
286         unsigned dev;
287         unsigned unit_off;
288 };
289
290 static void _calc_stripe_info(struct ore_layout *layout, u64 file_offset,
291                               struct _striping_info *si)
292 {
293         u32     stripe_unit = layout->stripe_unit;
294         u32     group_width = layout->group_width;
295         u64     group_depth = layout->group_depth;
296
297         u32     U = stripe_unit * group_width;
298         u64     T = U * group_depth;
299         u64     S = T * layout->group_count;
300         u64     M = div64_u64(file_offset, S);
301
302         /*
303         G = (L - (M * S)) / T
304         H = (L - (M * S)) % T
305         */
306         u64     LmodS = file_offset - M * S;
307         u32     G = div64_u64(LmodS, T);
308         u64     H = LmodS - G * T;
309
310         u32     N = div_u64(H, U);
311
312         /* "H - (N * U)" is just "H % U" so it's bound to u32 */
313         si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
314         si->dev *= layout->mirrors_p1;
315
316         div_u64_rem(file_offset, stripe_unit, &si->unit_off);
317
318         si->obj_offset = si->unit_off + (N * stripe_unit) +
319                                   (M * group_depth * stripe_unit);
320
321         si->group_length = T - H;
322         si->M = M;
323 }
324
325 static int _add_stripe_unit(struct ore_io_state *ios,  unsigned *cur_pg,
326                 unsigned pgbase, struct ore_per_dev_state *per_dev,
327                 int cur_len)
328 {
329         unsigned pg = *cur_pg;
330         struct request_queue *q =
331                         osd_request_queue(_ios_od(ios, per_dev->dev));
332
333         per_dev->length += cur_len;
334
335         if (per_dev->bio == NULL) {
336                 unsigned pages_in_stripe = ios->layout->group_width *
337                                         (ios->layout->stripe_unit / PAGE_SIZE);
338                 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
339                                                 ios->layout->group_width;
340
341                 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
342                 if (unlikely(!per_dev->bio)) {
343                         ORE_DBGMSG("Failed to allocate BIO size=%u\n",
344                                      bio_size);
345                         return -ENOMEM;
346                 }
347         }
348
349         while (cur_len > 0) {
350                 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
351                 unsigned added_len;
352
353                 BUG_ON(ios->nr_pages <= pg);
354                 cur_len -= pglen;
355
356                 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
357                                             pglen, pgbase);
358                 if (unlikely(pglen != added_len))
359                         return -ENOMEM;
360                 pgbase = 0;
361                 ++pg;
362         }
363         BUG_ON(cur_len);
364
365         *cur_pg = pg;
366         return 0;
367 }
368
369 static int _prepare_one_group(struct ore_io_state *ios, u64 length,
370                               struct _striping_info *si)
371 {
372         unsigned stripe_unit = ios->layout->stripe_unit;
373         unsigned mirrors_p1 = ios->layout->mirrors_p1;
374         unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
375         unsigned dev = si->dev;
376         unsigned first_dev = dev - (dev % devs_in_group);
377         unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
378         unsigned cur_pg = ios->pages_consumed;
379         int ret = 0;
380
381         while (length) {
382                 struct ore_per_dev_state *per_dev = &ios->per_dev[dev];
383                 unsigned cur_len, page_off = 0;
384
385                 if (!per_dev->length) {
386                         per_dev->dev = dev;
387                         if (dev < si->dev) {
388                                 per_dev->offset = si->obj_offset + stripe_unit -
389                                                                    si->unit_off;
390                                 cur_len = stripe_unit;
391                         } else if (dev == si->dev) {
392                                 per_dev->offset = si->obj_offset;
393                                 cur_len = stripe_unit - si->unit_off;
394                                 page_off = si->unit_off & ~PAGE_MASK;
395                                 BUG_ON(page_off && (page_off != ios->pgbase));
396                         } else { /* dev > si->dev */
397                                 per_dev->offset = si->obj_offset - si->unit_off;
398                                 cur_len = stripe_unit;
399                         }
400
401                         if (max_comp < dev)
402                                 max_comp = dev;
403                 } else {
404                         cur_len = stripe_unit;
405                 }
406                 if (cur_len >= length)
407                         cur_len = length;
408
409                 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
410                                        cur_len);
411                 if (unlikely(ret))
412                         goto out;
413
414                 dev += mirrors_p1;
415                 dev = (dev % devs_in_group) + first_dev;
416
417                 length -= cur_len;
418         }
419 out:
420         ios->numdevs = max_comp + mirrors_p1;
421         ios->pages_consumed = cur_pg;
422         return ret;
423 }
424
425 static int _prepare_for_striping(struct ore_io_state *ios)
426 {
427         u64 length = ios->length;
428         u64 offset = ios->offset;
429         struct _striping_info si;
430         int ret = 0;
431
432         if (!ios->pages) {
433                 if (ios->kern_buff) {
434                         struct ore_per_dev_state *per_dev = &ios->per_dev[0];
435
436                         _calc_stripe_info(ios->layout, ios->offset, &si);
437                         per_dev->offset = si.obj_offset;
438                         per_dev->dev = si.dev;
439
440                         /* no cross device without page array */
441                         BUG_ON((ios->layout->group_width > 1) &&
442                                (si.unit_off + ios->length >
443                                 ios->layout->stripe_unit));
444                 }
445                 ios->numdevs = ios->layout->mirrors_p1;
446                 return 0;
447         }
448
449         while (length) {
450                 _calc_stripe_info(ios->layout, offset, &si);
451
452                 if (length < si.group_length)
453                         si.group_length = length;
454
455                 ret = _prepare_one_group(ios, si.group_length, &si);
456                 if (unlikely(ret))
457                         goto out;
458
459                 offset += si.group_length;
460                 length -= si.group_length;
461         }
462
463 out:
464         return ret;
465 }
466
467 int ore_create(struct ore_io_state *ios)
468 {
469         int i, ret;
470
471         for (i = 0; i < ios->comps->numdevs; i++) {
472                 struct osd_request *or;
473
474                 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
475                 if (unlikely(!or)) {
476                         ORE_ERR("%s: osd_start_request failed\n", __func__);
477                         ret = -ENOMEM;
478                         goto out;
479                 }
480                 ios->per_dev[i].or = or;
481                 ios->numdevs++;
482
483                 osd_req_create_object(or, _ios_obj(ios, i));
484         }
485         ret = ore_io_execute(ios);
486
487 out:
488         return ret;
489 }
490
491 int ore_remove(struct ore_io_state *ios)
492 {
493         int i, ret;
494
495         for (i = 0; i < ios->comps->numdevs; i++) {
496                 struct osd_request *or;
497
498                 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
499                 if (unlikely(!or)) {
500                         ORE_ERR("%s: osd_start_request failed\n", __func__);
501                         ret = -ENOMEM;
502                         goto out;
503                 }
504                 ios->per_dev[i].or = or;
505                 ios->numdevs++;
506
507                 osd_req_remove_object(or, _ios_obj(ios, i));
508         }
509         ret = ore_io_execute(ios);
510
511 out:
512         return ret;
513 }
514
515 static int _write_mirror(struct ore_io_state *ios, int cur_comp)
516 {
517         struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
518         unsigned dev = ios->per_dev[cur_comp].dev;
519         unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
520         int ret = 0;
521
522         if (ios->pages && !master_dev->length)
523                 return 0; /* Just an empty slot */
524
525         for (; cur_comp < last_comp; ++cur_comp, ++dev) {
526                 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
527                 struct osd_request *or;
528
529                 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
530                 if (unlikely(!or)) {
531                         ORE_ERR("%s: osd_start_request failed\n", __func__);
532                         ret = -ENOMEM;
533                         goto out;
534                 }
535                 per_dev->or = or;
536                 per_dev->offset = master_dev->offset;
537
538                 if (ios->pages) {
539                         struct bio *bio;
540
541                         if (per_dev != master_dev) {
542                                 bio = bio_kmalloc(GFP_KERNEL,
543                                                   master_dev->bio->bi_max_vecs);
544                                 if (unlikely(!bio)) {
545                                         ORE_DBGMSG(
546                                               "Failed to allocate BIO size=%u\n",
547                                               master_dev->bio->bi_max_vecs);
548                                         ret = -ENOMEM;
549                                         goto out;
550                                 }
551
552                                 __bio_clone(bio, master_dev->bio);
553                                 bio->bi_bdev = NULL;
554                                 bio->bi_next = NULL;
555                                 per_dev->length = master_dev->length;
556                                 per_dev->bio =  bio;
557                                 per_dev->dev = dev;
558                         } else {
559                                 bio = master_dev->bio;
560                                 /* FIXME: bio_set_dir() */
561                                 bio->bi_rw |= REQ_WRITE;
562                         }
563
564                         osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
565                                       bio, per_dev->length);
566                         ORE_DBGMSG("write(0x%llx) offset=0x%llx "
567                                       "length=0x%llx dev=%d\n",
568                                      _LLU(_ios_obj(ios, dev)->id),
569                                      _LLU(per_dev->offset),
570                                      _LLU(per_dev->length), dev);
571                 } else if (ios->kern_buff) {
572                         ret = osd_req_write_kern(or, _ios_obj(ios, dev),
573                                                  per_dev->offset,
574                                                  ios->kern_buff, ios->length);
575                         if (unlikely(ret))
576                                 goto out;
577                         ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
578                                       "length=0x%llx dev=%d\n",
579                                      _LLU(_ios_obj(ios, dev)->id),
580                                      _LLU(per_dev->offset),
581                                      _LLU(ios->length), dev);
582                 } else {
583                         osd_req_set_attributes(or, _ios_obj(ios, dev));
584                         ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
585                                      _LLU(_ios_obj(ios, dev)->id),
586                                      ios->out_attr_len, dev);
587                 }
588
589                 if (ios->out_attr)
590                         osd_req_add_set_attr_list(or, ios->out_attr,
591                                                   ios->out_attr_len);
592
593                 if (ios->in_attr)
594                         osd_req_add_get_attr_list(or, ios->in_attr,
595                                                   ios->in_attr_len);
596         }
597
598 out:
599         return ret;
600 }
601
602 int ore_write(struct ore_io_state *ios)
603 {
604         int i;
605         int ret;
606
607         ret = _prepare_for_striping(ios);
608         if (unlikely(ret))
609                 return ret;
610
611         for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
612                 ret = _write_mirror(ios, i);
613                 if (unlikely(ret))
614                         return ret;
615         }
616
617         ret = ore_io_execute(ios);
618         return ret;
619 }
620
621 static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
622 {
623         struct osd_request *or;
624         struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
625         struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
626         unsigned first_dev = (unsigned)obj->id;
627
628         if (ios->pages && !per_dev->length)
629                 return 0; /* Just an empty slot */
630
631         first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
632         or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
633         if (unlikely(!or)) {
634                 ORE_ERR("%s: osd_start_request failed\n", __func__);
635                 return -ENOMEM;
636         }
637         per_dev->or = or;
638
639         if (ios->pages) {
640                 osd_req_read(or, obj, per_dev->offset,
641                                 per_dev->bio, per_dev->length);
642                 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
643                              " dev=%d\n", _LLU(obj->id),
644                              _LLU(per_dev->offset), _LLU(per_dev->length),
645                              first_dev);
646         } else if (ios->kern_buff) {
647                 int ret = osd_req_read_kern(or, obj, per_dev->offset,
648                                             ios->kern_buff, ios->length);
649                 ORE_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
650                               "length=0x%llx dev=%d ret=>%d\n",
651                               _LLU(obj->id), _LLU(per_dev->offset),
652                               _LLU(ios->length), first_dev, ret);
653                 if (unlikely(ret))
654                         return ret;
655         } else {
656                 osd_req_get_attributes(or, obj);
657                 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
658                               _LLU(obj->id),
659                               ios->in_attr_len, first_dev);
660         }
661         if (ios->out_attr)
662                 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
663
664         if (ios->in_attr)
665                 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
666
667         return 0;
668 }
669
670 int ore_read(struct ore_io_state *ios)
671 {
672         int i;
673         int ret;
674
675         ret = _prepare_for_striping(ios);
676         if (unlikely(ret))
677                 return ret;
678
679         for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
680                 ret = _read_mirror(ios, i);
681                 if (unlikely(ret))
682                         return ret;
683         }
684
685         ret = ore_io_execute(ios);
686         return ret;
687 }
688
689 int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
690 {
691         struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
692         void *iter = NULL;
693         int nelem;
694
695         do {
696                 nelem = 1;
697                 osd_req_decode_get_attr_list(ios->per_dev[0].or,
698                                              &cur_attr, &nelem, &iter);
699                 if ((cur_attr.attr_page == attr->attr_page) &&
700                     (cur_attr.attr_id == attr->attr_id)) {
701                         attr->len = cur_attr.len;
702                         attr->val_ptr = cur_attr.val_ptr;
703                         return 0;
704                 }
705         } while (iter);
706
707         return -EIO;
708 }
709
710 static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
711                              struct osd_attr *attr)
712 {
713         int last_comp = cur_comp + ios->layout->mirrors_p1;
714
715         for (; cur_comp < last_comp; ++cur_comp) {
716                 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
717                 struct osd_request *or;
718
719                 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
720                 if (unlikely(!or)) {
721                         ORE_ERR("%s: osd_start_request failed\n", __func__);
722                         return -ENOMEM;
723                 }
724                 per_dev->or = or;
725
726                 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
727                 osd_req_add_set_attr_list(or, attr, 1);
728         }
729
730         return 0;
731 }
732
733 struct _trunc_info {
734         struct _striping_info si;
735         u64 prev_group_obj_off;
736         u64 next_group_obj_off;
737
738         unsigned first_group_dev;
739         unsigned nex_group_dev;
740         unsigned max_devs;
741 };
742
743 void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
744                        struct _trunc_info *ti)
745 {
746         unsigned stripe_unit = layout->stripe_unit;
747
748         _calc_stripe_info(layout, file_offset, &ti->si);
749
750         ti->prev_group_obj_off = ti->si.M * stripe_unit;
751         ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
752
753         ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
754         ti->nex_group_dev = ti->first_group_dev + layout->group_width;
755         ti->max_devs = layout->group_width * layout->group_count;
756 }
757
758 int ore_truncate(struct ore_layout *layout, struct ore_components *comps,
759                    u64 size)
760 {
761         struct ore_io_state *ios;
762         struct exofs_trunc_attr {
763                 struct osd_attr attr;
764                 __be64 newsize;
765         } *size_attrs;
766         struct _trunc_info ti;
767         int i, ret;
768
769         ret = ore_get_io_state(layout, comps, &ios);
770         if (unlikely(ret))
771                 return ret;
772
773         _calc_trunk_info(ios->layout, size, &ti);
774
775         size_attrs = kcalloc(ti.max_devs, sizeof(*size_attrs),
776                              GFP_KERNEL);
777         if (unlikely(!size_attrs)) {
778                 ret = -ENOMEM;
779                 goto out;
780         }
781
782         ios->numdevs = ios->comps->numdevs;
783
784         for (i = 0; i < ti.max_devs; ++i) {
785                 struct exofs_trunc_attr *size_attr = &size_attrs[i];
786                 u64 obj_size;
787
788                 if (i < ti.first_group_dev)
789                         obj_size = ti.prev_group_obj_off;
790                 else if (i >= ti.nex_group_dev)
791                         obj_size = ti.next_group_obj_off;
792                 else if (i < ti.si.dev) /* dev within this group */
793                         obj_size = ti.si.obj_offset +
794                                       ios->layout->stripe_unit - ti.si.unit_off;
795                 else if (i == ti.si.dev)
796                         obj_size = ti.si.obj_offset;
797                 else /* i > ti.dev */
798                         obj_size = ti.si.obj_offset - ti.si.unit_off;
799
800                 size_attr->newsize = cpu_to_be64(obj_size);
801                 size_attr->attr = g_attr_logical_length;
802                 size_attr->attr.val_ptr = &size_attr->newsize;
803
804                 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
805                              _LLU(comps->comps->obj.id), _LLU(obj_size), i);
806                 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
807                                         &size_attr->attr);
808                 if (unlikely(ret))
809                         goto out;
810         }
811         ret = ore_io_execute(ios);
812
813 out:
814         kfree(size_attrs);
815         ore_put_io_state(ios);
816         return ret;
817 }
818
819 const struct osd_attr g_attr_logical_length = ATTR_DEF(
820         OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);