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 rc = llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
125 "cannot find log #"LPX64"#"LPX64"#%08x\n",
126 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogr,
127 lir->lid_id.lgl_ogen);
130 rc = llog_process(log_handle, llog_check_cb, NULL, NULL);
131 llog_close(log_handle);
133 switch (rec->lrh_type) {
137 case MDS_SETATTR_REC:
139 case LLOG_HDR_MAGIC: {
140 l = snprintf(out, remains, "[index]: %05d [type]: "
141 "%02x [len]: %04d ok\n",
142 cur_index, rec->lrh_type,
147 CERROR("no space to print log records\n");
148 RETURN(-LLOG_EEMPTY);
153 l = snprintf(out, remains, "[index]: %05d [type]: "
154 "%02x [len]: %04d failed\n",
155 cur_index, rec->lrh_type,
160 CERROR("no space to print log records\n");
161 RETURN(-LLOG_EEMPTY);
170 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
173 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
174 static int l, remains, from, to;
180 if (ioc_data->ioc_inllen1) {
182 remains = ioc_data->ioc_inllen4 +
183 size_round(ioc_data->ioc_inllen1) +
184 size_round(ioc_data->ioc_inllen2) +
185 size_round(ioc_data->ioc_inllen3);
186 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
189 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
192 out = ioc_data->ioc_bulk;
193 ioc_data->ioc_inllen1 = 0;
196 cur_index = rec->lrh_index;
197 if (cur_index < from)
199 if (to > 0 && cur_index > to)
200 RETURN(-LLOG_EEMPTY);
202 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
203 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
204 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
205 CERROR("invalid record in catalog\n");
209 l = snprintf(out, remains,
210 "[index]: %05d [logid]: #"LPX64"#"LPX64"#%08x\n",
211 cur_index, lir->lid_id.lgl_oid,
212 lir->lid_id.lgl_ogr, lir->lid_id.lgl_ogen);
214 l = snprintf(out, remains,
215 "[index]: %05d [type]: %02x [len]: %04d\n",
216 cur_index, rec->lrh_type,
222 CERROR("not enough space for print log records\n");
223 RETURN(-LLOG_EEMPTY);
228 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
230 struct llog_handle *log;
234 down_write(&cat->lgh_lock);
235 rc = llog_cat_id2handle(cat, &log, logid);
237 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
238 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen);
239 GOTO(out, rc = -ENOENT);
242 index = log->u.phd.phd_cookie.lgc_index;
244 rc = llog_destroy(log);
246 CDEBUG(D_IOCTL, "cannot destroy log\n");
249 llog_cat_set_first_idx(cat, index);
250 rc = llog_cancel_rec(cat, index);
252 llog_free_handle(log);
253 up_write(&cat->lgh_lock);
258 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
261 struct llog_logid_rec *lir = (struct llog_logid_rec*)rec;
265 if (rec->lrh_type != LLOG_LOGID_MAGIC)
267 rc = llog_remove_log(handle, &lir->lid_id);
273 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
275 struct llog_logid logid;
277 struct llog_handle *handle = NULL;
280 if (*data->ioc_inlbuf1 == '#') {
281 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
284 err = llog_create(ctxt, &handle, &logid, NULL);
287 } else if (*data->ioc_inlbuf1 == '$') {
288 char *name = data->ioc_inlbuf1 + 1;
289 err = llog_create(ctxt, &handle, NULL, name);
293 GOTO(out, err = -EINVAL);
296 err = llog_init_handle(handle, 0, NULL);
298 GOTO(out_close, err = -ENOENT);
301 case OBD_IOC_LLOG_INFO: {
303 int remains = data->ioc_inllen2 +
304 size_round(data->ioc_inllen1);
305 char *out = data->ioc_bulk;
307 l = snprintf(out, remains,
308 "logid: #"LPX64"#"LPX64"#%08x\n"
310 "records count: %d\n"
312 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_ogr,
313 handle->lgh_id.lgl_ogen,
314 handle->lgh_hdr->llh_flags,
315 handle->lgh_hdr->llh_flags &
316 LLOG_F_IS_CAT ? "cat" : "plain",
317 handle->lgh_hdr->llh_count,
318 handle->lgh_last_idx);
322 CERROR("not enough space for log header info\n");
324 GOTO(out_close, err);
326 case OBD_IOC_LLOG_CHECK: {
327 LASSERT(data->ioc_inllen1);
328 err = llog_process(handle, llog_check_cb, data, NULL);
329 if (err == -LLOG_EEMPTY)
331 GOTO(out_close, err);
334 case OBD_IOC_LLOG_PRINT: {
335 LASSERT(data->ioc_inllen1);
336 err = llog_process(handle, class_config_dump_handler,data,NULL);
337 if (err == -LLOG_EEMPTY)
340 err = llog_process(handle, llog_print_cb, data, NULL);
342 GOTO(out_close, err);
344 case OBD_IOC_LLOG_CANCEL: {
345 struct llog_cookie cookie;
346 struct llog_logid plain;
349 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
351 GOTO(out_close, err = -EINVAL);
353 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
354 down_write(&handle->lgh_lock);
355 err = llog_cancel_rec(handle, cookie.lgc_index);
356 up_write(&handle->lgh_lock);
357 GOTO(out_close, err);
360 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
362 GOTO(out_close, err);
363 cookie.lgc_lgl = plain;
365 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
366 GOTO(out_close, err = -EINVAL);
368 err = llog_cat_cancel_records(handle, 1, &cookie);
369 GOTO(out_close, err);
371 case OBD_IOC_LLOG_REMOVE: {
372 struct llog_logid plain;
374 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
375 err = llog_destroy(handle);
377 llog_free_handle(handle);
381 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
382 GOTO(out_close, err = -EINVAL);
384 if (data->ioc_inlbuf2) {
385 /*remove indicate log from the catalog*/
386 err = str2logid(&plain, data->ioc_inlbuf2,
389 GOTO(out_close, err);
390 err = llog_remove_log(handle, &plain);
392 /*remove all the log of the catalog*/
393 llog_process(handle, llog_delete_cb, NULL, NULL);
395 GOTO(out_close, err);
400 if (handle->lgh_hdr &&
401 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
402 llog_cat_put(handle);
408 EXPORT_SYMBOL(llog_ioctl);
410 int llog_catalog_list(struct obd_device *obd, int count,
411 struct obd_ioctl_data *data)
414 struct llog_catid *idarray;
415 struct llog_logid *id;
416 char name[32] = CATLIST;
418 int l, remains, rc = 0;
421 size = sizeof(*idarray) * count;
423 OBD_ALLOC(idarray, size);
426 memset(idarray, 0, size);
428 rc = llog_get_cat_list(obd, obd, name, count, idarray);
430 OBD_FREE(idarray, size);
434 out = data->ioc_bulk;
435 remains = data->ioc_inllen1;
436 for (i = 0; i < count; i++) {
437 id = &idarray[i].lci_logid;
438 l = snprintf(out, remains,
439 "catalog log: #"LPX64"#"LPX64"#%08x\n",
440 id->lgl_oid, id->lgl_ogr, id->lgl_ogen);
444 CWARN("not enough memory for catlog list\n");
448 OBD_FREE(idarray, size);
452 EXPORT_SYMBOL(llog_catalog_list);