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 if (!glob(globdir
, GLOB_NOESCAPE
| GLOB_MARK
| GLOB_ONLYDIR
, NULL
, &gl
))
95 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
96 char *dir
= gl
.gl_pathv
[j
];
97 int len
= strlen(gl
.gl_pathv
[j
]);
99 if (len
> 1 && dir
[len
- 1] == '/')
102 if (!lstat(gl
.gl_pathv
[j
], &s
) && !S_ISLNK(s
.st_mode
))
103 foreachdir(gl
.gl_pathv
[j
], cb
);
109 overlay_delete(const char *dir
, bool _keep_sysupgrade
)
111 keep_sysupgrade
= _keep_sysupgrade
;
112 foreachdir(dir
, handle_rmdir
);
116 overlay_mount(struct volume
*v
, char *fs
)
118 if (mkdir("/tmp/overlay", 0755)) {
119 ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
123 if (mount(v
->blk
, "/tmp/overlay", fs
, MS_NOATIME
, NULL
)) {
124 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n", fs
, v
->blk
);
132 switch2jffs(struct volume
*v
)
137 if (!stat(SWITCH_JFFS2
, &s
)) {
138 ULOG_ERR("jffs2 switch already running\n");
142 creat("/tmp/.switch_jffs2", 0600);
143 ret
= mount(v
->blk
, "/rom/overlay", "jffs2", MS_NOATIME
, NULL
);
144 unlink("/tmp/.switch_jffs2");
146 ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %m\n", v
->blk
);
150 if (mount("none", "/", NULL
, MS_NOATIME
| MS_REMOUNT
, 0)) {
151 ULOG_ERR("failed - mount -o remount,ro none: %m\n");
155 if (system("cp -a /tmp/root/* /rom/overlay")) {
156 ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %m\n");
160 if (pivot("/rom", "/mnt")) {
161 ULOG_ERR("failed - pivot /rom /mnt: %m\n");
165 if (mount_move("/mnt", "/tmp/root", "")) {
166 ULOG_ERR("failed - mount -o move /mnt /tmp/root %m\n");
170 return fopivot("/overlay", "/rom");
174 handle_whiteout(const char *dir
)
179 struct dirent
**namelist
;
182 n
= scandir(dir
, &namelist
, NULL
, NULL
);
190 snprintf(file
, sizeof(file
), "%s%s", dir
, namelist
[n
]->d_name
);
191 if (!lstat(file
, &s
) && S_ISLNK(s
.st_mode
)) {
192 sz
= readlink(file
, link
, sizeof(link
) - 1);
197 orig
= strstr(&file
[1], "/");
198 if (orig
&& !strcmp(link
, "(overlay-whiteout)"))
209 static char *overlay_fs_name(int type
)
225 jffs2_switch(struct volume
*v
)
230 if (find_overlay_mount("overlayfs:/tmp/root"))
233 if (find_filesystem("overlay")) {
234 ULOG_ERR("overlayfs not supported by kernel\n");
239 mp
= find_mount_point(v
->blk
, 0);
241 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
245 type
= volume_identify(v
);
246 fs_name
= overlay_fs_name(type
);
250 ULOG_ERR("no jffs2 marker found\n");
257 ULOG_INFO("performing overlay whiteout\n");
258 umount2("/tmp/root", MNT_DETACH
);
259 foreachdir("/overlay/", handle_whiteout
);
261 /* try hard to be in sync */
262 ULOG_INFO("syncronizing overlay\n");
263 system("cp -a /tmp/root/upper/* / 2>/dev/null");
269 if (overlay_mount(v
, fs_name
))
271 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
272 ULOG_ERR("switching to %s failed\n", fs_name
);
279 fs_state_set("/overlay", FS_STATE_READY
);
283 static int overlay_mount_fs(struct volume
*v
)
285 char *fstype
= overlay_fs_name(volume_identify(v
));
287 if (mkdir("/tmp/overlay", 0755)) {
288 ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
292 if (mount(v
->blk
, "/tmp/overlay", fstype
, MS_NOATIME
, NULL
)) {
293 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n",
301 enum fs_state
fs_state_get(const char *dir
)
308 path
= alloca(strlen(dir
) + 1 + sizeof("/.fs_state"));
309 sprintf(path
, "%s/.fs_state", dir
);
310 len
= readlink(path
, valstr
, sizeof(valstr
) - 1);
312 return FS_STATE_UNKNOWN
;
317 if (val
> __FS_STATE_LAST
)
318 return FS_STATE_UNKNOWN
;
324 int fs_state_set(const char *dir
, enum fs_state state
)
329 if (fs_state_get(dir
) == state
)
332 path
= alloca(strlen(dir
) + 1 + sizeof("/.fs_state"));
333 sprintf(path
, "%s/.fs_state", dir
);
335 snprintf(valstr
, sizeof(valstr
), "%d", state
);
337 return symlink(valstr
, path
);
341 int mount_overlay(struct volume
*v
)
348 mp
= find_mount_point(v
->blk
, 0);
350 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v
->blk
, mp
);
356 extroot_prefix
= "/tmp/overlay";
357 if (!mount_extroot()) {
358 ULOG_INFO("switched to extroot\n");
362 switch(fs_state_get("/tmp/overlay")) {
363 case FS_STATE_UNKNOWN
:
364 fs_state_set("/tmp/overlay", FS_STATE_PENDING
);
365 if (fs_state_get("/tmp/overlay") != FS_STATE_PENDING
) {
366 ULOG_ERR("unable to set filesystem state\n");
369 case FS_STATE_PENDING
:
370 ULOG_INFO("overlay filesystem has not been fully initialized yet\n");
371 overlay_delete("/tmp/overlay", true);
377 fs_name
= overlay_fs_name(volume_identify(v
));
378 ULOG_INFO("switching to %s overlay\n", fs_name
);
379 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
380 ULOG_ERR("switching to %s failed - fallback to ramoverlay\n", fs_name
);