Whamcloud - gitweb
b=3550
[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         int rc;
104         ENTRY;
105
106         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
107                 CERROR("invalid record in catalog\n");
108                 RETURN(-EINVAL);
109         }
110         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "LPX64"\n",
111                lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
112                rec->lrh_index, cathandle->lgh_id.lgl_oid);
113
114         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
115         if (rc) {
116                 CERROR("Cannot find handle for log "LPX64"\n",
117                        lir->lid_id.lgl_oid);
118                 RETURN(rc);
119         }
120
121         if (cathandle->lgh_last_idx == loghandle->u.phd.phd_cookie.lgc_index)
122                 cathandle->u.chd.chd_current_log = loghandle;
123
124         RETURN(rc);
125 }
126
127 /* lop_setup method for filter/osc */
128 // XXX how to set exports
129 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llogs *llogs,
130                           int index, struct obd_device *disk_obd, int count,
131                           struct llog_logid *logid)
132 {
133         struct llog_ctxt *ctxt;
134         struct llog_handle *handle;
135         struct lvfs_run_ctxt saved;
136         int rc;
137         ENTRY;
138
139         if (count == 0)
140                 RETURN(0);
141
142         LASSERT(count == 1);
143
144         ctxt = llog_get_context(llogs, index);
145         LASSERT(ctxt);
146         llog_gen_init(ctxt);
147
148         if (logid->lgl_oid) {
149                 rc = llog_open(ctxt, &handle, logid, NULL, 0);
150         } else {
151                 rc = llog_open(ctxt, &handle, NULL, NULL, 0);
152                 if (!rc)
153                         *logid = handle->lgh_id;
154         }
155         if (rc)
156                 RETURN(rc);
157
158         ctxt->loc_handle = handle;
159         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
160         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
161         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
162         if (rc)
163                 RETURN(rc);
164
165         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
166         if (rc)
167                 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
168
169         if (ctxt && rc) {
170                 llogs->llog_ctxt[index] = NULL;
171                 OBD_FREE(ctxt, sizeof(*ctxt));
172         }
173         RETURN(rc);
174 }
175 EXPORT_SYMBOL(llog_obd_origin_setup);
176
177 int obd_llog_cat_initialize(struct obd_device *obd, struct obd_llogs *llogs, 
178                             int count, char *name)
179 {
180         struct llog_catid *idarray;
181         int size = sizeof(*idarray) * count;
182         int rc;
183         ENTRY;
184
185         OBD_ALLOC(idarray, size);
186         if (!idarray)
187                 RETURN(-ENOMEM);
188
189         rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
190                                name, count, idarray);
191         if (rc) {
192                 CERROR("rc: %d\n", rc);
193                 GOTO(out, rc);
194         }
195
196         rc = obd_llog_init(obd, llogs, obd, count, idarray);
197         if (rc) {
198                 CERROR("rc: %d\n", rc);
199                 GOTO(out, rc);
200         }
201
202         rc = llog_put_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
203                                name, count, idarray);
204         if (rc) {
205                 CERROR("rc: %d\n", rc);
206                 GOTO(out, rc);
207         }
208
209  out:
210         OBD_FREE(idarray, size);
211         RETURN(rc);
212 }
213 EXPORT_SYMBOL(obd_llog_cat_initialize);
214
215 int obd_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
216                   struct obd_device *disk_obd, int count,
217                   struct llog_catid *logid)
218 {
219         int rc;
220         ENTRY;
221         OBD_CHECK_OP(obd, llog_init, 0);
222         OBD_COUNTER_INCREMENT(obd, llog_init);
223
224         rc = OBP(obd, llog_init)(obd, llogs, disk_obd, count, logid);
225         RETURN(rc);
226 }
227 EXPORT_SYMBOL(obd_llog_init);
228
229 int obd_llog_finish(struct obd_device *obd, struct obd_llogs *llogs, int count)
230 {
231         int rc;
232         ENTRY;
233         OBD_CHECK_OP(obd, llog_finish, 0);
234         OBD_COUNTER_INCREMENT(obd, llog_finish);
235
236         rc = OBP(obd, llog_finish)(obd, llogs, count);
237         RETURN(rc);
238 }
239 EXPORT_SYMBOL(obd_llog_finish);