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