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