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