Kernel  |  3.10

下载     查看原文件
C++程序  |  737行  |  23.78 KB
/*
 * fs/logfs/logfs.h
 *
 * As should be obvious for Linux kernel code, license is GPLv2
 *
 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
 *
 * Private header for logfs.
 */
#ifndef FS_LOGFS_LOGFS_H
#define FS_LOGFS_LOGFS_H

#undef __CHECK_ENDIAN__
#define __CHECK_ENDIAN__

#include <linux/btree.h>
#include <linux/crc32.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/mempool.h>
#include <linux/pagemap.h>
#include <linux/mtd/mtd.h>
#include "logfs_abi.h"

#define LOGFS_DEBUG_SUPER	(0x0001)
#define LOGFS_DEBUG_SEGMENT	(0x0002)
#define LOGFS_DEBUG_JOURNAL	(0x0004)
#define LOGFS_DEBUG_DIR		(0x0008)
#define LOGFS_DEBUG_FILE	(0x0010)
#define LOGFS_DEBUG_INODE	(0x0020)
#define LOGFS_DEBUG_READWRITE	(0x0040)
#define LOGFS_DEBUG_GC		(0x0080)
#define LOGFS_DEBUG_GC_NOISY	(0x0100)
#define LOGFS_DEBUG_ALIASES	(0x0200)
#define LOGFS_DEBUG_BLOCKMOVE	(0x0400)
#define LOGFS_DEBUG_ALL		(0xffffffff)

#define LOGFS_DEBUG		(0x01)
/*
 * To enable specific log messages, simply define LOGFS_DEBUG to match any
 * or all of the above.
 */
#ifndef LOGFS_DEBUG
#define LOGFS_DEBUG		(0)
#endif

#define log_cond(cond, fmt, arg...) do {	\
	if (cond)				\
		printk(KERN_DEBUG fmt, ##arg);	\
} while (0)

#define log_super(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_SUPER, fmt, ##arg)
#define log_segment(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_SEGMENT, fmt, ##arg)
#define log_journal(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_JOURNAL, fmt, ##arg)
#define log_dir(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_DIR, fmt, ##arg)
#define log_file(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_FILE, fmt, ##arg)
#define log_inode(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_INODE, fmt, ##arg)
#define log_readwrite(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_READWRITE, fmt, ##arg)
#define log_gc(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_GC, fmt, ##arg)
#define log_gc_noisy(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_GC_NOISY, fmt, ##arg)
#define log_aliases(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_ALIASES, fmt, ##arg)
#define log_blockmove(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_BLOCKMOVE, fmt, ##arg)

#define PG_pre_locked		PG_owner_priv_1
#define PagePreLocked(page)	test_bit(PG_pre_locked, &(page)->flags)
#define SetPagePreLocked(page)	set_bit(PG_pre_locked, &(page)->flags)
#define ClearPagePreLocked(page) clear_bit(PG_pre_locked, &(page)->flags)

/* FIXME: This should really be somewhere in the 64bit area. */
#define LOGFS_LINK_MAX		(1<<30)

/* Read-only filesystem */
#define LOGFS_SB_FLAG_RO	0x0001
#define LOGFS_SB_FLAG_DIRTY	0x0002
#define LOGFS_SB_FLAG_OBJ_ALIAS	0x0004
#define LOGFS_SB_FLAG_SHUTDOWN	0x0008

/* Write Control Flags */
#define WF_LOCK			0x01 /* take write lock */
#define WF_WRITE		0x02 /* write block */
#define WF_DELETE		0x04 /* delete old block */

typedef u8 __bitwise level_t;
typedef u8 __bitwise gc_level_t;

#define LEVEL(level) ((__force level_t)(level))
#define GC_LEVEL(gc_level) ((__force gc_level_t)(gc_level))

#define SUBLEVEL(level) ( (void)((level) == LEVEL(1)),	\
		(__force level_t)((__force u8)(level) - 1) )

