]>
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 | 77 | if (retval) { |
b04af4fe | 78 | com_err(__func__, retval, _("while reading inode %u"), ino); |
f84894bc RY |
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 | 86 | if (retval) { |
b04af4fe DW |
87 | com_err(__func__, retval, |
88 | _("while expanding directory")); | |
f84894bc RY |
89 | return retval; |
90 | } | |
a433db04 DW |
91 | retval = ext2fs_link(fs, parent_ino, name, ino, |
92 | ext2_file_type(inode.i_mode)); | |
f84894bc RY |
93 | } |
94 | if (retval) { | |
b04af4fe | 95 | com_err(__func__, retval, _("while linking \"%s\""), name); |
f84894bc RY |
96 | return retval; |
97 | } | |
98 | ||
99 | inode.i_links_count++; | |
100 | ||
a3111e80 | 101 | retval = ext2fs_write_inode(fs, ino, &inode); |
f84894bc | 102 | if (retval) |
b04af4fe | 103 | com_err(__func__, retval, _("while writing inode %u"), ino); |
f84894bc RY |
104 | |
105 | return retval; | |
106 | } | |
107 | ||
b6960654 | 108 | /* Set the uid, gid, mode and time for the inode */ |
a3111e80 DW |
109 | static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd, |
110 | ext2_ino_t ino, struct stat *st) | |
b6960654 RY |
111 | { |
112 | errcode_t retval; | |
113 | struct ext2_inode inode; | |
114 | ||
a3111e80 | 115 | retval = ext2fs_read_inode(fs, ino, &inode); |
b6960654 | 116 | if (retval) { |
b04af4fe | 117 | com_err(__func__, retval, _("while reading inode %u"), ino); |
b6960654 RY |
118 | return retval; |
119 | } | |
120 | ||
b04af4fe DW |
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; | |
b6960654 | 127 | |
a3111e80 | 128 | retval = ext2fs_write_inode(fs, ino, &inode); |
3aec816f | 129 | if (retval) |
b04af4fe | 130 | com_err(__func__, retval, _("while writing inode %u"), ino); |
3aec816f | 131 | return retval; |
b6960654 RY |
132 | } |
133 | ||
b04af4fe DW |
134 | static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino, |
135 | const char *filename) | |
c84da2ee RB |
136 | { |
137 | #ifdef HAVE_LLISTXATTR | |
138 | errcode_t retval, close_retval; | |
c84da2ee RB |
139 | struct ext2_xattr_handle *handle; |
140 | ssize_t size, value_size; | |
76f13234 | 141 | char *list = NULL; |
c84da2ee RB |
142 | int i; |
143 | ||
144 | size = llistxattr(filename, NULL, 0); | |
145 | if (size == -1) { | |
b04af4fe DW |
146 | retval = errno; |
147 | com_err(__func__, retval, _("while listing attributes of \"%s\""), | |
148 | filename); | |
149 | return retval; | |
c84da2ee RB |
150 | } else if (size == 0) { |
151 | return 0; | |
152 | } | |
153 | ||
154 | retval = ext2fs_xattrs_open(fs, ino, &handle); | |
155 | if (retval) { | |
156 | if (retval == EXT2_ET_MISSING_EA_FEATURE) | |
157 | return 0; | |
b04af4fe | 158 | com_err(__func__, retval, _("while opening inode %u"), ino); |
c84da2ee RB |
159 | return retval; |
160 | } | |
161 | ||
162 | retval = ext2fs_get_mem(size, &list); | |
163 | if (retval) { | |
b04af4fe | 164 | com_err(__func__, retval, _("while allocating memory")); |
c84da2ee RB |
165 | goto out; |
166 | } | |
167 | ||
168 | size = llistxattr(filename, list, size); | |
169 | if (size == -1) { | |
c84da2ee | 170 | retval = errno; |
b04af4fe DW |
171 | com_err(__func__, retval, _("while listing attributes of \"%s\""), |
172 | filename); | |
c84da2ee RB |
173 | goto out; |
174 | } | |
175 | ||
176 | for (i = 0; i < size; i += strlen(&list[i]) + 1) { | |
177 | const char *name = &list[i]; | |
178 | char *value; | |
179 | ||
180 | value_size = getxattr(filename, name, NULL, 0); | |
181 | if (value_size == -1) { | |
c84da2ee | 182 | retval = errno; |
b04af4fe DW |
183 | com_err(__func__, retval, |
184 | _("while reading attribute \"%s\" of \"%s\""), | |
185 | name, filename); | |
c84da2ee RB |
186 | break; |
187 | } | |
188 | ||
189 | retval = ext2fs_get_mem(value_size, &value); | |
190 | if (retval) { | |
b04af4fe | 191 | com_err(__func__, retval, _("while allocating memory")); |
c84da2ee RB |
192 | break; |
193 | } | |
194 | ||
195 | value_size = getxattr(filename, name, value, value_size); | |
196 | if (value_size == -1) { | |
197 | ext2fs_free_mem(&value); | |
c84da2ee | 198 | retval = errno; |
b04af4fe DW |
199 | com_err(__func__, retval, |
200 | _("while reading attribute \"%s\" of \"%s\""), | |
201 | name, filename); | |
c84da2ee RB |
202 | break; |
203 | } | |
204 | ||
205 | retval = ext2fs_xattr_set(handle, name, value, value_size); | |
206 | ext2fs_free_mem(&value); | |
207 | if (retval) { | |
208 | com_err(__func__, retval, | |
b04af4fe DW |
209 | _("while writing attribute \"%s\" to inode %u"), |
210 | name, ino); | |
c84da2ee RB |
211 | break; |
212 | } | |
213 | ||
214 | } | |
215 | out: | |
216 | ext2fs_free_mem(&list); | |
217 | close_retval = ext2fs_xattrs_close(&handle); | |
218 | if (close_retval) { | |
b04af4fe | 219 | com_err(__func__, retval, _("while closing inode %u"), ino); |
c84da2ee RB |
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 | retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino); |
b2346118 | 260 | if (retval) { |
b04af4fe DW |
261 | com_err(__func__, retval, _("while allocating inode \"%s\""), |
262 | name); | |
b2346118 RY |
263 | return retval; |
264 | } | |
265 | ||
266 | #ifdef DEBUGFS | |
267 | printf("Allocated inode: %u\n", ino); | |
268 | #endif | |
a3111e80 | 269 | retval = ext2fs_link(fs, cwd, name, ino, filetype); |
b2346118 | 270 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
a3111e80 | 271 | retval = ext2fs_expand_dir(fs, cwd); |
b2346118 | 272 | if (retval) { |
b04af4fe DW |
273 | com_err(__func__, retval, |
274 | _("while expanding directory")); | |
b2346118 RY |
275 | return retval; |
276 | } | |
a3111e80 | 277 | retval = ext2fs_link(fs, cwd, name, ino, filetype); |
b2346118 RY |
278 | } |
279 | if (retval) { | |
b04af4fe | 280 | com_err(name, retval, _("while creating inode \"%s\""), name); |
ec3a42b1 | 281 | return retval; |
b2346118 | 282 | } |
a3111e80 | 283 | if (ext2fs_test_inode_bitmap2(fs->inode_map, ino)) |
b2346118 | 284 | com_err(__func__, 0, "Warning: inode already set"); |
a3111e80 | 285 | ext2fs_inode_alloc_stats2(fs, ino, +1, 0); |
b2346118 RY |
286 | memset(&inode, 0, sizeof(inode)); |
287 | inode.i_mode = mode; | |
288 | inode.i_atime = inode.i_ctime = inode.i_mtime = | |
a3111e80 | 289 | fs->now ? fs->now : time(0); |
b2346118 | 290 | |
3cbb51f9 AD |
291 | if (filetype != S_IFIFO) { |
292 | devmajor = major(st->st_rdev); | |
293 | devminor = minor(st->st_rdev); | |
294 | ||
295 | if ((devmajor < 256) && (devminor < 256)) { | |
296 | inode.i_block[0] = devmajor * 256 + devminor; | |
297 | inode.i_block[1] = 0; | |
298 | } else { | |
299 | inode.i_block[0] = 0; | |
300 | inode.i_block[1] = (devminor & 0xff) | (devmajor << 8) | | |
301 | ((devminor & ~0xff) << 12); | |
302 | } | |
b2346118 RY |
303 | } |
304 | inode.i_links_count = 1; | |
305 | ||
a3111e80 | 306 | retval = ext2fs_write_new_inode(fs, ino, &inode); |
b2346118 | 307 | if (retval) |
b04af4fe | 308 | com_err(__func__, retval, _("while writing inode %u"), ino); |
b2346118 RY |
309 | |
310 | return retval; | |
0d4deba2 RY |
311 | } |
312 | ||
313 | /* Make a symlink name -> target */ | |
a3111e80 DW |
314 | errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name, |
315 | char *target, ext2_ino_t root) | |
0d4deba2 | 316 | { |
b25ddc5e RY |
317 | char *cp; |
318 | ext2_ino_t parent_ino; | |
319 | errcode_t retval; | |
b25ddc5e RY |
320 | |
321 | cp = strrchr(name, '/'); | |
322 | if (cp) { | |
323 | *cp = 0; | |
a3111e80 | 324 | retval = ext2fs_namei(fs, root, cwd, name, &parent_ino); |
8f8d8a57 | 325 | if (retval) { |
b25ddc5e RY |
326 | com_err(name, retval, 0); |
327 | return retval; | |
328 | } | |
329 | name = cp+1; | |
330 | } else | |
331 | parent_ino = cwd; | |
332 | ||
a3111e80 | 333 | retval = ext2fs_symlink(fs, parent_ino, 0, name, target); |
b25ddc5e | 334 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
a3111e80 | 335 | retval = ext2fs_expand_dir(fs, parent_ino); |
b25ddc5e | 336 | if (retval) { |
9c891f7e | 337 | com_err("do_symlink_internal", retval, |
b04af4fe | 338 | _("while expanding directory")); |
b25ddc5e RY |
339 | return retval; |
340 | } | |
b04af4fe | 341 | retval = ext2fs_symlink(fs, parent_ino, 0, name, target); |
b25ddc5e | 342 | } |
3aec816f | 343 | if (retval) |
b04af4fe DW |
344 | com_err("ext2fs_symlink", retval, |
345 | _("while creating symlink \"%s\""), name); | |
3aec816f | 346 | return retval; |
0d4deba2 RY |
347 | } |
348 | ||
349 | /* Make a directory in the fs */ | |
a3111e80 DW |
350 | errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name, |
351 | struct stat *st, ext2_ino_t root) | |
0d4deba2 | 352 | { |
3068f03f | 353 | char *cp; |
c4c9bc59 | 354 | ext2_ino_t parent_ino; |
3068f03f | 355 | errcode_t retval; |
3068f03f RY |
356 | |
357 | ||
358 | cp = strrchr(name, '/'); | |
359 | if (cp) { | |
360 | *cp = 0; | |
a3111e80 | 361 | retval = ext2fs_namei(fs, root, cwd, name, &parent_ino); |
8f8d8a57 | 362 | if (retval) { |
b04af4fe DW |
363 | com_err(name, retval, _("while looking up \"%s\""), |
364 | name); | |
3068f03f RY |
365 | return retval; |
366 | } | |
367 | name = cp+1; | |
368 | } else | |
369 | parent_ino = cwd; | |
370 | ||
a3111e80 | 371 | retval = ext2fs_mkdir(fs, parent_ino, 0, name); |
3068f03f | 372 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
a3111e80 | 373 | retval = ext2fs_expand_dir(fs, parent_ino); |
3068f03f | 374 | if (retval) { |
b04af4fe DW |
375 | com_err(__func__, retval, |
376 | _("while expanding directory")); | |
3068f03f RY |
377 | return retval; |
378 | } | |
b04af4fe | 379 | retval = ext2fs_mkdir(fs, parent_ino, 0, name); |
3068f03f | 380 | } |
3aec816f | 381 | if (retval) |
b04af4fe DW |
382 | com_err("ext2fs_mkdir", retval, |
383 | _("while creating directory \"%s\""), name); | |
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 | 608 | if (fd < 0) { |
b04af4fe DW |
609 | retval = errno; |
610 | com_err(__func__, retval, _("while opening \"%s\" to copy"), | |
611 | src); | |
612 | return retval; | |
ce600dc4 RY |
613 | } |
614 | if (fstat(fd, &statbuf) < 0) { | |
b04af4fe DW |
615 | retval = errno; |
616 | goto out; | |
ce600dc4 RY |
617 | } |
618 | ||
a3111e80 | 619 | retval = ext2fs_namei(fs, root, cwd, dest, &newfile); |
ce600dc4 | 620 | if (retval == 0) { |
b04af4fe DW |
621 | retval = EXT2_ET_FILE_EXISTS; |
622 | goto out; | |
ce600dc4 RY |
623 | } |
624 | ||
a3111e80 | 625 | retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile); |
b04af4fe DW |
626 | if (retval) |
627 | goto out; | |
ce600dc4 RY |
628 | #ifdef DEBUGFS |
629 | printf("Allocated inode: %u\n", newfile); | |
630 | #endif | |
a3111e80 | 631 | retval = ext2fs_link(fs, cwd, dest, newfile, |
ce600dc4 RY |
632 | EXT2_FT_REG_FILE); |
633 | if (retval == EXT2_ET_DIR_NO_SPACE) { | |
a3111e80 | 634 | retval = ext2fs_expand_dir(fs, cwd); |
b04af4fe DW |
635 | if (retval) |
636 | goto out; | |
a3111e80 | 637 | retval = ext2fs_link(fs, cwd, dest, newfile, |
ce600dc4 RY |
638 | EXT2_FT_REG_FILE); |
639 | } | |
b04af4fe DW |
640 | if (retval) |
641 | goto out; | |
a3111e80 | 642 | if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile)) |
ce600dc4 | 643 | com_err(__func__, 0, "Warning: inode already set"); |
a3111e80 | 644 | ext2fs_inode_alloc_stats2(fs, newfile, +1, 0); |
ce600dc4 RY |
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 = | |
a3111e80 | 648 | fs->now ? fs->now : time(0); |
ce600dc4 | 649 | inode.i_links_count = 1; |
22302aa3 | 650 | retval = ext2fs_inode_size_set(fs, &inode, statbuf.st_size); |
b04af4fe DW |
651 | if (retval) |
652 | goto out; | |
a3111e80 | 653 | if (EXT2_HAS_INCOMPAT_FEATURE(fs->super, |
d23b1965 TT |
654 | EXT4_FEATURE_INCOMPAT_INLINE_DATA)) { |
655 | inode.i_flags |= EXT4_INLINE_DATA_FL; | |
a3111e80 | 656 | } else if (fs->super->s_feature_incompat & |
d23b1965 | 657 | EXT3_FEATURE_INCOMPAT_EXTENTS) { |
3548bb64 DW |
658 | ext2_extent_handle_t handle; |
659 | ||
660 | inode.i_flags &= ~EXT4_EXTENTS_FL; | |
661 | retval = ext2fs_extent_open2(fs, newfile, &inode, &handle); | |
662 | if (retval) | |
b04af4fe | 663 | goto out; |
3548bb64 | 664 | ext2fs_extent_free(handle); |
ce600dc4 RY |
665 | } |
666 | ||
a3111e80 | 667 | retval = ext2fs_write_new_inode(fs, newfile, &inode); |
b04af4fe DW |
668 | if (retval) |
669 | goto out; | |
d23b1965 | 670 | if (inode.i_flags & EXT4_INLINE_DATA_FL) { |
a3111e80 | 671 | retval = ext2fs_inline_data_init(fs, newfile); |
b04af4fe DW |
672 | if (retval) |
673 | goto out; | |
d23b1965 | 674 | } |
ce600dc4 | 675 | if (LINUX_S_ISREG(inode.i_mode)) { |
76f13234 | 676 | retval = copy_file(fs, fd, &statbuf, newfile); |
ce600dc4 | 677 | if (retval) |
b04af4fe | 678 | goto out; |
ce600dc4 | 679 | } |
b04af4fe | 680 | out: |
ce600dc4 | 681 | close(fd); |
117b54c3 | 682 | return retval; |
0d4deba2 RY |
683 | } |
684 | ||
685 | /* Copy files from source_dir to fs */ | |
b99888a0 DW |
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) | |
0d4deba2 | 689 | { |
052064b9 RY |
690 | const char *name; |
691 | DIR *dh; | |
692 | struct dirent *dent; | |
693 | struct stat st; | |
694 | char ln_target[PATH_MAX]; | |
f84894bc | 695 | unsigned int save_inode; |
052064b9 | 696 | ext2_ino_t ino; |
1bad6f46 | 697 | errcode_t retval = 0; |
052064b9 | 698 | int read_cnt; |
f84894bc | 699 | int hdlink; |
052064b9 | 700 | |
052064b9 | 701 | if (chdir(source_dir) < 0) { |
b04af4fe DW |
702 | retval = errno; |
703 | com_err(__func__, retval, | |
9c891f7e DW |
704 | _("while changing working directory to \"%s\""), |
705 | source_dir); | |
b04af4fe | 706 | return retval; |
052064b9 RY |
707 | } |
708 | ||
709 | if (!(dh = opendir("."))) { | |
b04af4fe DW |
710 | retval = errno; |
711 | com_err(__func__, retval, | |
8f8d8a57 | 712 | _("while opening directory \"%s\""), source_dir); |
b04af4fe | 713 | return retval; |
052064b9 RY |
714 | } |
715 | ||
8f8d8a57 | 716 | while ((dent = readdir(dh))) { |
9c891f7e DW |
717 | if ((!strcmp(dent->d_name, ".")) || |
718 | (!strcmp(dent->d_name, ".."))) | |
052064b9 | 719 | continue; |
1bad6f46 | 720 | if (lstat(dent->d_name, &st)) { |
b04af4fe DW |
721 | retval = errno; |
722 | com_err(__func__, retval, _("while lstat \"%s\""), | |
1bad6f46 DW |
723 | dent->d_name); |
724 | goto out; | |
725 | } | |
052064b9 RY |
726 | name = dent->d_name; |
727 | ||
f84894bc RY |
728 | /* Check for hardlinks */ |
729 | save_inode = 0; | |
9c891f7e DW |
730 | if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) && |
731 | st.st_nlink > 1) { | |
b99888a0 | 732 | hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino); |
f84894bc | 733 | if (hdlink >= 0) { |
a3111e80 | 734 | retval = add_link(fs, parent_ino, |
b99888a0 | 735 | hdlinks->hdl[hdlink].dst_ino, |
9c891f7e | 736 | name); |
f84894bc | 737 | if (retval) { |
9c891f7e DW |
738 | com_err(__func__, retval, |
739 | "while linking %s", name); | |
1bad6f46 | 740 | goto out; |
f84894bc RY |
741 | } |
742 | continue; | |
743 | } else | |
744 | save_inode = 1; | |
745 | } | |
746 | ||
052064b9 | 747 | switch(st.st_mode & S_IFMT) { |
9c891f7e DW |
748 | case S_IFCHR: |
749 | case S_IFBLK: | |
750 | case S_IFIFO: | |
c61e0068 | 751 | case S_IFSOCK: |
a3111e80 | 752 | retval = do_mknod_internal(fs, parent_ino, name, &st); |
9c891f7e DW |
753 | if (retval) { |
754 | com_err(__func__, retval, | |
755 | _("while creating special file " | |
756 | "\"%s\""), name); | |
1bad6f46 | 757 | goto out; |
9c891f7e DW |
758 | } |
759 | break; | |
9c891f7e DW |
760 | case S_IFLNK: |
761 | read_cnt = readlink(name, ln_target, | |
1bad6f46 | 762 | sizeof(ln_target) - 1); |
9c891f7e | 763 | if (read_cnt == -1) { |
ec3a42b1 | 764 | retval = errno; |
b04af4fe DW |
765 | com_err(__func__, retval, |
766 | _("while trying to read link \"%s\""), | |
767 | name); | |
ec3a42b1 | 768 | goto out; |
9c891f7e DW |
769 | } |
770 | ln_target[read_cnt] = '\0'; | |
a3111e80 DW |
771 | retval = do_symlink_internal(fs, parent_ino, name, |
772 | ln_target, root); | |
9c891f7e DW |
773 | if (retval) { |
774 | com_err(__func__, retval, | |
775 | _("while writing symlink\"%s\""), | |
776 | name); | |
1bad6f46 | 777 | goto out; |
9c891f7e DW |
778 | } |
779 | break; | |
780 | case S_IFREG: | |
a3111e80 DW |
781 | retval = do_write_internal(fs, parent_ino, name, name, |
782 | root); | |
9c891f7e DW |
783 | if (retval) { |
784 | com_err(__func__, retval, | |
785 | _("while writing file \"%s\""), name); | |
1bad6f46 | 786 | goto out; |
9c891f7e DW |
787 | } |
788 | break; | |
789 | case S_IFDIR: | |
b04af4fe DW |
790 | /* Don't choke on /lost+found */ |
791 | if (parent_ino == EXT2_ROOT_INO && | |
792 | strcmp(name, "lost+found") == 0) | |
793 | goto find_lnf; | |
a3111e80 DW |
794 | retval = do_mkdir_internal(fs, parent_ino, name, &st, |
795 | root); | |
9c891f7e DW |
796 | if (retval) { |
797 | com_err(__func__, retval, | |
798 | _("while making dir \"%s\""), name); | |
1bad6f46 | 799 | goto out; |
9c891f7e | 800 | } |
b04af4fe | 801 | find_lnf: |
a3111e80 | 802 | retval = ext2fs_namei(fs, root, parent_ino, |
9c891f7e DW |
803 | name, &ino); |
804 | if (retval) { | |
805 | com_err(name, retval, 0); | |
1bad6f46 | 806 | goto out; |
9c891f7e DW |
807 | } |
808 | /* Populate the dir recursively*/ | |
b99888a0 | 809 | retval = __populate_fs(fs, ino, name, root, hdlinks); |
b04af4fe | 810 | if (retval) |
1bad6f46 | 811 | goto out; |
a3111e80 | 812 | if (chdir("..")) { |
1bad6f46 | 813 | retval = errno; |
b04af4fe DW |
814 | com_err(__func__, retval, |
815 | _("while changing directory")); | |
1bad6f46 | 816 | goto out; |
a3111e80 | 817 | } |
9c891f7e DW |
818 | break; |
819 | default: | |
820 | com_err(__func__, 0, | |
821 | _("ignoring entry \"%s\""), name); | |
052064b9 | 822 | } |
b6960654 | 823 | |
a3111e80 | 824 | retval = ext2fs_namei(fs, root, parent_ino, name, &ino); |
8f8d8a57 | 825 | if (retval) { |
b04af4fe DW |
826 | com_err(name, retval, _("while looking up \"%s\""), |
827 | name); | |
1bad6f46 | 828 | goto out; |
b6960654 RY |
829 | } |
830 | ||
a3111e80 | 831 | retval = set_inode_extra(fs, parent_ino, ino, &st); |
8f8d8a57 | 832 | if (retval) { |
b6960654 RY |
833 | com_err(__func__, retval, |
834 | _("while setting inode for \"%s\""), name); | |
1bad6f46 | 835 | goto out; |
b6960654 | 836 | } |
f84894bc | 837 | |
c84da2ee RB |
838 | retval = set_inode_xattr(fs, ino, name); |
839 | if (retval) { | |
840 | com_err(__func__, retval, | |
841 | _("while setting xattrs for \"%s\""), name); | |
842 | goto out; | |
843 | } | |
844 | ||
f84894bc RY |
845 | /* Save the hardlink ino */ |
846 | if (save_inode) { | |
847 | /* | |
848 | * Check whether need more memory, and we don't need | |
849 | * free() since the lifespan will be over after the fs | |
850 | * populated. | |
851 | */ | |
b99888a0 DW |
852 | if (hdlinks->count == hdlinks->size) { |
853 | void *p = realloc(hdlinks->hdl, | |
854 | (hdlinks->size + HDLINK_CNT) * | |
855 | sizeof(struct hdlink_s)); | |
856 | if (p == NULL) { | |
1bad6f46 | 857 | retval = EXT2_ET_NO_MEMORY; |
b04af4fe DW |
858 | com_err(name, retval, |
859 | _("while saving inode data")); | |
1bad6f46 | 860 | goto out; |
f84894bc | 861 | } |
b99888a0 DW |
862 | hdlinks->hdl = p; |
863 | hdlinks->size += HDLINK_CNT; | |
f84894bc | 864 | } |
b99888a0 DW |
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; | |
868 | hdlinks->count++; | |
f84894bc | 869 | } |
052064b9 | 870 | } |
1bad6f46 DW |
871 | |
872 | out: | |
052064b9 RY |
873 | closedir(dh); |
874 | return retval; | |
0d4deba2 | 875 | } |
b99888a0 DW |
876 | |
877 | errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino, | |
878 | const char *source_dir, ext2_ino_t root) | |
879 | { | |
880 | struct hdlinks_s hdlinks; | |
881 | errcode_t retval; | |
882 | ||
b04af4fe DW |
883 | if (!(fs->flags & EXT2_FLAG_RW)) { |
884 | com_err(__func__, 0, "Filesystem opened readonly"); | |
885 | return EROFS; | |
886 | } | |
887 | ||
b99888a0 DW |
888 | hdlinks.count = 0; |
889 | hdlinks.size = HDLINK_CNT; | |
890 | hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s)); | |
891 | if (hdlinks.hdl == NULL) { | |
b04af4fe DW |
892 | retval = errno; |
893 | com_err(__func__, retval, _("while allocating memory")); | |
894 | return retval; | |
b99888a0 DW |
895 | } |
896 | ||
897 | retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks); | |
898 | ||
899 | free(hdlinks.hdl); | |
900 | return retval; | |
901 | } |