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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * Implementation of cl_io for LOV layer.
33 * Author: Nikita Danilov <nikita.danilov@sun.com>
34 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
37 #define DEBUG_SUBSYSTEM S_LOV
39 #include "lov_cl_internal.h"
46 * Allocate a new sub IO
48 * \param[in] lio top level lov IO structure
49 * \param[in] index index into lov (stripe)
51 * \retval Pointer to allocated lov_io_sub
54 static inline struct lov_io_sub *lov_sub_alloc(struct lov_io *lio, int index)
56 struct lov_io_sub *sub;
58 if (lio->lis_nr_subios == 0) {
59 LASSERT(lio->lis_single_subio_index == -1);
60 sub = &lio->lis_single_subio;
61 lio->lis_single_subio_index = index;
62 memset(sub, 0, sizeof(*sub));
68 INIT_LIST_HEAD(&sub->sub_list);
69 INIT_LIST_HEAD(&sub->sub_linkage);
70 sub->sub_subio_index = index;
79 * \param[in] lio top level lov IO structure
80 * \param[in] sub sub io to individual stripe
83 static inline void lov_sub_free(struct lov_io *lio, struct lov_io_sub *sub)
85 if (sub->sub_subio_index == lio->lis_single_subio_index) {
86 LASSERT(sub == &lio->lis_single_subio);
87 lio->lis_single_subio_index = -1;
93 static void lov_io_sub_fini(const struct lu_env *env, struct lov_io *lio,
94 struct lov_io_sub *sub)
98 cl_io_fini(sub->sub_env, &sub->sub_io);
100 if (sub->sub_env && !IS_ERR(sub->sub_env)) {
101 cl_env_put(sub->sub_env, &sub->sub_refcheck);
108 is_index_within_mirror(struct lov_object *lov, int index, int mirror_index)
110 struct lov_mirror_entry *lre = lov_mirror_entry(lov, mirror_index);
112 return (index >= lre->lre_start && index <= lre->lre_end);
115 static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
116 struct lov_io_sub *sub)
118 struct lov_object *lov = lio->lis_object;
119 struct cl_io *sub_io;
120 struct cl_object *sub_obj;
121 struct cl_io *io = lio->lis_cl.cis_io;
122 int index = lov_comp_entry(sub->sub_subio_index);
123 int stripe = lov_comp_stripe(sub->sub_subio_index);
125 LASSERT(sub->sub_env == NULL);
128 if (unlikely(!lov_r0(lov, index)->lo_sub ||
129 !lov_r0(lov, index)->lo_sub[stripe]))
132 LASSERTF(ergo(lov_is_flr(lov),
133 is_index_within_mirror(lov, index,
134 lio->lis_mirror_index)),
135 DFID "iot = %d, index = %d, mirror = %d\n",
136 PFID(lu_object_fid(lov2lu(lov))), io->ci_type, index,
137 lio->lis_mirror_index);
139 /* obtain new environment */
140 sub->sub_env = cl_env_get(&sub->sub_refcheck);
141 if (IS_ERR(sub->sub_env)) {
142 result = PTR_ERR(sub->sub_env);
146 sub_obj = lovsub2cl(lov_r0(lov, index)->lo_sub[stripe]);
147 sub_io = &sub->sub_io;
149 sub_io->ci_obj = sub_obj;
150 sub_io->ci_result = 0;
152 sub_io->ci_parent = io;
153 sub_io->ci_lockreq = io->ci_lockreq;
154 sub_io->ci_type = io->ci_type;
155 sub_io->ci_no_srvlock = io->ci_no_srvlock;
156 sub_io->ci_noatime = io->ci_noatime;
157 sub_io->ci_async_readahead = io->ci_async_readahead;
158 sub_io->ci_lock_no_expand = io->ci_lock_no_expand;
159 sub_io->ci_ndelay = io->ci_ndelay;
160 sub_io->ci_layout_version = io->ci_layout_version;
161 sub_io->ci_tried_all_mirrors = io->ci_tried_all_mirrors;
163 result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
166 lov_io_sub_fini(env, lio, sub);
171 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
172 struct lov_io *lio, int index)
174 struct lov_io_sub *sub;
179 list_for_each_entry(sub, &lio->lis_subios, sub_list) {
180 if (sub->sub_subio_index == index) {
187 sub = lov_sub_alloc(lio, index);
189 GOTO(out, rc = -ENOMEM);
191 rc = lov_io_sub_init(env, lio, sub);
193 lov_sub_free(lio, sub);
197 list_add_tail(&sub->sub_list, &lio->lis_subios);
198 lio->lis_nr_subios++;
204 sub->sub_io.ci_noquota = lio->lis_cl.cis_io->ci_noquota;
211 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
216 LASSERT(lio->lis_object != NULL);
218 INIT_LIST_HEAD(&lio->lis_subios);
219 lio->lis_single_subio_index = -1;
220 lio->lis_nr_subios = 0;
226 * Decide if it will need write intent RPC
228 static int lov_io_mirror_write_intent(struct lov_io *lio,
229 struct lov_object *obj, struct cl_io *io)
231 struct lu_object *lobj = lov2lu(obj);
232 struct lov_layout_composite *comp = &obj->u.composite;
233 struct lu_extent *ext = &io->ci_write_intent;
234 struct lov_mirror_entry *lre;
235 struct lov_mirror_entry *primary;
236 struct lov_layout_entry *lle;
240 *ext = (typeof(*ext)) { lio->lis_pos, lio->lis_endpos };
241 io->ci_need_write_intent = 0;
243 if (!(io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io) ||
244 cl_io_is_fallocate(io) || cl_io_is_trunc(io)))
248 * FLR: check if it needs to send a write intent RPC to server.
249 * Writing to sync_pending file needs write intent RPC to change
250 * the file state back to write_pending, so that the layout version
251 * can be increased when the state changes to sync_pending at a later
252 * time. Otherwise there exists a chance that an evicted client may
253 * dirty the file data while resync client is working on it.
254 * Designated I/O is allowed for resync workload.
256 if (lov_flr_state(obj) == LCM_FL_RDONLY ||
257 (lov_flr_state(obj) == LCM_FL_SYNC_PENDING &&
258 io->ci_designated_mirror == 0)) {
259 io->ci_need_write_intent = 1;
263 LASSERT((lov_flr_state(obj) == LCM_FL_WRITE_PENDING));
264 LASSERT(comp->lo_preferred_mirror >= 0);
267 * need to iterate all components to see if there are
268 * multiple components covering the writing component
270 primary = &comp->lo_mirrors[comp->lo_preferred_mirror];
271 if (primary->lre_stale || !primary->lre_valid) {
273 * new server could pick a primary mirror which old client
274 * does not recognize, and old client would mark it as
277 CERROR(DFID ": cannot find known valid non-stale mirror, "
278 "could be new server picked a mirror which this client "
279 "does not recognize.\n",
280 PFID(lu_object_fid(lobj)));
284 lov_foreach_mirror_layout_entry(obj, lle, primary) {
285 LASSERT(lle->lle_valid);
286 if (!lu_extent_is_overlapped(ext, lle->lle_extent))
289 ext->e_start = min(ext->e_start, lle->lle_extent->e_start);
290 ext->e_end = max(ext->e_end, lle->lle_extent->e_end);
294 CERROR(DFID ": cannot find any valid components covering "
295 "file extent "DEXT", mirror: %d\n",
296 PFID(lu_object_fid(lobj)), PEXT(ext),
297 primary->lre_mirror_id);
302 lov_foreach_mirror_entry(obj, lre) {
306 lov_foreach_mirror_layout_entry(obj, lle, lre) {
310 if (lu_extent_is_overlapped(ext, lle->lle_extent)) {
317 CDEBUG(D_VFSTRACE, DFID "there are %zd components to be staled to "
318 "modify file extent "DEXT", iot: %d\n",
319 PFID(lu_object_fid(lobj)), count, PEXT(ext), io->ci_type);
321 io->ci_need_write_intent = count > 0;
326 static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
329 struct lov_layout_composite *comp = &obj->u.composite;
335 if (!lov_is_flr(obj)) {
336 /* only locks/pages are manipulated for CIT_MISC op, no
337 * cl_io_loop() will be called, don't check/set mirror info.
339 if (io->ci_type != CIT_MISC) {
340 LASSERT(comp->lo_preferred_mirror == 0);
341 lio->lis_mirror_index = comp->lo_preferred_mirror;
347 /* transfer the layout version for verification */
348 if (io->ci_layout_version == 0)
349 io->ci_layout_version = obj->lo_lsm->lsm_layout_gen;
351 /* find the corresponding mirror for designated mirror IO */
352 if (io->ci_designated_mirror > 0) {
353 struct lov_mirror_entry *entry;
355 LASSERT(!io->ci_ndelay);
357 CDEBUG(D_LAYOUT, "designated I/O mirror state: %d\n",
360 if ((cl_io_is_trunc(io) || io->ci_type == CIT_WRITE ||
361 cl_io_is_fallocate(io)) &&
362 (io->ci_layout_version != obj->lo_lsm->lsm_layout_gen)) {
364 * For resync I/O, the ci_layout_version was the layout
365 * version when resync starts. If it doesn't match the
366 * current object layout version, it means the layout
372 io->ci_layout_version |= LU_LAYOUT_RESYNC;
375 lio->lis_mirror_index = -1;
376 lov_foreach_mirror_entry(obj, entry) {
377 if (entry->lre_mirror_id ==
378 io->ci_designated_mirror) {
379 lio->lis_mirror_index = index;
386 RETURN(lio->lis_mirror_index < 0 ? -EINVAL : 0);
389 result = lov_io_mirror_write_intent(lio, obj, io);
393 if (io->ci_need_write_intent) {
394 CDEBUG(D_VFSTRACE, DFID " need write intent for [%llu, %llu)\n",
395 PFID(lu_object_fid(lov2lu(obj))),
396 lio->lis_pos, lio->lis_endpos);
398 if (cl_io_is_trunc(io)) {
400 * for truncate, we uses [size, EOF) to judge whether
401 * a write intent needs to be send, but we need to
402 * restore the write extent to [0, size], in truncate,
403 * the byte in the size position is accessed.
405 io->ci_write_intent.e_start = 0;
406 io->ci_write_intent.e_end =
407 io->u.ci_setattr.sa_attr.lvb_size + 1;
409 /* stop cl_io_init() loop */
413 if (io->ci_ndelay_tried == 0 || /* first time to try */
414 /* reset the mirror index if layout has changed */
415 lio->lis_mirror_layout_gen != obj->lo_lsm->lsm_layout_gen) {
416 lio->lis_mirror_layout_gen = obj->lo_lsm->lsm_layout_gen;
417 index = lio->lis_mirror_index = comp->lo_preferred_mirror;
419 index = lio->lis_mirror_index;
422 /* move mirror index to the next one */
423 index = (index + 1) % comp->lo_mirror_count;
426 for (i = 0; i < comp->lo_mirror_count; i++) {
427 struct lu_extent ext = { .e_start = lio->lis_pos,
428 .e_end = lio->lis_pos + 1 };
429 struct lov_mirror_entry *lre;
430 struct lov_layout_entry *lle;
433 lre = lov_mirror_entry(obj, (index + i) % comp->lo_mirror_count);
437 if (lre->lre_foreign)
440 lov_foreach_mirror_layout_entry(obj, lle, lre) {
444 if (lu_extent_is_overlapped(&ext, lle->lle_extent)) {
448 } /* each component of the mirror */
450 index = (index + i) % comp->lo_mirror_count;
455 if (i == comp->lo_mirror_count) {
456 CERROR(DFID": failed to find a component covering "
457 "I/O region at %llu\n",
458 PFID(lu_object_fid(lov2lu(obj))), lio->lis_pos);
460 dump_lsm(D_ERROR, obj->lo_lsm);
465 CDEBUG(D_VFSTRACE, DFID ": flr state: %d, move mirror from %d to %d, "
466 "have retried: %d, mirror count: %d\n",
467 PFID(lu_object_fid(lov2lu(obj))), lov_flr_state(obj),
468 lio->lis_mirror_index, index, io->ci_ndelay_tried,
469 comp->lo_mirror_count);
471 lio->lis_mirror_index = index;
474 * FLR: if all mirrors have been tried once, most likely the network
475 * of this client has been partitioned. We should relinquish CPU for
476 * a while before trying again.
478 if (io->ci_ndelay && io->ci_ndelay_tried > 0 &&
479 (io->ci_ndelay_tried % comp->lo_mirror_count == 0)) {
480 schedule_timeout_interruptible(cfs_time_seconds(1) / 100);
481 if (signal_pending(current))
485 * we'd set ci_tried_all_mirrors to turn off fast mirror
486 * switching for read after we've tried all mirrors several
489 io->ci_tried_all_mirrors = io->ci_ndelay_tried %
490 (comp->lo_mirror_count * 4) == 0;
492 ++io->ci_ndelay_tried;
494 CDEBUG(D_VFSTRACE, "use %sdelayed RPC state for this IO\n",
495 io->ci_ndelay ? "non-" : "");
500 static int lov_io_slice_init(struct lov_io *lio,
501 struct lov_object *obj, struct cl_io *io)
509 lio->lis_object = obj;
510 lio->lis_cached_entry = LIS_CACHE_ENTRY_NONE;
512 rdonly = lsm_is_rdonly(obj->lo_lsm);
513 switch (io->ci_type) {
516 if (io->ci_type == CIT_WRITE && rdonly) {
517 io->ci_need_pccro_clear = 1;
518 GOTO(out, result = 1);
520 lio->lis_pos = io->u.ci_rw.crw_pos;
521 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_bytes;
522 lio->lis_io_endpos = lio->lis_endpos;
523 if (cl_io_is_append(io)) {
524 LASSERT(io->ci_type == CIT_WRITE);
527 * If there is LOV EA hole, then we may cannot locate
528 * the current file-tail exactly.
530 if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
532 GOTO(out, result = -EIO);
535 lio->lis_endpos = OBD_OBJECT_EOF;
540 if (cl_io_is_fallocate(io)) {
542 io->ci_need_pccro_clear = 1;
543 GOTO(out, result = 1);
545 lio->lis_pos = io->u.ci_setattr.sa_falloc_offset;
546 lio->lis_endpos = io->u.ci_setattr.sa_falloc_end;
547 } else if (cl_io_is_trunc(io)) {
549 io->ci_need_pccro_clear = 1;
550 GOTO(out, result = 1);
552 lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
553 lio->lis_endpos = OBD_OBJECT_EOF;
556 lio->lis_endpos = OBD_OBJECT_EOF;
560 case CIT_DATA_VERSION:
562 lio->lis_endpos = OBD_OBJECT_EOF;
566 pgoff_t index = io->u.ci_fault.ft_index;
568 if (cl_io_is_mkwrite(io) && rdonly) {
569 io->ci_need_pccro_clear = 1;
570 GOTO(out, result = -ENODATA);
573 lio->lis_pos = index << PAGE_SHIFT;
574 lio->lis_endpos = (index + 1) << PAGE_SHIFT;
579 lio->lis_pos = io->u.ci_fsync.fi_start;
580 lio->lis_endpos = io->u.ci_fsync.fi_end;
585 lio->lis_pos = io->u.ci_ladvise.lio_start;
586 lio->lis_endpos = io->u.ci_ladvise.lio_end;
591 lio->lis_pos = io->u.ci_lseek.ls_start;
592 lio->lis_endpos = OBD_OBJECT_EOF;
598 lio->lis_endpos = OBD_OBJECT_EOF;
603 lio->lis_endpos = OBD_OBJECT_EOF;
611 * CIT_MISC + ci_ignore_layout can identify the I/O from the OSC layer,
612 * it won't care/access lov layout related info.
614 if (io->ci_ignore_layout && io->ci_type == CIT_MISC)
615 GOTO(out, result = 0);
617 LASSERT(obj->lo_lsm != NULL);
619 result = lov_io_mirror_init(lio, obj, io);
623 /* check if it needs to instantiate layout */
624 if (!(io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io) ||
625 cl_io_is_fallocate(io) ||
626 (cl_io_is_trunc(io) && io->u.ci_setattr.sa_attr.lvb_size > 0)))
627 GOTO(out, result = 0);
630 * for truncate, it only needs to instantiate the components
631 * before the truncated size.
633 if (cl_io_is_trunc(io)) {
634 io->ci_write_intent.e_start = 0;
635 /* for writes, e_end is endpos, the location of the file
636 * pointer after the write is completed, so it is not accessed.
637 * For truncate, 'end' is the size, and *is* acccessed.
638 * In other words, writes are [start, end), but truncate is
639 * [start, size], where both are included. So add 1 to the
640 * size when creating the write intent to account for this.
642 io->ci_write_intent.e_end =
643 io->u.ci_setattr.sa_attr.lvb_size + 1;
645 io->ci_write_intent.e_start = lio->lis_pos;
646 io->ci_write_intent.e_end = lio->lis_endpos;
650 lov_foreach_io_layout(index, lio, &io->ci_write_intent) {
651 if (!lsm_entry_inited(obj->lo_lsm, index)) {
652 io->ci_need_write_intent = 1;
657 if (io->ci_need_write_intent && io->ci_designated_mirror > 0) {
659 * REINT_SYNC RPC has already tried to instantiate all of the
660 * components involved, obviously it didn't succeed. Skip this
661 * mirror for now. The server won't be able to figure out
662 * which mirror it should instantiate components
664 CERROR(DFID": trying to instantiate components for designated "
665 "I/O, file state: %d\n",
666 PFID(lu_object_fid(lov2lu(obj))), lov_flr_state(obj));
668 io->ci_need_write_intent = 0;
669 GOTO(out, result = -EIO);
672 if (io->ci_need_write_intent)
673 GOTO(out, result = 1);
681 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
683 struct lov_io *lio = cl2lov_io(env, ios);
684 struct lov_object *lov = cl2lov(ios->cis_obj);
685 struct lov_io_sub *sub;
686 struct cl_io *io = lio->lis_cl.cis_io;
689 LASSERT(list_empty(&lio->lis_active));
691 while ((sub = list_first_entry_or_null(&lio->lis_subios,
693 sub_list)) != NULL) {
694 list_del_init(&sub->sub_list);
695 lio->lis_nr_subios--;
697 lov_io_sub_fini(env, lio, sub);
698 lov_sub_free(lio, sub);
700 LASSERT(lio->lis_nr_subios == 0);
702 if (!(io->ci_ignore_layout && io->ci_type == CIT_MISC)) {
703 LASSERT(atomic_read(&lov->lo_active_ios) > 0);
704 if (atomic_dec_and_test(&lov->lo_active_ios))
705 wake_up(&lov->lo_waitq);
710 static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
711 loff_t start, loff_t end)
713 struct cl_io *io = &sub->sub_io;
714 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
715 struct cl_io *parent = lio->lis_cl.cis_io;
716 int index = lov_comp_entry(sub->sub_subio_index);
717 int stripe = lov_comp_stripe(sub->sub_subio_index);
719 switch (io->ci_type) {
721 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
722 io->u.ci_setattr.sa_attr_flags =
723 parent->u.ci_setattr.sa_attr_flags;
724 io->u.ci_setattr.sa_avalid = parent->u.ci_setattr.sa_avalid;
725 io->u.ci_setattr.sa_xvalid = parent->u.ci_setattr.sa_xvalid;
726 io->u.ci_setattr.sa_falloc_mode =
727 parent->u.ci_setattr.sa_falloc_mode;
728 io->u.ci_setattr.sa_stripe_index = stripe;
729 io->u.ci_setattr.sa_parent_fid =
730 parent->u.ci_setattr.sa_parent_fid;
731 /* For SETATTR(fallocate) pass the subtype to lower IO */
732 io->u.ci_setattr.sa_subtype = parent->u.ci_setattr.sa_subtype;
733 if (cl_io_is_fallocate(io)) {
734 io->u.ci_setattr.sa_falloc_offset = start;
735 io->u.ci_setattr.sa_falloc_end = end;
736 io->u.ci_setattr.sa_falloc_uid =
737 parent->u.ci_setattr.sa_falloc_uid;
738 io->u.ci_setattr.sa_falloc_gid =
739 parent->u.ci_setattr.sa_falloc_gid;
740 io->u.ci_setattr.sa_falloc_projid =
741 parent->u.ci_setattr.sa_falloc_projid;
743 if (cl_io_is_trunc(io)) {
744 loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
746 new_size = lov_size_to_stripe(lsm, index, new_size,
748 io->u.ci_setattr.sa_attr.lvb_size = new_size;
750 lov_lsm2layout(lsm, lsm->lsm_entries[index],
751 &io->u.ci_setattr.sa_layout);
754 case CIT_DATA_VERSION: {
755 io->u.ci_data_version.dv_data_version = 0;
756 io->u.ci_data_version.dv_flags =
757 parent->u.ci_data_version.dv_flags;
761 loff_t off = parent->u.ci_fault.ft_index << PAGE_SHIFT;
763 io->u.ci_fault = parent->u.ci_fault;
764 off = lov_size_to_stripe(lsm, index, off, stripe);
765 io->u.ci_fault.ft_index = off >> PAGE_SHIFT;
769 io->u.ci_fsync.fi_start = start;
770 io->u.ci_fsync.fi_end = end;
771 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
772 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
777 io->u.ci_wr.wr_sync = cl_io_is_sync_write(parent);
778 io->ci_tried_all_mirrors = parent->ci_tried_all_mirrors;
779 if (cl_io_is_append(parent)) {
780 io->u.ci_wr.wr_append = 1;
782 io->u.ci_rw.crw_pos = start;
783 io->u.ci_rw.crw_bytes = end - start;
788 io->u.ci_ladvise.lio_start = start;
789 io->u.ci_ladvise.lio_end = end;
790 io->u.ci_ladvise.lio_fid = parent->u.ci_ladvise.lio_fid;
791 io->u.ci_ladvise.lio_advice = parent->u.ci_ladvise.lio_advice;
792 io->u.ci_ladvise.lio_flags = parent->u.ci_ladvise.lio_flags;
796 io->u.ci_lseek.ls_start = start;
797 io->u.ci_lseek.ls_whence = parent->u.ci_lseek.ls_whence;
798 io->u.ci_lseek.ls_result = parent->u.ci_lseek.ls_result;
808 static loff_t lov_offset_mod(loff_t val, int delta)
810 if (val != OBD_OBJECT_EOF)
815 static int lov_io_add_sub(const struct lu_env *env, struct lov_io *lio,
816 struct lov_io_sub *sub, u64 start, u64 end)
820 end = lov_offset_mod(end, 1);
821 lov_io_sub_inherit(sub, lio, start, end);
822 rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
824 cl_io_iter_fini(sub->sub_env, &sub->sub_io);
828 list_add_tail(&sub->sub_linkage, &lio->lis_active);
832 static int lov_io_iter_init(const struct lu_env *env,
833 const struct cl_io_slice *ios)
835 struct lov_io *lio = cl2lov_io(env, ios);
836 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
837 bool is_trunc = cl_io_is_trunc(ios->cis_io);
838 struct lov_io_sub *sub;
839 struct lu_extent ext;
845 ext.e_start = lio->lis_pos;
846 ext.e_end = lio->lis_endpos;
849 OBD_ALLOC_PTR_ARRAY(lio->lis_trunc_stripe_index,
850 lio->lis_object->u.composite.lo_entry_count);
851 if (lio->lis_trunc_stripe_index == NULL)
855 lov_foreach_io_layout(index, lio, &ext) {
856 struct lov_layout_entry *le = lov_entry(lio->lis_object, index);
857 struct lov_layout_raid0 *r0 = &le->lle_raid0;
861 bool tested_trunc_stripe = false;
864 lio->lis_trunc_stripe_index[index] = -1;
866 CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
867 index, lsm->lsm_entries[index]->lsme_flags);
868 if (!lsm_entry_inited(lsm, index)) {
870 * Read from uninitialized components should return
876 if (lsm_entry_is_foreign(lsm, index))
879 if (!le->lle_valid && !ios->cis_io->ci_designated_mirror) {
880 CERROR("I/O to invalid component: %d, mirror: %d\n",
881 index, lio->lis_mirror_index);
885 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
886 if (!lov_stripe_intersects(lsm, index, stripe,
890 if (unlikely(!r0->lo_sub[stripe])) {
891 if (ios->cis_io->ci_type == CIT_READ ||
892 ios->cis_io->ci_type == CIT_WRITE ||
893 ios->cis_io->ci_type == CIT_FAULT)
899 if (is_trunc && !tested_trunc_stripe) {
903 prev = (stripe == 0) ? r0->lo_nr - 1 :
906 * Only involving previous stripe if the
907 * truncate in this component is at the
908 * beginning of this stripe.
910 tested_trunc_stripe = true;
911 if (ext.e_start < lsm->lsm_entries[index]->
912 lsme_extent.e_start) {
913 /* need previous stripe involvement */
914 lio->lis_trunc_stripe_index[index] = prev;
916 div64_u64_rem(ext.e_start,
917 stripe_width(lsm, index),
919 /* tr_start %= stripe_swidth */
920 if (tr_start == stripe * lsm->
923 lio->lis_trunc_stripe_index[index] = prev;
927 /* if the last stripe is the trunc stripeno */
929 lio->lis_trunc_stripe_index[index] == stripe)
930 lio->lis_trunc_stripe_index[index] = -1;
932 sub = lov_sub_get(env, lio,
933 lov_comp_index(index, stripe));
937 rc = lov_io_add_sub(env, lio, sub, start, end);
944 if (is_trunc && lio->lis_trunc_stripe_index[index] != -1) {
945 stripe = lio->lis_trunc_stripe_index[index];
946 if (unlikely(!r0->lo_sub[stripe])) {
947 lio->lis_trunc_stripe_index[index] = -1;
950 sub = lov_sub_get(env, lio,
951 lov_comp_index(index, stripe));
956 * the prev sub could be used by another truncate, we'd
957 * skip it. LU-14128 happends when expand truncate +
958 * read get wrong kms.
960 if (!list_empty(&sub->sub_linkage)) {
961 lio->lis_trunc_stripe_index[index] = -1;
965 (void)lov_stripe_intersects(lsm, index, stripe, &ext,
967 rc = lov_io_add_sub(env, lio, sub, start, end);
976 static int lov_io_rw_iter_init(const struct lu_env *env,
977 const struct cl_io_slice *ios)
979 struct lov_io *lio = cl2lov_io(env, ios);
980 struct cl_io *io = ios->cis_io;
981 struct lov_stripe_md_entry *lse;
982 loff_t start = io->u.ci_rw.crw_pos;
986 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
989 if (cl_io_is_append(io))
990 RETURN(lov_io_iter_init(env, ios));
992 index = lov_io_layout_at(lio, io->u.ci_rw.crw_pos);
993 if (index < 0) { /* non-existing layout component */
994 if (io->ci_type == CIT_READ) {
996 * TODO: it needs to detect the next component and
997 * then set the next pos
1001 RETURN(lov_io_iter_init(env, ios));
1007 if (!lov_entry(lio->lis_object, index)->lle_valid &&
1008 !io->ci_designated_mirror)
1009 RETURN(io->ci_type == CIT_READ ? -EAGAIN : -EIO);
1011 lse = lov_lse(lio->lis_object, index);
1013 if (lsme_is_foreign(lse))
1016 next = MAX_LFS_FILESIZE;
1017 if (lse->lsme_stripe_count > 1) {
1018 unsigned long ssize = lse->lsme_stripe_size;
1020 start = div64_u64(start, ssize);
1021 next = (start + 1) * ssize;
1022 if (next <= start * ssize)
1023 next = MAX_LFS_FILESIZE;
1026 LASSERTF(io->u.ci_rw.crw_pos >= lse->lsme_extent.e_start,
1027 "pos %lld, [%lld, %lld)\n", io->u.ci_rw.crw_pos,
1028 lse->lsme_extent.e_start, lse->lsme_extent.e_end);
1029 next = min_t(__u64, next, lse->lsme_extent.e_end);
1030 next = min_t(loff_t, next, lio->lis_io_endpos);
1032 io->ci_continue = next < lio->lis_io_endpos;
1033 io->u.ci_rw.crw_bytes = next - io->u.ci_rw.crw_pos;
1034 lio->lis_pos = io->u.ci_rw.crw_pos;
1035 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_bytes;
1037 "stripe: %llu chunk: [%llu, %llu) %llu, %zd\n",
1038 (__u64)start, lio->lis_pos, lio->lis_endpos,
1039 (__u64)lio->lis_io_endpos, io->u.ci_rw.crw_bytes);
1042 * XXX The following call should be optimized: we know, that
1043 * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
1045 RETURN(lov_io_iter_init(env, ios));
1048 static int lov_io_setattr_iter_init(const struct lu_env *env,
1049 const struct cl_io_slice *ios)
1051 struct lov_io *lio = cl2lov_io(env, ios);
1052 struct cl_io *io = ios->cis_io;
1056 if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
1057 index = lov_io_layout_at(lio, lio->lis_pos - 1);
1058 /* no entry found for such offset */
1060 RETURN(io->ci_result = -ENODATA);
1063 RETURN(lov_io_iter_init(env, ios));
1066 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
1067 int (*iofunc)(const struct lu_env *, struct cl_io *))
1069 struct cl_io *parent = lio->lis_cl.cis_io;
1070 struct lov_io_sub *sub;
1074 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1075 rc = iofunc(sub->sub_env, &sub->sub_io);
1078 * fsync race with truncate, we'd continue to other
1079 * OST object's fsync to potentially discard
1080 * caching pages (osc_cache_writeback_range).
1082 if (rc == -ENOENT && parent->ci_type == CIT_FSYNC)
1087 if (parent->ci_result == 0)
1088 parent->ci_result = sub->sub_io.ci_result;
1093 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
1096 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
1099 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
1102 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
1105 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
1109 * It's possible that lov_io_start() wasn't called against this
1110 * sub-io, either because previous sub-io failed, or upper layer
1113 if (io->ci_state == CIS_IO_GOING)
1116 io->ci_state = CIS_IO_FINISHED;
1120 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
1122 cl_io_iter_fini(env, io);
1126 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
1128 cl_io_unlock(env, io);
1132 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
1136 /* Before ending each i/o, we must set lis_cached_entry to tell the
1137 * next i/o not to use stale cached lis information.
1139 cl2lov_io(env, ios)->lis_cached_entry = LIS_CACHE_ENTRY_NONE;
1141 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
1146 lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
1148 struct lov_io *lio = cl2lov_io(env, ios);
1149 struct cl_io *parent = lio->lis_cl.cis_io;
1150 struct cl_data_version_io *pdv = &parent->u.ci_data_version;
1151 struct lov_io_sub *sub;
1154 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1155 struct cl_data_version_io *sdv = &sub->sub_io.u.ci_data_version;
1157 lov_io_end_wrapper(sub->sub_env, &sub->sub_io);
1159 pdv->dv_data_version += sdv->dv_data_version;
1160 if (pdv->dv_layout_version > sdv->dv_layout_version)
1161 pdv->dv_layout_version = sdv->dv_layout_version;
1163 if (parent->ci_result == 0)
1164 parent->ci_result = sub->sub_io.ci_result;
1170 static void lov_io_iter_fini(const struct lu_env *env,
1171 const struct cl_io_slice *ios)
1173 struct lov_io *lio = cl2lov_io(env, ios);
1178 if (lio->lis_trunc_stripe_index != NULL)
1179 OBD_FREE_PTR_ARRAY(lio->lis_trunc_stripe_index,
1180 lio->lis_object->u.composite.lo_entry_count);
1181 lio->lis_trunc_stripe_index = NULL;
1183 rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
1185 while (!list_empty(&lio->lis_active))
1186 list_del_init(lio->lis_active.next);
1190 static void lov_io_unlock(const struct lu_env *env,
1191 const struct cl_io_slice *ios)
1196 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
1201 static int lov_io_read_ahead(const struct lu_env *env,
1202 const struct cl_io_slice *ios,
1203 pgoff_t start, struct cl_read_ahead *ra)
1205 struct lov_io *lio = cl2lov_io(env, ios);
1206 struct lov_object *loo = lio->lis_object;
1207 struct lov_layout_raid0 *r0;
1208 struct lov_io_sub *sub;
1212 unsigned int pps; /* pages per stripe */
1218 offset = start << PAGE_SHIFT;
1219 index = lov_io_layout_at(lio, offset);
1220 if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index) ||
1221 lsm_entry_is_foreign(loo->lo_lsm, index))
1224 /* avoid readahead to expand to stale components */
1225 if (!lov_entry(loo, index)->lle_valid)
1228 stripe = lov_stripe_number(loo->lo_lsm, index, offset);
1230 r0 = lov_r0(loo, index);
1231 if (unlikely(!r0->lo_sub[stripe]))
1234 sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
1236 RETURN(PTR_ERR(sub));
1238 lov_stripe_offset(loo->lo_lsm, index, offset, stripe, &suboff);
1239 rc = cl_io_read_ahead(sub->sub_env, &sub->sub_io,
1240 suboff >> PAGE_SHIFT, ra);
1242 CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
1243 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end_idx,
1249 * Adjust the stripe index by layout of comp. ra->cra_end is the
1250 * maximum page index covered by an underlying DLM lock.
1251 * This function converts cra_end from stripe level to file level, and
1252 * make sure it's not beyond stripe and component boundary.
1255 /* cra_end is stripe level, convert it into file level */
1256 ra_end = ra->cra_end_idx;
1257 if (ra_end != CL_PAGE_EOF)
1258 ra->cra_end_idx = lov_stripe_pgoff(loo->lo_lsm, index,
1261 /* boundary of current component */
1262 ra_end = lov_io_extent(lio, index)->e_end >> PAGE_SHIFT;
1263 if (ra_end != CL_PAGE_EOF && ra->cra_end_idx >= ra_end)
1264 ra->cra_end_idx = ra_end - 1;
1266 if (r0->lo_nr == 1) /* single stripe file */
1269 pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
1271 CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %d, "
1272 "stripe_size = %u, stripe no = %u, start index = %lu\n",
1273 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end_idx, pps, index,
1274 lov_lse(loo, index)->lsme_stripe_size, stripe, start);
1276 /* never exceed the end of the stripe */
1277 ra->cra_end_idx = min_t(pgoff_t, ra->cra_end_idx,
1278 start + pps - start % pps - 1);
1282 static int lov_io_lru_reserve(const struct lu_env *env,
1283 const struct cl_io_slice *ios, loff_t pos,
1286 struct lov_io *lio = cl2lov_io(env, ios);
1287 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
1288 struct lov_io_sub *sub;
1289 struct lu_extent ext;
1296 ext.e_end = pos + bytes;
1297 lov_foreach_io_layout(index, lio, &ext) {
1298 struct lov_layout_entry *le = lov_entry(lio->lis_object, index);
1299 struct lov_layout_raid0 *r0 = &le->lle_raid0;
1304 if (!lsm_entry_inited(lsm, index))
1307 if (!le->lle_valid && !ios->cis_io->ci_designated_mirror) {
1308 CERROR(DFID": I/O to invalid component: %d, mirror: %d\n",
1309 PFID(lu_object_fid(lov2lu(lio->lis_object))),
1310 index, lio->lis_mirror_index);
1314 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
1315 if (!lov_stripe_intersects(lsm, index, stripe,
1316 &ext, &start, &end))
1319 if (unlikely(!r0->lo_sub[stripe]))
1322 sub = lov_sub_get(env, lio,
1323 lov_comp_index(index, stripe));
1325 return PTR_ERR(sub);
1327 rc = cl_io_lru_reserve(sub->sub_env, &sub->sub_io, start,
1338 * lov implementation of cl_operations::cio_submit() method. It takes a list
1339 * of pages in \a queue, splits it into per-stripe sub-lists, invokes
1340 * cl_io_submit() on underlying devices to submit sub-lists, and then splices
1343 * Major complication of this function is a need to handle memory cleansing:
1344 * cl_io_submit() is called to write out pages as a part of VM memory
1345 * reclamation, and hence it may not fail due to memory shortages (system
1346 * dead-locks otherwise). To deal with this, some resources (sub-lists,
1347 * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
1348 * not-memory cleansing context), and in case of memory shortage, these
1349 * pre-allocated resources are used by lov_io_submit() under
1350 * lov_device::ld_mutex mutex.
1352 static int lov_io_submit(const struct lu_env *env,
1354 const struct cl_io_slice *ios,
1355 enum cl_req_type crt, struct cl_2queue *queue)
1357 struct cl_page_list *qin = &queue->c2_qin;
1358 struct lov_io *lio = cl2lov_io(env, ios);
1359 struct lov_io_sub *sub;
1360 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
1361 struct cl_page *page = cl_page_list_first(qin);
1362 struct cl_page *tmp;
1368 if (page->cp_type == CPT_TRANSIENT)
1371 cl_page_list_init(plist);
1372 while (qin->pl_nr > 0) {
1373 struct cl_2queue *cl2q = &lov_env_info(env)->lti_cl2q;
1375 page = cl_page_list_first(qin);
1376 if (lov_page_is_empty(page)) {
1377 cl_page_list_move(&queue->c2_qout, qin, page);
1380 * it could only be mirror read to get here therefore
1381 * the pages will be transient. We don't care about
1382 * the return code of cl_page_prep() at all.
1384 (void) cl_page_prep(env, ios->cis_io, page, crt);
1385 cl_page_completion(env, page, crt, 0);
1389 cl_2queue_init(cl2q);
1390 cl_page_list_move(&cl2q->c2_qin, qin, page);
1392 index = page->cp_lov_index;
1393 /* DIO is already split by stripe */
1395 cl_page_list_for_each_safe(page, tmp, qin) {
1396 /* this page is not on this stripe */
1397 if (index != page->cp_lov_index)
1400 cl_page_list_move(&cl2q->c2_qin, qin, page);
1403 cl_page_list_splice(qin, &cl2q->c2_qin);
1406 sub = lov_sub_get(env, lio, index);
1408 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
1414 cl_page_list_splice(&cl2q->c2_qin, plist);
1415 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
1416 cl_2queue_fini(env, cl2q);
1422 cl_page_list_splice(plist, qin);
1423 cl_page_list_fini(env, plist);
1428 static int lov_io_commit_async(const struct lu_env *env,
1429 const struct cl_io_slice *ios,
1430 struct cl_page_list *queue, int from, int to,
1433 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
1434 struct lov_io *lio = cl2lov_io(env, ios);
1435 struct lov_io_sub *sub;
1436 struct cl_page *page;
1440 if (lio->lis_nr_subios == 1) {
1441 int idx = lio->lis_single_subio_index;
1443 LASSERT(!lov_page_is_empty(cl_page_list_first(queue)));
1445 sub = lov_sub_get(env, lio, idx);
1446 LASSERT(!IS_ERR(sub));
1447 LASSERT(sub == &lio->lis_single_subio);
1448 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io, queue,
1453 cl_page_list_init(plist);
1454 while (queue->pl_nr > 0) {
1458 LASSERT(plist->pl_nr == 0);
1459 page = cl_page_list_first(queue);
1460 LASSERT(!lov_page_is_empty(page));
1462 cl_page_list_move(plist, queue, page);
1464 index = page->cp_lov_index;
1465 while (queue->pl_nr > 0) {
1466 page = cl_page_list_first(queue);
1467 if (index != page->cp_lov_index)
1470 cl_page_list_move(plist, queue, page);
1473 if (queue->pl_nr > 0) /* still has more pages */
1474 stripe_to = PAGE_SIZE;
1476 sub = lov_sub_get(env, lio, index);
1478 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io,
1479 plist, from, stripe_to, cb);
1485 if (plist->pl_nr > 0) /* short write */
1490 if (lov_comp_entry(index) !=
1491 lov_comp_entry(page->cp_lov_index))
1492 cl_io_extent_release(sub->sub_env, &sub->sub_io);
1495 /* for error case, add the page back into the qin list */
1496 LASSERT(ergo(rc == 0, plist->pl_nr == 0));
1497 while (plist->pl_nr > 0) {
1498 /* error occurred, add the uncommitted pages back into queue */
1499 page = cl_page_list_last(plist);
1500 cl_page_list_move_head(queue, plist, page);
1506 static int lov_io_fault_start(const struct lu_env *env,
1507 const struct cl_io_slice *ios)
1509 struct cl_fault_io *fio;
1511 struct lov_io_sub *sub;
1518 fio = &ios->cis_io->u.ci_fault;
1519 lio = cl2lov_io(env, ios);
1522 * LU-14502: ft_page could be an existing cl_page associated with
1523 * the vmpage covering the fault index, and the page may still
1524 * refer to another mirror of an old IO.
1526 if (lov_is_flr(lio->lis_object)) {
1527 offset = fio->ft_index << PAGE_SHIFT;;
1528 entry = lov_io_layout_at(lio, offset);
1530 CERROR(DFID": page fault index %lu invalid component: "
1532 PFID(lu_object_fid(&ios->cis_obj->co_lu)),
1533 fio->ft_index, entry,
1534 lio->lis_mirror_index);
1537 stripe = lov_stripe_number(lio->lis_object->lo_lsm,
1540 if (fio->ft_page->cp_lov_index !=
1541 lov_comp_index(entry, stripe)) {
1542 CDEBUG(D_INFO, DFID": page fault at index %lu, "
1543 "at mirror %u comp entry %u stripe %u, "
1544 "been used with comp entry %u stripe %u\n",
1545 PFID(lu_object_fid(&ios->cis_obj->co_lu)),
1546 fio->ft_index, lio->lis_mirror_index,
1548 lov_comp_entry(fio->ft_page->cp_lov_index),
1549 lov_comp_stripe(fio->ft_page->cp_lov_index));
1551 fio->ft_page->cp_lov_index =
1552 lov_comp_index(entry, stripe);
1556 sub = lov_sub_get(env, lio, fio->ft_page->cp_lov_index);
1557 sub->sub_io.u.ci_fault.ft_bytes = fio->ft_bytes;
1559 RETURN(lov_io_start(env, ios));
1562 static int lov_io_setattr_start(const struct lu_env *env,
1563 const struct cl_io_slice *ios)
1565 struct lov_io *lio = cl2lov_io(env, ios);
1566 struct cl_io *parent = ios->cis_io;
1567 struct lov_io_sub *sub;
1568 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
1572 if (cl_io_is_fallocate(parent)) {
1573 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1574 loff_t size = parent->u.ci_setattr.sa_attr.lvb_size;
1575 int index = lov_comp_entry(sub->sub_subio_index);
1576 int stripe = lov_comp_stripe(sub->sub_subio_index);
1578 size = lov_size_to_stripe(lsm, index, size, stripe);
1579 sub->sub_io.u.ci_setattr.sa_attr.lvb_size = size;
1580 sub->sub_io.u.ci_setattr.sa_avalid =
1581 parent->u.ci_setattr.sa_avalid;
1585 RETURN(lov_io_start(env, ios));
1588 static void lov_io_fsync_end(const struct lu_env *env,
1589 const struct cl_io_slice *ios)
1591 struct lov_io *lio = cl2lov_io(env, ios);
1592 struct lov_io_sub *sub;
1593 unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
1597 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1598 struct cl_io *subio = &sub->sub_io;
1600 lov_io_end_wrapper(sub->sub_env, subio);
1602 if (subio->ci_result == 0)
1603 *written += subio->u.ci_fsync.fi_nr_written;
1608 static void lov_io_lseek_end(const struct lu_env *env,
1609 const struct cl_io_slice *ios)
1611 struct lov_io *lio = cl2lov_io(env, ios);
1612 struct cl_io *io = lio->lis_cl.cis_io;
1613 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
1614 struct lov_io_sub *sub;
1615 loff_t offset = -ENXIO;
1617 bool seek_hole = io->u.ci_lseek.ls_whence == SEEK_HOLE;
1621 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1622 struct cl_io *subio = &sub->sub_io;
1623 int index = lov_comp_entry(sub->sub_subio_index);
1624 int stripe = lov_comp_stripe(sub->sub_subio_index);
1625 loff_t sub_off, lov_off;
1626 __u64 comp_end = lsm->lsm_entries[index]->lsme_extent.e_end;
1628 lov_io_end_wrapper(sub->sub_env, subio);
1630 if (io->ci_result == 0)
1631 io->ci_result = sub->sub_io.ci_result;
1636 CDEBUG(D_INFO, DFID": entry %x stripe %u: SEEK_%s from %lld\n",
1637 PFID(lu_object_fid(lov2lu(lio->lis_object))),
1638 index, stripe, seek_hole ? "HOLE" : "DATA",
1639 subio->u.ci_lseek.ls_start);
1641 /* first subio with positive result is what we need */
1642 sub_off = subio->u.ci_lseek.ls_result;
1643 /* Expected error, offset is out of stripe file size */
1644 if (sub_off == -ENXIO)
1646 /* Any other errors are not expected with ci_result == 0 */
1648 CDEBUG(D_INFO, "unexpected error: rc = %lld\n",
1650 io->ci_result = sub_off;
1653 lov_off = lov_stripe_size(lsm, index, sub_off + 1, stripe) - 1;
1655 /* the only way to get negatove lov_off here is too big
1656 * result. Return -EOVERFLOW then.
1658 io->ci_result = -EOVERFLOW;
1659 CDEBUG(D_INFO, "offset %llu is too big: rc = %d\n",
1660 (u64)lov_off, io->ci_result);
1663 if (lov_off < io->u.ci_lseek.ls_start) {
1664 io->ci_result = -EINVAL;
1665 CDEBUG(D_INFO, "offset %lld < start %lld: rc = %d\n",
1666 sub_off, io->u.ci_lseek.ls_start, io->ci_result);
1669 /* resulting offset can be out of component range if stripe
1670 * object is full and its file size was returned as virtual
1671 * hole start. Skip this result, the next component will give
1672 * us correct lseek result but keep possible hole offset in
1673 * case there is no more components ahead
1675 if (lov_off >= comp_end) {
1676 /* must be SEEK_HOLE case */
1677 if (likely(seek_hole)) {
1678 /* save comp end as potential hole offset */
1679 hole_off = max_t(__u64, comp_end, hole_off);
1681 io->ci_result = -EINVAL;
1683 "off %lld >= comp_end %llu: rc = %d\n",
1684 lov_off, comp_end, io->ci_result);
1689 CDEBUG(D_INFO, "SEEK_%s: %lld->%lld/%lld: rc = %d\n",
1690 seek_hole ? "HOLE" : "DATA",
1691 subio->u.ci_lseek.ls_start, sub_off, lov_off,
1692 sub->sub_io.ci_result);
1693 offset = min_t(__u64, offset, lov_off);
1695 /* no result but some component returns hole as component end */
1696 if (seek_hole && offset == -ENXIO && hole_off > 0)
1699 io->u.ci_lseek.ls_result = offset;
1703 static const struct cl_io_operations lov_io_ops = {
1706 .cio_fini = lov_io_fini,
1707 .cio_iter_init = lov_io_rw_iter_init,
1708 .cio_iter_fini = lov_io_iter_fini,
1709 .cio_lock = lov_io_lock,
1710 .cio_unlock = lov_io_unlock,
1711 .cio_start = lov_io_start,
1712 .cio_end = lov_io_end
1715 .cio_fini = lov_io_fini,
1716 .cio_iter_init = lov_io_rw_iter_init,
1717 .cio_iter_fini = lov_io_iter_fini,
1718 .cio_lock = lov_io_lock,
1719 .cio_unlock = lov_io_unlock,
1720 .cio_start = lov_io_start,
1721 .cio_end = lov_io_end
1724 .cio_fini = lov_io_fini,
1725 .cio_iter_init = lov_io_setattr_iter_init,
1726 .cio_iter_fini = lov_io_iter_fini,
1727 .cio_lock = lov_io_lock,
1728 .cio_unlock = lov_io_unlock,
1729 .cio_start = lov_io_setattr_start,
1730 .cio_end = lov_io_end
1732 [CIT_DATA_VERSION] = {
1733 .cio_fini = lov_io_fini,
1734 .cio_iter_init = lov_io_iter_init,
1735 .cio_iter_fini = lov_io_iter_fini,
1736 .cio_lock = lov_io_lock,
1737 .cio_unlock = lov_io_unlock,
1738 .cio_start = lov_io_start,
1739 .cio_end = lov_io_data_version_end,
1742 .cio_fini = lov_io_fini,
1743 .cio_iter_init = lov_io_iter_init,
1744 .cio_iter_fini = lov_io_iter_fini,
1745 .cio_lock = lov_io_lock,
1746 .cio_unlock = lov_io_unlock,
1747 .cio_start = lov_io_fault_start,
1748 .cio_end = lov_io_end
1751 .cio_fini = lov_io_fini,
1752 .cio_iter_init = lov_io_iter_init,
1753 .cio_iter_fini = lov_io_iter_fini,
1754 .cio_lock = lov_io_lock,
1755 .cio_unlock = lov_io_unlock,
1756 .cio_start = lov_io_start,
1757 .cio_end = lov_io_fsync_end
1760 .cio_fini = lov_io_fini,
1761 .cio_iter_init = lov_io_iter_init,
1762 .cio_iter_fini = lov_io_iter_fini,
1763 .cio_lock = lov_io_lock,
1764 .cio_unlock = lov_io_unlock,
1765 .cio_start = lov_io_start,
1766 .cio_end = lov_io_end
1769 .cio_fini = lov_io_fini,
1770 .cio_iter_init = lov_io_iter_init,
1771 .cio_iter_fini = lov_io_iter_fini,
1772 .cio_lock = lov_io_lock,
1773 .cio_unlock = lov_io_unlock,
1774 .cio_start = lov_io_start,
1775 .cio_end = lov_io_lseek_end
1778 .cio_fini = lov_io_fini,
1781 .cio_fini = lov_io_fini
1784 .cio_read_ahead = lov_io_read_ahead,
1785 .cio_lru_reserve = lov_io_lru_reserve,
1786 .cio_submit = lov_io_submit,
1787 .cio_commit_async = lov_io_commit_async,
1791 * Empty lov io operations.
1793 static void lov_empty_io_fini(const struct lu_env *env,
1794 const struct cl_io_slice *ios)
1796 struct lov_object *lov = cl2lov(ios->cis_obj);
1797 struct lov_io *lio = cl2lov_io(env, ios);
1798 struct cl_io *io = lio->lis_cl.cis_io;
1801 if (!(io->ci_type == CIT_MISC && io->ci_ignore_layout) &&
1802 atomic_dec_and_test(&lov->lo_active_ios))
1803 wake_up(&lov->lo_waitq);
1807 static int lov_empty_io_submit(const struct lu_env *env,
1809 const struct cl_io_slice *ios,
1810 enum cl_req_type crt, struct cl_2queue *queue)
1815 static void lov_empty_impossible(const struct lu_env *env,
1816 struct cl_io_slice *ios)
1821 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
1824 * An io operation vector for files without stripes.
1826 static const struct cl_io_operations lov_empty_io_ops = {
1829 .cio_fini = lov_empty_io_fini,
1831 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1832 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1833 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1834 .cio_end = LOV_EMPTY_IMPOSSIBLE
1838 .cio_fini = lov_empty_io_fini,
1839 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1840 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1841 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1842 .cio_end = LOV_EMPTY_IMPOSSIBLE
1845 .cio_fini = lov_empty_io_fini,
1846 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1847 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1848 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1849 .cio_end = LOV_EMPTY_IMPOSSIBLE
1852 .cio_fini = lov_empty_io_fini,
1853 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1854 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1855 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1856 .cio_end = LOV_EMPTY_IMPOSSIBLE
1859 .cio_fini = lov_empty_io_fini
1862 .cio_fini = lov_empty_io_fini
1865 .cio_fini = lov_empty_io_fini
1868 .cio_fini = lov_empty_io_fini
1871 .cio_submit = lov_empty_io_submit,
1872 .cio_commit_async = LOV_EMPTY_IMPOSSIBLE
1875 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
1878 struct lov_io *lio = lov_env_io(env);
1879 struct lov_object *lov = cl2lov(obj);
1884 INIT_LIST_HEAD(&lio->lis_active);
1885 result = lov_io_slice_init(lio, lov, io);
1889 result = lov_io_subio_init(env, lio, io);
1891 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
1892 if (!(io->ci_ignore_layout && io->ci_type == CIT_MISC))
1893 atomic_inc(&lov->lo_active_ios);
1897 io->ci_result = result < 0 ? result : 0;
1901 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
1904 struct lov_object *lov = cl2lov(obj);
1905 struct lov_io *lio = lov_env_io(env);
1909 lio->lis_object = lov;
1910 switch (io->ci_type) {
1922 case CIT_DATA_VERSION:
1930 CERROR("Page fault on a file without stripes: "DFID"\n",
1931 PFID(lu_object_fid(&obj->co_lu)));
1935 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1936 if (!(io->ci_ignore_layout && io->ci_type == CIT_MISC))
1937 atomic_inc(&lov->lo_active_ios);
1940 io->ci_result = result < 0 ? result : 0;
1944 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
1947 struct lov_object *lov = cl2lov(obj);
1948 struct lov_io *lio = lov_env_io(env);
1952 LASSERT(lov->lo_lsm != NULL);
1953 lio->lis_object = lov;
1955 switch (io->ci_type) {
1957 LASSERTF(0, "invalid type %d\n", io->ci_type);
1958 result = -EOPNOTSUPP;
1964 case CIT_DATA_VERSION:
1969 * the truncate to 0 is managed by MDT:
1970 * - in open, for open O_TRUNC
1971 * - in setattr, for truncate
1974 * the truncate is for size > 0 so triggers a restore,
1975 * also trigger a restore for prealloc/punch
1977 if (cl_io_is_trunc(io) || cl_io_is_fallocate(io)) {
1978 io->ci_restore_needed = 1;
1987 io->ci_restore_needed = 1;
1993 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1994 if (!(io->ci_ignore_layout && io->ci_type == CIT_MISC))
1995 atomic_inc(&lov->lo_active_ios);
1998 io->ci_result = result < 0 ? result : 0;
2003 * Return the index in composite:lo_entries by the file offset
2005 int lov_io_layout_at(struct lov_io *lio, __u64 offset)
2007 struct lov_object *lov = lio->lis_object;
2008 struct lov_layout_composite *comp = &lov->u.composite;
2009 int start_index = 0;
2010 int end_index = comp->lo_entry_count - 1;
2013 LASSERT(lov->lo_type == LLT_COMP);
2015 /* This is actual file offset so nothing can cover eof. */
2016 if (offset == LUSTRE_EOF)
2019 if (lov_is_flr(lov)) {
2020 struct lov_mirror_entry *lre;
2022 LASSERT(lio->lis_mirror_index >= 0);
2024 lre = lov_mirror_entry(lov, lio->lis_mirror_index);
2025 start_index = lre->lre_start;
2026 end_index = lre->lre_end;
2029 for (i = start_index; i <= end_index; i++) {
2030 struct lov_layout_entry *lle = lov_entry(lov, i);
2032 LASSERT(!lsme_is_foreign(lle->lle_lsme));
2034 if ((offset >= lle->lle_extent->e_start &&
2035 offset < lle->lle_extent->e_end) ||
2036 (offset == OBD_OBJECT_EOF &&
2037 lle->lle_extent->e_end == OBD_OBJECT_EOF))