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