finally move buildroot-ng to trunk
[openwrt.git] / target / linux / generic-2.6 / patches / 205-block2mtd_fix.patch
1 diff -urN linux.old/drivers/mtd/devices/block2mtd.c linux.dev/drivers/mtd/devices/block2mtd.c
2 --- linux.old/drivers/mtd/devices/block2mtd.c   2006-07-29 19:53:54.000000000 +0200
3 +++ linux.dev/drivers/mtd/devices/block2mtd.c   2006-07-29 19:47:03.000000000 +0200
4 @@ -4,11 +4,10 @@
5   * block2mtd.c - create an mtd from a block device
6   *
7   * Copyright (C) 2001,2002     Simon Evans <spse@secret.org.uk>
8 - * Copyright (C) 2004,2005     Jörn Engel <joern@wh.fh-wedel.de>
9 + * Copyright (C) 2004-2006     Jörn Engel <joern@wh.fh-wedel.de>
10   *
11   * Licence: GPL
12   */
13 -#include <linux/config.h>
14  #include <linux/module.h>
15  #include <linux/fs.h>
16  #include <linux/blkdev.h>
17 @@ -19,6 +18,7 @@ #include <linux/init.h>
18  #include <linux/mtd/mtd.h>
19  #include <linux/buffer_head.h>
20  #include <linux/mutex.h>
21 +#include <linux/mount.h>
22  
23  #define VERSION "$Revision: 1.30 $"
24  
25 @@ -62,10 +61,8 @@
26         read_lock_irq(&mapping->tree_lock);
27         for (i = 0; i < PAGE_READAHEAD; i++) {
28                 pagei = index + i;
29 -               if (pagei > end_index) {
30 -                       INFO("Overrun end of disk in cache readahead\n");
31 +               if (pagei > end_index)
32                         break;
33 -               }
34                 page = radix_tree_lookup(&mapping->page_tree, pagei);
35                 if (page && (!i))
36                         break;
37 @@ -237,6 +237,8 @@ static int _block2mtd_write(struct block
38         }
39         return 0;
40  }
41 +
42 +
43  static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
44                 size_t *retlen, const u_char *buf)
45  {
46 @@ -300,6 +302,19 @@ static struct block2mtd_dev *add_device(
47  
48         /* Get a handle on the device */
49         bdev = open_bdev_excl(devname, O_RDWR, NULL);
50 +#ifndef MODULE
51 +       if (IS_ERR(bdev)) {
52 +
53 +               /* We might not have rootfs mounted at this point. Try
54 +                  to resolve the device name by other means. */
55 +
56 +               dev_t dev = name_to_dev_t(devname);
57 +               if (dev != 0) {
58 +                       bdev = open_by_devnum(dev, FMODE_WRITE | FMODE_READ);
59 +               }
60 +       }
61 +#endif
62 +
63         if (IS_ERR(bdev)) {
64                 ERROR("error: cannot open device %s", devname);
65                 goto devinit_err;
66 @@ -331,7 +347,6 @@ static struct block2mtd_dev *add_device(
67         dev->mtd.writev = default_mtd_writev;
68         dev->mtd.sync = block2mtd_sync;
69         dev->mtd.read = block2mtd_read;
70 -       dev->mtd.readv = default_mtd_readv;
71         dev->mtd.priv = dev;
72         dev->mtd.owner = THIS_MODULE;
73  
74 @@ -351,6 +366,12 @@ devinit_err:
75  }
76  
77  
78 +/* This function works similar to reguler strtoul.  In addition, it
79 + * allows some suffixes for a more human-readable number format:
80 + * ki, Ki, kiB, KiB    - multiply result with 1024
81 + * Mi, MiB             - multiply result with 1024^2
82 + * Gi, GiB             - multiply result with 1024^3
83 + */
84  static int ustrtoul(const char *cp, char **endp, unsigned int base)
85  {
86         unsigned long result = simple_strtoul(cp, endp, base);
87 @@ -359,11 +380,16 @@ static int ustrtoul(const char *cp, char
88                 result *= 1024;
89         case 'M':
90                 result *= 1024;
91 +       case 'K':
92         case 'k':
93                 result *= 1024;
94         /* By dwmw2 editorial decree, "ki", "Mi" or "Gi" are to be used. */
95 -               if ((*endp)[1] == 'i')
96 -                       (*endp) += 2;
97 +               if ((*endp)[1] == 'i') {
98 +                       if ((*endp)[2] == 'B')
99 +                               (*endp) += 3;
100 +                       else
101 +                               (*endp) += 2;
102 +               }
103         }
104         return result;
105  }
106 @@ -383,26 +409,6 @@ static int parse_num(size_t *num, const 
107  }
108  
109  
110 -static int parse_name(char **pname, const char *token, size_t limit)
111 -{
112 -       size_t len;
113 -       char *name;
114 -
115 -       len = strlen(token) + 1;
116 -       if (len > limit)
117 -               return -ENOSPC;
118 -
119 -       name = kmalloc(len, GFP_KERNEL);
120 -       if (!name)
121 -               return -ENOMEM;
122 -
123 -       strcpy(name, token);
124 -
125 -       *pname = name;
126 -       return 0;
127 -}
128 -
129 -
130  static inline void kill_final_newline(char *str)
131  {
132         char *newline = strrchr(str, '\n');
133 @@ -416,9 +422,16 @@ #define parse_err(fmt, args...) do {               \
134         return 0;                               \
135  } while (0)
136  
137 -static int block2mtd_setup(const char *val, struct kernel_param *kp)
138 +#ifndef MODULE
139 +static int block2mtd_init_called = 0;
140 +static __initdata char block2mtd_paramline[80 + 12]; /* 80 for device, 12 for erase size */
141 +#endif
142 +
143 +
144 +static int block2mtd_setup2(const char *val)
145  {
146 -       char buf[80+12], *str=buf; /* 80 for device, 12 for erase size */
147 +       char buf[80 + 12]; /* 80 for device, 12 for erase size */
148 +       char *str = buf;
149         char *token[2];
150         char *name;
151         size_t erase_size = PAGE_SIZE;
152 @@ -430,7 +443,7 @@ static int block2mtd_setup(const char *v
153         strcpy(str, val);
154         kill_final_newline(str);
155  
156 -       for (i=0; i<2; i++)
157 +       for (i = 0; i < 2; i++)
158                 token[i] = strsep(&str, ",");
159  
160         if (str)
161 @@ -439,18 +452,16 @@ static int block2mtd_setup(const char *v
162         if (!token[0])
163                 parse_err("no argument");
164  
165 -       ret = parse_name(&name, token[0], 80);
166 -       if (ret == -ENOMEM)
167 -               parse_err("out of memory");
168 -       if (ret == -ENOSPC)
169 -               parse_err("name too long");
170 -       if (ret)
171 -               return 0;
172 +       name = token[0];
173 +       if (strlen(name) + 1 > 80)
174 +               parse_err("device name too long");
175  
176         if (token[1]) {
177                 ret = parse_num(&erase_size, token[1]);
178 -               if (ret)
179 +               if (ret) {
180 +                       kfree(name);
181                         parse_err("illegal erase size");
182 +               }
183         }
184  
185         add_device(name, erase_size);
186 @@ -459,13 +470,48 @@ static int block2mtd_setup(const char *v
187  }
188  
189  
190 +static int block2mtd_setup(const char *val, struct kernel_param *kp)
191 +{
192 +#ifdef MODULE
193 +       return block2mtd_setup2(val);
194 +#else
195 +       /* If more parameters are later passed in via
196 +          /sys/module/block2mtd/parameters/block2mtd
197 +          and block2mtd_init() has already been called,
198 +          we can parse the argument now. */
199 +
200 +       if (block2mtd_init_called)
201 +               return block2mtd_setup2(val);
202 +
203 +       /* During early boot stage, we only save the parameters
204 +          here. We must parse them later: if the param passed
205 +          from kernel boot command line, block2mtd_setup() is
206 +          called so early that it is not possible to resolve
207 +          the device (even kmalloc() fails). Deter that work to
208 +          block2mtd_setup2(). */
209 +
210 +       strlcpy(block2mtd_paramline, val, sizeof(block2mtd_paramline));
211 +
212 +       return 0;
213 +#endif
214 +}
215 +
216 +
217  module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
218  MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
219  
220  static int __init block2mtd_init(void)
221  {
222 +       int ret = 0;
223         INFO("version " VERSION);
224 -       return 0;
225 +
226 +#ifndef MODULE
227 +       if (strlen(block2mtd_paramline))
228 +               ret = block2mtd_setup2(block2mtd_paramline);
229 +       block2mtd_init_called = 1;
230 +#endif
231 +
232 +       return ret;
233  }
234  
235