Whamcloud - gitweb
landing smfs.
[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 <portals/list.h>
25
26 /* helper functions for calling the llog obd methods */
27
28 int obd_llog_setup(struct obd_device *obd, int index,
29                    struct obd_device *disk_obd, int count,
30                    struct llog_logid *logid, struct llog_operations *op)
31 {
32         int rc = 0;
33         struct llog_ctxt *ctxt;
34         ENTRY;
35
36         if (index < 0 || index >= LLOG_MAX_CTXTS)
37                 RETURN(-EFAULT);
38
39         OBD_ALLOC(ctxt, sizeof(*ctxt));
40         if (!ctxt)
41                 RETURN(-ENOMEM);
42
43         obd->obd_llog_ctxt[index] = ctxt;
44         ctxt->loc_logops = op;
45         ctxt->loc_obd = obd;
46         ctxt->loc_idx = index;
47         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
48         sema_init(&ctxt->loc_sem, 1);
49
50         if (op->lop_close == llog_lvfs_ops.lop_close) {
51                 ctxt->loc_fsops = disk_obd->obd_fsops;
52                 ctxt->loc_lvfs_ctxt = &disk_obd->obd_lvfs_ctxt;
53                 if (!strcmp(disk_obd->obd_type->typ_name, "mds")) {
54                         struct mds_obd *mds = &disk_obd->u.mds;
55                         ctxt->loc_objects_dir = mds->mds_objects_dir;
56                         ctxt->loc_logs_dir = mds->mds_logs_dir;
57                 }
58         }
59
60         if (op->lop_setup)
61                 rc = op->lop_setup(obd, index, disk_obd, count, logid);
62         if (ctxt && rc) {
63                 obd->obd_llog_ctxt[index] = NULL;
64                 OBD_FREE(ctxt, sizeof(*ctxt));
65         }
66
67         RETURN(rc);
68 }
69 EXPORT_SYMBOL(obd_llog_setup);
70
71 int obd_llog_cleanup(struct llog_ctxt *ctxt)
72 {
73         int rc = 0;
74         ENTRY;
75
76         LASSERT(ctxt);
77
78         ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
79         class_export_put(ctxt->loc_exp);
80         ctxt->loc_exp = NULL;
81
82         if (CTXTP(ctxt, cleanup)) 
83                 rc = CTXTP(ctxt, cleanup)(ctxt);
84         else
85                 OBD_FREE(ctxt, sizeof(*ctxt));
86
87         RETURN(rc);
88 }
89 EXPORT_SYMBOL(obd_llog_cleanup);
90
91 /* callback func for llog_process in llog_obd_origin_setup */
92 static int cat_cancel_cb(struct llog_handle *cathandle,
93                           struct llog_rec_hdr *rec, void *data)
94 {
95         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
96         struct llog_handle *loghandle;
97         struct llog_log_hdr *llh;
98         int rc, index;
99         ENTRY;
100
101         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
102                 CERROR("invalid record in catalog\n");
103                 RETURN(-EINVAL);
104         }
105         CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
106                lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
107                rec->lrh_index, cathandle->lgh_id.lgl_oid);
108
109         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
110         if (rc) {
111                 CERROR("Cannot find handle for log "LPX64"\n",
112                        lir->lid_id.lgl_oid);
113                 RETURN(rc);
114         }
115
116         llh = loghandle->lgh_hdr;
117         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
118             (llh->llh_count == 1)) {
119                 rc = llog_destroy(loghandle);
120                 if (rc)
121                         CERROR("failure destroying log in postsetup: %d\n", rc);
122                 LASSERT(rc == 0);
123
124                 index = loghandle->u.phd.phd_cookie.lgc_index;
125                 llog_free_handle(loghandle);
126
127                 LASSERT(index);
128                 llog_cat_set_first_idx(cathandle, index);
129                 rc = llog_cancel_rec(cathandle, index);
130                 if (rc == 0)
131                         CWARN("cancel log "LPX64":%x at index %u of catalog "
132                               LPX64"\n", lir->lid_id.lgl_oid,
133                               lir->lid_id.lgl_ogen, rec->lrh_index,
134                               cathandle->lgh_id.lgl_oid);
135         }
136
137         RETURN(rc);
138 }
139
140 /* lop_setup method for filter/osc */
141 // XXX how to set exports
142 int llog_obd_origin_setup(struct obd_device *obd, int index,
143                           struct obd_device *disk_obd, int count,
144                           struct llog_logid *logid)
145 {
146         struct llog_ctxt *ctxt;
147         struct llog_handle *handle;
148         struct lvfs_run_ctxt saved;
149         int rc;
150         ENTRY;
151
152         if (count == 0)
153                 RETURN(0);
154
155         LASSERT(count == 1);
156
157         ctxt = llog_get_context(obd, index);
158         LASSERT(ctxt);
159         llog_gen_init(ctxt);
160
161         if (logid->lgl_oid)
162                 rc = llog_create(ctxt, &handle, logid, NULL);
163         else {
164                 rc = llog_create(ctxt, &handle, NULL, NULL);
165                 if (!rc)
166                         *logid = handle->lgh_id;
167         }
168         if (rc)
169                 RETURN(rc);
170
171         ctxt->loc_handle = handle;
172         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
173         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
174         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
175         if (rc)
176                 RETURN(rc);
177
178         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
179         if (rc)
180                 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
181         RETURN(rc);
182 }
183 EXPORT_SYMBOL(llog_obd_origin_setup);
184
185 int obd_llog_cat_initialize(struct obd_device *obd, int count, char *name)
186 {
187         struct llog_catid *idarray;
188         int size = sizeof(*idarray) * count;
189         int rc;
190         ENTRY;
191
192         OBD_ALLOC(idarray, size);
193         if (!idarray)
194                 RETURN(-ENOMEM);
195
196         rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
197                                name, count, idarray);
198         if (rc) {
199                 CERROR("rc: %d\n", rc);
200                 GOTO(out, rc);
201         }
202
203         rc = obd_llog_init(obd, obd, count, idarray);
204         if (rc) {
205                 CERROR("rc: %d\n", rc);
206                 GOTO(out, rc);
207         }
208
209         rc = llog_put_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
210                                name, count, idarray);
211         if (rc) {
212                 CERROR("rc: %d\n", rc);
213                 GOTO(out, rc);
214         }
215
216  out:
217         OBD_FREE(idarray, size);
218         RETURN(rc);
219 }
220 EXPORT_SYMBOL(obd_llog_cat_initialize);
221
222 int obd_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
223                   int count, struct llog_catid *logid)
224 {
225         int rc;
226         ENTRY;
227         OBD_CHECK_OP(obd, llog_init, 0);
228         OBD_COUNTER_INCREMENT(obd, llog_init);
229
230         rc = OBP(obd, llog_init)(obd, disk_obd, count, logid);
231         RETURN(rc);
232 }
233 EXPORT_SYMBOL(obd_llog_init);
234
235 int obd_llog_finish(struct obd_device *obd, int count)
236 {
237         int rc;
238         ENTRY;
239         OBD_CHECK_OP(obd, llog_finish, 0);
240         OBD_COUNTER_INCREMENT(obd, llog_finish);
241
242         rc = OBP(obd, llog_finish)(obd, count);
243         RETURN(rc);
244 }
245 EXPORT_SYMBOL(obd_llog_finish);