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)
313 struct lu_extent ext;
319 lio->lis_object = obj;
321 LASSERT(obj->lo_lsm != NULL);
323 switch (io->ci_type) {
326 lio->lis_pos = io->u.ci_rw.rw_range.cir_pos;
327 lio->lis_endpos = lio->lis_pos + io->u.ci_rw.rw_range.cir_count;
328 lio->lis_io_endpos = lio->lis_endpos;
329 if (cl_io_is_append(io)) {
330 LASSERT(io->ci_type == CIT_WRITE);
332 /* If there is LOV EA hole, then we may cannot locate
333 * the current file-tail exactly. */
334 if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
339 lio->lis_endpos = OBD_OBJECT_EOF;
344 if (cl_io_is_trunc(io))
345 lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
348 lio->lis_endpos = OBD_OBJECT_EOF;
351 case CIT_DATA_VERSION:
353 lio->lis_endpos = OBD_OBJECT_EOF;
357 pgoff_t index = io->u.ci_fault.ft_index;
358 lio->lis_pos = cl_offset(io->ci_obj, index);
359 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
364 lio->lis_pos = io->u.ci_fsync.fi_start;
365 lio->lis_endpos = io->u.ci_fsync.fi_end;
370 lio->lis_pos = io->u.ci_ladvise.li_start;
371 lio->lis_endpos = io->u.ci_ladvise.li_end;
377 lio->lis_endpos = OBD_OBJECT_EOF;
379 if (lov_flr_state(obj) == LCM_FL_RDONLY &&
380 !OBD_FAIL_CHECK(OBD_FAIL_FLR_GLIMPSE_IMMUTABLE))
381 RETURN(1); /* SoM is accurate, no need glimpse */
386 lio->lis_endpos = OBD_OBJECT_EOF;
393 result = lov_io_mirror_init(lio, obj, io);
397 /* check if it needs to instantiate layout */
398 if (!(io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io) ||
399 (cl_io_is_trunc(io) && io->u.ci_setattr.sa_attr.lvb_size > 0)))
402 ext.e_start = lio->lis_pos;
403 ext.e_end = lio->lis_endpos;
405 /* for truncate, it only needs to instantiate the components
406 * before the truncated size. */
407 if (cl_io_is_trunc(io)) {
409 ext.e_end = io->u.ci_setattr.sa_attr.lvb_size;
413 lov_foreach_io_layout(index, lio, &ext) {
414 if (!lsm_entry_inited(obj->lo_lsm, index)) {
415 io->ci_need_write_intent = 1;
416 io->ci_write_intent = ext;
425 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
427 struct lov_io *lio = cl2lov_io(env, ios);
428 struct lov_object *lov = cl2lov(ios->cis_obj);
432 LASSERT(list_empty(&lio->lis_active));
434 while (!list_empty(&lio->lis_subios)) {
435 struct lov_io_sub *sub = list_entry(lio->lis_subios.next,
439 list_del_init(&sub->sub_list);
440 lio->lis_nr_subios--;
442 lov_io_sub_fini(env, lio, sub);
443 lov_sub_free(lio, sub);
445 LASSERT(lio->lis_nr_subios == 0);
447 LASSERT(atomic_read(&lov->lo_active_ios) > 0);
448 if (atomic_dec_and_test(&lov->lo_active_ios))
449 wake_up_all(&lov->lo_waitq);
453 static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
454 loff_t start, loff_t end)
456 struct cl_io *io = &sub->sub_io;
457 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
458 struct cl_io *parent = lio->lis_cl.cis_io;
459 int index = lov_comp_entry(sub->sub_subio_index);
460 int stripe = lov_comp_stripe(sub->sub_subio_index);
462 io->ci_pio = parent->ci_pio;
463 switch (io->ci_type) {
465 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
466 io->u.ci_setattr.sa_attr_flags =
467 parent->u.ci_setattr.sa_attr_flags;
468 io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
469 io->u.ci_setattr.sa_stripe_index = stripe;
470 io->u.ci_setattr.sa_parent_fid =
471 parent->u.ci_setattr.sa_parent_fid;
472 if (cl_io_is_trunc(io)) {
473 loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
475 new_size = lov_size_to_stripe(lsm, index, new_size,
477 io->u.ci_setattr.sa_attr.lvb_size = new_size;
479 lov_lsm2layout(lsm, lsm->lsm_entries[index],
480 &io->u.ci_setattr.sa_layout);
483 case CIT_DATA_VERSION: {
484 io->u.ci_data_version.dv_data_version = 0;
485 io->u.ci_data_version.dv_flags =
486 parent->u.ci_data_version.dv_flags;
490 struct cl_object *obj = parent->ci_obj;
491 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
493 io->u.ci_fault = parent->u.ci_fault;
494 off = lov_size_to_stripe(lsm, index, off, stripe);
495 io->u.ci_fault.ft_index = cl_index(obj, off);
499 io->u.ci_fsync.fi_start = start;
500 io->u.ci_fsync.fi_end = end;
501 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
502 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
507 io->u.ci_rw.rw_ptask = parent->u.ci_rw.rw_ptask;
508 io->u.ci_rw.rw_iter = parent->u.ci_rw.rw_iter;
509 io->u.ci_rw.rw_iocb = parent->u.ci_rw.rw_iocb;
510 io->u.ci_rw.rw_file = parent->u.ci_rw.rw_file;
511 io->u.ci_rw.rw_sync = parent->u.ci_rw.rw_sync;
512 if (cl_io_is_append(parent)) {
513 io->u.ci_rw.rw_append = 1;
515 io->u.ci_rw.rw_range.cir_pos = start;
516 io->u.ci_rw.rw_range.cir_count = end - start;
521 io->u.ci_ladvise.li_start = start;
522 io->u.ci_ladvise.li_end = end;
523 io->u.ci_ladvise.li_fid = parent->u.ci_ladvise.li_fid;
524 io->u.ci_ladvise.li_advice = parent->u.ci_ladvise.li_advice;
525 io->u.ci_ladvise.li_flags = parent->u.ci_ladvise.li_flags;
535 static loff_t lov_offset_mod(loff_t val, int delta)
537 if (val != OBD_OBJECT_EOF)
542 static int lov_io_iter_init(const struct lu_env *env,
543 const struct cl_io_slice *ios)
545 struct lov_io *lio = cl2lov_io(env, ios);
546 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
547 struct lov_io_sub *sub;
548 struct lu_extent ext;
554 ext.e_start = lio->lis_pos;
555 ext.e_end = lio->lis_endpos;
557 lov_foreach_io_layout(index, lio, &ext) {
558 struct lov_layout_raid0 *r0 = lov_r0(lio->lis_object, index);
563 CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
564 index, lsm->lsm_entries[index]->lsme_flags);
565 if (!lsm_entry_inited(lsm, index)) {
566 /* Read from uninitialized components should return
567 * zero filled pages. */
571 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
572 if (!lov_stripe_intersects(lsm, index, stripe,
576 if (unlikely(r0->lo_sub[stripe] == NULL)) {
577 if (ios->cis_io->ci_type == CIT_READ ||
578 ios->cis_io->ci_type == CIT_WRITE ||
579 ios->cis_io->ci_type == CIT_FAULT)
585 end = lov_offset_mod(end, 1);
586 sub = lov_sub_get(env, lio,
587 lov_comp_index(index, stripe));
593 lov_io_sub_inherit(sub, lio, start, end);
594 rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
596 cl_io_iter_fini(sub->sub_env, &sub->sub_io);
601 "shrink stripe: {%d, %d} range: [%llu, %llu)\n",
602 index, stripe, start, end);
604 list_add_tail(&sub->sub_linkage, &lio->lis_active);
612 static int lov_io_rw_iter_init(const struct lu_env *env,
613 const struct cl_io_slice *ios)
615 struct cl_io *io = ios->cis_io;
616 struct lov_io *lio = cl2lov_io(env, ios);
617 struct lov_stripe_md_entry *lse;
618 struct cl_io_range *range = &io->u.ci_rw.rw_range;
619 loff_t start = range->cir_pos;
623 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
626 if (cl_io_is_append(io))
627 RETURN(lov_io_iter_init(env, ios));
629 index = lov_io_layout_at(lio, range->cir_pos);
630 if (index < 0) { /* non-existing layout component */
631 if (io->ci_type == CIT_READ) {
632 /* TODO: it needs to detect the next component and
633 * then set the next pos */
635 /* execute it in main thread */
638 RETURN(lov_io_iter_init(env, ios));
644 lse = lov_lse(lio->lis_object, index);
646 next = MAX_LFS_FILESIZE;
647 if (lse->lsme_stripe_count > 1) {
648 unsigned long ssize = lse->lsme_stripe_size;
650 lov_do_div64(start, ssize);
651 next = (start + 1) * ssize;
652 if (next <= start * ssize)
653 next = MAX_LFS_FILESIZE;
656 LASSERTF(range->cir_pos >= lse->lsme_extent.e_start,
657 "pos %lld, [%lld, %lld)\n", range->cir_pos,
658 lse->lsme_extent.e_start, lse->lsme_extent.e_end);
659 next = min_t(__u64, next, lse->lsme_extent.e_end);
660 next = min_t(loff_t, next, lio->lis_io_endpos);
662 io->ci_continue = next < lio->lis_io_endpos;
663 range->cir_count = next - range->cir_pos;
664 lio->lis_pos = range->cir_pos;
665 lio->lis_endpos = range->cir_pos + range->cir_count;
667 "stripe: {%d, %llu} range: [%llu, %llu) end: %llu, count: %zd\n",
668 index, start, lio->lis_pos, lio->lis_endpos,
669 lio->lis_io_endpos, range->cir_count);
671 if (!io->ci_continue) {
672 /* the last piece of IO, execute it in main thread */
680 * XXX The following call should be optimized: we know, that
681 * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
683 RETURN(lov_io_iter_init(env, ios));
686 static int lov_io_setattr_iter_init(const struct lu_env *env,
687 const struct cl_io_slice *ios)
689 struct lov_io *lio = cl2lov_io(env, ios);
690 struct cl_io *io = ios->cis_io;
694 if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
695 index = lov_io_layout_at(lio, lio->lis_pos - 1);
696 /* no entry found for such offset */
698 RETURN(io->ci_result = -ENODATA);
701 RETURN(lov_io_iter_init(env, ios));
704 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
705 int (*iofunc)(const struct lu_env *, struct cl_io *))
707 struct cl_io *parent = lio->lis_cl.cis_io;
708 struct lov_io_sub *sub;
712 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
713 rc = iofunc(sub->sub_env, &sub->sub_io);
717 if (parent->ci_result == 0)
718 parent->ci_result = sub->sub_io.ci_result;
723 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
726 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
729 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
732 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
735 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
739 * It's possible that lov_io_start() wasn't called against this
740 * sub-io, either because previous sub-io failed, or upper layer
743 if (io->ci_state == CIS_IO_GOING)
746 io->ci_state = CIS_IO_FINISHED;
750 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
752 cl_io_iter_fini(env, io);
756 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
758 cl_io_unlock(env, io);
762 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
766 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
771 lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
773 struct lov_io *lio = cl2lov_io(env, ios);
774 struct cl_io *parent = lio->lis_cl.cis_io;
775 struct lov_io_sub *sub;
778 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
779 lov_io_end_wrapper(env, &sub->sub_io);
781 parent->u.ci_data_version.dv_data_version +=
782 sub->sub_io.u.ci_data_version.dv_data_version;
784 if (parent->ci_result == 0)
785 parent->ci_result = sub->sub_io.ci_result;
791 static void lov_io_iter_fini(const struct lu_env *env,
792 const struct cl_io_slice *ios)
794 struct lov_io *lio = cl2lov_io(env, ios);
798 rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
800 while (!list_empty(&lio->lis_active))
801 list_del_init(lio->lis_active.next);
805 static void lov_io_unlock(const struct lu_env *env,
806 const struct cl_io_slice *ios)
811 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
816 static int lov_io_read_ahead(const struct lu_env *env,
817 const struct cl_io_slice *ios,
818 pgoff_t start, struct cl_read_ahead *ra)
820 struct lov_io *lio = cl2lov_io(env, ios);
821 struct lov_object *loo = lio->lis_object;
822 struct cl_object *obj = lov2cl(loo);
823 struct lov_layout_raid0 *r0;
824 struct lov_io_sub *sub;
828 unsigned int pps; /* pages per stripe */
834 offset = cl_offset(obj, start);
835 index = lov_io_layout_at(lio, offset);
836 if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index))
839 stripe = lov_stripe_number(loo->lo_lsm, index, offset);
841 r0 = lov_r0(loo, index);
842 if (unlikely(r0->lo_sub[stripe] == NULL))
845 sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
847 RETURN(PTR_ERR(sub));
849 lov_stripe_offset(loo->lo_lsm, index, offset, stripe, &suboff);
850 rc = cl_io_read_ahead(sub->sub_env, &sub->sub_io,
851 cl_index(lovsub2cl(r0->lo_sub[stripe]), suboff),
854 CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
855 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, r0->lo_nr, rc);
860 * Adjust the stripe index by layout of comp. ra->cra_end is the
861 * maximum page index covered by an underlying DLM lock.
862 * This function converts cra_end from stripe level to file level, and
863 * make sure it's not beyond stripe and component boundary.
866 /* cra_end is stripe level, convert it into file level */
867 ra_end = ra->cra_end;
868 if (ra_end != CL_PAGE_EOF)
869 ra->cra_end = lov_stripe_pgoff(loo->lo_lsm, index,
872 /* boundary of current component */
873 ra_end = cl_index(obj, (loff_t)lov_io_extent(lio, index)->e_end);
874 if (ra_end != CL_PAGE_EOF && ra->cra_end >= ra_end)
875 ra->cra_end = ra_end - 1;
877 if (r0->lo_nr == 1) /* single stripe file */
880 pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
882 CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %u, "
883 "stripe_size = %u, stripe no = %u, start index = %lu\n",
884 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, pps, index,
885 lov_lse(loo, index)->lsme_stripe_size, stripe, start);
887 /* never exceed the end of the stripe */
888 ra->cra_end = min_t(pgoff_t,
889 ra->cra_end, start + pps - start % pps - 1);
894 * lov implementation of cl_operations::cio_submit() method. It takes a list
895 * of pages in \a queue, splits it into per-stripe sub-lists, invokes
896 * cl_io_submit() on underlying devices to submit sub-lists, and then splices
899 * Major complication of this function is a need to handle memory cleansing:
900 * cl_io_submit() is called to write out pages as a part of VM memory
901 * reclamation, and hence it may not fail due to memory shortages (system
902 * dead-locks otherwise). To deal with this, some resources (sub-lists,
903 * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
904 * not-memory cleansing context), and in case of memory shortage, these
905 * pre-allocated resources are used by lov_io_submit() under
906 * lov_device::ld_mutex mutex.
908 static int lov_io_submit(const struct lu_env *env,
909 const struct cl_io_slice *ios,
910 enum cl_req_type crt, struct cl_2queue *queue)
912 struct cl_page_list *qin = &queue->c2_qin;
913 struct lov_io *lio = cl2lov_io(env, ios);
914 struct lov_io_sub *sub;
915 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
916 struct cl_page *page;
921 if (lio->lis_nr_subios == 1) {
922 int idx = lio->lis_single_subio_index;
924 sub = lov_sub_get(env, lio, idx);
925 LASSERT(!IS_ERR(sub));
926 LASSERT(sub == &lio->lis_single_subio);
927 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
932 cl_page_list_init(plist);
933 while (qin->pl_nr > 0) {
934 struct cl_2queue *cl2q = &lov_env_info(env)->lti_cl2q;
936 cl_2queue_init(cl2q);
938 page = cl_page_list_first(qin);
939 cl_page_list_move(&cl2q->c2_qin, qin, page);
941 index = lov_page_index(page);
942 while (qin->pl_nr > 0) {
943 page = cl_page_list_first(qin);
944 if (index != lov_page_index(page))
947 cl_page_list_move(&cl2q->c2_qin, qin, page);
950 sub = lov_sub_get(env, lio, index);
952 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
958 cl_page_list_splice(&cl2q->c2_qin, plist);
959 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
960 cl_2queue_fini(env, cl2q);
966 cl_page_list_splice(plist, qin);
967 cl_page_list_fini(env, plist);
972 static int lov_io_commit_async(const struct lu_env *env,
973 const struct cl_io_slice *ios,
974 struct cl_page_list *queue, int from, int to,
977 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
978 struct lov_io *lio = cl2lov_io(env, ios);
979 struct lov_io_sub *sub;
980 struct cl_page *page;
984 if (lio->lis_nr_subios == 1) {
985 int idx = lio->lis_single_subio_index;
987 sub = lov_sub_get(env, lio, idx);
988 LASSERT(!IS_ERR(sub));
989 LASSERT(sub == &lio->lis_single_subio);
990 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io, queue,
995 cl_page_list_init(plist);
996 while (queue->pl_nr > 0) {
1000 LASSERT(plist->pl_nr == 0);
1001 page = cl_page_list_first(queue);
1002 cl_page_list_move(plist, queue, page);
1004 index = lov_page_index(page);
1005 while (queue->pl_nr > 0) {
1006 page = cl_page_list_first(queue);
1007 if (index != lov_page_index(page))
1010 cl_page_list_move(plist, queue, page);
1013 if (queue->pl_nr > 0) /* still has more pages */
1014 stripe_to = PAGE_SIZE;
1016 sub = lov_sub_get(env, lio, index);
1018 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io,
1019 plist, from, stripe_to, cb);
1025 if (plist->pl_nr > 0) /* short write */
1031 /* for error case, add the page back into the qin list */
1032 LASSERT(ergo(rc == 0, plist->pl_nr == 0));
1033 while (plist->pl_nr > 0) {
1034 /* error occurred, add the uncommitted pages back into queue */
1035 page = cl_page_list_last(plist);
1036 cl_page_list_move_head(queue, plist, page);
1042 static int lov_io_fault_start(const struct lu_env *env,
1043 const struct cl_io_slice *ios)
1045 struct cl_fault_io *fio;
1047 struct lov_io_sub *sub;
1051 fio = &ios->cis_io->u.ci_fault;
1052 lio = cl2lov_io(env, ios);
1053 sub = lov_sub_get(env, lio, lov_page_index(fio->ft_page));
1054 sub->sub_io.u.ci_fault.ft_nob = fio->ft_nob;
1056 RETURN(lov_io_start(env, ios));
1059 static void lov_io_fsync_end(const struct lu_env *env,
1060 const struct cl_io_slice *ios)
1062 struct lov_io *lio = cl2lov_io(env, ios);
1063 struct lov_io_sub *sub;
1064 unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
1068 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1069 struct cl_io *subio = &sub->sub_io;
1071 lov_io_end_wrapper(sub->sub_env, subio);
1073 if (subio->ci_result == 0)
1074 *written += subio->u.ci_fsync.fi_nr_written;
1079 static const struct cl_io_operations lov_io_ops = {
1082 .cio_fini = lov_io_fini,
1083 .cio_iter_init = lov_io_rw_iter_init,
1084 .cio_iter_fini = lov_io_iter_fini,
1085 .cio_lock = lov_io_lock,
1086 .cio_unlock = lov_io_unlock,
1087 .cio_start = lov_io_start,
1088 .cio_end = lov_io_end
1091 .cio_fini = lov_io_fini,
1092 .cio_iter_init = lov_io_rw_iter_init,
1093 .cio_iter_fini = lov_io_iter_fini,
1094 .cio_lock = lov_io_lock,
1095 .cio_unlock = lov_io_unlock,
1096 .cio_start = lov_io_start,
1097 .cio_end = lov_io_end
1100 .cio_fini = lov_io_fini,
1101 .cio_iter_init = lov_io_setattr_iter_init,
1102 .cio_iter_fini = lov_io_iter_fini,
1103 .cio_lock = lov_io_lock,
1104 .cio_unlock = lov_io_unlock,
1105 .cio_start = lov_io_start,
1106 .cio_end = lov_io_end
1108 [CIT_DATA_VERSION] = {
1109 .cio_fini = lov_io_fini,
1110 .cio_iter_init = lov_io_iter_init,
1111 .cio_iter_fini = lov_io_iter_fini,
1112 .cio_lock = lov_io_lock,
1113 .cio_unlock = lov_io_unlock,
1114 .cio_start = lov_io_start,
1115 .cio_end = lov_io_data_version_end,
1118 .cio_fini = lov_io_fini,
1119 .cio_iter_init = lov_io_iter_init,
1120 .cio_iter_fini = lov_io_iter_fini,
1121 .cio_lock = lov_io_lock,
1122 .cio_unlock = lov_io_unlock,
1123 .cio_start = lov_io_fault_start,
1124 .cio_end = lov_io_end
1127 .cio_fini = lov_io_fini,
1128 .cio_iter_init = lov_io_iter_init,
1129 .cio_iter_fini = lov_io_iter_fini,
1130 .cio_lock = lov_io_lock,
1131 .cio_unlock = lov_io_unlock,
1132 .cio_start = lov_io_start,
1133 .cio_end = lov_io_fsync_end
1136 .cio_fini = lov_io_fini,
1137 .cio_iter_init = lov_io_iter_init,
1138 .cio_iter_fini = lov_io_iter_fini,
1139 .cio_lock = lov_io_lock,
1140 .cio_unlock = lov_io_unlock,
1141 .cio_start = lov_io_start,
1142 .cio_end = lov_io_end
1145 .cio_fini = lov_io_fini,
1148 .cio_fini = lov_io_fini
1151 .cio_read_ahead = lov_io_read_ahead,
1152 .cio_submit = lov_io_submit,
1153 .cio_commit_async = lov_io_commit_async,
1156 /*****************************************************************************
1158 * Empty lov io operations.
1162 static void lov_empty_io_fini(const struct lu_env *env,
1163 const struct cl_io_slice *ios)
1165 struct lov_object *lov = cl2lov(ios->cis_obj);
1168 if (atomic_dec_and_test(&lov->lo_active_ios))
1169 wake_up_all(&lov->lo_waitq);
1173 static int lov_empty_io_submit(const struct lu_env *env,
1174 const struct cl_io_slice *ios,
1175 enum cl_req_type crt, struct cl_2queue *queue)
1180 static void lov_empty_impossible(const struct lu_env *env,
1181 struct cl_io_slice *ios)
1186 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
1189 * An io operation vector for files without stripes.
1191 static const struct cl_io_operations lov_empty_io_ops = {
1194 .cio_fini = lov_empty_io_fini,
1196 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1197 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1198 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1199 .cio_end = LOV_EMPTY_IMPOSSIBLE
1203 .cio_fini = lov_empty_io_fini,
1204 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1205 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1206 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1207 .cio_end = LOV_EMPTY_IMPOSSIBLE
1210 .cio_fini = lov_empty_io_fini,
1211 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1212 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1213 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1214 .cio_end = LOV_EMPTY_IMPOSSIBLE
1217 .cio_fini = lov_empty_io_fini,
1218 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1219 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1220 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1221 .cio_end = LOV_EMPTY_IMPOSSIBLE
1224 .cio_fini = lov_empty_io_fini
1227 .cio_fini = lov_empty_io_fini
1230 .cio_fini = lov_empty_io_fini
1233 .cio_fini = lov_empty_io_fini
1236 .cio_submit = lov_empty_io_submit,
1237 .cio_commit_async = LOV_EMPTY_IMPOSSIBLE
1240 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
1243 struct lov_io *lio = lov_env_io(env);
1244 struct lov_object *lov = cl2lov(obj);
1248 INIT_LIST_HEAD(&lio->lis_active);
1249 result = lov_io_slice_init(lio, lov, io);
1253 result = lov_io_subio_init(env, lio, io);
1255 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
1256 atomic_inc(&lov->lo_active_ios);
1260 io->ci_result = result < 0 ? result : 0;
1264 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
1267 struct lov_object *lov = cl2lov(obj);
1268 struct lov_io *lio = lov_env_io(env);
1272 lio->lis_object = lov;
1273 switch (io->ci_type) {
1284 case CIT_DATA_VERSION:
1292 CERROR("Page fault on a file without stripes: "DFID"\n",
1293 PFID(lu_object_fid(&obj->co_lu)));
1297 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1298 atomic_inc(&lov->lo_active_ios);
1301 io->ci_result = result < 0 ? result : 0;
1305 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
1308 struct lov_object *lov = cl2lov(obj);
1309 struct lov_io *lio = lov_env_io(env);
1313 LASSERT(lov->lo_lsm != NULL);
1314 lio->lis_object = lov;
1316 switch (io->ci_type) {
1318 LASSERTF(0, "invalid type %d\n", io->ci_type);
1319 result = -EOPNOTSUPP;
1325 case CIT_DATA_VERSION:
1329 /* the truncate to 0 is managed by MDT:
1330 * - in open, for open O_TRUNC
1331 * - in setattr, for truncate
1333 /* the truncate is for size > 0 so triggers a restore */
1334 if (cl_io_is_trunc(io)) {
1335 io->ci_restore_needed = 1;
1343 io->ci_restore_needed = 1;
1349 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1350 atomic_inc(&lov->lo_active_ios);
1353 io->ci_result = result < 0 ? result : 0;
1358 * Return the index in composite:lo_entries by the file offset
1360 int lov_io_layout_at(struct lov_io *lio, __u64 offset)
1362 struct lov_object *lov = lio->lis_object;
1363 struct lov_layout_composite *comp = &lov->u.composite;
1364 int start_index = 0;
1365 int end_index = comp->lo_entry_count - 1;
1368 LASSERT(lov->lo_type == LLT_COMP);
1370 /* This is actual file offset so nothing can cover eof. */
1371 if (offset == LUSTRE_EOF)
1374 if (lov_is_flr(lov)) {
1375 struct lov_mirror_entry *lre;
1377 LASSERT(lio->lis_mirror_index >= 0);
1379 lre = &comp->lo_mirrors[lio->lis_mirror_index];
1380 start_index = lre->lre_start;
1381 end_index = lre->lre_end;
1384 for (i = start_index; i <= end_index; i++) {
1385 struct lov_layout_entry *lle = lov_entry(lov, i);
1387 if ((offset >= lle->lle_extent->e_start &&
1388 offset < lle->lle_extent->e_end) ||
1389 (offset == OBD_OBJECT_EOF &&
1390 lle->lle_extent->e_end == OBD_OBJECT_EOF))