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;
58 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
62 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
63 lustre_swab_llogd_body);
65 CERROR ("Can't unpack llogd_body\n");
66 GOTO(out, rc =-EFAULT);
69 if (body->lgd_logid.lgl_oid > 0)
70 logid = &body->lgd_logid;
72 if (lustre_msg_bufcount(req->rq_reqmsg) > 2) {
73 name = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
75 CERROR("Can't unpack name\n");
76 GOTO(out, rc = -EFAULT);
78 CDEBUG(D_INFO, "opening log %s\n", name);
81 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
83 GOTO(out, rc = -EINVAL);
84 disk_obd = ctxt->loc_exp->exp_obd;
85 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
87 rc = llog_create(ctxt, &loghandle, logid, name);
91 rc = lustre_pack_reply(req, 2, size, NULL);
93 GOTO(out_close, rc = -ENOMEM);
95 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
96 body->lgd_logid = loghandle->lgh_id;
99 rc2 = llog_close(loghandle);
103 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
108 int llog_origin_handle_destroy(struct ptlrpc_request *req)
110 struct obd_export *exp = req->rq_export;
111 struct obd_device *obd = exp->exp_obd;
112 struct obd_device *disk_obd;
113 struct llog_handle *loghandle;
114 struct llogd_body *body;
115 struct lvfs_run_ctxt saved;
116 struct llog_logid *logid = NULL;
117 struct llog_ctxt *ctxt;
118 int size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
123 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
124 lustre_swab_llogd_body);
126 CERROR ("Can't unpack llogd_body\n");
127 GOTO(out, rc =-EFAULT);
130 if (body->lgd_logid.lgl_oid > 0)
131 logid = &body->lgd_logid;
133 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
135 GOTO(out, rc = -EINVAL);
136 disk_obd = ctxt->loc_exp->exp_obd;
137 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
139 rc = llog_create(ctxt, &loghandle, logid, NULL);
143 rc = lustre_pack_reply(req, 2, size, NULL);
145 GOTO(out_close, rc = -ENOMEM);
147 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof (*body));
148 body->lgd_logid = loghandle->lgh_id;
149 flags = body->lgd_llh_flags;
150 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
153 rc = llog_destroy(loghandle);
156 llog_free_handle(loghandle);
160 llog_close(loghandle);
162 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
167 int llog_origin_handle_next_block(struct ptlrpc_request *req)
169 struct obd_export *exp = req->rq_export;
170 struct obd_device *obd = exp->exp_obd;
171 struct obd_device *disk_obd;
172 struct llog_handle *loghandle;
173 struct llogd_body *body;
174 struct lvfs_run_ctxt saved;
175 struct llog_ctxt *ctxt;
179 int size[3] = { sizeof(struct ptlrpc_body),
185 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
186 lustre_swab_llogd_body);
188 CERROR ("Can't unpack llogd_body\n");
189 GOTO(out, rc =-EFAULT);
192 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
194 GOTO(out, rc = -ENOMEM);
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);
219 rc = lustre_pack_reply(req, 3, size, NULL);
221 GOTO(out_close, rc = -ENOMEM);
223 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof (body));
224 memcpy(ptr, body, sizeof(*body));
226 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, LLOG_CHUNK_SIZE);
227 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
230 rc2 = llog_close(loghandle);
235 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
237 OBD_FREE(buf, LLOG_CHUNK_SIZE);
242 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
244 struct obd_export *exp = req->rq_export;
245 struct obd_device *obd = exp->exp_obd;
246 struct llog_handle *loghandle;
247 struct llogd_body *body;
248 struct obd_device *disk_obd;
249 struct lvfs_run_ctxt saved;
250 struct llog_ctxt *ctxt;
254 int size[] = { sizeof(struct ptlrpc_body),
260 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
261 lustre_swab_llogd_body);
263 CERROR ("Can't unpack llogd_body\n");
264 GOTO(out, rc =-EFAULT);
267 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
269 GOTO(out, rc = -ENOMEM);
271 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
272 LASSERT(ctxt != NULL);
273 disk_obd = ctxt->loc_exp->exp_obd;
274 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
276 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
280 flags = body->lgd_llh_flags;
281 rc = llog_init_handle(loghandle, flags, NULL);
285 memset(buf, 0, LLOG_CHUNK_SIZE);
286 rc = llog_prev_block(loghandle, body->lgd_index,
287 buf, LLOG_CHUNK_SIZE);
292 rc = lustre_pack_reply(req, 3, size, NULL);
294 GOTO(out_close, rc = -ENOMEM);
296 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(body));
297 memcpy(ptr, body, sizeof(*body));
299 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF+1, LLOG_CHUNK_SIZE);
300 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
303 rc2 = llog_close(loghandle);
308 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
309 OBD_FREE(buf, LLOG_CHUNK_SIZE);
314 int llog_origin_handle_read_header(struct ptlrpc_request *req)
316 struct obd_export *exp = req->rq_export;
317 struct obd_device *obd = exp->exp_obd;
318 struct obd_device *disk_obd;
319 struct llog_handle *loghandle;
320 struct llogd_body *body;
321 struct llog_log_hdr *hdr;
322 struct lvfs_run_ctxt saved;
323 struct llog_ctxt *ctxt;
325 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*hdr) };
329 body = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*body),
330 lustre_swab_llogd_body);
332 CERROR ("Can't unpack llogd_body\n");
333 GOTO(out, rc =-EFAULT);
336 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
338 GOTO(out, rc = -EINVAL);
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);
346 /* init_handle reads the header */
347 flags = body->lgd_llh_flags;
348 rc = llog_init_handle(loghandle, flags, NULL);
352 rc = lustre_pack_reply(req, 2, size, NULL);
354 GOTO(out_close, rc = -ENOMEM);
356 hdr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*hdr));
357 memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
360 rc2 = llog_close(loghandle);
365 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
371 int llog_origin_handle_close(struct ptlrpc_request *req)
380 int llog_origin_handle_cancel(struct ptlrpc_request *req)
382 struct obd_device *obd = req->rq_export->exp_obd;
383 struct obd_device *disk_obd;
384 struct llog_cookie *logcookies;
385 struct llog_ctxt *ctxt;
386 int num_cookies, rc = 0, err, i;
387 struct lvfs_run_ctxt saved;
388 struct llog_handle *cathandle;
393 logcookies = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
394 sizeof(*logcookies));
395 num_cookies = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF) /
397 if (logcookies == NULL || num_cookies == 0) {
398 DEBUG_REQ(D_HA, req, "no cookies sent");
402 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
404 CWARN("llog subsys not setup or already cleanup\n");
408 disk_obd = ctxt->loc_exp->exp_obd;
409 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
410 for (i = 0; i < num_cookies; i++, logcookies++) {
411 cathandle = ctxt->loc_handle;
412 LASSERT(cathandle != NULL);
413 inode = cathandle->lgh_file->f_dentry->d_inode;
415 handle = fsfilt_start_log(disk_obd, inode,
416 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
417 if (IS_ERR(handle)) {
418 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
419 GOTO(pop_ctxt, rc = PTR_ERR(handle));
422 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
424 err = fsfilt_commit(disk_obd, inode, handle, 0);
426 CERROR("error committing transaction: %d\n", err);
433 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
435 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
437 CDEBUG(D_HA, "cancel %d llog-records\n", num_cookies);
441 EXPORT_SYMBOL(llog_origin_handle_cancel);
443 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
446 struct mds_obd *mds = &obd->u.mds;
447 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
448 struct lvfs_run_ctxt saved;
449 struct llog_handle *handle = NULL;
451 int rc, i, l, remains = buf_len;
454 if (ctxt == NULL || mds == NULL)
457 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
459 sprintf(name[0], "%s", mds->mds_profile);
460 sprintf(name[1], "%s-clean", mds->mds_profile);
461 sprintf(name[2], "%s", client);
462 sprintf(name[3], "%s-clean", client);
464 for (i = 0; i < 4; i++) {
465 int index, uncanceled = 0;
466 rc = llog_create(ctxt, &handle, NULL, name[i]);
469 rc = llog_init_handle(handle, 0, NULL);
472 GOTO(out_pop, rc = -ENOENT);
475 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
476 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
480 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
481 "Last Index: %d\nUncanceled Records: %d\n\n",
483 i_size_read(handle->lgh_file->f_dentry->d_inode),
484 handle->lgh_last_idx, uncanceled);
493 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
498 struct llog_ctxt *ctxt;
504 static int llog_catinfo_cb(struct llog_handle *cat,
505 struct llog_rec_hdr *rec, void *data)
507 static char *out = NULL;
508 static int remains = 0;
509 struct llog_ctxt *ctxt;
510 struct llog_handle *handle;
511 struct llog_logid *logid;
512 struct llog_logid_rec *lir;
513 int l, rc, index, count = 0;
514 struct cb_data *cbd = (struct cb_data*)data;
518 remains = cbd->remains;
523 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
526 lir = (struct llog_logid_rec *)rec;
527 logid = &lir->lid_id;
528 rc = llog_create(ctxt, &handle, logid, NULL);
531 rc = llog_init_handle(handle, 0, NULL);
535 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
536 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
540 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
541 "\tLog Size: %llu\n\tLast Index: %d\n"
542 "\tUncanceled Records: %d\n",
543 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
544 i_size_read(handle->lgh_file->f_dentry->d_inode),
545 handle->lgh_last_idx, count);
549 cbd->remains = remains;
551 CWARN("Not enough memory\n");
560 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
563 struct mds_obd *mds = &obd->u.mds;
564 struct llog_handle *handle;
565 struct lvfs_run_ctxt saved;
567 struct llog_catid *idarray;
568 struct llog_logid *id;
569 char name[32] = CATLIST;
572 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
574 if (ctxt == NULL || mds == NULL)
577 count = mds->mds_lov_desc.ld_tgt_count;
578 size = sizeof(*idarray) * count;
580 OBD_ALLOC(idarray, size);
584 rc = llog_get_cat_list(obd, obd, name, count, idarray);
588 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
592 data.remains = buf_len;
593 for (i = 0; i < count; i++) {
594 int l, index, uncanceled = 0;
596 id = &idarray[i].lci_logid;
597 rc = llog_create(ctxt, &handle, id, NULL);
600 rc = llog_init_handle(handle, 0, NULL);
603 GOTO(out_pop, rc = -ENOENT);
605 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
606 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
609 l = snprintf(data.out, data.remains,
610 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
612 id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
619 llog_process(handle, llog_catinfo_cb, &data, NULL);
622 if (data.remains <= 0)
626 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
628 OBD_FREE(idarray, size);
632 int llog_catinfo(struct ptlrpc_request *req)
634 struct obd_export *exp = req->rq_export;
635 struct obd_device *obd = exp->exp_obd;
638 int rc, buf_len = LLOG_CHUNK_SIZE;
639 int size[2] = { sizeof(struct ptlrpc_body), buf_len };
641 OBD_ALLOC(buf, buf_len);
644 memset(buf, 0, buf_len);
646 keyword = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, 0);
648 if (strcmp(keyword, "config") == 0) {
649 char *client = lustre_msg_string(req->rq_reqmsg,
651 rc = llog_catinfo_config(obd, buf, buf_len, client);
652 } else if (strcmp(keyword, "deletions") == 0) {
653 rc = llog_catinfo_deletions(obd, buf, buf_len);
658 rc = lustre_pack_reply(req, 2, size, NULL);
660 GOTO(out_free, rc = -ENOMEM);
662 reply = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, buf_len);
663 if (strlen(buf) == 0)
664 sprintf(buf, "%s", "No log informations\n");
665 memcpy(reply, buf, buf_len);
668 OBD_FREE(buf, buf_len);
672 #else /* !__KERNEL__ */
673 int llog_origin_handle_create(struct ptlrpc_request *req)
679 int llog_origin_handle_destroy(struct ptlrpc_request *req)
685 int llog_origin_handle_next_block(struct ptlrpc_request *req)
690 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
695 int llog_origin_handle_read_header(struct ptlrpc_request *req)
700 int llog_origin_handle_close(struct ptlrpc_request *req)
705 int llog_origin_handle_cancel(struct ptlrpc_request *req)