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