4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Implementation of cl_io for LOV layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_LOV
40 #include "lov_cl_internal.h"
46 static inline struct lov_io_sub *lov_sub_alloc(struct lov_io *lio, int index)
48 struct lov_io_sub *sub;
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));
60 INIT_LIST_HEAD(&sub->sub_list);
61 INIT_LIST_HEAD(&sub->sub_linkage);
62 sub->sub_subio_index = index;
68 static inline void lov_sub_free(struct lov_io *lio, struct lov_io_sub *sub)
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;
78 static void lov_io_sub_fini(const struct lu_env *env, struct lov_io *lio,
79 struct lov_io_sub *sub)
83 cl_io_fini(sub->sub_env, &sub->sub_io);
85 if (sub->sub_env != NULL && !IS_ERR(sub->sub_env)) {
86 cl_env_put(sub->sub_env, &sub->sub_refcheck);
93 is_index_within_mirror(struct lov_object *lov, int index, int mirror_index)
95 struct lov_layout_composite *comp = &lov->u.composite;
96 struct lov_mirror_entry *lre = &comp->lo_mirrors[mirror_index];
98 return (index >= lre->lre_start && index <= lre->lre_end);
101 static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
102 struct lov_io_sub *sub)
104 struct lov_object *lov = lio->lis_object;
105 struct cl_io *sub_io;
106 struct cl_object *sub_obj;
107 struct cl_io *io = lio->lis_cl.cis_io;
108 int index = lov_comp_entry(sub->sub_subio_index);
109 int stripe = lov_comp_stripe(sub->sub_subio_index);
111 LASSERT(sub->sub_env == NULL);
114 if (unlikely(!lov_r0(lov, index)->lo_sub ||
115 !lov_r0(lov, index)->lo_sub[stripe]))
118 LASSERTF(is_index_within_mirror(lov, index, lio->lis_mirror_index),
119 DFID "iot = %d, index = %d, mirror = %d\n",
120 PFID(lu_object_fid(lov2lu(lov))), io->ci_type, index,
121 lio->lis_mirror_index);
123 /* obtain new environment */
124 sub->sub_env = cl_env_get(&sub->sub_refcheck);
125 if (IS_ERR(sub->sub_env))
126 result = PTR_ERR(sub->sub_env);
128 sub_obj = lovsub2cl(lov_r0(lov, index)->lo_sub[stripe]);
129 sub_io = &sub->sub_io;
131 sub_io->ci_obj = sub_obj;
132 sub_io->ci_result = 0;
134 sub_io->ci_parent = io;
135 sub_io->ci_lockreq = io->ci_lockreq;
136 sub_io->ci_type = io->ci_type;
137 sub_io->ci_no_srvlock = io->ci_no_srvlock;
138 sub_io->ci_noatime = io->ci_noatime;
139 sub_io->ci_pio = io->ci_pio;
140 sub_io->ci_lock_no_expand = io->ci_lock_no_expand;
141 sub_io->ci_ndelay = io->ci_ndelay;
143 result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
146 lov_io_sub_fini(env, lio, sub);
151 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
152 struct lov_io *lio, int index)
154 struct lov_io_sub *sub;
159 list_for_each_entry(sub, &lio->lis_subios, sub_list) {
160 if (sub->sub_subio_index == index) {
167 sub = lov_sub_alloc(lio, index);
169 GOTO(out, rc = -ENOMEM);
171 rc = lov_io_sub_init(env, lio, sub);
173 lov_sub_free(lio, sub);
177 list_add_tail(&sub->sub_list, &lio->lis_subios);
178 lio->lis_nr_subios++;
186 /*****************************************************************************
192 int lov_page_index(const struct cl_page *page)
194 const struct cl_page_slice *slice;
197 slice = cl_page_at(page, &lov_device_type);
198 LASSERT(slice != NULL);
199 LASSERT(slice->cpl_obj != NULL);
201 RETURN(cl2lov_page(slice)->lps_index);
204 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
209 LASSERT(lio->lis_object != NULL);
211 INIT_LIST_HEAD(&lio->lis_subios);
212 lio->lis_single_subio_index = -1;
213 lio->lis_nr_subios = 0;
218 static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
221 struct lov_layout_composite *comp = &obj->u.composite;
226 if (!lov_is_flr(obj)) {
227 LASSERT(comp->lo_preferred_mirror == 0);
228 lio->lis_mirror_index = comp->lo_preferred_mirror;
233 if (io->ci_ndelay_tried == 0 || /* first time to try */
234 /* reset the mirror index if layout has changed */
235 lio->lis_mirror_layout_gen != obj->lo_lsm->lsm_layout_gen) {
236 lio->lis_mirror_layout_gen = obj->lo_lsm->lsm_layout_gen;
237 index = lio->lis_mirror_index = comp->lo_preferred_mirror;
239 index = lio->lis_mirror_index;
242 /* move mirror index to the next one */
243 index = (index + 1) % comp->lo_mirror_count;
246 for (i = 0; i < comp->lo_mirror_count; i++) {
247 struct lu_extent ext = { .e_start = lio->lis_pos,
248 .e_end = lio->lis_pos + 1 };
249 struct lov_mirror_entry *lre;
250 struct lov_layout_entry *lle;
253 lre = &comp->lo_mirrors[(index + i) % comp->lo_mirror_count];
257 lov_foreach_mirror_layout_entry(obj, lle, lre) {
261 if (lu_extent_is_overlapped(&ext, lle->lle_extent)) {
268 index = (index + i) % comp->lo_mirror_count;
272 if (i == comp->lo_mirror_count) {
273 CERROR(DFID": failed to find a component covering "
274 "I/O region at %llu\n",
275 PFID(lu_object_fid(lov2lu(obj))), lio->lis_pos);
277 dump_lsm(D_ERROR, obj->lo_lsm);
282 CDEBUG(D_VFSTRACE, DFID ": flr state: %d, move mirror from %d to %d, "
283 "have retried: %d, mirror count: %d\n",
284 PFID(lu_object_fid(lov2lu(obj))), lov_flr_state(obj),
285 lio->lis_mirror_index, index, io->ci_ndelay_tried,
286 comp->lo_mirror_count);
288 lio->lis_mirror_index = index;
290 /* FLR: if all mirrors have been tried once, most likely the network
291 * of this client has been partitioned. We should relinquish CPU for
292 * a while before trying again. */
293 ++io->ci_ndelay_tried;
294 if (io->ci_ndelay && io->ci_ndelay_tried >= comp->lo_mirror_count) {
295 set_current_state(TASK_INTERRUPTIBLE);
296 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC)); /* 10ms */
297 if (signal_pending(current))
300 /* reset retry counter */
301 io->ci_ndelay_tried = 1;
304 CDEBUG(D_VFSTRACE, "use %sdelayed RPC state for this IO\n",
305 io->ci_ndelay ? "non-" : "");
310 static int lov_io_slice_init(struct lov_io *lio,
311 struct lov_object *obj, struct cl_io *io)
317 lio->lis_object = obj;
319 LASSERT(obj->lo_lsm != NULL);
321 switch (io->ci_type) {
324 lio->lis_pos = io->u.ci_rw.rw_range.cir_pos;
325 lio->lis_endpos = lio->lis_pos + io->u.ci_rw.rw_range.cir_count;
326 lio->lis_io_endpos = lio->lis_endpos;
327 if (cl_io_is_append(io)) {
328 LASSERT(io->ci_type == CIT_WRITE);
330 /* If there is LOV EA hole, then we may cannot locate
331 * the current file-tail exactly. */
332 if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
337 lio->lis_endpos = OBD_OBJECT_EOF;
342 if (cl_io_is_trunc(io))
343 lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
346 lio->lis_endpos = OBD_OBJECT_EOF;
349 case CIT_DATA_VERSION:
351 lio->lis_endpos = OBD_OBJECT_EOF;
355 pgoff_t index = io->u.ci_fault.ft_index;
356 lio->lis_pos = cl_offset(io->ci_obj, index);
357 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
362 lio->lis_pos = io->u.ci_fsync.fi_start;
363 lio->lis_endpos = io->u.ci_fsync.fi_end;
368 lio->lis_pos = io->u.ci_ladvise.li_start;
369 lio->lis_endpos = io->u.ci_ladvise.li_end;
375 lio->lis_endpos = OBD_OBJECT_EOF;
377 if (lov_flr_state(obj) == LCM_FL_RDONLY &&
378 !OBD_FAIL_CHECK(OBD_FAIL_FLR_GLIMPSE_IMMUTABLE))
379 RETURN(1); /* SoM is accurate, no need glimpse */
384 lio->lis_endpos = OBD_OBJECT_EOF;
391 result = lov_io_mirror_init(lio, obj, io);
395 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
397 struct lov_io *lio = cl2lov_io(env, ios);
398 struct lov_object *lov = cl2lov(ios->cis_obj);
402 LASSERT(list_empty(&lio->lis_active));
404 while (!list_empty(&lio->lis_subios)) {
405 struct lov_io_sub *sub = list_entry(lio->lis_subios.next,
409 list_del_init(&sub->sub_list);
410 lio->lis_nr_subios--;
412 lov_io_sub_fini(env, lio, sub);
413 lov_sub_free(lio, sub);
415 LASSERT(lio->lis_nr_subios == 0);
417 LASSERT(atomic_read(&lov->lo_active_ios) > 0);
418 if (atomic_dec_and_test(&lov->lo_active_ios))
419 wake_up_all(&lov->lo_waitq);
423 static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
424 loff_t start, loff_t end)
426 struct cl_io *io = &sub->sub_io;
427 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
428 struct cl_io *parent = lio->lis_cl.cis_io;
429 int index = lov_comp_entry(sub->sub_subio_index);
430 int stripe = lov_comp_stripe(sub->sub_subio_index);
432 io->ci_pio = parent->ci_pio;
433 switch (io->ci_type) {
435 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
436 io->u.ci_setattr.sa_attr_flags =
437 parent->u.ci_setattr.sa_attr_flags;
438 io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
439 io->u.ci_setattr.sa_stripe_index = stripe;
440 io->u.ci_setattr.sa_parent_fid =
441 parent->u.ci_setattr.sa_parent_fid;
442 if (cl_io_is_trunc(io)) {
443 loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
445 new_size = lov_size_to_stripe(lsm, index, new_size,
447 io->u.ci_setattr.sa_attr.lvb_size = new_size;
449 lov_lsm2layout(lsm, lsm->lsm_entries[index],
450 &io->u.ci_setattr.sa_layout);
453 case CIT_DATA_VERSION: {
454 io->u.ci_data_version.dv_data_version = 0;
455 io->u.ci_data_version.dv_flags =
456 parent->u.ci_data_version.dv_flags;
460 struct cl_object *obj = parent->ci_obj;
461 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
463 io->u.ci_fault = parent->u.ci_fault;
464 off = lov_size_to_stripe(lsm, index, off, stripe);
465 io->u.ci_fault.ft_index = cl_index(obj, off);
469 io->u.ci_fsync.fi_start = start;
470 io->u.ci_fsync.fi_end = end;
471 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
472 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
477 io->u.ci_rw.rw_ptask = parent->u.ci_rw.rw_ptask;
478 io->u.ci_rw.rw_iter = parent->u.ci_rw.rw_iter;
479 io->u.ci_rw.rw_iocb = parent->u.ci_rw.rw_iocb;
480 io->u.ci_rw.rw_file = parent->u.ci_rw.rw_file;
481 io->u.ci_rw.rw_sync = parent->u.ci_rw.rw_sync;
482 if (cl_io_is_append(parent)) {
483 io->u.ci_rw.rw_append = 1;
485 io->u.ci_rw.rw_range.cir_pos = start;
486 io->u.ci_rw.rw_range.cir_count = end - start;
491 io->u.ci_ladvise.li_start = start;
492 io->u.ci_ladvise.li_end = end;
493 io->u.ci_ladvise.li_fid = parent->u.ci_ladvise.li_fid;
494 io->u.ci_ladvise.li_advice = parent->u.ci_ladvise.li_advice;
495 io->u.ci_ladvise.li_flags = parent->u.ci_ladvise.li_flags;
505 static loff_t lov_offset_mod(loff_t val, int delta)
507 if (val != OBD_OBJECT_EOF)
512 static int lov_io_iter_init(const struct lu_env *env,
513 const struct cl_io_slice *ios)
515 struct cl_io *io = ios->cis_io;
516 struct lov_io *lio = cl2lov_io(env, ios);
517 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
518 struct lov_io_sub *sub;
519 struct lu_extent ext;
525 ext.e_start = lio->lis_pos;
526 ext.e_end = lio->lis_endpos;
528 lov_foreach_io_layout(index, lio, &ext) {
529 struct lov_layout_raid0 *r0 = lov_r0(lio->lis_object, index);
534 CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
535 index, lsm->lsm_entries[index]->lsme_flags);
536 if (!lsm_entry_inited(lsm, index)) {
537 /* truncate IO will trigger write intent as well, and
538 * it's handled in lov_io_setattr_iter_init() */
539 if (io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io)) {
540 io->ci_need_write_intent = 1;
541 /* execute it in main thread */
547 /* Read from uninitialized components should return
548 * zero filled pages. */
552 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
553 if (!lov_stripe_intersects(lsm, index, stripe,
557 if (unlikely(r0->lo_sub[stripe] == NULL)) {
558 if (ios->cis_io->ci_type == CIT_READ ||
559 ios->cis_io->ci_type == CIT_WRITE ||
560 ios->cis_io->ci_type == CIT_FAULT)
566 end = lov_offset_mod(end, 1);
567 sub = lov_sub_get(env, lio,
568 lov_comp_index(index, stripe));
574 lov_io_sub_inherit(sub, lio, start, end);
575 rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
577 cl_io_iter_fini(sub->sub_env, &sub->sub_io);
582 "shrink stripe: {%d, %d} range: [%llu, %llu)\n",
583 index, stripe, start, end);
585 list_add_tail(&sub->sub_linkage, &lio->lis_active);
593 static int lov_io_rw_iter_init(const struct lu_env *env,
594 const struct cl_io_slice *ios)
596 struct cl_io *io = ios->cis_io;
597 struct lov_io *lio = cl2lov_io(env, ios);
598 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
599 struct lov_stripe_md_entry *lse;
600 struct cl_io_range *range = &io->u.ci_rw.rw_range;
601 loff_t start = range->cir_pos;
605 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
608 if (cl_io_is_append(io))
609 RETURN(lov_io_iter_init(env, ios));
611 index = lov_io_layout_at(lio, range->cir_pos);
612 if (index < 0) { /* non-existing layout component */
613 if (io->ci_type == CIT_READ) {
614 /* TODO: it needs to detect the next component and
615 * then set the next pos */
617 /* execute it in main thread */
620 RETURN(lov_io_iter_init(env, ios));
626 lse = lov_lse(lio->lis_object, index);
628 next = MAX_LFS_FILESIZE;
629 if (lse->lsme_stripe_count > 1) {
630 unsigned long ssize = lse->lsme_stripe_size;
632 lov_do_div64(start, ssize);
633 next = (start + 1) * ssize;
634 if (next <= start * ssize)
635 next = MAX_LFS_FILESIZE;
638 LASSERTF(range->cir_pos >= lse->lsme_extent.e_start,
639 "pos %lld, [%lld, %lld)\n", range->cir_pos,
640 lse->lsme_extent.e_start, lse->lsme_extent.e_end);
641 next = min_t(__u64, next, lse->lsme_extent.e_end);
642 next = min_t(loff_t, next, lio->lis_io_endpos);
644 io->ci_continue = next < lio->lis_io_endpos;
645 range->cir_count = next - range->cir_pos;
646 lio->lis_pos = range->cir_pos;
647 lio->lis_endpos = range->cir_pos + range->cir_count;
649 "stripe: {%d, %llu} range: [%llu, %llu) end: %llu, count: %zd\n",
650 index, start, lio->lis_pos, lio->lis_endpos,
651 lio->lis_io_endpos, range->cir_count);
653 if (!io->ci_continue) {
654 /* the last piece of IO, execute it in main thread */
659 /* it only splits IO here for parallel IO,
660 * there will be no actual IO going to occur,
661 * so it doesn't need to invoke lov_io_iter_init()
662 * to initialize sub IOs. */
663 if (!lsm_entry_inited(lsm, index)) {
664 io->ci_need_write_intent = 1;
671 * XXX The following call should be optimized: we know, that
672 * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
674 RETURN(lov_io_iter_init(env, ios));
677 static int lov_io_setattr_iter_init(const struct lu_env *env,
678 const struct cl_io_slice *ios)
680 struct lov_io *lio = cl2lov_io(env, ios);
681 struct cl_io *io = ios->cis_io;
682 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
686 if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
687 index = lov_io_layout_at(lio, lio->lis_pos - 1);
688 /* no entry found for such offset */
690 RETURN(io->ci_result = -ENODATA);
691 } else if (!lsm_entry_inited(lsm, index)) {
692 io->ci_need_write_intent = 1;
693 RETURN(io->ci_result = -ENODATA);
697 RETURN(lov_io_iter_init(env, ios));
700 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
701 int (*iofunc)(const struct lu_env *, struct cl_io *))
703 struct cl_io *parent = lio->lis_cl.cis_io;
704 struct lov_io_sub *sub;
708 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
709 rc = iofunc(sub->sub_env, &sub->sub_io);
713 if (parent->ci_result == 0)
714 parent->ci_result = sub->sub_io.ci_result;
719 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
722 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
725 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
728 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
731 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
735 * It's possible that lov_io_start() wasn't called against this
736 * sub-io, either because previous sub-io failed, or upper layer
739 if (io->ci_state == CIS_IO_GOING)
742 io->ci_state = CIS_IO_FINISHED;
746 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
748 cl_io_iter_fini(env, io);
752 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
754 cl_io_unlock(env, io);
758 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
762 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
767 lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
769 struct lov_io *lio = cl2lov_io(env, ios);
770 struct cl_io *parent = lio->lis_cl.cis_io;
771 struct lov_io_sub *sub;
774 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
775 lov_io_end_wrapper(env, &sub->sub_io);
777 parent->u.ci_data_version.dv_data_version +=
778 sub->sub_io.u.ci_data_version.dv_data_version;
780 if (parent->ci_result == 0)
781 parent->ci_result = sub->sub_io.ci_result;
787 static void lov_io_iter_fini(const struct lu_env *env,
788 const struct cl_io_slice *ios)
790 struct lov_io *lio = cl2lov_io(env, ios);
794 rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
796 while (!list_empty(&lio->lis_active))
797 list_del_init(lio->lis_active.next);
801 static void lov_io_unlock(const struct lu_env *env,
802 const struct cl_io_slice *ios)
807 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
812 static int lov_io_read_ahead(const struct lu_env *env,
813 const struct cl_io_slice *ios,
814 pgoff_t start, struct cl_read_ahead *ra)
816 struct lov_io *lio = cl2lov_io(env, ios);
817 struct lov_object *loo = lio->lis_object;
818 struct cl_object *obj = lov2cl(loo);
819 struct lov_layout_raid0 *r0;
820 struct lov_io_sub *sub;
824 unsigned int pps; /* pages per stripe */
830 offset = cl_offset(obj, start);
831 index = lov_io_layout_at(lio, offset);
832 if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index))
835 stripe = lov_stripe_number(loo->lo_lsm, index, offset);
837 r0 = lov_r0(loo, index);
838 if (unlikely(r0->lo_sub[stripe] == NULL))
841 sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
843 RETURN(PTR_ERR(sub));
845 lov_stripe_offset(loo->lo_lsm, index, offset, stripe, &suboff);
846 rc = cl_io_read_ahead(sub->sub_env, &sub->sub_io,
847 cl_index(lovsub2cl(r0->lo_sub[stripe]), suboff),
850 CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
851 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, r0->lo_nr, rc);
856 * Adjust the stripe index by layout of comp. ra->cra_end is the
857 * maximum page index covered by an underlying DLM lock.
858 * This function converts cra_end from stripe level to file level, and
859 * make sure it's not beyond stripe and component boundary.
862 /* cra_end is stripe level, convert it into file level */
863 ra_end = ra->cra_end;
864 if (ra_end != CL_PAGE_EOF)
865 ra->cra_end = lov_stripe_pgoff(loo->lo_lsm, index,
868 /* boundary of current component */
869 ra_end = cl_index(obj, (loff_t)lov_io_extent(lio, index)->e_end);
870 if (ra_end != CL_PAGE_EOF && ra->cra_end >= ra_end)
871 ra->cra_end = ra_end - 1;
873 if (r0->lo_nr == 1) /* single stripe file */
876 pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
878 CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %u, "
879 "stripe_size = %u, stripe no = %u, start index = %lu\n",
880 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, pps, index,
881 lov_lse(loo, index)->lsme_stripe_size, stripe, start);
883 /* never exceed the end of the stripe */
884 ra->cra_end = min_t(pgoff_t,
885 ra->cra_end, start + pps - start % pps - 1);
890 * lov implementation of cl_operations::cio_submit() method. It takes a list
891 * of pages in \a queue, splits it into per-stripe sub-lists, invokes
892 * cl_io_submit() on underlying devices to submit sub-lists, and then splices
895 * Major complication of this function is a need to handle memory cleansing:
896 * cl_io_submit() is called to write out pages as a part of VM memory
897 * reclamation, and hence it may not fail due to memory shortages (system
898 * dead-locks otherwise). To deal with this, some resources (sub-lists,
899 * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
900 * not-memory cleansing context), and in case of memory shortage, these
901 * pre-allocated resources are used by lov_io_submit() under
902 * lov_device::ld_mutex mutex.
904 static int lov_io_submit(const struct lu_env *env,
905 const struct cl_io_slice *ios,
906 enum cl_req_type crt, struct cl_2queue *queue)
908 struct cl_page_list *qin = &queue->c2_qin;
909 struct lov_io *lio = cl2lov_io(env, ios);
910 struct lov_io_sub *sub;
911 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
912 struct cl_page *page;
917 if (lio->lis_nr_subios == 1) {
918 int idx = lio->lis_single_subio_index;
920 sub = lov_sub_get(env, lio, idx);
921 LASSERT(!IS_ERR(sub));
922 LASSERT(sub == &lio->lis_single_subio);
923 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
928 cl_page_list_init(plist);
929 while (qin->pl_nr > 0) {
930 struct cl_2queue *cl2q = &lov_env_info(env)->lti_cl2q;
932 cl_2queue_init(cl2q);
934 page = cl_page_list_first(qin);
935 cl_page_list_move(&cl2q->c2_qin, qin, page);
937 index = lov_page_index(page);
938 while (qin->pl_nr > 0) {
939 page = cl_page_list_first(qin);
940 if (index != lov_page_index(page))
943 cl_page_list_move(&cl2q->c2_qin, qin, page);
946 sub = lov_sub_get(env, lio, index);
948 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
954 cl_page_list_splice(&cl2q->c2_qin, plist);
955 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
956 cl_2queue_fini(env, cl2q);
962 cl_page_list_splice(plist, qin);
963 cl_page_list_fini(env, plist);
968 static int lov_io_commit_async(const struct lu_env *env,
969 const struct cl_io_slice *ios,
970 struct cl_page_list *queue, int from, int to,
973 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
974 struct lov_io *lio = cl2lov_io(env, ios);
975 struct lov_io_sub *sub;
976 struct cl_page *page;
980 if (lio->lis_nr_subios == 1) {
981 int idx = lio->lis_single_subio_index;
983 sub = lov_sub_get(env, lio, idx);
984 LASSERT(!IS_ERR(sub));
985 LASSERT(sub == &lio->lis_single_subio);
986 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io, queue,
991 cl_page_list_init(plist);
992 while (queue->pl_nr > 0) {
996 LASSERT(plist->pl_nr == 0);
997 page = cl_page_list_first(queue);
998 cl_page_list_move(plist, queue, page);
1000 index = lov_page_index(page);
1001 while (queue->pl_nr > 0) {
1002 page = cl_page_list_first(queue);
1003 if (index != lov_page_index(page))
1006 cl_page_list_move(plist, queue, page);
1009 if (queue->pl_nr > 0) /* still has more pages */
1010 stripe_to = PAGE_SIZE;
1012 sub = lov_sub_get(env, lio, index);
1014 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io,
1015 plist, from, stripe_to, cb);
1021 if (plist->pl_nr > 0) /* short write */
1027 /* for error case, add the page back into the qin list */
1028 LASSERT(ergo(rc == 0, plist->pl_nr == 0));
1029 while (plist->pl_nr > 0) {
1030 /* error occurred, add the uncommitted pages back into queue */
1031 page = cl_page_list_last(plist);
1032 cl_page_list_move_head(queue, plist, page);
1038 static int lov_io_fault_start(const struct lu_env *env,
1039 const struct cl_io_slice *ios)
1041 struct cl_fault_io *fio;
1043 struct lov_io_sub *sub;
1047 fio = &ios->cis_io->u.ci_fault;
1048 lio = cl2lov_io(env, ios);
1049 sub = lov_sub_get(env, lio, lov_page_index(fio->ft_page));
1050 sub->sub_io.u.ci_fault.ft_nob = fio->ft_nob;
1052 RETURN(lov_io_start(env, ios));
1055 static void lov_io_fsync_end(const struct lu_env *env,
1056 const struct cl_io_slice *ios)
1058 struct lov_io *lio = cl2lov_io(env, ios);
1059 struct lov_io_sub *sub;
1060 unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
1064 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1065 struct cl_io *subio = &sub->sub_io;
1067 lov_io_end_wrapper(sub->sub_env, subio);
1069 if (subio->ci_result == 0)
1070 *written += subio->u.ci_fsync.fi_nr_written;
1075 static const struct cl_io_operations lov_io_ops = {
1078 .cio_fini = lov_io_fini,
1079 .cio_iter_init = lov_io_rw_iter_init,
1080 .cio_iter_fini = lov_io_iter_fini,
1081 .cio_lock = lov_io_lock,
1082 .cio_unlock = lov_io_unlock,
1083 .cio_start = lov_io_start,
1084 .cio_end = lov_io_end
1087 .cio_fini = lov_io_fini,
1088 .cio_iter_init = lov_io_rw_iter_init,
1089 .cio_iter_fini = lov_io_iter_fini,
1090 .cio_lock = lov_io_lock,
1091 .cio_unlock = lov_io_unlock,
1092 .cio_start = lov_io_start,
1093 .cio_end = lov_io_end
1096 .cio_fini = lov_io_fini,
1097 .cio_iter_init = lov_io_setattr_iter_init,
1098 .cio_iter_fini = lov_io_iter_fini,
1099 .cio_lock = lov_io_lock,
1100 .cio_unlock = lov_io_unlock,
1101 .cio_start = lov_io_start,
1102 .cio_end = lov_io_end
1104 [CIT_DATA_VERSION] = {
1105 .cio_fini = lov_io_fini,
1106 .cio_iter_init = lov_io_iter_init,
1107 .cio_iter_fini = lov_io_iter_fini,
1108 .cio_lock = lov_io_lock,
1109 .cio_unlock = lov_io_unlock,
1110 .cio_start = lov_io_start,
1111 .cio_end = lov_io_data_version_end,
1114 .cio_fini = lov_io_fini,
1115 .cio_iter_init = lov_io_iter_init,
1116 .cio_iter_fini = lov_io_iter_fini,
1117 .cio_lock = lov_io_lock,
1118 .cio_unlock = lov_io_unlock,
1119 .cio_start = lov_io_fault_start,
1120 .cio_end = lov_io_end
1123 .cio_fini = lov_io_fini,
1124 .cio_iter_init = lov_io_iter_init,
1125 .cio_iter_fini = lov_io_iter_fini,
1126 .cio_lock = lov_io_lock,
1127 .cio_unlock = lov_io_unlock,
1128 .cio_start = lov_io_start,
1129 .cio_end = lov_io_fsync_end
1132 .cio_fini = lov_io_fini,
1133 .cio_iter_init = lov_io_iter_init,
1134 .cio_iter_fini = lov_io_iter_fini,
1135 .cio_lock = lov_io_lock,
1136 .cio_unlock = lov_io_unlock,
1137 .cio_start = lov_io_start,
1138 .cio_end = lov_io_end
1141 .cio_fini = lov_io_fini,
1144 .cio_fini = lov_io_fini
1147 .cio_read_ahead = lov_io_read_ahead,
1148 .cio_submit = lov_io_submit,
1149 .cio_commit_async = lov_io_commit_async,
1152 /*****************************************************************************
1154 * Empty lov io operations.
1158 static void lov_empty_io_fini(const struct lu_env *env,
1159 const struct cl_io_slice *ios)
1161 struct lov_object *lov = cl2lov(ios->cis_obj);
1164 if (atomic_dec_and_test(&lov->lo_active_ios))
1165 wake_up_all(&lov->lo_waitq);
1169 static int lov_empty_io_submit(const struct lu_env *env,
1170 const struct cl_io_slice *ios,
1171 enum cl_req_type crt, struct cl_2queue *queue)
1176 static void lov_empty_impossible(const struct lu_env *env,
1177 struct cl_io_slice *ios)
1182 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
1185 * An io operation vector for files without stripes.
1187 static const struct cl_io_operations lov_empty_io_ops = {
1190 .cio_fini = lov_empty_io_fini,
1192 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1193 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1194 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1195 .cio_end = LOV_EMPTY_IMPOSSIBLE
1199 .cio_fini = lov_empty_io_fini,
1200 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1201 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1202 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1203 .cio_end = LOV_EMPTY_IMPOSSIBLE
1206 .cio_fini = lov_empty_io_fini,
1207 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1208 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1209 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1210 .cio_end = LOV_EMPTY_IMPOSSIBLE
1213 .cio_fini = lov_empty_io_fini,
1214 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1215 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1216 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1217 .cio_end = LOV_EMPTY_IMPOSSIBLE
1220 .cio_fini = lov_empty_io_fini
1223 .cio_fini = lov_empty_io_fini
1226 .cio_fini = lov_empty_io_fini
1229 .cio_fini = lov_empty_io_fini
1232 .cio_submit = lov_empty_io_submit,
1233 .cio_commit_async = LOV_EMPTY_IMPOSSIBLE
1236 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
1239 struct lov_io *lio = lov_env_io(env);
1240 struct lov_object *lov = cl2lov(obj);
1244 INIT_LIST_HEAD(&lio->lis_active);
1245 result = lov_io_slice_init(lio, lov, io);
1249 result = lov_io_subio_init(env, lio, io);
1251 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
1252 atomic_inc(&lov->lo_active_ios);
1256 io->ci_result = result < 0 ? result : 0;
1260 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
1263 struct lov_object *lov = cl2lov(obj);
1264 struct lov_io *lio = lov_env_io(env);
1268 lio->lis_object = lov;
1269 switch (io->ci_type) {
1280 case CIT_DATA_VERSION:
1288 CERROR("Page fault on a file without stripes: "DFID"\n",
1289 PFID(lu_object_fid(&obj->co_lu)));
1293 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1294 atomic_inc(&lov->lo_active_ios);
1297 io->ci_result = result < 0 ? result : 0;
1301 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
1304 struct lov_object *lov = cl2lov(obj);
1305 struct lov_io *lio = lov_env_io(env);
1309 LASSERT(lov->lo_lsm != NULL);
1310 lio->lis_object = lov;
1312 switch (io->ci_type) {
1314 LASSERTF(0, "invalid type %d\n", io->ci_type);
1315 result = -EOPNOTSUPP;
1321 case CIT_DATA_VERSION:
1325 /* the truncate to 0 is managed by MDT:
1326 * - in open, for open O_TRUNC
1327 * - in setattr, for truncate
1329 /* the truncate is for size > 0 so triggers a restore */
1330 if (cl_io_is_trunc(io)) {
1331 io->ci_restore_needed = 1;
1339 io->ci_restore_needed = 1;
1345 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1346 atomic_inc(&lov->lo_active_ios);
1349 io->ci_result = result < 0 ? result : 0;
1354 * Return the index in composite:lo_entries by the file offset
1356 int lov_io_layout_at(struct lov_io *lio, __u64 offset)
1358 struct lov_object *lov = lio->lis_object;
1359 struct lov_layout_composite *comp = &lov->u.composite;
1360 int start_index = 0;
1361 int end_index = comp->lo_entry_count - 1;
1364 LASSERT(lov->lo_type == LLT_COMP);
1366 /* This is actual file offset so nothing can cover eof. */
1367 if (offset == LUSTRE_EOF)
1370 if (lov_is_flr(lov)) {
1371 struct lov_mirror_entry *lre;
1373 LASSERT(lio->lis_mirror_index >= 0);
1375 lre = &comp->lo_mirrors[lio->lis_mirror_index];
1376 start_index = lre->lre_start;
1377 end_index = lre->lre_end;
1380 for (i = start_index; i <= end_index; i++) {
1381 struct lov_layout_entry *lle = lov_entry(lov, i);
1383 if ((offset >= lle->lle_extent->e_start &&
1384 offset < lle->lle_extent->e_end) ||
1385 (offset == OBD_OBJECT_EOF &&
1386 lle->lle_extent->e_end == OBD_OBJECT_EOF))