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