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, 2015, 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 "../include/obd_class.h"
41 #include "../include/obd_support.h"
42 #include "../include/lustre_fid.h"
43 #include <linux/list.h>
44 #include <linux/sched.h>
45 #include "../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));
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));
107 while (!list_empty(&io->ci_layers)) {
108 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
110 list_del_init(&slice->cis_linkage);
111 if (slice->cis_iop->op[io->ci_type].cio_fini)
112 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
114 * Invalidate slice to catch use after free. This assumes that
115 * slices are allocated within session and can be touched
116 * after ->cio_fini() returns.
118 slice->cis_io = NULL;
120 io->ci_state = CIS_FINI;
121 info = cl_env_info(env);
122 if (info->clt_current_io == io)
123 info->clt_current_io = NULL;
125 /* sanity check for layout change */
126 switch (io->ci_type) {
129 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));
146 EXPORT_SYMBOL(cl_io_fini);
148 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
149 enum cl_io_type iot, struct cl_object *obj)
151 struct cl_object *scan;
154 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
155 LINVRNT(cl_io_type_is_valid(iot));
156 LINVRNT(cl_io_invariant(io));
159 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
160 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
161 INIT_LIST_HEAD(&io->ci_layers);
164 cl_object_for_each(scan, obj) {
165 if (scan->co_ops->coo_io_init) {
166 result = scan->co_ops->coo_io_init(env, scan, io);
172 io->ci_state = CIS_INIT;
177 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
179 * \pre obj != cl_object_top(obj)
181 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
182 enum cl_io_type iot, struct cl_object *obj)
184 struct cl_thread_info *info = cl_env_info(env);
186 LASSERT(obj != cl_object_top(obj));
187 if (!info->clt_current_io)
188 info->clt_current_io = io;
189 return cl_io_init0(env, io, iot, obj);
191 EXPORT_SYMBOL(cl_io_sub_init);
194 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
196 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
197 * what the latter returned.
199 * \pre obj == cl_object_top(obj)
200 * \pre cl_io_type_is_valid(iot)
201 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
203 int cl_io_init(const struct lu_env *env, struct cl_io *io,
204 enum cl_io_type iot, struct cl_object *obj)
206 struct cl_thread_info *info = cl_env_info(env);
208 LASSERT(obj == cl_object_top(obj));
209 LASSERT(!info->clt_current_io);
211 info->clt_current_io = io;
212 return cl_io_init0(env, io, iot, obj);
214 EXPORT_SYMBOL(cl_io_init);
217 * Initialize read or write io.
219 * \pre iot == CIT_READ || iot == CIT_WRITE
221 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
222 enum cl_io_type iot, loff_t pos, size_t count)
224 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
227 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
228 "io range: %u [%llu, %llu) %u %u\n",
229 iot, (__u64)pos, (__u64)pos + count,
230 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
231 io->u.ci_rw.crw_pos = pos;
232 io->u.ci_rw.crw_count = count;
233 return cl_io_init(env, io, iot, io->ci_obj);
235 EXPORT_SYMBOL(cl_io_rw_init);
237 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
238 const struct cl_lock_descr *d1)
240 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
241 lu_object_fid(&d1->cld_obj->co_lu));
245 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
247 static void cl_io_locks_sort(struct cl_io *io)
251 /* hidden treasure: bubble sort for now. */
253 struct cl_io_lock_link *curr;
254 struct cl_io_lock_link *prev;
255 struct cl_io_lock_link *temp;
260 list_for_each_entry_safe(curr, temp,
261 &io->ci_lockset.cls_todo,
264 switch (cl_lock_descr_sort(&prev->cill_descr,
265 &curr->cill_descr)) {
268 * IMPOSSIBLE: Identical locks are
275 list_move_tail(&curr->cill_linkage,
276 &prev->cill_linkage);
278 continue; /* don't change prev: it's
281 case -1: /* already in order */
290 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
291 const struct cl_lock_descr *d1)
293 d0->cld_start = min(d0->cld_start, d1->cld_start);
294 d0->cld_end = max(d0->cld_end, d1->cld_end);
296 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
297 d0->cld_mode = CLM_WRITE;
299 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
300 d0->cld_mode = CLM_GROUP;
303 static int cl_lockset_merge(const struct cl_lockset *set,
304 const struct cl_lock_descr *need)
306 struct cl_io_lock_link *scan;
308 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
309 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
312 /* Merge locks for the same object because ldlm lock server
313 * may expand the lock extent, otherwise there is a deadlock
314 * case if two conflicted locks are queueud for the same object
315 * and lock server expands one lock to overlap the another.
316 * The side effect is that it can generate a multi-stripe lock
317 * that may cause casacading problem
319 cl_lock_descr_merge(&scan->cill_descr, need);
320 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
321 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
322 scan->cill_descr.cld_end);
328 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
329 struct cl_lockset *set)
331 struct cl_io_lock_link *link;
332 struct cl_io_lock_link *temp;
336 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
337 result = cl_lock_request(env, io, &link->cill_lock);
341 list_move(&link->cill_linkage, &set->cls_done);
347 * Takes locks necessary for the current iteration of io.
349 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
350 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
353 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
355 const struct cl_io_slice *scan;
358 LINVRNT(cl_io_is_loopable(io));
359 LINVRNT(io->ci_state == CIS_IT_STARTED);
360 LINVRNT(cl_io_invariant(io));
362 cl_io_for_each(scan, io) {
363 if (!scan->cis_iop->op[io->ci_type].cio_lock)
365 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
370 cl_io_locks_sort(io);
371 result = cl_lockset_lock(env, io, &io->ci_lockset);
374 cl_io_unlock(env, io);
376 io->ci_state = CIS_LOCKED;
379 EXPORT_SYMBOL(cl_io_lock);
382 * Release locks takes by io.
384 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
386 struct cl_lockset *set;
387 struct cl_io_lock_link *link;
388 struct cl_io_lock_link *temp;
389 const struct cl_io_slice *scan;
391 LASSERT(cl_io_is_loopable(io));
392 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
393 LINVRNT(cl_io_invariant(io));
395 set = &io->ci_lockset;
397 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
398 list_del_init(&link->cill_linkage);
400 link->cill_fini(env, link);
403 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
404 list_del_init(&link->cill_linkage);
405 cl_lock_release(env, &link->cill_lock);
407 link->cill_fini(env, link);
410 cl_io_for_each_reverse(scan, io) {
411 if (scan->cis_iop->op[io->ci_type].cio_unlock)
412 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
414 io->ci_state = CIS_UNLOCKED;
416 EXPORT_SYMBOL(cl_io_unlock);
419 * Prepares next iteration of io.
421 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
422 * layers a chance to modify io parameters, e.g., so that lov can restrict io
423 * to a single stripe.
425 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
427 const struct cl_io_slice *scan;
430 LINVRNT(cl_io_is_loopable(io));
431 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
432 LINVRNT(cl_io_invariant(io));
435 cl_io_for_each(scan, io) {
436 if (!scan->cis_iop->op[io->ci_type].cio_iter_init)
438 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
444 io->ci_state = CIS_IT_STARTED;
447 EXPORT_SYMBOL(cl_io_iter_init);
450 * Finalizes io iteration.
452 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
454 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
456 const struct cl_io_slice *scan;
458 LINVRNT(cl_io_is_loopable(io));
459 LINVRNT(io->ci_state == CIS_UNLOCKED);
460 LINVRNT(cl_io_invariant(io));
462 cl_io_for_each_reverse(scan, io) {
463 if (scan->cis_iop->op[io->ci_type].cio_iter_fini)
464 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
466 io->ci_state = CIS_IT_ENDED;
468 EXPORT_SYMBOL(cl_io_iter_fini);
471 * Records that read or write io progressed \a nob bytes forward.
473 static void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io,
476 const struct cl_io_slice *scan;
478 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
480 LINVRNT(cl_io_is_loopable(io));
481 LINVRNT(cl_io_invariant(io));
483 io->u.ci_rw.crw_pos += nob;
484 io->u.ci_rw.crw_count -= nob;
486 /* layers have to be notified. */
487 cl_io_for_each_reverse(scan, io) {
488 if (scan->cis_iop->op[io->ci_type].cio_advance)
489 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
495 * Adds a lock to a lockset.
497 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
498 struct cl_io_lock_link *link)
502 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr)) {
505 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
510 EXPORT_SYMBOL(cl_io_lock_add);
512 static void cl_free_io_lock_link(const struct lu_env *env,
513 struct cl_io_lock_link *link)
519 * Allocates new lock link, and uses it to add a lock to a lockset.
521 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
522 struct cl_lock_descr *descr)
524 struct cl_io_lock_link *link;
527 link = kzalloc(sizeof(*link), GFP_NOFS);
529 link->cill_descr = *descr;
530 link->cill_fini = cl_free_io_lock_link;
531 result = cl_io_lock_add(env, io, link);
532 if (result) /* lock match */
533 link->cill_fini(env, link);
540 EXPORT_SYMBOL(cl_io_lock_alloc_add);
543 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
545 int cl_io_start(const struct lu_env *env, struct cl_io *io)
547 const struct cl_io_slice *scan;
550 LINVRNT(cl_io_is_loopable(io));
551 LINVRNT(io->ci_state == CIS_LOCKED);
552 LINVRNT(cl_io_invariant(io));
554 io->ci_state = CIS_IO_GOING;
555 cl_io_for_each(scan, io) {
556 if (!scan->cis_iop->op[io->ci_type].cio_start)
558 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
566 EXPORT_SYMBOL(cl_io_start);
569 * Wait until current io iteration is finished by calling
570 * cl_io_operations::cio_end() bottom-to-top.
572 void cl_io_end(const struct lu_env *env, struct cl_io *io)
574 const struct cl_io_slice *scan;
576 LINVRNT(cl_io_is_loopable(io));
577 LINVRNT(io->ci_state == CIS_IO_GOING);
578 LINVRNT(cl_io_invariant(io));
580 cl_io_for_each_reverse(scan, io) {
581 if (scan->cis_iop->op[io->ci_type].cio_end)
582 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
583 /* TODO: error handling. */
585 io->ci_state = CIS_IO_FINISHED;
587 EXPORT_SYMBOL(cl_io_end);
590 * Called by read io, to decide the readahead extent
592 * \see cl_io_operations::cio_read_ahead()
594 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
595 pgoff_t start, struct cl_read_ahead *ra)
597 const struct cl_io_slice *scan;
600 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
601 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
602 LINVRNT(cl_io_invariant(io));
604 cl_io_for_each(scan, io) {
605 if (!scan->cis_iop->cio_read_ahead)
608 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
612 return result > 0 ? 0 : result;
614 EXPORT_SYMBOL(cl_io_read_ahead);
617 * Commit a list of contiguous pages into writeback cache.
619 * \returns 0 if all pages committed, or errcode if error occurred.
620 * \see cl_io_operations::cio_commit_async()
622 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
623 struct cl_page_list *queue, int from, int to,
626 const struct cl_io_slice *scan;
629 cl_io_for_each(scan, io) {
630 if (!scan->cis_iop->cio_commit_async)
632 result = scan->cis_iop->cio_commit_async(env, scan, queue,
639 EXPORT_SYMBOL(cl_io_commit_async);
642 * Submits a list of pages for immediate io.
644 * After the function gets returned, The submitted pages are moved to
645 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
646 * to be submitted, and the pages are errant to submit.
648 * \returns 0 if at least one page was submitted, error code otherwise.
649 * \see cl_io_operations::cio_submit()
651 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
652 enum cl_req_type crt, struct cl_2queue *queue)
654 const struct cl_io_slice *scan;
657 cl_io_for_each(scan, io) {
658 if (!scan->cis_iop->cio_submit)
660 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
665 * If ->cio_submit() failed, no pages were sent.
667 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
670 EXPORT_SYMBOL(cl_io_submit_rw);
672 static void cl_page_list_assume(const struct lu_env *env,
673 struct cl_io *io, struct cl_page_list *plist);
676 * Submit a sync_io and wait for the IO to be finished, or error happens.
677 * If \a timeout is zero, it means to wait for the IO unconditionally.
679 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
680 enum cl_req_type iot, struct cl_2queue *queue,
683 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
687 cl_page_list_for_each(pg, &queue->c2_qin) {
688 LASSERT(!pg->cp_sync_io);
689 pg->cp_sync_io = anchor;
692 cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
693 rc = cl_io_submit_rw(env, io, iot, queue);
696 * If some pages weren't sent for any reason (e.g.,
697 * read found up-to-date pages in the cache, or write found
698 * clean pages), count them as completed to avoid infinite
701 cl_page_list_for_each(pg, &queue->c2_qin) {
702 pg->cp_sync_io = NULL;
703 cl_sync_io_note(env, anchor, 1);
706 /* wait for the IO to be finished. */
707 rc = cl_sync_io_wait(env, anchor, timeout);
708 cl_page_list_assume(env, io, &queue->c2_qout);
710 LASSERT(list_empty(&queue->c2_qout.pl_pages));
711 cl_page_list_for_each(pg, &queue->c2_qin)
712 pg->cp_sync_io = NULL;
716 EXPORT_SYMBOL(cl_io_submit_sync);
721 * Pumps io through iterations calling
723 * - cl_io_iter_init()
733 * - cl_io_iter_fini()
735 * repeatedly until there is no more io to do.
737 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
741 LINVRNT(cl_io_is_loopable(io));
747 result = cl_io_iter_init(env, io);
750 result = cl_io_lock(env, io);
753 * Notify layers that locks has been taken,
756 * - llite: kms, short read;
757 * - llite: generic_file_read();
759 result = cl_io_start(env, io);
761 * Send any remaining pending
764 * - llite: ll_rw_stats_tally.
767 cl_io_unlock(env, io);
768 cl_io_rw_advance(env, io, io->ci_nob - nob);
771 cl_io_iter_fini(env, io);
772 } while (result == 0 && io->ci_continue);
774 result = io->ci_result;
775 return result < 0 ? result : 0;
777 EXPORT_SYMBOL(cl_io_loop);
780 * Adds io slice to the cl_io.
782 * This is called by cl_object_operations::coo_io_init() methods to add a
783 * per-layer state to the io. New state is added at the end of
784 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
786 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
788 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
789 struct cl_object *obj,
790 const struct cl_io_operations *ops)
792 struct list_head *linkage = &slice->cis_linkage;
794 LASSERT((!linkage->prev && !linkage->next) ||
795 list_empty(linkage));
797 list_add_tail(linkage, &io->ci_layers);
799 slice->cis_obj = obj;
800 slice->cis_iop = ops;
802 EXPORT_SYMBOL(cl_io_slice_add);
805 * Initializes page list.
807 void cl_page_list_init(struct cl_page_list *plist)
810 INIT_LIST_HEAD(&plist->pl_pages);
811 plist->pl_owner = current;
813 EXPORT_SYMBOL(cl_page_list_init);
816 * Adds a page to a page list.
818 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
820 /* it would be better to check that page is owned by "current" io, but
821 * it is not passed here.
823 LASSERT(page->cp_owner);
824 LINVRNT(plist->pl_owner == current);
826 LASSERT(list_empty(&page->cp_batch));
827 list_add_tail(&page->cp_batch, &plist->pl_pages);
829 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
832 EXPORT_SYMBOL(cl_page_list_add);
835 * Removes a page from a page list.
837 void cl_page_list_del(const struct lu_env *env, struct cl_page_list *plist,
838 struct cl_page *page)
840 LASSERT(plist->pl_nr > 0);
841 LASSERT(cl_page_is_vmlocked(env, page));
842 LINVRNT(plist->pl_owner == current);
844 list_del_init(&page->cp_batch);
846 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
847 cl_page_put(env, page);
849 EXPORT_SYMBOL(cl_page_list_del);
852 * Moves a page from one page list to another.
854 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
855 struct cl_page *page)
857 LASSERT(src->pl_nr > 0);
858 LINVRNT(dst->pl_owner == current);
859 LINVRNT(src->pl_owner == current);
861 list_move_tail(&page->cp_batch, &dst->pl_pages);
864 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
867 EXPORT_SYMBOL(cl_page_list_move);
870 * Moves a page from one page list to the head of another list.
872 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
873 struct cl_page *page)
875 LASSERT(src->pl_nr > 0);
876 LINVRNT(dst->pl_owner == current);
877 LINVRNT(src->pl_owner == current);
879 list_move(&page->cp_batch, &dst->pl_pages);
882 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
885 EXPORT_SYMBOL(cl_page_list_move_head);
888 * splice the cl_page_list, just as list head does
890 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
892 struct cl_page *page;
895 LINVRNT(list->pl_owner == current);
896 LINVRNT(head->pl_owner == current);
898 cl_page_list_for_each_safe(page, tmp, list)
899 cl_page_list_move(head, list, page);
901 EXPORT_SYMBOL(cl_page_list_splice);
905 * Disowns pages in a queue.
907 void cl_page_list_disown(const struct lu_env *env,
908 struct cl_io *io, struct cl_page_list *plist)
910 struct cl_page *page;
911 struct cl_page *temp;
913 LINVRNT(plist->pl_owner == current);
915 cl_page_list_for_each_safe(page, temp, plist) {
916 LASSERT(plist->pl_nr > 0);
918 list_del_init(&page->cp_batch);
921 * cl_page_disown0 rather than usual cl_page_disown() is used,
922 * because pages are possibly in CPS_FREEING state already due
923 * to the call to cl_page_list_discard().
926 * XXX cl_page_disown0() will fail if page is not locked.
928 cl_page_disown0(env, io, page);
929 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
931 cl_page_put(env, page);
934 EXPORT_SYMBOL(cl_page_list_disown);
937 * Releases pages from queue.
939 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
941 struct cl_page *page;
942 struct cl_page *temp;
944 LINVRNT(plist->pl_owner == current);
946 cl_page_list_for_each_safe(page, temp, plist)
947 cl_page_list_del(env, plist, page);
948 LASSERT(plist->pl_nr == 0);
950 EXPORT_SYMBOL(cl_page_list_fini);
953 * Assumes all pages in a queue.
955 static void cl_page_list_assume(const struct lu_env *env,
956 struct cl_io *io, struct cl_page_list *plist)
958 struct cl_page *page;
960 LINVRNT(plist->pl_owner == current);
962 cl_page_list_for_each(page, plist)
963 cl_page_assume(env, io, page);
967 * Discards all pages in a queue.
969 static void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
970 struct cl_page_list *plist)
972 struct cl_page *page;
974 LINVRNT(plist->pl_owner == current);
975 cl_page_list_for_each(page, plist)
976 cl_page_discard(env, io, page);
980 * Initialize dual page queue.
982 void cl_2queue_init(struct cl_2queue *queue)
984 cl_page_list_init(&queue->c2_qin);
985 cl_page_list_init(&queue->c2_qout);
987 EXPORT_SYMBOL(cl_2queue_init);
990 * Disown pages in both lists of a 2-queue.
992 void cl_2queue_disown(const struct lu_env *env,
993 struct cl_io *io, struct cl_2queue *queue)
995 cl_page_list_disown(env, io, &queue->c2_qin);
996 cl_page_list_disown(env, io, &queue->c2_qout);
998 EXPORT_SYMBOL(cl_2queue_disown);
1001 * Discard (truncate) pages in both lists of a 2-queue.
1003 void cl_2queue_discard(const struct lu_env *env,
1004 struct cl_io *io, struct cl_2queue *queue)
1006 cl_page_list_discard(env, io, &queue->c2_qin);
1007 cl_page_list_discard(env, io, &queue->c2_qout);
1009 EXPORT_SYMBOL(cl_2queue_discard);
1012 * Finalize both page lists of a 2-queue.
1014 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1016 cl_page_list_fini(env, &queue->c2_qout);
1017 cl_page_list_fini(env, &queue->c2_qin);
1019 EXPORT_SYMBOL(cl_2queue_fini);
1022 * Initialize a 2-queue to contain \a page in its incoming page list.
1024 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1026 cl_2queue_init(queue);
1028 * Add a page to the incoming page list of 2-queue.
1030 cl_page_list_add(&queue->c2_qin, page);
1032 EXPORT_SYMBOL(cl_2queue_init_page);
1035 * Returns top-level io.
1037 * \see cl_object_top()
1039 struct cl_io *cl_io_top(struct cl_io *io)
1041 while (io->ci_parent)
1045 EXPORT_SYMBOL(cl_io_top);
1048 * Fills in attributes that are passed to server together with transfer. Only
1049 * attributes from \a flags may be touched. This can be called multiple times
1050 * for the same request.
1052 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1053 struct cl_req_attr *attr)
1055 struct cl_object *scan;
1057 cl_object_for_each(scan, obj) {
1058 if (scan->co_ops->coo_req_attr_set)
1059 scan->co_ops->coo_req_attr_set(env, scan, attr);
1062 EXPORT_SYMBOL(cl_req_attr_set);
1064 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1065 * wait for the IO to finish.
1067 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1069 wake_up_all(&anchor->csi_waitq);
1071 /* it's safe to nuke or reuse anchor now */
1072 atomic_set(&anchor->csi_barrier, 0);
1074 EXPORT_SYMBOL(cl_sync_io_end);
1077 * Initialize synchronous io wait anchor
1079 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1080 void (*end)(const struct lu_env *, struct cl_sync_io *))
1082 memset(anchor, 0, sizeof(*anchor));
1083 init_waitqueue_head(&anchor->csi_waitq);
1084 atomic_set(&anchor->csi_sync_nr, nr);
1085 atomic_set(&anchor->csi_barrier, nr > 0);
1086 anchor->csi_sync_rc = 0;
1087 anchor->csi_end_io = end;
1090 EXPORT_SYMBOL(cl_sync_io_init);
1093 * Wait until all IO completes. Transfer completion routine has to call
1094 * cl_sync_io_note() for every entity.
1096 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1099 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1103 LASSERT(timeout >= 0);
1105 rc = l_wait_event(anchor->csi_waitq,
1106 atomic_read(&anchor->csi_sync_nr) == 0,
1109 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1110 rc, atomic_read(&anchor->csi_sync_nr));
1112 lwi = (struct l_wait_info) { 0 };
1113 (void)l_wait_event(anchor->csi_waitq,
1114 atomic_read(&anchor->csi_sync_nr) == 0,
1117 rc = anchor->csi_sync_rc;
1119 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1121 /* wait until cl_sync_io_note() has done wakeup */
1122 while (unlikely(atomic_read(&anchor->csi_barrier) != 0))
1128 EXPORT_SYMBOL(cl_sync_io_wait);
1131 * Indicate that transfer of a single page completed.
1133 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1136 if (anchor->csi_sync_rc == 0 && ioret < 0)
1137 anchor->csi_sync_rc = ioret;
1139 * Synchronous IO done without releasing page lock (e.g., as a part of
1140 * ->{prepare,commit}_write(). Completion is used to signal the end of
1143 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1144 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1145 LASSERT(anchor->csi_end_io);
1146 anchor->csi_end_io(env, anchor);
1147 /* Can't access anchor any more */
1150 EXPORT_SYMBOL(cl_sync_io_note);