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