/**
 * struct logfs_area - area management information
 *
 * @a_sb:			the superblock this area belongs to
 * @a_is_open:			1 if the area is currently open, else 0
 * @a_segno:			segment number of area
 * @a_written_bytes:		number of bytes already written back
 * @a_used_bytes:		number of used bytes
 * @a_ops:			area operations (either journal or ostore)
 * @a_erase_count:		erase count
 * @a_level:			GC level
 */
struct logfs_area { /* a segment open for writing */
	struct super_block *a_sb;
	int	a_is_open;
	u32	a_segno;
	u32	a_written_bytes;
	u32	a_used_bytes;
	const struct logfs_area_ops *a_ops;
	u32	a_erase_count;
	gc_level_t a_level;
};

/**
 * struct logfs_area_ops - area operations
 *
 * @get_free_segment:		fill area->ofs with the offset of a free segment
 * @get_erase_count:		fill area->erase_count (needs area->ofs)
 * @erase_segment:		erase and setup segment
 */
struct logfs_area_ops {
	void	(*get_free_segment)(struct logfs_area *area);
	void	(*get_erase_count)(struct logfs_area *area);
	int	(*erase_segment)(struct logfs_area *area);
};

struct logfs_super;	/* forward */
/**
 * struct logfs_device_ops - device access operations
 *
 * @readpage:			read one page (mm page)
 * @writeseg:			write one segment.  may be a partial segment
 * @erase:			erase one segment
 * @read:			read from the device
 * @erase:			erase part of the device
 * @can_write_buf:		decide whether wbuf can be written to ofs
 */
struct logfs_device_ops {
	struct page *(*find_first_sb)(struct super_block *sb, u64 *ofs);
	struct page *(*find_last_sb)(struct super_block *sb, u64 *ofs);
	int (*write_sb)(struct super_block *sb, struct page *page);
	int (*readpage)(void *_sb, struct page *page);
	void (*writeseg)(struct super_block *sb, u64 ofs, size_t len);
	int (*erase)(struct super_block *sb, loff_t ofs, size_t len,
			int ensure_write);
	int (*can_write_buf)(struct super_block *sb, u64 ofs);
	void (*sync)(struct super_block *sb);
	void (*put_device)(struct logfs_super *s);
};

/**
 * struct candidate_list - list of similar candidates
 */
struct candidate_list {
	struct rb_root rb_tree;
	int count;
	int maxcount;
	int sort_by_ec;
};

/**
 * struct gc_candidate - "candidate" segment to be garbage collected next
 *
 * @list:			list (either free of low)
 * @segno:			segment number
 * @valid:			number of valid bytes
 * @erase_count:		erase count of segment
 * @dist:			distance from tree root
 *
 * Candidates can be on two lists.  The free list contains electees rather
 * than candidates - segments that no longer contain any valid data.  The
 * low list contains candidates to be picked for GC.  It should be kept
 * short.  It is not required to always pick a perfect candidate.  In the
 * worst case GC will have to move more data than absolutely necessary.
 */
struct gc_candidate {
	struct rb_node rb_node;
	struct candidate_list *list;
	u32	segno;
	u32	valid;
	u32	erase_count;
	u8	dist;
};

/**
 * struct logfs_journal_entry - temporary structure used during journal scan
 *
 * @used:
 * @version:			normalized version
 * @len:			length
 * @offset:			offset
 */
struct logfs_journal_entry {
	int used;
	s16 version;
	u16 len;
	u16 datalen;
	u64 offset;
};

enum transaction_state {
	CREATE_1 = 1,
	CREATE_2,
	UNLINK_1,
	UNLINK_2,
	CROSS_RENAME_1,
	CROSS_RENAME_2,
	TARGET_RENAME_1,
	TARGET_RENAME_2,
	TARGET_RENAME_3
};

/**
 * struct logfs_transaction - essential fields to support atomic dirops
 *
 * @ino:			target inode
 * @dir:			inode of directory containing dentry
 * @pos:			pos of dentry in directory
 */
struct logfs_transaction {
	enum transaction_state state;
	u64	 ino;
	u64	 dir;
	u64	 pos;
};

