Whamcloud - gitweb
- cleanups in cmobd and others:
[fs/lustre-release.git] / lustre / obdclass / llog_obd.c
1
2 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
3  * vim:expandtab:shiftwidth=8:tabstop=8:
4  *
5  *   You should have received a copy of the GNU General Public License
6  *   along with Lustre; if not, write to the Free Software
7  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
8  */
9
10 #define DEBUG_SUBSYSTEM S_LOG
11
12 #ifndef EXPORT_SYMTAB
13 #define EXPORT_SYMTAB
14 #endif
15
16 #ifdef __KERNEL__
17 #include <linux/fs.h>
18 #else
19 #include <liblustre.h>
20 #endif
21
22 #include <linux/obd_class.h>
23 #include <linux/lustre_log.h>
24 #include <linux/lustre_mds.h>
25 #include <libcfs/list.h>
26
27 /* helper functions for calling the llog obd methods */
28 int obd_llog_setup(struct obd_device *obd, struct obd_llogs *llogs, 
29                    int index, struct obd_device *disk_obd, int count, 
30                    struct llog_logid *logid, struct llog_operations *op)
31 {
32         struct llog_ctxt *ctxt;
33         int rc = 0;
34         ENTRY;
35
36         LASSERT(llogs);
37
38         if (index < 0 || index >= LLOG_MAX_CTXTS)
39                 RETURN(-EFAULT);
40
41         OBD_ALLOC(ctxt, sizeof(*ctxt));
42         if (!ctxt)
43                 RETURN(-ENOMEM);
44
45         llogs->llog_ctxt[index] = ctxt;
46         obd->obd_llog_ctxt[index] = ctxt;
47         ctxt->loc_logops = op;
48         ctxt->loc_obd = obd;
49         ctxt->loc_llogs = llogs;
50         ctxt->loc_idx = index;
51         ctxt->loc_alone = 0;
52         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
53         sema_init(&ctxt->loc_sem, 1);
54
55         if (op->lop_close == llog_lvfs_ops.lop_close) {
56                 ctxt->loc_fsops = disk_obd->obd_fsops;
57                 ctxt->loc_lvfs_ctxt = &disk_obd->obd_lvfs_ctxt;
58                 if (!strcmp(disk_obd->obd_type->typ_name, OBD_MDS_DEVICENAME)) {
59                         struct mds_obd *mds = &disk_obd->u.mds;
60                         ctxt->loc_objects_dir = mds->mds_objects_dir;
61                         ctxt->loc_logs_dir = mds->mds_logs_dir;
62                 } else if (!strcmp(disk_obd->obd_type->typ_name, OBD_CONF_DEVICENAME)) {
63                         struct conf_obd *confobd = &disk_obd->u.conf;
64                         ctxt->loc_objects_dir = confobd->cfobd_objects_dir;
65                         ctxt->loc_logs_dir = confobd->cfobd_logs_dir;
66                 }
67         }
68
69         if (op->lop_setup)
70                 rc = op->lop_setup(obd, llogs, index, disk_obd, count, logid);
71         if (ctxt && rc) {
72                 obd->obd_llog_ctxt[index] = NULL;
73                 OBD_FREE(ctxt, sizeof(*ctxt));
74         }
75
76         RETURN(rc);
77 }
78 EXPORT_SYMBOL(obd_llog_setup);
79
80 int obd_llog_cleanup(struct llog_ctxt *ctxt)
81 {
82         int rc = 0;
83         ENTRY;
84
85         if (ctxt == NULL)
86                 RETURN(0);
87
88         if (CTXTP(ctxt, cleanup))  
89                 rc = CTXTP(ctxt, cleanup)(ctxt);
90                 
91         ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
92         ctxt->loc_llogs->llog_ctxt[ctxt->loc_idx] = NULL;
93         class_export_put(ctxt->loc_exp);
94         ctxt->loc_exp = NULL;
95         OBD_FREE(ctxt, sizeof(*ctxt));
96         RETURN(rc);
97 }
98 EXPORT_SYMBOL(obd_llog_cleanup);
99
100 /* callback func for llog_process in llog_obd_origin_setup */
101 static int cat_cancel_cb(struct llog_handle *cathandle,
102                           struct llog_rec_hdr *rec, void *data)
103 {
104         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
105         struct llog_handle *loghandle;
106         int rc;
107         ENTRY;
108
109         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
110                 CERROR("invalid record in catalog\n");
111                 RETURN(-EINVAL);
112         }
113         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "LPX64"\n",
114                lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
115                rec->lrh_index, cathandle->lgh_id.lgl_oid);
116
117         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
118         if (rc) {
119                 CERROR("Cannot find handle for log "LPX64"\n",
120                        lir->lid_id.lgl_oid);
121                 RETURN(rc);
122         }
123
124         if (cathandle->lgh_last_idx == loghandle->u.phd.phd_cookie.lgc_index)
125                 cathandle->u.chd.chd_current_log = loghandle;
126
127         RETURN(rc);
128 }
129
130 /* lop_setup method for filter/osc */
131 // XXX how to set exports
132 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llogs *llogs,
133                           int index, struct obd_device *disk_obd, int count,
134                           struct llog_logid *logid)
135 {
136         struct llog_ctxt *ctxt;
137         struct llog_handle *handle;
138         struct lvfs_run_ctxt saved;
139         int rc;
140         ENTRY;
141
142         if (count == 0)
143                 RETURN(0);
144
145         LASSERT(count == 1);
146
147         ctxt = llog_get_context(llogs, index);
148         LASSERT(ctxt);
149         llog_gen_init(ctxt);
150
151         if (logid->lgl_oid) {
152                 rc = llog_open(ctxt, &handle, logid, NULL, 0);
153         } else {
154                 rc = llog_open(ctxt, &handle, NULL, NULL, 0);
155                 if (!rc)
156                         *logid = handle->lgh_id;
157         }
158         if (rc)
159                 RETURN(rc);
160
161         ctxt->loc_handle = handle;
162         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
163         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
164         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
165         if (rc)
166                 RETURN(rc);
167
168         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
169         if (rc)
170                 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
171
172         if (ctxt && rc) {
173                 llogs->llog_ctxt[index] = NULL;
174                 OBD_FREE(ctxt, sizeof(*ctxt));
175         }
176         RETURN(rc);
177 }
178 EXPORT_SYMBOL(llog_obd_origin_setup);
179
180 int obd_llog_cat_initialize(struct obd_device *obd, struct obd_llogs *llogs, 
181                             int count, const char *name)
182 {
183         struct llog_catid *idarray;
184         int size = sizeof(*idarray) * count;
185         int rc;
186         ENTRY;
187
188         OBD_ALLOC(idarray, size);
189         if (!idarray)
190                 RETURN(-ENOMEM);
191
192         rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
193                                name, count, idarray);
194         if (rc) {
195                 CERROR("rc: %d\n", rc);
196                 GOTO(out, rc);
197         }
198
199         rc = obd_llog_init(obd, llogs, obd, count, idarray);
200         if (rc) {
201                 CERROR("rc: %d\n", rc);
202                 GOTO(out, rc);
203         }
204
205         rc = llog_put_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
206                                name, count, idarray);
207         if (rc) {
208                 CERROR("rc: %d\n", rc);
209                 GOTO(out, rc);
210         }
211
212  out:
213         OBD_FREE(idarray, size);
214         RETURN(rc);
215 }
216 EXPORT_SYMBOL(obd_llog_cat_initialize);
217
218 int obd_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
219                   struct obd_device *disk_obd, int count,
220                   struct llog_catid *logid)
221 {
222         int rc;
223         ENTRY;
224         OBD_CHECK_OP(obd, llog_init, 0);
225         OBD_COUNTER_INCREMENT(obd, llog_init);
226
227         rc = OBP(obd, llog_init)(obd, llogs, disk_obd, count, logid);
228         RETURN(rc);
229 }
230 EXPORT_SYMBOL(obd_llog_init);
231
232 int obd_llog_finish(struct obd_device *obd, struct obd_llogs *llogs, int count)
233 {
234         int rc;
235         ENTRY;
236         OBD_CHECK_OP(obd, llog_finish, 0);
237         OBD_COUNTER_INCREMENT(obd, llog_finish);
238
239         rc = OBP(obd, llog_finish)(obd, llogs, count);
240         RETURN(rc);
241 }
242 EXPORT_SYMBOL(obd_llog_finish);