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