Whamcloud - gitweb
LU-3321 clio: add pages into writeback cache in batch
[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, 2013, 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_valid = parent->u.ci_setattr.sa_valid;
95                 io->u.ci_setattr.sa_capa = parent->u.ci_setattr.sa_capa;
96                 if (cl_io_is_trunc(io)) {
97                         loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
98
99                         new_size = lov_size_to_stripe(lsm, new_size, stripe);
100                         io->u.ci_setattr.sa_attr.lvb_size = new_size;
101                 }
102                 break;
103         }
104         case CIT_FAULT: {
105                 struct cl_object *obj = parent->ci_obj;
106                 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
107
108                 io->u.ci_fault = parent->u.ci_fault;
109                 off = lov_size_to_stripe(lsm, off, stripe);
110                 io->u.ci_fault.ft_index = cl_index(obj, off);
111                 break;
112         }
113         case CIT_FSYNC: {
114                 io->u.ci_fsync.fi_start = start;
115                 io->u.ci_fsync.fi_end = end;
116                 io->u.ci_fsync.fi_capa = parent->u.ci_fsync.fi_capa;
117                 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
118                 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
119                 break;
120         }
121         case CIT_READ:
122         case CIT_WRITE: {
123                 io->u.ci_wr.wr_sync = cl_io_is_sync_write(parent);
124                 if (cl_io_is_append(parent)) {
125                         io->u.ci_wr.wr_append = 1;
126                 } else {
127                         io->u.ci_rw.crw_pos = start;
128                         io->u.ci_rw.crw_count = end - start;
129                 }
130                 break;
131         }
132         default:
133                 break;
134         }
135 }
136
137 static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
138                            struct lov_io_sub *sub)
139 {
140         struct lov_object *lov = lio->lis_object;
141         struct lov_device *ld  = lu2lov_dev(lov2cl(lov)->co_lu.lo_dev);
142         struct cl_io      *sub_io;
143         struct cl_object  *sub_obj;
144         struct cl_io      *io  = lio->lis_cl.cis_io;
145
146         int stripe = sub->sub_stripe;
147         int result;
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         result = 0;
155         sub->sub_io_initialized = 0;
156         sub->sub_borrowed = 0;
157
158         if (lio->lis_mem_frozen) {
159                 LASSERT(mutex_is_locked(&ld->ld_mutex));
160                 sub->sub_io  = &ld->ld_emrg[stripe]->emrg_subio;
161                 sub->sub_env = ld->ld_emrg[stripe]->emrg_env;
162                 sub->sub_borrowed = 1;
163         } else {
164                 void *cookie;
165
166                 /* obtain new environment */
167                 cookie = cl_env_reenter();
168                 sub->sub_env = cl_env_get(&sub->sub_refcheck);
169                 cl_env_reexit(cookie);
170                 if (IS_ERR(sub->sub_env))
171                         result = PTR_ERR(sub->sub_env);
172
173                 if (result == 0) {
174                         /*
175                          * First sub-io. Use ->lis_single_subio to
176                          * avoid dynamic allocation.
177                          */
178                         if (lio->lis_active_subios == 0) {
179                                 sub->sub_io = &lio->lis_single_subio;
180                                 lio->lis_single_subio_index = stripe;
181                         } else {
182                                 OBD_ALLOC_PTR(sub->sub_io);
183                                 if (sub->sub_io == NULL)
184                                         result = -ENOMEM;
185                         }
186                 }
187         }
188
189         if (result == 0) {
190                 sub_obj = lovsub2cl(lov_r0(lov)->lo_sub[stripe]);
191                 sub_io  = sub->sub_io;
192
193                 sub_io->ci_obj    = sub_obj;
194                 sub_io->ci_result = 0;
195
196                 sub_io->ci_parent  = io;
197                 sub_io->ci_lockreq = io->ci_lockreq;
198                 sub_io->ci_type    = io->ci_type;
199                 sub_io->ci_no_srvlock = io->ci_no_srvlock;
200                 sub_io->ci_noatime = io->ci_noatime;
201
202                 lov_sub_enter(sub);
203                 result = cl_io_sub_init(sub->sub_env, sub_io,
204                                         io->ci_type, sub_obj);
205                 lov_sub_exit(sub);
206                 if (result >= 0) {
207                         lio->lis_active_subios++;
208                         sub->sub_io_initialized = 1;
209                         result = 0;
210                 }
211         }
212         if (result != 0)
213                 lov_io_sub_fini(env, lio, sub);
214         RETURN(result);
215 }
216
217 struct lov_io_sub *lov_sub_get(const struct lu_env *env,
218                                struct lov_io *lio, int stripe)
219 {
220         int rc;
221         struct lov_io_sub *sub = &lio->lis_subs[stripe];
222
223         LASSERT(stripe < lio->lis_stripe_count);
224         ENTRY;
225
226         if (!sub->sub_io_initialized) {
227                 sub->sub_stripe = stripe;
228                 rc = lov_io_sub_init(env, lio, sub);
229         } else
230                 rc = 0;
231         if (rc == 0)
232                 lov_sub_enter(sub);
233         else
234                 sub = ERR_PTR(rc);
235         RETURN(sub);
236 }
237
238 void lov_sub_put(struct lov_io_sub *sub)
239 {
240         lov_sub_exit(sub);
241 }
242
243 /*****************************************************************************
244  *
245  * Lov io operations.
246  *
247  */
248
249 static int lov_page_stripe(const struct cl_page *page)
250 {
251         struct lovsub_object *subobj;
252
253         ENTRY;
254         subobj = lu2lovsub(
255                 lu_object_locate(page->cp_child->cp_obj->co_lu.lo_header,
256                                  &lovsub_device_type));
257         LASSERT(subobj != NULL);
258         RETURN(subobj->lso_index);
259 }
260
261 struct lov_io_sub *lov_page_subio(const struct lu_env *env, struct lov_io *lio,
262                                   const struct cl_page_slice *slice)
263 {
264         struct lov_stripe_md *lsm  = lio->lis_object->lo_lsm;
265         struct cl_page       *page = slice->cpl_page;
266         int stripe;
267
268         LASSERT(lio->lis_cl.cis_io != NULL);
269         LASSERT(cl2lov(slice->cpl_obj) == lio->lis_object);
270         LASSERT(lsm != NULL);
271         LASSERT(lio->lis_nr_subios > 0);
272         ENTRY;
273
274         stripe = lov_page_stripe(page);
275         RETURN(lov_sub_get(env, lio, stripe));
276 }
277
278
279 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
280                              struct cl_io *io)
281 {
282         struct lov_stripe_md *lsm;
283         int result;
284         ENTRY;
285
286         LASSERT(lio->lis_object != NULL);
287         lsm = lio->lis_object->lo_lsm;
288
289         /*
290          * Need to be optimized, we can't afford to allocate a piece of memory
291          * when writing a page. -jay
292          */
293         OBD_ALLOC_LARGE(lio->lis_subs,
294                         lsm->lsm_stripe_count * sizeof lio->lis_subs[0]);
295         if (lio->lis_subs != NULL) {
296                 lio->lis_nr_subios = lio->lis_stripe_count;
297                 lio->lis_single_subio_index = -1;
298                 lio->lis_active_subios = 0;
299                 result = 0;
300         } else
301                 result = -ENOMEM;
302         RETURN(result);
303 }
304
305 static void lov_io_slice_init(struct lov_io *lio,
306                               struct lov_object *obj, struct cl_io *io)
307 {
308         ENTRY;
309
310         io->ci_result = 0;
311         lio->lis_object = obj;
312
313         LASSERT(obj->lo_lsm != NULL);
314         lio->lis_stripe_count = obj->lo_lsm->lsm_stripe_count;
315
316         switch (io->ci_type) {
317         case CIT_READ:
318         case CIT_WRITE:
319                 lio->lis_pos = io->u.ci_rw.crw_pos;
320                 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
321                 lio->lis_io_endpos = lio->lis_endpos;
322                 if (cl_io_is_append(io)) {
323                         LASSERT(io->ci_type == CIT_WRITE);
324                         lio->lis_pos = 0;
325                         lio->lis_endpos = OBD_OBJECT_EOF;
326                 }
327                 break;
328
329         case CIT_SETATTR:
330                 if (cl_io_is_trunc(io))
331                         lio->lis_pos = io->u.ci_setattr.sa_attr.lvb_size;
332                 else
333                         lio->lis_pos = 0;
334                 lio->lis_endpos = OBD_OBJECT_EOF;
335                 break;
336
337         case CIT_FAULT: {
338                 pgoff_t index = io->u.ci_fault.ft_index;
339                 lio->lis_pos = cl_offset(io->ci_obj, index);
340                 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
341                 break;
342         }
343
344         case CIT_FSYNC: {
345                 lio->lis_pos = io->u.ci_fsync.fi_start;
346                 lio->lis_endpos = io->u.ci_fsync.fi_end;
347                 break;
348         }
349
350         case CIT_MISC:
351                 lio->lis_pos = 0;
352                 lio->lis_endpos = OBD_OBJECT_EOF;
353                 break;
354
355         default:
356                 LBUG();
357         }
358
359         EXIT;
360 }
361
362 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
363 {
364         struct lov_io *lio = cl2lov_io(env, ios);
365         struct lov_object *lov = cl2lov(ios->cis_obj);
366         int i;
367
368         ENTRY;
369         if (lio->lis_subs != NULL) {
370                 for (i = 0; i < lio->lis_nr_subios; i++)
371                         lov_io_sub_fini(env, lio, &lio->lis_subs[i]);
372                 OBD_FREE_LARGE(lio->lis_subs,
373                          lio->lis_nr_subios * sizeof lio->lis_subs[0]);
374                 lio->lis_nr_subios = 0;
375         }
376
377         LASSERT(cfs_atomic_read(&lov->lo_active_ios) > 0);
378         if (cfs_atomic_dec_and_test(&lov->lo_active_ios))
379                 wake_up_all(&lov->lo_waitq);
380         EXIT;
381 }
382
383 static obd_off lov_offset_mod(obd_off val, int delta)
384 {
385         if (val != OBD_OBJECT_EOF)
386                 val += delta;
387         return val;
388 }
389
390 static int lov_io_iter_init(const struct lu_env *env,
391                             const struct cl_io_slice *ios)
392 {
393         struct lov_io        *lio = cl2lov_io(env, ios);
394         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
395         struct lov_io_sub    *sub;
396         obd_off endpos;
397         obd_off start;
398         obd_off end;
399         int stripe;
400         int rc = 0;
401
402         ENTRY;
403         endpos = lov_offset_mod(lio->lis_endpos, -1);
404         for (stripe = 0; stripe < lio->lis_stripe_count; stripe++) {
405                 if (!lov_stripe_intersects(lsm, stripe, lio->lis_pos,
406                                            endpos, &start, &end))
407                         continue;
408
409                 end = lov_offset_mod(end, +1);
410                 sub = lov_sub_get(env, lio, stripe);
411                 if (!IS_ERR(sub)) {
412                         lov_io_sub_inherit(sub->sub_io, lio, stripe,
413                                            start, end);
414                         rc = cl_io_iter_init(sub->sub_env, sub->sub_io);
415                         lov_sub_put(sub);
416                         CDEBUG(D_VFSTRACE, "shrink: %d ["LPU64", "LPU64")\n",
417                                stripe, start, end);
418                 } else
419                         rc = PTR_ERR(sub);
420
421                 if (!rc)
422                         cfs_list_add_tail(&sub->sub_linkage, &lio->lis_active);
423                 else
424                         break;
425         }
426         RETURN(rc);
427 }
428
429 static int lov_io_rw_iter_init(const struct lu_env *env,
430                                const struct cl_io_slice *ios)
431 {
432         struct lov_io        *lio = cl2lov_io(env, ios);
433         struct cl_io         *io  = ios->cis_io;
434         struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
435         loff_t start = io->u.ci_rw.crw_pos;
436         loff_t next;
437         unsigned long ssize = lsm->lsm_stripe_size;
438
439         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
440         ENTRY;
441
442         /* fast path for common case. */
443         if (lio->lis_nr_subios != 1 && !cl_io_is_append(io)) {
444
445                 lov_do_div64(start, ssize);
446                 next = (start + 1) * ssize;
447                 if (next <= start * ssize)
448                         next = ~0ull;
449
450                 io->ci_continue = next < lio->lis_io_endpos;
451                 io->u.ci_rw.crw_count = min_t(loff_t, lio->lis_io_endpos,
452                                               next) - io->u.ci_rw.crw_pos;
453                 lio->lis_pos    = io->u.ci_rw.crw_pos;
454                 lio->lis_endpos = io->u.ci_rw.crw_pos + io->u.ci_rw.crw_count;
455                 CDEBUG(D_VFSTRACE, "stripe: "LPU64" chunk: ["LPU64", "LPU64") "
456                        LPU64"\n", (__u64)start, lio->lis_pos, lio->lis_endpos,
457                        (__u64)lio->lis_io_endpos);
458         }
459         /*
460          * XXX The following call should be optimized: we know, that
461          * [lio->lis_pos, lio->lis_endpos) intersects with exactly one stripe.
462          */
463         RETURN(lov_io_iter_init(env, ios));
464 }
465
466 static int lov_io_call(const struct lu_env *env, struct lov_io *lio,
467                        int (*iofunc)(const struct lu_env *, struct cl_io *))
468 {
469         struct cl_io *parent = lio->lis_cl.cis_io;
470         struct lov_io_sub *sub;
471         int rc = 0;
472
473         ENTRY;
474         cfs_list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
475                 lov_sub_enter(sub);
476                 rc = iofunc(sub->sub_env, sub->sub_io);
477                 lov_sub_exit(sub);
478                 if (rc)
479                         break;
480
481                 if (parent->ci_result == 0)
482                         parent->ci_result = sub->sub_io->ci_result;
483         }
484         RETURN(rc);
485 }
486
487 static int lov_io_lock(const struct lu_env *env, const struct cl_io_slice *ios)
488 {
489         ENTRY;
490         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_lock));
491 }
492
493 static int lov_io_start(const struct lu_env *env, const struct cl_io_slice *ios)
494 {
495         ENTRY;
496         RETURN(lov_io_call(env, cl2lov_io(env, ios), cl_io_start));
497 }
498
499 static int lov_io_end_wrapper(const struct lu_env *env, struct cl_io *io)
500 {
501         ENTRY;
502         /*
503          * It's possible that lov_io_start() wasn't called against this
504          * sub-io, either because previous sub-io failed, or upper layer
505          * completed IO.
506          */
507         if (io->ci_state == CIS_IO_GOING)
508                 cl_io_end(env, io);
509         else
510                 io->ci_state = CIS_IO_FINISHED;
511         RETURN(0);
512 }
513
514 static int lov_io_iter_fini_wrapper(const struct lu_env *env, struct cl_io *io)
515 {
516         cl_io_iter_fini(env, io);
517         RETURN(0);
518 }
519
520 static int lov_io_unlock_wrapper(const struct lu_env *env, struct cl_io *io)
521 {
522         cl_io_unlock(env, io);
523         RETURN(0);
524 }
525
526 static void lov_io_end(const struct lu_env *env, const struct cl_io_slice *ios)
527 {
528         int rc;
529
530         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_end_wrapper);
531         LASSERT(rc == 0);
532 }
533
534 static void lov_io_iter_fini(const struct lu_env *env,
535                              const struct cl_io_slice *ios)
536 {
537         struct lov_io *lio = cl2lov_io(env, ios);
538         int rc;
539
540         ENTRY;
541         rc = lov_io_call(env, lio, lov_io_iter_fini_wrapper);
542         LASSERT(rc == 0);
543         while (!cfs_list_empty(&lio->lis_active))
544                 cfs_list_del_init(lio->lis_active.next);
545         EXIT;
546 }
547
548 static void lov_io_unlock(const struct lu_env *env,
549                           const struct cl_io_slice *ios)
550 {
551         int rc;
552
553         ENTRY;
554         rc = lov_io_call(env, cl2lov_io(env, ios), lov_io_unlock_wrapper);
555         LASSERT(rc == 0);
556         EXIT;
557 }
558
559 /**
560  * lov implementation of cl_operations::cio_submit() method. It takes a list
561  * of pages in \a queue, splits it into per-stripe sub-lists, invokes
562  * cl_io_submit() on underlying devices to submit sub-lists, and then splices
563  * everything back.
564  *
565  * Major complication of this function is a need to handle memory cleansing:
566  * cl_io_submit() is called to write out pages as a part of VM memory
567  * reclamation, and hence it may not fail due to memory shortages (system
568  * dead-locks otherwise). To deal with this, some resources (sub-lists,
569  * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
570  * not-memory cleansing context), and in case of memory shortage, these
571  * pre-allocated resources are used by lov_io_submit() under
572  * lov_device::ld_mutex mutex.
573  */
574 static int lov_io_submit(const struct lu_env *env,
575                          const struct cl_io_slice *ios,
576                          enum cl_req_type crt, struct cl_2queue *queue)
577 {
578         struct cl_page_list     *qin = &queue->c2_qin;
579         struct lov_io           *lio = cl2lov_io(env, ios);
580         struct lov_io_sub       *sub;
581         struct cl_page_list     *plist = &lov_env_info(env)->lti_plist;
582         struct cl_page          *page;
583         int stripe;
584         int rc = 0;
585         ENTRY;
586
587         if (lio->lis_active_subios == 1) {
588                 int idx = lio->lis_single_subio_index;
589
590                 LASSERT(idx < lio->lis_nr_subios);
591                 sub = lov_sub_get(env, lio, idx);
592                 LASSERT(!IS_ERR(sub));
593                 LASSERT(sub->sub_io == &lio->lis_single_subio);
594                 rc = cl_io_submit_rw(sub->sub_env, sub->sub_io,
595                                      crt, queue);
596                 lov_sub_put(sub);
597                 RETURN(rc);
598         }
599
600         LASSERT(lio->lis_subs != NULL);
601
602         cl_page_list_init(plist);
603         while (qin->pl_nr > 0) {
604                 struct cl_2queue  *cl2q = &lov_env_info(env)->lti_cl2q;
605
606                 cl_2queue_init(cl2q);
607
608                 page = cl_page_list_first(qin);
609                 cl_page_list_move(&cl2q->c2_qin, qin, page);
610
611                 stripe = lov_page_stripe(page);
612                 while (qin->pl_nr > 0) {
613                         page = cl_page_list_first(qin);
614                         if (stripe != lov_page_stripe(page))
615                                 break;
616
617                         cl_page_list_move(&cl2q->c2_qin, qin, page);
618                 }
619
620                 sub = lov_sub_get(env, lio, stripe);
621                 if (!IS_ERR(sub)) {
622                         rc = cl_io_submit_rw(sub->sub_env, sub->sub_io,
623                                              crt, cl2q);
624                         lov_sub_put(sub);
625                 } else {
626                         rc = PTR_ERR(sub);
627                 }
628
629                 cl_page_list_splice(&cl2q->c2_qin, plist);
630                 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
631                 cl_2queue_fini(env, cl2q);
632
633                 if (rc != 0)
634                         break;
635         }
636
637         cl_page_list_splice(plist, qin);
638         cl_page_list_fini(env, plist);
639
640         RETURN(rc);
641 }
642
643 static int lov_io_commit_async(const struct lu_env *env,
644                                const struct cl_io_slice *ios,
645                                struct cl_page_list *queue, int from, int to,
646                                cl_commit_cbt cb)
647 {
648         struct cl_page_list *plist = &lov_env_info(env)->lti_plist;
649         struct lov_io     *lio = cl2lov_io(env, ios);
650         struct lov_io_sub *sub;
651         struct cl_page *page;
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_commit_async(sub->sub_env, sub->sub_io, queue,
663                                         from, to, cb);
664                 lov_sub_put(sub);
665                 RETURN(rc);
666         }
667
668         LASSERT(lio->lis_subs != NULL);
669
670         cl_page_list_init(plist);
671         while (queue->pl_nr > 0) {
672                 int stripe_to = to;
673                 int stripe;
674
675                 LASSERT(plist->pl_nr == 0);
676                 page = cl_page_list_first(queue);
677                 cl_page_list_move(plist, queue, page);
678
679                 stripe = lov_page_stripe(page);
680                 while (queue->pl_nr > 0) {
681                         page = cl_page_list_first(queue);
682                         if (stripe != lov_page_stripe(page))
683                                 break;
684
685                         cl_page_list_move(plist, queue, page);
686                 }
687
688                 if (queue->pl_nr > 0) /* still has more pages */
689                         stripe_to = PAGE_SIZE;
690
691                 sub = lov_sub_get(env, lio, stripe);
692                 if (!IS_ERR(sub)) {
693                         rc = cl_io_commit_async(sub->sub_env, sub->sub_io,
694                                                 plist, from, stripe_to, cb);
695                         lov_sub_put(sub);
696                 } else {
697                         rc = PTR_ERR(sub);
698                         break;
699                 }
700
701                 if (plist->pl_nr > 0) /* short write */
702                         break;
703
704                 from = 0;
705         }
706
707         /* for error case, add the page back into the qin list */
708         LASSERT(ergo(rc == 0, plist->pl_nr == 0));
709         while (plist->pl_nr > 0) {
710                 /* error occurred, add the uncommitted pages back into queue */
711                 page = cl_page_list_last(plist);
712                 cl_page_list_move_head(queue, plist, page);
713         }
714
715         RETURN(rc);
716 }
717
718 static int lov_io_fault_start(const struct lu_env *env,
719                               const struct cl_io_slice *ios)
720 {
721         struct cl_fault_io *fio;
722         struct lov_io      *lio;
723         struct lov_io_sub  *sub;
724
725         ENTRY;
726         fio = &ios->cis_io->u.ci_fault;
727         lio = cl2lov_io(env, ios);
728         sub = lov_sub_get(env, lio, lov_page_stripe(fio->ft_page));
729         sub->sub_io->u.ci_fault.ft_nob = fio->ft_nob;
730         lov_sub_put(sub);
731         RETURN(lov_io_start(env, ios));
732 }
733
734 static void lov_io_fsync_end(const struct lu_env *env,
735                              const struct cl_io_slice *ios)
736 {
737         struct lov_io *lio = cl2lov_io(env, ios);
738         struct lov_io_sub *sub;
739         unsigned int *written = &ios->cis_io->u.ci_fsync.fi_nr_written;
740         ENTRY;
741
742         *written = 0;
743         cfs_list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
744                 struct cl_io *subio = sub->sub_io;
745
746                 lov_sub_enter(sub);
747                 lov_io_end_wrapper(sub->sub_env, subio);
748                 lov_sub_exit(sub);
749
750                 if (subio->ci_result == 0)
751                         *written += subio->u.ci_fsync.fi_nr_written;
752         }
753         RETURN_EXIT;
754 }
755
756 static const struct cl_io_operations lov_io_ops = {
757         .op = {
758                 [CIT_READ] = {
759                         .cio_fini      = lov_io_fini,
760                         .cio_iter_init = lov_io_rw_iter_init,
761                         .cio_iter_fini = lov_io_iter_fini,
762                         .cio_lock      = lov_io_lock,
763                         .cio_unlock    = lov_io_unlock,
764                         .cio_start     = lov_io_start,
765                         .cio_end       = lov_io_end
766                 },
767                 [CIT_WRITE] = {
768                         .cio_fini      = lov_io_fini,
769                         .cio_iter_init = lov_io_rw_iter_init,
770                         .cio_iter_fini = lov_io_iter_fini,
771                         .cio_lock      = lov_io_lock,
772                         .cio_unlock    = lov_io_unlock,
773                         .cio_start     = lov_io_start,
774                         .cio_end       = lov_io_end
775                 },
776                 [CIT_SETATTR] = {
777                         .cio_fini      = lov_io_fini,
778                         .cio_iter_init = lov_io_iter_init,
779                         .cio_iter_fini = lov_io_iter_fini,
780                         .cio_lock      = lov_io_lock,
781                         .cio_unlock    = lov_io_unlock,
782                         .cio_start     = lov_io_start,
783                         .cio_end       = lov_io_end
784                 },
785                 [CIT_FAULT] = {
786                         .cio_fini      = lov_io_fini,
787                         .cio_iter_init = lov_io_iter_init,
788                         .cio_iter_fini = lov_io_iter_fini,
789                         .cio_lock      = lov_io_lock,
790                         .cio_unlock    = lov_io_unlock,
791                         .cio_start     = lov_io_fault_start,
792                         .cio_end       = lov_io_end
793                 },
794                 [CIT_FSYNC] = {
795                         .cio_fini      = lov_io_fini,
796                         .cio_iter_init = lov_io_iter_init,
797                         .cio_iter_fini = lov_io_iter_fini,
798                         .cio_lock      = lov_io_lock,
799                         .cio_unlock    = lov_io_unlock,
800                         .cio_start     = lov_io_start,
801                         .cio_end       = lov_io_fsync_end
802                 },
803                 [CIT_MISC] = {
804                         .cio_fini      = lov_io_fini
805                 }
806         },
807         .cio_submit                    = lov_io_submit,
808         .cio_commit_async              = lov_io_commit_async,
809 };
810
811 /*****************************************************************************
812  *
813  * Empty lov io operations.
814  *
815  */
816
817 static void lov_empty_io_fini(const struct lu_env *env,
818                               const struct cl_io_slice *ios)
819 {
820         struct lov_object *lov = cl2lov(ios->cis_obj);
821         ENTRY;
822
823         if (cfs_atomic_dec_and_test(&lov->lo_active_ios))
824                 wake_up_all(&lov->lo_waitq);
825         EXIT;
826 }
827
828 static void lov_empty_impossible(const struct lu_env *env,
829                                  struct cl_io_slice *ios)
830 {
831         LBUG();
832 }
833
834 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
835
836 /**
837  * An io operation vector for files without stripes.
838  */
839 static const struct cl_io_operations lov_empty_io_ops = {
840         .op = {
841                 [CIT_READ] = {
842                         .cio_fini       = lov_empty_io_fini,
843 #if 0
844                         .cio_iter_init  = LOV_EMPTY_IMPOSSIBLE,
845                         .cio_lock       = LOV_EMPTY_IMPOSSIBLE,
846                         .cio_start      = LOV_EMPTY_IMPOSSIBLE,
847                         .cio_end        = LOV_EMPTY_IMPOSSIBLE
848 #endif
849                 },
850                 [CIT_WRITE] = {
851                         .cio_fini      = lov_empty_io_fini,
852                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
853                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
854                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
855                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
856                 },
857                 [CIT_SETATTR] = {
858                         .cio_fini      = lov_empty_io_fini,
859                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
860                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
861                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
862                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
863                 },
864                 [CIT_FAULT] = {
865                         .cio_fini      = lov_empty_io_fini,
866                         .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
867                         .cio_lock      = LOV_EMPTY_IMPOSSIBLE,
868                         .cio_start     = LOV_EMPTY_IMPOSSIBLE,
869                         .cio_end       = LOV_EMPTY_IMPOSSIBLE
870                 },
871                 [CIT_FSYNC] = {
872                         .cio_fini      = lov_empty_io_fini
873                 },
874                 [CIT_MISC] = {
875                         .cio_fini      = lov_empty_io_fini
876                 }
877         },
878         .cio_submit                    = LOV_EMPTY_IMPOSSIBLE,
879         .cio_commit_async              = LOV_EMPTY_IMPOSSIBLE
880 };
881
882 int lov_io_init_raid0(const struct lu_env *env, struct cl_object *obj,
883                       struct cl_io *io)
884 {
885         struct lov_io       *lio = lov_env_io(env);
886         struct lov_object   *lov = cl2lov(obj);
887
888         ENTRY;
889         CFS_INIT_LIST_HEAD(&lio->lis_active);
890         lov_io_slice_init(lio, lov, io);
891         if (io->ci_result == 0) {
892                 io->ci_result = lov_io_subio_init(env, lio, io);
893                 if (io->ci_result == 0) {
894                         cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
895                         cfs_atomic_inc(&lov->lo_active_ios);
896                 }
897         }
898         RETURN(io->ci_result);
899 }
900
901 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
902                       struct cl_io *io)
903 {
904         struct lov_object *lov = cl2lov(obj);
905         struct lov_io *lio = lov_env_io(env);
906         int result;
907         ENTRY;
908
909         lio->lis_object = lov;
910         switch (io->ci_type) {
911         default:
912                 LBUG();
913         case CIT_MISC:
914         case CIT_READ:
915                 result = 0;
916                 break;
917         case CIT_FSYNC:
918         case CIT_SETATTR:
919                 result = +1;
920                 break;
921         case CIT_WRITE:
922                 result = -EBADF;
923                 break;
924         case CIT_FAULT:
925                 result = -EFAULT;
926                 CERROR("Page fault on a file without stripes: "DFID"\n",
927                        PFID(lu_object_fid(&obj->co_lu)));
928                 break;
929         }
930         if (result == 0) {
931                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
932                 cfs_atomic_inc(&lov->lo_active_ios);
933         }
934
935         io->ci_result = result < 0 ? result : 0;
936         RETURN(result != 0);
937 }
938
939 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
940                         struct cl_io *io)
941 {
942         struct lov_object *lov = cl2lov(obj);
943         struct lov_io *lio = lov_env_io(env);
944         int result;
945         ENTRY;
946
947         LASSERT(lov->lo_lsm != NULL);
948         lio->lis_object = lov;
949
950         switch (io->ci_type) {
951         default:
952                 LASSERTF(0, "invalid type %d\n", io->ci_type);
953         case CIT_MISC:
954         case CIT_FSYNC:
955                 result = 1;
956                 break;
957         case CIT_SETATTR:
958                 /* the truncate to 0 is managed by MDT:
959                  * - in open, for open O_TRUNC
960                  * - in setattr, for truncate
961                  */
962                 /* the truncate is for size > 0 so triggers a restore */
963                 if (cl_io_is_trunc(io))
964                         io->ci_restore_needed = 1;
965                 result = -ENODATA;
966                 break;
967         case CIT_READ:
968         case CIT_WRITE:
969         case CIT_FAULT:
970                 io->ci_restore_needed = 1;
971                 result = -ENODATA;
972                 break;
973         }
974         if (result == 0) {
975                 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
976                 cfs_atomic_inc(&lov->lo_active_ios);
977         }
978
979         io->ci_result = result < 0 ? result : 0;
980         RETURN(result != 0);
981 }
982 /** @} lov */