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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Implementation of cl_object for OSC layer.
40 * Author: Nikita Danilov <nikita.danilov@sun.com>
43 #define DEBUG_SUBSYSTEM S_OSC
45 #include "osc_cl_internal.h"
51 /*****************************************************************************
57 static struct lu_object *osc2lu(struct osc_object *osc)
59 return &osc->oo_cl.co_lu;
62 static struct osc_object *lu2osc(const struct lu_object *obj)
64 LINVRNT(osc_is_object(obj));
65 return container_of0(obj, struct osc_object, oo_cl.co_lu);
68 /*****************************************************************************
74 static int osc_object_init(const struct lu_env *env, struct lu_object *obj,
75 const struct lu_object_conf *conf)
77 struct osc_object *osc = lu2osc(obj);
78 const struct cl_object_conf *cconf = lu2cl_conf(conf);
81 osc->oo_oinfo = cconf->u.coc_oinfo;
82 #ifdef INVARIANT_CHECK
83 cfs_mutex_init(&osc->oo_debug_mutex);
85 cfs_spin_lock_init(&osc->oo_seatbelt);
86 for (i = 0; i < CRT_NR; ++i)
87 CFS_INIT_LIST_HEAD(&osc->oo_inflight[i]);
91 static void osc_object_free(const struct lu_env *env, struct lu_object *obj)
93 struct osc_object *osc = lu2osc(obj);
96 for (i = 0; i < CRT_NR; ++i)
97 LASSERT(cfs_list_empty(&osc->oo_inflight[i]));
100 OBD_SLAB_FREE_PTR(osc, osc_object_kmem);
103 int osc_lvb_print(const struct lu_env *env, void *cookie,
104 lu_printer_t p, const struct ost_lvb *lvb)
106 return (*p)(env, cookie, "size: "LPU64" mtime: "LPU64" atime: "LPU64" "
107 "ctime: "LPU64" blocks: "LPU64,
108 lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
109 lvb->lvb_ctime, lvb->lvb_blocks);
112 static int osc_object_print(const struct lu_env *env, void *cookie,
113 lu_printer_t p, const struct lu_object *obj)
115 struct osc_object *osc = lu2osc(obj);
116 struct lov_oinfo *oinfo = osc->oo_oinfo;
117 struct osc_async_rc *ar = &oinfo->loi_ar;
119 (*p)(env, cookie, "id: "LPU64" gr: "LPU64" "
120 "idx: %d gen: %d kms_valid: %u kms "LPU64" "
121 "rc: %d force_sync: %d min_xid: "LPU64" ",
122 oinfo->loi_id, oinfo->loi_seq, oinfo->loi_ost_idx,
123 oinfo->loi_ost_gen, oinfo->loi_kms_valid, oinfo->loi_kms,
124 ar->ar_rc, ar->ar_force_sync, ar->ar_min_xid);
125 osc_lvb_print(env, cookie, p, &oinfo->loi_lvb);
130 static int osc_attr_get(const struct lu_env *env, struct cl_object *obj,
131 struct cl_attr *attr)
133 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
135 cl_lvb2attr(attr, &oinfo->loi_lvb);
136 attr->cat_kms = oinfo->loi_kms_valid ? oinfo->loi_kms : 0;
140 int osc_attr_set(const struct lu_env *env, struct cl_object *obj,
141 const struct cl_attr *attr, unsigned valid)
143 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
144 struct ost_lvb *lvb = &oinfo->loi_lvb;
146 if (valid & CAT_SIZE)
147 lvb->lvb_size = attr->cat_size;
148 if (valid & CAT_MTIME)
149 lvb->lvb_mtime = attr->cat_mtime;
150 if (valid & CAT_ATIME)
151 lvb->lvb_atime = attr->cat_atime;
152 if (valid & CAT_CTIME)
153 lvb->lvb_ctime = attr->cat_ctime;
154 if (valid & CAT_BLOCKS)
155 lvb->lvb_blocks = attr->cat_blocks;
156 if (valid & CAT_KMS) {
157 CDEBUG(D_CACHE, "set kms from "LPU64"to "LPU64"\n",
158 oinfo->loi_kms, (__u64)attr->cat_kms);
159 loi_kms_set(oinfo, attr->cat_kms);
164 static int osc_object_glimpse(const struct lu_env *env,
165 const struct cl_object *obj, struct ost_lvb *lvb)
167 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
170 lvb->lvb_size = oinfo->loi_kms;
171 lvb->lvb_blocks = oinfo->loi_lvb.lvb_blocks;
176 void osc_object_set_contended(struct osc_object *obj)
178 obj->oo_contention_time = cfs_time_current();
180 obj->oo_contended = 1;
183 void osc_object_clear_contended(struct osc_object *obj)
185 obj->oo_contended = 0;
188 int osc_object_is_contended(struct osc_object *obj)
190 struct osc_device *dev = lu2osc_dev(obj->oo_cl.co_lu.lo_dev);
191 int osc_contention_time = dev->od_contention_time;
192 cfs_time_t cur_time = cfs_time_current();
193 cfs_time_t retry_time;
195 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_OBJECT_CONTENTION))
198 if (!obj->oo_contended)
202 * I like copy-paste. the code is copied from
203 * ll_file_is_contended.
205 retry_time = cfs_time_add(obj->oo_contention_time,
206 cfs_time_seconds(osc_contention_time));
207 if (cfs_time_after(cur_time, retry_time)) {
208 osc_object_clear_contended(obj);
214 static const struct cl_object_operations osc_ops = {
215 .coo_page_init = osc_page_init,
216 .coo_lock_init = osc_lock_init,
217 .coo_io_init = osc_io_init,
218 .coo_attr_get = osc_attr_get,
219 .coo_attr_set = osc_attr_set,
220 .coo_glimpse = osc_object_glimpse
223 static const struct lu_object_operations osc_lu_obj_ops = {
224 .loo_object_init = osc_object_init,
225 .loo_object_delete = NULL,
226 .loo_object_release = NULL,
227 .loo_object_free = osc_object_free,
228 .loo_object_print = osc_object_print,
229 .loo_object_invariant = NULL
232 struct lu_object *osc_object_alloc(const struct lu_env *env,
233 const struct lu_object_header *unused,
234 struct lu_device *dev)
236 struct osc_object *osc;
237 struct lu_object *obj;
239 OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, CFS_ALLOC_IO);
242 lu_object_init(obj, NULL, dev);
243 osc->oo_cl.co_ops = &osc_ops;
244 obj->lo_ops = &osc_lu_obj_ops;