1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5 * Author: Andreas Dilger <adilger@clusterfs.com>
7 * This file is part of the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
25 * remote api for llog - server side
29 #define DEBUG_SUBSYSTEM S_LOG
36 #include <liblustre.h>
39 #include <obd_class.h>
40 #include <lustre_log.h>
41 #include <lustre_net.h>
42 #include <libcfs/list.h>
43 #include <lustre_fsfilt.h>
45 #if defined(__KERNEL__) && defined(LUSTRE_LOG_SERVER)
47 int llog_origin_handle_create(struct ptlrpc_request *req)
49 struct obd_export *exp = req->rq_export;
50 struct obd_device *obd = exp->exp_obd;
51 struct obd_device *disk_obd;
52 struct llog_handle *loghandle;
53 struct llogd_body *body;
54 struct lvfs_run_ctxt saved;
55 struct llog_logid *logid = NULL;
56 struct llog_ctxt *ctxt;
61 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
65 if (body->lgd_logid.lgl_oid > 0)
66 logid = &body->lgd_logid;
68 if (req_capsule_field_present(&req->rq_pill, &RMF_NAME, RCL_CLIENT)) {
69 name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
72 CDEBUG(D_INFO, "opening log %s\n", name);
75 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
78 disk_obd = ctxt->loc_exp->exp_obd;
79 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
81 rc = llog_create(ctxt, &loghandle, logid, name);
85 rc = req_capsule_server_pack(&req->rq_pill);
87 GOTO(out_close, rc = -ENOMEM);
89 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
90 body->lgd_logid = loghandle->lgh_id;
93 rc2 = llog_close(loghandle);
97 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
102 int llog_origin_handle_destroy(struct ptlrpc_request *req)
104 struct obd_export *exp = req->rq_export;
105 struct obd_device *obd = exp->exp_obd;
106 struct obd_device *disk_obd;
107 struct llog_handle *loghandle;
108 struct llogd_body *body;
109 struct lvfs_run_ctxt saved;
110 struct llog_logid *logid = NULL;
111 struct llog_ctxt *ctxt;
116 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
120 if (body->lgd_logid.lgl_oid > 0)
121 logid = &body->lgd_logid;
123 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
127 disk_obd = ctxt->loc_exp->exp_obd;
128 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
130 rc = llog_create(ctxt, &loghandle, logid, NULL);
134 rc = req_capsule_server_pack(&req->rq_pill);
136 GOTO(out_close, rc = -ENOMEM);
138 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
139 body->lgd_logid = loghandle->lgh_id;
140 flags = body->lgd_llh_flags;
141 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
144 rc = llog_destroy(loghandle);
147 llog_free_handle(loghandle);
151 llog_close(loghandle);
153 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
158 int llog_origin_handle_next_block(struct ptlrpc_request *req)
160 struct obd_export *exp = req->rq_export;
161 struct obd_device *obd = exp->exp_obd;
162 struct obd_device *disk_obd;
163 struct llog_handle *loghandle;
164 struct llogd_body *body;
165 struct llogd_body *repbody;
166 struct lvfs_run_ctxt saved;
167 struct llog_ctxt *ctxt;
174 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
178 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
182 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
184 GOTO(out_free, rc = -EINVAL);
185 disk_obd = ctxt->loc_exp->exp_obd;
186 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
188 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
192 flags = body->lgd_llh_flags;
193 rc = llog_init_handle(loghandle, flags, NULL);
197 memset(buf, 0, LLOG_CHUNK_SIZE);
198 rc = llog_next_block(loghandle, &body->lgd_saved_index,
200 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
204 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
206 rc = req_capsule_server_pack(&req->rq_pill);
208 GOTO(out_close, rc = -ENOMEM);
210 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
213 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
214 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
217 rc2 = llog_close(loghandle);
222 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
225 OBD_FREE(buf, LLOG_CHUNK_SIZE);
229 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
231 struct obd_export *exp = req->rq_export;
232 struct obd_device *obd = exp->exp_obd;
233 struct llog_handle *loghandle;
234 struct llogd_body *body;
235 struct llogd_body *repbody;
236 struct obd_device *disk_obd;
237 struct lvfs_run_ctxt saved;
238 struct llog_ctxt *ctxt;
245 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
249 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
253 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
254 LASSERT(ctxt != NULL);
255 disk_obd = ctxt->loc_exp->exp_obd;
256 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
258 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
262 flags = body->lgd_llh_flags;
263 rc = llog_init_handle(loghandle, flags, NULL);
267 memset(buf, 0, LLOG_CHUNK_SIZE);
268 rc = llog_prev_block(loghandle, body->lgd_index,
269 buf, LLOG_CHUNK_SIZE);
273 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
275 rc = req_capsule_server_pack(&req->rq_pill);
277 GOTO(out_close, rc = -ENOMEM);
279 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
282 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
283 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
286 rc2 = llog_close(loghandle);
291 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
293 OBD_FREE(buf, LLOG_CHUNK_SIZE);
297 int llog_origin_handle_read_header(struct ptlrpc_request *req)
299 struct obd_export *exp = req->rq_export;
300 struct obd_device *obd = exp->exp_obd;
301 struct obd_device *disk_obd;
302 struct llog_handle *loghandle;
303 struct llogd_body *body;
304 struct llog_log_hdr *hdr;
305 struct lvfs_run_ctxt saved;
306 struct llog_ctxt *ctxt;
311 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
315 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
318 disk_obd = ctxt->loc_exp->exp_obd;
319 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
321 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
325 /* init_handle reads the header */
326 flags = body->lgd_llh_flags;
327 rc = llog_init_handle(loghandle, flags, NULL);
331 rc = req_capsule_server_pack(&req->rq_pill);
333 GOTO(out_close, rc = -ENOMEM);
335 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
336 *hdr = *loghandle->lgh_hdr;
339 rc2 = llog_close(loghandle);
343 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
348 int llog_origin_handle_close(struct ptlrpc_request *req)
357 int llog_origin_handle_cancel(struct ptlrpc_request *req)
359 struct obd_device *obd = req->rq_export->exp_obd;
360 struct obd_device *disk_obd;
361 struct llog_cookie *logcookies;
362 struct llog_ctxt *ctxt = NULL;
363 int num_cookies, rc = 0, err, i;
364 struct lvfs_run_ctxt saved;
365 struct llog_handle *cathandle;
370 logcookies = req_capsule_client_get(&req->rq_pill, &RMF_LOGCOOKIES);
371 num_cookies = req_capsule_get_size(&req->rq_pill, &RMF_LOGCOOKIES,
372 RCL_CLIENT) / sizeof(*logcookies);
373 if (logcookies == NULL || num_cookies == 0) {
374 DEBUG_REQ(D_HA, req, "no cookies sent");
378 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
380 CWARN("llog subsys not setup or already cleanup\n");
384 disk_obd = ctxt->loc_exp->exp_obd;
385 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
386 for (i = 0; i < num_cookies; i++, logcookies++) {
387 cathandle = ctxt->loc_handle;
388 LASSERT(cathandle != NULL);
389 inode = cathandle->lgh_file->f_dentry->d_inode;
391 handle = fsfilt_start_log(disk_obd, inode,
392 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
393 if (IS_ERR(handle)) {
394 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
395 GOTO(pop_ctxt, rc = PTR_ERR(handle));
398 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
400 err = fsfilt_commit(disk_obd, inode, handle, 0);
402 CERROR("error committing transaction: %d\n", err);
409 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
411 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
413 CDEBUG(D_RPCTRACE, "cancel %d llog-records\n", num_cookies);
418 EXPORT_SYMBOL(llog_origin_handle_cancel);
420 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
423 struct mds_obd *mds = &obd->u.mds;
424 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
425 struct lvfs_run_ctxt saved;
426 struct llog_handle *handle = NULL;
428 int rc, i, l, remains = buf_len;
431 if (ctxt == NULL || mds == NULL)
432 GOTO(release_ctxt, rc = -EOPNOTSUPP);
434 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
436 sprintf(name[0], "%s", mds->mds_profile);
437 sprintf(name[1], "%s-clean", mds->mds_profile);
438 sprintf(name[2], "%s", client);
439 sprintf(name[3], "%s-clean", client);
441 for (i = 0; i < 4; i++) {
442 int index, uncanceled = 0;
443 rc = llog_create(ctxt, &handle, NULL, name[i]);
446 rc = llog_init_handle(handle, 0, NULL);
449 GOTO(out_pop, rc = -ENOENT);
452 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
453 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
457 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
458 "Last Index: %d\nUncanceled Records: %d\n\n",
460 i_size_read(handle->lgh_file->f_dentry->d_inode),
461 handle->lgh_last_idx, uncanceled);
470 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
477 struct llog_ctxt *ctxt;
483 static int llog_catinfo_cb(struct llog_handle *cat,
484 struct llog_rec_hdr *rec, void *data)
486 static char *out = NULL;
487 static int remains = 0;
488 struct llog_ctxt *ctxt = NULL;
489 struct llog_handle *handle;
490 struct llog_logid *logid;
491 struct llog_logid_rec *lir;
492 int l, rc, index, count = 0;
493 struct cb_data *cbd = (struct cb_data*)data;
497 remains = cbd->remains;
501 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
507 lir = (struct llog_logid_rec *)rec;
508 logid = &lir->lid_id;
509 rc = llog_create(ctxt, &handle, logid, NULL);
512 rc = llog_init_handle(handle, 0, NULL);
516 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
517 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
521 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
522 "\tLog Size: %llu\n\tLast Index: %d\n"
523 "\tUncanceled Records: %d\n",
524 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
525 i_size_read(handle->lgh_file->f_dentry->d_inode),
526 handle->lgh_last_idx, count);
530 cbd->remains = remains;
532 CWARN("Not enough memory\n");
541 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
544 struct mds_obd *mds = &obd->u.mds;
545 struct llog_handle *handle;
546 struct lvfs_run_ctxt saved;
548 struct llog_catid *idarray;
549 struct llog_logid *id;
550 char name[32] = CATLIST;
552 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
555 if (ctxt == NULL || mds == NULL)
556 GOTO(release_ctxt, rc = -EOPNOTSUPP);
558 count = mds->mds_lov_desc.ld_tgt_count;
559 size = sizeof(*idarray) * count;
561 OBD_ALLOC(idarray, size);
563 GOTO(release_ctxt, rc = -ENOMEM);
565 rc = llog_get_cat_list(obd, obd, name, count, idarray);
569 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
573 data.remains = buf_len;
574 for (i = 0; i < count; i++) {
575 int l, index, uncanceled = 0;
577 id = &idarray[i].lci_logid;
578 rc = llog_create(ctxt, &handle, id, NULL);
581 rc = llog_init_handle(handle, 0, NULL);
584 GOTO(out_pop, rc = -ENOENT);
586 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
587 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
590 l = snprintf(data.out, data.remains,
591 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
593 id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
600 llog_process(handle, llog_catinfo_cb, &data, NULL);
603 if (data.remains <= 0)
607 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
609 OBD_FREE(idarray, size);
616 int llog_catinfo(struct ptlrpc_request *req)
618 struct obd_export *exp = req->rq_export;
619 struct obd_device *obd = exp->exp_obd;
624 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
627 memset(buf, 0, LLOG_CHUNK_SIZE);
629 keyword = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
632 if (strcmp(keyword, "config") == 0) {
633 char *client = req_capsule_client_get(&req->rq_pill,
637 rc = llog_catinfo_config(obd, buf, LLOG_CHUNK_SIZE, client);
638 } else if (strcmp(keyword, "deletions") == 0) {
639 rc = llog_catinfo_deletions(obd, buf, LLOG_CHUNK_SIZE);
644 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
646 rc = req_capsule_server_pack(&req->rq_pill);
648 GOTO(out_free, rc = -ENOMEM);
650 reply = req_capsule_server_get(&req->rq_pill, &RMF_STRING);
651 if (strlen(buf) == 0)
652 sprintf(buf, "%s", "No log informations\n");
653 memcpy(reply, buf, LLOG_CHUNK_SIZE);
656 OBD_FREE(buf, LLOG_CHUNK_SIZE);
660 #else /* !__KERNEL__ */
661 int llog_origin_handle_create(struct ptlrpc_request *req)
667 int llog_origin_handle_destroy(struct ptlrpc_request *req)
673 int llog_origin_handle_next_block(struct ptlrpc_request *req)
678 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
683 int llog_origin_handle_read_header(struct ptlrpc_request *req)
688 int llog_origin_handle_close(struct ptlrpc_request *req)
693 int llog_origin_handle_cancel(struct ptlrpc_request *req)