Whamcloud - gitweb
LU-4974 lod: Change pool_desc to "[lod|lov]_pool_desc"
[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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * Implementation of cl_io for LOV layer.
32  *
33  *   Author: Nikita Danilov <nikita.danilov@sun.com>
34  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOV
38
39 #include "lov_cl_internal.h"
40
41 /** \addtogroup lov
42  *  @{
43  */
44
45 /**
46  * Allocate a new sub IO
47  *
48  * \param[in] lio       top level lov IO structure
49  * \param[in] index     index into lov (stripe)
50  *
51  * \retval              Pointer to allocated lov_io_sub
52  *                      structure
53  */
54 static inline struct lov_io_sub *lov_sub_alloc(struct lov_io *lio, int index)
55 {
56         struct lov_io_sub *sub;
57
58         if (lio->lis_nr_subios == 0) {
59                 LASSERT(lio->lis_single_subio_index == -1);
60                 sub = &lio->lis_single_subio;
61                 lio->lis_single_subio_index = index;
62                 memset(sub, 0, sizeof(*sub));
63         } else {
64                 OBD_ALLOC_PTR(sub);
65         }
66
67         if (sub) {
68                 INIT_LIST_HEAD(&sub->sub_list);
69                 INIT_LIST_HEAD(&sub->sub_linkage);
70                 sub->sub_subio_index = index;
71         }
72
73         return sub;
74 }
75
76 /**
77  * Release a sub IO
78  *
79  * \param[in] lio       top level lov IO structure
80  * \param[in] sub       sub io to individual stripe
81  *
82  */
83 static inline void lov_sub_free(struct lov_io *lio, struct lov_io_sub *sub)
84 {
85         if (sub->sub_subio_index == lio->lis_single_subio_index) {
86                 LASSERT(sub == &lio->lis_single_subio);
87                 lio->lis_single_subio_index = -1;
88         } else {
89                 OBD_FREE_PTR(sub);
90         }
91 }
92
93 static void lov_io_sub_fini(const struct lu_env *env, struct lov_io *lio,
94                             struct lov_io_sub *sub)
95 {
96         ENTRY;
97
98         cl_io_fini(sub->sub_env, &sub->sub_io);
99
100         if (sub->sub_env && !IS_ERR(sub->sub_env)) {
101                 cl_env_put(sub->sub_env, &sub->sub_refcheck);
102                 sub->sub_env = NULL;
103         }
104         EXIT;
105 }
106
107 static inline bool
108 is_index_within_mirror(struct lov_object *lov, int index, int mirror_index)
109 {
110         struct lov_layout_composite *comp = &lov->u.composite;
111         struct lov_mirror_entry *lre = &comp->lo_mirrors[mirror_index];
112
113         return (index >= lre->lre_start && index <= lre->lre_end);
114 }
115
116 static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
117                            struct lov_io_sub *sub)
118 {
119         struct lov_object *lov = lio->lis_object;
120         struct cl_io *sub_io;
121         struct cl_object *sub_obj;
122         struct cl_io *io = lio->lis_cl.cis_io;
123         int index = lov_comp_entry(sub->sub_subio_index);
124         int stripe = lov_comp_stripe(sub->sub_subio_index);
125         int result = 0;
126         LASSERT(sub->sub_env == NULL);
127         ENTRY;
128
129         if (unlikely(!lov_r0(lov, index)->lo_sub ||
130                      !lov_r0(lov, index)->lo_sub[stripe]))
131                 RETURN(-EIO);
132
133         LASSERTF(ergo(lov_is_flr(lov),
134                       is_index_within_mirror(lov, index,
135                                              lio->lis_mirror_index)),
136                  DFID "iot = %d, index = %d, mirror = %d\n",
137                  PFID(lu_object_fid(lov2lu(lov))), io->ci_type, index,
138                  lio->lis_mirror_index);
139
140         /* obtain new environment */
141         sub->sub_env = cl_env_get(&sub->sub_refcheck);
142         if (IS_ERR(sub->sub_env)) {
143                 result = PTR_ERR(sub->sub_env);
144                 RETURN(result);
145         }
146
147         sub_obj = lovsub2cl(lov_r0(lov, index)->lo_sub[stripe]);
148         sub_io  = &sub->sub_io;
149
150         sub_io->ci_obj    = sub_obj;
151         sub_io->ci_result = 0;
152
153         sub_io->ci_parent  = io;
154         sub_io->ci_lockreq = io->ci_lockreq;
155         sub_io->ci_type    = io->ci_type;
156         sub_io->ci_no_srvlock = io->ci_no_srvlock;
157         sub_io->ci_noatime = io->ci_noatime;
158         sub_io->ci_async_readahead = io->ci_async_readahead;
159         sub_io->ci_lock_no_expand = io->ci_lock_no_expand;
160         sub_io->ci_ndelay = io->ci_ndelay;
161         sub_io->ci_layout_version = io->ci_layout_version;
162         sub_io->ci_tried_all_mirrors = io->ci_tried_all_mirrors;
163
164         result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
165
166         if (result < 0)
167                 lov_io_sub_fini(env, lio, sub);
168
169         RETURN(result);
170 }
171
172 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
173                                struct lov_io *lio, int index)
174 {
175         struct lov_io_sub *sub;
176         int rc = 0;
177
178         ENTRY;
179
180         list_for_each_entry(sub, &lio->lis_subios, sub_list) {
181                 if (sub->sub_subio_index == index) {
182                         rc = 1;
183                         break;
184                 }
185         }
186
187         if (rc == 0) {
188                 sub = lov_sub_alloc(lio, index);
189                 if (!sub)
190                         GOTO(out, rc = -ENOMEM);
191
192                 rc = lov_io_sub_init(env, lio, sub);
193                 if (rc < 0) {
194                         lov_sub_free(lio, sub);
195                         GOTO(out, rc);
196                 }
197
198                 list_add_tail(&sub->sub_list, &lio->lis_subios);
199                 lio->lis_nr_subios++;
200         }
201 out:
202         if (rc < 0)
203                 sub = ERR_PTR(rc);
204         else
205                 sub->sub_io.ci_noquota = lio->lis_cl.cis_io->ci_noquota;
206         RETURN(sub);
207 }
208
209 /**
210  * Lov io operations.
211  */
212 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
213                              struct cl_io *io)
214 {
215         ENTRY;
216
217         LASSERT(lio->lis_object != NULL);
218
219         INIT_LIST_HEAD(&lio->lis_subios);
220         lio->lis_single_subio_index = -1;
221         lio->lis_nr_subios = 0;
222
223         RETURN(0);
224 }
225
226 /**
227  * Decide if it will need write intent RPC
228  */
229 static int lov_io_mirror_write_intent(struct lov_io *lio,
230         struct lov_object *obj, struct cl_io *io)
231 {
232         struct lu_object *lobj = lov2lu(obj);
233         struct lov_layout_composite *comp = &obj->u.composite;
234         struct lu_extent *ext = &io->ci_write_intent;
235         struct lov_mirror_entry *lre;
236         struct lov_mirror_entry *primary;
237         struct lov_layout_entry *lle;
238         size_t count = 0;
239         ENTRY;
240
241         *ext = (typeof(*ext)) { lio->lis_pos, lio->lis_endpos };
242         io->ci_need_write_intent = 0;
243
244         if (!(io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io) ||
245               cl_io_is_fallocate(io) || cl_io_is_trunc(io)))
246                 RETURN(0);
247
248         /*
249          * FLR: check if it needs to send a write intent RPC to server.
250          * Writing to sync_pending file needs write intent RPC to change
251          * the file state back to write_pending, so that the layout version
252          * can be increased when the state changes to sync_pending at a later
253          * time. Otherwise there exists a chance that an evicted client may
254          * dirty the file data while resync client is working on it.
255          * Designated I/O is allowed for resync workload.
256          */
257         if (lov_flr_state(obj) == LCM_FL_RDONLY ||
258             (lov_flr_state(obj) == LCM_FL_SYNC_PENDING &&
259              io->ci_designated_mirror == 0)) {
260                 io->ci_need_write_intent = 1;
261                 RETURN(0);
262         }
263
264         LASSERT((lov_flr_state(obj) == LCM_FL_WRITE_PENDING));
265         LASSERT(comp->lo_preferred_mirror >= 0);
266
267         /*
268          * need to iterate all components to see if there are
269          * multiple components covering the writing component
270          */
271         primary = &comp->lo_mirrors[comp->lo_preferred_mirror];
272         if (primary->lre_stale || !primary->lre_valid) {
273                 /**
274                  * new server could pick a primary mirror which old client
275                  * does not recognize, and old client would mark it as
276                  * invalid.
277                  */
278                 CERROR(DFID ": cannot find known valid non-stale mirror, "
279                        "could be new server picked a mirror which this client "
280                        "does not recognize.\n",
281                        PFID(lu_object_fid(lobj)));
282                 RETURN(-EIO);
283         }
284
285         lov_foreach_mirror_layout_entry(obj, lle, primary) {
286                 LASSERT(lle->lle_valid);
287                 if (!lu_extent_is_overlapped(ext, lle->lle_extent))
288                         continue;
289
290                 ext->e_start = min(ext->e_start, lle->lle_extent->e_start);
291                 ext->e_end = max(ext->e_end, lle->lle_extent->e_end);
292                 ++count;
293         }
294         if (count == 0) {
295                 CERROR(DFID ": cannot find any valid components covering "
296                        "file extent "DEXT", mirror: %d\n",
297                        PFID(lu_object_fid(lobj)), PEXT(ext),
298                        primary->lre_mirror_id);
299                 RETURN(-EIO);
300         }
301
302         count = 0;
303         lov_foreach_mirror_entry(obj, lre) {
304                 if (lre == primary)
305                         continue;
306
307                 lov_foreach_mirror_layout_entry(obj, lle, lre) {
308                         if (!lle->lle_valid)
309                                 continue;
310
311                         if (lu_extent_is_overlapped(ext, lle->lle_extent)) {
312                                 ++count;
313                                 break;
314                         }
315                 }
316         }
317
318         CDEBUG(D_VFSTRACE, DFID "there are %zd components to be staled to "
319                "modify file extent "DEXT", iot: %d\n",
320                PFID(lu_object_fid(lobj)), count, PEXT(ext), io->ci_type);
321
322         io->ci_need_write_intent = count > 0;
323
324         RETURN(0);
325 }
326
327 static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
328                               struct cl_io *io)
329 {
330         struct lov_layout_composite *comp = &obj->u.composite;
331         int index;
332         int i;
333         int result;
334         ENTRY;
335
336         if (!lov_is_flr(obj)) {
337                 /* only locks/pages are manipulated for CIT_MISC op, no
338                  * cl_io_loop() will be called, don't check/set mirror info.
339                  */
340                 if (io->ci_type != CIT_MISC) {
341                         LASSERT(comp->lo_preferred_mirror == 0);
342                         lio->lis_mirror_index = comp->lo_preferred_mirror;
343                 }
344                 io->ci_ndelay = 0;
345                 RETURN(0);
346         }
347
348         /* transfer the layout version for verification */
349         if (io->ci_layout_version == 0)
350                 io->ci_layout_version = obj->lo_lsm->lsm_layout_gen;
351
352         /* find the corresponding mirror for designated mirror IO */
353         if (io->ci_designated_mirror > 0) {
354                 struct lov_mirror_entry *entry;
355
356                 LASSERT(!io->ci_ndelay);
357
358                 CDEBUG(D_LAYOUT, "designated I/O mirror state: %d\n",
359                       lov_flr_state(obj));
360
361                 if ((cl_io_is_trunc(io) || io->ci_type == CIT_WRITE ||
362                      cl_io_is_fallocate(io)) &&
363                     (io->ci_layout_version != obj->lo_lsm->lsm_layout_gen)) {
364                         /*
365                          * For resync I/O, the ci_layout_version was the layout
366                          * version when resync starts. If it doesn't match the
367                          * current object layout version, it means the layout
368                          * has been changed
369                          */
370                         RETURN(-ESTALE);
371                 }
372
373                 io->ci_layout_version |= LU_LAYOUT_RESYNC;
374
375                 index = 0;
376                 lio->lis_mirror_index = -1;
377                 lov_foreach_mirror_entry(obj, entry) {
378                         if (entry->lre_mirror_id ==
379                             io->ci_designated_mirror) {
380                                 lio->lis_mirror_index = index;
381                                 break;
382                         }
383
384                         index++;
385                 }
386
387                 RETURN(lio->lis_mirror_index < 0 ? -EINVAL : 0);
388         }
389
390         result = lov_io_mirror_write_intent(lio, obj, io);
391         if (result)
392                 RETURN(result);
393
394         if (io->ci_need_write_intent) {
395                 CDEBUG(D_VFSTRACE, DFID " need write intent for [%llu, %llu)\n",
396                        PFID(lu_object_fid(lov2lu(obj))),
397                        lio->lis_pos, lio->lis_endpos);
398
399                 if (cl_io_is_trunc(io)) {
400                         /**
401                          * for truncate, we uses [size, EOF) to judge whether
402                          * a write intent needs to be send, but we need to
403                          * restore the write extent to [0, size], in truncate,
404                          * the byte in the size position is accessed.
405                          */
406                         io->ci_write_intent.e_start = 0;
407                         io->ci_write_intent.e_end =
408                                         io->u.ci_setattr.sa_attr.lvb_size + 1;
409                 }
410                 /* stop cl_io_init() loop */
411                 RETURN(1);
412         }
413
414         if (io->ci_ndelay_tried == 0 || /* first time to try */
415             /* reset the mirror index if layout has changed */
416             lio->lis_mirror_layout_gen != obj->lo_lsm->lsm_layout_gen) {
417                 lio->lis_mirror_layout_gen = obj->lo_lsm->lsm_layout_gen;
418                 index = lio->lis_mirror_index = comp->lo_preferred_mirror;
419         } else {
420                 index = lio->lis_mirror_index;
421                 LASSERT(index >= 0);
422
423                 /* move mirror index to the next one */
424                 index = (index + 1) % comp->lo_mirror_count;
425         }
426
427         for (i = 0; i < comp->lo_mirror_count; i++) {
428                 struct lu_extent ext = { .e_start = lio->lis_pos,
429                                          .e_end   = lio->lis_pos + 1 };
430                 struct lov_mirror_entry *lre;
431                 struct lov_layout_entry *lle;
432                 bool found = false;
433
434                 lre = &comp->lo_mirrors[(index + i) % comp->lo_mirror_count];
435                 if (!lre->lre_valid)
436                         continue;
437
438                 if (lre->lre_foreign)
439                         continue;
440
441                 lov_foreach_mirror_layout_entry(obj, lle, lre) {
442                         if (!lle->lle_valid)
443                                 continue;
444
445                         if (lu_extent_is_overlapped(&ext, lle->lle_extent)) {
446                                 found = true;
447                                 break;
448                         }
449                 } /* each component of the mirror */
450                 if (found) {
451                         index = (index + i) % comp->lo_mirror_count;
452                         break;
453                 }
454         } /* each mirror */
455
456         if (i == comp->lo_mirror_count) {
457                 CERROR(DFID": failed to find a component covering "
458                        "I/O region at %llu\n",
459                        PFID(lu_object_fid(lov2lu(obj))), lio->lis_pos);
460
461                 dump_lsm(D_ERROR, obj->lo_lsm);
462
463                 RETURN(-EIO);
464         }
465
466         CDEBUG(D_VFSTRACE, DFID ": flr state: %d, move mirror from %d to %d, "
467                "have retried: %d, mirror count: %d\n",
468                PFID(lu_object_fid(lov2lu(obj))), lov_flr_state(obj),
469                lio->lis_mirror_index, index, io->ci_ndelay_tried,
470                comp->lo_mirror_count);
471
472         lio->lis_mirror_index = index;
473
474         /*
475          * FLR: if all mirrors have been tried once, most likely the network
476          * of this client has been partitioned. We should relinquish CPU for
477          * a while before trying again.
478          */
479         if (io->ci_ndelay && io->ci_ndelay_tried > 0 &&
480             (io->ci_ndelay_tried % comp->lo_mirror_count == 0)) {
481                 schedule_timeout_interruptible(cfs_time_seconds(1) / 100);
482                 if (signal_pending(current))
483                         RETURN(-EINTR);
484
485                 /**
486                  * we'd set ci_tried_all_mirrors to turn off fast mirror
487                  * switching for read after we've tried all mirrors several
488                  * rounds.
489                  */
490                 io->ci_tried_all_mirrors = io->ci_ndelay_tried %
491                                            (comp->lo_mirror_count * 4) == 0;
492         }
493         ++io->ci_ndelay_tried;
494
495         CDEBUG(D_VFSTRACE, "use %sdelayed RPC state for this IO\n",
496                io->ci_ndelay ? "non-" : "");
497
498         RETURN(0);
499 }
500
501 static int lov_io_slice_init(struct lov_io *lio,
502                              struct lov_object *obj, struct cl_io *io)
503 {
504         int index;
505         int result = 0;
506         ENTRY;
507
508         io->ci_result = 0;
509         lio->lis_object = obj;
510         lio->lis_cached_entry = LIS_CACHE_ENTRY_NONE;
511
512         switch (io->ci_type) {
513         case CIT_READ:
514         case CIT_WRITE:
515                 lio->lis_pos = io->u.ci_rw.crw_pos;
516                 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_bytes;
517                 lio->lis_io_endpos = lio->lis_endpos;
518                 if (cl_io_is_append(io)) {
519                         LASSERT(io->ci_type == CIT_WRITE);
520
521                         /*
522                          * If there is LOV EA hole, then we may cannot locate
523                          * the current file-tail exactly.
524                          */
525                         if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
526                                      LOV_PATTERN_F_HOLE))
527                                 GOTO(out, result = -EIO);
528
529                         lio->lis_pos = 0;
530                         lio->lis_endpos = OBD_OBJECT_EOF;
531                 }
532                 break;
533
534         case CIT_SETATTR:
535                 if (cl_io_is_fallocate(io)) {
536                         lio->lis_pos = io->u.ci_setattr.sa_falloc_offset;
537                         lio->lis_endpos = io->u.ci_setattr.sa_falloc_end;
538                 } else if (cl_io_is_trunc(io)) {
539                         lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
540                         lio->lis_endpos = OBD_OBJECT_EOF;
541                 } else {
542                         lio->lis_pos = 0;
543                         lio->lis_endpos = OBD_OBJECT_EOF;
544                 }
545                 break;
546
547         case CIT_DATA_VERSION:
548                 lio->lis_pos = 0;
549                 lio->lis_endpos = OBD_OBJECT_EOF;
550                 break;
551
552         case CIT_FAULT: {
553                 pgoff_t index = io->u.ci_fault.ft_index;
554
555                 lio->lis_pos = index << PAGE_SHIFT;
556                 lio->lis_endpos = (index + 1) << PAGE_SHIFT;
557                 break;
558         }
559
560         case CIT_FSYNC: {
561                 lio->lis_pos = io->u.ci_fsync.fi_start;
562                 lio->lis_endpos = io->u.ci_fsync.fi_end;
563                 break;
564         }
565
566         case CIT_LADVISE: {
567                 lio->lis_pos = io->u.ci_ladvise.li_start;
568                 lio->lis_endpos = io->u.ci_ladvise.li_end;
569                 break;
570         }
571
572         case CIT_LSEEK: {
573                 lio->lis_pos = io->u.ci_lseek.ls_start;
574                 lio->lis_endpos = OBD_OBJECT_EOF;
575                 break;
576         }
577
578         case CIT_GLIMPSE:
579                 lio->lis_pos = 0;
580                 lio->lis_endpos = OBD_OBJECT_EOF;
581                 break;
582
583         case CIT_MISC:
584                 lio->lis_pos = 0;
585                 lio->lis_endpos = OBD_OBJECT_EOF;
586                 break;
587
588         default:
589                 LBUG();
590         }
591
592         /*
593          * CIT_MISC + ci_ignore_layout can identify the I/O from the OSC layer,
594          * it won't care/access lov layout related info.
595          */
596         if (io->ci_ignore_layout && io->ci_type == CIT_MISC)
597                 GOTO(out, result = 0);
598
599         LASSERT(obj->lo_lsm != NULL);
600
601         result = lov_io_mirror_init(lio, obj, io);
602         if (result)
603                 GOTO(out, result);
604
605         /* check if it needs to instantiate layout */
606         if (!(io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io) ||
607               cl_io_is_fallocate(io) ||
608               (cl_io_is_trunc(io) && io->u.ci_setattr.sa_attr.lvb_size > 0)))
609                 GOTO(out, result = 0);
610
611         /*
612          * for truncate, it only needs to instantiate the components
613          * before the truncated size.
614          */
615         if (cl_io_is_trunc(io)) {
616                 io->ci_write_intent.e_start = 0;
617                 /* for writes, e_end is endpos, the location of the file
618                  * pointer after the write is completed, so it is not accessed.
619                  * For truncate, 'end' is the size, and *is* acccessed.
620                  * In other words, writes are [start, end), but truncate is
621                  * [start, size], where both are included.  So add 1 to the
622                  * size when creating the write intent to account for this.
623                  */
624                 io->ci_write_intent.e_end =
625                         io->u.ci_setattr.sa_attr.lvb_size + 1;
626         } else {
627                 io->ci_write_intent.e_start = lio->lis_pos;
628                 io->ci_write_intent.e_end = lio->lis_endpos;
629         }
630
631         index = 0;
632         lov_foreach_io_layout(index, lio, &io->ci_write_intent) {
633                 if (!lsm_entry_inited(obj->lo_lsm, index)) {
634                         io->ci_need_write_intent = 1;
635                         break;
636                 }
637         }
638
639         if (io->ci_need_write_intent && io->ci_designated_mirror > 0) {
640                 /*
641                  * REINT_SYNC RPC has already tried to instantiate all of the
642                  * components involved, obviously it didn't succeed. Skip this
643                  * mirror for now. The server won't be able to figure out
644                  * which mirror it should instantiate components
645                  */
646                 CERROR(DFID": trying to instantiate components for designated "
647                        "I/O, file state: %d\n",
648                        PFID(lu_object_fid(lov2lu(obj))), lov_flr_state(obj));
649
650                 io->ci_need_write_intent = 0;
651                 GOTO(out, result = -EIO);
652         }
653
654         if (io->ci_need_write_intent)
655                 GOTO(out, result = 1);
656
657         EXIT;
658
659 out:
660         return result;
661 }
662
663 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
664 {
665         struct lov_io *lio = cl2lov_io(env, ios);
666         struct lov_object *lov = cl2lov(ios->cis_obj);
667         struct lov_io_sub *sub;
668
669         ENTRY;
670         LASSERT(list_empty(&lio->lis_active));
671
672         while ((sub = list_first_entry_or_null(&lio->lis_subios,
673                                                struct lov_io_sub,
674                                                sub_list)) != NULL) {
675                 list_del_init(&sub->sub_list);
676                 lio->lis_nr_subios--;
677
678                 lov_io_sub_fini(env, lio, sub);
679                 lov_sub_free(lio, sub);
680         }
681         LASSERT(lio->lis_nr_subios == 0);
682
683         LASSERT(atomic_read(&lov->lo_active_ios) > 0);
684         if (atomic_dec_and_test(&lov->lo_active_ios))
685                 wake_up(&lov->lo_waitq);
686         EXIT;
687 }
688
689 static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
690                                loff_t start, loff_t end)
691 {
692         struct cl_io *io = &sub->sub_io;
693         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
694         struct cl_io *parent = lio->lis_cl.cis_io;
695         int index = lov_comp_entry(sub->sub_subio_index);
696         int stripe = lov_comp_stripe(sub->sub_subio_index);
697
698         switch (io->ci_type) {
699         case CIT_SETATTR: {
700                 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
701                 io->u.ci_setattr.sa_attr_flags =
702                         parent->u.ci_setattr.sa_attr_flags;
703                 io->u.ci_setattr.sa_avalid = parent->u.ci_setattr.sa_avalid;
704                 io->u.ci_setattr.sa_xvalid = parent->u.ci_setattr.sa_xvalid;
705                 io->u.ci_setattr.sa_falloc_mode =
706                         parent->u.ci_setattr.sa_falloc_mode;
707                 io->u.ci_setattr.sa_stripe_index = stripe;
708                 io->u.ci_setattr.sa_parent_fid =
709                                         parent->u.ci_setattr.sa_parent_fid;
710                 /* For SETATTR(fallocate) pass the subtype to lower IO */
711                 io->u.ci_setattr.sa_subtype = parent->u.ci_setattr.sa_subtype;
712                 if (cl_io_is_fallocate(io)) {
713                         io->u.ci_setattr.sa_falloc_offset = start;
714                         io->u.ci_setattr.sa_falloc_end = end;
715                         io->u.ci_setattr.sa_falloc_uid =
716                                 parent->u.ci_setattr.sa_falloc_uid;
717                         io->u.ci_setattr.sa_falloc_gid =
718                                 parent->u.ci_setattr.sa_falloc_gid;
719                         io->u.ci_setattr.sa_falloc_projid =
720                                 parent->u.ci_setattr.sa_falloc_projid;
721                 }
722                 if (cl_io_is_trunc(io)) {
723                         loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
724
725                         new_size = lov_size_to_stripe(lsm, index, new_size,
726                                                       stripe);
727                         io->u.ci_setattr.sa_attr.lvb_size = new_size;
728                 }
729                 lov_lsm2layout(lsm, lsm->lsm_entries[index],
730                                &io->u.ci_setattr.sa_layout);
731                 break;
732         }
733         case CIT_DATA_VERSION: {
734                 io->u.ci_data_version.dv_data_version = 0;
735                 io->u.ci_data_version.dv_flags =
736                         parent->u.ci_data_version.dv_flags;
737                 break;
738         }
739         case CIT_FAULT: {
740                 loff_t off = parent->u.ci_fault.ft_index << PAGE_SHIFT;
741
742                 io->u.ci_fault = parent->u.ci_fault;
743                 off = lov_size_to_stripe(lsm, index, off, stripe);
744                 io->u.ci_fault.ft_index = off >> PAGE_SHIFT;
745                 break;
746         }
747         case CIT_FSYNC: {
748                 io->u.ci_fsync.fi_start = start;
749                 io->u.ci_fsync.fi_end = end;
750                 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
751                 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
752                 break;
753         }
754         case CIT_READ:
755         case CIT_WRITE: {
756                 io->u.ci_wr.wr_sync = cl_io_is_sync_write(parent);
757                 io->ci_tried_all_mirrors = parent->ci_tried_all_mirrors;
758                 if (cl_io_is_append(parent)) {
759                         io->u.ci_wr.wr_append = 1;
760                 } else {
761                         io->u.ci_rw.crw_pos = start;
762                         io->u.ci_rw.crw_bytes = end - start;
763                 }
764                 break;
765         }
766         case CIT_LADVISE: {
767                 io->u.ci_ladvise.li_start = start;
768                 io->u.ci_ladvise.li_end = end;
769                 io->u.ci_ladvise.li_fid = parent->u.ci_ladvise.li_fid;
770                 io->u.ci_ladvise.li_advice = parent->u.ci_ladvise.li_advice;
771                 io->u.ci_ladvise.li_flags = parent->u.ci_ladvise.li_flags;
772                 break;
773         }
774         case CIT_LSEEK: {
775                 io->u.ci_lseek.ls_start = start;
776                 io->u.ci_lseek.ls_whence = parent->u.ci_lseek.ls_whence;
777                 io->u.ci_lseek.ls_result = parent->u.ci_lseek.ls_result;
778                 break;
779         }
780         case CIT_GLIMPSE:
781         case CIT_MISC:
782         default:
783                 break;
784         }
785 }
786
787 static loff_t lov_offset_mod(loff_t val, int delta)
788 {
789         if (val != OBD_OBJECT_EOF)
790                 val += delta;
791         return val;
792 }
793
794 static int lov_io_add_sub(const struct lu_env *env, struct lov_io *lio,
795                           struct lov_io_sub *sub, u64 start, u64 end)
796 {
797         int rc;
798
799         end = lov_offset_mod(end, 1);
800         lov_io_sub_inherit(sub, lio, start, end);
801         rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
802         if (rc != 0) {
803                 cl_io_iter_fini(sub->sub_env, &sub->sub_io);
804                 return rc;
805         }
806
807         list_add_tail(&sub->sub_linkage, &lio->lis_active);
808
809         return rc;
810 }
811 static int lov_io_iter_init(const struct lu_env *env,
812                             const struct cl_io_slice *ios)
813 {
814         struct lov_io *lio = cl2lov_io(env, ios);
815         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
816         bool is_trunc = cl_io_is_trunc(ios->cis_io);
817         struct lov_io_sub *sub;
818         struct lu_extent ext;
819         int index;
820         int rc = 0;
821
822         ENTRY;
823
824         ext.e_start = lio->lis_pos;
825         ext.e_end = lio->lis_endpos;
826
827         if (is_trunc) {
828                 OBD_ALLOC_PTR_ARRAY(lio->lis_trunc_stripe_index,
829                                     lio->lis_object->u.composite.lo_entry_count);
830                 if (lio->lis_trunc_stripe_index == NULL)
831                         RETURN(-ENOMEM);
832         }
833
834         lov_foreach_io_layout(index, lio, &ext) {
835                 struct lov_layout_entry *le = lov_entry(lio->lis_object, index);
836                 struct lov_layout_raid0 *r0 = &le->lle_raid0;
837                 u64 start;
838                 u64 end;
839                 int stripe;
840                 bool tested_trunc_stripe = false;
841
842                 if (is_trunc)
843                         lio->lis_trunc_stripe_index[index] = -1;
844
845                 CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
846                        index, lsm->lsm_entries[index]->lsme_flags);
847                 if (!lsm_entry_inited(lsm, index)) {
848                         /*
849                          * Read from uninitialized components should return
850                          * zero filled pages.
851                          */
852                         continue;
853                 }
854
855                 if (lsm_entry_is_foreign(lsm, index))
856                         continue;
857
858                 if (!le->lle_valid && !ios->cis_io->ci_designated_mirror) {
859                         CERROR("I/O to invalid component: %d, mirror: %d\n",
860                                index, lio->lis_mirror_index);
861                         RETURN(-EIO);
862                 }
863
864                 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
865                         if (!lov_stripe_intersects(lsm, index, stripe,
866                                                    &ext, &start, &end))
867                                 continue;
868
869                         if (unlikely(!r0->lo_sub[stripe])) {
870                                 if (ios->cis_io->ci_type == CIT_READ ||
871                                     ios->cis_io->ci_type == CIT_WRITE ||
872                                     ios->cis_io->ci_type == CIT_FAULT)
873                                         RETURN(-EIO);
874
875                                 continue;
876                         }
877
878                         if (is_trunc && !tested_trunc_stripe) {
879                                 int prev;
880                                 u64 tr_start;
881
882                                 prev = (stripe == 0) ? r0->lo_nr - 1 :
883                                                         stripe - 1;
884                                 /**
885                                  * Only involving previous stripe if the
886                                  * truncate in this component is at the
887                                  * beginning of this stripe.
888                                  */
889                                 tested_trunc_stripe = true;
890                                 if (ext.e_start < lsm->lsm_entries[index]->
891                                                         lsme_extent.e_start) {
892                                         /* need previous stripe involvement */
893                                         lio->lis_trunc_stripe_index[index] = prev;
894                                 } else {
895                                         div64_u64_rem(ext.e_start,
896                                                       stripe_width(lsm, index),
897                                                       &tr_start);
898                                         /* tr_start %= stripe_swidth */
899                                         if (tr_start == stripe * lsm->
900                                                         lsm_entries[index]->
901                                                         lsme_stripe_size)
902                                                 lio->lis_trunc_stripe_index[index] = prev;
903                                 }
904                         }
905
906                         /* if the last stripe is the trunc stripeno */
907                         if (is_trunc &&
908                             lio->lis_trunc_stripe_index[index] == stripe)
909                                 lio->lis_trunc_stripe_index[index] = -1;
910
911                         sub = lov_sub_get(env, lio,
912                                           lov_comp_index(index, stripe));
913                         if (IS_ERR(sub))
914                                 return PTR_ERR(sub);
915
916                         rc = lov_io_add_sub(env, lio, sub, start, end);
917                         if (rc != 0)
918                                 break;
919                 }
920                 if (rc != 0)
921                         break;
922
923                 if (is_trunc && lio->lis_trunc_stripe_index[index] != -1) {
924                         stripe = lio->lis_trunc_stripe_index[index];
925                         if (unlikely(!r0->lo_sub[stripe])) {
926                                 lio->lis_trunc_stripe_index[index] = -1;
927                                 continue;
928                         }
929                         sub = lov_sub_get(env, lio,
930                                           lov_comp_index(index, stripe));
931                         if (IS_ERR(sub))
932                                 return PTR_ERR(sub);
933
934                         /**
935                          * the prev sub could be used by another truncate, we'd
936                          * skip it. LU-14128 happends when expand truncate +
937                          * read get wrong kms.
938                          */
939                         if (!list_empty(&sub->sub_linkage)) {
940                                 lio->lis_trunc_stripe_index[index] = -1;
941                                 continue;
942                         }
943
944                         (void)lov_stripe_intersects(lsm, index, stripe, &ext,
945                                                     &start, &end);
946                         rc = lov_io_add_sub(env, lio, sub, start, end);
947                         if (rc != 0)
948                                 break;
949
950                 }
951         }
952         RETURN(rc);
953 }
954
955 static int lov_io_rw_iter_init(const struct lu_env *env,
956                                const struct cl_io_slice *ios)
957 {
958         struct lov_io *lio = cl2lov_io(env, ios);
959         struct cl_io *io = ios->cis_io;
960         struct lov_stripe_md_entry *lse;
961         loff_t start = io->u.ci_rw.crw_pos;
962         loff_t next;
963         int index;
964
965         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
966         ENTRY;
967
968         if (cl_io_is_append(io))
969                 RETURN(lov_io_iter_init(env, ios));
970
971         index = lov_io_layout_at(lio, io->u.ci_rw.crw_pos);
972         if (index < 0) { /* non-existing layout component */
973                 if (io->ci_type == CIT_READ) {
974                         /*
975                          * TODO: it needs to detect the next component and
976                          * then set the next pos
977                          */
978                         io->ci_continue = 0;
979
980                         RETURN(lov_io_iter_init(env, ios));
981                 }
982
983                 RETURN(-ENODATA);
984         }
985
986         if (!lov_entry(lio->lis_object, index)->lle_valid &&
987             !io->ci_designated_mirror)
988                 RETURN(io->ci_type == CIT_READ ? -EAGAIN : -EIO);
989
990         lse = lov_lse(lio->lis_object, index);
991
992         if (lsme_is_foreign(lse))
993                 RETURN(-EINVAL);
994
995         next = MAX_LFS_FILESIZE;
996         if (lse->lsme_stripe_count > 1) {
997                 unsigned long ssize = lse->lsme_stripe_size;
998
999                 start = div64_u64(start, ssize);
1000                 next = (start + 1) * ssize;
1001                 if (next <= start * ssize)
1002                         next = MAX_LFS_FILESIZE;
1003         }
1004
1005         LASSERTF(io->u.ci_rw.crw_pos >= lse->lsme_extent.e_start,
1006                  "pos %lld, [%lld, %lld)\n", io->u.ci_rw.crw_pos,
1007                  lse->lsme_extent.e_start, lse->lsme_extent.e_end);
1008         next = min_t(__u64, next, lse->lsme_extent.e_end);
1009         next = min_t(loff_t, next, lio->lis_io_endpos);
1010
1011         io->ci_continue = next < lio->lis_io_endpos;
1012         io->u.ci_rw.crw_bytes = next - io->u.ci_rw.crw_pos;
1013         lio->lis_pos    = io->u.ci_rw.crw_pos;
1014         lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_bytes;
1015         CDEBUG(D_VFSTRACE,
1016                "stripe: %llu chunk: [%llu, %llu) %llu, %zd\n",
1017                (__u64)start, lio->lis_pos, lio->lis_endpos,
1018                (__u64)lio->lis_io_endpos, io->u.ci_rw.crw_bytes);
1019
1020         /*
1021          * XXX The following call should be optimized: we know, that
1022          * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
1023          */
1024         RETURN(lov_io_iter_init(env, ios));
1025 }
1026
1027 static int lov_io_setattr_iter_init(const struct lu_env *env,
1028                                     const struct cl_io_slice *ios)
1029 {
1030         struct lov_io *lio = cl2lov_io(env, ios);
1031         struct cl_io *io = ios->cis_io;
1032         int index;
1033         ENTRY;
1034
1035         if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
1036                 index = lov_io_layout_at(lio, lio->lis_pos - 1);
1037                 /* no entry found for such offset */
1038                 if (index < 0)
1039                         RETURN(io->ci_result = -ENODATA);
1040         }
1041
1042         RETURN(lov_io_iter_init(env, ios));
1043 }
1044
1045 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
1046                        int (*iofunc)(const struct lu_env *, struct cl_io *))
1047 {
1048         struct cl_io *parent = lio->lis_cl.cis_io;
1049         struct lov_io_sub *sub;
1050         int rc = 0;
1051
1052         ENTRY;
1053         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1054                 rc = iofunc(sub->sub_env, &sub->sub_io);
1055                 if (rc) {
1056                         /**
1057                          * fsync race with truncate, we'd continue to other
1058                          * OST object's fsync to potentially discard
1059                          * caching pages (osc_cache_writeback_range).
1060                          */
1061                         if (rc == -ENOENT && parent->ci_type == CIT_FSYNC)
1062                                 continue;
1063                         break;
1064                 }
1065
1066                 if (parent->ci_result == 0)
1067                         parent->ci_result = sub->sub_io.ci_result;
1068         }
1069         RETURN(rc);
1070 }
1071
1072 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
1073 {
1074         ENTRY;
1075         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
1076 }
1077
1078 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
1079 {
1080         ENTRY;
1081         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
1082 }
1083
1084 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
1085 {
1086         ENTRY;
1087         /*
1088          * It's possible that lov_io_start() wasn't called against this
1089          * sub-io, either because previous sub-io failed, or upper layer
1090          * completed IO.
1091          */
1092         if (io->ci_state == CIS_IO_GOING)
1093                 cl_io_end(env, io);
1094         else
1095                 io->ci_state = CIS_IO_FINISHED;
1096         RETURN(0);
1097 }
1098
1099 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
1100 {
1101         cl_io_iter_fini(env, io);
1102         RETURN(0);
1103 }
1104
1105 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
1106 {
1107         cl_io_unlock(env, io);
1108         RETURN(0);
1109 }
1110
1111 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
1112 {
1113         int rc;
1114
1115         /* Before ending each i/o, we must set lis_cached_entry to tell the
1116          * next i/o not to use stale cached lis information.
1117          */
1118         cl2lov_io(env, ios)->lis_cached_entry = LIS_CACHE_ENTRY_NONE;
1119
1120         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
1121         LASSERT(rc == 0);
1122 }
1123
1124 static void
1125 lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
1126 {
1127         struct lov_io *lio = cl2lov_io(env, ios);
1128         struct cl_io *parent = lio->lis_cl.cis_io;
1129         struct cl_data_version_io *pdv = &parent->u.ci_data_version;
1130         struct lov_io_sub *sub;
1131
1132         ENTRY;
1133         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1134                 struct cl_data_version_io *sdv = &sub->sub_io.u.ci_data_version;
1135
1136                 lov_io_end_wrapper(sub->sub_env, &sub->sub_io);
1137
1138                 pdv->dv_data_version += sdv->dv_data_version;
1139                 if (pdv->dv_layout_version > sdv->dv_layout_version)
1140                         pdv->dv_layout_version = sdv->dv_layout_version;
1141
1142                 if (parent->ci_result == 0)
1143                         parent->ci_result = sub->sub_io.ci_result;
1144         }
1145
1146         EXIT;
1147 }
1148
1149 static void lov_io_iter_fini(const struct lu_env *env,
1150                              const struct cl_io_slice *ios)
1151 {
1152         struct lov_io *lio = cl2lov_io(env, ios);
1153         int rc;
1154
1155         ENTRY;
1156
1157         if (lio->lis_trunc_stripe_index != NULL)
1158                 OBD_FREE_PTR_ARRAY(lio->lis_trunc_stripe_index,
1159                                    lio->lis_object->u.composite.lo_entry_count);
1160         lio->lis_trunc_stripe_index = NULL;
1161
1162         rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
1163         LASSERT(rc == 0);
1164         while (!list_empty(&lio->lis_active))
1165                 list_del_init(lio->lis_active.next);
1166         EXIT;
1167 }
1168
1169 static void lov_io_unlock(const struct lu_env *env,
1170                           const struct cl_io_slice *ios)
1171 {
1172         int rc;
1173
1174         ENTRY;
1175         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
1176         LASSERT(rc == 0);
1177         EXIT;
1178 }
1179
1180 static int lov_io_read_ahead(const struct lu_env *env,
1181                              const struct cl_io_slice *ios,
1182                              pgoff_t start, struct cl_read_ahead *ra)
1183 {
1184         struct lov_io           *lio = cl2lov_io(env, ios);
1185         struct lov_object       *loo = lio->lis_object;
1186         struct lov_layout_raid0 *r0;
1187         struct lov_io_sub       *sub;
1188         loff_t                   offset;
1189         loff_t                   suboff;
1190         pgoff_t                  ra_end;
1191         unsigned int             pps; /* pages per stripe */
1192         int                      stripe;
1193         int                      index;
1194         int                      rc;
1195         ENTRY;
1196
1197         offset = start << PAGE_SHIFT;
1198         index = lov_io_layout_at(lio, offset);
1199         if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index) ||
1200             lsm_entry_is_foreign(loo->lo_lsm, index))
1201                 RETURN(-ENODATA);
1202
1203         /* avoid readahead to expand to stale components */
1204         if (!lov_entry(loo, index)->lle_valid)
1205                 RETURN(-EIO);
1206
1207         stripe = lov_stripe_number(loo->lo_lsm, index, offset);
1208
1209         r0 = lov_r0(loo, index);
1210         if (unlikely(!r0->lo_sub[stripe]))
1211                 RETURN(-EIO);
1212
1213         sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
1214         if (IS_ERR(sub))
1215                 RETURN(PTR_ERR(sub));
1216
1217         lov_stripe_offset(loo->lo_lsm, index, offset, stripe, &suboff);
1218         rc = cl_io_read_ahead(sub->sub_env, &sub->sub_io,
1219                               suboff >> PAGE_SHIFT, ra);
1220
1221         CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
1222                PFID(lu_object_fid(lov2lu(loo))), ra->cra_end_idx,
1223                     r0->lo_nr, rc);
1224         if (rc != 0)
1225                 RETURN(rc);
1226
1227         /**
1228          * Adjust the stripe index by layout of comp. ra->cra_end is the
1229          * maximum page index covered by an underlying DLM lock.
1230          * This function converts cra_end from stripe level to file level, and
1231          * make sure it's not beyond stripe and component boundary.
1232          */
1233
1234         /* cra_end is stripe level, convert it into file level */
1235         ra_end = ra->cra_end_idx;
1236         if (ra_end != CL_PAGE_EOF)
1237                 ra->cra_end_idx = lov_stripe_pgoff(loo->lo_lsm, index,
1238                                                    ra_end, stripe);
1239
1240         /* boundary of current component */
1241         ra_end = lov_io_extent(lio, index)->e_end >> PAGE_SHIFT;
1242         if (ra_end != CL_PAGE_EOF && ra->cra_end_idx >= ra_end)
1243                 ra->cra_end_idx = ra_end - 1;
1244
1245         if (r0->lo_nr == 1) /* single stripe file */
1246                 RETURN(0);
1247
1248         pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
1249
1250         CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %d, "
1251                "stripe_size = %u, stripe no = %u, start index = %lu\n",
1252                PFID(lu_object_fid(lov2lu(loo))), ra->cra_end_idx, pps, index,
1253                lov_lse(loo, index)->lsme_stripe_size, stripe, start);
1254
1255         /* never exceed the end of the stripe */
1256         ra->cra_end_idx = min_t(pgoff_t, ra->cra_end_idx,
1257                                 start + pps - start % pps - 1);
1258         RETURN(0);
1259 }
1260
1261 int lov_io_lru_reserve(const struct lu_env *env,
1262                        const struct cl_io_slice *ios, loff_t pos, size_t bytes)
1263 {
1264         struct lov_io *lio = cl2lov_io(env, ios);
1265         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
1266         struct lov_io_sub *sub;
1267         struct lu_extent ext;
1268         int index;
1269         int rc = 0;
1270
1271         ENTRY;
1272
1273         ext.e_start = pos;
1274         ext.e_end = pos + bytes;
1275         lov_foreach_io_layout(index, lio, &ext) {
1276                 struct lov_layout_entry *le = lov_entry(lio->lis_object, index);
1277                 struct lov_layout_raid0 *r0 = &le->lle_raid0;
1278                 u64 start;
1279                 u64 end;
1280                 int stripe;
1281
1282                 if (!lsm_entry_inited(lsm, index))
1283                         continue;
1284
1285                 if (!le->lle_valid && !ios->cis_io->ci_designated_mirror) {
1286                         CERROR(DFID": I/O to invalid component: %d, mirror: %d\n",
1287                                PFID(lu_object_fid(lov2lu(lio->lis_object))),
1288                                index, lio->lis_mirror_index);
1289                         RETURN(-EIO);
1290                 }
1291
1292                 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
1293                         if (!lov_stripe_intersects(lsm, index, stripe,
1294                                                    &ext, &start, &end))
1295                                 continue;
1296
1297                         if (unlikely(!r0->lo_sub[stripe]))
1298                                 RETURN(-EIO);
1299
1300                         sub = lov_sub_get(env, lio,
1301                                           lov_comp_index(index, stripe));
1302                         if (IS_ERR(sub))
1303                                 return PTR_ERR(sub);
1304
1305                         rc = cl_io_lru_reserve(sub->sub_env, &sub->sub_io, start,
1306                                                end - start + 1);
1307                         if (rc != 0)
1308                                 RETURN(rc);
1309                 }
1310         }
1311
1312         RETURN(0);
1313 }
1314
1315 /**
1316  * lov implementation of cl_operations::cio_submit() method. It takes a list
1317  * of pages in \a queue, splits it into per-stripe sub-lists, invokes
1318  * cl_io_submit() on underlying devices to submit sub-lists, and then splices
1319  * everything back.
1320  *
1321  * Major complication of this function is a need to handle memory cleansing:
1322  * cl_io_submit() is called to write out pages as a part of VM memory
1323  * reclamation, and hence it may not fail due to memory shortages (system
1324  * dead-locks otherwise). To deal with this, some resources (sub-lists,
1325  * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
1326  * not-memory cleansing context), and in case of memory shortage, these
1327  * pre-allocated resources are used by lov_io_submit() under
1328  * lov_device::ld_mutex mutex.
1329  */
1330 static int lov_io_submit(const struct lu_env *env,
1331                          const struct cl_io_slice *ios,
1332                          enum cl_req_type crt, struct cl_2queue *queue)
1333 {
1334         struct cl_page_list     *qin = &queue->c2_qin;
1335         struct lov_io           *lio = cl2lov_io(env, ios);
1336         struct lov_io_sub       *sub;
1337         struct cl_page_list     *plist = &lov_env_info(env)->lti_plist;
1338         struct cl_page          *page = cl_page_list_first(qin);
1339         struct cl_page          *tmp;
1340         bool dio = false;
1341         int index;
1342         int rc = 0;
1343         ENTRY;
1344
1345         if (page->cp_type == CPT_TRANSIENT)
1346                 dio = true;
1347
1348         cl_page_list_init(plist);
1349         while (qin->pl_nr > 0) {
1350                 struct cl_2queue  *cl2q = &lov_env_info(env)->lti_cl2q;
1351
1352                 page = cl_page_list_first(qin);
1353                 if (lov_page_is_empty(page)) {
1354                         cl_page_list_move(&queue->c2_qout, qin, page);
1355
1356                         /*
1357                          * it could only be mirror read to get here therefore
1358                          * the pages will be transient. We don't care about
1359                          * the return code of cl_page_prep() at all.
1360                          */
1361                         (void) cl_page_prep(env, ios->cis_io, page, crt);
1362                         cl_page_completion(env, page, crt, 0);
1363                         continue;
1364                 }
1365
1366                 cl_2queue_init(cl2q);
1367                 cl_page_list_move(&cl2q->c2_qin, qin, page);
1368
1369                 index = page->cp_lov_index;
1370                 /* DIO is already split by stripe */
1371                 if (!dio) {
1372                         cl_page_list_for_each_safe(page, tmp, qin) {
1373                                 /* this page is not on this stripe */
1374                                 if (index != page->cp_lov_index)
1375                                         continue;
1376
1377                                 cl_page_list_move(&cl2q->c2_qin, qin, page);
1378                         }
1379                 } else {
1380                         cl_page_list_splice(qin, &cl2q->c2_qin);
1381                 }
1382
1383                 sub = lov_sub_get(env, lio, index);
1384                 if (!IS_ERR(sub)) {
1385                         rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
1386                                              crt, cl2q);
1387                 } else {
1388                         rc = PTR_ERR(sub);
1389                 }
1390
1391                 cl_page_list_splice(&cl2q->c2_qin, plist);
1392                 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
1393                 cl_2queue_fini(env, cl2q);
1394
1395                 if (rc != 0)
1396                         break;
1397         }
1398
1399         cl_page_list_splice(plist, qin);
1400         cl_page_list_fini(env, plist);
1401
1402         RETURN(rc);
1403 }
1404
1405 static int lov_io_commit_async(const struct lu_env *env,
1406                                const struct cl_io_slice *ios,
1407                                struct cl_page_list *queue, int from, int to,
1408                                cl_commit_cbt cb)
1409 {
1410         struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
1411         struct lov_io *lio = cl2lov_io(env, ios);
1412         struct lov_io_sub *sub;
1413         struct cl_page *page;
1414         int rc = 0;
1415         ENTRY;
1416
1417         if (lio->lis_nr_subios == 1) {
1418                 int idx = lio->lis_single_subio_index;
1419
1420                 LASSERT(!lov_page_is_empty(cl_page_list_first(queue)));
1421
1422                 sub = lov_sub_get(env, lio, idx);
1423                 LASSERT(!IS_ERR(sub));
1424                 LASSERT(sub == &lio->lis_single_subio);
1425                 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io, queue,
1426                                         from, to, cb);
1427                 RETURN(rc);
1428         }
1429
1430         cl_page_list_init(plist);
1431         while (queue->pl_nr > 0) {
1432                 int stripe_to = to;
1433                 int index;
1434
1435                 LASSERT(plist->pl_nr == 0);
1436                 page = cl_page_list_first(queue);
1437                 LASSERT(!lov_page_is_empty(page));
1438
1439                 cl_page_list_move(plist, queue, page);
1440
1441                 index = page->cp_lov_index;
1442                 while (queue->pl_nr > 0) {
1443                         page = cl_page_list_first(queue);
1444                         if (index != page->cp_lov_index)
1445                                 break;
1446
1447                         cl_page_list_move(plist, queue, page);
1448                 }
1449
1450                 if (queue->pl_nr > 0) /* still has more pages */
1451                         stripe_to = PAGE_SIZE;
1452
1453                 sub = lov_sub_get(env, lio, index);
1454                 if (!IS_ERR(sub)) {
1455                         rc = cl_io_commit_async(sub->sub_env, &sub->sub_io,
1456                                                 plist, from, stripe_to, cb);
1457                 } else {
1458                         rc = PTR_ERR(sub);
1459                         break;
1460                 }
1461
1462                 if (plist->pl_nr > 0) /* short write */
1463                         break;
1464
1465                 from = 0;
1466
1467                 if (lov_comp_entry(index) !=
1468                     lov_comp_entry(page->cp_lov_index))
1469                         cl_io_extent_release(sub->sub_env, &sub->sub_io);
1470         }
1471
1472         /* for error case, add the page back into the qin list */
1473         LASSERT(ergo(rc == 0, plist->pl_nr == 0));
1474         while (plist->pl_nr > 0) {
1475                 /* error occurred, add the uncommitted pages back into queue */
1476                 page = cl_page_list_last(plist);
1477                 cl_page_list_move_head(queue, plist, page);
1478         }
1479
1480         RETURN(rc);
1481 }
1482
1483 static int lov_io_fault_start(const struct lu_env *env,
1484                               const struct cl_io_slice *ios)
1485 {
1486         struct cl_fault_io *fio;
1487         struct lov_io      *lio;
1488         struct lov_io_sub  *sub;
1489         loff_t offset;
1490         int entry;
1491         int stripe;
1492
1493         ENTRY;
1494
1495         fio = &ios->cis_io->u.ci_fault;
1496         lio = cl2lov_io(env, ios);
1497
1498         /**
1499          * LU-14502: ft_page could be an existing cl_page associated with
1500          * the vmpage covering the fault index, and the page may still
1501          * refer to another mirror of an old IO.
1502          */
1503         if (lov_is_flr(lio->lis_object)) {
1504                 offset = fio->ft_index << PAGE_SHIFT;;
1505                 entry = lov_io_layout_at(lio, offset);
1506                 if (entry < 0) {
1507                         CERROR(DFID": page fault index %lu invalid component: "
1508                                "%d, mirror: %d\n",
1509                                PFID(lu_object_fid(&ios->cis_obj->co_lu)),
1510                                fio->ft_index, entry,
1511                                lio->lis_mirror_index);
1512                         RETURN(-EIO);
1513                 }
1514                 stripe = lov_stripe_number(lio->lis_object->lo_lsm,
1515                                            entry, offset);
1516
1517                 if (fio->ft_page->cp_lov_index !=
1518                     lov_comp_index(entry, stripe)) {
1519                         CDEBUG(D_INFO, DFID": page fault at index %lu, "
1520                                "at mirror %u comp entry %u stripe %u, "
1521                                "been used with comp entry %u stripe %u\n",
1522                                PFID(lu_object_fid(&ios->cis_obj->co_lu)),
1523                                fio->ft_index, lio->lis_mirror_index,
1524                                entry, stripe,
1525                                lov_comp_entry(fio->ft_page->cp_lov_index),
1526                                lov_comp_stripe(fio->ft_page->cp_lov_index));
1527
1528                         fio->ft_page->cp_lov_index =
1529                                         lov_comp_index(entry, stripe);
1530                 }
1531         }
1532
1533         sub = lov_sub_get(env, lio, fio->ft_page->cp_lov_index);
1534         sub->sub_io.u.ci_fault.ft_bytes = fio->ft_bytes;
1535
1536         RETURN(lov_io_start(env, ios));
1537 }
1538
1539 static int lov_io_setattr_start(const struct lu_env *env,
1540                                 const struct cl_io_slice *ios)
1541 {
1542         struct lov_io *lio = cl2lov_io(env, ios);
1543         struct cl_io *parent = ios->cis_io;
1544         struct lov_io_sub *sub;
1545         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
1546
1547         ENTRY;
1548
1549         if (cl_io_is_fallocate(parent)) {
1550                 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1551                         loff_t size = parent->u.ci_setattr.sa_attr.lvb_size;
1552                         int index = lov_comp_entry(sub->sub_subio_index);
1553                         int stripe = lov_comp_stripe(sub->sub_subio_index);
1554
1555                         size = lov_size_to_stripe(lsm, index, size, stripe);
1556                         sub->sub_io.u.ci_setattr.sa_attr.lvb_size = size;
1557                         sub->sub_io.u.ci_setattr.sa_avalid =
1558                                                 parent->u.ci_setattr.sa_avalid;
1559                 }
1560         }
1561
1562         RETURN(lov_io_start(env, ios));
1563 }
1564
1565 static void lov_io_fsync_end(const struct lu_env *env,
1566                              const struct cl_io_slice *ios)
1567 {
1568         struct lov_io *lio = cl2lov_io(env, ios);
1569         struct lov_io_sub *sub;
1570         unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
1571         ENTRY;
1572
1573         *written = 0;
1574         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1575                 struct cl_io *subio = &sub->sub_io;
1576
1577                 lov_io_end_wrapper(sub->sub_env, subio);
1578
1579                 if (subio->ci_result == 0)
1580                         *written += subio->u.ci_fsync.fi_nr_written;
1581         }
1582         RETURN_EXIT;
1583 }
1584
1585 static void lov_io_lseek_end(const struct lu_env *env,
1586                              const struct cl_io_slice *ios)
1587 {
1588         struct lov_io *lio = cl2lov_io(env, ios);
1589         struct cl_io *io = lio->lis_cl.cis_io;
1590         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
1591         struct lov_io_sub *sub;
1592         loff_t offset = -ENXIO;
1593         __u64 hole_off = 0;
1594         bool seek_hole = io->u.ci_lseek.ls_whence == SEEK_HOLE;
1595
1596         ENTRY;
1597
1598         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1599                 struct cl_io *subio = &sub->sub_io;
1600                 int index = lov_comp_entry(sub->sub_subio_index);
1601                 int stripe = lov_comp_stripe(sub->sub_subio_index);
1602                 loff_t sub_off, lov_off;
1603                 __u64 comp_end = lsm->lsm_entries[index]->lsme_extent.e_end;
1604
1605                 lov_io_end_wrapper(sub->sub_env, subio);
1606
1607                 if (io->ci_result == 0)
1608                         io->ci_result = sub->sub_io.ci_result;
1609
1610                 if (io->ci_result)
1611                         continue;
1612
1613                 CDEBUG(D_INFO, DFID": entry %x stripe %u: SEEK_%s from %lld\n",
1614                        PFID(lu_object_fid(lov2lu(lio->lis_object))),
1615                        index, stripe, seek_hole ? "HOLE" : "DATA",
1616                        subio->u.ci_lseek.ls_start);
1617
1618                 /* first subio with positive result is what we need */
1619                 sub_off = subio->u.ci_lseek.ls_result;
1620                 /* Expected error, offset is out of stripe file size */
1621                 if (sub_off == -ENXIO)
1622                         continue;
1623                 /* Any other errors are not expected with ci_result == 0 */
1624                 if (sub_off < 0) {
1625                         CDEBUG(D_INFO, "unexpected error: rc = %lld\n",
1626                                sub_off);
1627                         io->ci_result = sub_off;
1628                         continue;
1629                 }
1630                 lov_off = lov_stripe_size(lsm, index, sub_off + 1, stripe) - 1;
1631                 if (lov_off < 0) {
1632                         /* the only way to get negatove lov_off here is too big
1633                          * result. Return -EOVERFLOW then.
1634                          */
1635                         io->ci_result = -EOVERFLOW;
1636                         CDEBUG(D_INFO, "offset %llu is too big: rc = %d\n",
1637                                (u64)lov_off, io->ci_result);
1638                         continue;
1639                 }
1640                 if (lov_off < io->u.ci_lseek.ls_start) {
1641                         io->ci_result = -EINVAL;
1642                         CDEBUG(D_INFO, "offset %lld < start %lld: rc = %d\n",
1643                                sub_off, io->u.ci_lseek.ls_start, io->ci_result);
1644                         continue;
1645                 }
1646                 /* resulting offset can be out of component range if stripe
1647                  * object is full and its file size was returned as virtual
1648                  * hole start. Skip this result, the next component will give
1649                  * us correct lseek result but keep possible hole offset in
1650                  * case there is no more components ahead
1651                  */
1652                 if (lov_off >= comp_end) {
1653                         /* must be SEEK_HOLE case */
1654                         if (likely(seek_hole)) {
1655                                 /* save comp end as potential hole offset */
1656                                 hole_off = max_t(__u64, comp_end, hole_off);
1657                         } else {
1658                                 io->ci_result = -EINVAL;
1659                                 CDEBUG(D_INFO,
1660                                        "off %lld >= comp_end %llu: rc = %d\n",
1661                                        lov_off, comp_end, io->ci_result);
1662                         }
1663                         continue;
1664                 }
1665
1666                 CDEBUG(D_INFO, "SEEK_%s: %lld->%lld/%lld: rc = %d\n",
1667                        seek_hole ? "HOLE" : "DATA",
1668                        subio->u.ci_lseek.ls_start, sub_off, lov_off,
1669                        sub->sub_io.ci_result);
1670                 offset = min_t(__u64, offset, lov_off);
1671         }
1672         /* no result but some component returns hole as component end */
1673         if (seek_hole && offset == -ENXIO && hole_off > 0)
1674                 offset = hole_off;
1675
1676         io->u.ci_lseek.ls_result = offset;
1677         RETURN_EXIT;
1678 }
1679
1680 static const struct cl_io_operations lov_io_ops = {
1681         .op = {
1682                 [CIT_READ] = {
1683                         .cio_fini      = lov_io_fini,
1684                         .cio_iter_init = lov_io_rw_iter_init,
1685                         .cio_iter_fini = lov_io_iter_fini,
1686                         .cio_lock      = lov_io_lock,
1687                         .cio_unlock    = lov_io_unlock,
1688                         .cio_start     = lov_io_start,
1689                         .cio_end       = lov_io_end
1690                 },
1691                 [CIT_WRITE] = {
1692                         .cio_fini      = lov_io_fini,
1693                         .cio_iter_init = lov_io_rw_iter_init,
1694                         .cio_iter_fini = lov_io_iter_fini,
1695                         .cio_lock      = lov_io_lock,
1696                         .cio_unlock    = lov_io_unlock,
1697                         .cio_start     = lov_io_start,
1698                         .cio_end       = lov_io_end
1699                 },
1700                 [CIT_SETATTR] = {
1701                         .cio_fini      = lov_io_fini,
1702                         .cio_iter_init = lov_io_setattr_iter_init,
1703                         .cio_iter_fini = lov_io_iter_fini,
1704                         .cio_lock      = lov_io_lock,
1705                         .cio_unlock    = lov_io_unlock,
1706                         .cio_start     = lov_io_setattr_start,
1707                         .cio_end       = lov_io_end
1708                 },
1709                 [CIT_DATA_VERSION] = {
1710                         .cio_fini       = lov_io_fini,
1711                         .cio_iter_init  = lov_io_iter_init,
1712                         .cio_iter_fini  = lov_io_iter_fini,
1713                         .cio_lock       = lov_io_lock,
1714                         .cio_unlock     = lov_io_unlock,
1715                         .cio_start      = lov_io_start,
1716                         .cio_end        = lov_io_data_version_end,
1717                 },
1718                 [CIT_FAULT] = {
1719                         .cio_fini      = lov_io_fini,
1720                         .cio_iter_init = lov_io_iter_init,
1721                         .cio_iter_fini = lov_io_iter_fini,
1722                         .cio_lock      = lov_io_lock,
1723                         .cio_unlock    = lov_io_unlock,
1724                         .cio_start     = lov_io_fault_start,
1725                         .cio_end       = lov_io_end
1726                 },
1727                 [CIT_FSYNC] = {
1728                         .cio_fini      = lov_io_fini,
1729                         .cio_iter_init = lov_io_iter_init,
1730                         .cio_iter_fini = lov_io_iter_fini,
1731                         .cio_lock      = lov_io_lock,
1732                         .cio_unlock    = lov_io_unlock,
1733                         .cio_start     = lov_io_start,
1734                         .cio_end       = lov_io_fsync_end
1735                 },
1736                 [CIT_LADVISE] = {
1737                         .cio_fini      = lov_io_fini,
1738                         .cio_iter_init = lov_io_iter_init,
1739                         .cio_iter_fini = lov_io_iter_fini,
1740                         .cio_lock      = lov_io_lock,
1741                         .cio_unlock    = lov_io_unlock,
1742                         .cio_start     = lov_io_start,
1743                         .cio_end       = lov_io_end
1744                 },
1745                 [CIT_LSEEK] = {
1746                         .cio_fini      = lov_io_fini,
1747                         .cio_iter_init = lov_io_iter_init,
1748                         .cio_iter_fini = lov_io_iter_fini,
1749                         .cio_lock      = lov_io_lock,
1750                         .cio_unlock    = lov_io_unlock,
1751                         .cio_start     = lov_io_start,
1752                         .cio_end       = lov_io_lseek_end
1753                 },
1754                 [CIT_GLIMPSE] = {
1755                         .cio_fini      = lov_io_fini,
1756                 },
1757                 [CIT_MISC] = {
1758                         .cio_fini      = lov_io_fini
1759                 }
1760         },
1761         .cio_read_ahead                = lov_io_read_ahead,
1762         .cio_lru_reserve               = lov_io_lru_reserve,
1763         .cio_submit                    = lov_io_submit,
1764         .cio_commit_async              = lov_io_commit_async,
1765 };
1766
1767 /**
1768  * Empty lov io operations.
1769  */
1770 static void lov_empty_io_fini(const struct lu_env *env,
1771                               const struct cl_io_slice *ios)
1772 {
1773         struct lov_object *lov = cl2lov(ios->cis_obj);
1774         ENTRY;
1775
1776         if (atomic_dec_and_test(&lov->lo_active_ios))
1777                 wake_up(&lov->lo_waitq);
1778         EXIT;
1779 }
1780
1781 static int lov_empty_io_submit(const struct lu_env *env,
1782                                const struct cl_io_slice *ios,
1783                                enum cl_req_type crt, struct cl_2queue *queue)
1784 {
1785         return -EBADF;
1786 }
1787
1788 static void lov_empty_impossible(const struct lu_env *env,
1789                                  struct cl_io_slice *ios)
1790 {
1791         LBUG();
1792 }
1793
1794 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
1795
1796 /**
1797  * An io operation vector for files without stripes.
1798  */
1799 static const struct cl_io_operations lov_empty_io_ops = {
1800         .op = {
1801                 [CIT_READ] = {
1802                         .cio_fini       = lov_empty_io_fini,
1803 #if 0
1804                         .cio_iter_init  = LOV_EMPTY_IMPOSSIBLE,
1805                         .cio_lock       = LOV_EMPTY_IMPOSSIBLE,
1806                         .cio_start      = LOV_EMPTY_IMPOSSIBLE,
1807                         .cio_end        = LOV_EMPTY_IMPOSSIBLE
1808 #endif
1809                 },
1810                 [CIT_WRITE] = {
1811                         .cio_fini      = lov_empty_io_fini,
1812                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1813                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
1814                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
1815                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
1816                 },
1817                 [CIT_SETATTR] = {
1818                         .cio_fini      = lov_empty_io_fini,
1819                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1820                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
1821                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
1822                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
1823                 },
1824                 [CIT_FAULT] = {
1825                         .cio_fini      = lov_empty_io_fini,
1826                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1827                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
1828                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
1829                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
1830                 },
1831                 [CIT_FSYNC] = {
1832                         .cio_fini      = lov_empty_io_fini
1833                 },
1834                 [CIT_LADVISE] = {
1835                         .cio_fini   = lov_empty_io_fini
1836                 },
1837                 [CIT_GLIMPSE] = {
1838                         .cio_fini      = lov_empty_io_fini
1839                 },
1840                 [CIT_MISC] = {
1841                         .cio_fini      = lov_empty_io_fini
1842                 }
1843         },
1844         .cio_submit                    = lov_empty_io_submit,
1845         .cio_commit_async              = LOV_EMPTY_IMPOSSIBLE
1846 };
1847
1848 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
1849                           struct cl_io *io)
1850 {
1851         struct lov_io *lio = lov_env_io(env);
1852         struct lov_object *lov = cl2lov(obj);
1853         int result;
1854
1855         ENTRY;
1856
1857         INIT_LIST_HEAD(&lio->lis_active);
1858         result = lov_io_slice_init(lio, lov, io);
1859         if (result)
1860                 GOTO(out, result);
1861
1862         result = lov_io_subio_init(env, lio, io);
1863         if (!result) {
1864                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
1865                 atomic_inc(&lov->lo_active_ios);
1866         }
1867         EXIT;
1868 out:
1869         io->ci_result = result < 0 ? result : 0;
1870         return result;
1871 }
1872
1873 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
1874                       struct cl_io *io)
1875 {
1876         struct lov_object *lov = cl2lov(obj);
1877         struct lov_io *lio = lov_env_io(env);
1878         int result;
1879         ENTRY;
1880
1881         lio->lis_object = lov;
1882         switch (io->ci_type) {
1883         default:
1884                 LBUG();
1885         case CIT_MISC:
1886         case CIT_GLIMPSE:
1887         case CIT_READ:
1888                 result = 0;
1889                 break;
1890         case CIT_FSYNC:
1891         case CIT_LADVISE:
1892         case CIT_LSEEK:
1893         case CIT_SETATTR:
1894         case CIT_DATA_VERSION:
1895                 result = +1;
1896                 break;
1897         case CIT_WRITE:
1898                 result = -EBADF;
1899                 break;
1900         case CIT_FAULT:
1901                 result = -EFAULT;
1902                 CERROR("Page fault on a file without stripes: "DFID"\n",
1903                        PFID(lu_object_fid(&obj->co_lu)));
1904                 break;
1905         }
1906         if (result == 0) {
1907                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1908                 atomic_inc(&lov->lo_active_ios);
1909         }
1910
1911         io->ci_result = result < 0 ? result : 0;
1912         RETURN(result);
1913 }
1914
1915 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
1916                          struct cl_io *io)
1917 {
1918         struct lov_object *lov = cl2lov(obj);
1919         struct lov_io *lio = lov_env_io(env);
1920         int result;
1921         ENTRY;
1922
1923         LASSERT(lov->lo_lsm != NULL);
1924         lio->lis_object = lov;
1925
1926         switch (io->ci_type) {
1927         default:
1928                 LASSERTF(0, "invalid type %d\n", io->ci_type);
1929                 result = -EOPNOTSUPP;
1930                 break;
1931         case CIT_GLIMPSE:
1932         case CIT_MISC:
1933         case CIT_FSYNC:
1934         case CIT_LADVISE:
1935         case CIT_DATA_VERSION:
1936                 result = 1;
1937                 break;
1938         case CIT_SETATTR:
1939                 /*
1940                  * the truncate to 0 is managed by MDT:
1941                  * - in open, for open O_TRUNC
1942                  * - in setattr, for truncate
1943                  */
1944                 /*
1945                  * the truncate is for size > 0 so triggers a restore,
1946                  * also trigger a restore for prealloc/punch
1947                  */
1948                 if (cl_io_is_trunc(io) || cl_io_is_fallocate(io)) {
1949                         io->ci_restore_needed = 1;
1950                         result = -ENODATA;
1951                 } else
1952                         result = 1;
1953                 break;
1954         case CIT_READ:
1955         case CIT_WRITE:
1956         case CIT_FAULT:
1957         case CIT_LSEEK:
1958                 io->ci_restore_needed = 1;
1959                 result = -ENODATA;
1960                 break;
1961         }
1962
1963         if (result == 0) {
1964                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1965                 atomic_inc(&lov->lo_active_ios);
1966         }
1967
1968         io->ci_result = result < 0 ? result : 0;
1969         RETURN(result);
1970 }
1971
1972 /**
1973  * Return the index in composite:lo_entries by the file offset
1974  */
1975 int lov_io_layout_at(struct lov_io *lio, __u64 offset)
1976 {
1977         struct lov_object *lov = lio->lis_object;
1978         struct lov_layout_composite *comp = &lov->u.composite;
1979         int start_index = 0;
1980         int end_index = comp->lo_entry_count - 1;
1981         int i;
1982
1983         LASSERT(lov->lo_type == LLT_COMP);
1984
1985         /* This is actual file offset so nothing can cover eof. */
1986         if (offset == LUSTRE_EOF)
1987                 return -1;
1988
1989         if (lov_is_flr(lov)) {
1990                 struct lov_mirror_entry *lre;
1991
1992                 LASSERT(lio->lis_mirror_index >= 0);
1993
1994                 lre = &comp->lo_mirrors[lio->lis_mirror_index];
1995                 start_index = lre->lre_start;
1996                 end_index = lre->lre_end;
1997         }
1998
1999         for (i = start_index; i <= end_index; i++) {
2000                 struct lov_layout_entry *lle = lov_entry(lov, i);
2001
2002                 LASSERT(!lsme_is_foreign(lle->lle_lsme));
2003
2004                 if ((offset >= lle->lle_extent->e_start &&
2005                      offset < lle->lle_extent->e_end) ||
2006                     (offset == OBD_OBJECT_EOF &&
2007                      lle->lle_extent->e_end == OBD_OBJECT_EOF))
2008                         return i;
2009         }
2010
2011         return -1;
2012 }
2013
2014 /** @} lov */