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