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"
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));
134 EXPORT_SYMBOL(cl_io_fini);
136 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
137 enum cl_io_type iot, struct cl_object *obj)
139 struct cl_object *scan;
142 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
143 LINVRNT(cl_io_type_is_valid(iot));
144 LINVRNT(cl_io_invariant(io));
148 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
149 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
150 INIT_LIST_HEAD(&io->ci_layers);
153 cl_object_for_each(scan, obj) {
154 if (scan->co_ops->coo_io_init != NULL) {
155 result = scan->co_ops->coo_io_init(env, scan, io);
161 io->ci_state = CIS_INIT;
166 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
168 * \pre obj != cl_object_top(obj)
170 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
171 enum cl_io_type iot, struct cl_object *obj)
173 LASSERT(obj != cl_object_top(obj));
175 return cl_io_init0(env, io, iot, obj);
177 EXPORT_SYMBOL(cl_io_sub_init);
180 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
182 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
183 * what the latter returned.
185 * \pre obj == cl_object_top(obj)
186 * \pre cl_io_type_is_valid(iot)
187 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
189 int cl_io_init(const struct lu_env *env, struct cl_io *io,
190 enum cl_io_type iot, struct cl_object *obj)
192 LASSERT(obj == cl_object_top(obj));
194 /* clear I/O restart from previous instance */
195 io->ci_need_restart = 0;
197 return cl_io_init0(env, io, iot, obj);
199 EXPORT_SYMBOL(cl_io_init);
202 * Initialize read or write io.
204 * \pre iot == CIT_READ || iot == CIT_WRITE
206 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
207 enum cl_io_type iot, loff_t pos, size_t count)
209 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
210 LINVRNT(io->ci_obj != NULL);
213 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
214 "io range: %u [%llu, %llu) %u %u\n",
215 iot, (__u64)pos, (__u64)pos + count,
216 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
217 io->u.ci_rw.crw_pos = pos;
218 io->u.ci_rw.crw_count = count;
219 RETURN(cl_io_init(env, io, iot, io->ci_obj));
221 EXPORT_SYMBOL(cl_io_rw_init);
223 static int cl_lock_descr_cmp(void *priv,
224 struct list_head *a, struct list_head *b)
226 const struct cl_io_lock_link *l0 = list_entry(a, struct cl_io_lock_link,
228 const struct cl_io_lock_link *l1 = list_entry(b, struct cl_io_lock_link,
230 const struct cl_lock_descr *d0 = &l0->cill_descr;
231 const struct cl_lock_descr *d1 = &l1->cill_descr;
233 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
234 lu_object_fid(&d1->cld_obj->co_lu));
237 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
238 const struct cl_lock_descr *d1)
240 d0->cld_start = min(d0->cld_start, d1->cld_start);
241 d0->cld_end = max(d0->cld_end, d1->cld_end);
243 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
244 d0->cld_mode = CLM_WRITE;
246 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
247 d0->cld_mode = CLM_GROUP;
250 static int cl_lockset_merge(const struct cl_lockset *set,
251 const struct cl_lock_descr *need)
253 struct cl_io_lock_link *scan;
256 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
257 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
260 /* Merge locks for the same object because ldlm lock server
261 * may expand the lock extent, otherwise there is a deadlock
262 * case if two conflicted locks are queueud for the same object
263 * and lock server expands one lock to overlap the another.
264 * The side effect is that it can generate a multi-stripe lock
265 * that may cause casacading problem */
266 cl_lock_descr_merge(&scan->cill_descr, need);
267 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
268 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
269 scan->cill_descr.cld_end);
275 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
276 struct cl_lockset *set)
278 struct cl_io_lock_link *link;
279 struct cl_io_lock_link *temp;
284 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
285 result = cl_lock_request(env, io, &link->cill_lock);
289 list_move(&link->cill_linkage, &set->cls_done);
295 * Takes locks necessary for the current iteration of io.
297 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
298 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
301 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
303 const struct cl_io_slice *scan;
306 LINVRNT(cl_io_is_loopable(io));
307 LINVRNT(io->ci_state == CIS_IT_STARTED);
308 LINVRNT(cl_io_invariant(io));
311 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
312 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
314 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
320 * Sort locks in lexicographical order of their (fid,
321 * start-offset) pairs to avoid deadlocks.
323 list_sort(NULL, &io->ci_lockset.cls_todo, cl_lock_descr_cmp);
324 result = cl_lockset_lock(env, io, &io->ci_lockset);
327 cl_io_unlock(env, io);
329 io->ci_state = CIS_LOCKED;
332 EXPORT_SYMBOL(cl_io_lock);
335 * Release locks takes by io.
337 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
339 struct cl_lockset *set;
340 struct cl_io_lock_link *link;
341 struct cl_io_lock_link *temp;
342 const struct cl_io_slice *scan;
344 LASSERT(cl_io_is_loopable(io));
345 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
346 LINVRNT(cl_io_invariant(io));
349 set = &io->ci_lockset;
351 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
352 list_del_init(&link->cill_linkage);
353 if (link->cill_fini != NULL)
354 link->cill_fini(env, link);
357 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
358 list_del_init(&link->cill_linkage);
359 cl_lock_release(env, &link->cill_lock);
360 if (link->cill_fini != NULL)
361 link->cill_fini(env, link);
364 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
365 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
366 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
368 io->ci_state = CIS_UNLOCKED;
371 EXPORT_SYMBOL(cl_io_unlock);
374 * Prepares next iteration of io.
376 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
377 * layers a chance to modify io parameters, e.g., so that lov can restrict io
378 * to a single stripe.
380 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
382 const struct cl_io_slice *scan;
385 LINVRNT(cl_io_is_loopable(io));
386 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
387 LINVRNT(cl_io_invariant(io));
391 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
392 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
394 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
400 io->ci_state = CIS_IT_STARTED;
403 EXPORT_SYMBOL(cl_io_iter_init);
406 * Finalizes io iteration.
408 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
410 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
412 const struct cl_io_slice *scan;
414 LINVRNT(cl_io_is_loopable(io));
415 LINVRNT(io->ci_state <= CIS_IT_STARTED ||
416 io->ci_state > CIS_IO_FINISHED);
417 LINVRNT(cl_io_invariant(io));
420 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
421 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
422 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
424 io->ci_state = CIS_IT_ENDED;
427 EXPORT_SYMBOL(cl_io_iter_fini);
430 * Records that read or write io progressed \a nob bytes forward.
432 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
434 const struct cl_io_slice *scan;
438 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
440 LINVRNT(cl_io_is_loopable(io));
441 LINVRNT(cl_io_invariant(io));
443 io->u.ci_rw.crw_pos += nob;
444 io->u.ci_rw.crw_count -= nob;
446 /* layers have to be notified. */
447 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
448 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
449 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
456 * Adds a lock to a lockset.
458 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
459 struct cl_io_lock_link *link)
464 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
467 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
472 EXPORT_SYMBOL(cl_io_lock_add);
474 static void cl_free_io_lock_link(const struct lu_env *env,
475 struct cl_io_lock_link *link)
481 * Allocates new lock link, and uses it to add a lock to a lockset.
483 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
484 struct cl_lock_descr *descr)
486 struct cl_io_lock_link *link;
492 link->cill_descr = *descr;
493 link->cill_fini = cl_free_io_lock_link;
494 result = cl_io_lock_add(env, io, link);
495 if (result) /* lock match */
496 link->cill_fini(env, link);
502 EXPORT_SYMBOL(cl_io_lock_alloc_add);
505 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
507 int cl_io_start(const struct lu_env *env, struct cl_io *io)
509 const struct cl_io_slice *scan;
512 LINVRNT(cl_io_is_loopable(io));
513 LINVRNT(io->ci_state == CIS_LOCKED);
514 LINVRNT(cl_io_invariant(io));
517 io->ci_state = CIS_IO_GOING;
518 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
519 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
521 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
529 EXPORT_SYMBOL(cl_io_start);
532 * Wait until current io iteration is finished by calling
533 * cl_io_operations::cio_end() bottom-to-top.
535 void cl_io_end(const struct lu_env *env, struct cl_io *io)
537 const struct cl_io_slice *scan;
539 LINVRNT(cl_io_is_loopable(io));
540 LINVRNT(io->ci_state == CIS_IO_GOING);
541 LINVRNT(cl_io_invariant(io));
544 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
545 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
546 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
547 /* TODO: error handling. */
549 io->ci_state = CIS_IO_FINISHED;
552 EXPORT_SYMBOL(cl_io_end);
555 * Called by read io, to decide the readahead extent
557 * \see cl_io_operations::cio_read_ahead()
559 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
560 pgoff_t start, struct cl_read_ahead *ra)
562 const struct cl_io_slice *scan;
565 LINVRNT(io->ci_type == CIT_READ ||
566 io->ci_type == CIT_FAULT ||
567 io->ci_type == CIT_WRITE);
568 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
569 LINVRNT(cl_io_invariant(io));
572 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
573 if (scan->cis_iop->cio_read_ahead == NULL)
576 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
580 RETURN(result > 0 ? 0 : result);
582 EXPORT_SYMBOL(cl_io_read_ahead);
585 * Commit a list of contiguous pages into writeback cache.
587 * \returns 0 if all pages committed, or errcode if error occurred.
588 * \see cl_io_operations::cio_commit_async()
590 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
591 struct cl_page_list *queue, int from, int to,
594 const struct cl_io_slice *scan;
598 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
599 if (scan->cis_iop->cio_commit_async == NULL)
601 result = scan->cis_iop->cio_commit_async(env, scan, queue,
608 EXPORT_SYMBOL(cl_io_commit_async);
611 * Submits a list of pages for immediate io.
613 * After the function gets returned, The submitted pages are moved to
614 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
615 * to be submitted, and the pages are errant to submit.
617 * \returns 0 if at least one page was submitted, error code otherwise.
618 * \see cl_io_operations::cio_submit()
620 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
621 enum cl_req_type crt, struct cl_2queue *queue)
623 const struct cl_io_slice *scan;
627 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
628 if (scan->cis_iop->cio_submit == NULL)
630 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
635 * If ->cio_submit() failed, no pages were sent.
637 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
640 EXPORT_SYMBOL(cl_io_submit_rw);
643 * Submit a sync_io and wait for the IO to be finished, or error happens.
644 * If \a timeout is zero, it means to wait for the IO unconditionally.
646 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
647 enum cl_req_type iot, struct cl_2queue *queue,
650 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
655 cl_page_list_for_each(pg, &queue->c2_qin) {
656 LASSERT(pg->cp_sync_io == NULL);
657 pg->cp_sync_io = anchor;
660 cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
661 rc = cl_io_submit_rw(env, io, iot, queue);
664 * If some pages weren't sent for any reason (e.g.,
665 * read found up-to-date pages in the cache, or write found
666 * clean pages), count them as completed to avoid infinite
669 cl_page_list_for_each(pg, &queue->c2_qin) {
670 pg->cp_sync_io = NULL;
671 cl_sync_io_note(env, anchor, 1);
674 /* wait for the IO to be finished. */
675 rc = cl_sync_io_wait(env, anchor, timeout);
676 cl_page_list_assume(env, io, &queue->c2_qout);
678 LASSERT(list_empty(&queue->c2_qout.pl_pages));
679 cl_page_list_for_each(pg, &queue->c2_qin)
680 pg->cp_sync_io = NULL;
684 EXPORT_SYMBOL(cl_io_submit_sync);
689 * Pumps io through iterations calling
691 * - cl_io_iter_init()
701 * - cl_io_iter_fini()
703 * repeatedly until there is no more io to do.
705 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
709 LINVRNT(cl_io_is_loopable(io));
716 result = cl_io_iter_init(env, io);
719 result = cl_io_lock(env, io);
722 * Notify layers that locks has been taken,
725 * - llite: kms, short read;
726 * - llite: generic_file_read();
728 result = cl_io_start(env, io);
730 * Send any remaining pending
733 ** - llite: ll_rw_stats_tally.
736 cl_io_unlock(env, io);
737 cl_io_rw_advance(env, io, io->ci_nob - nob);
740 cl_io_iter_fini(env, io);
741 } while (result == 0 && io->ci_continue);
743 if (result == -EWOULDBLOCK && io->ci_ndelay) {
744 io->ci_need_restart = 1;
749 result = io->ci_result;
750 RETURN(result < 0 ? result : 0);
752 EXPORT_SYMBOL(cl_io_loop);
755 * Adds io slice to the cl_io.
757 * This is called by cl_object_operations::coo_io_init() methods to add a
758 * per-layer state to the io. New state is added at the end of
759 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
761 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
763 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
764 struct cl_object *obj,
765 const struct cl_io_operations *ops)
767 struct list_head *linkage = &slice->cis_linkage;
769 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
770 list_empty(linkage));
773 list_add_tail(linkage, &io->ci_layers);
775 slice->cis_obj = obj;
776 slice->cis_iop = ops;
779 EXPORT_SYMBOL(cl_io_slice_add);
783 * Initializes page list.
785 void cl_page_list_init(struct cl_page_list *plist)
789 INIT_LIST_HEAD(&plist->pl_pages);
792 EXPORT_SYMBOL(cl_page_list_init);
795 * Adds a page to a page list.
797 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
800 /* it would be better to check that page is owned by "current" io, but
801 * it is not passed here. */
802 LASSERT(page->cp_owner != NULL);
804 LASSERT(list_empty(&page->cp_batch));
805 list_add_tail(&page->cp_batch, &plist->pl_pages);
807 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
811 EXPORT_SYMBOL(cl_page_list_add);
814 * Removes a page from a page list.
816 void cl_page_list_del(const struct lu_env *env,
817 struct cl_page_list *plist, struct cl_page *page)
819 LASSERT(plist->pl_nr > 0);
820 LASSERT(cl_page_is_vmlocked(env, page));
823 list_del_init(&page->cp_batch);
825 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
826 cl_page_put(env, page);
829 EXPORT_SYMBOL(cl_page_list_del);
832 * Moves a page from one page list to another.
834 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
835 struct cl_page *page)
837 LASSERT(src->pl_nr > 0);
840 list_move_tail(&page->cp_batch, &dst->pl_pages);
843 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
847 EXPORT_SYMBOL(cl_page_list_move);
850 * Moves a page from one page list to the head of another list.
852 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
853 struct cl_page *page)
855 LASSERT(src->pl_nr > 0);
858 list_move(&page->cp_batch, &dst->pl_pages);
861 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
865 EXPORT_SYMBOL(cl_page_list_move_head);
868 * splice the cl_page_list, just as list head does
870 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
872 struct cl_page *page;
877 cl_page_list_for_each_safe(page, tmp, list)
878 cl_page_list_move(head, list, page);
881 EXPORT_SYMBOL(cl_page_list_splice);
884 * Disowns pages in a queue.
886 void cl_page_list_disown(const struct lu_env *env,
887 struct cl_io *io, struct cl_page_list *plist)
889 struct cl_page *page;
890 struct cl_page *temp;
894 cl_page_list_for_each_safe(page, temp, plist) {
895 LASSERT(plist->pl_nr > 0);
897 list_del_init(&page->cp_batch);
900 * cl_page_disown0 rather than usual cl_page_disown() is used,
901 * because pages are possibly in CPS_FREEING state already due
902 * to the call to cl_page_list_discard().
905 * XXX cl_page_disown0() will fail if page is not locked.
907 cl_page_disown0(env, io, page);
908 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
910 cl_page_put(env, page);
914 EXPORT_SYMBOL(cl_page_list_disown);
917 * Releases pages from queue.
919 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
921 struct cl_page *page;
922 struct cl_page *temp;
926 cl_page_list_for_each_safe(page, temp, plist)
927 cl_page_list_del(env, plist, page);
928 LASSERT(plist->pl_nr == 0);
931 EXPORT_SYMBOL(cl_page_list_fini);
934 * Assumes all pages in a queue.
936 void cl_page_list_assume(const struct lu_env *env,
937 struct cl_io *io, struct cl_page_list *plist)
939 struct cl_page *page;
942 cl_page_list_for_each(page, plist)
943 cl_page_assume(env, io, page);
947 * Discards all pages in a queue.
949 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
950 struct cl_page_list *plist)
952 struct cl_page *page;
955 cl_page_list_for_each(page, plist)
956 cl_page_discard(env, io, page);
959 EXPORT_SYMBOL(cl_page_list_discard);
962 * Initialize dual page queue.
964 void cl_2queue_init(struct cl_2queue *queue)
967 cl_page_list_init(&queue->c2_qin);
968 cl_page_list_init(&queue->c2_qout);
971 EXPORT_SYMBOL(cl_2queue_init);
974 * Add a page to the incoming page list of 2-queue.
976 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
979 cl_page_list_add(&queue->c2_qin, page);
982 EXPORT_SYMBOL(cl_2queue_add);
985 * Disown pages in both lists of a 2-queue.
987 void cl_2queue_disown(const struct lu_env *env,
988 struct cl_io *io, struct cl_2queue *queue)
991 cl_page_list_disown(env, io, &queue->c2_qin);
992 cl_page_list_disown(env, io, &queue->c2_qout);
995 EXPORT_SYMBOL(cl_2queue_disown);
998 * Discard (truncate) pages in both lists of a 2-queue.
1000 void cl_2queue_discard(const struct lu_env *env,
1001 struct cl_io *io, struct cl_2queue *queue)
1004 cl_page_list_discard(env, io, &queue->c2_qin);
1005 cl_page_list_discard(env, io, &queue->c2_qout);
1008 EXPORT_SYMBOL(cl_2queue_discard);
1011 * Assume to own the pages in cl_2queue
1013 void cl_2queue_assume(const struct lu_env *env,
1014 struct cl_io *io, struct cl_2queue *queue)
1016 cl_page_list_assume(env, io, &queue->c2_qin);
1017 cl_page_list_assume(env, io, &queue->c2_qout);
1021 * Finalize both page lists of a 2-queue.
1023 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1026 cl_page_list_fini(env, &queue->c2_qout);
1027 cl_page_list_fini(env, &queue->c2_qin);
1030 EXPORT_SYMBOL(cl_2queue_fini);
1033 * Initialize a 2-queue to contain \a page in its incoming page list.
1035 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1038 cl_2queue_init(queue);
1039 cl_2queue_add(queue, page);
1042 EXPORT_SYMBOL(cl_2queue_init_page);
1045 * Returns top-level io.
1047 * \see cl_object_top()
1049 struct cl_io *cl_io_top(struct cl_io *io)
1052 while (io->ci_parent != NULL)
1056 EXPORT_SYMBOL(cl_io_top);
1059 * Prints human readable representation of \a io to the \a f.
1061 void cl_io_print(const struct lu_env *env, void *cookie,
1062 lu_printer_t printer, const struct cl_io *io)
1067 * Fills in attributes that are passed to server together with transfer. Only
1068 * attributes from \a flags may be touched. This can be called multiple times
1069 * for the same request.
1071 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1072 struct cl_req_attr *attr)
1074 struct cl_object *scan;
1077 cl_object_for_each(scan, obj) {
1078 if (scan->co_ops->coo_req_attr_set != NULL)
1079 scan->co_ops->coo_req_attr_set(env, scan, attr);
1083 EXPORT_SYMBOL(cl_req_attr_set);
1086 * Initialize synchronous io wait \a anchor for \a nr pages with optional
1088 * \param anchor owned by caller, initialzied here.
1089 * \param nr number of pages initally pending in sync.
1090 * \param end optional callback sync_io completion, can be used to
1091 * trigger erasure coding, integrity, dedupe, or similar operation.
1092 * \q end is called with a spinlock on anchor->csi_waitq.lock
1095 void cl_sync_io_init_notify(struct cl_sync_io *anchor, int nr,
1096 struct cl_dio_aio *aio, cl_sync_io_end_t *end)
1099 memset(anchor, 0, sizeof(*anchor));
1100 init_waitqueue_head(&anchor->csi_waitq);
1101 atomic_set(&anchor->csi_sync_nr, nr);
1102 anchor->csi_sync_rc = 0;
1103 anchor->csi_end_io = end;
1104 anchor->csi_aio = aio;
1107 EXPORT_SYMBOL(cl_sync_io_init_notify);
1110 * Wait until all IO completes. Transfer completion routine has to call
1111 * cl_sync_io_note() for every entity.
1113 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1119 LASSERT(timeout >= 0);
1122 wait_event_idle_timeout(anchor->csi_waitq,
1123 atomic_read(&anchor->csi_sync_nr) == 0,
1124 cfs_time_seconds(timeout)) == 0) {
1126 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1127 rc, atomic_read(&anchor->csi_sync_nr));
1130 wait_event_idle(anchor->csi_waitq,
1131 atomic_read(&anchor->csi_sync_nr) == 0);
1133 rc = anchor->csi_sync_rc;
1135 /* We take the lock to ensure that cl_sync_io_note() has finished */
1136 spin_lock(&anchor->csi_waitq.lock);
1137 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1138 spin_unlock(&anchor->csi_waitq.lock);
1142 EXPORT_SYMBOL(cl_sync_io_wait);
1144 #ifndef HAVE_AIO_COMPLETE
1145 static inline void aio_complete(struct kiocb *iocb, ssize_t res, ssize_t res2)
1147 if (iocb->ki_complete)
1148 iocb->ki_complete(iocb, res, res2);
1152 static void cl_aio_end(const struct lu_env *env, struct cl_sync_io *anchor)
1154 struct cl_dio_aio *aio = container_of(anchor, typeof(*aio), cda_sync);
1155 ssize_t ret = anchor->csi_sync_rc;
1160 while (aio->cda_pages.pl_nr > 0) {
1161 struct cl_page *page = cl_page_list_first(&aio->cda_pages);
1164 cl_page_list_del(env, &aio->cda_pages, page);
1165 cl_page_delete(env, page);
1166 cl_page_put(env, page);
1169 if (!is_sync_kiocb(aio->cda_iocb))
1170 aio_complete(aio->cda_iocb, ret ?: aio->cda_bytes, 0);
1175 struct cl_dio_aio *cl_aio_alloc(struct kiocb *iocb)
1177 struct cl_dio_aio *aio;
1179 OBD_SLAB_ALLOC_PTR_GFP(aio, cl_dio_aio_kmem, GFP_NOFS);
1182 * Hold one ref so that it won't be released until
1183 * every pages is added.
1185 cl_sync_io_init_notify(&aio->cda_sync, 1, is_sync_kiocb(iocb) ?
1186 NULL : aio, cl_aio_end);
1187 cl_page_list_init(&aio->cda_pages);
1188 aio->cda_iocb = iocb;
1192 EXPORT_SYMBOL(cl_aio_alloc);
1196 * Indicate that transfer of a single page completed.
1198 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1202 if (anchor->csi_sync_rc == 0 && ioret < 0)
1203 anchor->csi_sync_rc = ioret;
1205 * Synchronous IO done without releasing page lock (e.g., as a part of
1206 * ->{prepare,commit}_write(). Completion is used to signal the end of
1209 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1210 if (atomic_dec_and_lock(&anchor->csi_sync_nr,
1211 &anchor->csi_waitq.lock)) {
1212 struct cl_dio_aio *aio = NULL;
1214 cl_sync_io_end_t *end_io = anchor->csi_end_io;
1217 * Holding the lock across both the decrement and
1218 * the wakeup ensures cl_sync_io_wait() doesn't complete
1219 * before the wakeup completes and the contents of
1220 * of anchor become unsafe to access as the owner is free
1221 * to immediately reclaim anchor when cl_sync_io_wait()
1224 wake_up_all_locked(&anchor->csi_waitq);
1226 end_io(env, anchor);
1227 if (anchor->csi_aio)
1228 aio = anchor->csi_aio;
1230 spin_unlock(&anchor->csi_waitq.lock);
1233 * If anchor->csi_aio is set, we are responsible for freeing
1234 * memory here rather than when cl_sync_io_wait() completes.
1237 OBD_SLAB_FREE_PTR(aio, cl_dio_aio_kmem);
1241 EXPORT_SYMBOL(cl_sync_io_note);