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