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, 2015, 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_object for OSC layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
38 #define DEBUG_SUBSYSTEM S_OSC
40 #include "osc_cl_internal.h"
46 /*****************************************************************************
52 static struct lu_object *osc2lu(struct osc_object *osc)
54 return &osc->oo_cl.co_lu;
57 static struct osc_object *lu2osc(const struct lu_object *obj)
59 LINVRNT(osc_is_object(obj));
60 return container_of0(obj, struct osc_object, oo_cl.co_lu);
63 /*****************************************************************************
69 static int osc_object_init(const struct lu_env *env, struct lu_object *obj,
70 const struct lu_object_conf *conf)
72 struct osc_object *osc = lu2osc(obj);
73 const struct cl_object_conf *cconf = lu2cl_conf(conf);
75 osc->oo_oinfo = cconf->u.coc_oinfo;
76 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
77 mutex_init(&osc->oo_debug_mutex);
79 INIT_LIST_HEAD(&osc->oo_ready_item);
80 INIT_LIST_HEAD(&osc->oo_hp_ready_item);
81 INIT_LIST_HEAD(&osc->oo_write_item);
82 INIT_LIST_HEAD(&osc->oo_read_item);
84 osc->oo_root.rb_node = NULL;
85 INIT_LIST_HEAD(&osc->oo_hp_exts);
86 INIT_LIST_HEAD(&osc->oo_urgent_exts);
87 INIT_LIST_HEAD(&osc->oo_full_exts);
88 INIT_LIST_HEAD(&osc->oo_reading_exts);
89 atomic_set(&osc->oo_nr_reads, 0);
90 atomic_set(&osc->oo_nr_writes, 0);
91 spin_lock_init(&osc->oo_lock);
92 spin_lock_init(&osc->oo_tree_lock);
93 spin_lock_init(&osc->oo_ol_spin);
94 INIT_LIST_HEAD(&osc->oo_ol_list);
96 atomic_set(&osc->oo_nr_ios, 0);
97 init_waitqueue_head(&osc->oo_io_waitq);
99 cl_object_page_init(lu2cl(obj), sizeof(struct osc_page));
104 static void osc_object_free(const struct lu_env *env, struct lu_object *obj)
106 struct osc_object *osc = lu2osc(obj);
108 LASSERT(list_empty(&osc->oo_ready_item));
109 LASSERT(list_empty(&osc->oo_hp_ready_item));
110 LASSERT(list_empty(&osc->oo_write_item));
111 LASSERT(list_empty(&osc->oo_read_item));
113 LASSERT(osc->oo_root.rb_node == NULL);
114 LASSERT(list_empty(&osc->oo_hp_exts));
115 LASSERT(list_empty(&osc->oo_urgent_exts));
116 LASSERT(list_empty(&osc->oo_full_exts));
117 LASSERT(list_empty(&osc->oo_reading_exts));
118 LASSERT(atomic_read(&osc->oo_nr_reads) == 0);
119 LASSERT(atomic_read(&osc->oo_nr_writes) == 0);
120 LASSERT(list_empty(&osc->oo_ol_list));
121 LASSERT(atomic_read(&osc->oo_nr_ios) == 0);
124 OBD_SLAB_FREE_PTR(osc, osc_object_kmem);
127 int osc_lvb_print(const struct lu_env *env, void *cookie,
128 lu_printer_t p, const struct ost_lvb *lvb)
130 return (*p)(env, cookie, "size: %llu mtime: %llu atime: %llu "
131 "ctime: %llu blocks: %llu",
132 lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
133 lvb->lvb_ctime, lvb->lvb_blocks);
136 static int osc_object_print(const struct lu_env *env, void *cookie,
137 lu_printer_t p, const struct lu_object *obj)
139 struct osc_object *osc = lu2osc(obj);
140 struct lov_oinfo *oinfo = osc->oo_oinfo;
141 struct osc_async_rc *ar = &oinfo->loi_ar;
143 (*p)(env, cookie, "id: "DOSTID" "
144 "idx: %d gen: %d kms_valid: %u kms %llu "
145 "rc: %d force_sync: %d min_xid: %llu ",
146 POSTID(&oinfo->loi_oi), oinfo->loi_ost_idx,
147 oinfo->loi_ost_gen, oinfo->loi_kms_valid, oinfo->loi_kms,
148 ar->ar_rc, ar->ar_force_sync, ar->ar_min_xid);
149 osc_lvb_print(env, cookie, p, &oinfo->loi_lvb);
154 static int osc_attr_get(const struct lu_env *env, struct cl_object *obj,
155 struct cl_attr *attr)
157 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
159 cl_lvb2attr(attr, &oinfo->loi_lvb);
160 attr->cat_kms = oinfo->loi_kms_valid ? oinfo->loi_kms : 0;
164 static int osc_attr_update(const struct lu_env *env, struct cl_object *obj,
165 const struct cl_attr *attr, unsigned valid)
167 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
168 struct ost_lvb *lvb = &oinfo->loi_lvb;
170 if (valid & CAT_SIZE)
171 lvb->lvb_size = attr->cat_size;
172 if (valid & CAT_MTIME)
173 lvb->lvb_mtime = attr->cat_mtime;
174 if (valid & CAT_ATIME)
175 lvb->lvb_atime = attr->cat_atime;
176 if (valid & CAT_CTIME)
177 lvb->lvb_ctime = attr->cat_ctime;
178 if (valid & CAT_BLOCKS)
179 lvb->lvb_blocks = attr->cat_blocks;
180 if (valid & CAT_KMS) {
181 CDEBUG(D_CACHE, "set kms from %lluto %llu\n",
182 oinfo->loi_kms, (__u64)attr->cat_kms);
183 loi_kms_set(oinfo, attr->cat_kms);
188 static int osc_object_glimpse(const struct lu_env *env,
189 const struct cl_object *obj, struct ost_lvb *lvb)
191 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
194 lvb->lvb_size = oinfo->loi_kms;
195 lvb->lvb_blocks = oinfo->loi_lvb.lvb_blocks;
199 static int osc_object_ast_clear(struct ldlm_lock *lock, void *data)
203 if (lock->l_ast_data == data)
204 lock->l_ast_data = NULL;
205 RETURN(LDLM_ITER_CONTINUE);
208 static int osc_object_prune(const struct lu_env *env, struct cl_object *obj)
210 struct osc_object *osc = cl2osc(obj);
211 struct ldlm_res_id *resname = &osc_env_info(env)->oti_resname;
213 /* DLM locks don't hold a reference of osc_object so we have to
214 * clear it before the object is being destroyed. */
215 ostid_build_res_name(&osc->oo_oinfo->loi_oi, resname);
216 ldlm_resource_iterate(osc_export(osc)->exp_obd->obd_namespace, resname,
217 osc_object_ast_clear, osc);
221 static int osc_object_fiemap(const struct lu_env *env, struct cl_object *obj,
222 struct ll_fiemap_info_key *fmkey,
223 struct fiemap *fiemap, size_t *buflen)
225 struct obd_export *exp = osc_export(cl2osc(obj));
226 struct ldlm_res_id resid;
227 union ldlm_policy_data policy;
228 struct lustre_handle lockh;
229 enum ldlm_mode mode = LCK_MINMODE;
230 struct ptlrpc_request *req;
231 struct fiemap *reply;
236 fmkey->lfik_oa.o_oi = cl2osc(obj)->oo_oinfo->loi_oi;
237 if (!(fmkey->lfik_fiemap.fm_flags & FIEMAP_FLAG_SYNC))
240 policy.l_extent.start = fmkey->lfik_fiemap.fm_start & PAGE_MASK;
242 if (OBD_OBJECT_EOF - fmkey->lfik_fiemap.fm_length <=
243 fmkey->lfik_fiemap.fm_start + PAGE_SIZE - 1)
244 policy.l_extent.end = OBD_OBJECT_EOF;
246 policy.l_extent.end = (fmkey->lfik_fiemap.fm_start +
247 fmkey->lfik_fiemap.fm_length +
248 PAGE_SIZE - 1) & PAGE_MASK;
250 ostid_build_res_name(&fmkey->lfik_oa.o_oi, &resid);
251 mode = ldlm_lock_match(exp->exp_obd->obd_namespace,
252 LDLM_FL_BLOCK_GRANTED | LDLM_FL_LVB_READY,
253 &resid, LDLM_EXTENT, &policy,
254 LCK_PR | LCK_PW, &lockh, 0);
255 if (mode) { /* lock is cached on client */
256 if (mode != LCK_PR) {
257 ldlm_lock_addref(&lockh, LCK_PR);
258 ldlm_lock_decref(&lockh, LCK_PW);
260 } else { /* no cached lock, needs acquire lock on server side */
261 fmkey->lfik_oa.o_valid |= OBD_MD_FLFLAGS;
262 fmkey->lfik_oa.o_flags |= OBD_FL_SRVLOCK;
266 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
267 &RQF_OST_GET_INFO_FIEMAP);
269 GOTO(drop_lock, rc = -ENOMEM);
271 req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_KEY, RCL_CLIENT,
273 req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_VAL, RCL_CLIENT,
275 req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_VAL, RCL_SERVER,
278 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
280 ptlrpc_request_free(req);
283 tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_KEY);
284 memcpy(tmp, fmkey, sizeof(*fmkey));
285 tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_VAL);
286 memcpy(tmp, fiemap, *buflen);
287 ptlrpc_request_set_replen(req);
289 rc = ptlrpc_queue_wait(req);
293 reply = req_capsule_server_get(&req->rq_pill, &RMF_FIEMAP_VAL);
295 GOTO(fini_req, rc = -EPROTO);
297 memcpy(fiemap, reply, *buflen);
299 ptlrpc_req_finished(req);
302 ldlm_lock_decref(&lockh, LCK_PR);
306 void osc_object_set_contended(struct osc_object *obj)
308 obj->oo_contention_time = cfs_time_current();
310 obj->oo_contended = 1;
313 void osc_object_clear_contended(struct osc_object *obj)
315 obj->oo_contended = 0;
318 int osc_object_is_contended(struct osc_object *obj)
320 struct osc_device *dev = lu2osc_dev(obj->oo_cl.co_lu.lo_dev);
321 int osc_contention_time = dev->od_contention_time;
322 cfs_time_t cur_time = cfs_time_current();
323 cfs_time_t retry_time;
325 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_OBJECT_CONTENTION))
328 if (!obj->oo_contended)
332 * I like copy-paste. the code is copied from
333 * ll_file_is_contended.
335 retry_time = cfs_time_add(obj->oo_contention_time,
336 cfs_time_seconds(osc_contention_time));
337 if (cfs_time_after(cur_time, retry_time)) {
338 osc_object_clear_contended(obj);
345 * Implementation of struct cl_object_operations::coo_req_attr_set() for osc
346 * layer. osc is responsible for struct obdo::o_id and struct obdo::o_seq
349 static void osc_req_attr_set(const struct lu_env *env, struct cl_object *obj,
350 struct cl_req_attr *attr)
352 struct lov_oinfo *oinfo;
355 u64 flags = attr->cra_flags;
357 oinfo = cl2osc(obj)->oo_oinfo;
358 lvb = &oinfo->loi_lvb;
361 if ((flags & OBD_MD_FLMTIME) != 0) {
362 oa->o_mtime = lvb->lvb_mtime;
363 oa->o_valid |= OBD_MD_FLMTIME;
365 if ((flags & OBD_MD_FLATIME) != 0) {
366 oa->o_atime = lvb->lvb_atime;
367 oa->o_valid |= OBD_MD_FLATIME;
369 if ((flags & OBD_MD_FLCTIME) != 0) {
370 oa->o_ctime = lvb->lvb_ctime;
371 oa->o_valid |= OBD_MD_FLCTIME;
373 if (flags & OBD_MD_FLGROUP) {
374 ostid_set_seq(&oa->o_oi, ostid_seq(&oinfo->loi_oi));
375 oa->o_valid |= OBD_MD_FLGROUP;
377 if (flags & OBD_MD_FLID) {
380 rc = ostid_set_id(&oa->o_oi, ostid_id(&oinfo->loi_oi));
382 CERROR("Bad %llu to set " DOSTID " : rc %d\n",
383 (unsigned long long)ostid_id(&oinfo->loi_oi),
384 POSTID(&oa->o_oi), rc);
386 oa->o_valid |= OBD_MD_FLID;
388 if (flags & OBD_MD_FLHANDLE) {
389 struct ldlm_lock *lock;
390 struct osc_page *opg;
392 opg = osc_cl_page_osc(attr->cra_page, cl2osc(obj));
393 lock = osc_dlmlock_at_pgoff(env, cl2osc(obj), osc_index(opg),
394 OSC_DAP_FL_TEST_LOCK | OSC_DAP_FL_CANCELING);
395 if (lock == NULL && !opg->ops_srvlock) {
396 struct ldlm_resource *res;
397 struct ldlm_res_id *resname;
399 CL_PAGE_DEBUG(D_ERROR, env, attr->cra_page,
400 "uncovered page!\n");
402 resname = &osc_env_info(env)->oti_resname;
403 ostid_build_res_name(&oinfo->loi_oi, resname);
404 res = ldlm_resource_get(
405 osc_export(cl2osc(obj))->exp_obd->obd_namespace,
406 NULL, resname, LDLM_EXTENT, 0);
407 ldlm_resource_dump(D_ERROR, res);
409 libcfs_debug_dumpstack(NULL);
413 /* check for lockless io. */
415 oa->o_handle = lock->l_remote_handle;
416 oa->o_valid |= OBD_MD_FLHANDLE;
422 static const struct cl_object_operations osc_ops = {
423 .coo_page_init = osc_page_init,
424 .coo_lock_init = osc_lock_init,
425 .coo_io_init = osc_io_init,
426 .coo_attr_get = osc_attr_get,
427 .coo_attr_update = osc_attr_update,
428 .coo_glimpse = osc_object_glimpse,
429 .coo_prune = osc_object_prune,
430 .coo_fiemap = osc_object_fiemap,
431 .coo_req_attr_set = osc_req_attr_set
434 static const struct lu_object_operations osc_lu_obj_ops = {
435 .loo_object_init = osc_object_init,
436 .loo_object_release = NULL,
437 .loo_object_free = osc_object_free,
438 .loo_object_print = osc_object_print,
439 .loo_object_invariant = NULL
442 struct lu_object *osc_object_alloc(const struct lu_env *env,
443 const struct lu_object_header *unused,
444 struct lu_device *dev)
446 struct osc_object *osc;
447 struct lu_object *obj;
449 OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, GFP_NOFS);
452 lu_object_init(obj, NULL, dev);
453 osc->oo_cl.co_ops = &osc_ops;
454 obj->lo_ops = &osc_lu_obj_ops;
460 int osc_object_invalidate(const struct lu_env *env, struct osc_object *osc)
462 struct l_wait_info lwi = { 0 };
465 CDEBUG(D_INODE, "Invalidate osc object: %p, # of active IOs: %d\n",
466 osc, atomic_read(&osc->oo_nr_ios));
468 l_wait_event(osc->oo_io_waitq, atomic_read(&osc->oo_nr_ios) == 0, &lwi);
470 /* Discard all dirty pages of this object. */
471 osc_cache_truncate_start(env, osc, 0, NULL);
473 /* Discard all caching pages */
474 osc_lock_discard_pages(env, osc, 0, CL_PAGE_EOF, CLM_WRITE);
476 /* Clear ast data of dlm lock. Do this after discarding all pages */
477 osc_object_prune(env, osc2cl(osc));