1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Implementation of cl_io for OSC layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 /** \addtogroup osc osc @{ */
43 #define DEBUG_SUBSYSTEM S_OSC
45 #include "osc_cl_internal.h"
47 /*****************************************************************************
53 static struct osc_req *cl2osc_req(const struct cl_req_slice *slice)
55 LINVRNT(slice->crs_dev->cd_lu_dev.ld_type == &osc_device_type);
56 return container_of0(slice, struct osc_req, or_cl);
59 static struct osc_io *cl2osc_io(const struct lu_env *env,
60 const struct cl_io_slice *slice)
62 struct osc_io *oio = container_of0(slice, struct osc_io, oi_cl);
63 LINVRNT(oio == osc_env_io(env));
67 static struct osc_page *osc_cl_page_osc(struct cl_page *page)
69 const struct cl_page_slice *slice;
71 slice = cl_page_at(page, &osc_device_type);
72 LASSERT(slice != NULL);
74 return cl2osc_page(slice);
78 /*****************************************************************************
84 static void osc_io_fini(const struct lu_env *env, const struct cl_io_slice *io)
88 struct cl_page *osc_oap2cl_page(struct osc_async_page *oap)
90 return container_of(oap, struct osc_page, ops_oap)->ops_cl.cpl_page;
93 static void osc_io_unplug(const struct lu_env *env, struct osc_object *osc,
94 struct client_obd *cli)
96 loi_list_maint(cli, osc->oo_oinfo);
97 osc_check_rpcs(env, cli);
98 client_obd_list_unlock(&cli->cl_loi_list_lock);
102 * How many pages osc_io_submit() queues before checking whether an RPC is
105 #define OSC_QUEUE_GRAIN (32)
108 * An implementation of cl_io_operations::cio_io_submit() method for osc
109 * layer. Iterates over pages in the in-queue, prepares each for io by calling
110 * cl_page_prep() and then either submits them through osc_io_submit_page()
111 * or, if page is already submitted, changes osc flags through
112 * osc_set_async_flags_base().
114 static int osc_io_submit(const struct lu_env *env,
115 const struct cl_io_slice *ios,
116 enum cl_req_type crt, struct cl_2queue *queue,
117 enum cl_req_priority priority)
119 struct cl_page *page;
121 struct osc_object *osc0 = NULL;
122 struct client_obd *cli = NULL;
123 struct osc_object *osc = NULL; /* to keep gcc happy */
124 struct osc_page *opg;
127 struct cl_page_list *qin = &queue->c2_qin;
128 struct cl_page_list *qout = &queue->c2_qout;
132 LASSERT(qin->pl_nr > 0);
134 CDEBUG(D_INFO, "%i %i\n", qin->pl_nr, crt);
136 * NOTE: here @page is a top-level page. This is done to avoid
137 * creation of sub-page-list.
139 cl_page_list_for_each_safe(page, tmp, qin) {
140 struct osc_async_page *oap;
141 struct obd_export *exp;
147 opg = osc_cl_page_osc(page);
149 osc = cl2osc(opg->ops_cl.cpl_obj);
150 exp = osc_export(osc);
152 if (priority > CRP_NORMAL)
153 oap->oap_async_flags |= ASYNC_HP;
155 * This can be checked without cli->cl_loi_list_lock, because
156 * ->oap_*_item are always manipulated when the page is owned.
158 if (!list_empty(&oap->oap_urgent_item) ||
159 !list_empty(&oap->oap_rpc_item)) {
164 if (osc0 == NULL) { /* first iteration */
165 cli = &exp->exp_obd->u.cli;
167 } else /* check that all pages are against the same object
169 LASSERT(osc == osc0);
171 client_obd_list_lock(&cli->cl_loi_list_lock);
172 result = cl_page_prep(env, io, page, crt);
174 cl_page_list_move(qout, qin, page);
175 if (list_empty(&oap->oap_pending_item)) {
176 osc_io_submit_page(env, cl2osc_io(env, ios),
179 result = osc_set_async_flags_base(cli,
188 if (result != -EALREADY)
191 * Handle -EALREADY error: for read case, the page is
192 * already in UPTODATE state; for write, the page
198 * Don't keep client_obd_list_lock() for too long.
200 * XXX client_obd_list lock has to be unlocked periodically to
201 * avoid soft-lockups that tend to happen otherwise (see bug
202 * 16651). On the other hand, osc_io_submit_page() queues a
203 * page with ASYNC_URGENT flag and so all pages queued up
204 * until this point are sent out immediately by
205 * osc_io_unplug() resulting in sub-optimal RPCs (sub-optimal
206 * RPCs only happen during `warm up' phase when less than
207 * cl_max_rpcs_in_flight RPCs are in flight). To balance these
208 * conflicting requirements, one might unplug once enough
209 * pages to form a large RPC were queued (i.e., use
210 * cli->cl_max_pages_per_rpc as OSC_QUEUE_GRAIN, see
211 * lop_makes_rpc()), or ignore soft-lockup issue altogether.
213 * XXX lock_need_resched() should be used here, but it is not
214 * available in the older of supported kernels.
216 if (queued > OSC_QUEUE_GRAIN || cfs_need_resched()) {
218 osc_io_unplug(env, osc, cli);
223 LASSERT(ergo(result == 0, cli != NULL));
224 LASSERT(ergo(result == 0, osc == osc0));
227 osc_io_unplug(env, osc, cli);
228 CDEBUG(D_INFO, "%i/%i %i\n", qin->pl_nr, qout->pl_nr, result);
229 return qout->pl_nr > 0 ? 0 : result;
232 static void osc_page_touch_at(const struct lu_env *env,
233 struct cl_object *obj, pgoff_t idx, unsigned to)
235 struct lov_oinfo *loi = cl2osc(obj)->oo_oinfo;
236 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
240 /* offset within stripe */
241 kms = cl_offset(obj, idx) + to;
243 cl_object_attr_lock(obj);
247 * ll_inode_size_lock(inode, 0); lov_stripe_lock(lsm);
251 CDEBUG(D_INODE, "stripe KMS %sincreasing "LPU64"->"LPU64" "LPU64"\n",
252 kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms,
253 loi->loi_lvb.lvb_size);
256 if (kms > loi->loi_kms) {
260 if (kms > loi->loi_lvb.lvb_size) {
261 attr->cat_size = kms;
264 cl_object_attr_set(env, obj, attr, valid);
265 cl_object_attr_unlock(obj);
269 * This is called when a page is accessed within file in a way that creates
270 * new page, if one were missing (i.e., if there were a hole at that place in
271 * the file, or accessed page is beyond the current file size). Examples:
272 * ->commit_write() and ->nopage() methods.
274 * Expand stripe KMS if necessary.
276 static void osc_page_touch(const struct lu_env *env,
277 struct osc_page *opage, unsigned to)
279 struct cl_page *page = opage->ops_cl.cpl_page;
280 struct cl_object *obj = opage->ops_cl.cpl_obj;
282 osc_page_touch_at(env, obj, page->cp_index, to);
286 * Implements cl_io_operations::cio_prepare_write() method for osc layer.
288 * \retval -EIO transfer initiated against this osc will most likely fail
289 * \retval 0 transfer initiated against this osc will most likely succeed.
291 * The reason for this check is to immediately return an error to the caller
292 * in the case of a deactivated import. Note, that import can be deactivated
293 * later, while pages, dirtied by this IO, are still in the cache, but this is
294 * irrelevant, because that would still return an error to the application (if
295 * it does fsync), but many applications don't do fsync because of performance
296 * issues, and we wanted to return an -EIO at write time to notify the
299 static int osc_io_prepare_write(const struct lu_env *env,
300 const struct cl_io_slice *ios,
301 const struct cl_page_slice *slice,
302 unsigned from, unsigned to)
304 struct osc_device *dev = lu2osc_dev(slice->cpl_obj->co_lu.lo_dev);
305 struct obd_import *imp = class_exp2cliimp(dev->od_exp);
310 * This implements OBD_BRW_CHECK logic from old client.
313 RETURN(imp == NULL || imp->imp_invalid ? -EIO : 0);
316 static int osc_io_commit_write(const struct lu_env *env,
317 const struct cl_io_slice *ios,
318 const struct cl_page_slice *slice,
319 unsigned from, unsigned to)
321 struct osc_page *opg = cl2osc_page(slice);
322 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
323 struct osc_async_page *oap = &opg->ops_oap;
328 * XXX instead of calling osc_page_touch() here and in
329 * osc_io_fault_start() it might be more logical to introduce
330 * cl_page_touch() method, that generic cl_io_commit_write() and page
333 osc_page_touch(env, cl2osc_page(slice), to);
334 if (!client_is_remote(osc_export(obj)) &&
335 cfs_capable(CFS_CAP_SYS_RESOURCE))
336 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
341 static int osc_io_fault_start(const struct lu_env *env,
342 const struct cl_io_slice *ios)
345 struct cl_fault_io *fio;
350 fio = &io->u.ci_fault;
351 CDEBUG(D_INFO, "%lu %i %i\n",
352 fio->ft_index, fio->ft_writable, fio->ft_nob);
354 * If mapping is writeable, adjust kms to cover this page,
355 * but do not extend kms beyond actual file size.
358 if (fio->ft_writable)
359 osc_page_touch_at(env, ios->cis_obj,
360 fio->ft_index, fio->ft_nob);
364 static int osc_punch_upcall(void *a, int rc)
366 struct osc_punch_cbargs *args = a;
369 complete(&args->opc_sync);
375 * Checks that there are no pages being written in the extent being truncated.
377 static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
378 struct osc_io *oio, size_t size)
381 struct osc_object *obj;
382 struct cl_object *clob;
383 struct cl_page *page;
384 struct cl_page_list *list;
388 clob = oio->oi_cl.cis_obj;
390 start = cl_index(clob, size);
391 partial = cl_offset(clob, start) < size;
392 list = &osc_env_info(env)->oti_plist;
395 * Complain if there are pages in the truncated region.
397 * XXX this is quite expensive check.
399 cl_page_list_init(list);
400 cl_page_gang_lookup(env, clob, io, start + partial, CL_PAGE_EOF, list);
402 cl_page_list_for_each(page, list)
403 CL_PAGE_DEBUG(D_ERROR, env, page, "exists %lu\n", start);
405 cl_page_list_disown(env, io, list);
406 cl_page_list_fini(env, list);
408 spin_lock(&obj->oo_seatbelt);
409 list_for_each_entry(cp, &obj->oo_inflight[CRT_WRITE], ops_inflight) {
410 page = cp->ops_cl.cpl_page;
411 if (page->cp_index >= start + partial) {
412 cfs_task_t *submitter;
414 submitter = cp->ops_submitter;
416 * XXX Linux specific debugging stuff.
418 CL_PAGE_DEBUG(D_ERROR, env, page, "%s/%i %lu\n",
419 submitter->comm, submitter->pid, start);
420 libcfs_debug_dumpstack(submitter);
423 spin_unlock(&obj->oo_seatbelt);
425 #else /* __KERNEL__ */
426 # define osc_trunc_check(env, io, oio, size) do {;} while (0)
429 static int osc_io_trunc_start(const struct lu_env *env,
430 const struct cl_io_slice *slice)
432 struct cl_io *io = slice->cis_io;
433 struct osc_io *oio = cl2osc_io(env, slice);
434 struct cl_object *obj = slice->cis_obj;
435 struct lov_oinfo *loi = cl2osc(obj)->oo_oinfo;
436 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
437 struct obdo *oa = &oio->oi_oa;
438 struct osc_punch_cbargs *cbargs = &oio->oi_punch_cbarg;
439 struct obd_capa *capa;
440 loff_t size = io->u.ci_truncate.tr_size;
444 memset(oa, 0, sizeof(*oa));
446 osc_trunc_check(env, io, oio, size);
448 if (oio->oi_lockless == 0) {
449 cl_object_attr_lock(obj);
450 result = cl_object_attr_get(env, obj, attr);
452 attr->cat_size = attr->cat_kms = size;
453 result = cl_object_attr_set(env, obj, attr,
456 cl_object_attr_unlock(obj);
460 oa->o_id = loi->loi_id;
461 oa->o_gr = loi->loi_gr;
462 oa->o_mtime = attr->cat_mtime;
463 oa->o_atime = attr->cat_atime;
464 oa->o_ctime = attr->cat_ctime;
465 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLATIME |
466 OBD_MD_FLCTIME | OBD_MD_FLMTIME;
467 if (oio->oi_lockless) {
468 oa->o_flags = OBD_FL_TRUNCLOCK;
469 oa->o_valid |= OBD_MD_FLFLAGS;
472 oa->o_blocks = OBD_OBJECT_EOF;
473 oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
475 capa = io->u.ci_truncate.tr_capa;
476 init_completion(&cbargs->opc_sync);
477 result = osc_punch_base(osc_export(cl2osc(obj)), oa, capa,
478 osc_punch_upcall, cbargs, PTLRPCD_SET);
483 static void osc_io_trunc_end(const struct lu_env *env,
484 const struct cl_io_slice *slice)
486 struct cl_io *io = slice->cis_io;
487 struct osc_io *oio = cl2osc_io(env, slice);
488 struct osc_punch_cbargs *cbargs = &oio->oi_punch_cbarg;
489 struct obdo *oa = &oio->oi_oa;
492 wait_for_completion(&cbargs->opc_sync);
494 result = io->ci_result = cbargs->opc_rc;
496 struct cl_object *obj = slice->cis_obj;
497 if (oio->oi_lockless == 0) {
498 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
501 /* Update kms & size */
502 if (oa->o_valid & OBD_MD_FLSIZE) {
503 attr->cat_size = oa->o_size;
504 attr->cat_kms = oa->o_size;
505 valid |= CAT_KMS|CAT_SIZE;
507 if (oa->o_valid & OBD_MD_FLBLOCKS) {
508 attr->cat_blocks = oa->o_blocks;
511 if (oa->o_valid & OBD_MD_FLMTIME) {
512 attr->cat_mtime = oa->o_mtime;
515 if (oa->o_valid & OBD_MD_FLCTIME) {
516 attr->cat_ctime = oa->o_ctime;
519 if (oa->o_valid & OBD_MD_FLATIME) {
520 attr->cat_atime = oa->o_atime;
523 cl_object_attr_lock(obj);
524 result = cl_object_attr_set(env, obj, attr, valid);
525 cl_object_attr_unlock(obj);
526 } else { /* lockless truncate */
527 struct osc_device *osd = lu2osc_dev(obj->co_lu.lo_dev);
528 /* XXX: Need a lock. */
529 osd->od_stats.os_lockless_truncates++;
536 static const struct cl_io_operations osc_io_ops = {
539 .cio_fini = osc_io_fini
542 .cio_fini = osc_io_fini
545 .cio_start = osc_io_trunc_start,
546 .cio_end = osc_io_trunc_end
549 .cio_fini = osc_io_fini,
550 .cio_start = osc_io_fault_start
553 .cio_fini = osc_io_fini
558 .cio_submit = osc_io_submit
561 .cio_submit = osc_io_submit
564 .cio_prepare_write = osc_io_prepare_write,
565 .cio_commit_write = osc_io_commit_write
568 /*****************************************************************************
570 * Transfer operations.
574 static int osc_req_prep(const struct lu_env *env,
575 const struct cl_req_slice *slice)
580 static void osc_req_completion(const struct lu_env *env,
581 const struct cl_req_slice *slice, int ioret)
585 or = cl2osc_req(slice);
586 OBD_SLAB_FREE_PTR(or, osc_req_kmem);
590 * Implementation of struct cl_req_operations::cro_attr_set() for osc
591 * layer. osc is responsible for struct obdo::o_id and struct obdo::o_gr
594 static void osc_req_attr_set(const struct lu_env *env,
595 const struct cl_req_slice *slice,
596 const struct cl_object *obj,
597 struct cl_req_attr *attr, obd_valid flags)
599 struct lov_oinfo *oinfo;
600 struct cl_req *clerq;
601 struct cl_page *apage; /* _some_ page in @clerq */
602 struct cl_lock *lock; /* _some_ lock protecting @apage */
603 struct osc_lock *olck;
604 struct osc_page *opg;
608 oinfo = cl2osc(obj)->oo_oinfo;
609 if (flags & OBD_MD_FLID) {
610 oa->o_id = oinfo->loi_id;
611 oa->o_valid |= OBD_MD_FLID;
613 if (flags & OBD_MD_FLGROUP) {
614 oa->o_gr = oinfo->loi_gr;
615 oa->o_valid |= OBD_MD_FLGROUP;
617 if (flags & OBD_MD_FLHANDLE) {
618 clerq = slice->crs_req;
619 LASSERT(!list_empty(&clerq->crq_pages));
620 apage = container_of(clerq->crq_pages.next,
621 struct cl_page, cp_flight);
622 opg = osc_cl_page_osc(apage);
623 apage = opg->ops_cl.cpl_page; /* now apage is a sub-page */
624 lock = cl_lock_at_page(env, apage->cp_obj, apage, NULL, 1, 1);
625 LASSERT(lock != NULL);
626 olck = osc_lock_at(lock);
627 LASSERT(olck != NULL);
628 /* check for lockless io. */
629 if (olck->ols_lock != NULL) {
630 oa->o_handle = olck->ols_lock->l_remote_handle;
631 oa->o_valid |= OBD_MD_FLHANDLE;
633 cl_lock_put(env, lock);
637 static const struct cl_req_operations osc_req_ops = {
638 .cro_prep = osc_req_prep,
639 .cro_attr_set = osc_req_attr_set,
640 .cro_completion = osc_req_completion
644 int osc_io_init(const struct lu_env *env,
645 struct cl_object *obj, struct cl_io *io)
647 struct osc_io *oio = osc_env_io(env);
649 CL_IO_SLICE_CLEAN(oio, oi_cl);
650 cl_io_slice_add(io, &oio->oi_cl, obj, &osc_io_ops);
654 int osc_req_init(const struct lu_env *env, struct cl_device *dev,
660 OBD_SLAB_ALLOC_PTR_GFP(or, osc_req_kmem, CFS_ALLOC_IO);
662 cl_req_slice_add(req, &or->or_cl, dev, &osc_req_ops);