4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
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
46 #include <liblustre.h>
49 #include <obd_class.h>
50 #include <lustre_log.h>
51 #include <lustre_net.h>
52 #include <libcfs/list.h>
53 #include <lustre_fsfilt.h>
55 #if defined(__KERNEL__) && defined(LUSTRE_LOG_SERVER)
57 int llog_origin_handle_create(struct ptlrpc_request *req)
59 struct obd_export *exp = req->rq_export;
60 struct obd_device *obd = exp->exp_obd;
61 struct obd_device *disk_obd;
62 struct llog_handle *loghandle;
63 struct llogd_body *body;
64 struct lvfs_run_ctxt saved;
65 struct llog_logid *logid = NULL;
66 struct llog_ctxt *ctxt;
71 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
75 if (body->lgd_logid.lgl_oid > 0)
76 logid = &body->lgd_logid;
78 if (req_capsule_field_present(&req->rq_pill, &RMF_NAME, RCL_CLIENT)) {
79 name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
82 CDEBUG(D_INFO, "%s: opening log %s\n", obd->obd_name, name);
85 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
87 CDEBUG(D_WARNING, "%s: no ctxt. group=%p idx=%d name=%s\n",
88 obd->obd_name, &obd->obd_olg, body->lgd_ctxt_idx, name);
91 disk_obd = ctxt->loc_exp->exp_obd;
92 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
94 rc = llog_create(ctxt, &loghandle, logid, name);
98 rc = req_capsule_server_pack(&req->rq_pill);
100 GOTO(out_close, rc = -ENOMEM);
102 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
103 body->lgd_logid = loghandle->lgh_id;
107 rc2 = llog_close(loghandle);
111 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
116 int llog_origin_handle_destroy(struct ptlrpc_request *req)
118 struct obd_export *exp = req->rq_export;
119 struct obd_device *obd = exp->exp_obd;
120 struct obd_device *disk_obd;
121 struct llog_handle *loghandle;
122 struct llogd_body *body;
123 struct lvfs_run_ctxt saved;
124 struct llog_logid *logid = NULL;
125 struct llog_ctxt *ctxt;
129 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
133 if (body->lgd_logid.lgl_oid > 0)
134 logid = &body->lgd_logid;
136 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
140 disk_obd = ctxt->loc_exp->exp_obd;
141 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
143 rc = llog_create(ctxt, &loghandle, logid, NULL);
147 rc = req_capsule_server_pack(&req->rq_pill);
149 GOTO(out_close, rc = -ENOMEM);
151 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
152 body->lgd_logid = loghandle->lgh_id;
153 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
156 rc = llog_destroy(loghandle);
159 llog_free_handle(loghandle);
163 llog_close(loghandle);
165 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
170 int llog_origin_handle_next_block(struct ptlrpc_request *req)
172 struct obd_export *exp = req->rq_export;
173 struct obd_device *obd = exp->exp_obd;
174 struct obd_device *disk_obd;
175 struct llog_handle *loghandle;
176 struct llogd_body *body;
177 struct llogd_body *repbody;
178 struct lvfs_run_ctxt saved;
179 struct llog_ctxt *ctxt;
186 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
190 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
194 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
196 GOTO(out_free, rc = -ENODEV);
197 disk_obd = ctxt->loc_exp->exp_obd;
198 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
200 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
204 flags = body->lgd_llh_flags;
205 rc = llog_init_handle(loghandle, flags, NULL);
209 memset(buf, 0, LLOG_CHUNK_SIZE);
210 rc = llog_next_block(loghandle, &body->lgd_saved_index,
212 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
216 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
218 rc = req_capsule_server_pack(&req->rq_pill);
220 GOTO(out_close, rc = -ENOMEM);
222 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
225 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
226 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
229 rc2 = llog_close(loghandle);
233 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
236 OBD_FREE(buf, LLOG_CHUNK_SIZE);
240 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
242 struct obd_export *exp = req->rq_export;
243 struct obd_device *obd = exp->exp_obd;
244 struct llog_handle *loghandle;
245 struct llogd_body *body;
246 struct llogd_body *repbody;
247 struct obd_device *disk_obd;
248 struct lvfs_run_ctxt saved;
249 struct llog_ctxt *ctxt;
256 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
260 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
264 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
266 GOTO(out_free, rc = -ENODEV);
268 disk_obd = ctxt->loc_exp->exp_obd;
269 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
271 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
275 flags = body->lgd_llh_flags;
276 rc = llog_init_handle(loghandle, flags, NULL);
280 memset(buf, 0, LLOG_CHUNK_SIZE);
281 rc = llog_prev_block(loghandle, body->lgd_index,
282 buf, LLOG_CHUNK_SIZE);
286 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
288 rc = req_capsule_server_pack(&req->rq_pill);
290 GOTO(out_close, rc = -ENOMEM);
292 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
295 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
296 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
299 rc2 = llog_close(loghandle);
304 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
307 OBD_FREE(buf, LLOG_CHUNK_SIZE);
311 int llog_origin_handle_read_header(struct ptlrpc_request *req)
313 struct obd_export *exp = req->rq_export;
314 struct obd_device *obd = exp->exp_obd;
315 struct obd_device *disk_obd;
316 struct llog_handle *loghandle;
317 struct llogd_body *body;
318 struct llog_log_hdr *hdr;
319 struct lvfs_run_ctxt saved;
320 struct llog_ctxt *ctxt;
325 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
329 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
333 disk_obd = ctxt->loc_exp->exp_obd;
334 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
336 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
341 * llog_init_handle() reads the llog header
343 flags = body->lgd_llh_flags;
344 rc = llog_init_handle(loghandle, flags, NULL);
348 rc = req_capsule_server_pack(&req->rq_pill);
350 GOTO(out_close, rc = -ENOMEM);
352 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
353 *hdr = *loghandle->lgh_hdr;
356 rc2 = llog_close(loghandle);
360 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
365 int llog_origin_handle_close(struct ptlrpc_request *req)
372 int llog_origin_handle_cancel(struct ptlrpc_request *req)
374 struct obd_device *obd = req->rq_export->exp_obd;
375 int num_cookies, rc = 0, err, i, failed = 0;
376 struct obd_device *disk_obd;
377 struct llog_cookie *logcookies;
378 struct llog_ctxt *ctxt = NULL;
379 struct lvfs_run_ctxt saved;
380 struct llog_handle *cathandle;
385 logcookies = req_capsule_client_get(&req->rq_pill, &RMF_LOGCOOKIES);
386 num_cookies = req_capsule_get_size(&req->rq_pill, &RMF_LOGCOOKIES,
387 RCL_CLIENT) / sizeof(*logcookies);
388 if (logcookies == NULL || num_cookies == 0) {
389 DEBUG_REQ(D_HA, req, "No llog cookies sent");
393 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
397 disk_obd = ctxt->loc_exp->exp_obd;
398 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
399 for (i = 0; i < num_cookies; i++, logcookies++) {
400 cathandle = ctxt->loc_handle;
401 LASSERT(cathandle != NULL);
402 inode = cathandle->lgh_file->f_dentry->d_inode;
404 handle = fsfilt_start_log(disk_obd, inode,
405 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
406 if (IS_ERR(handle)) {
407 CERROR("fsfilt_start_log() failed: %ld\n",
409 GOTO(pop_ctxt, rc = PTR_ERR(handle));
412 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
415 * Do not raise -ENOENT errors for resent rpcs. This rec already
419 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)) {
421 * Do not change this message, reply-single.sh test_59b
422 * expects to find this in log.
424 CDEBUG(D_RPCTRACE, "RESENT cancel req %p - ignored\n",
427 } else if (rc == 0) {
428 CDEBUG(D_RPCTRACE, "Canceled %d llog-records\n",
432 err = fsfilt_commit(disk_obd, inode, handle, 0);
434 CERROR("Error committing transaction: %d\n", err);
444 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
446 CERROR("Cancel %d of %d llog-records failed: %d\n",
447 failed, num_cookies, rc);
452 EXPORT_SYMBOL(llog_origin_handle_cancel);
454 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
457 struct mds_obd *mds = &obd->u.mds;
458 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
459 struct lvfs_run_ctxt saved;
460 struct llog_handle *handle = NULL;
462 int rc, i, l, remains = buf_len;
466 if (ctxt == NULL || mds == NULL)
467 GOTO(release_ctxt, rc = -ENODEV);
469 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
471 sprintf(name[0], "%s", mds->mds_profile);
472 sprintf(name[1], "%s-clean", mds->mds_profile);
473 sprintf(name[2], "%s", client);
474 sprintf(name[3], "%s-clean", client);
476 for (i = 0; i < 4; i++) {
477 int index, uncanceled = 0;
478 rc = llog_create(ctxt, &handle, NULL, name[i]);
481 rc = llog_init_handle(handle, 0, NULL);
484 GOTO(out_pop, rc = -ENOENT);
487 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
488 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
492 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
493 "Last Index: %d\nUncanceled Records: %d\n\n",
495 i_size_read(handle->lgh_file->f_dentry->d_inode),
496 handle->lgh_last_idx, uncanceled);
506 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
513 struct llog_ctxt *ctxt;
519 static int llog_catinfo_cb(struct llog_handle *cat,
520 struct llog_rec_hdr *rec, void *data)
522 static char *out = NULL;
523 static int remains = 0;
524 struct llog_ctxt *ctxt = NULL;
525 struct llog_handle *handle = NULL;
526 struct llog_logid *logid;
527 struct llog_logid_rec *lir;
528 int l, rc, index, count = 0;
529 struct cb_data *cbd = (struct cb_data*)data;
534 remains = cbd->remains;
538 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
544 lir = (struct llog_logid_rec *)rec;
545 logid = &lir->lid_id;
546 rc = llog_create(ctxt, &handle, logid, NULL);
549 rc = llog_init_handle(handle, 0, NULL);
553 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
554 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
558 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
559 "\tLog Size: %llu\n\tLast Index: %d\n"
560 "\tUncanceled Records: %d\n",
561 logid->lgl_oid, logid->lgl_oseq, logid->lgl_ogen,
562 i_size_read(handle->lgh_file->f_dentry->d_inode),
563 handle->lgh_last_idx, count);
567 cbd->remains = remains;
569 CWARN("Not enough memory\n");
578 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
581 struct mds_obd *mds = &obd->u.mds;
582 struct llog_handle *handle;
583 struct lvfs_run_ctxt saved;
585 struct llog_catid *idarray;
586 char name[32] = CATLIST;
588 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
592 if (ctxt == NULL || mds == NULL)
593 GOTO(release_ctxt, rc = -ENODEV);
595 count = mds->mds_lov_desc.ld_tgt_count;
596 size = sizeof(*idarray) * count;
598 OBD_ALLOC_LARGE(idarray, size);
600 GOTO(release_ctxt, rc = -ENOMEM);
602 cfs_mutex_lock(&obd->obd_olg.olg_cat_processing);
603 rc = llog_get_cat_list(obd, name, 0, count, idarray);
607 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
611 data.remains = buf_len;
612 for (i = 0; i < count; i++) {
613 int l, index, uncanceled = 0;
615 rc = llog_create(ctxt, &handle, &idarray[i].lci_logid, NULL);
618 rc = llog_init_handle(handle, 0, NULL);
621 GOTO(out_pop, rc = -ENOENT);
623 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
624 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
627 l = snprintf(data.out, data.remains,
628 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
630 idarray[i].lci_logid.lgl_oid,
631 idarray[i].lci_logid.lgl_oseq,
632 idarray[i].lci_logid.lgl_ogen, uncanceled);
638 llog_process(handle, llog_catinfo_cb, &data, NULL);
641 if (data.remains <= 0)
646 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
648 cfs_mutex_unlock(&obd->obd_olg.olg_cat_processing);
649 OBD_FREE_LARGE(idarray, size);
655 int llog_catinfo(struct ptlrpc_request *req)
657 struct obd_export *exp = req->rq_export;
658 struct obd_device *obd = exp->exp_obd;
664 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
668 memset(buf, 0, LLOG_CHUNK_SIZE);
670 keyword = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
673 if (strcmp(keyword, "config") == 0) {
674 char *client = req_capsule_client_get(&req->rq_pill,
678 rc = llog_catinfo_config(obd, buf, LLOG_CHUNK_SIZE, client);
679 } else if (strcmp(keyword, "deletions") == 0) {
680 rc = llog_catinfo_deletions(obd, buf, LLOG_CHUNK_SIZE);
685 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
687 rc = req_capsule_server_pack(&req->rq_pill);
689 GOTO(out_free, rc = -ENOMEM);
691 reply = req_capsule_server_get(&req->rq_pill, &RMF_STRING);
692 if (strlen(buf) == 0)
693 sprintf(buf, "%s", "No log informations\n");
694 memcpy(reply, buf, LLOG_CHUNK_SIZE);
697 OBD_FREE(buf, LLOG_CHUNK_SIZE);
701 #else /* !__KERNEL__ */
702 int llog_origin_handle_create(struct ptlrpc_request *req)
708 int llog_origin_handle_destroy(struct ptlrpc_request *req)
714 int llog_origin_handle_next_block(struct ptlrpc_request *req)
719 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
724 int llog_origin_handle_read_header(struct ptlrpc_request *req)
729 int llog_origin_handle_close(struct ptlrpc_request *req)
734 int llog_origin_handle_cancel(struct ptlrpc_request *req)