Next: Introduction to the EXT2FS Library, Previous: (dir), Up: (dir) [Contents][Index]
This manual documents the EXT2FS Library, version 1.46.5.
Next: EXT2FS Library Functions, Previous: The EXT2FS Library, Up: The EXT2FS Library [Contents][Index]
The EXT2FS library is designed to allow user-level programs to manipulate an ext2 filesystem.
Next: Concept Index, Previous: Introduction to the EXT2FS Library, Up: The EXT2FS Library [Contents][Index]
Next: File I/O Functions, Previous: EXT2FS Library Functions, Up: EXT2FS Library Functions [Contents][Index]
The following functions operate on a filesystem handle. Most EXT2FS
Library functions require a filesystem handle as their first argument.
There are two functions which create a filesystem handle,
ext2fs_open
and ext2fs_initialize
.
The filesystem can also be closed using ext2fs_close
, and any
changes to the superblock and group descriptors can be written out to disk
using ext2fs_flush
.
Next: Closing and flushing out changes, Previous: Filesystem-level functions, Up: Filesystem-level functions [Contents][Index]
Most libext2fs functions take a filesystem handle of type
ext2_filsys
. A filesystem handle is created either by opening
an existing filesystem using ext2fs_open
, or by initializing a new
filesystem using ext2fs_initialize
.
Opens a filesystem named name, using the the io_manager
manager to define the input/output routines needed to read and
write the filesystem. In the case of the unix_io
io_manager,
name is interpreted as the Unix filename of the filesystem image.
This is often a device file, such as /dev/hda1.
The superblock parameter specifies the block number of the
superblock which should be used when opening the filesystem.
If superblock is zero, ext2fs_open
will use the primary
superblock located at offset 1024 bytes from the start of the filesystem
image.
The block_size parameter specifies the block size used by the
filesystem. Normally this is determined automatically from the
filesystem superblock. If block_size is non-zero, it must match
the block size found in the superblock, or the error
EXT2_ET_UNEXPECTED_BLOCK_SIZE
will be returned. The
block_size parameter is also used to help find the superblock when
superblock is non-zero.
The flags argument contains a bitmask of flags which control how the filesystem open should be handled.
EXT2_FLAG_RW
Open the filesystem for reading and writing. Without this flag, the filesystem is opened for reading only.
EXT2_FLAG_FORCE
Open the filesystem regardless of the feature sets listed in the superblock.
Next: Initializing a filesystem, Previous: Opening an ext2 filesystem, Up: Filesystem-level functions [Contents][Index]
Write any changes to the high-level filesystem data structures in the fs filesystem. The following data structures will be written out:
ext2fs_read_bitmaps
.
Close the io_manager abstraction for fs and release all memory associated with the filesystem handle.
Flush out any changes to the high-level filesystem data structures using
ext2fs_flush
if the filesystem is marked dirty; then close and
free the filesystem using ext2fs_free
.
Next: Filesystem flag functions, Previous: Closing and flushing out changes, Up: Filesystem-level functions [Contents][Index]
An ext2 filesystem is initializing by the mke2fs
program. The
two functions described here, ext2fs_initialize
and
ext2fs_allocate_tables
do much of the initial work for setting up
a filesystem. However, they don’t do the whole job. mke2fs
calls ext2fs_initialize
to set up the filesystem superblock, and
calls ext2fs_allocate_tables
to allocate space for the inode
table, and the inode and block bitmaps. In addition, mke2fs
must
also initialize the inode tables by clearing them with zeros, create the
root and lost+found directories, and reserve the reserved inodes.
This function is used by the mke2fs
program to initialize a
filesystem. The ext2fs_initialize
function creates a filesystem
handle which is returned in ret_fs that has been properly setup
for a filesystem to be located in name, using the io_manager
manager. The prototype superblock in param is used to
supply parameters such as the number of blocks in the filesystem, the
block size, etc.
The ext2fs_initialize
function does not actually do any I/O; that
will be done when the application program calls ext2fs_close
or
ext2fs_flush
. Also, this function only initializes the
superblock and group descriptor structures. It does not create the
inode table or the root directory. This must be done by the calling
application, such as mke2fs
.
The following values may be set in the param prototype superblock;
if a value of 0 is found in a field, ext2fs_initialize
will use a
default value. The calling application should zero out the prototype
entire superblock, and then fill in any appropriate values.
s_blocks_count
The number of blocks in the filesystem. This parameter is mandatory and must be set by the calling application.
s_inodes_count
The number of inodes in the filesystem. The default value is determined by calculating the size of the filesystem, and creating one inode for every 4096 bytes.
s_r_blocks_count
The number of blocks which should be reserved for the superuser. The default value is zero blocks.
s_log_block_size
The blocksize of the filesystem. Valid values are 0 (1024 bytes), 1 (2048 bytes), or 2 (4096 bytes). The default blocksize is 1024 bytes.
s_log_frag_size
The size of fragments. The ext2 filesystem does not support fragments
(and may never support fragments). Currently this field must be the
same as s_log_block_size
.
s_first_data_block
The first data block for the filesystem. For filesystem with a blocksize of 1024 bytes, this value must be at least 1, since the superblock is located in block number 1. For filesystems with larger blocksizes, the superblock is still located at an offset of 1024 bytes, so the superblock is located in block number 0. By default, this value is set to 1 for filesystems with a block size of 1024 bytes, or 0 for filesystems with larger blocksizes.
s_max_mnt_count
This field defines the number of times that the filesystem can be
mounted before it should be checked using e2fsck
. When
e2fsck
is run without the ‘-f’ option, e2fsck
will
skip the filesystem check if the number of times that the filesystem has
been mounted is less than s_max_mnt_count
and if the interval
between the last time a filesystem check was performed and the current
time is less than s_checkinterval
(see below). The default value
of s_max_mnt_count
is 20.
s_checkinterval
This field defines the minimal interval between filesystem checks. See
the previous entry for a discussion of how this field is used by
e2fsck
. The default value of this field is 180 days (six
months).
s_errors
This field defines the behavior which should be used by the kernel of errors are detected in the filesystem. Possible values include:
Continue execution when errors are detected.
Remount the filesystem read-only.
Panic.
The default behavior is ‘EXT2_ERRORS_CONTINUE’.
Allocate space for the inode table and the block and inode bitmaps. The inode tables and block and inode bitmaps aren’t actually initialized; this function just allocates the space for them.
Previous: Initializing a filesystem, Up: Filesystem-level functions [Contents][Index]
The filesystem handle has a number of flags which can be manipulated using the following function. Some of these flags affect how the libext2fs filesystem behaves; others are provided solely for the application’s convenience.
This flag indicates whether or not the filesystem has been changed. It is not used by the ext2fs library.
Mark the filesystem fs as being dirty; this will cause
the superblock information to be flushed out when ext2fs_close
is
called. ext2fs_mark_super_dirty
will also set the filesystem
changed flag. The dirty flag is automatically cleared by
ext2fs_flush
when the superblock is written to disk.
This flag indicates whether or not the filesystem is free of errors. It is not used by libext2fs, and is solely for the application’s convenience.
These flags indicate whether or not the inode or block bitmaps have been modified. If the flag is set, it will cause the appropriate bitmap to be written when the filesystem is closed or flushed.
Next: Inode Functions, Previous: Filesystem-level functions, Up: EXT2FS Library Functions [Contents][Index]
The following functions provide a convenient abstraction to read or write a file in an filesystem. The interface is similar in spirit to the Linux/POSIX file I/O system calls.
Next: Reading and writing data, Previous: File I/O Functions, Up: File I/O Functions [Contents][Index]
The file handle functions much like a file descriptor in the Linux/POSIX
file I/O system calls. Unlike the Linux/POSIX system calls, files are
opened via inode numbers instead of via pathnames. To resolve a
pathname to an inode number, use the function ext2fs_namei
or to
create a new file, use ext2fs_new_inode
and ext2fs_link
.
Opens a file identified by inode number ino in filesystem fs and returns a file handle in ret. If an inode structure is provided in inode, then it is used instead of reading the inode from the filesystem.
The flags argument contains a bitmask of flags which control how the file should be opened.
EXT2_FILE_WRITE
Open the file for reading and writing. Without this flag, the file is opened for writing only.
EXT2_FILE_CREATE
Create the file if it is not already present.
Return the filesystem handle where the open file file was opened.
Close the file handle file.
Force any data written via ext2fs_file_write
to disk.
Next: Changing the file offset, Previous: File handle manipulation, Up: File I/O Functions [Contents][Index]
Read wanted bytes of data from file store it in the buffer buf. The number of bytes that was actually read is returned via got.
Write wanted bytes of data from the buffer buf to the current file position of file. The number of bytes that was actually written is returned via written.
Next: Getting the file size, Previous: Reading and writing data, Up: File I/O Functions [Contents][Index]
Change the current file position of file according to the directive whence as follows:
EXT2_SEEK_SET
The file position is set to offset bytes from the beginning of the file.
EXT2_SEEK_CUR
The file position set to its current location plus offset bytes.
EXT2_SEEK_END
The file position is set to the size of the file plus offset bytes.
The current offset is returned via ret_pos.
Previous: Changing the file offset, Up: File I/O Functions [Contents][Index]
Return the size of the file file in ret_size.
Return the size of the file file.
Next: Directory functions, Previous: File I/O Functions, Up: EXT2FS Library Functions [Contents][Index]
Next: Iterating over inodes in a filesystem, Previous: Inode Functions, Up: Inode Functions [Contents][Index]
Read the inode number ino into inode.
Write inode to inode ino.
Next: Iterating over blocks in an inode, Previous: Reading and writing inodes, Up: Inode Functions [Contents][Index]
The inode_scan abstraction is useful for iterating over all the inodes in a filesystem.
Initialize the iteration variable scan. This variable is used by
ext2fs_get_next_inode
. The buffer_blocks parameter
controls how many blocks of the inode table are read in at a time. A
large number of blocks requires more memory, but reduces the overhead in
seeking and reading from the disk. If buffer_blocks is zero, a
suitable default value will be used.
Release the memory associated with scan and invalidate it.
This function returns the next inode from the filesystem; the inode number of the inode is stored in ino, and the inode is stored in inode.
If the inode is located in a block that has been marked as bad,
ext2fs_get_next_inode
will return the error
EXT2_ET_BAD_BLOCK_IN_INODE_TABLE
.
Start the inode scan at a particular ext2 blockgroup, group. This function may be safely called at any time while scan is valid.
Register a callback function which will be called by
ext2_get_next_inode
when all of the inodes in a block group have
been processed.
Set the scan_flags set_flags and clear the scan_flags clear_flags. The following flags can be set using this interface:
When a block group is missing an inode table, skip it. If this flag is
not set ext2fs_get_next_inode
will return the error
EXT2_ET_MISSING_INODE_TABLE.
Next: Convenience functions for Inodes, Previous: Iterating over inodes in a filesystem, Up: Inode Functions [Contents][Index]
Iterate over all of the blocks in inode number ino in filesystem
fs, by calling the function func for each block in the
inode. The block_buf parameter should either be NULL, or if the
ext2fs_block_iterate
function is called repeatedly, the overhead
of allocating and freeing scratch memory can be avoided by passing a
pointer to a scratch buffer which must be at least as big as three times the
filesystem’s blocksize.
The flags parameter controls how the iterator will function:
This flag indicates that the interator function should be called on blocks where the block number is zero (also known as “holes”.) It is also known as BLOCK_FLAG_APPEND, since it is also used by functions such as ext2fs_expand_dir() to add a new block to an inode.
This flag indicates that the iterator function for the indirect, doubly indirect, etc. blocks should be called after all of the blocks contained in the indirect blocks are processed. This is useful if you are going to be deallocating blocks from an inode.
This flag indicates that the iterator function should be called for data blocks only.
The callback function func is called with a number of parameters;
the fs and private parameters are self-explanatory, and
their values are taken from the parameters to
ext2fs_block_iterate
. (The private data structure is
generally used by callers to ext2fs_block_iterate
so that some
private data structure can be passed to the callback function. The
blockcnt parameter, if non-negative, indicates the logical block
number of a data block in the inode. If blockcnt is less than
zero, then func was called on a metadata block, and blockcnt
will be one of the following values: BLOCK_COUNT_IND, BLOCK_COUNT_DIND,
BLOCK_COUNT_TIND, or BLOCK_COUNT_TRANSLATOR. The blocknr is a
pointer to the inode or indirect block entry listing physical block
number. The callback function may modify the physical block number, if
it returns the BLOCK_CHANGED flag.
The callback function func returns a result code which is composed of the logical OR of the following flags:
This flag indicates that callback function has modified the physical block number pointed to by blocknr.
This flag requests that ext2fs_block_iterate
to stop immediately
and return to the caller.
This function is much like ext2fs_block_iterate
, except that the
blockcnt type is a 64-bit signed quantity, to support larger
files, and the addition of the ref_blk and ref_offset
arguments passed to the callback function, which identify the location
of the physical block pointed to by pointer blocknr. If
ref_blk is zero, then ref_offset contains the offset into
the i_blocks
array. If ref_blk is non-zero, then the physical
block location is contained inside an indirect block group, and
ref_offset contains the offset into the indirect block.
Previous: Iterating over blocks in an inode, Up: Inode Functions [Contents][Index]
Returns an array of blocks corresponding to the direct, indirect, doubly indirect, and triply indirect blocks as stored in the inode structure.
Returns 0 if ino is a directory, and ENOTDIR
if it is not.
Returns 1 if the inode’s block entries actually valid block entries, and 0 if not. Inodes which represent devices and fast symbolic links do not contain valid block entries.
Next: Bitmap Functions, Previous: Inode Functions, Up: EXT2FS Library Functions [Contents][Index]
Next: Iterating over a directory, Previous: Directory functions, Up: Directory functions [Contents][Index]
This function reads a directory block, performing byte swapping if necessary.
This function writes a directory block, performing byte swapping if necessary.
This function creates a new directory block in block. If dir_ino is non-zero, then dir_ino and parent_ino are used to initialize directory entries for . and .., respectively.
Next: Creating and expanding directories, Previous: Directory block functions, Up: Directory functions [Contents][Index]
This function iterates over all of the directory entries in the
directory dir, calling the callback function func for each
directory entry in the directory. The block_buf parameter should
either be NULL, or if the ext2fs_dir_iterate
function is
called repeatedly, the overhead of allocating and freeing
scratch memory can be avoided by passing a pointer to a scratch buffer
which must be at least as big as the filesystem’s blocksize.
The flags parameter controls how the iterator will function:
This flag indicates that the callback function should be called even for deleted or empty directory entries.
Next: Creating and removing directory entries, Previous: Iterating over a directory, Up: Directory functions [Contents][Index]
This function creates a new directory. If inum is zero, then a new inode will be allocated; otherwise, the directory will be created in the inode specified by inum. If name specifies the name of the new directory; if it is non-NULL, then the new directory will be linked into the parent directory parent.
This function adds a new empty directory block and appends it to
the directory dir. This allows functions such as
ext2fs_link
to add new directory entries to a directory which is full.
Next: Looking up filenames, Previous: Creating and expanding directories, Up: Directory functions [Contents][Index]
This function adds a new directory entry to the directory dir, with name and ino specifying the name and inode number in the directory entry, respectively.
The low 3 bits of the flags field is used to specify the file type of inode: (No other flags are currently defined.)
The file type is unknown.
The file type is a normal file.
The file type is a directory.
The file type is a character device.
The file type is a block device.
The file type is a named pipe.
The file type is a unix domain socket.
The file type is a symbolic link.
This function removes a directory entry from dir.
The directory entry to be removed is the first one which is
matched by name and ino. If name is non-NULL,
the directory entry’s name must match name. If ino is
non-zero, the directory entry’s inode number must match ino.
No flags are currently defined for ext2fs_unlink
; callers should
pass in zero to this parameter.
Next: Translating inode numbers to filenames, Previous: Creating and removing directory entries, Up: Directory functions [Contents][Index]
Previous: Looking up filenames, Up: Directory functions [Contents][Index]
Next: EXT2 data abstractions, Previous: Directory functions, Up: EXT2FS Library Functions [Contents][Index]
Next: Allocating Bitmaps, Previous: Bitmap Functions, Up: Bitmap Functions [Contents][Index]
Next: Freeing bitmaps, Previous: Reading and Writing Bitmaps, Up: Bitmap Functions [Contents][Index]
Next: Bitmap Operations, Previous: Allocating Bitmaps, Up: Bitmap Functions [Contents][Index]
Next: Comparing bitmaps, Previous: Freeing bitmaps, Up: Bitmap Functions [Contents][Index]
These functions set, clear, and test bits in a block bitmap bitmap.
These functions set, clear, and test bits in an inode bitmap bitmap.
These “fast” functions are like their normal counterparts; however, they are implemented as inline functions and do not perform bounds checks on the inode number or block number; they are assumed to be correct. They should only be used in speed-critical applications, where the inode or block number has already been validated by other means.
Return the first inode or block which is stored in the bitmap.
Return the last inode or block which is stored in the bitmap.
Next: Modifying Bitmaps, Previous: Bitmap Operations, Up: Bitmap Functions [Contents][Index]
Next: Resizing Bitmaps, Previous: Comparing bitmaps, Up: Bitmap Functions [Contents][Index]
Next: Clearing Bitmaps, Previous: Modifying Bitmaps, Up: Bitmap Functions [Contents][Index]
Previous: Resizing Bitmaps, Up: Bitmap Functions [Contents][Index]
This function sets all of the bits in the inode bitmap bitmap to be zero.
This function sets all of the bits in the block bitmap bitmap to be zero.
Next: Byte-swapping functions, Previous: Bitmap Functions, Up: EXT2FS Library Functions [Contents][Index]
The ext2 library has a number of abstractions which are useful for ext2 utility programs.
Next: Directory-block list management, Previous: EXT2 data abstractions, Up: EXT2 data abstractions [Contents][Index]
Next: Inode count functions, Previous: Badblocks list management, Up: EXT2 data abstractions [Contents][Index]
The dblist abstraction stores a list of blocks belonging to
directories. This list can be useful when a program needs to interate
over all directory entries in a filesystem; e2fsck
does this in
pass 2 of its operations, and debugfs
needs to do this when it is
trying to turn an inode number into a pathname.
Creates a dblist data structure and returns it in ret_dblist.
Free a dblist data structure.
Add an entry to the dblist data structure. This call records the fact that block number blockcnt of directory inode ino is stored in block blk.
Change an entry in the dblist data structure; this changes the location of block number blockcnt of directory inode ino to be block blk.
This iterator calls func for every entry in the dblist data structure.
This iterator takes reads in the directory block indicated in each dblist entry, and calls func for each directory entry in each directory block. If dblist contains all the directory blocks in a filesystem, this function provides a convenient way to iterate over all directory entries for that filesystem.
Previous: Directory-block list management, Up: EXT2 data abstractions [Contents][Index]
The icount abstraction is a specialized data type used by e2fsck
to store how many times a particular inode is referenced by the
filesystem. This is used twice; once to store the actual number of times
that the inode is reference; and once to store the claimed number of times
the inode is referenced according to the inode structure.
This abstraction is designed to be extremely efficient for storing this sort of information, by taking advantage of the following properties of inode counts, namely (1) inode counts are very often zero (because the inode is currently not in use), and (2) many files have a inode count of 1 (because they are a file which has no additional hard links).
Creates an icount structure for a filesystem fs, with initial space
for size inodes whose count is greater than 1. The flags
parameter is either 0 or EXT2_ICOUNT_OPT_INCREMENT
, which
indicates that icount structure should be able to increment inode counts
quickly. The icount structure is returned in ret. The returned
icount structure initially has a count of zero for all inodes.
The hint parameter allows the caller to optionally pass in another icount structure which is used to initialize the array of inodes whose count is greater than 1. It is used purely as a speed optimization so that the icount structure can determine in advance which inodes are likely to contain a count grater than 1.
Frees an icount structure.
Returns in ret the count for a particular inode ino.
Increments the ref count for inode ino.
Decrements the ref count for inode ino.
Sets the reference count for inode ino to be count.
Returns the current number of inodes in icount which has a count greater than 1.
Validates the internal rep invariant of icount; if there are any problems, print out debugging information to f. This function is intended for debugging and testing use only.
Next: Other functions, Previous: EXT2 data abstractions, Up: EXT2FS Library Functions [Contents][Index]
Previous: Byte-swapping functions, Up: EXT2FS Library Functions [Contents][Index]
/* alloc.c */
/* check_desc.c */
/* getsize.c */
/* ismounted.c */
/* version.c */
This function returns the current version of the ext2 library. The return value contains an integer version code, which consists of the major version number of the library multiplied by 100, plus the minor version number of the library. Hence, if the library version is 1.08, the returned value will be 108.
If ver_string and/or date_string are non-NULL, they will be set to point at a constant string containing the library version and/or release date, respectively.
This function takes a version string which may included in an
application and returns a version code using the same algorithm used by
ext2fs_get_library_version
. It can be used by programs included
in the e2fsprogs
distribution to assure that they are using an
up-to-date ext2 shared library.
/* inline functions */
This function returns the block group which contains the block blk.
This function returns the block group which contains the inode ino.
Next: Function and Type Index, Previous: EXT2FS Library Functions, Up: The EXT2FS Library [Contents][Index]
Previous: Concept Index, Up: The EXT2FS Library [Contents][Index]
Jump to: | E |
---|
Jump to: | E |
---|