Whamcloud - gitweb
c39574860ef179f40e773413a0baf1e2a0cffc57
[fs/lustre-release.git] / lustre / lmv / lmv_objmgr.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_LMV
26 #ifdef __KERNEL__
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>
34 #else
35 #include <liblustre.h>
36 #endif
37
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"
50
51 LIST_HEAD(lmv_obj_list);
52 spinlock_t lmv_obj_list_lock = SPIN_LOCK_UNLOCKED;
53
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,
57                 struct mea *mea)
58 {
59         int i;
60         struct lmv_obj *obj;
61         unsigned int obj_size;
62         struct lmv_obd *lmv = &obd->u.lmv;
63
64         OBD_ALLOC(obj, sizeof(*obj));
65         if (!obj)
66                 return NULL;
67
68         obj->obd = obd;
69         obj->fid = *fid;
70           
71         atomic_set(&obj->count, 0);
72         obj->objcount = mea->mea_count;
73
74         obj_size = sizeof(struct lmv_inode) *
75                 lmv->desc.ld_tgt_count;
76         
77         OBD_ALLOC(obj->objs, obj_size);
78         if (!obj->objs)
79                 goto err_obj;
80
81         memset(obj->objs, 0, obj_size);
82
83         /* put all fids in */
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];
90         }
91
92         return obj;
93         
94 err_obj:
95         OBD_FREE(obj, sizeof(*obj));
96         return NULL;
97 }
98
99 /* destroys passed @obj. */
100 static void
101 __lmv_free_obj(struct lmv_obj *obj)
102 {
103         unsigned int obj_size;
104         struct lmv_obd *lmv = &obj->obd->u.lmv;
105         
106         obj_size = sizeof(struct lmv_inode) *
107                 lmv->desc.ld_tgt_count;
108         
109         OBD_FREE(obj->objs, obj_size);
110         OBD_FREE(obj, sizeof(*obj));
111 }
112
113 struct lmv_obj *
114 lmv_get_obj(struct lmv_obj *obj)
115 {
116         LASSERT(obj);
117         atomic_inc(&obj->count);
118         return obj;
119 }
120
121 void
122 lmv_put_obj(struct lmv_obj *obj)
123 {
124         LASSERT(obj);
125
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);
131                 __lmv_free_obj(obj);
132         }
133 }
134
135 static struct lmv_obj *
136 __lmv_grab_obj(struct obd_device *obd, struct ll_fid *fid)
137 {
138         struct lmv_obj *obj;
139         struct list_head *cur;
140
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);
145         }
146         return NULL;
147 }
148
149 struct lmv_obj *
150 lmv_grab_obj(struct obd_device *obd, struct ll_fid *fid)
151 {
152         struct lmv_obj *obj;
153         ENTRY;
154         
155         spin_lock(&lmv_obj_list_lock);
156         obj = __lmv_grab_obj(obd, fid);
157         spin_unlock(&lmv_obj_list_lock);
158         
159         RETURN(obj);
160 }
161
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,
166                  struct mea *mea)
167 {
168         struct lmv_obj *obj, *cobj;
169         ENTRY;
170
171         obj = lmv_grab_obj(obd, fid);
172         if (obj)
173                 RETURN(obj);
174
175         /* no such object yet, allocate and initialize them. */
176         obj = __lmv_alloc_obj(obd, fid, mea);
177         if (!obj)
178                 RETURN(NULL);
179
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);
184         if (cobj) {
185                 /* someone created it already - put @obj and getting out. */
186                 __lmv_free_obj(obj);
187                 spin_unlock(&lmv_obj_list_lock);
188                 RETURN(cobj);
189         }
190
191         /* object is referenced by list and thus should have additional
192          * reference counted. */
193         lmv_get_obj(obj);
194         list_add(&obj->list, &lmv_obj_list);
195         spin_unlock(&lmv_obj_list_lock);
196
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);
200
201         RETURN(lmv_get_obj(obj));
202         
203 }
204
205 int
206 lmv_create_obj(struct obd_export *exp,
207                struct ll_fid *fid, struct mea *mea)
208 {
209         struct obd_device *obd = exp->exp_obd;
210         struct lmv_obd *lmv = &obd->u.lmv;
211         struct ptlrpc_request *req = NULL;
212         struct lmv_obj *obj;
213         struct lustre_md md;
214         int mealen, i, rc = 0;
215         ENTRY;
216
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);
220
221         if (!mea) {
222                 unsigned long valid;
223                 
224                 CDEBUG(D_OTHER, "mea isn't passed in, get it now\n");
225                 mealen = MEA_SIZE_LMV(lmv);
226                 
227                 /* time to update mea of parent fid */
228                 i = fid->mds;
229                 md.mea = NULL;
230                 
231                 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
232                 rc = md_getattr(lmv->tgts[fid->mds].ltd_exp, fid,
233                                 valid, mealen, &req);
234                 if (rc) {
235                         CERROR("md_getattr() failed, error %d\n", rc);
236                         GOTO(cleanup, rc);
237                 }
238
239                 rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
240                 if (rc) {
241                         CERROR("mdc_req2lustre_md() failed, error %d\n", rc);
242                         GOTO(cleanup, rc);
243                 }
244
245                 if (!md.mea)
246                         GOTO(cleanup, rc = -ENODATA);
247                         
248                 mea = md.mea;
249         }
250
251         /* got mea, now create obj for it. */
252         obj = __lmv_create_obj(obd, fid, mea);
253         if (!obj) {
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);
258         } else
259                 lmv_put_obj(obj);
260 cleanup:
261         if (req)       
262                 ptlrpc_req_finished(req);
263         RETURN(rc); 
264 }
265
266 int
267 lmv_setup_mgr(struct obd_device *obd)
268 {
269         CWARN("LMV object manager setup\n");
270         return 0;
271 }
272
273 void
274 lmv_cleanup_mgr(struct obd_device *obd)
275 {
276         struct lmv_obj *obj;
277         struct list_head *cur, *tmp;
278
279         CWARN("LMV object manager cleanup\n");
280         
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);
284                 
285                 if (obj->obd != obd)
286                         continue;
287
288                 list_del(&obj->list);
289
290                 if (atomic_read(&obj->count) > 1) {
291                         struct ll_fid *fid = &obj->fid;
292                         
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));
297                 }
298         
299                 /* list does not use object anymore, ref counter should be
300                  * descreased. */
301                 lmv_put_obj(obj);
302         }
303         spin_unlock(&lmv_obj_list_lock);
304 }