Whamcloud - gitweb
LU-6943 clio: get rid of cl_req
[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         case CIT_DATA_VERSION:
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
511 /**
512  * Adds a lock to a lockset.
513  */
514 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
515                    struct cl_io_lock_link *link)
516 {
517         int result;
518
519         ENTRY;
520         if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
521                 result = +1;
522         else {
523                 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
524                 result = 0;
525         }
526         RETURN(result);
527 }
528 EXPORT_SYMBOL(cl_io_lock_add);
529
530 static void cl_free_io_lock_link(const struct lu_env *env,
531                                  struct cl_io_lock_link *link)
532 {
533         OBD_FREE_PTR(link);
534 }
535
536 /**
537  * Allocates new lock link, and uses it to add a lock to a lockset.
538  */
539 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
540                          struct cl_lock_descr *descr)
541 {
542         struct cl_io_lock_link *link;
543         int result;
544
545         ENTRY;
546         OBD_ALLOC_PTR(link);
547         if (link != NULL) {
548                 link->cill_descr = *descr;
549                 link->cill_fini  = cl_free_io_lock_link;
550                 result = cl_io_lock_add(env, io, link);
551                 if (result) /* lock match */
552                         link->cill_fini(env, link);
553         } else
554                 result = -ENOMEM;
555
556         RETURN(result);
557 }
558 EXPORT_SYMBOL(cl_io_lock_alloc_add);
559
560 /**
561  * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
562  */
563 int cl_io_start(const struct lu_env *env, struct cl_io *io)
564 {
565         const struct cl_io_slice *scan;
566         int result = 0;
567
568         LINVRNT(cl_io_is_loopable(io));
569         LINVRNT(io->ci_state == CIS_LOCKED);
570         LINVRNT(cl_io_invariant(io));
571         ENTRY;
572
573         io->ci_state = CIS_IO_GOING;
574         cl_io_for_each(scan, io) {
575                 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
576                         continue;
577                 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
578                 if (result != 0)
579                         break;
580         }
581         if (result >= 0)
582                 result = 0;
583         RETURN(result);
584 }
585 EXPORT_SYMBOL(cl_io_start);
586
587 /**
588  * Wait until current io iteration is finished by calling
589  * cl_io_operations::cio_end() bottom-to-top.
590  */
591 void cl_io_end(const struct lu_env *env, struct cl_io *io)
592 {
593         const struct cl_io_slice *scan;
594
595         LINVRNT(cl_io_is_loopable(io));
596         LINVRNT(io->ci_state == CIS_IO_GOING);
597         LINVRNT(cl_io_invariant(io));
598         ENTRY;
599
600         cl_io_for_each_reverse(scan, io) {
601                 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
602                         scan->cis_iop->op[io->ci_type].cio_end(env, scan);
603                 /* TODO: error handling. */
604         }
605         io->ci_state = CIS_IO_FINISHED;
606         EXIT;
607 }
608 EXPORT_SYMBOL(cl_io_end);
609
610 /**
611  * Called by read io, to decide the readahead extent
612  *
613  * \see cl_io_operations::cio_read_ahead()
614  */
615 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
616                      pgoff_t start, struct cl_read_ahead *ra)
617 {
618         const struct cl_io_slice *scan;
619         int                       result = 0;
620
621         LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
622         LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
623         LINVRNT(cl_io_invariant(io));
624         ENTRY;
625
626         cl_io_for_each(scan, io) {
627                 if (scan->cis_iop->cio_read_ahead == NULL)
628                         continue;
629
630                 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
631                 if (result != 0)
632                         break;
633         }
634         RETURN(result > 0 ? 0 : result);
635 }
636 EXPORT_SYMBOL(cl_io_read_ahead);
637
638 /**
639  * Commit a list of contiguous pages into writeback cache.
640  *
641  * \returns 0 if all pages committed, or errcode if error occurred.
642  * \see cl_io_operations::cio_commit_async()
643  */
644 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
645                         struct cl_page_list *queue, int from, int to,
646                         cl_commit_cbt cb)
647 {
648         const struct cl_io_slice *scan;
649         int result = 0;
650         ENTRY;
651
652         cl_io_for_each(scan, io) {
653                 if (scan->cis_iop->cio_commit_async == NULL)
654                         continue;
655                 result = scan->cis_iop->cio_commit_async(env, scan, queue,
656                                                          from, to, cb);
657                 if (result != 0)
658                         break;
659         }
660         RETURN(result);
661 }
662 EXPORT_SYMBOL(cl_io_commit_async);
663
664 /**
665  * Submits a list of pages for immediate io.
666  *
667  * After the function gets returned, The submitted pages are moved to
668  * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
669  * to be submitted, and the pages are errant to submit.
670  *
671  * \returns 0 if at least one page was submitted, error code otherwise.
672  * \see cl_io_operations::cio_submit()
673  */
674 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
675                     enum cl_req_type crt, struct cl_2queue *queue)
676 {
677         const struct cl_io_slice *scan;
678         int result = 0;
679         ENTRY;
680
681         cl_io_for_each(scan, io) {
682                 if (scan->cis_iop->cio_submit == NULL)
683                         continue;
684                 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
685                 if (result != 0)
686                         break;
687         }
688         /*
689          * If ->cio_submit() failed, no pages were sent.
690          */
691         LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
692         RETURN(result);
693 }
694 EXPORT_SYMBOL(cl_io_submit_rw);
695
696 /**
697  * Submit a sync_io and wait for the IO to be finished, or error happens.
698  * If \a timeout is zero, it means to wait for the IO unconditionally.
699  */
700 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
701                       enum cl_req_type iot, struct cl_2queue *queue,
702                       long timeout)
703 {
704         struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
705         struct cl_page *pg;
706         int rc;
707
708         cl_page_list_for_each(pg, &queue->c2_qin) {
709                 LASSERT(pg->cp_sync_io == NULL);
710                 pg->cp_sync_io = anchor;
711         }
712
713         cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
714         rc = cl_io_submit_rw(env, io, iot, queue);
715         if (rc == 0) {
716                 /*
717                  * If some pages weren't sent for any reason (e.g.,
718                  * read found up-to-date pages in the cache, or write found
719                  * clean pages), count them as completed to avoid infinite
720                  * wait.
721                  */
722                 cl_page_list_for_each(pg, &queue->c2_qin) {
723                         pg->cp_sync_io = NULL;
724                         cl_sync_io_note(env, anchor, 1);
725                 }
726
727                 /* wait for the IO to be finished. */
728                 rc = cl_sync_io_wait(env, anchor, timeout);
729                 cl_page_list_assume(env, io, &queue->c2_qout);
730         } else {
731                 LASSERT(list_empty(&queue->c2_qout.pl_pages));
732                 cl_page_list_for_each(pg, &queue->c2_qin)
733                         pg->cp_sync_io = NULL;
734         }
735         return rc;
736 }
737 EXPORT_SYMBOL(cl_io_submit_sync);
738
739 /**
740  * Cancel an IO which has been submitted by cl_io_submit_rw.
741  */
742 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
743                  struct cl_page_list *queue)
744 {
745         struct cl_page *page;
746         int result = 0;
747
748         CERROR("Canceling ongoing page trasmission\n");
749         cl_page_list_for_each(page, queue) {
750                 int rc;
751
752                 rc = cl_page_cancel(env, page);
753                 result = result ?: rc;
754         }
755         return result;
756 }
757
758 /**
759  * Main io loop.
760  *
761  * Pumps io through iterations calling
762  *
763  *    - cl_io_iter_init()
764  *
765  *    - cl_io_lock()
766  *
767  *    - cl_io_start()
768  *
769  *    - cl_io_end()
770  *
771  *    - cl_io_unlock()
772  *
773  *    - cl_io_iter_fini()
774  *
775  * repeatedly until there is no more io to do.
776  */
777 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
778 {
779         int result   = 0;
780
781         LINVRNT(cl_io_is_loopable(io));
782         ENTRY;
783
784         do {
785                 size_t nob;
786
787                 io->ci_continue = 0;
788                 result = cl_io_iter_init(env, io);
789                 if (result == 0) {
790                         nob    = io->ci_nob;
791                         result = cl_io_lock(env, io);
792                         if (result == 0) {
793                                 /*
794                                  * Notify layers that locks has been taken,
795                                  * and do actual i/o.
796                                  *
797                                  *   - llite: kms, short read;
798                                  *   - llite: generic_file_read();
799                                  */
800                                 result = cl_io_start(env, io);
801                                 /*
802                                  * Send any remaining pending
803                                  * io, etc.
804                                  *
805                                  *   - llite: ll_rw_stats_tally.
806                                  */
807                                 cl_io_end(env, io);
808                                 cl_io_unlock(env, io);
809                                 cl_io_rw_advance(env, io, io->ci_nob - nob);
810                         }
811                 }
812                 cl_io_iter_fini(env, io);
813         } while (result == 0 && io->ci_continue);
814         if (result == 0)
815                 result = io->ci_result;
816         RETURN(result < 0 ? result : 0);
817 }
818 EXPORT_SYMBOL(cl_io_loop);
819
820 /**
821  * Adds io slice to the cl_io.
822  *
823  * This is called by cl_object_operations::coo_io_init() methods to add a
824  * per-layer state to the io. New state is added at the end of
825  * cl_io::ci_layers list, that is, it is at the bottom of the stack.
826  *
827  * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
828  */
829 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
830                      struct cl_object *obj,
831                      const struct cl_io_operations *ops)
832 {
833         struct list_head *linkage = &slice->cis_linkage;
834
835         LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
836                 list_empty(linkage));
837         ENTRY;
838
839         list_add_tail(linkage, &io->ci_layers);
840         slice->cis_io  = io;
841         slice->cis_obj = obj;
842         slice->cis_iop = ops;
843         EXIT;
844 }
845 EXPORT_SYMBOL(cl_io_slice_add);
846
847
848 /**
849  * Initializes page list.
850  */
851 void cl_page_list_init(struct cl_page_list *plist)
852 {
853         ENTRY;
854         plist->pl_nr = 0;
855         INIT_LIST_HEAD(&plist->pl_pages);
856         plist->pl_owner = current;
857         EXIT;
858 }
859 EXPORT_SYMBOL(cl_page_list_init);
860
861 /**
862  * Adds a page to a page list.
863  */
864 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
865 {
866         ENTRY;
867         /* it would be better to check that page is owned by "current" io, but
868          * it is not passed here. */
869         LASSERT(page->cp_owner != NULL);
870         LINVRNT(plist->pl_owner == current);
871
872         LASSERT(list_empty(&page->cp_batch));
873         list_add_tail(&page->cp_batch, &plist->pl_pages);
874         ++plist->pl_nr;
875         lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
876         cl_page_get(page);
877         EXIT;
878 }
879 EXPORT_SYMBOL(cl_page_list_add);
880
881 /**
882  * Removes a page from a page list.
883  */
884 void cl_page_list_del(const struct lu_env *env,
885                       struct cl_page_list *plist, struct cl_page *page)
886 {
887         LASSERT(plist->pl_nr > 0);
888         LASSERT(cl_page_is_vmlocked(env, page));
889         LINVRNT(plist->pl_owner == current);
890
891         ENTRY;
892         list_del_init(&page->cp_batch);
893         --plist->pl_nr;
894         lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
895         cl_page_put(env, page);
896         EXIT;
897 }
898 EXPORT_SYMBOL(cl_page_list_del);
899
900 /**
901  * Moves a page from one page list to another.
902  */
903 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
904                        struct cl_page *page)
905 {
906         LASSERT(src->pl_nr > 0);
907         LINVRNT(dst->pl_owner == current);
908         LINVRNT(src->pl_owner == current);
909
910         ENTRY;
911         list_move_tail(&page->cp_batch, &dst->pl_pages);
912         --src->pl_nr;
913         ++dst->pl_nr;
914         lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
915                       src, dst);
916         EXIT;
917 }
918 EXPORT_SYMBOL(cl_page_list_move);
919
920 /**
921  * Moves a page from one page list to the head of another list.
922  */
923 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
924                             struct cl_page *page)
925 {
926         LASSERT(src->pl_nr > 0);
927         LINVRNT(dst->pl_owner == current);
928         LINVRNT(src->pl_owner == current);
929
930         ENTRY;
931         list_move(&page->cp_batch, &dst->pl_pages);
932         --src->pl_nr;
933         ++dst->pl_nr;
934         lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
935                         src, dst);
936         EXIT;
937 }
938 EXPORT_SYMBOL(cl_page_list_move_head);
939
940 /**
941  * splice the cl_page_list, just as list head does
942  */
943 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
944 {
945         struct cl_page *page;
946         struct cl_page *tmp;
947
948         LINVRNT(list->pl_owner == current);
949         LINVRNT(head->pl_owner == current);
950
951         ENTRY;
952         cl_page_list_for_each_safe(page, tmp, list)
953                 cl_page_list_move(head, list, page);
954         EXIT;
955 }
956 EXPORT_SYMBOL(cl_page_list_splice);
957
958 /**
959  * Disowns pages in a queue.
960  */
961 void cl_page_list_disown(const struct lu_env *env,
962                          struct cl_io *io, struct cl_page_list *plist)
963 {
964         struct cl_page *page;
965         struct cl_page *temp;
966
967         LINVRNT(plist->pl_owner == current);
968
969         ENTRY;
970         cl_page_list_for_each_safe(page, temp, plist) {
971                 LASSERT(plist->pl_nr > 0);
972
973                 list_del_init(&page->cp_batch);
974                 --plist->pl_nr;
975                 /*
976                  * cl_page_disown0 rather than usual cl_page_disown() is used,
977                  * because pages are possibly in CPS_FREEING state already due
978                  * to the call to cl_page_list_discard().
979                  */
980                 /*
981                  * XXX cl_page_disown0() will fail if page is not locked.
982                  */
983                 cl_page_disown0(env, io, page);
984                 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
985                               plist);
986                 cl_page_put(env, page);
987         }
988         EXIT;
989 }
990 EXPORT_SYMBOL(cl_page_list_disown);
991
992 /**
993  * Releases pages from queue.
994  */
995 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
996 {
997         struct cl_page *page;
998         struct cl_page *temp;
999
1000         LINVRNT(plist->pl_owner == current);
1001
1002         ENTRY;
1003         cl_page_list_for_each_safe(page, temp, plist)
1004                 cl_page_list_del(env, plist, page);
1005         LASSERT(plist->pl_nr == 0);
1006         EXIT;
1007 }
1008 EXPORT_SYMBOL(cl_page_list_fini);
1009
1010 /**
1011  * Owns all pages in a queue.
1012  */
1013 int cl_page_list_own(const struct lu_env *env,
1014                      struct cl_io *io, struct cl_page_list *plist)
1015 {
1016         struct cl_page *page;
1017         struct cl_page *temp;
1018         int result;
1019
1020         LINVRNT(plist->pl_owner == current);
1021
1022         ENTRY;
1023         result = 0;
1024         cl_page_list_for_each_safe(page, temp, plist) {
1025                 if (cl_page_own(env, io, page) == 0)
1026                         result = result ?: page->cp_error;
1027                 else
1028                         cl_page_list_del(env, plist, page);
1029         }
1030         RETURN(result);
1031 }
1032
1033 /**
1034  * Assumes all pages in a queue.
1035  */
1036 void cl_page_list_assume(const struct lu_env *env,
1037                          struct cl_io *io, struct cl_page_list *plist)
1038 {
1039         struct cl_page *page;
1040
1041         LINVRNT(plist->pl_owner == current);
1042
1043         cl_page_list_for_each(page, plist)
1044                 cl_page_assume(env, io, page);
1045 }
1046
1047 /**
1048  * Discards all pages in a queue.
1049  */
1050 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1051                           struct cl_page_list *plist)
1052 {
1053         struct cl_page *page;
1054
1055         LINVRNT(plist->pl_owner == current);
1056         ENTRY;
1057         cl_page_list_for_each(page, plist)
1058                 cl_page_discard(env, io, page);
1059         EXIT;
1060 }
1061
1062 /**
1063  * Initialize dual page queue.
1064  */
1065 void cl_2queue_init(struct cl_2queue *queue)
1066 {
1067         ENTRY;
1068         cl_page_list_init(&queue->c2_qin);
1069         cl_page_list_init(&queue->c2_qout);
1070         EXIT;
1071 }
1072 EXPORT_SYMBOL(cl_2queue_init);
1073
1074 /**
1075  * Add a page to the incoming page list of 2-queue.
1076  */
1077 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1078 {
1079         ENTRY;
1080         cl_page_list_add(&queue->c2_qin, page);
1081         EXIT;
1082 }
1083 EXPORT_SYMBOL(cl_2queue_add);
1084
1085 /**
1086  * Disown pages in both lists of a 2-queue.
1087  */
1088 void cl_2queue_disown(const struct lu_env *env,
1089                       struct cl_io *io, struct cl_2queue *queue)
1090 {
1091         ENTRY;
1092         cl_page_list_disown(env, io, &queue->c2_qin);
1093         cl_page_list_disown(env, io, &queue->c2_qout);
1094         EXIT;
1095 }
1096 EXPORT_SYMBOL(cl_2queue_disown);
1097
1098 /**
1099  * Discard (truncate) pages in both lists of a 2-queue.
1100  */
1101 void cl_2queue_discard(const struct lu_env *env,
1102                        struct cl_io *io, struct cl_2queue *queue)
1103 {
1104         ENTRY;
1105         cl_page_list_discard(env, io, &queue->c2_qin);
1106         cl_page_list_discard(env, io, &queue->c2_qout);
1107         EXIT;
1108 }
1109 EXPORT_SYMBOL(cl_2queue_discard);
1110
1111 /**
1112  * Assume to own the pages in cl_2queue
1113  */
1114 void cl_2queue_assume(const struct lu_env *env,
1115                       struct cl_io *io, struct cl_2queue *queue)
1116 {
1117         cl_page_list_assume(env, io, &queue->c2_qin);
1118         cl_page_list_assume(env, io, &queue->c2_qout);
1119 }
1120
1121 /**
1122  * Finalize both page lists of a 2-queue.
1123  */
1124 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1125 {
1126         ENTRY;
1127         cl_page_list_fini(env, &queue->c2_qout);
1128         cl_page_list_fini(env, &queue->c2_qin);
1129         EXIT;
1130 }
1131 EXPORT_SYMBOL(cl_2queue_fini);
1132
1133 /**
1134  * Initialize a 2-queue to contain \a page in its incoming page list.
1135  */
1136 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1137 {
1138         ENTRY;
1139         cl_2queue_init(queue);
1140         cl_2queue_add(queue, page);
1141         EXIT;
1142 }
1143 EXPORT_SYMBOL(cl_2queue_init_page);
1144
1145 /**
1146  * Returns top-level io.
1147  *
1148  * \see cl_object_top()
1149  */
1150 struct cl_io *cl_io_top(struct cl_io *io)
1151 {
1152         ENTRY;
1153         while (io->ci_parent != NULL)
1154                 io = io->ci_parent;
1155         RETURN(io);
1156 }
1157 EXPORT_SYMBOL(cl_io_top);
1158
1159 /**
1160  * Prints human readable representation of \a io to the \a f.
1161  */
1162 void cl_io_print(const struct lu_env *env, void *cookie,
1163                  lu_printer_t printer, const struct cl_io *io)
1164 {
1165 }
1166
1167 /**
1168  * Fills in attributes that are passed to server together with transfer. Only
1169  * attributes from \a flags may be touched. This can be called multiple times
1170  * for the same request.
1171  */
1172 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1173                      struct cl_req_attr *attr)
1174 {
1175         struct cl_object *scan;
1176         ENTRY;
1177
1178         cl_object_for_each(scan, obj) {
1179                 if (scan->co_ops->coo_req_attr_set != NULL)
1180                         scan->co_ops->coo_req_attr_set(env, scan, attr);
1181         }
1182         EXIT;
1183 }
1184 EXPORT_SYMBOL(cl_req_attr_set);
1185
1186 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1187  * wait for the IO to finish. */
1188 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1189 {
1190         wake_up_all(&anchor->csi_waitq);
1191
1192         /* it's safe to nuke or reuse anchor now */
1193         atomic_set(&anchor->csi_barrier, 0);
1194 }
1195 EXPORT_SYMBOL(cl_sync_io_end);
1196
1197 /**
1198  * Initialize synchronous io wait anchor
1199  */
1200 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1201                      void (*end)(const struct lu_env *, struct cl_sync_io *))
1202 {
1203         ENTRY;
1204         memset(anchor, 0, sizeof(*anchor));
1205         init_waitqueue_head(&anchor->csi_waitq);
1206         atomic_set(&anchor->csi_sync_nr, nr);
1207         atomic_set(&anchor->csi_barrier, nr > 0);
1208         anchor->csi_sync_rc = 0;
1209         anchor->csi_end_io = end;
1210         LASSERT(end != NULL);
1211         EXIT;
1212 }
1213 EXPORT_SYMBOL(cl_sync_io_init);
1214
1215 /**
1216  * Wait until all IO completes. Transfer completion routine has to call
1217  * cl_sync_io_note() for every entity.
1218  */
1219 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1220                     long timeout)
1221 {
1222         struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1223                                                   NULL, NULL, NULL);
1224         int rc;
1225         ENTRY;
1226
1227         LASSERT(timeout >= 0);
1228
1229         rc = l_wait_event(anchor->csi_waitq,
1230                           atomic_read(&anchor->csi_sync_nr) == 0,
1231                           &lwi);
1232         if (rc < 0) {
1233                 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1234                        rc, atomic_read(&anchor->csi_sync_nr));
1235
1236                 lwi = (struct l_wait_info) { 0 };
1237                 (void)l_wait_event(anchor->csi_waitq,
1238                                    atomic_read(&anchor->csi_sync_nr) == 0,
1239                                    &lwi);
1240         } else {
1241                 rc = anchor->csi_sync_rc;
1242         }
1243         LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1244
1245         /* wait until cl_sync_io_note() has done wakeup */
1246         while (unlikely(atomic_read(&anchor->csi_barrier) != 0)) {
1247                 cpu_relax();
1248         }
1249         RETURN(rc);
1250 }
1251 EXPORT_SYMBOL(cl_sync_io_wait);
1252
1253 /**
1254  * Indicate that transfer of a single page completed.
1255  */
1256 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1257                      int ioret)
1258 {
1259         ENTRY;
1260         if (anchor->csi_sync_rc == 0 && ioret < 0)
1261                 anchor->csi_sync_rc = ioret;
1262         /*
1263          * Synchronous IO done without releasing page lock (e.g., as a part of
1264          * ->{prepare,commit}_write(). Completion is used to signal the end of
1265          * IO.
1266          */
1267         LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1268         if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1269                 LASSERT(anchor->csi_end_io != NULL);
1270                 anchor->csi_end_io(env, anchor);
1271                 /* Can't access anchor any more */
1272         }
1273         EXIT;
1274 }
1275 EXPORT_SYMBOL(cl_sync_io_note);