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 "support/nls-enable.h"
32 /* 64KiB is the minimium blksize to best minimize system call overhead. */
33 #define COPY_FILE_BUFLEN 65536
35 static int ext2_file_type(unsigned int mode
)
37 if (LINUX_S_ISREG(mode
))
38 return EXT2_FT_REG_FILE
;
40 if (LINUX_S_ISDIR(mode
))
43 if (LINUX_S_ISCHR(mode
))
44 return EXT2_FT_CHRDEV
;
46 if (LINUX_S_ISBLK(mode
))
47 return EXT2_FT_BLKDEV
;
49 if (LINUX_S_ISLNK(mode
))
50 return EXT2_FT_SYMLINK
;
52 if (LINUX_S_ISFIFO(mode
))
55 if (LINUX_S_ISSOCK(mode
))
61 /* Link an inode number to a directory */
62 static errcode_t
add_link(ext2_filsys fs
, ext2_ino_t parent_ino
,
63 ext2_ino_t ino
, const char *name
)
65 struct ext2_inode inode
;
68 retval
= ext2fs_read_inode(fs
, ino
, &inode
);
70 com_err(__func__
, retval
, _("while reading inode %u"), ino
);
74 retval
= ext2fs_link(fs
, parent_ino
, name
, ino
,
75 ext2_file_type(inode
.i_mode
));
76 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
77 retval
= ext2fs_expand_dir(fs
, parent_ino
);
79 com_err(__func__
, retval
,
80 _("while expanding directory"));
83 retval
= ext2fs_link(fs
, parent_ino
, name
, ino
,
84 ext2_file_type(inode
.i_mode
));
87 com_err(__func__
, retval
, _("while linking \"%s\""), name
);
91 inode
.i_links_count
++;
93 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
95 com_err(__func__
, retval
, _("while writing inode %u"), ino
);
100 /* Set the uid, gid, mode and time for the inode */
101 static errcode_t
set_inode_extra(ext2_filsys fs
, ext2_ino_t ino
,
105 struct ext2_inode inode
;
107 retval
= ext2fs_read_inode(fs
, ino
, &inode
);
109 com_err(__func__
, retval
, _("while reading inode %u"), ino
);
113 inode
.i_uid
= st
->st_uid
;
114 inode
.i_gid
= st
->st_gid
;
115 inode
.i_mode
|= st
->st_mode
;
116 inode
.i_atime
= st
->st_atime
;
117 inode
.i_mtime
= st
->st_mtime
;
118 inode
.i_ctime
= st
->st_ctime
;
120 retval
= ext2fs_write_inode(fs
, ino
, &inode
);
122 com_err(__func__
, retval
, _("while writing inode %u"), ino
);
126 #ifdef HAVE_LLISTXATTR
127 static errcode_t
set_inode_xattr(ext2_filsys fs
, ext2_ino_t ino
,
128 const char *filename
)
130 errcode_t retval
, close_retval
;
131 struct ext2_xattr_handle
*handle
;
132 ssize_t size
, value_size
;
136 size
= llistxattr(filename
, NULL
, 0);
139 com_err(__func__
, retval
, _("while listing attributes of \"%s\""),
142 } else if (size
== 0) {
146 retval
= ext2fs_xattrs_open(fs
, ino
, &handle
);
148 if (retval
== EXT2_ET_MISSING_EA_FEATURE
)
150 com_err(__func__
, retval
, _("while opening inode %u"), ino
);
154 retval
= ext2fs_get_mem(size
, &list
);
156 com_err(__func__
, retval
, _("while allocating memory"));
160 size
= llistxattr(filename
, list
, size
);
163 com_err(__func__
, retval
, _("while listing attributes of \"%s\""),
168 for (i
= 0; i
< size
; i
+= strlen(&list
[i
]) + 1) {
169 const char *name
= &list
[i
];
172 value_size
= lgetxattr(filename
, name
, NULL
, 0);
173 if (value_size
== -1) {
175 com_err(__func__
, retval
,
176 _("while reading attribute \"%s\" of \"%s\""),
181 retval
= ext2fs_get_mem(value_size
, &value
);
183 com_err(__func__
, retval
, _("while allocating memory"));
187 value_size
= lgetxattr(filename
, name
, value
, value_size
);
188 if (value_size
== -1) {
189 ext2fs_free_mem(&value
);
191 com_err(__func__
, retval
,
192 _("while reading attribute \"%s\" of \"%s\""),
197 retval
= ext2fs_xattr_set(handle
, name
, value
, value_size
);
198 ext2fs_free_mem(&value
);
200 com_err(__func__
, retval
,
201 _("while writing attribute \"%s\" to inode %u"),
208 ext2fs_free_mem(&list
);
209 close_retval
= ext2fs_xattrs_close(&handle
);
211 com_err(__func__
, retval
, _("while closing inode %u"), ino
);
212 retval
= retval
? retval
: close_retval
;
217 #else /* HAVE_LLISTXATTR */
218 static errcode_t
set_inode_xattr(ext2_filsys fs
EXT2FS_ATTR((unused
)),
219 ext2_ino_t ino
EXT2FS_ATTR((unused
)),
220 const char *filename
EXT2FS_ATTR((unused
)))
224 #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 retval
= ext2fs_new_inode(fs
, cwd
, 010755, 0, &ino
);
259 com_err(__func__
, retval
, _("while allocating inode \"%s\""),
265 printf("Allocated inode: %u\n", ino
);
267 retval
= ext2fs_link(fs
, cwd
, name
, ino
, filetype
);
268 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
269 retval
= ext2fs_expand_dir(fs
, cwd
);
271 com_err(__func__
, retval
,
272 _("while expanding directory"));
275 retval
= ext2fs_link(fs
, cwd
, name
, ino
, filetype
);
278 com_err(name
, retval
, _("while creating inode \"%s\""), name
);
281 if (ext2fs_test_inode_bitmap2(fs
->inode_map
, ino
))
282 com_err(__func__
, 0, "Warning: inode already set");
283 ext2fs_inode_alloc_stats2(fs
, ino
, +1, 0);
284 memset(&inode
, 0, sizeof(inode
));
286 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
287 fs
->now
? fs
->now
: time(0);
289 if (filetype
!= S_IFIFO
) {
290 devmajor
= major(st
->st_rdev
);
291 devminor
= minor(st
->st_rdev
);
293 if ((devmajor
< 256) && (devminor
< 256)) {
294 inode
.i_block
[0] = devmajor
* 256 + devminor
;
295 inode
.i_block
[1] = 0;
297 inode
.i_block
[0] = 0;
298 inode
.i_block
[1] = (devminor
& 0xff) | (devmajor
<< 8) |
299 ((devminor
& ~0xff) << 12);
302 inode
.i_links_count
= 1;
304 retval
= ext2fs_write_new_inode(fs
, ino
, &inode
);
306 com_err(__func__
, retval
, _("while writing inode %u"), ino
);
311 /* Make a symlink name -> target */
312 errcode_t
do_symlink_internal(ext2_filsys fs
, ext2_ino_t cwd
, const char *name
,
313 char *target
, ext2_ino_t root
)
316 ext2_ino_t parent_ino
;
319 cp
= strrchr(name
, '/');
322 retval
= ext2fs_namei(fs
, root
, cwd
, name
, &parent_ino
);
324 com_err(name
, retval
, 0);
331 retval
= ext2fs_symlink(fs
, parent_ino
, 0, name
, target
);
332 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
333 retval
= ext2fs_expand_dir(fs
, parent_ino
);
335 com_err("do_symlink_internal", retval
,
336 _("while expanding directory"));
339 retval
= ext2fs_symlink(fs
, parent_ino
, 0, name
, target
);
342 com_err("ext2fs_symlink", retval
,
343 _("while creating symlink \"%s\""), name
);
347 /* Make a directory in the fs */
348 errcode_t
do_mkdir_internal(ext2_filsys fs
, ext2_ino_t cwd
, const char *name
,
352 ext2_ino_t parent_ino
;
356 cp
= strrchr(name
, '/');
359 retval
= ext2fs_namei(fs
, root
, cwd
, name
, &parent_ino
);
361 com_err(name
, retval
, _("while looking up \"%s\""),
369 retval
= ext2fs_mkdir(fs
, parent_ino
, 0, name
);
370 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
371 retval
= ext2fs_expand_dir(fs
, parent_ino
);
373 com_err(__func__
, retval
,
374 _("while expanding directory"));
377 retval
= ext2fs_mkdir(fs
, parent_ino
, 0, name
);
380 com_err("ext2fs_mkdir", retval
,
381 _("while creating directory \"%s\""), name
);
385 #if !defined HAVE_PREAD64 && !defined HAVE_PREAD
386 static ssize_t
my_pread(int fd
, void *buf
, size_t count
, off_t offset
)
388 if (lseek(fd
, offset
, SEEK_SET
) < 0)
391 return read(fd
, buf
, count
);
393 #endif /* !defined HAVE_PREAD64 && !defined HAVE_PREAD */
395 static errcode_t
copy_file_range(ext2_filsys fs
, int fd
, ext2_file_t e2_file
,
396 off_t start
, off_t end
, char *buf
,
401 unsigned int written
;
405 for (off
= start
; off
< end
; off
+= COPY_FILE_BUFLEN
) {
407 got
= pread64(fd
, buf
, COPY_FILE_BUFLEN
, off
);
409 got
= pread(fd
, buf
, COPY_FILE_BUFLEN
, off
);
411 got
= my_pread(fd
, buf
, COPY_FILE_BUFLEN
, off
);
417 for (bpos
= 0, ptr
= buf
; bpos
< got
; bpos
+= fs
->blocksize
) {
418 blen
= fs
->blocksize
;
419 if (blen
> got
- bpos
)
421 if (memcmp(ptr
, zerobuf
, blen
) == 0) {
425 err
= ext2fs_file_lseek(e2_file
, off
+ bpos
,
426 EXT2_SEEK_SET
, NULL
);
430 err
= ext2fs_file_write(e2_file
, ptr
, blen
,
447 static errcode_t
try_lseek_copy(ext2_filsys fs
, int fd
, struct stat
*statbuf
,
448 ext2_file_t e2_file
, char *buf
, char *zerobuf
)
450 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
451 off_t data
= 0, hole
;
452 off_t data_blk
, hole_blk
;
455 /* Try to use SEEK_DATA and SEEK_HOLE */
456 while (data
< statbuf
->st_size
) {
457 data
= lseek(fd
, data
, SEEK_DATA
);
461 return EXT2_ET_UNIMPLEMENTED
;
463 hole
= lseek(fd
, data
, SEEK_HOLE
);
465 return EXT2_ET_UNIMPLEMENTED
;
467 data_blk
= data
& ~(fs
->blocksize
- 1);
468 hole_blk
= (hole
+ (fs
->blocksize
- 1)) & ~(fs
->blocksize
- 1);
469 err
= copy_file_range(fs
, fd
, e2_file
, data_blk
, hole_blk
, buf
,
479 return EXT2_ET_UNIMPLEMENTED
;
480 #endif /* SEEK_DATA and SEEK_HOLE */
483 static errcode_t
try_fiemap_copy(ext2_filsys fs
, int fd
, ext2_file_t e2_file
,
484 char *buf
, char *zerobuf
)
486 #if defined(FS_IOC_FIEMAP)
487 #define EXTENT_MAX_COUNT 512
488 struct fiemap
*fiemap_buf
;
489 struct fiemap_extent
*ext_buf
, *ext
;
490 int ext_buf_size
, fie_buf_size
;
495 ext_buf_size
= EXTENT_MAX_COUNT
* sizeof(struct fiemap_extent
);
496 fie_buf_size
= sizeof(struct fiemap
) + ext_buf_size
;
498 err
= ext2fs_get_memzero(fie_buf_size
, &fiemap_buf
);
502 ext_buf
= fiemap_buf
->fm_extents
;
503 memset(fiemap_buf
, 0, fie_buf_size
);
504 fiemap_buf
->fm_length
= FIEMAP_MAX_OFFSET
;
505 fiemap_buf
->fm_flags
|= FIEMAP_FLAG_SYNC
;
506 fiemap_buf
->fm_extent_count
= EXTENT_MAX_COUNT
;
509 fiemap_buf
->fm_start
= pos
;
510 memset(ext_buf
, 0, ext_buf_size
);
511 err
= ioctl(fd
, FS_IOC_FIEMAP
, fiemap_buf
);
512 if (err
< 0 && (errno
== EOPNOTSUPP
|| errno
== ENOTTY
)) {
513 err
= EXT2_ET_UNIMPLEMENTED
;
515 } else if (err
< 0 || fiemap_buf
->fm_mapped_extents
== 0) {
519 for (i
= 0, ext
= ext_buf
; i
< fiemap_buf
->fm_mapped_extents
;
521 err
= copy_file_range(fs
, fd
, e2_file
, ext
->fe_logical
,
522 ext
->fe_logical
+ ext
->fe_length
,
529 /* Record file's logical offset this time */
530 pos
= ext
->fe_logical
+ ext
->fe_length
;
532 * If fm_extents array has been filled and
533 * there are extents left, continue to cycle.
535 } while (fiemap_buf
->fm_mapped_extents
== EXTENT_MAX_COUNT
&&
536 !(ext
->fe_flags
& FIEMAP_EXTENT_LAST
));
538 ext2fs_free_mem(&fiemap_buf
);
541 return EXT2_ET_UNIMPLEMENTED
;
542 #endif /* FS_IOC_FIEMAP */
545 static errcode_t
copy_file(ext2_filsys fs
, int fd
, struct stat
*statbuf
,
549 char *buf
= NULL
, *zerobuf
= NULL
;
550 errcode_t err
, close_err
;
552 err
= ext2fs_file_open(fs
, ino
, EXT2_FILE_WRITE
, &e2_file
);
556 err
= ext2fs_get_mem(COPY_FILE_BUFLEN
, &buf
);
560 err
= ext2fs_get_memzero(fs
->blocksize
, &zerobuf
);
564 err
= try_lseek_copy(fs
, fd
, statbuf
, e2_file
, buf
, zerobuf
);
565 if (err
!= EXT2_ET_UNIMPLEMENTED
)
568 err
= try_fiemap_copy(fs
, fd
, e2_file
, buf
, zerobuf
);
569 if (err
!= EXT2_ET_UNIMPLEMENTED
)
572 err
= copy_file_range(fs
, fd
, e2_file
, 0, statbuf
->st_size
, buf
,
575 ext2fs_free_mem(&zerobuf
);
576 ext2fs_free_mem(&buf
);
577 close_err
= ext2fs_file_close(e2_file
);
583 static int is_hardlink(struct hdlinks_s
*hdlinks
, dev_t dev
, ino_t ino
)
587 for (i
= 0; i
< hdlinks
->count
; i
++) {
588 if (hdlinks
->hdl
[i
].src_dev
== dev
&&
589 hdlinks
->hdl
[i
].src_ino
== ino
)
595 /* Copy the native file to the fs */
596 errcode_t
do_write_internal(ext2_filsys fs
, ext2_ino_t cwd
, const char *src
,
597 const char *dest
, ext2_ino_t root
)
603 struct ext2_inode inode
;
605 fd
= ext2fs_open_file(src
, O_RDONLY
, 0);
608 com_err(__func__
, retval
, _("while opening \"%s\" to copy"),
612 if (fstat(fd
, &statbuf
) < 0) {
617 retval
= ext2fs_namei(fs
, root
, cwd
, dest
, &newfile
);
619 retval
= EXT2_ET_FILE_EXISTS
;
623 retval
= ext2fs_new_inode(fs
, cwd
, 010755, 0, &newfile
);
627 printf("Allocated inode: %u\n", newfile
);
629 retval
= ext2fs_link(fs
, cwd
, dest
, newfile
,
631 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
632 retval
= ext2fs_expand_dir(fs
, cwd
);
635 retval
= ext2fs_link(fs
, cwd
, dest
, newfile
,
640 if (ext2fs_test_inode_bitmap2(fs
->inode_map
, newfile
))
641 com_err(__func__
, 0, "Warning: inode already set");
642 ext2fs_inode_alloc_stats2(fs
, newfile
, +1, 0);
643 memset(&inode
, 0, sizeof(inode
));
644 inode
.i_mode
= (statbuf
.st_mode
& ~LINUX_S_IFMT
) | LINUX_S_IFREG
;
645 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
646 fs
->now
? fs
->now
: time(0);
647 inode
.i_links_count
= 1;
648 retval
= ext2fs_inode_size_set(fs
, &inode
, statbuf
.st_size
);
651 if (ext2fs_has_feature_inline_data(fs
->super
)) {
652 inode
.i_flags
|= EXT4_INLINE_DATA_FL
;
653 } else if (ext2fs_has_feature_extents(fs
->super
)) {
654 ext2_extent_handle_t handle
;
656 inode
.i_flags
&= ~EXT4_EXTENTS_FL
;
657 retval
= ext2fs_extent_open2(fs
, newfile
, &inode
, &handle
);
660 ext2fs_extent_free(handle
);
663 retval
= ext2fs_write_new_inode(fs
, newfile
, &inode
);
666 if (inode
.i_flags
& EXT4_INLINE_DATA_FL
) {
667 retval
= ext2fs_inline_data_init(fs
, newfile
);
671 if (LINUX_S_ISREG(inode
.i_mode
)) {
672 retval
= copy_file(fs
, fd
, &statbuf
, newfile
);
681 /* Copy files from source_dir to fs */
682 static errcode_t
__populate_fs(ext2_filsys fs
, ext2_ino_t parent_ino
,
683 const char *source_dir
, ext2_ino_t root
,
684 struct hdlinks_s
*hdlinks
)
690 char *ln_target
= NULL
;
691 unsigned int save_inode
;
693 errcode_t retval
= 0;
697 if (chdir(source_dir
) < 0) {
699 com_err(__func__
, retval
,
700 _("while changing working directory to \"%s\""),
705 if (!(dh
= opendir("."))) {
707 com_err(__func__
, retval
,
708 _("while opening directory \"%s\""), source_dir
);
712 while ((dent
= readdir(dh
))) {
713 if ((!strcmp(dent
->d_name
, ".")) ||
714 (!strcmp(dent
->d_name
, "..")))
716 if (lstat(dent
->d_name
, &st
)) {
718 com_err(__func__
, retval
, _("while lstat \"%s\""),
724 /* Check for hardlinks */
726 if (!S_ISDIR(st
.st_mode
) && !S_ISLNK(st
.st_mode
) &&
728 hdlink
= is_hardlink(hdlinks
, st
.st_dev
, st
.st_ino
);
730 retval
= add_link(fs
, parent_ino
,
731 hdlinks
->hdl
[hdlink
].dst_ino
,
734 com_err(__func__
, retval
,
735 "while linking %s", name
);
743 switch(st
.st_mode
& S_IFMT
) {
748 retval
= do_mknod_internal(fs
, parent_ino
, name
, &st
);
750 com_err(__func__
, retval
,
751 _("while creating special file "
757 ln_target
= malloc(st
.st_size
+ 1);
758 if (ln_target
== NULL
) {
759 com_err(__func__
, retval
,
763 read_cnt
= readlink(name
, ln_target
,
765 if (read_cnt
== -1) {
767 com_err(__func__
, retval
,
768 _("while trying to read link \"%s\""),
772 if (read_cnt
> st
.st_size
) {
773 com_err(__func__
, retval
,
774 _("symlink increased in size "
775 "between lstat() and readlink()"));
779 ln_target
[read_cnt
] = '\0';
780 retval
= do_symlink_internal(fs
, parent_ino
, name
,
784 com_err(__func__
, retval
,
785 _("while writing symlink\"%s\""),
791 retval
= do_write_internal(fs
, parent_ino
, name
, name
,
794 com_err(__func__
, retval
,
795 _("while writing file \"%s\""), name
);
800 /* Don't choke on /lost+found */
801 if (parent_ino
== EXT2_ROOT_INO
&&
802 strcmp(name
, "lost+found") == 0)
804 retval
= do_mkdir_internal(fs
, parent_ino
, name
,
807 com_err(__func__
, retval
,
808 _("while making dir \"%s\""), name
);
812 retval
= ext2fs_namei(fs
, root
, parent_ino
,
815 com_err(name
, retval
, 0);
818 /* Populate the dir recursively*/
819 retval
= __populate_fs(fs
, ino
, name
, root
, hdlinks
);
824 com_err(__func__
, retval
,
825 _("while changing directory"));
831 _("ignoring entry \"%s\""), name
);
834 retval
= ext2fs_namei(fs
, root
, parent_ino
, name
, &ino
);
836 com_err(name
, retval
, _("while looking up \"%s\""),
841 retval
= set_inode_extra(fs
, ino
, &st
);
843 com_err(__func__
, retval
,
844 _("while setting inode for \"%s\""), name
);
848 retval
= set_inode_xattr(fs
, ino
, name
);
850 com_err(__func__
, retval
,
851 _("while setting xattrs for \"%s\""), name
);
855 /* Save the hardlink ino */
858 * Check whether need more memory, and we don't need
859 * free() since the lifespan will be over after the fs
862 if (hdlinks
->count
== hdlinks
->size
) {
863 void *p
= realloc(hdlinks
->hdl
,
864 (hdlinks
->size
+ HDLINK_CNT
) *
865 sizeof(struct hdlink_s
));
867 retval
= EXT2_ET_NO_MEMORY
;
868 com_err(name
, retval
,
869 _("while saving inode data"));
873 hdlinks
->size
+= HDLINK_CNT
;
875 hdlinks
->hdl
[hdlinks
->count
].src_dev
= st
.st_dev
;
876 hdlinks
->hdl
[hdlinks
->count
].src_ino
= st
.st_ino
;
877 hdlinks
->hdl
[hdlinks
->count
].dst_ino
= ino
;
887 errcode_t
populate_fs(ext2_filsys fs
, ext2_ino_t parent_ino
,
888 const char *source_dir
, ext2_ino_t root
)
890 struct hdlinks_s hdlinks
;
893 if (!(fs
->flags
& EXT2_FLAG_RW
)) {
894 com_err(__func__
, 0, "Filesystem opened readonly");
899 hdlinks
.size
= HDLINK_CNT
;
900 hdlinks
.hdl
= realloc(NULL
, hdlinks
.size
* sizeof(struct hdlink_s
));
901 if (hdlinks
.hdl
== NULL
) {
903 com_err(__func__
, retval
, _("while allocating memory"));
907 retval
= __populate_fs(fs
, parent_ino
, source_dir
, root
, &hdlinks
);