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