2 * create_inode.c --- create an inode
4 * Copyright (C) 2014 Robert Yang <liezhi.yang@windriver.com>
7 * This file may be redistributed under the terms of the GNU library
8 * General Public License, version 2.
14 #include <limits.h> /* for PATH_MAX */
15 #ifdef HAVE_ATTR_XATTR_H
16 #include <attr/xattr.h>
19 #include "create_inode.h"
20 #include "nls-enable.h"
22 #if __STDC_VERSION__ < 199901L
24 # define __func__ __FUNCTION__
26 # define __func__ "<unknown>"
30 /* 64KiB is the minimium blksize to best minimize system call overhead. */
32 #define IO_BUFSIZE 64*1024
35 /* Block size for `st_blocks' */
40 static int ext2_file_type(unsigned int mode)
42 if (LINUX_S_ISREG(mode))
43 return EXT2_FT_REG_FILE;
45 if (LINUX_S_ISDIR(mode))
48 if (LINUX_S_ISCHR(mode))
49 return EXT2_FT_CHRDEV;
51 if (LINUX_S_ISBLK(mode))
52 return EXT2_FT_BLKDEV;
54 if (LINUX_S_ISLNK(mode))
55 return EXT2_FT_SYMLINK;
57 if (LINUX_S_ISFIFO(mode))
60 if (LINUX_S_ISSOCK(mode))
66 /* Link an inode number to a directory */
67 static errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino,
68 ext2_ino_t ino, const char *name)
70 struct ext2_inode inode;
73 retval = ext2fs_read_inode(fs, ino, &inode);
75 com_err(__func__, retval, "while reading inode %u", ino);
79 retval = ext2fs_link(fs, parent_ino, name, ino,
80 ext2_file_type(inode.i_mode));
81 if (retval == EXT2_ET_DIR_NO_SPACE) {
82 retval = ext2fs_expand_dir(fs, parent_ino);
84 com_err(__func__, retval, "while expanding directory");
87 retval = ext2fs_link(fs, parent_ino, name, ino,
88 ext2_file_type(inode.i_mode));
91 com_err(__func__, retval, "while linking %s", name);
95 inode.i_links_count++;
97 retval = ext2fs_write_inode(fs, ino, &inode);
99 com_err(__func__, retval, "while writing inode %u", ino);
104 /* Fill the uid, gid, mode and time for the inode */
105 static void fill_inode(struct ext2_inode *inode, struct stat *st)
108 inode->i_uid = st->st_uid;
109 inode->i_gid = st->st_gid;
110 inode->i_mode |= st->st_mode;
111 inode->i_atime = st->st_atime;
112 inode->i_mtime = st->st_mtime;
113 inode->i_ctime = st->st_ctime;
117 /* Set the uid, gid, mode and time for the inode */
118 static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd,
119 ext2_ino_t ino, struct stat *st)
122 struct ext2_inode inode;
124 retval = ext2fs_read_inode(fs, ino, &inode);
126 com_err(__func__, retval, "while reading inode %u", ino);
130 fill_inode(&inode, st);
132 retval = ext2fs_write_inode(fs, ino, &inode);
134 com_err(__func__, retval, "while writing inode %u", ino);
138 static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino, const char *filename)
140 #ifdef HAVE_LLISTXATTR
141 errcode_t retval, close_retval;
142 struct ext2_inode inode;
143 struct ext2_xattr_handle *handle;
144 ssize_t size, value_size;
148 size = llistxattr(filename, NULL, 0);
150 com_err(__func__, errno, "llistxattr failed on %s", filename);
152 } else if (size == 0) {
156 retval = ext2fs_xattrs_open(fs, ino, &handle);
158 if (retval == EXT2_ET_MISSING_EA_FEATURE)
160 com_err(__func__, retval, "while opening inode %u", ino);
164 retval = ext2fs_get_mem(size, &list);
166 com_err(__func__, retval, "whilst allocating memory");
170 size = llistxattr(filename, list, size);
172 com_err(__func__, errno, "llistxattr failed on %s", filename);
177 for (i = 0; i < size; i += strlen(&list[i]) + 1) {
178 const char *name = &list[i];
181 value_size = getxattr(filename, name, NULL, 0);
182 if (value_size == -1) {
183 com_err(__func__, errno, "getxattr failed on %s",
189 retval = ext2fs_get_mem(value_size, &value);
191 com_err(__func__, retval, "whilst allocating memory");
195 value_size = getxattr(filename, name, value, value_size);
196 if (value_size == -1) {
197 ext2fs_free_mem(&value);
198 com_err(__func__, errno, "getxattr failed on %s",
204 retval = ext2fs_xattr_set(handle, name, value, value_size);
205 ext2fs_free_mem(&value);
207 com_err(__func__, retval,
208 "while writing xattr %u", ino);
214 ext2fs_free_mem(&list);
215 close_retval = ext2fs_xattrs_close(&handle);
217 com_err(__func__, retval, "while closing inode %u", ino);
218 retval = retval ? retval : close_retval;
221 #else /* HAVE_LLISTXATTR */
223 #endif /* HAVE_LLISTXATTR */
226 /* Make a special files (block and character devices), fifo's, and sockets */
227 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
232 struct ext2_inode inode;
233 unsigned long devmajor, devminor, mode;
236 switch(st->st_mode & S_IFMT) {
238 mode = LINUX_S_IFCHR;
239 filetype = EXT2_FT_CHRDEV;
242 mode = LINUX_S_IFBLK;
243 filetype = EXT2_FT_BLKDEV;
246 mode = LINUX_S_IFIFO;
247 filetype = EXT2_FT_FIFO;
250 mode = LINUX_S_IFSOCK;
251 filetype = EXT2_FT_SOCK;
254 return EXT2_ET_INVALID_ARGUMENT;
257 if (!(fs->flags & EXT2_FLAG_RW)) {
258 com_err(__func__, 0, "Filesystem opened read/only");
261 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
263 com_err(__func__, retval, 0);
268 printf("Allocated inode: %u\n", ino);
270 retval = ext2fs_link(fs, cwd, name, ino, filetype);
271 if (retval == EXT2_ET_DIR_NO_SPACE) {
272 retval = ext2fs_expand_dir(fs, cwd);
274 com_err(__func__, retval, "while expanding directory");
277 retval = ext2fs_link(fs, cwd, name, ino, filetype);
280 com_err(name, retval, 0);
283 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino))
284 com_err(__func__, 0, "Warning: inode already set");
285 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
286 memset(&inode, 0, sizeof(inode));
288 inode.i_atime = inode.i_ctime = inode.i_mtime =
289 fs->now ? fs->now : time(0);
291 if (filetype != S_IFIFO) {
292 devmajor = major(st->st_rdev);
293 devminor = minor(st->st_rdev);
295 if ((devmajor < 256) && (devminor < 256)) {
296 inode.i_block[0] = devmajor * 256 + devminor;
297 inode.i_block[1] = 0;
299 inode.i_block[0] = 0;
300 inode.i_block[1] = (devminor & 0xff) | (devmajor << 8) |
301 ((devminor & ~0xff) << 12);
304 inode.i_links_count = 1;
306 retval = ext2fs_write_new_inode(fs, ino, &inode);
308 com_err(__func__, retval, "while creating inode %u", ino);
313 /* Make a symlink name -> target */
314 errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
315 char *target, ext2_ino_t root)
318 ext2_ino_t parent_ino;
321 cp = strrchr(name, '/');
324 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
326 com_err(name, retval, 0);
334 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
335 if (retval == EXT2_ET_DIR_NO_SPACE) {
336 retval = ext2fs_expand_dir(fs, parent_ino);
338 com_err("do_symlink_internal", retval,
339 "while expanding directory");
345 com_err("ext2fs_symlink", retval, 0);
349 /* Make a directory in the fs */
350 errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
351 struct stat *st, ext2_ino_t root)
354 ext2_ino_t parent_ino;
358 cp = strrchr(name, '/');
361 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
363 com_err(name, retval, 0);
371 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
372 if (retval == EXT2_ET_DIR_NO_SPACE) {
373 retval = ext2fs_expand_dir(fs, parent_ino);
375 com_err(__func__, retval, "while expanding directory");
381 com_err("ext2fs_mkdir", retval, 0);
385 static errcode_t copy_file(ext2_filsys fs, int fd, ext2_ino_t newfile,
386 int bufsize, int make_holes)
389 errcode_t retval, close_ret;
391 unsigned int written;
397 retval = ext2fs_file_open(fs, newfile,
398 EXT2_FILE_WRITE, &e2_file);
402 retval = ext2fs_get_mem(bufsize, &buf);
404 com_err("copy_file", retval, "can't allocate buffer\n");
408 /* This is used for checking whether the whole block is zero */
409 retval = ext2fs_get_memzero(bufsize, &zero_buf);
411 com_err("copy_file", retval, "can't allocate zero buffer\n");
416 got = read(fd, buf, bufsize);
427 /* Check whether all is zero */
428 cmp = memcmp(ptr, zero_buf, got);
430 /* The whole block is zero, make a hole */
431 retval = ext2fs_file_lseek(e2_file, got,
442 retval = ext2fs_file_write(e2_file, ptr,
453 ext2fs_free_mem(&zero_buf);
455 ext2fs_free_mem(&buf);
457 close_ret = ext2fs_file_close(e2_file);
463 static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino)
467 for (i = 0; i < hdlinks->count; i++) {
468 if (hdlinks->hdl[i].src_dev == dev &&
469 hdlinks->hdl[i].src_ino == ino)
475 /* Copy the native file to the fs */
476 errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
477 const char *dest, ext2_ino_t root)
483 struct ext2_inode inode;
484 int bufsize = IO_BUFSIZE;
487 fd = ext2fs_open_file(src, O_RDONLY, 0);
489 com_err(src, errno, 0);
492 if (fstat(fd, &statbuf) < 0) {
493 com_err(src, errno, 0);
498 retval = ext2fs_namei(fs, root, cwd, dest, &newfile);
501 return EXT2_ET_FILE_EXISTS;
504 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile);
506 com_err(__func__, retval, 0);
511 printf("Allocated inode: %u\n", newfile);
513 retval = ext2fs_link(fs, cwd, dest, newfile,
515 if (retval == EXT2_ET_DIR_NO_SPACE) {
516 retval = ext2fs_expand_dir(fs, cwd);
518 com_err(__func__, retval, "while expanding directory");
522 retval = ext2fs_link(fs, cwd, dest, newfile,
526 com_err(dest, retval, 0);
530 if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile))
531 com_err(__func__, 0, "Warning: inode already set");
532 ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
533 memset(&inode, 0, sizeof(inode));
534 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
535 inode.i_atime = inode.i_ctime = inode.i_mtime =
536 fs->now ? fs->now : time(0);
537 inode.i_links_count = 1;
538 retval = ext2fs_inode_size_set(fs, &inode, statbuf.st_size);
540 com_err(dest, retval, 0);
544 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
545 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
546 inode.i_flags |= EXT4_INLINE_DATA_FL;
547 } else if (fs->super->s_feature_incompat &
548 EXT3_FEATURE_INCOMPAT_EXTENTS) {
549 ext2_extent_handle_t handle;
551 inode.i_flags &= ~EXT4_EXTENTS_FL;
552 retval = ext2fs_extent_open2(fs, newfile, &inode, &handle);
555 ext2fs_extent_free(handle);
558 retval = ext2fs_write_new_inode(fs, newfile, &inode);
560 com_err(__func__, retval, "while creating inode %u", newfile);
564 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
565 retval = ext2fs_inline_data_init(fs, newfile);
567 com_err("copy_file", retval, 0);
572 if (LINUX_S_ISREG(inode.i_mode)) {
573 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
576 * Use I/O blocksize as buffer size when
577 * copying sparse files.
579 bufsize = statbuf.st_blksize;
581 retval = copy_file(fs, fd, newfile, bufsize, make_holes);
583 com_err("copy_file", retval, 0);
590 /* Copy files from source_dir to fs */
591 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
592 const char *source_dir, ext2_ino_t root,
593 struct hdlinks_s *hdlinks)
599 char ln_target[PATH_MAX];
600 unsigned int save_inode;
602 errcode_t retval = 0;
606 if (chdir(source_dir) < 0) {
607 com_err(__func__, errno,
608 _("while changing working directory to \"%s\""),
613 if (!(dh = opendir("."))) {
614 com_err(__func__, errno,
615 _("while opening directory \"%s\""), source_dir);
619 while ((dent = readdir(dh))) {
620 if ((!strcmp(dent->d_name, ".")) ||
621 (!strcmp(dent->d_name, "..")))
623 if (lstat(dent->d_name, &st)) {
624 com_err(__func__, errno, _("while lstat \"%s\""),
630 /* Check for hardlinks */
632 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
634 hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino);
636 retval = add_link(fs, parent_ino,
637 hdlinks->hdl[hdlink].dst_ino,
640 com_err(__func__, retval,
641 "while linking %s", name);
649 switch(st.st_mode & S_IFMT) {
654 retval = do_mknod_internal(fs, parent_ino, name, &st);
656 com_err(__func__, retval,
657 _("while creating special file "
663 read_cnt = readlink(name, ln_target,
664 sizeof(ln_target) - 1);
665 if (read_cnt == -1) {
666 com_err(__func__, errno,
667 _("while trying to readlink \"%s\""),
672 ln_target[read_cnt] = '\0';
673 retval = do_symlink_internal(fs, parent_ino, name,
676 com_err(__func__, retval,
677 _("while writing symlink\"%s\""),
683 retval = do_write_internal(fs, parent_ino, name, name,
686 com_err(__func__, retval,
687 _("while writing file \"%s\""), name);
692 retval = do_mkdir_internal(fs, parent_ino, name, &st,
695 com_err(__func__, retval,
696 _("while making dir \"%s\""), name);
699 retval = ext2fs_namei(fs, root, parent_ino,
702 com_err(name, retval, 0);
705 /* Populate the dir recursively*/
706 retval = __populate_fs(fs, ino, name, root, hdlinks);
708 com_err(__func__, retval,
709 _("while adding dir \"%s\""), name);
713 com_err(__func__, errno, _("during cd .."));
720 _("ignoring entry \"%s\""), name);
723 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
725 com_err(name, retval, 0);
729 retval = set_inode_extra(fs, parent_ino, ino, &st);
731 com_err(__func__, retval,
732 _("while setting inode for \"%s\""), name);
736 retval = set_inode_xattr(fs, ino, name);
738 com_err(__func__, retval,
739 _("while setting xattrs for \"%s\""), name);
743 /* Save the hardlink ino */
746 * Check whether need more memory, and we don't need
747 * free() since the lifespan will be over after the fs
750 if (hdlinks->count == hdlinks->size) {
751 void *p = realloc(hdlinks->hdl,
752 (hdlinks->size + HDLINK_CNT) *
753 sizeof(struct hdlink_s));
756 _("Not enough memory"));
757 retval = EXT2_ET_NO_MEMORY;
761 hdlinks->size += HDLINK_CNT;
763 hdlinks->hdl[hdlinks->count].src_dev = st.st_dev;
764 hdlinks->hdl[hdlinks->count].src_ino = st.st_ino;
765 hdlinks->hdl[hdlinks->count].dst_ino = ino;
775 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
776 const char *source_dir, ext2_ino_t root)
778 struct hdlinks_s hdlinks;
782 hdlinks.size = HDLINK_CNT;
783 hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s));
784 if (hdlinks.hdl == NULL) {
785 com_err(__func__, errno, "Not enough memory");
789 retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks);