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, 2017, 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 <uapi/linux/lustre/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 unsigned long long id, seq;
49 if (start[0] == '[') {
50 struct lu_fid *fid = &logid->lgl_oi.oi_fid;
55 num = sscanf(start + 1, SFID, RFID(fid));
56 CDEBUG(D_INFO, DFID":%x\n", PFID(fid), logid->lgl_ogen);
57 RETURN(num == 3 && fid_is_sane(fid) ? 0 : -EINVAL);
60 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
62 * logids used to be input in the form "#id#seq:ogen" before they
63 * were changed over to accept the FID [seq:oid:ver] format.
64 * This is accepted for compatibility reasons, though I doubt
65 * anyone is actually using this for anything.
71 if (start - str >= len - 1)
73 end = strchr(start, '#');
74 if (end == NULL || end == start)
78 rc = kstrtoull(start, 0, &id);
83 if (start - str >= len - 1)
86 end = strchr(start, '#');
87 if (!end || end == start)
91 rc = kstrtoull(start, 0, &seq);
95 ostid_set_seq(&logid->lgl_oi, seq);
96 if (ostid_set_id(&logid->lgl_oi, id))
100 if (start - str >= len - 1)
103 rc = kstrtouint(start, 16, &ogen);
106 logid->lgl_ogen = ogen;
114 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
115 struct llog_rec_hdr *rec, void *data)
117 struct obd_ioctl_data *ioc_data = data;
118 static int l, remains;
119 static long from, to;
125 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
127 remains = ioc_data->ioc_inllen4 +
128 round_up(ioc_data->ioc_inllen1, 8) +
129 round_up(ioc_data->ioc_inllen2, 8) +
130 round_up(ioc_data->ioc_inllen3, 8);
132 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
136 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
140 ioc_data->ioc_inllen1 = 0;
141 out = ioc_data->ioc_bulk;
144 cur_index = rec->lrh_index;
145 if (cur_index < from)
147 if (to > 0 && cur_index > to)
148 RETURN(-LLOG_EEMPTY);
150 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
151 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
152 struct llog_handle *loghandle;
154 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
155 l = snprintf(out, remains,
156 "[index]: %05d [type]: %02x [len]: %04d failed\n",
157 cur_index, rec->lrh_type,
160 if (handle->lgh_ctxt == NULL)
162 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
164 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
165 PFID(&lir->lid_id.lgl_oi.oi_fid),
166 lir->lid_id.lgl_ogen);
169 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
170 llog_handle_put(loghandle);
174 switch (rec->lrh_type) {
177 case MDS_UNLINK64_REC:
178 case MDS_SETATTR64_REC:
188 l = snprintf(out, remains, "[index]: %05d [type]: "
189 "%02x [len]: %04d %s\n",
190 cur_index, rec->lrh_type, rec->lrh_len,
191 ok ? "ok" : "failed");
195 CERROR("%s: no space to print log records\n",
196 handle->lgh_ctxt->loc_obd->obd_name);
197 RETURN(-LLOG_EEMPTY);
203 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
204 struct llog_rec_hdr *rec, void *data)
206 struct obd_ioctl_data *ioc_data = data;
207 static int l, remains;
208 static long from, to;
214 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
216 remains = ioc_data->ioc_inllen4 +
217 round_up(ioc_data->ioc_inllen1, 8) +
218 round_up(ioc_data->ioc_inllen2, 8) +
219 round_up(ioc_data->ioc_inllen3, 8);
221 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
225 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
229 out = ioc_data->ioc_bulk;
230 ioc_data->ioc_inllen1 = 0;
233 cur_index = rec->lrh_index;
234 if (cur_index < from)
236 if (to > 0 && cur_index > to)
237 RETURN(-LLOG_EEMPTY);
239 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
240 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
242 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
243 CERROR("invalid record in catalog\n");
247 l = snprintf(out, remains,
248 "[index]: %05d [logid]: "DFID":%x\n",
249 cur_index, PFID(&lir->lid_id.lgl_oi.oi_fid),
250 lir->lid_id.lgl_ogen);
251 } else if (rec->lrh_type == OBD_CFG_REC) {
254 rc = class_config_yaml_output(rec, out, remains);
259 l = snprintf(out, remains,
260 "[index]: %05d [type]: %02x [len]: %04d\n",
261 cur_index, rec->lrh_type, rec->lrh_len);
266 CERROR("not enough space for print log records\n");
267 RETURN(-LLOG_EEMPTY);
272 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
273 struct llog_logid *logid)
275 struct llog_handle *log;
280 rc = llog_cat_id2handle(env, cat, &log, logid);
282 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
283 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
287 rc = llog_destroy(env, log);
289 CDEBUG(D_IOCTL, "cannot destroy log "DFID":%x\n",
290 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
293 llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
295 llog_handle_put(log);
300 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
301 struct llog_rec_hdr *rec, void *data)
303 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
307 if (rec->lrh_type != LLOG_LOGID_MAGIC)
309 rc = llog_remove_log(env, handle, &lir->lid_id);
315 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
316 struct obd_ioctl_data *data)
318 struct llog_logid logid;
320 struct llog_handle *handle = NULL;
325 logname = data->ioc_inlbuf1;
326 if (logname[0] == '#' || logname[0] == '[') {
327 rc = str2logid(&logid, logname, data->ioc_inllen1);
330 rc = llog_open(env, ctxt, &handle, &logid, NULL,
334 } else if (logname[0] == '$' || isalpha(logname[0])) {
335 if (logname[0] == '$')
338 rc = llog_open(env, ctxt, &handle, NULL, logname,
346 rc = llog_init_handle(env, handle, 0, NULL);
348 GOTO(out_close, rc = -ENOENT);
351 case OBD_IOC_LLOG_INFO: {
353 int remains = data->ioc_inllen2 +
354 cfs_size_round(data->ioc_inllen1);
355 char *out = data->ioc_bulk;
357 l = snprintf(out, remains,
360 "records_count: %d\n"
362 PFID(&handle->lgh_id.lgl_oi.oi_fid),
363 handle->lgh_id.lgl_ogen,
364 handle->lgh_hdr->llh_flags,
365 handle->lgh_hdr->llh_flags &
366 LLOG_F_IS_CAT ? "cat" : "plain",
367 handle->lgh_hdr->llh_count,
368 handle->lgh_last_idx);
372 CERROR("%s: not enough space for log header info\n",
373 ctxt->loc_obd->obd_name);
378 case OBD_IOC_LLOG_CHECK:
379 LASSERT(data->ioc_inllen1 > 0);
380 rc = llog_process(env, handle, llog_check_cb, data, NULL);
381 if (rc == -LLOG_EEMPTY)
386 case OBD_IOC_LLOG_PRINT:
387 LASSERT(data->ioc_inllen1 > 0);
388 rc = llog_process(env, handle, llog_print_cb, data, NULL);
389 if (rc == -LLOG_EEMPTY)
394 case OBD_IOC_LLOG_CANCEL: {
395 struct llog_cookie cookie;
396 struct llog_logid plain;
399 rc = kstrtouint(data->ioc_inlbuf3, 0, &lgc_index);
402 cookie.lgc_index = lgc_index;
404 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
405 rc = llog_cancel_rec(env, handle, cookie.lgc_index);
407 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
408 GOTO(out_close, rc = -EINVAL);
411 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
412 GOTO(out_close, rc = -ENOTTY);
414 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
417 cookie.lgc_lgl = plain;
418 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
423 case OBD_IOC_LLOG_REMOVE: {
424 struct llog_logid plain;
426 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
427 rc = llog_destroy(env, handle);
429 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
430 GOTO(out_close, rc = -EINVAL);
433 if (data->ioc_inlbuf2) {
434 /* remove indicate log from the catalog */
435 rc = str2logid(&plain, data->ioc_inlbuf2,
439 rc = llog_remove_log(env, handle, &plain);
441 /* remove all the log of the catalog */
442 rc = llog_process(env, handle, llog_delete_cb, NULL,
450 CERROR("%s: Unknown ioctl cmd %#x\n",
451 ctxt->loc_obd->obd_name, cmd);
452 GOTO(out_close, rc = -ENOTTY);
456 if (handle->lgh_hdr &&
457 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
458 llog_cat_close(env, handle);
460 llog_close(env, handle);
463 EXPORT_SYMBOL(llog_ioctl);
465 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
466 int count, struct obd_ioctl_data *data,
467 const struct lu_fid *fid)
470 struct llog_catid *idarray;
471 struct llog_logid *id;
473 int l, remains, rc = 0;
477 if (count == 0) { /* get total number of logs */
478 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
484 size = sizeof(*idarray) * count;
486 OBD_ALLOC_LARGE(idarray, size);
490 rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
494 out = data->ioc_bulk;
495 remains = data->ioc_inllen1;
496 for (i = 0; i < count; i++) {
497 id = &idarray[i].lci_logid;
498 l = snprintf(out, remains, "catalog_log: "DFID":%x\n",
499 PFID(&id->lgl_oi.oi_fid), id->lgl_ogen);
506 OBD_FREE_LARGE(idarray, size);
509 EXPORT_SYMBOL(llog_catalog_list);