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