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 if (system("cp -a /tmp/root/* /rom/overlay")) {
147 ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %s\n", strerror(errno));
151 if (pivot("/rom", "/mnt")) {
152 ULOG_ERR("failed - pivot /rom /mnt: %s\n", strerror(errno));
156 if (mount_move("/mnt", "/tmp/root", "")) {
157 ULOG_ERR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
161 return fopivot("/overlay", "/rom");
165 handle_whiteout(const char *dir)
170 struct dirent **namelist;
173 n = scandir(dir, &namelist, NULL, NULL);
181 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
182 if (!lstat(file, &s) && S_ISLNK(s.st_mode)) {
183 sz = readlink(file, link, sizeof(link) - 1);
188 orig = strstr(&file[1], "/");
189 if (orig && !strcmp(link, "(overlay-whiteout)"))
201 jffs2_switch(struct volume *v)
206 if (find_overlay_mount("overlayfs:/tmp/root"))
209 if (find_filesystem("overlay")) {
210 ULOG_ERR("overlayfs not supported by kernel\n");
214 mp = find_mount_point(v->blk, 0);
216 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
220 switch (volume_identify(v)) {
222 ULOG_ERR("no jffs2 marker found\n");
226 ret = switch2jffs(v);
228 ULOG_INFO("performing overlay whiteout\n");
229 umount2("/tmp/root", MNT_DETACH);
230 foreachdir("/overlay/", handle_whiteout);
235 ret = overlay_mount(v, "jffs2");
238 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
239 ULOG_ERR("switching to jffs2 failed\n");
245 ret = overlay_mount(v, "ubifs");
248 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
249 ULOG_ERR("switching to ubifs failed\n");
259 fs_state_set("/overlay", FS_STATE_READY);
263 static int overlay_mount_fs(struct volume *v)
267 if (mkdir("/tmp/overlay", 0755)) {
268 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
274 switch (volume_identify(v)) {
282 if (mount(v->blk, "/tmp/overlay", fstype, MS_NOATIME, NULL)) {
283 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n",
284 fstype, v->blk, strerror(errno));
291 enum fs_state fs_state_get(const char *dir)
295 if (fs_getxattr(dir, "user.fs_state", &val, sizeof(val)) != sizeof(val))
296 return FS_STATE_UNKNOWN;
298 if (val > __FS_STATE_LAST)
299 return FS_STATE_UNKNOWN;
305 int fs_state_set(const char *dir, enum fs_state state)
307 uint32_t val = state;
309 return fs_setxattr(dir, "user.fs_state", &val, sizeof(val), 0);
313 int mount_overlay(struct volume *v)
320 mp = find_mount_point(v->blk, 0);
322 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
328 extroot_prefix = "/tmp/overlay";
329 if (!mount_extroot()) {
330 ULOG_INFO("switched to extroot\n");
334 switch(fs_state_get("/tmp/overlay")) {
335 case FS_STATE_UNKNOWN:
336 fs_state_set("/tmp/overlay", FS_STATE_PENDING);
337 if (fs_state_get("/tmp/overlay") != FS_STATE_PENDING) {
338 ULOG_ERR("unable to set filesystem state\n");
341 case FS_STATE_PENDING:
342 ULOG_INFO("overlay filesystem has not been fully initialized yet\n");
343 overlay_delete("/tmp/overlay", true);
349 ULOG_INFO("switching to jffs2 overlay\n");
350 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
351 ULOG_ERR("switching to jffs2 failed - fallback to ramoverlay\n");