Whamcloud - gitweb
Merge branch 'ry/mke2fs-populate' into next
[tools/e2fsprogs.git] / misc / create_inode.c
1 #include "create_inode.h"
2
3 #if __STDC_VERSION__ < 199901L
4 # if __GNUC__ >= 2
5 #  define __func__ __FUNCTION__
6 # else
7 #  define __func__ "<unknown>"
8 # endif
9 #endif
10
11 /* 64KiB is the minimium blksize to best minimize system call overhead. */
12 #ifndef IO_BUFSIZE
13 #define IO_BUFSIZE 64*1024
14 #endif
15
16 /* Block size for `st_blocks' */
17 #ifndef S_BLKSIZE
18 #define S_BLKSIZE 512
19 #endif
20
21 /* For saving the hard links */
22 int hdlink_cnt = HDLINK_CNT;
23
24 /* Link an inode number to a directory */
25 static errcode_t add_link(ext2_ino_t parent_ino, ext2_ino_t ino, const char *name)
26 {
27         struct ext2_inode       inode;
28         errcode_t               retval;
29
30         retval = ext2fs_read_inode(current_fs, ino, &inode);
31         if (retval) {
32                 com_err(__func__, retval, "while reading inode %u", ino);
33                 return retval;
34         }
35
36         retval = ext2fs_link(current_fs, parent_ino, name, ino, inode.i_flags);
37         if (retval == EXT2_ET_DIR_NO_SPACE) {
38                 retval = ext2fs_expand_dir(current_fs, parent_ino);
39                 if (retval) {
40                         com_err(__func__, retval, "while expanding directory");
41                         return retval;
42                 }
43                 retval = ext2fs_link(current_fs, parent_ino, name, ino, inode.i_flags);
44         }
45         if (retval) {
46                 com_err(__func__, retval, "while linking %s", name);
47                 return retval;
48         }
49
50         inode.i_links_count++;
51
52         retval = ext2fs_write_inode(current_fs, ino, &inode);
53         if (retval)
54                 com_err(__func__, retval, "while writing inode %u", ino);
55
56         return retval;
57 }
58
59 /* Fill the uid, gid, mode and time for the inode */
60 static void fill_inode(struct ext2_inode *inode, struct stat *st)
61 {
62         if (st != NULL) {
63                 inode->i_uid = st->st_uid;
64                 inode->i_gid = st->st_gid;
65                 inode->i_mode |= st->st_mode;
66                 inode->i_atime = st->st_atime;
67                 inode->i_mtime = st->st_mtime;
68                 inode->i_ctime = st->st_ctime;
69         }
70 }
71
72 /* Set the uid, gid, mode and time for the inode */
73 errcode_t set_inode_extra(ext2_ino_t cwd, ext2_ino_t ino, struct stat *st)
74 {
75         errcode_t               retval;
76         struct ext2_inode       inode;
77
78         retval = ext2fs_read_inode(current_fs, ino, &inode);
79         if (retval) {
80                 com_err(__func__, retval, "while reading inode %u", ino);
81                 return retval;
82         }
83
84         fill_inode(&inode, st);
85
86         retval = ext2fs_write_inode(current_fs, ino, &inode);
87         if (retval) {
88                 com_err(__func__, retval, "while writing inode %u", ino);
89                 return retval;
90         }
91 }
92
93 /* Make a special file which is block, character and fifo */
94 errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st)
95 {
96         ext2_ino_t              ino;
97         errcode_t               retval;
98         struct ext2_inode       inode;
99         unsigned long           major, minor, mode;
100         int                     filetype;
101
102         switch(st->st_mode & S_IFMT) {
103                 case S_IFCHR:
104                         mode = LINUX_S_IFCHR;
105                         filetype = EXT2_FT_CHRDEV;
106                         break;
107                 case S_IFBLK:
108                         mode = LINUX_S_IFBLK;
109                         filetype =  EXT2_FT_BLKDEV;
110                         break;
111                 case S_IFIFO:
112                         mode = LINUX_S_IFIFO;
113                         filetype = EXT2_FT_FIFO;
114                         break;
115         }
116
117         if (!(current_fs->flags & EXT2_FLAG_RW)) {
118                 com_err(__func__, 0, "Filesystem opened read/only");
119                 return -1;
120         }
121         retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino);
122         if (retval) {
123                 com_err(__func__, retval, 0);
124                 return retval;
125         }
126
127 #ifdef DEBUGFS
128         printf("Allocated inode: %u\n", ino);
129 #endif
130         retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
131         if (retval == EXT2_ET_DIR_NO_SPACE) {
132                 retval = ext2fs_expand_dir(current_fs, cwd);
133                 if (retval) {
134                         com_err(__func__, retval, "while expanding directory");
135                         return retval;
136                 }
137                 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
138         }
139         if (retval) {
140                 com_err(name, retval, 0);
141                 return -1;
142         }
143         if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino))
144                 com_err(__func__, 0, "Warning: inode already set");
145         ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
146         memset(&inode, 0, sizeof(inode));
147         inode.i_mode = mode;
148         inode.i_atime = inode.i_ctime = inode.i_mtime =
149                 current_fs->now ? current_fs->now : time(0);
150
151         major = major(st->st_rdev);
152         minor = minor(st->st_rdev);
153
154         if ((major < 256) && (minor < 256)) {
155                 inode.i_block[0] = major * 256 + minor;
156                 inode.i_block[1] = 0;
157         } else {
158                 inode.i_block[0] = 0;
159                 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
160         }
161         inode.i_links_count = 1;
162
163         retval = ext2fs_write_new_inode(current_fs, ino, &inode);
164         if (retval)
165                 com_err(__func__, retval, "while creating inode %u", ino);
166
167         return retval;
168 }
169
170 /* Make a symlink name -> target */
171 errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
172 {
173         char                    *cp;
174         ext2_ino_t              parent_ino;
175         errcode_t               retval;
176         struct ext2_inode       inode;
177         struct stat             st;
178
179         cp = strrchr(name, '/');
180         if (cp) {
181                 *cp = 0;
182                 if ((retval =  ext2fs_namei(current_fs, root, cwd, name, &parent_ino))){
183                         com_err(name, retval, 0);
184                         return retval;
185                 }
186                 name = cp+1;
187         } else
188                 parent_ino = cwd;
189
190 try_again:
191         retval = ext2fs_symlink(current_fs, parent_ino, 0, name, target);
192         if (retval == EXT2_ET_DIR_NO_SPACE) {
193                 retval = ext2fs_expand_dir(current_fs, parent_ino);
194                 if (retval) {
195                         com_err("do_symlink_internal", retval, "while expanding directory");
196                         return retval;
197                 }
198                 goto try_again;
199         }
200         if (retval) {
201                 com_err("ext2fs_symlink", retval, 0);
202                 return retval;
203         }
204
205 }
206
207 /* Make a directory in the fs */
208 errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
209 {
210         char                    *cp;
211         ext2_ino_t              parent_ino, ino;
212         errcode_t               retval;
213         struct ext2_inode       inode;
214
215
216         cp = strrchr(name, '/');
217         if (cp) {
218                 *cp = 0;
219                 if ((retval =  ext2fs_namei(current_fs, root, cwd, name, &parent_ino))){
220                         com_err(name, retval, 0);
221                         return retval;
222                 }
223                 name = cp+1;
224         } else
225                 parent_ino = cwd;
226
227 try_again:
228         retval = ext2fs_mkdir(current_fs, parent_ino, 0, name);
229         if (retval == EXT2_ET_DIR_NO_SPACE) {
230                 retval = ext2fs_expand_dir(current_fs, parent_ino);
231                 if (retval) {
232                         com_err(__func__, retval, "while expanding directory");
233                         return retval;
234                 }
235                 goto try_again;
236         }
237         if (retval) {
238                 com_err("ext2fs_mkdir", retval, 0);
239                 return retval;
240         }
241 }
242
243 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
244 {
245         ext2_file_t     e2_file;
246         errcode_t       retval;
247         int             got;
248         unsigned int    written;
249         char            *buf;
250         char            *ptr;
251         char            *zero_buf;
252         int             cmp;
253
254         retval = ext2fs_file_open(current_fs, newfile,
255                                   EXT2_FILE_WRITE, &e2_file);
256         if (retval)
257                 return retval;
258
259         retval = ext2fs_get_mem(bufsize, &buf);
260         if (retval) {
261                 com_err("copy_file", retval, "can't allocate buffer\n");
262                 return retval;
263         }
264
265         /* This is used for checking whether the whole block is zero */
266         retval = ext2fs_get_memzero(bufsize, &zero_buf);
267         if (retval) {
268                 com_err("copy_file", retval, "can't allocate buffer\n");
269                 ext2fs_free_mem(&buf);
270                 return retval;
271         }
272
273         while (1) {
274                 got = read(fd, buf, bufsize);
275                 if (got == 0)
276                         break;
277                 if (got < 0) {
278                         retval = errno;
279                         goto fail;
280                 }
281                 ptr = buf;
282
283                 /* Sparse copy */
284                 if (make_holes) {
285                         /* Check whether all is zero */
286                         cmp = memcmp(ptr, zero_buf, got);
287                         if (cmp == 0) {
288                                  /* The whole block is zero, make a hole */
289                                 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
290                                 if (retval)
291                                         goto fail;
292                                 got = 0;
293                         }
294                 }
295
296                 /* Normal copy */
297                 while (got > 0) {
298                         retval = ext2fs_file_write(e2_file, ptr,
299                                                    got, &written);
300                         if (retval)
301                                 goto fail;
302
303                         got -= written;
304                         ptr += written;
305                 }
306         }
307         ext2fs_free_mem(&buf);
308         ext2fs_free_mem(&zero_buf);
309         retval = ext2fs_file_close(e2_file);
310         return retval;
311
312 fail:
313         ext2fs_free_mem(&buf);
314         ext2fs_free_mem(&zero_buf);
315         (void) ext2fs_file_close(e2_file);
316         return retval;
317 }
318
319 int is_hardlink(ext2_ino_t ino)
320 {
321         int i;
322
323         for(i = 0; i < hdlinks.count; i++) {
324                 if(hdlinks.hdl[i].src_ino == ino)
325                         return i;
326         }
327         return -1;
328 }
329
330 /* Copy the native file to the fs */
331 errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
332 {
333         int             fd;
334         struct stat     statbuf;
335         ext2_ino_t      newfile;
336         errcode_t       retval;
337         struct ext2_inode inode;
338         int             bufsize = IO_BUFSIZE;
339         int             make_holes = 0;
340
341         fd = open(src, O_RDONLY);
342         if (fd < 0) {
343                 com_err(src, errno, 0);
344                 return errno;
345         }
346         if (fstat(fd, &statbuf) < 0) {
347                 com_err(src, errno, 0);
348                 close(fd);
349                 return errno;
350         }
351
352         retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile);
353         if (retval == 0) {
354                 com_err(__func__, 0, "The file '%s' already exists\n", dest);
355                 close(fd);
356                 return errno;
357         }
358
359         retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
360         if (retval) {
361                 com_err(__func__, retval, 0);
362                 close(fd);
363                 return errno;
364         }
365 #ifdef DEBUGFS
366         printf("Allocated inode: %u\n", newfile);
367 #endif
368         retval = ext2fs_link(current_fs, cwd, dest, newfile,
369                                 EXT2_FT_REG_FILE);
370         if (retval == EXT2_ET_DIR_NO_SPACE) {
371                 retval = ext2fs_expand_dir(current_fs, cwd);
372                 if (retval) {
373                         com_err(__func__, retval, "while expanding directory");
374                         close(fd);
375                         return errno;
376                 }
377                 retval = ext2fs_link(current_fs, cwd, dest, newfile,
378                                         EXT2_FT_REG_FILE);
379         }
380         if (retval) {
381                 com_err(dest, retval, 0);
382                 close(fd);
383                 return errno;
384         }
385         if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile))
386                 com_err(__func__, 0, "Warning: inode already set");
387         ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
388         memset(&inode, 0, sizeof(inode));
389         inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
390         inode.i_atime = inode.i_ctime = inode.i_mtime =
391                 current_fs->now ? current_fs->now : time(0);
392         inode.i_links_count = 1;
393         inode.i_size = statbuf.st_size;
394         if (EXT2_HAS_INCOMPAT_FEATURE(current_fs->super,
395                                       EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
396                 inode.i_flags |= EXT4_INLINE_DATA_FL;
397         } else if (current_fs->super->s_feature_incompat &
398                    EXT3_FEATURE_INCOMPAT_EXTENTS) {
399                 int i;
400                 struct ext3_extent_header *eh;
401
402                 eh = (struct ext3_extent_header *) &inode.i_block[0];
403                 eh->eh_depth = 0;
404                 eh->eh_entries = 0;
405                 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
406                 i = (sizeof(inode.i_block) - sizeof(*eh)) /
407                         sizeof(struct ext3_extent);
408                 eh->eh_max = ext2fs_cpu_to_le16(i);
409                 inode.i_flags |= EXT4_EXTENTS_FL;
410         }
411
412         if ((retval = ext2fs_write_new_inode(current_fs, newfile, &inode))) {
413                 com_err(__func__, retval, "while creating inode %u", newfile);
414                 close(fd);
415                 return errno;
416         }
417         if (inode.i_flags & EXT4_INLINE_DATA_FL) {
418                 retval = ext2fs_inline_data_init(current_fs, newfile);
419                 if (retval)
420                         return;
421         }
422         if (LINUX_S_ISREG(inode.i_mode)) {
423                 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
424                         make_holes = 1;
425                         /*
426                          * Use I/O blocksize as buffer size when
427                          * copying sparse files.
428                          */
429                         bufsize = statbuf.st_blksize;
430                 }
431                 retval = copy_file(fd, newfile, bufsize, make_holes);
432                 if (retval)
433                         com_err("copy_file", retval, 0);
434         }
435         close(fd);
436
437         return 0;
438 }
439
440 /* Copy files from source_dir to fs */
441 errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
442 {
443         const char      *name;
444         DIR             *dh;
445         struct dirent   *dent;
446         struct stat     st;
447         char            ln_target[PATH_MAX];
448         unsigned int    save_inode;
449         ext2_ino_t      ino;
450         errcode_t       retval;
451         int             read_cnt;
452         int             hdlink;
453
454         root = EXT2_ROOT_INO;
455
456         if (chdir(source_dir) < 0) {
457                 com_err(__func__, errno,
458                         _("while changing working directory to \"%s\""), source_dir);
459                 return errno;
460         }
461
462         if (!(dh = opendir("."))) {
463                 com_err(__func__, errno,
464                         _("while openning directory \"%s\""), source_dir);
465                 return errno;
466         }
467
468         while((dent = readdir(dh))) {
469                 if((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
470                         continue;
471                 lstat(dent->d_name, &st);
472                 name = dent->d_name;
473
474                 /* Check for hardlinks */
475                 save_inode = 0;
476                 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) && st.st_nlink > 1) {
477                         hdlink = is_hardlink(st.st_ino);
478                         if (hdlink >= 0) {
479                                 retval = add_link(parent_ino,
480                                                 hdlinks.hdl[hdlink].dst_ino, name);
481                                 if (retval) {
482                                         com_err(__func__, retval, "while linking %s", name);
483                                         return retval;
484                                 }
485                                 continue;
486                         } else
487                                 save_inode = 1;
488                 }
489
490                 switch(st.st_mode & S_IFMT) {
491                         case S_IFCHR:
492                         case S_IFBLK:
493                         case S_IFIFO:
494                                 if ((retval = do_mknod_internal(parent_ino, name, &st))) {
495                                         com_err(__func__, retval,
496                                                 _("while creating special file \"%s\""), name);
497                                         return retval;
498                                 }
499                                 break;
500                         case S_IFSOCK:
501                                 /* FIXME: there is no make socket function atm. */
502                                 com_err(__func__, 0,
503                                         _("ignoring socket file \"%s\""), name);
504                                 continue;
505                         case S_IFLNK:
506                                 if((read_cnt = readlink(name, ln_target, sizeof(ln_target))) == -1) {
507                                         com_err(__func__, errno,
508                                                 _("while trying to readlink \"%s\""), name);
509                                         return errno;
510                                 }
511                                 ln_target[read_cnt] = '\0';
512                                 if ((retval = do_symlink_internal(parent_ino, name, ln_target))) {
513                                         com_err(__func__, retval,
514                                                 _("while writing symlink\"%s\""), name);
515                                         return retval;
516                                 }
517                                 break;
518                         case S_IFREG:
519                                 if ((retval = do_write_internal(parent_ino, name, name))) {
520                                         com_err(__func__, retval,
521                                                 _("while writing file \"%s\""), name);
522                                         return retval;
523                                 }
524                                 break;
525                         case S_IFDIR:
526                                 if ((retval = do_mkdir_internal(parent_ino, name, &st))) {
527                                         com_err(__func__, retval,
528                                                 _("while making dir \"%s\""), name);
529                                         return retval;
530                                 }
531                                 if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))) {
532                                         com_err(name, retval, 0);
533                                                 return retval;
534                                 }
535                                 /* Populate the dir recursively*/
536                                 retval = populate_fs(ino, name);
537                                 if (retval) {
538                                         com_err(__func__, retval, _("while adding dir \"%s\""), name);
539                                         return retval;
540                                 }
541                                 chdir("..");
542                                 break;
543                         default:
544                                 com_err(__func__, 0,
545                                         _("ignoring entry \"%s\""), name);
546                 }
547
548                 if ((retval =  ext2fs_namei(current_fs, root, parent_ino, name, &ino))){
549                         com_err(name, retval, 0);
550                         return retval;
551                 }
552
553                 if ((retval = set_inode_extra(parent_ino, ino, &st))) {
554                         com_err(__func__, retval,
555                                 _("while setting inode for \"%s\""), name);
556                         return retval;
557                 }
558
559                 /* Save the hardlink ino */
560                 if (save_inode) {
561                         /*
562                          * Check whether need more memory, and we don't need
563                          * free() since the lifespan will be over after the fs
564                          * populated.
565                          */
566                         if (hdlinks.count == hdlink_cnt) {
567                                 if ((hdlinks.hdl = realloc (hdlinks.hdl,
568                                                 (hdlink_cnt + HDLINK_CNT) *
569                                                 sizeof (struct hdlink_s))) == NULL) {
570                                         com_err(name, errno, "Not enough memory");
571                                         return errno;
572                                 }
573                                 hdlink_cnt += HDLINK_CNT;
574                         }
575                         hdlinks.hdl[hdlinks.count].src_ino = st.st_ino;
576                         hdlinks.hdl[hdlinks.count].dst_ino = ino;
577                         hdlinks.count++;
578                 }
579         }
580         closedir(dh);
581         return retval;
582 }