nilfs-utils: move segment i/o routines to an internal library

Add a new header file "segment.h", move definition of segment i/o
routines into it, and move the implementation from libnilfs to a new
internal library libsegment.

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
This commit is contained in:
Ryusuke Konishi 2016-09-11 18:48:36 +09:00
parent 6bdf1142c6
commit 6aa0531a53
17 changed files with 200 additions and 163 deletions

View File

@ -10,6 +10,7 @@ chcp_SOURCES = chcp.c
chcp_LDADD = $(LDADD) $(LIB_POSIX_SEM) $(top_builddir)/lib/libparser.la
dumpseg_SOURCES = dumpseg.c
dumpseg_LDADD = $(LDADD) $(top_builddir)/lib/libsegment.la
lscp_SOURCES = lscp.c

View File

@ -47,6 +47,7 @@
#endif /* HAVE_TIME_H */
#include "nilfs.h"
#include "segment.h"
#ifdef _GNU_SOURCE
#include <getopt.h>

View File

@ -3,4 +3,4 @@
include_HEADERS = nilfs.h nilfs2_api.h nilfs2_ondisk.h nilfs_cleaner.h
noinst_HEADERS = realpath.h nls.h parser.h nilfs_feature.h \
vector.h nilfs_gc.h cnormap.h cleaner_msg.h cleaner_exec.h \
compat.h crc32.h pathnames.h util.h
compat.h crc32.h pathnames.h segment.h util.h

View File

@ -16,6 +16,9 @@
#include <time.h>
#endif /* HAVE_TIME_H */
#include <endian.h>
#include <byteswap.h>
/* Old linux/magic.h may not have the file system magic number of NILFS */
#ifndef NILFS_SUPER_MAGIC
#define NILFS_SUPER_MAGIC 0x3434 /* NILFS filesystem magic number */
@ -90,4 +93,36 @@
((a)->tv_sec cmp (b)->tv_sec))
#endif
/* Byte order */
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define le16_to_cpu(x) ((__u16)(x))
#define le32_to_cpu(x) ((__u32)(x))
#define le64_to_cpu(x) ((__u64)(x))
#define cpu_to_le16(x) ((__u16)(x))
#define cpu_to_le32(x) ((__u32)(x))
#define cpu_to_le64(x) ((__u64)(x))
#define be16_to_cpu(x) bswap_16(x)
#define be32_to_cpu(x) bswap_32(x)
#define be64_to_cpu(x) bswap_64(x)
#define cpu_to_be16(x) bswap_16(x)
#define cpu_to_be32(x) bswap_32(x)
#define cpu_to_be64(x) bswap_64(x)
#elif __BYTE_ORDER == __BIG_ENDIAN
#define le16_to_cpu(x) bswap_16(x)
#define le32_to_cpu(x) bswap_32(x)
#define le64_to_cpu(x) bswap_64(x)
#define cpu_to_le16(x) bswap_16(x)
#define cpu_to_le32(x) bswap_32(x)
#define cpu_to_le64(x) bswap_64(x)
#define be16_to_cpu(x) ((__u16)(x))
#define be32_to_cpu(x) ((__u32)(x))
#define be64_to_cpu(x) ((__u64)(x))
#define cpu_to_be16(x) ((__u16)(x))
#define cpu_to_be32(x) ((__u32)(x))
#define cpu_to_be64(x) ((__u64)(x))
#else
#error "unknown endian"
#endif /* __BYTE_ORDER */
#endif /* __COMPAT_H__ */

View File

