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.
12 #define _FILE_OFFSET_BITS 64
13 #define _LARGEFILE64_SOURCE 1
19 #include <sys/types.h>
21 #include <limits.h> /* for PATH_MAX */
22 #if defined HAVE_SYS_XATTR_H
23 #include <sys/xattr.h>
24 #elif defined HAVE_ATTR_XATTR_H
25 #include <attr/xattr.h>
27 #ifdef HAVE_SYS_IOCTL_H
28 #include <sys/ioctl.h>
30 #ifdef HAVE_SYS_SYSMACROS_H
31 #include <sys/sysmacros.h>
34 #include <ext2fs/ext2fs.h>
35 #include <ext2fs/ext2_types.h>
36 #include <ext2fs/fiemap.h>
38 #include "create_inode.h"
39 #include "support/nls-enable.h"
41 /* 64KiB is the minimum blksize to best minimize system call overhead. */
42 #define COPY_FILE_BUFLEN 65536
44 static int ext2_file_type(unsigned int mode)
46 if (LINUX_S_ISREG(mode))
47 return EXT2_FT_REG_FILE;
49 if (LINUX_S_ISDIR(mode))
52 if (LINUX_S_ISCHR(mode))
53 return EXT2_FT_CHRDEV;
55 if (LINUX_S_ISBLK(mode))
56 return EXT2_FT_BLKDEV;
58 if (LINUX_S_ISLNK(mode))
59 return EXT2_FT_SYMLINK;
61 if (LINUX_S_ISFIFO(mode))
64 if (LINUX_S_ISSOCK(mode))
70 /* Link an inode number to a directory */
71 static errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino,
72 ext2_ino_t ino, const char *name)
74 struct ext2_inode inode;
77 retval = ext2fs_read_inode(fs, ino, &inode);
79 com_err(__func__, retval, _("while reading inode %u"), ino);
83 retval = ext2fs_link(fs, parent_ino, name, ino,
84 ext2_file_type(inode.i_mode));
85 if (retval == EXT2_ET_DIR_NO_SPACE) {
86 retval = ext2fs_expand_dir(fs, parent_ino);
88 com_err(__func__, retval,
89 _("while expanding directory"));
92 retval = ext2fs_link(fs, parent_ino, name, ino,
93 ext2_file_type(inode.i_mode));
96 com_err(__func__, retval, _("while linking \"%s\""), name);
100 inode.i_links_count++;
102 retval = ext2fs_write_inode(fs, ino, &inode);
104 com_err(__func__, retval, _("while writing inode %u"), ino);
109 /* Set the uid, gid, mode and time for the inode */
110 static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t ino,
114 struct ext2_inode inode;
116 retval = ext2fs_read_inode(fs, ino, &inode);
118 com_err(__func__, retval, _("while reading inode %u"), ino);
122 inode.i_uid = st->st_uid;
123 inode.i_gid = st->st_gid;
124 inode.i_mode |= st->st_mode;
125 inode.i_atime = st->st_atime;
126 inode.i_mtime = st->st_mtime;
127 inode.i_ctime = st->st_ctime;
129 retval = ext2fs_write_inode(fs, ino, &inode);
131 com_err(__func__, retval, _("while writing inode %u"), ino);
135 #ifdef HAVE_LLISTXATTR
136 static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino,
137 const char *filename)
139 errcode_t retval, close_retval;
140 struct ext2_xattr_handle *handle;
141 ssize_t size, value_size;
148 size = llistxattr(filename, NULL, 0);
151 com_err(__func__, retval, _("while listing attributes of \"%s\""),
154 } else if (size == 0) {
158 retval = ext2fs_xattrs_open(fs, ino, &handle);
160 if (retval == EXT2_ET_MISSING_EA_FEATURE)
162 com_err(__func__, retval, _("while opening inode %u"), ino);
166 retval = ext2fs_get_mem(size, &list);
168 com_err(__func__, retval, _("while allocating memory"));
172 size = llistxattr(filename, list, size);
175 com_err(__func__, retval, _("while listing attributes of \"%s\""),
180 for (i = 0; i < size; i += strlen(&list[i]) + 1) {
181 const char *name = &list[i];
184 value_size = lgetxattr(filename, name, NULL, 0);
185 if (value_size == -1) {
187 com_err(__func__, retval,
188 _("while reading attribute \"%s\" of \"%s\""),
193 retval = ext2fs_get_mem(value_size, &value);
195 com_err(__func__, retval, _("while allocating memory"));
199 value_size = lgetxattr(filename, name, value, value_size);
200 if (value_size == -1) {
201 ext2fs_free_mem(&value);
203 com_err(__func__, retval,
204 _("while reading attribute \"%s\" of \"%s\""),
209 retval = ext2fs_xattr_set(handle, name, value, value_size);
210 ext2fs_free_mem(&value);
212 com_err(__func__, retval,
213 _("while writing attribute \"%s\" to inode %u"),
220 ext2fs_free_mem(&list);
221 close_retval = ext2fs_xattrs_close(&handle);
223 com_err(__func__, retval, _("while closing inode %u"), ino);
224 retval = retval ? retval : close_retval;
229 #else /* HAVE_LLISTXATTR */
230 static errcode_t set_inode_xattr(ext2_filsys fs EXT2FS_ATTR((unused)),
231 ext2_ino_t ino EXT2FS_ATTR((unused)),
232 const char *filename EXT2FS_ATTR((unused)))
236 #endif /* HAVE_LLISTXATTR */
239 /* Make a special files (block and character devices), fifo's, and sockets */
240 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
241 unsigned int st_mode, unsigned int st_rdev)
245 struct ext2_inode inode;
246 unsigned long devmajor, devminor, mode;
249 switch(st_mode & S_IFMT) {
251 mode = LINUX_S_IFCHR;
252 filetype = EXT2_FT_CHRDEV;
255 mode = LINUX_S_IFBLK;
256 filetype = EXT2_FT_BLKDEV;
259 mode = LINUX_S_IFIFO;
260 filetype = EXT2_FT_FIFO;
264 mode = LINUX_S_IFSOCK;
265 filetype = EXT2_FT_SOCK;
269 return EXT2_ET_INVALID_ARGUMENT;
272 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
274 com_err(__func__, retval, _("while allocating inode \"%s\""),
280 printf("Allocated inode: %u\n", ino);
282 retval = ext2fs_link(fs, cwd, name, ino, filetype);
283 if (retval == EXT2_ET_DIR_NO_SPACE) {
284 retval = ext2fs_expand_dir(fs, cwd);
286 com_err(__func__, retval,
287 _("while expanding directory"));
290 retval = ext2fs_link(fs, cwd, name, ino, filetype);
293 com_err(name, retval, _("while creating inode \"%s\""), name);
296 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino))
297 com_err(__func__, 0, "Warning: inode already set");
298 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
299 memset(&inode, 0, sizeof(inode));
301 inode.i_atime = inode.i_ctime = inode.i_mtime =
302 fs->now ? fs->now : time(0);
304 if (filetype != S_IFIFO) {
305 devmajor = major(st_rdev);
306 devminor = minor(st_rdev);
308 if ((devmajor < 256) && (devminor < 256)) {
309 inode.i_block[0] = devmajor * 256 + devminor;
310 inode.i_block[1] = 0;
312 inode.i_block[0] = 0;
313 inode.i_block[1] = (devminor & 0xff) | (devmajor << 8) |
314 ((devminor & ~0xff) << 12);
317 inode.i_links_count = 1;
319 retval = ext2fs_write_new_inode(fs, ino, &inode);
321 com_err(__func__, retval, _("while writing inode %u"), ino);
327 /* Make a symlink name -> target */
328 errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
329 char *target, ext2_ino_t root)
332 ext2_ino_t parent_ino;
335 cp = strrchr(name, '/');
338 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
340 com_err(name, retval, 0);
347 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
348 if (retval == EXT2_ET_DIR_NO_SPACE) {
349 retval = ext2fs_expand_dir(fs, parent_ino);
351 com_err("do_symlink_internal", retval,
352 _("while expanding directory"));
355 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
358 com_err("ext2fs_symlink", retval,
359 _("while creating symlink \"%s\""), name);
363 /* Make a directory in the fs */
364 errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
368 ext2_ino_t parent_ino;
372 cp = strrchr(name, '/');
375 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
377 com_err(name, retval, _("while looking up \"%s\""),
385 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
386 if (retval == EXT2_ET_DIR_NO_SPACE) {
387 retval = ext2fs_expand_dir(fs, parent_ino);
389 com_err(__func__, retval,
390 _("while expanding directory"));
393 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
396 com_err("ext2fs_mkdir", retval,
397 _("while creating directory \"%s\""), name);
401 #if !defined HAVE_PREAD64 && !defined HAVE_PREAD
402 static ssize_t my_pread(int fd, void *buf, size_t count, off_t offset)
404 if (lseek(fd, offset, SEEK_SET) < 0)
407 return read(fd, buf, count);
409 #endif /* !defined HAVE_PREAD64 && !defined HAVE_PREAD */
411 static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
412 off_t start, off_t end, char *buf,
417 unsigned int written;
421 for (off = start; off < end; off += COPY_FILE_BUFLEN) {
423 got = pread64(fd, buf, COPY_FILE_BUFLEN, off);
425 got = pread(fd, buf, COPY_FILE_BUFLEN, off);
427 got = my_pread(fd, buf, COPY_FILE_BUFLEN, off);
433 for (bpos = 0, ptr = buf; bpos < got; bpos += fs->blocksize) {
434 blen = fs->blocksize;
435 if (blen > got - bpos)
437 if (memcmp(ptr, zerobuf, blen) == 0) {
441 err = ext2fs_file_llseek(e2_file, off + bpos,
442 EXT2_SEEK_SET, NULL);
446 err = ext2fs_file_write(e2_file, ptr, blen,
463 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
464 static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
465 ext2_file_t e2_file, char *buf, char *zerobuf)
467 off_t data = 0, hole;
468 off_t data_blk, hole_blk;
471 /* Try to use SEEK_DATA and SEEK_HOLE */
472 while (data < statbuf->st_size) {
473 data = lseek(fd, data, SEEK_DATA);
477 return EXT2_ET_UNIMPLEMENTED;
479 hole = lseek(fd, data, SEEK_HOLE);
481 return EXT2_ET_UNIMPLEMENTED;
483 data_blk = data & ~(off_t)(fs->blocksize - 1);
484 hole_blk = (hole + (off_t)(fs->blocksize - 1)) & ~(off_t)(fs->blocksize - 1);
485 err = copy_file_chunk(fs, fd, e2_file, data_blk, hole_blk, buf,
495 #endif /* SEEK_DATA and SEEK_HOLE */
497 #if defined(FS_IOC_FIEMAP)
498 static errcode_t try_fiemap_copy(ext2_filsys fs, int fd, ext2_file_t e2_file,
499 char *buf, char *zerobuf)
501 #define EXTENT_MAX_COUNT 512
502 struct fiemap *fiemap_buf;
503 struct fiemap_extent *ext_buf, *ext;
504 int ext_buf_size, fie_buf_size;
509 ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
510 fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
512 err = ext2fs_get_memzero(fie_buf_size, &fiemap_buf);
516 ext_buf = fiemap_buf->fm_extents;
517 memset(fiemap_buf, 0, fie_buf_size);
518 fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
519 fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
520 fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
523 fiemap_buf->fm_start = pos;
524 memset(ext_buf, 0, ext_buf_size);
525 err = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
526 if (err < 0 && (errno == EOPNOTSUPP || errno == ENOTTY)) {
527 err = EXT2_ET_UNIMPLEMENTED;
529 } else if (err < 0) {
532 } else if (fiemap_buf->fm_mapped_extents == 0)
534 for (i = 0, ext = ext_buf; i < fiemap_buf->fm_mapped_extents;
536 err = copy_file_chunk(fs, fd, e2_file, ext->fe_logical,
537 ext->fe_logical + ext->fe_length,
544 /* Record file's logical offset this time */
545 pos = ext->fe_logical + ext->fe_length;
547 * If fm_extents array has been filled and
548 * there are extents left, continue to cycle.
550 } while (fiemap_buf->fm_mapped_extents == EXTENT_MAX_COUNT &&
551 !(ext->fe_flags & FIEMAP_EXTENT_LAST));
553 ext2fs_free_mem(&fiemap_buf);
556 #endif /* FS_IOC_FIEMAP */
558 static errcode_t copy_file(ext2_filsys fs, int fd, struct stat *statbuf,
562 char *buf = NULL, *zerobuf = NULL;
563 errcode_t err, close_err;
565 err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &e2_file);
569 err = ext2fs_get_mem(COPY_FILE_BUFLEN, &buf);
573 err = ext2fs_get_memzero(fs->blocksize, &zerobuf);
577 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
578 err = try_lseek_copy(fs, fd, statbuf, e2_file, buf, zerobuf);
579 if (err != EXT2_ET_UNIMPLEMENTED)
583 #if defined(FS_IOC_FIEMAP)
584 err = try_fiemap_copy(fs, fd, e2_file, buf, zerobuf);
585 if (err != EXT2_ET_UNIMPLEMENTED)
589 err = copy_file_chunk(fs, fd, e2_file, 0, statbuf->st_size, buf,
592 ext2fs_free_mem(&zerobuf);
593 ext2fs_free_mem(&buf);
594 close_err = ext2fs_file_close(e2_file);
600 static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino)
604 for (i = 0; i < hdlinks->count; i++) {
605 if (hdlinks->hdl[i].src_dev == dev &&
606 hdlinks->hdl[i].src_ino == ino)
612 /* Copy the native file to the fs */
613 errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
614 const char *dest, ext2_ino_t root)
620 struct ext2_inode inode;
622 fd = ext2fs_open_file(src, O_RDONLY, 0);
625 com_err(__func__, retval, _("while opening \"%s\" to copy"),
629 if (fstat(fd, &statbuf) < 0) {
634 retval = ext2fs_namei(fs, root, cwd, dest, &newfile);
636 retval = EXT2_ET_FILE_EXISTS;
640 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile);
644 printf("Allocated inode: %u\n", newfile);
646 retval = ext2fs_link(fs, cwd, dest, newfile,
648 if (retval == EXT2_ET_DIR_NO_SPACE) {
649 retval = ext2fs_expand_dir(fs, cwd);
652 retval = ext2fs_link(fs, cwd, dest, newfile,
657 if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile))
658 com_err(__func__, 0, "Warning: inode already set");
659 ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
660 memset(&inode, 0, sizeof(inode));
661 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
662 inode.i_atime = inode.i_ctime = inode.i_mtime =
663 fs->now ? fs->now : time(0);
664 inode.i_links_count = 1;
665 retval = ext2fs_inode_size_set(fs, &inode, statbuf.st_size);
668 if (ext2fs_has_feature_inline_data(fs->super)) {
669 inode.i_flags |= EXT4_INLINE_DATA_FL;
670 } else if (ext2fs_has_feature_extents(fs->super)) {
671 ext2_extent_handle_t handle;
673 inode.i_flags &= ~EXT4_EXTENTS_FL;
674 retval = ext2fs_extent_open2(fs, newfile, &inode, &handle);
677 ext2fs_extent_free(handle);
680 retval = ext2fs_write_new_inode(fs, newfile, &inode);
683 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
684 retval = ext2fs_inline_data_init(fs, newfile);
688 if (LINUX_S_ISREG(inode.i_mode)) {
689 retval = copy_file(fs, fd, &statbuf, newfile);
704 static errcode_t path_append(struct file_info *target, const char *file)
706 if (strlen(file) + target->path_len + 1 > target->path_max_len) {
707 target->path_max_len *= 2;
708 target->path = realloc(target->path, target->path_max_len);
710 return EXT2_ET_NO_MEMORY;
712 target->path_len += sprintf(target->path + target->path_len, "/%s",
717 /* Copy files from source_dir to fs */
718 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
719 const char *source_dir, ext2_ino_t root,
720 struct hdlinks_s *hdlinks,
721 struct file_info *target,
722 struct fs_ops_callbacks *fs_callbacks)
728 char *ln_target = NULL;
729 unsigned int save_inode;
731 errcode_t retval = 0;
734 size_t cur_dir_path_len;
736 if (chdir(source_dir) < 0) {
738 com_err(__func__, retval,
739 _("while changing working directory to \"%s\""),
744 if (!(dh = opendir("."))) {
746 com_err(__func__, retval,
747 _("while opening directory \"%s\""), source_dir);
751 while ((dent = readdir(dh))) {
752 if ((!strcmp(dent->d_name, ".")) ||
753 (!strcmp(dent->d_name, "..")))
755 if (lstat(dent->d_name, &st)) {
757 com_err(__func__, retval, _("while lstat \"%s\""),
763 /* Check for hardlinks */
765 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
767 hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino);
769 retval = add_link(fs, parent_ino,
770 hdlinks->hdl[hdlink].dst_ino,
773 com_err(__func__, retval,
774 "while linking %s", name);
782 cur_dir_path_len = target->path_len;
783 retval = path_append(target, name);
785 com_err(__func__, retval,
786 "while appending %s", name);
790 if (fs_callbacks && fs_callbacks->create_new_inode) {
791 retval = fs_callbacks->create_new_inode(fs,
792 target->path, name, parent_ino, root,
793 st.st_mode & S_IFMT);
798 switch(st.st_mode & S_IFMT) {
804 retval = do_mknod_internal(fs, parent_ino, name,
805 st.st_mode, st.st_rdev);
807 com_err(__func__, retval,
808 _("while creating special file "
814 ln_target = malloc(st.st_size + 1);
815 if (ln_target == NULL) {
816 com_err(__func__, retval,
820 read_cnt = readlink(name, ln_target,
822 if (read_cnt == -1) {
824 com_err(__func__, retval,
825 _("while trying to read link \"%s\""),
830 if (read_cnt > st.st_size) {
831 com_err(__func__, retval,
832 _("symlink increased in size "
833 "between lstat() and readlink()"));
837 ln_target[read_cnt] = '\0';
838 retval = do_symlink_internal(fs, parent_ino, name,
842 com_err(__func__, retval,
843 _("while writing symlink\"%s\""),
850 retval = do_write_internal(fs, parent_ino, name, name,
853 com_err(__func__, retval,
854 _("while writing file \"%s\""), name);
859 /* Don't choke on /lost+found */
860 if (parent_ino == EXT2_ROOT_INO &&
861 strcmp(name, "lost+found") == 0)
863 retval = do_mkdir_internal(fs, parent_ino, name,
866 com_err(__func__, retval,
867 _("while making dir \"%s\""), name);
871 retval = ext2fs_namei(fs, root, parent_ino,
874 com_err(name, retval, 0);
877 /* Populate the dir recursively*/
878 retval = __populate_fs(fs, ino, name, root, hdlinks,
879 target, fs_callbacks);
884 com_err(__func__, retval,
885 _("while changing directory"));
891 _("ignoring entry \"%s\""), name);
894 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
896 com_err(name, retval, _("while looking up \"%s\""),
901 retval = set_inode_extra(fs, ino, &st);
903 com_err(__func__, retval,
904 _("while setting inode for \"%s\""), name);
908 retval = set_inode_xattr(fs, ino, name);
910 com_err(__func__, retval,
911 _("while setting xattrs for \"%s\""), name);
915 if (fs_callbacks && fs_callbacks->end_create_new_inode) {
916 retval = fs_callbacks->end_create_new_inode(fs,
917 target->path, name, parent_ino, root,
918 st.st_mode & S_IFMT);
923 /* Save the hardlink ino */
926 * Check whether need more memory, and we don't need
927 * free() since the lifespan will be over after the fs
930 if (hdlinks->count == hdlinks->size) {
931 void *p = realloc(hdlinks->hdl,
932 (hdlinks->size + HDLINK_CNT) *
933 sizeof(struct hdlink_s));
935 retval = EXT2_ET_NO_MEMORY;
936 com_err(name, retval,
937 _("while saving inode data"));
941 hdlinks->size += HDLINK_CNT;
943 hdlinks->hdl[hdlinks->count].src_dev = st.st_dev;
944 hdlinks->hdl[hdlinks->count].src_ino = st.st_ino;
945 hdlinks->hdl[hdlinks->count].dst_ino = ino;
948 target->path_len = cur_dir_path_len;
949 target->path[target->path_len] = 0;
957 errcode_t populate_fs2(ext2_filsys fs, ext2_ino_t parent_ino,
958 const char *source_dir, ext2_ino_t root,
959 struct fs_ops_callbacks *fs_callbacks)
961 struct file_info file_info;
962 struct hdlinks_s hdlinks;
965 if (!(fs->flags & EXT2_FLAG_RW)) {
966 com_err(__func__, 0, "Filesystem opened readonly");
971 hdlinks.size = HDLINK_CNT;
972 hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s));
973 if (hdlinks.hdl == NULL) {
975 com_err(__func__, retval, _("while allocating memory"));
979 file_info.path_len = 0;
980 file_info.path_max_len = 255;
981 file_info.path = calloc(file_info.path_max_len, 1);
983 retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks,
984 &file_info, fs_callbacks);
986 free(file_info.path);
991 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
992 const char *source_dir, ext2_ino_t root)
994 return populate_fs2(fs, parent_ino, source_dir, root, NULL);