Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | From 2973c74afaa532f3f72639b463322b2523519c20 Mon Sep 17 00:00:00 2001 |
| 2 | From: Robert Yang <liezhi.yang@windriver.com> |
| 3 | Date: Mon, 23 Dec 2013 03:28:12 -0500 |
| 4 | Subject: [PATCH 05/11] misc/create_inode.c: copy regular file |
| 5 | |
| 6 | The do_write_internal() is used for copying file from native fs to |
| 7 | target, most of the code are from debugfs/debugfs.c, the |
| 8 | debugfs/debugfs.c will be modified to use this function. |
| 9 | |
| 10 | Upstream-Status: Backport |
| 11 | |
| 12 | Signed-off-by: Robert Yang <liezhi.yang@windriver.com> |
| 13 | Reviewed-by: Darren Hart <dvhart@linux.intel.com> |
| 14 | --- |
| 15 | misc/create_inode.c | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 16 | 1 file changed, 183 insertions(+) |
| 17 | |
| 18 | diff --git a/misc/create_inode.c b/misc/create_inode.c |
| 19 | index f845103..98f4a93 100644 |
| 20 | --- a/misc/create_inode.c |
| 21 | +++ b/misc/create_inode.c |
| 22 | @@ -8,6 +8,16 @@ |
| 23 | # endif |
| 24 | #endif |
| 25 | |
| 26 | +/* 64KiB is the minimium blksize to best minimize system call overhead. */ |
| 27 | +#ifndef IO_BUFSIZE |
| 28 | +#define IO_BUFSIZE 64*1024 |
| 29 | +#endif |
| 30 | + |
| 31 | +/* Block size for `st_blocks' */ |
| 32 | +#ifndef S_BLKSIZE |
| 33 | +#define S_BLKSIZE 512 |
| 34 | +#endif |
| 35 | + |
| 36 | /* Make a special file which is block, character and fifo */ |
| 37 | errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st) |
| 38 | { |
| 39 | @@ -127,9 +137,182 @@ errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | +static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes) |
| 44 | +{ |
| 45 | + ext2_file_t e2_file; |
| 46 | + errcode_t retval; |
| 47 | + int got; |
| 48 | + unsigned int written; |
| 49 | + char *buf; |
| 50 | + char *ptr; |
| 51 | + char *zero_buf; |
| 52 | + int cmp; |
| 53 | + |
| 54 | + retval = ext2fs_file_open(current_fs, newfile, |
| 55 | + EXT2_FILE_WRITE, &e2_file); |
| 56 | + if (retval) |
| 57 | + return retval; |
| 58 | + |
| 59 | + retval = ext2fs_get_mem(bufsize, &buf); |
| 60 | + if (retval) { |
| 61 | + com_err("copy_file", retval, "can't allocate buffer\n"); |
| 62 | + return retval; |
| 63 | + } |
| 64 | + |
| 65 | + /* This is used for checking whether the whole block is zero */ |
| 66 | + retval = ext2fs_get_memzero(bufsize, &zero_buf); |
| 67 | + if (retval) { |
| 68 | + com_err("copy_file", retval, "can't allocate buffer\n"); |
| 69 | + ext2fs_free_mem(&buf); |
| 70 | + return retval; |
| 71 | + } |
| 72 | + |
| 73 | + while (1) { |
| 74 | + got = read(fd, buf, bufsize); |
| 75 | + if (got == 0) |
| 76 | + break; |
| 77 | + if (got < 0) { |
| 78 | + retval = errno; |
| 79 | + goto fail; |
| 80 | + } |
| 81 | + ptr = buf; |
| 82 | + |
| 83 | + /* Sparse copy */ |
| 84 | + if (make_holes) { |
| 85 | + /* Check whether all is zero */ |
| 86 | + cmp = memcmp(ptr, zero_buf, got); |
| 87 | + if (cmp == 0) { |
| 88 | + /* The whole block is zero, make a hole */ |
| 89 | + retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL); |
| 90 | + if (retval) |
| 91 | + goto fail; |
| 92 | + got = 0; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /* Normal copy */ |
| 97 | + while (got > 0) { |
| 98 | + retval = ext2fs_file_write(e2_file, ptr, |
| 99 | + got, &written); |
| 100 | + if (retval) |
| 101 | + goto fail; |
| 102 | + |
| 103 | + got -= written; |
| 104 | + ptr += written; |
| 105 | + } |
| 106 | + } |
| 107 | + ext2fs_free_mem(&buf); |
| 108 | + ext2fs_free_mem(&zero_buf); |
| 109 | + retval = ext2fs_file_close(e2_file); |
| 110 | + return retval; |
| 111 | + |
| 112 | +fail: |
| 113 | + ext2fs_free_mem(&buf); |
| 114 | + ext2fs_free_mem(&zero_buf); |
| 115 | + (void) ext2fs_file_close(e2_file); |
| 116 | + return retval; |
| 117 | +} |
| 118 | + |
| 119 | /* Copy the native file to the fs */ |
| 120 | errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest) |
| 121 | { |
| 122 | + int fd; |
| 123 | + struct stat statbuf; |
| 124 | + ext2_ino_t newfile; |
| 125 | + errcode_t retval; |
| 126 | + struct ext2_inode inode; |
| 127 | + int bufsize = IO_BUFSIZE; |
| 128 | + int make_holes = 0; |
| 129 | + |
| 130 | + fd = open(src, O_RDONLY); |
| 131 | + if (fd < 0) { |
| 132 | + com_err(src, errno, 0); |
| 133 | + return errno; |
| 134 | + } |
| 135 | + if (fstat(fd, &statbuf) < 0) { |
| 136 | + com_err(src, errno, 0); |
| 137 | + close(fd); |
| 138 | + return errno; |
| 139 | + } |
| 140 | + |
| 141 | + retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile); |
| 142 | + if (retval == 0) { |
| 143 | + com_err(__func__, 0, "The file '%s' already exists\n", dest); |
| 144 | + close(fd); |
| 145 | + return retval; |
| 146 | + } |
| 147 | + |
| 148 | + retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); |
| 149 | + if (retval) { |
| 150 | + com_err(__func__, retval, 0); |
| 151 | + close(fd); |
| 152 | + return retval; |
| 153 | + } |
| 154 | +#ifdef DEBUGFS |
| 155 | + printf("Allocated inode: %u\n", newfile); |
| 156 | +#endif |
| 157 | + retval = ext2fs_link(current_fs, cwd, dest, newfile, |
| 158 | + EXT2_FT_REG_FILE); |
| 159 | + if (retval == EXT2_ET_DIR_NO_SPACE) { |
| 160 | + retval = ext2fs_expand_dir(current_fs, cwd); |
| 161 | + if (retval) { |
| 162 | + com_err(__func__, retval, "while expanding directory"); |
| 163 | + close(fd); |
| 164 | + return retval; |
| 165 | + } |
| 166 | + retval = ext2fs_link(current_fs, cwd, dest, newfile, |
| 167 | + EXT2_FT_REG_FILE); |
| 168 | + } |
| 169 | + if (retval) { |
| 170 | + com_err(dest, retval, 0); |
| 171 | + close(fd); |
| 172 | + return retval; |
| 173 | + } |
| 174 | + if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile)) |
| 175 | + com_err(__func__, 0, "Warning: inode already set"); |
| 176 | + ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0); |
| 177 | + memset(&inode, 0, sizeof(inode)); |
| 178 | + inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG; |
| 179 | + inode.i_atime = inode.i_ctime = inode.i_mtime = |
| 180 | + current_fs->now ? current_fs->now : time(0); |
| 181 | + inode.i_links_count = 1; |
| 182 | + inode.i_size = statbuf.st_size; |
| 183 | + if (current_fs->super->s_feature_incompat & |
| 184 | + EXT3_FEATURE_INCOMPAT_EXTENTS) { |
| 185 | + int i; |
| 186 | + struct ext3_extent_header *eh; |
| 187 | + |
| 188 | + eh = (struct ext3_extent_header *) &inode.i_block[0]; |
| 189 | + eh->eh_depth = 0; |
| 190 | + eh->eh_entries = 0; |
| 191 | + eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC); |
| 192 | + i = (sizeof(inode.i_block) - sizeof(*eh)) / |
| 193 | + sizeof(struct ext3_extent); |
| 194 | + eh->eh_max = ext2fs_cpu_to_le16(i); |
| 195 | + inode.i_flags |= EXT4_EXTENTS_FL; |
| 196 | + } |
| 197 | + |
| 198 | + if ((retval = ext2fs_write_new_inode(current_fs, newfile, &inode))) { |
| 199 | + com_err(__func__, retval, "while creating inode %u", newfile); |
| 200 | + close(fd); |
| 201 | + return retval; |
| 202 | + } |
| 203 | + if (LINUX_S_ISREG(inode.i_mode)) { |
| 204 | + if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) { |
| 205 | + make_holes = 1; |
| 206 | + /* |
| 207 | + * Use I/O blocksize as buffer size when |
| 208 | + * copying sparse files. |
| 209 | + */ |
| 210 | + bufsize = statbuf.st_blksize; |
| 211 | + } |
| 212 | + retval = copy_file(fd, newfile, bufsize, make_holes); |
| 213 | + if (retval) |
| 214 | + com_err("copy_file", retval, 0); |
| 215 | + } |
| 216 | + close(fd); |
| 217 | + |
| 218 | + return 0; |
| 219 | } |
| 220 | |
| 221 | /* Copy files from source_dir to fs */ |
| 222 | -- |
| 223 | 1.7.10.4 |
| 224 | |