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