mount_root: keep track of overlay initialization state (via xattr)
[project/fstools.git] / libfstools / overlay.c
1 /*
2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3 *
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
7 *
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.
12 */
13
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <sys/mount.h>
17 #include <sys/syscall.h>
18
19 #include <asm/byteorder.h>
20
21 #include <errno.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <glob.h>
27 #include <errno.h>
28 #include <dirent.h>
29 #include <fcntl.h>
30
31 #include "libfstools.h"
32 #include "volume.h"
33
34 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
35
36 static bool keep_sysupgrade;
37
38 static ssize_t
39 fs_getxattr(const char *path, const char *name, void *value, size_t size)
40 {
41 return syscall(__NR_getxattr, path, name, value, size);
42 }
43
44 static ssize_t
45 fs_setxattr(const char *path, const char *name, const void *value, size_t size, int flags)
46 {
47 return syscall(__NR_setxattr, path, name, value, size, flags);
48 }
49
50 static int
51 handle_rmdir(const char *dir)
52 {
53 struct dirent *dt;
54 struct stat st;
55 DIR *d;
56 int fd;
57
58 d = opendir(dir);
59 if (!d)
60 return -1;
61
62 fd = dirfd(d);
63
64 while ((dt = readdir(d)) != NULL) {
65 if (fstatat(fd, dt->d_name, &st, AT_SYMLINK_NOFOLLOW) || S_ISDIR(st.st_mode))
66 continue;
67
68 if (keep_sysupgrade && !strcmp(dt->d_name, "sysupgrade.tgz"))
69 continue;
70
71 unlinkat(fd, dt->d_name, 0);
72 }
73
74 closedir(d);
75 rmdir(dir);
76
77 return 0;
78 }
79
80 void
81 foreachdir(const char *dir, int (*cb)(const char*))
82 {
83 char globdir[256];
84 glob_t gl;
85 int j;
86
87 if (dir[strlen(dir) - 1] == '/')
88 snprintf(globdir, 256, "%s*", dir);
89 else
90 snprintf(globdir, 256, "%s/*", dir); /**/
91
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);
95
96 cb(dir);
97 }
98
99 void
100 overlay_delete(const char *dir, bool _keep_sysupgrade)
101 {
102 keep_sysupgrade = _keep_sysupgrade;
103 foreachdir(dir, handle_rmdir);
104 }
105
106 static int
107 overlay_mount(struct volume *v, char *fs)
108 {
109 if (mkdir("/tmp/overlay", 0755)) {
110 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
111 return -1;
112 }
113
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));
116 return -1;
117 }
118
119 return volume_init(v);
120 }
121
122 static int
123 switch2jffs(struct volume *v)
124 {
125 struct stat s;
126 int ret;
127
128 if (!stat(SWITCH_JFFS2, &s)) {
129 ULOG_ERR("jffs2 switch already running\n");
130 return -1;
131 }
132
133 creat("/tmp/.switch_jffs2", 0600);
134 ret = mount(v->blk, "/rom/overlay", "jffs2", MS_NOATIME, NULL);
135 unlink("/tmp/.switch_jffs2");
136 if (ret) {
137 ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %s\n", v->blk, strerror(errno));
138 return -1;
139 }
140
141 if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
142 ULOG_ERR("failed - mount -o remount,ro none: %s\n", strerror(errno));
143 return -1;
144 }
145
146 system("cp -a /tmp/root/* /rom/overlay"); /**/
147
148 if (pivot("/rom", "/mnt")) {
149 ULOG_ERR("failed - pivot /rom /mnt: %s\n", strerror(errno));
150 return -1;
151 }
152
153 if (mount_move("/mnt", "/tmp/root", "")) {
154 ULOG_ERR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
155 return -1;
156 }
157
158 return fopivot("/overlay", "/rom");
159 }
160
161 int
162 handle_whiteout(const char *dir)
163 {
164 struct stat s;
165 char link[256];
166 ssize_t sz;
167 struct dirent **namelist;
168 int n;
169
170 n = scandir(dir, &namelist, NULL, NULL);
171
172 if (n < 1)
173 return -1;
174
175 while (n--) {
176 char file[256];
177
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);
181 if (sz > 0) {
182 char *orig;
183
184 link[sz] = '\0';
185 orig = strstr(&file[1], "/");
186 if (orig && !strcmp(link, "(overlay-whiteout)"))
187 unlink(orig);
188 }
189 }
190 free(namelist[n]);
191 }
192 free(namelist);
193
194 return 0;
195 }
196
197 int
198 jffs2_switch(struct volume *v)
199 {
200 char *mp;
201 int ret = -1;
202
203 if (find_overlay_mount("overlayfs:/tmp/root"))
204 return -1;
205
206 if (find_filesystem("overlay")) {
207 ULOG_ERR("overlayfs not supported by kernel\n");
208 return ret;
209 }
210
211 mp = find_mount_point(v->blk, 0);
212 if (mp) {
213 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
214 return -1;
215 }
216
217 switch (volume_identify(v)) {
218 case FS_NONE:
219 ULOG_ERR("no jffs2 marker found\n");
220 /* fall through */
221
222 case FS_DEADCODE:
223 ret = switch2jffs(v);
224 if (!ret) {
225 ULOG_INFO("performing overlay whiteout\n");
226 umount2("/tmp/root", MNT_DETACH);
227 foreachdir("/overlay/", handle_whiteout);
228 }
229 break;
230
231 case FS_JFFS2:
232 ret = overlay_mount(v, "jffs2");
233 if (ret)
234 break;
235 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
236 ULOG_ERR("switching to jffs2 failed\n");
237 ret = -1;
238 }
239 break;
240
241 case FS_UBIFS:
242 ret = overlay_mount(v, "ubifs");
243 if (ret)
244 break;
245 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
246 ULOG_ERR("switching to ubifs failed\n");
247 ret = -1;
248 }
249 break;
250 }
251
252 if (ret)
253 return ret;
254
255 sync();
256 fs_state_set("/overlay", FS_STATE_READY);
257 return 0;
258 }
259
260 static int overlay_mount_fs(struct volume *v)
261 {
262 char *fstype;
263
264 if (mkdir("/tmp/overlay", 0755)) {
265 ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
266 return -1;
267 }
268
269 fstype = "jffs2";
270
271 switch (volume_identify(v)) {
272 case FS_UBIFS:
273 fstype = "ubifs";
274 break;
275 }
276
277 volume_init(v);
278
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));
282 return -1;
283 }
284
285 return -1;
286 }
287
288 enum fs_state fs_state_get(const char *dir)
289 {
290 uint32_t val;
291
292 if (fs_getxattr(dir, "user.fs_state", &val, sizeof(val)) != sizeof(val))
293 return FS_STATE_UNKNOWN;
294
295 if (val > __FS_STATE_LAST)
296 return FS_STATE_UNKNOWN;
297
298 return val;
299 }
300
301
302 int fs_state_set(const char *dir, enum fs_state state)
303 {
304 uint32_t val = state;
305
306 return fs_setxattr(dir, "user.fs_state", &val, sizeof(val), 0);
307 }
308
309
310 int mount_overlay(struct volume *v)
311 {
312 char *mp;
313
314 if (!v)
315 return -1;
316
317 mp = find_mount_point(v->blk, 0);
318 if (mp) {
319 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
320 return -1;
321 }
322
323 overlay_mount_fs(v);
324
325 extroot_prefix = "/tmp/overlay";
326 if (!mount_extroot()) {
327 ULOG_INFO("switched to extroot\n");
328 return 0;
329 }
330
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");
336 break;
337 }
338 case FS_STATE_PENDING:
339 ULOG_INFO("overlay filesystem has not been fully initialized yet\n");
340 overlay_delete("/tmp/overlay", true);
341 break;
342 case FS_STATE_READY:
343 break;
344 }
345
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");
349 return ramoverlay();
350 }
351
352 return -1;
353 }