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