/**
 * struct logfs_shadow - old block in the shadow of a not-yet-committed new one
 * @old_ofs:			offset of old block on medium
 * @new_ofs:			offset of new block on medium
 * @ino:			inode number
 * @bix:			block index
 * @old_len:			size of old block, including header
 * @new_len:			size of new block, including header
 * @level:			block level
 */
struct logfs_shadow {
	u64 old_ofs;
	u64 new_ofs;
	u64 ino;
	u64 bix;
	int old_len;
	int new_len;
	gc_level_t gc_level;
};

/**
 * struct shadow_tree
 * @new:			shadows where old_ofs==0, indexed by new_ofs
 * @old:			shadows where old_ofs!=0, indexed by old_ofs
 * @segment_map:		bitfield of segments containing shadows
 * @no_shadowed_segment:	number of segments containing shadows
 */
struct shadow_tree {
	struct btree_head64 new;
	struct btree_head64 old;
	struct btree_head32 segment_map;
	int no_shadowed_segments;
};

struct object_alias_item {
	struct list_head list;
	__be64 val;
	int child_no;
};

/**
 * struct logfs_block - contains any block state
 * @type:			indirect block or inode
 * @full:			number of fully populated children
 * @partial:			number of partially populated children
 *
 * Most blocks are directly represented by page cache pages.  But when a block
 * becomes dirty, is part of a transaction, contains aliases or is otherwise
 * special, a struct logfs_block is allocated to track the additional state.
 * Inodes are very similar to indirect blocks, so they can also get one of
 * these structures added when appropriate.
 */
#define BLOCK_INDIRECT	1	/* Indirect block */
#define BLOCK_INODE	2	/* Inode */
struct logfs_block_ops;
struct logfs_block {
	struct list_head alias_list;
	struct list_head item_list;
	struct super_block *sb;
	u64 ino;
	u64 bix;
	level_t level;
	struct page *page;
	struct inode *inode;
	struct logfs_transaction *ta;
	unsigned long alias_map[LOGFS_BLOCK_FACTOR / BITS_PER_LONG];
	struct logfs_block_ops *ops;
	int full;
	int partial;
	int reserved_bytes;
};

typedef int write_alias_t(struct super_block *sb, u64 ino, u64 bix,
		level_t level, int child_no, __be64 val);
struct logfs_block_ops {
	void	(*write_block)(struct logfs_block *block);
	void	(*free_block)(struct super_block *sb, struct logfs_block*block);
	int	(*write_alias)(struct super_block *sb,
			struct logfs_block *block,
			write_alias_t *write_one_alias);
};

#define MAX_JOURNAL_ENTRIES 256

