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 the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
25 * remote api for llog - server side
29 #define DEBUG_SUBSYSTEM S_LOG
36 #include <liblustre.h>
39 #include <obd_class.h>
40 #include <lustre_log.h>
41 #include <lustre_net.h>
42 #include <libcfs/list.h>
43 #include <lustre_fsfilt.h>
45 #if defined(__KERNEL__) && defined(LUSTRE_LOG_SERVER)
47 int llog_origin_handle_create(struct ptlrpc_request *req)
49 struct obd_export *exp = req->rq_export;
50 struct obd_device *obd = exp->exp_obd;
51 struct obd_device *disk_obd;
52 struct llog_handle *loghandle;
53 struct llogd_body *body;
54 struct lvfs_run_ctxt saved;
55 struct llog_logid *logid = NULL;
56 struct llog_ctxt *ctxt;
58 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
62 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
63 lustre_swab_llogd_body);
65 CERROR ("Can't unpack llogd_body\n");
69 if (body->lgd_logid.lgl_oid > 0)
70 logid = &body->lgd_logid;
72 if (lustre_msg_bufcount(req->rq_reqmsg) > 2) {
73 name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
75 CERROR("Can't unpack name\n");
78 CDEBUG(D_INFO, "opening log %s\n", name);
81 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
84 disk_obd = ctxt->loc_exp->exp_obd;
85 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
87 rc = llog_create(ctxt, &loghandle, logid, name);
91 rc = lustre_pack_reply(req, 2, size, NULL);
93 GOTO(out_close, rc = -ENOMEM);
95 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
96 body->lgd_logid = loghandle->lgh_id;
99 rc2 = llog_close(loghandle);
103 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
108 int llog_origin_handle_destroy(struct ptlrpc_request *req)
110 struct obd_export *exp = req->rq_export;
111 struct obd_device *obd = exp->exp_obd;
112 struct obd_device *disk_obd;
113 struct llog_handle *loghandle;
114 struct llogd_body *body;
115 struct lvfs_run_ctxt saved;
116 struct llog_logid *logid = NULL;
117 struct llog_ctxt *ctxt;
118 int size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
123 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
124 lustre_swab_llogd_body);
126 CERROR ("Can't unpack llogd_body\n");
130 if (body->lgd_logid.lgl_oid > 0)
131 logid = &body->lgd_logid;
133 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
137 disk_obd = ctxt->loc_exp->exp_obd;
138 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
140 rc = llog_create(ctxt, &loghandle, logid, NULL);
144 rc = lustre_pack_reply(req, 2, size, NULL);
146 GOTO(out_close, rc = -ENOMEM);
148 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof (*body));
149 body->lgd_logid = loghandle->lgh_id;
150 flags = body->lgd_llh_flags;
151 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
154 rc = llog_destroy(loghandle);
157 llog_free_handle(loghandle);
161 llog_close(loghandle);
163 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
168 int llog_origin_handle_next_block(struct ptlrpc_request *req)
170 struct obd_export *exp = req->rq_export;
171 struct obd_device *obd = exp->exp_obd;
172 struct obd_device *disk_obd;
173 struct llog_handle *loghandle;
174 struct llogd_body *body;
175 struct lvfs_run_ctxt saved;
176 struct llog_ctxt *ctxt;
180 int size[3] = { sizeof(struct ptlrpc_body),
186 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
187 lustre_swab_llogd_body);
189 CERROR ("Can't unpack llogd_body\n");
193 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
197 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
199 GOTO(out_free, rc = -EINVAL);
200 disk_obd = ctxt->loc_exp->exp_obd;
201 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
203 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
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, &body->lgd_saved_index,
215 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
220 rc = lustre_pack_reply(req, 3, size, NULL);
222 GOTO(out_close, rc = -ENOMEM);
224 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof (body));
225 memcpy(ptr, body, sizeof(*body));
227 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF+1, LLOG_CHUNK_SIZE);
228 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
231 rc2 = llog_close(loghandle);
236 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
239 OBD_FREE(buf, LLOG_CHUNK_SIZE);
243 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
245 struct obd_export *exp = req->rq_export;
246 struct obd_device *obd = exp->exp_obd;
247 struct llog_handle *loghandle;
248 struct llogd_body *body;
249 struct obd_device *disk_obd;
250 struct lvfs_run_ctxt saved;
251 struct llog_ctxt *ctxt;
255 int size[] = { sizeof(struct ptlrpc_body),
261 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
262 lustre_swab_llogd_body);
264 CERROR ("Can't unpack llogd_body\n");
268 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
272 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
273 LASSERT(ctxt != NULL);
274 disk_obd = ctxt->loc_exp->exp_obd;
275 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
277 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
281 flags = body->lgd_llh_flags;
282 rc = llog_init_handle(loghandle, flags, NULL);
286 memset(buf, 0, LLOG_CHUNK_SIZE);
287 rc = llog_prev_block(loghandle, body->lgd_index,
288 buf, LLOG_CHUNK_SIZE);
292 rc = lustre_pack_reply(req, 3, size, NULL);
294 GOTO(out_close, rc = -ENOMEM);
296 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(body));
297 memcpy(ptr, body, sizeof(*body));
299 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF+1, LLOG_CHUNK_SIZE);
300 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
303 rc2 = llog_close(loghandle);
308 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
310 OBD_FREE(buf, LLOG_CHUNK_SIZE);
314 int llog_origin_handle_read_header(struct ptlrpc_request *req)
316 struct obd_export *exp = req->rq_export;
317 struct obd_device *obd = exp->exp_obd;
318 struct obd_device *disk_obd;
319 struct llog_handle *loghandle;
320 struct llogd_body *body;
321 struct llog_log_hdr *hdr;
322 struct lvfs_run_ctxt saved;
323 struct llog_ctxt *ctxt;
325 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*hdr) };
329 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
330 lustre_swab_llogd_body);
332 CERROR ("Can't unpack llogd_body\n");
336 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
339 disk_obd = ctxt->loc_exp->exp_obd;
340 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
342 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
346 /* init_handle reads the header */
347 flags = body->lgd_llh_flags;
348 rc = llog_init_handle(loghandle, flags, NULL);
352 rc = lustre_pack_reply(req, 2, size, NULL);
354 GOTO(out_close, rc = -ENOMEM);
356 hdr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*hdr));
357 memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
360 rc2 = llog_close(loghandle);
364 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
369 int llog_origin_handle_close(struct ptlrpc_request *req)
378 int llog_origin_handle_cancel(struct ptlrpc_request *req)
380 struct obd_device *obd = req->rq_export->exp_obd;
381 struct obd_device *disk_obd;
382 struct llog_cookie *logcookies;
383 struct llog_ctxt *ctxt = NULL;
384 int num_cookies, rc = 0, err, i;
385 struct lvfs_run_ctxt saved;
386 struct llog_handle *cathandle;
391 logcookies = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
392 sizeof(*logcookies));
393 num_cookies = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF) /
395 if (logcookies == NULL || num_cookies == 0) {
396 DEBUG_REQ(D_HA, req, "no cookies sent");
400 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
402 CWARN("llog subsys not setup or already cleanup\n");
406 disk_obd = ctxt->loc_exp->exp_obd;
407 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
408 for (i = 0; i < num_cookies; i++, logcookies++) {
409 cathandle = ctxt->loc_handle;
410 LASSERT(cathandle != NULL);
411 inode = cathandle->lgh_file->f_dentry->d_inode;
413 handle = fsfilt_start_log(disk_obd, inode,
414 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
415 if (IS_ERR(handle)) {
416 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
417 GOTO(pop_ctxt, rc = PTR_ERR(handle));
420 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
422 err = fsfilt_commit(disk_obd, inode, handle, 0);
424 CERROR("error committing transaction: %d\n", err);
431 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
433 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
435 CDEBUG(D_RPCTRACE, "cancel %d llog-records\n", num_cookies);
440 EXPORT_SYMBOL(llog_origin_handle_cancel);
442 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
445 struct mds_obd *mds = &obd->u.mds;
446 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
447 struct lvfs_run_ctxt saved;
448 struct llog_handle *handle = NULL;
450 int rc, i, l, remains = buf_len;
453 if (ctxt == NULL || mds == NULL)
454 GOTO(release_ctxt, rc = -EOPNOTSUPP);
456 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
458 sprintf(name[0], "%s", mds->mds_profile);
459 sprintf(name[1], "%s-clean", mds->mds_profile);
460 sprintf(name[2], "%s", client);
461 sprintf(name[3], "%s-clean", client);
463 for (i = 0; i < 4; i++) {
464 int index, uncanceled = 0;
465 rc = llog_create(ctxt, &handle, NULL, name[i]);
468 rc = llog_init_handle(handle, 0, NULL);
471 GOTO(out_pop, rc = -ENOENT);
474 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
475 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
479 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
480 "Last Index: %d\nUncanceled Records: %d\n\n",
482 handle->lgh_file->f_dentry->d_inode->i_size,
483 handle->lgh_last_idx, uncanceled);
492 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
499 struct llog_ctxt *ctxt;
505 static int llog_catinfo_cb(struct llog_handle *cat,
506 struct llog_rec_hdr *rec, void *data)
508 static char *out = NULL;
509 static int remains = 0;
510 struct llog_ctxt *ctxt = NULL;
511 struct llog_handle *handle;
512 struct llog_logid *logid;
513 struct llog_logid_rec *lir;
514 int l, rc, index, count = 0;
515 struct cb_data *cbd = (struct cb_data*)data;
519 remains = cbd->remains;
523 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
529 lir = (struct llog_logid_rec *)rec;
530 logid = &lir->lid_id;
531 rc = llog_create(ctxt, &handle, logid, NULL);
534 rc = llog_init_handle(handle, 0, NULL);
538 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
539 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
543 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
544 "\tLog Size: %llu\n\tLast Index: %d\n"
545 "\tUncanceled Records: %d\n",
546 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
547 handle->lgh_file->f_dentry->d_inode->i_size,
548 handle->lgh_last_idx, count);
552 cbd->remains = remains;
554 CWARN("Not enough memory\n");
563 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
566 struct mds_obd *mds = &obd->u.mds;
567 struct llog_handle *handle;
568 struct lvfs_run_ctxt saved;
570 struct llog_catid *idarray;
571 struct llog_logid *id;
572 char name[32] = CATLIST;
575 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
577 if (ctxt == NULL || mds == NULL)
578 GOTO(release_ctxt, rc = -EOPNOTSUPP);
580 count = mds->mds_lov_desc.ld_tgt_count;
581 size = sizeof(*idarray) * count;
583 OBD_ALLOC(idarray, size);
585 GOTO(release_ctxt, rc = -ENOMEM);
587 rc = llog_get_cat_list(obd, obd, name, count, idarray);
591 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
595 data.remains = buf_len;
596 for (i = 0; i < count; i++) {
597 int l, index, uncanceled = 0;
599 id = &idarray[i].lci_logid;
600 rc = llog_create(ctxt, &handle, id, NULL);
603 rc = llog_init_handle(handle, 0, NULL);
606 GOTO(out_pop, rc = -ENOENT);
608 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
609 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
612 l = snprintf(data.out, data.remains,
613 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
615 id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
622 llog_process(handle, llog_catinfo_cb, &data, NULL);
625 if (data.remains <= 0)
629 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
631 OBD_FREE(idarray, size);
638 int llog_catinfo(struct ptlrpc_request *req)
640 struct obd_export *exp = req->rq_export;
641 struct obd_device *obd = exp->exp_obd;
644 int rc, buf_len = LLOG_CHUNK_SIZE;
645 int size[2] = { sizeof(struct ptlrpc_body), buf_len };
647 OBD_ALLOC(buf, buf_len);
650 memset(buf, 0, buf_len);
652 keyword = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, 0);
654 if (strcmp(keyword, "config") == 0) {
655 char *client = lustre_msg_string(req->rq_reqmsg,
657 rc = llog_catinfo_config(obd, buf, buf_len, client);
658 } else if (strcmp(keyword, "deletions") == 0) {
659 rc = llog_catinfo_deletions(obd, buf, buf_len);
664 rc = lustre_pack_reply(req, 2, size, NULL);
666 GOTO(out_free, rc = -ENOMEM);
668 reply = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, buf_len);
669 if (strlen(buf) == 0)
670 sprintf(buf, "%s", "No log informations\n");
671 memcpy(reply, buf, buf_len);
674 OBD_FREE(buf, buf_len);
678 #else /* !__KERNEL__ */
679 int llog_origin_handle_create(struct ptlrpc_request *req)
685 int llog_origin_handle_destroy(struct ptlrpc_request *req)
691 int llog_origin_handle_next_block(struct ptlrpc_request *req)
696 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
701 int llog_origin_handle_read_header(struct ptlrpc_request *req)
706 int llog_origin_handle_close(struct ptlrpc_request *req)
711 int llog_origin_handle_cancel(struct ptlrpc_request *req)