1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 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 <linux/obd_support.h>
39 #include <linux/lustre_lib.h>
40 #include <linux/lustre_net.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_dlm.h>
43 #include <linux/lustre_mds.h>
44 #include <linux/obd_class.h>
45 #include <linux/obd_ost.h>
46 #include <linux/lprocfs_status.h>
47 #include <linux/lustre_fsfilt.h>
48 #include <linux/obd_lmv.h>
49 #include "lmv_internal.h"
51 static LIST_HEAD(lmv_obj_list);
52 static spinlock_t lmv_obj_list_lock = SPIN_LOCK_UNLOCKED;
54 /* creates new obj on passed @id and @mea. */
56 lmv_alloc_obj(struct obd_device *obd,
62 unsigned int obj_size;
63 struct lmv_obd *lmv = &obd->u.lmv;
65 LASSERT(mea->mea_magic == MEA_MAGIC_LAST_CHAR
66 || mea->mea_magic == MEA_MAGIC_ALL_CHARS);
68 OBD_ALLOC(obj, sizeof(*obj));
75 obj->hashtype = mea->mea_magic;
77 init_MUTEX(&obj->guard);
78 atomic_set(&obj->count, 0);
79 obj->objcount = mea->mea_count;
81 obj_size = sizeof(struct lmv_inode) *
82 lmv->desc.ld_tgt_count;
84 OBD_ALLOC(obj->objs, obj_size);
88 memset(obj->objs, 0, obj_size);
91 for (i = 0; i < mea->mea_count; i++) {
92 CDEBUG(D_OTHER, "subobj "DLID4"\n",
93 OLID4(&mea->mea_ids[i]));
94 obj->objs[i].id = mea->mea_ids[i];
95 LASSERT(id_fid(&obj->objs[i].id));
101 OBD_FREE(obj, sizeof(*obj));
105 /* destroy passed @obj. */
107 lmv_free_obj(struct lmv_obj *obj)
109 unsigned int obj_size;
110 struct lmv_obd *lmv = &obj->obd->u.lmv;
112 obj_size = sizeof(struct lmv_inode) *
113 lmv->desc.ld_tgt_count;
115 OBD_FREE(obj->objs, obj_size);
116 OBD_FREE(obj, sizeof(*obj));
120 __add_obj(struct lmv_obj *obj)
122 atomic_inc(&obj->count);
123 list_add(&obj->list, &lmv_obj_list);
127 lmv_add_obj(struct lmv_obj *obj)
129 spin_lock(&lmv_obj_list_lock);
131 spin_unlock(&lmv_obj_list_lock);
135 __del_obj(struct lmv_obj *obj)
137 if (!(obj->state & O_FREEING))
140 list_del(&obj->list);
145 lmv_del_obj(struct lmv_obj *obj)
147 spin_lock(&lmv_obj_list_lock);
149 spin_unlock(&lmv_obj_list_lock);
152 static struct lmv_obj *
153 __get_obj(struct lmv_obj *obj)
156 atomic_inc(&obj->count);
161 lmv_get_obj(struct lmv_obj *obj)
163 spin_lock(&lmv_obj_list_lock);
165 spin_unlock(&lmv_obj_list_lock);
171 __put_obj(struct lmv_obj *obj)
175 if (atomic_dec_and_test(&obj->count)) {
176 struct lustre_id *id = &obj->id;
177 CDEBUG(D_OTHER, "last reference to "DLID4" - "
178 "destroying\n", OLID4(id));
184 lmv_put_obj(struct lmv_obj *obj)
186 spin_lock(&lmv_obj_list_lock);
188 spin_unlock(&lmv_obj_list_lock);
191 static struct lmv_obj *
192 __grab_obj(struct obd_device *obd, struct lustre_id *id)
195 struct list_head *cur;
197 list_for_each(cur, &lmv_obj_list) {
198 obj = list_entry(cur, struct lmv_obj, list);
200 /* check if object is in progress of destroying. If so - skip
202 if (obj->state & O_FREEING)
205 /* check if this is what we're looking for. */
206 if (id_equal_fid(&obj->id, id))
207 return __get_obj(obj);
214 lmv_grab_obj(struct obd_device *obd, struct lustre_id *id)
219 spin_lock(&lmv_obj_list_lock);
220 obj = __grab_obj(obd, id);
221 spin_unlock(&lmv_obj_list_lock);
226 /* looks in objects list for an object that matches passed @id. If it is not
227 * found -- creates it using passed @mea and puts onto list. */
228 static struct lmv_obj *
229 __create_obj(struct obd_device *obd, struct lustre_id *id, struct mea *mea)
231 struct lmv_obj *new, *obj;
234 obj = lmv_grab_obj(obd, id);
238 /* no such object yet, allocate and initialize it. */
239 new = lmv_alloc_obj(obd, id, mea);
243 /* check if someone create it already while we were dealing with
244 * allocating @obj. */
245 spin_lock(&lmv_obj_list_lock);
246 obj = __grab_obj(obd, id);
248 /* someone created it already - put @obj and getting out. */
250 spin_unlock(&lmv_obj_list_lock);
257 spin_unlock(&lmv_obj_list_lock);
259 CDEBUG(D_OTHER, "new obj in lmv cache: "DLID4"\n",
266 /* creates object from passed @id and @mea. If @mea is NULL, it will be
267 * obtained from correct MDT and used for constructing the object. */
269 lmv_create_obj(struct obd_export *exp, struct lustre_id *id, struct mea *mea)
271 struct obd_device *obd = exp->exp_obd;
272 struct lmv_obd *lmv = &obd->u.lmv;
273 struct ptlrpc_request *req = NULL;
279 CDEBUG(D_OTHER, "get mea for "DLID4" and create lmv obj\n",
285 CDEBUG(D_OTHER, "mea isn't passed in, get it now\n");
286 mealen = MEA_SIZE_LMV(lmv);
288 /* time to update mea of parent id */
289 i = id->li_fid.lf_group;
292 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
293 rc = md_getattr(lmv->tgts[id->li_fid.lf_group].ltd_exp,
294 id, valid, mealen, &req);
296 CERROR("md_getattr() failed, error %d\n", rc);
297 GOTO(cleanup, obj = ERR_PTR(rc));
300 rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
302 CERROR("mdc_req2lustre_md() failed, error %d\n", rc);
303 GOTO(cleanup, obj = ERR_PTR(rc));
307 GOTO(cleanup, obj = ERR_PTR(-ENODATA));
312 /* got mea, now create obj for it. */
313 obj = __create_obj(obd, id, mea);
315 CERROR("Can't create new object "DLID4"\n",
317 GOTO(cleanup, obj = ERR_PTR(-ENOMEM));
321 ptlrpc_req_finished(req);
325 /* looks for object with @id and orders to destroy it. It is possible the
326 * object will not be destroyed right now, because it is still using by
327 * someone. In this case it will be marked as "freeing" and will not be
328 * accessible anymore for subsequent callers of lmv_grab_obj(). */
330 lmv_delete_obj(struct obd_export *exp, struct lustre_id *id)
332 struct obd_device *obd = exp->exp_obd;
337 spin_lock(&lmv_obj_list_lock);
339 obj = __grab_obj(obd, id);
341 obj->state |= O_FREEING;
343 if (atomic_read(&obj->count) > 1)
344 CERROR("obj "DLID4" has count > 2 (%d)\n",
345 OLID4(&obj->id), atomic_read(&obj->count));
351 spin_unlock(&lmv_obj_list_lock);
356 lmv_setup_mgr(struct obd_device *obd)
358 CDEBUG(D_INFO, "LMV object manager setup (%s)\n",
364 lmv_cleanup_mgr(struct obd_device *obd)
367 struct list_head *cur, *tmp;
369 CDEBUG(D_INFO, "LMV object manager cleanup (%s)\n",
372 spin_lock(&lmv_obj_list_lock);
373 list_for_each_safe(cur, tmp, &lmv_obj_list) {
374 obj = list_entry(cur, struct lmv_obj, list);
379 obj->state |= O_FREEING;
380 if (atomic_read(&obj->count) > 1)
381 CERROR("obj "DLID4" has count > 1 (%d)\n",
382 OLID4(&obj->id), atomic_read(&obj->count));
385 spin_unlock(&lmv_obj_list_lock);