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