]>
Commit | Line | Data |
---|---|---|
bbccc6f3 AD |
1 | /* |
2 | * create_inode.c --- create an inode | |
3 | * | |
4 | * Copyright (C) 2014 Robert Yang <liezhi.yang@windriver.com> | |
5 | * | |
6 | * %Begin-Header% | |
7 | * This file may be redistributed under the terms of the GNU library | |
8 | * General Public License, version 2. | |
9 | * %End-Header% | |
10 | */ | |
11 | ||
76f13234 DW |
12 | #define _FILE_OFFSET_BITS 64 |
13 | #define _LARGEFILE64_SOURCE 1 | |
14 | #define _GNU_SOURCE 1 | |
15 | ||
16 | #include "config.h" | |
8f8d8a57 | 17 | #include <time.h> |
76f13234 | 18 | #include <sys/types.h> |
8f8d8a57 | 19 | #include <unistd.h> |
bbccc6f3 | 20 | #include <limits.h> /* for PATH_MAX */ |
c84da2ee RB |
21 | #ifdef HAVE_ATTR_XATTR_H |
22 | #include <attr/xattr.h> | |
23 | #endif | |
76f13234 DW |
24 | #include <sys/ioctl.h> |
25 | #include <ext2fs/ext2fs.h> | |
26 | #include <ext2fs/ext2_types.h> | |
27 | #include <ext2fs/fiemap.h> | |
8f8d8a57 | 28 | |
0d4deba2 | 29 | #include "create_inode.h" |
0c49965f | 30 | #include "nls-enable.h" |
0d4deba2 | 31 | |
052064b9 RY |
32 | #if __STDC_VERSION__ < 199901L |
33 | # if __GNUC__ >= 2 | |
34 | # define __func__ __FUNCTION__ | |
35 | # else | |
36 | # define __func__ "<unknown>" | |
37 | # endif | |
38 | #endif | |
39 | ||
ce600dc4 | 40 | /* 64KiB is the minimium blksize to best minimize system call overhead. */ |
76f13234 | 41 | #define COPY_FILE_BUFLEN 65536 |
ce600dc4 | 42 | |
a433db04 DW |
43 | static int ext2_file_type(unsigned int mode) |
44 | { | |
45 | if (LINUX_S_ISREG(mode)) | |
46 | return EXT2_FT_REG_FILE; | |
47 | ||
48 | if (LINUX_S_ISDIR(mode)) | |
49 | return EXT2_FT_DIR; | |
50 | ||
51 | if (LINUX_S_ISCHR(mode)) | |
52 | return EXT2_FT_CHRDEV; | |
53 | ||
54 | if (LINUX_S_ISBLK(mode)) | |
55 | return EXT2_FT_BLKDEV; | |
56 | ||
57 | if (LINUX_S_ISLNK(mode)) | |
58 | return EXT2_FT_SYMLINK; | |
59 | ||
60 | if (LINUX_S_ISFIFO(mode)) | |
61 | return EXT2_FT_FIFO; | |
62 | ||
63 | if (LINUX_S_ISSOCK(mode)) | |
64 | return EXT2_FT_SOCK; | |
65 | ||
66 | return 0; | |
67 | } | |
68 | ||
f84894bc | 69 | /* Link an inode number to a directory */ |
a3111e80 DW |
70 | static errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino, |
71 | ext2_ino_t ino, const char *name) | |
f84894bc RY |
72 | { |
73 | struct ext2_inode inode; | |
74 | errcode_t retval; | |
75 | ||
a3111e80 | 76 | retval = ext2fs_read_inode(fs, ino, &inode); |
f84894bc RY |
77 | if (retval) { |
78 | com_err(__func__, retval, "while reading inode %u", ino); | |
79 | return retval; | |
80 | } | |
81 | ||
a433db04 DW |
82 | retval = ext2fs_link(fs, parent_ino, name, ino, |
83 | ext2_file_type(inode.i_mode)); | |
f84894bc | 84 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
a3111e80 | 85 | retval = ext2fs_expand_dir(fs, parent_ino); |
f84894bc RY |
86 | if (retval) { |
87 | com_err(__func__, retval, "while expanding directory"); | |
88 | return retval; | |
89 | } | |
a433db04 DW |
90 | retval = ext2fs_link(fs, parent_ino, name, ino, |
91 | ext2_file_type(inode.i_mode)); | |
f84894bc RY |
92 | } |
93 | if (retval) { | |
94 | com_err(__func__, retval, "while linking %s", name); | |
95 | return retval; | |
96 | } | |
97 | ||
98 | inode.i_links_count++; | |
99 | ||
a3111e80 | 100 | retval = ext2fs_write_inode(fs, ino, &inode); |
f84894bc RY |
101 | if (retval) |
102 | com_err(__func__, retval, "while writing inode %u", ino); | |
103 | ||
104 | return retval; | |
105 | } | |
106 | ||
b6960654 RY |
107 | /* Fill the uid, gid, mode and time for the inode */ |
108 | static void fill_inode(struct ext2_inode *inode, struct stat *st) | |
109 | { | |
110 | if (st != NULL) { | |
111 | inode->i_uid = st->st_uid; | |
112 | inode->i_gid = st->st_gid; | |
113 | inode->i_mode |= st->st_mode; | |
114 | inode->i_atime = st->st_atime; | |
115 | inode->i_mtime = st->st_mtime; | |
116 | inode->i_ctime = st->st_ctime; | |
117 | } | |
118 | } | |
119 | ||
120 | /* Set the uid, gid, mode and time for the inode */ | |
a3111e80 DW |
121 | static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd, |
122 | ext2_ino_t ino, struct stat *st) | |
b6960654 RY |
123 | { |
124 | errcode_t retval; | |
125 | struct ext2_inode inode; | |
126 | ||
a3111e80 | 127 | retval = ext2fs_read_inode(fs, ino, &inode); |
b6960654 RY |
128 | if (retval) { |
129 | com_err(__func__, retval, "while reading inode %u", ino); | |
130 | return retval; | |
131 | } | |
132 | ||
133 | fill_inode(&inode, st); | |
134 | ||
a3111e80 | 135 | retval = ext2fs_write_inode(fs, ino, &inode); |
3aec816f | 136 | if (retval) |
b6960654 | 137 | com_err(__func__, retval, "while writing inode %u", ino); |
3aec816f | 138 | return retval; |
b6960654 RY |
139 | } |
140 | ||
c84da2ee RB |
141 | static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino, const char *filename) |
142 | { | |
143 | #ifdef HAVE_LLISTXATTR | |
144 | errcode_t retval, close_retval; | |
c84da2ee RB |
145 | struct ext2_xattr_handle *handle; |
146 | ssize_t size, value_size; | |
76f13234 | 147 | char *list = NULL; |
c84da2ee RB |
148 | int i; |
149 | ||
150 | size = llistxattr(filename, NULL, 0); | |
151 | if (size == -1) { | |
152 | com_err(__func__, errno, "llistxattr failed on %s", filename); | |
153 | return errno; | |
154 | } else if (size == 0) { | |
155 | return 0; | |
156 | } | |
157 | ||
158 | retval = ext2fs_xattrs_open(fs, ino, &handle); | |
159 | if (retval) { | |
160 | if (retval == EXT2_ET_MISSING_EA_FEATURE) | |
161 | return 0; | |
162 | com_err(__func__, retval, "while opening inode %u", ino); | |
163 | return retval; | |
164 | } | |
165 | ||
166 | retval = ext2fs_get_mem(size, &list); | |
167 | if (retval) { | |
168 | com_err(__func__, retval, "whilst allocating memory"); | |
169 | goto out; | |
170 | } | |
171 | ||
172 | size = llistxattr(filename, list, size); | |
173 | if (size == -1) { | |
174 | com_err(__func__, errno, "llistxattr failed on %s", filename); | |
175 | retval = errno; | |
176 | goto out; | |
177 | } | |
178 | ||
179 | for (i = 0; i < size; i += strlen(&list[i]) + 1) { | |
180 | const char *name = &list[i]; | |
181 | char *value; | |
182 | ||
183 | value_size = getxattr(filename, name, NULL, 0); | |
184 | if (value_size == -1) { | |
185 | com_err(__func__, errno, "getxattr failed on %s", | |
186 | filename); | |
187 | retval = errno; | |
188 | break; | |
189 | } | |
190 | ||
191 | retval = ext2fs_get_mem(value_size, &value); | |
192 | if (retval) { | |
193 | com_err(__func__, retval, "whilst allocating memory"); | |
194 | break; | |
195 | } | |
196 | ||
197 | value_size = getxattr(filename, name, value, value_size); | |
198 | if (value_size == -1) { | |
199 | ext2fs_free_mem(&value); | |
200 | com_err(__func__, errno, "getxattr failed on %s", | |
201 | filename); | |
202 | retval = errno; | |
203 | break; | |
204 | } | |
205 | ||
206 | retval = ext2fs_xattr_set(handle, name, value, value_size); | |
207 | ext2fs_free_mem(&value); | |
208 | if (retval) { | |
209 | com_err(__func__, retval, | |
210 | "while writing xattr %u", ino); | |
211 | break; | |
212 | } | |
213 | ||
214 | } | |
215 | out: | |
216 | ext2fs_free_mem(&list); | |
217 | close_retval = ext2fs_xattrs_close(&handle); | |
218 | if (close_retval) { | |
219 | com_err(__func__, retval, "while closing inode %u", ino); | |
220 | retval = retval ? retval : close_retval; | |
221 | } | |
222 | return retval; | |
223 | #else /* HAVE_LLISTXATTR */ | |
224 | return 0; | |
225 | #endif /* HAVE_LLISTXATTR */ | |
226 | } | |
227 | ||
c61e0068 | 228 | /* Make a special files (block and character devices), fifo's, and sockets */ |
a3111e80 DW |
229 | errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name, |
230 | struct stat *st) | |
0d4deba2 | 231 | { |
b2346118 | 232 | ext2_ino_t ino; |
3cbb51f9 | 233 | errcode_t retval; |
b2346118 | 234 | struct ext2_inode inode; |
3cbb51f9 | 235 | unsigned long devmajor, devminor, mode; |
b2346118 RY |
236 | int filetype; |
237 | ||
238 | switch(st->st_mode & S_IFMT) { | |
9c891f7e DW |
239 | case S_IFCHR: |
240 | mode = LINUX_S_IFCHR; | |
241 | filetype = EXT2_FT_CHRDEV; | |
242 | break; | |
243 | case S_IFBLK: | |
244 | mode = LINUX_S_IFBLK; | |
245 | filetype = EXT2_FT_BLKDEV; | |
246 | break; | |
247 | case S_IFIFO: | |
248 | mode = LINUX_S_IFIFO; | |
249 | filetype = EXT2_FT_FIFO; | |
250 | break; | |
c61e0068 DW |
251 | case S_IFSOCK: |
252 | mode = LINUX_S_IFSOCK; | |
253 | filetype = EXT2_FT_SOCK; | |
ec3a42b1 | 254 | break; |
3aec816f | 255 | default: |
ec3a42b1 | 256 | return EXT2_ET_INVALID_ARGUMENT; |
b2346118 RY |
257 | } |
258 | ||
a3111e80 | 259 | if (!(fs->flags & EXT2_FLAG_RW)) { |
b2346118 | 260 | com_err(__func__, 0, "Filesystem opened read/only"); |
c61e0068 | 261 | return EROFS; |
b2346118 | 262 | } |
a3111e80 | 263 | retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino); |
b2346118 RY |
264 | if (retval) { |
265 | com_err(__func__, retval, 0); | |
266 | return retval; | |
267 | } | |
268 | ||
269 | #ifdef DEBUGFS | |
270 | printf("Allocated inode: %u\n", ino); | |
271 | #endif | |
a3111e80 | 272 | retval = ext2fs_link(fs, cwd, name, ino, filetype); |
b2346118 | 273 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
a3111e80 | 274 | retval = ext2fs_expand_dir(fs, cwd); |
b2346118 RY |
275 | if (retval) { |
276 | com_err(__func__, retval, "while expanding directory"); | |
277 | return retval; | |
278 | } | |
a3111e80 | 279 | retval = ext2fs_link(fs, cwd, name, ino, filetype); |
b2346118 RY |
280 | } |
281 | if (retval) { | |
282 | com_err(name, retval, 0); | |
ec3a42b1 | 283 | return retval; |
b2346118 | 284 | } |
a3111e80 | 285 | if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) |
b2346118 | 286 | com_err(__func__, 0, "Warning: inode already set"); |
a3111e80 | 287 | ext2fs_inode_alloc_stats2(fs, ino, +1, 0); |
b2346118 RY |
288 | memset(&inode, 0, sizeof(inode)); |
289 | inode.i_mode = mode; | |
290 | inode.i_atime = inode.i_ctime = inode.i_mtime = | |
a3111e80 | 291 | fs->now ? fs->now : time(0); |
b2346118 | 292 | |
3cbb51f9 AD |
293 | if (filetype != S_IFIFO) { |
294 | devmajor = major(st->st_rdev); | |
295 | devminor = minor(st->st_rdev); | |
296 | ||
297 | if ((devmajor < 256) && (devminor < 256)) { | |
298 | inode.i_block[0] = devmajor * 256 + devminor; | |
299 | inode.i_block[1] = 0; | |
300 | } else { | |
301 | inode.i_block[0] = 0; | |
302 | inode.i_block[1] = (devminor & 0xff) | (devmajor << 8) | | |
303 | ((devminor & ~0xff) << 12); | |
304 | } | |
b2346118 RY |
305 | } |
306 | inode.i_links_count = 1; | |
307 | ||
a3111e80 | 308 | retval = ext2fs_write_new_inode(fs, ino, &inode); |
b2346118 RY |
309 | if (retval) |
310 | com_err(__func__, retval, "while creating inode %u", ino); | |
311 | ||
312 | return retval; | |
0d4deba2 RY |
313 | } |
314 | ||
315 | /* Make a symlink name -> target */ | |
a3111e80 DW |
316 | errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name, |
317 | char *target, ext2_ino_t root) | |
0d4deba2 | 318 | { |
b25ddc5e RY |
319 | char *cp; |
320 | ext2_ino_t parent_ino; | |
321 | errcode_t retval; | |
b25ddc5e RY |
322 | |
323 | cp = strrchr(name, '/'); | |
324 | if (cp) { | |
325 | *cp = 0; | |
a3111e80 | 326 | retval = ext2fs_namei(fs, root, cwd, name, &parent_ino); |
8f8d8a57 | 327 | if (retval) { |
b25ddc5e RY |
328 | com_err(name, retval, 0); |
329 | return retval; | |
330 | } | |
331 | name = cp+1; | |
332 | } else | |
333 | parent_ino = cwd; | |
334 | ||
335 | try_again: | |
a3111e80 | 336 | retval = ext2fs_symlink(fs, parent_ino, 0, name, target); |
b25ddc5e | 337 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
a3111e80 | 338 | retval = ext2fs_expand_dir(fs, parent_ino); |
b25ddc5e | 339 | if (retval) { |
9c891f7e DW |
340 | com_err("do_symlink_internal", retval, |
341 | "while expanding directory"); | |
b25ddc5e RY |
342 | return retval; |
343 | } | |
344 | goto try_again; | |
345 | } | |
3aec816f | 346 | if (retval) |
b25ddc5e | 347 | com_err("ext2fs_symlink", retval, 0); |
3aec816f | 348 | return retval; |
0d4deba2 RY |
349 | } |
350 | ||
351 | /* Make a directory in the fs */ | |
a3111e80 DW |
352 | errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name, |
353 | struct stat *st, ext2_ino_t root) | |
0d4deba2 | 354 | { |
3068f03f | 355 | char *cp; |
c4c9bc59 | 356 | ext2_ino_t parent_ino; |
3068f03f | 357 | errcode_t retval; |
3068f03f RY |
358 | |
359 | ||
360 | cp = strrchr(name, '/'); | |
361 | if (cp) { | |
362 | *cp = 0; | |
a3111e80 | 363 | retval = ext2fs_namei(fs, root, cwd, name, &parent_ino); |
8f8d8a57 | 364 | if (retval) { |
3068f03f RY |
365 | com_err(name, retval, 0); |
366 | return retval; | |
367 | } | |
368 | name = cp+1; | |
369 | } else | |
370 | parent_ino = cwd; | |
371 | ||
372 | try_again: | |
a3111e80 | 373 | retval = ext2fs_mkdir(fs, parent_ino, 0, name); |
3068f03f | 374 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
a3111e80 | 375 | retval = ext2fs_expand_dir(fs, parent_ino); |
3068f03f RY |
376 | if (retval) { |
377 | com_err(__func__, retval, "while expanding directory"); | |
378 | return retval; | |
379 | } | |
380 | goto try_again; | |
381 | } | |
3aec816f | 382 | if (retval) |
3068f03f | 383 | com_err("ext2fs_mkdir", retval, 0); |
3aec816f | 384 | return retval; |
0d4deba2 RY |
385 | } |
386 | ||
76f13234 DW |
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) | |
ce600dc4 | 389 | { |
76f13234 DW |
390 | if (lseek(fd, offset, SEEK_SET) < 0) |
391 | return 0; | |
ce600dc4 | 392 | |
76f13234 DW |
393 | return read(fd, buf, count); |
394 | } | |
395 | #endif /* !defined HAVE_PREAD64 && !defined HAVE_PREAD */ | |
ce600dc4 | 396 | |
76f13234 DW |
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, | |
399 | char *zerobuf) | |
400 | { | |
401 | off_t off, bpos; | |
402 | ssize_t got, blen; | |
403 | unsigned int written; | |
404 | char *ptr; | |
405 | errcode_t err = 0; | |
406 | ||
407 | for (off = start; off < end; off += COPY_FILE_BUFLEN) { | |
408 | #ifdef HAVE_PREAD64 | |
409 | got = pread64(fd, buf, COPY_FILE_BUFLEN, off); | |
410 | #elif HAVE_PREAD | |
411 | got = pread(fd, buf, COPY_FILE_BUFLEN, off); | |
412 | #else | |
413 | got = my_pread(fd, buf, COPY_FILE_BUFLEN, off); | |
414 | #endif | |
ce600dc4 | 415 | if (got < 0) { |
76f13234 | 416 | err = errno; |
ce600dc4 RY |
417 | goto fail; |
418 | } | |
76f13234 DW |
419 | for (bpos = 0, ptr = buf; bpos < got; bpos += fs->blocksize) { |
420 | blen = fs->blocksize; | |
421 | if (blen > got - bpos) | |
422 | blen = got - bpos; | |
423 | if (memcmp(ptr, zerobuf, blen) == 0) { | |
424 | ptr += blen; | |
425 | continue; | |
426 | } | |
427 | err = ext2fs_file_lseek(e2_file, off + bpos, | |
428 | EXT2_SEEK_SET, NULL); | |
429 | if (err) | |
430 | goto fail; | |
431 | while (blen > 0) { | |
432 | err = ext2fs_file_write(e2_file, ptr, blen, | |
433 | &written); | |
434 | if (err) | |
435 | goto fail; | |
436 | if (written == 0) { | |
437 | err = EIO; | |
ce600dc4 | 438 | goto fail; |
76f13234 DW |
439 | } |
440 | blen -= written; | |
441 | ptr += written; | |
ce600dc4 RY |
442 | } |
443 | } | |
76f13234 DW |
444 | } |
445 | fail: | |
446 | return err; | |
447 | } | |
ce600dc4 | 448 | |
76f13234 DW |
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) | |
451 | { | |
452 | #if defined(SEEK_DATA) && defined(SEEK_HOLE) | |
453 | off_t data = 0, hole; | |
454 | off_t data_blk, hole_blk; | |
455 | errcode_t err; | |
456 | ||
457 | /* Try to use SEEK_DATA and SEEK_HOLE */ | |
458 | while (data < statbuf->st_size) { | |
459 | data = lseek(fd, data, SEEK_DATA); | |
460 | if (data < 0) { | |
461 | if (errno == ENXIO) | |
462 | break; | |
463 | return EXT2_ET_UNIMPLEMENTED; | |
ce600dc4 | 464 | } |
76f13234 DW |
465 | hole = lseek(fd, data, SEEK_HOLE); |
466 | if (hole < 0) | |
467 | return EXT2_ET_UNIMPLEMENTED; | |
468 | ||
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, | |
472 | zerobuf); | |
473 | if (err) | |
474 | return err; | |
475 | ||
476 | data = hole; | |
ce600dc4 | 477 | } |
ce600dc4 | 478 | |
76f13234 DW |
479 | return err; |
480 | #else | |
481 | return EXT2_ET_UNIMPLEMENTED; | |
482 | #endif /* SEEK_DATA and SEEK_HOLE */ | |
483 | } | |
484 | ||
485 | static errcode_t try_fiemap_copy(ext2_filsys fs, int fd, ext2_file_t e2_file, | |
486 | char *buf, char *zerobuf) | |
487 | { | |
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; | |
493 | off_t pos = 0; | |
494 | unsigned int i; | |
495 | errcode_t err; | |
496 | ||
497 | ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent); | |
498 | fie_buf_size = sizeof(struct fiemap) + ext_buf_size; | |
499 | ||
500 | err = ext2fs_get_memzero(fie_buf_size, &fiemap_buf); | |
501 | if (err) | |
502 | return err; | |
503 | ||
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; | |
509 | ||
510 | do { | |
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; | |
516 | goto out; | |
517 | } else if (err < 0 || fiemap_buf->fm_mapped_extents == 0) { | |
518 | err = errno; | |
519 | goto out; | |
520 | } | |
521 | for (i = 0, ext = ext_buf; i < fiemap_buf->fm_mapped_extents; | |
522 | i++, ext++) { | |
523 | err = copy_file_range(fs, fd, e2_file, ext->fe_logical, | |
524 | ext->fe_logical + ext->fe_length, | |
525 | buf, zerobuf); | |
526 | if (err) | |
527 | goto out; | |
528 | } | |
529 | ||
530 | ext--; | |
531 | /* Record file's logical offset this time */ | |
532 | pos = ext->fe_logical + ext->fe_length; | |
533 | /* | |
534 | * If fm_extents array has been filled and | |
535 | * there are extents left, continue to cycle. | |
536 | */ | |
537 | } while (fiemap_buf->fm_mapped_extents == EXTENT_MAX_COUNT && | |
538 | !(ext->fe_flags & FIEMAP_EXTENT_LAST)); | |
539 | out: | |
540 | ext2fs_free_mem(&fiemap_buf); | |
541 | return err; | |
542 | #else | |
543 | return EXT2_ET_UNIMPLEMENTED; | |
544 | #endif /* FS_IOC_FIEMAP */ | |
545 | } | |
546 | ||
547 | static errcode_t copy_file(ext2_filsys fs, int fd, struct stat *statbuf, | |
548 | ext2_ino_t ino) | |
549 | { | |
550 | ext2_file_t e2_file; | |
551 | char *buf = NULL, *zerobuf = NULL; | |
552 | errcode_t err, close_err; | |
553 | ||
554 | err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &e2_file); | |
555 | if (err) | |
556 | return err; | |
557 | ||
558 | err = ext2fs_get_mem(COPY_FILE_BUFLEN, &buf); | |
559 | if (err) | |
560 | goto out; | |
561 | ||
562 | err = ext2fs_get_memzero(fs->blocksize, &zerobuf); | |
563 | if (err) | |
564 | goto out; | |
565 | ||
566 | err = try_lseek_copy(fs, fd, statbuf, e2_file, buf, zerobuf); | |
567 | if (err != EXT2_ET_UNIMPLEMENTED) | |
568 | goto out; | |
569 | ||
570 | err = try_fiemap_copy(fs, fd, e2_file, buf, zerobuf); | |
571 | if (err != EXT2_ET_UNIMPLEMENTED) | |
572 | goto out; | |
573 | ||
574 | err = copy_file_range(fs, fd, e2_file, 0, statbuf->st_size, buf, | |
575 | zerobuf); | |
576 | out: | |
577 | ext2fs_free_mem(&zerobuf); | |
6bb88459 | 578 | ext2fs_free_mem(&buf); |
76f13234 DW |
579 | close_err = ext2fs_file_close(e2_file); |
580 | if (err == 0) | |
581 | err = close_err; | |
582 | return err; | |
ce600dc4 RY |
583 | } |
584 | ||
b99888a0 | 585 | static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino) |
f84894bc RY |
586 | { |
587 | int i; | |
588 | ||
b99888a0 DW |
589 | for (i = 0; i < hdlinks->count; i++) { |
590 | if (hdlinks->hdl[i].src_dev == dev && | |
591 | hdlinks->hdl[i].src_ino == ino) | |
f84894bc RY |
592 | return i; |
593 | } | |
594 | return -1; | |
595 | } | |
596 | ||
0d4deba2 | 597 | /* Copy the native file to the fs */ |
a3111e80 DW |
598 | errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src, |
599 | const char *dest, ext2_ino_t root) | |
0d4deba2 | 600 | { |
ce600dc4 RY |
601 | int fd; |
602 | struct stat statbuf; | |
603 | ext2_ino_t newfile; | |
604 | errcode_t retval; | |
605 | struct ext2_inode inode; | |
ce600dc4 | 606 | |
ec3a42b1 | 607 | fd = ext2fs_open_file(src, O_RDONLY, 0); |
ce600dc4 RY |
608 | if (fd < 0) { |
609 | com_err(src, errno, 0); | |
610 | return errno; | |
611 | } | |
612 | if (fstat(fd, &statbuf) < 0) { | |
613 | com_err(src, errno, 0); | |
614 | close(fd); | |
615 | return errno; | |
616 | } | |
617 | ||
a3111e80 | 618 | retval = ext2fs_namei(fs, root, cwd, dest, &newfile); |
ce600dc4 | 619 | if (retval == 0) { |
ce600dc4 | 620 | close(fd); |
117b54c3 | 621 | return EXT2_ET_FILE_EXISTS; |
ce600dc4 RY |
622 | } |
623 | ||
a3111e80 | 624 | retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile); |
ce600dc4 RY |
625 | if (retval) { |
626 | com_err(__func__, retval, 0); | |
627 | close(fd); | |
117b54c3 | 628 | return retval; |
ce600dc4 RY |
629 | } |
630 | #ifdef DEBUGFS | |
631 | printf("Allocated inode: %u\n", newfile); | |
632 | #endif | |
a3111e80 | 633 | retval = ext2fs_link(fs, cwd, dest, newfile, |
ce600dc4 RY |
634 | EXT2_FT_REG_FILE); |
635 | if (retval == EXT2_ET_DIR_NO_SPACE) { | |
a3111e80 | 636 | retval = ext2fs_expand_dir(fs, cwd); |
ce600dc4 RY |
637 | if (retval) { |
638 | com_err(__func__, retval, "while expanding directory"); | |
639 | close(fd); | |
117b54c3 | 640 | return retval; |
ce600dc4 | 641 | } |
a3111e80 | 642 | retval = ext2fs_link(fs, cwd, dest, newfile, |
ce600dc4 RY |
643 | EXT2_FT_REG_FILE); |
644 | } | |
645 | if (retval) { | |
646 | com_err(dest, retval, 0); | |
647 | close(fd); | |
648 | return errno; | |
649 | } | |
a3111e80 | 650 | if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile)) |
ce600dc4 | 651 | com_err(__func__, 0, "Warning: inode already set"); |
a3111e80 | 652 | ext2fs_inode_alloc_stats2(fs, newfile, +1, 0); |
ce600dc4 RY |
653 | memset(&inode, 0, sizeof(inode)); |
654 | inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG; | |
655 | inode.i_atime = inode.i_ctime = inode.i_mtime = | |
a3111e80 | 656 | fs->now ? fs->now : time(0); |
ce600dc4 | 657 | inode.i_links_count = 1; |
22302aa3 TT |
658 | retval = ext2fs_inode_size_set(fs, &inode, statbuf.st_size); |
659 | if (retval) { | |
660 | com_err(dest, retval, 0); | |
661 | close(fd); | |
662 | return retval; | |
663 | } | |
a3111e80 | 664 | if (EXT2_HAS_INCOMPAT_FEATURE(fs->super, |
d23b1965 TT |
665 | EXT4_FEATURE_INCOMPAT_INLINE_DATA)) { |
666 | inode.i_flags |= EXT4_INLINE_DATA_FL; | |
a3111e80 | 667 | } else if (fs->super->s_feature_incompat & |
d23b1965 | 668 | EXT3_FEATURE_INCOMPAT_EXTENTS) { |
3548bb64 DW |
669 | ext2_extent_handle_t handle; |
670 | ||
671 | inode.i_flags &= ~EXT4_EXTENTS_FL; | |
672 | retval = ext2fs_extent_open2(fs, newfile, &inode, &handle); | |
673 | if (retval) | |
674 | return retval; | |
675 | ext2fs_extent_free(handle); | |
ce600dc4 RY |
676 | } |
677 | ||
a3111e80 | 678 | retval = ext2fs_write_new_inode(fs, newfile, &inode); |
8f8d8a57 | 679 | if (retval) { |
ce600dc4 RY |
680 | com_err(__func__, retval, "while creating inode %u", newfile); |
681 | close(fd); | |
117b54c3 | 682 | return retval; |
ce600dc4 | 683 | } |
d23b1965 | 684 | if (inode.i_flags & EXT4_INLINE_DATA_FL) { |
a3111e80 | 685 | retval = ext2fs_inline_data_init(fs, newfile); |
117b54c3 DW |
686 | if (retval) { |
687 | com_err("copy_file", retval, 0); | |
688 | close(fd); | |
689 | return retval; | |
690 | } | |
d23b1965 | 691 | } |
ce600dc4 | 692 | if (LINUX_S_ISREG(inode.i_mode)) { |
76f13234 | 693 | retval = copy_file(fs, fd, &statbuf, newfile); |
ce600dc4 | 694 | if (retval) |
76f13234 | 695 | com_err("copy_file", retval, _("while copying %s"), src); |
ce600dc4 RY |
696 | } |
697 | close(fd); | |
698 | ||
117b54c3 | 699 | return retval; |
0d4deba2 RY |
700 | } |
701 | ||
702 | /* Copy files from source_dir to fs */ | |
b99888a0 DW |
703 | static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino, |
704 | const char *source_dir, ext2_ino_t root, | |
705 | struct hdlinks_s *hdlinks) | |
0d4deba2 | 706 | { |
052064b9 RY |
707 | const char *name; |
708 | DIR *dh; | |
709 | struct dirent *dent; | |
710 | struct stat st; | |
711 | char ln_target[PATH_MAX]; | |
f84894bc | 712 | unsigned int save_inode; |
052064b9 | 713 | ext2_ino_t ino; |
1bad6f46 | 714 | errcode_t retval = 0; |
052064b9 | 715 | int read_cnt; |
f84894bc | 716 | int hdlink; |
052064b9 | 717 | |
052064b9 RY |
718 | if (chdir(source_dir) < 0) { |
719 | com_err(__func__, errno, | |
9c891f7e DW |
720 | _("while changing working directory to \"%s\""), |
721 | source_dir); | |
052064b9 RY |
722 | return errno; |
723 | } | |
724 | ||
725 | if (!(dh = opendir("."))) { | |
726 | com_err(__func__, errno, | |
8f8d8a57 | 727 | _("while opening directory \"%s\""), source_dir); |
052064b9 RY |
728 | return errno; |
729 | } | |
730 | ||
8f8d8a57 | 731 | while ((dent = readdir(dh))) { |
9c891f7e DW |
732 | if ((!strcmp(dent->d_name, ".")) || |
733 | (!strcmp(dent->d_name, ".."))) | |
052064b9 | 734 | continue; |
1bad6f46 DW |
735 | if (lstat(dent->d_name, &st)) { |
736 | com_err(__func__, errno, _("while lstat \"%s\""), | |
737 | dent->d_name); | |
738 | goto out; | |
739 | } | |
052064b9 RY |
740 | name = dent->d_name; |
741 | ||
f84894bc RY |
742 | /* Check for hardlinks */ |
743 | save_inode = 0; | |
9c891f7e DW |
744 | if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) && |
745 | st.st_nlink > 1) { | |
b99888a0 | 746 | hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino); |
f84894bc | 747 | if (hdlink >= 0) { |
a3111e80 | 748 | retval = add_link(fs, parent_ino, |
b99888a0 | 749 | hdlinks->hdl[hdlink].dst_ino, |
9c891f7e | 750 | name); |
f84894bc | 751 | if (retval) { |
9c891f7e DW |
752 | com_err(__func__, retval, |
753 | "while linking %s", name); | |
1bad6f46 | 754 | goto out; |
f84894bc RY |
755 | } |
756 | continue; | |
757 | } else | |
758 | save_inode = 1; | |
759 | } | |
760 | ||
052064b9 | 761 | switch(st.st_mode & S_IFMT) { |
9c891f7e DW |
762 | case S_IFCHR: |
763 | case S_IFBLK: | |
764 | case S_IFIFO: | |
c61e0068 | 765 | case S_IFSOCK: |
a3111e80 | 766 | retval = do_mknod_internal(fs, parent_ino, name, &st); |
9c891f7e DW |
767 | if (retval) { |
768 | com_err(__func__, retval, | |
769 | _("while creating special file " | |
770 | "\"%s\""), name); | |
1bad6f46 | 771 | goto out; |
9c891f7e DW |
772 | } |
773 | break; | |
9c891f7e DW |
774 | case S_IFLNK: |
775 | read_cnt = readlink(name, ln_target, | |
1bad6f46 | 776 | sizeof(ln_target) - 1); |
9c891f7e DW |
777 | if (read_cnt == -1) { |
778 | com_err(__func__, errno, | |
779 | _("while trying to readlink \"%s\""), | |
780 | name); | |
ec3a42b1 DW |
781 | retval = errno; |
782 | goto out; | |
9c891f7e DW |
783 | } |
784 | ln_target[read_cnt] = '\0'; | |
a3111e80 DW |
785 | retval = do_symlink_internal(fs, parent_ino, name, |
786 | ln_target, root); | |
9c891f7e DW |
787 | if (retval) { |
788 | com_err(__func__, retval, | |
789 | _("while writing symlink\"%s\""), | |
790 | name); | |
1bad6f46 | 791 | goto out; |
9c891f7e DW |
792 | } |
793 | break; | |
794 | case S_IFREG: | |
a3111e80 DW |
795 | retval = do_write_internal(fs, parent_ino, name, name, |
796 | root); | |
9c891f7e DW |
797 | if (retval) { |
798 | com_err(__func__, retval, | |
799 | _("while writing file \"%s\""), name); | |
1bad6f46 | 800 | goto out; |
9c891f7e DW |
801 | } |
802 | break; | |
803 | case S_IFDIR: | |
a3111e80 DW |
804 | retval = do_mkdir_internal(fs, parent_ino, name, &st, |
805 | root); | |
9c891f7e DW |
806 | if (retval) { |
807 | com_err(__func__, retval, | |
808 | _("while making dir \"%s\""), name); | |
1bad6f46 | 809 | goto out; |
9c891f7e | 810 | } |
a3111e80 | 811 | retval = ext2fs_namei(fs, root, parent_ino, |
9c891f7e DW |
812 | name, &ino); |
813 | if (retval) { | |
814 | com_err(name, retval, 0); | |
1bad6f46 | 815 | goto out; |
9c891f7e DW |
816 | } |
817 | /* Populate the dir recursively*/ | |
b99888a0 | 818 | retval = __populate_fs(fs, ino, name, root, hdlinks); |
9c891f7e DW |
819 | if (retval) { |
820 | com_err(__func__, retval, | |
821 | _("while adding dir \"%s\""), name); | |
1bad6f46 | 822 | goto out; |
9c891f7e | 823 | } |
a3111e80 | 824 | if (chdir("..")) { |
1bad6f46 DW |
825 | com_err(__func__, errno, _("during cd ..")); |
826 | retval = errno; | |
827 | goto out; | |
a3111e80 | 828 | } |
9c891f7e DW |
829 | break; |
830 | default: | |
831 | com_err(__func__, 0, | |
832 | _("ignoring entry \"%s\""), name); | |
052064b9 | 833 | } |
b6960654 | 834 | |
a3111e80 | 835 | retval = ext2fs_namei(fs, root, parent_ino, name, &ino); |
8f8d8a57 | 836 | if (retval) { |
b6960654 | 837 | com_err(name, retval, 0); |
1bad6f46 | 838 | goto out; |
b6960654 RY |
839 | } |
840 | ||
a3111e80 | 841 | retval = set_inode_extra(fs, parent_ino, ino, &st); |
8f8d8a57 | 842 | if (retval) { |
b6960654 RY |
843 | com_err(__func__, retval, |
844 | _("while setting inode for \"%s\""), name); | |
1bad6f46 | 845 | goto out; |
b6960654 | 846 | } |
f84894bc | 847 | |
c84da2ee RB |
848 | retval = set_inode_xattr(fs, ino, name); |
849 | if (retval) { | |
850 | com_err(__func__, retval, | |
851 | _("while setting xattrs for \"%s\""), name); | |
852 | goto out; | |
853 | } | |
854 | ||
f84894bc RY |
855 | /* Save the hardlink ino */ |
856 | if (save_inode) { | |
857 | /* | |
858 | * Check whether need more memory, and we don't need | |
859 | * free() since the lifespan will be over after the fs | |
860 | * populated. | |
861 | */ | |
b99888a0 DW |
862 | if (hdlinks->count == hdlinks->size) { |
863 | void *p = realloc(hdlinks->hdl, | |
864 | (hdlinks->size + HDLINK_CNT) * | |
865 | sizeof(struct hdlink_s)); | |
866 | if (p == NULL) { | |
867 | com_err(name, errno, | |
868 | _("Not enough memory")); | |
1bad6f46 DW |
869 | retval = EXT2_ET_NO_MEMORY; |
870 | goto out; | |
f84894bc | 871 | } |
b99888a0 DW |
872 | hdlinks->hdl = p; |
873 | hdlinks->size += HDLINK_CNT; | |
f84894bc | 874 | } |
b99888a0 DW |
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; | |
878 | hdlinks->count++; | |
f84894bc | 879 | } |
052064b9 | 880 | } |
1bad6f46 DW |
881 | |
882 | out: | |
052064b9 RY |
883 | closedir(dh); |
884 | return retval; | |
0d4deba2 | 885 | } |
b99888a0 DW |
886 | |
887 | errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino, | |
888 | const char *source_dir, ext2_ino_t root) | |
889 | { | |
890 | struct hdlinks_s hdlinks; | |
891 | errcode_t retval; | |
892 | ||
893 | hdlinks.count = 0; | |
894 | hdlinks.size = HDLINK_CNT; | |
895 | hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s)); | |
896 | if (hdlinks.hdl == NULL) { | |
897 | com_err(__func__, errno, "Not enough memory"); | |
898 | return errno; | |
899 | } | |
900 | ||
901 | retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks); | |
902 | ||
903 | free(hdlinks.hdl); | |
904 | return retval; | |
905 | } |