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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Implementation of cl_object for OSC layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
39 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
42 #define DEBUG_SUBSYSTEM S_OSC
44 #include "osc_cl_internal.h"
50 /*****************************************************************************
56 static struct lu_object *osc2lu(struct osc_object *osc)
58 return &osc->oo_cl.co_lu;
61 static struct osc_object *lu2osc(const struct lu_object *obj)
63 LINVRNT(osc_is_object(obj));
64 return container_of0(obj, struct osc_object, oo_cl.co_lu);
67 /*****************************************************************************
73 static int osc_object_init(const struct lu_env *env, struct lu_object *obj,
74 const struct lu_object_conf *conf)
76 struct osc_object *osc = lu2osc(obj);
77 const struct cl_object_conf *cconf = lu2cl_conf(conf);
80 osc->oo_oinfo = cconf->u.coc_oinfo;
81 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
82 mutex_init(&osc->oo_debug_mutex);
84 spin_lock_init(&osc->oo_seatbelt);
85 for (i = 0; i < CRT_NR; ++i)
86 CFS_INIT_LIST_HEAD(&osc->oo_inflight[i]);
88 CFS_INIT_LIST_HEAD(&osc->oo_ready_item);
89 CFS_INIT_LIST_HEAD(&osc->oo_hp_ready_item);
90 CFS_INIT_LIST_HEAD(&osc->oo_write_item);
91 CFS_INIT_LIST_HEAD(&osc->oo_read_item);
93 osc->oo_root.rb_node = NULL;
94 CFS_INIT_LIST_HEAD(&osc->oo_hp_exts);
95 CFS_INIT_LIST_HEAD(&osc->oo_urgent_exts);
96 CFS_INIT_LIST_HEAD(&osc->oo_rpc_exts);
97 CFS_INIT_LIST_HEAD(&osc->oo_reading_exts);
98 atomic_set(&osc->oo_nr_reads, 0);
99 atomic_set(&osc->oo_nr_writes, 0);
100 spin_lock_init(&osc->oo_lock);
101 spin_lock_init(&osc->oo_tree_lock);
103 cl_object_page_init(lu2cl(obj), sizeof(struct osc_page));
108 static void osc_object_free(const struct lu_env *env, struct lu_object *obj)
110 struct osc_object *osc = lu2osc(obj);
113 for (i = 0; i < CRT_NR; ++i)
114 LASSERT(cfs_list_empty(&osc->oo_inflight[i]));
116 LASSERT(cfs_list_empty(&osc->oo_ready_item));
117 LASSERT(cfs_list_empty(&osc->oo_hp_ready_item));
118 LASSERT(cfs_list_empty(&osc->oo_write_item));
119 LASSERT(cfs_list_empty(&osc->oo_read_item));
121 LASSERT(osc->oo_root.rb_node == NULL);
122 LASSERT(cfs_list_empty(&osc->oo_hp_exts));
123 LASSERT(cfs_list_empty(&osc->oo_urgent_exts));
124 LASSERT(cfs_list_empty(&osc->oo_rpc_exts));
125 LASSERT(cfs_list_empty(&osc->oo_reading_exts));
126 LASSERT(atomic_read(&osc->oo_nr_reads) == 0);
127 LASSERT(atomic_read(&osc->oo_nr_writes) == 0);
130 OBD_SLAB_FREE_PTR(osc, osc_object_kmem);
133 int osc_lvb_print(const struct lu_env *env, void *cookie,
134 lu_printer_t p, const struct ost_lvb *lvb)
136 return (*p)(env, cookie, "size: "LPU64" mtime: "LPU64" atime: "LPU64" "
137 "ctime: "LPU64" blocks: "LPU64,
138 lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
139 lvb->lvb_ctime, lvb->lvb_blocks);
142 static int osc_object_print(const struct lu_env *env, void *cookie,
143 lu_printer_t p, const struct lu_object *obj)
145 struct osc_object *osc = lu2osc(obj);
146 struct lov_oinfo *oinfo = osc->oo_oinfo;
147 struct osc_async_rc *ar = &oinfo->loi_ar;
149 (*p)(env, cookie, "id: "DOSTID" "
150 "idx: %d gen: %d kms_valid: %u kms "LPU64" "
151 "rc: %d force_sync: %d min_xid: "LPU64" ",
152 POSTID(&oinfo->loi_oi), oinfo->loi_ost_idx,
153 oinfo->loi_ost_gen, oinfo->loi_kms_valid, oinfo->loi_kms,
154 ar->ar_rc, ar->ar_force_sync, ar->ar_min_xid);
155 osc_lvb_print(env, cookie, p, &oinfo->loi_lvb);
160 static int osc_attr_get(const struct lu_env *env, struct cl_object *obj,
161 struct cl_attr *attr)
163 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
165 cl_lvb2attr(attr, &oinfo->loi_lvb);
166 attr->cat_kms = oinfo->loi_kms_valid ? oinfo->loi_kms : 0;
170 int osc_attr_set(const struct lu_env *env, struct cl_object *obj,
171 const struct cl_attr *attr, unsigned valid)
173 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
174 struct ost_lvb *lvb = &oinfo->loi_lvb;
176 if (valid & CAT_SIZE)
177 lvb->lvb_size = attr->cat_size;
178 if (valid & CAT_MTIME)
179 lvb->lvb_mtime = attr->cat_mtime;
180 if (valid & CAT_ATIME)
181 lvb->lvb_atime = attr->cat_atime;
182 if (valid & CAT_CTIME)
183 lvb->lvb_ctime = attr->cat_ctime;
184 if (valid & CAT_BLOCKS)
185 lvb->lvb_blocks = attr->cat_blocks;
186 if (valid & CAT_KMS) {
187 CDEBUG(D_CACHE, "set kms from "LPU64"to "LPU64"\n",
188 oinfo->loi_kms, (__u64)attr->cat_kms);
189 loi_kms_set(oinfo, attr->cat_kms);
194 static int osc_object_glimpse(const struct lu_env *env,
195 const struct cl_object *obj, struct ost_lvb *lvb)
197 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
200 lvb->lvb_size = oinfo->loi_kms;
201 lvb->lvb_blocks = oinfo->loi_lvb.lvb_blocks;
206 void osc_object_set_contended(struct osc_object *obj)
208 obj->oo_contention_time = cfs_time_current();
210 obj->oo_contended = 1;
213 void osc_object_clear_contended(struct osc_object *obj)
215 obj->oo_contended = 0;
218 int osc_object_is_contended(struct osc_object *obj)
220 struct osc_device *dev = lu2osc_dev(obj->oo_cl.co_lu.lo_dev);
221 int osc_contention_time = dev->od_contention_time;
222 cfs_time_t cur_time = cfs_time_current();
223 cfs_time_t retry_time;
225 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_OBJECT_CONTENTION))
228 if (!obj->oo_contended)
232 * I like copy-paste. the code is copied from
233 * ll_file_is_contended.
235 retry_time = cfs_time_add(obj->oo_contention_time,
236 cfs_time_seconds(osc_contention_time));
237 if (cfs_time_after(cur_time, retry_time)) {
238 osc_object_clear_contended(obj);
244 static const struct cl_object_operations osc_ops = {
245 .coo_page_init = osc_page_init,
246 .coo_lock_init = osc_lock_init,
247 .coo_io_init = osc_io_init,
248 .coo_attr_get = osc_attr_get,
249 .coo_attr_set = osc_attr_set,
250 .coo_glimpse = osc_object_glimpse
253 static const struct lu_object_operations osc_lu_obj_ops = {
254 .loo_object_init = osc_object_init,
255 .loo_object_delete = NULL,
256 .loo_object_release = NULL,
257 .loo_object_free = osc_object_free,
258 .loo_object_print = osc_object_print,
259 .loo_object_invariant = NULL
262 struct lu_object *osc_object_alloc(const struct lu_env *env,
263 const struct lu_object_header *unused,
264 struct lu_device *dev)
266 struct osc_object *osc;
267 struct lu_object *obj;
269 OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, __GFP_IO);
272 lu_object_init(obj, NULL, dev);
273 osc->oo_cl.co_ops = &osc_ops;
274 obj->lo_ops = &osc_lu_obj_ops;