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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LOG
35 #include <obd_class.h>
36 #include <lustre_ioctl.h>
37 #include <lustre_log.h>
38 #include "llog_internal.h"
40 static int str2logid(struct llog_logid *logid, char *str, int len)
42 char *start, *end, *endp;
51 if (start - str >= len - 1)
53 end = strchr(start, '#');
54 if (end == NULL || end == start)
58 id = simple_strtoull(start, &endp, 0);
63 if (start - str >= len - 1)
65 end = strchr(start, '#');
66 if (end == NULL || end == start)
70 seq = simple_strtoull(start, &endp, 0);
74 ostid_set_seq(&logid->lgl_oi, seq);
75 ostid_set_id(&logid->lgl_oi, id);
78 if (start - str >= len - 1)
80 logid->lgl_ogen = simple_strtoul(start, &endp, 16);
87 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
88 struct llog_rec_hdr *rec, void *data)
90 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
91 static int l, remains;
95 int cur_index, rc = 0;
99 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
101 remains = ioc_data->ioc_inllen4 +
102 cfs_size_round(ioc_data->ioc_inllen1) +
103 cfs_size_round(ioc_data->ioc_inllen2) +
104 cfs_size_round(ioc_data->ioc_inllen3);
105 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
108 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
111 ioc_data->ioc_inllen1 = 0;
112 out = ioc_data->ioc_bulk;
115 cur_index = rec->lrh_index;
116 if (cur_index < from)
118 if (to > 0 && cur_index > to)
119 RETURN(-LLOG_EEMPTY);
121 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
122 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
123 struct llog_handle *loghandle;
125 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
126 l = snprintf(out, remains, "[index]: %05d [type]: "
127 "%02x [len]: %04d failed\n",
128 cur_index, rec->lrh_type,
131 if (handle->lgh_ctxt == NULL)
133 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
135 CDEBUG(D_IOCTL, "cannot find log #"DOSTID"#%08x\n",
136 POSTID(&lir->lid_id.lgl_oi),
137 lir->lid_id.lgl_ogen);
140 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
141 llog_handle_put(loghandle);
145 switch (rec->lrh_type) {
148 case MDS_UNLINK64_REC:
149 case MDS_SETATTR64_REC:
159 l = snprintf(out, remains, "[index]: %05d [type]: "
160 "%02x [len]: %04d %s\n",
161 cur_index, rec->lrh_type, rec->lrh_len,
162 ok ? "ok" : "failed");
166 CERROR("%s: no space to print log records\n",
167 handle->lgh_ctxt->loc_obd->obd_name);
168 RETURN(-LLOG_EEMPTY);
174 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
175 struct llog_rec_hdr *rec, void *data)
177 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
178 static int l, remains;
179 static long from, to;
185 if (ioc_data != NULL && ioc_data->ioc_inllen1 > 0) {
187 remains = ioc_data->ioc_inllen4 +
188 cfs_size_round(ioc_data->ioc_inllen1) +
189 cfs_size_round(ioc_data->ioc_inllen2) +
190 cfs_size_round(ioc_data->ioc_inllen3);
191 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
194 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
197 out = ioc_data->ioc_bulk;
198 ioc_data->ioc_inllen1 = 0;
201 cur_index = rec->lrh_index;
202 if (cur_index < from)
204 if (to > 0 && cur_index > to)
205 RETURN(-LLOG_EEMPTY);
207 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
208 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
210 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
211 CERROR("invalid record in catalog\n");
215 l = snprintf(out, remains,
216 "[index]: %05d [logid]: #"DOSTID"#%08x\n",
217 cur_index, POSTID(&lir->lid_id.lgl_oi),
218 lir->lid_id.lgl_ogen);
219 } else if (rec->lrh_type == OBD_CFG_REC) {
222 rc = class_config_yaml_output(rec, out, remains);
227 l = snprintf(out, remains,
228 "[index]: %05d [type]: %02x [len]: %04d\n",
229 cur_index, rec->lrh_type, rec->lrh_len);
234 CERROR("not enough space for print log records\n");
235 RETURN(-LLOG_EEMPTY);
240 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
241 struct llog_logid *logid)
243 struct llog_handle *log;
248 rc = llog_cat_id2handle(env, cat, &log, logid);
250 CDEBUG(D_IOCTL, "cannot find log #"DOSTID"#%08x\n",
251 POSTID(&logid->lgl_oi), logid->lgl_ogen);
255 rc = llog_destroy(env, log);
257 CDEBUG(D_IOCTL, "cannot destroy log\n");
260 llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
262 llog_handle_put(log);
267 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
268 struct llog_rec_hdr *rec, void *data)
270 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
274 if (rec->lrh_type != LLOG_LOGID_MAGIC)
276 rc = llog_remove_log(env, handle, &lir->lid_id);
282 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
283 struct obd_ioctl_data *data)
285 struct llog_logid logid;
287 struct llog_handle *handle = NULL;
291 if (*data->ioc_inlbuf1 == '#') {
292 rc = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
295 rc = llog_open(env, ctxt, &handle, &logid, NULL,
299 } else if (*data->ioc_inlbuf1 == '$') {
300 char *name = data->ioc_inlbuf1 + 1;
302 rc = llog_open(env, ctxt, &handle, NULL, name,
310 rc = llog_init_handle(env, handle, 0, NULL);
312 GOTO(out_close, rc = -ENOENT);
315 case OBD_IOC_LLOG_INFO: {
317 int remains = data->ioc_inllen2 +
318 cfs_size_round(data->ioc_inllen1);
319 char *out = data->ioc_bulk;
321 l = snprintf(out, remains,
322 "logid: #"DOSTID"#%08x\n"
324 "records count: %d\n"
326 POSTID(&handle->lgh_id.lgl_oi),
327 handle->lgh_id.lgl_ogen,
328 handle->lgh_hdr->llh_flags,
329 handle->lgh_hdr->llh_flags &
330 LLOG_F_IS_CAT ? "cat" : "plain",
331 handle->lgh_hdr->llh_count,
332 handle->lgh_last_idx);
336 CERROR("%s: not enough space for log header info\n",
337 ctxt->loc_obd->obd_name);
342 case OBD_IOC_LLOG_CHECK:
343 LASSERT(data->ioc_inllen1 > 0);
344 rc = llog_process(env, handle, llog_check_cb, data, NULL);
345 if (rc == -LLOG_EEMPTY)
350 case OBD_IOC_LLOG_PRINT:
351 LASSERT(data->ioc_inllen1 > 0);
352 rc = llog_process(env, handle, llog_print_cb, data, NULL);
353 if (rc == -LLOG_EEMPTY)
358 case OBD_IOC_LLOG_CANCEL: {
359 struct llog_cookie cookie;
360 struct llog_logid plain;
363 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
365 GOTO(out_close, rc = -EINVAL);
367 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
368 rc = llog_cancel_rec(env, handle, cookie.lgc_index);
370 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
371 GOTO(out_close, rc = -EINVAL);
374 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
375 GOTO(out_close, rc = -ENOTTY);
377 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
380 cookie.lgc_lgl = plain;
381 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
386 case OBD_IOC_LLOG_REMOVE: {
387 struct llog_logid plain;
389 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
390 rc = llog_destroy(env, handle);
392 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
393 GOTO(out_close, rc = -EINVAL);
396 if (data->ioc_inlbuf2 > 0) {
397 /* remove indicate log from the catalog */
398 rc = str2logid(&plain, data->ioc_inlbuf2,
402 rc = llog_remove_log(env, handle, &plain);
404 /* remove all the log of the catalog */
405 rc = llog_process(env, handle, llog_delete_cb, NULL,
413 CERROR("%s: Unknown ioctl cmd %#x\n",
414 ctxt->loc_obd->obd_name, cmd);
415 GOTO(out_close, rc = -ENOTTY);
419 if (handle->lgh_hdr &&
420 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
421 llog_cat_close(env, handle);
423 llog_close(env, handle);
426 EXPORT_SYMBOL(llog_ioctl);
428 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
429 int count, struct obd_ioctl_data *data,
430 const struct lu_fid *fid)
433 struct llog_catid *idarray;
434 struct llog_logid *id;
436 int l, remains, rc = 0;
440 if (count == 0) { /* get total number of logs */
441 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
447 size = sizeof(*idarray) * count;
449 OBD_ALLOC_LARGE(idarray, size);
453 rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
457 out = data->ioc_bulk;
458 remains = data->ioc_inllen1;
459 for (i = 0; i < count; i++) {
460 id = &idarray[i].lci_logid;
461 l = snprintf(out, remains,
462 "catalog log: #"DOSTID"#%08x\n",
471 OBD_FREE_LARGE(idarray, size);
474 EXPORT_SYMBOL(llog_catalog_list);