Whamcloud - gitweb
LU-13799 llite: Adjust dio refcounting
[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 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  * Called before io start, to reserve enough LRU slots to avoid
587  * deadlock.
588  *
589  * \see cl_io_operations::cio_lru_reserve()
590  */
591 int cl_io_lru_reserve(const struct lu_env *env, struct cl_io *io,
592                       loff_t pos, size_t bytes)
593 {
594         const struct cl_io_slice *scan;
595         int result = 0;
596
597         LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
598         LINVRNT(cl_io_invariant(io));
599         ENTRY;
600
601         list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
602                 if (scan->cis_iop->cio_lru_reserve) {
603                         result = scan->cis_iop->cio_lru_reserve(env, scan,
604                                                                 pos, bytes);
605                         if (result)
606                                 break;
607                 }
608         }
609
610         RETURN(result);
611 }
612 EXPORT_SYMBOL(cl_io_lru_reserve);
613
614 /**
615  * Commit a list of contiguous pages into writeback cache.
616  *
617  * \returns 0 if all pages committed, or errcode if error occurred.
618  * \see cl_io_operations::cio_commit_async()
619  */
620 int cl_io_commit_async(const struct lu_env *env, struct cl_io *io,
621                        struct cl_page_list *queue, int from, int to,
622                        cl_commit_cbt cb)
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_commit_async == NULL)
630                         continue;
631                 result = scan->cis_iop->cio_commit_async(env, scan, queue,
632                                                          from, to, cb);
633                 if (result != 0)
634                         break;
635         }
636         RETURN(result);
637 }
638 EXPORT_SYMBOL(cl_io_commit_async);
639
640 void cl_io_extent_release(const struct lu_env *env, struct cl_io *io)
641 {
642         const struct cl_io_slice *scan;
643         ENTRY;
644
645         list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
646                 if (scan->cis_iop->cio_extent_release == NULL)
647                         continue;
648                 scan->cis_iop->cio_extent_release(env, scan);
649         }
650         EXIT;
651 }
652 EXPORT_SYMBOL(cl_io_extent_release);
653
654 /**
655  * Submits a list of pages for immediate io.
656  *
657  * After the function gets returned, The submitted pages are moved to
658  * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
659  * to be submitted, and the pages are errant to submit.
660  *
661  * \returns 0 if at least one page was submitted, error code otherwise.
662  * \see cl_io_operations::cio_submit()
663  */
664 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
665                     enum cl_req_type crt, struct cl_2queue *queue)
666 {
667         const struct cl_io_slice *scan;
668         int result = 0;
669         ENTRY;
670
671         list_for_each_entry(scan, &io->ci_layers, cis_linkage) {
672                 if (scan->cis_iop->cio_submit == NULL)
673                         continue;
674                 result = scan->cis_iop->cio_submit(env, scan, crt, queue);
675                 if (result != 0)
676                         break;
677         }
678         /*
679          * If ->cio_submit() failed, no pages were sent.
680          */
681         LASSERT(ergo(result != 0, list_empty(&queue->c2_qout.pl_pages)));
682         RETURN(result);
683 }
684 EXPORT_SYMBOL(cl_io_submit_rw);
685
686 /**
687  * Submit a sync_io and wait for the IO to be finished, or error happens.
688  * If \a timeout is zero, it means to wait for the IO unconditionally.
689  */
690 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
691                       enum cl_req_type iot, struct cl_2queue *queue,
692                       long timeout)
693 {
694         struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
695         struct cl_page *pg;
696         int rc;
697         ENTRY;
698
699         cl_page_list_for_each(pg, &queue->c2_qin) {
700                 LASSERT(pg->cp_sync_io == NULL);
701                 pg->cp_sync_io = anchor;
702         }
703
704         cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
705         rc = cl_io_submit_rw(env, io, iot, queue);
706         if (rc == 0) {
707                 /*
708                  * If some pages weren't sent for any reason (e.g.,
709                  * read found up-to-date pages in the cache, or write found
710                  * clean pages), count them as completed to avoid infinite
711                  * wait.
712                  */
713                 cl_page_list_for_each(pg, &queue->c2_qin) {
714                         pg->cp_sync_io = NULL;
715                         cl_sync_io_note(env, anchor, 1);
716                 }
717
718                 /* wait for the IO to be finished. */
719                 rc = cl_sync_io_wait(env, anchor, timeout);
720                 cl_page_list_assume(env, io, &queue->c2_qout);
721         } else {
722                 LASSERT(list_empty(&queue->c2_qout.pl_pages));
723                 cl_page_list_for_each(pg, &queue->c2_qin)
724                         pg->cp_sync_io = NULL;
725         }
726         RETURN(rc);
727 }
728 EXPORT_SYMBOL(cl_io_submit_sync);
729
730 /**
731  * Main io loop.
732  *
733  * Pumps io through iterations calling
734  *
735  *    - cl_io_iter_init()
736  *
737  *    - cl_io_lock()
738  *
739  *    - cl_io_start()
740  *
741  *    - cl_io_end()
742  *
743  *    - cl_io_unlock()
744  *
745  *    - cl_io_iter_fini()
746  *
747  * repeatedly until there is no more io to do.
748  */
749 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
750 {
751         int result = 0;
752         int rc = 0;
753
754         LINVRNT(cl_io_is_loopable(io));
755         ENTRY;
756
757         do {
758                 size_t nob;
759
760                 io->ci_continue = 0;
761                 result = cl_io_iter_init(env, io);
762                 if (result == 0) {
763                         nob    = io->ci_nob;
764                         result = cl_io_lock(env, io);
765                         if (result == 0) {
766                                 /*
767                                  * Notify layers that locks has been taken,
768                                  * and do actual i/o.
769                                  *
770                                  *   - llite: kms, short read;
771                                  *   - llite: generic_file_read();
772                                  */
773                                 result = cl_io_start(env, io);
774                                 /*
775                                  * Send any remaining pending
776                                  * io, etc.
777                                  *
778                                  **   - llite: ll_rw_stats_tally.
779                                  */
780                                 cl_io_end(env, io);
781                                 cl_io_unlock(env, io);
782                                 cl_io_rw_advance(env, io, io->ci_nob - nob);
783                         }
784                 }
785                 cl_io_iter_fini(env, io);
786                 if (result)
787                         rc = result;
788         } while ((result == 0 || result == -EIOCBQUEUED) &&
789                  io->ci_continue);
790
791         if (rc && !result)
792                 result = rc;
793
794         if (result == -EAGAIN && io->ci_ndelay) {
795                 io->ci_need_restart = 1;
796                 result = 0;
797         }
798
799         if (result == 0)
800                 result = io->ci_result;
801         RETURN(result < 0 ? result : 0);
802 }
803 EXPORT_SYMBOL(cl_io_loop);
804
805 /**
806  * Adds io slice to the cl_io.
807  *
808  * This is called by cl_object_operations::coo_io_init() methods to add a
809  * per-layer state to the io. New state is added at the end of
810  * cl_io::ci_layers list, that is, it is at the bottom of the stack.
811  *
812  * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
813  */
814 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
815                      struct cl_object *obj,
816                      const struct cl_io_operations *ops)
817 {
818         struct list_head *linkage = &slice->cis_linkage;
819
820         LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
821                 list_empty(linkage));
822         ENTRY;
823
824         list_add_tail(linkage, &io->ci_layers);
825         slice->cis_io  = io;
826         slice->cis_obj = obj;
827         slice->cis_iop = ops;
828         EXIT;
829 }
830 EXPORT_SYMBOL(cl_io_slice_add);
831
832
833 /**
834  * Initializes page list.
835  */
836 void cl_page_list_init(struct cl_page_list *plist)
837 {
838         ENTRY;
839         plist->pl_nr = 0;
840         INIT_LIST_HEAD(&plist->pl_pages);
841         EXIT;
842 }
843 EXPORT_SYMBOL(cl_page_list_init);
844
845 /**
846  * Adds a page to a page list.
847  */
848 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page,
849                       bool get_ref)
850 {
851         ENTRY;
852         /* it would be better to check that page is owned by "current" io, but
853          * it is not passed here. */
854         LASSERT(page->cp_owner != NULL);
855
856         LASSERT(list_empty(&page->cp_batch));
857         list_add_tail(&page->cp_batch, &plist->pl_pages);
858         ++plist->pl_nr;
859         lu_ref_add_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
860         if (get_ref)
861                 cl_page_get(page);
862         EXIT;
863 }
864 EXPORT_SYMBOL(cl_page_list_add);
865
866 /**
867  * Removes a page from a page list.
868  */
869 void cl_page_list_del(const struct lu_env *env,
870                       struct cl_page_list *plist, struct cl_page *page)
871 {
872         LASSERT(plist->pl_nr > 0);
873         LASSERT(cl_page_is_vmlocked(env, page));
874
875         ENTRY;
876         list_del_init(&page->cp_batch);
877         --plist->pl_nr;
878         lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue", plist);
879         cl_page_put(env, page);
880         EXIT;
881 }
882 EXPORT_SYMBOL(cl_page_list_del);
883
884 /**
885  * Moves a page from one page list to another.
886  */
887 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
888                        struct cl_page *page)
889 {
890         LASSERT(src->pl_nr > 0);
891
892         ENTRY;
893         list_move_tail(&page->cp_batch, &dst->pl_pages);
894         --src->pl_nr;
895         ++dst->pl_nr;
896         lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
897                       src, dst);
898         EXIT;
899 }
900 EXPORT_SYMBOL(cl_page_list_move);
901
902 /**
903  * Moves a page from one page list to the head of another list.
904  */
905 void cl_page_list_move_head(struct cl_page_list *dst, struct cl_page_list *src,
906                             struct cl_page *page)
907 {
908         LASSERT(src->pl_nr > 0);
909
910         ENTRY;
911         list_move(&page->cp_batch, &dst->pl_pages);
912         --src->pl_nr;
913         ++dst->pl_nr;
914         lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref, "queue",
915                         src, dst);
916         EXIT;
917 }
918 EXPORT_SYMBOL(cl_page_list_move_head);
919
920 /**
921  * splice the cl_page_list, just as list head does
922  */
923 void cl_page_list_splice(struct cl_page_list *src, struct cl_page_list *dst)
924 {
925 #ifdef USE_LU_REF
926         struct cl_page *page;
927         struct cl_page *tmp;
928
929
930         ENTRY;
931         cl_page_list_for_each_safe(page, tmp, list)
932                 lu_ref_set_at(&page->cp_reference, &page->cp_queue_ref,
933                               "queue", src, dst);
934 #else
935         ENTRY;
936 #endif
937         dst->pl_nr += src->pl_nr;
938         src->pl_nr = 0;
939         list_splice_tail_init(&src->pl_pages, &dst->pl_pages);
940
941         EXIT;
942 }
943 EXPORT_SYMBOL(cl_page_list_splice);
944
945 /**
946  * Disowns pages in a queue.
947  */
948 void cl_page_list_disown(const struct lu_env *env,
949                          struct cl_io *io, struct cl_page_list *plist)
950 {
951         struct cl_page *page;
952         struct cl_page *temp;
953
954
955         ENTRY;
956         cl_page_list_for_each_safe(page, temp, plist) {
957                 LASSERT(plist->pl_nr > 0);
958
959                 list_del_init(&page->cp_batch);
960                 --plist->pl_nr;
961                 /*
962                  * cl_page_disown0 rather than usual cl_page_disown() is used,
963                  * because pages are possibly in CPS_FREEING state already due
964                  * to the call to cl_page_list_discard().
965                  */
966                 /*
967                  * XXX cl_page_disown0() will fail if page is not locked.
968                  */
969                 cl_page_disown0(env, io, page);
970                 lu_ref_del_at(&page->cp_reference, &page->cp_queue_ref, "queue",
971                               plist);
972                 cl_page_put(env, page);
973         }
974         EXIT;
975 }
976 EXPORT_SYMBOL(cl_page_list_disown);
977
978 /**
979  * Releases pages from queue.
980  */
981 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
982 {
983         struct cl_page *page;
984         struct cl_page *temp;
985
986
987         ENTRY;
988         cl_page_list_for_each_safe(page, temp, plist)
989                 cl_page_list_del(env, plist, page);
990         LASSERT(plist->pl_nr == 0);
991         EXIT;
992 }
993 EXPORT_SYMBOL(cl_page_list_fini);
994
995 /**
996  * Assumes all pages in a queue.
997  */
998 void cl_page_list_assume(const struct lu_env *env,
999                          struct cl_io *io, struct cl_page_list *plist)
1000 {
1001         struct cl_page *page;
1002
1003
1004         cl_page_list_for_each(page, plist)
1005                 cl_page_assume(env, io, page);
1006 }
1007
1008 /**
1009  * Discards all pages in a queue.
1010  */
1011 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1012                           struct cl_page_list *plist)
1013 {
1014         struct cl_page *page;
1015
1016         ENTRY;
1017         cl_page_list_for_each(page, plist)
1018                 cl_page_discard(env, io, page);
1019         EXIT;
1020 }
1021 EXPORT_SYMBOL(cl_page_list_discard);
1022
1023 /**
1024  * Initialize dual page queue.
1025  */
1026 void cl_2queue_init(struct cl_2queue *queue)
1027 {
1028         ENTRY;
1029         cl_page_list_init(&queue->c2_qin);
1030         cl_page_list_init(&queue->c2_qout);
1031         EXIT;
1032 }
1033 EXPORT_SYMBOL(cl_2queue_init);
1034
1035 /**
1036  * Add a page to the incoming page list of 2-queue.
1037  */
1038 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page, bool get_ref)
1039 {
1040         cl_page_list_add(&queue->c2_qin, page, get_ref);
1041 }
1042 EXPORT_SYMBOL(cl_2queue_add);
1043
1044 /**
1045  * Disown pages in both lists of a 2-queue.
1046  */
1047 void cl_2queue_disown(const struct lu_env *env,
1048                       struct cl_io *io, struct cl_2queue *queue)
1049 {
1050         ENTRY;
1051         cl_page_list_disown(env, io, &queue->c2_qin);
1052         cl_page_list_disown(env, io, &queue->c2_qout);
1053         EXIT;
1054 }
1055 EXPORT_SYMBOL(cl_2queue_disown);
1056
1057 /**
1058  * Discard (truncate) pages in both lists of a 2-queue.
1059  */
1060 void cl_2queue_discard(const struct lu_env *env,
1061                        struct cl_io *io, struct cl_2queue *queue)
1062 {
1063         ENTRY;
1064         cl_page_list_discard(env, io, &queue->c2_qin);
1065         cl_page_list_discard(env, io, &queue->c2_qout);
1066         EXIT;
1067 }
1068 EXPORT_SYMBOL(cl_2queue_discard);
1069
1070 /**
1071  * Assume to own the pages in cl_2queue
1072  */
1073 void cl_2queue_assume(const struct lu_env *env,
1074                       struct cl_io *io, struct cl_2queue *queue)
1075 {
1076         cl_page_list_assume(env, io, &queue->c2_qin);
1077         cl_page_list_assume(env, io, &queue->c2_qout);
1078 }
1079
1080 /**
1081  * Finalize both page lists of a 2-queue.
1082  */
1083 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1084 {
1085         ENTRY;
1086         cl_page_list_fini(env, &queue->c2_qout);
1087         cl_page_list_fini(env, &queue->c2_qin);
1088         EXIT;
1089 }
1090 EXPORT_SYMBOL(cl_2queue_fini);
1091
1092 /**
1093  * Initialize a 2-queue to contain \a page in its incoming page list.
1094  */
1095 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1096 {
1097         ENTRY;
1098         cl_2queue_init(queue);
1099         cl_2queue_add(queue, page, true);
1100         EXIT;
1101 }
1102 EXPORT_SYMBOL(cl_2queue_init_page);
1103
1104 /**
1105  * Returns top-level io.
1106  *
1107  * \see cl_object_top()
1108  */
1109 struct cl_io *cl_io_top(struct cl_io *io)
1110 {
1111         ENTRY;
1112         while (io->ci_parent != NULL)
1113                 io = io->ci_parent;
1114         RETURN(io);
1115 }
1116 EXPORT_SYMBOL(cl_io_top);
1117
1118 /**
1119  * Prints human readable representation of \a io to the \a f.
1120  */
1121 void cl_io_print(const struct lu_env *env, void *cookie,
1122                  lu_printer_t printer, const struct cl_io *io)
1123 {
1124 }
1125
1126 /**
1127  * Fills in attributes that are passed to server together with transfer. Only
1128  * attributes from \a flags may be touched. This can be called multiple times
1129  * for the same request.
1130  */
1131 void cl_req_attr_set(const struct lu_env *env, struct cl_object *obj,
1132                      struct cl_req_attr *attr)
1133 {
1134         struct cl_object *scan;
1135         ENTRY;
1136
1137         cl_object_for_each(scan, obj) {
1138                 if (scan->co_ops->coo_req_attr_set != NULL)
1139                         scan->co_ops->coo_req_attr_set(env, scan, attr);
1140         }
1141         EXIT;
1142 }
1143 EXPORT_SYMBOL(cl_req_attr_set);
1144
1145 /**
1146  * Initialize synchronous io wait \a anchor for \a nr pages with optional
1147  * \a end handler.
1148  * \param anchor owned by caller, initialzied here.
1149  * \param nr number of pages initally pending in sync.
1150  * \param end optional callback sync_io completion, can be used to
1151  *  trigger erasure coding, integrity, dedupe, or similar operation.
1152  * \q end is called with a spinlock on anchor->csi_waitq.lock
1153  */
1154
1155 void cl_sync_io_init_notify(struct cl_sync_io *anchor, int nr,
1156                             struct cl_dio_aio *aio, cl_sync_io_end_t *end)
1157 {
1158         ENTRY;
1159         memset(anchor, 0, sizeof(*anchor));
1160         init_waitqueue_head(&anchor->csi_waitq);
1161         atomic_set(&anchor->csi_sync_nr, nr);
1162         anchor->csi_sync_rc = 0;
1163         anchor->csi_end_io = end;
1164         anchor->csi_aio = aio;
1165         EXIT;
1166 }
1167 EXPORT_SYMBOL(cl_sync_io_init_notify);
1168
1169 /**
1170  * Wait until all IO completes. Transfer completion routine has to call
1171  * cl_sync_io_note() for every entity.
1172  */
1173 int cl_sync_io_wait(const struct lu_env *env, struct cl_sync_io *anchor,
1174                     long timeout)
1175 {
1176         int rc = 0;
1177         ENTRY;
1178
1179         LASSERT(timeout >= 0);
1180
1181         if (timeout > 0 &&
1182             wait_event_idle_timeout(anchor->csi_waitq,
1183                                     atomic_read(&anchor->csi_sync_nr) == 0,
1184                                     cfs_time_seconds(timeout)) == 0) {
1185                 rc = -ETIMEDOUT;
1186                 CERROR("IO failed: %d, still wait for %d remaining entries\n",
1187                        rc, atomic_read(&anchor->csi_sync_nr));
1188         }
1189
1190         wait_event_idle(anchor->csi_waitq,
1191                         atomic_read(&anchor->csi_sync_nr) == 0);
1192         if (!rc)
1193                 rc = anchor->csi_sync_rc;
1194
1195         /* We take the lock to ensure that cl_sync_io_note() has finished */
1196         spin_lock(&anchor->csi_waitq.lock);
1197         LASSERT(atomic_read(&anchor->csi_sync_nr) == 0);
1198         spin_unlock(&anchor->csi_waitq.lock);
1199
1200         RETURN(rc);
1201 }
1202 EXPORT_SYMBOL(cl_sync_io_wait);
1203
1204 #ifndef HAVE_AIO_COMPLETE
1205 static inline void aio_complete(struct kiocb *iocb, ssize_t res, ssize_t res2)
1206 {
1207         if (iocb->ki_complete)
1208                 iocb->ki_complete(iocb, res, res2);
1209 }
1210 #endif
1211
1212 static void cl_aio_end(const struct lu_env *env, struct cl_sync_io *anchor)
1213 {
1214         struct cl_dio_aio *aio = container_of(anchor, typeof(*aio), cda_sync);
1215         ssize_t ret = anchor->csi_sync_rc;
1216
1217         ENTRY;
1218
1219         /* release pages */
1220         while (aio->cda_pages.pl_nr > 0) {
1221                 struct cl_page *page = cl_page_list_first(&aio->cda_pages);
1222
1223                 cl_page_get(page);
1224                 cl_page_list_del(env, &aio->cda_pages, page);
1225                 cl_page_delete(env, page);
1226                 cl_page_put(env, page);
1227         }
1228
1229         if (!is_sync_kiocb(aio->cda_iocb) && !aio->cda_no_aio_complete)
1230                 aio_complete(aio->cda_iocb, ret ?: aio->cda_bytes, 0);
1231
1232         EXIT;
1233 }
1234
1235 struct cl_dio_aio *cl_aio_alloc(struct kiocb *iocb, struct cl_object *obj)
1236 {
1237         struct cl_dio_aio *aio;
1238
1239         OBD_SLAB_ALLOC_PTR_GFP(aio, cl_dio_aio_kmem, GFP_NOFS);
1240         if (aio != NULL) {
1241                 /*
1242                  * Hold one ref so that it won't be released until
1243                  * every pages is added.
1244                  */
1245                 cl_sync_io_init_notify(&aio->cda_sync, 1, is_sync_kiocb(iocb) ?
1246                                        NULL : aio, cl_aio_end);
1247                 cl_page_list_init(&aio->cda_pages);
1248                 aio->cda_iocb = iocb;
1249                 aio->cda_no_aio_complete = 0;
1250                 cl_object_get(obj);
1251                 aio->cda_obj = obj;
1252         }
1253         return aio;
1254 }
1255 EXPORT_SYMBOL(cl_aio_alloc);
1256
1257 void cl_aio_free(const struct lu_env *env, struct cl_dio_aio *aio)
1258 {
1259         if (aio) {
1260                 cl_object_put(env, aio->cda_obj);
1261                 OBD_SLAB_FREE_PTR(aio, cl_dio_aio_kmem);
1262         }
1263 }
1264 EXPORT_SYMBOL(cl_aio_free);
1265
1266
1267 /**
1268  * Indicate that transfer of a single page completed.
1269  */
1270 void cl_sync_io_note(const struct lu_env *env, struct cl_sync_io *anchor,
1271                      int ioret)
1272 {
1273         ENTRY;
1274         if (anchor->csi_sync_rc == 0 && ioret < 0)
1275                 anchor->csi_sync_rc = ioret;
1276         /*
1277          * Synchronous IO done without releasing page lock (e.g., as a part of
1278          * ->{prepare,commit}_write(). Completion is used to signal the end of
1279          * IO.
1280          */
1281         LASSERT(atomic_read(&anchor->csi_sync_nr) > 0);
1282         if (atomic_dec_and_lock(&anchor->csi_sync_nr,
1283                                 &anchor->csi_waitq.lock)) {
1284                 struct cl_dio_aio *aio = NULL;
1285
1286                 cl_sync_io_end_t *end_io = anchor->csi_end_io;
1287
1288                 /*
1289                  * Holding the lock across both the decrement and
1290                  * the wakeup ensures cl_sync_io_wait() doesn't complete
1291                  * before the wakeup completes and the contents of
1292                  * of anchor become unsafe to access as the owner is free
1293                  * to immediately reclaim anchor when cl_sync_io_wait()
1294                  * completes.
1295                  */
1296                 wake_up_locked(&anchor->csi_waitq);
1297                 if (end_io)
1298                         end_io(env, anchor);
1299                 if (anchor->csi_aio)
1300                         aio = anchor->csi_aio;
1301
1302                 spin_unlock(&anchor->csi_waitq.lock);
1303
1304                 /**
1305                  * If anchor->csi_aio is set, we are responsible for freeing
1306                  * memory here rather than when cl_sync_io_wait() completes.
1307                  */
1308                 cl_aio_free(env, aio);
1309         }
1310         EXIT;
1311 }
1312 EXPORT_SYMBOL(cl_sync_io_note);
1313
1314
1315 int cl_sync_io_wait_recycle(const struct lu_env *env, struct cl_sync_io *anchor,
1316                             long timeout, int ioret)
1317 {
1318         int rc = 0;
1319
1320         /*
1321          * @anchor was inited as 1 to prevent end_io to be
1322          * called before we add all pages for IO, so drop
1323          * one extra reference to make sure we could wait
1324          * count to be zero.
1325          */
1326         cl_sync_io_note(env, anchor, ioret);
1327         /* Wait for completion of normal dio.
1328          * This replaces the EIOCBQEUED return from the DIO/AIO
1329          * path, and this is where AIO and DIO implementations
1330          * split.
1331          */
1332         rc = cl_sync_io_wait(env, anchor, timeout);
1333         /**
1334          * One extra reference again, as if @anchor is
1335          * reused we assume it as 1 before using.
1336          */
1337         atomic_add(1, &anchor->csi_sync_nr);
1338
1339         return rc;
1340 }
1341 EXPORT_SYMBOL(cl_sync_io_wait_recycle);