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, Whamcloud, Inc.
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 <obd_class.h>
44 #include <obd_support.h>
45 #include <lustre_fid.h>
46 #include <libcfs/list.h>
47 /* lu_time_global_{init,fini}() */
50 #include <cl_object.h>
51 #include "cl_internal.h"
53 /*****************************************************************************
59 #define cl_io_for_each(slice, io) \
60 cfs_list_for_each_entry((slice), &io->ci_layers, cis_linkage)
61 #define cl_io_for_each_reverse(slice, io) \
62 cfs_list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
64 static inline int cl_io_type_is_valid(enum cl_io_type type)
66 return CIT_READ <= type && type < CIT_OP_NR;
69 static inline int cl_io_is_loopable(const struct cl_io *io)
71 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
75 * Returns true iff there is an IO ongoing in the given environment.
77 int cl_io_is_going(const struct lu_env *env)
79 return cl_env_info(env)->clt_current_io != NULL;
81 EXPORT_SYMBOL(cl_io_is_going);
84 * cl_io invariant that holds at all times when exported cl_io_*() functions
85 * are entered and left.
87 static int cl_io_invariant(const struct cl_io *io)
94 * io can own pages only when it is ongoing. Sub-io might
95 * still be in CIS_LOCKED state when top-io is in
98 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
99 (io->ci_state == CIS_LOCKED && up != NULL));
103 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
105 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
107 struct cl_io_slice *slice;
108 struct cl_thread_info *info;
110 LINVRNT(cl_io_type_is_valid(io->ci_type));
111 LINVRNT(cl_io_invariant(io));
114 while (!cfs_list_empty(&io->ci_layers)) {
115 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
117 cfs_list_del_init(&slice->cis_linkage);
118 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
119 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
121 * Invalidate slice to catch use after free. This assumes that
122 * slices are allocated within session and can be touched
123 * after ->cio_fini() returns.
125 slice->cis_io = NULL;
127 io->ci_state = CIS_FINI;
128 info = cl_env_info(env);
129 if (info->clt_current_io == io)
130 info->clt_current_io = NULL;
132 /* sanity check for layout change */
133 switch(io->ci_type) {
138 LASSERT(!io->ci_need_restart);
142 /* Check ignore layout change conf */
143 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
144 !io->ci_need_restart));
151 EXPORT_SYMBOL(cl_io_fini);
153 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
154 enum cl_io_type iot, struct cl_object *obj)
156 struct cl_object *scan;
159 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
160 LINVRNT(cl_io_type_is_valid(iot));
161 LINVRNT(cl_io_invariant(io));
165 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
166 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
167 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_done);
168 CFS_INIT_LIST_HEAD(&io->ci_layers);
171 cl_object_for_each(scan, obj) {
172 if (scan->co_ops->coo_io_init != NULL) {
173 result = scan->co_ops->coo_io_init(env, scan, io);
179 io->ci_state = CIS_INIT;
184 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
186 * \pre obj != cl_object_top(obj)
188 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
189 enum cl_io_type iot, struct cl_object *obj)
191 struct cl_thread_info *info = cl_env_info(env);
193 LASSERT(obj != cl_object_top(obj));
194 if (info->clt_current_io == NULL)
195 info->clt_current_io = io;
196 return cl_io_init0(env, io, iot, obj);
198 EXPORT_SYMBOL(cl_io_sub_init);
201 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
203 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
204 * what the latter returned.
206 * \pre obj == cl_object_top(obj)
207 * \pre cl_io_type_is_valid(iot)
208 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
210 int cl_io_init(const struct lu_env *env, struct cl_io *io,
211 enum cl_io_type iot, struct cl_object *obj)
213 struct cl_thread_info *info = cl_env_info(env);
215 LASSERT(obj == cl_object_top(obj));
216 LASSERT(info->clt_current_io == NULL);
218 info->clt_current_io = io;
219 return cl_io_init0(env, io, iot, obj);
221 EXPORT_SYMBOL(cl_io_init);
224 * Initialize read or write io.
226 * \pre iot == CIT_READ || iot == CIT_WRITE
228 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
229 enum cl_io_type iot, loff_t pos, size_t count)
231 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
232 LINVRNT(io->ci_obj != NULL);
235 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
236 "io range: %u ["LPU64", "LPU64") %u %u\n",
237 iot, (__u64)pos, (__u64)pos + count,
238 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
239 io->u.ci_rw.crw_pos = pos;
240 io->u.ci_rw.crw_count = count;
241 RETURN(cl_io_init(env, io, iot, io->ci_obj));
243 EXPORT_SYMBOL(cl_io_rw_init);
245 static inline const struct lu_fid *
246 cl_lock_descr_fid(const struct cl_lock_descr *descr)
248 return lu_object_fid(&descr->cld_obj->co_lu);
251 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
252 const struct cl_lock_descr *d1)
254 return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
255 __diff_normalize(d0->cld_start, d1->cld_start);
258 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
259 const struct cl_lock_descr *d1)
263 ret = lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1));
266 if (d0->cld_end < d1->cld_start)
268 if (d0->cld_start > d0->cld_end)
273 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
274 const struct cl_lock_descr *d1)
276 d0->cld_start = min(d0->cld_start, d1->cld_start);
277 d0->cld_end = max(d0->cld_end, d1->cld_end);
279 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
280 d0->cld_mode = CLM_WRITE;
282 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
283 d0->cld_mode = CLM_GROUP;
287 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
289 static void cl_io_locks_sort(struct cl_io *io)
294 /* hidden treasure: bubble sort for now. */
296 struct cl_io_lock_link *curr;
297 struct cl_io_lock_link *prev;
298 struct cl_io_lock_link *temp;
303 cfs_list_for_each_entry_safe(curr, temp,
304 &io->ci_lockset.cls_todo,
307 switch (cl_lock_descr_sort(&prev->cill_descr,
308 &curr->cill_descr)) {
311 * IMPOSSIBLE: Identical locks are
318 cfs_list_move_tail(&curr->cill_linkage,
319 &prev->cill_linkage);
321 continue; /* don't change prev: it's
322 * still "previous" */
323 case -1: /* already in order */
334 * Check whether \a queue contains locks matching \a need.
336 * \retval +ve there is a matching lock in the \a queue
337 * \retval 0 there are no matching locks in the \a queue
339 int cl_queue_match(const cfs_list_t *queue,
340 const struct cl_lock_descr *need)
342 struct cl_io_lock_link *scan;
345 cfs_list_for_each_entry(scan, queue, cill_linkage) {
346 if (cl_lock_descr_match(&scan->cill_descr, need))
351 EXPORT_SYMBOL(cl_queue_match);
353 static int cl_queue_merge(const cfs_list_t *queue,
354 const struct cl_lock_descr *need)
356 struct cl_io_lock_link *scan;
359 cfs_list_for_each_entry(scan, queue, cill_linkage) {
360 if (cl_lock_descr_cmp(&scan->cill_descr, need))
362 cl_lock_descr_merge(&scan->cill_descr, need);
363 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
364 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
365 scan->cill_descr.cld_end);
372 static int cl_lockset_match(const struct cl_lockset *set,
373 const struct cl_lock_descr *need)
375 return cl_queue_match(&set->cls_curr, need) ||
376 cl_queue_match(&set->cls_done, need);
379 static int cl_lockset_merge(const struct cl_lockset *set,
380 const struct cl_lock_descr *need)
382 return cl_queue_merge(&set->cls_todo, need) ||
383 cl_lockset_match(set, need);
386 static int cl_lockset_lock_one(const struct lu_env *env,
387 struct cl_io *io, struct cl_lockset *set,
388 struct cl_io_lock_link *link)
390 struct cl_lock *lock;
395 if (io->ci_lockreq == CILR_PEEK) {
396 lock = cl_lock_peek(env, io, &link->cill_descr, "io", io);
398 lock = ERR_PTR(-ENODATA);
400 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
403 link->cill_lock = lock;
404 cfs_list_move(&link->cill_linkage, &set->cls_curr);
405 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
406 result = cl_wait(env, lock);
408 cfs_list_move(&link->cill_linkage,
413 result = PTR_ERR(lock);
417 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
418 struct cl_io_lock_link *link)
420 struct cl_lock *lock = link->cill_lock;
423 cfs_list_del_init(&link->cill_linkage);
425 cl_lock_release(env, lock, "io", io);
426 link->cill_lock = NULL;
428 if (link->cill_fini != NULL)
429 link->cill_fini(env, link);
433 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
434 struct cl_lockset *set)
436 struct cl_io_lock_link *link;
437 struct cl_io_lock_link *temp;
438 struct cl_lock *lock;
443 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
444 if (!cl_lockset_match(set, &link->cill_descr)) {
445 /* XXX some locking to guarantee that locks aren't
446 * expanded in between. */
447 result = cl_lockset_lock_one(env, io, set, link);
451 cl_lock_link_fini(env, io, link);
454 cfs_list_for_each_entry_safe(link, temp,
455 &set->cls_curr, cill_linkage) {
456 lock = link->cill_lock;
457 result = cl_wait(env, lock);
459 cfs_list_move(&link->cill_linkage,
469 * Takes locks necessary for the current iteration of io.
471 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
472 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
475 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
477 const struct cl_io_slice *scan;
480 LINVRNT(cl_io_is_loopable(io));
481 LINVRNT(io->ci_state == CIS_IT_STARTED);
482 LINVRNT(cl_io_invariant(io));
485 cl_io_for_each(scan, io) {
486 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
488 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
493 cl_io_locks_sort(io);
494 result = cl_lockset_lock(env, io, &io->ci_lockset);
497 cl_io_unlock(env, io);
499 io->ci_state = CIS_LOCKED;
502 EXPORT_SYMBOL(cl_io_lock);
505 * Release locks takes by io.
507 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
509 struct cl_lockset *set;
510 struct cl_io_lock_link *link;
511 struct cl_io_lock_link *temp;
512 const struct cl_io_slice *scan;
514 LASSERT(cl_io_is_loopable(io));
515 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
516 LINVRNT(cl_io_invariant(io));
519 set = &io->ci_lockset;
521 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
522 cl_lock_link_fini(env, io, link);
524 cfs_list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
525 cl_lock_link_fini(env, io, link);
527 cfs_list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
528 cl_unuse(env, link->cill_lock);
529 cl_lock_link_fini(env, io, link);
531 cl_io_for_each_reverse(scan, io) {
532 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
533 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
535 io->ci_state = CIS_UNLOCKED;
536 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
539 EXPORT_SYMBOL(cl_io_unlock);
542 * Prepares next iteration of io.
544 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
545 * layers a chance to modify io parameters, e.g., so that lov can restrict io
546 * to a single stripe.
548 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
550 const struct cl_io_slice *scan;
553 LINVRNT(cl_io_is_loopable(io));
554 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
555 LINVRNT(cl_io_invariant(io));
559 cl_io_for_each(scan, io) {
560 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
562 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
568 io->ci_state = CIS_IT_STARTED;
571 EXPORT_SYMBOL(cl_io_iter_init);
574 * Finalizes io iteration.
576 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
578 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
580 const struct cl_io_slice *scan;
582 LINVRNT(cl_io_is_loopable(io));
583 LINVRNT(io->ci_state == CIS_UNLOCKED);
584 LINVRNT(cl_io_invariant(io));
587 cl_io_for_each_reverse(scan, io) {
588 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
589 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
591 io->ci_state = CIS_IT_ENDED;
594 EXPORT_SYMBOL(cl_io_iter_fini);
597 * Records that read or write io progressed \a nob bytes forward.
599 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
601 const struct cl_io_slice *scan;
603 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
605 LINVRNT(cl_io_is_loopable(io));
606 LINVRNT(cl_io_invariant(io));
610 io->u.ci_rw.crw_pos += nob;
611 io->u.ci_rw.crw_count -= nob;
613 /* layers have to be notified. */
614 cl_io_for_each_reverse(scan, io) {
615 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
616 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
621 EXPORT_SYMBOL(cl_io_rw_advance);
624 * Adds a lock to a lockset.
626 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
627 struct cl_io_lock_link *link)
632 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
635 cfs_list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
640 EXPORT_SYMBOL(cl_io_lock_add);
642 static void cl_free_io_lock_link(const struct lu_env *env,
643 struct cl_io_lock_link *link)
649 * Allocates new lock link, and uses it to add a lock to a lockset.
651 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
652 struct cl_lock_descr *descr)
654 struct cl_io_lock_link *link;
660 link->cill_descr = *descr;
661 link->cill_fini = cl_free_io_lock_link;
662 result = cl_io_lock_add(env, io, link);
663 if (result) /* lock match */
664 link->cill_fini(env, link);
670 EXPORT_SYMBOL(cl_io_lock_alloc_add);
673 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
675 int cl_io_start(const struct lu_env *env, struct cl_io *io)
677 const struct cl_io_slice *scan;
680 LINVRNT(cl_io_is_loopable(io));
681 LINVRNT(io->ci_state == CIS_LOCKED);
682 LINVRNT(cl_io_invariant(io));
685 io->ci_state = CIS_IO_GOING;
686 cl_io_for_each(scan, io) {
687 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
689 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
697 EXPORT_SYMBOL(cl_io_start);
700 * Wait until current io iteration is finished by calling
701 * cl_io_operations::cio_end() bottom-to-top.
703 void cl_io_end(const struct lu_env *env, struct cl_io *io)
705 const struct cl_io_slice *scan;
707 LINVRNT(cl_io_is_loopable(io));
708 LINVRNT(io->ci_state == CIS_IO_GOING);
709 LINVRNT(cl_io_invariant(io));
712 cl_io_for_each_reverse(scan, io) {
713 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
714 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
715 /* TODO: error handling. */
717 io->ci_state = CIS_IO_FINISHED;
720 EXPORT_SYMBOL(cl_io_end);
722 static const struct cl_page_slice *
723 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
725 const struct cl_page_slice *slice;
727 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
728 LINVRNT(slice != NULL);
733 * True iff \a page is within \a io range.
735 static int cl_page_in_io(const struct cl_page *page, const struct cl_io *io)
742 idx = page->cp_index;
743 switch (io->ci_type) {
747 * check that [start, end) and [pos, pos + count) extents
750 if (!cl_io_is_append(io)) {
751 const struct cl_io_rw_common *crw = &(io->u.ci_rw);
752 start = cl_offset(page->cp_obj, idx);
753 end = cl_offset(page->cp_obj, idx + 1);
754 result = crw->crw_pos < end &&
755 start < crw->crw_pos + crw->crw_count;
759 result = io->u.ci_fault.ft_index == idx;
768 * Called by read io, when page has to be read from the server.
770 * \see cl_io_operations::cio_read_page()
772 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
773 struct cl_page *page)
775 const struct cl_io_slice *scan;
776 struct cl_2queue *queue;
779 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
780 LINVRNT(cl_page_is_owned(page, io));
781 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
782 LINVRNT(cl_page_in_io(page, io));
783 LINVRNT(cl_io_invariant(io));
786 queue = &io->ci_queue;
788 cl_2queue_init(queue);
790 * ->cio_read_page() methods called in the loop below are supposed to
791 * never block waiting for network (the only subtle point is the
792 * creation of new pages for read-ahead that might result in cache
793 * shrinking, but currently only clean pages are shrunk and this
794 * requires no network io).
796 * Should this ever starts blocking, retry loop would be needed for
797 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
799 cl_io_for_each(scan, io) {
800 if (scan->cis_iop->cio_read_page != NULL) {
801 const struct cl_page_slice *slice;
803 slice = cl_io_slice_page(scan, page);
804 LINVRNT(slice != NULL);
805 result = scan->cis_iop->cio_read_page(env, scan, slice);
811 result = cl_io_submit_rw(env, io, CRT_READ, queue);
813 * Unlock unsent pages in case of error.
815 cl_page_list_disown(env, io, &queue->c2_qin);
816 cl_2queue_fini(env, queue);
819 EXPORT_SYMBOL(cl_io_read_page);
822 * Called by write io to prepare page to receive data from user buffer.
824 * \see cl_io_operations::cio_prepare_write()
826 int cl_io_prepare_write(const struct lu_env *env, struct cl_io *io,
827 struct cl_page *page, unsigned from, unsigned to)
829 const struct cl_io_slice *scan;
832 LINVRNT(io->ci_type == CIT_WRITE);
833 LINVRNT(cl_page_is_owned(page, io));
834 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
835 LINVRNT(cl_io_invariant(io));
836 LASSERT(cl_page_in_io(page, io));
839 cl_io_for_each_reverse(scan, io) {
840 if (scan->cis_iop->cio_prepare_write != NULL) {
841 const struct cl_page_slice *slice;
843 slice = cl_io_slice_page(scan, page);
844 result = scan->cis_iop->cio_prepare_write(env, scan,
853 EXPORT_SYMBOL(cl_io_prepare_write);
856 * Called by write io after user data were copied into a page.
858 * \see cl_io_operations::cio_commit_write()
860 int cl_io_commit_write(const struct lu_env *env, struct cl_io *io,
861 struct cl_page *page, unsigned from, unsigned to)
863 const struct cl_io_slice *scan;
866 LINVRNT(io->ci_type == CIT_WRITE);
867 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
868 LINVRNT(cl_io_invariant(io));
870 * XXX Uh... not nice. Top level cl_io_commit_write() call (vvp->lov)
871 * already called cl_page_cache_add(), moving page into CPS_CACHED
872 * state. Better (and more general) way of dealing with such situation
875 LASSERT(cl_page_is_owned(page, io) || page->cp_parent != NULL);
876 LASSERT(cl_page_in_io(page, io));
879 cl_io_for_each(scan, io) {
880 if (scan->cis_iop->cio_commit_write != NULL) {
881 const struct cl_page_slice *slice;
883 slice = cl_io_slice_page(scan, page);
884 result = scan->cis_iop->cio_commit_write(env, scan,
891 LINVRNT(result <= 0);
894 EXPORT_SYMBOL(cl_io_commit_write);
897 * Submits a list of pages for immediate io.
899 * After the function gets returned, The submitted pages are moved to
900 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
901 * to be submitted, and the pages are errant to submit.
903 * \returns 0 if at least one page was submitted, error code otherwise.
904 * \see cl_io_operations::cio_submit()
906 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
907 enum cl_req_type crt, struct cl_2queue *queue)
909 const struct cl_io_slice *scan;
912 LINVRNT(crt < ARRAY_SIZE(scan->cis_iop->req_op));
915 cl_io_for_each(scan, io) {
916 if (scan->cis_iop->req_op[crt].cio_submit == NULL)
918 result = scan->cis_iop->req_op[crt].cio_submit(env, scan, crt,
924 * If ->cio_submit() failed, no pages were sent.
926 LASSERT(ergo(result != 0, cfs_list_empty(&queue->c2_qout.pl_pages)));
929 EXPORT_SYMBOL(cl_io_submit_rw);
932 * Submit a sync_io and wait for the IO to be finished, or error happens.
933 * If \a timeout is zero, it means to wait for the IO unconditionally.
935 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
936 enum cl_req_type iot, struct cl_2queue *queue,
939 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
943 cl_page_list_for_each(pg, &queue->c2_qin) {
944 LASSERT(pg->cp_sync_io == NULL);
945 pg->cp_sync_io = anchor;
948 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
949 rc = cl_io_submit_rw(env, io, iot, queue);
952 * If some pages weren't sent for any reason (e.g.,
953 * read found up-to-date pages in the cache, or write found
954 * clean pages), count them as completed to avoid infinite
957 cl_page_list_for_each(pg, &queue->c2_qin) {
958 pg->cp_sync_io = NULL;
959 cl_sync_io_note(anchor, +1);
962 /* wait for the IO to be finished. */
963 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
966 LASSERT(cfs_list_empty(&queue->c2_qout.pl_pages));
967 cl_page_list_for_each(pg, &queue->c2_qin)
968 pg->cp_sync_io = NULL;
972 EXPORT_SYMBOL(cl_io_submit_sync);
975 * Cancel an IO which has been submitted by cl_io_submit_rw.
977 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
978 struct cl_page_list *queue)
980 struct cl_page *page;
983 CERROR("Canceling ongoing page trasmission\n");
984 cl_page_list_for_each(page, queue) {
987 LINVRNT(cl_page_in_io(page, io));
988 rc = cl_page_cancel(env, page);
989 result = result ?: rc;
993 EXPORT_SYMBOL(cl_io_cancel);
998 * Pumps io through iterations calling
1000 * - cl_io_iter_init()
1010 * - cl_io_iter_fini()
1012 * repeatedly until there is no more io to do.
1014 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
1018 LINVRNT(cl_io_is_loopable(io));
1024 io->ci_continue = 0;
1025 result = cl_io_iter_init(env, io);
1028 result = cl_io_lock(env, io);
1031 * Notify layers that locks has been taken,
1032 * and do actual i/o.
1034 * - llite: kms, short read;
1035 * - llite: generic_file_read();
1037 result = cl_io_start(env, io);
1039 * Send any remaining pending
1042 * - llite: ll_rw_stats_tally.
1045 cl_io_unlock(env, io);
1046 cl_io_rw_advance(env, io, io->ci_nob - nob);
1049 cl_io_iter_fini(env, io);
1050 } while (result == 0 && io->ci_continue);
1052 result = io->ci_result;
1053 RETURN(result < 0 ? result : 0);
1055 EXPORT_SYMBOL(cl_io_loop);
1058 * Adds io slice to the cl_io.
1060 * This is called by cl_object_operations::coo_io_init() methods to add a
1061 * per-layer state to the io. New state is added at the end of
1062 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
1064 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
1066 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
1067 struct cl_object *obj,
1068 const struct cl_io_operations *ops)
1070 cfs_list_t *linkage = &slice->cis_linkage;
1072 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
1073 cfs_list_empty(linkage));
1076 cfs_list_add_tail(linkage, &io->ci_layers);
1078 slice->cis_obj = obj;
1079 slice->cis_iop = ops;
1082 EXPORT_SYMBOL(cl_io_slice_add);
1086 * Initializes page list.
1088 void cl_page_list_init(struct cl_page_list *plist)
1092 CFS_INIT_LIST_HEAD(&plist->pl_pages);
1093 plist->pl_owner = cfs_current();
1096 EXPORT_SYMBOL(cl_page_list_init);
1099 * Adds a page to a page list.
1101 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1104 /* it would be better to check that page is owned by "current" io, but
1105 * it is not passed here. */
1106 LASSERT(page->cp_owner != NULL);
1107 LINVRNT(plist->pl_owner == cfs_current());
1110 mutex_lock(&page->cp_mutex);
1112 LASSERT(cfs_list_empty(&page->cp_batch));
1113 cfs_list_add_tail(&page->cp_batch, &plist->pl_pages);
1115 page->cp_queue_ref = lu_ref_add(&page->cp_reference, "queue", plist);
1119 EXPORT_SYMBOL(cl_page_list_add);
1122 * Removes a page from a page list.
1124 void cl_page_list_del(const struct lu_env *env,
1125 struct cl_page_list *plist, struct cl_page *page)
1127 LASSERT(plist->pl_nr > 0);
1128 LINVRNT(plist->pl_owner == cfs_current());
1131 cfs_list_del_init(&page->cp_batch);
1133 mutex_unlock(&page->cp_mutex);
1136 lu_ref_del_at(&page->cp_reference, page->cp_queue_ref, "queue", plist);
1137 cl_page_put(env, page);
1140 EXPORT_SYMBOL(cl_page_list_del);
1143 * Moves a page from one page list to another.
1145 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1146 struct cl_page *page)
1148 LASSERT(src->pl_nr > 0);
1149 LINVRNT(dst->pl_owner == cfs_current());
1150 LINVRNT(src->pl_owner == cfs_current());
1153 cfs_list_move_tail(&page->cp_batch, &dst->pl_pages);
1156 lu_ref_set_at(&page->cp_reference,
1157 page->cp_queue_ref, "queue", src, dst);
1160 EXPORT_SYMBOL(cl_page_list_move);
1163 * splice the cl_page_list, just as list head does
1165 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1167 struct cl_page *page;
1168 struct cl_page *tmp;
1170 LINVRNT(list->pl_owner == cfs_current());
1171 LINVRNT(head->pl_owner == cfs_current());
1174 cl_page_list_for_each_safe(page, tmp, list)
1175 cl_page_list_move(head, list, page);
1178 EXPORT_SYMBOL(cl_page_list_splice);
1180 void cl_page_disown0(const struct lu_env *env,
1181 struct cl_io *io, struct cl_page *pg);
1184 * Disowns pages in a queue.
1186 void cl_page_list_disown(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;
1192 LINVRNT(plist->pl_owner == cfs_current());
1195 cl_page_list_for_each_safe(page, temp, plist) {
1196 LASSERT(plist->pl_nr > 0);
1198 cfs_list_del_init(&page->cp_batch);
1200 mutex_unlock(&page->cp_mutex);
1204 * cl_page_disown0 rather than usual cl_page_disown() is used,
1205 * because pages are possibly in CPS_FREEING state already due
1206 * to the call to cl_page_list_discard().
1209 * XXX cl_page_disown0() will fail if page is not locked.
1211 cl_page_disown0(env, io, page);
1212 lu_ref_del(&page->cp_reference, "queue", plist);
1213 cl_page_put(env, page);
1217 EXPORT_SYMBOL(cl_page_list_disown);
1220 * Releases pages from queue.
1222 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1224 struct cl_page *page;
1225 struct cl_page *temp;
1227 LINVRNT(plist->pl_owner == cfs_current());
1230 cl_page_list_for_each_safe(page, temp, plist)
1231 cl_page_list_del(env, plist, page);
1232 LASSERT(plist->pl_nr == 0);
1235 EXPORT_SYMBOL(cl_page_list_fini);
1238 * Owns all pages in a queue.
1240 int cl_page_list_own(const struct lu_env *env,
1241 struct cl_io *io, struct cl_page_list *plist)
1243 struct cl_page *page;
1244 struct cl_page *temp;
1248 LINVRNT(plist->pl_owner == cfs_current());
1252 cl_page_list_for_each_safe(page, temp, plist) {
1253 LASSERT(index <= page->cp_index);
1254 index = page->cp_index;
1255 if (cl_page_own(env, io, page) == 0)
1256 result = result ?: page->cp_error;
1258 cl_page_list_del(env, plist, page);
1262 EXPORT_SYMBOL(cl_page_list_own);
1265 * Assumes all pages in a queue.
1267 void cl_page_list_assume(const struct lu_env *env,
1268 struct cl_io *io, struct cl_page_list *plist)
1270 struct cl_page *page;
1272 LINVRNT(plist->pl_owner == cfs_current());
1274 cl_page_list_for_each(page, plist)
1275 cl_page_assume(env, io, page);
1277 EXPORT_SYMBOL(cl_page_list_assume);
1280 * Discards all pages in a queue.
1282 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1283 struct cl_page_list *plist)
1285 struct cl_page *page;
1287 LINVRNT(plist->pl_owner == cfs_current());
1289 cl_page_list_for_each(page, plist)
1290 cl_page_discard(env, io, page);
1293 EXPORT_SYMBOL(cl_page_list_discard);
1296 * Unmaps all pages in a queue from user virtual memory.
1298 int cl_page_list_unmap(const struct lu_env *env, struct cl_io *io,
1299 struct cl_page_list *plist)
1301 struct cl_page *page;
1304 LINVRNT(plist->pl_owner == cfs_current());
1307 cl_page_list_for_each(page, plist) {
1308 result = cl_page_unmap(env, io, page);
1314 EXPORT_SYMBOL(cl_page_list_unmap);
1317 * Initialize dual page queue.
1319 void cl_2queue_init(struct cl_2queue *queue)
1322 cl_page_list_init(&queue->c2_qin);
1323 cl_page_list_init(&queue->c2_qout);
1326 EXPORT_SYMBOL(cl_2queue_init);
1329 * Add a page to the incoming page list of 2-queue.
1331 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1334 cl_page_list_add(&queue->c2_qin, page);
1337 EXPORT_SYMBOL(cl_2queue_add);
1340 * Disown pages in both lists of a 2-queue.
1342 void cl_2queue_disown(const struct lu_env *env,
1343 struct cl_io *io, struct cl_2queue *queue)
1346 cl_page_list_disown(env, io, &queue->c2_qin);
1347 cl_page_list_disown(env, io, &queue->c2_qout);
1350 EXPORT_SYMBOL(cl_2queue_disown);
1353 * Discard (truncate) pages in both lists of a 2-queue.
1355 void cl_2queue_discard(const struct lu_env *env,
1356 struct cl_io *io, struct cl_2queue *queue)
1359 cl_page_list_discard(env, io, &queue->c2_qin);
1360 cl_page_list_discard(env, io, &queue->c2_qout);
1363 EXPORT_SYMBOL(cl_2queue_discard);
1366 * Assume to own the pages in cl_2queue
1368 void cl_2queue_assume(const struct lu_env *env,
1369 struct cl_io *io, struct cl_2queue *queue)
1371 cl_page_list_assume(env, io, &queue->c2_qin);
1372 cl_page_list_assume(env, io, &queue->c2_qout);
1374 EXPORT_SYMBOL(cl_2queue_assume);
1377 * Finalize both page lists of a 2-queue.
1379 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1382 cl_page_list_fini(env, &queue->c2_qout);
1383 cl_page_list_fini(env, &queue->c2_qin);
1386 EXPORT_SYMBOL(cl_2queue_fini);
1389 * Initialize a 2-queue to contain \a page in its incoming page list.
1391 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1394 cl_2queue_init(queue);
1395 cl_2queue_add(queue, page);
1398 EXPORT_SYMBOL(cl_2queue_init_page);
1401 * Returns top-level io.
1403 * \see cl_object_top(), cl_page_top().
1405 struct cl_io *cl_io_top(struct cl_io *io)
1408 while (io->ci_parent != NULL)
1412 EXPORT_SYMBOL(cl_io_top);
1415 * Prints human readable representation of \a io to the \a f.
1417 void cl_io_print(const struct lu_env *env, void *cookie,
1418 lu_printer_t printer, const struct cl_io *io)
1423 * Adds request slice to the compound request.
1425 * This is called by cl_device_operations::cdo_req_init() methods to add a
1426 * per-layer state to the request. New state is added at the end of
1427 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1429 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1431 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1432 struct cl_device *dev,
1433 const struct cl_req_operations *ops)
1436 cfs_list_add_tail(&slice->crs_linkage, &req->crq_layers);
1437 slice->crs_dev = dev;
1438 slice->crs_ops = ops;
1439 slice->crs_req = req;
1442 EXPORT_SYMBOL(cl_req_slice_add);
1444 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1448 LASSERT(cfs_list_empty(&req->crq_pages));
1449 LASSERT(req->crq_nrpages == 0);
1450 LINVRNT(cfs_list_empty(&req->crq_layers));
1451 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1454 if (req->crq_o != NULL) {
1455 for (i = 0; i < req->crq_nrobjs; ++i) {
1456 struct cl_object *obj = req->crq_o[i].ro_obj;
1458 lu_object_ref_del_at(&obj->co_lu,
1459 req->crq_o[i].ro_obj_ref,
1461 cl_object_put(env, obj);
1464 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1470 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1471 struct cl_page *page)
1473 struct cl_device *dev;
1474 struct cl_page_slice *slice;
1479 page = cl_page_top(page);
1481 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1482 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1483 if (dev->cd_ops->cdo_req_init != NULL) {
1484 result = dev->cd_ops->cdo_req_init(env,
1490 page = page->cp_child;
1491 } while (page != NULL && result == 0);
1496 * Invokes per-request transfer completion call-backs
1497 * (cl_req_operations::cro_completion()) bottom-to-top.
1499 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1501 struct cl_req_slice *slice;
1505 * for the lack of list_for_each_entry_reverse_safe()...
1507 while (!cfs_list_empty(&req->crq_layers)) {
1508 slice = cfs_list_entry(req->crq_layers.prev,
1509 struct cl_req_slice, crs_linkage);
1510 cfs_list_del_init(&slice->crs_linkage);
1511 if (slice->crs_ops->cro_completion != NULL)
1512 slice->crs_ops->cro_completion(env, slice, rc);
1514 cl_req_free(env, req);
1517 EXPORT_SYMBOL(cl_req_completion);
1520 * Allocates new transfer request.
1522 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1523 enum cl_req_type crt, int nr_objects)
1527 LINVRNT(nr_objects > 0);
1534 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1535 if (req->crq_o != NULL) {
1536 req->crq_nrobjs = nr_objects;
1537 req->crq_type = crt;
1538 CFS_INIT_LIST_HEAD(&req->crq_pages);
1539 CFS_INIT_LIST_HEAD(&req->crq_layers);
1540 result = cl_req_init(env, req, page);
1544 cl_req_completion(env, req, result);
1545 req = ERR_PTR(result);
1548 req = ERR_PTR(-ENOMEM);
1551 EXPORT_SYMBOL(cl_req_alloc);
1554 * Adds a page to a request.
1556 void cl_req_page_add(const struct lu_env *env,
1557 struct cl_req *req, struct cl_page *page)
1559 struct cl_object *obj;
1560 struct cl_req_obj *rqo;
1564 page = cl_page_top(page);
1566 LASSERT(cfs_list_empty(&page->cp_flight));
1567 LASSERT(page->cp_req == NULL);
1569 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1570 req, req->crq_type, req->crq_nrpages);
1572 cfs_list_add_tail(&page->cp_flight, &req->crq_pages);
1575 obj = cl_object_top(page->cp_obj);
1576 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1577 if (rqo->ro_obj == NULL) {
1580 rqo->ro_obj_ref = lu_object_ref_add(&obj->co_lu,
1585 LASSERT(i < req->crq_nrobjs);
1588 EXPORT_SYMBOL(cl_req_page_add);
1591 * Removes a page from a request.
1593 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1595 struct cl_req *req = page->cp_req;
1598 page = cl_page_top(page);
1600 LASSERT(!cfs_list_empty(&page->cp_flight));
1601 LASSERT(req->crq_nrpages > 0);
1603 cfs_list_del_init(&page->cp_flight);
1605 page->cp_req = NULL;
1608 EXPORT_SYMBOL(cl_req_page_done);
1611 * Notifies layers that request is about to depart by calling
1612 * cl_req_operations::cro_prep() top-to-bottom.
1614 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1618 const struct cl_req_slice *slice;
1622 * Check that the caller of cl_req_alloc() didn't lie about the number
1625 for (i = 0; i < req->crq_nrobjs; ++i)
1626 LASSERT(req->crq_o[i].ro_obj != NULL);
1629 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1630 if (slice->crs_ops->cro_prep != NULL) {
1631 result = slice->crs_ops->cro_prep(env, slice);
1638 EXPORT_SYMBOL(cl_req_prep);
1641 * Fills in attributes that are passed to server together with transfer. Only
1642 * attributes from \a flags may be touched. This can be called multiple times
1643 * for the same request.
1645 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1646 struct cl_req_attr *attr, obd_valid flags)
1648 const struct cl_req_slice *slice;
1649 struct cl_page *page;
1652 LASSERT(!cfs_list_empty(&req->crq_pages));
1655 /* Take any page to use as a model. */
1656 page = cfs_list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1658 for (i = 0; i < req->crq_nrobjs; ++i) {
1659 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1660 const struct cl_page_slice *scan;
1661 const struct cl_object *obj;
1663 scan = cl_page_at(page,
1664 slice->crs_dev->cd_lu_dev.ld_type);
1665 LASSERT(scan != NULL);
1666 obj = scan->cpl_obj;
1667 if (slice->crs_ops->cro_attr_set != NULL)
1668 slice->crs_ops->cro_attr_set(env, slice, obj,
1674 EXPORT_SYMBOL(cl_req_attr_set);
1676 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1677 * implemented in libcfs. */
1679 # include <linux/sched.h>
1680 #else /* __KERNEL__ */
1681 # include <liblustre.h>
1685 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1687 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1690 cfs_waitq_init(&anchor->csi_waitq);
1691 cfs_atomic_set(&anchor->csi_sync_nr, nrpages);
1692 anchor->csi_sync_rc = 0;
1695 EXPORT_SYMBOL(cl_sync_io_init);
1698 * Wait until all transfer completes. Transfer completion routine has to call
1699 * cl_sync_io_note() for every page.
1701 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1702 struct cl_page_list *queue, struct cl_sync_io *anchor,
1705 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1710 LASSERT(timeout >= 0);
1712 rc = l_wait_event(anchor->csi_waitq,
1713 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1716 CERROR("SYNC IO failed with error: %d, try to cancel "
1717 "%d remaining pages\n",
1718 rc, cfs_atomic_read(&anchor->csi_sync_nr));
1720 (void)cl_io_cancel(env, io, queue);
1722 lwi = (struct l_wait_info) { 0 };
1723 (void)l_wait_event(anchor->csi_waitq,
1724 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1727 rc = anchor->csi_sync_rc;
1729 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) == 0);
1730 cl_page_list_assume(env, io, queue);
1731 POISON(anchor, 0x5a, sizeof *anchor);
1734 EXPORT_SYMBOL(cl_sync_io_wait);
1737 * Indicate that transfer of a single page completed.
1739 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1742 if (anchor->csi_sync_rc == 0 && ioret < 0)
1743 anchor->csi_sync_rc = ioret;
1745 * Synchronous IO done without releasing page lock (e.g., as a part of
1746 * ->{prepare,commit}_write(). Completion is used to signal the end of
1749 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) > 0);
1750 if (cfs_atomic_dec_and_test(&anchor->csi_sync_nr))
1751 cfs_waitq_broadcast(&anchor->csi_waitq);
1754 EXPORT_SYMBOL(cl_sync_io_note);