Whamcloud - gitweb
LU-5478 obdclass: get rid of obd_* typedefs
[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 /**
996  * Disowns pages in a queue.
997  */
998 void cl_page_list_disown(const struct lu_env *env,
999                          struct cl_io *io, struct cl_page_list *plist)
1000 {
1001         struct cl_page *page;
1002         struct cl_page *temp;
1003
1004         LINVRNT(plist->pl_owner == current);
1005
1006         ENTRY;
1007         cl_page_list_for_each_safe(page, temp, plist) {
1008                 LASSERT(plist->pl_nr > 0);
1009
1010                 list_del_init(&page->cp_batch);
1011                 --plist->pl_nr;
1012                 /*
1013                  * cl_page_disown0 rather than usual cl_page_disown() is used,
1014                  * because pages are possibly in CPS_FREEING state already due
1015                  * to the call to cl_page_list_discard().
1016                  */
1017                 /*
1018                  * XXX cl_page_disown0() will fail if page is not locked.
1019                  */
1020                 cl_page_disown0(env, io, page);
1021                 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
1022                               plist);
1023                 cl_page_put(env, page);
1024         }
1025         EXIT;
1026 }
1027 EXPORT_SYMBOL(cl_page_list_disown);
1028
1029 /**
1030  * Releases pages from queue.
1031  */
1032 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1033 {
1034         struct cl_page *page;
1035         struct cl_page *temp;
1036
1037         LINVRNT(plist->pl_owner == current);
1038
1039         ENTRY;
1040         cl_page_list_for_each_safe(page, temp, plist)
1041                 cl_page_list_del(env, plist, page);
1042         LASSERT(plist->pl_nr == 0);
1043         EXIT;
1044 }
1045 EXPORT_SYMBOL(cl_page_list_fini);
1046
1047 /**
1048  * Owns all pages in a queue.
1049  */
1050 int cl_page_list_own(const struct lu_env *env,
1051                      struct cl_io *io, struct cl_page_list *plist)
1052 {
1053         struct cl_page *page;
1054         struct cl_page *temp;
1055         int result;
1056
1057         LINVRNT(plist->pl_owner == current);
1058
1059         ENTRY;
1060         result = 0;
1061         cl_page_list_for_each_safe(page, temp, plist) {
1062                 if (cl_page_own(env, io, page) == 0)
1063                         result = result ?: page->cp_error;
1064                 else
1065                         cl_page_list_del(env, plist, page);
1066         }
1067         RETURN(result);
1068 }
1069 EXPORT_SYMBOL(cl_page_list_own);
1070
1071 /**
1072  * Assumes all pages in a queue.
1073  */
1074 void cl_page_list_assume(const struct lu_env *env,
1075                          struct cl_io *io, struct cl_page_list *plist)
1076 {
1077         struct cl_page *page;
1078
1079         LINVRNT(plist->pl_owner == current);
1080
1081         cl_page_list_for_each(page, plist)
1082                 cl_page_assume(env, io, page);
1083 }
1084 EXPORT_SYMBOL(cl_page_list_assume);
1085
1086 /**
1087  * Discards all pages in a queue.
1088  */
1089 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1090                           struct cl_page_list *plist)
1091 {
1092         struct cl_page *page;
1093
1094         LINVRNT(plist->pl_owner == current);
1095         ENTRY;
1096         cl_page_list_for_each(page, plist)
1097                 cl_page_discard(env, io, page);
1098         EXIT;
1099 }
1100 EXPORT_SYMBOL(cl_page_list_discard);
1101
1102 /**
1103  * Initialize dual page queue.
1104  */
1105 void cl_2queue_init(struct cl_2queue *queue)
1106 {
1107         ENTRY;
1108         cl_page_list_init(&queue->c2_qin);
1109         cl_page_list_init(&queue->c2_qout);
1110         EXIT;
1111 }
1112 EXPORT_SYMBOL(cl_2queue_init);
1113
1114 /**
1115  * Add a page to the incoming page list of 2-queue.
1116  */
1117 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1118 {
1119         ENTRY;
1120         cl_page_list_add(&queue->c2_qin, page);
1121         EXIT;
1122 }
1123 EXPORT_SYMBOL(cl_2queue_add);
1124
1125 /**
1126  * Disown pages in both lists of a 2-queue.
1127  */
1128 void cl_2queue_disown(const struct lu_env *env,
1129                       struct cl_io *io, struct cl_2queue *queue)
1130 {
1131         ENTRY;
1132         cl_page_list_disown(env, io, &queue->c2_qin);
1133         cl_page_list_disown(env, io, &queue->c2_qout);
1134         EXIT;
1135 }
1136 EXPORT_SYMBOL(cl_2queue_disown);
1137
1138 /**
1139  * Discard (truncate) pages in both lists of a 2-queue.
1140  */
1141 void cl_2queue_discard(const struct lu_env *env,
1142                        struct cl_io *io, struct cl_2queue *queue)
1143 {
1144         ENTRY;
1145         cl_page_list_discard(env, io, &queue->c2_qin);
1146         cl_page_list_discard(env, io, &queue->c2_qout);
1147         EXIT;
1148 }
1149 EXPORT_SYMBOL(cl_2queue_discard);
1150
1151 /**
1152  * Assume to own the pages in cl_2queue
1153  */
1154 void cl_2queue_assume(const struct lu_env *env,
1155                       struct cl_io *io, struct cl_2queue *queue)
1156 {
1157         cl_page_list_assume(env, io, &queue->c2_qin);
1158         cl_page_list_assume(env, io, &queue->c2_qout);
1159 }
1160 EXPORT_SYMBOL(cl_2queue_assume);
1161
1162 /**
1163  * Finalize both page lists of a 2-queue.
1164  */
1165 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1166 {
1167         ENTRY;
1168         cl_page_list_fini(env, &queue->c2_qout);
1169         cl_page_list_fini(env, &queue->c2_qin);
1170         EXIT;
1171 }
1172 EXPORT_SYMBOL(cl_2queue_fini);
1173
1174 /**
1175  * Initialize a 2-queue to contain \a page in its incoming page list.
1176  */
1177 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1178 {
1179         ENTRY;
1180         cl_2queue_init(queue);
1181         cl_2queue_add(queue, page);
1182         EXIT;
1183 }
1184 EXPORT_SYMBOL(cl_2queue_init_page);
1185
1186 /**
1187  * Returns top-level io.
1188  *
1189  * \see cl_object_top()
1190  */
1191 struct cl_io *cl_io_top(struct cl_io *io)
1192 {
1193         ENTRY;
1194         while (io->ci_parent != NULL)
1195                 io = io->ci_parent;
1196         RETURN(io);
1197 }
1198 EXPORT_SYMBOL(cl_io_top);
1199
1200 /**
1201  * Prints human readable representation of \a io to the \a f.
1202  */
1203 void cl_io_print(const struct lu_env *env, void *cookie,
1204                  lu_printer_t printer, const struct cl_io *io)
1205 {
1206 }
1207
1208 /**
1209  * Adds request slice to the compound request.
1210  *
1211  * This is called by cl_device_operations::cdo_req_init() methods to add a
1212  * per-layer state to the request. New state is added at the end of
1213  * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1214  *
1215  * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1216  */
1217 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1218                       struct cl_device *dev,
1219                       const struct cl_req_operations *ops)
1220 {
1221         ENTRY;
1222         list_add_tail(&slice->crs_linkage, &req->crq_layers);
1223         slice->crs_dev = dev;
1224         slice->crs_ops = ops;
1225         slice->crs_req = req;
1226         EXIT;
1227 }
1228 EXPORT_SYMBOL(cl_req_slice_add);
1229
1230 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1231 {
1232         unsigned i;
1233
1234         LASSERT(list_empty(&req->crq_pages));
1235         LASSERT(req->crq_nrpages == 0);
1236         LINVRNT(list_empty(&req->crq_layers));
1237         LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1238         ENTRY;
1239
1240         if (req->crq_o != NULL) {
1241                 for (i = 0; i < req->crq_nrobjs; ++i) {
1242                         struct cl_object *obj = req->crq_o[i].ro_obj;
1243                         if (obj != NULL) {
1244                                 lu_object_ref_del_at(&obj->co_lu,
1245                                                      &req->crq_o[i].ro_obj_ref,
1246                                                      "cl_req", req);
1247                                 cl_object_put(env, obj);
1248                         }
1249                 }
1250                 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1251         }
1252         OBD_FREE_PTR(req);
1253         EXIT;
1254 }
1255
1256 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1257                        struct cl_page *page)
1258 {
1259         struct cl_device     *dev;
1260         struct cl_page_slice *slice;
1261         int result;
1262
1263         ENTRY;
1264         result = 0;
1265         list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1266                 dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1267                 if (dev->cd_ops->cdo_req_init != NULL) {
1268                         result = dev->cd_ops->cdo_req_init(env,
1269                                         dev, req);
1270                         if (result != 0)
1271                                 break;
1272                 }
1273         }
1274         RETURN(result);
1275 }
1276
1277 /**
1278  * Invokes per-request transfer completion call-backs
1279  * (cl_req_operations::cro_completion()) bottom-to-top.
1280  */
1281 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1282 {
1283         struct cl_req_slice *slice;
1284
1285         ENTRY;
1286         /*
1287          * for the lack of list_for_each_entry_reverse_safe()...
1288          */
1289         while (!list_empty(&req->crq_layers)) {
1290                 slice = list_entry(req->crq_layers.prev,
1291                                    struct cl_req_slice, crs_linkage);
1292                 list_del_init(&slice->crs_linkage);
1293                 if (slice->crs_ops->cro_completion != NULL)
1294                         slice->crs_ops->cro_completion(env, slice, rc);
1295         }
1296         cl_req_free(env, req);
1297         EXIT;
1298 }
1299 EXPORT_SYMBOL(cl_req_completion);
1300
1301 /**
1302  * Allocates new transfer request.
1303  */
1304 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1305                             enum cl_req_type crt, int nr_objects)
1306 {
1307         struct cl_req *req;
1308
1309         LINVRNT(nr_objects > 0);
1310         ENTRY;
1311
1312         OBD_ALLOC_PTR(req);
1313         if (req != NULL) {
1314                 int result;
1315
1316                 req->crq_type = crt;
1317                 INIT_LIST_HEAD(&req->crq_pages);
1318                 INIT_LIST_HEAD(&req->crq_layers);
1319
1320                 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1321                 if (req->crq_o != NULL) {
1322                         req->crq_nrobjs = nr_objects;
1323                         result = cl_req_init(env, req, page);
1324                 } else
1325                         result = -ENOMEM;
1326                 if (result != 0) {
1327                         cl_req_completion(env, req, result);
1328                         req = ERR_PTR(result);
1329                 }
1330         } else
1331                 req = ERR_PTR(-ENOMEM);
1332         RETURN(req);
1333 }
1334 EXPORT_SYMBOL(cl_req_alloc);
1335
1336 /**
1337  * Adds a page to a request.
1338  */
1339 void cl_req_page_add(const struct lu_env *env,
1340                      struct cl_req *req, struct cl_page *page)
1341 {
1342         struct cl_object  *obj;
1343         struct cl_req_obj *rqo;
1344         unsigned int i;
1345
1346         ENTRY;
1347
1348         LASSERT(list_empty(&page->cp_flight));
1349         LASSERT(page->cp_req == NULL);
1350
1351         CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1352                       req, req->crq_type, req->crq_nrpages);
1353
1354         list_add_tail(&page->cp_flight, &req->crq_pages);
1355         ++req->crq_nrpages;
1356         page->cp_req = req;
1357         obj = cl_object_top(page->cp_obj);
1358         for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1359                 if (rqo->ro_obj == NULL) {
1360                         rqo->ro_obj = obj;
1361                         cl_object_get(obj);
1362                         lu_object_ref_add_at(&obj->co_lu, &rqo->ro_obj_ref,
1363                                              "cl_req", req);
1364                         break;
1365                 }
1366         }
1367         LASSERT(i < req->crq_nrobjs);
1368         EXIT;
1369 }
1370 EXPORT_SYMBOL(cl_req_page_add);
1371
1372 /**
1373  * Removes a page from a request.
1374  */
1375 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1376 {
1377         struct cl_req *req = page->cp_req;
1378
1379         ENTRY;
1380
1381         LASSERT(!list_empty(&page->cp_flight));
1382         LASSERT(req->crq_nrpages > 0);
1383
1384         list_del_init(&page->cp_flight);
1385         --req->crq_nrpages;
1386         page->cp_req = NULL;
1387         EXIT;
1388 }
1389 EXPORT_SYMBOL(cl_req_page_done);
1390
1391 /**
1392  * Notifies layers that request is about to depart by calling
1393  * cl_req_operations::cro_prep() top-to-bottom.
1394  */
1395 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1396 {
1397         unsigned int i;
1398         int result;
1399         const struct cl_req_slice *slice;
1400
1401         ENTRY;
1402         /*
1403          * Check that the caller of cl_req_alloc() didn't lie about the number
1404          * of objects.
1405          */
1406         for (i = 0; i < req->crq_nrobjs; ++i)
1407                 LASSERT(req->crq_o[i].ro_obj != NULL);
1408
1409         result = 0;
1410         list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1411                 if (slice->crs_ops->cro_prep != NULL) {
1412                         result = slice->crs_ops->cro_prep(env, slice);
1413                         if (result != 0)
1414                                 break;
1415                 }
1416         }
1417         RETURN(result);
1418 }
1419 EXPORT_SYMBOL(cl_req_prep);
1420
1421 /**
1422  * Fills in attributes that are passed to server together with transfer. Only
1423  * attributes from \a flags may be touched. This can be called multiple times
1424  * for the same request.
1425  */
1426 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1427                      struct cl_req_attr *attr, u64 flags)
1428 {
1429         const struct cl_req_slice *slice;
1430         struct cl_page            *page;
1431         unsigned int i;
1432
1433         LASSERT(!list_empty(&req->crq_pages));
1434         ENTRY;
1435
1436         /* Take any page to use as a model. */
1437         page = list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1438
1439         for (i = 0; i < req->crq_nrobjs; ++i) {
1440                 list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1441                         const struct cl_page_slice *scan;
1442                         const struct cl_object     *obj;
1443
1444                         scan = cl_page_at(page,
1445                                           slice->crs_dev->cd_lu_dev.ld_type);
1446                         LASSERT(scan != NULL);
1447                         obj = scan->cpl_obj;
1448                         if (slice->crs_ops->cro_attr_set != NULL)
1449                                 slice->crs_ops->cro_attr_set(env, slice, obj,
1450                                                              attr + i, flags);
1451                 }
1452         }
1453         EXIT;
1454 }
1455 EXPORT_SYMBOL(cl_req_attr_set);
1456
1457 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1458  * wait for the IO to finish. */
1459 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1460 {
1461         wake_up_all(&anchor->csi_waitq);
1462
1463         /* it's safe to nuke or reuse anchor now */
1464         atomic_set(&anchor->csi_barrier, 0);
1465 }
1466 EXPORT_SYMBOL(cl_sync_io_end);
1467
1468 /**
1469  * Initialize synchronous io wait anchor
1470  */
1471 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1472                      void (*end)(const struct lu_env *, struct cl_sync_io *))
1473 {
1474         ENTRY;
1475         memset(anchor, 0, sizeof(*anchor));
1476         init_waitqueue_head(&anchor->csi_waitq);
1477         atomic_set(&anchor->csi_sync_nr, nr);
1478         atomic_set(&anchor->csi_barrier, nr > 0);
1479         anchor->csi_sync_rc = 0;
1480         anchor->csi_end_io = end;
1481         LASSERT(end != NULL);
1482         EXIT;
1483 }
1484 EXPORT_SYMBOL(cl_sync_io_init);
1485
1486 /**
1487  * Wait until all IO completes. Transfer completion routine has to call
1488  * cl_sync_io_note() for every entity.
1489  */
1490 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1491                     long timeout)
1492 {
1493         struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1494                                                   NULL, NULL, NULL);
1495         int rc;
1496         ENTRY;
1497
1498         LASSERT(timeout >= 0);
1499
1500         rc = l_wait_event(anchor->csi_waitq,
1501                           atomic_read(&anchor->csi_sync_nr) == 0,
1502                           &lwi);
1503         if (rc < 0) {
1504                 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1505                        rc, atomic_read(&anchor->csi_sync_nr));
1506
1507                 lwi = (struct l_wait_info) { 0 };
1508                 (void)l_wait_event(anchor->csi_waitq,
1509                                    atomic_read(&anchor->csi_sync_nr) == 0,
1510                                    &lwi);
1511         } else {
1512                 rc = anchor->csi_sync_rc;
1513         }
1514         LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1515
1516         /* wait until cl_sync_io_note() has done wakeup */
1517         while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1518                 cpu_relax();
1519         }
1520         RETURN(rc);
1521 }
1522 EXPORT_SYMBOL(cl_sync_io_wait);
1523
1524 /**
1525  * Indicate that transfer of a single page completed.
1526  */
1527 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1528                      int ioret)
1529 {
1530         ENTRY;
1531         if (anchor->csi_sync_rc == 0 && ioret < 0)
1532                 anchor->csi_sync_rc = ioret;
1533         /*
1534          * Synchronous IO done without releasing page lock (e.g., as a part of
1535          * ->{prepare,commit}_write(). Completion is used to signal the end of
1536          * IO.
1537          */
1538         LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1539         if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1540                 LASSERT(anchor->csi_end_io != NULL);
1541                 anchor->csi_end_io(env, anchor);
1542                 /* Can't access anchor any more */
1543         }
1544         EXIT;
1545 }
1546 EXPORT_SYMBOL(cl_sync_io_note);