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