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