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, "%s: opening log %s\n", obd->obd_name, name);
89 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
91 CDEBUG(D_WARNING, "%s: no ctxt. group=%p idx=%d name=%s\n",
92 obd->obd_name, &obd->obd_olg, body->lgd_ctxt_idx, name);
95 disk_obd = ctxt->loc_exp->exp_obd;
96 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
98 rc = llog_create(ctxt, &loghandle, logid, name);
102 rc = req_capsule_server_pack(&req->rq_pill);
104 GOTO(out_close, rc = -ENOMEM);
106 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
107 body->lgd_logid = loghandle->lgh_id;
111 rc2 = llog_close(loghandle);
115 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
120 int llog_origin_handle_destroy(struct ptlrpc_request *req)
122 struct obd_export *exp = req->rq_export;
123 struct obd_device *obd = exp->exp_obd;
124 struct obd_device *disk_obd;
125 struct llog_handle *loghandle;
126 struct llogd_body *body;
127 struct lvfs_run_ctxt saved;
128 struct llog_logid *logid = NULL;
129 struct llog_ctxt *ctxt;
134 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
138 if (body->lgd_logid.lgl_oid > 0)
139 logid = &body->lgd_logid;
141 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
145 disk_obd = ctxt->loc_exp->exp_obd;
146 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
148 rc = llog_create(ctxt, &loghandle, logid, NULL);
152 rc = req_capsule_server_pack(&req->rq_pill);
154 GOTO(out_close, rc = -ENOMEM);
156 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
157 body->lgd_logid = loghandle->lgh_id;
158 flags = body->lgd_llh_flags;
159 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
162 rc = llog_destroy(loghandle);
165 llog_free_handle(loghandle);
169 llog_close(loghandle);
171 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
176 int llog_origin_handle_next_block(struct ptlrpc_request *req)
178 struct obd_export *exp = req->rq_export;
179 struct obd_device *obd = exp->exp_obd;
180 struct obd_device *disk_obd;
181 struct llog_handle *loghandle;
182 struct llogd_body *body;
183 struct llogd_body *repbody;
184 struct lvfs_run_ctxt saved;
185 struct llog_ctxt *ctxt;
192 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
196 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
200 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
202 GOTO(out_free, rc = -ENODEV);
203 disk_obd = ctxt->loc_exp->exp_obd;
204 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
206 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
210 flags = body->lgd_llh_flags;
211 rc = llog_init_handle(loghandle, flags, NULL);
215 memset(buf, 0, LLOG_CHUNK_SIZE);
216 rc = llog_next_block(loghandle, &body->lgd_saved_index,
218 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
222 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
224 rc = req_capsule_server_pack(&req->rq_pill);
226 GOTO(out_close, rc = -ENOMEM);
228 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
231 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
232 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
235 rc2 = llog_close(loghandle);
239 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
242 OBD_FREE(buf, LLOG_CHUNK_SIZE);
246 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
248 struct obd_export *exp = req->rq_export;
249 struct obd_device *obd = exp->exp_obd;
250 struct llog_handle *loghandle;
251 struct llogd_body *body;
252 struct llogd_body *repbody;
253 struct obd_device *disk_obd;
254 struct lvfs_run_ctxt saved;
255 struct llog_ctxt *ctxt;
262 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
266 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
270 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
272 GOTO(out_free, rc = -ENODEV);
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 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
294 rc = req_capsule_server_pack(&req->rq_pill);
296 GOTO(out_close, rc = -ENOMEM);
298 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
301 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
302 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
305 rc2 = llog_close(loghandle);
310 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
313 OBD_FREE(buf, LLOG_CHUNK_SIZE);
317 int llog_origin_handle_read_header(struct ptlrpc_request *req)
319 struct obd_export *exp = req->rq_export;
320 struct obd_device *obd = exp->exp_obd;
321 struct obd_device *disk_obd;
322 struct llog_handle *loghandle;
323 struct llogd_body *body;
324 struct llog_log_hdr *hdr;
325 struct lvfs_run_ctxt saved;
326 struct llog_ctxt *ctxt;
331 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
335 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);
347 * llog_init_handle() reads the llog header
349 flags = body->lgd_llh_flags;
350 rc = llog_init_handle(loghandle, flags, NULL);
354 rc = req_capsule_server_pack(&req->rq_pill);
356 GOTO(out_close, rc = -ENOMEM);
358 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
359 *hdr = *loghandle->lgh_hdr;
362 rc2 = llog_close(loghandle);
366 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
371 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 int num_cookies, rc = 0, err, i, failed = 0;
382 struct obd_device *disk_obd;
383 struct llog_cookie *logcookies;
384 struct llog_ctxt *ctxt = NULL;
385 struct lvfs_run_ctxt saved;
386 struct llog_handle *cathandle;
391 logcookies = req_capsule_client_get(&req->rq_pill, &RMF_LOGCOOKIES);
392 num_cookies = req_capsule_get_size(&req->rq_pill, &RMF_LOGCOOKIES,
393 RCL_CLIENT) / sizeof(*logcookies);
394 if (logcookies == NULL || num_cookies == 0) {
395 DEBUG_REQ(D_HA, req, "No llog cookies sent");
399 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
403 disk_obd = ctxt->loc_exp->exp_obd;
404 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
405 for (i = 0; i < num_cookies; i++, logcookies++) {
406 cathandle = ctxt->loc_handle;
407 LASSERT(cathandle != NULL);
408 inode = cathandle->lgh_file->f_dentry->d_inode;
410 handle = fsfilt_start_log(disk_obd, inode,
411 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
412 if (IS_ERR(handle)) {
413 CERROR("fsfilt_start_log() failed: %ld\n",
415 GOTO(pop_ctxt, rc = PTR_ERR(handle));
418 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
421 * Do not raise -ENOENT errors for resent rpcs. This rec already
425 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)) {
427 * Do not change this message, reply-single.sh test_59b
428 * expects to find this in log.
430 CDEBUG(D_RPCTRACE, "RESENT cancel req %p - ignored\n",
433 } else if (rc == 0) {
434 CDEBUG(D_RPCTRACE, "Canceled %d llog-records\n",
438 err = fsfilt_commit(disk_obd, inode, handle, 0);
440 CERROR("Error committing transaction: %d\n", err);
450 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
452 CERROR("Cancel %d of %d llog-records failed: %d\n",
453 failed, num_cookies, rc);
458 EXPORT_SYMBOL(llog_origin_handle_cancel);
460 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
463 struct mds_obd *mds = &obd->u.mds;
464 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
465 struct lvfs_run_ctxt saved;
466 struct llog_handle *handle = NULL;
468 int rc, i, l, remains = buf_len;
472 if (ctxt == NULL || mds == NULL)
473 GOTO(release_ctxt, rc = -ENODEV);
475 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
477 sprintf(name[0], "%s", mds->mds_profile);
478 sprintf(name[1], "%s-clean", mds->mds_profile);
479 sprintf(name[2], "%s", client);
480 sprintf(name[3], "%s-clean", client);
482 for (i = 0; i < 4; i++) {
483 int index, uncanceled = 0;
484 rc = llog_create(ctxt, &handle, NULL, name[i]);
487 rc = llog_init_handle(handle, 0, NULL);
490 GOTO(out_pop, rc = -ENOENT);
493 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
494 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
498 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
499 "Last Index: %d\nUncanceled Records: %d\n\n",
501 i_size_read(handle->lgh_file->f_dentry->d_inode),
502 handle->lgh_last_idx, uncanceled);
512 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
519 struct llog_ctxt *ctxt;
525 static int llog_catinfo_cb(struct llog_handle *cat,
526 struct llog_rec_hdr *rec, void *data)
528 static char *out = NULL;
529 static int remains = 0;
530 struct llog_ctxt *ctxt = NULL;
531 struct llog_handle *handle;
532 struct llog_logid *logid;
533 struct llog_logid_rec *lir;
534 int l, rc, index, count = 0;
535 struct cb_data *cbd = (struct cb_data*)data;
540 remains = cbd->remains;
544 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
550 lir = (struct llog_logid_rec *)rec;
551 logid = &lir->lid_id;
552 rc = llog_create(ctxt, &handle, logid, NULL);
555 rc = llog_init_handle(handle, 0, NULL);
559 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
560 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
564 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
565 "\tLog Size: %llu\n\tLast Index: %d\n"
566 "\tUncanceled Records: %d\n",
567 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
568 i_size_read(handle->lgh_file->f_dentry->d_inode),
569 handle->lgh_last_idx, count);
573 cbd->remains = remains;
575 CWARN("Not enough memory\n");
584 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
587 struct mds_obd *mds = &obd->u.mds;
588 struct llog_handle *handle;
589 struct lvfs_run_ctxt saved;
591 struct llog_catid *idarray;
592 char name[32] = CATLIST;
594 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
598 if (ctxt == NULL || mds == NULL)
599 GOTO(release_ctxt, rc = -ENODEV);
601 count = mds->mds_lov_desc.ld_tgt_count;
602 size = sizeof(*idarray) * count;
604 OBD_VMALLOC(idarray, size);
606 GOTO(release_ctxt, rc = -ENOMEM);
608 mutex_down(&obd->obd_olg.olg_cat_processing);
609 rc = llog_get_cat_list(obd, name, 0, count, idarray);
613 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
617 data.remains = buf_len;
618 for (i = 0; i < count; i++) {
619 int l, index, uncanceled = 0;
621 rc = llog_create(ctxt, &handle, &idarray[i].lci_logid, NULL);
624 rc = llog_init_handle(handle, 0, NULL);
627 GOTO(out_pop, rc = -ENOENT);
629 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
630 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
633 l = snprintf(data.out, data.remains,
634 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
636 idarray[i].lci_logid.lgl_oid,
637 idarray[i].lci_logid.lgl_ogr,
638 idarray[i].lci_logid.lgl_ogen, uncanceled);
644 llog_process(handle, llog_catinfo_cb, &data, NULL);
647 if (data.remains <= 0)
652 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
654 mutex_up(&obd->obd_olg.olg_cat_processing);
655 OBD_VFREE(idarray, size);
661 int llog_catinfo(struct ptlrpc_request *req)
663 struct obd_export *exp = req->rq_export;
664 struct obd_device *obd = exp->exp_obd;
670 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
674 memset(buf, 0, LLOG_CHUNK_SIZE);
676 keyword = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
679 if (strcmp(keyword, "config") == 0) {
680 char *client = req_capsule_client_get(&req->rq_pill,
684 rc = llog_catinfo_config(obd, buf, LLOG_CHUNK_SIZE, client);
685 } else if (strcmp(keyword, "deletions") == 0) {
686 rc = llog_catinfo_deletions(obd, buf, LLOG_CHUNK_SIZE);
691 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
693 rc = req_capsule_server_pack(&req->rq_pill);
695 GOTO(out_free, rc = -ENOMEM);
697 reply = req_capsule_server_get(&req->rq_pill, &RMF_STRING);
698 if (strlen(buf) == 0)
699 sprintf(buf, "%s", "No log informations\n");
700 memcpy(reply, buf, LLOG_CHUNK_SIZE);
703 OBD_FREE(buf, LLOG_CHUNK_SIZE);
707 #else /* !__KERNEL__ */
708 int llog_origin_handle_create(struct ptlrpc_request *req)
714 int llog_origin_handle_destroy(struct ptlrpc_request *req)
720 int llog_origin_handle_next_block(struct ptlrpc_request *req)
725 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
730 int llog_origin_handle_read_header(struct ptlrpc_request *req)
735 int llog_origin_handle_close(struct ptlrpc_request *req)
740 int llog_origin_handle_cancel(struct ptlrpc_request *req)