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
32 #include <linux/obd_class.h>
33 #include <linux/lustre_log.h>
34 #include <libcfs/list.h>
35 #include "llog_internal.h"
37 static int str2logid(struct llog_logid *logid, char *str, int len)
39 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;
88 cur_index = rec->lrh_index;
90 if (ioc_data && (ioc_data->ioc_inllen1)) {
92 remains = ioc_data->ioc_inllen4 +
93 size_round(ioc_data->ioc_inllen1) +
94 size_round(ioc_data->ioc_inllen2) +
95 size_round(ioc_data->ioc_inllen3);
96 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
99 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
102 ioc_data->ioc_inllen1 = 0;
103 out = ioc_data->ioc_bulk;
104 if (cur_index < from)
106 if (to > 0 && cur_index > to)
107 RETURN(-LLOG_EEMPTY);
109 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
110 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
111 struct llog_handle *log_handle;
113 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
114 l = snprintf(out, remains, "[index]: %05d [type]: "
115 "%02x [len]: %04d failed\n",
116 cur_index, rec->lrh_type,
119 if (handle->lgh_ctxt == NULL)
121 llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
122 rc = llog_process(log_handle, llog_check_cb, NULL, NULL);
123 llog_close(log_handle);
125 switch (rec->lrh_type) {
129 case MDS_SETATTR_REC:
131 case PTL_CFG_REC: /* obsolete */
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;
172 if (ioc_data->ioc_inllen1) {
174 remains = ioc_data->ioc_inllen4 +
175 size_round(ioc_data->ioc_inllen1) +
176 size_round(ioc_data->ioc_inllen2) +
177 size_round(ioc_data->ioc_inllen3);
178 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
181 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
184 out = ioc_data->ioc_bulk;
185 ioc_data->ioc_inllen1 = 0;
188 cur_index = rec->lrh_index;
189 if (cur_index < from)
191 if (to > 0 && cur_index > to)
192 RETURN(-LLOG_EEMPTY);
194 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
195 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
196 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
197 CERROR("invalid record in catalog\n");
201 l = snprintf(out, remains,
202 "[index]: %05d [logid]: #"LPX64"#"LPX64"#%08x\n",
203 cur_index, lir->lid_id.lgl_oid,
204 lir->lid_id.lgl_ogr, lir->lid_id.lgl_ogen);
206 l = snprintf(out, remains,
207 "[index]: %05d [type]: %02x [len]: %04d\n",
208 cur_index, rec->lrh_type,
214 CERROR("not enough space for print log records\n");
215 RETURN(-LLOG_EEMPTY);
220 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
222 struct llog_handle *log;
225 down_write(&cat->lgh_lock);
226 rc = llog_cat_id2handle(cat, &log, logid);
228 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
229 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen);
230 GOTO(out, rc = -ENOENT);
233 index = log->u.phd.phd_cookie.lgc_index;
235 rc = llog_destroy(log);
237 CDEBUG(D_IOCTL, "cannot destroy log\n");
240 llog_cat_set_first_idx(cat, index);
241 rc = llog_cancel_rec(cat, index);
243 llog_free_handle(log);
244 up_write(&cat->lgh_lock);
249 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
252 struct llog_logid_rec *lir = (struct llog_logid_rec*)rec;
255 if (rec->lrh_type != LLOG_LOGID_MAGIC)
257 rc = llog_remove_log(handle, &lir->lid_id);
263 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
265 struct llog_logid logid;
267 struct llog_handle *handle = NULL;
269 if (*data->ioc_inlbuf1 == '#') {
270 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
273 err = llog_create(ctxt, &handle, &logid, NULL);
276 } else if (*data->ioc_inlbuf1 == '$') {
277 char *name = data->ioc_inlbuf1 + 1;
278 err = llog_create(ctxt, &handle, NULL, name);
282 GOTO(out, err = -EINVAL);
285 err = llog_init_handle(handle, 0, NULL);
287 GOTO(out_close, err = -ENOENT);
290 case OBD_IOC_LLOG_INFO: {
292 int remains = data->ioc_inllen2 +
293 size_round(data->ioc_inllen1);
294 char *out = data->ioc_bulk;
296 l = snprintf(out, remains,
297 "logid: #"LPX64"#"LPX64"#%08x\n"
299 "records count: %d\n"
301 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_ogr,
302 handle->lgh_id.lgl_ogen,
303 handle->lgh_hdr->llh_flags,
304 handle->lgh_hdr->llh_flags &
305 LLOG_F_IS_CAT ? "cat" : "plain",
306 handle->lgh_hdr->llh_count,
307 handle->lgh_last_idx);
311 CERROR("not enough space for log header info\n");
313 GOTO(out_close, err);
315 case OBD_IOC_LLOG_CHECK: {
316 LASSERT(data->ioc_inllen1);
317 err = llog_process(handle, llog_check_cb, data, NULL);
318 if (err == -LLOG_EEMPTY)
320 GOTO(out_close, err);
323 case OBD_IOC_LLOG_PRINT: {
324 LASSERT(data->ioc_inllen1);
325 err = llog_process(handle, class_config_dump_handler,data,NULL);
326 if (err == -LLOG_EEMPTY)
329 err = llog_process(handle, llog_print_cb, data, NULL);
331 GOTO(out_close, err);
333 case OBD_IOC_LLOG_CANCEL: {
334 struct llog_cookie cookie;
335 struct llog_logid plain;
338 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
340 GOTO(out_close, err = -EINVAL);
342 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
343 down_write(&handle->lgh_lock);
344 err = llog_cancel_rec(handle, cookie.lgc_index);
345 up_write(&handle->lgh_lock);
346 GOTO(out_close, err);
349 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
351 GOTO(out_close, err);
352 cookie.lgc_lgl = plain;
354 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
355 GOTO(out_close, err = -EINVAL);
357 err = llog_cat_cancel_records(handle, 1, &cookie);
358 GOTO(out_close, err);
360 case OBD_IOC_LLOG_REMOVE: {
361 struct llog_logid plain;
363 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
364 err = llog_destroy(handle);
366 llog_free_handle(handle);
370 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
371 GOTO(out_close, err = -EINVAL);
373 if (data->ioc_inlbuf2) {
374 /*remove indicate log from the catalog*/
375 err = str2logid(&plain, data->ioc_inlbuf2,
378 GOTO(out_close, err);
379 err = llog_remove_log(handle, &plain);
381 /*remove all the log of the catalog*/
382 llog_process(handle, llog_delete_cb, NULL, NULL);
384 GOTO(out_close, err);
389 if (handle->lgh_hdr &&
390 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
391 llog_cat_put(handle);
397 EXPORT_SYMBOL(llog_ioctl);
399 int llog_catalog_list(struct obd_device *obd, int count,
400 struct obd_ioctl_data *data)
403 struct llog_catid *idarray;
404 struct llog_logid *id;
405 char name[32] = CATLIST;
407 int l, remains, rc = 0;
409 size = sizeof(*idarray) * count;
411 OBD_ALLOC(idarray, size);
414 memset(idarray, 0, size);
416 rc = llog_get_cat_list(obd, obd, name, count, idarray);
418 OBD_FREE(idarray, size);
422 out = data->ioc_bulk;
423 remains = data->ioc_inllen1;
424 for (i = 0; i < count; i++) {
425 id = &idarray[i].lci_logid;
426 l = snprintf(out, remains,
427 "catalog log: #"LPX64"#"LPX64"#%08x\n",
428 id->lgl_oid, id->lgl_ogr, id->lgl_ogen);
432 CWARN("not enough memory for catlog list\n");
436 OBD_FREE(idarray, size);
440 EXPORT_SYMBOL(llog_catalog_list);