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