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 /* Link an inode number to a directory */
41 static errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino,
42 ext2_ino_t ino, const char *name)
44 struct ext2_inode inode;
47 retval = ext2fs_read_inode(fs, ino, &inode);
49 com_err(__func__, retval, "while reading inode %u", ino);
53 retval = ext2fs_link(fs, parent_ino, name, ino, inode.i_flags);
54 if (retval == EXT2_ET_DIR_NO_SPACE) {
55 retval = ext2fs_expand_dir(fs, parent_ino);
57 com_err(__func__, retval, "while expanding directory");
60 retval = ext2fs_link(fs, parent_ino, name, ino, inode.i_flags);
63 com_err(__func__, retval, "while linking %s", name);
67 inode.i_links_count++;
69 retval = ext2fs_write_inode(fs, ino, &inode);
71 com_err(__func__, retval, "while writing inode %u", ino);
76 /* Fill the uid, gid, mode and time for the inode */
77 static void fill_inode(struct ext2_inode *inode, struct stat *st)
80 inode->i_uid = st->st_uid;
81 inode->i_gid = st->st_gid;
82 inode->i_mode |= st->st_mode;
83 inode->i_atime = st->st_atime;
84 inode->i_mtime = st->st_mtime;
85 inode->i_ctime = st->st_ctime;
89 /* Set the uid, gid, mode and time for the inode */
90 static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd,
91 ext2_ino_t ino, struct stat *st)
94 struct ext2_inode inode;
96 retval = ext2fs_read_inode(fs, ino, &inode);
98 com_err(__func__, retval, "while reading inode %u", ino);
102 fill_inode(&inode, st);
104 retval = ext2fs_write_inode(fs, ino, &inode);
106 com_err(__func__, retval, "while writing inode %u", ino);
110 static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino, const char *filename)
112 #ifdef HAVE_LLISTXATTR
113 errcode_t retval, close_retval;
114 struct ext2_inode inode;
115 struct ext2_xattr_handle *handle;
116 ssize_t size, value_size;
120 size = llistxattr(filename, NULL, 0);
122 com_err(__func__, errno, "llistxattr failed on %s", filename);
124 } else if (size == 0) {
128 retval = ext2fs_xattrs_open(fs, ino, &handle);
130 if (retval == EXT2_ET_MISSING_EA_FEATURE)
132 com_err(__func__, retval, "while opening inode %u", ino);
136 retval = ext2fs_get_mem(size, &list);
138 com_err(__func__, retval, "whilst allocating memory");
142 size = llistxattr(filename, list, size);
144 com_err(__func__, errno, "llistxattr failed on %s", filename);
149 for (i = 0; i < size; i += strlen(&list[i]) + 1) {
150 const char *name = &list[i];
153 value_size = getxattr(filename, name, NULL, 0);
154 if (value_size == -1) {
155 com_err(__func__, errno, "getxattr failed on %s",
161 retval = ext2fs_get_mem(value_size, &value);
163 com_err(__func__, retval, "whilst allocating memory");
167 value_size = getxattr(filename, name, value, value_size);
168 if (value_size == -1) {
169 ext2fs_free_mem(&value);
170 com_err(__func__, errno, "getxattr failed on %s",
176 retval = ext2fs_xattr_set(handle, name, value, value_size);
177 ext2fs_free_mem(&value);
179 com_err(__func__, retval,
180 "while writing xattr %u", ino);
186 ext2fs_free_mem(&list);
187 close_retval = ext2fs_xattrs_close(&handle);
189 com_err(__func__, retval, "while closing inode %u", ino);
190 retval = retval ? retval : close_retval;
193 #else /* HAVE_LLISTXATTR */
195 #endif /* HAVE_LLISTXATTR */
198 /* Make a special files (block and character devices), fifo's, and sockets */
199 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
204 struct ext2_inode inode;
205 unsigned long devmajor, devminor, mode;
208 switch(st->st_mode & S_IFMT) {
210 mode = LINUX_S_IFCHR;
211 filetype = EXT2_FT_CHRDEV;
214 mode = LINUX_S_IFBLK;
215 filetype = EXT2_FT_BLKDEV;
218 mode = LINUX_S_IFIFO;
219 filetype = EXT2_FT_FIFO;
222 mode = LINUX_S_IFSOCK;
223 filetype = EXT2_FT_SOCK;
226 return EXT2_ET_INVALID_ARGUMENT;
229 if (!(fs->flags & EXT2_FLAG_RW)) {
230 com_err(__func__, 0, "Filesystem opened read/only");
233 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
235 com_err(__func__, retval, 0);
240 printf("Allocated inode: %u\n", ino);
242 retval = ext2fs_link(fs, cwd, name, ino, filetype);
243 if (retval == EXT2_ET_DIR_NO_SPACE) {
244 retval = ext2fs_expand_dir(fs, cwd);
246 com_err(__func__, retval, "while expanding directory");
249 retval = ext2fs_link(fs, cwd, name, ino, filetype);
252 com_err(name, retval, 0);
255 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino))
256 com_err(__func__, 0, "Warning: inode already set");
257 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
258 memset(&inode, 0, sizeof(inode));
260 inode.i_atime = inode.i_ctime = inode.i_mtime =
261 fs->now ? fs->now : time(0);
263 if (filetype != S_IFIFO) {
264 devmajor = major(st->st_rdev);
265 devminor = minor(st->st_rdev);
267 if ((devmajor < 256) && (devminor < 256)) {
268 inode.i_block[0] = devmajor * 256 + devminor;
269 inode.i_block[1] = 0;
271 inode.i_block[0] = 0;
272 inode.i_block[1] = (devminor & 0xff) | (devmajor << 8) |
273 ((devminor & ~0xff) << 12);
276 inode.i_links_count = 1;
278 retval = ext2fs_write_new_inode(fs, ino, &inode);
280 com_err(__func__, retval, "while creating inode %u", ino);
285 /* Make a symlink name -> target */
286 errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
287 char *target, ext2_ino_t root)
290 ext2_ino_t parent_ino;
293 cp = strrchr(name, '/');
296 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
298 com_err(name, retval, 0);
306 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
307 if (retval == EXT2_ET_DIR_NO_SPACE) {
308 retval = ext2fs_expand_dir(fs, parent_ino);
310 com_err("do_symlink_internal", retval,
311 "while expanding directory");
317 com_err("ext2fs_symlink", retval, 0);
321 /* Make a directory in the fs */
322 errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
323 struct stat *st, ext2_ino_t root)
326 ext2_ino_t parent_ino;
330 cp = strrchr(name, '/');
333 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
335 com_err(name, retval, 0);
343 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
344 if (retval == EXT2_ET_DIR_NO_SPACE) {
345 retval = ext2fs_expand_dir(fs, parent_ino);
347 com_err(__func__, retval, "while expanding directory");
353 com_err("ext2fs_mkdir", retval, 0);
357 static errcode_t copy_file(ext2_filsys fs, int fd, ext2_ino_t newfile,
358 int bufsize, int make_holes)
361 errcode_t retval, close_ret;
363 unsigned int written;
369 retval = ext2fs_file_open(fs, newfile,
370 EXT2_FILE_WRITE, &e2_file);
374 retval = ext2fs_get_mem(bufsize, &buf);
376 com_err("copy_file", retval, "can't allocate buffer\n");
380 /* This is used for checking whether the whole block is zero */
381 retval = ext2fs_get_memzero(bufsize, &zero_buf);
383 com_err("copy_file", retval, "can't allocate zero buffer\n");
388 got = read(fd, buf, bufsize);
399 /* Check whether all is zero */
400 cmp = memcmp(ptr, zero_buf, got);
402 /* The whole block is zero, make a hole */
403 retval = ext2fs_file_lseek(e2_file, got,
414 retval = ext2fs_file_write(e2_file, ptr,
425 ext2fs_free_mem(&zero_buf);
427 ext2fs_free_mem(&buf);
429 close_ret = ext2fs_file_close(e2_file);
435 static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino)
439 for (i = 0; i < hdlinks->count; i++) {
440 if (hdlinks->hdl[i].src_dev == dev &&
441 hdlinks->hdl[i].src_ino == ino)
447 /* Copy the native file to the fs */
448 errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
449 const char *dest, ext2_ino_t root)
455 struct ext2_inode inode;
456 int bufsize = IO_BUFSIZE;
459 fd = ext2fs_open_file(src, O_RDONLY, 0);
461 com_err(src, errno, 0);
464 if (fstat(fd, &statbuf) < 0) {
465 com_err(src, errno, 0);
470 retval = ext2fs_namei(fs, root, cwd, dest, &newfile);
473 return EXT2_ET_FILE_EXISTS;
476 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile);
478 com_err(__func__, retval, 0);
483 printf("Allocated inode: %u\n", newfile);
485 retval = ext2fs_link(fs, cwd, dest, newfile,
487 if (retval == EXT2_ET_DIR_NO_SPACE) {
488 retval = ext2fs_expand_dir(fs, cwd);
490 com_err(__func__, retval, "while expanding directory");
494 retval = ext2fs_link(fs, cwd, dest, newfile,
498 com_err(dest, retval, 0);
502 if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile))
503 com_err(__func__, 0, "Warning: inode already set");
504 ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
505 memset(&inode, 0, sizeof(inode));
506 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
507 inode.i_atime = inode.i_ctime = inode.i_mtime =
508 fs->now ? fs->now : time(0);
509 inode.i_links_count = 1;
510 retval = ext2fs_inode_size_set(fs, &inode, statbuf.st_size);
512 com_err(dest, retval, 0);
516 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
517 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
518 inode.i_flags |= EXT4_INLINE_DATA_FL;
519 } else if (fs->super->s_feature_incompat &
520 EXT3_FEATURE_INCOMPAT_EXTENTS) {
522 struct ext3_extent_header *eh;
524 eh = (struct ext3_extent_header *) &inode.i_block[0];
527 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
528 i = (sizeof(inode.i_block) - sizeof(*eh)) /
529 sizeof(struct ext3_extent);
530 eh->eh_max = ext2fs_cpu_to_le16(i);
531 inode.i_flags |= EXT4_EXTENTS_FL;
534 retval = ext2fs_write_new_inode(fs, newfile, &inode);
536 com_err(__func__, retval, "while creating inode %u", newfile);
540 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
541 retval = ext2fs_inline_data_init(fs, newfile);
543 com_err("copy_file", retval, 0);
548 if (LINUX_S_ISREG(inode.i_mode)) {
549 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
552 * Use I/O blocksize as buffer size when
553 * copying sparse files.
555 bufsize = statbuf.st_blksize;
557 retval = copy_file(fs, fd, newfile, bufsize, make_holes);
559 com_err("copy_file", retval, 0);
566 /* Copy files from source_dir to fs */
567 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
568 const char *source_dir, ext2_ino_t root,
569 struct hdlinks_s *hdlinks)
575 char ln_target[PATH_MAX];
576 unsigned int save_inode;
578 errcode_t retval = 0;
582 if (chdir(source_dir) < 0) {
583 com_err(__func__, errno,
584 _("while changing working directory to \"%s\""),
589 if (!(dh = opendir("."))) {
590 com_err(__func__, errno,
591 _("while opening directory \"%s\""), source_dir);
595 while ((dent = readdir(dh))) {
596 if ((!strcmp(dent->d_name, ".")) ||
597 (!strcmp(dent->d_name, "..")))
599 if (lstat(dent->d_name, &st)) {
600 com_err(__func__, errno, _("while lstat \"%s\""),
606 /* Check for hardlinks */
608 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
610 hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino);
612 retval = add_link(fs, parent_ino,
613 hdlinks->hdl[hdlink].dst_ino,
616 com_err(__func__, retval,
617 "while linking %s", name);
625 switch(st.st_mode & S_IFMT) {
630 retval = do_mknod_internal(fs, parent_ino, name, &st);
632 com_err(__func__, retval,
633 _("while creating special file "
639 read_cnt = readlink(name, ln_target,
640 sizeof(ln_target) - 1);
641 if (read_cnt == -1) {
642 com_err(__func__, errno,
643 _("while trying to readlink \"%s\""),
648 ln_target[read_cnt] = '\0';
649 retval = do_symlink_internal(fs, parent_ino, name,
652 com_err(__func__, retval,
653 _("while writing symlink\"%s\""),
659 retval = do_write_internal(fs, parent_ino, name, name,
662 com_err(__func__, retval,
663 _("while writing file \"%s\""), name);
668 retval = do_mkdir_internal(fs, parent_ino, name, &st,
671 com_err(__func__, retval,
672 _("while making dir \"%s\""), name);
675 retval = ext2fs_namei(fs, root, parent_ino,
678 com_err(name, retval, 0);
681 /* Populate the dir recursively*/
682 retval = __populate_fs(fs, ino, name, root, hdlinks);
684 com_err(__func__, retval,
685 _("while adding dir \"%s\""), name);
689 com_err(__func__, errno, _("during cd .."));
696 _("ignoring entry \"%s\""), name);
699 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
701 com_err(name, retval, 0);
705 retval = set_inode_extra(fs, parent_ino, ino, &st);
707 com_err(__func__, retval,
708 _("while setting inode for \"%s\""), name);
712 retval = set_inode_xattr(fs, ino, name);
714 com_err(__func__, retval,
715 _("while setting xattrs for \"%s\""), name);
719 /* Save the hardlink ino */
722 * Check whether need more memory, and we don't need
723 * free() since the lifespan will be over after the fs
726 if (hdlinks->count == hdlinks->size) {
727 void *p = realloc(hdlinks->hdl,
728 (hdlinks->size + HDLINK_CNT) *
729 sizeof(struct hdlink_s));
732 _("Not enough memory"));
733 retval = EXT2_ET_NO_MEMORY;
737 hdlinks->size += HDLINK_CNT;
739 hdlinks->hdl[hdlinks->count].src_dev = st.st_dev;
740 hdlinks->hdl[hdlinks->count].src_ino = st.st_ino;
741 hdlinks->hdl[hdlinks->count].dst_ino = ino;
751 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
752 const char *source_dir, ext2_ino_t root)
754 struct hdlinks_s hdlinks;
758 hdlinks.size = HDLINK_CNT;
759 hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s));
760 if (hdlinks.hdl == NULL) {
761 com_err(__func__, errno, "Not enough memory");
765 retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks);