Whamcloud - gitweb
8b71f49b6b3748ec35f077b3a61f0f339e608daa
[fs/lustre-release.git] / lustre / lov / lov_io.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
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 void lov_sub_enter(struct lov_io_sub *sub)
50 {
51         ENTRY;
52         if (sub->sub_reenter++ == 0) {
53                 sub->sub_cookie = cl_env_reenter();
54                 cl_env_implant(sub->sub_env, &sub->sub_refcheck2);
55         }
56         EXIT;
57 }
58
59 static void lov_sub_exit(struct lov_io_sub *sub)
60 {
61         ENTRY;
62         if (--sub->sub_reenter == 0) {
63                 cl_env_unplant(sub->sub_env, &sub->sub_refcheck2);
64                 cl_env_reexit(sub->sub_cookie);
65         }
66         EXIT;
67 }
68
69 static void lov_io_sub_fini(const struct lu_env *env, struct lov_io *lio,
70                             struct lov_io_sub *sub)
71 {
72         ENTRY;
73         if (sub->sub_io != NULL) {
74                 if (sub->sub_io_initialized) {
75                         lov_sub_enter(sub);
76                         cl_io_fini(sub->sub_env, sub->sub_io);
77                         lov_sub_exit(sub);
78                         sub->sub_io_initialized = 0;
79                         lio->lis_active_subios--;
80                 }
81                 if (sub->sub_stripe == lio->lis_single_subio_index)
82                         lio->lis_single_subio_index = -1;
83                 else if (!sub->sub_borrowed)
84                         OBD_FREE_PTR(sub->sub_io);
85                 sub->sub_io = NULL;
86         }
87         if (sub->sub_env != NULL && !IS_ERR(sub->sub_env)) {
88                 if (!sub->sub_borrowed)
89                         cl_env_put(sub->sub_env, &sub->sub_refcheck);
90                 sub->sub_env = NULL;
91         }
92         EXIT;
93 }
94
95 static void lov_io_sub_inherit(struct cl_io *io, struct lov_io *lio,
96                                int stripe, loff_t start, loff_t end)
97 {
98         struct lov_stripe_md *lsm    = lov_r0(lio->lis_object)->lo_lsm;
99         struct cl_io         *parent = lio->lis_cl.cis_io;
100
101         switch(io->ci_type) {
102         case CIT_TRUNC: {
103                 size_t new_size = parent->u.ci_truncate.tr_size;
104
105                 new_size = lov_size_to_stripe(lsm, new_size, stripe);
106                 io->u.ci_truncate.tr_capa = parent->u.ci_truncate.tr_capa;
107                 io->u.ci_truncate.tr_size = new_size;
108                 break;
109         }
110         case CIT_FAULT: {
111                 struct cl_object *obj = parent->ci_obj;
112                 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
113
114                 io->u.ci_fault = parent->u.ci_fault;
115                 off = lov_size_to_stripe(lsm, off, stripe);
116                 io->u.ci_fault.ft_index = cl_index(obj, off);
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(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_TRUNC:
327                 lio->lis_pos = io->u.ci_truncate.tr_size;
328                 lio->lis_endpos = OBD_OBJECT_EOF;
329                 break;
330
331         case CIT_FAULT: {
332                 pgoff_t index = io->u.ci_fault.ft_index;
333                 lio->lis_pos = cl_offset(io->ci_obj, index);
334                 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
335                 break;
336         }
337
338         case CIT_MISC:
339                 lio->lis_pos = 0;
340                 lio->lis_endpos = OBD_OBJECT_EOF;
341                 break;
342
343         default:
344                 LBUG();
345         }
346
347         EXIT;
348 }
349
350 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
351 {
352         struct lov_io *lio = cl2lov_io(env, ios);
353         int i;
354
355         ENTRY;
356         if (lio->lis_subs != NULL) {
357                 for (i = 0; i < lio->lis_nr_subios; i++)
358                         lov_io_sub_fini(env, lio, &lio->lis_subs[i]);
359                 OBD_FREE(lio->lis_subs,
360                          lio->lis_nr_subios * sizeof lio->lis_subs[0]);
361                 lio->lis_nr_subios = 0;
362         }
363         EXIT;
364 }
365
366 static obd_off lov_offset_mod(obd_off val, int delta)
367 {
368         if (val != OBD_OBJECT_EOF)
369                 val += delta;
370         return val;
371 }
372
373 static int lov_io_iter_init(const struct lu_env *env,
374                             const struct cl_io_slice *ios)
375 {
376         struct lov_io        *lio = cl2lov_io(env, ios);
377         struct lov_stripe_md *lsm = lov_r0(lio->lis_object)->lo_lsm;
378         struct lov_io_sub    *sub;
379         obd_off endpos;
380         obd_off start;
381         obd_off end;
382         int stripe;
383         int rc = 0;
384
385         ENTRY;
386         endpos = lov_offset_mod(lio->lis_endpos, -1);
387         for (stripe = 0; stripe < lio->lis_stripe_count; stripe++) {
388                 if (!lov_stripe_intersects(lsm, stripe, lio->lis_pos,
389                                            endpos, &start, &end))
390                         continue;
391
392                 end = lov_offset_mod(end, +1);
393                 sub = lov_sub_get(env, lio, stripe);
394                 if (!IS_ERR(sub)) {
395                         lov_io_sub_inherit(sub->sub_io, lio, stripe,
396                                            start, end);
397                         rc = cl_io_iter_init(sub->sub_env, sub->sub_io);
398                         lov_sub_put(sub);
399                         CDEBUG(D_VFSTRACE, "shrink: %i [%llu, %llu)\n",
400                                stripe, start, end);
401                 } else
402                         rc = PTR_ERR(sub);
403
404                 if (!rc)
405                         cfs_list_add_tail(&sub->sub_linkage, &lio->lis_active);
406                 else
407                         break;
408         }
409         RETURN(rc);
410 }
411
412 static int lov_io_rw_iter_init(const struct lu_env *env,
413                                const struct cl_io_slice *ios)
414 {
415         struct lov_io        *lio = cl2lov_io(env, ios);
416         struct cl_io         *io  = ios->cis_io;
417         struct lov_stripe_md *lsm = lov_r0(cl2lov(ios->cis_obj))->lo_lsm;
418         loff_t start = io->u.ci_rw.crw_pos;
419         loff_t next;
420         unsigned long ssize = lsm->lsm_stripe_size;
421
422         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
423         ENTRY;
424
425         /* fast path for common case. */
426         if (lio->lis_nr_subios != 1 && !cl_io_is_append(io)) {
427
428                 do_div(start, ssize);
429                 next = (start + 1) * ssize;
430                 if (next <= start * ssize)
431                         next = ~0ull;
432
433                 io->ci_continue = next < lio->lis_io_endpos;
434                 io->u.ci_rw.crw_count = min_t(loff_t, lio->lis_io_endpos,
435                                               next) - io->u.ci_rw.crw_pos;
436                 lio->lis_pos    = io->u.ci_rw.crw_pos;
437                 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
438                 CDEBUG(D_VFSTRACE, "stripe: %llu chunk: [%llu, %llu) %llu\n",
439                        (__u64)start, lio->lis_pos, lio->lis_endpos,
440                        (__u64)lio->lis_io_endpos);
441         }
442         /*
443          * XXX The following call should be optimized: we know, that
444          * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
445          */
446         RETURN(lov_io_iter_init(env, ios));
447 }
448
449 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
450                        int (*iofunc)(const struct lu_env *, struct cl_io *))
451 {
452         struct lov_io_sub *sub;
453         int rc = 0;
454
455         ENTRY;
456         cfs_list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
457                 lov_sub_enter(sub);
458                 rc = iofunc(sub->sub_env, sub->sub_io);
459                 lov_sub_exit(sub);
460                 if (rc)
461                         break;
462         }
463         RETURN(rc);
464 }
465
466 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
467 {
468         ENTRY;
469         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
470 }
471
472 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
473 {
474         ENTRY;
475         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
476 }
477
478 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
479 {
480         ENTRY;
481         /*
482          * It's possible that lov_io_start() wasn't called against this
483          * sub-io, either because previous sub-io failed, or upper layer
484          * completed IO.
485          */
486         if (io->ci_state == CIS_IO_GOING)
487                 cl_io_end(env, io);
488         else
489                 io->ci_state = CIS_IO_FINISHED;
490         RETURN(0);
491 }
492
493 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
494 {
495         cl_io_iter_fini(env, io);
496         RETURN(0);
497 }
498
499 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
500 {
501         cl_io_unlock(env, io);
502         RETURN(0);
503 }
504
505 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
506 {
507         int rc;
508
509         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
510         LASSERT(rc == 0);
511 }
512
513 static void lov_io_iter_fini(const struct lu_env *env,
514                              const struct cl_io_slice *ios)
515 {
516         struct lov_io *lio = cl2lov_io(env, ios);
517         int rc;
518
519         ENTRY;
520         rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
521         LASSERT(rc == 0);
522         while (!cfs_list_empty(&lio->lis_active))
523                 cfs_list_del_init(lio->lis_active.next);
524         EXIT;
525 }
526
527 static void lov_io_unlock(const struct lu_env *env,
528                           const struct cl_io_slice *ios)
529 {
530         int rc;
531
532         ENTRY;
533         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
534         LASSERT(rc == 0);
535         EXIT;
536 }
537
538
539 static struct cl_page_list *lov_io_submit_qin(struct lov_device *ld,
540                                               struct cl_page_list *qin,
541                                               int idx, int alloc)
542 {
543         return alloc ? &qin[idx] : &ld->ld_emrg[idx]->emrg_page_list;
544 }
545
546 /**
547  * lov implementation of cl_operations::cio_submit() method. It takes a list
548  * of pages in \a queue, splits it into per-stripe sub-lists, invokes
549  * cl_io_submit() on underlying devices to submit sub-lists, and then splices
550  * everything back.
551  *
552  * Major complication of this function is a need to handle memory cleansing:
553  * cl_io_submit() is called to write out pages as a part of VM memory
554  * reclamation, and hence it may not fail due to memory shortages (system
555  * dead-locks otherwise). To deal with this, some resources (sub-lists,
556  * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
557  * not-memory cleansing context), and in case of memory shortage, these
558  * pre-allocated resources are used by lov_io_submit() under
559  * lov_device::ld_mutex mutex.
560  */
561 static int lov_io_submit(const struct lu_env *env,
562                          const struct cl_io_slice *ios,
563                          enum cl_req_type crt, struct cl_2queue *queue,
564                          enum cl_req_priority priority)
565 {
566         struct lov_io          *lio = cl2lov_io(env, ios);
567         struct lov_object      *obj = lio->lis_object;
568         struct lov_device       *ld = lu2lov_dev(lov2cl(obj)->co_lu.lo_dev);
569         struct cl_page_list    *qin = &queue->c2_qin;
570         struct cl_2queue      *cl2q = &lov_env_info(env)->lti_cl2q;
571         struct cl_page_list *stripes_qin = NULL;
572         struct cl_page *page;
573         struct cl_page *tmp;
574         int stripe;
575
576 #define QIN(stripe) lov_io_submit_qin(ld, stripes_qin, stripe, alloc)
577
578         int rc = 0;
579         int alloc =
580 #if defined(__KERNEL__) && defined(__linux__)
581                 !(current->flags & PF_MEMALLOC);
582 #else
583                 1;
584 #endif
585         ENTRY;
586         if (lio->lis_active_subios == 1) {
587                 int idx = lio->lis_single_subio_index;
588                 struct lov_io_sub *sub;
589
590                 LASSERT(idx < lio->lis_nr_subios);
591                 sub = lov_sub_get(env, lio, idx);
592                 LASSERT(!IS_ERR(sub));
593                 LASSERT(sub->sub_io == &lio->lis_single_subio);
594                 rc = cl_io_submit_rw(sub->sub_env, sub->sub_io,
595                                      crt, queue, priority);
596                 lov_sub_put(sub);
597                 RETURN(rc);
598         }
599
600         LASSERT(lio->lis_subs != NULL);
601         if (alloc) {
602                 OBD_ALLOC(stripes_qin,
603                           sizeof(*stripes_qin) * lio->lis_nr_subios);
604                 if (stripes_qin == NULL)
605                         RETURN(-ENOMEM);
606
607                 for (stripe = 0; stripe < lio->lis_nr_subios; stripe++)
608                         cl_page_list_init(&stripes_qin[stripe]);
609         } else {
610                 /*
611                  * If we get here, it means pageout & swap doesn't help.
612                  * In order to not make things worse, even don't try to
613                  * allocate the memory with __GFP_NOWARN. -jay
614                  */
615                 cfs_mutex_lock(&ld->ld_mutex);
616                 lio->lis_mem_frozen = 1;
617         }
618
619         cl_2queue_init(cl2q);
620         cl_page_list_for_each_safe(page, tmp, qin) {
621                 stripe = lov_page_stripe(page);
622                 cl_page_list_move(QIN(stripe), qin, page);
623         }
624
625         for (stripe = 0; stripe < lio->lis_nr_subios; stripe++) {
626                 struct lov_io_sub   *sub;
627                 struct cl_page_list *sub_qin = QIN(stripe);
628
629                 if (cfs_list_empty(&sub_qin->pl_pages))
630                         continue;
631
632                 cl_page_list_splice(sub_qin, &cl2q->c2_qin);
633                 sub = lov_sub_get(env, lio, stripe);
634                 if (!IS_ERR(sub)) {
635                         rc = cl_io_submit_rw(sub->sub_env, sub->sub_io,
636                                              crt, cl2q, priority);
637                         lov_sub_put(sub);
638                 } else
639                         rc = PTR_ERR(sub);
640                 cl_page_list_splice(&cl2q->c2_qin,  &queue->c2_qin);
641                 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
642                 if (rc != 0)
643                         break;
644         }
645
646         for (stripe = 0; stripe < lio->lis_nr_subios; stripe++) {
647                 struct cl_page_list *sub_qin = QIN(stripe);
648
649                 if (cfs_list_empty(&sub_qin->pl_pages))
650                         continue;
651
652                 cl_page_list_splice(sub_qin, qin);
653         }
654
655         if (alloc) {
656                 OBD_FREE(stripes_qin,
657                          sizeof(*stripes_qin) * lio->lis_nr_subios);
658         } else {
659                 int i;
660
661                 for (i = 0; i < lio->lis_nr_subios; i++) {
662                         struct cl_io *cio = lio->lis_subs[i].sub_io;
663
664                         if (cio && cio == &ld->ld_emrg[i]->emrg_subio)
665                                 lov_io_sub_fini(env, lio, &lio->lis_subs[i]);
666                 }
667                 lio->lis_mem_frozen = 0;
668                 cfs_mutex_unlock(&ld->ld_mutex);
669         }
670
671         RETURN(rc);
672 #undef QIN
673 }
674
675 static int lov_io_prepare_write(const struct lu_env *env,
676                                 const struct cl_io_slice *ios,
677                                 const struct cl_page_slice *slice,
678                                 unsigned from, unsigned to)
679 {
680         struct lov_io     *lio      = cl2lov_io(env, ios);
681         struct cl_page    *sub_page = lov_sub_page(slice);
682         struct lov_io_sub *sub;
683         int result;
684
685         ENTRY;
686         sub = lov_page_subio(env, lio, slice);
687         if (!IS_ERR(sub)) {
688                 result = cl_io_prepare_write(sub->sub_env, sub->sub_io,
689                                              sub_page, from, to);
690                 lov_sub_put(sub);
691         } else
692                 result = PTR_ERR(sub);
693         RETURN(result);
694 }
695
696 static int lov_io_commit_write(const struct lu_env *env,
697                                const struct cl_io_slice *ios,
698                                const struct cl_page_slice *slice,
699                                unsigned from, unsigned to)
700 {
701         struct lov_io     *lio      = cl2lov_io(env, ios);
702         struct cl_page    *sub_page = lov_sub_page(slice);
703         struct lov_io_sub *sub;
704         int result;
705
706         ENTRY;
707         sub = lov_page_subio(env, lio, slice);
708         if (!IS_ERR(sub)) {
709                 result = cl_io_commit_write(sub->sub_env, sub->sub_io,
710                                             sub_page, from, to);
711                 lov_sub_put(sub);
712         } else
713                 result = PTR_ERR(sub);
714         RETURN(result);
715 }
716
717 static int lov_io_fault_start(const struct lu_env *env,
718                               const struct cl_io_slice *ios)
719 {
720         struct cl_fault_io *fio;
721         struct lov_io      *lio;
722         struct lov_io_sub  *sub;
723
724         ENTRY;
725         fio = &ios->cis_io->u.ci_fault;
726         lio = cl2lov_io(env, ios);
727         sub = lov_sub_get(env, lio, lov_page_stripe(fio->ft_page));
728         sub->sub_io->u.ci_fault.ft_nob = fio->ft_nob;
729         lov_sub_put(sub);
730         RETURN(lov_io_start(env, ios));
731 }
732
733 static const struct cl_io_operations lov_io_ops = {
734         .op = {
735                 [CIT_READ] = {
736                         .cio_fini      = lov_io_fini,
737                         .cio_iter_init = lov_io_rw_iter_init,
738                         .cio_iter_fini = lov_io_iter_fini,
739                         .cio_lock      = lov_io_lock,
740                         .cio_unlock    = lov_io_unlock,
741                         .cio_start     = lov_io_start,
742                         .cio_end       = lov_io_end
743                 },
744                 [CIT_WRITE] = {
745                         .cio_fini      = lov_io_fini,
746                         .cio_iter_init = lov_io_rw_iter_init,
747                         .cio_iter_fini = lov_io_iter_fini,
748                         .cio_lock      = lov_io_lock,
749                         .cio_unlock    = lov_io_unlock,
750                         .cio_start     = lov_io_start,
751                         .cio_end       = lov_io_end
752                 },
753                 [CIT_TRUNC] = {
754                         .cio_fini      = lov_io_fini,
755                         .cio_iter_init = lov_io_iter_init,
756                         .cio_iter_fini = lov_io_iter_fini,
757                         .cio_lock      = lov_io_lock,
758                         .cio_unlock    = lov_io_unlock,
759                         .cio_start     = lov_io_start,
760                         .cio_end       = lov_io_end
761                 },
762                 [CIT_FAULT] = {
763                         .cio_fini      = lov_io_fini,
764                         .cio_iter_init = lov_io_iter_init,
765                         .cio_iter_fini = lov_io_iter_fini,
766                         .cio_lock      = lov_io_lock,
767                         .cio_unlock    = lov_io_unlock,
768                         .cio_start     = lov_io_fault_start,
769                         .cio_end       = lov_io_end
770                 },
771                 [CIT_MISC] = {
772                         .cio_fini   = lov_io_fini
773                 }
774         },
775         .req_op = {
776                  [CRT_READ] = {
777                          .cio_submit    = lov_io_submit
778                  },
779                  [CRT_WRITE] = {
780                          .cio_submit    = lov_io_submit
781                  }
782          },
783         .cio_prepare_write = lov_io_prepare_write,
784         .cio_commit_write  = lov_io_commit_write
785 };
786
787 /*****************************************************************************
788  *
789  * Empty lov io operations.
790  *
791  */
792
793 static void lov_empty_io_fini(const struct lu_env *env,
794                               const struct cl_io_slice *ios)
795 {
796         ENTRY;
797         EXIT;
798 }
799
800 static void lov_empty_impossible(const struct lu_env *env,
801                                  struct cl_io_slice *ios)
802 {
803         LBUG();
804 }
805
806 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
807
808 /**
809  * An io operation vector for files without stripes.
810  */
811 static const struct cl_io_operations lov_empty_io_ops = {
812         .op = {
813                 [CIT_READ] = {
814 #if 0
815                         .cio_fini       = lov_empty_io_fini,
816                         .cio_iter_init  = LOV_EMPTY_IMPOSSIBLE,
817                         .cio_lock       = LOV_EMPTY_IMPOSSIBLE,
818                         .cio_start      = LOV_EMPTY_IMPOSSIBLE,
819                         .cio_end        = LOV_EMPTY_IMPOSSIBLE
820 #endif
821                 },
822                 [CIT_WRITE] = {
823                         .cio_fini      = lov_empty_io_fini,
824                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
825                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
826                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
827                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
828                 },
829                 [CIT_TRUNC] = {
830                         .cio_fini      = lov_empty_io_fini,
831                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
832                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
833                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
834                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
835                 },
836                 [CIT_FAULT] = {
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                 },
843                 [CIT_MISC] = {
844                         .cio_fini   = lov_empty_io_fini
845                 }
846         },
847         .req_op = {
848                  [CRT_READ] = {
849                          .cio_submit    = LOV_EMPTY_IMPOSSIBLE
850                  },
851                  [CRT_WRITE] = {
852                          .cio_submit    = LOV_EMPTY_IMPOSSIBLE
853                  }
854          },
855         .cio_commit_write = LOV_EMPTY_IMPOSSIBLE
856 };
857
858 int lov_io_init_raid0(const struct lu_env *env, struct cl_object *obj,
859                       struct cl_io *io)
860 {
861         struct lov_io       *lio = lov_env_io(env);
862         struct lov_object   *lov = cl2lov(obj);
863
864         ENTRY;
865         CFS_INIT_LIST_HEAD(&lio->lis_active);
866         lov_io_slice_init(lio, lov, io);
867         if (io->ci_result == 0) {
868                 LASSERT(lov_r0(lov)->lo_lsm != NULL);
869                 io->ci_result = lov_io_subio_init(env, lio, io);
870                 if (io->ci_result == 0)
871                         cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
872         }
873         RETURN(io->ci_result);
874 }
875
876 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
877                       struct cl_io *io)
878 {
879         struct lov_io *lio = lov_env_io(env);
880         int result;
881
882         ENTRY;
883         switch (io->ci_type) {
884         default:
885                 LBUG();
886         case CIT_MISC:
887         case CIT_READ:
888                 result = 0;
889                 break;
890         case CIT_WRITE:
891         case CIT_TRUNC:
892                 result = -EBADF;
893                 break;
894         case CIT_FAULT:
895                 result = -EFAULT;
896                 CERROR("Page fault on a file without stripes: "DFID"\n",
897                        PFID(lu_object_fid(&obj->co_lu)));
898                 break;
899         }
900         if (result == 0)
901                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
902         io->ci_result = result;
903         RETURN(result != 0);
904 }
905
906 /** @} lov */