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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
42 #define DEBUG_SUBSYSTEM S_CLASS
44 #include <linux/sched.h>
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_fid.h>
48 #include <libcfs/list.h>
49 #include <cl_object.h>
50 #include "cl_internal.h"
52 /*****************************************************************************
58 #define cl_io_for_each(slice, io) \
59 list_for_each_entry((slice), &io->ci_layers, cis_linkage)
60 #define cl_io_for_each_reverse(slice, io) \
61 list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
63 static inline int cl_io_type_is_valid(enum cl_io_type type)
65 return CIT_READ <= type && type < CIT_OP_NR;
68 static inline int cl_io_is_loopable(const struct cl_io *io)
70 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
74 * Returns true iff there is an IO ongoing in the given environment.
76 int cl_io_is_going(const struct lu_env *env)
78 return cl_env_info(env)->clt_current_io != NULL;
80 EXPORT_SYMBOL(cl_io_is_going);
83 * cl_io invariant that holds at all times when exported cl_io_*() functions
84 * are entered and left.
86 static int cl_io_invariant(const struct cl_io *io)
93 * io can own pages only when it is ongoing. Sub-io might
94 * still be in CIS_LOCKED state when top-io is in
97 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
98 (io->ci_state == CIS_LOCKED && up != NULL));
102 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
104 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
106 struct cl_io_slice *slice;
107 struct cl_thread_info *info;
109 LINVRNT(cl_io_type_is_valid(io->ci_type));
110 LINVRNT(cl_io_invariant(io));
113 while (!list_empty(&io->ci_layers)) {
114 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
116 list_del_init(&slice->cis_linkage);
117 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
118 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
120 * Invalidate slice to catch use after free. This assumes that
121 * slices are allocated within session and can be touched
122 * after ->cio_fini() returns.
124 slice->cis_io = NULL;
126 io->ci_state = CIS_FINI;
127 info = cl_env_info(env);
128 if (info->clt_current_io == io)
129 info->clt_current_io = NULL;
131 /* sanity check for layout change */
132 switch(io->ci_type) {
138 LASSERT(!io->ci_need_restart);
142 /* Check ignore layout change conf */
143 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
144 !io->ci_need_restart));
151 EXPORT_SYMBOL(cl_io_fini);
153 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
154 enum cl_io_type iot, struct cl_object *obj)
156 struct cl_object *scan;
159 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
160 LINVRNT(cl_io_type_is_valid(iot));
161 LINVRNT(cl_io_invariant(io));
165 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
166 INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
167 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
168 INIT_LIST_HEAD(&io->ci_layers);
171 cl_object_for_each(scan, obj) {
172 if (scan->co_ops->coo_io_init != NULL) {
173 result = scan->co_ops->coo_io_init(env, scan, io);
179 io->ci_state = CIS_INIT;
184 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
186 * \pre obj != cl_object_top(obj)
188 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
189 enum cl_io_type iot, struct cl_object *obj)
191 struct cl_thread_info *info = cl_env_info(env);
193 LASSERT(obj != cl_object_top(obj));
194 if (info->clt_current_io == NULL)
195 info->clt_current_io = io;
196 return cl_io_init0(env, io, iot, obj);
198 EXPORT_SYMBOL(cl_io_sub_init);
201 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
203 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
204 * what the latter returned.
206 * \pre obj == cl_object_top(obj)
207 * \pre cl_io_type_is_valid(iot)
208 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
210 int cl_io_init(const struct lu_env *env, struct cl_io *io,
211 enum cl_io_type iot, struct cl_object *obj)
213 struct cl_thread_info *info = cl_env_info(env);
215 LASSERT(obj == cl_object_top(obj));
216 LASSERT(info->clt_current_io == NULL);
218 info->clt_current_io = io;
219 return cl_io_init0(env, io, iot, obj);
221 EXPORT_SYMBOL(cl_io_init);
224 * Initialize read or write io.
226 * \pre iot == CIT_READ || iot == CIT_WRITE
228 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
229 enum cl_io_type iot, loff_t pos, size_t count)
231 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
232 LINVRNT(io->ci_obj != NULL);
235 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
236 "io range: %u ["LPU64", "LPU64") %u %u\n",
237 iot, (__u64)pos, (__u64)pos + count,
238 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
239 io->u.ci_rw.crw_pos = pos;
240 io->u.ci_rw.crw_count = count;
241 RETURN(cl_io_init(env, io, iot, io->ci_obj));
243 EXPORT_SYMBOL(cl_io_rw_init);
245 static inline const struct lu_fid *
246 cl_lock_descr_fid(const struct cl_lock_descr *descr)
248 return lu_object_fid(&descr->cld_obj->co_lu);
251 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
252 const struct cl_lock_descr *d1)
254 return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
255 __diff_normalize(d0->cld_start, d1->cld_start);
258 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
259 const struct cl_lock_descr *d1)
263 ret = lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1));
266 if (d0->cld_end < d1->cld_start)
268 if (d0->cld_start > d0->cld_end)
273 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
274 const struct cl_lock_descr *d1)
276 d0->cld_start = min(d0->cld_start, d1->cld_start);
277 d0->cld_end = max(d0->cld_end, d1->cld_end);
279 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
280 d0->cld_mode = CLM_WRITE;
282 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
283 d0->cld_mode = CLM_GROUP;
287 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
289 static void cl_io_locks_sort(struct cl_io *io)
294 /* hidden treasure: bubble sort for now. */
296 struct cl_io_lock_link *curr;
297 struct cl_io_lock_link *prev;
298 struct cl_io_lock_link *temp;
303 list_for_each_entry_safe(curr, temp, &io->ci_lockset.cls_todo,
306 switch (cl_lock_descr_sort(&prev->cill_descr,
307 &curr->cill_descr)) {
310 * IMPOSSIBLE: Identical locks are
317 list_move_tail(&curr->cill_linkage,
318 &prev->cill_linkage);
320 continue; /* don't change prev: it's
321 * still "previous" */
322 case -1: /* already in order */
333 * Check whether \a queue contains locks matching \a need.
335 * \retval +ve there is a matching lock in the \a queue
336 * \retval 0 there are no matching locks in the \a queue
338 int cl_queue_match(const struct list_head *queue,
339 const struct cl_lock_descr *need)
341 struct cl_io_lock_link *scan;
344 list_for_each_entry(scan, queue, cill_linkage) {
345 if (cl_lock_descr_match(&scan->cill_descr, need))
350 EXPORT_SYMBOL(cl_queue_match);
352 static int cl_queue_merge(const struct list_head *queue,
353 const struct cl_lock_descr *need)
355 struct cl_io_lock_link *scan;
358 list_for_each_entry(scan, queue, cill_linkage) {
359 if (cl_lock_descr_cmp(&scan->cill_descr, need))
361 cl_lock_descr_merge(&scan->cill_descr, need);
362 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
363 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
364 scan->cill_descr.cld_end);
370 static int cl_lockset_match(const struct cl_lockset *set,
371 const struct cl_lock_descr *need)
373 return cl_queue_match(&set->cls_curr, need) ||
374 cl_queue_match(&set->cls_done, need);
377 static int cl_lockset_merge(const struct cl_lockset *set,
378 const struct cl_lock_descr *need)
380 return cl_queue_merge(&set->cls_todo, need) ||
381 cl_lockset_match(set, need);
384 static int cl_lockset_lock_one(const struct lu_env *env,
385 struct cl_io *io, struct cl_lockset *set,
386 struct cl_io_lock_link *link)
388 struct cl_lock *lock;
393 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
396 link->cill_lock = lock;
397 list_move(&link->cill_linkage, &set->cls_curr);
398 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
399 result = cl_wait(env, lock);
401 list_move(&link->cill_linkage, &set->cls_done);
405 result = PTR_ERR(lock);
409 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
410 struct cl_io_lock_link *link)
412 struct cl_lock *lock = link->cill_lock;
415 list_del_init(&link->cill_linkage);
417 cl_lock_release(env, lock, "io", io);
418 link->cill_lock = NULL;
420 if (link->cill_fini != NULL)
421 link->cill_fini(env, link);
425 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
426 struct cl_lockset *set)
428 struct cl_io_lock_link *link;
429 struct cl_io_lock_link *temp;
430 struct cl_lock *lock;
435 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
436 if (!cl_lockset_match(set, &link->cill_descr)) {
437 /* XXX some locking to guarantee that locks aren't
438 * expanded in between. */
439 result = cl_lockset_lock_one(env, io, set, link);
443 cl_lock_link_fini(env, io, link);
446 list_for_each_entry_safe(link, temp, &set->cls_curr,
448 lock = link->cill_lock;
449 result = cl_wait(env, lock);
451 list_move(&link->cill_linkage, &set->cls_done);
460 * Takes locks necessary for the current iteration of io.
462 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
463 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
466 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
468 const struct cl_io_slice *scan;
471 LINVRNT(cl_io_is_loopable(io));
472 LINVRNT(io->ci_state == CIS_IT_STARTED);
473 LINVRNT(cl_io_invariant(io));
476 cl_io_for_each(scan, io) {
477 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
479 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
484 cl_io_locks_sort(io);
485 result = cl_lockset_lock(env, io, &io->ci_lockset);
488 cl_io_unlock(env, io);
490 io->ci_state = CIS_LOCKED;
493 EXPORT_SYMBOL(cl_io_lock);
496 * Release locks takes by io.
498 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
500 struct cl_lockset *set;
501 struct cl_io_lock_link *link;
502 struct cl_io_lock_link *temp;
503 const struct cl_io_slice *scan;
505 LASSERT(cl_io_is_loopable(io));
506 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
507 LINVRNT(cl_io_invariant(io));
510 set = &io->ci_lockset;
512 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
513 cl_lock_link_fini(env, io, link);
515 list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
516 cl_lock_link_fini(env, io, link);
518 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
519 cl_unuse(env, link->cill_lock);
520 cl_lock_link_fini(env, io, link);
522 cl_io_for_each_reverse(scan, io) {
523 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
524 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
526 io->ci_state = CIS_UNLOCKED;
527 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
530 EXPORT_SYMBOL(cl_io_unlock);
533 * Prepares next iteration of io.
535 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
536 * layers a chance to modify io parameters, e.g., so that lov can restrict io
537 * to a single stripe.
539 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
541 const struct cl_io_slice *scan;
544 LINVRNT(cl_io_is_loopable(io));
545 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
546 LINVRNT(cl_io_invariant(io));
550 cl_io_for_each(scan, io) {
551 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
553 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
559 io->ci_state = CIS_IT_STARTED;
562 EXPORT_SYMBOL(cl_io_iter_init);
565 * Finalizes io iteration.
567 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
569 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
571 const struct cl_io_slice *scan;
573 LINVRNT(cl_io_is_loopable(io));
574 LINVRNT(io->ci_state == CIS_UNLOCKED);
575 LINVRNT(cl_io_invariant(io));
578 cl_io_for_each_reverse(scan, io) {
579 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
580 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
582 io->ci_state = CIS_IT_ENDED;
585 EXPORT_SYMBOL(cl_io_iter_fini);
588 * Records that read or write io progressed \a nob bytes forward.
590 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
592 const struct cl_io_slice *scan;
594 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
596 LINVRNT(cl_io_is_loopable(io));
597 LINVRNT(cl_io_invariant(io));
601 io->u.ci_rw.crw_pos += nob;
602 io->u.ci_rw.crw_count -= nob;
604 /* layers have to be notified. */
605 cl_io_for_each_reverse(scan, io) {
606 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
607 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
612 EXPORT_SYMBOL(cl_io_rw_advance);
615 * Adds a lock to a lockset.
617 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
618 struct cl_io_lock_link *link)
623 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
626 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
631 EXPORT_SYMBOL(cl_io_lock_add);
633 static void cl_free_io_lock_link(const struct lu_env *env,
634 struct cl_io_lock_link *link)
640 * Allocates new lock link, and uses it to add a lock to a lockset.
642 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
643 struct cl_lock_descr *descr)
645 struct cl_io_lock_link *link;
651 link->cill_descr = *descr;
652 link->cill_fini = cl_free_io_lock_link;
653 result = cl_io_lock_add(env, io, link);
654 if (result) /* lock match */
655 link->cill_fini(env, link);
661 EXPORT_SYMBOL(cl_io_lock_alloc_add);
664 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
666 int cl_io_start(const struct lu_env *env, struct cl_io *io)
668 const struct cl_io_slice *scan;
671 LINVRNT(cl_io_is_loopable(io));
672 LINVRNT(io->ci_state == CIS_LOCKED);
673 LINVRNT(cl_io_invariant(io));
676 io->ci_state = CIS_IO_GOING;
677 cl_io_for_each(scan, io) {
678 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
680 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
688 EXPORT_SYMBOL(cl_io_start);
691 * Wait until current io iteration is finished by calling
692 * cl_io_operations::cio_end() bottom-to-top.
694 void cl_io_end(const struct lu_env *env, struct cl_io *io)
696 const struct cl_io_slice *scan;
698 LINVRNT(cl_io_is_loopable(io));
699 LINVRNT(io->ci_state == CIS_IO_GOING);
700 LINVRNT(cl_io_invariant(io));
703 cl_io_for_each_reverse(scan, io) {
704 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
705 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
706 /* TODO: error handling. */
708 io->ci_state = CIS_IO_FINISHED;
711 EXPORT_SYMBOL(cl_io_end);
713 static const struct cl_page_slice *
714 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
716 const struct cl_page_slice *slice;
718 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
719 LINVRNT(slice != NULL);
724 * Called by read io, when page has to be read from the server.
726 * \see cl_io_operations::cio_read_page()
728 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
729 struct cl_page *page)
731 const struct cl_io_slice *scan;
732 struct cl_2queue *queue;
735 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
736 LINVRNT(cl_page_is_owned(page, io));
737 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
738 LINVRNT(cl_io_invariant(io));
741 queue = &io->ci_queue;
743 cl_2queue_init(queue);
745 * ->cio_read_page() methods called in the loop below are supposed to
746 * never block waiting for network (the only subtle point is the
747 * creation of new pages for read-ahead that might result in cache
748 * shrinking, but currently only clean pages are shrunk and this
749 * requires no network io).
751 * Should this ever starts blocking, retry loop would be needed for
752 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
754 cl_io_for_each(scan, io) {
755 if (scan->cis_iop->cio_read_page != NULL) {
756 const struct cl_page_slice *slice;
758 slice = cl_io_slice_page(scan, page);
759 LINVRNT(slice != NULL);
760 result = scan->cis_iop->cio_read_page(env, scan, slice);
765 if (result == 0 && queue->c2_qin.pl_nr > 0)
766 result = cl_io_submit_rw(env, io, CRT_READ, queue);
768 * Unlock unsent pages in case of error.
770 cl_page_list_disown(env, io, &queue->c2_qin);
771 cl_2queue_fini(env, queue);
774 EXPORT_SYMBOL(cl_io_read_page);
777 * Commit a list of contiguous pages into writeback cache.
779 * \returns 0 if all pages committed, or errcode if error occurred.
780 * \see cl_io_operations::cio_commit_async()
782 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
783 struct cl_page_list *queue, int from, int to,
786 const struct cl_io_slice *scan;
790 cl_io_for_each(scan, io) {
791 if (scan->cis_iop->cio_commit_async == NULL)
793 result = scan->cis_iop->cio_commit_async(env, scan, queue,
800 EXPORT_SYMBOL(cl_io_commit_async);
803 * Submits a list of pages for immediate io.
805 * After the function gets returned, The submitted pages are moved to
806 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
807 * to be submitted, and the pages are errant to submit.
809 * \returns 0 if at least one page was submitted, error code otherwise.
810 * \see cl_io_operations::cio_submit()
812 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
813 enum cl_req_type crt, struct cl_2queue *queue)
815 const struct cl_io_slice *scan;
819 cl_io_for_each(scan, io) {
820 if (scan->cis_iop->cio_submit == NULL)
822 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
827 * If ->cio_submit() failed, no pages were sent.
829 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
832 EXPORT_SYMBOL(cl_io_submit_rw);
835 * Submit a sync_io and wait for the IO to be finished, or error happens.
836 * If \a timeout is zero, it means to wait for the IO unconditionally.
838 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
839 enum cl_req_type iot, struct cl_2queue *queue,
842 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
846 cl_page_list_for_each(pg, &queue->c2_qin) {
847 LASSERT(pg->cp_sync_io == NULL);
848 pg->cp_sync_io = anchor;
851 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
852 rc = cl_io_submit_rw(env, io, iot, queue);
855 * If some pages weren't sent for any reason (e.g.,
856 * read found up-to-date pages in the cache, or write found
857 * clean pages), count them as completed to avoid infinite
860 cl_page_list_for_each(pg, &queue->c2_qin) {
861 pg->cp_sync_io = NULL;
862 cl_sync_io_note(anchor, +1);
865 /* wait for the IO to be finished. */
866 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
869 LASSERT(list_empty(&queue->c2_qout.pl_pages));
870 cl_page_list_for_each(pg, &queue->c2_qin)
871 pg->cp_sync_io = NULL;
875 EXPORT_SYMBOL(cl_io_submit_sync);
878 * Cancel an IO which has been submitted by cl_io_submit_rw.
880 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
881 struct cl_page_list *queue)
883 struct cl_page *page;
886 CERROR("Canceling ongoing page trasmission\n");
887 cl_page_list_for_each(page, queue) {
890 rc = cl_page_cancel(env, page);
891 result = result ?: rc;
895 EXPORT_SYMBOL(cl_io_cancel);
900 * Pumps io through iterations calling
902 * - cl_io_iter_init()
912 * - cl_io_iter_fini()
914 * repeatedly until there is no more io to do.
916 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
920 LINVRNT(cl_io_is_loopable(io));
927 result = cl_io_iter_init(env, io);
930 result = cl_io_lock(env, io);
933 * Notify layers that locks has been taken,
936 * - llite: kms, short read;
937 * - llite: generic_file_read();
939 result = cl_io_start(env, io);
941 * Send any remaining pending
944 * - llite: ll_rw_stats_tally.
947 cl_io_unlock(env, io);
948 cl_io_rw_advance(env, io, io->ci_nob - nob);
951 cl_io_iter_fini(env, io);
952 } while (result == 0 && io->ci_continue);
954 result = io->ci_result;
955 RETURN(result < 0 ? result : 0);
957 EXPORT_SYMBOL(cl_io_loop);
960 * Adds io slice to the cl_io.
962 * This is called by cl_object_operations::coo_io_init() methods to add a
963 * per-layer state to the io. New state is added at the end of
964 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
966 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
968 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
969 struct cl_object *obj,
970 const struct cl_io_operations *ops)
972 struct list_head *linkage = &slice->cis_linkage;
974 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
975 list_empty(linkage));
978 list_add_tail(linkage, &io->ci_layers);
980 slice->cis_obj = obj;
981 slice->cis_iop = ops;
984 EXPORT_SYMBOL(cl_io_slice_add);
988 * Initializes page list.
990 void cl_page_list_init(struct cl_page_list *plist)
994 INIT_LIST_HEAD(&plist->pl_pages);
995 plist->pl_owner = current;
998 EXPORT_SYMBOL(cl_page_list_init);
1001 * Adds a page to a page list.
1003 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1006 /* it would be better to check that page is owned by "current" io, but
1007 * it is not passed here. */
1008 LASSERT(page->cp_owner != NULL);
1009 LINVRNT(plist->pl_owner == current);
1011 LASSERT(list_empty(&page->cp_batch));
1012 list_add_tail(&page->cp_batch, &plist->pl_pages);
1014 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1018 EXPORT_SYMBOL(cl_page_list_add);
1021 * Removes a page from a page list.
1023 void cl_page_list_del(const struct lu_env *env,
1024 struct cl_page_list *plist, struct cl_page *page)
1026 LASSERT(plist->pl_nr > 0);
1027 LASSERT(cl_page_is_vmlocked(env, page));
1028 LINVRNT(plist->pl_owner == current);
1031 list_del_init(&page->cp_batch);
1033 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1034 cl_page_put(env, page);
1037 EXPORT_SYMBOL(cl_page_list_del);
1040 * Moves a page from one page list to another.
1042 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1043 struct cl_page *page)
1045 LASSERT(src->pl_nr > 0);
1046 LINVRNT(dst->pl_owner == current);
1047 LINVRNT(src->pl_owner == current);
1050 list_move_tail(&page->cp_batch, &dst->pl_pages);
1053 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1057 EXPORT_SYMBOL(cl_page_list_move);
1060 * Moves a page from one page list to the head of another list.
1062 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
1063 struct cl_page *page)
1065 LASSERT(src->pl_nr > 0);
1066 LINVRNT(dst->pl_owner == current);
1067 LINVRNT(src->pl_owner == current);
1070 list_move(&page->cp_batch, &dst->pl_pages);
1073 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1077 EXPORT_SYMBOL(cl_page_list_move_head);
1080 * splice the cl_page_list, just as list head does
1082 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1084 struct cl_page *page;
1085 struct cl_page *tmp;
1087 LINVRNT(list->pl_owner == current);
1088 LINVRNT(head->pl_owner == current);
1091 cl_page_list_for_each_safe(page, tmp, list)
1092 cl_page_list_move(head, list, page);
1095 EXPORT_SYMBOL(cl_page_list_splice);
1097 void cl_page_disown0(const struct lu_env *env,
1098 struct cl_io *io, struct cl_page *pg);
1101 * Disowns pages in a queue.
1103 void cl_page_list_disown(const struct lu_env *env,
1104 struct cl_io *io, struct cl_page_list *plist)
1106 struct cl_page *page;
1107 struct cl_page *temp;
1109 LINVRNT(plist->pl_owner == current);
1112 cl_page_list_for_each_safe(page, temp, plist) {
1113 LASSERT(plist->pl_nr > 0);
1115 list_del_init(&page->cp_batch);
1118 * cl_page_disown0 rather than usual cl_page_disown() is used,
1119 * because pages are possibly in CPS_FREEING state already due
1120 * to the call to cl_page_list_discard().
1123 * XXX cl_page_disown0() will fail if page is not locked.
1125 cl_page_disown0(env, io, page);
1126 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1128 cl_page_put(env, page);
1132 EXPORT_SYMBOL(cl_page_list_disown);
1135 * Releases pages from queue.
1137 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1139 struct cl_page *page;
1140 struct cl_page *temp;
1142 LINVRNT(plist->pl_owner == current);
1145 cl_page_list_for_each_safe(page, temp, plist)
1146 cl_page_list_del(env, plist, page);
1147 LASSERT(plist->pl_nr == 0);
1150 EXPORT_SYMBOL(cl_page_list_fini);
1153 * Owns all pages in a queue.
1155 int cl_page_list_own(const struct lu_env *env,
1156 struct cl_io *io, struct cl_page_list *plist)
1158 struct cl_page *page;
1159 struct cl_page *temp;
1162 LINVRNT(plist->pl_owner == current);
1166 cl_page_list_for_each_safe(page, temp, plist) {
1167 if (cl_page_own(env, io, page) == 0)
1168 result = result ?: page->cp_error;
1170 cl_page_list_del(env, plist, page);
1174 EXPORT_SYMBOL(cl_page_list_own);
1177 * Assumes all pages in a queue.
1179 void cl_page_list_assume(const struct lu_env *env,
1180 struct cl_io *io, struct cl_page_list *plist)
1182 struct cl_page *page;
1184 LINVRNT(plist->pl_owner == current);
1186 cl_page_list_for_each(page, plist)
1187 cl_page_assume(env, io, page);
1189 EXPORT_SYMBOL(cl_page_list_assume);
1192 * Discards all pages in a queue.
1194 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1195 struct cl_page_list *plist)
1197 struct cl_page *page;
1199 LINVRNT(plist->pl_owner == current);
1201 cl_page_list_for_each(page, plist)
1202 cl_page_discard(env, io, page);
1205 EXPORT_SYMBOL(cl_page_list_discard);
1208 * Initialize dual page queue.
1210 void cl_2queue_init(struct cl_2queue *queue)
1213 cl_page_list_init(&queue->c2_qin);
1214 cl_page_list_init(&queue->c2_qout);
1217 EXPORT_SYMBOL(cl_2queue_init);
1220 * Add a page to the incoming page list of 2-queue.
1222 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1225 cl_page_list_add(&queue->c2_qin, page);
1228 EXPORT_SYMBOL(cl_2queue_add);
1231 * Disown pages in both lists of a 2-queue.
1233 void cl_2queue_disown(const struct lu_env *env,
1234 struct cl_io *io, struct cl_2queue *queue)
1237 cl_page_list_disown(env, io, &queue->c2_qin);
1238 cl_page_list_disown(env, io, &queue->c2_qout);
1241 EXPORT_SYMBOL(cl_2queue_disown);
1244 * Discard (truncate) pages in both lists of a 2-queue.
1246 void cl_2queue_discard(const struct lu_env *env,
1247 struct cl_io *io, struct cl_2queue *queue)
1250 cl_page_list_discard(env, io, &queue->c2_qin);
1251 cl_page_list_discard(env, io, &queue->c2_qout);
1254 EXPORT_SYMBOL(cl_2queue_discard);
1257 * Assume to own the pages in cl_2queue
1259 void cl_2queue_assume(const struct lu_env *env,
1260 struct cl_io *io, struct cl_2queue *queue)
1262 cl_page_list_assume(env, io, &queue->c2_qin);
1263 cl_page_list_assume(env, io, &queue->c2_qout);
1265 EXPORT_SYMBOL(cl_2queue_assume);
1268 * Finalize both page lists of a 2-queue.
1270 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1273 cl_page_list_fini(env, &queue->c2_qout);
1274 cl_page_list_fini(env, &queue->c2_qin);
1277 EXPORT_SYMBOL(cl_2queue_fini);
1280 * Initialize a 2-queue to contain \a page in its incoming page list.
1282 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1285 cl_2queue_init(queue);
1286 cl_2queue_add(queue, page);
1289 EXPORT_SYMBOL(cl_2queue_init_page);
1292 * Returns top-level io.
1294 * \see cl_object_top()
1296 struct cl_io *cl_io_top(struct cl_io *io)
1299 while (io->ci_parent != NULL)
1303 EXPORT_SYMBOL(cl_io_top);
1306 * Prints human readable representation of \a io to the \a f.
1308 void cl_io_print(const struct lu_env *env, void *cookie,
1309 lu_printer_t printer, const struct cl_io *io)
1314 * Adds request slice to the compound request.
1316 * This is called by cl_device_operations::cdo_req_init() methods to add a
1317 * per-layer state to the request. New state is added at the end of
1318 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1320 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1322 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1323 struct cl_device *dev,
1324 const struct cl_req_operations *ops)
1327 list_add_tail(&slice->crs_linkage, &req->crq_layers);
1328 slice->crs_dev = dev;
1329 slice->crs_ops = ops;
1330 slice->crs_req = req;
1333 EXPORT_SYMBOL(cl_req_slice_add);
1335 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1339 LASSERT(list_empty(&req->crq_pages));
1340 LASSERT(req->crq_nrpages == 0);
1341 LINVRNT(list_empty(&req->crq_layers));
1342 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1345 if (req->crq_o != NULL) {
1346 for (i = 0; i < req->crq_nrobjs; ++i) {
1347 struct cl_object *obj = req->crq_o[i].ro_obj;
1349 lu_object_ref_del_at(&obj->co_lu,
1350 &req->crq_o[i].ro_obj_ref,
1352 cl_object_put(env, obj);
1355 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1361 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1362 struct cl_page *page)
1364 struct cl_device *dev;
1365 struct cl_page_slice *slice;
1370 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1371 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1372 if (dev->cd_ops->cdo_req_init != NULL) {
1373 result = dev->cd_ops->cdo_req_init(env,
1383 * Invokes per-request transfer completion call-backs
1384 * (cl_req_operations::cro_completion()) bottom-to-top.
1386 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1388 struct cl_req_slice *slice;
1392 * for the lack of list_for_each_entry_reverse_safe()...
1394 while (!list_empty(&req->crq_layers)) {
1395 slice = list_entry(req->crq_layers.prev,
1396 struct cl_req_slice, crs_linkage);
1397 list_del_init(&slice->crs_linkage);
1398 if (slice->crs_ops->cro_completion != NULL)
1399 slice->crs_ops->cro_completion(env, slice, rc);
1401 cl_req_free(env, req);
1404 EXPORT_SYMBOL(cl_req_completion);
1407 * Allocates new transfer request.
1409 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1410 enum cl_req_type crt, int nr_objects)
1414 LINVRNT(nr_objects > 0);
1421 req->crq_type = crt;
1422 INIT_LIST_HEAD(&req->crq_pages);
1423 INIT_LIST_HEAD(&req->crq_layers);
1425 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1426 if (req->crq_o != NULL) {
1427 req->crq_nrobjs = nr_objects;
1428 result = cl_req_init(env, req, page);
1432 cl_req_completion(env, req, result);
1433 req = ERR_PTR(result);
1436 req = ERR_PTR(-ENOMEM);
1439 EXPORT_SYMBOL(cl_req_alloc);
1442 * Adds a page to a request.
1444 void cl_req_page_add(const struct lu_env *env,
1445 struct cl_req *req, struct cl_page *page)
1447 struct cl_object *obj;
1448 struct cl_req_obj *rqo;
1453 LASSERT(list_empty(&page->cp_flight));
1454 LASSERT(page->cp_req == NULL);
1456 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1457 req, req->crq_type, req->crq_nrpages);
1459 list_add_tail(&page->cp_flight, &req->crq_pages);
1462 obj = cl_object_top(page->cp_obj);
1463 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1464 if (rqo->ro_obj == NULL) {
1467 lu_object_ref_add_at(&obj->co_lu, &rqo->ro_obj_ref,
1472 LASSERT(i < req->crq_nrobjs);
1475 EXPORT_SYMBOL(cl_req_page_add);
1478 * Removes a page from a request.
1480 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1482 struct cl_req *req = page->cp_req;
1486 LASSERT(!list_empty(&page->cp_flight));
1487 LASSERT(req->crq_nrpages > 0);
1489 list_del_init(&page->cp_flight);
1491 page->cp_req = NULL;
1494 EXPORT_SYMBOL(cl_req_page_done);
1497 * Notifies layers that request is about to depart by calling
1498 * cl_req_operations::cro_prep() top-to-bottom.
1500 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1504 const struct cl_req_slice *slice;
1508 * Check that the caller of cl_req_alloc() didn't lie about the number
1511 for (i = 0; i < req->crq_nrobjs; ++i)
1512 LASSERT(req->crq_o[i].ro_obj != NULL);
1515 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1516 if (slice->crs_ops->cro_prep != NULL) {
1517 result = slice->crs_ops->cro_prep(env, slice);
1524 EXPORT_SYMBOL(cl_req_prep);
1527 * Fills in attributes that are passed to server together with transfer. Only
1528 * attributes from \a flags may be touched. This can be called multiple times
1529 * for the same request.
1531 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1532 struct cl_req_attr *attr, obd_valid flags)
1534 const struct cl_req_slice *slice;
1535 struct cl_page *page;
1538 LASSERT(!list_empty(&req->crq_pages));
1541 /* Take any page to use as a model. */
1542 page = list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1544 for (i = 0; i < req->crq_nrobjs; ++i) {
1545 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1546 const struct cl_page_slice *scan;
1547 const struct cl_object *obj;
1549 scan = cl_page_at(page,
1550 slice->crs_dev->cd_lu_dev.ld_type);
1551 LASSERT(scan != NULL);
1552 obj = scan->cpl_obj;
1553 if (slice->crs_ops->cro_attr_set != NULL)
1554 slice->crs_ops->cro_attr_set(env, slice, obj,
1560 EXPORT_SYMBOL(cl_req_attr_set);
1563 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1565 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1568 init_waitqueue_head(&anchor->csi_waitq);
1569 atomic_set(&anchor->csi_sync_nr, nrpages);
1570 atomic_set(&anchor->csi_barrier, nrpages > 0);
1571 anchor->csi_sync_rc = 0;
1574 EXPORT_SYMBOL(cl_sync_io_init);
1577 * Wait until all transfer completes. Transfer completion routine has to call
1578 * cl_sync_io_note() for every page.
1580 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1581 struct cl_page_list *queue, struct cl_sync_io *anchor,
1584 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1589 LASSERT(timeout >= 0);
1591 rc = l_wait_event(anchor->csi_waitq,
1592 atomic_read(&anchor->csi_sync_nr) == 0,
1595 CERROR("SYNC IO failed with error: %d, try to cancel "
1596 "%d remaining pages\n",
1597 rc, atomic_read(&anchor->csi_sync_nr));
1599 (void)cl_io_cancel(env, io, queue);
1601 lwi = (struct l_wait_info) { 0 };
1602 (void)l_wait_event(anchor->csi_waitq,
1603 atomic_read(&anchor->csi_sync_nr) == 0,
1606 rc = anchor->csi_sync_rc;
1608 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1609 cl_page_list_assume(env, io, queue);
1611 /* wait until cl_sync_io_note() has done wakeup */
1612 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1616 POISON(anchor, 0x5a, sizeof *anchor);
1619 EXPORT_SYMBOL(cl_sync_io_wait);
1622 * Indicate that transfer of a single page completed.
1624 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1627 if (anchor->csi_sync_rc == 0 && ioret < 0)
1628 anchor->csi_sync_rc = ioret;
1630 * Synchronous IO done without releasing page lock (e.g., as a part of
1631 * ->{prepare,commit}_write(). Completion is used to signal the end of
1634 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1635 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1636 wake_up_all(&anchor->csi_waitq);
1637 /* it's safe to nuke or reuse anchor now */
1638 atomic_set(&anchor->csi_barrier, 0);
1642 EXPORT_SYMBOL(cl_sync_io_note);