Whamcloud - gitweb
Fixes and cleanups in lmv.
[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
52 LIST_HEAD(lmv_obj_list);
53 spinlock_t lmv_obj_list_lock = SPIN_LOCK_UNLOCKED;
54
55 struct lmv_obj *lmv_grab_obj(struct obd_device *obd,
56                              struct ll_fid *fid, int create)
57 {
58         struct lmv_obd *lmv = &obd->u.lmv;
59         struct list_head *cur;
60         struct lmv_obj *obj, *obj2;
61
62         spin_lock(&lmv_obj_list_lock);
63         list_for_each(cur, &lmv_obj_list) {
64                 obj = list_entry(cur, struct lmv_obj, list);
65                 if (obj->fid.mds == fid->mds && obj->fid.id == fid->id &&
66                                 obj->fid.generation == fid->generation) {
67                         atomic_inc(&obj->count);
68                         spin_unlock(&lmv_obj_list_lock);
69                         RETURN(obj);
70                 }
71         }
72         spin_unlock(&lmv_obj_list_lock);
73
74         if (!create)
75                 RETURN(NULL);
76
77         /* no such object yet, allocate and initialize them */
78         OBD_ALLOC(obj, sizeof(*obj));
79         if (!obj)
80                 RETURN(NULL);
81         atomic_set(&obj->count, 0);
82         obj->fid = *fid;
83         obj->obd = obd;
84
85         OBD_ALLOC(obj->objs, sizeof(struct lmv_inode) * lmv->desc.ld_tgt_count);
86         if (!obj->objs) {
87                 OBD_FREE(obj, sizeof(*obj));
88                 RETURN(NULL);
89         }
90         memset(obj->objs, 0,  sizeof(struct lmv_inode) * lmv->desc.ld_tgt_count);
91
92         spin_lock(&lmv_obj_list_lock);
93         list_for_each(cur, &lmv_obj_list) {
94                 obj2 = list_entry(cur, struct lmv_obj, list);
95                 if (obj2->fid.mds == fid->mds && obj2->fid.id == fid->id &&
96                                 obj2->fid.generation == fid->generation) {
97                         /* someone created it already */
98                         OBD_FREE(obj->objs,
99                                   sizeof(struct lmv_inode) * lmv->desc.ld_tgt_count);
100                         OBD_FREE(obj, sizeof(*obj));
101
102                         atomic_inc(&obj2->count);
103                         spin_unlock(&lmv_obj_list_lock);
104                         RETURN(obj2);
105                 }
106         }
107         list_add(&obj->list, &lmv_obj_list);
108         CDEBUG(D_OTHER, "new obj in lmv cache: %lu/%lu/%lu\n",
109                (unsigned long) fid->mds, (unsigned long) fid->id,
110                (unsigned long) fid->generation);
111         spin_unlock(&lmv_obj_list_lock);
112
113         RETURN(obj);
114         
115 }
116
117 void lmv_put_obj(struct lmv_obj *obj)
118 {
119         if (!obj)
120                 return;
121         if (atomic_dec_and_test(&obj->count)) {
122                 CDEBUG(D_OTHER, "last reference to %lu/%lu/%lu\n",
123                        (unsigned long) obj->fid.mds,
124                        (unsigned long) obj->fid.id,
125                        (unsigned long) obj->fid.generation);
126         }
127 }
128
129 void lmv_cleanup_objs(struct obd_device *obd)
130 {
131         struct lmv_obd *lmv = &obd->u.lmv;
132         struct list_head *cur, *tmp;
133         struct lmv_obj *obj;
134
135         spin_lock(&lmv_obj_list_lock);
136         list_for_each_safe(cur, tmp, &lmv_obj_list) {
137                 obj = list_entry(cur, struct lmv_obj, list);
138                 if (obj->obd != obd)
139                         continue;
140
141                 list_del(&obj->list);
142                 OBD_FREE(obj->objs,
143                          sizeof(struct lmv_inode) * lmv->desc.ld_tgt_count);
144                 OBD_FREE(obj, sizeof(*obj));
145         }
146         spin_unlock(&lmv_obj_list_lock);
147 }
148
149 int lmv_create_obj_from_attrs(struct obd_export *exp,
150                               struct ll_fid *fid, struct mea *mea)
151 {
152         struct obd_device *obd = exp->exp_obd;
153         struct lmv_obd *lmv = &obd->u.lmv;
154         struct ptlrpc_request *req = NULL;
155         struct lmv_obj *obj;
156         struct lustre_md md;
157         int mealen, i, rc = 0;
158         ENTRY;
159
160         CDEBUG(D_OTHER, "get mea for %lu/%lu/%lu and create lmv obj\n",
161                (unsigned long) fid->mds, (unsigned long) fid->id,
162                (unsigned long) fid->generation);
163
164         if (!mea) {
165                 unsigned long valid;
166                 
167                 CDEBUG(D_OTHER, "mea isn't passed in, get it now\n");
168                 mealen = MEA_SIZE_LMV(lmv);
169                 
170                 /* time to update mea of parent fid */
171                 i = fid->mds;
172                 md.mea = NULL;
173                 
174                 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
175                 rc = md_getattr(lmv->tgts[fid->mds].ltd_exp, fid,
176                                 valid, mealen, &req);
177                 if (rc) {
178                         CERROR("md_getattr() failed, rc = %d\n", rc);
179                         GOTO(cleanup, rc);
180                 }
181
182                 rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
183                 if (rc) {
184                         CERROR("mdc_req2lustre_md() failed, rc = %d\n", rc);
185                         GOTO(cleanup, rc);
186                 }
187
188                 if (md.mea == NULL)
189                         GOTO(cleanup, rc = -ENODATA);
190                         
191                 mea = md.mea;
192         }
193
194         /* got mea, now create obj for it */
195         obj = lmv_grab_obj(obd, fid, 1);
196         if (!obj)
197                 GOTO(cleanup, rc = -ENOMEM);
198
199         obj->objcount = mea->mea_count;
200         /* put all fids in */
201         for (i = 0; i < mea->mea_count; i++) {
202                 CDEBUG(D_OTHER, "subobj %lu/%lu/%lu\n",
203                        (unsigned long) mea->mea_fids[i].mds,
204                        (unsigned long) mea->mea_fids[i].id,
205                        (unsigned long) mea->mea_fids[i].generation);
206                 obj->objs[i].fid = mea->mea_fids[i];
207         }
208
209 cleanup:
210         if (req)       
211                 ptlrpc_req_finished(req);
212         RETURN(rc); 
213 }
214
215