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)
118 struct cl_page *page;
120 struct osc_object *osc0 = NULL;
121 struct client_obd *cli = NULL;
122 struct osc_object *osc = NULL; /* to keep gcc happy */
123 struct osc_page *opg;
126 struct cl_page_list *qin = &queue->c2_qin;
127 struct cl_page_list *qout = &queue->c2_qout;
131 LASSERT(qin->pl_nr > 0);
133 CDEBUG(D_INFO, "%i %i\n", qin->pl_nr, crt);
135 * NOTE: here @page is a top-level page. This is done to avoid
136 * creation of sub-page-list.
138 cl_page_list_for_each_safe(page, tmp, qin) {
139 struct osc_async_page *oap;
140 struct obd_export *exp;
146 opg = osc_cl_page_osc(page);
148 osc = cl2osc(opg->ops_cl.cpl_obj);
149 exp = osc_export(osc);
152 * This can be checked without cli->cl_loi_list_lock, because
153 * ->oap_*_item are always manipulated when the page is owned.
155 if (!list_empty(&oap->oap_urgent_item) ||
156 !list_empty(&oap->oap_rpc_item)) {
161 if (osc0 == NULL) { /* first iteration */
162 cli = &exp->exp_obd->u.cli;
164 } else /* check that all pages are against the same object
166 LASSERT(osc == osc0);
168 client_obd_list_lock(&cli->cl_loi_list_lock);
169 result = cl_page_prep(env, io, page, crt);
171 cl_page_list_move(qout, qin, page);
172 if (list_empty(&oap->oap_pending_item)) {
173 osc_io_submit_page(env, cl2osc_io(env, ios),
176 result = osc_set_async_flags_base(cli,
185 if (result != -EALREADY)
188 * Handle -EALREADY error: for read case, the page is
189 * already in UPTODATE state; for write, the page
195 * Don't keep client_obd_list_lock() for too long.
197 * XXX lock_need_resched() should be used here, but it is not
198 * available in the older of supported kernels.
200 if (queued > OSC_QUEUE_GRAIN || cfs_need_resched()) {
202 osc_io_unplug(env, osc, cli);
207 LASSERT(ergo(result == 0, cli != NULL));
208 LASSERT(ergo(result == 0, osc == osc0));
211 osc_io_unplug(env, osc, cli);
212 CDEBUG(D_INFO, "%i/%i %i\n", qin->pl_nr, qout->pl_nr, result);
213 return qout->pl_nr > 0 ? 0 : result;
216 static void osc_page_touch_at(const struct lu_env *env,
217 struct cl_object *obj, pgoff_t idx, unsigned to)
219 struct lov_oinfo *loi = cl2osc(obj)->oo_oinfo;
220 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
224 /* offset within stripe */
225 kms = cl_offset(obj, idx) + to;
227 cl_object_attr_lock(obj);
231 * ll_inode_size_lock(inode, 0); lov_stripe_lock(lsm);
235 CDEBUG(D_INODE, "stripe KMS %sincreasing "LPU64"->"LPU64" "LPU64"\n",
236 kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms,
237 loi->loi_lvb.lvb_size);
240 if (kms > loi->loi_kms) {
244 if (kms > loi->loi_lvb.lvb_size) {
245 attr->cat_size = kms;
248 cl_object_attr_set(env, obj, attr, valid);
249 cl_object_attr_unlock(obj);
253 * This is called when a page is accessed within file in a way that creates
254 * new page, if one were missing (i.e., if there were a hole at that place in
255 * the file, or accessed page is beyond the current file size). Examples:
256 * ->commit_write() and ->nopage() methods.
258 * Expand stripe KMS if necessary.
260 static void osc_page_touch(const struct lu_env *env,
261 struct osc_page *opage, unsigned to)
263 struct cl_page *page = opage->ops_cl.cpl_page;
264 struct cl_object *obj = opage->ops_cl.cpl_obj;
266 osc_page_touch_at(env, obj, page->cp_index, to);
270 * Implements cl_io_operations::cio_prepare_write() method for osc layer.
272 * \retval -EIO transfer initiated against this osc will most likely fail
273 * \retval 0 transfer initiated against this osc will most likely succeed.
275 * The reason for this check is to immediately return an error to the caller
276 * in the case of a deactivated import. Note, that import can be deactivated
277 * later, while pages, dirtied by this IO, are still in the cache, but this is
278 * irrelevant, because that would still return an error to the application (if
279 * it does fsync), but many applications don't do fsync because of performance
280 * issues, and we wanted to return an -EIO at write time to notify the
283 static int osc_io_prepare_write(const struct lu_env *env,
284 const struct cl_io_slice *ios,
285 const struct cl_page_slice *slice,
286 unsigned from, unsigned to)
288 struct osc_device *dev = lu2osc_dev(slice->cpl_obj->co_lu.lo_dev);
289 struct obd_import *imp = class_exp2cliimp(dev->od_exp);
294 * This implements OBD_BRW_CHECK logic from old client.
297 RETURN(imp == NULL || imp->imp_invalid ? -EIO : 0);
300 static int osc_io_commit_write(const struct lu_env *env,
301 const struct cl_io_slice *ios,
302 const struct cl_page_slice *slice,
303 unsigned from, unsigned to)
305 struct osc_page *opg = cl2osc_page(slice);
306 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
307 struct osc_async_page *oap = &opg->ops_oap;
312 * XXX instead of calling osc_page_touch() here and in
313 * osc_io_fault_start() it might be more logical to introduce
314 * cl_page_touch() method, that generic cl_io_commit_write() and page
317 osc_page_touch(env, cl2osc_page(slice), to);
318 if (!client_is_remote(osc_export(obj)) &&
319 cfs_capable(CFS_CAP_SYS_RESOURCE))
320 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
325 static int osc_io_fault_start(const struct lu_env *env,
326 const struct cl_io_slice *ios)
329 struct cl_fault_io *fio;
334 fio = &io->u.ci_fault;
335 CDEBUG(D_INFO, "%lu %i %i\n",
336 fio->ft_index, fio->ft_writable, fio->ft_nob);
338 * If mapping is writeable, adjust kms to cover this page,
339 * but do not extend kms beyond actual file size.
342 if (fio->ft_writable)
343 osc_page_touch_at(env, ios->cis_obj,
344 fio->ft_index, fio->ft_nob);
348 static int osc_punch_upcall(void *a, int rc)
350 struct osc_punch_cbargs *args = a;
353 complete(&args->opc_sync);
359 * Checks that there are no pages being written in the extent being truncated.
361 static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
362 struct osc_io *oio, size_t size)
365 struct osc_object *obj;
366 struct cl_object *clob;
367 struct cl_page *page;
368 struct cl_page_list *list;
372 clob = oio->oi_cl.cis_obj;
374 start = cl_index(clob, size);
375 partial = cl_offset(clob, start) < size;
376 list = &osc_env_info(env)->oti_plist;
379 * Complain if there are pages in the truncated region.
381 * XXX this is quite expensive check.
383 cl_page_list_init(list);
384 cl_page_gang_lookup(env, clob, io, start + partial, CL_PAGE_EOF, list);
386 cl_page_list_for_each(page, list)
387 CL_PAGE_DEBUG(D_ERROR, env, page, "exists %lu\n", start);
389 cl_page_list_disown(env, io, list);
390 cl_page_list_fini(env, list);
392 spin_lock(&obj->oo_seatbelt);
393 list_for_each_entry(cp, &obj->oo_inflight[CRT_WRITE], ops_inflight) {
394 page = cp->ops_cl.cpl_page;
395 if (page->cp_index >= start + partial) {
396 cfs_task_t *submitter;
398 submitter = cp->ops_submitter;
400 * XXX Linux specific debugging stuff.
402 CL_PAGE_DEBUG(D_ERROR, env, page, "%s/%i %lu\n",
403 submitter->comm, submitter->pid, start);
404 libcfs_debug_dumpstack(submitter);
407 spin_unlock(&obj->oo_seatbelt);
409 #else /* __KERNEL__ */
410 # define osc_trunc_check(env, io, oio, size) do {;} while (0)
413 static int osc_io_trunc_start(const struct lu_env *env,
414 const struct cl_io_slice *slice)
416 struct cl_io *io = slice->cis_io;
417 struct osc_io *oio = cl2osc_io(env, slice);
418 struct cl_object *obj = slice->cis_obj;
419 struct lov_oinfo *loi = cl2osc(obj)->oo_oinfo;
420 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
421 struct obdo *oa = &oio->oi_oa;
422 struct osc_punch_cbargs *cbargs = &oio->oi_punch_cbarg;
423 struct obd_capa *capa;
424 loff_t size = io->u.ci_truncate.tr_size;
427 memset(oa, 0, sizeof(*oa));
429 osc_trunc_check(env, io, oio, size);
431 cl_object_attr_lock(obj);
432 result = cl_object_attr_get(env, obj, attr);
434 attr->cat_size = attr->cat_kms = size;
435 result = cl_object_attr_set(env, obj, attr, CAT_SIZE|CAT_KMS);
437 cl_object_attr_unlock(obj);
440 oa->o_id = loi->loi_id;
441 oa->o_gr = loi->loi_gr;
442 oa->o_mtime = attr->cat_mtime;
443 oa->o_atime = attr->cat_atime;
444 oa->o_ctime = attr->cat_ctime;
445 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLATIME |
446 OBD_MD_FLCTIME | OBD_MD_FLMTIME;
447 if (oio->oi_lockless) {
448 oa->o_flags = OBD_FL_TRUNCLOCK;
449 oa->o_valid |= OBD_MD_FLFLAGS;
452 oa->o_blocks = OBD_OBJECT_EOF;
453 oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
455 capa = io->u.ci_truncate.tr_capa;
456 init_completion(&cbargs->opc_sync);
457 result = osc_punch_base(osc_export(cl2osc(obj)), oa, capa,
458 osc_punch_upcall, cbargs, PTLRPCD_SET);
463 static void osc_io_trunc_end(const struct lu_env *env,
464 const struct cl_io_slice *slice)
466 struct cl_io *io = slice->cis_io;
467 struct osc_io *oio = cl2osc_io(env, slice);
468 struct osc_punch_cbargs *cbargs = &oio->oi_punch_cbarg;
469 struct obdo *oa = &oio->oi_oa;
472 wait_for_completion(&cbargs->opc_sync);
474 result = io->ci_result = cbargs->opc_rc;
476 struct cl_object *obj = slice->cis_obj;
477 if (oio->oi_lockless == 0) {
478 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
481 /* Update kms & size */
482 if (oa->o_valid & OBD_MD_FLSIZE) {
483 attr->cat_size = oa->o_size;
484 attr->cat_kms = oa->o_size;
485 valid |= CAT_KMS|CAT_SIZE;
487 if (oa->o_valid & OBD_MD_FLBLOCKS) {
488 attr->cat_blocks = oa->o_blocks;
491 if (oa->o_valid & OBD_MD_FLMTIME) {
492 attr->cat_mtime = oa->o_mtime;
495 if (oa->o_valid & OBD_MD_FLCTIME) {
496 attr->cat_ctime = oa->o_ctime;
499 if (oa->o_valid & OBD_MD_FLATIME) {
500 attr->cat_atime = oa->o_atime;
503 cl_object_attr_lock(obj);
504 result = cl_object_attr_set(env, obj, attr, valid);
505 cl_object_attr_unlock(obj);
506 } else { /* lockless truncate */
507 struct osc_device *osd = lu2osc_dev(obj->co_lu.lo_dev);
508 /* XXX: Need a lock. */
509 osd->od_stats.os_lockless_truncates++;
516 static const struct cl_io_operations osc_io_ops = {
519 .cio_fini = osc_io_fini
522 .cio_fini = osc_io_fini
525 .cio_start = osc_io_trunc_start,
526 .cio_end = osc_io_trunc_end
529 .cio_fini = osc_io_fini,
530 .cio_start = osc_io_fault_start
533 .cio_fini = osc_io_fini
538 .cio_submit = osc_io_submit
541 .cio_submit = osc_io_submit
544 .cio_prepare_write = osc_io_prepare_write,
545 .cio_commit_write = osc_io_commit_write
548 /*****************************************************************************
550 * Transfer operations.
554 static int osc_req_prep(const struct lu_env *env,
555 const struct cl_req_slice *slice)
560 static void osc_req_completion(const struct lu_env *env,
561 const struct cl_req_slice *slice, int ioret)
565 or = cl2osc_req(slice);
566 OBD_SLAB_FREE_PTR(or, osc_req_kmem);
570 * Implementation of struct cl_req_operations::cro_attr_set() for osc
571 * layer. osc is responsible for struct obdo::o_id and struct obdo::o_gr
574 static void osc_req_attr_set(const struct lu_env *env,
575 const struct cl_req_slice *slice,
576 const struct cl_object *obj,
577 struct cl_req_attr *attr, obd_valid flags)
579 struct lov_oinfo *oinfo;
580 struct cl_req *clerq;
581 struct cl_page *apage; /* _some_ page in @clerq */
582 struct cl_lock *lock; /* _some_ lock protecting @apage */
583 struct osc_lock *olck;
584 struct osc_page *opg;
588 oinfo = cl2osc(obj)->oo_oinfo;
589 if (flags & OBD_MD_FLID) {
590 oa->o_id = oinfo->loi_id;
591 oa->o_valid |= OBD_MD_FLID;
593 if (flags & OBD_MD_FLGROUP) {
594 oa->o_gr = oinfo->loi_gr;
595 oa->o_valid |= OBD_MD_FLGROUP;
597 if (flags & OBD_MD_FLHANDLE) {
598 clerq = slice->crs_req;
599 LASSERT(!list_empty(&clerq->crq_pages));
600 apage = container_of(clerq->crq_pages.next,
601 struct cl_page, cp_flight);
602 opg = osc_cl_page_osc(apage);
603 apage = opg->ops_cl.cpl_page; /* now apage is a sub-page */
604 lock = cl_lock_at_page(env, apage->cp_obj, apage, NULL, 1, 1);
606 olck = osc_lock_at(lock);
607 LASSERT(olck != NULL);
608 /* check for lockless io. */
609 if (olck->ols_lock != NULL) {
610 oa->o_handle = olck->ols_lock->l_remote_handle;
611 oa->o_valid |= OBD_MD_FLHANDLE;
613 cl_lock_put(env, lock);
615 /* Should only be possible with liblustre */
616 LASSERT(LIBLUSTRE_CLIENT);
621 static const struct cl_req_operations osc_req_ops = {
622 .cro_prep = osc_req_prep,
623 .cro_attr_set = osc_req_attr_set,
624 .cro_completion = osc_req_completion
628 int osc_io_init(const struct lu_env *env,
629 struct cl_object *obj, struct cl_io *io)
631 struct osc_io *oio = osc_env_io(env);
633 CL_IO_SLICE_CLEAN(oio, oi_cl);
634 cl_io_slice_add(io, &oio->oi_cl, obj, &osc_io_ops);
638 int osc_req_init(const struct lu_env *env, struct cl_device *dev,
644 OBD_SLAB_ALLOC_PTR(or, osc_req_kmem);
646 cl_req_slice_add(req, &or->or_cl, dev, &osc_req_ops);