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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_LMV
42 #include <linux/slab.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/pagemap.h>
47 #include <asm/div64.h>
48 #include <linux/seq_file.h>
50 #include <liblustre.h>
53 #include <lustre/lustre_idl.h>
54 #include <obd_support.h>
55 #include <lustre_lib.h>
56 #include <lustre_net.h>
57 #include <lustre_dlm.h>
58 #include <obd_class.h>
59 #include <lprocfs_status.h>
60 #include "lmv_internal.h"
63 extern cfs_mem_cache_t *obj_cache;
64 extern atomic_t obj_cache_count;
66 /* object list and its guard. */
67 static CFS_LIST_HEAD(obj_list);
68 static spinlock_t obj_list_lock = SPIN_LOCK_UNLOCKED;
70 /* creates new obj on passed @fid and @mea. */
72 lmv_obj_alloc(struct obd_device *obd,
73 const struct lu_fid *fid,
74 struct lmv_stripe_md *mea)
78 unsigned int obj_size;
79 struct lmv_obd *lmv = &obd->u.lmv;
81 LASSERT(mea->mea_magic == MEA_MAGIC_LAST_CHAR
82 || mea->mea_magic == MEA_MAGIC_ALL_CHARS
83 || mea->mea_magic == MEA_MAGIC_HASH_SEGMENT);
85 OBD_SLAB_ALLOC(obj, obj_cache, CFS_ALLOC_STD,
90 atomic_inc(&obj_cache_count);
95 obj->lo_hashtype = mea->mea_magic;
97 init_MUTEX(&obj->lo_guard);
98 atomic_set(&obj->lo_count, 0);
99 obj->lo_objcount = mea->mea_count;
101 obj_size = sizeof(struct lmv_inode) *
102 lmv->desc.ld_tgt_count;
104 OBD_ALLOC(obj->lo_inodes, obj_size);
108 memset(obj->lo_inodes, 0, obj_size);
111 for (i = 0; i < mea->mea_count; i++) {
114 CDEBUG(D_OTHER, "subobj "DFID"\n",
115 PFID(&mea->mea_ids[i]));
116 obj->lo_inodes[i].li_fid = mea->mea_ids[i];
117 LASSERT(fid_is_sane(&obj->lo_inodes[i].li_fid));
120 * Cache slave mds number to use it in all cases it is needed
121 * instead of constant lookup.
123 rc = lmv_fld_lookup(lmv, &obj->lo_inodes[i].li_fid,
124 &obj->lo_inodes[i].li_mds);
132 OBD_FREE(obj, sizeof(*obj));
136 /* destroy passed @obj. */
138 lmv_obj_free(struct lmv_obj *obj)
140 struct lmv_obd *lmv = &obj->lo_obd->u.lmv;
141 unsigned int obj_size;
143 LASSERT(!atomic_read(&obj->lo_count));
145 obj_size = sizeof(struct lmv_inode) *
146 lmv->desc.ld_tgt_count;
148 OBD_FREE(obj->lo_inodes, obj_size);
149 OBD_SLAB_FREE(obj, obj_cache, sizeof(*obj));
150 atomic_dec(&obj_cache_count);
154 __lmv_obj_add(struct lmv_obj *obj)
156 atomic_inc(&obj->lo_count);
157 list_add(&obj->lo_list, &obj_list);
161 lmv_obj_add(struct lmv_obj *obj)
163 spin_lock(&obj_list_lock);
165 spin_unlock(&obj_list_lock);
169 __lmv_obj_del(struct lmv_obj *obj)
171 list_del(&obj->lo_list);
176 lmv_obj_del(struct lmv_obj *obj)
178 spin_lock(&obj_list_lock);
180 spin_unlock(&obj_list_lock);
183 static struct lmv_obj *
184 __lmv_obj_get(struct lmv_obj *obj)
186 LASSERT(obj != NULL);
187 atomic_inc(&obj->lo_count);
192 lmv_obj_get(struct lmv_obj *obj)
194 spin_lock(&obj_list_lock);
196 spin_unlock(&obj_list_lock);
201 __lmv_obj_put(struct lmv_obj *obj)
205 if (atomic_dec_and_test(&obj->lo_count)) {
206 CDEBUG(D_OTHER, "last reference to "DFID" - "
207 "destroying\n", PFID(&obj->lo_fid));
213 lmv_obj_put(struct lmv_obj *obj)
215 spin_lock(&obj_list_lock);
217 spin_unlock(&obj_list_lock);
220 static struct lmv_obj *
221 __lmv_obj_grab(struct obd_device *obd, const struct lu_fid *fid)
224 struct list_head *cur;
226 list_for_each(cur, &obj_list) {
227 obj = list_entry(cur, struct lmv_obj, lo_list);
229 /* check if object is in progress of destroying. If so - skip
231 if (obj->lo_state & O_FREEING)
235 * we should make sure, that we have found object belong to
236 * passed obd. It is possible that, object manager will have two
237 * objects with the same fid belong to different obds, if client
238 * and mds runs on the same host. May be it is good idea to have
239 * objects list associated with obd.
241 if (obj->lo_obd != obd)
244 /* check if this is what we're looking for. */
245 if (lu_fid_eq(&obj->lo_fid, fid))
246 return __lmv_obj_get(obj);
253 lmv_obj_grab(struct obd_device *obd, const struct lu_fid *fid)
258 spin_lock(&obj_list_lock);
259 obj = __lmv_obj_grab(obd, fid);
260 spin_unlock(&obj_list_lock);
265 /* looks in objects list for an object that matches passed @fid. If it is not
266 * found -- creates it using passed @mea and puts onto list. */
267 static struct lmv_obj *
268 __lmv_obj_create(struct obd_device *obd, const struct lu_fid *fid,
269 struct lmv_stripe_md *mea)
271 struct lmv_obj *new, *obj;
274 obj = lmv_obj_grab(obd, fid);
278 /* no such object yet, allocate and initialize it. */
279 new = lmv_obj_alloc(obd, fid, mea);
283 /* check if someone create it already while we were dealing with
284 * allocating @obj. */
285 spin_lock(&obj_list_lock);
286 obj = __lmv_obj_grab(obd, fid);
288 /* someone created it already - put @obj and getting out. */
289 spin_unlock(&obj_list_lock);
297 spin_unlock(&obj_list_lock);
299 CDEBUG(D_OTHER, "new obj in lmv cache: "DFID"\n",
306 /* creates object from passed @fid and @mea. If @mea is NULL, it will be
307 * obtained from correct MDT and used for constructing the object. */
309 lmv_obj_create(struct obd_export *exp, const struct lu_fid *fid,
310 struct lmv_stripe_md *mea)
312 struct obd_device *obd = exp->exp_obd;
313 struct lmv_obd *lmv = &obd->u.lmv;
314 struct ptlrpc_request *req = NULL;
315 struct obd_export *tgt_exp;
321 CDEBUG(D_OTHER, "get mea for "DFID" and create lmv obj\n",
329 CDEBUG(D_OTHER, "mea isn't passed in, get it now\n");
330 mealen = lmv_get_easize(lmv);
332 /* time to update mea of parent fid */
334 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
336 tgt_exp = lmv_find_export(lmv, fid);
338 GOTO(cleanup, obj = (void *)tgt_exp);
340 rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
342 CERROR("md_getattr() failed, error %d\n", rc);
343 GOTO(cleanup, obj = ERR_PTR(rc));
346 rc = md_get_lustre_md(exp, req, NULL, exp, &md);
348 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
349 GOTO(cleanup, obj = ERR_PTR(rc));
353 GOTO(cleanup, obj = ERR_PTR(-ENODATA));
358 /* got mea, now create obj for it. */
359 obj = __lmv_obj_create(obd, fid, mea);
361 CERROR("Can't create new object "DFID"\n",
363 GOTO(cleanup, obj = ERR_PTR(-ENOMEM));
366 /* XXX LOV STACKING */
368 obd_free_memmd(exp, (void *)&md.mea);
373 ptlrpc_req_finished(req);
378 * looks for object with @fid and orders to destroy it. It is possible the object
379 * will not be destroyed right now, because it is still using by someone. In
380 * this case it will be marked as "freeing" and will not be accessible anymore
381 * for subsequent callers of lmv_obj_grab().
384 lmv_obj_delete(struct obd_export *exp, const struct lu_fid *fid)
386 struct obd_device *obd = exp->exp_obd;
391 spin_lock(&obj_list_lock);
392 obj = __lmv_obj_grab(obd, fid);
394 obj->lo_state |= O_FREEING;
399 spin_unlock(&obj_list_lock);
405 lmv_obj_setup(struct obd_device *obd)
408 LASSERT(obd != NULL);
410 CDEBUG(D_INFO, "LMV object manager setup (%s)\n",
417 lmv_obj_cleanup(struct obd_device *obd)
419 struct list_head *cur, *tmp;
423 CDEBUG(D_INFO, "LMV object manager cleanup (%s)\n",
426 spin_lock(&obj_list_lock);
427 list_for_each_safe(cur, tmp, &obj_list) {
428 obj = list_entry(cur, struct lmv_obj, lo_list);
430 if (obj->lo_obd != obd)
433 obj->lo_state |= O_FREEING;
434 if (atomic_read(&obj->lo_count) > 1) {
435 CERROR("obj "DFID" has count > 1 (%d)\n",
436 PFID(&obj->lo_fid), atomic_read(&obj->lo_count));
440 spin_unlock(&obj_list_lock);