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;
80 EXPORT_SYMBOL(cl_io_is_going);
83 * cl_io invariant that holds at all times when exported cl_io_*() functions
84 * are entered and left.
86 static int cl_io_invariant(const struct cl_io *io)
93 * io can own pages only when it is ongoing. Sub-io might
94 * still be in CIS_LOCKED state when top-io is in
97 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
98 (io->ci_state == CIS_LOCKED && up != NULL));
102 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
104 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
106 struct cl_io_slice *slice;
107 struct cl_thread_info *info;
109 LINVRNT(cl_io_type_is_valid(io->ci_type));
110 LINVRNT(cl_io_invariant(io));
113 while (!list_empty(&io->ci_layers)) {
114 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
116 list_del_init(&slice->cis_linkage);
117 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
118 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
120 * Invalidate slice to catch use after free. This assumes that
121 * slices are allocated within session and can be touched
122 * after ->cio_fini() returns.
124 slice->cis_io = NULL;
126 io->ci_state = CIS_FINI;
127 info = cl_env_info(env);
128 if (info->clt_current_io == io)
129 info->clt_current_io = NULL;
131 /* sanity check for layout change */
132 switch(io->ci_type) {
138 LASSERT(!io->ci_need_restart);
142 /* Check ignore layout change conf */
143 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
144 !io->ci_need_restart));
151 EXPORT_SYMBOL(cl_io_fini);
153 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
154 enum cl_io_type iot, struct cl_object *obj)
156 struct cl_object *scan;
159 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
160 LINVRNT(cl_io_type_is_valid(iot));
161 LINVRNT(cl_io_invariant(io));
165 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
166 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
167 INIT_LIST_HEAD(&io->ci_layers);
170 cl_object_for_each(scan, obj) {
171 if (scan->co_ops->coo_io_init != NULL) {
172 result = scan->co_ops->coo_io_init(env, scan, io);
178 io->ci_state = CIS_INIT;
183 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
185 * \pre obj != cl_object_top(obj)
187 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
188 enum cl_io_type iot, struct cl_object *obj)
190 struct cl_thread_info *info = cl_env_info(env);
192 LASSERT(obj != cl_object_top(obj));
193 if (info->clt_current_io == NULL)
194 info->clt_current_io = io;
195 return cl_io_init0(env, io, iot, obj);
197 EXPORT_SYMBOL(cl_io_sub_init);
200 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
202 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
203 * what the latter returned.
205 * \pre obj == cl_object_top(obj)
206 * \pre cl_io_type_is_valid(iot)
207 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
209 int cl_io_init(const struct lu_env *env, struct cl_io *io,
210 enum cl_io_type iot, struct cl_object *obj)
212 struct cl_thread_info *info = cl_env_info(env);
214 LASSERT(obj == cl_object_top(obj));
215 LASSERT(info->clt_current_io == NULL);
217 info->clt_current_io = io;
218 return cl_io_init0(env, io, iot, obj);
220 EXPORT_SYMBOL(cl_io_init);
223 * Initialize read or write io.
225 * \pre iot == CIT_READ || iot == CIT_WRITE
227 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
228 enum cl_io_type iot, loff_t pos, size_t count)
230 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
231 LINVRNT(io->ci_obj != NULL);
234 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
235 "io range: %u ["LPU64", "LPU64") %u %u\n",
236 iot, (__u64)pos, (__u64)pos + count,
237 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
238 io->u.ci_rw.crw_pos = pos;
239 io->u.ci_rw.crw_count = count;
240 RETURN(cl_io_init(env, io, iot, io->ci_obj));
242 EXPORT_SYMBOL(cl_io_rw_init);
244 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
245 const struct cl_lock_descr *d1)
247 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
248 lu_object_fid(&d1->cld_obj->co_lu));
252 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
254 static void cl_io_locks_sort(struct cl_io *io)
259 /* hidden treasure: bubble sort for now. */
261 struct cl_io_lock_link *curr;
262 struct cl_io_lock_link *prev;
263 struct cl_io_lock_link *temp;
268 list_for_each_entry_safe(curr, temp, &io->ci_lockset.cls_todo,
271 switch (cl_lock_descr_sort(&prev->cill_descr,
272 &curr->cill_descr)) {
275 * IMPOSSIBLE: Identical locks are
282 list_move_tail(&curr->cill_linkage,
283 &prev->cill_linkage);
285 continue; /* don't change prev: it's
286 * still "previous" */
287 case -1: /* already in order */
297 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
298 const struct cl_lock_descr *d1)
300 d0->cld_start = min(d0->cld_start, d1->cld_start);
301 d0->cld_end = max(d0->cld_end, d1->cld_end);
303 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
304 d0->cld_mode = CLM_WRITE;
306 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
307 d0->cld_mode = CLM_GROUP;
310 static int cl_lockset_merge(const struct cl_lockset *set,
311 const struct cl_lock_descr *need)
313 struct cl_io_lock_link *scan;
316 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
317 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
320 /* Merge locks for the same object because ldlm lock server
321 * may expand the lock extent, otherwise there is a deadlock
322 * case if two conflicted locks are queueud for the same object
323 * and lock server expands one lock to overlap the another.
324 * The side effect is that it can generate a multi-stripe lock
325 * that may cause casacading problem */
326 cl_lock_descr_merge(&scan->cill_descr, need);
327 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
328 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
329 scan->cill_descr.cld_end);
335 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
336 struct cl_lockset *set)
338 struct cl_io_lock_link *link;
339 struct cl_io_lock_link *temp;
344 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
345 result = cl_lock_request(env, io, &link->cill_lock);
349 list_move(&link->cill_linkage, &set->cls_done);
355 * Takes locks necessary for the current iteration of io.
357 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
358 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
361 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
363 const struct cl_io_slice *scan;
366 LINVRNT(cl_io_is_loopable(io));
367 LINVRNT(io->ci_state == CIS_IT_STARTED);
368 LINVRNT(cl_io_invariant(io));
371 cl_io_for_each(scan, io) {
372 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
374 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
379 cl_io_locks_sort(io);
380 result = cl_lockset_lock(env, io, &io->ci_lockset);
383 cl_io_unlock(env, io);
385 io->ci_state = CIS_LOCKED;
388 EXPORT_SYMBOL(cl_io_lock);
391 * Release locks takes by io.
393 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
395 struct cl_lockset *set;
396 struct cl_io_lock_link *link;
397 struct cl_io_lock_link *temp;
398 const struct cl_io_slice *scan;
400 LASSERT(cl_io_is_loopable(io));
401 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
402 LINVRNT(cl_io_invariant(io));
405 set = &io->ci_lockset;
407 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
408 list_del_init(&link->cill_linkage);
409 if (link->cill_fini != NULL)
410 link->cill_fini(env, link);
413 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
414 list_del_init(&link->cill_linkage);
415 cl_lock_release(env, &link->cill_lock);
416 if (link->cill_fini != NULL)
417 link->cill_fini(env, link);
420 cl_io_for_each_reverse(scan, io) {
421 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
422 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
424 io->ci_state = CIS_UNLOCKED;
425 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
428 EXPORT_SYMBOL(cl_io_unlock);
431 * Prepares next iteration of io.
433 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
434 * layers a chance to modify io parameters, e.g., so that lov can restrict io
435 * to a single stripe.
437 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
439 const struct cl_io_slice *scan;
442 LINVRNT(cl_io_is_loopable(io));
443 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
444 LINVRNT(cl_io_invariant(io));
448 cl_io_for_each(scan, io) {
449 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
451 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
457 io->ci_state = CIS_IT_STARTED;
460 EXPORT_SYMBOL(cl_io_iter_init);
463 * Finalizes io iteration.
465 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
467 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
469 const struct cl_io_slice *scan;
471 LINVRNT(cl_io_is_loopable(io));
472 LINVRNT(io->ci_state == CIS_UNLOCKED);
473 LINVRNT(cl_io_invariant(io));
476 cl_io_for_each_reverse(scan, io) {
477 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
478 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
480 io->ci_state = CIS_IT_ENDED;
483 EXPORT_SYMBOL(cl_io_iter_fini);
486 * Records that read or write io progressed \a nob bytes forward.
488 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
490 const struct cl_io_slice *scan;
492 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
494 LINVRNT(cl_io_is_loopable(io));
495 LINVRNT(cl_io_invariant(io));
499 io->u.ci_rw.crw_pos += nob;
500 io->u.ci_rw.crw_count -= nob;
502 /* layers have to be notified. */
503 cl_io_for_each_reverse(scan, io) {
504 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
505 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
510 EXPORT_SYMBOL(cl_io_rw_advance);
513 * Adds a lock to a lockset.
515 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
516 struct cl_io_lock_link *link)
521 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
524 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
529 EXPORT_SYMBOL(cl_io_lock_add);
531 static void cl_free_io_lock_link(const struct lu_env *env,
532 struct cl_io_lock_link *link)
538 * Allocates new lock link, and uses it to add a lock to a lockset.
540 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
541 struct cl_lock_descr *descr)
543 struct cl_io_lock_link *link;
549 link->cill_descr = *descr;
550 link->cill_fini = cl_free_io_lock_link;
551 result = cl_io_lock_add(env, io, link);
552 if (result) /* lock match */
553 link->cill_fini(env, link);
559 EXPORT_SYMBOL(cl_io_lock_alloc_add);
562 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
564 int cl_io_start(const struct lu_env *env, struct cl_io *io)
566 const struct cl_io_slice *scan;
569 LINVRNT(cl_io_is_loopable(io));
570 LINVRNT(io->ci_state == CIS_LOCKED);
571 LINVRNT(cl_io_invariant(io));
574 io->ci_state = CIS_IO_GOING;
575 cl_io_for_each(scan, io) {
576 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
578 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
586 EXPORT_SYMBOL(cl_io_start);
589 * Wait until current io iteration is finished by calling
590 * cl_io_operations::cio_end() bottom-to-top.
592 void cl_io_end(const struct lu_env *env, struct cl_io *io)
594 const struct cl_io_slice *scan;
596 LINVRNT(cl_io_is_loopable(io));
597 LINVRNT(io->ci_state == CIS_IO_GOING);
598 LINVRNT(cl_io_invariant(io));
601 cl_io_for_each_reverse(scan, io) {
602 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
603 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
604 /* TODO: error handling. */
606 io->ci_state = CIS_IO_FINISHED;
609 EXPORT_SYMBOL(cl_io_end);
611 static const struct cl_page_slice *
612 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
614 const struct cl_page_slice *slice;
616 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
617 LINVRNT(slice != NULL);
622 * Called by read io, when page has to be read from the server.
624 * \see cl_io_operations::cio_read_page()
626 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
627 struct cl_page *page)
629 const struct cl_io_slice *scan;
630 struct cl_2queue *queue;
633 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
634 LINVRNT(cl_page_is_owned(page, io));
635 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
636 LINVRNT(cl_io_invariant(io));
639 queue = &io->ci_queue;
641 cl_2queue_init(queue);
643 * ->cio_read_page() methods called in the loop below are supposed to
644 * never block waiting for network (the only subtle point is the
645 * creation of new pages for read-ahead that might result in cache
646 * shrinking, but currently only clean pages are shrunk and this
647 * requires no network io).
649 * Should this ever starts blocking, retry loop would be needed for
650 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
652 cl_io_for_each(scan, io) {
653 if (scan->cis_iop->cio_read_page != NULL) {
654 const struct cl_page_slice *slice;
656 slice = cl_io_slice_page(scan, page);
657 LINVRNT(slice != NULL);
658 result = scan->cis_iop->cio_read_page(env, scan, slice);
663 if (result == 0 && queue->c2_qin.pl_nr > 0)
664 result = cl_io_submit_rw(env, io, CRT_READ, queue);
666 * Unlock unsent pages in case of error.
668 cl_page_list_disown(env, io, &queue->c2_qin);
669 cl_2queue_fini(env, queue);
672 EXPORT_SYMBOL(cl_io_read_page);
675 * Commit a list of contiguous pages into writeback cache.
677 * \returns 0 if all pages committed, or errcode if error occurred.
678 * \see cl_io_operations::cio_commit_async()
680 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
681 struct cl_page_list *queue, int from, int to,
684 const struct cl_io_slice *scan;
688 cl_io_for_each(scan, io) {
689 if (scan->cis_iop->cio_commit_async == NULL)
691 result = scan->cis_iop->cio_commit_async(env, scan, queue,
698 EXPORT_SYMBOL(cl_io_commit_async);
701 * Submits a list of pages for immediate io.
703 * After the function gets returned, The submitted pages are moved to
704 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
705 * to be submitted, and the pages are errant to submit.
707 * \returns 0 if at least one page was submitted, error code otherwise.
708 * \see cl_io_operations::cio_submit()
710 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
711 enum cl_req_type crt, struct cl_2queue *queue)
713 const struct cl_io_slice *scan;
717 cl_io_for_each(scan, io) {
718 if (scan->cis_iop->cio_submit == NULL)
720 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
725 * If ->cio_submit() failed, no pages were sent.
727 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
730 EXPORT_SYMBOL(cl_io_submit_rw);
733 * Submit a sync_io and wait for the IO to be finished, or error happens.
734 * If \a timeout is zero, it means to wait for the IO unconditionally.
736 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
737 enum cl_req_type iot, struct cl_2queue *queue,
740 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
744 cl_page_list_for_each(pg, &queue->c2_qin) {
745 LASSERT(pg->cp_sync_io == NULL);
746 pg->cp_sync_io = anchor;
749 cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
750 rc = cl_io_submit_rw(env, io, iot, queue);
753 * If some pages weren't sent for any reason (e.g.,
754 * read found up-to-date pages in the cache, or write found
755 * clean pages), count them as completed to avoid infinite
758 cl_page_list_for_each(pg, &queue->c2_qin) {
759 pg->cp_sync_io = NULL;
760 cl_sync_io_note(env, anchor, 1);
763 /* wait for the IO to be finished. */
764 rc = cl_sync_io_wait(env, anchor, timeout);
765 cl_page_list_assume(env, io, &queue->c2_qout);
767 LASSERT(list_empty(&queue->c2_qout.pl_pages));
768 cl_page_list_for_each(pg, &queue->c2_qin)
769 pg->cp_sync_io = NULL;
773 EXPORT_SYMBOL(cl_io_submit_sync);
776 * Cancel an IO which has been submitted by cl_io_submit_rw.
778 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
779 struct cl_page_list *queue)
781 struct cl_page *page;
784 CERROR("Canceling ongoing page trasmission\n");
785 cl_page_list_for_each(page, queue) {
788 rc = cl_page_cancel(env, page);
789 result = result ?: rc;
793 EXPORT_SYMBOL(cl_io_cancel);
798 * Pumps io through iterations calling
800 * - cl_io_iter_init()
810 * - cl_io_iter_fini()
812 * repeatedly until there is no more io to do.
814 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
818 LINVRNT(cl_io_is_loopable(io));
825 result = cl_io_iter_init(env, io);
828 result = cl_io_lock(env, io);
831 * Notify layers that locks has been taken,
834 * - llite: kms, short read;
835 * - llite: generic_file_read();
837 result = cl_io_start(env, io);
839 * Send any remaining pending
842 * - llite: ll_rw_stats_tally.
845 cl_io_unlock(env, io);
846 cl_io_rw_advance(env, io, io->ci_nob - nob);
849 cl_io_iter_fini(env, io);
850 } while (result == 0 && io->ci_continue);
852 result = io->ci_result;
853 RETURN(result < 0 ? result : 0);
855 EXPORT_SYMBOL(cl_io_loop);
858 * Adds io slice to the cl_io.
860 * This is called by cl_object_operations::coo_io_init() methods to add a
861 * per-layer state to the io. New state is added at the end of
862 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
864 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
866 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
867 struct cl_object *obj,
868 const struct cl_io_operations *ops)
870 struct list_head *linkage = &slice->cis_linkage;
872 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
873 list_empty(linkage));
876 list_add_tail(linkage, &io->ci_layers);
878 slice->cis_obj = obj;
879 slice->cis_iop = ops;
882 EXPORT_SYMBOL(cl_io_slice_add);
886 * Initializes page list.
888 void cl_page_list_init(struct cl_page_list *plist)
892 INIT_LIST_HEAD(&plist->pl_pages);
893 plist->pl_owner = current;
896 EXPORT_SYMBOL(cl_page_list_init);
899 * Adds a page to a page list.
901 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
904 /* it would be better to check that page is owned by "current" io, but
905 * it is not passed here. */
906 LASSERT(page->cp_owner != NULL);
907 LINVRNT(plist->pl_owner == current);
909 LASSERT(list_empty(&page->cp_batch));
910 list_add_tail(&page->cp_batch, &plist->pl_pages);
912 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
916 EXPORT_SYMBOL(cl_page_list_add);
919 * Removes a page from a page list.
921 void cl_page_list_del(const struct lu_env *env,
922 struct cl_page_list *plist, struct cl_page *page)
924 LASSERT(plist->pl_nr > 0);
925 LASSERT(cl_page_is_vmlocked(env, page));
926 LINVRNT(plist->pl_owner == current);
929 list_del_init(&page->cp_batch);
931 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
932 cl_page_put(env, page);
935 EXPORT_SYMBOL(cl_page_list_del);
938 * Moves a page from one page list to another.
940 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
941 struct cl_page *page)
943 LASSERT(src->pl_nr > 0);
944 LINVRNT(dst->pl_owner == current);
945 LINVRNT(src->pl_owner == current);
948 list_move_tail(&page->cp_batch, &dst->pl_pages);
951 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
955 EXPORT_SYMBOL(cl_page_list_move);
958 * Moves a page from one page list to the head of another list.
960 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
961 struct cl_page *page)
963 LASSERT(src->pl_nr > 0);
964 LINVRNT(dst->pl_owner == current);
965 LINVRNT(src->pl_owner == current);
968 list_move(&page->cp_batch, &dst->pl_pages);
971 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
975 EXPORT_SYMBOL(cl_page_list_move_head);
978 * splice the cl_page_list, just as list head does
980 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
982 struct cl_page *page;
985 LINVRNT(list->pl_owner == current);
986 LINVRNT(head->pl_owner == current);
989 cl_page_list_for_each_safe(page, tmp, list)
990 cl_page_list_move(head, list, page);
993 EXPORT_SYMBOL(cl_page_list_splice);
996 * Disowns pages in a queue.
998 void cl_page_list_disown(const struct lu_env *env,
999 struct cl_io *io, struct cl_page_list *plist)
1001 struct cl_page *page;
1002 struct cl_page *temp;
1004 LINVRNT(plist->pl_owner == current);
1007 cl_page_list_for_each_safe(page, temp, plist) {
1008 LASSERT(plist->pl_nr > 0);
1010 list_del_init(&page->cp_batch);
1013 * cl_page_disown0 rather than usual cl_page_disown() is used,
1014 * because pages are possibly in CPS_FREEING state already due
1015 * to the call to cl_page_list_discard().
1018 * XXX cl_page_disown0() will fail if page is not locked.
1020 cl_page_disown0(env, io, page);
1021 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1023 cl_page_put(env, page);
1027 EXPORT_SYMBOL(cl_page_list_disown);
1030 * Releases pages from queue.
1032 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1034 struct cl_page *page;
1035 struct cl_page *temp;
1037 LINVRNT(plist->pl_owner == current);
1040 cl_page_list_for_each_safe(page, temp, plist)
1041 cl_page_list_del(env, plist, page);
1042 LASSERT(plist->pl_nr == 0);
1045 EXPORT_SYMBOL(cl_page_list_fini);
1048 * Owns all pages in a queue.
1050 int cl_page_list_own(const struct lu_env *env,
1051 struct cl_io *io, struct cl_page_list *plist)
1053 struct cl_page *page;
1054 struct cl_page *temp;
1057 LINVRNT(plist->pl_owner == current);
1061 cl_page_list_for_each_safe(page, temp, plist) {
1062 if (cl_page_own(env, io, page) == 0)
1063 result = result ?: page->cp_error;
1065 cl_page_list_del(env, plist, page);
1069 EXPORT_SYMBOL(cl_page_list_own);
1072 * Assumes all pages in a queue.
1074 void cl_page_list_assume(const struct lu_env *env,
1075 struct cl_io *io, struct cl_page_list *plist)
1077 struct cl_page *page;
1079 LINVRNT(plist->pl_owner == current);
1081 cl_page_list_for_each(page, plist)
1082 cl_page_assume(env, io, page);
1084 EXPORT_SYMBOL(cl_page_list_assume);
1087 * Discards all pages in a queue.
1089 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1090 struct cl_page_list *plist)
1092 struct cl_page *page;
1094 LINVRNT(plist->pl_owner == current);
1096 cl_page_list_for_each(page, plist)
1097 cl_page_discard(env, io, page);
1100 EXPORT_SYMBOL(cl_page_list_discard);
1103 * Initialize dual page queue.
1105 void cl_2queue_init(struct cl_2queue *queue)
1108 cl_page_list_init(&queue->c2_qin);
1109 cl_page_list_init(&queue->c2_qout);
1112 EXPORT_SYMBOL(cl_2queue_init);
1115 * Add a page to the incoming page list of 2-queue.
1117 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1120 cl_page_list_add(&queue->c2_qin, page);
1123 EXPORT_SYMBOL(cl_2queue_add);
1126 * Disown pages in both lists of a 2-queue.
1128 void cl_2queue_disown(const struct lu_env *env,
1129 struct cl_io *io, struct cl_2queue *queue)
1132 cl_page_list_disown(env, io, &queue->c2_qin);
1133 cl_page_list_disown(env, io, &queue->c2_qout);
1136 EXPORT_SYMBOL(cl_2queue_disown);
1139 * Discard (truncate) pages in both lists of a 2-queue.
1141 void cl_2queue_discard(const struct lu_env *env,
1142 struct cl_io *io, struct cl_2queue *queue)
1145 cl_page_list_discard(env, io, &queue->c2_qin);
1146 cl_page_list_discard(env, io, &queue->c2_qout);
1149 EXPORT_SYMBOL(cl_2queue_discard);
1152 * Assume to own the pages in cl_2queue
1154 void cl_2queue_assume(const struct lu_env *env,
1155 struct cl_io *io, struct cl_2queue *queue)
1157 cl_page_list_assume(env, io, &queue->c2_qin);
1158 cl_page_list_assume(env, io, &queue->c2_qout);
1160 EXPORT_SYMBOL(cl_2queue_assume);
1163 * Finalize both page lists of a 2-queue.
1165 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1168 cl_page_list_fini(env, &queue->c2_qout);
1169 cl_page_list_fini(env, &queue->c2_qin);
1172 EXPORT_SYMBOL(cl_2queue_fini);
1175 * Initialize a 2-queue to contain \a page in its incoming page list.
1177 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1180 cl_2queue_init(queue);
1181 cl_2queue_add(queue, page);
1184 EXPORT_SYMBOL(cl_2queue_init_page);
1187 * Returns top-level io.
1189 * \see cl_object_top()
1191 struct cl_io *cl_io_top(struct cl_io *io)
1194 while (io->ci_parent != NULL)
1198 EXPORT_SYMBOL(cl_io_top);
1201 * Prints human readable representation of \a io to the \a f.
1203 void cl_io_print(const struct lu_env *env, void *cookie,
1204 lu_printer_t printer, const struct cl_io *io)
1209 * Adds request slice to the compound request.
1211 * This is called by cl_device_operations::cdo_req_init() methods to add a
1212 * per-layer state to the request. New state is added at the end of
1213 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1215 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1217 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1218 struct cl_device *dev,
1219 const struct cl_req_operations *ops)
1222 list_add_tail(&slice->crs_linkage, &req->crq_layers);
1223 slice->crs_dev = dev;
1224 slice->crs_ops = ops;
1225 slice->crs_req = req;
1228 EXPORT_SYMBOL(cl_req_slice_add);
1230 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1234 LASSERT(list_empty(&req->crq_pages));
1235 LASSERT(req->crq_nrpages == 0);
1236 LINVRNT(list_empty(&req->crq_layers));
1237 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1240 if (req->crq_o != NULL) {
1241 for (i = 0; i < req->crq_nrobjs; ++i) {
1242 struct cl_object *obj = req->crq_o[i].ro_obj;
1244 lu_object_ref_del_at(&obj->co_lu,
1245 &req->crq_o[i].ro_obj_ref,
1247 cl_object_put(env, obj);
1250 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1256 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1257 struct cl_page *page)
1259 struct cl_device *dev;
1260 struct cl_page_slice *slice;
1265 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1266 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1267 if (dev->cd_ops->cdo_req_init != NULL) {
1268 result = dev->cd_ops->cdo_req_init(env,
1278 * Invokes per-request transfer completion call-backs
1279 * (cl_req_operations::cro_completion()) bottom-to-top.
1281 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1283 struct cl_req_slice *slice;
1287 * for the lack of list_for_each_entry_reverse_safe()...
1289 while (!list_empty(&req->crq_layers)) {
1290 slice = list_entry(req->crq_layers.prev,
1291 struct cl_req_slice, crs_linkage);
1292 list_del_init(&slice->crs_linkage);
1293 if (slice->crs_ops->cro_completion != NULL)
1294 slice->crs_ops->cro_completion(env, slice, rc);
1296 cl_req_free(env, req);
1299 EXPORT_SYMBOL(cl_req_completion);
1302 * Allocates new transfer request.
1304 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1305 enum cl_req_type crt, int nr_objects)
1309 LINVRNT(nr_objects > 0);
1316 req->crq_type = crt;
1317 INIT_LIST_HEAD(&req->crq_pages);
1318 INIT_LIST_HEAD(&req->crq_layers);
1320 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1321 if (req->crq_o != NULL) {
1322 req->crq_nrobjs = nr_objects;
1323 result = cl_req_init(env, req, page);
1327 cl_req_completion(env, req, result);
1328 req = ERR_PTR(result);
1331 req = ERR_PTR(-ENOMEM);
1334 EXPORT_SYMBOL(cl_req_alloc);
1337 * Adds a page to a request.
1339 void cl_req_page_add(const struct lu_env *env,
1340 struct cl_req *req, struct cl_page *page)
1342 struct cl_object *obj;
1343 struct cl_req_obj *rqo;
1348 LASSERT(list_empty(&page->cp_flight));
1349 LASSERT(page->cp_req == NULL);
1351 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1352 req, req->crq_type, req->crq_nrpages);
1354 list_add_tail(&page->cp_flight, &req->crq_pages);
1357 obj = cl_object_top(page->cp_obj);
1358 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1359 if (rqo->ro_obj == NULL) {
1362 lu_object_ref_add_at(&obj->co_lu, &rqo->ro_obj_ref,
1367 LASSERT(i < req->crq_nrobjs);
1370 EXPORT_SYMBOL(cl_req_page_add);
1373 * Removes a page from a request.
1375 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1377 struct cl_req *req = page->cp_req;
1381 LASSERT(!list_empty(&page->cp_flight));
1382 LASSERT(req->crq_nrpages > 0);
1384 list_del_init(&page->cp_flight);
1386 page->cp_req = NULL;
1389 EXPORT_SYMBOL(cl_req_page_done);
1392 * Notifies layers that request is about to depart by calling
1393 * cl_req_operations::cro_prep() top-to-bottom.
1395 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1399 const struct cl_req_slice *slice;
1403 * Check that the caller of cl_req_alloc() didn't lie about the number
1406 for (i = 0; i < req->crq_nrobjs; ++i)
1407 LASSERT(req->crq_o[i].ro_obj != NULL);
1410 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1411 if (slice->crs_ops->cro_prep != NULL) {
1412 result = slice->crs_ops->cro_prep(env, slice);
1419 EXPORT_SYMBOL(cl_req_prep);
1422 * Fills in attributes that are passed to server together with transfer. Only
1423 * attributes from \a flags may be touched. This can be called multiple times
1424 * for the same request.
1426 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1427 struct cl_req_attr *attr, u64 flags)
1429 const struct cl_req_slice *slice;
1430 struct cl_page *page;
1433 LASSERT(!list_empty(&req->crq_pages));
1436 /* Take any page to use as a model. */
1437 page = list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1439 for (i = 0; i < req->crq_nrobjs; ++i) {
1440 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1441 const struct cl_page_slice *scan;
1442 const struct cl_object *obj;
1444 scan = cl_page_at(page,
1445 slice->crs_dev->cd_lu_dev.ld_type);
1446 LASSERT(scan != NULL);
1447 obj = scan->cpl_obj;
1448 if (slice->crs_ops->cro_attr_set != NULL)
1449 slice->crs_ops->cro_attr_set(env, slice, obj,
1455 EXPORT_SYMBOL(cl_req_attr_set);
1457 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1458 * wait for the IO to finish. */
1459 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1461 wake_up_all(&anchor->csi_waitq);
1463 /* it's safe to nuke or reuse anchor now */
1464 atomic_set(&anchor->csi_barrier, 0);
1466 EXPORT_SYMBOL(cl_sync_io_end);
1469 * Initialize synchronous io wait anchor
1471 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1472 void (*end)(const struct lu_env *, struct cl_sync_io *))
1475 memset(anchor, 0, sizeof(*anchor));
1476 init_waitqueue_head(&anchor->csi_waitq);
1477 atomic_set(&anchor->csi_sync_nr, nr);
1478 atomic_set(&anchor->csi_barrier, nr > 0);
1479 anchor->csi_sync_rc = 0;
1480 anchor->csi_end_io = end;
1481 LASSERT(end != NULL);
1484 EXPORT_SYMBOL(cl_sync_io_init);
1487 * Wait until all IO completes. Transfer completion routine has to call
1488 * cl_sync_io_note() for every entity.
1490 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1493 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1498 LASSERT(timeout >= 0);
1500 rc = l_wait_event(anchor->csi_waitq,
1501 atomic_read(&anchor->csi_sync_nr) == 0,
1504 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1505 rc, atomic_read(&anchor->csi_sync_nr));
1507 lwi = (struct l_wait_info) { 0 };
1508 (void)l_wait_event(anchor->csi_waitq,
1509 atomic_read(&anchor->csi_sync_nr) == 0,
1512 rc = anchor->csi_sync_rc;
1514 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1516 /* wait until cl_sync_io_note() has done wakeup */
1517 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1522 EXPORT_SYMBOL(cl_sync_io_wait);
1525 * Indicate that transfer of a single page completed.
1527 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1531 if (anchor->csi_sync_rc == 0 && ioret < 0)
1532 anchor->csi_sync_rc = ioret;
1534 * Synchronous IO done without releasing page lock (e.g., as a part of
1535 * ->{prepare,commit}_write(). Completion is used to signal the end of
1538 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1539 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1540 LASSERT(anchor->csi_end_io != NULL);
1541 anchor->csi_end_io(env, anchor);
1542 /* Can't access anchor any more */
1546 EXPORT_SYMBOL(cl_sync_io_note);