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