1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_LMV
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
32 #include <asm/div64.h>
33 #include <linux/seq_file.h>
35 #include <liblustre.h>
38 #include <lustre/lustre_idl.h>
39 #include <obd_support.h>
40 #include <lustre_lib.h>
41 #include <lustre_net.h>
42 #include <lustre_dlm.h>
43 #include <obd_class.h>
44 #include <lprocfs_status.h>
45 #include "lmv_internal.h"
48 extern cfs_mem_cache_t *obj_cache;
49 extern atomic_t obj_cache_count;
51 /* object list and its guard. */
52 static LIST_HEAD(obj_list);
53 static spinlock_t obj_list_lock = SPIN_LOCK_UNLOCKED;
55 /* creates new obj on passed @fid and @mea. */
57 lmv_obj_alloc(struct obd_device *obd,
58 const struct lu_fid *fid,
59 struct lmv_stripe_md *mea)
63 unsigned int obj_size;
64 struct lmv_obd *lmv = &obd->u.lmv;
66 LASSERT(mea->mea_magic == MEA_MAGIC_LAST_CHAR
67 || mea->mea_magic == MEA_MAGIC_ALL_CHARS
68 || mea->mea_magic == MEA_MAGIC_HASH_SEGMENT);
70 OBD_SLAB_ALLOC(obj, obj_cache, CFS_ALLOC_STD,
75 atomic_inc(&obj_cache_count);
80 obj->lo_hashtype = mea->mea_magic;
82 init_MUTEX(&obj->lo_guard);
83 atomic_set(&obj->lo_count, 0);
84 obj->lo_objcount = mea->mea_count;
86 obj_size = sizeof(struct lmv_inode) *
87 lmv->desc.ld_tgt_count;
89 OBD_ALLOC(obj->lo_inodes, obj_size);
93 memset(obj->lo_inodes, 0, obj_size);
96 for (i = 0; i < mea->mea_count; i++) {
99 CDEBUG(D_OTHER, "subobj "DFID"\n",
100 PFID(&mea->mea_ids[i]));
101 obj->lo_inodes[i].li_fid = mea->mea_ids[i];
102 LASSERT(fid_is_sane(&obj->lo_inodes[i].li_fid));
105 * Cache slave mds number to use it in all cases it is needed
106 * instead of constant lookup.
108 rc = lmv_fld_lookup(lmv, &obj->lo_inodes[i].li_fid,
109 &obj->lo_inodes[i].li_mds);
117 OBD_FREE(obj, sizeof(*obj));
121 /* destroy passed @obj. */
123 lmv_obj_free(struct lmv_obj *obj)
125 struct lmv_obd *lmv = &obj->lo_obd->u.lmv;
126 unsigned int obj_size;
128 LASSERT(!atomic_read(&obj->lo_count));
130 obj_size = sizeof(struct lmv_inode) *
131 lmv->desc.ld_tgt_count;
133 OBD_FREE(obj->lo_inodes, obj_size);
134 OBD_SLAB_FREE(obj, obj_cache, sizeof(*obj));
135 atomic_dec(&obj_cache_count);
139 __lmv_obj_add(struct lmv_obj *obj)
141 atomic_inc(&obj->lo_count);
142 list_add(&obj->lo_list, &obj_list);
146 lmv_obj_add(struct lmv_obj *obj)
148 spin_lock(&obj_list_lock);
150 spin_unlock(&obj_list_lock);
154 __lmv_obj_del(struct lmv_obj *obj)
156 list_del(&obj->lo_list);
161 lmv_obj_del(struct lmv_obj *obj)
163 spin_lock(&obj_list_lock);
165 spin_unlock(&obj_list_lock);
168 static struct lmv_obj *
169 __lmv_obj_get(struct lmv_obj *obj)
171 LASSERT(obj != NULL);
172 atomic_inc(&obj->lo_count);
177 lmv_obj_get(struct lmv_obj *obj)
179 spin_lock(&obj_list_lock);
181 spin_unlock(&obj_list_lock);
186 __lmv_obj_put(struct lmv_obj *obj)
190 if (atomic_dec_and_test(&obj->lo_count)) {
191 CDEBUG(D_OTHER, "last reference to "DFID" - "
192 "destroying\n", PFID(&obj->lo_fid));
198 lmv_obj_put(struct lmv_obj *obj)
200 spin_lock(&obj_list_lock);
202 spin_unlock(&obj_list_lock);
205 static struct lmv_obj *
206 __lmv_obj_grab(struct obd_device *obd, const struct lu_fid *fid)
209 struct list_head *cur;
211 list_for_each(cur, &obj_list) {
212 obj = list_entry(cur, struct lmv_obj, lo_list);
214 /* check if object is in progress of destroying. If so - skip
216 if (obj->lo_state & O_FREEING)
220 * we should make sure, that we have found object belong to
221 * passed obd. It is possible that, object manager will have two
222 * objects with the same fid belong to different obds, if client
223 * and mds runs on the same host. May be it is good idea to have
224 * objects list associated with obd.
226 if (obj->lo_obd != obd)
229 /* check if this is what we're looking for. */
230 if (lu_fid_eq(&obj->lo_fid, fid))
231 return __lmv_obj_get(obj);
238 lmv_obj_grab(struct obd_device *obd, const struct lu_fid *fid)
243 spin_lock(&obj_list_lock);
244 obj = __lmv_obj_grab(obd, fid);
245 spin_unlock(&obj_list_lock);
250 /* looks in objects list for an object that matches passed @fid. If it is not
251 * found -- creates it using passed @mea and puts onto list. */
252 static struct lmv_obj *
253 __lmv_obj_create(struct obd_device *obd, const struct lu_fid *fid,
254 struct lmv_stripe_md *mea)
256 struct lmv_obj *new, *obj;
259 obj = lmv_obj_grab(obd, fid);
263 /* no such object yet, allocate and initialize it. */
264 new = lmv_obj_alloc(obd, fid, mea);
268 /* check if someone create it already while we were dealing with
269 * allocating @obj. */
270 spin_lock(&obj_list_lock);
271 obj = __lmv_obj_grab(obd, fid);
273 /* someone created it already - put @obj and getting out. */
274 spin_unlock(&obj_list_lock);
282 spin_unlock(&obj_list_lock);
284 CDEBUG(D_OTHER, "new obj in lmv cache: "DFID"\n",
291 /* creates object from passed @fid and @mea. If @mea is NULL, it will be
292 * obtained from correct MDT and used for constructing the object. */
294 lmv_obj_create(struct obd_export *exp, const struct lu_fid *fid,
295 struct lmv_stripe_md *mea)
297 struct obd_device *obd = exp->exp_obd;
298 struct lmv_obd *lmv = &obd->u.lmv;
299 struct ptlrpc_request *req = NULL;
300 struct obd_export *tgt_exp;
306 CDEBUG(D_OTHER, "get mea for "DFID" and create lmv obj\n",
314 CDEBUG(D_OTHER, "mea isn't passed in, get it now\n");
315 mealen = lmv_get_easize(lmv);
317 /* time to update mea of parent fid */
319 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
321 tgt_exp = lmv_find_export(lmv, fid);
323 GOTO(cleanup, obj = (void *)tgt_exp);
325 rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
327 CERROR("md_getattr() failed, error %d\n", rc);
328 GOTO(cleanup, obj = ERR_PTR(rc));
331 rc = md_get_lustre_md(exp, req, NULL, exp, &md);
333 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
334 GOTO(cleanup, obj = ERR_PTR(rc));
338 GOTO(cleanup, obj = ERR_PTR(-ENODATA));
343 /* got mea, now create obj for it. */
344 obj = __lmv_obj_create(obd, fid, mea);
346 CERROR("Can't create new object "DFID"\n",
348 GOTO(cleanup, obj = ERR_PTR(-ENOMEM));
352 obd_free_memmd(exp, (struct lov_stripe_md **)&md.mea);
357 ptlrpc_req_finished(req);
362 * looks for object with @fid and orders to destroy it. It is possible the object
363 * will not be destroyed right now, because it is still using by someone. In
364 * this case it will be marked as "freeing" and will not be accessible anymore
365 * for subsequent callers of lmv_obj_grab().
368 lmv_obj_delete(struct obd_export *exp, const struct lu_fid *fid)
370 struct obd_device *obd = exp->exp_obd;
375 spin_lock(&obj_list_lock);
376 obj = __lmv_obj_grab(obd, fid);
378 obj->lo_state |= O_FREEING;
383 spin_unlock(&obj_list_lock);
389 lmv_obj_setup(struct obd_device *obd)
392 LASSERT(obd != NULL);
394 CDEBUG(D_INFO, "LMV object manager setup (%s)\n",
401 lmv_obj_cleanup(struct obd_device *obd)
403 struct list_head *cur, *tmp;
407 CDEBUG(D_INFO, "LMV object manager cleanup (%s)\n",
410 spin_lock(&obj_list_lock);
411 list_for_each_safe(cur, tmp, &obj_list) {
412 obj = list_entry(cur, struct lmv_obj, lo_list);
414 if (obj->lo_obd != obd)
417 obj->lo_state |= O_FREEING;
418 if (atomic_read(&obj->lo_count) > 1) {
419 CERROR("obj "DFID" has count > 1 (%d)\n",
420 PFID(&obj->lo_fid), atomic_read(&obj->lo_count));
424 spin_unlock(&obj_list_lock);