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, 2016, 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 <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 <lustre_compat.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));
133 EXPORT_SYMBOL(cl_io_fini);
135 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
136 enum cl_io_type iot, struct cl_object *obj)
138 struct cl_object *scan;
141 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
142 LINVRNT(cl_io_type_is_valid(iot));
143 LINVRNT(cl_io_invariant(io));
147 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
148 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
149 INIT_LIST_HEAD(&io->ci_layers);
152 cl_object_for_each(scan, obj) {
153 if (scan->co_ops->coo_io_init != NULL) {
154 result = scan->co_ops->coo_io_init(env, scan, io);
160 io->ci_state = CIS_INIT;
165 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
167 * \pre obj != cl_object_top(obj)
169 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
170 enum cl_io_type iot, struct cl_object *obj)
172 LASSERT(obj != cl_object_top(obj));
174 return cl_io_init0(env, io, iot, obj);
176 EXPORT_SYMBOL(cl_io_sub_init);
179 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
181 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
182 * what the latter returned.
184 * \pre obj == cl_object_top(obj)
185 * \pre cl_io_type_is_valid(iot)
186 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
188 int cl_io_init(const struct lu_env *env, struct cl_io *io,
189 enum cl_io_type iot, struct cl_object *obj)
191 LASSERT(obj == cl_object_top(obj));
193 return cl_io_init0(env, io, iot, obj);
195 EXPORT_SYMBOL(cl_io_init);
198 * Initialize read or write io.
200 * \pre iot == CIT_READ || iot == CIT_WRITE
202 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
203 enum cl_io_type iot, loff_t pos, size_t count)
205 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
206 LINVRNT(io->ci_obj != NULL);
209 if (cfs_ptengine_weight(cl_io_engine) < 2)
212 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
213 "io %s range: [%llu, %llu) %s %s %s %s\n",
214 iot == CIT_READ ? "read" : "write",
216 io->u.ci_rw.rw_nonblock ? "nonblock" : "block",
217 io->u.ci_rw.rw_append ? "append" : "-",
218 io->u.ci_rw.rw_sync ? "sync" : "-",
219 io->ci_pio ? "pio" : "-");
221 io->u.ci_rw.rw_range.cir_pos = pos;
222 io->u.ci_rw.rw_range.cir_count = count;
224 RETURN(cl_io_init(env, io, iot, io->ci_obj));
226 EXPORT_SYMBOL(cl_io_rw_init);
228 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
229 const struct cl_lock_descr *d1)
231 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
232 lu_object_fid(&d1->cld_obj->co_lu));
236 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
238 static void cl_io_locks_sort(struct cl_io *io)
243 /* hidden treasure: bubble sort for now. */
245 struct cl_io_lock_link *curr;
246 struct cl_io_lock_link *prev;
247 struct cl_io_lock_link *temp;
252 list_for_each_entry_safe(curr, temp, &io->ci_lockset.cls_todo,
255 switch (cl_lock_descr_sort(&prev->cill_descr,
256 &curr->cill_descr)) {
259 * IMPOSSIBLE: Identical locks are
266 list_move_tail(&curr->cill_linkage,
267 &prev->cill_linkage);
269 continue; /* don't change prev: it's
270 * still "previous" */
271 case -1: /* already in order */
281 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
282 const struct cl_lock_descr *d1)
284 d0->cld_start = min(d0->cld_start, d1->cld_start);
285 d0->cld_end = max(d0->cld_end, d1->cld_end);
287 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
288 d0->cld_mode = CLM_WRITE;
290 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
291 d0->cld_mode = CLM_GROUP;
294 static int cl_lockset_merge(const struct cl_lockset *set,
295 const struct cl_lock_descr *need)
297 struct cl_io_lock_link *scan;
300 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
301 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
304 /* Merge locks for the same object because ldlm lock server
305 * may expand the lock extent, otherwise there is a deadlock
306 * case if two conflicted locks are queueud for the same object
307 * and lock server expands one lock to overlap the another.
308 * The side effect is that it can generate a multi-stripe lock
309 * that may cause casacading problem */
310 cl_lock_descr_merge(&scan->cill_descr, need);
311 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
312 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
313 scan->cill_descr.cld_end);
319 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
320 struct cl_lockset *set)
322 struct cl_io_lock_link *link;
323 struct cl_io_lock_link *temp;
328 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
329 result = cl_lock_request(env, io, &link->cill_lock);
333 list_move(&link->cill_linkage, &set->cls_done);
339 * Takes locks necessary for the current iteration of io.
341 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
342 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
345 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
347 const struct cl_io_slice *scan;
350 LINVRNT(cl_io_is_loopable(io));
351 LINVRNT(io->ci_state == CIS_IT_STARTED);
352 LINVRNT(cl_io_invariant(io));
355 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
356 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
358 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
363 cl_io_locks_sort(io);
364 result = cl_lockset_lock(env, io, &io->ci_lockset);
367 cl_io_unlock(env, io);
369 io->ci_state = CIS_LOCKED;
372 EXPORT_SYMBOL(cl_io_lock);
375 * Release locks takes by io.
377 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
379 struct cl_lockset *set;
380 struct cl_io_lock_link *link;
381 struct cl_io_lock_link *temp;
382 const struct cl_io_slice *scan;
384 LASSERT(cl_io_is_loopable(io));
385 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
386 LINVRNT(cl_io_invariant(io));
389 set = &io->ci_lockset;
391 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
392 list_del_init(&link->cill_linkage);
393 if (link->cill_fini != NULL)
394 link->cill_fini(env, link);
397 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
398 list_del_init(&link->cill_linkage);
399 cl_lock_release(env, &link->cill_lock);
400 if (link->cill_fini != NULL)
401 link->cill_fini(env, link);
404 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
405 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
406 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
408 io->ci_state = CIS_UNLOCKED;
411 EXPORT_SYMBOL(cl_io_unlock);
414 * Prepares next iteration of io.
416 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
417 * layers a chance to modify io parameters, e.g., so that lov can restrict io
418 * to a single stripe.
420 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
422 const struct cl_io_slice *scan;
425 LINVRNT(cl_io_is_loopable(io));
426 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
427 LINVRNT(cl_io_invariant(io));
431 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
432 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
434 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
440 io->ci_state = CIS_IT_STARTED;
443 EXPORT_SYMBOL(cl_io_iter_init);
446 * Finalizes io iteration.
448 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
450 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
452 const struct cl_io_slice *scan;
454 LINVRNT(cl_io_is_loopable(io));
455 LINVRNT(io->ci_state == CIS_UNLOCKED);
456 LINVRNT(cl_io_invariant(io));
459 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
460 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
461 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
463 io->ci_state = CIS_IT_ENDED;
466 EXPORT_SYMBOL(cl_io_iter_fini);
469 * Records that read or write io progressed \a nob bytes forward.
471 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
473 const struct cl_io_slice *scan;
475 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
477 LINVRNT(cl_io_is_loopable(io));
478 LINVRNT(cl_io_invariant(io));
482 io->u.ci_rw.rw_range.cir_pos += nob;
483 io->u.ci_rw.rw_range.cir_count -= nob;
485 /* layers have to be notified. */
486 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
487 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
488 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
495 * Adds a lock to a lockset.
497 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
498 struct cl_io_lock_link *link)
503 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
506 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
511 EXPORT_SYMBOL(cl_io_lock_add);
513 static void cl_free_io_lock_link(const struct lu_env *env,
514 struct cl_io_lock_link *link)
520 * Allocates new lock link, and uses it to add a lock to a lockset.
522 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
523 struct cl_lock_descr *descr)
525 struct cl_io_lock_link *link;
531 link->cill_descr = *descr;
532 link->cill_fini = cl_free_io_lock_link;
533 result = cl_io_lock_add(env, io, link);
534 if (result) /* lock match */
535 link->cill_fini(env, link);
541 EXPORT_SYMBOL(cl_io_lock_alloc_add);
544 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
546 int cl_io_start(const struct lu_env *env, struct cl_io *io)
548 const struct cl_io_slice *scan;
551 LINVRNT(cl_io_is_loopable(io));
552 LINVRNT(io->ci_state == CIS_LOCKED);
553 LINVRNT(cl_io_invariant(io));
556 io->ci_state = CIS_IO_GOING;
557 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
558 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
560 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
568 EXPORT_SYMBOL(cl_io_start);
571 * Wait until current io iteration is finished by calling
572 * cl_io_operations::cio_end() bottom-to-top.
574 void cl_io_end(const struct lu_env *env, struct cl_io *io)
576 const struct cl_io_slice *scan;
578 LINVRNT(cl_io_is_loopable(io));
579 LINVRNT(io->ci_state == CIS_IO_GOING);
580 LINVRNT(cl_io_invariant(io));
583 list_for_each_entry_reverse(scan, &io->ci_layers, cis_linkage) {
584 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
585 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
586 /* TODO: error handling. */
588 io->ci_state = CIS_IO_FINISHED;
591 EXPORT_SYMBOL(cl_io_end);
594 * Called by read io, to decide the readahead extent
596 * \see cl_io_operations::cio_read_ahead()
598 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
599 pgoff_t start, struct cl_read_ahead *ra)
601 const struct cl_io_slice *scan;
604 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
605 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
606 LINVRNT(cl_io_invariant(io));
609 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
610 if (scan->cis_iop->cio_read_ahead == NULL)
613 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
617 RETURN(result > 0 ? 0 : result);
619 EXPORT_SYMBOL(cl_io_read_ahead);
622 * Commit a list of contiguous pages into writeback cache.
624 * \returns 0 if all pages committed, or errcode if error occurred.
625 * \see cl_io_operations::cio_commit_async()
627 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
628 struct cl_page_list *queue, int from, int to,
631 const struct cl_io_slice *scan;
635 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
636 if (scan->cis_iop->cio_commit_async == NULL)
638 result = scan->cis_iop->cio_commit_async(env, scan, queue,
645 EXPORT_SYMBOL(cl_io_commit_async);
648 * Submits a list of pages for immediate io.
650 * After the function gets returned, The submitted pages are moved to
651 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
652 * to be submitted, and the pages are errant to submit.
654 * \returns 0 if at least one page was submitted, error code otherwise.
655 * \see cl_io_operations::cio_submit()
657 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
658 enum cl_req_type crt, struct cl_2queue *queue)
660 const struct cl_io_slice *scan;
664 list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
665 if (scan->cis_iop->cio_submit == NULL)
667 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
672 * If ->cio_submit() failed, no pages were sent.
674 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
677 EXPORT_SYMBOL(cl_io_submit_rw);
680 * Submit a sync_io and wait for the IO to be finished, or error happens.
681 * If \a timeout is zero, it means to wait for the IO unconditionally.
683 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
684 enum cl_req_type iot, struct cl_2queue *queue,
687 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
691 cl_page_list_for_each(pg, &queue->c2_qin) {
692 LASSERT(pg->cp_sync_io == NULL);
693 pg->cp_sync_io = anchor;
696 cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
697 rc = cl_io_submit_rw(env, io, iot, queue);
700 * If some pages weren't sent for any reason (e.g.,
701 * read found up-to-date pages in the cache, or write found
702 * clean pages), count them as completed to avoid infinite
705 cl_page_list_for_each(pg, &queue->c2_qin) {
706 pg->cp_sync_io = NULL;
707 cl_sync_io_note(env, anchor, 1);
710 /* wait for the IO to be finished. */
711 rc = cl_sync_io_wait(env, anchor, timeout);
712 cl_page_list_assume(env, io, &queue->c2_qout);
714 LASSERT(list_empty(&queue->c2_qout.pl_pages));
715 cl_page_list_for_each(pg, &queue->c2_qin)
716 pg->cp_sync_io = NULL;
720 EXPORT_SYMBOL(cl_io_submit_sync);
723 * Cancel an IO which has been submitted by cl_io_submit_rw.
725 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
726 struct cl_page_list *queue)
728 struct cl_page *page;
731 CERROR("Canceling ongoing page trasmission\n");
732 cl_page_list_for_each(page, queue) {
735 rc = cl_page_cancel(env, page);
736 result = result ?: rc;
742 struct cl_io_pt *cl_io_submit_pt(struct cl_io *io, loff_t pos, size_t count)
747 OBD_ALLOC(pt, sizeof(*pt));
749 RETURN(ERR_PTR(-ENOMEM));
752 init_sync_kiocb(&pt->cip_iocb, io->u.ci_rw.rw_file);
753 pt->cip_iocb.ki_pos = pos;
754 #ifdef HAVE_KIOCB_KI_LEFT
755 pt->cip_iocb.ki_left = count;
756 #elif defined(HAVE_KI_NBYTES)
757 pt->cip_iocb.ki_nbytes = count;
759 pt->cip_iter = io->u.ci_rw.rw_iter;
760 iov_iter_truncate(&pt->cip_iter, count);
761 pt->cip_file = io->u.ci_rw.rw_file;
762 pt->cip_iot = io->ci_type;
764 pt->cip_count = count;
767 rc = cfs_ptask_init(&pt->cip_task, io->u.ci_rw.rw_ptask, pt,
768 PTF_ORDERED | PTF_COMPLETE |
769 PTF_USER_MM | PTF_RETRY, smp_processor_id());
773 CDEBUG(D_VFSTRACE, "submit %s range: [%llu, %llu)\n",
774 io->ci_type == CIT_READ ? "read" : "write",
777 rc = cfs_ptask_submit(&pt->cip_task, cl_io_engine);
784 OBD_FREE(pt, sizeof(*pt));
791 * Pumps io through iterations calling
793 * - cl_io_iter_init()
803 * - cl_io_iter_fini()
805 * repeatedly until there is no more io to do.
807 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
809 struct cl_io_pt *pt = NULL, *head = NULL;
810 struct cl_io_pt **tail = &head;
813 size_t last_chunk_count = 0;
814 bool short_io = false;
818 LINVRNT(cl_io_is_loopable(io));
823 rc = cl_io_iter_init(env, io);
825 cl_io_iter_fini(env, io);
829 pos = io->u.ci_rw.rw_range.cir_pos;
830 count = io->u.ci_rw.rw_range.cir_count;
833 /* submit this range for parallel execution */
834 pt = cl_io_submit_pt(io, pos, count);
836 cl_io_iter_fini(env, io);
842 tail = &pt->cip_next;
844 size_t nob = io->ci_nob;
847 "execute type %u range: [%llu, %llu) nob: %zu %s\n",
848 io->ci_type, pos, pos + count, nob,
849 io->ci_continue ? "continue" : "stop");
851 rc = cl_io_lock(env, io);
853 cl_io_iter_fini(env, io);
858 * Notify layers that locks has been taken,
861 * - llite: kms, short read;
862 * - llite: generic_file_read();
864 rc = cl_io_start(env, io);
867 * Send any remaining pending
870 * - llite: ll_rw_stats_tally.
873 cl_io_unlock(env, io);
875 count = io->ci_nob - nob;
876 last_chunk_count = count;
879 cl_io_rw_advance(env, io, count);
880 cl_io_iter_fini(env, io);
881 } while (!rc && io->ci_continue);
883 CDEBUG(D_VFSTRACE, "loop type %u done: nob: %zu, rc: %d %s\n",
884 io->ci_type, io->ci_nob, rc,
885 io->ci_continue ? "continue" : "stop");
887 while (head != NULL) {
891 head = head->cip_next;
893 rc2 = cfs_ptask_wait_for(&pt->cip_task);
894 LASSERTF(!rc2, "wait for task error: %d\n", rc2);
896 rc2 = cfs_ptask_result(&pt->cip_task);
898 "done %s range: [%llu, %llu) ret: %zd, rc: %d\n",
899 pt->cip_iot == CIT_READ ? "read" : "write",
900 pt->cip_pos, pt->cip_pos + pt->cip_count,
901 pt->cip_result, rc2);
905 if (!rc2) /* IO is done by this task successfully */
906 io->ci_nob += pt->cip_result;
907 if (pt->cip_result < pt->cip_count) {
908 /* short IO happened.
909 * Not necessary to be an error */
911 "incomplete range: [%llu, %llu) "
912 "last_chunk_count: %zu\n",
914 pt->cip_pos + pt->cip_count,
916 io->ci_nob -= last_chunk_count;
920 OBD_FREE(pt, sizeof(*pt));
923 CDEBUG(D_VFSTRACE, "return nob: %zu (%s io), rc: %d\n",
924 io->ci_nob, short_io ? "short" : "full", rc);
926 RETURN(rc < 0 ? rc : io->ci_result);
928 EXPORT_SYMBOL(cl_io_loop);
931 * Adds io slice to the cl_io.
933 * This is called by cl_object_operations::coo_io_init() methods to add a
934 * per-layer state to the io. New state is added at the end of
935 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
937 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
939 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
940 struct cl_object *obj,
941 const struct cl_io_operations *ops)
943 struct list_head *linkage = &slice->cis_linkage;
945 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
946 list_empty(linkage));
949 list_add_tail(linkage, &io->ci_layers);
951 slice->cis_obj = obj;
952 slice->cis_iop = ops;
955 EXPORT_SYMBOL(cl_io_slice_add);
959 * Initializes page list.
961 void cl_page_list_init(struct cl_page_list *plist)
965 INIT_LIST_HEAD(&plist->pl_pages);
966 plist->pl_owner = current;
969 EXPORT_SYMBOL(cl_page_list_init);
972 * Adds a page to a page list.
974 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
977 /* it would be better to check that page is owned by "current" io, but
978 * it is not passed here. */
979 LASSERT(page->cp_owner != NULL);
980 LINVRNT(plist->pl_owner == current);
982 LASSERT(list_empty(&page->cp_batch));
983 list_add_tail(&page->cp_batch, &plist->pl_pages);
985 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
989 EXPORT_SYMBOL(cl_page_list_add);
992 * Removes a page from a page list.
994 void cl_page_list_del(const struct lu_env *env,
995 struct cl_page_list *plist, struct cl_page *page)
997 LASSERT(plist->pl_nr > 0);
998 LASSERT(cl_page_is_vmlocked(env, page));
999 LINVRNT(plist->pl_owner == current);
1002 list_del_init(&page->cp_batch);
1004 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1005 cl_page_put(env, page);
1008 EXPORT_SYMBOL(cl_page_list_del);
1011 * Moves a page from one page list to another.
1013 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1014 struct cl_page *page)
1016 LASSERT(src->pl_nr > 0);
1017 LINVRNT(dst->pl_owner == current);
1018 LINVRNT(src->pl_owner == current);
1021 list_move_tail(&page->cp_batch, &dst->pl_pages);
1024 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1028 EXPORT_SYMBOL(cl_page_list_move);
1031 * Moves a page from one page list to the head of another list.
1033 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
1034 struct cl_page *page)
1036 LASSERT(src->pl_nr > 0);
1037 LINVRNT(dst->pl_owner == current);
1038 LINVRNT(src->pl_owner == current);
1041 list_move(&page->cp_batch, &dst->pl_pages);
1044 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1048 EXPORT_SYMBOL(cl_page_list_move_head);
1051 * splice the cl_page_list, just as list head does
1053 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1055 struct cl_page *page;
1056 struct cl_page *tmp;
1058 LINVRNT(list->pl_owner == current);
1059 LINVRNT(head->pl_owner == current);
1062 cl_page_list_for_each_safe(page, tmp, list)
1063 cl_page_list_move(head, list, page);
1066 EXPORT_SYMBOL(cl_page_list_splice);
1069 * Disowns pages in a queue.
1071 void cl_page_list_disown(const struct lu_env *env,
1072 struct cl_io *io, struct cl_page_list *plist)
1074 struct cl_page *page;
1075 struct cl_page *temp;
1077 LINVRNT(plist->pl_owner == current);
1080 cl_page_list_for_each_safe(page, temp, plist) {
1081 LASSERT(plist->pl_nr > 0);
1083 list_del_init(&page->cp_batch);
1086 * cl_page_disown0 rather than usual cl_page_disown() is used,
1087 * because pages are possibly in CPS_FREEING state already due
1088 * to the call to cl_page_list_discard().
1091 * XXX cl_page_disown0() will fail if page is not locked.
1093 cl_page_disown0(env, io, page);
1094 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1096 cl_page_put(env, page);
1100 EXPORT_SYMBOL(cl_page_list_disown);
1103 * Releases pages from queue.
1105 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1107 struct cl_page *page;
1108 struct cl_page *temp;
1110 LINVRNT(plist->pl_owner == current);
1113 cl_page_list_for_each_safe(page, temp, plist)
1114 cl_page_list_del(env, plist, page);
1115 LASSERT(plist->pl_nr == 0);
1118 EXPORT_SYMBOL(cl_page_list_fini);
1121 * Assumes all pages in a queue.
1123 void cl_page_list_assume(const struct lu_env *env,
1124 struct cl_io *io, struct cl_page_list *plist)
1126 struct cl_page *page;
1128 LINVRNT(plist->pl_owner == current);
1130 cl_page_list_for_each(page, plist)
1131 cl_page_assume(env, io, page);
1135 * Discards all pages in a queue.
1137 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1138 struct cl_page_list *plist)
1140 struct cl_page *page;
1142 LINVRNT(plist->pl_owner == current);
1144 cl_page_list_for_each(page, plist)
1145 cl_page_discard(env, io, page);
1150 * Initialize dual page queue.
1152 void cl_2queue_init(struct cl_2queue *queue)
1155 cl_page_list_init(&queue->c2_qin);
1156 cl_page_list_init(&queue->c2_qout);
1159 EXPORT_SYMBOL(cl_2queue_init);
1162 * Add a page to the incoming page list of 2-queue.
1164 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1167 cl_page_list_add(&queue->c2_qin, page);
1170 EXPORT_SYMBOL(cl_2queue_add);
1173 * Disown pages in both lists of a 2-queue.
1175 void cl_2queue_disown(const struct lu_env *env,
1176 struct cl_io *io, struct cl_2queue *queue)
1179 cl_page_list_disown(env, io, &queue->c2_qin);
1180 cl_page_list_disown(env, io, &queue->c2_qout);
1183 EXPORT_SYMBOL(cl_2queue_disown);
1186 * Discard (truncate) pages in both lists of a 2-queue.
1188 void cl_2queue_discard(const struct lu_env *env,
1189 struct cl_io *io, struct cl_2queue *queue)
1192 cl_page_list_discard(env, io, &queue->c2_qin);
1193 cl_page_list_discard(env, io, &queue->c2_qout);
1196 EXPORT_SYMBOL(cl_2queue_discard);
1199 * Assume to own the pages in cl_2queue
1201 void cl_2queue_assume(const struct lu_env *env,
1202 struct cl_io *io, struct cl_2queue *queue)
1204 cl_page_list_assume(env, io, &queue->c2_qin);
1205 cl_page_list_assume(env, io, &queue->c2_qout);
1209 * Finalize both page lists of a 2-queue.
1211 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1214 cl_page_list_fini(env, &queue->c2_qout);
1215 cl_page_list_fini(env, &queue->c2_qin);
1218 EXPORT_SYMBOL(cl_2queue_fini);
1221 * Initialize a 2-queue to contain \a page in its incoming page list.
1223 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1226 cl_2queue_init(queue);
1227 cl_2queue_add(queue, page);
1230 EXPORT_SYMBOL(cl_2queue_init_page);
1233 * Returns top-level io.
1235 * \see cl_object_top()
1237 struct cl_io *cl_io_top(struct cl_io *io)
1240 while (io->ci_parent != NULL)
1244 EXPORT_SYMBOL(cl_io_top);
1247 * Prints human readable representation of \a io to the \a f.
1249 void cl_io_print(const struct lu_env *env, void *cookie,
1250 lu_printer_t printer, const struct cl_io *io)
1255 * Fills in attributes that are passed to server together with transfer. Only
1256 * attributes from \a flags may be touched. This can be called multiple times
1257 * for the same request.
1259 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1260 struct cl_req_attr *attr)
1262 struct cl_object *scan;
1265 cl_object_for_each(scan, obj) {
1266 if (scan->co_ops->coo_req_attr_set != NULL)
1267 scan->co_ops->coo_req_attr_set(env, scan, attr);
1271 EXPORT_SYMBOL(cl_req_attr_set);
1273 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1274 * wait for the IO to finish. */
1275 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1277 wake_up_all(&anchor->csi_waitq);
1279 /* it's safe to nuke or reuse anchor now */
1280 atomic_set(&anchor->csi_barrier, 0);
1282 EXPORT_SYMBOL(cl_sync_io_end);
1285 * Initialize synchronous io wait anchor
1287 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1288 void (*end)(const struct lu_env *, struct cl_sync_io *))
1291 memset(anchor, 0, sizeof(*anchor));
1292 init_waitqueue_head(&anchor->csi_waitq);
1293 atomic_set(&anchor->csi_sync_nr, nr);
1294 atomic_set(&anchor->csi_barrier, nr > 0);
1295 anchor->csi_sync_rc = 0;
1296 anchor->csi_end_io = end;
1297 LASSERT(end != NULL);
1300 EXPORT_SYMBOL(cl_sync_io_init);
1303 * Wait until all IO completes. Transfer completion routine has to call
1304 * cl_sync_io_note() for every entity.
1306 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1309 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1314 LASSERT(timeout >= 0);
1316 rc = l_wait_event(anchor->csi_waitq,
1317 atomic_read(&anchor->csi_sync_nr) == 0,
1320 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1321 rc, atomic_read(&anchor->csi_sync_nr));
1323 lwi = (struct l_wait_info) { 0 };
1324 (void)l_wait_event(anchor->csi_waitq,
1325 atomic_read(&anchor->csi_sync_nr) == 0,
1328 rc = anchor->csi_sync_rc;
1330 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1332 /* wait until cl_sync_io_note() has done wakeup */
1333 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1338 EXPORT_SYMBOL(cl_sync_io_wait);
1341 * Indicate that transfer of a single page completed.
1343 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1347 if (anchor->csi_sync_rc == 0 && ioret < 0)
1348 anchor->csi_sync_rc = ioret;
1350 * Synchronous IO done without releasing page lock (e.g., as a part of
1351 * ->{prepare,commit}_write(). Completion is used to signal the end of
1354 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1355 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1356 LASSERT(anchor->csi_end_io != NULL);
1357 anchor->csi_end_io(env, anchor);
1358 /* Can't access anchor any more */
1362 EXPORT_SYMBOL(cl_sync_io_note);