From: Robert Yang <liezhi.yang@windriver.com>
To: <tytso@mit.edu>, <dvhart@linux.intel.com>
Cc: <linux-ext4@vger.kernel.org>
Subject: [PATCH V4 05/11] misc/create_inode.c: copy regular file
Date: Sat, 1 Mar 2014 03:06:09 -0500 [thread overview]
Message-ID: <1393661175-459-6-git-send-email-liezhi.yang@windriver.com> (raw)
In-Reply-To: <1393661175-459-1-git-send-email-liezhi.yang@windriver.com>
The do_write_internal() is used for copying file from native fs to
target, most of the code are from debugfs/debugfs.c, the
debugfs/debugfs.c will be modified to use this function.
Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
Reviewed-by: Darren Hart <dvhart@linux.intel.com>
---
misc/create_inode.c | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 183 insertions(+)
diff --git a/misc/create_inode.c b/misc/create_inode.c
index f845103..98f4a93 100644
--- a/misc/create_inode.c
+++ b/misc/create_inode.c
@@ -8,6 +8,16 @@
# endif
#endif
+/* 64KiB is the minimium blksize to best minimize system call overhead. */
+#ifndef IO_BUFSIZE
+#define IO_BUFSIZE 64*1024
+#endif
+
+/* Block size for `st_blocks' */
+#ifndef S_BLKSIZE
+#define S_BLKSIZE 512
+#endif
+
/* Make a special file which is block, character and fifo */
errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st)
{
@@ -127,9 +137,182 @@ errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
{
}
+static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
+{
+ ext2_file_t e2_file;
+ errcode_t retval;
+ int got;
+ unsigned int written;
+ char *buf;
+ char *ptr;
+ char *zero_buf;
+ int cmp;
+
+ retval = ext2fs_file_open(current_fs, newfile,
+ EXT2_FILE_WRITE, &e2_file);
+ if (retval)
+ return retval;
+
+ retval = ext2fs_get_mem(bufsize, &buf);
+ if (retval) {
+ com_err("copy_file", retval, "can't allocate buffer\n");
+ return retval;
+ }
+
+ /* This is used for checking whether the whole block is zero */
+ retval = ext2fs_get_memzero(bufsize, &zero_buf);
+ if (retval) {
+ com_err("copy_file", retval, "can't allocate buffer\n");
+ ext2fs_free_mem(&buf);
+ return retval;
+ }
+
+ while (1) {
+ got = read(fd, buf, bufsize);
+ if (got == 0)
+ break;
+ if (got < 0) {
+ retval = errno;
+ goto fail;
+ }
+ ptr = buf;
+
+ /* Sparse copy */
+ if (make_holes) {
+ /* Check whether all is zero */
+ cmp = memcmp(ptr, zero_buf, got);
+ if (cmp == 0) {
+ /* The whole block is zero, make a hole */
+ retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
+ if (retval)
+ goto fail;
+ got = 0;
+ }
+ }
+
+ /* Normal copy */
+ while (got > 0) {
+ retval = ext2fs_file_write(e2_file, ptr,
+ got, &written);
+ if (retval)
+ goto fail;
+
+ got -= written;
+ ptr += written;
+ }
+ }
+ ext2fs_free_mem(&buf);
+ ext2fs_free_mem(&zero_buf);
+ retval = ext2fs_file_close(e2_file);
+ return retval;
+
+fail:
+ ext2fs_free_mem(&buf);
+ ext2fs_free_mem(&zero_buf);
+ (void) ext2fs_file_close(e2_file);
+ return retval;
+}
+
/* Copy the native file to the fs */
errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
{
+ int fd;
+ struct stat statbuf;
+ ext2_ino_t newfile;
+ errcode_t retval;
+ struct ext2_inode inode;
+ int bufsize = IO_BUFSIZE;
+ int make_holes = 0;
+
+ fd = open(src, O_RDONLY);
+ if (fd < 0) {
+ com_err(src, errno, 0);
+ return errno;
+ }
+ if (fstat(fd, &statbuf) < 0) {
+ com_err(src, errno, 0);
+ close(fd);
+ return errno;
+ }
+
+ retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile);
+ if (retval == 0) {
+ com_err(__func__, 0, "The file '%s' already exists\n", dest);
+ close(fd);
+ return errno;
+ }
+
+ retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
+ if (retval) {
+ com_err(__func__, retval, 0);
+ close(fd);
+ return errno;
+ }
+#ifdef DEBUGFS
+ printf("Allocated inode: %u\n", newfile);
+#endif
+ retval = ext2fs_link(current_fs, cwd, dest, newfile,
+ EXT2_FT_REG_FILE);
+ if (retval == EXT2_ET_DIR_NO_SPACE) {
+ retval = ext2fs_expand_dir(current_fs, cwd);
+ if (retval) {
+ com_err(__func__, retval, "while expanding directory");
+ close(fd);
+ return errno;
+ }
+ retval = ext2fs_link(current_fs, cwd, dest, newfile,
+ EXT2_FT_REG_FILE);
+ }
+ if (retval) {
+ com_err(dest, retval, 0);
+ close(fd);
+ return errno;
+ }
+ if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile))
+ com_err(__func__, 0, "Warning: inode already set");
+ ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
+ memset(&inode, 0, sizeof(inode));
+ inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
+ inode.i_atime = inode.i_ctime = inode.i_mtime =
+ current_fs->now ? current_fs->now : time(0);
+ inode.i_links_count = 1;
+ inode.i_size = statbuf.st_size;
+ if (current_fs->super->s_feature_incompat &
+ EXT3_FEATURE_INCOMPAT_EXTENTS) {
+ int i;
+ struct ext3_extent_header *eh;
+
+ eh = (struct ext3_extent_header *) &inode.i_block[0];
+ eh->eh_depth = 0;
+ eh->eh_entries = 0;
+ eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
+ i = (sizeof(inode.i_block) - sizeof(*eh)) /
+ sizeof(struct ext3_extent);
+ eh->eh_max = ext2fs_cpu_to_le16(i);
+ inode.i_flags |= EXT4_EXTENTS_FL;
+ }
+
+ if ((retval = ext2fs_write_new_inode(current_fs, newfile, &inode))) {
+ com_err(__func__, retval, "while creating inode %u", newfile);
+ close(fd);
+ return errno;
+ }
+ if (LINUX_S_ISREG(inode.i_mode)) {
+ if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
+ make_holes = 1;
+ /*
+ * Use I/O blocksize as buffer size when
+ * copying sparse files.
+ */
+ bufsize = statbuf.st_blksize;
+ }
+ retval = copy_file(fd, newfile, bufsize, make_holes);
+ if (retval)
+ com_err("copy_file", retval, 0);
+ }
+ close(fd);
+
+ return 0;
}
/* Copy files from source_dir to fs */
--
1.7.10.4
next prev parent reply other threads:[~2014-03-01 8:06 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-01 8:06 [PATCH V4 00/11] 2fsprogs/mke2fs: add an option: -d root-directory Robert Yang
2014-03-01 8:06 ` [PATCH V4 01/11] mke2fs: add the ability to copy files from a given directory Robert Yang
2014-03-01 8:06 ` [PATCH V4 02/11] misc/create_inode.c: copy files recursively Robert Yang
2014-03-01 8:06 ` [PATCH V4 03/11] misc/create_inode.c: create special file Robert Yang
2014-03-01 8:06 ` [PATCH V4 04/11] misc/create_inode.c: create symlink Robert Yang
2014-03-01 8:06 ` Robert Yang [this message]
2014-03-06 19:06 ` [PATCH V4 05/11] misc/create_inode.c: copy regular file Darrick J. Wong
2014-03-06 19:47 ` Theodore Ts'o
2014-03-06 20:14 ` Darrick J. Wong
2014-03-06 22:57 ` Theodore Ts'o
2014-03-07 1:24 ` Robert Yang
2014-03-07 2:56 ` Darrick J. Wong
2014-03-07 3:22 ` Robert Yang
2014-03-07 2:38 ` Darrick J. Wong
2014-03-01 8:06 ` [PATCH V4 06/11] misc/create_inode.c: create directory Robert Yang
2014-03-01 8:06 ` [PATCH V4 07/11] misc/create_inode.c: set owner/mode/time for the inode Robert Yang
2014-03-01 8:06 ` [PATCH V4 08/11] mke2fs.c: add an option: -d root-directory Robert Yang
2014-03-01 8:06 ` [PATCH V4 09/11] misc/create_inode.c: handle hardlinks Robert Yang
2014-03-01 8:06 ` [PATCH V4 10/11] debugfs: use the functions in misc/create_inode.c Robert Yang
2014-03-01 8:06 ` [PATCH V4 11/11] mke2fs.8.in: update the manual for the -d option Robert Yang
2014-03-06 8:21 ` [PATCH V4 00/11] 2fsprogs/mke2fs: add an option: -d root-directory Darren Hart
2014-03-06 9:39 ` Robert Yang
2014-03-06 16:30 ` Theodore Ts'o
2014-03-07 2:51 ` Robert Yang
2014-03-07 2:59 ` Darrick J. Wong
2014-03-07 3:24 ` Robert Yang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1393661175-459-6-git-send-email-liezhi.yang@windriver.com \
--to=liezhi.yang@windriver.com \
--cc=dvhart@linux.intel.com \
--cc=linux-ext4@vger.kernel.org \
--cc=tytso@mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).