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 #define cl_io_for_each(slice, io) \
56 list_for_each_entry((slice), &io->ci_layers, cis_linkage)
57 #define cl_io_for_each_reverse(slice, io) \
58 list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
60 static inline int cl_io_type_is_valid(enum cl_io_type type)
62 return CIT_READ <= type && type < CIT_OP_NR;
65 static inline int cl_io_is_loopable(const struct cl_io *io)
67 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
71 * cl_io invariant that holds at all times when exported cl_io_*() functions
72 * are entered and left.
74 static int cl_io_invariant(const struct cl_io *io)
81 * io can own pages only when it is ongoing. Sub-io might
82 * still be in CIS_LOCKED state when top-io is in
85 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
86 (io->ci_state == CIS_LOCKED && up != NULL));
90 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
92 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
94 struct cl_io_slice *slice;
96 LINVRNT(cl_io_type_is_valid(io->ci_type));
97 LINVRNT(cl_io_invariant(io));
100 while (!list_empty(&io->ci_layers)) {
101 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
103 list_del_init(&slice->cis_linkage);
104 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
105 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
107 * Invalidate slice to catch use after free. This assumes that
108 * slices are allocated within session and can be touched
109 * after ->cio_fini() returns.
111 slice->cis_io = NULL;
113 io->ci_state = CIS_FINI;
115 /* sanity check for layout change */
116 switch(io->ci_type) {
119 case CIT_DATA_VERSION:
123 LASSERT(!io->ci_need_restart);
127 /* Check ignore layout change conf */
128 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
129 !io->ci_need_restart));
138 EXPORT_SYMBOL(cl_io_fini);
140 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
141 enum cl_io_type iot, struct cl_object *obj)
143 struct cl_object *scan;
146 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
147 LINVRNT(cl_io_type_is_valid(iot));
148 LINVRNT(cl_io_invariant(io));
152 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
153 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
154 INIT_LIST_HEAD(&io->ci_layers);
157 cl_object_for_each(scan, obj) {
158 if (scan->co_ops->coo_io_init != NULL) {
159 result = scan->co_ops->coo_io_init(env, scan, io);
165 io->ci_state = CIS_INIT;
170 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
172 * \pre obj != cl_object_top(obj)
174 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
175 enum cl_io_type iot, struct cl_object *obj)
177 LASSERT(obj != cl_object_top(obj));
179 return cl_io_init0(env, io, iot, obj);
181 EXPORT_SYMBOL(cl_io_sub_init);
184 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
186 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
187 * what the latter returned.
189 * \pre obj == cl_object_top(obj)
190 * \pre cl_io_type_is_valid(iot)
191 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
193 int cl_io_init(const struct lu_env *env, struct cl_io *io,
194 enum cl_io_type iot, struct cl_object *obj)
196 LASSERT(obj == cl_object_top(obj));
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 if (cfs_ptengine_weight(cl_io_engine) < 2)
217 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
218 "io %s range: [%llu, %llu) %s %s %s %s\n",
219 iot == CIT_READ ? "read" : "write",
221 io->u.ci_rw.rw_nonblock ? "nonblock" : "block",
222 io->u.ci_rw.rw_append ? "append" : "-",
223 io->u.ci_rw.rw_sync ? "sync" : "-",
224 io->ci_pio ? "pio" : "-");
226 io->u.ci_rw.rw_range.cir_pos = pos;
227 io->u.ci_rw.rw_range.cir_count = count;
229 RETURN(cl_io_init(env, io, iot, io->ci_obj));
231 EXPORT_SYMBOL(cl_io_rw_init);
233 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
234 const struct cl_lock_descr *d1)
236 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
237 lu_object_fid(&d1->cld_obj->co_lu));
241 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
243 static void cl_io_locks_sort(struct cl_io *io)
248 /* hidden treasure: bubble sort for now. */
250 struct cl_io_lock_link *curr;
251 struct cl_io_lock_link *prev;
252 struct cl_io_lock_link *temp;
257 list_for_each_entry_safe(curr, temp, &io->ci_lockset.cls_todo,
260 switch (cl_lock_descr_sort(&prev->cill_descr,
261 &curr->cill_descr)) {
264 * IMPOSSIBLE: Identical locks are
271 list_move_tail(&curr->cill_linkage,
272 &prev->cill_linkage);
274 continue; /* don't change prev: it's
275 * still "previous" */
276 case -1: /* already in order */
286 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
287 const struct cl_lock_descr *d1)
289 d0->cld_start = min(d0->cld_start, d1->cld_start);
290 d0->cld_end = max(d0->cld_end, d1->cld_end);
292 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
293 d0->cld_mode = CLM_WRITE;
295 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
296 d0->cld_mode = CLM_GROUP;
299 static int cl_lockset_merge(const struct cl_lockset *set,
300 const struct cl_lock_descr *need)
302 struct cl_io_lock_link *scan;
305 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
306 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
309 /* Merge locks for the same object because ldlm lock server
310 * may expand the lock extent, otherwise there is a deadlock
311 * case if two conflicted locks are queueud for the same object
312 * and lock server expands one lock to overlap the another.
313 * The side effect is that it can generate a multi-stripe lock
314 * that may cause casacading problem */
315 cl_lock_descr_merge(&scan->cill_descr, need);
316 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
317 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
318 scan->cill_descr.cld_end);
324 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
325 struct cl_lockset *set)
327 struct cl_io_lock_link *link;
328 struct cl_io_lock_link *temp;
333 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
334 result = cl_lock_request(env, io, &link->cill_lock);
338 list_move(&link->cill_linkage, &set->cls_done);
344 * Takes locks necessary for the current iteration of io.
346 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
347 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
350 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
352 const struct cl_io_slice *scan;
355 LINVRNT(cl_io_is_loopable(io));
356 LINVRNT(io->ci_state == CIS_IT_STARTED);
357 LINVRNT(cl_io_invariant(io));
360 cl_io_for_each(scan, io) {
361 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
363 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
368 cl_io_locks_sort(io);
369 result = cl_lockset_lock(env, io, &io->ci_lockset);
372 cl_io_unlock(env, io);
374 io->ci_state = CIS_LOCKED;
377 EXPORT_SYMBOL(cl_io_lock);
380 * Release locks takes by io.
382 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
384 struct cl_lockset *set;
385 struct cl_io_lock_link *link;
386 struct cl_io_lock_link *temp;
387 const struct cl_io_slice *scan;
389 LASSERT(cl_io_is_loopable(io));
390 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
391 LINVRNT(cl_io_invariant(io));
394 set = &io->ci_lockset;
396 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
397 list_del_init(&link->cill_linkage);
398 if (link->cill_fini != NULL)
399 link->cill_fini(env, link);
402 list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
403 list_del_init(&link->cill_linkage);
404 cl_lock_release(env, &link->cill_lock);
405 if (link->cill_fini != NULL)
406 link->cill_fini(env, link);
409 cl_io_for_each_reverse(scan, io) {
410 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
411 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
413 io->ci_state = CIS_UNLOCKED;
416 EXPORT_SYMBOL(cl_io_unlock);
419 * Prepares next iteration of io.
421 * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
422 * layers a chance to modify io parameters, e.g., so that lov can restrict io
423 * to a single stripe.
425 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
427 const struct cl_io_slice *scan;
430 LINVRNT(cl_io_is_loopable(io));
431 LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
432 LINVRNT(cl_io_invariant(io));
436 cl_io_for_each(scan, io) {
437 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
439 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
445 io->ci_state = CIS_IT_STARTED;
448 EXPORT_SYMBOL(cl_io_iter_init);
451 * Finalizes io iteration.
453 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
455 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
457 const struct cl_io_slice *scan;
459 LINVRNT(cl_io_is_loopable(io));
460 LINVRNT(io->ci_state == CIS_UNLOCKED);
461 LINVRNT(cl_io_invariant(io));
464 cl_io_for_each_reverse(scan, io) {
465 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
466 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
468 io->ci_state = CIS_IT_ENDED;
471 EXPORT_SYMBOL(cl_io_iter_fini);
474 * Records that read or write io progressed \a nob bytes forward.
476 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
478 const struct cl_io_slice *scan;
480 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
482 LINVRNT(cl_io_is_loopable(io));
483 LINVRNT(cl_io_invariant(io));
487 io->u.ci_rw.rw_range.cir_pos += nob;
488 io->u.ci_rw.rw_range.cir_count -= nob;
490 /* layers have to be notified. */
491 cl_io_for_each_reverse(scan, io) {
492 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
493 scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
500 * Adds a lock to a lockset.
502 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
503 struct cl_io_lock_link *link)
508 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
511 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
516 EXPORT_SYMBOL(cl_io_lock_add);
518 static void cl_free_io_lock_link(const struct lu_env *env,
519 struct cl_io_lock_link *link)
525 * Allocates new lock link, and uses it to add a lock to a lockset.
527 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
528 struct cl_lock_descr *descr)
530 struct cl_io_lock_link *link;
536 link->cill_descr = *descr;
537 link->cill_fini = cl_free_io_lock_link;
538 result = cl_io_lock_add(env, io, link);
539 if (result) /* lock match */
540 link->cill_fini(env, link);
546 EXPORT_SYMBOL(cl_io_lock_alloc_add);
549 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
551 int cl_io_start(const struct lu_env *env, struct cl_io *io)
553 const struct cl_io_slice *scan;
556 LINVRNT(cl_io_is_loopable(io));
557 LINVRNT(io->ci_state == CIS_LOCKED);
558 LINVRNT(cl_io_invariant(io));
561 io->ci_state = CIS_IO_GOING;
562 cl_io_for_each(scan, io) {
563 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
565 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
573 EXPORT_SYMBOL(cl_io_start);
576 * Wait until current io iteration is finished by calling
577 * cl_io_operations::cio_end() bottom-to-top.
579 void cl_io_end(const struct lu_env *env, struct cl_io *io)
581 const struct cl_io_slice *scan;
583 LINVRNT(cl_io_is_loopable(io));
584 LINVRNT(io->ci_state == CIS_IO_GOING);
585 LINVRNT(cl_io_invariant(io));
588 cl_io_for_each_reverse(scan, io) {
589 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
590 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
591 /* TODO: error handling. */
593 io->ci_state = CIS_IO_FINISHED;
596 EXPORT_SYMBOL(cl_io_end);
599 * Called by read io, to decide the readahead extent
601 * \see cl_io_operations::cio_read_ahead()
603 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
604 pgoff_t start, struct cl_read_ahead *ra)
606 const struct cl_io_slice *scan;
609 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
610 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
611 LINVRNT(cl_io_invariant(io));
614 cl_io_for_each(scan, io) {
615 if (scan->cis_iop->cio_read_ahead == NULL)
618 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
622 RETURN(result > 0 ? 0 : result);
624 EXPORT_SYMBOL(cl_io_read_ahead);
627 * Commit a list of contiguous pages into writeback cache.
629 * \returns 0 if all pages committed, or errcode if error occurred.
630 * \see cl_io_operations::cio_commit_async()
632 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
633 struct cl_page_list *queue, int from, int to,
636 const struct cl_io_slice *scan;
640 cl_io_for_each(scan, io) {
641 if (scan->cis_iop->cio_commit_async == NULL)
643 result = scan->cis_iop->cio_commit_async(env, scan, queue,
650 EXPORT_SYMBOL(cl_io_commit_async);
653 * Submits a list of pages for immediate io.
655 * After the function gets returned, The submitted pages are moved to
656 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
657 * to be submitted, and the pages are errant to submit.
659 * \returns 0 if at least one page was submitted, error code otherwise.
660 * \see cl_io_operations::cio_submit()
662 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
663 enum cl_req_type crt, struct cl_2queue *queue)
665 const struct cl_io_slice *scan;
669 cl_io_for_each(scan, io) {
670 if (scan->cis_iop->cio_submit == NULL)
672 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
677 * If ->cio_submit() failed, no pages were sent.
679 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
682 EXPORT_SYMBOL(cl_io_submit_rw);
685 * Submit a sync_io and wait for the IO to be finished, or error happens.
686 * If \a timeout is zero, it means to wait for the IO unconditionally.
688 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
689 enum cl_req_type iot, struct cl_2queue *queue,
692 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
696 cl_page_list_for_each(pg, &queue->c2_qin) {
697 LASSERT(pg->cp_sync_io == NULL);
698 pg->cp_sync_io = anchor;
701 cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
702 rc = cl_io_submit_rw(env, io, iot, queue);
705 * If some pages weren't sent for any reason (e.g.,
706 * read found up-to-date pages in the cache, or write found
707 * clean pages), count them as completed to avoid infinite
710 cl_page_list_for_each(pg, &queue->c2_qin) {
711 pg->cp_sync_io = NULL;
712 cl_sync_io_note(env, anchor, 1);
715 /* wait for the IO to be finished. */
716 rc = cl_sync_io_wait(env, anchor, timeout);
717 cl_page_list_assume(env, io, &queue->c2_qout);
719 LASSERT(list_empty(&queue->c2_qout.pl_pages));
720 cl_page_list_for_each(pg, &queue->c2_qin)
721 pg->cp_sync_io = NULL;
725 EXPORT_SYMBOL(cl_io_submit_sync);
728 * Cancel an IO which has been submitted by cl_io_submit_rw.
730 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
731 struct cl_page_list *queue)
733 struct cl_page *page;
736 CERROR("Canceling ongoing page trasmission\n");
737 cl_page_list_for_each(page, queue) {
740 rc = cl_page_cancel(env, page);
741 result = result ?: rc;
747 struct cl_io_pt *cl_io_submit_pt(struct cl_io *io, loff_t pos, size_t count)
752 OBD_ALLOC(pt, sizeof(*pt));
754 RETURN(ERR_PTR(-ENOMEM));
757 init_sync_kiocb(&pt->cip_iocb, io->u.ci_rw.rw_file);
758 pt->cip_iocb.ki_pos = pos;
759 #ifdef HAVE_KIOCB_KI_LEFT
760 pt->cip_iocb.ki_left = count;
761 #elif defined(HAVE_KI_NBYTES)
762 pt->cip_iocb.ki_nbytes = count;
764 pt->cip_iter = io->u.ci_rw.rw_iter;
765 iov_iter_truncate(&pt->cip_iter, count);
766 pt->cip_file = io->u.ci_rw.rw_file;
767 pt->cip_iot = io->ci_type;
769 pt->cip_count = count;
772 rc = cfs_ptask_init(&pt->cip_task, io->u.ci_rw.rw_ptask, pt,
773 PTF_ORDERED | PTF_COMPLETE |
774 PTF_USER_MM | PTF_RETRY, smp_processor_id());
778 CDEBUG(D_VFSTRACE, "submit %s range: [%llu, %llu)\n",
779 io->ci_type == CIT_READ ? "read" : "write",
782 rc = cfs_ptask_submit(&pt->cip_task, cl_io_engine);
789 OBD_FREE(pt, sizeof(*pt));
796 * Pumps io through iterations calling
798 * - cl_io_iter_init()
808 * - cl_io_iter_fini()
810 * repeatedly until there is no more io to do.
812 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
814 struct cl_io_pt *pt = NULL, *head = NULL;
815 struct cl_io_pt **tail = &head;
818 size_t last_chunk_count = 0;
819 bool short_io = false;
823 LINVRNT(cl_io_is_loopable(io));
828 rc = cl_io_iter_init(env, io);
830 cl_io_iter_fini(env, io);
834 pos = io->u.ci_rw.rw_range.cir_pos;
835 count = io->u.ci_rw.rw_range.cir_count;
838 /* submit this range for parallel execution */
839 pt = cl_io_submit_pt(io, pos, count);
841 cl_io_iter_fini(env, io);
847 tail = &pt->cip_next;
849 size_t nob = io->ci_nob;
852 "execute type %u range: [%llu, %llu) nob: %zu %s\n",
853 io->ci_type, pos, pos + count, nob,
854 io->ci_continue ? "continue" : "stop");
856 rc = cl_io_lock(env, io);
858 cl_io_iter_fini(env, io);
863 * Notify layers that locks has been taken,
866 * - llite: kms, short read;
867 * - llite: generic_file_read();
869 rc = cl_io_start(env, io);
872 * Send any remaining pending
875 * - llite: ll_rw_stats_tally.
878 cl_io_unlock(env, io);
880 count = io->ci_nob - nob;
881 last_chunk_count = count;
884 cl_io_rw_advance(env, io, count);
885 cl_io_iter_fini(env, io);
886 } while (!rc && io->ci_continue);
888 CDEBUG(D_VFSTRACE, "loop type %u done: nob: %zu, rc: %d %s\n",
889 io->ci_type, io->ci_nob, rc,
890 io->ci_continue ? "continue" : "stop");
892 while (head != NULL) {
896 head = head->cip_next;
898 rc2 = cfs_ptask_wait_for(&pt->cip_task);
899 LASSERTF(!rc2, "wait for task error: %d\n", rc2);
901 rc2 = cfs_ptask_result(&pt->cip_task);
903 "done %s range: [%llu, %llu) ret: %zd, rc: %d\n",
904 pt->cip_iot == CIT_READ ? "read" : "write",
905 pt->cip_pos, pt->cip_pos + pt->cip_count,
906 pt->cip_result, rc2);
910 if (!rc2) /* IO is done by this task successfully */
911 io->ci_nob += pt->cip_result;
912 if (pt->cip_result < pt->cip_count) {
913 /* short IO happened.
914 * Not necessary to be an error */
916 "incomplete range: [%llu, %llu) "
917 "last_chunk_count: %zu\n",
919 pt->cip_pos + pt->cip_count,
921 io->ci_nob -= last_chunk_count;
925 OBD_FREE(pt, sizeof(*pt));
928 CDEBUG(D_VFSTRACE, "return nob: %zu (%s io), rc: %d\n",
929 io->ci_nob, short_io ? "short" : "full", rc);
931 RETURN(rc < 0 ? rc : io->ci_result);
933 EXPORT_SYMBOL(cl_io_loop);
936 * Adds io slice to the cl_io.
938 * This is called by cl_object_operations::coo_io_init() methods to add a
939 * per-layer state to the io. New state is added at the end of
940 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
942 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
944 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
945 struct cl_object *obj,
946 const struct cl_io_operations *ops)
948 struct list_head *linkage = &slice->cis_linkage;
950 LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
951 list_empty(linkage));
954 list_add_tail(linkage, &io->ci_layers);
956 slice->cis_obj = obj;
957 slice->cis_iop = ops;
960 EXPORT_SYMBOL(cl_io_slice_add);
964 * Initializes page list.
966 void cl_page_list_init(struct cl_page_list *plist)
970 INIT_LIST_HEAD(&plist->pl_pages);
971 plist->pl_owner = current;
974 EXPORT_SYMBOL(cl_page_list_init);
977 * Adds a page to a page list.
979 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
982 /* it would be better to check that page is owned by "current" io, but
983 * it is not passed here. */
984 LASSERT(page->cp_owner != NULL);
985 LINVRNT(plist->pl_owner == current);
987 LASSERT(list_empty(&page->cp_batch));
988 list_add_tail(&page->cp_batch, &plist->pl_pages);
990 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
994 EXPORT_SYMBOL(cl_page_list_add);
997 * Removes a page from a page list.
999 void cl_page_list_del(const struct lu_env *env,
1000 struct cl_page_list *plist, struct cl_page *page)
1002 LASSERT(plist->pl_nr > 0);
1003 LASSERT(cl_page_is_vmlocked(env, page));
1004 LINVRNT(plist->pl_owner == current);
1007 list_del_init(&page->cp_batch);
1009 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
1010 cl_page_put(env, page);
1013 EXPORT_SYMBOL(cl_page_list_del);
1016 * Moves a page from one page list to another.
1018 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1019 struct cl_page *page)
1021 LASSERT(src->pl_nr > 0);
1022 LINVRNT(dst->pl_owner == current);
1023 LINVRNT(src->pl_owner == current);
1026 list_move_tail(&page->cp_batch, &dst->pl_pages);
1029 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1033 EXPORT_SYMBOL(cl_page_list_move);
1036 * Moves a page from one page list to the head of another list.
1038 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
1039 struct cl_page *page)
1041 LASSERT(src->pl_nr > 0);
1042 LINVRNT(dst->pl_owner == current);
1043 LINVRNT(src->pl_owner == current);
1046 list_move(&page->cp_batch, &dst->pl_pages);
1049 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1053 EXPORT_SYMBOL(cl_page_list_move_head);
1056 * splice the cl_page_list, just as list head does
1058 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1060 struct cl_page *page;
1061 struct cl_page *tmp;
1063 LINVRNT(list->pl_owner == current);
1064 LINVRNT(head->pl_owner == current);
1067 cl_page_list_for_each_safe(page, tmp, list)
1068 cl_page_list_move(head, list, page);
1071 EXPORT_SYMBOL(cl_page_list_splice);
1074 * Disowns pages in a queue.
1076 void cl_page_list_disown(const struct lu_env *env,
1077 struct cl_io *io, struct cl_page_list *plist)
1079 struct cl_page *page;
1080 struct cl_page *temp;
1082 LINVRNT(plist->pl_owner == current);
1085 cl_page_list_for_each_safe(page, temp, plist) {
1086 LASSERT(plist->pl_nr > 0);
1088 list_del_init(&page->cp_batch);
1091 * cl_page_disown0 rather than usual cl_page_disown() is used,
1092 * because pages are possibly in CPS_FREEING state already due
1093 * to the call to cl_page_list_discard().
1096 * XXX cl_page_disown0() will fail if page is not locked.
1098 cl_page_disown0(env, io, page);
1099 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1101 cl_page_put(env, page);
1105 EXPORT_SYMBOL(cl_page_list_disown);
1108 * Releases pages from queue.
1110 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1112 struct cl_page *page;
1113 struct cl_page *temp;
1115 LINVRNT(plist->pl_owner == current);
1118 cl_page_list_for_each_safe(page, temp, plist)
1119 cl_page_list_del(env, plist, page);
1120 LASSERT(plist->pl_nr == 0);
1123 EXPORT_SYMBOL(cl_page_list_fini);
1126 * Assumes all pages in a queue.
1128 void cl_page_list_assume(const struct lu_env *env,
1129 struct cl_io *io, struct cl_page_list *plist)
1131 struct cl_page *page;
1133 LINVRNT(plist->pl_owner == current);
1135 cl_page_list_for_each(page, plist)
1136 cl_page_assume(env, io, page);
1140 * Discards all pages in a queue.
1142 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1143 struct cl_page_list *plist)
1145 struct cl_page *page;
1147 LINVRNT(plist->pl_owner == current);
1149 cl_page_list_for_each(page, plist)
1150 cl_page_discard(env, io, page);
1155 * Initialize dual page queue.
1157 void cl_2queue_init(struct cl_2queue *queue)
1160 cl_page_list_init(&queue->c2_qin);
1161 cl_page_list_init(&queue->c2_qout);
1164 EXPORT_SYMBOL(cl_2queue_init);
1167 * Add a page to the incoming page list of 2-queue.
1169 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1172 cl_page_list_add(&queue->c2_qin, page);
1175 EXPORT_SYMBOL(cl_2queue_add);
1178 * Disown pages in both lists of a 2-queue.
1180 void cl_2queue_disown(const struct lu_env *env,
1181 struct cl_io *io, struct cl_2queue *queue)
1184 cl_page_list_disown(env, io, &queue->c2_qin);
1185 cl_page_list_disown(env, io, &queue->c2_qout);
1188 EXPORT_SYMBOL(cl_2queue_disown);
1191 * Discard (truncate) pages in both lists of a 2-queue.
1193 void cl_2queue_discard(const struct lu_env *env,
1194 struct cl_io *io, struct cl_2queue *queue)
1197 cl_page_list_discard(env, io, &queue->c2_qin);
1198 cl_page_list_discard(env, io, &queue->c2_qout);
1201 EXPORT_SYMBOL(cl_2queue_discard);
1204 * Assume to own the pages in cl_2queue
1206 void cl_2queue_assume(const struct lu_env *env,
1207 struct cl_io *io, struct cl_2queue *queue)
1209 cl_page_list_assume(env, io, &queue->c2_qin);
1210 cl_page_list_assume(env, io, &queue->c2_qout);
1214 * Finalize both page lists of a 2-queue.
1216 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1219 cl_page_list_fini(env, &queue->c2_qout);
1220 cl_page_list_fini(env, &queue->c2_qin);
1223 EXPORT_SYMBOL(cl_2queue_fini);
1226 * Initialize a 2-queue to contain \a page in its incoming page list.
1228 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1231 cl_2queue_init(queue);
1232 cl_2queue_add(queue, page);
1235 EXPORT_SYMBOL(cl_2queue_init_page);
1238 * Returns top-level io.
1240 * \see cl_object_top()
1242 struct cl_io *cl_io_top(struct cl_io *io)
1245 while (io->ci_parent != NULL)
1249 EXPORT_SYMBOL(cl_io_top);
1252 * Prints human readable representation of \a io to the \a f.
1254 void cl_io_print(const struct lu_env *env, void *cookie,
1255 lu_printer_t printer, const struct cl_io *io)
1260 * Fills in attributes that are passed to server together with transfer. Only
1261 * attributes from \a flags may be touched. This can be called multiple times
1262 * for the same request.
1264 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1265 struct cl_req_attr *attr)
1267 struct cl_object *scan;
1270 cl_object_for_each(scan, obj) {
1271 if (scan->co_ops->coo_req_attr_set != NULL)
1272 scan->co_ops->coo_req_attr_set(env, scan, attr);
1276 EXPORT_SYMBOL(cl_req_attr_set);
1278 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1279 * wait for the IO to finish. */
1280 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1282 wake_up_all(&anchor->csi_waitq);
1284 /* it's safe to nuke or reuse anchor now */
1285 atomic_set(&anchor->csi_barrier, 0);
1287 EXPORT_SYMBOL(cl_sync_io_end);
1290 * Initialize synchronous io wait anchor
1292 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1293 void (*end)(const struct lu_env *, struct cl_sync_io *))
1296 memset(anchor, 0, sizeof(*anchor));
1297 init_waitqueue_head(&anchor->csi_waitq);
1298 atomic_set(&anchor->csi_sync_nr, nr);
1299 atomic_set(&anchor->csi_barrier, nr > 0);
1300 anchor->csi_sync_rc = 0;
1301 anchor->csi_end_io = end;
1302 LASSERT(end != NULL);
1305 EXPORT_SYMBOL(cl_sync_io_init);
1308 * Wait until all IO completes. Transfer completion routine has to call
1309 * cl_sync_io_note() for every entity.
1311 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1314 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1319 LASSERT(timeout >= 0);
1321 rc = l_wait_event(anchor->csi_waitq,
1322 atomic_read(&anchor->csi_sync_nr) == 0,
1325 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1326 rc, atomic_read(&anchor->csi_sync_nr));
1328 lwi = (struct l_wait_info) { 0 };
1329 (void)l_wait_event(anchor->csi_waitq,
1330 atomic_read(&anchor->csi_sync_nr) == 0,
1333 rc = anchor->csi_sync_rc;
1335 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1337 /* wait until cl_sync_io_note() has done wakeup */
1338 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1343 EXPORT_SYMBOL(cl_sync_io_wait);
1346 * Indicate that transfer of a single page completed.
1348 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1352 if (anchor->csi_sync_rc == 0 && ioret < 0)
1353 anchor->csi_sync_rc = ioret;
1355 * Synchronous IO done without releasing page lock (e.g., as a part of
1356 * ->{prepare,commit}_write(). Completion is used to signal the end of
1359 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1360 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1361 LASSERT(anchor->csi_end_io != NULL);
1362 anchor->csi_end_io(env, anchor);
1363 /* Can't access anchor any more */
1367 EXPORT_SYMBOL(cl_sync_io_note);