4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 #include "../include/obd_class.h"
44 #include "../include/obd_support.h"
45 #include "../include/lustre_fid.h"
46 #include <linux/list.h>
47 #include "../include/cl_object.h"
48 #include "cl_internal.h"
50 /*****************************************************************************
56 #define cl_io_for_each(slice, io) \
57 list_for_each_entry((slice), &io->ci_layers, cis_linkage)
58 #define cl_io_for_each_reverse(slice, io) \
59 list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
61 static inline int cl_io_type_is_valid(enum cl_io_type type)
63 return CIT_READ <= type && type < CIT_OP_NR;
66 static inline int cl_io_is_loopable(const struct cl_io *io)
68 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
72 * Returns true iff there is an IO ongoing in the given environment.
74 int cl_io_is_going(const struct lu_env *env)
76 return cl_env_info(env)->clt_current_io != NULL;
78 EXPORT_SYMBOL(cl_io_is_going);
81 * cl_io invariant that holds at all times when exported cl_io_*() functions
82 * are entered and left.
84 static int cl_io_invariant(const struct cl_io *io)
91 * io can own pages only when it is ongoing. Sub-io might
92 * still be in CIS_LOCKED state when top-io is in
95 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
96 (io->ci_state == CIS_LOCKED && up != NULL));
100 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
102 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
104 struct cl_io_slice *slice;
105 struct cl_thread_info *info;
107 LINVRNT(cl_io_type_is_valid(io->ci_type));
108 LINVRNT(cl_io_invariant(io));
110 while (!list_empty(&io->ci_layers)) {
111 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
113 list_del_init(&slice->cis_linkage);
114 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
115 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
117 * Invalidate slice to catch use after free. This assumes that
118 * slices are allocated within session and can be touched
119 * after ->cio_fini() returns.
121 slice->cis_io = NULL;
123 io->ci_state = CIS_FINI;
124 info = cl_env_info(env);
125 if (info->clt_current_io == io)
126 info->clt_current_io = NULL;
128 /* sanity check for layout change */
129 switch(io->ci_type) {
135 LASSERT(!io->ci_need_restart);
139 /* Check ignore layout change conf */
140 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
141 !io->ci_need_restart));
147 EXPORT_SYMBOL(cl_io_fini);
149 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
150 enum cl_io_type iot, struct cl_object *obj)
152 struct cl_object *scan;
155 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
156 LINVRNT(cl_io_type_is_valid(iot));
157 LINVRNT(cl_io_invariant(io));
160 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
161 INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
162 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
163 INIT_LIST_HEAD(&io->ci_layers);
166 cl_object_for_each(scan, obj) {
167 if (scan->co_ops->coo_io_init != NULL) {
168 result = scan->co_ops->coo_io_init(env, scan, io);
174 io->ci_state = CIS_INIT;
179 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
181 * \pre obj != cl_object_top(obj)
183 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
184 enum cl_io_type iot, struct cl_object *obj)
186 struct cl_thread_info *info = cl_env_info(env);
188 LASSERT(obj != cl_object_top(obj));
189 if (info->clt_current_io == NULL)
190 info->clt_current_io = io;
191 return cl_io_init0(env, io, iot, obj);
193 EXPORT_SYMBOL(cl_io_sub_init);
196 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
198 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
199 * what the latter returned.
201 * \pre obj == cl_object_top(obj)
202 * \pre cl_io_type_is_valid(iot)
203 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
205 int cl_io_init(const struct lu_env *env, struct cl_io *io,
206 enum cl_io_type iot, struct cl_object *obj)
208 struct cl_thread_info *info = cl_env_info(env);
210 LASSERT(obj == cl_object_top(obj));
211 LASSERT(info->clt_current_io == NULL);
213 info->clt_current_io = io;
214 return cl_io_init0(env, io, iot, obj);
216 EXPORT_SYMBOL(cl_io_init);
219 * Initialize read or write io.
221 * \pre iot == CIT_READ || iot == CIT_WRITE
223 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
224 enum cl_io_type iot, loff_t pos, size_t count)
226 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
227 LINVRNT(io->ci_obj != NULL);
229 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
230 "io range: %u [%llu, %llu) %u %u\n",
231 iot, (__u64)pos, (__u64)pos + count,
232 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
233 io->u.ci_rw.crw_pos = pos;
234 io->u.ci_rw.crw_count = count;
235 return cl_io_init(env, io, iot, io->ci_obj);
237 EXPORT_SYMBOL(cl_io_rw_init);
239 static inline const struct lu_fid *
240 cl_lock_descr_fid(const struct cl_lock_descr *descr)
242 return lu_object_fid(&descr->cld_obj->co_lu);
245 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
246 const struct cl_lock_descr *d1)
248 return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
249 __diff_normalize(d0->cld_start, d1->cld_start);
252 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
253 const struct cl_lock_descr *d1)
257 ret = lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1));
260 if (d0->cld_end < d1->cld_start)
262 if (d0->cld_start > d0->cld_end)
267 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
268 const struct cl_lock_descr *d1)
270 d0->cld_start = min(d0->cld_start, d1->cld_start);
271 d0->cld_end = max(d0->cld_end, d1->cld_end);
273 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
274 d0->cld_mode = CLM_WRITE;
276 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
277 d0->cld_mode = CLM_GROUP;
281 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
283 static void cl_io_locks_sort(struct cl_io *io)
287 /* hidden treasure: bubble sort for now. */
289 struct cl_io_lock_link *curr;
290 struct cl_io_lock_link *prev;
291 struct cl_io_lock_link *temp;
296 list_for_each_entry_safe(curr, temp,
297 &io->ci_lockset.cls_todo,
300 switch (cl_lock_descr_sort(&prev->cill_descr,
301 &curr->cill_descr)) {
304 * IMPOSSIBLE: Identical locks are
311 list_move_tail(&curr->cill_linkage,
312 &prev->cill_linkage);
314 continue; /* don't change prev: it's
315 * still "previous" */
316 case -1: /* already in order */
326 * Check whether \a queue contains locks matching \a need.
328 * \retval +ve there is a matching lock in the \a queue
329 * \retval 0 there are no matching locks in the \a queue
331 int cl_queue_match(const struct list_head *queue,
332 const struct cl_lock_descr *need)
334 struct cl_io_lock_link *scan;
336 list_for_each_entry(scan, queue, cill_linkage) {
337 if (cl_lock_descr_match(&scan->cill_descr, need))
342 EXPORT_SYMBOL(cl_queue_match);
344 static int cl_queue_merge(const struct list_head *queue,
345 const struct cl_lock_descr *need)
347 struct cl_io_lock_link *scan;
349 list_for_each_entry(scan, queue, cill_linkage) {
350 if (cl_lock_descr_cmp(&scan->cill_descr, need))
352 cl_lock_descr_merge(&scan->cill_descr, need);
353 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
354 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
355 scan->cill_descr.cld_end);
362 static int cl_lockset_match(const struct cl_lockset *set,
363 const struct cl_lock_descr *need)
365 return cl_queue_match(&set->cls_curr, need) ||
366 cl_queue_match(&set->cls_done, need);
369 static int cl_lockset_merge(const struct cl_lockset *set,
370 const struct cl_lock_descr *need)
372 return cl_queue_merge(&set->cls_todo, need) ||
373 cl_lockset_match(set, need);
376 static int cl_lockset_lock_one(const struct lu_env *env,
377 struct cl_io *io, struct cl_lockset *set,
378 struct cl_io_lock_link *link)
380 struct cl_lock *lock;
383 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
386 link->cill_lock = lock;
387 list_move(&link->cill_linkage, &set->cls_curr);
388 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
389 result = cl_wait(env, lock);
391 list_move(&link->cill_linkage,
396 result = PTR_ERR(lock);
400 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
401 struct cl_io_lock_link *link)
403 struct cl_lock *lock = link->cill_lock;
405 list_del_init(&link->cill_linkage);
407 cl_lock_release(env, lock, "io", io);
408 link->cill_lock = NULL;
410 if (link->cill_fini != NULL)
411 link->cill_fini(env, link);
414 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
415 struct cl_lockset *set)
417 struct cl_io_lock_link *link;
418 struct cl_io_lock_link *temp;
419 struct cl_lock *lock;
423 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
424 if (!cl_lockset_match(set, &link->cill_descr)) {
425 /* XXX some locking to guarantee that locks aren't
426 * expanded in between. */
427 result = cl_lockset_lock_one(env, io, set, link);
431 cl_lock_link_fini(env, io, link);
434 list_for_each_entry_safe(link, temp,
435 &set->cls_curr, cill_linkage) {
436 lock = link->cill_lock;
437 result = cl_wait(env, lock);
439 list_move(&link->cill_linkage,
449 * Takes locks necessary for the current iteration of io.
451 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
452 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
455 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
457 const struct cl_io_slice *scan;
460 LINVRNT(cl_io_is_loopable(io));
461 LINVRNT(io->ci_state == CIS_IT_STARTED);
462 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));
497 set = &io->ci_lockset;
499 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
500 cl_lock_link_fini(env, io, link);
502 list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
503 cl_lock_link_fini(env, io, link);
505 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
506 cl_unuse(env, link->cill_lock);
507 cl_lock_link_fini(env, io, link);
509 cl_io_for_each_reverse(scan, io) {
510 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
511 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
513 io->ci_state = CIS_UNLOCKED;
514 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
516 EXPORT_SYMBOL(cl_io_unlock);
519 * Prepares next iteration of io.
521 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
522 * layers a chance to modify io parameters, e.g., so that lov can restrict io
523 * to a single stripe.
525 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
527 const struct cl_io_slice *scan;
530 LINVRNT(cl_io_is_loopable(io));
531 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
532 LINVRNT(cl_io_invariant(io));
535 cl_io_for_each(scan, io) {
536 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
538 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
544 io->ci_state = CIS_IT_STARTED;
547 EXPORT_SYMBOL(cl_io_iter_init);
550 * Finalizes io iteration.
552 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
554 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
556 const struct cl_io_slice *scan;
558 LINVRNT(cl_io_is_loopable(io));
559 LINVRNT(io->ci_state == CIS_UNLOCKED);
560 LINVRNT(cl_io_invariant(io));
562 cl_io_for_each_reverse(scan, io) {
563 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
564 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
566 io->ci_state = CIS_IT_ENDED;
568 EXPORT_SYMBOL(cl_io_iter_fini);
571 * Records that read or write io progressed \a nob bytes forward.
573 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
575 const struct cl_io_slice *scan;
577 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
579 LINVRNT(cl_io_is_loopable(io));
580 LINVRNT(cl_io_invariant(io));
582 io->u.ci_rw.crw_pos += nob;
583 io->u.ci_rw.crw_count -= nob;
585 /* layers have to be notified. */
586 cl_io_for_each_reverse(scan, io) {
587 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
588 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
592 EXPORT_SYMBOL(cl_io_rw_advance);
595 * Adds a lock to a lockset.
597 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
598 struct cl_io_lock_link *link)
602 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
605 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
610 EXPORT_SYMBOL(cl_io_lock_add);
612 static void cl_free_io_lock_link(const struct lu_env *env,
613 struct cl_io_lock_link *link)
619 * Allocates new lock link, and uses it to add a lock to a lockset.
621 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
622 struct cl_lock_descr *descr)
624 struct cl_io_lock_link *link;
629 link->cill_descr = *descr;
630 link->cill_fini = cl_free_io_lock_link;
631 result = cl_io_lock_add(env, io, link);
632 if (result) /* lock match */
633 link->cill_fini(env, link);
639 EXPORT_SYMBOL(cl_io_lock_alloc_add);
642 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
644 int cl_io_start(const struct lu_env *env, struct cl_io *io)
646 const struct cl_io_slice *scan;
649 LINVRNT(cl_io_is_loopable(io));
650 LINVRNT(io->ci_state == CIS_LOCKED);
651 LINVRNT(cl_io_invariant(io));
653 io->ci_state = CIS_IO_GOING;
654 cl_io_for_each(scan, io) {
655 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
657 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
665 EXPORT_SYMBOL(cl_io_start);
668 * Wait until current io iteration is finished by calling
669 * cl_io_operations::cio_end() bottom-to-top.
671 void cl_io_end(const struct lu_env *env, struct cl_io *io)
673 const struct cl_io_slice *scan;
675 LINVRNT(cl_io_is_loopable(io));
676 LINVRNT(io->ci_state == CIS_IO_GOING);
677 LINVRNT(cl_io_invariant(io));
679 cl_io_for_each_reverse(scan, io) {
680 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
681 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
682 /* TODO: error handling. */
684 io->ci_state = CIS_IO_FINISHED;
686 EXPORT_SYMBOL(cl_io_end);
688 static const struct cl_page_slice *
689 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
691 const struct cl_page_slice *slice;
693 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
694 LINVRNT(slice != NULL);
699 * True iff \a page is within \a io range.
701 static int cl_page_in_io(const struct cl_page *page, const struct cl_io *io)
708 idx = page->cp_index;
709 switch (io->ci_type) {
713 * check that [start, end) and [pos, pos + count) extents
716 if (!cl_io_is_append(io)) {
717 const struct cl_io_rw_common *crw = &(io->u.ci_rw);
718 start = cl_offset(page->cp_obj, idx);
719 end = cl_offset(page->cp_obj, idx + 1);
720 result = crw->crw_pos < end &&
721 start < crw->crw_pos + crw->crw_count;
725 result = io->u.ci_fault.ft_index == idx;
734 * Called by read io, when page has to be read from the server.
736 * \see cl_io_operations::cio_read_page()
738 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
739 struct cl_page *page)
741 const struct cl_io_slice *scan;
742 struct cl_2queue *queue;
745 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
746 LINVRNT(cl_page_is_owned(page, io));
747 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
748 LINVRNT(cl_page_in_io(page, io));
749 LINVRNT(cl_io_invariant(io));
751 queue = &io->ci_queue;
753 cl_2queue_init(queue);
755 * ->cio_read_page() methods called in the loop below are supposed to
756 * never block waiting for network (the only subtle point is the
757 * creation of new pages for read-ahead that might result in cache
758 * shrinking, but currently only clean pages are shrunk and this
759 * requires no network io).
761 * Should this ever starts blocking, retry loop would be needed for
762 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
764 cl_io_for_each(scan, io) {
765 if (scan->cis_iop->cio_read_page != NULL) {
766 const struct cl_page_slice *slice;
768 slice = cl_io_slice_page(scan, page);
769 LINVRNT(slice != NULL);
770 result = scan->cis_iop->cio_read_page(env, scan, slice);
776 result = cl_io_submit_rw(env, io, CRT_READ, queue);
778 * Unlock unsent pages in case of error.
780 cl_page_list_disown(env, io, &queue->c2_qin);
781 cl_2queue_fini(env, queue);
784 EXPORT_SYMBOL(cl_io_read_page);
787 * Called by write io to prepare page to receive data from user buffer.
789 * \see cl_io_operations::cio_prepare_write()
791 int cl_io_prepare_write(const struct lu_env *env, struct cl_io *io,
792 struct cl_page *page, unsigned from, unsigned to)
794 const struct cl_io_slice *scan;
797 LINVRNT(io->ci_type == CIT_WRITE);
798 LINVRNT(cl_page_is_owned(page, io));
799 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
800 LINVRNT(cl_io_invariant(io));
801 LASSERT(cl_page_in_io(page, io));
803 cl_io_for_each_reverse(scan, io) {
804 if (scan->cis_iop->cio_prepare_write != NULL) {
805 const struct cl_page_slice *slice;
807 slice = cl_io_slice_page(scan, page);
808 result = scan->cis_iop->cio_prepare_write(env, scan,
817 EXPORT_SYMBOL(cl_io_prepare_write);
820 * Called by write io after user data were copied into a page.
822 * \see cl_io_operations::cio_commit_write()
824 int cl_io_commit_write(const struct lu_env *env, struct cl_io *io,
825 struct cl_page *page, unsigned from, unsigned to)
827 const struct cl_io_slice *scan;
830 LINVRNT(io->ci_type == CIT_WRITE);
831 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
832 LINVRNT(cl_io_invariant(io));
834 * XXX Uh... not nice. Top level cl_io_commit_write() call (vvp->lov)
835 * already called cl_page_cache_add(), moving page into CPS_CACHED
836 * state. Better (and more general) way of dealing with such situation
839 LASSERT(cl_page_is_owned(page, io) || page->cp_parent != NULL);
840 LASSERT(cl_page_in_io(page, io));
842 cl_io_for_each(scan, io) {
843 if (scan->cis_iop->cio_commit_write != NULL) {
844 const struct cl_page_slice *slice;
846 slice = cl_io_slice_page(scan, page);
847 result = scan->cis_iop->cio_commit_write(env, scan,
854 LINVRNT(result <= 0);
857 EXPORT_SYMBOL(cl_io_commit_write);
860 * Submits a list of pages for immediate io.
862 * After the function gets returned, The submitted pages are moved to
863 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
864 * to be submitted, and the pages are errant to submit.
866 * \returns 0 if at least one page was submitted, error code otherwise.
867 * \see cl_io_operations::cio_submit()
869 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
870 enum cl_req_type crt, struct cl_2queue *queue)
872 const struct cl_io_slice *scan;
875 LINVRNT(crt < ARRAY_SIZE(scan->cis_iop->req_op));
877 cl_io_for_each(scan, io) {
878 if (scan->cis_iop->req_op[crt].cio_submit == NULL)
880 result = scan->cis_iop->req_op[crt].cio_submit(env, scan, crt,
886 * If ->cio_submit() failed, no pages were sent.
888 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
891 EXPORT_SYMBOL(cl_io_submit_rw);
894 * Submit a sync_io and wait for the IO to be finished, or error happens.
895 * If \a timeout is zero, it means to wait for the IO unconditionally.
897 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
898 enum cl_req_type iot, struct cl_2queue *queue,
901 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
905 cl_page_list_for_each(pg, &queue->c2_qin) {
906 LASSERT(pg->cp_sync_io == NULL);
907 pg->cp_sync_io = anchor;
910 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
911 rc = cl_io_submit_rw(env, io, iot, queue);
914 * If some pages weren't sent for any reason (e.g.,
915 * read found up-to-date pages in the cache, or write found
916 * clean pages), count them as completed to avoid infinite
919 cl_page_list_for_each(pg, &queue->c2_qin) {
920 pg->cp_sync_io = NULL;
921 cl_sync_io_note(anchor, +1);
924 /* wait for the IO to be finished. */
925 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
928 LASSERT(list_empty(&queue->c2_qout.pl_pages));
929 cl_page_list_for_each(pg, &queue->c2_qin)
930 pg->cp_sync_io = NULL;
934 EXPORT_SYMBOL(cl_io_submit_sync);
937 * Cancel an IO which has been submitted by cl_io_submit_rw.
939 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
940 struct cl_page_list *queue)
942 struct cl_page *page;
945 CERROR("Canceling ongoing page transmission\n");
946 cl_page_list_for_each(page, queue) {
949 LINVRNT(cl_page_in_io(page, io));
950 rc = cl_page_cancel(env, page);
951 result = result ?: rc;
955 EXPORT_SYMBOL(cl_io_cancel);
960 * Pumps io through iterations calling
962 * - cl_io_iter_init()
972 * - cl_io_iter_fini()
974 * repeatedly until there is no more io to do.
976 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
980 LINVRNT(cl_io_is_loopable(io));
986 result = cl_io_iter_init(env, io);
989 result = cl_io_lock(env, io);
992 * Notify layers that locks has been taken,
995 * - llite: kms, short read;
996 * - llite: generic_file_read();
998 result = cl_io_start(env, io);
1000 * Send any remaining pending
1003 * - llite: ll_rw_stats_tally.
1006 cl_io_unlock(env, io);
1007 cl_io_rw_advance(env, io, io->ci_nob - nob);
1010 cl_io_iter_fini(env, io);
1011 } while (result == 0 && io->ci_continue);
1013 result = io->ci_result;
1014 return result < 0 ? result : 0;
1016 EXPORT_SYMBOL(cl_io_loop);
1019 * Adds io slice to the cl_io.
1021 * This is called by cl_object_operations::coo_io_init() methods to add a
1022 * per-layer state to the io. New state is added at the end of
1023 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
1025 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
1027 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
1028 struct cl_object *obj,
1029 const struct cl_io_operations *ops)
1031 struct list_head *linkage = &slice->cis_linkage;
1033 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
1034 list_empty(linkage));
1036 list_add_tail(linkage, &io->ci_layers);
1038 slice->cis_obj = obj;
1039 slice->cis_iop = ops;
1041 EXPORT_SYMBOL(cl_io_slice_add);
1045 * Initializes page list.
1047 void cl_page_list_init(struct cl_page_list *plist)
1050 INIT_LIST_HEAD(&plist->pl_pages);
1051 plist->pl_owner = current;
1053 EXPORT_SYMBOL(cl_page_list_init);
1056 * Adds a page to a page list.
1058 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1060 /* it would be better to check that page is owned by "current" io, but
1061 * it is not passed here. */
1062 LASSERT(page->cp_owner != NULL);
1063 LINVRNT(plist->pl_owner == current);
1066 mutex_lock(&page->cp_mutex);
1068 LASSERT(list_empty(&page->cp_batch));
1069 list_add_tail(&page->cp_batch, &plist->pl_pages);
1071 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1074 EXPORT_SYMBOL(cl_page_list_add);
1077 * Removes a page from a page list.
1079 void cl_page_list_del(const struct lu_env *env,
1080 struct cl_page_list *plist, struct cl_page *page)
1082 LASSERT(plist->pl_nr > 0);
1083 LINVRNT(plist->pl_owner == current);
1085 list_del_init(&page->cp_batch);
1087 mutex_unlock(&page->cp_mutex);
1090 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1091 cl_page_put(env, page);
1093 EXPORT_SYMBOL(cl_page_list_del);
1096 * Moves a page from one page list to another.
1098 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1099 struct cl_page *page)
1101 LASSERT(src->pl_nr > 0);
1102 LINVRNT(dst->pl_owner == current);
1103 LINVRNT(src->pl_owner == current);
1105 list_move_tail(&page->cp_batch, &dst->pl_pages);
1108 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1111 EXPORT_SYMBOL(cl_page_list_move);
1114 * splice the cl_page_list, just as list head does
1116 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1118 struct cl_page *page;
1119 struct cl_page *tmp;
1121 LINVRNT(list->pl_owner == current);
1122 LINVRNT(head->pl_owner == current);
1124 cl_page_list_for_each_safe(page, tmp, list)
1125 cl_page_list_move(head, list, page);
1127 EXPORT_SYMBOL(cl_page_list_splice);
1129 void cl_page_disown0(const struct lu_env *env,
1130 struct cl_io *io, struct cl_page *pg);
1133 * Disowns pages in a queue.
1135 void cl_page_list_disown(const struct lu_env *env,
1136 struct cl_io *io, struct cl_page_list *plist)
1138 struct cl_page *page;
1139 struct cl_page *temp;
1141 LINVRNT(plist->pl_owner == current);
1143 cl_page_list_for_each_safe(page, temp, plist) {
1144 LASSERT(plist->pl_nr > 0);
1146 list_del_init(&page->cp_batch);
1148 mutex_unlock(&page->cp_mutex);
1152 * cl_page_disown0 rather than usual cl_page_disown() is used,
1153 * because pages are possibly in CPS_FREEING state already due
1154 * to the call to cl_page_list_discard().
1157 * XXX cl_page_disown0() will fail if page is not locked.
1159 cl_page_disown0(env, io, page);
1160 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1162 cl_page_put(env, page);
1165 EXPORT_SYMBOL(cl_page_list_disown);
1168 * Releases pages from queue.
1170 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1172 struct cl_page *page;
1173 struct cl_page *temp;
1175 LINVRNT(plist->pl_owner == current);
1177 cl_page_list_for_each_safe(page, temp, plist)
1178 cl_page_list_del(env, plist, page);
1179 LASSERT(plist->pl_nr == 0);
1181 EXPORT_SYMBOL(cl_page_list_fini);
1184 * Owns all pages in a queue.
1186 int cl_page_list_own(const struct lu_env *env,
1187 struct cl_io *io, struct cl_page_list *plist)
1189 struct cl_page *page;
1190 struct cl_page *temp;
1194 LINVRNT(plist->pl_owner == current);
1197 cl_page_list_for_each_safe(page, temp, plist) {
1198 LASSERT(index <= page->cp_index);
1199 index = page->cp_index;
1200 if (cl_page_own(env, io, page) == 0)
1201 result = result ?: page->cp_error;
1203 cl_page_list_del(env, plist, page);
1207 EXPORT_SYMBOL(cl_page_list_own);
1210 * Assumes all pages in a queue.
1212 void cl_page_list_assume(const struct lu_env *env,
1213 struct cl_io *io, struct cl_page_list *plist)
1215 struct cl_page *page;
1217 LINVRNT(plist->pl_owner == current);
1219 cl_page_list_for_each(page, plist)
1220 cl_page_assume(env, io, page);
1222 EXPORT_SYMBOL(cl_page_list_assume);
1225 * Discards all pages in a queue.
1227 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1228 struct cl_page_list *plist)
1230 struct cl_page *page;
1232 LINVRNT(plist->pl_owner == current);
1233 cl_page_list_for_each(page, plist)
1234 cl_page_discard(env, io, page);
1236 EXPORT_SYMBOL(cl_page_list_discard);
1239 * Unmaps all pages in a queue from user virtual memory.
1241 int cl_page_list_unmap(const struct lu_env *env, struct cl_io *io,
1242 struct cl_page_list *plist)
1244 struct cl_page *page;
1247 LINVRNT(plist->pl_owner == current);
1249 cl_page_list_for_each(page, plist) {
1250 result = cl_page_unmap(env, io, page);
1256 EXPORT_SYMBOL(cl_page_list_unmap);
1259 * Initialize dual page queue.
1261 void cl_2queue_init(struct cl_2queue *queue)
1263 cl_page_list_init(&queue->c2_qin);
1264 cl_page_list_init(&queue->c2_qout);
1266 EXPORT_SYMBOL(cl_2queue_init);
1269 * Add a page to the incoming page list of 2-queue.
1271 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1273 cl_page_list_add(&queue->c2_qin, page);
1275 EXPORT_SYMBOL(cl_2queue_add);
1278 * Disown pages in both lists of a 2-queue.
1280 void cl_2queue_disown(const struct lu_env *env,
1281 struct cl_io *io, struct cl_2queue *queue)
1283 cl_page_list_disown(env, io, &queue->c2_qin);
1284 cl_page_list_disown(env, io, &queue->c2_qout);
1286 EXPORT_SYMBOL(cl_2queue_disown);
1289 * Discard (truncate) pages in both lists of a 2-queue.
1291 void cl_2queue_discard(const struct lu_env *env,
1292 struct cl_io *io, struct cl_2queue *queue)
1294 cl_page_list_discard(env, io, &queue->c2_qin);
1295 cl_page_list_discard(env, io, &queue->c2_qout);
1297 EXPORT_SYMBOL(cl_2queue_discard);
1300 * Assume to own the pages in cl_2queue
1302 void cl_2queue_assume(const struct lu_env *env,
1303 struct cl_io *io, struct cl_2queue *queue)
1305 cl_page_list_assume(env, io, &queue->c2_qin);
1306 cl_page_list_assume(env, io, &queue->c2_qout);
1308 EXPORT_SYMBOL(cl_2queue_assume);
1311 * Finalize both page lists of a 2-queue.
1313 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1315 cl_page_list_fini(env, &queue->c2_qout);
1316 cl_page_list_fini(env, &queue->c2_qin);
1318 EXPORT_SYMBOL(cl_2queue_fini);
1321 * Initialize a 2-queue to contain \a page in its incoming page list.
1323 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1325 cl_2queue_init(queue);
1326 cl_2queue_add(queue, page);
1328 EXPORT_SYMBOL(cl_2queue_init_page);
1331 * Returns top-level io.
1333 * \see cl_object_top(), cl_page_top().
1335 struct cl_io *cl_io_top(struct cl_io *io)
1337 while (io->ci_parent != NULL)
1341 EXPORT_SYMBOL(cl_io_top);
1344 * Prints human readable representation of \a io to the \a f.
1346 void cl_io_print(const struct lu_env *env, void *cookie,
1347 lu_printer_t printer, const struct cl_io *io)
1352 * Adds request slice to the compound request.
1354 * This is called by cl_device_operations::cdo_req_init() methods to add a
1355 * per-layer state to the request. New state is added at the end of
1356 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1358 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1360 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1361 struct cl_device *dev,
1362 const struct cl_req_operations *ops)
1364 list_add_tail(&slice->crs_linkage, &req->crq_layers);
1365 slice->crs_dev = dev;
1366 slice->crs_ops = ops;
1367 slice->crs_req = req;
1369 EXPORT_SYMBOL(cl_req_slice_add);
1371 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1375 LASSERT(list_empty(&req->crq_pages));
1376 LASSERT(req->crq_nrpages == 0);
1377 LINVRNT(list_empty(&req->crq_layers));
1378 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1380 if (req->crq_o != NULL) {
1381 for (i = 0; i < req->crq_nrobjs; ++i) {
1382 struct cl_object *obj = req->crq_o[i].ro_obj;
1384 lu_object_ref_del_at(&obj->co_lu,
1385 &req->crq_o[i].ro_obj_ref,
1387 cl_object_put(env, obj);
1390 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof(req->crq_o[0]));
1395 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1396 struct cl_page *page)
1398 struct cl_device *dev;
1399 struct cl_page_slice *slice;
1403 page = cl_page_top(page);
1405 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1406 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1407 if (dev->cd_ops->cdo_req_init != NULL) {
1408 result = dev->cd_ops->cdo_req_init(env,
1414 page = page->cp_child;
1415 } while (page != NULL && result == 0);
1420 * Invokes per-request transfer completion call-backs
1421 * (cl_req_operations::cro_completion()) bottom-to-top.
1423 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1425 struct cl_req_slice *slice;
1428 * for the lack of list_for_each_entry_reverse_safe()...
1430 while (!list_empty(&req->crq_layers)) {
1431 slice = list_entry(req->crq_layers.prev,
1432 struct cl_req_slice, crs_linkage);
1433 list_del_init(&slice->crs_linkage);
1434 if (slice->crs_ops->cro_completion != NULL)
1435 slice->crs_ops->cro_completion(env, slice, rc);
1437 cl_req_free(env, req);
1439 EXPORT_SYMBOL(cl_req_completion);
1442 * Allocates new transfer request.
1444 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1445 enum cl_req_type crt, int nr_objects)
1449 LINVRNT(nr_objects > 0);
1455 req->crq_type = crt;
1456 INIT_LIST_HEAD(&req->crq_pages);
1457 INIT_LIST_HEAD(&req->crq_layers);
1459 OBD_ALLOC(req->crq_o, nr_objects * sizeof(req->crq_o[0]));
1460 if (req->crq_o != NULL) {
1461 req->crq_nrobjs = nr_objects;
1462 result = cl_req_init(env, req, page);
1466 cl_req_completion(env, req, result);
1467 req = ERR_PTR(result);
1470 req = ERR_PTR(-ENOMEM);
1473 EXPORT_SYMBOL(cl_req_alloc);
1476 * Adds a page to a request.
1478 void cl_req_page_add(const struct lu_env *env,
1479 struct cl_req *req, struct cl_page *page)
1481 struct cl_object *obj;
1482 struct cl_req_obj *rqo;
1485 page = cl_page_top(page);
1487 LASSERT(list_empty(&page->cp_flight));
1488 LASSERT(page->cp_req == NULL);
1490 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1491 req, req->crq_type, req->crq_nrpages);
1493 list_add_tail(&page->cp_flight, &req->crq_pages);
1496 obj = cl_object_top(page->cp_obj);
1497 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1498 if (rqo->ro_obj == NULL) {
1501 lu_object_ref_add_at(&obj->co_lu, &rqo->ro_obj_ref,
1506 LASSERT(i < req->crq_nrobjs);
1508 EXPORT_SYMBOL(cl_req_page_add);
1511 * Removes a page from a request.
1513 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1515 struct cl_req *req = page->cp_req;
1517 page = cl_page_top(page);
1519 LASSERT(!list_empty(&page->cp_flight));
1520 LASSERT(req->crq_nrpages > 0);
1522 list_del_init(&page->cp_flight);
1524 page->cp_req = NULL;
1526 EXPORT_SYMBOL(cl_req_page_done);
1529 * Notifies layers that request is about to depart by calling
1530 * cl_req_operations::cro_prep() top-to-bottom.
1532 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1536 const struct cl_req_slice *slice;
1539 * Check that the caller of cl_req_alloc() didn't lie about the number
1542 for (i = 0; i < req->crq_nrobjs; ++i)
1543 LASSERT(req->crq_o[i].ro_obj != NULL);
1546 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1547 if (slice->crs_ops->cro_prep != NULL) {
1548 result = slice->crs_ops->cro_prep(env, slice);
1555 EXPORT_SYMBOL(cl_req_prep);
1558 * Fills in attributes that are passed to server together with transfer. Only
1559 * attributes from \a flags may be touched. This can be called multiple times
1560 * for the same request.
1562 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1563 struct cl_req_attr *attr, obd_valid flags)
1565 const struct cl_req_slice *slice;
1566 struct cl_page *page;
1569 LASSERT(!list_empty(&req->crq_pages));
1571 /* Take any page to use as a model. */
1572 page = list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1574 for (i = 0; i < req->crq_nrobjs; ++i) {
1575 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1576 const struct cl_page_slice *scan;
1577 const struct cl_object *obj;
1579 scan = cl_page_at(page,
1580 slice->crs_dev->cd_lu_dev.ld_type);
1581 LASSERT(scan != NULL);
1582 obj = scan->cpl_obj;
1583 if (slice->crs_ops->cro_attr_set != NULL)
1584 slice->crs_ops->cro_attr_set(env, slice, obj,
1589 EXPORT_SYMBOL(cl_req_attr_set);
1591 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1592 * implemented in libcfs. */
1593 # include <linux/sched.h>
1596 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1598 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1600 init_waitqueue_head(&anchor->csi_waitq);
1601 atomic_set(&anchor->csi_sync_nr, nrpages);
1602 atomic_set(&anchor->csi_barrier, nrpages > 0);
1603 anchor->csi_sync_rc = 0;
1605 EXPORT_SYMBOL(cl_sync_io_init);
1608 * Wait until all transfer completes. Transfer completion routine has to call
1609 * cl_sync_io_note() for every page.
1611 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1612 struct cl_page_list *queue, struct cl_sync_io *anchor,
1615 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1619 LASSERT(timeout >= 0);
1621 rc = l_wait_event(anchor->csi_waitq,
1622 atomic_read(&anchor->csi_sync_nr) == 0,
1625 CERROR("SYNC IO failed with error: %d, try to cancel "
1626 "%d remaining pages\n",
1627 rc, atomic_read(&anchor->csi_sync_nr));
1629 (void)cl_io_cancel(env, io, queue);
1631 lwi = (struct l_wait_info) { 0 };
1632 (void)l_wait_event(anchor->csi_waitq,
1633 atomic_read(&anchor->csi_sync_nr) == 0,
1636 rc = anchor->csi_sync_rc;
1638 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1639 cl_page_list_assume(env, io, queue);
1641 /* wait until cl_sync_io_note() has done wakeup */
1642 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1646 POISON(anchor, 0x5a, sizeof(*anchor));
1649 EXPORT_SYMBOL(cl_sync_io_wait);
1652 * Indicate that transfer of a single page completed.
1654 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1656 if (anchor->csi_sync_rc == 0 && ioret < 0)
1657 anchor->csi_sync_rc = ioret;
1659 * Synchronous IO done without releasing page lock (e.g., as a part of
1660 * ->{prepare,commit}_write(). Completion is used to signal the end of
1663 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1664 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1665 wake_up_all(&anchor->csi_waitq);
1666 /* it's safe to nuke or reuse anchor now */
1667 atomic_set(&anchor->csi_barrier, 0);
1670 EXPORT_SYMBOL(cl_sync_io_note);