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, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
42 #define DEBUG_SUBSYSTEM S_CLASS
44 #include <linux/sched.h>
45 #include <obd_class.h>
46 #include <obd_support.h>
47 #include <lustre_fid.h>
48 #include <libcfs/list.h>
49 #include <cl_object.h>
50 #include "cl_internal.h"
52 /*****************************************************************************
58 #define cl_io_for_each(slice, io) \
59 list_for_each_entry((slice), &io->ci_layers, cis_linkage)
60 #define cl_io_for_each_reverse(slice, io) \
61 list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
63 static inline int cl_io_type_is_valid(enum cl_io_type type)
65 return CIT_READ <= type && type < CIT_OP_NR;
68 static inline int cl_io_is_loopable(const struct cl_io *io)
70 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
74 * Returns true iff there is an IO ongoing in the given environment.
76 int cl_io_is_going(const struct lu_env *env)
78 return cl_env_info(env)->clt_current_io != NULL;
82 * cl_io invariant that holds at all times when exported cl_io_*() functions
83 * are entered and left.
85 static int cl_io_invariant(const struct cl_io *io)
92 * io can own pages only when it is ongoing. Sub-io might
93 * still be in CIS_LOCKED state when top-io is in
96 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
97 (io->ci_state == CIS_LOCKED && up != NULL));
101 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
103 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
105 struct cl_io_slice *slice;
106 struct cl_thread_info *info;
108 LINVRNT(cl_io_type_is_valid(io->ci_type));
109 LINVRNT(cl_io_invariant(io));
112 while (!list_empty(&io->ci_layers)) {
113 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
115 list_del_init(&slice->cis_linkage);
116 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
117 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
119 * Invalidate slice to catch use after free. This assumes that
120 * slices are allocated within session and can be touched
121 * after ->cio_fini() returns.
123 slice->cis_io = NULL;
125 io->ci_state = CIS_FINI;
126 info = cl_env_info(env);
127 if (info->clt_current_io == io)
128 info->clt_current_io = NULL;
130 /* sanity check for layout change */
131 switch(io->ci_type) {
137 LASSERT(!io->ci_need_restart);
141 /* Check ignore layout change conf */
142 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
143 !io->ci_need_restart));
150 EXPORT_SYMBOL(cl_io_fini);
152 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
153 enum cl_io_type iot, struct cl_object *obj)
155 struct cl_object *scan;
158 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
159 LINVRNT(cl_io_type_is_valid(iot));
160 LINVRNT(cl_io_invariant(io));
164 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
165 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
166 INIT_LIST_HEAD(&io->ci_layers);
169 cl_object_for_each(scan, obj) {
170 if (scan->co_ops->coo_io_init != NULL) {
171 result = scan->co_ops->coo_io_init(env, scan, io);
177 io->ci_state = CIS_INIT;
182 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
184 * \pre obj != cl_object_top(obj)
186 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
187 enum cl_io_type iot, struct cl_object *obj)
189 struct cl_thread_info *info = cl_env_info(env);
191 LASSERT(obj != cl_object_top(obj));
192 if (info->clt_current_io == NULL)
193 info->clt_current_io = io;
194 return cl_io_init0(env, io, iot, obj);
196 EXPORT_SYMBOL(cl_io_sub_init);
199 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
201 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
202 * what the latter returned.
204 * \pre obj == cl_object_top(obj)
205 * \pre cl_io_type_is_valid(iot)
206 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
208 int cl_io_init(const struct lu_env *env, struct cl_io *io,
209 enum cl_io_type iot, struct cl_object *obj)
211 struct cl_thread_info *info = cl_env_info(env);
213 LASSERT(obj == cl_object_top(obj));
214 LASSERT(info->clt_current_io == NULL);
216 info->clt_current_io = io;
217 return cl_io_init0(env, io, iot, obj);
219 EXPORT_SYMBOL(cl_io_init);
222 * Initialize read or write io.
224 * \pre iot == CIT_READ || iot == CIT_WRITE
226 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
227 enum cl_io_type iot, loff_t pos, size_t count)
229 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
230 LINVRNT(io->ci_obj != NULL);
233 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
234 "io range: %u ["LPU64", "LPU64") %u %u\n",
235 iot, (__u64)pos, (__u64)pos + count,
236 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
237 io->u.ci_rw.crw_pos = pos;
238 io->u.ci_rw.crw_count = count;
239 RETURN(cl_io_init(env, io, iot, io->ci_obj));
241 EXPORT_SYMBOL(cl_io_rw_init);
243 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
244 const struct cl_lock_descr *d1)
246 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
247 lu_object_fid(&d1->cld_obj->co_lu));
251 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
253 static void cl_io_locks_sort(struct cl_io *io)
258 /* hidden treasure: bubble sort for now. */
260 struct cl_io_lock_link *curr;
261 struct cl_io_lock_link *prev;
262 struct cl_io_lock_link *temp;
267 list_for_each_entry_safe(curr, temp, &io->ci_lockset.cls_todo,
270 switch (cl_lock_descr_sort(&prev->cill_descr,
271 &curr->cill_descr)) {
274 * IMPOSSIBLE: Identical locks are
281 list_move_tail(&curr->cill_linkage,
282 &prev->cill_linkage);
284 continue; /* don't change prev: it's
285 * still "previous" */
286 case -1: /* already in order */
296 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
297 const struct cl_lock_descr *d1)
299 d0->cld_start = min(d0->cld_start, d1->cld_start);
300 d0->cld_end = max(d0->cld_end, d1->cld_end);
302 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
303 d0->cld_mode = CLM_WRITE;
305 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
306 d0->cld_mode = CLM_GROUP;
309 static int cl_lockset_merge(const struct cl_lockset *set,
310 const struct cl_lock_descr *need)
312 struct cl_io_lock_link *scan;
315 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
316 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
319 /* Merge locks for the same object because ldlm lock server
320 * may expand the lock extent, otherwise there is a deadlock
321 * case if two conflicted locks are queueud for the same object
322 * and lock server expands one lock to overlap the another.
323 * The side effect is that it can generate a multi-stripe lock
324 * that may cause casacading problem */
325 cl_lock_descr_merge(&scan->cill_descr, need);
326 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
327 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
328 scan->cill_descr.cld_end);
334 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
335 struct cl_lockset *set)
337 struct cl_io_lock_link *link;
338 struct cl_io_lock_link *temp;
343 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
344 result = cl_lock_request(env, io, &link->cill_lock);
348 list_move(&link->cill_linkage, &set->cls_done);
354 * Takes locks necessary for the current iteration of io.
356 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
357 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
360 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
362 const struct cl_io_slice *scan;
365 LINVRNT(cl_io_is_loopable(io));
366 LINVRNT(io->ci_state == CIS_IT_STARTED);
367 LINVRNT(cl_io_invariant(io));
370 cl_io_for_each(scan, io) {
371 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
373 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
378 cl_io_locks_sort(io);
379 result = cl_lockset_lock(env, io, &io->ci_lockset);
382 cl_io_unlock(env, io);
384 io->ci_state = CIS_LOCKED;
387 EXPORT_SYMBOL(cl_io_lock);
390 * Release locks takes by io.
392 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
394 struct cl_lockset *set;
395 struct cl_io_lock_link *link;
396 struct cl_io_lock_link *temp;
397 const struct cl_io_slice *scan;
399 LASSERT(cl_io_is_loopable(io));
400 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
401 LINVRNT(cl_io_invariant(io));
404 set = &io->ci_lockset;
406 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
407 list_del_init(&link->cill_linkage);
408 if (link->cill_fini != NULL)
409 link->cill_fini(env, link);
412 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
413 list_del_init(&link->cill_linkage);
414 cl_lock_release(env, &link->cill_lock);
415 if (link->cill_fini != NULL)
416 link->cill_fini(env, link);
419 cl_io_for_each_reverse(scan, io) {
420 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
421 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
423 io->ci_state = CIS_UNLOCKED;
424 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
427 EXPORT_SYMBOL(cl_io_unlock);
430 * Prepares next iteration of io.
432 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
433 * layers a chance to modify io parameters, e.g., so that lov can restrict io
434 * to a single stripe.
436 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
438 const struct cl_io_slice *scan;
441 LINVRNT(cl_io_is_loopable(io));
442 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
443 LINVRNT(cl_io_invariant(io));
447 cl_io_for_each(scan, io) {
448 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
450 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
456 io->ci_state = CIS_IT_STARTED;
459 EXPORT_SYMBOL(cl_io_iter_init);
462 * Finalizes io iteration.
464 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
466 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
468 const struct cl_io_slice *scan;
470 LINVRNT(cl_io_is_loopable(io));
471 LINVRNT(io->ci_state == CIS_UNLOCKED);
472 LINVRNT(cl_io_invariant(io));
475 cl_io_for_each_reverse(scan, io) {
476 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
477 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
479 io->ci_state = CIS_IT_ENDED;
482 EXPORT_SYMBOL(cl_io_iter_fini);
485 * Records that read or write io progressed \a nob bytes forward.
487 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
489 const struct cl_io_slice *scan;
491 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
493 LINVRNT(cl_io_is_loopable(io));
494 LINVRNT(cl_io_invariant(io));
498 io->u.ci_rw.crw_pos += nob;
499 io->u.ci_rw.crw_count -= nob;
501 /* layers have to be notified. */
502 cl_io_for_each_reverse(scan, io) {
503 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
504 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
511 * Adds a lock to a lockset.
513 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
514 struct cl_io_lock_link *link)
519 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
522 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
527 EXPORT_SYMBOL(cl_io_lock_add);
529 static void cl_free_io_lock_link(const struct lu_env *env,
530 struct cl_io_lock_link *link)
536 * Allocates new lock link, and uses it to add a lock to a lockset.
538 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
539 struct cl_lock_descr *descr)
541 struct cl_io_lock_link *link;
547 link->cill_descr = *descr;
548 link->cill_fini = cl_free_io_lock_link;
549 result = cl_io_lock_add(env, io, link);
550 if (result) /* lock match */
551 link->cill_fini(env, link);
557 EXPORT_SYMBOL(cl_io_lock_alloc_add);
560 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
562 int cl_io_start(const struct lu_env *env, struct cl_io *io)
564 const struct cl_io_slice *scan;
567 LINVRNT(cl_io_is_loopable(io));
568 LINVRNT(io->ci_state == CIS_LOCKED);
569 LINVRNT(cl_io_invariant(io));
572 io->ci_state = CIS_IO_GOING;
573 cl_io_for_each(scan, io) {
574 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
576 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
584 EXPORT_SYMBOL(cl_io_start);
587 * Wait until current io iteration is finished by calling
588 * cl_io_operations::cio_end() bottom-to-top.
590 void cl_io_end(const struct lu_env *env, struct cl_io *io)
592 const struct cl_io_slice *scan;
594 LINVRNT(cl_io_is_loopable(io));
595 LINVRNT(io->ci_state == CIS_IO_GOING);
596 LINVRNT(cl_io_invariant(io));
599 cl_io_for_each_reverse(scan, io) {
600 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
601 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
602 /* TODO: error handling. */
604 io->ci_state = CIS_IO_FINISHED;
607 EXPORT_SYMBOL(cl_io_end);
610 * Called by read io, to decide the readahead extent
612 * \see cl_io_operations::cio_read_ahead()
614 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
615 pgoff_t start, struct cl_read_ahead *ra)
617 const struct cl_io_slice *scan;
620 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
621 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
622 LINVRNT(cl_io_invariant(io));
625 cl_io_for_each(scan, io) {
626 if (scan->cis_iop->cio_read_ahead == NULL)
629 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
633 RETURN(result > 0 ? 0 : result);
635 EXPORT_SYMBOL(cl_io_read_ahead);
638 * Commit a list of contiguous pages into writeback cache.
640 * \returns 0 if all pages committed, or errcode if error occurred.
641 * \see cl_io_operations::cio_commit_async()
643 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
644 struct cl_page_list *queue, int from, int to,
647 const struct cl_io_slice *scan;
651 cl_io_for_each(scan, io) {
652 if (scan->cis_iop->cio_commit_async == NULL)
654 result = scan->cis_iop->cio_commit_async(env, scan, queue,
661 EXPORT_SYMBOL(cl_io_commit_async);
664 * Submits a list of pages for immediate io.
666 * After the function gets returned, The submitted pages are moved to
667 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
668 * to be submitted, and the pages are errant to submit.
670 * \returns 0 if at least one page was submitted, error code otherwise.
671 * \see cl_io_operations::cio_submit()
673 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
674 enum cl_req_type crt, struct cl_2queue *queue)
676 const struct cl_io_slice *scan;
680 cl_io_for_each(scan, io) {
681 if (scan->cis_iop->cio_submit == NULL)
683 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
688 * If ->cio_submit() failed, no pages were sent.
690 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
693 EXPORT_SYMBOL(cl_io_submit_rw);
696 * Submit a sync_io and wait for the IO to be finished, or error happens.
697 * If \a timeout is zero, it means to wait for the IO unconditionally.
699 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
700 enum cl_req_type iot, struct cl_2queue *queue,
703 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
707 cl_page_list_for_each(pg, &queue->c2_qin) {
708 LASSERT(pg->cp_sync_io == NULL);
709 pg->cp_sync_io = anchor;
712 cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
713 rc = cl_io_submit_rw(env, io, iot, queue);
716 * If some pages weren't sent for any reason (e.g.,
717 * read found up-to-date pages in the cache, or write found
718 * clean pages), count them as completed to avoid infinite
721 cl_page_list_for_each(pg, &queue->c2_qin) {
722 pg->cp_sync_io = NULL;
723 cl_sync_io_note(env, anchor, 1);
726 /* wait for the IO to be finished. */
727 rc = cl_sync_io_wait(env, anchor, timeout);
728 cl_page_list_assume(env, io, &queue->c2_qout);
730 LASSERT(list_empty(&queue->c2_qout.pl_pages));
731 cl_page_list_for_each(pg, &queue->c2_qin)
732 pg->cp_sync_io = NULL;
736 EXPORT_SYMBOL(cl_io_submit_sync);
739 * Cancel an IO which has been submitted by cl_io_submit_rw.
741 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
742 struct cl_page_list *queue)
744 struct cl_page *page;
747 CERROR("Canceling ongoing page trasmission\n");
748 cl_page_list_for_each(page, queue) {
751 rc = cl_page_cancel(env, page);
752 result = result ?: rc;
760 * Pumps io through iterations calling
762 * - cl_io_iter_init()
772 * - cl_io_iter_fini()
774 * repeatedly until there is no more io to do.
776 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
780 LINVRNT(cl_io_is_loopable(io));
787 result = cl_io_iter_init(env, io);
790 result = cl_io_lock(env, io);
793 * Notify layers that locks has been taken,
796 * - llite: kms, short read;
797 * - llite: generic_file_read();
799 result = cl_io_start(env, io);
801 * Send any remaining pending
804 * - llite: ll_rw_stats_tally.
807 cl_io_unlock(env, io);
808 cl_io_rw_advance(env, io, io->ci_nob - nob);
811 cl_io_iter_fini(env, io);
812 } while (result == 0 && io->ci_continue);
814 result = io->ci_result;
815 RETURN(result < 0 ? result : 0);
817 EXPORT_SYMBOL(cl_io_loop);
820 * Adds io slice to the cl_io.
822 * This is called by cl_object_operations::coo_io_init() methods to add a
823 * per-layer state to the io. New state is added at the end of
824 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
826 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
828 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
829 struct cl_object *obj,
830 const struct cl_io_operations *ops)
832 struct list_head *linkage = &slice->cis_linkage;
834 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
835 list_empty(linkage));
838 list_add_tail(linkage, &io->ci_layers);
840 slice->cis_obj = obj;
841 slice->cis_iop = ops;
844 EXPORT_SYMBOL(cl_io_slice_add);
848 * Initializes page list.
850 void cl_page_list_init(struct cl_page_list *plist)
854 INIT_LIST_HEAD(&plist->pl_pages);
855 plist->pl_owner = current;
858 EXPORT_SYMBOL(cl_page_list_init);
861 * Adds a page to a page list.
863 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
866 /* it would be better to check that page is owned by "current" io, but
867 * it is not passed here. */
868 LASSERT(page->cp_owner != NULL);
869 LINVRNT(plist->pl_owner == current);
871 LASSERT(list_empty(&page->cp_batch));
872 list_add_tail(&page->cp_batch, &plist->pl_pages);
874 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
878 EXPORT_SYMBOL(cl_page_list_add);
881 * Removes a page from a page list.
883 void cl_page_list_del(const struct lu_env *env,
884 struct cl_page_list *plist, struct cl_page *page)
886 LASSERT(plist->pl_nr > 0);
887 LASSERT(cl_page_is_vmlocked(env, page));
888 LINVRNT(plist->pl_owner == current);
891 list_del_init(&page->cp_batch);
893 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
894 cl_page_put(env, page);
897 EXPORT_SYMBOL(cl_page_list_del);
900 * Moves a page from one page list to another.
902 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
903 struct cl_page *page)
905 LASSERT(src->pl_nr > 0);
906 LINVRNT(dst->pl_owner == current);
907 LINVRNT(src->pl_owner == current);
910 list_move_tail(&page->cp_batch, &dst->pl_pages);
913 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
917 EXPORT_SYMBOL(cl_page_list_move);
920 * Moves a page from one page list to the head of another list.
922 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
923 struct cl_page *page)
925 LASSERT(src->pl_nr > 0);
926 LINVRNT(dst->pl_owner == current);
927 LINVRNT(src->pl_owner == current);
930 list_move(&page->cp_batch, &dst->pl_pages);
933 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
937 EXPORT_SYMBOL(cl_page_list_move_head);
940 * splice the cl_page_list, just as list head does
942 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
944 struct cl_page *page;
947 LINVRNT(list->pl_owner == current);
948 LINVRNT(head->pl_owner == current);
951 cl_page_list_for_each_safe(page, tmp, list)
952 cl_page_list_move(head, list, page);
955 EXPORT_SYMBOL(cl_page_list_splice);
958 * Disowns pages in a queue.
960 void cl_page_list_disown(const struct lu_env *env,
961 struct cl_io *io, struct cl_page_list *plist)
963 struct cl_page *page;
964 struct cl_page *temp;
966 LINVRNT(plist->pl_owner == current);
969 cl_page_list_for_each_safe(page, temp, plist) {
970 LASSERT(plist->pl_nr > 0);
972 list_del_init(&page->cp_batch);
975 * cl_page_disown0 rather than usual cl_page_disown() is used,
976 * because pages are possibly in CPS_FREEING state already due
977 * to the call to cl_page_list_discard().
980 * XXX cl_page_disown0() will fail if page is not locked.
982 cl_page_disown0(env, io, page);
983 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
985 cl_page_put(env, page);
989 EXPORT_SYMBOL(cl_page_list_disown);
992 * Releases pages from queue.
994 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
996 struct cl_page *page;
997 struct cl_page *temp;
999 LINVRNT(plist->pl_owner == current);
1002 cl_page_list_for_each_safe(page, temp, plist)
1003 cl_page_list_del(env, plist, page);
1004 LASSERT(plist->pl_nr == 0);
1007 EXPORT_SYMBOL(cl_page_list_fini);
1010 * Owns all pages in a queue.
1012 int cl_page_list_own(const struct lu_env *env,
1013 struct cl_io *io, struct cl_page_list *plist)
1015 struct cl_page *page;
1016 struct cl_page *temp;
1019 LINVRNT(plist->pl_owner == current);
1023 cl_page_list_for_each_safe(page, temp, plist) {
1024 if (cl_page_own(env, io, page) == 0)
1025 result = result ?: page->cp_error;
1027 cl_page_list_del(env, plist, page);
1033 * Assumes all pages in a queue.
1035 void cl_page_list_assume(const struct lu_env *env,
1036 struct cl_io *io, struct cl_page_list *plist)
1038 struct cl_page *page;
1040 LINVRNT(plist->pl_owner == current);
1042 cl_page_list_for_each(page, plist)
1043 cl_page_assume(env, io, page);
1047 * Discards all pages in a queue.
1049 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1050 struct cl_page_list *plist)
1052 struct cl_page *page;
1054 LINVRNT(plist->pl_owner == current);
1056 cl_page_list_for_each(page, plist)
1057 cl_page_discard(env, io, page);
1062 * Initialize dual page queue.
1064 void cl_2queue_init(struct cl_2queue *queue)
1067 cl_page_list_init(&queue->c2_qin);
1068 cl_page_list_init(&queue->c2_qout);
1071 EXPORT_SYMBOL(cl_2queue_init);
1074 * Add a page to the incoming page list of 2-queue.
1076 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1079 cl_page_list_add(&queue->c2_qin, page);
1082 EXPORT_SYMBOL(cl_2queue_add);
1085 * Disown pages in both lists of a 2-queue.
1087 void cl_2queue_disown(const struct lu_env *env,
1088 struct cl_io *io, struct cl_2queue *queue)
1091 cl_page_list_disown(env, io, &queue->c2_qin);
1092 cl_page_list_disown(env, io, &queue->c2_qout);
1095 EXPORT_SYMBOL(cl_2queue_disown);
1098 * Discard (truncate) pages in both lists of a 2-queue.
1100 void cl_2queue_discard(const struct lu_env *env,
1101 struct cl_io *io, struct cl_2queue *queue)
1104 cl_page_list_discard(env, io, &queue->c2_qin);
1105 cl_page_list_discard(env, io, &queue->c2_qout);
1108 EXPORT_SYMBOL(cl_2queue_discard);
1111 * Assume to own the pages in cl_2queue
1113 void cl_2queue_assume(const struct lu_env *env,
1114 struct cl_io *io, struct cl_2queue *queue)
1116 cl_page_list_assume(env, io, &queue->c2_qin);
1117 cl_page_list_assume(env, io, &queue->c2_qout);
1121 * Finalize both page lists of a 2-queue.
1123 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1126 cl_page_list_fini(env, &queue->c2_qout);
1127 cl_page_list_fini(env, &queue->c2_qin);
1130 EXPORT_SYMBOL(cl_2queue_fini);
1133 * Initialize a 2-queue to contain \a page in its incoming page list.
1135 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1138 cl_2queue_init(queue);
1139 cl_2queue_add(queue, page);
1142 EXPORT_SYMBOL(cl_2queue_init_page);
1145 * Returns top-level io.
1147 * \see cl_object_top()
1149 struct cl_io *cl_io_top(struct cl_io *io)
1152 while (io->ci_parent != NULL)
1156 EXPORT_SYMBOL(cl_io_top);
1159 * Prints human readable representation of \a io to the \a f.
1161 void cl_io_print(const struct lu_env *env, void *cookie,
1162 lu_printer_t printer, const struct cl_io *io)
1167 * Adds request slice to the compound request.
1169 * This is called by cl_device_operations::cdo_req_init() methods to add a
1170 * per-layer state to the request. New state is added at the end of
1171 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1173 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1175 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1176 struct cl_device *dev,
1177 const struct cl_req_operations *ops)
1180 list_add_tail(&slice->crs_linkage, &req->crq_layers);
1181 slice->crs_dev = dev;
1182 slice->crs_ops = ops;
1183 slice->crs_req = req;
1186 EXPORT_SYMBOL(cl_req_slice_add);
1188 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1192 LASSERT(list_empty(&req->crq_pages));
1193 LASSERT(req->crq_nrpages == 0);
1194 LINVRNT(list_empty(&req->crq_layers));
1195 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1198 if (req->crq_o != NULL) {
1199 for (i = 0; i < req->crq_nrobjs; ++i) {
1200 struct cl_object *obj = req->crq_o[i].ro_obj;
1202 lu_object_ref_del_at(&obj->co_lu,
1203 &req->crq_o[i].ro_obj_ref,
1205 cl_object_put(env, obj);
1208 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1214 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1215 struct cl_page *page)
1217 struct cl_device *dev;
1218 struct cl_page_slice *slice;
1223 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1224 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1225 if (dev->cd_ops->cdo_req_init != NULL) {
1226 result = dev->cd_ops->cdo_req_init(env,
1236 * Invokes per-request transfer completion call-backs
1237 * (cl_req_operations::cro_completion()) bottom-to-top.
1239 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1241 struct cl_req_slice *slice;
1245 * for the lack of list_for_each_entry_reverse_safe()...
1247 while (!list_empty(&req->crq_layers)) {
1248 slice = list_entry(req->crq_layers.prev,
1249 struct cl_req_slice, crs_linkage);
1250 list_del_init(&slice->crs_linkage);
1251 if (slice->crs_ops->cro_completion != NULL)
1252 slice->crs_ops->cro_completion(env, slice, rc);
1254 cl_req_free(env, req);
1257 EXPORT_SYMBOL(cl_req_completion);
1260 * Allocates new transfer request.
1262 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1263 enum cl_req_type crt, int nr_objects)
1267 LINVRNT(nr_objects > 0);
1274 req->crq_type = crt;
1275 INIT_LIST_HEAD(&req->crq_pages);
1276 INIT_LIST_HEAD(&req->crq_layers);
1278 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1279 if (req->crq_o != NULL) {
1280 req->crq_nrobjs = nr_objects;
1281 result = cl_req_init(env, req, page);
1285 cl_req_completion(env, req, result);
1286 req = ERR_PTR(result);
1289 req = ERR_PTR(-ENOMEM);
1292 EXPORT_SYMBOL(cl_req_alloc);
1295 * Adds a page to a request.
1297 void cl_req_page_add(const struct lu_env *env,
1298 struct cl_req *req, struct cl_page *page)
1300 struct cl_object *obj;
1301 struct cl_req_obj *rqo;
1306 LASSERT(list_empty(&page->cp_flight));
1307 LASSERT(page->cp_req == NULL);
1309 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1310 req, req->crq_type, req->crq_nrpages);
1312 list_add_tail(&page->cp_flight, &req->crq_pages);
1315 obj = cl_object_top(page->cp_obj);
1316 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1317 if (rqo->ro_obj == NULL) {
1320 lu_object_ref_add_at(&obj->co_lu, &rqo->ro_obj_ref,
1325 LASSERT(i < req->crq_nrobjs);
1328 EXPORT_SYMBOL(cl_req_page_add);
1331 * Removes a page from a request.
1333 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1335 struct cl_req *req = page->cp_req;
1339 LASSERT(!list_empty(&page->cp_flight));
1340 LASSERT(req->crq_nrpages > 0);
1342 list_del_init(&page->cp_flight);
1344 page->cp_req = NULL;
1347 EXPORT_SYMBOL(cl_req_page_done);
1350 * Notifies layers that request is about to depart by calling
1351 * cl_req_operations::cro_prep() top-to-bottom.
1353 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1357 const struct cl_req_slice *slice;
1361 * Check that the caller of cl_req_alloc() didn't lie about the number
1364 for (i = 0; i < req->crq_nrobjs; ++i)
1365 LASSERT(req->crq_o[i].ro_obj != NULL);
1368 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1369 if (slice->crs_ops->cro_prep != NULL) {
1370 result = slice->crs_ops->cro_prep(env, slice);
1377 EXPORT_SYMBOL(cl_req_prep);
1380 * Fills in attributes that are passed to server together with transfer. Only
1381 * attributes from \a flags may be touched. This can be called multiple times
1382 * for the same request.
1384 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1385 struct cl_req_attr *attr, u64 flags)
1387 const struct cl_req_slice *slice;
1388 struct cl_page *page;
1391 LASSERT(!list_empty(&req->crq_pages));
1394 /* Take any page to use as a model. */
1395 page = list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1397 for (i = 0; i < req->crq_nrobjs; ++i) {
1398 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1399 const struct cl_page_slice *scan;
1400 const struct cl_object *obj;
1402 scan = cl_page_at(page,
1403 slice->crs_dev->cd_lu_dev.ld_type);
1404 LASSERT(scan != NULL);
1405 obj = scan->cpl_obj;
1406 if (slice->crs_ops->cro_attr_set != NULL)
1407 slice->crs_ops->cro_attr_set(env, slice, obj,
1413 EXPORT_SYMBOL(cl_req_attr_set);
1415 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1416 * wait for the IO to finish. */
1417 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1419 wake_up_all(&anchor->csi_waitq);
1421 /* it's safe to nuke or reuse anchor now */
1422 atomic_set(&anchor->csi_barrier, 0);
1424 EXPORT_SYMBOL(cl_sync_io_end);
1427 * Initialize synchronous io wait anchor
1429 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1430 void (*end)(const struct lu_env *, struct cl_sync_io *))
1433 memset(anchor, 0, sizeof(*anchor));
1434 init_waitqueue_head(&anchor->csi_waitq);
1435 atomic_set(&anchor->csi_sync_nr, nr);
1436 atomic_set(&anchor->csi_barrier, nr > 0);
1437 anchor->csi_sync_rc = 0;
1438 anchor->csi_end_io = end;
1439 LASSERT(end != NULL);
1442 EXPORT_SYMBOL(cl_sync_io_init);
1445 * Wait until all IO completes. Transfer completion routine has to call
1446 * cl_sync_io_note() for every entity.
1448 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1451 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1456 LASSERT(timeout >= 0);
1458 rc = l_wait_event(anchor->csi_waitq,
1459 atomic_read(&anchor->csi_sync_nr) == 0,
1462 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1463 rc, atomic_read(&anchor->csi_sync_nr));
1465 lwi = (struct l_wait_info) { 0 };
1466 (void)l_wait_event(anchor->csi_waitq,
1467 atomic_read(&anchor->csi_sync_nr) == 0,
1470 rc = anchor->csi_sync_rc;
1472 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1474 /* wait until cl_sync_io_note() has done wakeup */
1475 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1480 EXPORT_SYMBOL(cl_sync_io_wait);
1483 * Indicate that transfer of a single page completed.
1485 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1489 if (anchor->csi_sync_rc == 0 && ioret < 0)
1490 anchor->csi_sync_rc = ioret;
1492 * Synchronous IO done without releasing page lock (e.g., as a part of
1493 * ->{prepare,commit}_write(). Completion is used to signal the end of
1496 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1497 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1498 LASSERT(anchor->csi_end_io != NULL);
1499 anchor->csi_end_io(env, anchor);
1500 /* Can't access anchor any more */
1504 EXPORT_SYMBOL(cl_sync_io_note);