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, 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>
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 #include <cl_object.h>
48 #include "cl_internal.h"
50 /*****************************************************************************
56 #define cl_io_for_each(slice, io) \
57 cfs_list_for_each_entry((slice), &io->ci_layers, cis_linkage)
58 #define cl_io_for_each_reverse(slice, io) \
59 cfs_list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
61 static inline int cl_io_type_is_valid(enum cl_io_type type)
63 return CIT_READ <= type && type < CIT_OP_NR;
66 static inline int cl_io_is_loopable(const struct cl_io *io)
68 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
72 * Returns true iff there is an IO ongoing in the given environment.
74 int cl_io_is_going(const struct lu_env *env)
76 return cl_env_info(env)->clt_current_io != NULL;
78 EXPORT_SYMBOL(cl_io_is_going);
81 * cl_io invariant that holds at all times when exported cl_io_*() functions
82 * are entered and left.
84 static int cl_io_invariant(const struct cl_io *io)
91 * io can own pages only when it is ongoing. Sub-io might
92 * still be in CIS_LOCKED state when top-io is in
95 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
96 (io->ci_state == CIS_LOCKED && up != NULL));
100 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
102 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
104 struct cl_io_slice *slice;
105 struct cl_thread_info *info;
107 LINVRNT(cl_io_type_is_valid(io->ci_type));
108 LINVRNT(cl_io_invariant(io));
111 while (!cfs_list_empty(&io->ci_layers)) {
112 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
114 cfs_list_del_init(&slice->cis_linkage);
115 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
116 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
118 * Invalidate slice to catch use after free. This assumes that
119 * slices are allocated within session and can be touched
120 * after ->cio_fini() returns.
122 slice->cis_io = NULL;
124 io->ci_state = CIS_FINI;
125 info = cl_env_info(env);
126 if (info->clt_current_io == io)
127 info->clt_current_io = NULL;
129 /* sanity check for layout change */
130 switch(io->ci_type) {
135 LASSERT(!io->ci_need_restart);
139 /* Check ignore layout change conf */
140 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
141 !io->ci_need_restart));
148 EXPORT_SYMBOL(cl_io_fini);
150 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
151 enum cl_io_type iot, struct cl_object *obj)
153 struct cl_object *scan;
156 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
157 LINVRNT(cl_io_type_is_valid(iot));
158 LINVRNT(cl_io_invariant(io));
162 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
163 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
164 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_done);
165 CFS_INIT_LIST_HEAD(&io->ci_layers);
168 cl_object_for_each(scan, obj) {
169 if (scan->co_ops->coo_io_init != NULL) {
170 result = scan->co_ops->coo_io_init(env, scan, io);
176 io->ci_state = CIS_INIT;
181 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
183 * \pre obj != cl_object_top(obj)
185 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
186 enum cl_io_type iot, struct cl_object *obj)
188 struct cl_thread_info *info = cl_env_info(env);
190 LASSERT(obj != cl_object_top(obj));
191 if (info->clt_current_io == NULL)
192 info->clt_current_io = io;
193 return cl_io_init0(env, io, iot, obj);
195 EXPORT_SYMBOL(cl_io_sub_init);
198 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
200 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
201 * what the latter returned.
203 * \pre obj == cl_object_top(obj)
204 * \pre cl_io_type_is_valid(iot)
205 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
207 int cl_io_init(const struct lu_env *env, struct cl_io *io,
208 enum cl_io_type iot, struct cl_object *obj)
210 struct cl_thread_info *info = cl_env_info(env);
212 LASSERT(obj == cl_object_top(obj));
213 LASSERT(info->clt_current_io == NULL);
215 info->clt_current_io = io;
216 return cl_io_init0(env, io, iot, obj);
218 EXPORT_SYMBOL(cl_io_init);
221 * Initialize read or write io.
223 * \pre iot == CIT_READ || iot == CIT_WRITE
225 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
226 enum cl_io_type iot, loff_t pos, size_t count)
228 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
229 LINVRNT(io->ci_obj != NULL);
232 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
233 "io range: %u ["LPU64", "LPU64") %u %u\n",
234 iot, (__u64)pos, (__u64)pos + count,
235 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
236 io->u.ci_rw.crw_pos = pos;
237 io->u.ci_rw.crw_count = count;
238 RETURN(cl_io_init(env, io, iot, io->ci_obj));
240 EXPORT_SYMBOL(cl_io_rw_init);
242 static inline const struct lu_fid *
243 cl_lock_descr_fid(const struct cl_lock_descr *descr)
245 return lu_object_fid(&descr->cld_obj->co_lu);
248 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
249 const struct cl_lock_descr *d1)
251 return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
252 __diff_normalize(d0->cld_start, d1->cld_start);
255 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
256 const struct cl_lock_descr *d1)
260 ret = lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1));
263 if (d0->cld_end < d1->cld_start)
265 if (d0->cld_start > d0->cld_end)
270 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
271 const struct cl_lock_descr *d1)
273 d0->cld_start = min(d0->cld_start, d1->cld_start);
274 d0->cld_end = max(d0->cld_end, d1->cld_end);
276 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
277 d0->cld_mode = CLM_WRITE;
279 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
280 d0->cld_mode = CLM_GROUP;
284 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
286 static void cl_io_locks_sort(struct cl_io *io)
291 /* hidden treasure: bubble sort for now. */
293 struct cl_io_lock_link *curr;
294 struct cl_io_lock_link *prev;
295 struct cl_io_lock_link *temp;
300 cfs_list_for_each_entry_safe(curr, temp,
301 &io->ci_lockset.cls_todo,
304 switch (cl_lock_descr_sort(&prev->cill_descr,
305 &curr->cill_descr)) {
308 * IMPOSSIBLE: Identical locks are
315 cfs_list_move_tail(&curr->cill_linkage,
316 &prev->cill_linkage);
318 continue; /* don't change prev: it's
319 * still "previous" */
320 case -1: /* already in order */
331 * Check whether \a queue contains locks matching \a need.
333 * \retval +ve there is a matching lock in the \a queue
334 * \retval 0 there are no matching locks in the \a queue
336 int cl_queue_match(const cfs_list_t *queue,
337 const struct cl_lock_descr *need)
339 struct cl_io_lock_link *scan;
342 cfs_list_for_each_entry(scan, queue, cill_linkage) {
343 if (cl_lock_descr_match(&scan->cill_descr, need))
348 EXPORT_SYMBOL(cl_queue_match);
350 static int cl_queue_merge(const cfs_list_t *queue,
351 const struct cl_lock_descr *need)
353 struct cl_io_lock_link *scan;
356 cfs_list_for_each_entry(scan, queue, cill_linkage) {
357 if (cl_lock_descr_cmp(&scan->cill_descr, need))
359 cl_lock_descr_merge(&scan->cill_descr, need);
360 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
361 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
362 scan->cill_descr.cld_end);
369 static int cl_lockset_match(const struct cl_lockset *set,
370 const struct cl_lock_descr *need)
372 return cl_queue_match(&set->cls_curr, need) ||
373 cl_queue_match(&set->cls_done, need);
376 static int cl_lockset_merge(const struct cl_lockset *set,
377 const struct cl_lock_descr *need)
379 return cl_queue_merge(&set->cls_todo, need) ||
380 cl_lockset_match(set, need);
383 static int cl_lockset_lock_one(const struct lu_env *env,
384 struct cl_io *io, struct cl_lockset *set,
385 struct cl_io_lock_link *link)
387 struct cl_lock *lock;
392 if (io->ci_lockreq == CILR_PEEK) {
393 lock = cl_lock_peek(env, io, &link->cill_descr, "io", io);
395 lock = ERR_PTR(-ENODATA);
397 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
400 link->cill_lock = lock;
401 cfs_list_move(&link->cill_linkage, &set->cls_curr);
402 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
403 result = cl_wait(env, lock);
405 cfs_list_move(&link->cill_linkage,
410 result = PTR_ERR(lock);
414 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
415 struct cl_io_lock_link *link)
417 struct cl_lock *lock = link->cill_lock;
420 cfs_list_del_init(&link->cill_linkage);
422 cl_lock_release(env, lock, "io", io);
423 link->cill_lock = NULL;
425 if (link->cill_fini != NULL)
426 link->cill_fini(env, link);
430 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
431 struct cl_lockset *set)
433 struct cl_io_lock_link *link;
434 struct cl_io_lock_link *temp;
435 struct cl_lock *lock;
440 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
441 if (!cl_lockset_match(set, &link->cill_descr)) {
442 /* XXX some locking to guarantee that locks aren't
443 * expanded in between. */
444 result = cl_lockset_lock_one(env, io, set, link);
448 cl_lock_link_fini(env, io, link);
451 cfs_list_for_each_entry_safe(link, temp,
452 &set->cls_curr, cill_linkage) {
453 lock = link->cill_lock;
454 result = cl_wait(env, lock);
456 cfs_list_move(&link->cill_linkage,
466 * Takes locks necessary for the current iteration of io.
468 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
469 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
472 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
474 const struct cl_io_slice *scan;
477 LINVRNT(cl_io_is_loopable(io));
478 LINVRNT(io->ci_state == CIS_IT_STARTED);
479 LINVRNT(cl_io_invariant(io));
482 cl_io_for_each(scan, io) {
483 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
485 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
490 cl_io_locks_sort(io);
491 result = cl_lockset_lock(env, io, &io->ci_lockset);
494 cl_io_unlock(env, io);
496 io->ci_state = CIS_LOCKED;
499 EXPORT_SYMBOL(cl_io_lock);
502 * Release locks takes by io.
504 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
506 struct cl_lockset *set;
507 struct cl_io_lock_link *link;
508 struct cl_io_lock_link *temp;
509 const struct cl_io_slice *scan;
511 LASSERT(cl_io_is_loopable(io));
512 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
513 LINVRNT(cl_io_invariant(io));
516 set = &io->ci_lockset;
518 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
519 cl_lock_link_fini(env, io, link);
521 cfs_list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
522 cl_lock_link_fini(env, io, link);
524 cfs_list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
525 cl_unuse(env, link->cill_lock);
526 cl_lock_link_fini(env, io, link);
528 cl_io_for_each_reverse(scan, io) {
529 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
530 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
532 io->ci_state = CIS_UNLOCKED;
533 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
536 EXPORT_SYMBOL(cl_io_unlock);
539 * Prepares next iteration of io.
541 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
542 * layers a chance to modify io parameters, e.g., so that lov can restrict io
543 * to a single stripe.
545 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
547 const struct cl_io_slice *scan;
550 LINVRNT(cl_io_is_loopable(io));
551 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
552 LINVRNT(cl_io_invariant(io));
556 cl_io_for_each(scan, io) {
557 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
559 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
565 io->ci_state = CIS_IT_STARTED;
568 EXPORT_SYMBOL(cl_io_iter_init);
571 * Finalizes io iteration.
573 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
575 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
577 const struct cl_io_slice *scan;
579 LINVRNT(cl_io_is_loopable(io));
580 LINVRNT(io->ci_state == CIS_UNLOCKED);
581 LINVRNT(cl_io_invariant(io));
584 cl_io_for_each_reverse(scan, io) {
585 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
586 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
588 io->ci_state = CIS_IT_ENDED;
591 EXPORT_SYMBOL(cl_io_iter_fini);
594 * Records that read or write io progressed \a nob bytes forward.
596 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
598 const struct cl_io_slice *scan;
600 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
602 LINVRNT(cl_io_is_loopable(io));
603 LINVRNT(cl_io_invariant(io));
607 io->u.ci_rw.crw_pos += nob;
608 io->u.ci_rw.crw_count -= nob;
610 /* layers have to be notified. */
611 cl_io_for_each_reverse(scan, io) {
612 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
613 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
618 EXPORT_SYMBOL(cl_io_rw_advance);
621 * Adds a lock to a lockset.
623 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
624 struct cl_io_lock_link *link)
629 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
632 cfs_list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
637 EXPORT_SYMBOL(cl_io_lock_add);
639 static void cl_free_io_lock_link(const struct lu_env *env,
640 struct cl_io_lock_link *link)
646 * Allocates new lock link, and uses it to add a lock to a lockset.
648 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
649 struct cl_lock_descr *descr)
651 struct cl_io_lock_link *link;
657 link->cill_descr = *descr;
658 link->cill_fini = cl_free_io_lock_link;
659 result = cl_io_lock_add(env, io, link);
660 if (result) /* lock match */
661 link->cill_fini(env, link);
667 EXPORT_SYMBOL(cl_io_lock_alloc_add);
670 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
672 int cl_io_start(const struct lu_env *env, struct cl_io *io)
674 const struct cl_io_slice *scan;
677 LINVRNT(cl_io_is_loopable(io));
678 LINVRNT(io->ci_state == CIS_LOCKED);
679 LINVRNT(cl_io_invariant(io));
682 io->ci_state = CIS_IO_GOING;
683 cl_io_for_each(scan, io) {
684 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
686 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
694 EXPORT_SYMBOL(cl_io_start);
697 * Wait until current io iteration is finished by calling
698 * cl_io_operations::cio_end() bottom-to-top.
700 void cl_io_end(const struct lu_env *env, struct cl_io *io)
702 const struct cl_io_slice *scan;
704 LINVRNT(cl_io_is_loopable(io));
705 LINVRNT(io->ci_state == CIS_IO_GOING);
706 LINVRNT(cl_io_invariant(io));
709 cl_io_for_each_reverse(scan, io) {
710 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
711 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
712 /* TODO: error handling. */
714 io->ci_state = CIS_IO_FINISHED;
717 EXPORT_SYMBOL(cl_io_end);
719 static const struct cl_page_slice *
720 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
722 const struct cl_page_slice *slice;
724 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
725 LINVRNT(slice != NULL);
730 * True iff \a page is within \a io range.
732 static int cl_page_in_io(const struct cl_page *page, const struct cl_io *io)
739 idx = page->cp_index;
740 switch (io->ci_type) {
744 * check that [start, end) and [pos, pos + count) extents
747 if (!cl_io_is_append(io)) {
748 const struct cl_io_rw_common *crw = &(io->u.ci_rw);
749 start = cl_offset(page->cp_obj, idx);
750 end = cl_offset(page->cp_obj, idx + 1);
751 result = crw->crw_pos < end &&
752 start < crw->crw_pos + crw->crw_count;
756 result = io->u.ci_fault.ft_index == idx;
765 * Called by read io, when page has to be read from the server.
767 * \see cl_io_operations::cio_read_page()
769 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
770 struct cl_page *page)
772 const struct cl_io_slice *scan;
773 struct cl_2queue *queue;
776 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
777 LINVRNT(cl_page_is_owned(page, io));
778 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
779 LINVRNT(cl_page_in_io(page, io));
780 LINVRNT(cl_io_invariant(io));
783 queue = &io->ci_queue;
785 cl_2queue_init(queue);
787 * ->cio_read_page() methods called in the loop below are supposed to
788 * never block waiting for network (the only subtle point is the
789 * creation of new pages for read-ahead that might result in cache
790 * shrinking, but currently only clean pages are shrunk and this
791 * requires no network io).
793 * Should this ever starts blocking, retry loop would be needed for
794 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
796 cl_io_for_each(scan, io) {
797 if (scan->cis_iop->cio_read_page != NULL) {
798 const struct cl_page_slice *slice;
800 slice = cl_io_slice_page(scan, page);
801 LINVRNT(slice != NULL);
802 result = scan->cis_iop->cio_read_page(env, scan, slice);
808 result = cl_io_submit_rw(env, io, CRT_READ, queue);
810 * Unlock unsent pages in case of error.
812 cl_page_list_disown(env, io, &queue->c2_qin);
813 cl_2queue_fini(env, queue);
816 EXPORT_SYMBOL(cl_io_read_page);
819 * Called by write io to prepare page to receive data from user buffer.
821 * \see cl_io_operations::cio_prepare_write()
823 int cl_io_prepare_write(const struct lu_env *env, struct cl_io *io,
824 struct cl_page *page, unsigned from, unsigned to)
826 const struct cl_io_slice *scan;
829 LINVRNT(io->ci_type == CIT_WRITE);
830 LINVRNT(cl_page_is_owned(page, io));
831 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
832 LINVRNT(cl_io_invariant(io));
833 LASSERT(cl_page_in_io(page, io));
836 cl_io_for_each_reverse(scan, io) {
837 if (scan->cis_iop->cio_prepare_write != NULL) {
838 const struct cl_page_slice *slice;
840 slice = cl_io_slice_page(scan, page);
841 result = scan->cis_iop->cio_prepare_write(env, scan,
850 EXPORT_SYMBOL(cl_io_prepare_write);
853 * Called by write io after user data were copied into a page.
855 * \see cl_io_operations::cio_commit_write()
857 int cl_io_commit_write(const struct lu_env *env, struct cl_io *io,
858 struct cl_page *page, unsigned from, unsigned to)
860 const struct cl_io_slice *scan;
863 LINVRNT(io->ci_type == CIT_WRITE);
864 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
865 LINVRNT(cl_io_invariant(io));
867 * XXX Uh... not nice. Top level cl_io_commit_write() call (vvp->lov)
868 * already called cl_page_cache_add(), moving page into CPS_CACHED
869 * state. Better (and more general) way of dealing with such situation
872 LASSERT(cl_page_is_owned(page, io) || page->cp_parent != NULL);
873 LASSERT(cl_page_in_io(page, io));
876 cl_io_for_each(scan, io) {
877 if (scan->cis_iop->cio_commit_write != NULL) {
878 const struct cl_page_slice *slice;
880 slice = cl_io_slice_page(scan, page);
881 result = scan->cis_iop->cio_commit_write(env, scan,
888 LINVRNT(result <= 0);
891 EXPORT_SYMBOL(cl_io_commit_write);
894 * Submits a list of pages for immediate io.
896 * After the function gets returned, The submitted pages are moved to
897 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
898 * to be submitted, and the pages are errant to submit.
900 * \returns 0 if at least one page was submitted, error code otherwise.
901 * \see cl_io_operations::cio_submit()
903 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
904 enum cl_req_type crt, struct cl_2queue *queue)
906 const struct cl_io_slice *scan;
909 LINVRNT(crt < ARRAY_SIZE(scan->cis_iop->req_op));
912 cl_io_for_each(scan, io) {
913 if (scan->cis_iop->req_op[crt].cio_submit == NULL)
915 result = scan->cis_iop->req_op[crt].cio_submit(env, scan, crt,
921 * If ->cio_submit() failed, no pages were sent.
923 LASSERT(ergo(result != 0, cfs_list_empty(&queue->c2_qout.pl_pages)));
926 EXPORT_SYMBOL(cl_io_submit_rw);
929 * Submit a sync_io and wait for the IO to be finished, or error happens.
930 * If \a timeout is zero, it means to wait for the IO unconditionally.
932 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
933 enum cl_req_type iot, struct cl_2queue *queue,
936 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
940 cl_page_list_for_each(pg, &queue->c2_qin) {
941 LASSERT(pg->cp_sync_io == NULL);
942 pg->cp_sync_io = anchor;
945 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
946 rc = cl_io_submit_rw(env, io, iot, queue);
949 * If some pages weren't sent for any reason (e.g.,
950 * read found up-to-date pages in the cache, or write found
951 * clean pages), count them as completed to avoid infinite
954 cl_page_list_for_each(pg, &queue->c2_qin) {
955 pg->cp_sync_io = NULL;
956 cl_sync_io_note(anchor, +1);
959 /* wait for the IO to be finished. */
960 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
963 LASSERT(cfs_list_empty(&queue->c2_qout.pl_pages));
964 cl_page_list_for_each(pg, &queue->c2_qin)
965 pg->cp_sync_io = NULL;
969 EXPORT_SYMBOL(cl_io_submit_sync);
972 * Cancel an IO which has been submitted by cl_io_submit_rw.
974 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
975 struct cl_page_list *queue)
977 struct cl_page *page;
980 CERROR("Canceling ongoing page trasmission\n");
981 cl_page_list_for_each(page, queue) {
984 LINVRNT(cl_page_in_io(page, io));
985 rc = cl_page_cancel(env, page);
986 result = result ?: rc;
990 EXPORT_SYMBOL(cl_io_cancel);
995 * Pumps io through iterations calling
997 * - cl_io_iter_init()
1007 * - cl_io_iter_fini()
1009 * repeatedly until there is no more io to do.
1011 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
1015 LINVRNT(cl_io_is_loopable(io));
1021 io->ci_continue = 0;
1022 result = cl_io_iter_init(env, io);
1025 result = cl_io_lock(env, io);
1028 * Notify layers that locks has been taken,
1029 * and do actual i/o.
1031 * - llite: kms, short read;
1032 * - llite: generic_file_read();
1034 result = cl_io_start(env, io);
1036 * Send any remaining pending
1039 * - llite: ll_rw_stats_tally.
1042 cl_io_unlock(env, io);
1043 cl_io_rw_advance(env, io, io->ci_nob - nob);
1046 cl_io_iter_fini(env, io);
1047 } while (result == 0 && io->ci_continue);
1049 result = io->ci_result;
1050 RETURN(result < 0 ? result : 0);
1052 EXPORT_SYMBOL(cl_io_loop);
1055 * Adds io slice to the cl_io.
1057 * This is called by cl_object_operations::coo_io_init() methods to add a
1058 * per-layer state to the io. New state is added at the end of
1059 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
1061 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
1063 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
1064 struct cl_object *obj,
1065 const struct cl_io_operations *ops)
1067 cfs_list_t *linkage = &slice->cis_linkage;
1069 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
1070 cfs_list_empty(linkage));
1073 cfs_list_add_tail(linkage, &io->ci_layers);
1075 slice->cis_obj = obj;
1076 slice->cis_iop = ops;
1079 EXPORT_SYMBOL(cl_io_slice_add);
1083 * Initializes page list.
1085 void cl_page_list_init(struct cl_page_list *plist)
1089 CFS_INIT_LIST_HEAD(&plist->pl_pages);
1090 plist->pl_owner = cfs_current();
1093 EXPORT_SYMBOL(cl_page_list_init);
1096 * Adds a page to a page list.
1098 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1101 /* it would be better to check that page is owned by "current" io, but
1102 * it is not passed here. */
1103 LASSERT(page->cp_owner != NULL);
1104 LINVRNT(plist->pl_owner == cfs_current());
1107 mutex_lock(&page->cp_mutex);
1109 LASSERT(cfs_list_empty(&page->cp_batch));
1110 cfs_list_add_tail(&page->cp_batch, &plist->pl_pages);
1112 page->cp_queue_ref = lu_ref_add(&page->cp_reference, "queue", plist);
1116 EXPORT_SYMBOL(cl_page_list_add);
1119 * Removes a page from a page list.
1121 void cl_page_list_del(const struct lu_env *env,
1122 struct cl_page_list *plist, struct cl_page *page)
1124 LASSERT(plist->pl_nr > 0);
1125 LINVRNT(plist->pl_owner == cfs_current());
1128 cfs_list_del_init(&page->cp_batch);
1130 mutex_unlock(&page->cp_mutex);
1133 lu_ref_del_at(&page->cp_reference, page->cp_queue_ref, "queue", plist);
1134 cl_page_put(env, page);
1137 EXPORT_SYMBOL(cl_page_list_del);
1140 * Moves a page from one page list to another.
1142 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1143 struct cl_page *page)
1145 LASSERT(src->pl_nr > 0);
1146 LINVRNT(dst->pl_owner == cfs_current());
1147 LINVRNT(src->pl_owner == cfs_current());
1150 cfs_list_move_tail(&page->cp_batch, &dst->pl_pages);
1153 lu_ref_set_at(&page->cp_reference,
1154 page->cp_queue_ref, "queue", src, dst);
1157 EXPORT_SYMBOL(cl_page_list_move);
1160 * splice the cl_page_list, just as list head does
1162 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1164 struct cl_page *page;
1165 struct cl_page *tmp;
1167 LINVRNT(list->pl_owner == cfs_current());
1168 LINVRNT(head->pl_owner == cfs_current());
1171 cl_page_list_for_each_safe(page, tmp, list)
1172 cl_page_list_move(head, list, page);
1175 EXPORT_SYMBOL(cl_page_list_splice);
1177 void cl_page_disown0(const struct lu_env *env,
1178 struct cl_io *io, struct cl_page *pg);
1181 * Disowns pages in a queue.
1183 void cl_page_list_disown(const struct lu_env *env,
1184 struct cl_io *io, struct cl_page_list *plist)
1186 struct cl_page *page;
1187 struct cl_page *temp;
1189 LINVRNT(plist->pl_owner == cfs_current());
1192 cl_page_list_for_each_safe(page, temp, plist) {
1193 LASSERT(plist->pl_nr > 0);
1195 cfs_list_del_init(&page->cp_batch);
1197 mutex_unlock(&page->cp_mutex);
1201 * cl_page_disown0 rather than usual cl_page_disown() is used,
1202 * because pages are possibly in CPS_FREEING state already due
1203 * to the call to cl_page_list_discard().
1206 * XXX cl_page_disown0() will fail if page is not locked.
1208 cl_page_disown0(env, io, page);
1209 lu_ref_del(&page->cp_reference, "queue", plist);
1210 cl_page_put(env, page);
1214 EXPORT_SYMBOL(cl_page_list_disown);
1217 * Releases pages from queue.
1219 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1221 struct cl_page *page;
1222 struct cl_page *temp;
1224 LINVRNT(plist->pl_owner == cfs_current());
1227 cl_page_list_for_each_safe(page, temp, plist)
1228 cl_page_list_del(env, plist, page);
1229 LASSERT(plist->pl_nr == 0);
1232 EXPORT_SYMBOL(cl_page_list_fini);
1235 * Owns all pages in a queue.
1237 int cl_page_list_own(const struct lu_env *env,
1238 struct cl_io *io, struct cl_page_list *plist)
1240 struct cl_page *page;
1241 struct cl_page *temp;
1245 LINVRNT(plist->pl_owner == cfs_current());
1249 cl_page_list_for_each_safe(page, temp, plist) {
1250 LASSERT(index <= page->cp_index);
1251 index = page->cp_index;
1252 if (cl_page_own(env, io, page) == 0)
1253 result = result ?: page->cp_error;
1255 cl_page_list_del(env, plist, page);
1259 EXPORT_SYMBOL(cl_page_list_own);
1262 * Assumes all pages in a queue.
1264 void cl_page_list_assume(const struct lu_env *env,
1265 struct cl_io *io, struct cl_page_list *plist)
1267 struct cl_page *page;
1269 LINVRNT(plist->pl_owner == cfs_current());
1271 cl_page_list_for_each(page, plist)
1272 cl_page_assume(env, io, page);
1274 EXPORT_SYMBOL(cl_page_list_assume);
1277 * Discards all pages in a queue.
1279 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1280 struct cl_page_list *plist)
1282 struct cl_page *page;
1284 LINVRNT(plist->pl_owner == cfs_current());
1286 cl_page_list_for_each(page, plist)
1287 cl_page_discard(env, io, page);
1290 EXPORT_SYMBOL(cl_page_list_discard);
1293 * Unmaps all pages in a queue from user virtual memory.
1295 int cl_page_list_unmap(const struct lu_env *env, struct cl_io *io,
1296 struct cl_page_list *plist)
1298 struct cl_page *page;
1301 LINVRNT(plist->pl_owner == cfs_current());
1304 cl_page_list_for_each(page, plist) {
1305 result = cl_page_unmap(env, io, page);
1311 EXPORT_SYMBOL(cl_page_list_unmap);
1314 * Initialize dual page queue.
1316 void cl_2queue_init(struct cl_2queue *queue)
1319 cl_page_list_init(&queue->c2_qin);
1320 cl_page_list_init(&queue->c2_qout);
1323 EXPORT_SYMBOL(cl_2queue_init);
1326 * Add a page to the incoming page list of 2-queue.
1328 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1331 cl_page_list_add(&queue->c2_qin, page);
1334 EXPORT_SYMBOL(cl_2queue_add);
1337 * Disown pages in both lists of a 2-queue.
1339 void cl_2queue_disown(const struct lu_env *env,
1340 struct cl_io *io, struct cl_2queue *queue)
1343 cl_page_list_disown(env, io, &queue->c2_qin);
1344 cl_page_list_disown(env, io, &queue->c2_qout);
1347 EXPORT_SYMBOL(cl_2queue_disown);
1350 * Discard (truncate) pages in both lists of a 2-queue.
1352 void cl_2queue_discard(const struct lu_env *env,
1353 struct cl_io *io, struct cl_2queue *queue)
1356 cl_page_list_discard(env, io, &queue->c2_qin);
1357 cl_page_list_discard(env, io, &queue->c2_qout);
1360 EXPORT_SYMBOL(cl_2queue_discard);
1363 * Assume to own the pages in cl_2queue
1365 void cl_2queue_assume(const struct lu_env *env,
1366 struct cl_io *io, struct cl_2queue *queue)
1368 cl_page_list_assume(env, io, &queue->c2_qin);
1369 cl_page_list_assume(env, io, &queue->c2_qout);
1371 EXPORT_SYMBOL(cl_2queue_assume);
1374 * Finalize both page lists of a 2-queue.
1376 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1379 cl_page_list_fini(env, &queue->c2_qout);
1380 cl_page_list_fini(env, &queue->c2_qin);
1383 EXPORT_SYMBOL(cl_2queue_fini);
1386 * Initialize a 2-queue to contain \a page in its incoming page list.
1388 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1391 cl_2queue_init(queue);
1392 cl_2queue_add(queue, page);
1395 EXPORT_SYMBOL(cl_2queue_init_page);
1398 * Returns top-level io.
1400 * \see cl_object_top(), cl_page_top().
1402 struct cl_io *cl_io_top(struct cl_io *io)
1405 while (io->ci_parent != NULL)
1409 EXPORT_SYMBOL(cl_io_top);
1412 * Prints human readable representation of \a io to the \a f.
1414 void cl_io_print(const struct lu_env *env, void *cookie,
1415 lu_printer_t printer, const struct cl_io *io)
1420 * Adds request slice to the compound request.
1422 * This is called by cl_device_operations::cdo_req_init() methods to add a
1423 * per-layer state to the request. New state is added at the end of
1424 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1426 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1428 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1429 struct cl_device *dev,
1430 const struct cl_req_operations *ops)
1433 cfs_list_add_tail(&slice->crs_linkage, &req->crq_layers);
1434 slice->crs_dev = dev;
1435 slice->crs_ops = ops;
1436 slice->crs_req = req;
1439 EXPORT_SYMBOL(cl_req_slice_add);
1441 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1445 LASSERT(cfs_list_empty(&req->crq_pages));
1446 LASSERT(req->crq_nrpages == 0);
1447 LINVRNT(cfs_list_empty(&req->crq_layers));
1448 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1451 if (req->crq_o != NULL) {
1452 for (i = 0; i < req->crq_nrobjs; ++i) {
1453 struct cl_object *obj = req->crq_o[i].ro_obj;
1455 lu_object_ref_del_at(&obj->co_lu,
1456 req->crq_o[i].ro_obj_ref,
1458 cl_object_put(env, obj);
1461 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1467 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1468 struct cl_page *page)
1470 struct cl_device *dev;
1471 struct cl_page_slice *slice;
1476 page = cl_page_top(page);
1478 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1479 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1480 if (dev->cd_ops->cdo_req_init != NULL) {
1481 result = dev->cd_ops->cdo_req_init(env,
1487 page = page->cp_child;
1488 } while (page != NULL && result == 0);
1493 * Invokes per-request transfer completion call-backs
1494 * (cl_req_operations::cro_completion()) bottom-to-top.
1496 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1498 struct cl_req_slice *slice;
1502 * for the lack of list_for_each_entry_reverse_safe()...
1504 while (!cfs_list_empty(&req->crq_layers)) {
1505 slice = cfs_list_entry(req->crq_layers.prev,
1506 struct cl_req_slice, crs_linkage);
1507 cfs_list_del_init(&slice->crs_linkage);
1508 if (slice->crs_ops->cro_completion != NULL)
1509 slice->crs_ops->cro_completion(env, slice, rc);
1511 cl_req_free(env, req);
1514 EXPORT_SYMBOL(cl_req_completion);
1517 * Allocates new transfer request.
1519 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1520 enum cl_req_type crt, int nr_objects)
1524 LINVRNT(nr_objects > 0);
1531 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1532 if (req->crq_o != NULL) {
1533 req->crq_nrobjs = nr_objects;
1534 req->crq_type = crt;
1535 CFS_INIT_LIST_HEAD(&req->crq_pages);
1536 CFS_INIT_LIST_HEAD(&req->crq_layers);
1537 result = cl_req_init(env, req, page);
1541 cl_req_completion(env, req, result);
1542 req = ERR_PTR(result);
1545 req = ERR_PTR(-ENOMEM);
1548 EXPORT_SYMBOL(cl_req_alloc);
1551 * Adds a page to a request.
1553 void cl_req_page_add(const struct lu_env *env,
1554 struct cl_req *req, struct cl_page *page)
1556 struct cl_object *obj;
1557 struct cl_req_obj *rqo;
1561 page = cl_page_top(page);
1563 LASSERT(cfs_list_empty(&page->cp_flight));
1564 LASSERT(page->cp_req == NULL);
1566 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1567 req, req->crq_type, req->crq_nrpages);
1569 cfs_list_add_tail(&page->cp_flight, &req->crq_pages);
1572 obj = cl_object_top(page->cp_obj);
1573 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1574 if (rqo->ro_obj == NULL) {
1577 rqo->ro_obj_ref = lu_object_ref_add(&obj->co_lu,
1582 LASSERT(i < req->crq_nrobjs);
1585 EXPORT_SYMBOL(cl_req_page_add);
1588 * Removes a page from a request.
1590 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1592 struct cl_req *req = page->cp_req;
1595 page = cl_page_top(page);
1597 LASSERT(!cfs_list_empty(&page->cp_flight));
1598 LASSERT(req->crq_nrpages > 0);
1600 cfs_list_del_init(&page->cp_flight);
1602 page->cp_req = NULL;
1605 EXPORT_SYMBOL(cl_req_page_done);
1608 * Notifies layers that request is about to depart by calling
1609 * cl_req_operations::cro_prep() top-to-bottom.
1611 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1615 const struct cl_req_slice *slice;
1619 * Check that the caller of cl_req_alloc() didn't lie about the number
1622 for (i = 0; i < req->crq_nrobjs; ++i)
1623 LASSERT(req->crq_o[i].ro_obj != NULL);
1626 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1627 if (slice->crs_ops->cro_prep != NULL) {
1628 result = slice->crs_ops->cro_prep(env, slice);
1635 EXPORT_SYMBOL(cl_req_prep);
1638 * Fills in attributes that are passed to server together with transfer. Only
1639 * attributes from \a flags may be touched. This can be called multiple times
1640 * for the same request.
1642 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1643 struct cl_req_attr *attr, obd_valid flags)
1645 const struct cl_req_slice *slice;
1646 struct cl_page *page;
1649 LASSERT(!cfs_list_empty(&req->crq_pages));
1652 /* Take any page to use as a model. */
1653 page = cfs_list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1655 for (i = 0; i < req->crq_nrobjs; ++i) {
1656 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1657 const struct cl_page_slice *scan;
1658 const struct cl_object *obj;
1660 scan = cl_page_at(page,
1661 slice->crs_dev->cd_lu_dev.ld_type);
1662 LASSERT(scan != NULL);
1663 obj = scan->cpl_obj;
1664 if (slice->crs_ops->cro_attr_set != NULL)
1665 slice->crs_ops->cro_attr_set(env, slice, obj,
1671 EXPORT_SYMBOL(cl_req_attr_set);
1673 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1674 * implemented in libcfs. */
1676 # include <linux/sched.h>
1677 #else /* __KERNEL__ */
1678 # include <liblustre.h>
1682 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1684 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1687 cfs_waitq_init(&anchor->csi_waitq);
1688 cfs_atomic_set(&anchor->csi_sync_nr, nrpages);
1689 anchor->csi_sync_rc = 0;
1692 EXPORT_SYMBOL(cl_sync_io_init);
1695 * Wait until all transfer completes. Transfer completion routine has to call
1696 * cl_sync_io_note() for every page.
1698 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1699 struct cl_page_list *queue, struct cl_sync_io *anchor,
1702 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1707 LASSERT(timeout >= 0);
1709 rc = l_wait_event(anchor->csi_waitq,
1710 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1713 CERROR("SYNC IO failed with error: %d, try to cancel "
1714 "%d remaining pages\n",
1715 rc, cfs_atomic_read(&anchor->csi_sync_nr));
1717 (void)cl_io_cancel(env, io, queue);
1719 lwi = (struct l_wait_info) { 0 };
1720 (void)l_wait_event(anchor->csi_waitq,
1721 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1724 rc = anchor->csi_sync_rc;
1726 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) == 0);
1727 cl_page_list_assume(env, io, queue);
1728 POISON(anchor, 0x5a, sizeof *anchor);
1731 EXPORT_SYMBOL(cl_sync_io_wait);
1734 * Indicate that transfer of a single page completed.
1736 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1739 if (anchor->csi_sync_rc == 0 && ioret < 0)
1740 anchor->csi_sync_rc = ioret;
1742 * Synchronous IO done without releasing page lock (e.g., as a part of
1743 * ->{prepare,commit}_write(). Completion is used to signal the end of
1746 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) > 0);
1747 if (cfs_atomic_dec_and_test(&anchor->csi_sync_nr))
1748 cfs_waitq_broadcast(&anchor->csi_waitq);
1751 EXPORT_SYMBOL(cl_sync_io_note);