1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/llog_server.c
38 * remote api for llog - server side
40 * Author: Andreas Dilger <adilger@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_LOG
50 #include <liblustre.h>
53 #include <obd_class.h>
54 #include <lustre_log.h>
55 #include <lustre_net.h>
56 #include <libcfs/list.h>
57 #include <lustre_fsfilt.h>
59 #if defined(__KERNEL__) && defined(LUSTRE_LOG_SERVER)
61 int llog_origin_handle_create(struct ptlrpc_request *req)
63 struct obd_export *exp = req->rq_export;
64 struct obd_device *obd = exp->exp_obd;
65 struct obd_device *disk_obd;
66 struct llog_handle *loghandle;
67 struct llogd_body *body;
68 struct lvfs_run_ctxt saved;
69 struct llog_logid *logid = NULL;
70 struct llog_ctxt *ctxt;
72 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
76 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
77 lustre_swab_llogd_body);
79 CERROR("Can't unpack llogd_body\n");
83 if (body->lgd_logid.lgl_oid > 0)
84 logid = &body->lgd_logid;
86 if (lustre_msg_bufcount(req->rq_reqmsg) > 2) {
87 name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
89 CERROR("Can't unpack name\n");
92 CDEBUG(D_INFO, "Opening log %s\n", name);
95 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
99 disk_obd = ctxt->loc_exp->exp_obd;
100 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
102 rc = llog_create(ctxt, &loghandle, logid, name);
106 rc = lustre_pack_reply(req, 2, size, NULL);
108 GOTO(out_close, rc = -ENOMEM);
110 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
111 body->lgd_logid = loghandle->lgh_id;
114 rc2 = llog_close(loghandle);
118 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
123 int llog_origin_handle_destroy(struct ptlrpc_request *req)
125 struct obd_export *exp = req->rq_export;
126 struct obd_device *obd = exp->exp_obd;
127 struct obd_device *disk_obd;
128 struct llog_handle *loghandle;
129 struct llogd_body *body;
130 struct lvfs_run_ctxt saved;
131 struct llog_logid *logid = NULL;
132 struct llog_ctxt *ctxt;
133 int size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
138 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
139 lustre_swab_llogd_body);
141 CERROR ("Can't unpack llogd_body\n");
145 if (body->lgd_logid.lgl_oid > 0)
146 logid = &body->lgd_logid;
148 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
152 disk_obd = ctxt->loc_exp->exp_obd;
153 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
155 rc = llog_create(ctxt, &loghandle, logid, NULL);
157 /* This might already be killed. Let's check if this is
160 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
165 rc = lustre_pack_reply(req, 2, size, NULL);
167 GOTO(out_close, rc = -ENOMEM);
169 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof (*body));
170 body->lgd_logid = loghandle->lgh_id;
171 flags = body->lgd_llh_flags;
172 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
175 rc = llog_destroy(loghandle);
177 /* Do not check for resent as this is already done above after
180 llog_free_handle(loghandle);
184 llog_close(loghandle);
186 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
191 int llog_origin_handle_next_block(struct ptlrpc_request *req)
193 struct obd_export *exp = req->rq_export;
194 struct obd_device *obd = exp->exp_obd;
195 struct obd_device *disk_obd;
196 struct llog_handle *loghandle;
197 struct llogd_body *body;
198 struct lvfs_run_ctxt saved;
199 struct llog_ctxt *ctxt;
203 int size[3] = { sizeof(struct ptlrpc_body),
209 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
210 lustre_swab_llogd_body);
212 CERROR ("Can't unpack llogd_body\n");
216 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
220 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
222 GOTO(out_free, rc = -ENODEV);
224 disk_obd = ctxt->loc_exp->exp_obd;
225 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
227 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
231 flags = body->lgd_llh_flags;
232 rc = llog_init_handle(loghandle, flags, NULL);
236 memset(buf, 0, LLOG_CHUNK_SIZE);
237 rc = llog_next_block(loghandle, &body->lgd_saved_index,
239 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
243 rc = lustre_pack_reply(req, 3, size, NULL);
245 GOTO(out_close, rc = -ENOMEM);
247 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof (body));
248 memcpy(ptr, body, sizeof(*body));
250 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF+1, LLOG_CHUNK_SIZE);
251 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
254 rc2 = llog_close(loghandle);
258 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
261 OBD_FREE(buf, LLOG_CHUNK_SIZE);
265 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
267 struct obd_export *exp = req->rq_export;
268 struct obd_device *obd = exp->exp_obd;
269 struct llog_handle *loghandle;
270 struct llogd_body *body;
271 struct obd_device *disk_obd;
272 struct lvfs_run_ctxt saved;
273 struct llog_ctxt *ctxt;
277 int size[] = { sizeof(struct ptlrpc_body),
283 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
284 lustre_swab_llogd_body);
286 CERROR ("Can't unpack llogd_body\n");
290 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
294 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
296 GOTO(out_free, rc = -ENODEV);
298 disk_obd = ctxt->loc_exp->exp_obd;
299 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
301 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
305 flags = body->lgd_llh_flags;
306 rc = llog_init_handle(loghandle, flags, NULL);
310 memset(buf, 0, LLOG_CHUNK_SIZE);
311 rc = llog_prev_block(loghandle, body->lgd_index,
312 buf, LLOG_CHUNK_SIZE);
316 rc = lustre_pack_reply(req, 3, size, NULL);
318 GOTO(out_close, rc = -ENOMEM);
320 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(body));
321 memcpy(ptr, body, sizeof(*body));
323 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF+1, LLOG_CHUNK_SIZE);
324 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
327 rc2 = llog_close(loghandle);
331 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
334 OBD_FREE(buf, LLOG_CHUNK_SIZE);
338 int llog_origin_handle_read_header(struct ptlrpc_request *req)
340 struct obd_export *exp = req->rq_export;
341 struct obd_device *obd = exp->exp_obd;
342 struct obd_device *disk_obd;
343 struct llog_handle *loghandle;
344 struct llogd_body *body;
345 struct llog_log_hdr *hdr;
346 struct lvfs_run_ctxt saved;
347 struct llog_ctxt *ctxt;
349 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*hdr) };
353 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
354 lustre_swab_llogd_body);
356 CERROR ("Can't unpack llogd_body\n");
360 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
364 disk_obd = ctxt->loc_exp->exp_obd;
365 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
366 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
370 /* llog_init_handle() reads the header */
371 flags = body->lgd_llh_flags;
372 rc = llog_init_handle(loghandle, flags, NULL);
376 rc = lustre_pack_reply(req, 2, size, NULL);
378 GOTO(out_close, rc = -ENOMEM);
380 hdr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*hdr));
381 memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
384 rc2 = llog_close(loghandle);
388 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
393 int llog_origin_handle_close(struct ptlrpc_request *req)
399 int llog_origin_handle_cancel(struct ptlrpc_request *req)
401 struct obd_device *obd = req->rq_export->exp_obd;
402 int num_cookies, rc = 0, err, i, failed = 0;
403 struct obd_device *disk_obd;
404 struct llog_cookie *logcookies;
405 struct llog_ctxt *ctxt = NULL;
406 struct lvfs_run_ctxt saved;
407 struct llog_handle *cathandle;
412 logcookies = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
413 sizeof(*logcookies));
414 num_cookies = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF) /
416 if (logcookies == NULL || num_cookies == 0) {
417 DEBUG_REQ(D_HA, req, "No llog cookies sent");
421 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
425 disk_obd = ctxt->loc_exp->exp_obd;
426 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
427 for (i = 0; i < num_cookies; i++, logcookies++) {
428 cathandle = ctxt->loc_handle;
429 LASSERT(cathandle != NULL);
430 inode = cathandle->lgh_file->f_dentry->d_inode;
432 handle = fsfilt_start_log(disk_obd, inode,
433 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
434 if (IS_ERR(handle)) {
435 CERROR("fsfilt_start_log() failed: %ld\n",
437 GOTO(pop_ctxt, rc = PTR_ERR(handle));
440 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
442 err = fsfilt_commit(disk_obd, inode, handle, 0);
444 /* Do not raise -ENOENT errors for resent rpcs. This rec already
445 * might be killed. */
447 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)) {
448 CDEBUG(D_INFO, "RESENT cancel req %p - ignored\n", req);
453 CERROR("Error committing transaction: %d\n", err);
463 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
465 CERROR("Cancel %d of %d llog-records failed: %d\n",
466 failed, num_cookies, rc);
468 CDEBUG(D_RPCTRACE, "Canceled %d llog-records\n",
474 EXPORT_SYMBOL(llog_origin_handle_cancel);
476 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
479 struct mds_obd *mds = &obd->u.mds;
480 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
481 struct lvfs_run_ctxt saved;
482 struct llog_handle *handle = NULL;
484 int rc, i, l, remains = buf_len;
488 if (ctxt == NULL || mds == NULL)
489 GOTO(release_ctxt, rc = -ENODEV);
491 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
493 sprintf(name[0], "%s", mds->mds_profile);
494 sprintf(name[1], "%s-clean", mds->mds_profile);
495 sprintf(name[2], "%s", client);
496 sprintf(name[3], "%s-clean", client);
498 for (i = 0; i < 4; i++) {
499 int index, uncanceled = 0;
500 rc = llog_create(ctxt, &handle, NULL, name[i]);
503 rc = llog_init_handle(handle, 0, NULL);
506 GOTO(out_pop, rc = -ENOENT);
509 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
510 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
514 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
515 "Last Index: %d\nUncanceled Records: %d\n\n",
517 i_size_read(handle->lgh_file->f_dentry->d_inode),
518 handle->lgh_last_idx, uncanceled);
528 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
535 struct llog_ctxt *ctxt;
541 static int llog_catinfo_cb(struct llog_handle *cat,
542 struct llog_rec_hdr *rec, void *data)
544 static char *out = NULL;
545 static int remains = 0;
546 struct llog_ctxt *ctxt = NULL;
547 struct llog_handle *handle;
548 struct llog_logid *logid;
549 struct llog_logid_rec *lir;
550 int l, rc, index, count = 0;
551 struct cb_data *cbd = (struct cb_data*)data;
556 remains = cbd->remains;
560 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
566 lir = (struct llog_logid_rec *)rec;
567 logid = &lir->lid_id;
568 rc = llog_create(ctxt, &handle, logid, NULL);
572 rc = llog_init_handle(handle, 0, NULL);
576 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
577 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
581 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
582 "\tLog Size: %llu\n\tLast Index: %d\n"
583 "\tUncanceled Records: %d\n",
584 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
585 i_size_read(handle->lgh_file->f_dentry->d_inode),
586 handle->lgh_last_idx, count);
590 cbd->remains = remains;
592 CWARN("Not enough memory\n");
601 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
604 struct mds_obd *mds = &obd->u.mds;
605 struct llog_handle *handle;
606 struct lvfs_run_ctxt saved;
608 struct llog_catid *idarray;
609 char name[32] = CATLIST;
612 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
615 if (ctxt == NULL || mds == NULL)
616 GOTO(release_ctxt, rc = -ENODEV);
618 count = mds->mds_lov_desc.ld_tgt_count;
619 size = sizeof(*idarray) * count;
621 OBD_VMALLOC(idarray, size);
623 GOTO(release_ctxt, rc = -ENOMEM);
625 mutex_down(&obd->obd_llog_cat_process);
626 rc = llog_get_cat_list(obd, obd, name, 0, count, idarray);
630 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
634 data.remains = buf_len;
635 for (i = 0; i < count; i++) {
636 int l, index, uncanceled = 0;
638 rc = llog_create(ctxt, &handle, &idarray[i].lci_logid, NULL);
641 rc = llog_init_handle(handle, 0, NULL);
644 GOTO(out_pop, rc = -ENOENT);
646 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
647 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
650 l = snprintf(data.out, data.remains,
651 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
653 idarray[i].lci_logid.lgl_oid,
654 idarray[i].lci_logid.lgl_ogr,
655 idarray[i].lci_logid.lgl_ogen, uncanceled);
661 llog_process(handle, llog_catinfo_cb, &data, NULL);
664 if (data.remains <= 0)
669 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
671 mutex_up(&obd->obd_llog_cat_process);
672 OBD_VFREE(idarray, size);
678 int llog_catinfo(struct ptlrpc_request *req)
680 struct obd_export *exp = req->rq_export;
681 struct obd_device *obd = exp->exp_obd;
684 int rc, buf_len = LLOG_CHUNK_SIZE;
685 int size[2] = { sizeof(struct ptlrpc_body), buf_len };
688 OBD_ALLOC(buf, buf_len);
692 keyword = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, 0);
694 if (strcmp(keyword, "config") == 0) {
695 char *client = lustre_msg_string(req->rq_reqmsg,
697 rc = llog_catinfo_config(obd, buf, buf_len, client);
698 } else if (strcmp(keyword, "deletions") == 0) {
699 rc = llog_catinfo_deletions(obd, buf, buf_len);
704 rc = lustre_pack_reply(req, 2, size, NULL);
706 GOTO(out_free, rc = -ENOMEM);
708 reply = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, buf_len);
709 if (strlen(buf) == 0)
710 sprintf(buf, "%s", "No log informations\n");
711 memcpy(reply, buf, buf_len);
714 OBD_FREE(buf, buf_len);
718 #else /* !__KERNEL__ */
719 int llog_origin_handle_create(struct ptlrpc_request *req)
725 int llog_origin_handle_destroy(struct ptlrpc_request *req)
731 int llog_origin_handle_next_block(struct ptlrpc_request *req)
736 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
741 int llog_origin_handle_read_header(struct ptlrpc_request *req)
746 int llog_origin_handle_close(struct ptlrpc_request *req)
751 int llog_origin_handle_cancel(struct ptlrpc_request *req)