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