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
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;
133 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
137 if (body->lgd_logid.lgl_oid > 0)
138 logid = &body->lgd_logid;
140 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
144 disk_obd = ctxt->loc_exp->exp_obd;
145 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
147 rc = llog_create(ctxt, &loghandle, logid, NULL);
151 rc = req_capsule_server_pack(&req->rq_pill);
153 GOTO(out_close, rc = -ENOMEM);
155 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
156 body->lgd_logid = loghandle->lgh_id;
157 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
160 rc = llog_destroy(loghandle);
163 llog_free_handle(loghandle);
167 llog_close(loghandle);
169 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
174 int llog_origin_handle_next_block(struct ptlrpc_request *req)
176 struct obd_export *exp = req->rq_export;
177 struct obd_device *obd = exp->exp_obd;
178 struct obd_device *disk_obd;
179 struct llog_handle *loghandle;
180 struct llogd_body *body;
181 struct llogd_body *repbody;
182 struct lvfs_run_ctxt saved;
183 struct llog_ctxt *ctxt;
190 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
194 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
198 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
200 GOTO(out_free, rc = -ENODEV);
201 disk_obd = ctxt->loc_exp->exp_obd;
202 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
204 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
208 flags = body->lgd_llh_flags;
209 rc = llog_init_handle(loghandle, flags, NULL);
213 memset(buf, 0, LLOG_CHUNK_SIZE);
214 rc = llog_next_block(loghandle, &body->lgd_saved_index,
216 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
220 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
222 rc = req_capsule_server_pack(&req->rq_pill);
224 GOTO(out_close, rc = -ENOMEM);
226 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
229 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
230 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
233 rc2 = llog_close(loghandle);
237 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
240 OBD_FREE(buf, LLOG_CHUNK_SIZE);
244 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
246 struct obd_export *exp = req->rq_export;
247 struct obd_device *obd = exp->exp_obd;
248 struct llog_handle *loghandle;
249 struct llogd_body *body;
250 struct llogd_body *repbody;
251 struct obd_device *disk_obd;
252 struct lvfs_run_ctxt saved;
253 struct llog_ctxt *ctxt;
260 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
264 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
268 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
270 GOTO(out_free, rc = -ENODEV);
272 disk_obd = ctxt->loc_exp->exp_obd;
273 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
275 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
279 flags = body->lgd_llh_flags;
280 rc = llog_init_handle(loghandle, flags, NULL);
284 memset(buf, 0, LLOG_CHUNK_SIZE);
285 rc = llog_prev_block(loghandle, body->lgd_index,
286 buf, LLOG_CHUNK_SIZE);
290 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
292 rc = req_capsule_server_pack(&req->rq_pill);
294 GOTO(out_close, rc = -ENOMEM);
296 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
299 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
300 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
303 rc2 = llog_close(loghandle);
308 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
311 OBD_FREE(buf, LLOG_CHUNK_SIZE);
315 int llog_origin_handle_read_header(struct ptlrpc_request *req)
317 struct obd_export *exp = req->rq_export;
318 struct obd_device *obd = exp->exp_obd;
319 struct obd_device *disk_obd;
320 struct llog_handle *loghandle;
321 struct llogd_body *body;
322 struct llog_log_hdr *hdr;
323 struct lvfs_run_ctxt saved;
324 struct llog_ctxt *ctxt;
329 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
333 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
337 disk_obd = ctxt->loc_exp->exp_obd;
338 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
340 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
345 * llog_init_handle() reads the llog header
347 flags = body->lgd_llh_flags;
348 rc = llog_init_handle(loghandle, flags, NULL);
352 rc = req_capsule_server_pack(&req->rq_pill);
354 GOTO(out_close, rc = -ENOMEM);
356 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
357 *hdr = *loghandle->lgh_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)
376 int llog_origin_handle_cancel(struct ptlrpc_request *req)
378 struct obd_device *obd = req->rq_export->exp_obd;
379 int num_cookies, rc = 0, err, i, failed = 0;
380 struct obd_device *disk_obd;
381 struct llog_cookie *logcookies;
382 struct llog_ctxt *ctxt = NULL;
383 struct lvfs_run_ctxt saved;
384 struct llog_handle *cathandle;
389 logcookies = req_capsule_client_get(&req->rq_pill, &RMF_LOGCOOKIES);
390 num_cookies = req_capsule_get_size(&req->rq_pill, &RMF_LOGCOOKIES,
391 RCL_CLIENT) / sizeof(*logcookies);
392 if (logcookies == NULL || num_cookies == 0) {
393 DEBUG_REQ(D_HA, req, "No llog cookies sent");
397 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
401 disk_obd = ctxt->loc_exp->exp_obd;
402 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
403 for (i = 0; i < num_cookies; i++, logcookies++) {
404 cathandle = ctxt->loc_handle;
405 LASSERT(cathandle != NULL);
406 inode = cathandle->lgh_file->f_dentry->d_inode;
408 handle = fsfilt_start_log(disk_obd, inode,
409 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
410 if (IS_ERR(handle)) {
411 CERROR("fsfilt_start_log() failed: %ld\n",
413 GOTO(pop_ctxt, rc = PTR_ERR(handle));
416 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
419 * Do not raise -ENOENT errors for resent rpcs. This rec already
423 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)) {
425 * Do not change this message, reply-single.sh test_59b
426 * expects to find this in log.
428 CDEBUG(D_RPCTRACE, "RESENT cancel req %p - ignored\n",
431 } else if (rc == 0) {
432 CDEBUG(D_RPCTRACE, "Canceled %d llog-records\n",
436 err = fsfilt_commit(disk_obd, inode, handle, 0);
438 CERROR("Error committing transaction: %d\n", err);
448 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
450 CERROR("Cancel %d of %d llog-records failed: %d\n",
451 failed, num_cookies, rc);
456 EXPORT_SYMBOL(llog_origin_handle_cancel);
458 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
461 struct mds_obd *mds = &obd->u.mds;
462 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
463 struct lvfs_run_ctxt saved;
464 struct llog_handle *handle = NULL;
466 int rc, i, l, remains = buf_len;
470 if (ctxt == NULL || mds == NULL)
471 GOTO(release_ctxt, rc = -ENODEV);
473 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
475 sprintf(name[0], "%s", mds->mds_profile);
476 sprintf(name[1], "%s-clean", mds->mds_profile);
477 sprintf(name[2], "%s", client);
478 sprintf(name[3], "%s-clean", client);
480 for (i = 0; i < 4; i++) {
481 int index, uncanceled = 0;
482 rc = llog_create(ctxt, &handle, NULL, name[i]);
485 rc = llog_init_handle(handle, 0, NULL);
488 GOTO(out_pop, rc = -ENOENT);
491 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
492 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
496 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
497 "Last Index: %d\nUncanceled Records: %d\n\n",
499 i_size_read(handle->lgh_file->f_dentry->d_inode),
500 handle->lgh_last_idx, uncanceled);
510 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
517 struct llog_ctxt *ctxt;
523 static int llog_catinfo_cb(struct llog_handle *cat,
524 struct llog_rec_hdr *rec, void *data)
526 static char *out = NULL;
527 static int remains = 0;
528 struct llog_ctxt *ctxt = NULL;
529 struct llog_handle *handle = NULL;
530 struct llog_logid *logid;
531 struct llog_logid_rec *lir;
532 int l, rc, index, count = 0;
533 struct cb_data *cbd = (struct cb_data*)data;
538 remains = cbd->remains;
542 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
548 lir = (struct llog_logid_rec *)rec;
549 logid = &lir->lid_id;
550 rc = llog_create(ctxt, &handle, logid, NULL);
553 rc = llog_init_handle(handle, 0, NULL);
557 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
558 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
562 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
563 "\tLog Size: %llu\n\tLast Index: %d\n"
564 "\tUncanceled Records: %d\n",
565 logid->lgl_oid, logid->lgl_oseq, logid->lgl_ogen,
566 i_size_read(handle->lgh_file->f_dentry->d_inode),
567 handle->lgh_last_idx, count);
571 cbd->remains = remains;
573 CWARN("Not enough memory\n");
582 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
585 struct mds_obd *mds = &obd->u.mds;
586 struct llog_handle *handle;
587 struct lvfs_run_ctxt saved;
589 struct llog_catid *idarray;
590 char name[32] = CATLIST;
592 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
596 if (ctxt == NULL || mds == NULL)
597 GOTO(release_ctxt, rc = -ENODEV);
599 count = mds->mds_lov_desc.ld_tgt_count;
600 size = sizeof(*idarray) * count;
602 OBD_ALLOC_LARGE(idarray, size);
604 GOTO(release_ctxt, rc = -ENOMEM);
606 cfs_mutex_lock(&obd->obd_olg.olg_cat_processing);
607 rc = llog_get_cat_list(obd, name, 0, count, idarray);
611 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
615 data.remains = buf_len;
616 for (i = 0; i < count; i++) {
617 int l, index, uncanceled = 0;
619 rc = llog_create(ctxt, &handle, &idarray[i].lci_logid, NULL);
622 rc = llog_init_handle(handle, 0, NULL);
625 GOTO(out_pop, rc = -ENOENT);
627 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
628 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
631 l = snprintf(data.out, data.remains,
632 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
634 idarray[i].lci_logid.lgl_oid,
635 idarray[i].lci_logid.lgl_oseq,
636 idarray[i].lci_logid.lgl_ogen, uncanceled);
642 llog_process(handle, llog_catinfo_cb, &data, NULL);
645 if (data.remains <= 0)
650 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
652 cfs_mutex_unlock(&obd->obd_olg.olg_cat_processing);
653 OBD_FREE_LARGE(idarray, size);
659 int llog_catinfo(struct ptlrpc_request *req)
661 struct obd_export *exp = req->rq_export;
662 struct obd_device *obd = exp->exp_obd;
668 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
672 memset(buf, 0, LLOG_CHUNK_SIZE);
674 keyword = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
677 if (strcmp(keyword, "config") == 0) {
678 char *client = req_capsule_client_get(&req->rq_pill,
682 rc = llog_catinfo_config(obd, buf, LLOG_CHUNK_SIZE, client);
683 } else if (strcmp(keyword, "deletions") == 0) {
684 rc = llog_catinfo_deletions(obd, buf, LLOG_CHUNK_SIZE);
689 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
691 rc = req_capsule_server_pack(&req->rq_pill);
693 GOTO(out_free, rc = -ENOMEM);
695 reply = req_capsule_server_get(&req->rq_pill, &RMF_STRING);
696 if (strlen(buf) == 0)
697 sprintf(buf, "%s", "No log informations\n");
698 memcpy(reply, buf, LLOG_CHUNK_SIZE);
701 OBD_FREE(buf, LLOG_CHUNK_SIZE);
705 #else /* !__KERNEL__ */
706 int llog_origin_handle_create(struct ptlrpc_request *req)
712 int llog_origin_handle_destroy(struct ptlrpc_request *req)
718 int llog_origin_handle_next_block(struct ptlrpc_request *req)
723 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
728 int llog_origin_handle_read_header(struct ptlrpc_request *req)
733 int llog_origin_handle_close(struct ptlrpc_request *req)
738 int llog_origin_handle_cancel(struct ptlrpc_request *req)