@ -28,47 +28,9 @@
#include <time.h>
#include <sys/ioctl.h>
#include <semaphore.h>
#include <linux/types.h>
#include <endian.h>
#include <byteswap.h>
/* FIX ME */
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define le16_to_cpu(x) ((__u16)(x))
#define le32_to_cpu(x) ((__u32)(x))
#define le64_to_cpu(x) ((__u64)(x))
#define cpu_to_le16(x) ((__u16)(x))
#define cpu_to_le32(x) ((__u32)(x))
#define cpu_to_le64(x) ((__u64)(x))
#define be16_to_cpu(x) bswap_16(x)
#define be32_to_cpu(x) bswap_32(x)
#define be64_to_cpu(x) bswap_64(x)
#define cpu_to_be16(x) bswap_16(x)
#define cpu_to_be32(x) bswap_32(x)
#define cpu_to_be64(x) bswap_64(x)
#elif __BYTE_ORDER == __BIG_ENDIAN
#define le16_to_cpu(x) bswap_16(x)
#define le32_to_cpu(x) bswap_32(x)
#define le64_to_cpu(x) bswap_64(x)
#define cpu_to_le16(x) bswap_16(x)
#define cpu_to_le32(x) bswap_32(x)
#define cpu_to_le64(x) bswap_64(x)
#define be16_to_cpu(x) ((__u16)(x))
#define be32_to_cpu(x) ((__u32)(x))
#define be64_to_cpu(x) ((__u64)(x))
#define cpu_to_be16(x) ((__u16)(x))
#define cpu_to_be32(x) ((__u32)(x))
#define cpu_to_be64(x) ((__u64)(x))
#else
#error "unknown endian"
#endif /* __BYTE_ORDER */
#include "nilfs2_ondisk.h"
#include "nilfs2_api.h"
/* XXX: sector_t is not defined in user land */
typedef __u64 sector_t; /* XXX: __u64 ?? */
typedef sector_t nilfs_blkoff_t;
typedef __u64 nilfs_cno_t;
#define NILFS_FSTYPE "nilfs2"
@ -82,6 +44,8 @@ typedef __u64 nilfs_cno_t;
#define NILFS_SB_COMMIT_INTERVAL 0x4000
#define NILFS_SB_BLOCK_MAX 0x8000
struct nilfs_super_block;
/**
* struct nilfs_layout - layout information of nilfs
* @rev_level: revison level
@ -187,123 +151,6 @@ static inline int nilfs_unlock_##name(struct nilfs *nilfs) \
NILFS_LOCK_FNS(cleaner, 0)
/**
* struct nilfs_psegment - partial segment iterator
* @p_segnum: segment number
* @p_blocknr: block number of partial segment
* @p_segblocknr: block number of segment
* @p_nblocks: number of blocks in segment
* @p_maxblocks: maximum number of blocks in segment
* @p_blksize: block size
* @p_seed: CRC seed
*/
struct nilfs_psegment {
struct nilfs_segment_summary *p_segsum;
sector_t p_blocknr;
sector_t p_segblocknr;
size_t p_nblocks;
size_t p_maxblocks;
size_t p_blksize;
__u32 p_seed;
};
/**
* struct nilfs_file - file iterator
* @f_finfo: file information
* @f_blocknr: block number
* @f_offset: byte offset from the beginning of segment
* @f_index: index
* @f_psegment: partial segment
*/
struct nilfs_file {
struct nilfs_finfo *f_finfo;
sector_t f_blocknr;
unsigned long f_offset;
int f_index;
const struct nilfs_psegment *f_psegment;
};
/**
* struct nilfs_block - block iterator
* @b_binfo: block information
* @b_blocknr: block number
* @b_offset: byte offset from the beginning of segment
* @b_index: index
* @b_dsize: size of data block information
* @b_nsize: size of node block information
* @b_file: file
*/
struct nilfs_block {
void *b_binfo;
sector_t b_blocknr;
unsigned long b_offset;
int b_index;
size_t b_dsize;
size_t b_nsize;
const struct nilfs_file *b_file;
};
/* virtual block number and block offset */
#define NILFS_BINFO_DATA_SIZE (sizeof(__le64) + sizeof(__le64))
/* virtual block number */
#define NILFS_BINFO_NODE_SIZE sizeof(__le64)
/* block offset */
#define NILFS_BINFO_DAT_DATA_SIZE sizeof(__le64)
/* block offset and level */
#define NILFS_BINFO_DAT_NODE_SIZE (sizeof(__le64) + sizeof(__le64))
/* partial segment iterator */
void nilfs_psegment_init(struct nilfs_psegment *, __u64,
void *, size_t, const struct nilfs *);
int nilfs_psegment_is_end(const struct nilfs_psegment *);
void nilfs_psegment_next(struct nilfs_psegment *);
#define nilfs_psegment_for_each(pseg, segnum, seg, nblocks, nilfs) \
for (nilfs_psegment_init(pseg, segnum, seg, nblocks, nilfs); \
!nilfs_psegment_is_end(pseg); \
nilfs_psegment_next(pseg))
/* file iterator */
void nilfs_file_init(struct nilfs_file *, const struct nilfs_psegment *);
int nilfs_file_is_end(const struct nilfs_file *);
void nilfs_file_next(struct nilfs_file *);
static inline int nilfs_file_is_super(const struct nilfs_file *file)
{
__u64 ino;
ino = le64_to_cpu(file->f_finfo->fi_ino);
return ino == NILFS_DAT_INO;
}
#define nilfs_file_for_each(file, pseg) \
for (nilfs_file_init(file, pseg); \
!nilfs_file_is_end(file); \
nilfs_file_next(file))
/* block iterator */
void nilfs_block_init(struct nilfs_block *, const struct nilfs_file *);
int nilfs_block_is_end(const struct nilfs_block *);
void nilfs_block_next(struct nilfs_block *);
static inline int nilfs_block_is_data(const struct nilfs_block *blk)
{
return blk->b_index < le32_to_cpu(blk->b_file->f_finfo->fi_ndatablk);
}
static inline int nilfs_block_is_node(const struct nilfs_block *blk)
{
return blk->b_index >= le32_to_cpu(blk->b_file->f_finfo->fi_ndatablk);
}
#define nilfs_block_for_each(blk, file) \
for (nilfs_block_init(blk, file); \
!nilfs_block_is_end(blk); \
nilfs_block_next(blk))
struct nilfs_super_block *nilfs_sb_read(int devfd);
int nilfs_sb_write(int devfd, struct nilfs_super_block *sbp, int mask);

