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