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