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