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_layout_composite *comp = &lov->u.composite;
111 struct lov_mirror_entry *lre = &comp->lo_mirrors[mirror_index];
113 return (index >= lre->lre_start && index <= lre->lre_end);
116 static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
117 struct lov_io_sub *sub)
119 struct lov_object *lov = lio->lis_object;
120 struct cl_io *sub_io;
121 struct cl_object *sub_obj;
122 struct cl_io *io = lio->lis_cl.cis_io;
123 int index = lov_comp_entry(sub->sub_subio_index);
124 int stripe = lov_comp_stripe(sub->sub_subio_index);
126 LASSERT(sub->sub_env == NULL);
129 if (unlikely(!lov_r0(lov, index)->lo_sub ||
130 !lov_r0(lov, index)->lo_sub[stripe]))
133 LASSERTF(ergo(lov_is_flr(lov),
134 is_index_within_mirror(lov, index,
135 lio->lis_mirror_index)),
136 DFID "iot = %d, index = %d, mirror = %d\n",
137 PFID(lu_object_fid(lov2lu(lov))), io->ci_type, index,
138 lio->lis_mirror_index);
140 /* obtain new environment */
141 sub->sub_env = cl_env_get(&sub->sub_refcheck);
142 if (IS_ERR(sub->sub_env)) {
143 result = PTR_ERR(sub->sub_env);
147 sub_obj = lovsub2cl(lov_r0(lov, index)->lo_sub[stripe]);
148 sub_io = &sub->sub_io;
150 sub_io->ci_obj = sub_obj;
151 sub_io->ci_result = 0;
153 sub_io->ci_parent = io;
154 sub_io->ci_lockreq = io->ci_lockreq;
155 sub_io->ci_type = io->ci_type;
156 sub_io->ci_no_srvlock = io->ci_no_srvlock;
157 sub_io->ci_noatime = io->ci_noatime;
158 sub_io->ci_async_readahead = io->ci_async_readahead;
159 sub_io->ci_lock_no_expand = io->ci_lock_no_expand;
160 sub_io->ci_ndelay = io->ci_ndelay;
161 sub_io->ci_layout_version = io->ci_layout_version;
162 sub_io->ci_tried_all_mirrors = io->ci_tried_all_mirrors;
164 result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
167 lov_io_sub_fini(env, lio, sub);
172 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
173 struct lov_io *lio, int index)
175 struct lov_io_sub *sub;
180 list_for_each_entry(sub, &lio->lis_subios, sub_list) {
181 if (sub->sub_subio_index == index) {
188 sub = lov_sub_alloc(lio, index);
190 GOTO(out, rc = -ENOMEM);
192 rc = lov_io_sub_init(env, lio, sub);
194 lov_sub_free(lio, sub);
198 list_add_tail(&sub->sub_list, &lio->lis_subios);
199 lio->lis_nr_subios++;
205 sub->sub_io.ci_noquota = lio->lis_cl.cis_io->ci_noquota;
212 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
217 LASSERT(lio->lis_object != NULL);
219 INIT_LIST_HEAD(&lio->lis_subios);
220 lio->lis_single_subio_index = -1;
221 lio->lis_nr_subios = 0;
227 * Decide if it will need write intent RPC
229 static int lov_io_mirror_write_intent(struct lov_io *lio,
230 struct lov_object *obj, struct cl_io *io)
232 struct lu_object *lobj = lov2lu(obj);
233 struct lov_layout_composite *comp = &obj->u.composite;
234 struct lu_extent *ext = &io->ci_write_intent;
235 struct lov_mirror_entry *lre;
236 struct lov_mirror_entry *primary;
237 struct lov_layout_entry *lle;
241 *ext = (typeof(*ext)) { lio->lis_pos, lio->lis_endpos };
242 io->ci_need_write_intent = 0;
244 if (!(io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io) ||
245 cl_io_is_fallocate(io) || cl_io_is_trunc(io)))
249 * FLR: check if it needs to send a write intent RPC to server.
250 * Writing to sync_pending file needs write intent RPC to change
251 * the file state back to write_pending, so that the layout version
252 * can be increased when the state changes to sync_pending at a later
253 * time. Otherwise there exists a chance that an evicted client may
254 * dirty the file data while resync client is working on it.
255 * Designated I/O is allowed for resync workload.
257 if (lov_flr_state(obj) == LCM_FL_RDONLY ||
258 (lov_flr_state(obj) == LCM_FL_SYNC_PENDING &&
259 io->ci_designated_mirror == 0)) {
260 io->ci_need_write_intent = 1;
264 LASSERT((lov_flr_state(obj) == LCM_FL_WRITE_PENDING));
265 LASSERT(comp->lo_preferred_mirror >= 0);
268 * need to iterate all components to see if there are
269 * multiple components covering the writing component
271 primary = &comp->lo_mirrors[comp->lo_preferred_mirror];
272 if (primary->lre_stale || !primary->lre_valid) {
274 * new server could pick a primary mirror which old client
275 * does not recognize, and old client would mark it as
278 CERROR(DFID ": cannot find known valid non-stale mirror, "
279 "could be new server picked a mirror which this client "
280 "does not recognize.\n",
281 PFID(lu_object_fid(lobj)));
285 lov_foreach_mirror_layout_entry(obj, lle, primary) {
286 LASSERT(lle->lle_valid);
287 if (!lu_extent_is_overlapped(ext, lle->lle_extent))
290 ext->e_start = min(ext->e_start, lle->lle_extent->e_start);
291 ext->e_end = max(ext->e_end, lle->lle_extent->e_end);
295 CERROR(DFID ": cannot find any valid components covering "
296 "file extent "DEXT", mirror: %d\n",
297 PFID(lu_object_fid(lobj)), PEXT(ext),
298 primary->lre_mirror_id);
303 lov_foreach_mirror_entry(obj, lre) {
307 lov_foreach_mirror_layout_entry(obj, lle, lre) {
311 if (lu_extent_is_overlapped(ext, lle->lle_extent)) {
318 CDEBUG(D_VFSTRACE, DFID "there are %zd components to be staled to "
319 "modify file extent "DEXT", iot: %d\n",
320 PFID(lu_object_fid(lobj)), count, PEXT(ext), io->ci_type);
322 io->ci_need_write_intent = count > 0;
327 static int lov_io_mirror_init(struct lov_io *lio, struct lov_object *obj,
330 struct lov_layout_composite *comp = &obj->u.composite;
336 if (!lov_is_flr(obj)) {
337 /* only locks/pages are manipulated for CIT_MISC op, no
338 * cl_io_loop() will be called, don't check/set mirror info.
340 if (io->ci_type != CIT_MISC) {
341 LASSERT(comp->lo_preferred_mirror == 0);
342 lio->lis_mirror_index = comp->lo_preferred_mirror;
348 /* transfer the layout version for verification */
349 if (io->ci_layout_version == 0)
350 io->ci_layout_version = obj->lo_lsm->lsm_layout_gen;
352 /* find the corresponding mirror for designated mirror IO */
353 if (io->ci_designated_mirror > 0) {
354 struct lov_mirror_entry *entry;
356 LASSERT(!io->ci_ndelay);
358 CDEBUG(D_LAYOUT, "designated I/O mirror state: %d\n",
361 if ((cl_io_is_trunc(io) || io->ci_type == CIT_WRITE ||
362 cl_io_is_fallocate(io)) &&
363 (io->ci_layout_version != obj->lo_lsm->lsm_layout_gen)) {
365 * For resync I/O, the ci_layout_version was the layout
366 * version when resync starts. If it doesn't match the
367 * current object layout version, it means the layout
373 io->ci_layout_version |= LU_LAYOUT_RESYNC;
376 lio->lis_mirror_index = -1;
377 lov_foreach_mirror_entry(obj, entry) {
378 if (entry->lre_mirror_id ==
379 io->ci_designated_mirror) {
380 lio->lis_mirror_index = index;
387 RETURN(lio->lis_mirror_index < 0 ? -EINVAL : 0);
390 result = lov_io_mirror_write_intent(lio, obj, io);
394 if (io->ci_need_write_intent) {
395 CDEBUG(D_VFSTRACE, DFID " need write intent for [%llu, %llu)\n",
396 PFID(lu_object_fid(lov2lu(obj))),
397 lio->lis_pos, lio->lis_endpos);
399 if (cl_io_is_trunc(io)) {
401 * for truncate, we uses [size, EOF) to judge whether
402 * a write intent needs to be send, but we need to
403 * restore the write extent to [0, size], in truncate,
404 * the byte in the size position is accessed.
406 io->ci_write_intent.e_start = 0;
407 io->ci_write_intent.e_end =
408 io->u.ci_setattr.sa_attr.lvb_size + 1;
410 /* stop cl_io_init() loop */
414 if (io->ci_ndelay_tried == 0 || /* first time to try */
415 /* reset the mirror index if layout has changed */
416 lio->lis_mirror_layout_gen != obj->lo_lsm->lsm_layout_gen) {
417 lio->lis_mirror_layout_gen = obj->lo_lsm->lsm_layout_gen;
418 index = lio->lis_mirror_index = comp->lo_preferred_mirror;
420 index = lio->lis_mirror_index;
423 /* move mirror index to the next one */
424 index = (index + 1) % comp->lo_mirror_count;
427 for (i = 0; i < comp->lo_mirror_count; i++) {
428 struct lu_extent ext = { .e_start = lio->lis_pos,
429 .e_end = lio->lis_pos + 1 };
430 struct lov_mirror_entry *lre;
431 struct lov_layout_entry *lle;
434 lre = &comp->lo_mirrors[(index + i) % comp->lo_mirror_count];
438 if (lre->lre_foreign)
441 lov_foreach_mirror_layout_entry(obj, lle, lre) {
445 if (lu_extent_is_overlapped(&ext, lle->lle_extent)) {
449 } /* each component of the mirror */
451 index = (index + i) % comp->lo_mirror_count;
456 if (i == comp->lo_mirror_count) {
457 CERROR(DFID": failed to find a component covering "
458 "I/O region at %llu\n",
459 PFID(lu_object_fid(lov2lu(obj))), lio->lis_pos);
461 dump_lsm(D_ERROR, obj->lo_lsm);
466 CDEBUG(D_VFSTRACE, DFID ": flr state: %d, move mirror from %d to %d, "
467 "have retried: %d, mirror count: %d\n",
468 PFID(lu_object_fid(lov2lu(obj))), lov_flr_state(obj),
469 lio->lis_mirror_index, index, io->ci_ndelay_tried,
470 comp->lo_mirror_count);
472 lio->lis_mirror_index = index;
475 * FLR: if all mirrors have been tried once, most likely the network
476 * of this client has been partitioned. We should relinquish CPU for
477 * a while before trying again.
479 if (io->ci_ndelay && io->ci_ndelay_tried > 0 &&
480 (io->ci_ndelay_tried % comp->lo_mirror_count == 0)) {
481 schedule_timeout_interruptible(cfs_time_seconds(1) / 100);
482 if (signal_pending(current))
486 * we'd set ci_tried_all_mirrors to turn off fast mirror
487 * switching for read after we've tried all mirrors several
490 io->ci_tried_all_mirrors = io->ci_ndelay_tried %
491 (comp->lo_mirror_count * 4) == 0;
493 ++io->ci_ndelay_tried;
495 CDEBUG(D_VFSTRACE, "use %sdelayed RPC state for this IO\n",
496 io->ci_ndelay ? "non-" : "");
501 static int lov_io_slice_init(struct lov_io *lio,
502 struct lov_object *obj, struct cl_io *io)
509 lio->lis_object = obj;
510 lio->lis_cached_entry = LIS_CACHE_ENTRY_NONE;
512 switch (io->ci_type) {
515 lio->lis_pos = io->u.ci_rw.crw_pos;
516 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_bytes;
517 lio->lis_io_endpos = lio->lis_endpos;
518 if (cl_io_is_append(io)) {
519 LASSERT(io->ci_type == CIT_WRITE);
522 * If there is LOV EA hole, then we may cannot locate
523 * the current file-tail exactly.
525 if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
527 GOTO(out, result = -EIO);
530 lio->lis_endpos = OBD_OBJECT_EOF;
535 if (cl_io_is_fallocate(io)) {
536 lio->lis_pos = io->u.ci_setattr.sa_falloc_offset;
537 lio->lis_endpos = io->u.ci_setattr.sa_falloc_end;
538 } else if (cl_io_is_trunc(io)) {
539 lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
540 lio->lis_endpos = OBD_OBJECT_EOF;
543 lio->lis_endpos = OBD_OBJECT_EOF;
547 case CIT_DATA_VERSION:
549 lio->lis_endpos = OBD_OBJECT_EOF;
553 pgoff_t index = io->u.ci_fault.ft_index;
555 lio->lis_pos = index << PAGE_SHIFT;
556 lio->lis_endpos = (index + 1) << PAGE_SHIFT;
561 lio->lis_pos = io->u.ci_fsync.fi_start;
562 lio->lis_endpos = io->u.ci_fsync.fi_end;
567 lio->lis_pos = io->u.ci_ladvise.li_start;
568 lio->lis_endpos = io->u.ci_ladvise.li_end;
573 lio->lis_pos = io->u.ci_lseek.ls_start;
574 lio->lis_endpos = OBD_OBJECT_EOF;
580 lio->lis_endpos = OBD_OBJECT_EOF;
585 lio->lis_endpos = OBD_OBJECT_EOF;
593 * CIT_MISC + ci_ignore_layout can identify the I/O from the OSC layer,
594 * it won't care/access lov layout related info.
596 if (io->ci_ignore_layout && io->ci_type == CIT_MISC)
597 GOTO(out, result = 0);
599 LASSERT(obj->lo_lsm != NULL);
601 result = lov_io_mirror_init(lio, obj, io);
605 /* check if it needs to instantiate layout */
606 if (!(io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io) ||
607 cl_io_is_fallocate(io) ||
608 (cl_io_is_trunc(io) && io->u.ci_setattr.sa_attr.lvb_size > 0)))
609 GOTO(out, result = 0);
612 * for truncate, it only needs to instantiate the components
613 * before the truncated size.
615 if (cl_io_is_trunc(io)) {
616 io->ci_write_intent.e_start = 0;
617 /* for writes, e_end is endpos, the location of the file
618 * pointer after the write is completed, so it is not accessed.
619 * For truncate, 'end' is the size, and *is* acccessed.
620 * In other words, writes are [start, end), but truncate is
621 * [start, size], where both are included. So add 1 to the
622 * size when creating the write intent to account for this.
624 io->ci_write_intent.e_end =
625 io->u.ci_setattr.sa_attr.lvb_size + 1;
627 io->ci_write_intent.e_start = lio->lis_pos;
628 io->ci_write_intent.e_end = lio->lis_endpos;
632 lov_foreach_io_layout(index, lio, &io->ci_write_intent) {
633 if (!lsm_entry_inited(obj->lo_lsm, index)) {
634 io->ci_need_write_intent = 1;
639 if (io->ci_need_write_intent && io->ci_designated_mirror > 0) {
641 * REINT_SYNC RPC has already tried to instantiate all of the
642 * components involved, obviously it didn't succeed. Skip this
643 * mirror for now. The server won't be able to figure out
644 * which mirror it should instantiate components
646 CERROR(DFID": trying to instantiate components for designated "
647 "I/O, file state: %d\n",
648 PFID(lu_object_fid(lov2lu(obj))), lov_flr_state(obj));
650 io->ci_need_write_intent = 0;
651 GOTO(out, result = -EIO);
654 if (io->ci_need_write_intent)
655 GOTO(out, result = 1);
663 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
665 struct lov_io *lio = cl2lov_io(env, ios);
666 struct lov_object *lov = cl2lov(ios->cis_obj);
667 struct lov_io_sub *sub;
670 LASSERT(list_empty(&lio->lis_active));
672 while ((sub = list_first_entry_or_null(&lio->lis_subios,
674 sub_list)) != NULL) {
675 list_del_init(&sub->sub_list);
676 lio->lis_nr_subios--;
678 lov_io_sub_fini(env, lio, sub);
679 lov_sub_free(lio, sub);
681 LASSERT(lio->lis_nr_subios == 0);
683 LASSERT(atomic_read(&lov->lo_active_ios) > 0);
684 if (atomic_dec_and_test(&lov->lo_active_ios))
685 wake_up(&lov->lo_waitq);
689 static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
690 loff_t start, loff_t end)
692 struct cl_io *io = &sub->sub_io;
693 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
694 struct cl_io *parent = lio->lis_cl.cis_io;
695 int index = lov_comp_entry(sub->sub_subio_index);
696 int stripe = lov_comp_stripe(sub->sub_subio_index);
698 switch (io->ci_type) {
700 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
701 io->u.ci_setattr.sa_attr_flags =
702 parent->u.ci_setattr.sa_attr_flags;
703 io->u.ci_setattr.sa_avalid = parent->u.ci_setattr.sa_avalid;
704 io->u.ci_setattr.sa_xvalid = parent->u.ci_setattr.sa_xvalid;
705 io->u.ci_setattr.sa_falloc_mode =
706 parent->u.ci_setattr.sa_falloc_mode;
707 io->u.ci_setattr.sa_stripe_index = stripe;
708 io->u.ci_setattr.sa_parent_fid =
709 parent->u.ci_setattr.sa_parent_fid;
710 /* For SETATTR(fallocate) pass the subtype to lower IO */
711 io->u.ci_setattr.sa_subtype = parent->u.ci_setattr.sa_subtype;
712 if (cl_io_is_fallocate(io)) {
713 io->u.ci_setattr.sa_falloc_offset = start;
714 io->u.ci_setattr.sa_falloc_end = end;
715 io->u.ci_setattr.sa_falloc_uid =
716 parent->u.ci_setattr.sa_falloc_uid;
717 io->u.ci_setattr.sa_falloc_gid =
718 parent->u.ci_setattr.sa_falloc_gid;
719 io->u.ci_setattr.sa_falloc_projid =
720 parent->u.ci_setattr.sa_falloc_projid;
722 if (cl_io_is_trunc(io)) {
723 loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
725 new_size = lov_size_to_stripe(lsm, index, new_size,
727 io->u.ci_setattr.sa_attr.lvb_size = new_size;
729 lov_lsm2layout(lsm, lsm->lsm_entries[index],
730 &io->u.ci_setattr.sa_layout);
733 case CIT_DATA_VERSION: {
734 io->u.ci_data_version.dv_data_version = 0;
735 io->u.ci_data_version.dv_flags =
736 parent->u.ci_data_version.dv_flags;
740 loff_t off = parent->u.ci_fault.ft_index << PAGE_SHIFT;
742 io->u.ci_fault = parent->u.ci_fault;
743 off = lov_size_to_stripe(lsm, index, off, stripe);
744 io->u.ci_fault.ft_index = off >> PAGE_SHIFT;
748 io->u.ci_fsync.fi_start = start;
749 io->u.ci_fsync.fi_end = end;
750 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
751 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
756 io->u.ci_wr.wr_sync = cl_io_is_sync_write(parent);
757 io->ci_tried_all_mirrors = parent->ci_tried_all_mirrors;
758 if (cl_io_is_append(parent)) {
759 io->u.ci_wr.wr_append = 1;
761 io->u.ci_rw.crw_pos = start;
762 io->u.ci_rw.crw_bytes = end - start;
767 io->u.ci_ladvise.li_start = start;
768 io->u.ci_ladvise.li_end = end;
769 io->u.ci_ladvise.li_fid = parent->u.ci_ladvise.li_fid;
770 io->u.ci_ladvise.li_advice = parent->u.ci_ladvise.li_advice;
771 io->u.ci_ladvise.li_flags = parent->u.ci_ladvise.li_flags;
775 io->u.ci_lseek.ls_start = start;
776 io->u.ci_lseek.ls_whence = parent->u.ci_lseek.ls_whence;
777 io->u.ci_lseek.ls_result = parent->u.ci_lseek.ls_result;
787 static loff_t lov_offset_mod(loff_t val, int delta)
789 if (val != OBD_OBJECT_EOF)
794 static int lov_io_add_sub(const struct lu_env *env, struct lov_io *lio,
795 struct lov_io_sub *sub, u64 start, u64 end)
799 end = lov_offset_mod(end, 1);
800 lov_io_sub_inherit(sub, lio, start, end);
801 rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
803 cl_io_iter_fini(sub->sub_env, &sub->sub_io);
807 list_add_tail(&sub->sub_linkage, &lio->lis_active);
811 static int lov_io_iter_init(const struct lu_env *env,
812 const struct cl_io_slice *ios)
814 struct lov_io *lio = cl2lov_io(env, ios);
815 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
816 bool is_trunc = cl_io_is_trunc(ios->cis_io);
817 struct lov_io_sub *sub;
818 struct lu_extent ext;
824 ext.e_start = lio->lis_pos;
825 ext.e_end = lio->lis_endpos;
828 OBD_ALLOC_PTR_ARRAY(lio->lis_trunc_stripe_index,
829 lio->lis_object->u.composite.lo_entry_count);
830 if (lio->lis_trunc_stripe_index == NULL)
834 lov_foreach_io_layout(index, lio, &ext) {
835 struct lov_layout_entry *le = lov_entry(lio->lis_object, index);
836 struct lov_layout_raid0 *r0 = &le->lle_raid0;
840 bool tested_trunc_stripe = false;
843 lio->lis_trunc_stripe_index[index] = -1;
845 CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
846 index, lsm->lsm_entries[index]->lsme_flags);
847 if (!lsm_entry_inited(lsm, index)) {
849 * Read from uninitialized components should return
855 if (lsm_entry_is_foreign(lsm, index))
858 if (!le->lle_valid && !ios->cis_io->ci_designated_mirror) {
859 CERROR("I/O to invalid component: %d, mirror: %d\n",
860 index, lio->lis_mirror_index);
864 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
865 if (!lov_stripe_intersects(lsm, index, stripe,
869 if (unlikely(!r0->lo_sub[stripe])) {
870 if (ios->cis_io->ci_type == CIT_READ ||
871 ios->cis_io->ci_type == CIT_WRITE ||
872 ios->cis_io->ci_type == CIT_FAULT)
878 if (is_trunc && !tested_trunc_stripe) {
882 prev = (stripe == 0) ? r0->lo_nr - 1 :
885 * Only involving previous stripe if the
886 * truncate in this component is at the
887 * beginning of this stripe.
889 tested_trunc_stripe = true;
890 if (ext.e_start < lsm->lsm_entries[index]->
891 lsme_extent.e_start) {
892 /* need previous stripe involvement */
893 lio->lis_trunc_stripe_index[index] = prev;
895 div64_u64_rem(ext.e_start,
896 stripe_width(lsm, index),
898 /* tr_start %= stripe_swidth */
899 if (tr_start == stripe * lsm->
902 lio->lis_trunc_stripe_index[index] = prev;
906 /* if the last stripe is the trunc stripeno */
908 lio->lis_trunc_stripe_index[index] == stripe)
909 lio->lis_trunc_stripe_index[index] = -1;
911 sub = lov_sub_get(env, lio,
912 lov_comp_index(index, stripe));
916 rc = lov_io_add_sub(env, lio, sub, start, end);
923 if (is_trunc && lio->lis_trunc_stripe_index[index] != -1) {
924 stripe = lio->lis_trunc_stripe_index[index];
925 if (unlikely(!r0->lo_sub[stripe])) {
926 lio->lis_trunc_stripe_index[index] = -1;
929 sub = lov_sub_get(env, lio,
930 lov_comp_index(index, stripe));
935 * the prev sub could be used by another truncate, we'd
936 * skip it. LU-14128 happends when expand truncate +
937 * read get wrong kms.
939 if (!list_empty(&sub->sub_linkage)) {
940 lio->lis_trunc_stripe_index[index] = -1;
944 (void)lov_stripe_intersects(lsm, index, stripe, &ext,
946 rc = lov_io_add_sub(env, lio, sub, start, end);
955 static int lov_io_rw_iter_init(const struct lu_env *env,
956 const struct cl_io_slice *ios)
958 struct lov_io *lio = cl2lov_io(env, ios);
959 struct cl_io *io = ios->cis_io;
960 struct lov_stripe_md_entry *lse;
961 loff_t start = io->u.ci_rw.crw_pos;
965 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
968 if (cl_io_is_append(io))
969 RETURN(lov_io_iter_init(env, ios));
971 index = lov_io_layout_at(lio, io->u.ci_rw.crw_pos);
972 if (index < 0) { /* non-existing layout component */
973 if (io->ci_type == CIT_READ) {
975 * TODO: it needs to detect the next component and
976 * then set the next pos
980 RETURN(lov_io_iter_init(env, ios));
986 if (!lov_entry(lio->lis_object, index)->lle_valid &&
987 !io->ci_designated_mirror)
988 RETURN(io->ci_type == CIT_READ ? -EAGAIN : -EIO);
990 lse = lov_lse(lio->lis_object, index);
992 if (lsme_is_foreign(lse))
995 next = MAX_LFS_FILESIZE;
996 if (lse->lsme_stripe_count > 1) {
997 unsigned long ssize = lse->lsme_stripe_size;
999 start = div64_u64(start, ssize);
1000 next = (start + 1) * ssize;
1001 if (next <= start * ssize)
1002 next = MAX_LFS_FILESIZE;
1005 LASSERTF(io->u.ci_rw.crw_pos >= lse->lsme_extent.e_start,
1006 "pos %lld, [%lld, %lld)\n", io->u.ci_rw.crw_pos,
1007 lse->lsme_extent.e_start, lse->lsme_extent.e_end);
1008 next = min_t(__u64, next, lse->lsme_extent.e_end);
1009 next = min_t(loff_t, next, lio->lis_io_endpos);
1011 io->ci_continue = next < lio->lis_io_endpos;
1012 io->u.ci_rw.crw_bytes = next - io->u.ci_rw.crw_pos;
1013 lio->lis_pos = io->u.ci_rw.crw_pos;
1014 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_bytes;
1016 "stripe: %llu chunk: [%llu, %llu) %llu, %zd\n",
1017 (__u64)start, lio->lis_pos, lio->lis_endpos,
1018 (__u64)lio->lis_io_endpos, io->u.ci_rw.crw_bytes);
1021 * XXX The following call should be optimized: we know, that
1022 * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
1024 RETURN(lov_io_iter_init(env, ios));
1027 static int lov_io_setattr_iter_init(const struct lu_env *env,
1028 const struct cl_io_slice *ios)
1030 struct lov_io *lio = cl2lov_io(env, ios);
1031 struct cl_io *io = ios->cis_io;
1035 if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
1036 index = lov_io_layout_at(lio, lio->lis_pos - 1);
1037 /* no entry found for such offset */
1039 RETURN(io->ci_result = -ENODATA);
1042 RETURN(lov_io_iter_init(env, ios));
1045 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
1046 int (*iofunc)(const struct lu_env *, struct cl_io *))
1048 struct cl_io *parent = lio->lis_cl.cis_io;
1049 struct lov_io_sub *sub;
1053 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1054 rc = iofunc(sub->sub_env, &sub->sub_io);
1057 * fsync race with truncate, we'd continue to other
1058 * OST object's fsync to potentially discard
1059 * caching pages (osc_cache_writeback_range).
1061 if (rc == -ENOENT && parent->ci_type == CIT_FSYNC)
1066 if (parent->ci_result == 0)
1067 parent->ci_result = sub->sub_io.ci_result;
1072 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
1075 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
1078 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
1081 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
1084 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
1088 * It's possible that lov_io_start() wasn't called against this
1089 * sub-io, either because previous sub-io failed, or upper layer
1092 if (io->ci_state == CIS_IO_GOING)
1095 io->ci_state = CIS_IO_FINISHED;
1099 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
1101 cl_io_iter_fini(env, io);
1105 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
1107 cl_io_unlock(env, io);
1111 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
1115 /* Before ending each i/o, we must set lis_cached_entry to tell the
1116 * next i/o not to use stale cached lis information.
1118 cl2lov_io(env, ios)->lis_cached_entry = LIS_CACHE_ENTRY_NONE;
1120 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
1125 lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
1127 struct lov_io *lio = cl2lov_io(env, ios);
1128 struct cl_io *parent = lio->lis_cl.cis_io;
1129 struct cl_data_version_io *pdv = &parent->u.ci_data_version;
1130 struct lov_io_sub *sub;
1133 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1134 struct cl_data_version_io *sdv = &sub->sub_io.u.ci_data_version;
1136 lov_io_end_wrapper(sub->sub_env, &sub->sub_io);
1138 pdv->dv_data_version += sdv->dv_data_version;
1139 if (pdv->dv_layout_version > sdv->dv_layout_version)
1140 pdv->dv_layout_version = sdv->dv_layout_version;
1142 if (parent->ci_result == 0)
1143 parent->ci_result = sub->sub_io.ci_result;
1149 static void lov_io_iter_fini(const struct lu_env *env,
1150 const struct cl_io_slice *ios)
1152 struct lov_io *lio = cl2lov_io(env, ios);
1157 if (lio->lis_trunc_stripe_index != NULL)
1158 OBD_FREE_PTR_ARRAY(lio->lis_trunc_stripe_index,
1159 lio->lis_object->u.composite.lo_entry_count);
1160 lio->lis_trunc_stripe_index = NULL;
1162 rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
1164 while (!list_empty(&lio->lis_active))
1165 list_del_init(lio->lis_active.next);
1169 static void lov_io_unlock(const struct lu_env *env,
1170 const struct cl_io_slice *ios)
1175 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
1180 static int lov_io_read_ahead(const struct lu_env *env,
1181 const struct cl_io_slice *ios,
1182 pgoff_t start, struct cl_read_ahead *ra)
1184 struct lov_io *lio = cl2lov_io(env, ios);
1185 struct lov_object *loo = lio->lis_object;
1186 struct lov_layout_raid0 *r0;
1187 struct lov_io_sub *sub;
1191 unsigned int pps; /* pages per stripe */
1197 offset = start << PAGE_SHIFT;
1198 index = lov_io_layout_at(lio, offset);
1199 if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index) ||
1200 lsm_entry_is_foreign(loo->lo_lsm, index))
1203 /* avoid readahead to expand to stale components */
1204 if (!lov_entry(loo, index)->lle_valid)
1207 stripe = lov_stripe_number(loo->lo_lsm, index, offset);
1209 r0 = lov_r0(loo, index);
1210 if (unlikely(!r0->lo_sub[stripe]))
1213 sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
1215 RETURN(PTR_ERR(sub));
1217 lov_stripe_offset(loo->lo_lsm, index, offset, stripe, &suboff);
1218 rc = cl_io_read_ahead(sub->sub_env, &sub->sub_io,
1219 suboff >> PAGE_SHIFT, ra);
1221 CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
1222 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end_idx,
1228 * Adjust the stripe index by layout of comp. ra->cra_end is the
1229 * maximum page index covered by an underlying DLM lock.
1230 * This function converts cra_end from stripe level to file level, and
1231 * make sure it's not beyond stripe and component boundary.
1234 /* cra_end is stripe level, convert it into file level */
1235 ra_end = ra->cra_end_idx;
1236 if (ra_end != CL_PAGE_EOF)
1237 ra->cra_end_idx = lov_stripe_pgoff(loo->lo_lsm, index,
1240 /* boundary of current component */
1241 ra_end = lov_io_extent(lio, index)->e_end >> PAGE_SHIFT;
1242 if (ra_end != CL_PAGE_EOF && ra->cra_end_idx >= ra_end)
1243 ra->cra_end_idx = ra_end - 1;
1245 if (r0->lo_nr == 1) /* single stripe file */
1248 pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
1250 CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %d, "
1251 "stripe_size = %u, stripe no = %u, start index = %lu\n",
1252 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end_idx, pps, index,
1253 lov_lse(loo, index)->lsme_stripe_size, stripe, start);
1255 /* never exceed the end of the stripe */
1256 ra->cra_end_idx = min_t(pgoff_t, ra->cra_end_idx,
1257 start + pps - start % pps - 1);
1261 int lov_io_lru_reserve(const struct lu_env *env,
1262 const struct cl_io_slice *ios, loff_t pos, size_t bytes)
1264 struct lov_io *lio = cl2lov_io(env, ios);
1265 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
1266 struct lov_io_sub *sub;
1267 struct lu_extent ext;
1274 ext.e_end = pos + bytes;
1275 lov_foreach_io_layout(index, lio, &ext) {
1276 struct lov_layout_entry *le = lov_entry(lio->lis_object, index);
1277 struct lov_layout_raid0 *r0 = &le->lle_raid0;
1282 if (!lsm_entry_inited(lsm, index))
1285 if (!le->lle_valid && !ios->cis_io->ci_designated_mirror) {
1286 CERROR(DFID": I/O to invalid component: %d, mirror: %d\n",
1287 PFID(lu_object_fid(lov2lu(lio->lis_object))),
1288 index, lio->lis_mirror_index);
1292 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
1293 if (!lov_stripe_intersects(lsm, index, stripe,
1294 &ext, &start, &end))
1297 if (unlikely(!r0->lo_sub[stripe]))
1300 sub = lov_sub_get(env, lio,
1301 lov_comp_index(index, stripe));
1303 return PTR_ERR(sub);
1305 rc = cl_io_lru_reserve(sub->sub_env, &sub->sub_io, start,
1316 * lov implementation of cl_operations::cio_submit() method. It takes a list
1317 * of pages in \a queue, splits it into per-stripe sub-lists, invokes
1318 * cl_io_submit() on underlying devices to submit sub-lists, and then splices
1321 * Major complication of this function is a need to handle memory cleansing:
1322 * cl_io_submit() is called to write out pages as a part of VM memory
1323 * reclamation, and hence it may not fail due to memory shortages (system
1324 * dead-locks otherwise). To deal with this, some resources (sub-lists,
1325 * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
1326 * not-memory cleansing context), and in case of memory shortage, these
1327 * pre-allocated resources are used by lov_io_submit() under
1328 * lov_device::ld_mutex mutex.
1330 static int lov_io_submit(const struct lu_env *env,
1331 const struct cl_io_slice *ios,
1332 enum cl_req_type crt, struct cl_2queue *queue)
1334 struct cl_page_list *qin = &queue->c2_qin;
1335 struct lov_io *lio = cl2lov_io(env, ios);
1336 struct lov_io_sub *sub;
1337 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
1338 struct cl_page *page = cl_page_list_first(qin);
1339 struct cl_page *tmp;
1345 if (page->cp_type == CPT_TRANSIENT)
1348 cl_page_list_init(plist);
1349 while (qin->pl_nr > 0) {
1350 struct cl_2queue *cl2q = &lov_env_info(env)->lti_cl2q;
1352 page = cl_page_list_first(qin);
1353 if (lov_page_is_empty(page)) {
1354 cl_page_list_move(&queue->c2_qout, qin, page);
1357 * it could only be mirror read to get here therefore
1358 * the pages will be transient. We don't care about
1359 * the return code of cl_page_prep() at all.
1361 (void) cl_page_prep(env, ios->cis_io, page, crt);
1362 cl_page_completion(env, page, crt, 0);
1366 cl_2queue_init(cl2q);
1367 cl_page_list_move(&cl2q->c2_qin, qin, page);
1369 index = page->cp_lov_index;
1370 /* DIO is already split by stripe */
1372 cl_page_list_for_each_safe(page, tmp, qin) {
1373 /* this page is not on this stripe */
1374 if (index != page->cp_lov_index)
1377 cl_page_list_move(&cl2q->c2_qin, qin, page);
1380 cl_page_list_splice(qin, &cl2q->c2_qin);
1383 sub = lov_sub_get(env, lio, index);
1385 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
1391 cl_page_list_splice(&cl2q->c2_qin, plist);
1392 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
1393 cl_2queue_fini(env, cl2q);
1399 cl_page_list_splice(plist, qin);
1400 cl_page_list_fini(env, plist);
1405 static int lov_io_commit_async(const struct lu_env *env,
1406 const struct cl_io_slice *ios,
1407 struct cl_page_list *queue, int from, int to,
1410 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
1411 struct lov_io *lio = cl2lov_io(env, ios);
1412 struct lov_io_sub *sub;
1413 struct cl_page *page;
1417 if (lio->lis_nr_subios == 1) {
1418 int idx = lio->lis_single_subio_index;
1420 LASSERT(!lov_page_is_empty(cl_page_list_first(queue)));
1422 sub = lov_sub_get(env, lio, idx);
1423 LASSERT(!IS_ERR(sub));
1424 LASSERT(sub == &lio->lis_single_subio);
1425 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io, queue,
1430 cl_page_list_init(plist);
1431 while (queue->pl_nr > 0) {
1435 LASSERT(plist->pl_nr == 0);
1436 page = cl_page_list_first(queue);
1437 LASSERT(!lov_page_is_empty(page));
1439 cl_page_list_move(plist, queue, page);
1441 index = page->cp_lov_index;
1442 while (queue->pl_nr > 0) {
1443 page = cl_page_list_first(queue);
1444 if (index != page->cp_lov_index)
1447 cl_page_list_move(plist, queue, page);
1450 if (queue->pl_nr > 0) /* still has more pages */
1451 stripe_to = PAGE_SIZE;
1453 sub = lov_sub_get(env, lio, index);
1455 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io,
1456 plist, from, stripe_to, cb);
1462 if (plist->pl_nr > 0) /* short write */
1467 if (lov_comp_entry(index) !=
1468 lov_comp_entry(page->cp_lov_index))
1469 cl_io_extent_release(sub->sub_env, &sub->sub_io);
1472 /* for error case, add the page back into the qin list */
1473 LASSERT(ergo(rc == 0, plist->pl_nr == 0));
1474 while (plist->pl_nr > 0) {
1475 /* error occurred, add the uncommitted pages back into queue */
1476 page = cl_page_list_last(plist);
1477 cl_page_list_move_head(queue, plist, page);
1483 static int lov_io_fault_start(const struct lu_env *env,
1484 const struct cl_io_slice *ios)
1486 struct cl_fault_io *fio;
1488 struct lov_io_sub *sub;
1495 fio = &ios->cis_io->u.ci_fault;
1496 lio = cl2lov_io(env, ios);
1499 * LU-14502: ft_page could be an existing cl_page associated with
1500 * the vmpage covering the fault index, and the page may still
1501 * refer to another mirror of an old IO.
1503 if (lov_is_flr(lio->lis_object)) {
1504 offset = fio->ft_index << PAGE_SHIFT;;
1505 entry = lov_io_layout_at(lio, offset);
1507 CERROR(DFID": page fault index %lu invalid component: "
1509 PFID(lu_object_fid(&ios->cis_obj->co_lu)),
1510 fio->ft_index, entry,
1511 lio->lis_mirror_index);
1514 stripe = lov_stripe_number(lio->lis_object->lo_lsm,
1517 if (fio->ft_page->cp_lov_index !=
1518 lov_comp_index(entry, stripe)) {
1519 CDEBUG(D_INFO, DFID": page fault at index %lu, "
1520 "at mirror %u comp entry %u stripe %u, "
1521 "been used with comp entry %u stripe %u\n",
1522 PFID(lu_object_fid(&ios->cis_obj->co_lu)),
1523 fio->ft_index, lio->lis_mirror_index,
1525 lov_comp_entry(fio->ft_page->cp_lov_index),
1526 lov_comp_stripe(fio->ft_page->cp_lov_index));
1528 fio->ft_page->cp_lov_index =
1529 lov_comp_index(entry, stripe);
1533 sub = lov_sub_get(env, lio, fio->ft_page->cp_lov_index);
1534 sub->sub_io.u.ci_fault.ft_bytes = fio->ft_bytes;
1536 RETURN(lov_io_start(env, ios));
1539 static int lov_io_setattr_start(const struct lu_env *env,
1540 const struct cl_io_slice *ios)
1542 struct lov_io *lio = cl2lov_io(env, ios);
1543 struct cl_io *parent = ios->cis_io;
1544 struct lov_io_sub *sub;
1545 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
1549 if (cl_io_is_fallocate(parent)) {
1550 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1551 loff_t size = parent->u.ci_setattr.sa_attr.lvb_size;
1552 int index = lov_comp_entry(sub->sub_subio_index);
1553 int stripe = lov_comp_stripe(sub->sub_subio_index);
1555 size = lov_size_to_stripe(lsm, index, size, stripe);
1556 sub->sub_io.u.ci_setattr.sa_attr.lvb_size = size;
1557 sub->sub_io.u.ci_setattr.sa_avalid =
1558 parent->u.ci_setattr.sa_avalid;
1562 RETURN(lov_io_start(env, ios));
1565 static void lov_io_fsync_end(const struct lu_env *env,
1566 const struct cl_io_slice *ios)
1568 struct lov_io *lio = cl2lov_io(env, ios);
1569 struct lov_io_sub *sub;
1570 unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
1574 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1575 struct cl_io *subio = &sub->sub_io;
1577 lov_io_end_wrapper(sub->sub_env, subio);
1579 if (subio->ci_result == 0)
1580 *written += subio->u.ci_fsync.fi_nr_written;
1585 static void lov_io_lseek_end(const struct lu_env *env,
1586 const struct cl_io_slice *ios)
1588 struct lov_io *lio = cl2lov_io(env, ios);
1589 struct cl_io *io = lio->lis_cl.cis_io;
1590 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
1591 struct lov_io_sub *sub;
1592 loff_t offset = -ENXIO;
1594 bool seek_hole = io->u.ci_lseek.ls_whence == SEEK_HOLE;
1598 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
1599 struct cl_io *subio = &sub->sub_io;
1600 int index = lov_comp_entry(sub->sub_subio_index);
1601 int stripe = lov_comp_stripe(sub->sub_subio_index);
1602 loff_t sub_off, lov_off;
1603 __u64 comp_end = lsm->lsm_entries[index]->lsme_extent.e_end;
1605 lov_io_end_wrapper(sub->sub_env, subio);
1607 if (io->ci_result == 0)
1608 io->ci_result = sub->sub_io.ci_result;
1613 CDEBUG(D_INFO, DFID": entry %x stripe %u: SEEK_%s from %lld\n",
1614 PFID(lu_object_fid(lov2lu(lio->lis_object))),
1615 index, stripe, seek_hole ? "HOLE" : "DATA",
1616 subio->u.ci_lseek.ls_start);
1618 /* first subio with positive result is what we need */
1619 sub_off = subio->u.ci_lseek.ls_result;
1620 /* Expected error, offset is out of stripe file size */
1621 if (sub_off == -ENXIO)
1623 /* Any other errors are not expected with ci_result == 0 */
1625 CDEBUG(D_INFO, "unexpected error: rc = %lld\n",
1627 io->ci_result = sub_off;
1630 lov_off = lov_stripe_size(lsm, index, sub_off + 1, stripe) - 1;
1632 /* the only way to get negatove lov_off here is too big
1633 * result. Return -EOVERFLOW then.
1635 io->ci_result = -EOVERFLOW;
1636 CDEBUG(D_INFO, "offset %llu is too big: rc = %d\n",
1637 (u64)lov_off, io->ci_result);
1640 if (lov_off < io->u.ci_lseek.ls_start) {
1641 io->ci_result = -EINVAL;
1642 CDEBUG(D_INFO, "offset %lld < start %lld: rc = %d\n",
1643 sub_off, io->u.ci_lseek.ls_start, io->ci_result);
1646 /* resulting offset can be out of component range if stripe
1647 * object is full and its file size was returned as virtual
1648 * hole start. Skip this result, the next component will give
1649 * us correct lseek result but keep possible hole offset in
1650 * case there is no more components ahead
1652 if (lov_off >= comp_end) {
1653 /* must be SEEK_HOLE case */
1654 if (likely(seek_hole)) {
1655 /* save comp end as potential hole offset */
1656 hole_off = max_t(__u64, comp_end, hole_off);
1658 io->ci_result = -EINVAL;
1660 "off %lld >= comp_end %llu: rc = %d\n",
1661 lov_off, comp_end, io->ci_result);
1666 CDEBUG(D_INFO, "SEEK_%s: %lld->%lld/%lld: rc = %d\n",
1667 seek_hole ? "HOLE" : "DATA",
1668 subio->u.ci_lseek.ls_start, sub_off, lov_off,
1669 sub->sub_io.ci_result);
1670 offset = min_t(__u64, offset, lov_off);
1672 /* no result but some component returns hole as component end */
1673 if (seek_hole && offset == -ENXIO && hole_off > 0)
1676 io->u.ci_lseek.ls_result = offset;
1680 static const struct cl_io_operations lov_io_ops = {
1683 .cio_fini = lov_io_fini,
1684 .cio_iter_init = lov_io_rw_iter_init,
1685 .cio_iter_fini = lov_io_iter_fini,
1686 .cio_lock = lov_io_lock,
1687 .cio_unlock = lov_io_unlock,
1688 .cio_start = lov_io_start,
1689 .cio_end = lov_io_end
1692 .cio_fini = lov_io_fini,
1693 .cio_iter_init = lov_io_rw_iter_init,
1694 .cio_iter_fini = lov_io_iter_fini,
1695 .cio_lock = lov_io_lock,
1696 .cio_unlock = lov_io_unlock,
1697 .cio_start = lov_io_start,
1698 .cio_end = lov_io_end
1701 .cio_fini = lov_io_fini,
1702 .cio_iter_init = lov_io_setattr_iter_init,
1703 .cio_iter_fini = lov_io_iter_fini,
1704 .cio_lock = lov_io_lock,
1705 .cio_unlock = lov_io_unlock,
1706 .cio_start = lov_io_setattr_start,
1707 .cio_end = lov_io_end
1709 [CIT_DATA_VERSION] = {
1710 .cio_fini = lov_io_fini,
1711 .cio_iter_init = lov_io_iter_init,
1712 .cio_iter_fini = lov_io_iter_fini,
1713 .cio_lock = lov_io_lock,
1714 .cio_unlock = lov_io_unlock,
1715 .cio_start = lov_io_start,
1716 .cio_end = lov_io_data_version_end,
1719 .cio_fini = lov_io_fini,
1720 .cio_iter_init = lov_io_iter_init,
1721 .cio_iter_fini = lov_io_iter_fini,
1722 .cio_lock = lov_io_lock,
1723 .cio_unlock = lov_io_unlock,
1724 .cio_start = lov_io_fault_start,
1725 .cio_end = lov_io_end
1728 .cio_fini = lov_io_fini,
1729 .cio_iter_init = lov_io_iter_init,
1730 .cio_iter_fini = lov_io_iter_fini,
1731 .cio_lock = lov_io_lock,
1732 .cio_unlock = lov_io_unlock,
1733 .cio_start = lov_io_start,
1734 .cio_end = lov_io_fsync_end
1737 .cio_fini = lov_io_fini,
1738 .cio_iter_init = lov_io_iter_init,
1739 .cio_iter_fini = lov_io_iter_fini,
1740 .cio_lock = lov_io_lock,
1741 .cio_unlock = lov_io_unlock,
1742 .cio_start = lov_io_start,
1743 .cio_end = lov_io_end
1746 .cio_fini = lov_io_fini,
1747 .cio_iter_init = lov_io_iter_init,
1748 .cio_iter_fini = lov_io_iter_fini,
1749 .cio_lock = lov_io_lock,
1750 .cio_unlock = lov_io_unlock,
1751 .cio_start = lov_io_start,
1752 .cio_end = lov_io_lseek_end
1755 .cio_fini = lov_io_fini,
1758 .cio_fini = lov_io_fini
1761 .cio_read_ahead = lov_io_read_ahead,
1762 .cio_lru_reserve = lov_io_lru_reserve,
1763 .cio_submit = lov_io_submit,
1764 .cio_commit_async = lov_io_commit_async,
1768 * Empty lov io operations.
1770 static void lov_empty_io_fini(const struct lu_env *env,
1771 const struct cl_io_slice *ios)
1773 struct lov_object *lov = cl2lov(ios->cis_obj);
1776 if (atomic_dec_and_test(&lov->lo_active_ios))
1777 wake_up(&lov->lo_waitq);
1781 static int lov_empty_io_submit(const struct lu_env *env,
1782 const struct cl_io_slice *ios,
1783 enum cl_req_type crt, struct cl_2queue *queue)
1788 static void lov_empty_impossible(const struct lu_env *env,
1789 struct cl_io_slice *ios)
1794 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
1797 * An io operation vector for files without stripes.
1799 static const struct cl_io_operations lov_empty_io_ops = {
1802 .cio_fini = lov_empty_io_fini,
1804 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1805 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1806 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1807 .cio_end = LOV_EMPTY_IMPOSSIBLE
1811 .cio_fini = lov_empty_io_fini,
1812 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1813 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1814 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1815 .cio_end = LOV_EMPTY_IMPOSSIBLE
1818 .cio_fini = lov_empty_io_fini,
1819 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1820 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1821 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1822 .cio_end = LOV_EMPTY_IMPOSSIBLE
1825 .cio_fini = lov_empty_io_fini,
1826 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1827 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1828 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1829 .cio_end = LOV_EMPTY_IMPOSSIBLE
1832 .cio_fini = lov_empty_io_fini
1835 .cio_fini = lov_empty_io_fini
1838 .cio_fini = lov_empty_io_fini
1841 .cio_fini = lov_empty_io_fini
1844 .cio_submit = lov_empty_io_submit,
1845 .cio_commit_async = LOV_EMPTY_IMPOSSIBLE
1848 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
1851 struct lov_io *lio = lov_env_io(env);
1852 struct lov_object *lov = cl2lov(obj);
1857 INIT_LIST_HEAD(&lio->lis_active);
1858 result = lov_io_slice_init(lio, lov, io);
1862 result = lov_io_subio_init(env, lio, io);
1864 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
1865 atomic_inc(&lov->lo_active_ios);
1869 io->ci_result = result < 0 ? result : 0;
1873 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
1876 struct lov_object *lov = cl2lov(obj);
1877 struct lov_io *lio = lov_env_io(env);
1881 lio->lis_object = lov;
1882 switch (io->ci_type) {
1894 case CIT_DATA_VERSION:
1902 CERROR("Page fault on a file without stripes: "DFID"\n",
1903 PFID(lu_object_fid(&obj->co_lu)));
1907 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1908 atomic_inc(&lov->lo_active_ios);
1911 io->ci_result = result < 0 ? result : 0;
1915 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
1918 struct lov_object *lov = cl2lov(obj);
1919 struct lov_io *lio = lov_env_io(env);
1923 LASSERT(lov->lo_lsm != NULL);
1924 lio->lis_object = lov;
1926 switch (io->ci_type) {
1928 LASSERTF(0, "invalid type %d\n", io->ci_type);
1929 result = -EOPNOTSUPP;
1935 case CIT_DATA_VERSION:
1940 * the truncate to 0 is managed by MDT:
1941 * - in open, for open O_TRUNC
1942 * - in setattr, for truncate
1945 * the truncate is for size > 0 so triggers a restore,
1946 * also trigger a restore for prealloc/punch
1948 if (cl_io_is_trunc(io) || cl_io_is_fallocate(io)) {
1949 io->ci_restore_needed = 1;
1958 io->ci_restore_needed = 1;
1964 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1965 atomic_inc(&lov->lo_active_ios);
1968 io->ci_result = result < 0 ? result : 0;
1973 * Return the index in composite:lo_entries by the file offset
1975 int lov_io_layout_at(struct lov_io *lio, __u64 offset)
1977 struct lov_object *lov = lio->lis_object;
1978 struct lov_layout_composite *comp = &lov->u.composite;
1979 int start_index = 0;
1980 int end_index = comp->lo_entry_count - 1;
1983 LASSERT(lov->lo_type == LLT_COMP);
1985 /* This is actual file offset so nothing can cover eof. */
1986 if (offset == LUSTRE_EOF)
1989 if (lov_is_flr(lov)) {
1990 struct lov_mirror_entry *lre;
1992 LASSERT(lio->lis_mirror_index >= 0);
1994 lre = &comp->lo_mirrors[lio->lis_mirror_index];
1995 start_index = lre->lre_start;
1996 end_index = lre->lre_end;
1999 for (i = start_index; i <= end_index; i++) {
2000 struct lov_layout_entry *lle = lov_entry(lov, i);
2002 LASSERT(!lsme_is_foreign(lle->lle_lsme));
2004 if ((offset >= lle->lle_extent->e_start &&
2005 offset < lle->lle_extent->e_end) ||
2006 (offset == OBD_OBJECT_EOF &&
2007 lle->lle_extent->e_end == OBD_OBJECT_EOF))