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