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