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