exofs: Move exofs specific osd operations out of ios.c
[firefly-linux-kernel-4.4.55.git] / fs / exofs / ios.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 <scsi/scsi_device.h>
27 #include <asm/div64.h>
28
29 #include "exofs.h"
30
31 #define EXOFS_DBGMSG2(M...) do {} while (0)
32 /* #define EXOFS_DBGMSG2 EXOFS_DBGMSG */
33
34 int  exofs_get_rw_state(struct exofs_layout *layout, bool is_reading,
35                         u64 offset, u64 length, struct exofs_io_state **pios)
36 {
37         struct exofs_io_state *ios;
38
39         /*TODO: Maybe use kmem_cach per sbi of size
40          * exofs_io_state_size(layout->s_numdevs)
41          */
42         ios = kzalloc(exofs_io_state_size(layout->s_numdevs), GFP_KERNEL);
43         if (unlikely(!ios)) {
44                 EXOFS_DBGMSG("Failed kzalloc bytes=%d\n",
45                              exofs_io_state_size(layout->s_numdevs));
46                 *pios = NULL;
47                 return -ENOMEM;
48         }
49
50         ios->layout = layout;
51         ios->obj.partition = layout->s_pid;
52         ios->offset = offset;
53         ios->length = length;
54         ios->reading = is_reading;
55
56         *pios = ios;
57         return 0;
58 }
59
60 int  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
66 void 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
84 static void _sync_done(struct exofs_io_state *ios, void *p)
85 {
86         struct completion *waiting = p;
87
88         complete(waiting);
89 }
90
91 static 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
99 static 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
106 static 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)) {
124                         EXOFS_DBGMSG("Failed to osd_finalize_request() => %d\n",
125                                      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         }
148         return ret;
149 }
150
151 static 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
166 int 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;
174                 struct osd_request *or = ios->per_dev[i].or;
175                 int ret;
176
177                 if (unlikely(!or))
178                         continue;
179
180                 ret = osd_req_decode_sense(or, &osi);
181                 if (likely(!ret))
182                         continue;
183
184                 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",
189                                 _LLU(ios->per_dev[i].offset),
190                                 _LLU(ios->per_dev[i].length));
191
192                         continue; /* we recovered */
193                 }
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
212 /*
213  * L - logical offset into the file
214  *
215  * U - The number of bytes in a stripe within a group
216  *
217  *      U = stripe_unit * group_width
218  *
219  * T - The number of bytes striped within a group of component objects
220  *     (before advancing to the next group)
221  *
222  *      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
244  *
245  * C - The component index coresponding to L
246  *
247  *      C = (H - (N * U)) / stripe_unit + G * group_width
248  *      [or (L % U) / stripe_unit + G * group_width]
249  *
250  * O - The component offset coresponding to L
251  *
252  *      O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
253  */
254 struct _striping_info {
255         u64 obj_offset;
256         u64 group_length;
257         u64 M; /* for truncate */
258         unsigned dev;
259         unsigned unit_off;
260 };
261
262 static void _calc_stripe_info(struct exofs_layout *layout, u64 file_offset,
263                               struct _striping_info *si)
264 {
265         u32     stripe_unit = layout->stripe_unit;
266         u32     group_width = layout->group_width;
267         u64     group_depth = layout->group_depth;
268
269         u32     U = stripe_unit * group_width;
270         u64     T = U * group_depth;
271         u64     S = T * layout->group_count;
272         u64     M = div64_u64(file_offset, S);
273
274         /*
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;
281
282         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;
286         si->dev *= layout->mirrors_p1;
287
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;
294         si->M = M;
295 }
296
297 static 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)
300 {
301         unsigned pg = *cur_pg;
302         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);
310                 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
311                                                 ios->layout->group_width;
312
313                 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
314                 if (unlikely(!per_dev->bio)) {
315                         EXOFS_DBGMSG("Failed to allocate BIO size=%u\n",
316                                      bio_size);
317                         return -ENOMEM;
318                 }
319         }
320
321         while (cur_len > 0) {
322                 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
323                 unsigned added_len;
324
325                 BUG_ON(ios->nr_pages <= pg);
326                 cur_len -= pglen;
327
328                 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
329                                             pglen, pgbase);
330                 if (unlikely(pglen != added_len))
331                         return -ENOMEM;
332                 pgbase = 0;
333                 ++pg;
334         }
335         BUG_ON(cur_len);
336
337         *cur_pg = pg;
338         return 0;
339 }
340
341 static int _prepare_one_group(struct exofs_io_state *ios, u64 length,
342                               struct _striping_info *si)
343 {
344         unsigned stripe_unit = ios->layout->stripe_unit;
345         unsigned mirrors_p1 = ios->layout->mirrors_p1;
346         unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
347         unsigned dev = si->dev;
348         unsigned first_dev = dev - (dev % devs_in_group);
349         unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
350         unsigned cur_pg = ios->pages_consumed;
351         int ret = 0;
352
353         while (length) {
354                 struct exofs_per_dev_state *per_dev = &ios->per_dev[dev];
355                 unsigned cur_len, page_off = 0;
356
357                 if (!per_dev->length) {
358                         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                         }
372
373                         if (max_comp < dev)
374                                 max_comp = dev;
375                 } else {
376                         cur_len = stripe_unit;
377                 }
378                 if (cur_len >= length)
379                         cur_len = length;
380
381                 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
382                                        cur_len);
383                 if (unlikely(ret))
384                         goto out;
385
386                 dev += mirrors_p1;
387                 dev = (dev % devs_in_group) + first_dev;
388
389                 length -= cur_len;
390         }
391 out:
392         ios->numdevs = max_comp + mirrors_p1;
393         ios->pages_consumed = cur_pg;
394         return ret;
395 }
396
397 static int _prepare_for_striping(struct exofs_io_state *ios)
398 {
399         u64 length = ios->length;
400         u64 offset = ios->offset;
401         struct _striping_info si;
402         int ret = 0;
403
404         if (!ios->pages) {
405                 if (ios->kern_buff) {
406                         struct exofs_per_dev_state *per_dev = &ios->per_dev[0];
407
408                         _calc_stripe_info(ios->layout, ios->offset, &si);
409                         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
421         while (length) {
422                 _calc_stripe_info(ios->layout, offset, &si);
423
424                 if (length < si.group_length)
425                         si.group_length = length;
426
427                 ret = _prepare_one_group(ios, si.group_length, &si);
428                 if (unlikely(ret))
429                         goto out;
430
431                 offset += si.group_length;
432                 length -= si.group_length;
433         }
434
435 out:
436         return ret;
437 }
438
439 int exofs_sbi_create(struct exofs_io_state *ios)
440 {
441         int i, ret;
442
443         for (i = 0; i < ios->layout->s_numdevs; i++) {
444                 struct osd_request *or;
445
446                 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
447                 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
459 out:
460         return ret;
461 }
462
463 int exofs_sbi_remove(struct exofs_io_state *ios)
464 {
465         int i, ret;
466
467         for (i = 0; i < ios->layout->s_numdevs; i++) {
468                 struct osd_request *or;
469
470                 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
471                 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
483 out:
484         return ret;
485 }
486
487 static int _sbi_write_mirror(struct exofs_io_state *ios, int cur_comp)
488 {
489         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;
493
494         if (ios->pages && !master_dev->length)
495                 return 0; /* Just an empty slot */
496
497         for (; cur_comp < last_comp; ++cur_comp, ++dev) {
498                 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
499                 struct osd_request *or;
500
501                 or = osd_start_request(exofs_ios_od(ios, dev), GFP_KERNEL);
502                 if (unlikely(!or)) {
503                         EXOFS_ERR("%s: osd_start_request failed\n", __func__);
504                         ret = -ENOMEM;
505                         goto out;
506                 }
507                 per_dev->or = or;
508                 per_dev->offset = master_dev->offset;
509
510                 if (ios->pages) {
511                         struct bio *bio;
512
513                         if (per_dev != master_dev) {
514                                 bio = bio_kmalloc(GFP_KERNEL,
515                                                   master_dev->bio->bi_max_vecs);
516                                 if (unlikely(!bio)) {
517                                         EXOFS_DBGMSG(
518                                               "Failed to allocate BIO size=%u\n",
519                                               master_dev->bio->bi_max_vecs);
520                                         ret = -ENOMEM;
521                                         goto out;
522                                 }
523
524                                 __bio_clone(bio, master_dev->bio);
525                                 bio->bi_bdev = NULL;
526                                 bio->bi_next = NULL;
527                                 per_dev->length = master_dev->length;
528                                 per_dev->bio =  bio;
529                                 per_dev->dev = dev;
530                         } else {
531                                 bio = master_dev->bio;
532                                 /* FIXME: bio_set_dir() */
533                                 bio->bi_rw |= REQ_WRITE;
534                         }
535
536                         osd_req_write(or, &ios->obj, per_dev->offset, bio,
537                                       per_dev->length);
538                         EXOFS_DBGMSG("write(0x%llx) offset=0x%llx "
539                                       "length=0x%llx dev=%d\n",
540                                      _LLU(ios->obj.id), _LLU(per_dev->offset),
541                                      _LLU(per_dev->length), dev);
542                 } else if (ios->kern_buff) {
543                         ret = osd_req_write_kern(or, &ios->obj, per_dev->offset,
544                                            ios->kern_buff, ios->length);
545                         if (unlikely(ret))
546                                 goto out;
547                         EXOFS_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
548                                       "length=0x%llx dev=%d\n",
549                                      _LLU(ios->obj.id), _LLU(per_dev->offset),
550                                      _LLU(ios->length), dev);
551                 } else {
552                         osd_req_set_attributes(or, &ios->obj);
553                         EXOFS_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
554                                      _LLU(ios->obj.id), ios->out_attr_len, dev);
555                 }
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         }
565
566 out:
567         return ret;
568 }
569
570 int 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
589 static int _sbi_read_mirror(struct exofs_io_state *ios, unsigned cur_comp)
590 {
591         struct osd_request *or;
592         struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
593         unsigned first_dev = (unsigned)ios->obj.id;
594
595         if (ios->pages && !per_dev->length)
596                 return 0; /* Just an empty slot */
597
598         first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
599         or = osd_start_request(exofs_ios_od(ios, first_dev), GFP_KERNEL);
600         if (unlikely(!or)) {
601                 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
602                 return -ENOMEM;
603         }
604         per_dev->or = or;
605
606         if (ios->pages) {
607                 osd_req_read(or, &ios->obj, per_dev->offset,
608                                 per_dev->bio, per_dev->length);
609                 EXOFS_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
610                              " dev=%d\n", _LLU(ios->obj.id),
611                              _LLU(per_dev->offset), _LLU(per_dev->length),
612                              first_dev);
613         } else if (ios->kern_buff) {
614                 int ret = osd_req_read_kern(or, &ios->obj, per_dev->offset,
615                                             ios->kern_buff, ios->length);
616                 EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
617                               "length=0x%llx dev=%d ret=>%d\n",
618                               _LLU(ios->obj.id), _LLU(per_dev->offset),
619                               _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         }
627         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
633         return 0;
634 }
635
636 int 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;
653 }
654
655 int extract_attr_from_ios(struct exofs_io_state *ios, struct osd_attr *attr)
656 {
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;
663                 osd_req_decode_get_attr_list(ios->per_dev[0].or,
664                                              &cur_attr, &nelem, &iter);
665                 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 }
675
676 static 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
699 struct _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
709 void _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
724 int 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;
728         struct exofs_trunc_attr {
729                 struct osd_attr attr;
730                 __be64 newsize;
731         } *size_attrs;
732         struct _trunc_info ti;
733         int i, ret;
734
735         ret = exofs_get_io_state(&sbi->layout, &ios);
736         if (unlikely(ret))
737                 return ret;
738
739         _calc_trunk_info(ios->layout, size, &ti);
740
741         size_attrs = kcalloc(ti.max_devs, sizeof(*size_attrs),
742                              GFP_KERNEL);
743         if (unlikely(!size_attrs)) {
744                 ret = -ENOMEM;
745                 goto out;
746         }
747
748         ios->obj.id = exofs_oi_objno(oi);
749         ios->cred = oi->i_cred;
750         ios->numdevs = ios->layout->s_numdevs;
751
752         for (i = 0; i < ti.max_devs; ++i) {
753                 struct exofs_trunc_attr *size_attr = &size_attrs[i];
754                 u64 obj_size;
755
756                 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;
767
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
772                 EXOFS_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
773                              _LLU(ios->obj.id), _LLU(obj_size), i);
774                 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
775                                         &size_attr->attr);
776                 if (unlikely(ret))
777                         goto out;
778         }
779         ret = exofs_io_execute(ios);
780
781 out:
782         kfree(size_attrs);
783         exofs_put_io_state(ios);
784         return ret;
785 }
786
787 const struct osd_attr g_attr_logical_length = ATTR_DEF(
788         OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);