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