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