skip ubi volume detection if ubifs is not present (fall back to mtd in that case)
[project/fstools.git] / libfstools / jffs2.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 void
36 foreachdir(const char *dir, int (*cb)(const char*))
37 {
38         char globdir[256];
39         glob_t gl;
40         int j;
41
42         if (dir[strlen(dir) - 1] == '/')
43                 snprintf(globdir, 256, "%s*", dir);
44         else
45                 snprintf(globdir, 256, "%s/*", dir);
46
47         if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
48                 for (j = 0; j < gl.gl_pathc; j++)
49                         foreachdir(gl.gl_pathv[j], cb);
50
51         cb(dir);
52 }
53
54 static int
55 jffs2_mount(void)
56 {
57         struct volume *v;
58
59         if (mkdir("/tmp/overlay", 0755)) {
60                 fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
61                 return -1;
62         }
63
64         v = volume_find("rootfs_data");
65         if (!v) {
66                 fprintf(stderr, "rootfs_data does not exist\n");
67                 return -1;
68         }
69
70         if (mount(v->blk, "/tmp/overlay", "jffs2", MS_NOATIME, NULL)) {
71                 fprintf(stderr, "failed to mount -t jffs2 %s /tmp/overlay: %s\n", v->blk, strerror(errno));
72                 return -1;
73         }
74
75         return volume_init(v);
76 }
77
78 static int
79 switch2jffs(void)
80 {
81         struct volume *v = volume_find("rootfs_data");
82         struct stat s;
83         int ret;
84
85         if (!stat(SWITCH_JFFS2, &s)) {
86                 fprintf(stderr, "jffs2 switch already running\n");
87                 return -1;
88         }
89
90         if (!v) {
91                 fprintf(stderr, "no rootfs_data was found\n");
92                 return -1;
93         }
94
95         creat("/tmp/.switch_jffs2", 0600);
96         ret = mount(v->blk, "/rom/overlay", "jffs2", MS_NOATIME, NULL);
97         unlink("/tmp/.switch_jffs2");
98         if (ret) {
99                 fprintf(stderr, "failed - mount -t jffs2 %s /rom/overlay: %s\n", v->blk, strerror(errno));
100                 return -1;
101         }
102
103         if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
104                 fprintf(stderr, "failed - mount -o remount,ro none: %s\n", strerror(errno));
105                 return -1;
106         }
107
108         system("cp -a /tmp/root/* /rom/overlay");
109
110         if (pivot("/rom", "/mnt")) {
111                 fprintf(stderr, "failed - pivot /rom /mnt: %s\n", strerror(errno));
112                 return -1;
113         }
114
115         if (mount_move("/mnt", "/tmp/root", "")) {
116                 fprintf(stderr, "failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
117                 return -1;
118         }
119
120         return fopivot("/overlay", "/rom");
121 }
122
123 int
124 handle_whiteout(const char *dir)
125 {
126         struct stat s;
127         char link[256];
128         ssize_t sz;
129         struct dirent **namelist;
130         int n;
131
132         n = scandir(dir, &namelist, NULL, NULL);
133
134         if (n < 1)
135                 return -1;
136
137         while (n--) {
138                 char file[256];
139
140                 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
141                 if (!lstat(file, &s) && S_ISLNK(s.st_mode)) {
142                         sz = readlink(file, link, sizeof(link) - 1);
143                         if (sz > 0) {
144                                 char *orig;
145
146                                 link[sz] = '\0';
147                                 orig = strstr(&file[1], "/");
148                                 if (orig && !strcmp(link, "(overlay-whiteout)"))
149                                         unlink(orig);
150                         }
151                 }
152                 free(namelist[n]);
153         }
154         free(namelist);
155
156         return 0;
157 }
158
159 int
160 jffs2_switch(int argc, char **argv)
161 {
162         struct volume *v;
163         char *mp;
164         int ret = -1;
165
166         if (find_mount_overlay("overlayfs:/tmp/root"))
167                 return -1;
168
169         if (find_filesystem("overlay")) {
170                 fprintf(stderr, "overlayfs not found\n");
171                 return ret;
172         }
173
174         v = volume_find("rootfs_data");
175         mp = find_mount_point(v->blk, NULL);
176         if (mp) {
177                 fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", v->blk, mp);
178                 return -1;
179         }
180
181         switch (volume_identify(v)) {
182         case FS_NONE:
183                 fprintf(stderr, "no jffs2 marker found\n");
184                 /* fall through */
185
186         case FS_DEADCODE:
187                 ret = switch2jffs();
188                 if (!ret) {
189                         fprintf(stderr, "doing fo cleanup\n");
190                         umount2("/tmp/root", MNT_DETACH);
191                         foreachdir("/overlay/", handle_whiteout);
192                 }
193                 break;
194
195         case FS_JFFS2:
196                 ret = jffs2_mount();
197                 if (ret)
198                         break;
199                 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
200                         fprintf(stderr, "switching to jffs2 failed\n");
201                         ret = -1;
202                 }
203                 break;
204         }
205
206         return ret;
207 }
208
209 static int mount_overlay_fs(void)
210 {
211         struct volume *v;
212
213         if (mkdir("/tmp/overlay", 0755)) {
214                 fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
215                 return -1;
216         }
217
218         v = volume_find("rootfs_data");
219         if (!v) {
220                 fprintf(stderr, "rootfs_data does not exist\n");
221                 return -1;
222         }
223
224         if (mount(v->blk, "/tmp/overlay", "jffs2", MS_NOATIME, NULL)) {
225                 fprintf(stderr, "failed to mount -t jffs2 %s /tmp/overlay: %s\n",
226                                 v->blk, strerror(errno));
227                 return -1;
228         }
229
230         volume_init(v);
231
232         return -1;
233 }
234
235 int mount_overlay(void)
236 {
237         struct volume *v = volume_find("rootfs_data");;
238         char *mp;
239
240         if (!v)
241                 return -1;
242
243         mp = find_mount_point(v->blk, NULL);
244         if (mp) {
245                 fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", v->blk, mp);
246                 return -1;
247         }
248
249         mount_overlay_fs();
250
251         extroot_prefix = "/tmp/overlay";
252         if (!mount_extroot()) {
253                 fprintf(stderr, "fs-state: switched to extroot\n");
254                 return 0;
255         }
256
257         fprintf(stderr, "switching to jffs2\n");
258         if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
259                 fprintf(stderr, "switching to jffs2 failed - fallback to ramoverlay\n");
260                 return ramoverlay();
261         }
262
263         return -1;
264 }