Whamcloud - gitweb
land b_ost_amd onto HEAD.
[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 <portals/list.h>
26
27 /* helper functions for calling the llog obd methods */
28
29 int obd_llog_setup(struct obd_device *obd, struct obd_llogs *llogs, 
30                    int index, struct obd_device *disk_obd, int count, 
31                    struct llog_logid *logid, struct llog_operations *op)
32 {
33         int rc = 0;
34         struct llog_ctxt *ctxt;
35         ENTRY;
36
37         LASSERT(llogs);
38
39         if (index < 0 || index >= LLOG_MAX_CTXTS)
40                 RETURN(-EFAULT);
41
42         OBD_ALLOC(ctxt, sizeof(*ctxt));
43         if (!ctxt)
44                 RETURN(-ENOMEM);
45
46         llogs->llog_ctxt[index] = ctxt;
47         obd->obd_llog_ctxt[index] = ctxt;
48         ctxt->loc_logops = op;
49         ctxt->loc_obd = obd;
50         ctxt->loc_llogs = llogs;
51         ctxt->loc_idx = index;
52         ctxt->loc_alone = 0;
53         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
54         sema_init(&ctxt->loc_sem, 1);
55
56         if (op->lop_close == llog_lvfs_ops.lop_close) {
57                 ctxt->loc_fsops = disk_obd->obd_fsops;
58                 ctxt->loc_lvfs_ctxt = &disk_obd->obd_lvfs_ctxt;
59                 if (!strcmp(disk_obd->obd_type->typ_name, LUSTRE_MDS_NAME)) {
60                         struct mds_obd *mds = &disk_obd->u.mds;
61                         ctxt->loc_objects_dir = mds->mds_objects_dir;
62                         ctxt->loc_logs_dir = mds->mds_logs_dir;
63                 }
64         }
65
66         if (op->lop_setup)
67                 rc = op->lop_setup(obd, llogs, index, disk_obd, count, logid);
68         if (ctxt && rc) {
69                 obd->obd_llog_ctxt[index] = NULL;
70                 OBD_FREE(ctxt, sizeof(*ctxt));
71         }
72
73         RETURN(rc);
74 }
75 EXPORT_SYMBOL(obd_llog_setup);
76
77 int obd_llog_cleanup(struct llog_ctxt *ctxt)
78 {
79         int rc = 0;
80         ENTRY;
81
82         if (ctxt == NULL)
83                 RETURN(0);
84
85         if (CTXTP(ctxt, cleanup))  
86                 rc = CTXTP(ctxt, cleanup)(ctxt);
87                 
88         ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
89         ctxt->loc_llogs->llog_ctxt[ctxt->loc_idx] = NULL;
90         class_export_put(ctxt->loc_exp);
91         ctxt->loc_exp = NULL;
92         OBD_FREE(ctxt, sizeof(*ctxt));
93         RETURN(rc);
94 }
95 EXPORT_SYMBOL(obd_llog_cleanup);
96
97 /* callback func for llog_process in llog_obd_origin_setup */
98 static int cat_cancel_cb(struct llog_handle *cathandle,
99                           struct llog_rec_hdr *rec, void *data)
100 {
101         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
102         struct llog_handle *loghandle;
103         struct llog_log_hdr *llh;
104         int rc, index;
105         ENTRY;
106
107         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
108                 CERROR("invalid record in catalog\n");
109                 RETURN(-EINVAL);
110         }
111         CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
112                lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
113                rec->lrh_index, cathandle->lgh_id.lgl_oid);
114
115         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
116         if (rc) {
117                 CERROR("Cannot find handle for log "LPX64"\n",
118                        lir->lid_id.lgl_oid);
119                 RETURN(rc);
120         }
121
122         llh = loghandle->lgh_hdr;
123         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
124             (llh->llh_count == 1)) {
125                 rc = llog_destroy(loghandle);
126                 if (rc)
127                         CERROR("failure destroying log in postsetup: %d\n", rc);
128                 LASSERT(rc == 0);
129
130                 index = loghandle->u.phd.phd_cookie.lgc_index;
131                 llog_free_handle(loghandle);
132
133                 LASSERT(index);
134                 llog_cat_set_first_idx(cathandle, index);
135                 rc = llog_cancel_rec(cathandle, index);
136                 if (rc == 0)
137                         CWARN("cancel log "LPX64":%x at index %u of catalog "
138                               LPX64"\n", lir->lid_id.lgl_oid,
139                               lir->lid_id.lgl_ogen, rec->lrh_index,
140                               cathandle->lgh_id.lgl_oid);
141         }
142
143         RETURN(rc);
144 }
145
146 /* lop_setup method for filter/osc */
147 // XXX how to set exports
148 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llogs *llogs,
149                           int index, struct obd_device *disk_obd, int count,
150                           struct llog_logid *logid)
151 {
152         struct llog_ctxt *ctxt;
153         struct llog_handle *handle;
154         struct lvfs_run_ctxt saved;
155         int rc;
156         ENTRY;
157
158         if (count == 0)
159                 RETURN(0);
160
161         LASSERT(count == 1);
162
163         ctxt = llog_get_context(llogs, index);
164         LASSERT(ctxt);
165         llog_gen_init(ctxt);
166
167         if (logid->lgl_oid) {
168                 rc = llog_open(ctxt, &handle, logid, NULL, 0);
169         } else {
170                 rc = llog_open(ctxt, &handle, NULL, NULL, 0);
171                 if (!rc)
172                         *logid = handle->lgh_id;
173         }
174         if (rc)
175                 RETURN(rc);
176
177         ctxt->loc_handle = handle;
178         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
179         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
180         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
181         if (rc)
182                 RETURN(rc);
183
184         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
185         if (rc)
186                 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
187
188         if (ctxt && rc) {
189                 llogs->llog_ctxt[index] = NULL;
190                 OBD_FREE(ctxt, sizeof(*ctxt));
191         }
192         RETURN(rc);
193 }
194 EXPORT_SYMBOL(llog_obd_origin_setup);
195
196 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
197 {
198         struct llog_handle *cathandle, *n, *loghandle;
199         struct llog_log_hdr *llh;
200         int rc, index;
201         ENTRY;
202
203         if (!ctxt)
204                 return 0;
205
206         cathandle = ctxt->loc_handle;
207         if (cathandle) {
208                 list_for_each_entry_safe(loghandle, n,
209                                          &cathandle->u.chd.chd_head,
210                                          u.phd.phd_entry) {
211                         llh = loghandle->lgh_hdr;
212                         if ((llh->llh_flags &
213                                 LLOG_F_ZAP_WHEN_EMPTY) &&
214                             (llh->llh_count == 1)) {
215                                 rc = llog_destroy(loghandle);
216                                 if (rc)
217                                         CERROR("failure destroying log during "
218                                                "cleanup: %d\n", rc);
219                                 LASSERT(rc == 0);
220
221                                 index = loghandle->u.phd.phd_cookie.lgc_index;
222                                 llog_free_handle(loghandle);
223
224                                 LASSERT(index);
225                                 llog_cat_set_first_idx(cathandle, index);
226                                 rc = llog_cancel_rec(cathandle, index);
227                                 if (rc == 0)
228                                         CDEBUG(D_HA, "cancel plain log at index"
229                                                " %u of catalog "LPX64"\n",
230                                                index,cathandle->lgh_id.lgl_oid);
231                         }
232                 }
233                 llog_cat_put(ctxt->loc_handle);
234         }
235         return 0;
236 }
237 EXPORT_SYMBOL(llog_obd_origin_cleanup);
238
239 /* add for obdfilter/sz and mds/unlink */
240 int llog_obd_origin_add(struct llog_ctxt *ctxt,
241                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
242                         struct llog_cookie *logcookies, int numcookies)
243 {
244         struct llog_handle *cathandle;
245         int rc;
246         ENTRY;
247
248         cathandle = ctxt->loc_handle;
249         LASSERT(cathandle != NULL);
250         rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
251         if (rc != 1)
252                 CERROR("write one catalog record failed: %d\n", rc);
253         RETURN(rc);
254 }
255 EXPORT_SYMBOL(llog_obd_origin_add);
256
257 int obd_llog_cat_initialize(struct obd_device *obd, struct obd_llogs *llogs, 
258                             int count, char *name)
259 {
260         struct llog_catid *idarray;
261         int size = sizeof(*idarray) * count;
262         int rc;
263         ENTRY;
264
265         OBD_ALLOC(idarray, size);
266         if (!idarray)
267                 RETURN(-ENOMEM);
268
269         rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
270                                name, count, idarray);
271         if (rc) {
272                 CERROR("rc: %d\n", rc);
273                 GOTO(out, rc);
274         }
275
276         rc = obd_llog_init(obd, llogs, obd, count, idarray);
277         if (rc) {
278                 CERROR("rc: %d\n", rc);
279                 GOTO(out, rc);
280         }
281
282         rc = llog_put_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
283                                name, count, idarray);
284         if (rc) {
285                 CERROR("rc: %d\n", rc);
286                 GOTO(out, rc);
287         }
288
289  out:
290         OBD_FREE(idarray, size);
291         RETURN(rc);
292 }
293 EXPORT_SYMBOL(obd_llog_cat_initialize);
294
295 int obd_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
296                   struct obd_device *disk_obd, int count,
297                   struct llog_catid *logid)
298 {
299         int rc;
300         ENTRY;
301         OBD_CHECK_OP(obd, llog_init, 0);
302         OBD_COUNTER_INCREMENT(obd, llog_init);
303
304         rc = OBP(obd, llog_init)(obd, llogs, disk_obd, count, logid);
305         RETURN(rc);
306 }
307 EXPORT_SYMBOL(obd_llog_init);
308
309 int obd_llog_finish(struct obd_device *obd, struct obd_llogs *llogs, int count)
310 {
311         int rc;
312         ENTRY;
313         OBD_CHECK_OP(obd, llog_finish, 0);
314         OBD_COUNTER_INCREMENT(obd, llog_finish);
315
316         rc = OBP(obd, llog_finish)(obd, llogs, count);
317         RETURN(rc);
318 }
319 EXPORT_SYMBOL(obd_llog_finish);