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