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;
56 num = sscanf(start + 1, SFID, RFID(fid));
57 CDEBUG(D_INFO, DFID":%x\n", PFID(fid), logid->lgl_ogen);
58 logid_to_fid(logid, &sfid);
59 RETURN(num == 3 && fid_is_sane(&sfid) ? 0 : -EINVAL);
62 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
64 * logids used to be input in the form "#id#seq:ogen" before they
65 * were changed over to accept the FID [seq:oid:ver] format.
66 * This is accepted for compatibility reasons, though I doubt
67 * anyone is actually using this for anything.
73 if (start - str >= len - 1)
75 end = strchr(start, '#');
76 if (end == NULL || end == start)
80 rc = kstrtoull(start, 0, &id);
85 if (start - str >= len - 1)
88 end = strchr(start, '#');
89 if (!end || end == start)
93 rc = kstrtoull(start, 0, &seq);
97 ostid_set_seq(&logid->lgl_oi, seq);
98 if (ostid_set_id(&logid->lgl_oi, id))
102 if (start - str >= len - 1)
105 rc = kstrtouint(start, 16, &ogen);
108 logid->lgl_ogen = ogen;
116 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
117 struct llog_rec_hdr *rec, void *data)
119 struct obd_ioctl_data *ioc_data = data;
120 static int l, remains;
121 static long from, to;
127 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
129 remains = ioc_data->ioc_inllen4 +
130 round_up(ioc_data->ioc_inllen1, 8) +
131 round_up(ioc_data->ioc_inllen2, 8) +
132 round_up(ioc_data->ioc_inllen3, 8);
134 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
138 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
142 ioc_data->ioc_inllen1 = 0;
143 out = ioc_data->ioc_bulk;
146 cur_index = rec->lrh_index;
147 if (cur_index < from)
149 if (to > 0 && cur_index > to)
150 RETURN(-LLOG_EEMPTY);
152 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
153 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
154 struct llog_handle *loghandle;
156 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
157 l = snprintf(out, remains,
158 "[index]: %05d [type]: %02x [len]: %04d failed\n",
159 cur_index, rec->lrh_type,
162 if (handle->lgh_ctxt == NULL)
164 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
166 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
167 PFID(&lir->lid_id.lgl_oi.oi_fid),
168 lir->lid_id.lgl_ogen);
171 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
172 llog_handle_put(env, loghandle);
176 switch (rec->lrh_type) {
179 case MDS_UNLINK64_REC:
180 case MDS_SETATTR64_REC:
190 l = snprintf(out, remains, "[index]: %05d [type]: "
191 "%02x [len]: %04d %s\n",
192 cur_index, rec->lrh_type, rec->lrh_len,
193 ok ? "ok" : "failed");
197 CERROR("%s: no space to print log records\n",
198 handle->lgh_ctxt->loc_obd->obd_name);
199 RETURN(-LLOG_EEMPTY);
205 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
206 struct llog_rec_hdr *rec, void *data)
208 struct obd_ioctl_data *ioc_data = data;
209 static int l, remains;
210 static long from, to;
216 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
218 remains = ioc_data->ioc_inllen4 +
219 round_up(ioc_data->ioc_inllen1, 8) +
220 round_up(ioc_data->ioc_inllen2, 8) +
221 round_up(ioc_data->ioc_inllen3, 8);
223 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
227 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
231 out = ioc_data->ioc_bulk;
232 ioc_data->ioc_inllen1 = 0;
235 cur_index = rec->lrh_index;
236 if (cur_index < from)
238 if (to > 0 && cur_index > to)
239 RETURN(-LLOG_EEMPTY);
241 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
242 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
244 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
245 CERROR("invalid record in catalog\n");
249 l = snprintf(out, remains,
250 "[index]: %05d [logid]: "DFID":%x\n",
251 cur_index, PFID(&lir->lid_id.lgl_oi.oi_fid),
252 lir->lid_id.lgl_ogen);
253 } else if (rec->lrh_type == OBD_CFG_REC) {
256 rc = class_config_yaml_output(rec, out, remains);
261 l = snprintf(out, remains,
262 "[index]: %05d [type]: %02x [len]: %04d\n",
263 cur_index, rec->lrh_type, rec->lrh_len);
268 CERROR("not enough space for print log records\n");
269 RETURN(-LLOG_EEMPTY);
274 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
275 struct llog_logid *logid)
277 struct llog_handle *log;
282 rc = llog_cat_id2handle(env, cat, &log, logid);
284 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
285 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
289 rc = llog_destroy(env, log);
291 CDEBUG(D_IOCTL, "cannot destroy log "DFID":%x\n",
292 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
295 llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
297 llog_handle_put(env, log);
302 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
303 struct llog_rec_hdr *rec, void *data)
305 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
309 if (rec->lrh_type != LLOG_LOGID_MAGIC)
311 rc = llog_remove_log(env, handle, &lir->lid_id);
317 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
318 struct obd_ioctl_data *data)
320 struct llog_logid logid;
322 struct llog_handle *handle = NULL;
323 char *logname, start;
327 logname = data->ioc_inlbuf1;
329 if (start == '#' || start == '[') {
330 rc = str2logid(&logid, logname, data->ioc_inllen1);
333 rc = llog_open(env, ctxt, &handle, &logid, NULL,
337 } else if (start == '$' || isalpha(start) || isdigit(start)) {
341 rc = llog_open(env, ctxt, &handle, NULL, logname,
347 CDEBUG(D_INFO, "%s: invalid log name '%s': rc = %d\n",
348 ctxt->loc_obd->obd_name, logname, rc);
352 rc = llog_init_handle(env, handle, 0, NULL);
354 GOTO(out_close, rc = -ENOENT);
357 case OBD_IOC_LLOG_INFO: {
359 int remains = data->ioc_inllen2 +
360 cfs_size_round(data->ioc_inllen1);
361 char *out = data->ioc_bulk;
363 l = snprintf(out, remains,
366 "records_count: %d\n"
368 PFID(&handle->lgh_id.lgl_oi.oi_fid),
369 handle->lgh_id.lgl_ogen,
370 handle->lgh_hdr->llh_flags,
371 handle->lgh_hdr->llh_flags &
372 LLOG_F_IS_CAT ? "cat" : "plain",
373 handle->lgh_hdr->llh_count,
374 handle->lgh_last_idx);
378 CERROR("%s: not enough space for log header info\n",
379 ctxt->loc_obd->obd_name);
384 case OBD_IOC_LLOG_CHECK:
385 LASSERT(data->ioc_inllen1 > 0);
386 rc = llog_process(env, handle, llog_check_cb, data, NULL);
387 if (rc == -LLOG_EEMPTY)
392 case OBD_IOC_LLOG_PRINT:
393 LASSERT(data->ioc_inllen1 > 0);
394 rc = llog_process(env, handle, llog_print_cb, data, NULL);
395 if (rc == -LLOG_EEMPTY)
400 case OBD_IOC_LLOG_CANCEL: {
401 struct llog_cookie cookie;
402 struct llog_logid plain;
405 rc = kstrtouint(data->ioc_inlbuf3, 0, &lgc_index);
408 cookie.lgc_index = lgc_index;
410 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
411 rc = llog_cancel_rec(env, handle, cookie.lgc_index);
413 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
414 GOTO(out_close, rc = -EINVAL);
417 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
418 GOTO(out_close, rc = -ENOTTY);
420 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
423 cookie.lgc_lgl = plain;
424 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
429 case OBD_IOC_LLOG_REMOVE: {
430 struct llog_logid plain;
432 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
433 rc = llog_destroy(env, handle);
435 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
436 GOTO(out_close, rc = -EINVAL);
439 if (data->ioc_inlbuf2) {
440 /* remove indicate log from the catalog */
441 rc = str2logid(&plain, data->ioc_inlbuf2,
445 rc = llog_remove_log(env, handle, &plain);
447 /* remove all the log of the catalog */
448 rc = llog_process(env, handle, llog_delete_cb, NULL,
456 CERROR("%s: Unknown ioctl cmd %#x\n",
457 ctxt->loc_obd->obd_name, cmd);
458 GOTO(out_close, rc = -ENOTTY);
462 if (handle->lgh_hdr &&
463 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
464 llog_cat_close(env, handle);
466 llog_close(env, handle);
469 EXPORT_SYMBOL(llog_ioctl);
471 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
472 int count, struct obd_ioctl_data *data,
473 const struct lu_fid *fid)
476 struct llog_catid *idarray;
477 struct llog_logid *id;
479 int l, remains, rc = 0;
483 if (count == 0) { /* get total number of logs */
484 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
490 size = sizeof(*idarray) * count;
492 OBD_ALLOC_LARGE(idarray, size);
496 rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
500 out = data->ioc_bulk;
501 remains = data->ioc_inllen1;
502 /* OBD_FAIL: fetch the catalog records from the specified one */
503 if (OBD_FAIL_CHECK(OBD_FAIL_CATLIST))
504 data->ioc_count = cfs_fail_val - 1;
505 for (i = data->ioc_count; i < count; i++) {
506 id = &idarray[i].lci_logid;
507 l = snprintf(out, remains, "catalog_log: "DFID":%x\n",
508 PFID(&id->lgl_oi.oi_fid), id->lgl_ogen);
513 /* the print is not complete */
515 data->ioc_bulk[out - data->ioc_bulk - l] = '\0';
518 data->ioc_count = i++;
525 OBD_FREE_LARGE(idarray, size);
528 EXPORT_SYMBOL(llog_catalog_list);