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 #define DEBUG_SUBSYSTEM S_OSC
43 #include "osc_cl_internal.h"
49 /*****************************************************************************
55 static struct osc_req *cl2osc_req(const struct cl_req_slice *slice)
57 LINVRNT(slice->crs_dev->cd_lu_dev.ld_type == &osc_device_type);
58 return container_of0(slice, struct osc_req, or_cl);
61 static struct osc_io *cl2osc_io(const struct lu_env *env,
62 const struct cl_io_slice *slice)
64 struct osc_io *oio = container_of0(slice, struct osc_io, oi_cl);
65 LINVRNT(oio == osc_env_io(env));
69 static struct osc_page *osc_cl_page_osc(struct cl_page *page)
71 const struct cl_page_slice *slice;
73 slice = cl_page_at(page, &osc_device_type);
74 LASSERT(slice != NULL);
76 return cl2osc_page(slice);
80 /*****************************************************************************
86 static void osc_io_fini(const struct lu_env *env, const struct cl_io_slice *io)
90 struct cl_page *osc_oap2cl_page(struct osc_async_page *oap)
92 return container_of(oap, struct osc_page, ops_oap)->ops_cl.cpl_page;
95 static void osc_io_unplug(const struct lu_env *env, struct osc_object *osc,
96 struct client_obd *cli)
98 loi_list_maint(cli, osc->oo_oinfo);
99 osc_check_rpcs(env, cli);
100 client_obd_list_unlock(&cli->cl_loi_list_lock);
104 * An implementation of cl_io_operations::cio_io_submit() method for osc
105 * layer. Iterates over pages in the in-queue, prepares each for io by calling
106 * cl_page_prep() and then either submits them through osc_io_submit_page()
107 * or, if page is already submitted, changes osc flags through
108 * osc_set_async_flags_base().
110 static int osc_io_submit(const struct lu_env *env,
111 const struct cl_io_slice *ios,
112 enum cl_req_type crt, struct cl_2queue *queue,
113 enum cl_req_priority priority)
115 struct cl_page *page;
117 struct osc_object *osc0 = NULL;
118 struct client_obd *cli = NULL;
119 struct osc_object *osc = NULL; /* to keep gcc happy */
120 struct osc_page *opg;
123 struct cl_page_list *qin = &queue->c2_qin;
124 struct cl_page_list *qout = &queue->c2_qout;
128 LASSERT(qin->pl_nr > 0);
130 CDEBUG(D_INFO, "%i %i\n", qin->pl_nr, crt);
132 * NOTE: here @page is a top-level page. This is done to avoid
133 * creation of sub-page-list.
135 cl_page_list_for_each_safe(page, tmp, qin) {
136 struct osc_async_page *oap;
137 struct obd_export *exp;
143 opg = osc_cl_page_osc(page);
145 osc = cl2osc(opg->ops_cl.cpl_obj);
146 exp = osc_export(osc);
148 if (priority > CRP_NORMAL) {
149 cfs_spin_lock(&oap->oap_lock);
150 oap->oap_async_flags |= ASYNC_HP;
151 cfs_spin_unlock(&oap->oap_lock);
154 * This can be checked without cli->cl_loi_list_lock, because
155 * ->oap_*_item are always manipulated when the page is owned.
157 if (!cfs_list_empty(&oap->oap_urgent_item) ||
158 !cfs_list_empty(&oap->oap_rpc_item)) {
163 if (osc0 == NULL) { /* first iteration */
164 cli = &exp->exp_obd->u.cli;
166 } else /* check that all pages are against the same object
168 LASSERT(osc == osc0);
170 client_obd_list_lock(&cli->cl_loi_list_lock);
171 result = cl_page_prep(env, io, page, crt);
173 cl_page_list_move(qout, qin, page);
174 if (cfs_list_empty(&oap->oap_pending_item)) {
175 osc_io_submit_page(env, cl2osc_io(env, ios),
178 result = osc_set_async_flags_base(cli,
183 * bug 18881: we can't just break out here when
184 * error occurrs after cl_page_prep has been
185 * called against the page. The correct
186 * way is to call page's completion routine,
187 * as in osc_oap_interrupted. For simplicity,
188 * we just force osc_set_async_flags_base() to
191 LASSERT(result == 0);
193 opg->ops_submit_time = cfs_time_current();
196 if (result != -EALREADY)
199 * Handle -EALREADY error: for read case, the page is
200 * already in UPTODATE state; for write, the page
207 * We might hold client_obd_list_lock() for too long and cause
208 * soft-lockups (see bug 16651). But on the other hand, pages
209 * are queued here with ASYNC_URGENT flag, thus will be sent
210 * out immediately once osc_io_unplug() be called, possibly
211 * resulting sub-optimal RPCs.
213 * We think creating optimal-sized RPCs is more important than
214 * avoiding the transient soft-lockups, plus I believe the
215 * soft-locks only happen in full debug testing.
219 LASSERT(ergo(result == 0, cli != NULL));
220 LASSERT(ergo(result == 0, osc == osc0));
223 osc_io_unplug(env, osc, cli);
224 CDEBUG(D_INFO, "%i/%i %i\n", qin->pl_nr, qout->pl_nr, result);
225 return qout->pl_nr > 0 ? 0 : result;
228 static void osc_page_touch_at(const struct lu_env *env,
229 struct cl_object *obj, pgoff_t idx, unsigned to)
231 struct lov_oinfo *loi = cl2osc(obj)->oo_oinfo;
232 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
236 /* offset within stripe */
237 kms = cl_offset(obj, idx) + to;
239 cl_object_attr_lock(obj);
243 * ll_inode_size_lock(inode, 0); lov_stripe_lock(lsm);
247 CDEBUG(D_INODE, "stripe KMS %sincreasing "LPU64"->"LPU64" "LPU64"\n",
248 kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms,
249 loi->loi_lvb.lvb_size);
252 if (kms > loi->loi_kms) {
256 if (kms > loi->loi_lvb.lvb_size) {
257 attr->cat_size = kms;
260 cl_object_attr_set(env, obj, attr, valid);
261 cl_object_attr_unlock(obj);
265 * This is called when a page is accessed within file in a way that creates
266 * new page, if one were missing (i.e., if there were a hole at that place in
267 * the file, or accessed page is beyond the current file size). Examples:
268 * ->commit_write() and ->nopage() methods.
270 * Expand stripe KMS if necessary.
272 static void osc_page_touch(const struct lu_env *env,
273 struct osc_page *opage, unsigned to)
275 struct cl_page *page = opage->ops_cl.cpl_page;
276 struct cl_object *obj = opage->ops_cl.cpl_obj;
278 osc_page_touch_at(env, obj, page->cp_index, to);
282 * Implements cl_io_operations::cio_prepare_write() method for osc layer.
284 * \retval -EIO transfer initiated against this osc will most likely fail
285 * \retval 0 transfer initiated against this osc will most likely succeed.
287 * The reason for this check is to immediately return an error to the caller
288 * in the case of a deactivated import. Note, that import can be deactivated
289 * later, while pages, dirtied by this IO, are still in the cache, but this is
290 * irrelevant, because that would still return an error to the application (if
291 * it does fsync), but many applications don't do fsync because of performance
292 * issues, and we wanted to return an -EIO at write time to notify the
295 static int osc_io_prepare_write(const struct lu_env *env,
296 const struct cl_io_slice *ios,
297 const struct cl_page_slice *slice,
298 unsigned from, unsigned to)
300 struct osc_device *dev = lu2osc_dev(slice->cpl_obj->co_lu.lo_dev);
301 struct obd_import *imp = class_exp2cliimp(dev->od_exp);
306 * This implements OBD_BRW_CHECK logic from old client.
309 RETURN(imp == NULL || imp->imp_invalid ? -EIO : 0);
312 static int osc_io_commit_write(const struct lu_env *env,
313 const struct cl_io_slice *ios,
314 const struct cl_page_slice *slice,
315 unsigned from, unsigned to)
317 struct osc_page *opg = cl2osc_page(slice);
318 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
319 struct osc_async_page *oap = &opg->ops_oap;
324 * XXX instead of calling osc_page_touch() here and in
325 * osc_io_fault_start() it might be more logical to introduce
326 * cl_page_touch() method, that generic cl_io_commit_write() and page
329 osc_page_touch(env, cl2osc_page(slice), to);
330 if (!client_is_remote(osc_export(obj)) &&
331 cfs_capable(CFS_CAP_SYS_RESOURCE))
332 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
337 static int osc_io_fault_start(const struct lu_env *env,
338 const struct cl_io_slice *ios)
341 struct cl_fault_io *fio;
346 fio = &io->u.ci_fault;
347 CDEBUG(D_INFO, "%lu %i %i\n",
348 fio->ft_index, fio->ft_writable, fio->ft_nob);
350 * If mapping is writeable, adjust kms to cover this page,
351 * but do not extend kms beyond actual file size.
354 if (fio->ft_writable)
355 osc_page_touch_at(env, ios->cis_obj,
356 fio->ft_index, fio->ft_nob);
360 static int osc_setattr_upcall(void *a, int rc)
362 struct osc_setattr_cbargs *args = a;
365 cfs_complete(&args->opc_sync);
369 /* Disable osc_trunc_check() because it is naturally race between read and
370 * truncate. See bug 20645 for details.
372 #if 0 && defined(__KERNEL__)
374 * Checks that there are no pages being written in the extent being truncated.
376 static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
377 struct osc_io *oio, size_t size)
380 struct osc_object *obj;
381 struct cl_object *clob;
382 struct cl_page *page;
383 struct cl_page_list *list;
387 clob = oio->oi_cl.cis_obj;
389 start = cl_index(clob, size);
390 partial = cl_offset(clob, start) < size;
391 list = &osc_env_info(env)->oti_plist;
394 * Complain if there are pages in the truncated region.
396 * XXX this is quite expensive check.
398 cl_page_list_init(list);
399 cl_page_gang_lookup(env, clob, io, start + partial, CL_PAGE_EOF, list, 0);
401 cl_page_list_for_each(page, list)
402 CL_PAGE_DEBUG(D_ERROR, env, page, "exists %lu\n", start);
404 cl_page_list_disown(env, io, list);
405 cl_page_list_fini(env, list);
407 cfs_spin_lock(&obj->oo_seatbelt);
408 cfs_list_for_each_entry(cp, &obj->oo_inflight[CRT_WRITE],
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 cfs_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_setattr_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_setattr_cbargs *cbargs = &oio->oi_setattr_cbarg;
439 loff_t size = io->u.ci_setattr.sa_attr.lvb_size;
440 unsigned int ia_valid = io->u.ci_setattr.sa_valid;
442 struct obd_info oinfo = { { { 0 } } };
444 if (ia_valid & ATTR_SIZE)
445 osc_trunc_check(env, io, oio, size);
447 if (oio->oi_lockless == 0) {
448 cl_object_attr_lock(obj);
449 result = cl_object_attr_get(env, obj, attr);
451 unsigned int cl_valid = 0;
453 if (ia_valid & ATTR_SIZE) {
454 attr->cat_size = attr->cat_kms = size;
455 cl_valid = (CAT_SIZE | CAT_KMS);
457 if (ia_valid & ATTR_MTIME_SET) {
458 attr->cat_mtime = io->u.ci_setattr.sa_attr.lvb_mtime;
459 cl_valid |= CAT_MTIME;
461 if (ia_valid & ATTR_ATIME_SET) {
462 attr->cat_atime = io->u.ci_setattr.sa_attr.lvb_atime;
463 cl_valid |= CAT_ATIME;
465 if (ia_valid & ATTR_CTIME_SET) {
466 attr->cat_ctime = io->u.ci_setattr.sa_attr.lvb_ctime;
467 cl_valid |= CAT_CTIME;
469 result = cl_object_attr_set(env, obj, attr, cl_valid);
471 cl_object_attr_unlock(obj);
473 memset(oa, 0, sizeof(*oa));
475 oa->o_id = loi->loi_id;
476 oa->o_gr = loi->loi_gr;
477 oa->o_mtime = attr->cat_mtime;
478 oa->o_atime = attr->cat_atime;
479 oa->o_ctime = attr->cat_ctime;
480 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLATIME |
481 OBD_MD_FLCTIME | OBD_MD_FLMTIME;
482 if (ia_valid & ATTR_SIZE) {
484 oa->o_blocks = OBD_OBJECT_EOF;
485 oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
487 if (oio->oi_lockless) {
488 oa->o_flags = OBD_FL_SRVLOCK;
489 oa->o_valid |= OBD_MD_FLFLAGS;
492 LASSERT(oio->oi_lockless == 0);
496 oinfo.oi_capa = io->u.ci_setattr.sa_capa;
497 cfs_init_completion(&cbargs->opc_sync);
499 if (ia_valid & ATTR_SIZE)
500 result = osc_punch_base(osc_export(cl2osc(obj)),
501 &oinfo, osc_setattr_upcall,
502 cbargs, PTLRPCD_SET);
504 result = osc_setattr_async_base(osc_export(cl2osc(obj)),
507 cbargs, PTLRPCD_SET);
512 static void osc_io_setattr_end(const struct lu_env *env,
513 const struct cl_io_slice *slice)
515 struct cl_io *io = slice->cis_io;
516 struct osc_io *oio = cl2osc_io(env, slice);
517 struct osc_setattr_cbargs *cbargs = &oio->oi_setattr_cbarg;
520 cfs_wait_for_completion(&cbargs->opc_sync);
522 result = io->ci_result = cbargs->opc_rc;
524 struct cl_object *obj = slice->cis_obj;
525 if (oio->oi_lockless) {
526 /* lockless truncate */
527 struct osc_device *osd = lu2osc_dev(obj->co_lu.lo_dev);
529 LASSERT(cl_io_is_trunc(io));
530 /* XXX: Need a lock. */
531 osd->od_stats.os_lockless_truncates++;
536 static int osc_io_read_start(const struct lu_env *env,
537 const struct cl_io_slice *slice)
539 struct osc_io *oio = cl2osc_io(env, slice);
540 struct cl_object *obj = slice->cis_obj;
541 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
545 if (oio->oi_lockless == 0) {
546 cl_object_attr_lock(obj);
547 result = cl_object_attr_get(env, obj, attr);
549 attr->cat_atime = LTIME_S(CFS_CURRENT_TIME);
550 result = cl_object_attr_set(env, obj, attr,
553 cl_object_attr_unlock(obj);
558 static int osc_io_write_start(const struct lu_env *env,
559 const struct cl_io_slice *slice)
561 struct osc_io *oio = cl2osc_io(env, slice);
562 struct cl_object *obj = slice->cis_obj;
563 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
567 if (oio->oi_lockless == 0) {
568 cl_object_attr_lock(obj);
569 result = cl_object_attr_get(env, obj, attr);
571 attr->cat_mtime = attr->cat_ctime =
572 LTIME_S(CFS_CURRENT_TIME);
573 result = cl_object_attr_set(env, obj, attr,
574 CAT_MTIME | CAT_CTIME);
576 cl_object_attr_unlock(obj);
581 static const struct cl_io_operations osc_io_ops = {
584 .cio_start = osc_io_read_start,
585 .cio_fini = osc_io_fini
588 .cio_start = osc_io_write_start,
589 .cio_fini = osc_io_fini
592 .cio_start = osc_io_setattr_start,
593 .cio_end = osc_io_setattr_end
596 .cio_fini = osc_io_fini,
597 .cio_start = osc_io_fault_start
600 .cio_fini = osc_io_fini
605 .cio_submit = osc_io_submit
608 .cio_submit = osc_io_submit
611 .cio_prepare_write = osc_io_prepare_write,
612 .cio_commit_write = osc_io_commit_write
615 /*****************************************************************************
617 * Transfer operations.
621 static int osc_req_prep(const struct lu_env *env,
622 const struct cl_req_slice *slice)
627 static void osc_req_completion(const struct lu_env *env,
628 const struct cl_req_slice *slice, int ioret)
632 or = cl2osc_req(slice);
633 OBD_SLAB_FREE_PTR(or, osc_req_kmem);
637 * Implementation of struct cl_req_operations::cro_attr_set() for osc
638 * layer. osc is responsible for struct obdo::o_id and struct obdo::o_gr
641 static void osc_req_attr_set(const struct lu_env *env,
642 const struct cl_req_slice *slice,
643 const struct cl_object *obj,
644 struct cl_req_attr *attr, obd_valid flags)
646 struct lov_oinfo *oinfo;
647 struct cl_req *clerq;
648 struct cl_page *apage; /* _some_ page in @clerq */
649 struct cl_lock *lock; /* _some_ lock protecting @apage */
650 struct osc_lock *olck;
651 struct osc_page *opg;
655 oinfo = cl2osc(obj)->oo_oinfo;
656 if (flags & OBD_MD_FLID) {
657 oa->o_id = oinfo->loi_id;
658 oa->o_valid |= OBD_MD_FLID;
660 if (flags & OBD_MD_FLGROUP) {
661 oa->o_gr = oinfo->loi_gr;
662 oa->o_valid |= OBD_MD_FLGROUP;
664 if (flags & OBD_MD_FLHANDLE) {
665 clerq = slice->crs_req;
666 LASSERT(!cfs_list_empty(&clerq->crq_pages));
667 apage = container_of(clerq->crq_pages.next,
668 struct cl_page, cp_flight);
669 opg = osc_cl_page_osc(apage);
670 apage = opg->ops_cl.cpl_page; /* now apage is a sub-page */
671 lock = cl_lock_at_page(env, apage->cp_obj, apage, NULL, 1, 1);
673 struct cl_object_header *head;
674 struct cl_lock *scan;
676 head = cl_object_header(apage->cp_obj);
677 cfs_list_for_each_entry(scan, &head->coh_locks,
679 CL_LOCK_DEBUG(D_ERROR, env, scan,
681 CL_PAGE_DEBUG(D_ERROR, env, apage,
682 "dump uncover page!\n");
683 libcfs_debug_dumpstack(NULL);
687 olck = osc_lock_at(lock);
688 LASSERT(olck != NULL);
689 LASSERT(ergo(opg->ops_srvlock, olck->ols_lock == NULL));
690 /* check for lockless io. */
691 if (olck->ols_lock != NULL) {
692 oa->o_handle = olck->ols_lock->l_remote_handle;
693 oa->o_valid |= OBD_MD_FLHANDLE;
695 cl_lock_put(env, lock);
699 static const struct cl_req_operations osc_req_ops = {
700 .cro_prep = osc_req_prep,
701 .cro_attr_set = osc_req_attr_set,
702 .cro_completion = osc_req_completion
706 int osc_io_init(const struct lu_env *env,
707 struct cl_object *obj, struct cl_io *io)
709 struct osc_io *oio = osc_env_io(env);
711 CL_IO_SLICE_CLEAN(oio, oi_cl);
712 cl_io_slice_add(io, &oio->oi_cl, obj, &osc_io_ops);
716 int osc_req_init(const struct lu_env *env, struct cl_device *dev,
722 OBD_SLAB_ALLOC_PTR_GFP(or, osc_req_kmem, CFS_ALLOC_IO);
724 cl_req_slice_add(req, &or->or_cl, dev, &osc_req_ops);