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