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