1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2005 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
25 #define DEBUG_SUBSYSTEM S_LOG
31 #include <obd_class.h>
32 #include <lustre_log.h>
33 #include <libcfs/list.h>
34 #include "llog_internal.h"
36 static int str2logid(struct llog_logid *logid, char *str, int len)
38 char *start, *end, *endp;
46 if (start - str >= len - 1)
48 end = strchr(start, '#');
49 if (end == NULL || end == start)
53 logid->lgl_oid = simple_strtoull(start, &endp, 0);
58 if (start - str >= len - 1)
60 end = strchr(start, '#');
61 if (end == NULL || end == start)
65 logid->lgl_ogr = simple_strtoull(start, &endp, 0);
70 if (start - str >= len - 1)
72 logid->lgl_ogen = simple_strtoul(start, &endp, 16);
79 static int llog_check_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
82 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
83 static int l, remains, from, to;
86 int cur_index, rc = 0;
89 cur_index = rec->lrh_index;
91 if (ioc_data && (ioc_data->ioc_inllen1)) {
93 remains = ioc_data->ioc_inllen4 +
94 size_round(ioc_data->ioc_inllen1) +
95 size_round(ioc_data->ioc_inllen2) +
96 size_round(ioc_data->ioc_inllen3);
97 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
100 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
103 ioc_data->ioc_inllen1 = 0;
104 out = ioc_data->ioc_bulk;
105 if (cur_index < from)
107 if (to > 0 && cur_index > to)
108 RETURN(-LLOG_EEMPTY);
110 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
111 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
112 struct llog_handle *log_handle;
114 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
115 l = snprintf(out, remains, "[index]: %05d [type]: "
116 "%02x [len]: %04d failed\n",
117 cur_index, rec->lrh_type,
120 if (handle->lgh_ctxt == NULL)
122 llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
123 rc = llog_process(log_handle, llog_check_cb, NULL, NULL);
124 llog_close(log_handle);
126 switch (rec->lrh_type) {
130 case MDS_SETATTR_REC:
132 case LLOG_HDR_MAGIC: {
133 l = snprintf(out, remains, "[index]: %05d [type]: "
134 "%02x [len]: %04d ok\n",
135 cur_index, rec->lrh_type,
140 CERROR("no space to print log records\n");
141 RETURN(-LLOG_EEMPTY);
146 l = snprintf(out, remains, "[index]: %05d [type]: "
147 "%02x [len]: %04d failed\n",
148 cur_index, rec->lrh_type,
153 CERROR("no space to print log records\n");
154 RETURN(-LLOG_EEMPTY);
163 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
166 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
167 static int l, remains, from, to;
173 if (ioc_data->ioc_inllen1) {
175 remains = ioc_data->ioc_inllen4 +
176 size_round(ioc_data->ioc_inllen1) +
177 size_round(ioc_data->ioc_inllen2) +
178 size_round(ioc_data->ioc_inllen3);
179 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
182 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
185 out = ioc_data->ioc_bulk;
186 ioc_data->ioc_inllen1 = 0;
189 cur_index = rec->lrh_index;
190 if (cur_index < from)
192 if (to > 0 && cur_index > to)
193 RETURN(-LLOG_EEMPTY);
195 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
196 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
197 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
198 CERROR("invalid record in catalog\n");
202 l = snprintf(out, remains,
203 "[index]: %05d [logid]: #"LPX64"#"LPX64"#%08x\n",
204 cur_index, lir->lid_id.lgl_oid,
205 lir->lid_id.lgl_ogr, lir->lid_id.lgl_ogen);
207 l = snprintf(out, remains,
208 "[index]: %05d [type]: %02x [len]: %04d\n",
209 cur_index, rec->lrh_type,
215 CERROR("not enough space for print log records\n");
216 RETURN(-LLOG_EEMPTY);
221 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
223 struct llog_handle *log;
227 down_write(&cat->lgh_lock);
228 rc = llog_cat_id2handle(cat, &log, logid);
230 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
231 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen);
232 GOTO(out, rc = -ENOENT);
235 index = log->u.phd.phd_cookie.lgc_index;
237 rc = llog_destroy(log);
239 CDEBUG(D_IOCTL, "cannot destroy log\n");
242 llog_cat_set_first_idx(cat, index);
243 rc = llog_cancel_rec(cat, index);
245 llog_free_handle(log);
246 up_write(&cat->lgh_lock);
251 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
254 struct llog_logid_rec *lir = (struct llog_logid_rec*)rec;
258 if (rec->lrh_type != LLOG_LOGID_MAGIC)
260 rc = llog_remove_log(handle, &lir->lid_id);
266 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
268 struct llog_logid logid;
270 struct llog_handle *handle = NULL;
273 if (*data->ioc_inlbuf1 == '#') {
274 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
277 err = llog_create(ctxt, &handle, &logid, NULL);
280 } else if (*data->ioc_inlbuf1 == '$') {
281 char *name = data->ioc_inlbuf1 + 1;
282 err = llog_create(ctxt, &handle, NULL, name);
286 GOTO(out, err = -EINVAL);
289 err = llog_init_handle(handle, 0, NULL);
291 GOTO(out_close, err = -ENOENT);
294 case OBD_IOC_LLOG_INFO: {
296 int remains = data->ioc_inllen2 +
297 size_round(data->ioc_inllen1);
298 char *out = data->ioc_bulk;
300 l = snprintf(out, remains,
301 "logid: #"LPX64"#"LPX64"#%08x\n"
303 "records count: %d\n"
305 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_ogr,
306 handle->lgh_id.lgl_ogen,
307 handle->lgh_hdr->llh_flags,
308 handle->lgh_hdr->llh_flags &
309 LLOG_F_IS_CAT ? "cat" : "plain",
310 handle->lgh_hdr->llh_count,
311 handle->lgh_last_idx);
315 CERROR("not enough space for log header info\n");
317 GOTO(out_close, err);
319 case OBD_IOC_LLOG_CHECK: {
320 LASSERT(data->ioc_inllen1);
321 err = llog_process(handle, llog_check_cb, data, NULL);
322 if (err == -LLOG_EEMPTY)
324 GOTO(out_close, err);
327 case OBD_IOC_LLOG_PRINT: {
328 LASSERT(data->ioc_inllen1);
329 err = llog_process(handle, class_config_dump_handler,data,NULL);
330 if (err == -LLOG_EEMPTY)
333 err = llog_process(handle, llog_print_cb, data, NULL);
335 GOTO(out_close, err);
337 case OBD_IOC_LLOG_CANCEL: {
338 struct llog_cookie cookie;
339 struct llog_logid plain;
342 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
344 GOTO(out_close, err = -EINVAL);
346 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
347 down_write(&handle->lgh_lock);
348 err = llog_cancel_rec(handle, cookie.lgc_index);
349 up_write(&handle->lgh_lock);
350 GOTO(out_close, err);
353 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
355 GOTO(out_close, err);
356 cookie.lgc_lgl = plain;
358 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
359 GOTO(out_close, err = -EINVAL);
361 err = llog_cat_cancel_records(handle, 1, &cookie);
362 GOTO(out_close, err);
364 case OBD_IOC_LLOG_REMOVE: {
365 struct llog_logid plain;
367 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
368 err = llog_destroy(handle);
370 llog_free_handle(handle);
374 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
375 GOTO(out_close, err = -EINVAL);
377 if (data->ioc_inlbuf2) {
378 /*remove indicate log from the catalog*/
379 err = str2logid(&plain, data->ioc_inlbuf2,
382 GOTO(out_close, err);
383 err = llog_remove_log(handle, &plain);
385 /*remove all the log of the catalog*/
386 llog_process(handle, llog_delete_cb, NULL, NULL);
388 GOTO(out_close, err);
393 if (handle->lgh_hdr &&
394 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
395 llog_cat_put(handle);
401 EXPORT_SYMBOL(llog_ioctl);
403 int llog_catalog_list(struct obd_device *obd, int count,
404 struct obd_ioctl_data *data)
407 struct llog_catid *idarray;
408 struct llog_logid *id;
409 char name[32] = CATLIST;
411 int l, remains, rc = 0;
414 size = sizeof(*idarray) * count;
416 OBD_ALLOC(idarray, size);
419 memset(idarray, 0, size);
421 rc = llog_get_cat_list(obd, obd, name, count, idarray);
423 OBD_FREE(idarray, size);
427 out = data->ioc_bulk;
428 remains = data->ioc_inllen1;
429 for (i = 0; i < count; i++) {
430 id = &idarray[i].lci_logid;
431 l = snprintf(out, remains,
432 "catalog log: #"LPX64"#"LPX64"#%08x\n",
433 id->lgl_oid, id->lgl_ogr, id->lgl_ogen);
437 CWARN("not enough memory for catlog list\n");
441 OBD_FREE(idarray, size);
445 EXPORT_SYMBOL(llog_catalog_list);