4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Implementation of cl_io for OSC layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@whamcloud.com>
38 #define DEBUG_SUBSYSTEM S_OSC
40 #include <lustre_obdo.h>
41 #include <lustre_osc.h>
43 #include "osc_internal.h"
49 /*****************************************************************************
55 static void osc_io_fini(const struct lu_env *env, const struct cl_io_slice *io)
59 static void osc_read_ahead_release(const struct lu_env *env,
62 struct ldlm_lock *dlmlock = cbdata;
63 struct lustre_handle lockh;
65 ldlm_lock2handle(dlmlock, &lockh);
66 ldlm_lock_decref(&lockh, LCK_PR);
67 LDLM_LOCK_PUT(dlmlock);
70 static int osc_io_read_ahead(const struct lu_env *env,
71 const struct cl_io_slice *ios,
72 pgoff_t start, struct cl_read_ahead *ra)
74 struct osc_object *osc = cl2osc(ios->cis_obj);
75 struct ldlm_lock *dlmlock;
76 int result = -ENODATA;
79 dlmlock = osc_dlmlock_at_pgoff(env, osc, start, 0);
80 if (dlmlock != NULL) {
81 LASSERT(dlmlock->l_ast_data == osc);
82 if (dlmlock->l_req_mode != LCK_PR) {
83 struct lustre_handle lockh;
84 ldlm_lock2handle(dlmlock, &lockh);
85 ldlm_lock_addref(&lockh, LCK_PR);
86 ldlm_lock_decref(&lockh, dlmlock->l_req_mode);
89 ra->cra_rpc_size = osc_cli(osc)->cl_max_pages_per_rpc;
90 ra->cra_end = cl_index(osc2cl(osc),
91 dlmlock->l_policy_data.l_extent.end);
92 ra->cra_release = osc_read_ahead_release;
93 ra->cra_cbdata = dlmlock;
101 * An implementation of cl_io_operations::cio_io_submit() method for osc
102 * layer. Iterates over pages in the in-queue, prepares each for io by calling
103 * cl_page_prep() and then either submits them through osc_io_submit_page()
104 * or, if page is already submitted, changes osc flags through
105 * osc_set_async_flags().
107 static int osc_io_submit(const struct lu_env *env,
108 const struct cl_io_slice *ios,
109 enum cl_req_type crt, struct cl_2queue *queue)
111 struct cl_page *page;
113 struct client_obd *cli = NULL;
114 struct osc_object *osc = NULL; /* to keep gcc happy */
115 struct osc_page *opg;
117 struct list_head list = LIST_HEAD_INIT(list);
119 struct cl_page_list *qin = &queue->c2_qin;
120 struct cl_page_list *qout = &queue->c2_qout;
121 unsigned int queued = 0;
125 unsigned int max_pages;
127 LASSERT(qin->pl_nr > 0);
129 CDEBUG(D_CACHE|D_READA, "%d %d\n", qin->pl_nr, crt);
131 osc = cl2osc(ios->cis_obj);
133 max_pages = cli->cl_max_pages_per_rpc;
135 cmd = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
136 brw_flags = osc_io_srvlock(cl2osc_io(env, ios)) ? OBD_BRW_SRVLOCK : 0;
139 * NOTE: here @page is a top-level page. This is done to avoid
140 * creation of sub-page-list.
142 cl_page_list_for_each_safe(page, tmp, qin) {
143 struct osc_async_page *oap;
149 opg = osc_cl_page_osc(page, osc);
151 LASSERT(osc == oap->oap_obj);
153 if (!list_empty(&oap->oap_pending_item) ||
154 !list_empty(&oap->oap_rpc_item)) {
155 CDEBUG(D_CACHE, "Busy oap %p page %p for submit.\n",
161 result = cl_page_prep(env, io, page, crt);
164 if (result != -EALREADY)
167 * Handle -EALREADY error: for read case, the page is
168 * already in UPTODATE state; for write, the page
175 spin_lock(&oap->oap_lock);
176 oap->oap_async_flags = ASYNC_URGENT|ASYNC_READY;
177 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
178 spin_unlock(&oap->oap_lock);
180 osc_page_submit(env, opg, crt, brw_flags);
181 list_add_tail(&oap->oap_pending_item, &list);
183 if (page->cp_sync_io != NULL)
184 cl_page_list_move(qout, qin, page);
186 cl_page_list_del(env, qin, page);
188 if (++queued == max_pages) {
190 result = osc_queue_sync_pages(env, osc, &list, cmd,
198 result = osc_queue_sync_pages(env, osc, &list, cmd, brw_flags);
200 /* Update c/mtime for sync write. LU-7310 */
201 if (crt == CRT_WRITE && qout->pl_nr > 0 && result == 0) {
202 struct cl_object *obj = ios->cis_obj;
203 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
205 cl_object_attr_lock(obj);
206 attr->cat_mtime = attr->cat_ctime = ktime_get_real_seconds();
207 cl_object_attr_update(env, obj, attr, CAT_MTIME | CAT_CTIME);
208 cl_object_attr_unlock(obj);
211 CDEBUG(D_INFO, "%d/%d %d\n", qin->pl_nr, qout->pl_nr, result);
212 return qout->pl_nr > 0 ? 0 : result;
216 * This is called when a page is accessed within file in a way that creates
217 * new page, if one were missing (i.e., if there were a hole at that place in
218 * the file, or accessed page is beyond the current file size).
220 * Expand stripe KMS if necessary.
222 static void osc_page_touch_at(const struct lu_env *env,
223 struct cl_object *obj, pgoff_t idx, size_t to)
225 struct lov_oinfo *loi = cl2osc(obj)->oo_oinfo;
226 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
230 /* offset within stripe */
231 kms = cl_offset(obj, idx) + to;
233 cl_object_attr_lock(obj);
237 * ll_inode_size_lock(inode, 0); lov_stripe_lock(lsm);
241 CDEBUG(D_INODE, "stripe KMS %sincreasing %llu->%llu %llu\n",
242 kms > loi->loi_kms ? "" : "not ", loi->loi_kms, kms,
243 loi->loi_lvb.lvb_size);
245 attr->cat_mtime = attr->cat_ctime = ktime_get_real_seconds();
246 valid = CAT_MTIME | CAT_CTIME;
247 if (kms > loi->loi_kms) {
251 if (kms > loi->loi_lvb.lvb_size) {
252 attr->cat_size = kms;
255 cl_object_attr_update(env, obj, attr, valid);
256 cl_object_attr_unlock(obj);
259 static int osc_io_commit_async(const struct lu_env *env,
260 const struct cl_io_slice *ios,
261 struct cl_page_list *qin, int from, int to,
264 struct cl_io *io = ios->cis_io;
265 struct osc_io *oio = cl2osc_io(env, ios);
266 struct osc_object *osc = cl2osc(ios->cis_obj);
267 struct cl_page *page;
268 struct cl_page *last_page;
269 struct osc_page *opg;
273 LASSERT(qin->pl_nr > 0);
275 /* Handle partial page cases */
276 last_page = cl_page_list_last(qin);
277 if (oio->oi_lockless) {
278 page = cl_page_list_first(qin);
279 if (page == last_page) {
280 cl_page_clip(env, page, from, to);
283 cl_page_clip(env, page, from, PAGE_SIZE);
285 cl_page_clip(env, last_page, 0, to);
289 while (qin->pl_nr > 0) {
290 struct osc_async_page *oap;
292 page = cl_page_list_first(qin);
293 opg = osc_cl_page_osc(page, osc);
296 if (!list_empty(&oap->oap_rpc_item)) {
297 CDEBUG(D_CACHE, "Busy oap %p page %p for submit.\n",
303 /* The page may be already in dirty cache. */
304 if (list_empty(&oap->oap_pending_item)) {
305 result = osc_page_cache_add(env, &opg->ops_cl, io);
310 osc_page_touch_at(env, osc2cl(osc), osc_index(opg),
311 page == last_page ? to : PAGE_SIZE);
313 cl_page_list_del(env, qin, page);
315 (*cb)(env, io, page);
316 /* Can't access page any more. Page can be in transfer and
317 * complete at any time. */
320 /* for sync write, kernel will wait for this page to be flushed before
321 * osc_io_end() is called, so release it earlier.
322 * for mkwrite(), it's known there is no further pages. */
323 if (cl_io_is_sync_write(io) && oio->oi_active != NULL) {
324 osc_extent_release(env, oio->oi_active);
325 oio->oi_active = NULL;
328 CDEBUG(D_INFO, "%d %d\n", qin->pl_nr, result);
332 static int osc_io_iter_init(const struct lu_env *env,
333 const struct cl_io_slice *ios)
335 struct osc_object *osc = cl2osc(ios->cis_obj);
336 struct obd_import *imp = osc_cli(osc)->cl_import;
339 spin_lock(&imp->imp_lock);
340 if (likely(!imp->imp_invalid)) {
341 struct osc_io *oio = osc_env_io(env);
343 atomic_inc(&osc->oo_nr_ios);
344 oio->oi_is_active = 1;
347 spin_unlock(&imp->imp_lock);
352 static int osc_io_write_iter_init(const struct lu_env *env,
353 const struct cl_io_slice *ios)
355 struct cl_io *io = ios->cis_io;
356 struct osc_io *oio = osc_env_io(env);
357 struct osc_object *osc = cl2osc(ios->cis_obj);
358 unsigned long npages;
361 if (cl_io_is_append(io))
362 RETURN(osc_io_iter_init(env, ios));
364 npages = io->u.ci_rw.rw_range.cir_count >> PAGE_SHIFT;
365 if (io->u.ci_rw.rw_range.cir_pos & ~PAGE_MASK)
368 oio->oi_lru_reserved = osc_lru_reserve(osc_cli(osc), npages);
370 RETURN(osc_io_iter_init(env, ios));
373 static void osc_io_iter_fini(const struct lu_env *env,
374 const struct cl_io_slice *ios)
376 struct osc_io *oio = osc_env_io(env);
378 if (oio->oi_is_active) {
379 struct osc_object *osc = cl2osc(ios->cis_obj);
381 oio->oi_is_active = 0;
382 LASSERT(atomic_read(&osc->oo_nr_ios) > 0);
383 if (atomic_dec_and_test(&osc->oo_nr_ios))
384 wake_up_all(&osc->oo_io_waitq);
388 static void osc_io_write_iter_fini(const struct lu_env *env,
389 const struct cl_io_slice *ios)
391 struct osc_io *oio = osc_env_io(env);
392 struct osc_object *osc = cl2osc(ios->cis_obj);
394 if (oio->oi_lru_reserved > 0) {
395 osc_lru_unreserve(osc_cli(osc), oio->oi_lru_reserved);
396 oio->oi_lru_reserved = 0;
398 oio->oi_write_osclock = NULL;
400 osc_io_iter_fini(env, ios);
403 static int osc_io_fault_start(const struct lu_env *env,
404 const struct cl_io_slice *ios)
407 struct cl_fault_io *fio;
411 fio = &io->u.ci_fault;
412 CDEBUG(D_INFO, "%lu %d %zu\n",
413 fio->ft_index, fio->ft_writable, fio->ft_nob);
415 * If mapping is writeable, adjust kms to cover this page,
416 * but do not extend kms beyond actual file size.
419 if (fio->ft_writable)
420 osc_page_touch_at(env, ios->cis_obj,
421 fio->ft_index, fio->ft_nob);
425 static int osc_async_upcall(void *a, int rc)
427 struct osc_async_cbargs *args = a;
430 complete(&args->opc_sync);
435 * Checks that there are no pages being written in the extent being truncated.
437 static int trunc_check_cb(const struct lu_env *env, struct cl_io *io,
438 struct osc_page *ops , void *cbdata)
440 struct cl_page *page = ops->ops_cl.cpl_page;
441 struct osc_async_page *oap;
442 __u64 start = *(__u64 *)cbdata;
445 if (oap->oap_cmd & OBD_BRW_WRITE &&
446 !list_empty(&oap->oap_pending_item))
447 CL_PAGE_DEBUG(D_ERROR, env, page, "exists %llu/%s.\n",
448 start, current->comm);
450 if (PageLocked(page->cp_vmpage))
451 CDEBUG(D_CACHE, "page %p index %lu locked for %d.\n",
452 ops, osc_index(ops), oap->oap_cmd & OBD_BRW_RWMASK);
454 return CLP_GANG_OKAY;
457 static void osc_trunc_check(const struct lu_env *env, struct cl_io *io,
458 struct osc_io *oio, __u64 size)
460 struct cl_object *clob;
464 clob = oio->oi_cl.cis_obj;
465 start = cl_index(clob, size);
466 partial = cl_offset(clob, start) < size;
469 * Complain if there are pages in the truncated region.
471 osc_page_gang_lookup(env, io, cl2osc(clob),
472 start + partial, CL_PAGE_EOF,
473 trunc_check_cb, (void *)&size);
476 static int osc_io_setattr_start(const struct lu_env *env,
477 const struct cl_io_slice *slice)
479 struct cl_io *io = slice->cis_io;
480 struct osc_io *oio = cl2osc_io(env, slice);
481 struct cl_object *obj = slice->cis_obj;
482 struct lov_oinfo *loi = cl2osc(obj)->oo_oinfo;
483 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
484 struct obdo *oa = &oio->oi_oa;
485 struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
486 __u64 size = io->u.ci_setattr.sa_attr.lvb_size;
487 unsigned int ia_valid = io->u.ci_setattr.sa_valid;
491 /* truncate cache dirty pages first */
492 if (cl_io_is_trunc(io))
493 result = osc_cache_truncate_start(env, cl2osc(obj), size,
496 if (result == 0 && oio->oi_lockless == 0) {
497 cl_object_attr_lock(obj);
498 result = cl_object_attr_get(env, obj, attr);
500 struct ost_lvb *lvb = &io->u.ci_setattr.sa_attr;
501 unsigned int cl_valid = 0;
503 if (ia_valid & ATTR_SIZE) {
504 attr->cat_size = attr->cat_kms = size;
505 cl_valid = (CAT_SIZE | CAT_KMS);
507 if (ia_valid & ATTR_MTIME_SET) {
508 attr->cat_mtime = lvb->lvb_mtime;
509 cl_valid |= CAT_MTIME;
511 if (ia_valid & ATTR_ATIME_SET) {
512 attr->cat_atime = lvb->lvb_atime;
513 cl_valid |= CAT_ATIME;
515 if (ia_valid & ATTR_CTIME_SET) {
516 attr->cat_ctime = lvb->lvb_ctime;
517 cl_valid |= CAT_CTIME;
519 result = cl_object_attr_update(env, obj, attr,
522 cl_object_attr_unlock(obj);
524 memset(oa, 0, sizeof(*oa));
526 oa->o_oi = loi->loi_oi;
527 obdo_set_parent_fid(oa, io->u.ci_setattr.sa_parent_fid);
528 oa->o_stripe_idx = io->u.ci_setattr.sa_stripe_index;
529 oa->o_layout = io->u.ci_setattr.sa_layout;
530 oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP |
532 if (ia_valid & ATTR_CTIME) {
533 oa->o_valid |= OBD_MD_FLCTIME;
534 oa->o_ctime = attr->cat_ctime;
536 if (ia_valid & ATTR_ATIME) {
537 oa->o_valid |= OBD_MD_FLATIME;
538 oa->o_atime = attr->cat_atime;
540 if (ia_valid & ATTR_MTIME) {
541 oa->o_valid |= OBD_MD_FLMTIME;
542 oa->o_mtime = attr->cat_mtime;
544 if (ia_valid & ATTR_SIZE) {
546 oa->o_blocks = OBD_OBJECT_EOF;
547 oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
549 if (oio->oi_lockless) {
550 oa->o_flags = OBD_FL_SRVLOCK;
551 oa->o_valid |= OBD_MD_FLFLAGS;
554 LASSERT(oio->oi_lockless == 0);
557 if (ia_valid & ATTR_ATTR_FLAG) {
558 oa->o_flags = io->u.ci_setattr.sa_attr_flags;
559 oa->o_valid |= OBD_MD_FLFLAGS;
562 init_completion(&cbargs->opc_sync);
564 if (ia_valid & ATTR_SIZE)
565 result = osc_punch_base(osc_export(cl2osc(obj)),
566 oa, osc_async_upcall,
567 cbargs, PTLRPCD_SET);
569 result = osc_setattr_async(osc_export(cl2osc(obj)),
570 oa, osc_async_upcall,
571 cbargs, PTLRPCD_SET);
573 cbargs->opc_rpc_sent = result == 0;
579 static void osc_io_setattr_end(const struct lu_env *env,
580 const struct cl_io_slice *slice)
582 struct cl_io *io = slice->cis_io;
583 struct osc_io *oio = cl2osc_io(env, slice);
584 struct cl_object *obj = slice->cis_obj;
585 struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
588 if (cbargs->opc_rpc_sent) {
589 wait_for_completion(&cbargs->opc_sync);
590 result = io->ci_result = cbargs->opc_rc;
593 if (oio->oi_lockless) {
594 /* lockless truncate */
595 struct osc_device *osd = lu2osc_dev(obj->co_lu.lo_dev);
597 LASSERT(cl_io_is_trunc(io));
598 /* XXX: Need a lock. */
599 osd->od_stats.os_lockless_truncates++;
603 if (cl_io_is_trunc(io)) {
604 __u64 size = io->u.ci_setattr.sa_attr.lvb_size;
605 osc_trunc_check(env, io, oio, size);
606 osc_cache_truncate_end(env, oio->oi_trunc);
607 oio->oi_trunc = NULL;
611 struct osc_data_version_args {
612 struct osc_io *dva_oio;
616 osc_data_version_interpret(const struct lu_env *env, struct ptlrpc_request *req,
619 struct osc_data_version_args *dva = arg;
620 struct osc_io *oio = dva->dva_oio;
621 const struct ost_body *body;
627 body = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
629 GOTO(out, rc = -EPROTO);
631 lustre_get_wire_obdo(&req->rq_import->imp_connect_data, &oio->oi_oa,
635 oio->oi_cbarg.opc_rc = rc;
636 complete(&oio->oi_cbarg.opc_sync);
641 static int osc_io_data_version_start(const struct lu_env *env,
642 const struct cl_io_slice *slice)
644 struct cl_data_version_io *dv = &slice->cis_io->u.ci_data_version;
645 struct osc_io *oio = cl2osc_io(env, slice);
646 struct obdo *oa = &oio->oi_oa;
647 struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
648 struct osc_object *obj = cl2osc(slice->cis_obj);
649 struct lov_oinfo *loi = obj->oo_oinfo;
650 struct obd_export *exp = osc_export(obj);
651 struct ptlrpc_request *req;
652 struct ost_body *body;
653 struct osc_data_version_args *dva;
657 memset(oa, 0, sizeof(*oa));
658 oa->o_oi = loi->loi_oi;
659 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
661 if (dv->dv_flags & (LL_DV_RD_FLUSH | LL_DV_WR_FLUSH)) {
662 oa->o_valid |= OBD_MD_FLFLAGS;
663 oa->o_flags |= OBD_FL_SRVLOCK;
664 if (dv->dv_flags & LL_DV_WR_FLUSH)
665 oa->o_flags |= OBD_FL_FLUSH;
668 init_completion(&cbargs->opc_sync);
670 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_OST_GETATTR);
674 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GETATTR);
676 ptlrpc_request_free(req);
680 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
681 lustre_set_wire_obdo(&req->rq_import->imp_connect_data, &body->oa, oa);
683 ptlrpc_request_set_replen(req);
684 req->rq_interpret_reply = osc_data_version_interpret;
685 CLASSERT(sizeof(*dva) <= sizeof(req->rq_async_args));
686 dva = ptlrpc_req_async_args(req);
689 ptlrpcd_add_req(req);
694 static void osc_io_data_version_end(const struct lu_env *env,
695 const struct cl_io_slice *slice)
697 struct cl_data_version_io *dv = &slice->cis_io->u.ci_data_version;
698 struct osc_io *oio = cl2osc_io(env, slice);
699 struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
702 wait_for_completion(&cbargs->opc_sync);
704 if (cbargs->opc_rc != 0) {
705 slice->cis_io->ci_result = cbargs->opc_rc;
706 } else if (!(oio->oi_oa.o_valid & OBD_MD_FLDATAVERSION)) {
707 slice->cis_io->ci_result = -EOPNOTSUPP;
709 dv->dv_data_version = oio->oi_oa.o_data_version;
710 slice->cis_io->ci_result = 0;
716 static int osc_io_read_start(const struct lu_env *env,
717 const struct cl_io_slice *slice)
719 struct cl_object *obj = slice->cis_obj;
720 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
724 if (!slice->cis_io->ci_noatime) {
725 cl_object_attr_lock(obj);
726 attr->cat_atime = ktime_get_real_seconds();
727 rc = cl_object_attr_update(env, obj, attr, CAT_ATIME);
728 cl_object_attr_unlock(obj);
734 static int osc_io_write_start(const struct lu_env *env,
735 const struct cl_io_slice *slice)
737 struct cl_object *obj = slice->cis_obj;
738 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
742 OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_DELAY_SETTIME, 1);
743 cl_object_attr_lock(obj);
744 attr->cat_mtime = attr->cat_ctime = ktime_get_real_seconds();
745 rc = cl_object_attr_update(env, obj, attr, CAT_MTIME | CAT_CTIME);
746 cl_object_attr_unlock(obj);
751 static int osc_fsync_ost(const struct lu_env *env, struct osc_object *obj,
752 struct cl_fsync_io *fio)
754 struct osc_io *oio = osc_env_io(env);
755 struct obdo *oa = &oio->oi_oa;
756 struct lov_oinfo *loi = obj->oo_oinfo;
757 struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
761 memset(oa, 0, sizeof(*oa));
762 oa->o_oi = loi->loi_oi;
763 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
765 /* reload size abd blocks for start and end of sync range */
766 oa->o_size = fio->fi_start;
767 oa->o_blocks = fio->fi_end;
768 oa->o_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
770 obdo_set_parent_fid(oa, fio->fi_fid);
772 init_completion(&cbargs->opc_sync);
774 rc = osc_sync_base(obj, oa, osc_async_upcall, cbargs, PTLRPCD_SET);
778 static int osc_io_fsync_start(const struct lu_env *env,
779 const struct cl_io_slice *slice)
781 struct cl_io *io = slice->cis_io;
782 struct cl_fsync_io *fio = &io->u.ci_fsync;
783 struct cl_object *obj = slice->cis_obj;
784 struct osc_object *osc = cl2osc(obj);
785 pgoff_t start = cl_index(obj, fio->fi_start);
786 pgoff_t end = cl_index(obj, fio->fi_end);
790 if (fio->fi_end == OBD_OBJECT_EOF)
793 result = osc_cache_writeback_range(env, osc, start, end, 0,
794 fio->fi_mode == CL_FSYNC_DISCARD);
796 fio->fi_nr_written += result;
799 if (fio->fi_mode == CL_FSYNC_ALL) {
802 /* we have to wait for writeback to finish before we can
803 * send OST_SYNC RPC. This is bad because it causes extents
804 * to be written osc by osc. However, we usually start
805 * writeback before CL_FSYNC_ALL so this won't have any real
807 rc = osc_cache_wait_range(env, osc, start, end);
810 rc = osc_fsync_ost(env, osc, fio);
818 static void osc_io_fsync_end(const struct lu_env *env,
819 const struct cl_io_slice *slice)
821 struct cl_fsync_io *fio = &slice->cis_io->u.ci_fsync;
822 struct cl_object *obj = slice->cis_obj;
823 pgoff_t start = cl_index(obj, fio->fi_start);
824 pgoff_t end = cl_index(obj, fio->fi_end);
827 if (fio->fi_mode == CL_FSYNC_LOCAL) {
828 result = osc_cache_wait_range(env, cl2osc(obj), start, end);
829 } else if (fio->fi_mode == CL_FSYNC_ALL) {
830 struct osc_io *oio = cl2osc_io(env, slice);
831 struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
833 wait_for_completion(&cbargs->opc_sync);
835 result = cbargs->opc_rc;
837 slice->cis_io->ci_result = result;
840 static int osc_io_ladvise_start(const struct lu_env *env,
841 const struct cl_io_slice *slice)
844 struct cl_io *io = slice->cis_io;
845 struct osc_io *oio = cl2osc_io(env, slice);
846 struct cl_object *obj = slice->cis_obj;
847 struct lov_oinfo *loi = cl2osc(obj)->oo_oinfo;
848 struct cl_ladvise_io *lio = &io->u.ci_ladvise;
849 struct obdo *oa = &oio->oi_oa;
850 struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
851 struct lu_ladvise *ladvise;
852 struct ladvise_hdr *ladvise_hdr;
857 /* TODO: add multiple ladvise support in CLIO */
858 buf_size = offsetof(typeof(*ladvise_hdr), lah_advise[num_advise]);
859 if (osc_env_info(env)->oti_ladvise_buf.lb_len < buf_size)
860 lu_buf_realloc(&osc_env_info(env)->oti_ladvise_buf, buf_size);
862 ladvise_hdr = osc_env_info(env)->oti_ladvise_buf.lb_buf;
863 if (ladvise_hdr == NULL)
866 memset(ladvise_hdr, 0, buf_size);
867 ladvise_hdr->lah_magic = LADVISE_MAGIC;
868 ladvise_hdr->lah_count = num_advise;
869 ladvise_hdr->lah_flags = lio->li_flags;
871 memset(oa, 0, sizeof(*oa));
872 oa->o_oi = loi->loi_oi;
873 oa->o_valid = OBD_MD_FLID;
874 obdo_set_parent_fid(oa, lio->li_fid);
876 ladvise = ladvise_hdr->lah_advise;
877 ladvise->lla_start = lio->li_start;
878 ladvise->lla_end = lio->li_end;
879 ladvise->lla_advice = lio->li_advice;
881 if (lio->li_flags & LF_ASYNC) {
882 result = osc_ladvise_base(osc_export(cl2osc(obj)), oa,
883 ladvise_hdr, NULL, NULL, NULL);
885 init_completion(&cbargs->opc_sync);
886 result = osc_ladvise_base(osc_export(cl2osc(obj)), oa,
887 ladvise_hdr, osc_async_upcall,
888 cbargs, PTLRPCD_SET);
889 cbargs->opc_rpc_sent = result == 0;
894 static void osc_io_ladvise_end(const struct lu_env *env,
895 const struct cl_io_slice *slice)
897 struct cl_io *io = slice->cis_io;
898 struct osc_io *oio = cl2osc_io(env, slice);
899 struct osc_async_cbargs *cbargs = &oio->oi_cbarg;
901 struct cl_ladvise_io *lio = &io->u.ci_ladvise;
903 if ((!(lio->li_flags & LF_ASYNC)) && cbargs->opc_rpc_sent) {
904 wait_for_completion(&cbargs->opc_sync);
905 result = cbargs->opc_rc;
907 slice->cis_io->ci_result = result;
910 static void osc_io_end(const struct lu_env *env,
911 const struct cl_io_slice *slice)
913 struct osc_io *oio = cl2osc_io(env, slice);
915 if (oio->oi_active) {
916 osc_extent_release(env, oio->oi_active);
917 oio->oi_active = NULL;
921 static const struct cl_io_operations osc_io_ops = {
924 .cio_iter_init = osc_io_iter_init,
925 .cio_iter_fini = osc_io_iter_fini,
926 .cio_start = osc_io_read_start,
927 .cio_fini = osc_io_fini
930 .cio_iter_init = osc_io_write_iter_init,
931 .cio_iter_fini = osc_io_write_iter_fini,
932 .cio_start = osc_io_write_start,
933 .cio_end = osc_io_end,
934 .cio_fini = osc_io_fini
937 .cio_iter_init = osc_io_iter_init,
938 .cio_iter_fini = osc_io_iter_fini,
939 .cio_start = osc_io_setattr_start,
940 .cio_end = osc_io_setattr_end
942 [CIT_DATA_VERSION] = {
943 .cio_start = osc_io_data_version_start,
944 .cio_end = osc_io_data_version_end,
947 .cio_iter_init = osc_io_iter_init,
948 .cio_iter_fini = osc_io_iter_fini,
949 .cio_start = osc_io_fault_start,
950 .cio_end = osc_io_end,
951 .cio_fini = osc_io_fini
954 .cio_start = osc_io_fsync_start,
955 .cio_end = osc_io_fsync_end,
956 .cio_fini = osc_io_fini
959 .cio_start = osc_io_ladvise_start,
960 .cio_end = osc_io_ladvise_end,
961 .cio_fini = osc_io_fini
964 .cio_fini = osc_io_fini
967 .cio_read_ahead = osc_io_read_ahead,
968 .cio_submit = osc_io_submit,
969 .cio_commit_async = osc_io_commit_async
972 /*****************************************************************************
974 * Transfer operations.
978 int osc_io_init(const struct lu_env *env,
979 struct cl_object *obj, struct cl_io *io)
981 struct osc_io *oio = osc_env_io(env);
983 CL_IO_SLICE_CLEAN(oio, oi_cl);
984 cl_io_slice_add(io, &oio->oi_cl, obj, &osc_io_ops);