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