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) {
521 ext2_extent_handle_t handle;
523 inode.i_flags &= ~EXT4_EXTENTS_FL;
524 retval = ext2fs_extent_open2(fs, newfile, &inode, &handle);
527 ext2fs_extent_free(handle);
530 retval = ext2fs_write_new_inode(fs, newfile, &inode);
532 com_err(__func__, retval, "while creating inode %u", newfile);
536 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
537 retval = ext2fs_inline_data_init(fs, newfile);
539 com_err("copy_file", retval, 0);
544 if (LINUX_S_ISREG(inode.i_mode)) {
545 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
548 * Use I/O blocksize as buffer size when
549 * copying sparse files.
551 bufsize = statbuf.st_blksize;
553 retval = copy_file(fs, fd, newfile, bufsize, make_holes);
555 com_err("copy_file", retval, 0);
562 /* Copy files from source_dir to fs */
563 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
564 const char *source_dir, ext2_ino_t root,
565 struct hdlinks_s *hdlinks)
571 char ln_target[PATH_MAX];
572 unsigned int save_inode;
574 errcode_t retval = 0;
578 if (chdir(source_dir) < 0) {
579 com_err(__func__, errno,
580 _("while changing working directory to \"%s\""),
585 if (!(dh = opendir("."))) {
586 com_err(__func__, errno,
587 _("while opening directory \"%s\""), source_dir);
591 while ((dent = readdir(dh))) {
592 if ((!strcmp(dent->d_name, ".")) ||
593 (!strcmp(dent->d_name, "..")))
595 if (lstat(dent->d_name, &st)) {
596 com_err(__func__, errno, _("while lstat \"%s\""),
602 /* Check for hardlinks */
604 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
606 hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino);
608 retval = add_link(fs, parent_ino,
609 hdlinks->hdl[hdlink].dst_ino,
612 com_err(__func__, retval,
613 "while linking %s", name);
621 switch(st.st_mode & S_IFMT) {
626 retval = do_mknod_internal(fs, parent_ino, name, &st);
628 com_err(__func__, retval,
629 _("while creating special file "
635 read_cnt = readlink(name, ln_target,
636 sizeof(ln_target) - 1);
637 if (read_cnt == -1) {
638 com_err(__func__, errno,
639 _("while trying to readlink \"%s\""),
644 ln_target[read_cnt] = '\0';
645 retval = do_symlink_internal(fs, parent_ino, name,
648 com_err(__func__, retval,
649 _("while writing symlink\"%s\""),
655 retval = do_write_internal(fs, parent_ino, name, name,
658 com_err(__func__, retval,
659 _("while writing file \"%s\""), name);
664 retval = do_mkdir_internal(fs, parent_ino, name, &st,
667 com_err(__func__, retval,
668 _("while making dir \"%s\""), name);
671 retval = ext2fs_namei(fs, root, parent_ino,
674 com_err(name, retval, 0);
677 /* Populate the dir recursively*/
678 retval = __populate_fs(fs, ino, name, root, hdlinks);
680 com_err(__func__, retval,
681 _("while adding dir \"%s\""), name);
685 com_err(__func__, errno, _("during cd .."));
692 _("ignoring entry \"%s\""), name);
695 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
697 com_err(name, retval, 0);
701 retval = set_inode_extra(fs, parent_ino, ino, &st);
703 com_err(__func__, retval,
704 _("while setting inode for \"%s\""), name);
708 retval = set_inode_xattr(fs, ino, name);
710 com_err(__func__, retval,
711 _("while setting xattrs for \"%s\""), name);
715 /* Save the hardlink ino */
718 * Check whether need more memory, and we don't need
719 * free() since the lifespan will be over after the fs
722 if (hdlinks->count == hdlinks->size) {
723 void *p = realloc(hdlinks->hdl,
724 (hdlinks->size + HDLINK_CNT) *
725 sizeof(struct hdlink_s));
728 _("Not enough memory"));
729 retval = EXT2_ET_NO_MEMORY;
733 hdlinks->size += HDLINK_CNT;
735 hdlinks->hdl[hdlinks->count].src_dev = st.st_dev;
736 hdlinks->hdl[hdlinks->count].src_ino = st.st_ino;
737 hdlinks->hdl[hdlinks->count].dst_ino = ino;
747 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
748 const char *source_dir, ext2_ino_t root)
750 struct hdlinks_s hdlinks;
754 hdlinks.size = HDLINK_CNT;
755 hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s));
756 if (hdlinks.hdl == NULL) {
757 com_err(__func__, errno, "Not enough memory");
761 retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks);