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;
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;
130 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
134 if (body->lgd_logid.lgl_oid > 0)
135 logid = &body->lgd_logid;
137 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
141 disk_obd = ctxt->loc_exp->exp_obd;
142 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
144 rc = llog_create(ctxt, &loghandle, logid, NULL);
148 rc = req_capsule_server_pack(&req->rq_pill);
150 GOTO(out_close, rc = -ENOMEM);
152 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
153 body->lgd_logid = loghandle->lgh_id;
154 flags = body->lgd_llh_flags;
155 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
158 rc = llog_destroy(loghandle);
161 llog_free_handle(loghandle);
165 llog_close(loghandle);
167 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
172 int llog_origin_handle_next_block(struct ptlrpc_request *req)
174 struct obd_export *exp = req->rq_export;
175 struct obd_device *obd = exp->exp_obd;
176 struct obd_device *disk_obd;
177 struct llog_handle *loghandle;
178 struct llogd_body *body;
179 struct llogd_body *repbody;
180 struct lvfs_run_ctxt saved;
181 struct llog_ctxt *ctxt;
188 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
192 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
196 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
198 GOTO(out_free, rc = -EINVAL);
199 disk_obd = ctxt->loc_exp->exp_obd;
200 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
202 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
206 flags = body->lgd_llh_flags;
207 rc = llog_init_handle(loghandle, flags, NULL);
211 memset(buf, 0, LLOG_CHUNK_SIZE);
212 rc = llog_next_block(loghandle, &body->lgd_saved_index,
214 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
218 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
220 rc = req_capsule_server_pack(&req->rq_pill);
222 GOTO(out_close, rc = -ENOMEM);
224 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
227 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
228 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
231 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);
268 LASSERT(ctxt != NULL);
269 disk_obd = ctxt->loc_exp->exp_obd;
270 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
272 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
276 flags = body->lgd_llh_flags;
277 rc = llog_init_handle(loghandle, flags, NULL);
281 memset(buf, 0, LLOG_CHUNK_SIZE);
282 rc = llog_prev_block(loghandle, body->lgd_index,
283 buf, LLOG_CHUNK_SIZE);
287 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
289 rc = req_capsule_server_pack(&req->rq_pill);
291 GOTO(out_close, rc = -ENOMEM);
293 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
296 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
297 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
300 rc2 = llog_close(loghandle);
305 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);
332 disk_obd = ctxt->loc_exp->exp_obd;
333 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
335 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
339 /* init_handle reads the header */
340 flags = body->lgd_llh_flags;
341 rc = llog_init_handle(loghandle, flags, NULL);
345 rc = req_capsule_server_pack(&req->rq_pill);
347 GOTO(out_close, rc = -ENOMEM);
349 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
350 *hdr = *loghandle->lgh_hdr;
353 rc2 = llog_close(loghandle);
357 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
362 int llog_origin_handle_close(struct ptlrpc_request *req)
371 int llog_origin_handle_cancel(struct ptlrpc_request *req)
373 struct obd_device *obd = req->rq_export->exp_obd;
374 struct obd_device *disk_obd;
375 struct llog_cookie *logcookies;
376 struct llog_ctxt *ctxt = NULL;
377 int num_cookies, rc = 0, err, i;
378 struct lvfs_run_ctxt saved;
379 struct llog_handle *cathandle;
384 logcookies = req_capsule_client_get(&req->rq_pill, &RMF_LOGCOOKIES);
385 num_cookies = req_capsule_get_size(&req->rq_pill, &RMF_LOGCOOKIES,
386 RCL_CLIENT) / sizeof(*logcookies);
387 if (logcookies == NULL || num_cookies == 0) {
388 DEBUG_REQ(D_HA, req, "no cookies sent");
392 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
394 CWARN("llog subsys not setup or already cleanup\n");
398 disk_obd = ctxt->loc_exp->exp_obd;
399 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
400 for (i = 0; i < num_cookies; i++, logcookies++) {
401 cathandle = ctxt->loc_handle;
402 LASSERT(cathandle != NULL);
403 inode = cathandle->lgh_file->f_dentry->d_inode;
405 handle = fsfilt_start_log(disk_obd, inode,
406 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
407 if (IS_ERR(handle)) {
408 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
409 GOTO(pop_ctxt, rc = PTR_ERR(handle));
412 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
414 err = fsfilt_commit(disk_obd, inode, handle, 0);
416 CERROR("error committing transaction: %d\n", err);
423 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
425 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
427 CDEBUG(D_RPCTRACE, "cancel %d llog-records\n", num_cookies);
432 EXPORT_SYMBOL(llog_origin_handle_cancel);
434 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
437 struct mds_obd *mds = &obd->u.mds;
438 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
439 struct lvfs_run_ctxt saved;
440 struct llog_handle *handle = NULL;
442 int rc, i, l, remains = buf_len;
445 if (ctxt == NULL || mds == NULL)
446 GOTO(release_ctxt, rc = -EOPNOTSUPP);
448 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
450 sprintf(name[0], "%s", mds->mds_profile);
451 sprintf(name[1], "%s-clean", mds->mds_profile);
452 sprintf(name[2], "%s", client);
453 sprintf(name[3], "%s-clean", client);
455 for (i = 0; i < 4; i++) {
456 int index, uncanceled = 0;
457 rc = llog_create(ctxt, &handle, NULL, name[i]);
460 rc = llog_init_handle(handle, 0, NULL);
463 GOTO(out_pop, rc = -ENOENT);
466 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
467 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
471 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
472 "Last Index: %d\nUncanceled Records: %d\n\n",
474 i_size_read(handle->lgh_file->f_dentry->d_inode),
475 handle->lgh_last_idx, uncanceled);
484 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
491 struct llog_ctxt *ctxt;
497 static int llog_catinfo_cb(struct llog_handle *cat,
498 struct llog_rec_hdr *rec, void *data)
500 static char *out = NULL;
501 static int remains = 0;
502 struct llog_ctxt *ctxt = NULL;
503 struct llog_handle *handle;
504 struct llog_logid *logid;
505 struct llog_logid_rec *lir;
506 int l, rc, index, count = 0;
507 struct cb_data *cbd = (struct cb_data*)data;
511 remains = cbd->remains;
515 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
521 lir = (struct llog_logid_rec *)rec;
522 logid = &lir->lid_id;
523 rc = llog_create(ctxt, &handle, logid, NULL);
526 rc = llog_init_handle(handle, 0, NULL);
530 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
531 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
535 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
536 "\tLog Size: %llu\n\tLast Index: %d\n"
537 "\tUncanceled Records: %d\n",
538 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
539 i_size_read(handle->lgh_file->f_dentry->d_inode),
540 handle->lgh_last_idx, count);
544 cbd->remains = remains;
546 CWARN("Not enough memory\n");
555 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
558 struct mds_obd *mds = &obd->u.mds;
559 struct llog_handle *handle;
560 struct lvfs_run_ctxt saved;
562 struct llog_catid *idarray;
563 struct llog_logid *id;
564 char name[32] = CATLIST;
566 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
569 if (ctxt == NULL || mds == NULL)
570 GOTO(release_ctxt, rc = -EOPNOTSUPP);
572 count = mds->mds_lov_desc.ld_tgt_count;
573 size = sizeof(*idarray) * count;
575 OBD_ALLOC(idarray, size);
577 GOTO(release_ctxt, rc = -ENOMEM);
579 rc = llog_get_cat_list(obd, obd, name, count, idarray);
583 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
587 data.remains = buf_len;
588 for (i = 0; i < count; i++) {
589 int l, index, uncanceled = 0;
591 id = &idarray[i].lci_logid;
592 rc = llog_create(ctxt, &handle, id, NULL);
595 rc = llog_init_handle(handle, 0, NULL);
598 GOTO(out_pop, rc = -ENOENT);
600 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
601 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
604 l = snprintf(data.out, data.remains,
605 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
607 id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
614 llog_process(handle, llog_catinfo_cb, &data, NULL);
617 if (data.remains <= 0)
621 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
623 OBD_FREE(idarray, size);
630 int llog_catinfo(struct ptlrpc_request *req)
632 struct obd_export *exp = req->rq_export;
633 struct obd_device *obd = exp->exp_obd;
638 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
641 memset(buf, 0, LLOG_CHUNK_SIZE);
643 keyword = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
646 if (strcmp(keyword, "config") == 0) {
647 char *client = req_capsule_client_get(&req->rq_pill,
651 rc = llog_catinfo_config(obd, buf, LLOG_CHUNK_SIZE, client);
652 } else if (strcmp(keyword, "deletions") == 0) {
653 rc = llog_catinfo_deletions(obd, buf, LLOG_CHUNK_SIZE);
658 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
660 rc = req_capsule_server_pack(&req->rq_pill);
662 GOTO(out_free, rc = -ENOMEM);
664 reply = req_capsule_server_get(&req->rq_pill, &RMF_STRING);
665 if (strlen(buf) == 0)
666 sprintf(buf, "%s", "No log informations\n");
667 memcpy(reply, buf, LLOG_CHUNK_SIZE);
670 OBD_FREE(buf, LLOG_CHUNK_SIZE);
674 #else /* !__KERNEL__ */
675 int llog_origin_handle_create(struct ptlrpc_request *req)
681 int llog_origin_handle_destroy(struct ptlrpc_request *req)
687 int llog_origin_handle_next_block(struct ptlrpc_request *req)
692 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
697 int llog_origin_handle_read_header(struct ptlrpc_request *req)
702 int llog_origin_handle_close(struct ptlrpc_request *req)
707 int llog_origin_handle_cancel(struct ptlrpc_request *req)