1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * You should have received a copy of the GNU General Public License
5 * along with Lustre; if not, write to the Free Software
6 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
9 #define DEBUG_SUBSYSTEM S_LOG
16 #include <linux/obd_class.h>
17 #include <linux/lustre_log.h>
18 #include <portals/list.h>
19 #include "llog_internal.h"
21 static int str2logid(struct llog_logid *logid, char *str, int len)
23 char *start, *end, *endp;
30 if (start - str >= len - 1)
32 end = strchr(start, '#');
33 if (end == NULL || end == start)
37 logid->lgl_oid = simple_strtoull(start, &endp, 0);
42 if (start - str >= len - 1)
44 end = strchr(start, '#');
45 if (end == NULL || end == start)
49 logid->lgl_ogr = simple_strtoull(start, &endp, 0);
54 if (start - str >= len - 1)
56 logid->lgl_ogen = simple_strtoul(start, &endp, 16);
63 static int llog_check_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
66 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
67 static int l, remains, from, to;
70 int cur_index, rc = 0;
72 cur_index = rec->lrh_index;
74 if (ioc_data && (ioc_data->ioc_inllen1)) {
76 remains = ioc_data->ioc_inllen4 +
77 size_round(ioc_data->ioc_inllen1) +
78 size_round(ioc_data->ioc_inllen2) +
79 size_round(ioc_data->ioc_inllen3);
80 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
83 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
86 ioc_data->ioc_inllen1 = 0;
87 out = ioc_data->ioc_bulk;
90 if (to > 0 && cur_index > to)
93 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
94 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
95 struct llog_handle *log_handle;
97 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
98 l = snprintf(out, remains, "[index]: %05d [type]: "
99 "%02x [len]: %04d failed\n",
100 cur_index, rec->lrh_type,
103 if (handle->lgh_ctxt == NULL)
105 llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
106 rc = llog_process(log_handle, llog_check_cb, NULL, NULL);
107 llog_close(log_handle);
109 switch (rec->lrh_type) {
115 case LLOG_HDR_MAGIC: {
116 l = snprintf(out, remains, "[index]: %05d [type]: "
117 "%02x [len]: %04d ok\n",
118 cur_index, rec->lrh_type,
123 CERROR("no space to print log records\n");
124 RETURN(-LLOG_EEMPTY);
129 l = snprintf(out, remains, "[index]: %05d [type]: "
130 "%02x [len]: %04d failed\n",
131 cur_index, rec->lrh_type,
136 CERROR("no space to print log records\n");
137 RETURN(-LLOG_EEMPTY);
146 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
149 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
150 static int l, remains, from, to;
155 if (ioc_data->ioc_inllen1) {
157 remains = ioc_data->ioc_inllen4 +
158 size_round(ioc_data->ioc_inllen1) +
159 size_round(ioc_data->ioc_inllen2) +
160 size_round(ioc_data->ioc_inllen3);
161 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
164 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
167 out = ioc_data->ioc_bulk;
168 ioc_data->ioc_inllen1 = 0;
171 cur_index = rec->lrh_index;
172 if (cur_index < from)
174 if (to > 0 && cur_index > to)
175 RETURN(-LLOG_EEMPTY);
177 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
178 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
179 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
180 CERROR("invalid record in catalog\n");
184 l = snprintf(out, remains,
185 "[index]: %05d [logid]: #"LPX64"#"LPX64"#%08x\n",
186 cur_index, lir->lid_id.lgl_oid,
187 lir->lid_id.lgl_ogr, lir->lid_id.lgl_ogen);
189 l = snprintf(out, remains,
190 "[index]: %05d [type]: %02x [len]: %04d\n",
191 cur_index, rec->lrh_type,
197 CERROR("not enough space for print log records\n");
198 RETURN(-LLOG_EEMPTY);
203 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
205 struct llog_handle *log;
208 down_write(&cat->lgh_lock);
209 rc = llog_cat_id2handle(cat, &log, logid);
211 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
212 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen);
213 GOTO(out, rc = -ENOENT);
216 index = log->u.phd.phd_cookie.lgc_index;
218 rc = llog_destroy(log);
220 CDEBUG(D_IOCTL, "cannot destroy log\n");
223 llog_cat_set_first_idx(cat, index);
224 rc = llog_cancel_rec(cat, index);
226 llog_free_handle(log);
227 up_write(&cat->lgh_lock);
232 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
235 struct llog_logid_rec *lir = (struct llog_logid_rec*)rec;
238 if (rec->lrh_type != LLOG_LOGID_MAGIC)
240 rc = llog_remove_log(handle, &lir->lid_id);
246 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
248 struct llog_logid logid;
250 struct llog_handle *handle = NULL;
252 if (*data->ioc_inlbuf1 == '#') {
253 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
256 err = llog_create(ctxt, &handle, &logid, NULL);
259 } else if (*data->ioc_inlbuf1 == '$') {
260 char *name = data->ioc_inlbuf1 + 1;
261 err = llog_create(ctxt, &handle, NULL, name);
265 GOTO(out, err = -EINVAL);
268 err = llog_init_handle(handle, 0, NULL);
270 GOTO(out_close, err = -ENOENT);
273 case OBD_IOC_LLOG_INFO: {
275 int remains = data->ioc_inllen2 +
276 size_round(data->ioc_inllen1);
277 char *out = data->ioc_bulk;
279 l = snprintf(out, remains,
280 "logid: #"LPX64"#"LPX64"#%08x\n"
282 "records count: %d\n"
284 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_ogr,
285 handle->lgh_id.lgl_ogen,
286 handle->lgh_hdr->llh_flags,
287 handle->lgh_hdr->llh_flags &
288 LLOG_F_IS_CAT ? "cat" : "plain",
289 handle->lgh_hdr->llh_count,
290 handle->lgh_last_idx);
294 CERROR("not enough space for log header info\n");
296 GOTO(out_close, err);
298 case OBD_IOC_LLOG_CHECK: {
299 LASSERT(data->ioc_inllen1);
300 err = llog_process(handle, llog_check_cb, data, NULL);
301 if (err == -LLOG_EEMPTY)
303 GOTO(out_close, err);
306 case OBD_IOC_LLOG_PRINT: {
307 LASSERT(data->ioc_inllen1);
308 err = llog_process(handle, class_config_dump_handler,data,NULL);
309 if (err == -LLOG_EEMPTY)
312 err = llog_process(handle, llog_print_cb, data, NULL);
314 GOTO(out_close, err);
316 case OBD_IOC_LLOG_CANCEL: {
317 struct llog_cookie cookie;
318 struct llog_logid plain;
321 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
323 GOTO(out_close, err = -EINVAL);
325 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
326 down_write(&handle->lgh_lock);
327 err = llog_cancel_rec(handle, cookie.lgc_index);
328 up_write(&handle->lgh_lock);
329 GOTO(out_close, err);
332 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
334 GOTO(out_close, err);
335 cookie.lgc_lgl = plain;
337 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
338 GOTO(out_close, err = -EINVAL);
340 err = llog_cat_cancel_records(handle, 1, &cookie);
341 GOTO(out_close, err);
343 case OBD_IOC_LLOG_REMOVE: {
344 struct llog_logid plain;
346 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
347 err = llog_destroy(handle);
349 llog_free_handle(handle);
353 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
354 GOTO(out_close, err = -EINVAL);
356 if (data->ioc_inlbuf2) {
357 /*remove indicate log from the catalog*/
358 err = str2logid(&plain, data->ioc_inlbuf2,
361 GOTO(out_close, err);
362 err = llog_remove_log(handle, &plain);
364 /*remove all the log of the catalog*/
365 llog_process(handle, llog_delete_cb, NULL, NULL);
367 GOTO(out_close, err);
372 if (handle->lgh_hdr &&
373 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
374 llog_cat_put(handle);
380 EXPORT_SYMBOL(llog_ioctl);
382 int llog_catalog_list(struct obd_device *obd, int count,
383 struct obd_ioctl_data *data)
386 struct llog_catid *idarray;
387 struct llog_logid *id;
388 char name[32] = CATLIST;
390 int l, remains, rc = 0;
392 size = sizeof(*idarray) * count;
394 OBD_ALLOC(idarray, size);
397 memset(idarray, 0, size);
399 rc = llog_get_cat_list(obd, obd, name, count, idarray);
401 OBD_FREE(idarray, size);
405 out = data->ioc_bulk;
406 remains = data->ioc_inllen1;
407 for (i = 0; i < count; i++) {
408 id = &idarray[i].lci_logid;
409 l = snprintf(out, remains,
410 "catalog log: #"LPX64"#"LPX64"#%08x\n",
411 id->lgl_oid, id->lgl_ogr, id->lgl_ogen);
415 CWARN("not enough memory for catlog list\n");
419 OBD_FREE(idarray, size);
423 EXPORT_SYMBOL(llog_catalog_list);