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