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 cfs_list_for_each_entry((slice), &io->ci_layers, cis_linkage)
59 #define cl_io_for_each_reverse(slice, io) \
60 cfs_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 (!cfs_list_empty(&io->ci_layers)) {
113 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
115 cfs_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 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
165 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
166 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_done);
167 CFS_INIT_LIST_HEAD(&io->ci_layers);
170 cl_object_for_each(scan, obj) {
171 if (scan->co_ops->coo_io_init != NULL) {
172 result = scan->co_ops->coo_io_init(env, scan, io);
178 io->ci_state = CIS_INIT;
183 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
185 * \pre obj != cl_object_top(obj)
187 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
188 enum cl_io_type iot, struct cl_object *obj)
190 struct cl_thread_info *info = cl_env_info(env);
192 LASSERT(obj != cl_object_top(obj));
193 if (info->clt_current_io == NULL)
194 info->clt_current_io = io;
195 return cl_io_init0(env, io, iot, obj);
197 EXPORT_SYMBOL(cl_io_sub_init);
200 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
202 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
203 * what the latter returned.
205 * \pre obj == cl_object_top(obj)
206 * \pre cl_io_type_is_valid(iot)
207 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
209 int cl_io_init(const struct lu_env *env, struct cl_io *io,
210 enum cl_io_type iot, struct cl_object *obj)
212 struct cl_thread_info *info = cl_env_info(env);
214 LASSERT(obj == cl_object_top(obj));
215 LASSERT(info->clt_current_io == NULL);
217 info->clt_current_io = io;
218 return cl_io_init0(env, io, iot, obj);
220 EXPORT_SYMBOL(cl_io_init);
223 * Initialize read or write io.
225 * \pre iot == CIT_READ || iot == CIT_WRITE
227 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
228 enum cl_io_type iot, loff_t pos, size_t count)
230 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
231 LINVRNT(io->ci_obj != NULL);
234 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
235 "io range: %u ["LPU64", "LPU64") %u %u\n",
236 iot, (__u64)pos, (__u64)pos + count,
237 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
238 io->u.ci_rw.crw_pos = pos;
239 io->u.ci_rw.crw_count = count;
240 RETURN(cl_io_init(env, io, iot, io->ci_obj));
242 EXPORT_SYMBOL(cl_io_rw_init);
244 static inline const struct lu_fid *
245 cl_lock_descr_fid(const struct cl_lock_descr *descr)
247 return lu_object_fid(&descr->cld_obj->co_lu);
250 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
251 const struct cl_lock_descr *d1)
253 return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
254 __diff_normalize(d0->cld_start, d1->cld_start);
257 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
258 const struct cl_lock_descr *d1)
262 ret = lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1));
265 if (d0->cld_end < d1->cld_start)
267 if (d0->cld_start > d0->cld_end)
272 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
273 const struct cl_lock_descr *d1)
275 d0->cld_start = min(d0->cld_start, d1->cld_start);
276 d0->cld_end = max(d0->cld_end, d1->cld_end);
278 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
279 d0->cld_mode = CLM_WRITE;
281 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
282 d0->cld_mode = CLM_GROUP;
286 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
288 static void cl_io_locks_sort(struct cl_io *io)
293 /* hidden treasure: bubble sort for now. */
295 struct cl_io_lock_link *curr;
296 struct cl_io_lock_link *prev;
297 struct cl_io_lock_link *temp;
302 cfs_list_for_each_entry_safe(curr, temp,
303 &io->ci_lockset.cls_todo,
306 switch (cl_lock_descr_sort(&prev->cill_descr,
307 &curr->cill_descr)) {
310 * IMPOSSIBLE: Identical locks are
317 cfs_list_move_tail(&curr->cill_linkage,
318 &prev->cill_linkage);
320 continue; /* don't change prev: it's
321 * still "previous" */
322 case -1: /* already in order */
333 * Check whether \a queue contains locks matching \a need.
335 * \retval +ve there is a matching lock in the \a queue
336 * \retval 0 there are no matching locks in the \a queue
338 int cl_queue_match(const cfs_list_t *queue,
339 const struct cl_lock_descr *need)
341 struct cl_io_lock_link *scan;
344 cfs_list_for_each_entry(scan, queue, cill_linkage) {
345 if (cl_lock_descr_match(&scan->cill_descr, need))
350 EXPORT_SYMBOL(cl_queue_match);
352 static int cl_queue_merge(const cfs_list_t *queue,
353 const struct cl_lock_descr *need)
355 struct cl_io_lock_link *scan;
358 cfs_list_for_each_entry(scan, queue, cill_linkage) {
359 if (cl_lock_descr_cmp(&scan->cill_descr, need))
361 cl_lock_descr_merge(&scan->cill_descr, need);
362 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
363 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
364 scan->cill_descr.cld_end);
371 static int cl_lockset_match(const struct cl_lockset *set,
372 const struct cl_lock_descr *need)
374 return cl_queue_match(&set->cls_curr, need) ||
375 cl_queue_match(&set->cls_done, need);
378 static int cl_lockset_merge(const struct cl_lockset *set,
379 const struct cl_lock_descr *need)
381 return cl_queue_merge(&set->cls_todo, need) ||
382 cl_lockset_match(set, need);
385 static int cl_lockset_lock_one(const struct lu_env *env,
386 struct cl_io *io, struct cl_lockset *set,
387 struct cl_io_lock_link *link)
389 struct cl_lock *lock;
394 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
397 link->cill_lock = lock;
398 cfs_list_move(&link->cill_linkage, &set->cls_curr);
399 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
400 result = cl_wait(env, lock);
402 cfs_list_move(&link->cill_linkage,
407 result = PTR_ERR(lock);
411 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
412 struct cl_io_lock_link *link)
414 struct cl_lock *lock = link->cill_lock;
417 cfs_list_del_init(&link->cill_linkage);
419 cl_lock_release(env, lock, "io", io);
420 link->cill_lock = NULL;
422 if (link->cill_fini != NULL)
423 link->cill_fini(env, link);
427 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
428 struct cl_lockset *set)
430 struct cl_io_lock_link *link;
431 struct cl_io_lock_link *temp;
432 struct cl_lock *lock;
437 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
438 if (!cl_lockset_match(set, &link->cill_descr)) {
439 /* XXX some locking to guarantee that locks aren't
440 * expanded in between. */
441 result = cl_lockset_lock_one(env, io, set, link);
445 cl_lock_link_fini(env, io, link);
448 cfs_list_for_each_entry_safe(link, temp,
449 &set->cls_curr, cill_linkage) {
450 lock = link->cill_lock;
451 result = cl_wait(env, lock);
453 cfs_list_move(&link->cill_linkage,
463 * Takes locks necessary for the current iteration of io.
465 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
466 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
469 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
471 const struct cl_io_slice *scan;
474 LINVRNT(cl_io_is_loopable(io));
475 LINVRNT(io->ci_state == CIS_IT_STARTED);
476 LINVRNT(cl_io_invariant(io));
479 cl_io_for_each(scan, io) {
480 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
482 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
487 cl_io_locks_sort(io);
488 result = cl_lockset_lock(env, io, &io->ci_lockset);
491 cl_io_unlock(env, io);
493 io->ci_state = CIS_LOCKED;
496 EXPORT_SYMBOL(cl_io_lock);
499 * Release locks takes by io.
501 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
503 struct cl_lockset *set;
504 struct cl_io_lock_link *link;
505 struct cl_io_lock_link *temp;
506 const struct cl_io_slice *scan;
508 LASSERT(cl_io_is_loopable(io));
509 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
510 LINVRNT(cl_io_invariant(io));
513 set = &io->ci_lockset;
515 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
516 cl_lock_link_fini(env, io, link);
518 cfs_list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
519 cl_lock_link_fini(env, io, link);
521 cfs_list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
522 cl_unuse(env, link->cill_lock);
523 cl_lock_link_fini(env, io, link);
525 cl_io_for_each_reverse(scan, io) {
526 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
527 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
529 io->ci_state = CIS_UNLOCKED;
530 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
533 EXPORT_SYMBOL(cl_io_unlock);
536 * Prepares next iteration of io.
538 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
539 * layers a chance to modify io parameters, e.g., so that lov can restrict io
540 * to a single stripe.
542 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
544 const struct cl_io_slice *scan;
547 LINVRNT(cl_io_is_loopable(io));
548 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
549 LINVRNT(cl_io_invariant(io));
553 cl_io_for_each(scan, io) {
554 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
556 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
562 io->ci_state = CIS_IT_STARTED;
565 EXPORT_SYMBOL(cl_io_iter_init);
568 * Finalizes io iteration.
570 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
572 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
574 const struct cl_io_slice *scan;
576 LINVRNT(cl_io_is_loopable(io));
577 LINVRNT(io->ci_state == CIS_UNLOCKED);
578 LINVRNT(cl_io_invariant(io));
581 cl_io_for_each_reverse(scan, io) {
582 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
583 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
585 io->ci_state = CIS_IT_ENDED;
588 EXPORT_SYMBOL(cl_io_iter_fini);
591 * Records that read or write io progressed \a nob bytes forward.
593 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
595 const struct cl_io_slice *scan;
597 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
599 LINVRNT(cl_io_is_loopable(io));
600 LINVRNT(cl_io_invariant(io));
604 io->u.ci_rw.crw_pos += nob;
605 io->u.ci_rw.crw_count -= nob;
607 /* layers have to be notified. */
608 cl_io_for_each_reverse(scan, io) {
609 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
610 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
615 EXPORT_SYMBOL(cl_io_rw_advance);
618 * Adds a lock to a lockset.
620 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
621 struct cl_io_lock_link *link)
626 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
629 cfs_list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
634 EXPORT_SYMBOL(cl_io_lock_add);
636 static void cl_free_io_lock_link(const struct lu_env *env,
637 struct cl_io_lock_link *link)
643 * Allocates new lock link, and uses it to add a lock to a lockset.
645 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
646 struct cl_lock_descr *descr)
648 struct cl_io_lock_link *link;
654 link->cill_descr = *descr;
655 link->cill_fini = cl_free_io_lock_link;
656 result = cl_io_lock_add(env, io, link);
657 if (result) /* lock match */
658 link->cill_fini(env, link);
664 EXPORT_SYMBOL(cl_io_lock_alloc_add);
667 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
669 int cl_io_start(const struct lu_env *env, struct cl_io *io)
671 const struct cl_io_slice *scan;
674 LINVRNT(cl_io_is_loopable(io));
675 LINVRNT(io->ci_state == CIS_LOCKED);
676 LINVRNT(cl_io_invariant(io));
679 io->ci_state = CIS_IO_GOING;
680 cl_io_for_each(scan, io) {
681 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
683 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
691 EXPORT_SYMBOL(cl_io_start);
694 * Wait until current io iteration is finished by calling
695 * cl_io_operations::cio_end() bottom-to-top.
697 void cl_io_end(const struct lu_env *env, struct cl_io *io)
699 const struct cl_io_slice *scan;
701 LINVRNT(cl_io_is_loopable(io));
702 LINVRNT(io->ci_state == CIS_IO_GOING);
703 LINVRNT(cl_io_invariant(io));
706 cl_io_for_each_reverse(scan, io) {
707 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
708 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
709 /* TODO: error handling. */
711 io->ci_state = CIS_IO_FINISHED;
714 EXPORT_SYMBOL(cl_io_end);
716 static const struct cl_page_slice *
717 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
719 const struct cl_page_slice *slice;
721 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
722 LINVRNT(slice != NULL);
727 * Called by read io, when page has to be read from the server.
729 * \see cl_io_operations::cio_read_page()
731 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
732 struct cl_page *page)
734 const struct cl_io_slice *scan;
735 struct cl_2queue *queue;
738 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
739 LINVRNT(cl_page_is_owned(page, io));
740 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
741 LINVRNT(cl_io_invariant(io));
744 queue = &io->ci_queue;
746 cl_2queue_init(queue);
748 * ->cio_read_page() methods called in the loop below are supposed to
749 * never block waiting for network (the only subtle point is the
750 * creation of new pages for read-ahead that might result in cache
751 * shrinking, but currently only clean pages are shrunk and this
752 * requires no network io).
754 * Should this ever starts blocking, retry loop would be needed for
755 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
757 cl_io_for_each(scan, io) {
758 if (scan->cis_iop->cio_read_page != NULL) {
759 const struct cl_page_slice *slice;
761 slice = cl_io_slice_page(scan, page);
762 LINVRNT(slice != NULL);
763 result = scan->cis_iop->cio_read_page(env, scan, slice);
769 result = cl_io_submit_rw(env, io, CRT_READ, queue);
771 * Unlock unsent pages in case of error.
773 cl_page_list_disown(env, io, &queue->c2_qin);
774 cl_2queue_fini(env, queue);
777 EXPORT_SYMBOL(cl_io_read_page);
780 * Commit a list of contiguous pages into writeback cache.
782 * \returns 0 if all pages committed, or errcode if error occurred.
783 * \see cl_io_operations::cio_commit_async()
785 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
786 struct cl_page_list *queue, int from, int to,
789 const struct cl_io_slice *scan;
793 cl_io_for_each(scan, io) {
794 if (scan->cis_iop->cio_commit_async == NULL)
796 result = scan->cis_iop->cio_commit_async(env, scan, queue,
803 EXPORT_SYMBOL(cl_io_commit_async);
806 * Submits a list of pages for immediate io.
808 * After the function gets returned, The submitted pages are moved to
809 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
810 * to be submitted, and the pages are errant to submit.
812 * \returns 0 if at least one page was submitted, error code otherwise.
813 * \see cl_io_operations::cio_submit()
815 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
816 enum cl_req_type crt, struct cl_2queue *queue)
818 const struct cl_io_slice *scan;
822 cl_io_for_each(scan, io) {
823 if (scan->cis_iop->cio_submit == NULL)
825 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
830 * If ->cio_submit() failed, no pages were sent.
832 LASSERT(ergo(result != 0, cfs_list_empty(&queue->c2_qout.pl_pages)));
835 EXPORT_SYMBOL(cl_io_submit_rw);
838 * Submit a sync_io and wait for the IO to be finished, or error happens.
839 * If \a timeout is zero, it means to wait for the IO unconditionally.
841 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
842 enum cl_req_type iot, struct cl_2queue *queue,
845 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
849 cl_page_list_for_each(pg, &queue->c2_qin) {
850 LASSERT(pg->cp_sync_io == NULL);
851 pg->cp_sync_io = anchor;
854 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
855 rc = cl_io_submit_rw(env, io, iot, queue);
858 * If some pages weren't sent for any reason (e.g.,
859 * read found up-to-date pages in the cache, or write found
860 * clean pages), count them as completed to avoid infinite
863 cl_page_list_for_each(pg, &queue->c2_qin) {
864 pg->cp_sync_io = NULL;
865 cl_sync_io_note(anchor, +1);
868 /* wait for the IO to be finished. */
869 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
872 LASSERT(cfs_list_empty(&queue->c2_qout.pl_pages));
873 cl_page_list_for_each(pg, &queue->c2_qin)
874 pg->cp_sync_io = NULL;
878 EXPORT_SYMBOL(cl_io_submit_sync);
881 * Cancel an IO which has been submitted by cl_io_submit_rw.
883 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
884 struct cl_page_list *queue)
886 struct cl_page *page;
889 CERROR("Canceling ongoing page trasmission\n");
890 cl_page_list_for_each(page, queue) {
893 rc = cl_page_cancel(env, page);
894 result = result ?: rc;
898 EXPORT_SYMBOL(cl_io_cancel);
903 * Pumps io through iterations calling
905 * - cl_io_iter_init()
915 * - cl_io_iter_fini()
917 * repeatedly until there is no more io to do.
919 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
923 LINVRNT(cl_io_is_loopable(io));
930 result = cl_io_iter_init(env, io);
933 result = cl_io_lock(env, io);
936 * Notify layers that locks has been taken,
939 * - llite: kms, short read;
940 * - llite: generic_file_read();
942 result = cl_io_start(env, io);
944 * Send any remaining pending
947 * - llite: ll_rw_stats_tally.
950 cl_io_unlock(env, io);
951 cl_io_rw_advance(env, io, io->ci_nob - nob);
954 cl_io_iter_fini(env, io);
955 } while (result == 0 && io->ci_continue);
957 result = io->ci_result;
958 RETURN(result < 0 ? result : 0);
960 EXPORT_SYMBOL(cl_io_loop);
963 * Adds io slice to the cl_io.
965 * This is called by cl_object_operations::coo_io_init() methods to add a
966 * per-layer state to the io. New state is added at the end of
967 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
969 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
971 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
972 struct cl_object *obj,
973 const struct cl_io_operations *ops)
975 cfs_list_t *linkage = &slice->cis_linkage;
977 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
978 cfs_list_empty(linkage));
981 cfs_list_add_tail(linkage, &io->ci_layers);
983 slice->cis_obj = obj;
984 slice->cis_iop = ops;
987 EXPORT_SYMBOL(cl_io_slice_add);
991 * Initializes page list.
993 void cl_page_list_init(struct cl_page_list *plist)
997 CFS_INIT_LIST_HEAD(&plist->pl_pages);
998 plist->pl_owner = current;
1001 EXPORT_SYMBOL(cl_page_list_init);
1004 * Adds a page to a page list.
1006 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1009 /* it would be better to check that page is owned by "current" io, but
1010 * it is not passed here. */
1011 LASSERT(page->cp_owner != NULL);
1012 LINVRNT(plist->pl_owner == current);
1015 mutex_lock(&page->cp_mutex);
1017 LASSERT(cfs_list_empty(&page->cp_batch));
1018 cfs_list_add_tail(&page->cp_batch, &plist->pl_pages);
1020 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1024 EXPORT_SYMBOL(cl_page_list_add);
1027 * Removes a page from a page list.
1029 void cl_page_list_del(const struct lu_env *env,
1030 struct cl_page_list *plist, struct cl_page *page)
1032 LASSERT(plist->pl_nr > 0);
1033 LINVRNT(plist->pl_owner == current);
1036 cfs_list_del_init(&page->cp_batch);
1038 mutex_unlock(&page->cp_mutex);
1041 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1042 cl_page_put(env, page);
1045 EXPORT_SYMBOL(cl_page_list_del);
1048 * Moves a page from one page list to another.
1050 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1051 struct cl_page *page)
1053 LASSERT(src->pl_nr > 0);
1054 LINVRNT(dst->pl_owner == current);
1055 LINVRNT(src->pl_owner == current);
1058 cfs_list_move_tail(&page->cp_batch, &dst->pl_pages);
1061 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1065 EXPORT_SYMBOL(cl_page_list_move);
1068 * Moves a page from one page list to the head of another list.
1070 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
1071 struct cl_page *page)
1073 LASSERT(src->pl_nr > 0);
1074 LINVRNT(dst->pl_owner == current);
1075 LINVRNT(src->pl_owner == current);
1078 cfs_list_move(&page->cp_batch, &dst->pl_pages);
1081 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1085 EXPORT_SYMBOL(cl_page_list_move_head);
1088 * splice the cl_page_list, just as list head does
1090 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1092 struct cl_page *page;
1093 struct cl_page *tmp;
1095 LINVRNT(list->pl_owner == current);
1096 LINVRNT(head->pl_owner == current);
1099 cl_page_list_for_each_safe(page, tmp, list)
1100 cl_page_list_move(head, list, page);
1103 EXPORT_SYMBOL(cl_page_list_splice);
1105 void cl_page_disown0(const struct lu_env *env,
1106 struct cl_io *io, struct cl_page *pg);
1109 * Disowns pages in a queue.
1111 void cl_page_list_disown(const struct lu_env *env,
1112 struct cl_io *io, struct cl_page_list *plist)
1114 struct cl_page *page;
1115 struct cl_page *temp;
1117 LINVRNT(plist->pl_owner == current);
1120 cl_page_list_for_each_safe(page, temp, plist) {
1121 LASSERT(plist->pl_nr > 0);
1123 cfs_list_del_init(&page->cp_batch);
1125 mutex_unlock(&page->cp_mutex);
1129 * cl_page_disown0 rather than usual cl_page_disown() is used,
1130 * because pages are possibly in CPS_FREEING state already due
1131 * to the call to cl_page_list_discard().
1134 * XXX cl_page_disown0() will fail if page is not locked.
1136 cl_page_disown0(env, io, page);
1137 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1139 cl_page_put(env, page);
1143 EXPORT_SYMBOL(cl_page_list_disown);
1146 * Releases pages from queue.
1148 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1150 struct cl_page *page;
1151 struct cl_page *temp;
1153 LINVRNT(plist->pl_owner == current);
1156 cl_page_list_for_each_safe(page, temp, plist)
1157 cl_page_list_del(env, plist, page);
1158 LASSERT(plist->pl_nr == 0);
1161 EXPORT_SYMBOL(cl_page_list_fini);
1164 * Owns all pages in a queue.
1166 int cl_page_list_own(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;
1173 LINVRNT(plist->pl_owner == current);
1177 cl_page_list_for_each_safe(page, temp, plist) {
1178 if (cl_page_own(env, io, page) == 0)
1179 result = result ?: page->cp_error;
1181 cl_page_list_del(env, plist, page);
1185 EXPORT_SYMBOL(cl_page_list_own);
1188 * Assumes all pages in a queue.
1190 void cl_page_list_assume(const struct lu_env *env,
1191 struct cl_io *io, struct cl_page_list *plist)
1193 struct cl_page *page;
1195 LINVRNT(plist->pl_owner == current);
1197 cl_page_list_for_each(page, plist)
1198 cl_page_assume(env, io, page);
1200 EXPORT_SYMBOL(cl_page_list_assume);
1203 * Discards all pages in a queue.
1205 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1206 struct cl_page_list *plist)
1208 struct cl_page *page;
1210 LINVRNT(plist->pl_owner == current);
1212 cl_page_list_for_each(page, plist)
1213 cl_page_discard(env, io, page);
1216 EXPORT_SYMBOL(cl_page_list_discard);
1219 * Initialize dual page queue.
1221 void cl_2queue_init(struct cl_2queue *queue)
1224 cl_page_list_init(&queue->c2_qin);
1225 cl_page_list_init(&queue->c2_qout);
1228 EXPORT_SYMBOL(cl_2queue_init);
1231 * Add a page to the incoming page list of 2-queue.
1233 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1236 cl_page_list_add(&queue->c2_qin, page);
1239 EXPORT_SYMBOL(cl_2queue_add);
1242 * Disown pages in both lists of a 2-queue.
1244 void cl_2queue_disown(const struct lu_env *env,
1245 struct cl_io *io, struct cl_2queue *queue)
1248 cl_page_list_disown(env, io, &queue->c2_qin);
1249 cl_page_list_disown(env, io, &queue->c2_qout);
1252 EXPORT_SYMBOL(cl_2queue_disown);
1255 * Discard (truncate) pages in both lists of a 2-queue.
1257 void cl_2queue_discard(const struct lu_env *env,
1258 struct cl_io *io, struct cl_2queue *queue)
1261 cl_page_list_discard(env, io, &queue->c2_qin);
1262 cl_page_list_discard(env, io, &queue->c2_qout);
1265 EXPORT_SYMBOL(cl_2queue_discard);
1268 * Assume to own the pages in cl_2queue
1270 void cl_2queue_assume(const struct lu_env *env,
1271 struct cl_io *io, struct cl_2queue *queue)
1273 cl_page_list_assume(env, io, &queue->c2_qin);
1274 cl_page_list_assume(env, io, &queue->c2_qout);
1276 EXPORT_SYMBOL(cl_2queue_assume);
1279 * Finalize both page lists of a 2-queue.
1281 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1284 cl_page_list_fini(env, &queue->c2_qout);
1285 cl_page_list_fini(env, &queue->c2_qin);
1288 EXPORT_SYMBOL(cl_2queue_fini);
1291 * Initialize a 2-queue to contain \a page in its incoming page list.
1293 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1296 cl_2queue_init(queue);
1297 cl_2queue_add(queue, page);
1300 EXPORT_SYMBOL(cl_2queue_init_page);
1303 * Returns top-level io.
1305 * \see cl_object_top()
1307 struct cl_io *cl_io_top(struct cl_io *io)
1310 while (io->ci_parent != NULL)
1314 EXPORT_SYMBOL(cl_io_top);
1317 * Prints human readable representation of \a io to the \a f.
1319 void cl_io_print(const struct lu_env *env, void *cookie,
1320 lu_printer_t printer, const struct cl_io *io)
1325 * Adds request slice to the compound request.
1327 * This is called by cl_device_operations::cdo_req_init() methods to add a
1328 * per-layer state to the request. New state is added at the end of
1329 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1331 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1333 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1334 struct cl_device *dev,
1335 const struct cl_req_operations *ops)
1338 cfs_list_add_tail(&slice->crs_linkage, &req->crq_layers);
1339 slice->crs_dev = dev;
1340 slice->crs_ops = ops;
1341 slice->crs_req = req;
1344 EXPORT_SYMBOL(cl_req_slice_add);
1346 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1350 LASSERT(cfs_list_empty(&req->crq_pages));
1351 LASSERT(req->crq_nrpages == 0);
1352 LINVRNT(cfs_list_empty(&req->crq_layers));
1353 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1356 if (req->crq_o != NULL) {
1357 for (i = 0; i < req->crq_nrobjs; ++i) {
1358 struct cl_object *obj = req->crq_o[i].ro_obj;
1360 lu_object_ref_del_at(&obj->co_lu,
1361 &req->crq_o[i].ro_obj_ref,
1363 cl_object_put(env, obj);
1366 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1372 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1373 struct cl_page *page)
1375 struct cl_device *dev;
1376 struct cl_page_slice *slice;
1381 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1382 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1383 if (dev->cd_ops->cdo_req_init != NULL) {
1384 result = dev->cd_ops->cdo_req_init(env,
1394 * Invokes per-request transfer completion call-backs
1395 * (cl_req_operations::cro_completion()) bottom-to-top.
1397 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1399 struct cl_req_slice *slice;
1403 * for the lack of list_for_each_entry_reverse_safe()...
1405 while (!cfs_list_empty(&req->crq_layers)) {
1406 slice = cfs_list_entry(req->crq_layers.prev,
1407 struct cl_req_slice, crs_linkage);
1408 cfs_list_del_init(&slice->crs_linkage);
1409 if (slice->crs_ops->cro_completion != NULL)
1410 slice->crs_ops->cro_completion(env, slice, rc);
1412 cl_req_free(env, req);
1415 EXPORT_SYMBOL(cl_req_completion);
1418 * Allocates new transfer request.
1420 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1421 enum cl_req_type crt, int nr_objects)
1425 LINVRNT(nr_objects > 0);
1432 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1433 if (req->crq_o != NULL) {
1434 req->crq_nrobjs = nr_objects;
1435 req->crq_type = crt;
1436 CFS_INIT_LIST_HEAD(&req->crq_pages);
1437 CFS_INIT_LIST_HEAD(&req->crq_layers);
1438 result = cl_req_init(env, req, page);
1442 cl_req_completion(env, req, result);
1443 req = ERR_PTR(result);
1446 req = ERR_PTR(-ENOMEM);
1449 EXPORT_SYMBOL(cl_req_alloc);
1452 * Adds a page to a request.
1454 void cl_req_page_add(const struct lu_env *env,
1455 struct cl_req *req, struct cl_page *page)
1457 struct cl_object *obj;
1458 struct cl_req_obj *rqo;
1463 LASSERT(cfs_list_empty(&page->cp_flight));
1464 LASSERT(page->cp_req == NULL);
1466 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1467 req, req->crq_type, req->crq_nrpages);
1469 cfs_list_add_tail(&page->cp_flight, &req->crq_pages);
1472 obj = cl_object_top(page->cp_obj);
1473 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1474 if (rqo->ro_obj == NULL) {
1477 lu_object_ref_add_at(&obj->co_lu, &rqo->ro_obj_ref,
1482 LASSERT(i < req->crq_nrobjs);
1485 EXPORT_SYMBOL(cl_req_page_add);
1488 * Removes a page from a request.
1490 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1492 struct cl_req *req = page->cp_req;
1496 LASSERT(!cfs_list_empty(&page->cp_flight));
1497 LASSERT(req->crq_nrpages > 0);
1499 cfs_list_del_init(&page->cp_flight);
1501 page->cp_req = NULL;
1504 EXPORT_SYMBOL(cl_req_page_done);
1507 * Notifies layers that request is about to depart by calling
1508 * cl_req_operations::cro_prep() top-to-bottom.
1510 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1514 const struct cl_req_slice *slice;
1518 * Check that the caller of cl_req_alloc() didn't lie about the number
1521 for (i = 0; i < req->crq_nrobjs; ++i)
1522 LASSERT(req->crq_o[i].ro_obj != NULL);
1525 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1526 if (slice->crs_ops->cro_prep != NULL) {
1527 result = slice->crs_ops->cro_prep(env, slice);
1534 EXPORT_SYMBOL(cl_req_prep);
1537 * Fills in attributes that are passed to server together with transfer. Only
1538 * attributes from \a flags may be touched. This can be called multiple times
1539 * for the same request.
1541 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1542 struct cl_req_attr *attr, obd_valid flags)
1544 const struct cl_req_slice *slice;
1545 struct cl_page *page;
1548 LASSERT(!cfs_list_empty(&req->crq_pages));
1551 /* Take any page to use as a model. */
1552 page = cfs_list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1554 for (i = 0; i < req->crq_nrobjs; ++i) {
1555 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1556 const struct cl_page_slice *scan;
1557 const struct cl_object *obj;
1559 scan = cl_page_at(page,
1560 slice->crs_dev->cd_lu_dev.ld_type);
1561 LASSERT(scan != NULL);
1562 obj = scan->cpl_obj;
1563 if (slice->crs_ops->cro_attr_set != NULL)
1564 slice->crs_ops->cro_attr_set(env, slice, obj,
1570 EXPORT_SYMBOL(cl_req_attr_set);
1572 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1573 * implemented in libcfs. */
1575 # include <linux/sched.h>
1576 #else /* __KERNEL__ */
1577 # include <liblustre.h>
1581 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1583 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1586 init_waitqueue_head(&anchor->csi_waitq);
1587 cfs_atomic_set(&anchor->csi_sync_nr, nrpages);
1588 cfs_atomic_set(&anchor->csi_barrier, nrpages > 0);
1589 anchor->csi_sync_rc = 0;
1592 EXPORT_SYMBOL(cl_sync_io_init);
1595 * Wait until all transfer completes. Transfer completion routine has to call
1596 * cl_sync_io_note() for every page.
1598 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1599 struct cl_page_list *queue, struct cl_sync_io *anchor,
1602 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1607 LASSERT(timeout >= 0);
1609 rc = l_wait_event(anchor->csi_waitq,
1610 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1613 CERROR("SYNC IO failed with error: %d, try to cancel "
1614 "%d remaining pages\n",
1615 rc, cfs_atomic_read(&anchor->csi_sync_nr));
1617 (void)cl_io_cancel(env, io, queue);
1619 lwi = (struct l_wait_info) { 0 };
1620 (void)l_wait_event(anchor->csi_waitq,
1621 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1624 rc = anchor->csi_sync_rc;
1626 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) == 0);
1627 cl_page_list_assume(env, io, queue);
1629 /* wait until cl_sync_io_note() has done wakeup */
1630 while (unlikely(cfs_atomic_read(&anchor->csi_barrier) != 0)) {
1636 POISON(anchor, 0x5a, sizeof *anchor);
1639 EXPORT_SYMBOL(cl_sync_io_wait);
1642 * Indicate that transfer of a single page completed.
1644 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1647 if (anchor->csi_sync_rc == 0 && ioret < 0)
1648 anchor->csi_sync_rc = ioret;
1650 * Synchronous IO done without releasing page lock (e.g., as a part of
1651 * ->{prepare,commit}_write(). Completion is used to signal the end of
1654 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) > 0);
1655 if (cfs_atomic_dec_and_test(&anchor->csi_sync_nr)) {
1656 wake_up_all(&anchor->csi_waitq);
1657 /* it's safe to nuke or reuse anchor now */
1658 cfs_atomic_set(&anchor->csi_barrier, 0);
1662 EXPORT_SYMBOL(cl_sync_io_note);