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