Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[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 #else
34 #include <liblustre.h>
35 #endif
36
37 #include <linux/obd_support.h>
38 #include <linux/lustre_lib.h>
39 #include <linux/lustre_net.h>
40 #include <linux/lustre_idl.h>
41 #include <linux/lustre_dlm.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/obd_class.h>
44 #include <linux/obd_ost.h>
45 #include <linux/seq_file.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->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->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->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->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                 CDEBUG(D_OTHER, "mea isn't passed in, get it now\n");
166                 mealen = MEA_SIZE_LMV(lmv);
167                 /* time to update mea of parent fid */
168                 i = fid->mds;
169                 rc = md_getattr(lmv->tgts[fid->mds].exp, fid,
170                                 OBD_MD_FLEASIZE, mealen, &req);
171                 LASSERT(rc == 0);
172                 md.mea = NULL;
173                 rc = mdc_req2lustre_md(req, 0, NULL, exp, &md);
174                 LASSERT(rc == 0);
175                 LASSERT(md.mea != NULL);
176                 mea = md.mea;
177         }
178
179         /* got mea, now create obj for it */
180         obj = lmv_grab_obj(obd, fid, 1);
181         if (!obj)
182                 GOTO(cleanup, rc = -ENOMEM);
183
184         obj->objcount = mea->mea_count;
185         /* put all fids in */
186         for (i = 0; i < mea->mea_count; i++) {
187                 CDEBUG(D_OTHER, "subobj %lu/%lu/%lu\n",
188                        (unsigned long) mea->mea_fids[i].mds,
189                        (unsigned long) mea->mea_fids[i].id,
190                        (unsigned long) mea->mea_fids[i].generation);
191                 obj->objs[i].fid = mea->mea_fids[i];
192         }
193
194 cleanup:
195         if (req)       
196                 ptlrpc_req_finished(req);
197         RETURN(rc); 
198 }
199
200