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