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;
126 sub_io->ci_lock_no_expand = io->ci_lock_no_expand;
128 result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
131 lov_io_sub_fini(env, lio, sub);
136 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
137 struct lov_io *lio, int index)
139 struct lov_io_sub *sub;
144 list_for_each_entry(sub, &lio->lis_subios, sub_list) {
145 if (sub->sub_subio_index == index) {
152 sub = lov_sub_alloc(lio, index);
154 GOTO(out, rc = -ENOMEM);
156 rc = lov_io_sub_init(env, lio, sub);
158 lov_sub_free(lio, sub);
162 list_add_tail(&sub->sub_list, &lio->lis_subios);
163 lio->lis_nr_subios++;
171 /*****************************************************************************
177 int lov_page_index(const struct cl_page *page)
179 const struct cl_page_slice *slice;
182 slice = cl_page_at(page, &lov_device_type);
183 LASSERT(slice != NULL);
184 LASSERT(slice->cpl_obj != NULL);
186 RETURN(cl2lov_page(slice)->lps_index);
189 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
194 LASSERT(lio->lis_object != NULL);
196 INIT_LIST_HEAD(&lio->lis_subios);
197 lio->lis_single_subio_index = -1;
198 lio->lis_nr_subios = 0;
203 static int lov_io_slice_init(struct lov_io *lio,
204 struct lov_object *obj, struct cl_io *io)
209 lio->lis_object = obj;
211 LASSERT(obj->lo_lsm != NULL);
213 switch (io->ci_type) {
216 lio->lis_pos = io->u.ci_rw.rw_range.cir_pos;
217 lio->lis_endpos = lio->lis_pos + io->u.ci_rw.rw_range.cir_count;
218 lio->lis_io_endpos = lio->lis_endpos;
219 if (cl_io_is_append(io)) {
220 LASSERT(io->ci_type == CIT_WRITE);
222 /* If there is LOV EA hole, then we may cannot locate
223 * the current file-tail exactly. */
224 if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
229 lio->lis_endpos = OBD_OBJECT_EOF;
234 if (cl_io_is_trunc(io))
235 lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
238 lio->lis_endpos = OBD_OBJECT_EOF;
241 case CIT_DATA_VERSION:
243 lio->lis_endpos = OBD_OBJECT_EOF;
247 pgoff_t index = io->u.ci_fault.ft_index;
248 lio->lis_pos = cl_offset(io->ci_obj, index);
249 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
254 lio->lis_pos = io->u.ci_fsync.fi_start;
255 lio->lis_endpos = io->u.ci_fsync.fi_end;
260 lio->lis_pos = io->u.ci_ladvise.li_start;
261 lio->lis_endpos = io->u.ci_ladvise.li_end;
267 lio->lis_endpos = OBD_OBJECT_EOF;
277 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
279 struct lov_io *lio = cl2lov_io(env, ios);
280 struct lov_object *lov = cl2lov(ios->cis_obj);
284 LASSERT(list_empty(&lio->lis_active));
286 while (!list_empty(&lio->lis_subios)) {
287 struct lov_io_sub *sub = list_entry(lio->lis_subios.next,
291 list_del_init(&sub->sub_list);
292 lio->lis_nr_subios--;
294 lov_io_sub_fini(env, lio, sub);
295 lov_sub_free(lio, sub);
297 LASSERT(lio->lis_nr_subios == 0);
299 LASSERT(atomic_read(&lov->lo_active_ios) > 0);
300 if (atomic_dec_and_test(&lov->lo_active_ios))
301 wake_up_all(&lov->lo_waitq);
305 static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
306 loff_t start, loff_t end)
308 struct cl_io *io = &sub->sub_io;
309 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
310 struct cl_io *parent = lio->lis_cl.cis_io;
311 int index = lov_comp_entry(sub->sub_subio_index);
312 int stripe = lov_comp_stripe(sub->sub_subio_index);
314 io->ci_pio = parent->ci_pio;
315 switch (io->ci_type) {
317 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
318 io->u.ci_setattr.sa_attr_flags =
319 parent->u.ci_setattr.sa_attr_flags;
320 io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
321 io->u.ci_setattr.sa_stripe_index = stripe;
322 io->u.ci_setattr.sa_parent_fid =
323 parent->u.ci_setattr.sa_parent_fid;
324 if (cl_io_is_trunc(io)) {
325 loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
327 new_size = lov_size_to_stripe(lsm, index, new_size,
329 io->u.ci_setattr.sa_attr.lvb_size = new_size;
331 lov_lsm2layout(lsm, lsm->lsm_entries[index],
332 &io->u.ci_setattr.sa_layout);
335 case CIT_DATA_VERSION: {
336 io->u.ci_data_version.dv_data_version = 0;
337 io->u.ci_data_version.dv_flags =
338 parent->u.ci_data_version.dv_flags;
342 struct cl_object *obj = parent->ci_obj;
343 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
345 io->u.ci_fault = parent->u.ci_fault;
346 off = lov_size_to_stripe(lsm, index, off, stripe);
347 io->u.ci_fault.ft_index = cl_index(obj, off);
351 io->u.ci_fsync.fi_start = start;
352 io->u.ci_fsync.fi_end = end;
353 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
354 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
359 io->u.ci_rw.rw_ptask = parent->u.ci_rw.rw_ptask;
360 io->u.ci_rw.rw_iter = parent->u.ci_rw.rw_iter;
361 io->u.ci_rw.rw_iocb = parent->u.ci_rw.rw_iocb;
362 io->u.ci_rw.rw_file = parent->u.ci_rw.rw_file;
363 io->u.ci_rw.rw_sync = parent->u.ci_rw.rw_sync;
364 if (cl_io_is_append(parent)) {
365 io->u.ci_rw.rw_append = 1;
367 io->u.ci_rw.rw_range.cir_pos = start;
368 io->u.ci_rw.rw_range.cir_count = end - start;
373 io->u.ci_ladvise.li_start = start;
374 io->u.ci_ladvise.li_end = end;
375 io->u.ci_ladvise.li_fid = parent->u.ci_ladvise.li_fid;
376 io->u.ci_ladvise.li_advice = parent->u.ci_ladvise.li_advice;
377 io->u.ci_ladvise.li_flags = parent->u.ci_ladvise.li_flags;
385 static loff_t lov_offset_mod(loff_t val, int delta)
387 if (val != OBD_OBJECT_EOF)
392 static int lov_io_iter_init(const struct lu_env *env,
393 const struct cl_io_slice *ios)
395 struct cl_io *io = ios->cis_io;
396 struct lov_io *lio = cl2lov_io(env, ios);
397 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
398 struct lov_io_sub *sub;
399 struct lov_layout_entry *le;
400 struct lu_extent ext;
406 ext.e_start = lio->lis_pos;
407 ext.e_end = lio->lis_endpos;
410 lov_foreach_layout_entry(lio->lis_object, le) {
411 struct lov_layout_raid0 *r0 = &le->lle_raid0;
417 if (!lu_extent_is_overlapped(&ext, &le->lle_extent))
420 CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
421 index - 1, lsm->lsm_entries[index - 1]->lsme_flags);
422 if (!lsm_entry_inited(lsm, index - 1)) {
423 /* truncate IO will trigger write intent as well, and
424 * it's handled in lov_io_setattr_iter_init() */
425 if (io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io)) {
426 io->ci_need_write_intent = 1;
427 /* execute it in main thread */
433 /* Read from uninitialized components should return
434 * zero filled pages. */
438 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
439 if (!lov_stripe_intersects(lsm, index - 1, stripe,
443 if (unlikely(r0->lo_sub[stripe] == NULL)) {
444 if (ios->cis_io->ci_type == CIT_READ ||
445 ios->cis_io->ci_type == CIT_WRITE ||
446 ios->cis_io->ci_type == CIT_FAULT)
452 end = lov_offset_mod(end, 1);
453 sub = lov_sub_get(env, lio,
454 lov_comp_index(index - 1, stripe));
460 lov_io_sub_inherit(sub, lio, start, end);
461 rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
463 cl_io_iter_fini(sub->sub_env, &sub->sub_io);
468 "shrink stripe: {%d, %d} range: [%llu, %llu)\n",
469 index, stripe, start, end);
471 list_add_tail(&sub->sub_linkage, &lio->lis_active);
479 static int lov_io_rw_iter_init(const struct lu_env *env,
480 const struct cl_io_slice *ios)
482 struct cl_io *io = ios->cis_io;
483 struct lov_io *lio = cl2lov_io(env, ios);
484 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
485 struct lov_stripe_md_entry *lse;
486 struct cl_io_range *range = &io->u.ci_rw.rw_range;
487 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 next = MAX_LFS_FILESIZE;
515 if (lse->lsme_stripe_count > 1) {
516 unsigned long ssize = lse->lsme_stripe_size;
518 lov_do_div64(start, ssize);
519 next = (start + 1) * ssize;
520 if (next <= start * ssize)
521 next = MAX_LFS_FILESIZE;
524 LASSERTF(range->cir_pos >= lse->lsme_extent.e_start,
525 "pos %lld, [%lld, %lld)\n", range->cir_pos,
526 lse->lsme_extent.e_start, lse->lsme_extent.e_end);
527 next = min_t(__u64, next, lse->lsme_extent.e_end);
528 next = min_t(loff_t, next, lio->lis_io_endpos);
530 io->ci_continue = next < lio->lis_io_endpos;
531 range->cir_count = next - range->cir_pos;
532 lio->lis_pos = range->cir_pos;
533 lio->lis_endpos = range->cir_pos + range->cir_count;
535 "stripe: {%d, %llu} range: [%llu, %llu) end: %llu, count: %zd\n",
536 index, start, lio->lis_pos, lio->lis_endpos,
537 lio->lis_io_endpos, range->cir_count);
539 if (!io->ci_continue) {
540 /* the last piece of IO, execute it in main thread */
545 /* it only splits IO here for parallel IO,
546 * there will be no actual IO going to occur,
547 * so it doesn't need to invoke lov_io_iter_init()
548 * to initialize sub IOs. */
549 if (!lsm_entry_inited(lsm, index)) {
550 io->ci_need_write_intent = 1;
557 * XXX The following call should be optimized: we know, that
558 * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
560 RETURN(lov_io_iter_init(env, ios));
563 static int lov_io_setattr_iter_init(const struct lu_env *env,
564 const struct cl_io_slice *ios)
566 struct lov_io *lio = cl2lov_io(env, ios);
567 struct cl_io *io = ios->cis_io;
568 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
572 if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
573 index = lov_lsm_entry(lsm, lio->lis_pos - 1);
574 if (index > 0 && !lsm_entry_inited(lsm, index)) {
575 io->ci_need_write_intent = 1;
576 RETURN(io->ci_result = -ENODATA);
580 RETURN(lov_io_iter_init(env, ios));
583 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
584 int (*iofunc)(const struct lu_env *, struct cl_io *))
586 struct cl_io *parent = lio->lis_cl.cis_io;
587 struct lov_io_sub *sub;
591 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
592 rc = iofunc(sub->sub_env, &sub->sub_io);
596 if (parent->ci_result == 0)
597 parent->ci_result = sub->sub_io.ci_result;
602 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
605 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
608 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
611 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
614 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
618 * It's possible that lov_io_start() wasn't called against this
619 * sub-io, either because previous sub-io failed, or upper layer
622 if (io->ci_state == CIS_IO_GOING)
625 io->ci_state = CIS_IO_FINISHED;
629 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
631 cl_io_iter_fini(env, io);
635 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
637 cl_io_unlock(env, io);
641 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
645 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
650 lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
652 struct lov_io *lio = cl2lov_io(env, ios);
653 struct cl_io *parent = lio->lis_cl.cis_io;
654 struct lov_io_sub *sub;
657 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
658 lov_io_end_wrapper(env, &sub->sub_io);
660 parent->u.ci_data_version.dv_data_version +=
661 sub->sub_io.u.ci_data_version.dv_data_version;
663 if (parent->ci_result == 0)
664 parent->ci_result = sub->sub_io.ci_result;
670 static void lov_io_iter_fini(const struct lu_env *env,
671 const struct cl_io_slice *ios)
673 struct lov_io *lio = cl2lov_io(env, ios);
677 rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
679 while (!list_empty(&lio->lis_active))
680 list_del_init(lio->lis_active.next);
684 static void lov_io_unlock(const struct lu_env *env,
685 const struct cl_io_slice *ios)
690 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
695 static int lov_io_read_ahead(const struct lu_env *env,
696 const struct cl_io_slice *ios,
697 pgoff_t start, struct cl_read_ahead *ra)
699 struct lov_io *lio = cl2lov_io(env, ios);
700 struct lov_object *loo = lio->lis_object;
701 struct cl_object *obj = lov2cl(loo);
702 struct lov_layout_raid0 *r0;
703 struct lov_io_sub *sub;
707 unsigned int pps; /* pages per stripe */
713 offset = cl_offset(obj, start);
714 index = lov_lsm_entry(loo->lo_lsm, offset);
715 if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index))
718 stripe = lov_stripe_number(loo->lo_lsm, index, offset);
720 r0 = lov_r0(loo, index);
721 if (unlikely(r0->lo_sub[stripe] == NULL))
724 sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
726 RETURN(PTR_ERR(sub));
728 lov_stripe_offset(loo->lo_lsm, index, offset, stripe, &suboff);
729 rc = cl_io_read_ahead(sub->sub_env, &sub->sub_io,
730 cl_index(lovsub2cl(r0->lo_sub[stripe]), suboff),
733 CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
734 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, r0->lo_nr, rc);
739 * Adjust the stripe index by layout of comp. ra->cra_end is the
740 * maximum page index covered by an underlying DLM lock.
741 * This function converts cra_end from stripe level to file level, and
742 * make sure it's not beyond stripe and component boundary.
745 /* cra_end is stripe level, convert it into file level */
746 ra_end = ra->cra_end;
747 if (ra_end != CL_PAGE_EOF)
748 ra->cra_end = lov_stripe_pgoff(loo->lo_lsm, index,
751 /* boundary of current component */
752 ra_end = cl_index(obj, (loff_t)lov_lse(loo, index)->lsme_extent.e_end);
753 if (ra_end != CL_PAGE_EOF && ra->cra_end >= ra_end)
754 ra->cra_end = ra_end - 1;
756 if (r0->lo_nr == 1) /* single stripe file */
759 pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
761 CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %u, "
762 "stripe_size = %u, stripe no = %u, start index = %lu\n",
763 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, pps, index,
764 lov_lse(loo, index)->lsme_stripe_size, stripe, start);
766 /* never exceed the end of the stripe */
767 ra->cra_end = min_t(pgoff_t,
768 ra->cra_end, start + pps - start % pps - 1);
773 * lov implementation of cl_operations::cio_submit() method. It takes a list
774 * of pages in \a queue, splits it into per-stripe sub-lists, invokes
775 * cl_io_submit() on underlying devices to submit sub-lists, and then splices
778 * Major complication of this function is a need to handle memory cleansing:
779 * cl_io_submit() is called to write out pages as a part of VM memory
780 * reclamation, and hence it may not fail due to memory shortages (system
781 * dead-locks otherwise). To deal with this, some resources (sub-lists,
782 * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
783 * not-memory cleansing context), and in case of memory shortage, these
784 * pre-allocated resources are used by lov_io_submit() under
785 * lov_device::ld_mutex mutex.
787 static int lov_io_submit(const struct lu_env *env,
788 const struct cl_io_slice *ios,
789 enum cl_req_type crt, struct cl_2queue *queue)
791 struct cl_page_list *qin = &queue->c2_qin;
792 struct lov_io *lio = cl2lov_io(env, ios);
793 struct lov_io_sub *sub;
794 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
795 struct cl_page *page;
800 if (lio->lis_nr_subios == 1) {
801 int idx = lio->lis_single_subio_index;
803 sub = lov_sub_get(env, lio, idx);
804 LASSERT(!IS_ERR(sub));
805 LASSERT(sub == &lio->lis_single_subio);
806 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
811 cl_page_list_init(plist);
812 while (qin->pl_nr > 0) {
813 struct cl_2queue *cl2q = &lov_env_info(env)->lti_cl2q;
815 cl_2queue_init(cl2q);
817 page = cl_page_list_first(qin);
818 cl_page_list_move(&cl2q->c2_qin, qin, page);
820 index = lov_page_index(page);
821 while (qin->pl_nr > 0) {
822 page = cl_page_list_first(qin);
823 if (index != lov_page_index(page))
826 cl_page_list_move(&cl2q->c2_qin, qin, page);
829 sub = lov_sub_get(env, lio, index);
831 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
837 cl_page_list_splice(&cl2q->c2_qin, plist);
838 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
839 cl_2queue_fini(env, cl2q);
845 cl_page_list_splice(plist, qin);
846 cl_page_list_fini(env, plist);
851 static int lov_io_commit_async(const struct lu_env *env,
852 const struct cl_io_slice *ios,
853 struct cl_page_list *queue, int from, int to,
856 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
857 struct lov_io *lio = cl2lov_io(env, ios);
858 struct lov_io_sub *sub;
859 struct cl_page *page;
863 if (lio->lis_nr_subios == 1) {
864 int idx = lio->lis_single_subio_index;
866 sub = lov_sub_get(env, lio, idx);
867 LASSERT(!IS_ERR(sub));
868 LASSERT(sub == &lio->lis_single_subio);
869 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io, queue,
874 cl_page_list_init(plist);
875 while (queue->pl_nr > 0) {
879 LASSERT(plist->pl_nr == 0);
880 page = cl_page_list_first(queue);
881 cl_page_list_move(plist, queue, page);
883 index = lov_page_index(page);
884 while (queue->pl_nr > 0) {
885 page = cl_page_list_first(queue);
886 if (index != lov_page_index(page))
889 cl_page_list_move(plist, queue, page);
892 if (queue->pl_nr > 0) /* still has more pages */
893 stripe_to = PAGE_SIZE;
895 sub = lov_sub_get(env, lio, index);
897 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io,
898 plist, from, stripe_to, cb);
904 if (plist->pl_nr > 0) /* short write */
910 /* for error case, add the page back into the qin list */
911 LASSERT(ergo(rc == 0, plist->pl_nr == 0));
912 while (plist->pl_nr > 0) {
913 /* error occurred, add the uncommitted pages back into queue */
914 page = cl_page_list_last(plist);
915 cl_page_list_move_head(queue, plist, page);
921 static int lov_io_fault_start(const struct lu_env *env,
922 const struct cl_io_slice *ios)
924 struct cl_fault_io *fio;
926 struct lov_io_sub *sub;
930 fio = &ios->cis_io->u.ci_fault;
931 lio = cl2lov_io(env, ios);
932 sub = lov_sub_get(env, lio, lov_page_index(fio->ft_page));
933 sub->sub_io.u.ci_fault.ft_nob = fio->ft_nob;
935 RETURN(lov_io_start(env, ios));
938 static void lov_io_fsync_end(const struct lu_env *env,
939 const struct cl_io_slice *ios)
941 struct lov_io *lio = cl2lov_io(env, ios);
942 struct lov_io_sub *sub;
943 unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
947 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
948 struct cl_io *subio = &sub->sub_io;
950 lov_io_end_wrapper(sub->sub_env, subio);
952 if (subio->ci_result == 0)
953 *written += subio->u.ci_fsync.fi_nr_written;
958 static const struct cl_io_operations lov_io_ops = {
961 .cio_fini = lov_io_fini,
962 .cio_iter_init = lov_io_rw_iter_init,
963 .cio_iter_fini = lov_io_iter_fini,
964 .cio_lock = lov_io_lock,
965 .cio_unlock = lov_io_unlock,
966 .cio_start = lov_io_start,
967 .cio_end = lov_io_end
970 .cio_fini = lov_io_fini,
971 .cio_iter_init = lov_io_rw_iter_init,
972 .cio_iter_fini = lov_io_iter_fini,
973 .cio_lock = lov_io_lock,
974 .cio_unlock = lov_io_unlock,
975 .cio_start = lov_io_start,
976 .cio_end = lov_io_end
979 .cio_fini = lov_io_fini,
980 .cio_iter_init = lov_io_setattr_iter_init,
981 .cio_iter_fini = lov_io_iter_fini,
982 .cio_lock = lov_io_lock,
983 .cio_unlock = lov_io_unlock,
984 .cio_start = lov_io_start,
985 .cio_end = lov_io_end
987 [CIT_DATA_VERSION] = {
988 .cio_fini = lov_io_fini,
989 .cio_iter_init = lov_io_iter_init,
990 .cio_iter_fini = lov_io_iter_fini,
991 .cio_lock = lov_io_lock,
992 .cio_unlock = lov_io_unlock,
993 .cio_start = lov_io_start,
994 .cio_end = lov_io_data_version_end,
997 .cio_fini = lov_io_fini,
998 .cio_iter_init = lov_io_iter_init,
999 .cio_iter_fini = lov_io_iter_fini,
1000 .cio_lock = lov_io_lock,
1001 .cio_unlock = lov_io_unlock,
1002 .cio_start = lov_io_fault_start,
1003 .cio_end = lov_io_end
1006 .cio_fini = lov_io_fini,
1007 .cio_iter_init = lov_io_iter_init,
1008 .cio_iter_fini = lov_io_iter_fini,
1009 .cio_lock = lov_io_lock,
1010 .cio_unlock = lov_io_unlock,
1011 .cio_start = lov_io_start,
1012 .cio_end = lov_io_fsync_end
1015 .cio_fini = lov_io_fini,
1016 .cio_iter_init = lov_io_iter_init,
1017 .cio_iter_fini = lov_io_iter_fini,
1018 .cio_lock = lov_io_lock,
1019 .cio_unlock = lov_io_unlock,
1020 .cio_start = lov_io_start,
1021 .cio_end = lov_io_end
1024 .cio_fini = lov_io_fini
1027 .cio_read_ahead = lov_io_read_ahead,
1028 .cio_submit = lov_io_submit,
1029 .cio_commit_async = lov_io_commit_async,
1032 /*****************************************************************************
1034 * Empty lov io operations.
1038 static void lov_empty_io_fini(const struct lu_env *env,
1039 const struct cl_io_slice *ios)
1041 struct lov_object *lov = cl2lov(ios->cis_obj);
1044 if (atomic_dec_and_test(&lov->lo_active_ios))
1045 wake_up_all(&lov->lo_waitq);
1049 static int lov_empty_io_submit(const struct lu_env *env,
1050 const struct cl_io_slice *ios,
1051 enum cl_req_type crt, struct cl_2queue *queue)
1056 static void lov_empty_impossible(const struct lu_env *env,
1057 struct cl_io_slice *ios)
1062 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
1065 * An io operation vector for files without stripes.
1067 static const struct cl_io_operations lov_empty_io_ops = {
1070 .cio_fini = lov_empty_io_fini,
1072 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1073 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1074 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1075 .cio_end = LOV_EMPTY_IMPOSSIBLE
1079 .cio_fini = lov_empty_io_fini,
1080 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1081 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1082 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1083 .cio_end = LOV_EMPTY_IMPOSSIBLE
1086 .cio_fini = lov_empty_io_fini,
1087 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1088 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1089 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1090 .cio_end = LOV_EMPTY_IMPOSSIBLE
1093 .cio_fini = lov_empty_io_fini,
1094 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1095 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1096 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1097 .cio_end = LOV_EMPTY_IMPOSSIBLE
1100 .cio_fini = lov_empty_io_fini
1103 .cio_fini = lov_empty_io_fini
1106 .cio_fini = lov_empty_io_fini
1109 .cio_submit = lov_empty_io_submit,
1110 .cio_commit_async = LOV_EMPTY_IMPOSSIBLE
1113 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
1116 struct lov_io *lio = lov_env_io(env);
1117 struct lov_object *lov = cl2lov(obj);
1120 INIT_LIST_HEAD(&lio->lis_active);
1121 io->ci_result = lov_io_slice_init(lio, lov, io);
1122 if (io->ci_result != 0)
1123 RETURN(io->ci_result);
1125 if (io->ci_result == 0) {
1126 io->ci_result = lov_io_subio_init(env, lio, io);
1127 if (io->ci_result == 0) {
1128 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
1129 atomic_inc(&lov->lo_active_ios);
1132 RETURN(io->ci_result);
1135 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
1138 struct lov_object *lov = cl2lov(obj);
1139 struct lov_io *lio = lov_env_io(env);
1143 lio->lis_object = lov;
1144 switch (io->ci_type) {
1154 case CIT_DATA_VERSION:
1162 CERROR("Page fault on a file without stripes: "DFID"\n",
1163 PFID(lu_object_fid(&obj->co_lu)));
1167 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1168 atomic_inc(&lov->lo_active_ios);
1171 io->ci_result = result < 0 ? result : 0;
1175 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
1178 struct lov_object *lov = cl2lov(obj);
1179 struct lov_io *lio = lov_env_io(env);
1183 LASSERT(lov->lo_lsm != NULL);
1184 lio->lis_object = lov;
1186 switch (io->ci_type) {
1188 LASSERTF(0, "invalid type %d\n", io->ci_type);
1189 result = -EOPNOTSUPP;
1194 case CIT_DATA_VERSION:
1198 /* the truncate to 0 is managed by MDT:
1199 * - in open, for open O_TRUNC
1200 * - in setattr, for truncate
1202 /* the truncate is for size > 0 so triggers a restore */
1203 if (cl_io_is_trunc(io)) {
1204 io->ci_restore_needed = 1;
1212 io->ci_restore_needed = 1;
1218 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1219 atomic_inc(&lov->lo_active_ios);
1222 io->ci_result = result < 0 ? result : 0;