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