1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 # define EXPORT_SYMTAB
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <libcfs/list.h>
50 /* lu_time_global_{init,fini}() */
53 #include <cl_object.h>
54 #include "cl_internal.h"
56 /*****************************************************************************
62 #define cl_io_for_each(slice, io) \
63 cfs_list_for_each_entry((slice), &io->ci_layers, cis_linkage)
64 #define cl_io_for_each_reverse(slice, io) \
65 cfs_list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
67 static inline int cl_io_type_is_valid(enum cl_io_type type)
69 return CIT_READ <= type && type < CIT_OP_NR;
72 static inline int cl_io_is_loopable(const struct cl_io *io)
74 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
78 * Returns true iff there is an IO ongoing in the given environment.
80 int cl_io_is_going(const struct lu_env *env)
82 return cl_env_info(env)->clt_current_io != NULL;
84 EXPORT_SYMBOL(cl_io_is_going);
87 * cl_io invariant that holds at all times when exported cl_io_*() functions
88 * are entered and left.
90 static int cl_io_invariant(const struct cl_io *io)
97 * io can own pages only when it is ongoing. Sub-io might
98 * still be in CIS_LOCKED state when top-io is in
101 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
102 (io->ci_state == CIS_LOCKED && up != NULL));
106 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
108 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
110 struct cl_io_slice *slice;
111 struct cl_thread_info *info;
113 LINVRNT(cl_io_type_is_valid(io->ci_type));
114 LINVRNT(cl_io_invariant(io));
117 while (!cfs_list_empty(&io->ci_layers)) {
118 slice = container_of(io->ci_layers.next, struct cl_io_slice,
120 cfs_list_del_init(&slice->cis_linkage);
121 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
122 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
124 * Invalidate slice to catch use after free. This assumes that
125 * slices are allocated within session and can be touched
126 * after ->cio_fini() returns.
128 slice->cis_io = NULL;
130 io->ci_state = CIS_FINI;
131 info = cl_env_info(env);
132 if (info->clt_current_io == io)
133 info->clt_current_io = NULL;
136 EXPORT_SYMBOL(cl_io_fini);
138 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
139 enum cl_io_type iot, struct cl_object *obj)
141 struct cl_object *scan;
144 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
145 LINVRNT(cl_io_type_is_valid(iot));
146 LINVRNT(cl_io_invariant(io));
150 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
151 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
152 CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_done);
153 CFS_INIT_LIST_HEAD(&io->ci_layers);
156 cl_object_for_each(scan, obj) {
157 if (scan->co_ops->coo_io_init != NULL) {
158 result = scan->co_ops->coo_io_init(env, scan, io);
164 io->ci_state = CIS_INIT;
169 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
171 * \pre obj != cl_object_top(obj)
173 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
174 enum cl_io_type iot, struct cl_object *obj)
176 struct cl_thread_info *info = cl_env_info(env);
178 LASSERT(obj != cl_object_top(obj));
179 if (info->clt_current_io == NULL)
180 info->clt_current_io = io;
181 return cl_io_init0(env, io, iot, obj);
183 EXPORT_SYMBOL(cl_io_sub_init);
186 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
188 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
189 * what the latter returned.
191 * \pre obj == cl_object_top(obj)
192 * \pre cl_io_type_is_valid(iot)
193 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
195 int cl_io_init(const struct lu_env *env, struct cl_io *io,
196 enum cl_io_type iot, struct cl_object *obj)
198 struct cl_thread_info *info = cl_env_info(env);
200 LASSERT(obj == cl_object_top(obj));
201 LASSERT(info->clt_current_io == NULL);
203 info->clt_current_io = io;
204 return cl_io_init0(env, io, iot, obj);
206 EXPORT_SYMBOL(cl_io_init);
209 * Initialize read or write io.
211 * \pre iot == CIT_READ || iot == CIT_WRITE
213 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
214 enum cl_io_type iot, loff_t pos, size_t count)
216 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
217 LINVRNT(io->ci_obj != NULL);
220 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
221 "io range: %u ["LPU64", "LPU64") %u %u\n",
222 iot, (__u64)pos, (__u64)pos + count,
223 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
224 io->u.ci_rw.crw_pos = pos;
225 io->u.ci_rw.crw_count = count;
226 RETURN(cl_io_init(env, io, iot, io->ci_obj));
228 EXPORT_SYMBOL(cl_io_rw_init);
230 static inline const struct lu_fid *
231 cl_lock_descr_fid(const struct cl_lock_descr *descr)
233 return lu_object_fid(&descr->cld_obj->co_lu);
236 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
237 const struct cl_lock_descr *d1)
239 return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
240 __diff_normalize(d0->cld_start, d1->cld_start);
243 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
244 const struct cl_lock_descr *d1)
248 ret = lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1));
251 if (d0->cld_end < d1->cld_start)
253 if (d0->cld_start > d0->cld_end)
258 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
259 const struct cl_lock_descr *d1)
261 d0->cld_start = min(d0->cld_start, d1->cld_start);
262 d0->cld_end = max(d0->cld_end, d1->cld_end);
264 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
265 d0->cld_mode = CLM_WRITE;
267 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
268 d0->cld_mode = CLM_GROUP;
272 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
274 static void cl_io_locks_sort(struct cl_io *io)
279 /* hidden treasure: bubble sort for now. */
281 struct cl_io_lock_link *curr;
282 struct cl_io_lock_link *prev;
283 struct cl_io_lock_link *temp;
288 cfs_list_for_each_entry_safe(curr, temp,
289 &io->ci_lockset.cls_todo,
292 switch (cl_lock_descr_sort(&prev->cill_descr,
293 &curr->cill_descr)) {
296 * IMPOSSIBLE: Identical locks are
303 cfs_list_move_tail(&curr->cill_linkage,
304 &prev->cill_linkage);
306 continue; /* don't change prev: it's
307 * still "previous" */
308 case -1: /* already in order */
319 * Check whether \a queue contains locks matching \a need.
321 * \retval +ve there is a matching lock in the \a queue
322 * \retval 0 there are no matching locks in the \a queue
324 int cl_queue_match(const cfs_list_t *queue,
325 const struct cl_lock_descr *need)
327 struct cl_io_lock_link *scan;
330 cfs_list_for_each_entry(scan, queue, cill_linkage) {
331 if (cl_lock_descr_match(&scan->cill_descr, need))
336 EXPORT_SYMBOL(cl_queue_match);
338 static int cl_queue_merge(const cfs_list_t *queue,
339 const struct cl_lock_descr *need)
341 struct cl_io_lock_link *scan;
344 cfs_list_for_each_entry(scan, queue, cill_linkage) {
345 if (cl_lock_descr_cmp(&scan->cill_descr, need))
347 cl_lock_descr_merge(&scan->cill_descr, need);
348 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
349 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
350 scan->cill_descr.cld_end);
357 static int cl_lockset_match(const struct cl_lockset *set,
358 const struct cl_lock_descr *need)
360 return cl_queue_match(&set->cls_curr, need) ||
361 cl_queue_match(&set->cls_done, need);
364 static int cl_lockset_merge(const struct cl_lockset *set,
365 const struct cl_lock_descr *need)
367 return cl_queue_merge(&set->cls_todo, need) ||
368 cl_lockset_match(set, need);
371 static int cl_lockset_lock_one(const struct lu_env *env,
372 struct cl_io *io, struct cl_lockset *set,
373 struct cl_io_lock_link *link)
375 struct cl_lock *lock;
380 if (io->ci_lockreq == CILR_PEEK) {
381 lock = cl_lock_peek(env, io, &link->cill_descr, "io", io);
383 lock = ERR_PTR(-ENODATA);
385 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
388 link->cill_lock = lock;
389 cfs_list_move(&link->cill_linkage, &set->cls_curr);
390 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
391 result = cl_wait(env, lock);
393 cfs_list_move(&link->cill_linkage,
398 result = PTR_ERR(lock);
402 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
403 struct cl_io_lock_link *link)
405 struct cl_lock *lock = link->cill_lock;
408 cfs_list_del_init(&link->cill_linkage);
410 cl_lock_release(env, lock, "io", io);
411 link->cill_lock = NULL;
413 if (link->cill_fini != NULL)
414 link->cill_fini(env, link);
418 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
419 struct cl_lockset *set)
421 struct cl_io_lock_link *link;
422 struct cl_io_lock_link *temp;
423 struct cl_lock *lock;
428 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
429 if (!cl_lockset_match(set, &link->cill_descr)) {
430 /* XXX some locking to guarantee that locks aren't
431 * expanded in between. */
432 result = cl_lockset_lock_one(env, io, set, link);
436 cl_lock_link_fini(env, io, link);
439 cfs_list_for_each_entry_safe(link, temp,
440 &set->cls_curr, cill_linkage) {
441 lock = link->cill_lock;
442 result = cl_wait(env, lock);
444 cfs_list_move(&link->cill_linkage,
454 * Takes locks necessary for the current iteration of io.
456 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
457 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
460 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
462 const struct cl_io_slice *scan;
465 LINVRNT(cl_io_is_loopable(io));
466 LINVRNT(io->ci_state == CIS_IT_STARTED);
467 LINVRNT(cl_io_invariant(io));
470 cl_io_for_each(scan, io) {
471 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
473 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
478 cl_io_locks_sort(io);
479 result = cl_lockset_lock(env, io, &io->ci_lockset);
482 cl_io_unlock(env, io);
484 io->ci_state = CIS_LOCKED;
487 EXPORT_SYMBOL(cl_io_lock);
490 * Release locks takes by io.
492 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
494 struct cl_lockset *set;
495 struct cl_io_lock_link *link;
496 struct cl_io_lock_link *temp;
497 const struct cl_io_slice *scan;
499 LASSERT(cl_io_is_loopable(io));
500 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
501 LINVRNT(cl_io_invariant(io));
504 set = &io->ci_lockset;
506 cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
507 cl_lock_link_fini(env, io, link);
509 cfs_list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
510 cl_lock_link_fini(env, io, link);
512 cfs_list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
513 cl_unuse(env, link->cill_lock);
514 cl_lock_link_fini(env, io, link);
516 cl_io_for_each_reverse(scan, io) {
517 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
518 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
520 io->ci_state = CIS_UNLOCKED;
521 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
524 EXPORT_SYMBOL(cl_io_unlock);
527 * Prepares next iteration of io.
529 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
530 * layers a chance to modify io parameters, e.g., so that lov can restrict io
531 * to a single stripe.
533 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
535 const struct cl_io_slice *scan;
538 LINVRNT(cl_io_is_loopable(io));
539 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
540 LINVRNT(cl_io_invariant(io));
544 cl_io_for_each(scan, io) {
545 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
547 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
553 io->ci_state = CIS_IT_STARTED;
556 EXPORT_SYMBOL(cl_io_iter_init);
559 * Finalizes io iteration.
561 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
563 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
565 const struct cl_io_slice *scan;
567 LINVRNT(cl_io_is_loopable(io));
568 LINVRNT(io->ci_state == CIS_UNLOCKED);
569 LINVRNT(cl_io_invariant(io));
572 cl_io_for_each_reverse(scan, io) {
573 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
574 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
576 io->ci_state = CIS_IT_ENDED;
579 EXPORT_SYMBOL(cl_io_iter_fini);
582 * Records that read or write io progressed \a nob bytes forward.
584 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
586 const struct cl_io_slice *scan;
588 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
590 LINVRNT(cl_io_is_loopable(io));
591 LINVRNT(cl_io_invariant(io));
595 io->u.ci_rw.crw_pos += nob;
596 io->u.ci_rw.crw_count -= nob;
598 /* layers have to be notified. */
599 cl_io_for_each_reverse(scan, io) {
600 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
601 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
606 EXPORT_SYMBOL(cl_io_rw_advance);
609 * Adds a lock to a lockset.
611 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
612 struct cl_io_lock_link *link)
617 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
620 cfs_list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
625 EXPORT_SYMBOL(cl_io_lock_add);
627 static void cl_free_io_lock_link(const struct lu_env *env,
628 struct cl_io_lock_link *link)
634 * Allocates new lock link, and uses it to add a lock to a lockset.
636 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
637 struct cl_lock_descr *descr)
639 struct cl_io_lock_link *link;
645 link->cill_descr = *descr;
646 link->cill_fini = cl_free_io_lock_link;
647 result = cl_io_lock_add(env, io, link);
648 if (result) /* lock match */
649 link->cill_fini(env, link);
655 EXPORT_SYMBOL(cl_io_lock_alloc_add);
658 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
660 int cl_io_start(const struct lu_env *env, struct cl_io *io)
662 const struct cl_io_slice *scan;
665 LINVRNT(cl_io_is_loopable(io));
666 LINVRNT(io->ci_state == CIS_LOCKED);
667 LINVRNT(cl_io_invariant(io));
670 io->ci_state = CIS_IO_GOING;
671 cl_io_for_each(scan, io) {
672 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
674 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
682 EXPORT_SYMBOL(cl_io_start);
685 * Wait until current io iteration is finished by calling
686 * cl_io_operations::cio_end() bottom-to-top.
688 void cl_io_end(const struct lu_env *env, struct cl_io *io)
690 const struct cl_io_slice *scan;
692 LINVRNT(cl_io_is_loopable(io));
693 LINVRNT(io->ci_state == CIS_IO_GOING);
694 LINVRNT(cl_io_invariant(io));
697 cl_io_for_each_reverse(scan, io) {
698 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
699 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
700 /* TODO: error handling. */
702 io->ci_state = CIS_IO_FINISHED;
705 EXPORT_SYMBOL(cl_io_end);
707 static const struct cl_page_slice *
708 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
710 const struct cl_page_slice *slice;
712 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
713 LINVRNT(slice != NULL);
718 * True iff \a page is within \a io range.
720 static int cl_page_in_io(const struct cl_page *page, const struct cl_io *io)
727 idx = page->cp_index;
728 switch (io->ci_type) {
732 * check that [start, end) and [pos, pos + count) extents
735 if (!cl_io_is_append(io)) {
736 const struct cl_io_rw_common *crw = &(io->u.ci_rw);
737 start = cl_offset(page->cp_obj, idx);
738 end = cl_offset(page->cp_obj, idx + 1);
739 result = crw->crw_pos < end &&
740 start < crw->crw_pos + crw->crw_count;
744 result = io->u.ci_fault.ft_index == idx;
753 * Called by read io, when page has to be read from the server.
755 * \see cl_io_operations::cio_read_page()
757 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
758 struct cl_page *page)
760 const struct cl_io_slice *scan;
761 struct cl_2queue *queue;
764 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
765 LINVRNT(cl_page_is_owned(page, io));
766 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
767 LINVRNT(cl_page_in_io(page, io));
768 LINVRNT(cl_io_invariant(io));
771 queue = &io->ci_queue;
773 cl_2queue_init(queue);
775 * ->cio_read_page() methods called in the loop below are supposed to
776 * never block waiting for network (the only subtle point is the
777 * creation of new pages for read-ahead that might result in cache
778 * shrinking, but currently only clean pages are shrunk and this
779 * requires no network io).
781 * Should this ever starts blocking, retry loop would be needed for
782 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
784 cl_io_for_each(scan, io) {
785 if (scan->cis_iop->cio_read_page != NULL) {
786 const struct cl_page_slice *slice;
788 slice = cl_io_slice_page(scan, page);
789 LINVRNT(slice != NULL);
790 result = scan->cis_iop->cio_read_page(env, scan, slice);
796 result = cl_io_submit_rw(env, io, CRT_READ, queue, CRP_NORMAL);
798 * Unlock unsent pages in case of error.
800 cl_page_list_disown(env, io, &queue->c2_qin);
801 cl_2queue_fini(env, queue);
804 EXPORT_SYMBOL(cl_io_read_page);
807 * Called by write io to prepare page to receive data from user buffer.
809 * \see cl_io_operations::cio_prepare_write()
811 int cl_io_prepare_write(const struct lu_env *env, struct cl_io *io,
812 struct cl_page *page, unsigned from, unsigned to)
814 const struct cl_io_slice *scan;
817 LINVRNT(io->ci_type == CIT_WRITE);
818 LINVRNT(cl_page_is_owned(page, io));
819 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
820 LINVRNT(cl_io_invariant(io));
821 LASSERT(cl_page_in_io(page, io));
824 cl_io_for_each_reverse(scan, io) {
825 if (scan->cis_iop->cio_prepare_write != NULL) {
826 const struct cl_page_slice *slice;
828 slice = cl_io_slice_page(scan, page);
829 result = scan->cis_iop->cio_prepare_write(env, scan,
838 EXPORT_SYMBOL(cl_io_prepare_write);
841 * Called by write io after user data were copied into a page.
843 * \see cl_io_operations::cio_commit_write()
845 int cl_io_commit_write(const struct lu_env *env, struct cl_io *io,
846 struct cl_page *page, unsigned from, unsigned to)
848 const struct cl_io_slice *scan;
851 LINVRNT(io->ci_type == CIT_WRITE);
852 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
853 LINVRNT(cl_io_invariant(io));
855 * XXX Uh... not nice. Top level cl_io_commit_write() call (vvp->lov)
856 * already called cl_page_cache_add(), moving page into CPS_CACHED
857 * state. Better (and more general) way of dealing with such situation
860 LASSERT(cl_page_is_owned(page, io) || page->cp_parent != NULL);
861 LASSERT(cl_page_in_io(page, io));
864 cl_io_for_each(scan, io) {
865 if (scan->cis_iop->cio_commit_write != NULL) {
866 const struct cl_page_slice *slice;
868 slice = cl_io_slice_page(scan, page);
869 result = scan->cis_iop->cio_commit_write(env, scan,
876 LINVRNT(result <= 0);
879 EXPORT_SYMBOL(cl_io_commit_write);
882 * Submits a list of pages for immediate io.
884 * After the function gets returned, The submitted pages are moved to
885 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
886 * to be submitted, and the pages are errant to submit.
888 * \returns 0 if at least one page was submitted, error code otherwise.
889 * \see cl_io_operations::cio_submit()
891 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
892 enum cl_req_type crt, struct cl_2queue *queue,
893 enum cl_req_priority priority)
895 const struct cl_io_slice *scan;
898 LINVRNT(crt < ARRAY_SIZE(scan->cis_iop->req_op));
901 cl_io_for_each(scan, io) {
902 if (scan->cis_iop->req_op[crt].cio_submit == NULL)
904 result = scan->cis_iop->req_op[crt].cio_submit(env, scan, crt,
910 * If ->cio_submit() failed, no pages were sent.
912 LASSERT(ergo(result != 0, cfs_list_empty(&queue->c2_qout.pl_pages)));
915 EXPORT_SYMBOL(cl_io_submit_rw);
918 * Submit a sync_io and wait for the IO to be finished, or error happens.
919 * If \a timeout is zero, it means to wait for the IO unconditionally.
921 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
922 enum cl_req_type iot, struct cl_2queue *queue,
923 enum cl_req_priority prio, long timeout)
925 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
929 LASSERT(prio == CRP_NORMAL || prio == CRP_CANCEL);
931 cl_page_list_for_each(pg, &queue->c2_qin) {
932 LASSERT(pg->cp_sync_io == NULL);
933 pg->cp_sync_io = anchor;
936 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
937 rc = cl_io_submit_rw(env, io, iot, queue, prio);
940 * If some pages weren't sent for any reason (e.g.,
941 * read found up-to-date pages in the cache, or write found
942 * clean pages), count them as completed to avoid infinite
945 cl_page_list_for_each(pg, &queue->c2_qin) {
946 pg->cp_sync_io = NULL;
947 cl_sync_io_note(anchor, +1);
950 /* wait for the IO to be finished. */
951 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
954 LASSERT(cfs_list_empty(&queue->c2_qout.pl_pages));
955 cl_page_list_for_each(pg, &queue->c2_qin)
956 pg->cp_sync_io = NULL;
960 EXPORT_SYMBOL(cl_io_submit_sync);
963 * Cancel an IO which has been submitted by cl_io_submit_rw.
965 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
966 struct cl_page_list *queue)
968 struct cl_page *page;
971 CERROR("Canceling ongoing page trasmission\n");
972 cl_page_list_for_each(page, queue) {
975 LINVRNT(cl_page_in_io(page, io));
976 rc = cl_page_cancel(env, page);
977 result = result ?: rc;
981 EXPORT_SYMBOL(cl_io_cancel);
986 * Pumps io through iterations calling
988 * - cl_io_iter_init()
998 * - cl_io_iter_fini()
1000 * repeatedly until there is no more io to do.
1002 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
1006 LINVRNT(cl_io_is_loopable(io));
1012 io->ci_continue = 0;
1013 result = cl_io_iter_init(env, io);
1016 result = cl_io_lock(env, io);
1019 * Notify layers that locks has been taken,
1020 * and do actual i/o.
1022 * - llite: kms, short read;
1023 * - llite: generic_file_read();
1025 result = cl_io_start(env, io);
1027 * Send any remaining pending
1030 * - llite: ll_rw_stats_tally.
1033 cl_io_unlock(env, io);
1034 cl_io_rw_advance(env, io, io->ci_nob - nob);
1037 cl_io_iter_fini(env, io);
1038 } while (result == 0 && io->ci_continue);
1039 RETURN(result < 0 ? result : 0);
1041 EXPORT_SYMBOL(cl_io_loop);
1044 * Adds io slice to the cl_io.
1046 * This is called by cl_object_operations::coo_io_init() methods to add a
1047 * per-layer state to the io. New state is added at the end of
1048 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
1050 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
1052 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
1053 struct cl_object *obj,
1054 const struct cl_io_operations *ops)
1056 cfs_list_t *linkage = &slice->cis_linkage;
1058 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
1059 cfs_list_empty(linkage));
1062 cfs_list_add_tail(linkage, &io->ci_layers);
1064 slice->cis_obj = obj;
1065 slice->cis_iop = ops;
1068 EXPORT_SYMBOL(cl_io_slice_add);
1072 * Initializes page list.
1074 void cl_page_list_init(struct cl_page_list *plist)
1078 CFS_INIT_LIST_HEAD(&plist->pl_pages);
1079 plist->pl_owner = cfs_current();
1082 EXPORT_SYMBOL(cl_page_list_init);
1085 * Adds a page to a page list.
1087 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1090 /* it would be better to check that page is owned by "current" io, but
1091 * it is not passed here. */
1092 LASSERT(page->cp_owner != NULL);
1093 LINVRNT(plist->pl_owner == cfs_current());
1096 cfs_mutex_lock(&page->cp_mutex);
1098 LASSERT(cfs_list_empty(&page->cp_batch));
1099 cfs_list_add_tail(&page->cp_batch, &plist->pl_pages);
1101 page->cp_queue_ref = lu_ref_add(&page->cp_reference, "queue", plist);
1105 EXPORT_SYMBOL(cl_page_list_add);
1108 * Removes a page from a page list.
1110 void cl_page_list_del(const struct lu_env *env,
1111 struct cl_page_list *plist, struct cl_page *page)
1113 LASSERT(plist->pl_nr > 0);
1114 LINVRNT(plist->pl_owner == cfs_current());
1117 cfs_list_del_init(&page->cp_batch);
1119 cfs_mutex_unlock(&page->cp_mutex);
1122 lu_ref_del_at(&page->cp_reference, page->cp_queue_ref, "queue", plist);
1123 cl_page_put(env, page);
1126 EXPORT_SYMBOL(cl_page_list_del);
1129 * Moves a page from one page list to another.
1131 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1132 struct cl_page *page)
1134 LASSERT(src->pl_nr > 0);
1135 LINVRNT(dst->pl_owner == cfs_current());
1136 LINVRNT(src->pl_owner == cfs_current());
1139 cfs_list_move_tail(&page->cp_batch, &dst->pl_pages);
1142 lu_ref_set_at(&page->cp_reference,
1143 page->cp_queue_ref, "queue", src, dst);
1146 EXPORT_SYMBOL(cl_page_list_move);
1149 * splice the cl_page_list, just as list head does
1151 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1153 struct cl_page *page;
1154 struct cl_page *tmp;
1156 LINVRNT(list->pl_owner == cfs_current());
1157 LINVRNT(head->pl_owner == cfs_current());
1160 cl_page_list_for_each_safe(page, tmp, list)
1161 cl_page_list_move(head, list, page);
1164 EXPORT_SYMBOL(cl_page_list_splice);
1166 void cl_page_disown0(const struct lu_env *env,
1167 struct cl_io *io, struct cl_page *pg);
1170 * Disowns pages in a queue.
1172 void cl_page_list_disown(const struct lu_env *env,
1173 struct cl_io *io, struct cl_page_list *plist)
1175 struct cl_page *page;
1176 struct cl_page *temp;
1178 LINVRNT(plist->pl_owner == cfs_current());
1181 cl_page_list_for_each_safe(page, temp, plist) {
1182 LASSERT(plist->pl_nr > 0);
1184 cfs_list_del_init(&page->cp_batch);
1186 cfs_mutex_unlock(&page->cp_mutex);
1190 * cl_page_disown0 rather than usual cl_page_disown() is used,
1191 * because pages are possibly in CPS_FREEING state already due
1192 * to the call to cl_page_list_discard().
1195 * XXX cl_page_disown0() will fail if page is not locked.
1197 cl_page_disown0(env, io, page);
1198 lu_ref_del(&page->cp_reference, "queue", plist);
1199 cl_page_put(env, page);
1203 EXPORT_SYMBOL(cl_page_list_disown);
1206 * Releases pages from queue.
1208 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1210 struct cl_page *page;
1211 struct cl_page *temp;
1213 LINVRNT(plist->pl_owner == cfs_current());
1216 cl_page_list_for_each_safe(page, temp, plist)
1217 cl_page_list_del(env, plist, page);
1218 LASSERT(plist->pl_nr == 0);
1221 EXPORT_SYMBOL(cl_page_list_fini);
1224 * Owns all pages in a queue.
1226 int cl_page_list_own(const struct lu_env *env,
1227 struct cl_io *io, struct cl_page_list *plist)
1229 struct cl_page *page;
1230 struct cl_page *temp;
1234 LINVRNT(plist->pl_owner == cfs_current());
1238 cl_page_list_for_each_safe(page, temp, plist) {
1239 LASSERT(index <= page->cp_index);
1240 index = page->cp_index;
1241 if (cl_page_own(env, io, page) == 0)
1242 result = result ?: page->cp_error;
1244 cl_page_list_del(env, plist, page);
1248 EXPORT_SYMBOL(cl_page_list_own);
1251 * Assumes all pages in a queue.
1253 void cl_page_list_assume(const struct lu_env *env,
1254 struct cl_io *io, struct cl_page_list *plist)
1256 struct cl_page *page;
1258 LINVRNT(plist->pl_owner == cfs_current());
1260 cl_page_list_for_each(page, plist)
1261 cl_page_assume(env, io, page);
1263 EXPORT_SYMBOL(cl_page_list_assume);
1266 * Discards all pages in a queue.
1268 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1269 struct cl_page_list *plist)
1271 struct cl_page *page;
1273 LINVRNT(plist->pl_owner == cfs_current());
1275 cl_page_list_for_each(page, plist)
1276 cl_page_discard(env, io, page);
1279 EXPORT_SYMBOL(cl_page_list_discard);
1282 * Unmaps all pages in a queue from user virtual memory.
1284 int cl_page_list_unmap(const struct lu_env *env, struct cl_io *io,
1285 struct cl_page_list *plist)
1287 struct cl_page *page;
1290 LINVRNT(plist->pl_owner == cfs_current());
1293 cl_page_list_for_each(page, plist) {
1294 result = cl_page_unmap(env, io, page);
1300 EXPORT_SYMBOL(cl_page_list_unmap);
1303 * Initialize dual page queue.
1305 void cl_2queue_init(struct cl_2queue *queue)
1308 cl_page_list_init(&queue->c2_qin);
1309 cl_page_list_init(&queue->c2_qout);
1312 EXPORT_SYMBOL(cl_2queue_init);
1315 * Add a page to the incoming page list of 2-queue.
1317 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1320 cl_page_list_add(&queue->c2_qin, page);
1323 EXPORT_SYMBOL(cl_2queue_add);
1326 * Disown pages in both lists of a 2-queue.
1328 void cl_2queue_disown(const struct lu_env *env,
1329 struct cl_io *io, struct cl_2queue *queue)
1332 cl_page_list_disown(env, io, &queue->c2_qin);
1333 cl_page_list_disown(env, io, &queue->c2_qout);
1336 EXPORT_SYMBOL(cl_2queue_disown);
1339 * Discard (truncate) pages in both lists of a 2-queue.
1341 void cl_2queue_discard(const struct lu_env *env,
1342 struct cl_io *io, struct cl_2queue *queue)
1345 cl_page_list_discard(env, io, &queue->c2_qin);
1346 cl_page_list_discard(env, io, &queue->c2_qout);
1349 EXPORT_SYMBOL(cl_2queue_discard);
1352 * Assume to own the pages in cl_2queue
1354 void cl_2queue_assume(const struct lu_env *env,
1355 struct cl_io *io, struct cl_2queue *queue)
1357 cl_page_list_assume(env, io, &queue->c2_qin);
1358 cl_page_list_assume(env, io, &queue->c2_qout);
1360 EXPORT_SYMBOL(cl_2queue_assume);
1363 * Finalize both page lists of a 2-queue.
1365 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1368 cl_page_list_fini(env, &queue->c2_qout);
1369 cl_page_list_fini(env, &queue->c2_qin);
1372 EXPORT_SYMBOL(cl_2queue_fini);
1375 * Initialize a 2-queue to contain \a page in its incoming page list.
1377 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1380 cl_2queue_init(queue);
1381 cl_2queue_add(queue, page);
1384 EXPORT_SYMBOL(cl_2queue_init_page);
1387 * Returns top-level io.
1389 * \see cl_object_top(), cl_page_top().
1391 struct cl_io *cl_io_top(struct cl_io *io)
1394 while (io->ci_parent != NULL)
1398 EXPORT_SYMBOL(cl_io_top);
1401 * Prints human readable representation of \a io to the \a f.
1403 void cl_io_print(const struct lu_env *env, void *cookie,
1404 lu_printer_t printer, const struct cl_io *io)
1409 * Adds request slice to the compound request.
1411 * This is called by cl_device_operations::cdo_req_init() methods to add a
1412 * per-layer state to the request. New state is added at the end of
1413 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1415 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1417 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1418 struct cl_device *dev,
1419 const struct cl_req_operations *ops)
1422 cfs_list_add_tail(&slice->crs_linkage, &req->crq_layers);
1423 slice->crs_dev = dev;
1424 slice->crs_ops = ops;
1425 slice->crs_req = req;
1428 EXPORT_SYMBOL(cl_req_slice_add);
1430 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1434 LASSERT(cfs_list_empty(&req->crq_pages));
1435 LASSERT(req->crq_nrpages == 0);
1436 LINVRNT(cfs_list_empty(&req->crq_layers));
1437 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1440 if (req->crq_o != NULL) {
1441 for (i = 0; i < req->crq_nrobjs; ++i) {
1442 struct cl_object *obj = req->crq_o[i].ro_obj;
1444 lu_object_ref_del_at(&obj->co_lu,
1445 req->crq_o[i].ro_obj_ref,
1447 cl_object_put(env, obj);
1450 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1456 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1457 struct cl_page *page)
1459 struct cl_device *dev;
1460 struct cl_page_slice *slice;
1465 page = cl_page_top(page);
1467 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1468 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1469 if (dev->cd_ops->cdo_req_init != NULL) {
1470 result = dev->cd_ops->cdo_req_init(env,
1476 page = page->cp_child;
1477 } while (page != NULL && result == 0);
1482 * Invokes per-request transfer completion call-backs
1483 * (cl_req_operations::cro_completion()) bottom-to-top.
1485 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1487 struct cl_req_slice *slice;
1491 * for the lack of list_for_each_entry_reverse_safe()...
1493 while (!cfs_list_empty(&req->crq_layers)) {
1494 slice = cfs_list_entry(req->crq_layers.prev,
1495 struct cl_req_slice, crs_linkage);
1496 cfs_list_del_init(&slice->crs_linkage);
1497 if (slice->crs_ops->cro_completion != NULL)
1498 slice->crs_ops->cro_completion(env, slice, rc);
1500 cl_req_free(env, req);
1503 EXPORT_SYMBOL(cl_req_completion);
1506 * Allocates new transfer request.
1508 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1509 enum cl_req_type crt, int nr_objects)
1513 LINVRNT(nr_objects > 0);
1520 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1521 if (req->crq_o != NULL) {
1522 req->crq_nrobjs = nr_objects;
1523 req->crq_type = crt;
1524 CFS_INIT_LIST_HEAD(&req->crq_pages);
1525 CFS_INIT_LIST_HEAD(&req->crq_layers);
1526 result = cl_req_init(env, req, page);
1530 cl_req_completion(env, req, result);
1531 req = ERR_PTR(result);
1534 req = ERR_PTR(-ENOMEM);
1537 EXPORT_SYMBOL(cl_req_alloc);
1540 * Adds a page to a request.
1542 void cl_req_page_add(const struct lu_env *env,
1543 struct cl_req *req, struct cl_page *page)
1545 struct cl_object *obj;
1546 struct cl_req_obj *rqo;
1550 page = cl_page_top(page);
1552 LASSERT(cfs_list_empty(&page->cp_flight));
1553 LASSERT(page->cp_req == NULL);
1555 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1556 req, req->crq_type, req->crq_nrpages);
1558 cfs_list_add_tail(&page->cp_flight, &req->crq_pages);
1561 obj = cl_object_top(page->cp_obj);
1562 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1563 if (rqo->ro_obj == NULL) {
1566 rqo->ro_obj_ref = lu_object_ref_add(&obj->co_lu,
1571 LASSERT(i < req->crq_nrobjs);
1574 EXPORT_SYMBOL(cl_req_page_add);
1577 * Removes a page from a request.
1579 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1581 struct cl_req *req = page->cp_req;
1584 page = cl_page_top(page);
1586 LASSERT(!cfs_list_empty(&page->cp_flight));
1587 LASSERT(req->crq_nrpages > 0);
1589 cfs_list_del_init(&page->cp_flight);
1591 page->cp_req = NULL;
1594 EXPORT_SYMBOL(cl_req_page_done);
1597 * Notifies layers that request is about to depart by calling
1598 * cl_req_operations::cro_prep() top-to-bottom.
1600 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1604 const struct cl_req_slice *slice;
1608 * Check that the caller of cl_req_alloc() didn't lie about the number
1611 for (i = 0; i < req->crq_nrobjs; ++i)
1612 LASSERT(req->crq_o[i].ro_obj != NULL);
1615 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1616 if (slice->crs_ops->cro_prep != NULL) {
1617 result = slice->crs_ops->cro_prep(env, slice);
1624 EXPORT_SYMBOL(cl_req_prep);
1627 * Fills in attributes that are passed to server together with transfer. Only
1628 * attributes from \a flags may be touched. This can be called multiple times
1629 * for the same request.
1631 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1632 struct cl_req_attr *attr, obd_valid flags)
1634 const struct cl_req_slice *slice;
1635 struct cl_page *page;
1638 LASSERT(!cfs_list_empty(&req->crq_pages));
1641 /* Take any page to use as a model. */
1642 page = cfs_list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1644 for (i = 0; i < req->crq_nrobjs; ++i) {
1645 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1646 const struct cl_page_slice *scan;
1647 const struct cl_object *obj;
1649 scan = cl_page_at(page,
1650 slice->crs_dev->cd_lu_dev.ld_type);
1651 LASSERT(scan != NULL);
1652 obj = scan->cpl_obj;
1653 if (slice->crs_ops->cro_attr_set != NULL)
1654 slice->crs_ops->cro_attr_set(env, slice, obj,
1660 EXPORT_SYMBOL(cl_req_attr_set);
1662 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1663 * implemented in libcfs. */
1665 # include <linux/sched.h>
1666 #else /* __KERNEL__ */
1667 # include <liblustre.h>
1671 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1673 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1676 cfs_waitq_init(&anchor->csi_waitq);
1677 cfs_atomic_set(&anchor->csi_sync_nr, nrpages);
1678 anchor->csi_sync_rc = 0;
1681 EXPORT_SYMBOL(cl_sync_io_init);
1684 * Wait until all transfer completes. Transfer completion routine has to call
1685 * cl_sync_io_note() for every page.
1687 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1688 struct cl_page_list *queue, struct cl_sync_io *anchor,
1691 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1696 LASSERT(timeout >= 0);
1698 rc = l_wait_event(anchor->csi_waitq,
1699 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1702 CERROR("SYNC IO failed with error: %d, try to cancel "
1703 "%d remaining pages\n",
1704 rc, cfs_atomic_read(&anchor->csi_sync_nr));
1706 (void)cl_io_cancel(env, io, queue);
1708 lwi = (struct l_wait_info) { 0 };
1709 (void)l_wait_event(anchor->csi_waitq,
1710 cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1713 rc = anchor->csi_sync_rc;
1715 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) == 0);
1716 cl_page_list_assume(env, io, queue);
1717 POISON(anchor, 0x5a, sizeof *anchor);
1720 EXPORT_SYMBOL(cl_sync_io_wait);
1723 * Indicate that transfer of a single page completed.
1725 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1728 if (anchor->csi_sync_rc == 0 && ioret < 0)
1729 anchor->csi_sync_rc = ioret;
1731 * Synchronous IO done without releasing page lock (e.g., as a part of
1732 * ->{prepare,commit}_write(). Completion is used to signal the end of
1735 LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) > 0);
1736 if (cfs_atomic_dec_and_test(&anchor->csi_sync_nr))
1737 cfs_waitq_broadcast(&anchor->csi_waitq);
1740 EXPORT_SYMBOL(cl_sync_io_note);