struct logfs_super {
	struct mtd_info *s_mtd;			/* underlying device */
	struct block_device *s_bdev;		/* underlying device */
	const struct logfs_device_ops *s_devops;/* device access */
	struct inode	*s_master_inode;	/* inode file */
	struct inode	*s_segfile_inode;	/* segment file */
	struct inode *s_mapping_inode;		/* device mapping */
	atomic_t s_pending_writes;		/* outstanting bios */
	long	 s_flags;
	mempool_t *s_btree_pool;		/* for btree nodes */
	mempool_t *s_alias_pool;		/* aliases in segment.c */
	u64	 s_feature_incompat;
	u64	 s_feature_ro_compat;
	u64	 s_feature_compat;
	u64	 s_feature_flags;
	u64	 s_sb_ofs[2];
	struct page *s_erase_page;		/* for dev_bdev.c */
	/* alias.c fields */
	struct btree_head32 s_segment_alias;	/* remapped segments */
	int	 s_no_object_aliases;
	struct list_head s_object_alias;	/* remapped objects */
	struct btree_head128 s_object_alias_tree; /* remapped objects */
	struct mutex s_object_alias_mutex;
	/* dir.c fields */
	struct mutex s_dirop_mutex;		/* for creat/unlink/rename */
	u64	 s_victim_ino;			/* used for atomic dir-ops */
	u64	 s_rename_dir;			/* source directory ino */
	u64	 s_rename_pos;			/* position of source dd */
	/* gc.c fields */
	long	 s_segsize;			/* size of a segment */
	int	 s_segshift;			/* log2 of segment size */
	long	 s_segmask;			/* 1 << s_segshift - 1 */
	long	 s_no_segs;			/* segments on device */
	long	 s_no_journal_segs;		/* segments used for journal */
	long	 s_no_blocks;			/* blocks per segment */
	long	 s_writesize;			/* minimum write size */
	int	 s_writeshift;			/* log2 of write size */
	u64	 s_size;			/* filesystem size */
	struct logfs_area *s_area[LOGFS_NO_AREAS];	/* open segment array */
	u64	 s_gec;				/* global erase count */
	u64	 s_wl_gec_ostore;		/* time of last wl event */
	u64	 s_wl_gec_journal;		/* time of last wl event */
	u64	 s_sweeper;			/* current sweeper pos */
	u8	 s_ifile_levels;		/* max level of ifile */
	u8	 s_iblock_levels;		/* max level of regular files */
	u8	 s_data_levels;			/* # of segments to leaf block*/
	u8	 s_total_levels;		/* sum of above three */
	struct btree_head32 s_cand_tree;	/* all candidates */
	struct candidate_list s_free_list;	/* 100% free segments */
	struct candidate_list s_reserve_list;	/* Bad segment reserve */
	struct candidate_list s_low_list[LOGFS_NO_AREAS];/* good candidates */
	struct candidate_list s_ec_list;	/* wear level candidates */
	struct btree_head32 s_reserved_segments;/* sb, journal, bad, etc. */
	/* inode.c fields */
	u64	 s_last_ino;			/* highest ino used */
	long	 s_inos_till_wrap;
	u32	 s_generation;			/* i_generation for new files */
	struct list_head s_freeing_list;	/* inodes being freed */
	/* journal.c fields */
	struct mutex s_journal_mutex;
	void	*s_je;				/* journal entry to compress */
	void	*s_compressed_je;		/* block to write to journal */
	u32	 s_journal_seg[LOGFS_JOURNAL_SEGS]; /* journal segments */
	u32	 s_journal_ec[LOGFS_JOURNAL_SEGS]; /* journal erasecounts */
	u64	 s_last_version;
	struct logfs_area *s_journal_area;	/* open journal segment */
	__be64	s_je_array[MAX_JOURNAL_ENTRIES];
	int	s_no_je;

	int	 s_sum_index;			/* for the 12 summaries */
	struct shadow_tree s_shadow_tree;
	int	 s_je_fill;			/* index of current je */
	/* readwrite.c fields */
	struct mutex s_write_mutex;
	int	 s_lock_count;
	mempool_t *s_block_pool;		/* struct logfs_block pool */
	mempool_t *s_shadow_pool;		/* struct logfs_shadow pool */
	struct list_head s_writeback_list;	/* writeback pages */
	/*
	 * Space accounting:
	 * - s_used_bytes specifies space used to store valid data objects.
	 * - s_dirty_used_bytes is space used to store non-committed data
	 *   objects.  Those objects have already been written themselves,
	 *   but they don't become valid until all indirect blocks up to the
	 *   journal have been written as well.
	 * - s_dirty_free_bytes is space used to store the old copy of a
	 *   replaced object, as long as the replacement is non-committed.
	 *   In other words, it is the amount of space freed when all dirty
	 *   blocks are written back.
	 * - s_free_bytes is the amount of free space available for any
	 *   purpose.
	 * - s_root_reserve is the amount of free space available only to
	 *   the root user.  Non-privileged users can no longer write once
	 *   this watermark has been reached.
	 * - s_speed_reserve is space which remains unused to speed up
	 *   garbage collection performance.
	 * - s_dirty_pages is the space reserved for currently dirty pages.
	 *   It is a pessimistic estimate, so some/most will get freed on
	 *   page writeback.
	 *
	 * s_used_bytes + s_free_bytes + s_speed_reserve = total usable size
	 */
	u64	 s_free_bytes;
	u64	 s_used_bytes;
	u64	 s_dirty_free_bytes;
	u64	 s_dirty_used_bytes;
	u64	 s_root_reserve;
	u64	 s_speed_reserve;
	u64	 s_dirty_pages;
	/* Bad block handling:
	 * - s_bad_seg_reserve is a number of segments usually kept
	 *   free.  When encountering bad blocks, the affected segment's data
	 *   is _temporarily_ moved to a reserved segment.
	 * - s_bad_segments is the number of known bad segments.
	 */
	u32	 s_bad_seg_reserve;
	u32	 s_bad_segments;
};

