Whamcloud - gitweb
Land first part of new dcache handling (bug 16654).
[fs/lustre-release.git] / lustre / obdclass / cl_io.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Client IO.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42 #ifndef EXPORT_SYMTAB
43 # define EXPORT_SYMTAB
44 #endif
45
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <libcfs/list.h>
50 /* lu_time_global_{init,fini}() */
51 #include <lu_time.h>
52
53 #include <cl_object.h>
54 #include "cl_internal.h"
55
56 /*****************************************************************************
57  *
58  * cl_io interface.
59  *
60  */
61
62 #define cl_io_for_each(slice, io) \
63         list_for_each_entry((slice), &io->ci_layers, cis_linkage)
64 #define cl_io_for_each_reverse(slice, io)                 \
65         list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
66
67 static inline int cl_io_type_is_valid(enum cl_io_type type)
68 {
69         return CIT_READ <= type && type < CIT_OP_NR;
70 }
71
72 static inline int cl_io_is_loopable(const struct cl_io *io)
73 {
74         return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
75 }
76
77 /**
78  * True, iff \a io is a sendfile().
79  */
80 int cl_io_is_sendfile(const struct cl_io *io)
81 {
82         return io->ci_type == CIT_READ && io->u.ci_rd.rd_is_sendfile;
83 }
84 EXPORT_SYMBOL(cl_io_is_sendfile);
85
86 /**
87  * Returns true iff there is an IO ongoing in the given environment.
88  */
89 int cl_io_is_going(const struct lu_env *env)
90 {
91         return cl_env_info(env)->clt_current_io != NULL;
92 }
93 EXPORT_SYMBOL(cl_io_is_going);
94
95 /**
96  * cl_io invariant that holds at all times when exported cl_io_*() functions
97  * are entered and left.
98  */
99 static int cl_io_invariant(const struct cl_io *io)
100 {
101         struct cl_io *up;
102
103         up = io->ci_parent;
104         return
105                 /*
106                  * io can own pages only when it is ongoing. Sub-io might
107                  * still be in CIS_LOCKED state when top-io is in
108                  * CIS_IO_GOING.
109                  */
110                 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
111                      (io->ci_state == CIS_LOCKED && up != NULL));
112 }
113
114 /**
115  * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
116  */
117 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
118 {
119         struct cl_io_slice    *slice;
120         struct cl_thread_info *info;
121
122         LINVRNT(cl_io_type_is_valid(io->ci_type));
123         LINVRNT(cl_io_invariant(io));
124         ENTRY;
125
126         while (!list_empty(&io->ci_layers)) {
127                 slice = container_of(io->ci_layers.next, struct cl_io_slice,
128                                      cis_linkage);
129                 list_del_init(&slice->cis_linkage);
130                 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
131                         slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
132                 /*
133                  * Invalidate slice to catch use after free. This assumes that
134                  * slices are allocated within session and can be touched
135                  * after ->cio_fini() returns.
136                  */
137                 slice->cis_io = NULL;
138         }
139         io->ci_state = CIS_FINI;
140         info = cl_env_info(env);
141         if (info->clt_current_io == io)
142                 info->clt_current_io = NULL;
143         EXIT;
144 }
145 EXPORT_SYMBOL(cl_io_fini);
146
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)
149 {
150         struct cl_object *scan;
151         int result;
152
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));
156         ENTRY;
157
158         io->ci_type = iot;
159         CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
160         CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
161         CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_done);
162         CFS_INIT_LIST_HEAD(&io->ci_layers);
163
164         result = 0;
165         cl_object_for_each(scan, obj) {
166                 if (scan->co_ops->coo_io_init != NULL) {
167                         result = scan->co_ops->coo_io_init(env, scan, io);
168                         if (result != 0)
169                                 break;
170                 }
171         }
172         if (result == 0)
173                 io->ci_state = CIS_INIT;
174         RETURN(result);
175 }
176
177 /**
178  * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
179  *
180  * \pre obj != cl_object_top(obj)
181  */
182 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
183                    enum cl_io_type iot, struct cl_object *obj)
184 {
185         struct cl_thread_info *info = cl_env_info(env);
186
187         LASSERT(obj != cl_object_top(obj));
188         if (info->clt_current_io == NULL)
189                 info->clt_current_io = io;
190         return cl_io_init0(env, io, iot, obj);
191 }
192 EXPORT_SYMBOL(cl_io_sub_init);
193
194 /**
195  * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
196  *
197  * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
198  * what the latter returned.
199  *
200  * \pre obj == cl_object_top(obj)
201  * \pre cl_io_type_is_valid(iot)
202  * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
203  */
204 int cl_io_init(const struct lu_env *env, struct cl_io *io,
205                enum cl_io_type iot, struct cl_object *obj)
206 {
207         struct cl_thread_info *info = cl_env_info(env);
208
209         LASSERT(obj == cl_object_top(obj));
210         LASSERT(info->clt_current_io == NULL);
211
212         info->clt_current_io = io;
213         return cl_io_init0(env, io, iot, obj);
214 }
215 EXPORT_SYMBOL(cl_io_init);
216
217 /**
218  * Initialize read or write io.
219  *
220  * \pre iot == CIT_READ || iot == CIT_WRITE
221  */
222 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
223                   enum cl_io_type iot, loff_t pos, size_t count)
224 {
225         LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
226         LINVRNT(io->ci_obj != NULL);
227         ENTRY;
228
229         LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
230                          "io range: %i [%llu, %llu) %i %i\n",
231                          iot, (__u64)pos, (__u64)(pos + count),
232                          io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
233         io->u.ci_rw.crw_pos    = pos;
234         io->u.ci_rw.crw_count  = count;
235         RETURN(cl_io_init(env, io, iot, io->ci_obj));
236 }
237 EXPORT_SYMBOL(cl_io_rw_init);
238
239 static inline const struct lu_fid *
240 cl_lock_descr_fid(const struct cl_lock_descr *descr)
241 {
242         return lu_object_fid(&descr->cld_obj->co_lu);
243 }
244
245 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
246                              const struct cl_lock_descr *d1)
247 {
248         return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
249                 __diff_normalize(d0->cld_start, d1->cld_start);
250 }
251
252 /*
253  * Sort locks in lexicographical order of their (fid, start-offset) pairs.
254  */
255 static void cl_io_locks_sort(struct cl_io *io)
256 {
257         int done = 0;
258
259         ENTRY;
260         /* hidden treasure: bubble sort for now. */
261         do {
262                 struct cl_io_lock_link *curr;
263                 struct cl_io_lock_link *prev;
264                 struct cl_io_lock_link *temp;
265
266                 done = 1;
267                 prev = NULL;
268
269                 list_for_each_entry_safe(curr, temp, &io->ci_lockset.cls_todo,
270                                          cill_linkage) {
271                         if (prev != NULL) {
272                                 switch (cl_lock_descr_cmp(&prev->cill_descr,
273                                                           &curr->cill_descr)) {
274                                 case 0:
275                                         /*
276                                          * IMPOSSIBLE: Identical locks are
277                                          *             already removed at
278                                          *             this point.
279                                          */
280                                 default:
281                                         LBUG();
282                                 case +1:
283                                         list_move_tail(&curr->cill_linkage,
284                                                        &prev->cill_linkage);
285                                         done = 0;
286                                         continue; /* don't change prev: it's
287                                                    * still "previous" */
288                                 case -1: /* already in order */
289                                         break;
290                                 }
291                         }
292                         prev = curr;
293                 }
294         } while (!done);
295         EXIT;
296 }
297
298 /**
299  * Check whether \a queue contains locks matching \a need.
300  *
301  * \retval +ve there is a matching lock in the \a queue
302  * \retval   0 there are no matching locks in the \a queue
303  */
304 int cl_queue_match(const struct list_head *queue,
305                    const struct cl_lock_descr *need)
306 {
307        struct cl_io_lock_link *scan;
308
309        ENTRY;
310        list_for_each_entry(scan, queue, cill_linkage) {
311                if (cl_lock_descr_match(&scan->cill_descr, need))
312                        RETURN(+1);
313        }
314        return 0;
315 }
316 EXPORT_SYMBOL(cl_queue_match);
317
318 static int cl_lockset_match(const struct cl_lockset *set,
319                             const struct cl_lock_descr *need, int all_queues)
320 {
321         return (all_queues ? cl_queue_match(&set->cls_todo, need) : 0) ||
322                 cl_queue_match(&set->cls_curr, need) ||
323                 cl_queue_match(&set->cls_done, need);
324 }
325
326 static int cl_lockset_lock_one(const struct lu_env *env,
327                                struct cl_io *io, struct cl_lockset *set,
328                                struct cl_io_lock_link *link)
329 {
330         struct cl_lock *lock;
331         int             result;
332
333         ENTRY;
334
335         lock = cl_lock_request(env, io, &link->cill_descr, link->cill_enq_flags,
336                                "io", io);
337         if (!IS_ERR(lock)) {
338                 link->cill_lock = lock;
339                 list_move(&link->cill_linkage, &set->cls_curr);
340                 if (!(link->cill_enq_flags & CEF_ASYNC)) {
341                         result = cl_wait(env, lock);
342                         if (result == 0)
343                                 list_move(&link->cill_linkage, &set->cls_done);
344                 } else
345                         result = 0;
346         } else
347                 result = PTR_ERR(lock);
348         RETURN(result);
349 }
350
351 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
352                               struct cl_io_lock_link *link)
353 {
354         struct cl_lock *lock = link->cill_lock;
355
356         ENTRY;
357         list_del_init(&link->cill_linkage);
358         if (lock != NULL) {
359                 cl_lock_release(env, lock, "io", io);
360                 link->cill_lock = NULL;
361         }
362         if (link->cill_fini != NULL)
363                 link->cill_fini(env, link);
364         EXIT;
365 }
366
367 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
368                            struct cl_lockset *set)
369 {
370         struct cl_io_lock_link *link;
371         struct cl_io_lock_link *temp;
372         struct cl_lock         *lock;
373         int result;
374
375         ENTRY;
376         result = 0;
377         list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
378                 if (!cl_lockset_match(set, &link->cill_descr, 0)) {
379                         /* XXX some locking to guarantee that locks aren't
380                          * expanded in between. */
381                         result = cl_lockset_lock_one(env, io, set, link);
382                         if (result != 0)
383                                 break;
384                 } else
385                         cl_lock_link_fini(env, io, link);
386         }
387         if (result == 0) {
388                 list_for_each_entry_safe(link, temp,
389                                          &set->cls_curr, cill_linkage) {
390                         lock = link->cill_lock;
391                         result = cl_wait(env, lock);
392                         if (result == 0)
393                                 list_move(&link->cill_linkage, &set->cls_done);
394                         else
395                                 break;
396                 }
397         }
398         RETURN(result);
399 }
400
401 /**
402  * Takes locks necessary for the current iteration of io.
403  *
404  * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
405  * by layers for the current iteration. Then sort locks (to avoid dead-locks),
406  * and acquire them.
407  */
408 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
409 {
410         const struct cl_io_slice *scan;
411         int result = 0;
412
413         LINVRNT(cl_io_is_loopable(io));
414         LINVRNT(io->ci_state == CIS_IT_STARTED);
415         LINVRNT(cl_io_invariant(io));
416
417         ENTRY;
418         cl_io_for_each(scan, io) {
419                 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
420                         continue;
421                 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
422                 if (result != 0)
423                         break;
424         }
425         if (result == 0) {
426                 cl_io_locks_sort(io);
427                 result = cl_lockset_lock(env, io, &io->ci_lockset);
428         }
429         if (result != 0)
430                 cl_io_unlock(env, io);
431         else
432                 io->ci_state = CIS_LOCKED;
433         RETURN(result);
434 }
435 EXPORT_SYMBOL(cl_io_lock);
436
437 /**
438  * Release locks takes by io.
439  */
440 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
441 {
442         struct cl_lockset        *set;
443         struct cl_io_lock_link   *link;
444         struct cl_io_lock_link   *temp;
445         const struct cl_io_slice *scan;
446
447         LASSERT(cl_io_is_loopable(io));
448         LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
449         LINVRNT(cl_io_invariant(io));
450
451         ENTRY;
452         set = &io->ci_lockset;
453
454         list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
455                 cl_lock_link_fini(env, io, link);
456
457         list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
458                 cl_lock_link_fini(env, io, link);
459
460         list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
461                 cl_unuse(env, link->cill_lock);
462                 cl_lock_link_fini(env, io, link);
463         }
464         cl_io_for_each_reverse(scan, io) {
465                 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
466                         scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
467         }
468         io->ci_state = CIS_UNLOCKED;
469         LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
470         EXIT;
471 }
472 EXPORT_SYMBOL(cl_io_unlock);
473
474 /**
475  * Prepares next iteration of io.
476  *
477  * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
478  * layers a chance to modify io parameters, e.g., so that lov can restrict io
479  * to a single stripe.
480  */
481 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
482 {
483         const struct cl_io_slice *scan;
484         int result;
485
486         LINVRNT(cl_io_is_loopable(io));
487         LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
488         LINVRNT(cl_io_invariant(io));
489
490         ENTRY;
491         result = 0;
492         cl_io_for_each(scan, io) {
493                 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
494                         continue;
495                 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
496                                                                       scan);
497                 if (result != 0)
498                         break;
499         }
500         if (result == 0)
501                 io->ci_state = CIS_IT_STARTED;
502         RETURN(result);
503 }
504 EXPORT_SYMBOL(cl_io_iter_init);
505
506 /**
507  * Finalizes io iteration.
508  *
509  * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
510  */
511 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
512 {
513         const struct cl_io_slice *scan;
514
515         LINVRNT(cl_io_is_loopable(io));
516         LINVRNT(io->ci_state == CIS_UNLOCKED);
517         LINVRNT(cl_io_invariant(io));
518
519         ENTRY;
520         cl_io_for_each_reverse(scan, io) {
521                 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
522                         scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
523         }
524         io->ci_state = CIS_IT_ENDED;
525         EXIT;
526 }
527 EXPORT_SYMBOL(cl_io_iter_fini);
528
529 /**
530  * Records that read or write io progressed \a nob bytes forward.
531  */
532 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
533 {
534         const struct cl_io_slice *scan;
535
536         LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
537                 nob == 0);
538         LINVRNT(cl_io_is_loopable(io));
539         LINVRNT(cl_io_invariant(io));
540
541         ENTRY;
542
543         io->u.ci_rw.crw_pos   += nob;
544         io->u.ci_rw.crw_count -= nob;
545
546         /* layers have to be notified. */
547         cl_io_for_each_reverse(scan, io) {
548                 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
549                         scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
550                                                                    nob);
551         }
552         EXIT;
553 }
554 EXPORT_SYMBOL(cl_io_rw_advance);
555
556 /**
557  * Adds a lock to a lockset.
558  */
559 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
560                    struct cl_io_lock_link *link)
561 {
562         int result;
563
564         ENTRY;
565         if (cl_lockset_match(&io->ci_lockset, &link->cill_descr, 1))
566                 result = +1;
567         else {
568                 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
569                 result = 0;
570         }
571         RETURN(result);
572 }
573 EXPORT_SYMBOL(cl_io_lock_add);
574
575 static void cl_free_io_lock_link(const struct lu_env *env,
576                                  struct cl_io_lock_link *link)
577 {
578         OBD_FREE_PTR(link);
579 }
580
581 /**
582  * Allocates new lock link, and uses it to add a lock to a lockset.
583  */
584 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
585                          struct cl_lock_descr *descr, int enqflags)
586 {
587         struct cl_io_lock_link *link;
588         int result;
589
590         ENTRY;
591         OBD_ALLOC_PTR(link);
592         if (link != NULL) {
593                 link->cill_descr     = *descr;
594                 link->cill_enq_flags = enqflags;
595                 link->cill_fini      = cl_free_io_lock_link;
596                 result = cl_io_lock_add(env, io, link);
597                 if (result) /* lock match */
598                         link->cill_fini(env, link);
599         } else
600                 result = -ENOMEM;
601
602         RETURN(result);
603 }
604 EXPORT_SYMBOL(cl_io_lock_alloc_add);
605
606 /**
607  * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
608  */
609 int cl_io_start(const struct lu_env *env, struct cl_io *io)
610 {
611         const struct cl_io_slice *scan;
612         int result = 0;
613
614         LINVRNT(cl_io_is_loopable(io));
615         LINVRNT(io->ci_state == CIS_LOCKED);
616         LINVRNT(cl_io_invariant(io));
617         ENTRY;
618
619         io->ci_state = CIS_IO_GOING;
620         cl_io_for_each(scan, io) {
621                 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
622                         continue;
623                 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
624                 if (result != 0)
625                         break;
626         }
627         if (result >= 0)
628                 result = 0;
629         RETURN(result);
630 }
631 EXPORT_SYMBOL(cl_io_start);
632
633 /**
634  * Wait until current io iteration is finished by calling
635  * cl_io_operations::cio_end() bottom-to-top.
636  */
637 void cl_io_end(const struct lu_env *env, struct cl_io *io)
638 {
639         const struct cl_io_slice *scan;
640
641         LINVRNT(cl_io_is_loopable(io));
642         LINVRNT(io->ci_state == CIS_IO_GOING);
643         LINVRNT(cl_io_invariant(io));
644         ENTRY;
645
646         cl_io_for_each_reverse(scan, io) {
647                 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
648                         scan->cis_iop->op[io->ci_type].cio_end(env, scan);
649                 /* TODO: error handling. */
650         }
651         io->ci_state = CIS_IO_FINISHED;
652         EXIT;
653 }
654 EXPORT_SYMBOL(cl_io_end);
655
656 static const struct cl_page_slice *
657 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
658 {
659         const struct cl_page_slice *slice;
660
661         slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
662         LINVRNT(slice != NULL);
663         return slice;
664 }
665
666 /**
667  * True iff \a page is within \a io range.
668  */
669 static int cl_page_in_io(const struct cl_page *page, const struct cl_io *io)
670 {
671         int     result;
672         loff_t  start;
673         loff_t  end;
674         pgoff_t idx;
675
676         idx = page->cp_index;
677         switch (io->ci_type) {
678         case CIT_READ:
679         case CIT_WRITE:
680                 /*
681                  * check that [start, end) and [pos, pos + count) extents
682                  * overlap.
683                  */
684                 start = cl_offset(page->cp_obj, idx);
685                 end   = cl_offset(page->cp_obj, idx + 1);
686                 result = io->u.ci_rw.crw_pos < end &&
687                         start < io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
688                 break;
689         case CIT_FAULT:
690                 result = io->u.ci_fault.ft_index == idx;
691                 break;
692         default:
693                 LBUG();
694         }
695         return result;
696 }
697
698 /**
699  * Called by read io, when page has to be read from the server.
700  *
701  * \see cl_io_operations::cio_read_page()
702  */
703 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
704                     struct cl_page *page)
705 {
706         const struct cl_io_slice *scan;
707         struct cl_2queue         *queue;
708         int                       result = 0;
709
710         LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
711         LINVRNT(cl_page_is_owned(page, io));
712         LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
713         LINVRNT(cl_page_in_io(page, io));
714         LINVRNT(cl_io_invariant(io));
715         ENTRY;
716
717         queue = &io->ci_queue;
718
719         cl_2queue_init(queue);
720         /*
721          * ->cio_read_page() methods called in the loop below are supposed to
722          * never block waiting for network (the only subtle point is the
723          * creation of new pages for read-ahead that might result in cache
724          * shrinking, but currently only clean pages are shrunk and this
725          * requires no network io).
726          *
727          * Should this ever starts blocking, retry loop would be needed for
728          * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
729          */
730         cl_io_for_each(scan, io) {
731                 if (scan->cis_iop->cio_read_page != NULL) {
732                         const struct cl_page_slice *slice;
733
734                         slice = cl_io_slice_page(scan, page);
735                         LINVRNT(slice != NULL);
736                         result = scan->cis_iop->cio_read_page(env, scan, slice);
737                         if (result != 0)
738                                 break;
739                 }
740         }
741         if (result == 0)
742                 result = cl_io_submit_rw(env, io, CRT_READ, queue, CRP_NORMAL);
743         /*
744          * Unlock unsent pages in case of error.
745          */
746         cl_page_list_disown(env, io, &queue->c2_qin);
747         cl_2queue_fini(env, queue);
748         RETURN(result);
749 }
750 EXPORT_SYMBOL(cl_io_read_page);
751
752 /**
753  * Called by write io to prepare page to receive data from user buffer.
754  *
755  * \see cl_io_operations::cio_prepare_write()
756  */
757 int cl_io_prepare_write(const struct lu_env *env, struct cl_io *io,
758                         struct cl_page *page, unsigned from, unsigned to)
759 {
760         const struct cl_io_slice *scan;
761         int result = 0;
762
763         LINVRNT(io->ci_type == CIT_WRITE);
764         LINVRNT(cl_page_is_owned(page, io));
765         LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
766         LINVRNT(cl_io_invariant(io));
767         LASSERT(cl_page_in_io(page, io));
768         ENTRY;
769
770         cl_io_for_each_reverse(scan, io) {
771                 if (scan->cis_iop->cio_prepare_write != NULL) {
772                         const struct cl_page_slice *slice;
773
774                         slice = cl_io_slice_page(scan, page);
775                         result = scan->cis_iop->cio_prepare_write(env, scan,
776                                                                   slice,
777                                                                   from, to);
778                         if (result != 0)
779                                 break;
780                 }
781         }
782         RETURN(result);
783 }
784 EXPORT_SYMBOL(cl_io_prepare_write);
785
786 /**
787  * Called by write io after user data were copied into a page.
788  *
789  * \see cl_io_operations::cio_commit_write()
790  */
791 int cl_io_commit_write(const struct lu_env *env, struct cl_io *io,
792                        struct cl_page *page, unsigned from, unsigned to)
793 {
794         const struct cl_io_slice *scan;
795         int result = 0;
796
797         LINVRNT(io->ci_type == CIT_WRITE);
798         LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
799         LINVRNT(cl_io_invariant(io));
800         /*
801          * XXX Uh... not nice. Top level cl_io_commit_write() call (vvp->lov)
802          * already called cl_page_cache_add(), moving page into CPS_CACHED
803          * state. Better (and more general) way of dealing with such situation
804          * is needed.
805          */
806         LASSERT(cl_page_is_owned(page, io) || page->cp_parent != NULL);
807         LASSERT(cl_page_in_io(page, io));
808         ENTRY;
809
810         cl_io_for_each(scan, io) {
811                 if (scan->cis_iop->cio_commit_write != NULL) {
812                         const struct cl_page_slice *slice;
813
814                         slice = cl_io_slice_page(scan, page);
815                         result = scan->cis_iop->cio_commit_write(env, scan,
816                                                                  slice,
817                                                                  from, to);
818                         if (result != 0)
819                                 break;
820                 }
821         }
822         LINVRNT(result <= 0);
823         RETURN(result);
824 }
825 EXPORT_SYMBOL(cl_io_commit_write);
826
827 /**
828  * Submits a list of pages for immediate io.
829  *
830  * After the function gets returned, The submitted pages are moved to
831  * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
832  * to be submitted, and the pages are errant to submit.
833  *
834  * \returns 0 if at least one page was submitted, error code otherwise.
835  * \see cl_io_operations::cio_submit()
836  */
837 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
838                     enum cl_req_type crt, struct cl_2queue *queue,
839                     enum cl_req_priority priority)
840 {
841         const struct cl_io_slice *scan;
842         int result = 0;
843
844         LINVRNT(crt < ARRAY_SIZE(scan->cis_iop->req_op));
845         ENTRY;
846
847         cl_io_for_each(scan, io) {
848                 if (scan->cis_iop->req_op[crt].cio_submit == NULL)
849                         continue;
850                 result = scan->cis_iop->req_op[crt].cio_submit(env, scan, crt,
851                                                                queue, priority);
852                 if (result != 0)
853                         break;
854         }
855         /*
856          * If ->cio_submit() failed, no pages were sent.
857          */
858         LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
859         RETURN(result);
860 }
861 EXPORT_SYMBOL(cl_io_submit_rw);
862
863 /**
864  * Cancel an IO which has been submitted by cl_io_submit_rw.
865  */
866 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
867                  struct cl_page_list *queue)
868 {
869         struct cl_page *page;
870         int result = 0;
871
872         CERROR("Canceling ongoing page trasmission\n");
873         cl_page_list_for_each(page, queue) {
874                 int rc;
875
876                 LINVRNT(cl_page_in_io(page, io));
877                 rc = cl_page_cancel(env, page);
878                 result = result ?: rc;
879         }
880         return result;
881 }
882 EXPORT_SYMBOL(cl_io_cancel);
883
884 /**
885  * Main io loop.
886  *
887  * Pumps io through iterations calling
888  *
889  *    - cl_io_iter_init()
890  *
891  *    - cl_io_lock()
892  *
893  *    - cl_io_start()
894  *
895  *    - cl_io_end()
896  *
897  *    - cl_io_unlock()
898  *
899  *    - cl_io_iter_fini()
900  *
901  * repeatedly until there is no more io to do.
902  */
903 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
904 {
905         int result   = 0;
906
907         LINVRNT(cl_io_is_loopable(io));
908         ENTRY;
909
910         do {
911                 size_t nob;
912
913                 io->ci_continue = 0;
914                 result = cl_io_iter_init(env, io);
915                 if (result == 0) {
916                         nob    = io->ci_nob;
917                         result = cl_io_lock(env, io);
918                         if (result == 0) {
919                                 /*
920                                  * Notify layers that locks has been taken,
921                                  * and do actual i/o.
922                                  *
923                                  *   - llite: kms, short read;
924                                  *   - llite: generic_file_read();
925                                  */
926                                 result = cl_io_start(env, io);
927                                 /*
928                                  * Send any remaining pending
929                                  * io, etc.
930                                  *
931                                  *   - llite: ll_rw_stats_tally.
932                                  */
933                                 cl_io_end(env, io);
934                                 cl_io_unlock(env, io);
935                                 cl_io_rw_advance(env, io, io->ci_nob - nob);
936                         }
937                 }
938                 cl_io_iter_fini(env, io);
939         } while (result == 0 && io->ci_continue);
940         RETURN(result < 0 ? result : 0);
941 }
942 EXPORT_SYMBOL(cl_io_loop);
943
944 /**
945  * Adds io slice to the cl_io.
946  *
947  * This is called by cl_object_operations::coo_io_init() methods to add a
948  * per-layer state to the io. New state is added at the end of
949  * cl_io::ci_layers list, that is, it is at the bottom of the stack.
950  *
951  * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
952  */
953 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
954                      struct cl_object *obj,
955                      const struct cl_io_operations *ops)
956 {
957         struct list_head *linkage = &slice->cis_linkage;
958
959         LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
960                 list_empty(linkage));
961         ENTRY;
962
963         list_add_tail(linkage, &io->ci_layers);
964         slice->cis_io  = io;
965         slice->cis_obj = obj;
966         slice->cis_iop = ops;
967         EXIT;
968 }
969 EXPORT_SYMBOL(cl_io_slice_add);
970
971
972 /**
973  * Initializes page list.
974  */
975 void cl_page_list_init(struct cl_page_list *plist)
976 {
977         ENTRY;
978         plist->pl_nr = 0;
979         CFS_INIT_LIST_HEAD(&plist->pl_pages);
980         plist->pl_owner = cfs_current();
981         EXIT;
982 }
983 EXPORT_SYMBOL(cl_page_list_init);
984
985 /**
986  * Adds a page to a page list.
987  */
988 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
989 {
990         ENTRY;
991         /* it would be better to check that page is owned by "current" io, but
992          * it is not passed here. */
993         LASSERT(page->cp_owner != NULL);
994         LINVRNT(plist->pl_owner == cfs_current());
995
996         lockdep_off();
997         mutex_lock(&page->cp_mutex);
998         lockdep_on();
999         LASSERT(list_empty(&page->cp_batch));
1000         list_add_tail(&page->cp_batch, &plist->pl_pages);
1001         ++plist->pl_nr;
1002         page->cp_queue_ref = lu_ref_add(&page->cp_reference, "queue", plist);
1003         cl_page_get(page);
1004         EXIT;
1005 }
1006 EXPORT_SYMBOL(cl_page_list_add);
1007
1008 /**
1009  * Removes a page from a page list.
1010  */
1011 void cl_page_list_del(const struct lu_env *env,
1012                       struct cl_page_list *plist, struct cl_page *page)
1013 {
1014         LASSERT(plist->pl_nr > 0);
1015         LINVRNT(plist->pl_owner == cfs_current());
1016
1017         ENTRY;
1018         list_del_init(&page->cp_batch);
1019         lockdep_off();
1020         mutex_unlock(&page->cp_mutex);
1021         lockdep_on();
1022         --plist->pl_nr;
1023         lu_ref_del_at(&page->cp_reference, page->cp_queue_ref, "queue", plist);
1024         cl_page_put(env, page);
1025         EXIT;
1026 }
1027 EXPORT_SYMBOL(cl_page_list_del);
1028
1029 /**
1030  * Moves a page from one page list to another.
1031  */
1032 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1033                        struct cl_page *page)
1034 {
1035         LASSERT(src->pl_nr > 0);
1036         LINVRNT(dst->pl_owner == cfs_current());
1037         LINVRNT(src->pl_owner == cfs_current());
1038
1039         ENTRY;
1040         list_move_tail(&page->cp_batch, &dst->pl_pages);
1041         --src->pl_nr;
1042         ++dst->pl_nr;
1043         lu_ref_set_at(&page->cp_reference,
1044                       page->cp_queue_ref, "queue", src, dst);
1045         EXIT;
1046 }
1047 EXPORT_SYMBOL(cl_page_list_move);
1048
1049 /**
1050  * splice the cl_page_list, just as list head does
1051  */
1052 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1053 {
1054         struct cl_page *page;
1055         struct cl_page *tmp;
1056
1057         LINVRNT(list->pl_owner == cfs_current());
1058         LINVRNT(head->pl_owner == cfs_current());
1059
1060         ENTRY;
1061         cl_page_list_for_each_safe(page, tmp, list)
1062                 cl_page_list_move(head, list, page);
1063         EXIT;
1064 }
1065 EXPORT_SYMBOL(cl_page_list_splice);
1066
1067 void cl_page_disown0(const struct lu_env *env,
1068                      struct cl_io *io, struct cl_page *pg);
1069
1070 /**
1071  * Disowns pages in a queue.
1072  */
1073 void cl_page_list_disown(const struct lu_env *env,
1074                          struct cl_io *io, struct cl_page_list *plist)
1075 {
1076         struct cl_page *page;
1077         struct cl_page *temp;
1078
1079         LINVRNT(plist->pl_owner == cfs_current());
1080
1081         ENTRY;
1082         cl_page_list_for_each_safe(page, temp, plist) {
1083                 LASSERT(plist->pl_nr > 0);
1084
1085                 list_del_init(&page->cp_batch);
1086                 lockdep_off();
1087                 mutex_unlock(&page->cp_mutex);
1088                 lockdep_on();
1089                 --plist->pl_nr;
1090                 /*
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().
1094                  */
1095                 /*
1096                  * XXX cl_page_disown0() will fail if page is not locked.
1097                  */
1098                 cl_page_disown0(env, io, page);
1099                 lu_ref_del(&page->cp_reference, "queue", plist);
1100                 cl_page_put(env, page);
1101         }
1102         EXIT;
1103 }
1104 EXPORT_SYMBOL(cl_page_list_disown);
1105
1106 /**
1107  * Releases pages from queue.
1108  */
1109 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1110 {
1111         struct cl_page *page;
1112         struct cl_page *temp;
1113
1114         LINVRNT(plist->pl_owner == cfs_current());
1115
1116         ENTRY;
1117         cl_page_list_for_each_safe(page, temp, plist)
1118                 cl_page_list_del(env, plist, page);
1119         LASSERT(plist->pl_nr == 0);
1120         EXIT;
1121 }
1122 EXPORT_SYMBOL(cl_page_list_fini);
1123
1124 /**
1125  * Owns all pages in a queue.
1126  */
1127 int cl_page_list_own(const struct lu_env *env,
1128                      struct cl_io *io, struct cl_page_list *plist)
1129 {
1130         struct cl_page *page;
1131         struct cl_page *temp;
1132         pgoff_t index = 0;
1133         int result;
1134
1135         LINVRNT(plist->pl_owner == cfs_current());
1136
1137         ENTRY;
1138         result = 0;
1139         cl_page_list_for_each_safe(page, temp, plist) {
1140                 LASSERT(index <= page->cp_index);
1141                 index = page->cp_index;
1142                 if (cl_page_own(env, io, page) == 0)
1143                         result = result ?: page->cp_error;
1144                 else
1145                         cl_page_list_del(env, plist, page);
1146         }
1147         RETURN(result);
1148 }
1149 EXPORT_SYMBOL(cl_page_list_own);
1150
1151 /**
1152  * Assumes all pages in a queue.
1153  */
1154 void cl_page_list_assume(const struct lu_env *env,
1155                          struct cl_io *io, struct cl_page_list *plist)
1156 {
1157         struct cl_page *page;
1158
1159         LINVRNT(plist->pl_owner == cfs_current());
1160
1161         cl_page_list_for_each(page, plist)
1162                 cl_page_assume(env, io, page);
1163 }
1164 EXPORT_SYMBOL(cl_page_list_assume);
1165
1166 /**
1167  * Discards all pages in a queue.
1168  */
1169 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1170                           struct cl_page_list *plist)
1171 {
1172         struct cl_page *page;
1173
1174         LINVRNT(plist->pl_owner == cfs_current());
1175         ENTRY;
1176         cl_page_list_for_each(page, plist)
1177                 cl_page_discard(env, io, page);
1178         EXIT;
1179 }
1180 EXPORT_SYMBOL(cl_page_list_discard);
1181
1182 /**
1183  * Unmaps all pages in a queue from user virtual memory.
1184  */
1185 int cl_page_list_unmap(const struct lu_env *env, struct cl_io *io,
1186                         struct cl_page_list *plist)
1187 {
1188         struct cl_page *page;
1189         int result;
1190
1191         LINVRNT(plist->pl_owner == cfs_current());
1192         ENTRY;
1193         result = 0;
1194         cl_page_list_for_each(page, plist) {
1195                 result = cl_page_unmap(env, io, page);
1196                 if (result != 0)
1197                         break;
1198         }
1199         RETURN(result);
1200 }
1201 EXPORT_SYMBOL(cl_page_list_unmap);
1202
1203 /**
1204  * Initialize dual page queue.
1205  */
1206 void cl_2queue_init(struct cl_2queue *queue)
1207 {
1208         ENTRY;
1209         cl_page_list_init(&queue->c2_qin);
1210         cl_page_list_init(&queue->c2_qout);
1211         EXIT;
1212 }
1213 EXPORT_SYMBOL(cl_2queue_init);
1214
1215 /**
1216  * Add a page to the incoming page list of 2-queue.
1217  */
1218 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1219 {
1220         ENTRY;
1221         cl_page_list_add(&queue->c2_qin, page);
1222         EXIT;
1223 }
1224 EXPORT_SYMBOL(cl_2queue_add);
1225
1226 /**
1227  * Disown pages in both lists of a 2-queue.
1228  */
1229 void cl_2queue_disown(const struct lu_env *env,
1230                       struct cl_io *io, struct cl_2queue *queue)
1231 {
1232         ENTRY;
1233         cl_page_list_disown(env, io, &queue->c2_qin);
1234         cl_page_list_disown(env, io, &queue->c2_qout);
1235         EXIT;
1236 }
1237 EXPORT_SYMBOL(cl_2queue_disown);
1238
1239 /**
1240  * Discard (truncate) pages in both lists of a 2-queue.
1241  */
1242 void cl_2queue_discard(const struct lu_env *env,
1243                        struct cl_io *io, struct cl_2queue *queue)
1244 {
1245         ENTRY;
1246         cl_page_list_discard(env, io, &queue->c2_qin);
1247         cl_page_list_discard(env, io, &queue->c2_qout);
1248         EXIT;
1249 }
1250 EXPORT_SYMBOL(cl_2queue_discard);
1251
1252 /**
1253  * Assume to own the pages in cl_2queue
1254  */
1255 void cl_2queue_assume(const struct lu_env *env,
1256                       struct cl_io *io, struct cl_2queue *queue)
1257 {
1258         cl_page_list_assume(env, io, &queue->c2_qin);
1259         cl_page_list_assume(env, io, &queue->c2_qout);
1260 }
1261 EXPORT_SYMBOL(cl_2queue_assume);
1262
1263 /**
1264  * Finalize both page lists of a 2-queue.
1265  */
1266 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1267 {
1268         ENTRY;
1269         cl_page_list_fini(env, &queue->c2_qout);
1270         cl_page_list_fini(env, &queue->c2_qin);
1271         EXIT;
1272 }
1273 EXPORT_SYMBOL(cl_2queue_fini);
1274
1275 /**
1276  * Initialize a 2-queue to contain \a page in its incoming page list.
1277  */
1278 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1279 {
1280         ENTRY;
1281         cl_2queue_init(queue);
1282         cl_2queue_add(queue, page);
1283         EXIT;
1284 }
1285 EXPORT_SYMBOL(cl_2queue_init_page);
1286
1287 /**
1288  * Returns top-level io.
1289  *
1290  * \see cl_object_top(), cl_page_top().
1291  */
1292 struct cl_io *cl_io_top(struct cl_io *io)
1293 {
1294         ENTRY;
1295         while (io->ci_parent != NULL)
1296                 io = io->ci_parent;
1297         RETURN(io);
1298 }
1299 EXPORT_SYMBOL(cl_io_top);
1300
1301 /**
1302  * Prints human readable representation of \a io to the \a f.
1303  */
1304 void cl_io_print(const struct lu_env *env, void *cookie,
1305                  lu_printer_t printer, const struct cl_io *io)
1306 {
1307 }
1308
1309 /**
1310  * Adds request slice to the compound request.
1311  *
1312  * This is called by cl_device_operations::cdo_req_init() methods to add a
1313  * per-layer state to the request. New state is added at the end of
1314  * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1315  *
1316  * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1317  */
1318 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1319                       struct cl_device *dev,
1320                       const struct cl_req_operations *ops)
1321 {
1322         ENTRY;
1323         list_add_tail(&slice->crs_linkage, &req->crq_layers);
1324         slice->crs_dev = dev;
1325         slice->crs_ops = ops;
1326         slice->crs_req = req;
1327         EXIT;
1328 }
1329 EXPORT_SYMBOL(cl_req_slice_add);
1330
1331 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1332 {
1333         unsigned i;
1334
1335         LASSERT(list_empty(&req->crq_pages));
1336         LASSERT(req->crq_nrpages == 0);
1337         LINVRNT(list_empty(&req->crq_layers));
1338         LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1339         ENTRY;
1340
1341         if (req->crq_o != NULL) {
1342                 for (i = 0; i < req->crq_nrobjs; ++i) {
1343                         struct cl_object *obj = req->crq_o[i].ro_obj;
1344                         if (obj != NULL) {
1345                                 lu_object_ref_del_at(&obj->co_lu,
1346                                                      req->crq_o[i].ro_obj_ref,
1347                                                      "cl_req", req);
1348                                 cl_object_put(env, obj);
1349                         }
1350                 }
1351                 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1352         }
1353         OBD_FREE_PTR(req);
1354         EXIT;
1355 }
1356
1357 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1358                        struct cl_page *page)
1359 {
1360         struct cl_device     *dev;
1361         struct cl_page_slice *slice;
1362         int result;
1363
1364         ENTRY;
1365         result = 0;
1366         page = cl_page_top(page);
1367         do {
1368                 list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1369                         dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1370                         if (dev->cd_ops->cdo_req_init != NULL) {
1371                                 result = dev->cd_ops->cdo_req_init(env,
1372                                                                    dev, req);
1373                                 if (result != 0)
1374                                         break;
1375                         }
1376                 }
1377                 page = page->cp_child;
1378         } while (page != NULL && result == 0);
1379         RETURN(result);
1380 }
1381
1382 /**
1383  * Invokes per-request transfer completion call-backs
1384  * (cl_req_operations::cro_completion()) bottom-to-top.
1385  */
1386 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1387 {
1388         struct cl_req_slice *slice;
1389
1390         ENTRY;
1391         /*
1392          * for the lack of list_for_each_entry_reverse_safe()...
1393          */
1394         while (!list_empty(&req->crq_layers)) {
1395                 slice = list_entry(req->crq_layers.prev,
1396                                    struct cl_req_slice, crs_linkage);
1397                 list_del_init(&slice->crs_linkage);
1398                 if (slice->crs_ops->cro_completion != NULL)
1399                         slice->crs_ops->cro_completion(env, slice, rc);
1400         }
1401         cl_req_free(env, req);
1402         EXIT;
1403 }
1404 EXPORT_SYMBOL(cl_req_completion);
1405
1406 /**
1407  * Allocates new transfer request.
1408  */
1409 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1410                             enum cl_req_type crt, int nr_objects)
1411 {
1412         struct cl_req *req;
1413
1414         LINVRNT(nr_objects > 0);
1415         ENTRY;
1416
1417         OBD_ALLOC_PTR(req);
1418         if (req != NULL) {
1419                 int result;
1420
1421                 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1422                 if (req->crq_o != NULL) {
1423                         req->crq_nrobjs = nr_objects;
1424                         req->crq_type = crt;
1425                         CFS_INIT_LIST_HEAD(&req->crq_pages);
1426                         CFS_INIT_LIST_HEAD(&req->crq_layers);
1427                         result = cl_req_init(env, req, page);
1428                 } else
1429                         result = -ENOMEM;
1430                 if (result != 0) {
1431                         cl_req_completion(env, req, result);
1432                         req = ERR_PTR(result);
1433                 }
1434         } else
1435                 req = ERR_PTR(-ENOMEM);
1436         RETURN(req);
1437 }
1438 EXPORT_SYMBOL(cl_req_alloc);
1439
1440 /**
1441  * Adds a page to a request.
1442  */
1443 void cl_req_page_add(const struct lu_env *env,
1444                      struct cl_req *req, struct cl_page *page)
1445 {
1446         struct cl_object  *obj;
1447         struct cl_req_obj *rqo;
1448         int i;
1449
1450         ENTRY;
1451         page = cl_page_top(page);
1452
1453         LINVRNT(cl_page_is_vmlocked(env, page));
1454         LASSERT(list_empty(&page->cp_flight));
1455         LASSERT(page->cp_req == NULL);
1456
1457         list_add_tail(&page->cp_flight, &req->crq_pages);
1458         ++req->crq_nrpages;
1459         page->cp_req = req;
1460         obj = cl_object_top(page->cp_obj);
1461         for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1462                 if (rqo->ro_obj == NULL) {
1463                         rqo->ro_obj = obj;
1464                         cl_object_get(obj);
1465                         rqo->ro_obj_ref = lu_object_ref_add(&obj->co_lu,
1466                                                             "cl_req", req);
1467                         break;
1468                 }
1469         }
1470         LASSERT(i < req->crq_nrobjs);
1471         EXIT;
1472 }
1473 EXPORT_SYMBOL(cl_req_page_add);
1474
1475 /**
1476  * Removes a page from a request.
1477  */
1478 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1479 {
1480         struct cl_req *req = page->cp_req;
1481
1482         ENTRY;
1483         page = cl_page_top(page);
1484
1485         LINVRNT(cl_page_is_vmlocked(env, page));
1486         LASSERT(!list_empty(&page->cp_flight));
1487         LASSERT(req->crq_nrpages > 0);
1488
1489         list_del_init(&page->cp_flight);
1490         --req->crq_nrpages;
1491         page->cp_req = NULL;
1492         EXIT;
1493 }
1494 EXPORT_SYMBOL(cl_req_page_done);
1495
1496 /**
1497  * Notifies layers that request is about to depart by calling
1498  * cl_req_operations::cro_prep() top-to-bottom.
1499  */
1500 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1501 {
1502         int i;
1503         int result;
1504         const struct cl_req_slice *slice;
1505
1506         ENTRY;
1507         /*
1508          * Check that the caller of cl_req_alloc() didn't lie about the number
1509          * of objects.
1510          */
1511         for (i = 0; i < req->crq_nrobjs; ++i)
1512                 LASSERT(req->crq_o[i].ro_obj != NULL);
1513
1514         result = 0;
1515         list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1516                 if (slice->crs_ops->cro_prep != NULL) {
1517                         result = slice->crs_ops->cro_prep(env, slice);
1518                         if (result != 0)
1519                                 break;
1520                 }
1521         }
1522         RETURN(result);
1523 }
1524 EXPORT_SYMBOL(cl_req_prep);
1525
1526 /**
1527  * Fills in attributes that are passed to server together with transfer. Only
1528  * attributes from \a flags may be touched. This can be called multiple times
1529  * for the same request.
1530  */
1531 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1532                      struct cl_req_attr *attr, obd_valid flags)
1533 {
1534         const struct cl_req_slice *slice;
1535         struct cl_page            *page;
1536         int i;
1537
1538         LASSERT(!list_empty(&req->crq_pages));
1539         ENTRY;
1540
1541         /* Take any page to use as a model. */
1542         page = list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1543
1544         for (i = 0; i < req->crq_nrobjs; ++i) {
1545                 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1546                         const struct cl_page_slice *scan;
1547                         const struct cl_object     *obj;
1548
1549                         scan = cl_page_at(page,
1550                                           slice->crs_dev->cd_lu_dev.ld_type);
1551                         LASSERT(scan != NULL);
1552                         obj = scan->cpl_obj;
1553                         if (slice->crs_ops->cro_attr_set != NULL)
1554                                 slice->crs_ops->cro_attr_set(env, slice, obj,
1555                                                              attr + i, flags);
1556                 }
1557         }
1558         EXIT;
1559 }
1560 EXPORT_SYMBOL(cl_req_attr_set);
1561
1562 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1563  * implemented in libcfs. */
1564 #ifdef __KERNEL__
1565 # include <linux/sched.h>
1566 #else /* __KERNEL__ */
1567 # include <liblustre.h>
1568 #endif
1569
1570 /**
1571  * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1572  */
1573 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1574 {
1575         ENTRY;
1576         init_completion(&anchor->csi_sync_completion);
1577         atomic_set(&anchor->csi_sync_nr, nrpages);
1578         anchor->csi_sync_rc  = 0;
1579         EXIT;
1580 }
1581 EXPORT_SYMBOL(cl_sync_io_init);
1582
1583 /**
1584  * Wait until all transfer completes. Transfer completion routine has to call
1585  * cl_sync_io_note() for every page.
1586  */
1587 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1588                     struct cl_page_list *queue, struct cl_sync_io *anchor)
1589 {
1590         int rc;
1591         ENTRY;
1592
1593         rc = wait_for_completion_interruptible(&anchor->csi_sync_completion);
1594         if (rc < 0) {
1595                 int rc2;
1596                 rc2 = cl_io_cancel(env, io, queue);
1597                 if (rc2 < 0) {
1598                         /* Too bad, some pages are still in IO. */
1599                         CDEBUG(D_VFSTRACE, "Failed to cancel transfer (%i). "
1600                                "Waiting for %i pages\n",
1601                                rc2, atomic_read(&anchor->csi_sync_nr));
1602                         wait_for_completion(&anchor->csi_sync_completion);
1603                 }
1604         } else
1605                 rc = anchor->csi_sync_rc;
1606         LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1607         cl_page_list_assume(env, io, queue);
1608         POISON(anchor, 0x5a, sizeof *anchor);
1609         RETURN(rc);
1610 }
1611 EXPORT_SYMBOL(cl_sync_io_wait);
1612
1613 /**
1614  * Indicate that transfer of a single page completed.
1615  */
1616 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1617 {
1618         ENTRY;
1619         if (anchor->csi_sync_rc == 0 && ioret < 0)
1620                 anchor->csi_sync_rc = ioret;
1621         /*
1622          * Synchronous IO done without releasing page lock (e.g., as a part of
1623          * ->{prepare,commit}_write(). Completion is used to signal the end of
1624          * IO.
1625          */
1626         if (atomic_dec_and_test(&anchor->csi_sync_nr))
1627                 complete(&anchor->csi_sync_completion);
1628         EXIT;
1629 }
1630 EXPORT_SYMBOL(cl_sync_io_note);