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