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.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
39 #define DEBUG_SUBSYSTEM S_LOG
45 #include <obd_class.h>
46 #include <lustre_log.h>
47 #include <libcfs/list.h>
48 #include "llog_internal.h"
50 static int str2logid(struct llog_logid *logid, char *str, int len)
52 char *start, *end, *endp;
60 if (start - str >= len - 1)
62 end = strchr(start, '#');
63 if (end == NULL || end == start)
67 logid->lgl_oid = simple_strtoull(start, &endp, 0);
72 if (start - str >= len - 1)
74 end = strchr(start, '#');
75 if (end == NULL || end == start)
79 logid->lgl_oseq = simple_strtoull(start, &endp, 0);
84 if (start - str >= len - 1)
86 logid->lgl_ogen = simple_strtoul(start, &endp, 16);
93 static int llog_check_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
96 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
97 static int l, remains, from, to;
100 int cur_index, rc = 0;
103 cur_index = rec->lrh_index;
105 if (ioc_data && (ioc_data->ioc_inllen1)) {
107 remains = ioc_data->ioc_inllen4 +
108 cfs_size_round(ioc_data->ioc_inllen1) +
109 cfs_size_round(ioc_data->ioc_inllen2) +
110 cfs_size_round(ioc_data->ioc_inllen3);
111 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
114 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
117 ioc_data->ioc_inllen1 = 0;
118 out = ioc_data->ioc_bulk;
119 if (cur_index < from)
121 if (to > 0 && cur_index > to)
122 RETURN(-LLOG_EEMPTY);
124 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
125 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
126 struct llog_handle *log_handle;
128 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
129 l = snprintf(out, remains, "[index]: %05d [type]: "
130 "%02x [len]: %04d failed\n",
131 cur_index, rec->lrh_type,
134 if (handle->lgh_ctxt == NULL)
136 rc = llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
139 "cannot find log #"LPX64"#"LPX64"#%08x\n",
140 lir->lid_id.lgl_oid, lir->lid_id.lgl_oseq,
141 lir->lid_id.lgl_ogen);
144 rc = llog_process(log_handle, llog_check_cb, NULL, NULL);
145 llog_close(log_handle);
147 switch (rec->lrh_type) {
151 case MDS_SETATTR_REC:
152 case MDS_SETATTR64_REC:
154 case LLOG_HDR_MAGIC: {
155 l = snprintf(out, remains, "[index]: %05d [type]: "
156 "%02x [len]: %04d ok\n",
157 cur_index, rec->lrh_type,
162 CERROR("no space to print log records\n");
163 RETURN(-LLOG_EEMPTY);
168 l = snprintf(out, remains, "[index]: %05d [type]: "
169 "%02x [len]: %04d failed\n",
170 cur_index, rec->lrh_type,
175 CERROR("no space to print log records\n");
176 RETURN(-LLOG_EEMPTY);
185 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
188 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
189 static int l, remains, from, to;
195 if (ioc_data->ioc_inllen1) {
197 remains = ioc_data->ioc_inllen4 +
198 cfs_size_round(ioc_data->ioc_inllen1) +
199 cfs_size_round(ioc_data->ioc_inllen2) +
200 cfs_size_round(ioc_data->ioc_inllen3);
201 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
204 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
207 out = ioc_data->ioc_bulk;
208 ioc_data->ioc_inllen1 = 0;
211 cur_index = rec->lrh_index;
212 if (cur_index < from)
214 if (to > 0 && cur_index > to)
215 RETURN(-LLOG_EEMPTY);
217 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
218 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
219 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
220 CERROR("invalid record in catalog\n");
224 l = snprintf(out, remains,
225 "[index]: %05d [logid]: #"LPX64"#"LPX64"#%08x\n",
226 cur_index, lir->lid_id.lgl_oid,
227 lir->lid_id.lgl_oseq, lir->lid_id.lgl_ogen);
229 l = snprintf(out, remains,
230 "[index]: %05d [type]: %02x [len]: %04d\n",
231 cur_index, rec->lrh_type,
237 CERROR("not enough space for print log records\n");
238 RETURN(-LLOG_EEMPTY);
243 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
245 struct llog_handle *log;
249 cfs_down_write(&cat->lgh_lock);
250 rc = llog_cat_id2handle(cat, &log, logid);
252 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
253 logid->lgl_oid, logid->lgl_oseq, logid->lgl_ogen);
254 GOTO(out, rc = -ENOENT);
257 index = log->u.phd.phd_cookie.lgc_index;
259 rc = llog_destroy(log);
261 CDEBUG(D_IOCTL, "cannot destroy log\n");
264 llog_cat_set_first_idx(cat, index);
265 rc = llog_cancel_rec(cat, index);
267 llog_free_handle(log);
268 cfs_up_write(&cat->lgh_lock);
273 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
276 struct llog_logid_rec *lir = (struct llog_logid_rec*)rec;
280 if (rec->lrh_type != LLOG_LOGID_MAGIC)
282 rc = llog_remove_log(handle, &lir->lid_id);
288 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
290 struct llog_logid logid;
292 struct llog_handle *handle = NULL;
295 if (*data->ioc_inlbuf1 == '#') {
296 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
299 err = llog_create(ctxt, &handle, &logid, NULL);
302 } else if (*data->ioc_inlbuf1 == '$') {
303 char *name = data->ioc_inlbuf1 + 1;
304 err = llog_create(ctxt, &handle, NULL, name);
308 GOTO(out, err = -EINVAL);
311 err = llog_init_handle(handle, 0, NULL);
313 GOTO(out_close, err = -ENOENT);
316 case OBD_IOC_LLOG_INFO: {
318 int remains = data->ioc_inllen2 +
319 cfs_size_round(data->ioc_inllen1);
320 char *out = data->ioc_bulk;
322 l = snprintf(out, remains,
323 "logid: #"LPX64"#"LPX64"#%08x\n"
325 "records count: %d\n"
327 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_oseq,
328 handle->lgh_id.lgl_ogen,
329 handle->lgh_hdr->llh_flags,
330 handle->lgh_hdr->llh_flags &
331 LLOG_F_IS_CAT ? "cat" : "plain",
332 handle->lgh_hdr->llh_count,
333 handle->lgh_last_idx);
337 CERROR("not enough space for log header info\n");
339 GOTO(out_close, err);
341 case OBD_IOC_LLOG_CHECK: {
342 LASSERT(data->ioc_inllen1);
343 err = llog_process(handle, llog_check_cb, data, NULL);
344 if (err == -LLOG_EEMPTY)
346 GOTO(out_close, err);
349 case OBD_IOC_LLOG_PRINT: {
350 LASSERT(data->ioc_inllen1);
351 err = llog_process(handle, class_config_dump_handler,data,NULL);
352 if (err == -LLOG_EEMPTY)
355 err = llog_process(handle, llog_print_cb, data, NULL);
357 GOTO(out_close, err);
359 case OBD_IOC_LLOG_CANCEL: {
360 struct llog_cookie cookie;
361 struct llog_logid plain;
364 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
366 GOTO(out_close, err = -EINVAL);
368 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
369 cfs_down_write(&handle->lgh_lock);
370 err = llog_cancel_rec(handle, cookie.lgc_index);
371 cfs_up_write(&handle->lgh_lock);
372 GOTO(out_close, err);
375 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
377 GOTO(out_close, err);
378 cookie.lgc_lgl = plain;
380 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
381 GOTO(out_close, err = -EINVAL);
383 err = llog_cat_cancel_records(handle, 1, &cookie);
384 GOTO(out_close, err);
386 case OBD_IOC_LLOG_REMOVE: {
387 struct llog_logid plain;
389 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
390 err = llog_destroy(handle);
392 llog_free_handle(handle);
396 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
397 GOTO(out_close, err = -EINVAL);
399 if (data->ioc_inlbuf2) {
400 /*remove indicate log from the catalog*/
401 err = str2logid(&plain, data->ioc_inlbuf2,
404 GOTO(out_close, err);
405 err = llog_remove_log(handle, &plain);
407 /*remove all the log of the catalog*/
408 llog_process(handle, llog_delete_cb, NULL, NULL);
410 GOTO(out_close, err);
415 if (handle->lgh_hdr &&
416 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
417 llog_cat_put(handle);
423 EXPORT_SYMBOL(llog_ioctl);
425 int llog_catalog_list(struct obd_device *obd, int count,
426 struct obd_ioctl_data *data)
429 struct llog_catid *idarray;
430 struct llog_logid *id;
431 char name[32] = CATLIST;
433 int l, remains, rc = 0;
436 size = sizeof(*idarray) * count;
438 OBD_ALLOC_LARGE(idarray, size);
442 cfs_mutex_lock(&obd->obd_olg.olg_cat_processing);
443 rc = llog_get_cat_list(obd, name, 0, count, idarray);
447 out = data->ioc_bulk;
448 remains = data->ioc_inllen1;
449 for (i = 0; i < count; i++) {
450 id = &idarray[i].lci_logid;
451 l = snprintf(out, remains,
452 "catalog log: #"LPX64"#"LPX64"#%08x\n",
453 id->lgl_oid, id->lgl_oseq, id->lgl_ogen);
457 CWARN("not enough memory for catlog list\n");
462 /* release semaphore */
463 cfs_mutex_unlock(&obd->obd_olg.olg_cat_processing);
465 OBD_FREE_LARGE(idarray, size);
469 EXPORT_SYMBOL(llog_catalog_list);