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