/**
 * struct logfs_inode - in-memory inode
 *
 * @vfs_inode:			struct inode
 * @li_data:			data pointers
 * @li_used_bytes:		number of used bytes
 * @li_freeing_list:		used to track inodes currently being freed
 * @li_flags:			inode flags
 * @li_refcount:		number of internal (GC-induced) references
 */
struct logfs_inode {
	struct inode vfs_inode;
	u64	li_data[LOGFS_EMBEDDED_FIELDS];
	u64	li_used_bytes;
	struct list_head li_freeing_list;
	struct logfs_block *li_block;
	u32	li_flags;
	u8	li_height;
	int	li_refcount;
};

#define journal_for_each(__i) for (__i = 0; __i < LOGFS_JOURNAL_SEGS; __i++)
#define for_each_area(__i) for (__i = 0; __i < LOGFS_NO_AREAS; __i++)
#define for_each_area_down(__i) for (__i = LOGFS_NO_AREAS - 1; __i >= 0; __i--)

/* compr.c */
int logfs_compress(void *in, void *out, size_t inlen, size_t outlen);
int logfs_uncompress(void *in, void *out, size_t inlen, size_t outlen);
int __init logfs_compr_init(void);
void logfs_compr_exit(void);

/* dev_bdev.c */
#ifdef CONFIG_BLOCK
int logfs_get_sb_bdev(struct logfs_super *s,
		struct file_system_type *type,
		const char *devname);
#else
static inline int logfs_get_sb_bdev(struct logfs_super *s,
		struct file_system_type *type,
		const char *devname)
{
	return -ENODEV;
}
#endif

/* dev_mtd.c */
#ifdef CONFIG_MTD
int logfs_get_sb_mtd(struct logfs_super *s, int mtdnr);
#else
static inline int logfs_get_sb_mtd(struct logfs_super *s, int mtdnr)
{
	return -ENODEV;
}
#endif

/* dir.c */
extern const struct inode_operations logfs_symlink_iops;
extern const struct inode_operations logfs_dir_iops;
extern const struct file_operations logfs_dir_fops;
int logfs_replay_journal(struct super_block *sb);

/* file.c */
extern const struct inode_operations logfs_reg_iops;
extern const struct file_operations logfs_reg_fops;
extern const struct address_space_operations logfs_reg_aops;
int logfs_readpage(struct file *file, struct page *page);
long logfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
int logfs_fsync(struct file *file, loff_t start, loff_t end, int datasync);

/* gc.c */
u32 get_best_cand(struct super_block *sb, struct candidate_list *list, u32 *ec);
void logfs_gc_pass(struct super_block *sb);
int logfs_check_areas(struct super_block *sb);
int logfs_init_gc(struct super_block *sb);
void logfs_cleanup_gc(struct super_block *sb);

/* inode.c */
extern const struct super_operations logfs_super_operations;
struct inode *logfs_iget(struct super_block *sb, ino_t ino);
struct inode *logfs_safe_iget(struct super_block *sb, ino_t ino, int *cookie);
void logfs_safe_iput(struct inode *inode, int cookie);
struct inode *logfs_new_inode(struct inode *dir, umode_t mode);
struct inode *logfs_new_meta_inode(struct super_block *sb, u64 ino);
struct inode *logfs_read_meta_inode(struct super_block *sb, u64 ino);
int logfs_init_inode_cache(void);
void logfs_destroy_inode_cache(void);
void logfs_set_blocks(struct inode *inode, u64 no);
/* these logically belong into inode.c but actually reside in readwrite.c */
int logfs_read_inode(struct inode *inode);
int __logfs_write_inode(struct inode *inode, struct page *, long flags);
void logfs_evict_inode(struct inode *inode);

