Whamcloud - gitweb
aab7682f6b757f6fc709f8b3bae4e5558683ddf5
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Client IO.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42
43 #include <obd_class.h>
44 #include <obd_support.h>
45 #include <lustre_fid.h>
46 #include <libcfs/list.h>
47 #include <cl_object.h>
48 #include "cl_internal.h"
49
50 /*****************************************************************************
51  *
52  * cl_io interface.
53  *
54  */
55
56 #define cl_io_for_each(slice, io) \
57         cfs_list_for_each_entry((slice), &io->ci_layers, cis_linkage)
58 #define cl_io_for_each_reverse(slice, io)                 \
59         cfs_list_for_each_entry_reverse((slice), &io->ci_layers, cis_linkage)
60
61 static inline int cl_io_type_is_valid(enum cl_io_type type)
62 {
63         return CIT_READ <= type && type < CIT_OP_NR;
64 }
65
66 static inline int cl_io_is_loopable(const struct cl_io *io)
67 {
68         return cl_io_type_is_valid(io->ci_type) && io->ci_type != CIT_MISC;
69 }
70
71 /**
72  * Returns true iff there is an IO ongoing in the given environment.
73  */
74 int cl_io_is_going(const struct lu_env *env)
75 {
76         return cl_env_info(env)->clt_current_io != NULL;
77 }
78 EXPORT_SYMBOL(cl_io_is_going);
79
80 /**
81  * cl_io invariant that holds at all times when exported cl_io_*() functions
82  * are entered and left.
83  */
84 static int cl_io_invariant(const struct cl_io *io)
85 {
86         struct cl_io *up;
87
88         up = io->ci_parent;
89         return
90                 /*
91                  * io can own pages only when it is ongoing. Sub-io might
92                  * still be in CIS_LOCKED state when top-io is in
93                  * CIS_IO_GOING.
94                  */
95                 ergo(io->ci_owned_nr > 0, io->ci_state == CIS_IO_GOING ||
96                      (io->ci_state == CIS_LOCKED && up != NULL));
97 }
98
99 /**
100  * Finalize \a io, by calling cl_io_operations::cio_fini() bottom-to-top.
101  */
102 void cl_io_fini(const struct lu_env *env, struct cl_io *io)
103 {
104         struct cl_io_slice    *slice;
105         struct cl_thread_info *info;
106
107         LINVRNT(cl_io_type_is_valid(io->ci_type));
108         LINVRNT(cl_io_invariant(io));
109         ENTRY;
110
111         while (!cfs_list_empty(&io->ci_layers)) {
112                 slice = container_of(io->ci_layers.prev, struct cl_io_slice,
113                                      cis_linkage);
114                 cfs_list_del_init(&slice->cis_linkage);
115                 if (slice->cis_iop->op[io->ci_type].cio_fini != NULL)
116                         slice->cis_iop->op[io->ci_type].cio_fini(env, slice);
117                 /*
118                  * Invalidate slice to catch use after free. This assumes that
119                  * slices are allocated within session and can be touched
120                  * after ->cio_fini() returns.
121                  */
122                 slice->cis_io = NULL;
123         }
124         io->ci_state = CIS_FINI;
125         info = cl_env_info(env);
126         if (info->clt_current_io == io)
127                 info->clt_current_io = NULL;
128
129         /* sanity check for layout change */
130         switch(io->ci_type) {
131         case CIT_READ:
132         case CIT_WRITE:
133                 break;
134         case CIT_FAULT:
135         case CIT_FSYNC:
136                 LASSERT(!io->ci_need_restart);
137                 break;
138         case CIT_SETATTR:
139         case CIT_MISC:
140                 /* Check ignore layout change conf */
141                 LASSERT(ergo(io->ci_ignore_layout || !io->ci_verify_layout,
142                                 !io->ci_need_restart));
143                 break;
144         default:
145                 LBUG();
146         }
147         EXIT;
148 }
149 EXPORT_SYMBOL(cl_io_fini);
150
151 static int cl_io_init0(const struct lu_env *env, struct cl_io *io,
152                        enum cl_io_type iot, struct cl_object *obj)
153 {
154         struct cl_object *scan;
155         int result;
156
157         LINVRNT(io->ci_state == CIS_ZERO || io->ci_state == CIS_FINI);
158         LINVRNT(cl_io_type_is_valid(iot));
159         LINVRNT(cl_io_invariant(io));
160         ENTRY;
161
162         io->ci_type = iot;
163         CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_todo);
164         CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_curr);
165         CFS_INIT_LIST_HEAD(&io->ci_lockset.cls_done);
166         CFS_INIT_LIST_HEAD(&io->ci_layers);
167
168         result = 0;
169         cl_object_for_each(scan, obj) {
170                 if (scan->co_ops->coo_io_init != NULL) {
171                         result = scan->co_ops->coo_io_init(env, scan, io);
172                         if (result != 0)
173                                 break;
174                 }
175         }
176         if (result == 0)
177                 io->ci_state = CIS_INIT;
178         RETURN(result);
179 }
180
181 /**
182  * Initialize sub-io, by calling cl_io_operations::cio_init() top-to-bottom.
183  *
184  * \pre obj != cl_object_top(obj)
185  */
186 int cl_io_sub_init(const struct lu_env *env, struct cl_io *io,
187                    enum cl_io_type iot, struct cl_object *obj)
188 {
189         struct cl_thread_info *info = cl_env_info(env);
190
191         LASSERT(obj != cl_object_top(obj));
192         if (info->clt_current_io == NULL)
193                 info->clt_current_io = io;
194         return cl_io_init0(env, io, iot, obj);
195 }
196 EXPORT_SYMBOL(cl_io_sub_init);
197
198 /**
199  * Initialize \a io, by calling cl_io_operations::cio_init() top-to-bottom.
200  *
201  * Caller has to call cl_io_fini() after a call to cl_io_init(), no matter
202  * what the latter returned.
203  *
204  * \pre obj == cl_object_top(obj)
205  * \pre cl_io_type_is_valid(iot)
206  * \post cl_io_type_is_valid(io->ci_type) && io->ci_type == iot
207  */
208 int cl_io_init(const struct lu_env *env, struct cl_io *io,
209                enum cl_io_type iot, struct cl_object *obj)
210 {
211         struct cl_thread_info *info = cl_env_info(env);
212
213         LASSERT(obj == cl_object_top(obj));
214         LASSERT(info->clt_current_io == NULL);
215
216         info->clt_current_io = io;
217         return cl_io_init0(env, io, iot, obj);
218 }
219 EXPORT_SYMBOL(cl_io_init);
220
221 /**
222  * Initialize read or write io.
223  *
224  * \pre iot == CIT_READ || iot == CIT_WRITE
225  */
226 int cl_io_rw_init(const struct lu_env *env, struct cl_io *io,
227                   enum cl_io_type iot, loff_t pos, size_t count)
228 {
229         LINVRNT(iot == CIT_READ || iot == CIT_WRITE);
230         LINVRNT(io->ci_obj != NULL);
231         ENTRY;
232
233         LU_OBJECT_HEADER(D_VFSTRACE, env, &io->ci_obj->co_lu,
234                          "io range: %u ["LPU64", "LPU64") %u %u\n",
235                          iot, (__u64)pos, (__u64)pos + count,
236                          io->u.ci_rw.crw_nonblock, io->u.ci_wr.wr_append);
237         io->u.ci_rw.crw_pos    = pos;
238         io->u.ci_rw.crw_count  = count;
239         RETURN(cl_io_init(env, io, iot, io->ci_obj));
240 }
241 EXPORT_SYMBOL(cl_io_rw_init);
242
243 static inline const struct lu_fid *
244 cl_lock_descr_fid(const struct cl_lock_descr *descr)
245 {
246         return lu_object_fid(&descr->cld_obj->co_lu);
247 }
248
249 static int cl_lock_descr_sort(const struct cl_lock_descr *d0,
250                               const struct cl_lock_descr *d1)
251 {
252         return lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1)) ?:
253                 __diff_normalize(d0->cld_start, d1->cld_start);
254 }
255
256 static int cl_lock_descr_cmp(const struct cl_lock_descr *d0,
257                              const struct cl_lock_descr *d1)
258 {
259         int ret;
260
261         ret = lu_fid_cmp(cl_lock_descr_fid(d0), cl_lock_descr_fid(d1));
262         if (ret)
263                 return ret;
264         if (d0->cld_end < d1->cld_start)
265                 return -1;
266         if (d0->cld_start > d0->cld_end)
267                 return 1;
268         return 0;
269 }
270
271 static void cl_lock_descr_merge(struct cl_lock_descr *d0,
272                                 const struct cl_lock_descr *d1)
273 {
274         d0->cld_start = min(d0->cld_start, d1->cld_start);
275         d0->cld_end = max(d0->cld_end, d1->cld_end);
276
277         if (d1->cld_mode == CLM_WRITE && d0->cld_mode != CLM_WRITE)
278                 d0->cld_mode = CLM_WRITE;
279
280         if (d1->cld_mode == CLM_GROUP && d0->cld_mode != CLM_GROUP)
281                 d0->cld_mode = CLM_GROUP;
282 }
283
284 /*
285  * Sort locks in lexicographical order of their (fid, start-offset) pairs.
286  */
287 static void cl_io_locks_sort(struct cl_io *io)
288 {
289         int done = 0;
290
291         ENTRY;
292         /* hidden treasure: bubble sort for now. */
293         do {
294                 struct cl_io_lock_link *curr;
295                 struct cl_io_lock_link *prev;
296                 struct cl_io_lock_link *temp;
297
298                 done = 1;
299                 prev = NULL;
300
301                 cfs_list_for_each_entry_safe(curr, temp,
302                                              &io->ci_lockset.cls_todo,
303                                              cill_linkage) {
304                         if (prev != NULL) {
305                                 switch (cl_lock_descr_sort(&prev->cill_descr,
306                                                           &curr->cill_descr)) {
307                                 case 0:
308                                         /*
309                                          * IMPOSSIBLE: Identical locks are
310                                          *             already removed at
311                                          *             this point.
312                                          */
313                                 default:
314                                         LBUG();
315                                 case +1:
316                                         cfs_list_move_tail(&curr->cill_linkage,
317                                                            &prev->cill_linkage);
318                                         done = 0;
319                                         continue; /* don't change prev: it's
320                                                    * still "previous" */
321                                 case -1: /* already in order */
322                                         break;
323                                 }
324                         }
325                         prev = curr;
326                 }
327         } while (!done);
328         EXIT;
329 }
330
331 /**
332  * Check whether \a queue contains locks matching \a need.
333  *
334  * \retval +ve there is a matching lock in the \a queue
335  * \retval   0 there are no matching locks in the \a queue
336  */
337 int cl_queue_match(const cfs_list_t *queue,
338                    const struct cl_lock_descr *need)
339 {
340        struct cl_io_lock_link *scan;
341
342        ENTRY;
343        cfs_list_for_each_entry(scan, queue, cill_linkage) {
344                if (cl_lock_descr_match(&scan->cill_descr, need))
345                        RETURN(+1);
346        }
347        RETURN(0);
348 }
349 EXPORT_SYMBOL(cl_queue_match);
350
351 static int cl_queue_merge(const cfs_list_t *queue,
352                           const struct cl_lock_descr *need)
353 {
354        struct cl_io_lock_link *scan;
355
356        ENTRY;
357        cfs_list_for_each_entry(scan, queue, cill_linkage) {
358                if (cl_lock_descr_cmp(&scan->cill_descr, need))
359                        continue;
360                cl_lock_descr_merge(&scan->cill_descr, need);
361                CDEBUG(D_VFSTRACE, "lock: %d: [%lu, %lu]\n",
362                       scan->cill_descr.cld_mode, scan->cill_descr.cld_start,
363                       scan->cill_descr.cld_end);
364                RETURN(+1);
365        }
366        RETURN(0);
367
368 }
369
370 static int cl_lockset_match(const struct cl_lockset *set,
371                             const struct cl_lock_descr *need)
372 {
373         return cl_queue_match(&set->cls_curr, need) ||
374                cl_queue_match(&set->cls_done, need);
375 }
376
377 static int cl_lockset_merge(const struct cl_lockset *set,
378                             const struct cl_lock_descr *need)
379 {
380         return cl_queue_merge(&set->cls_todo, need) ||
381                cl_lockset_match(set, need);
382 }
383
384 static int cl_lockset_lock_one(const struct lu_env *env,
385                                struct cl_io *io, struct cl_lockset *set,
386                                struct cl_io_lock_link *link)
387 {
388         struct cl_lock *lock;
389         int             result;
390
391         ENTRY;
392
393         if (io->ci_lockreq == CILR_PEEK) {
394                 lock = cl_lock_peek(env, io, &link->cill_descr, "io", io);
395                 if (lock == NULL)
396                         lock = ERR_PTR(-ENODATA);
397         } else
398                 lock = cl_lock_request(env, io, &link->cill_descr, "io", io);
399
400         if (!IS_ERR(lock)) {
401                 link->cill_lock = lock;
402                 cfs_list_move(&link->cill_linkage, &set->cls_curr);
403                 if (!(link->cill_descr.cld_enq_flags & CEF_ASYNC)) {
404                         result = cl_wait(env, lock);
405                         if (result == 0)
406                                 cfs_list_move(&link->cill_linkage,
407                                               &set->cls_done);
408                 } else
409                         result = 0;
410         } else
411                 result = PTR_ERR(lock);
412         RETURN(result);
413 }
414
415 static void cl_lock_link_fini(const struct lu_env *env, struct cl_io *io,
416                               struct cl_io_lock_link *link)
417 {
418         struct cl_lock *lock = link->cill_lock;
419
420         ENTRY;
421         cfs_list_del_init(&link->cill_linkage);
422         if (lock != NULL) {
423                 cl_lock_release(env, lock, "io", io);
424                 link->cill_lock = NULL;
425         }
426         if (link->cill_fini != NULL)
427                 link->cill_fini(env, link);
428         EXIT;
429 }
430
431 static int cl_lockset_lock(const struct lu_env *env, struct cl_io *io,
432                            struct cl_lockset *set)
433 {
434         struct cl_io_lock_link *link;
435         struct cl_io_lock_link *temp;
436         struct cl_lock         *lock;
437         int result;
438
439         ENTRY;
440         result = 0;
441         cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage) {
442                 if (!cl_lockset_match(set, &link->cill_descr)) {
443                         /* XXX some locking to guarantee that locks aren't
444                          * expanded in between. */
445                         result = cl_lockset_lock_one(env, io, set, link);
446                         if (result != 0)
447                                 break;
448                 } else
449                         cl_lock_link_fini(env, io, link);
450         }
451         if (result == 0) {
452                 cfs_list_for_each_entry_safe(link, temp,
453                                              &set->cls_curr, cill_linkage) {
454                         lock = link->cill_lock;
455                         result = cl_wait(env, lock);
456                         if (result == 0)
457                                 cfs_list_move(&link->cill_linkage,
458                                               &set->cls_done);
459                         else
460                                 break;
461                 }
462         }
463         RETURN(result);
464 }
465
466 /**
467  * Takes locks necessary for the current iteration of io.
468  *
469  * Calls cl_io_operations::cio_lock() top-to-bottom to collect locks required
470  * by layers for the current iteration. Then sort locks (to avoid dead-locks),
471  * and acquire them.
472  */
473 int cl_io_lock(const struct lu_env *env, struct cl_io *io)
474 {
475         const struct cl_io_slice *scan;
476         int result = 0;
477
478         LINVRNT(cl_io_is_loopable(io));
479         LINVRNT(io->ci_state == CIS_IT_STARTED);
480         LINVRNT(cl_io_invariant(io));
481
482         ENTRY;
483         cl_io_for_each(scan, io) {
484                 if (scan->cis_iop->op[io->ci_type].cio_lock == NULL)
485                         continue;
486                 result = scan->cis_iop->op[io->ci_type].cio_lock(env, scan);
487                 if (result != 0)
488                         break;
489         }
490         if (result == 0) {
491                 cl_io_locks_sort(io);
492                 result = cl_lockset_lock(env, io, &io->ci_lockset);
493         }
494         if (result != 0)
495                 cl_io_unlock(env, io);
496         else
497                 io->ci_state = CIS_LOCKED;
498         RETURN(result);
499 }
500 EXPORT_SYMBOL(cl_io_lock);
501
502 /**
503  * Release locks takes by io.
504  */
505 void cl_io_unlock(const struct lu_env *env, struct cl_io *io)
506 {
507         struct cl_lockset        *set;
508         struct cl_io_lock_link   *link;
509         struct cl_io_lock_link   *temp;
510         const struct cl_io_slice *scan;
511
512         LASSERT(cl_io_is_loopable(io));
513         LASSERT(CIS_IT_STARTED <= io->ci_state && io->ci_state < CIS_UNLOCKED);
514         LINVRNT(cl_io_invariant(io));
515
516         ENTRY;
517         set = &io->ci_lockset;
518
519         cfs_list_for_each_entry_safe(link, temp, &set->cls_todo, cill_linkage)
520                 cl_lock_link_fini(env, io, link);
521
522         cfs_list_for_each_entry_safe(link, temp, &set->cls_curr, cill_linkage)
523                 cl_lock_link_fini(env, io, link);
524
525         cfs_list_for_each_entry_safe(link, temp, &set->cls_done, cill_linkage) {
526                 cl_unuse(env, link->cill_lock);
527                 cl_lock_link_fini(env, io, link);
528         }
529         cl_io_for_each_reverse(scan, io) {
530                 if (scan->cis_iop->op[io->ci_type].cio_unlock != NULL)
531                         scan->cis_iop->op[io->ci_type].cio_unlock(env, scan);
532         }
533         io->ci_state = CIS_UNLOCKED;
534         LASSERT(!cl_env_info(env)->clt_counters[CNL_TOP].ctc_nr_locks_acquired);
535         EXIT;
536 }
537 EXPORT_SYMBOL(cl_io_unlock);
538
539 /**
540  * Prepares next iteration of io.
541  *
542  * Calls cl_io_operations::cio_iter_init() top-to-bottom. This exists to give
543  * layers a chance to modify io parameters, e.g., so that lov can restrict io
544  * to a single stripe.
545  */
546 int cl_io_iter_init(const struct lu_env *env, struct cl_io *io)
547 {
548         const struct cl_io_slice *scan;
549         int result;
550
551         LINVRNT(cl_io_is_loopable(io));
552         LINVRNT(io->ci_state == CIS_INIT || io->ci_state == CIS_IT_ENDED);
553         LINVRNT(cl_io_invariant(io));
554
555         ENTRY;
556         result = 0;
557         cl_io_for_each(scan, io) {
558                 if (scan->cis_iop->op[io->ci_type].cio_iter_init == NULL)
559                         continue;
560                 result = scan->cis_iop->op[io->ci_type].cio_iter_init(env,
561                                                                       scan);
562                 if (result != 0)
563                         break;
564         }
565         if (result == 0)
566                 io->ci_state = CIS_IT_STARTED;
567         RETURN(result);
568 }
569 EXPORT_SYMBOL(cl_io_iter_init);
570
571 /**
572  * Finalizes io iteration.
573  *
574  * Calls cl_io_operations::cio_iter_fini() bottom-to-top.
575  */
576 void cl_io_iter_fini(const struct lu_env *env, struct cl_io *io)
577 {
578         const struct cl_io_slice *scan;
579
580         LINVRNT(cl_io_is_loopable(io));
581         LINVRNT(io->ci_state == CIS_UNLOCKED);
582         LINVRNT(cl_io_invariant(io));
583
584         ENTRY;
585         cl_io_for_each_reverse(scan, io) {
586                 if (scan->cis_iop->op[io->ci_type].cio_iter_fini != NULL)
587                         scan->cis_iop->op[io->ci_type].cio_iter_fini(env, scan);
588         }
589         io->ci_state = CIS_IT_ENDED;
590         EXIT;
591 }
592 EXPORT_SYMBOL(cl_io_iter_fini);
593
594 /**
595  * Records that read or write io progressed \a nob bytes forward.
596  */
597 void cl_io_rw_advance(const struct lu_env *env, struct cl_io *io, size_t nob)
598 {
599         const struct cl_io_slice *scan;
600
601         LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE ||
602                 nob == 0);
603         LINVRNT(cl_io_is_loopable(io));
604         LINVRNT(cl_io_invariant(io));
605
606         ENTRY;
607
608         io->u.ci_rw.crw_pos   += nob;
609         io->u.ci_rw.crw_count -= nob;
610
611         /* layers have to be notified. */
612         cl_io_for_each_reverse(scan, io) {
613                 if (scan->cis_iop->op[io->ci_type].cio_advance != NULL)
614                         scan->cis_iop->op[io->ci_type].cio_advance(env, scan,
615                                                                    nob);
616         }
617         EXIT;
618 }
619 EXPORT_SYMBOL(cl_io_rw_advance);
620
621 /**
622  * Adds a lock to a lockset.
623  */
624 int cl_io_lock_add(const struct lu_env *env, struct cl_io *io,
625                    struct cl_io_lock_link *link)
626 {
627         int result;
628
629         ENTRY;
630         if (cl_lockset_merge(&io->ci_lockset, &link->cill_descr))
631                 result = +1;
632         else {
633                 cfs_list_add(&link->cill_linkage, &io->ci_lockset.cls_todo);
634                 result = 0;
635         }
636         RETURN(result);
637 }
638 EXPORT_SYMBOL(cl_io_lock_add);
639
640 static void cl_free_io_lock_link(const struct lu_env *env,
641                                  struct cl_io_lock_link *link)
642 {
643         OBD_FREE_PTR(link);
644 }
645
646 /**
647  * Allocates new lock link, and uses it to add a lock to a lockset.
648  */
649 int cl_io_lock_alloc_add(const struct lu_env *env, struct cl_io *io,
650                          struct cl_lock_descr *descr)
651 {
652         struct cl_io_lock_link *link;
653         int result;
654
655         ENTRY;
656         OBD_ALLOC_PTR(link);
657         if (link != NULL) {
658                 link->cill_descr     = *descr;
659                 link->cill_fini      = cl_free_io_lock_link;
660                 result = cl_io_lock_add(env, io, link);
661                 if (result) /* lock match */
662                         link->cill_fini(env, link);
663         } else
664                 result = -ENOMEM;
665
666         RETURN(result);
667 }
668 EXPORT_SYMBOL(cl_io_lock_alloc_add);
669
670 /**
671  * Starts io by calling cl_io_operations::cio_start() top-to-bottom.
672  */
673 int cl_io_start(const struct lu_env *env, struct cl_io *io)
674 {
675         const struct cl_io_slice *scan;
676         int result = 0;
677
678         LINVRNT(cl_io_is_loopable(io));
679         LINVRNT(io->ci_state == CIS_LOCKED);
680         LINVRNT(cl_io_invariant(io));
681         ENTRY;
682
683         io->ci_state = CIS_IO_GOING;
684         cl_io_for_each(scan, io) {
685                 if (scan->cis_iop->op[io->ci_type].cio_start == NULL)
686                         continue;
687                 result = scan->cis_iop->op[io->ci_type].cio_start(env, scan);
688                 if (result != 0)
689                         break;
690         }
691         if (result >= 0)
692                 result = 0;
693         RETURN(result);
694 }
695 EXPORT_SYMBOL(cl_io_start);
696
697 /**
698  * Wait until current io iteration is finished by calling
699  * cl_io_operations::cio_end() bottom-to-top.
700  */
701 void cl_io_end(const struct lu_env *env, struct cl_io *io)
702 {
703         const struct cl_io_slice *scan;
704
705         LINVRNT(cl_io_is_loopable(io));
706         LINVRNT(io->ci_state == CIS_IO_GOING);
707         LINVRNT(cl_io_invariant(io));
708         ENTRY;
709
710         cl_io_for_each_reverse(scan, io) {
711                 if (scan->cis_iop->op[io->ci_type].cio_end != NULL)
712                         scan->cis_iop->op[io->ci_type].cio_end(env, scan);
713                 /* TODO: error handling. */
714         }
715         io->ci_state = CIS_IO_FINISHED;
716         EXIT;
717 }
718 EXPORT_SYMBOL(cl_io_end);
719
720 static const struct cl_page_slice *
721 cl_io_slice_page(const struct cl_io_slice *ios, struct cl_page *page)
722 {
723         const struct cl_page_slice *slice;
724
725         slice = cl_page_at(page, ios->cis_obj->co_lu.lo_dev->ld_type);
726         LINVRNT(slice != NULL);
727         return slice;
728 }
729
730 /**
731  * True iff \a page is within \a io range.
732  */
733 static int cl_page_in_io(const struct cl_page *page, const struct cl_io *io)
734 {
735         int     result = 1;
736         loff_t  start;
737         loff_t  end;
738         pgoff_t idx;
739
740         idx = page->cp_index;
741         switch (io->ci_type) {
742         case CIT_READ:
743         case CIT_WRITE:
744                 /*
745                  * check that [start, end) and [pos, pos + count) extents
746                  * overlap.
747                  */
748                 if (!cl_io_is_append(io)) {
749                         const struct cl_io_rw_common *crw = &(io->u.ci_rw);
750                         start = cl_offset(page->cp_obj, idx);
751                         end   = cl_offset(page->cp_obj, idx + 1);
752                         result = crw->crw_pos < end &&
753                                  start < crw->crw_pos + crw->crw_count;
754                 }
755                 break;
756         case CIT_FAULT:
757                 result = io->u.ci_fault.ft_index == idx;
758                 break;
759         default:
760                 LBUG();
761         }
762         return result;
763 }
764
765 /**
766  * Called by read io, when page has to be read from the server.
767  *
768  * \see cl_io_operations::cio_read_page()
769  */
770 int cl_io_read_page(const struct lu_env *env, struct cl_io *io,
771                     struct cl_page *page)
772 {
773         const struct cl_io_slice *scan;
774         struct cl_2queue         *queue;
775         int                       result = 0;
776
777         LINVRNT(io->ci_type == CIT_READ || io->ci_type == CIT_FAULT);
778         LINVRNT(cl_page_is_owned(page, io));
779         LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
780         LINVRNT(cl_page_in_io(page, io));
781         LINVRNT(cl_io_invariant(io));
782         ENTRY;
783
784         queue = &io->ci_queue;
785
786         cl_2queue_init(queue);
787         /*
788          * ->cio_read_page() methods called in the loop below are supposed to
789          * never block waiting for network (the only subtle point is the
790          * creation of new pages for read-ahead that might result in cache
791          * shrinking, but currently only clean pages are shrunk and this
792          * requires no network io).
793          *
794          * Should this ever starts blocking, retry loop would be needed for
795          * "parallel io" (see CLO_REPEAT loops in cl_lock.c).
796          */
797         cl_io_for_each(scan, io) {
798                 if (scan->cis_iop->cio_read_page != NULL) {
799                         const struct cl_page_slice *slice;
800
801                         slice = cl_io_slice_page(scan, page);
802                         LINVRNT(slice != NULL);
803                         result = scan->cis_iop->cio_read_page(env, scan, slice);
804                         if (result != 0)
805                                 break;
806                 }
807         }
808         if (result == 0)
809                 result = cl_io_submit_rw(env, io, CRT_READ, queue);
810         /*
811          * Unlock unsent pages in case of error.
812          */
813         cl_page_list_disown(env, io, &queue->c2_qin);
814         cl_2queue_fini(env, queue);
815         RETURN(result);
816 }
817 EXPORT_SYMBOL(cl_io_read_page);
818
819 /**
820  * Called by write io to prepare page to receive data from user buffer.
821  *
822  * \see cl_io_operations::cio_prepare_write()
823  */
824 int cl_io_prepare_write(const struct lu_env *env, struct cl_io *io,
825                         struct cl_page *page, unsigned from, unsigned to)
826 {
827         const struct cl_io_slice *scan;
828         int result = 0;
829
830         LINVRNT(io->ci_type == CIT_WRITE);
831         LINVRNT(cl_page_is_owned(page, io));
832         LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
833         LINVRNT(cl_io_invariant(io));
834         LASSERT(cl_page_in_io(page, io));
835         ENTRY;
836
837         cl_io_for_each_reverse(scan, io) {
838                 if (scan->cis_iop->cio_prepare_write != NULL) {
839                         const struct cl_page_slice *slice;
840
841                         slice = cl_io_slice_page(scan, page);
842                         result = scan->cis_iop->cio_prepare_write(env, scan,
843                                                                   slice,
844                                                                   from, to);
845                         if (result != 0)
846                                 break;
847                 }
848         }
849         RETURN(result);
850 }
851 EXPORT_SYMBOL(cl_io_prepare_write);
852
853 /**
854  * Called by write io after user data were copied into a page.
855  *
856  * \see cl_io_operations::cio_commit_write()
857  */
858 int cl_io_commit_write(const struct lu_env *env, struct cl_io *io,
859                        struct cl_page *page, unsigned from, unsigned to)
860 {
861         const struct cl_io_slice *scan;
862         int result = 0;
863
864         LINVRNT(io->ci_type == CIT_WRITE);
865         LINVRNT(io->ci_state == CIS_IO_GOING || io->ci_state == CIS_LOCKED);
866         LINVRNT(cl_io_invariant(io));
867         /*
868          * XXX Uh... not nice. Top level cl_io_commit_write() call (vvp->lov)
869          * already called cl_page_cache_add(), moving page into CPS_CACHED
870          * state. Better (and more general) way of dealing with such situation
871          * is needed.
872          */
873         LASSERT(cl_page_is_owned(page, io) || page->cp_parent != NULL);
874         LASSERT(cl_page_in_io(page, io));
875         ENTRY;
876
877         cl_io_for_each(scan, io) {
878                 if (scan->cis_iop->cio_commit_write != NULL) {
879                         const struct cl_page_slice *slice;
880
881                         slice = cl_io_slice_page(scan, page);
882                         result = scan->cis_iop->cio_commit_write(env, scan,
883                                                                  slice,
884                                                                  from, to);
885                         if (result != 0)
886                                 break;
887                 }
888         }
889         LINVRNT(result <= 0);
890         RETURN(result);
891 }
892 EXPORT_SYMBOL(cl_io_commit_write);
893
894 /**
895  * Submits a list of pages for immediate io.
896  *
897  * After the function gets returned, The submitted pages are moved to
898  * queue->c2_qout queue, and queue->c2_qin contain both the pages don't need
899  * to be submitted, and the pages are errant to submit.
900  *
901  * \returns 0 if at least one page was submitted, error code otherwise.
902  * \see cl_io_operations::cio_submit()
903  */
904 int cl_io_submit_rw(const struct lu_env *env, struct cl_io *io,
905                     enum cl_req_type crt, struct cl_2queue *queue)
906 {
907         const struct cl_io_slice *scan;
908         int result = 0;
909
910         LINVRNT(crt < ARRAY_SIZE(scan->cis_iop->req_op));
911         ENTRY;
912
913         cl_io_for_each(scan, io) {
914                 if (scan->cis_iop->req_op[crt].cio_submit == NULL)
915                         continue;
916                 result = scan->cis_iop->req_op[crt].cio_submit(env, scan, crt,
917                                                                queue);
918                 if (result != 0)
919                         break;
920         }
921         /*
922          * If ->cio_submit() failed, no pages were sent.
923          */
924         LASSERT(ergo(result != 0, cfs_list_empty(&queue->c2_qout.pl_pages)));
925         RETURN(result);
926 }
927 EXPORT_SYMBOL(cl_io_submit_rw);
928
929 /**
930  * Submit a sync_io and wait for the IO to be finished, or error happens.
931  * If \a timeout is zero, it means to wait for the IO unconditionally.
932  */
933 int cl_io_submit_sync(const struct lu_env *env, struct cl_io *io,
934                       enum cl_req_type iot, struct cl_2queue *queue,
935                       long timeout)
936 {
937         struct cl_sync_io *anchor = &cl_env_info(env)->clt_anchor;
938         struct cl_page *pg;
939         int rc;
940
941         cl_page_list_for_each(pg, &queue->c2_qin) {
942                 LASSERT(pg->cp_sync_io == NULL);
943                 pg->cp_sync_io = anchor;
944         }
945
946         cl_sync_io_init(anchor, queue->c2_qin.pl_nr);
947         rc = cl_io_submit_rw(env, io, iot, queue);
948         if (rc == 0) {
949                 /*
950                  * If some pages weren't sent for any reason (e.g.,
951                  * read found up-to-date pages in the cache, or write found
952                  * clean pages), count them as completed to avoid infinite
953                  * wait.
954                  */
955                  cl_page_list_for_each(pg, &queue->c2_qin) {
956                         pg->cp_sync_io = NULL;
957                         cl_sync_io_note(anchor, +1);
958                  }
959
960                  /* wait for the IO to be finished. */
961                  rc = cl_sync_io_wait(env, io, &queue->c2_qout,
962                                       anchor, timeout);
963         } else {
964                 LASSERT(cfs_list_empty(&queue->c2_qout.pl_pages));
965                 cl_page_list_for_each(pg, &queue->c2_qin)
966                         pg->cp_sync_io = NULL;
967         }
968         return rc;
969 }
970 EXPORT_SYMBOL(cl_io_submit_sync);
971
972 /**
973  * Cancel an IO which has been submitted by cl_io_submit_rw.
974  */
975 int cl_io_cancel(const struct lu_env *env, struct cl_io *io,
976                  struct cl_page_list *queue)
977 {
978         struct cl_page *page;
979         int result = 0;
980
981         CERROR("Canceling ongoing page trasmission\n");
982         cl_page_list_for_each(page, queue) {
983                 int rc;
984
985                 LINVRNT(cl_page_in_io(page, io));
986                 rc = cl_page_cancel(env, page);
987                 result = result ?: rc;
988         }
989         return result;
990 }
991 EXPORT_SYMBOL(cl_io_cancel);
992
993 /**
994  * Main io loop.
995  *
996  * Pumps io through iterations calling
997  *
998  *    - cl_io_iter_init()
999  *
1000  *    - cl_io_lock()
1001  *
1002  *    - cl_io_start()
1003  *
1004  *    - cl_io_end()
1005  *
1006  *    - cl_io_unlock()
1007  *
1008  *    - cl_io_iter_fini()
1009  *
1010  * repeatedly until there is no more io to do.
1011  */
1012 int cl_io_loop(const struct lu_env *env, struct cl_io *io)
1013 {
1014         int result   = 0;
1015
1016         LINVRNT(cl_io_is_loopable(io));
1017         ENTRY;
1018
1019         do {
1020                 size_t nob;
1021
1022                 io->ci_continue = 0;
1023                 result = cl_io_iter_init(env, io);
1024                 if (result == 0) {
1025                         nob    = io->ci_nob;
1026                         result = cl_io_lock(env, io);
1027                         if (result == 0) {
1028                                 /*
1029                                  * Notify layers that locks has been taken,
1030                                  * and do actual i/o.
1031                                  *
1032                                  *   - llite: kms, short read;
1033                                  *   - llite: generic_file_read();
1034                                  */
1035                                 result = cl_io_start(env, io);
1036                                 /*
1037                                  * Send any remaining pending
1038                                  * io, etc.
1039                                  *
1040                                  *   - llite: ll_rw_stats_tally.
1041                                  */
1042                                 cl_io_end(env, io);
1043                                 cl_io_unlock(env, io);
1044                                 cl_io_rw_advance(env, io, io->ci_nob - nob);
1045                         }
1046                 }
1047                 cl_io_iter_fini(env, io);
1048         } while (result == 0 && io->ci_continue);
1049         if (result == 0)
1050                 result = io->ci_result;
1051         RETURN(result < 0 ? result : 0);
1052 }
1053 EXPORT_SYMBOL(cl_io_loop);
1054
1055 /**
1056  * Adds io slice to the cl_io.
1057  *
1058  * This is called by cl_object_operations::coo_io_init() methods to add a
1059  * per-layer state to the io. New state is added at the end of
1060  * cl_io::ci_layers list, that is, it is at the bottom of the stack.
1061  *
1062  * \see cl_lock_slice_add(), cl_req_slice_add(), cl_page_slice_add()
1063  */
1064 void cl_io_slice_add(struct cl_io *io, struct cl_io_slice *slice,
1065                      struct cl_object *obj,
1066                      const struct cl_io_operations *ops)
1067 {
1068         cfs_list_t *linkage = &slice->cis_linkage;
1069
1070         LASSERT((linkage->prev == NULL && linkage->next == NULL) ||
1071                 cfs_list_empty(linkage));
1072         ENTRY;
1073
1074         cfs_list_add_tail(linkage, &io->ci_layers);
1075         slice->cis_io  = io;
1076         slice->cis_obj = obj;
1077         slice->cis_iop = ops;
1078         EXIT;
1079 }
1080 EXPORT_SYMBOL(cl_io_slice_add);
1081
1082
1083 /**
1084  * Initializes page list.
1085  */
1086 void cl_page_list_init(struct cl_page_list *plist)
1087 {
1088         ENTRY;
1089         plist->pl_nr = 0;
1090         CFS_INIT_LIST_HEAD(&plist->pl_pages);
1091         plist->pl_owner = cfs_current();
1092         EXIT;
1093 }
1094 EXPORT_SYMBOL(cl_page_list_init);
1095
1096 /**
1097  * Adds a page to a page list.
1098  */
1099 void cl_page_list_add(struct cl_page_list *plist, struct cl_page *page)
1100 {
1101         ENTRY;
1102         /* it would be better to check that page is owned by "current" io, but
1103          * it is not passed here. */
1104         LASSERT(page->cp_owner != NULL);
1105         LINVRNT(plist->pl_owner == cfs_current());
1106
1107         lockdep_off();
1108         mutex_lock(&page->cp_mutex);
1109         lockdep_on();
1110         LASSERT(cfs_list_empty(&page->cp_batch));
1111         cfs_list_add_tail(&page->cp_batch, &plist->pl_pages);
1112         ++plist->pl_nr;
1113         page->cp_queue_ref = lu_ref_add(&page->cp_reference, "queue", plist);
1114         cl_page_get(page);
1115         EXIT;
1116 }
1117 EXPORT_SYMBOL(cl_page_list_add);
1118
1119 /**
1120  * Removes a page from a page list.
1121  */
1122 void cl_page_list_del(const struct lu_env *env,
1123                       struct cl_page_list *plist, struct cl_page *page)
1124 {
1125         LASSERT(plist->pl_nr > 0);
1126         LINVRNT(plist->pl_owner == cfs_current());
1127
1128         ENTRY;
1129         cfs_list_del_init(&page->cp_batch);
1130         lockdep_off();
1131         mutex_unlock(&page->cp_mutex);
1132         lockdep_on();
1133         --plist->pl_nr;
1134         lu_ref_del_at(&page->cp_reference, page->cp_queue_ref, "queue", plist);
1135         cl_page_put(env, page);
1136         EXIT;
1137 }
1138 EXPORT_SYMBOL(cl_page_list_del);
1139
1140 /**
1141  * Moves a page from one page list to another.
1142  */
1143 void cl_page_list_move(struct cl_page_list *dst, struct cl_page_list *src,
1144                        struct cl_page *page)
1145 {
1146         LASSERT(src->pl_nr > 0);
1147         LINVRNT(dst->pl_owner == cfs_current());
1148         LINVRNT(src->pl_owner == cfs_current());
1149
1150         ENTRY;
1151         cfs_list_move_tail(&page->cp_batch, &dst->pl_pages);
1152         --src->pl_nr;
1153         ++dst->pl_nr;
1154         lu_ref_set_at(&page->cp_reference,
1155                       page->cp_queue_ref, "queue", src, dst);
1156         EXIT;
1157 }
1158 EXPORT_SYMBOL(cl_page_list_move);
1159
1160 /**
1161  * splice the cl_page_list, just as list head does
1162  */
1163 void cl_page_list_splice(struct cl_page_list *list, struct cl_page_list *head)
1164 {
1165         struct cl_page *page;
1166         struct cl_page *tmp;
1167
1168         LINVRNT(list->pl_owner == cfs_current());
1169         LINVRNT(head->pl_owner == cfs_current());
1170
1171         ENTRY;
1172         cl_page_list_for_each_safe(page, tmp, list)
1173                 cl_page_list_move(head, list, page);
1174         EXIT;
1175 }
1176 EXPORT_SYMBOL(cl_page_list_splice);
1177
1178 void cl_page_disown0(const struct lu_env *env,
1179                      struct cl_io *io, struct cl_page *pg);
1180
1181 /**
1182  * Disowns pages in a queue.
1183  */
1184 void cl_page_list_disown(const struct lu_env *env,
1185                          struct cl_io *io, struct cl_page_list *plist)
1186 {
1187         struct cl_page *page;
1188         struct cl_page *temp;
1189
1190         LINVRNT(plist->pl_owner == cfs_current());
1191
1192         ENTRY;
1193         cl_page_list_for_each_safe(page, temp, plist) {
1194                 LASSERT(plist->pl_nr > 0);
1195
1196                 cfs_list_del_init(&page->cp_batch);
1197                 lockdep_off();
1198                 mutex_unlock(&page->cp_mutex);
1199                 lockdep_on();
1200                 --plist->pl_nr;
1201                 /*
1202                  * cl_page_disown0 rather than usual cl_page_disown() is used,
1203                  * because pages are possibly in CPS_FREEING state already due
1204                  * to the call to cl_page_list_discard().
1205                  */
1206                 /*
1207                  * XXX cl_page_disown0() will fail if page is not locked.
1208                  */
1209                 cl_page_disown0(env, io, page);
1210                 lu_ref_del(&page->cp_reference, "queue", plist);
1211                 cl_page_put(env, page);
1212         }
1213         EXIT;
1214 }
1215 EXPORT_SYMBOL(cl_page_list_disown);
1216
1217 /**
1218  * Releases pages from queue.
1219  */
1220 void cl_page_list_fini(const struct lu_env *env, struct cl_page_list *plist)
1221 {
1222         struct cl_page *page;
1223         struct cl_page *temp;
1224
1225         LINVRNT(plist->pl_owner == cfs_current());
1226
1227         ENTRY;
1228         cl_page_list_for_each_safe(page, temp, plist)
1229                 cl_page_list_del(env, plist, page);
1230         LASSERT(plist->pl_nr == 0);
1231         EXIT;
1232 }
1233 EXPORT_SYMBOL(cl_page_list_fini);
1234
1235 /**
1236  * Owns all pages in a queue.
1237  */
1238 int cl_page_list_own(const struct lu_env *env,
1239                      struct cl_io *io, struct cl_page_list *plist)
1240 {
1241         struct cl_page *page;
1242         struct cl_page *temp;
1243         pgoff_t index = 0;
1244         int result;
1245
1246         LINVRNT(plist->pl_owner == cfs_current());
1247
1248         ENTRY;
1249         result = 0;
1250         cl_page_list_for_each_safe(page, temp, plist) {
1251                 LASSERT(index <= page->cp_index);
1252                 index = page->cp_index;
1253                 if (cl_page_own(env, io, page) == 0)
1254                         result = result ?: page->cp_error;
1255                 else
1256                         cl_page_list_del(env, plist, page);
1257         }
1258         RETURN(result);
1259 }
1260 EXPORT_SYMBOL(cl_page_list_own);
1261
1262 /**
1263  * Assumes all pages in a queue.
1264  */
1265 void cl_page_list_assume(const struct lu_env *env,
1266                          struct cl_io *io, struct cl_page_list *plist)
1267 {
1268         struct cl_page *page;
1269
1270         LINVRNT(plist->pl_owner == cfs_current());
1271
1272         cl_page_list_for_each(page, plist)
1273                 cl_page_assume(env, io, page);
1274 }
1275 EXPORT_SYMBOL(cl_page_list_assume);
1276
1277 /**
1278  * Discards all pages in a queue.
1279  */
1280 void cl_page_list_discard(const struct lu_env *env, struct cl_io *io,
1281                           struct cl_page_list *plist)
1282 {
1283         struct cl_page *page;
1284
1285         LINVRNT(plist->pl_owner == cfs_current());
1286         ENTRY;
1287         cl_page_list_for_each(page, plist)
1288                 cl_page_discard(env, io, page);
1289         EXIT;
1290 }
1291 EXPORT_SYMBOL(cl_page_list_discard);
1292
1293 /**
1294  * Unmaps all pages in a queue from user virtual memory.
1295  */
1296 int cl_page_list_unmap(const struct lu_env *env, struct cl_io *io,
1297                         struct cl_page_list *plist)
1298 {
1299         struct cl_page *page;
1300         int result;
1301
1302         LINVRNT(plist->pl_owner == cfs_current());
1303         ENTRY;
1304         result = 0;
1305         cl_page_list_for_each(page, plist) {
1306                 result = cl_page_unmap(env, io, page);
1307                 if (result != 0)
1308                         break;
1309         }
1310         RETURN(result);
1311 }
1312 EXPORT_SYMBOL(cl_page_list_unmap);
1313
1314 /**
1315  * Initialize dual page queue.
1316  */
1317 void cl_2queue_init(struct cl_2queue *queue)
1318 {
1319         ENTRY;
1320         cl_page_list_init(&queue->c2_qin);
1321         cl_page_list_init(&queue->c2_qout);
1322         EXIT;
1323 }
1324 EXPORT_SYMBOL(cl_2queue_init);
1325
1326 /**
1327  * Add a page to the incoming page list of 2-queue.
1328  */
1329 void cl_2queue_add(struct cl_2queue *queue, struct cl_page *page)
1330 {
1331         ENTRY;
1332         cl_page_list_add(&queue->c2_qin, page);
1333         EXIT;
1334 }
1335 EXPORT_SYMBOL(cl_2queue_add);
1336
1337 /**
1338  * Disown pages in both lists of a 2-queue.
1339  */
1340 void cl_2queue_disown(const struct lu_env *env,
1341                       struct cl_io *io, struct cl_2queue *queue)
1342 {
1343         ENTRY;
1344         cl_page_list_disown(env, io, &queue->c2_qin);
1345         cl_page_list_disown(env, io, &queue->c2_qout);
1346         EXIT;
1347 }
1348 EXPORT_SYMBOL(cl_2queue_disown);
1349
1350 /**
1351  * Discard (truncate) pages in both lists of a 2-queue.
1352  */
1353 void cl_2queue_discard(const struct lu_env *env,
1354                        struct cl_io *io, struct cl_2queue *queue)
1355 {
1356         ENTRY;
1357         cl_page_list_discard(env, io, &queue->c2_qin);
1358         cl_page_list_discard(env, io, &queue->c2_qout);
1359         EXIT;
1360 }
1361 EXPORT_SYMBOL(cl_2queue_discard);
1362
1363 /**
1364  * Assume to own the pages in cl_2queue
1365  */
1366 void cl_2queue_assume(const struct lu_env *env,
1367                       struct cl_io *io, struct cl_2queue *queue)
1368 {
1369         cl_page_list_assume(env, io, &queue->c2_qin);
1370         cl_page_list_assume(env, io, &queue->c2_qout);
1371 }
1372 EXPORT_SYMBOL(cl_2queue_assume);
1373
1374 /**
1375  * Finalize both page lists of a 2-queue.
1376  */
1377 void cl_2queue_fini(const struct lu_env *env, struct cl_2queue *queue)
1378 {
1379         ENTRY;
1380         cl_page_list_fini(env, &queue->c2_qout);
1381         cl_page_list_fini(env, &queue->c2_qin);
1382         EXIT;
1383 }
1384 EXPORT_SYMBOL(cl_2queue_fini);
1385
1386 /**
1387  * Initialize a 2-queue to contain \a page in its incoming page list.
1388  */
1389 void cl_2queue_init_page(struct cl_2queue *queue, struct cl_page *page)
1390 {
1391         ENTRY;
1392         cl_2queue_init(queue);
1393         cl_2queue_add(queue, page);
1394         EXIT;
1395 }
1396 EXPORT_SYMBOL(cl_2queue_init_page);
1397
1398 /**
1399  * Returns top-level io.
1400  *
1401  * \see cl_object_top(), cl_page_top().
1402  */
1403 struct cl_io *cl_io_top(struct cl_io *io)
1404 {
1405         ENTRY;
1406         while (io->ci_parent != NULL)
1407                 io = io->ci_parent;
1408         RETURN(io);
1409 }
1410 EXPORT_SYMBOL(cl_io_top);
1411
1412 /**
1413  * Prints human readable representation of \a io to the \a f.
1414  */
1415 void cl_io_print(const struct lu_env *env, void *cookie,
1416                  lu_printer_t printer, const struct cl_io *io)
1417 {
1418 }
1419
1420 /**
1421  * Adds request slice to the compound request.
1422  *
1423  * This is called by cl_device_operations::cdo_req_init() methods to add a
1424  * per-layer state to the request. New state is added at the end of
1425  * cl_req::crq_layers list, that is, it is at the bottom of the stack.
1426  *
1427  * \see cl_lock_slice_add(), cl_page_slice_add(), cl_io_slice_add()
1428  */
1429 void cl_req_slice_add(struct cl_req *req, struct cl_req_slice *slice,
1430                       struct cl_device *dev,
1431                       const struct cl_req_operations *ops)
1432 {
1433         ENTRY;
1434         cfs_list_add_tail(&slice->crs_linkage, &req->crq_layers);
1435         slice->crs_dev = dev;
1436         slice->crs_ops = ops;
1437         slice->crs_req = req;
1438         EXIT;
1439 }
1440 EXPORT_SYMBOL(cl_req_slice_add);
1441
1442 static void cl_req_free(const struct lu_env *env, struct cl_req *req)
1443 {
1444         unsigned i;
1445
1446         LASSERT(cfs_list_empty(&req->crq_pages));
1447         LASSERT(req->crq_nrpages == 0);
1448         LINVRNT(cfs_list_empty(&req->crq_layers));
1449         LINVRNT(equi(req->crq_nrobjs > 0, req->crq_o != NULL));
1450         ENTRY;
1451
1452         if (req->crq_o != NULL) {
1453                 for (i = 0; i < req->crq_nrobjs; ++i) {
1454                         struct cl_object *obj = req->crq_o[i].ro_obj;
1455                         if (obj != NULL) {
1456                                 lu_object_ref_del_at(&obj->co_lu,
1457                                                      req->crq_o[i].ro_obj_ref,
1458                                                      "cl_req", req);
1459                                 cl_object_put(env, obj);
1460                         }
1461                 }
1462                 OBD_FREE(req->crq_o, req->crq_nrobjs * sizeof req->crq_o[0]);
1463         }
1464         OBD_FREE_PTR(req);
1465         EXIT;
1466 }
1467
1468 static int cl_req_init(const struct lu_env *env, struct cl_req *req,
1469                        struct cl_page *page)
1470 {
1471         struct cl_device     *dev;
1472         struct cl_page_slice *slice;
1473         int result;
1474
1475         ENTRY;
1476         result = 0;
1477         page = cl_page_top(page);
1478         do {
1479                 cfs_list_for_each_entry(slice, &page->cp_layers, cpl_linkage) {
1480                         dev = lu2cl_dev(slice->cpl_obj->co_lu.lo_dev);
1481                         if (dev->cd_ops->cdo_req_init != NULL) {
1482                                 result = dev->cd_ops->cdo_req_init(env,
1483                                                                    dev, req);
1484                                 if (result != 0)
1485                                         break;
1486                         }
1487                 }
1488                 page = page->cp_child;
1489         } while (page != NULL && result == 0);
1490         RETURN(result);
1491 }
1492
1493 /**
1494  * Invokes per-request transfer completion call-backs
1495  * (cl_req_operations::cro_completion()) bottom-to-top.
1496  */
1497 void cl_req_completion(const struct lu_env *env, struct cl_req *req, int rc)
1498 {
1499         struct cl_req_slice *slice;
1500
1501         ENTRY;
1502         /*
1503          * for the lack of list_for_each_entry_reverse_safe()...
1504          */
1505         while (!cfs_list_empty(&req->crq_layers)) {
1506                 slice = cfs_list_entry(req->crq_layers.prev,
1507                                        struct cl_req_slice, crs_linkage);
1508                 cfs_list_del_init(&slice->crs_linkage);
1509                 if (slice->crs_ops->cro_completion != NULL)
1510                         slice->crs_ops->cro_completion(env, slice, rc);
1511         }
1512         cl_req_free(env, req);
1513         EXIT;
1514 }
1515 EXPORT_SYMBOL(cl_req_completion);
1516
1517 /**
1518  * Allocates new transfer request.
1519  */
1520 struct cl_req *cl_req_alloc(const struct lu_env *env, struct cl_page *page,
1521                             enum cl_req_type crt, int nr_objects)
1522 {
1523         struct cl_req *req;
1524
1525         LINVRNT(nr_objects > 0);
1526         ENTRY;
1527
1528         OBD_ALLOC_PTR(req);
1529         if (req != NULL) {
1530                 int result;
1531
1532                 OBD_ALLOC(req->crq_o, nr_objects * sizeof req->crq_o[0]);
1533                 if (req->crq_o != NULL) {
1534                         req->crq_nrobjs = nr_objects;
1535                         req->crq_type = crt;
1536                         CFS_INIT_LIST_HEAD(&req->crq_pages);
1537                         CFS_INIT_LIST_HEAD(&req->crq_layers);
1538                         result = cl_req_init(env, req, page);
1539                 } else
1540                         result = -ENOMEM;
1541                 if (result != 0) {
1542                         cl_req_completion(env, req, result);
1543                         req = ERR_PTR(result);
1544                 }
1545         } else
1546                 req = ERR_PTR(-ENOMEM);
1547         RETURN(req);
1548 }
1549 EXPORT_SYMBOL(cl_req_alloc);
1550
1551 /**
1552  * Adds a page to a request.
1553  */
1554 void cl_req_page_add(const struct lu_env *env,
1555                      struct cl_req *req, struct cl_page *page)
1556 {
1557         struct cl_object  *obj;
1558         struct cl_req_obj *rqo;
1559         int i;
1560
1561         ENTRY;
1562         page = cl_page_top(page);
1563
1564         LASSERT(cfs_list_empty(&page->cp_flight));
1565         LASSERT(page->cp_req == NULL);
1566
1567         CL_PAGE_DEBUG(D_PAGE, env, page, "req %p, %d, %u\n",
1568                       req, req->crq_type, req->crq_nrpages);
1569
1570         cfs_list_add_tail(&page->cp_flight, &req->crq_pages);
1571         ++req->crq_nrpages;
1572         page->cp_req = req;
1573         obj = cl_object_top(page->cp_obj);
1574         for (i = 0, rqo = req->crq_o; obj != rqo->ro_obj; ++i, ++rqo) {
1575                 if (rqo->ro_obj == NULL) {
1576                         rqo->ro_obj = obj;
1577                         cl_object_get(obj);
1578                         rqo->ro_obj_ref = lu_object_ref_add(&obj->co_lu,
1579                                                             "cl_req", req);
1580                         break;
1581                 }
1582         }
1583         LASSERT(i < req->crq_nrobjs);
1584         EXIT;
1585 }
1586 EXPORT_SYMBOL(cl_req_page_add);
1587
1588 /**
1589  * Removes a page from a request.
1590  */
1591 void cl_req_page_done(const struct lu_env *env, struct cl_page *page)
1592 {
1593         struct cl_req *req = page->cp_req;
1594
1595         ENTRY;
1596         page = cl_page_top(page);
1597
1598         LASSERT(!cfs_list_empty(&page->cp_flight));
1599         LASSERT(req->crq_nrpages > 0);
1600
1601         cfs_list_del_init(&page->cp_flight);
1602         --req->crq_nrpages;
1603         page->cp_req = NULL;
1604         EXIT;
1605 }
1606 EXPORT_SYMBOL(cl_req_page_done);
1607
1608 /**
1609  * Notifies layers that request is about to depart by calling
1610  * cl_req_operations::cro_prep() top-to-bottom.
1611  */
1612 int cl_req_prep(const struct lu_env *env, struct cl_req *req)
1613 {
1614         int i;
1615         int result;
1616         const struct cl_req_slice *slice;
1617
1618         ENTRY;
1619         /*
1620          * Check that the caller of cl_req_alloc() didn't lie about the number
1621          * of objects.
1622          */
1623         for (i = 0; i < req->crq_nrobjs; ++i)
1624                 LASSERT(req->crq_o[i].ro_obj != NULL);
1625
1626         result = 0;
1627         cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1628                 if (slice->crs_ops->cro_prep != NULL) {
1629                         result = slice->crs_ops->cro_prep(env, slice);
1630                         if (result != 0)
1631                                 break;
1632                 }
1633         }
1634         RETURN(result);
1635 }
1636 EXPORT_SYMBOL(cl_req_prep);
1637
1638 /**
1639  * Fills in attributes that are passed to server together with transfer. Only
1640  * attributes from \a flags may be touched. This can be called multiple times
1641  * for the same request.
1642  */
1643 void cl_req_attr_set(const struct lu_env *env, struct cl_req *req,
1644                      struct cl_req_attr *attr, obd_valid flags)
1645 {
1646         const struct cl_req_slice *slice;
1647         struct cl_page            *page;
1648         int i;
1649
1650         LASSERT(!cfs_list_empty(&req->crq_pages));
1651         ENTRY;
1652
1653         /* Take any page to use as a model. */
1654         page = cfs_list_entry(req->crq_pages.next, struct cl_page, cp_flight);
1655
1656         for (i = 0; i < req->crq_nrobjs; ++i) {
1657                 cfs_list_for_each_entry(slice, &req->crq_layers, crs_linkage) {
1658                         const struct cl_page_slice *scan;
1659                         const struct cl_object     *obj;
1660
1661                         scan = cl_page_at(page,
1662                                           slice->crs_dev->cd_lu_dev.ld_type);
1663                         LASSERT(scan != NULL);
1664                         obj = scan->cpl_obj;
1665                         if (slice->crs_ops->cro_attr_set != NULL)
1666                                 slice->crs_ops->cro_attr_set(env, slice, obj,
1667                                                              attr + i, flags);
1668                 }
1669         }
1670         EXIT;
1671 }
1672 EXPORT_SYMBOL(cl_req_attr_set);
1673
1674 /* XXX complete(), init_completion(), and wait_for_completion(), until they are
1675  * implemented in libcfs. */
1676 #ifdef __KERNEL__
1677 # include <linux/sched.h>
1678 #else /* __KERNEL__ */
1679 # include <liblustre.h>
1680 #endif
1681
1682 /**
1683  * Initialize synchronous io wait anchor, for transfer of \a nrpages pages.
1684  */
1685 void cl_sync_io_init(struct cl_sync_io *anchor, int nrpages)
1686 {
1687         ENTRY;
1688         cfs_waitq_init(&anchor->csi_waitq);
1689         cfs_atomic_set(&anchor->csi_sync_nr, nrpages);
1690         cfs_atomic_set(&anchor->csi_barrier, nrpages > 0);
1691         anchor->csi_sync_rc = 0;
1692         EXIT;
1693 }
1694 EXPORT_SYMBOL(cl_sync_io_init);
1695
1696 /**
1697  * Wait until all transfer completes. Transfer completion routine has to call
1698  * cl_sync_io_note() for every page.
1699  */
1700 int cl_sync_io_wait(const struct lu_env *env, struct cl_io *io,
1701                     struct cl_page_list *queue, struct cl_sync_io *anchor,
1702                     long timeout)
1703 {
1704         struct l_wait_info lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
1705                                                   NULL, NULL, NULL);
1706         int rc;
1707         ENTRY;
1708
1709         LASSERT(timeout >= 0);
1710
1711         rc = l_wait_event(anchor->csi_waitq,
1712                           cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1713                           &lwi);
1714         if (rc < 0) {
1715                 CERROR("SYNC IO failed with error: %d, try to cancel "
1716                        "%d remaining pages\n",
1717                        rc, cfs_atomic_read(&anchor->csi_sync_nr));
1718
1719                 (void)cl_io_cancel(env, io, queue);
1720
1721                 lwi = (struct l_wait_info) { 0 };
1722                 (void)l_wait_event(anchor->csi_waitq,
1723                                    cfs_atomic_read(&anchor->csi_sync_nr) == 0,
1724                                    &lwi);
1725         } else {
1726                 rc = anchor->csi_sync_rc;
1727         }
1728         LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) == 0);
1729         cl_page_list_assume(env, io, queue);
1730
1731         /* wait until cl_sync_io_note() has done wakeup */
1732         while (unlikely(cfs_atomic_read(&anchor->csi_barrier) != 0)) {
1733 #ifdef __KERNEL__
1734                 cpu_relax();
1735 #endif
1736         }
1737
1738         POISON(anchor, 0x5a, sizeof *anchor);
1739         RETURN(rc);
1740 }
1741 EXPORT_SYMBOL(cl_sync_io_wait);
1742
1743 /**
1744  * Indicate that transfer of a single page completed.
1745  */
1746 void cl_sync_io_note(struct cl_sync_io *anchor, int ioret)
1747 {
1748         ENTRY;
1749         if (anchor->csi_sync_rc == 0 && ioret < 0)
1750                 anchor->csi_sync_rc = ioret;
1751         /*
1752          * Synchronous IO done without releasing page lock (e.g., as a part of
1753          * ->{prepare,commit}_write(). Completion is used to signal the end of
1754          * IO.
1755          */
1756         LASSERT(cfs_atomic_read(&anchor->csi_sync_nr) > 0);
1757         if (cfs_atomic_dec_and_test(&anchor->csi_sync_nr)) {
1758                 cfs_waitq_broadcast(&anchor->csi_waitq);
1759                 /* it's safe to nuke or reuse anchor now */
1760                 cfs_atomic_set(&anchor->csi_barrier, 0);
1761         }
1762         EXIT;
1763 }
1764 EXPORT_SYMBOL(cl_sync_io_note);