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