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