1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5 * Author: Andreas Dilger <adilger@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * remote api for llog - server side
26 #define DEBUG_SUBSYSTEM S_LOG
33 #include <liblustre.h>
38 #include <linux/obd_class.h>
39 #include <linux/lustre_log.h>
40 #include <linux/lustre_net.h>
41 #include <libcfs/list.h>
42 #include <linux/lustre_fsfilt.h>
46 int llog_origin_handle_open(struct ptlrpc_request *req)
48 struct obd_export *exp = req->rq_export;
49 struct obd_device *obd = exp->exp_obd;
50 struct llog_handle *loghandle;
51 struct llogd_body *body;
52 struct lvfs_run_ctxt saved;
53 struct llog_logid *logid = NULL;
54 struct llog_ctxt *ctxt;
56 int rc, rc2, size = sizeof (*body);
59 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
60 lustre_swab_llogd_body);
62 CERROR ("Can't unpack llogd_body\n");
63 GOTO(out, rc =-EFAULT);
66 if (body->lgd_logid.lgl_oid > 0)
67 logid = &body->lgd_logid;
69 if (req->rq_reqmsg->bufcount > 1) {
70 name = lustre_msg_string(req->rq_reqmsg, 1, 0);
72 CERROR("Can't unpack name\n");
73 GOTO(out, rc = -EFAULT);
77 ctxt = llog_get_context(&obd->obd_llogs, body->lgd_ctxt_idx);
79 GOTO(out, rc = -EINVAL);
81 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
83 rc = llog_open(ctxt, &loghandle, logid, name, body->lgd_llh_flags);
87 rc = lustre_pack_reply(req, 1, &size, NULL);
89 GOTO(out_close, rc = -ENOMEM);
91 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
92 body->lgd_logid = loghandle->lgh_id;
95 rc2 = llog_close(loghandle);
99 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
104 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
106 struct obd_export *exp = req->rq_export;
107 struct obd_device *obd = exp->exp_obd;
108 struct llog_handle *loghandle;
109 struct llogd_body *body;
110 struct lvfs_run_ctxt saved;
111 struct llog_ctxt *ctxt;
115 int rc, rc2, size[] = {sizeof (*body), LLOG_CHUNK_SIZE};
118 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
119 lustre_swab_llogd_body);
121 CERROR ("Can't unpack llogd_body\n");
122 GOTO(out, rc =-EFAULT);
125 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
127 GOTO(out, rc = -ENOMEM);
129 ctxt = llog_get_context(&obd->obd_llogs, body->lgd_ctxt_idx);
131 GOTO(out, rc = -EINVAL);
132 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
134 rc = llog_open(ctxt, &loghandle, &body->lgd_logid, NULL, 0);
138 flags = body->lgd_llh_flags;
139 rc = llog_init_handle(loghandle, flags, NULL);
143 memset(buf, 0, LLOG_CHUNK_SIZE);
144 rc = llog_prev_block(loghandle, body->lgd_index, buf, LLOG_CHUNK_SIZE);
149 rc = lustre_pack_reply(req, 2, size, NULL);
151 GOTO(out_close, rc = -ENOMEM);
153 ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
154 memcpy(ptr, body, sizeof(*body));
156 ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
157 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
160 rc2 = llog_close(loghandle);
165 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
166 OBD_FREE(buf, LLOG_CHUNK_SIZE);
171 int llog_origin_handle_next_block(struct ptlrpc_request *req)
173 struct obd_export *exp = req->rq_export;
174 struct obd_device *obd = exp->exp_obd;
175 struct llog_handle *loghandle;
176 struct llogd_body *body;
177 struct lvfs_run_ctxt saved;
178 struct llog_ctxt *ctxt;
182 int size[] = {sizeof (*body),
187 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
188 lustre_swab_llogd_body);
190 CERROR ("Can't unpack llogd_body\n");
191 GOTO(out, rc =-EFAULT);
194 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
196 GOTO(out, rc = -ENOMEM);
198 ctxt = llog_get_context(&obd->obd_llogs, body->lgd_ctxt_idx);
200 GOTO(out, rc = -EINVAL);
201 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
203 rc = llog_open(ctxt, &loghandle, &body->lgd_logid, NULL, 0);
207 flags = body->lgd_llh_flags;
208 rc = llog_init_handle(loghandle, flags, NULL);
212 memset(buf, 0, LLOG_CHUNK_SIZE);
213 rc = llog_next_block(loghandle, (int *)&body->lgd_saved_index,
214 body->lgd_index, &body->lgd_cur_offset,
215 buf, LLOG_CHUNK_SIZE);
220 rc = lustre_pack_reply(req, 2, size, NULL);
222 GOTO(out_close, rc = -ENOMEM);
224 ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
225 memcpy(ptr, body, sizeof(*body));
227 ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
228 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
231 rc2 = llog_close(loghandle);
236 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
237 OBD_FREE(buf, LLOG_CHUNK_SIZE);
242 int llog_origin_handle_read_header(struct ptlrpc_request *req)
244 struct obd_export *exp = req->rq_export;
245 struct obd_device *obd = exp->exp_obd;
246 struct llog_handle *loghandle;
247 struct llogd_body *body;
248 struct llog_log_hdr *hdr;
249 struct lvfs_run_ctxt saved;
250 struct llog_ctxt *ctxt;
252 int size[] = {sizeof (*hdr)};
256 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
257 lustre_swab_llogd_body);
259 CERROR ("Can't unpack llogd_body\n");
260 GOTO(out, rc =-EFAULT);
263 ctxt = llog_get_context(&obd->obd_llogs, body->lgd_ctxt_idx);
265 GOTO(out, rc = -EINVAL);
266 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
268 rc = llog_open(ctxt, &loghandle, &body->lgd_logid, NULL, 0);
272 /* init_handle reads the header */
273 flags = body->lgd_llh_flags;
274 rc = llog_init_handle(loghandle, flags, NULL);
278 rc = lustre_pack_reply(req, 1, size, NULL);
280 GOTO(out_close, rc = -ENOMEM);
282 hdr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*hdr));
283 memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
286 rc2 = llog_close(loghandle);
291 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
297 int llog_origin_handle_close(struct ptlrpc_request *req)
306 int llog_origin_handle_cancel(struct ptlrpc_request *req)
308 struct obd_device *obd = req->rq_export->exp_obd;
309 struct llog_cookie *logcookies;
310 struct llog_ctxt *ctxt;
311 int num_cookies, rc = 0, err, i;
312 struct lvfs_run_ctxt saved;
313 struct llog_handle *cathandle;
318 logcookies = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*logcookies));
319 num_cookies = req->rq_reqmsg->buflens[0]/sizeof(*logcookies);
320 if (logcookies == NULL || num_cookies == 0) {
321 DEBUG_REQ(D_HA, req, "no cookies sent");
325 ctxt = llog_get_context(&obd->obd_llogs, logcookies->lgc_subsys);
327 CWARN("llog subsys not setup or already cleanup\n");
331 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
332 for (i = 0; i < num_cookies; i++, logcookies++) {
333 cathandle = ctxt->loc_handle;
334 LASSERT(cathandle != NULL);
335 inode = cathandle->lgh_file->f_dentry->d_inode;
337 handle = llog_fsfilt_start(ctxt, inode,
338 FSFILT_OP_CANCEL_UNLINK, NULL);
339 if (IS_ERR(handle)) {
340 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
341 GOTO(pop_ctxt, rc = PTR_ERR(handle));
344 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
346 err = llog_fsfilt_commit(ctxt, inode, handle, 0);
348 CERROR("error committing transaction: %d\n", err);
355 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
357 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
359 CDEBUG(D_HA, "cancel %d llog-records\n", num_cookies);
363 EXPORT_SYMBOL(llog_origin_handle_cancel);
365 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
368 struct mds_obd *mds = &obd->u.mds;
369 struct llog_ctxt *ctxt;
370 struct lvfs_run_ctxt saved;
371 struct llog_handle *handle = NULL;
373 int rc, i, l, remains = buf_len;
376 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
377 if (ctxt == NULL || mds == NULL)
380 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
382 sprintf(name[0], "%s", mds->mds_profile);
383 sprintf(name[1], "%s-clean", mds->mds_profile);
384 sprintf(name[2], "%s", client);
385 sprintf(name[3], "%s-clean", client);
387 for (i = 0; i < 4; i++) {
388 int index, uncanceled = 0;
389 rc = llog_open(ctxt, &handle, NULL, name[i], 0);
392 rc = llog_init_handle(handle, 0, NULL);
395 GOTO(out_pop, rc = -ENOENT);
398 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
399 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
403 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
404 "Last Index: %d\nUncanceled Records: %d\n\n",
406 handle->lgh_file->f_dentry->d_inode->i_size,
407 handle->lgh_last_idx, uncanceled);
416 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
421 struct llog_ctxt *ctxt;
427 static int llog_catinfo_cb(struct llog_handle *cat,
428 struct llog_rec_hdr *rec, void *data)
430 static char *out = NULL;
431 static int remains = 0;
432 struct llog_ctxt *ctxt;
433 struct llog_handle *handle;
434 struct llog_logid *logid;
435 struct llog_logid_rec *lir;
436 int l, rc, index, count = 0;
437 struct cb_data *cbd = (struct cb_data*)data;
441 remains = cbd->remains;
446 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
449 lir = (struct llog_logid_rec *)rec;
450 logid = &lir->lid_id;
451 rc = llog_open(ctxt, &handle, logid, NULL, 0);
454 rc = llog_init_handle(handle, 0, NULL);
458 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
459 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
463 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
464 "\tLog Size: %llu\n\tLast Index: %d\n"
465 "\tUncanceled Records: %d\n",
466 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
467 handle->lgh_file->f_dentry->d_inode->i_size,
468 handle->lgh_last_idx, count);
472 cbd->remains = remains;
474 CWARN("Not enough memory\n");
483 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
486 struct mds_obd *mds = &obd->u.mds;
487 struct llog_handle *handle;
488 struct lvfs_run_ctxt saved;
490 struct llog_catid *idarray;
491 struct llog_logid *id;
492 char name[32] = CATLIST;
495 struct llog_ctxt *ctxt = llog_get_context(&obd->obd_llogs,
496 LLOG_CONFIG_ORIG_CTXT);
498 if (ctxt == NULL || mds == NULL)
501 count = mds->mds_dt_desc.ld_tgt_count;
502 size = sizeof(*idarray) * count;
504 OBD_ALLOC(idarray, size);
508 rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
509 name, count, idarray);
513 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
517 data.remains = buf_len;
518 for (i = 0; i < count; i++) {
519 int l, index, uncanceled = 0;
521 id = &idarray[i].lci_logid;
522 rc = llog_open(ctxt, &handle, id, NULL, 0);
525 rc = llog_init_handle(handle, 0, NULL);
528 GOTO(out_pop, rc = -ENOENT);
530 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
531 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
534 l = snprintf(data.out, data.remains,
535 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
537 id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
544 llog_process(handle, llog_catinfo_cb, &data, NULL);
547 if (data.remains <= 0)
551 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
553 OBD_FREE(idarray, size);
557 int llog_catinfo(struct ptlrpc_request *req)
559 struct obd_export *exp = req->rq_export;
560 struct obd_device *obd = exp->exp_obd;
563 int rc, buf_len = LLOG_CHUNK_SIZE;
565 OBD_ALLOC(buf, buf_len);
568 memset(buf, 0, buf_len);
570 keyword = lustre_msg_string(req->rq_reqmsg, 0, 0);
572 if (strcmp(keyword, "config") == 0) {
573 char *client = lustre_msg_string(req->rq_reqmsg, 1, 0);
574 rc = llog_catinfo_config(obd, buf, buf_len, client);
575 } else if (strcmp(keyword, "deletions") == 0) {
576 rc = llog_catinfo_deletions(obd, buf, buf_len);
581 rc = lustre_pack_reply(req, 1, &buf_len, NULL);
583 GOTO(out_free, rc = -ENOMEM);
585 reply = lustre_msg_buf(req->rq_repmsg, 0, buf_len);
586 if (strlen(buf) == 0)
587 sprintf(buf, "%s", "No log informations\n");
588 memcpy(reply, buf, buf_len);
591 OBD_FREE(buf, buf_len);
595 #else /* !__KERNEL__ */
596 int llog_origin_handle_open(struct ptlrpc_request *req)
601 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
606 int llog_origin_handle_next_block(struct ptlrpc_request *req)
611 int llog_origin_handle_read_header(struct ptlrpc_request *req)
616 int llog_origin_handle_close(struct ptlrpc_request *req)
621 int llog_origin_handle_cancel(struct ptlrpc_request *req)