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 (c) 2003, 2010, Oracle and/or its affiliates. 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_oseq = 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 cfs_size_round(ioc_data->ioc_inllen1) +
107 cfs_size_round(ioc_data->ioc_inllen2) +
108 cfs_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_oseq,
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:
150 case MDS_SETATTR64_REC:
152 case LLOG_HDR_MAGIC: {
153 l = snprintf(out, remains, "[index]: %05d [type]: "
154 "%02x [len]: %04d ok\n",
155 cur_index, rec->lrh_type,
160 CERROR("no space to print log records\n");
161 RETURN(-LLOG_EEMPTY);
166 l = snprintf(out, remains, "[index]: %05d [type]: "
167 "%02x [len]: %04d failed\n",
168 cur_index, rec->lrh_type,
173 CERROR("no space to print log records\n");
174 RETURN(-LLOG_EEMPTY);
183 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
186 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
187 static int l, remains, from, to;
193 if (ioc_data->ioc_inllen1) {
195 remains = ioc_data->ioc_inllen4 +
196 cfs_size_round(ioc_data->ioc_inllen1) +
197 cfs_size_round(ioc_data->ioc_inllen2) +
198 cfs_size_round(ioc_data->ioc_inllen3);
199 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
202 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
205 out = ioc_data->ioc_bulk;
206 ioc_data->ioc_inllen1 = 0;
209 cur_index = rec->lrh_index;
210 if (cur_index < from)
212 if (to > 0 && cur_index > to)
213 RETURN(-LLOG_EEMPTY);
215 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
216 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
217 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
218 CERROR("invalid record in catalog\n");
222 l = snprintf(out, remains,
223 "[index]: %05d [logid]: #"LPX64"#"LPX64"#%08x\n",
224 cur_index, lir->lid_id.lgl_oid,
225 lir->lid_id.lgl_oseq, lir->lid_id.lgl_ogen);
227 l = snprintf(out, remains,
228 "[index]: %05d [type]: %02x [len]: %04d\n",
229 cur_index, rec->lrh_type,
235 CERROR("not enough space for print log records\n");
236 RETURN(-LLOG_EEMPTY);
241 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
243 struct llog_handle *log;
247 cfs_down_write(&cat->lgh_lock);
248 rc = llog_cat_id2handle(cat, &log, logid);
250 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
251 logid->lgl_oid, logid->lgl_oseq, logid->lgl_ogen);
252 GOTO(out, rc = -ENOENT);
255 index = log->u.phd.phd_cookie.lgc_index;
257 rc = llog_destroy(log);
259 CDEBUG(D_IOCTL, "cannot destroy log\n");
262 llog_cat_set_first_idx(cat, index);
263 rc = llog_cancel_rec(cat, index);
265 llog_free_handle(log);
266 cfs_up_write(&cat->lgh_lock);
271 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
274 struct llog_logid_rec *lir = (struct llog_logid_rec*)rec;
278 if (rec->lrh_type != LLOG_LOGID_MAGIC)
280 rc = llog_remove_log(handle, &lir->lid_id);
286 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
288 struct llog_logid logid;
290 struct llog_handle *handle = NULL;
293 if (*data->ioc_inlbuf1 == '#') {
294 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
297 err = llog_create(ctxt, &handle, &logid, NULL);
300 } else if (*data->ioc_inlbuf1 == '$') {
301 char *name = data->ioc_inlbuf1 + 1;
302 err = llog_create(ctxt, &handle, NULL, name);
306 GOTO(out, err = -EINVAL);
309 err = llog_init_handle(handle, 0, NULL);
311 GOTO(out_close, err = -ENOENT);
314 case OBD_IOC_LLOG_INFO: {
316 int remains = data->ioc_inllen2 +
317 cfs_size_round(data->ioc_inllen1);
318 char *out = data->ioc_bulk;
320 l = snprintf(out, remains,
321 "logid: #"LPX64"#"LPX64"#%08x\n"
323 "records count: %d\n"
325 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_oseq,
326 handle->lgh_id.lgl_ogen,
327 handle->lgh_hdr->llh_flags,
328 handle->lgh_hdr->llh_flags &
329 LLOG_F_IS_CAT ? "cat" : "plain",
330 handle->lgh_hdr->llh_count,
331 handle->lgh_last_idx);
335 CERROR("not enough space for log header info\n");
337 GOTO(out_close, err);
339 case OBD_IOC_LLOG_CHECK: {
340 LASSERT(data->ioc_inllen1);
341 err = llog_process(handle, llog_check_cb, data, NULL);
342 if (err == -LLOG_EEMPTY)
344 GOTO(out_close, err);
347 case OBD_IOC_LLOG_PRINT: {
348 LASSERT(data->ioc_inllen1);
349 err = llog_process(handle, class_config_dump_handler,data,NULL);
350 if (err == -LLOG_EEMPTY)
353 err = llog_process(handle, llog_print_cb, data, NULL);
355 GOTO(out_close, err);
357 case OBD_IOC_LLOG_CANCEL: {
358 struct llog_cookie cookie;
359 struct llog_logid plain;
362 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
364 GOTO(out_close, err = -EINVAL);
366 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
367 cfs_down_write(&handle->lgh_lock);
368 err = llog_cancel_rec(handle, cookie.lgc_index);
369 cfs_up_write(&handle->lgh_lock);
370 GOTO(out_close, err);
373 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
375 GOTO(out_close, err);
376 cookie.lgc_lgl = plain;
378 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
379 GOTO(out_close, err = -EINVAL);
381 err = llog_cat_cancel_records(handle, 1, &cookie);
382 GOTO(out_close, err);
384 case OBD_IOC_LLOG_REMOVE: {
385 struct llog_logid plain;
387 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
388 err = llog_destroy(handle);
390 llog_free_handle(handle);
394 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
395 GOTO(out_close, err = -EINVAL);
397 if (data->ioc_inlbuf2) {
398 /*remove indicate log from the catalog*/
399 err = str2logid(&plain, data->ioc_inlbuf2,
402 GOTO(out_close, err);
403 err = llog_remove_log(handle, &plain);
405 /*remove all the log of the catalog*/
406 llog_process(handle, llog_delete_cb, NULL, NULL);
408 GOTO(out_close, err);
413 if (handle->lgh_hdr &&
414 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
415 llog_cat_put(handle);
421 EXPORT_SYMBOL(llog_ioctl);
423 int llog_catalog_list(struct obd_device *obd, int count,
424 struct obd_ioctl_data *data)
427 struct llog_catid *idarray;
428 struct llog_logid *id;
429 char name[32] = CATLIST;
431 int l, remains, rc = 0;
434 size = sizeof(*idarray) * count;
436 OBD_ALLOC_LARGE(idarray, size);
440 cfs_mutex_down(&obd->obd_olg.olg_cat_processing);
441 rc = llog_get_cat_list(obd, name, 0, count, idarray);
445 out = data->ioc_bulk;
446 remains = data->ioc_inllen1;
447 for (i = 0; i < count; i++) {
448 id = &idarray[i].lci_logid;
449 l = snprintf(out, remains,
450 "catalog log: #"LPX64"#"LPX64"#%08x\n",
451 id->lgl_oid, id->lgl_oseq, id->lgl_ogen);
455 CWARN("not enough memory for catlog list\n");
460 /* release semaphore */
461 cfs_mutex_up(&obd->obd_olg.olg_cat_processing);
463 OBD_FREE_LARGE(idarray, size);
467 EXPORT_SYMBOL(llog_catalog_list);