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 <linux/obd_class.h>
34 #include <linux/lustre_log.h>
35 #include <linux/lustre_net.h>
36 #include <portals/list.h>
37 #include <linux/lustre_fsfilt.h>
39 int llog_origin_handle_create(struct ptlrpc_request *req)
41 struct obd_export *exp = req->rq_export;
42 struct obd_device *obd = exp->exp_obd;
43 struct obd_device *disk_obd;
44 struct llog_handle *loghandle;
45 struct llogd_body *body;
46 struct obd_run_ctxt saved;
47 struct llog_logid *logid = NULL;
48 struct llog_ctxt *ctxt;
50 int size = sizeof (*body);
54 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
55 lustre_swab_llogd_body);
57 CERROR ("Can't unpack llogd_body\n");
58 GOTO(out, rc =-EFAULT);
61 if (body->lgd_logid.lgl_oid > 0)
62 logid = &body->lgd_logid;
64 if (req->rq_reqmsg->bufcount > 1) {
65 name = lustre_msg_string(req->rq_reqmsg, 1, 0);
67 CERROR("Can't unpack name\n");
68 GOTO(out, rc = -EFAULT);
72 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
73 LASSERT(ctxt != NULL);
74 disk_obd = ctxt->loc_exp->exp_obd;
75 push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
77 rc = llog_create(ctxt, &loghandle, logid, name);
81 rc = lustre_pack_reply(req, 1, &size, NULL);
83 GOTO(out_close, rc = -ENOMEM);
85 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
86 body->lgd_logid = loghandle->lgh_id;
89 rc2 = llog_close(loghandle);
93 pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
98 int llog_origin_handle_next_block(struct ptlrpc_request *req)
100 struct obd_export *exp = req->rq_export;
101 struct obd_device *obd = exp->exp_obd;
102 struct obd_device *disk_obd;
103 struct llog_handle *loghandle;
104 struct llogd_body *body;
105 struct obd_run_ctxt saved;
106 struct llog_ctxt *ctxt;
110 int size[] = {sizeof (*body),
115 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
116 lustre_swab_llogd_body);
118 CERROR ("Can't unpack llogd_body\n");
119 GOTO(out, rc =-EFAULT);
122 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
124 GOTO(out, rc = -ENOMEM);
126 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
127 LASSERT(ctxt != NULL);
128 disk_obd = ctxt->loc_exp->exp_obd;
129 push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
131 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
135 flags = body->lgd_llh_flags;
136 rc = llog_init_handle(loghandle, flags, NULL);
140 memset(buf, 0, LLOG_CHUNK_SIZE);
141 rc = llog_next_block(loghandle, &body->lgd_saved_index,
143 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
148 rc = lustre_pack_reply(req, 2, size, NULL);
150 GOTO(out_close, rc = -ENOMEM);
152 ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
153 memcpy(ptr, body, sizeof(*body));
155 ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
156 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
159 rc2 = llog_close(loghandle);
164 pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
165 OBD_FREE(buf, LLOG_CHUNK_SIZE);
170 int llog_origin_handle_read_header(struct ptlrpc_request *req)
172 struct obd_export *exp = req->rq_export;
173 struct obd_device *obd = exp->exp_obd;
174 struct obd_device *disk_obd;
175 struct llog_handle *loghandle;
176 struct llogd_body *body;
177 struct llog_log_hdr *hdr;
178 struct obd_run_ctxt saved;
179 struct llog_ctxt *ctxt;
182 int size[] = {sizeof (*hdr)};
186 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
187 lustre_swab_llogd_body);
189 CERROR ("Can't unpack llogd_body\n");
190 GOTO(out, rc =-EFAULT);
193 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
195 GOTO(out, rc = -ENOMEM);
197 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
198 LASSERT(ctxt != NULL);
199 disk_obd = ctxt->loc_exp->exp_obd;
200 push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
202 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
206 /* init_handle reads the header */
207 flags = body->lgd_llh_flags;
208 rc = llog_init_handle(loghandle, flags, NULL);
213 rc = lustre_pack_reply(req, 1, size, NULL);
215 GOTO(out_close, rc = -ENOMEM);
217 hdr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*hdr));
218 memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
221 rc2 = llog_close(loghandle);
226 pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
227 OBD_FREE(buf, LLOG_CHUNK_SIZE);
233 int llog_origin_handle_close(struct ptlrpc_request *req)
242 int llog_origin_handle_cancel(struct ptlrpc_request *req)
244 struct obd_device *obd = req->rq_export->exp_obd;
245 struct obd_device *disk_obd;
246 struct llog_cookie *logcookies;
247 struct llog_ctxt *ctxt;
248 int num_cookies, rc = 0, err, i;
249 struct obd_run_ctxt saved;
250 struct llog_handle *cathandle;
255 logcookies = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*logcookies));
256 num_cookies = req->rq_reqmsg->buflens[0]/sizeof(*logcookies);
257 if (logcookies == NULL || num_cookies == 0) {
258 DEBUG_REQ(D_HA, req, "no cookies sent");
262 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
264 CWARN("llog subsys not setup or already cleanup\n");
268 disk_obd = ctxt->loc_exp->exp_obd;
269 push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
270 for (i = 0; i < num_cookies; i++, logcookies++) {
271 cathandle = ctxt->loc_handle;
272 LASSERT(cathandle != NULL);
273 inode = cathandle->lgh_file->f_dentry->d_inode;
275 handle = fsfilt_start(disk_obd, inode,
276 FSFILT_OP_CANCEL_UNLINK_LOG, NULL);
277 if (IS_ERR(handle)) {
278 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
279 GOTO(pop_ctxt, rc = PTR_ERR(handle));
282 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
284 err = fsfilt_commit(disk_obd, inode, handle, 0);
286 CERROR("error committing transaction: %d\n", err);
293 pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
295 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
297 CDEBUG(D_HA, "cancel %d llog-records\n", num_cookies);
301 EXPORT_SYMBOL(llog_origin_handle_cancel);
303 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
306 struct mds_obd *mds = &obd->u.mds;
307 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
308 struct obd_run_ctxt saved;
309 struct llog_handle *handle = NULL;
311 int rc, i, l, remains = buf_len;
314 if (ctxt == NULL || mds == NULL)
317 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
319 sprintf(name[0], "%s", mds->mds_profile);
320 sprintf(name[1], "%s-clean", mds->mds_profile);
321 sprintf(name[2], "%s", client);
322 sprintf(name[3], "%s-clean", client);
324 for (i = 0; i < 4; i++) {
325 int index, uncanceled = 0;
326 rc = llog_create(ctxt, &handle, NULL, name[i]);
329 rc = llog_init_handle(handle, 0, NULL);
332 GOTO(out_pop, rc = -ENOENT);
335 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
336 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
340 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
341 "Last Index: %d\nUncanceled Records: %d\n\n",
343 handle->lgh_file->f_dentry->d_inode->i_size,
344 handle->lgh_last_idx, uncanceled);
353 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
358 struct llog_ctxt *ctxt;
364 static int llog_catinfo_cb(struct llog_handle *cat,
365 struct llog_rec_hdr *rec, void *data)
367 static char *out = NULL;
368 static int remains = 0;
369 struct llog_ctxt *ctxt;
370 struct llog_handle *handle;
371 struct llog_logid *logid;
372 struct llog_logid_rec *lir;
373 int l, rc, index, count = 0;
374 struct cb_data *cbd = (struct cb_data*)data;
378 remains = cbd->remains;
383 if (!(cat->lgh_hdr->llh_flags & cpu_to_le32(LLOG_F_IS_CAT)))
386 lir = (struct llog_logid_rec *)rec;
387 logid = &lir->lid_id;
388 rc = llog_create(ctxt, &handle, logid, NULL);
391 rc = llog_init_handle(handle, 0, NULL);
395 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
396 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
400 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
401 "\tLog Size: %llu\n\tLast Index: %d\n"
402 "\tUncanceled Records: %d\n",
403 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
404 handle->lgh_file->f_dentry->d_inode->i_size,
405 handle->lgh_last_idx, count);
409 cbd->remains = remains;
411 CWARN("Not enough memory\n");
420 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
423 struct mds_obd *mds = &obd->u.mds;
424 struct llog_handle *handle;
425 struct obd_run_ctxt saved;
427 struct llog_logid *idarray, *id;
428 char name[32] = "CATLIST";
431 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
433 if (ctxt == NULL || mds == NULL)
436 count = mds->mds_lov_desc.ld_tgt_count;
437 size = sizeof(*idarray) * count;
439 OBD_ALLOC(idarray, size);
442 memset(idarray, 0, size);
444 rc = llog_get_cat_list(obd, obd, name, count, idarray);
448 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
453 data.remains = buf_len;
454 for (i = 0; i < count; i++) {
455 int l, index, uncanceled = 0;
456 rc = llog_create(ctxt, &handle, id, NULL);
459 rc = llog_init_handle(handle, 0, NULL);
462 GOTO(out_pop, rc = -ENOENT);
464 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
465 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
468 l = snprintf(data.out, data.remains,
469 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
471 id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
478 llog_process(handle, llog_catinfo_cb, &data, NULL);
481 if (data.remains <= 0)
485 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
487 OBD_FREE(idarray, size);
491 int llog_catinfo(struct ptlrpc_request *req)
493 struct obd_export *exp = req->rq_export;
494 struct obd_device *obd = exp->exp_obd;
497 int rc, buf_len = LLOG_CHUNK_SIZE;
499 OBD_ALLOC(buf, buf_len);
502 memset(buf, 0, buf_len);
504 keyword = lustre_msg_string(req->rq_reqmsg, 0, 0);
506 if (strcmp(keyword, "config") == 0) {
507 char *client = lustre_msg_string(req->rq_reqmsg, 1, 0);
508 rc = llog_catinfo_config(obd, buf, buf_len, client);
509 } else if (strcmp(keyword, "deletions") == 0) {
510 rc = llog_catinfo_deletions(obd, buf, buf_len);
515 rc = lustre_pack_reply(req, 1, &buf_len, NULL);
517 GOTO(out_free, rc = -ENOMEM);
519 reply = lustre_msg_buf(req->rq_repmsg, 0, buf_len);
520 if (strlen(buf) == 0)
521 sprintf(buf, "%s", "No log informations\n");
522 memcpy(reply, buf, buf_len);
525 OBD_FREE(buf, buf_len);