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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
41 # define EXPORT_SYMTAB
43 #define DEBUG_SUBSYSTEM S_LMV
45 #include <linux/slab.h>
46 #include <linux/module.h>
47 #include <linux/init.h>
48 #include <linux/slab.h>
49 #include <linux/pagemap.h>
50 #include <asm/div64.h>
51 #include <linux/seq_file.h>
53 #include <liblustre.h>
56 #include <obd_support.h>
57 #include <lustre/lustre_idl.h>
58 #include <lustre_lib.h>
59 #include <lustre_net.h>
60 #include <lustre_dlm.h>
61 #include <obd_class.h>
62 #include <lprocfs_status.h>
63 #include "lmv_internal.h"
65 extern cfs_mem_cache_t *lmv_object_cache;
66 extern cfs_atomic_t lmv_object_count;
68 static CFS_LIST_HEAD(obj_list);
69 static cfs_spinlock_t obj_list_lock = CFS_SPIN_LOCK_UNLOCKED;
71 struct lmv_object *lmv_object_alloc(struct obd_device *obd,
72 const struct lu_fid *fid,
73 struct lmv_stripe_md *mea)
75 struct lmv_obd *lmv = &obd->u.lmv;
76 unsigned int obj_size;
77 struct lmv_object *obj;
80 LASSERT(mea->mea_magic == MEA_MAGIC_LAST_CHAR
81 || mea->mea_magic == MEA_MAGIC_ALL_CHARS
82 || mea->mea_magic == MEA_MAGIC_HASH_SEGMENT);
84 OBD_SLAB_ALLOC_PTR(obj, lmv_object_cache);
88 cfs_atomic_inc(&lmv_object_count);
93 obj->lo_hashtype = mea->mea_magic;
95 cfs_init_mutex(&obj->lo_guard);
96 cfs_atomic_set(&obj->lo_count, 0);
97 obj->lo_objcount = mea->mea_count;
99 obj_size = sizeof(struct lmv_stripe) *
100 lmv->desc.ld_tgt_count;
102 OBD_ALLOC_LARGE(obj->lo_stripes, obj_size);
103 if (!obj->lo_stripes)
106 CDEBUG(D_INODE, "Allocate object for "DFID"\n",
108 for (i = 0; i < mea->mea_count; i++) {
111 CDEBUG(D_INODE, "Process subobject "DFID"\n",
112 PFID(&mea->mea_ids[i]));
113 obj->lo_stripes[i].ls_fid = mea->mea_ids[i];
114 LASSERT(fid_is_sane(&obj->lo_stripes[i].ls_fid));
117 * Cache slave mds number to use it in all cases it is needed
118 * instead of constant lookup.
120 rc = lmv_fld_lookup(lmv, &obj->lo_stripes[i].ls_fid,
121 &obj->lo_stripes[i].ls_mds);
128 OBD_FREE(obj, sizeof(*obj));
132 void lmv_object_free(struct lmv_object *obj)
134 struct lmv_obd *lmv = &obj->lo_obd->u.lmv;
135 unsigned int obj_size;
137 LASSERT(!cfs_atomic_read(&obj->lo_count));
139 obj_size = sizeof(struct lmv_stripe) *
140 lmv->desc.ld_tgt_count;
142 OBD_FREE_LARGE(obj->lo_stripes, obj_size);
143 OBD_SLAB_FREE(obj, lmv_object_cache, sizeof(*obj));
144 cfs_atomic_dec(&lmv_object_count);
147 static void __lmv_object_add(struct lmv_object *obj)
149 cfs_atomic_inc(&obj->lo_count);
150 cfs_list_add(&obj->lo_list, &obj_list);
153 void lmv_object_add(struct lmv_object *obj)
155 cfs_spin_lock(&obj_list_lock);
156 __lmv_object_add(obj);
157 cfs_spin_unlock(&obj_list_lock);
160 static void __lmv_object_del(struct lmv_object *obj)
162 cfs_list_del(&obj->lo_list);
163 lmv_object_free(obj);
166 void lmv_object_del(struct lmv_object *obj)
168 cfs_spin_lock(&obj_list_lock);
169 __lmv_object_del(obj);
170 cfs_spin_unlock(&obj_list_lock);
173 static struct lmv_object *__lmv_object_get(struct lmv_object *obj)
175 LASSERT(obj != NULL);
176 cfs_atomic_inc(&obj->lo_count);
180 struct lmv_object *lmv_object_get(struct lmv_object *obj)
182 cfs_spin_lock(&obj_list_lock);
183 __lmv_object_get(obj);
184 cfs_spin_unlock(&obj_list_lock);
188 static void __lmv_object_put(struct lmv_object *obj)
192 if (cfs_atomic_dec_and_test(&obj->lo_count)) {
193 CDEBUG(D_INODE, "Last reference to "DFID" - "
194 "destroying\n", PFID(&obj->lo_fid));
195 __lmv_object_del(obj);
199 void lmv_object_put(struct lmv_object *obj)
201 cfs_spin_lock(&obj_list_lock);
202 __lmv_object_put(obj);
203 cfs_spin_unlock(&obj_list_lock);
206 void lmv_object_put_unlock(struct lmv_object *obj)
208 lmv_object_unlock(obj);
212 static struct lmv_object *__lmv_object_find(struct obd_device *obd, const struct lu_fid *fid)
214 struct lmv_object *obj;
217 cfs_list_for_each(cur, &obj_list) {
218 obj = cfs_list_entry(cur, struct lmv_object, lo_list);
221 * Check if object is in destroying phase. If so - skip
224 if (obj->lo_state & O_FREEING)
228 * We should make sure, that we have found object belong to
229 * passed obd. It is possible that, object manager will have two
230 * objects with the same fid belong to different obds, if client
231 * and mds runs on the same host. May be it is good idea to have
232 * objects list associated with obd.
234 if (obj->lo_obd != obd)
238 * Check if this is what we're looking for.
240 if (lu_fid_eq(&obj->lo_fid, fid))
241 return __lmv_object_get(obj);
247 struct lmv_object *lmv_object_find(struct obd_device *obd,
248 const struct lu_fid *fid)
250 struct lmv_obd *lmv = &obd->u.lmv;
251 struct lmv_object *obj = NULL;
254 /* For single MDT case, lmv_object list is always empty. */
255 if (lmv->desc.ld_tgt_count > 1) {
256 cfs_spin_lock(&obj_list_lock);
257 obj = __lmv_object_find(obd, fid);
258 cfs_spin_unlock(&obj_list_lock);
264 struct lmv_object *lmv_object_find_lock(struct obd_device *obd,
265 const struct lu_fid *fid)
267 struct lmv_object *obj;
270 obj = lmv_object_find(obd, fid);
272 lmv_object_lock(obj);
277 static struct lmv_object *__lmv_object_create(struct obd_device *obd,
278 const struct lu_fid *fid,
279 struct lmv_stripe_md *mea)
281 struct lmv_object *new;
282 struct lmv_object *obj;
285 obj = lmv_object_find(obd, fid);
289 new = lmv_object_alloc(obd, fid, mea);
294 * Check if someone created it already while we were dealing with
297 cfs_spin_lock(&obj_list_lock);
298 obj = __lmv_object_find(obd, fid);
301 * Someone created it already - put @obj and getting out.
303 cfs_spin_unlock(&obj_list_lock);
304 lmv_object_free(new);
308 __lmv_object_add(new);
309 __lmv_object_get(new);
311 cfs_spin_unlock(&obj_list_lock);
313 CDEBUG(D_INODE, "New obj in lmv cache: "DFID"\n",
319 struct lmv_object *lmv_object_create(struct obd_export *exp,
320 const struct lu_fid *fid,
321 struct lmv_stripe_md *mea)
323 struct obd_device *obd = exp->exp_obd;
324 struct lmv_obd *lmv = &obd->u.lmv;
325 struct ptlrpc_request *req = NULL;
326 struct lmv_tgt_desc *tgt;
327 struct lmv_object *obj;
333 CDEBUG(D_INODE, "Get mea for "DFID" and create lmv obj\n",
339 struct md_op_data *op_data;
342 CDEBUG(D_INODE, "Mea isn't passed in, get it now\n");
343 mealen = lmv_get_easize(lmv);
346 * Time to update mea of parent fid.
349 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
351 tgt = lmv_find_target(lmv, fid);
353 GOTO(cleanup, obj = (void *)tgt);
355 OBD_ALLOC_PTR(op_data);
357 GOTO(cleanup, obj = ERR_PTR(-ENOMEM));
359 op_data->op_fid1 = *fid;
360 op_data->op_mode = mealen;
361 op_data->op_valid = valid;
362 rc = md_getattr(tgt->ltd_exp, op_data, &req);
363 OBD_FREE_PTR(op_data);
365 CERROR("md_getattr() failed, error %d\n", rc);
366 GOTO(cleanup, obj = ERR_PTR(rc));
369 rc = md_get_lustre_md(exp, req, NULL, exp, &md);
371 CERROR("md_get_lustre_md() failed, error %d\n", rc);
372 GOTO(cleanup, obj = ERR_PTR(rc));
376 GOTO(cleanup, obj = ERR_PTR(-ENODATA));
382 * Got mea, now create obj for it.
384 obj = __lmv_object_create(obd, fid, mea);
386 CERROR("Can't create new object "DFID"\n",
388 GOTO(cleanup, obj = ERR_PTR(-ENOMEM));
392 obd_free_memmd(exp, (void *)&md.mea);
397 ptlrpc_req_finished(req);
401 int lmv_object_delete(struct obd_export *exp, const struct lu_fid *fid)
403 struct obd_device *obd = exp->exp_obd;
404 struct lmv_object *obj;
408 cfs_spin_lock(&obj_list_lock);
409 obj = __lmv_object_find(obd, fid);
411 obj->lo_state |= O_FREEING;
412 __lmv_object_put(obj);
413 __lmv_object_put(obj);
416 cfs_spin_unlock(&obj_list_lock);
420 int lmv_object_setup(struct obd_device *obd)
423 LASSERT(obd != NULL);
425 CDEBUG(D_INFO, "LMV object manager setup (%s)\n",
431 void lmv_object_cleanup(struct obd_device *obd)
435 struct lmv_object *obj;
438 CDEBUG(D_INFO, "LMV object manager cleanup (%s)\n",
441 cfs_spin_lock(&obj_list_lock);
442 cfs_list_for_each_safe(cur, tmp, &obj_list) {
443 obj = cfs_list_entry(cur, struct lmv_object, lo_list);
445 if (obj->lo_obd != obd)
448 obj->lo_state |= O_FREEING;
449 if (cfs_atomic_read(&obj->lo_count) > 1) {
450 CERROR("Object "DFID" has count (%d)\n",
452 cfs_atomic_read(&obj->lo_count));
454 __lmv_object_put(obj);
456 cfs_spin_unlock(&obj_list_lock);