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/
33 * Author: Nikita Danilov <nikita.danilov@sun.com>
34 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
37 #define DEBUG_SUBSYSTEM S_CLASS
39 #include <linux/sched.h>
40 #include <linux/list.h>
41 #include <linux/list_sort.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lustre_fid.h>
45 #include <cl_object.h>
46 #include "cl_internal.h"
47 #include <libcfs/crypto/llcrypt.h>
49 /*****************************************************************************
55 static inline int cl_io_type_is_valid(enum cl_io_type type)
57 return CIT_READ <= type && type < CIT_OP_NR;
60 static inline int cl_io_is_loopable(const struct cl_io *io)
62 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
66 * cl_io invariant that holds at all times when exported cl_io_*() functions
67 * are entered and left.
69 static int cl_io_invariant(const struct cl_io *io)
76 * io can own pages only when it is ongoing. Sub-io might
77 * still be in CIS_LOCKED state when top-io is in
80 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
81 (io->ci_state == CIS_LOCKED && up != NULL));
85 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
87 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
89 struct cl_io_slice *slice;
91 LINVRNT(cl_io_type_is_valid(io->ci_type));
92 LINVRNT(cl_io_invariant(io));
95 while (!list_empty(&io->ci_layers)) {
96 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
98 list_del_init(&slice->cis_linkage);
99 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
100 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
102 * Invalidate slice to catch use after free. This assumes that
103 * slices are allocated within session and can be touched
104 * after ->cio_fini() returns.
106 slice->cis_io = NULL;
108 io->ci_state = CIS_FINI;
110 /* sanity check for layout change */
111 switch(io->ci_type) {
114 case CIT_DATA_VERSION:
118 LASSERT(!io->ci_need_restart);
122 /* Check ignore layout change conf */
123 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
124 !io->ci_need_restart));
135 EXPORT_SYMBOL(cl_io_fini);
137 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
138 enum cl_io_type iot, struct cl_object *obj)
140 struct cl_object *scan;
143 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
144 LINVRNT(cl_io_type_is_valid(iot));
145 LINVRNT(cl_io_invariant(io));
149 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
150 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
151 INIT_LIST_HEAD(&io->ci_layers);
154 cl_object_for_each(scan, obj) {
155 if (scan->co_ops->coo_io_init != NULL) {
156 result = scan->co_ops->coo_io_init(env, scan, io);
162 io->ci_state = CIS_INIT;
167 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
169 * \pre obj != cl_object_top(obj)
171 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
172 enum cl_io_type iot, struct cl_object *obj)
174 LASSERT(obj != cl_object_top(obj));
176 return cl_io_init0(env, io, iot, obj);
178 EXPORT_SYMBOL(cl_io_sub_init);
181 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
183 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
184 * what the latter returned.
186 * \pre obj == cl_object_top(obj)
187 * \pre cl_io_type_is_valid(iot)
188 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
190 int cl_io_init(const struct lu_env *env, struct cl_io *io,
191 enum cl_io_type iot, struct cl_object *obj)
193 LASSERT(obj == cl_object_top(obj));
195 /* clear I/O restart from previous instance */
196 io->ci_need_restart = 0;
198 return cl_io_init0(env, io, iot, obj);
200 EXPORT_SYMBOL(cl_io_init);
203 * Initialize read or write io.
205 * \pre iot == CIT_READ || iot == CIT_WRITE
207 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
208 enum cl_io_type iot, loff_t pos, size_t count)
210 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
211 LINVRNT(io->ci_obj != NULL);
214 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
215 "io range: %u [%llu, %llu) %u %u\n",
216 iot, (__u64)pos, (__u64)pos + count,
217 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
218 io->u.ci_rw.crw_pos = pos;
219 io->u.ci_rw.crw_count = count;
220 RETURN(cl_io_init(env, io, iot, io->ci_obj));
222 EXPORT_SYMBOL(cl_io_rw_init);
224 static int cl_lock_descr_cmp(void *priv,
225 struct list_head *a, struct list_head *b)
227 const struct cl_io_lock_link *l0 = list_entry(a, struct cl_io_lock_link,
229 const struct cl_io_lock_link *l1 = list_entry(b, struct cl_io_lock_link,
231 const struct cl_lock_descr *d0 = &l0->cill_descr;
232 const struct cl_lock_descr *d1 = &l1->cill_descr;
234 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
235 lu_object_fid(&d1->cld_obj->co_lu));
238 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
239 const struct cl_lock_descr *d1)
241 d0->cld_start = min(d0->cld_start, d1->cld_start);
242 d0->cld_end = max(d0->cld_end, d1->cld_end);
244 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
245 d0->cld_mode = CLM_WRITE;
247 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
248 d0->cld_mode = CLM_GROUP;
251 static int cl_lockset_merge(const struct cl_lockset *set,
252 const struct cl_lock_descr *need)
254 struct cl_io_lock_link *scan;
257 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
258 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
261 /* Merge locks for the same object because ldlm lock server
262 * may expand the lock extent, otherwise there is a deadlock
263 * case if two conflicted locks are queueud for the same object
264 * and lock server expands one lock to overlap the another.
265 * The side effect is that it can generate a multi-stripe lock
266 * that may cause casacading problem */
267 cl_lock_descr_merge(&scan->cill_descr, need);
268 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
269 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
270 scan->cill_descr.cld_end);
276 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
277 struct cl_lockset *set)
279 struct cl_io_lock_link *link;
280 struct cl_io_lock_link *temp;
285 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
286 result = cl_lock_request(env, io, &link->cill_lock);
290 list_move(&link->cill_linkage, &set->cls_done);
296 * Takes locks necessary for the current iteration of io.
298 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
299 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
302 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
304 const struct cl_io_slice *scan;
307 LINVRNT(cl_io_is_loopable(io));
308 LINVRNT(io->ci_state == CIS_IT_STARTED);
309 LINVRNT(cl_io_invariant(io));
312 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
313 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
315 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
321 * Sort locks in lexicographical order of their (fid,
322 * start-offset) pairs to avoid deadlocks.
324 list_sort(NULL, &io->ci_lockset.cls_todo, cl_lock_descr_cmp);
325 result = cl_lockset_lock(env, io, &io->ci_lockset);
328 cl_io_unlock(env, io);
330 io->ci_state = CIS_LOCKED;
333 EXPORT_SYMBOL(cl_io_lock);
336 * Release locks takes by io.
338 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
340 struct cl_lockset *set;
341 struct cl_io_lock_link *link;
342 struct cl_io_lock_link *temp;
343 const struct cl_io_slice *scan;
345 LASSERT(cl_io_is_loopable(io));
346 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
347 LINVRNT(cl_io_invariant(io));
350 set = &io->ci_lockset;
352 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
353 list_del_init(&link->cill_linkage);
354 if (link->cill_fini != NULL)
355 link->cill_fini(env, link);
358 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
359 list_del_init(&link->cill_linkage);
360 cl_lock_release(env, &link->cill_lock);
361 if (link->cill_fini != NULL)
362 link->cill_fini(env, link);
365 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
366 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
367 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
369 io->ci_state = CIS_UNLOCKED;
372 EXPORT_SYMBOL(cl_io_unlock);
375 * Prepares next iteration of io.
377 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
378 * layers a chance to modify io parameters, e.g., so that lov can restrict io
379 * to a single stripe.
381 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
383 const struct cl_io_slice *scan;
386 LINVRNT(cl_io_is_loopable(io));
387 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
388 LINVRNT(cl_io_invariant(io));
392 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
393 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
395 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
401 io->ci_state = CIS_IT_STARTED;
404 EXPORT_SYMBOL(cl_io_iter_init);
407 * Finalizes io iteration.
409 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
411 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
413 const struct cl_io_slice *scan;
415 LINVRNT(cl_io_is_loopable(io));
416 LINVRNT(io->ci_state <= CIS_IT_STARTED ||
417 io->ci_state > CIS_IO_FINISHED);
418 LINVRNT(cl_io_invariant(io));
421 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
422 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
423 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
425 io->ci_state = CIS_IT_ENDED;
428 EXPORT_SYMBOL(cl_io_iter_fini);
431 * Records that read or write io progressed \a nob bytes forward.
433 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
435 const struct cl_io_slice *scan;
439 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
441 LINVRNT(cl_io_is_loopable(io));
442 LINVRNT(cl_io_invariant(io));
444 io->u.ci_rw.crw_pos += nob;
445 io->u.ci_rw.crw_count -= nob;
447 /* layers have to be notified. */
448 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
449 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
450 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
457 * Adds a lock to a lockset.
459 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
460 struct cl_io_lock_link *link)
465 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
468 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
473 EXPORT_SYMBOL(cl_io_lock_add);
475 static void cl_free_io_lock_link(const struct lu_env *env,
476 struct cl_io_lock_link *link)
482 * Allocates new lock link, and uses it to add a lock to a lockset.
484 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
485 struct cl_lock_descr *descr)
487 struct cl_io_lock_link *link;
493 link->cill_descr = *descr;
494 link->cill_fini = cl_free_io_lock_link;
495 result = cl_io_lock_add(env, io, link);
496 if (result) /* lock match */
497 link->cill_fini(env, link);
503 EXPORT_SYMBOL(cl_io_lock_alloc_add);
506 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
508 int cl_io_start(const struct lu_env *env, struct cl_io *io)
510 const struct cl_io_slice *scan;
513 LINVRNT(cl_io_is_loopable(io));
514 LINVRNT(io->ci_state == CIS_LOCKED);
515 LINVRNT(cl_io_invariant(io));
518 io->ci_state = CIS_IO_GOING;
519 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
520 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
522 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
530 EXPORT_SYMBOL(cl_io_start);
533 * Wait until current io iteration is finished by calling
534 * cl_io_operations::cio_end() bottom-to-top.
536 void cl_io_end(const struct lu_env *env, struct cl_io *io)
538 const struct cl_io_slice *scan;
540 LINVRNT(cl_io_is_loopable(io));
541 LINVRNT(io->ci_state == CIS_IO_GOING);
542 LINVRNT(cl_io_invariant(io));
545 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
546 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
547 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
548 /* TODO: error handling. */
550 io->ci_state = CIS_IO_FINISHED;
553 EXPORT_SYMBOL(cl_io_end);
556 * Called by read io, to decide the readahead extent
558 * \see cl_io_operations::cio_read_ahead()
560 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
561 pgoff_t start, struct cl_read_ahead *ra)
563 const struct cl_io_slice *scan;
566 LINVRNT(io->ci_type == CIT_READ ||
567 io->ci_type == CIT_FAULT ||
568 io->ci_type == CIT_WRITE);
569 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
570 LINVRNT(cl_io_invariant(io));
573 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
574 if (scan->cis_iop->cio_read_ahead == NULL)
577 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
581 RETURN(result > 0 ? 0 : result);
583 EXPORT_SYMBOL(cl_io_read_ahead);
586 * Called before io start, to reserve enough LRU slots to avoid
589 * \see cl_io_operations::cio_lru_reserve()
591 int cl_io_lru_reserve(const struct lu_env *env, struct cl_io *io,
592 loff_t pos, size_t bytes)
594 const struct cl_io_slice *scan;
597 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
598 LINVRNT(cl_io_invariant(io));
601 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
602 if (scan->cis_iop->cio_lru_reserve) {
603 result = scan->cis_iop->cio_lru_reserve(env, scan,
612 EXPORT_SYMBOL(cl_io_lru_reserve);
615 * Commit a list of contiguous pages into writeback cache.
617 * \returns 0 if all pages committed, or errcode if error occurred.
618 * \see cl_io_operations::cio_commit_async()
620 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
621 struct cl_page_list *queue, int from, int to,
624 const struct cl_io_slice *scan;
628 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
629 if (scan->cis_iop->cio_commit_async == NULL)
631 result = scan->cis_iop->cio_commit_async(env, scan, queue,
638 EXPORT_SYMBOL(cl_io_commit_async);
640 void cl_io_extent_release(const struct lu_env *env, struct cl_io *io)
642 const struct cl_io_slice *scan;
645 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
646 if (scan->cis_iop->cio_extent_release == NULL)
648 scan->cis_iop->cio_extent_release(env, scan);
652 EXPORT_SYMBOL(cl_io_extent_release);
655 * Submits a list of pages for immediate io.
657 * After the function gets returned, The submitted pages are moved to
658 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
659 * to be submitted, and the pages are errant to submit.
661 * \returns 0 if at least one page was submitted, error code otherwise.
662 * \see cl_io_operations::cio_submit()
664 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
665 enum cl_req_type crt, struct cl_2queue *queue)
667 const struct cl_io_slice *scan;
671 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
672 if (scan->cis_iop->cio_submit == NULL)
674 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
679 * If ->cio_submit() failed, no pages were sent.
681 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
684 EXPORT_SYMBOL(cl_io_submit_rw);
687 * Submit a sync_io and wait for the IO to be finished, or error happens.
688 * If \a timeout is zero, it means to wait for the IO unconditionally.
690 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
691 enum cl_req_type iot, struct cl_2queue *queue,
694 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
699 cl_page_list_for_each(pg, &queue->c2_qin) {
700 LASSERT(pg->cp_sync_io == NULL);
701 pg->cp_sync_io = anchor;
704 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
705 rc = cl_io_submit_rw(env, io, iot, queue);
708 * If some pages weren't sent for any reason (e.g.,
709 * read found up-to-date pages in the cache, or write found
710 * clean pages), count them as completed to avoid infinite
713 cl_page_list_for_each(pg, &queue->c2_qin) {
714 pg->cp_sync_io = NULL;
715 cl_sync_io_note(env, anchor, 1);
718 /* wait for the IO to be finished. */
719 rc = cl_sync_io_wait(env, anchor, timeout);
720 cl_page_list_assume(env, io, &queue->c2_qout);
722 LASSERT(list_empty(&queue->c2_qout.pl_pages));
723 cl_page_list_for_each(pg, &queue->c2_qin)
724 pg->cp_sync_io = NULL;
728 EXPORT_SYMBOL(cl_io_submit_sync);
733 * Pumps io through iterations calling
735 * - cl_io_iter_init()
745 * - cl_io_iter_fini()
747 * repeatedly until there is no more io to do.
749 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
754 LINVRNT(cl_io_is_loopable(io));
761 result = cl_io_iter_init(env, io);
764 result = cl_io_lock(env, io);
767 * Notify layers that locks has been taken,
770 * - llite: kms, short read;
771 * - llite: generic_file_read();
773 result = cl_io_start(env, io);
775 * Send any remaining pending
778 ** - llite: ll_rw_stats_tally.
781 cl_io_unlock(env, io);
782 cl_io_rw_advance(env, io, io->ci_nob - nob);
785 cl_io_iter_fini(env, io);
788 } while ((result == 0 || result == -EIOCBQUEUED) &&
794 if (result == -EAGAIN && io->ci_ndelay) {
795 io->ci_need_restart = 1;
800 result = io->ci_result;
801 RETURN(result < 0 ? result : 0);
803 EXPORT_SYMBOL(cl_io_loop);
806 * Adds io slice to the cl_io.
808 * This is called by cl_object_operations::coo_io_init() methods to add a
809 * per-layer state to the io. New state is added at the end of
810 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
812 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
814 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
815 struct cl_object *obj,
816 const struct cl_io_operations *ops)
818 struct list_head *linkage = &slice->cis_linkage;
820 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
821 list_empty(linkage));
824 list_add_tail(linkage, &io->ci_layers);
826 slice->cis_obj = obj;
827 slice->cis_iop = ops;
830 EXPORT_SYMBOL(cl_io_slice_add);
834 * Initializes page list.
836 void cl_page_list_init(struct cl_page_list *plist)
840 INIT_LIST_HEAD(&plist->pl_pages);
843 EXPORT_SYMBOL(cl_page_list_init);
846 * Adds a page to a page list.
848 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
851 /* it would be better to check that page is owned by "current" io, but
852 * it is not passed here. */
853 LASSERT(page->cp_owner != NULL);
855 LASSERT(list_empty(&page->cp_batch));
856 list_add_tail(&page->cp_batch, &plist->pl_pages);
858 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
862 EXPORT_SYMBOL(cl_page_list_add);
865 * Removes a page from a page list.
867 void cl_page_list_del(const struct lu_env *env,
868 struct cl_page_list *plist, struct cl_page *page)
870 LASSERT(plist->pl_nr > 0);
871 LASSERT(cl_page_is_vmlocked(env, page));
874 list_del_init(&page->cp_batch);
876 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
877 cl_page_put(env, page);
880 EXPORT_SYMBOL(cl_page_list_del);
883 * Moves a page from one page list to another.
885 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
886 struct cl_page *page)
888 LASSERT(src->pl_nr > 0);
891 list_move_tail(&page->cp_batch, &dst->pl_pages);
894 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
898 EXPORT_SYMBOL(cl_page_list_move);
901 * Moves a page from one page list to the head of another list.
903 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
904 struct cl_page *page)
906 LASSERT(src->pl_nr > 0);
909 list_move(&page->cp_batch, &dst->pl_pages);
912 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
916 EXPORT_SYMBOL(cl_page_list_move_head);
919 * splice the cl_page_list, just as list head does
921 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
923 struct cl_page *page;
928 cl_page_list_for_each_safe(page, tmp, list)
929 cl_page_list_move(head, list, page);
932 EXPORT_SYMBOL(cl_page_list_splice);
935 * Disowns pages in a queue.
937 void cl_page_list_disown(const struct lu_env *env,
938 struct cl_io *io, struct cl_page_list *plist)
940 struct cl_page *page;
941 struct cl_page *temp;
945 cl_page_list_for_each_safe(page, temp, plist) {
946 LASSERT(plist->pl_nr > 0);
948 list_del_init(&page->cp_batch);
951 * cl_page_disown0 rather than usual cl_page_disown() is used,
952 * because pages are possibly in CPS_FREEING state already due
953 * to the call to cl_page_list_discard().
956 * XXX cl_page_disown0() will fail if page is not locked.
958 cl_page_disown0(env, io, page);
959 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
961 cl_page_put(env, page);
965 EXPORT_SYMBOL(cl_page_list_disown);
968 * Releases pages from queue.
970 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
972 struct cl_page *page;
973 struct cl_page *temp;
977 cl_page_list_for_each_safe(page, temp, plist)
978 cl_page_list_del(env, plist, page);
979 LASSERT(plist->pl_nr == 0);
982 EXPORT_SYMBOL(cl_page_list_fini);
985 * Assumes all pages in a queue.
987 void cl_page_list_assume(const struct lu_env *env,
988 struct cl_io *io, struct cl_page_list *plist)
990 struct cl_page *page;
993 cl_page_list_for_each(page, plist)
994 cl_page_assume(env, io, page);
998 * Discards all pages in a queue.
1000 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1001 struct cl_page_list *plist)
1003 struct cl_page *page;
1006 cl_page_list_for_each(page, plist)
1007 cl_page_discard(env, io, page);
1010 EXPORT_SYMBOL(cl_page_list_discard);
1013 * Initialize dual page queue.
1015 void cl_2queue_init(struct cl_2queue *queue)
1018 cl_page_list_init(&queue->c2_qin);
1019 cl_page_list_init(&queue->c2_qout);
1022 EXPORT_SYMBOL(cl_2queue_init);
1025 * Add a page to the incoming page list of 2-queue.
1027 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1030 cl_page_list_add(&queue->c2_qin, page);
1033 EXPORT_SYMBOL(cl_2queue_add);
1036 * Disown pages in both lists of a 2-queue.
1038 void cl_2queue_disown(const struct lu_env *env,
1039 struct cl_io *io, struct cl_2queue *queue)
1042 cl_page_list_disown(env, io, &queue->c2_qin);
1043 cl_page_list_disown(env, io, &queue->c2_qout);
1046 EXPORT_SYMBOL(cl_2queue_disown);
1049 * Discard (truncate) pages in both lists of a 2-queue.
1051 void cl_2queue_discard(const struct lu_env *env,
1052 struct cl_io *io, struct cl_2queue *queue)
1055 cl_page_list_discard(env, io, &queue->c2_qin);
1056 cl_page_list_discard(env, io, &queue->c2_qout);
1059 EXPORT_SYMBOL(cl_2queue_discard);
1062 * Assume to own the pages in cl_2queue
1064 void cl_2queue_assume(const struct lu_env *env,
1065 struct cl_io *io, struct cl_2queue *queue)
1067 cl_page_list_assume(env, io, &queue->c2_qin);
1068 cl_page_list_assume(env, io, &queue->c2_qout);
1072 * Finalize both page lists of a 2-queue.
1074 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1077 cl_page_list_fini(env, &queue->c2_qout);
1078 cl_page_list_fini(env, &queue->c2_qin);
1081 EXPORT_SYMBOL(cl_2queue_fini);
1084 * Initialize a 2-queue to contain \a page in its incoming page list.
1086 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1089 cl_2queue_init(queue);
1090 cl_2queue_add(queue, page);
1093 EXPORT_SYMBOL(cl_2queue_init_page);
1096 * Returns top-level io.
1098 * \see cl_object_top()
1100 struct cl_io *cl_io_top(struct cl_io *io)
1103 while (io->ci_parent != NULL)
1107 EXPORT_SYMBOL(cl_io_top);
1110 * Prints human readable representation of \a io to the \a f.
1112 void cl_io_print(const struct lu_env *env, void *cookie,
1113 lu_printer_t printer, const struct cl_io *io)
1118 * Fills in attributes that are passed to server together with transfer. Only
1119 * attributes from \a flags may be touched. This can be called multiple times
1120 * for the same request.
1122 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1123 struct cl_req_attr *attr)
1125 struct cl_object *scan;
1128 cl_object_for_each(scan, obj) {
1129 if (scan->co_ops->coo_req_attr_set != NULL)
1130 scan->co_ops->coo_req_attr_set(env, scan, attr);
1134 EXPORT_SYMBOL(cl_req_attr_set);
1137 * Initialize synchronous io wait \a anchor for \a nr pages with optional
1139 * \param anchor owned by caller, initialzied here.
1140 * \param nr number of pages initally pending in sync.
1141 * \param end optional callback sync_io completion, can be used to
1142 * trigger erasure coding, integrity, dedupe, or similar operation.
1143 * \q end is called with a spinlock on anchor->csi_waitq.lock
1146 void cl_sync_io_init_notify(struct cl_sync_io *anchor, int nr,
1147 struct cl_dio_aio *aio, cl_sync_io_end_t *end)
1150 memset(anchor, 0, sizeof(*anchor));
1151 init_waitqueue_head(&anchor->csi_waitq);
1152 atomic_set(&anchor->csi_sync_nr, nr);
1153 anchor->csi_sync_rc = 0;
1154 anchor->csi_end_io = end;
1155 anchor->csi_aio = aio;
1158 EXPORT_SYMBOL(cl_sync_io_init_notify);
1161 * Wait until all IO completes. Transfer completion routine has to call
1162 * cl_sync_io_note() for every entity.
1164 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1170 LASSERT(timeout >= 0);
1173 wait_event_idle_timeout(anchor->csi_waitq,
1174 atomic_read(&anchor->csi_sync_nr) == 0,
1175 cfs_time_seconds(timeout)) == 0) {
1177 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1178 rc, atomic_read(&anchor->csi_sync_nr));
1181 wait_event_idle(anchor->csi_waitq,
1182 atomic_read(&anchor->csi_sync_nr) == 0);
1184 rc = anchor->csi_sync_rc;
1186 /* We take the lock to ensure that cl_sync_io_note() has finished */
1187 spin_lock(&anchor->csi_waitq.lock);
1188 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1189 spin_unlock(&anchor->csi_waitq.lock);
1193 EXPORT_SYMBOL(cl_sync_io_wait);
1195 #ifndef HAVE_AIO_COMPLETE
1196 static inline void aio_complete(struct kiocb *iocb, ssize_t res, ssize_t res2)
1198 if (iocb->ki_complete)
1199 iocb->ki_complete(iocb, res, res2);
1203 static void cl_aio_end(const struct lu_env *env, struct cl_sync_io *anchor)
1205 struct cl_dio_aio *aio = container_of(anchor, typeof(*aio), cda_sync);
1206 ssize_t ret = anchor->csi_sync_rc;
1211 while (aio->cda_pages.pl_nr > 0) {
1212 struct cl_page *page = cl_page_list_first(&aio->cda_pages);
1215 cl_page_list_del(env, &aio->cda_pages, page);
1216 cl_page_delete(env, page);
1217 cl_page_put(env, page);
1220 if (!is_sync_kiocb(aio->cda_iocb) && !aio->cda_no_aio_complete)
1221 aio_complete(aio->cda_iocb, ret ?: aio->cda_bytes, 0);
1226 struct cl_dio_aio *cl_aio_alloc(struct kiocb *iocb)
1228 struct cl_dio_aio *aio;
1230 OBD_SLAB_ALLOC_PTR_GFP(aio, cl_dio_aio_kmem, GFP_NOFS);
1233 * Hold one ref so that it won't be released until
1234 * every pages is added.
1236 cl_sync_io_init_notify(&aio->cda_sync, 1, is_sync_kiocb(iocb) ?
1237 NULL : aio, cl_aio_end);
1238 cl_page_list_init(&aio->cda_pages);
1239 aio->cda_iocb = iocb;
1240 aio->cda_no_aio_complete = 0;
1244 EXPORT_SYMBOL(cl_aio_alloc);
1246 void cl_aio_free(struct cl_dio_aio *aio)
1249 OBD_SLAB_FREE_PTR(aio, cl_dio_aio_kmem);
1251 EXPORT_SYMBOL(cl_aio_free);
1255 * Indicate that transfer of a single page completed.
1257 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1261 if (anchor->csi_sync_rc == 0 && ioret < 0)
1262 anchor->csi_sync_rc = ioret;
1264 * Synchronous IO done without releasing page lock (e.g., as a part of
1265 * ->{prepare,commit}_write(). Completion is used to signal the end of
1268 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1269 if (atomic_dec_and_lock(&anchor->csi_sync_nr,
1270 &anchor->csi_waitq.lock)) {
1271 struct cl_dio_aio *aio = NULL;
1273 cl_sync_io_end_t *end_io = anchor->csi_end_io;
1276 * Holding the lock across both the decrement and
1277 * the wakeup ensures cl_sync_io_wait() doesn't complete
1278 * before the wakeup completes and the contents of
1279 * of anchor become unsafe to access as the owner is free
1280 * to immediately reclaim anchor when cl_sync_io_wait()
1283 wake_up_locked(&anchor->csi_waitq);
1285 end_io(env, anchor);
1286 if (anchor->csi_aio)
1287 aio = anchor->csi_aio;
1289 spin_unlock(&anchor->csi_waitq.lock);
1292 * If anchor->csi_aio is set, we are responsible for freeing
1293 * memory here rather than when cl_sync_io_wait() completes.
1299 EXPORT_SYMBOL(cl_sync_io_note);