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