1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * You should have received a copy of the GNU General Public License
5 * along with Lustre; if not, write to the Free Software
6 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
9 #define DEBUG_SUBSYSTEM S_LOG
16 #include <linux/obd_class.h>
17 #include <linux/lustre_log.h>
18 #include <portals/list.h>
19 #include "llog_internal.h"
21 static int str2logid(struct llog_logid *logid, char *str, int len)
23 char *start, *end, *endp;
30 if (start - str >= len - 1)
32 end = strchr(start, '#');
33 if (end == NULL || end == start)
37 logid->lgl_oid = simple_strtoull(start, &endp, 16);
42 if (start - str >= len - 1)
44 end = strchr(start, '#');
45 if (end == NULL || end == start)
49 logid->lgl_ogr = simple_strtoull(start, &endp, 16);
54 if (start - str >= len - 1)
56 logid->lgl_ogen = simple_strtoul(start, &endp, 16);
63 static int llog_check_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
66 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
67 static int l, remains, from, to;
70 int cur_index, rc = 0;
72 cur_index = le32_to_cpu(rec->lrh_index);
74 if (ioc_data && (ioc_data->ioc_inllen1)) {
76 remains = ioc_data->ioc_inllen4 +
77 size_round(ioc_data->ioc_inllen1) +
78 size_round(ioc_data->ioc_inllen2) +
79 size_round(ioc_data->ioc_inllen3);
80 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
83 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
86 ioc_data->ioc_inllen1 = 0;
87 out = ioc_data->ioc_bulk;
90 if (to > 0 && cur_index > to)
93 if (handle->lgh_hdr->llh_flags & cpu_to_le32(LLOG_F_IS_CAT)) {
94 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
95 struct llog_handle *log_handle;
97 if (rec->lrh_type != cpu_to_le32(LLOG_LOGID_MAGIC)) {
98 l = snprintf(out, remains,
99 "[index]: %05d [type]: %02x [len]: %04d failed\n",
100 cur_index, le32_to_cpu(rec->lrh_type),
101 le32_to_cpu(rec->lrh_len));
103 if (handle->lgh_ctxt == NULL)
105 llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
106 rc = llog_process(log_handle, llog_check_cb, NULL);
107 llog_close(log_handle);
109 switch (le32_to_cpu(rec->lrh_type)) {
115 case LLOG_HDR_MAGIC: {
116 l = snprintf(out, remains,
117 "[index]: %05d [type]: %02x [len]: %04d ok\n",
118 cur_index, le32_to_cpu(rec->lrh_type),
119 le32_to_cpu(rec->lrh_len));
123 CERROR("not enough space for print log records\n");
124 RETURN(-LLOG_EEMPTY);
129 l = snprintf(out, remains,
130 "[index]: %05d [type]: %02x [len]: %04d failed\n",
131 cur_index, le32_to_cpu(rec->lrh_type),
132 le32_to_cpu(rec->lrh_len));
136 CERROR("not enough space for print log records\n");
137 RETURN(-LLOG_EEMPTY);
146 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
149 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
150 static int l, remains, from, to;
155 if (ioc_data->ioc_inllen1) {
157 remains = ioc_data->ioc_inllen4 +
158 size_round(ioc_data->ioc_inllen1) +
159 size_round(ioc_data->ioc_inllen2) +
160 size_round(ioc_data->ioc_inllen3);
161 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
164 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
167 out = ioc_data->ioc_bulk;
168 ioc_data->ioc_inllen1 = 0;
171 cur_index = le32_to_cpu(rec->lrh_index);
172 if (cur_index < from)
174 if (to > 0 && cur_index > to)
175 RETURN(-LLOG_EEMPTY);
177 if (handle->lgh_hdr->llh_flags & cpu_to_le32(LLOG_F_IS_CAT)) {
178 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
179 if (rec->lrh_type != cpu_to_le32(LLOG_LOGID_MAGIC)) {
180 CERROR("invalid record in catalog\n");
184 l = snprintf(out, remains,
185 "[index]: %05d [logid]: #%llx#%llx#%08x\n",
186 cur_index, lir->lid_id.lgl_oid,
187 lir->lid_id.lgl_ogr, lir->lid_id.lgl_ogen);
189 l = snprintf(out, remains,
190 "[index]: %05d [type]: %02x [len]: %04d\n",
191 cur_index, le32_to_cpu(rec->lrh_type),
192 le32_to_cpu(rec->lrh_len));
197 CERROR("not enough space for print log records\n");
198 RETURN(-LLOG_EEMPTY);
203 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
205 struct llog_handle *log;
208 down_write(&cat->lgh_lock);
209 rc = llog_cat_id2handle(cat, &log, logid);
211 CDEBUG(D_IOCTL, "cannot find log #%0llx#%0llx#%08x\n",
212 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen);
213 GOTO(out, rc = -ENOENT);
216 index = log->u.phd.phd_cookie.lgc_index;
218 rc = llog_destroy(log);
220 CDEBUG(D_IOCTL, "cannot destroy log\n");
223 rc = llog_cancel_rec(cat, index);
225 llog_free_handle(log);
226 up_write(&cat->lgh_lock);
230 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
233 struct llog_logid_rec *lir = (struct llog_logid_rec*)rec;
236 if (rec->lrh_type != cpu_to_le32(LLOG_LOGID_MAGIC))
238 rc = llog_remove_log(handle, &lir->lid_id);
244 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
246 struct llog_logid logid;
248 struct llog_handle *handle = NULL;
250 if (*data->ioc_inlbuf1 == '#') {
251 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
254 err = llog_create(ctxt, &handle, &logid, NULL);
257 } else if (*data->ioc_inlbuf1 == '$') {
258 char *name = data->ioc_inlbuf1 + 1;
259 err = llog_create(ctxt, &handle, NULL, name);
263 GOTO(out, err = -EINVAL);
266 err = llog_init_handle(handle, 0, NULL);
268 GOTO(out_close, err = -ENOENT);
271 case OBD_IOC_LLOG_INFO: {
273 int remains = data->ioc_inllen2 +
274 size_round(data->ioc_inllen1);
275 char *out = data->ioc_bulk;
277 l = snprintf(out, remains,
278 "logid: #%llx#%llx#%08x\n"
280 "records count: %d\n"
282 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_ogr,
283 handle->lgh_id.lgl_ogen,
284 le32_to_cpu(handle->lgh_hdr->llh_flags),
285 le32_to_cpu(handle->lgh_hdr->llh_flags) &
286 LLOG_F_IS_CAT ? "cat" : "plain",
287 le32_to_cpu(handle->lgh_hdr->llh_count),
288 handle->lgh_last_idx);
292 CERROR("not enough space for log header info\n");
294 GOTO(out_close, err);
296 case OBD_IOC_LLOG_CHECK: {
297 LASSERT(data->ioc_inllen1);
298 err = llog_process(handle, llog_check_cb, data);
299 if (err == -LLOG_EEMPTY)
301 GOTO(out_close, err);
304 case OBD_IOC_LLOG_PRINT: {
305 LASSERT(data->ioc_inllen1);
306 err = llog_process(handle, llog_print_cb, data);
307 if (err == -LLOG_EEMPTY)
310 GOTO(out_close, err);
312 case OBD_IOC_LLOG_CANCEL: {
313 struct llog_cookie cookie;
314 struct llog_logid plain;
317 if (!(handle->lgh_hdr->llh_flags & cpu_to_le32(LLOG_F_IS_CAT)))
318 GOTO(out_close, err = -EINVAL);
320 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
322 GOTO(out_close, err);
323 cookie.lgc_lgl = plain;
324 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3,
327 GOTO(out_close, err = -EINVAL);
329 err = llog_cat_cancel_records(handle, 1, &cookie);
330 GOTO(out_close, err);
332 case OBD_IOC_LLOG_REMOVE: {
333 struct llog_logid plain;
335 if (!(handle->lgh_hdr->llh_flags & cpu_to_le32(LLOG_F_IS_CAT)))
336 GOTO(out_close, err = -EINVAL);
338 if (data->ioc_inlbuf2) {
339 /*remove indicate log from the catalog*/
340 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
342 GOTO(out_close, err);
343 err = llog_remove_log(handle, &plain);
345 /*remove all the log of the catalog*/
346 llog_process(handle, llog_delete_cb, NULL);
348 GOTO(out_close, err);
353 if (handle->lgh_hdr &&
354 handle->lgh_hdr->llh_flags & cpu_to_le32(LLOG_F_IS_CAT))
355 llog_cat_put(handle);
361 EXPORT_SYMBOL(llog_ioctl);
363 int llog_catlog_list(struct obd_device *obd, int count,
364 struct obd_ioctl_data *data)
367 struct llog_logid *idarray, *id;
368 char name[32] = "CATLIST";
370 int l, remains, rc = 0;
372 size = sizeof(*idarray) * count;
374 OBD_ALLOC(idarray, size);
377 memset(idarray, 0, size);
379 rc = llog_get_cat_list(obd, obd, name, count, idarray);
381 OBD_FREE(idarray, size);
385 out = data->ioc_bulk;
386 remains = data->ioc_inllen1;
388 for (i = 0; i < count; i++) {
389 l = snprintf(out, remains,
390 "catalog log: #%llx#%llx#%08x\n",
391 id->lgl_oid, id->lgl_ogr, id->lgl_ogen);
396 CWARN("not enough memory for catlog list\n");
400 OBD_FREE(idarray, size);
404 EXPORT_SYMBOL(llog_catlog_list);