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] == '[') {
49 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 logid_to_fid(logid, &sfid);
58 RETURN(num == 3 && fid_is_sane(&sfid) ? 0 : -EINVAL);
61 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
63 * logids used to be input in the form "#id#seq:ogen" before they
64 * were changed over to accept the FID [seq:oid:ver] format.
65 * This is accepted for compatibility reasons, though I doubt
66 * anyone is actually using this for anything.
72 if (start - str >= len - 1)
74 end = strchr(start, '#');
75 if (end == NULL || end == start)
79 rc = kstrtoull(start, 0, &id);
84 if (start - str >= len - 1)
87 end = strchr(start, '#');
88 if (!end || end == start)
92 rc = kstrtoull(start, 0, &seq);
96 ostid_set_seq(&logid->lgl_oi, seq);
97 if (ostid_set_id(&logid->lgl_oi, id))
101 if (start - str >= len - 1)
104 rc = kstrtouint(start, 16, &ogen);
107 logid->lgl_ogen = ogen;
115 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
116 struct llog_rec_hdr *rec, void *data)
118 struct obd_ioctl_data *ioc_data = data;
119 static int l, remains;
120 static long from, to;
126 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
128 remains = ioc_data->ioc_inllen4 +
129 round_up(ioc_data->ioc_inllen1, 8) +
130 round_up(ioc_data->ioc_inllen2, 8) +
131 round_up(ioc_data->ioc_inllen3, 8);
133 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
137 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
141 ioc_data->ioc_inllen1 = 0;
142 out = ioc_data->ioc_bulk;
145 cur_index = rec->lrh_index;
146 if (cur_index < from)
148 if (to > 0 && cur_index > to)
149 RETURN(-LLOG_EEMPTY);
151 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
152 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
153 struct llog_handle *loghandle;
155 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
156 l = snprintf(out, remains,
157 "[index]: %05d [type]: %02x [len]: %04d failed\n",
158 cur_index, rec->lrh_type,
161 if (handle->lgh_ctxt == NULL)
163 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
165 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
166 PFID(&lir->lid_id.lgl_oi.oi_fid),
167 lir->lid_id.lgl_ogen);
170 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
171 llog_handle_put(env, loghandle);
175 switch (rec->lrh_type) {
178 case MDS_UNLINK64_REC:
179 case MDS_SETATTR64_REC:
189 l = snprintf(out, remains, "[index]: %05d [type]: "
190 "%02x [len]: %04d %s\n",
191 cur_index, rec->lrh_type, rec->lrh_len,
192 ok ? "ok" : "failed");
196 CERROR("%s: no space to print log records\n",
197 handle->lgh_ctxt->loc_obd->obd_name);
198 RETURN(-LLOG_EEMPTY);
204 struct llog_print_data {
205 struct obd_ioctl_data *lprd_data;
206 unsigned int lprd_cfg_flags;
210 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
211 struct llog_rec_hdr *rec, void *data)
213 struct llog_print_data *lprd = data;
214 struct obd_ioctl_data *ioc_data = lprd->lprd_data;
215 static int l, remains;
216 static long from, to;
222 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
224 remains = ioc_data->ioc_inllen4 +
225 round_up(ioc_data->ioc_inllen1, 8) +
226 round_up(ioc_data->ioc_inllen2, 8) +
227 round_up(ioc_data->ioc_inllen3, 8);
229 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
233 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
237 out = ioc_data->ioc_bulk;
238 ioc_data->ioc_inllen1 = 0;
241 cur_index = rec->lrh_index;
242 if (cur_index < from)
244 if (to > 0 && cur_index > to)
245 RETURN(-LLOG_EEMPTY);
247 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
248 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
250 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
251 CERROR("invalid record in catalog\n");
255 l = snprintf(out, remains,
256 "[index]: %05d [logid]: "DFID":%x\n",
257 cur_index, PFID(&lir->lid_id.lgl_oi.oi_fid),
258 lir->lid_id.lgl_ogen);
259 } else if (rec->lrh_type == OBD_CFG_REC) {
262 rc = class_config_yaml_output(rec, out, remains,
263 &lprd->lprd_cfg_flags,
269 l = snprintf(out, remains,
270 "[index]: %05d [type]: %02x [len]: %04d\n",
271 cur_index, rec->lrh_type, rec->lrh_len);
276 CERROR("not enough space for print log records\n");
277 RETURN(-LLOG_EEMPTY);
282 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
283 struct llog_logid *logid)
285 struct llog_handle *log;
290 rc = llog_cat_id2handle(env, cat, &log, logid);
292 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
293 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
297 rc = llog_destroy(env, log);
299 CDEBUG(D_IOCTL, "cannot destroy log "DFID":%x\n",
300 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
303 llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
305 llog_handle_put(env, log);
310 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
311 struct llog_rec_hdr *rec, void *data)
313 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
317 if (rec->lrh_type != LLOG_LOGID_MAGIC)
319 rc = llog_remove_log(env, handle, &lir->lid_id);
325 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
326 struct obd_ioctl_data *data)
328 struct llog_logid logid;
330 struct llog_handle *handle = NULL;
331 char *logname, start;
335 logname = data->ioc_inlbuf1;
337 if (start == '#' || start == '[') {
338 rc = str2logid(&logid, logname, data->ioc_inllen1);
341 rc = llog_open(env, ctxt, &handle, &logid, NULL,
345 } else if (start == '$' || isalpha(start) || isdigit(start)) {
349 rc = llog_open(env, ctxt, &handle, NULL, logname,
355 CDEBUG(D_INFO, "%s: invalid log name '%s': rc = %d\n",
356 ctxt->loc_obd->obd_name, logname, rc);
360 rc = llog_init_handle(env, handle, 0, NULL);
362 GOTO(out_close, rc = -ENOENT);
365 case OBD_IOC_LLOG_INFO: {
367 int remains = data->ioc_inllen2 +
368 cfs_size_round(data->ioc_inllen1);
369 char *out = data->ioc_bulk;
371 l = snprintf(out, remains,
374 "records_count: %d\n"
376 PFID(&handle->lgh_id.lgl_oi.oi_fid),
377 handle->lgh_id.lgl_ogen,
378 handle->lgh_hdr->llh_flags,
379 handle->lgh_hdr->llh_flags &
380 LLOG_F_IS_CAT ? "cat" : "plain",
381 handle->lgh_hdr->llh_count,
382 handle->lgh_last_idx);
386 CERROR("%s: not enough space for log header info\n",
387 ctxt->loc_obd->obd_name);
392 case OBD_IOC_LLOG_CHECK:
393 LASSERT(data->ioc_inllen1 > 0);
394 rc = llog_process(env, handle, llog_check_cb, data, NULL);
395 if (rc == -LLOG_EEMPTY)
400 case OBD_IOC_LLOG_PRINT: {
401 struct llog_print_data lprd = {
403 .lprd_raw = data->ioc_u32_1,
406 LASSERT(data->ioc_inllen1 > 0);
407 rc = llog_process(env, handle, llog_print_cb, &lprd, NULL);
408 if (rc == -LLOG_EEMPTY)
414 case OBD_IOC_LLOG_CANCEL: {
415 struct llog_cookie cookie;
416 struct llog_logid plain;
419 rc = kstrtouint(data->ioc_inlbuf3, 0, &lgc_index);
422 cookie.lgc_index = lgc_index;
424 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
425 rc = llog_cancel_rec(env, handle, cookie.lgc_index);
427 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
428 GOTO(out_close, rc = -EINVAL);
431 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
432 GOTO(out_close, rc = -ENOTTY);
434 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
437 cookie.lgc_lgl = plain;
438 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
443 case OBD_IOC_LLOG_REMOVE: {
444 struct llog_logid plain;
446 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
447 rc = llog_destroy(env, handle);
449 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
450 GOTO(out_close, rc = -EINVAL);
453 if (data->ioc_inlbuf2) {
454 /* remove indicate log from the catalog */
455 rc = str2logid(&plain, data->ioc_inlbuf2,
459 rc = llog_remove_log(env, handle, &plain);
461 /* remove all the log of the catalog */
462 rc = llog_process(env, handle, llog_delete_cb, NULL,
470 CERROR("%s: Unknown ioctl cmd %#x\n",
471 ctxt->loc_obd->obd_name, cmd);
472 GOTO(out_close, rc = -ENOTTY);
476 if (handle->lgh_hdr &&
477 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
478 llog_cat_close(env, handle);
480 llog_close(env, handle);
483 EXPORT_SYMBOL(llog_ioctl);
485 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
486 int count, struct obd_ioctl_data *data,
487 const struct lu_fid *fid)
490 struct llog_catid *idarray;
491 struct llog_logid *id;
493 int l, remains, rc = 0;
497 if (count == 0) { /* get total number of logs */
498 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
504 size = sizeof(*idarray) * count;
506 OBD_ALLOC_LARGE(idarray, size);
510 rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
514 out = data->ioc_bulk;
515 remains = data->ioc_inllen1;
516 /* OBD_FAIL: fetch the catalog records from the specified one */
517 if (OBD_FAIL_CHECK(OBD_FAIL_CATLIST))
518 data->ioc_count = cfs_fail_val - 1;
519 for (i = data->ioc_count; i < count; i++) {
520 id = &idarray[i].lci_logid;
521 l = snprintf(out, remains, "catalog_log: "DFID":%x\n",
522 PFID(&id->lgl_oi.oi_fid), id->lgl_ogen);
527 /* the print is not complete */
529 data->ioc_bulk[out - data->ioc_bulk - l] = '\0';
532 data->ioc_count = i++;
539 OBD_FREE_LARGE(idarray, size);
542 EXPORT_SYMBOL(llog_catalog_list);