1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LOG
43 #include <obd_class.h>
44 #include <lustre_log.h>
45 #include <libcfs/list.h>
46 #include "llog_internal.h"
48 static int str2logid(struct llog_logid *logid, char *str, int len)
50 char *start, *end, *endp;
58 if (start - str >= len - 1)
60 end = strchr(start, '#');
61 if (end == NULL || end == start)
65 logid->lgl_oid = simple_strtoull(start, &endp, 0);
70 if (start - str >= len - 1)
72 end = strchr(start, '#');
73 if (end == NULL || end == start)
77 logid->lgl_ogr = simple_strtoull(start, &endp, 0);
82 if (start - str >= len - 1)
84 logid->lgl_ogen = simple_strtoul(start, &endp, 16);
91 static int llog_check_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
94 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
95 static int l, remains, from, to;
98 int cur_index, rc = 0;
101 cur_index = rec->lrh_index;
103 if (ioc_data && (ioc_data->ioc_inllen1)) {
105 remains = ioc_data->ioc_inllen4 +
106 size_round(ioc_data->ioc_inllen1) +
107 size_round(ioc_data->ioc_inllen2) +
108 size_round(ioc_data->ioc_inllen3);
109 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
112 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
115 ioc_data->ioc_inllen1 = 0;
116 out = ioc_data->ioc_bulk;
117 if (cur_index < from)
119 if (to > 0 && cur_index > to)
120 RETURN(-LLOG_EEMPTY);
122 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
123 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
124 struct llog_handle *log_handle;
126 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
127 l = snprintf(out, remains, "[index]: %05d [type]: "
128 "%02x [len]: %04d failed\n",
129 cur_index, rec->lrh_type,
132 if (handle->lgh_ctxt == NULL)
134 rc = llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
137 "cannot find log #"LPX64"#"LPX64"#%08x\n",
138 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogr,
139 lir->lid_id.lgl_ogen);
142 rc = llog_process(log_handle, llog_check_cb, NULL, NULL);
143 llog_close(log_handle);
145 switch (rec->lrh_type) {
149 case MDS_SETATTR_REC:
151 case LLOG_HDR_MAGIC: {
152 l = snprintf(out, remains, "[index]: %05d [type]: "
153 "%02x [len]: %04d ok\n",
154 cur_index, rec->lrh_type,
159 CERROR("no space to print log records\n");
160 RETURN(-LLOG_EEMPTY);
165 l = snprintf(out, remains, "[index]: %05d [type]: "
166 "%02x [len]: %04d failed\n",
167 cur_index, rec->lrh_type,
172 CERROR("no space to print log records\n");
173 RETURN(-LLOG_EEMPTY);
182 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
185 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
186 static int l, remains, from, to;
192 if (ioc_data->ioc_inllen1) {
194 remains = ioc_data->ioc_inllen4 +
195 size_round(ioc_data->ioc_inllen1) +
196 size_round(ioc_data->ioc_inllen2) +
197 size_round(ioc_data->ioc_inllen3);
198 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
201 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
204 out = ioc_data->ioc_bulk;
205 ioc_data->ioc_inllen1 = 0;
208 cur_index = rec->lrh_index;
209 if (cur_index < from)
211 if (to > 0 && cur_index > to)
212 RETURN(-LLOG_EEMPTY);
214 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
215 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
216 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
217 CERROR("invalid record in catalog\n");
221 l = snprintf(out, remains,
222 "[index]: %05d [logid]: #"LPX64"#"LPX64"#%08x\n",
223 cur_index, lir->lid_id.lgl_oid,
224 lir->lid_id.lgl_ogr, lir->lid_id.lgl_ogen);
226 l = snprintf(out, remains,
227 "[index]: %05d [type]: %02x [len]: %04d\n",
228 cur_index, rec->lrh_type,
234 CERROR("not enough space for print log records\n");
235 RETURN(-LLOG_EEMPTY);
240 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
242 struct llog_handle *log;
246 down_write(&cat->lgh_lock);
247 rc = llog_cat_id2handle(cat, &log, logid);
249 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
250 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen);
251 GOTO(out, rc = -ENOENT);
254 index = log->u.phd.phd_cookie.lgc_index;
256 rc = llog_destroy(log);
258 CDEBUG(D_IOCTL, "cannot destroy log\n");
261 llog_cat_set_first_idx(cat, index);
262 rc = llog_cancel_rec(cat, index);
264 llog_free_handle(log);
265 up_write(&cat->lgh_lock);
270 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
273 struct llog_logid_rec *lir = (struct llog_logid_rec*)rec;
277 if (rec->lrh_type != LLOG_LOGID_MAGIC)
279 rc = llog_remove_log(handle, &lir->lid_id);
285 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
287 struct llog_logid logid;
289 struct llog_handle *handle = NULL;
292 if (*data->ioc_inlbuf1 == '#') {
293 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
296 err = llog_create(ctxt, &handle, &logid, NULL);
299 } else if (*data->ioc_inlbuf1 == '$') {
300 char *name = data->ioc_inlbuf1 + 1;
301 err = llog_create(ctxt, &handle, NULL, name);
305 GOTO(out, err = -EINVAL);
308 err = llog_init_handle(handle, 0, NULL);
310 GOTO(out_close, err = -ENOENT);
313 case OBD_IOC_LLOG_INFO: {
315 int remains = data->ioc_inllen2 +
316 size_round(data->ioc_inllen1);
317 char *out = data->ioc_bulk;
319 l = snprintf(out, remains,
320 "logid: #"LPX64"#"LPX64"#%08x\n"
322 "records count: %d\n"
324 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_ogr,
325 handle->lgh_id.lgl_ogen,
326 handle->lgh_hdr->llh_flags,
327 handle->lgh_hdr->llh_flags &
328 LLOG_F_IS_CAT ? "cat" : "plain",
329 handle->lgh_hdr->llh_count,
330 handle->lgh_last_idx);
334 CERROR("not enough space for log header info\n");
336 GOTO(out_close, err);
338 case OBD_IOC_LLOG_CHECK: {
339 LASSERT(data->ioc_inllen1);
340 err = llog_process(handle, llog_check_cb, data, NULL);
341 if (err == -LLOG_EEMPTY)
343 GOTO(out_close, err);
346 case OBD_IOC_LLOG_PRINT: {
347 LASSERT(data->ioc_inllen1);
348 err = llog_process(handle, class_config_dump_handler,data,NULL);
349 if (err == -LLOG_EEMPTY)
352 err = llog_process(handle, llog_print_cb, data, NULL);
354 GOTO(out_close, err);
356 case OBD_IOC_LLOG_CANCEL: {
357 struct llog_cookie cookie;
358 struct llog_logid plain;
361 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
363 GOTO(out_close, err = -EINVAL);
365 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
366 down_write(&handle->lgh_lock);
367 err = llog_cancel_rec(handle, cookie.lgc_index);
368 up_write(&handle->lgh_lock);
369 GOTO(out_close, err);
372 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
374 GOTO(out_close, err);
375 cookie.lgc_lgl = plain;
377 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
378 GOTO(out_close, err = -EINVAL);
380 err = llog_cat_cancel_records(handle, 1, &cookie);
381 GOTO(out_close, err);
383 case OBD_IOC_LLOG_REMOVE: {
384 struct llog_logid plain;
386 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
387 err = llog_destroy(handle);
389 llog_free_handle(handle);
393 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
394 GOTO(out_close, err = -EINVAL);
396 if (data->ioc_inlbuf2) {
397 /*remove indicate log from the catalog*/
398 err = str2logid(&plain, data->ioc_inlbuf2,
401 GOTO(out_close, err);
402 err = llog_remove_log(handle, &plain);
404 /*remove all the log of the catalog*/
405 llog_process(handle, llog_delete_cb, NULL, NULL);
407 GOTO(out_close, err);
412 if (handle->lgh_hdr &&
413 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
414 llog_cat_put(handle);
420 EXPORT_SYMBOL(llog_ioctl);
422 int llog_catalog_list(struct obd_device *obd, int count,
423 struct obd_ioctl_data *data)
426 struct llog_catid *idarray;
427 struct llog_logid *id;
428 char name[32] = CATLIST;
430 int l, remains, rc = 0;
433 size = sizeof(*idarray) * count;
435 OBD_ALLOC(idarray, size);
438 memset(idarray, 0, size);
440 rc = llog_get_cat_list(obd, obd, name, count, idarray);
442 OBD_FREE(idarray, size);
446 out = data->ioc_bulk;
447 remains = data->ioc_inllen1;
448 for (i = 0; i < count; i++) {
449 id = &idarray[i].lci_logid;
450 l = snprintf(out, remains,
451 "catalog log: #"LPX64"#"LPX64"#%08x\n",
452 id->lgl_oid, id->lgl_ogr, id->lgl_ogen);
456 CWARN("not enough memory for catlog list\n");
460 OBD_FREE(idarray, size);
464 EXPORT_SYMBOL(llog_catalog_list);