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