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 * OST<->MDS recovery logging infrastructure.
24 * Invariants in implementation:
25 * - we do not share logs among different OST<->MDS connections, so that
26 * if an OST or MDS fails it need only look at log(s) relevant to itself
29 #define DEBUG_SUBSYSTEM S_LOG
38 #include <liblustre.h>
41 #include <linux/obd.h>
42 #include <linux/obd_class.h>
43 #include <linux/lustre_log.h>
44 #include <linux/obd_ost.h>
45 #include <portals/list.h>
46 #include <linux/lvfs.h>
47 #include <linux/lustre_fsfilt.h>
48 #include "llog_internal.h"
52 static int llog_lvfs_pad(struct obd_device *obd, struct l_file *file,
55 struct llog_rec_hdr rec = { 0 };
56 struct llog_rec_tail tail;
60 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
62 tail.lrt_len = rec.lrh_len = len;
63 tail.lrt_index = rec.lrh_index = index;
66 rc = fsfilt_write_record(obd, file, &rec, sizeof(rec), &file->f_pos, 0);
68 CERROR("error writing padding record: rc %d\n", rc);
72 file->f_pos += len - sizeof(rec) - sizeof(tail);
73 rc = fsfilt_write_record(obd, file, &tail, sizeof(tail),&file->f_pos,0);
75 CERROR("error writing padding record: rc %d\n", rc);
83 static int llog_lvfs_write_blob(struct obd_device *obd, struct l_file *file,
84 struct llog_rec_hdr *rec, void *buf, loff_t off)
87 struct llog_rec_tail end;
88 loff_t saved_off = file->f_pos;
89 int buflen = rec->lrh_len;
95 rc = fsfilt_write_record(obd, file, rec, buflen,&file->f_pos,0);
97 CERROR("error writing log record: rc %d\n", rc);
104 rec->lrh_len = sizeof(*rec) + buflen + sizeof(end);
105 rc = fsfilt_write_record(obd, file, rec, sizeof(*rec), &file->f_pos, 0);
107 CERROR("error writing log hdr: rc %d\n", rc);
111 rc = fsfilt_write_record(obd, file, buf, buflen, &file->f_pos, 0);
113 CERROR("error writing log buffer: rc %d\n", rc);
117 end.lrt_len = rec->lrh_len;
118 end.lrt_index = rec->lrh_index;
119 rc = fsfilt_write_record(obd, file, &end, sizeof(end), &file->f_pos, 0);
121 CERROR("error writing log tail: rc %d\n", rc);
127 if (saved_off > file->f_pos)
128 file->f_pos = saved_off;
133 static int llog_lvfs_read_blob(struct obd_device *obd, struct l_file *file,
134 void *buf, int size, loff_t off)
140 rc = fsfilt_read_record(obd, file, buf, size, &offset);
142 CERROR("error reading log record: rc %d\n", rc);
148 static int llog_lvfs_read_header(struct llog_handle *handle)
150 struct obd_device *obd;
154 LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
156 obd = handle->lgh_ctxt->loc_exp->exp_obd;
158 if (handle->lgh_file->f_dentry->d_inode->i_size == 0) {
159 CDEBUG(D_HA, "not reading header from 0-byte log\n");
163 rc = llog_lvfs_read_blob(obd, handle->lgh_file, handle->lgh_hdr,
166 CERROR("error reading log header\n");
168 struct llog_rec_hdr *llh_hdr = &handle->lgh_hdr->llh_hdr;
170 * These need to be fixed for bug 1987
172 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
173 CERROR("bad log header magic: %#x (expecting %#x)\n",
174 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
176 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
177 CERROR("incorrectly sized log header: %#x "
179 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
180 CERROR("you may need to re-run lconf --write_conf.\n");
186 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
187 handle->lgh_file->f_pos = handle->lgh_file->f_dentry->d_inode->i_size;
192 /* returns negative in on error; 0 if success && reccookie == 0; 1 otherwise */
193 /* appends if idx == -1, otherwise overwrites record idx. */
194 static int llog_lvfs_write_rec(struct llog_handle *loghandle,
195 struct llog_rec_hdr *rec,
196 struct llog_cookie *reccookie, int cookiecount,
199 struct llog_log_hdr *llh;
200 int reclen = rec->lrh_len, index, rc;
201 struct llog_rec_tail *lrt;
202 struct obd_device *obd;
207 llh = loghandle->lgh_hdr;
208 file = loghandle->lgh_file;
209 obd = loghandle->lgh_ctxt->loc_exp->exp_obd;
211 /* record length should not bigger than LLOG_CHUNK_SIZE */
213 rc = (reclen > LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr) -
214 sizeof(struct llog_rec_tail)) ? -E2BIG : 0;
216 rc = (reclen > LLOG_CHUNK_SIZE) ? -E2BIG : 0;
223 /* no header: only allowed to insert record 1 */
224 if (idx != 1 && !file->f_dentry->d_inode->i_size) {
225 CERROR("idx != -1 in empty log\n");
229 if (idx && llh->llh_size && llh->llh_size != reclen)
232 rc = llog_lvfs_write_blob(obd, file, &llh->llh_hdr, NULL, 0);
233 /* we are done if we only write the header or on error */
237 saved_offset = sizeof(*llh) + (idx-1)*rec->lrh_len;
238 rc = llog_lvfs_write_blob(obd, file, rec, buf, saved_offset);
239 if (rc == 0 && reccookie) {
240 reccookie->lgc_lgl = loghandle->lgh_id;
241 reccookie->lgc_index = idx;
247 /* Make sure that records don't cross a chunk boundary, so we can
248 * process them page-at-a-time if needed. If it will cross a chunk
249 * boundary, write in a fake (but referenced) entry to pad the chunk.
251 * We know that llog_current_log() will return a loghandle that is
252 * big enough to hold reclen, so all we care about is padding here.
254 left = LLOG_CHUNK_SIZE - (file->f_pos & (LLOG_CHUNK_SIZE - 1));
256 reclen = sizeof(*rec) + rec->lrh_len +
257 sizeof(struct llog_rec_tail);
259 /* NOTE: padding is a record, but no bit is set */
260 if (left != 0 && left != reclen &&
261 left < (reclen + LLOG_MIN_REC_SIZE)) {
262 int bitmap_size = sizeof(llh->llh_bitmap) * 8;
263 loghandle->lgh_last_idx++;
264 rc = llog_lvfs_pad(obd, file, left, loghandle->lgh_last_idx);
267 /* if it's the last idx in log file, then return -ENOSPC */
268 if (loghandle->lgh_last_idx == bitmap_size - 1)
272 loghandle->lgh_last_idx++;
273 index = loghandle->lgh_last_idx;
274 rec->lrh_index = index;
276 lrt = (struct llog_rec_tail *)
277 ((char *)rec + rec->lrh_len - sizeof(*lrt));
278 lrt->lrt_len = rec->lrh_len;
279 lrt->lrt_index = rec->lrh_index;
281 if (ext2_set_bit(index, llh->llh_bitmap)) {
282 CERROR("argh, index %u already set in log bitmap?\n", index);
283 LBUG(); /* should never happen */
286 llh->llh_tail.lrt_index = index;
288 rc = llog_lvfs_write_blob(obd, file, &llh->llh_hdr, NULL, 0);
292 rc = llog_lvfs_write_blob(obd, file, rec, buf, file->f_pos);
296 CDEBUG(D_HA, "added record "LPX64": idx: %u, %u bytes\n",
297 loghandle->lgh_id.lgl_oid, index, rec->lrh_len);
298 if (rc == 0 && reccookie) {
299 reccookie->lgc_lgl = loghandle->lgh_id;
300 reccookie->lgc_index = index;
301 if (rec->lrh_type == MDS_UNLINK_REC)
302 reccookie->lgc_subsys = LLOG_UNLINK_ORIG_CTXT;
303 else if (rec->lrh_type == OST_SZ_REC)
304 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
305 else if (rec->lrh_type == OST_RAID1_REC)
306 reccookie->lgc_subsys = LLOG_RD1_ORIG_CTXT;
308 reccookie->lgc_subsys = -1;
311 if (rc == 0 && rec->lrh_type == LLOG_GEN_REC)
317 /* We can skip reading at least as many log blocks as the number of
318 * minimum sized log records we are skipping. If it turns out
319 * that we are not far enough along the log (because the
320 * actual records are larger than minimum size) we just skip
321 * some more records. */
323 static void llog_skip_over(__u64 *off, int curr, int goal)
327 *off = (*off + (goal-curr-1) * LLOG_MIN_REC_SIZE) &
328 ~(LLOG_CHUNK_SIZE - 1);
333 * - cur_offset to the furthest point read in the log file
334 * - cur_idx to the log index preceeding cur_offset
335 * returns -EIO/-EINVAL on error
337 static int llog_lvfs_next_block(struct llog_handle *loghandle, int *cur_idx,
338 int next_idx, __u64 *cur_offset, void *buf,
344 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
347 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
348 next_idx, *cur_idx, *cur_offset);
350 while (*cur_offset < loghandle->lgh_file->f_dentry->d_inode->i_size) {
351 struct llog_rec_hdr *rec;
352 struct llog_rec_tail *tail;
355 llog_skip_over(cur_offset, *cur_idx, next_idx);
358 rc = fsfilt_read_record(loghandle->lgh_ctxt->loc_exp->exp_obd,
359 loghandle->lgh_file, buf, len,
363 CERROR("Cant read llog block at log id "LPU64
364 "/%u offset "LPU64"\n",
365 loghandle->lgh_id.lgl_oid,
366 loghandle->lgh_id.lgl_ogen,
371 /* put number of bytes read into rc to make code simpler */
372 rc = ppos - *cur_offset;
375 if (rc == 0) /* end of file, nothing to do */
378 if (rc < sizeof(*tail)) {
379 CERROR("Invalid llog block at log id "LPU64"/%u offset "
380 LPU64"\n", loghandle->lgh_id.lgl_oid,
381 loghandle->lgh_id.lgl_ogen, *cur_offset);
385 tail = buf + rc - sizeof(struct llog_rec_tail);
386 *cur_idx = tail->lrt_index;
388 /* this shouldn't happen */
389 if (tail->lrt_index == 0) {
390 CERROR("Invalid llog tail at log id "LPU64"/%u offset "
391 LPU64"\n", loghandle->lgh_id.lgl_oid,
392 loghandle->lgh_id.lgl_ogen, *cur_offset);
395 if (tail->lrt_index < next_idx)
398 /* sanity check that the start of the new buffer is no farther
399 * than the record that we wanted. This shouldn't happen. */
401 if (rec->lrh_index > next_idx) {
402 CERROR("missed desired record? %u > %u\n",
403 rec->lrh_index, next_idx);
411 static struct file *llog_filp_open(char *name, int flags, int mode)
417 OBD_ALLOC(logname, PATH_MAX);
419 return ERR_PTR(-ENOMEM);
421 len = snprintf(logname, PATH_MAX, "LOGS/%s", name);
422 if (len >= PATH_MAX - 1) {
423 filp = ERR_PTR(-ENAMETOOLONG);
425 filp = l_filp_open(logname, flags, mode);
427 CERROR("logfile creation %s: %ld\n", logname,
431 OBD_FREE(logname, PATH_MAX);
435 /* This is a callback from the llog_* functions.
436 * Assumes caller has already pushed us into the kernel context. */
437 static int llog_lvfs_create(struct llog_ctxt *ctxt, struct llog_handle **res,
438 struct llog_logid *logid, char *name)
440 struct llog_handle *handle;
441 struct obd_device *obd;
442 struct l_dentry *dchild = NULL;
443 struct obdo *oa = NULL;
444 int rc = 0, cleanup_phase = 1;
445 int open_flags = O_RDWR | O_CREAT | O_LARGEFILE;
448 handle = llog_alloc_handle();
454 LASSERT(ctxt->loc_exp);
455 obd = ctxt->loc_exp->exp_obd;
458 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, logid->lgl_oid,
459 logid->lgl_ogen, logid->lgl_ogr);
461 if (IS_ERR(dchild)) {
462 rc = PTR_ERR(dchild);
463 CERROR("error looking up logfile "LPX64":0x%x: rc %d\n",
464 logid->lgl_oid, logid->lgl_ogen, rc);
469 if (dchild->d_inode == NULL) {
471 CERROR("nonexistent log file "LPX64":"LPX64": rc %d\n",
472 logid->lgl_oid, logid->lgl_ogr, rc);
476 handle->lgh_file = l_dentry_open(&obd->obd_ctxt, dchild,
477 O_RDWR | O_LARGEFILE);
478 if (IS_ERR(handle->lgh_file)) {
479 rc = PTR_ERR(handle->lgh_file);
480 CERROR("error opening logfile "LPX64"0x%x: rc %d\n",
481 logid->lgl_oid, logid->lgl_ogen, rc);
485 /* assign the value of lgh_id for handle directly */
486 handle->lgh_id = *logid;
489 handle->lgh_file = llog_filp_open(name, open_flags, 0644);
490 if (IS_ERR(handle->lgh_file))
491 GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
493 handle->lgh_id.lgl_ogr = 1;
494 handle->lgh_id.lgl_oid =
495 handle->lgh_file->f_dentry->d_inode->i_ino;
496 handle->lgh_id.lgl_ogen =
497 handle->lgh_file->f_dentry->d_inode->i_generation;
501 GOTO(cleanup, rc = -ENOMEM);
502 /* XXX get some filter group constants */
504 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLGROUP;
505 rc = obd_create(ctxt->loc_exp, oa, NULL, NULL);
509 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, oa->o_id,
510 oa->o_generation, oa->o_gr);
513 GOTO(cleanup, rc = PTR_ERR(dchild));
515 handle->lgh_file = l_dentry_open(&obd->obd_ctxt, dchild,
517 if (IS_ERR(handle->lgh_file))
518 GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
520 handle->lgh_id.lgl_ogr = oa->o_gr;
521 handle->lgh_id.lgl_oid = oa->o_id;
522 handle->lgh_id.lgl_ogen = oa->o_generation;
525 handle->lgh_ctxt = ctxt;
531 switch (cleanup_phase) {
535 llog_free_handle(handle);
540 static int llog_lvfs_close(struct llog_handle *handle)
545 rc = filp_close(handle->lgh_file, 0);
547 CERROR("error closing log: rc %d\n", rc);
551 static int llog_lvfs_destroy(struct llog_handle *handle)
553 struct dentry *fdentry;
558 fdentry = handle->lgh_file->f_dentry;
559 if (!strcmp(fdentry->d_parent->d_name.name, "LOGS")) {
560 struct inode *inode = fdentry->d_parent->d_inode;
561 rc = llog_lvfs_close(handle);
566 rc = vfs_unlink(inode, fdentry);
575 oa->o_id = handle->lgh_id.lgl_oid;
576 oa->o_gr = handle->lgh_id.lgl_ogr;
577 oa->o_generation = handle->lgh_id.lgl_ogen;
578 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLGENER;
580 rc = llog_lvfs_close(handle);
584 rc = obd_destroy(handle->lgh_ctxt->loc_exp, oa, NULL, NULL);
590 /* reads the catalog list */
591 int llog_get_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
592 char *name, int count, struct llog_catid *idarray)
594 struct obd_run_ctxt saved;
597 int size = sizeof(*idarray) * count;
602 push_ctxt(&saved, &obd->obd_ctxt, NULL);
603 file = filp_open(name, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
604 if (!file || IS_ERR(file)) {
606 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
611 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
612 CERROR("%s is not a regular file!: mode = %o\n", name,
613 file->f_dentry->d_inode->i_mode);
614 GOTO(out, rc = -ENOENT);
617 rc = fsfilt_read_record(disk_obd, file, idarray, size, &off);
619 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
625 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
626 if (file && !IS_ERR(file))
627 rc = filp_close(file, 0);
630 EXPORT_SYMBOL(llog_get_cat_list);
632 /* writes the cat list */
633 int llog_put_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
634 char *name, int count, struct llog_catid *idarray)
636 struct obd_run_ctxt saved;
639 int size = sizeof(*idarray) * count;
644 push_ctxt(&saved, &obd->obd_ctxt, NULL);
645 file = filp_open(name, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
646 if (!file || IS_ERR(file)) {
648 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
653 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
654 CERROR("%s is not a regular file!: mode = %o\n", name,
655 file->f_dentry->d_inode->i_mode);
656 GOTO(out, rc = -ENOENT);
659 rc = fsfilt_write_record(disk_obd, file, idarray, size, &off, 1);
661 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
667 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
668 if (file && !IS_ERR(file))
669 rc = filp_close(file, 0);
673 struct llog_operations llog_lvfs_ops = {
674 lop_write_rec: llog_lvfs_write_rec,
675 lop_next_block: llog_lvfs_next_block,
676 lop_read_header: llog_lvfs_read_header,
677 lop_create: llog_lvfs_create,
678 lop_destroy: llog_lvfs_destroy,
679 lop_close: llog_lvfs_close,
680 // lop_cancel: llog_lvfs_cancel,
683 EXPORT_SYMBOL(llog_lvfs_ops);
685 #else /* !__KERNEL__ */
687 static int llog_lvfs_read_header(struct llog_handle *handle)
693 static int llog_lvfs_write_rec(struct llog_handle *loghandle,
694 struct llog_rec_hdr *rec,
695 struct llog_cookie *reccookie, int cookiecount,
702 static int llog_lvfs_next_block(struct llog_handle *loghandle, int *cur_idx,
703 int next_idx, __u64 *cur_offset, void *buf,
710 static int llog_lvfs_create(struct llog_ctxt *ctxt, struct llog_handle **res,
711 struct llog_logid *logid, char *name)
717 static int llog_lvfs_close(struct llog_handle *handle)
723 static int llog_lvfs_destroy(struct llog_handle *handle)
729 int llog_get_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
730 char *name, int count, struct llog_logid *idarray)
736 int llog_put_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
737 char *name, int count, struct llog_logid *idarray)
743 struct llog_operations llog_lvfs_ops = {
744 lop_write_rec: llog_lvfs_write_rec,
745 lop_next_block: llog_lvfs_next_block,
746 lop_read_header: llog_lvfs_read_header,
747 lop_create: llog_lvfs_create,
748 lop_destroy: llog_lvfs_destroy,
749 lop_close: llog_lvfs_close,
750 // lop_cancel: llog_lvfs_cancel,