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 char *start, *end, *endp;
47 if (start[0] == '[') {
48 struct lu_fid *fid = &logid->lgl_oi.oi_fid;
53 num = sscanf(start + 1, SFID, RFID(fid));
54 CDEBUG(D_INFO, DFID":%x\n", PFID(fid), logid->lgl_ogen);
55 RETURN(num == 3 && fid_is_sane(fid) ? 0 : -EINVAL);
58 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 1, 53, 0)
59 /* logids used to be input in the form "#id#seq:ogen" before they
60 * were changed over to accept the FID [seq:oid:ver] format.
61 * This is accepted for compatibility reasons, though I doubt
62 * anyone is actually using this for anything. */
67 if (start - str >= len - 1)
69 end = strchr(start, '#');
70 if (end == NULL || end == start)
74 id = simple_strtoull(start, &endp, 0);
79 if (start - str >= len - 1)
81 end = strchr(start, '#');
82 if (end == NULL || end == start)
86 seq = simple_strtoull(start, &endp, 0);
90 ostid_set_seq(&logid->lgl_oi, seq);
91 if (ostid_set_id(&logid->lgl_oi, id))
95 if (start - str >= len - 1)
97 logid->lgl_ogen = simple_strtoul(start, &endp, 16);
107 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
108 struct llog_rec_hdr *rec, void *data)
110 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
111 static int l, remains;
112 static long from, to;
115 int cur_index, rc = 0;
119 if (ioc_data && ioc_data->ioc_inllen1 > 0) {
121 remains = ioc_data->ioc_inllen4 +
122 cfs_size_round(ioc_data->ioc_inllen1) +
123 cfs_size_round(ioc_data->ioc_inllen2) +
124 cfs_size_round(ioc_data->ioc_inllen3);
125 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
128 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
131 ioc_data->ioc_inllen1 = 0;
132 out = ioc_data->ioc_bulk;
135 cur_index = rec->lrh_index;
136 if (cur_index < from)
138 if (to > 0 && cur_index > to)
139 RETURN(-LLOG_EEMPTY);
141 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
142 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
143 struct llog_handle *loghandle;
145 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
146 l = snprintf(out, remains, "[index]: %05d [type]: "
147 "%02x [len]: %04d failed\n",
148 cur_index, rec->lrh_type,
151 if (handle->lgh_ctxt == NULL)
153 rc = llog_cat_id2handle(env, handle, &loghandle, &lir->lid_id);
155 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
156 PFID(&lir->lid_id.lgl_oi.oi_fid),
157 lir->lid_id.lgl_ogen);
160 rc = llog_process(env, loghandle, llog_check_cb, NULL, NULL);
161 llog_handle_put(loghandle);
165 switch (rec->lrh_type) {
168 case MDS_UNLINK64_REC:
169 case MDS_SETATTR64_REC:
179 l = snprintf(out, remains, "[index]: %05d [type]: "
180 "%02x [len]: %04d %s\n",
181 cur_index, rec->lrh_type, rec->lrh_len,
182 ok ? "ok" : "failed");
186 CERROR("%s: no space to print log records\n",
187 handle->lgh_ctxt->loc_obd->obd_name);
188 RETURN(-LLOG_EEMPTY);
194 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
195 struct llog_rec_hdr *rec, void *data)
197 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
198 static int l, remains;
199 static long from, to;
205 if (ioc_data != NULL && ioc_data->ioc_inllen1 > 0) {
207 remains = ioc_data->ioc_inllen4 +
208 cfs_size_round(ioc_data->ioc_inllen1) +
209 cfs_size_round(ioc_data->ioc_inllen2) +
210 cfs_size_round(ioc_data->ioc_inllen3);
211 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
214 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
217 out = ioc_data->ioc_bulk;
218 ioc_data->ioc_inllen1 = 0;
221 cur_index = rec->lrh_index;
222 if (cur_index < from)
224 if (to > 0 && cur_index > to)
225 RETURN(-LLOG_EEMPTY);
227 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
228 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
230 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
231 CERROR("invalid record in catalog\n");
235 l = snprintf(out, remains,
236 "[index]: %05d [logid]: "DFID":%x\n",
237 cur_index, PFID(&lir->lid_id.lgl_oi.oi_fid),
238 lir->lid_id.lgl_ogen);
239 } else if (rec->lrh_type == OBD_CFG_REC) {
242 rc = class_config_yaml_output(rec, out, remains);
247 l = snprintf(out, remains,
248 "[index]: %05d [type]: %02x [len]: %04d\n",
249 cur_index, rec->lrh_type, rec->lrh_len);
254 CERROR("not enough space for print log records\n");
255 RETURN(-LLOG_EEMPTY);
260 static int llog_remove_log(const struct lu_env *env, struct llog_handle *cat,
261 struct llog_logid *logid)
263 struct llog_handle *log;
268 rc = llog_cat_id2handle(env, cat, &log, logid);
270 CDEBUG(D_IOCTL, "cannot find log "DFID":%x\n",
271 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
275 rc = llog_destroy(env, log);
277 CDEBUG(D_IOCTL, "cannot destroy log "DFID":%x\n",
278 PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen);
281 llog_cat_cleanup(env, cat, log, log->u.phd.phd_cookie.lgc_index);
283 llog_handle_put(log);
288 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
289 struct llog_rec_hdr *rec, void *data)
291 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
295 if (rec->lrh_type != LLOG_LOGID_MAGIC)
297 rc = llog_remove_log(env, handle, &lir->lid_id);
303 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
304 struct obd_ioctl_data *data)
306 struct llog_logid logid;
308 struct llog_handle *handle = NULL;
313 logname = data->ioc_inlbuf1;
314 if (logname[0] == '#' || logname[0] == '[') {
315 rc = str2logid(&logid, logname, data->ioc_inllen1);
318 rc = llog_open(env, ctxt, &handle, &logid, NULL,
322 } else if (logname[0] == '$' || isalpha(logname[0])) {
323 if (logname[0] == '$')
326 rc = llog_open(env, ctxt, &handle, NULL, logname,
334 rc = llog_init_handle(env, handle, 0, NULL);
336 GOTO(out_close, rc = -ENOENT);
339 case OBD_IOC_LLOG_INFO: {
341 int remains = data->ioc_inllen2 +
342 cfs_size_round(data->ioc_inllen1);
343 char *out = data->ioc_bulk;
345 l = snprintf(out, remains,
348 "records_count: %d\n"
350 PFID(&handle->lgh_id.lgl_oi.oi_fid),
351 handle->lgh_id.lgl_ogen,
352 handle->lgh_hdr->llh_flags,
353 handle->lgh_hdr->llh_flags &
354 LLOG_F_IS_CAT ? "cat" : "plain",
355 handle->lgh_hdr->llh_count,
356 handle->lgh_last_idx);
360 CERROR("%s: not enough space for log header info\n",
361 ctxt->loc_obd->obd_name);
366 case OBD_IOC_LLOG_CHECK:
367 LASSERT(data->ioc_inllen1 > 0);
368 rc = llog_process(env, handle, llog_check_cb, data, NULL);
369 if (rc == -LLOG_EEMPTY)
374 case OBD_IOC_LLOG_PRINT:
375 LASSERT(data->ioc_inllen1 > 0);
376 rc = llog_process(env, handle, llog_print_cb, data, NULL);
377 if (rc == -LLOG_EEMPTY)
382 case OBD_IOC_LLOG_CANCEL: {
383 struct llog_cookie cookie;
384 struct llog_logid plain;
387 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
389 GOTO(out_close, rc = -EINVAL);
391 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
392 rc = llog_cancel_rec(env, handle, cookie.lgc_index);
394 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
395 GOTO(out_close, rc = -EINVAL);
398 if (data->ioc_inlbuf2 == NULL) /* catalog but no logid */
399 GOTO(out_close, rc = -ENOTTY);
401 rc = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
404 cookie.lgc_lgl = plain;
405 rc = llog_cat_cancel_records(env, handle, 1, &cookie);
410 case OBD_IOC_LLOG_REMOVE: {
411 struct llog_logid plain;
413 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
414 rc = llog_destroy(env, handle);
416 } else if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)) {
417 GOTO(out_close, rc = -EINVAL);
420 if (data->ioc_inlbuf2 > 0) {
421 /* remove indicate log from the catalog */
422 rc = str2logid(&plain, data->ioc_inlbuf2,
426 rc = llog_remove_log(env, handle, &plain);
428 /* remove all the log of the catalog */
429 rc = llog_process(env, handle, llog_delete_cb, NULL,
437 CERROR("%s: Unknown ioctl cmd %#x\n",
438 ctxt->loc_obd->obd_name, cmd);
439 GOTO(out_close, rc = -ENOTTY);
443 if (handle->lgh_hdr &&
444 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
445 llog_cat_close(env, handle);
447 llog_close(env, handle);
450 EXPORT_SYMBOL(llog_ioctl);
452 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
453 int count, struct obd_ioctl_data *data,
454 const struct lu_fid *fid)
457 struct llog_catid *idarray;
458 struct llog_logid *id;
460 int l, remains, rc = 0;
464 if (count == 0) { /* get total number of logs */
465 rc = llog_osd_get_cat_list(env, d, 0, 0, NULL, fid);
471 size = sizeof(*idarray) * count;
473 OBD_ALLOC_LARGE(idarray, size);
477 rc = llog_osd_get_cat_list(env, d, 0, count, idarray, fid);
481 out = data->ioc_bulk;
482 remains = data->ioc_inllen1;
483 for (i = 0; i < count; i++) {
484 id = &idarray[i].lci_logid;
485 l = snprintf(out, remains, "catalog_log: "DFID":%x\n",
486 PFID(&id->lgl_oi.oi_fid), id->lgl_ogen);
493 OBD_FREE_LARGE(idarray, size);
496 EXPORT_SYMBOL(llog_catalog_list);