[kernel] fix mtd warnings
[openwrt/svn-archive/archive.git] / target / linux / generic / patches-2.6.37 / 400-rootfs_split.patch
1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -53,6 +53,16 @@ config MTD_PARTITIONS
4 devices. Partitioning on NFTL 'devices' is a different - that's the
5 'normal' form of partitioning used on a block device.
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 depends on MTD_PARTITIONS
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 @@ -618,6 +620,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 + strcpy((char *)&dpart[1], ROOTFS_SPLIT_NAME);
115 + dpart->name = (unsigned char *)&dpart[1];
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 @@ -633,7 +784,7 @@ int add_mtd_partitions(struct mtd_info *
197 {
198 struct mtd_part *slave;
199 uint64_t cur_offset = 0;
200 - int i;
201 + int i, ret;
202
203 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
204
205 @@ -648,6 +799,21 @@ int add_mtd_partitions(struct mtd_info *
206
207 add_mtd_device(&slave->mtd);
208
209 + if (!strcmp(parts[i].name, "rootfs")) {
210 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
211 + if (ROOT_DEV == 0) {
212 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
213 + "set to be root filesystem\n");
214 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
215 + }
216 +#endif
217 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
218 + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
219 + /* if (ret == 0)
220 + * j++; */
221 +#endif
222 + }
223 +
224 cur_offset = slave->offset + slave->mtd.size;
225 }
226
227 @@ -655,6 +821,32 @@ int add_mtd_partitions(struct mtd_info *
228 }
229 EXPORT_SYMBOL(add_mtd_partitions);
230
231 +int refresh_mtd_partitions(struct mtd_info *mtd)
232 +{
233 + int ret = 0;
234 +
235 + if (IS_PART(mtd)) {
236 + struct mtd_part *part;
237 + struct mtd_info *master;
238 +
239 + part = PART(mtd);
240 + master = part->master;
241 + if (master->refresh_device)
242 + ret = master->refresh_device(master);
243 + }
244 +
245 + if (!ret && mtd->refresh_device)
246 + ret = mtd->refresh_device(mtd);
247 +
248 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
249 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
250 + refresh_rootfs_split(mtd);
251 +#endif
252 +
253 + return 0;
254 +}
255 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
256 +
257 static DEFINE_SPINLOCK(part_parser_lock);
258 static LIST_HEAD(part_parsers);
259
260 --- a/drivers/mtd/mtdchar.c
261 +++ b/drivers/mtd/mtdchar.c
262 @@ -841,6 +841,13 @@ static int mtd_ioctl(struct file *file,
263 file->f_pos = 0;
264 break;
265 }
266 +#ifdef CONFIG_MTD_PARTITIONS
267 + case MTDREFRESH:
268 + {
269 + ret = refresh_mtd_partitions(mtd);
270 + break;
271 + }
272 +#endif
273
274 case OTPGETREGIONCOUNT:
275 case OTPGETREGIONINFO:
276 --- a/include/linux/mtd/mtd.h
277 +++ b/include/linux/mtd/mtd.h
278 @@ -125,6 +125,7 @@ struct nand_ecclayout {
279 struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
280 };
281
282 +struct mtd_info;
283 struct mtd_info {
284 u_char type;
285 uint32_t flags;
286 @@ -266,6 +267,9 @@ struct mtd_info {
287 struct device dev;
288 int usecount;
289
290 + int (*refresh_device)(struct mtd_info *mtd);
291 + struct mtd_info *split;
292 +
293 /* If the driver is something smart, like UBI, it may need to maintain
294 * its own reference counting. The below functions are only for driver.
295 * The driver may register its callbacks. These callbacks are not
296 --- a/include/linux/mtd/partitions.h
297 +++ b/include/linux/mtd/partitions.h
298 @@ -34,12 +34,14 @@
299 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
300 */
301
302 +struct mtd_partition;
303 struct mtd_partition {
304 - char *name; /* identifier string */
305 + const 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