Whamcloud - gitweb
8cdfbf419ad6bbfe2cd58cb8dee65836e0943bd8
[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 int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
93                            struct lov_io_sub *sub)
94 {
95         struct lov_object *lov = lio->lis_object;
96         struct cl_io *sub_io;
97         struct cl_object *sub_obj;
98         struct cl_io *io = lio->lis_cl.cis_io;
99         int index = lov_comp_entry(sub->sub_subio_index);
100         int stripe = lov_comp_stripe(sub->sub_subio_index);
101         int result = 0;
102         LASSERT(sub->sub_env == NULL);
103         ENTRY;
104
105         if (unlikely(!lov_r0(lov, index)->lo_sub ||
106                      !lov_r0(lov, index)->lo_sub[stripe]))
107                 RETURN(-EIO);
108
109         /* obtain new environment */
110         sub->sub_env = cl_env_get(&sub->sub_refcheck);
111         if (IS_ERR(sub->sub_env))
112                 result = PTR_ERR(sub->sub_env);
113
114         sub_obj = lovsub2cl(lov_r0(lov, index)->lo_sub[stripe]);
115         sub_io  = &sub->sub_io;
116
117         sub_io->ci_obj    = sub_obj;
118         sub_io->ci_result = 0;
119
120         sub_io->ci_parent  = io;
121         sub_io->ci_lockreq = io->ci_lockreq;
122         sub_io->ci_type    = io->ci_type;
123         sub_io->ci_no_srvlock = io->ci_no_srvlock;
124         sub_io->ci_noatime = io->ci_noatime;
125         sub_io->ci_pio = io->ci_pio;
126         sub_io->ci_lock_no_expand = io->ci_lock_no_expand;
127
128         result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
129
130         if (result < 0)
131                 lov_io_sub_fini(env, lio, sub);
132
133         RETURN(result);
134 }
135
136 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
137                                struct lov_io *lio, int index)
138 {
139         struct lov_io_sub *sub;
140         int rc = 0;
141
142         ENTRY;
143
144         list_for_each_entry(sub, &lio->lis_subios, sub_list) {
145                 if (sub->sub_subio_index == index) {
146                         rc = 1;
147                         break;
148                 }
149         }
150
151         if (rc == 0) {
152                 sub = lov_sub_alloc(lio, index);
153                 if (sub == NULL)
154                         GOTO(out, rc = -ENOMEM);
155
156                 rc = lov_io_sub_init(env, lio, sub);
157                 if (rc < 0) {
158                         lov_sub_free(lio, sub);
159                         GOTO(out, rc);
160                 }
161
162                 list_add_tail(&sub->sub_list, &lio->lis_subios);
163                 lio->lis_nr_subios++;
164         }
165 out:
166         if (rc < 0)
167                 sub = ERR_PTR(rc);
168         RETURN(sub);
169 }
170
171 /*****************************************************************************
172  *
173  * Lov io operations.
174  *
175  */
176
177 int lov_page_index(const struct cl_page *page)
178 {
179         const struct cl_page_slice *slice;
180         ENTRY;
181
182         slice = cl_page_at(page, &lov_device_type);
183         LASSERT(slice != NULL);
184         LASSERT(slice->cpl_obj != NULL);
185
186         RETURN(cl2lov_page(slice)->lps_index);
187 }
188
189 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
190                              struct cl_io *io)
191 {
192         ENTRY;
193
194         LASSERT(lio->lis_object != NULL);
195
196         INIT_LIST_HEAD(&lio->lis_subios);
197         lio->lis_single_subio_index = -1;
198         lio->lis_nr_subios = 0;
199
200         RETURN(0);
201 }
202
203 static int lov_io_slice_init(struct lov_io *lio,
204                              struct lov_object *obj, struct cl_io *io)
205 {
206         ENTRY;
207
208         io->ci_result = 0;
209         lio->lis_object = obj;
210
211         LASSERT(obj->lo_lsm != NULL);
212
213         switch (io->ci_type) {
214         case CIT_READ:
215         case CIT_WRITE:
216                 lio->lis_pos = io->u.ci_rw.rw_range.cir_pos;
217                 lio->lis_endpos = lio->lis_pos + io->u.ci_rw.rw_range.cir_count;
218                 lio->lis_io_endpos = lio->lis_endpos;
219                 if (cl_io_is_append(io)) {
220                         LASSERT(io->ci_type == CIT_WRITE);
221
222                         /* If there is LOV EA hole, then we may cannot locate
223                          * the current file-tail exactly. */
224                         if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
225                                      LOV_PATTERN_F_HOLE))
226                                 RETURN(-EIO);
227
228                         lio->lis_pos = 0;
229                         lio->lis_endpos = OBD_OBJECT_EOF;
230                 }
231                 break;
232
233         case CIT_SETATTR:
234                 if (cl_io_is_trunc(io))
235                         lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
236                 else
237                         lio->lis_pos = 0;
238                 lio->lis_endpos = OBD_OBJECT_EOF;
239                 break;
240
241         case CIT_DATA_VERSION:
242                 lio->lis_pos = 0;
243                 lio->lis_endpos = OBD_OBJECT_EOF;
244                 break;
245
246         case CIT_FAULT: {
247                 pgoff_t index = io->u.ci_fault.ft_index;
248                 lio->lis_pos = cl_offset(io->ci_obj, index);
249                 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
250                 break;
251         }
252
253         case CIT_FSYNC: {
254                 lio->lis_pos = io->u.ci_fsync.fi_start;
255                 lio->lis_endpos = io->u.ci_fsync.fi_end;
256                 break;
257         }
258
259         case CIT_LADVISE: {
260                 lio->lis_pos = io->u.ci_ladvise.li_start;
261                 lio->lis_endpos = io->u.ci_ladvise.li_end;
262                 break;
263         }
264
265         case CIT_GLIMPSE:
266         case CIT_MISC:
267                 lio->lis_pos = 0;
268                 lio->lis_endpos = OBD_OBJECT_EOF;
269                 break;
270
271         default:
272                 LBUG();
273         }
274
275         RETURN(0);
276 }
277
278 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
279 {
280         struct lov_io *lio = cl2lov_io(env, ios);
281         struct lov_object *lov = cl2lov(ios->cis_obj);
282
283         ENTRY;
284
285         LASSERT(list_empty(&lio->lis_active));
286
287         while (!list_empty(&lio->lis_subios)) {
288                 struct lov_io_sub *sub = list_entry(lio->lis_subios.next,
289                                                     struct lov_io_sub,
290                                                     sub_list);
291
292                 list_del_init(&sub->sub_list);
293                 lio->lis_nr_subios--;
294
295                 lov_io_sub_fini(env, lio, sub);
296                 lov_sub_free(lio, sub);
297         }
298         LASSERT(lio->lis_nr_subios == 0);
299
300         LASSERT(atomic_read(&lov->lo_active_ios) > 0);
301         if (atomic_dec_and_test(&lov->lo_active_ios))
302                 wake_up_all(&lov->lo_waitq);
303         EXIT;
304 }
305
306 static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
307                                loff_t start, loff_t end)
308 {
309         struct cl_io *io = &sub->sub_io;
310         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
311         struct cl_io *parent = lio->lis_cl.cis_io;
312         int index = lov_comp_entry(sub->sub_subio_index);
313         int stripe = lov_comp_stripe(sub->sub_subio_index);
314
315         io->ci_pio = parent->ci_pio;
316         switch (io->ci_type) {
317         case CIT_SETATTR: {
318                 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
319                 io->u.ci_setattr.sa_attr_flags =
320                         parent->u.ci_setattr.sa_attr_flags;
321                 io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
322                 io->u.ci_setattr.sa_stripe_index = stripe;
323                 io->u.ci_setattr.sa_parent_fid =
324                                         parent->u.ci_setattr.sa_parent_fid;
325                 if (cl_io_is_trunc(io)) {
326                         loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
327
328                         new_size = lov_size_to_stripe(lsm, index, new_size,
329                                                       stripe);
330                         io->u.ci_setattr.sa_attr.lvb_size = new_size;
331                 }
332                 lov_lsm2layout(lsm, lsm->lsm_entries[index],
333                                &io->u.ci_setattr.sa_layout);
334                 break;
335         }
336         case CIT_DATA_VERSION: {
337                 io->u.ci_data_version.dv_data_version = 0;
338                 io->u.ci_data_version.dv_flags =
339                         parent->u.ci_data_version.dv_flags;
340                 break;
341         }
342         case CIT_FAULT: {
343                 struct cl_object *obj = parent->ci_obj;
344                 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
345
346                 io->u.ci_fault = parent->u.ci_fault;
347                 off = lov_size_to_stripe(lsm, index, off, stripe);
348                 io->u.ci_fault.ft_index = cl_index(obj, off);
349                 break;
350         }
351         case CIT_FSYNC: {
352                 io->u.ci_fsync.fi_start = start;
353                 io->u.ci_fsync.fi_end = end;
354                 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
355                 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
356                 break;
357         }
358         case CIT_READ:
359         case CIT_WRITE: {
360                 io->u.ci_rw.rw_ptask = parent->u.ci_rw.rw_ptask;
361                 io->u.ci_rw.rw_iter = parent->u.ci_rw.rw_iter;
362                 io->u.ci_rw.rw_iocb = parent->u.ci_rw.rw_iocb;
363                 io->u.ci_rw.rw_file = parent->u.ci_rw.rw_file;
364                 io->u.ci_rw.rw_sync = parent->u.ci_rw.rw_sync;
365                 if (cl_io_is_append(parent)) {
366                         io->u.ci_rw.rw_append = 1;
367                 } else {
368                         io->u.ci_rw.rw_range.cir_pos = start;
369                         io->u.ci_rw.rw_range.cir_count = end - start;
370                 }
371                 break;
372         }
373         case CIT_LADVISE: {
374                 io->u.ci_ladvise.li_start = start;
375                 io->u.ci_ladvise.li_end = end;
376                 io->u.ci_ladvise.li_fid = parent->u.ci_ladvise.li_fid;
377                 io->u.ci_ladvise.li_advice = parent->u.ci_ladvise.li_advice;
378                 io->u.ci_ladvise.li_flags = parent->u.ci_ladvise.li_flags;
379                 break;
380         }
381         case CIT_GLIMPSE:
382         case CIT_MISC:
383         default:
384                 break;
385         }
386 }
387
388 static loff_t lov_offset_mod(loff_t val, int delta)
389 {
390         if (val != OBD_OBJECT_EOF)
391                 val += delta;
392         return val;
393 }
394
395 static int lov_io_iter_init(const struct lu_env *env,
396                             const struct cl_io_slice *ios)
397 {
398         struct cl_io         *io = ios->cis_io;
399         struct lov_io        *lio = cl2lov_io(env, ios);
400         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
401         struct lov_io_sub    *sub;
402         struct lov_layout_entry *le;
403         struct lu_extent ext;
404         int index;
405         int rc = 0;
406
407         ENTRY;
408
409         ext.e_start = lio->lis_pos;
410         ext.e_end = lio->lis_endpos;
411
412         index = 0;
413         lov_foreach_layout_entry(lio->lis_object, le) {
414                 struct lov_layout_raid0 *r0 = &le->lle_raid0;
415                 u64 start;
416                 u64 end;
417                 int stripe;
418
419                 index++;
420                 if (!lu_extent_is_overlapped(&ext, &le->lle_extent))
421                         continue;
422
423                 CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
424                        index - 1, lsm->lsm_entries[index - 1]->lsme_flags);
425                 if (!lsm_entry_inited(lsm, index - 1)) {
426                         /* truncate IO will trigger write intent as well, and
427                          * it's handled in lov_io_setattr_iter_init() */
428                         if (io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io)) {
429                                 io->ci_need_write_intent = 1;
430                                 /* execute it in main thread */
431                                 io->ci_pio = 0;
432                                 rc = -ENODATA;
433                                 break;
434                         }
435
436                         /* Read from uninitialized components should return
437                          * zero filled pages. */
438                         continue;
439                 }
440
441                 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
442                         if (!lov_stripe_intersects(lsm, index - 1, stripe,
443                                                    &ext, &start, &end))
444                                 continue;
445
446                         if (unlikely(r0->lo_sub[stripe] == NULL)) {
447                                 if (ios->cis_io->ci_type == CIT_READ ||
448                                     ios->cis_io->ci_type == CIT_WRITE ||
449                                     ios->cis_io->ci_type == CIT_FAULT)
450                                         RETURN(-EIO);
451
452                                 continue;
453                         }
454
455                         end = lov_offset_mod(end, 1);
456                         sub = lov_sub_get(env, lio,
457                                           lov_comp_index(index - 1, stripe));
458                         if (IS_ERR(sub)) {
459                                 rc = PTR_ERR(sub);
460                                 break;
461                         }
462
463                         lov_io_sub_inherit(sub, lio, start, end);
464                         rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
465                         if (rc != 0)
466                                 cl_io_iter_fini(sub->sub_env, &sub->sub_io);
467                         if (rc != 0)
468                                 break;
469
470                         CDEBUG(D_VFSTRACE,
471                                 "shrink stripe: {%d, %d} range: [%llu, %llu)\n",
472                                 index, stripe, start, end);
473
474                         list_add_tail(&sub->sub_linkage, &lio->lis_active);
475                 }
476                 if (rc != 0)
477                         break;
478         }
479         RETURN(rc);
480 }
481
482 static int lov_io_rw_iter_init(const struct lu_env *env,
483                                const struct cl_io_slice *ios)
484 {
485         struct cl_io *io = ios->cis_io;
486         struct lov_io *lio = cl2lov_io(env, ios);
487         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
488         struct lov_stripe_md_entry *lse;
489         struct cl_io_range *range = &io->u.ci_rw.rw_range;
490         loff_t start = range->cir_pos;
491         loff_t next;
492         int index;
493
494         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
495         ENTRY;
496
497         if (cl_io_is_append(io))
498                 RETURN(lov_io_iter_init(env, ios));
499
500         index = lov_lsm_entry(lsm, range->cir_pos);
501         if (index < 0) { /* non-existing layout component */
502                 if (io->ci_type == CIT_READ) {
503                         /* TODO: it needs to detect the next component and
504                          * then set the next pos */
505                         io->ci_continue = 0;
506                         /* execute it in main thread */
507                         io->ci_pio = 0;
508
509                         RETURN(lov_io_iter_init(env, ios));
510                 }
511
512                 RETURN(-ENODATA);
513         }
514
515         lse = lov_lse(lio->lis_object, index);
516
517         next = MAX_LFS_FILESIZE;
518         if (lse->lsme_stripe_count > 1) {
519                 unsigned long ssize = lse->lsme_stripe_size;
520
521                 lov_do_div64(start, ssize);
522                 next = (start + 1) * ssize;
523                 if (next <= start * ssize)
524                         next = MAX_LFS_FILESIZE;
525         }
526
527         LASSERTF(range->cir_pos >= lse->lsme_extent.e_start,
528                  "pos %lld, [%lld, %lld)\n", range->cir_pos,
529                  lse->lsme_extent.e_start, lse->lsme_extent.e_end);
530         next = min_t(__u64, next, lse->lsme_extent.e_end);
531         next = min_t(loff_t, next, lio->lis_io_endpos);
532
533         io->ci_continue  = next < lio->lis_io_endpos;
534         range->cir_count = next - range->cir_pos;
535         lio->lis_pos     = range->cir_pos;
536         lio->lis_endpos  = range->cir_pos + range->cir_count;
537         CDEBUG(D_VFSTRACE,
538                "stripe: {%d, %llu} range: [%llu, %llu) end: %llu, count: %zd\n",
539                index, start, lio->lis_pos, lio->lis_endpos,
540                lio->lis_io_endpos, range->cir_count);
541
542         if (!io->ci_continue) {
543                 /* the last piece of IO, execute it in main thread */
544                 io->ci_pio = 0;
545         }
546
547         if (io->ci_pio) {
548                 /* it only splits IO here for parallel IO,
549                  * there will be no actual IO going to occur,
550                  * so it doesn't need to invoke lov_io_iter_init()
551                  * to initialize sub IOs. */
552                 if (!lsm_entry_inited(lsm, index)) {
553                         io->ci_need_write_intent = 1;
554                         RETURN(-ENODATA);
555                 }
556                 RETURN(0);
557         }
558
559         /*
560          * XXX The following call should be optimized: we know, that
561          * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
562          */
563         RETURN(lov_io_iter_init(env, ios));
564 }
565
566 static int lov_io_setattr_iter_init(const struct lu_env *env,
567                                     const struct cl_io_slice *ios)
568 {
569         struct lov_io *lio = cl2lov_io(env, ios);
570         struct cl_io *io = ios->cis_io;
571         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
572         int index;
573         ENTRY;
574
575         if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
576                 index = lov_lsm_entry(lsm, lio->lis_pos - 1);
577                 /* no entry found for such offset */
578                 if (index < 0) {
579                         RETURN(io->ci_result = -ENODATA);
580                 } else if (!lsm_entry_inited(lsm, index)) {
581                         io->ci_need_write_intent = 1;
582                         RETURN(io->ci_result = -ENODATA);
583                 }
584         }
585
586         RETURN(lov_io_iter_init(env, ios));
587 }
588
589 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
590                        int (*iofunc)(const struct lu_env *, struct cl_io *))
591 {
592         struct cl_io *parent = lio->lis_cl.cis_io;
593         struct lov_io_sub *sub;
594         int rc = 0;
595
596         ENTRY;
597         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
598                 rc = iofunc(sub->sub_env, &sub->sub_io);
599                 if (rc)
600                         break;
601
602                 if (parent->ci_result == 0)
603                         parent->ci_result = sub->sub_io.ci_result;
604         }
605         RETURN(rc);
606 }
607
608 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
609 {
610         ENTRY;
611         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
612 }
613
614 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
615 {
616         ENTRY;
617         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
618 }
619
620 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
621 {
622         ENTRY;
623         /*
624          * It's possible that lov_io_start() wasn't called against this
625          * sub-io, either because previous sub-io failed, or upper layer
626          * completed IO.
627          */
628         if (io->ci_state == CIS_IO_GOING)
629                 cl_io_end(env, io);
630         else
631                 io->ci_state = CIS_IO_FINISHED;
632         RETURN(0);
633 }
634
635 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
636 {
637         cl_io_iter_fini(env, io);
638         RETURN(0);
639 }
640
641 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
642 {
643         cl_io_unlock(env, io);
644         RETURN(0);
645 }
646
647 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
648 {
649         int rc;
650
651         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
652         LASSERT(rc == 0);
653 }
654
655 static void
656 lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
657 {
658         struct lov_io *lio = cl2lov_io(env, ios);
659         struct cl_io *parent = lio->lis_cl.cis_io;
660         struct lov_io_sub *sub;
661
662         ENTRY;
663         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
664                 lov_io_end_wrapper(env, &sub->sub_io);
665
666                 parent->u.ci_data_version.dv_data_version +=
667                         sub->sub_io.u.ci_data_version.dv_data_version;
668
669                 if (parent->ci_result == 0)
670                         parent->ci_result = sub->sub_io.ci_result;
671         }
672
673         EXIT;
674 }
675
676 static void lov_io_iter_fini(const struct lu_env *env,
677                              const struct cl_io_slice *ios)
678 {
679         struct lov_io *lio = cl2lov_io(env, ios);
680         int rc;
681
682         ENTRY;
683         rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
684         LASSERT(rc == 0);
685         while (!list_empty(&lio->lis_active))
686                 list_del_init(lio->lis_active.next);
687         EXIT;
688 }
689
690 static void lov_io_unlock(const struct lu_env *env,
691                           const struct cl_io_slice *ios)
692 {
693         int rc;
694
695         ENTRY;
696         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
697         LASSERT(rc == 0);
698         EXIT;
699 }
700
701 static int lov_io_read_ahead(const struct lu_env *env,
702                              const struct cl_io_slice *ios,
703                              pgoff_t start, struct cl_read_ahead *ra)
704 {
705         struct lov_io           *lio = cl2lov_io(env, ios);
706         struct lov_object       *loo = lio->lis_object;
707         struct cl_object        *obj = lov2cl(loo);
708         struct lov_layout_raid0 *r0;
709         struct lov_io_sub       *sub;
710         loff_t                   offset;
711         loff_t                   suboff;
712         pgoff_t                  ra_end;
713         unsigned int             pps; /* pages per stripe */
714         int                      stripe;
715         int                      index;
716         int                      rc;
717         ENTRY;
718
719         offset = cl_offset(obj, start);
720         index = lov_lsm_entry(loo->lo_lsm, offset);
721         if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index))
722                 RETURN(-ENODATA);
723
724         stripe = lov_stripe_number(loo->lo_lsm, index, offset);
725
726         r0 = lov_r0(loo, index);
727         if (unlikely(r0->lo_sub[stripe] == NULL))
728                 RETURN(-EIO);
729
730         sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
731         if (IS_ERR(sub))
732                 RETURN(PTR_ERR(sub));
733
734         lov_stripe_offset(loo->lo_lsm, index, offset, stripe, &suboff);
735         rc = cl_io_read_ahead(sub->sub_env, &sub->sub_io,
736                               cl_index(lovsub2cl(r0->lo_sub[stripe]), suboff),
737                               ra);
738
739         CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
740                PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, r0->lo_nr, rc);
741         if (rc != 0)
742                 RETURN(rc);
743
744         /**
745          * Adjust the stripe index by layout of comp. ra->cra_end is the
746          * maximum page index covered by an underlying DLM lock.
747          * This function converts cra_end from stripe level to file level, and
748          * make sure it's not beyond stripe and component boundary.
749          */
750
751         /* cra_end is stripe level, convert it into file level */
752         ra_end = ra->cra_end;
753         if (ra_end != CL_PAGE_EOF)
754                 ra->cra_end = lov_stripe_pgoff(loo->lo_lsm, index,
755                                                ra_end, stripe);
756
757         /* boundary of current component */
758         ra_end = cl_index(obj, (loff_t)lov_lse(loo, index)->lsme_extent.e_end);
759         if (ra_end != CL_PAGE_EOF && ra->cra_end >= ra_end)
760                 ra->cra_end = ra_end - 1;
761
762         if (r0->lo_nr == 1) /* single stripe file */
763                 RETURN(0);
764
765         pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
766
767         CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %u, "
768                "stripe_size = %u, stripe no = %u, start index = %lu\n",
769                PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, pps, index,
770                lov_lse(loo, index)->lsme_stripe_size, stripe, start);
771
772         /* never exceed the end of the stripe */
773         ra->cra_end = min_t(pgoff_t,
774                             ra->cra_end, start + pps - start % pps - 1);
775         RETURN(0);
776 }
777
778 /**
779  * lov implementation of cl_operations::cio_submit() method. It takes a list
780  * of pages in \a queue, splits it into per-stripe sub-lists, invokes
781  * cl_io_submit() on underlying devices to submit sub-lists, and then splices
782  * everything back.
783  *
784  * Major complication of this function is a need to handle memory cleansing:
785  * cl_io_submit() is called to write out pages as a part of VM memory
786  * reclamation, and hence it may not fail due to memory shortages (system
787  * dead-locks otherwise). To deal with this, some resources (sub-lists,
788  * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
789  * not-memory cleansing context), and in case of memory shortage, these
790  * pre-allocated resources are used by lov_io_submit() under
791  * lov_device::ld_mutex mutex.
792  */
793 static int lov_io_submit(const struct lu_env *env,
794                          const struct cl_io_slice *ios,
795                          enum cl_req_type crt, struct cl_2queue *queue)
796 {
797         struct cl_page_list     *qin = &queue->c2_qin;
798         struct lov_io           *lio = cl2lov_io(env, ios);
799         struct lov_io_sub       *sub;
800         struct cl_page_list     *plist = &lov_env_info(env)->lti_plist;
801         struct cl_page          *page;
802         int index;
803         int rc = 0;
804         ENTRY;
805
806         if (lio->lis_nr_subios == 1) {
807                 int idx = lio->lis_single_subio_index;
808
809                 sub = lov_sub_get(env, lio, idx);
810                 LASSERT(!IS_ERR(sub));
811                 LASSERT(sub == &lio->lis_single_subio);
812                 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
813                                      crt, queue);
814                 RETURN(rc);
815         }
816
817         cl_page_list_init(plist);
818         while (qin->pl_nr > 0) {
819                 struct cl_2queue  *cl2q = &lov_env_info(env)->lti_cl2q;
820
821                 cl_2queue_init(cl2q);
822
823                 page = cl_page_list_first(qin);
824                 cl_page_list_move(&cl2q->c2_qin, qin, page);
825
826                 index = lov_page_index(page);
827                 while (qin->pl_nr > 0) {
828                         page = cl_page_list_first(qin);
829                         if (index != lov_page_index(page))
830                                 break;
831
832                         cl_page_list_move(&cl2q->c2_qin, qin, page);
833                 }
834
835                 sub = lov_sub_get(env, lio, index);
836                 if (!IS_ERR(sub)) {
837                         rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
838                                              crt, cl2q);
839                 } else {
840                         rc = PTR_ERR(sub);
841                 }
842
843                 cl_page_list_splice(&cl2q->c2_qin, plist);
844                 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
845                 cl_2queue_fini(env, cl2q);
846
847                 if (rc != 0)
848                         break;
849         }
850
851         cl_page_list_splice(plist, qin);
852         cl_page_list_fini(env, plist);
853
854         RETURN(rc);
855 }
856
857 static int lov_io_commit_async(const struct lu_env *env,
858                                const struct cl_io_slice *ios,
859                                struct cl_page_list *queue, int from, int to,
860                                cl_commit_cbt cb)
861 {
862         struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
863         struct lov_io     *lio = cl2lov_io(env, ios);
864         struct lov_io_sub *sub;
865         struct cl_page *page;
866         int rc = 0;
867         ENTRY;
868
869         if (lio->lis_nr_subios == 1) {
870                 int idx = lio->lis_single_subio_index;
871
872                 sub = lov_sub_get(env, lio, idx);
873                 LASSERT(!IS_ERR(sub));
874                 LASSERT(sub == &lio->lis_single_subio);
875                 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io, queue,
876                                         from, to, cb);
877                 RETURN(rc);
878         }
879
880         cl_page_list_init(plist);
881         while (queue->pl_nr > 0) {
882                 int stripe_to = to;
883                 int index;
884
885                 LASSERT(plist->pl_nr == 0);
886                 page = cl_page_list_first(queue);
887                 cl_page_list_move(plist, queue, page);
888
889                 index = lov_page_index(page);
890                 while (queue->pl_nr > 0) {
891                         page = cl_page_list_first(queue);
892                         if (index != lov_page_index(page))
893                                 break;
894
895                         cl_page_list_move(plist, queue, page);
896                 }
897
898                 if (queue->pl_nr > 0) /* still has more pages */
899                         stripe_to = PAGE_SIZE;
900
901                 sub = lov_sub_get(env, lio, index);
902                 if (!IS_ERR(sub)) {
903                         rc = cl_io_commit_async(sub->sub_env, &sub->sub_io,
904                                                 plist, from, stripe_to, cb);
905                 } else {
906                         rc = PTR_ERR(sub);
907                         break;
908                 }
909
910                 if (plist->pl_nr > 0) /* short write */
911                         break;
912
913                 from = 0;
914         }
915
916         /* for error case, add the page back into the qin list */
917         LASSERT(ergo(rc == 0, plist->pl_nr == 0));
918         while (plist->pl_nr > 0) {
919                 /* error occurred, add the uncommitted pages back into queue */
920                 page = cl_page_list_last(plist);
921                 cl_page_list_move_head(queue, plist, page);
922         }
923
924         RETURN(rc);
925 }
926
927 static int lov_io_fault_start(const struct lu_env *env,
928                               const struct cl_io_slice *ios)
929 {
930         struct cl_fault_io *fio;
931         struct lov_io      *lio;
932         struct lov_io_sub  *sub;
933
934         ENTRY;
935
936         fio = &ios->cis_io->u.ci_fault;
937         lio = cl2lov_io(env, ios);
938         sub = lov_sub_get(env, lio, lov_page_index(fio->ft_page));
939         sub->sub_io.u.ci_fault.ft_nob = fio->ft_nob;
940
941         RETURN(lov_io_start(env, ios));
942 }
943
944 static void lov_io_fsync_end(const struct lu_env *env,
945                              const struct cl_io_slice *ios)
946 {
947         struct lov_io *lio = cl2lov_io(env, ios);
948         struct lov_io_sub *sub;
949         unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
950         ENTRY;
951
952         *written = 0;
953         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
954                 struct cl_io *subio = &sub->sub_io;
955
956                 lov_io_end_wrapper(sub->sub_env, subio);
957
958                 if (subio->ci_result == 0)
959                         *written += subio->u.ci_fsync.fi_nr_written;
960         }
961         RETURN_EXIT;
962 }
963
964 static const struct cl_io_operations lov_io_ops = {
965         .op = {
966                 [CIT_READ] = {
967                         .cio_fini      = lov_io_fini,
968                         .cio_iter_init = lov_io_rw_iter_init,
969                         .cio_iter_fini = lov_io_iter_fini,
970                         .cio_lock      = lov_io_lock,
971                         .cio_unlock    = lov_io_unlock,
972                         .cio_start     = lov_io_start,
973                         .cio_end       = lov_io_end
974                 },
975                 [CIT_WRITE] = {
976                         .cio_fini      = lov_io_fini,
977                         .cio_iter_init = lov_io_rw_iter_init,
978                         .cio_iter_fini = lov_io_iter_fini,
979                         .cio_lock      = lov_io_lock,
980                         .cio_unlock    = lov_io_unlock,
981                         .cio_start     = lov_io_start,
982                         .cio_end       = lov_io_end
983                 },
984                 [CIT_SETATTR] = {
985                         .cio_fini      = lov_io_fini,
986                         .cio_iter_init = lov_io_setattr_iter_init,
987                         .cio_iter_fini = lov_io_iter_fini,
988                         .cio_lock      = lov_io_lock,
989                         .cio_unlock    = lov_io_unlock,
990                         .cio_start     = lov_io_start,
991                         .cio_end       = lov_io_end
992                 },
993                 [CIT_DATA_VERSION] = {
994                         .cio_fini       = lov_io_fini,
995                         .cio_iter_init  = lov_io_iter_init,
996                         .cio_iter_fini  = lov_io_iter_fini,
997                         .cio_lock       = lov_io_lock,
998                         .cio_unlock     = lov_io_unlock,
999                         .cio_start      = lov_io_start,
1000                         .cio_end        = lov_io_data_version_end,
1001                 },
1002                 [CIT_FAULT] = {
1003                         .cio_fini      = lov_io_fini,
1004                         .cio_iter_init = lov_io_iter_init,
1005                         .cio_iter_fini = lov_io_iter_fini,
1006                         .cio_lock      = lov_io_lock,
1007                         .cio_unlock    = lov_io_unlock,
1008                         .cio_start     = lov_io_fault_start,
1009                         .cio_end       = lov_io_end
1010                 },
1011                 [CIT_FSYNC] = {
1012                         .cio_fini      = lov_io_fini,
1013                         .cio_iter_init = lov_io_iter_init,
1014                         .cio_iter_fini = lov_io_iter_fini,
1015                         .cio_lock      = lov_io_lock,
1016                         .cio_unlock    = lov_io_unlock,
1017                         .cio_start     = lov_io_start,
1018                         .cio_end       = lov_io_fsync_end
1019                 },
1020                 [CIT_LADVISE] = {
1021                         .cio_fini      = lov_io_fini,
1022                         .cio_iter_init = lov_io_iter_init,
1023                         .cio_iter_fini = lov_io_iter_fini,
1024                         .cio_lock      = lov_io_lock,
1025                         .cio_unlock    = lov_io_unlock,
1026                         .cio_start     = lov_io_start,
1027                         .cio_end       = lov_io_end
1028                 },
1029                 [CIT_GLIMPSE] = {
1030                         .cio_fini      = lov_io_fini,
1031                 },
1032                 [CIT_MISC] = {
1033                         .cio_fini      = lov_io_fini
1034                 }
1035         },
1036         .cio_read_ahead                = lov_io_read_ahead,
1037         .cio_submit                    = lov_io_submit,
1038         .cio_commit_async              = lov_io_commit_async,
1039 };
1040
1041 /*****************************************************************************
1042  *
1043  * Empty lov io operations.
1044  *
1045  */
1046
1047 static void lov_empty_io_fini(const struct lu_env *env,
1048                               const struct cl_io_slice *ios)
1049 {
1050         struct lov_object *lov = cl2lov(ios->cis_obj);
1051         ENTRY;
1052
1053         if (atomic_dec_and_test(&lov->lo_active_ios))
1054                 wake_up_all(&lov->lo_waitq);
1055         EXIT;
1056 }
1057
1058 static int lov_empty_io_submit(const struct lu_env *env,
1059                                const struct cl_io_slice *ios,
1060                                enum cl_req_type crt, struct cl_2queue *queue)
1061 {
1062         return -EBADF;
1063 }
1064
1065 static void lov_empty_impossible(const struct lu_env *env,
1066                                  struct cl_io_slice *ios)
1067 {
1068         LBUG();
1069 }
1070
1071 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
1072
1073 /**
1074  * An io operation vector for files without stripes.
1075  */
1076 static const struct cl_io_operations lov_empty_io_ops = {
1077         .op = {
1078                 [CIT_READ] = {
1079                         .cio_fini       = lov_empty_io_fini,
1080 #if 0
1081                         .cio_iter_init  = LOV_EMPTY_IMPOSSIBLE,
1082                         .cio_lock       = LOV_EMPTY_IMPOSSIBLE,
1083                         .cio_start      = LOV_EMPTY_IMPOSSIBLE,
1084                         .cio_end        = LOV_EMPTY_IMPOSSIBLE
1085 #endif
1086                 },
1087                 [CIT_WRITE] = {
1088                         .cio_fini      = lov_empty_io_fini,
1089                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1090                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
1091                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
1092                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
1093                 },
1094                 [CIT_SETATTR] = {
1095                         .cio_fini      = lov_empty_io_fini,
1096                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1097                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
1098                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
1099                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
1100                 },
1101                 [CIT_FAULT] = {
1102                         .cio_fini      = lov_empty_io_fini,
1103                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1104                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
1105                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
1106                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
1107                 },
1108                 [CIT_FSYNC] = {
1109                         .cio_fini      = lov_empty_io_fini
1110                 },
1111                 [CIT_LADVISE] = {
1112                         .cio_fini   = lov_empty_io_fini
1113                 },
1114                 [CIT_GLIMPSE] = {
1115                         .cio_fini      = lov_empty_io_fini
1116                 },
1117                 [CIT_MISC] = {
1118                         .cio_fini      = lov_empty_io_fini
1119                 }
1120         },
1121         .cio_submit                    = lov_empty_io_submit,
1122         .cio_commit_async              = LOV_EMPTY_IMPOSSIBLE
1123 };
1124
1125 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
1126                           struct cl_io *io)
1127 {
1128         struct lov_io       *lio = lov_env_io(env);
1129         struct lov_object   *lov = cl2lov(obj);
1130
1131         ENTRY;
1132         INIT_LIST_HEAD(&lio->lis_active);
1133         io->ci_result = lov_io_slice_init(lio, lov, io);
1134         if (io->ci_result != 0)
1135                 RETURN(io->ci_result);
1136
1137         if (io->ci_result == 0) {
1138                 io->ci_result = lov_io_subio_init(env, lio, io);
1139                 if (io->ci_result == 0) {
1140                         cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
1141                         atomic_inc(&lov->lo_active_ios);
1142                 }
1143         }
1144         RETURN(io->ci_result);
1145 }
1146
1147 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
1148                       struct cl_io *io)
1149 {
1150         struct lov_object *lov = cl2lov(obj);
1151         struct lov_io *lio = lov_env_io(env);
1152         int result;
1153         ENTRY;
1154
1155         lio->lis_object = lov;
1156         switch (io->ci_type) {
1157         default:
1158                 LBUG();
1159         case CIT_MISC:
1160         case CIT_GLIMPSE:
1161         case CIT_READ:
1162                 result = 0;
1163                 break;
1164         case CIT_FSYNC:
1165         case CIT_LADVISE:
1166         case CIT_SETATTR:
1167         case CIT_DATA_VERSION:
1168                 result = +1;
1169                 break;
1170         case CIT_WRITE:
1171                 result = -EBADF;
1172                 break;
1173         case CIT_FAULT:
1174                 result = -EFAULT;
1175                 CERROR("Page fault on a file without stripes: "DFID"\n",
1176                        PFID(lu_object_fid(&obj->co_lu)));
1177                 break;
1178         }
1179         if (result == 0) {
1180                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1181                 atomic_inc(&lov->lo_active_ios);
1182         }
1183
1184         io->ci_result = result < 0 ? result : 0;
1185         RETURN(result);
1186 }
1187
1188 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
1189                         struct cl_io *io)
1190 {
1191         struct lov_object *lov = cl2lov(obj);
1192         struct lov_io *lio = lov_env_io(env);
1193         int result;
1194         ENTRY;
1195
1196         LASSERT(lov->lo_lsm != NULL);
1197         lio->lis_object = lov;
1198
1199         switch (io->ci_type) {
1200         default:
1201                 LASSERTF(0, "invalid type %d\n", io->ci_type);
1202                 result = -EOPNOTSUPP;
1203                 break;
1204         case CIT_MISC:
1205         case CIT_GLIMPSE:
1206         case CIT_FSYNC:
1207         case CIT_LADVISE:
1208         case CIT_DATA_VERSION:
1209                 result = 1;
1210                 break;
1211         case CIT_SETATTR:
1212                 /* the truncate to 0 is managed by MDT:
1213                  * - in open, for open O_TRUNC
1214                  * - in setattr, for truncate
1215                  */
1216                 /* the truncate is for size > 0 so triggers a restore */
1217                 if (cl_io_is_trunc(io)) {
1218                         io->ci_restore_needed = 1;
1219                         result = -ENODATA;
1220                 } else
1221                         result = 1;
1222                 break;
1223         case CIT_READ:
1224         case CIT_WRITE:
1225         case CIT_FAULT:
1226                 io->ci_restore_needed = 1;
1227                 result = -ENODATA;
1228                 break;
1229         }
1230
1231         if (result == 0) {
1232                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1233                 atomic_inc(&lov->lo_active_ios);
1234         }
1235
1236         io->ci_result = result < 0 ? result : 0;
1237         RETURN(result);
1238 }
1239 /** @} lov */