Whamcloud - gitweb
- fixes after FIDs inspection
[fs/lustre-release.git] / lustre / lmv / lmv_object.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 <lustre/lustre_idl.h>
39 #include <obd_support.h>
40 #include <lustre_lib.h>
41 #include <lustre_net.h>
42 #include <lustre_dlm.h>
43 #include <obd_class.h>
44 #include <lprocfs_status.h>
45 #include "lmv_internal.h"
46
47 /* objects cache. */
48 extern cfs_mem_cache_t *obj_cache;
49 extern atomic_t obj_cache_count;
50
51 /* object list and its guard. */
52 static LIST_HEAD(obj_list);
53 static spinlock_t obj_list_lock = SPIN_LOCK_UNLOCKED;
54
55 /* creates new obj on passed @fid and @mea. */
56 struct lmv_obj *
57 lmv_obj_alloc(struct obd_device *obd,
58               struct lu_fid *fid,
59               struct lmv_stripe_md *mea)
60 {
61         int i;
62         struct lmv_obj *obj;
63         unsigned int obj_size;
64         struct lmv_obd *lmv = &obd->u.lmv;
65
66         LASSERT(mea->mea_magic == MEA_MAGIC_LAST_CHAR
67                 || mea->mea_magic == MEA_MAGIC_ALL_CHARS);
68
69         OBD_SLAB_ALLOC(obj, obj_cache, CFS_ALLOC_STD,
70                        sizeof(*obj));
71         if (!obj)
72                 return NULL;
73
74         atomic_inc(&obj_cache_count);
75         
76         obj->lo_fid = *fid;
77         obj->lo_obd = obd;
78         obj->lo_state = 0;
79         obj->lo_hashtype = mea->mea_magic;
80
81         init_MUTEX(&obj->lo_guard);
82         atomic_set(&obj->lo_count, 0);
83         obj->lo_objcount = mea->mea_count;
84
85         obj_size = sizeof(struct lmv_inode) *
86                 lmv->desc.ld_tgt_count;
87         
88         OBD_ALLOC(obj->lo_inodes, obj_size);
89         if (!obj->lo_inodes)
90                 goto err_obj;
91
92         memset(obj->lo_inodes, 0, obj_size);
93
94         /* put all ids in */
95         for (i = 0; i < mea->mea_count; i++) {
96                 CDEBUG(D_OTHER, "subobj "DFID3"\n",
97                        PFID3(&mea->mea_ids[i]));
98                 obj->lo_inodes[i].li_fid = mea->mea_ids[i];
99                 LASSERT(fid_is_sane(&obj->lo_inodes[i].li_fid));
100         }
101
102         return obj;
103         
104 err_obj:
105         OBD_FREE(obj, sizeof(*obj));
106         return NULL;
107 }
108
109 /* destroy passed @obj. */
110 void
111 lmv_obj_free(struct lmv_obj *obj)
112 {
113         struct lmv_obd *lmv = &obj->lo_obd->u.lmv;
114         unsigned int obj_size;
115         
116         LASSERT(!atomic_read(&obj->lo_count));
117         
118         obj_size = sizeof(struct lmv_inode) *
119                 lmv->desc.ld_tgt_count;
120         
121         OBD_FREE(obj->lo_inodes, obj_size);
122         OBD_SLAB_FREE(obj, obj_cache, sizeof(*obj));
123         atomic_dec(&obj_cache_count);
124 }
125
126 static void
127 __lmv_obj_add(struct lmv_obj *obj)
128 {
129         atomic_inc(&obj->lo_count);
130         list_add(&obj->lo_list, &obj_list);
131 }
132
133 void
134 lmv_obj_add(struct lmv_obj *obj)
135 {
136         spin_lock(&obj_list_lock);
137         __lmv_obj_add(obj);
138         spin_unlock(&obj_list_lock);
139 }
140
141 static void
142 __lmv_obj_del(struct lmv_obj *obj)
143 {
144         list_del(&obj->lo_list);
145         lmv_obj_free(obj);
146 }
147
148 void
149 lmv_obj_del(struct lmv_obj *obj)
150 {
151         spin_lock(&obj_list_lock);
152         __lmv_obj_del(obj);
153         spin_unlock(&obj_list_lock);
154 }
155
156 static struct lmv_obj *
157 __lmv_obj_get(struct lmv_obj *obj)
158 {
159         LASSERT(obj != NULL);
160         atomic_inc(&obj->lo_count);
161         return obj;
162 }
163
164 struct lmv_obj *
165 lmv_obj_get(struct lmv_obj *obj)
166 {
167         spin_lock(&obj_list_lock);
168         __lmv_obj_get(obj);
169         spin_unlock(&obj_list_lock);
170         return obj;
171 }
172
173 static void
174 __lmv_obj_put(struct lmv_obj *obj)
175 {
176         LASSERT(obj);
177
178         if (atomic_dec_and_test(&obj->lo_count)) {
179                 struct lu_fid *fid = &obj->lo_fid;
180                 CDEBUG(D_OTHER, "last reference to "DFID3" - "
181                        "destroying\n", PFID3(fid));
182                 __lmv_obj_del(obj);
183         }
184 }
185
186 void
187 lmv_obj_put(struct lmv_obj *obj)
188 {
189         spin_lock(&obj_list_lock);
190         __lmv_obj_put(obj);
191         spin_unlock(&obj_list_lock);
192 }
193
194 static struct lmv_obj *
195 __lmv_obj_grab(struct obd_device *obd, struct lu_fid *fid)
196 {
197         struct lmv_obj *obj;
198         struct list_head *cur;
199
200         list_for_each(cur, &obj_list) {
201                 obj = list_entry(cur, struct lmv_obj, lo_list);
202
203                 /* check if object is in progress of destroying. If so - skip
204                  * it. */
205                 if (obj->lo_state & O_FREEING)
206                         continue;
207
208                 /* 
209                  * we should make sure, that we have found object belong to
210                  * passed obd. It is possible that, object manager will have two
211                  * objects with the same fid belong to different obds, if client
212                  * and mds runs on the same host. May be it is good idea to have
213                  * objects list assosiated with obd.
214                  */
215                 if (obj->lo_obd != obd)
216                         continue;
217
218                 /* check if this is what we're looking for. */
219                 if (lu_fid_eq(&obj->lo_fid, fid))
220                         return __lmv_obj_get(obj);
221         }
222
223         return NULL;
224 }
225
226 struct lmv_obj *
227 lmv_obj_grab(struct obd_device *obd, struct lu_fid *fid)
228 {
229         struct lmv_obj *obj;
230         ENTRY;
231         
232         spin_lock(&obj_list_lock);
233         obj = __lmv_obj_grab(obd, fid);
234         spin_unlock(&obj_list_lock);
235         
236         RETURN(obj);
237 }
238
239 /* looks in objects list for an object that matches passed @fid. If it is not
240  * found -- creates it using passed @mea and puts onto list. */
241 static struct lmv_obj *
242 __lmv_obj_create(struct obd_device *obd, struct lu_fid *fid,
243                  struct lmv_stripe_md *mea)
244 {
245         struct lmv_obj *new, *obj;
246         ENTRY;
247
248         obj = lmv_obj_grab(obd, fid);
249         if (obj)
250                 RETURN(obj);
251
252         /* no such object yet, allocate and initialize it. */
253         new = lmv_obj_alloc(obd, fid, mea);
254         if (!new)
255                 RETURN(NULL);
256
257         /* check if someone create it already while we were dealing with
258          * allocating @obj. */
259         spin_lock(&obj_list_lock);
260         obj = __lmv_obj_grab(obd, fid);
261         if (obj) {
262                 /* someone created it already - put @obj and getting out. */
263                 lmv_obj_free(new);
264                 spin_unlock(&obj_list_lock);
265                 RETURN(obj);
266         }
267
268         __lmv_obj_add(new);
269         __lmv_obj_get(new);
270         
271         spin_unlock(&obj_list_lock);
272
273         CDEBUG(D_OTHER, "new obj in lmv cache: "DFID3"\n",
274                PFID3(fid));
275
276         RETURN(new);
277         
278 }
279
280 /* creates object from passed @fid and @mea. If @mea is NULL, it will be
281  * obtained from correct MDT and used for constructing the object. */
282 struct lmv_obj *
283 lmv_obj_create(struct obd_export *exp, struct lu_fid *fid,
284                struct lmv_stripe_md *mea)
285 {
286         struct obd_device *obd = exp->exp_obd;
287         struct lmv_obd *lmv = &obd->u.lmv;
288         struct ptlrpc_request *req = NULL;
289         struct lmv_obj *obj;
290         struct lustre_md md;
291         int mealen, rc, mds;
292         ENTRY;
293
294         CDEBUG(D_OTHER, "get mea for "DFID3" and create lmv obj\n",
295                PFID3(fid));
296
297         md.mea = NULL;
298         
299         if (mea == NULL) {
300                 __u64 valid;
301                 
302                 CDEBUG(D_OTHER, "mea isn't passed in, get it now\n");
303                 mealen = MEA_SIZE_LMV(lmv);
304                 
305                 /* time to update mea of parent fid */
306                 md.mea = NULL;
307                 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
308
309                 mds = lmv_fld_lookup(obd, fid);
310
311                 rc = md_getattr(lmv->tgts[mds].ltd_exp, fid, valid, mealen, &req);
312                 if (rc) {
313                         CERROR("md_getattr() failed, error %d\n", rc);
314                         GOTO(cleanup, obj = ERR_PTR(rc));
315                 }
316
317                 rc = md_get_lustre_md(exp, req, 0, NULL, &md);
318                 if (rc) {
319                         CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
320                         GOTO(cleanup, obj = ERR_PTR(rc));
321                 }
322
323                 if (md.mea == NULL)
324                         GOTO(cleanup, obj = ERR_PTR(-ENODATA));
325                         
326                 mea = md.mea;
327         }
328
329         /* got mea, now create obj for it. */
330         obj = __lmv_obj_create(obd, fid, mea);
331         if (!obj) {
332                 CERROR("Can't create new object "DFID3"\n",
333                        PFID3(fid));
334                 GOTO(cleanup, obj = ERR_PTR(-ENOMEM));
335         }
336         
337         if (md.mea != NULL)
338                 obd_free_memmd(exp, (struct lov_stripe_md **)&md.mea);
339         
340         EXIT;
341 cleanup:
342         if (req)
343                 ptlrpc_req_finished(req);
344         return obj;
345 }
346
347 /*
348  * looks for object with @fid and orders to destroy it. It is possible the object
349  * will not be destroyed right now, because it is still using by someone. In
350  * this case it will be marked as "freeing" and will not be accessible anymore
351  * for subsequent callers of lmv_obj_grab().
352  */
353 int
354 lmv_obj_delete(struct obd_export *exp, struct lu_fid *fid)
355 {
356         struct obd_device *obd = exp->exp_obd;
357         struct lmv_obj *obj;
358         int rc = 0;
359         ENTRY;
360
361         spin_lock(&obj_list_lock);
362         obj = __lmv_obj_grab(obd, fid);
363         if (obj) {
364                 obj->lo_state |= O_FREEING;
365                 __lmv_obj_put(obj);
366                 __lmv_obj_put(obj);
367                 rc = 1;
368         }
369         spin_unlock(&obj_list_lock);
370
371         RETURN(rc);
372 }
373
374 int
375 lmv_mgr_setup(struct obd_device *obd)
376 {
377         ENTRY;
378         LASSERT(obd != NULL);
379         
380         CDEBUG(D_INFO, "LMV object manager setup (%s)\n",
381                obd->obd_uuid.uuid);
382
383         RETURN(0);
384 }
385
386 void
387 lmv_mgr_cleanup(struct obd_device *obd)
388 {
389         struct list_head *cur, *tmp;
390         struct lmv_obj *obj;
391         ENTRY;
392
393         CDEBUG(D_INFO, "LMV object manager cleanup (%s)\n",
394                obd->obd_uuid.uuid);
395         
396         spin_lock(&obj_list_lock);
397         list_for_each_safe(cur, tmp, &obj_list) {
398                 obj = list_entry(cur, struct lmv_obj, lo_list);
399                 
400                 if (obj->lo_obd != obd)
401                         continue;
402
403                 obj->lo_state |= O_FREEING;
404                 if (atomic_read(&obj->lo_count) > 1) {
405                         CERROR("obj "DFID3" has count > 1 (%d)\n",
406                                PFID3(&obj->lo_fid), atomic_read(&obj->lo_count));
407                 }
408                 __lmv_obj_put(obj);
409         }
410         spin_unlock(&obj_list_lock);
411         EXIT;
412 }