Whamcloud - gitweb
- added standard CFS headers in some source files.
[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 static LIST_HEAD(lmv_obj_list);
52 static spinlock_t lmv_obj_list_lock = SPIN_LOCK_UNLOCKED;
53
54 /* creates new obj on passed @id and @mea. */
55 struct lmv_obj *
56 lmv_alloc_obj(struct obd_device *obd,
57               struct lustre_id *id,
58               struct mea *mea)
59 {
60         int i;
61         struct lmv_obj *obj;
62         unsigned int obj_size;
63         struct lmv_obd *lmv = &obd->u.lmv;
64
65         LASSERT(mea->mea_magic == MEA_MAGIC_LAST_CHAR
66                 || mea->mea_magic == MEA_MAGIC_ALL_CHARS);
67
68         OBD_ALLOC(obj, sizeof(*obj));
69         if (!obj)
70                 return NULL;
71
72         obj->id = *id;
73         obj->obd = obd;
74         obj->state = 0;
75         obj->hashtype = mea->mea_magic;
76
77         init_MUTEX(&obj->guard);
78         atomic_set(&obj->count, 0);
79         obj->objcount = mea->mea_count;
80
81         obj_size = sizeof(struct lmv_inode) *
82                 lmv->desc.ld_tgt_count;
83         
84         OBD_ALLOC(obj->objs, obj_size);
85         if (!obj->objs)
86                 goto err_obj;
87
88         memset(obj->objs, 0, obj_size);
89
90         /* put all ids in */
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));
96         }
97
98         return obj;
99         
100 err_obj:
101         OBD_FREE(obj, sizeof(*obj));
102         return NULL;
103 }
104
105 /* destroy passed @obj. */
106 void
107 lmv_free_obj(struct lmv_obj *obj)
108 {
109         unsigned int obj_size;
110         struct lmv_obd *lmv = &obj->obd->u.lmv;
111         
112         obj_size = sizeof(struct lmv_inode) *
113                 lmv->desc.ld_tgt_count;
114         
115         OBD_FREE(obj->objs, obj_size);
116         OBD_FREE(obj, sizeof(*obj));
117 }
118
119 static void
120 __add_obj(struct lmv_obj *obj)
121 {
122         atomic_inc(&obj->count);
123         list_add(&obj->list, &lmv_obj_list);
124 }
125
126 void
127 lmv_add_obj(struct lmv_obj *obj)
128 {
129         spin_lock(&lmv_obj_list_lock);
130         __add_obj(obj);
131         spin_unlock(&lmv_obj_list_lock);
132 }
133
134 static void
135 __del_obj(struct lmv_obj *obj)
136 {
137         if (!(obj->state & O_FREEING))
138                 LBUG();
139         
140         list_del(&obj->list);
141         lmv_free_obj(obj);
142 }
143
144 void
145 lmv_del_obj(struct lmv_obj *obj)
146 {
147         spin_lock(&lmv_obj_list_lock);
148         __del_obj(obj);
149         spin_unlock(&lmv_obj_list_lock);
150 }
151
152 static struct lmv_obj *
153 __get_obj(struct lmv_obj *obj)
154 {
155         LASSERT(obj);
156         atomic_inc(&obj->count);
157         return obj;
158 }
159
160 struct lmv_obj *
161 lmv_get_obj(struct lmv_obj *obj)
162 {
163         spin_lock(&lmv_obj_list_lock);
164         __get_obj(obj);
165         spin_unlock(&lmv_obj_list_lock);
166
167         return obj;
168 }
169
170 static void
171 __put_obj(struct lmv_obj *obj)
172 {
173         LASSERT(obj);
174
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));
179                 __del_obj(obj);
180         }
181 }
182
183 void
184 lmv_put_obj(struct lmv_obj *obj)
185 {
186         spin_lock(&lmv_obj_list_lock);
187         __put_obj(obj);
188         spin_unlock(&lmv_obj_list_lock);
189 }
190
191 static struct lmv_obj *
192 __grab_obj(struct obd_device *obd, struct lustre_id *id)
193 {
194         struct lmv_obj *obj;
195         struct list_head *cur;
196
197         list_for_each(cur, &lmv_obj_list) {
198                 obj = list_entry(cur, struct lmv_obj, list);
199
200                 /* check if object is in progress of destroying. If so - skip
201                  * it. */
202                 if (obj->state & O_FREEING)
203                         continue;
204
205                 /* check if this is what we're looking for. */
206                 if (id_equal_fid(&obj->id, id))
207                         return __get_obj(obj);
208         }
209
210         return NULL;
211 }
212
213 struct lmv_obj *
214 lmv_grab_obj(struct obd_device *obd, struct lustre_id *id)
215 {
216         struct lmv_obj *obj;
217         ENTRY;
218         
219         spin_lock(&lmv_obj_list_lock);
220         obj = __grab_obj(obd, id);
221         spin_unlock(&lmv_obj_list_lock);
222         
223         RETURN(obj);
224 }
225
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)
230 {
231         struct lmv_obj *new, *obj;
232         ENTRY;
233
234         obj = lmv_grab_obj(obd, id);
235         if (obj)
236                 RETURN(obj);
237
238         /* no such object yet, allocate and initialize it. */
239         new = lmv_alloc_obj(obd, id, mea);
240         if (!new)
241                 RETURN(NULL);
242
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);
247         if (obj) {
248                 /* someone created it already - put @obj and getting out. */
249                 lmv_free_obj(new);
250                 spin_unlock(&lmv_obj_list_lock);
251                 RETURN(obj);
252         }
253
254         __add_obj(new);
255         __get_obj(new);
256         
257         spin_unlock(&lmv_obj_list_lock);
258
259         CDEBUG(D_OTHER, "new obj in lmv cache: "DLID4"\n",
260                OLID4(id));
261
262         RETURN(new);
263         
264 }
265
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. */
268 struct lmv_obj *
269 lmv_create_obj(struct obd_export *exp, struct lustre_id *id, struct mea *mea)
270 {
271         struct obd_device *obd = exp->exp_obd;
272         struct lmv_obd *lmv = &obd->u.lmv;
273         struct ptlrpc_request *req = NULL;
274         struct lmv_obj *obj;
275         struct lustre_md md;
276         int mealen, i, rc;
277         ENTRY;
278
279         CDEBUG(D_OTHER, "get mea for "DLID4" and create lmv obj\n",
280                OLID4(id));
281
282         if (!mea) {
283                 unsigned long valid;
284                 
285                 CDEBUG(D_OTHER, "mea isn't passed in, get it now\n");
286                 mealen = MEA_SIZE_LMV(lmv);
287                 
288                 /* time to update mea of parent id */
289                 i = id->li_fid.lf_group;
290                 md.mea = NULL;
291                 
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);
295                 if (rc) {
296                         CERROR("md_getattr() failed, error %d\n", rc);
297                         GOTO(cleanup, obj = ERR_PTR(rc));
298                 }
299
300                 rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
301                 if (rc) {
302                         CERROR("mdc_req2lustre_md() failed, error %d\n", rc);
303                         GOTO(cleanup, obj = ERR_PTR(rc));
304                 }
305
306                 if (!md.mea)
307                         GOTO(cleanup, obj = ERR_PTR(-ENODATA));
308                         
309                 mea = md.mea;
310         }
311
312         /* got mea, now create obj for it. */
313         obj = __create_obj(obd, id, mea);
314         if (!obj) {
315                 CERROR("Can't create new object "DLID4"\n",
316                        OLID4(id));
317                 GOTO(cleanup, obj = ERR_PTR(-ENOMEM));
318         }
319 cleanup:
320         if (req)       
321                 ptlrpc_req_finished(req);
322         RETURN(obj);
323 }
324
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(). */
329 int
330 lmv_delete_obj(struct obd_export *exp, struct lustre_id *id)
331 {
332         struct obd_device *obd = exp->exp_obd;
333         struct lmv_obj *obj;
334         int rc = 0;
335         ENTRY;
336
337         spin_lock(&lmv_obj_list_lock);
338         
339         obj = __grab_obj(obd, id);
340         if (obj) {
341                 obj->state |= O_FREEING;
342                 
343                 if (atomic_read(&obj->count) > 1)
344                         CERROR("obj "DLID4" has count > 2 (%d)\n",
345                                OLID4(&obj->id), atomic_read(&obj->count));
346                 __put_obj(obj);
347                 __put_obj(obj);
348                 rc = 1;
349         }
350
351         spin_unlock(&lmv_obj_list_lock);
352         RETURN(rc);
353 }
354
355 int
356 lmv_setup_mgr(struct obd_device *obd)
357 {
358         CDEBUG(D_INFO, "LMV object manager setup (%s)\n",
359                obd->obd_uuid.uuid);
360         return 0;
361 }
362
363 void
364 lmv_cleanup_mgr(struct obd_device *obd)
365 {
366         struct lmv_obj *obj;
367         struct list_head *cur, *tmp;
368
369         CDEBUG(D_INFO, "LMV object manager cleanup (%s)\n",
370                obd->obd_uuid.uuid);
371         
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);
375                 
376                 if (obj->obd != obd)
377                         continue;
378
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));
383                 __put_obj(obj);
384         }
385         spin_unlock(&lmv_obj_list_lock);
386 }