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, 2015, 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 <linux/list.h>
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <lustre_fid.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) {
134 case CIT_DATA_VERSION:
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));
153 EXPORT_SYMBOL(cl_io_fini);
155 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
156 enum cl_io_type iot, struct cl_object *obj)
158 struct cl_object *scan;
161 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
162 LINVRNT(cl_io_type_is_valid(iot));
163 LINVRNT(cl_io_invariant(io));
167 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
168 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
169 INIT_LIST_HEAD(&io->ci_layers);
172 cl_object_for_each(scan, obj) {
173 if (scan->co_ops->coo_io_init != NULL) {
174 result = scan->co_ops->coo_io_init(env, scan, io);
180 io->ci_state = CIS_INIT;
185 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
187 * \pre obj != cl_object_top(obj)
189 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
190 enum cl_io_type iot, struct cl_object *obj)
192 struct cl_thread_info *info = cl_env_info(env);
194 LASSERT(obj != cl_object_top(obj));
195 if (info->clt_current_io == NULL)
196 info->clt_current_io = io;
197 return cl_io_init0(env, io, iot, obj);
199 EXPORT_SYMBOL(cl_io_sub_init);
202 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
204 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
205 * what the latter returned.
207 * \pre obj == cl_object_top(obj)
208 * \pre cl_io_type_is_valid(iot)
209 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
211 int cl_io_init(const struct lu_env *env, struct cl_io *io,
212 enum cl_io_type iot, struct cl_object *obj)
214 struct cl_thread_info *info = cl_env_info(env);
216 LASSERT(obj == cl_object_top(obj));
217 LASSERT(info->clt_current_io == NULL);
219 info->clt_current_io = io;
220 return cl_io_init0(env, io, iot, obj);
222 EXPORT_SYMBOL(cl_io_init);
225 * Initialize read or write io.
227 * \pre iot == CIT_READ || iot == CIT_WRITE
229 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
230 enum cl_io_type iot, loff_t pos, size_t count)
232 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
233 LINVRNT(io->ci_obj != NULL);
236 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
237 "io range: %u [%llu, %llu) %u %u\n",
238 iot, (__u64)pos, (__u64)pos + count,
239 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
240 io->u.ci_rw.crw_pos = pos;
241 io->u.ci_rw.crw_count = count;
242 RETURN(cl_io_init(env, io, iot, io->ci_obj));
244 EXPORT_SYMBOL(cl_io_rw_init);
246 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
247 const struct cl_lock_descr *d1)
249 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
250 lu_object_fid(&d1->cld_obj->co_lu));
254 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
256 static void cl_io_locks_sort(struct cl_io *io)
261 /* hidden treasure: bubble sort for now. */
263 struct cl_io_lock_link *curr;
264 struct cl_io_lock_link *prev;
265 struct cl_io_lock_link *temp;
270 list_for_each_entry_safe(curr, temp, &io->ci_lockset.cls_todo,
273 switch (cl_lock_descr_sort(&prev->cill_descr,
274 &curr->cill_descr)) {
277 * IMPOSSIBLE: Identical locks are
284 list_move_tail(&curr->cill_linkage,
285 &prev->cill_linkage);
287 continue; /* don't change prev: it's
288 * still "previous" */
289 case -1: /* already in order */
299 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
300 const struct cl_lock_descr *d1)
302 d0->cld_start = min(d0->cld_start, d1->cld_start);
303 d0->cld_end = max(d0->cld_end, d1->cld_end);
305 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
306 d0->cld_mode = CLM_WRITE;
308 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
309 d0->cld_mode = CLM_GROUP;
312 static int cl_lockset_merge(const struct cl_lockset *set,
313 const struct cl_lock_descr *need)
315 struct cl_io_lock_link *scan;
318 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
319 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
322 /* Merge locks for the same object because ldlm lock server
323 * may expand the lock extent, otherwise there is a deadlock
324 * case if two conflicted locks are queueud for the same object
325 * and lock server expands one lock to overlap the another.
326 * The side effect is that it can generate a multi-stripe lock
327 * that may cause casacading problem */
328 cl_lock_descr_merge(&scan->cill_descr, need);
329 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
330 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
331 scan->cill_descr.cld_end);
337 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
338 struct cl_lockset *set)
340 struct cl_io_lock_link *link;
341 struct cl_io_lock_link *temp;
346 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
347 result = cl_lock_request(env, io, &link->cill_lock);
351 list_move(&link->cill_linkage, &set->cls_done);
357 * Takes locks necessary for the current iteration of io.
359 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
360 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
363 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
365 const struct cl_io_slice *scan;
368 LINVRNT(cl_io_is_loopable(io));
369 LINVRNT(io->ci_state == CIS_IT_STARTED);
370 LINVRNT(cl_io_invariant(io));
373 cl_io_for_each(scan, io) {
374 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
376 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
381 cl_io_locks_sort(io);
382 result = cl_lockset_lock(env, io, &io->ci_lockset);
385 cl_io_unlock(env, io);
387 io->ci_state = CIS_LOCKED;
390 EXPORT_SYMBOL(cl_io_lock);
393 * Release locks takes by io.
395 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
397 struct cl_lockset *set;
398 struct cl_io_lock_link *link;
399 struct cl_io_lock_link *temp;
400 const struct cl_io_slice *scan;
402 LASSERT(cl_io_is_loopable(io));
403 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
404 LINVRNT(cl_io_invariant(io));
407 set = &io->ci_lockset;
409 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
410 list_del_init(&link->cill_linkage);
411 if (link->cill_fini != NULL)
412 link->cill_fini(env, link);
415 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
416 list_del_init(&link->cill_linkage);
417 cl_lock_release(env, &link->cill_lock);
418 if (link->cill_fini != NULL)
419 link->cill_fini(env, link);
422 cl_io_for_each_reverse(scan, io) {
423 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
424 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
426 io->ci_state = CIS_UNLOCKED;
429 EXPORT_SYMBOL(cl_io_unlock);
432 * Prepares next iteration of io.
434 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
435 * layers a chance to modify io parameters, e.g., so that lov can restrict io
436 * to a single stripe.
438 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
440 const struct cl_io_slice *scan;
443 LINVRNT(cl_io_is_loopable(io));
444 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
445 LINVRNT(cl_io_invariant(io));
449 cl_io_for_each(scan, io) {
450 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
452 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
458 io->ci_state = CIS_IT_STARTED;
461 EXPORT_SYMBOL(cl_io_iter_init);
464 * Finalizes io iteration.
466 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
468 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
470 const struct cl_io_slice *scan;
472 LINVRNT(cl_io_is_loopable(io));
473 LINVRNT(io->ci_state == CIS_UNLOCKED);
474 LINVRNT(cl_io_invariant(io));
477 cl_io_for_each_reverse(scan, io) {
478 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
479 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
481 io->ci_state = CIS_IT_ENDED;
484 EXPORT_SYMBOL(cl_io_iter_fini);
487 * Records that read or write io progressed \a nob bytes forward.
489 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
491 const struct cl_io_slice *scan;
493 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
495 LINVRNT(cl_io_is_loopable(io));
496 LINVRNT(cl_io_invariant(io));
500 io->u.ci_rw.crw_pos += nob;
501 io->u.ci_rw.crw_count -= nob;
503 /* layers have to be notified. */
504 cl_io_for_each_reverse(scan, io) {
505 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
506 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
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);
612 * Called by read io, to decide the readahead extent
614 * \see cl_io_operations::cio_read_ahead()
616 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
617 pgoff_t start, struct cl_read_ahead *ra)
619 const struct cl_io_slice *scan;
622 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
623 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
624 LINVRNT(cl_io_invariant(io));
627 cl_io_for_each(scan, io) {
628 if (scan->cis_iop->cio_read_ahead == NULL)
631 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
635 RETURN(result > 0 ? 0 : result);
637 EXPORT_SYMBOL(cl_io_read_ahead);
640 * Commit a list of contiguous pages into writeback cache.
642 * \returns 0 if all pages committed, or errcode if error occurred.
643 * \see cl_io_operations::cio_commit_async()
645 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
646 struct cl_page_list *queue, int from, int to,
649 const struct cl_io_slice *scan;
653 cl_io_for_each(scan, io) {
654 if (scan->cis_iop->cio_commit_async == NULL)
656 result = scan->cis_iop->cio_commit_async(env, scan, queue,
663 EXPORT_SYMBOL(cl_io_commit_async);
666 * Submits a list of pages for immediate io.
668 * After the function gets returned, The submitted pages are moved to
669 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
670 * to be submitted, and the pages are errant to submit.
672 * \returns 0 if at least one page was submitted, error code otherwise.
673 * \see cl_io_operations::cio_submit()
675 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
676 enum cl_req_type crt, struct cl_2queue *queue)
678 const struct cl_io_slice *scan;
682 cl_io_for_each(scan, io) {
683 if (scan->cis_iop->cio_submit == NULL)
685 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
690 * If ->cio_submit() failed, no pages were sent.
692 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
695 EXPORT_SYMBOL(cl_io_submit_rw);
698 * Submit a sync_io and wait for the IO to be finished, or error happens.
699 * If \a timeout is zero, it means to wait for the IO unconditionally.
701 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
702 enum cl_req_type iot, struct cl_2queue *queue,
705 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
709 cl_page_list_for_each(pg, &queue->c2_qin) {
710 LASSERT(pg->cp_sync_io == NULL);
711 pg->cp_sync_io = anchor;
714 cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
715 rc = cl_io_submit_rw(env, io, iot, queue);
718 * If some pages weren't sent for any reason (e.g.,
719 * read found up-to-date pages in the cache, or write found
720 * clean pages), count them as completed to avoid infinite
723 cl_page_list_for_each(pg, &queue->c2_qin) {
724 pg->cp_sync_io = NULL;
725 cl_sync_io_note(env, anchor, 1);
728 /* wait for the IO to be finished. */
729 rc = cl_sync_io_wait(env, anchor, timeout);
730 cl_page_list_assume(env, io, &queue->c2_qout);
732 LASSERT(list_empty(&queue->c2_qout.pl_pages));
733 cl_page_list_for_each(pg, &queue->c2_qin)
734 pg->cp_sync_io = NULL;
738 EXPORT_SYMBOL(cl_io_submit_sync);
741 * Cancel an IO which has been submitted by cl_io_submit_rw.
743 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
744 struct cl_page_list *queue)
746 struct cl_page *page;
749 CERROR("Canceling ongoing page trasmission\n");
750 cl_page_list_for_each(page, queue) {
753 rc = cl_page_cancel(env, page);
754 result = result ?: rc;
762 * Pumps io through iterations calling
764 * - cl_io_iter_init()
774 * - cl_io_iter_fini()
776 * repeatedly until there is no more io to do.
778 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
782 LINVRNT(cl_io_is_loopable(io));
789 result = cl_io_iter_init(env, io);
792 result = cl_io_lock(env, io);
795 * Notify layers that locks has been taken,
798 * - llite: kms, short read;
799 * - llite: generic_file_read();
801 result = cl_io_start(env, io);
803 * Send any remaining pending
806 * - llite: ll_rw_stats_tally.
809 cl_io_unlock(env, io);
810 cl_io_rw_advance(env, io, io->ci_nob - nob);
813 cl_io_iter_fini(env, io);
814 } while (result == 0 && io->ci_continue);
816 result = io->ci_result;
817 RETURN(result < 0 ? result : 0);
819 EXPORT_SYMBOL(cl_io_loop);
822 * Adds io slice to the cl_io.
824 * This is called by cl_object_operations::coo_io_init() methods to add a
825 * per-layer state to the io. New state is added at the end of
826 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
828 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
830 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
831 struct cl_object *obj,
832 const struct cl_io_operations *ops)
834 struct list_head *linkage = &slice->cis_linkage;
836 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
837 list_empty(linkage));
840 list_add_tail(linkage, &io->ci_layers);
842 slice->cis_obj = obj;
843 slice->cis_iop = ops;
846 EXPORT_SYMBOL(cl_io_slice_add);
850 * Initializes page list.
852 void cl_page_list_init(struct cl_page_list *plist)
856 INIT_LIST_HEAD(&plist->pl_pages);
857 plist->pl_owner = current;
860 EXPORT_SYMBOL(cl_page_list_init);
863 * Adds a page to a page list.
865 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
868 /* it would be better to check that page is owned by "current" io, but
869 * it is not passed here. */
870 LASSERT(page->cp_owner != NULL);
871 LINVRNT(plist->pl_owner == current);
873 LASSERT(list_empty(&page->cp_batch));
874 list_add_tail(&page->cp_batch, &plist->pl_pages);
876 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
880 EXPORT_SYMBOL(cl_page_list_add);
883 * Removes a page from a page list.
885 void cl_page_list_del(const struct lu_env *env,
886 struct cl_page_list *plist, struct cl_page *page)
888 LASSERT(plist->pl_nr > 0);
889 LASSERT(cl_page_is_vmlocked(env, page));
890 LINVRNT(plist->pl_owner == current);
893 list_del_init(&page->cp_batch);
895 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
896 cl_page_put(env, page);
899 EXPORT_SYMBOL(cl_page_list_del);
902 * Moves a page from one page list to another.
904 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
905 struct cl_page *page)
907 LASSERT(src->pl_nr > 0);
908 LINVRNT(dst->pl_owner == current);
909 LINVRNT(src->pl_owner == current);
912 list_move_tail(&page->cp_batch, &dst->pl_pages);
915 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
919 EXPORT_SYMBOL(cl_page_list_move);
922 * Moves a page from one page list to the head of another list.
924 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
925 struct cl_page *page)
927 LASSERT(src->pl_nr > 0);
928 LINVRNT(dst->pl_owner == current);
929 LINVRNT(src->pl_owner == current);
932 list_move(&page->cp_batch, &dst->pl_pages);
935 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
939 EXPORT_SYMBOL(cl_page_list_move_head);
942 * splice the cl_page_list, just as list head does
944 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
946 struct cl_page *page;
949 LINVRNT(list->pl_owner == current);
950 LINVRNT(head->pl_owner == current);
953 cl_page_list_for_each_safe(page, tmp, list)
954 cl_page_list_move(head, list, page);
957 EXPORT_SYMBOL(cl_page_list_splice);
960 * Disowns pages in a queue.
962 void cl_page_list_disown(const struct lu_env *env,
963 struct cl_io *io, struct cl_page_list *plist)
965 struct cl_page *page;
966 struct cl_page *temp;
968 LINVRNT(plist->pl_owner == current);
971 cl_page_list_for_each_safe(page, temp, plist) {
972 LASSERT(plist->pl_nr > 0);
974 list_del_init(&page->cp_batch);
977 * cl_page_disown0 rather than usual cl_page_disown() is used,
978 * because pages are possibly in CPS_FREEING state already due
979 * to the call to cl_page_list_discard().
982 * XXX cl_page_disown0() will fail if page is not locked.
984 cl_page_disown0(env, io, page);
985 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
987 cl_page_put(env, page);
991 EXPORT_SYMBOL(cl_page_list_disown);
994 * Releases pages from queue.
996 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
998 struct cl_page *page;
999 struct cl_page *temp;
1001 LINVRNT(plist->pl_owner == current);
1004 cl_page_list_for_each_safe(page, temp, plist)
1005 cl_page_list_del(env, plist, page);
1006 LASSERT(plist->pl_nr == 0);
1009 EXPORT_SYMBOL(cl_page_list_fini);
1012 * Assumes all pages in a queue.
1014 void cl_page_list_assume(const struct lu_env *env,
1015 struct cl_io *io, struct cl_page_list *plist)
1017 struct cl_page *page;
1019 LINVRNT(plist->pl_owner == current);
1021 cl_page_list_for_each(page, plist)
1022 cl_page_assume(env, io, page);
1026 * Discards all pages in a queue.
1028 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1029 struct cl_page_list *plist)
1031 struct cl_page *page;
1033 LINVRNT(plist->pl_owner == current);
1035 cl_page_list_for_each(page, plist)
1036 cl_page_discard(env, io, page);
1041 * Initialize dual page queue.
1043 void cl_2queue_init(struct cl_2queue *queue)
1046 cl_page_list_init(&queue->c2_qin);
1047 cl_page_list_init(&queue->c2_qout);
1050 EXPORT_SYMBOL(cl_2queue_init);
1053 * Add a page to the incoming page list of 2-queue.
1055 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1058 cl_page_list_add(&queue->c2_qin, page);
1061 EXPORT_SYMBOL(cl_2queue_add);
1064 * Disown pages in both lists of a 2-queue.
1066 void cl_2queue_disown(const struct lu_env *env,
1067 struct cl_io *io, struct cl_2queue *queue)
1070 cl_page_list_disown(env, io, &queue->c2_qin);
1071 cl_page_list_disown(env, io, &queue->c2_qout);
1074 EXPORT_SYMBOL(cl_2queue_disown);
1077 * Discard (truncate) pages in both lists of a 2-queue.
1079 void cl_2queue_discard(const struct lu_env *env,
1080 struct cl_io *io, struct cl_2queue *queue)
1083 cl_page_list_discard(env, io, &queue->c2_qin);
1084 cl_page_list_discard(env, io, &queue->c2_qout);
1087 EXPORT_SYMBOL(cl_2queue_discard);
1090 * Assume to own the pages in cl_2queue
1092 void cl_2queue_assume(const struct lu_env *env,
1093 struct cl_io *io, struct cl_2queue *queue)
1095 cl_page_list_assume(env, io, &queue->c2_qin);
1096 cl_page_list_assume(env, io, &queue->c2_qout);
1100 * Finalize both page lists of a 2-queue.
1102 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1105 cl_page_list_fini(env, &queue->c2_qout);
1106 cl_page_list_fini(env, &queue->c2_qin);
1109 EXPORT_SYMBOL(cl_2queue_fini);
1112 * Initialize a 2-queue to contain \a page in its incoming page list.
1114 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1117 cl_2queue_init(queue);
1118 cl_2queue_add(queue, page);
1121 EXPORT_SYMBOL(cl_2queue_init_page);
1124 * Returns top-level io.
1126 * \see cl_object_top()
1128 struct cl_io *cl_io_top(struct cl_io *io)
1131 while (io->ci_parent != NULL)
1135 EXPORT_SYMBOL(cl_io_top);
1138 * Prints human readable representation of \a io to the \a f.
1140 void cl_io_print(const struct lu_env *env, void *cookie,
1141 lu_printer_t printer, const struct cl_io *io)
1146 * Fills in attributes that are passed to server together with transfer. Only
1147 * attributes from \a flags may be touched. This can be called multiple times
1148 * for the same request.
1150 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1151 struct cl_req_attr *attr)
1153 struct cl_object *scan;
1156 cl_object_for_each(scan, obj) {
1157 if (scan->co_ops->coo_req_attr_set != NULL)
1158 scan->co_ops->coo_req_attr_set(env, scan, attr);
1162 EXPORT_SYMBOL(cl_req_attr_set);
1164 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1165 * wait for the IO to finish. */
1166 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1168 wake_up_all(&anchor->csi_waitq);
1170 /* it's safe to nuke or reuse anchor now */
1171 atomic_set(&anchor->csi_barrier, 0);
1173 EXPORT_SYMBOL(cl_sync_io_end);
1176 * Initialize synchronous io wait anchor
1178 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1179 void (*end)(const struct lu_env *, struct cl_sync_io *))
1182 memset(anchor, 0, sizeof(*anchor));
1183 init_waitqueue_head(&anchor->csi_waitq);
1184 atomic_set(&anchor->csi_sync_nr, nr);
1185 atomic_set(&anchor->csi_barrier, nr > 0);
1186 anchor->csi_sync_rc = 0;
1187 anchor->csi_end_io = end;
1188 LASSERT(end != NULL);
1191 EXPORT_SYMBOL(cl_sync_io_init);
1194 * Wait until all IO completes. Transfer completion routine has to call
1195 * cl_sync_io_note() for every entity.
1197 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1200 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1205 LASSERT(timeout >= 0);
1207 rc = l_wait_event(anchor->csi_waitq,
1208 atomic_read(&anchor->csi_sync_nr) == 0,
1211 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1212 rc, atomic_read(&anchor->csi_sync_nr));
1214 lwi = (struct l_wait_info) { 0 };
1215 (void)l_wait_event(anchor->csi_waitq,
1216 atomic_read(&anchor->csi_sync_nr) == 0,
1219 rc = anchor->csi_sync_rc;
1221 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1223 /* wait until cl_sync_io_note() has done wakeup */
1224 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1229 EXPORT_SYMBOL(cl_sync_io_wait);
1232 * Indicate that transfer of a single page completed.
1234 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1238 if (anchor->csi_sync_rc == 0 && ioret < 0)
1239 anchor->csi_sync_rc = ioret;
1241 * Synchronous IO done without releasing page lock (e.g., as a part of
1242 * ->{prepare,commit}_write(). Completion is used to signal the end of
1245 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1246 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1247 LASSERT(anchor->csi_end_io != NULL);
1248 anchor->csi_end_io(env, anchor);
1249 /* Can't access anchor any more */
1253 EXPORT_SYMBOL(cl_sync_io_note);