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));
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 * Commit a list of contiguous pages into writeback cache.
588 * \returns 0 if all pages committed, or errcode if error occurred.
589 * \see cl_io_operations::cio_commit_async()
591 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
592 struct cl_page_list *queue, int from, int to,
595 const struct cl_io_slice *scan;
599 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
600 if (scan->cis_iop->cio_commit_async == NULL)
602 result = scan->cis_iop->cio_commit_async(env, scan, queue,
609 EXPORT_SYMBOL(cl_io_commit_async);
612 * Submits a list of pages for immediate io.
614 * After the function gets returned, The submitted pages are moved to
615 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
616 * to be submitted, and the pages are errant to submit.
618 * \returns 0 if at least one page was submitted, error code otherwise.
619 * \see cl_io_operations::cio_submit()
621 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
622 enum cl_req_type crt, struct cl_2queue *queue)
624 const struct cl_io_slice *scan;
628 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
629 if (scan->cis_iop->cio_submit == NULL)
631 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
636 * If ->cio_submit() failed, no pages were sent.
638 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
641 EXPORT_SYMBOL(cl_io_submit_rw);
644 * Submit a sync_io and wait for the IO to be finished, or error happens.
645 * If \a timeout is zero, it means to wait for the IO unconditionally.
647 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
648 enum cl_req_type iot, struct cl_2queue *queue,
651 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
656 cl_page_list_for_each(pg, &queue->c2_qin) {
657 LASSERT(pg->cp_sync_io == NULL);
658 pg->cp_sync_io = anchor;
661 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
662 rc = cl_io_submit_rw(env, io, iot, queue);
665 * If some pages weren't sent for any reason (e.g.,
666 * read found up-to-date pages in the cache, or write found
667 * clean pages), count them as completed to avoid infinite
670 cl_page_list_for_each(pg, &queue->c2_qin) {
671 pg->cp_sync_io = NULL;
672 cl_sync_io_note(env, anchor, 1);
675 /* wait for the IO to be finished. */
676 rc = cl_sync_io_wait(env, anchor, timeout);
677 cl_page_list_assume(env, io, &queue->c2_qout);
679 LASSERT(list_empty(&queue->c2_qout.pl_pages));
680 cl_page_list_for_each(pg, &queue->c2_qin)
681 pg->cp_sync_io = NULL;
685 EXPORT_SYMBOL(cl_io_submit_sync);
690 * Pumps io through iterations calling
692 * - cl_io_iter_init()
702 * - cl_io_iter_fini()
704 * repeatedly until there is no more io to do.
706 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
711 LINVRNT(cl_io_is_loopable(io));
718 result = cl_io_iter_init(env, io);
721 result = cl_io_lock(env, io);
724 * Notify layers that locks has been taken,
727 * - llite: kms, short read;
728 * - llite: generic_file_read();
730 result = cl_io_start(env, io);
732 * Send any remaining pending
735 ** - llite: ll_rw_stats_tally.
738 cl_io_unlock(env, io);
739 cl_io_rw_advance(env, io, io->ci_nob - nob);
742 cl_io_iter_fini(env, io);
745 } while ((result == 0 || result == -EIOCBQUEUED) &&
751 if (result == -EWOULDBLOCK && io->ci_ndelay) {
752 io->ci_need_restart = 1;
757 result = io->ci_result;
758 RETURN(result < 0 ? result : 0);
760 EXPORT_SYMBOL(cl_io_loop);
763 * Adds io slice to the cl_io.
765 * This is called by cl_object_operations::coo_io_init() methods to add a
766 * per-layer state to the io. New state is added at the end of
767 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
769 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
771 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
772 struct cl_object *obj,
773 const struct cl_io_operations *ops)
775 struct list_head *linkage = &slice->cis_linkage;
777 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
778 list_empty(linkage));
781 list_add_tail(linkage, &io->ci_layers);
783 slice->cis_obj = obj;
784 slice->cis_iop = ops;
787 EXPORT_SYMBOL(cl_io_slice_add);
791 * Initializes page list.
793 void cl_page_list_init(struct cl_page_list *plist)
797 INIT_LIST_HEAD(&plist->pl_pages);
800 EXPORT_SYMBOL(cl_page_list_init);
803 * Adds a page to a page list.
805 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
808 /* it would be better to check that page is owned by "current" io, but
809 * it is not passed here. */
810 LASSERT(page->cp_owner != NULL);
812 LASSERT(list_empty(&page->cp_batch));
813 list_add_tail(&page->cp_batch, &plist->pl_pages);
815 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
819 EXPORT_SYMBOL(cl_page_list_add);
822 * Removes a page from a page list.
824 void cl_page_list_del(const struct lu_env *env,
825 struct cl_page_list *plist, struct cl_page *page)
827 LASSERT(plist->pl_nr > 0);
828 LASSERT(cl_page_is_vmlocked(env, page));
831 list_del_init(&page->cp_batch);
833 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
834 cl_page_put(env, page);
837 EXPORT_SYMBOL(cl_page_list_del);
840 * Moves a page from one page list to another.
842 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
843 struct cl_page *page)
845 LASSERT(src->pl_nr > 0);
848 list_move_tail(&page->cp_batch, &dst->pl_pages);
851 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
855 EXPORT_SYMBOL(cl_page_list_move);
858 * Moves a page from one page list to the head of another list.
860 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
861 struct cl_page *page)
863 LASSERT(src->pl_nr > 0);
866 list_move(&page->cp_batch, &dst->pl_pages);
869 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
873 EXPORT_SYMBOL(cl_page_list_move_head);
876 * splice the cl_page_list, just as list head does
878 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
880 struct cl_page *page;
885 cl_page_list_for_each_safe(page, tmp, list)
886 cl_page_list_move(head, list, page);
889 EXPORT_SYMBOL(cl_page_list_splice);
892 * Disowns pages in a queue.
894 void cl_page_list_disown(const struct lu_env *env,
895 struct cl_io *io, struct cl_page_list *plist)
897 struct cl_page *page;
898 struct cl_page *temp;
902 cl_page_list_for_each_safe(page, temp, plist) {
903 LASSERT(plist->pl_nr > 0);
905 list_del_init(&page->cp_batch);
908 * cl_page_disown0 rather than usual cl_page_disown() is used,
909 * because pages are possibly in CPS_FREEING state already due
910 * to the call to cl_page_list_discard().
913 * XXX cl_page_disown0() will fail if page is not locked.
915 cl_page_disown0(env, io, page);
916 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
918 cl_page_put(env, page);
922 EXPORT_SYMBOL(cl_page_list_disown);
925 * Releases pages from queue.
927 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
929 struct cl_page *page;
930 struct cl_page *temp;
934 cl_page_list_for_each_safe(page, temp, plist)
935 cl_page_list_del(env, plist, page);
936 LASSERT(plist->pl_nr == 0);
939 EXPORT_SYMBOL(cl_page_list_fini);
942 * Assumes all pages in a queue.
944 void cl_page_list_assume(const struct lu_env *env,
945 struct cl_io *io, struct cl_page_list *plist)
947 struct cl_page *page;
950 cl_page_list_for_each(page, plist)
951 cl_page_assume(env, io, page);
955 * Discards all pages in a queue.
957 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
958 struct cl_page_list *plist)
960 struct cl_page *page;
963 cl_page_list_for_each(page, plist)
964 cl_page_discard(env, io, page);
967 EXPORT_SYMBOL(cl_page_list_discard);
970 * Initialize dual page queue.
972 void cl_2queue_init(struct cl_2queue *queue)
975 cl_page_list_init(&queue->c2_qin);
976 cl_page_list_init(&queue->c2_qout);
979 EXPORT_SYMBOL(cl_2queue_init);
982 * Add a page to the incoming page list of 2-queue.
984 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
987 cl_page_list_add(&queue->c2_qin, page);
990 EXPORT_SYMBOL(cl_2queue_add);
993 * Disown pages in both lists of a 2-queue.
995 void cl_2queue_disown(const struct lu_env *env,
996 struct cl_io *io, struct cl_2queue *queue)
999 cl_page_list_disown(env, io, &queue->c2_qin);
1000 cl_page_list_disown(env, io, &queue->c2_qout);
1003 EXPORT_SYMBOL(cl_2queue_disown);
1006 * Discard (truncate) pages in both lists of a 2-queue.
1008 void cl_2queue_discard(const struct lu_env *env,
1009 struct cl_io *io, struct cl_2queue *queue)
1012 cl_page_list_discard(env, io, &queue->c2_qin);
1013 cl_page_list_discard(env, io, &queue->c2_qout);
1016 EXPORT_SYMBOL(cl_2queue_discard);
1019 * Assume to own the pages in cl_2queue
1021 void cl_2queue_assume(const struct lu_env *env,
1022 struct cl_io *io, struct cl_2queue *queue)
1024 cl_page_list_assume(env, io, &queue->c2_qin);
1025 cl_page_list_assume(env, io, &queue->c2_qout);
1029 * Finalize both page lists of a 2-queue.
1031 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1034 cl_page_list_fini(env, &queue->c2_qout);
1035 cl_page_list_fini(env, &queue->c2_qin);
1038 EXPORT_SYMBOL(cl_2queue_fini);
1041 * Initialize a 2-queue to contain \a page in its incoming page list.
1043 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1046 cl_2queue_init(queue);
1047 cl_2queue_add(queue, page);
1050 EXPORT_SYMBOL(cl_2queue_init_page);
1053 * Returns top-level io.
1055 * \see cl_object_top()
1057 struct cl_io *cl_io_top(struct cl_io *io)
1060 while (io->ci_parent != NULL)
1064 EXPORT_SYMBOL(cl_io_top);
1067 * Prints human readable representation of \a io to the \a f.
1069 void cl_io_print(const struct lu_env *env, void *cookie,
1070 lu_printer_t printer, const struct cl_io *io)
1075 * Fills in attributes that are passed to server together with transfer. Only
1076 * attributes from \a flags may be touched. This can be called multiple times
1077 * for the same request.
1079 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1080 struct cl_req_attr *attr)
1082 struct cl_object *scan;
1085 cl_object_for_each(scan, obj) {
1086 if (scan->co_ops->coo_req_attr_set != NULL)
1087 scan->co_ops->coo_req_attr_set(env, scan, attr);
1091 EXPORT_SYMBOL(cl_req_attr_set);
1094 * Initialize synchronous io wait \a anchor for \a nr pages with optional
1096 * \param anchor owned by caller, initialzied here.
1097 * \param nr number of pages initally pending in sync.
1098 * \param end optional callback sync_io completion, can be used to
1099 * trigger erasure coding, integrity, dedupe, or similar operation.
1100 * \q end is called with a spinlock on anchor->csi_waitq.lock
1103 void cl_sync_io_init_notify(struct cl_sync_io *anchor, int nr,
1104 struct cl_dio_aio *aio, cl_sync_io_end_t *end)
1107 memset(anchor, 0, sizeof(*anchor));
1108 init_waitqueue_head(&anchor->csi_waitq);
1109 atomic_set(&anchor->csi_sync_nr, nr);
1110 anchor->csi_sync_rc = 0;
1111 anchor->csi_end_io = end;
1112 anchor->csi_aio = aio;
1115 EXPORT_SYMBOL(cl_sync_io_init_notify);
1118 * Wait until all IO completes. Transfer completion routine has to call
1119 * cl_sync_io_note() for every entity.
1121 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1127 LASSERT(timeout >= 0);
1130 wait_event_idle_timeout(anchor->csi_waitq,
1131 atomic_read(&anchor->csi_sync_nr) == 0,
1132 cfs_time_seconds(timeout)) == 0) {
1134 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1135 rc, atomic_read(&anchor->csi_sync_nr));
1138 wait_event_idle(anchor->csi_waitq,
1139 atomic_read(&anchor->csi_sync_nr) == 0);
1141 rc = anchor->csi_sync_rc;
1143 /* We take the lock to ensure that cl_sync_io_note() has finished */
1144 spin_lock(&anchor->csi_waitq.lock);
1145 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1146 spin_unlock(&anchor->csi_waitq.lock);
1150 EXPORT_SYMBOL(cl_sync_io_wait);
1152 #ifndef HAVE_AIO_COMPLETE
1153 static inline void aio_complete(struct kiocb *iocb, ssize_t res, ssize_t res2)
1155 if (iocb->ki_complete)
1156 iocb->ki_complete(iocb, res, res2);
1160 static void cl_aio_end(const struct lu_env *env, struct cl_sync_io *anchor)
1162 struct cl_dio_aio *aio = container_of(anchor, typeof(*aio), cda_sync);
1163 ssize_t ret = anchor->csi_sync_rc;
1168 while (aio->cda_pages.pl_nr > 0) {
1169 struct cl_page *page = cl_page_list_first(&aio->cda_pages);
1170 struct page *vmpage = cl_page_vmpage(page);
1171 struct inode *inode = vmpage ? page2inode(vmpage) : NULL;
1174 /* We end up here in case of Direct IO only. For encrypted file,
1175 * mapping was set on pages in ll_direct_rw_pages(), so it has
1176 * to be cleared now before page cleanup.
1177 * PageChecked flag was also set there, so we clean up here.
1179 if (inode && IS_ENCRYPTED(inode)) {
1180 vmpage->mapping = NULL;
1181 ClearPageChecked(vmpage);
1183 cl_page_list_del(env, &aio->cda_pages, page);
1184 cl_page_delete(env, page);
1185 cl_page_put(env, page);
1188 if (!is_sync_kiocb(aio->cda_iocb) && !aio->cda_no_aio_complete)
1189 aio_complete(aio->cda_iocb, ret ?: aio->cda_bytes, 0);
1194 struct cl_dio_aio *cl_aio_alloc(struct kiocb *iocb)
1196 struct cl_dio_aio *aio;
1198 OBD_SLAB_ALLOC_PTR_GFP(aio, cl_dio_aio_kmem, GFP_NOFS);
1201 * Hold one ref so that it won't be released until
1202 * every pages is added.
1204 cl_sync_io_init_notify(&aio->cda_sync, 1, is_sync_kiocb(iocb) ?
1205 NULL : aio, cl_aio_end);
1206 cl_page_list_init(&aio->cda_pages);
1207 aio->cda_iocb = iocb;
1208 aio->cda_no_aio_complete = 0;
1212 EXPORT_SYMBOL(cl_aio_alloc);
1214 void cl_aio_free(struct cl_dio_aio *aio)
1217 OBD_SLAB_FREE_PTR(aio, cl_dio_aio_kmem);
1219 EXPORT_SYMBOL(cl_aio_free);
1223 * Indicate that transfer of a single page completed.
1225 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1229 if (anchor->csi_sync_rc == 0 && ioret < 0)
1230 anchor->csi_sync_rc = ioret;
1232 * Synchronous IO done without releasing page lock (e.g., as a part of
1233 * ->{prepare,commit}_write(). Completion is used to signal the end of
1236 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1237 if (atomic_dec_and_lock(&anchor->csi_sync_nr,
1238 &anchor->csi_waitq.lock)) {
1239 struct cl_dio_aio *aio = NULL;
1241 cl_sync_io_end_t *end_io = anchor->csi_end_io;
1244 * Holding the lock across both the decrement and
1245 * the wakeup ensures cl_sync_io_wait() doesn't complete
1246 * before the wakeup completes and the contents of
1247 * of anchor become unsafe to access as the owner is free
1248 * to immediately reclaim anchor when cl_sync_io_wait()
1251 wake_up_all_locked(&anchor->csi_waitq);
1253 end_io(env, anchor);
1254 if (anchor->csi_aio)
1255 aio = anchor->csi_aio;
1257 spin_unlock(&anchor->csi_waitq.lock);
1260 * If anchor->csi_aio is set, we are responsible for freeing
1261 * memory here rather than when cl_sync_io_wait() completes.
1267 EXPORT_SYMBOL(cl_sync_io_note);