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