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