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>
20 static int str2logid(struct llog_logid *logid, char *str, int len)
22 char *start, *end, *endp;
29 if (start - str >= len - 1)
31 end = strchr(start, '#');
32 if (end == NULL || end == start)
36 logid->lgl_oid = simple_strtoull(start, &endp, 0);
41 if (start - str >= len - 1)
43 end = strchr(start, '#');
44 if (end == NULL || end == start)
48 logid->lgl_ogr = simple_strtoull(start, &endp, 0);
53 if (start - str >= len - 1)
55 logid->lgl_ogen = simple_strtoul(start, &endp, 16);
62 static int llog_check_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
65 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
66 static int l, remains, from, to;
69 int cur_index, rc = 0;
71 cur_index = rec->lrh_index;
73 if (ioc_data && (ioc_data->ioc_inllen1)) {
75 remains = ioc_data->ioc_inllen4 +
76 size_round(ioc_data->ioc_inllen1) +
77 size_round(ioc_data->ioc_inllen2) +
78 size_round(ioc_data->ioc_inllen3);
79 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
82 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
85 ioc_data->ioc_inllen1 = 0;
86 out = ioc_data->ioc_bulk;
89 if (to > 0 && cur_index > to)
92 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
93 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
94 struct llog_handle *log_handle;
96 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
97 l = snprintf(out, remains, "[index]: %05d [type]: "
98 "%02x [len]: %04d failed\n",
99 cur_index, rec->lrh_type,
102 if (handle->lgh_ctxt == NULL)
104 llog_cat_id2handle(handle, &log_handle, &lir->lid_id);
105 rc = llog_process(log_handle, llog_check_cb, NULL, NULL);
106 llog_close(log_handle);
108 switch (rec->lrh_type) {
114 case LLOG_HDR_MAGIC: {
115 l = snprintf(out, remains, "[index]: %05d [type]: "
116 "%02x [len]: %04d ok\n",
117 cur_index, rec->lrh_type,
122 CERROR("no space to print log records\n");
123 RETURN(-LLOG_EEMPTY);
128 l = snprintf(out, remains, "[index]: %05d [type]: "
129 "%02x [len]: %04d failed\n",
130 cur_index, rec->lrh_type,
135 CERROR("no space to print log records\n");
136 RETURN(-LLOG_EEMPTY);
145 static int llog_print_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
148 struct obd_ioctl_data *ioc_data = (struct obd_ioctl_data *)data;
149 static int l, remains, from, to;
154 if (ioc_data->ioc_inllen1) {
156 remains = ioc_data->ioc_inllen4 +
157 size_round(ioc_data->ioc_inllen1) +
158 size_round(ioc_data->ioc_inllen2) +
159 size_round(ioc_data->ioc_inllen3);
160 from = simple_strtol(ioc_data->ioc_inlbuf2, &endp, 0);
163 to = simple_strtol(ioc_data->ioc_inlbuf3, &endp, 0);
166 out = ioc_data->ioc_bulk;
167 ioc_data->ioc_inllen1 = 0;
170 cur_index = rec->lrh_index;
171 if (cur_index < from)
173 if (to > 0 && cur_index > to)
174 RETURN(-LLOG_EEMPTY);
176 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
177 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
178 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
179 CERROR("invalid record in catalog\n");
183 l = snprintf(out, remains,
184 "[index]: %05d [logid]: #"LPX64"#"LPX64"#%08x\n",
185 cur_index, lir->lid_id.lgl_oid,
186 lir->lid_id.lgl_ogr, lir->lid_id.lgl_ogen);
188 l = snprintf(out, remains,
189 "[index]: %05d [type]: %02x [len]: %04d\n",
190 cur_index, rec->lrh_type,
196 CERROR("not enough space for print log records\n");
197 RETURN(-LLOG_EEMPTY);
202 static int llog_remove_log(struct llog_handle *cat, struct llog_logid *logid)
204 struct llog_handle *log;
207 down_write(&cat->lgh_lock);
208 rc = llog_cat_id2handle(cat, &log, logid);
210 CDEBUG(D_IOCTL, "cannot find log #"LPX64"#"LPX64"#%08x\n",
211 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen);
212 GOTO(out, rc = -ENOENT);
215 index = log->u.phd.phd_cookie.lgc_index;
217 rc = llog_destroy(log);
219 CDEBUG(D_IOCTL, "cannot destroy log\n");
222 llog_cat_set_first_idx(cat, index);
223 rc = llog_cancel_rec(cat, index);
225 llog_free_handle(log);
226 up_write(&cat->lgh_lock);
231 static int llog_delete_cb(struct llog_handle *handle, struct llog_rec_hdr *rec,
234 struct llog_logid_rec *lir = (struct llog_logid_rec*)rec;
237 if (rec->lrh_type != LLOG_LOGID_MAGIC)
239 rc = llog_remove_log(handle, &lir->lid_id);
245 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data)
247 struct llog_logid logid;
249 struct llog_handle *handle = NULL;
251 if (*data->ioc_inlbuf1 == '#') {
252 err = str2logid(&logid, data->ioc_inlbuf1, data->ioc_inllen1);
255 err = llog_open(ctxt, &handle, &logid, NULL, 0);
258 } else if (*data->ioc_inlbuf1 == '$') {
259 char *name = data->ioc_inlbuf1 + 1;
260 err = llog_open(ctxt, &handle, NULL, name, 0);
264 GOTO(out, err = -EINVAL);
267 err = llog_init_handle(handle, 0, NULL);
269 GOTO(out_close, err = -ENOENT);
272 case OBD_IOC_LLOG_INFO: {
274 int remains = data->ioc_inllen2 +
275 size_round(data->ioc_inllen1);
276 char *out = data->ioc_bulk;
278 l = snprintf(out, remains,
279 "logid: #"LPX64"#"LPX64"#%08x\n"
281 "records count: %d\n"
283 handle->lgh_id.lgl_oid, handle->lgh_id.lgl_ogr,
284 handle->lgh_id.lgl_ogen,
285 handle->lgh_hdr->llh_flags,
286 handle->lgh_hdr->llh_flags &
287 LLOG_F_IS_CAT ? "cat" : "plain",
288 handle->lgh_hdr->llh_count,
289 handle->lgh_last_idx);
293 CERROR("not enough space for log header info\n");
295 GOTO(out_close, err);
297 case OBD_IOC_LLOG_CHECK: {
298 LASSERT(data->ioc_inllen1);
299 err = llog_process(handle, llog_check_cb, data, NULL);
300 if (err == -LLOG_EEMPTY)
302 GOTO(out_close, err);
305 case OBD_IOC_LLOG_PRINT: {
306 LASSERT(data->ioc_inllen1);
307 err = llog_process(handle, llog_print_cb, data, NULL);
308 if (err == -LLOG_EEMPTY)
311 GOTO(out_close, err);
313 case OBD_IOC_LLOG_CANCEL: {
314 struct llog_cookie cookie;
315 struct llog_logid plain;
318 cookie.lgc_index = simple_strtoul(data->ioc_inlbuf3, &endp, 0);
320 GOTO(out_close, err = -EINVAL);
322 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT) {
323 down_write(&handle->lgh_lock);
324 err = llog_cancel_rec(handle, cookie.lgc_index);
325 up_write(&handle->lgh_lock);
326 GOTO(out_close, err);
329 err = str2logid(&plain, data->ioc_inlbuf2, data->ioc_inllen2);
331 GOTO(out_close, err);
332 cookie.lgc_lgl = plain;
334 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
335 GOTO(out_close, err = -EINVAL);
337 err = llog_cat_cancel_records(handle, 1, &cookie);
338 GOTO(out_close, err);
340 case OBD_IOC_LLOG_REMOVE: {
341 struct llog_logid plain;
343 if (handle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN) {
344 err = llog_destroy(handle);
346 llog_free_handle(handle);
350 if (!(handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
351 GOTO(out_close, err = -EINVAL);
353 if (data->ioc_inlbuf2) {
354 /*remove indicate log from the catalog*/
355 err = str2logid(&plain, data->ioc_inlbuf2,
358 GOTO(out_close, err);
359 err = llog_remove_log(handle, &plain);
361 /*remove all the log of the catalog*/
362 llog_process(handle, llog_delete_cb, NULL, NULL);
364 GOTO(out_close, err);
369 if (handle->lgh_hdr &&
370 handle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
371 llog_cat_put(handle);
377 EXPORT_SYMBOL(llog_ioctl);
379 int llog_catalog_list(struct obd_device *obd, int count,
380 struct obd_ioctl_data *data)
383 struct llog_catid *idarray;
384 struct llog_logid *id;
385 char name[32] = CATLIST;
387 int l, remains, rc = 0;
389 size = sizeof(*idarray) * count;
391 OBD_ALLOC(idarray, size);
394 memset(idarray, 0, size);
396 rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
397 name, count, idarray);
399 OBD_FREE(idarray, size);
403 out = data->ioc_bulk;
404 remains = data->ioc_inllen1;
405 for (i = 0; i < count; i++) {
406 id = &idarray[i].lci_logid;
407 l = snprintf(out, remains,
408 "catalog log: #"LPX64"#"LPX64"#%08x\n",
409 id->lgl_oid, id->lgl_ogr, id->lgl_ogen);
413 CWARN("not enough memory for catlog list\n");
417 OBD_FREE(idarray, size);
421 EXPORT_SYMBOL(llog_catalog_list);