/* journal.c */
void logfs_write_anchor(struct super_block *sb);
int logfs_init_journal(struct super_block *sb);
void logfs_cleanup_journal(struct super_block *sb);
int write_alias_journal(struct super_block *sb, u64 ino, u64 bix,
		level_t level, int child_no, __be64 val);
void do_logfs_journal_wl_pass(struct super_block *sb);

/* readwrite.c */
pgoff_t logfs_pack_index(u64 bix, level_t level);
void logfs_unpack_index(pgoff_t index, u64 *bix, level_t *level);
int logfs_inode_write(struct inode *inode, const void *buf, size_t count,
		loff_t bix, long flags, struct shadow_tree *shadow_tree);
int logfs_readpage_nolock(struct page *page);
int logfs_write_buf(struct inode *inode, struct page *page, long flags);
int logfs_delete(struct inode *inode, pgoff_t index,
		struct shadow_tree *shadow_tree);
int logfs_rewrite_block(struct inode *inode, u64 bix, u64 ofs,
		gc_level_t gc_level, long flags);
int logfs_is_valid_block(struct super_block *sb, u64 ofs, u64 ino, u64 bix,
		gc_level_t gc_level);
int logfs_truncate(struct inode *inode, u64 size);
u64 logfs_seek_hole(struct inode *inode, u64 bix);
u64 logfs_seek_data(struct inode *inode, u64 bix);
int logfs_open_segfile(struct super_block *sb);
int logfs_init_rw(struct super_block *sb);
void logfs_cleanup_rw(struct super_block *sb);
void logfs_add_transaction(struct inode *inode, struct logfs_transaction *ta);
void logfs_del_transaction(struct inode *inode, struct logfs_transaction *ta);
void logfs_write_block(struct logfs_block *block, long flags);
int logfs_write_obj_aliases_pagecache(struct super_block *sb);
void logfs_get_segment_entry(struct super_block *sb, u32 segno,
		struct logfs_segment_entry *se);
void logfs_set_segment_used(struct super_block *sb, u64 ofs, int increment);
void logfs_set_segment_erased(struct super_block *sb, u32 segno, u32 ec,
		gc_level_t gc_level);
void logfs_set_segment_reserved(struct super_block *sb, u32 segno);
void logfs_set_segment_unreserved(struct super_block *sb, u32 segno, u32 ec);
struct logfs_block *__alloc_block(struct super_block *sb,
		u64 ino, u64 bix, level_t level);
void __free_block(struct super_block *sb, struct logfs_block *block);
void btree_write_block(struct logfs_block *block);
void initialize_block_counters(struct page *page, struct logfs_block *block,
		__be64 *array, int page_is_empty);
int logfs_exist_block(struct inode *inode, u64 bix);
int get_page_reserve(struct inode *inode, struct page *page);
void logfs_get_wblocks(struct super_block *sb, struct page *page, int lock);
void logfs_put_wblocks(struct super_block *sb, struct page *page, int lock);
extern struct logfs_block_ops indirect_block_ops;

/* segment.c */
int logfs_erase_segment(struct super_block *sb, u32 ofs, int ensure_erase);
int wbuf_read(struct super_block *sb, u64 ofs, size_t len, void *buf);
int logfs_segment_read(struct inode *inode, struct page *page, u64 ofs, u64 bix,
		level_t level);
int logfs_segment_write(struct inode *inode, struct page *page,
		struct logfs_shadow *shadow);
int logfs_segment_delete(struct inode *inode, struct logfs_shadow *shadow);
int logfs_load_object_aliases(struct super_block *sb,
		struct logfs_obj_alias *oa, int count);
void move_page_to_btree(struct page *page);
int logfs_init_mapping(struct super_block *sb);
void logfs_sync_area(struct logfs_area *area);
void logfs_sync_segments(struct super_block *sb);
void freeseg(struct super_block *sb, u32 segno);
void free_areas(struct super_block *sb);

