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 * Commit a list of contiguous pages into writeback cache.
589 * \returns 0 if all pages committed, or errcode if error occurred.
590 * \see cl_io_operations::cio_commit_async()
592 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
593 struct cl_page_list *queue, int from, int to,
596 const struct cl_io_slice *scan;
600 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
601 if (scan->cis_iop->cio_commit_async == NULL)
603 result = scan->cis_iop->cio_commit_async(env, scan, queue,
610 EXPORT_SYMBOL(cl_io_commit_async);
613 * Submits a list of pages for immediate io.
615 * After the function gets returned, The submitted pages are moved to
616 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
617 * to be submitted, and the pages are errant to submit.
619 * \returns 0 if at least one page was submitted, error code otherwise.
620 * \see cl_io_operations::cio_submit()
622 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
623 enum cl_req_type crt, struct cl_2queue *queue)
625 const struct cl_io_slice *scan;
629 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
630 if (scan->cis_iop->cio_submit == NULL)
632 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
637 * If ->cio_submit() failed, no pages were sent.
639 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
642 EXPORT_SYMBOL(cl_io_submit_rw);
645 * Submit a sync_io and wait for the IO to be finished, or error happens.
646 * If \a timeout is zero, it means to wait for the IO unconditionally.
648 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
649 enum cl_req_type iot, struct cl_2queue *queue,
652 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
657 cl_page_list_for_each(pg, &queue->c2_qin) {
658 LASSERT(pg->cp_sync_io == NULL);
659 pg->cp_sync_io = anchor;
662 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
663 rc = cl_io_submit_rw(env, io, iot, queue);
666 * If some pages weren't sent for any reason (e.g.,
667 * read found up-to-date pages in the cache, or write found
668 * clean pages), count them as completed to avoid infinite
671 cl_page_list_for_each(pg, &queue->c2_qin) {
672 pg->cp_sync_io = NULL;
673 cl_sync_io_note(env, anchor, 1);
676 /* wait for the IO to be finished. */
677 rc = cl_sync_io_wait(env, anchor, timeout);
678 cl_page_list_assume(env, io, &queue->c2_qout);
680 LASSERT(list_empty(&queue->c2_qout.pl_pages));
681 cl_page_list_for_each(pg, &queue->c2_qin)
682 pg->cp_sync_io = NULL;
686 EXPORT_SYMBOL(cl_io_submit_sync);
691 * Pumps io through iterations calling
693 * - cl_io_iter_init()
703 * - cl_io_iter_fini()
705 * repeatedly until there is no more io to do.
707 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
712 LINVRNT(cl_io_is_loopable(io));
719 result = cl_io_iter_init(env, io);
722 result = cl_io_lock(env, io);
725 * Notify layers that locks has been taken,
728 * - llite: kms, short read;
729 * - llite: generic_file_read();
731 result = cl_io_start(env, io);
733 * Send any remaining pending
736 ** - llite: ll_rw_stats_tally.
739 cl_io_unlock(env, io);
740 cl_io_rw_advance(env, io, io->ci_nob - nob);
743 cl_io_iter_fini(env, io);
746 } while ((result == 0 || result == -EIOCBQUEUED) &&
752 if (result == -EWOULDBLOCK && io->ci_ndelay) {
753 io->ci_need_restart = 1;
758 result = io->ci_result;
759 RETURN(result < 0 ? result : 0);
761 EXPORT_SYMBOL(cl_io_loop);
764 * Adds io slice to the cl_io.
766 * This is called by cl_object_operations::coo_io_init() methods to add a
767 * per-layer state to the io. New state is added at the end of
768 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
770 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
772 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
773 struct cl_object *obj,
774 const struct cl_io_operations *ops)
776 struct list_head *linkage = &slice->cis_linkage;
778 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
779 list_empty(linkage));
782 list_add_tail(linkage, &io->ci_layers);
784 slice->cis_obj = obj;
785 slice->cis_iop = ops;
788 EXPORT_SYMBOL(cl_io_slice_add);
792 * Initializes page list.
794 void cl_page_list_init(struct cl_page_list *plist)
798 INIT_LIST_HEAD(&plist->pl_pages);
801 EXPORT_SYMBOL(cl_page_list_init);
804 * Adds a page to a page list.
806 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
809 /* it would be better to check that page is owned by "current" io, but
810 * it is not passed here. */
811 LASSERT(page->cp_owner != NULL);
813 LASSERT(list_empty(&page->cp_batch));
814 list_add_tail(&page->cp_batch, &plist->pl_pages);
816 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
820 EXPORT_SYMBOL(cl_page_list_add);
823 * Removes a page from a page list.
825 void cl_page_list_del(const struct lu_env *env,
826 struct cl_page_list *plist, struct cl_page *page)
828 LASSERT(plist->pl_nr > 0);
829 LASSERT(cl_page_is_vmlocked(env, page));
832 list_del_init(&page->cp_batch);
834 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
835 cl_page_put(env, page);
838 EXPORT_SYMBOL(cl_page_list_del);
841 * Moves a page from one page list to another.
843 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
844 struct cl_page *page)
846 LASSERT(src->pl_nr > 0);
849 list_move_tail(&page->cp_batch, &dst->pl_pages);
852 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
856 EXPORT_SYMBOL(cl_page_list_move);
859 * Moves a page from one page list to the head of another list.
861 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
862 struct cl_page *page)
864 LASSERT(src->pl_nr > 0);
867 list_move(&page->cp_batch, &dst->pl_pages);
870 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
874 EXPORT_SYMBOL(cl_page_list_move_head);
877 * splice the cl_page_list, just as list head does
879 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
881 struct cl_page *page;
886 cl_page_list_for_each_safe(page, tmp, list)
887 cl_page_list_move(head, list, page);
890 EXPORT_SYMBOL(cl_page_list_splice);
893 * Disowns pages in a queue.
895 void cl_page_list_disown(const struct lu_env *env,
896 struct cl_io *io, struct cl_page_list *plist)
898 struct cl_page *page;
899 struct cl_page *temp;
903 cl_page_list_for_each_safe(page, temp, plist) {
904 LASSERT(plist->pl_nr > 0);
906 list_del_init(&page->cp_batch);
909 * cl_page_disown0 rather than usual cl_page_disown() is used,
910 * because pages are possibly in CPS_FREEING state already due
911 * to the call to cl_page_list_discard().
914 * XXX cl_page_disown0() will fail if page is not locked.
916 cl_page_disown0(env, io, page);
917 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
919 cl_page_put(env, page);
923 EXPORT_SYMBOL(cl_page_list_disown);
926 * Releases pages from queue.
928 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
930 struct cl_page *page;
931 struct cl_page *temp;
935 cl_page_list_for_each_safe(page, temp, plist)
936 cl_page_list_del(env, plist, page);
937 LASSERT(plist->pl_nr == 0);
940 EXPORT_SYMBOL(cl_page_list_fini);
943 * Assumes all pages in a queue.
945 void cl_page_list_assume(const struct lu_env *env,
946 struct cl_io *io, struct cl_page_list *plist)
948 struct cl_page *page;
951 cl_page_list_for_each(page, plist)
952 cl_page_assume(env, io, page);
956 * Discards all pages in a queue.
958 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
959 struct cl_page_list *plist)
961 struct cl_page *page;
964 cl_page_list_for_each(page, plist)
965 cl_page_discard(env, io, page);
968 EXPORT_SYMBOL(cl_page_list_discard);
971 * Initialize dual page queue.
973 void cl_2queue_init(struct cl_2queue *queue)
976 cl_page_list_init(&queue->c2_qin);
977 cl_page_list_init(&queue->c2_qout);
980 EXPORT_SYMBOL(cl_2queue_init);
983 * Add a page to the incoming page list of 2-queue.
985 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
988 cl_page_list_add(&queue->c2_qin, page);
991 EXPORT_SYMBOL(cl_2queue_add);
994 * Disown pages in both lists of a 2-queue.
996 void cl_2queue_disown(const struct lu_env *env,
997 struct cl_io *io, struct cl_2queue *queue)
1000 cl_page_list_disown(env, io, &queue->c2_qin);
1001 cl_page_list_disown(env, io, &queue->c2_qout);
1004 EXPORT_SYMBOL(cl_2queue_disown);
1007 * Discard (truncate) pages in both lists of a 2-queue.
1009 void cl_2queue_discard(const struct lu_env *env,
1010 struct cl_io *io, struct cl_2queue *queue)
1013 cl_page_list_discard(env, io, &queue->c2_qin);
1014 cl_page_list_discard(env, io, &queue->c2_qout);
1017 EXPORT_SYMBOL(cl_2queue_discard);
1020 * Assume to own the pages in cl_2queue
1022 void cl_2queue_assume(const struct lu_env *env,
1023 struct cl_io *io, struct cl_2queue *queue)
1025 cl_page_list_assume(env, io, &queue->c2_qin);
1026 cl_page_list_assume(env, io, &queue->c2_qout);
1030 * Finalize both page lists of a 2-queue.
1032 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1035 cl_page_list_fini(env, &queue->c2_qout);
1036 cl_page_list_fini(env, &queue->c2_qin);
1039 EXPORT_SYMBOL(cl_2queue_fini);
1042 * Initialize a 2-queue to contain \a page in its incoming page list.
1044 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1047 cl_2queue_init(queue);
1048 cl_2queue_add(queue, page);
1051 EXPORT_SYMBOL(cl_2queue_init_page);
1054 * Returns top-level io.
1056 * \see cl_object_top()
1058 struct cl_io *cl_io_top(struct cl_io *io)
1061 while (io->ci_parent != NULL)
1065 EXPORT_SYMBOL(cl_io_top);
1068 * Prints human readable representation of \a io to the \a f.
1070 void cl_io_print(const struct lu_env *env, void *cookie,
1071 lu_printer_t printer, const struct cl_io *io)
1076 * Fills in attributes that are passed to server together with transfer. Only
1077 * attributes from \a flags may be touched. This can be called multiple times
1078 * for the same request.
1080 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1081 struct cl_req_attr *attr)
1083 struct cl_object *scan;
1086 cl_object_for_each(scan, obj) {
1087 if (scan->co_ops->coo_req_attr_set != NULL)
1088 scan->co_ops->coo_req_attr_set(env, scan, attr);
1092 EXPORT_SYMBOL(cl_req_attr_set);
1095 * Initialize synchronous io wait \a anchor for \a nr pages with optional
1097 * \param anchor owned by caller, initialzied here.
1098 * \param nr number of pages initally pending in sync.
1099 * \param end optional callback sync_io completion, can be used to
1100 * trigger erasure coding, integrity, dedupe, or similar operation.
1101 * \q end is called with a spinlock on anchor->csi_waitq.lock
1104 void cl_sync_io_init_notify(struct cl_sync_io *anchor, int nr,
1105 struct cl_dio_aio *aio, cl_sync_io_end_t *end)
1108 memset(anchor, 0, sizeof(*anchor));
1109 init_waitqueue_head(&anchor->csi_waitq);
1110 atomic_set(&anchor->csi_sync_nr, nr);
1111 anchor->csi_sync_rc = 0;
1112 anchor->csi_end_io = end;
1113 anchor->csi_aio = aio;
1116 EXPORT_SYMBOL(cl_sync_io_init_notify);
1119 * Wait until all IO completes. Transfer completion routine has to call
1120 * cl_sync_io_note() for every entity.
1122 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1128 LASSERT(timeout >= 0);
1131 wait_event_idle_timeout(anchor->csi_waitq,
1132 atomic_read(&anchor->csi_sync_nr) == 0,
1133 cfs_time_seconds(timeout)) == 0) {
1135 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1136 rc, atomic_read(&anchor->csi_sync_nr));
1139 wait_event_idle(anchor->csi_waitq,
1140 atomic_read(&anchor->csi_sync_nr) == 0);
1142 rc = anchor->csi_sync_rc;
1144 /* We take the lock to ensure that cl_sync_io_note() has finished */
1145 spin_lock(&anchor->csi_waitq.lock);
1146 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1147 spin_unlock(&anchor->csi_waitq.lock);
1151 EXPORT_SYMBOL(cl_sync_io_wait);
1153 #ifndef HAVE_AIO_COMPLETE
1154 static inline void aio_complete(struct kiocb *iocb, ssize_t res, ssize_t res2)
1156 if (iocb->ki_complete)
1157 iocb->ki_complete(iocb, res, res2);
1161 static void cl_aio_end(const struct lu_env *env, struct cl_sync_io *anchor)
1163 struct cl_dio_aio *aio = container_of(anchor, typeof(*aio), cda_sync);
1164 ssize_t ret = anchor->csi_sync_rc;
1169 while (aio->cda_pages.pl_nr > 0) {
1170 struct cl_page *page = cl_page_list_first(&aio->cda_pages);
1171 struct page *vmpage = cl_page_vmpage(page);
1172 struct inode *inode = vmpage ? page2inode(vmpage) : NULL;
1175 /* We end up here in case of Direct IO only. For encrypted file,
1176 * mapping was set on pages in ll_direct_rw_pages(), so it has
1177 * to be cleared now before page cleanup.
1178 * PageChecked flag was also set there, so we clean up here.
1180 if (inode && IS_ENCRYPTED(inode)) {
1181 vmpage->mapping = NULL;
1182 ClearPageChecked(vmpage);
1184 cl_page_list_del(env, &aio->cda_pages, page);
1185 cl_page_delete(env, page);
1186 cl_page_put(env, page);
1189 if (!is_sync_kiocb(aio->cda_iocb) && !aio->cda_no_aio_complete)
1190 aio_complete(aio->cda_iocb, ret ?: aio->cda_bytes, 0);
1195 struct cl_dio_aio *cl_aio_alloc(struct kiocb *iocb)
1197 struct cl_dio_aio *aio;
1199 OBD_SLAB_ALLOC_PTR_GFP(aio, cl_dio_aio_kmem, GFP_NOFS);
1202 * Hold one ref so that it won't be released until
1203 * every pages is added.
1205 cl_sync_io_init_notify(&aio->cda_sync, 1, is_sync_kiocb(iocb) ?
1206 NULL : aio, cl_aio_end);
1207 cl_page_list_init(&aio->cda_pages);
1208 aio->cda_iocb = iocb;
1209 aio->cda_no_aio_complete = 0;
1213 EXPORT_SYMBOL(cl_aio_alloc);
1215 void cl_aio_free(struct cl_dio_aio *aio)
1218 OBD_SLAB_FREE_PTR(aio, cl_dio_aio_kmem);
1220 EXPORT_SYMBOL(cl_aio_free);
1224 * Indicate that transfer of a single page completed.
1226 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1230 if (anchor->csi_sync_rc == 0 && ioret < 0)
1231 anchor->csi_sync_rc = ioret;
1233 * Synchronous IO done without releasing page lock (e.g., as a part of
1234 * ->{prepare,commit}_write(). Completion is used to signal the end of
1237 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1238 if (atomic_dec_and_lock(&anchor->csi_sync_nr,
1239 &anchor->csi_waitq.lock)) {
1240 struct cl_dio_aio *aio = NULL;
1242 cl_sync_io_end_t *end_io = anchor->csi_end_io;
1245 * Holding the lock across both the decrement and
1246 * the wakeup ensures cl_sync_io_wait() doesn't complete
1247 * before the wakeup completes and the contents of
1248 * of anchor become unsafe to access as the owner is free
1249 * to immediately reclaim anchor when cl_sync_io_wait()
1252 wake_up_all_locked(&anchor->csi_waitq);
1254 end_io(env, anchor);
1255 if (anchor->csi_aio)
1256 aio = anchor->csi_aio;
1258 spin_unlock(&anchor->csi_waitq.lock);
1261 * If anchor->csi_aio is set, we are responsible for freeing
1262 * memory here rather than when cl_sync_io_wait() completes.
1268 EXPORT_SYMBOL(cl_sync_io_note);