Whamcloud - gitweb
LU-3321 clio: remove stackable cl_page completely
[fs/lustre-release.git] / lustre / lov / lov_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, 2013, 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  * Implementation of cl_io for LOV layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LOV
43
44 #include "lov_cl_internal.h"
45
46 /** \addtogroup lov
47  *  @{
48  */
49
50 static inline void lov_sub_enter(struct lov_io_sub *sub)
51 {
52         sub->sub_reenter++;
53 }
54 static inline void lov_sub_exit(struct lov_io_sub *sub)
55 {
56         sub->sub_reenter--;
57 }
58
59 static void lov_io_sub_fini(const struct lu_env *env, struct lov_io *lio,
60                             struct lov_io_sub *sub)
61 {
62         ENTRY;
63         if (sub->sub_io != NULL) {
64                 if (sub->sub_io_initialized) {
65                         lov_sub_enter(sub);
66                         cl_io_fini(sub->sub_env, sub->sub_io);
67                         lov_sub_exit(sub);
68                         sub->sub_io_initialized = 0;
69                         lio->lis_active_subios--;
70                 }
71                 if (sub->sub_stripe == lio->lis_single_subio_index)
72                         lio->lis_single_subio_index = -1;
73                 else if (!sub->sub_borrowed)
74                         OBD_FREE_PTR(sub->sub_io);
75                 sub->sub_io = NULL;
76         }
77         if (sub->sub_env != NULL && !IS_ERR(sub->sub_env)) {
78                 if (!sub->sub_borrowed)
79                         cl_env_put(sub->sub_env, &sub->sub_refcheck);
80                 sub->sub_env = NULL;
81         }
82         EXIT;
83 }
84
85 static void lov_io_sub_inherit(struct cl_io *io, struct lov_io *lio,
86                                int stripe, loff_t start, loff_t end)
87 {
88         struct lov_stripe_md *lsm    = lio->lis_object->lo_lsm;
89         struct cl_io         *parent = lio->lis_cl.cis_io;
90
91         switch(io->ci_type) {
92         case CIT_SETATTR: {
93                 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
94                 io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
95                 io->u.ci_setattr.sa_capa = parent->u.ci_setattr.sa_capa;
96                 if (cl_io_is_trunc(io)) {
97                         loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
98
99                         new_size = lov_size_to_stripe(lsm, new_size, stripe);
100                         io->u.ci_setattr.sa_attr.lvb_size = new_size;
101                 }
102                 break;
103         }
104         case CIT_FAULT: {
105                 struct cl_object *obj = parent->ci_obj;
106                 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
107
108                 io->u.ci_fault = parent->u.ci_fault;
109                 off = lov_size_to_stripe(lsm, off, stripe);
110                 io->u.ci_fault.ft_index = cl_index(obj, off);
111                 break;
112         }
113         case CIT_FSYNC: {
114                 io->u.ci_fsync.fi_start = start;
115                 io->u.ci_fsync.fi_end = end;
116                 io->u.ci_fsync.fi_capa = parent->u.ci_fsync.fi_capa;
117                 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
118                 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
119                 break;
120         }
121         case CIT_READ:
122         case CIT_WRITE: {
123                 io->u.ci_wr.wr_sync = cl_io_is_sync_write(parent);
124                 if (cl_io_is_append(parent)) {
125                         io->u.ci_wr.wr_append = 1;
126                 } else {
127                         io->u.ci_rw.crw_pos = start;
128                         io->u.ci_rw.crw_count = end - start;
129                 }
130                 break;
131         }
132         default:
133                 break;
134         }
135 }
136
137 static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
138                            struct lov_io_sub *sub)
139 {
140         struct lov_object *lov = lio->lis_object;
141         struct lov_device *ld  = lu2lov_dev(lov2cl(lov)->co_lu.lo_dev);
142         struct cl_io      *sub_io;
143         struct cl_object  *sub_obj;
144         struct cl_io      *io  = lio->lis_cl.cis_io;
145
146         int stripe = sub->sub_stripe;
147         int result;
148
149         LASSERT(sub->sub_io == NULL);
150         LASSERT(sub->sub_env == NULL);
151         LASSERT(sub->sub_stripe < lio->lis_stripe_count);
152         ENTRY;
153
154         result = 0;
155         sub->sub_io_initialized = 0;
156         sub->sub_borrowed = 0;
157
158         if (lio->lis_mem_frozen) {
159                 LASSERT(mutex_is_locked(&ld->ld_mutex));
160                 sub->sub_io  = &ld->ld_emrg[stripe]->emrg_subio;
161                 sub->sub_env = ld->ld_emrg[stripe]->emrg_env;
162                 sub->sub_borrowed = 1;
163         } else {
164                 void *cookie;
165
166                 /* obtain new environment */
167                 cookie = cl_env_reenter();
168                 sub->sub_env = cl_env_get(&sub->sub_refcheck);
169                 cl_env_reexit(cookie);
170                 if (IS_ERR(sub->sub_env))
171                         result = PTR_ERR(sub->sub_env);
172
173                 if (result == 0) {
174                         /*
175                          * First sub-io. Use ->lis_single_subio to
176                          * avoid dynamic allocation.
177                          */
178                         if (lio->lis_active_subios == 0) {
179                                 sub->sub_io = &lio->lis_single_subio;
180                                 lio->lis_single_subio_index = stripe;
181                         } else {
182                                 OBD_ALLOC_PTR(sub->sub_io);
183                                 if (sub->sub_io == NULL)
184                                         result = -ENOMEM;
185                         }
186                 }
187         }
188
189         if (result == 0) {
190                 sub_obj = lovsub2cl(lov_r0(lov)->lo_sub[stripe]);
191                 sub_io  = sub->sub_io;
192
193                 sub_io->ci_obj    = sub_obj;
194                 sub_io->ci_result = 0;
195
196                 sub_io->ci_parent  = io;
197                 sub_io->ci_lockreq = io->ci_lockreq;
198                 sub_io->ci_type    = io->ci_type;
199                 sub_io->ci_no_srvlock = io->ci_no_srvlock;
200                 sub_io->ci_noatime = io->ci_noatime;
201
202                 lov_sub_enter(sub);
203                 result = cl_io_sub_init(sub->sub_env, sub_io,
204                                         io->ci_type, sub_obj);
205                 lov_sub_exit(sub);
206                 if (result >= 0) {
207                         lio->lis_active_subios++;
208                         sub->sub_io_initialized = 1;
209                         result = 0;
210                 }
211         }
212         if (result != 0)
213                 lov_io_sub_fini(env, lio, sub);
214         RETURN(result);
215 }
216
217 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
218                                struct lov_io *lio, int stripe)
219 {
220         int rc;
221         struct lov_io_sub *sub = &lio->lis_subs[stripe];
222
223         LASSERT(stripe < lio->lis_stripe_count);
224         ENTRY;
225
226         if (!sub->sub_io_initialized) {
227                 sub->sub_stripe = stripe;
228                 rc = lov_io_sub_init(env, lio, sub);
229         } else
230                 rc = 0;
231         if (rc == 0)
232                 lov_sub_enter(sub);
233         else
234                 sub = ERR_PTR(rc);
235         RETURN(sub);
236 }
237
238 void lov_sub_put(struct lov_io_sub *sub)
239 {
240         lov_sub_exit(sub);
241 }
242
243 /*****************************************************************************
244  *
245  * Lov io operations.
246  *
247  */
248
249 static int lov_page_stripe(const struct cl_page *page)
250 {
251         struct lovsub_object *subobj;
252         const struct cl_page_slice *slice;
253         ENTRY;
254
255         slice = cl_page_at(page, &lovsub_device_type);
256         LASSERT(slice != NULL);
257         LASSERT(slice->cpl_obj != NULL);
258
259         subobj = cl2lovsub(slice->cpl_obj);
260         RETURN(subobj->lso_index);
261 }
262
263 struct lov_io_sub *lov_page_subio(const struct lu_env *env, struct lov_io *lio,
264                                   const struct cl_page_slice *slice)
265 {
266         struct lov_stripe_md *lsm  = lio->lis_object->lo_lsm;
267         struct cl_page       *page = slice->cpl_page;
268         int stripe;
269
270         LASSERT(lio->lis_cl.cis_io != NULL);
271         LASSERT(cl2lov(slice->cpl_obj) == lio->lis_object);
272         LASSERT(lsm != NULL);
273         LASSERT(lio->lis_nr_subios > 0);
274         ENTRY;
275
276         stripe = lov_page_stripe(page);
277         RETURN(lov_sub_get(env, lio, stripe));
278 }
279
280
281 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
282                              struct cl_io *io)
283 {
284         struct lov_stripe_md *lsm;
285         int result;
286         ENTRY;
287
288         LASSERT(lio->lis_object != NULL);
289         lsm = lio->lis_object->lo_lsm;
290
291         /*
292          * Need to be optimized, we can't afford to allocate a piece of memory
293          * when writing a page. -jay
294          */
295         OBD_ALLOC_LARGE(lio->lis_subs,
296                         lsm->lsm_stripe_count * sizeof lio->lis_subs[0]);
297         if (lio->lis_subs != NULL) {
298                 lio->lis_nr_subios = lio->lis_stripe_count;
299                 lio->lis_single_subio_index = -1;
300                 lio->lis_active_subios = 0;
301                 result = 0;
302         } else
303                 result = -ENOMEM;
304         RETURN(result);
305 }
306
307 static void lov_io_slice_init(struct lov_io *lio,
308                               struct lov_object *obj, struct cl_io *io)
309 {
310         ENTRY;
311
312         io->ci_result = 0;
313         lio->lis_object = obj;
314
315         LASSERT(obj->lo_lsm != NULL);
316         lio->lis_stripe_count = obj->lo_lsm->lsm_stripe_count;
317
318         switch (io->ci_type) {
319         case CIT_READ:
320         case CIT_WRITE:
321                 lio->lis_pos = io->u.ci_rw.crw_pos;
322                 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
323                 lio->lis_io_endpos = lio->lis_endpos;
324                 if (cl_io_is_append(io)) {
325                         LASSERT(io->ci_type == CIT_WRITE);
326                         lio->lis_pos = 0;
327                         lio->lis_endpos = OBD_OBJECT_EOF;
328                 }
329                 break;
330
331         case CIT_SETATTR:
332                 if (cl_io_is_trunc(io))
333                         lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
334                 else
335                         lio->lis_pos = 0;
336                 lio->lis_endpos = OBD_OBJECT_EOF;
337                 break;
338
339         case CIT_FAULT: {
340                 pgoff_t index = io->u.ci_fault.ft_index;
341                 lio->lis_pos = cl_offset(io->ci_obj, index);
342                 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
343                 break;
344         }
345
346         case CIT_FSYNC: {
347                 lio->lis_pos = io->u.ci_fsync.fi_start;
348                 lio->lis_endpos = io->u.ci_fsync.fi_end;
349                 break;
350         }
351
352         case CIT_MISC:
353                 lio->lis_pos = 0;
354                 lio->lis_endpos = OBD_OBJECT_EOF;
355                 break;
356
357         default:
358                 LBUG();
359         }
360
361         EXIT;
362 }
363
364 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
365 {
366         struct lov_io *lio = cl2lov_io(env, ios);
367         struct lov_object *lov = cl2lov(ios->cis_obj);
368         int i;
369
370         ENTRY;
371         if (lio->lis_subs != NULL) {
372                 for (i = 0; i < lio->lis_nr_subios; i++)
373                         lov_io_sub_fini(env, lio, &lio->lis_subs[i]);
374                 OBD_FREE_LARGE(lio->lis_subs,
375                          lio->lis_nr_subios * sizeof lio->lis_subs[0]);
376                 lio->lis_nr_subios = 0;
377         }
378
379         LASSERT(cfs_atomic_read(&lov->lo_active_ios) > 0);
380         if (cfs_atomic_dec_and_test(&lov->lo_active_ios))
381                 wake_up_all(&lov->lo_waitq);
382         EXIT;
383 }
384
385 static obd_off lov_offset_mod(obd_off val, int delta)
386 {
387         if (val != OBD_OBJECT_EOF)
388                 val += delta;
389         return val;
390 }
391
392 static int lov_io_iter_init(const struct lu_env *env,
393                             const struct cl_io_slice *ios)
394 {
395         struct lov_io        *lio = cl2lov_io(env, ios);
396         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
397         struct lov_io_sub    *sub;
398         obd_off endpos;
399         obd_off start;
400         obd_off end;
401         int stripe;
402         int rc = 0;
403
404         ENTRY;
405         endpos = lov_offset_mod(lio->lis_endpos, -1);
406         for (stripe = 0; stripe < lio->lis_stripe_count; stripe++) {
407                 if (!lov_stripe_intersects(lsm, stripe, lio->lis_pos,
408                                            endpos, &start, &end))
409                         continue;
410
411                 end = lov_offset_mod(end, +1);
412                 sub = lov_sub_get(env, lio, stripe);
413                 if (!IS_ERR(sub)) {
414                         lov_io_sub_inherit(sub->sub_io, lio, stripe,
415                                            start, end);
416                         rc = cl_io_iter_init(sub->sub_env, sub->sub_io);
417                         lov_sub_put(sub);
418                         CDEBUG(D_VFSTRACE, "shrink: %d ["LPU64", "LPU64")\n",
419                                stripe, start, end);
420                 } else
421                         rc = PTR_ERR(sub);
422
423                 if (!rc)
424                         cfs_list_add_tail(&sub->sub_linkage, &lio->lis_active);
425                 else
426                         break;
427         }
428         RETURN(rc);
429 }
430
431 static int lov_io_rw_iter_init(const struct lu_env *env,
432                                const struct cl_io_slice *ios)
433 {
434         struct lov_io        *lio = cl2lov_io(env, ios);
435         struct cl_io         *io  = ios->cis_io;
436         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
437         loff_t start = io->u.ci_rw.crw_pos;
438         loff_t next;
439         unsigned long ssize = lsm->lsm_stripe_size;
440
441         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
442         ENTRY;
443
444         /* fast path for common case. */
445         if (lio->lis_nr_subios != 1 && !cl_io_is_append(io)) {
446
447                 lov_do_div64(start, ssize);
448                 next = (start + 1) * ssize;
449                 if (next <= start * ssize)
450                         next = ~0ull;
451
452                 io->ci_continue = next < lio->lis_io_endpos;
453                 io->u.ci_rw.crw_count = min_t(loff_t, lio->lis_io_endpos,
454                                               next) - io->u.ci_rw.crw_pos;
455                 lio->lis_pos    = io->u.ci_rw.crw_pos;
456                 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
457                 CDEBUG(D_VFSTRACE, "stripe: "LPU64" chunk: ["LPU64", "LPU64") "
458                        LPU64"\n", (__u64)start, lio->lis_pos, lio->lis_endpos,
459                        (__u64)lio->lis_io_endpos);
460         }
461         /*
462          * XXX The following call should be optimized: we know, that
463          * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
464          */
465         RETURN(lov_io_iter_init(env, ios));
466 }
467
468 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
469                        int (*iofunc)(const struct lu_env *, struct cl_io *))
470 {
471         struct cl_io *parent = lio->lis_cl.cis_io;
472         struct lov_io_sub *sub;
473         int rc = 0;
474
475         ENTRY;
476         cfs_list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
477                 lov_sub_enter(sub);
478                 rc = iofunc(sub->sub_env, sub->sub_io);
479                 lov_sub_exit(sub);
480                 if (rc)
481                         break;
482
483                 if (parent->ci_result == 0)
484                         parent->ci_result = sub->sub_io->ci_result;
485         }
486         RETURN(rc);
487 }
488
489 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
490 {
491         ENTRY;
492         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
493 }
494
495 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
496 {
497         ENTRY;
498         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
499 }
500
501 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
502 {
503         ENTRY;
504         /*
505          * It's possible that lov_io_start() wasn't called against this
506          * sub-io, either because previous sub-io failed, or upper layer
507          * completed IO.
508          */
509         if (io->ci_state == CIS_IO_GOING)
510                 cl_io_end(env, io);
511         else
512                 io->ci_state = CIS_IO_FINISHED;
513         RETURN(0);
514 }
515
516 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
517 {
518         cl_io_iter_fini(env, io);
519         RETURN(0);
520 }
521
522 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
523 {
524         cl_io_unlock(env, io);
525         RETURN(0);
526 }
527
528 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
529 {
530         int rc;
531
532         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
533         LASSERT(rc == 0);
534 }
535
536 static void lov_io_iter_fini(const struct lu_env *env,
537                              const struct cl_io_slice *ios)
538 {
539         struct lov_io *lio = cl2lov_io(env, ios);
540         int rc;
541
542         ENTRY;
543         rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
544         LASSERT(rc == 0);
545         while (!cfs_list_empty(&lio->lis_active))
546                 cfs_list_del_init(lio->lis_active.next);
547         EXIT;
548 }
549
550 static void lov_io_unlock(const struct lu_env *env,
551                           const struct cl_io_slice *ios)
552 {
553         int rc;
554
555         ENTRY;
556         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
557         LASSERT(rc == 0);
558         EXIT;
559 }
560
561 /**
562  * lov implementation of cl_operations::cio_submit() method. It takes a list
563  * of pages in \a queue, splits it into per-stripe sub-lists, invokes
564  * cl_io_submit() on underlying devices to submit sub-lists, and then splices
565  * everything back.
566  *
567  * Major complication of this function is a need to handle memory cleansing:
568  * cl_io_submit() is called to write out pages as a part of VM memory
569  * reclamation, and hence it may not fail due to memory shortages (system
570  * dead-locks otherwise). To deal with this, some resources (sub-lists,
571  * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
572  * not-memory cleansing context), and in case of memory shortage, these
573  * pre-allocated resources are used by lov_io_submit() under
574  * lov_device::ld_mutex mutex.
575  */
576 static int lov_io_submit(const struct lu_env *env,
577                          const struct cl_io_slice *ios,
578                          enum cl_req_type crt, struct cl_2queue *queue)
579 {
580         struct cl_page_list     *qin = &queue->c2_qin;
581         struct lov_io           *lio = cl2lov_io(env, ios);
582         struct lov_io_sub       *sub;
583         struct cl_page_list     *plist = &lov_env_info(env)->lti_plist;
584         struct cl_page          *page;
585         int stripe;
586         int rc = 0;
587         ENTRY;
588
589         if (lio->lis_active_subios == 1) {
590                 int idx = lio->lis_single_subio_index;
591
592                 LASSERT(idx < lio->lis_nr_subios);
593                 sub = lov_sub_get(env, lio, idx);
594                 LASSERT(!IS_ERR(sub));
595                 LASSERT(sub->sub_io == &lio->lis_single_subio);
596                 rc = cl_io_submit_rw(sub->sub_env, sub->sub_io,
597                                      crt, queue);
598                 lov_sub_put(sub);
599                 RETURN(rc);
600         }
601
602         LASSERT(lio->lis_subs != NULL);
603
604         cl_page_list_init(plist);
605         while (qin->pl_nr > 0) {
606                 struct cl_2queue  *cl2q = &lov_env_info(env)->lti_cl2q;
607
608                 cl_2queue_init(cl2q);
609
610                 page = cl_page_list_first(qin);
611                 cl_page_list_move(&cl2q->c2_qin, qin, page);
612
613                 stripe = lov_page_stripe(page);
614                 while (qin->pl_nr > 0) {
615                         page = cl_page_list_first(qin);
616                         if (stripe != lov_page_stripe(page))
617                                 break;
618
619                         cl_page_list_move(&cl2q->c2_qin, qin, page);
620                 }
621
622                 sub = lov_sub_get(env, lio, stripe);
623                 if (!IS_ERR(sub)) {
624                         rc = cl_io_submit_rw(sub->sub_env, sub->sub_io,
625                                              crt, cl2q);
626                         lov_sub_put(sub);
627                 } else {
628                         rc = PTR_ERR(sub);
629                 }
630
631                 cl_page_list_splice(&cl2q->c2_qin, plist);
632                 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
633                 cl_2queue_fini(env, cl2q);
634
635                 if (rc != 0)
636                         break;
637         }
638
639         cl_page_list_splice(plist, qin);
640         cl_page_list_fini(env, plist);
641
642         RETURN(rc);
643 }
644
645 static int lov_io_commit_async(const struct lu_env *env,
646                                const struct cl_io_slice *ios,
647                                struct cl_page_list *queue, int from, int to,
648                                cl_commit_cbt cb)
649 {
650         struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
651         struct lov_io     *lio = cl2lov_io(env, ios);
652         struct lov_io_sub *sub;
653         struct cl_page *page;
654         int rc = 0;
655         ENTRY;
656
657         if (lio->lis_active_subios == 1) {
658                 int idx = lio->lis_single_subio_index;
659
660                 LASSERT(idx < lio->lis_nr_subios);
661                 sub = lov_sub_get(env, lio, idx);
662                 LASSERT(!IS_ERR(sub));
663                 LASSERT(sub->sub_io == &lio->lis_single_subio);
664                 rc = cl_io_commit_async(sub->sub_env, sub->sub_io, queue,
665                                         from, to, cb);
666                 lov_sub_put(sub);
667                 RETURN(rc);
668         }
669
670         LASSERT(lio->lis_subs != NULL);
671
672         cl_page_list_init(plist);
673         while (queue->pl_nr > 0) {
674                 int stripe_to = to;
675                 int stripe;
676
677                 LASSERT(plist->pl_nr == 0);
678                 page = cl_page_list_first(queue);
679                 cl_page_list_move(plist, queue, page);
680
681                 stripe = lov_page_stripe(page);
682                 while (queue->pl_nr > 0) {
683                         page = cl_page_list_first(queue);
684                         if (stripe != lov_page_stripe(page))
685                                 break;
686
687                         cl_page_list_move(plist, queue, page);
688                 }
689
690                 if (queue->pl_nr > 0) /* still has more pages */
691                         stripe_to = PAGE_SIZE;
692
693                 sub = lov_sub_get(env, lio, stripe);
694                 if (!IS_ERR(sub)) {
695                         rc = cl_io_commit_async(sub->sub_env, sub->sub_io,
696                                                 plist, from, stripe_to, cb);
697                         lov_sub_put(sub);
698                 } else {
699                         rc = PTR_ERR(sub);
700                         break;
701                 }
702
703                 if (plist->pl_nr > 0) /* short write */
704                         break;
705
706                 from = 0;
707         }
708
709         /* for error case, add the page back into the qin list */
710         LASSERT(ergo(rc == 0, plist->pl_nr == 0));
711         while (plist->pl_nr > 0) {
712                 /* error occurred, add the uncommitted pages back into queue */
713                 page = cl_page_list_last(plist);
714                 cl_page_list_move_head(queue, plist, page);
715         }
716
717         RETURN(rc);
718 }
719
720 static int lov_io_fault_start(const struct lu_env *env,
721                               const struct cl_io_slice *ios)
722 {
723         struct cl_fault_io *fio;
724         struct lov_io      *lio;
725         struct lov_io_sub  *sub;
726
727         ENTRY;
728         fio = &ios->cis_io->u.ci_fault;
729         lio = cl2lov_io(env, ios);
730         sub = lov_sub_get(env, lio, lov_page_stripe(fio->ft_page));
731         sub->sub_io->u.ci_fault.ft_nob = fio->ft_nob;
732         lov_sub_put(sub);
733         RETURN(lov_io_start(env, ios));
734 }
735
736 static void lov_io_fsync_end(const struct lu_env *env,
737                              const struct cl_io_slice *ios)
738 {
739         struct lov_io *lio = cl2lov_io(env, ios);
740         struct lov_io_sub *sub;
741         unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
742         ENTRY;
743
744         *written = 0;
745         cfs_list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
746                 struct cl_io *subio = sub->sub_io;
747
748                 lov_sub_enter(sub);
749                 lov_io_end_wrapper(sub->sub_env, subio);
750                 lov_sub_exit(sub);
751
752                 if (subio->ci_result == 0)
753                         *written += subio->u.ci_fsync.fi_nr_written;
754         }
755         RETURN_EXIT;
756 }
757
758 static const struct cl_io_operations lov_io_ops = {
759         .op = {
760                 [CIT_READ] = {
761                         .cio_fini      = lov_io_fini,
762                         .cio_iter_init = lov_io_rw_iter_init,
763                         .cio_iter_fini = lov_io_iter_fini,
764                         .cio_lock      = lov_io_lock,
765                         .cio_unlock    = lov_io_unlock,
766                         .cio_start     = lov_io_start,
767                         .cio_end       = lov_io_end
768                 },
769                 [CIT_WRITE] = {
770                         .cio_fini      = lov_io_fini,
771                         .cio_iter_init = lov_io_rw_iter_init,
772                         .cio_iter_fini = lov_io_iter_fini,
773                         .cio_lock      = lov_io_lock,
774                         .cio_unlock    = lov_io_unlock,
775                         .cio_start     = lov_io_start,
776                         .cio_end       = lov_io_end
777                 },
778                 [CIT_SETATTR] = {
779                         .cio_fini      = lov_io_fini,
780                         .cio_iter_init = lov_io_iter_init,
781                         .cio_iter_fini = lov_io_iter_fini,
782                         .cio_lock      = lov_io_lock,
783                         .cio_unlock    = lov_io_unlock,
784                         .cio_start     = lov_io_start,
785                         .cio_end       = lov_io_end
786                 },
787                 [CIT_FAULT] = {
788                         .cio_fini      = lov_io_fini,
789                         .cio_iter_init = lov_io_iter_init,
790                         .cio_iter_fini = lov_io_iter_fini,
791                         .cio_lock      = lov_io_lock,
792                         .cio_unlock    = lov_io_unlock,
793                         .cio_start     = lov_io_fault_start,
794                         .cio_end       = lov_io_end
795                 },
796                 [CIT_FSYNC] = {
797                         .cio_fini      = lov_io_fini,
798                         .cio_iter_init = lov_io_iter_init,
799                         .cio_iter_fini = lov_io_iter_fini,
800                         .cio_lock      = lov_io_lock,
801                         .cio_unlock    = lov_io_unlock,
802                         .cio_start     = lov_io_start,
803                         .cio_end       = lov_io_fsync_end
804                 },
805                 [CIT_MISC] = {
806                         .cio_fini      = lov_io_fini
807                 }
808         },
809         .cio_submit                    = lov_io_submit,
810         .cio_commit_async              = lov_io_commit_async,
811 };
812
813 /*****************************************************************************
814  *
815  * Empty lov io operations.
816  *
817  */
818
819 static void lov_empty_io_fini(const struct lu_env *env,
820                               const struct cl_io_slice *ios)
821 {
822         struct lov_object *lov = cl2lov(ios->cis_obj);
823         ENTRY;
824
825         if (cfs_atomic_dec_and_test(&lov->lo_active_ios))
826                 wake_up_all(&lov->lo_waitq);
827         EXIT;
828 }
829
830 static void lov_empty_impossible(const struct lu_env *env,
831                                  struct cl_io_slice *ios)
832 {
833         LBUG();
834 }
835
836 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
837
838 /**
839  * An io operation vector for files without stripes.
840  */
841 static const struct cl_io_operations lov_empty_io_ops = {
842         .op = {
843                 [CIT_READ] = {
844                         .cio_fini       = lov_empty_io_fini,
845 #if 0
846                         .cio_iter_init  = LOV_EMPTY_IMPOSSIBLE,
847                         .cio_lock       = LOV_EMPTY_IMPOSSIBLE,
848                         .cio_start      = LOV_EMPTY_IMPOSSIBLE,
849                         .cio_end        = LOV_EMPTY_IMPOSSIBLE
850 #endif
851                 },
852                 [CIT_WRITE] = {
853                         .cio_fini      = lov_empty_io_fini,
854                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
855                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
856                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
857                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
858                 },
859                 [CIT_SETATTR] = {
860                         .cio_fini      = lov_empty_io_fini,
861                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
862                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
863                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
864                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
865                 },
866                 [CIT_FAULT] = {
867                         .cio_fini      = lov_empty_io_fini,
868                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
869                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
870                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
871                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
872                 },
873                 [CIT_FSYNC] = {
874                         .cio_fini      = lov_empty_io_fini
875                 },
876                 [CIT_MISC] = {
877                         .cio_fini      = lov_empty_io_fini
878                 }
879         },
880         .cio_submit                    = LOV_EMPTY_IMPOSSIBLE,
881         .cio_commit_async              = LOV_EMPTY_IMPOSSIBLE
882 };
883
884 int lov_io_init_raid0(const struct lu_env *env, struct cl_object *obj,
885                       struct cl_io *io)
886 {
887         struct lov_io       *lio = lov_env_io(env);
888         struct lov_object   *lov = cl2lov(obj);
889
890         ENTRY;
891         CFS_INIT_LIST_HEAD(&lio->lis_active);
892         lov_io_slice_init(lio, lov, io);
893         if (io->ci_result == 0) {
894                 io->ci_result = lov_io_subio_init(env, lio, io);
895                 if (io->ci_result == 0) {
896                         cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
897                         cfs_atomic_inc(&lov->lo_active_ios);
898                 }
899         }
900         RETURN(io->ci_result);
901 }
902
903 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
904                       struct cl_io *io)
905 {
906         struct lov_object *lov = cl2lov(obj);
907         struct lov_io *lio = lov_env_io(env);
908         int result;
909         ENTRY;
910
911         lio->lis_object = lov;
912         switch (io->ci_type) {
913         default:
914                 LBUG();
915         case CIT_MISC:
916         case CIT_READ:
917                 result = 0;
918                 break;
919         case CIT_FSYNC:
920         case CIT_SETATTR:
921                 result = +1;
922                 break;
923         case CIT_WRITE:
924                 result = -EBADF;
925                 break;
926         case CIT_FAULT:
927                 result = -EFAULT;
928                 CERROR("Page fault on a file without stripes: "DFID"\n",
929                        PFID(lu_object_fid(&obj->co_lu)));
930                 break;
931         }
932         if (result == 0) {
933                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
934                 cfs_atomic_inc(&lov->lo_active_ios);
935         }
936
937         io->ci_result = result < 0 ? result : 0;
938         RETURN(result != 0);
939 }
940
941 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
942                         struct cl_io *io)
943 {
944         struct lov_object *lov = cl2lov(obj);
945         struct lov_io *lio = lov_env_io(env);
946         int result;
947         ENTRY;
948
949         LASSERT(lov->lo_lsm != NULL);
950         lio->lis_object = lov;
951
952         switch (io->ci_type) {
953         default:
954                 LASSERTF(0, "invalid type %d\n", io->ci_type);
955         case CIT_MISC:
956         case CIT_FSYNC:
957                 result = 1;
958                 break;
959         case CIT_SETATTR:
960                 /* the truncate to 0 is managed by MDT:
961                  * - in open, for open O_TRUNC
962                  * - in setattr, for truncate
963                  */
964                 /* the truncate is for size > 0 so triggers a restore */
965                 if (cl_io_is_trunc(io))
966                         io->ci_restore_needed = 1;
967                 result = -ENODATA;
968                 break;
969         case CIT_READ:
970         case CIT_WRITE:
971         case CIT_FAULT:
972                 io->ci_restore_needed = 1;
973                 result = -ENODATA;
974                 break;
975         }
976         if (result == 0) {
977                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
978                 cfs_atomic_inc(&lov->lo_active_ios);
979         }
980
981         io->ci_result = result < 0 ? result : 0;
982         RETURN(result != 0);
983 }
984 /** @} lov */