4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
42 #define DEBUG_SUBSYSTEM S_CLASS
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <lustre_fid.h>
47 #include <libcfs/list.h>
48 #include <cl_object.h>
49 #include "cl_internal.h"
51 /*****************************************************************************
57 #define cl_io_for_each(slice, io) \
58 list_for_each_entry((slice), &io->ci_layers, cis_linkage)
59 #define cl_io_for_each_reverse(slice, io) \
60 list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
62 static inline int cl_io_type_is_valid(enum cl_io_type type)
64 return CIT_READ <= type && type < CIT_OP_NR;
67 static inline int cl_io_is_loopable(const struct cl_io *io)
69 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
73 * Returns true iff there is an IO ongoing in the given environment.
75 int cl_io_is_going(const struct lu_env *env)
77 return cl_env_info(env)->clt_current_io != NULL;
79 EXPORT_SYMBOL(cl_io_is_going);
82 * cl_io invariant that holds at all times when exported cl_io_*() functions
83 * are entered and left.
85 static int cl_io_invariant(const struct cl_io *io)
92 * io can own pages only when it is ongoing. Sub-io might
93 * still be in CIS_LOCKED state when top-io is in
96 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
97 (io->ci_state == CIS_LOCKED && up != NULL));
101 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
103 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
105 struct cl_io_slice *slice;
106 struct cl_thread_info *info;
108 LINVRNT(cl_io_type_is_valid(io->ci_type));
109 LINVRNT(cl_io_invariant(io));
112 while (!list_empty(&io->ci_layers)) {
113 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
115 list_del_init(&slice->cis_linkage);
116 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
117 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
119 * Invalidate slice to catch use after free. This assumes that
120 * slices are allocated within session and can be touched
121 * after ->cio_fini() returns.
123 slice->cis_io = NULL;
125 io->ci_state = CIS_FINI;
126 info = cl_env_info(env);
127 if (info->clt_current_io == io)
128 info->clt_current_io = NULL;
130 /* sanity check for layout change */
131 switch(io->ci_type) {
137 LASSERT(!io->ci_need_restart);
141 /* Check ignore layout change conf */
142 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
143 !io->ci_need_restart));
150 EXPORT_SYMBOL(cl_io_fini);
152 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
153 enum cl_io_type iot, struct cl_object *obj)
155 struct cl_object *scan;
158 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
159 LINVRNT(cl_io_type_is_valid(iot));
160 LINVRNT(cl_io_invariant(io));
164 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
165 INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
166 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
167 INIT_LIST_HEAD(&io->ci_layers);
170 cl_object_for_each(scan, obj) {
171 if (scan->co_ops->coo_io_init != NULL) {
172 result = scan->co_ops->coo_io_init(env, scan, io);
178 io->ci_state = CIS_INIT;
183 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
185 * \pre obj != cl_object_top(obj)
187 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
188 enum cl_io_type iot, struct cl_object *obj)
190 struct cl_thread_info *info = cl_env_info(env);
192 LASSERT(obj != cl_object_top(obj));
193 if (info->clt_current_io == NULL)
194 info->clt_current_io = io;
195 return cl_io_init0(env, io, iot, obj);
197 EXPORT_SYMBOL(cl_io_sub_init);
200 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
202 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
203 * what the latter returned.
205 * \pre obj == cl_object_top(obj)
206 * \pre cl_io_type_is_valid(iot)
207 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
209 int cl_io_init(const struct lu_env *env, struct cl_io *io,
210 enum cl_io_type iot, struct cl_object *obj)
212 struct cl_thread_info *info = cl_env_info(env);
214 LASSERT(obj == cl_object_top(obj));
215 LASSERT(info->clt_current_io == NULL);
217 info->clt_current_io = io;
218 return cl_io_init0(env, io, iot, obj);
220 EXPORT_SYMBOL(cl_io_init);
223 * Initialize read or write io.
225 * \pre iot == CIT_READ || iot == CIT_WRITE
227 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
228 enum cl_io_type iot, loff_t pos, size_t count)
230 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
231 LINVRNT(io->ci_obj != NULL);
234 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
235 "io range: %u ["LPU64", "LPU64") %u %u\n",
236 iot, (__u64)pos, (__u64)pos + count,
237 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
238 io->u.ci_rw.crw_pos = pos;
239 io->u.ci_rw.crw_count = count;
240 RETURN(cl_io_init(env, io, iot, io->ci_obj));
242 EXPORT_SYMBOL(cl_io_rw_init);
244 static inline const struct lu_fid *
245 cl_lock_descr_fid(const struct cl_lock_descr *descr)
247 return lu_object_fid(&descr->cld_obj->co_lu);
250 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
251 const struct cl_lock_descr *d1)
253 return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
254 __diff_normalize(d0->cld_start, d1->cld_start);
257 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
258 const struct cl_lock_descr *d1)
262 ret = lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1));
265 if (d0->cld_end < d1->cld_start)
267 if (d0->cld_start > d0->cld_end)
272 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
273 const struct cl_lock_descr *d1)
275 d0->cld_start = min(d0->cld_start, d1->cld_start);
276 d0->cld_end = max(d0->cld_end, d1->cld_end);
278 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
279 d0->cld_mode = CLM_WRITE;
281 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
282 d0->cld_mode = CLM_GROUP;
286 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
288 static void cl_io_locks_sort(struct cl_io *io)
293 /* hidden treasure: bubble sort for now. */
295 struct cl_io_lock_link *curr;
296 struct cl_io_lock_link *prev;
297 struct cl_io_lock_link *temp;
302 list_for_each_entry_safe(curr, temp, &io->ci_lockset.cls_todo,
305 switch (cl_lock_descr_sort(&prev->cill_descr,
306 &curr->cill_descr)) {
309 * IMPOSSIBLE: Identical locks are
316 list_move_tail(&curr->cill_linkage,
317 &prev->cill_linkage);
319 continue; /* don't change prev: it's
320 * still "previous" */
321 case -1: /* already in order */
332 * Check whether \a queue contains locks matching \a need.
334 * \retval +ve there is a matching lock in the \a queue
335 * \retval 0 there are no matching locks in the \a queue
337 int cl_queue_match(const struct list_head *queue,
338 const struct cl_lock_descr *need)
340 struct cl_io_lock_link *scan;
343 list_for_each_entry(scan, queue, cill_linkage) {
344 if (cl_lock_descr_match(&scan->cill_descr, need))
349 EXPORT_SYMBOL(cl_queue_match);
351 static int cl_queue_merge(const struct list_head *queue,
352 const struct cl_lock_descr *need)
354 struct cl_io_lock_link *scan;
357 list_for_each_entry(scan, queue, cill_linkage) {
358 if (cl_lock_descr_cmp(&scan->cill_descr, need))
360 cl_lock_descr_merge(&scan->cill_descr, need);
361 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
362 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
363 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 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
395 link->cill_lock = lock;
396 list_move(&link->cill_linkage, &set->cls_curr);
397 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
398 result = cl_wait(env, lock);
400 list_move(&link->cill_linkage, &set->cls_done);
404 result = PTR_ERR(lock);
408 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
409 struct cl_io_lock_link *link)
411 struct cl_lock *lock = link->cill_lock;
414 list_del_init(&link->cill_linkage);
416 cl_lock_release(env, lock, "io", io);
417 link->cill_lock = NULL;
419 if (link->cill_fini != NULL)
420 link->cill_fini(env, link);
424 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
425 struct cl_lockset *set)
427 struct cl_io_lock_link *link;
428 struct cl_io_lock_link *temp;
429 struct cl_lock *lock;
434 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
435 if (!cl_lockset_match(set, &link->cill_descr)) {
436 /* XXX some locking to guarantee that locks aren't
437 * expanded in between. */
438 result = cl_lockset_lock_one(env, io, set, link);
442 cl_lock_link_fini(env, io, link);
445 list_for_each_entry_safe(link, temp, &set->cls_curr,
447 lock = link->cill_lock;
448 result = cl_wait(env, lock);
450 list_move(&link->cill_linkage, &set->cls_done);
459 * Takes locks necessary for the current iteration of io.
461 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
462 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
465 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
467 const struct cl_io_slice *scan;
470 LINVRNT(cl_io_is_loopable(io));
471 LINVRNT(io->ci_state == CIS_IT_STARTED);
472 LINVRNT(cl_io_invariant(io));
475 cl_io_for_each(scan, io) {
476 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
478 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
483 cl_io_locks_sort(io);
484 result = cl_lockset_lock(env, io, &io->ci_lockset);
487 cl_io_unlock(env, io);
489 io->ci_state = CIS_LOCKED;
492 EXPORT_SYMBOL(cl_io_lock);
495 * Release locks takes by io.
497 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
499 struct cl_lockset *set;
500 struct cl_io_lock_link *link;
501 struct cl_io_lock_link *temp;
502 const struct cl_io_slice *scan;
504 LASSERT(cl_io_is_loopable(io));
505 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
506 LINVRNT(cl_io_invariant(io));
509 set = &io->ci_lockset;
511 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
512 cl_lock_link_fini(env, io, link);
514 list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
515 cl_lock_link_fini(env, io, link);
517 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
518 cl_unuse(env, link->cill_lock);
519 cl_lock_link_fini(env, io, link);
521 cl_io_for_each_reverse(scan, io) {
522 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
523 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
525 io->ci_state = CIS_UNLOCKED;
526 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
529 EXPORT_SYMBOL(cl_io_unlock);
532 * Prepares next iteration of io.
534 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
535 * layers a chance to modify io parameters, e.g., so that lov can restrict io
536 * to a single stripe.
538 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
540 const struct cl_io_slice *scan;
543 LINVRNT(cl_io_is_loopable(io));
544 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
545 LINVRNT(cl_io_invariant(io));
549 cl_io_for_each(scan, io) {
550 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
552 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
558 io->ci_state = CIS_IT_STARTED;
561 EXPORT_SYMBOL(cl_io_iter_init);
564 * Finalizes io iteration.
566 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
568 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
570 const struct cl_io_slice *scan;
572 LINVRNT(cl_io_is_loopable(io));
573 LINVRNT(io->ci_state == CIS_UNLOCKED);
574 LINVRNT(cl_io_invariant(io));
577 cl_io_for_each_reverse(scan, io) {
578 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
579 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
581 io->ci_state = CIS_IT_ENDED;
584 EXPORT_SYMBOL(cl_io_iter_fini);
587 * Records that read or write io progressed \a nob bytes forward.
589 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
591 const struct cl_io_slice *scan;
593 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
595 LINVRNT(cl_io_is_loopable(io));
596 LINVRNT(cl_io_invariant(io));
600 io->u.ci_rw.crw_pos += nob;
601 io->u.ci_rw.crw_count -= nob;
603 /* layers have to be notified. */
604 cl_io_for_each_reverse(scan, io) {
605 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
606 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
611 EXPORT_SYMBOL(cl_io_rw_advance);
614 * Adds a lock to a lockset.
616 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
617 struct cl_io_lock_link *link)
622 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
625 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
630 EXPORT_SYMBOL(cl_io_lock_add);
632 static void cl_free_io_lock_link(const struct lu_env *env,
633 struct cl_io_lock_link *link)
639 * Allocates new lock link, and uses it to add a lock to a lockset.
641 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
642 struct cl_lock_descr *descr)
644 struct cl_io_lock_link *link;
650 link->cill_descr = *descr;
651 link->cill_fini = cl_free_io_lock_link;
652 result = cl_io_lock_add(env, io, link);
653 if (result) /* lock match */
654 link->cill_fini(env, link);
660 EXPORT_SYMBOL(cl_io_lock_alloc_add);
663 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
665 int cl_io_start(const struct lu_env *env, struct cl_io *io)
667 const struct cl_io_slice *scan;
670 LINVRNT(cl_io_is_loopable(io));
671 LINVRNT(io->ci_state == CIS_LOCKED);
672 LINVRNT(cl_io_invariant(io));
675 io->ci_state = CIS_IO_GOING;
676 cl_io_for_each(scan, io) {
677 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
679 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
687 EXPORT_SYMBOL(cl_io_start);
690 * Wait until current io iteration is finished by calling
691 * cl_io_operations::cio_end() bottom-to-top.
693 void cl_io_end(const struct lu_env *env, struct cl_io *io)
695 const struct cl_io_slice *scan;
697 LINVRNT(cl_io_is_loopable(io));
698 LINVRNT(io->ci_state == CIS_IO_GOING);
699 LINVRNT(cl_io_invariant(io));
702 cl_io_for_each_reverse(scan, io) {
703 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
704 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
705 /* TODO: error handling. */
707 io->ci_state = CIS_IO_FINISHED;
710 EXPORT_SYMBOL(cl_io_end);
712 static const struct cl_page_slice *
713 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
715 const struct cl_page_slice *slice;
717 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
718 LINVRNT(slice != NULL);
723 * Called by read io, when page has to be read from the server.
725 * \see cl_io_operations::cio_read_page()
727 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
728 struct cl_page *page)
730 const struct cl_io_slice *scan;
731 struct cl_2queue *queue;
734 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
735 LINVRNT(cl_page_is_owned(page, io));
736 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
737 LINVRNT(cl_io_invariant(io));
740 queue = &io->ci_queue;
742 cl_2queue_init(queue);
744 * ->cio_read_page() methods called in the loop below are supposed to
745 * never block waiting for network (the only subtle point is the
746 * creation of new pages for read-ahead that might result in cache
747 * shrinking, but currently only clean pages are shrunk and this
748 * requires no network io).
750 * Should this ever starts blocking, retry loop would be needed for
751 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
753 cl_io_for_each(scan, io) {
754 if (scan->cis_iop->cio_read_page != NULL) {
755 const struct cl_page_slice *slice;
757 slice = cl_io_slice_page(scan, page);
758 LINVRNT(slice != NULL);
759 result = scan->cis_iop->cio_read_page(env, scan, slice);
764 if (result == 0 && queue->c2_qin.pl_nr > 0)
765 result = cl_io_submit_rw(env, io, CRT_READ, queue);
767 * Unlock unsent pages in case of error.
769 cl_page_list_disown(env, io, &queue->c2_qin);
770 cl_2queue_fini(env, queue);
773 EXPORT_SYMBOL(cl_io_read_page);
776 * Commit a list of contiguous pages into writeback cache.
778 * \returns 0 if all pages committed, or errcode if error occurred.
779 * \see cl_io_operations::cio_commit_async()
781 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
782 struct cl_page_list *queue, int from, int to,
785 const struct cl_io_slice *scan;
789 cl_io_for_each(scan, io) {
790 if (scan->cis_iop->cio_commit_async == NULL)
792 result = scan->cis_iop->cio_commit_async(env, scan, queue,
799 EXPORT_SYMBOL(cl_io_commit_async);
802 * Submits a list of pages for immediate io.
804 * After the function gets returned, The submitted pages are moved to
805 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
806 * to be submitted, and the pages are errant to submit.
808 * \returns 0 if at least one page was submitted, error code otherwise.
809 * \see cl_io_operations::cio_submit()
811 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
812 enum cl_req_type crt, struct cl_2queue *queue)
814 const struct cl_io_slice *scan;
818 cl_io_for_each(scan, io) {
819 if (scan->cis_iop->cio_submit == NULL)
821 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
826 * If ->cio_submit() failed, no pages were sent.
828 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
831 EXPORT_SYMBOL(cl_io_submit_rw);
834 * Submit a sync_io and wait for the IO to be finished, or error happens.
835 * If \a timeout is zero, it means to wait for the IO unconditionally.
837 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
838 enum cl_req_type iot, struct cl_2queue *queue,
841 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
845 cl_page_list_for_each(pg, &queue->c2_qin) {
846 LASSERT(pg->cp_sync_io == NULL);
847 pg->cp_sync_io = anchor;
850 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
851 rc = cl_io_submit_rw(env, io, iot, queue);
854 * If some pages weren't sent for any reason (e.g.,
855 * read found up-to-date pages in the cache, or write found
856 * clean pages), count them as completed to avoid infinite
859 cl_page_list_for_each(pg, &queue->c2_qin) {
860 pg->cp_sync_io = NULL;
861 cl_sync_io_note(anchor, +1);
864 /* wait for the IO to be finished. */
865 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
868 LASSERT(list_empty(&queue->c2_qout.pl_pages));
869 cl_page_list_for_each(pg, &queue->c2_qin)
870 pg->cp_sync_io = NULL;
874 EXPORT_SYMBOL(cl_io_submit_sync);
877 * Cancel an IO which has been submitted by cl_io_submit_rw.
879 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
880 struct cl_page_list *queue)
882 struct cl_page *page;
885 CERROR("Canceling ongoing page trasmission\n");
886 cl_page_list_for_each(page, queue) {
889 rc = cl_page_cancel(env, page);
890 result = result ?: rc;
894 EXPORT_SYMBOL(cl_io_cancel);
899 * Pumps io through iterations calling
901 * - cl_io_iter_init()
911 * - cl_io_iter_fini()
913 * repeatedly until there is no more io to do.
915 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
919 LINVRNT(cl_io_is_loopable(io));
926 result = cl_io_iter_init(env, io);
929 result = cl_io_lock(env, io);
932 * Notify layers that locks has been taken,
935 * - llite: kms, short read;
936 * - llite: generic_file_read();
938 result = cl_io_start(env, io);
940 * Send any remaining pending
943 * - llite: ll_rw_stats_tally.
946 cl_io_unlock(env, io);
947 cl_io_rw_advance(env, io, io->ci_nob - nob);
950 cl_io_iter_fini(env, io);
951 } while (result == 0 && io->ci_continue);
953 result = io->ci_result;
954 RETURN(result < 0 ? result : 0);
956 EXPORT_SYMBOL(cl_io_loop);
959 * Adds io slice to the cl_io.
961 * This is called by cl_object_operations::coo_io_init() methods to add a
962 * per-layer state to the io. New state is added at the end of
963 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
965 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
967 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
968 struct cl_object *obj,
969 const struct cl_io_operations *ops)
971 struct list_head *linkage = &slice->cis_linkage;
973 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
974 list_empty(linkage));
977 list_add_tail(linkage, &io->ci_layers);
979 slice->cis_obj = obj;
980 slice->cis_iop = ops;
983 EXPORT_SYMBOL(cl_io_slice_add);
987 * Initializes page list.
989 void cl_page_list_init(struct cl_page_list *plist)
993 INIT_LIST_HEAD(&plist->pl_pages);
994 plist->pl_owner = current;
997 EXPORT_SYMBOL(cl_page_list_init);
1000 * Adds a page to a page list.
1002 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1005 /* it would be better to check that page is owned by "current" io, but
1006 * it is not passed here. */
1007 LASSERT(page->cp_owner != NULL);
1008 LINVRNT(plist->pl_owner == current);
1010 LASSERT(list_empty(&page->cp_batch));
1011 list_add_tail(&page->cp_batch, &plist->pl_pages);
1013 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1017 EXPORT_SYMBOL(cl_page_list_add);
1020 * Removes a page from a page list.
1022 void cl_page_list_del(const struct lu_env *env,
1023 struct cl_page_list *plist, struct cl_page *page)
1025 LASSERT(plist->pl_nr > 0);
1026 LASSERT(cl_page_is_vmlocked(env, page));
1027 LINVRNT(plist->pl_owner == current);
1030 list_del_init(&page->cp_batch);
1032 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1033 cl_page_put(env, page);
1036 EXPORT_SYMBOL(cl_page_list_del);
1039 * Moves a page from one page list to another.
1041 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1042 struct cl_page *page)
1044 LASSERT(src->pl_nr > 0);
1045 LINVRNT(dst->pl_owner == current);
1046 LINVRNT(src->pl_owner == current);
1049 list_move_tail(&page->cp_batch, &dst->pl_pages);
1052 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1056 EXPORT_SYMBOL(cl_page_list_move);
1059 * Moves a page from one page list to the head of another list.
1061 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
1062 struct cl_page *page)
1064 LASSERT(src->pl_nr > 0);
1065 LINVRNT(dst->pl_owner == current);
1066 LINVRNT(src->pl_owner == current);
1069 list_move(&page->cp_batch, &dst->pl_pages);
1072 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1076 EXPORT_SYMBOL(cl_page_list_move_head);
1079 * splice the cl_page_list, just as list head does
1081 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1083 struct cl_page *page;
1084 struct cl_page *tmp;
1086 LINVRNT(list->pl_owner == current);
1087 LINVRNT(head->pl_owner == current);
1090 cl_page_list_for_each_safe(page, tmp, list)
1091 cl_page_list_move(head, list, page);
1094 EXPORT_SYMBOL(cl_page_list_splice);
1096 void cl_page_disown0(const struct lu_env *env,
1097 struct cl_io *io, struct cl_page *pg);
1100 * Disowns pages in a queue.
1102 void cl_page_list_disown(const struct lu_env *env,
1103 struct cl_io *io, struct cl_page_list *plist)
1105 struct cl_page *page;
1106 struct cl_page *temp;
1108 LINVRNT(plist->pl_owner == current);
1111 cl_page_list_for_each_safe(page, temp, plist) {
1112 LASSERT(plist->pl_nr > 0);
1114 list_del_init(&page->cp_batch);
1117 * cl_page_disown0 rather than usual cl_page_disown() is used,
1118 * because pages are possibly in CPS_FREEING state already due
1119 * to the call to cl_page_list_discard().
1122 * XXX cl_page_disown0() will fail if page is not locked.
1124 cl_page_disown0(env, io, page);
1125 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1127 cl_page_put(env, page);
1131 EXPORT_SYMBOL(cl_page_list_disown);
1134 * Releases pages from queue.
1136 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1138 struct cl_page *page;
1139 struct cl_page *temp;
1141 LINVRNT(plist->pl_owner == current);
1144 cl_page_list_for_each_safe(page, temp, plist)
1145 cl_page_list_del(env, plist, page);
1146 LASSERT(plist->pl_nr == 0);
1149 EXPORT_SYMBOL(cl_page_list_fini);
1152 * Owns all pages in a queue.
1154 int cl_page_list_own(const struct lu_env *env,
1155 struct cl_io *io, struct cl_page_list *plist)
1157 struct cl_page *page;
1158 struct cl_page *temp;
1161 LINVRNT(plist->pl_owner == current);
1165 cl_page_list_for_each_safe(page, temp, plist) {
1166 if (cl_page_own(env, io, page) == 0)
1167 result = result ?: page->cp_error;
1169 cl_page_list_del(env, plist, page);
1173 EXPORT_SYMBOL(cl_page_list_own);
1176 * Assumes all pages in a queue.
1178 void cl_page_list_assume(const struct lu_env *env,
1179 struct cl_io *io, struct cl_page_list *plist)
1181 struct cl_page *page;
1183 LINVRNT(plist->pl_owner == current);
1185 cl_page_list_for_each(page, plist)
1186 cl_page_assume(env, io, page);
1188 EXPORT_SYMBOL(cl_page_list_assume);
1191 * Discards all pages in a queue.
1193 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1194 struct cl_page_list *plist)
1196 struct cl_page *page;
1198 LINVRNT(plist->pl_owner == current);
1200 cl_page_list_for_each(page, plist)
1201 cl_page_discard(env, io, page);
1204 EXPORT_SYMBOL(cl_page_list_discard);
1207 * Initialize dual page queue.
1209 void cl_2queue_init(struct cl_2queue *queue)
1212 cl_page_list_init(&queue->c2_qin);
1213 cl_page_list_init(&queue->c2_qout);
1216 EXPORT_SYMBOL(cl_2queue_init);
1219 * Add a page to the incoming page list of 2-queue.
1221 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1224 cl_page_list_add(&queue->c2_qin, page);
1227 EXPORT_SYMBOL(cl_2queue_add);
1230 * Disown pages in both lists of a 2-queue.
1232 void cl_2queue_disown(const struct lu_env *env,
1233 struct cl_io *io, struct cl_2queue *queue)
1236 cl_page_list_disown(env, io, &queue->c2_qin);
1237 cl_page_list_disown(env, io, &queue->c2_qout);
1240 EXPORT_SYMBOL(cl_2queue_disown);
1243 * Discard (truncate) pages in both lists of a 2-queue.
1245 void cl_2queue_discard(const struct lu_env *env,
1246 struct cl_io *io, struct cl_2queue *queue)
1249 cl_page_list_discard(env, io, &queue->c2_qin);
1250 cl_page_list_discard(env, io, &queue->c2_qout);
1253 EXPORT_SYMBOL(cl_2queue_discard);
1256 * Assume to own the pages in cl_2queue
1258 void cl_2queue_assume(const struct lu_env *env,
1259 struct cl_io *io, struct cl_2queue *queue)
1261 cl_page_list_assume(env, io, &queue->c2_qin);
1262 cl_page_list_assume(env, io, &queue->c2_qout);
1264 EXPORT_SYMBOL(cl_2queue_assume);
1267 * Finalize both page lists of a 2-queue.
1269 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1272 cl_page_list_fini(env, &queue->c2_qout);
1273 cl_page_list_fini(env, &queue->c2_qin);
1276 EXPORT_SYMBOL(cl_2queue_fini);
1279 * Initialize a 2-queue to contain \a page in its incoming page list.
1281 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1284 cl_2queue_init(queue);
1285 cl_2queue_add(queue, page);
1288 EXPORT_SYMBOL(cl_2queue_init_page);
1291 * Returns top-level io.
1293 * \see cl_object_top()
1295 struct cl_io *cl_io_top(struct cl_io *io)
1298 while (io->ci_parent != NULL)
1302 EXPORT_SYMBOL(cl_io_top);
1305 * Prints human readable representation of \a io to the \a f.
1307 void cl_io_print(const struct lu_env *env, void *cookie,
1308 lu_printer_t printer, const struct cl_io *io)
1313 * Adds request slice to the compound request.
1315 * This is called by cl_device_operations::cdo_req_init() methods to add a
1316 * per-layer state to the request. New state is added at the end of
1317 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1319 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1321 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1322 struct cl_device *dev,
1323 const struct cl_req_operations *ops)
1326 list_add_tail(&slice->crs_linkage, &req->crq_layers);
1327 slice->crs_dev = dev;
1328 slice->crs_ops = ops;
1329 slice->crs_req = req;
1332 EXPORT_SYMBOL(cl_req_slice_add);
1334 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1338 LASSERT(list_empty(&req->crq_pages));
1339 LASSERT(req->crq_nrpages == 0);
1340 LINVRNT(list_empty(&req->crq_layers));
1341 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1344 if (req->crq_o != NULL) {
1345 for (i = 0; i < req->crq_nrobjs; ++i) {
1346 struct cl_object *obj = req->crq_o[i].ro_obj;
1348 lu_object_ref_del_at(&obj->co_lu,
1349 &req->crq_o[i].ro_obj_ref,
1351 cl_object_put(env, obj);
1354 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1360 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1361 struct cl_page *page)
1363 struct cl_device *dev;
1364 struct cl_page_slice *slice;
1369 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1370 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1371 if (dev->cd_ops->cdo_req_init != NULL) {
1372 result = dev->cd_ops->cdo_req_init(env,
1382 * Invokes per-request transfer completion call-backs
1383 * (cl_req_operations::cro_completion()) bottom-to-top.
1385 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1387 struct cl_req_slice *slice;
1391 * for the lack of list_for_each_entry_reverse_safe()...
1393 while (!list_empty(&req->crq_layers)) {
1394 slice = list_entry(req->crq_layers.prev,
1395 struct cl_req_slice, crs_linkage);
1396 list_del_init(&slice->crs_linkage);
1397 if (slice->crs_ops->cro_completion != NULL)
1398 slice->crs_ops->cro_completion(env, slice, rc);
1400 cl_req_free(env, req);
1403 EXPORT_SYMBOL(cl_req_completion);
1406 * Allocates new transfer request.
1408 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1409 enum cl_req_type crt, int nr_objects)
1413 LINVRNT(nr_objects > 0);
1420 req->crq_type = crt;
1421 INIT_LIST_HEAD(&req->crq_pages);
1422 INIT_LIST_HEAD(&req->crq_layers);
1424 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1425 if (req->crq_o != NULL) {
1426 req->crq_nrobjs = nr_objects;
1427 result = cl_req_init(env, req, page);
1431 cl_req_completion(env, req, result);
1432 req = ERR_PTR(result);
1435 req = ERR_PTR(-ENOMEM);
1438 EXPORT_SYMBOL(cl_req_alloc);
1441 * Adds a page to a request.
1443 void cl_req_page_add(const struct lu_env *env,
1444 struct cl_req *req, struct cl_page *page)
1446 struct cl_object *obj;
1447 struct cl_req_obj *rqo;
1452 LASSERT(list_empty(&page->cp_flight));
1453 LASSERT(page->cp_req == NULL);
1455 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1456 req, req->crq_type, req->crq_nrpages);
1458 list_add_tail(&page->cp_flight, &req->crq_pages);
1461 obj = cl_object_top(page->cp_obj);
1462 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1463 if (rqo->ro_obj == NULL) {
1466 lu_object_ref_add_at(&obj->co_lu, &rqo->ro_obj_ref,
1471 LASSERT(i < req->crq_nrobjs);
1474 EXPORT_SYMBOL(cl_req_page_add);
1477 * Removes a page from a request.
1479 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1481 struct cl_req *req = page->cp_req;
1485 LASSERT(!list_empty(&page->cp_flight));
1486 LASSERT(req->crq_nrpages > 0);
1488 list_del_init(&page->cp_flight);
1490 page->cp_req = NULL;
1493 EXPORT_SYMBOL(cl_req_page_done);
1496 * Notifies layers that request is about to depart by calling
1497 * cl_req_operations::cro_prep() top-to-bottom.
1499 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1503 const struct cl_req_slice *slice;
1507 * Check that the caller of cl_req_alloc() didn't lie about the number
1510 for (i = 0; i < req->crq_nrobjs; ++i)
1511 LASSERT(req->crq_o[i].ro_obj != NULL);
1514 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1515 if (slice->crs_ops->cro_prep != NULL) {
1516 result = slice->crs_ops->cro_prep(env, slice);
1523 EXPORT_SYMBOL(cl_req_prep);
1526 * Fills in attributes that are passed to server together with transfer. Only
1527 * attributes from \a flags may be touched. This can be called multiple times
1528 * for the same request.
1530 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1531 struct cl_req_attr *attr, obd_valid flags)
1533 const struct cl_req_slice *slice;
1534 struct cl_page *page;
1537 LASSERT(!list_empty(&req->crq_pages));
1540 /* Take any page to use as a model. */
1541 page = list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1543 for (i = 0; i < req->crq_nrobjs; ++i) {
1544 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1545 const struct cl_page_slice *scan;
1546 const struct cl_object *obj;
1548 scan = cl_page_at(page,
1549 slice->crs_dev->cd_lu_dev.ld_type);
1550 LASSERT(scan != NULL);
1551 obj = scan->cpl_obj;
1552 if (slice->crs_ops->cro_attr_set != NULL)
1553 slice->crs_ops->cro_attr_set(env, slice, obj,
1559 EXPORT_SYMBOL(cl_req_attr_set);
1561 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1562 * implemented in libcfs. */
1564 # include <linux/sched.h>
1565 #else /* __KERNEL__ */
1566 # include <liblustre.h>
1570 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1572 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1575 init_waitqueue_head(&anchor->csi_waitq);
1576 atomic_set(&anchor->csi_sync_nr, nrpages);
1577 atomic_set(&anchor->csi_barrier, nrpages > 0);
1578 anchor->csi_sync_rc = 0;
1581 EXPORT_SYMBOL(cl_sync_io_init);
1584 * Wait until all transfer completes. Transfer completion routine has to call
1585 * cl_sync_io_note() for every page.
1587 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1588 struct cl_page_list *queue, struct cl_sync_io *anchor,
1591 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1596 LASSERT(timeout >= 0);
1598 rc = l_wait_event(anchor->csi_waitq,
1599 atomic_read(&anchor->csi_sync_nr) == 0,
1602 CERROR("SYNC IO failed with error: %d, try to cancel "
1603 "%d remaining pages\n",
1604 rc, atomic_read(&anchor->csi_sync_nr));
1606 (void)cl_io_cancel(env, io, queue);
1608 lwi = (struct l_wait_info) { 0 };
1609 (void)l_wait_event(anchor->csi_waitq,
1610 atomic_read(&anchor->csi_sync_nr) == 0,
1613 rc = anchor->csi_sync_rc;
1615 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1616 cl_page_list_assume(env, io, queue);
1618 /* wait until cl_sync_io_note() has done wakeup */
1619 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1625 POISON(anchor, 0x5a, sizeof *anchor);
1628 EXPORT_SYMBOL(cl_sync_io_wait);
1631 * Indicate that transfer of a single page completed.
1633 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1636 if (anchor->csi_sync_rc == 0 && ioret < 0)
1637 anchor->csi_sync_rc = ioret;
1639 * Synchronous IO done without releasing page lock (e.g., as a part of
1640 * ->{prepare,commit}_write(). Completion is used to signal the end of
1643 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1644 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1645 wake_up_all(&anchor->csi_waitq);
1646 /* it's safe to nuke or reuse anchor now */
1647 atomic_set(&anchor->csi_barrier, 0);
1651 EXPORT_SYMBOL(cl_sync_io_note);