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 2008 Sun Microsystems, Inc. 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;
75 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
79 if (body->lgd_logid.lgl_oid > 0)
80 logid = &body->lgd_logid;
82 if (req_capsule_field_present(&req->rq_pill, &RMF_NAME, RCL_CLIENT)) {
83 name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
86 CDEBUG(D_INFO, "opening log %s\n", name);
89 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
92 disk_obd = ctxt->loc_exp->exp_obd;
93 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
95 rc = llog_create(ctxt, &loghandle, logid, name);
99 rc = req_capsule_server_pack(&req->rq_pill);
101 GOTO(out_close, rc = -ENOMEM);
103 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
104 body->lgd_logid = loghandle->lgh_id;
108 rc2 = llog_close(loghandle);
112 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
117 int llog_origin_handle_destroy(struct ptlrpc_request *req)
119 struct obd_export *exp = req->rq_export;
120 struct obd_device *obd = exp->exp_obd;
121 struct obd_device *disk_obd;
122 struct llog_handle *loghandle;
123 struct llogd_body *body;
124 struct lvfs_run_ctxt saved;
125 struct llog_logid *logid = NULL;
126 struct llog_ctxt *ctxt;
131 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
135 if (body->lgd_logid.lgl_oid > 0)
136 logid = &body->lgd_logid;
138 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
142 disk_obd = ctxt->loc_exp->exp_obd;
143 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
145 rc = llog_create(ctxt, &loghandle, logid, NULL);
149 rc = req_capsule_server_pack(&req->rq_pill);
151 GOTO(out_close, rc = -ENOMEM);
153 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
154 body->lgd_logid = loghandle->lgh_id;
155 flags = body->lgd_llh_flags;
156 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
159 rc = llog_destroy(loghandle);
162 llog_free_handle(loghandle);
166 llog_close(loghandle);
168 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
173 int llog_origin_handle_next_block(struct ptlrpc_request *req)
175 struct obd_export *exp = req->rq_export;
176 struct obd_device *obd = exp->exp_obd;
177 struct obd_device *disk_obd;
178 struct llog_handle *loghandle;
179 struct llogd_body *body;
180 struct llogd_body *repbody;
181 struct lvfs_run_ctxt saved;
182 struct llog_ctxt *ctxt;
189 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
193 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
197 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
199 GOTO(out_free, rc = -ENODEV);
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);
219 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
221 rc = req_capsule_server_pack(&req->rq_pill);
223 GOTO(out_close, rc = -ENOMEM);
225 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
228 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
229 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
232 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 llogd_body *repbody;
250 struct obd_device *disk_obd;
251 struct lvfs_run_ctxt saved;
252 struct llog_ctxt *ctxt;
259 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
263 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
267 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
269 GOTO(out_free, rc = -ENODEV);
271 disk_obd = ctxt->loc_exp->exp_obd;
272 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
274 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
278 flags = body->lgd_llh_flags;
279 rc = llog_init_handle(loghandle, flags, NULL);
283 memset(buf, 0, LLOG_CHUNK_SIZE);
284 rc = llog_prev_block(loghandle, body->lgd_index,
285 buf, LLOG_CHUNK_SIZE);
289 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
291 rc = req_capsule_server_pack(&req->rq_pill);
293 GOTO(out_close, rc = -ENOMEM);
295 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
298 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
299 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
302 rc2 = llog_close(loghandle);
307 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;
328 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
332 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
336 disk_obd = ctxt->loc_exp->exp_obd;
337 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
339 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
344 * llog_init_handle() reads the llog header
346 flags = body->lgd_llh_flags;
347 rc = llog_init_handle(loghandle, flags, NULL);
351 rc = req_capsule_server_pack(&req->rq_pill);
353 GOTO(out_close, rc = -ENOMEM);
355 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
356 *hdr = *loghandle->lgh_hdr;
359 rc2 = llog_close(loghandle);
363 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
368 int llog_origin_handle_close(struct ptlrpc_request *req)
375 int llog_origin_handle_cancel(struct ptlrpc_request *req)
377 struct obd_device *obd = req->rq_export->exp_obd;
378 int num_cookies, rc = 0, err, i, failed = 0;
379 struct obd_device *disk_obd;
380 struct llog_cookie *logcookies;
381 struct llog_ctxt *ctxt = NULL;
382 struct lvfs_run_ctxt saved;
383 struct llog_handle *cathandle;
388 logcookies = req_capsule_client_get(&req->rq_pill, &RMF_LOGCOOKIES);
389 num_cookies = req_capsule_get_size(&req->rq_pill, &RMF_LOGCOOKIES,
390 RCL_CLIENT) / sizeof(*logcookies);
391 if (logcookies == NULL || num_cookies == 0) {
392 DEBUG_REQ(D_HA, req, "No llog cookies sent");
396 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
400 disk_obd = ctxt->loc_exp->exp_obd;
401 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
402 for (i = 0; i < num_cookies; i++, logcookies++) {
403 cathandle = ctxt->loc_handle;
404 LASSERT(cathandle != NULL);
405 inode = cathandle->lgh_file->f_dentry->d_inode;
407 handle = fsfilt_start_log(disk_obd, inode,
408 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
409 if (IS_ERR(handle)) {
410 CERROR("fsfilt_start_log() failed: %ld\n",
412 GOTO(pop_ctxt, rc = PTR_ERR(handle));
415 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
418 * Do not raise -ENOENT errors for resent rpcs. This rec already
422 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)) {
424 * Do not change this message, reply-single.sh test_59b
425 * expects to find this in log.
427 CDEBUG(D_RPCTRACE, "RESENT cancel req %p - ignored\n",
430 } else if (rc == 0) {
431 CDEBUG(D_RPCTRACE, "Canceled %d llog-records\n",
435 err = fsfilt_commit(disk_obd, inode, handle, 0);
437 CERROR("Error committing transaction: %d\n", err);
447 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
449 CERROR("Cancel %d of %d llog-records failed: %d\n",
450 failed, num_cookies, rc);
455 EXPORT_SYMBOL(llog_origin_handle_cancel);
457 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
460 struct mds_obd *mds = &obd->u.mds;
461 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
462 struct lvfs_run_ctxt saved;
463 struct llog_handle *handle = NULL;
465 int rc, i, l, remains = buf_len;
469 if (ctxt == NULL || mds == NULL)
470 GOTO(release_ctxt, rc = -ENODEV);
472 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
474 sprintf(name[0], "%s", mds->mds_profile);
475 sprintf(name[1], "%s-clean", mds->mds_profile);
476 sprintf(name[2], "%s", client);
477 sprintf(name[3], "%s-clean", client);
479 for (i = 0; i < 4; i++) {
480 int index, uncanceled = 0;
481 rc = llog_create(ctxt, &handle, NULL, name[i]);
484 rc = llog_init_handle(handle, 0, NULL);
487 GOTO(out_pop, rc = -ENOENT);
490 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
491 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
495 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
496 "Last Index: %d\nUncanceled Records: %d\n\n",
498 i_size_read(handle->lgh_file->f_dentry->d_inode),
499 handle->lgh_last_idx, uncanceled);
509 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
516 struct llog_ctxt *ctxt;
522 static int llog_catinfo_cb(struct llog_handle *cat,
523 struct llog_rec_hdr *rec, void *data)
525 static char *out = NULL;
526 static int remains = 0;
527 struct llog_ctxt *ctxt = NULL;
528 struct llog_handle *handle;
529 struct llog_logid *logid;
530 struct llog_logid_rec *lir;
531 int l, rc, index, count = 0;
532 struct cb_data *cbd = (struct cb_data*)data;
537 remains = cbd->remains;
541 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
547 lir = (struct llog_logid_rec *)rec;
548 logid = &lir->lid_id;
549 rc = llog_create(ctxt, &handle, logid, NULL);
552 rc = llog_init_handle(handle, 0, NULL);
556 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
557 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
561 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
562 "\tLog Size: %llu\n\tLast Index: %d\n"
563 "\tUncanceled Records: %d\n",
564 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
565 i_size_read(handle->lgh_file->f_dentry->d_inode),
566 handle->lgh_last_idx, count);
570 cbd->remains = remains;
572 CWARN("Not enough memory\n");
581 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
584 struct mds_obd *mds = &obd->u.mds;
585 struct llog_handle *handle;
586 struct lvfs_run_ctxt saved;
588 struct llog_catid *idarray;
589 char name[32] = CATLIST;
591 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
595 if (ctxt == NULL || mds == NULL)
596 GOTO(release_ctxt, rc = -ENODEV);
598 count = mds->mds_lov_desc.ld_tgt_count;
599 size = sizeof(*idarray) * count;
601 OBD_VMALLOC(idarray, size);
603 GOTO(release_ctxt, rc = -ENOMEM);
605 mutex_down(&obd->obd_olg.olg_cat_processing);
606 rc = llog_get_cat_list(obd, obd, name, 0, count, idarray);
610 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
614 data.remains = buf_len;
615 for (i = 0; i < count; i++) {
616 int l, index, uncanceled = 0;
618 rc = llog_create(ctxt, &handle, &idarray[i].lci_logid, NULL);
621 rc = llog_init_handle(handle, 0, NULL);
624 GOTO(out_pop, rc = -ENOENT);
626 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
627 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
630 l = snprintf(data.out, data.remains,
631 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
633 idarray[i].lci_logid.lgl_oid,
634 idarray[i].lci_logid.lgl_ogr,
635 idarray[i].lci_logid.lgl_ogen, uncanceled);
641 llog_process(handle, llog_catinfo_cb, &data, NULL);
644 if (data.remains <= 0)
649 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
651 mutex_up(&obd->obd_olg.olg_cat_processing);
652 OBD_VFREE(idarray, size);
658 int llog_catinfo(struct ptlrpc_request *req)
660 struct obd_export *exp = req->rq_export;
661 struct obd_device *obd = exp->exp_obd;
667 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
671 memset(buf, 0, LLOG_CHUNK_SIZE);
673 keyword = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
676 if (strcmp(keyword, "config") == 0) {
677 char *client = req_capsule_client_get(&req->rq_pill,
681 rc = llog_catinfo_config(obd, buf, LLOG_CHUNK_SIZE, client);
682 } else if (strcmp(keyword, "deletions") == 0) {
683 rc = llog_catinfo_deletions(obd, buf, LLOG_CHUNK_SIZE);
688 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
690 rc = req_capsule_server_pack(&req->rq_pill);
692 GOTO(out_free, rc = -ENOMEM);
694 reply = req_capsule_server_get(&req->rq_pill, &RMF_STRING);
695 if (strlen(buf) == 0)
696 sprintf(buf, "%s", "No log informations\n");
697 memcpy(reply, buf, LLOG_CHUNK_SIZE);
700 OBD_FREE(buf, LLOG_CHUNK_SIZE);
704 #else /* !__KERNEL__ */
705 int llog_origin_handle_create(struct ptlrpc_request *req)
711 int llog_origin_handle_destroy(struct ptlrpc_request *req)
717 int llog_origin_handle_next_block(struct ptlrpc_request *req)
722 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
727 int llog_origin_handle_read_header(struct ptlrpc_request *req)
732 int llog_origin_handle_close(struct ptlrpc_request *req)
737 int llog_origin_handle_cancel(struct ptlrpc_request *req)