Whamcloud - gitweb
LU-1154 clio: pass fid for OST setattr
[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, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Implementation of cl_io for LOV layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  *   Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LOV
43
44 #include "lov_cl_internal.h"
45
46 /** \addtogroup lov
47  *  @{
48  */
49
50 static inline void lov_sub_enter(struct lov_io_sub *sub)
51 {
52         sub->sub_reenter++;
53 }
54 static inline void lov_sub_exit(struct lov_io_sub *sub)
55 {
56         sub->sub_reenter--;
57 }
58
59 static void lov_io_sub_fini(const struct lu_env *env, struct lov_io *lio,
60                             struct lov_io_sub *sub)
61 {
62         ENTRY;
63         if (sub->sub_io != NULL) {
64                 if (sub->sub_io_initialized) {
65                         lov_sub_enter(sub);
66                         cl_io_fini(sub->sub_env, sub->sub_io);
67                         lov_sub_exit(sub);
68                         sub->sub_io_initialized = 0;
69                         lio->lis_active_subios--;
70                 }
71                 if (sub->sub_stripe == lio->lis_single_subio_index)
72                         lio->lis_single_subio_index = -1;
73                 else if (!sub->sub_borrowed)
74                         OBD_FREE_PTR(sub->sub_io);
75                 sub->sub_io = NULL;
76         }
77         if (sub->sub_env != NULL && !IS_ERR(sub->sub_env)) {
78                 if (!sub->sub_borrowed)
79                         cl_env_put(sub->sub_env, &sub->sub_refcheck);
80                 sub->sub_env = NULL;
81         }
82         EXIT;
83 }
84
85 static void lov_io_sub_inherit(struct cl_io *io, struct lov_io *lio,
86                                int stripe, loff_t start, loff_t end)
87 {
88         struct lov_stripe_md *lsm    = lio->lis_object->lo_lsm;
89         struct cl_io         *parent = lio->lis_cl.cis_io;
90
91         switch (io->ci_type) {
92         case CIT_SETATTR: {
93                 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
94                 io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
95                 io->u.ci_setattr.sa_stripe_index = stripe;
96                 io->u.ci_setattr.sa_parent_fid =
97                                         parent->u.ci_setattr.sa_parent_fid;
98                 io->u.ci_setattr.sa_capa = parent->u.ci_setattr.sa_capa;
99                 if (cl_io_is_trunc(io)) {
100                         loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
101
102                         new_size = lov_size_to_stripe(lsm, new_size, stripe);
103                         io->u.ci_setattr.sa_attr.lvb_size = new_size;
104                 }
105                 break;
106         }
107         case CIT_FAULT: {
108                 struct cl_object *obj = parent->ci_obj;
109                 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
110
111                 io->u.ci_fault = parent->u.ci_fault;
112                 off = lov_size_to_stripe(lsm, off, stripe);
113                 io->u.ci_fault.ft_index = cl_index(obj, off);
114                 break;
115         }
116         case CIT_FSYNC: {
117                 io->u.ci_fsync.fi_start = start;
118                 io->u.ci_fsync.fi_end = end;
119                 io->u.ci_fsync.fi_capa = parent->u.ci_fsync.fi_capa;
120                 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
121                 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
122                 break;
123         }
124         case CIT_READ:
125         case CIT_WRITE: {
126                 io->u.ci_wr.wr_sync = cl_io_is_sync_write(parent);
127                 if (cl_io_is_append(parent)) {
128                         io->u.ci_wr.wr_append = 1;
129                 } else {
130                         io->u.ci_rw.crw_pos = start;
131                         io->u.ci_rw.crw_count = end - start;
132                 }
133                 break;
134         }
135         default:
136                 break;
137         }
138 }
139
140 static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
141                            struct lov_io_sub *sub)
142 {
143         struct lov_object *lov = lio->lis_object;
144         struct lov_device *ld  = lu2lov_dev(lov2cl(lov)->co_lu.lo_dev);
145         struct cl_io      *sub_io;
146         struct cl_object  *sub_obj;
147         struct cl_io      *io  = lio->lis_cl.cis_io;
148
149         int stripe = sub->sub_stripe;
150         int result;
151
152         LASSERT(sub->sub_io == NULL);
153         LASSERT(sub->sub_env == NULL);
154         LASSERT(sub->sub_stripe < lio->lis_stripe_count);
155         ENTRY;
156
157         if (unlikely(lov_r0(lov)->lo_sub[stripe] == NULL))
158                 RETURN(-EIO);
159
160         result = 0;
161         sub->sub_io_initialized = 0;
162         sub->sub_borrowed = 0;
163
164         if (lio->lis_mem_frozen) {
165                 LASSERT(mutex_is_locked(&ld->ld_mutex));
166                 sub->sub_io  = &ld->ld_emrg[stripe]->emrg_subio;
167                 sub->sub_env = ld->ld_emrg[stripe]->emrg_env;
168                 sub->sub_borrowed = 1;
169         } else {
170                 void *cookie;
171
172                 /* obtain new environment */
173                 cookie = cl_env_reenter();
174                 sub->sub_env = cl_env_get(&sub->sub_refcheck);
175                 cl_env_reexit(cookie);
176                 if (IS_ERR(sub->sub_env))
177                         result = PTR_ERR(sub->sub_env);
178
179                 if (result == 0) {
180                         /*
181                          * First sub-io. Use ->lis_single_subio to
182                          * avoid dynamic allocation.
183                          */
184                         if (lio->lis_active_subios == 0) {
185                                 sub->sub_io = &lio->lis_single_subio;
186                                 lio->lis_single_subio_index = stripe;
187                         } else {
188                                 OBD_ALLOC_PTR(sub->sub_io);
189                                 if (sub->sub_io == NULL)
190                                         result = -ENOMEM;
191                         }
192                 }
193         }
194
195         if (result == 0) {
196                 sub_obj = lovsub2cl(lov_r0(lov)->lo_sub[stripe]);
197                 sub_io  = sub->sub_io;
198
199                 sub_io->ci_obj    = sub_obj;
200                 sub_io->ci_result = 0;
201
202                 sub_io->ci_parent  = io;
203                 sub_io->ci_lockreq = io->ci_lockreq;
204                 sub_io->ci_type    = io->ci_type;
205                 sub_io->ci_no_srvlock = io->ci_no_srvlock;
206                 sub_io->ci_noatime = io->ci_noatime;
207
208                 lov_sub_enter(sub);
209                 result = cl_io_sub_init(sub->sub_env, sub_io,
210                                         io->ci_type, sub_obj);
211                 lov_sub_exit(sub);
212                 if (result >= 0) {
213                         lio->lis_active_subios++;
214                         sub->sub_io_initialized = 1;
215                         result = 0;
216                 }
217         }
218         if (result != 0)
219                 lov_io_sub_fini(env, lio, sub);
220         RETURN(result);
221 }
222
223 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
224                                struct lov_io *lio, int stripe)
225 {
226         int rc;
227         struct lov_io_sub *sub = &lio->lis_subs[stripe];
228
229         LASSERT(stripe < lio->lis_stripe_count);
230         ENTRY;
231
232         if (!sub->sub_io_initialized) {
233                 sub->sub_stripe = stripe;
234                 rc = lov_io_sub_init(env, lio, sub);
235         } else
236                 rc = 0;
237         if (rc == 0)
238                 lov_sub_enter(sub);
239         else
240                 sub = ERR_PTR(rc);
241         RETURN(sub);
242 }
243
244 void lov_sub_put(struct lov_io_sub *sub)
245 {
246         lov_sub_exit(sub);
247 }
248
249 /*****************************************************************************
250  *
251  * Lov io operations.
252  *
253  */
254
255 int lov_page_stripe(const struct cl_page *page)
256 {
257         const struct cl_page_slice *slice;
258         ENTRY;
259
260         slice = cl_page_at(page, &lov_device_type);
261         LASSERT(slice != NULL);
262         LASSERT(slice->cpl_obj != NULL);
263
264         RETURN(cl2lov_page(slice)->lps_stripe);
265 }
266
267 struct lov_io_sub *lov_page_subio(const struct lu_env *env, struct lov_io *lio,
268                                   const struct cl_page_slice *slice)
269 {
270         struct lov_stripe_md *lsm  = lio->lis_object->lo_lsm;
271         struct cl_page       *page = slice->cpl_page;
272         int stripe;
273
274         LASSERT(lio->lis_cl.cis_io != NULL);
275         LASSERT(cl2lov(slice->cpl_obj) == lio->lis_object);
276         LASSERT(lsm != NULL);
277         LASSERT(lio->lis_nr_subios > 0);
278         ENTRY;
279
280         stripe = lov_page_stripe(page);
281         RETURN(lov_sub_get(env, lio, stripe));
282 }
283
284
285 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
286                              struct cl_io *io)
287 {
288         struct lov_stripe_md *lsm;
289         int result;
290         ENTRY;
291
292         LASSERT(lio->lis_object != NULL);
293         lsm = lio->lis_object->lo_lsm;
294
295         /*
296          * Need to be optimized, we can't afford to allocate a piece of memory
297          * when writing a page. -jay
298          */
299         OBD_ALLOC_LARGE(lio->lis_subs,
300                         lsm->lsm_stripe_count * sizeof lio->lis_subs[0]);
301         if (lio->lis_subs != NULL) {
302                 lio->lis_nr_subios = lio->lis_stripe_count;
303                 lio->lis_single_subio_index = -1;
304                 lio->lis_active_subios = 0;
305                 result = 0;
306         } else
307                 result = -ENOMEM;
308         RETURN(result);
309 }
310
311 static int lov_io_slice_init(struct lov_io *lio,
312                              struct lov_object *obj, struct cl_io *io)
313 {
314         ENTRY;
315
316         io->ci_result = 0;
317         lio->lis_object = obj;
318
319         LASSERT(obj->lo_lsm != NULL);
320         lio->lis_stripe_count = obj->lo_lsm->lsm_stripe_count;
321
322         switch (io->ci_type) {
323         case CIT_READ:
324         case CIT_WRITE:
325                 lio->lis_pos = io->u.ci_rw.crw_pos;
326                 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
327                 lio->lis_io_endpos = lio->lis_endpos;
328                 if (cl_io_is_append(io)) {
329                         LASSERT(io->ci_type == CIT_WRITE);
330
331                         /* If there is LOV EA hole, then we may cannot locate
332                          * the current file-tail exactly. */
333                         if (unlikely(obj->lo_lsm->lsm_pattern &
334                                      LOV_PATTERN_F_HOLE))
335                                 RETURN(-EIO);
336
337                         lio->lis_pos = 0;
338                         lio->lis_endpos = OBD_OBJECT_EOF;
339                 }
340                 break;
341
342         case CIT_SETATTR:
343                 if (cl_io_is_trunc(io))
344                         lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
345                 else
346                         lio->lis_pos = 0;
347                 lio->lis_endpos = OBD_OBJECT_EOF;
348                 break;
349
350         case CIT_FAULT: {
351                 pgoff_t index = io->u.ci_fault.ft_index;
352                 lio->lis_pos = cl_offset(io->ci_obj, index);
353                 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
354                 break;
355         }
356
357         case CIT_FSYNC: {
358                 lio->lis_pos = io->u.ci_fsync.fi_start;
359                 lio->lis_endpos = io->u.ci_fsync.fi_end;
360                 break;
361         }
362
363         case CIT_MISC:
364                 lio->lis_pos = 0;
365                 lio->lis_endpos = OBD_OBJECT_EOF;
366                 break;
367
368         default:
369                 LBUG();
370         }
371
372         RETURN(0);
373 }
374
375 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
376 {
377         struct lov_io *lio = cl2lov_io(env, ios);
378         struct lov_object *lov = cl2lov(ios->cis_obj);
379         int i;
380
381         ENTRY;
382         if (lio->lis_subs != NULL) {
383                 for (i = 0; i < lio->lis_nr_subios; i++)
384                         lov_io_sub_fini(env, lio, &lio->lis_subs[i]);
385                 OBD_FREE_LARGE(lio->lis_subs,
386                          lio->lis_nr_subios * sizeof lio->lis_subs[0]);
387                 lio->lis_nr_subios = 0;
388         }
389
390         LASSERT(atomic_read(&lov->lo_active_ios) > 0);
391         if (atomic_dec_and_test(&lov->lo_active_ios))
392                 wake_up_all(&lov->lo_waitq);
393         EXIT;
394 }
395
396 static obd_off lov_offset_mod(obd_off val, int delta)
397 {
398         if (val != OBD_OBJECT_EOF)
399                 val += delta;
400         return val;
401 }
402
403 static int lov_io_iter_init(const struct lu_env *env,
404                             const struct cl_io_slice *ios)
405 {
406         struct lov_io        *lio = cl2lov_io(env, ios);
407         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
408         struct lov_io_sub    *sub;
409         obd_off endpos;
410         obd_off start;
411         obd_off end;
412         int stripe;
413         int rc = 0;
414
415         ENTRY;
416         endpos = lov_offset_mod(lio->lis_endpos, -1);
417         for (stripe = 0; stripe < lio->lis_stripe_count; stripe++) {
418                 if (!lov_stripe_intersects(lsm, stripe, lio->lis_pos,
419                                            endpos, &start, &end))
420                         continue;
421
422                 if (unlikely(lov_r0(lio->lis_object)->lo_sub[stripe] == NULL)) {
423                         if (ios->cis_io->ci_type == CIT_READ ||
424                             ios->cis_io->ci_type == CIT_WRITE ||
425                             ios->cis_io->ci_type == CIT_FAULT)
426                                 RETURN(-EIO);
427
428                         continue;
429                 }
430
431                 end = lov_offset_mod(end, +1);
432                 sub = lov_sub_get(env, lio, stripe);
433                 if (!IS_ERR(sub)) {
434                         lov_io_sub_inherit(sub->sub_io, lio, stripe,
435                                            start, end);
436                         rc = cl_io_iter_init(sub->sub_env, sub->sub_io);
437                         lov_sub_put(sub);
438                         CDEBUG(D_VFSTRACE, "shrink: %d ["LPU64", "LPU64")\n",
439                                stripe, start, end);
440                 } else
441                         rc = PTR_ERR(sub);
442
443                 if (!rc)
444                         list_add_tail(&sub->sub_linkage, &lio->lis_active);
445                 else
446                         break;
447         }
448         RETURN(rc);
449 }
450
451 static int lov_io_rw_iter_init(const struct lu_env *env,
452                                const struct cl_io_slice *ios)
453 {
454         struct lov_io        *lio = cl2lov_io(env, ios);
455         struct cl_io         *io  = ios->cis_io;
456         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
457         loff_t start = io->u.ci_rw.crw_pos;
458         loff_t next;
459         unsigned long ssize = lsm->lsm_stripe_size;
460
461         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
462         ENTRY;
463
464         /* fast path for common case. */
465         if (lio->lis_nr_subios != 1 && !cl_io_is_append(io)) {
466
467                 lov_do_div64(start, ssize);
468                 next = (start + 1) * ssize;
469                 if (next <= start * ssize)
470                         next = ~0ull;
471
472                 io->ci_continue = next < lio->lis_io_endpos;
473                 io->u.ci_rw.crw_count = min_t(loff_t, lio->lis_io_endpos,
474                                               next) - io->u.ci_rw.crw_pos;
475                 lio->lis_pos    = io->u.ci_rw.crw_pos;
476                 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
477                 CDEBUG(D_VFSTRACE, "stripe: "LPU64" chunk: ["LPU64", "LPU64") "
478                        LPU64"\n", (__u64)start, lio->lis_pos, lio->lis_endpos,
479                        (__u64)lio->lis_io_endpos);
480         }
481         /*
482          * XXX The following call should be optimized: we know, that
483          * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
484          */
485         RETURN(lov_io_iter_init(env, ios));
486 }
487
488 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
489                        int (*iofunc)(const struct lu_env *, struct cl_io *))
490 {
491         struct cl_io *parent = lio->lis_cl.cis_io;
492         struct lov_io_sub *sub;
493         int rc = 0;
494
495         ENTRY;
496         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
497                 lov_sub_enter(sub);
498                 rc = iofunc(sub->sub_env, sub->sub_io);
499                 lov_sub_exit(sub);
500                 if (rc)
501                         break;
502
503                 if (parent->ci_result == 0)
504                         parent->ci_result = sub->sub_io->ci_result;
505         }
506         RETURN(rc);
507 }
508
509 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
510 {
511         ENTRY;
512         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
513 }
514
515 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
516 {
517         ENTRY;
518         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
519 }
520
521 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
522 {
523         ENTRY;
524         /*
525          * It's possible that lov_io_start() wasn't called against this
526          * sub-io, either because previous sub-io failed, or upper layer
527          * completed IO.
528          */
529         if (io->ci_state == CIS_IO_GOING)
530                 cl_io_end(env, io);
531         else
532                 io->ci_state = CIS_IO_FINISHED;
533         RETURN(0);
534 }
535
536 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
537 {
538         cl_io_iter_fini(env, io);
539         RETURN(0);
540 }
541
542 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
543 {
544         cl_io_unlock(env, io);
545         RETURN(0);
546 }
547
548 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
549 {
550         int rc;
551
552         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
553         LASSERT(rc == 0);
554 }
555
556 static void lov_io_iter_fini(const struct lu_env *env,
557                              const struct cl_io_slice *ios)
558 {
559         struct lov_io *lio = cl2lov_io(env, ios);
560         int rc;
561
562         ENTRY;
563         rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
564         LASSERT(rc == 0);
565         while (!list_empty(&lio->lis_active))
566                 list_del_init(lio->lis_active.next);
567         EXIT;
568 }
569
570 static void lov_io_unlock(const struct lu_env *env,
571                           const struct cl_io_slice *ios)
572 {
573         int rc;
574
575         ENTRY;
576         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
577         LASSERT(rc == 0);
578         EXIT;
579 }
580
581 /**
582  * lov implementation of cl_operations::cio_submit() method. It takes a list
583  * of pages in \a queue, splits it into per-stripe sub-lists, invokes
584  * cl_io_submit() on underlying devices to submit sub-lists, and then splices
585  * everything back.
586  *
587  * Major complication of this function is a need to handle memory cleansing:
588  * cl_io_submit() is called to write out pages as a part of VM memory
589  * reclamation, and hence it may not fail due to memory shortages (system
590  * dead-locks otherwise). To deal with this, some resources (sub-lists,
591  * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
592  * not-memory cleansing context), and in case of memory shortage, these
593  * pre-allocated resources are used by lov_io_submit() under
594  * lov_device::ld_mutex mutex.
595  */
596 static int lov_io_submit(const struct lu_env *env,
597                          const struct cl_io_slice *ios,
598                          enum cl_req_type crt, struct cl_2queue *queue)
599 {
600         struct cl_page_list     *qin = &queue->c2_qin;
601         struct lov_io           *lio = cl2lov_io(env, ios);
602         struct lov_io_sub       *sub;
603         struct cl_page_list     *plist = &lov_env_info(env)->lti_plist;
604         struct cl_page          *page;
605         int stripe;
606         int rc = 0;
607         ENTRY;
608
609         if (lio->lis_active_subios == 1) {
610                 int idx = lio->lis_single_subio_index;
611
612                 LASSERT(idx < lio->lis_nr_subios);
613                 sub = lov_sub_get(env, lio, idx);
614                 LASSERT(!IS_ERR(sub));
615                 LASSERT(sub->sub_io == &lio->lis_single_subio);
616                 rc = cl_io_submit_rw(sub->sub_env, sub->sub_io,
617                                      crt, queue);
618                 lov_sub_put(sub);
619                 RETURN(rc);
620         }
621
622         LASSERT(lio->lis_subs != NULL);
623
624         cl_page_list_init(plist);
625         while (qin->pl_nr > 0) {
626                 struct cl_2queue  *cl2q = &lov_env_info(env)->lti_cl2q;
627
628                 cl_2queue_init(cl2q);
629
630                 page = cl_page_list_first(qin);
631                 cl_page_list_move(&cl2q->c2_qin, qin, page);
632
633                 stripe = lov_page_stripe(page);
634                 while (qin->pl_nr > 0) {
635                         page = cl_page_list_first(qin);
636                         if (stripe != lov_page_stripe(page))
637                                 break;
638
639                         cl_page_list_move(&cl2q->c2_qin, qin, page);
640                 }
641
642                 sub = lov_sub_get(env, lio, stripe);
643                 if (!IS_ERR(sub)) {
644                         rc = cl_io_submit_rw(sub->sub_env, sub->sub_io,
645                                              crt, cl2q);
646                         lov_sub_put(sub);
647                 } else {
648                         rc = PTR_ERR(sub);
649                 }
650
651                 cl_page_list_splice(&cl2q->c2_qin, plist);
652                 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
653                 cl_2queue_fini(env, cl2q);
654
655                 if (rc != 0)
656                         break;
657         }
658
659         cl_page_list_splice(plist, qin);
660         cl_page_list_fini(env, plist);
661
662         RETURN(rc);
663 }
664
665 static int lov_io_commit_async(const struct lu_env *env,
666                                const struct cl_io_slice *ios,
667                                struct cl_page_list *queue, int from, int to,
668                                cl_commit_cbt cb)
669 {
670         struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
671         struct lov_io     *lio = cl2lov_io(env, ios);
672         struct lov_io_sub *sub;
673         struct cl_page *page;
674         int rc = 0;
675         ENTRY;
676
677         if (lio->lis_active_subios == 1) {
678                 int idx = lio->lis_single_subio_index;
679
680                 LASSERT(idx < lio->lis_nr_subios);
681                 sub = lov_sub_get(env, lio, idx);
682                 LASSERT(!IS_ERR(sub));
683                 LASSERT(sub->sub_io == &lio->lis_single_subio);
684                 rc = cl_io_commit_async(sub->sub_env, sub->sub_io, queue,
685                                         from, to, cb);
686                 lov_sub_put(sub);
687                 RETURN(rc);
688         }
689
690         LASSERT(lio->lis_subs != NULL);
691
692         cl_page_list_init(plist);
693         while (queue->pl_nr > 0) {
694                 int stripe_to = to;
695                 int stripe;
696
697                 LASSERT(plist->pl_nr == 0);
698                 page = cl_page_list_first(queue);
699                 cl_page_list_move(plist, queue, page);
700
701                 stripe = lov_page_stripe(page);
702                 while (queue->pl_nr > 0) {
703                         page = cl_page_list_first(queue);
704                         if (stripe != lov_page_stripe(page))
705                                 break;
706
707                         cl_page_list_move(plist, queue, page);
708                 }
709
710                 if (queue->pl_nr > 0) /* still has more pages */
711                         stripe_to = PAGE_SIZE;
712
713                 sub = lov_sub_get(env, lio, stripe);
714                 if (!IS_ERR(sub)) {
715                         rc = cl_io_commit_async(sub->sub_env, sub->sub_io,
716                                                 plist, from, stripe_to, cb);
717                         lov_sub_put(sub);
718                 } else {
719                         rc = PTR_ERR(sub);
720                         break;
721                 }
722
723                 if (plist->pl_nr > 0) /* short write */
724                         break;
725
726                 from = 0;
727         }
728
729         /* for error case, add the page back into the qin list */
730         LASSERT(ergo(rc == 0, plist->pl_nr == 0));
731         while (plist->pl_nr > 0) {
732                 /* error occurred, add the uncommitted pages back into queue */
733                 page = cl_page_list_last(plist);
734                 cl_page_list_move_head(queue, plist, page);
735         }
736
737         RETURN(rc);
738 }
739
740 static int lov_io_fault_start(const struct lu_env *env,
741                               const struct cl_io_slice *ios)
742 {
743         struct cl_fault_io *fio;
744         struct lov_io      *lio;
745         struct lov_io_sub  *sub;
746
747         ENTRY;
748         fio = &ios->cis_io->u.ci_fault;
749         lio = cl2lov_io(env, ios);
750         sub = lov_sub_get(env, lio, lov_page_stripe(fio->ft_page));
751         sub->sub_io->u.ci_fault.ft_nob = fio->ft_nob;
752         lov_sub_put(sub);
753         RETURN(lov_io_start(env, ios));
754 }
755
756 static void lov_io_fsync_end(const struct lu_env *env,
757                              const struct cl_io_slice *ios)
758 {
759         struct lov_io *lio = cl2lov_io(env, ios);
760         struct lov_io_sub *sub;
761         unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
762         ENTRY;
763
764         *written = 0;
765         list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
766                 struct cl_io *subio = sub->sub_io;
767
768                 lov_sub_enter(sub);
769                 lov_io_end_wrapper(sub->sub_env, subio);
770                 lov_sub_exit(sub);
771
772                 if (subio->ci_result == 0)
773                         *written += subio->u.ci_fsync.fi_nr_written;
774         }
775         RETURN_EXIT;
776 }
777
778 static const struct cl_io_operations lov_io_ops = {
779         .op = {
780                 [CIT_READ] = {
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_WRITE] = {
790                         .cio_fini      = lov_io_fini,
791                         .cio_iter_init = lov_io_rw_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_SETATTR] = {
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_start,
805                         .cio_end       = lov_io_end
806                 },
807                 [CIT_FAULT] = {
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_fault_start,
814                         .cio_end       = lov_io_end
815                 },
816                 [CIT_FSYNC] = {
817                         .cio_fini      = lov_io_fini,
818                         .cio_iter_init = lov_io_iter_init,
819                         .cio_iter_fini = lov_io_iter_fini,
820                         .cio_lock      = lov_io_lock,
821                         .cio_unlock    = lov_io_unlock,
822                         .cio_start     = lov_io_start,
823                         .cio_end       = lov_io_fsync_end
824                 },
825                 [CIT_MISC] = {
826                         .cio_fini      = lov_io_fini
827                 }
828         },
829         .cio_submit                    = lov_io_submit,
830         .cio_commit_async              = lov_io_commit_async,
831 };
832
833 /*****************************************************************************
834  *
835  * Empty lov io operations.
836  *
837  */
838
839 static void lov_empty_io_fini(const struct lu_env *env,
840                               const struct cl_io_slice *ios)
841 {
842         struct lov_object *lov = cl2lov(ios->cis_obj);
843         ENTRY;
844
845         if (atomic_dec_and_test(&lov->lo_active_ios))
846                 wake_up_all(&lov->lo_waitq);
847         EXIT;
848 }
849
850 static void lov_empty_impossible(const struct lu_env *env,
851                                  struct cl_io_slice *ios)
852 {
853         LBUG();
854 }
855
856 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
857
858 /**
859  * An io operation vector for files without stripes.
860  */
861 static const struct cl_io_operations lov_empty_io_ops = {
862         .op = {
863                 [CIT_READ] = {
864                         .cio_fini       = lov_empty_io_fini,
865 #if 0
866                         .cio_iter_init  = LOV_EMPTY_IMPOSSIBLE,
867                         .cio_lock       = LOV_EMPTY_IMPOSSIBLE,
868                         .cio_start      = LOV_EMPTY_IMPOSSIBLE,
869                         .cio_end        = LOV_EMPTY_IMPOSSIBLE
870 #endif
871                 },
872                 [CIT_WRITE] = {
873                         .cio_fini      = lov_empty_io_fini,
874                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
875                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
876                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
877                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
878                 },
879                 [CIT_SETATTR] = {
880                         .cio_fini      = lov_empty_io_fini,
881                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
882                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
883                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
884                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
885                 },
886                 [CIT_FAULT] = {
887                         .cio_fini      = lov_empty_io_fini,
888                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
889                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
890                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
891                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
892                 },
893                 [CIT_FSYNC] = {
894                         .cio_fini      = lov_empty_io_fini
895                 },
896                 [CIT_MISC] = {
897                         .cio_fini      = lov_empty_io_fini
898                 }
899         },
900         .cio_submit                    = LOV_EMPTY_IMPOSSIBLE,
901         .cio_commit_async              = LOV_EMPTY_IMPOSSIBLE
902 };
903
904 int lov_io_init_raid0(const struct lu_env *env, struct cl_object *obj,
905                       struct cl_io *io)
906 {
907         struct lov_io       *lio = lov_env_io(env);
908         struct lov_object   *lov = cl2lov(obj);
909
910         ENTRY;
911         INIT_LIST_HEAD(&lio->lis_active);
912         io->ci_result = lov_io_slice_init(lio, lov, io);
913         if (io->ci_result != 0)
914                 RETURN(io->ci_result);
915
916         if (io->ci_result == 0) {
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                         atomic_inc(&lov->lo_active_ios);
921                 }
922         }
923         RETURN(io->ci_result);
924 }
925
926 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
927                       struct cl_io *io)
928 {
929         struct lov_object *lov = cl2lov(obj);
930         struct lov_io *lio = lov_env_io(env);
931         int result;
932         ENTRY;
933
934         lio->lis_object = lov;
935         switch (io->ci_type) {
936         default:
937                 LBUG();
938         case CIT_MISC:
939         case CIT_READ:
940                 result = 0;
941                 break;
942         case CIT_FSYNC:
943         case CIT_SETATTR:
944                 result = +1;
945                 break;
946         case CIT_WRITE:
947                 result = -EBADF;
948                 break;
949         case CIT_FAULT:
950                 result = -EFAULT;
951                 CERROR("Page fault on a file without stripes: "DFID"\n",
952                        PFID(lu_object_fid(&obj->co_lu)));
953                 break;
954         }
955         if (result == 0) {
956                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
957                 atomic_inc(&lov->lo_active_ios);
958         }
959
960         io->ci_result = result < 0 ? result : 0;
961         RETURN(result != 0);
962 }
963
964 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
965                         struct cl_io *io)
966 {
967         struct lov_object *lov = cl2lov(obj);
968         struct lov_io *lio = lov_env_io(env);
969         int result;
970         ENTRY;
971
972         LASSERT(lov->lo_lsm != NULL);
973         lio->lis_object = lov;
974
975         switch (io->ci_type) {
976         default:
977                 LASSERTF(0, "invalid type %d\n", io->ci_type);
978         case CIT_MISC:
979         case CIT_FSYNC:
980                 result = 1;
981                 break;
982         case CIT_SETATTR:
983                 /* the truncate to 0 is managed by MDT:
984                  * - in open, for open O_TRUNC
985                  * - in setattr, for truncate
986                  */
987                 /* the truncate is for size > 0 so triggers a restore */
988                 if (cl_io_is_trunc(io))
989                         io->ci_restore_needed = 1;
990                 result = -ENODATA;
991                 break;
992         case CIT_READ:
993         case CIT_WRITE:
994         case CIT_FAULT:
995                 io->ci_restore_needed = 1;
996                 result = -ENODATA;
997                 break;
998         }
999         if (result == 0) {
1000                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
1001                 atomic_inc(&lov->lo_active_ios);
1002         }
1003
1004         io->ci_result = result < 0 ? result : 0;
1005         RETURN(result != 0);
1006 }
1007 /** @} lov */