1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
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 # define EXPORT_SYMTAB
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <libcfs/list.h>
50 /* lu_time_global_{init,fini}() */
53 #include <cl_object.h>
54 #include "cl_internal.h"
56 /*****************************************************************************
62 #define cl_io_for_each(slice, io) \
63 list_for_each_entry((slice), &io->ci_layers, cis_linkage)
64 #define cl_io_for_each_reverse(slice, io) \
65 list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
67 static inline int cl_io_type_is_valid(enum cl_io_type type)
69 return CIT_READ <= type && type < CIT_OP_NR;
72 static inline int cl_io_is_loopable(const struct cl_io *io)
74 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
78 * Returns true iff there is an IO ongoing in the given environment.
80 int cl_io_is_going(const struct lu_env *env)
82 return cl_env_info(env)->clt_current_io != NULL;
84 EXPORT_SYMBOL(cl_io_is_going);
87 * cl_io invariant that holds at all times when exported cl_io_*() functions
88 * are entered and left.
90 static int cl_io_invariant(const struct cl_io *io)
97 * io can own pages only when it is ongoing. Sub-io might
98 * still be in CIS_LOCKED state when top-io is in
101 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
102 (io->ci_state == CIS_LOCKED && up != NULL));
106 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
108 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
110 struct cl_io_slice *slice;
111 struct cl_thread_info *info;
113 LINVRNT(cl_io_type_is_valid(io->ci_type));
114 LINVRNT(cl_io_invariant(io));
117 while (!list_empty(&io->ci_layers)) {
118 slice = container_of(io->ci_layers.next, struct cl_io_slice,
120 list_del_init(&slice->cis_linkage);
121 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
122 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
124 * Invalidate slice to catch use after free. This assumes that
125 * slices are allocated within session and can be touched
126 * after ->cio_fini() returns.
128 slice->cis_io = NULL;
130 io->ci_state = CIS_FINI;
131 info = cl_env_info(env);
132 if (info->clt_current_io == io)
133 info->clt_current_io = NULL;
136 EXPORT_SYMBOL(cl_io_fini);
138 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
139 enum cl_io_type iot, struct cl_object *obj)
141 struct cl_object *scan;
144 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
145 LINVRNT(cl_io_type_is_valid(iot));
146 LINVRNT(cl_io_invariant(io));
150 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
151 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
152 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_done);
153 CFS_INIT_LIST_HEAD(&io->ci_layers);
156 cl_object_for_each(scan, obj) {
157 if (scan->co_ops->coo_io_init != NULL) {
158 result = scan->co_ops->coo_io_init(env, scan, io);
164 io->ci_state = CIS_INIT;
169 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
171 * \pre obj != cl_object_top(obj)
173 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
174 enum cl_io_type iot, struct cl_object *obj)
176 struct cl_thread_info *info = cl_env_info(env);
178 LASSERT(obj != cl_object_top(obj));
179 if (info->clt_current_io == NULL)
180 info->clt_current_io = io;
181 return cl_io_init0(env, io, iot, obj);
183 EXPORT_SYMBOL(cl_io_sub_init);
186 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
188 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
189 * what the latter returned.
191 * \pre obj == cl_object_top(obj)
192 * \pre cl_io_type_is_valid(iot)
193 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
195 int cl_io_init(const struct lu_env *env, struct cl_io *io,
196 enum cl_io_type iot, struct cl_object *obj)
198 struct cl_thread_info *info = cl_env_info(env);
200 LASSERT(obj == cl_object_top(obj));
201 LASSERT(info->clt_current_io == NULL);
203 info->clt_current_io = io;
204 return cl_io_init0(env, io, iot, obj);
206 EXPORT_SYMBOL(cl_io_init);
209 * Initialize read or write io.
211 * \pre iot == CIT_READ || iot == CIT_WRITE
213 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
214 enum cl_io_type iot, loff_t pos, size_t count)
216 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
217 LINVRNT(io->ci_obj != NULL);
220 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
221 "io range: %u ["LPU64", "LPU64") %u %u\n",
222 iot, (__u64)pos, (__u64)pos + count,
223 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
224 io->u.ci_rw.crw_pos = pos;
225 io->u.ci_rw.crw_count = count;
226 RETURN(cl_io_init(env, io, iot, io->ci_obj));
228 EXPORT_SYMBOL(cl_io_rw_init);
230 static inline const struct lu_fid *
231 cl_lock_descr_fid(const struct cl_lock_descr *descr)
233 return lu_object_fid(&descr->cld_obj->co_lu);
236 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
237 const struct cl_lock_descr *d1)
239 return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
240 __diff_normalize(d0->cld_start, d1->cld_start);
244 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
246 static void cl_io_locks_sort(struct cl_io *io)
251 /* hidden treasure: bubble sort for now. */
253 struct cl_io_lock_link *curr;
254 struct cl_io_lock_link *prev;
255 struct cl_io_lock_link *temp;
260 list_for_each_entry_safe(curr, temp, &io->ci_lockset.cls_todo,
263 switch (cl_lock_descr_cmp(&prev->cill_descr,
264 &curr->cill_descr)) {
267 * IMPOSSIBLE: Identical locks are
274 list_move_tail(&curr->cill_linkage,
275 &prev->cill_linkage);
277 continue; /* don't change prev: it's
278 * still "previous" */
279 case -1: /* already in order */
290 * Check whether \a queue contains locks matching \a need.
292 * \retval +ve there is a matching lock in the \a queue
293 * \retval 0 there are no matching locks in the \a queue
295 int cl_queue_match(const struct list_head *queue,
296 const struct cl_lock_descr *need)
298 struct cl_io_lock_link *scan;
301 list_for_each_entry(scan, queue, cill_linkage) {
302 if (cl_lock_descr_match(&scan->cill_descr, need))
307 EXPORT_SYMBOL(cl_queue_match);
309 static int cl_lockset_match(const struct cl_lockset *set,
310 const struct cl_lock_descr *need, int all_queues)
312 return (all_queues ? cl_queue_match(&set->cls_todo, need) : 0) ||
313 cl_queue_match(&set->cls_curr, need) ||
314 cl_queue_match(&set->cls_done, need);
317 static int cl_lockset_lock_one(const struct lu_env *env,
318 struct cl_io *io, struct cl_lockset *set,
319 struct cl_io_lock_link *link)
321 struct cl_lock *lock;
326 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
328 link->cill_lock = lock;
329 list_move(&link->cill_linkage, &set->cls_curr);
330 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
331 result = cl_wait(env, lock);
333 list_move(&link->cill_linkage, &set->cls_done);
337 result = PTR_ERR(lock);
341 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
342 struct cl_io_lock_link *link)
344 struct cl_lock *lock = link->cill_lock;
347 list_del_init(&link->cill_linkage);
349 cl_lock_release(env, lock, "io", io);
350 link->cill_lock = NULL;
352 if (link->cill_fini != NULL)
353 link->cill_fini(env, link);
357 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
358 struct cl_lockset *set)
360 struct cl_io_lock_link *link;
361 struct cl_io_lock_link *temp;
362 struct cl_lock *lock;
367 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
368 if (!cl_lockset_match(set, &link->cill_descr, 0)) {
369 /* XXX some locking to guarantee that locks aren't
370 * expanded in between. */
371 result = cl_lockset_lock_one(env, io, set, link);
375 cl_lock_link_fini(env, io, link);
378 list_for_each_entry_safe(link, temp,
379 &set->cls_curr, cill_linkage) {
380 lock = link->cill_lock;
381 result = cl_wait(env, lock);
383 list_move(&link->cill_linkage, &set->cls_done);
392 * Takes locks necessary for the current iteration of io.
394 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
395 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
398 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
400 const struct cl_io_slice *scan;
403 LINVRNT(cl_io_is_loopable(io));
404 LINVRNT(io->ci_state == CIS_IT_STARTED);
405 LINVRNT(cl_io_invariant(io));
408 cl_io_for_each(scan, io) {
409 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
411 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
416 cl_io_locks_sort(io);
417 result = cl_lockset_lock(env, io, &io->ci_lockset);
420 cl_io_unlock(env, io);
422 io->ci_state = CIS_LOCKED;
425 EXPORT_SYMBOL(cl_io_lock);
428 * Release locks takes by io.
430 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
432 struct cl_lockset *set;
433 struct cl_io_lock_link *link;
434 struct cl_io_lock_link *temp;
435 const struct cl_io_slice *scan;
437 LASSERT(cl_io_is_loopable(io));
438 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
439 LINVRNT(cl_io_invariant(io));
442 set = &io->ci_lockset;
444 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
445 cl_lock_link_fini(env, io, link);
447 list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
448 cl_lock_link_fini(env, io, link);
450 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
451 cl_unuse(env, link->cill_lock);
452 cl_lock_link_fini(env, io, link);
454 cl_io_for_each_reverse(scan, io) {
455 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
456 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
458 io->ci_state = CIS_UNLOCKED;
459 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
462 EXPORT_SYMBOL(cl_io_unlock);
465 * Prepares next iteration of io.
467 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
468 * layers a chance to modify io parameters, e.g., so that lov can restrict io
469 * to a single stripe.
471 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
473 const struct cl_io_slice *scan;
476 LINVRNT(cl_io_is_loopable(io));
477 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
478 LINVRNT(cl_io_invariant(io));
482 cl_io_for_each(scan, io) {
483 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
485 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
491 io->ci_state = CIS_IT_STARTED;
494 EXPORT_SYMBOL(cl_io_iter_init);
497 * Finalizes io iteration.
499 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
501 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
503 const struct cl_io_slice *scan;
505 LINVRNT(cl_io_is_loopable(io));
506 LINVRNT(io->ci_state == CIS_UNLOCKED);
507 LINVRNT(cl_io_invariant(io));
510 cl_io_for_each_reverse(scan, io) {
511 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
512 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
514 io->ci_state = CIS_IT_ENDED;
517 EXPORT_SYMBOL(cl_io_iter_fini);
520 * Records that read or write io progressed \a nob bytes forward.
522 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
524 const struct cl_io_slice *scan;
526 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
528 LINVRNT(cl_io_is_loopable(io));
529 LINVRNT(cl_io_invariant(io));
533 io->u.ci_rw.crw_pos += nob;
534 io->u.ci_rw.crw_count -= nob;
536 /* layers have to be notified. */
537 cl_io_for_each_reverse(scan, io) {
538 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
539 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
544 EXPORT_SYMBOL(cl_io_rw_advance);
547 * Adds a lock to a lockset.
549 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
550 struct cl_io_lock_link *link)
555 if (cl_lockset_match(&io->ci_lockset, &link->cill_descr, 1))
558 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
563 EXPORT_SYMBOL(cl_io_lock_add);
565 static void cl_free_io_lock_link(const struct lu_env *env,
566 struct cl_io_lock_link *link)
572 * Allocates new lock link, and uses it to add a lock to a lockset.
574 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
575 struct cl_lock_descr *descr)
577 struct cl_io_lock_link *link;
583 link->cill_descr = *descr;
584 link->cill_fini = cl_free_io_lock_link;
585 result = cl_io_lock_add(env, io, link);
586 if (result) /* lock match */
587 link->cill_fini(env, link);
593 EXPORT_SYMBOL(cl_io_lock_alloc_add);
596 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
598 int cl_io_start(const struct lu_env *env, struct cl_io *io)
600 const struct cl_io_slice *scan;
603 LINVRNT(cl_io_is_loopable(io));
604 LINVRNT(io->ci_state == CIS_LOCKED);
605 LINVRNT(cl_io_invariant(io));
608 io->ci_state = CIS_IO_GOING;
609 cl_io_for_each(scan, io) {
610 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
612 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
620 EXPORT_SYMBOL(cl_io_start);
623 * Wait until current io iteration is finished by calling
624 * cl_io_operations::cio_end() bottom-to-top.
626 void cl_io_end(const struct lu_env *env, struct cl_io *io)
628 const struct cl_io_slice *scan;
630 LINVRNT(cl_io_is_loopable(io));
631 LINVRNT(io->ci_state == CIS_IO_GOING);
632 LINVRNT(cl_io_invariant(io));
635 cl_io_for_each_reverse(scan, io) {
636 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
637 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
638 /* TODO: error handling. */
640 io->ci_state = CIS_IO_FINISHED;
643 EXPORT_SYMBOL(cl_io_end);
645 static const struct cl_page_slice *
646 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
648 const struct cl_page_slice *slice;
650 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
651 LINVRNT(slice != NULL);
656 * True iff \a page is within \a io range.
658 static int cl_page_in_io(const struct cl_page *page, const struct cl_io *io)
665 idx = page->cp_index;
666 switch (io->ci_type) {
670 * check that [start, end) and [pos, pos + count) extents
673 if (!cl_io_is_append(io)) {
674 const struct cl_io_rw_common *crw = &(io->u.ci_rw);
675 start = cl_offset(page->cp_obj, idx);
676 end = cl_offset(page->cp_obj, idx + 1);
677 result = crw->crw_pos < end &&
678 start < crw->crw_pos + crw->crw_count;
682 result = io->u.ci_fault.ft_index == idx;
691 * Called by read io, when page has to be read from the server.
693 * \see cl_io_operations::cio_read_page()
695 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
696 struct cl_page *page)
698 const struct cl_io_slice *scan;
699 struct cl_2queue *queue;
702 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
703 LINVRNT(cl_page_is_owned(page, io));
704 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
705 LINVRNT(cl_page_in_io(page, io));
706 LINVRNT(cl_io_invariant(io));
709 queue = &io->ci_queue;
711 cl_2queue_init(queue);
713 * ->cio_read_page() methods called in the loop below are supposed to
714 * never block waiting for network (the only subtle point is the
715 * creation of new pages for read-ahead that might result in cache
716 * shrinking, but currently only clean pages are shrunk and this
717 * requires no network io).
719 * Should this ever starts blocking, retry loop would be needed for
720 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
722 cl_io_for_each(scan, io) {
723 if (scan->cis_iop->cio_read_page != NULL) {
724 const struct cl_page_slice *slice;
726 slice = cl_io_slice_page(scan, page);
727 LINVRNT(slice != NULL);
728 result = scan->cis_iop->cio_read_page(env, scan, slice);
734 result = cl_io_submit_rw(env, io, CRT_READ, queue, CRP_NORMAL);
736 * Unlock unsent pages in case of error.
738 cl_page_list_disown(env, io, &queue->c2_qin);
739 cl_2queue_fini(env, queue);
742 EXPORT_SYMBOL(cl_io_read_page);
745 * Called by write io to prepare page to receive data from user buffer.
747 * \see cl_io_operations::cio_prepare_write()
749 int cl_io_prepare_write(const struct lu_env *env, struct cl_io *io,
750 struct cl_page *page, unsigned from, unsigned to)
752 const struct cl_io_slice *scan;
755 LINVRNT(io->ci_type == CIT_WRITE);
756 LINVRNT(cl_page_is_owned(page, io));
757 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
758 LINVRNT(cl_io_invariant(io));
759 LASSERT(cl_page_in_io(page, io));
762 cl_io_for_each_reverse(scan, io) {
763 if (scan->cis_iop->cio_prepare_write != NULL) {
764 const struct cl_page_slice *slice;
766 slice = cl_io_slice_page(scan, page);
767 result = scan->cis_iop->cio_prepare_write(env, scan,
776 EXPORT_SYMBOL(cl_io_prepare_write);
779 * Called by write io after user data were copied into a page.
781 * \see cl_io_operations::cio_commit_write()
783 int cl_io_commit_write(const struct lu_env *env, struct cl_io *io,
784 struct cl_page *page, unsigned from, unsigned to)
786 const struct cl_io_slice *scan;
789 LINVRNT(io->ci_type == CIT_WRITE);
790 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
791 LINVRNT(cl_io_invariant(io));
793 * XXX Uh... not nice. Top level cl_io_commit_write() call (vvp->lov)
794 * already called cl_page_cache_add(), moving page into CPS_CACHED
795 * state. Better (and more general) way of dealing with such situation
798 LASSERT(cl_page_is_owned(page, io) || page->cp_parent != NULL);
799 LASSERT(cl_page_in_io(page, io));
802 cl_io_for_each(scan, io) {
803 if (scan->cis_iop->cio_commit_write != NULL) {
804 const struct cl_page_slice *slice;
806 slice = cl_io_slice_page(scan, page);
807 result = scan->cis_iop->cio_commit_write(env, scan,
814 LINVRNT(result <= 0);
817 EXPORT_SYMBOL(cl_io_commit_write);
820 * Submits a list of pages for immediate io.
822 * After the function gets returned, The submitted pages are moved to
823 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
824 * to be submitted, and the pages are errant to submit.
826 * \returns 0 if at least one page was submitted, error code otherwise.
827 * \see cl_io_operations::cio_submit()
829 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
830 enum cl_req_type crt, struct cl_2queue *queue,
831 enum cl_req_priority priority)
833 const struct cl_io_slice *scan;
836 LINVRNT(crt < ARRAY_SIZE(scan->cis_iop->req_op));
839 cl_io_for_each(scan, io) {
840 if (scan->cis_iop->req_op[crt].cio_submit == NULL)
842 result = scan->cis_iop->req_op[crt].cio_submit(env, scan, crt,
848 * If ->cio_submit() failed, no pages were sent.
850 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
853 EXPORT_SYMBOL(cl_io_submit_rw);
856 * Submit a sync_io and wait for the IO to be finished, or error happens.
857 * If \a timeout is zero, it means to wait for the IO unconditionally.
859 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
860 enum cl_req_type iot, struct cl_2queue *queue,
861 enum cl_req_priority prio, long timeout)
863 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
867 LASSERT(prio == CRP_NORMAL || prio == CRP_CANCEL);
869 cl_page_list_for_each(pg, &queue->c2_qin) {
870 LASSERT(pg->cp_sync_io == NULL);
871 pg->cp_sync_io = anchor;
874 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
875 rc = cl_io_submit_rw(env, io, iot, queue, prio);
878 * If some pages weren't sent for any reason (e.g.,
879 * read found up-to-date pages in the cache, or write found
880 * clean pages), count them as completed to avoid infinite
883 cl_page_list_for_each(pg, &queue->c2_qin) {
884 pg->cp_sync_io = NULL;
885 cl_sync_io_note(anchor, +1);
888 /* wait for the IO to be finished. */
889 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
892 LASSERT(list_empty(&queue->c2_qout.pl_pages));
893 cl_page_list_for_each(pg, &queue->c2_qin)
894 pg->cp_sync_io = NULL;
898 EXPORT_SYMBOL(cl_io_submit_sync);
901 * Cancel an IO which has been submitted by cl_io_submit_rw.
903 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
904 struct cl_page_list *queue)
906 struct cl_page *page;
909 CERROR("Canceling ongoing page trasmission\n");
910 cl_page_list_for_each(page, queue) {
913 LINVRNT(cl_page_in_io(page, io));
914 rc = cl_page_cancel(env, page);
915 result = result ?: rc;
919 EXPORT_SYMBOL(cl_io_cancel);
924 * Pumps io through iterations calling
926 * - cl_io_iter_init()
936 * - cl_io_iter_fini()
938 * repeatedly until there is no more io to do.
940 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
944 LINVRNT(cl_io_is_loopable(io));
951 result = cl_io_iter_init(env, io);
954 result = cl_io_lock(env, io);
957 * Notify layers that locks has been taken,
960 * - llite: kms, short read;
961 * - llite: generic_file_read();
963 result = cl_io_start(env, io);
965 * Send any remaining pending
968 * - llite: ll_rw_stats_tally.
971 cl_io_unlock(env, io);
972 cl_io_rw_advance(env, io, io->ci_nob - nob);
975 cl_io_iter_fini(env, io);
976 } while (result == 0 && io->ci_continue);
977 RETURN(result < 0 ? result : 0);
979 EXPORT_SYMBOL(cl_io_loop);
982 * Adds io slice to the cl_io.
984 * This is called by cl_object_operations::coo_io_init() methods to add a
985 * per-layer state to the io. New state is added at the end of
986 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
988 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
990 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
991 struct cl_object *obj,
992 const struct cl_io_operations *ops)
994 struct list_head *linkage = &slice->cis_linkage;
996 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
997 list_empty(linkage));
1000 list_add_tail(linkage, &io->ci_layers);
1002 slice->cis_obj = obj;
1003 slice->cis_iop = ops;
1006 EXPORT_SYMBOL(cl_io_slice_add);
1010 * Initializes page list.
1012 void cl_page_list_init(struct cl_page_list *plist)
1016 CFS_INIT_LIST_HEAD(&plist->pl_pages);
1017 plist->pl_owner = cfs_current();
1020 EXPORT_SYMBOL(cl_page_list_init);
1023 * Adds a page to a page list.
1025 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1028 /* it would be better to check that page is owned by "current" io, but
1029 * it is not passed here. */
1030 LASSERT(page->cp_owner != NULL);
1031 LINVRNT(plist->pl_owner == cfs_current());
1034 mutex_lock(&page->cp_mutex);
1036 LASSERT(list_empty(&page->cp_batch));
1037 list_add_tail(&page->cp_batch, &plist->pl_pages);
1039 page->cp_queue_ref = lu_ref_add(&page->cp_reference, "queue", plist);
1043 EXPORT_SYMBOL(cl_page_list_add);
1046 * Removes a page from a page list.
1048 void cl_page_list_del(const struct lu_env *env,
1049 struct cl_page_list *plist, struct cl_page *page)
1051 LASSERT(plist->pl_nr > 0);
1052 LINVRNT(plist->pl_owner == cfs_current());
1055 list_del_init(&page->cp_batch);
1057 mutex_unlock(&page->cp_mutex);
1060 lu_ref_del_at(&page->cp_reference, page->cp_queue_ref, "queue", plist);
1061 cl_page_put(env, page);
1064 EXPORT_SYMBOL(cl_page_list_del);
1067 * Moves a page from one page list to another.
1069 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1070 struct cl_page *page)
1072 LASSERT(src->pl_nr > 0);
1073 LINVRNT(dst->pl_owner == cfs_current());
1074 LINVRNT(src->pl_owner == cfs_current());
1077 list_move_tail(&page->cp_batch, &dst->pl_pages);
1080 lu_ref_set_at(&page->cp_reference,
1081 page->cp_queue_ref, "queue", src, dst);
1084 EXPORT_SYMBOL(cl_page_list_move);
1087 * splice the cl_page_list, just as list head does
1089 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1091 struct cl_page *page;
1092 struct cl_page *tmp;
1094 LINVRNT(list->pl_owner == cfs_current());
1095 LINVRNT(head->pl_owner == cfs_current());
1098 cl_page_list_for_each_safe(page, tmp, list)
1099 cl_page_list_move(head, list, page);
1102 EXPORT_SYMBOL(cl_page_list_splice);
1104 void cl_page_disown0(const struct lu_env *env,
1105 struct cl_io *io, struct cl_page *pg);
1108 * Disowns pages in a queue.
1110 void cl_page_list_disown(const struct lu_env *env,
1111 struct cl_io *io, struct cl_page_list *plist)
1113 struct cl_page *page;
1114 struct cl_page *temp;
1116 LINVRNT(plist->pl_owner == cfs_current());
1119 cl_page_list_for_each_safe(page, temp, plist) {
1120 LASSERT(plist->pl_nr > 0);
1122 list_del_init(&page->cp_batch);
1124 mutex_unlock(&page->cp_mutex);
1128 * cl_page_disown0 rather than usual cl_page_disown() is used,
1129 * because pages are possibly in CPS_FREEING state already due
1130 * to the call to cl_page_list_discard().
1133 * XXX cl_page_disown0() will fail if page is not locked.
1135 cl_page_disown0(env, io, page);
1136 lu_ref_del(&page->cp_reference, "queue", plist);
1137 cl_page_put(env, page);
1141 EXPORT_SYMBOL(cl_page_list_disown);
1144 * Releases pages from queue.
1146 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1148 struct cl_page *page;
1149 struct cl_page *temp;
1151 LINVRNT(plist->pl_owner == cfs_current());
1154 cl_page_list_for_each_safe(page, temp, plist)
1155 cl_page_list_del(env, plist, page);
1156 LASSERT(plist->pl_nr == 0);
1159 EXPORT_SYMBOL(cl_page_list_fini);
1162 * Owns all pages in a queue.
1164 int cl_page_list_own(const struct lu_env *env,
1165 struct cl_io *io, struct cl_page_list *plist)
1167 struct cl_page *page;
1168 struct cl_page *temp;
1172 LINVRNT(plist->pl_owner == cfs_current());
1176 cl_page_list_for_each_safe(page, temp, plist) {
1177 LASSERT(index <= page->cp_index);
1178 index = page->cp_index;
1179 if (cl_page_own(env, io, page) == 0)
1180 result = result ?: page->cp_error;
1182 cl_page_list_del(env, plist, page);
1186 EXPORT_SYMBOL(cl_page_list_own);
1189 * Assumes all pages in a queue.
1191 void cl_page_list_assume(const struct lu_env *env,
1192 struct cl_io *io, struct cl_page_list *plist)
1194 struct cl_page *page;
1196 LINVRNT(plist->pl_owner == cfs_current());
1198 cl_page_list_for_each(page, plist)
1199 cl_page_assume(env, io, page);
1201 EXPORT_SYMBOL(cl_page_list_assume);
1204 * Discards all pages in a queue.
1206 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1207 struct cl_page_list *plist)
1209 struct cl_page *page;
1211 LINVRNT(plist->pl_owner == cfs_current());
1213 cl_page_list_for_each(page, plist)
1214 cl_page_discard(env, io, page);
1217 EXPORT_SYMBOL(cl_page_list_discard);
1220 * Unmaps all pages in a queue from user virtual memory.
1222 int cl_page_list_unmap(const struct lu_env *env, struct cl_io *io,
1223 struct cl_page_list *plist)
1225 struct cl_page *page;
1228 LINVRNT(plist->pl_owner == cfs_current());
1231 cl_page_list_for_each(page, plist) {
1232 result = cl_page_unmap(env, io, page);
1238 EXPORT_SYMBOL(cl_page_list_unmap);
1241 * Initialize dual page queue.
1243 void cl_2queue_init(struct cl_2queue *queue)
1246 cl_page_list_init(&queue->c2_qin);
1247 cl_page_list_init(&queue->c2_qout);
1250 EXPORT_SYMBOL(cl_2queue_init);
1253 * Add a page to the incoming page list of 2-queue.
1255 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1258 cl_page_list_add(&queue->c2_qin, page);
1261 EXPORT_SYMBOL(cl_2queue_add);
1264 * Disown pages in both lists of a 2-queue.
1266 void cl_2queue_disown(const struct lu_env *env,
1267 struct cl_io *io, struct cl_2queue *queue)
1270 cl_page_list_disown(env, io, &queue->c2_qin);
1271 cl_page_list_disown(env, io, &queue->c2_qout);
1274 EXPORT_SYMBOL(cl_2queue_disown);
1277 * Discard (truncate) pages in both lists of a 2-queue.
1279 void cl_2queue_discard(const struct lu_env *env,
1280 struct cl_io *io, struct cl_2queue *queue)
1283 cl_page_list_discard(env, io, &queue->c2_qin);
1284 cl_page_list_discard(env, io, &queue->c2_qout);
1287 EXPORT_SYMBOL(cl_2queue_discard);
1290 * Assume to own the pages in cl_2queue
1292 void cl_2queue_assume(const struct lu_env *env,
1293 struct cl_io *io, struct cl_2queue *queue)
1295 cl_page_list_assume(env, io, &queue->c2_qin);
1296 cl_page_list_assume(env, io, &queue->c2_qout);
1298 EXPORT_SYMBOL(cl_2queue_assume);
1301 * Finalize both page lists of a 2-queue.
1303 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1306 cl_page_list_fini(env, &queue->c2_qout);
1307 cl_page_list_fini(env, &queue->c2_qin);
1310 EXPORT_SYMBOL(cl_2queue_fini);
1313 * Initialize a 2-queue to contain \a page in its incoming page list.
1315 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1318 cl_2queue_init(queue);
1319 cl_2queue_add(queue, page);
1322 EXPORT_SYMBOL(cl_2queue_init_page);
1325 * Returns top-level io.
1327 * \see cl_object_top(), cl_page_top().
1329 struct cl_io *cl_io_top(struct cl_io *io)
1332 while (io->ci_parent != NULL)
1336 EXPORT_SYMBOL(cl_io_top);
1339 * Prints human readable representation of \a io to the \a f.
1341 void cl_io_print(const struct lu_env *env, void *cookie,
1342 lu_printer_t printer, const struct cl_io *io)
1347 * Adds request slice to the compound request.
1349 * This is called by cl_device_operations::cdo_req_init() methods to add a
1350 * per-layer state to the request. New state is added at the end of
1351 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1353 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1355 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1356 struct cl_device *dev,
1357 const struct cl_req_operations *ops)
1360 list_add_tail(&slice->crs_linkage, &req->crq_layers);
1361 slice->crs_dev = dev;
1362 slice->crs_ops = ops;
1363 slice->crs_req = req;
1366 EXPORT_SYMBOL(cl_req_slice_add);
1368 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1372 LASSERT(list_empty(&req->crq_pages));
1373 LASSERT(req->crq_nrpages == 0);
1374 LINVRNT(list_empty(&req->crq_layers));
1375 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1378 if (req->crq_o != NULL) {
1379 for (i = 0; i < req->crq_nrobjs; ++i) {
1380 struct cl_object *obj = req->crq_o[i].ro_obj;
1382 lu_object_ref_del_at(&obj->co_lu,
1383 req->crq_o[i].ro_obj_ref,
1385 cl_object_put(env, obj);
1388 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1394 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1395 struct cl_page *page)
1397 struct cl_device *dev;
1398 struct cl_page_slice *slice;
1403 page = cl_page_top(page);
1405 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1406 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1407 if (dev->cd_ops->cdo_req_init != NULL) {
1408 result = dev->cd_ops->cdo_req_init(env,
1414 page = page->cp_child;
1415 } while (page != NULL && result == 0);
1420 * Invokes per-request transfer completion call-backs
1421 * (cl_req_operations::cro_completion()) bottom-to-top.
1423 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1425 struct cl_req_slice *slice;
1429 * for the lack of list_for_each_entry_reverse_safe()...
1431 while (!list_empty(&req->crq_layers)) {
1432 slice = list_entry(req->crq_layers.prev,
1433 struct cl_req_slice, crs_linkage);
1434 list_del_init(&slice->crs_linkage);
1435 if (slice->crs_ops->cro_completion != NULL)
1436 slice->crs_ops->cro_completion(env, slice, rc);
1438 cl_req_free(env, req);
1441 EXPORT_SYMBOL(cl_req_completion);
1444 * Allocates new transfer request.
1446 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1447 enum cl_req_type crt, int nr_objects)
1451 LINVRNT(nr_objects > 0);
1458 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1459 if (req->crq_o != NULL) {
1460 req->crq_nrobjs = nr_objects;
1461 req->crq_type = crt;
1462 CFS_INIT_LIST_HEAD(&req->crq_pages);
1463 CFS_INIT_LIST_HEAD(&req->crq_layers);
1464 result = cl_req_init(env, req, page);
1468 cl_req_completion(env, req, result);
1469 req = ERR_PTR(result);
1472 req = ERR_PTR(-ENOMEM);
1475 EXPORT_SYMBOL(cl_req_alloc);
1478 * Adds a page to a request.
1480 void cl_req_page_add(const struct lu_env *env,
1481 struct cl_req *req, struct cl_page *page)
1483 struct cl_object *obj;
1484 struct cl_req_obj *rqo;
1488 page = cl_page_top(page);
1490 LINVRNT(cl_page_is_vmlocked(env, page));
1491 LASSERT(list_empty(&page->cp_flight));
1492 LASSERT(page->cp_req == NULL);
1494 list_add_tail(&page->cp_flight, &req->crq_pages);
1497 obj = cl_object_top(page->cp_obj);
1498 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1499 if (rqo->ro_obj == NULL) {
1502 rqo->ro_obj_ref = lu_object_ref_add(&obj->co_lu,
1507 LASSERT(i < req->crq_nrobjs);
1510 EXPORT_SYMBOL(cl_req_page_add);
1513 * Removes a page from a request.
1515 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1517 struct cl_req *req = page->cp_req;
1520 page = cl_page_top(page);
1522 LINVRNT(cl_page_is_vmlocked(env, page));
1523 LASSERT(!list_empty(&page->cp_flight));
1524 LASSERT(req->crq_nrpages > 0);
1526 list_del_init(&page->cp_flight);
1528 page->cp_req = NULL;
1531 EXPORT_SYMBOL(cl_req_page_done);
1534 * Notifies layers that request is about to depart by calling
1535 * cl_req_operations::cro_prep() top-to-bottom.
1537 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1541 const struct cl_req_slice *slice;
1545 * Check that the caller of cl_req_alloc() didn't lie about the number
1548 for (i = 0; i < req->crq_nrobjs; ++i)
1549 LASSERT(req->crq_o[i].ro_obj != NULL);
1552 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1553 if (slice->crs_ops->cro_prep != NULL) {
1554 result = slice->crs_ops->cro_prep(env, slice);
1561 EXPORT_SYMBOL(cl_req_prep);
1564 * Fills in attributes that are passed to server together with transfer. Only
1565 * attributes from \a flags may be touched. This can be called multiple times
1566 * for the same request.
1568 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1569 struct cl_req_attr *attr, obd_valid flags)
1571 const struct cl_req_slice *slice;
1572 struct cl_page *page;
1575 LASSERT(!list_empty(&req->crq_pages));
1578 /* Take any page to use as a model. */
1579 page = list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1581 for (i = 0; i < req->crq_nrobjs; ++i) {
1582 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1583 const struct cl_page_slice *scan;
1584 const struct cl_object *obj;
1586 scan = cl_page_at(page,
1587 slice->crs_dev->cd_lu_dev.ld_type);
1588 LASSERT(scan != NULL);
1589 obj = scan->cpl_obj;
1590 if (slice->crs_ops->cro_attr_set != NULL)
1591 slice->crs_ops->cro_attr_set(env, slice, obj,
1597 EXPORT_SYMBOL(cl_req_attr_set);
1599 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1600 * implemented in libcfs. */
1602 # include <linux/sched.h>
1603 #else /* __KERNEL__ */
1604 # include <liblustre.h>
1608 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1610 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1613 cfs_waitq_init(&anchor->csi_waitq);
1614 atomic_set(&anchor->csi_sync_nr, nrpages);
1615 anchor->csi_sync_rc = 0;
1618 EXPORT_SYMBOL(cl_sync_io_init);
1621 * Wait until all transfer completes. Transfer completion routine has to call
1622 * cl_sync_io_note() for every page.
1624 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1625 struct cl_page_list *queue, struct cl_sync_io *anchor,
1628 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1633 LASSERT(timeout >= 0);
1635 rc = l_wait_event(anchor->csi_waitq,
1636 atomic_read(&anchor->csi_sync_nr) == 0,
1639 CERROR("SYNC IO failed with error: %d, try to cancel "
1640 "%d remaining pages\n",
1641 rc, atomic_read(&anchor->csi_sync_nr));
1643 (void)cl_io_cancel(env, io, queue);
1645 lwi = (struct l_wait_info) { 0 };
1646 (void)l_wait_event(anchor->csi_waitq,
1647 atomic_read(&anchor->csi_sync_nr) == 0,
1650 rc = anchor->csi_sync_rc;
1652 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1653 cl_page_list_assume(env, io, queue);
1654 POISON(anchor, 0x5a, sizeof *anchor);
1657 EXPORT_SYMBOL(cl_sync_io_wait);
1660 * Indicate that transfer of a single page completed.
1662 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1665 if (anchor->csi_sync_rc == 0 && ioret < 0)
1666 anchor->csi_sync_rc = ioret;
1668 * Synchronous IO done without releasing page lock (e.g., as a part of
1669 * ->{prepare,commit}_write(). Completion is used to signal the end of
1672 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1673 if (atomic_dec_and_test(&anchor->csi_sync_nr))
1674 cfs_waitq_broadcast(&anchor->csi_waitq);
1677 EXPORT_SYMBOL(cl_sync_io_note);