kernel: add support for 3.9-rc2
[openwrt/openwrt.git] / target / linux / generic / patches-3.9 / 400-rootfs_split.patch
1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -23,6 +23,14 @@ config MTD_TESTS
4 WARNING: some of the tests will ERASE entire MTD device which they
5 test. Do not use these tests unless you really know what you do.
6
7 +config MTD_ROOTFS_ROOT_DEV
8 + bool "Automatically set 'rootfs' partition to be root filesystem"
9 + default y
10 +
11 +config MTD_ROOTFS_SPLIT
12 + bool "Automatically split 'rootfs' partition for squashfs"
13 + default y
14 +
15 config MTD_REDBOOT_PARTS
16 tristate "RedBoot partition table parsing"
17 ---help---
18 --- a/drivers/mtd/mtdpart.c
19 +++ b/drivers/mtd/mtdpart.c
20 @@ -29,6 +29,8 @@
21 #include <linux/kmod.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/partitions.h>
24 +#include <linux/root_dev.h>
25 +#include <linux/magic.h>
26 #include <linux/err.h>
27
28 #include "mtdcore.h"
29 @@ -50,7 +52,7 @@ struct mtd_part {
30 * the pointer to that structure with this macro.
31 */
32 #define PART(x) ((struct mtd_part *)(x))
33 -
34 +#define IS_PART(mtd) (mtd->_read == part_read)
35
36 /*
37 * MTD methods which simply translate the effective address and pass through
38 @@ -613,6 +615,155 @@ int mtd_del_partition(struct mtd_info *m
39 }
40 EXPORT_SYMBOL_GPL(mtd_del_partition);
41
42 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
43 +#define ROOTFS_SPLIT_NAME "rootfs_data"
44 +#define ROOTFS_REMOVED_NAME "<removed>"
45 +
46 +struct squashfs_super_block {
47 + __le32 s_magic;
48 + __le32 pad0[9];
49 + __le64 bytes_used;
50 +};
51 +
52 +
53 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
54 +{
55 + struct squashfs_super_block sb;
56 + int len, ret;
57 +
58 + ret = mtd_read(master, offset, sizeof(sb), &len, (void *) &sb);
59 + if (ret || (len != sizeof(sb))) {
60 + printk(KERN_ALERT "split_squashfs: error occured while reading "
61 + "from \"%s\"\n", master->name);
62 + return -EINVAL;
63 + }
64 +
65 + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
66 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
67 + master->name);
68 + *split_offset = 0;
69 + return 0;
70 + }
71 +
72 + if (le64_to_cpu((sb.bytes_used)) <= 0) {
73 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
74 + master->name);
75 + *split_offset = 0;
76 + return 0;
77 + }
78 +
79 + len = (u32) le64_to_cpu(sb.bytes_used);
80 + len += (offset & 0x000fffff);
81 + len += (master->erasesize - 1);
82 + len &= ~(master->erasesize - 1);
83 + len -= (offset & 0x000fffff);
84 + *split_offset = offset + len;
85 +
86 + return 0;
87 +}
88 +
89 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part)
90 +{
91 + struct mtd_partition *dpart;
92 + struct mtd_part *slave = NULL;
93 + struct mtd_part *spart;
94 + int ret, split_offset = 0;
95 +
96 + spart = PART(rpart);
97 + ret = split_squashfs(master, spart->offset, &split_offset);
98 + if (ret)
99 + return ret;
100 +
101 + if (split_offset <= 0)
102 + return 0;
103 +
104 + dpart = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
105 + if (dpart == NULL) {
106 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
107 + ROOTFS_SPLIT_NAME);
108 + return -ENOMEM;
109 + }
110 +
111 + memcpy(dpart, part, sizeof(*part));
112 + dpart->name = (unsigned char *)&dpart[1];
113 + strcpy(dpart->name, ROOTFS_SPLIT_NAME);
114 +
115 + dpart->size = rpart->size - (split_offset - spart->offset);
116 + dpart->offset = split_offset;
117 +
118 + if (dpart == NULL)
119 + return 1;
120 +
121 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
122 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
123 +
124 + slave = allocate_partition(master, dpart, 0, split_offset);
125 + if (IS_ERR(slave))
126 + return PTR_ERR(slave);
127 + mutex_lock(&mtd_partitions_mutex);
128 + list_add(&slave->list, &mtd_partitions);
129 + mutex_unlock(&mtd_partitions_mutex);
130 +
131 + add_mtd_device(&slave->mtd);
132 +
133 + rpart->split = &slave->mtd;
134 +
135 + return 0;
136 +}
137 +
138 +static int refresh_rootfs_split(struct mtd_info *mtd)
139 +{
140 + struct mtd_partition tpart;
141 + struct mtd_part *part;
142 + char *name;
143 + //int index = 0;
144 + int offset, size;
145 + int ret;
146 +
147 + part = PART(mtd);
148 +
149 + /* check for the new squashfs offset first */
150 + ret = split_squashfs(part->master, part->offset, &offset);
151 + if (ret)
152 + return ret;
153 +
154 + if ((offset > 0) && !mtd->split) {
155 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
156 + /* if we don't have a rootfs split partition, create a new one */
157 + tpart.name = (char *) mtd->name;
158 + tpart.size = mtd->size;
159 + tpart.offset = part->offset;
160 +
161 + return split_rootfs_data(part->master, &part->mtd, &tpart);
162 + } else if ((offset > 0) && mtd->split) {
163 + /* update the offsets of the existing partition */
164 + size = mtd->size + part->offset - offset;
165 +
166 + part = PART(mtd->split);
167 + part->offset = offset;
168 + part->mtd.size = size;
169 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
170 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
171 + (u32) part->offset, (u32) part->mtd.size);
172 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
173 + strcpy(name, ROOTFS_SPLIT_NAME);
174 + part->mtd.name = name;
175 + } else if ((offset <= 0) && mtd->split) {
176 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
177 +
178 + /* mark existing partition as removed */
179 + part = PART(mtd->split);
180 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
181 + strcpy(name, ROOTFS_REMOVED_NAME);
182 + part->mtd.name = name;
183 + part->offset = 0;
184 + part->mtd.size = 0;
185 + }
186 +
187 + return 0;
188 +}
189 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
190 +
191 /*
192 * This function, given a master MTD object and a partition table, creates
193 * and registers slave MTD objects which are bound to the master according to
194 @@ -629,6 +780,9 @@ int add_mtd_partitions(struct mtd_info *
195 struct mtd_part *slave;
196 uint64_t cur_offset = 0;
197 int i;
198 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
199 + int ret;
200 +#endif
201
202 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
203
204 @@ -643,12 +797,53 @@ int add_mtd_partitions(struct mtd_info *
205
206 add_mtd_device(&slave->mtd);
207
208 + if (!strcmp(parts[i].name, "rootfs")) {
209 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
210 + if (ROOT_DEV == 0) {
211 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
212 + "set to be root filesystem\n");
213 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
214 + }
215 +#endif
216 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
217 + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
218 + /* if (ret == 0)
219 + * j++; */
220 +#endif
221 + }
222 +
223 cur_offset = slave->offset + slave->mtd.size;
224 }
225
226 return 0;
227 }
228
229 +int mtd_device_refresh(struct mtd_info *mtd)
230 +{
231 + int ret = 0;
232 +
233 + if (IS_PART(mtd)) {
234 + struct mtd_part *part;
235 + struct mtd_info *master;
236 +
237 + part = PART(mtd);
238 + master = part->master;
239 + if (master->refresh_device)
240 + ret = master->refresh_device(master);
241 + }
242 +
243 + if (!ret && mtd->refresh_device)
244 + ret = mtd->refresh_device(mtd);
245 +
246 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
247 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
248 + refresh_rootfs_split(mtd);
249 +#endif
250 +
251 + return 0;
252 +}
253 +EXPORT_SYMBOL_GPL(mtd_device_refresh);
254 +
255 static DEFINE_SPINLOCK(part_parser_lock);
256 static LIST_HEAD(part_parsers);
257
258 --- a/drivers/mtd/mtdchar.c
259 +++ b/drivers/mtd/mtdchar.c
260 @@ -1012,6 +1012,12 @@ static int mtdchar_ioctl(struct file *fi
261 break;
262 }
263
264 + case MTDREFRESH:
265 + {
266 + ret = mtd_device_refresh(mtd);
267 + break;
268 + }
269 +
270 default:
271 ret = -ENOTTY;
272 }
273 --- a/include/linux/mtd/mtd.h
274 +++ b/include/linux/mtd/mtd.h
275 @@ -114,6 +114,7 @@ struct nand_ecclayout {
276
277 struct module; /* only needed for owner field in mtd_info */
278
279 +struct mtd_info;
280 struct mtd_info {
281 u_char type;
282 uint32_t flags;
283 @@ -226,6 +227,9 @@ struct mtd_info {
284 int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
285 int (*_suspend) (struct mtd_info *mtd);
286 void (*_resume) (struct mtd_info *mtd);
287 + int (*refresh_device)(struct mtd_info *mtd);
288 + struct mtd_info *split;
289 +
290 /*
291 * If the driver is something smart, like UBI, it may need to maintain
292 * its own reference counting. The below functions are only for driver.
293 @@ -368,6 +372,7 @@ extern int mtd_device_parse_register(str
294 int defnr_parts);
295 #define mtd_device_register(master, parts, nr_parts) \
296 mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
297 +extern int mtd_device_refresh(struct mtd_info *master);
298 extern int mtd_device_unregister(struct mtd_info *master);
299 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
300 extern int __get_mtd_device(struct mtd_info *mtd);
301 --- a/include/linux/mtd/partitions.h
302 +++ b/include/linux/mtd/partitions.h
303 @@ -36,12 +36,14 @@
304 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
305 */
306
307 +struct mtd_partition;
308 struct mtd_partition {
309 char *name; /* identifier string */
310 uint64_t size; /* partition size */
311 uint64_t offset; /* offset within the master MTD space */
312 uint32_t mask_flags; /* master MTD flags to mask out for this partition */
313 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */
314 + int (*refresh_partition)(struct mtd_info *);
315 };
316
317 #define MTDPART_OFS_RETAIN (-3)
318 --- a/include/uapi/mtd/mtd-abi.h
319 +++ b/include/uapi/mtd/mtd-abi.h
320 @@ -202,6 +202,7 @@ struct otp_info {
321 * without OOB, e.g., NOR flash.
322 */
323 #define MEMWRITE _IOWR('M', 24, struct mtd_write_req)
324 +#define MTDREFRESH _IO('M', 50)
325
326 /*
327 * Obsolete legacy interface. Keep it in order not to break userspace