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 @fid and @mea. */
56 lmv_alloc_obj(struct obd_device *obd, struct ll_fid *fid,
61 unsigned int obj_size;
62 struct lmv_obd *lmv = &obd->u.lmv;
64 LASSERT(mea->mea_magic == MEA_MAGIC_LAST_CHAR
65 || mea->mea_magic == MEA_MAGIC_ALL_CHARS);
67 OBD_ALLOC(obj, sizeof(*obj));
74 obj->hashtype = mea->mea_magic;
76 init_MUTEX(&obj->guard);
77 atomic_set(&obj->count, 0);
78 obj->objcount = mea->mea_count;
80 obj_size = sizeof(struct lmv_inode) *
81 lmv->desc.ld_tgt_count;
83 OBD_ALLOC(obj->objs, obj_size);
87 memset(obj->objs, 0, obj_size);
90 for (i = 0; i < mea->mea_count; i++) {
91 CDEBUG(D_OTHER, "subobj %lu/%lu/%lu\n",
92 (unsigned long)mea->mea_fids[i].mds,
93 (unsigned long)mea->mea_fids[i].id,
94 (unsigned long)mea->mea_fids[i].generation);
95 obj->objs[i].fid = mea->mea_fids[i];
101 OBD_FREE(obj, sizeof(*obj));
105 /* destroys 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 ll_fid *fid = &obj->fid;
177 CDEBUG(D_OTHER, "last reference to %lu/%lu/%lu - destroying\n",
178 (unsigned long)fid->mds, (unsigned long)fid->id,
179 (unsigned long)fid->generation);
185 lmv_put_obj(struct lmv_obj *obj)
187 spin_lock(&lmv_obj_list_lock);
189 spin_unlock(&lmv_obj_list_lock);
192 static struct lmv_obj *
193 __grab_obj(struct obd_device *obd, struct ll_fid *fid)
196 struct list_head *cur;
198 list_for_each(cur, &lmv_obj_list) {
199 obj = list_entry(cur, struct lmv_obj, list);
201 /* check if object is in progress of destroying. If so - skip
203 if (obj->state & O_FREEING)
206 /* check if this is waht we're looking for. */
207 if (fid_equal(&obj->fid, fid))
208 return __get_obj(obj);
215 lmv_grab_obj(struct obd_device *obd, struct ll_fid *fid)
220 spin_lock(&lmv_obj_list_lock);
221 obj = __grab_obj(obd, fid);
222 spin_unlock(&lmv_obj_list_lock);
227 /* looks in objects list for an object that matches passed @fid. If it is not
228 * found -- creates it using passed @mea and puts onto list. */
229 static struct lmv_obj *
230 __create_obj(struct obd_device *obd, struct ll_fid *fid, struct mea *mea)
232 struct lmv_obj *new, *obj;
235 obj = lmv_grab_obj(obd, fid);
239 /* no such object yet, allocate and initialize it. */
240 new = lmv_alloc_obj(obd, fid, mea);
244 /* check if someone create it already while we were dealing with
245 * allocating @obj. */
246 spin_lock(&lmv_obj_list_lock);
247 obj = __grab_obj(obd, fid);
249 /* someone created it already - put @obj and getting out. */
251 spin_unlock(&lmv_obj_list_lock);
258 spin_unlock(&lmv_obj_list_lock);
260 CDEBUG(D_OTHER, "new obj in lmv cache: %lu/%lu/%lu\n",
261 (unsigned long)fid->mds, (unsigned long)fid->id,
262 (unsigned long)fid->generation);
268 /* creates object from passed @fid and @mea. If @mea is NULL, it will be
269 * obtained from correct MDT and used for constructing the object. */
271 lmv_create_obj(struct obd_export *exp, struct ll_fid *fid, struct mea *mea)
273 struct obd_device *obd = exp->exp_obd;
274 struct lmv_obd *lmv = &obd->u.lmv;
275 struct ptlrpc_request *req = NULL;
281 CDEBUG(D_OTHER, "get mea for %lu/%lu/%lu and create lmv obj\n",
282 (unsigned long)fid->mds, (unsigned long)fid->id,
283 (unsigned long)fid->generation);
288 CDEBUG(D_OTHER, "mea isn't passed in, get it now\n");
289 mealen = MEA_SIZE_LMV(lmv);
291 /* time to update mea of parent fid */
295 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
296 rc = md_getattr(lmv->tgts[fid->mds].ltd_exp, fid,
297 valid, mealen, &req);
299 CERROR("md_getattr() failed, error %d\n", rc);
300 GOTO(cleanup, obj = ERR_PTR(rc));
303 rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
305 CERROR("mdc_req2lustre_md() failed, error %d\n", rc);
306 GOTO(cleanup, obj = ERR_PTR(rc));
310 GOTO(cleanup, obj = ERR_PTR(-ENODATA));
315 /* got mea, now create obj for it. */
316 obj = __create_obj(obd, fid, mea);
318 CERROR("Can't create new object %lu/%lu/%lu\n",
319 (unsigned long)fid->mds, (unsigned long)fid->id,
320 (unsigned long)fid->generation);
321 GOTO(cleanup, obj = ERR_PTR(-ENOMEM));
325 ptlrpc_req_finished(req);
329 /* looks for object with @fid and orders to destroy it. It possible the object
330 * will not be destroyed right now, because it is still using by someone. In
331 * this case it will be marked as "freeing" and will not be accessible anymore
332 * for subsequent callers of lmv_grab_obj(). */
334 lmv_delete_obj(struct obd_export *exp, struct ll_fid *fid)
336 struct obd_device *obd = exp->exp_obd;
341 spin_lock(&lmv_obj_list_lock);
343 obj = __grab_obj(obd, fid);
345 obj->state |= O_FREEING;
347 if (atomic_read(&obj->count) > 1)
348 CERROR("obj %lu/%lu/%lu has count > 2 (%d)\n",
349 (unsigned long) obj->fid.mds,
350 (unsigned long) obj->fid.id,
351 (unsigned long) obj->fid.generation,
352 atomic_read(&obj->count));
358 spin_unlock(&lmv_obj_list_lock);
363 lmv_setup_mgr(struct obd_device *obd)
365 CDEBUG(D_INFO, "LMV object manager setup (%s)\n", obd->obd_uuid.uuid);
370 lmv_cleanup_mgr(struct obd_device *obd)
373 struct list_head *cur, *tmp;
375 CDEBUG(D_INFO, "LMV object manager cleanup (%s)\n", obd->obd_uuid.uuid);
377 spin_lock(&lmv_obj_list_lock);
378 list_for_each_safe(cur, tmp, &lmv_obj_list) {
379 obj = list_entry(cur, struct lmv_obj, list);
384 obj->state |= O_FREEING;
385 if (atomic_read(&obj->count) > 1)
386 CERROR("obj %lu/%lu/%lu has count > 1 (%d)\n",
387 (unsigned long) obj->fid.mds,
388 (unsigned long) obj->fid.id,
389 (unsigned long) obj->fid.generation,
390 atomic_read(&obj->count));
393 spin_unlock(&lmv_obj_list_lock);