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 LIST_HEAD(lmv_obj_list);
52 spinlock_t lmv_obj_list_lock = SPIN_LOCK_UNLOCKED;
54 /* creates new obj on passed @fid and @mea. */
55 static struct lmv_obj *
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 OBD_ALLOC(obj, sizeof(*obj));
71 atomic_set(&obj->count, 0);
72 obj->objcount = mea->mea_count;
74 obj_size = sizeof(struct lmv_inode) *
75 lmv->desc.ld_tgt_count;
77 OBD_ALLOC(obj->objs, obj_size);
81 memset(obj->objs, 0, obj_size);
84 for (i = 0; i < mea->mea_count; i++) {
85 CDEBUG(D_OTHER, "subobj %lu/%lu/%lu\n",
86 (unsigned long)mea->mea_fids[i].mds,
87 (unsigned long)mea->mea_fids[i].id,
88 (unsigned long)mea->mea_fids[i].generation);
89 obj->objs[i].fid = mea->mea_fids[i];
95 OBD_FREE(obj, sizeof(*obj));
99 /* destroys passed @obj. */
101 __lmv_free_obj(struct lmv_obj *obj)
103 unsigned int obj_size;
104 struct lmv_obd *lmv = &obj->obd->u.lmv;
106 obj_size = sizeof(struct lmv_inode) *
107 lmv->desc.ld_tgt_count;
109 OBD_FREE(obj->objs, obj_size);
110 OBD_FREE(obj, sizeof(*obj));
114 lmv_get_obj(struct lmv_obj *obj)
117 atomic_inc(&obj->count);
122 lmv_put_obj(struct lmv_obj *obj)
126 if (atomic_dec_and_test(&obj->count)) {
127 struct ll_fid *fid = &obj->fid;
128 CDEBUG(D_OTHER, "last reference to %lu/%lu/%lu - destroying\n",
129 (unsigned long)fid->mds, (unsigned long)fid->id,
130 (unsigned long)fid->generation);
135 static struct lmv_obj *
136 __lmv_grab_obj(struct obd_device *obd, struct ll_fid *fid)
139 struct list_head *cur;
141 list_for_each(cur, &lmv_obj_list) {
142 obj = list_entry(cur, struct lmv_obj, list);
143 if (fid_equal(&obj->fid, fid))
144 return lmv_get_obj(obj);
150 lmv_grab_obj(struct obd_device *obd, struct ll_fid *fid)
155 spin_lock(&lmv_obj_list_lock);
156 obj = __lmv_grab_obj(obd, fid);
157 spin_unlock(&lmv_obj_list_lock);
162 /* looks in objects list for an object that matches passed @fid. If it is not
163 * found -- creates it using passed @mea and puts to list. */
164 static struct lmv_obj *
165 __lmv_create_obj(struct obd_device *obd, struct ll_fid *fid,
168 struct lmv_obj *obj, *cobj;
171 obj = lmv_grab_obj(obd, fid);
175 /* no such object yet, allocate and initialize them. */
176 obj = __lmv_alloc_obj(obd, fid, mea);
180 /* check if someone create it already while we were dealing with
181 * allocating @obj. */
182 spin_lock(&lmv_obj_list_lock);
183 cobj = __lmv_grab_obj(obd, fid);
185 /* someone created it already - put @obj and getting out. */
187 spin_unlock(&lmv_obj_list_lock);
191 /* object is referenced by list and thus should have additional
192 * reference counted. */
194 list_add(&obj->list, &lmv_obj_list);
195 spin_unlock(&lmv_obj_list_lock);
197 CDEBUG(D_OTHER, "new obj in lmv cache: %lu/%lu/%lu\n",
198 (unsigned long)fid->mds, (unsigned long)fid->id,
199 (unsigned long)fid->generation);
201 RETURN(lmv_get_obj(obj));
206 lmv_create_obj(struct obd_export *exp,
207 struct ll_fid *fid, struct mea *mea)
209 struct obd_device *obd = exp->exp_obd;
210 struct lmv_obd *lmv = &obd->u.lmv;
211 struct ptlrpc_request *req = NULL;
214 int mealen, i, rc = 0;
217 CDEBUG(D_OTHER, "get mea for %lu/%lu/%lu and create lmv obj\n",
218 (unsigned long)fid->mds, (unsigned long)fid->id,
219 (unsigned long)fid->generation);
224 CDEBUG(D_OTHER, "mea isn't passed in, get it now\n");
225 mealen = MEA_SIZE_LMV(lmv);
227 /* time to update mea of parent fid */
231 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
232 rc = md_getattr(lmv->tgts[fid->mds].ltd_exp, fid,
233 valid, mealen, &req);
235 CERROR("md_getattr() failed, error %d\n", rc);
239 rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
241 CERROR("mdc_req2lustre_md() failed, error %d\n", rc);
246 GOTO(cleanup, rc = -ENODATA);
251 /* got mea, now create obj for it. */
252 obj = __lmv_create_obj(obd, fid, mea);
254 CERROR("Can't create new object %lu/%lu/%lu\n",
255 (unsigned long)fid->mds, (unsigned long)fid->id,
256 (unsigned long)fid->generation);
257 GOTO(cleanup, rc = -ENOMEM);
262 ptlrpc_req_finished(req);
267 lmv_setup_mgr(struct obd_device *obd)
269 CWARN("LMV object manager setup\n");
274 lmv_cleanup_mgr(struct obd_device *obd)
277 struct list_head *cur, *tmp;
279 CWARN("LMV object manager cleanup\n");
281 spin_lock(&lmv_obj_list_lock);
282 list_for_each_safe(cur, tmp, &lmv_obj_list) {
283 obj = list_entry(cur, struct lmv_obj, list);
288 list_del(&obj->list);
290 if (atomic_read(&obj->count) > 1) {
291 struct ll_fid *fid = &obj->fid;
293 CERROR("Object %lu/%lu/%lu has invalid ref count %d\n",
294 (unsigned long)fid->mds, (unsigned long)fid->id,
295 (unsigned long)fid->generation,
296 atomic_read(&obj->count));
299 /* list does not use object anymore, ref counter should be
303 spin_unlock(&lmv_obj_list_lock);