Whamcloud - gitweb
fa84386366b029c15d68756835f24a4579c81622
[fs/lustre-release.git] / lustre / lov / lov_io.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Implementation of cl_io for LOV layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_LOV
39
40 #include "lov_cl_internal.h"
41
42 /** \addtogroup lov
43  *  @{
44  */
45
46 static inline struct lov_io_sub *lov_sub_alloc(struct lov_io *lio, int index)
47 {
48         struct lov_io_sub *sub;
49
50         if (lio->lis_nr_subios == 0) {
51                 LASSERT(lio->lis_single_subio_index == -1);
52                 sub = &lio->lis_single_subio;
53                 lio->lis_single_subio_index = index;
54                 memset(sub, 0, sizeof(*sub));
55         } else {
56                 OBD_ALLOC_PTR(sub);
57         }
58
59         if (sub != NULL) {
60                 INIT_LIST_HEAD(&sub->sub_list);
61                 INIT_LIST_HEAD(&sub->sub_linkage);
62                 sub->sub_subio_index = index;
63         }
64
65         return sub;
66 }
67
68 static inline void lov_sub_free(struct lov_io *lio, struct lov_io_sub *sub)
69 {
70         if (sub->sub_subio_index == lio->lis_single_subio_index) {
71                 LASSERT(sub == &lio->lis_single_subio);
72                 lio->lis_single_subio_index = -1;
73         } else {
74                 OBD_FREE_PTR(sub);
75         }
76 }
77
78 static void lov_io_sub_fini(const struct lu_env *env, struct lov_io *lio,
79                             struct lov_io_sub *sub)
80 {
81         ENTRY;
82
83         cl_io_fini(sub->sub_env, &sub->sub_io);
84
85         if (sub->sub_env != NULL && !IS_ERR(sub->sub_env)) {
86                 cl_env_put(sub->sub_env, &sub->sub_refcheck);
87                 sub->sub_env = NULL;
88         }
89         EXIT;
90 }
91
92 static inline bool
93 is_index_within_mirror(struct lov_object *lov, int index, int mirror_index)
94 {
95         struct lov_layout_composite *comp = &lov->u.composite;
96         struct lov_mirror_entry *lre = &comp->lo_mirrors[mirror_index];
97
98         return (index >= lre->lre_start && index <= lre->lre_end);
99 }
100
101 static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
102                            struct lov_io_sub *sub)
103 {
104         struct lov_object *lov = lio->lis_object;
105         struct cl_io *sub_io;
106         struct cl_object *sub_obj;
107         struct cl_io *io = lio->lis_cl.cis_io;
108         int index = lov_comp_entry(sub->sub_subio_index);
109         int stripe = lov_comp_stripe(sub->sub_subio_index);
110         int result = 0;
111         LASSERT(sub->sub_env == NULL);
112         ENTRY;
113
114         if (unlikely(!lov_r0(lov, index)->lo_sub ||
115                      !lov_r0(lov, index)->lo_sub[stripe]))
116                 RETURN(-EIO);
117
118         LASSERTF(is_index_within_mirror(lov, index, lio->lis_mirror_index),
119                  DFID "iot = %d, index = %d, mirror = %d\n",
120                  PFID(lu_object_fid(lov2lu(lov))), io->ci_type, index,
121                  lio->lis_mirror_index);
122
123         /* obtain new environment */
124         sub->sub_env = cl_env_get(&sub->sub_refcheck);
125         if (IS_ERR(sub->sub_env))
126                 result = PTR_ERR(sub->sub_env);
127
128         sub_obj = lovsub2cl(lov_r0(lov, index)->lo_sub[stripe]);
129         sub_io  = &sub->sub_io;
130
131         sub_io->ci_obj    = sub_obj;
132         sub_io->ci_result = 0;
133
134         sub_io->ci_parent  = io;
135         sub_io->ci_lockreq = io->ci_lockreq;
136         sub_io->ci_type    = io->ci_type;
137         sub_io->ci_no_srvlock = io->ci_no_srvlock;
138         sub_io->ci_noatime = io->ci_noatime;
139         sub_io->ci_pio = io->ci_pio;
140         sub_io->ci_lock_no_expand = io->ci_lock_no_expand;
141         sub_io->ci_ndelay = io->ci_ndelay;
142
143         result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
144
145         if (result < 0)
146                 lov_io_sub_fini(env, lio, sub);
147
148         RETURN(result);
149 }
150
151 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
152                                struct lov_io *lio, int index)
153 {
154         struct lov_io_sub *sub;
155         int rc = 0;
156
157         ENTRY;
158
159         list_for_each_entry(sub, &lio->lis_subios, sub_list) {
160                 if (sub->sub_subio_index == index) {
161                         rc = 1;
162                         break;
163                 }
164         }
165
166         if (rc == 0) {
167                 sub = lov_sub_alloc(lio, index);
168                 if (sub == NULL)
169                         GOTO(out, rc = -ENOMEM);
170
171                 rc = lov_io_sub_init(env, lio, sub);
172                 if (rc < 0) {
173                         lov_sub_free(lio, sub);
174                         GOTO(out, rc);
175                 }
176
177                 list_add_tail(&sub->sub_list, &lio->lis_subios);
178                 lio->lis_nr_subios++;
179         }
180 out:
181         if (rc < 0)
182                 sub = ERR_PTR(rc);
183         RETURN(sub);
184 }
185
186 /*****************************************************************************
187  *
188  * Lov io operations.
189  *
190  */
191
192 int lov_page_index(const struct cl_page *page)
193 {
194         const struct cl_page_slice *slice;
195         ENTRY;
196
197         slice = cl_page_at(page, &lov_device_type);
198         LASSERT(slice != NULL);
199         LASSERT(slice->cpl_obj != NULL);
200
201         RETURN(cl2lov_page(slice)->lps_index);
202 }
203
204 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
205                              struct cl_io *io)
206 {
207         ENTRY;
208
209         LASSERT(lio->lis_object != NULL);
210
211         INIT_LIST_HEAD(&lio->lis_subios);
212         lio->lis_single_subio_index = -1;
213         lio->lis_nr_subios = 0;
214
215         RETURN(0);
216 }
217
218 static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
219                                struct cl_io *io)
220 {
221         struct lov_layout_composite *comp = &obj->u.composite;
222         int index;
223         int i;
224         ENTRY;
225
226         if (!lov_is_flr(obj)) {
227                 LASSERT(comp->lo_preferred_mirror == 0);
228                 lio->lis_mirror_index = comp->lo_preferred_mirror;
229                 io->ci_ndelay = 0;
230                 RETURN(0);
231         }
232
233         if (io->ci_ndelay_tried == 0 || /* first time to try */
234             /* reset the mirror index if layout has changed */
235             lio->lis_mirror_layout_gen != obj->lo_lsm->lsm_layout_gen) {
236                 lio->lis_mirror_layout_gen = obj->lo_lsm->lsm_layout_gen;
237                 index = lio->lis_mirror_index = comp->lo_preferred_mirror;
238         } else {
239                 index = lio->lis_mirror_index;
240                 LASSERT(index >= 0);
241
242                 /* move mirror index to the next one */
243                 index = (index + 1) % comp->lo_mirror_count;
244         }
245
246         for (i = 0; i < comp->lo_mirror_count; i++) {
247                 struct lu_extent ext = { .e_start = lio->lis_pos,
248                                          .e_end   = lio->lis_pos + 1 };
249                 struct lov_mirror_entry *lre;
250                 struct lov_layout_entry *lle;
251                 bool found = false;
252
253                 lre = &comp->lo_mirrors[(index + i) % comp->lo_mirror_count];
254                 if (!lre->lre_valid)
255                         continue;
256
257                 lov_foreach_mirror_layout_entry(obj, lle, lre) {
258                         if (!lle->lle_valid)
259                                 continue;
260
261                         if (lu_extent_is_overlapped(&ext, lle->lle_extent)) {
262                                 found = true;
263                                 break;
264                         }
265                 }
266
267                 if (found) {
268                         index = (index + i) % comp->lo_mirror_count;
269                         break;
270                 }
271         }
272         if (i == comp->lo_mirror_count) {
273                 CERROR(DFID": failed to find a component covering "
274                        "I/O region at %llu\n",
275                        PFID(lu_object_fid(lov2lu(obj))), lio->lis_pos);
276
277                 dump_lsm(D_ERROR, obj->lo_lsm);
278
279                 RETURN(-EIO);
280         }
281
282         CDEBUG(D_VFSTRACE, DFID ": flr state: %d, move mirror from %d to %d, "
283                "have retried: %d, mirror count: %d\n",
284                PFID(lu_object_fid(lov2lu(obj))), lov_flr_state(obj),
285                lio->lis_mirror_index, index, io->ci_ndelay_tried,
286                comp->lo_mirror_count);
287
288         lio->lis_mirror_index = index;
289
290         /* FLR: if all mirrors have been tried once, most likely the network
291          * of this client has been partitioned. We should relinquish CPU for
292          * a while before trying again. */
293         ++io->ci_ndelay_tried;
294         if (io->ci_ndelay && io->ci_ndelay_tried >= comp->lo_mirror_count) {
295                 set_current_state(TASK_INTERRUPTIBLE);
296                 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC)); /* 10ms */
297                 if (signal_pending(current))
298                         RETURN(-EINTR);
299
300                 /* reset retry counter */
301                 io->ci_ndelay_tried = 1;
302         }
303
304         CDEBUG(D_VFSTRACE, "use %sdelayed RPC state for this IO\n",
305                io->ci_ndelay ? "non-" : "");
306
307         RETURN(0);
308 }
309
310 static int lov_io_slice_init(struct lov_io *lio,
311                              struct lov_object *obj, struct cl_io *io)
312 {
313         struct lu_extent ext;
314         int index;
315         int result = 0;
316         ENTRY;
317
318         io->ci_result = 0;
319         lio->lis_object = obj;
320
321         LASSERT(obj->lo_lsm != NULL);
322
323         switch (io->ci_type) {
324         case CIT_READ:
325         case CIT_WRITE:
326                 lio->lis_pos = io->u.ci_rw.rw_range.cir_pos;
327                 lio->lis_endpos = lio->lis_pos + io->u.ci_rw.rw_range.cir_count;
328                 lio->lis_io_endpos = lio->lis_endpos;
329                 if (cl_io_is_append(io)) {
330                         LASSERT(io->ci_type == CIT_WRITE);
331
332                         /* If there is LOV EA hole, then we may cannot locate
333                          * the current file-tail exactly. */
334                         if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
335                                      LOV_PATTERN_F_HOLE))
336                                 RETURN(-EIO);
337
338                         lio->lis_pos = 0;
339                         lio->lis_endpos = OBD_OBJECT_EOF;
340                 }
341                 break;
342
343         case CIT_SETATTR:
344                 if (cl_io_is_trunc(io))
345                         lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
346                 else
347                         lio->lis_pos = 0;
348                 lio->lis_endpos = OBD_OBJECT_EOF;
349                 break;
350
351         case CIT_DATA_VERSION:
352                 lio->lis_pos = 0;
353                 lio->lis_endpos = OBD_OBJECT_EOF;
354                 break;
355
356         case CIT_FAULT: {
357                 pgoff_t index = io->u.ci_fault.ft_index;
358                 lio->lis_pos = cl_offset(io->ci_obj, index);
359                 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
360                 break;
361         }
362
363         case CIT_FSYNC: {
364                 lio->lis_pos = io->u.ci_fsync.fi_start;
365                 lio->lis_endpos = io->u.ci_fsync.fi_end;
366                 break;
367         }
368
369         case CIT_LADVISE: {
370                 lio->lis_pos = io->u.ci_ladvise.li_start;
371                 lio->lis_endpos = io->u.ci_ladvise.li_end;
372                 break;
373         }
374
375         case CIT_GLIMPSE:
376                 lio->lis_pos = 0;
377                 lio->lis_endpos = OBD_OBJECT_EOF;
378
379                 if (lov_flr_state(obj) == LCM_FL_RDONLY &&
380                     !OBD_FAIL_CHECK(OBD_FAIL_FLR_GLIMPSE_IMMUTABLE))
381                         RETURN(1); /* SoM is accurate, no need glimpse */
382                 break;
383
384         case CIT_MISC:
385                 lio->lis_pos = 0;
386                 lio->lis_endpos = OBD_OBJECT_EOF;
387                 break;
388
389         default:
390                 LBUG();
391         }
392
393         result = lov_io_mirror_init(lio, obj, io);
394         if (result)
395                 RETURN(result);
396
397         /* check if it needs to instantiate layout */
398         if (!(io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io) ||
399               (cl_io_is_trunc(io) && io->u.ci_setattr.sa_attr.lvb_size > 0)))
400                 RETURN(0);
401
402         ext.e_start = lio->lis_pos;
403         ext.e_end = lio->lis_endpos;
404
405         /* for truncate, it only needs to instantiate the components
406          * before the truncated size. */
407         if (cl_io_is_trunc(io)) {
408                 ext.e_start = 0;
409                 ext.e_end = io->u.ci_setattr.sa_attr.lvb_size;
410         }
411
412         index = 0;
413         lov_foreach_io_layout(index, lio, &ext) {
414                 if (!lsm_entry_inited(obj->lo_lsm, index)) {
415                         io->ci_need_write_intent = 1;
416                         io->ci_write_intent = ext;
417                         result = 1;
418                         break;
419                 }
420         }
421
422         RETURN(result);
423 }
424
425 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
426 {
427         struct lov_io *lio = cl2lov_io(env, ios);
428         struct lov_object *lov = cl2lov(ios->cis_obj);
429
430         ENTRY;
431
432         LASSERT(list_empty(&lio->lis_active));
433
434         while (!list_empty(&lio->lis_subios)) {
435                 struct lov_io_sub *sub = list_entry(lio->lis_subios.next,
436                                                     struct lov_io_sub,
437                                                     sub_list);
438
439                 list_del_init(&sub->sub_list);
440                 lio->lis_nr_subios--;
441
442                 lov_io_sub_fini(env, lio, sub);
443                 lov_sub_free(lio, sub);
444         }
445         LASSERT(lio->lis_nr_subios == 0);
446
447         LASSERT(atomic_read(&lov->lo_active_ios) > 0);
448         if (atomic_dec_and_test(&lov->lo_active_ios))
449                 wake_up_all(&lov->lo_waitq);
450         EXIT;
451 }
452
453 static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
454                                loff_t start, loff_t end)
455 {
456         struct cl_io *io = &sub->sub_io;
457         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
458         struct cl_io *parent = lio->lis_cl.cis_io;
459         int index = lov_comp_entry(sub->sub_subio_index);
460         int stripe = lov_comp_stripe(sub->sub_subio_index);
461
462         io->ci_pio = parent->ci_pio;
463         switch (io->ci_type) {
464         case CIT_SETATTR: {
465                 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
466                 io->u.ci_setattr.sa_attr_flags =
467                         parent->u.ci_setattr.sa_attr_flags;
468                 io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
469                 io->u.ci_setattr.sa_stripe_index = stripe;
470                 io->u.ci_setattr.sa_parent_fid =
471                                         parent->u.ci_setattr.sa_parent_fid;
472                 if (cl_io_is_trunc(io)) {
473                         loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
474
475                         new_size = lov_size_to_stripe(lsm, index, new_size,
476                                                       stripe);
477                         io->u.ci_setattr.sa_attr.lvb_size = new_size;
478                 }
479                 lov_lsm2layout(lsm, lsm->lsm_entries[index],
480                                &io->u.ci_setattr.sa_layout);
481                 break;
482         }
483         case CIT_DATA_VERSION: {
484                 io->u.ci_data_version.dv_data_version = 0;
485                 io->u.ci_data_version.dv_flags =
486                         parent->u.ci_data_version.dv_flags;
487                 break;
488         }
489         case CIT_FAULT: {
490                 struct cl_object *obj = parent->ci_obj;
491                 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
492
493                 io->u.ci_fault = parent->u.ci_fault;
494                 off = lov_size_to_stripe(lsm, index, off, stripe);
495                 io->u.ci_fault.ft_index = cl_index(obj, off);
496                 break;
497         }
498         case CIT_FSYNC: {
499                 io->u.ci_fsync.fi_start = start;
500                 io->u.ci_fsync.fi_end = end;
501                 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
502                 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
503                 break;
504         }
505         case CIT_READ:
506         case CIT_WRITE: {
507                 io->u.ci_rw.rw_ptask = parent->u.ci_rw.rw_ptask;
508                 io->u.ci_rw.rw_iter = parent->u.ci_rw.rw_iter;
509                 io->u.ci_rw.rw_iocb = parent->u.ci_rw.rw_iocb;
510                 io->u.ci_rw.rw_file = parent->u.ci_rw.rw_file;
511                 io->u.ci_rw.rw_sync = parent->u.ci_rw.rw_sync;
512                 if (cl_io_is_append(parent)) {
513                         io->u.ci_rw.rw_append = 1;
514                 } else {
515                         io->u.ci_rw.rw_range.cir_pos = start;
516                         io->u.ci_rw.rw_range.cir_count = end - start;
517                 }
518                 break;
519         }
520         case CIT_LADVISE: {
521                 io->u.ci_ladvise.li_start = start;
522                 io->u.ci_ladvise.li_end = end;
523                 io->u.ci_ladvise.li_fid = parent->u.ci_ladvise.li_fid;
524                 io->u.ci_ladvise.li_advice = parent->u.ci_ladvise.li_advice;
525                 io->u.ci_ladvise.li_flags = parent->u.ci_ladvise.li_flags;
526                 break;
527         }
528         case CIT_GLIMPSE:
529         case CIT_MISC:
530         default:
531                 break;
532         }
533 }
534
535 static loff_t lov_offset_mod(loff_t val, int delta)
536 {
537         if (val != OBD_OBJECT_EOF)
538                 val += delta;
539         return val;
540 }
541
542 static int lov_io_iter_init(const struct lu_env *env,
543                             const struct cl_io_slice *ios)
544 {
545         struct lov_io        *lio = cl2lov_io(env, ios);
546         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
547         struct lov_io_sub    *sub;
548         struct lu_extent ext;
549         int index;
550         int rc = 0;
551
552         ENTRY;
553
554         ext.e_start = lio->lis_pos;
555         ext.e_end = lio->lis_endpos;
556
557         lov_foreach_io_layout(index, lio, &ext) {
558                 struct lov_layout_raid0 *r0 = lov_r0(lio->lis_object, index);
559                 u64 start;
560                 u64 end;
561                 int stripe;
562
563                 CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
564                        index, lsm->lsm_entries[index]->lsme_flags);
565                 if (!lsm_entry_inited(lsm, index)) {
566                         /* Read from uninitialized components should return
567                          * zero filled pages. */
568                         continue;
569                 }
570
571                 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
572                         if (!lov_stripe_intersects(lsm, index, stripe,
573                                                    &ext, &start, &end))
574                                 continue;
575
576                         if (unlikely(r0->lo_sub[stripe] == NULL)) {
577                                 if (ios->cis_io->ci_type == CIT_READ ||
578                                     ios->cis_io->ci_type == CIT_WRITE ||
579                                     ios->cis_io->ci_type == CIT_FAULT)
580                                         RETURN(-EIO);
581
582                                 continue;
583                         }
584
585                         end = lov_offset_mod(end, 1);
586                         sub = lov_sub_get(env, lio,
587                                           lov_comp_index(index, stripe));
588                         if (IS_ERR(sub)) {
589                                 rc = PTR_ERR(sub);
590                                 break;
591                         }
592
593                         lov_io_sub_inherit(sub, lio, start, end);
594                         rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
595                         if (rc != 0)
596                                 cl_io_iter_fini(sub->sub_env, &sub->sub_io);
597                         if (rc != 0)
598                                 break;
599
600                         CDEBUG(D_VFSTRACE,
601                                 "shrink stripe: {%d, %d} range: [%llu, %llu)\n",
602                                 index, stripe, start, end);
603
604                         list_add_tail(&sub->sub_linkage, &lio->lis_active);
605                 }
606                 if (rc != 0)
607                         break;
608         }
609         RETURN(rc);
610 }
611
612 static int lov_io_rw_iter_init(const struct lu_env *env,
613                                const struct cl_io_slice *ios)
614 {
615         struct cl_io *io = ios->cis_io;
616         struct lov_io *lio = cl2lov_io(env, ios);
617         struct lov_stripe_md_entry *lse;
618         struct cl_io_range *range = &io->u.ci_rw.rw_range;
619         loff_t start = range->cir_pos;
620         loff_t next;
621         int index;
622
623         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
624         ENTRY;
625
626         if (cl_io_is_append(io))
627                 RETURN(lov_io_iter_init(env, ios));
628
629         index = lov_io_layout_at(lio, range->cir_pos);
630         if (index < 0) { /* non-existing layout component */
631                 if (io->ci_type == CIT_READ) {
632                         /* TODO: it needs to detect the next component and
633                          * then set the next pos */
634                         io->ci_continue = 0;
635                         /* execute it in main thread */
636                         io->ci_pio = 0;
637
638                         RETURN(lov_io_iter_init(env, ios));
639                 }
640
641                 RETURN(-ENODATA);
642         }
643
644         lse = lov_lse(lio->lis_object, index);
645
646         next = MAX_LFS_FILESIZE;
647         if (lse->lsme_stripe_count > 1) {
648                 unsigned long ssize = lse->lsme_stripe_size;
649
650                 lov_do_div64(start, ssize);
651                 next = (start + 1) * ssize;
652                 if (next <= start * ssize)
653                         next = MAX_LFS_FILESIZE;
654         }
655
656         LASSERTF(range->cir_pos >= lse->lsme_extent.e_start,
657                  "pos %lld, [%lld, %lld)\n", range->cir_pos,
658                  lse->lsme_extent.e_start, lse->lsme_extent.e_end);
659         next = min_t(__u64, next, lse->lsme_extent.e_end);
660         next = min_t(loff_t, next, lio->lis_io_endpos);
661
662         io->ci_continue  = next < lio->lis_io_endpos;
663         range->cir_count = next - range->cir_pos;
664         lio->lis_pos     = range->cir_pos;
665         lio->lis_endpos  = range->cir_pos + range->cir_count;
666         CDEBUG(D_VFSTRACE,
667                "stripe: {%d, %llu} range: [%llu, %llu) end: %llu, count: %zd\n",
668                index, start, lio->lis_pos, lio->lis_endpos,
669                lio->lis_io_endpos, range->cir_count);
670
671         if (!io->ci_continue) {
672                 /* the last piece of IO, execute it in main thread */
673                 io->ci_pio = 0;
674         }
675
676         if (io->ci_pio)
677                 RETURN(0);
678
679         /*
680          * XXX The following call should be optimized: we know, that
681          * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
682          */
683         RETURN(lov_io_iter_init(env, ios));
684 }
685
686 static int lov_io_setattr_iter_init(const struct lu_env *env,
687                                     const struct cl_io_slice *ios)
688 {
689         struct lov_io *lio = cl2lov_io(env, ios);
690         struct cl_io *io = ios->cis_io;
691         int index;
692         ENTRY;
693
694         if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
695                 index = lov_io_layout_at(lio, lio->lis_pos - 1);
696                 /* no entry found for such offset */
697                 if (index < 0)
698                         RETURN(io->ci_result = -ENODATA);
699         }
700
701         RETURN(lov_io_iter_init(env, ios));
702 }
703
704 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
705                        int (*iofunc)(const struct lu_env *, struct cl_io *))
706 {
707         struct cl_io *parent = lio->lis_cl.cis_io;
708         struct lov_io_sub *sub;
709         int rc = 0;
710
711         ENTRY;
712         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
713                 rc = iofunc(sub->sub_env, &sub->sub_io);
714                 if (rc)
715                         break;
716
717                 if (parent->ci_result == 0)
718                         parent->ci_result = sub->sub_io.ci_result;
719         }
720         RETURN(rc);
721 }
722
723 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
724 {
725         ENTRY;
726         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
727 }
728
729 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
730 {
731         ENTRY;
732         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
733 }
734
735 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
736 {
737         ENTRY;
738         /*
739          * It's possible that lov_io_start() wasn't called against this
740          * sub-io, either because previous sub-io failed, or upper layer
741          * completed IO.
742          */
743         if (io->ci_state == CIS_IO_GOING)
744                 cl_io_end(env, io);
745         else
746                 io->ci_state = CIS_IO_FINISHED;
747         RETURN(0);
748 }
749
750 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
751 {
752         cl_io_iter_fini(env, io);
753         RETURN(0);
754 }
755
756 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
757 {
758         cl_io_unlock(env, io);
759         RETURN(0);
760 }
761
762 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
763 {
764         int rc;
765
766         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
767         LASSERT(rc == 0);
768 }
769
770 static void
771 lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
772 {
773         struct lov_io *lio = cl2lov_io(env, ios);
774         struct cl_io *parent = lio->lis_cl.cis_io;
775         struct lov_io_sub *sub;
776
777         ENTRY;
778         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
779                 lov_io_end_wrapper(env, &sub->sub_io);
780
781                 parent->u.ci_data_version.dv_data_version +=
782                         sub->sub_io.u.ci_data_version.dv_data_version;
783
784                 if (parent->ci_result == 0)
785                         parent->ci_result = sub->sub_io.ci_result;
786         }
787
788         EXIT;
789 }
790
791 static void lov_io_iter_fini(const struct lu_env *env,
792                              const struct cl_io_slice *ios)
793 {
794         struct lov_io *lio = cl2lov_io(env, ios);
795         int rc;
796
797         ENTRY;
798         rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
799         LASSERT(rc == 0);
800         while (!list_empty(&lio->lis_active))
801                 list_del_init(lio->lis_active.next);
802         EXIT;
803 }
804
805 static void lov_io_unlock(const struct lu_env *env,
806                           const struct cl_io_slice *ios)
807 {
808         int rc;
809
810         ENTRY;
811         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
812         LASSERT(rc == 0);
813         EXIT;
814 }
815
816 static int lov_io_read_ahead(const struct lu_env *env,
817                              const struct cl_io_slice *ios,
818                              pgoff_t start, struct cl_read_ahead *ra)
819 {
820         struct lov_io           *lio = cl2lov_io(env, ios);
821         struct lov_object       *loo = lio->lis_object;
822         struct cl_object        *obj = lov2cl(loo);
823         struct lov_layout_raid0 *r0;
824         struct lov_io_sub       *sub;
825         loff_t                   offset;
826         loff_t                   suboff;
827         pgoff_t                  ra_end;
828         unsigned int             pps; /* pages per stripe */
829         int                      stripe;
830         int                      index;
831         int                      rc;
832         ENTRY;
833
834         offset = cl_offset(obj, start);
835         index = lov_io_layout_at(lio, offset);
836         if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index))
837                 RETURN(-ENODATA);
838
839         stripe = lov_stripe_number(loo->lo_lsm, index, offset);
840
841         r0 = lov_r0(loo, index);
842         if (unlikely(r0->lo_sub[stripe] == NULL))
843                 RETURN(-EIO);
844
845         sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
846         if (IS_ERR(sub))
847                 RETURN(PTR_ERR(sub));
848
849         lov_stripe_offset(loo->lo_lsm, index, offset, stripe, &suboff);
850         rc = cl_io_read_ahead(sub->sub_env, &sub->sub_io,
851                               cl_index(lovsub2cl(r0->lo_sub[stripe]), suboff),
852                               ra);
853
854         CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
855                PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, r0->lo_nr, rc);
856         if (rc != 0)
857                 RETURN(rc);
858
859         /**
860          * Adjust the stripe index by layout of comp. ra->cra_end is the
861          * maximum page index covered by an underlying DLM lock.
862          * This function converts cra_end from stripe level to file level, and
863          * make sure it's not beyond stripe and component boundary.
864          */
865
866         /* cra_end is stripe level, convert it into file level */
867         ra_end = ra->cra_end;
868         if (ra_end != CL_PAGE_EOF)
869                 ra->cra_end = lov_stripe_pgoff(loo->lo_lsm, index,
870                                                ra_end, stripe);
871
872         /* boundary of current component */
873         ra_end = cl_index(obj, (loff_t)lov_io_extent(lio, index)->e_end);
874         if (ra_end != CL_PAGE_EOF && ra->cra_end >= ra_end)
875                 ra->cra_end = ra_end - 1;
876
877         if (r0->lo_nr == 1) /* single stripe file */
878                 RETURN(0);
879
880         pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
881
882         CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %u, "
883                "stripe_size = %u, stripe no = %u, start index = %lu\n",
884                PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, pps, index,
885                lov_lse(loo, index)->lsme_stripe_size, stripe, start);
886
887         /* never exceed the end of the stripe */
888         ra->cra_end = min_t(pgoff_t,
889                             ra->cra_end, start + pps - start % pps - 1);
890         RETURN(0);
891 }
892
893 /**
894  * lov implementation of cl_operations::cio_submit() method. It takes a list
895  * of pages in \a queue, splits it into per-stripe sub-lists, invokes
896  * cl_io_submit() on underlying devices to submit sub-lists, and then splices
897  * everything back.
898  *
899  * Major complication of this function is a need to handle memory cleansing:
900  * cl_io_submit() is called to write out pages as a part of VM memory
901  * reclamation, and hence it may not fail due to memory shortages (system
902  * dead-locks otherwise). To deal with this, some resources (sub-lists,
903  * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
904  * not-memory cleansing context), and in case of memory shortage, these
905  * pre-allocated resources are used by lov_io_submit() under
906  * lov_device::ld_mutex mutex.
907  */
908 static int lov_io_submit(const struct lu_env *env,
909                          const struct cl_io_slice *ios,
910                          enum cl_req_type crt, struct cl_2queue *queue)
911 {
912         struct cl_page_list     *qin = &queue->c2_qin;
913         struct lov_io           *lio = cl2lov_io(env, ios);
914         struct lov_io_sub       *sub;
915         struct cl_page_list     *plist = &lov_env_info(env)->lti_plist;
916         struct cl_page          *page;
917         int index;
918         int rc = 0;
919         ENTRY;
920
921         if (lio->lis_nr_subios == 1) {
922                 int idx = lio->lis_single_subio_index;
923
924                 sub = lov_sub_get(env, lio, idx);
925                 LASSERT(!IS_ERR(sub));
926                 LASSERT(sub == &lio->lis_single_subio);
927                 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
928                                      crt, queue);
929                 RETURN(rc);
930         }
931
932         cl_page_list_init(plist);
933         while (qin->pl_nr > 0) {
934                 struct cl_2queue  *cl2q = &lov_env_info(env)->lti_cl2q;
935
936                 cl_2queue_init(cl2q);
937
938                 page = cl_page_list_first(qin);
939                 cl_page_list_move(&cl2q->c2_qin, qin, page);
940
941                 index = lov_page_index(page);
942                 while (qin->pl_nr > 0) {
943                         page = cl_page_list_first(qin);
944                         if (index != lov_page_index(page))
945                                 break;
946
947                         cl_page_list_move(&cl2q->c2_qin, qin, page);
948                 }
949
950                 sub = lov_sub_get(env, lio, index);
951                 if (!IS_ERR(sub)) {
952                         rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
953                                              crt, cl2q);
954                 } else {
955                         rc = PTR_ERR(sub);
956                 }
957
958                 cl_page_list_splice(&cl2q->c2_qin, plist);
959                 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
960                 cl_2queue_fini(env, cl2q);
961
962                 if (rc != 0)
963                         break;
964         }
965
966         cl_page_list_splice(plist, qin);
967         cl_page_list_fini(env, plist);
968
969         RETURN(rc);
970 }
971
972 static int lov_io_commit_async(const struct lu_env *env,
973                                const struct cl_io_slice *ios,
974                                struct cl_page_list *queue, int from, int to,
975                                cl_commit_cbt cb)
976 {
977         struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
978         struct lov_io     *lio = cl2lov_io(env, ios);
979         struct lov_io_sub *sub;
980         struct cl_page *page;
981         int rc = 0;
982         ENTRY;
983
984         if (lio->lis_nr_subios == 1) {
985                 int idx = lio->lis_single_subio_index;
986
987                 sub = lov_sub_get(env, lio, idx);
988                 LASSERT(!IS_ERR(sub));
989                 LASSERT(sub == &lio->lis_single_subio);
990                 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io, queue,
991                                         from, to, cb);
992                 RETURN(rc);
993         }
994
995         cl_page_list_init(plist);
996         while (queue->pl_nr > 0) {
997                 int stripe_to = to;
998                 int index;
999
1000                 LASSERT(plist->pl_nr == 0);
1001                 page = cl_page_list_first(queue);
1002                 cl_page_list_move(plist, queue, page);
1003
1004                 index = lov_page_index(page);
1005                 while (queue->pl_nr > 0) {
1006                         page = cl_page_list_first(queue);
1007                         if (index != lov_page_index(page))
1008                                 break;
1009
1010                         cl_page_list_move(plist, queue, page);
1011                 }
1012
1013                 if (queue->pl_nr > 0) /* still has more pages */
1014                         stripe_to = PAGE_SIZE;
1015
1016                 sub = lov_sub_get(env, lio, index);
1017                 if (!IS_ERR(sub)) {
1018                         rc = cl_io_commit_async(sub->sub_env, &sub->sub_io,
1019                                                 plist, from, stripe_to, cb);
1020                 } else {
1021                         rc = PTR_ERR(sub);
1022                         break;
1023                 }
1024
1025                 if (plist->pl_nr > 0) /* short write */
1026                         break;
1027
1028                 from = 0;
1029         }
1030
1031         /* for error case, add the page back into the qin list */
1032         LASSERT(ergo(rc == 0, plist->pl_nr == 0));
1033         while (plist->pl_nr > 0) {
1034                 /* error occurred, add the uncommitted pages back into queue */
1035                 page = cl_page_list_last(plist);
1036                 cl_page_list_move_head(queue, plist, page);
1037         }
1038
1039         RETURN(rc);
1040 }
1041
1042 static int lov_io_fault_start(const struct lu_env *env,
1043                               const struct cl_io_slice *ios)
1044 {
1045         struct cl_fault_io *fio;
1046         struct lov_io      *lio;
1047         struct lov_io_sub  *sub;
1048
1049         ENTRY;
1050
1051         fio = &ios->cis_io->u.ci_fault;
1052         lio = cl2lov_io(env, ios);
1053         sub = lov_sub_get(env, lio, lov_page_index(fio->ft_page));
1054         sub->sub_io.u.ci_fault.ft_nob = fio->ft_nob;
1055
1056         RETURN(lov_io_start(env, ios));
1057 }
1058
1059 static void lov_io_fsync_end(const struct lu_env *env,
1060                              const struct cl_io_slice *ios)
1061 {
1062         struct lov_io *lio = cl2lov_io(env, ios);
1063         struct lov_io_sub *sub;
1064         unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
1065         ENTRY;
1066
1067         *written = 0;
1068         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1069                 struct cl_io *subio = &sub->sub_io;
1070
1071                 lov_io_end_wrapper(sub->sub_env, subio);
1072
1073                 if (subio->ci_result == 0)
1074                         *written += subio->u.ci_fsync.fi_nr_written;
1075         }
1076         RETURN_EXIT;
1077 }
1078
1079 static const struct cl_io_operations lov_io_ops = {
1080         .op = {
1081                 [CIT_READ] = {
1082                         .cio_fini      = lov_io_fini,
1083                         .cio_iter_init = lov_io_rw_iter_init,
1084                         .cio_iter_fini = lov_io_iter_fini,
1085                         .cio_lock      = lov_io_lock,
1086                         .cio_unlock    = lov_io_unlock,
1087                         .cio_start     = lov_io_start,
1088                         .cio_end       = lov_io_end
1089                 },
1090                 [CIT_WRITE] = {
1091                         .cio_fini      = lov_io_fini,
1092                         .cio_iter_init = lov_io_rw_iter_init,
1093                         .cio_iter_fini = lov_io_iter_fini,
1094                         .cio_lock      = lov_io_lock,
1095                         .cio_unlock    = lov_io_unlock,
1096                         .cio_start     = lov_io_start,
1097                         .cio_end       = lov_io_end
1098                 },
1099                 [CIT_SETATTR] = {
1100                         .cio_fini      = lov_io_fini,
1101                         .cio_iter_init = lov_io_setattr_iter_init,
1102                         .cio_iter_fini = lov_io_iter_fini,
1103                         .cio_lock      = lov_io_lock,
1104                         .cio_unlock    = lov_io_unlock,
1105                         .cio_start     = lov_io_start,
1106                         .cio_end       = lov_io_end
1107                 },
1108                 [CIT_DATA_VERSION] = {
1109                         .cio_fini       = lov_io_fini,
1110                         .cio_iter_init  = lov_io_iter_init,
1111                         .cio_iter_fini  = lov_io_iter_fini,
1112                         .cio_lock       = lov_io_lock,
1113                         .cio_unlock     = lov_io_unlock,
1114                         .cio_start      = lov_io_start,
1115                         .cio_end        = lov_io_data_version_end,
1116                 },
1117                 [CIT_FAULT] = {
1118                         .cio_fini      = lov_io_fini,
1119                         .cio_iter_init = lov_io_iter_init,
1120                         .cio_iter_fini = lov_io_iter_fini,
1121                         .cio_lock      = lov_io_lock,
1122                         .cio_unlock    = lov_io_unlock,
1123                         .cio_start     = lov_io_fault_start,
1124                         .cio_end       = lov_io_end
1125                 },
1126                 [CIT_FSYNC] = {
1127                         .cio_fini      = lov_io_fini,
1128                         .cio_iter_init = lov_io_iter_init,
1129                         .cio_iter_fini = lov_io_iter_fini,
1130                         .cio_lock      = lov_io_lock,
1131                         .cio_unlock    = lov_io_unlock,
1132                         .cio_start     = lov_io_start,
1133                         .cio_end       = lov_io_fsync_end
1134                 },
1135                 [CIT_LADVISE] = {
1136                         .cio_fini      = lov_io_fini,
1137                         .cio_iter_init = lov_io_iter_init,
1138                         .cio_iter_fini = lov_io_iter_fini,
1139                         .cio_lock      = lov_io_lock,
1140                         .cio_unlock    = lov_io_unlock,
1141                         .cio_start     = lov_io_start,
1142                         .cio_end       = lov_io_end
1143                 },
1144                 [CIT_GLIMPSE] = {
1145                         .cio_fini      = lov_io_fini,
1146                 },
1147                 [CIT_MISC] = {
1148                         .cio_fini      = lov_io_fini
1149                 }
1150         },
1151         .cio_read_ahead                = lov_io_read_ahead,
1152         .cio_submit                    = lov_io_submit,
1153         .cio_commit_async              = lov_io_commit_async,
1154 };
1155
1156 /*****************************************************************************
1157  *
1158  * Empty lov io operations.
1159  *
1160  */
1161
1162 static void lov_empty_io_fini(const struct lu_env *env,
1163                               const struct cl_io_slice *ios)
1164 {
1165         struct lov_object *lov = cl2lov(ios->cis_obj);
1166         ENTRY;
1167
1168         if (atomic_dec_and_test(&lov->lo_active_ios))
1169                 wake_up_all(&lov->lo_waitq);
1170         EXIT;
1171 }
1172
1173 static int lov_empty_io_submit(const struct lu_env *env,
1174                                const struct cl_io_slice *ios,
1175                                enum cl_req_type crt, struct cl_2queue *queue)
1176 {
1177         return -EBADF;
1178 }
1179
1180 static void lov_empty_impossible(const struct lu_env *env,
1181                                  struct cl_io_slice *ios)
1182 {
1183         LBUG();
1184 }
1185
1186 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
1187
1188 /**
1189  * An io operation vector for files without stripes.
1190  */
1191 static const struct cl_io_operations lov_empty_io_ops = {
1192         .op = {
1193                 [CIT_READ] = {
1194                         .cio_fini       = lov_empty_io_fini,
1195 #if 0
1196                         .cio_iter_init  = LOV_EMPTY_IMPOSSIBLE,
1197                         .cio_lock       = LOV_EMPTY_IMPOSSIBLE,
1198                         .cio_start      = LOV_EMPTY_IMPOSSIBLE,
1199                         .cio_end        = LOV_EMPTY_IMPOSSIBLE
1200 #endif
1201                 },
1202                 [CIT_WRITE] = {
1203                         .cio_fini      = lov_empty_io_fini,
1204                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1205                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
1206                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
1207                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
1208                 },
1209                 [CIT_SETATTR] = {
1210                         .cio_fini      = lov_empty_io_fini,
1211                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1212                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
1213                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
1214                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
1215                 },
1216                 [CIT_FAULT] = {
1217                         .cio_fini      = lov_empty_io_fini,
1218                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1219                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
1220                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
1221                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
1222                 },
1223                 [CIT_FSYNC] = {
1224                         .cio_fini      = lov_empty_io_fini
1225                 },
1226                 [CIT_LADVISE] = {
1227                         .cio_fini   = lov_empty_io_fini
1228                 },
1229                 [CIT_GLIMPSE] = {
1230                         .cio_fini      = lov_empty_io_fini
1231                 },
1232                 [CIT_MISC] = {
1233                         .cio_fini      = lov_empty_io_fini
1234                 }
1235         },
1236         .cio_submit                    = lov_empty_io_submit,
1237         .cio_commit_async              = LOV_EMPTY_IMPOSSIBLE
1238 };
1239
1240 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
1241                           struct cl_io *io)
1242 {
1243         struct lov_io       *lio = lov_env_io(env);
1244         struct lov_object   *lov = cl2lov(obj);
1245         int result;
1246         ENTRY;
1247
1248         INIT_LIST_HEAD(&lio->lis_active);
1249         result = lov_io_slice_init(lio, lov, io);
1250         if (result)
1251                 GOTO(out, result);
1252
1253         result = lov_io_subio_init(env, lio, io);
1254         if (!result) {
1255                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
1256                 atomic_inc(&lov->lo_active_ios);
1257         }
1258         EXIT;
1259 out:
1260         io->ci_result = result < 0 ? result : 0;
1261         return result;
1262 }
1263
1264 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
1265                       struct cl_io *io)
1266 {
1267         struct lov_object *lov = cl2lov(obj);
1268         struct lov_io *lio = lov_env_io(env);
1269         int result;
1270         ENTRY;
1271
1272         lio->lis_object = lov;
1273         switch (io->ci_type) {
1274         default:
1275                 LBUG();
1276         case CIT_MISC:
1277         case CIT_GLIMPSE:
1278         case CIT_READ:
1279                 result = 0;
1280                 break;
1281         case CIT_FSYNC:
1282         case CIT_LADVISE:
1283         case CIT_SETATTR:
1284         case CIT_DATA_VERSION:
1285                 result = +1;
1286                 break;
1287         case CIT_WRITE:
1288                 result = -EBADF;
1289                 break;
1290         case CIT_FAULT:
1291                 result = -EFAULT;
1292                 CERROR("Page fault on a file without stripes: "DFID"\n",
1293                        PFID(lu_object_fid(&obj->co_lu)));
1294                 break;
1295         }
1296         if (result == 0) {
1297                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1298                 atomic_inc(&lov->lo_active_ios);
1299         }
1300
1301         io->ci_result = result < 0 ? result : 0;
1302         RETURN(result);
1303 }
1304
1305 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
1306                         struct cl_io *io)
1307 {
1308         struct lov_object *lov = cl2lov(obj);
1309         struct lov_io *lio = lov_env_io(env);
1310         int result;
1311         ENTRY;
1312
1313         LASSERT(lov->lo_lsm != NULL);
1314         lio->lis_object = lov;
1315
1316         switch (io->ci_type) {
1317         default:
1318                 LASSERTF(0, "invalid type %d\n", io->ci_type);
1319                 result = -EOPNOTSUPP;
1320                 break;
1321         case CIT_GLIMPSE:
1322         case CIT_MISC:
1323         case CIT_FSYNC:
1324         case CIT_LADVISE:
1325         case CIT_DATA_VERSION:
1326                 result = 1;
1327                 break;
1328         case CIT_SETATTR:
1329                 /* the truncate to 0 is managed by MDT:
1330                  * - in open, for open O_TRUNC
1331                  * - in setattr, for truncate
1332                  */
1333                 /* the truncate is for size > 0 so triggers a restore */
1334                 if (cl_io_is_trunc(io)) {
1335                         io->ci_restore_needed = 1;
1336                         result = -ENODATA;
1337                 } else
1338                         result = 1;
1339                 break;
1340         case CIT_READ:
1341         case CIT_WRITE:
1342         case CIT_FAULT:
1343                 io->ci_restore_needed = 1;
1344                 result = -ENODATA;
1345                 break;
1346         }
1347
1348         if (result == 0) {
1349                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1350                 atomic_inc(&lov->lo_active_ios);
1351         }
1352
1353         io->ci_result = result < 0 ? result : 0;
1354         RETURN(result);
1355 }
1356
1357 /**
1358  * Return the index in composite:lo_entries by the file offset
1359  */
1360 int lov_io_layout_at(struct lov_io *lio, __u64 offset)
1361 {
1362         struct lov_object *lov = lio->lis_object;
1363         struct lov_layout_composite *comp = &lov->u.composite;
1364         int start_index = 0;
1365         int end_index = comp->lo_entry_count - 1;
1366         int i;
1367
1368         LASSERT(lov->lo_type == LLT_COMP);
1369
1370         /* This is actual file offset so nothing can cover eof. */
1371         if (offset == LUSTRE_EOF)
1372                 return -1;
1373
1374         if (lov_is_flr(lov)) {
1375                 struct lov_mirror_entry *lre;
1376
1377                 LASSERT(lio->lis_mirror_index >= 0);
1378
1379                 lre = &comp->lo_mirrors[lio->lis_mirror_index];
1380                 start_index = lre->lre_start;
1381                 end_index = lre->lre_end;
1382         }
1383
1384         for (i = start_index; i <= end_index; i++) {
1385                 struct lov_layout_entry *lle = lov_entry(lov, i);
1386
1387                 if ((offset >= lle->lle_extent->e_start &&
1388                      offset < lle->lle_extent->e_end) ||
1389                     (offset == OBD_OBJECT_EOF &&
1390                      lle->lle_extent->e_end == OBD_OBJECT_EOF))
1391                         return i;
1392         }
1393
1394         return -1;
1395 }
1396
1397 /** @} lov */