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;
61 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
63 GOTO(out, rc =-EFAULT);
65 if (body->lgd_logid.lgl_oid > 0)
66 logid = &body->lgd_logid;
68 if (req_capsule_field_present(&req->rq_pill, &RMF_NAME, RCL_CLIENT)) {
69 name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
71 GOTO(out, rc = -EFAULT);
72 CDEBUG(D_INFO, "opening log %s\n", name);
75 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
77 GOTO(out, rc = -EINVAL);
78 disk_obd = ctxt->loc_exp->exp_obd;
79 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
81 rc = llog_create(ctxt, &loghandle, logid, name);
85 rc = req_capsule_server_pack(&req->rq_pill);
87 GOTO(out_close, rc = -ENOMEM);
89 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
90 body->lgd_logid = loghandle->lgh_id;
93 rc2 = llog_close(loghandle);
97 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
102 int llog_origin_handle_destroy(struct ptlrpc_request *req)
104 struct obd_export *exp = req->rq_export;
105 struct obd_device *obd = exp->exp_obd;
106 struct obd_device *disk_obd;
107 struct llog_handle *loghandle;
108 struct llogd_body *body;
109 struct lvfs_run_ctxt saved;
110 struct llog_logid *logid = NULL;
111 struct llog_ctxt *ctxt;
116 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
118 GOTO(out, rc =-EFAULT);
120 if (body->lgd_logid.lgl_oid > 0)
121 logid = &body->lgd_logid;
123 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
125 GOTO(out, rc = -EINVAL);
126 disk_obd = ctxt->loc_exp->exp_obd;
127 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
129 rc = llog_create(ctxt, &loghandle, logid, NULL);
133 rc = req_capsule_server_pack(&req->rq_pill);
135 GOTO(out_close, rc = -ENOMEM);
137 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
138 body->lgd_logid = loghandle->lgh_id;
139 flags = body->lgd_llh_flags;
140 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
143 rc = llog_destroy(loghandle);
146 llog_free_handle(loghandle);
150 llog_close(loghandle);
152 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
157 int llog_origin_handle_next_block(struct ptlrpc_request *req)
159 struct obd_export *exp = req->rq_export;
160 struct obd_device *obd = exp->exp_obd;
161 struct obd_device *disk_obd;
162 struct llog_handle *loghandle;
163 struct llogd_body *body;
164 struct llogd_body *repbody;
165 struct lvfs_run_ctxt saved;
166 struct llog_ctxt *ctxt;
173 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
175 GOTO(out, rc =-EFAULT);
177 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
179 GOTO(out, rc = -ENOMEM);
181 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
183 GOTO(out_free, rc = -EINVAL);
184 disk_obd = ctxt->loc_exp->exp_obd;
185 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
187 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
191 flags = body->lgd_llh_flags;
192 rc = llog_init_handle(loghandle, flags, NULL);
196 memset(buf, 0, LLOG_CHUNK_SIZE);
197 rc = llog_next_block(loghandle, &body->lgd_saved_index,
199 &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
203 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
205 rc = req_capsule_server_pack(&req->rq_pill);
207 GOTO(out_close, rc = -ENOMEM);
209 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
212 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
213 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
216 rc2 = llog_close(loghandle);
221 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
223 OBD_FREE(buf, LLOG_CHUNK_SIZE);
228 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
230 struct obd_export *exp = req->rq_export;
231 struct obd_device *obd = exp->exp_obd;
232 struct llog_handle *loghandle;
233 struct llogd_body *body;
234 struct llogd_body *repbody;
235 struct obd_device *disk_obd;
236 struct lvfs_run_ctxt saved;
237 struct llog_ctxt *ctxt;
244 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
246 GOTO(out, rc =-EFAULT);
248 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
250 GOTO(out, rc = -ENOMEM);
252 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
253 LASSERT(ctxt != NULL);
254 disk_obd = ctxt->loc_exp->exp_obd;
255 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
257 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
261 flags = body->lgd_llh_flags;
262 rc = llog_init_handle(loghandle, flags, NULL);
266 memset(buf, 0, LLOG_CHUNK_SIZE);
267 rc = llog_prev_block(loghandle, body->lgd_index,
268 buf, LLOG_CHUNK_SIZE);
272 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER,
274 rc = req_capsule_server_pack(&req->rq_pill);
276 GOTO(out_close, rc = -ENOMEM);
278 repbody = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
281 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
282 memcpy(ptr, buf, LLOG_CHUNK_SIZE);
285 rc2 = llog_close(loghandle);
290 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
291 OBD_FREE(buf, LLOG_CHUNK_SIZE);
296 int llog_origin_handle_read_header(struct ptlrpc_request *req)
298 struct obd_export *exp = req->rq_export;
299 struct obd_device *obd = exp->exp_obd;
300 struct obd_device *disk_obd;
301 struct llog_handle *loghandle;
302 struct llogd_body *body;
303 struct llog_log_hdr *hdr;
304 struct lvfs_run_ctxt saved;
305 struct llog_ctxt *ctxt;
310 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
312 GOTO(out, rc =-EFAULT);
314 ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
316 GOTO(out, rc = -EINVAL);
317 disk_obd = ctxt->loc_exp->exp_obd;
318 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
320 rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
324 /* init_handle reads the header */
325 flags = body->lgd_llh_flags;
326 rc = llog_init_handle(loghandle, flags, NULL);
330 rc = req_capsule_server_pack(&req->rq_pill);
332 GOTO(out_close, rc = -ENOMEM);
334 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
335 *hdr = *loghandle->lgh_hdr;
338 rc2 = llog_close(loghandle);
343 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
349 int llog_origin_handle_close(struct ptlrpc_request *req)
358 int llog_origin_handle_cancel(struct ptlrpc_request *req)
360 struct obd_device *obd = req->rq_export->exp_obd;
361 struct obd_device *disk_obd;
362 struct llog_cookie *logcookies;
363 struct llog_ctxt *ctxt;
364 int num_cookies, rc = 0, err, i;
365 struct lvfs_run_ctxt saved;
366 struct llog_handle *cathandle;
371 logcookies = req_capsule_client_get(&req->rq_pill, &RMF_LOGCOOKIES);
372 num_cookies = req_capsule_get_size(&req->rq_pill, &RMF_LOGCOOKIES,
373 RCL_CLIENT) / sizeof(*logcookies);
374 if (logcookies == NULL || num_cookies == 0) {
375 DEBUG_REQ(D_HA, req, "no cookies sent");
379 ctxt = llog_get_context(obd, logcookies->lgc_subsys);
381 CWARN("llog subsys not setup or already cleanup\n");
385 disk_obd = ctxt->loc_exp->exp_obd;
386 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
387 for (i = 0; i < num_cookies; i++, logcookies++) {
388 cathandle = ctxt->loc_handle;
389 LASSERT(cathandle != NULL);
390 inode = cathandle->lgh_file->f_dentry->d_inode;
392 handle = fsfilt_start_log(disk_obd, inode,
393 FSFILT_OP_CANCEL_UNLINK, NULL, 1);
394 if (IS_ERR(handle)) {
395 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
396 GOTO(pop_ctxt, rc = PTR_ERR(handle));
399 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
401 err = fsfilt_commit(disk_obd, inode, handle, 0);
403 CERROR("error committing transaction: %d\n", err);
410 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
412 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
414 CDEBUG(D_HA, "cancel %d llog-records\n", num_cookies);
418 EXPORT_SYMBOL(llog_origin_handle_cancel);
420 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
423 struct mds_obd *mds = &obd->u.mds;
424 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
425 struct lvfs_run_ctxt saved;
426 struct llog_handle *handle = NULL;
428 int rc, i, l, remains = buf_len;
431 if (ctxt == NULL || mds == NULL)
434 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
436 sprintf(name[0], "%s", mds->mds_profile);
437 sprintf(name[1], "%s-clean", mds->mds_profile);
438 sprintf(name[2], "%s", client);
439 sprintf(name[3], "%s-clean", client);
441 for (i = 0; i < 4; i++) {
442 int index, uncanceled = 0;
443 rc = llog_create(ctxt, &handle, NULL, name[i]);
446 rc = llog_init_handle(handle, 0, NULL);
449 GOTO(out_pop, rc = -ENOENT);
452 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
453 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
457 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
458 "Last Index: %d\nUncanceled Records: %d\n\n",
460 i_size_read(handle->lgh_file->f_dentry->d_inode),
461 handle->lgh_last_idx, uncanceled);
470 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
475 struct llog_ctxt *ctxt;
481 static int llog_catinfo_cb(struct llog_handle *cat,
482 struct llog_rec_hdr *rec, void *data)
484 static char *out = NULL;
485 static int remains = 0;
486 struct llog_ctxt *ctxt;
487 struct llog_handle *handle;
488 struct llog_logid *logid;
489 struct llog_logid_rec *lir;
490 int l, rc, index, count = 0;
491 struct cb_data *cbd = (struct cb_data*)data;
495 remains = cbd->remains;
500 if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
503 lir = (struct llog_logid_rec *)rec;
504 logid = &lir->lid_id;
505 rc = llog_create(ctxt, &handle, logid, NULL);
508 rc = llog_init_handle(handle, 0, NULL);
512 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
513 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
517 l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
518 "\tLog Size: %llu\n\tLast Index: %d\n"
519 "\tUncanceled Records: %d\n",
520 logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
521 i_size_read(handle->lgh_file->f_dentry->d_inode),
522 handle->lgh_last_idx, count);
526 cbd->remains = remains;
528 CWARN("Not enough memory\n");
537 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
540 struct mds_obd *mds = &obd->u.mds;
541 struct llog_handle *handle;
542 struct lvfs_run_ctxt saved;
544 struct llog_catid *idarray;
545 struct llog_logid *id;
546 char name[32] = CATLIST;
548 struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
551 if (ctxt == NULL || mds == NULL)
554 count = mds->mds_lov_desc.ld_tgt_count;
555 size = sizeof(*idarray) * count;
557 OBD_ALLOC(idarray, size);
561 rc = llog_get_cat_list(obd, obd, name, count, idarray);
565 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
569 data.remains = buf_len;
570 for (i = 0; i < count; i++) {
571 int l, index, uncanceled = 0;
573 id = &idarray[i].lci_logid;
574 rc = llog_create(ctxt, &handle, id, NULL);
577 rc = llog_init_handle(handle, 0, NULL);
580 GOTO(out_pop, rc = -ENOENT);
582 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
583 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
586 l = snprintf(data.out, data.remains,
587 "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x "
589 id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
596 llog_process(handle, llog_catinfo_cb, &data, NULL);
599 if (data.remains <= 0)
603 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
605 OBD_FREE(idarray, size);
609 int llog_catinfo(struct ptlrpc_request *req)
611 struct obd_export *exp = req->rq_export;
612 struct obd_device *obd = exp->exp_obd;
617 OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
620 memset(buf, 0, LLOG_CHUNK_SIZE);
622 keyword = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
625 if (strcmp(keyword, "config") == 0) {
626 char *client = req_capsule_client_get(&req->rq_pill,
630 rc = llog_catinfo_config(obd, buf, LLOG_CHUNK_SIZE, client);
631 } else if (strcmp(keyword, "deletions") == 0) {
632 rc = llog_catinfo_deletions(obd, buf, LLOG_CHUNK_SIZE);
637 req_capsule_set_size(&req->rq_pill, &RMF_STRING, RCL_SERVER,
639 rc = req_capsule_server_pack(&req->rq_pill);
641 GOTO(out_free, rc = -ENOMEM);
643 reply = req_capsule_server_get(&req->rq_pill, &RMF_STRING);
644 if (strlen(buf) == 0)
645 sprintf(buf, "%s", "No log informations\n");
646 memcpy(reply, buf, LLOG_CHUNK_SIZE);
649 OBD_FREE(buf, LLOG_CHUNK_SIZE);
653 #else /* !__KERNEL__ */
654 int llog_origin_handle_create(struct ptlrpc_request *req)
660 int llog_origin_handle_destroy(struct ptlrpc_request *req)
666 int llog_origin_handle_next_block(struct ptlrpc_request *req)
671 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
676 int llog_origin_handle_read_header(struct ptlrpc_request *req)
681 int llog_origin_handle_close(struct ptlrpc_request *req)
686 int llog_origin_handle_cancel(struct ptlrpc_request *req)