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
39 #include <lustre_osc.h>
41 #include "osc_internal.h"
47 /*****************************************************************************
53 static int osc_object_init(const struct lu_env *env, struct lu_object *obj,
54 const struct lu_object_conf *conf)
56 struct osc_object *osc = lu2osc(obj);
57 const struct cl_object_conf *cconf = lu2cl_conf(conf);
59 osc->oo_oinfo = cconf->u.coc_oinfo;
60 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
61 mutex_init(&osc->oo_debug_mutex);
63 INIT_LIST_HEAD(&osc->oo_ready_item);
64 INIT_LIST_HEAD(&osc->oo_hp_ready_item);
65 INIT_LIST_HEAD(&osc->oo_write_item);
66 INIT_LIST_HEAD(&osc->oo_read_item);
68 osc->oo_root.rb_node = NULL;
69 INIT_LIST_HEAD(&osc->oo_hp_exts);
70 INIT_LIST_HEAD(&osc->oo_urgent_exts);
71 INIT_LIST_HEAD(&osc->oo_full_exts);
72 INIT_LIST_HEAD(&osc->oo_reading_exts);
73 atomic_set(&osc->oo_nr_reads, 0);
74 atomic_set(&osc->oo_nr_writes, 0);
75 spin_lock_init(&osc->oo_lock);
76 spin_lock_init(&osc->oo_tree_lock);
77 spin_lock_init(&osc->oo_ol_spin);
78 INIT_LIST_HEAD(&osc->oo_ol_list);
80 atomic_set(&osc->oo_nr_ios, 0);
81 init_waitqueue_head(&osc->oo_io_waitq);
83 cl_object_page_init(lu2cl(obj), sizeof(struct osc_page));
88 static void osc_object_free(const struct lu_env *env, struct lu_object *obj)
90 struct osc_object *osc = lu2osc(obj);
92 LASSERT(list_empty(&osc->oo_ready_item));
93 LASSERT(list_empty(&osc->oo_hp_ready_item));
94 LASSERT(list_empty(&osc->oo_write_item));
95 LASSERT(list_empty(&osc->oo_read_item));
97 LASSERT(osc->oo_root.rb_node == NULL);
98 LASSERT(list_empty(&osc->oo_hp_exts));
99 LASSERT(list_empty(&osc->oo_urgent_exts));
100 LASSERT(list_empty(&osc->oo_full_exts));
101 LASSERT(list_empty(&osc->oo_reading_exts));
102 LASSERT(atomic_read(&osc->oo_nr_reads) == 0);
103 LASSERT(atomic_read(&osc->oo_nr_writes) == 0);
104 LASSERT(list_empty(&osc->oo_ol_list));
105 LASSERT(atomic_read(&osc->oo_nr_ios) == 0);
108 OBD_SLAB_FREE_PTR(osc, osc_object_kmem);
111 int osc_lvb_print(const struct lu_env *env, void *cookie,
112 lu_printer_t p, const struct ost_lvb *lvb)
114 return (*p)(env, cookie, "size: %llu mtime: %llu atime: %llu "
115 "ctime: %llu blocks: %llu",
116 lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
117 lvb->lvb_ctime, lvb->lvb_blocks);
120 static int osc_object_print(const struct lu_env *env, void *cookie,
121 lu_printer_t p, const struct lu_object *obj)
123 struct osc_object *osc = lu2osc(obj);
124 struct lov_oinfo *oinfo = osc->oo_oinfo;
125 struct osc_async_rc *ar = &oinfo->loi_ar;
127 (*p)(env, cookie, "id: "DOSTID" "
128 "idx: %d gen: %d kms_valid: %u kms %llu "
129 "rc: %d force_sync: %d min_xid: %llu ",
130 POSTID(&oinfo->loi_oi), oinfo->loi_ost_idx,
131 oinfo->loi_ost_gen, oinfo->loi_kms_valid, oinfo->loi_kms,
132 ar->ar_rc, ar->ar_force_sync, ar->ar_min_xid);
133 osc_lvb_print(env, cookie, p, &oinfo->loi_lvb);
138 static int osc_attr_get(const struct lu_env *env, struct cl_object *obj,
139 struct cl_attr *attr)
141 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
143 cl_lvb2attr(attr, &oinfo->loi_lvb);
144 attr->cat_kms = oinfo->loi_kms_valid ? oinfo->loi_kms : 0;
148 static int osc_attr_update(const struct lu_env *env, struct cl_object *obj,
149 const struct cl_attr *attr, unsigned valid)
151 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
152 struct ost_lvb *lvb = &oinfo->loi_lvb;
154 if (valid & CAT_SIZE)
155 lvb->lvb_size = attr->cat_size;
156 if (valid & CAT_MTIME)
157 lvb->lvb_mtime = attr->cat_mtime;
158 if (valid & CAT_ATIME)
159 lvb->lvb_atime = attr->cat_atime;
160 if (valid & CAT_CTIME)
161 lvb->lvb_ctime = attr->cat_ctime;
162 if (valid & CAT_BLOCKS)
163 lvb->lvb_blocks = attr->cat_blocks;
164 if (valid & CAT_KMS) {
165 CDEBUG(D_CACHE, "set kms from %lluto %llu\n",
166 oinfo->loi_kms, (__u64)attr->cat_kms);
167 loi_kms_set(oinfo, attr->cat_kms);
172 static int osc_object_glimpse(const struct lu_env *env,
173 const struct cl_object *obj, struct ost_lvb *lvb)
175 struct lov_oinfo *oinfo = cl2osc(obj)->oo_oinfo;
178 lvb->lvb_size = oinfo->loi_kms;
179 lvb->lvb_blocks = oinfo->loi_lvb.lvb_blocks;
183 static int osc_object_ast_clear(struct ldlm_lock *lock, void *data)
187 if (lock->l_ast_data == data)
188 lock->l_ast_data = NULL;
189 RETURN(LDLM_ITER_CONTINUE);
192 static int osc_object_prune(const struct lu_env *env, struct cl_object *obj)
194 struct osc_object *osc = cl2osc(obj);
195 struct ldlm_res_id *resname = &osc_env_info(env)->oti_resname;
197 /* DLM locks don't hold a reference of osc_object so we have to
198 * clear it before the object is being destroyed. */
199 ostid_build_res_name(&osc->oo_oinfo->loi_oi, resname);
200 ldlm_resource_iterate(osc_export(osc)->exp_obd->obd_namespace, resname,
201 osc_object_ast_clear, osc);
205 static int osc_object_fiemap(const struct lu_env *env, struct cl_object *obj,
206 struct ll_fiemap_info_key *fmkey,
207 struct fiemap *fiemap, size_t *buflen)
209 struct obd_export *exp = osc_export(cl2osc(obj));
210 struct ldlm_res_id resid;
211 union ldlm_policy_data policy;
212 struct lustre_handle lockh;
213 enum ldlm_mode mode = LCK_MINMODE;
214 struct ptlrpc_request *req;
215 struct fiemap *reply;
220 fmkey->lfik_oa.o_oi = cl2osc(obj)->oo_oinfo->loi_oi;
221 if (!(fmkey->lfik_fiemap.fm_flags & FIEMAP_FLAG_SYNC))
224 policy.l_extent.start = fmkey->lfik_fiemap.fm_start & PAGE_MASK;
226 if (OBD_OBJECT_EOF - fmkey->lfik_fiemap.fm_length <=
227 fmkey->lfik_fiemap.fm_start + PAGE_SIZE - 1)
228 policy.l_extent.end = OBD_OBJECT_EOF;
230 policy.l_extent.end = (fmkey->lfik_fiemap.fm_start +
231 fmkey->lfik_fiemap.fm_length +
232 PAGE_SIZE - 1) & PAGE_MASK;
234 ostid_build_res_name(&fmkey->lfik_oa.o_oi, &resid);
235 mode = ldlm_lock_match(exp->exp_obd->obd_namespace,
236 LDLM_FL_BLOCK_GRANTED | LDLM_FL_LVB_READY,
237 &resid, LDLM_EXTENT, &policy,
238 LCK_PR | LCK_PW, &lockh, 0);
239 if (mode) { /* lock is cached on client */
240 if (mode != LCK_PR) {
241 ldlm_lock_addref(&lockh, LCK_PR);
242 ldlm_lock_decref(&lockh, LCK_PW);
244 } else { /* no cached lock, needs acquire lock on server side */
245 fmkey->lfik_oa.o_valid |= OBD_MD_FLFLAGS;
246 fmkey->lfik_oa.o_flags |= OBD_FL_SRVLOCK;
250 req = ptlrpc_request_alloc(class_exp2cliimp(exp),
251 &RQF_OST_GET_INFO_FIEMAP);
253 GOTO(drop_lock, rc = -ENOMEM);
255 req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_KEY, RCL_CLIENT,
257 req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_VAL, RCL_CLIENT,
259 req_capsule_set_size(&req->rq_pill, &RMF_FIEMAP_VAL, RCL_SERVER,
262 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, OST_GET_INFO);
264 ptlrpc_request_free(req);
267 tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_KEY);
268 memcpy(tmp, fmkey, sizeof(*fmkey));
269 tmp = req_capsule_client_get(&req->rq_pill, &RMF_FIEMAP_VAL);
270 memcpy(tmp, fiemap, *buflen);
271 ptlrpc_request_set_replen(req);
273 rc = ptlrpc_queue_wait(req);
277 reply = req_capsule_server_get(&req->rq_pill, &RMF_FIEMAP_VAL);
279 GOTO(fini_req, rc = -EPROTO);
281 memcpy(fiemap, reply, *buflen);
283 ptlrpc_req_finished(req);
286 ldlm_lock_decref(&lockh, LCK_PR);
290 void osc_object_set_contended(struct osc_object *obj)
292 obj->oo_contention_time = cfs_time_current();
294 obj->oo_contended = 1;
297 void osc_object_clear_contended(struct osc_object *obj)
299 obj->oo_contended = 0;
302 int osc_object_is_contended(struct osc_object *obj)
304 struct osc_device *dev = lu2osc_dev(obj->oo_cl.co_lu.lo_dev);
305 int osc_contention_time = dev->od_contention_time;
306 cfs_time_t cur_time = cfs_time_current();
307 cfs_time_t retry_time;
309 if (OBD_FAIL_CHECK(OBD_FAIL_OSC_OBJECT_CONTENTION))
312 if (!obj->oo_contended)
316 * I like copy-paste. the code is copied from
317 * ll_file_is_contended.
319 retry_time = cfs_time_add(obj->oo_contention_time,
320 cfs_time_seconds(osc_contention_time));
321 if (cfs_time_after(cur_time, retry_time)) {
322 osc_object_clear_contended(obj);
329 * Implementation of struct cl_object_operations::coo_req_attr_set() for osc
330 * layer. osc is responsible for struct obdo::o_id and struct obdo::o_seq
333 static void osc_req_attr_set(const struct lu_env *env, struct cl_object *obj,
334 struct cl_req_attr *attr)
336 struct lov_oinfo *oinfo;
339 u64 flags = attr->cra_flags;
341 oinfo = cl2osc(obj)->oo_oinfo;
342 lvb = &oinfo->loi_lvb;
345 if ((flags & OBD_MD_FLMTIME) != 0) {
346 oa->o_mtime = lvb->lvb_mtime;
347 oa->o_valid |= OBD_MD_FLMTIME;
349 if ((flags & OBD_MD_FLATIME) != 0) {
350 oa->o_atime = lvb->lvb_atime;
351 oa->o_valid |= OBD_MD_FLATIME;
353 if ((flags & OBD_MD_FLCTIME) != 0) {
354 oa->o_ctime = lvb->lvb_ctime;
355 oa->o_valid |= OBD_MD_FLCTIME;
357 if (flags & OBD_MD_FLGROUP) {
358 ostid_set_seq(&oa->o_oi, ostid_seq(&oinfo->loi_oi));
359 oa->o_valid |= OBD_MD_FLGROUP;
361 if (flags & OBD_MD_FLID) {
364 rc = ostid_set_id(&oa->o_oi, ostid_id(&oinfo->loi_oi));
366 CERROR("Bad %llu to set " DOSTID " : rc %d\n",
367 (unsigned long long)ostid_id(&oinfo->loi_oi),
368 POSTID(&oa->o_oi), rc);
370 oa->o_valid |= OBD_MD_FLID;
372 if (flags & OBD_MD_FLHANDLE) {
373 struct ldlm_lock *lock;
374 struct osc_page *opg;
376 opg = osc_cl_page_osc(attr->cra_page, cl2osc(obj));
377 lock = osc_dlmlock_at_pgoff(env, cl2osc(obj), osc_index(opg),
378 OSC_DAP_FL_TEST_LOCK | OSC_DAP_FL_CANCELING);
379 if (lock == NULL && !opg->ops_srvlock) {
380 struct ldlm_resource *res;
381 struct ldlm_res_id *resname;
383 CL_PAGE_DEBUG(D_ERROR, env, attr->cra_page,
384 "uncovered page!\n");
386 resname = &osc_env_info(env)->oti_resname;
387 ostid_build_res_name(&oinfo->loi_oi, resname);
388 res = ldlm_resource_get(
389 osc_export(cl2osc(obj))->exp_obd->obd_namespace,
390 NULL, resname, LDLM_EXTENT, 0);
391 ldlm_resource_dump(D_ERROR, res);
393 libcfs_debug_dumpstack(NULL);
397 /* check for lockless io. */
399 oa->o_handle = lock->l_remote_handle;
400 oa->o_valid |= OBD_MD_FLHANDLE;
406 static const struct cl_object_operations osc_ops = {
407 .coo_page_init = osc_page_init,
408 .coo_lock_init = osc_lock_init,
409 .coo_io_init = osc_io_init,
410 .coo_attr_get = osc_attr_get,
411 .coo_attr_update = osc_attr_update,
412 .coo_glimpse = osc_object_glimpse,
413 .coo_prune = osc_object_prune,
414 .coo_fiemap = osc_object_fiemap,
415 .coo_req_attr_set = osc_req_attr_set
418 static const struct lu_object_operations osc_lu_obj_ops = {
419 .loo_object_init = osc_object_init,
420 .loo_object_release = NULL,
421 .loo_object_free = osc_object_free,
422 .loo_object_print = osc_object_print,
423 .loo_object_invariant = NULL
426 struct lu_object *osc_object_alloc(const struct lu_env *env,
427 const struct lu_object_header *unused,
428 struct lu_device *dev)
430 struct osc_object *osc;
431 struct lu_object *obj;
433 OBD_SLAB_ALLOC_PTR_GFP(osc, osc_object_kmem, GFP_NOFS);
436 lu_object_init(obj, NULL, dev);
437 osc->oo_cl.co_ops = &osc_ops;
438 obj->lo_ops = &osc_lu_obj_ops;
444 int osc_object_invalidate(const struct lu_env *env, struct osc_object *osc)
446 struct l_wait_info lwi = { 0 };
449 CDEBUG(D_INODE, "Invalidate osc object: %p, # of active IOs: %d\n",
450 osc, atomic_read(&osc->oo_nr_ios));
452 l_wait_event(osc->oo_io_waitq, atomic_read(&osc->oo_nr_ios) == 0, &lwi);
454 /* Discard all dirty pages of this object. */
455 osc_cache_truncate_start(env, osc, 0, NULL);
457 /* Discard all caching pages */
458 osc_lock_discard_pages(env, osc, 0, CL_PAGE_EOF, true);
460 /* Clear ast data of dlm lock. Do this after discarding all pages */
461 osc_object_prune(env, osc2cl(osc));