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;
56 struct llog_rec_tail tail;
60 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0xf) == 0);
62 tail.lrt_len = rec.lrh_len = cpu_to_le32(len);
63 tail.lrt_index = rec.lrh_index = cpu_to_le32(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 = le32_to_cpu(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 = cpu_to_le32(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 llog_rec_tail tail;
151 struct obd_device *obd;
155 LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
157 obd = handle->lgh_ctxt->loc_exp->exp_obd;
159 if (handle->lgh_file->f_dentry->d_inode->i_size == 0) {
160 CDEBUG(D_HA, "not reading header from 0-byte log\n");
164 rc = llog_lvfs_read_blob(obd, handle->lgh_file, handle->lgh_hdr,
167 CERROR("error reading log header\n");
169 rc = llog_lvfs_read_blob(obd, handle->lgh_file, &tail, sizeof(tail),
170 handle->lgh_file->f_dentry->d_inode->i_size -
173 CERROR("error reading log tail\n");
175 handle->lgh_last_idx = le32_to_cpu(tail.lrt_index);
176 handle->lgh_file->f_pos = handle->lgh_file->f_dentry->d_inode->i_size;
181 /* returns negative in on error; 0 if success && reccookie == 0; 1 otherwise */
182 /* appends if idx == -1, otherwise overwrites record idx. */
183 static int llog_lvfs_write_rec(struct llog_handle *loghandle,
184 struct llog_rec_hdr *rec,
185 struct llog_cookie *reccookie, int cookiecount,
188 struct llog_log_hdr *llh;
189 int reclen = le32_to_cpu(rec->lrh_len), index, rc;
190 struct llog_rec_tail *lrt;
191 struct obd_device *obd;
197 llh = loghandle->lgh_hdr;
198 file = loghandle->lgh_file;
199 obd = loghandle->lgh_ctxt->loc_exp->exp_obd;
201 /* record length should not bigger than LLOG_CHUNK_SIZE */
203 rc = (reclen > LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr)
204 - sizeof(struct llog_rec_tail)) ? -E2BIG : 0;
206 rc = (reclen > LLOG_CHUNK_SIZE) ? -E2BIG : 0;
213 /* no header: only allowed to insert record 1 */
214 if (idx != 1 && !file->f_dentry->d_inode->i_size) {
215 CERROR("idx != -1 in empty log\n");
219 if (idx && llh->llh_size && llh->llh_size != reclen)
222 rc = llog_lvfs_write_blob(obd, file, &llh->llh_hdr, NULL, 0);
223 /* we are done if we only write the header or on error */
227 saved_offset = sizeof(*llh) + (idx-1) * le32_to_cpu(rec->lrh_len);
228 rc = llog_lvfs_write_blob(obd, file, rec, buf, saved_offset);
229 if (rc == 0 && reccookie) {
230 reccookie->lgc_lgl = loghandle->lgh_id;
231 reccookie->lgc_index = idx;
237 /* Make sure that records don't cross a chunk boundary, so we can
238 * process them page-at-a-time if needed. If it will cross a chunk
239 * boundary, write in a fake (but referenced) entry to pad the chunk.
241 * We know that llog_current_log() will return a loghandle that is
242 * big enough to hold reclen, so all we care about is padding here.
244 left = LLOG_CHUNK_SIZE - (file->f_pos & (LLOG_CHUNK_SIZE - 1));
246 reclen = sizeof(*rec) + le32_to_cpu(rec->lrh_len) +
247 sizeof(struct llog_rec_tail);
249 /* NOTE: padding is a record, but no bit is set */
250 if (left != 0 && left < reclen) {
251 loghandle->lgh_last_idx++;
252 rc = llog_lvfs_pad(obd, file, left, loghandle->lgh_last_idx);
257 loghandle->lgh_last_idx++;
258 index = loghandle->lgh_last_idx;
259 rec->lrh_index = cpu_to_le32(index);
261 lrt = (void *)rec + le32_to_cpu(rec->lrh_len) - sizeof(*lrt);
262 lrt->lrt_len = rec->lrh_len;
263 lrt->lrt_index = rec->lrh_index;
265 if (ext2_set_bit(index, llh->llh_bitmap)) {
266 CERROR("argh, index %u already set in log bitmap?\n", index);
267 LBUG(); /* should never happen */
269 llh->llh_count = cpu_to_le32(le32_to_cpu(llh->llh_count) + 1);
272 rc = llog_lvfs_write_blob(obd, file, &llh->llh_hdr, NULL, 0);
276 rc = llog_lvfs_write_blob(obd, file, rec, buf, file->f_pos);
280 CDEBUG(D_HA, "added record "LPX64": idx: %u, %u bytes\n",
281 loghandle->lgh_id.lgl_oid, index, le32_to_cpu(rec->lrh_len));
282 if (rc == 0 && reccookie) {
283 reccookie->lgc_lgl = loghandle->lgh_id;
284 reccookie->lgc_index = index;
285 if (le32_to_cpu(rec->lrh_type) == MDS_UNLINK_REC)
286 reccookie->lgc_subsys = LLOG_UNLINK_ORIG_CTXT;
287 else if (le32_to_cpu(rec->lrh_type) == OST_SZ_REC)
288 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
289 else if (le32_to_cpu(rec->lrh_type) == OST_RAID1_REC)
290 reccookie->lgc_subsys = LLOG_RD1_ORIG_CTXT;
292 reccookie->lgc_subsys = -1;
298 /* We can skip reading at least as many log blocks as the number of
299 * minimum sized log records we are skipping. If it turns out
300 * that we are not far enough along the log (because the
301 * actual records are larger than minimum size) we just skip
302 * some more records. */
304 static void llog_skip_over(__u64 *off, int curr, int goal)
308 *off = (*off + (goal-curr-1) * LLOG_MIN_REC_SIZE) &
309 ~(LLOG_CHUNK_SIZE - 1);
314 * - cur_offset to the furthest point read in the log file
315 * - cur_idx to the log index preceeding cur_offset
316 * returns -EIO/-EINVAL on error
318 static int llog_lvfs_next_block(struct llog_handle *loghandle, int *cur_idx,
319 int next_idx, __u64 *cur_offset, void *buf,
325 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
328 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
329 next_idx, *cur_idx, *cur_offset);
331 while (*cur_offset < loghandle->lgh_file->f_dentry->d_inode->i_size) {
332 struct llog_rec_hdr *rec;
333 struct llog_rec_tail *tail;
336 llog_skip_over(cur_offset, *cur_idx, next_idx);
339 rc = fsfilt_read_record(loghandle->lgh_ctxt->loc_exp->exp_obd,
340 loghandle->lgh_file, buf, len,
344 CERROR("Cant read llog block at log id "LPU64
345 "/%u offset "LPU64"\n",
346 loghandle->lgh_id.lgl_oid,
347 loghandle->lgh_id.lgl_ogen,
352 /* put number of readed bytes in rc to make code simpler */
353 rc = *cur_offset - ppos;
355 if (rc == 0) /* end of file, nothing to do */
358 if (rc < sizeof(*tail)) {
359 CERROR("Invalid llog block at log id "LPU64"/%u offset "
360 LPU64"\n", loghandle->lgh_id.lgl_oid,
361 loghandle->lgh_id.lgl_ogen, *cur_offset);
365 tail = buf + rc - sizeof(struct llog_rec_tail);
366 *cur_idx = le32_to_cpu(tail->lrt_index);
368 /* this shouldn't happen */
369 if (tail->lrt_index == 0) {
370 CERROR("Invalid llog tail at log id "LPU64"/%u offset "
371 LPU64"\n", loghandle->lgh_id.lgl_oid,
372 loghandle->lgh_id.lgl_ogen, *cur_offset);
375 if (le32_to_cpu(tail->lrt_index) < next_idx)
378 /* sanity check that the start of the new buffer is no farther
379 * than the record that we wanted. This shouldn't happen. */
381 if (le32_to_cpu(rec->lrh_index) > next_idx) {
382 CERROR("missed desired record? %u > %u\n",
383 le32_to_cpu(rec->lrh_index), next_idx);
391 static struct file *llog_filp_open(char *name, int flags, int mode)
397 OBD_ALLOC(logname, PATH_MAX);
399 return ERR_PTR(-ENOMEM);
401 len = snprintf(logname, PATH_MAX, "LOGS/%s", name);
402 if (len >= PATH_MAX - 1) {
403 filp = ERR_PTR(-ENAMETOOLONG);
405 filp = l_filp_open(logname, flags, mode);
407 CERROR("logfile creation %s: %ld\n", logname,
411 OBD_FREE(logname, PATH_MAX);
415 /* This is a callback from the llog_* functions.
416 * Assumes caller has already pushed us into the kernel context. */
417 static int llog_lvfs_create(struct llog_ctxt *ctxt, struct llog_handle **res,
418 struct llog_logid *logid, char *name)
420 struct llog_handle *handle;
421 struct obd_device *obd;
422 struct l_dentry *dchild = NULL;
423 struct obdo *oa = NULL;
424 int rc = 0, cleanup_phase = 1;
425 int open_flags = O_RDWR | O_CREAT | O_LARGEFILE;
428 handle = llog_alloc_handle();
434 LASSERT(ctxt->loc_exp);
435 obd = ctxt->loc_exp->exp_obd;
438 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, logid->lgl_oid,
439 logid->lgl_ogen, logid->lgl_ogr);
441 if (IS_ERR(dchild)) {
442 rc = PTR_ERR(dchild);
443 CERROR("error looking up log file "LPX64":0x%x: rc %d\n",
444 logid->lgl_oid, logid->lgl_ogen, rc);
449 if (dchild->d_inode == NULL) {
451 CERROR("nonexistent log file "LPX64":"LPX64": rc %d\n",
452 logid->lgl_oid, logid->lgl_ogr, rc);
456 handle->lgh_file = l_dentry_open(&obd->obd_ctxt, dchild,
457 O_RDWR | O_LARGEFILE);
458 if (IS_ERR(handle->lgh_file)) {
459 rc = PTR_ERR(handle->lgh_file);
460 CERROR("error opening logfile "LPX64"0x%x: rc %d\n",
461 logid->lgl_oid, logid->lgl_ogen, rc);
465 /* assign the value of lgh_id for handle directly */
466 handle->lgh_id = *logid;
469 handle->lgh_file = llog_filp_open(name, open_flags, 0644);
470 if (IS_ERR(handle->lgh_file))
471 GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
473 handle->lgh_id.lgl_ogr = 1;
474 handle->lgh_id.lgl_oid =
475 handle->lgh_file->f_dentry->d_inode->i_ino;
476 handle->lgh_id.lgl_ogen =
477 handle->lgh_file->f_dentry->d_inode->i_generation;
481 GOTO(cleanup, rc = -ENOMEM);
482 /* XXX get some filter group constants */
484 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLGROUP;
485 rc = obd_create(ctxt->loc_exp, oa, NULL, NULL);
489 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, oa->o_id,
490 oa->o_generation, oa->o_gr);
493 GOTO(cleanup, rc = PTR_ERR(dchild));
495 handle->lgh_file = l_dentry_open(&obd->obd_ctxt, dchild,
497 if (IS_ERR(handle->lgh_file))
498 GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
500 handle->lgh_id.lgl_ogr = oa->o_gr;
501 handle->lgh_id.lgl_oid = oa->o_id;
502 handle->lgh_id.lgl_ogen = oa->o_generation;
505 handle->lgh_ctxt = ctxt;
511 switch (cleanup_phase) {
515 llog_free_handle(handle);
520 static int llog_lvfs_close(struct llog_handle *handle)
525 rc = filp_close(handle->lgh_file, 0);
527 CERROR("error closing log: rc %d\n", rc);
531 static int llog_lvfs_destroy(struct llog_handle *handle)
541 oa->o_id = handle->lgh_id.lgl_oid;
542 oa->o_gr = handle->lgh_id.lgl_ogr;
543 oa->o_generation = handle->lgh_id.lgl_ogen;
544 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLGENER;
546 rc = llog_lvfs_close(handle);
550 rc = obd_destroy(handle->lgh_ctxt->loc_exp, oa, NULL, NULL);
556 /* reads the catalog list */
557 int llog_get_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
558 char *name, int count, struct llog_logid *idarray)
560 struct obd_run_ctxt saved;
563 int size = sizeof(*idarray) * count;
568 push_ctxt(&saved, &obd->obd_ctxt, NULL);
569 file = filp_open(name, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
570 if (!file || IS_ERR(file)) {
572 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
577 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
578 CERROR("%s is not a regular file!: mode = %o\n", name,
579 file->f_dentry->d_inode->i_mode);
580 GOTO(out, rc = -ENOENT);
583 rc = fsfilt_read_record(disk_obd, file, idarray, size, &off);
585 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
591 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
592 if (file && !IS_ERR(file))
593 rc = filp_close(file, 0);
597 /* writes the cat list */
598 int llog_put_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
599 char *name, int count, struct llog_logid *idarray)
601 struct obd_run_ctxt saved;
604 int size = sizeof(*idarray) * count;
609 push_ctxt(&saved, &obd->obd_ctxt, NULL);
610 file = filp_open(name, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
611 if (!file || IS_ERR(file)) {
613 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
618 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
619 CERROR("%s is not a regular file!: mode = %o\n", name,
620 file->f_dentry->d_inode->i_mode);
621 GOTO(out, rc = -ENOENT);
624 rc = fsfilt_write_record(disk_obd, file, idarray, size, &off, 1);
626 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
632 pop_ctxt(&saved, &obd->obd_ctxt, NULL);
633 if (file && !IS_ERR(file))
634 rc = filp_close(file, 0);
638 struct llog_operations llog_lvfs_ops = {
639 lop_write_rec: llog_lvfs_write_rec,
640 lop_next_block: llog_lvfs_next_block,
641 lop_read_header: llog_lvfs_read_header,
642 lop_create: llog_lvfs_create,
643 lop_destroy: llog_lvfs_destroy,
644 lop_close: llog_lvfs_close,
645 // lop_cancel: llog_lvfs_cancel,
648 EXPORT_SYMBOL(llog_lvfs_ops);
650 #else /* !__KERNEL__ */
652 static int llog_lvfs_read_header(struct llog_handle *handle)
657 static int llog_lvfs_write_rec(struct llog_handle *loghandle,
658 struct llog_rec_hdr *rec,
659 struct llog_cookie *reccookie, int cookiecount,
665 static int llog_lvfs_next_block(struct llog_handle *loghandle, int *cur_idx,
666 int next_idx, __u64 *cur_offset, void *buf,
672 static int llog_lvfs_create(struct llog_obd_ctxt *ctxt, struct llog_handle **res,
673 struct llog_logid *logid, char *name)
678 static int llog_lvfs_close(struct llog_handle *handle)
683 static int llog_lvfs_destroy(struct llog_handle *handle)
689 int llog_get_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
690 char *name, int count, struct llog_logid *idarray)
696 int llog_put_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
697 char *name, int count, struct llog_logid *idarray)
703 struct llog_operations llog_lvfs_ops = {
704 lop_write_rec: llog_lvfs_write_rec,
705 lop_next_block: llog_lvfs_next_block,
706 lop_read_header: llog_lvfs_read_header,
707 lop_create: llog_lvfs_create,
708 lop_destroy: llog_lvfs_destroy,
709 lop_close: llog_lvfs_close,
710 // lop_cancel: llog_lvfs_cancel,