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, 2015, 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 "../include/obd_class.h"
41 #include "../include/obd_support.h"
42 #include "../include/lustre_fid.h"
43 #include <linux/list.h>
44 #include <linux/sched.h>
45 #include "../include/cl_object.h"
46 #include "cl_internal.h"
48 /*****************************************************************************
54 #define cl_io_for_each(slice, io) \
55 list_for_each_entry((slice), &io->ci_layers, cis_linkage)
56 #define cl_io_for_each_reverse(slice, io) \
57 list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
59 static inline int cl_io_type_is_valid(enum cl_io_type type)
61 return CIT_READ <= type && type < CIT_OP_NR;
64 static inline int cl_io_is_loopable(const struct cl_io *io)
66 return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
70 * Returns true iff there is an IO ongoing in the given environment.
72 int cl_io_is_going(const struct lu_env *env)
74 return cl_env_info(env)->clt_current_io != NULL;
78 * cl_io invariant that holds at all times when exported cl_io_*() functions
79 * are entered and left.
81 static int cl_io_invariant(const struct cl_io *io)
88 * io can own pages only when it is ongoing. Sub-io might
89 * still be in CIS_LOCKED state when top-io is in
92 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
93 (io->ci_state == CIS_LOCKED && up));
97 * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
99 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
101 struct cl_io_slice *slice;
102 struct cl_thread_info *info;
104 LINVRNT(cl_io_type_is_valid(io->ci_type));
105 LINVRNT(cl_io_invariant(io));
107 while (!list_empty(&io->ci_layers)) {
108 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
110 list_del_init(&slice->cis_linkage);
111 if (slice->cis_iop->op[io->ci_type].cio_fini)
112 slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
114 * Invalidate slice to catch use after free. This assumes that
115 * slices are allocated within session and can be touched
116 * after ->cio_fini() returns.
118 slice->cis_io = NULL;
120 io->ci_state = CIS_FINI;
121 info = cl_env_info(env);
122 if (info->clt_current_io == io)
123 info->clt_current_io = NULL;
125 /* sanity check for layout change */
126 switch (io->ci_type) {
133 LASSERT(!io->ci_need_restart);
137 /* Check ignore layout change conf */
138 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
139 !io->ci_need_restart));
145 EXPORT_SYMBOL(cl_io_fini);
147 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
148 enum cl_io_type iot, struct cl_object *obj)
150 struct cl_object *scan;
153 LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
154 LINVRNT(cl_io_type_is_valid(iot));
155 LINVRNT(cl_io_invariant(io));
158 INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
159 INIT_LIST_HEAD(&io->ci_lockset.cls_done);
160 INIT_LIST_HEAD(&io->ci_layers);
163 cl_object_for_each(scan, obj) {
164 if (scan->co_ops->coo_io_init) {
165 result = scan->co_ops->coo_io_init(env, scan, io);
171 io->ci_state = CIS_INIT;
176 * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
178 * \pre obj != cl_object_top(obj)
180 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
181 enum cl_io_type iot, struct cl_object *obj)
183 struct cl_thread_info *info = cl_env_info(env);
185 LASSERT(obj != cl_object_top(obj));
186 if (!info->clt_current_io)
187 info->clt_current_io = io;
188 return cl_io_init0(env, io, iot, obj);
190 EXPORT_SYMBOL(cl_io_sub_init);
193 * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
195 * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
196 * what the latter returned.
198 * \pre obj == cl_object_top(obj)
199 * \pre cl_io_type_is_valid(iot)
200 * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
202 int cl_io_init(const struct lu_env *env, struct cl_io *io,
203 enum cl_io_type iot, struct cl_object *obj)
205 struct cl_thread_info *info = cl_env_info(env);
207 LASSERT(obj == cl_object_top(obj));
208 LASSERT(!info->clt_current_io);
210 info->clt_current_io = io;
211 return cl_io_init0(env, io, iot, obj);
213 EXPORT_SYMBOL(cl_io_init);
216 * Initialize read or write io.
218 * \pre iot == CIT_READ || iot == CIT_WRITE
220 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
221 enum cl_io_type iot, loff_t pos, size_t count)
223 LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
226 LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
227 "io range: %u [%llu, %llu) %u %u\n",
228 iot, (__u64)pos, (__u64)pos + count,
229 io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
230 io->u.ci_rw.crw_pos = pos;
231 io->u.ci_rw.crw_count = count;
232 return cl_io_init(env, io, iot, io->ci_obj);
234 EXPORT_SYMBOL(cl_io_rw_init);
236 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
237 const struct cl_lock_descr *d1)
239 return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
240 lu_object_fid(&d1->cld_obj->co_lu));
244 * Sort locks in lexicographical order of their (fid, start-offset) pairs.
246 static void cl_io_locks_sort(struct cl_io *io)
250 /* hidden treasure: bubble sort for now. */
252 struct cl_io_lock_link *curr;
253 struct cl_io_lock_link *prev;
254 struct cl_io_lock_link *temp;
259 list_for_each_entry_safe(curr, temp,
260 &io->ci_lockset.cls_todo,
263 switch (cl_lock_descr_sort(&prev->cill_descr,
264 &curr->cill_descr)) {
267 * IMPOSSIBLE: Identical locks are
274 list_move_tail(&curr->cill_linkage,
275 &prev->cill_linkage);
277 continue; /* don't change prev: it's
280 case -1: /* already in order */
289 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
290 const struct cl_lock_descr *d1)
292 d0->cld_start = min(d0->cld_start, d1->cld_start);
293 d0->cld_end = max(d0->cld_end, d1->cld_end);
295 if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
296 d0->cld_mode = CLM_WRITE;
298 if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
299 d0->cld_mode = CLM_GROUP;
302 static int cl_lockset_merge(const struct cl_lockset *set,
303 const struct cl_lock_descr *need)
305 struct cl_io_lock_link *scan;
307 list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
308 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
311 /* Merge locks for the same object because ldlm lock server
312 * may expand the lock extent, otherwise there is a deadlock
313 * case if two conflicted locks are queueud for the same object
314 * and lock server expands one lock to overlap the another.
315 * The side effect is that it can generate a multi-stripe lock
316 * that may cause casacading problem
318 cl_lock_descr_merge(&scan->cill_descr, need);
319 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
320 scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
321 scan->cill_descr.cld_end);
327 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
328 struct cl_lockset *set)
330 struct cl_io_lock_link *link;
331 struct cl_io_lock_link *temp;
335 list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
336 result = cl_lock_request(env, io, &link->cill_lock);
340 list_move(&link->cill_linkage, &set->cls_done);
346 * Takes locks necessary for the current iteration of io.
348 * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
349 * by layers for the current iteration. Then sort locks (to avoid dead-locks),
352 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
354 const struct cl_io_slice *scan;
357 LINVRNT(cl_io_is_loopable(io));
358 LINVRNT(io->ci_state == CIS_IT_STARTED);
359 LINVRNT(cl_io_invariant(io));
361 cl_io_for_each(scan, io) {
362 if (!scan->cis_iop->op[io->ci_type].cio_lock)
364 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
369 cl_io_locks_sort(io);
370 result = cl_lockset_lock(env, io, &io->ci_lockset);
373 cl_io_unlock(env, io);
375 io->ci_state = CIS_LOCKED;
378 EXPORT_SYMBOL(cl_io_lock);
381 * Release locks takes by io.
383 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
385 struct cl_lockset *set;
386 struct cl_io_lock_link *link;
387 struct cl_io_lock_link *temp;
388 const struct cl_io_slice *scan;
390 LASSERT(cl_io_is_loopable(io));
391 LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
392 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);
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);
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)
411 scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
413 io->ci_state = CIS_UNLOCKED;
414 LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
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));
435 cl_io_for_each(scan, io) {
436 if (!scan->cis_iop->op[io->ci_type].cio_iter_init)
438 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
444 io->ci_state = CIS_IT_STARTED;
447 EXPORT_SYMBOL(cl_io_iter_init);
450 * Finalizes io iteration.
452 * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
454 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
456 const struct cl_io_slice *scan;
458 LINVRNT(cl_io_is_loopable(io));
459 LINVRNT(io->ci_state == CIS_UNLOCKED);
460 LINVRNT(cl_io_invariant(io));
462 cl_io_for_each_reverse(scan, io) {
463 if (scan->cis_iop->op[io->ci_type].cio_iter_fini)
464 scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
466 io->ci_state = CIS_IT_ENDED;
468 EXPORT_SYMBOL(cl_io_iter_fini);
471 * Records that read or write io progressed \a nob bytes forward.
473 static void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io,
476 const struct cl_io_slice *scan;
478 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
480 LINVRNT(cl_io_is_loopable(io));
481 LINVRNT(cl_io_invariant(io));
483 io->u.ci_rw.crw_pos += nob;
484 io->u.ci_rw.crw_count -= nob;
486 /* layers have to be notified. */
487 cl_io_for_each_reverse(scan, io) {
488 if (scan->cis_iop->op[io->ci_type].cio_advance)
489 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)
502 if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr)) {
505 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
510 EXPORT_SYMBOL(cl_io_lock_add);
512 static void cl_free_io_lock_link(const struct lu_env *env,
513 struct cl_io_lock_link *link)
519 * Allocates new lock link, and uses it to add a lock to a lockset.
521 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
522 struct cl_lock_descr *descr)
524 struct cl_io_lock_link *link;
527 link = kzalloc(sizeof(*link), GFP_NOFS);
529 link->cill_descr = *descr;
530 link->cill_fini = cl_free_io_lock_link;
531 result = cl_io_lock_add(env, io, link);
532 if (result) /* lock match */
533 link->cill_fini(env, link);
540 EXPORT_SYMBOL(cl_io_lock_alloc_add);
543 * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
545 int cl_io_start(const struct lu_env *env, struct cl_io *io)
547 const struct cl_io_slice *scan;
550 LINVRNT(cl_io_is_loopable(io));
551 LINVRNT(io->ci_state == CIS_LOCKED);
552 LINVRNT(cl_io_invariant(io));
554 io->ci_state = CIS_IO_GOING;
555 cl_io_for_each(scan, io) {
556 if (!scan->cis_iop->op[io->ci_type].cio_start)
558 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
566 EXPORT_SYMBOL(cl_io_start);
569 * Wait until current io iteration is finished by calling
570 * cl_io_operations::cio_end() bottom-to-top.
572 void cl_io_end(const struct lu_env *env, struct cl_io *io)
574 const struct cl_io_slice *scan;
576 LINVRNT(cl_io_is_loopable(io));
577 LINVRNT(io->ci_state == CIS_IO_GOING);
578 LINVRNT(cl_io_invariant(io));
580 cl_io_for_each_reverse(scan, io) {
581 if (scan->cis_iop->op[io->ci_type].cio_end)
582 scan->cis_iop->op[io->ci_type].cio_end(env, scan);
583 /* TODO: error handling. */
585 io->ci_state = CIS_IO_FINISHED;
587 EXPORT_SYMBOL(cl_io_end);
589 static const struct cl_page_slice *
590 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
592 const struct cl_page_slice *slice;
594 slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
600 * Called by read io, when page has to be read from the server.
602 * \see cl_io_operations::cio_read_page()
604 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
605 struct cl_page *page)
607 const struct cl_io_slice *scan;
608 struct cl_2queue *queue;
611 LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
612 LINVRNT(cl_page_is_owned(page, io));
613 LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
614 LINVRNT(cl_io_invariant(io));
616 queue = &io->ci_queue;
618 cl_2queue_init(queue);
620 * ->cio_read_page() methods called in the loop below are supposed to
621 * never block waiting for network (the only subtle point is the
622 * creation of new pages for read-ahead that might result in cache
623 * shrinking, but currently only clean pages are shrunk and this
624 * requires no network io).
626 * Should this ever starts blocking, retry loop would be needed for
627 * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
629 cl_io_for_each(scan, io) {
630 if (scan->cis_iop->cio_read_page) {
631 const struct cl_page_slice *slice;
633 slice = cl_io_slice_page(scan, page);
635 result = scan->cis_iop->cio_read_page(env, scan, slice);
640 if (result == 0 && queue->c2_qin.pl_nr > 0)
641 result = cl_io_submit_rw(env, io, CRT_READ, queue);
643 * Unlock unsent pages in case of error.
645 cl_page_list_disown(env, io, &queue->c2_qin);
646 cl_2queue_fini(env, queue);
649 EXPORT_SYMBOL(cl_io_read_page);
652 * Commit a list of contiguous pages into writeback cache.
654 * \returns 0 if all pages committed, or errcode if error occurred.
655 * \see cl_io_operations::cio_commit_async()
657 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
658 struct cl_page_list *queue, int from, int to,
661 const struct cl_io_slice *scan;
664 cl_io_for_each(scan, io) {
665 if (!scan->cis_iop->cio_commit_async)
667 result = scan->cis_iop->cio_commit_async(env, scan, queue,
674 EXPORT_SYMBOL(cl_io_commit_async);
677 * Submits a list of pages for immediate io.
679 * After the function gets returned, The submitted pages are moved to
680 * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
681 * to be submitted, and the pages are errant to submit.
683 * \returns 0 if at least one page was submitted, error code otherwise.
684 * \see cl_io_operations::cio_submit()
686 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
687 enum cl_req_type crt, struct cl_2queue *queue)
689 const struct cl_io_slice *scan;
692 cl_io_for_each(scan, io) {
693 if (!scan->cis_iop->cio_submit)
695 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
700 * If ->cio_submit() failed, no pages were sent.
702 LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
705 EXPORT_SYMBOL(cl_io_submit_rw);
707 static void cl_page_list_assume(const struct lu_env *env,
708 struct cl_io *io, struct cl_page_list *plist);
711 * Submit a sync_io and wait for the IO to be finished, or error happens.
712 * If \a timeout is zero, it means to wait for the IO unconditionally.
714 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
715 enum cl_req_type iot, struct cl_2queue *queue,
718 struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
722 cl_page_list_for_each(pg, &queue->c2_qin) {
723 LASSERT(!pg->cp_sync_io);
724 pg->cp_sync_io = anchor;
727 cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
728 rc = cl_io_submit_rw(env, io, iot, queue);
731 * If some pages weren't sent for any reason (e.g.,
732 * read found up-to-date pages in the cache, or write found
733 * clean pages), count them as completed to avoid infinite
736 cl_page_list_for_each(pg, &queue->c2_qin) {
737 pg->cp_sync_io = NULL;
738 cl_sync_io_note(env, anchor, 1);
741 /* wait for the IO to be finished. */
742 rc = cl_sync_io_wait(env, anchor, timeout);
743 cl_page_list_assume(env, io, &queue->c2_qout);
745 LASSERT(list_empty(&queue->c2_qout.pl_pages));
746 cl_page_list_for_each(pg, &queue->c2_qin)
747 pg->cp_sync_io = NULL;
751 EXPORT_SYMBOL(cl_io_submit_sync);
756 * Pumps io through iterations calling
758 * - cl_io_iter_init()
768 * - cl_io_iter_fini()
770 * repeatedly until there is no more io to do.
772 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
776 LINVRNT(cl_io_is_loopable(io));
782 result = cl_io_iter_init(env, io);
785 result = cl_io_lock(env, io);
788 * Notify layers that locks has been taken,
791 * - llite: kms, short read;
792 * - llite: generic_file_read();
794 result = cl_io_start(env, io);
796 * Send any remaining pending
799 * - llite: ll_rw_stats_tally.
802 cl_io_unlock(env, io);
803 cl_io_rw_advance(env, io, io->ci_nob - nob);
806 cl_io_iter_fini(env, io);
807 } while (result == 0 && io->ci_continue);
809 result = io->ci_result;
810 return result < 0 ? result : 0;
812 EXPORT_SYMBOL(cl_io_loop);
815 * Adds io slice to the cl_io.
817 * This is called by cl_object_operations::coo_io_init() methods to add a
818 * per-layer state to the io. New state is added at the end of
819 * cl_io::ci_layers list, that is, it is at the bottom of the stack.
821 * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
823 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
824 struct cl_object *obj,
825 const struct cl_io_operations *ops)
827 struct list_head *linkage = &slice->cis_linkage;
829 LASSERT((!linkage->prev && !linkage->next) ||
830 list_empty(linkage));
832 list_add_tail(linkage, &io->ci_layers);
834 slice->cis_obj = obj;
835 slice->cis_iop = ops;
837 EXPORT_SYMBOL(cl_io_slice_add);
840 * Initializes page list.
842 void cl_page_list_init(struct cl_page_list *plist)
845 INIT_LIST_HEAD(&plist->pl_pages);
846 plist->pl_owner = current;
848 EXPORT_SYMBOL(cl_page_list_init);
851 * Adds a page to a page list.
853 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
855 /* it would be better to check that page is owned by "current" io, but
856 * it is not passed here.
858 LASSERT(page->cp_owner);
859 LINVRNT(plist->pl_owner == current);
861 LASSERT(list_empty(&page->cp_batch));
862 list_add_tail(&page->cp_batch, &plist->pl_pages);
864 lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
867 EXPORT_SYMBOL(cl_page_list_add);
870 * Removes a page from a page list.
872 void cl_page_list_del(const struct lu_env *env, struct cl_page_list *plist,
873 struct cl_page *page)
875 LASSERT(plist->pl_nr > 0);
876 LASSERT(cl_page_is_vmlocked(env, page));
877 LINVRNT(plist->pl_owner == current);
879 list_del_init(&page->cp_batch);
881 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
882 cl_page_put(env, page);
884 EXPORT_SYMBOL(cl_page_list_del);
887 * Moves a page from one page list to another.
889 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
890 struct cl_page *page)
892 LASSERT(src->pl_nr > 0);
893 LINVRNT(dst->pl_owner == current);
894 LINVRNT(src->pl_owner == current);
896 list_move_tail(&page->cp_batch, &dst->pl_pages);
899 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
902 EXPORT_SYMBOL(cl_page_list_move);
905 * Moves a page from one page list to the head of another list.
907 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
908 struct cl_page *page)
910 LASSERT(src->pl_nr > 0);
911 LINVRNT(dst->pl_owner == current);
912 LINVRNT(src->pl_owner == current);
914 list_move(&page->cp_batch, &dst->pl_pages);
917 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
920 EXPORT_SYMBOL(cl_page_list_move_head);
923 * splice the cl_page_list, just as list head does
925 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
927 struct cl_page *page;
930 LINVRNT(list->pl_owner == current);
931 LINVRNT(head->pl_owner == current);
933 cl_page_list_for_each_safe(page, tmp, list)
934 cl_page_list_move(head, list, page);
936 EXPORT_SYMBOL(cl_page_list_splice);
940 * Disowns pages in a queue.
942 void cl_page_list_disown(const struct lu_env *env,
943 struct cl_io *io, struct cl_page_list *plist)
945 struct cl_page *page;
946 struct cl_page *temp;
948 LINVRNT(plist->pl_owner == current);
950 cl_page_list_for_each_safe(page, temp, plist) {
951 LASSERT(plist->pl_nr > 0);
953 list_del_init(&page->cp_batch);
956 * cl_page_disown0 rather than usual cl_page_disown() is used,
957 * because pages are possibly in CPS_FREEING state already due
958 * to the call to cl_page_list_discard().
961 * XXX cl_page_disown0() will fail if page is not locked.
963 cl_page_disown0(env, io, page);
964 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
966 cl_page_put(env, page);
969 EXPORT_SYMBOL(cl_page_list_disown);
972 * Releases pages from queue.
974 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
976 struct cl_page *page;
977 struct cl_page *temp;
979 LINVRNT(plist->pl_owner == current);
981 cl_page_list_for_each_safe(page, temp, plist)
982 cl_page_list_del(env, plist, page);
983 LASSERT(plist->pl_nr == 0);
985 EXPORT_SYMBOL(cl_page_list_fini);
988 * Assumes all pages in a queue.
990 static void cl_page_list_assume(const struct lu_env *env,
991 struct cl_io *io, struct cl_page_list *plist)
993 struct cl_page *page;
995 LINVRNT(plist->pl_owner == current);
997 cl_page_list_for_each(page, plist)
998 cl_page_assume(env, io, page);
1002 * Discards all pages in a queue.
1004 static void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1005 struct cl_page_list *plist)
1007 struct cl_page *page;
1009 LINVRNT(plist->pl_owner == current);
1010 cl_page_list_for_each(page, plist)
1011 cl_page_discard(env, io, page);
1015 * Initialize dual page queue.
1017 void cl_2queue_init(struct cl_2queue *queue)
1019 cl_page_list_init(&queue->c2_qin);
1020 cl_page_list_init(&queue->c2_qout);
1022 EXPORT_SYMBOL(cl_2queue_init);
1025 * Disown pages in both lists of a 2-queue.
1027 void cl_2queue_disown(const struct lu_env *env,
1028 struct cl_io *io, struct cl_2queue *queue)
1030 cl_page_list_disown(env, io, &queue->c2_qin);
1031 cl_page_list_disown(env, io, &queue->c2_qout);
1033 EXPORT_SYMBOL(cl_2queue_disown);
1036 * Discard (truncate) pages in both lists of a 2-queue.
1038 void cl_2queue_discard(const struct lu_env *env,
1039 struct cl_io *io, struct cl_2queue *queue)
1041 cl_page_list_discard(env, io, &queue->c2_qin);
1042 cl_page_list_discard(env, io, &queue->c2_qout);
1044 EXPORT_SYMBOL(cl_2queue_discard);
1047 * Finalize both page lists of a 2-queue.
1049 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1051 cl_page_list_fini(env, &queue->c2_qout);
1052 cl_page_list_fini(env, &queue->c2_qin);
1054 EXPORT_SYMBOL(cl_2queue_fini);
1057 * Initialize a 2-queue to contain \a page in its incoming page list.
1059 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1061 cl_2queue_init(queue);
1063 * Add a page to the incoming page list of 2-queue.
1065 cl_page_list_add(&queue->c2_qin, page);
1067 EXPORT_SYMBOL(cl_2queue_init_page);
1070 * Returns top-level io.
1072 * \see cl_object_top()
1074 struct cl_io *cl_io_top(struct cl_io *io)
1076 while (io->ci_parent)
1080 EXPORT_SYMBOL(cl_io_top);
1083 * Adds request slice to the compound request.
1085 * This is called by cl_device_operations::cdo_req_init() methods to add a
1086 * per-layer state to the request. New state is added at the end of
1087 * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1089 * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1091 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1092 struct cl_device *dev,
1093 const struct cl_req_operations *ops)
1095 list_add_tail(&slice->crs_linkage, &req->crq_layers);
1096 slice->crs_dev = dev;
1097 slice->crs_ops = ops;
1098 slice->crs_req = req;
1100 EXPORT_SYMBOL(cl_req_slice_add);
1102 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1106 LASSERT(list_empty(&req->crq_pages));
1107 LASSERT(req->crq_nrpages == 0);
1108 LINVRNT(list_empty(&req->crq_layers));
1109 LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o));
1112 for (i = 0; i < req->crq_nrobjs; ++i) {
1113 struct cl_object *obj = req->crq_o[i].ro_obj;
1116 lu_object_ref_del_at(&obj->co_lu,
1117 &req->crq_o[i].ro_obj_ref,
1119 cl_object_put(env, obj);
1127 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1128 struct cl_page *page)
1130 struct cl_device *dev;
1131 struct cl_page_slice *slice;
1135 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1136 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1137 if (dev->cd_ops->cdo_req_init) {
1138 result = dev->cd_ops->cdo_req_init(env, dev, req);
1147 * Invokes per-request transfer completion call-backs
1148 * (cl_req_operations::cro_completion()) bottom-to-top.
1150 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1152 struct cl_req_slice *slice;
1155 * for the lack of list_for_each_entry_reverse_safe()...
1157 while (!list_empty(&req->crq_layers)) {
1158 slice = list_entry(req->crq_layers.prev,
1159 struct cl_req_slice, crs_linkage);
1160 list_del_init(&slice->crs_linkage);
1161 if (slice->crs_ops->cro_completion)
1162 slice->crs_ops->cro_completion(env, slice, rc);
1164 cl_req_free(env, req);
1166 EXPORT_SYMBOL(cl_req_completion);
1169 * Allocates new transfer request.
1171 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1172 enum cl_req_type crt, int nr_objects)
1176 LINVRNT(nr_objects > 0);
1178 req = kzalloc(sizeof(*req), GFP_NOFS);
1182 req->crq_type = crt;
1183 INIT_LIST_HEAD(&req->crq_pages);
1184 INIT_LIST_HEAD(&req->crq_layers);
1186 req->crq_o = kcalloc(nr_objects, sizeof(req->crq_o[0]),
1189 req->crq_nrobjs = nr_objects;
1190 result = cl_req_init(env, req, page);
1195 cl_req_completion(env, req, result);
1196 req = ERR_PTR(result);
1199 req = ERR_PTR(-ENOMEM);
1203 EXPORT_SYMBOL(cl_req_alloc);
1206 * Adds a page to a request.
1208 void cl_req_page_add(const struct lu_env *env,
1209 struct cl_req *req, struct cl_page *page)
1211 struct cl_object *obj;
1212 struct cl_req_obj *rqo;
1215 LASSERT(list_empty(&page->cp_flight));
1216 LASSERT(!page->cp_req);
1218 CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1219 req, req->crq_type, req->crq_nrpages);
1221 list_add_tail(&page->cp_flight, &req->crq_pages);
1224 obj = cl_object_top(page->cp_obj);
1225 for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1229 lu_object_ref_add_at(&obj->co_lu, &rqo->ro_obj_ref,
1234 LASSERT(i < req->crq_nrobjs);
1236 EXPORT_SYMBOL(cl_req_page_add);
1239 * Removes a page from a request.
1241 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1243 struct cl_req *req = page->cp_req;
1245 LASSERT(!list_empty(&page->cp_flight));
1246 LASSERT(req->crq_nrpages > 0);
1248 list_del_init(&page->cp_flight);
1250 page->cp_req = NULL;
1252 EXPORT_SYMBOL(cl_req_page_done);
1255 * Notifies layers that request is about to depart by calling
1256 * cl_req_operations::cro_prep() top-to-bottom.
1258 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1262 const struct cl_req_slice *slice;
1265 * Check that the caller of cl_req_alloc() didn't lie about the number
1268 for (i = 0; i < req->crq_nrobjs; ++i)
1269 LASSERT(req->crq_o[i].ro_obj);
1272 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1273 if (slice->crs_ops->cro_prep) {
1274 result = slice->crs_ops->cro_prep(env, slice);
1281 EXPORT_SYMBOL(cl_req_prep);
1284 * Fills in attributes that are passed to server together with transfer. Only
1285 * attributes from \a flags may be touched. This can be called multiple times
1286 * for the same request.
1288 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1289 struct cl_req_attr *attr, u64 flags)
1291 const struct cl_req_slice *slice;
1292 struct cl_page *page;
1295 LASSERT(!list_empty(&req->crq_pages));
1297 /* Take any page to use as a model. */
1298 page = list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1300 for (i = 0; i < req->crq_nrobjs; ++i) {
1301 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1302 const struct cl_page_slice *scan;
1303 const struct cl_object *obj;
1305 scan = cl_page_at(page,
1306 slice->crs_dev->cd_lu_dev.ld_type);
1307 obj = scan->cpl_obj;
1308 if (slice->crs_ops->cro_attr_set)
1309 slice->crs_ops->cro_attr_set(env, slice, obj,
1314 EXPORT_SYMBOL(cl_req_attr_set);
1316 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1317 * wait for the IO to finish.
1319 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1321 wake_up_all(&anchor->csi_waitq);
1323 /* it's safe to nuke or reuse anchor now */
1324 atomic_set(&anchor->csi_barrier, 0);
1326 EXPORT_SYMBOL(cl_sync_io_end);
1329 * Initialize synchronous io wait anchor
1331 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1332 void (*end)(const struct lu_env *, struct cl_sync_io *))
1334 memset(anchor, 0, sizeof(*anchor));
1335 init_waitqueue_head(&anchor->csi_waitq);
1336 atomic_set(&anchor->csi_sync_nr, nr);
1337 atomic_set(&anchor->csi_barrier, nr > 0);
1338 anchor->csi_sync_rc = 0;
1339 anchor->csi_end_io = end;
1342 EXPORT_SYMBOL(cl_sync_io_init);
1345 * Wait until all IO completes. Transfer completion routine has to call
1346 * cl_sync_io_note() for every entity.
1348 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1351 struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1355 LASSERT(timeout >= 0);
1357 rc = l_wait_event(anchor->csi_waitq,
1358 atomic_read(&anchor->csi_sync_nr) == 0,
1361 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1362 rc, atomic_read(&anchor->csi_sync_nr));
1364 lwi = (struct l_wait_info) { 0 };
1365 (void)l_wait_event(anchor->csi_waitq,
1366 atomic_read(&anchor->csi_sync_nr) == 0,
1369 rc = anchor->csi_sync_rc;
1371 LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1373 /* wait until cl_sync_io_note() has done wakeup */
1374 while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1380 EXPORT_SYMBOL(cl_sync_io_wait);
1383 * Indicate that transfer of a single page completed.
1385 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1388 if (anchor->csi_sync_rc == 0 && ioret < 0)
1389 anchor->csi_sync_rc = ioret;
1391 * Synchronous IO done without releasing page lock (e.g., as a part of
1392 * ->{prepare,commit}_write(). Completion is used to signal the end of
1395 LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1396 if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1397 LASSERT(anchor->csi_end_io);
1398 anchor->csi_end_io(env, anchor);
1399 /* Can't access anchor any more */
1402 EXPORT_SYMBOL(cl_sync_io_note);