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, 2012, 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
lov_sub_enter(struct lov_io_sub * sub)50 static inline void lov_sub_enter(struct lov_io_sub *sub)
51 {
52 sub->sub_reenter++;
53 }
lov_sub_exit(struct lov_io_sub * sub)54 static inline void lov_sub_exit(struct lov_io_sub *sub)
55 {
56 sub->sub_reenter--;
57 }
58
lov_io_sub_fini(const struct lu_env * env,struct lov_io * lio,struct lov_io_sub * sub)59 static void lov_io_sub_fini(const struct lu_env *env, struct lov_io *lio,
60 struct lov_io_sub *sub)
61 {
62 if (sub->sub_io != NULL) {
63 if (sub->sub_io_initialized) {
64 lov_sub_enter(sub);
65 cl_io_fini(sub->sub_env, sub->sub_io);
66 lov_sub_exit(sub);
67 sub->sub_io_initialized = 0;
68 lio->lis_active_subios--;
69 }
70 if (sub->sub_stripe == lio->lis_single_subio_index)
71 lio->lis_single_subio_index = -1;
72 else if (!sub->sub_borrowed)
73 OBD_FREE_PTR(sub->sub_io);
74 sub->sub_io = NULL;
75 }
76 if (sub->sub_env != NULL && !IS_ERR(sub->sub_env)) {
77 if (!sub->sub_borrowed)
78 cl_env_put(sub->sub_env, &sub->sub_refcheck);
79 sub->sub_env = NULL;
80 }
81 }
82
lov_io_sub_inherit(struct cl_io * io,struct lov_io * lio,int stripe,loff_t start,loff_t end)83 static void lov_io_sub_inherit(struct cl_io *io, struct lov_io *lio,
84 int stripe, loff_t start, loff_t end)
85 {
86 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
87 struct cl_io *parent = lio->lis_cl.cis_io;
88
89 switch (io->ci_type) {
90 case CIT_SETATTR: {
91 io->u.ci_setattr.sa_attr = parent->u.ci_setattr.sa_attr;
92 io->u.ci_setattr.sa_valid = parent->u.ci_setattr.sa_valid;
93 io->u.ci_setattr.sa_capa = parent->u.ci_setattr.sa_capa;
94 if (cl_io_is_trunc(io)) {
95 loff_t new_size = parent->u.ci_setattr.sa_attr.lvb_size;
96
97 new_size = lov_size_to_stripe(lsm, new_size, stripe);
98 io->u.ci_setattr.sa_attr.lvb_size = new_size;
99 }
100 break;
101 }
102 case CIT_FAULT: {
103 struct cl_object *obj = parent->ci_obj;
104 loff_t off = cl_offset(obj, parent->u.ci_fault.ft_index);
105
106 io->u.ci_fault = parent->u.ci_fault;
107 off = lov_size_to_stripe(lsm, off, stripe);
108 io->u.ci_fault.ft_index = cl_index(obj, off);
109 break;
110 }
111 case CIT_FSYNC: {
112 io->u.ci_fsync.fi_start = start;
113 io->u.ci_fsync.fi_end = end;
114 io->u.ci_fsync.fi_capa = parent->u.ci_fsync.fi_capa;
115 io->u.ci_fsync.fi_fid = parent->u.ci_fsync.fi_fid;
116 io->u.ci_fsync.fi_mode = parent->u.ci_fsync.fi_mode;
117 break;
118 }
119 case CIT_READ:
120 case CIT_WRITE: {
121 io->u.ci_wr.wr_sync = cl_io_is_sync_write(parent);
122 if (cl_io_is_append(parent)) {
123 io->u.ci_wr.wr_append = 1;
124 } else {
125 io->u.ci_rw.crw_pos = start;
126 io->u.ci_rw.crw_count = end - start;
127 }
128 break;
129 }
130 default:
131 break;
132 }
133 }
134
lov_io_sub_init(const struct lu_env * env,struct lov_io * lio,struct lov_io_sub * sub)135 static int lov_io_sub_init(const struct lu_env *env, struct lov_io *lio,
136 struct lov_io_sub *sub)
137 {
138 struct lov_object *lov = lio->lis_object;
139 struct lov_device *ld = lu2lov_dev(lov2cl(lov)->co_lu.lo_dev);
140 struct cl_io *sub_io;
141 struct cl_object *sub_obj;
142 struct cl_io *io = lio->lis_cl.cis_io;
143
144 int stripe = sub->sub_stripe;
145 int result;
146
147 LASSERT(sub->sub_io == NULL);
148 LASSERT(sub->sub_env == NULL);
149 LASSERT(sub->sub_stripe < lio->lis_stripe_count);
150
151 if (unlikely(lov_r0(lov)->lo_sub[stripe] == NULL))
152 return -EIO;
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
lov_sub_get(const struct lu_env * env,struct lov_io * lio,int stripe)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
225 if (!sub->sub_io_initialized) {
226 sub->sub_stripe = stripe;
227 rc = lov_io_sub_init(env, lio, sub);
228 } else
229 rc = 0;
230 if (rc == 0)
231 lov_sub_enter(sub);
232 else
233 sub = ERR_PTR(rc);
234 return sub;
235 }
236
lov_sub_put(struct lov_io_sub * sub)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
lov_page_stripe(const struct cl_page * page)248 static int lov_page_stripe(const struct cl_page *page)
249 {
250 struct lovsub_object *subobj;
251
252 subobj = lu2lovsub(
253 lu_object_locate(page->cp_child->cp_obj->co_lu.lo_header,
254 &lovsub_device_type));
255 LASSERT(subobj != NULL);
256 return subobj->lso_index;
257 }
258
lov_page_subio(const struct lu_env * env,struct lov_io * lio,const struct cl_page_slice * slice)259 struct lov_io_sub *lov_page_subio(const struct lu_env *env, struct lov_io *lio,
260 const struct cl_page_slice *slice)
261 {
262 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
263 struct cl_page *page = slice->cpl_page;
264 int stripe;
265
266 LASSERT(lio->lis_cl.cis_io != NULL);
267 LASSERT(cl2lov(slice->cpl_obj) == lio->lis_object);
268 LASSERT(lsm != NULL);
269 LASSERT(lio->lis_nr_subios > 0);
270
271 stripe = lov_page_stripe(page);
272 return lov_sub_get(env, lio, stripe);
273 }
274
275
lov_io_subio_init(const struct lu_env * env,struct lov_io * lio,struct cl_io * io)276 static int lov_io_subio_init(const struct lu_env *env, struct lov_io *lio,
277 struct cl_io *io)
278 {
279 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
280 int result;
281
282 LASSERT(lio->lis_object != NULL);
283
284 /*
285 * Need to be optimized, we can't afford to allocate a piece of memory
286 * when writing a page. -jay
287 */
288 OBD_ALLOC_LARGE(lio->lis_subs,
289 lsm->lsm_stripe_count * sizeof(lio->lis_subs[0]));
290 if (lio->lis_subs != NULL) {
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 return result;
298 }
299
lov_io_slice_init(struct lov_io * lio,struct lov_object * obj,struct cl_io * io)300 static void lov_io_slice_init(struct lov_io *lio,
301 struct lov_object *obj, struct cl_io *io)
302 {
303 io->ci_result = 0;
304 lio->lis_object = obj;
305
306 LASSERT(obj->lo_lsm != NULL);
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 lio->lis_pos = cl_offset(io->ci_obj, index);
333 lio->lis_endpos = cl_offset(io->ci_obj, index + 1);
334 break;
335 }
336
337 case CIT_FSYNC: {
338 lio->lis_pos = io->u.ci_fsync.fi_start;
339 lio->lis_endpos = io->u.ci_fsync.fi_end;
340 break;
341 }
342
343 case CIT_MISC:
344 lio->lis_pos = 0;
345 lio->lis_endpos = OBD_OBJECT_EOF;
346 break;
347
348 default:
349 LBUG();
350 }
351 }
352
lov_io_fini(const struct lu_env * env,const struct cl_io_slice * ios)353 static void lov_io_fini(const struct lu_env *env, const struct cl_io_slice *ios)
354 {
355 struct lov_io *lio = cl2lov_io(env, ios);
356 struct lov_object *lov = cl2lov(ios->cis_obj);
357 int i;
358
359 if (lio->lis_subs != NULL) {
360 for (i = 0; i < lio->lis_nr_subios; i++)
361 lov_io_sub_fini(env, lio, &lio->lis_subs[i]);
362 OBD_FREE_LARGE(lio->lis_subs,
363 lio->lis_nr_subios * sizeof(lio->lis_subs[0]));
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
lov_offset_mod(u64 val,int delta)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
lov_io_iter_init(const struct lu_env * env,const struct cl_io_slice * ios)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] == NULL)) {
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 if (!rc)
419 list_add_tail(&sub->sub_linkage, &lio->lis_active);
420 else
421 break;
422 }
423 return rc;
424 }
425
lov_io_rw_iter_init(const struct lu_env * env,const struct cl_io_slice * ios)426 static int lov_io_rw_iter_init(const struct lu_env *env,
427 const struct cl_io_slice *ios)
428 {
429 struct lov_io *lio = cl2lov_io(env, ios);
430 struct cl_io *io = ios->cis_io;
431 struct lov_stripe_md *lsm = lio->lis_object->lo_lsm;
432 __u64 start = io->u.ci_rw.crw_pos;
433 loff_t next;
434 unsigned long ssize = lsm->lsm_stripe_size;
435
436 LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
437
438 /* fast path for common case. */
439 if (lio->lis_nr_subios != 1 && !cl_io_is_append(io)) {
440
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
lov_io_call(const struct lu_env * env,struct lov_io * lio,int (* iofunc)(const struct lu_env *,struct cl_io *))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
lov_io_lock(const struct lu_env * env,const struct cl_io_slice * ios)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
lov_io_start(const struct lu_env * env,const struct cl_io_slice * ios)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
lov_io_end_wrapper(const struct lu_env * env,struct cl_io * io)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
lov_io_iter_fini_wrapper(const struct lu_env * env,struct cl_io * io)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
lov_io_unlock_wrapper(const struct lu_env * env,struct cl_io * io)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
lov_io_end(const struct lu_env * env,const struct cl_io_slice * ios)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
lov_io_iter_fini(const struct lu_env * env,const struct cl_io_slice * ios)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
lov_io_unlock(const struct lu_env * env,const struct cl_io_slice * ios)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
lov_io_submit_qin(struct lov_device * ld,struct cl_page_list * qin,int idx,int alloc)548 static struct cl_page_list *lov_io_submit_qin(struct lov_device *ld,
549 struct cl_page_list *qin,
550 int idx, int alloc)
551 {
552 return alloc ? &qin[idx] : &ld->ld_emrg[idx]->emrg_page_list;
553 }
554
555 /**
556 * lov implementation of cl_operations::cio_submit() method. It takes a list
557 * of pages in \a queue, splits it into per-stripe sub-lists, invokes
558 * cl_io_submit() on underlying devices to submit sub-lists, and then splices
559 * everything back.
560 *
561 * Major complication of this function is a need to handle memory cleansing:
562 * cl_io_submit() is called to write out pages as a part of VM memory
563 * reclamation, and hence it may not fail due to memory shortages (system
564 * dead-locks otherwise). To deal with this, some resources (sub-lists,
565 * sub-environment, etc.) are allocated per-device on "startup" (i.e., in a
566 * not-memory cleansing context), and in case of memory shortage, these
567 * pre-allocated resources are used by lov_io_submit() under
568 * lov_device::ld_mutex mutex.
569 */
lov_io_submit(const struct lu_env * env,const struct cl_io_slice * ios,enum cl_req_type crt,struct cl_2queue * queue)570 static int lov_io_submit(const struct lu_env *env,
571 const struct cl_io_slice *ios,
572 enum cl_req_type crt, struct cl_2queue *queue)
573 {
574 struct lov_io *lio = cl2lov_io(env, ios);
575 struct lov_object *obj = lio->lis_object;
576 struct lov_device *ld = lu2lov_dev(lov2cl(obj)->co_lu.lo_dev);
577 struct cl_page_list *qin = &queue->c2_qin;
578 struct cl_2queue *cl2q = &lov_env_info(env)->lti_cl2q;
579 struct cl_page_list *stripes_qin = NULL;
580 struct cl_page *page;
581 struct cl_page *tmp;
582 int stripe;
583
584 #define QIN(stripe) lov_io_submit_qin(ld, stripes_qin, stripe, alloc)
585
586 int rc = 0;
587 int alloc =
588 !(current->flags & PF_MEMALLOC);
589
590 if (lio->lis_active_subios == 1) {
591 int idx = lio->lis_single_subio_index;
592 struct lov_io_sub *sub;
593
594 LASSERT(idx < lio->lis_nr_subios);
595 sub = lov_sub_get(env, lio, idx);
596 LASSERT(!IS_ERR(sub));
597 LASSERT(sub->sub_io == &lio->lis_single_subio);
598 rc = cl_io_submit_rw(sub->sub_env, sub->sub_io,
599 crt, queue);
600 lov_sub_put(sub);
601 return rc;
602 }
603
604 LASSERT(lio->lis_subs != NULL);
605 if (alloc) {
606 OBD_ALLOC_LARGE(stripes_qin,
607 sizeof(*stripes_qin) * lio->lis_nr_subios);
608 if (stripes_qin == NULL)
609 return -ENOMEM;
610
611 for (stripe = 0; stripe < lio->lis_nr_subios; stripe++)
612 cl_page_list_init(&stripes_qin[stripe]);
613 } else {
614 /*
615 * If we get here, it means pageout & swap doesn't help.
616 * In order to not make things worse, even don't try to
617 * allocate the memory with __GFP_NOWARN. -jay
618 */
619 mutex_lock(&ld->ld_mutex);
620 lio->lis_mem_frozen = 1;
621 }
622
623 cl_2queue_init(cl2q);
624 cl_page_list_for_each_safe(page, tmp, qin) {
625 stripe = lov_page_stripe(page);
626 cl_page_list_move(QIN(stripe), qin, page);
627 }
628
629 for (stripe = 0; stripe < lio->lis_nr_subios; stripe++) {
630 struct lov_io_sub *sub;
631 struct cl_page_list *sub_qin = QIN(stripe);
632
633 if (list_empty(&sub_qin->pl_pages))
634 continue;
635
636 cl_page_list_splice(sub_qin, &cl2q->c2_qin);
637 sub = lov_sub_get(env, lio, stripe);
638 if (!IS_ERR(sub)) {
639 rc = cl_io_submit_rw(sub->sub_env, sub->sub_io,
640 crt, cl2q);
641 lov_sub_put(sub);
642 } else
643 rc = PTR_ERR(sub);
644 cl_page_list_splice(&cl2q->c2_qin, &queue->c2_qin);
645 cl_page_list_splice(&cl2q->c2_qout, &queue->c2_qout);
646 if (rc != 0)
647 break;
648 }
649
650 for (stripe = 0; stripe < lio->lis_nr_subios; stripe++) {
651 struct cl_page_list *sub_qin = QIN(stripe);
652
653 if (list_empty(&sub_qin->pl_pages))
654 continue;
655
656 cl_page_list_splice(sub_qin, qin);
657 }
658
659 if (alloc) {
660 OBD_FREE_LARGE(stripes_qin,
661 sizeof(*stripes_qin) * lio->lis_nr_subios);
662 } else {
663 int i;
664
665 for (i = 0; i < lio->lis_nr_subios; i++) {
666 struct cl_io *cio = lio->lis_subs[i].sub_io;
667
668 if (cio && cio == &ld->ld_emrg[i]->emrg_subio)
669 lov_io_sub_fini(env, lio, &lio->lis_subs[i]);
670 }
671 lio->lis_mem_frozen = 0;
672 mutex_unlock(&ld->ld_mutex);
673 }
674
675 return rc;
676 #undef QIN
677 }
678
lov_io_prepare_write(const struct lu_env * env,const struct cl_io_slice * ios,const struct cl_page_slice * slice,unsigned from,unsigned to)679 static int lov_io_prepare_write(const struct lu_env *env,
680 const struct cl_io_slice *ios,
681 const struct cl_page_slice *slice,
682 unsigned from, unsigned to)
683 {
684 struct lov_io *lio = cl2lov_io(env, ios);
685 struct cl_page *sub_page = lov_sub_page(slice);
686 struct lov_io_sub *sub;
687 int result;
688
689 sub = lov_page_subio(env, lio, slice);
690 if (!IS_ERR(sub)) {
691 result = cl_io_prepare_write(sub->sub_env, sub->sub_io,
692 sub_page, from, to);
693 lov_sub_put(sub);
694 } else
695 result = PTR_ERR(sub);
696 return result;
697 }
698
lov_io_commit_write(const struct lu_env * env,const struct cl_io_slice * ios,const struct cl_page_slice * slice,unsigned from,unsigned to)699 static int lov_io_commit_write(const struct lu_env *env,
700 const struct cl_io_slice *ios,
701 const struct cl_page_slice *slice,
702 unsigned from, unsigned to)
703 {
704 struct lov_io *lio = cl2lov_io(env, ios);
705 struct cl_page *sub_page = lov_sub_page(slice);
706 struct lov_io_sub *sub;
707 int result;
708
709 sub = lov_page_subio(env, lio, slice);
710 if (!IS_ERR(sub)) {
711 result = cl_io_commit_write(sub->sub_env, sub->sub_io,
712 sub_page, from, to);
713 lov_sub_put(sub);
714 } else
715 result = PTR_ERR(sub);
716 return result;
717 }
718
lov_io_fault_start(const struct lu_env * env,const struct cl_io_slice * ios)719 static int lov_io_fault_start(const struct lu_env *env,
720 const struct cl_io_slice *ios)
721 {
722 struct cl_fault_io *fio;
723 struct lov_io *lio;
724 struct lov_io_sub *sub;
725
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
lov_io_fsync_end(const struct lu_env * env,const struct cl_io_slice * ios)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
741 *written = 0;
742 list_for_each_entry(sub, &lio->lis_active, sub_linkage) {
743 struct cl_io *subio = sub->sub_io;
744
745 lov_sub_enter(sub);
746 lov_io_end_wrapper(sub->sub_env, subio);
747 lov_sub_exit(sub);
748
749 if (subio->ci_result == 0)
750 *written += subio->u.ci_fsync.fi_nr_written;
751 }
752 }
753
754 static const struct cl_io_operations lov_io_ops = {
755 .op = {
756 [CIT_READ] = {
757 .cio_fini = lov_io_fini,
758 .cio_iter_init = lov_io_rw_iter_init,
759 .cio_iter_fini = lov_io_iter_fini,
760 .cio_lock = lov_io_lock,
761 .cio_unlock = lov_io_unlock,
762 .cio_start = lov_io_start,
763 .cio_end = lov_io_end
764 },
765 [CIT_WRITE] = {
766 .cio_fini = lov_io_fini,
767 .cio_iter_init = lov_io_rw_iter_init,
768 .cio_iter_fini = lov_io_iter_fini,
769 .cio_lock = lov_io_lock,
770 .cio_unlock = lov_io_unlock,
771 .cio_start = lov_io_start,
772 .cio_end = lov_io_end
773 },
774 [CIT_SETATTR] = {
775 .cio_fini = lov_io_fini,
776 .cio_iter_init = lov_io_iter_init,
777 .cio_iter_fini = lov_io_iter_fini,
778 .cio_lock = lov_io_lock,
779 .cio_unlock = lov_io_unlock,
780 .cio_start = lov_io_start,
781 .cio_end = lov_io_end
782 },
783 [CIT_FAULT] = {
784 .cio_fini = lov_io_fini,
785 .cio_iter_init = lov_io_iter_init,
786 .cio_iter_fini = lov_io_iter_fini,
787 .cio_lock = lov_io_lock,
788 .cio_unlock = lov_io_unlock,
789 .cio_start = lov_io_fault_start,
790 .cio_end = lov_io_end
791 },
792 [CIT_FSYNC] = {
793 .cio_fini = lov_io_fini,
794 .cio_iter_init = lov_io_iter_init,
795 .cio_iter_fini = lov_io_iter_fini,
796 .cio_lock = lov_io_lock,
797 .cio_unlock = lov_io_unlock,
798 .cio_start = lov_io_start,
799 .cio_end = lov_io_fsync_end
800 },
801 [CIT_MISC] = {
802 .cio_fini = lov_io_fini
803 }
804 },
805 .req_op = {
806 [CRT_READ] = {
807 .cio_submit = lov_io_submit
808 },
809 [CRT_WRITE] = {
810 .cio_submit = lov_io_submit
811 }
812 },
813 .cio_prepare_write = lov_io_prepare_write,
814 .cio_commit_write = lov_io_commit_write
815 };
816
817 /*****************************************************************************
818 *
819 * Empty lov io operations.
820 *
821 */
822
lov_empty_io_fini(const struct lu_env * env,const struct cl_io_slice * ios)823 static void lov_empty_io_fini(const struct lu_env *env,
824 const struct cl_io_slice *ios)
825 {
826 struct lov_object *lov = cl2lov(ios->cis_obj);
827
828 if (atomic_dec_and_test(&lov->lo_active_ios))
829 wake_up_all(&lov->lo_waitq);
830 }
831
lov_empty_impossible(const struct lu_env * env,struct cl_io_slice * ios)832 static void lov_empty_impossible(const struct lu_env *env,
833 struct cl_io_slice *ios)
834 {
835 LBUG();
836 }
837
838 #define LOV_EMPTY_IMPOSSIBLE ((void *)lov_empty_impossible)
839
840 /**
841 * An io operation vector for files without stripes.
842 */
843 static const struct cl_io_operations lov_empty_io_ops = {
844 .op = {
845 [CIT_READ] = {
846 .cio_fini = lov_empty_io_fini,
847 #if 0
848 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
849 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
850 .cio_start = LOV_EMPTY_IMPOSSIBLE,
851 .cio_end = LOV_EMPTY_IMPOSSIBLE
852 #endif
853 },
854 [CIT_WRITE] = {
855 .cio_fini = lov_empty_io_fini,
856 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
857 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
858 .cio_start = LOV_EMPTY_IMPOSSIBLE,
859 .cio_end = LOV_EMPTY_IMPOSSIBLE
860 },
861 [CIT_SETATTR] = {
862 .cio_fini = lov_empty_io_fini,
863 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
864 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
865 .cio_start = LOV_EMPTY_IMPOSSIBLE,
866 .cio_end = LOV_EMPTY_IMPOSSIBLE
867 },
868 [CIT_FAULT] = {
869 .cio_fini = lov_empty_io_fini,
870 .cio_iter_init = LOV_EMPTY_IMPOSSIBLE,
871 .cio_lock = LOV_EMPTY_IMPOSSIBLE,
872 .cio_start = LOV_EMPTY_IMPOSSIBLE,
873 .cio_end = LOV_EMPTY_IMPOSSIBLE
874 },
875 [CIT_FSYNC] = {
876 .cio_fini = lov_empty_io_fini
877 },
878 [CIT_MISC] = {
879 .cio_fini = lov_empty_io_fini
880 }
881 },
882 .req_op = {
883 [CRT_READ] = {
884 .cio_submit = LOV_EMPTY_IMPOSSIBLE
885 },
886 [CRT_WRITE] = {
887 .cio_submit = LOV_EMPTY_IMPOSSIBLE
888 }
889 },
890 .cio_commit_write = LOV_EMPTY_IMPOSSIBLE
891 };
892
lov_io_init_raid0(const struct lu_env * env,struct cl_object * obj,struct cl_io * io)893 int lov_io_init_raid0(const struct lu_env *env, struct cl_object *obj,
894 struct cl_io *io)
895 {
896 struct lov_io *lio = lov_env_io(env);
897 struct lov_object *lov = cl2lov(obj);
898
899 INIT_LIST_HEAD(&lio->lis_active);
900 lov_io_slice_init(lio, lov, io);
901 if (io->ci_result == 0) {
902 io->ci_result = lov_io_subio_init(env, lio, io);
903 if (io->ci_result == 0) {
904 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_io_ops);
905 atomic_inc(&lov->lo_active_ios);
906 }
907 }
908 return io->ci_result;
909 }
910
lov_io_init_empty(const struct lu_env * env,struct cl_object * obj,struct cl_io * io)911 int lov_io_init_empty(const struct lu_env *env, struct cl_object *obj,
912 struct cl_io *io)
913 {
914 struct lov_object *lov = cl2lov(obj);
915 struct lov_io *lio = lov_env_io(env);
916 int result;
917
918 lio->lis_object = lov;
919 switch (io->ci_type) {
920 default:
921 LBUG();
922 case CIT_MISC:
923 case CIT_READ:
924 result = 0;
925 break;
926 case CIT_FSYNC:
927 case CIT_SETATTR:
928 result = 1;
929 break;
930 case CIT_WRITE:
931 result = -EBADF;
932 break;
933 case CIT_FAULT:
934 result = -EFAULT;
935 CERROR("Page fault on a file without stripes: "DFID"\n",
936 PFID(lu_object_fid(&obj->co_lu)));
937 break;
938 }
939 if (result == 0) {
940 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
941 atomic_inc(&lov->lo_active_ios);
942 }
943
944 io->ci_result = result < 0 ? result : 0;
945 return result != 0;
946 }
947
lov_io_init_released(const struct lu_env * env,struct cl_object * obj,struct cl_io * io)948 int lov_io_init_released(const struct lu_env *env, struct cl_object *obj,
949 struct cl_io *io)
950 {
951 struct lov_object *lov = cl2lov(obj);
952 struct lov_io *lio = lov_env_io(env);
953 int result;
954
955 LASSERT(lov->lo_lsm != NULL);
956 lio->lis_object = lov;
957
958 switch (io->ci_type) {
959 default:
960 LASSERTF(0, "invalid type %d\n", io->ci_type);
961 case CIT_MISC:
962 case CIT_FSYNC:
963 result = 1;
964 break;
965 case CIT_SETATTR:
966 /* the truncate to 0 is managed by MDT:
967 * - in open, for open O_TRUNC
968 * - in setattr, for truncate
969 */
970 /* the truncate is for size > 0 so triggers a restore */
971 if (cl_io_is_trunc(io))
972 io->ci_restore_needed = 1;
973 result = -ENODATA;
974 break;
975 case CIT_READ:
976 case CIT_WRITE:
977 case CIT_FAULT:
978 io->ci_restore_needed = 1;
979 result = -ENODATA;
980 break;
981 }
982 if (result == 0) {
983 cl_io_slice_add(io, &lio->lis_cl, obj, &lov_empty_io_ops);
984 atomic_inc(&lov->lo_active_ios);
985 }
986
987 io->ci_result = result < 0 ? result : 0;
988 return result != 0;
989 }
990 /** @} lov */
991