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