4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
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
39 #include <obd_class.h>
40 #include <lustre_log.h>
41 #include "llog_internal.h"
43 static int str2logid(struct llog_logid *logid, char *str, int len)
45 char *start, *end, *endp;
53 if (start - str >= len - 1)
55 end = strchr(start, '#');
56 if (end == NULL || end == start)
60 logid->lgl_oid = simple_strtoull(start, &endp, 0);
65 if (start - str >= len - 1)
67 end = strchr(start, '#');
68 if (end == NULL || end == start)
72 logid->lgl_oseq = simple_strtoull(start, &endp, 0);
77 if (start - str >= len - 1)
79 logid->lgl_ogen = simple_strtoul(start, &endp, 16);
86 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
87 struct llog_rec_hdr *rec, void *data)
89 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
90 static int l, remains, from, to;
93 int cur_index, rc = 0;
97 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
99 remains = ioc_data->ioc_inllen4 +
100 cfs_size_round(ioc_data->ioc_inllen1) +
101 cfs_size_round(ioc_data->ioc_inllen2) +
102 cfs_size_round(ioc_data->ioc_inllen3);
103 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
106 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
109 ioc_data->ioc_inllen1 = 0;
110 out = ioc_data->ioc_bulk;
113 cur_index = rec->lrh_index;
114 if (cur_index < from)
116 if (to > 0 && cur_index > to)
117 RETURN(-LLOG_EEMPTY);
119 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
120 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
121 struct llog_handle *loghandle;
123 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
124 l = snprintf(out, remains, "[index]: %05d [type]: "
125 "%02x [len]: %04d failed\n",
126 cur_index, rec->lrh_type,
129 if (handle->lgh_ctxt == NULL)
131 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
134 "cannot find log #"LPX64"#"LPX64"#%08x\n",
135 lir->lid_id.lgl_oid, lir->lid_id.lgl_oseq,
136 lir->lid_id.lgl_ogen);
139 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
140 llog_handle_put(loghandle);
144 switch (rec->lrh_type) {
147 case MDS_UNLINK64_REC:
148 case MDS_SETATTR64_REC:
158 l = snprintf(out, remains, "[index]: %05d [type]: "
159 "%02x [len]: %04d %s\n",
160 cur_index, rec->lrh_type, rec->lrh_len,
161 ok ? "ok" : "failed");
165 CERROR("%s: no space to print log records\n",
166 handle->lgh_ctxt->loc_obd->obd_name);
167 RETURN(-LLOG_EEMPTY);
173 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
174 struct llog_rec_hdr *rec, void *data)
176 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
177 static int l, remains, from, to;
183 if (ioc_data != NULL && ioc_data->ioc_inllen1 > 0) {
185 remains = ioc_data->ioc_inllen4 +
186 cfs_size_round(ioc_data->ioc_inllen1) +
187 cfs_size_round(ioc_data->ioc_inllen2) +
188 cfs_size_round(ioc_data->ioc_inllen3);
189 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
192 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
195 out = ioc_data->ioc_bulk;
196 ioc_data->ioc_inllen1 = 0;
199 cur_index = rec->lrh_index;
200 if (cur_index < from)
202 if (to > 0 && cur_index > to)
203 RETURN(-LLOG_EEMPTY);
205 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
206 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
208 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
209 CERROR("invalid record in catalog\n");
213 l = snprintf(out, remains,
214 "[index]: %05d [logid]: #"LPX64"#"LPX64"#%08x\n",
215 cur_index, lir->lid_id.lgl_oid,
216 lir->lid_id.lgl_oseq, lir->lid_id.lgl_ogen);
217 } else if (rec->lrh_type == OBD_CFG_REC) {
220 rc = class_config_parse_rec(rec, out, remains);
225 l = snprintf(out, remains,
226 "[index]: %05d [type]: %02x [len]: %04d\n",
227 cur_index, rec->lrh_type, rec->lrh_len);
232 CERROR("not enough space for print log records\n");
233 RETURN(-LLOG_EEMPTY);
238 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
239 struct llog_logid *logid)
241 struct llog_handle *log;
246 rc = llog_cat_id2handle(env, cat, &log, logid);
248 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
249 logid->lgl_oid, logid->lgl_oseq, logid->lgl_ogen);
253 rc = llog_destroy(env, log);
255 CDEBUG(D_IOCTL, "cannot destroy log\n");
258 llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
260 llog_handle_put(log);
265 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
266 struct llog_rec_hdr *rec, void *data)
268 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
272 if (rec->lrh_type != LLOG_LOGID_MAGIC)
274 rc = llog_remove_log(env, handle, &lir->lid_id);
280 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
281 struct obd_ioctl_data *data)
283 struct llog_logid logid;
285 struct llog_handle *handle = NULL;
289 if (*data->ioc_inlbuf1 == '#') {
290 rc = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
293 rc = llog_open(env, ctxt, &handle, &logid, NULL,
297 } else if (*data->ioc_inlbuf1 == '$') {
298 char *name = data->ioc_inlbuf1 + 1;
300 rc = llog_open(env, ctxt, &handle, NULL, name,
308 rc = llog_init_handle(env, handle, 0, NULL);
310 GOTO(out_close, rc = -ENOENT);
313 case OBD_IOC_LLOG_INFO: {
315 int remains = data->ioc_inllen2 +
316 cfs_size_round(data->ioc_inllen1);
317 char *out = data->ioc_bulk;
319 l = snprintf(out, remains,
320 "logid: #"LPX64"#"LPX64"#%08x\n"
322 "records count: %d\n"
324 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_oseq,
325 handle->lgh_id.lgl_ogen,
326 handle->lgh_hdr->llh_flags,
327 handle->lgh_hdr->llh_flags &
328 LLOG_F_IS_CAT ? "cat" : "plain",
329 handle->lgh_hdr->llh_count,
330 handle->lgh_last_idx);
334 CERROR("%s: not enough space for log header info\n",
335 ctxt->loc_obd->obd_name);
340 case OBD_IOC_LLOG_CHECK:
341 LASSERT(data->ioc_inllen1 > 0);
342 rc = llog_process(env, handle, llog_check_cb, data, NULL);
343 if (rc == -LLOG_EEMPTY)
348 case OBD_IOC_LLOG_PRINT:
349 LASSERT(data->ioc_inllen1 > 0);
350 rc = llog_process(env, handle, llog_print_cb, data, NULL);
351 if (rc == -LLOG_EEMPTY)
356 case OBD_IOC_LLOG_CANCEL: {
357 struct llog_cookie cookie;
358 struct llog_logid plain;
361 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
363 GOTO(out_close, rc = -EINVAL);
365 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
366 rc = llog_cancel_rec(NULL, handle, cookie.lgc_index);
368 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
369 GOTO(out_close, rc = -EINVAL);
372 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
373 GOTO(out_close, rc = -ENOTTY);
375 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
378 cookie.lgc_lgl = plain;
379 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
384 case OBD_IOC_LLOG_REMOVE: {
385 struct llog_logid plain;
387 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
388 rc = llog_destroy(env, handle);
390 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
391 GOTO(out_close, rc = -EINVAL);
394 if (data->ioc_inlbuf2 > 0) {
395 /* remove indicate log from the catalog */
396 rc = str2logid(&plain, data->ioc_inlbuf2,
400 rc = llog_remove_log(env, handle, &plain);
402 /* remove all the log of the catalog */
403 rc = llog_process(env, handle, llog_delete_cb, NULL,
411 CERROR("%s: Unknown ioctl cmd %#x\n",
412 ctxt->loc_obd->obd_name, cmd);
413 GOTO(out_close, rc = -ENOTTY);
417 if (handle->lgh_hdr &&
418 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
419 llog_cat_close(env, handle);
421 llog_close(env, handle);
424 EXPORT_SYMBOL(llog_ioctl);
426 #ifdef HAVE_LDISKFS_OSD
427 int llog_catalog_list(struct obd_device *obd, int count,
428 struct obd_ioctl_data *data)
431 struct llog_catid *idarray;
432 struct llog_logid *id;
433 char name[32] = CATLIST;
435 int l, remains, rc = 0;
438 size = sizeof(*idarray) * count;
440 OBD_ALLOC_LARGE(idarray, size);
444 mutex_lock(&obd->obd_olg.olg_cat_processing);
445 rc = llog_get_cat_list(obd, name, 0, count, idarray);
449 out = data->ioc_bulk;
450 remains = data->ioc_inllen1;
451 for (i = 0; i < count; i++) {
452 id = &idarray[i].lci_logid;
453 l = snprintf(out, remains,
454 "catalog log: #"LPX64"#"LPX64"#%08x\n",
455 id->lgl_oid, id->lgl_oseq, id->lgl_ogen);
459 CWARN("not enough memory for catlog list\n");
464 /* release semaphore */
465 mutex_unlock(&obd->obd_olg.olg_cat_processing);
467 OBD_FREE_LARGE(idarray, size);
471 EXPORT_SYMBOL(llog_catalog_list);