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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
38 #define DEBUG_SUBSYSTEM S_CLASS
40 #include <linux/sched.h>
41 #include <linux/list.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lustre_fid.h>
45 #include <cl_object.h>
46 #include "cl_internal.h"
48 /*****************************************************************************
54 #define cl_io_for_each(slice, io) \
55 list_for_each_entry((slice), &io->ci_layers, cis_linkage)
56 #define cl_io_for_each_reverse(slice, io) \
57 list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
59 static inline int cl_io_type_is_valid(enum cl_io_type type)
61 return CIT_READ <= type && type < CIT_OP_NR;
64 static inline int cl_io_is_loopable(const struct cl_io *io)
66 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
70 * Returns true iff there is an IO ongoing in the given environment.
72 int cl_io_is_going(const struct lu_env *env)
74 return cl_env_info(env)->clt_current_io != NULL;
78 * cl_io invariant that holds at all times when exported cl_io_*() functions
79 * are entered and left.
81 static int cl_io_invariant(const struct cl_io *io)
88 * io can own pages only when it is ongoing. Sub-io might
89 * still be in CIS_LOCKED state when top-io is in
92 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
93 (io->ci_state == CIS_LOCKED && up != NULL));
97 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
99 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
101 struct cl_io_slice *slice;
102 struct cl_thread_info *info;
104 LINVRNT(cl_io_type_is_valid(io->ci_type));
105 LINVRNT(cl_io_invariant(io));
108 while (!list_empty(&io->ci_layers)) {
109 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
111 list_del_init(&slice->cis_linkage);
112 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
113 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
115 * Invalidate slice to catch use after free. This assumes that
116 * slices are allocated within session and can be touched
117 * after ->cio_fini() returns.
119 slice->cis_io = NULL;
121 io->ci_state = CIS_FINI;
122 info = cl_env_info(env);
123 if (info->clt_current_io == io)
124 info->clt_current_io = NULL;
126 /* sanity check for layout change */
127 switch(io->ci_type) {
130 case CIT_DATA_VERSION:
134 LASSERT(!io->ci_need_restart);
138 /* Check ignore layout change conf */
139 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
140 !io->ci_need_restart));
149 EXPORT_SYMBOL(cl_io_fini);
151 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
152 enum cl_io_type iot, struct cl_object *obj)
154 struct cl_object *scan;
157 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
158 LINVRNT(cl_io_type_is_valid(iot));
159 LINVRNT(cl_io_invariant(io));
163 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
164 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
165 INIT_LIST_HEAD(&io->ci_layers);
168 cl_object_for_each(scan, obj) {
169 if (scan->co_ops->coo_io_init != NULL) {
170 result = scan->co_ops->coo_io_init(env, scan, io);
176 io->ci_state = CIS_INIT;
181 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
183 * \pre obj != cl_object_top(obj)
185 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
186 enum cl_io_type iot, struct cl_object *obj)
188 struct cl_thread_info *info = cl_env_info(env);
190 LASSERT(obj != cl_object_top(obj));
191 if (info->clt_current_io == NULL)
192 info->clt_current_io = io;
193 return cl_io_init0(env, io, iot, obj);
195 EXPORT_SYMBOL(cl_io_sub_init);
198 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
200 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
201 * what the latter returned.
203 * \pre obj == cl_object_top(obj)
204 * \pre cl_io_type_is_valid(iot)
205 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
207 int cl_io_init(const struct lu_env *env, struct cl_io *io,
208 enum cl_io_type iot, struct cl_object *obj)
210 struct cl_thread_info *info = cl_env_info(env);
212 LASSERT(obj == cl_object_top(obj));
213 LASSERT(info->clt_current_io == NULL);
215 info->clt_current_io = io;
216 return cl_io_init0(env, io, iot, obj);
218 EXPORT_SYMBOL(cl_io_init);
221 * Initialize read or write io.
223 * \pre iot == CIT_READ || iot == CIT_WRITE
225 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
226 enum cl_io_type iot, loff_t pos, size_t count)
228 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
229 LINVRNT(io->ci_obj != NULL);
232 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
233 "io range: %u [%llu, %llu) %u %u\n",
234 iot, (__u64)pos, (__u64)pos + count,
235 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
236 io->u.ci_rw.crw_pos = pos;
237 io->u.ci_rw.crw_count = count;
238 RETURN(cl_io_init(env, io, iot, io->ci_obj));
240 EXPORT_SYMBOL(cl_io_rw_init);
242 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
243 const struct cl_lock_descr *d1)
245 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
246 lu_object_fid(&d1->cld_obj->co_lu));
250 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
252 static void cl_io_locks_sort(struct cl_io *io)
257 /* hidden treasure: bubble sort for now. */
259 struct cl_io_lock_link *curr;
260 struct cl_io_lock_link *prev;
261 struct cl_io_lock_link *temp;
266 list_for_each_entry_safe(curr, temp, &io->ci_lockset.cls_todo,
269 switch (cl_lock_descr_sort(&prev->cill_descr,
270 &curr->cill_descr)) {
273 * IMPOSSIBLE: Identical locks are
280 list_move_tail(&curr->cill_linkage,
281 &prev->cill_linkage);
283 continue; /* don't change prev: it's
284 * still "previous" */
285 case -1: /* already in order */
295 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
296 const struct cl_lock_descr *d1)
298 d0->cld_start = min(d0->cld_start, d1->cld_start);
299 d0->cld_end = max(d0->cld_end, d1->cld_end);
301 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
302 d0->cld_mode = CLM_WRITE;
304 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
305 d0->cld_mode = CLM_GROUP;
308 static int cl_lockset_merge(const struct cl_lockset *set,
309 const struct cl_lock_descr *need)
311 struct cl_io_lock_link *scan;
314 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
315 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
318 /* Merge locks for the same object because ldlm lock server
319 * may expand the lock extent, otherwise there is a deadlock
320 * case if two conflicted locks are queueud for the same object
321 * and lock server expands one lock to overlap the another.
322 * The side effect is that it can generate a multi-stripe lock
323 * that may cause casacading problem */
324 cl_lock_descr_merge(&scan->cill_descr, need);
325 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
326 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
327 scan->cill_descr.cld_end);
333 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
334 struct cl_lockset *set)
336 struct cl_io_lock_link *link;
337 struct cl_io_lock_link *temp;
342 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
343 result = cl_lock_request(env, io, &link->cill_lock);
347 list_move(&link->cill_linkage, &set->cls_done);
353 * Takes locks necessary for the current iteration of io.
355 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
356 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
359 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
361 const struct cl_io_slice *scan;
364 LINVRNT(cl_io_is_loopable(io));
365 LINVRNT(io->ci_state == CIS_IT_STARTED);
366 LINVRNT(cl_io_invariant(io));
369 cl_io_for_each(scan, io) {
370 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
372 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
377 cl_io_locks_sort(io);
378 result = cl_lockset_lock(env, io, &io->ci_lockset);
381 cl_io_unlock(env, io);
383 io->ci_state = CIS_LOCKED;
386 EXPORT_SYMBOL(cl_io_lock);
389 * Release locks takes by io.
391 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
393 struct cl_lockset *set;
394 struct cl_io_lock_link *link;
395 struct cl_io_lock_link *temp;
396 const struct cl_io_slice *scan;
398 LASSERT(cl_io_is_loopable(io));
399 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
400 LINVRNT(cl_io_invariant(io));
403 set = &io->ci_lockset;
405 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
406 list_del_init(&link->cill_linkage);
407 if (link->cill_fini != NULL)
408 link->cill_fini(env, link);
411 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
412 list_del_init(&link->cill_linkage);
413 cl_lock_release(env, &link->cill_lock);
414 if (link->cill_fini != NULL)
415 link->cill_fini(env, link);
418 cl_io_for_each_reverse(scan, io) {
419 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
420 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
422 io->ci_state = CIS_UNLOCKED;
425 EXPORT_SYMBOL(cl_io_unlock);
428 * Prepares next iteration of io.
430 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
431 * layers a chance to modify io parameters, e.g., so that lov can restrict io
432 * to a single stripe.
434 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
436 const struct cl_io_slice *scan;
439 LINVRNT(cl_io_is_loopable(io));
440 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
441 LINVRNT(cl_io_invariant(io));
445 cl_io_for_each(scan, io) {
446 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
448 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
454 io->ci_state = CIS_IT_STARTED;
457 EXPORT_SYMBOL(cl_io_iter_init);
460 * Finalizes io iteration.
462 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
464 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
466 const struct cl_io_slice *scan;
468 LINVRNT(cl_io_is_loopable(io));
469 LINVRNT(io->ci_state == CIS_UNLOCKED);
470 LINVRNT(cl_io_invariant(io));
473 cl_io_for_each_reverse(scan, io) {
474 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
475 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
477 io->ci_state = CIS_IT_ENDED;
480 EXPORT_SYMBOL(cl_io_iter_fini);
483 * Records that read or write io progressed \a nob bytes forward.
485 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
487 const struct cl_io_slice *scan;
489 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
491 LINVRNT(cl_io_is_loopable(io));
492 LINVRNT(cl_io_invariant(io));
496 io->u.ci_rw.crw_pos += nob;
497 io->u.ci_rw.crw_count -= nob;
499 /* layers have to be notified. */
500 cl_io_for_each_reverse(scan, io) {
501 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
502 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
509 * Adds a lock to a lockset.
511 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
512 struct cl_io_lock_link *link)
517 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
520 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
525 EXPORT_SYMBOL(cl_io_lock_add);
527 static void cl_free_io_lock_link(const struct lu_env *env,
528 struct cl_io_lock_link *link)
534 * Allocates new lock link, and uses it to add a lock to a lockset.
536 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
537 struct cl_lock_descr *descr)
539 struct cl_io_lock_link *link;
545 link->cill_descr = *descr;
546 link->cill_fini = cl_free_io_lock_link;
547 result = cl_io_lock_add(env, io, link);
548 if (result) /* lock match */
549 link->cill_fini(env, link);
555 EXPORT_SYMBOL(cl_io_lock_alloc_add);
558 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
560 int cl_io_start(const struct lu_env *env, struct cl_io *io)
562 const struct cl_io_slice *scan;
565 LINVRNT(cl_io_is_loopable(io));
566 LINVRNT(io->ci_state == CIS_LOCKED);
567 LINVRNT(cl_io_invariant(io));
570 io->ci_state = CIS_IO_GOING;
571 cl_io_for_each(scan, io) {
572 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
574 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
582 EXPORT_SYMBOL(cl_io_start);
585 * Wait until current io iteration is finished by calling
586 * cl_io_operations::cio_end() bottom-to-top.
588 void cl_io_end(const struct lu_env *env, struct cl_io *io)
590 const struct cl_io_slice *scan;
592 LINVRNT(cl_io_is_loopable(io));
593 LINVRNT(io->ci_state == CIS_IO_GOING);
594 LINVRNT(cl_io_invariant(io));
597 cl_io_for_each_reverse(scan, io) {
598 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
599 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
600 /* TODO: error handling. */
602 io->ci_state = CIS_IO_FINISHED;
605 EXPORT_SYMBOL(cl_io_end);
608 * Called by read io, to decide the readahead extent
610 * \see cl_io_operations::cio_read_ahead()
612 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
613 pgoff_t start, struct cl_read_ahead *ra)
615 const struct cl_io_slice *scan;
618 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
619 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
620 LINVRNT(cl_io_invariant(io));
623 cl_io_for_each(scan, io) {
624 if (scan->cis_iop->cio_read_ahead == NULL)
627 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
631 RETURN(result > 0 ? 0 : result);
633 EXPORT_SYMBOL(cl_io_read_ahead);
636 * Commit a list of contiguous pages into writeback cache.
638 * \returns 0 if all pages committed, or errcode if error occurred.
639 * \see cl_io_operations::cio_commit_async()
641 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
642 struct cl_page_list *queue, int from, int to,
645 const struct cl_io_slice *scan;
649 cl_io_for_each(scan, io) {
650 if (scan->cis_iop->cio_commit_async == NULL)
652 result = scan->cis_iop->cio_commit_async(env, scan, queue,
659 EXPORT_SYMBOL(cl_io_commit_async);
662 * Submits a list of pages for immediate io.
664 * After the function gets returned, The submitted pages are moved to
665 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
666 * to be submitted, and the pages are errant to submit.
668 * \returns 0 if at least one page was submitted, error code otherwise.
669 * \see cl_io_operations::cio_submit()
671 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
672 enum cl_req_type crt, struct cl_2queue *queue)
674 const struct cl_io_slice *scan;
678 cl_io_for_each(scan, io) {
679 if (scan->cis_iop->cio_submit == NULL)
681 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
686 * If ->cio_submit() failed, no pages were sent.
688 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
691 EXPORT_SYMBOL(cl_io_submit_rw);
694 * Submit a sync_io and wait for the IO to be finished, or error happens.
695 * If \a timeout is zero, it means to wait for the IO unconditionally.
697 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
698 enum cl_req_type iot, struct cl_2queue *queue,
701 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
705 cl_page_list_for_each(pg, &queue->c2_qin) {
706 LASSERT(pg->cp_sync_io == NULL);
707 pg->cp_sync_io = anchor;
710 cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
711 rc = cl_io_submit_rw(env, io, iot, queue);
714 * If some pages weren't sent for any reason (e.g.,
715 * read found up-to-date pages in the cache, or write found
716 * clean pages), count them as completed to avoid infinite
719 cl_page_list_for_each(pg, &queue->c2_qin) {
720 pg->cp_sync_io = NULL;
721 cl_sync_io_note(env, anchor, 1);
724 /* wait for the IO to be finished. */
725 rc = cl_sync_io_wait(env, anchor, timeout);
726 cl_page_list_assume(env, io, &queue->c2_qout);
728 LASSERT(list_empty(&queue->c2_qout.pl_pages));
729 cl_page_list_for_each(pg, &queue->c2_qin)
730 pg->cp_sync_io = NULL;
734 EXPORT_SYMBOL(cl_io_submit_sync);
737 * Cancel an IO which has been submitted by cl_io_submit_rw.
739 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
740 struct cl_page_list *queue)
742 struct cl_page *page;
745 CERROR("Canceling ongoing page trasmission\n");
746 cl_page_list_for_each(page, queue) {
749 rc = cl_page_cancel(env, page);
750 result = result ?: rc;
758 * Pumps io through iterations calling
760 * - cl_io_iter_init()
770 * - cl_io_iter_fini()
772 * repeatedly until there is no more io to do.
774 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
778 LINVRNT(cl_io_is_loopable(io));
785 result = cl_io_iter_init(env, io);
788 result = cl_io_lock(env, io);
791 * Notify layers that locks has been taken,
794 * - llite: kms, short read;
795 * - llite: generic_file_read();
797 result = cl_io_start(env, io);
799 * Send any remaining pending
802 * - llite: ll_rw_stats_tally.
805 cl_io_unlock(env, io);
806 cl_io_rw_advance(env, io, io->ci_nob - nob);
809 cl_io_iter_fini(env, io);
810 } while (result == 0 && io->ci_continue);
812 result = io->ci_result;
813 RETURN(result < 0 ? result : 0);
815 EXPORT_SYMBOL(cl_io_loop);
818 * Adds io slice to the cl_io.
820 * This is called by cl_object_operations::coo_io_init() methods to add a
821 * per-layer state to the io. New state is added at the end of
822 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
824 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
826 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
827 struct cl_object *obj,
828 const struct cl_io_operations *ops)
830 struct list_head *linkage = &slice->cis_linkage;
832 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
833 list_empty(linkage));
836 list_add_tail(linkage, &io->ci_layers);
838 slice->cis_obj = obj;
839 slice->cis_iop = ops;
842 EXPORT_SYMBOL(cl_io_slice_add);
846 * Initializes page list.
848 void cl_page_list_init(struct cl_page_list *plist)
852 INIT_LIST_HEAD(&plist->pl_pages);
853 plist->pl_owner = current;
856 EXPORT_SYMBOL(cl_page_list_init);
859 * Adds a page to a page list.
861 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
864 /* it would be better to check that page is owned by "current" io, but
865 * it is not passed here. */
866 LASSERT(page->cp_owner != NULL);
867 LINVRNT(plist->pl_owner == current);
869 LASSERT(list_empty(&page->cp_batch));
870 list_add_tail(&page->cp_batch, &plist->pl_pages);
872 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
876 EXPORT_SYMBOL(cl_page_list_add);
879 * Removes a page from a page list.
881 void cl_page_list_del(const struct lu_env *env,
882 struct cl_page_list *plist, struct cl_page *page)
884 LASSERT(plist->pl_nr > 0);
885 LASSERT(cl_page_is_vmlocked(env, page));
886 LINVRNT(plist->pl_owner == current);
889 list_del_init(&page->cp_batch);
891 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
892 cl_page_put(env, page);
895 EXPORT_SYMBOL(cl_page_list_del);
898 * Moves a page from one page list to another.
900 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
901 struct cl_page *page)
903 LASSERT(src->pl_nr > 0);
904 LINVRNT(dst->pl_owner == current);
905 LINVRNT(src->pl_owner == current);
908 list_move_tail(&page->cp_batch, &dst->pl_pages);
911 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
915 EXPORT_SYMBOL(cl_page_list_move);
918 * Moves a page from one page list to the head of another list.
920 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
921 struct cl_page *page)
923 LASSERT(src->pl_nr > 0);
924 LINVRNT(dst->pl_owner == current);
925 LINVRNT(src->pl_owner == current);
928 list_move(&page->cp_batch, &dst->pl_pages);
931 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
935 EXPORT_SYMBOL(cl_page_list_move_head);
938 * splice the cl_page_list, just as list head does
940 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
942 struct cl_page *page;
945 LINVRNT(list->pl_owner == current);
946 LINVRNT(head->pl_owner == current);
949 cl_page_list_for_each_safe(page, tmp, list)
950 cl_page_list_move(head, list, page);
953 EXPORT_SYMBOL(cl_page_list_splice);
956 * Disowns pages in a queue.
958 void cl_page_list_disown(const struct lu_env *env,
959 struct cl_io *io, struct cl_page_list *plist)
961 struct cl_page *page;
962 struct cl_page *temp;
964 LINVRNT(plist->pl_owner == current);
967 cl_page_list_for_each_safe(page, temp, plist) {
968 LASSERT(plist->pl_nr > 0);
970 list_del_init(&page->cp_batch);
973 * cl_page_disown0 rather than usual cl_page_disown() is used,
974 * because pages are possibly in CPS_FREEING state already due
975 * to the call to cl_page_list_discard().
978 * XXX cl_page_disown0() will fail if page is not locked.
980 cl_page_disown0(env, io, page);
981 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
983 cl_page_put(env, page);
987 EXPORT_SYMBOL(cl_page_list_disown);
990 * Releases pages from queue.
992 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
994 struct cl_page *page;
995 struct cl_page *temp;
997 LINVRNT(plist->pl_owner == current);
1000 cl_page_list_for_each_safe(page, temp, plist)
1001 cl_page_list_del(env, plist, page);
1002 LASSERT(plist->pl_nr == 0);
1005 EXPORT_SYMBOL(cl_page_list_fini);
1008 * Assumes all pages in a queue.
1010 void cl_page_list_assume(const struct lu_env *env,
1011 struct cl_io *io, struct cl_page_list *plist)
1013 struct cl_page *page;
1015 LINVRNT(plist->pl_owner == current);
1017 cl_page_list_for_each(page, plist)
1018 cl_page_assume(env, io, page);
1022 * Discards all pages in a queue.
1024 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1025 struct cl_page_list *plist)
1027 struct cl_page *page;
1029 LINVRNT(plist->pl_owner == current);
1031 cl_page_list_for_each(page, plist)
1032 cl_page_discard(env, io, page);
1037 * Initialize dual page queue.
1039 void cl_2queue_init(struct cl_2queue *queue)
1042 cl_page_list_init(&queue->c2_qin);
1043 cl_page_list_init(&queue->c2_qout);
1046 EXPORT_SYMBOL(cl_2queue_init);
1049 * Add a page to the incoming page list of 2-queue.
1051 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1054 cl_page_list_add(&queue->c2_qin, page);
1057 EXPORT_SYMBOL(cl_2queue_add);
1060 * Disown pages in both lists of a 2-queue.
1062 void cl_2queue_disown(const struct lu_env *env,
1063 struct cl_io *io, struct cl_2queue *queue)
1066 cl_page_list_disown(env, io, &queue->c2_qin);
1067 cl_page_list_disown(env, io, &queue->c2_qout);
1070 EXPORT_SYMBOL(cl_2queue_disown);
1073 * Discard (truncate) pages in both lists of a 2-queue.
1075 void cl_2queue_discard(const struct lu_env *env,
1076 struct cl_io *io, struct cl_2queue *queue)
1079 cl_page_list_discard(env, io, &queue->c2_qin);
1080 cl_page_list_discard(env, io, &queue->c2_qout);
1083 EXPORT_SYMBOL(cl_2queue_discard);
1086 * Assume to own the pages in cl_2queue
1088 void cl_2queue_assume(const struct lu_env *env,
1089 struct cl_io *io, struct cl_2queue *queue)
1091 cl_page_list_assume(env, io, &queue->c2_qin);
1092 cl_page_list_assume(env, io, &queue->c2_qout);
1096 * Finalize both page lists of a 2-queue.
1098 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1101 cl_page_list_fini(env, &queue->c2_qout);
1102 cl_page_list_fini(env, &queue->c2_qin);
1105 EXPORT_SYMBOL(cl_2queue_fini);
1108 * Initialize a 2-queue to contain \a page in its incoming page list.
1110 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1113 cl_2queue_init(queue);
1114 cl_2queue_add(queue, page);
1117 EXPORT_SYMBOL(cl_2queue_init_page);
1120 * Returns top-level io.
1122 * \see cl_object_top()
1124 struct cl_io *cl_io_top(struct cl_io *io)
1127 while (io->ci_parent != NULL)
1131 EXPORT_SYMBOL(cl_io_top);
1134 * Prints human readable representation of \a io to the \a f.
1136 void cl_io_print(const struct lu_env *env, void *cookie,
1137 lu_printer_t printer, const struct cl_io *io)
1142 * Fills in attributes that are passed to server together with transfer. Only
1143 * attributes from \a flags may be touched. This can be called multiple times
1144 * for the same request.
1146 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1147 struct cl_req_attr *attr)
1149 struct cl_object *scan;
1152 cl_object_for_each(scan, obj) {
1153 if (scan->co_ops->coo_req_attr_set != NULL)
1154 scan->co_ops->coo_req_attr_set(env, scan, attr);
1158 EXPORT_SYMBOL(cl_req_attr_set);
1160 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1161 * wait for the IO to finish. */
1162 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1164 wake_up_all(&anchor->csi_waitq);
1166 /* it's safe to nuke or reuse anchor now */
1167 atomic_set(&anchor->csi_barrier, 0);
1169 EXPORT_SYMBOL(cl_sync_io_end);
1172 * Initialize synchronous io wait anchor
1174 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1175 void (*end)(const struct lu_env *, struct cl_sync_io *))
1178 memset(anchor, 0, sizeof(*anchor));
1179 init_waitqueue_head(&anchor->csi_waitq);
1180 atomic_set(&anchor->csi_sync_nr, nr);
1181 atomic_set(&anchor->csi_barrier, nr > 0);
1182 anchor->csi_sync_rc = 0;
1183 anchor->csi_end_io = end;
1184 LASSERT(end != NULL);
1187 EXPORT_SYMBOL(cl_sync_io_init);
1190 * Wait until all IO completes. Transfer completion routine has to call
1191 * cl_sync_io_note() for every entity.
1193 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1196 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1201 LASSERT(timeout >= 0);
1203 rc = l_wait_event(anchor->csi_waitq,
1204 atomic_read(&anchor->csi_sync_nr) == 0,
1207 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1208 rc, atomic_read(&anchor->csi_sync_nr));
1210 lwi = (struct l_wait_info) { 0 };
1211 (void)l_wait_event(anchor->csi_waitq,
1212 atomic_read(&anchor->csi_sync_nr) == 0,
1215 rc = anchor->csi_sync_rc;
1217 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1219 /* wait until cl_sync_io_note() has done wakeup */
1220 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1225 EXPORT_SYMBOL(cl_sync_io_wait);
1228 * Indicate that transfer of a single page completed.
1230 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1234 if (anchor->csi_sync_rc == 0 && ioret < 0)
1235 anchor->csi_sync_rc = ioret;
1237 * Synchronous IO done without releasing page lock (e.g., as a part of
1238 * ->{prepare,commit}_write(). Completion is used to signal the end of
1241 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1242 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1243 LASSERT(anchor->csi_end_io != NULL);
1244 anchor->csi_end_io(env, anchor);
1245 /* Can't access anchor any more */
1249 EXPORT_SYMBOL(cl_sync_io_note);