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 (c) 2008, 2010, Oracle and/or its affiliates. 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 cfs_list_for_each_entry((slice), &io->ci_layers, cis_linkage)
64 #define cl_io_for_each_reverse(slice, io) \
65 cfs_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 (!cfs_list_empty(&io->ci_layers)) {
118 slice = container_of(io->ci_layers.next, struct cl_io_slice,
120 cfs_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_sort(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);
243 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
244 const struct cl_lock_descr *d1)
248 ret = lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1));
251 if (d0->cld_end < d1->cld_start)
253 if (d0->cld_start > d0->cld_end)
258 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
259 const struct cl_lock_descr *d1)
261 d0->cld_start = min(d0->cld_start, d1->cld_start);
262 d0->cld_end = max(d0->cld_end, d1->cld_end);
264 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
265 d0->cld_mode = CLM_WRITE;
267 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
268 d0->cld_mode = CLM_GROUP;
272 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
274 static void cl_io_locks_sort(struct cl_io *io)
279 /* hidden treasure: bubble sort for now. */
281 struct cl_io_lock_link *curr;
282 struct cl_io_lock_link *prev;
283 struct cl_io_lock_link *temp;
288 cfs_list_for_each_entry_safe(curr, temp,
289 &io->ci_lockset.cls_todo,
292 switch (cl_lock_descr_sort(&prev->cill_descr,
293 &curr->cill_descr)) {
296 * IMPOSSIBLE: Identical locks are
303 cfs_list_move_tail(&curr->cill_linkage,
304 &prev->cill_linkage);
306 continue; /* don't change prev: it's
307 * still "previous" */
308 case -1: /* already in order */
319 * Check whether \a queue contains locks matching \a need.
321 * \retval +ve there is a matching lock in the \a queue
322 * \retval 0 there are no matching locks in the \a queue
324 int cl_queue_match(const cfs_list_t *queue,
325 const struct cl_lock_descr *need)
327 struct cl_io_lock_link *scan;
330 cfs_list_for_each_entry(scan, queue, cill_linkage) {
331 if (cl_lock_descr_match(&scan->cill_descr, need))
336 EXPORT_SYMBOL(cl_queue_match);
338 static int cl_queue_merge(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_cmp(&scan->cill_descr, need))
347 cl_lock_descr_merge(&scan->cill_descr, need);
348 CDEBUG(D_VFSTRACE, "lock: %i: [%lu, %lu]\n",
349 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
350 scan->cill_descr.cld_end);
357 static int cl_lockset_match(const struct cl_lockset *set,
358 const struct cl_lock_descr *need)
360 return cl_queue_match(&set->cls_curr, need) ||
361 cl_queue_match(&set->cls_done, need);
364 static int cl_lockset_merge(const struct cl_lockset *set,
365 const struct cl_lock_descr *need)
367 return cl_queue_merge(&set->cls_todo, need) ||
368 cl_lockset_match(set, need);
371 static int cl_lockset_lock_one(const struct lu_env *env,
372 struct cl_io *io, struct cl_lockset *set,
373 struct cl_io_lock_link *link)
375 struct cl_lock *lock;
380 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
382 link->cill_lock = lock;
383 cfs_list_move(&link->cill_linkage, &set->cls_curr);
384 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
385 result = cl_wait(env, lock);
387 cfs_list_move(&link->cill_linkage,
392 result = PTR_ERR(lock);
396 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
397 struct cl_io_lock_link *link)
399 struct cl_lock *lock = link->cill_lock;
402 cfs_list_del_init(&link->cill_linkage);
404 cl_lock_release(env, lock, "io", io);
405 link->cill_lock = NULL;
407 if (link->cill_fini != NULL)
408 link->cill_fini(env, link);
412 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
413 struct cl_lockset *set)
415 struct cl_io_lock_link *link;
416 struct cl_io_lock_link *temp;
417 struct cl_lock *lock;
422 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
423 if (!cl_lockset_match(set, &link->cill_descr)) {
424 /* XXX some locking to guarantee that locks aren't
425 * expanded in between. */
426 result = cl_lockset_lock_one(env, io, set, link);
430 cl_lock_link_fini(env, io, link);
433 cfs_list_for_each_entry_safe(link, temp,
434 &set->cls_curr, cill_linkage) {
435 lock = link->cill_lock;
436 result = cl_wait(env, lock);
438 cfs_list_move(&link->cill_linkage,
448 * Takes locks necessary for the current iteration of io.
450 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
451 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
454 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
456 const struct cl_io_slice *scan;
459 LINVRNT(cl_io_is_loopable(io));
460 LINVRNT(io->ci_state == CIS_IT_STARTED);
461 LINVRNT(cl_io_invariant(io));
464 cl_io_for_each(scan, io) {
465 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
467 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
472 cl_io_locks_sort(io);
473 result = cl_lockset_lock(env, io, &io->ci_lockset);
476 cl_io_unlock(env, io);
478 io->ci_state = CIS_LOCKED;
481 EXPORT_SYMBOL(cl_io_lock);
484 * Release locks takes by io.
486 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
488 struct cl_lockset *set;
489 struct cl_io_lock_link *link;
490 struct cl_io_lock_link *temp;
491 const struct cl_io_slice *scan;
493 LASSERT(cl_io_is_loopable(io));
494 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
495 LINVRNT(cl_io_invariant(io));
498 set = &io->ci_lockset;
500 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
501 cl_lock_link_fini(env, io, link);
503 cfs_list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
504 cl_lock_link_fini(env, io, link);
506 cfs_list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
507 cl_unuse(env, link->cill_lock);
508 cl_lock_link_fini(env, io, link);
510 cl_io_for_each_reverse(scan, io) {
511 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
512 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
514 io->ci_state = CIS_UNLOCKED;
515 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
518 EXPORT_SYMBOL(cl_io_unlock);
521 * Prepares next iteration of io.
523 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
524 * layers a chance to modify io parameters, e.g., so that lov can restrict io
525 * to a single stripe.
527 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
529 const struct cl_io_slice *scan;
532 LINVRNT(cl_io_is_loopable(io));
533 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
534 LINVRNT(cl_io_invariant(io));
538 cl_io_for_each(scan, io) {
539 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
541 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
547 io->ci_state = CIS_IT_STARTED;
550 EXPORT_SYMBOL(cl_io_iter_init);
553 * Finalizes io iteration.
555 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
557 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
559 const struct cl_io_slice *scan;
561 LINVRNT(cl_io_is_loopable(io));
562 LINVRNT(io->ci_state == CIS_UNLOCKED);
563 LINVRNT(cl_io_invariant(io));
566 cl_io_for_each_reverse(scan, io) {
567 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
568 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
570 io->ci_state = CIS_IT_ENDED;
573 EXPORT_SYMBOL(cl_io_iter_fini);
576 * Records that read or write io progressed \a nob bytes forward.
578 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
580 const struct cl_io_slice *scan;
582 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
584 LINVRNT(cl_io_is_loopable(io));
585 LINVRNT(cl_io_invariant(io));
589 io->u.ci_rw.crw_pos += nob;
590 io->u.ci_rw.crw_count -= nob;
592 /* layers have to be notified. */
593 cl_io_for_each_reverse(scan, io) {
594 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
595 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
600 EXPORT_SYMBOL(cl_io_rw_advance);
603 * Adds a lock to a lockset.
605 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
606 struct cl_io_lock_link *link)
611 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
614 cfs_list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
619 EXPORT_SYMBOL(cl_io_lock_add);
621 static void cl_free_io_lock_link(const struct lu_env *env,
622 struct cl_io_lock_link *link)
628 * Allocates new lock link, and uses it to add a lock to a lockset.
630 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
631 struct cl_lock_descr *descr)
633 struct cl_io_lock_link *link;
639 link->cill_descr = *descr;
640 link->cill_fini = cl_free_io_lock_link;
641 result = cl_io_lock_add(env, io, link);
642 if (result) /* lock match */
643 link->cill_fini(env, link);
649 EXPORT_SYMBOL(cl_io_lock_alloc_add);
652 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
654 int cl_io_start(const struct lu_env *env, struct cl_io *io)
656 const struct cl_io_slice *scan;
659 LINVRNT(cl_io_is_loopable(io));
660 LINVRNT(io->ci_state == CIS_LOCKED);
661 LINVRNT(cl_io_invariant(io));
664 io->ci_state = CIS_IO_GOING;
665 cl_io_for_each(scan, io) {
666 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
668 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
676 EXPORT_SYMBOL(cl_io_start);
679 * Wait until current io iteration is finished by calling
680 * cl_io_operations::cio_end() bottom-to-top.
682 void cl_io_end(const struct lu_env *env, struct cl_io *io)
684 const struct cl_io_slice *scan;
686 LINVRNT(cl_io_is_loopable(io));
687 LINVRNT(io->ci_state == CIS_IO_GOING);
688 LINVRNT(cl_io_invariant(io));
691 cl_io_for_each_reverse(scan, io) {
692 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
693 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
694 /* TODO: error handling. */
696 io->ci_state = CIS_IO_FINISHED;
699 EXPORT_SYMBOL(cl_io_end);
701 static const struct cl_page_slice *
702 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
704 const struct cl_page_slice *slice;
706 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
707 LINVRNT(slice != NULL);
712 * True iff \a page is within \a io range.
714 static int cl_page_in_io(const struct cl_page *page, const struct cl_io *io)
721 idx = page->cp_index;
722 switch (io->ci_type) {
726 * check that [start, end) and [pos, pos + count) extents
729 if (!cl_io_is_append(io)) {
730 const struct cl_io_rw_common *crw = &(io->u.ci_rw);
731 start = cl_offset(page->cp_obj, idx);
732 end = cl_offset(page->cp_obj, idx + 1);
733 result = crw->crw_pos < end &&
734 start < crw->crw_pos + crw->crw_count;
738 result = io->u.ci_fault.ft_index == idx;
747 * Called by read io, when page has to be read from the server.
749 * \see cl_io_operations::cio_read_page()
751 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
752 struct cl_page *page)
754 const struct cl_io_slice *scan;
755 struct cl_2queue *queue;
758 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
759 LINVRNT(cl_page_is_owned(page, io));
760 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
761 LINVRNT(cl_page_in_io(page, io));
762 LINVRNT(cl_io_invariant(io));
765 queue = &io->ci_queue;
767 cl_2queue_init(queue);
769 * ->cio_read_page() methods called in the loop below are supposed to
770 * never block waiting for network (the only subtle point is the
771 * creation of new pages for read-ahead that might result in cache
772 * shrinking, but currently only clean pages are shrunk and this
773 * requires no network io).
775 * Should this ever starts blocking, retry loop would be needed for
776 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
778 cl_io_for_each(scan, io) {
779 if (scan->cis_iop->cio_read_page != NULL) {
780 const struct cl_page_slice *slice;
782 slice = cl_io_slice_page(scan, page);
783 LINVRNT(slice != NULL);
784 result = scan->cis_iop->cio_read_page(env, scan, slice);
790 result = cl_io_submit_rw(env, io, CRT_READ, queue, CRP_NORMAL);
792 * Unlock unsent pages in case of error.
794 cl_page_list_disown(env, io, &queue->c2_qin);
795 cl_2queue_fini(env, queue);
798 EXPORT_SYMBOL(cl_io_read_page);
801 * Called by write io to prepare page to receive data from user buffer.
803 * \see cl_io_operations::cio_prepare_write()
805 int cl_io_prepare_write(const struct lu_env *env, struct cl_io *io,
806 struct cl_page *page, unsigned from, unsigned to)
808 const struct cl_io_slice *scan;
811 LINVRNT(io->ci_type == CIT_WRITE);
812 LINVRNT(cl_page_is_owned(page, io));
813 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
814 LINVRNT(cl_io_invariant(io));
815 LASSERT(cl_page_in_io(page, io));
818 cl_io_for_each_reverse(scan, io) {
819 if (scan->cis_iop->cio_prepare_write != NULL) {
820 const struct cl_page_slice *slice;
822 slice = cl_io_slice_page(scan, page);
823 result = scan->cis_iop->cio_prepare_write(env, scan,
832 EXPORT_SYMBOL(cl_io_prepare_write);
835 * Called by write io after user data were copied into a page.
837 * \see cl_io_operations::cio_commit_write()
839 int cl_io_commit_write(const struct lu_env *env, struct cl_io *io,
840 struct cl_page *page, unsigned from, unsigned to)
842 const struct cl_io_slice *scan;
845 LINVRNT(io->ci_type == CIT_WRITE);
846 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
847 LINVRNT(cl_io_invariant(io));
849 * XXX Uh... not nice. Top level cl_io_commit_write() call (vvp->lov)
850 * already called cl_page_cache_add(), moving page into CPS_CACHED
851 * state. Better (and more general) way of dealing with such situation
854 LASSERT(cl_page_is_owned(page, io) || page->cp_parent != NULL);
855 LASSERT(cl_page_in_io(page, io));
858 cl_io_for_each(scan, io) {
859 if (scan->cis_iop->cio_commit_write != NULL) {
860 const struct cl_page_slice *slice;
862 slice = cl_io_slice_page(scan, page);
863 result = scan->cis_iop->cio_commit_write(env, scan,
870 LINVRNT(result <= 0);
873 EXPORT_SYMBOL(cl_io_commit_write);
876 * Submits a list of pages for immediate io.
878 * After the function gets returned, The submitted pages are moved to
879 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
880 * to be submitted, and the pages are errant to submit.
882 * \returns 0 if at least one page was submitted, error code otherwise.
883 * \see cl_io_operations::cio_submit()
885 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
886 enum cl_req_type crt, struct cl_2queue *queue,
887 enum cl_req_priority priority)
889 const struct cl_io_slice *scan;
892 LINVRNT(crt < ARRAY_SIZE(scan->cis_iop->req_op));
895 cl_io_for_each(scan, io) {
896 if (scan->cis_iop->req_op[crt].cio_submit == NULL)
898 result = scan->cis_iop->req_op[crt].cio_submit(env, scan, crt,
904 * If ->cio_submit() failed, no pages were sent.
906 LASSERT(ergo(result != 0, cfs_list_empty(&queue->c2_qout.pl_pages)));
909 EXPORT_SYMBOL(cl_io_submit_rw);
912 * Submit a sync_io and wait for the IO to be finished, or error happens.
913 * If \a timeout is zero, it means to wait for the IO unconditionally.
915 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
916 enum cl_req_type iot, struct cl_2queue *queue,
917 enum cl_req_priority prio, long timeout)
919 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
923 LASSERT(prio == CRP_NORMAL || prio == CRP_CANCEL);
925 cl_page_list_for_each(pg, &queue->c2_qin) {
926 LASSERT(pg->cp_sync_io == NULL);
927 pg->cp_sync_io = anchor;
930 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
931 rc = cl_io_submit_rw(env, io, iot, queue, prio);
934 * If some pages weren't sent for any reason (e.g.,
935 * read found up-to-date pages in the cache, or write found
936 * clean pages), count them as completed to avoid infinite
939 cl_page_list_for_each(pg, &queue->c2_qin) {
940 pg->cp_sync_io = NULL;
941 cl_sync_io_note(anchor, +1);
944 /* wait for the IO to be finished. */
945 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
948 LASSERT(cfs_list_empty(&queue->c2_qout.pl_pages));
949 cl_page_list_for_each(pg, &queue->c2_qin)
950 pg->cp_sync_io = NULL;
954 EXPORT_SYMBOL(cl_io_submit_sync);
957 * Cancel an IO which has been submitted by cl_io_submit_rw.
959 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
960 struct cl_page_list *queue)
962 struct cl_page *page;
965 CERROR("Canceling ongoing page trasmission\n");
966 cl_page_list_for_each(page, queue) {
969 LINVRNT(cl_page_in_io(page, io));
970 rc = cl_page_cancel(env, page);
971 result = result ?: rc;
975 EXPORT_SYMBOL(cl_io_cancel);
980 * Pumps io through iterations calling
982 * - cl_io_iter_init()
992 * - cl_io_iter_fini()
994 * repeatedly until there is no more io to do.
996 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
1000 LINVRNT(cl_io_is_loopable(io));
1006 io->ci_continue = 0;
1007 result = cl_io_iter_init(env, io);
1010 result = cl_io_lock(env, io);
1013 * Notify layers that locks has been taken,
1014 * and do actual i/o.
1016 * - llite: kms, short read;
1017 * - llite: generic_file_read();
1019 result = cl_io_start(env, io);
1021 * Send any remaining pending
1024 * - llite: ll_rw_stats_tally.
1027 cl_io_unlock(env, io);
1028 cl_io_rw_advance(env, io, io->ci_nob - nob);
1031 cl_io_iter_fini(env, io);
1032 } while (result == 0 && io->ci_continue);
1033 RETURN(result < 0 ? result : 0);
1035 EXPORT_SYMBOL(cl_io_loop);
1038 * Adds io slice to the cl_io.
1040 * This is called by cl_object_operations::coo_io_init() methods to add a
1041 * per-layer state to the io. New state is added at the end of
1042 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
1044 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
1046 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
1047 struct cl_object *obj,
1048 const struct cl_io_operations *ops)
1050 cfs_list_t *linkage = &slice->cis_linkage;
1052 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
1053 cfs_list_empty(linkage));
1056 cfs_list_add_tail(linkage, &io->ci_layers);
1058 slice->cis_obj = obj;
1059 slice->cis_iop = ops;
1062 EXPORT_SYMBOL(cl_io_slice_add);
1066 * Initializes page list.
1068 void cl_page_list_init(struct cl_page_list *plist)
1072 CFS_INIT_LIST_HEAD(&plist->pl_pages);
1073 plist->pl_owner = cfs_current();
1076 EXPORT_SYMBOL(cl_page_list_init);
1079 * Adds a page to a page list.
1081 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1084 /* it would be better to check that page is owned by "current" io, but
1085 * it is not passed here. */
1086 LASSERT(page->cp_owner != NULL);
1087 LINVRNT(plist->pl_owner == cfs_current());
1090 cfs_mutex_lock(&page->cp_mutex);
1092 LASSERT(cfs_list_empty(&page->cp_batch));
1093 cfs_list_add_tail(&page->cp_batch, &plist->pl_pages);
1095 page->cp_queue_ref = lu_ref_add(&page->cp_reference, "queue", plist);
1099 EXPORT_SYMBOL(cl_page_list_add);
1102 * Removes a page from a page list.
1104 void cl_page_list_del(const struct lu_env *env,
1105 struct cl_page_list *plist, struct cl_page *page)
1107 LASSERT(plist->pl_nr > 0);
1108 LINVRNT(plist->pl_owner == cfs_current());
1111 cfs_list_del_init(&page->cp_batch);
1113 cfs_mutex_unlock(&page->cp_mutex);
1116 lu_ref_del_at(&page->cp_reference, page->cp_queue_ref, "queue", plist);
1117 cl_page_put(env, page);
1120 EXPORT_SYMBOL(cl_page_list_del);
1123 * Moves a page from one page list to another.
1125 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1126 struct cl_page *page)
1128 LASSERT(src->pl_nr > 0);
1129 LINVRNT(dst->pl_owner == cfs_current());
1130 LINVRNT(src->pl_owner == cfs_current());
1133 cfs_list_move_tail(&page->cp_batch, &dst->pl_pages);
1136 lu_ref_set_at(&page->cp_reference,
1137 page->cp_queue_ref, "queue", src, dst);
1140 EXPORT_SYMBOL(cl_page_list_move);
1143 * splice the cl_page_list, just as list head does
1145 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1147 struct cl_page *page;
1148 struct cl_page *tmp;
1150 LINVRNT(list->pl_owner == cfs_current());
1151 LINVRNT(head->pl_owner == cfs_current());
1154 cl_page_list_for_each_safe(page, tmp, list)
1155 cl_page_list_move(head, list, page);
1158 EXPORT_SYMBOL(cl_page_list_splice);
1160 void cl_page_disown0(const struct lu_env *env,
1161 struct cl_io *io, struct cl_page *pg);
1164 * Disowns pages in a queue.
1166 void cl_page_list_disown(const struct lu_env *env,
1167 struct cl_io *io, struct cl_page_list *plist)
1169 struct cl_page *page;
1170 struct cl_page *temp;
1172 LINVRNT(plist->pl_owner == cfs_current());
1175 cl_page_list_for_each_safe(page, temp, plist) {
1176 LASSERT(plist->pl_nr > 0);
1178 cfs_list_del_init(&page->cp_batch);
1180 cfs_mutex_unlock(&page->cp_mutex);
1184 * cl_page_disown0 rather than usual cl_page_disown() is used,
1185 * because pages are possibly in CPS_FREEING state already due
1186 * to the call to cl_page_list_discard().
1189 * XXX cl_page_disown0() will fail if page is not locked.
1191 cl_page_disown0(env, io, page);
1192 lu_ref_del(&page->cp_reference, "queue", plist);
1193 cl_page_put(env, page);
1197 EXPORT_SYMBOL(cl_page_list_disown);
1200 * Releases pages from queue.
1202 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1204 struct cl_page *page;
1205 struct cl_page *temp;
1207 LINVRNT(plist->pl_owner == cfs_current());
1210 cl_page_list_for_each_safe(page, temp, plist)
1211 cl_page_list_del(env, plist, page);
1212 LASSERT(plist->pl_nr == 0);
1215 EXPORT_SYMBOL(cl_page_list_fini);
1218 * Owns all pages in a queue.
1220 int cl_page_list_own(const struct lu_env *env,
1221 struct cl_io *io, struct cl_page_list *plist)
1223 struct cl_page *page;
1224 struct cl_page *temp;
1228 LINVRNT(plist->pl_owner == cfs_current());
1232 cl_page_list_for_each_safe(page, temp, plist) {
1233 LASSERT(index <= page->cp_index);
1234 index = page->cp_index;
1235 if (cl_page_own(env, io, page) == 0)
1236 result = result ?: page->cp_error;
1238 cl_page_list_del(env, plist, page);
1242 EXPORT_SYMBOL(cl_page_list_own);
1245 * Assumes all pages in a queue.
1247 void cl_page_list_assume(const struct lu_env *env,
1248 struct cl_io *io, struct cl_page_list *plist)
1250 struct cl_page *page;
1252 LINVRNT(plist->pl_owner == cfs_current());
1254 cl_page_list_for_each(page, plist)
1255 cl_page_assume(env, io, page);
1257 EXPORT_SYMBOL(cl_page_list_assume);
1260 * Discards all pages in a queue.
1262 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1263 struct cl_page_list *plist)
1265 struct cl_page *page;
1267 LINVRNT(plist->pl_owner == cfs_current());
1269 cl_page_list_for_each(page, plist)
1270 cl_page_discard(env, io, page);
1273 EXPORT_SYMBOL(cl_page_list_discard);
1276 * Unmaps all pages in a queue from user virtual memory.
1278 int cl_page_list_unmap(const struct lu_env *env, struct cl_io *io,
1279 struct cl_page_list *plist)
1281 struct cl_page *page;
1284 LINVRNT(plist->pl_owner == cfs_current());
1287 cl_page_list_for_each(page, plist) {
1288 result = cl_page_unmap(env, io, page);
1294 EXPORT_SYMBOL(cl_page_list_unmap);
1297 * Initialize dual page queue.
1299 void cl_2queue_init(struct cl_2queue *queue)
1302 cl_page_list_init(&queue->c2_qin);
1303 cl_page_list_init(&queue->c2_qout);
1306 EXPORT_SYMBOL(cl_2queue_init);
1309 * Add a page to the incoming page list of 2-queue.
1311 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1314 cl_page_list_add(&queue->c2_qin, page);
1317 EXPORT_SYMBOL(cl_2queue_add);
1320 * Disown pages in both lists of a 2-queue.
1322 void cl_2queue_disown(const struct lu_env *env,
1323 struct cl_io *io, struct cl_2queue *queue)
1326 cl_page_list_disown(env, io, &queue->c2_qin);
1327 cl_page_list_disown(env, io, &queue->c2_qout);
1330 EXPORT_SYMBOL(cl_2queue_disown);
1333 * Discard (truncate) pages in both lists of a 2-queue.
1335 void cl_2queue_discard(const struct lu_env *env,
1336 struct cl_io *io, struct cl_2queue *queue)
1339 cl_page_list_discard(env, io, &queue->c2_qin);
1340 cl_page_list_discard(env, io, &queue->c2_qout);
1343 EXPORT_SYMBOL(cl_2queue_discard);
1346 * Assume to own the pages in cl_2queue
1348 void cl_2queue_assume(const struct lu_env *env,
1349 struct cl_io *io, struct cl_2queue *queue)
1351 cl_page_list_assume(env, io, &queue->c2_qin);
1352 cl_page_list_assume(env, io, &queue->c2_qout);
1354 EXPORT_SYMBOL(cl_2queue_assume);
1357 * Finalize both page lists of a 2-queue.
1359 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1362 cl_page_list_fini(env, &queue->c2_qout);
1363 cl_page_list_fini(env, &queue->c2_qin);
1366 EXPORT_SYMBOL(cl_2queue_fini);
1369 * Initialize a 2-queue to contain \a page in its incoming page list.
1371 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1374 cl_2queue_init(queue);
1375 cl_2queue_add(queue, page);
1378 EXPORT_SYMBOL(cl_2queue_init_page);
1381 * Returns top-level io.
1383 * \see cl_object_top(), cl_page_top().
1385 struct cl_io *cl_io_top(struct cl_io *io)
1388 while (io->ci_parent != NULL)
1392 EXPORT_SYMBOL(cl_io_top);
1395 * Prints human readable representation of \a io to the \a f.
1397 void cl_io_print(const struct lu_env *env, void *cookie,
1398 lu_printer_t printer, const struct cl_io *io)
1403 * Adds request slice to the compound request.
1405 * This is called by cl_device_operations::cdo_req_init() methods to add a
1406 * per-layer state to the request. New state is added at the end of
1407 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1409 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1411 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1412 struct cl_device *dev,
1413 const struct cl_req_operations *ops)
1416 cfs_list_add_tail(&slice->crs_linkage, &req->crq_layers);
1417 slice->crs_dev = dev;
1418 slice->crs_ops = ops;
1419 slice->crs_req = req;
1422 EXPORT_SYMBOL(cl_req_slice_add);
1424 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1428 LASSERT(cfs_list_empty(&req->crq_pages));
1429 LASSERT(req->crq_nrpages == 0);
1430 LINVRNT(cfs_list_empty(&req->crq_layers));
1431 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1434 if (req->crq_o != NULL) {
1435 for (i = 0; i < req->crq_nrobjs; ++i) {
1436 struct cl_object *obj = req->crq_o[i].ro_obj;
1438 lu_object_ref_del_at(&obj->co_lu,
1439 req->crq_o[i].ro_obj_ref,
1441 cl_object_put(env, obj);
1444 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1450 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1451 struct cl_page *page)
1453 struct cl_device *dev;
1454 struct cl_page_slice *slice;
1459 page = cl_page_top(page);
1461 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1462 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1463 if (dev->cd_ops->cdo_req_init != NULL) {
1464 result = dev->cd_ops->cdo_req_init(env,
1470 page = page->cp_child;
1471 } while (page != NULL && result == 0);
1476 * Invokes per-request transfer completion call-backs
1477 * (cl_req_operations::cro_completion()) bottom-to-top.
1479 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1481 struct cl_req_slice *slice;
1485 * for the lack of list_for_each_entry_reverse_safe()...
1487 while (!cfs_list_empty(&req->crq_layers)) {
1488 slice = cfs_list_entry(req->crq_layers.prev,
1489 struct cl_req_slice, crs_linkage);
1490 cfs_list_del_init(&slice->crs_linkage);
1491 if (slice->crs_ops->cro_completion != NULL)
1492 slice->crs_ops->cro_completion(env, slice, rc);
1494 cl_req_free(env, req);
1497 EXPORT_SYMBOL(cl_req_completion);
1500 * Allocates new transfer request.
1502 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1503 enum cl_req_type crt, int nr_objects)
1507 LINVRNT(nr_objects > 0);
1514 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1515 if (req->crq_o != NULL) {
1516 req->crq_nrobjs = nr_objects;
1517 req->crq_type = crt;
1518 CFS_INIT_LIST_HEAD(&req->crq_pages);
1519 CFS_INIT_LIST_HEAD(&req->crq_layers);
1520 result = cl_req_init(env, req, page);
1524 cl_req_completion(env, req, result);
1525 req = ERR_PTR(result);
1528 req = ERR_PTR(-ENOMEM);
1531 EXPORT_SYMBOL(cl_req_alloc);
1534 * Adds a page to a request.
1536 void cl_req_page_add(const struct lu_env *env,
1537 struct cl_req *req, struct cl_page *page)
1539 struct cl_object *obj;
1540 struct cl_req_obj *rqo;
1544 page = cl_page_top(page);
1546 LINVRNT(cl_page_is_vmlocked(env, page));
1547 LASSERT(cfs_list_empty(&page->cp_flight));
1548 LASSERT(page->cp_req == NULL);
1550 cfs_list_add_tail(&page->cp_flight, &req->crq_pages);
1553 obj = cl_object_top(page->cp_obj);
1554 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1555 if (rqo->ro_obj == NULL) {
1558 rqo->ro_obj_ref = lu_object_ref_add(&obj->co_lu,
1563 LASSERT(i < req->crq_nrobjs);
1566 EXPORT_SYMBOL(cl_req_page_add);
1569 * Removes a page from a request.
1571 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1573 struct cl_req *req = page->cp_req;
1576 page = cl_page_top(page);
1578 LINVRNT(cl_page_is_vmlocked(env, page));
1579 LASSERT(!cfs_list_empty(&page->cp_flight));
1580 LASSERT(req->crq_nrpages > 0);
1582 cfs_list_del_init(&page->cp_flight);
1584 page->cp_req = NULL;
1587 EXPORT_SYMBOL(cl_req_page_done);
1590 * Notifies layers that request is about to depart by calling
1591 * cl_req_operations::cro_prep() top-to-bottom.
1593 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1597 const struct cl_req_slice *slice;
1601 * Check that the caller of cl_req_alloc() didn't lie about the number
1604 for (i = 0; i < req->crq_nrobjs; ++i)
1605 LASSERT(req->crq_o[i].ro_obj != NULL);
1608 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1609 if (slice->crs_ops->cro_prep != NULL) {
1610 result = slice->crs_ops->cro_prep(env, slice);
1617 EXPORT_SYMBOL(cl_req_prep);
1620 * Fills in attributes that are passed to server together with transfer. Only
1621 * attributes from \a flags may be touched. This can be called multiple times
1622 * for the same request.
1624 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1625 struct cl_req_attr *attr, obd_valid flags)
1627 const struct cl_req_slice *slice;
1628 struct cl_page *page;
1631 LASSERT(!cfs_list_empty(&req->crq_pages));
1634 /* Take any page to use as a model. */
1635 page = cfs_list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1637 for (i = 0; i < req->crq_nrobjs; ++i) {
1638 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1639 const struct cl_page_slice *scan;
1640 const struct cl_object *obj;
1642 scan = cl_page_at(page,
1643 slice->crs_dev->cd_lu_dev.ld_type);
1644 LASSERT(scan != NULL);
1645 obj = scan->cpl_obj;
1646 if (slice->crs_ops->cro_attr_set != NULL)
1647 slice->crs_ops->cro_attr_set(env, slice, obj,
1653 EXPORT_SYMBOL(cl_req_attr_set);
1655 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1656 * implemented in libcfs. */
1658 # include <linux/sched.h>
1659 #else /* __KERNEL__ */
1660 # include <liblustre.h>
1664 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1666 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1669 cfs_waitq_init(&anchor->csi_waitq);
1670 cfs_atomic_set(&anchor->csi_sync_nr, nrpages);
1671 anchor->csi_sync_rc = 0;
1674 EXPORT_SYMBOL(cl_sync_io_init);
1677 * Wait until all transfer completes. Transfer completion routine has to call
1678 * cl_sync_io_note() for every page.
1680 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1681 struct cl_page_list *queue, struct cl_sync_io *anchor,
1684 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1689 LASSERT(timeout >= 0);
1691 rc = l_wait_event(anchor->csi_waitq,
1692 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1695 CERROR("SYNC IO failed with error: %d, try to cancel "
1696 "%d remaining pages\n",
1697 rc, cfs_atomic_read(&anchor->csi_sync_nr));
1699 (void)cl_io_cancel(env, io, queue);
1701 lwi = (struct l_wait_info) { 0 };
1702 (void)l_wait_event(anchor->csi_waitq,
1703 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1706 rc = anchor->csi_sync_rc;
1708 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) == 0);
1709 cl_page_list_assume(env, io, queue);
1710 POISON(anchor, 0x5a, sizeof *anchor);
1713 EXPORT_SYMBOL(cl_sync_io_wait);
1716 * Indicate that transfer of a single page completed.
1718 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1721 if (anchor->csi_sync_rc == 0 && ioret < 0)
1722 anchor->csi_sync_rc = ioret;
1724 * Synchronous IO done without releasing page lock (e.g., as a part of
1725 * ->{prepare,commit}_write(). Completion is used to signal the end of
1728 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) > 0);
1729 if (cfs_atomic_dec_and_test(&anchor->csi_sync_nr))
1730 cfs_waitq_broadcast(&anchor->csi_waitq);
1733 EXPORT_SYMBOL(cl_sync_io_note);