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