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 static void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io,
576 const struct cl_io_slice *scan;
578 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
580 LINVRNT(cl_io_is_loopable(io));
581 LINVRNT(cl_io_invariant(io));
583 io->u.ci_rw.crw_pos += nob;
584 io->u.ci_rw.crw_count -= nob;
586 /* layers have to be notified. */
587 cl_io_for_each_reverse(scan, io) {
588 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
589 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
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;
627 link = kzalloc(sizeof(*link), GFP_NOFS);
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);
719 start = cl_offset(page->cp_obj, idx);
720 end = cl_offset(page->cp_obj, idx + 1);
721 result = crw->crw_pos < end &&
722 start < crw->crw_pos + crw->crw_count;
726 result = io->u.ci_fault.ft_index == idx;
735 * Called by read io, when page has to be read from the server.
737 * \see cl_io_operations::cio_read_page()
739 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
740 struct cl_page *page)
742 const struct cl_io_slice *scan;
743 struct cl_2queue *queue;
746 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
747 LINVRNT(cl_page_is_owned(page, io));
748 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
749 LINVRNT(cl_page_in_io(page, io));
750 LINVRNT(cl_io_invariant(io));
752 queue = &io->ci_queue;
754 cl_2queue_init(queue);
756 * ->cio_read_page() methods called in the loop below are supposed to
757 * never block waiting for network (the only subtle point is the
758 * creation of new pages for read-ahead that might result in cache
759 * shrinking, but currently only clean pages are shrunk and this
760 * requires no network io).
762 * Should this ever starts blocking, retry loop would be needed for
763 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
765 cl_io_for_each(scan, io) {
766 if (scan->cis_iop->cio_read_page != NULL) {
767 const struct cl_page_slice *slice;
769 slice = cl_io_slice_page(scan, page);
770 LINVRNT(slice != NULL);
771 result = scan->cis_iop->cio_read_page(env, scan, slice);
777 result = cl_io_submit_rw(env, io, CRT_READ, queue);
779 * Unlock unsent pages in case of error.
781 cl_page_list_disown(env, io, &queue->c2_qin);
782 cl_2queue_fini(env, queue);
785 EXPORT_SYMBOL(cl_io_read_page);
788 * Called by write io to prepare page to receive data from user buffer.
790 * \see cl_io_operations::cio_prepare_write()
792 int cl_io_prepare_write(const struct lu_env *env, struct cl_io *io,
793 struct cl_page *page, unsigned from, unsigned to)
795 const struct cl_io_slice *scan;
798 LINVRNT(io->ci_type == CIT_WRITE);
799 LINVRNT(cl_page_is_owned(page, io));
800 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
801 LINVRNT(cl_io_invariant(io));
802 LASSERT(cl_page_in_io(page, io));
804 cl_io_for_each_reverse(scan, io) {
805 if (scan->cis_iop->cio_prepare_write != NULL) {
806 const struct cl_page_slice *slice;
808 slice = cl_io_slice_page(scan, page);
809 result = scan->cis_iop->cio_prepare_write(env, scan,
818 EXPORT_SYMBOL(cl_io_prepare_write);
821 * Called by write io after user data were copied into a page.
823 * \see cl_io_operations::cio_commit_write()
825 int cl_io_commit_write(const struct lu_env *env, struct cl_io *io,
826 struct cl_page *page, unsigned from, unsigned to)
828 const struct cl_io_slice *scan;
831 LINVRNT(io->ci_type == CIT_WRITE);
832 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
833 LINVRNT(cl_io_invariant(io));
835 * XXX Uh... not nice. Top level cl_io_commit_write() call (vvp->lov)
836 * already called cl_page_cache_add(), moving page into CPS_CACHED
837 * state. Better (and more general) way of dealing with such situation
840 LASSERT(cl_page_is_owned(page, io) || page->cp_parent != NULL);
841 LASSERT(cl_page_in_io(page, io));
843 cl_io_for_each(scan, io) {
844 if (scan->cis_iop->cio_commit_write != NULL) {
845 const struct cl_page_slice *slice;
847 slice = cl_io_slice_page(scan, page);
848 result = scan->cis_iop->cio_commit_write(env, scan,
855 LINVRNT(result <= 0);
858 EXPORT_SYMBOL(cl_io_commit_write);
861 * Submits a list of pages for immediate io.
863 * After the function gets returned, The submitted pages are moved to
864 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
865 * to be submitted, and the pages are errant to submit.
867 * \returns 0 if at least one page was submitted, error code otherwise.
868 * \see cl_io_operations::cio_submit()
870 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
871 enum cl_req_type crt, struct cl_2queue *queue)
873 const struct cl_io_slice *scan;
876 LINVRNT(crt < ARRAY_SIZE(scan->cis_iop->req_op));
878 cl_io_for_each(scan, io) {
879 if (scan->cis_iop->req_op[crt].cio_submit == NULL)
881 result = scan->cis_iop->req_op[crt].cio_submit(env, scan, crt,
887 * If ->cio_submit() failed, no pages were sent.
889 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
892 EXPORT_SYMBOL(cl_io_submit_rw);
895 * Submit a sync_io and wait for the IO to be finished, or error happens.
896 * If \a timeout is zero, it means to wait for the IO unconditionally.
898 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
899 enum cl_req_type iot, struct cl_2queue *queue,
902 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
906 cl_page_list_for_each(pg, &queue->c2_qin) {
907 LASSERT(pg->cp_sync_io == NULL);
908 pg->cp_sync_io = anchor;
911 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
912 rc = cl_io_submit_rw(env, io, iot, queue);
915 * If some pages weren't sent for any reason (e.g.,
916 * read found up-to-date pages in the cache, or write found
917 * clean pages), count them as completed to avoid infinite
920 cl_page_list_for_each(pg, &queue->c2_qin) {
921 pg->cp_sync_io = NULL;
922 cl_sync_io_note(anchor, 1);
925 /* wait for the IO to be finished. */
926 rc = cl_sync_io_wait(env, io, &queue->c2_qout,
929 LASSERT(list_empty(&queue->c2_qout.pl_pages));
930 cl_page_list_for_each(pg, &queue->c2_qin)
931 pg->cp_sync_io = NULL;
935 EXPORT_SYMBOL(cl_io_submit_sync);
938 * Cancel an IO which has been submitted by cl_io_submit_rw.
940 static int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
941 struct cl_page_list *queue)
943 struct cl_page *page;
946 CERROR("Canceling ongoing page transmission\n");
947 cl_page_list_for_each(page, queue) {
950 LINVRNT(cl_page_in_io(page, io));
951 rc = cl_page_cancel(env, page);
952 result = result ?: rc;
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);
1044 * Initializes page list.
1046 void cl_page_list_init(struct cl_page_list *plist)
1049 INIT_LIST_HEAD(&plist->pl_pages);
1050 plist->pl_owner = current;
1052 EXPORT_SYMBOL(cl_page_list_init);
1055 * Adds a page to a page list.
1057 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1059 /* it would be better to check that page is owned by "current" io, but
1060 * it is not passed here. */
1061 LASSERT(page->cp_owner != NULL);
1062 LINVRNT(plist->pl_owner == current);
1065 mutex_lock(&page->cp_mutex);
1067 LASSERT(list_empty(&page->cp_batch));
1068 list_add_tail(&page->cp_batch, &plist->pl_pages);
1070 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1073 EXPORT_SYMBOL(cl_page_list_add);
1076 * Removes a page from a page list.
1078 static void cl_page_list_del(const struct lu_env *env,
1079 struct cl_page_list *plist, struct cl_page *page)
1081 LASSERT(plist->pl_nr > 0);
1082 LINVRNT(plist->pl_owner == current);
1084 list_del_init(&page->cp_batch);
1086 mutex_unlock(&page->cp_mutex);
1089 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1090 cl_page_put(env, page);
1094 * Moves a page from one page list to another.
1096 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1097 struct cl_page *page)
1099 LASSERT(src->pl_nr > 0);
1100 LINVRNT(dst->pl_owner == current);
1101 LINVRNT(src->pl_owner == current);
1103 list_move_tail(&page->cp_batch, &dst->pl_pages);
1106 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1109 EXPORT_SYMBOL(cl_page_list_move);
1112 * splice the cl_page_list, just as list head does
1114 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1116 struct cl_page *page;
1117 struct cl_page *tmp;
1119 LINVRNT(list->pl_owner == current);
1120 LINVRNT(head->pl_owner == current);
1122 cl_page_list_for_each_safe(page, tmp, list)
1123 cl_page_list_move(head, list, page);
1125 EXPORT_SYMBOL(cl_page_list_splice);
1127 void cl_page_disown0(const struct lu_env *env,
1128 struct cl_io *io, struct cl_page *pg);
1131 * Disowns pages in a queue.
1133 void cl_page_list_disown(const struct lu_env *env,
1134 struct cl_io *io, struct cl_page_list *plist)
1136 struct cl_page *page;
1137 struct cl_page *temp;
1139 LINVRNT(plist->pl_owner == current);
1141 cl_page_list_for_each_safe(page, temp, plist) {
1142 LASSERT(plist->pl_nr > 0);
1144 list_del_init(&page->cp_batch);
1146 mutex_unlock(&page->cp_mutex);
1150 * cl_page_disown0 rather than usual cl_page_disown() is used,
1151 * because pages are possibly in CPS_FREEING state already due
1152 * to the call to cl_page_list_discard().
1155 * XXX cl_page_disown0() will fail if page is not locked.
1157 cl_page_disown0(env, io, page);
1158 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1160 cl_page_put(env, page);
1163 EXPORT_SYMBOL(cl_page_list_disown);
1166 * Releases pages from queue.
1168 static void cl_page_list_fini(const struct lu_env *env,
1169 struct cl_page_list *plist)
1171 struct cl_page *page;
1172 struct cl_page *temp;
1174 LINVRNT(plist->pl_owner == current);
1176 cl_page_list_for_each_safe(page, temp, plist)
1177 cl_page_list_del(env, plist, page);
1178 LASSERT(plist->pl_nr == 0);
1182 * Assumes all pages in a queue.
1184 static void cl_page_list_assume(const struct lu_env *env,
1185 struct cl_io *io, struct cl_page_list *plist)
1187 struct cl_page *page;
1189 LINVRNT(plist->pl_owner == current);
1191 cl_page_list_for_each(page, plist)
1192 cl_page_assume(env, io, page);
1196 * Discards all pages in a queue.
1198 static void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1199 struct cl_page_list *plist)
1201 struct cl_page *page;
1203 LINVRNT(plist->pl_owner == current);
1204 cl_page_list_for_each(page, plist)
1205 cl_page_discard(env, io, page);
1209 * Initialize dual page queue.
1211 void cl_2queue_init(struct cl_2queue *queue)
1213 cl_page_list_init(&queue->c2_qin);
1214 cl_page_list_init(&queue->c2_qout);
1216 EXPORT_SYMBOL(cl_2queue_init);
1219 * Add a page to the incoming page list of 2-queue.
1221 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1223 cl_page_list_add(&queue->c2_qin, page);
1225 EXPORT_SYMBOL(cl_2queue_add);
1228 * Disown pages in both lists of a 2-queue.
1230 void cl_2queue_disown(const struct lu_env *env,
1231 struct cl_io *io, struct cl_2queue *queue)
1233 cl_page_list_disown(env, io, &queue->c2_qin);
1234 cl_page_list_disown(env, io, &queue->c2_qout);
1236 EXPORT_SYMBOL(cl_2queue_disown);
1239 * Discard (truncate) pages in both lists of a 2-queue.
1241 void cl_2queue_discard(const struct lu_env *env,
1242 struct cl_io *io, struct cl_2queue *queue)
1244 cl_page_list_discard(env, io, &queue->c2_qin);
1245 cl_page_list_discard(env, io, &queue->c2_qout);
1247 EXPORT_SYMBOL(cl_2queue_discard);
1250 * Finalize both page lists of a 2-queue.
1252 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1254 cl_page_list_fini(env, &queue->c2_qout);
1255 cl_page_list_fini(env, &queue->c2_qin);
1257 EXPORT_SYMBOL(cl_2queue_fini);
1260 * Initialize a 2-queue to contain \a page in its incoming page list.
1262 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1264 cl_2queue_init(queue);
1265 cl_2queue_add(queue, page);
1267 EXPORT_SYMBOL(cl_2queue_init_page);
1270 * Returns top-level io.
1272 * \see cl_object_top(), cl_page_top().
1274 struct cl_io *cl_io_top(struct cl_io *io)
1276 while (io->ci_parent != NULL)
1280 EXPORT_SYMBOL(cl_io_top);
1283 * Adds request slice to the compound request.
1285 * This is called by cl_device_operations::cdo_req_init() methods to add a
1286 * per-layer state to the request. New state is added at the end of
1287 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1289 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1291 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1292 struct cl_device *dev,
1293 const struct cl_req_operations *ops)
1295 list_add_tail(&slice->crs_linkage, &req->crq_layers);
1296 slice->crs_dev = dev;
1297 slice->crs_ops = ops;
1298 slice->crs_req = req;
1300 EXPORT_SYMBOL(cl_req_slice_add);
1302 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1306 LASSERT(list_empty(&req->crq_pages));
1307 LASSERT(req->crq_nrpages == 0);
1308 LINVRNT(list_empty(&req->crq_layers));
1309 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1311 if (req->crq_o != NULL) {
1312 for (i = 0; i < req->crq_nrobjs; ++i) {
1313 struct cl_object *obj = req->crq_o[i].ro_obj;
1316 lu_object_ref_del_at(&obj->co_lu,
1317 &req->crq_o[i].ro_obj_ref,
1319 cl_object_put(env, obj);
1327 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1328 struct cl_page *page)
1330 struct cl_device *dev;
1331 struct cl_page_slice *slice;
1335 page = cl_page_top(page);
1337 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1338 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1339 if (dev->cd_ops->cdo_req_init != NULL) {
1340 result = dev->cd_ops->cdo_req_init(env,
1346 page = page->cp_child;
1347 } while (page != NULL && result == 0);
1352 * Invokes per-request transfer completion call-backs
1353 * (cl_req_operations::cro_completion()) bottom-to-top.
1355 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1357 struct cl_req_slice *slice;
1360 * for the lack of list_for_each_entry_reverse_safe()...
1362 while (!list_empty(&req->crq_layers)) {
1363 slice = list_entry(req->crq_layers.prev,
1364 struct cl_req_slice, crs_linkage);
1365 list_del_init(&slice->crs_linkage);
1366 if (slice->crs_ops->cro_completion != NULL)
1367 slice->crs_ops->cro_completion(env, slice, rc);
1369 cl_req_free(env, req);
1371 EXPORT_SYMBOL(cl_req_completion);
1374 * Allocates new transfer request.
1376 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1377 enum cl_req_type crt, int nr_objects)
1381 LINVRNT(nr_objects > 0);
1383 req = kzalloc(sizeof(*req), GFP_NOFS);
1387 req->crq_type = crt;
1388 INIT_LIST_HEAD(&req->crq_pages);
1389 INIT_LIST_HEAD(&req->crq_layers);
1391 req->crq_o = kcalloc(nr_objects, sizeof(req->crq_o[0]),
1393 if (req->crq_o != NULL) {
1394 req->crq_nrobjs = nr_objects;
1395 result = cl_req_init(env, req, page);
1399 cl_req_completion(env, req, result);
1400 req = ERR_PTR(result);
1403 req = ERR_PTR(-ENOMEM);
1406 EXPORT_SYMBOL(cl_req_alloc);
1409 * Adds a page to a request.
1411 void cl_req_page_add(const struct lu_env *env,
1412 struct cl_req *req, struct cl_page *page)
1414 struct cl_object *obj;
1415 struct cl_req_obj *rqo;
1418 page = cl_page_top(page);
1420 LASSERT(list_empty(&page->cp_flight));
1421 LASSERT(page->cp_req == NULL);
1423 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1424 req, req->crq_type, req->crq_nrpages);
1426 list_add_tail(&page->cp_flight, &req->crq_pages);
1429 obj = cl_object_top(page->cp_obj);
1430 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1431 if (rqo->ro_obj == NULL) {
1434 lu_object_ref_add_at(&obj->co_lu, &rqo->ro_obj_ref,
1439 LASSERT(i < req->crq_nrobjs);
1441 EXPORT_SYMBOL(cl_req_page_add);
1444 * Removes a page from a request.
1446 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1448 struct cl_req *req = page->cp_req;
1450 page = cl_page_top(page);
1452 LASSERT(!list_empty(&page->cp_flight));
1453 LASSERT(req->crq_nrpages > 0);
1455 list_del_init(&page->cp_flight);
1457 page->cp_req = NULL;
1459 EXPORT_SYMBOL(cl_req_page_done);
1462 * Notifies layers that request is about to depart by calling
1463 * cl_req_operations::cro_prep() top-to-bottom.
1465 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1469 const struct cl_req_slice *slice;
1472 * Check that the caller of cl_req_alloc() didn't lie about the number
1475 for (i = 0; i < req->crq_nrobjs; ++i)
1476 LASSERT(req->crq_o[i].ro_obj != NULL);
1479 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1480 if (slice->crs_ops->cro_prep != NULL) {
1481 result = slice->crs_ops->cro_prep(env, slice);
1488 EXPORT_SYMBOL(cl_req_prep);
1491 * Fills in attributes that are passed to server together with transfer. Only
1492 * attributes from \a flags may be touched. This can be called multiple times
1493 * for the same request.
1495 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1496 struct cl_req_attr *attr, u64 flags)
1498 const struct cl_req_slice *slice;
1499 struct cl_page *page;
1502 LASSERT(!list_empty(&req->crq_pages));
1504 /* Take any page to use as a model. */
1505 page = list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1507 for (i = 0; i < req->crq_nrobjs; ++i) {
1508 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1509 const struct cl_page_slice *scan;
1510 const struct cl_object *obj;
1512 scan = cl_page_at(page,
1513 slice->crs_dev->cd_lu_dev.ld_type);
1514 LASSERT(scan != NULL);
1515 obj = scan->cpl_obj;
1516 if (slice->crs_ops->cro_attr_set != NULL)
1517 slice->crs_ops->cro_attr_set(env, slice, obj,
1522 EXPORT_SYMBOL(cl_req_attr_set);
1524 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1525 * implemented in libcfs. */
1526 # include <linux/sched.h>
1529 * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1531 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1533 init_waitqueue_head(&anchor->csi_waitq);
1534 atomic_set(&anchor->csi_sync_nr, nrpages);
1535 atomic_set(&anchor->csi_barrier, nrpages > 0);
1536 anchor->csi_sync_rc = 0;
1538 EXPORT_SYMBOL(cl_sync_io_init);
1541 * Wait until all transfer completes. Transfer completion routine has to call
1542 * cl_sync_io_note() for every page.
1544 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1545 struct cl_page_list *queue, struct cl_sync_io *anchor,
1548 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1552 LASSERT(timeout >= 0);
1554 rc = l_wait_event(anchor->csi_waitq,
1555 atomic_read(&anchor->csi_sync_nr) == 0,
1558 CERROR("SYNC IO failed with error: %d, try to cancel %d remaining pages\n",
1559 rc, atomic_read(&anchor->csi_sync_nr));
1561 (void)cl_io_cancel(env, io, queue);
1563 lwi = (struct l_wait_info) { 0 };
1564 (void)l_wait_event(anchor->csi_waitq,
1565 atomic_read(&anchor->csi_sync_nr) == 0,
1568 rc = anchor->csi_sync_rc;
1570 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1571 cl_page_list_assume(env, io, queue);
1573 /* wait until cl_sync_io_note() has done wakeup */
1574 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1578 POISON(anchor, 0x5a, sizeof(*anchor));
1581 EXPORT_SYMBOL(cl_sync_io_wait);
1584 * Indicate that transfer of a single page completed.
1586 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1588 if (anchor->csi_sync_rc == 0 && ioret < 0)
1589 anchor->csi_sync_rc = ioret;
1591 * Synchronous IO done without releasing page lock (e.g., as a part of
1592 * ->{prepare,commit}_write(). Completion is used to signal the end of
1595 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1596 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1597 wake_up_all(&anchor->csi_waitq);
1598 /* it's safe to nuke or reuse anchor now */
1599 atomic_set(&anchor->csi_barrier, 0);
1602 EXPORT_SYMBOL(cl_sync_io_note);