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