4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Implementation of cl_io for LOV layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_LOV
40 #include "lov_cl_internal.h"
46 static inline struct lov_io_sub *lov_sub_alloc(struct lov_io *lio, int index)
48 struct lov_io_sub *sub;
50 if (lio->lis_nr_subios == 0) {
51 LASSERT(lio->lis_single_subio_index == -1);
52 sub = &lio->lis_single_subio;
53 lio->lis_single_subio_index = index;
54 memset(sub, 0, sizeof(*sub));
60 INIT_LIST_HEAD(&sub->sub_list);
61 INIT_LIST_HEAD(&sub->sub_linkage);
62 sub->sub_subio_index = index;
68 static inline void lov_sub_free(struct lov_io *lio, struct lov_io_sub *sub)
70 if (sub->sub_subio_index == lio->lis_single_subio_index) {
71 LASSERT(sub == &lio->lis_single_subio);
72 lio->lis_single_subio_index = -1;
78 static void lov_io_sub_fini(const struct lu_env *env, struct lov_io *lio,
79 struct lov_io_sub *sub)
83 cl_io_fini(sub->sub_env, &sub->sub_io);
85 if (sub->sub_env != NULL && !IS_ERR(sub->sub_env)) {
86 cl_env_put(sub->sub_env, &sub->sub_refcheck);
92 static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
93 struct lov_io_sub *sub)
95 struct lov_object *lov = lio->lis_object;
97 struct cl_object *sub_obj;
98 struct cl_io *io = lio->lis_cl.cis_io;
99 int index = lov_comp_entry(sub->sub_subio_index);
100 int stripe = lov_comp_stripe(sub->sub_subio_index);
102 LASSERT(sub->sub_env == NULL);
105 if (unlikely(!lov_r0(lov, index)->lo_sub ||
106 !lov_r0(lov, index)->lo_sub[stripe]))
109 /* obtain new environment */
110 sub->sub_env = cl_env_get(&sub->sub_refcheck);
111 if (IS_ERR(sub->sub_env))
112 result = PTR_ERR(sub->sub_env);
114 sub_obj = lovsub2cl(lov_r0(lov, index)->lo_sub[stripe]);
115 sub_io = &sub->sub_io;
117 sub_io->ci_obj = sub_obj;
118 sub_io->ci_result = 0;
120 sub_io->ci_parent = io;
121 sub_io->ci_lockreq = io->ci_lockreq;
122 sub_io->ci_type = io->ci_type;
123 sub_io->ci_no_srvlock = io->ci_no_srvlock;
124 sub_io->ci_noatime = io->ci_noatime;
125 sub_io->ci_pio = io->ci_pio;
127 result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
130 lov_io_sub_fini(env, lio, sub);
135 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
136 struct lov_io *lio, int index)
138 struct lov_io_sub *sub;
143 list_for_each_entry(sub, &lio->lis_subios, sub_list) {
144 if (sub->sub_subio_index == index) {
151 sub = lov_sub_alloc(lio, index);
153 GOTO(out, rc = -ENOMEM);
155 rc = lov_io_sub_init(env, lio, sub);
157 lov_sub_free(lio, sub);
161 list_add_tail(&sub->sub_list, &lio->lis_subios);
162 lio->lis_nr_subios++;
170 /*****************************************************************************
176 int lov_page_index(const struct cl_page *page)
178 const struct cl_page_slice *slice;
181 slice = cl_page_at(page, &lov_device_type);
182 LASSERT(slice != NULL);
183 LASSERT(slice->cpl_obj != NULL);
185 RETURN(cl2lov_page(slice)->lps_index);
188 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
193 LASSERT(lio->lis_object != NULL);
195 INIT_LIST_HEAD(&lio->lis_subios);
196 lio->lis_single_subio_index = -1;
197 lio->lis_nr_subios = 0;
202 static int lov_io_slice_init(struct lov_io *lio,
203 struct lov_object *obj, struct cl_io *io)
208 lio->lis_object = obj;
210 LASSERT(obj->lo_lsm != NULL);
212 switch (io->ci_type) {
215 lio->lis_pos = io->u.ci_rw.rw_range.cir_pos;
216 lio->lis_endpos = lio->lis_pos + io->u.ci_rw.rw_range.cir_count;
217 lio->lis_io_endpos = lio->lis_endpos;
218 if (cl_io_is_append(io)) {
219 LASSERT(io->ci_type == CIT_WRITE);
221 /* If there is LOV EA hole, then we may cannot locate
222 * the current file-tail exactly. */
223 if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
228 lio->lis_endpos = OBD_OBJECT_EOF;
233 if (cl_io_is_trunc(io))
234 lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
237 lio->lis_endpos = OBD_OBJECT_EOF;
240 case CIT_DATA_VERSION:
242 lio->lis_endpos = OBD_OBJECT_EOF;
246 pgoff_t index = io->u.ci_fault.ft_index;
247 lio->lis_pos = cl_offset(io->ci_obj, index);
248 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
253 lio->lis_pos = io->u.ci_fsync.fi_start;
254 lio->lis_endpos = io->u.ci_fsync.fi_end;
259 lio->lis_pos = io->u.ci_ladvise.li_start;
260 lio->lis_endpos = io->u.ci_ladvise.li_end;
266 lio->lis_endpos = OBD_OBJECT_EOF;
276 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
278 struct lov_io *lio = cl2lov_io(env, ios);
279 struct lov_object *lov = cl2lov(ios->cis_obj);
283 LASSERT(list_empty(&lio->lis_active));
285 while (!list_empty(&lio->lis_subios)) {
286 struct lov_io_sub *sub = list_entry(lio->lis_subios.next,
290 list_del_init(&sub->sub_list);
291 lio->lis_nr_subios--;
293 lov_io_sub_fini(env, lio, sub);
294 lov_sub_free(lio, sub);
296 LASSERT(lio->lis_nr_subios == 0);
298 LASSERT(atomic_read(&lov->lo_active_ios) > 0);
299 if (atomic_dec_and_test(&lov->lo_active_ios))
300 wake_up_all(&lov->lo_waitq);
304 static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
305 loff_t start, loff_t end)
307 struct cl_io *io = &sub->sub_io;
308 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
309 struct cl_io *parent = lio->lis_cl.cis_io;
310 int index = lov_comp_entry(sub->sub_subio_index);
311 int stripe = lov_comp_stripe(sub->sub_subio_index);
313 io->ci_pio = parent->ci_pio;
314 switch (io->ci_type) {
316 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
317 io->u.ci_setattr.sa_attr_flags =
318 parent->u.ci_setattr.sa_attr_flags;
319 io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
320 io->u.ci_setattr.sa_stripe_index = stripe;
321 io->u.ci_setattr.sa_parent_fid =
322 parent->u.ci_setattr.sa_parent_fid;
323 if (cl_io_is_trunc(io)) {
324 loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
326 new_size = lov_size_to_stripe(lsm, index, new_size,
328 io->u.ci_setattr.sa_attr.lvb_size = new_size;
330 lov_lsm2layout(lsm, lsm->lsm_entries[index],
331 &io->u.ci_setattr.sa_layout);
334 case CIT_DATA_VERSION: {
335 io->u.ci_data_version.dv_data_version = 0;
336 io->u.ci_data_version.dv_flags =
337 parent->u.ci_data_version.dv_flags;
341 struct cl_object *obj = parent->ci_obj;
342 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
344 io->u.ci_fault = parent->u.ci_fault;
345 off = lov_size_to_stripe(lsm, index, off, stripe);
346 io->u.ci_fault.ft_index = cl_index(obj, off);
350 io->u.ci_fsync.fi_start = start;
351 io->u.ci_fsync.fi_end = end;
352 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
353 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
358 io->u.ci_rw.rw_ptask = parent->u.ci_rw.rw_ptask;
359 io->u.ci_rw.rw_iter = parent->u.ci_rw.rw_iter;
360 io->u.ci_rw.rw_iocb = parent->u.ci_rw.rw_iocb;
361 io->u.ci_rw.rw_file = parent->u.ci_rw.rw_file;
362 io->u.ci_rw.rw_sync = parent->u.ci_rw.rw_sync;
363 if (cl_io_is_append(parent)) {
364 io->u.ci_rw.rw_append = 1;
366 io->u.ci_rw.rw_range.cir_pos = start;
367 io->u.ci_rw.rw_range.cir_count = end - start;
372 io->u.ci_ladvise.li_start = start;
373 io->u.ci_ladvise.li_end = end;
374 io->u.ci_ladvise.li_fid = parent->u.ci_ladvise.li_fid;
375 io->u.ci_ladvise.li_advice = parent->u.ci_ladvise.li_advice;
376 io->u.ci_ladvise.li_flags = parent->u.ci_ladvise.li_flags;
384 static loff_t lov_offset_mod(loff_t val, int delta)
386 if (val != OBD_OBJECT_EOF)
391 static int lov_io_iter_init(const struct lu_env *env,
392 const struct cl_io_slice *ios)
394 struct cl_io *io = ios->cis_io;
395 struct lov_io *lio = cl2lov_io(env, ios);
396 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
397 struct lov_io_sub *sub;
398 struct lov_layout_entry *le;
399 struct lu_extent ext;
405 ext.e_start = lio->lis_pos;
406 ext.e_end = lio->lis_endpos;
409 lov_foreach_layout_entry(lio->lis_object, le) {
410 struct lov_layout_raid0 *r0 = &le->lle_raid0;
416 if (!lu_extent_is_overlapped(&ext, &le->lle_extent))
419 CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
420 index - 1, lsm->lsm_entries[index - 1]->lsme_flags);
421 if (!lsm_entry_inited(lsm, index - 1)) {
422 /* truncate IO will trigger write intent as well, and
423 * it's handled in lov_io_setattr_iter_init() */
424 if (io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io)) {
425 io->ci_need_write_intent = 1;
426 /* execute it in main thread */
432 /* Read from uninitialized components should return
433 * zero filled pages. */
437 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
438 if (!lov_stripe_intersects(lsm, index - 1, stripe,
442 if (unlikely(r0->lo_sub[stripe] == NULL)) {
443 if (ios->cis_io->ci_type == CIT_READ ||
444 ios->cis_io->ci_type == CIT_WRITE ||
445 ios->cis_io->ci_type == CIT_FAULT)
451 end = lov_offset_mod(end, 1);
452 sub = lov_sub_get(env, lio,
453 lov_comp_index(index - 1, stripe));
459 lov_io_sub_inherit(sub, lio, start, end);
460 rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
462 cl_io_iter_fini(sub->sub_env, &sub->sub_io);
467 "shrink stripe: {%d, %d} range: [%llu, %llu)\n",
468 index, stripe, start, end);
470 list_add_tail(&sub->sub_linkage, &lio->lis_active);
478 static int lov_io_rw_iter_init(const struct lu_env *env,
479 const struct cl_io_slice *ios)
481 struct cl_io *io = ios->cis_io;
482 struct lov_io *lio = cl2lov_io(env, ios);
483 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
484 struct lov_stripe_md_entry *lse;
485 struct cl_io_range *range = &io->u.ci_rw.rw_range;
486 loff_t start = range->cir_pos;
491 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
494 if (cl_io_is_append(io))
495 RETURN(lov_io_iter_init(env, ios));
497 index = lov_lsm_entry(lsm, range->cir_pos);
498 if (index < 0) { /* non-existing layout component */
499 if (io->ci_type == CIT_READ) {
500 /* TODO: it needs to detect the next component and
501 * then set the next pos */
503 /* execute it in main thread */
506 RETURN(lov_io_iter_init(env, ios));
512 lse = lov_lse(lio->lis_object, index);
514 ssize = lse->lsme_stripe_size;
515 lov_do_div64(start, ssize);
516 next = (start + 1) * ssize;
517 if (next <= start * ssize)
520 LASSERTF(range->cir_pos >= lse->lsme_extent.e_start,
521 "pos %lld, [%lld, %lld)\n", range->cir_pos,
522 lse->lsme_extent.e_start, lse->lsme_extent.e_end);
523 next = min_t(__u64, next, lse->lsme_extent.e_end);
524 next = min_t(loff_t, next, lio->lis_io_endpos);
526 io->ci_continue = next < lio->lis_io_endpos;
527 range->cir_count = next - range->cir_pos;
528 lio->lis_pos = range->cir_pos;
529 lio->lis_endpos = range->cir_pos + range->cir_count;
531 "stripe: {%d, %llu} range: [%llu, %llu) end: %llu, count: %zd\n",
532 index, start, lio->lis_pos, lio->lis_endpos,
533 lio->lis_io_endpos, range->cir_count);
535 if (!io->ci_continue) {
536 /* the last piece of IO, execute it in main thread */
541 /* it only splits IO here for parallel IO,
542 * there will be no actual IO going to occur,
543 * so it doesn't need to invoke lov_io_iter_init()
544 * to initialize sub IOs. */
545 if (!lsm_entry_inited(lsm, index)) {
546 io->ci_need_write_intent = 1;
553 * XXX The following call should be optimized: we know, that
554 * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
556 RETURN(lov_io_iter_init(env, ios));
559 static int lov_io_setattr_iter_init(const struct lu_env *env,
560 const struct cl_io_slice *ios)
562 struct lov_io *lio = cl2lov_io(env, ios);
563 struct cl_io *io = ios->cis_io;
564 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
568 if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
569 index = lov_lsm_entry(lsm, lio->lis_pos - 1);
570 if (index > 0 && !lsm_entry_inited(lsm, index)) {
571 io->ci_need_write_intent = 1;
572 RETURN(io->ci_result = -ENODATA);
576 RETURN(lov_io_iter_init(env, ios));
579 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
580 int (*iofunc)(const struct lu_env *, struct cl_io *))
582 struct cl_io *parent = lio->lis_cl.cis_io;
583 struct lov_io_sub *sub;
587 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
588 rc = iofunc(sub->sub_env, &sub->sub_io);
592 if (parent->ci_result == 0)
593 parent->ci_result = sub->sub_io.ci_result;
598 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
601 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
604 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
607 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
610 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
614 * It's possible that lov_io_start() wasn't called against this
615 * sub-io, either because previous sub-io failed, or upper layer
618 if (io->ci_state == CIS_IO_GOING)
621 io->ci_state = CIS_IO_FINISHED;
625 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
627 cl_io_iter_fini(env, io);
631 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
633 cl_io_unlock(env, io);
637 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
641 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
646 lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
648 struct lov_io *lio = cl2lov_io(env, ios);
649 struct cl_io *parent = lio->lis_cl.cis_io;
650 struct lov_io_sub *sub;
653 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
654 lov_io_end_wrapper(env, &sub->sub_io);
656 parent->u.ci_data_version.dv_data_version +=
657 sub->sub_io.u.ci_data_version.dv_data_version;
659 if (parent->ci_result == 0)
660 parent->ci_result = sub->sub_io.ci_result;
666 static void lov_io_iter_fini(const struct lu_env *env,
667 const struct cl_io_slice *ios)
669 struct lov_io *lio = cl2lov_io(env, ios);
673 rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
675 while (!list_empty(&lio->lis_active))
676 list_del_init(lio->lis_active.next);
680 static void lov_io_unlock(const struct lu_env *env,
681 const struct cl_io_slice *ios)
686 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
691 static int lov_io_read_ahead(const struct lu_env *env,
692 const struct cl_io_slice *ios,
693 pgoff_t start, struct cl_read_ahead *ra)
695 struct lov_io *lio = cl2lov_io(env, ios);
696 struct lov_object *loo = lio->lis_object;
697 struct cl_object *obj = lov2cl(loo);
698 struct lov_layout_raid0 *r0;
699 struct lov_io_sub *sub;
703 unsigned int pps; /* pages per stripe */
709 offset = cl_offset(obj, start);
710 index = lov_lsm_entry(loo->lo_lsm, offset);
711 if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index))
714 stripe = lov_stripe_number(loo->lo_lsm, index, offset);
716 r0 = lov_r0(loo, index);
717 if (unlikely(r0->lo_sub[stripe] == NULL))
720 sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
722 RETURN(PTR_ERR(sub));
724 lov_stripe_offset(loo->lo_lsm, index, offset, stripe, &suboff);
725 rc = cl_io_read_ahead(sub->sub_env, &sub->sub_io,
726 cl_index(lovsub2cl(r0->lo_sub[stripe]), suboff),
729 CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
730 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, r0->lo_nr, rc);
735 * Adjust the stripe index by layout of comp. ra->cra_end is the
736 * maximum page index covered by an underlying DLM lock.
737 * This function converts cra_end from stripe level to file level, and
738 * make sure it's not beyond stripe and component boundary.
741 /* cra_end is stripe level, convert it into file level */
742 ra_end = ra->cra_end;
743 if (ra_end != CL_PAGE_EOF)
744 ra->cra_end = lov_stripe_pgoff(loo->lo_lsm, index,
747 /* boundary of current component */
748 ra_end = cl_index(obj, (loff_t)lov_lse(loo, index)->lsme_extent.e_end);
749 if (ra_end != CL_PAGE_EOF && ra->cra_end >= ra_end)
750 ra->cra_end = ra_end - 1;
752 if (r0->lo_nr == 1) /* single stripe file */
755 pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
757 CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %u, "
758 "stripe_size = %u, stripe no = %u, start index = %lu\n",
759 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, pps, index,
760 lov_lse(loo, index)->lsme_stripe_size, stripe, start);
762 /* never exceed the end of the stripe */
763 ra->cra_end = min_t(pgoff_t,
764 ra->cra_end, start + pps - start % pps - 1);
769 * lov implementation of cl_operations::cio_submit() method. It takes a list
770 * of pages in \a queue, splits it into per-stripe sub-lists, invokes
771 * cl_io_submit() on underlying devices to submit sub-lists, and then splices
774 * Major complication of this function is a need to handle memory cleansing:
775 * cl_io_submit() is called to write out pages as a part of VM memory
776 * reclamation, and hence it may not fail due to memory shortages (system
777 * dead-locks otherwise). To deal with this, some resources (sub-lists,
778 * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
779 * not-memory cleansing context), and in case of memory shortage, these
780 * pre-allocated resources are used by lov_io_submit() under
781 * lov_device::ld_mutex mutex.
783 static int lov_io_submit(const struct lu_env *env,
784 const struct cl_io_slice *ios,
785 enum cl_req_type crt, struct cl_2queue *queue)
787 struct cl_page_list *qin = &queue->c2_qin;
788 struct lov_io *lio = cl2lov_io(env, ios);
789 struct lov_io_sub *sub;
790 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
791 struct cl_page *page;
796 if (lio->lis_nr_subios == 1) {
797 int idx = lio->lis_single_subio_index;
799 sub = lov_sub_get(env, lio, idx);
800 LASSERT(!IS_ERR(sub));
801 LASSERT(sub == &lio->lis_single_subio);
802 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
807 cl_page_list_init(plist);
808 while (qin->pl_nr > 0) {
809 struct cl_2queue *cl2q = &lov_env_info(env)->lti_cl2q;
811 cl_2queue_init(cl2q);
813 page = cl_page_list_first(qin);
814 cl_page_list_move(&cl2q->c2_qin, qin, page);
816 index = lov_page_index(page);
817 while (qin->pl_nr > 0) {
818 page = cl_page_list_first(qin);
819 if (index != lov_page_index(page))
822 cl_page_list_move(&cl2q->c2_qin, qin, page);
825 sub = lov_sub_get(env, lio, index);
827 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
833 cl_page_list_splice(&cl2q->c2_qin, plist);
834 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
835 cl_2queue_fini(env, cl2q);
841 cl_page_list_splice(plist, qin);
842 cl_page_list_fini(env, plist);
847 static int lov_io_commit_async(const struct lu_env *env,
848 const struct cl_io_slice *ios,
849 struct cl_page_list *queue, int from, int to,
852 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
853 struct lov_io *lio = cl2lov_io(env, ios);
854 struct lov_io_sub *sub;
855 struct cl_page *page;
859 if (lio->lis_nr_subios == 1) {
860 int idx = lio->lis_single_subio_index;
862 sub = lov_sub_get(env, lio, idx);
863 LASSERT(!IS_ERR(sub));
864 LASSERT(sub == &lio->lis_single_subio);
865 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io, queue,
870 cl_page_list_init(plist);
871 while (queue->pl_nr > 0) {
875 LASSERT(plist->pl_nr == 0);
876 page = cl_page_list_first(queue);
877 cl_page_list_move(plist, queue, page);
879 index = lov_page_index(page);
880 while (queue->pl_nr > 0) {
881 page = cl_page_list_first(queue);
882 if (index != lov_page_index(page))
885 cl_page_list_move(plist, queue, page);
888 if (queue->pl_nr > 0) /* still has more pages */
889 stripe_to = PAGE_SIZE;
891 sub = lov_sub_get(env, lio, index);
893 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io,
894 plist, from, stripe_to, cb);
900 if (plist->pl_nr > 0) /* short write */
906 /* for error case, add the page back into the qin list */
907 LASSERT(ergo(rc == 0, plist->pl_nr == 0));
908 while (plist->pl_nr > 0) {
909 /* error occurred, add the uncommitted pages back into queue */
910 page = cl_page_list_last(plist);
911 cl_page_list_move_head(queue, plist, page);
917 static int lov_io_fault_start(const struct lu_env *env,
918 const struct cl_io_slice *ios)
920 struct cl_fault_io *fio;
922 struct lov_io_sub *sub;
926 fio = &ios->cis_io->u.ci_fault;
927 lio = cl2lov_io(env, ios);
928 sub = lov_sub_get(env, lio, lov_page_index(fio->ft_page));
929 sub->sub_io.u.ci_fault.ft_nob = fio->ft_nob;
931 RETURN(lov_io_start(env, ios));
934 static void lov_io_fsync_end(const struct lu_env *env,
935 const struct cl_io_slice *ios)
937 struct lov_io *lio = cl2lov_io(env, ios);
938 struct lov_io_sub *sub;
939 unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
943 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
944 struct cl_io *subio = &sub->sub_io;
946 lov_io_end_wrapper(sub->sub_env, subio);
948 if (subio->ci_result == 0)
949 *written += subio->u.ci_fsync.fi_nr_written;
954 static const struct cl_io_operations lov_io_ops = {
957 .cio_fini = lov_io_fini,
958 .cio_iter_init = lov_io_rw_iter_init,
959 .cio_iter_fini = lov_io_iter_fini,
960 .cio_lock = lov_io_lock,
961 .cio_unlock = lov_io_unlock,
962 .cio_start = lov_io_start,
963 .cio_end = lov_io_end
966 .cio_fini = lov_io_fini,
967 .cio_iter_init = lov_io_rw_iter_init,
968 .cio_iter_fini = lov_io_iter_fini,
969 .cio_lock = lov_io_lock,
970 .cio_unlock = lov_io_unlock,
971 .cio_start = lov_io_start,
972 .cio_end = lov_io_end
975 .cio_fini = lov_io_fini,
976 .cio_iter_init = lov_io_setattr_iter_init,
977 .cio_iter_fini = lov_io_iter_fini,
978 .cio_lock = lov_io_lock,
979 .cio_unlock = lov_io_unlock,
980 .cio_start = lov_io_start,
981 .cio_end = lov_io_end
983 [CIT_DATA_VERSION] = {
984 .cio_fini = lov_io_fini,
985 .cio_iter_init = lov_io_iter_init,
986 .cio_iter_fini = lov_io_iter_fini,
987 .cio_lock = lov_io_lock,
988 .cio_unlock = lov_io_unlock,
989 .cio_start = lov_io_start,
990 .cio_end = lov_io_data_version_end,
993 .cio_fini = lov_io_fini,
994 .cio_iter_init = lov_io_iter_init,
995 .cio_iter_fini = lov_io_iter_fini,
996 .cio_lock = lov_io_lock,
997 .cio_unlock = lov_io_unlock,
998 .cio_start = lov_io_fault_start,
999 .cio_end = lov_io_end
1002 .cio_fini = lov_io_fini,
1003 .cio_iter_init = lov_io_iter_init,
1004 .cio_iter_fini = lov_io_iter_fini,
1005 .cio_lock = lov_io_lock,
1006 .cio_unlock = lov_io_unlock,
1007 .cio_start = lov_io_start,
1008 .cio_end = lov_io_fsync_end
1011 .cio_fini = lov_io_fini,
1012 .cio_iter_init = lov_io_iter_init,
1013 .cio_iter_fini = lov_io_iter_fini,
1014 .cio_lock = lov_io_lock,
1015 .cio_unlock = lov_io_unlock,
1016 .cio_start = lov_io_start,
1017 .cio_end = lov_io_end
1020 .cio_fini = lov_io_fini
1023 .cio_read_ahead = lov_io_read_ahead,
1024 .cio_submit = lov_io_submit,
1025 .cio_commit_async = lov_io_commit_async,
1028 /*****************************************************************************
1030 * Empty lov io operations.
1034 static void lov_empty_io_fini(const struct lu_env *env,
1035 const struct cl_io_slice *ios)
1037 struct lov_object *lov = cl2lov(ios->cis_obj);
1040 if (atomic_dec_and_test(&lov->lo_active_ios))
1041 wake_up_all(&lov->lo_waitq);
1045 static int lov_empty_io_submit(const struct lu_env *env,
1046 const struct cl_io_slice *ios,
1047 enum cl_req_type crt, struct cl_2queue *queue)
1052 static void lov_empty_impossible(const struct lu_env *env,
1053 struct cl_io_slice *ios)
1058 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
1061 * An io operation vector for files without stripes.
1063 static const struct cl_io_operations lov_empty_io_ops = {
1066 .cio_fini = lov_empty_io_fini,
1068 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1069 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1070 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1071 .cio_end = LOV_EMPTY_IMPOSSIBLE
1075 .cio_fini = lov_empty_io_fini,
1076 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1077 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1078 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1079 .cio_end = LOV_EMPTY_IMPOSSIBLE
1082 .cio_fini = lov_empty_io_fini,
1083 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1084 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1085 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1086 .cio_end = LOV_EMPTY_IMPOSSIBLE
1089 .cio_fini = lov_empty_io_fini,
1090 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1091 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1092 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1093 .cio_end = LOV_EMPTY_IMPOSSIBLE
1096 .cio_fini = lov_empty_io_fini
1099 .cio_fini = lov_empty_io_fini
1102 .cio_fini = lov_empty_io_fini
1105 .cio_submit = lov_empty_io_submit,
1106 .cio_commit_async = LOV_EMPTY_IMPOSSIBLE
1109 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
1112 struct lov_io *lio = lov_env_io(env);
1113 struct lov_object *lov = cl2lov(obj);
1116 INIT_LIST_HEAD(&lio->lis_active);
1117 io->ci_result = lov_io_slice_init(lio, lov, io);
1118 if (io->ci_result != 0)
1119 RETURN(io->ci_result);
1121 if (io->ci_result == 0) {
1122 io->ci_result = lov_io_subio_init(env, lio, io);
1123 if (io->ci_result == 0) {
1124 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
1125 atomic_inc(&lov->lo_active_ios);
1128 RETURN(io->ci_result);
1131 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
1134 struct lov_object *lov = cl2lov(obj);
1135 struct lov_io *lio = lov_env_io(env);
1139 lio->lis_object = lov;
1140 switch (io->ci_type) {
1150 case CIT_DATA_VERSION:
1158 CERROR("Page fault on a file without stripes: "DFID"\n",
1159 PFID(lu_object_fid(&obj->co_lu)));
1163 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1164 atomic_inc(&lov->lo_active_ios);
1167 io->ci_result = result < 0 ? result : 0;
1171 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
1174 struct lov_object *lov = cl2lov(obj);
1175 struct lov_io *lio = lov_env_io(env);
1179 LASSERT(lov->lo_lsm != NULL);
1180 lio->lis_object = lov;
1182 switch (io->ci_type) {
1184 LASSERTF(0, "invalid type %d\n", io->ci_type);
1185 result = -EOPNOTSUPP;
1190 case CIT_DATA_VERSION:
1194 /* the truncate to 0 is managed by MDT:
1195 * - in open, for open O_TRUNC
1196 * - in setattr, for truncate
1198 /* the truncate is for size > 0 so triggers a restore */
1199 if (cl_io_is_trunc(io)) {
1200 io->ci_restore_needed = 1;
1208 io->ci_restore_needed = 1;
1214 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1215 atomic_inc(&lov->lo_active_ios);
1218 io->ci_result = result < 0 ? result : 0;