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