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