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_create(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 size = sizeof (*body);
60 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
61 lustre_swab_llogd_body);
63 CERROR ("Can't unpack llogd_body\n");
64 GOTO(out, rc =-EFAULT);
67 if (body->lgd_logid.lgl_oid > 0)
68 logid = &body->lgd_logid;
70 if (req->rq_reqmsg->bufcount > 1) {
71 name = lustre_msg_string(req->rq_reqmsg, 1, 0);
73 CERROR("Can't unpack name\n");
74 GOTO(out, rc = -EFAULT);
78 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
79 LASSERT(ctxt != NULL);
80 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
82 rc = llog_create(ctxt, &loghandle, logid, name);
86 rc = lustre_pack_reply(req, 1, &size, NULL);
88 GOTO(out_close, rc = -ENOMEM);
90 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
91 body->lgd_logid = loghandle->lgh_id;
94 rc2 = llog_close(loghandle);
98 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
103 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
105 struct obd_export *exp = req->rq_export;
106 struct obd_device *obd = exp->exp_obd;
107 struct llog_handle *loghandle;
108 struct llogd_body *body;
109 struct lvfs_run_ctxt saved;
110 struct llog_ctxt *ctxt;
114 int rc, rc2, size[] = {sizeof (*body), LLOG_CHUNK_SIZE};
117 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
118 lustre_swab_llogd_body);
120 CERROR ("Can't unpack llogd_body\n");
121 GOTO(out, rc =-EFAULT);
124 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
126 GOTO(out, rc = -ENOMEM);
128 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
129 LASSERT(ctxt != NULL);
130 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
132 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
136 flags = body->lgd_llh_flags;
137 rc = llog_init_handle(loghandle, flags, NULL);
141 memset(buf, 0, LLOG_CHUNK_SIZE);
142 rc = llog_prev_block(loghandle, body->lgd_index, buf, LLOG_CHUNK_SIZE);
147 rc = lustre_pack_reply(req, 2, size, NULL);
149 GOTO(out_close, rc = -ENOMEM);
151 ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
152 memcpy(ptr, body, sizeof(*body));
154 ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
155 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
158 rc2 = llog_close(loghandle);
163 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
164 OBD_FREE(buf, LLOG_CHUNK_SIZE);
169 int llog_origin_handle_next_block(struct ptlrpc_request *req)
171 struct obd_export *exp = req->rq_export;
172 struct obd_device *obd = exp->exp_obd;
173 struct llog_handle *loghandle;
174 struct llogd_body *body;
175 struct lvfs_run_ctxt saved;
176 struct llog_ctxt *ctxt;
180 int size[] = {sizeof (*body),
185 body = lustre_swab_reqbuf(req, 0, 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);
197 LASSERT(ctxt != NULL);
198 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
200 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
204 flags = body->lgd_llh_flags;
205 rc = llog_init_handle(loghandle, flags, NULL);
209 memset(buf, 0, LLOG_CHUNK_SIZE);
210 rc = llog_next_block(loghandle, &body->lgd_saved_index,
212 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
217 rc = lustre_pack_reply(req, 2, size, NULL);
219 GOTO(out_close, rc = -ENOMEM);
221 ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
222 memcpy(ptr, body, sizeof(*body));
224 ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
225 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
228 rc2 = llog_close(loghandle);
233 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
234 OBD_FREE(buf, LLOG_CHUNK_SIZE);
239 int llog_origin_handle_read_header(struct ptlrpc_request *req)
241 struct obd_export *exp = req->rq_export;
242 struct obd_device *obd = exp->exp_obd;
243 struct llog_handle *loghandle;
244 struct llogd_body *body;
245 struct llog_log_hdr *hdr;
246 struct lvfs_run_ctxt saved;
247 struct llog_ctxt *ctxt;
249 int size[] = {sizeof (*hdr)};
253 body = lustre_swab_reqbuf(req, 0, sizeof(*body),
254 lustre_swab_llogd_body);
256 CERROR ("Can't unpack llogd_body\n");
257 GOTO(out, rc =-EFAULT);
260 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
261 LASSERT(ctxt != NULL);
262 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
264 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
268 /* init_handle reads the header */
269 flags = body->lgd_llh_flags;
270 rc = llog_init_handle(loghandle, flags, NULL);
274 rc = lustre_pack_reply(req, 1, size, NULL);
276 GOTO(out_close, rc = -ENOMEM);
278 hdr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*hdr));
279 memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
282 rc2 = llog_close(loghandle);
287 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
293 int llog_origin_handle_close(struct ptlrpc_request *req)
302 int llog_origin_handle_cancel(struct ptlrpc_request *req)
304 struct obd_device *obd = req->rq_export->exp_obd;
305 struct llog_cookie *logcookies;
306 struct llog_ctxt *ctxt;
307 int num_cookies, rc = 0, err, i;
308 struct lvfs_run_ctxt saved;
309 struct llog_handle *cathandle;
314 logcookies = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*logcookies));
315 num_cookies = req->rq_reqmsg->buflens[0]/sizeof(*logcookies);
316 if (logcookies == NULL || num_cookies == 0) {
317 DEBUG_REQ(D_HA, req, "no cookies sent");
321 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
323 CWARN("llog subsys not setup or already cleanup\n");
327 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
328 for (i = 0; i < num_cookies; i++, logcookies++) {
329 cathandle = ctxt->loc_handle;
330 LASSERT(cathandle != NULL);
331 inode = cathandle->lgh_file->f_dentry->d_inode;
333 handle = llog_fsfilt_start(ctxt, inode,
334 FSFILT_OP_CANCEL_UNLINK, NULL);
335 if (IS_ERR(handle)) {
336 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
337 GOTO(pop_ctxt, rc = PTR_ERR(handle));
340 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
342 err = llog_fsfilt_commit(ctxt, inode, handle, 0);
344 CERROR("error committing transaction: %d\n", err);
351 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
353 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
355 CDEBUG(D_HA, "cancel %d llog-records\n", num_cookies);
359 EXPORT_SYMBOL(llog_origin_handle_cancel);
361 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
364 struct mds_obd *mds = &obd->u.mds;
365 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
366 struct lvfs_run_ctxt saved;
367 struct llog_handle *handle = NULL;
369 int rc, i, l, remains = buf_len;
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_create(ctxt, &handle, NULL, name[i]);
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_create(ctxt, &handle, logid, NULL);
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, LLOG_CONFIG_ORIG_CTXT);
492 if (ctxt == NULL || mds == NULL)
495 count = mds->mds_lov_desc.ld_tgt_count;
496 size = sizeof(*idarray) * count;
498 OBD_ALLOC(idarray, size);
502 rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
503 name, count, idarray);
507 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
511 data.remains = buf_len;
512 for (i = 0; i < count; i++) {
513 int l, index, uncanceled = 0;
515 id = &idarray[i].lci_logid;
516 rc = llog_create(ctxt, &handle, id, NULL);
519 rc = llog_init_handle(handle, 0, NULL);
522 GOTO(out_pop, rc = -ENOENT);
524 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
525 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
528 l = snprintf(data.out, data.remains,
529 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
531 id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
538 llog_process(handle, llog_catinfo_cb, &data, NULL);
541 if (data.remains <= 0)
545 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
547 OBD_FREE(idarray, size);
551 int llog_catinfo(struct ptlrpc_request *req)
553 struct obd_export *exp = req->rq_export;
554 struct obd_device *obd = exp->exp_obd;
557 int rc, buf_len = LLOG_CHUNK_SIZE;
559 OBD_ALLOC(buf, buf_len);
562 memset(buf, 0, buf_len);
564 keyword = lustre_msg_string(req->rq_reqmsg, 0, 0);
566 if (strcmp(keyword, "config") == 0) {
567 char *client = lustre_msg_string(req->rq_reqmsg, 1, 0);
568 rc = llog_catinfo_config(obd, buf, buf_len, client);
569 } else if (strcmp(keyword, "deletions") == 0) {
570 rc = llog_catinfo_deletions(obd, buf, buf_len);
575 rc = lustre_pack_reply(req, 1, &buf_len, NULL);
577 GOTO(out_free, rc = -ENOMEM);
579 reply = lustre_msg_buf(req->rq_repmsg, 0, buf_len);
580 if (strlen(buf) == 0)
581 sprintf(buf, "%s", "No log informations\n");
582 memcpy(reply, buf, buf_len);
585 OBD_FREE(buf, buf_len);
589 #else /* !__KERNEL__ */
590 int llog_origin_handle_create(struct ptlrpc_request *req)
595 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
600 int llog_origin_handle_next_block(struct ptlrpc_request *req)
605 int llog_origin_handle_read_header(struct ptlrpc_request *req)
610 int llog_origin_handle_close(struct ptlrpc_request *req)
615 int llog_origin_handle_cancel(struct ptlrpc_request *req)