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