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)
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. */
69 if (start - str >= len - 1)
71 end = strchr(start, '#');
72 if (end == NULL || end == start)
76 rc = kstrtoull(start, 0, &id);
81 if (start - str >= len - 1)
84 end = strchr(start, '#');
85 if (!end || end == start)
89 rc = kstrtoull(start, 0, &seq);
93 ostid_set_seq(&logid->lgl_oi, seq);
94 if (ostid_set_id(&logid->lgl_oi, id))
98 if (start - str >= len - 1)
101 rc = kstrtouint(start, 16, &ogen);
104 logid->lgl_ogen = ogen;
112 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
113 struct llog_rec_hdr *rec, void *data)
115 struct obd_ioctl_data *ioc_data = data;
116 static int l, remains;
117 static long from, to;
123 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
125 remains = ioc_data->ioc_inllen4 +
126 round_up(ioc_data->ioc_inllen1, 8) +
127 round_up(ioc_data->ioc_inllen2, 8) +
128 round_up(ioc_data->ioc_inllen3, 8);
130 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
134 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
138 ioc_data->ioc_inllen1 = 0;
139 out = ioc_data->ioc_bulk;
142 cur_index = rec->lrh_index;
143 if (cur_index < from)
145 if (to > 0 && cur_index > to)
146 RETURN(-LLOG_EEMPTY);
148 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
149 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
150 struct llog_handle *loghandle;
152 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
153 l = snprintf(out, remains, "[index]: %05d [type]: "
154 "%02x [len]: %04d failed\n",
155 cur_index, rec->lrh_type,
158 if (handle->lgh_ctxt == NULL)
160 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
162 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
163 PFID(&lir->lid_id.lgl_oi.oi_fid),
164 lir->lid_id.lgl_ogen);
167 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
168 llog_handle_put(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 int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
202 struct llog_rec_hdr *rec, void *data)
204 struct obd_ioctl_data *ioc_data = data;
205 static int l, remains;
206 static long from, to;
212 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
214 remains = ioc_data->ioc_inllen4 +
215 round_up(ioc_data->ioc_inllen1, 8) +
216 round_up(ioc_data->ioc_inllen2, 8) +
217 round_up(ioc_data->ioc_inllen3, 8);
219 rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
223 rc = kstrtol(ioc_data->ioc_inlbuf3, 0, &to);
227 out = ioc_data->ioc_bulk;
228 ioc_data->ioc_inllen1 = 0;
231 cur_index = rec->lrh_index;
232 if (cur_index < from)
234 if (to > 0 && cur_index > to)
235 RETURN(-LLOG_EEMPTY);
237 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
238 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
240 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
241 CERROR("invalid record in catalog\n");
245 l = snprintf(out, remains,
246 "[index]: %05d [logid]: "DFID":%x\n",
247 cur_index, PFID(&lir->lid_id.lgl_oi.oi_fid),
248 lir->lid_id.lgl_ogen);
249 } else if (rec->lrh_type == OBD_CFG_REC) {
252 rc = class_config_yaml_output(rec, out, remains);
257 l = snprintf(out, remains,
258 "[index]: %05d [type]: %02x [len]: %04d\n",
259 cur_index, rec->lrh_type, rec->lrh_len);
264 CERROR("not enough space for print log records\n");
265 RETURN(-LLOG_EEMPTY);
270 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
271 struct llog_logid *logid)
273 struct llog_handle *log;
278 rc = llog_cat_id2handle(env, cat, &log, logid);
280 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
281 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
285 rc = llog_destroy(env, log);
287 CDEBUG(D_IOCTL, "cannot destroy log "DFID":%x\n",
288 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
291 llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
293 llog_handle_put(log);
298 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
299 struct llog_rec_hdr *rec, void *data)
301 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
305 if (rec->lrh_type != LLOG_LOGID_MAGIC)
307 rc = llog_remove_log(env, handle, &lir->lid_id);
313 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
314 struct obd_ioctl_data *data)
316 struct llog_logid logid;
318 struct llog_handle *handle = NULL;
323 logname = data->ioc_inlbuf1;
324 if (logname[0] == '#' || logname[0] == '[') {
325 rc = str2logid(&logid, logname, data->ioc_inllen1);
328 rc = llog_open(env, ctxt, &handle, &logid, NULL,
332 } else if (logname[0] == '$' || isalpha(logname[0])) {
333 if (logname[0] == '$')
336 rc = llog_open(env, ctxt, &handle, NULL, logname,
344 rc = llog_init_handle(env, handle, 0, NULL);
346 GOTO(out_close, rc = -ENOENT);
349 case OBD_IOC_LLOG_INFO: {
351 int remains = data->ioc_inllen2 +
352 cfs_size_round(data->ioc_inllen1);
353 char *out = data->ioc_bulk;
355 l = snprintf(out, remains,
358 "records_count: %d\n"
360 PFID(&handle->lgh_id.lgl_oi.oi_fid),
361 handle->lgh_id.lgl_ogen,
362 handle->lgh_hdr->llh_flags,
363 handle->lgh_hdr->llh_flags &
364 LLOG_F_IS_CAT ? "cat" : "plain",
365 handle->lgh_hdr->llh_count,
366 handle->lgh_last_idx);
370 CERROR("%s: not enough space for log header info\n",
371 ctxt->loc_obd->obd_name);
376 case OBD_IOC_LLOG_CHECK:
377 LASSERT(data->ioc_inllen1 > 0);
378 rc = llog_process(env, handle, llog_check_cb, data, NULL);
379 if (rc == -LLOG_EEMPTY)
384 case OBD_IOC_LLOG_PRINT:
385 LASSERT(data->ioc_inllen1 > 0);
386 rc = llog_process(env, handle, llog_print_cb, data, NULL);
387 if (rc == -LLOG_EEMPTY)
392 case OBD_IOC_LLOG_CANCEL: {
393 struct llog_cookie cookie;
394 struct llog_logid plain;
397 rc = kstrtouint(data->ioc_inlbuf3, 0, &lgc_index);
400 cookie.lgc_index = lgc_index;
402 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
403 rc = llog_cancel_rec(env, handle, cookie.lgc_index);
405 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
406 GOTO(out_close, rc = -EINVAL);
409 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
410 GOTO(out_close, rc = -ENOTTY);
412 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
415 cookie.lgc_lgl = plain;
416 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
421 case OBD_IOC_LLOG_REMOVE: {
422 struct llog_logid plain;
424 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
425 rc = llog_destroy(env, handle);
427 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
428 GOTO(out_close, rc = -EINVAL);
431 if (data->ioc_inlbuf2 > 0) {
432 /* remove indicate log from the catalog */
433 rc = str2logid(&plain, data->ioc_inlbuf2,
437 rc = llog_remove_log(env, handle, &plain);
439 /* remove all the log of the catalog */
440 rc = llog_process(env, handle, llog_delete_cb, NULL,
448 CERROR("%s: Unknown ioctl cmd %#x\n",
449 ctxt->loc_obd->obd_name, cmd);
450 GOTO(out_close, rc = -ENOTTY);
454 if (handle->lgh_hdr &&
455 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
456 llog_cat_close(env, handle);
458 llog_close(env, handle);
461 EXPORT_SYMBOL(llog_ioctl);
463 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
464 int count, struct obd_ioctl_data *data,
465 const struct lu_fid *fid)
468 struct llog_catid *idarray;
469 struct llog_logid *id;
471 int l, remains, rc = 0;
475 if (count == 0) { /* get total number of logs */
476 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
482 size = sizeof(*idarray) * count;
484 OBD_ALLOC_LARGE(idarray, size);
488 rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
492 out = data->ioc_bulk;
493 remains = data->ioc_inllen1;
494 for (i = 0; i < count; i++) {
495 id = &idarray[i].lci_logid;
496 l = snprintf(out, remains, "catalog_log: "DFID":%x\n",
497 PFID(&id->lgl_oi.oi_fid), id->lgl_ogen);
504 OBD_FREE_LARGE(idarray, size);
507 EXPORT_SYMBOL(llog_catalog_list);