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