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