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