140
include/segment.h Normal file
View File

@ -0,0 +1,140 @@
/*
* segment.h - NILFS segment i/o routines
*
* Licensed under LGPLv2: the complete text of the GNU Lesser General
* Public License can be found in COPYING file of the nilfs-utils
* package.
*/
#ifndef NILFS_SEGMENT_H
#define NILFS_SEGMENT_H
#include <linux/types.h>
#include "compat.h"
#include "nilfs2_ondisk.h"
typedef __u64 sector_t;
/**
* struct nilfs_psegment - partial segment iterator
* @p_segnum: segment number
* @p_blocknr: block number of partial segment
* @p_segblocknr: block number of segment
* @p_nblocks: number of blocks in segment
* @p_maxblocks: maximum number of blocks in segment
* @p_blksize: block size
* @p_seed: CRC seed
*/
struct nilfs_psegment {
struct nilfs_segment_summary *p_segsum;
sector_t p_blocknr;
sector_t p_segblocknr;
size_t p_nblocks;
size_t p_maxblocks;
size_t p_blksize;
__u32 p_seed;
};
/**
* struct nilfs_file - file iterator
* @f_finfo: file information
* @f_blocknr: block number
* @f_offset: byte offset from the beginning of segment
* @f_index: index
* @f_psegment: partial segment
*/
struct nilfs_file {
struct nilfs_finfo *f_finfo;
sector_t f_blocknr;
unsigned long f_offset;
int f_index;
const struct nilfs_psegment *f_psegment;
};
/**
* struct nilfs_block - block iterator
* @b_binfo: block information
* @b_blocknr: block number
* @b_offset: byte offset from the beginning of segment
* @b_index: index
* @b_dsize: size of data block information
* @b_nsize: size of node block information
* @b_file: file
*/
struct nilfs_block {
void *b_binfo;
sector_t b_blocknr;
unsigned long b_offset;
int b_index;
size_t b_dsize;
size_t b_nsize;
const struct nilfs_file *b_file;
};
/* virtual block number and block offset */
#define NILFS_BINFO_DATA_SIZE (sizeof(__le64) + sizeof(__le64))
/* virtual block number */
#define NILFS_BINFO_NODE_SIZE sizeof(__le64)
/* block offset */
#define NILFS_BINFO_DAT_DATA_SIZE sizeof(__le64)
/* block offset and level */
#define NILFS_BINFO_DAT_NODE_SIZE (sizeof(__le64) + sizeof(__le64))
struct nilfs;
/* partial segment iterator */
void nilfs_psegment_init(struct nilfs_psegment *, __u64,
void *, size_t, const struct nilfs *);
int nilfs_psegment_is_end(const struct nilfs_psegment *);
void nilfs_psegment_next(struct nilfs_psegment *);
#define nilfs_psegment_for_each(pseg, segnum, seg, nblocks, nilfs) \
for (nilfs_psegment_init(pseg, segnum, seg, nblocks, nilfs); \
!nilfs_psegment_is_end(pseg); \
nilfs_psegment_next(pseg))
/* file iterator */
void nilfs_file_init(struct nilfs_file *, const struct nilfs_psegment *);
int nilfs_file_is_end(const struct nilfs_file *);
void nilfs_file_next(struct nilfs_file *);
static inline int nilfs_file_is_super(const struct nilfs_file *file)
{
__u64 ino;
ino = le64_to_cpu(file->f_finfo->fi_ino);
return ino == NILFS_DAT_INO;
}
#define nilfs_file_for_each(file, pseg) \
for (nilfs_file_init(file, pseg); \
!nilfs_file_is_end(file); \
nilfs_file_next(file))
/* block iterator */
void nilfs_block_init(struct nilfs_block *, const struct nilfs_file *);
int nilfs_block_is_end(const struct nilfs_block *);
void nilfs_block_next(struct nilfs_block *);
static inline int nilfs_block_is_data(const struct nilfs_block *blk)
{
return blk->b_index < le32_to_cpu(blk->b_file->f_finfo->fi_ndatablk);
}
static inline int nilfs_block_is_node(const struct nilfs_block *blk)
{
return blk->b_index >= le32_to_cpu(blk->b_file->f_finfo->fi_ndatablk);
}
#define nilfs_block_for_each(blk, file) \
for (nilfs_block_init(blk, file); \
!nilfs_block_is_end(blk); \
nilfs_block_next(blk))
#endif /* NILFS_SEGMENT_H */

