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, 2012, Whamcloud, Inc.
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>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 #include <obd_class.h>
44 #include <obd_support.h>
45 #include <lustre_fid.h>
46 #include <libcfs/list.h>
47 /* lu_time_global_{init,fini}() */
50 #include <cl_object.h>
51 #include "cl_internal.h"
53 /*****************************************************************************
59 #define cl_io_for_each(slice, io) \
60 cfs_list_for_each_entry((slice), &io->ci_layers, cis_linkage)
61 #define cl_io_for_each_reverse(slice, io) \
62 cfs_list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
64 static inline int cl_io_type_is_valid(enum cl_io_type type)
66 return CIT_READ <= type && type < CIT_OP_NR;
69 static inline int cl_io_is_loopable(const struct cl_io *io)
71 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
75 * Returns true iff there is an IO ongoing in the given environment.
77 int cl_io_is_going(const struct lu_env *env)
79 return cl_env_info(env)->clt_current_io != NULL;
81 EXPORT_SYMBOL(cl_io_is_going);
84 * cl_io invariant that holds at all times when exported cl_io_*() functions
85 * are entered and left.
87 static int cl_io_invariant(const struct cl_io *io)
94 * io can own pages only when it is ongoing. Sub-io might
95 * still be in CIS_LOCKED state when top-io is in
98 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
99 (io->ci_state == CIS_LOCKED && up != NULL));
103 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
105 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
107 struct cl_io_slice *slice;
108 struct cl_thread_info *info;
110 LINVRNT(cl_io_type_is_valid(io->ci_type));
111 LINVRNT(cl_io_invariant(io));
114 while (!cfs_list_empty(&io->ci_layers)) {
115 slice = container_of(io->ci_layers.next, struct cl_io_slice,
117 cfs_list_del_init(&slice->cis_linkage);
118 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
119 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
121 * Invalidate slice to catch use after free. This assumes that
122 * slices are allocated within session and can be touched
123 * after ->cio_fini() returns.
125 slice->cis_io = NULL;
127 io->ci_state = CIS_FINI;
128 info = cl_env_info(env);
129 if (info->clt_current_io == io)
130 info->clt_current_io = NULL;
132 /* sanity check for layout change */
133 switch(io->ci_type) {
138 LASSERT(!io->ci_need_restart);
141 /* Check ignore layout change conf */
142 LASSERT(ergo(io->ci_ignore_layout, !io->ci_need_restart));
150 EXPORT_SYMBOL(cl_io_fini);
152 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
153 enum cl_io_type iot, struct cl_object *obj)
155 struct cl_object *scan;
158 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
159 LINVRNT(cl_io_type_is_valid(iot));
160 LINVRNT(cl_io_invariant(io));
164 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
165 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
166 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_done);
167 CFS_INIT_LIST_HEAD(&io->ci_layers);
170 cl_object_for_each(scan, obj) {
171 if (scan->co_ops->coo_io_init != NULL) {
172 result = scan->co_ops->coo_io_init(env, scan, io);
178 io->ci_state = CIS_INIT;
183 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
185 * \pre obj != cl_object_top(obj)
187 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
188 enum cl_io_type iot, struct cl_object *obj)
190 struct cl_thread_info *info = cl_env_info(env);
192 LASSERT(obj != cl_object_top(obj));
193 if (info->clt_current_io == NULL)
194 info->clt_current_io = io;
195 return cl_io_init0(env, io, iot, obj);
197 EXPORT_SYMBOL(cl_io_sub_init);
200 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
202 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
203 * what the latter returned.
205 * \pre obj == cl_object_top(obj)
206 * \pre cl_io_type_is_valid(iot)
207 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
209 int cl_io_init(const struct lu_env *env, struct cl_io *io,
210 enum cl_io_type iot, struct cl_object *obj)
212 struct cl_thread_info *info = cl_env_info(env);
214 LASSERT(obj == cl_object_top(obj));
215 LASSERT(info->clt_current_io == NULL);
217 info->clt_current_io = io;
218 return cl_io_init0(env, io, iot, obj);
220 EXPORT_SYMBOL(cl_io_init);
223 * Initialize read or write io.
225 * \pre iot == CIT_READ || iot == CIT_WRITE
227 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
228 enum cl_io_type iot, loff_t pos, size_t count)
230 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
231 LINVRNT(io->ci_obj != NULL);
234 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
235 "io range: %u ["LPU64", "LPU64") %u %u\n",
236 iot, (__u64)pos, (__u64)pos + count,
237 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
238 io->u.ci_rw.crw_pos = pos;
239 io->u.ci_rw.crw_count = count;
240 RETURN(cl_io_init(env, io, iot, io->ci_obj));
242 EXPORT_SYMBOL(cl_io_rw_init);
244 static inline const struct lu_fid *
245 cl_lock_descr_fid(const struct cl_lock_descr *descr)
247 return lu_object_fid(&descr->cld_obj->co_lu);
250 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
251 const struct cl_lock_descr *d1)
253 return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
254 __diff_normalize(d0->cld_start, d1->cld_start);
257 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
258 const struct cl_lock_descr *d1)
262 ret = lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1));
265 if (d0->cld_end < d1->cld_start)
267 if (d0->cld_start > d0->cld_end)
272 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
273 const struct cl_lock_descr *d1)
275 d0->cld_start = min(d0->cld_start, d1->cld_start);
276 d0->cld_end = max(d0->cld_end, d1->cld_end);
278 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
279 d0->cld_mode = CLM_WRITE;
281 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
282 d0->cld_mode = CLM_GROUP;
286 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
288 static void cl_io_locks_sort(struct cl_io *io)
293 /* hidden treasure: bubble sort for now. */
295 struct cl_io_lock_link *curr;
296 struct cl_io_lock_link *prev;
297 struct cl_io_lock_link *temp;
302 cfs_list_for_each_entry_safe(curr, temp,
303 &io->ci_lockset.cls_todo,
306 switch (cl_lock_descr_sort(&prev->cill_descr,
307 &curr->cill_descr)) {
310 * IMPOSSIBLE: Identical locks are
317 cfs_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 cfs_list_t *queue,
339 const struct cl_lock_descr *need)
341 struct cl_io_lock_link *scan;
344 cfs_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 cfs_list_t *queue,
353 const struct cl_lock_descr *need)
355 struct cl_io_lock_link *scan;
358 cfs_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);
371 static int cl_lockset_match(const struct cl_lockset *set,
372 const struct cl_lock_descr *need)
374 return cl_queue_match(&set->cls_curr, need) ||
375 cl_queue_match(&set->cls_done, need);
378 static int cl_lockset_merge(const struct cl_lockset *set,
379 const struct cl_lock_descr *need)
381 return cl_queue_merge(&set->cls_todo, need) ||
382 cl_lockset_match(set, need);
385 static int cl_lockset_lock_one(const struct lu_env *env,
386 struct cl_io *io, struct cl_lockset *set,
387 struct cl_io_lock_link *link)
389 struct cl_lock *lock;
394 if (io->ci_lockreq == CILR_PEEK) {
395 lock = cl_lock_peek(env, io, &link->cill_descr, "io", io);
397 lock = ERR_PTR(-ENODATA);
399 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
402 link->cill_lock = lock;
403 cfs_list_move(&link->cill_linkage, &set->cls_curr);
404 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
405 result = cl_wait(env, lock);
407 cfs_list_move(&link->cill_linkage,
412 result = PTR_ERR(lock);
416 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
417 struct cl_io_lock_link *link)
419 struct cl_lock *lock = link->cill_lock;
422 cfs_list_del_init(&link->cill_linkage);
424 cl_lock_release(env, lock, "io", io);
425 link->cill_lock = NULL;
427 if (link->cill_fini != NULL)
428 link->cill_fini(env, link);
432 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
433 struct cl_lockset *set)
435 struct cl_io_lock_link *link;
436 struct cl_io_lock_link *temp;
437 struct cl_lock *lock;
442 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
443 if (!cl_lockset_match(set, &link->cill_descr)) {
444 /* XXX some locking to guarantee that locks aren't
445 * expanded in between. */
446 result = cl_lockset_lock_one(env, io, set, link);
450 cl_lock_link_fini(env, io, link);
453 cfs_list_for_each_entry_safe(link, temp,
454 &set->cls_curr, cill_linkage) {
455 lock = link->cill_lock;
456 result = cl_wait(env, lock);
458 cfs_list_move(&link->cill_linkage,
468 * Takes locks necessary for the current iteration of io.
470 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
471 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
474 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
476 const struct cl_io_slice *scan;
479 LINVRNT(cl_io_is_loopable(io));
480 LINVRNT(io->ci_state == CIS_IT_STARTED);
481 LINVRNT(cl_io_invariant(io));
484 cl_io_for_each(scan, io) {
485 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
487 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
492 cl_io_locks_sort(io);
493 result = cl_lockset_lock(env, io, &io->ci_lockset);
496 cl_io_unlock(env, io);
498 io->ci_state = CIS_LOCKED;
501 EXPORT_SYMBOL(cl_io_lock);
504 * Release locks takes by io.
506 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
508 struct cl_lockset *set;
509 struct cl_io_lock_link *link;
510 struct cl_io_lock_link *temp;
511 const struct cl_io_slice *scan;
513 LASSERT(cl_io_is_loopable(io));
514 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
515 LINVRNT(cl_io_invariant(io));
518 set = &io->ci_lockset;
520 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
521 cl_lock_link_fini(env, io, link);
523 cfs_list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
524 cl_lock_link_fini(env, io, link);
526 cfs_list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
527 cl_unuse(env, link->cill_lock);
528 cl_lock_link_fini(env, io, link);
530 cl_io_for_each_reverse(scan, io) {
531 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
532 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
534 io->ci_state = CIS_UNLOCKED;
535 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
538 EXPORT_SYMBOL(cl_io_unlock);
541 * Prepares next iteration of io.
543 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
544 * layers a chance to modify io parameters, e.g., so that lov can restrict io
545 * to a single stripe.
547 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
549 const struct cl_io_slice *scan;
552 LINVRNT(cl_io_is_loopable(io));
553 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
554 LINVRNT(cl_io_invariant(io));
558 cl_io_for_each(scan, io) {
559 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
561 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
567 io->ci_state = CIS_IT_STARTED;
570 EXPORT_SYMBOL(cl_io_iter_init);
573 * Finalizes io iteration.
575 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
577 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
579 const struct cl_io_slice *scan;
581 LINVRNT(cl_io_is_loopable(io));
582 LINVRNT(io->ci_state == CIS_UNLOCKED);
583 LINVRNT(cl_io_invariant(io));
586 cl_io_for_each_reverse(scan, io) {
587 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
588 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
590 io->ci_state = CIS_IT_ENDED;
593 EXPORT_SYMBOL(cl_io_iter_fini);
596 * Records that read or write io progressed \a nob bytes forward.
598 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
600 const struct cl_io_slice *scan;
602 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
604 LINVRNT(cl_io_is_loopable(io));
605 LINVRNT(cl_io_invariant(io));
609 io->u.ci_rw.crw_pos += nob;
610 io->u.ci_rw.crw_count -= nob;
612 /* layers have to be notified. */
613 cl_io_for_each_reverse(scan, io) {
614 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
615 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
620 EXPORT_SYMBOL(cl_io_rw_advance);
623 * Adds a lock to a lockset.
625 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
626 struct cl_io_lock_link *link)
631 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
634 cfs_list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
639 EXPORT_SYMBOL(cl_io_lock_add);
641 static void cl_free_io_lock_link(const struct lu_env *env,
642 struct cl_io_lock_link *link)
648 * Allocates new lock link, and uses it to add a lock to a lockset.
650 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
651 struct cl_lock_descr *descr)
653 struct cl_io_lock_link *link;
659 link->cill_descr = *descr;
660 link->cill_fini = cl_free_io_lock_link;
661 result = cl_io_lock_add(env, io, link);
662 if (result) /* lock match */
663 link->cill_fini(env, link);
669 EXPORT_SYMBOL(cl_io_lock_alloc_add);
672 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
674 int cl_io_start(const struct lu_env *env, struct cl_io *io)
676 const struct cl_io_slice *scan;
679 LINVRNT(cl_io_is_loopable(io));
680 LINVRNT(io->ci_state == CIS_LOCKED);
681 LINVRNT(cl_io_invariant(io));
684 io->ci_state = CIS_IO_GOING;
685 cl_io_for_each(scan, io) {
686 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
688 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
696 EXPORT_SYMBOL(cl_io_start);
699 * Wait until current io iteration is finished by calling
700 * cl_io_operations::cio_end() bottom-to-top.
702 void cl_io_end(const struct lu_env *env, struct cl_io *io)
704 const struct cl_io_slice *scan;
706 LINVRNT(cl_io_is_loopable(io));
707 LINVRNT(io->ci_state == CIS_IO_GOING);
708 LINVRNT(cl_io_invariant(io));
711 cl_io_for_each_reverse(scan, io) {
712 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
713 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
714 /* TODO: error handling. */
716 io->ci_state = CIS_IO_FINISHED;
719 EXPORT_SYMBOL(cl_io_end);
721 static const struct cl_page_slice *
722 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
724 const struct cl_page_slice *slice;
726 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
727 LINVRNT(slice != NULL);
732 * True iff \a page is within \a io range.
734 static int cl_page_in_io(const struct cl_page *page, const struct cl_io *io)
741 idx = page->cp_index;
742 switch (io->ci_type) {
746 * check that [start, end) and [pos, pos + count) extents
749 if (!cl_io_is_append(io)) {
750 const struct cl_io_rw_common *crw = &(io->u.ci_rw);
751 start = cl_offset(page->cp_obj, idx);
752 end = cl_offset(page->cp_obj, idx + 1);
753 result = crw->crw_pos < end &&
754 start < crw->crw_pos + crw->crw_count;
758 result = io->u.ci_fault.ft_index == idx;
767 * Called by read io, when page has to be read from the server.
769 * \see cl_io_operations::cio_read_page()
771 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
772 struct cl_page *page)
774 const struct cl_io_slice *scan;
775 struct cl_2queue *queue;
778 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
779 LINVRNT(cl_page_is_owned(page, io));
780 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
781 LINVRNT(cl_page_in_io(page, io));
782 LINVRNT(cl_io_invariant(io));
785 queue = &io->ci_queue;
787 cl_2queue_init(queue);
789 * ->cio_read_page() methods called in the loop below are supposed to
790 * never block waiting for network (the only subtle point is the
791 * creation of new pages for read-ahead that might result in cache
792 * shrinking, but currently only clean pages are shrunk and this
793 * requires no network io).
795 * Should this ever starts blocking, retry loop would be needed for
796 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
798 cl_io_for_each(scan, io) {
799 if (scan->cis_iop->cio_read_page != NULL) {
800 const struct cl_page_slice *slice;
802 slice = cl_io_slice_page(scan, page);
803 LINVRNT(slice != NULL);
804 result = scan->cis_iop->cio_read_page(env, scan, slice);
810 result = cl_io_submit_rw(env, io, CRT_READ, queue);
812 * Unlock unsent pages in case of error.
814 cl_page_list_disown(env, io, &queue->c2_qin);
815 cl_2queue_fini(env, queue);
818 EXPORT_SYMBOL(cl_io_read_page);
821 * Called by write io to prepare page to receive data from user buffer.
823 * \see cl_io_operations::cio_prepare_write()
825 int cl_io_prepare_write(const struct lu_env *env, struct cl_io *io,
826 struct cl_page *page, unsigned from, unsigned to)
828 const struct cl_io_slice *scan;
831 LINVRNT(io->ci_type == CIT_WRITE);
832 LINVRNT(cl_page_is_owned(page, io));
833 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
834 LINVRNT(cl_io_invariant(io));
835 LASSERT(cl_page_in_io(page, io));
838 cl_io_for_each_reverse(scan, io) {
839 if (scan->cis_iop->cio_prepare_write != NULL) {
840 const struct cl_page_slice *slice;
842 slice = cl_io_slice_page(scan, page);
843 result = scan->cis_iop->cio_prepare_write(env, scan,
852 EXPORT_SYMBOL(cl_io_prepare_write);
855 * Called by write io after user data were copied into a page.
857 * \see cl_io_operations::cio_commit_write()
859 int cl_io_commit_write(const struct lu_env *env, struct cl_io *io,
860 struct cl_page *page, unsigned from, unsigned to)
862 const struct cl_io_slice *scan;
865 LINVRNT(io->ci_type == CIT_WRITE);
866 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
867 LINVRNT(cl_io_invariant(io));
869 * XXX Uh... not nice. Top level cl_io_commit_write() call (vvp->lov)
870 * already called cl_page_cache_add(), moving page into CPS_CACHED
871 * state. Better (and more general) way of dealing with such situation
874 LASSERT(cl_page_is_owned(page, io) || page->cp_parent != NULL);
875 LASSERT(cl_page_in_io(page, io));
878 cl_io_for_each(scan, io) {
879 if (scan->cis_iop->cio_commit_write != NULL) {
880 const struct cl_page_slice *slice;
882 slice = cl_io_slice_page(scan, page);
883 result = scan->cis_iop->cio_commit_write(env, scan,
890 LINVRNT(result <= 0);
893 EXPORT_SYMBOL(cl_io_commit_write);
896 * Submits a list of pages for immediate io.
898 * After the function gets returned, The submitted pages are moved to
899 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
900 * to be submitted, and the pages are errant to submit.
902 * \returns 0 if at least one page was submitted, error code otherwise.
903 * \see cl_io_operations::cio_submit()
905 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
906 enum cl_req_type crt, struct cl_2queue *queue)
908 const struct cl_io_slice *scan;
911 LINVRNT(crt < ARRAY_SIZE(scan->cis_iop->req_op));
914 cl_io_for_each(scan, io) {
915 if (scan->cis_iop->req_op[crt].cio_submit == NULL)
917 result = scan->cis_iop->req_op[crt].cio_submit(env, scan, crt,
923 * If ->cio_submit() failed, no pages were sent.
925 LASSERT(ergo(result != 0, cfs_list_empty(&queue->c2_qout.pl_pages)));
928 EXPORT_SYMBOL(cl_io_submit_rw);
931 * Submit a sync_io and wait for the IO to be finished, or error happens.
932 * If \a timeout is zero, it means to wait for the IO unconditionally.
934 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
935 enum cl_req_type iot, struct cl_2queue *queue,
938 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
942 cl_page_list_for_each(pg, &queue->c2_qin) {
943 LASSERT(pg->cp_sync_io == NULL);
944 pg->cp_sync_io = anchor;
947 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
948 rc = cl_io_submit_rw(env, io, iot, queue);
951 * If some pages weren't sent for any reason (e.g.,
952 * read found up-to-date pages in the cache, or write found
953 * clean pages), count them as completed to avoid infinite
956 cl_page_list_for_each(pg, &queue->c2_qin) {
957 pg->cp_sync_io = NULL;
958 cl_sync_io_note(anchor, +1);
961 /* wait for the IO to be finished. */
962 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
965 LASSERT(cfs_list_empty(&queue->c2_qout.pl_pages));
966 cl_page_list_for_each(pg, &queue->c2_qin)
967 pg->cp_sync_io = NULL;
971 EXPORT_SYMBOL(cl_io_submit_sync);
974 * Cancel an IO which has been submitted by cl_io_submit_rw.
976 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
977 struct cl_page_list *queue)
979 struct cl_page *page;
982 CERROR("Canceling ongoing page trasmission\n");
983 cl_page_list_for_each(page, queue) {
986 LINVRNT(cl_page_in_io(page, io));
987 rc = cl_page_cancel(env, page);
988 result = result ?: rc;
992 EXPORT_SYMBOL(cl_io_cancel);
997 * Pumps io through iterations calling
999 * - cl_io_iter_init()
1009 * - cl_io_iter_fini()
1011 * repeatedly until there is no more io to do.
1013 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
1017 LINVRNT(cl_io_is_loopable(io));
1023 io->ci_continue = 0;
1024 result = cl_io_iter_init(env, io);
1027 result = cl_io_lock(env, io);
1030 * Notify layers that locks has been taken,
1031 * and do actual i/o.
1033 * - llite: kms, short read;
1034 * - llite: generic_file_read();
1036 result = cl_io_start(env, io);
1038 * Send any remaining pending
1041 * - llite: ll_rw_stats_tally.
1044 cl_io_unlock(env, io);
1045 cl_io_rw_advance(env, io, io->ci_nob - nob);
1048 cl_io_iter_fini(env, io);
1049 } while (result == 0 && io->ci_continue);
1051 result = io->ci_result;
1052 RETURN(result < 0 ? result : 0);
1054 EXPORT_SYMBOL(cl_io_loop);
1057 * Adds io slice to the cl_io.
1059 * This is called by cl_object_operations::coo_io_init() methods to add a
1060 * per-layer state to the io. New state is added at the end of
1061 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
1063 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
1065 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
1066 struct cl_object *obj,
1067 const struct cl_io_operations *ops)
1069 cfs_list_t *linkage = &slice->cis_linkage;
1071 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
1072 cfs_list_empty(linkage));
1075 cfs_list_add_tail(linkage, &io->ci_layers);
1077 slice->cis_obj = obj;
1078 slice->cis_iop = ops;
1081 EXPORT_SYMBOL(cl_io_slice_add);
1085 * Initializes page list.
1087 void cl_page_list_init(struct cl_page_list *plist)
1091 CFS_INIT_LIST_HEAD(&plist->pl_pages);
1092 plist->pl_owner = cfs_current();
1095 EXPORT_SYMBOL(cl_page_list_init);
1098 * Adds a page to a page list.
1100 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1103 /* it would be better to check that page is owned by "current" io, but
1104 * it is not passed here. */
1105 LASSERT(page->cp_owner != NULL);
1106 LINVRNT(plist->pl_owner == cfs_current());
1109 cfs_mutex_lock(&page->cp_mutex);
1111 LASSERT(cfs_list_empty(&page->cp_batch));
1112 cfs_list_add_tail(&page->cp_batch, &plist->pl_pages);
1114 page->cp_queue_ref = lu_ref_add(&page->cp_reference, "queue", plist);
1118 EXPORT_SYMBOL(cl_page_list_add);
1121 * Removes a page from a page list.
1123 void cl_page_list_del(const struct lu_env *env,
1124 struct cl_page_list *plist, struct cl_page *page)
1126 LASSERT(plist->pl_nr > 0);
1127 LINVRNT(plist->pl_owner == cfs_current());
1130 cfs_list_del_init(&page->cp_batch);
1132 cfs_mutex_unlock(&page->cp_mutex);
1135 lu_ref_del_at(&page->cp_reference, page->cp_queue_ref, "queue", plist);
1136 cl_page_put(env, page);
1139 EXPORT_SYMBOL(cl_page_list_del);
1142 * Moves a page from one page list to another.
1144 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1145 struct cl_page *page)
1147 LASSERT(src->pl_nr > 0);
1148 LINVRNT(dst->pl_owner == cfs_current());
1149 LINVRNT(src->pl_owner == cfs_current());
1152 cfs_list_move_tail(&page->cp_batch, &dst->pl_pages);
1155 lu_ref_set_at(&page->cp_reference,
1156 page->cp_queue_ref, "queue", src, dst);
1159 EXPORT_SYMBOL(cl_page_list_move);
1162 * splice the cl_page_list, just as list head does
1164 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1166 struct cl_page *page;
1167 struct cl_page *tmp;
1169 LINVRNT(list->pl_owner == cfs_current());
1170 LINVRNT(head->pl_owner == cfs_current());
1173 cl_page_list_for_each_safe(page, tmp, list)
1174 cl_page_list_move(head, list, page);
1177 EXPORT_SYMBOL(cl_page_list_splice);
1179 void cl_page_disown0(const struct lu_env *env,
1180 struct cl_io *io, struct cl_page *pg);
1183 * Disowns pages in a queue.
1185 void cl_page_list_disown(const struct lu_env *env,
1186 struct cl_io *io, struct cl_page_list *plist)
1188 struct cl_page *page;
1189 struct cl_page *temp;
1191 LINVRNT(plist->pl_owner == cfs_current());
1194 cl_page_list_for_each_safe(page, temp, plist) {
1195 LASSERT(plist->pl_nr > 0);
1197 cfs_list_del_init(&page->cp_batch);
1199 cfs_mutex_unlock(&page->cp_mutex);
1203 * cl_page_disown0 rather than usual cl_page_disown() is used,
1204 * because pages are possibly in CPS_FREEING state already due
1205 * to the call to cl_page_list_discard().
1208 * XXX cl_page_disown0() will fail if page is not locked.
1210 cl_page_disown0(env, io, page);
1211 lu_ref_del(&page->cp_reference, "queue", plist);
1212 cl_page_put(env, page);
1216 EXPORT_SYMBOL(cl_page_list_disown);
1219 * Releases pages from queue.
1221 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1223 struct cl_page *page;
1224 struct cl_page *temp;
1226 LINVRNT(plist->pl_owner == cfs_current());
1229 cl_page_list_for_each_safe(page, temp, plist)
1230 cl_page_list_del(env, plist, page);
1231 LASSERT(plist->pl_nr == 0);
1234 EXPORT_SYMBOL(cl_page_list_fini);
1237 * Owns all pages in a queue.
1239 int cl_page_list_own(const struct lu_env *env,
1240 struct cl_io *io, struct cl_page_list *plist)
1242 struct cl_page *page;
1243 struct cl_page *temp;
1247 LINVRNT(plist->pl_owner == cfs_current());
1251 cl_page_list_for_each_safe(page, temp, plist) {
1252 LASSERT(index <= page->cp_index);
1253 index = page->cp_index;
1254 if (cl_page_own(env, io, page) == 0)
1255 result = result ?: page->cp_error;
1257 cl_page_list_del(env, plist, page);
1261 EXPORT_SYMBOL(cl_page_list_own);
1264 * Assumes all pages in a queue.
1266 void cl_page_list_assume(const struct lu_env *env,
1267 struct cl_io *io, struct cl_page_list *plist)
1269 struct cl_page *page;
1271 LINVRNT(plist->pl_owner == cfs_current());
1273 cl_page_list_for_each(page, plist)
1274 cl_page_assume(env, io, page);
1276 EXPORT_SYMBOL(cl_page_list_assume);
1279 * Discards all pages in a queue.
1281 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1282 struct cl_page_list *plist)
1284 struct cl_page *page;
1286 LINVRNT(plist->pl_owner == cfs_current());
1288 cl_page_list_for_each(page, plist)
1289 cl_page_discard(env, io, page);
1292 EXPORT_SYMBOL(cl_page_list_discard);
1295 * Unmaps all pages in a queue from user virtual memory.
1297 int cl_page_list_unmap(const struct lu_env *env, struct cl_io *io,
1298 struct cl_page_list *plist)
1300 struct cl_page *page;
1303 LINVRNT(plist->pl_owner == cfs_current());
1306 cl_page_list_for_each(page, plist) {
1307 result = cl_page_unmap(env, io, page);
1313 EXPORT_SYMBOL(cl_page_list_unmap);
1316 * Initialize dual page queue.
1318 void cl_2queue_init(struct cl_2queue *queue)
1321 cl_page_list_init(&queue->c2_qin);
1322 cl_page_list_init(&queue->c2_qout);
1325 EXPORT_SYMBOL(cl_2queue_init);
1328 * Add a page to the incoming page list of 2-queue.
1330 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1333 cl_page_list_add(&queue->c2_qin, page);
1336 EXPORT_SYMBOL(cl_2queue_add);
1339 * Disown pages in both lists of a 2-queue.
1341 void cl_2queue_disown(const struct lu_env *env,
1342 struct cl_io *io, struct cl_2queue *queue)
1345 cl_page_list_disown(env, io, &queue->c2_qin);
1346 cl_page_list_disown(env, io, &queue->c2_qout);
1349 EXPORT_SYMBOL(cl_2queue_disown);
1352 * Discard (truncate) pages in both lists of a 2-queue.
1354 void cl_2queue_discard(const struct lu_env *env,
1355 struct cl_io *io, struct cl_2queue *queue)
1358 cl_page_list_discard(env, io, &queue->c2_qin);
1359 cl_page_list_discard(env, io, &queue->c2_qout);
1362 EXPORT_SYMBOL(cl_2queue_discard);
1365 * Assume to own the pages in cl_2queue
1367 void cl_2queue_assume(const struct lu_env *env,
1368 struct cl_io *io, struct cl_2queue *queue)
1370 cl_page_list_assume(env, io, &queue->c2_qin);
1371 cl_page_list_assume(env, io, &queue->c2_qout);
1373 EXPORT_SYMBOL(cl_2queue_assume);
1376 * Finalize both page lists of a 2-queue.
1378 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1381 cl_page_list_fini(env, &queue->c2_qout);
1382 cl_page_list_fini(env, &queue->c2_qin);
1385 EXPORT_SYMBOL(cl_2queue_fini);
1388 * Initialize a 2-queue to contain \a page in its incoming page list.
1390 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1393 cl_2queue_init(queue);
1394 cl_2queue_add(queue, page);
1397 EXPORT_SYMBOL(cl_2queue_init_page);
1400 * Returns top-level io.
1402 * \see cl_object_top(), cl_page_top().
1404 struct cl_io *cl_io_top(struct cl_io *io)
1407 while (io->ci_parent != NULL)
1411 EXPORT_SYMBOL(cl_io_top);
1414 * Prints human readable representation of \a io to the \a f.
1416 void cl_io_print(const struct lu_env *env, void *cookie,
1417 lu_printer_t printer, const struct cl_io *io)
1422 * Adds request slice to the compound request.
1424 * This is called by cl_device_operations::cdo_req_init() methods to add a
1425 * per-layer state to the request. New state is added at the end of
1426 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1428 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1430 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1431 struct cl_device *dev,
1432 const struct cl_req_operations *ops)
1435 cfs_list_add_tail(&slice->crs_linkage, &req->crq_layers);
1436 slice->crs_dev = dev;
1437 slice->crs_ops = ops;
1438 slice->crs_req = req;
1441 EXPORT_SYMBOL(cl_req_slice_add);
1443 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1447 LASSERT(cfs_list_empty(&req->crq_pages));
1448 LASSERT(req->crq_nrpages == 0);
1449 LINVRNT(cfs_list_empty(&req->crq_layers));
1450 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1453 if (req->crq_o != NULL) {
1454 for (i = 0; i < req->crq_nrobjs; ++i) {
1455 struct cl_object *obj = req->crq_o[i].ro_obj;
1457 lu_object_ref_del_at(&obj->co_lu,
1458 req->crq_o[i].ro_obj_ref,
1460 cl_object_put(env, obj);
1463 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1469 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1470 struct cl_page *page)
1472 struct cl_device *dev;
1473 struct cl_page_slice *slice;
1478 page = cl_page_top(page);
1480 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1481 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1482 if (dev->cd_ops->cdo_req_init != NULL) {
1483 result = dev->cd_ops->cdo_req_init(env,
1489 page = page->cp_child;
1490 } while (page != NULL && result == 0);
1495 * Invokes per-request transfer completion call-backs
1496 * (cl_req_operations::cro_completion()) bottom-to-top.
1498 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1500 struct cl_req_slice *slice;
1504 * for the lack of list_for_each_entry_reverse_safe()...
1506 while (!cfs_list_empty(&req->crq_layers)) {
1507 slice = cfs_list_entry(req->crq_layers.prev,
1508 struct cl_req_slice, crs_linkage);
1509 cfs_list_del_init(&slice->crs_linkage);
1510 if (slice->crs_ops->cro_completion != NULL)
1511 slice->crs_ops->cro_completion(env, slice, rc);
1513 cl_req_free(env, req);
1516 EXPORT_SYMBOL(cl_req_completion);
1519 * Allocates new transfer request.
1521 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1522 enum cl_req_type crt, int nr_objects)
1526 LINVRNT(nr_objects > 0);
1533 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1534 if (req->crq_o != NULL) {
1535 req->crq_nrobjs = nr_objects;
1536 req->crq_type = crt;
1537 CFS_INIT_LIST_HEAD(&req->crq_pages);
1538 CFS_INIT_LIST_HEAD(&req->crq_layers);
1539 result = cl_req_init(env, req, page);
1543 cl_req_completion(env, req, result);
1544 req = ERR_PTR(result);
1547 req = ERR_PTR(-ENOMEM);
1550 EXPORT_SYMBOL(cl_req_alloc);
1553 * Adds a page to a request.
1555 void cl_req_page_add(const struct lu_env *env,
1556 struct cl_req *req, struct cl_page *page)
1558 struct cl_object *obj;
1559 struct cl_req_obj *rqo;
1563 page = cl_page_top(page);
1565 LASSERT(cfs_list_empty(&page->cp_flight));
1566 LASSERT(page->cp_req == NULL);
1568 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1569 req, req->crq_type, req->crq_nrpages);
1571 cfs_list_add_tail(&page->cp_flight, &req->crq_pages);
1574 obj = cl_object_top(page->cp_obj);
1575 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1576 if (rqo->ro_obj == NULL) {
1579 rqo->ro_obj_ref = lu_object_ref_add(&obj->co_lu,
1584 LASSERT(i < req->crq_nrobjs);
1587 EXPORT_SYMBOL(cl_req_page_add);
1590 * Removes a page from a request.
1592 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1594 struct cl_req *req = page->cp_req;
1597 page = cl_page_top(page);
1599 LASSERT(!cfs_list_empty(&page->cp_flight));
1600 LASSERT(req->crq_nrpages > 0);
1602 cfs_list_del_init(&page->cp_flight);
1604 page->cp_req = NULL;
1607 EXPORT_SYMBOL(cl_req_page_done);
1610 * Notifies layers that request is about to depart by calling
1611 * cl_req_operations::cro_prep() top-to-bottom.
1613 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1617 const struct cl_req_slice *slice;
1621 * Check that the caller of cl_req_alloc() didn't lie about the number
1624 for (i = 0; i < req->crq_nrobjs; ++i)
1625 LASSERT(req->crq_o[i].ro_obj != NULL);
1628 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1629 if (slice->crs_ops->cro_prep != NULL) {
1630 result = slice->crs_ops->cro_prep(env, slice);
1637 EXPORT_SYMBOL(cl_req_prep);
1640 * Fills in attributes that are passed to server together with transfer. Only
1641 * attributes from \a flags may be touched. This can be called multiple times
1642 * for the same request.
1644 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1645 struct cl_req_attr *attr, obd_valid flags)
1647 const struct cl_req_slice *slice;
1648 struct cl_page *page;
1651 LASSERT(!cfs_list_empty(&req->crq_pages));
1654 /* Take any page to use as a model. */
1655 page = cfs_list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1657 for (i = 0; i < req->crq_nrobjs; ++i) {
1658 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1659 const struct cl_page_slice *scan;
1660 const struct cl_object *obj;
1662 scan = cl_page_at(page,
1663 slice->crs_dev->cd_lu_dev.ld_type);
1664 LASSERT(scan != NULL);
1665 obj = scan->cpl_obj;
1666 if (slice->crs_ops->cro_attr_set != NULL)
1667 slice->crs_ops->cro_attr_set(env, slice, obj,
1673 EXPORT_SYMBOL(cl_req_attr_set);
1675 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1676 * implemented in libcfs. */
1678 # include <linux/sched.h>
1679 #else /* __KERNEL__ */
1680 # include <liblustre.h>
1684 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1686 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1689 cfs_waitq_init(&anchor->csi_waitq);
1690 cfs_atomic_set(&anchor->csi_sync_nr, nrpages);
1691 anchor->csi_sync_rc = 0;
1694 EXPORT_SYMBOL(cl_sync_io_init);
1697 * Wait until all transfer completes. Transfer completion routine has to call
1698 * cl_sync_io_note() for every page.
1700 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1701 struct cl_page_list *queue, struct cl_sync_io *anchor,
1704 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1709 LASSERT(timeout >= 0);
1711 rc = l_wait_event(anchor->csi_waitq,
1712 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1715 CERROR("SYNC IO failed with error: %d, try to cancel "
1716 "%d remaining pages\n",
1717 rc, cfs_atomic_read(&anchor->csi_sync_nr));
1719 (void)cl_io_cancel(env, io, queue);
1721 lwi = (struct l_wait_info) { 0 };
1722 (void)l_wait_event(anchor->csi_waitq,
1723 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1726 rc = anchor->csi_sync_rc;
1728 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) == 0);
1729 cl_page_list_assume(env, io, queue);
1730 POISON(anchor, 0x5a, sizeof *anchor);
1733 EXPORT_SYMBOL(cl_sync_io_wait);
1736 * Indicate that transfer of a single page completed.
1738 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1741 if (anchor->csi_sync_rc == 0 && ioret < 0)
1742 anchor->csi_sync_rc = ioret;
1744 * Synchronous IO done without releasing page lock (e.g., as a part of
1745 * ->{prepare,commit}_write(). Completion is used to signal the end of
1748 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) > 0);
1749 if (cfs_atomic_dec_and_test(&anchor->csi_sync_nr))
1750 cfs_waitq_broadcast(&anchor->csi_waitq);
1753 EXPORT_SYMBOL(cl_sync_io_note);