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>
17 #include <sys/syscall.h>
19 #include <asm/byteorder.h>
31 #include "libfstools.h"
34 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
36 static bool keep_sysupgrade
;
39 fs_getxattr(const char *path
, const char *name
, void *value
, size_t size
)
41 return syscall(__NR_getxattr
, path
, name
, value
, size
);
45 fs_setxattr(const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
47 return syscall(__NR_setxattr
, path
, name
, value
, size
, flags
);
51 handle_rmdir(const char *dir
)
64 while ((dt
= readdir(d
)) != NULL
) {
65 if (fstatat(fd
, dt
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) || S_ISDIR(st
.st_mode
))
68 if (keep_sysupgrade
&& !strcmp(dt
->d_name
, "sysupgrade.tgz"))
71 unlinkat(fd
, dt
->d_name
, 0);
81 foreachdir(const char *dir
, int (*cb
)(const char*))
87 if (dir
[strlen(dir
) - 1] == '/')
88 snprintf(globdir
, 256, "%s*", dir
);
90 snprintf(globdir
, 256, "%s/*", dir
); /**/
92 if (!glob(globdir
, GLOB_NOESCAPE
| GLOB_MARK
| GLOB_ONLYDIR
, NULL
, &gl
))
93 for (j
= 0; j
< gl
.gl_pathc
; j
++)
94 foreachdir(gl
.gl_pathv
[j
], cb
);
100 overlay_delete(const char *dir
, bool _keep_sysupgrade
)
102 keep_sysupgrade
= _keep_sysupgrade
;
103 foreachdir(dir
, handle_rmdir
);
107 overlay_mount(struct volume
*v
, char *fs
)
109 if (mkdir("/tmp/overlay", 0755)) {
110 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno
));
114 if (mount(v
->blk
, "/tmp/overlay", fs
, MS_NOATIME
, NULL
)) {
115 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n", fs
, v
->blk
, strerror(errno
));
119 return volume_init(v
);
123 switch2jffs(struct volume
*v
)
128 if (!stat(SWITCH_JFFS2
, &s
)) {
129 ULOG_ERR("jffs2 switch already running\n");
133 creat("/tmp/.switch_jffs2", 0600);
134 ret
= mount(v
->blk
, "/rom/overlay", "jffs2", MS_NOATIME
, NULL
);
135 unlink("/tmp/.switch_jffs2");
137 ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %s\n", v
->blk
, strerror(errno
));
141 if (mount("none", "/", NULL
, MS_NOATIME
| MS_REMOUNT
, 0)) {
142 ULOG_ERR("failed - mount -o remount,ro none: %s\n", strerror(errno
));
146 system("cp -a /tmp/root/* /rom/overlay"); /**/
148 if (pivot("/rom", "/mnt")) {
149 ULOG_ERR("failed - pivot /rom /mnt: %s\n", strerror(errno
));
153 if (mount_move("/mnt", "/tmp/root", "")) {
154 ULOG_ERR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno
));
158 return fopivot("/overlay", "/rom");
162 handle_whiteout(const char *dir
)
167 struct dirent
**namelist
;
170 n
= scandir(dir
, &namelist
, NULL
, NULL
);
178 snprintf(file
, sizeof(file
), "%s%s", dir
, namelist
[n
]->d_name
);
179 if (!lstat(file
, &s
) && S_ISLNK(s
.st_mode
)) {
180 sz
= readlink(file
, link
, sizeof(link
) - 1);
185 orig
= strstr(&file
[1], "/");
186 if (orig
&& !strcmp(link
, "(overlay-whiteout)"))
198 jffs2_switch(struct volume
*v
)
203 if (find_overlay_mount("overlayfs:/tmp/root"))
206 if (find_filesystem("overlay")) {
207 ULOG_ERR("overlayfs not supported by kernel\n");
211 mp
= find_mount_point(v
->blk
, 0);
213 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
217 switch (volume_identify(v
)) {
219 ULOG_ERR("no jffs2 marker found\n");
223 ret
= switch2jffs(v
);
225 ULOG_INFO("performing overlay whiteout\n");
226 umount2("/tmp/root", MNT_DETACH
);
227 foreachdir("/overlay/", handle_whiteout
);
232 ret
= overlay_mount(v
, "jffs2");
235 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
236 ULOG_ERR("switching to jffs2 failed\n");
242 ret
= overlay_mount(v
, "ubifs");
245 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
246 ULOG_ERR("switching to ubifs failed\n");
256 fs_state_set("/overlay", FS_STATE_READY
);
260 static int overlay_mount_fs(struct volume
*v
)
264 if (mkdir("/tmp/overlay", 0755)) {
265 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno
));
271 switch (volume_identify(v
)) {
279 if (mount(v
->blk
, "/tmp/overlay", fstype
, MS_NOATIME
, NULL
)) {
280 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n",
281 fstype
, v
->blk
, strerror(errno
));
288 enum fs_state
fs_state_get(const char *dir
)
292 if (fs_getxattr(dir
, "user.fs_state", &val
, sizeof(val
)) != sizeof(val
))
293 return FS_STATE_UNKNOWN
;
295 if (val
> __FS_STATE_LAST
)
296 return FS_STATE_UNKNOWN
;
302 int fs_state_set(const char *dir
, enum fs_state state
)
304 uint32_t val
= state
;
306 return fs_setxattr(dir
, "user.fs_state", &val
, sizeof(val
), 0);
310 int mount_overlay(struct volume
*v
)
317 mp
= find_mount_point(v
->blk
, 0);
319 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
325 extroot_prefix
= "/tmp/overlay";
326 if (!mount_extroot()) {
327 ULOG_INFO("switched to extroot\n");
331 switch(fs_state_get("/tmp/overlay")) {
332 case FS_STATE_UNKNOWN
:
333 fs_state_set("/tmp/overlay", FS_STATE_PENDING
);
334 if (fs_state_get("/tmp/overlay") != FS_STATE_PENDING
) {
335 ULOG_ERR("unable to set filesystem state\n");
338 case FS_STATE_PENDING
:
339 ULOG_INFO("overlay filesystem has not been fully initialized yet\n");
340 overlay_delete("/tmp/overlay", true);
346 ULOG_INFO("switching to jffs2 overlay\n");
347 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
348 ULOG_ERR("switching to jffs2 failed - fallback to ramoverlay\n");