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) {
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));
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,
512 * Adds a lock to a lockset.
514 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
515 struct cl_io_lock_link *link)
520 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
523 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
528 EXPORT_SYMBOL(cl_io_lock_add);
530 static void cl_free_io_lock_link(const struct lu_env *env,
531 struct cl_io_lock_link *link)
537 * Allocates new lock link, and uses it to add a lock to a lockset.
539 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
540 struct cl_lock_descr *descr)
542 struct cl_io_lock_link *link;
548 link->cill_descr = *descr;
549 link->cill_fini = cl_free_io_lock_link;
550 result = cl_io_lock_add(env, io, link);
551 if (result) /* lock match */
552 link->cill_fini(env, link);
558 EXPORT_SYMBOL(cl_io_lock_alloc_add);
561 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
563 int cl_io_start(const struct lu_env *env, struct cl_io *io)
565 const struct cl_io_slice *scan;
568 LINVRNT(cl_io_is_loopable(io));
569 LINVRNT(io->ci_state == CIS_LOCKED);
570 LINVRNT(cl_io_invariant(io));
573 io->ci_state = CIS_IO_GOING;
574 cl_io_for_each(scan, io) {
575 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
577 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
585 EXPORT_SYMBOL(cl_io_start);
588 * Wait until current io iteration is finished by calling
589 * cl_io_operations::cio_end() bottom-to-top.
591 void cl_io_end(const struct lu_env *env, struct cl_io *io)
593 const struct cl_io_slice *scan;
595 LINVRNT(cl_io_is_loopable(io));
596 LINVRNT(io->ci_state == CIS_IO_GOING);
597 LINVRNT(cl_io_invariant(io));
600 cl_io_for_each_reverse(scan, io) {
601 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
602 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
603 /* TODO: error handling. */
605 io->ci_state = CIS_IO_FINISHED;
608 EXPORT_SYMBOL(cl_io_end);
611 * Called by read io, to decide the readahead extent
613 * \see cl_io_operations::cio_read_ahead()
615 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
616 pgoff_t start, struct cl_read_ahead *ra)
618 const struct cl_io_slice *scan;
621 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
622 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
623 LINVRNT(cl_io_invariant(io));
626 cl_io_for_each(scan, io) {
627 if (scan->cis_iop->cio_read_ahead == NULL)
630 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
634 RETURN(result > 0 ? 0 : result);
636 EXPORT_SYMBOL(cl_io_read_ahead);
639 * Commit a list of contiguous pages into writeback cache.
641 * \returns 0 if all pages committed, or errcode if error occurred.
642 * \see cl_io_operations::cio_commit_async()
644 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
645 struct cl_page_list *queue, int from, int to,
648 const struct cl_io_slice *scan;
652 cl_io_for_each(scan, io) {
653 if (scan->cis_iop->cio_commit_async == NULL)
655 result = scan->cis_iop->cio_commit_async(env, scan, queue,
662 EXPORT_SYMBOL(cl_io_commit_async);
665 * Submits a list of pages for immediate io.
667 * After the function gets returned, The submitted pages are moved to
668 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
669 * to be submitted, and the pages are errant to submit.
671 * \returns 0 if at least one page was submitted, error code otherwise.
672 * \see cl_io_operations::cio_submit()
674 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
675 enum cl_req_type crt, struct cl_2queue *queue)
677 const struct cl_io_slice *scan;
681 cl_io_for_each(scan, io) {
682 if (scan->cis_iop->cio_submit == NULL)
684 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
689 * If ->cio_submit() failed, no pages were sent.
691 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
694 EXPORT_SYMBOL(cl_io_submit_rw);
697 * Submit a sync_io and wait for the IO to be finished, or error happens.
698 * If \a timeout is zero, it means to wait for the IO unconditionally.
700 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
701 enum cl_req_type iot, struct cl_2queue *queue,
704 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
708 cl_page_list_for_each(pg, &queue->c2_qin) {
709 LASSERT(pg->cp_sync_io == NULL);
710 pg->cp_sync_io = anchor;
713 cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
714 rc = cl_io_submit_rw(env, io, iot, queue);
717 * If some pages weren't sent for any reason (e.g.,
718 * read found up-to-date pages in the cache, or write found
719 * clean pages), count them as completed to avoid infinite
722 cl_page_list_for_each(pg, &queue->c2_qin) {
723 pg->cp_sync_io = NULL;
724 cl_sync_io_note(env, anchor, 1);
727 /* wait for the IO to be finished. */
728 rc = cl_sync_io_wait(env, anchor, timeout);
729 cl_page_list_assume(env, io, &queue->c2_qout);
731 LASSERT(list_empty(&queue->c2_qout.pl_pages));
732 cl_page_list_for_each(pg, &queue->c2_qin)
733 pg->cp_sync_io = NULL;
737 EXPORT_SYMBOL(cl_io_submit_sync);
740 * Cancel an IO which has been submitted by cl_io_submit_rw.
742 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
743 struct cl_page_list *queue)
745 struct cl_page *page;
748 CERROR("Canceling ongoing page trasmission\n");
749 cl_page_list_for_each(page, queue) {
752 rc = cl_page_cancel(env, page);
753 result = result ?: rc;
761 * Pumps io through iterations calling
763 * - cl_io_iter_init()
773 * - cl_io_iter_fini()
775 * repeatedly until there is no more io to do.
777 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
781 LINVRNT(cl_io_is_loopable(io));
788 result = cl_io_iter_init(env, io);
791 result = cl_io_lock(env, io);
794 * Notify layers that locks has been taken,
797 * - llite: kms, short read;
798 * - llite: generic_file_read();
800 result = cl_io_start(env, io);
802 * Send any remaining pending
805 * - llite: ll_rw_stats_tally.
808 cl_io_unlock(env, io);
809 cl_io_rw_advance(env, io, io->ci_nob - nob);
812 cl_io_iter_fini(env, io);
813 } while (result == 0 && io->ci_continue);
815 result = io->ci_result;
816 RETURN(result < 0 ? result : 0);
818 EXPORT_SYMBOL(cl_io_loop);
821 * Adds io slice to the cl_io.
823 * This is called by cl_object_operations::coo_io_init() methods to add a
824 * per-layer state to the io. New state is added at the end of
825 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
827 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
829 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
830 struct cl_object *obj,
831 const struct cl_io_operations *ops)
833 struct list_head *linkage = &slice->cis_linkage;
835 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
836 list_empty(linkage));
839 list_add_tail(linkage, &io->ci_layers);
841 slice->cis_obj = obj;
842 slice->cis_iop = ops;
845 EXPORT_SYMBOL(cl_io_slice_add);
849 * Initializes page list.
851 void cl_page_list_init(struct cl_page_list *plist)
855 INIT_LIST_HEAD(&plist->pl_pages);
856 plist->pl_owner = current;
859 EXPORT_SYMBOL(cl_page_list_init);
862 * Adds a page to a page list.
864 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
867 /* it would be better to check that page is owned by "current" io, but
868 * it is not passed here. */
869 LASSERT(page->cp_owner != NULL);
870 LINVRNT(plist->pl_owner == current);
872 LASSERT(list_empty(&page->cp_batch));
873 list_add_tail(&page->cp_batch, &plist->pl_pages);
875 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
879 EXPORT_SYMBOL(cl_page_list_add);
882 * Removes a page from a page list.
884 void cl_page_list_del(const struct lu_env *env,
885 struct cl_page_list *plist, struct cl_page *page)
887 LASSERT(plist->pl_nr > 0);
888 LASSERT(cl_page_is_vmlocked(env, page));
889 LINVRNT(plist->pl_owner == current);
892 list_del_init(&page->cp_batch);
894 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
895 cl_page_put(env, page);
898 EXPORT_SYMBOL(cl_page_list_del);
901 * Moves a page from one page list to another.
903 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
904 struct cl_page *page)
906 LASSERT(src->pl_nr > 0);
907 LINVRNT(dst->pl_owner == current);
908 LINVRNT(src->pl_owner == current);
911 list_move_tail(&page->cp_batch, &dst->pl_pages);
914 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
918 EXPORT_SYMBOL(cl_page_list_move);
921 * Moves a page from one page list to the head of another list.
923 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
924 struct cl_page *page)
926 LASSERT(src->pl_nr > 0);
927 LINVRNT(dst->pl_owner == current);
928 LINVRNT(src->pl_owner == current);
931 list_move(&page->cp_batch, &dst->pl_pages);
934 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
938 EXPORT_SYMBOL(cl_page_list_move_head);
941 * splice the cl_page_list, just as list head does
943 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
945 struct cl_page *page;
948 LINVRNT(list->pl_owner == current);
949 LINVRNT(head->pl_owner == current);
952 cl_page_list_for_each_safe(page, tmp, list)
953 cl_page_list_move(head, list, page);
956 EXPORT_SYMBOL(cl_page_list_splice);
959 * Disowns pages in a queue.
961 void cl_page_list_disown(const struct lu_env *env,
962 struct cl_io *io, struct cl_page_list *plist)
964 struct cl_page *page;
965 struct cl_page *temp;
967 LINVRNT(plist->pl_owner == current);
970 cl_page_list_for_each_safe(page, temp, plist) {
971 LASSERT(plist->pl_nr > 0);
973 list_del_init(&page->cp_batch);
976 * cl_page_disown0 rather than usual cl_page_disown() is used,
977 * because pages are possibly in CPS_FREEING state already due
978 * to the call to cl_page_list_discard().
981 * XXX cl_page_disown0() will fail if page is not locked.
983 cl_page_disown0(env, io, page);
984 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
986 cl_page_put(env, page);
990 EXPORT_SYMBOL(cl_page_list_disown);
993 * Releases pages from queue.
995 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
997 struct cl_page *page;
998 struct cl_page *temp;
1000 LINVRNT(plist->pl_owner == current);
1003 cl_page_list_for_each_safe(page, temp, plist)
1004 cl_page_list_del(env, plist, page);
1005 LASSERT(plist->pl_nr == 0);
1008 EXPORT_SYMBOL(cl_page_list_fini);
1011 * Owns all pages in a queue.
1013 int cl_page_list_own(const struct lu_env *env,
1014 struct cl_io *io, struct cl_page_list *plist)
1016 struct cl_page *page;
1017 struct cl_page *temp;
1020 LINVRNT(plist->pl_owner == current);
1024 cl_page_list_for_each_safe(page, temp, plist) {
1025 if (cl_page_own(env, io, page) == 0)
1026 result = result ?: page->cp_error;
1028 cl_page_list_del(env, plist, page);
1034 * Assumes all pages in a queue.
1036 void cl_page_list_assume(const struct lu_env *env,
1037 struct cl_io *io, struct cl_page_list *plist)
1039 struct cl_page *page;
1041 LINVRNT(plist->pl_owner == current);
1043 cl_page_list_for_each(page, plist)
1044 cl_page_assume(env, io, page);
1048 * Discards all pages in a queue.
1050 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1051 struct cl_page_list *plist)
1053 struct cl_page *page;
1055 LINVRNT(plist->pl_owner == current);
1057 cl_page_list_for_each(page, plist)
1058 cl_page_discard(env, io, page);
1063 * Initialize dual page queue.
1065 void cl_2queue_init(struct cl_2queue *queue)
1068 cl_page_list_init(&queue->c2_qin);
1069 cl_page_list_init(&queue->c2_qout);
1072 EXPORT_SYMBOL(cl_2queue_init);
1075 * Add a page to the incoming page list of 2-queue.
1077 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1080 cl_page_list_add(&queue->c2_qin, page);
1083 EXPORT_SYMBOL(cl_2queue_add);
1086 * Disown pages in both lists of a 2-queue.
1088 void cl_2queue_disown(const struct lu_env *env,
1089 struct cl_io *io, struct cl_2queue *queue)
1092 cl_page_list_disown(env, io, &queue->c2_qin);
1093 cl_page_list_disown(env, io, &queue->c2_qout);
1096 EXPORT_SYMBOL(cl_2queue_disown);
1099 * Discard (truncate) pages in both lists of a 2-queue.
1101 void cl_2queue_discard(const struct lu_env *env,
1102 struct cl_io *io, struct cl_2queue *queue)
1105 cl_page_list_discard(env, io, &queue->c2_qin);
1106 cl_page_list_discard(env, io, &queue->c2_qout);
1109 EXPORT_SYMBOL(cl_2queue_discard);
1112 * Assume to own the pages in cl_2queue
1114 void cl_2queue_assume(const struct lu_env *env,
1115 struct cl_io *io, struct cl_2queue *queue)
1117 cl_page_list_assume(env, io, &queue->c2_qin);
1118 cl_page_list_assume(env, io, &queue->c2_qout);
1122 * Finalize both page lists of a 2-queue.
1124 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1127 cl_page_list_fini(env, &queue->c2_qout);
1128 cl_page_list_fini(env, &queue->c2_qin);
1131 EXPORT_SYMBOL(cl_2queue_fini);
1134 * Initialize a 2-queue to contain \a page in its incoming page list.
1136 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1139 cl_2queue_init(queue);
1140 cl_2queue_add(queue, page);
1143 EXPORT_SYMBOL(cl_2queue_init_page);
1146 * Returns top-level io.
1148 * \see cl_object_top()
1150 struct cl_io *cl_io_top(struct cl_io *io)
1153 while (io->ci_parent != NULL)
1157 EXPORT_SYMBOL(cl_io_top);
1160 * Prints human readable representation of \a io to the \a f.
1162 void cl_io_print(const struct lu_env *env, void *cookie,
1163 lu_printer_t printer, const struct cl_io *io)
1168 * Fills in attributes that are passed to server together with transfer. Only
1169 * attributes from \a flags may be touched. This can be called multiple times
1170 * for the same request.
1172 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1173 struct cl_req_attr *attr)
1175 struct cl_object *scan;
1178 cl_object_for_each(scan, obj) {
1179 if (scan->co_ops->coo_req_attr_set != NULL)
1180 scan->co_ops->coo_req_attr_set(env, scan, attr);
1184 EXPORT_SYMBOL(cl_req_attr_set);
1186 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1187 * wait for the IO to finish. */
1188 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1190 wake_up_all(&anchor->csi_waitq);
1192 /* it's safe to nuke or reuse anchor now */
1193 atomic_set(&anchor->csi_barrier, 0);
1195 EXPORT_SYMBOL(cl_sync_io_end);
1198 * Initialize synchronous io wait anchor
1200 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1201 void (*end)(const struct lu_env *, struct cl_sync_io *))
1204 memset(anchor, 0, sizeof(*anchor));
1205 init_waitqueue_head(&anchor->csi_waitq);
1206 atomic_set(&anchor->csi_sync_nr, nr);
1207 atomic_set(&anchor->csi_barrier, nr > 0);
1208 anchor->csi_sync_rc = 0;
1209 anchor->csi_end_io = end;
1210 LASSERT(end != NULL);
1213 EXPORT_SYMBOL(cl_sync_io_init);
1216 * Wait until all IO completes. Transfer completion routine has to call
1217 * cl_sync_io_note() for every entity.
1219 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1222 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1227 LASSERT(timeout >= 0);
1229 rc = l_wait_event(anchor->csi_waitq,
1230 atomic_read(&anchor->csi_sync_nr) == 0,
1233 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1234 rc, atomic_read(&anchor->csi_sync_nr));
1236 lwi = (struct l_wait_info) { 0 };
1237 (void)l_wait_event(anchor->csi_waitq,
1238 atomic_read(&anchor->csi_sync_nr) == 0,
1241 rc = anchor->csi_sync_rc;
1243 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1245 /* wait until cl_sync_io_note() has done wakeup */
1246 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1251 EXPORT_SYMBOL(cl_sync_io_wait);
1254 * Indicate that transfer of a single page completed.
1256 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1260 if (anchor->csi_sync_rc == 0 && ioret < 0)
1261 anchor->csi_sync_rc = ioret;
1263 * Synchronous IO done without releasing page lock (e.g., as a part of
1264 * ->{prepare,commit}_write(). Completion is used to signal the end of
1267 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1268 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1269 LASSERT(anchor->csi_end_io != NULL);
1270 anchor->csi_end_io(env, anchor);
1271 /* Can't access anchor any more */
1275 EXPORT_SYMBOL(cl_sync_io_note);