1 From db89992fab2215f52dce4c595a39271b4ff3000b Mon Sep 17 00:00:00 2001
2 From: Robert Yang <liezhi.yang@windriver.com>
3 Date: Mon, 23 Dec 2013 04:22:22 -0500
4 Subject: [PATCH 10/11] debugfs: use the functions in misc/create_inode.c
6 * Use the functions in misc/create_inode.c, and remove the duplicated
9 * The CREATE_INODE_DEPS in the debugfs/Makefile.in is used for recording
10 create_inode.o's depends which is from misc/Makefile.in, we have to
11 recompile create_inode.o because we need it to print more messages when
12 it is used by debugfs, just like we recompile e2freefrag.o, but it seems
13 that the e2freefrag.o's depends in debugfs/Makefile.in is incorrect, it
14 would not rebuild when its depends (e.g.: lib/config.h) is changed,
15 which would cause unexpected errors. Make duplicated code in
16 debugfs/Makefile.in and misc/Makefile.in is not a good idea, maybe we'd
17 better define CREATE_INODE_DEPS in the top Makefile, I'd like to send
18 another patch and fix the e2freefrag if you are fine with it.
20 Upstream-Status: Backport
22 Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
23 Reviewed-by: Darren Hart <dvhart@linux.intel.com>
25 debugfs/Makefile.in | 17 ++-
26 debugfs/debugfs.c | 305 +++------------------------------------------------
27 debugfs/debugfs.h | 1 +
28 3 files changed, 31 insertions(+), 292 deletions(-)
30 diff --git a/debugfs/Makefile.in b/debugfs/Makefile.in
31 index bf037e0..f51b78e 100644
32 --- a/debugfs/Makefile.in
33 +++ b/debugfs/Makefile.in
34 @@ -18,7 +18,7 @@ MK_CMDS= _SS_DIR_OVERRIDE=../lib/ss ../lib/ss/mk_cmds
36 DEBUG_OBJS= debug_cmds.o debugfs.o util.o ncheck.o icheck.o ls.o \
37 lsdel.o dump.o set_fields.o logdump.o htree.o unused.o e2freefrag.o \
38 - filefrag.o extent_cmds.o extent_inode.o zap.o
39 + filefrag.o extent_cmds.o extent_inode.o zap.o create_inode.o
41 RO_DEBUG_OBJS= ro_debug_cmds.o ro_debugfs.o util.o ncheck.o icheck.o ls.o \
42 lsdel.o logdump.o htree.o e2freefrag.o filefrag.o extent_cmds.o \
43 @@ -28,7 +28,13 @@ SRCS= debug_cmds.c $(srcdir)/debugfs.c $(srcdir)/util.c $(srcdir)/ls.c \
44 $(srcdir)/ncheck.c $(srcdir)/icheck.c $(srcdir)/lsdel.c \
45 $(srcdir)/dump.c $(srcdir)/set_fields.c ${srcdir}/logdump.c \
46 $(srcdir)/htree.c $(srcdir)/unused.c ${srcdir}/../misc/e2freefrag.c \
47 - $(srcdir)/filefrag.c $(srcdir)/extent_inode.c $(srcdir)/zap.c
48 + $(srcdir)/filefrag.c $(srcdir)/extent_inode.c $(srcdir)/zap.c \
49 + $(srcdir)/../misc/create_inode.c
51 +CREATE_INODE_DEPS= $(srcdir)/../misc/create_inode.h \
52 + $(srcdir)/../misc/create_inode.c $(top_builddir)/lib/config.h \
53 + $(srcdir)/../lib/ext2fs/ext2fs.h $(srcdir)/../lib/et/com_err.h \
54 + $(srcdir)/../lib/e2p/e2p.h $(srcdir)/../misc/nls-enable.h
56 LIBS= $(LIBEXT2FS) $(LIBE2P) $(LIBSS) $(LIBCOM_ERR) $(LIBBLKID) \
58 @@ -81,6 +87,11 @@ e2freefrag.o: $(srcdir)/../misc/e2freefrag.c
60 $(Q) $(CC) -c $(ALL_CFLAGS) -I$(srcdir) $< -DDEBUGFS -o $@
62 +create_inode.o: $(CREATE_INODE_DEPS)
64 + $(Q) $(CC) -c $(ALL_CFLAGS) -I$(srcdir) \
65 + $(srcdir)/../misc/create_inode.c -DDEBUGFS -o $@
67 debugfs.8: $(DEP_SUBSTITUTE) $(srcdir)/debugfs.8.in
69 $(Q) $(SUBSTITUTE_UPTIME) $(srcdir)/debugfs.8.in debugfs.8
70 @@ -142,7 +153,7 @@ debugfs.o: $(srcdir)/debugfs.c $(top_srcdir)/lib/et/com_err.h \
71 $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/bitops.h \
72 $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/version.h $(srcdir)/jfs_user.h \
73 $(top_srcdir)/lib/ext2fs/kernel-jbd.h $(top_srcdir)/lib/ext2fs/jfs_compat.h \
74 - $(top_srcdir)/lib/ext2fs/kernel-list.h
75 + $(top_srcdir)/lib/ext2fs/kernel-list.h $(top_srcdir)/misc/util.h
76 util.o: $(srcdir)/util.c $(srcdir)/debugfs.h \
77 $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \
78 $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \
79 diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c
80 index 9c215f7..62e9f44 100644
81 --- a/debugfs/debugfs.c
82 +++ b/debugfs/debugfs.c
83 @@ -25,8 +25,6 @@ extern char *optarg;
87 -#include <sys/types.h>
88 -#include <sys/stat.h>
91 #include "uuid/uuid.h"
92 @@ -41,22 +39,11 @@ extern char *optarg;
96 -/* 64KiB is the minimium blksize to best minimize system call overhead. */
98 -#define IO_BUFSIZE 64*1024
101 -/* Block size for `st_blocks' */
103 -#define S_BLKSIZE 512
106 ss_request_table *extra_cmds;
107 const char *debug_prog_name;
110 -ext2_filsys current_fs = NULL;
111 -ext2_ino_t root, cwd;
114 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
115 blk64_t blocksize, int catastrophic,
116 @@ -1576,189 +1563,24 @@ void do_find_free_inode(int argc, char *argv[])
120 -static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
122 - ext2_file_t e2_file;
125 - unsigned int written;
131 - retval = ext2fs_file_open(current_fs, newfile,
132 - EXT2_FILE_WRITE, &e2_file);
136 - retval = ext2fs_get_mem(bufsize, &buf);
138 - com_err("copy_file", retval, "can't allocate buffer\n");
142 - /* This is used for checking whether the whole block is zero */
143 - retval = ext2fs_get_memzero(bufsize, &zero_buf);
145 - com_err("copy_file", retval, "can't allocate buffer\n");
146 - ext2fs_free_mem(&buf);
151 - got = read(fd, buf, bufsize);
162 - /* Check whether all is zero */
163 - cmp = memcmp(ptr, zero_buf, got);
165 - /* The whole block is zero, make a hole */
166 - retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
175 - retval = ext2fs_file_write(e2_file, ptr,
184 - ext2fs_free_mem(&buf);
185 - ext2fs_free_mem(&zero_buf);
186 - retval = ext2fs_file_close(e2_file);
190 - ext2fs_free_mem(&buf);
191 - ext2fs_free_mem(&zero_buf);
192 - (void) ext2fs_file_close(e2_file);
197 void do_write(int argc, char *argv[])
200 - struct stat statbuf;
201 - ext2_ino_t newfile;
203 - struct ext2_inode inode;
204 - int bufsize = IO_BUFSIZE;
205 - int make_holes = 0;
207 if (common_args_process(argc, argv, 3, 3, "write",
208 "<native file> <new file>", CHECK_FS_RW))
211 - fd = open(argv[1], O_RDONLY);
213 - com_err(argv[1], errno, 0);
216 - if (fstat(fd, &statbuf) < 0) {
217 - com_err(argv[1], errno, 0);
222 - retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
224 - com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
229 - retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
231 + if ((retval = do_write_internal(cwd, argv[1], argv[2])))
232 com_err(argv[0], retval, 0);
236 - printf("Allocated inode: %u\n", newfile);
237 - retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
239 - if (retval == EXT2_ET_DIR_NO_SPACE) {
240 - retval = ext2fs_expand_dir(current_fs, cwd);
242 - com_err(argv[0], retval, "while expanding directory");
246 - retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
250 - com_err(argv[2], retval, 0);
254 - if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
255 - com_err(argv[0], 0, "Warning: inode already set");
256 - ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
257 - memset(&inode, 0, sizeof(inode));
258 - inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
259 - inode.i_atime = inode.i_ctime = inode.i_mtime =
260 - current_fs->now ? current_fs->now : time(0);
261 - inode.i_links_count = 1;
262 - inode.i_size = statbuf.st_size;
263 - if (current_fs->super->s_feature_incompat &
264 - EXT3_FEATURE_INCOMPAT_EXTENTS) {
266 - struct ext3_extent_header *eh;
268 - eh = (struct ext3_extent_header *) &inode.i_block[0];
270 - eh->eh_entries = 0;
271 - eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
272 - i = (sizeof(inode.i_block) - sizeof(*eh)) /
273 - sizeof(struct ext3_extent);
274 - eh->eh_max = ext2fs_cpu_to_le16(i);
275 - inode.i_flags |= EXT4_EXTENTS_FL;
277 - if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
281 - if (LINUX_S_ISREG(inode.i_mode)) {
282 - if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
285 - * Use I/O blocksize as buffer size when
286 - * copying sparse files.
288 - bufsize = statbuf.st_blksize;
290 - retval = copy_file(fd, newfile, bufsize, make_holes);
292 - com_err("copy_file", retval, 0);
297 void do_mknod(int argc, char *argv[])
299 unsigned long mode, major, minor;
300 - ext2_ino_t newfile;
302 - struct ext2_inode inode;
306 if (check_fs_open(argv[0]))
308 @@ -1767,115 +1589,50 @@ void do_mknod(int argc, char *argv[])
309 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
313 mode = minor = major = 0;
314 switch (argv[2][0]) {
316 - mode = LINUX_S_IFIFO;
317 - filetype = EXT2_FT_FIFO;
318 + st.st_mode = S_IFIFO;
322 - mode = LINUX_S_IFCHR;
323 - filetype = EXT2_FT_CHRDEV;
324 + st.st_mode = S_IFCHR;
328 - mode = LINUX_S_IFBLK;
329 - filetype = EXT2_FT_BLKDEV;
330 + st.st_mode = S_IFBLK;
339 major = strtoul(argv[3], argv+3, 0);
340 minor = strtoul(argv[4], argv+4, 0);
341 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
347 - if (check_fs_read_write(argv[0]))
349 - retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
352 + st.st_rdev = makedev(major, minor);
353 + if ((retval = do_mknod_internal(cwd, argv[1], &st)))
354 com_err(argv[0], retval, 0);
357 - printf("Allocated inode: %u\n", newfile);
358 - retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
359 - if (retval == EXT2_ET_DIR_NO_SPACE) {
360 - retval = ext2fs_expand_dir(current_fs, cwd);
362 - com_err(argv[0], retval, "while expanding directory");
365 - retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
369 - com_err(argv[1], retval, 0);
372 - if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
373 - com_err(argv[0], 0, "Warning: inode already set");
374 - ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
375 - memset(&inode, 0, sizeof(inode));
376 - inode.i_mode = mode;
377 - inode.i_atime = inode.i_ctime = inode.i_mtime =
378 - current_fs->now ? current_fs->now : time(0);
379 - if ((major < 256) && (minor < 256)) {
380 - inode.i_block[0] = major*256+minor;
381 - inode.i_block[1] = 0;
383 - inode.i_block[0] = 0;
384 - inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
386 - inode.i_links_count = 1;
387 - if (debugfs_write_new_inode(newfile, &inode, argv[0]))
391 void do_mkdir(int argc, char *argv[])
398 if (common_args_process(argc, argv, 2, 2, "mkdir",
399 "<filename>", CHECK_FS_RW))
402 - cp = strrchr(argv[1], '/');
405 - parent = string_to_inode(argv[1]);
407 - com_err(argv[1], ENOENT, 0);
417 - retval = ext2fs_mkdir(current_fs, parent, 0, name);
418 - if (retval == EXT2_ET_DIR_NO_SPACE) {
419 - retval = ext2fs_expand_dir(current_fs, parent);
421 - com_err(argv[0], retval, "while expanding directory");
427 - com_err("ext2fs_mkdir", retval, 0);
430 + if ((retval = do_mkdir_internal(cwd, argv[1], NULL)))
431 + com_err(argv[0], retval, 0);
435 @@ -2270,44 +2027,14 @@ void do_punch(int argc, char *argv[])
437 void do_symlink(int argc, char *argv[])
441 - char *name, *target;
444 if (common_args_process(argc, argv, 3, 3, "symlink",
445 "<filename> <target>", CHECK_FS_RW))
448 - cp = strrchr(argv[1], '/');
451 - parent = string_to_inode(argv[1]);
453 - com_err(argv[1], ENOENT, 0);
464 - retval = ext2fs_symlink(current_fs, parent, 0, name, target);
465 - if (retval == EXT2_ET_DIR_NO_SPACE) {
466 - retval = ext2fs_expand_dir(current_fs, parent);
468 - com_err(argv[0], retval, "while expanding directory");
474 - com_err("ext2fs_symlink", retval, 0);
477 + if ((retval = do_symlink_internal(cwd, argv[1], argv[2])))
478 + com_err(argv[0], retval, 0);
482 diff --git a/debugfs/debugfs.h b/debugfs/debugfs.h
483 index 6b4f6ef..2d464c1 100644
484 --- a/debugfs/debugfs.h
485 +++ b/debugfs/debugfs.h
488 #include "ext2fs/ext2_fs.h"
489 #include "ext2fs/ext2fs.h"
490 +#include "../misc/create_inode.h"