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