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