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 Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * remote api for llog - server side
26 #define DEBUG_SUBSYSTEM S_LOG
33 #include <liblustre.h>
38 #include <linux/obd_class.h>
39 #include <linux/lustre_log.h>
40 #include <linux/lustre_net.h>
41 #include <portals/list.h>
42 #include <linux/lustre_fsfilt.h>
46 int llog_origin_handle_open(struct ptlrpc_request *req)
48 struct obd_export *exp = req->rq_export;
49 struct obd_device *obd = exp->exp_obd;
50 struct llog_handle *loghandle;
51 struct llogd_body *body;
52 struct lvfs_run_ctxt saved;
53 struct llog_logid *logid = NULL;
54 struct llog_ctxt *ctxt;
56 int rc, rc2, size = sizeof (*body);
59 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
60 lustre_swab_llogd_body);
62 CERROR ("Can't unpack llogd_body\n");
63 GOTO(out, rc =-EFAULT);
66 if (body->lgd_logid.lgl_oid > 0)
67 logid = &body->lgd_logid;
69 if (req->rq_reqmsg->bufcount > 1) {
70 name = lustre_msg_string(req->rq_reqmsg, 1, 0);
72 CERROR("Can't unpack name\n");
73 GOTO(out, rc = -EFAULT);
77 ctxt = llog_get_context(&obd->obd_llogs, body->lgd_ctxt_idx);
78 LASSERT(ctxt != NULL);
79 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
81 rc = llog_open(ctxt, &loghandle, logid, name, body->lgd_llh_flags);
85 rc = lustre_pack_reply(req, 1, &size, NULL);
87 GOTO(out_close, rc = -ENOMEM);
89 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
90 body->lgd_logid = loghandle->lgh_id;
93 rc2 = llog_close(loghandle);
97 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
102 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
104 struct obd_export *exp = req->rq_export;
105 struct obd_device *obd = exp->exp_obd;
106 struct llog_handle *loghandle;
107 struct llogd_body *body;
108 struct lvfs_run_ctxt saved;
109 struct llog_ctxt *ctxt;
113 int rc, rc2, size[] = {sizeof (*body), LLOG_CHUNK_SIZE};
116 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
117 lustre_swab_llogd_body);
119 CERROR ("Can't unpack llogd_body\n");
120 GOTO(out, rc =-EFAULT);
123 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
125 GOTO(out, rc = -ENOMEM);
127 ctxt = llog_get_context(&obd->obd_llogs, body->lgd_ctxt_idx);
128 LASSERT(ctxt != NULL);
129 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
131 rc = llog_open(ctxt, &loghandle, &body->lgd_logid, NULL, 0);
135 flags = body->lgd_llh_flags;
136 rc = llog_init_handle(loghandle, flags, NULL);
140 memset(buf, 0, LLOG_CHUNK_SIZE);
141 rc = llog_prev_block(loghandle, body->lgd_index, buf, LLOG_CHUNK_SIZE);
146 rc = lustre_pack_reply(req, 2, size, NULL);
148 GOTO(out_close, rc = -ENOMEM);
150 ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
151 memcpy(ptr, body, sizeof(*body));
153 ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
154 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
157 rc2 = llog_close(loghandle);
162 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
163 OBD_FREE(buf, LLOG_CHUNK_SIZE);
168 int llog_origin_handle_next_block(struct ptlrpc_request *req)
170 struct obd_export *exp = req->rq_export;
171 struct obd_device *obd = exp->exp_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->obd_llogs, body->lgd_ctxt_idx);
196 LASSERT(ctxt != NULL);
197 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
199 rc = llog_open(ctxt, &loghandle, &body->lgd_logid, NULL, 0);
203 flags = body->lgd_llh_flags;
204 rc = llog_init_handle(loghandle, flags, NULL);
208 memset(buf, 0, LLOG_CHUNK_SIZE);
209 rc = llog_next_block(loghandle, &body->lgd_saved_index,
211 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
216 rc = lustre_pack_reply(req, 2, size, NULL);
218 GOTO(out_close, rc = -ENOMEM);
220 ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
221 memcpy(ptr, body, sizeof(*body));
223 ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
224 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
227 rc2 = llog_close(loghandle);
232 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
233 OBD_FREE(buf, LLOG_CHUNK_SIZE);
238 int llog_origin_handle_read_header(struct ptlrpc_request *req)
240 struct obd_export *exp = req->rq_export;
241 struct obd_device *obd = exp->exp_obd;
242 struct llog_handle *loghandle;
243 struct llogd_body *body;
244 struct llog_log_hdr *hdr;
245 struct lvfs_run_ctxt saved;
246 struct llog_ctxt *ctxt;
248 int size[] = {sizeof (*hdr)};
252 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
253 lustre_swab_llogd_body);
255 CERROR ("Can't unpack llogd_body\n");
256 GOTO(out, rc =-EFAULT);
259 ctxt = llog_get_context(&obd->obd_llogs, body->lgd_ctxt_idx);
260 LASSERT(ctxt != NULL);
261 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
263 rc = llog_open(ctxt, &loghandle, &body->lgd_logid, NULL, 0);
267 /* init_handle reads the header */
268 flags = body->lgd_llh_flags;
269 rc = llog_init_handle(loghandle, flags, NULL);
273 rc = lustre_pack_reply(req, 1, size, NULL);
275 GOTO(out_close, rc = -ENOMEM);
277 hdr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*hdr));
278 memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
281 rc2 = llog_close(loghandle);
286 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
292 int llog_origin_handle_close(struct ptlrpc_request *req)
301 int llog_origin_handle_cancel(struct ptlrpc_request *req)
303 struct obd_device *obd = req->rq_export->exp_obd;
304 struct llog_cookie *logcookies;
305 struct llog_ctxt *ctxt;
306 int num_cookies, rc = 0, err, i;
307 struct lvfs_run_ctxt saved;
308 struct llog_handle *cathandle;
313 logcookies = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*logcookies));
314 num_cookies = req->rq_reqmsg->buflens[0]/sizeof(*logcookies);
315 if (logcookies == NULL || num_cookies == 0) {
316 DEBUG_REQ(D_HA, req, "no cookies sent");
320 ctxt = llog_get_context(&obd->obd_llogs, logcookies->lgc_subsys);
322 CWARN("llog subsys not setup or already cleanup\n");
326 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
327 for (i = 0; i < num_cookies; i++, logcookies++) {
328 cathandle = ctxt->loc_handle;
329 LASSERT(cathandle != NULL);
330 inode = cathandle->lgh_file->f_dentry->d_inode;
332 handle = llog_fsfilt_start(ctxt, inode,
333 FSFILT_OP_CANCEL_UNLINK, NULL);
334 if (IS_ERR(handle)) {
335 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
336 GOTO(pop_ctxt, rc = PTR_ERR(handle));
339 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
341 err = llog_fsfilt_commit(ctxt, inode, handle, 0);
343 CERROR("error committing transaction: %d\n", err);
350 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
352 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
354 CDEBUG(D_HA, "cancel %d llog-records\n", num_cookies);
358 EXPORT_SYMBOL(llog_origin_handle_cancel);
360 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
363 struct mds_obd *mds = &obd->u.mds;
364 struct llog_ctxt *ctxt;
365 struct lvfs_run_ctxt saved;
366 struct llog_handle *handle = NULL;
368 int rc, i, l, remains = buf_len;
371 ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
372 if (ctxt == NULL || mds == NULL)
375 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
377 sprintf(name[0], "%s", mds->mds_profile);
378 sprintf(name[1], "%s-clean", mds->mds_profile);
379 sprintf(name[2], "%s", client);
380 sprintf(name[3], "%s-clean", client);
382 for (i = 0; i < 4; i++) {
383 int index, uncanceled = 0;
384 rc = llog_open(ctxt, &handle, NULL, name[i], 0);
387 rc = llog_init_handle(handle, 0, NULL);
390 GOTO(out_pop, rc = -ENOENT);
393 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
394 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
398 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
399 "Last Index: %d\nUncanceled Records: %d\n\n",
401 handle->lgh_file->f_dentry->d_inode->i_size,
402 handle->lgh_last_idx, uncanceled);
411 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
416 struct llog_ctxt *ctxt;
422 static int llog_catinfo_cb(struct llog_handle *cat,
423 struct llog_rec_hdr *rec, void *data)
425 static char *out = NULL;
426 static int remains = 0;
427 struct llog_ctxt *ctxt;
428 struct llog_handle *handle;
429 struct llog_logid *logid;
430 struct llog_logid_rec *lir;
431 int l, rc, index, count = 0;
432 struct cb_data *cbd = (struct cb_data*)data;
436 remains = cbd->remains;
441 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
444 lir = (struct llog_logid_rec *)rec;
445 logid = &lir->lid_id;
446 rc = llog_open(ctxt, &handle, logid, NULL, 0);
449 rc = llog_init_handle(handle, 0, NULL);
453 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
454 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
458 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
459 "\tLog Size: %llu\n\tLast Index: %d\n"
460 "\tUncanceled Records: %d\n",
461 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
462 handle->lgh_file->f_dentry->d_inode->i_size,
463 handle->lgh_last_idx, count);
467 cbd->remains = remains;
469 CWARN("Not enough memory\n");
478 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
481 struct mds_obd *mds = &obd->u.mds;
482 struct llog_handle *handle;
483 struct lvfs_run_ctxt saved;
485 struct llog_catid *idarray;
486 struct llog_logid *id;
487 char name[32] = CATLIST;
490 struct llog_ctxt *ctxt = llog_get_context(&obd->obd_llogs,
491 LLOG_CONFIG_ORIG_CTXT);
493 if (ctxt == NULL || mds == NULL)
496 count = mds->mds_lov_desc.ld_tgt_count;
497 size = sizeof(*idarray) * count;
499 OBD_ALLOC(idarray, size);
503 rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
504 name, count, idarray);
508 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
512 data.remains = buf_len;
513 for (i = 0; i < count; i++) {
514 int l, index, uncanceled = 0;
516 id = &idarray[i].lci_logid;
517 rc = llog_open(ctxt, &handle, id, NULL, 0);
520 rc = llog_init_handle(handle, 0, NULL);
523 GOTO(out_pop, rc = -ENOENT);
525 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
526 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
529 l = snprintf(data.out, data.remains,
530 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
532 id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
539 llog_process(handle, llog_catinfo_cb, &data, NULL);
542 if (data.remains <= 0)
546 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
548 OBD_FREE(idarray, size);
552 int llog_catinfo(struct ptlrpc_request *req)
554 struct obd_export *exp = req->rq_export;
555 struct obd_device *obd = exp->exp_obd;
558 int rc, buf_len = LLOG_CHUNK_SIZE;
560 OBD_ALLOC(buf, buf_len);
563 memset(buf, 0, buf_len);
565 keyword = lustre_msg_string(req->rq_reqmsg, 0, 0);
567 if (strcmp(keyword, "config") == 0) {
568 char *client = lustre_msg_string(req->rq_reqmsg, 1, 0);
569 rc = llog_catinfo_config(obd, buf, buf_len, client);
570 } else if (strcmp(keyword, "deletions") == 0) {
571 rc = llog_catinfo_deletions(obd, buf, buf_len);
576 rc = lustre_pack_reply(req, 1, &buf_len, NULL);
578 GOTO(out_free, rc = -ENOMEM);
580 reply = lustre_msg_buf(req->rq_repmsg, 0, buf_len);
581 if (strlen(buf) == 0)
582 sprintf(buf, "%s", "No log informations\n");
583 memcpy(reply, buf, buf_len);
586 OBD_FREE(buf, buf_len);
590 #else /* !__KERNEL__ */
591 int llog_origin_handle_open(struct ptlrpc_request *req)
596 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
601 int llog_origin_handle_next_block(struct ptlrpc_request *req)
606 int llog_origin_handle_read_header(struct ptlrpc_request *req)
611 int llog_origin_handle_close(struct ptlrpc_request *req)
616 int llog_origin_handle_cancel(struct ptlrpc_request *req)