GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / lustre / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Client IO.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_CLASS
39
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <lustre_fid.h>
43 #include <linux/list.h>
44 #include <linux/sched.h>
45 #include <cl_object.h>
46 #include "cl_internal.h"
47
48 /*****************************************************************************
49  *
50  * cl_io interface.
51  *
52  */
53
54 #define cl_io_for_each(slice, io) \
55         list_for_each_entry((slice), &io->ci_layers, cis_linkage)
56 #define cl_io_for_each_reverse(slice, io)                \
57         list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
58
59 static inline int cl_io_type_is_valid(enum cl_io_type type)
60 {
61         return CIT_READ <= type && type < CIT_OP_NR;
62 }
63
64 static inline int cl_io_is_loopable(const struct cl_io *io)
65 {
66         return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
67 }
68
69 /**
70  * Returns true iff there is an IO ongoing in the given environment.
71  */
72 int cl_io_is_going(const struct lu_env *env)
73 {
74         return cl_env_info(env)->clt_current_io != NULL;
75 }
76
77 /**
78  * cl_io invariant that holds at all times when exported cl_io_*() functions
79  * are entered and left.
80  */
81 static int cl_io_invariant(const struct cl_io *io)
82 {
83         struct cl_io *up;
84
85         up = io->ci_parent;
86         return
87                 /*
88                  * io can own pages only when it is ongoing. Sub-io might
89                  * still be in CIS_LOCKED state when top-io is in
90                  * CIS_IO_GOING.
91                  */
92                 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
93                      (io->ci_state == CIS_LOCKED && up));
94 }
95
96 /**
97  * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
98  */
99 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
100 {
101         struct cl_io_slice    *slice;
102         struct cl_thread_info *info;
103
104         LINVRNT(cl_io_type_is_valid(io->ci_type));
105         LINVRNT(cl_io_invariant(io));
106
107         while (!list_empty(&io->ci_layers)) {
108                 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
109                                      cis_linkage);
110                 list_del_init(&slice->cis_linkage);
111                 if (slice->cis_iop->op[io->ci_type].cio_fini)
112                         slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
113                 /*
114                  * Invalidate slice to catch use after free. This assumes that
115                  * slices are allocated within session and can be touched
116                  * after ->cio_fini() returns.
117                  */
118                 slice->cis_io = NULL;
119         }
120         io->ci_state = CIS_FINI;
121         info = cl_env_info(env);
122         if (info->clt_current_io == io)
123                 info->clt_current_io = NULL;
124
125         /* sanity check for layout change */
126         switch (io->ci_type) {
127         case CIT_READ:
128         case CIT_WRITE:
129         case CIT_DATA_VERSION:
130                 break;
131         case CIT_FAULT:
132                 break;
133         case CIT_FSYNC:
134                 LASSERT(!io->ci_need_restart);
135                 break;
136         case CIT_SETATTR:
137         case CIT_MISC:
138                 /* Check ignore layout change conf */
139                 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
140                              !io->ci_need_restart));
141                 break;
142         default:
143                 LBUG();
144         }
145 }
146 EXPORT_SYMBOL(cl_io_fini);
147
148 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
149                        enum cl_io_type iot, struct cl_object *obj)
150 {
151         struct cl_object *scan;
152         int result;
153
154         LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
155         LINVRNT(cl_io_type_is_valid(iot));
156         LINVRNT(cl_io_invariant(io));
157
158         io->ci_type = iot;
159         INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
160         INIT_LIST_HEAD(&io->ci_lockset.cls_done);
161         INIT_LIST_HEAD(&io->ci_layers);
162
163         result = 0;
164         cl_object_for_each(scan, obj) {
165                 if (scan->co_ops->coo_io_init) {
166                         result = scan->co_ops->coo_io_init(env, scan, io);
167                         if (result != 0)
168                                 break;
169                 }
170         }
171         if (result == 0)
172                 io->ci_state = CIS_INIT;
173         return result;
174 }
175
176 /**
177  * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
178  *
179  * \pre obj != cl_object_top(obj)
180  */
181 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
182                    enum cl_io_type iot, struct cl_object *obj)
183 {
184         struct cl_thread_info *info = cl_env_info(env);
185
186         LASSERT(obj != cl_object_top(obj));
187         if (!info->clt_current_io)
188                 info->clt_current_io = io;
189         return cl_io_init0(env, io, iot, obj);
190 }
191 EXPORT_SYMBOL(cl_io_sub_init);
192
193 /**
194  * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
195  *
196  * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
197  * what the latter returned.
198  *
199  * \pre obj == cl_object_top(obj)
200  * \pre cl_io_type_is_valid(iot)
201  * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
202  */
203 int cl_io_init(const struct lu_env *env, struct cl_io *io,
204                enum cl_io_type iot, struct cl_object *obj)
205 {
206         struct cl_thread_info *info = cl_env_info(env);
207
208         LASSERT(obj == cl_object_top(obj));
209         LASSERT(!info->clt_current_io);
210
211         info->clt_current_io = io;
212         return cl_io_init0(env, io, iot, obj);
213 }
214 EXPORT_SYMBOL(cl_io_init);
215
216 /**
217  * Initialize read or write io.
218  *
219  * \pre iot == CIT_READ || iot == CIT_WRITE
220  */
221 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
222                   enum cl_io_type iot, loff_t pos, size_t count)
223 {
224         LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
225         LINVRNT(io->ci_obj);
226
227         LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
228                          "io range: %u [%llu, %llu) %u %u\n",
229                          iot, (__u64)pos, (__u64)pos + count,
230                          io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
231         io->u.ci_rw.crw_pos    = pos;
232         io->u.ci_rw.crw_count  = count;
233         return cl_io_init(env, io, iot, io->ci_obj);
234 }
235 EXPORT_SYMBOL(cl_io_rw_init);
236
237 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
238                               const struct cl_lock_descr *d1)
239 {
240         return lu_fid_cmp(lu_object_fid(&d0->cld_obj->co_lu),
241                           lu_object_fid(&d1->cld_obj->co_lu));
242 }
243
244 /*
245  * Sort locks in lexicographical order of their (fid, start-offset) pairs.
246  */
247 static void cl_io_locks_sort(struct cl_io *io)
248 {
249         int done = 0;
250
251         /* hidden treasure: bubble sort for now. */
252         do {
253                 struct cl_io_lock_link *curr;
254                 struct cl_io_lock_link *prev;
255                 struct cl_io_lock_link *temp;
256
257                 done = 1;
258                 prev = NULL;
259
260                 list_for_each_entry_safe(curr, temp,
261                                          &io->ci_lockset.cls_todo,
262                                          cill_linkage) {
263                         if (prev) {
264                                 switch (cl_lock_descr_sort(&prev->cill_descr,
265                                                            &curr->cill_descr)) {
266                                 case 0:
267                                         /*
268                                          * IMPOSSIBLE: Identical locks are
269                                          *           already removed at
270                                          *           this point.
271                                          */
272                                 default:
273                                         LBUG();
274                                 case 1:
275                                         list_move_tail(&curr->cill_linkage,
276                                                        &prev->cill_linkage);
277                                         done = 0;
278                                         continue; /* don't change prev: it's
279                                                    * still "previous"
280                                                    */
281                                 case -1: /* already in order */
282                                         break;
283                                 }
284                         }
285                         prev = curr;
286                 }
287         } while (!done);
288 }
289
290 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
291                                 const struct cl_lock_descr *d1)
292 {
293         d0->cld_start = min(d0->cld_start, d1->cld_start);
294         d0->cld_end = max(d0->cld_end, d1->cld_end);
295
296         if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
297                 d0->cld_mode = CLM_WRITE;
298
299         if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
300                 d0->cld_mode = CLM_GROUP;
301 }
302
303 static int cl_lockset_merge(const struct cl_lockset *set,
304                             const struct cl_lock_descr *need)
305 {
306         struct cl_io_lock_link *scan;
307
308         list_for_each_entry(scan, &set->cls_todo, cill_linkage) {
309                 if (!cl_object_same(scan->cill_descr.cld_obj, need->cld_obj))
310                         continue;
311
312                 /* Merge locks for the same object because ldlm lock server
313                  * may expand the lock extent, otherwise there is a deadlock
314                  * case if two conflicted locks are queueud for the same object
315                  * and lock server expands one lock to overlap the another.
316                  * The side effect is that it can generate a multi-stripe lock
317                  * that may cause casacading problem
318                  */
319                 cl_lock_descr_merge(&scan->cill_descr, need);
320                 CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
321                        scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
322                        scan->cill_descr.cld_end);
323                 return 1;
324         }
325         return 0;
326 }
327
328 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
329                            struct cl_lockset *set)
330 {
331         struct cl_io_lock_link *link;
332         struct cl_io_lock_link *temp;
333         int result;
334
335         result = 0;
336         list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
337                 result = cl_lock_request(env, io, &link->cill_lock);
338                 if (result < 0)
339                         break;
340
341                 list_move(&link->cill_linkage, &set->cls_done);
342         }
343         return result;
344 }
345
346 /**
347  * Takes locks necessary for the current iteration of io.
348  *
349  * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
350  * by layers for the current iteration. Then sort locks (to avoid dead-locks),
351  * and acquire them.
352  */
353 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
354 {
355         const struct cl_io_slice *scan;
356         int result = 0;
357
358         LINVRNT(cl_io_is_loopable(io));
359         LINVRNT(io->ci_state == CIS_IT_STARTED);
360         LINVRNT(cl_io_invariant(io));
361
362         cl_io_for_each(scan, io) {
363                 if (!scan->cis_iop->op[io->ci_type].cio_lock)
364                         continue;
365                 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
366                 if (result != 0)
367                         break;
368         }
369         if (result == 0) {
370                 cl_io_locks_sort(io);
371                 result = cl_lockset_lock(env, io, &io->ci_lockset);
372         }
373         if (result != 0)
374                 cl_io_unlock(env, io);
375         else
376                 io->ci_state = CIS_LOCKED;
377         return result;
378 }
379 EXPORT_SYMBOL(cl_io_lock);
380
381 /**
382  * Release locks takes by io.
383  */
384 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
385 {
386         struct cl_lockset       *set;
387         struct cl_io_lock_link   *link;
388         struct cl_io_lock_link   *temp;
389         const struct cl_io_slice *scan;
390
391         LASSERT(cl_io_is_loopable(io));
392         LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
393         LINVRNT(cl_io_invariant(io));
394
395         set = &io->ci_lockset;
396
397         list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
398                 list_del_init(&link->cill_linkage);
399                 if (link->cill_fini)
400                         link->cill_fini(env, link);
401         }
402
403         list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
404                 list_del_init(&link->cill_linkage);
405                 cl_lock_release(env, &link->cill_lock);
406                 if (link->cill_fini)
407                         link->cill_fini(env, link);
408         }
409
410         cl_io_for_each_reverse(scan, io) {
411                 if (scan->cis_iop->op[io->ci_type].cio_unlock)
412                         scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
413         }
414         io->ci_state = CIS_UNLOCKED;
415 }
416 EXPORT_SYMBOL(cl_io_unlock);
417
418 /**
419  * Prepares next iteration of io.
420  *
421  * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
422  * layers a chance to modify io parameters, e.g., so that lov can restrict io
423  * to a single stripe.
424  */
425 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
426 {
427         const struct cl_io_slice *scan;
428         int result;
429
430         LINVRNT(cl_io_is_loopable(io));
431         LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
432         LINVRNT(cl_io_invariant(io));
433
434         result = 0;
435         cl_io_for_each(scan, io) {
436                 if (!scan->cis_iop->op[io->ci_type].cio_iter_init)
437                         continue;
438                 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
439                                                                       scan);
440                 if (result != 0)
441                         break;
442         }
443         if (result == 0)
444                 io->ci_state = CIS_IT_STARTED;
445         return result;
446 }
447 EXPORT_SYMBOL(cl_io_iter_init);
448
449 /**
450  * Finalizes io iteration.
451  *
452  * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
453  */
454 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
455 {
456         const struct cl_io_slice *scan;
457
458         LINVRNT(cl_io_is_loopable(io));
459         LINVRNT(io->ci_state == CIS_UNLOCKED);
460         LINVRNT(cl_io_invariant(io));
461
462         cl_io_for_each_reverse(scan, io) {
463                 if (scan->cis_iop->op[io->ci_type].cio_iter_fini)
464                         scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
465         }
466         io->ci_state = CIS_IT_ENDED;
467 }
468 EXPORT_SYMBOL(cl_io_iter_fini);
469
470 /**
471  * Records that read or write io progressed \a nob bytes forward.
472  */
473 static void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io,
474                              size_t nob)
475 {
476         const struct cl_io_slice *scan;
477
478         LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
479                 nob == 0);
480         LINVRNT(cl_io_is_loopable(io));
481         LINVRNT(cl_io_invariant(io));
482
483         io->u.ci_rw.crw_pos   += nob;
484         io->u.ci_rw.crw_count -= nob;
485
486         /* layers have to be notified. */
487         cl_io_for_each_reverse(scan, io) {
488                 if (scan->cis_iop->op[io->ci_type].cio_advance)
489                         scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
490                                                                    nob);
491         }
492 }
493
494 /**
495  * Adds a lock to a lockset.
496  */
497 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
498                    struct cl_io_lock_link *link)
499 {
500         int result;
501
502         if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr)) {
503                 result = 1;
504         } else {
505                 list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
506                 result = 0;
507         }
508         return result;
509 }
510 EXPORT_SYMBOL(cl_io_lock_add);
511
512 static void cl_free_io_lock_link(const struct lu_env *env,
513                                  struct cl_io_lock_link *link)
514 {
515         kfree(link);
516 }
517
518 /**
519  * Allocates new lock link, and uses it to add a lock to a lockset.
520  */
521 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
522                          struct cl_lock_descr *descr)
523 {
524         struct cl_io_lock_link *link;
525         int result;
526
527         link = kzalloc(sizeof(*link), GFP_NOFS);
528         if (link) {
529                 link->cill_descr     = *descr;
530                 link->cill_fini      = cl_free_io_lock_link;
531                 result = cl_io_lock_add(env, io, link);
532                 if (result) /* lock match */
533                         link->cill_fini(env, link);
534         } else {
535                 result = -ENOMEM;
536         }
537
538         return result;
539 }
540 EXPORT_SYMBOL(cl_io_lock_alloc_add);
541
542 /**
543  * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
544  */
545 int cl_io_start(const struct lu_env *env, struct cl_io *io)
546 {
547         const struct cl_io_slice *scan;
548         int result = 0;
549
550         LINVRNT(cl_io_is_loopable(io));
551         LINVRNT(io->ci_state == CIS_LOCKED);
552         LINVRNT(cl_io_invariant(io));
553
554         io->ci_state = CIS_IO_GOING;
555         cl_io_for_each(scan, io) {
556                 if (!scan->cis_iop->op[io->ci_type].cio_start)
557                         continue;
558                 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
559                 if (result != 0)
560                         break;
561         }
562         if (result >= 0)
563                 result = 0;
564         return result;
565 }
566 EXPORT_SYMBOL(cl_io_start);
567
568 /**
569  * Wait until current io iteration is finished by calling
570  * cl_io_operations::cio_end() bottom-to-top.
571  */
572 void cl_io_end(const struct lu_env *env, struct cl_io *io)
573 {
574         const struct cl_io_slice *scan;
575
576         LINVRNT(cl_io_is_loopable(io));
577         LINVRNT(io->ci_state == CIS_IO_GOING);
578         LINVRNT(cl_io_invariant(io));
579
580         cl_io_for_each_reverse(scan, io) {
581                 if (scan->cis_iop->op[io->ci_type].cio_end)
582                         scan->cis_iop->op[io->ci_type].cio_end(env, scan);
583                 /* TODO: error handling. */
584         }
585         io->ci_state = CIS_IO_FINISHED;
586 }
587 EXPORT_SYMBOL(cl_io_end);
588
589 /**
590  * Called by read io, to decide the readahead extent
591  *
592  * \see cl_io_operations::cio_read_ahead()
593  */
594 int cl_io_read_ahead(const struct lu_env *env, struct cl_io *io,
595                      pgoff_t start, struct cl_read_ahead *ra)
596 {
597         const struct cl_io_slice *scan;
598         int                    result = 0;
599
600         LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
601         LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
602         LINVRNT(cl_io_invariant(io));
603
604         cl_io_for_each(scan, io) {
605                 if (!scan->cis_iop->cio_read_ahead)
606                         continue;
607
608                 result = scan->cis_iop->cio_read_ahead(env, scan, start, ra);
609                 if (result)
610                         break;
611         }
612         return result > 0 ? 0 : result;
613 }
614 EXPORT_SYMBOL(cl_io_read_ahead);
615
616 /**
617  * Commit a list of contiguous pages into writeback cache.
618  *
619  * \returns 0 if all pages committed, or errcode if error occurred.
620  * \see cl_io_operations::cio_commit_async()
621  */
622 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
623                        struct cl_page_list *queue, int from, int to,
624                        cl_commit_cbt cb)
625 {
626         const struct cl_io_slice *scan;
627         int result = 0;
628
629         cl_io_for_each(scan, io) {
630                 if (!scan->cis_iop->cio_commit_async)
631                         continue;
632                 result = scan->cis_iop->cio_commit_async(env, scan, queue,
633                                                          from, to, cb);
634                 if (result != 0)
635                         break;
636         }
637         return result;
638 }
639 EXPORT_SYMBOL(cl_io_commit_async);
640
641 /**
642  * Submits a list of pages for immediate io.
643  *
644  * After the function gets returned, The submitted pages are moved to
645  * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
646  * to be submitted, and the pages are errant to submit.
647  *
648  * \returns 0 if at least one page was submitted, error code otherwise.
649  * \see cl_io_operations::cio_submit()
650  */
651 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
652                     enum cl_req_type crt, struct cl_2queue *queue)
653 {
654         const struct cl_io_slice *scan;
655         int result = 0;
656
657         cl_io_for_each(scan, io) {
658                 if (!scan->cis_iop->cio_submit)
659                         continue;
660                 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
661                 if (result != 0)
662                         break;
663         }
664         /*
665          * If ->cio_submit() failed, no pages were sent.
666          */
667         LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
668         return result;
669 }
670 EXPORT_SYMBOL(cl_io_submit_rw);
671
672 static void cl_page_list_assume(const struct lu_env *env,
673                                 struct cl_io *io, struct cl_page_list *plist);
674
675 /**
676  * Submit a sync_io and wait for the IO to be finished, or error happens.
677  * If \a timeout is zero, it means to wait for the IO unconditionally.
678  */
679 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
680                       enum cl_req_type iot, struct cl_2queue *queue,
681                       long timeout)
682 {
683         struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
684         struct cl_page *pg;
685         int rc;
686
687         cl_page_list_for_each(pg, &queue->c2_qin) {
688                 LASSERT(!pg->cp_sync_io);
689                 pg->cp_sync_io = anchor;
690         }
691
692         cl_sync_io_init(anchor, queue->c2_qin.pl_nr, &cl_sync_io_end);
693         rc = cl_io_submit_rw(env, io, iot, queue);
694         if (rc == 0) {
695                 /*
696                  * If some pages weren't sent for any reason (e.g.,
697                  * read found up-to-date pages in the cache, or write found
698                  * clean pages), count them as completed to avoid infinite
699                  * wait.
700                  */
701                 cl_page_list_for_each(pg, &queue->c2_qin) {
702                         pg->cp_sync_io = NULL;
703                         cl_sync_io_note(env, anchor, 1);
704                 }
705
706                 /* wait for the IO to be finished. */
707                 rc = cl_sync_io_wait(env, anchor, timeout);
708                 cl_page_list_assume(env, io, &queue->c2_qout);
709         } else {
710                 LASSERT(list_empty(&queue->c2_qout.pl_pages));
711                 cl_page_list_for_each(pg, &queue->c2_qin)
712                         pg->cp_sync_io = NULL;
713         }
714         return rc;
715 }
716 EXPORT_SYMBOL(cl_io_submit_sync);
717
718 /**
719  * Main io loop.
720  *
721  * Pumps io through iterations calling
722  *
723  *    - cl_io_iter_init()
724  *
725  *    - cl_io_lock()
726  *
727  *    - cl_io_start()
728  *
729  *    - cl_io_end()
730  *
731  *    - cl_io_unlock()
732  *
733  *    - cl_io_iter_fini()
734  *
735  * repeatedly until there is no more io to do.
736  */
737 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
738 {
739         int result   = 0;
740
741         LINVRNT(cl_io_is_loopable(io));
742
743         do {
744                 size_t nob;
745
746                 io->ci_continue = 0;
747                 result = cl_io_iter_init(env, io);
748                 if (result == 0) {
749                         nob    = io->ci_nob;
750                         result = cl_io_lock(env, io);
751                         if (result == 0) {
752                                 /*
753                                  * Notify layers that locks has been taken,
754                                  * and do actual i/o.
755                                  *
756                                  *   - llite: kms, short read;
757                                  *   - llite: generic_file_read();
758                                  */
759                                 result = cl_io_start(env, io);
760                                 /*
761                                  * Send any remaining pending
762                                  * io, etc.
763                                  *
764                                  *   - llite: ll_rw_stats_tally.
765                                  */
766                                 cl_io_end(env, io);
767                                 cl_io_unlock(env, io);
768                                 cl_io_rw_advance(env, io, io->ci_nob - nob);
769                         }
770                 }
771                 cl_io_iter_fini(env, io);
772         } while (result == 0 && io->ci_continue);
773         if (result == 0)
774                 result = io->ci_result;
775         return result < 0 ? result : 0;
776 }
777 EXPORT_SYMBOL(cl_io_loop);
778
779 /**
780  * Adds io slice to the cl_io.
781  *
782  * This is called by cl_object_operations::coo_io_init() methods to add a
783  * per-layer state to the io. New state is added at the end of
784  * cl_io::ci_layers list, that is, it is at the bottom of the stack.
785  *
786  * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
787  */
788 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
789                      struct cl_object *obj,
790                      const struct cl_io_operations *ops)
791 {
792         struct list_head *linkage = &slice->cis_linkage;
793
794         LASSERT((!linkage->prev && !linkage->next) ||
795                 list_empty(linkage));
796
797         list_add_tail(linkage, &io->ci_layers);
798         slice->cis_io  = io;
799         slice->cis_obj = obj;
800         slice->cis_iop = ops;
801 }
802 EXPORT_SYMBOL(cl_io_slice_add);
803
804 /**
805  * Initializes page list.
806  */
807 void cl_page_list_init(struct cl_page_list *plist)
808 {
809         plist->pl_nr = 0;
810         INIT_LIST_HEAD(&plist->pl_pages);
811         plist->pl_owner = current;
812 }
813 EXPORT_SYMBOL(cl_page_list_init);
814
815 /**
816  * Adds a page to a page list.
817  */
818 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
819 {
820         /* it would be better to check that page is owned by "current" io, but
821          * it is not passed here.
822          */
823         LASSERT(page->cp_owner);
824         LINVRNT(plist->pl_owner == current);
825
826         LASSERT(list_empty(&page->cp_batch));
827         list_add_tail(&page->cp_batch, &plist->pl_pages);
828         ++plist->pl_nr;
829         lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
830         cl_page_get(page);
831 }
832 EXPORT_SYMBOL(cl_page_list_add);
833
834 /**
835  * Removes a page from a page list.
836  */
837 void cl_page_list_del(const struct lu_env *env, struct cl_page_list *plist,
838                       struct cl_page *page)
839 {
840         LASSERT(plist->pl_nr > 0);
841         LASSERT(cl_page_is_vmlocked(env, page));
842         LINVRNT(plist->pl_owner == current);
843
844         list_del_init(&page->cp_batch);
845         --plist->pl_nr;
846         lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
847         cl_page_put(env, page);
848 }
849 EXPORT_SYMBOL(cl_page_list_del);
850
851 /**
852  * Moves a page from one page list to another.
853  */
854 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
855                        struct cl_page *page)
856 {
857         LASSERT(src->pl_nr > 0);
858         LINVRNT(dst->pl_owner == current);
859         LINVRNT(src->pl_owner == current);
860
861         list_move_tail(&page->cp_batch, &dst->pl_pages);
862         --src->pl_nr;
863         ++dst->pl_nr;
864         lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
865                       src, dst);
866 }
867 EXPORT_SYMBOL(cl_page_list_move);
868
869 /**
870  * Moves a page from one page list to the head of another list.
871  */
872 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
873                             struct cl_page *page)
874 {
875         LASSERT(src->pl_nr > 0);
876         LINVRNT(dst->pl_owner == current);
877         LINVRNT(src->pl_owner == current);
878
879         list_move(&page->cp_batch, &dst->pl_pages);
880         --src->pl_nr;
881         ++dst->pl_nr;
882         lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
883                       src, dst);
884 }
885 EXPORT_SYMBOL(cl_page_list_move_head);
886
887 /**
888  * splice the cl_page_list, just as list head does
889  */
890 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
891 {
892         struct cl_page *page;
893         struct cl_page *tmp;
894
895         LINVRNT(list->pl_owner == current);
896         LINVRNT(head->pl_owner == current);
897
898         cl_page_list_for_each_safe(page, tmp, list)
899                 cl_page_list_move(head, list, page);
900 }
901 EXPORT_SYMBOL(cl_page_list_splice);
902
903
904 /**
905  * Disowns pages in a queue.
906  */
907 void cl_page_list_disown(const struct lu_env *env,
908                          struct cl_io *io, struct cl_page_list *plist)
909 {
910         struct cl_page *page;
911         struct cl_page *temp;
912
913         LINVRNT(plist->pl_owner == current);
914
915         cl_page_list_for_each_safe(page, temp, plist) {
916                 LASSERT(plist->pl_nr > 0);
917
918                 list_del_init(&page->cp_batch);
919                 --plist->pl_nr;
920                 /*
921                  * cl_page_disown0 rather than usual cl_page_disown() is used,
922                  * because pages are possibly in CPS_FREEING state already due
923                  * to the call to cl_page_list_discard().
924                  */
925                 /*
926                  * XXX cl_page_disown0() will fail if page is not locked.
927                  */
928                 cl_page_disown0(env, io, page);
929                 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
930                               plist);
931                 cl_page_put(env, page);
932         }
933 }
934 EXPORT_SYMBOL(cl_page_list_disown);
935
936 /**
937  * Releases pages from queue.
938  */
939 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
940 {
941         struct cl_page *page;
942         struct cl_page *temp;
943
944         LINVRNT(plist->pl_owner == current);
945
946         cl_page_list_for_each_safe(page, temp, plist)
947                 cl_page_list_del(env, plist, page);
948         LASSERT(plist->pl_nr == 0);
949 }
950 EXPORT_SYMBOL(cl_page_list_fini);
951
952 /**
953  * Assumes all pages in a queue.
954  */
955 static void cl_page_list_assume(const struct lu_env *env,
956                                 struct cl_io *io, struct cl_page_list *plist)
957 {
958         struct cl_page *page;
959
960         LINVRNT(plist->pl_owner == current);
961
962         cl_page_list_for_each(page, plist)
963                 cl_page_assume(env, io, page);
964 }
965
966 /**
967  * Discards all pages in a queue.
968  */
969 static void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
970                                  struct cl_page_list *plist)
971 {
972         struct cl_page *page;
973
974         LINVRNT(plist->pl_owner == current);
975         cl_page_list_for_each(page, plist)
976                 cl_page_discard(env, io, page);
977 }
978
979 /**
980  * Initialize dual page queue.
981  */
982 void cl_2queue_init(struct cl_2queue *queue)
983 {
984         cl_page_list_init(&queue->c2_qin);
985         cl_page_list_init(&queue->c2_qout);
986 }
987 EXPORT_SYMBOL(cl_2queue_init);
988
989 /**
990  * Disown pages in both lists of a 2-queue.
991  */
992 void cl_2queue_disown(const struct lu_env *env,
993                       struct cl_io *io, struct cl_2queue *queue)
994 {
995         cl_page_list_disown(env, io, &queue->c2_qin);
996         cl_page_list_disown(env, io, &queue->c2_qout);
997 }
998 EXPORT_SYMBOL(cl_2queue_disown);
999
1000 /**
1001  * Discard (truncate) pages in both lists of a 2-queue.
1002  */
1003 void cl_2queue_discard(const struct lu_env *env,
1004                        struct cl_io *io, struct cl_2queue *queue)
1005 {
1006         cl_page_list_discard(env, io, &queue->c2_qin);
1007         cl_page_list_discard(env, io, &queue->c2_qout);
1008 }
1009 EXPORT_SYMBOL(cl_2queue_discard);
1010
1011 /**
1012  * Finalize both page lists of a 2-queue.
1013  */
1014 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1015 {
1016         cl_page_list_fini(env, &queue->c2_qout);
1017         cl_page_list_fini(env, &queue->c2_qin);
1018 }
1019 EXPORT_SYMBOL(cl_2queue_fini);
1020
1021 /**
1022  * Initialize a 2-queue to contain \a page in its incoming page list.
1023  */
1024 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1025 {
1026         cl_2queue_init(queue);
1027         /*
1028          * Add a page to the incoming page list of 2-queue.
1029          */
1030         cl_page_list_add(&queue->c2_qin, page);
1031 }
1032 EXPORT_SYMBOL(cl_2queue_init_page);
1033
1034 /**
1035  * Returns top-level io.
1036  *
1037  * \see cl_object_top()
1038  */
1039 struct cl_io *cl_io_top(struct cl_io *io)
1040 {
1041         while (io->ci_parent)
1042                 io = io->ci_parent;
1043         return io;
1044 }
1045 EXPORT_SYMBOL(cl_io_top);
1046
1047 /**
1048  * Fills in attributes that are passed to server together with transfer. Only
1049  * attributes from \a flags may be touched. This can be called multiple times
1050  * for the same request.
1051  */
1052 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1053                      struct cl_req_attr *attr)
1054 {
1055         struct cl_object *scan;
1056
1057         cl_object_for_each(scan, obj) {
1058                 if (scan->co_ops->coo_req_attr_set)
1059                         scan->co_ops->coo_req_attr_set(env, scan, attr);
1060         }
1061 }
1062 EXPORT_SYMBOL(cl_req_attr_set);
1063
1064 /* cl_sync_io_callback assumes the caller must call cl_sync_io_wait() to
1065  * wait for the IO to finish.
1066  */
1067 void cl_sync_io_end(const struct lu_env *env, struct cl_sync_io *anchor)
1068 {
1069         wake_up_all(&anchor->csi_waitq);
1070
1071         /* it's safe to nuke or reuse anchor now */
1072         atomic_set(&anchor->csi_barrier, 0);
1073 }
1074 EXPORT_SYMBOL(cl_sync_io_end);
1075
1076 /**
1077  * Initialize synchronous io wait anchor
1078  */
1079 void cl_sync_io_init(struct cl_sync_io *anchor, int nr,
1080                      void (*end)(const struct lu_env *, struct cl_sync_io *))
1081 {
1082         memset(anchor, 0, sizeof(*anchor));
1083         init_waitqueue_head(&anchor->csi_waitq);
1084         atomic_set(&anchor->csi_sync_nr, nr);
1085         atomic_set(&anchor->csi_barrier, nr > 0);
1086         anchor->csi_sync_rc = 0;
1087         anchor->csi_end_io = end;
1088         LASSERT(end);
1089 }
1090 EXPORT_SYMBOL(cl_sync_io_init);
1091
1092 /**
1093  * Wait until all IO completes. Transfer completion routine has to call
1094  * cl_sync_io_note() for every entity.
1095  */
1096 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1097                     long timeout)
1098 {
1099         struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1100                                                   NULL, NULL, NULL);
1101         int rc;
1102
1103         LASSERT(timeout >= 0);
1104
1105         rc = l_wait_event(anchor->csi_waitq,
1106                           atomic_read(&anchor->csi_sync_nr) == 0,
1107                           &lwi);
1108         if (rc < 0) {
1109                 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1110                        rc, atomic_read(&anchor->csi_sync_nr));
1111
1112                 lwi = (struct l_wait_info) { 0 };
1113                 (void)l_wait_event(anchor->csi_waitq,
1114                                    atomic_read(&anchor->csi_sync_nr) == 0,
1115                                    &lwi);
1116         } else {
1117                 rc = anchor->csi_sync_rc;
1118         }
1119         LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1120
1121         /* wait until cl_sync_io_note() has done wakeup */
1122         while (unlikely(atomic_read(&anchor->csi_barrier) != 0))
1123                 cpu_relax();
1124
1125
1126         return rc;
1127 }
1128 EXPORT_SYMBOL(cl_sync_io_wait);
1129
1130 /**
1131  * Indicate that transfer of a single page completed.
1132  */
1133 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1134                      int ioret)
1135 {
1136         if (anchor->csi_sync_rc == 0 && ioret < 0)
1137                 anchor->csi_sync_rc = ioret;
1138         /*
1139          * Synchronous IO done without releasing page lock (e.g., as a part of
1140          * ->{prepare,commit}_write(). Completion is used to signal the end of
1141          * IO.
1142          */
1143         LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1144         if (atomic_dec_and_test(&anchor->csi_sync_nr)) {
1145                 LASSERT(anchor->csi_end_io);
1146                 anchor->csi_end_io(env, anchor);
1147                 /* Can't access anchor any more */
1148         }
1149 }
1150 EXPORT_SYMBOL(cl_sync_io_note);