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 = sizeof (*body);
62 body = lustre_swab_reqbuf(req, 0, 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 (req->rq_reqmsg->bufcount > 1) {
73 name = lustre_msg_string(req->rq_reqmsg, 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, 1, &size, NULL);
93 GOTO(out_close, rc = -ENOMEM);
95 body = lustre_msg_buf(req->rq_repmsg, 0, 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 (*body);
123 body = lustre_swab_reqbuf(req, 0, 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, 1, &size, NULL);
145 GOTO(out_close, rc = -ENOMEM);
147 body = lustre_msg_buf(req->rq_repmsg, 0, 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[] = {sizeof (*body),
184 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
185 lustre_swab_llogd_body);
187 CERROR ("Can't unpack llogd_body\n");
188 GOTO(out, rc =-EFAULT);
191 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
193 GOTO(out, rc = -ENOMEM);
195 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
197 GOTO(out, rc = -EINVAL);
198 disk_obd = ctxt->loc_exp->exp_obd;
199 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
201 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
205 flags = body->lgd_llh_flags;
206 rc = llog_init_handle(loghandle, flags, NULL);
210 memset(buf, 0, LLOG_CHUNK_SIZE);
211 rc = llog_next_block(loghandle, &body->lgd_saved_index,
213 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
218 rc = lustre_pack_reply(req, 2, size, NULL);
220 GOTO(out_close, rc = -ENOMEM);
222 ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
223 memcpy(ptr, body, sizeof(*body));
225 ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
226 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
229 rc2 = llog_close(loghandle);
234 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
235 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 obd_device *disk_obd;
247 struct lvfs_run_ctxt saved;
248 struct llog_ctxt *ctxt;
252 int size[] = {sizeof (*body),
257 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
258 lustre_swab_llogd_body);
260 CERROR ("Can't unpack llogd_body\n");
261 GOTO(out, rc =-EFAULT);
264 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
266 GOTO(out, rc = -ENOMEM);
268 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
269 LASSERT(ctxt != NULL);
270 disk_obd = ctxt->loc_exp->exp_obd;
271 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
273 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
277 flags = body->lgd_llh_flags;
278 rc = llog_init_handle(loghandle, flags, NULL);
282 memset(buf, 0, LLOG_CHUNK_SIZE);
283 rc = llog_prev_block(loghandle, body->lgd_index,
284 buf, LLOG_CHUNK_SIZE);
289 rc = lustre_pack_reply(req, 2, size, NULL);
291 GOTO(out_close, rc = -ENOMEM);
293 ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
294 memcpy(ptr, body, sizeof(*body));
296 ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
297 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
300 rc2 = llog_close(loghandle);
305 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
306 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;
322 int size[] = {sizeof (*hdr)};
326 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
327 lustre_swab_llogd_body);
329 CERROR ("Can't unpack llogd_body\n");
330 GOTO(out, rc =-EFAULT);
333 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
335 GOTO(out, rc = -EINVAL);
336 disk_obd = ctxt->loc_exp->exp_obd;
337 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
339 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
343 /* init_handle reads the header */
344 flags = body->lgd_llh_flags;
345 rc = llog_init_handle(loghandle, flags, NULL);
349 rc = lustre_pack_reply(req, 1, size, NULL);
351 GOTO(out_close, rc = -ENOMEM);
353 hdr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*hdr));
354 memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
357 rc2 = llog_close(loghandle);
362 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
368 int llog_origin_handle_close(struct ptlrpc_request *req)
377 int llog_origin_handle_cancel(struct ptlrpc_request *req)
379 struct obd_device *obd = req->rq_export->exp_obd;
380 struct obd_device *disk_obd;
381 struct llog_cookie *logcookies;
382 struct llog_ctxt *ctxt;
383 int num_cookies, rc = 0, err, i;
384 struct lvfs_run_ctxt saved;
385 struct llog_handle *cathandle;
390 logcookies = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*logcookies));
391 num_cookies = req->rq_reqmsg->buflens[0]/sizeof(*logcookies);
392 if (logcookies == NULL || num_cookies == 0) {
393 DEBUG_REQ(D_HA, req, "no cookies sent");
397 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
399 CWARN("llog subsys not setup or already cleanup\n");
403 disk_obd = ctxt->loc_exp->exp_obd;
404 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
405 for (i = 0; i < num_cookies; i++, logcookies++) {
406 cathandle = ctxt->loc_handle;
407 LASSERT(cathandle != NULL);
408 inode = cathandle->lgh_file->f_dentry->d_inode;
410 handle = fsfilt_start_log(disk_obd, inode,
411 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
412 if (IS_ERR(handle)) {
413 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
414 GOTO(pop_ctxt, rc = PTR_ERR(handle));
417 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
419 err = fsfilt_commit(disk_obd, inode, handle, 0);
421 CERROR("error committing transaction: %d\n", err);
428 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
430 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
432 CDEBUG(D_HA, "cancel %d llog-records\n", num_cookies);
436 EXPORT_SYMBOL(llog_origin_handle_cancel);
438 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
441 struct mds_obd *mds = &obd->u.mds;
442 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
443 struct lvfs_run_ctxt saved;
444 struct llog_handle *handle = NULL;
446 int rc, i, l, remains = buf_len;
449 if (ctxt == NULL || mds == NULL)
452 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
454 sprintf(name[0], "%s", mds->mds_profile);
455 sprintf(name[1], "%s-clean", mds->mds_profile);
456 sprintf(name[2], "%s", client);
457 sprintf(name[3], "%s-clean", client);
459 for (i = 0; i < 4; i++) {
460 int index, uncanceled = 0;
461 rc = llog_create(ctxt, &handle, NULL, name[i]);
464 rc = llog_init_handle(handle, 0, NULL);
467 GOTO(out_pop, rc = -ENOENT);
470 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
471 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
475 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
476 "Last Index: %d\nUncanceled Records: %d\n\n",
478 handle->lgh_file->f_dentry->d_inode->i_size,
479 handle->lgh_last_idx, uncanceled);
488 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
493 struct llog_ctxt *ctxt;
499 static int llog_catinfo_cb(struct llog_handle *cat,
500 struct llog_rec_hdr *rec, void *data)
502 static char *out = NULL;
503 static int remains = 0;
504 struct llog_ctxt *ctxt;
505 struct llog_handle *handle;
506 struct llog_logid *logid;
507 struct llog_logid_rec *lir;
508 int l, rc, index, count = 0;
509 struct cb_data *cbd = (struct cb_data*)data;
513 remains = cbd->remains;
518 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 handle->lgh_file->f_dentry->d_inode->i_size,
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;
567 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
569 if (ctxt == NULL || mds == NULL)
572 count = mds->mds_lov_desc.ld_tgt_count;
573 size = sizeof(*idarray) * count;
575 OBD_ALLOC(idarray, size);
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);
627 int llog_catinfo(struct ptlrpc_request *req)
629 struct obd_export *exp = req->rq_export;
630 struct obd_device *obd = exp->exp_obd;
633 int rc, buf_len = LLOG_CHUNK_SIZE;
635 OBD_ALLOC(buf, buf_len);
638 memset(buf, 0, buf_len);
640 keyword = lustre_msg_string(req->rq_reqmsg, 0, 0);
642 if (strcmp(keyword, "config") == 0) {
643 char *client = lustre_msg_string(req->rq_reqmsg, 1, 0);
644 rc = llog_catinfo_config(obd, buf, buf_len, client);
645 } else if (strcmp(keyword, "deletions") == 0) {
646 rc = llog_catinfo_deletions(obd, buf, buf_len);
651 rc = lustre_pack_reply(req, 1, &buf_len, NULL);
653 GOTO(out_free, rc = -ENOMEM);
655 reply = lustre_msg_buf(req->rq_repmsg, 0, buf_len);
656 if (strlen(buf) == 0)
657 sprintf(buf, "%s", "No log informations\n");
658 memcpy(reply, buf, buf_len);
661 OBD_FREE(buf, buf_len);
665 #else /* !__KERNEL__ */
666 int llog_origin_handle_create(struct ptlrpc_request *req)
672 int llog_origin_handle_destroy(struct ptlrpc_request *req)
678 int llog_origin_handle_next_block(struct ptlrpc_request *req)
683 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
688 int llog_origin_handle_read_header(struct ptlrpc_request *req)
693 int llog_origin_handle_close(struct ptlrpc_request *req)
698 int llog_origin_handle_cancel(struct ptlrpc_request *req)