]>
Commit | Line | Data |
---|---|---|
81cbf1ef DW |
1 | /* |
2 | * fuse2fs.c - FUSE server for e2fsprogs. | |
3 | * | |
4 | * Copyright (C) 2014 Oracle. | |
5 | * | |
6 | * %Begin-Header% | |
7 | * This file may be redistributed under the terms of the GNU Public | |
8 | * License. | |
9 | * %End-Header% | |
10 | */ | |
11 | #define _FILE_OFFSET_BITS 64 | |
12 | #define FUSE_USE_VERSION 29 | |
4e222d9b | 13 | #ifndef _GNU_SOURCE |
81cbf1ef | 14 | #define _GNU_SOURCE |
4e222d9b | 15 | #endif |
81cbf1ef DW |
16 | #include "config.h" |
17 | #include <pthread.h> | |
18 | #ifdef __linux__ | |
19 | # include <linux/fs.h> | |
20 | # include <linux/falloc.h> | |
21 | # include <linux/xattr.h> | |
aa6d3260 | 22 | # define FUSE_PLATFORM_OPTS ",big_writes" |
81cbf1ef DW |
23 | # ifdef HAVE_SYS_ACL_H |
24 | # define TRANSLATE_LINUX_ACLS | |
25 | # endif | |
26 | #else | |
27 | # define FUSE_PLATFORM_OPTS "" | |
28 | #endif | |
29 | #ifdef TRANSLATE_LINUX_ACLS | |
30 | # include <sys/acl.h> | |
31 | #endif | |
32 | #include <sys/ioctl.h> | |
33 | #include <unistd.h> | |
34 | #include <fuse.h> | |
35 | #include <inttypes.h> | |
36 | #include "ext2fs/ext2fs.h" | |
37 | #include "ext2fs/ext2_fs.h" | |
38 | ||
69df8496 TT |
39 | #include "../version.h" |
40 | ||
81cbf1ef DW |
41 | #ifdef ENABLE_NLS |
42 | #include <libintl.h> | |
43 | #include <locale.h> | |
44 | #define _(a) (gettext(a)) | |
45 | #ifdef gettext_noop | |
46 | #define N_(a) gettext_noop(a) | |
47 | #else | |
48 | #define N_(a) (a) | |
49 | #endif | |
50 | #define P_(singular, plural, n) (ngettext(singular, plural, n)) | |
51 | #ifndef NLS_CAT_NAME | |
52 | #define NLS_CAT_NAME "e2fsprogs" | |
53 | #endif | |
54 | #ifndef LOCALEDIR | |
55 | #define LOCALEDIR "/usr/share/locale" | |
56 | #endif | |
57 | #else | |
58 | #define _(a) (a) | |
59 | #define N_(a) a | |
60 | #define P_(singular, plural, n) ((n) == 1 ? (singular) : (plural)) | |
61 | #endif | |
62 | ||
63 | static ext2_filsys global_fs; /* Try not to use this directly */ | |
64 | ||
65 | #undef DEBUG | |
66 | ||
67 | #ifdef DEBUG | |
68 | # define dbg_printf(f, a...) do {printf("FUSE2FS-" f, ## a); \ | |
69 | fflush(stdout); \ | |
70 | } while (0) | |
71 | #else | |
72 | # define dbg_printf(f, a...) | |
73 | #endif | |
74 | ||
75 | #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8) | |
76 | # ifdef _IOR | |
77 | # ifdef _IOW | |
78 | # define SUPPORT_I_FLAGS | |
79 | # endif | |
80 | # endif | |
81 | #endif | |
82 | ||
83 | #ifdef FALLOC_FL_KEEP_SIZE | |
84 | # define FL_KEEP_SIZE_FLAG FALLOC_FL_KEEP_SIZE | |
85 | # define SUPPORT_FALLOCATE | |
86 | #else | |
87 | # define FL_KEEP_SIZE_FLAG (0) | |
88 | #endif | |
89 | ||
90 | #ifdef FALLOC_FL_PUNCH_HOLE | |
91 | # define FL_PUNCH_HOLE_FLAG FALLOC_FL_PUNCH_HOLE | |
92 | #else | |
93 | # define FL_PUNCH_HOLE_FLAG (0) | |
94 | #endif | |
95 | ||
96 | errcode_t ext2fs_run_ext3_journal(ext2_filsys *fs); | |
97 | ||
1fc23b5e TT |
98 | #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */ |
99 | int journal_enable_debug = -1; | |
100 | #endif | |
101 | ||
81cbf1ef DW |
102 | /* ACL translation stuff */ |
103 | #ifdef TRANSLATE_LINUX_ACLS | |
104 | /* | |
105 | * Copied from acl_ea.h in libacl source; ACLs have to be sent to and from fuse | |
106 | * in this format... at least on Linux. | |
107 | */ | |
108 | #define ACL_EA_ACCESS "system.posix_acl_access" | |
109 | #define ACL_EA_DEFAULT "system.posix_acl_default" | |
110 | ||
111 | #define ACL_EA_VERSION 0x0002 | |
112 | ||
113 | typedef struct { | |
114 | u_int16_t e_tag; | |
115 | u_int16_t e_perm; | |
116 | u_int32_t e_id; | |
117 | } acl_ea_entry; | |
118 | ||
119 | typedef struct { | |
120 | u_int32_t a_version; | |
fddc423d | 121 | #if __GNUC_PREREQ (4, 8) |
ac3256fd TT |
122 | #pragma GCC diagnostic push |
123 | #pragma GCC diagnostic ignored "-Wpedantic" | |
fddc423d | 124 | #endif |
81cbf1ef | 125 | acl_ea_entry a_entries[0]; |
fddc423d | 126 | #if __GNUC_PREREQ (4, 8) |
ac3256fd | 127 | #pragma GCC diagnostic pop |
fddc423d | 128 | #endif |
81cbf1ef DW |
129 | } acl_ea_header; |
130 | ||
131 | static inline size_t acl_ea_size(int count) | |
132 | { | |
133 | return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry); | |
134 | } | |
135 | ||
136 | static inline int acl_ea_count(size_t size) | |
137 | { | |
138 | if (size < sizeof(acl_ea_header)) | |
139 | return -1; | |
140 | size -= sizeof(acl_ea_header); | |
141 | if (size % sizeof(acl_ea_entry)) | |
142 | return -1; | |
143 | return size / sizeof(acl_ea_entry); | |
144 | } | |
145 | ||
146 | /* | |
147 | * ext4 ACL structures, copied from fs/ext4/acl.h. | |
148 | */ | |
149 | #define EXT4_ACL_VERSION 0x0001 | |
150 | ||
151 | typedef struct { | |
152 | __u16 e_tag; | |
153 | __u16 e_perm; | |
154 | __u32 e_id; | |
155 | } ext4_acl_entry; | |
156 | ||
157 | typedef struct { | |
158 | __u16 e_tag; | |
159 | __u16 e_perm; | |
160 | } ext4_acl_entry_short; | |
161 | ||
162 | typedef struct { | |
163 | __u32 a_version; | |
164 | } ext4_acl_header; | |
165 | ||
166 | static inline size_t ext4_acl_size(int count) | |
167 | { | |
168 | if (count <= 4) { | |
169 | return sizeof(ext4_acl_header) + | |
170 | count * sizeof(ext4_acl_entry_short); | |
171 | } else { | |
172 | return sizeof(ext4_acl_header) + | |
173 | 4 * sizeof(ext4_acl_entry_short) + | |
174 | (count - 4) * sizeof(ext4_acl_entry); | |
175 | } | |
176 | } | |
177 | ||
178 | static inline int ext4_acl_count(size_t size) | |
179 | { | |
180 | ssize_t s; | |
181 | ||
182 | size -= sizeof(ext4_acl_header); | |
183 | s = size - 4 * sizeof(ext4_acl_entry_short); | |
184 | if (s < 0) { | |
185 | if (size % sizeof(ext4_acl_entry_short)) | |
186 | return -1; | |
187 | return size / sizeof(ext4_acl_entry_short); | |
188 | } | |
189 | if (s % sizeof(ext4_acl_entry)) | |
190 | return -1; | |
191 | return s / sizeof(ext4_acl_entry) + 4; | |
192 | } | |
193 | ||
194 | static errcode_t fuse_to_ext4_acl(acl_ea_header *facl, size_t facl_sz, | |
195 | ext4_acl_header **eacl, size_t *eacl_sz) | |
196 | { | |
197 | int i, facl_count; | |
198 | ext4_acl_header *h; | |
199 | size_t h_sz; | |
200 | ext4_acl_entry *e; | |
201 | acl_ea_entry *a; | |
4e222d9b | 202 | unsigned char *hptr; |
81cbf1ef DW |
203 | errcode_t err; |
204 | ||
205 | facl_count = acl_ea_count(facl_sz); | |
206 | h_sz = ext4_acl_size(facl_count); | |
207 | if (facl_count < 0 || facl->a_version != ACL_EA_VERSION) | |
208 | return EXT2_ET_INVALID_ARGUMENT; | |
209 | ||
210 | err = ext2fs_get_mem(h_sz, &h); | |
211 | if (err) | |
212 | return err; | |
213 | ||
214 | h->a_version = ext2fs_cpu_to_le32(EXT4_ACL_VERSION); | |
4e222d9b | 215 | hptr = (unsigned char *) (h + 1); |
81cbf1ef | 216 | for (i = 0, a = facl->a_entries; i < facl_count; i++, a++) { |
4e222d9b | 217 | e = (ext4_acl_entry *) hptr; |
81cbf1ef DW |
218 | e->e_tag = ext2fs_cpu_to_le16(a->e_tag); |
219 | e->e_perm = ext2fs_cpu_to_le16(a->e_perm); | |
220 | ||
221 | switch (a->e_tag) { | |
222 | case ACL_USER: | |
223 | case ACL_GROUP: | |
224 | e->e_id = ext2fs_cpu_to_le32(a->e_id); | |
225 | hptr += sizeof(ext4_acl_entry); | |
226 | break; | |
227 | case ACL_USER_OBJ: | |
228 | case ACL_GROUP_OBJ: | |
229 | case ACL_MASK: | |
230 | case ACL_OTHER: | |
231 | hptr += sizeof(ext4_acl_entry_short); | |
232 | break; | |
233 | default: | |
234 | err = EXT2_ET_INVALID_ARGUMENT; | |
235 | goto out; | |
236 | } | |
237 | } | |
238 | ||
239 | *eacl = h; | |
240 | *eacl_sz = h_sz; | |
241 | return err; | |
242 | out: | |
243 | ext2fs_free_mem(&h); | |
244 | return err; | |
245 | } | |
246 | ||
247 | static errcode_t ext4_to_fuse_acl(acl_ea_header **facl, size_t *facl_sz, | |
248 | ext4_acl_header *eacl, size_t eacl_sz) | |
249 | { | |
250 | int i, eacl_count; | |
251 | acl_ea_header *f; | |
252 | ext4_acl_entry *e; | |
253 | acl_ea_entry *a; | |
254 | size_t f_sz; | |
4e222d9b | 255 | unsigned char *hptr; |
81cbf1ef DW |
256 | errcode_t err; |
257 | ||
258 | eacl_count = ext4_acl_count(eacl_sz); | |
259 | f_sz = acl_ea_size(eacl_count); | |
260 | if (eacl_count < 0 || | |
261 | eacl->a_version != ext2fs_cpu_to_le32(EXT4_ACL_VERSION)) | |
262 | return EXT2_ET_INVALID_ARGUMENT; | |
263 | ||
264 | err = ext2fs_get_mem(f_sz, &f); | |
265 | if (err) | |
266 | return err; | |
267 | ||
268 | f->a_version = ACL_EA_VERSION; | |
4e222d9b | 269 | hptr = (unsigned char *) (eacl + 1); |
81cbf1ef | 270 | for (i = 0, a = f->a_entries; i < eacl_count; i++, a++) { |
4e222d9b | 271 | e = (ext4_acl_entry *) hptr; |
81cbf1ef DW |
272 | a->e_tag = ext2fs_le16_to_cpu(e->e_tag); |
273 | a->e_perm = ext2fs_le16_to_cpu(e->e_perm); | |
274 | ||
275 | switch (a->e_tag) { | |
276 | case ACL_USER: | |
277 | case ACL_GROUP: | |
278 | a->e_id = ext2fs_le32_to_cpu(e->e_id); | |
279 | hptr += sizeof(ext4_acl_entry); | |
280 | break; | |
281 | case ACL_USER_OBJ: | |
282 | case ACL_GROUP_OBJ: | |
283 | case ACL_MASK: | |
284 | case ACL_OTHER: | |
285 | hptr += sizeof(ext4_acl_entry_short); | |
286 | break; | |
287 | default: | |
288 | err = EXT2_ET_INVALID_ARGUMENT; | |
289 | goto out; | |
290 | } | |
291 | } | |
292 | ||
293 | *facl = f; | |
294 | *facl_sz = f_sz; | |
295 | return err; | |
296 | out: | |
297 | ext2fs_free_mem(&f); | |
298 | return err; | |
299 | } | |
300 | #endif /* TRANSLATE_LINUX_ACLS */ | |
301 | ||
302 | /* | |
303 | * ext2_file_t contains a struct inode, so we can't leave files open. | |
304 | * Use this as a proxy instead. | |
305 | */ | |
306 | #define FUSE2FS_FILE_MAGIC (0xEF53DEAFUL) | |
307 | struct fuse2fs_file_handle { | |
308 | unsigned long magic; | |
309 | ext2_ino_t ino; | |
310 | int open_flags; | |
311 | }; | |
312 | ||
313 | /* Main program context */ | |
314 | #define FUSE2FS_MAGIC (0xEF53DEADUL) | |
315 | struct fuse2fs { | |
316 | unsigned long magic; | |
317 | ext2_filsys fs; | |
318 | pthread_mutex_t bfl; | |
69df8496 TT |
319 | char *device; |
320 | int ro; | |
321 | int debug; | |
322 | int no_default_opts; | |
81cbf1ef DW |
323 | int panic_on_error; |
324 | int minixdf; | |
ea1a3fa2 | 325 | int fakeroot; |
81cbf1ef | 326 | int alloc_all_blocks; |
75e3a9ef | 327 | int norecovery; |
81cbf1ef DW |
328 | FILE *err_fp; |
329 | unsigned int next_generation; | |
330 | }; | |
331 | ||
332 | #define FUSE2FS_CHECK_MAGIC(fs, ptr, num) do {if ((ptr)->magic != (num)) \ | |
333 | return translate_error((fs), 0, EXT2_ET_MAGIC_EXT2_FILE); \ | |
334 | } while (0) | |
335 | ||
336 | #define FUSE2FS_CHECK_CONTEXT(ptr) do {if ((ptr)->magic != FUSE2FS_MAGIC) \ | |
337 | return translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC); \ | |
338 | } while (0) | |
339 | ||
340 | static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino, | |
341 | const char *file, int line); | |
342 | #define translate_error(fs, ino, err) __translate_error((fs), (err), (ino), \ | |
343 | __FILE__, __LINE__) | |
344 | ||
345 | /* for macosx */ | |
346 | #ifndef W_OK | |
347 | # define W_OK 2 | |
348 | #endif | |
349 | ||
350 | #ifndef R_OK | |
351 | # define R_OK 4 | |
352 | #endif | |
353 | ||
354 | #define EXT4_EPOCH_BITS 2 | |
355 | #define EXT4_EPOCH_MASK ((1 << EXT4_EPOCH_BITS) - 1) | |
356 | #define EXT4_NSEC_MASK (~0UL << EXT4_EPOCH_BITS) | |
357 | ||
358 | /* | |
359 | * Extended fields will fit into an inode if the filesystem was formatted | |
360 | * with large inodes (-I 256 or larger) and there are not currently any EAs | |
361 | * consuming all of the available space. For new inodes we always reserve | |
362 | * enough space for the kernel's known extended fields, but for inodes | |
363 | * created with an old kernel this might not have been the case. None of | |
364 | * the extended inode fields is critical for correct filesystem operation. | |
365 | * This macro checks if a certain field fits in the inode. Note that | |
366 | * inode-size = GOOD_OLD_INODE_SIZE + i_extra_isize | |
367 | */ | |
368 | #define EXT4_FITS_IN_INODE(ext4_inode, field) \ | |
369 | ((offsetof(typeof(*ext4_inode), field) + \ | |
370 | sizeof((ext4_inode)->field)) \ | |
4e222d9b | 371 | <= ((size_t) EXT2_GOOD_OLD_INODE_SIZE + \ |
81cbf1ef DW |
372 | (ext4_inode)->i_extra_isize)) \ |
373 | ||
374 | static inline __u32 ext4_encode_extra_time(const struct timespec *time) | |
375 | { | |
376 | __u32 extra = sizeof(time->tv_sec) > 4 ? | |
377 | ((time->tv_sec - (__s32)time->tv_sec) >> 32) & | |
378 | EXT4_EPOCH_MASK : 0; | |
379 | return extra | (time->tv_nsec << EXT4_EPOCH_BITS); | |
380 | } | |
381 | ||
382 | static inline void ext4_decode_extra_time(struct timespec *time, __u32 extra) | |
383 | { | |
384 | if (sizeof(time->tv_sec) > 4 && (extra & EXT4_EPOCH_MASK)) { | |
385 | __u64 extra_bits = extra & EXT4_EPOCH_MASK; | |
386 | /* | |
387 | * Prior to kernel 3.14?, we had a broken decode function, | |
388 | * wherein we effectively did this: | |
389 | * if (extra_bits == 3) | |
390 | * extra_bits = 0; | |
391 | */ | |
392 | time->tv_sec += extra_bits << 32; | |
393 | } | |
394 | time->tv_nsec = ((extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS; | |
395 | } | |
396 | ||
397 | #define EXT4_INODE_SET_XTIME(xtime, timespec, raw_inode) \ | |
398 | do { \ | |
399 | (raw_inode)->xtime = (timespec)->tv_sec; \ | |
400 | if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \ | |
401 | (raw_inode)->xtime ## _extra = \ | |
402 | ext4_encode_extra_time(timespec); \ | |
403 | } while (0) | |
404 | ||
405 | #define EXT4_EINODE_SET_XTIME(xtime, timespec, raw_inode) \ | |
406 | do { \ | |
407 | if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \ | |
408 | (raw_inode)->xtime = (timespec)->tv_sec; \ | |
409 | if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \ | |
410 | (raw_inode)->xtime ## _extra = \ | |
411 | ext4_encode_extra_time(timespec); \ | |
412 | } while (0) | |
413 | ||
414 | #define EXT4_INODE_GET_XTIME(xtime, timespec, raw_inode) \ | |
415 | do { \ | |
416 | (timespec)->tv_sec = (signed)((raw_inode)->xtime); \ | |
417 | if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \ | |
418 | ext4_decode_extra_time((timespec), \ | |
419 | (raw_inode)->xtime ## _extra); \ | |
420 | else \ | |
421 | (timespec)->tv_nsec = 0; \ | |
422 | } while (0) | |
423 | ||
424 | #define EXT4_EINODE_GET_XTIME(xtime, timespec, raw_inode) \ | |
425 | do { \ | |
426 | if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \ | |
427 | (timespec)->tv_sec = \ | |
428 | (signed)((raw_inode)->xtime); \ | |
429 | if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \ | |
430 | ext4_decode_extra_time((timespec), \ | |
431 | raw_inode->xtime ## _extra); \ | |
432 | else \ | |
433 | (timespec)->tv_nsec = 0; \ | |
434 | } while (0) | |
435 | ||
436 | static void get_now(struct timespec *now) | |
437 | { | |
438 | #ifdef CLOCK_REALTIME | |
439 | if (!clock_gettime(CLOCK_REALTIME, now)) | |
440 | return; | |
441 | #endif | |
442 | ||
443 | now->tv_sec = time(NULL); | |
444 | now->tv_nsec = 0; | |
445 | } | |
446 | ||
447 | static void increment_version(struct ext2_inode_large *inode) | |
448 | { | |
449 | __u64 ver; | |
450 | ||
451 | ver = inode->osd1.linux1.l_i_version; | |
452 | if (EXT4_FITS_IN_INODE(inode, i_version_hi)) | |
453 | ver |= (__u64)inode->i_version_hi << 32; | |
454 | ver++; | |
455 | inode->osd1.linux1.l_i_version = ver; | |
456 | if (EXT4_FITS_IN_INODE(inode, i_version_hi)) | |
457 | inode->i_version_hi = ver >> 32; | |
458 | } | |
459 | ||
460 | static void init_times(struct ext2_inode_large *inode) | |
461 | { | |
462 | struct timespec now; | |
463 | ||
464 | get_now(&now); | |
465 | EXT4_INODE_SET_XTIME(i_atime, &now, inode); | |
466 | EXT4_INODE_SET_XTIME(i_ctime, &now, inode); | |
467 | EXT4_INODE_SET_XTIME(i_mtime, &now, inode); | |
468 | EXT4_EINODE_SET_XTIME(i_crtime, &now, inode); | |
469 | increment_version(inode); | |
470 | } | |
471 | ||
472 | static int update_ctime(ext2_filsys fs, ext2_ino_t ino, | |
473 | struct ext2_inode_large *pinode) | |
474 | { | |
475 | errcode_t err; | |
476 | struct timespec now; | |
477 | struct ext2_inode_large inode; | |
478 | ||
479 | get_now(&now); | |
480 | ||
481 | /* If user already has a inode buffer, just update that */ | |
482 | if (pinode) { | |
483 | increment_version(pinode); | |
484 | EXT4_INODE_SET_XTIME(i_ctime, &now, pinode); | |
485 | return 0; | |
486 | } | |
487 | ||
488 | /* Otherwise we have to read-modify-write the inode */ | |
489 | memset(&inode, 0, sizeof(inode)); | |
490 | err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
491 | sizeof(inode)); | |
492 | if (err) | |
493 | return translate_error(fs, ino, err); | |
494 | ||
495 | increment_version(&inode); | |
496 | EXT4_INODE_SET_XTIME(i_ctime, &now, &inode); | |
497 | ||
498 | err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
499 | sizeof(inode)); | |
500 | if (err) | |
501 | return translate_error(fs, ino, err); | |
502 | ||
503 | return 0; | |
504 | } | |
505 | ||
506 | static int update_atime(ext2_filsys fs, ext2_ino_t ino) | |
507 | { | |
508 | errcode_t err; | |
509 | struct ext2_inode_large inode, *pinode; | |
510 | struct timespec atime, mtime, now; | |
511 | ||
512 | if (!(fs->flags & EXT2_FLAG_RW)) | |
513 | return 0; | |
514 | memset(&inode, 0, sizeof(inode)); | |
515 | err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
516 | sizeof(inode)); | |
517 | if (err) | |
518 | return translate_error(fs, ino, err); | |
519 | ||
520 | pinode = &inode; | |
521 | EXT4_INODE_GET_XTIME(i_atime, &atime, pinode); | |
522 | EXT4_INODE_GET_XTIME(i_mtime, &mtime, pinode); | |
523 | get_now(&now); | |
524 | /* | |
525 | * If atime is newer than mtime and atime hasn't been updated in thirty | |
526 | * seconds, skip the atime update. Same idea as Linux "relatime". | |
527 | */ | |
528 | if (atime.tv_sec >= mtime.tv_sec && atime.tv_sec >= now.tv_sec - 30) | |
529 | return 0; | |
530 | EXT4_INODE_SET_XTIME(i_atime, &now, &inode); | |
531 | ||
532 | err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
533 | sizeof(inode)); | |
534 | if (err) | |
535 | return translate_error(fs, ino, err); | |
536 | ||
537 | return 0; | |
538 | } | |
539 | ||
540 | static int update_mtime(ext2_filsys fs, ext2_ino_t ino, | |
541 | struct ext2_inode_large *pinode) | |
542 | { | |
543 | errcode_t err; | |
544 | struct ext2_inode_large inode; | |
545 | struct timespec now; | |
546 | ||
547 | if (pinode) { | |
548 | get_now(&now); | |
549 | EXT4_INODE_SET_XTIME(i_mtime, &now, pinode); | |
550 | EXT4_INODE_SET_XTIME(i_ctime, &now, pinode); | |
551 | increment_version(pinode); | |
552 | return 0; | |
553 | } | |
554 | ||
555 | memset(&inode, 0, sizeof(inode)); | |
556 | err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
557 | sizeof(inode)); | |
558 | if (err) | |
559 | return translate_error(fs, ino, err); | |
560 | ||
561 | get_now(&now); | |
562 | EXT4_INODE_SET_XTIME(i_mtime, &now, &inode); | |
563 | EXT4_INODE_SET_XTIME(i_ctime, &now, &inode); | |
564 | increment_version(&inode); | |
565 | ||
566 | err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
567 | sizeof(inode)); | |
568 | if (err) | |
569 | return translate_error(fs, ino, err); | |
570 | ||
571 | return 0; | |
572 | } | |
573 | ||
574 | static int ext2_file_type(unsigned int mode) | |
575 | { | |
576 | if (LINUX_S_ISREG(mode)) | |
577 | return EXT2_FT_REG_FILE; | |
578 | ||
579 | if (LINUX_S_ISDIR(mode)) | |
580 | return EXT2_FT_DIR; | |
581 | ||
582 | if (LINUX_S_ISCHR(mode)) | |
583 | return EXT2_FT_CHRDEV; | |
584 | ||
585 | if (LINUX_S_ISBLK(mode)) | |
586 | return EXT2_FT_BLKDEV; | |
587 | ||
588 | if (LINUX_S_ISLNK(mode)) | |
589 | return EXT2_FT_SYMLINK; | |
590 | ||
591 | if (LINUX_S_ISFIFO(mode)) | |
592 | return EXT2_FT_FIFO; | |
593 | ||
594 | if (LINUX_S_ISSOCK(mode)) | |
595 | return EXT2_FT_SOCK; | |
596 | ||
597 | return 0; | |
598 | } | |
599 | ||
600 | static int fs_can_allocate(struct fuse2fs *ff, blk64_t num) | |
601 | { | |
602 | ext2_filsys fs = ff->fs; | |
603 | blk64_t reserved; | |
604 | ||
605 | dbg_printf("%s: Asking for %llu; alloc_all=%d total=%llu free=%llu " | |
606 | "rsvd=%llu\n", __func__, num, ff->alloc_all_blocks, | |
607 | ext2fs_blocks_count(fs->super), | |
608 | ext2fs_free_blocks_count(fs->super), | |
609 | ext2fs_r_blocks_count(fs->super)); | |
610 | if (num > ext2fs_blocks_count(fs->super)) | |
611 | return 0; | |
612 | ||
613 | if (ff->alloc_all_blocks) | |
614 | return 1; | |
615 | ||
616 | /* | |
617 | * Different meaning for r_blocks -- libext2fs has bugs where the FS | |
618 | * can get corrupted if it totally runs out of blocks. Avoid this | |
619 | * by refusing to allocate any of the reserve blocks to anybody. | |
620 | */ | |
621 | reserved = ext2fs_r_blocks_count(fs->super); | |
622 | if (reserved == 0) | |
623 | reserved = ext2fs_blocks_count(fs->super) / 10; | |
624 | return ext2fs_free_blocks_count(fs->super) > reserved + num; | |
625 | } | |
626 | ||
627 | static int fs_writeable(ext2_filsys fs) | |
628 | { | |
629 | return (fs->flags & EXT2_FLAG_RW) && (fs->super->s_error_count == 0); | |
630 | } | |
631 | ||
4e222d9b | 632 | static int check_inum_access(ext2_filsys fs, ext2_ino_t ino, mode_t mask) |
81cbf1ef DW |
633 | { |
634 | struct fuse_context *ctxt = fuse_get_context(); | |
ea1a3fa2 | 635 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; |
81cbf1ef DW |
636 | struct ext2_inode inode; |
637 | mode_t perms; | |
638 | errcode_t err; | |
639 | ||
640 | /* no writing to read-only or broken fs */ | |
641 | if ((mask & W_OK) && !fs_writeable(fs)) | |
642 | return -EROFS; | |
643 | ||
644 | err = ext2fs_read_inode(fs, ino, &inode); | |
645 | if (err) | |
646 | return translate_error(fs, ino, err); | |
647 | perms = inode.i_mode & 0777; | |
648 | ||
649 | dbg_printf("access ino=%d mask=e%s%s%s perms=0%o fuid=%d fgid=%d " | |
650 | "uid=%d gid=%d\n", ino, | |
651 | (mask & R_OK ? "r" : ""), (mask & W_OK ? "w" : ""), | |
dae1ecc2 TT |
652 | (mask & X_OK ? "x" : ""), perms, inode_uid(inode), |
653 | inode_gid(inode), ctxt->uid, ctxt->gid); | |
81cbf1ef DW |
654 | |
655 | /* existence check */ | |
656 | if (mask == 0) | |
657 | return 0; | |
658 | ||
659 | /* is immutable? */ | |
660 | if ((mask & W_OK) && | |
661 | (inode.i_flags & EXT2_IMMUTABLE_FL)) | |
662 | return -EACCES; | |
663 | ||
664 | /* Figure out what root's allowed to do */ | |
ea1a3fa2 | 665 | if (ff->fakeroot || ctxt->uid == 0) { |
81cbf1ef DW |
666 | /* Non-file access always ok */ |
667 | if (!LINUX_S_ISREG(inode.i_mode)) | |
668 | return 0; | |
669 | ||
670 | /* R/W access to a file always ok */ | |
671 | if (!(mask & X_OK)) | |
672 | return 0; | |
673 | ||
674 | /* X access to a file ok if a user/group/other can X */ | |
675 | if (perms & 0111) | |
676 | return 0; | |
677 | ||
678 | /* Trying to execute a file that's not executable. BZZT! */ | |
679 | return -EACCES; | |
680 | } | |
681 | ||
682 | /* allow owner, if perms match */ | |
dae1ecc2 | 683 | if (inode_uid(inode) == ctxt->uid) { |
81cbf1ef DW |
684 | if ((mask & (perms >> 6)) == mask) |
685 | return 0; | |
686 | return -EACCES; | |
687 | } | |
688 | ||
689 | /* allow group, if perms match */ | |
dae1ecc2 | 690 | if (inode_gid(inode) == ctxt->gid) { |
81cbf1ef DW |
691 | if ((mask & (perms >> 3)) == mask) |
692 | return 0; | |
693 | return -EACCES; | |
694 | } | |
695 | ||
696 | /* otherwise check other */ | |
697 | if ((mask & perms) == mask) | |
698 | return 0; | |
699 | return -EACCES; | |
700 | } | |
701 | ||
4e222d9b | 702 | static void op_destroy(void *p EXT2FS_ATTR((unused))) |
81cbf1ef DW |
703 | { |
704 | struct fuse_context *ctxt = fuse_get_context(); | |
705 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
706 | ext2_filsys fs; | |
707 | errcode_t err; | |
708 | ||
709 | if (ff->magic != FUSE2FS_MAGIC) { | |
710 | translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC); | |
711 | return; | |
712 | } | |
713 | fs = ff->fs; | |
714 | dbg_printf("%s: dev=%s\n", __func__, fs->device_name); | |
715 | if (fs->flags & EXT2_FLAG_RW) { | |
716 | fs->super->s_state |= EXT2_VALID_FS; | |
717 | if (fs->super->s_error_count) | |
718 | fs->super->s_state |= EXT2_ERROR_FS; | |
719 | ext2fs_mark_super_dirty(fs); | |
720 | err = ext2fs_set_gdt_csum(fs); | |
721 | if (err) | |
722 | translate_error(fs, 0, err); | |
723 | ||
724 | err = ext2fs_flush2(fs, 0); | |
725 | if (err) | |
726 | translate_error(fs, 0, err); | |
727 | } | |
728 | } | |
729 | ||
730 | static void *op_init(struct fuse_conn_info *conn) | |
731 | { | |
732 | struct fuse_context *ctxt = fuse_get_context(); | |
733 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
734 | ext2_filsys fs; | |
735 | errcode_t err; | |
736 | ||
737 | if (ff->magic != FUSE2FS_MAGIC) { | |
738 | translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC); | |
739 | return NULL; | |
740 | } | |
741 | fs = ff->fs; | |
742 | dbg_printf("%s: dev=%s\n", __func__, fs->device_name); | |
743 | #ifdef FUSE_CAP_IOCTL_DIR | |
744 | conn->want |= FUSE_CAP_IOCTL_DIR; | |
745 | #endif | |
746 | if (fs->flags & EXT2_FLAG_RW) { | |
747 | fs->super->s_mnt_count++; | |
748 | fs->super->s_mtime = time(NULL); | |
749 | fs->super->s_state &= ~EXT2_VALID_FS; | |
750 | ext2fs_mark_super_dirty(fs); | |
751 | err = ext2fs_flush2(fs, 0); | |
752 | if (err) | |
753 | translate_error(fs, 0, err); | |
754 | } | |
755 | return ff; | |
756 | } | |
757 | ||
81cbf1ef DW |
758 | static int stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *statbuf) |
759 | { | |
760 | struct ext2_inode_large inode; | |
761 | dev_t fakedev = 0; | |
762 | errcode_t err; | |
763 | int ret = 0; | |
764 | struct timespec tv; | |
765 | ||
766 | memset(&inode, 0, sizeof(inode)); | |
767 | err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
768 | sizeof(inode)); | |
769 | if (err) | |
770 | return translate_error(fs, ino, err); | |
771 | ||
772 | memcpy(&fakedev, fs->super->s_uuid, sizeof(fakedev)); | |
773 | statbuf->st_dev = fakedev; | |
774 | statbuf->st_ino = ino; | |
775 | statbuf->st_mode = inode.i_mode; | |
776 | statbuf->st_nlink = inode.i_links_count; | |
dae1ecc2 TT |
777 | statbuf->st_uid = inode_uid(inode); |
778 | statbuf->st_gid = inode_gid(inode); | |
81cbf1ef DW |
779 | statbuf->st_size = EXT2_I_SIZE(&inode); |
780 | statbuf->st_blksize = fs->blocksize; | |
3a15d85b TT |
781 | statbuf->st_blocks = ext2fs_get_stat_i_blocks(fs, |
782 | (struct ext2_inode *)&inode); | |
81cbf1ef DW |
783 | EXT4_INODE_GET_XTIME(i_atime, &tv, &inode); |
784 | statbuf->st_atime = tv.tv_sec; | |
785 | EXT4_INODE_GET_XTIME(i_mtime, &tv, &inode); | |
786 | statbuf->st_mtime = tv.tv_sec; | |
787 | EXT4_INODE_GET_XTIME(i_ctime, &tv, &inode); | |
788 | statbuf->st_ctime = tv.tv_sec; | |
789 | if (LINUX_S_ISCHR(inode.i_mode) || | |
790 | LINUX_S_ISBLK(inode.i_mode)) { | |
791 | if (inode.i_block[0]) | |
792 | statbuf->st_rdev = inode.i_block[0]; | |
793 | else | |
794 | statbuf->st_rdev = inode.i_block[1]; | |
795 | } | |
796 | ||
797 | return ret; | |
798 | } | |
799 | ||
800 | static int op_getattr(const char *path, struct stat *statbuf) | |
801 | { | |
802 | struct fuse_context *ctxt = fuse_get_context(); | |
803 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
804 | ext2_filsys fs; | |
805 | ext2_ino_t ino; | |
806 | errcode_t err; | |
807 | int ret = 0; | |
808 | ||
809 | FUSE2FS_CHECK_CONTEXT(ff); | |
810 | fs = ff->fs; | |
811 | dbg_printf("%s: path=%s\n", __func__, path); | |
812 | pthread_mutex_lock(&ff->bfl); | |
813 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
814 | if (err) { | |
815 | ret = translate_error(fs, 0, err); | |
816 | goto out; | |
817 | } | |
818 | ret = stat_inode(fs, ino, statbuf); | |
819 | out: | |
820 | pthread_mutex_unlock(&ff->bfl); | |
821 | return ret; | |
822 | } | |
823 | ||
824 | static int op_readlink(const char *path, char *buf, size_t len) | |
825 | { | |
826 | struct fuse_context *ctxt = fuse_get_context(); | |
827 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
828 | ext2_filsys fs; | |
829 | errcode_t err; | |
830 | ext2_ino_t ino; | |
831 | struct ext2_inode inode; | |
832 | unsigned int got; | |
833 | ext2_file_t file; | |
834 | int ret = 0; | |
835 | ||
836 | FUSE2FS_CHECK_CONTEXT(ff); | |
837 | fs = ff->fs; | |
838 | dbg_printf("%s: path=%s\n", __func__, path); | |
839 | pthread_mutex_lock(&ff->bfl); | |
840 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
841 | if (err || ino == 0) { | |
842 | ret = translate_error(fs, 0, err); | |
843 | goto out; | |
844 | } | |
845 | ||
846 | err = ext2fs_read_inode(fs, ino, &inode); | |
847 | if (err) { | |
848 | ret = translate_error(fs, ino, err); | |
849 | goto out; | |
850 | } | |
851 | ||
852 | if (!LINUX_S_ISLNK(inode.i_mode)) { | |
853 | ret = -EINVAL; | |
854 | goto out; | |
855 | } | |
856 | ||
857 | len--; | |
858 | if (inode.i_size < len) | |
859 | len = inode.i_size; | |
cf0be234 TE |
860 | if (ext2fs_is_fast_symlink(&inode)) |
861 | memcpy(buf, (char *)inode.i_block, len); | |
862 | else { | |
81cbf1ef DW |
863 | /* big/inline symlink */ |
864 | ||
865 | err = ext2fs_file_open(fs, ino, 0, &file); | |
866 | if (err) { | |
867 | ret = translate_error(fs, ino, err); | |
868 | goto out; | |
869 | } | |
870 | ||
871 | err = ext2fs_file_read(file, buf, len, &got); | |
872 | if (err || got != len) { | |
873 | ext2fs_file_close(file); | |
874 | ret = translate_error(fs, ino, err); | |
875 | goto out2; | |
876 | } | |
877 | ||
878 | out2: | |
879 | err = ext2fs_file_close(file); | |
880 | if (ret) | |
881 | goto out; | |
882 | if (err) { | |
883 | ret = translate_error(fs, ino, err); | |
884 | goto out; | |
885 | } | |
cf0be234 | 886 | } |
81cbf1ef DW |
887 | buf[len] = 0; |
888 | ||
889 | if (fs_writeable(fs)) { | |
890 | ret = update_atime(fs, ino); | |
891 | if (ret) | |
892 | goto out; | |
893 | } | |
894 | ||
895 | out: | |
896 | pthread_mutex_unlock(&ff->bfl); | |
897 | return ret; | |
898 | } | |
899 | ||
900 | static int op_mknod(const char *path, mode_t mode, dev_t dev) | |
901 | { | |
902 | struct fuse_context *ctxt = fuse_get_context(); | |
903 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
904 | ext2_filsys fs; | |
905 | ext2_ino_t parent, child; | |
da9d5b80 | 906 | char *temp_path; |
81cbf1ef DW |
907 | errcode_t err; |
908 | char *node_name, a; | |
909 | int filetype; | |
910 | struct ext2_inode_large inode; | |
911 | int ret = 0; | |
912 | ||
913 | FUSE2FS_CHECK_CONTEXT(ff); | |
914 | fs = ff->fs; | |
915 | dbg_printf("%s: path=%s mode=0%o dev=0x%x\n", __func__, path, mode, | |
916 | (unsigned int)dev); | |
da9d5b80 | 917 | temp_path = strdup(path); |
81cbf1ef DW |
918 | if (!temp_path) { |
919 | ret = -ENOMEM; | |
920 | goto out; | |
921 | } | |
922 | node_name = strrchr(temp_path, '/'); | |
923 | if (!node_name) { | |
924 | ret = -ENOMEM; | |
925 | goto out; | |
926 | } | |
927 | node_name++; | |
928 | a = *node_name; | |
929 | *node_name = 0; | |
930 | ||
931 | pthread_mutex_lock(&ff->bfl); | |
932 | if (!fs_can_allocate(ff, 2)) { | |
933 | ret = -ENOSPC; | |
934 | goto out2; | |
935 | } | |
936 | ||
937 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path, | |
938 | &parent); | |
939 | if (err) { | |
940 | ret = translate_error(fs, 0, err); | |
941 | goto out2; | |
942 | } | |
943 | ||
944 | ret = check_inum_access(fs, parent, W_OK); | |
945 | if (ret) | |
946 | goto out2; | |
947 | ||
948 | *node_name = a; | |
949 | ||
950 | if (LINUX_S_ISCHR(mode)) | |
951 | filetype = EXT2_FT_CHRDEV; | |
952 | else if (LINUX_S_ISBLK(mode)) | |
953 | filetype = EXT2_FT_BLKDEV; | |
954 | else if (LINUX_S_ISFIFO(mode)) | |
955 | filetype = EXT2_FT_FIFO; | |
956 | else if (LINUX_S_ISSOCK(mode)) | |
957 | filetype = EXT2_FT_SOCK; | |
958 | else { | |
959 | ret = -EINVAL; | |
960 | goto out2; | |
961 | } | |
962 | ||
963 | err = ext2fs_new_inode(fs, parent, mode, 0, &child); | |
964 | if (err) { | |
965 | ret = translate_error(fs, 0, err); | |
966 | goto out2; | |
967 | } | |
968 | ||
969 | dbg_printf("%s: create ino=%d/name=%s in dir=%d\n", __func__, child, | |
970 | node_name, parent); | |
971 | err = ext2fs_link(fs, parent, node_name, child, filetype); | |
972 | if (err == EXT2_ET_DIR_NO_SPACE) { | |
973 | err = ext2fs_expand_dir(fs, parent); | |
974 | if (err) { | |
975 | ret = translate_error(fs, parent, err); | |
976 | goto out2; | |
977 | } | |
978 | ||
979 | err = ext2fs_link(fs, parent, node_name, child, | |
980 | filetype); | |
981 | } | |
982 | if (err) { | |
983 | ret = translate_error(fs, parent, err); | |
984 | goto out2; | |
985 | } | |
986 | ||
987 | ret = update_mtime(fs, parent, NULL); | |
988 | if (ret) | |
989 | goto out2; | |
990 | ||
991 | memset(&inode, 0, sizeof(inode)); | |
992 | inode.i_mode = mode; | |
993 | ||
994 | if (dev & ~0xFFFF) | |
995 | inode.i_block[1] = dev; | |
996 | else | |
997 | inode.i_block[0] = dev; | |
998 | inode.i_links_count = 1; | |
999 | inode.i_extra_isize = sizeof(struct ext2_inode_large) - | |
1000 | EXT2_GOOD_OLD_INODE_SIZE; | |
f150bdec | 1001 | inode.i_uid = ctxt->uid; |
dae1ecc2 | 1002 | ext2fs_set_i_uid_high(inode, ctxt->uid >> 16); |
f150bdec | 1003 | inode.i_gid = ctxt->gid; |
dae1ecc2 | 1004 | ext2fs_set_i_gid_high(inode, ctxt->gid >> 16); |
81cbf1ef DW |
1005 | |
1006 | err = ext2fs_write_new_inode(fs, child, (struct ext2_inode *)&inode); | |
1007 | if (err) { | |
1008 | ret = translate_error(fs, child, err); | |
1009 | goto out2; | |
1010 | } | |
1011 | ||
1012 | inode.i_generation = ff->next_generation++; | |
1013 | init_times(&inode); | |
1014 | err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode, | |
1015 | sizeof(inode)); | |
1016 | if (err) { | |
1017 | ret = translate_error(fs, child, err); | |
1018 | goto out2; | |
1019 | } | |
1020 | ||
1021 | ext2fs_inode_alloc_stats2(fs, child, 1, 0); | |
1022 | ||
1023 | out2: | |
1024 | pthread_mutex_unlock(&ff->bfl); | |
1025 | out: | |
1026 | free(temp_path); | |
1027 | return ret; | |
1028 | } | |
1029 | ||
1030 | static int op_mkdir(const char *path, mode_t mode) | |
1031 | { | |
1032 | struct fuse_context *ctxt = fuse_get_context(); | |
1033 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
1034 | ext2_filsys fs; | |
1035 | ext2_ino_t parent, child; | |
da9d5b80 | 1036 | char *temp_path; |
81cbf1ef DW |
1037 | errcode_t err; |
1038 | char *node_name, a; | |
1039 | struct ext2_inode_large inode; | |
1040 | char *block; | |
1041 | blk64_t blk; | |
1042 | int ret = 0; | |
1043 | mode_t parent_sgid; | |
1044 | ||
1045 | FUSE2FS_CHECK_CONTEXT(ff); | |
1046 | fs = ff->fs; | |
1047 | dbg_printf("%s: path=%s mode=0%o\n", __func__, path, mode); | |
da9d5b80 | 1048 | temp_path = strdup(path); |
81cbf1ef DW |
1049 | if (!temp_path) { |
1050 | ret = -ENOMEM; | |
1051 | goto out; | |
1052 | } | |
1053 | node_name = strrchr(temp_path, '/'); | |
1054 | if (!node_name) { | |
1055 | ret = -ENOMEM; | |
1056 | goto out; | |
1057 | } | |
1058 | node_name++; | |
1059 | a = *node_name; | |
1060 | *node_name = 0; | |
1061 | ||
1062 | pthread_mutex_lock(&ff->bfl); | |
1063 | if (!fs_can_allocate(ff, 1)) { | |
1064 | ret = -ENOSPC; | |
1065 | goto out2; | |
1066 | } | |
1067 | ||
1068 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path, | |
1069 | &parent); | |
1070 | if (err) { | |
1071 | ret = translate_error(fs, 0, err); | |
1072 | goto out2; | |
1073 | } | |
1074 | ||
1075 | ret = check_inum_access(fs, parent, W_OK); | |
1076 | if (ret) | |
1077 | goto out2; | |
1078 | ||
1079 | /* Is the parent dir sgid? */ | |
1080 | err = ext2fs_read_inode_full(fs, parent, (struct ext2_inode *)&inode, | |
1081 | sizeof(inode)); | |
1082 | if (err) { | |
1083 | ret = translate_error(fs, parent, err); | |
1084 | goto out2; | |
1085 | } | |
1086 | parent_sgid = inode.i_mode & S_ISGID; | |
1087 | ||
1088 | *node_name = a; | |
1089 | ||
1090 | err = ext2fs_mkdir(fs, parent, 0, node_name); | |
1091 | if (err == EXT2_ET_DIR_NO_SPACE) { | |
1092 | err = ext2fs_expand_dir(fs, parent); | |
1093 | if (err) { | |
1094 | ret = translate_error(fs, parent, err); | |
1095 | goto out2; | |
1096 | } | |
1097 | ||
1098 | err = ext2fs_mkdir(fs, parent, 0, node_name); | |
1099 | } | |
1100 | if (err) { | |
1101 | ret = translate_error(fs, parent, err); | |
1102 | goto out2; | |
1103 | } | |
1104 | ||
1105 | ret = update_mtime(fs, parent, NULL); | |
1106 | if (ret) | |
1107 | goto out2; | |
1108 | ||
1109 | /* Still have to update the uid/gid of the dir */ | |
1110 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path, | |
1111 | &child); | |
1112 | if (err) { | |
1113 | ret = translate_error(fs, 0, err); | |
1114 | goto out2; | |
1115 | } | |
1116 | dbg_printf("%s: created ino=%d/path=%s in dir=%d\n", __func__, child, | |
1117 | node_name, parent); | |
1118 | ||
1119 | memset(&inode, 0, sizeof(inode)); | |
1120 | err = ext2fs_read_inode_full(fs, child, (struct ext2_inode *)&inode, | |
1121 | sizeof(inode)); | |
1122 | if (err) { | |
1123 | ret = translate_error(fs, child, err); | |
1124 | goto out2; | |
1125 | } | |
1126 | ||
1127 | inode.i_uid = ctxt->uid; | |
dae1ecc2 | 1128 | ext2fs_set_i_uid_high(inode, ctxt->uid >> 16); |
81cbf1ef | 1129 | inode.i_gid = ctxt->gid; |
dae1ecc2 | 1130 | ext2fs_set_i_gid_high(inode, ctxt->gid >> 16); |
81cbf1ef DW |
1131 | inode.i_mode = LINUX_S_IFDIR | (mode & ~(S_ISUID | fs->umask)) | |
1132 | parent_sgid; | |
1133 | inode.i_generation = ff->next_generation++; | |
1134 | ||
1135 | err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode, | |
1136 | sizeof(inode)); | |
1137 | if (err) { | |
1138 | ret = translate_error(fs, child, err); | |
1139 | goto out2; | |
1140 | } | |
1141 | ||
1142 | /* Rewrite the directory block checksum, having set i_generation */ | |
1143 | if ((inode.i_flags & EXT4_INLINE_DATA_FL) || | |
7889640d | 1144 | !ext2fs_has_feature_metadata_csum(fs->super)) |
81cbf1ef DW |
1145 | goto out2; |
1146 | err = ext2fs_new_dir_block(fs, child, parent, &block); | |
1147 | if (err) { | |
1148 | ret = translate_error(fs, child, err); | |
1149 | goto out2; | |
1150 | } | |
1151 | err = ext2fs_bmap2(fs, child, (struct ext2_inode *)&inode, NULL, 0, 0, | |
1152 | NULL, &blk); | |
1153 | if (err) { | |
1154 | ret = translate_error(fs, child, err); | |
1155 | goto out3; | |
1156 | } | |
1157 | err = ext2fs_write_dir_block4(fs, blk, block, 0, child); | |
1158 | if (err) { | |
1159 | ret = translate_error(fs, child, err); | |
1160 | goto out3; | |
1161 | } | |
1162 | ||
1163 | out3: | |
1164 | ext2fs_free_mem(&block); | |
1165 | out2: | |
1166 | pthread_mutex_unlock(&ff->bfl); | |
1167 | out: | |
1168 | free(temp_path); | |
1169 | return ret; | |
1170 | } | |
1171 | ||
1172 | static int unlink_file_by_name(ext2_filsys fs, const char *path) | |
1173 | { | |
1174 | errcode_t err; | |
1175 | ext2_ino_t dir; | |
1176 | char *filename = strdup(path); | |
1177 | char *base_name; | |
1178 | int ret; | |
1179 | ||
1180 | base_name = strrchr(filename, '/'); | |
1181 | if (base_name) { | |
1182 | *base_name++ = '\0'; | |
1183 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, filename, | |
1184 | &dir); | |
1185 | if (err) { | |
1186 | free(filename); | |
1187 | return translate_error(fs, 0, err); | |
1188 | } | |
1189 | } else { | |
1190 | dir = EXT2_ROOT_INO; | |
1191 | base_name = filename; | |
1192 | } | |
1193 | ||
1194 | ret = check_inum_access(fs, dir, W_OK); | |
1195 | if (ret) { | |
1196 | free(filename); | |
1197 | return ret; | |
1198 | } | |
1199 | ||
1200 | dbg_printf("%s: unlinking name=%s from dir=%d\n", __func__, | |
1201 | base_name, dir); | |
1202 | err = ext2fs_unlink(fs, dir, base_name, 0, 0); | |
1203 | free(filename); | |
1204 | if (err) | |
1205 | return translate_error(fs, dir, err); | |
1206 | ||
1207 | return update_mtime(fs, dir, NULL); | |
1208 | } | |
1209 | ||
1210 | static int remove_inode(struct fuse2fs *ff, ext2_ino_t ino) | |
1211 | { | |
1212 | ext2_filsys fs = ff->fs; | |
1213 | errcode_t err; | |
1214 | struct ext2_inode_large inode; | |
1215 | int ret = 0; | |
1216 | ||
1217 | memset(&inode, 0, sizeof(inode)); | |
1218 | err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
1219 | sizeof(inode)); | |
1220 | if (err) { | |
1221 | ret = translate_error(fs, ino, err); | |
1222 | goto out; | |
1223 | } | |
1224 | dbg_printf("%s: put ino=%d links=%d\n", __func__, ino, | |
1225 | inode.i_links_count); | |
1226 | ||
1227 | switch (inode.i_links_count) { | |
1228 | case 0: | |
1229 | return 0; /* XXX: already done? */ | |
1230 | case 1: | |
1231 | inode.i_links_count--; | |
1232 | inode.i_dtime = fs->now ? fs->now : time(0); | |
1233 | break; | |
1234 | default: | |
1235 | inode.i_links_count--; | |
1236 | } | |
1237 | ||
1238 | ret = update_ctime(fs, ino, &inode); | |
1239 | if (ret) | |
1240 | goto out; | |
1241 | ||
1242 | if (inode.i_links_count) | |
1243 | goto write_out; | |
1244 | ||
1245 | /* Nobody holds this file; free its blocks! */ | |
1246 | err = ext2fs_free_ext_attr(fs, ino, &inode); | |
1247 | if (err) | |
1248 | goto write_out; | |
1249 | ||
1250 | if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) { | |
1251 | err = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL, | |
1252 | 0, ~0ULL); | |
1253 | if (err) { | |
1254 | ret = translate_error(fs, ino, err); | |
1255 | goto write_out; | |
1256 | } | |
1257 | } | |
1258 | ||
1259 | ext2fs_inode_alloc_stats2(fs, ino, -1, | |
1260 | LINUX_S_ISDIR(inode.i_mode)); | |
1261 | ||
1262 | write_out: | |
1263 | err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
1264 | sizeof(inode)); | |
1265 | if (err) { | |
1266 | ret = translate_error(fs, ino, err); | |
1267 | goto out; | |
1268 | } | |
1269 | out: | |
1270 | return ret; | |
1271 | } | |
1272 | ||
1273 | static int __op_unlink(struct fuse2fs *ff, const char *path) | |
1274 | { | |
1275 | ext2_filsys fs = ff->fs; | |
1276 | ext2_ino_t ino; | |
1277 | errcode_t err; | |
1278 | int ret = 0; | |
1279 | ||
1280 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
1281 | if (err) { | |
1282 | ret = translate_error(fs, 0, err); | |
1283 | goto out; | |
1284 | } | |
1285 | ||
1286 | ret = unlink_file_by_name(fs, path); | |
1287 | if (ret) | |
1288 | goto out; | |
1289 | ||
1290 | ret = remove_inode(ff, ino); | |
1291 | if (ret) | |
1292 | goto out; | |
1293 | out: | |
1294 | return ret; | |
1295 | } | |
1296 | ||
1297 | static int op_unlink(const char *path) | |
1298 | { | |
1299 | struct fuse_context *ctxt = fuse_get_context(); | |
1300 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
1301 | int ret; | |
1302 | ||
1303 | FUSE2FS_CHECK_CONTEXT(ff); | |
1304 | pthread_mutex_lock(&ff->bfl); | |
1305 | ret = __op_unlink(ff, path); | |
1306 | pthread_mutex_unlock(&ff->bfl); | |
1307 | return ret; | |
1308 | } | |
1309 | ||
1310 | struct rd_struct { | |
1311 | ext2_ino_t parent; | |
1312 | int empty; | |
1313 | }; | |
1314 | ||
1315 | static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), | |
1316 | int entry EXT2FS_ATTR((unused)), | |
1317 | struct ext2_dir_entry *dirent, | |
1318 | int offset EXT2FS_ATTR((unused)), | |
1319 | int blocksize EXT2FS_ATTR((unused)), | |
1320 | char *buf EXT2FS_ATTR((unused)), | |
1321 | void *private) | |
1322 | { | |
1323 | struct rd_struct *rds = (struct rd_struct *) private; | |
1324 | ||
1325 | if (dirent->inode == 0) | |
1326 | return 0; | |
1327 | if (((dirent->name_len & 0xFF) == 1) && (dirent->name[0] == '.')) | |
1328 | return 0; | |
1329 | if (((dirent->name_len & 0xFF) == 2) && (dirent->name[0] == '.') && | |
1330 | (dirent->name[1] == '.')) { | |
1331 | rds->parent = dirent->inode; | |
1332 | return 0; | |
1333 | } | |
1334 | rds->empty = 0; | |
1335 | return 0; | |
1336 | } | |
1337 | ||
1338 | static int __op_rmdir(struct fuse2fs *ff, const char *path) | |
1339 | { | |
1340 | ext2_filsys fs = ff->fs; | |
1341 | ext2_ino_t child; | |
1342 | errcode_t err; | |
1343 | struct ext2_inode_large inode; | |
1344 | struct rd_struct rds; | |
1345 | int ret = 0; | |
1346 | ||
1347 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &child); | |
1348 | if (err) { | |
1349 | ret = translate_error(fs, 0, err); | |
1350 | goto out; | |
1351 | } | |
1352 | dbg_printf("%s: rmdir path=%s ino=%d\n", __func__, path, child); | |
1353 | ||
1354 | rds.parent = 0; | |
1355 | rds.empty = 1; | |
1356 | ||
1357 | err = ext2fs_dir_iterate2(fs, child, 0, 0, rmdir_proc, &rds); | |
1358 | if (err) { | |
1359 | ret = translate_error(fs, child, err); | |
1360 | goto out; | |
1361 | } | |
1362 | ||
1363 | if (rds.empty == 0) { | |
1364 | ret = -ENOTEMPTY; | |
1365 | goto out; | |
1366 | } | |
1367 | ||
1368 | ret = unlink_file_by_name(fs, path); | |
1369 | if (ret) | |
1370 | goto out; | |
1371 | /* Directories have to be "removed" twice. */ | |
1372 | ret = remove_inode(ff, child); | |
1373 | if (ret) | |
1374 | goto out; | |
1375 | ret = remove_inode(ff, child); | |
1376 | if (ret) | |
1377 | goto out; | |
1378 | ||
1379 | if (rds.parent) { | |
1380 | dbg_printf("%s: decr dir=%d link count\n", __func__, | |
1381 | rds.parent); | |
1382 | err = ext2fs_read_inode_full(fs, rds.parent, | |
1383 | (struct ext2_inode *)&inode, | |
1384 | sizeof(inode)); | |
1385 | if (err) { | |
1386 | ret = translate_error(fs, rds.parent, err); | |
1387 | goto out; | |
1388 | } | |
1389 | if (inode.i_links_count > 1) | |
1390 | inode.i_links_count--; | |
1391 | ret = update_mtime(fs, rds.parent, &inode); | |
1392 | if (ret) | |
1393 | goto out; | |
1394 | err = ext2fs_write_inode_full(fs, rds.parent, | |
1395 | (struct ext2_inode *)&inode, | |
1396 | sizeof(inode)); | |
1397 | if (err) { | |
1398 | ret = translate_error(fs, rds.parent, err); | |
1399 | goto out; | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | out: | |
1404 | return ret; | |
1405 | } | |
1406 | ||
1407 | static int op_rmdir(const char *path) | |
1408 | { | |
1409 | struct fuse_context *ctxt = fuse_get_context(); | |
1410 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
1411 | int ret; | |
1412 | ||
1413 | FUSE2FS_CHECK_CONTEXT(ff); | |
1414 | pthread_mutex_lock(&ff->bfl); | |
1415 | ret = __op_rmdir(ff, path); | |
1416 | pthread_mutex_unlock(&ff->bfl); | |
1417 | return ret; | |
1418 | } | |
1419 | ||
1420 | static int op_symlink(const char *src, const char *dest) | |
1421 | { | |
1422 | struct fuse_context *ctxt = fuse_get_context(); | |
1423 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
1424 | ext2_filsys fs; | |
1425 | ext2_ino_t parent, child; | |
da9d5b80 | 1426 | char *temp_path; |
81cbf1ef DW |
1427 | errcode_t err; |
1428 | char *node_name, a; | |
1429 | struct ext2_inode_large inode; | |
1430 | int ret = 0; | |
1431 | ||
1432 | FUSE2FS_CHECK_CONTEXT(ff); | |
1433 | fs = ff->fs; | |
1434 | dbg_printf("%s: symlink %s to %s\n", __func__, src, dest); | |
da9d5b80 | 1435 | temp_path = strdup(dest); |
81cbf1ef DW |
1436 | if (!temp_path) { |
1437 | ret = -ENOMEM; | |
1438 | goto out; | |
1439 | } | |
1440 | node_name = strrchr(temp_path, '/'); | |
1441 | if (!node_name) { | |
1442 | ret = -ENOMEM; | |
1443 | goto out; | |
1444 | } | |
1445 | node_name++; | |
1446 | a = *node_name; | |
1447 | *node_name = 0; | |
1448 | ||
1449 | pthread_mutex_lock(&ff->bfl); | |
1450 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path, | |
1451 | &parent); | |
1452 | *node_name = a; | |
1453 | if (err) { | |
1454 | ret = translate_error(fs, 0, err); | |
1455 | goto out2; | |
1456 | } | |
1457 | ||
1458 | ret = check_inum_access(fs, parent, W_OK); | |
1459 | if (ret) | |
1460 | goto out2; | |
1461 | ||
1462 | ||
1463 | /* Create symlink */ | |
4e222d9b | 1464 | err = ext2fs_symlink(fs, parent, 0, node_name, src); |
81cbf1ef DW |
1465 | if (err == EXT2_ET_DIR_NO_SPACE) { |
1466 | err = ext2fs_expand_dir(fs, parent); | |
1467 | if (err) { | |
1468 | ret = translate_error(fs, parent, err); | |
1469 | goto out2; | |
1470 | } | |
1471 | ||
4e222d9b | 1472 | err = ext2fs_symlink(fs, parent, 0, node_name, src); |
81cbf1ef DW |
1473 | } |
1474 | if (err) { | |
1475 | ret = translate_error(fs, parent, err); | |
1476 | goto out2; | |
1477 | } | |
1478 | ||
1479 | /* Update parent dir's mtime */ | |
1480 | ret = update_mtime(fs, parent, NULL); | |
1481 | if (ret) | |
1482 | goto out2; | |
1483 | ||
1484 | /* Still have to update the uid/gid of the symlink */ | |
1485 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path, | |
1486 | &child); | |
1487 | if (err) { | |
1488 | ret = translate_error(fs, 0, err); | |
1489 | goto out2; | |
1490 | } | |
1491 | dbg_printf("%s: symlinking ino=%d/name=%s to dir=%d\n", __func__, | |
1492 | child, node_name, parent); | |
1493 | ||
1494 | memset(&inode, 0, sizeof(inode)); | |
1495 | err = ext2fs_read_inode_full(fs, child, (struct ext2_inode *)&inode, | |
1496 | sizeof(inode)); | |
1497 | if (err) { | |
1498 | ret = translate_error(fs, child, err); | |
1499 | goto out2; | |
1500 | } | |
1501 | ||
1502 | inode.i_uid = ctxt->uid; | |
dae1ecc2 | 1503 | ext2fs_set_i_uid_high(inode, ctxt->uid >> 16); |
81cbf1ef | 1504 | inode.i_gid = ctxt->gid; |
dae1ecc2 | 1505 | ext2fs_set_i_gid_high(inode, ctxt->gid >> 16); |
81cbf1ef DW |
1506 | inode.i_generation = ff->next_generation++; |
1507 | ||
1508 | err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode, | |
1509 | sizeof(inode)); | |
1510 | if (err) { | |
1511 | ret = translate_error(fs, child, err); | |
1512 | goto out2; | |
1513 | } | |
1514 | out2: | |
1515 | pthread_mutex_unlock(&ff->bfl); | |
1516 | out: | |
1517 | free(temp_path); | |
1518 | return ret; | |
1519 | } | |
1520 | ||
1521 | struct update_dotdot { | |
1522 | ext2_ino_t new_dotdot; | |
1523 | }; | |
1524 | ||
4e222d9b TT |
1525 | static int update_dotdot_helper(ext2_ino_t dir EXT2FS_ATTR((unused)), |
1526 | int entry EXT2FS_ATTR((unused)), | |
1527 | struct ext2_dir_entry *dirent, | |
1528 | int offset EXT2FS_ATTR((unused)), | |
1529 | int blocksize EXT2FS_ATTR((unused)), | |
1530 | char *buf EXT2FS_ATTR((unused)), | |
1531 | void *priv_data) | |
81cbf1ef DW |
1532 | { |
1533 | struct update_dotdot *ud = priv_data; | |
1534 | ||
1535 | if (ext2fs_dirent_name_len(dirent) == 2 && | |
1536 | dirent->name[0] == '.' && dirent->name[1] == '.') { | |
1537 | dirent->inode = ud->new_dotdot; | |
1538 | return DIRENT_CHANGED | DIRENT_ABORT; | |
1539 | } | |
1540 | ||
1541 | return 0; | |
1542 | } | |
1543 | ||
1544 | static int op_rename(const char *from, const char *to) | |
1545 | { | |
1546 | struct fuse_context *ctxt = fuse_get_context(); | |
1547 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
1548 | ext2_filsys fs; | |
1549 | errcode_t err; | |
1550 | ext2_ino_t from_ino, to_ino, to_dir_ino, from_dir_ino; | |
1551 | char *temp_to = NULL, *temp_from = NULL; | |
1552 | char *cp, a; | |
1553 | struct ext2_inode inode; | |
1554 | struct update_dotdot ud; | |
1555 | int ret = 0; | |
1556 | ||
1557 | FUSE2FS_CHECK_CONTEXT(ff); | |
1558 | fs = ff->fs; | |
1559 | dbg_printf("%s: renaming %s to %s\n", __func__, from, to); | |
1560 | pthread_mutex_lock(&ff->bfl); | |
1561 | if (!fs_can_allocate(ff, 5)) { | |
1562 | ret = -ENOSPC; | |
1563 | goto out; | |
1564 | } | |
1565 | ||
1566 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, from, &from_ino); | |
1567 | if (err || from_ino == 0) { | |
1568 | ret = translate_error(fs, 0, err); | |
1569 | goto out; | |
1570 | } | |
1571 | ||
1572 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, to, &to_ino); | |
1573 | if (err && err != EXT2_ET_FILE_NOT_FOUND) { | |
1574 | ret = translate_error(fs, 0, err); | |
1575 | goto out; | |
1576 | } | |
1577 | ||
1578 | if (err == EXT2_ET_FILE_NOT_FOUND) | |
1579 | to_ino = 0; | |
1580 | ||
1581 | /* Already the same file? */ | |
1582 | if (to_ino != 0 && to_ino == from_ino) { | |
1583 | ret = 0; | |
1584 | goto out; | |
1585 | } | |
1586 | ||
1587 | temp_to = strdup(to); | |
1588 | if (!temp_to) { | |
1589 | ret = -ENOMEM; | |
1590 | goto out; | |
1591 | } | |
1592 | ||
1593 | temp_from = strdup(from); | |
1594 | if (!temp_from) { | |
1595 | ret = -ENOMEM; | |
1596 | goto out2; | |
1597 | } | |
1598 | ||
1599 | /* Find parent dir of the source and check write access */ | |
1600 | cp = strrchr(temp_from, '/'); | |
1601 | if (!cp) { | |
1602 | ret = -EINVAL; | |
1603 | goto out2; | |
1604 | } | |
1605 | ||
1606 | a = *(cp + 1); | |
1607 | *(cp + 1) = 0; | |
1608 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_from, | |
1609 | &from_dir_ino); | |
1610 | *(cp + 1) = a; | |
1611 | if (err) { | |
1612 | ret = translate_error(fs, 0, err); | |
1613 | goto out2; | |
1614 | } | |
1615 | if (from_dir_ino == 0) { | |
1616 | ret = -ENOENT; | |
1617 | goto out2; | |
1618 | } | |
1619 | ||
1620 | ret = check_inum_access(fs, from_dir_ino, W_OK); | |
1621 | if (ret) | |
1622 | goto out2; | |
1623 | ||
1624 | /* Find parent dir of the destination and check write access */ | |
1625 | cp = strrchr(temp_to, '/'); | |
1626 | if (!cp) { | |
1627 | ret = -EINVAL; | |
1628 | goto out2; | |
1629 | } | |
1630 | ||
1631 | a = *(cp + 1); | |
1632 | *(cp + 1) = 0; | |
1633 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_to, | |
1634 | &to_dir_ino); | |
1635 | *(cp + 1) = a; | |
1636 | if (err) { | |
1637 | ret = translate_error(fs, 0, err); | |
1638 | goto out2; | |
1639 | } | |
1640 | if (to_dir_ino == 0) { | |
1641 | ret = -ENOENT; | |
1642 | goto out2; | |
1643 | } | |
1644 | ||
1645 | ret = check_inum_access(fs, to_dir_ino, W_OK); | |
1646 | if (ret) | |
1647 | goto out2; | |
1648 | ||
1649 | /* If the target exists, unlink it first */ | |
1650 | if (to_ino != 0) { | |
1651 | err = ext2fs_read_inode(fs, to_ino, &inode); | |
1652 | if (err) { | |
1653 | ret = translate_error(fs, to_ino, err); | |
1654 | goto out2; | |
1655 | } | |
1656 | ||
1657 | dbg_printf("%s: unlinking %s ino=%d\n", __func__, | |
1658 | LINUX_S_ISDIR(inode.i_mode) ? "dir" : "file", | |
1659 | to_ino); | |
1660 | if (LINUX_S_ISDIR(inode.i_mode)) | |
1661 | ret = __op_rmdir(ff, to); | |
1662 | else | |
1663 | ret = __op_unlink(ff, to); | |
1664 | if (ret) | |
1665 | goto out2; | |
1666 | } | |
1667 | ||
1668 | /* Get ready to do the move */ | |
1669 | err = ext2fs_read_inode(fs, from_ino, &inode); | |
1670 | if (err) { | |
1671 | ret = translate_error(fs, from_ino, err); | |
1672 | goto out2; | |
1673 | } | |
1674 | ||
1675 | /* Link in the new file */ | |
1676 | dbg_printf("%s: linking ino=%d/path=%s to dir=%d\n", __func__, | |
1677 | from_ino, cp + 1, to_dir_ino); | |
1678 | err = ext2fs_link(fs, to_dir_ino, cp + 1, from_ino, | |
1679 | ext2_file_type(inode.i_mode)); | |
1680 | if (err == EXT2_ET_DIR_NO_SPACE) { | |
1681 | err = ext2fs_expand_dir(fs, to_dir_ino); | |
1682 | if (err) { | |
1683 | ret = translate_error(fs, to_dir_ino, err); | |
1684 | goto out2; | |
1685 | } | |
1686 | ||
1687 | err = ext2fs_link(fs, to_dir_ino, cp + 1, from_ino, | |
1688 | ext2_file_type(inode.i_mode)); | |
1689 | } | |
1690 | if (err) { | |
1691 | ret = translate_error(fs, to_dir_ino, err); | |
1692 | goto out2; | |
1693 | } | |
1694 | ||
1695 | /* Update '..' pointer if dir */ | |
1696 | err = ext2fs_read_inode(fs, from_ino, &inode); | |
1697 | if (err) { | |
1698 | ret = translate_error(fs, from_ino, err); | |
1699 | goto out2; | |
1700 | } | |
1701 | ||
1702 | if (LINUX_S_ISDIR(inode.i_mode)) { | |
1703 | ud.new_dotdot = to_dir_ino; | |
1704 | dbg_printf("%s: updating .. entry for dir=%d\n", __func__, | |
1705 | to_dir_ino); | |
1706 | err = ext2fs_dir_iterate2(fs, from_ino, 0, NULL, | |
1707 | update_dotdot_helper, &ud); | |
1708 | if (err) { | |
1709 | ret = translate_error(fs, from_ino, err); | |
1710 | goto out2; | |
1711 | } | |
1712 | ||
1713 | /* Decrease from_dir_ino's links_count */ | |
1714 | dbg_printf("%s: moving linkcount from dir=%d to dir=%d\n", | |
1715 | __func__, from_dir_ino, to_dir_ino); | |
1716 | err = ext2fs_read_inode(fs, from_dir_ino, &inode); | |
1717 | if (err) { | |
1718 | ret = translate_error(fs, from_dir_ino, err); | |
1719 | goto out2; | |
1720 | } | |
1721 | inode.i_links_count--; | |
1722 | err = ext2fs_write_inode(fs, from_dir_ino, &inode); | |
1723 | if (err) { | |
1724 | ret = translate_error(fs, from_dir_ino, err); | |
1725 | goto out2; | |
1726 | } | |
1727 | ||
1728 | /* Increase to_dir_ino's links_count */ | |
1729 | err = ext2fs_read_inode(fs, to_dir_ino, &inode); | |
1730 | if (err) { | |
1731 | ret = translate_error(fs, to_dir_ino, err); | |
1732 | goto out2; | |
1733 | } | |
1734 | inode.i_links_count++; | |
1735 | err = ext2fs_write_inode(fs, to_dir_ino, &inode); | |
1736 | if (err) { | |
1737 | ret = translate_error(fs, to_dir_ino, err); | |
1738 | goto out2; | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | /* Update timestamps */ | |
1743 | ret = update_ctime(fs, from_ino, NULL); | |
1744 | if (ret) | |
1745 | goto out2; | |
1746 | ||
1747 | ret = update_mtime(fs, to_dir_ino, NULL); | |
1748 | if (ret) | |
1749 | goto out2; | |
1750 | ||
1751 | /* Remove the old file */ | |
1752 | ret = unlink_file_by_name(fs, from); | |
1753 | if (ret) | |
1754 | goto out2; | |
1755 | ||
1756 | /* Flush the whole mess out */ | |
1757 | err = ext2fs_flush2(fs, 0); | |
1758 | if (err) | |
1759 | ret = translate_error(fs, 0, err); | |
1760 | ||
1761 | out2: | |
1762 | free(temp_from); | |
1763 | free(temp_to); | |
1764 | out: | |
1765 | pthread_mutex_unlock(&ff->bfl); | |
1766 | return ret; | |
1767 | } | |
1768 | ||
1769 | static int op_link(const char *src, const char *dest) | |
1770 | { | |
1771 | struct fuse_context *ctxt = fuse_get_context(); | |
1772 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
1773 | ext2_filsys fs; | |
da9d5b80 | 1774 | char *temp_path; |
81cbf1ef DW |
1775 | errcode_t err; |
1776 | char *node_name, a; | |
1777 | ext2_ino_t parent, ino; | |
1778 | struct ext2_inode_large inode; | |
1779 | int ret = 0; | |
1780 | ||
1781 | FUSE2FS_CHECK_CONTEXT(ff); | |
1782 | fs = ff->fs; | |
1783 | dbg_printf("%s: src=%s dest=%s\n", __func__, src, dest); | |
da9d5b80 | 1784 | temp_path = strdup(dest); |
81cbf1ef DW |
1785 | if (!temp_path) { |
1786 | ret = -ENOMEM; | |
1787 | goto out; | |
1788 | } | |
1789 | node_name = strrchr(temp_path, '/'); | |
1790 | if (!node_name) { | |
1791 | ret = -ENOMEM; | |
1792 | goto out; | |
1793 | } | |
1794 | node_name++; | |
1795 | a = *node_name; | |
1796 | *node_name = 0; | |
1797 | ||
1798 | pthread_mutex_lock(&ff->bfl); | |
1799 | if (!fs_can_allocate(ff, 2)) { | |
1800 | ret = -ENOSPC; | |
1801 | goto out2; | |
1802 | } | |
1803 | ||
1804 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path, | |
1805 | &parent); | |
1806 | *node_name = a; | |
1807 | if (err) { | |
1808 | err = -ENOENT; | |
1809 | goto out2; | |
1810 | } | |
1811 | ||
1812 | ret = check_inum_access(fs, parent, W_OK); | |
1813 | if (ret) | |
1814 | goto out2; | |
1815 | ||
1816 | ||
1817 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, src, &ino); | |
1818 | if (err || ino == 0) { | |
1819 | ret = translate_error(fs, 0, err); | |
1820 | goto out2; | |
1821 | } | |
1822 | ||
1823 | memset(&inode, 0, sizeof(inode)); | |
1824 | err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
1825 | sizeof(inode)); | |
1826 | if (err) { | |
1827 | ret = translate_error(fs, ino, err); | |
1828 | goto out2; | |
1829 | } | |
1830 | ||
1831 | inode.i_links_count++; | |
1832 | ret = update_ctime(fs, ino, &inode); | |
1833 | if (ret) | |
1834 | goto out2; | |
1835 | ||
1836 | err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
1837 | sizeof(inode)); | |
1838 | if (err) { | |
1839 | ret = translate_error(fs, ino, err); | |
1840 | goto out2; | |
1841 | } | |
1842 | ||
1843 | dbg_printf("%s: linking ino=%d/name=%s to dir=%d\n", __func__, ino, | |
1844 | node_name, parent); | |
1845 | err = ext2fs_link(fs, parent, node_name, ino, | |
1846 | ext2_file_type(inode.i_mode)); | |
1847 | if (err == EXT2_ET_DIR_NO_SPACE) { | |
1848 | err = ext2fs_expand_dir(fs, parent); | |
1849 | if (err) { | |
1850 | ret = translate_error(fs, parent, err); | |
1851 | goto out2; | |
1852 | } | |
1853 | ||
1854 | err = ext2fs_link(fs, parent, node_name, ino, | |
1855 | ext2_file_type(inode.i_mode)); | |
1856 | } | |
1857 | if (err) { | |
1858 | ret = translate_error(fs, parent, err); | |
1859 | goto out2; | |
1860 | } | |
1861 | ||
1862 | ret = update_mtime(fs, parent, NULL); | |
1863 | if (ret) | |
1864 | goto out2; | |
1865 | ||
1866 | out2: | |
1867 | pthread_mutex_unlock(&ff->bfl); | |
1868 | out: | |
1869 | free(temp_path); | |
1870 | return ret; | |
1871 | } | |
1872 | ||
1873 | static int op_chmod(const char *path, mode_t mode) | |
1874 | { | |
1875 | struct fuse_context *ctxt = fuse_get_context(); | |
1876 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
1877 | ext2_filsys fs; | |
1878 | errcode_t err; | |
1879 | ext2_ino_t ino; | |
1880 | struct ext2_inode_large inode; | |
1881 | int ret = 0; | |
1882 | ||
1883 | FUSE2FS_CHECK_CONTEXT(ff); | |
1884 | fs = ff->fs; | |
1885 | pthread_mutex_lock(&ff->bfl); | |
1886 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
1887 | if (err) { | |
1888 | ret = translate_error(fs, 0, err); | |
1889 | goto out; | |
1890 | } | |
1891 | dbg_printf("%s: path=%s mode=0%o ino=%d\n", __func__, path, mode, ino); | |
1892 | ||
1893 | memset(&inode, 0, sizeof(inode)); | |
1894 | err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
1895 | sizeof(inode)); | |
1896 | if (err) { | |
1897 | ret = translate_error(fs, ino, err); | |
1898 | goto out; | |
1899 | } | |
1900 | ||
dae1ecc2 | 1901 | if (!ff->fakeroot && ctxt->uid != 0 && ctxt->uid != inode_uid(inode)) { |
81cbf1ef DW |
1902 | ret = -EPERM; |
1903 | goto out; | |
1904 | } | |
1905 | ||
1906 | /* | |
1907 | * XXX: We should really check that the inode gid is not in /any/ | |
1908 | * of the user's groups, but FUSE only tells us about the primary | |
1909 | * group. | |
1910 | */ | |
dae1ecc2 | 1911 | if (!ff->fakeroot && ctxt->uid != 0 && ctxt->gid != inode_gid(inode)) |
81cbf1ef DW |
1912 | mode &= ~S_ISGID; |
1913 | ||
1914 | inode.i_mode &= ~0xFFF; | |
1915 | inode.i_mode |= mode & 0xFFF; | |
1916 | ret = update_ctime(fs, ino, &inode); | |
1917 | if (ret) | |
1918 | goto out; | |
1919 | ||
1920 | err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
1921 | sizeof(inode)); | |
1922 | if (err) { | |
1923 | ret = translate_error(fs, ino, err); | |
1924 | goto out; | |
1925 | } | |
1926 | ||
1927 | out: | |
1928 | pthread_mutex_unlock(&ff->bfl); | |
1929 | return ret; | |
1930 | } | |
1931 | ||
1932 | static int op_chown(const char *path, uid_t owner, gid_t group) | |
1933 | { | |
1934 | struct fuse_context *ctxt = fuse_get_context(); | |
1935 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
1936 | ext2_filsys fs; | |
1937 | errcode_t err; | |
1938 | ext2_ino_t ino; | |
1939 | struct ext2_inode_large inode; | |
1940 | int ret = 0; | |
1941 | ||
1942 | FUSE2FS_CHECK_CONTEXT(ff); | |
1943 | fs = ff->fs; | |
1944 | pthread_mutex_lock(&ff->bfl); | |
1945 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
1946 | if (err) { | |
1947 | ret = translate_error(fs, 0, err); | |
1948 | goto out; | |
1949 | } | |
1950 | dbg_printf("%s: path=%s owner=%d group=%d ino=%d\n", __func__, | |
1951 | path, owner, group, ino); | |
1952 | ||
1953 | memset(&inode, 0, sizeof(inode)); | |
1954 | err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
1955 | sizeof(inode)); | |
1956 | if (err) { | |
1957 | ret = translate_error(fs, ino, err); | |
1958 | goto out; | |
1959 | } | |
1960 | ||
1961 | /* FUSE seems to feed us ~0 to mean "don't change" */ | |
4e222d9b | 1962 | if (owner != (uid_t) ~0) { |
81cbf1ef | 1963 | /* Only root gets to change UID. */ |
ea1a3fa2 | 1964 | if (!ff->fakeroot && ctxt->uid != 0 && |
dae1ecc2 | 1965 | !(inode_uid(inode) == ctxt->uid && owner == ctxt->uid)) { |
81cbf1ef DW |
1966 | ret = -EPERM; |
1967 | goto out; | |
1968 | } | |
1969 | inode.i_uid = owner; | |
dae1ecc2 | 1970 | ext2fs_set_i_uid_high(inode, owner >> 16); |
81cbf1ef DW |
1971 | } |
1972 | ||
4e222d9b | 1973 | if (group != (gid_t) ~0) { |
81cbf1ef | 1974 | /* Only root or the owner get to change GID. */ |
dae1ecc2 TT |
1975 | if (!ff->fakeroot && ctxt->uid != 0 && |
1976 | inode_uid(inode) != ctxt->uid) { | |
81cbf1ef DW |
1977 | ret = -EPERM; |
1978 | goto out; | |
1979 | } | |
1980 | ||
1981 | /* XXX: We /should/ check group membership but FUSE */ | |
1982 | inode.i_gid = group; | |
dae1ecc2 | 1983 | ext2fs_set_i_gid_high(inode, group >> 16); |
81cbf1ef DW |
1984 | } |
1985 | ||
1986 | ret = update_ctime(fs, ino, &inode); | |
1987 | if (ret) | |
1988 | goto out; | |
1989 | ||
1990 | err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
1991 | sizeof(inode)); | |
1992 | if (err) { | |
1993 | ret = translate_error(fs, ino, err); | |
1994 | goto out; | |
1995 | } | |
1996 | ||
1997 | out: | |
1998 | pthread_mutex_unlock(&ff->bfl); | |
1999 | return ret; | |
2000 | } | |
2001 | ||
2002 | static int op_truncate(const char *path, off_t len) | |
2003 | { | |
2004 | struct fuse_context *ctxt = fuse_get_context(); | |
2005 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
2006 | ext2_filsys fs; | |
2007 | errcode_t err; | |
2008 | ext2_ino_t ino; | |
2009 | ext2_file_t file; | |
2010 | int ret = 0; | |
2011 | ||
2012 | FUSE2FS_CHECK_CONTEXT(ff); | |
2013 | fs = ff->fs; | |
2014 | pthread_mutex_lock(&ff->bfl); | |
2015 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
2016 | if (err || ino == 0) { | |
2017 | ret = translate_error(fs, 0, err); | |
2018 | goto out; | |
2019 | } | |
2020 | dbg_printf("%s: ino=%d len=%jd\n", __func__, ino, len); | |
2021 | ||
2022 | ret = check_inum_access(fs, ino, W_OK); | |
2023 | if (ret) | |
2024 | goto out; | |
2025 | ||
2026 | err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file); | |
2027 | if (err) { | |
2028 | ret = translate_error(fs, ino, err); | |
2029 | goto out; | |
2030 | } | |
2031 | ||
2032 | err = ext2fs_file_set_size2(file, len); | |
2033 | if (err) { | |
2034 | ret = translate_error(fs, ino, err); | |
2035 | goto out2; | |
2036 | } | |
2037 | ||
2038 | out2: | |
2039 | err = ext2fs_file_close(file); | |
2040 | if (ret) | |
2041 | goto out; | |
2042 | if (err) { | |
2043 | ret = translate_error(fs, ino, err); | |
2044 | goto out; | |
2045 | } | |
2046 | ||
2047 | ret = update_mtime(fs, ino, NULL); | |
2048 | ||
2049 | out: | |
2050 | pthread_mutex_unlock(&ff->bfl); | |
2051 | return err; | |
2052 | } | |
2053 | ||
2054 | #ifdef __linux__ | |
2055 | static void detect_linux_executable_open(int kernel_flags, int *access_check, | |
2056 | int *e2fs_open_flags) | |
2057 | { | |
2058 | /* | |
2059 | * On Linux, execve will bleed __FMODE_EXEC into the file mode flags, | |
2060 | * and FUSE is more than happy to let that slip through. | |
2061 | */ | |
2062 | if (kernel_flags & 0x20) { | |
2063 | *access_check = X_OK; | |
2064 | *e2fs_open_flags &= ~EXT2_FILE_WRITE; | |
2065 | } | |
2066 | } | |
2067 | #else | |
2068 | static void detect_linux_executable_open(int kernel_flags, int *access_check, | |
2069 | int *e2fs_open_flags) | |
2070 | { | |
2071 | /* empty */ | |
2072 | } | |
2073 | #endif /* __linux__ */ | |
2074 | ||
2075 | static int __op_open(struct fuse2fs *ff, const char *path, | |
2076 | struct fuse_file_info *fp) | |
2077 | { | |
2078 | ext2_filsys fs = ff->fs; | |
2079 | errcode_t err; | |
2080 | struct fuse2fs_file_handle *file; | |
2081 | int check = 0, ret = 0; | |
2082 | ||
2083 | dbg_printf("%s: path=%s\n", __func__, path); | |
2084 | err = ext2fs_get_mem(sizeof(*file), &file); | |
2085 | if (err) | |
2086 | return translate_error(fs, 0, err); | |
2087 | file->magic = FUSE2FS_FILE_MAGIC; | |
2088 | ||
2089 | file->open_flags = 0; | |
2090 | switch (fp->flags & O_ACCMODE) { | |
2091 | case O_RDONLY: | |
2092 | check = R_OK; | |
2093 | break; | |
2094 | case O_WRONLY: | |
2095 | check = W_OK; | |
2096 | file->open_flags |= EXT2_FILE_WRITE; | |
2097 | break; | |
2098 | case O_RDWR: | |
2099 | check = R_OK | W_OK; | |
2100 | file->open_flags |= EXT2_FILE_WRITE; | |
2101 | break; | |
2102 | } | |
2103 | ||
2104 | detect_linux_executable_open(fp->flags, &check, &file->open_flags); | |
2105 | ||
2106 | if (fp->flags & O_CREAT) | |
2107 | file->open_flags |= EXT2_FILE_CREATE; | |
2108 | ||
2109 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &file->ino); | |
2110 | if (err || file->ino == 0) { | |
2111 | ret = translate_error(fs, 0, err); | |
2112 | goto out; | |
2113 | } | |
2114 | dbg_printf("%s: ino=%d\n", __func__, file->ino); | |
2115 | ||
2116 | ret = check_inum_access(fs, file->ino, check); | |
2117 | if (ret) { | |
2118 | /* | |
2119 | * In a regular (Linux) fs driver, the kernel will open | |
2120 | * binaries for reading if the user has --x privileges (i.e. | |
2121 | * execute without read). Since the kernel doesn't have any | |
2122 | * way to tell us if it's opening a file via execve, we'll | |
2123 | * just assume that allowing access is ok if asking for ro mode | |
2124 | * fails but asking for x mode succeeds. Of course we can | |
2125 | * also employ undocumented hacks (see above). | |
2126 | */ | |
2127 | if (check == R_OK) { | |
2128 | ret = check_inum_access(fs, file->ino, X_OK); | |
2129 | if (ret) | |
2130 | goto out; | |
2131 | } else | |
2132 | goto out; | |
2133 | } | |
377e3a96 | 2134 | fp->fh = (uintptr_t)file; |
81cbf1ef DW |
2135 | |
2136 | out: | |
2137 | if (ret) | |
2138 | ext2fs_free_mem(&file); | |
2139 | return ret; | |
2140 | } | |
2141 | ||
2142 | static int op_open(const char *path, struct fuse_file_info *fp) | |
2143 | { | |
2144 | struct fuse_context *ctxt = fuse_get_context(); | |
2145 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
2146 | int ret; | |
2147 | ||
2148 | FUSE2FS_CHECK_CONTEXT(ff); | |
2149 | pthread_mutex_lock(&ff->bfl); | |
2150 | ret = __op_open(ff, path, fp); | |
2151 | pthread_mutex_unlock(&ff->bfl); | |
2152 | return ret; | |
2153 | } | |
2154 | ||
4e222d9b TT |
2155 | static int op_read(const char *path EXT2FS_ATTR((unused)), char *buf, |
2156 | size_t len, off_t offset, | |
2157 | struct fuse_file_info *fp) | |
81cbf1ef DW |
2158 | { |
2159 | struct fuse_context *ctxt = fuse_get_context(); | |
2160 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
377e3a96 TT |
2161 | struct fuse2fs_file_handle *fh = |
2162 | (struct fuse2fs_file_handle *)(uintptr_t)fp->fh; | |
81cbf1ef DW |
2163 | ext2_filsys fs; |
2164 | ext2_file_t efp; | |
2165 | errcode_t err; | |
2166 | unsigned int got = 0; | |
2167 | int ret = 0; | |
2168 | ||
2169 | FUSE2FS_CHECK_CONTEXT(ff); | |
2170 | fs = ff->fs; | |
2171 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
2172 | dbg_printf("%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino, offset, | |
2173 | len); | |
2174 | pthread_mutex_lock(&ff->bfl); | |
2175 | err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp); | |
2176 | if (err) { | |
2177 | ret = translate_error(fs, fh->ino, err); | |
2178 | goto out; | |
2179 | } | |
2180 | ||
2181 | err = ext2fs_file_llseek(efp, offset, SEEK_SET, NULL); | |
2182 | if (err) { | |
2183 | ret = translate_error(fs, fh->ino, err); | |
2184 | goto out2; | |
2185 | } | |
2186 | ||
2187 | err = ext2fs_file_read(efp, buf, len, &got); | |
2188 | if (err) { | |
2189 | ret = translate_error(fs, fh->ino, err); | |
2190 | goto out2; | |
2191 | } | |
2192 | ||
2193 | out2: | |
2194 | err = ext2fs_file_close(efp); | |
2195 | if (ret) | |
2196 | goto out; | |
2197 | if (err) { | |
2198 | ret = translate_error(fs, fh->ino, err); | |
2199 | goto out; | |
2200 | } | |
2201 | ||
2202 | if (fs_writeable(fs)) { | |
2203 | ret = update_atime(fs, fh->ino); | |
2204 | if (ret) | |
2205 | goto out; | |
2206 | } | |
2207 | out: | |
2208 | pthread_mutex_unlock(&ff->bfl); | |
4e222d9b | 2209 | return got ? (int) got : ret; |
81cbf1ef DW |
2210 | } |
2211 | ||
4e222d9b TT |
2212 | static int op_write(const char *path EXT2FS_ATTR((unused)), |
2213 | const char *buf, size_t len, off_t offset, | |
2214 | struct fuse_file_info *fp) | |
81cbf1ef DW |
2215 | { |
2216 | struct fuse_context *ctxt = fuse_get_context(); | |
2217 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
377e3a96 TT |
2218 | struct fuse2fs_file_handle *fh = |
2219 | (struct fuse2fs_file_handle *)(uintptr_t)fp->fh; | |
81cbf1ef DW |
2220 | ext2_filsys fs; |
2221 | ext2_file_t efp; | |
2222 | errcode_t err; | |
2223 | unsigned int got = 0; | |
2224 | int ret = 0; | |
2225 | ||
2226 | FUSE2FS_CHECK_CONTEXT(ff); | |
2227 | fs = ff->fs; | |
2228 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
2229 | dbg_printf("%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino, offset, | |
2230 | len); | |
2231 | pthread_mutex_lock(&ff->bfl); | |
2232 | if (!fs_writeable(fs)) { | |
2233 | ret = -EROFS; | |
2234 | goto out; | |
2235 | } | |
2236 | ||
2237 | if (!fs_can_allocate(ff, len / fs->blocksize)) { | |
2238 | ret = -ENOSPC; | |
2239 | goto out; | |
2240 | } | |
2241 | ||
2242 | err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp); | |
2243 | if (err) { | |
2244 | ret = translate_error(fs, fh->ino, err); | |
2245 | goto out; | |
2246 | } | |
2247 | ||
2248 | err = ext2fs_file_llseek(efp, offset, SEEK_SET, NULL); | |
2249 | if (err) { | |
2250 | ret = translate_error(fs, fh->ino, err); | |
2251 | goto out2; | |
2252 | } | |
2253 | ||
2254 | err = ext2fs_file_write(efp, buf, len, &got); | |
2255 | if (err) { | |
2256 | ret = translate_error(fs, fh->ino, err); | |
2257 | goto out2; | |
2258 | } | |
2259 | ||
2260 | err = ext2fs_file_flush(efp); | |
2261 | if (err) { | |
2262 | got = 0; | |
2263 | ret = translate_error(fs, fh->ino, err); | |
2264 | goto out2; | |
2265 | } | |
2266 | ||
2267 | out2: | |
2268 | err = ext2fs_file_close(efp); | |
2269 | if (ret) | |
2270 | goto out; | |
2271 | if (err) { | |
2272 | ret = translate_error(fs, fh->ino, err); | |
2273 | goto out; | |
2274 | } | |
2275 | ||
2276 | ret = update_mtime(fs, fh->ino, NULL); | |
2277 | if (ret) | |
2278 | goto out; | |
2279 | ||
2280 | out: | |
2281 | pthread_mutex_unlock(&ff->bfl); | |
4e222d9b | 2282 | return got ? (int) got : ret; |
81cbf1ef DW |
2283 | } |
2284 | ||
4e222d9b TT |
2285 | static int op_release(const char *path EXT2FS_ATTR((unused)), |
2286 | struct fuse_file_info *fp) | |
81cbf1ef DW |
2287 | { |
2288 | struct fuse_context *ctxt = fuse_get_context(); | |
2289 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
377e3a96 TT |
2290 | struct fuse2fs_file_handle *fh = |
2291 | (struct fuse2fs_file_handle *)(uintptr_t)fp->fh; | |
81cbf1ef DW |
2292 | ext2_filsys fs; |
2293 | errcode_t err; | |
2294 | int ret = 0; | |
2295 | ||
2296 | FUSE2FS_CHECK_CONTEXT(ff); | |
2297 | fs = ff->fs; | |
2298 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
2299 | dbg_printf("%s: ino=%d\n", __func__, fh->ino); | |
2300 | pthread_mutex_lock(&ff->bfl); | |
2301 | if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) { | |
2302 | err = ext2fs_flush2(fs, EXT2_FLAG_FLUSH_NO_SYNC); | |
2303 | if (err) | |
2304 | ret = translate_error(fs, fh->ino, err); | |
2305 | } | |
2306 | fp->fh = 0; | |
2307 | pthread_mutex_unlock(&ff->bfl); | |
2308 | ||
2309 | ext2fs_free_mem(&fh); | |
2310 | ||
2311 | return ret; | |
2312 | } | |
2313 | ||
4e222d9b TT |
2314 | static int op_fsync(const char *path EXT2FS_ATTR((unused)), |
2315 | int datasync EXT2FS_ATTR((unused)), | |
2316 | struct fuse_file_info *fp) | |
81cbf1ef DW |
2317 | { |
2318 | struct fuse_context *ctxt = fuse_get_context(); | |
2319 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
377e3a96 TT |
2320 | struct fuse2fs_file_handle *fh = |
2321 | (struct fuse2fs_file_handle *)(uintptr_t)fp->fh; | |
81cbf1ef DW |
2322 | ext2_filsys fs; |
2323 | errcode_t err; | |
2324 | int ret = 0; | |
2325 | ||
2326 | FUSE2FS_CHECK_CONTEXT(ff); | |
2327 | fs = ff->fs; | |
2328 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
2329 | dbg_printf("%s: ino=%d\n", __func__, fh->ino); | |
2330 | /* For now, flush everything, even if it's slow */ | |
2331 | pthread_mutex_lock(&ff->bfl); | |
2332 | if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) { | |
2333 | err = ext2fs_flush2(fs, 0); | |
2334 | if (err) | |
2335 | ret = translate_error(fs, fh->ino, err); | |
2336 | } | |
2337 | pthread_mutex_unlock(&ff->bfl); | |
2338 | ||
2339 | return ret; | |
2340 | } | |
2341 | ||
4e222d9b TT |
2342 | static int op_statfs(const char *path EXT2FS_ATTR((unused)), |
2343 | struct statvfs *buf) | |
81cbf1ef DW |
2344 | { |
2345 | struct fuse_context *ctxt = fuse_get_context(); | |
2346 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
2347 | ext2_filsys fs; | |
2348 | uint64_t fsid, *f; | |
2349 | blk64_t overhead, reserved, free; | |
2350 | ||
2351 | FUSE2FS_CHECK_CONTEXT(ff); | |
2352 | fs = ff->fs; | |
2353 | dbg_printf("%s: path=%s\n", __func__, path); | |
2354 | buf->f_bsize = fs->blocksize; | |
2355 | buf->f_frsize = 0; | |
2356 | ||
2357 | if (ff->minixdf) | |
2358 | overhead = 0; | |
2359 | else | |
2360 | overhead = fs->desc_blocks + | |
102993ec | 2361 | (blk64_t)fs->group_desc_count * |
81cbf1ef DW |
2362 | (fs->inode_blocks_per_group + 2); |
2363 | reserved = ext2fs_r_blocks_count(fs->super); | |
2364 | if (!reserved) | |
2365 | reserved = ext2fs_blocks_count(fs->super) / 10; | |
2366 | free = ext2fs_free_blocks_count(fs->super); | |
2367 | ||
2368 | buf->f_blocks = ext2fs_blocks_count(fs->super) - overhead; | |
2369 | buf->f_bfree = free; | |
2370 | if (free < reserved) | |
2371 | buf->f_bavail = 0; | |
2372 | else | |
2373 | buf->f_bavail = free - reserved; | |
2374 | buf->f_files = fs->super->s_inodes_count; | |
2375 | buf->f_ffree = fs->super->s_free_inodes_count; | |
2376 | buf->f_favail = fs->super->s_free_inodes_count; | |
2377 | f = (uint64_t *)fs->super->s_uuid; | |
2378 | fsid = *f; | |
2379 | f++; | |
2380 | fsid ^= *f; | |
2381 | buf->f_fsid = fsid; | |
2382 | buf->f_flag = 0; | |
2383 | if (fs->flags & EXT2_FLAG_RW) | |
2384 | buf->f_flag |= ST_RDONLY; | |
2385 | buf->f_namemax = EXT2_NAME_LEN; | |
2386 | ||
2387 | return 0; | |
2388 | } | |
2389 | ||
2390 | typedef errcode_t (*xattr_xlate_get)(void **cooked_buf, size_t *cooked_sz, | |
2391 | const void *raw_buf, size_t raw_sz); | |
2392 | typedef errcode_t (*xattr_xlate_set)(const void *cooked_buf, size_t cooked_sz, | |
4e222d9b | 2393 | const void **raw_buf, size_t *raw_sz); |
81cbf1ef DW |
2394 | struct xattr_translate { |
2395 | const char *prefix; | |
2396 | xattr_xlate_get get; | |
2397 | xattr_xlate_set set; | |
2398 | }; | |
2399 | ||
2400 | #define XATTR_TRANSLATOR(p, g, s) \ | |
2401 | {.prefix = (p), \ | |
2402 | .get = (xattr_xlate_get)(g), \ | |
2403 | .set = (xattr_xlate_set)(s)} | |
2404 | ||
2405 | static struct xattr_translate xattr_translators[] = { | |
2406 | #ifdef TRANSLATE_LINUX_ACLS | |
2407 | XATTR_TRANSLATOR(ACL_EA_ACCESS, ext4_to_fuse_acl, fuse_to_ext4_acl), | |
2408 | XATTR_TRANSLATOR(ACL_EA_DEFAULT, ext4_to_fuse_acl, fuse_to_ext4_acl), | |
2409 | #endif | |
2410 | XATTR_TRANSLATOR(NULL, NULL, NULL), | |
2411 | }; | |
2412 | #undef XATTR_TRANSLATOR | |
2413 | ||
2414 | static int op_getxattr(const char *path, const char *key, char *value, | |
2415 | size_t len) | |
2416 | { | |
2417 | struct fuse_context *ctxt = fuse_get_context(); | |
2418 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
2419 | ext2_filsys fs; | |
2420 | struct ext2_xattr_handle *h; | |
2421 | struct xattr_translate *xt; | |
2422 | void *ptr, *cptr; | |
2423 | size_t plen, clen; | |
2424 | ext2_ino_t ino; | |
2425 | errcode_t err; | |
2426 | int ret = 0; | |
2427 | ||
2428 | FUSE2FS_CHECK_CONTEXT(ff); | |
2429 | fs = ff->fs; | |
2430 | pthread_mutex_lock(&ff->bfl); | |
7889640d | 2431 | if (!ext2fs_has_feature_xattr(fs->super)) { |
81cbf1ef DW |
2432 | ret = -ENOTSUP; |
2433 | goto out; | |
2434 | } | |
2435 | ||
2436 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
2437 | if (err || ino == 0) { | |
2438 | ret = translate_error(fs, 0, err); | |
2439 | goto out; | |
2440 | } | |
2441 | dbg_printf("%s: ino=%d\n", __func__, ino); | |
2442 | ||
2443 | ret = check_inum_access(fs, ino, R_OK); | |
2444 | if (ret) | |
2445 | goto out; | |
2446 | ||
2447 | err = ext2fs_xattrs_open(fs, ino, &h); | |
2448 | if (err) { | |
2449 | ret = translate_error(fs, ino, err); | |
2450 | goto out; | |
2451 | } | |
2452 | ||
2453 | err = ext2fs_xattrs_read(h); | |
2454 | if (err) { | |
2455 | ret = translate_error(fs, ino, err); | |
2456 | goto out2; | |
2457 | } | |
2458 | ||
2459 | err = ext2fs_xattr_get(h, key, &ptr, &plen); | |
2460 | if (err) { | |
2461 | ret = translate_error(fs, ino, err); | |
2462 | goto out2; | |
2463 | } | |
2464 | ||
2465 | for (xt = xattr_translators; xt->prefix != NULL; xt++) { | |
2466 | if (strncmp(key, xt->prefix, strlen(xt->prefix)) == 0) { | |
2467 | err = xt->get(&cptr, &clen, ptr, plen); | |
2468 | if (err) | |
2469 | goto out3; | |
2470 | ext2fs_free_mem(&ptr); | |
2471 | ptr = cptr; | |
2472 | plen = clen; | |
2473 | } | |
2474 | } | |
2475 | ||
2476 | if (!len) { | |
2477 | ret = plen; | |
2478 | } else if (len < plen) { | |
2479 | ret = -ERANGE; | |
2480 | } else { | |
2481 | memcpy(value, ptr, plen); | |
2482 | ret = plen; | |
2483 | } | |
2484 | ||
2485 | out3: | |
2486 | ext2fs_free_mem(&ptr); | |
2487 | out2: | |
2488 | err = ext2fs_xattrs_close(&h); | |
2489 | if (err) | |
2490 | ret = translate_error(fs, ino, err); | |
2491 | out: | |
2492 | pthread_mutex_unlock(&ff->bfl); | |
2493 | ||
2494 | return ret; | |
2495 | } | |
2496 | ||
4e222d9b TT |
2497 | static int count_buffer_space(char *name, char *value EXT2FS_ATTR((unused)), |
2498 | size_t value_len EXT2FS_ATTR((unused)), | |
81cbf1ef DW |
2499 | void *data) |
2500 | { | |
2501 | unsigned int *x = data; | |
2502 | ||
2503 | *x = *x + strlen(name) + 1; | |
2504 | return 0; | |
2505 | } | |
2506 | ||
4e222d9b TT |
2507 | static int copy_names(char *name, char *value EXT2FS_ATTR((unused)), |
2508 | size_t value_len EXT2FS_ATTR((unused)), void *data) | |
81cbf1ef DW |
2509 | { |
2510 | char **b = data; | |
44ee1fb8 | 2511 | size_t name_len = strlen(name); |
81cbf1ef | 2512 | |
44ee1fb8 EB |
2513 | memcpy(*b, name, name_len + 1); |
2514 | *b = *b + name_len + 1; | |
81cbf1ef DW |
2515 | |
2516 | return 0; | |
2517 | } | |
2518 | ||
2519 | static int op_listxattr(const char *path, char *names, size_t len) | |
2520 | { | |
2521 | struct fuse_context *ctxt = fuse_get_context(); | |
2522 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
2523 | ext2_filsys fs; | |
2524 | struct ext2_xattr_handle *h; | |
2525 | unsigned int bufsz; | |
2526 | ext2_ino_t ino; | |
2527 | errcode_t err; | |
2528 | int ret = 0; | |
2529 | ||
2530 | FUSE2FS_CHECK_CONTEXT(ff); | |
2531 | fs = ff->fs; | |
2532 | pthread_mutex_lock(&ff->bfl); | |
7889640d | 2533 | if (!ext2fs_has_feature_xattr(fs->super)) { |
81cbf1ef DW |
2534 | ret = -ENOTSUP; |
2535 | goto out; | |
2536 | } | |
2537 | ||
2538 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
2539 | if (err || ino == 0) { | |
2540 | ret = translate_error(fs, ino, err); | |
2541 | goto out; | |
2542 | } | |
2543 | dbg_printf("%s: ino=%d\n", __func__, ino); | |
2544 | ||
2545 | ret = check_inum_access(fs, ino, R_OK); | |
2546 | if (ret) | |
da9d5b80 | 2547 | goto out; |
81cbf1ef DW |
2548 | |
2549 | err = ext2fs_xattrs_open(fs, ino, &h); | |
2550 | if (err) { | |
2551 | ret = translate_error(fs, ino, err); | |
2552 | goto out; | |
2553 | } | |
2554 | ||
2555 | err = ext2fs_xattrs_read(h); | |
2556 | if (err) { | |
2557 | ret = translate_error(fs, ino, err); | |
2558 | goto out2; | |
2559 | } | |
2560 | ||
2561 | /* Count buffer space needed for names */ | |
2562 | bufsz = 0; | |
2563 | err = ext2fs_xattrs_iterate(h, count_buffer_space, &bufsz); | |
2564 | if (err) { | |
2565 | ret = translate_error(fs, ino, err); | |
2566 | goto out2; | |
2567 | } | |
2568 | ||
2569 | if (len == 0) { | |
2570 | ret = bufsz; | |
2571 | goto out2; | |
2572 | } else if (len < bufsz) { | |
2573 | ret = -ERANGE; | |
2574 | goto out2; | |
2575 | } | |
2576 | ||
2577 | /* Copy names out */ | |
2578 | memset(names, 0, len); | |
2579 | err = ext2fs_xattrs_iterate(h, copy_names, &names); | |
2580 | if (err) { | |
2581 | ret = translate_error(fs, ino, err); | |
2582 | goto out2; | |
2583 | } | |
2584 | ret = bufsz; | |
2585 | out2: | |
2586 | err = ext2fs_xattrs_close(&h); | |
2587 | if (err) | |
2588 | ret = translate_error(fs, ino, err); | |
2589 | out: | |
2590 | pthread_mutex_unlock(&ff->bfl); | |
2591 | ||
2592 | return ret; | |
2593 | } | |
2594 | ||
4e222d9b TT |
2595 | static int op_setxattr(const char *path EXT2FS_ATTR((unused)), |
2596 | const char *key, const char *value, | |
2597 | size_t len, int flags EXT2FS_ATTR((unused))) | |
81cbf1ef DW |
2598 | { |
2599 | struct fuse_context *ctxt = fuse_get_context(); | |
2600 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
2601 | ext2_filsys fs; | |
2602 | struct ext2_xattr_handle *h; | |
2603 | struct xattr_translate *xt; | |
4e222d9b | 2604 | const void *cvalue; |
81cbf1ef DW |
2605 | size_t clen; |
2606 | ext2_ino_t ino; | |
2607 | errcode_t err; | |
2608 | int ret = 0; | |
2609 | ||
2610 | FUSE2FS_CHECK_CONTEXT(ff); | |
2611 | fs = ff->fs; | |
2612 | pthread_mutex_lock(&ff->bfl); | |
7889640d | 2613 | if (!ext2fs_has_feature_xattr(fs->super)) { |
81cbf1ef DW |
2614 | ret = -ENOTSUP; |
2615 | goto out; | |
2616 | } | |
2617 | ||
2618 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
2619 | if (err || ino == 0) { | |
2620 | ret = translate_error(fs, 0, err); | |
2621 | goto out; | |
2622 | } | |
2623 | dbg_printf("%s: ino=%d\n", __func__, ino); | |
2624 | ||
2625 | ret = check_inum_access(fs, ino, W_OK); | |
2626 | if (ret == -EACCES) { | |
2627 | ret = -EPERM; | |
2628 | goto out; | |
2629 | } else if (ret) | |
2630 | goto out; | |
2631 | ||
2632 | err = ext2fs_xattrs_open(fs, ino, &h); | |
2633 | if (err) { | |
2634 | ret = translate_error(fs, ino, err); | |
2635 | goto out; | |
2636 | } | |
2637 | ||
2638 | err = ext2fs_xattrs_read(h); | |
2639 | if (err) { | |
2640 | ret = translate_error(fs, ino, err); | |
2641 | goto out2; | |
2642 | } | |
2643 | ||
4e222d9b | 2644 | cvalue = value; |
81cbf1ef DW |
2645 | clen = len; |
2646 | for (xt = xattr_translators; xt->prefix != NULL; xt++) { | |
2647 | if (strncmp(key, xt->prefix, strlen(xt->prefix)) == 0) { | |
2648 | err = xt->set(value, len, &cvalue, &clen); | |
2649 | if (err) | |
2650 | goto out3; | |
2651 | } | |
2652 | } | |
2653 | ||
2654 | err = ext2fs_xattr_set(h, key, cvalue, clen); | |
2655 | if (err) { | |
2656 | ret = translate_error(fs, ino, err); | |
2657 | goto out3; | |
2658 | } | |
2659 | ||
81cbf1ef DW |
2660 | ret = update_ctime(fs, ino, NULL); |
2661 | out3: | |
2662 | if (cvalue != value) | |
2663 | ext2fs_free_mem(&cvalue); | |
2664 | out2: | |
2665 | err = ext2fs_xattrs_close(&h); | |
2666 | if (!ret && err) | |
2667 | ret = translate_error(fs, ino, err); | |
2668 | out: | |
2669 | pthread_mutex_unlock(&ff->bfl); | |
2670 | ||
2671 | return ret; | |
2672 | } | |
2673 | ||
2674 | static int op_removexattr(const char *path, const char *key) | |
2675 | { | |
2676 | struct fuse_context *ctxt = fuse_get_context(); | |
2677 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
2678 | ext2_filsys fs; | |
2679 | struct ext2_xattr_handle *h; | |
2680 | ext2_ino_t ino; | |
2681 | errcode_t err; | |
2682 | int ret = 0; | |
2683 | ||
2684 | FUSE2FS_CHECK_CONTEXT(ff); | |
2685 | fs = ff->fs; | |
2686 | pthread_mutex_lock(&ff->bfl); | |
7889640d | 2687 | if (!ext2fs_has_feature_xattr(fs->super)) { |
81cbf1ef DW |
2688 | ret = -ENOTSUP; |
2689 | goto out; | |
2690 | } | |
2691 | ||
2692 | if (!fs_can_allocate(ff, 1)) { | |
2693 | ret = -ENOSPC; | |
2694 | goto out; | |
2695 | } | |
2696 | ||
2697 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
2698 | if (err || ino == 0) { | |
2699 | ret = translate_error(fs, 0, err); | |
2700 | goto out; | |
2701 | } | |
2702 | dbg_printf("%s: ino=%d\n", __func__, ino); | |
2703 | ||
2704 | ret = check_inum_access(fs, ino, W_OK); | |
2705 | if (ret) | |
2706 | goto out; | |
2707 | ||
2708 | err = ext2fs_xattrs_open(fs, ino, &h); | |
2709 | if (err) { | |
2710 | ret = translate_error(fs, ino, err); | |
2711 | goto out; | |
2712 | } | |
2713 | ||
2714 | err = ext2fs_xattrs_read(h); | |
2715 | if (err) { | |
2716 | ret = translate_error(fs, ino, err); | |
2717 | goto out2; | |
2718 | } | |
2719 | ||
2720 | err = ext2fs_xattr_remove(h, key); | |
2721 | if (err) { | |
2722 | ret = translate_error(fs, ino, err); | |
2723 | goto out2; | |
2724 | } | |
2725 | ||
81cbf1ef DW |
2726 | ret = update_ctime(fs, ino, NULL); |
2727 | out2: | |
2728 | err = ext2fs_xattrs_close(&h); | |
2729 | if (err) | |
2730 | ret = translate_error(fs, ino, err); | |
2731 | out: | |
2732 | pthread_mutex_unlock(&ff->bfl); | |
2733 | ||
2734 | return ret; | |
2735 | } | |
2736 | ||
2737 | struct readdir_iter { | |
2738 | void *buf; | |
2739 | fuse_fill_dir_t func; | |
2740 | }; | |
2741 | ||
4e222d9b TT |
2742 | static int op_readdir_iter(ext2_ino_t dir EXT2FS_ATTR((unused)), |
2743 | int entry EXT2FS_ATTR((unused)), | |
2744 | struct ext2_dir_entry *dirent, | |
2745 | int offset EXT2FS_ATTR((unused)), | |
2746 | int blocksize EXT2FS_ATTR((unused)), | |
2747 | char *buf EXT2FS_ATTR((unused)), void *data) | |
81cbf1ef DW |
2748 | { |
2749 | struct readdir_iter *i = data; | |
2750 | char namebuf[EXT2_NAME_LEN + 1]; | |
2751 | int ret; | |
2752 | ||
2753 | memcpy(namebuf, dirent->name, dirent->name_len & 0xFF); | |
2754 | namebuf[dirent->name_len & 0xFF] = 0; | |
2755 | ret = i->func(i->buf, namebuf, NULL, 0); | |
2756 | if (ret) | |
2757 | return DIRENT_ABORT; | |
2758 | ||
2759 | return 0; | |
2760 | } | |
2761 | ||
4e222d9b TT |
2762 | static int op_readdir(const char *path EXT2FS_ATTR((unused)), |
2763 | void *buf, fuse_fill_dir_t fill_func, | |
2764 | off_t offset EXT2FS_ATTR((unused)), | |
2765 | struct fuse_file_info *fp) | |
81cbf1ef DW |
2766 | { |
2767 | struct fuse_context *ctxt = fuse_get_context(); | |
2768 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
377e3a96 TT |
2769 | struct fuse2fs_file_handle *fh = |
2770 | (struct fuse2fs_file_handle *)(uintptr_t)fp->fh; | |
81cbf1ef DW |
2771 | ext2_filsys fs; |
2772 | errcode_t err; | |
2773 | struct readdir_iter i; | |
2774 | int ret = 0; | |
2775 | ||
2776 | FUSE2FS_CHECK_CONTEXT(ff); | |
2777 | fs = ff->fs; | |
2778 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
2779 | dbg_printf("%s: ino=%d\n", __func__, fh->ino); | |
2780 | pthread_mutex_lock(&ff->bfl); | |
2781 | i.buf = buf; | |
2782 | i.func = fill_func; | |
2783 | err = ext2fs_dir_iterate2(fs, fh->ino, 0, NULL, op_readdir_iter, &i); | |
2784 | if (err) { | |
2785 | ret = translate_error(fs, fh->ino, err); | |
2786 | goto out; | |
2787 | } | |
2788 | ||
2789 | if (fs_writeable(fs)) { | |
2790 | ret = update_atime(fs, fh->ino); | |
2791 | if (ret) | |
2792 | goto out; | |
2793 | } | |
2794 | out: | |
2795 | pthread_mutex_unlock(&ff->bfl); | |
2796 | return ret; | |
2797 | } | |
2798 | ||
2799 | static int op_access(const char *path, int mask) | |
2800 | { | |
2801 | struct fuse_context *ctxt = fuse_get_context(); | |
2802 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
2803 | ext2_filsys fs; | |
2804 | errcode_t err; | |
2805 | ext2_ino_t ino; | |
2806 | int ret = 0; | |
2807 | ||
2808 | FUSE2FS_CHECK_CONTEXT(ff); | |
2809 | fs = ff->fs; | |
2810 | dbg_printf("%s: path=%s mask=0x%x\n", __func__, path, mask); | |
2811 | pthread_mutex_lock(&ff->bfl); | |
2812 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
2813 | if (err || ino == 0) { | |
2814 | ret = translate_error(fs, 0, err); | |
2815 | goto out; | |
2816 | } | |
2817 | ||
2818 | ret = check_inum_access(fs, ino, mask); | |
2819 | if (ret) | |
2820 | goto out; | |
2821 | ||
2822 | out: | |
2823 | pthread_mutex_unlock(&ff->bfl); | |
2824 | return ret; | |
2825 | } | |
2826 | ||
2827 | static int op_create(const char *path, mode_t mode, struct fuse_file_info *fp) | |
2828 | { | |
2829 | struct fuse_context *ctxt = fuse_get_context(); | |
2830 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
2831 | ext2_filsys fs; | |
2832 | ext2_ino_t parent, child; | |
da9d5b80 | 2833 | char *temp_path; |
81cbf1ef DW |
2834 | errcode_t err; |
2835 | char *node_name, a; | |
2836 | int filetype; | |
2837 | struct ext2_inode_large inode; | |
2838 | int ret = 0; | |
2839 | ||
2840 | FUSE2FS_CHECK_CONTEXT(ff); | |
2841 | fs = ff->fs; | |
2842 | dbg_printf("%s: path=%s mode=0%o\n", __func__, path, mode); | |
da9d5b80 | 2843 | temp_path = strdup(path); |
81cbf1ef DW |
2844 | if (!temp_path) { |
2845 | ret = -ENOMEM; | |
2846 | goto out; | |
2847 | } | |
2848 | node_name = strrchr(temp_path, '/'); | |
2849 | if (!node_name) { | |
2850 | ret = -ENOMEM; | |
2851 | goto out; | |
2852 | } | |
2853 | node_name++; | |
2854 | a = *node_name; | |
2855 | *node_name = 0; | |
2856 | ||
2857 | pthread_mutex_lock(&ff->bfl); | |
2858 | if (!fs_can_allocate(ff, 1)) { | |
2859 | ret = -ENOSPC; | |
2860 | goto out2; | |
2861 | } | |
2862 | ||
2863 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path, | |
2864 | &parent); | |
2865 | if (err) { | |
2866 | ret = translate_error(fs, 0, err); | |
2867 | goto out2; | |
2868 | } | |
2869 | ||
2870 | ret = check_inum_access(fs, parent, W_OK); | |
2871 | if (ret) | |
2872 | goto out2; | |
2873 | ||
2874 | *node_name = a; | |
2875 | ||
2876 | filetype = ext2_file_type(mode); | |
2877 | ||
2878 | err = ext2fs_new_inode(fs, parent, mode, 0, &child); | |
2879 | if (err) { | |
2880 | ret = translate_error(fs, parent, err); | |
2881 | goto out2; | |
2882 | } | |
2883 | ||
2884 | dbg_printf("%s: creating ino=%d/name=%s in dir=%d\n", __func__, child, | |
2885 | node_name, parent); | |
2886 | err = ext2fs_link(fs, parent, node_name, child, filetype); | |
2887 | if (err == EXT2_ET_DIR_NO_SPACE) { | |
2888 | err = ext2fs_expand_dir(fs, parent); | |
2889 | if (err) { | |
2890 | ret = translate_error(fs, parent, err); | |
2891 | goto out2; | |
2892 | } | |
2893 | ||
2894 | err = ext2fs_link(fs, parent, node_name, child, | |
2895 | filetype); | |
2896 | } | |
2897 | if (err) { | |
2898 | ret = translate_error(fs, parent, err); | |
2899 | goto out2; | |
2900 | } | |
2901 | ||
2902 | ret = update_mtime(fs, parent, NULL); | |
2903 | if (ret) | |
2904 | goto out2; | |
2905 | ||
2906 | memset(&inode, 0, sizeof(inode)); | |
2907 | inode.i_mode = mode; | |
2908 | inode.i_links_count = 1; | |
2909 | inode.i_extra_isize = sizeof(struct ext2_inode_large) - | |
2910 | EXT2_GOOD_OLD_INODE_SIZE; | |
f150bdec | 2911 | inode.i_uid = ctxt->uid; |
dae1ecc2 | 2912 | ext2fs_set_i_uid_high(inode, ctxt->uid >> 16); |
f150bdec | 2913 | inode.i_gid = ctxt->gid; |
dae1ecc2 | 2914 | ext2fs_set_i_gid_high(inode, ctxt->gid >> 16); |
7889640d | 2915 | if (ext2fs_has_feature_extents(fs->super)) { |
81cbf1ef DW |
2916 | ext2_extent_handle_t handle; |
2917 | ||
2918 | inode.i_flags &= ~EXT4_EXTENTS_FL; | |
2919 | ret = ext2fs_extent_open2(fs, child, | |
2920 | (struct ext2_inode *)&inode, &handle); | |
2921 | if (ret) | |
2922 | return ret; | |
2923 | ext2fs_extent_free(handle); | |
2924 | } | |
2925 | ||
2926 | err = ext2fs_write_new_inode(fs, child, (struct ext2_inode *)&inode); | |
2927 | if (err) { | |
2928 | ret = translate_error(fs, child, err); | |
2929 | goto out2; | |
2930 | } | |
2931 | ||
2932 | inode.i_generation = ff->next_generation++; | |
2933 | init_times(&inode); | |
2934 | err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode, | |
2935 | sizeof(inode)); | |
2936 | if (err) { | |
2937 | ret = translate_error(fs, child, err); | |
2938 | goto out2; | |
2939 | } | |
2940 | ||
2941 | ext2fs_inode_alloc_stats2(fs, child, 1, 0); | |
2942 | ||
2943 | ret = __op_open(ff, path, fp); | |
2944 | if (ret) | |
2945 | goto out2; | |
2946 | out2: | |
2947 | pthread_mutex_unlock(&ff->bfl); | |
2948 | out: | |
2949 | free(temp_path); | |
2950 | return ret; | |
2951 | } | |
2952 | ||
4e222d9b TT |
2953 | static int op_ftruncate(const char *path EXT2FS_ATTR((unused)), |
2954 | off_t len, struct fuse_file_info *fp) | |
81cbf1ef DW |
2955 | { |
2956 | struct fuse_context *ctxt = fuse_get_context(); | |
2957 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
377e3a96 TT |
2958 | struct fuse2fs_file_handle *fh = |
2959 | (struct fuse2fs_file_handle *)(uintptr_t)fp->fh; | |
81cbf1ef DW |
2960 | ext2_filsys fs; |
2961 | ext2_file_t efp; | |
2962 | errcode_t err; | |
2963 | int ret = 0; | |
2964 | ||
2965 | FUSE2FS_CHECK_CONTEXT(ff); | |
2966 | fs = ff->fs; | |
2967 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
2968 | dbg_printf("%s: ino=%d len=%jd\n", __func__, fh->ino, len); | |
2969 | pthread_mutex_lock(&ff->bfl); | |
2970 | if (!fs_writeable(fs)) { | |
2971 | ret = -EROFS; | |
2972 | goto out; | |
2973 | } | |
2974 | ||
2975 | err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp); | |
2976 | if (err) { | |
2977 | ret = translate_error(fs, fh->ino, err); | |
2978 | goto out; | |
2979 | } | |
2980 | ||
2981 | err = ext2fs_file_set_size2(efp, len); | |
2982 | if (err) { | |
2983 | ret = translate_error(fs, fh->ino, err); | |
2984 | goto out2; | |
2985 | } | |
2986 | ||
2987 | out2: | |
2988 | err = ext2fs_file_close(efp); | |
2989 | if (ret) | |
2990 | goto out; | |
2991 | if (err) { | |
2992 | ret = translate_error(fs, fh->ino, err); | |
2993 | goto out; | |
2994 | } | |
2995 | ||
2996 | ret = update_mtime(fs, fh->ino, NULL); | |
2997 | if (ret) | |
2998 | goto out; | |
2999 | ||
3000 | out: | |
3001 | pthread_mutex_unlock(&ff->bfl); | |
3002 | return 0; | |
3003 | } | |
3004 | ||
4e222d9b TT |
3005 | static int op_fgetattr(const char *path EXT2FS_ATTR((unused)), |
3006 | struct stat *statbuf, | |
81cbf1ef DW |
3007 | struct fuse_file_info *fp) |
3008 | { | |
3009 | struct fuse_context *ctxt = fuse_get_context(); | |
3010 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
3011 | ext2_filsys fs; | |
377e3a96 TT |
3012 | struct fuse2fs_file_handle *fh = |
3013 | (struct fuse2fs_file_handle *)(uintptr_t)fp->fh; | |
81cbf1ef DW |
3014 | int ret = 0; |
3015 | ||
3016 | FUSE2FS_CHECK_CONTEXT(ff); | |
3017 | fs = ff->fs; | |
3018 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
3019 | dbg_printf("%s: ino=%d\n", __func__, fh->ino); | |
3020 | pthread_mutex_lock(&ff->bfl); | |
3021 | ret = stat_inode(fs, fh->ino, statbuf); | |
3022 | pthread_mutex_unlock(&ff->bfl); | |
3023 | ||
3024 | return ret; | |
3025 | } | |
3026 | ||
3027 | static int op_utimens(const char *path, const struct timespec ctv[2]) | |
3028 | { | |
3029 | struct fuse_context *ctxt = fuse_get_context(); | |
3030 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
4e222d9b | 3031 | struct timespec tv[2]; |
81cbf1ef DW |
3032 | ext2_filsys fs; |
3033 | errcode_t err; | |
3034 | ext2_ino_t ino; | |
3035 | struct ext2_inode_large inode; | |
3036 | int ret = 0; | |
3037 | ||
3038 | FUSE2FS_CHECK_CONTEXT(ff); | |
3039 | fs = ff->fs; | |
3040 | pthread_mutex_lock(&ff->bfl); | |
3041 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
3042 | if (err) { | |
3043 | ret = translate_error(fs, 0, err); | |
3044 | goto out; | |
3045 | } | |
3046 | dbg_printf("%s: ino=%d\n", __func__, ino); | |
3047 | ||
3048 | ret = check_inum_access(fs, ino, W_OK); | |
3049 | if (ret) | |
3050 | goto out; | |
3051 | ||
3052 | memset(&inode, 0, sizeof(inode)); | |
3053 | err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
3054 | sizeof(inode)); | |
3055 | if (err) { | |
3056 | ret = translate_error(fs, ino, err); | |
3057 | goto out; | |
3058 | } | |
3059 | ||
4e222d9b TT |
3060 | tv[0] = ctv[0]; |
3061 | tv[1] = ctv[1]; | |
81cbf1ef DW |
3062 | #ifdef UTIME_NOW |
3063 | if (tv[0].tv_nsec == UTIME_NOW) | |
3064 | get_now(tv); | |
3065 | if (tv[1].tv_nsec == UTIME_NOW) | |
3066 | get_now(tv + 1); | |
3067 | #endif /* UTIME_NOW */ | |
3068 | #ifdef UTIME_OMIT | |
3069 | if (tv[0].tv_nsec != UTIME_OMIT) | |
3070 | EXT4_INODE_SET_XTIME(i_atime, tv, &inode); | |
3071 | if (tv[1].tv_nsec != UTIME_OMIT) | |
3072 | EXT4_INODE_SET_XTIME(i_mtime, tv + 1, &inode); | |
3073 | #endif /* UTIME_OMIT */ | |
3074 | ret = update_ctime(fs, ino, &inode); | |
3075 | if (ret) | |
3076 | goto out; | |
3077 | ||
3078 | err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode, | |
3079 | sizeof(inode)); | |
3080 | if (err) { | |
3081 | ret = translate_error(fs, ino, err); | |
3082 | goto out; | |
3083 | } | |
3084 | ||
3085 | out: | |
3086 | pthread_mutex_unlock(&ff->bfl); | |
3087 | return ret; | |
3088 | } | |
3089 | ||
3090 | #ifdef SUPPORT_I_FLAGS | |
3091 | static int ioctl_getflags(ext2_filsys fs, struct fuse2fs_file_handle *fh, | |
3092 | void *data) | |
3093 | { | |
3094 | errcode_t err; | |
3095 | struct ext2_inode_large inode; | |
3096 | ||
3097 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
3098 | dbg_printf("%s: ino=%d\n", __func__, fh->ino); | |
3099 | memset(&inode, 0, sizeof(inode)); | |
3100 | err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode, | |
3101 | sizeof(inode)); | |
3102 | if (err) | |
3103 | return translate_error(fs, fh->ino, err); | |
3104 | ||
3105 | *(__u32 *)data = inode.i_flags & EXT2_FL_USER_VISIBLE; | |
3106 | return 0; | |
3107 | } | |
3108 | ||
3109 | #define FUSE2FS_MODIFIABLE_IFLAGS \ | |
3110 | (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL | EXT2_NODUMP_FL | \ | |
3111 | EXT2_NOATIME_FL | EXT3_JOURNAL_DATA_FL | EXT2_DIRSYNC_FL | \ | |
3112 | EXT2_TOPDIR_FL) | |
3113 | ||
3114 | static int ioctl_setflags(ext2_filsys fs, struct fuse2fs_file_handle *fh, | |
3115 | void *data) | |
3116 | { | |
3117 | errcode_t err; | |
3118 | struct ext2_inode_large inode; | |
3119 | int ret; | |
3120 | __u32 flags = *(__u32 *)data; | |
3121 | struct fuse_context *ctxt = fuse_get_context(); | |
ea1a3fa2 | 3122 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; |
81cbf1ef DW |
3123 | |
3124 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
3125 | dbg_printf("%s: ino=%d\n", __func__, fh->ino); | |
3126 | memset(&inode, 0, sizeof(inode)); | |
3127 | err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode, | |
3128 | sizeof(inode)); | |
3129 | if (err) | |
3130 | return translate_error(fs, fh->ino, err); | |
3131 | ||
dae1ecc2 | 3132 | if (!ff->fakeroot && ctxt->uid != 0 && inode_uid(inode) != ctxt->uid) |
81cbf1ef DW |
3133 | return -EPERM; |
3134 | ||
3135 | if ((inode.i_flags ^ flags) & ~FUSE2FS_MODIFIABLE_IFLAGS) | |
3136 | return -EINVAL; | |
3137 | ||
3138 | inode.i_flags = (inode.i_flags & ~FUSE2FS_MODIFIABLE_IFLAGS) | | |
3139 | (flags & FUSE2FS_MODIFIABLE_IFLAGS); | |
3140 | ||
3141 | ret = update_ctime(fs, fh->ino, &inode); | |
3142 | if (ret) | |
3143 | return ret; | |
3144 | ||
3145 | err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode, | |
3146 | sizeof(inode)); | |
3147 | if (err) | |
3148 | return translate_error(fs, fh->ino, err); | |
3149 | ||
3150 | return 0; | |
3151 | } | |
3152 | ||
3153 | static int ioctl_getversion(ext2_filsys fs, struct fuse2fs_file_handle *fh, | |
3154 | void *data) | |
3155 | { | |
3156 | errcode_t err; | |
3157 | struct ext2_inode_large inode; | |
3158 | ||
3159 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
3160 | dbg_printf("%s: ino=%d\n", __func__, fh->ino); | |
3161 | memset(&inode, 0, sizeof(inode)); | |
3162 | err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode, | |
3163 | sizeof(inode)); | |
3164 | if (err) | |
3165 | return translate_error(fs, fh->ino, err); | |
3166 | ||
3167 | *(__u32 *)data = inode.i_generation; | |
3168 | return 0; | |
3169 | } | |
3170 | ||
3171 | static int ioctl_setversion(ext2_filsys fs, struct fuse2fs_file_handle *fh, | |
3172 | void *data) | |
3173 | { | |
3174 | errcode_t err; | |
3175 | struct ext2_inode_large inode; | |
3176 | int ret; | |
3177 | __u32 generation = *(__u32 *)data; | |
3178 | struct fuse_context *ctxt = fuse_get_context(); | |
ea1a3fa2 | 3179 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; |
81cbf1ef DW |
3180 | |
3181 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
3182 | dbg_printf("%s: ino=%d\n", __func__, fh->ino); | |
3183 | memset(&inode, 0, sizeof(inode)); | |
3184 | err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode, | |
3185 | sizeof(inode)); | |
3186 | if (err) | |
3187 | return translate_error(fs, fh->ino, err); | |
3188 | ||
dae1ecc2 | 3189 | if (!ff->fakeroot && ctxt->uid != 0 && inode_uid(inode) != ctxt->uid) |
81cbf1ef DW |
3190 | return -EPERM; |
3191 | ||
3192 | inode.i_generation = generation; | |
3193 | ||
3194 | ret = update_ctime(fs, fh->ino, &inode); | |
3195 | if (ret) | |
3196 | return ret; | |
3197 | ||
3198 | err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode, | |
3199 | sizeof(inode)); | |
3200 | if (err) | |
3201 | return translate_error(fs, fh->ino, err); | |
3202 | ||
3203 | return 0; | |
3204 | } | |
3205 | #endif /* SUPPORT_I_FLAGS */ | |
3206 | ||
3207 | #ifdef FITRIM | |
3208 | static int ioctl_fitrim(ext2_filsys fs, struct fuse2fs_file_handle *fh, | |
3209 | void *data) | |
3210 | { | |
3211 | struct fstrim_range *fr = data; | |
3212 | blk64_t start, end, max_blocks, b, cleared; | |
3213 | errcode_t err = 0; | |
3214 | ||
3215 | start = fr->start / fs->blocksize; | |
3216 | end = (fr->start + fr->len - 1) / fs->blocksize; | |
3217 | dbg_printf("%s: start=%llu end=%llu\n", __func__, start, end); | |
3218 | ||
3219 | if (start < fs->super->s_first_data_block) | |
3220 | start = fs->super->s_first_data_block; | |
3221 | if (start >= ext2fs_blocks_count(fs->super)) | |
3222 | start = ext2fs_blocks_count(fs->super) - 1; | |
3223 | ||
3224 | if (end < fs->super->s_first_data_block) | |
3225 | end = fs->super->s_first_data_block; | |
3226 | if (end >= ext2fs_blocks_count(fs->super)) | |
3227 | end = ext2fs_blocks_count(fs->super) - 1; | |
3228 | ||
3229 | cleared = 0; | |
3230 | max_blocks = 2048ULL * 1024 * 1024 / fs->blocksize; | |
3231 | ||
3232 | fr->len = 0; | |
3233 | while (start <= end) { | |
3234 | err = ext2fs_find_first_zero_block_bitmap2(fs->block_map, | |
3235 | start, end, &start); | |
3236 | if (err == ENOENT) | |
3237 | return 0; | |
3238 | else if (err) | |
3239 | return translate_error(fs, fh->ino, err); | |
3240 | ||
3241 | b = start + max_blocks < end ? start + max_blocks : end; | |
3242 | err = ext2fs_find_first_set_block_bitmap2(fs->block_map, | |
3243 | start, b, &b); | |
3244 | if (err && err != ENOENT) | |
3245 | return translate_error(fs, fh->ino, err); | |
3246 | if (b - start >= fr->minlen) { | |
3247 | err = io_channel_discard(fs->io, start, b - start); | |
3248 | if (err) | |
3249 | return translate_error(fs, fh->ino, err); | |
3250 | cleared += b - start; | |
3251 | fr->len = cleared * fs->blocksize; | |
3252 | } | |
3253 | start = b + 1; | |
3254 | } | |
3255 | ||
3256 | return err; | |
3257 | } | |
3258 | #endif /* FITRIM */ | |
3259 | ||
3260 | #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8) | |
4e222d9b TT |
3261 | static int op_ioctl(const char *path EXT2FS_ATTR((unused)), int cmd, |
3262 | void *arg EXT2FS_ATTR((unused)), | |
3263 | struct fuse_file_info *fp, | |
3264 | unsigned int flags EXT2FS_ATTR((unused)), void *data) | |
81cbf1ef DW |
3265 | { |
3266 | struct fuse_context *ctxt = fuse_get_context(); | |
3267 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
377e3a96 TT |
3268 | struct fuse2fs_file_handle *fh = |
3269 | (struct fuse2fs_file_handle *)(uintptr_t)fp->fh; | |
81cbf1ef DW |
3270 | ext2_filsys fs; |
3271 | int ret = 0; | |
3272 | ||
3273 | FUSE2FS_CHECK_CONTEXT(ff); | |
3274 | fs = ff->fs; | |
3275 | pthread_mutex_lock(&ff->bfl); | |
4e222d9b | 3276 | switch ((unsigned long) cmd) { |
81cbf1ef DW |
3277 | #ifdef SUPPORT_I_FLAGS |
3278 | case EXT2_IOC_GETFLAGS: | |
3279 | ret = ioctl_getflags(fs, fh, data); | |
3280 | break; | |
3281 | case EXT2_IOC_SETFLAGS: | |
3282 | ret = ioctl_setflags(fs, fh, data); | |
3283 | break; | |
3284 | case EXT2_IOC_GETVERSION: | |
3285 | ret = ioctl_getversion(fs, fh, data); | |
3286 | break; | |
3287 | case EXT2_IOC_SETVERSION: | |
3288 | ret = ioctl_setversion(fs, fh, data); | |
3289 | break; | |
3290 | #endif | |
3291 | #ifdef FITRIM | |
3292 | case FITRIM: | |
3293 | ret = ioctl_fitrim(fs, fh, data); | |
3294 | break; | |
3295 | #endif | |
3296 | default: | |
3297 | dbg_printf("%s: Unknown ioctl %d\n", __func__, cmd); | |
3298 | ret = -ENOTTY; | |
3299 | } | |
3300 | pthread_mutex_unlock(&ff->bfl); | |
3301 | ||
3302 | return ret; | |
3303 | } | |
3304 | #endif /* FUSE 28 */ | |
3305 | ||
4e222d9b TT |
3306 | static int op_bmap(const char *path, size_t blocksize EXT2FS_ATTR((unused)), |
3307 | uint64_t *idx) | |
81cbf1ef DW |
3308 | { |
3309 | struct fuse_context *ctxt = fuse_get_context(); | |
3310 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
3311 | ext2_filsys fs; | |
3312 | ext2_ino_t ino; | |
3313 | errcode_t err; | |
3314 | int ret = 0; | |
3315 | ||
3316 | FUSE2FS_CHECK_CONTEXT(ff); | |
3317 | fs = ff->fs; | |
3318 | pthread_mutex_lock(&ff->bfl); | |
3319 | err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino); | |
3320 | if (err) { | |
3321 | ret = translate_error(fs, 0, err); | |
3322 | goto out; | |
3323 | } | |
3324 | dbg_printf("%s: ino=%d blk=%"PRIu64"\n", __func__, ino, *idx); | |
3325 | ||
3326 | err = ext2fs_bmap2(fs, ino, NULL, NULL, 0, *idx, 0, (blk64_t *)idx); | |
3327 | if (err) { | |
3328 | ret = translate_error(fs, ino, err); | |
3329 | goto out; | |
3330 | } | |
3331 | ||
3332 | out: | |
3333 | pthread_mutex_unlock(&ff->bfl); | |
3334 | return ret; | |
3335 | } | |
3336 | ||
3337 | #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9) | |
3338 | # ifdef SUPPORT_FALLOCATE | |
3339 | static int fallocate_helper(struct fuse_file_info *fp, int mode, off_t offset, | |
3340 | off_t len) | |
3341 | { | |
3342 | struct fuse_context *ctxt = fuse_get_context(); | |
3343 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
377e3a96 TT |
3344 | struct fuse2fs_file_handle *fh = |
3345 | (struct fuse2fs_file_handle *)(uintptr_t)fp->fh; | |
81cbf1ef DW |
3346 | ext2_filsys fs; |
3347 | struct ext2_inode_large inode; | |
3348 | blk64_t start, end; | |
3349 | __u64 fsize; | |
3350 | errcode_t err; | |
3351 | int flags; | |
3352 | ||
3353 | FUSE2FS_CHECK_CONTEXT(ff); | |
3354 | fs = ff->fs; | |
3355 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
3356 | start = offset / fs->blocksize; | |
3357 | end = (offset + len - 1) / fs->blocksize; | |
3358 | dbg_printf("%s: ino=%d mode=0x%x start=%jd end=%llu\n", __func__, | |
3359 | fh->ino, mode, offset / fs->blocksize, end); | |
3360 | if (!fs_can_allocate(ff, len / fs->blocksize)) | |
3361 | return -ENOSPC; | |
3362 | ||
3363 | memset(&inode, 0, sizeof(inode)); | |
3364 | err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode, | |
3365 | sizeof(inode)); | |
3366 | if (err) | |
3367 | return err; | |
3368 | fsize = EXT2_I_SIZE(&inode); | |
3369 | ||
3370 | /* Allocate a bunch of blocks */ | |
3371 | flags = (mode & FL_KEEP_SIZE_FLAG ? 0 : | |
3372 | EXT2_FALLOCATE_INIT_BEYOND_EOF); | |
3373 | err = ext2fs_fallocate(fs, flags, fh->ino, | |
3374 | (struct ext2_inode *)&inode, | |
3375 | ~0ULL, start, end - start + 1); | |
3376 | if (err && err != EXT2_ET_BLOCK_ALLOC_FAIL) | |
3377 | return translate_error(fs, fh->ino, err); | |
3378 | ||
3379 | /* Update i_size */ | |
3380 | if (!(mode & FL_KEEP_SIZE_FLAG)) { | |
4e222d9b | 3381 | if ((__u64) offset + len > fsize) { |
81cbf1ef DW |
3382 | err = ext2fs_inode_size_set(fs, |
3383 | (struct ext2_inode *)&inode, | |
3384 | offset + len); | |
3385 | if (err) | |
3386 | return translate_error(fs, fh->ino, err); | |
3387 | } | |
3388 | } | |
3389 | ||
3390 | err = update_mtime(fs, fh->ino, &inode); | |
3391 | if (err) | |
3392 | return err; | |
3393 | ||
3394 | err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode, | |
3395 | sizeof(inode)); | |
3396 | if (err) | |
3397 | return translate_error(fs, fh->ino, err); | |
3398 | ||
3399 | return err; | |
3400 | } | |
3401 | ||
3402 | static errcode_t clean_block_middle(ext2_filsys fs, ext2_ino_t ino, | |
3403 | struct ext2_inode_large *inode, off_t offset, | |
3404 | off_t len, char **buf) | |
3405 | { | |
3406 | blk64_t blk; | |
3407 | off_t residue; | |
3408 | int retflags; | |
3409 | errcode_t err; | |
3410 | ||
3411 | residue = offset % fs->blocksize; | |
3412 | if (residue == 0) | |
3413 | return 0; | |
3414 | ||
3415 | if (!*buf) { | |
3416 | err = ext2fs_get_mem(fs->blocksize, buf); | |
3417 | if (err) | |
3418 | return err; | |
3419 | } | |
3420 | ||
3421 | err = ext2fs_bmap2(fs, ino, (struct ext2_inode *)inode, *buf, 0, | |
3422 | offset / fs->blocksize, &retflags, &blk); | |
3423 | if (err) | |
3424 | return err; | |
3425 | if (!blk || (retflags & BMAP_RET_UNINIT)) | |
3426 | return 0; | |
3427 | ||
3428 | err = io_channel_read_blk(fs->io, blk, 1, *buf); | |
3429 | if (err) | |
3430 | return err; | |
3431 | ||
3432 | memset(*buf + residue, 0, len); | |
3433 | ||
3434 | return io_channel_write_blk(fs->io, blk, 1, *buf); | |
3435 | } | |
3436 | ||
3437 | static errcode_t clean_block_edge(ext2_filsys fs, ext2_ino_t ino, | |
3438 | struct ext2_inode_large *inode, off_t offset, | |
3439 | int clean_before, char **buf) | |
3440 | { | |
3441 | blk64_t blk; | |
3442 | int retflags; | |
3443 | off_t residue; | |
3444 | errcode_t err; | |
3445 | ||
3446 | residue = offset % fs->blocksize; | |
3447 | if (residue == 0) | |
3448 | return 0; | |
3449 | ||
3450 | if (!*buf) { | |
3451 | err = ext2fs_get_mem(fs->blocksize, buf); | |
3452 | if (err) | |
3453 | return err; | |
3454 | } | |
3455 | ||
3456 | err = ext2fs_bmap2(fs, ino, (struct ext2_inode *)inode, *buf, 0, | |
3457 | offset / fs->blocksize, &retflags, &blk); | |
3458 | if (err) | |
3459 | return err; | |
3460 | ||
3461 | err = io_channel_read_blk(fs->io, blk, 1, *buf); | |
3462 | if (err) | |
3463 | return err; | |
3464 | if (!blk || (retflags & BMAP_RET_UNINIT)) | |
3465 | return 0; | |
3466 | ||
3467 | if (clean_before) | |
3468 | memset(*buf, 0, residue); | |
3469 | else | |
3470 | memset(*buf + residue, 0, fs->blocksize - residue); | |
3471 | ||
3472 | return io_channel_write_blk(fs->io, blk, 1, *buf); | |
3473 | } | |
3474 | ||
3475 | static int punch_helper(struct fuse_file_info *fp, int mode, off_t offset, | |
3476 | off_t len) | |
3477 | { | |
3478 | struct fuse_context *ctxt = fuse_get_context(); | |
3479 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
377e3a96 TT |
3480 | struct fuse2fs_file_handle *fh = |
3481 | (struct fuse2fs_file_handle *)(uintptr_t)fp->fh; | |
81cbf1ef DW |
3482 | ext2_filsys fs; |
3483 | struct ext2_inode_large inode; | |
3484 | blk64_t start, end; | |
3485 | errcode_t err; | |
3486 | char *buf = NULL; | |
3487 | ||
3488 | FUSE2FS_CHECK_CONTEXT(ff); | |
3489 | fs = ff->fs; | |
3490 | FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC); | |
3491 | dbg_printf("%s: offset=%jd len=%jd\n", __func__, offset, len); | |
3492 | ||
3493 | /* kernel ext4 punch requires this flag to be set */ | |
3494 | if (!(mode & FL_KEEP_SIZE_FLAG)) | |
3495 | return -EINVAL; | |
3496 | ||
3497 | /* Punch out a bunch of blocks */ | |
3498 | start = (offset + fs->blocksize - 1) / fs->blocksize; | |
3499 | end = (offset + len - fs->blocksize) / fs->blocksize; | |
3500 | dbg_printf("%s: ino=%d mode=0x%x start=%llu end=%llu\n", __func__, | |
3501 | fh->ino, mode, start, end); | |
3502 | ||
3503 | memset(&inode, 0, sizeof(inode)); | |
3504 | err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode, | |
3505 | sizeof(inode)); | |
3506 | if (err) | |
3507 | return translate_error(fs, fh->ino, err); | |
3508 | ||
3509 | /* Zero everything before the first block and after the last block */ | |
3510 | if ((offset / fs->blocksize) == ((offset + len) / fs->blocksize)) | |
3511 | err = clean_block_middle(fs, fh->ino, &inode, offset, | |
3512 | len, &buf); | |
3513 | else { | |
3514 | err = clean_block_edge(fs, fh->ino, &inode, offset, 0, &buf); | |
3515 | if (!err) | |
3516 | err = clean_block_edge(fs, fh->ino, &inode, | |
3517 | offset + len, 1, &buf); | |
3518 | } | |
3519 | if (buf) | |
3520 | ext2fs_free_mem(&buf); | |
3521 | if (err) | |
3522 | return translate_error(fs, fh->ino, err); | |
3523 | ||
3524 | /* Unmap full blocks in the middle */ | |
3525 | if (start <= end) { | |
3526 | err = ext2fs_punch(fs, fh->ino, (struct ext2_inode *)&inode, | |
3527 | NULL, start, end); | |
3528 | if (err) | |
3529 | return translate_error(fs, fh->ino, err); | |
3530 | } | |
3531 | ||
3532 | err = update_mtime(fs, fh->ino, &inode); | |
3533 | if (err) | |
3534 | return err; | |
3535 | ||
3536 | err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode, | |
3537 | sizeof(inode)); | |
3538 | if (err) | |
3539 | return translate_error(fs, fh->ino, err); | |
3540 | ||
3541 | return 0; | |
3542 | } | |
3543 | ||
4e222d9b TT |
3544 | static int op_fallocate(const char *path EXT2FS_ATTR((unused)), int mode, |
3545 | off_t offset, off_t len, | |
81cbf1ef DW |
3546 | struct fuse_file_info *fp) |
3547 | { | |
3548 | struct fuse_context *ctxt = fuse_get_context(); | |
3549 | struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data; | |
3550 | ext2_filsys fs = ff->fs; | |
3551 | int ret; | |
3552 | ||
3553 | /* Catch unknown flags */ | |
3554 | if (mode & ~(FL_PUNCH_HOLE_FLAG | FL_KEEP_SIZE_FLAG)) | |
3555 | return -EINVAL; | |
3556 | ||
3557 | pthread_mutex_lock(&ff->bfl); | |
3558 | if (!fs_writeable(fs)) { | |
3559 | ret = -EROFS; | |
3560 | goto out; | |
3561 | } | |
3562 | if (mode & FL_PUNCH_HOLE_FLAG) | |
3563 | ret = punch_helper(fp, mode, offset, len); | |
3564 | else | |
3565 | ret = fallocate_helper(fp, mode, offset, len); | |
3566 | out: | |
3567 | pthread_mutex_unlock(&ff->bfl); | |
3568 | ||
3569 | return ret; | |
3570 | } | |
3571 | # endif /* SUPPORT_FALLOCATE */ | |
3572 | #endif /* FUSE 29 */ | |
3573 | ||
3574 | static struct fuse_operations fs_ops = { | |
3575 | .init = op_init, | |
3576 | .destroy = op_destroy, | |
3577 | .getattr = op_getattr, | |
3578 | .readlink = op_readlink, | |
3579 | .mknod = op_mknod, | |
3580 | .mkdir = op_mkdir, | |
3581 | .unlink = op_unlink, | |
3582 | .rmdir = op_rmdir, | |
3583 | .symlink = op_symlink, | |
3584 | .rename = op_rename, | |
3585 | .link = op_link, | |
3586 | .chmod = op_chmod, | |
3587 | .chown = op_chown, | |
3588 | .truncate = op_truncate, | |
3589 | .open = op_open, | |
3590 | .read = op_read, | |
3591 | .write = op_write, | |
3592 | .statfs = op_statfs, | |
3593 | .release = op_release, | |
3594 | .fsync = op_fsync, | |
3595 | .setxattr = op_setxattr, | |
3596 | .getxattr = op_getxattr, | |
3597 | .listxattr = op_listxattr, | |
3598 | .removexattr = op_removexattr, | |
3599 | .opendir = op_open, | |
3600 | .readdir = op_readdir, | |
3601 | .releasedir = op_release, | |
3602 | .fsyncdir = op_fsync, | |
3603 | .access = op_access, | |
3604 | .create = op_create, | |
3605 | .ftruncate = op_ftruncate, | |
3606 | .fgetattr = op_fgetattr, | |
3607 | .utimens = op_utimens, | |
3608 | #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9) | |
3609 | # if defined(UTIME_NOW) || defined(UTIME_OMIT) | |
3610 | .flag_utime_omit_ok = 1, | |
3611 | # endif | |
3612 | #endif | |
3613 | .bmap = op_bmap, | |
3614 | #ifdef SUPERFLUOUS | |
3615 | .lock = op_lock, | |
3616 | .poll = op_poll, | |
3617 | #endif | |
3618 | #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8) | |
3619 | .ioctl = op_ioctl, | |
3620 | .flag_nullpath_ok = 1, | |
3621 | #endif | |
3622 | #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9) | |
3623 | .flag_nopath = 1, | |
3624 | # ifdef SUPPORT_FALLOCATE | |
3625 | .fallocate = op_fallocate, | |
3626 | # endif | |
3627 | #endif | |
3628 | }; | |
3629 | ||
3630 | static int get_random_bytes(void *p, size_t sz) | |
3631 | { | |
3632 | int fd; | |
3633 | ssize_t r; | |
3634 | ||
4e222d9b | 3635 | fd = open("/dev/urandom", O_RDONLY); |
81cbf1ef | 3636 | if (fd < 0) { |
4e222d9b | 3637 | perror("/dev/urandom"); |
81cbf1ef DW |
3638 | return 0; |
3639 | } | |
3640 | ||
3641 | r = read(fd, p, sz); | |
3642 | ||
3643 | close(fd); | |
4e222d9b | 3644 | return (size_t) r == sz; |
81cbf1ef DW |
3645 | } |
3646 | ||
69df8496 TT |
3647 | enum { |
3648 | FUSE2FS_VERSION, | |
3649 | FUSE2FS_HELP, | |
3650 | FUSE2FS_HELPFULL, | |
3651 | }; | |
3652 | ||
3653 | #define FUSE2FS_OPT(t, p, v) { t, offsetof(struct fuse2fs, p), v } | |
3654 | ||
3655 | static struct fuse_opt fuse2fs_opts[] = { | |
3656 | FUSE2FS_OPT("ro", ro, 1), | |
3657 | FUSE2FS_OPT("errors=panic", panic_on_error, 1), | |
3658 | FUSE2FS_OPT("minixdf", minixdf, 1), | |
ea1a3fa2 | 3659 | FUSE2FS_OPT("fakeroot", fakeroot, 1), |
69df8496 TT |
3660 | FUSE2FS_OPT("fuse2fs_debug", debug, 1), |
3661 | FUSE2FS_OPT("no_default_opts", no_default_opts, 1), | |
75e3a9ef | 3662 | FUSE2FS_OPT("norecovery", norecovery, 1), |
69df8496 TT |
3663 | |
3664 | FUSE_OPT_KEY("-V", FUSE2FS_VERSION), | |
3665 | FUSE_OPT_KEY("--version", FUSE2FS_VERSION), | |
3666 | FUSE_OPT_KEY("-h", FUSE2FS_HELP), | |
3667 | FUSE_OPT_KEY("--help", FUSE2FS_HELP), | |
3668 | FUSE_OPT_KEY("--helpfull", FUSE2FS_HELPFULL), | |
3669 | FUSE_OPT_END | |
3670 | }; | |
3671 | ||
3672 | ||
3673 | static int fuse2fs_opt_proc(void *data, const char *arg, | |
3674 | int key, struct fuse_args *outargs) | |
81cbf1ef | 3675 | { |
69df8496 TT |
3676 | struct fuse2fs *ff = data; |
3677 | ||
3678 | switch (key) { | |
3679 | case FUSE_OPT_KEY_NONOPT: | |
3680 | if (!ff->device) { | |
3681 | ff->device = strdup(arg); | |
3682 | return 0; | |
3683 | } | |
3684 | return 1; | |
3685 | case FUSE2FS_HELP: | |
3686 | case FUSE2FS_HELPFULL: | |
3687 | fprintf(stderr, | |
3688 | "usage: %s device/image mountpoint [options]\n" | |
3689 | "\n" | |
3690 | "general options:\n" | |
3691 | " -o opt,[opt...] mount options\n" | |
3692 | " -h --help print help\n" | |
3693 | " -V --version print version\n" | |
3694 | "\n" | |
3695 | "fuse2fs options:\n" | |
3696 | " -o ro read-only mount\n" | |
3697 | " -o errors=panic dump core on error\n" | |
3698 | " -o minixdf minix-style df\n" | |
ea1a3fa2 | 3699 | " -o fakeroot pretend to be root for permission checks\n" |
69df8496 | 3700 | " -o no_default_opts do not include default fuse options\n" |
75e3a9ef | 3701 | " -o norecovery don't replay the journal (implies ro)\n" |
69df8496 TT |
3702 | " -o fuse2fs_debug enable fuse2fs debugging\n" |
3703 | "\n", | |
3704 | outargs->argv[0]); | |
3705 | if (key == FUSE2FS_HELPFULL) { | |
3706 | fuse_opt_add_arg(outargs, "-ho"); | |
3707 | fuse_main(outargs->argc, outargs->argv, &fs_ops, NULL); | |
3708 | } else { | |
3709 | fprintf(stderr, "Try --helpfull to get a list of " | |
3710 | "all flags, including the FUSE options.\n"); | |
3711 | } | |
3712 | exit(1); | |
3713 | ||
3714 | case FUSE2FS_VERSION: | |
3715 | fprintf(stderr, "fuse2fs %s (%s)\n", E2FSPROGS_VERSION, | |
3716 | E2FSPROGS_DATE); | |
3717 | fuse_opt_add_arg(outargs, "--version"); | |
3718 | fuse_main(outargs->argc, outargs->argv, &fs_ops, NULL); | |
3719 | exit(0); | |
3720 | } | |
3721 | return 1; | |
81cbf1ef DW |
3722 | } |
3723 | ||
3724 | int main(int argc, char *argv[]) | |
3725 | { | |
69df8496 TT |
3726 | struct fuse_args args = FUSE_ARGS_INIT(argc, argv); |
3727 | struct fuse2fs fctx; | |
81cbf1ef | 3728 | errcode_t err; |
8feb6f43 | 3729 | char *logfile; |
81cbf1ef | 3730 | char extra_args[BUFSIZ]; |
37d56864 TT |
3731 | int ret = 0; |
3732 | int flags = EXT2_FLAG_64BITS | EXT2_FLAG_THREADS | EXT2_FLAG_EXCLUSIVE; | |
81cbf1ef | 3733 | |
69df8496 TT |
3734 | memset(&fctx, 0, sizeof(fctx)); |
3735 | fctx.magic = FUSE2FS_MAGIC; | |
81cbf1ef | 3736 | |
69df8496 TT |
3737 | fuse_opt_parse(&args, &fctx, fuse2fs_opts, fuse2fs_opt_proc); |
3738 | if (fctx.device == NULL) { | |
3739 | fprintf(stderr, "Missing ext4 device/image\n"); | |
3740 | fprintf(stderr, "See '%s -h' for usage\n", argv[0]); | |
3741 | exit(1); | |
81cbf1ef DW |
3742 | } |
3743 | ||
75e3a9ef TT |
3744 | if (fctx.norecovery) |
3745 | fctx.ro = 1; | |
69df8496 | 3746 | if (fctx.ro) |
81cbf1ef DW |
3747 | printf("%s", _("Mounting read-only.\n")); |
3748 | ||
3749 | #ifdef ENABLE_NLS | |
3750 | setlocale(LC_MESSAGES, ""); | |
3751 | setlocale(LC_CTYPE, ""); | |
3752 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); | |
3753 | textdomain(NLS_CAT_NAME); | |
3754 | set_com_err_gettext(gettext); | |
3755 | #endif | |
3756 | add_error_table(&et_ext2_error_table); | |
3757 | ||
81cbf1ef DW |
3758 | /* Set up error logging */ |
3759 | logfile = getenv("FUSE2FS_LOGFILE"); | |
3760 | if (logfile) { | |
69df8496 TT |
3761 | fctx.err_fp = fopen(logfile, "a"); |
3762 | if (!fctx.err_fp) { | |
81cbf1ef | 3763 | perror(logfile); |
6ae16a68 | 3764 | goto out; |
81cbf1ef DW |
3765 | } |
3766 | } else | |
69df8496 | 3767 | fctx.err_fp = stderr; |
81cbf1ef DW |
3768 | |
3769 | /* Will we allow users to allocate every last block? */ | |
3770 | if (getenv("FUSE2FS_ALLOC_ALL_BLOCKS")) { | |
3771 | printf(_("%s: Allowing users to allocate all blocks. " | |
69df8496 TT |
3772 | "This is dangerous!\n"), fctx.device); |
3773 | fctx.alloc_all_blocks = 1; | |
81cbf1ef DW |
3774 | } |
3775 | ||
3776 | /* Start up the fs (while we still can use stdout) */ | |
3777 | ret = 2; | |
69df8496 | 3778 | if (!fctx.ro) |
81cbf1ef | 3779 | flags |= EXT2_FLAG_RW; |
69df8496 | 3780 | err = ext2fs_open2(fctx.device, NULL, flags, 0, 0, unix_io_manager, |
81cbf1ef DW |
3781 | &global_fs); |
3782 | if (err) { | |
69df8496 TT |
3783 | printf(_("%s: %s.\n"), fctx.device, error_message(err)); |
3784 | printf(_("Please run e2fsck -fy %s.\n"), fctx.device); | |
6ae16a68 | 3785 | goto out; |
81cbf1ef | 3786 | } |
69df8496 TT |
3787 | fctx.fs = global_fs; |
3788 | global_fs->priv_data = &fctx; | |
81cbf1ef DW |
3789 | |
3790 | ret = 3; | |
aca77cad | 3791 | |
7889640d | 3792 | if (ext2fs_has_feature_journal_needs_recovery(global_fs->super)) { |
75e3a9ef TT |
3793 | if (fctx.norecovery) { |
3794 | printf(_("%s: mounting read-only without " | |
3795 | "recovering journal\n"), | |
3796 | fctx.device); | |
3797 | } else if (!fctx.ro) { | |
69df8496 | 3798 | printf(_("%s: recovering journal\n"), fctx.device); |
81cbf1ef DW |
3799 | err = ext2fs_run_ext3_journal(&global_fs); |
3800 | if (err) { | |
69df8496 | 3801 | printf(_("%s: %s.\n"), fctx.device, |
81cbf1ef DW |
3802 | error_message(err)); |
3803 | printf(_("Please run e2fsck -fy %s.\n"), | |
69df8496 | 3804 | fctx.device); |
81cbf1ef DW |
3805 | goto out; |
3806 | } | |
7889640d | 3807 | ext2fs_clear_feature_journal_needs_recovery(global_fs->super); |
81cbf1ef DW |
3808 | ext2fs_mark_super_dirty(global_fs); |
3809 | } else { | |
3810 | printf("%s", _("Journal needs recovery; running " | |
3811 | "`e2fsck -E journal_only' is required.\n")); | |
3812 | goto out; | |
3813 | } | |
3814 | } | |
3815 | ||
69df8496 | 3816 | if (!fctx.ro) { |
7889640d | 3817 | if (ext2fs_has_feature_journal(global_fs->super)) |
81cbf1ef | 3818 | printf(_("%s: Writing to the journal is not supported.\n"), |
69df8496 | 3819 | fctx.device); |
81cbf1ef DW |
3820 | err = ext2fs_read_inode_bitmap(global_fs); |
3821 | if (err) { | |
3822 | translate_error(global_fs, 0, err); | |
3823 | goto out; | |
3824 | } | |
3825 | err = ext2fs_read_block_bitmap(global_fs); | |
3826 | if (err) { | |
3827 | translate_error(global_fs, 0, err); | |
3828 | goto out; | |
3829 | } | |
3830 | } | |
3831 | ||
3832 | if (!(global_fs->super->s_state & EXT2_VALID_FS)) | |
3833 | printf("%s", _("Warning: Mounting unchecked fs, running e2fsck " | |
3834 | "is recommended.\n")); | |
3835 | if (global_fs->super->s_max_mnt_count > 0 && | |
3836 | global_fs->super->s_mnt_count >= global_fs->super->s_max_mnt_count) | |
3837 | printf("%s", _("Warning: Maximal mount count reached, running " | |
3838 | "e2fsck is recommended.\n")); | |
3839 | if (global_fs->super->s_checkinterval > 0 && | |
377e3a96 TT |
3840 | (time_t) (global_fs->super->s_lastcheck + |
3841 | global_fs->super->s_checkinterval) <= time(0)) | |
81cbf1ef DW |
3842 | printf("%s", _("Warning: Check time reached; running e2fsck " |
3843 | "is recommended.\n")); | |
3844 | if (global_fs->super->s_last_orphan) | |
3845 | printf("%s", | |
3846 | _("Orphans detected; running e2fsck is recommended.\n")); | |
3847 | ||
3848 | if (global_fs->super->s_state & EXT2_ERROR_FS) { | |
3849 | printf("%s", | |
3850 | _("Errors detected; running e2fsck is required.\n")); | |
3851 | goto out; | |
3852 | } | |
3853 | ||
3854 | /* Initialize generation counter */ | |
69df8496 | 3855 | get_random_bytes(&fctx.next_generation, sizeof(unsigned int)); |
81cbf1ef | 3856 | |
69df8496 | 3857 | /* Set up default fuse parameters */ |
81cbf1ef | 3858 | snprintf(extra_args, BUFSIZ, "-okernel_cache,subtype=ext4,use_ino," |
69df8496 | 3859 | "fsname=%s,attr_timeout=0" FUSE_PLATFORM_OPTS, |
4dda2315 | 3860 | fctx.device); |
69df8496 TT |
3861 | if (fctx.no_default_opts == 0) |
3862 | fuse_opt_add_arg(&args, extra_args); | |
3863 | ||
ea1a3fa2 NC |
3864 | if (fctx.fakeroot) { |
3865 | #ifdef HAVE_MOUNT_NODEV | |
3866 | fuse_opt_add_arg(&args,"-onodev"); | |
3867 | #endif | |
3868 | #ifdef HAVE_MOUNT_NOSUID | |
3869 | fuse_opt_add_arg(&args,"-onosuid"); | |
3870 | #endif | |
3871 | } | |
3872 | ||
69df8496 TT |
3873 | if (fctx.debug) { |
3874 | int i; | |
3875 | ||
3876 | printf("fuse arguments:"); | |
3877 | for (i = 0; i < args.argc; i++) | |
3878 | printf(" '%s'", args.argv[i]); | |
3879 | printf("\n"); | |
3880 | } | |
81cbf1ef | 3881 | |
69df8496 TT |
3882 | pthread_mutex_init(&fctx.bfl, NULL); |
3883 | fuse_main(args.argc, args.argv, &fs_ops, &fctx); | |
3884 | pthread_mutex_destroy(&fctx.bfl); | |
81cbf1ef DW |
3885 | |
3886 | ret = 0; | |
3887 | out: | |
6ae16a68 TT |
3888 | if (global_fs) { |
3889 | err = ext2fs_close(global_fs); | |
3890 | if (err) | |
3891 | com_err(argv[0], err, "while closing fs"); | |
3892 | global_fs = NULL; | |
3893 | } | |
81cbf1ef DW |
3894 | return ret; |
3895 | } | |
3896 | ||
3897 | static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino, | |
3898 | const char *file, int line) | |
3899 | { | |
3900 | struct timespec now; | |
3901 | int ret = err; | |
3902 | struct fuse2fs *ff = fs->priv_data; | |
3903 | int is_err = 0; | |
3904 | ||
3905 | /* Translate ext2 error to unix error code */ | |
3906 | if (err < EXT2_ET_BASE) | |
3907 | goto no_translation; | |
3908 | switch (err) { | |
3909 | case EXT2_ET_NO_MEMORY: | |
3910 | case EXT2_ET_TDB_ERR_OOM: | |
3911 | ret = -ENOMEM; | |
3912 | break; | |
3913 | case EXT2_ET_INVALID_ARGUMENT: | |
3914 | case EXT2_ET_LLSEEK_FAILED: | |
3915 | ret = -EINVAL; | |
3916 | break; | |
3917 | case EXT2_ET_NO_DIRECTORY: | |
3918 | ret = -ENOTDIR; | |
3919 | break; | |
3920 | case EXT2_ET_FILE_NOT_FOUND: | |
3921 | ret = -ENOENT; | |
3922 | break; | |
3923 | case EXT2_ET_DIR_NO_SPACE: | |
3924 | is_err = 1; | |
da9d5b80 | 3925 | /* fallthrough */ |
81cbf1ef DW |
3926 | case EXT2_ET_TOOSMALL: |
3927 | case EXT2_ET_BLOCK_ALLOC_FAIL: | |
3928 | case EXT2_ET_INODE_ALLOC_FAIL: | |
3929 | case EXT2_ET_EA_NO_SPACE: | |
3930 | ret = -ENOSPC; | |
3931 | break; | |
3932 | case EXT2_ET_SYMLINK_LOOP: | |
3933 | ret = -EMLINK; | |
3934 | break; | |
3935 | case EXT2_ET_FILE_TOO_BIG: | |
3936 | ret = -EFBIG; | |
3937 | break; | |
3938 | case EXT2_ET_TDB_ERR_EXISTS: | |
3939 | case EXT2_ET_FILE_EXISTS: | |
3940 | ret = -EEXIST; | |
3941 | break; | |
3942 | case EXT2_ET_MMP_FAILED: | |
3943 | case EXT2_ET_MMP_FSCK_ON: | |
3944 | ret = -EBUSY; | |
3945 | break; | |
3946 | case EXT2_ET_EA_KEY_NOT_FOUND: | |
3947 | #ifdef ENODATA | |
3948 | ret = -ENODATA; | |
3949 | #else | |
3950 | ret = -ENOENT; | |
3951 | #endif | |
3952 | break; | |
3953 | /* Sometimes fuse returns a garbage file handle pointer to us... */ | |
3954 | case EXT2_ET_MAGIC_EXT2_FILE: | |
3955 | ret = -EFAULT; | |
3956 | break; | |
3957 | case EXT2_ET_UNIMPLEMENTED: | |
3958 | ret = -EOPNOTSUPP; | |
3959 | break; | |
3960 | default: | |
3961 | is_err = 1; | |
3962 | ret = -EIO; | |
3963 | break; | |
3964 | } | |
3965 | ||
3966 | no_translation: | |
3967 | if (!is_err) | |
3968 | return ret; | |
3969 | ||
3970 | if (ino) | |
3971 | fprintf(ff->err_fp, "FUSE2FS (%s): %s (inode #%d) at %s:%d.\n", | |
da9d5b80 | 3972 | fs->device_name ? fs->device_name : "???", |
81cbf1ef DW |
3973 | error_message(err), ino, file, line); |
3974 | else | |
3975 | fprintf(ff->err_fp, "FUSE2FS (%s): %s at %s:%d.\n", | |
da9d5b80 | 3976 | fs->device_name ? fs->device_name : "???", |
81cbf1ef DW |
3977 | error_message(err), file, line); |
3978 | fflush(ff->err_fp); | |
3979 | ||
3980 | /* Make a note in the error log */ | |
3981 | get_now(&now); | |
3982 | fs->super->s_last_error_time = now.tv_sec; | |
3983 | fs->super->s_last_error_ino = ino; | |
3984 | fs->super->s_last_error_line = line; | |
3985 | fs->super->s_last_error_block = err; /* Yeah... */ | |
3986 | strncpy((char *)fs->super->s_last_error_func, file, | |
3987 | sizeof(fs->super->s_last_error_func)); | |
3988 | if (fs->super->s_first_error_time == 0) { | |
3989 | fs->super->s_first_error_time = now.tv_sec; | |
3990 | fs->super->s_first_error_ino = ino; | |
3991 | fs->super->s_first_error_line = line; | |
3992 | fs->super->s_first_error_block = err; | |
3993 | strncpy((char *)fs->super->s_first_error_func, file, | |
3994 | sizeof(fs->super->s_first_error_func)); | |
3995 | } | |
3996 | ||
3997 | fs->super->s_error_count++; | |
3998 | ext2fs_mark_super_dirty(fs); | |
3999 | ext2fs_flush(fs); | |
4000 | if (ff->panic_on_error) | |
4001 | abort(); | |
4002 | ||
4003 | return ret; | |
4004 | } |