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 #define MARKER_DIFF 10
215 int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
216 struct llog_rec_hdr *rec, void *data)
218 struct llog_print_data *lprd = data;
219 struct obd_ioctl_data *ioc_data = lprd->lprd_data;
220 static int l, remains;
221 static long from, to;
227 if (unlikely(!ioc_data))
230 if (ioc_data->ioc_inllen1 > 0) {
232 remains = ioc_data->ioc_inllen4 +
233 round_up(ioc_data->ioc_inllen1, 8) +
234 round_up(ioc_data->ioc_inllen2, 8) +
235 round_up(ioc_data->ioc_inllen3, 8);
237 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
241 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
245 out = ioc_data->ioc_bulk;
246 ioc_data->ioc_inllen1 = 0;
247 ioc_data->ioc_offset = 0;
248 ioc_data->ioc_count = remains;
249 } else if (ioc_data) {
250 out = ioc_data->ioc_bulk + ioc_data->ioc_offset;
251 remains = ioc_data->ioc_count;
254 cur_index = rec->lrh_index;
255 ioc_data->ioc_u32_2 = llog_idx_is_eof(handle, cur_index);
256 if (from > MARKER_DIFF && cur_index >= from - MARKER_DIFF &&
258 /* LU-15706: try to remember the marker cfg_flag that the "from"
259 * is using, in case that the "from" record doesn't know its
260 * "SKIP" or not flag.
262 llog_get_marker_cfg_flags(rec, &lprd->lprd_cfg_flags);
264 if (cur_index < from)
266 if (to > 0 && cur_index > to)
267 RETURN(-LLOG_EEMPTY);
269 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
270 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
272 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
273 CERROR("invalid record in catalog\n");
277 l = snprintf(out, remains, "[index]: %05d [logid]: "DFID"\n",
278 cur_index, PLOGID(&lir->lid_id));
279 } else if (rec->lrh_type == OBD_CFG_REC) {
282 rc = class_config_yaml_output(rec, out, remains,
283 &lprd->lprd_cfg_flags,
289 l = snprintf(out, remains,
290 "[index]: %05d [type]: %02x [len]: %04d\n",
291 cur_index, rec->lrh_type, rec->lrh_len);
296 CERROR("not enough space for print log records\n");
297 RETURN(-LLOG_EEMPTY);
301 /* save offset and remains, then we don't always rely on those
302 * static variables, which is more flexible.
304 ioc_data->ioc_offset += l;
305 ioc_data->ioc_count = remains;
310 EXPORT_SYMBOL(llog_print_cb);
312 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
313 struct llog_logid *logid)
315 struct llog_handle *log;
320 rc = llog_cat_id2handle(env, cat, &log, logid);
322 CDEBUG(D_IOCTL, "cannot find log "DFID"\n", PLOGID(logid));
326 rc = llog_destroy(env, log);
328 CDEBUG(D_IOCTL, "cannot destroy log "DFID"\n", PLOGID(logid));
331 llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
333 llog_handle_put(env, log);
338 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
339 struct llog_rec_hdr *rec, void *data)
341 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
345 if (rec->lrh_type != LLOG_LOGID_MAGIC)
347 rc = llog_remove_log(env, handle, &lir->lid_id);
352 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
353 struct obd_ioctl_data *data)
355 struct llog_logid logid;
357 struct llog_handle *handle = NULL;
358 char *logname, start;
362 logname = data->ioc_inlbuf1;
364 if (start == '#' || start == '[') {
365 rc = str2logid(&logid, logname, data->ioc_inllen1);
368 rc = llog_open(env, ctxt, &handle, &logid, NULL,
372 } else if (start == '$' || isalpha(start) || isdigit(start)) {
376 rc = llog_open(env, ctxt, &handle, NULL, logname,
382 CDEBUG(D_INFO, "%s: invalid log name '%s': rc = %d\n",
383 ctxt->loc_obd->obd_name, logname, rc);
387 rc = llog_init_handle(env, handle, 0, NULL);
389 GOTO(out_close, rc = -ENOENT);
392 case OBD_IOC_LLOG_INFO: {
394 int remains = data->ioc_inllen2 +
395 cfs_size_round(data->ioc_inllen1);
396 char *out = data->ioc_bulk;
398 l = snprintf(out, remains,
401 "records_count: %d\n"
403 PLOGID(&handle->lgh_id),
404 handle->lgh_hdr->llh_flags,
405 handle->lgh_hdr->llh_flags &
406 LLOG_F_IS_CAT ? "cat" : "plain",
407 handle->lgh_hdr->llh_count,
408 handle->lgh_last_idx);
412 CERROR("%s: not enough space for log header info\n",
413 ctxt->loc_obd->obd_name);
418 case OBD_IOC_LLOG_CHECK:
419 LASSERT(data->ioc_inllen1 > 0);
420 rc = llog_process(env, handle, llog_check_cb, data, NULL);
421 if (rc == -LLOG_EEMPTY)
426 case OBD_IOC_LLOG_PRINT: {
427 struct llog_print_data lprd = {
429 .lprd_raw = data->ioc_u32_1,
432 LASSERT(data->ioc_inllen1 > 0);
433 rc = llog_process(env, handle, llog_print_cb, &lprd, NULL);
434 if (rc == -LLOG_EEMPTY)
440 case OBD_IOC_LLOG_CANCEL: {
441 struct llog_cookie cookie;
442 struct llog_logid plain;
445 rc = kstrtouint(data->ioc_inlbuf3, 0, &lgc_index);
448 cookie.lgc_index = lgc_index;
450 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
451 rc = llog_cancel_rec(env, handle, cookie.lgc_index);
453 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
454 GOTO(out_close, rc = -EINVAL);
457 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
458 GOTO(out_close, rc = -ENOTTY);
460 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
463 cookie.lgc_lgl = plain;
464 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
469 case OBD_IOC_LLOG_REMOVE: {
470 struct llog_logid plain;
472 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
473 rc = llog_destroy(env, handle);
475 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
476 GOTO(out_close, rc = -EINVAL);
479 if (data->ioc_inlbuf2) {
480 /* remove indicate log from the catalog */
481 rc = str2logid(&plain, data->ioc_inlbuf2,
485 rc = llog_remove_log(env, handle, &plain);
487 /* remove all the log of the catalog */
488 rc = llog_process(env, handle, llog_delete_cb, NULL,
496 CERROR("%s: Unknown ioctl cmd %#x\n",
497 ctxt->loc_obd->obd_name, cmd);
498 GOTO(out_close, rc = -ENOTTY);
502 if (handle->lgh_hdr &&
503 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
504 llog_cat_close(env, handle);
506 llog_close(env, handle);
509 EXPORT_SYMBOL(llog_ioctl);
511 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
512 int count, struct obd_ioctl_data *data,
513 const struct lu_fid *fid)
516 struct llog_catid *idarray;
517 struct llog_logid *id;
519 int l, remains, rc = 0;
523 if (count == 0) { /* get total number of logs */
524 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
530 size = sizeof(*idarray) * count;
532 OBD_ALLOC_LARGE(idarray, size);
536 rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
540 out = data->ioc_bulk;
541 remains = data->ioc_inllen1;
542 /* OBD_FAIL: fetch the catalog records from the specified one */
543 if (OBD_FAIL_CHECK(OBD_FAIL_CATLIST))
544 data->ioc_count = cfs_fail_val - 1;
545 for (i = data->ioc_count; i < count; i++) {
546 id = &idarray[i].lci_logid;
547 l = snprintf(out, remains, "catalog_log: "DFID"\n",
553 /* the print is not complete */
555 data->ioc_bulk[out - data->ioc_bulk - l] = '\0';
558 data->ioc_count = i++;
565 OBD_FREE_LARGE(idarray, size);
568 EXPORT_SYMBOL(llog_catalog_list);