View File

@ -5,7 +5,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include
lib_LTLIBRARIES = libnilfs.la libnilfsgc.la libnilfscleaner.la
noinst_LTLIBRARIES = librealpath.la libnilfsfeature.la libparser.la \
libmountchk.la libcrc32.la libcleanerexec.la
libmountchk.la libcrc32.la libcleanerexec.la libsegment.la
librealpath_la_SOURCES = realpath.c
@ -19,12 +19,14 @@ libcrc32_la_SOURCES = crc32.c
libcleanerexec_la_SOURCES = cleaner_exec.c
libsegment_la_SOURCES = segment.c
libnilfs_CURRENT = 2
libnilfs_REVISION = 3
libnilfs_AGE = 2
libnilfs_VERSIONINFO = $(libnilfs_CURRENT):$(libnilfs_REVISION):$(libnilfs_AGE)
libnilfs_la_SOURCES = nilfs.c sb.c segment.c
libnilfs_la_SOURCES = nilfs.c sb.c
libnilfs_la_LDFLAGS = -version-info $(libnilfs_VERSIONINFO)
libnilfs_la_LIBADD = librealpath.la libcrc32.la $(LIB_POSIX_SEM)
@ -35,7 +37,8 @@ nilfsgc_VERSIONINFO = $(nilfsgc_CURRENT):$(nilfsgc_REVISION):$(nilfsgc_AGE)
libnilfsgc_la_SOURCES = gc.c vector.c cnormap.c
libnilfsgc_la_LDFLAGS = -version-info $(nilfsgc_VERSIONINFO)
libnilfsgc_la_LIBADD = libnilfs.la $(LIB_POSIX_SEM) $(LIB_POSIX_TIMER)
libnilfsgc_la_LIBADD = libnilfs.la libsegment.la $(LIB_POSIX_SEM) \
$(LIB_POSIX_TIMER)
cleanerctl_CURRENT = 1
cleanerctl_REVISION = 0

View File

@ -40,10 +40,11 @@
#endif /* HAVE_LINUX_TYPES_H */
#include <linux/fs.h>
#include <errno.h>
#include <assert.h>
#include "nilfs.h"
#include "compat.h"
#include "nilfs2_ondisk.h"
#include "nilfs_feature.h"
struct nilfs_feature {

View File

@ -38,6 +38,7 @@
#include <stdarg.h>
#include <signal.h>
#include "util.h"
#include "segment.h"
#include "vector.h"
#include "nilfs_gc.h"

View File

@ -70,6 +70,7 @@
#include <assert.h>
#include "nilfs.h"
#include "compat.h"
#include "nilfs2_ondisk.h"
#include "util.h"
#include "pathnames.h"
#include "realpath.h"

View File

@ -60,6 +60,7 @@
#include <assert.h>
#include "nilfs.h"
#include "compat.h"
#include "nilfs2_ondisk.h"
#include "crc32.h"
#define NILFS_MAX_SB_SIZE 1024

View File

@ -19,6 +19,7 @@
#include <errno.h>
#include "nilfs.h"
#include "segment.h"
#include "util.h"
#include "crc32.h"

View File

@ -80,6 +80,7 @@
#include <uuid/uuid.h>
#include "nilfs.h"
#include "compat.h"
#include "nilfs2_ondisk.h" /* NILFS_MIN_NRSVSEGS */
#include "util.h"
#include "vector.h"
#include "nilfs_gc.h"

View File

@ -71,10 +71,9 @@
#endif /* HAVE_BLKID_BLKID_H */
#include "nilfs.h"
#include "compat.h"
#include "mkfs.h"
#include "util.h"
#include "nilfs_feature.h"
#include "mkfs.h"
#include "pathnames.h"
#include "crc32.h"

View File

@ -23,6 +23,7 @@
#ifndef NILFS_MKFS_H
#define NILFS_MKFS_H
#include "compat.h"
#include "nilfs2_ondisk.h"
#include "util.h" /* BUG_ON() */

View File

@ -60,6 +60,8 @@
#include <errno.h>
#include <signal.h>
#include "nilfs.h"
#include "compat.h"
#include "nilfs2_ondisk.h"
#include "util.h"
#include "nilfs_gc.h"

View File

@ -57,6 +57,8 @@
#include <assert.h>
#include <errno.h>
#include "nilfs.h"
#include "compat.h"
#include "nilfs2_ondisk.h"
#include "nilfs_feature.h"