/* area handling */
int logfs_init_areas(struct super_block *sb);
void logfs_cleanup_areas(struct super_block *sb);
int logfs_open_area(struct logfs_area *area, size_t bytes);
int __logfs_buf_write(struct logfs_area *area, u64 ofs, void *buf, size_t len,
		int use_filler);

static inline int logfs_buf_write(struct logfs_area *area, u64 ofs,
		void *buf, size_t len)
{
	return __logfs_buf_write(area, ofs, buf, len, 0);
}

static inline int logfs_buf_recover(struct logfs_area *area, u64 ofs,
		void *buf, size_t len)
{
	return __logfs_buf_write(area, ofs, buf, len, 1);
}

/* super.c */
struct page *emergency_read_begin(struct address_space *mapping, pgoff_t index);
void emergency_read_end(struct page *page);
void logfs_crash_dump(struct super_block *sb);
int logfs_statfs(struct dentry *dentry, struct kstatfs *stats);
int logfs_check_ds(struct logfs_disk_super *ds);
int logfs_write_sb(struct super_block *sb);

static inline struct logfs_super *logfs_super(struct super_block *sb)
{
	return sb->s_fs_info;
}

static inline struct logfs_inode *logfs_inode(struct inode *inode)
{
	return container_of(inode, struct logfs_inode, vfs_inode);
}

static inline void logfs_set_ro(struct super_block *sb)
{
	logfs_super(sb)->s_flags |= LOGFS_SB_FLAG_RO;
}

#define LOGFS_BUG(sb) do {					\
	struct super_block *__sb = sb;				\
	logfs_crash_dump(__sb);					\
	logfs_super(__sb)->s_flags |= LOGFS_SB_FLAG_RO;		\
	BUG();							\
} while (0)

#define LOGFS_BUG_ON(condition, sb) \
	do { if (unlikely(condition)) LOGFS_BUG((sb)); } while (0)

static inline __be32 logfs_crc32(void *data, size_t len, size_t skip)
{
	return cpu_to_be32(crc32(~0, data+skip, len-skip));
}

static inline u8 logfs_type(struct inode *inode)
{
	return (inode->i_mode >> 12) & 15;
}

static inline pgoff_t logfs_index(struct super_block *sb, u64 pos)
{
	return pos >> sb->s_blocksize_bits;
}

static inline u64 dev_ofs(struct super_block *sb, u32 segno, u32 ofs)
{
	return ((u64)segno << logfs_super(sb)->s_segshift) + ofs;
}

static inline u32 seg_no(struct super_block *sb, u64 ofs)
{
	return ofs >> logfs_super(sb)->s_segshift;
}

static inline u32 seg_ofs(struct super_block *sb, u64 ofs)
{
	return ofs & logfs_super(sb)->s_segmask;
}

static inline u64 seg_align(struct super_block *sb, u64 ofs)
{
	return ofs & ~logfs_super(sb)->s_segmask;
}

static inline struct logfs_block *logfs_block(struct page *page)
{
	return (void *)page->private;
}

static inline level_t shrink_level(gc_level_t __level)
{
	u8 level = (__force u8)__level;

	if (level >= LOGFS_MAX_LEVELS)
		level -= LOGFS_MAX_LEVELS;
	return (__force level_t)level;
}

static inline gc_level_t expand_level(u64 ino, level_t __level)
{
	u8 level = (__force u8)__level;

	if (ino == LOGFS_INO_MASTER) {
		/* ifile has separate areas */
		level += LOGFS_MAX_LEVELS;
	}
	return (__force gc_level_t)level;
}

static inline int logfs_block_shift(struct super_block *sb, level_t level)
{
	level = shrink_level((__force gc_level_t)level);
	return (__force int)level * (sb->s_blocksize_bits - 3);
}

static inline u64 logfs_block_mask(struct super_block *sb, level_t level)
{
	return ~0ull << logfs_block_shift(sb, level);
}

static inline struct logfs_area *get_area(struct super_block *sb,
		gc_level_t gc_level)
{
	return logfs_super(sb)->s_area[(__force u8)gc_level];
}

static inline void logfs_mempool_destroy(mempool_t *pool)
{
	if (pool)
		mempool_destroy(pool);
}

#endif