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, 2017, 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 <linux/list_sort.h>
43 #include <obd_class.h>
44 #include <obd_support.h>
45 #include <lustre_fid.h>
46 #include <cl_object.h>
47 #include "cl_internal.h"
48 #include <libcfs/crypto/llcrypt.h>
50 /*****************************************************************************
56 static inline int cl_io_type_is_valid(enum cl_io_type type)
58 return CIT_READ <= type && type < CIT_OP_NR;
61 static inline int cl_io_is_loopable(const struct cl_io *io)
63 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
67 * cl_io invariant that holds at all times when exported cl_io_*() functions
68 * are entered and left.
70 static int cl_io_invariant(const struct cl_io *io)
77 * io can own pages only when it is ongoing. Sub-io might
78 * still be in CIS_LOCKED state when top-io is in
81 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
82 (io->ci_state == CIS_LOCKED && up != NULL));
86 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
88 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
90 struct cl_io_slice *slice;
92 LINVRNT(cl_io_type_is_valid(io->ci_type));
93 LINVRNT(cl_io_invariant(io));
96 while (!list_empty(&io->ci_layers)) {
97 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
99 list_del_init(&slice->cis_linkage);
100 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
101 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
103 * Invalidate slice to catch use after free. This assumes that
104 * slices are allocated within session and can be touched
105 * after ->cio_fini() returns.
107 slice->cis_io = NULL;
109 io->ci_state = CIS_FINI;
111 /* sanity check for layout change */
112 switch(io->ci_type) {
115 case CIT_DATA_VERSION:
119 LASSERT(!io->ci_need_restart);
123 /* Check ignore layout change conf */
124 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
125 !io->ci_need_restart));
136 EXPORT_SYMBOL(cl_io_fini);
138 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
139 enum cl_io_type iot, struct cl_object *obj)
141 struct cl_object *scan;
144 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
145 LINVRNT(cl_io_type_is_valid(iot));
146 LINVRNT(cl_io_invariant(io));
150 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
151 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
152 INIT_LIST_HEAD(&io->ci_layers);
155 cl_object_for_each(scan, obj) {
156 if (scan->co_ops->coo_io_init != NULL) {
157 result = scan->co_ops->coo_io_init(env, scan, io);
163 io->ci_state = CIS_INIT;
168 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
170 * \pre obj != cl_object_top(obj)
172 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
173 enum cl_io_type iot, struct cl_object *obj)
175 LASSERT(obj != cl_object_top(obj));
177 return cl_io_init0(env, io, iot, obj);
179 EXPORT_SYMBOL(cl_io_sub_init);
182 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
184 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
185 * what the latter returned.
187 * \pre obj == cl_object_top(obj)
188 * \pre cl_io_type_is_valid(iot)
189 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
191 int cl_io_init(const struct lu_env *env, struct cl_io *io,
192 enum cl_io_type iot, struct cl_object *obj)
194 LASSERT(obj == cl_object_top(obj));
196 /* clear I/O restart from previous instance */
197 io->ci_need_restart = 0;
199 return cl_io_init0(env, io, iot, obj);
201 EXPORT_SYMBOL(cl_io_init);
204 * Initialize read or write io.
206 * \pre iot == CIT_READ || iot == CIT_WRITE
208 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
209 enum cl_io_type iot, loff_t pos, size_t count)
211 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
212 LINVRNT(io->ci_obj != NULL);
215 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
216 "io range: %u [%llu, %llu) %u %u\n",
217 iot, (__u64)pos, (__u64)pos + count,
218 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
219 io->u.ci_rw.crw_pos = pos;
220 io->u.ci_rw.crw_count = count;
221 RETURN(cl_io_init(env, io, iot, io->ci_obj));
223 EXPORT_SYMBOL(cl_io_rw_init);
225 static int cl_lock_descr_cmp(void *priv,
226 struct list_head *a, struct list_head *b)
228 const struct cl_io_lock_link *l0 = list_entry(a, struct cl_io_lock_link,
230 const struct cl_io_lock_link *l1 = list_entry(b, struct cl_io_lock_link,
232 const struct cl_lock_descr *d0 = &l0->cill_descr;
233 const struct cl_lock_descr *d1 = &l1->cill_descr;
235 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
236 lu_object_fid(&d1->cld_obj->co_lu));
239 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
240 const struct cl_lock_descr *d1)
242 d0->cld_start = min(d0->cld_start, d1->cld_start);
243 d0->cld_end = max(d0->cld_end, d1->cld_end);
245 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
246 d0->cld_mode = CLM_WRITE;
248 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
249 d0->cld_mode = CLM_GROUP;
252 static int cl_lockset_merge(const struct cl_lockset *set,
253 const struct cl_lock_descr *need)
255 struct cl_io_lock_link *scan;
258 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
259 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
262 /* Merge locks for the same object because ldlm lock server
263 * may expand the lock extent, otherwise there is a deadlock
264 * case if two conflicted locks are queueud for the same object
265 * and lock server expands one lock to overlap the another.
266 * The side effect is that it can generate a multi-stripe lock
267 * that may cause casacading problem */
268 cl_lock_descr_merge(&scan->cill_descr, need);
269 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
270 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
271 scan->cill_descr.cld_end);
277 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
278 struct cl_lockset *set)
280 struct cl_io_lock_link *link;
281 struct cl_io_lock_link *temp;
286 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
287 result = cl_lock_request(env, io, &link->cill_lock);
291 list_move(&link->cill_linkage, &set->cls_done);
297 * Takes locks necessary for the current iteration of io.
299 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
300 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
303 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
305 const struct cl_io_slice *scan;
308 LINVRNT(cl_io_is_loopable(io));
309 LINVRNT(io->ci_state == CIS_IT_STARTED);
310 LINVRNT(cl_io_invariant(io));
313 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
314 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
316 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
322 * Sort locks in lexicographical order of their (fid,
323 * start-offset) pairs to avoid deadlocks.
325 list_sort(NULL, &io->ci_lockset.cls_todo, cl_lock_descr_cmp);
326 result = cl_lockset_lock(env, io, &io->ci_lockset);
329 cl_io_unlock(env, io);
331 io->ci_state = CIS_LOCKED;
334 EXPORT_SYMBOL(cl_io_lock);
337 * Release locks takes by io.
339 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
341 struct cl_lockset *set;
342 struct cl_io_lock_link *link;
343 struct cl_io_lock_link *temp;
344 const struct cl_io_slice *scan;
346 LASSERT(cl_io_is_loopable(io));
347 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
348 LINVRNT(cl_io_invariant(io));
351 set = &io->ci_lockset;
353 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
354 list_del_init(&link->cill_linkage);
355 if (link->cill_fini != NULL)
356 link->cill_fini(env, link);
359 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
360 list_del_init(&link->cill_linkage);
361 cl_lock_release(env, &link->cill_lock);
362 if (link->cill_fini != NULL)
363 link->cill_fini(env, link);
366 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
367 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
368 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
370 io->ci_state = CIS_UNLOCKED;
373 EXPORT_SYMBOL(cl_io_unlock);
376 * Prepares next iteration of io.
378 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
379 * layers a chance to modify io parameters, e.g., so that lov can restrict io
380 * to a single stripe.
382 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
384 const struct cl_io_slice *scan;
387 LINVRNT(cl_io_is_loopable(io));
388 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
389 LINVRNT(cl_io_invariant(io));
393 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
394 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
396 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
402 io->ci_state = CIS_IT_STARTED;
405 EXPORT_SYMBOL(cl_io_iter_init);
408 * Finalizes io iteration.
410 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
412 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
414 const struct cl_io_slice *scan;
416 LINVRNT(cl_io_is_loopable(io));
417 LINVRNT(io->ci_state <= CIS_IT_STARTED ||
418 io->ci_state > CIS_IO_FINISHED);
419 LINVRNT(cl_io_invariant(io));
422 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
423 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
424 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
426 io->ci_state = CIS_IT_ENDED;
429 EXPORT_SYMBOL(cl_io_iter_fini);
432 * Records that read or write io progressed \a nob bytes forward.
434 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
436 const struct cl_io_slice *scan;
440 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
442 LINVRNT(cl_io_is_loopable(io));
443 LINVRNT(cl_io_invariant(io));
445 io->u.ci_rw.crw_pos += nob;
446 io->u.ci_rw.crw_count -= nob;
448 /* layers have to be notified. */
449 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
450 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
451 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
458 * Adds a lock to a lockset.
460 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
461 struct cl_io_lock_link *link)
466 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
469 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
474 EXPORT_SYMBOL(cl_io_lock_add);
476 static void cl_free_io_lock_link(const struct lu_env *env,
477 struct cl_io_lock_link *link)
483 * Allocates new lock link, and uses it to add a lock to a lockset.
485 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
486 struct cl_lock_descr *descr)
488 struct cl_io_lock_link *link;
494 link->cill_descr = *descr;
495 link->cill_fini = cl_free_io_lock_link;
496 result = cl_io_lock_add(env, io, link);
497 if (result) /* lock match */
498 link->cill_fini(env, link);
504 EXPORT_SYMBOL(cl_io_lock_alloc_add);
507 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
509 int cl_io_start(const struct lu_env *env, struct cl_io *io)
511 const struct cl_io_slice *scan;
514 LINVRNT(cl_io_is_loopable(io));
515 LINVRNT(io->ci_state == CIS_LOCKED);
516 LINVRNT(cl_io_invariant(io));
519 io->ci_state = CIS_IO_GOING;
520 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
521 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
523 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
531 EXPORT_SYMBOL(cl_io_start);
534 * Wait until current io iteration is finished by calling
535 * cl_io_operations::cio_end() bottom-to-top.
537 void cl_io_end(const struct lu_env *env, struct cl_io *io)
539 const struct cl_io_slice *scan;
541 LINVRNT(cl_io_is_loopable(io));
542 LINVRNT(io->ci_state == CIS_IO_GOING);
543 LINVRNT(cl_io_invariant(io));
546 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
547 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
548 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
549 /* TODO: error handling. */
551 io->ci_state = CIS_IO_FINISHED;
554 EXPORT_SYMBOL(cl_io_end);
557 * Called by read io, to decide the readahead extent
559 * \see cl_io_operations::cio_read_ahead()
561 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
562 pgoff_t start, struct cl_read_ahead *ra)
564 const struct cl_io_slice *scan;
567 LINVRNT(io->ci_type == CIT_READ ||
568 io->ci_type == CIT_FAULT ||
569 io->ci_type == CIT_WRITE);
570 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
571 LINVRNT(cl_io_invariant(io));
574 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
575 if (scan->cis_iop->cio_read_ahead == NULL)
578 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
582 RETURN(result > 0 ? 0 : result);
584 EXPORT_SYMBOL(cl_io_read_ahead);
587 * Called before io start, to reserve enough LRU slots to avoid
590 * \see cl_io_operations::cio_lru_reserve()
592 int cl_io_lru_reserve(const struct lu_env *env, struct cl_io *io,
593 loff_t pos, size_t bytes)
595 const struct cl_io_slice *scan;
598 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
599 LINVRNT(cl_io_invariant(io));
602 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
603 if (scan->cis_iop->cio_lru_reserve) {
604 result = scan->cis_iop->cio_lru_reserve(env, scan,
613 EXPORT_SYMBOL(cl_io_lru_reserve);
616 * Commit a list of contiguous pages into writeback cache.
618 * \returns 0 if all pages committed, or errcode if error occurred.
619 * \see cl_io_operations::cio_commit_async()
621 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
622 struct cl_page_list *queue, int from, int to,
625 const struct cl_io_slice *scan;
629 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
630 if (scan->cis_iop->cio_commit_async == NULL)
632 result = scan->cis_iop->cio_commit_async(env, scan, queue,
639 EXPORT_SYMBOL(cl_io_commit_async);
641 void cl_io_extent_release(const struct lu_env *env, struct cl_io *io)
643 const struct cl_io_slice *scan;
646 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
647 if (scan->cis_iop->cio_extent_release == NULL)
649 scan->cis_iop->cio_extent_release(env, scan);
653 EXPORT_SYMBOL(cl_io_extent_release);
656 * Submits a list of pages for immediate io.
658 * After the function gets returned, The submitted pages are moved to
659 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
660 * to be submitted, and the pages are errant to submit.
662 * \returns 0 if at least one page was submitted, error code otherwise.
663 * \see cl_io_operations::cio_submit()
665 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
666 enum cl_req_type crt, struct cl_2queue *queue)
668 const struct cl_io_slice *scan;
672 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
673 if (scan->cis_iop->cio_submit == NULL)
675 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
680 * If ->cio_submit() failed, no pages were sent.
682 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
685 EXPORT_SYMBOL(cl_io_submit_rw);
688 * Submit a sync_io and wait for the IO to be finished, or error happens.
689 * If \a timeout is zero, it means to wait for the IO unconditionally.
691 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
692 enum cl_req_type iot, struct cl_2queue *queue,
695 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
700 cl_page_list_for_each(pg, &queue->c2_qin) {
701 LASSERT(pg->cp_sync_io == NULL);
702 pg->cp_sync_io = anchor;
705 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
706 rc = cl_io_submit_rw(env, io, iot, queue);
709 * If some pages weren't sent for any reason (e.g.,
710 * read found up-to-date pages in the cache, or write found
711 * clean pages), count them as completed to avoid infinite
714 cl_page_list_for_each(pg, &queue->c2_qin) {
715 pg->cp_sync_io = NULL;
716 cl_sync_io_note(env, anchor, 1);
719 /* wait for the IO to be finished. */
720 rc = cl_sync_io_wait(env, anchor, timeout);
721 cl_page_list_assume(env, io, &queue->c2_qout);
723 LASSERT(list_empty(&queue->c2_qout.pl_pages));
724 cl_page_list_for_each(pg, &queue->c2_qin)
725 pg->cp_sync_io = NULL;
729 EXPORT_SYMBOL(cl_io_submit_sync);
734 * Pumps io through iterations calling
736 * - cl_io_iter_init()
746 * - cl_io_iter_fini()
748 * repeatedly until there is no more io to do.
750 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
755 LINVRNT(cl_io_is_loopable(io));
762 result = cl_io_iter_init(env, io);
765 result = cl_io_lock(env, io);
768 * Notify layers that locks has been taken,
771 * - llite: kms, short read;
772 * - llite: generic_file_read();
774 result = cl_io_start(env, io);
776 * Send any remaining pending
779 ** - llite: ll_rw_stats_tally.
782 cl_io_unlock(env, io);
783 cl_io_rw_advance(env, io, io->ci_nob - nob);
786 cl_io_iter_fini(env, io);
789 } while ((result == 0 || result == -EIOCBQUEUED) &&
795 if (result == -EAGAIN && io->ci_ndelay) {
796 io->ci_need_restart = 1;
801 result = io->ci_result;
802 RETURN(result < 0 ? result : 0);
804 EXPORT_SYMBOL(cl_io_loop);
807 * Adds io slice to the cl_io.
809 * This is called by cl_object_operations::coo_io_init() methods to add a
810 * per-layer state to the io. New state is added at the end of
811 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
813 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
815 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
816 struct cl_object *obj,
817 const struct cl_io_operations *ops)
819 struct list_head *linkage = &slice->cis_linkage;
821 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
822 list_empty(linkage));
825 list_add_tail(linkage, &io->ci_layers);
827 slice->cis_obj = obj;
828 slice->cis_iop = ops;
831 EXPORT_SYMBOL(cl_io_slice_add);
835 * Initializes page list.
837 void cl_page_list_init(struct cl_page_list *plist)
841 INIT_LIST_HEAD(&plist->pl_pages);
844 EXPORT_SYMBOL(cl_page_list_init);
847 * Adds a page to a page list.
849 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
852 /* it would be better to check that page is owned by "current" io, but
853 * it is not passed here. */
854 LASSERT(page->cp_owner != NULL);
856 LASSERT(list_empty(&page->cp_batch));
857 list_add_tail(&page->cp_batch, &plist->pl_pages);
859 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
863 EXPORT_SYMBOL(cl_page_list_add);
866 * Removes a page from a page list.
868 void cl_page_list_del(const struct lu_env *env,
869 struct cl_page_list *plist, struct cl_page *page)
871 LASSERT(plist->pl_nr > 0);
872 LASSERT(cl_page_is_vmlocked(env, page));
875 list_del_init(&page->cp_batch);
877 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
878 cl_page_put(env, page);
881 EXPORT_SYMBOL(cl_page_list_del);
884 * Moves a page from one page list to another.
886 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
887 struct cl_page *page)
889 LASSERT(src->pl_nr > 0);
892 list_move_tail(&page->cp_batch, &dst->pl_pages);
895 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
899 EXPORT_SYMBOL(cl_page_list_move);
902 * Moves a page from one page list to the head of another list.
904 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
905 struct cl_page *page)
907 LASSERT(src->pl_nr > 0);
910 list_move(&page->cp_batch, &dst->pl_pages);
913 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
917 EXPORT_SYMBOL(cl_page_list_move_head);
920 * splice the cl_page_list, just as list head does
922 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
924 struct cl_page *page;
929 cl_page_list_for_each_safe(page, tmp, list)
930 cl_page_list_move(head, list, page);
933 EXPORT_SYMBOL(cl_page_list_splice);
936 * Disowns pages in a queue.
938 void cl_page_list_disown(const struct lu_env *env,
939 struct cl_io *io, struct cl_page_list *plist)
941 struct cl_page *page;
942 struct cl_page *temp;
946 cl_page_list_for_each_safe(page, temp, plist) {
947 LASSERT(plist->pl_nr > 0);
949 list_del_init(&page->cp_batch);
952 * cl_page_disown0 rather than usual cl_page_disown() is used,
953 * because pages are possibly in CPS_FREEING state already due
954 * to the call to cl_page_list_discard().
957 * XXX cl_page_disown0() will fail if page is not locked.
959 cl_page_disown0(env, io, page);
960 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
962 cl_page_put(env, page);
966 EXPORT_SYMBOL(cl_page_list_disown);
969 * Releases pages from queue.
971 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
973 struct cl_page *page;
974 struct cl_page *temp;
978 cl_page_list_for_each_safe(page, temp, plist)
979 cl_page_list_del(env, plist, page);
980 LASSERT(plist->pl_nr == 0);
983 EXPORT_SYMBOL(cl_page_list_fini);
986 * Assumes all pages in a queue.
988 void cl_page_list_assume(const struct lu_env *env,
989 struct cl_io *io, struct cl_page_list *plist)
991 struct cl_page *page;
994 cl_page_list_for_each(page, plist)
995 cl_page_assume(env, io, page);
999 * Discards all pages in a queue.
1001 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1002 struct cl_page_list *plist)
1004 struct cl_page *page;
1007 cl_page_list_for_each(page, plist)
1008 cl_page_discard(env, io, page);
1011 EXPORT_SYMBOL(cl_page_list_discard);
1014 * Initialize dual page queue.
1016 void cl_2queue_init(struct cl_2queue *queue)
1019 cl_page_list_init(&queue->c2_qin);
1020 cl_page_list_init(&queue->c2_qout);
1023 EXPORT_SYMBOL(cl_2queue_init);
1026 * Add a page to the incoming page list of 2-queue.
1028 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1031 cl_page_list_add(&queue->c2_qin, page);
1034 EXPORT_SYMBOL(cl_2queue_add);
1037 * Disown pages in both lists of a 2-queue.
1039 void cl_2queue_disown(const struct lu_env *env,
1040 struct cl_io *io, struct cl_2queue *queue)
1043 cl_page_list_disown(env, io, &queue->c2_qin);
1044 cl_page_list_disown(env, io, &queue->c2_qout);
1047 EXPORT_SYMBOL(cl_2queue_disown);
1050 * Discard (truncate) pages in both lists of a 2-queue.
1052 void cl_2queue_discard(const struct lu_env *env,
1053 struct cl_io *io, struct cl_2queue *queue)
1056 cl_page_list_discard(env, io, &queue->c2_qin);
1057 cl_page_list_discard(env, io, &queue->c2_qout);
1060 EXPORT_SYMBOL(cl_2queue_discard);
1063 * Assume to own the pages in cl_2queue
1065 void cl_2queue_assume(const struct lu_env *env,
1066 struct cl_io *io, struct cl_2queue *queue)
1068 cl_page_list_assume(env, io, &queue->c2_qin);
1069 cl_page_list_assume(env, io, &queue->c2_qout);
1073 * Finalize both page lists of a 2-queue.
1075 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1078 cl_page_list_fini(env, &queue->c2_qout);
1079 cl_page_list_fini(env, &queue->c2_qin);
1082 EXPORT_SYMBOL(cl_2queue_fini);
1085 * Initialize a 2-queue to contain \a page in its incoming page list.
1087 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1090 cl_2queue_init(queue);
1091 cl_2queue_add(queue, page);
1094 EXPORT_SYMBOL(cl_2queue_init_page);
1097 * Returns top-level io.
1099 * \see cl_object_top()
1101 struct cl_io *cl_io_top(struct cl_io *io)
1104 while (io->ci_parent != NULL)
1108 EXPORT_SYMBOL(cl_io_top);
1111 * Prints human readable representation of \a io to the \a f.
1113 void cl_io_print(const struct lu_env *env, void *cookie,
1114 lu_printer_t printer, const struct cl_io *io)
1119 * Fills in attributes that are passed to server together with transfer. Only
1120 * attributes from \a flags may be touched. This can be called multiple times
1121 * for the same request.
1123 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1124 struct cl_req_attr *attr)
1126 struct cl_object *scan;
1129 cl_object_for_each(scan, obj) {
1130 if (scan->co_ops->coo_req_attr_set != NULL)
1131 scan->co_ops->coo_req_attr_set(env, scan, attr);
1135 EXPORT_SYMBOL(cl_req_attr_set);
1138 * Initialize synchronous io wait \a anchor for \a nr pages with optional
1140 * \param anchor owned by caller, initialzied here.
1141 * \param nr number of pages initally pending in sync.
1142 * \param end optional callback sync_io completion, can be used to
1143 * trigger erasure coding, integrity, dedupe, or similar operation.
1144 * \q end is called with a spinlock on anchor->csi_waitq.lock
1147 void cl_sync_io_init_notify(struct cl_sync_io *anchor, int nr,
1148 struct cl_dio_aio *aio, cl_sync_io_end_t *end)
1151 memset(anchor, 0, sizeof(*anchor));
1152 init_waitqueue_head(&anchor->csi_waitq);
1153 atomic_set(&anchor->csi_sync_nr, nr);
1154 anchor->csi_sync_rc = 0;
1155 anchor->csi_end_io = end;
1156 anchor->csi_aio = aio;
1159 EXPORT_SYMBOL(cl_sync_io_init_notify);
1162 * Wait until all IO completes. Transfer completion routine has to call
1163 * cl_sync_io_note() for every entity.
1165 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1171 LASSERT(timeout >= 0);
1174 wait_event_idle_timeout(anchor->csi_waitq,
1175 atomic_read(&anchor->csi_sync_nr) == 0,
1176 cfs_time_seconds(timeout)) == 0) {
1178 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1179 rc, atomic_read(&anchor->csi_sync_nr));
1182 wait_event_idle(anchor->csi_waitq,
1183 atomic_read(&anchor->csi_sync_nr) == 0);
1185 rc = anchor->csi_sync_rc;
1187 /* We take the lock to ensure that cl_sync_io_note() has finished */
1188 spin_lock(&anchor->csi_waitq.lock);
1189 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1190 spin_unlock(&anchor->csi_waitq.lock);
1194 EXPORT_SYMBOL(cl_sync_io_wait);
1196 #ifndef HAVE_AIO_COMPLETE
1197 static inline void aio_complete(struct kiocb *iocb, ssize_t res, ssize_t res2)
1199 if (iocb->ki_complete)
1200 iocb->ki_complete(iocb, res, res2);
1204 static void cl_aio_end(const struct lu_env *env, struct cl_sync_io *anchor)
1206 struct cl_dio_aio *aio = container_of(anchor, typeof(*aio), cda_sync);
1207 ssize_t ret = anchor->csi_sync_rc;
1212 while (aio->cda_pages.pl_nr > 0) {
1213 struct cl_page *page = cl_page_list_first(&aio->cda_pages);
1216 cl_page_list_del(env, &aio->cda_pages, page);
1217 cl_page_delete(env, page);
1218 cl_page_put(env, page);
1221 if (!is_sync_kiocb(aio->cda_iocb) && !aio->cda_no_aio_complete)
1222 aio_complete(aio->cda_iocb, ret ?: aio->cda_bytes, 0);
1227 struct cl_dio_aio *cl_aio_alloc(struct kiocb *iocb)
1229 struct cl_dio_aio *aio;
1231 OBD_SLAB_ALLOC_PTR_GFP(aio, cl_dio_aio_kmem, GFP_NOFS);
1234 * Hold one ref so that it won't be released until
1235 * every pages is added.
1237 cl_sync_io_init_notify(&aio->cda_sync, 1, is_sync_kiocb(iocb) ?
1238 NULL : aio, cl_aio_end);
1239 cl_page_list_init(&aio->cda_pages);
1240 aio->cda_iocb = iocb;
1241 aio->cda_no_aio_complete = 0;
1245 EXPORT_SYMBOL(cl_aio_alloc);
1247 void cl_aio_free(struct cl_dio_aio *aio)
1250 OBD_SLAB_FREE_PTR(aio, cl_dio_aio_kmem);
1252 EXPORT_SYMBOL(cl_aio_free);
1256 * Indicate that transfer of a single page completed.
1258 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1262 if (anchor->csi_sync_rc == 0 && ioret < 0)
1263 anchor->csi_sync_rc = ioret;
1265 * Synchronous IO done without releasing page lock (e.g., as a part of
1266 * ->{prepare,commit}_write(). Completion is used to signal the end of
1269 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1270 if (atomic_dec_and_lock(&anchor->csi_sync_nr,
1271 &anchor->csi_waitq.lock)) {
1272 struct cl_dio_aio *aio = NULL;
1274 cl_sync_io_end_t *end_io = anchor->csi_end_io;
1277 * Holding the lock across both the decrement and
1278 * the wakeup ensures cl_sync_io_wait() doesn't complete
1279 * before the wakeup completes and the contents of
1280 * of anchor become unsafe to access as the owner is free
1281 * to immediately reclaim anchor when cl_sync_io_wait()
1284 wake_up_locked(&anchor->csi_waitq);
1286 end_io(env, anchor);
1287 if (anchor->csi_aio)
1288 aio = anchor->csi_aio;
1290 spin_unlock(&anchor->csi_waitq.lock);
1293 * If anchor->csi_aio is set, we are responsible for freeing
1294 * memory here rather than when cl_sync_io_wait() completes.
1300 EXPORT_SYMBOL(cl_sync_io_note);