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
18 #include <sys/types.h>
20 #include <limits.h> /* for PATH_MAX */
21 #ifdef HAVE_ATTR_XATTR_H
22 #include <attr/xattr.h>
24 #include <sys/ioctl.h>
25 #include <ext2fs/ext2fs.h>
26 #include <ext2fs/ext2_types.h>
27 #include <ext2fs/fiemap.h>
29 #include "create_inode.h"
30 #include "nls-enable.h"
32 #if __STDC_VERSION__ < 199901L
34 # define __func__ __FUNCTION__
36 # define __func__ "<unknown>"
40 /* 64KiB is the minimium blksize to best minimize system call overhead. */
41 #define COPY_FILE_BUFLEN 65536
43 static int ext2_file_type(unsigned int mode)
45 if (LINUX_S_ISREG(mode))
46 return EXT2_FT_REG_FILE;
48 if (LINUX_S_ISDIR(mode))
51 if (LINUX_S_ISCHR(mode))
52 return EXT2_FT_CHRDEV;
54 if (LINUX_S_ISBLK(mode))
55 return EXT2_FT_BLKDEV;
57 if (LINUX_S_ISLNK(mode))
58 return EXT2_FT_SYMLINK;
60 if (LINUX_S_ISFIFO(mode))
63 if (LINUX_S_ISSOCK(mode))
69 /* Link an inode number to a directory */
70 static errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino,
71 ext2_ino_t ino, const char *name)
73 struct ext2_inode inode;
76 retval = ext2fs_read_inode(fs, ino, &inode);
78 com_err(__func__, retval, _("while reading inode %u"), ino);
82 retval = ext2fs_link(fs, parent_ino, name, ino,
83 ext2_file_type(inode.i_mode));
84 if (retval == EXT2_ET_DIR_NO_SPACE) {
85 retval = ext2fs_expand_dir(fs, parent_ino);
87 com_err(__func__, retval,
88 _("while expanding directory"));
91 retval = ext2fs_link(fs, parent_ino, name, ino,
92 ext2_file_type(inode.i_mode));
95 com_err(__func__, retval, _("while linking \"%s\""), name);
99 inode.i_links_count++;
101 retval = ext2fs_write_inode(fs, ino, &inode);
103 com_err(__func__, retval, _("while writing inode %u"), ino);
108 /* Set the uid, gid, mode and time for the inode */
109 static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd,
110 ext2_ino_t ino, struct stat *st)
113 struct ext2_inode inode;
115 retval = ext2fs_read_inode(fs, ino, &inode);
117 com_err(__func__, retval, _("while reading inode %u"), ino);
121 inode.i_uid = st->st_uid;
122 inode.i_gid = st->st_gid;
123 inode.i_mode |= st->st_mode;
124 inode.i_atime = st->st_atime;
125 inode.i_mtime = st->st_mtime;
126 inode.i_ctime = st->st_ctime;
128 retval = ext2fs_write_inode(fs, ino, &inode);
130 com_err(__func__, retval, _("while writing inode %u"), ino);
134 static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino,
135 const char *filename)
137 #ifdef HAVE_LLISTXATTR
138 errcode_t retval, close_retval;
139 struct ext2_xattr_handle *handle;
140 ssize_t size, value_size;
144 size = llistxattr(filename, NULL, 0);
147 com_err(__func__, retval, _("while listing attributes of \"%s\""),
150 } else if (size == 0) {
154 retval = ext2fs_xattrs_open(fs, ino, &handle);
156 if (retval == EXT2_ET_MISSING_EA_FEATURE)
158 com_err(__func__, retval, _("while opening inode %u"), ino);
162 retval = ext2fs_get_mem(size, &list);
164 com_err(__func__, retval, _("while allocating memory"));
168 size = llistxattr(filename, list, size);
171 com_err(__func__, retval, _("while listing attributes of \"%s\""),
176 for (i = 0; i < size; i += strlen(&list[i]) + 1) {
177 const char *name = &list[i];
180 value_size = getxattr(filename, name, NULL, 0);
181 if (value_size == -1) {
183 com_err(__func__, retval,
184 _("while reading attribute \"%s\" of \"%s\""),
189 retval = ext2fs_get_mem(value_size, &value);
191 com_err(__func__, retval, _("while allocating memory"));
195 value_size = getxattr(filename, name, value, value_size);
196 if (value_size == -1) {
197 ext2fs_free_mem(&value);
199 com_err(__func__, retval,
200 _("while reading attribute \"%s\" of \"%s\""),
205 retval = ext2fs_xattr_set(handle, name, value, value_size);
206 ext2fs_free_mem(&value);
208 com_err(__func__, retval,
209 _("while writing attribute \"%s\" to inode %u"),
216 ext2fs_free_mem(&list);
217 close_retval = ext2fs_xattrs_close(&handle);
219 com_err(__func__, retval, _("while closing inode %u"), ino);
220 retval = retval ? retval : close_retval;
223 #else /* HAVE_LLISTXATTR */
225 #endif /* HAVE_LLISTXATTR */
228 /* Make a special files (block and character devices), fifo's, and sockets */
229 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
234 struct ext2_inode inode;
235 unsigned long devmajor, devminor, mode;
238 switch(st->st_mode & S_IFMT) {
240 mode = LINUX_S_IFCHR;
241 filetype = EXT2_FT_CHRDEV;
244 mode = LINUX_S_IFBLK;
245 filetype = EXT2_FT_BLKDEV;
248 mode = LINUX_S_IFIFO;
249 filetype = EXT2_FT_FIFO;
252 mode = LINUX_S_IFSOCK;
253 filetype = EXT2_FT_SOCK;
256 return EXT2_ET_INVALID_ARGUMENT;
259 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
261 com_err(__func__, retval, _("while allocating inode \"%s\""),
267 printf("Allocated inode: %u\n", ino);
269 retval = ext2fs_link(fs, cwd, name, ino, filetype);
270 if (retval == EXT2_ET_DIR_NO_SPACE) {
271 retval = ext2fs_expand_dir(fs, cwd);
273 com_err(__func__, retval,
274 _("while expanding directory"));
277 retval = ext2fs_link(fs, cwd, name, ino, filetype);
280 com_err(name, retval, _("while creating inode \"%s\""), name);
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 writing 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);
333 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
334 if (retval == EXT2_ET_DIR_NO_SPACE) {
335 retval = ext2fs_expand_dir(fs, parent_ino);
337 com_err("do_symlink_internal", retval,
338 _("while expanding directory"));
341 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
344 com_err("ext2fs_symlink", retval,
345 _("while creating symlink \"%s\""), name);
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, _("while looking up \"%s\""),
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,
376 _("while expanding directory"));
379 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
382 com_err("ext2fs_mkdir", retval,
383 _("while creating directory \"%s\""), name);
387 #if !defined HAVE_PREAD64 && !defined HAVE_PREAD
388 static ssize_t my_pread(int fd, const void *buf, size_t count, off_t offset)
390 if (lseek(fd, offset, SEEK_SET) < 0)
393 return read(fd, buf, count);
395 #endif /* !defined HAVE_PREAD64 && !defined HAVE_PREAD */
397 static errcode_t copy_file_range(ext2_filsys fs, int fd, ext2_file_t e2_file,
398 off_t start, off_t end, char *buf,
403 unsigned int written;
407 for (off = start; off < end; off += COPY_FILE_BUFLEN) {
409 got = pread64(fd, buf, COPY_FILE_BUFLEN, off);
411 got = pread(fd, buf, COPY_FILE_BUFLEN, off);
413 got = my_pread(fd, buf, COPY_FILE_BUFLEN, off);
419 for (bpos = 0, ptr = buf; bpos < got; bpos += fs->blocksize) {
420 blen = fs->blocksize;
421 if (blen > got - bpos)
423 if (memcmp(ptr, zerobuf, blen) == 0) {
427 err = ext2fs_file_lseek(e2_file, off + bpos,
428 EXT2_SEEK_SET, NULL);
432 err = ext2fs_file_write(e2_file, ptr, blen,
449 static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
450 ext2_file_t e2_file, char *buf, char *zerobuf)
452 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
453 off_t data = 0, hole;
454 off_t data_blk, hole_blk;
457 /* Try to use SEEK_DATA and SEEK_HOLE */
458 while (data < statbuf->st_size) {
459 data = lseek(fd, data, SEEK_DATA);
463 return EXT2_ET_UNIMPLEMENTED;
465 hole = lseek(fd, data, SEEK_HOLE);
467 return EXT2_ET_UNIMPLEMENTED;
469 data_blk = data & ~(fs->blocksize - 1);
470 hole_blk = (hole + (fs->blocksize - 1)) & ~(fs->blocksize - 1);
471 err = copy_file_range(fs, fd, e2_file, data_blk, hole_blk, buf,
481 return EXT2_ET_UNIMPLEMENTED;
482 #endif /* SEEK_DATA and SEEK_HOLE */
485 static errcode_t try_fiemap_copy(ext2_filsys fs, int fd, ext2_file_t e2_file,
486 char *buf, char *zerobuf)
488 #if defined(FS_IOC_FIEMAP)
489 #define EXTENT_MAX_COUNT 512
490 struct fiemap *fiemap_buf;
491 struct fiemap_extent *ext_buf, *ext;
492 int ext_buf_size, fie_buf_size;
497 ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
498 fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
500 err = ext2fs_get_memzero(fie_buf_size, &fiemap_buf);
504 ext_buf = fiemap_buf->fm_extents;
505 memset(fiemap_buf, 0, fie_buf_size);
506 fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
507 fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
508 fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
511 fiemap_buf->fm_start = pos;
512 memset(ext_buf, 0, ext_buf_size);
513 err = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
514 if (err < 0 && (errno == EOPNOTSUPP || errno == ENOTTY)) {
515 err = EXT2_ET_UNIMPLEMENTED;
517 } else if (err < 0 || fiemap_buf->fm_mapped_extents == 0) {
521 for (i = 0, ext = ext_buf; i < fiemap_buf->fm_mapped_extents;
523 err = copy_file_range(fs, fd, e2_file, ext->fe_logical,
524 ext->fe_logical + ext->fe_length,
531 /* Record file's logical offset this time */
532 pos = ext->fe_logical + ext->fe_length;
534 * If fm_extents array has been filled and
535 * there are extents left, continue to cycle.
537 } while (fiemap_buf->fm_mapped_extents == EXTENT_MAX_COUNT &&
538 !(ext->fe_flags & FIEMAP_EXTENT_LAST));
540 ext2fs_free_mem(&fiemap_buf);
543 return EXT2_ET_UNIMPLEMENTED;
544 #endif /* FS_IOC_FIEMAP */
547 static errcode_t copy_file(ext2_filsys fs, int fd, struct stat *statbuf,
551 char *buf = NULL, *zerobuf = NULL;
552 errcode_t err, close_err;
554 err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &e2_file);
558 err = ext2fs_get_mem(COPY_FILE_BUFLEN, &buf);
562 err = ext2fs_get_memzero(fs->blocksize, &zerobuf);
566 err = try_lseek_copy(fs, fd, statbuf, e2_file, buf, zerobuf);
567 if (err != EXT2_ET_UNIMPLEMENTED)
570 err = try_fiemap_copy(fs, fd, e2_file, buf, zerobuf);
571 if (err != EXT2_ET_UNIMPLEMENTED)
574 err = copy_file_range(fs, fd, e2_file, 0, statbuf->st_size, buf,
577 ext2fs_free_mem(&zerobuf);
578 ext2fs_free_mem(&buf);
579 close_err = ext2fs_file_close(e2_file);
585 static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino)
589 for (i = 0; i < hdlinks->count; i++) {
590 if (hdlinks->hdl[i].src_dev == dev &&
591 hdlinks->hdl[i].src_ino == ino)
597 /* Copy the native file to the fs */
598 errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
599 const char *dest, ext2_ino_t root)
605 struct ext2_inode inode;
607 fd = ext2fs_open_file(src, O_RDONLY, 0);
610 com_err(__func__, retval, _("while opening \"%s\" to copy"),
614 if (fstat(fd, &statbuf) < 0) {
619 retval = ext2fs_namei(fs, root, cwd, dest, &newfile);
621 retval = EXT2_ET_FILE_EXISTS;
625 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile);
629 printf("Allocated inode: %u\n", newfile);
631 retval = ext2fs_link(fs, cwd, dest, newfile,
633 if (retval == EXT2_ET_DIR_NO_SPACE) {
634 retval = ext2fs_expand_dir(fs, cwd);
637 retval = ext2fs_link(fs, cwd, dest, newfile,
642 if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile))
643 com_err(__func__, 0, "Warning: inode already set");
644 ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
645 memset(&inode, 0, sizeof(inode));
646 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
647 inode.i_atime = inode.i_ctime = inode.i_mtime =
648 fs->now ? fs->now : time(0);
649 inode.i_links_count = 1;
650 retval = ext2fs_inode_size_set(fs, &inode, statbuf.st_size);
653 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
654 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
655 inode.i_flags |= EXT4_INLINE_DATA_FL;
656 } else if (fs->super->s_feature_incompat &
657 EXT3_FEATURE_INCOMPAT_EXTENTS) {
658 ext2_extent_handle_t handle;
660 inode.i_flags &= ~EXT4_EXTENTS_FL;
661 retval = ext2fs_extent_open2(fs, newfile, &inode, &handle);
664 ext2fs_extent_free(handle);
667 retval = ext2fs_write_new_inode(fs, newfile, &inode);
670 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
671 retval = ext2fs_inline_data_init(fs, newfile);
675 if (LINUX_S_ISREG(inode.i_mode)) {
676 retval = copy_file(fs, fd, &statbuf, newfile);
685 /* Copy files from source_dir to fs */
686 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
687 const char *source_dir, ext2_ino_t root,
688 struct hdlinks_s *hdlinks)
694 char ln_target[PATH_MAX];
695 unsigned int save_inode;
697 errcode_t retval = 0;
701 if (chdir(source_dir) < 0) {
703 com_err(__func__, retval,
704 _("while changing working directory to \"%s\""),
709 if (!(dh = opendir("."))) {
711 com_err(__func__, retval,
712 _("while opening directory \"%s\""), source_dir);
716 while ((dent = readdir(dh))) {
717 if ((!strcmp(dent->d_name, ".")) ||
718 (!strcmp(dent->d_name, "..")))
720 if (lstat(dent->d_name, &st)) {
722 com_err(__func__, retval, _("while lstat \"%s\""),
728 /* Check for hardlinks */
730 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
732 hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino);
734 retval = add_link(fs, parent_ino,
735 hdlinks->hdl[hdlink].dst_ino,
738 com_err(__func__, retval,
739 "while linking %s", name);
747 switch(st.st_mode & S_IFMT) {
752 retval = do_mknod_internal(fs, parent_ino, name, &st);
754 com_err(__func__, retval,
755 _("while creating special file "
761 read_cnt = readlink(name, ln_target,
762 sizeof(ln_target) - 1);
763 if (read_cnt == -1) {
765 com_err(__func__, retval,
766 _("while trying to read link \"%s\""),
770 ln_target[read_cnt] = '\0';
771 retval = do_symlink_internal(fs, parent_ino, name,
774 com_err(__func__, retval,
775 _("while writing symlink\"%s\""),
781 retval = do_write_internal(fs, parent_ino, name, name,
784 com_err(__func__, retval,
785 _("while writing file \"%s\""), name);
790 /* Don't choke on /lost+found */
791 if (parent_ino == EXT2_ROOT_INO &&
792 strcmp(name, "lost+found") == 0)
794 retval = do_mkdir_internal(fs, parent_ino, name, &st,
797 com_err(__func__, retval,
798 _("while making dir \"%s\""), name);
802 retval = ext2fs_namei(fs, root, parent_ino,
805 com_err(name, retval, 0);
808 /* Populate the dir recursively*/
809 retval = __populate_fs(fs, ino, name, root, hdlinks);
814 com_err(__func__, retval,
815 _("while changing directory"));
821 _("ignoring entry \"%s\""), name);
824 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
826 com_err(name, retval, _("while looking up \"%s\""),
831 retval = set_inode_extra(fs, parent_ino, ino, &st);
833 com_err(__func__, retval,
834 _("while setting inode for \"%s\""), name);
838 retval = set_inode_xattr(fs, ino, name);
840 com_err(__func__, retval,
841 _("while setting xattrs for \"%s\""), name);
845 /* Save the hardlink ino */
848 * Check whether need more memory, and we don't need
849 * free() since the lifespan will be over after the fs
852 if (hdlinks->count == hdlinks->size) {
853 void *p = realloc(hdlinks->hdl,
854 (hdlinks->size + HDLINK_CNT) *
855 sizeof(struct hdlink_s));
857 retval = EXT2_ET_NO_MEMORY;
858 com_err(name, retval,
859 _("while saving inode data"));
863 hdlinks->size += HDLINK_CNT;
865 hdlinks->hdl[hdlinks->count].src_dev = st.st_dev;
866 hdlinks->hdl[hdlinks->count].src_ino = st.st_ino;
867 hdlinks->hdl[hdlinks->count].dst_ino = ino;
877 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
878 const char *source_dir, ext2_ino_t root)
880 struct hdlinks_s hdlinks;
883 if (!(fs->flags & EXT2_FLAG_RW)) {
884 com_err(__func__, 0, "Filesystem opened readonly");
889 hdlinks.size = HDLINK_CNT;
890 hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s));
891 if (hdlinks.hdl == NULL) {
893 com_err(__func__, retval, _("while allocating memory"));
897 retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks);