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