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