39215d5926a9b72728f6f9e201e0580c4bf78f58
[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
18 #include <asm/byteorder.h>
19
20 #include <errno.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <glob.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29
30 #include "libfstools.h"
31 #include "volume.h"
32
33 #ifndef GLOB_ONLYDIR
34 #define GLOB_ONLYDIR 0x100
35 #endif
36
37 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
38
39 static bool keep_sysupgrade;
40
41 static int
42 handle_rmdir(const char *dir)
43 {
44 struct dirent *dt;
45 struct stat st;
46 DIR *d;
47 int fd;
48
49 d = opendir(dir);
50 if (!d)
51 return -1;
52
53 fd = dirfd(d);
54
55 while ((dt = readdir(d)) != NULL) {
56 if (fstatat(fd, dt->d_name, &st, AT_SYMLINK_NOFOLLOW) || S_ISDIR(st.st_mode))
57 continue;
58
59 if (keep_sysupgrade && !strcmp(dt->d_name, "sysupgrade.tgz"))
60 continue;
61
62 unlinkat(fd, dt->d_name, 0);
63 }
64
65 closedir(d);
66 rmdir(dir);
67
68 return 0;
69 }
70
71 void
72 foreachdir(const char *dir, int (*cb)(const char*))
73 {
74 static char *globdir = NULL;
75 static size_t globdirlen = 0;
76 struct stat s = { 0 };
77 size_t dirlen = strlen(dir);
78 glob_t gl;
79 int j;
80
81 if (dirlen + sizeof("/*") > globdirlen) {
82 /* Alloc extra 256 B to avoid too many reallocs */
83 size_t len = dirlen + sizeof("/*") + 256;
84 char *tmp;
85
86 tmp = realloc(globdir, len);
87 if (!tmp)
88 return;
89 globdir = tmp;
90 globdirlen = len;
91 }
92
93 sprintf(globdir, "%s/*", dir);
94
95 /* Include GLOB_MARK as callbacks expect a trailing slash */
96 if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
97 for (j = 0; j < gl.gl_pathc; j++) {
98 char *dir = gl.gl_pathv[j];
99 int len = strlen(gl.gl_pathv[j]);
100 int err;
101
102 /* Quick way of skipping files */
103 if (dir[len - 1] != '/')
104 continue;
105
106 /* lstat needs path without a trailing slash */
107 if (len > 1)
108 dir[len - 1] = '\0';
109 err = lstat(gl.gl_pathv[j], &s);
110 if (len > 1)
111 dir[len - 1] = '/';
112
113 if (!err && !S_ISLNK(s.st_mode))
114 foreachdir(gl.gl_pathv[j], cb);
115 }
116 cb(dir);
117 }
118
119 static void foreach_mount(int (*cb)(const char *, const char *))
120 {
121 FILE *fp = fopen("/proc/mounts", "r");
122 static char line[256];
123
124 if (!fp)
125 return;
126
127 while (fgets(line, sizeof(line), fp)) {
128 char device[32], mount_point[32];
129
130 if (sscanf(line, "%31s %31s %*s %*s %*u %*u", device, mount_point) == 2)
131 cb(device, mount_point);
132 }
133
134 fclose(fp);
135 }
136
137 void
138 overlay_delete(const char *dir, bool _keep_sysupgrade)
139 {
140 keep_sysupgrade = _keep_sysupgrade;
141 foreachdir(dir, handle_rmdir);
142 }
143
144 static int
145 overlay_mount(struct volume *v, char *fs)
146 {
147 if (mkdir("/tmp/overlay", 0755)) {
148 ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
149 return -1;
150 }
151
152 if (mount(v->blk, "/tmp/overlay", fs, MS_NOATIME, NULL)) {
153 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n", fs, v->blk);
154 return -1;
155 }
156
157 return 0;
158 }
159
160 /**
161 * move_mount - move mount point to the new root
162 */
163 static int move_mount(const char *device, const char *mount_point)
164 {
165 static const char *prefix = "/tmp/root/";
166
167 if (strncmp(mount_point, prefix, strlen(prefix)))
168 return 0;
169
170 return mount_move(prefix, "/", mount_point + strlen(prefix));
171 }
172
173 static int
174 switch2jffs(struct volume *v)
175 {
176 struct stat s;
177 int ret;
178
179 if (!stat(SWITCH_JFFS2, &s)) {
180 ULOG_ERR("jffs2 switch already running\n");
181 return -1;
182 }
183
184 creat("/tmp/.switch_jffs2", 0600);
185 ret = mount(v->blk, "/rom/overlay", "jffs2", MS_NOATIME, NULL);
186 unlink("/tmp/.switch_jffs2");
187 if (ret) {
188 ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %m\n", v->blk);
189 return -1;
190 }
191
192 if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
193 ULOG_ERR("failed - mount -o remount,ro none: %m\n");
194 return -1;
195 }
196
197 if (system("cp -a /tmp/root/* /rom/overlay")) {
198 ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %m\n");
199 return -1;
200 }
201
202 if (pivot("/rom", "/mnt")) {
203 ULOG_ERR("failed - pivot /rom /mnt: %m\n");
204 return -1;
205 }
206
207 if (mount_move("/mnt", "/tmp/root", "")) {
208 ULOG_ERR("failed - mount -o move /mnt /tmp/root %m\n");
209 return -1;
210 }
211
212 ret = fopivot("/overlay", "/rom");
213
214 /*
215 * Besides copying overlay data from "tmpfs" to "jffs2" we should also
216 * move mount points that user could create during JFFS2 formatting.
217 * This has to happen after fopivot call because:
218 * 1) It's trivial to find mount points to move then (/tmp/root/...).
219 * 2) We can't do that earlier using /rom/overlay/upper/ as overlay(fs)
220 * doesn't support mounts. Mounting to upper dir don't make overlay
221 * /propagate/ files to the target dir.
222 */
223 foreach_mount(move_mount);
224
225 return ret;
226 }
227
228 int
229 handle_whiteout(const char *dir)
230 {
231 struct stat s;
232 char link[256];
233 ssize_t sz;
234 struct dirent **namelist;
235 int n;
236
237 n = scandir(dir, &namelist, NULL, NULL);
238
239 if (n < 1)
240 return -1;
241
242 while (n--) {
243 char file[256];
244
245 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
246 if (!lstat(file, &s) && S_ISLNK(s.st_mode)) {
247 sz = readlink(file, link, sizeof(link) - 1);
248 if (sz > 0) {
249 char *orig;
250
251 link[sz] = '\0';
252 orig = strstr(&file[1], "/");
253 if (orig && !strcmp(link, "(overlay-whiteout)"))
254 unlink(orig);
255 }
256 }
257 free(namelist[n]);
258 }
259 free(namelist);
260
261 return 0;
262 }
263
264 static char *overlay_fs_name(int type)
265 {
266 switch (type) {
267 case FS_EXT4:
268 return "ext4";
269 case FS_F2FS:
270 return "f2fs";
271 case FS_UBIFS:
272 return "ubifs";
273 case FS_JFFS2:
274 default:
275 return "jffs2";
276 }
277 }
278
279 int
280 jffs2_switch(struct volume *v)
281 {
282 char *mp, *fs_name;
283 int type;
284
285 if (find_overlay_mount("overlayfs:/tmp/root"))
286 return -1;
287
288 if (find_filesystem("overlay")) {
289 ULOG_ERR("overlayfs not supported by kernel\n");
290 return -1;
291 }
292
293 volume_init(v);
294 mp = find_mount_point(v->blk, 0);
295 if (mp) {
296 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
297 return -1;
298 }
299
300 type = volume_identify(v);
301 fs_name = overlay_fs_name(type);
302
303 switch (type) {
304 case FS_NONE:
305 ULOG_ERR("no jffs2 marker found\n");
306 /* fall through */
307
308 case FS_DEADCODE:
309 if (switch2jffs(v))
310 return -1;
311
312 ULOG_INFO("performing overlay whiteout\n");
313 umount2("/tmp/root", MNT_DETACH);
314 foreachdir("/overlay/", handle_whiteout);
315
316 /* try hard to be in sync */
317 ULOG_INFO("syncronizing overlay\n");
318 if (system("cp -a /tmp/root/upper/* / 2>/dev/null"))
319 ULOG_ERR("failed to sync jffs2 overlay\n");
320 break;
321
322 case FS_EXT4:
323 case FS_F2FS:
324 case FS_UBIFS:
325 if (overlay_mount(v, fs_name))
326 return -1;
327 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
328 ULOG_ERR("switching to %s failed\n", fs_name);
329 return -1;
330 }
331 break;
332 }
333
334 sync();
335 fs_state_set("/overlay", FS_STATE_READY);
336 return 0;
337 }
338
339 static int overlay_mount_fs(struct volume *v)
340 {
341 char *fstype = overlay_fs_name(volume_identify(v));
342
343 if (mkdir("/tmp/overlay", 0755)) {
344 ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
345 return -1;
346 }
347
348 if (mount(v->blk, "/tmp/overlay", fstype,
349 #ifdef OVL_MOUNT_FULL_ACCESS_TIME
350 MS_RELATIME,
351 #else
352 MS_NOATIME,
353 #endif
354 #ifdef OVL_MOUNT_COMPRESS_ZLIB
355 "compr=zlib"
356 #else
357 NULL
358 #endif
359 )) {
360 ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n",
361 fstype, v->blk);
362 return -1;
363 }
364
365 return 0;
366 }
367
368 enum fs_state fs_state_get(const char *dir)
369 {
370 char *path;
371 char valstr[16];
372 uint32_t val;
373 ssize_t len;
374
375 path = alloca(strlen(dir) + 1 + sizeof("/.fs_state"));
376 sprintf(path, "%s/.fs_state", dir);
377 len = readlink(path, valstr, sizeof(valstr) - 1);
378 if (len < 0)
379 return FS_STATE_UNKNOWN;
380
381 valstr[len] = 0;
382 val = atoi(valstr);
383
384 if (val > __FS_STATE_LAST)
385 return FS_STATE_UNKNOWN;
386
387 return val;
388 }
389
390
391 int fs_state_set(const char *dir, enum fs_state state)
392 {
393 char valstr[16];
394 char *path;
395
396 if (fs_state_get(dir) == state)
397 return 0;
398
399 path = alloca(strlen(dir) + 1 + sizeof("/.fs_state"));
400 sprintf(path, "%s/.fs_state", dir);
401 unlink(path);
402 snprintf(valstr, sizeof(valstr), "%d", state);
403
404 return symlink(valstr, path);
405 }
406
407
408 int mount_overlay(struct volume *v)
409 {
410 char *mp, *fs_name;
411
412 if (!v)
413 return -1;
414
415 mp = find_mount_point(v->blk, 0);
416 if (mp) {
417 ULOG_ERR("rootfs_data:%s is already mounted as %s\n", v->blk, mp);
418 return -1;
419 }
420
421 overlay_mount_fs(v);
422
423 extroot_prefix = "/tmp/overlay";
424 if (!mount_extroot()) {
425 ULOG_INFO("switched to extroot\n");
426 return 0;
427 }
428
429 switch(fs_state_get("/tmp/overlay")) {
430 case FS_STATE_UNKNOWN:
431 fs_state_set("/tmp/overlay", FS_STATE_PENDING);
432 if (fs_state_get("/tmp/overlay") != FS_STATE_PENDING) {
433 ULOG_ERR("unable to set filesystem state\n");
434 break;
435 }
436 case FS_STATE_PENDING:
437 ULOG_INFO("overlay filesystem has not been fully initialized yet\n");
438 overlay_delete("/tmp/overlay", true);
439 break;
440 case FS_STATE_READY:
441 break;
442 }
443
444 fs_name = overlay_fs_name(volume_identify(v));
445 ULOG_INFO("switching to %s overlay\n", fs_name);
446 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
447 ULOG_ERR("switching to %s failed - fallback to ramoverlay\n", fs_name);
448 return ramoverlay();
449 }
450
451 return -1;
452 }