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/
32 #define DEBUG_SUBSYSTEM S_LOG
34 #include <obd_class.h>
35 #include <uapi/linux/lustre/lustre_ioctl.h>
36 #include <lustre_log.h>
37 #include "llog_internal.h"
39 static int str2logid(struct llog_logid *logid, char *str, int len)
41 unsigned long long id, seq;
48 if (start[0] == '[') {
53 num = sscanf(start + 1, SFID, RFID(&fid));
54 CDEBUG(D_INFO, "get FID "DFID"\n", PFID(&fid));
55 fid_to_logid(&fid, logid);
56 RETURN(num == 3 && fid_is_sane(&fid) ? 0 : -EINVAL);
59 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
61 * logids used to be input in the form "#id#seq:ogen" before they
62 * were changed over to accept the FID [seq:oid:ver] format.
63 * This is accepted for compatibility reasons, though I doubt
64 * anyone is actually using this for anything.
70 if (start - str >= len - 1)
72 end = strchr(start, '#');
73 if (end == NULL || end == start)
77 rc = kstrtoull(start, 0, &id);
82 if (start - str >= len - 1)
85 end = strchr(start, '#');
86 if (!end || end == start)
90 rc = kstrtoull(start, 0, &seq);
94 ostid_set_seq(&logid->lgl_oi, seq);
95 if (ostid_set_id(&logid->lgl_oi, id))
99 if (start - str >= len - 1)
102 rc = kstrtouint(start, 16, &ogen);
105 logid->lgl_ogen = ogen;
113 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
114 struct llog_rec_hdr *rec, void *data)
116 struct obd_ioctl_data *ioc_data = data;
117 static int l, remains;
118 static long from, to;
124 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
126 remains = ioc_data->ioc_inllen4 +
127 round_up(ioc_data->ioc_inllen1, 8) +
128 round_up(ioc_data->ioc_inllen2, 8) +
129 round_up(ioc_data->ioc_inllen3, 8);
131 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
135 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
139 ioc_data->ioc_inllen1 = 0;
140 out = ioc_data->ioc_bulk;
143 cur_index = rec->lrh_index;
144 if (cur_index < from)
146 if (to > 0 && cur_index > to)
147 RETURN(-LLOG_EEMPTY);
149 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
150 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
151 struct llog_handle *loghandle;
153 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
154 l = snprintf(out, remains,
155 "[index]: %05d [type]: %02x [len]: %04d failed\n",
156 cur_index, rec->lrh_type,
159 if (handle->lgh_ctxt == NULL)
161 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
163 CDEBUG(D_IOCTL, "cannot find log "DFID"\n",
164 PLOGID(&lir->lid_id));
167 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
168 llog_handle_put(env, loghandle);
172 switch (rec->lrh_type) {
175 case MDS_UNLINK64_REC:
176 case MDS_SETATTR64_REC:
186 l = snprintf(out, remains, "[index]: %05d [type]: "
187 "%02x [len]: %04d %s\n",
188 cur_index, rec->lrh_type, rec->lrh_len,
189 ok ? "ok" : "failed");
193 CERROR("%s: no space to print log records\n",
194 handle->lgh_ctxt->loc_obd->obd_name);
195 RETURN(-LLOG_EEMPTY);
201 static inline bool llog_idx_is_eof(struct llog_handle *llh, __u32 cur_idx)
203 __u32 last_idx = llh->lgh_last_idx;
205 /* catalog is wrapped ? */
206 if (unlikely(llh->lgh_hdr->llh_flags & LLOG_F_IS_CAT &&
207 llh->lgh_hdr->llh_cat_idx >= llh->lgh_last_idx &&
208 llh->lgh_hdr->llh_count > 1))
209 last_idx = LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1;
211 return cur_idx >= last_idx;
214 struct llog_print_data {
215 struct obd_ioctl_data *lprd_data;
216 unsigned int lprd_cfg_flags;
220 #define MARKER_DIFF 10
221 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
222 struct llog_rec_hdr *rec, void *data)
224 struct llog_print_data *lprd = data;
225 struct obd_ioctl_data *ioc_data = lprd->lprd_data;
226 static int l, remains;
227 static long from, to;
233 if (unlikely(!ioc_data))
236 if (ioc_data->ioc_inllen1 > 0) {
238 remains = ioc_data->ioc_inllen4 +
239 round_up(ioc_data->ioc_inllen1, 8) +
240 round_up(ioc_data->ioc_inllen2, 8) +
241 round_up(ioc_data->ioc_inllen3, 8);
243 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
247 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
251 out = ioc_data->ioc_bulk;
252 ioc_data->ioc_inllen1 = 0;
255 cur_index = rec->lrh_index;
256 ioc_data->ioc_u32_2 = llog_idx_is_eof(handle, cur_index);
257 if (from > MARKER_DIFF && cur_index >= from - MARKER_DIFF &&
259 /* LU-15706: try to remember the marker cfg_flag that the "from"
260 * is using, in case that the "from" record doesn't know its
261 * "SKIP" or not flag.
263 llog_get_marker_cfg_flags(rec, &lprd->lprd_cfg_flags);
265 if (cur_index < from)
267 if (to > 0 && cur_index > to)
268 RETURN(-LLOG_EEMPTY);
270 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
271 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
273 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
274 CERROR("invalid record in catalog\n");
278 l = snprintf(out, remains, "[index]: %05d [logid]: "DFID"\n",
279 cur_index, PLOGID(&lir->lid_id));
280 } else if (rec->lrh_type == OBD_CFG_REC) {
283 rc = class_config_yaml_output(rec, out, remains,
284 &lprd->lprd_cfg_flags,
290 l = snprintf(out, remains,
291 "[index]: %05d [type]: %02x [len]: %04d\n",
292 cur_index, rec->lrh_type, rec->lrh_len);
297 CERROR("not enough space for print log records\n");
298 RETURN(-LLOG_EEMPTY);
303 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
304 struct llog_logid *logid)
306 struct llog_handle *log;
311 rc = llog_cat_id2handle(env, cat, &log, logid);
313 CDEBUG(D_IOCTL, "cannot find log "DFID"\n", PLOGID(logid));
317 rc = llog_destroy(env, log);
319 CDEBUG(D_IOCTL, "cannot destroy log "DFID"\n", PLOGID(logid));
322 llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
324 llog_handle_put(env, log);
329 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
330 struct llog_rec_hdr *rec, void *data)
332 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
336 if (rec->lrh_type != LLOG_LOGID_MAGIC)
338 rc = llog_remove_log(env, handle, &lir->lid_id);
344 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
345 struct obd_ioctl_data *data)
347 struct llog_logid logid;
349 struct llog_handle *handle = NULL;
350 char *logname, start;
354 logname = data->ioc_inlbuf1;
356 if (start == '#' || start == '[') {
357 rc = str2logid(&logid, logname, data->ioc_inllen1);
360 rc = llog_open(env, ctxt, &handle, &logid, NULL,
364 } else if (start == '$' || isalpha(start) || isdigit(start)) {
368 rc = llog_open(env, ctxt, &handle, NULL, logname,
374 CDEBUG(D_INFO, "%s: invalid log name '%s': rc = %d\n",
375 ctxt->loc_obd->obd_name, logname, rc);
379 rc = llog_init_handle(env, handle, 0, NULL);
381 GOTO(out_close, rc = -ENOENT);
384 case OBD_IOC_LLOG_INFO: {
386 int remains = data->ioc_inllen2 +
387 cfs_size_round(data->ioc_inllen1);
388 char *out = data->ioc_bulk;
390 l = snprintf(out, remains,
393 "records_count: %d\n"
395 PLOGID(&handle->lgh_id),
396 handle->lgh_hdr->llh_flags,
397 handle->lgh_hdr->llh_flags &
398 LLOG_F_IS_CAT ? "cat" : "plain",
399 handle->lgh_hdr->llh_count,
400 handle->lgh_last_idx);
404 CERROR("%s: not enough space for log header info\n",
405 ctxt->loc_obd->obd_name);
410 case OBD_IOC_LLOG_CHECK:
411 LASSERT(data->ioc_inllen1 > 0);
412 rc = llog_process(env, handle, llog_check_cb, data, NULL);
413 if (rc == -LLOG_EEMPTY)
418 case OBD_IOC_LLOG_PRINT: {
419 struct llog_print_data lprd = {
421 .lprd_raw = data->ioc_u32_1,
424 LASSERT(data->ioc_inllen1 > 0);
425 rc = llog_process(env, handle, llog_print_cb, &lprd, NULL);
426 if (rc == -LLOG_EEMPTY)
432 case OBD_IOC_LLOG_CANCEL: {
433 struct llog_cookie cookie;
434 struct llog_logid plain;
437 rc = kstrtouint(data->ioc_inlbuf3, 0, &lgc_index);
440 cookie.lgc_index = lgc_index;
442 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
443 rc = llog_cancel_rec(env, handle, cookie.lgc_index);
445 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
446 GOTO(out_close, rc = -EINVAL);
449 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
450 GOTO(out_close, rc = -ENOTTY);
452 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
455 cookie.lgc_lgl = plain;
456 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
461 case OBD_IOC_LLOG_REMOVE: {
462 struct llog_logid plain;
464 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
465 rc = llog_destroy(env, handle);
467 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
468 GOTO(out_close, rc = -EINVAL);
471 if (data->ioc_inlbuf2) {
472 /* remove indicate log from the catalog */
473 rc = str2logid(&plain, data->ioc_inlbuf2,
477 rc = llog_remove_log(env, handle, &plain);
479 /* remove all the log of the catalog */
480 rc = llog_process(env, handle, llog_delete_cb, NULL,
488 CERROR("%s: Unknown ioctl cmd %#x\n",
489 ctxt->loc_obd->obd_name, cmd);
490 GOTO(out_close, rc = -ENOTTY);
494 if (handle->lgh_hdr &&
495 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
496 llog_cat_close(env, handle);
498 llog_close(env, handle);
501 EXPORT_SYMBOL(llog_ioctl);
503 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
504 int count, struct obd_ioctl_data *data,
505 const struct lu_fid *fid)
508 struct llog_catid *idarray;
509 struct llog_logid *id;
511 int l, remains, rc = 0;
515 if (count == 0) { /* get total number of logs */
516 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
522 size = sizeof(*idarray) * count;
524 OBD_ALLOC_LARGE(idarray, size);
528 rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
532 out = data->ioc_bulk;
533 remains = data->ioc_inllen1;
534 /* OBD_FAIL: fetch the catalog records from the specified one */
535 if (OBD_FAIL_CHECK(OBD_FAIL_CATLIST))
536 data->ioc_count = cfs_fail_val - 1;
537 for (i = data->ioc_count; i < count; i++) {
538 id = &idarray[i].lci_logid;
539 l = snprintf(out, remains, "catalog_log: "DFID"\n",
545 /* the print is not complete */
547 data->ioc_bulk[out - data->ioc_bulk - l] = '\0';
550 data->ioc_count = i++;
557 OBD_FREE_LARGE(idarray, size);
560 EXPORT_SYMBOL(llog_catalog_list);