2000-10-24 22:33:16 +04:00
|
|
|
/*
|
|
|
|
* mkjournal.c --- make a journal for a filesystem
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000 Theodore Ts'o.
|
2008-08-28 07:07:54 +04:00
|
|
|
*
|
2000-10-24 22:33:16 +04:00
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the GNU Public
|
|
|
|
* License.
|
|
|
|
* %End-Header%
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#if HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
2001-06-11 04:49:29 +04:00
|
|
|
#if HAVE_SYS_IOCTL_H
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
2000-10-27 00:45:58 +04:00
|
|
|
#if HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
2000-10-24 22:33:16 +04:00
|
|
|
|
|
|
|
#include "ext2_fs.h"
|
2001-01-06 01:23:22 +03:00
|
|
|
#include "e2p/e2p.h"
|
2000-10-24 22:33:16 +04:00
|
|
|
#include "ext2fs.h"
|
2000-12-09 08:47:45 +03:00
|
|
|
#include "jfs_user.h"
|
2000-10-24 22:33:16 +04:00
|
|
|
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
/*
|
|
|
|
* This function automatically sets up the journal superblock and
|
|
|
|
* returns it as an allocated block.
|
|
|
|
*/
|
|
|
|
errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
|
|
|
|
__u32 size, int flags,
|
|
|
|
char **ret_jsb)
|
2000-10-24 22:33:16 +04:00
|
|
|
{
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
errcode_t retval;
|
|
|
|
journal_superblock_t *jsb;
|
2001-04-17 04:53:25 +04:00
|
|
|
|
|
|
|
if (size < 1024)
|
|
|
|
return EXT2_ET_JOURNAL_TOO_SMALL;
|
|
|
|
|
2003-08-01 17:41:07 +04:00
|
|
|
if ((retval = ext2fs_get_mem(fs->blocksize, &jsb)))
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
return retval;
|
|
|
|
|
|
|
|
memset (jsb, 0, fs->blocksize);
|
2000-12-09 08:47:45 +03:00
|
|
|
|
2000-10-24 22:33:16 +04:00
|
|
|
jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
|
2000-12-09 17:39:16 +03:00
|
|
|
if (flags & EXT2_MKJOURNAL_V1_SUPER)
|
|
|
|
jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V1);
|
|
|
|
else
|
|
|
|
jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
jsb->s_blocksize = htonl(fs->blocksize);
|
2000-10-24 22:33:16 +04:00
|
|
|
jsb->s_maxlen = htonl(size);
|
2001-02-08 06:37:16 +03:00
|
|
|
jsb->s_nr_users = htonl(1);
|
2000-10-24 22:33:16 +04:00
|
|
|
jsb->s_first = htonl(1);
|
|
|
|
jsb->s_sequence = htonl(1);
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
memcpy(jsb->s_uuid, fs->super->s_uuid, sizeof(fs->super->s_uuid));
|
|
|
|
/*
|
2001-02-08 06:37:16 +03:00
|
|
|
* If we're creating an external journal device, we need to
|
|
|
|
* adjust these fields.
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
*/
|
|
|
|
if (fs->super->s_feature_incompat &
|
2001-07-28 07:33:40 +04:00
|
|
|
EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
jsb->s_nr_users = 0;
|
2001-07-28 07:33:40 +04:00
|
|
|
if (fs->blocksize == 1024)
|
|
|
|
jsb->s_first = htonl(3);
|
|
|
|
else
|
|
|
|
jsb->s_first = htonl(2);
|
|
|
|
}
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
|
|
|
|
*ret_jsb = (char *) jsb;
|
|
|
|
return 0;
|
2000-10-24 22:33:16 +04:00
|
|
|
}
|
2001-01-03 22:22:42 +03:00
|
|
|
|
2000-10-24 22:33:16 +04:00
|
|
|
/*
|
2001-01-03 22:22:42 +03:00
|
|
|
* This function writes a journal using POSIX routines. It is used
|
|
|
|
* for creating external journals and creating journals on live
|
|
|
|
* filesystems.
|
2000-10-24 22:33:16 +04:00
|
|
|
*/
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
static errcode_t write_journal_file(ext2_filsys fs, char *filename,
|
2000-12-09 17:39:16 +03:00
|
|
|
blk_t size, int flags)
|
2000-10-24 22:33:16 +04:00
|
|
|
{
|
|
|
|
errcode_t retval;
|
|
|
|
char *buf = 0;
|
2003-12-07 09:28:50 +03:00
|
|
|
int fd, ret_size;
|
|
|
|
blk_t i;
|
2000-10-24 22:33:16 +04:00
|
|
|
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
|
|
|
|
return retval;
|
2000-10-24 22:33:16 +04:00
|
|
|
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
/* Open the device or journal file */
|
|
|
|
if ((fd = open(filename, O_WRONLY)) < 0) {
|
2000-10-24 22:33:16 +04:00
|
|
|
retval = errno;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the superblock out */
|
|
|
|
retval = EXT2_ET_SHORT_WRITE;
|
|
|
|
ret_size = write(fd, buf, fs->blocksize);
|
|
|
|
if (ret_size < 0) {
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
retval = errno;
|
2000-10-24 22:33:16 +04:00
|
|
|
goto errout;
|
|
|
|
}
|
2003-12-07 09:28:50 +03:00
|
|
|
if (ret_size != (int) fs->blocksize)
|
2000-10-24 22:33:16 +04:00
|
|
|
goto errout;
|
|
|
|
memset(buf, 0, fs->blocksize);
|
2001-01-03 22:22:42 +03:00
|
|
|
|
|
|
|
for (i = 1; i < size; i++) {
|
2000-10-24 22:33:16 +04:00
|
|
|
ret_size = write(fd, buf, fs->blocksize);
|
|
|
|
if (ret_size < 0) {
|
|
|
|
retval = errno;
|
|
|
|
goto errout;
|
|
|
|
}
|
2003-12-07 09:28:50 +03:00
|
|
|
if (ret_size != (int) fs->blocksize)
|
2000-10-24 22:33:16 +04:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2001-01-03 22:22:42 +03:00
|
|
|
retval = 0;
|
2000-10-24 22:33:16 +04:00
|
|
|
errout:
|
2003-08-01 17:41:07 +04:00
|
|
|
ext2fs_free_mem(&buf);
|
2000-10-24 22:33:16 +04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-04-28 00:10:32 +04:00
|
|
|
/*
|
|
|
|
* Convenience function which zeros out _num_ blocks starting at
|
|
|
|
* _blk_. In case of an error, the details of the error is returned
|
|
|
|
* via _ret_blk_ and _ret_count_ if they are non-NULL pointers.
|
|
|
|
* Returns 0 on success, and an error code on an error.
|
|
|
|
*
|
|
|
|
* As a special case, if the first argument is NULL, then it will
|
|
|
|
* attempt to free the static zeroizing buffer. (This is to keep
|
|
|
|
* programs that check for memory leaks happy.)
|
|
|
|
*/
|
|
|
|
#define STRIDE_LENGTH 8
|
2009-06-02 00:15:40 +04:00
|
|
|
errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
|
|
|
|
blk64_t *ret_blk, int *ret_count)
|
2008-04-28 00:10:32 +04:00
|
|
|
{
|
|
|
|
int j, count;
|
|
|
|
static char *buf;
|
|
|
|
errcode_t retval;
|
|
|
|
|
|
|
|
/* If fs is null, clean up the static buffer and return */
|
|
|
|
if (!fs) {
|
|
|
|
if (buf) {
|
|
|
|
free(buf);
|
|
|
|
buf = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Allocate the zeroizing buffer if necessary */
|
|
|
|
if (!buf) {
|
|
|
|
buf = malloc(fs->blocksize * STRIDE_LENGTH);
|
|
|
|
if (!buf)
|
|
|
|
return ENOMEM;
|
|
|
|
memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
|
|
|
|
}
|
|
|
|
/* OK, do the write loop */
|
|
|
|
j=0;
|
|
|
|
while (j < num) {
|
2008-06-17 08:18:58 +04:00
|
|
|
if (blk % STRIDE_LENGTH) {
|
2008-04-28 00:10:32 +04:00
|
|
|
count = STRIDE_LENGTH - (blk % STRIDE_LENGTH);
|
2008-06-17 08:18:58 +04:00
|
|
|
if (count > (num - j))
|
|
|
|
count = num - j;
|
|
|
|
} else {
|
2008-04-28 00:10:32 +04:00
|
|
|
count = num - j;
|
|
|
|
if (count > STRIDE_LENGTH)
|
|
|
|
count = STRIDE_LENGTH;
|
|
|
|
}
|
2009-06-02 00:15:40 +04:00
|
|
|
retval = io_channel_write_blk64(fs->io, blk, count, buf);
|
2008-04-28 00:10:32 +04:00
|
|
|
if (retval) {
|
|
|
|
if (ret_count)
|
|
|
|
*ret_count = count;
|
|
|
|
if (ret_blk)
|
|
|
|
*ret_blk = blk;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
j += count; blk += count;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-02 00:15:40 +04:00
|
|
|
errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
|
|
|
|
blk_t *ret_blk, int *ret_count)
|
|
|
|
{
|
|
|
|
blk64_t ret_blk2;
|
|
|
|
errcode_t retval;
|
|
|
|
|
|
|
|
retval = ext2fs_zero_blocks2(fs, blk, num, &ret_blk2, ret_count);
|
|
|
|
if (retval)
|
|
|
|
*ret_blk = (blk_t) ret_blk2;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2000-10-24 22:33:16 +04:00
|
|
|
/*
|
2001-01-03 22:22:42 +03:00
|
|
|
* Helper function for creating the journal using direct I/O routines
|
2000-10-24 22:33:16 +04:00
|
|
|
*/
|
|
|
|
struct mkjournal_struct {
|
|
|
|
int num_blocks;
|
|
|
|
int newblocks;
|
2008-08-27 23:11:28 +04:00
|
|
|
blk_t goal;
|
2008-04-28 00:10:32 +04:00
|
|
|
blk_t blk_to_zero;
|
|
|
|
int zero_count;
|
2000-10-24 22:33:16 +04:00
|
|
|
char *buf;
|
|
|
|
errcode_t err;
|
|
|
|
};
|
|
|
|
|
2003-12-07 09:28:50 +03:00
|
|
|
static int mkjournal_proc(ext2_filsys fs,
|
|
|
|
blk_t *blocknr,
|
|
|
|
e2_blkcnt_t blockcnt,
|
|
|
|
blk_t ref_block EXT2FS_ATTR((unused)),
|
|
|
|
int ref_offset EXT2FS_ATTR((unused)),
|
|
|
|
void *priv_data)
|
2000-10-24 22:33:16 +04:00
|
|
|
{
|
|
|
|
struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
|
|
|
|
blk_t new_blk;
|
|
|
|
errcode_t retval;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2000-10-24 22:33:16 +04:00
|
|
|
if (*blocknr) {
|
2008-08-27 23:11:28 +04:00
|
|
|
es->goal = *blocknr;
|
2000-10-24 22:33:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2008-08-27 23:11:28 +04:00
|
|
|
retval = ext2fs_new_block(fs, es->goal, 0, &new_blk);
|
2000-10-24 22:33:16 +04:00
|
|
|
if (retval) {
|
|
|
|
es->err = retval;
|
|
|
|
return BLOCK_ABORT;
|
|
|
|
}
|
2008-08-28 00:23:30 +04:00
|
|
|
if (blockcnt >= 0)
|
2000-10-24 22:33:16 +04:00
|
|
|
es->num_blocks--;
|
|
|
|
|
|
|
|
es->newblocks++;
|
2008-04-28 00:10:32 +04:00
|
|
|
retval = 0;
|
|
|
|
if (blockcnt <= 0)
|
2009-09-08 05:14:24 +04:00
|
|
|
retval = io_channel_write_blk64(fs->io, new_blk, 1, es->buf);
|
2008-04-28 00:10:32 +04:00
|
|
|
else {
|
|
|
|
if (es->zero_count) {
|
|
|
|
if ((es->blk_to_zero + es->zero_count == new_blk) &&
|
|
|
|
(es->zero_count < 1024))
|
|
|
|
es->zero_count++;
|
|
|
|
else {
|
|
|
|
retval = ext2fs_zero_blocks(fs,
|
|
|
|
es->blk_to_zero,
|
|
|
|
es->zero_count,
|
|
|
|
0, 0);
|
|
|
|
es->zero_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (es->zero_count == 0) {
|
|
|
|
es->blk_to_zero = new_blk;
|
|
|
|
es->zero_count = 1;
|
|
|
|
}
|
|
|
|
}
|
2000-10-24 22:33:16 +04:00
|
|
|
|
|
|
|
if (blockcnt == 0)
|
|
|
|
memset(es->buf, 0, fs->blocksize);
|
|
|
|
|
|
|
|
if (retval) {
|
|
|
|
es->err = retval;
|
|
|
|
return BLOCK_ABORT;
|
|
|
|
}
|
2008-08-27 23:11:28 +04:00
|
|
|
*blocknr = es->goal = new_blk;
|
dir_iterate.c (ext2fs_dir_iterate2, ext2fs_process_dir_block):
Add support for a new flag, DIRENT_FLAG_INCLUDE_REMOVED,
which will return deleted directory entries.
ext2fs_dir_iterate2 takes a new callback function which
is identical with the one used by
ext2fs_dblist_dir_iterate(). If the directory entry is
deleted, the callback function will be called with the
entry paraemter set to DIRENT_DELETED_FILE.
Makefile.in, alloc_stats.c (ext2fs_inode_alloc_stats,
ext2fs_block_alloc_stats): New functions which update
block/inode allocation statistics in the bitmaps, block
group descriptors, and superblock.
mkjournal.c (mkjournal_proc), mkdir.c (ext2fs_mkdir),
expanddir.c (expand_dir_proc), bb_inode.c
(clear_bad_block_proc, set_bad_block_proc,
ext2fs_update_bb_inode), alloc.c (ext2fs_alloc_block):
Update to use new block/inode allocation statistics.
2002-01-03 11:29:19 +03:00
|
|
|
ext2fs_block_alloc_stats(fs, new_blk, +1);
|
2000-10-24 22:33:16 +04:00
|
|
|
|
|
|
|
if (es->num_blocks == 0)
|
|
|
|
return (BLOCK_CHANGED | BLOCK_ABORT);
|
|
|
|
else
|
|
|
|
return BLOCK_CHANGED;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2000-10-24 22:33:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-01-03 22:22:42 +03:00
|
|
|
* This function creates a journal using direct I/O routines.
|
2000-10-24 22:33:16 +04:00
|
|
|
*/
|
Many files:
alloc.c, bb_inode.c, bitmaps.c, bitops.h, block.c, bmap.c, bmove.c,
brel.h, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c,
expanddir.c, ext2fs.h, ext2fsP.h, fileio.c, finddev.c, get_pathname.c,
icount.c, inode.c, irel.h, irel_ma.c, ismounted.c, link.c, lookup.c,
mkdir.c, mkjournal.c, namei.c, newdir.c, read_bb_file.c, test_io.c,
tst_iscan.c, unix_io.c, unlink.c: Change use of ino_t to ext2_ino_t,
to protect applications that attempt to compile
-D_FILE_OFFSET_BITS=64, since this inexplicably changes ino_t(!?). So
we use ext2_ino_t to avoid an unexpected ABI change.
2001-01-11 07:54:39 +03:00
|
|
|
static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
|
2001-01-03 22:22:42 +03:00
|
|
|
blk_t size, int flags)
|
2000-10-24 22:33:16 +04:00
|
|
|
{
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
char *buf;
|
2008-08-28 18:20:43 +04:00
|
|
|
dgrp_t group, start, end, i, log_flex;
|
2000-10-24 22:33:16 +04:00
|
|
|
errcode_t retval;
|
|
|
|
struct ext2_inode inode;
|
|
|
|
struct mkjournal_struct es;
|
|
|
|
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
|
|
|
|
return retval;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2000-10-24 22:33:16 +04:00
|
|
|
if ((retval = ext2fs_read_bitmaps(fs)))
|
|
|
|
return retval;
|
|
|
|
|
2001-01-03 22:22:42 +03:00
|
|
|
if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
|
2000-10-24 22:33:16 +04:00
|
|
|
return retval;
|
|
|
|
|
|
|
|
if (inode.i_blocks > 0)
|
|
|
|
return EEXIST;
|
|
|
|
|
|
|
|
es.num_blocks = size;
|
|
|
|
es.newblocks = 0;
|
|
|
|
es.buf = buf;
|
|
|
|
es.err = 0;
|
2008-04-28 00:10:32 +04:00
|
|
|
es.zero_count = 0;
|
2000-10-24 22:33:16 +04:00
|
|
|
|
2008-08-27 23:50:44 +04:00
|
|
|
if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
|
|
|
|
inode.i_flags |= EXT4_EXTENTS_FL;
|
|
|
|
if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-08-27 23:11:28 +04:00
|
|
|
/*
|
|
|
|
* Set the initial goal block to be roughly at the middle of
|
|
|
|
* the filesystem. Pick a group that has the largest number
|
|
|
|
* of free blocks.
|
|
|
|
*/
|
2008-08-28 07:07:54 +04:00
|
|
|
group = ext2fs_group_of_blk(fs, (fs->super->s_blocks_count -
|
2008-08-27 23:11:28 +04:00
|
|
|
fs->super->s_first_data_block) / 2);
|
2008-08-28 18:20:43 +04:00
|
|
|
log_flex = 1 << fs->super->s_log_groups_per_flex;
|
|
|
|
if (fs->super->s_log_groups_per_flex && (group > log_flex)) {
|
|
|
|
group = group & ~(log_flex - 1);
|
|
|
|
while ((group < fs->group_desc_count) &&
|
|
|
|
fs->group_desc[group].bg_free_blocks_count == 0)
|
|
|
|
group++;
|
|
|
|
if (group == fs->group_desc_count)
|
|
|
|
group = 0;
|
|
|
|
start = group;
|
|
|
|
} else
|
|
|
|
start = (group > 0) ? group-1 : group;
|
2008-08-27 23:11:28 +04:00
|
|
|
end = ((group+1) < fs->group_desc_count) ? group+1 : group;
|
|
|
|
group = start;
|
|
|
|
for (i=start+1; i <= end; i++)
|
|
|
|
if (fs->group_desc[i].bg_free_blocks_count >
|
|
|
|
fs->group_desc[group].bg_free_blocks_count)
|
|
|
|
group = i;
|
|
|
|
|
|
|
|
es.goal = (fs->super->s_blocks_per_group * group) +
|
|
|
|
fs->super->s_first_data_block;
|
|
|
|
|
2001-01-03 22:22:42 +03:00
|
|
|
retval = ext2fs_block_iterate2(fs, journal_ino, BLOCK_FLAG_APPEND,
|
2000-10-24 22:33:16 +04:00
|
|
|
0, mkjournal_proc, &es);
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
if (es.err) {
|
|
|
|
retval = es.err;
|
|
|
|
goto errout;
|
|
|
|
}
|
2008-04-28 00:10:32 +04:00
|
|
|
if (es.zero_count) {
|
|
|
|
retval = ext2fs_zero_blocks(fs, es.blk_to_zero,
|
|
|
|
es.zero_count, 0, 0);
|
|
|
|
if (retval)
|
|
|
|
goto errout;
|
|
|
|
}
|
2000-10-24 22:33:16 +04:00
|
|
|
|
2001-01-03 22:22:42 +03:00
|
|
|
if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
goto errout;
|
2000-10-24 22:33:16 +04:00
|
|
|
|
|
|
|
inode.i_size += fs->blocksize * size;
|
2008-04-09 19:39:11 +04:00
|
|
|
ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
|
2005-09-25 04:14:51 +04:00
|
|
|
inode.i_mtime = inode.i_ctime = fs->now ? fs->now : time(0);
|
2000-10-24 22:33:16 +04:00
|
|
|
inode.i_links_count = 1;
|
|
|
|
inode.i_mode = LINUX_S_IFREG | 0600;
|
|
|
|
|
2009-06-17 06:50:53 +04:00
|
|
|
if ((retval = ext2fs_write_new_inode(fs, journal_ino, &inode)))
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
goto errout;
|
|
|
|
retval = 0;
|
2000-10-24 22:33:16 +04:00
|
|
|
|
2003-08-21 08:40:26 +04:00
|
|
|
memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
|
|
|
|
fs->super->s_jnl_blocks[16] = inode.i_size;
|
|
|
|
fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
|
|
|
|
ext2fs_mark_super_dirty(fs);
|
|
|
|
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
errout:
|
2003-08-01 17:41:07 +04:00
|
|
|
ext2fs_free_mem(&buf);
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
return retval;
|
2001-01-03 22:22:42 +03:00
|
|
|
}
|
|
|
|
|
2007-06-21 19:59:06 +04:00
|
|
|
/*
|
|
|
|
* Find a reasonable journal file size (in blocks) given the number of blocks
|
|
|
|
* in the filesystem. For very small filesystems, it is not reasonable to
|
|
|
|
* have a journal that fills more than half of the filesystem.
|
|
|
|
*/
|
|
|
|
int ext2fs_default_journal_size(__u64 blocks)
|
|
|
|
{
|
|
|
|
if (blocks < 2048)
|
|
|
|
return -1;
|
|
|
|
if (blocks < 32768)
|
|
|
|
return (1024);
|
|
|
|
if (blocks < 256*1024)
|
|
|
|
return (4096);
|
|
|
|
if (blocks < 512*1024)
|
|
|
|
return (8192);
|
|
|
|
if (blocks < 1024*1024)
|
|
|
|
return (16384);
|
|
|
|
return 32768;
|
|
|
|
}
|
|
|
|
|
2001-01-03 22:22:42 +03:00
|
|
|
/*
|
|
|
|
* This function adds a journal device to a filesystem
|
|
|
|
*/
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
|
2001-01-03 22:22:42 +03:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
errcode_t retval;
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
char buf[1024];
|
|
|
|
journal_superblock_t *jsb;
|
2003-12-07 09:28:50 +03:00
|
|
|
int start;
|
|
|
|
__u32 i, nr_users;
|
2001-01-03 22:22:42 +03:00
|
|
|
|
|
|
|
/* Make sure the device exists and is a block device */
|
2001-01-18 04:44:19 +03:00
|
|
|
if (stat(journal_dev->device_name, &st) < 0)
|
2001-01-03 22:22:42 +03:00
|
|
|
return errno;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2001-01-03 22:22:42 +03:00
|
|
|
if (!S_ISBLK(st.st_mode))
|
2001-04-17 04:53:25 +04:00
|
|
|
return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
|
2001-01-03 22:22:42 +03:00
|
|
|
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
/* Get the journal superblock */
|
2001-07-27 07:44:39 +04:00
|
|
|
start = 1;
|
|
|
|
if (journal_dev->blocksize == 1024)
|
|
|
|
start++;
|
2009-09-08 05:14:24 +04:00
|
|
|
if ((retval = io_channel_read_blk64(journal_dev->io, start, -1024,
|
|
|
|
buf)))
|
2001-01-03 22:22:42 +03:00
|
|
|
return retval;
|
|
|
|
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
jsb = (journal_superblock_t *) buf;
|
|
|
|
if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
|
|
|
|
(jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2)))
|
2001-04-17 04:53:25 +04:00
|
|
|
return EXT2_ET_NO_JOURNAL_SB;
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
|
2003-12-07 09:28:50 +03:00
|
|
|
if (ntohl(jsb->s_blocksize) != (unsigned long) fs->blocksize)
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
|
|
|
|
|
|
|
|
/* Check and see if this filesystem has already been added */
|
|
|
|
nr_users = ntohl(jsb->s_nr_users);
|
|
|
|
for (i=0; i < nr_users; i++) {
|
|
|
|
if (memcmp(fs->super->s_uuid,
|
|
|
|
&jsb->s_users[i*16], 16) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= nr_users) {
|
|
|
|
memcpy(&jsb->s_users[nr_users*16],
|
|
|
|
fs->super->s_uuid, 16);
|
|
|
|
jsb->s_nr_users = htonl(nr_users+1);
|
|
|
|
}
|
2001-01-03 22:22:42 +03:00
|
|
|
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
/* Writeback the journal superblock */
|
2009-09-08 05:14:24 +04:00
|
|
|
if ((retval = io_channel_write_blk64(journal_dev->io, start, -1024, buf)))
|
2001-01-03 22:22:42 +03:00
|
|
|
return retval;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2001-01-03 22:22:42 +03:00
|
|
|
fs->super->s_journal_inum = 0;
|
|
|
|
fs->super->s_journal_dev = st.st_rdev;
|
ChangeLog, ext2_err.et.in, ext2fs.h, initialize.c, mkjournal.c, openfs.c:
initialize.c (ext2fs_initialize): Add support for initializing the
ext2 superblock for external journal devices. This basically means we
don't bother to allocate any block group descriptors.
openfs.c (ext2fs_open): Only open external journal devices if the new
flag EXT2_FLAG_JOURNAL_DEV_OK is passed to ext2fs_open. When opening
such devices, don't try to read the block group descriptors, since
they're not there.
ext2_err.et.in (EXT2_NO_JOURNAL_SB): Add new error code
mkjournal.c: Export a new function,
ext2fs_create_journal_superblock(), which allocates and returns a
buffer containing a journal superblock. This is needed by mke2fs to
create an external journal. Rewrote ext2fs_add_journal_device() so
that it no longer creates the external journal, but rather adds a
filesystem to an existing external journal. It handles all of the
UUID manipulation.
ext2fs.h: List the EXT3_FEATURE_JOURNAL_DEV as a flag supported by the
library. Define the EXT2_FLAG_JOURNAL_DEV_OK. Changed function
prototype for ext2fs_add_journal_device().
2001-01-16 09:56:14 +03:00
|
|
|
memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
|
2001-01-03 22:22:42 +03:00
|
|
|
sizeof(fs->super->s_journal_uuid));
|
|
|
|
fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
|
|
|
|
ext2fs_mark_super_dirty(fs);
|
2001-01-06 01:23:22 +03:00
|
|
|
return 0;
|
2001-01-03 22:22:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function adds a journal inode to a filesystem, using either
|
|
|
|
* POSIX routines if the filesystem is mounted, or using direct I/O
|
|
|
|
* functions if it is not.
|
|
|
|
*/
|
|
|
|
errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags)
|
|
|
|
{
|
|
|
|
errcode_t retval;
|
Many files:
alloc.c, bb_inode.c, bitmaps.c, bitops.h, block.c, bmap.c, bmove.c,
brel.h, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c,
expanddir.c, ext2fs.h, ext2fsP.h, fileio.c, finddev.c, get_pathname.c,
icount.c, inode.c, irel.h, irel_ma.c, ismounted.c, link.c, lookup.c,
mkdir.c, mkjournal.c, namei.c, newdir.c, read_bb_file.c, test_io.c,
tst_iscan.c, unix_io.c, unlink.c: Change use of ino_t to ext2_ino_t,
to protect applications that attempt to compile
-D_FILE_OFFSET_BITS=64, since this inexplicably changes ino_t(!?). So
we use ext2_ino_t to avoid an unexpected ABI change.
2001-01-11 07:54:39 +03:00
|
|
|
ext2_ino_t journal_ino;
|
2001-01-03 22:22:42 +03:00
|
|
|
struct stat st;
|
|
|
|
char jfile[1024];
|
2005-12-11 05:50:30 +03:00
|
|
|
int mount_flags, f;
|
|
|
|
int fd = -1;
|
2001-01-03 22:22:42 +03:00
|
|
|
|
2001-01-06 01:23:22 +03:00
|
|
|
if ((retval = ext2fs_check_mount_point(fs->device_name, &mount_flags,
|
|
|
|
jfile, sizeof(jfile)-10)))
|
2001-01-03 22:22:42 +03:00
|
|
|
return retval;
|
|
|
|
|
|
|
|
if (mount_flags & EXT2_MF_MOUNTED) {
|
|
|
|
strcat(jfile, "/.journal");
|
|
|
|
|
2001-11-10 01:34:54 +03:00
|
|
|
/*
|
2008-08-28 07:07:54 +04:00
|
|
|
* If .../.journal already exists, make sure any
|
2001-11-10 01:34:54 +03:00
|
|
|
* immutable or append-only flags are cleared.
|
|
|
|
*/
|
|
|
|
#if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
|
|
|
|
(void) chflags (jfile, 0);
|
|
|
|
#else
|
|
|
|
#if HAVE_EXT2_IOCTLS
|
|
|
|
fd = open(jfile, O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
f = 0;
|
|
|
|
ioctl(fd, EXT2_IOC_SETFLAGS, &f);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2001-01-03 22:22:42 +03:00
|
|
|
/* Create the journal file */
|
|
|
|
if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
if ((retval = write_journal_file(fs, jfile, size, flags)))
|
2001-06-23 05:52:14 +04:00
|
|
|
goto errout;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2001-01-03 22:22:42 +03:00
|
|
|
/* Get inode number of the journal file */
|
2009-08-25 18:07:16 +04:00
|
|
|
if (fstat(fd, &st) < 0) {
|
|
|
|
retval = errno;
|
2001-06-23 05:52:14 +04:00
|
|
|
goto errout;
|
2009-08-25 18:07:16 +04:00
|
|
|
}
|
2001-01-03 22:22:42 +03:00
|
|
|
|
2001-06-23 05:20:47 +04:00
|
|
|
#if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
|
2001-06-11 04:49:29 +04:00
|
|
|
retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
|
|
|
|
#else
|
|
|
|
#if HAVE_EXT2_IOCTLS
|
2009-08-25 18:07:16 +04:00
|
|
|
if (ioctl(fd, EXT2_IOC_GETFLAGS, &f) < 0) {
|
|
|
|
retval = errno;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
f |= EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
|
2001-06-11 04:49:29 +04:00
|
|
|
retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
|
|
|
|
#endif
|
|
|
|
#endif
|
2009-08-25 18:07:16 +04:00
|
|
|
if (retval) {
|
|
|
|
retval = errno;
|
2001-06-23 05:52:14 +04:00
|
|
|
goto errout;
|
2009-08-25 18:07:16 +04:00
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2009-08-25 18:07:16 +04:00
|
|
|
if (close(fd) < 0) {
|
|
|
|
retval = errno;
|
|
|
|
fd = -1;
|
|
|
|
goto errout;
|
|
|
|
}
|
2001-01-03 22:22:42 +03:00
|
|
|
journal_ino = st.st_ino;
|
|
|
|
} else {
|
2006-04-05 03:23:41 +04:00
|
|
|
if ((mount_flags & EXT2_MF_BUSY) &&
|
|
|
|
!(fs->flags & EXT2_FLAG_EXCLUSIVE)) {
|
2005-07-20 00:04:22 +04:00
|
|
|
retval = EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
2001-01-03 22:22:42 +03:00
|
|
|
journal_ino = EXT2_JOURNAL_INO;
|
|
|
|
if ((retval = write_journal_inode(fs, journal_ino,
|
|
|
|
size, flags)))
|
|
|
|
return retval;
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2001-01-03 22:22:42 +03:00
|
|
|
fs->super->s_journal_inum = journal_ino;
|
|
|
|
fs->super->s_journal_dev = 0;
|
|
|
|
memset(fs->super->s_journal_uuid, 0,
|
|
|
|
sizeof(fs->super->s_journal_uuid));
|
2000-10-24 22:33:16 +04:00
|
|
|
fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
|
|
|
|
|
|
|
|
ext2fs_mark_super_dirty(fs);
|
|
|
|
return 0;
|
2001-06-23 05:52:14 +04:00
|
|
|
errout:
|
2005-12-11 05:50:30 +03:00
|
|
|
if (fd > 0)
|
|
|
|
close(fd);
|
2001-06-23 05:52:14 +04:00
|
|
|
return retval;
|
2000-10-24 22:33:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
errcode_t retval;
|
|
|
|
char *device_name;
|
|
|
|
ext2_filsys fs;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
device_name = argv[1];
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2000-10-24 22:33:16 +04:00
|
|
|
retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
|
|
|
|
unix_io_manager, &fs);
|
|
|
|
if (retval) {
|
|
|
|
com_err(argv[0], retval, "while opening %s", device_name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2001-01-03 16:04:12 +03:00
|
|
|
retval = ext2fs_add_journal_inode(fs, 1024);
|
2000-10-24 22:33:16 +04:00
|
|
|
if (retval) {
|
|
|
|
com_err(argv[0], retval, "while adding journal to %s",
|
|
|
|
device_name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
retval = ext2fs_flush(fs);
|
|
|
|
if (retval) {
|
|
|
|
printf("Warning, had trouble writing out superblocks.\n");
|
|
|
|
}
|
|
|
|
ext2fs_close(fs);
|
|
|
|
exit(0);
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2000-10-24 22:33:16 +04:00
|
|
|
}
|
|
|
|
#endif
|