2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <sys/types.h>
16 #include <sys/mount.h>
18 #include <asm/byteorder.h>
30 #include "libfstools.h"
33 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
35 static bool keep_sysupgrade
;
38 handle_rmdir(const char *dir
)
51 while ((dt
= readdir(d
)) != NULL
) {
52 if (fstatat(fd
, dt
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) || S_ISDIR(st
.st_mode
))
55 if (keep_sysupgrade
&& !strcmp(dt
->d_name
, "sysupgrade.tgz"))
58 unlinkat(fd
, dt
->d_name
, 0);
68 foreachdir(const char *dir
, int (*cb
)(const char*))
70 static char *globdir
= NULL
;
71 static size_t globdirlen
= 0;
72 struct stat s
= { 0 };
73 size_t dirlen
= strlen(dir
);
77 if (dirlen
+ sizeof("/*") > globdirlen
) {
78 /* Alloc extra 256 B to avoid too many reallocs */
79 size_t len
= dirlen
+ sizeof("/*") + 256;
82 tmp
= realloc(globdir
, len
);
89 if (dir
[dirlen
- 1] == '/')
90 sprintf(globdir
, "%s*", dir
);
92 sprintf(globdir
, "%s/*", dir
);
94 /* Include GLOB_MARK as callbacks expect a trailing slash */
95 if (!glob(globdir
, GLOB_NOESCAPE
| GLOB_MARK
| GLOB_ONLYDIR
, NULL
, &gl
))
96 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
97 char *dir
= gl
.gl_pathv
[j
];
98 int len
= strlen(gl
.gl_pathv
[j
]);
101 /* Quick way of skipping files */
102 if (dir
[len
- 1] != '/')
105 /* lstat needs path without a trailing slash */
108 err
= lstat(gl
.gl_pathv
[j
], &s
);
112 if (!err
&& !S_ISLNK(s
.st_mode
))
113 foreachdir(gl
.gl_pathv
[j
], cb
);
119 overlay_delete(const char *dir
, bool _keep_sysupgrade
)
121 keep_sysupgrade
= _keep_sysupgrade
;
122 foreachdir(dir
, handle_rmdir
);
126 overlay_mount(struct volume
*v
, char *fs
)
128 if (mkdir("/tmp/overlay", 0755)) {
129 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno
));
133 if (mount(v
->blk
, "/tmp/overlay", fs
, MS_NOATIME
, NULL
)) {
134 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n", fs
, v
->blk
, strerror(errno
));
142 switch2jffs(struct volume
*v
)
147 if (!stat(SWITCH_JFFS2
, &s
)) {
148 ULOG_ERR("jffs2 switch already running\n");
152 creat("/tmp/.switch_jffs2", 0600);
153 ret
= mount(v
->blk
, "/rom/overlay", "jffs2", MS_NOATIME
, NULL
);
154 unlink("/tmp/.switch_jffs2");
156 ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %s\n", v
->blk
, strerror(errno
));
160 if (mount("none", "/", NULL
, MS_NOATIME
| MS_REMOUNT
, 0)) {
161 ULOG_ERR("failed - mount -o remount,ro none: %s\n", strerror(errno
));
165 if (system("cp -a /tmp/root/* /rom/overlay")) {
166 ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %s\n", strerror(errno
));
170 if (pivot("/rom", "/mnt")) {
171 ULOG_ERR("failed - pivot /rom /mnt: %s\n", strerror(errno
));
175 if (mount_move("/mnt", "/tmp/root", "")) {
176 ULOG_ERR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno
));
180 return fopivot("/overlay", "/rom");
184 handle_whiteout(const char *dir
)
189 struct dirent
**namelist
;
192 n
= scandir(dir
, &namelist
, NULL
, NULL
);
200 snprintf(file
, sizeof(file
), "%s%s", dir
, namelist
[n
]->d_name
);
201 if (!lstat(file
, &s
) && S_ISLNK(s
.st_mode
)) {
202 sz
= readlink(file
, link
, sizeof(link
) - 1);
207 orig
= strstr(&file
[1], "/");
208 if (orig
&& !strcmp(link
, "(overlay-whiteout)"))
219 static char *overlay_fs_name(int type
)
235 jffs2_switch(struct volume
*v
)
240 if (find_overlay_mount("overlayfs:/tmp/root"))
243 if (find_filesystem("overlay")) {
244 ULOG_ERR("overlayfs not supported by kernel\n");
249 mp
= find_mount_point(v
->blk
, 0);
251 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
255 type
= volume_identify(v
);
256 fs_name
= overlay_fs_name(type
);
260 ULOG_ERR("no jffs2 marker found\n");
267 ULOG_INFO("performing overlay whiteout\n");
268 umount2("/tmp/root", MNT_DETACH
);
269 foreachdir("/overlay/", handle_whiteout
);
275 if (overlay_mount(v
, fs_name
))
277 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
278 ULOG_ERR("switching to %s failed\n", fs_name
);
285 fs_state_set("/overlay", FS_STATE_READY
);
289 static int overlay_mount_fs(struct volume
*v
)
291 char *fstype
= overlay_fs_name(volume_identify(v
));
293 if (mkdir("/tmp/overlay", 0755)) {
294 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno
));
298 if (mount(v
->blk
, "/tmp/overlay", fstype
, MS_NOATIME
, NULL
)) {
299 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n",
300 fstype
, v
->blk
, strerror(errno
));
307 enum fs_state
fs_state_get(const char *dir
)
314 path
= alloca(strlen(dir
) + 1 + sizeof("/.fs_state"));
315 sprintf(path
, "%s/.fs_state", dir
);
316 len
= readlink(path
, valstr
, sizeof(valstr
) - 1);
318 return FS_STATE_UNKNOWN
;
323 if (val
> __FS_STATE_LAST
)
324 return FS_STATE_UNKNOWN
;
330 int fs_state_set(const char *dir
, enum fs_state state
)
335 if (fs_state_get(dir
) == state
)
338 path
= alloca(strlen(dir
) + 1 + sizeof("/.fs_state"));
339 sprintf(path
, "%s/.fs_state", dir
);
341 snprintf(valstr
, sizeof(valstr
), "%d", state
);
343 return symlink(valstr
, path
);
347 int mount_overlay(struct volume
*v
)
354 mp
= find_mount_point(v
->blk
, 0);
356 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
362 extroot_prefix
= "/tmp/overlay";
363 if (!mount_extroot()) {
364 ULOG_INFO("switched to extroot\n");
368 switch(fs_state_get("/tmp/overlay")) {
369 case FS_STATE_UNKNOWN
:
370 fs_state_set("/tmp/overlay", FS_STATE_PENDING
);
371 if (fs_state_get("/tmp/overlay") != FS_STATE_PENDING
) {
372 ULOG_ERR("unable to set filesystem state\n");
375 case FS_STATE_PENDING
:
376 ULOG_INFO("overlay filesystem has not been fully initialized yet\n");
377 overlay_delete("/tmp/overlay", true);
383 fs_name
= overlay_fs_name(volume_identify(v
));
384 ULOG_INFO("switching to %s overlay\n", fs_name
);
385 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
386 ULOG_ERR("switching to %s failed - fallback to ramoverlay\n", fs_name
);