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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LOG
39 #include <obd_class.h>
40 #include <lustre_log.h>
41 #include <libcfs/list.h>
42 #include "llog_internal.h"
44 static int str2logid(struct llog_logid *logid, char *str, int len)
46 char *start, *end, *endp;
54 if (start - str >= len - 1)
56 end = strchr(start, '#');
57 if (end == NULL || end == start)
61 logid->lgl_oid = simple_strtoull(start, &endp, 0);
66 if (start - str >= len - 1)
68 end = strchr(start, '#');
69 if (end == NULL || end == start)
73 logid->lgl_oseq = simple_strtoull(start, &endp, 0);
78 if (start - str >= len - 1)
80 logid->lgl_ogen = simple_strtoul(start, &endp, 16);
87 static int llog_check_cb(const struct lu_env *env, struct llog_handle *handle,
88 struct llog_rec_hdr *rec, void *data)
90 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
91 static int l, remains, from, to;
94 int cur_index, rc = 0;
97 cur_index = rec->lrh_index;
99 if (ioc_data && (ioc_data->ioc_inllen1)) {
101 remains = ioc_data->ioc_inllen4 +
102 cfs_size_round(ioc_data->ioc_inllen1) +
103 cfs_size_round(ioc_data->ioc_inllen2) +
104 cfs_size_round(ioc_data->ioc_inllen3);
105 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
108 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
111 ioc_data->ioc_inllen1 = 0;
112 out = ioc_data->ioc_bulk;
113 if (cur_index < from)
115 if (to > 0 && cur_index > to)
116 RETURN(-LLOG_EEMPTY);
118 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
119 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
120 struct llog_handle *log_handle;
122 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
123 l = snprintf(out, remains, "[index]: %05d [type]: "
124 "%02x [len]: %04d failed\n",
125 cur_index, rec->lrh_type,
128 if (handle->lgh_ctxt == NULL)
130 rc = llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
133 "cannot find log #"LPX64"#"LPX64"#%08x\n",
134 lir->lid_id.lgl_oid, lir->lid_id.lgl_oseq,
135 lir->lid_id.lgl_ogen);
138 rc = llog_process(env, log_handle, llog_check_cb, NULL, NULL);
139 llog_close(log_handle);
141 switch (rec->lrh_type) {
144 case MDS_SETATTR64_REC:
146 case LLOG_HDR_MAGIC: {
147 l = snprintf(out, remains, "[index]: %05d [type]: "
148 "%02x [len]: %04d ok\n",
149 cur_index, rec->lrh_type,
154 CERROR("no space to print log records\n");
155 RETURN(-LLOG_EEMPTY);
160 l = snprintf(out, remains, "[index]: %05d [type]: "
161 "%02x [len]: %04d failed\n",
162 cur_index, rec->lrh_type,
167 CERROR("no space to print log records\n");
168 RETURN(-LLOG_EEMPTY);
177 static int llog_print_cb(const struct lu_env *env, struct llog_handle *handle,
178 struct llog_rec_hdr *rec, void *data)
180 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
181 static int l, remains, from, to;
187 if (ioc_data->ioc_inllen1) {
189 remains = ioc_data->ioc_inllen4 +
190 cfs_size_round(ioc_data->ioc_inllen1) +
191 cfs_size_round(ioc_data->ioc_inllen2) +
192 cfs_size_round(ioc_data->ioc_inllen3);
193 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
196 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
199 out = ioc_data->ioc_bulk;
200 ioc_data->ioc_inllen1 = 0;
203 cur_index = rec->lrh_index;
204 if (cur_index < from)
206 if (to > 0 && cur_index > to)
207 RETURN(-LLOG_EEMPTY);
209 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
210 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
212 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
213 CERROR("invalid record in catalog\n");
217 l = snprintf(out, remains,
218 "[index]: %05d [logid]: #"LPX64"#"LPX64"#%08x\n",
219 cur_index, lir->lid_id.lgl_oid,
220 lir->lid_id.lgl_oseq, lir->lid_id.lgl_ogen);
222 l = snprintf(out, remains,
223 "[index]: %05d [type]: %02x [len]: %04d\n",
224 cur_index, rec->lrh_type,
230 CERROR("not enough space for print log records\n");
231 RETURN(-LLOG_EEMPTY);
236 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
238 struct llog_handle *log;
242 cfs_down_write(&cat->lgh_lock);
243 rc = llog_cat_id2handle(cat, &log, logid);
245 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
246 logid->lgl_oid, logid->lgl_oseq, logid->lgl_ogen);
247 GOTO(out, rc = -ENOENT);
250 index = log->u.phd.phd_cookie.lgc_index;
252 rc = llog_destroy(log);
254 CDEBUG(D_IOCTL, "cannot destroy log\n");
257 llog_cat_set_first_idx(cat, index);
258 rc = llog_cancel_rec(cat, index);
260 llog_free_handle(log);
261 cfs_up_write(&cat->lgh_lock);
266 static int llog_delete_cb(const struct lu_env *env, struct llog_handle *handle,
267 struct llog_rec_hdr *rec, void *data)
269 struct llog_logid_rec *lir = (struct llog_logid_rec*)rec;
273 if (rec->lrh_type != LLOG_LOGID_MAGIC)
275 rc = llog_remove_log(handle, &lir->lid_id);
281 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
283 struct llog_logid logid;
285 struct llog_handle *handle = NULL;
288 if (*data->ioc_inlbuf1 == '#') {
289 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
292 err = llog_create(ctxt, &handle, &logid, NULL);
295 } else if (*data->ioc_inlbuf1 == '$') {
296 char *name = data->ioc_inlbuf1 + 1;
297 err = llog_create(ctxt, &handle, NULL, name);
301 GOTO(out, err = -EINVAL);
304 err = llog_init_handle(handle, 0, NULL);
306 GOTO(out_close, err = -ENOENT);
309 case OBD_IOC_LLOG_INFO: {
311 int remains = data->ioc_inllen2 +
312 cfs_size_round(data->ioc_inllen1);
313 char *out = data->ioc_bulk;
315 l = snprintf(out, remains,
316 "logid: #"LPX64"#"LPX64"#%08x\n"
318 "records count: %d\n"
320 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_oseq,
321 handle->lgh_id.lgl_ogen,
322 handle->lgh_hdr->llh_flags,
323 handle->lgh_hdr->llh_flags &
324 LLOG_F_IS_CAT ? "cat" : "plain",
325 handle->lgh_hdr->llh_count,
326 handle->lgh_last_idx);
330 CERROR("not enough space for log header info\n");
332 GOTO(out_close, err);
334 case OBD_IOC_LLOG_CHECK: {
335 LASSERT(data->ioc_inllen1);
336 err = llog_process(NULL, handle, llog_check_cb, data, NULL);
337 if (err == -LLOG_EEMPTY)
339 GOTO(out_close, err);
342 case OBD_IOC_LLOG_PRINT: {
343 LASSERT(data->ioc_inllen1);
344 err = llog_process(NULL, handle, class_config_dump_handler,
346 if (err == -LLOG_EEMPTY)
349 err = llog_process(NULL, handle, llog_print_cb, data,
352 GOTO(out_close, err);
354 case OBD_IOC_LLOG_CANCEL: {
355 struct llog_cookie cookie;
356 struct llog_logid plain;
359 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
361 GOTO(out_close, err = -EINVAL);
363 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
364 cfs_down_write(&handle->lgh_lock);
365 err = llog_cancel_rec(handle, cookie.lgc_index);
366 cfs_up_write(&handle->lgh_lock);
367 GOTO(out_close, err);
370 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
372 GOTO(out_close, err);
373 cookie.lgc_lgl = plain;
375 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
376 GOTO(out_close, err = -EINVAL);
378 err = llog_cat_cancel_records(handle, 1, &cookie);
379 GOTO(out_close, err);
381 case OBD_IOC_LLOG_REMOVE: {
382 struct llog_logid plain;
384 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
385 err = llog_destroy(handle);
387 llog_free_handle(handle);
391 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
392 GOTO(out_close, err = -EINVAL);
394 if (data->ioc_inlbuf2) {
395 /*remove indicate log from the catalog*/
396 err = str2logid(&plain, data->ioc_inlbuf2,
399 GOTO(out_close, err);
400 err = llog_remove_log(handle, &plain);
402 /* remove all the log of the catalog */
403 llog_process(NULL, handle, llog_delete_cb, NULL, NULL);
405 GOTO(out_close, err);
410 if (handle->lgh_hdr &&
411 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
412 llog_cat_put(handle);
418 EXPORT_SYMBOL(llog_ioctl);
420 int llog_catalog_list(struct obd_device *obd, int count,
421 struct obd_ioctl_data *data)
424 struct llog_catid *idarray;
425 struct llog_logid *id;
426 char name[32] = CATLIST;
428 int l, remains, rc = 0;
431 size = sizeof(*idarray) * count;
433 OBD_ALLOC_LARGE(idarray, size);
437 cfs_mutex_lock(&obd->obd_olg.olg_cat_processing);
438 rc = llog_get_cat_list(obd, name, 0, count, idarray);
442 out = data->ioc_bulk;
443 remains = data->ioc_inllen1;
444 for (i = 0; i < count; i++) {
445 id = &idarray[i].lci_logid;
446 l = snprintf(out, remains,
447 "catalog log: #"LPX64"#"LPX64"#%08x\n",
448 id->lgl_oid, id->lgl_oseq, id->lgl_ogen);
452 CWARN("not enough memory for catlog list\n");
457 /* release semaphore */
458 cfs_mutex_unlock(&obd->obd_olg.olg_cat_processing);
460 OBD_FREE_LARGE(idarray, size);
464 EXPORT_SYMBOL(llog_catalog_list);