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;
126 result = cl_io_sub_init(sub->sub_env, sub_io, io->ci_type, sub_obj);
129 lov_io_sub_fini(env, lio, sub);
134 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
135 struct lov_io *lio, int index)
137 struct lov_io_sub *sub;
142 list_for_each_entry(sub, &lio->lis_subios, sub_list) {
143 if (sub->sub_subio_index == index) {
150 sub = lov_sub_alloc(lio, index);
152 GOTO(out, rc = -ENOMEM);
154 rc = lov_io_sub_init(env, lio, sub);
156 lov_sub_free(lio, sub);
160 list_add_tail(&sub->sub_list, &lio->lis_subios);
161 lio->lis_nr_subios++;
169 /*****************************************************************************
175 int lov_page_index(const struct cl_page *page)
177 const struct cl_page_slice *slice;
180 slice = cl_page_at(page, &lov_device_type);
181 LASSERT(slice != NULL);
182 LASSERT(slice->cpl_obj != NULL);
184 RETURN(cl2lov_page(slice)->lps_index);
187 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
192 LASSERT(lio->lis_object != NULL);
194 INIT_LIST_HEAD(&lio->lis_subios);
195 lio->lis_single_subio_index = -1;
196 lio->lis_nr_subios = 0;
201 static int lov_io_slice_init(struct lov_io *lio,
202 struct lov_object *obj, struct cl_io *io)
207 lio->lis_object = obj;
209 LASSERT(obj->lo_lsm != NULL);
211 switch (io->ci_type) {
214 lio->lis_pos = io->u.ci_rw.crw_pos;
215 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
216 lio->lis_io_endpos = lio->lis_endpos;
217 if (cl_io_is_append(io)) {
218 LASSERT(io->ci_type == CIT_WRITE);
220 /* If there is LOV EA hole, then we may cannot locate
221 * the current file-tail exactly. */
222 if (unlikely(obj->lo_lsm->lsm_entries[0]->lsme_pattern &
227 lio->lis_endpos = OBD_OBJECT_EOF;
232 if (cl_io_is_trunc(io))
233 lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
236 lio->lis_endpos = OBD_OBJECT_EOF;
239 case CIT_DATA_VERSION:
241 lio->lis_endpos = OBD_OBJECT_EOF;
245 pgoff_t index = io->u.ci_fault.ft_index;
246 lio->lis_pos = cl_offset(io->ci_obj, index);
247 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
252 lio->lis_pos = io->u.ci_fsync.fi_start;
253 lio->lis_endpos = io->u.ci_fsync.fi_end;
258 lio->lis_pos = io->u.ci_ladvise.li_start;
259 lio->lis_endpos = io->u.ci_ladvise.li_end;
265 lio->lis_endpos = OBD_OBJECT_EOF;
275 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
277 struct lov_io *lio = cl2lov_io(env, ios);
278 struct lov_object *lov = cl2lov(ios->cis_obj);
282 LASSERT(list_empty(&lio->lis_active));
284 while (!list_empty(&lio->lis_subios)) {
285 struct lov_io_sub *sub = list_entry(lio->lis_subios.next,
289 list_del_init(&sub->sub_list);
290 lio->lis_nr_subios--;
292 lov_io_sub_fini(env, lio, sub);
293 lov_sub_free(lio, sub);
295 LASSERT(lio->lis_nr_subios == 0);
297 LASSERT(atomic_read(&lov->lo_active_ios) > 0);
298 if (atomic_dec_and_test(&lov->lo_active_ios))
299 wake_up_all(&lov->lo_waitq);
303 static void lov_io_sub_inherit(struct lov_io_sub *sub, struct lov_io *lio,
304 loff_t start, loff_t end)
306 struct cl_io *io = &sub->sub_io;
307 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
308 struct cl_io *parent = lio->lis_cl.cis_io;
309 int index = lov_comp_entry(sub->sub_subio_index);
310 int stripe = lov_comp_stripe(sub->sub_subio_index);
312 switch (io->ci_type) {
314 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
315 io->u.ci_setattr.sa_attr_flags =
316 parent->u.ci_setattr.sa_attr_flags;
317 io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
318 io->u.ci_setattr.sa_stripe_index = stripe;
319 io->u.ci_setattr.sa_parent_fid =
320 parent->u.ci_setattr.sa_parent_fid;
321 if (cl_io_is_trunc(io)) {
322 loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
324 new_size = lov_size_to_stripe(lsm, index, new_size,
326 io->u.ci_setattr.sa_attr.lvb_size = new_size;
328 lov_lsm2layout(lsm, lsm->lsm_entries[index],
329 &io->u.ci_setattr.sa_layout);
332 case CIT_DATA_VERSION: {
333 io->u.ci_data_version.dv_data_version = 0;
334 io->u.ci_data_version.dv_flags =
335 parent->u.ci_data_version.dv_flags;
339 struct cl_object *obj = parent->ci_obj;
340 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
342 io->u.ci_fault = parent->u.ci_fault;
343 off = lov_size_to_stripe(lsm, index, off, stripe);
344 io->u.ci_fault.ft_index = cl_index(obj, off);
348 io->u.ci_fsync.fi_start = start;
349 io->u.ci_fsync.fi_end = end;
350 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
351 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
356 io->u.ci_wr.wr_sync = cl_io_is_sync_write(parent);
357 if (cl_io_is_append(parent)) {
358 io->u.ci_wr.wr_append = 1;
360 io->u.ci_rw.crw_pos = start;
361 io->u.ci_rw.crw_count = end - start;
366 io->u.ci_ladvise.li_start = start;
367 io->u.ci_ladvise.li_end = end;
368 io->u.ci_ladvise.li_fid = parent->u.ci_ladvise.li_fid;
369 io->u.ci_ladvise.li_advice = parent->u.ci_ladvise.li_advice;
370 io->u.ci_ladvise.li_flags = parent->u.ci_ladvise.li_flags;
378 static loff_t lov_offset_mod(loff_t val, int delta)
380 if (val != OBD_OBJECT_EOF)
385 static int lov_io_iter_init(const struct lu_env *env,
386 const struct cl_io_slice *ios)
388 struct cl_io *io = ios->cis_io;
389 struct lov_io *lio = cl2lov_io(env, ios);
390 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
391 struct lov_io_sub *sub;
392 struct lov_layout_entry *le;
393 struct lu_extent ext;
399 ext.e_start = lio->lis_pos;
400 ext.e_end = lio->lis_endpos;
403 lov_foreach_layout_entry(lio->lis_object, le) {
404 struct lov_layout_raid0 *r0 = &le->lle_raid0;
410 if (!lu_extent_is_overlapped(&ext, &le->lle_extent))
413 CDEBUG(D_VFSTRACE, "component[%d] flags %#x\n",
414 index - 1, lsm->lsm_entries[index - 1]->lsme_flags);
415 if (!lsm_entry_inited(lsm, index - 1)) {
416 /* truncate IO will trigger write intent as well, and
417 * it's handled in lov_io_setattr_iter_init() */
418 if (io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io)) {
419 io->ci_need_write_intent = 1;
424 /* Read from uninitialized components should return
425 * zero filled pages. */
429 for (stripe = 0; stripe < r0->lo_nr; stripe++) {
430 if (!lov_stripe_intersects(lsm, index - 1, stripe,
434 if (unlikely(r0->lo_sub[stripe] == NULL)) {
435 if (ios->cis_io->ci_type == CIT_READ ||
436 ios->cis_io->ci_type == CIT_WRITE ||
437 ios->cis_io->ci_type == CIT_FAULT)
443 end = lov_offset_mod(end, 1);
444 sub = lov_sub_get(env, lio,
445 lov_comp_index(index - 1, stripe));
451 lov_io_sub_inherit(sub, lio, start, end);
452 rc = cl_io_iter_init(sub->sub_env, &sub->sub_io);
454 cl_io_iter_fini(sub->sub_env, &sub->sub_io);
458 CDEBUG(D_VFSTRACE, "shrink: %d [%llu, %llu)\n",
461 list_add_tail(&sub->sub_linkage, &lio->lis_active);
469 static int lov_io_rw_iter_init(const struct lu_env *env,
470 const struct cl_io_slice *ios)
472 struct lov_io *lio = cl2lov_io(env, ios);
473 struct cl_io *io = ios->cis_io;
474 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
475 struct lov_stripe_md_entry *lse;
476 loff_t start = io->u.ci_rw.crw_pos;
481 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
484 if (cl_io_is_append(io))
485 RETURN(lov_io_iter_init(env, ios));
487 index = lov_lsm_entry(lsm, io->u.ci_rw.crw_pos);
488 if (index < 0) { /* non-existing layout component */
489 if (io->ci_type == CIT_READ) {
490 /* TODO: it needs to detect the next component and
491 * then set the next pos */
494 RETURN(lov_io_iter_init(env, ios));
500 lse = lov_lse(lio->lis_object, index);
502 ssize = lse->lsme_stripe_size;
503 lov_do_div64(start, ssize);
504 next = (start + 1) * ssize;
505 if (next <= start * ssize)
508 LASSERTF(io->u.ci_rw.crw_pos >= lse->lsme_extent.e_start,
509 "pos %lld, [%lld, %lld)\n", io->u.ci_rw.crw_pos,
510 lse->lsme_extent.e_start, lse->lsme_extent.e_end);
511 next = min_t(__u64, next, lse->lsme_extent.e_end);
512 next = min_t(loff_t, next, lio->lis_io_endpos);
514 io->ci_continue = next < lio->lis_io_endpos;
515 io->u.ci_rw.crw_count = next - io->u.ci_rw.crw_pos;
516 lio->lis_pos = io->u.ci_rw.crw_pos;
517 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
519 "stripe: %llu chunk: [%llu, %llu) %llu, %zd\n",
520 (__u64)start, lio->lis_pos, lio->lis_endpos,
521 (__u64)lio->lis_io_endpos, io->u.ci_rw.crw_count);
524 * XXX The following call should be optimized: we know, that
525 * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
527 RETURN(lov_io_iter_init(env, ios));
530 static int lov_io_setattr_iter_init(const struct lu_env *env,
531 const struct cl_io_slice *ios)
533 struct lov_io *lio = cl2lov_io(env, ios);
534 struct cl_io *io = ios->cis_io;
535 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
539 if (cl_io_is_trunc(io) && lio->lis_pos > 0) {
540 index = lov_lsm_entry(lsm, lio->lis_pos - 1);
541 if (index > 0 && !lsm_entry_inited(lsm, index)) {
542 io->ci_need_write_intent = 1;
543 RETURN(io->ci_result = -ENODATA);
547 RETURN(lov_io_iter_init(env, ios));
550 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
551 int (*iofunc)(const struct lu_env *, struct cl_io *))
553 struct cl_io *parent = lio->lis_cl.cis_io;
554 struct lov_io_sub *sub;
558 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
559 rc = iofunc(sub->sub_env, &sub->sub_io);
563 if (parent->ci_result == 0)
564 parent->ci_result = sub->sub_io.ci_result;
569 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
572 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
575 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
578 RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
581 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
585 * It's possible that lov_io_start() wasn't called against this
586 * sub-io, either because previous sub-io failed, or upper layer
589 if (io->ci_state == CIS_IO_GOING)
592 io->ci_state = CIS_IO_FINISHED;
596 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
598 cl_io_iter_fini(env, io);
602 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
604 cl_io_unlock(env, io);
608 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
612 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
617 lov_io_data_version_end(const struct lu_env *env, const struct cl_io_slice *ios)
619 struct lov_io *lio = cl2lov_io(env, ios);
620 struct cl_io *parent = lio->lis_cl.cis_io;
621 struct lov_io_sub *sub;
624 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
625 lov_io_end_wrapper(env, &sub->sub_io);
627 parent->u.ci_data_version.dv_data_version +=
628 sub->sub_io.u.ci_data_version.dv_data_version;
630 if (parent->ci_result == 0)
631 parent->ci_result = sub->sub_io.ci_result;
637 static void lov_io_iter_fini(const struct lu_env *env,
638 const struct cl_io_slice *ios)
640 struct lov_io *lio = cl2lov_io(env, ios);
644 rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
646 while (!list_empty(&lio->lis_active))
647 list_del_init(lio->lis_active.next);
651 static void lov_io_unlock(const struct lu_env *env,
652 const struct cl_io_slice *ios)
657 rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
662 static int lov_io_read_ahead(const struct lu_env *env,
663 const struct cl_io_slice *ios,
664 pgoff_t start, struct cl_read_ahead *ra)
666 struct lov_io *lio = cl2lov_io(env, ios);
667 struct lov_object *loo = lio->lis_object;
668 struct cl_object *obj = lov2cl(loo);
669 struct lov_layout_raid0 *r0;
670 struct lov_io_sub *sub;
674 unsigned int pps; /* pages per stripe */
680 offset = cl_offset(obj, start);
681 index = lov_lsm_entry(loo->lo_lsm, offset);
682 if (index < 0 || !lsm_entry_inited(loo->lo_lsm, index))
685 stripe = lov_stripe_number(loo->lo_lsm, index, offset);
687 r0 = lov_r0(loo, index);
688 if (unlikely(r0->lo_sub[stripe] == NULL))
691 sub = lov_sub_get(env, lio, lov_comp_index(index, stripe));
693 RETURN(PTR_ERR(sub));
695 lov_stripe_offset(loo->lo_lsm, index, offset, stripe, &suboff);
696 rc = cl_io_read_ahead(sub->sub_env, &sub->sub_io,
697 cl_index(lovsub2cl(r0->lo_sub[stripe]), suboff),
700 CDEBUG(D_READA, DFID " cra_end = %lu, stripes = %d, rc = %d\n",
701 PFID(lu_object_fid(lov2lu(loo))), ra->cra_end, r0->lo_nr, rc);
706 * Adjust the stripe index by layout of raid0. ra->cra_end is the
707 * maximum page index covered by an underlying DLM lock.
708 * This function converts cra_end from stripe level to file level, and
709 * make sure it's not beyond stripe boundary.
711 if (r0->lo_nr == 1) /* single stripe file */
714 /* cra_end is stripe level, convert it into file level */
715 ra_end = ra->cra_end;
716 if (ra_end != CL_PAGE_EOF)
717 ra_end = lov_stripe_pgoff(loo->lo_lsm, index, ra_end, stripe);
719 pps = lov_lse(loo, index)->lsme_stripe_size >> PAGE_SHIFT;
721 CDEBUG(D_READA, DFID " max_index = %lu, pps = %u, index = %u, "
722 "stripe_size = %u, stripe no = %u, start index = %lu\n",
723 PFID(lu_object_fid(lov2lu(loo))), ra_end, pps, index,
724 lov_lse(loo, index)->lsme_stripe_size, stripe, start);
726 /* never exceed the end of the stripe */
727 ra->cra_end = min_t(pgoff_t, ra_end, start + pps - start % pps - 1);
732 * lov implementation of cl_operations::cio_submit() method. It takes a list
733 * of pages in \a queue, splits it into per-stripe sub-lists, invokes
734 * cl_io_submit() on underlying devices to submit sub-lists, and then splices
737 * Major complication of this function is a need to handle memory cleansing:
738 * cl_io_submit() is called to write out pages as a part of VM memory
739 * reclamation, and hence it may not fail due to memory shortages (system
740 * dead-locks otherwise). To deal with this, some resources (sub-lists,
741 * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
742 * not-memory cleansing context), and in case of memory shortage, these
743 * pre-allocated resources are used by lov_io_submit() under
744 * lov_device::ld_mutex mutex.
746 static int lov_io_submit(const struct lu_env *env,
747 const struct cl_io_slice *ios,
748 enum cl_req_type crt, struct cl_2queue *queue)
750 struct cl_page_list *qin = &queue->c2_qin;
751 struct lov_io *lio = cl2lov_io(env, ios);
752 struct lov_io_sub *sub;
753 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
754 struct cl_page *page;
759 if (lio->lis_nr_subios == 1) {
760 int idx = lio->lis_single_subio_index;
762 sub = lov_sub_get(env, lio, idx);
763 LASSERT(!IS_ERR(sub));
764 LASSERT(sub == &lio->lis_single_subio);
765 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
770 cl_page_list_init(plist);
771 while (qin->pl_nr > 0) {
772 struct cl_2queue *cl2q = &lov_env_info(env)->lti_cl2q;
774 cl_2queue_init(cl2q);
776 page = cl_page_list_first(qin);
777 cl_page_list_move(&cl2q->c2_qin, qin, page);
779 index = lov_page_index(page);
780 while (qin->pl_nr > 0) {
781 page = cl_page_list_first(qin);
782 if (index != lov_page_index(page))
785 cl_page_list_move(&cl2q->c2_qin, qin, page);
788 sub = lov_sub_get(env, lio, index);
790 rc = cl_io_submit_rw(sub->sub_env, &sub->sub_io,
796 cl_page_list_splice(&cl2q->c2_qin, plist);
797 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
798 cl_2queue_fini(env, cl2q);
804 cl_page_list_splice(plist, qin);
805 cl_page_list_fini(env, plist);
810 static int lov_io_commit_async(const struct lu_env *env,
811 const struct cl_io_slice *ios,
812 struct cl_page_list *queue, int from, int to,
815 struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
816 struct lov_io *lio = cl2lov_io(env, ios);
817 struct lov_io_sub *sub;
818 struct cl_page *page;
822 if (lio->lis_nr_subios == 1) {
823 int idx = lio->lis_single_subio_index;
825 sub = lov_sub_get(env, lio, idx);
826 LASSERT(!IS_ERR(sub));
827 LASSERT(sub == &lio->lis_single_subio);
828 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io, queue,
833 cl_page_list_init(plist);
834 while (queue->pl_nr > 0) {
838 LASSERT(plist->pl_nr == 0);
839 page = cl_page_list_first(queue);
840 cl_page_list_move(plist, queue, page);
842 index = lov_page_index(page);
843 while (queue->pl_nr > 0) {
844 page = cl_page_list_first(queue);
845 if (index != lov_page_index(page))
848 cl_page_list_move(plist, queue, page);
851 if (queue->pl_nr > 0) /* still has more pages */
852 stripe_to = PAGE_SIZE;
854 sub = lov_sub_get(env, lio, index);
856 rc = cl_io_commit_async(sub->sub_env, &sub->sub_io,
857 plist, from, stripe_to, cb);
863 if (plist->pl_nr > 0) /* short write */
869 /* for error case, add the page back into the qin list */
870 LASSERT(ergo(rc == 0, plist->pl_nr == 0));
871 while (plist->pl_nr > 0) {
872 /* error occurred, add the uncommitted pages back into queue */
873 page = cl_page_list_last(plist);
874 cl_page_list_move_head(queue, plist, page);
880 static int lov_io_fault_start(const struct lu_env *env,
881 const struct cl_io_slice *ios)
883 struct cl_fault_io *fio;
885 struct lov_io_sub *sub;
889 fio = &ios->cis_io->u.ci_fault;
890 lio = cl2lov_io(env, ios);
891 sub = lov_sub_get(env, lio, lov_page_index(fio->ft_page));
892 sub->sub_io.u.ci_fault.ft_nob = fio->ft_nob;
894 RETURN(lov_io_start(env, ios));
897 static void lov_io_fsync_end(const struct lu_env *env,
898 const struct cl_io_slice *ios)
900 struct lov_io *lio = cl2lov_io(env, ios);
901 struct lov_io_sub *sub;
902 unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
906 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
907 struct cl_io *subio = &sub->sub_io;
909 lov_io_end_wrapper(sub->sub_env, subio);
911 if (subio->ci_result == 0)
912 *written += subio->u.ci_fsync.fi_nr_written;
917 static const struct cl_io_operations lov_io_ops = {
920 .cio_fini = lov_io_fini,
921 .cio_iter_init = lov_io_rw_iter_init,
922 .cio_iter_fini = lov_io_iter_fini,
923 .cio_lock = lov_io_lock,
924 .cio_unlock = lov_io_unlock,
925 .cio_start = lov_io_start,
926 .cio_end = lov_io_end
929 .cio_fini = lov_io_fini,
930 .cio_iter_init = lov_io_rw_iter_init,
931 .cio_iter_fini = lov_io_iter_fini,
932 .cio_lock = lov_io_lock,
933 .cio_unlock = lov_io_unlock,
934 .cio_start = lov_io_start,
935 .cio_end = lov_io_end
938 .cio_fini = lov_io_fini,
939 .cio_iter_init = lov_io_setattr_iter_init,
940 .cio_iter_fini = lov_io_iter_fini,
941 .cio_lock = lov_io_lock,
942 .cio_unlock = lov_io_unlock,
943 .cio_start = lov_io_start,
944 .cio_end = lov_io_end
946 [CIT_DATA_VERSION] = {
947 .cio_fini = lov_io_fini,
948 .cio_iter_init = lov_io_iter_init,
949 .cio_iter_fini = lov_io_iter_fini,
950 .cio_lock = lov_io_lock,
951 .cio_unlock = lov_io_unlock,
952 .cio_start = lov_io_start,
953 .cio_end = lov_io_data_version_end,
956 .cio_fini = lov_io_fini,
957 .cio_iter_init = lov_io_iter_init,
958 .cio_iter_fini = lov_io_iter_fini,
959 .cio_lock = lov_io_lock,
960 .cio_unlock = lov_io_unlock,
961 .cio_start = lov_io_fault_start,
962 .cio_end = lov_io_end
965 .cio_fini = lov_io_fini,
966 .cio_iter_init = lov_io_iter_init,
967 .cio_iter_fini = lov_io_iter_fini,
968 .cio_lock = lov_io_lock,
969 .cio_unlock = lov_io_unlock,
970 .cio_start = lov_io_start,
971 .cio_end = lov_io_fsync_end
974 .cio_fini = lov_io_fini,
975 .cio_iter_init = lov_io_iter_init,
976 .cio_iter_fini = lov_io_iter_fini,
977 .cio_lock = lov_io_lock,
978 .cio_unlock = lov_io_unlock,
979 .cio_start = lov_io_start,
980 .cio_end = lov_io_end
983 .cio_fini = lov_io_fini
986 .cio_read_ahead = lov_io_read_ahead,
987 .cio_submit = lov_io_submit,
988 .cio_commit_async = lov_io_commit_async,
991 /*****************************************************************************
993 * Empty lov io operations.
997 static void lov_empty_io_fini(const struct lu_env *env,
998 const struct cl_io_slice *ios)
1000 struct lov_object *lov = cl2lov(ios->cis_obj);
1003 if (atomic_dec_and_test(&lov->lo_active_ios))
1004 wake_up_all(&lov->lo_waitq);
1008 static int lov_empty_io_submit(const struct lu_env *env,
1009 const struct cl_io_slice *ios,
1010 enum cl_req_type crt, struct cl_2queue *queue)
1015 static void lov_empty_impossible(const struct lu_env *env,
1016 struct cl_io_slice *ios)
1021 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
1024 * An io operation vector for files without stripes.
1026 static const struct cl_io_operations lov_empty_io_ops = {
1029 .cio_fini = lov_empty_io_fini,
1031 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1032 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1033 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1034 .cio_end = LOV_EMPTY_IMPOSSIBLE
1038 .cio_fini = lov_empty_io_fini,
1039 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1040 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1041 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1042 .cio_end = LOV_EMPTY_IMPOSSIBLE
1045 .cio_fini = lov_empty_io_fini,
1046 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1047 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1048 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1049 .cio_end = LOV_EMPTY_IMPOSSIBLE
1052 .cio_fini = lov_empty_io_fini,
1053 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
1054 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
1055 .cio_start = LOV_EMPTY_IMPOSSIBLE,
1056 .cio_end = LOV_EMPTY_IMPOSSIBLE
1059 .cio_fini = lov_empty_io_fini
1062 .cio_fini = lov_empty_io_fini
1065 .cio_fini = lov_empty_io_fini
1068 .cio_submit = lov_empty_io_submit,
1069 .cio_commit_async = LOV_EMPTY_IMPOSSIBLE
1072 int lov_io_init_composite(const struct lu_env *env, struct cl_object *obj,
1075 struct lov_io *lio = lov_env_io(env);
1076 struct lov_object *lov = cl2lov(obj);
1079 INIT_LIST_HEAD(&lio->lis_active);
1080 io->ci_result = lov_io_slice_init(lio, lov, io);
1081 if (io->ci_result != 0)
1082 RETURN(io->ci_result);
1084 if (io->ci_result == 0) {
1085 io->ci_result = lov_io_subio_init(env, lio, io);
1086 if (io->ci_result == 0) {
1087 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
1088 atomic_inc(&lov->lo_active_ios);
1091 RETURN(io->ci_result);
1094 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
1097 struct lov_object *lov = cl2lov(obj);
1098 struct lov_io *lio = lov_env_io(env);
1102 lio->lis_object = lov;
1103 switch (io->ci_type) {
1113 case CIT_DATA_VERSION:
1121 CERROR("Page fault on a file without stripes: "DFID"\n",
1122 PFID(lu_object_fid(&obj->co_lu)));
1126 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1127 atomic_inc(&lov->lo_active_ios);
1130 io->ci_result = result < 0 ? result : 0;
1134 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
1137 struct lov_object *lov = cl2lov(obj);
1138 struct lov_io *lio = lov_env_io(env);
1142 LASSERT(lov->lo_lsm != NULL);
1143 lio->lis_object = lov;
1145 switch (io->ci_type) {
1147 LASSERTF(0, "invalid type %d\n", io->ci_type);
1148 result = -EOPNOTSUPP;
1153 case CIT_DATA_VERSION:
1157 /* the truncate to 0 is managed by MDT:
1158 * - in open, for open O_TRUNC
1159 * - in setattr, for truncate
1161 /* the truncate is for size > 0 so triggers a restore */
1162 if (cl_io_is_trunc(io)) {
1163 io->ci_restore_needed = 1;
1171 io->ci_restore_needed = 1;
1177 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1178 atomic_inc(&lov->lo_active_ios);
1181 io->ci_result = result < 0 ? result : 0;